Commit Graph

1129 Commits

Author SHA1 Message Date
Julien Fischer
db05403325 Writing 16- and 32-bit integers to binary file streams.
Add predicates for writing 16- and 32-bit integers to binary file streams in
native, little and big endian byte order to the standard library.

library/io.m:
    Add the new predicates.

library/uint16.m:
library/uint32.m:
    Add a functions for casting from the signed to unsigned versions
    of these types.

runtime/mercury_int.h:
    New header intended to hold things related to the implementation
    of the integer types in C grades.  Currently it contains the
    macros defining the byte reverse operations for 16- and 32-bit
    types.   (It will eventually contain macros for (un)boxing 64-bit
    integer types on 32-bit platforms as well.)

runtime/mercury_imp.h:
runtime/Mmakefile:
     Add the new header.

tests/hard_coded/Mmakefile:
tests/write_binary_multibyte_int.{m,exp}:
     Add a test for the new predicates.
2017-09-25 06:50:42 -04:00
Zoltan Somogyi
a064f80600 Fix a bug in tail recursive semidet predicates.
The bug was reported by Peter Wang.

compiler/ml_proc_gen.m:
    Ensure that the succeeded var is defined and initialized
    when the only code using it is a return statement.

tests/hard_coded/only_failing_goal_is_tail_rec.{m,exp}:
    A runnable version of Peter's test case.

tests/hard_coded/Mmakefile:
    Enable the new test case.
2017-09-23 04:45:29 +10:00
Julien Fischer
69dbd420d9 Add predicates for binary I/O of int8s and uint8s.
Add predicates for reading and writing int8s and uint8s with binary file
streams.

Document the predicates that are used to write fixed size integer types
to text file streams properly.

library/io.m:
     As above.

tests/hard_coded/Mmakefile:
tests/hard_coded/write_binary_uint8.{m,exp}:
tests/hard_coded/write_binary_int8.{m,exp}:
     Test the new predicates.
2017-09-21 10:52:26 -04:00
Julien Fischer
61b0860e2c Extend hard_coded/write_binary to cover fixed size integers.
tests/hard_coded/write_binary,{m,exp}:
     As above.
2017-09-15 10:13:52 -04:00
Julien Fischer
d146f53864 Extend hard_coded/write to cover fixed size integers.
tests/hard_coded/write.m:
tests/hard_coded/write.exp*:
    As above.

    Also, update the expected outputs for the non-C grades for the chnage in
    commit 253378f.
2017-09-15 09:56:23 -04:00
Julien Fischer
4126952a8c Test switches on uint{8,16,32}s.
tests/hard_coded/Mmakefile:
tests/hard_coded/uint{8,16,32}_switch_test.{m,exp}:
    As above.
2017-09-15 02:43:34 -04:00
Julien Fischer
9845ffdcb8 Add tests of integer -> int{8,16,32} conversion.
tests/hard_coded/Mmakefile:
tests/hard_coded/integer_int8_conv.{m,exp}:
tests/hard_coded/integer_int16_conv.{m,exp}:
tests/hard_coded/integer_int32_conv.{m,exp}:
    As above.
2017-09-14 09:23:53 -04:00
Julien Fischer
c399d66a43 Add tests of integer -> uint{8,16,32} conversion.
tests/hard_coded/Mmakefile:
tests/hard_coded/integer_uint8_conv.{m,exp}:
tests/hard_coded/integer_uint16_conv.{m,exp}:
tests/hard_coded/integer_uint32_conv.{m,exp}:
    As above.
2017-09-14 08:37:47 -04:00
Julien Fischer
2f39008fbf Add tests of int -> {int32,uint32} conversion.
Fix a bug in int -> uint32 conversion for the C grades.

tests/Mmakefile:
tests/hard_coded/from_int_int32.{m,exp}:
tests/hard_coded/from_int_uint32.m:
     Add the above tests.

tests/hard_coded/from_int_uint32.exp:
     Expected output for machines with a 64-bit int type.

tests/hard_coded/from_int_uint32.exp2:
     Expected output for machines with a 32-bit int type.

library/uint32.m:
     Fix a bug in the definition of from_int/2.  On machines where Mercury has
     a 64-bit int type the conversion should succeed for inputs up to (and
     including) UINT32_MAX.
2017-09-13 00:53:17 -04:00
Julien Fischer
32670e1536 Extend hard_coded/deconstruct_arg to cover fixed size integers.
tests/hard_coded/deconstruct_arg.m:
tests/hard_coded/deconstruct_arg.exp*:
     As above.
2017-09-11 01:36:27 -04:00
Julien Fischer
c78479c524 Extend hard_coded/construct_test to cover fixed size integers.
tests/hard_coded/construct_test.{m,exp}:
     As above.
2017-09-10 20:06:03 -04:00
Julien Fischer
49ce8494c1 Add missing univ-to-term conversion for new fixed integer types.
library/term_conversion.m:
    As above.

tests/hard_coded/type_to_term.{m,exp}:
    Extend this test to cover the 8-, 16- and 32-bit fixed size integer types.
2017-09-08 11:01:41 -04:00
Julien Fischer
307b74467f Add tests of conversion from int -> 8 and 16 bit integers.
tests/hard_coded/from_int_int{8,16}.{m,exp}:
tests/hard_coded/from_int_uint{8,16}.{m,exp}:
    As above.

tests/hard_coded/Mmakefile:
    Add the new tests.
2017-09-08 04:04:01 -04:00
Julien Fischer
9ba6fb4701 Fix an off-by-one error in the bitwise tests.
tests/hard_coded/bitwise_int*.m:
tests/hard_coded/uint_bitwise.m:
    Fix an off-by-one error in the C code that prints integers in
    their binary representation.
2017-09-08 01:33:50 -04:00
Julien Fischer
ca55327c27 Fix various problems with the new integer types and the Java backend.
compiler/mlds_to_java.m:
    Generate the correct for uint16 and uint8 right shifts: the
    existing code failed to account for the fact that Java will
    promote a byte or short that is the first operand of a shift
    to an int.

library/io.m:
    Add the missing Java definition of do_write_uint/5.

tests/hard_coded/test_int_hash.m:
    Translate the Java implementation of the hash function
    into C#.

tests/hard_coded/Mmakefile:
    Run the test_int_hash test in all grades.

test/hard_coded/bitwise_int.exp2:
    Add an expected output for backends where int is a 32-bit
    type.
2017-09-03 04:18:35 +10:00
Julien Fischer
0730d99a6e Reimplement the hash_pred for ints in Mercury.
library/hash_pred.m:
    As above: implementing this in Mercury means that we now have a working
    implementation for the non-C backends as well.

    Provide a link to the archived version of Thomas Wang's website.

tests/Mmakefile:
tests/hard_coded/test_int_hash.{m,exp}:
    Add a test the compares the above Mercury implementation to Ralph's
    original C implementation.
2017-09-03 01:25:57 +10:00
Zoltan Somogyi
9277d315d2 Test model_semi as well as model_det mutual tail recursion. 2017-08-31 22:16:58 +10:00
Julien Fischer
8e66e5b075 Change the semantics of the int shift operations.
Change int.(<<) and int.(>>) to throw an exception if their second operand is
not in [0, bits_per_int).  This brings them into line with the other integer
types.

As a transitional measure, add new functions that provide the old semantics.

library/int.m:
    As above.

NEWS:
    Announce the above change.

    Include the fixed size integer types in the list of reserved type names.

tests/hard_coded/shift_test.m:
     Call the legacy versions of the int shift operations.

tests/hard_coded/bitwise_int.{m,exp}:
     A new test of bitwise operations for the int type.
     (XXX I will add a 32-bit expected output separately.)

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
     Include the new test case.

     Do not warn about the use of the obsolete legacy shift operations.
2017-08-30 20:46:11 +10:00
Julien Fischer
5d3633f0ca Add tests of bitwise operations for fixed sized integers.
tests/hard_coded/Mmakefile:
tests/hard_coded/bitwise*.{m,exp}:
     As above.
2017-08-27 02:05:29 +10:00
Julien Fischer
bd9c845a44 Add tests of comparison operations for fixed size integers.
tests/hard_coded/cmp_int{8,16,32}.{m,exp}:
tests/hard_coded/cmp_uint{8,16,32}.{m,exp}:
     As above.

tests/hard_coded/Mmakefile:
     Add the new tests.
2017-08-26 12:07:18 +10:00
Julien Fischer
90cf8ee2f9 Add tests of basic arithmetic for 32-bit integers.
tests/hard_coded/Mmakefile:
tests/hard_coded/arith_int32.{m,exp}:
tests/hard_coded/arith-_uint32.{m,exp}:
     As above.
2017-08-22 22:05:24 +10:00
Julien Fischer
509e2156bc Add tests of basic arithmetic for 16-bit integers.
tests/hard_coded/arith_int16.{m,exp}:
tests/hard_coded/arith_uint16.{m,exp}:
tests/hard_coded/Mmakefile:
     As above.
2017-08-21 22:02:22 +10:00
Julien Fischer
28b9348b26 Add tests of basic arithmetic for 8-bit integers.
tests/hard_coded/arith_int8.{m,exp}:
tests/hard_coded/arith_uint8.{m,exp}:
tests/hard_coded/Mmakefile:
     As above.
2017-08-21 21:27:01 +10:00
Zoltan Somogyi
da9808036c Implement self-tail-call optimization in the MLDS code generator.
This is a step towards implementing not just self- but also mutual
tail recursion in the MLDS code generator.

compiler/options.m:
    Add an option, --optimize-tailcalls-codegen, that asks for MLDS backend
    to optimize self tail calls via the code generator, not ml_tailcall.m.
    (We still use ml_tailcall.m if the new option is not set.) The new option
    is set by default, but this can be changed if we find a problem
    with the new approach.

compiler/mark_tail_calls.m:
    Fix a bug. In a disjunction, the nonlast disjuncts cannot contain tail
    calls, because (a) if a nonlast disjunct is semidet, then after any
    such recursive call fails, we can still backtrack to later disjuncts,
    and (b) if the nonlast disjunct involved is det, the later disjuncts
    should have been optimized away, and the disjunct wouldn't be nonlast
    anymore.

    Export predicates that allow the MLDS backend to mark tail calls
    as *it* wants them marked.

    Reorganize the predicates that generate warnings to make them useable
    from the MLDS code generator as well, and export the required predicates.
    (The MLDS code generator needs this access because in some cases,
    a call that mark_tail_calls.m thinks is a tail call cannot be implemented
    as such. Since only the MLDS code generator knows this fact, only *it*
    can know when this warning may need to be generated.)

    Some of the reorganization of code that generates warnings factors out
    common code between mark_tail_calls.m and ml_tailcall.m.

compiler/ml_tailcall.m:
    Export some functionality for the code generator to use.

    Replace the found_recursive_call type, which used to be defined here,
    with the found_any_rec_calls type from mark_tail_calls.m, since they
    were isomorphic and had the same job.

    Delete the code that was factored out into mark_tail_calls.m.

compiler/mercury_compile_mlds_back_end.m:
    If --optimize-tailcalls-codegen is set, run the mark_tail_calls pass
    before MLDS codegen.

    Prepare for the code generator to generate warnings about calls that should
    be tail calls not actually being tail calls.

    Delete an unused exported predicate, and the imports it used to need.

compiler/mercury_compile_llds_back_end.m:
    Fix style.

compiler/ml_gen_info.m:
    Extend the code generator state with information needed for tail call
    optimization, and for generating warnings.

compiler/ml_proc_gen.m:
    When starting to generate code for a procedure, set up the new part
    of the code generator state with the information needed to handle tail
    calls, if we both (a) can optimize tail calls in the predicate, and (b)
    we have been asked to.

    After the code for the procedure body has been generated, *and* if
    we have actually turned some tail calls into jumps to the start of
    the procedure, create the wrapper around the MLDS code implementing
    the body goal that makes such jumps possible.

compiler/ml_call_gen.m:
    When generating code for a plain call, test whether it is a tail call,
    and if so, try to optimize it. If we fail, generate a warning about
    that fact, and fall back to generating code for it as we would do
    for any non-tail call.

    Provide mechanisms for this new code to compute the actual parameter
    lvals for the input arguments of a (tail) call, since only these
    have to be assigned from when replacing a tail call.

compiler/ml_code_util.m:
    Provide mechanisms for ml_proc_gen to compute the mlds_arguments
    of the formal parameters of just the input arguments of a procedure,
    since only these have be assigned to when replacing a tail call.

    (The predicates that perform these mirror image tasks should be
    next to each other, perhaps in a new ml_args_util.m module.)

compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/ml_optimize.m:
    Export to ml_call_gen.m (a slightly modified form of) a predicate
    that is used in the replacement of tail calls.

    Peephole optimization a pattern that we now generate.

compiler/rtti_to_mlds.m:
    Conform to the changes above.

tests/hard_coded/semi_tail_call_in_nonlast_disjunct.{m,exp}:
    Add a test case for the bug fixed in mark_tail_calls.m. Without the fix,
    the updated MLDS code generator's output fails this test.

tests/hard_coded/Mmakefile:
    Enable the new test case.
2017-08-07 19:33:33 +02:00
Zoltan Somogyi
3dc5650844 Implicitly rename apart vars occurring only in trace goals.
At the moment, the compiler rejects code like the following

     p(A, B, X) :-
         trace [io(!IO)] (
             ToPrint = A,
             io.format("A = %d\n", [i(ToPrint)], !IO)
         ),
         trace [io(!IO)] (
             ToPrint = B,
             io.format("B = %d\n", [i(ToPrint)], !IO)
         ),
         X = A + B,
         trace [io(!IO)] (
             ToPrint = X,
             io.format("X = %d\n", [i(ToPrint)], !IO)
         ).

because the three trace goals each attempt to bind a variable (ToPrint)
that is not local to the trace goal. The compiler already contains
several predicates that contain two or more trace goals whose local
variables (usually intermediates between the raw data -such as X-
and the actual task of the trace goal -such as the call to io.format-)
had to be renamed because of this. This is annoying, and the error message
the compiler generates without the rename is also surprising; it goes
against people's expectations.

Fix this violation of the law of least astonishment by automatically
and implicitly renaming apart variables in clauses that (a) occur
only in trace goals, and (b) occur in more than one trace goal.

compiler/pre_quantification.m:
    This new module contains code to wrap an existential quantification
    around the body of a trace goal if that trace goal contains one of
    the above variables. The actual renaming will be done by the immediately
    following invocation of quantification.

compiler/hlds.m:
compiler/notes/compiler_design.html:
    Mention the new module.

compiler/Mercury.options:
    Allow the new module to compile until the recent improvement to
    unused_imports.m is installed.

compiler/add_clause.m:
    Invoke the new module on clauses being added to the HLDS if they
    contain any trace goals. (Most don't, so we don't invoke the new
    module on them.)

compiler/goal_expr_to_goal.m:
    When translating a trace goal from the parse tree form to the HLDS form,
    record this fact.

compiler/qual_info.m:
    Add a flag to the qual_info to provide the space for this recording.

tests/hard_coded/dup_vars_in_trace_scopes_only.{m,exp}:
    A test case for the new capability.

tests/hard_coded/Mmakefile:
    Enable the new test case.
2017-07-03 23:55:22 +02:00
Julien Fischer
bfc5e2ed2f Fix term deconstruction for uints.
runtime/mercury_ml_expand_body.h:
library/rtti_implementation.m:
library/erlang_rtti_implementation.m:
    As above.

tests/hard_coded/deconstruct_arg.m:
     Add a test for uints.

     Avoid the use of a deprecated predicate.

tests/hard_coded/deconstruct_arg.{exp,exp2}:
     Update expected outputs for the above.
2017-06-18 21:16:46 +10:00
Julien Fischer
7c616b239c Extend hard_coded/construct_test to cover uints.
tests/hard_coded/construct_test.{m,exp}:
    As above.
2017-06-17 20:18:22 +10:00
Julien Fischer
787cee5d3d Extend type_to_term test to cover uints.
tests/hard_coded/type_to_term.{m,exp}:
    As above.
2017-06-17 20:07:17 +10:00
Zoltan Somogyi
05be9d1349 Fix a test case broken by the new modes of uint.xor. 2017-06-06 12:25:14 +02:00
Julien Fischer
e9b0c489d2 Two fixes for uint support.
1. Fix an abort in the MLDS backend involving switches on uints.

2. Make string.string/1 handle uints.

compiler/ml_switch_gen.m:
    Handle uints properly in a spot.

library/string.to_string.m:
     Make string.string/1 handle uint values.

tests/hard_coded/Mmakefile:
tests/hard_coded/uint_switch_test.{m,exp}:
     Add a test case.
2017-05-24 09:59:01 +10:00
Julien Fischer
1da3702b18 Avoid running a test in profdeep grades.
tests/hard_coded/Mmakefile:
    Do not run the test intermod_try_goal in deep profiling grades
    as they do not handle catching exceptions.
2017-05-21 02:31:22 +10:00
Julien Fischer
4421be3dbb Fix a test case failure in asm_fast.gc
tests/hard_coded/uint_bitwise.m:
    The C foreign procs in this module require the definition of
    ML_BITS_PER_UINT; add the required foreign_import_module pragma.
    (This currently "works" in many other grades because uint.mh gets
    implicitly imported.)
2017-05-21 01:07:24 +10:00
Julien Fischer
dd4b389f56 Implement compile time evaluation for uint operations.
compiler/const_prop.m:
    Extend this module to support compile time evaluation of uint operations.

compiler/uint_emu.m:
    A new module that emulates `uint' operations for the bits-per-uint in
    a manner similar to the int_emu module.

compiler/libs.m:
    Include the new module.

tests/hard_coded/constant_prop_1.{m,exp}:
tests/hard_coded/constant_prop_2.{m,exp}:
    Test compile time evaluation of uint operations.

    Update coding style in these tests.

tests/hard_coded/Mmakefile:
    Conform to the above changes.  (XXX the existing check
    for the Java grade doesn't actually work.)

    Delete left over IL and GCC backend stuff.
2017-05-14 02:40:26 +10:00
Julien Fischer
6330b51b61 Implement checked division for uints.
library/uint.m:
     Add (//)/2, (/)/2 and rem/2 for uints.

     Add mod/2 and div/2 as synonyms for the above.

tests/hard_coded/uint_arith.m
     Enable the division tests.

tests/hard_coded/uint_bitwise.exp*:
     Update the expected outputs.
2017-05-12 21:59:36 +10:00
Julien Fischer
f5769f0d58 Implement checked shifts for uints.
library/uint.m:
     Add checked left and right shift operations on uints.

tests/hard_coded/Mmakefile:
tests/hard_coded/uint_bitwise.m:
     Test bitwise operators on uints.

tests/hard_coded/uint_bitwise.exp:
     Expected output for 64-bit machines.

tests/hard_coded/uint_bitwise.exp2:
     Expected output for 32-bit machines (as well as the
     C# and Java grades).
2017-05-07 13:59:09 +10:00
Julien Fischer
5896264b9b Efficient insertion of duplicate items into bags.
library/bag.m:
    Add predicates and a function for efficiently inserting multiple copies of
    an item into a bag.

NEWS:
    Announce the above additions.

tests/hard_coded/bag_various.{m,exp}:
    Extend this test to cover the above.
2017-05-07 02:39:53 +10:00
Julien Fischer
0a7cf0f261 Add a test of basic arithmetic on uints.
tests/hard_coded/Mmakefile:
tests/hard_coded/uint_arith.m:
tests/hard_coded/uint_arith.exp*:
     As above.  (The .exp file is for 64-bit machines; the .exp2 for
     32-bit.)
2017-04-26 16:29:55 +10:00
Julien Fischer
fdb91d5aba Extend the lexer to recognise additional integer literals.
Extend the lexer to recognise uint literals, optional signedness suffixes on
ints and the literals for all of the proposed fixed size integer types.

Fix XXX UINTs in the library and compiler.

library/lexer.m:
    Uncomment the other alternatives in the integer_size/0 type.

    Handle signedness and size suffixes in integer literals.

library/parser.m
library/term.m:
    Conform to the above changes.

library/stream.string_writer.m:
    Fix an XXX UINT: make write handle uints properly.

library/term_io.m:
     Fix an XXX UINT: output integer signedness and size suffixes for
     integers when appropriate.

compiler/superhomogeneous.m:
     Print an error message if we encounter a fixed size integer literal
     as the rest of the compiler does not yet support them.

compiler/hlds_out_util.m:
compiler/parse_tree_out_info.m:
     Output the 'u' suffix on uint values.

test/hard_coded/lexer_zero.{m,inp,exp*}:
     Extend this test to cover zeros of varying signedness and size.

     Prefix each line of the output with the input line number of the
     token -- this makes it easier to relate the output back to the
     input.

tests/hard_coded/Mmakefile:
     Add the new test case.

tests/hard_coded/lexer_ints.{m,inp,exp}:
     Test the lexer on non-zero integer literals.
2017-04-26 10:00:45 +10:00
Julien Fischer
a56c4f5708 Merge integer tokens in lexer.
library/lexer.m:
     Merge the 'integer' and 'big_integer' tokens and extend them to include
     signedness and size information.  This conforms to recent changes to
     the rest of the system and is another step towards supporting additional
     types of integer literal.

library/parser.m:
mdbcomp/trace_counts.m:
     Conform to the above change.

tests/hard_coded/impl_def_lex.exp:
tests/hard_coded/impl_def_lex_string.exp:
tests/hard_coded/lexer_bigint.exp:
tests/hard_coded/lexer_zero.exp*:
tests/hard_coded/parse_number_from_string.exp*:
     Update these expected outputs.
2017-04-26 10:00:45 +10:00
Julien Fischer
e6e295a3cc Generalise the representation of integers in the term module.
In preparation for supporting uint literals and literals for the fixed size
integer types, generalise the representation of integers in the term module, so
that for every integer literal we record its base, value (as an arbitrary
precision integer), signedness and size (the latter two based on the literal's
suffix or lack thereof).

Have the lexer attach information about the integer base to machine sized ints;
we already did this for the 'big_integer' alternative but not the normal one.
In conjunction with the first change, this fixes a problem where the compiler
was accepting non-decimal integers in like arity specifications.  (The
resulting error messages could be improved, but that's a separate change.)

Support uints in more places; mark other places which require further work with
XXX UINT.

library/term.m:
     Generalise the representation of integer terms so that we can store
     the base, signedness and size of a integer along with its value.
     In the new design the value is always stored as an arbitrary precision
     integer so we no longer require the big_integer/2 alternative; delete it.

     Add some utility predicates that make it easier to work with integer terms.

library/term_conversion.m:
library/term_io.m:
     Conform to the above changes,

     Add missing handling for uints in some spots; add XXX UINT comments
     in others -- these will be addressed later.

library/lexer.m:
     Record the base of word sized integer literals.

library/parser.m:
compiler/analysis_file.m:
compiler/fact_table.m:
compiler/get_dependencies.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
compiler/make.module_dep_file.m:
compiler/parse_class.m:
compiler/parse_inst_mode_name.m:
compiler/parse_item.m:
compiler/parse_pragma.m:
compiler/parse_sym_name.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_to_term.m:
compiler/parse_type_defn.m:
compiler/parse_util.m:
compiler/prog_ctgc.m:
compiler/prog_util.m:
compiler/recompilation.check.m:
compiler/recompilation.version.m:
compiler/superhomogeneous.m:
mdbcomp/trace_counts.m:
samples/calculator2.m:
extras/moose/moose.m:
    Conform to the above changes.

tests/hard_coded/impl_def_lex.exp:
tests/hard_coded/impl_def_lex_string.exp:
tests/hard_coded/lexer_bigint.exp*:
tests/hard_coded/lexer_zero.exp*:
tests/hard_coded/parse_number_from_string.exp*:
tests/hard_coded/term_to_unit_test.exp:
    Update these expected outputs.
2017-04-22 11:53:14 +10:00
Julien Fischer
8f583d15fa Implement integer-to-uint and uint-to-integer conversion.
library/integer.m:
     As above -- I have upcoming changes that require this.
     (There's a bunch places marked XXX UINT that will need
     to revisited once more uint support is available.)

tests/Mmakefile:
tests/hard_coded/integer_uint_conv.{m,exp,exp2}:
      Add test for the conversions.
2017-04-17 00:54:01 +10:00
Zoltan Somogyi
d6c140af1f Allow inlining of linear mutual tail recursions.
Lets say an SCC contains n procedures, P1 through Pn. The SCC is *linearly
tail recursive* if the set of tail recursive calls in the SCC is {P1 -> P2,
P2 -> P3, ... Pn-1 -> Pn, Pn -> P1}, i.e. each procedure calls the next one
and the last one calls the first. For each Pi that is called from above
the SCC, the new optimization inlines the callee at every tail recursive
call site except the one that calls Pi itself. For example, if Pi is P1,
it would inline the tail call to P2, the tail call to P3 inside P2,
the tail call to P4 inside P3, and so on. Since the only tail recursive
call left in Pi is to Pi, this scheme transforms mutual tail recursion,
which the MLDS backend cannot (yet) implement, into self tail recursion,
which it *can* implement.

We only perform the transformation if each procedure in the SCC
contains *exactly one* tail recursive call. This is because each extra
tail recursive call may *double* the size of the resulting code.

Any recursive calls that are not *tail* recursive are left alone.

compiler/options.m:
doc/user_guide.texi:
    Add a new option, --inline-linear-tail-rec-sccs, that calls for the
    new transformation.

NEWS:
    Announce the new option.

compiler/mercury_compile_middle_passes.m:
    Call inlining if the new option is set.

compiler/inlining.m:
    If the new option is given, implement the transformation described
    at the top.

    Fix several variable and predicate names that misleadingly implied
    that a predicate *has* been inlined when it has only been decided
    that it is *worth* inlining, with the actual inlining taking place later.

    Pass the needed_map inside the inline_params.

    Rename the top predicate to fit the naming scheme used in the rest
    of the module.

compiler/hlds_goal.m:
    Add a goal feature that designates the call that it decorates as being
    either a self or a mutual tail recursive call.

    Rename the existing goal features that apply only to self tail recursive
    calls to make clear that fact.

compiler/mark_tail_calls.m:
    Generalize the code to look either for

    - just self tail recursive calls, as at the moment, as needed by both
      the LLDS and the MLDS code generator (for different purposes), or for
    - both self and mutual tail recursive calls, as needed by the new
      kind of inlining.

    Give the top level predicates names that indicate their overall purpose.

    Change the representation of the at_tail type to separate out the
    flag that says whether we have or haven't seen a recursive call
    in the backward traversal so far. This yields cleaner code
    in several places.

    Store the list of generated error_specs, and the flag that says whether
    the traversal has found any recursive call, in the mark_tail_calls_info
    structure, instead of returning them as separate output arguments.
    This is better, since *most* places don't add error_specs or set the flag.

compiler/dependency_graph.m:
    Explicitly gather the list of edges in the graph we are building,
    and record that list. Inlining now needs this, because it needs to know
    not just that e.g. two procedures p and q in an SCC call each other,
    also *from how many call sites". The dependency graph does not contain
    that information, but the list of edges from which it is built does.

compiler/hlds_dependency_graph.m:
    For most purposes, we want to add an edge from p to q if

    - p calls q in a tail call,
    - p calls q in a non-tail call, or
    - p *refers* to q without calling it, e.g. by constructing a closure
      whose procedure is q.

    However, when constructing the *tail* SCC within a conventional SCC,
    we want to add an edge from p to q only in the first situation.
    Add an option that controls whether we add an edge just in the first
    situation or all three.

compiler/ml_tailcall.m:
    ZZZ

compiler/call_gen.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/mercury_compile_llds_back_end.m:
compiler/saved_vars.m:
compiler/term_constr_main.m:
    Conform to the changes above.

tests/hard_coded/tail_rec_scc.{m,exp}:
    A new test case for the new option.

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
    Enable the new test case.
2017-03-21 19:32:00 +11:00
Julien Fischer
b3835dd826 Fix bug #430.
The lexer was not allowing underscores between leading zeros in decimal integer
literals and float literals (e.g. 0_0 or 0_0.0).

library/lexer.m:
     Allow underscores in the above cases.

tests/hard_coded/parse_number_from_io.{m,exp,exp2,exp3}:
tests/hard_coded/parse_number_from_string.{m,exp,exp2,exp3}:
     Extend these tests to cover the above cases.

tests/invalid/invalid_binary_literal.err_exp:
tests/invalid/invalid_octal_literal.err_exp:
tests/invalid/invalid_hex_literal.err_exp:
     Conform to the above change.
2017-01-29 14:38:26 +11:00
Julien Fischer
d8f0d402fe Document underscores in numeric literals.
Standardize terminology in error messages about ill-formed numeric literals.

doc/reference_manual.texi:
      Document underscores in numeric literals.

      Add a TODO comment about a future piece of work.

NEWS:
     Announce the addition of support for underscores in numeric literals.

library/lexer.m:
     Use the term "literal" instead of "constant" or "token" when
     referring to numeric literals in error messages.

     s/hex/hexadecimal/ in those same error messages.

tests/hard_coded/parse_number_from_string.exp*:
tests/invalid/invalid_{binary,decimal,hex,octal}_literal.err_exp:
     Conform to the above change in error messages from the lexer.
2017-01-16 16:24:48 +11:00
Julien Fischer
170e3e3d90 Fixes for underscores in numeric literals.
library/lexer.m:
    Add a missing predicate.

tests/hard_coded/parse_number_from_io.exp[23]:
tests/hard_coded/parse_number_from_string.exp[23]:
    Additional expected outputs for the Java and C# backends.
2017-01-12 01:10:31 +11:00
Julien Fischer
61c4ef7e50 Allow optional underscores in numeric literals.
Allow the optional use of underscores in numeric literals for the purpose of
improving their readability (e.g. by grouping digits etc).  We allow any number
of underscores between digits and also between the radix prefix (if present) and
the initial digit.  (When integer type suffixes are supported we will also
allow them to be preceded by any number of underscores.)  The following are
*not* allowed:

   1. Leading underscores.
   2. Trailing underscores.
   3. Underscores inside the components of a radix prefix (e.g.
      0_xffff or 0__b101010.)
   4. Underscores immediately adjacent to the decimal point in a float
      literal (e.g. 123_._123.)
   5. Underscores immediately adjacent to the exponent ('e' or 'E) in
      a float literal (e.g. 123_e12 or 123E_12.)
   6. Underscores immediately adjacent to the optional sign of an exponent
      in a float literal (e.g. 123_+e12 or 123-_E12.)
   7. Underscores between the optional sign of an exponent and the exponent
      indicator (e.g. 123+_e12.)

library/lexer.m:
    Modify the scanner to account of underscores in numeric literals according
    to the scheme above.

library/string.m:
library/integer.m:
    Export undocumented functions for converting strings containing underscores
    into ints or integers respectively.

tests/hard_coded/parse_number_from_io.{m,exp}:
     Test parsing of valid numeric literals from file streams.

tests/hard_coed/parse_number_from_string.{m,exp}:
     Test parsing of valid and invalid numeric literal from string.

tests/invalid/invalid_binary_literal.{m,err_exp}:
tests/invalid/invalid_decimal_literal.{m,err_exp}:
tests/invalid/invalid_octal_literal.{m,err_exp}:
tests/invalid/invalid_hex_literal.{m,err_exp}:
tests/invalid/invalid_float_literal.{m,err_exp}:
      Test parsing of invalid numeric literals from file streams.

tests/hard_coded/parse_number_from_{io,string}.m:
tests/hard_coded/parse_number_from_{io,string}.exp:
     Test parsing of valid numeric literals.

tests/hard_coded/Mmakefile:
tests/invalid/Mmakefile:
     Add the new test cases.
2017-01-12 01:10:31 +11:00
Zoltan Somogyi
ae90431ed4 Don't execute model_non fact tables in non-LLDS grades. 2016-12-19 09:26:52 +11:00
Zoltan Somogyi
18933e8ec1 Add more thorough tests of fact tables.
tests/hard_coded/fact_table_test_{1,2}.{m,exp}:
    Add these two test cases.

tests/hard_coded/fact_table_test_1_table_a:
tests/hard_coded/fact_table_test_1_table_b:
tests/hard_coded/fact_table_test_1_table_c:
tests/hard_coded/fact_table_test_2_table_a:
    The fact table files of the new test cases.

tests/hard_coded/Mmakefile:
    Enable the new test cases.
2016-12-16 08:01:29 +11:00
Zoltan Somogyi
0d0004c38c Modernize the style of the fact table tests. 2016-11-07 05:35:20 +11:00
Julien Fischer
8e8fc26209 Add a builtin unsigned word sized integer type -- Part 2.
Begin implementing library support for uints.

Update the compiler to use the uint type.

library/uint.m:
    Begin filling this module in.

library/private_builtin.m:
    Use the proper argument type for builtin_{unify,compare}_uint
    and provide actual implementations for them.

library/table_builtin.m:
    Add tabling builtins for uints.

library/string.m:
    Add a function to convert a uint to a decimal string.
    (XXX NYI for Erlang).

library/io.m:
    Add write_uint/[45].

    Add the stream instance for uints and text output streams.

library/stream.string_writer.m:
    Add put_uint/4.

    Support uints in string_writer.write etc.

library/pprint.m:
    Make uint an instance of the doc/1 type class.

library/pretty_printer.m:
    Add a default formatter for uints.

library/int.m:
    Unrelated change: fix formatting.

compiler/builtin_ops.m:
compiler/elds.m:
compiler/elds_to_erlang.m:
compiler/hlds_data.m:
compiler/llds.m:
compiler/llds_out_data.m:
compiler/mercury_to_mercury.m:
compiler/ml_lookup_switch.m:
compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/mlds_to_cs.m:
compiler/opt_debug.m
compiler/parse_tree_out.m:
compiler/parse_tree_out_info.m:
compiler/prog_data.m:
compiler/prog_out.m:
compiler/prog_rep.m:
compiler/hlds_out_util.m:
    Use the uint type in places where we should.

compiler/mlds_to_java.m:
    Fix a bug that causes us to generate badly typed Java.
    For div and mod we need to cast the entire expression to
    an int, not just the first operand.

compiler/c_util.m:
compiler/mlds_to_cs.m:
    Add predicates for outputting unsigned integers in C and C#.

tests/hard_coded/test_pretty_printer_defaults.exp:
    Conform to the above change to the pretty_printer module.
2016-10-31 10:31:05 +11:00