139 Commits

Author SHA1 Message Date
Julien Fischer
bad6ce0733 Update and fix more copyright notices.
compiler/*.m:
    As above.
2024-12-14 23:55:01 +11:00
Zoltan Somogyi
81b4c3272e Stop using digraph.tsort and digraph.atsort ...
... and start using their more descriptively-named synonyms.

In some cases, instead of getting and then reversing a list of sccs,
ask for the sccs in the needed order in the first place.

compiler/dependency_graph.m:
compiler/generate_dep_d_files.m:
compiler/make.program_target.m:
compiler/mlds_to_java_global.m:
compiler/mode_ordering.m:
compiler/rtti_to_mlds.m:
compiler/stratify.m:
    As above.

compiler/prog_event.m:
    As above.

    Address an old and now-inaccurate XXX. When a synthesized attribute
    depends on its value, either directly or through the values of other
    synthesized attributes, include the names of the attributes involved
    in the error message, instead of just saying that there is circular
    dependency *somewhere* among the attributes.

tests/invalid/synth_attr_error.err_exp:
    Expect the updated, more specific error messages.

tests/invalid/Mmakefile:
    Enable the synth_attr_error test case, which wasn't enabled before
    (though it does have an entry in Mercury.options.)

tests/invalid/invalid_event_spec:
tests/invalid/syntax_error_event_spec:
tests/invalid/synth_attr_error_spec:
    Replace tabs with spaces.
2023-06-22 00:25:26 +02:00
Zoltan Somogyi
b59f78c5fc Post-commit review of Paul's recent commit.
compiler/dependency_graph.m:
    Name the lambda predicate. Add module qualification.

compiler/mark_tail_calls.m:
    Use separate, independent options for warnings about non-tail recursion
    for self- and mutually-recursive calls not just in options.m, but in the
    internals of this module as well. This simplifies the code.

    Clarify the code for pragmas overriding the options.
2017-03-31 22:18:22 +11:00
Paul Bone
d4ef274451 Enable non-tail-call warnings for mutually-recursive code
This change enables warnings for mutual recursion for the low level C
backend.

--warn-non-tail-recursion currently only works for direct recursion.  This
change renames it to --warn-non-tail-recursion-self and adds
--warn-non-tail-recursion which is intended to produce warnings for self
and mutual recursion.

compiler/options.m:
    Add new option.

compiler/handle_options.m:
    Make --warn-non-tail-recursion imply --warn-non-tail-recursion-self.

compiler/ml_tailcall.m:
    Conform to changes in options.m.

compiler/mark_tail_calls.m:
    Simplify the code that interprets tail recursion warning options, this
    avoids the need for the warn_non_tail_rec_calls_opt which was only used
    here in an unnecessary step.  This logic has also been moved to a
    separate predicate.

    Add a warning for mutually recursive calls that are not tail recursive.

    Update these warning/error messages to say if the call is self or
    mutually recursive.  Also in the case of mutually recursive calls, name
    the callee.

compiler/mercury_compile_llds_back_end.m:
    Compute the dependency information before running the pre-LLDS passes.
    This makes sure that we do this once for the whole module, and
    independently of --trad-passes.

compiler/dependency_graph.m:
    Add a method for getting the SCC of a given node from a dependency_info
    structure.  The SCC is retrieved from a lazily built map.

doc/user_guide.texi:
    Document the changes.

tests/invalid/Mmakefile:
tests/invalid/require_tailrec_1.err_exp:
tests/invalid/require_tailrec_1.m:
tests/invalid/require_tailrec_2.err_exp:
tests/invalid/require_tailrec_2.m:
tests/invalid/require_tailrec_3.err_exp:
tests/invalid/require_tailrec_3.m:
tests/valid/Mmakefile:
tests/valid/require_tailrec_1.m:
tests/valid/require_tailrec_2.m:
tests/valid/require_tailrec_3.m:
    Test the tail recursion warnings a lot more extensively, some of these
    are new test programs, others just have many more test cases within
    them.

tests/invalid/Mercury.options:
tests/valid/Mercury.options:
    Add new test files.

    Disable most optimisations as these could optimise away the mutual tail
    recursions.

tests/EXPECT_FAIL_TESTS.hlc.gc:
    New test case failures.
2017-03-30 17:14:46 +11: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
Zoltan Somogyi
dea4368f7d Make each SCC in the dependency graph a set, not a list.
This is to make the data type follow the inherent semantics of SCCs
more closely, and enforce the invariant that a procedure can appear
in the SCC only once.

Also, rename the list of SCCs from "dependency_ordering", which does
not give a clue about *which way* the SCCs are ordered, to "bottom_up_sccs",
which does.

compiler/dependency_graph.m:
    Make the changes described above.

    Document why we reverse the list generated by digraph.atsort.

library/digraph.m:
    Document the order in which digraph.atsort returns the list of SCCs.

    Note that the last step of atsort is to reverse the list, which
    its caller in compiler/dependency_graph.m will then immediately
    re-reverse.

    Document the order in which digraph.tsort and digraph.dfs return
    a list of items.

    Give some variables more meaningful names, and make the argument order
    of some predicates conform to our conventions.

compiler/hlds_out_module.m:
    Add code to print out the dependency info in the module_info, if asked.

doc/user_guide.texi:
    Document the dump string option that asks for this.

compiler/hlds_dependency_graph.m:
    Make the same changes for hlds_dependency_info as dependency_graph.m
    did to just plain dependency_info.

compiler/hlds_pred.m:
    Make the scc type expand to a set, not a list, of pred_proc_ids.

compiler/dep_par_conj.m:
compiler/stratify.m:
    Conform to the changes above, and simplify some code.

compiler/closure_analysis.m:
compiler/ctgc.util.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/exception_analysis.m:
compiler/goal_util.m:
compiler/granularity.m:
compiler/inlining.m:
compiler/lco.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mode_constraints.m:
compiler/rbmm.interproc_region_lifetime.m:
compiler/rbmm.points_to_analysis.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_constr_data.m:
compiler/term_constr_errors.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_main.m:
compiler/term_constr_pass2.m:
compiler/term_constr_util.m:
compiler/term_errors.m:
compiler/term_pass1.m:
compiler/term_pass2.m:
compiler/term_util.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/tupling.m:
    Conform to the changes above.
2017-02-19 16:08:48 +11:00
Zoltan Somogyi
c0b2dd20d4 Post-commit review comments for the previous diff. 2017-02-16 16:02:57 +11:00
Paul Bone
e2ffb8d01a Reorganise dependency graph code
This change moves code that could be generic/common out of
hlds_dependency_graph.m into dependency_graph.m.  It also moves some code
from hlds_module.m into hlds_dependency_graph.m where it makes more sense.

compiler/dependency_graph.m:
    New module.

    Make the accessors predicates functions and give them more sensible
    names.

    Create a new accessors to get a version or the dependency ordering,
    condensed into a single list.

compiler/hlds_dependency_graph.m:
    Build the dependency_info structure in a more straightforward way.  It
    can easily be created with a single call rather than three.  The
    dependency ordering information is now calculated by dependency_graph.m.

    Make build_dependency_graph, build_proc_dependency_graph and
    build_pred_dependency_graph functions rather than predicates.

compiler/hlds_module.m:
    As above.

compiler/closure_analysis.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/dep_par_conj.m:
compiler/exception_analysis.m:
compiler/granularity.m:
compiler/hlds_dependency_graph.m:
compiler/inlining.m:
compiler/lco.m:
compiler/libs.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mode_constraints.m:
compiler/par_loop_control.m:
compiler/rbmm.interproc_region_lifetime.m:
compiler/rbmm.points_to_analysis.m:
compiler/stratify.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/tupling.m:
    Conform to changes.
2017-02-16 12:41:51 +11:00
Paul Bone
65171172fd Move dependency_graph module
Move the dependency_graph module from transform_hlds to the hlds parent
module.  Rename the dependency_graph.m file to hlds_dependency_graph.m

This change will allow us to move generic dependency_graph code to
dependency_graph.m.

compiler/dependency_graph.m -> hlds_dependency_graph.m:
compiler/hlds.m:
compiler/transform_hlds.m:
    As above.

compiler/closure_analysis.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/dep_par_conj.m:
compiler/exception_analysis.m:
compiler/granularity.m:
compiler/inlining.m:
compiler/lco.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_main.m:
compiler/mode_constraints.m:
compiler/par_loop_control.m:
compiler/rbmm.interproc_region_lifetime.m:
compiler/rbmm.points_to_analysis.m:
compiler/stratify.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/tupling.m:
    Conform to changes.
2017-02-15 22:05:49 +11:00
Julien Fischer
092e175f45 Add a builtin unsigned word sized integer type -- Part 1.
Add a new builtin type: uint, which is an unsigned word sized integer type.
Support for this new type will need be bootstrapped over several changes.
This is the first such change and does the following:

- Extends the compiler to recognize 'uint' as a builtin type.
- Extends the set of builtin operations to include relational and (some)
  arithmetic operations on uints.
- Extends all of the code generators to handle the above.  There are some
  limitations currently marked by 'XXX UINT'.  These will be lifted once
  the compiler recognised uint and additional library support becomes
  available.
- Extends the runtime to support uints.

compiler/prog_type.m:
compiler/prog_data.m:
compiler/builtin_lib_types.m:
    Recognize uint as a builtin type.

    Add a new alternative to the cons_id/0 type corresponding to the uint type
    -- for bootstrapping purposes its argument is currently an int.

compiler/builtin_ops.m:
    Add builtin relational and arithmetic operations on uints.  Note that the
    existing 'unsigned_le' operation is actually intended for use with signed
    values.  Rather than attempt to modify its meaning, I have just added new
    operations specific to the uint type.

compiler/hlds_data.m:
    Add a new tag type for uints.

compiler/type_ctor_info.m:
    Recognise uint as a builtin.

    Bump the RTTI version number here.

compiler/ctgc.selector.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/export.m:
compiler/foreign.m:
compiler/goal_util.m:
compiler/higher_order.m:
compiler/hlds_code_util.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/hlds_pred.m:
compiler/implementation_defined_literals.m:
compiler/inst_check.m:
compiler/mercury_to_mercury.m:
compiler/mode_util.m:
compiler/module_qual.qualify_items.m:
compiler/parse_tree_to_term.m:
compiler/parse_type_name.m:
compiler/polymorphism.m:
compiler/prog_out.m:
compiler/prog_rep.m:
compiler/prog_rep_tables.m:
compiler/prog_util.m:
compiler/rbmm.execution_path.m:
compiler/rtti.m:
compiler/special_pred.m:
compiler/switch_gen.m:
compiler/switch_util.m:
compiler/table_gen.m:
compiler/type_constraints.m:
compiler/type_util.m:
compiler/typecheck.m:
compiler/unify_gen.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
compiler/write_module_interface_files.m:
compiler/xml_documentation.m:
    Conform to the above changes to the parse tree and HLDS.

compiler/c_util.m:
    Support generating builtin operations for uints.

compiler/llds.m:
    Add a representation for uint constants to the LLDS.

    Map uints onto MR_Unsigned.

compiler/call_gen.m:
compiler/dupproc.m:
compiler/exprn_aux.m:
compiler/global_data.m:
compiler/jumpopt.m:
compiler/llds_out_data.m:
compiler/llds_out_instr.m:
compiler/opt_debug.m:
compiler/opt_util.m:
    Support uints in the LLDS code generator.

compiler/mlds.m:
     Support uint constants in the MLDS.

compiler/ml_accurate_gc.m:
compiler/ml_call_gen.m:
compiler/ml_global_data.m:
compiler/ml_simplify_switch.m:
compiler/ml_switch_gen.m:
compiler/ml_tailcall.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/ml_util.m:
compiler/rtti_to_mlds.m:
    Conform to the above change to the MLDS.

compiler/mlds_to_c.m:
compiler/mlds_to_java.m:
compiler/mlds_to_cs.m:
     Generate the appropriate target code for uint constants and uint
     relational operations.

compiler/bytecode.m:
compiler/bytecode_gen.m:
     Handle uints in the bytecode generator: we just abort if we
     encounter them for now.

compiler/elds.m:
compiler/elds_to_erlang.m:
compiler/erl_call_gen.m:
compiler/erl_code_util.m:
compiler/erl_rtti.m:
compiler/erl_unify_gen.m:
    Handle uints in the Erlang code generator.

library/private_builtin.m:
    Add placeholders for builtin_{unify,compare}_uint.  Since the
    bootstrapping compiler will not recognize uint as a type, we
    give them polymorphic arguments.  These can be replaced after
    this change has bootstrapped.

    Update the Java list of TypeCtorRep constants, which for some
    reason is defined here.

library/uint.m:
    New module that will eventually contain operations on uints.

library/MODULES_DOCS:
library/library.m:
     Add the uint module.

library/construct.m:
library/erlang_rtti_implementation.m:
library/rtti_implementation.m:
mdbcomp/program_representation.m:
     Handle uints.

deep_profiler/program_representation_utils.m:
     Conform to the above change.

runtime/mercury_dotnet.cs.in:
     Update the list of TypeCtorReps for C#

java/runtime/TypeCtorRep.java:
     Update this, although the actual TypeCtorRep constants
     are defined the library.

runtime/mercury_type_info.h:
    Bump the RTTI version number.

    Add an alternative for uints to the tyepctor rep enum.

runtime/mercury_builtin_types.{h,c}:
runtime/mercury_builtin_types_proc_layouts.h:
runtime/mercury_deconstruct.c:
runtime/mercury_deep_copy_body.h:
runtime/mercury_table_type_body.h:
runtime/mercury_tabling.h:
runtime/mercury_tabling_macros.h:
runtime/mercury_unify_compare_body.h:
    Add uint as a builtin type and handle it throughout the runtime.

runtime/mercury_grade.h:
    Bump the binary compatibility version.

runtime/mercury_term_size.c:
runtime/mercury_ml_expand_body.h:
    Handle uint and fix probable bugs with the handling of ints on
    64-bit Windows.
2016-10-24 12:55:35 +11:00
Zoltan Somogyi
3cb166eb07 Delete some unused predicates; add consider_used pragmas for some others. 2015-12-29 01:50:46 +11:00
Zoltan Somogyi
cc9912faa8 Don't import anything in packages.
Packages are modules whose only job is to serve as a container for submodules.
Modules like top_level.m, hlds.m, parse_tree.m and ll_backend.m are packages
in this (informal) sense.

Besides the include_module declarations for their submodules, most of the
packages in the compiler used to import some modules, mostly other packages
whose component modules their submodules may need. For example, ll_backend.m
used to import parse_tree.m. This meant that modules in the ll_backend package
did not have to import parse_tree.m before importing modules in the parse_tree
package.

However, this had a price. When we add a new module to the parse_tree package,
parse_tree.int would change, and this would require the recompilation of ALL
the modules in the ll_backend package, even the ones that did NOT import ANY
of the modules in the parse_tree package.

This happened even at one remove. Pretty much all modules in every one
of the backend have to import one or more modules in the hlds package,
and they therefore have import hlds.m. Since hlds.m imported transform_hlds.m,
any addition of a new middle pass to the transform_hlds package required
the recompilation of all backend modules, even in the usual case of the two
having nothing to do with each other.

This diff removes all import_module declarations from the packages,
and replaces them with import_module declarations in the modules that need
them. This includes only a SUBSET of their child modules and of the non-child
modules that import them.
2015-11-13 15:03:20 +11:00
Zoltan Somogyi
a828e36796 Convert (C->T;E) to (if C then T else E).
Avoid the use of semidet functions.
2015-10-18 06:57:19 +11:00
Zoltan Somogyi
753fe25c90 Put the later parts of .*opt files in a standard order.
compiler/intermod.m:
    We used to put predicate declarations and definitions into .opt files
    in order of pred_id. Since pred_ids are allocated sequentially, this
    meant that the order of the pragmas in .*opt files recording analysis
    results was determined by the order of the predicate declarations
    in the .m file, and if this changes, everything that depends on the
    .opt file has to be recompiled. Change this to put the pragmas into
    an order based on the predicates' names, arities, and pred_or_func flag.

    Precede each block of analysis results (one block per pragma type)
    with a blank line, to make .*opt files a bit easier to read by revealing
    their structure.

    When printing analysis results for all the procedures of a predicate,
    don't get a list of proc_ids and then look them up one by one; just
    iterated over all the proc_infos in the proc_table.

    Rename some predicates to make their names fit into the naming scheme
    used in the rest of the module.

compiler/hlds_module.m:
compiler/hlds_pred.m:
    The results of the exception, trailing and mm_tabling analyses
    used to be stored in the module_info as maps from pred_proc_ids
    to the results about the procedure. Change this to store them
    in the proc_info of the procedure, since any code that looks up
    the results of an analysis typically also wants to look up other
    information in the proc_info as well. (intermod.m certainly does.)

    The results of the termination, structure sharing and structure reuse
    analysis were already stored in the proc_info.

compiler/add_pragma.m:
    When reading in exceptions, trailing and mm_tabling pragmas,
    add their information to the named procedure's proc_info,
    not to a map in the module_info.

compiler/exception_analysis.m:
compiler/tabling_analysis.m:
compiler/trailing_analysis.m:
    Put analysis results into procedures' proc_infos, not into a map
    in the module_info, and if need be, look it up there as well.

compiler/goal_form.m:
    Look up the results of exception_analysis in procedures' proc_infos,
    not in a map in the module_info.

compiler/lco.m:
    Work around a bug in lco.m itself exposed by the move of analysis
    results to proc_infos. When lco.m duplicates the proc_info of a procedure
    that it optimizes, it now duplicates its analysis results as well.
    The duplication is correct in a sense, since any results of the exception,
    trailing and mm_tabling analysis that hold for the original procedure
    have to hold for the duplicate copy as well, but interestingly, this extra
    precision causes simplify to believe that the call from the original
    procedure to duplicated procedure is dead code that can be eliminated,
    since it is det and appears to have no outputs. In fact, it does have
    outputs, but it returns those outputs via impure calls to store_at_ref.
    While the call to store_at_ref in the duplicated procedure is marked
    as impure, the call to the duplicate procedure in the original procedure
    is NOT (yet) so marked.

compiler/parse_tree_out_pred_decl.m:
    When printing parts of predicate declarations, e.g. as parts of pragmas
    that record analysis results, don't take as an argument a context that
    will never be used.

compiler/add_clause.m:
compiler/dependency_graph.m:
compiler/hlds_out_pred.m:
compiler/make_hlds_error.m:
compiler/mode_errors.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_pragma.m:
compiler/typecheck.m:
    Conform to the changes in parse_tree_out_pred_decl.m above.

tests/term/*.trans_opt_exp:
    Expect the same termination analysis results as before, just in
    predicate name order, and with a blank line before each block.
2015-09-16 10:45:05 +10:00
Zoltan Somogyi
656493dfdf Use separate types for the status of different entity kinds.
We used the old import_status type to represent the status of six different
kinds of entities:

- types
- insts
- modes
- typeclasses
- instances
- predicates

even though some statuses that made sense for one kind of entity didn't for
another another (e.g. predicates can be pseudo imported/exported, but the
other five kinds of entities cannot).

Create the new types type_status, inst_status, ..., pred_status to represent
the status of these entities in the HLDS. For now, these are just wrappers
around the renamed old_import_status type, but I plan to replace them with
status types that *are* specialized to the applicable kind of entity,
along the lines of compiler/notes/status_proposal. This is a necessary
first step towards that proposal.

compiler/status.m:
    Define the six new entity-kind-specific status types, and replicate
    the test predicates that used to work on the import_status type
    to work on these instead.

    Define a status type, item_mercury_status, that contains just the info
    that is common to all entities in an item block, for use during
    the process of adding items to the HLDS.

    Move the predicates that converted section markers to statuses
    from here to make_hlds_passes.m, since that is the only place
    where they are used, or can be used.

    Move the combine_status predicate here from add_type.m, since
    it is needed for combining the statuses of other kinds of entities
    as well, not just types.

compiler/hlds_data.m:
    Change the HLDS types that record the information we have about
    types, du type fields, insts, modes, typeclasses and instances
    to have kind-specific status fields, instead of the old generic
    import_status type.

    Change the prefix on the field names of the hlds_instance_defn type
    to avoid a name clash, and to make them more meaningful.

    Change the prefix on the field names of the hlds_class_defn type
    to make them more meaningful.

compiler/hlds_pred.m:
    Change the HLDS type that records the information we have about predicates
    to have a kind-specific status field, instead of the old generic
    import_status type.

    Update the predicates that test predicate statuses accordingly.

compiler/hlds_module.m:
    Change the HLDS types that record the information we have about
    type constructors to be type_status, not the old generic import_status.

compiler/make_hlds_passes.m:
    As we process each item block, pass along an item_mercury_status
    instead of an import_status. The code used to use only a subset
    of the possible values of the import_status type, since we can never say
    that all the entities in an item block are e.g. pseudo-exported.
    An item_mercury_status has just the information we actually *know*
    about the item block as a whole. We convert the item_mercury_status
    to a kind-specific status if and when we need to, but for several purposes,
    the item_mercury_status is enough on its own.

    In a few cases, add a new predicate to do this conversion.

    Pass the need_qualifier flag separately from the status. It is needed
    in only a few places, but this was not apparent when we always passed it
    around paired with the import_status.

    Move the predicates that converted section markers to statuses
    to here from status.m, since here is the only place where they are used,
    or can be used.

compiler/add_class.m:
    Convert the statuses of typeclasses and instances to the statuses
    of the predicates implementing their virtual and concrete methods.

compiler/check_typeclass.m:
    Simplify some over-complex code.

compiler/add_special_pred.m:
    Convert the statuses of types to the statuses of the predicates
    implementing their unify, index, compare and solver init operations.
    Note some places where the process of this conversion is (to say the least)
    unclear and undocumented.

compiler/hlds_out_util.m:
    Provide utility predicates to print all the new kinds of statuses.
    These replace the old predicate that did the same in hlds_out_pred.m,
    but printing e.g. type statuses in hlds_out_pred doesn't seem right.

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

    Consistently use switches on the booleans returned by xxx_status_to_write,
    instead wrapping a semidet predicate around it and calling that.
    The switches yield code that is both smaller and more maintainable.

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

    Delete a simple wrapper predicate that was used only in one place.
    That place now does the wrapping itself.

compiler/qual_info.m:
    Replace the import_status field in the qual_info with a simple
    is_opt_imported/is_not_opt_imported flag, since that was the only
    thing we used the import_status field for.

compiler/accumulator.m:
compiler/add_clause.m:
compiler/add_foreign_enum.m:
compiler/add_foreign_proc.m:
compiler/add_mode.m:
compiler/add_mutable_aux_preds.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_pragma_type_spec.m:
compiler/add_pred.m:
compiler/add_solver.m:
compiler/add_type.m:
compiler/base_typeclass_info.m:
compiler/ctgc.util.m:
compiler/dead_proc_elim.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/det_report.m:
compiler/elds_to_erlang.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/export.m:
compiler/float_regs.m:
compiler/higher_order.m:
compiler/hlds_code_util.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/inst_check.m:
compiler/lambda.m:
compiler/lco.m:
compiler/make_hlds.m:
compiler/make_hlds_warn.m:
compiler/make_tags.m:
compiler/ml_proc_gen.m:
compiler/ml_type_gen.m:
compiler/mode_errors.m:
compiler/oisu_check.m:
compiler/par_loop_control.m:
compiler/polymorphism.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/prop_mode_constraints.m:
compiler/recompilation.usage.m:
compiler/simplify_proc.m:
compiler/smm_common.m:
compiler/special_pred.m:
compiler/ssdebug.m:
compiler/status.m:
compiler/stm_expand.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/superhomogeneous.m:
compiler/table_gen.m:
compiler/term_constr_initial.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trace_params.m:
compiler/type_class_info.m:
compiler/type_constraints.m:
compiler/type_ctor_info.m:
compiler/typecheck.m:
compiler/typecheck_info.m:
compiler/typeclasses.m:
compiler/unify_proc.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above.
2015-09-12 09:07:45 +10:00
Zoltan Somogyi
ea094b5bb7 Make the import_status type part of the HLDS.
The import_status type was defined in parse_tree.status.m, but it is
not actually used in the parse_tree package. It is used, extensively,
in the hlds package.

compiler/status.m:
compiler/prog_item.m:
compiler/prog_data.m:
    Move the parts of status.m that *are* needed in the parse_tree package
    to modules in that package. The section markers and the import_locn type
    are moved to prog_item.m, while the need_qualifier type is moved to
    prog_data.m.

compiler/parse_tree.m:
compiler/hlds.m:
    Switch the status.m module from being in the parse_tree package
    to being in the hlds package.

compiler/notes/compiler_design.html:
    Document the switch.

compiler/*.m:
    Update import_module declarations as needed after the above change.

    In some places, import parse_tree.prog_item as well as hlds.status,
    even if we are only intested in statuses, because the import_locn type,
    which part of some statuses, *is* used in the parse_tree package,
    and must therefore be defined there. These undesirable dependencies
    will go away when we implement the proposal for purpose-specific status
    types.
2015-09-09 01:47:08 +10:00
Zoltan Somogyi
bbb2535eb5 Break up mercury_to_mercury.m.
With almost 6000 lines, mercury_to_mercury.m was one of the biggest modules
of compiler, but it was far from cohesive. This diff carves seven new modules
out of it, each of which is much more cohesive. The stuff remaining in
mercury_to_mercury.m is still not as cohesive as one would like, but it is
now small enough that moving its individually-cohesive parts into modules
of their own would be overkill.

Three consequences of the old mercury_to_mercury.m's lack of cohesion
were that

- the order of predicate declarations often did not match the order of
  their implementation;
- related predicates were not grouped together;
- even when they were grouped together, the order of those groups
  was often random.

This diff fixes all three of these problems for all eight successor modules
of mercury_to_mercury.m: the seven new modules, and the new
mercury_to_mercury.m itself.

In some cases, this diff adds or improves the documentation of the predicates
in mercury_to_mercury.m's successor modules. In some other cases, it just
documents the lack of documentation :-(. In yet other cases, it removes
"documentation" that says nothing that isn't obvious from the predicate's name.

There are some algorithmic changes, but they are all trivial.

compiler/parse_tree_out.m:
    New module containing the code to print out the top levels of parse trees,
    including most sorts of items.

compiler/parse_tree_out_clause.m:
    New module containing the code to print out clauses and goals.

compiler/parse_tree_out_pragma.m:
    New module containing the code to print out pragmas.

compiler/parse_tree_out_pred_decl.m:
    New module containing the code to print out predicate, function and
    mode declarations. It is separate from parse_tree_out.m because a
    significant number of compiler modules need only its functionality,
    and not parse_tree_out.m's functionality.

compiler/parse_tree_out_inst.m:
    New module containing the code to print out insts and modes.

compiler/parse_tree_out_term.m:
    New module containing the code to print out variables and terms.

compiler/parse_tree_out_info.m:
    New module containing the infrastructure of both mercury_to_mercury.m
    and the other new modules.

compiler/parse_tree.m:
    Include the new modules.

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/Mercury.options:
    Transfer an option from mercury_to_mercury.m to the successor module
    that needs it.

compiler/*.m:
    Import one of the new modules either as well as, or instead of,
    mercury_to_mercury.m. In most cases, we need to import only one
    or two of mercury_to_mercury.m's successor modules; nowhere do we
    need to import all eight.

    Clean up some code in termination.m around a call to one of the
    new modules.

tools/speedtest:
    Replace mercury_to_mercury.m on the list of the ten largest modules
    of the compiler.
2015-09-06 21:01:11 +10:00
Zoltan Somogyi
7d20d3e623 Simplify the representation of clauses.
compiler/hlds_clauses.m:
    Instead of representing the list of clauses as either a forward list,
    a reversed list or both, represent it as just a cord. It can be added
    to in O(1) time, and can be flattened to a list in O(N) time, like
    the previous representation, but unlike the previous representation,
    the flattened version can be appended to again, and it never duplicates
    the list.

    Provide three separate predicates for getting the clause list, and one
    for getting just the first clause, for use in different circumstances.
    These replace two previous predicates.

compiler/add_clause.m:
compiler/add_foreign_proc.m:
compiler/assertion.m:
compiler/build_mode_constraints.m:
compiler/clause_to_proc.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/goal_path.m:
compiler/headvar_names.m:
compiler/hlds_out_pred.m:
compiler/implementation_defined_literals.m:
compiler/intermod.m:
compiler/modes.m:
compiler/ordering_mode_constraints.m:
compiler/polymorphism.m:
compiler/post_typecheck.m:
compiler/proc_gen.m:
compiler/prop_mode_constraints.m:
compiler/type_constraints.m:
compiler/typecheck.m:
compiler/unused_imports.m:
    Conform to the changes in hlds_clauses.m, and use the appropriate
    predicate to get the clause list.
2015-08-28 14:32:43 +10:00
Zoltan Somogyi
62ec97d443 Report imports shadowed by other imports.
If a module has two or more import_module or use_module declarations
for the same module, (typically, but not always, one being in its interface
and one in its implementation), generate an informational message about
each redundant declaration if --warn-unused-imports is enabled.

compiler/hlds_module.m:
    We used to record the set of imported/used modules, and the set of
    modules imported/used in the interface of the current module. However,
    these sets

    - did not record the distinction between imports and uses;
    - did not allow distinction between single and multiple imports/uses;
    - did not record the locations of the imports/uses.

    The first distinction was needed only by module_qual.m, which *did*
    pay attention to it; the other two were not needed at all.

    To generate messages for imports/uses shadowing other imports/uses,
    we need all three, so change the data structure storing such information
    for *direct* imports to one that records all three of the above kinds
    of information. (For imports made by read-in interface and optimization
    files, the old set of modules approach is fine, and this diff leaves
    the set of thus *indirectly* imported module names alone.)

compiler/unused_imports.m:
    Use the extra information now available to generate a
    severity_informational message about any import or use that is made
    redundant by an earlier, more general import or use.

    Fix two bugs in the code that generated warnings for just plain unused
    modules.

    (1) It did not consider that a use of the builtin type char justified
    an import of char.m, but without that import, the type is not visible.

    (2) It scanned cons_ids in goals in procedure bodies, but did not scan
    cons_ids that have been put into the const_struct_db. (I did not update
    the code here when I added the const_struct_db.)

    Also, add a (hopefully temporary) workaround for a bug in
    make_hlds_passes.m, which is noted below.

    However, there are at least three problems that prevent us from enabling
    --warn-unused-imports by default.

    (1) In some places, the import of a module is used only by clauses for
    a predicate that also has foreign procs. When compiled in a grade that
    selects one of those foreign_procs as the implementation of the predicate,
    the clauses are discarded *without* being added to the HLDS at all.
    This leads unused_imports.m to generate an uncalled-for warning in such
    cases. To fix this, we would need to preserve the Mercury clauses for
    *all* predicates, even those with foreign procs, and do all the semantic
    checks on them before throwing them away. (I tried to do this once, and
    failed, but the task should be easier after the item list change.)

    (2) We have two pieces of code to generate import warnings. The one in
    unused_imports.m operates on the HLDS after type and mode checking,
    while module_qual.m operates on the parse tree before the creation of
    the HLDS. The former is more powerful, since it knows e.g. what types and
    modes are used in the bodies of predicates, and hence can generate warnings
    about an import being unused *anywhere* in a module, as opposed to just
    unused in its interface.

    If --warn-unused-imports is enabled, we will get two separate set of
    reports about an interface import being unused in the interface,
    *unless* we get a type or mode error, in which case unused_imports.m
    won't be invoked. But in case we do get such errors, we don't want to
    throw away the warnings from module_qual.m. We could store them and
    throw them away only after we know we won't need them, or just get
    the two modules to generate identical error_specs for each warning,
    so that the sort_and_remove_dups of the error specs will do the
    throwing away for us for free, if we get that far.

    (3) The valid/bug100.m test case was added as a regression test for a bug
    that was fixed in module_qual.m. However the bug is still present in
    unused_imports.m.

compiler/make_hlds_passes.m:
    Give hlds_module.m the extra information it now needs for each item_avail.

    Add an XXX for a bug that cannot be fixed right now: the setting of
    the status of abstract instances to abstract_imported. (The "abstract"
    part is correct; the "imported" part may not be.)

compiler/intermod.m:
compiler/try_expand.m:
compiler/xml_documentation.m:
    Conform to the change in hlds_module.m.

compiler/module_qual.m:
    Update the documentation of the relationship of this module
    with unused_imports.m.

compiler/hlds_data.m:
    Document a problem with the status of instance definitions.

compiler/hlds_out_module.m:
    Update the code that prints out the module_info to conform to the change
    to hlds_module.m.

    Print status information about instances, which was needed to diagnose
    one of the bugs in unused_imports.m. Format the output for instances
    nicer.

compiler/prog_item.m:
    Add a convenience predicate.

compiler/prog_data.m:
    Remove a type synonym that makes things harder to understand, not easier.

compiler/modules.m:
    Delete an XXX that asks for the feature this diff implements.
    Add another XXX about how that feature could be improved.

compiler/Mercury.options.m:
    Add some more modules to the list of modules on which the compiler
    should be invoked with --no-warn-unused-imports.

compiler/*.m:
library/*.m:
mdbcomp/*.m:
browser/*.m:
deep_profiler/*.m:
mfilterjavac/*.m:
    Delete unneeded imports. Many of these shadow other imports, and some
    are just plain unneeded, as shown by --warn-unused-imports. In a few
    modules, there were a *lot* of unneeded imports, but most had just
    one or two.

    In a few cases, removing an import from a module, because it *itself*
    does not need it, required adding that same import to those of its
    submodules which *do* need it.

    In a few cases, conform to other changes above.

tests/invalid/Mercury.options:
    Test the generation of messages about import shadowing on the existing
    import_in_parent.m test case (although it was also tested very thoroughly
    when giving me the information needed for the deletion of all the
    unneeded imports above).

tests/*/*.{m,*exp}:
    Delete unneeded imports, and update any expected error messages
    to expect the now-smaller line numbers.
2015-08-25 00:38:49 +10:00
Zoltan Somogyi
f2043fc9bd Replace the item list with more structured ASTs.
The parts of the compiler that run before the HLDS is constructed used to use
a raw list of items to represent source files (.m), interface files (.int0,
.int3, .int2 and .int) and optimization files (.opt, and .trans_opt).
These lists had structure, but this structure was implicit, not explicit,
and its invariants were never really documented.

This diff changes that. It replaces the item list with FIVE separate types.

Three of these each represent the unprocessed content of one file:

- parse_tree_int represents the contents of one interface file;
- parse_tree_opt represents the contents of one optimization file;
- parse_tree_src represents the contents of one source file.

Two of these each represent the processed contents of one or more files:

- raw_compilation_unit represents the contents of one module in a source file.
  (The source file may contain several nested modules; the compilation unit
  represents just one.)
- aug_compilation_unit represents the contents of one module in a source file,
  just like raw_compilation_unit, but it is augmented with the contents of the
  interface and optimization files of the other modules imported (directly or
  indirectly) by the original module.

These five separate concepts all used to be represented by the same type,
list(item), but different invariants applied to the structure of those lists.
The most important of those invariants at least are now explicit in the types.
I think it is entirely possible that there are other invariants I haven't
discovered and documented (for example, .int3 files must have stricter
invariants on what can appear in them than .int files), but discovering
and documenting these should be MUCH easier after this change.

I have marked many further opportunities for improvements with "XXX ITEM_LIST".
Some of these include moving code between modules, and the creation of new
modules. However, I have left acting on those XXXs until later, in order to
keep the size of this diff down as much as possible, for easier reviewing.

compiler/prog_item.m:
    Define the five new AST types described above, and utility predicates
    that operate on them.

    In the rest of this change, I tried, as much as possible, to change
    predicates that used to take item lists as arguments to make them change
    one of these types instead. In many cases, this required putting
    the argument lists of those predicates into a more consistent order.
    (Often, predicates that operated on the contents of the module
    took the name of the module and the list of items in the module
    not just as separate arguments, but as separate arguments that
    weren't even next to each other.)

    Define types that identify the different kinds of interface and
    optimization files (.int, .int2 etc). These replace the string suffixes
    we used to use to identify file types. Predicates that used to take strings
    representing suffixes as arguments now have to specify whether they can
    handle all these file types (source, interface and optimization),
    or just (e.g.) all interface file types.

    We used to have items corresponding to `:- module' and `:- end_module'.
    Delete these; this information is now implicit in the structure of the
    relevant AST. The parser handles the corresponding terms as markers,
    not items; these markers are live only during parsing.

    We used to have module_defns corresponding to `:- interface' and
    `:- implementation'. Delete these; this information is now also implicit
    in the structure of the relevant AST. Delete also, for the same reason,
    the module_defns used to mark the starts of sublists in the overall lists
    of items whose items came from the interface files or optimization files
    of other modules. The former are now markers during parsing. The latter
    are never parsed, but are created directly, after parsing has been done.

    Delete the pragma type for `:- pragma source_file'. This is never
    needed later; it is now a marker during parsing.

    Change the internal representation of `:- import' and `:- use'.
    It used to store a list of module names, but that list was an actual list
    only during parsing; after that, it always had exactly one element.
    It now stores one module name, and the parser has a mechanism to convert
    one read-in term to more than one item, for use with terms such as
    `:- import_module a, b'.

    Delete the internal representation of `:- export', which was never
    implemented, since if it IS ever implemented, it will almost certainly
    be in a different form, which will need different support.

    Document some further opportunities for simplification, later.
    (This diff is already more than big enough.)

compiler/prog_io_item.m:
    Rewrite the top-level part of this module. Instead of returning an item
    for every parsed term, distinguish between parsing items that end up
    in item lists inside ASTs, and parsing markers that end up creating
    the STRUCTURE of those ASTs.

compiler/prog_io.m:
    Rewrite the meat of this module. Instead of reading in a simple item list,
    we now have to read in three different parse trees with three different
    grammars, each of which is more complex than a simple list.

compiler/read_modules.m:
    We used to have a map that mapped file names to the contents of those
    files. We now need three separate maps, for interface files, optimization
    files and source files, due to their separate types.
    (We don't actually use the map for optimization files, which seems
    to be a potential performance bug. The root cause of that problem
    us that while intermod.m and the grab_*modules part of modules.m do
    similar jobs, they don't use the same mechanisms.)

    Replace the read_module predicate with the predicates read_module_src
    and read_module_int, since these now return different types.

    To avoid having to create AST-type-specialized variants of
    read_module_ignore_errors and read_module_if_changed, give each of
    read_module_{src,int} arguments that optionally tell them to ignore errors
    and/or to read the module only if changed (though the "and" part of
    "and/or" should not be needed.) These options already existed, but
    they weren't exported.

compiler/timestamp.m:
    Define the type we use for this option in read_modules.

compiler/status.m:
    New module, containing mostly

    - stuff carved out of hlds_pred.m, which defines the import_status type,
      and the predicates that operate on it;
    - stuff carved out of make_hlds_passes.m, which defines the item_status
      type and the predicates that operate on that; and
    - stuff carved out prog_data.m, which defines the section (now
      module_section) and import_locn types.

    It also contains the new section kinds we now use to represent item blocks
    that were imported from interface and optimization files.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Add status.m to the parse_tree package.

compiler/hlds_pred.m:
compiler/prog_data.m:
    Remove the stuff now in status.m.

compiler/error_util.m:
    Provide a mechanism to control the order of messages with respect to
    ALL other messages, not just those that also specify ordering.

compiler/mercury_to_mercury.m:
    Provide predicates for printing out parse_tree_* and *_compilation_unit,
    since printing out a simple item list is no longer enough for debugging.

    Pretty-print type definitions nicely.

    Replace a boolean with a purpose-specific enum.

compiler/modules.m:
    Rewrite virtually all this module to make it work on the new AST
    representations. Generate more detailed error messages for duplicate
    module inclusions. Note lots of possibilities for further improvements,
    including in the documentation. Mark places I am still not sure about,
    especially places where I am not sure *why* the code is doing
    what it is doing.

compiler/module_imports.m:
    This module stores the data structure in which we accumulate the stuff
    imported into a compilation unit, i.e. it is in these data structures
    that a raw_compilation_unit becomes an aug_compilation_unit. Modify
    the data structure and the predicates that operate on it to work on the
    new AST representations, not on an (apparently) simple list of items.

    Avoid ambiguities by adding a prefix to field names.

    Add some convenience predicates.

compiler/module_qual.m:
    Perform module qualification on both raw lists of items (for use when
    generating .int3 files) but also on item blocks (for use pretty much
    in every other situation).

    Generate warnings about module imports that are unnecessarily in the
    module interface using the module's context (the context of the `:- module'
    declaration), not line 1 of the relevant file.

compiler/prog_io_error.m:
    Split some error categories more finely, since some error kinds here
    actually used to be reported for more than one distinct situation.

compiler/prog_io_util.m:
    Provide utility predicates that operate on nonempty lists.

compiler/recompilation.version.m:
    Make the comparison of the old and new contents of the interface file
    work on two parse_tree_ints, not on two raw sequences of items.

    Delete a boolean option that was always `yes', never 'no'.

compiler/recompilation.m:
    Turn some functions into predicates to allow the use of state variable
    notation.

    Avoid ambiguities by adding a prefix to field names.

compiler/write_module_interface_files.m:
    Besides updating the code in this module to work on the new parse tree
    representations, also use cords instead of reversed lists in several cases.
    Note many possibilities for further improvements.

library/list.m:
    Move the type one_or_more here from the compiler directory, since
    we now use it in more than one compiler module, and this is its natural
    home.

mdbcomp/sym_name.m:
    Rename "match_sym_name" to "partial_sym_name_matches_full", since this
    better describes its job.

    Add a det version of sym_name_get_module_name.

compiler/equiv_type.m:
    Rename some types to make them more expressive.

compiler/accumulator.m:
compiler/add_class.m:
compiler/add_foreign_enum.m:
compiler/add_foreign_proc.m:
compiler/add_mode.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_pred.m:
compiler/add_solver.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/assertion.m:
compiler/base_typeclass_info.m:
compiler/check_typeclass.m:
compiler/ctgc.util.m:
compiler/dead_proc_elim.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/deps_map.m:
compiler/det_report.m:
compiler/elds_to_erlang.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/export.m:
compiler/format_call.m:
compiler/higher_order.m:
compiler/hlds_data.m:
compiler/hlds_module.m:
compiler/hlds_out_pred.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/lambda.m:
compiler/lco.m:
compiler/make.module_dep_file.m:
compiler/make_hlds.m:
compiler/make_hlds_error.m:
compiler/make_hlds_passes.m:
compiler/make_tags.m:
compiler/mercury_compile.m:
compiler/ml_proc_gen.m:
compiler/ml_type_gen.m:
compiler/mode_errors.m:
compiler/oisu_check.m:
compiler/par_loop_control.m:
compiler/polymorphism.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/pred_table.m:
compiler/prog_io_dcg.m:
compiler/prog_io_find.m:
compiler/prog_io_pragma.m:
compiler/prog_io_sym_name.m:
compiler/prog_io_type_defn.m:
compiler/prog_io_typeclass.m:
compiler/prop_mode_constraints.m:
compiler/push_goals_together.m:
compiler/qual_info.m:
compiler/recompilation.check.m:
compiler/recompilation.usage.m:
compiler/simplify_proc.m:
compiler/smm_common.m:
compiler/special_pred.m:
compiler/ssdebug.m:
compiler/stm_expand.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/table_gen.m:
compiler/term_constr_initial.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trace_params.m:
compiler/trans_opt.m:
compiler/type_class_info.m:
compiler/type_ctor_info.m:
compiler/typecheck.m:
compiler/typecheck_errors.m:
compiler/typecheck_info.m:
compiler/unify_proc.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/write_deps_file.m:
compiler/xml_documentation.m:
    Conform to the changes above.

tests/hard_coded/higher_order_func_test.m:
tests/hard_coded/higher_order_syntax.m:
    Avoid a warning about importing a module in the interface, not the
    implementation.

tests/invalid/after_end_module.err_exp:
tests/invalid/any_mode.err_exp:
tests/invalid/bad_end_module.err_exp:
tests/invalid/bigtest.err_exp:
tests/invalid/bug113.err_exp:
tests/invalid/duplicate_modes.err_exp:
tests/invalid/errors.err_exp:
tests/invalid/errors1.err_exp:
tests/invalid/errors2.err_exp:
tests/invalid/funcs_as_preds.err_exp:
tests/invalid/inst_list_dup.err_exp:
tests/invalid/invalid_main.err_exp:
tests/invalid/missing_interface_import2.err_exp:
tests/invalid/no_exports.err_exp:
tests/invalid/occurs.err_exp:
tests/invalid/predmode.err_exp:
tests/invalid/prog_io_erroneous.err_exp:
tests/invalid/type_inf_loop.err_exp:
tests/invalid/typeclass_missing_det_3.err_exp:
tests/invalid/typeclass_test_11.err_exp:
tests/invalid/types.err_exp:
tests/invalid/undef_inst.err_exp:
tests/invalid/undef_mode.err_exp:
tests/invalid/undef_type.err_exp:
tests/invalid/unicode1.err_exp:
tests/invalid/unicode2.err_exp:
tests/invalid/vars_in_wrong_places.err_exp:
tests/warnings/unused_import.exp:
tests/warnings/unused_interface_import.exp:
    Update the expected outputs in the invalid and warnings directories
    to account for one or more of the following five changes.

    Error messages that warn about a module not exporting anything
    used to always refer to line 1 of the module's source file.
    Now expect these messages to refer to the actual context of the module,
    which is the context of its `:- module' declaration.

    Expect a similarly updated context for messages that warn about
    unnecessarily importing modules in the interface, not in the
    implementation.

    Expect a similarly updated context for messages that warn about
    importing a module via both `:- import_module' and `:- use_module'.

    For the modules that follow the `:- module' declaration directly with code,
    also expect an error message about the missing section marker.

    For modules that have terms after the `:- end_module' declaration,
    replace "end_module" with "`:- end_module'" in the error message.

tests/invalid/func_class.{m,err_exp}:
    New test case. It is a copy of the old tests/valid/func_class.m, which
    is missing more than one module marker. The expected output is what I think
    we should generate. The test case currently fails, because we currently
    print only a subset of the expected errors. I am pretty sure the reason
    for that is that old code I have not modified simply throws away the
    missing error messages. Fixing this is work for the near future.

tests/invalid/Mmakefile:
    Enable the new test case.

tests/misc_tests/pretty_print_test.exp:
    Expect the pretty-printed output to use four-space indentation,
    per our current style guide, since the compiler now generates such output.

tests/misc_tests/pretty_print_test.m:
    Clean up the source code of the test as well.

tests/valid/complicated_unify.m:
tests/valid/det_switch.m:
tests/valid/easy_nondet_test.m:
tests/valid/error.m:
tests/valid/func_class.m:
tests/valid/func_int_bug_main.m:
tests/valid/higher_order.m:
tests/valid/higher_order2.m:
tests/valid/implied_mode.m:
tests/valid/indexing.m:
tests/valid/multidet_test.m:
tests/valid/nasty_func_test.m:
tests/valid/semidet_disj.m:
tests/valid/stack_alloc.m:
tests/valid/switches.m:
    Add missing section markers to these modules. They used to follow
    the `:- module' declaration directly with code.
2015-07-21 04:06:52 +10:00
Zoltan Somogyi
05f5ec7d41 Fix a bug that put $typed_insts in .opt files.
compiler/mercury_to_mercury.m:
    Make the predicates that output insts take a parameter that says whether
    we are trying to print valid Mercury code (in which case we strip any
    $typed_inst wrappers around other insts), or as-informative-as-possible
    representations of insts for debugging, in which case we do NOT strip
    those wrappers. When invoked on insts that CANNOT be printed as valid
    Mercury even though valid Mercury is being requested, abort.

    Rationalize the argument order of the involved predicates.

    Move the output_lang type here from hlds_out_util.m to make the above
    possible. Change the flag that governs whether item names are qualified
    or not from a bool to a bespoke enum type. Fix a bug involving that bool.

compiler/hlds_out_mode.m:
    This module also has a predicate that output insts, though it already had
    a valid mercury/debug output flag. If it is asked to print valid Mercury,
    make it strip $typed_inst wrappers, and abort on insts that cannot be
    printed as valid Mercury.

    A better fix would be to eliminate this predicate, and other predicates
    in this module that also duplicate predicates in mercury_to_mercury.m,
    after generalizing the latter as needed, but that would take more time.

compiler/add_clause.m:
compiler/add_mutable_aux_preds.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/hlds_error_util.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
compiler/liveness.m:
compiler/make_hlds_error.m:
compiler/mercury_to_mercury.m:
compiler/mode_errors.m:
compiler/passes_aux.m:
compiler/pd_debug.m:
compiler/push_goals_together.m:
compiler/saved_vars.m:
compiler/stack_opt.m:
compiler/structure_reuse.analysis.m:
compiler/structure_sharing.analysis.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/unneeded_code.m:
    Conform to the above.

compiler/mode_constraint_robdd.m:
compiler/mode_constraints.m:
    Fix an unrelated problem that ALSO prevented a bootcheck with -O5
    --intermodule-optimization, which is that with these options, several
    predicates in both these modules accumulated enough type ambiguities
    for the type checker to throw up its hands in despair.

    The type checker should NOT be influenced by the additional types, data
    constructors, field names, functions and predicates added by these options
    to their respective tables, but it is. Fixing that bug is future work.
    For now, just reduce the ambiguities to the allowable level, mostly
    by adding module qualifiers, and by converting some function calls to
    predicate calls. (Function calls introduce ambiguities, since the
    typechecker thinks they may be partially applied predicate calls.)
2015-04-02 12:09:42 +11:00
Zoltan Somogyi
ccc726fd79 Clean up compiler/d*.m.
compiler/delay_info.m:
    Remove redundant comments on predicate definitions documenting the purpose
    of the predicate; they belong on the predicate *declarations*. Move
    information in these inappropriately-placed comments to the predicate
    declaration, if it wasn't there already.

    Use trace goals where appropriate.

compiler/dependency_graph.m:
    Delete some unused types.

compiler/distance_granularity.m:
    Remove two old URLs; neither is valid anymore.

compiler/*.m:
    Minor cleanups.
2015-01-03 22:45:35 +11:00
Zoltan Somogyi
c1402f8b99 Clean up hlds_module.m.
compiler/hlds_module.m:
    Put related fields of the module_sub_info next to each other.

    Some of those fields contained lists that were built reversed,
    in order to avoid O(N^2) behavior when repeatedly adding new items
    to the end of the list. Replace these with cords, which did not exist
    when those fields were first added.

    Give some fields and their getter/setter predicates more descriptive
    names.

    Separate out both the declarations and definitions of the getter and
    setter predicates, and put them into the same order as the (updated)
    order of the fields. Put the utility predicates (those that are more
    complicated than just getters or setters) into an order based on
    what fields they work on, following the same order.

    Improve the operation of some of the utility predicates, e.g. replacing
    a nondet predicate with a det predicate returning a set.

    Delete an unused type.

    Conform to the changes in the modules imported by hlds_module.m,
    e.g. pred_table.m, prog_data.m and prog_foreign.m.

compiler/pred_table.m:
    We used to store the set of valid pred ids as two lists, again to avoid
    O(N^2) behavior. Replace the two lists with a set. This allows
    looking up the set *without* updating the pred_table, or, when
    the pred_table is within the module_info, updating the module_info.

    Instead of allowing callers to replace the set of valid pred ids wholesale,
    enforce the documented invariant on that set by only allowing deletions.

    Conform to the changes above.

compiler/add_pragma.m:
compiler/bytecode_gen.m:
compiler/check_typeclass.m:
compiler/compile_target_code.m:
compiler/cse_detection.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/deps_map.m:
compiler/det_analysis.m:
compiler/distance_granularity.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/exception_analysis.m:
compiler/export.m:
compiler/float_regs.m:
compiler/foreign.m:
compiler/higher_order.m:
compiler/hlds_module.m:
compiler/inlining.m:
compiler/intermod.m:
compiler/introduce_parallelism.m:
compiler/lambda.m:
compiler/liveness.m:
    Conform to the changes above.

    In many places, the change to how the valid pred ids are stored
    allows us to avoid creating new module_infos.

    In some places, fix style issues I noticed while working on the above.

compiler/llds.m:
compiler/mercury_compile_llds_back_end.m:
    Conform to the changes above.

    Move a type from llds.m to mercury_compile_llds_back_end.m, since
    only the latter uses it.

compiler/prog_data.m:
compiler/prog_foreign.m:
    Replace some types that used to hold reversed lists with cords.

    In prog_foreign.m, represent the two kinds of foreign code that
    do NOT define procedures with similarly named types.

    Delete a type (user_foreign_code) that duplicated another type.

    Replace an equivalence type with a notag type, for safety.

compiler/recompilation.usage.m:
compiler/typecheck.m:
compiler/typecheck_errors.m:
    Now that we have direct access to the set of visible modules,
    simplify the logic of some code dealing with those modules.

compiler/module_imports.m:
    Put some related fields next to each other.

compiler/llds_out_file.m:
compiler/make.dependencies.m:
compiler/make.module_dep_file.m:
compiler/make_hlds_passes.m:
compiler/mark_tail_calls.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_middle_passes.m:
compiler/ml_proc_gen.m:
compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/mlds_to_cs.m:
compiler/mlds_to_il.m:
compiler/mlds_to_java.m:
compiler/mlds_to_managed.m:
compiler/mode_constraints.m:
compiler/modes.m:
compiler/modules.m:
compiler/passes_aux.m:
compiler/polymorphism.m:
compiler/post_typecheck.m:
compiler/pred_table.m:
compiler/proc_gen.m:
compiler/prog_item.m:
compiler/purity.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.live_region_analysis.m:
compiler/rbmm.live_variable_analysis.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_arguments.m:
compiler/rbmm.region_instruction.m:
compiler/ssdebug.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.domain.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/switch_detection.m:
compiler/tabling_analysis.m:
compiler/term_constr_initial.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/trans_opt.m:
compiler/try_expand.m:
compiler/type_constraints.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/write_deps_file.m:
2014-12-29 22:18:51 +11:00
Zoltan Somogyi
efb56544ed Speed up pred_info's setter predicates a bit.
compiler/hlds_pred.m:
    If the new value of a field of pred_info is likely to be bit-identical
    to the old value, then test the old and new bits for equality in the
    setter, and if they are the same, do not allocate a new pred_info
    structure that is guaranteed to be the same as the old one.

    By avoiding unnecessary memory turnover, this speeds up the compiler a bit,
    though I cannot nail down by how much. I measured it several times, with
    the results being no change, a speedup of 1%, and a speedup of 2%.

    Remove the unused setter predicate for the attributes field.

    Rename some access predicates to pred_infos to better reflect what they do.

    Add a distinguishing prefix to the fields of pred_infos.

compiler/*.m:
    Conform to the changes above.
2014-12-14 10:32:27 +11:00
Zoltan Somogyi
500948d549 Break up mdbcomp/prim_data.m. The new modules have much better cohesion.
mdbcomp/sym_name.m:
    New module, containing the part of the old prim_data.m that
    dealt with sym_names.

mdbcomp/builtin_modules.m:
    New module, containing the part of the old prim_data.m that
    dealt with builtin modules.

mdbcomp/prim_data.m:
    Remove the things that are now in the two new modules.

mdbcomp/mdbcomp.m:
deep_proiler/Mmakefile:
slice/Mmakefile:
    Add the two new modules.

browser/*.m:
compiler/*.m:
deep_proiler/*.m:
mdbcomp/*.m:
slice/*.m:
    Conform to the above changes.
2014-09-02 05:20:23 +02:00
Zoltan Somogyi
8a6ffaab19 Fix Mantis bug #354.
I/O tabling has two main purposes. The first and more important is to allow the
debugger to replay parts of the program execution for the programmer, which
requires making I/O operations idempotent (so that we get the same results on
the second, third etc "execution" as on the first). The second purpose is to
let the person using the debugger actually see a list of the I/O actions, and
their results.

The root of the problem here is that the compiler can do the second part
only if it has access to the type_infos describing the types of the arguments
of the I/O action. With the current infrastructure for representing typeclass
information, this is not always possible in the presence of typeclass
constraints on I/O action predicates. The reason is that polymorphism.m can
put the typeinfo for a type variable that is subject to a typeclass constraint
arbitrarily deep inside the typeclass_info for that constraint, but the RTTI
can encode such locations only up to a fixed depth (currently only the
shallowest embedded is encodable).

Before this fix, the test case for this bug got a compiler abort when the
I/O tabling transformation tried to figure out how to table the typeclass
info representing the typeclass constraint on a I/O action predicate.

We still cannot table typeclass infos. We could store them (I/O tabling
does not require anything more complicated), but the problem of deeply buried
typeinfos inside them would still remain. So this fix consists of two parts:

- for typeclass constrained I/O primitives, recording only enough information
  to allow them to replayed (the first purpose above), and not to print them
  out (the second purpose), and
- getting the runtime system to understand this, and not crash with a core dump
  in the absence of the information required for the second purpose.

This second part requires changes to the RTTI used by I/O tabling. These
changes BREAK BINARY COMPATIBILITY in debug grades.

runtime/mercury_stack_layout.h:
    Rename the MR_TableIoDecl structure as the MR_TableIoEntry structure,
    since the I/O table entries that it describes are used not just for
    declarative debugging, but also for printing out I/O actions.

    Add a field to it that specifies whether the fields describing
    the types of the I/O action's arguments are meaningful.

runtime/mercury_grade.h:
    Bump the debug-only binary compatibility version number, since
    the change to mercury_stack_layout.h requires it.

runtime/mercury_trace_base.[ch]:
    When returning information about a tabled I/O action, return a boolean
    that says whether the information abouts its arguments is actually
    present or not. Do not return information about the arguments if
    we cannot convert them into univs due to missing type information.

browser/io_action.m:
    Pay attention to the new info returned by MR_trace_get_action,
    and avoid a potential core dump by generating a description of the
    requested I/O action only if the argument type information needed
    to generate that description is actually available.

trace/mercury_trace_vars.c:
    Pay attention to the new info returned by MR_trace_get_action.
    When the argument type information needed to generate an accurate
    description of the I/O action is not available, generate a
    "description" that mentions this fact.

trace/mercury_trace_cmd_browsing.c:
    Make the fix to mercury_trace_vars.c easier to test by adding a mechanism
    to print out all existing I/O actions, as long as there aren't too many
    of them.

compiler/hlds_pred.m:
compiler/layout.m:
compiler/prog_data.m:
    Prepare for the possibility that we have cannot record the information
    needed to reconstruct the runtime types of the arguments of a I/O tabled
    predicate.

compiler/table_gen.m:
    If an I/O tabled predicate has one or more typeclass constraints,
    do not attempt to record the RTTI needed to reconstruct the types
    of its arguments at runtime.

compiler/continuation_info.m:
compiler/hlds_data.m:
    Rename some data structures that referred to the old MR_TableIoDecl
    structure to refer to its replacement, the MR_TableIoEntry structure.

compiler/bytecode_gen.m:
compiler/ctgc.selector.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/erl_unify_gen.m:
compiler/export.m:
compiler/higher_order.m:
compiler/hlds_code_util.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/hlds_pred.m:
compiler/implementation_defined_literals.m:
compiler/inst_check.m:
compiler/layout.m:
compiler/layout_out.m:
compiler/llds.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_util.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_to_mercury.m:
compiler/ml_global_data.m:
compiler/ml_switch_gen.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/mode_util.m:
compiler/module_qual.m:
compiler/opt_debug.m:
compiler/proc_gen.m:
compiler/prog_data.m:
compiler/prog_out.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/rbmm.execution_path.m:
compiler/stack_layout.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/switch_gen.m:
compiler/switch_util.m:
compiler/type_ctor_info.m:
compiler/unify_gen.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
runtime/mercury_misc.h:
runtime/mercury_tabling.h:
    Conform to the above changes.

tests/debugger/tabled_typeclass.{m,inp,exp,exp2}:
    New test case to test that I/O actions that have typeclass constraints
    on them can be printed in mdb.

tests/debugger/Mmakefile:
tests/debugger/Mercury.options:
    Enable the new case.
2014-08-30 00:48:53 +02:00
Zoltan Somogyi
884838b9df If the backend supports constant structures, and we do not need unifications
Estimated hours taken: 8
Branches: main

If the backend supports constant structures, and we do not need unifications
to retain their original shapes, then convert each from_ground_term scope
into a unification with a cons_id that represents the ground term being
built up.

This speeds up the compilation of training_cars_full.m by about 6%.

compiler/simplify.m:
	Make the conversion if enabled. By doing the conversion in this phase,
	we don't have to teach the semantic analysis passes about unifications
	with the new cons_id, but we do get the benefit of later passes being
	faster, because they have less code to process.

compiler/const_struct.m:
	The declarative debugger does not yet know how to handle the new
	cons_id, so do not introduce it if we are preparing for declarative
	debugging.

compiler/trace_params.m:
	Export a predicate for const_struct.m.

compiler/prog_data.m:
	Add the new cons_id, ground_term_const.

compiler/hlds_data.m:
	Add the tag of the new cons_id, ground_term_const_tag.

compiler/hlds_code_util.m:
	Convert the new cons_id to the new cons_tag.

	Fix an old problem with that conversion process: it always converted
	tuple_cons to single_functor_tag. However, arity-zero tuples are
	(dummy) constants, not heap cells, so we now convert them to a (dummy)
	integer tag. This matters now because the process that generates
	code (actually data) for constant structures handles the cons_tags that
	build constants and heap cells separately. As a side benefit, we
	no longer reserve a word-sized heap cell for arity-zero tuples.

compiler/unify_gen.m:
compiler/ml_unify_gen.m:
	Implement the generation of code for arbitrary constant structures,
	not just those that can implement typeinfos and typeclass_infos.

compiler/term_norm.m:
	Compute the sizes of ground terms for each of our norms.

compiler/term_traversal.m:
	Manage the computation of sizes of ground terms.

	Simplify and thereby speed up a predicate.

compiler/term_constr_build.m:
	Note that we should manage the computation of sizes of ground terms.

compiler/term_util.m:
	Simplify the style of a predicate.

compiler/layout.m:
	Give some field names prefixes to avoid ambiguities.

compiler/bytecode_gen.m:
compiler/ctgc.selector.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/erl_unify_gen.m:
compiler/export.m:
compiler/higher_order.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_util.m:
compiler/implementation_defined_literals.m:
compiler/inst_check.m:
compiler/mercury_to_mercury.m:
compiler/ml_global_data.m:
compiler/ml_type_gen.m:
compiler/mode_util.m:
compiler/module_qual.m:
compiler/polymorphism.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/rbmm.execution_path.m:
compiler/switch_gen.m:
compiler/switch_util.m:
compiler/type_ctor_info.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Conform to the changes above.

tests/hard_coded/ground_terms.{m,exp}:
	A new test case to test the handling of ground terms.

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
	Enable the new test case.
2012-06-11 03:13:24 +00:00
Zoltan Somogyi
ee63cb8d84 Heavily polymorphic code, such as that generated by g12, often builds the same
Estimated hours taken: 80
Branches: main

Heavily polymorphic code, such as that generated by g12, often builds the same
typeinfos and typeclass infos over and over again. We have long had caches
that avoid building a new typeinfo or typeclass info if some variable in the
current scope already contains the right value, but a program that has many
scopes may still build the same typeinfo or typeclass info many times.
If that typeinfo or typeclass info is a ground term, the code generators
will recognize that fact, and will turn all the constructions of that ground
term in different scopes into referencess to the same constant structure.
However, in the meantime, the program can be much bigger than necessary.
In the motivating test case for this change, a single call to fdic_post
is preceded by 133 goals that build the four typeclass infos it needs.

The main idea of this diff is to construct constant typeinfos and typeclass
infos out of line, in a separate data structure. Polymorphism then binds
variables representing typeinfo and typeclass infos to reference to these
constant structures. In the motivating example, this allows polymorphism.m
to insert just four goals before the call to fdic_post, the minimal possible
number: one for each typeclass info that predicate needs.

On Leslie's bug344 program, this change speeds up the compiler by a factor
of five to eight (reducing compile time from about 80 or 85 seconds to
10 or 15).

There is a drawback to this scheme, but it is minor. That drawback is that
once a constant structure is entered into our database of constant structures,
it cannot (yet) be removed. Even if all the references to a constant structure
are eliminated by optimizations, the structure will remain.

------------------------------------------

CHANGES IN THE FRONT END

compiler/const_struct.m:
	A new module to look after our new database of constant structures.
	Currently, its use is enable only on the LLDS and MLDS C backends.

compiler/hlds.m:
compiler/notes/compiler_design.html:
	Add the new module to the HLDS package.

compiler/hlds_module.m:
	Include the constant structure database in the module_info.

compiler/hlds_data.m:
	Add two new cons_ids, which refer to typeinfos and typeclass infos
	implemented as constant structures.

	Move the code for calculating the number of extra instance args
	in base_typeclass_infos here from base_typeclass_info.m, since
	polymorphism.m now needs it too. We can now also eliminate the
	duplicate copy of that code in higher_order.m.

	Make an independent optimization: make the restrict_list_elements
	function more efficient by avoiding redundant tests.

compiler/polymorphism.m:
	When building typeinfo and typeclass infos, keep track of whether
	the structure being built is constant. If it is, then put it in the
	database of constant structures, and replace the code building it
	with a simple reference to that new entry.

	Since I now expect most goal sequences inserted before goals to be
	short, consistent use lists of goals to represent these, since the
	costs of conversions to and from cord form are unlikely to be paid back
	by the higher efficiency of cord operations on longer sequences.

	When we want to get the typeclass info of a superclass out of the
	typeclass info of a subclass, if the typeclass info of the subclass
	is known, do the extraction here. We used to do this optimization
	only in higher_order.m, but doing so here reduces the size of the HLDS
	between polymorphism.m and higher_order.m, and thus improves
	compilation time.

	Reorganize some of the structure of this module to make the above
	changes possible. In particular, our new approach requires making
	snapshots of the varsets and vartypes, and later restoring those
	snapshots if the variables allocated turn out to be unnecessary,
	due to all of them describing the components of a constant structure.
	The correctness of such code is much easier to check if the taking
	and restoring of each snapshot takes places in a single predicate.

	Remove the code moved to higher_order.m.

	Add some debugging code for now. If no issues arise in the next few
	weeks, it can be deleted.

compiler/modecheck_unify.m:
	Treat unifications whose right hand side has a cons_id referring to a
	constant structure specially.

compiler/base_typeclass_info.m:
	Replace the code that is now in num_extra_instance_args with a call
	to that predicate.

	Put the arguments of some predicates in a more logical order.

compiler/higher_order.m:
	When looking up the components of existing typeclass infos, handle
	cases where those typeclass infos are constant structures.

	Give some types, fields and variables better names.

	Avoid a redundant map search.

	Avoid some redundant tests by providing separate predicates to handle
	higher order calls and method calls.

	Move the predicate is_typeclass_info_manipulator here from
	polymorphism.m, since this is the only module that uses that predicate.

------------------------------------------

CHANGES IN THE LLDS BACKEND:

compiler/llds.m:
	Add a type to map constant structure numbers to rvals together with
	their LLDS types.

	Introduce a type to represent rvals together with their LLDS types.

compiler/mercury_compile_llds_back_end.m:
	Before we generate code for the predicates of the module, convert
	the constant structures to typed LLDS rvals. Create a map mapping
	each constant structure number to the corresponding typed rvals.

compiler/proc_gen.m:
	Take that map, and put it into the code_info, to allow references
	to those structures to be translated.

	Put the arguments of some predicates into a more logical order.

compiler/code_info.m:
	Include a map giving the representation of each constant structure
	in the code_info.

compiler/unify_gen.m:
	Add the predicates needed to convert the constant structures of a
	module to LLDS rvals. For now, this code works only on the kinds of
	constant structures generated by polymorphism.m.

	Handle unifications whose right hand side is a reference to a constant
	structure.

compiler/global_data.m:
compiler/stack_layout.m:
	Use the new typed_rval type where relevant.

------------------------------------------

CHANGES IN THE MLDS BACKEND:

compiler/ml_proc_gen.m:
	Before we generate code for the predicates of the module, convert
	the constant structures to typed MLDS rvals. Create a map mapping
	each constant structure number to the corresponding typed rvals.

	Factor out some code into a predicate of its own.

compiler/ml_gen_info.m:
	Include a map giving the representation of each constant structure
	in the ml_gen_info.

	Also add to the ml_gen_info an indication of what GC system we are
	generating code for, since the code generator needs to know this often.

compiler/ml_unify_gen.m:
	Add the predicates needed to convert the constant structures of a
	module to MLDS rvals. For now, this code works only on the kinds of
	constant structures generated by polymorphism.m.

	Handle unifications whose right hand side is a reference to a constant
	structure.

	Simplify some existing code.

------------------------------------------

MINOR CHANGES:

mdbcomp/prim_data.m:
	Add a predicate that gets both the module name and the base name
	from a sym_name at the same time. This is used for minor speedups
	in other code updated in this diff.

compiler/dead_proc_elim.m:
	Scan constant structures for references to entities that need to be
	kept alive.

compiler/term_constr_build.m:
compiler/term_traversal.m:
	Do not build size constraints from references to constant structures.
	The sizes of constant terms don't change, so they are irrelevant
	when building constraints for finding argument size changes.

------------------------------------------

TRIVIAL CHANGES TO CONFORM TO OTHER CHANGES:

compiler/hlds_out_module.m:
	Print out the constant structure database if asked.

doc/user_guide.tex:
	Document how to ask for it.

compiler/hlds_out_util.m:
	Print out the new cons_ids.

compiler/hlds_out_mode.m:
	Print out the new cons_ids in insts.

	Remove a compiler abort, to help debug a problem.

	Improve the structure of a predicate.

compiler/hlds_out_goal.m:
	Fix some missing newlines.

compiler/hlds_code_util.m:
	Add some utility predicates needed by the modules above.

	Conform to the changes above.

compiler/mlds_to_il.m:
	Reorder some predicates.

	Conform to the changes above.

compiler/bytecode_gen.m:
compiler/ctgc.selector.m:
compiler/dependency_graph.m:
compiler/erl_unify_gen.m:
compiler/export.m:
compiler/implementation_defined_literals.m:
compiler/inst_check.m:
compiler/llds_out_globals.m:
compiler/mercury_to_mercury.m:
compiler/ml_global_data.m:
compiler/ml_switch_gen.m:
compiler/ml_type_gen.m:
compiler/module_qual.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/rbmm.execution_path.m:
compiler/switch_gen.m:
compiler/switch_util.m:
compiler/type_ctor_info.m:
compiler/unused_imports.m:
compiler/var_locn.m:
compiler/xml_documentation.m:
	Conform to the changes above.

------------------------------------------

OTHER INDEPENDENT CHANGES:

compiler/handle_options.m:
	Add a dump option that is useful for debugging when working on
	polymorphism.m and constant structures.

compiler/equiv_type_hlds.m:
	Fix an old performance bug: make the code handling try goals keep
	the old memory cells representing such goals, instead of rebuilding
	them, if no changes took place inside them.

compiler/ml_accurate_gc.m:
	Move a test earlier, to allow us to avoid more work in the common case.

compiler/erl_code_gen.m:
compiler/error_util.m:
compiler/hhf.m:
compiler/inst_util.m:
compiler/ml_code_util.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/modecheck_call.m:
compiler/modecheck_util.m:
compiler/post_typecheck.m:
compiler/size_prof.m:
compiler/stack_opt.m:
compiler/stratify.m:
compiler/unused_args.m:
compiler/post_type_analysis.m:
library/erland_rtti_implementation.m:
	Minor cleanups.

------------------------------------------

CHANGES TO THE TEST SUITE:

tests/invalid/any_passed_as_ground.err_exp2:
tests/invalid/invalid_default_func_1.err_exp2:
tests/invalid/invalid_default_func_3.err_exp2:
tests/invalid/try_detism.err_exp2:
	Add second expected output files for these tests. We need alternate
	expected outputs because the numbers of some of the typeinfo variables
	mentioned in error message are different depending on whether or not
	const structures are enabled.
2012-06-08 15:37:07 +00:00
Peter Wang
b86f973fa9 Allow the use of Mercury abstract machine float registers for passing
Branches: main

Allow the use of Mercury abstract machine float registers for passing
double-precision float arguments in higher order calls.

In of itself this is not so useful for typical Mercury code.  However, as
all non-local procedures are potentially the targets of higher order calls,
without this change first order calls to non-local procedures could not use
float registers either.  That is the actual motivation for this change.

The basic mechanism is straightforward.  As before, do_call_closure_* is
invoked to place the closure's hidden arguments into r1, ..., rN, and extra
input arguments shifted into rN+1, etc.  With float registers, extra input
arguments may also be in f1, f2, etc. and the closure may also have hidden
float arguments.  Optimising for calls, we order the closure's hidden
arguments so that all float register arguments come after all regular
register arguments in the vector.  Having the arguments out of order does
complicate code which needs to deconstruct closures, but that is not so
important.

Polymorphism complicates things.  A closure with type pred(float) may be
passed to a procedure expecting pred(T).  Due to the `float' argument type,
the closure expects its argument in a float register.  But when passed to the
procedure, the polymorphic argument type means it would be called with the
argument in a regular register.

Higher-order insts already contain information about the calling convention,
without which a higher-order term cannot be called.  We extend higher-order
insts to include information about the register class required for each
argument.  For example, we can distinguish between:

	pred(in) is semidet /* arg regs: [reg_f] */
and
	pred(in) is semidet /* arg regs: [reg_r] */

Using this information, we can create a wrapper around a higher-order
variable if it appears in a context requiring a different calling convention.
We do this in a new HLDS pass, called float_regs.m.

Note: Mercury code has a tendency to lose insts for higher-order terms, then
"recover" them by hacky means.  The float_regs pass depends on higher-order
insts; it is impossible to create a wrapper for a procedure without knowing
how to call it.  The float_regs pass will report errors which we otherwise
accepted, due to higher-order insts being unavailable.  It should be possible
for the user to adjust the code to satisfy the pass, though the user may not
understand why it should be necessary.  In most cases, it probably really
*is* unnecessary.  We may be able to make the float_regs pass more tolerant
of missing higher-order insts in the future.

Class method calls do not use float registers because I didn't want to deal
with them yet.


compiler/options.m:
compiler/handle_options.m:
	Always enable float registers in low-level C grades when floats are
	wider than a word.

compiler/make_hlds_passes.m:
	Always allow double word floats to be stored unboxed in cells on C
	grades.

compiler/hlds_goal.m:
	Add an extra field to `generic_call' which gives the register class
	to use for each argument.  This is set by the float_regs pass.

compiler/prog_data.m:
	Add an extra field to `pred_inst_info' which records the register class
	to use for each argument.  This is set by the float_regs pass.

compiler/hlds_pred.m:
	Add a field to `proc_sub_info' which lists the headvars which must be
	passed via regular registers despite their types.

	Add a field to `pred_sub_info' to record the original unsubstituted
	argument types for instance method predicates.

compiler/check_typeclass.m:
	In the pred_info of an instance method predicate, record the original
	argument types before substituting the type variables for the instance.

compiler/float_regs.m:
compiler/transform_hlds.m:
	Add the new HLDS pass.

compiler/mercury_compile_middle_passes.m:
	Run the new pass if float registers are enabled.

compiler/lambda.m:
	Export the predicate to produce a predicate from a lambda.
	This is reused by float_regs.m to create wrapper closures.

	Add an argument to `expand_lambda' to set the reg_r_headvars field on
	the newly created procedure.

	Delete some unused fields from `lambda_info'.

compiler/arg_info.m:
	Make `generate_proc_arg_info' no longer always use regular registers
	for calls to exported procedures.  Do always use regular registers for
	class methods calls.

	Add a version of `make_arg_infos' which takes an explicit list of
	argument registers.  Rename the previous version.

	Add `generic_call_arg_reg_types' to return the argument registers
	for a generic call.

	Add a version of `compute_in_and_out_vars' which additionally separates
	arguments for float and regular registers.

compiler/call_gen.m:
	Use float registers for argument passing in higher-order calls, as
	directed by the new field in `generic_call'.

compiler/code_util.m:
	Add a function to encode the number of regular and float register
	arguments when making a higher-order call.

compiler/llds.m:
	Say that the `do_call_closure_N' functions only work for zero float
	register arguments.

compiler/follow_vars.m:
compiler/interval.m:
	Account for the use of float registers by generic call goals in these
	passes.

compiler/unify_gen.m:
	Move float register arguments to the end of a closure's hidden
	arguments vector, after regular register arguments.

	Count hidden regular and float register arguments separately, but
	encode them in the same word in the closure.  This is preferable to
	using two words because it reduces the differences between grades
	with and without float registers present.

	Disable generating code which creates a closure from an existing
	closure, if float registers exist.  That code does not understand the
	reordered hidden arguments vector yet.

compiler/continuation_info.m:
	Replace an argument's type_info in the closure layout if the argument
	is a float *and* is passed via a regular register, when floats are
	normally passed via float registers.  Instead, give it the type_info
	for `private_builtin.float_box'.

compiler/builtin_lib_types.m:
	Add function to return the type of `private_builtin.float_box/0'.

compiler/hlds_out_goal.m:
compiler/hlds_out_pred.m:
compiler/mercury_to_mercury.m:
	Dump the new fields added to `generic_call', `pred_inst_info' and
	`proc_sub_info'.

compiler/prog_type.m:
	Add helper predicate.

compiler/*.m:
	Conform to changes.

library/private_builtin.m:
	Add a type `float_box'.

runtime/mercury_ho_call.h:
	Describe the modified closure representation.

	Rename the field which counts the number of hidden arguments to prevent
	it being used incorrectly, as it now encodes two numbers (potentially).

	Add macros to unpack the encoded field.

runtime/mercury_ho_call.c:
	Update the description of how higher-order calls work.

	Update code which extracts closure arguments to take account the
	arguments being reordered in the hidden arguments vector.

runtime/mercury_deep_copy.c:
runtime/mercury_deep_copy_body.h:
runtime/mercury_layout_util.c:
runtime/mercury_ml_expand_body.h:
	Update code which extracts closure arguments to take account the
	arguments being reordered in the hidden arguments vector.

runtime/mercury_type_info.c:
runtime/mercury_type_info.h:
	Add helper function.

tools/make_spec_ho_call:
	Update the generated do_call_closure_* functions to place float
	register arguments.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/ho_float_reg.exp:
tests/hard_coded/ho_float_reg.m:
	Add new test case.

tests/hard_coded/copy_pred.exp:
tests/hard_coded/copy_pred.m:
tests/hard_coded/deconstruct_arg.exp:
tests/hard_coded/deconstruct_arg.exp2:
tests/hard_coded/deconstruct_arg.m:
	Extend test cases with float arguments in closures.

tests/debugger/higher_order.exp2:
	Add alternative output, changed due to closure wrapping.

tests/hard_coded/ho_univ_to_type.m:
	Adjust test case so that the float_regs pass does not report errors
	about missing higher-order insts.

compiler/notes/compiler_design.html:
	Describe the new module.

	Delete a duplicated paragraph.

compiler/notes/todo.html:
TODO:
	Delete one hundred billion year old todos.
2012-02-13 00:11:57 +00:00
Zoltan Somogyi
585c1d623c Fix a problem with from_ground_term scopes.
Estimated hours taken: 20
Branches: main

Fix a problem with from_ground_term scopes. When they are built, the scopes
are tentantively marked as from_ground_term_construct scopes, and the
unifications inside them are in a top down order. Mode analysis therefore
expected the unifications inside from_ground_term_construct scopes to have
that order.

The problem was that mode analysis, when it confirmed that a
from_ground_term scope is indeed a from_ground_term_construct scope,
itself reversed the order of the unifications, putting them in a bottom up
order. When mode analysis is reinvoked, either for unique mode checking, or
after cse_detection finds common subexpressions, this meant that
mode analysis found the unifications in the "wrong" order, and therefore
disregarded the scope, discarding all its performance benefits.

This diff separates out the two notions that we previously conflated.
The scope kind from_ground_term_construct now refers only to scopes
which are definitely known to construct ground terms. We can know that
only after mode analysis. Until then, from_ground_term scopes are now marked
as from_ground_term_initial. The two kinds have different though overlapping
sets of invariants; in particular, they promise different orderings of the
unifications in the scope.

This diff reduces the time needed to compile mas_objects.data.m
from about 221 seconds to about 8.

compiler/hlds_goal.m:
	Add the from_ground_term_initial kind. Document the invariants
	that each kind of from_ground_term scope promises.

compiler/superhomogeneous.m:
	Mark from_ground_term scopes initially as from_ground_term_initial,
	not from_ground_term_construct.

compiler/post_typecheck.m:
	Make the predicate that converts function calls that look like
	unifications (such as X = int.min) into actual function calls
	say whether it performed such a conversion.

compiler/purity.m:
	Use the new functionality in post_typecheck.m to convert
	from_ground_term_initial scopes into from_ground_term_other scopes
	if the conversion of a unification into a call means that we have to
	break an invariant expected of from_ground_term_initial scopes.

compiler/cse_detection.m:
compiler/switch_detection.m:
	Maintain the invariants we now expect of from_ground_term_deconstruct
	scopes.

compiler/modecheck_goal.m:
	Maintain the invariants we now expect of the different
	from_ground_term scopes.

	Avoid traversing such scopes if a previous invocation of mode analysis
	says we can.

	Optimize away from_ground_term_construct scopes if the variable being
	constructed is not needed later.

compiler/quantification.m:
	If the variable named in a from_ground_term_initial or
	from_ground_term_construct scope is not referred to outside the scope,
	set the nonlocals set of the scope to empty, which allows later
	compiler passes to optimize it away.

	Avoid some unnecessary work by the compiler.

compiler/add_trail_ops.m:
compiler/closure_analysis.m:
compiler/constraint.m:
compiler/dead_proc_elim.m:
compiler/deep_profile.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/exception_analysis.m:
compiler/follow_code.m:
compiler/follow_vars.m:
compiler/goal_form.m:
compiler/goal_util.m:
compiler/granularity.m:
compiler/inlining.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lco.m:
compiler/middle_rec.m:
compiler/mode_util.m:
compiler/parallel_to_plain.m:
compiler/simplify.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/tabling_analysis.m:
compiler/term_pass1.m:
compiler/try_expand.m:
compiler/tupling.m:
compiler/untupling.m:
compiler/unused_args.m:
	Avoid traversing from_ground_term_deconstruct scopes in cases
	where the invariants that now hold (mainly the absence of anything
	but deconstruct unifications) make such traversals unnecessary.

compiler/live_vars.m:
compiler/liveness.m:
compiler/structure_reuse.lbu.m:
	Add comments about exploiting from_ground_term_deconstruct scopes.

compiler/det_analysis.m:
compiler/hlds_out_goal.m:
compiler/polymorphism.m:
compiler/saved_vars.m:
compiler/unique_modes.m:
	Handle from_ground_term_initial scopes.

compiler/handle_options.m:
	Add a dump verbosity option that is useful for comparing HLDS dumps
	created by two different compilers.

compiler/type_util.m:
	Minor speedup.

compiler/mode_info.m:
compiler/modecheck_conj.m:
compiler/prog_data.m:
compiler/rbmm.region_transformation.m:
compiler/typecheck.m:
	Improve documentation.
2011-08-31 07:59:35 +00:00
Zoltan Somogyi
295415090e Convert almost all remaining modules in the compiler to use
Estimated hours taken: 6
Branches: main

compiler/*.m:
	Convert almost all remaining modules in the compiler to use
	"$module, $pred" instead of "this_file" in error messages.

	In a few cases, the old error message was misleading, since it
	contained an incorrect, out-of-date or cut-and-pasted predicate name.

tests/invalid/unresolved_overloading.err_exp:
	Update an expected output containing an updated error message.
2011-05-23 05:08:24 +00:00
Julien Fischer
9ae7fe6b70 Change the argument ordering of predicates in the set module.
Branches: main

Change the argument ordering of predicates in the set module.

library/set.m:
	Change predicate argument orders to match the versions
	in the svset module.

	Group function definitions with the corresponding predicates
	rather than at the end of the file.

	Delete Ralph's comments regarding the argument order in the
	module interface: readers of the library reference guide are
	unlikely to be interested in his opinion of the argument ordering
	ten or so years ago.

	Add extra modes for set.map/3 and set.map_fold/5.

library/svset.m:
library/eqvclass.m:
library/tree234.m:
library/varset.m:
browser/*.m:
compiler/*.m:
deep_profiler/*.m:
mdbcomp/trace_counts.m:
extras/moose/grammar.m:
extras/moose/lalr.m:
extras/moose/moose.m:
tests/hard_coded/bitset_tester.m:
	Conform to the above change.

NEWS:
	Announce the above changes.
2011-05-06 05:03:29 +00:00
Zoltan Somogyi
b3fa535100 A rewrite of the state variable transformation from the ground up.
Estimated hours taken: 60
Branches: main

A rewrite of the state variable transformation from the ground up.

The initial aim was to avoid situations (encountered in the g12 project)
in which the old state variable transformation generated code that
did not satisfy the mode checker, due to unnecessary unifications.
The new system tries hard to minimize the number of unifications added to the
program. It does this by relying extensively on the idea that in a branched
structure such as an disjunction, if two branches both update the same state
variable, and the variables representing the last state of the state variable
in the two branches are (say) X and Y, and we pick X to represent the current
state after the disjunction, then we don't have to put the assignment X := Y
into the second branch; instead, we can RENAME Y to X in that branch.
To avoid renaming a goal several times (for itself, for its parent, for its
grandparent etc), we delay all renamings until the end, when we do it all
in one traversal.

The old state var system was opaque and hard to understand, partly because
its basic operations did different things in different contexts. The new system
is a much more direct expression of the intuitive meaning of state variables;
it keeps track of their state much as the programmer writing the original code
would. It should therefore be significantly easier to understand and to modify
in the future.

The new system can also detect more kinds of errors in the use of state
variables. For example it can discover that some branches of a disjunction or
if-then-else set the initial value of a state variable and some do not.
This is ok if the non-setting-branch cannot succeed; if it can, then it is
a bug. We therefore generate messages about such branches, but print them
only if mode analysis finds a bug in the procedure, since in that case,
the lack of initialization may be the cause of the bug.

doc/reference_manual.texi:
	Replaced an old example that didn't know what it was talking about,
	and thoroughly confused the issue of what is legal use of state
	variables and what is not.

compiler/state_var.m:
	Rewrite this module along the lines mentioned above.

compiler/options.m:
	Add two new options. One, warn-state-var-shadowing, controls whether
	we generate warnings for one state var shadowing another (which
	G12 has lots of). The other, --allow-defn-for-builtins, is for
	developers only; it is needed to bootstrap changes that add new
	builtins. I needed this for a form of the state variable transformation
	that used calls to a new builtin predicate to copy the values of state
	variables in branches that did not modify them, even though other
	branches did. I ultimately used unifications to do this copying,
	for reasons documented in state_var.m.

compiler/add_clause.m:
compiler/add_pragma.m:
	Respect the new --allow-defn-for-builtins option.
	(Previously, we changed the code that now looks up the value of the
	option.)

doc/user_guide.texi:
	Document the --warn-state-var-shadowing option.

	Fix some old documentation about dump options.

compiler/simplify.m:
	Fix an old oversight: list the predicates in table_builtin.m that may
	have calls introduced to them by table_gen.m.

compiler/superhomogeneous.m:
compiler/field_access.m:
compiler/add_clause.m:
compiler/goal_expr_to_goal.m:
	Together with state_var.m, these modules contain the transformation
	from the parse tree to the HLDS. Since the change to state_var.m
	involves significant changes in its interface (such as separating out
	the persistent and location-dependent aspects of the information needed
	by the state variable transformation), and needing callbacks at
	different points than the old transformation, these modules had to
	change extensively as well to conform.

	goal_expr_to_goal.m is a new module carved out of add_clause.m.
	It deserves a module of its own because its code has a significantly
	different purpose than add_clause.m. The two separate modules each
	have much better cohesion than the old conjoined module did.

	In superhomogeneous.m, replace two predicates that did the same thing
	with one predicate.

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

compiler/hlds_goal.m:
	Add a mechanism to do the kind of incremental renaming that the state
	variable transformation needs.

	Add some utility predicates needed by the new code in other modules.

compiler/hlds_clause.m:
compiler/hlds_pred.m:
	Add an extra piece of information to clauses and proc_infos:
	a list of informational messages generated by the state variable
	transformation about some branches of branched goals not giving initial
	values to some state variables, while other branches do.

	The state variable transformation fills in this field in clauses
	where relevant.

compiler/clause_to_proc.m:
	Copy this list of messages from clauses to proc_infos.

compiler/modes.m:
	When generating an error message for a procedure, include this list
	of messages from the state var transformation in the output.

compiler/handle_options.m:
	Add a dump alias for debugging the state var transformation.

compiler/hlds_out_goal.m:
	Add a predicate that is useful in trace messages when debugging
	the compiler.

compiler/hlds_out_pred.m:
	Print goal path and goal id information in clauses as well as
	proc_infos, since the state var transformation now uses goal ids.

compiler/prog_item.m:
	In lists of quantified vars in scope headers, separate out the vars
	introduced as !S from those introduced as !.S and !:S. This makes it
	easier for the state var transformation to handle them.

	Document that we expect lists of quantified variables and state
	variables to contain no duplicates. The state var transformation
	is slightly simpler if we impose this requirement, and quantifying
	a variable twice in the same scope does not make sense, and is
	therefore almost certainly an error.

compiler/prog_io_util.m:
	Generate error messages when a variable or state variable IS
	listed twice in the same quantification list.

	Factor out some code used to generate error messages.

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

	Break a very large predicate into two smaller pieces.

compiler/add_class.m:
compiler/add_pragma.m:
compiler/add_pred.m:
compiler/assertion.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/headvar_names.m:
compiler/hhf.m:
compiler/hlds_out_module.m:
compiler/inlining.m:
compiler/intermod.m:
compiler/mercury_to_mercury.m:
compiler/module_imports.m:
compiler/module_qual.m:
compiler/post_typecheck.m:
compiler/prog_io_goal.m:
compiler/prog_util.m:
compiler/purity.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
	Conform to the changes above.

compiler/mode_constraints.m:
compiler/modules.m:
compiler/structure_reuse.analysis.m:
	Avoid the warnings we now generate about one state variable shadowing
	another.

browser/declarative_user.m:
compiler/hlds_out_util.m:
compiler/ordering_mode_constraints.m:
compiler/table_gen.m:
deep_profiler/read_profile.m:
	Improve programming style.

library/require.m:
	Add expect_not, a negated version of expect.

library/varset.m:
	Return lists of new variables in order, not reverse order.

mdbcomp/mdbcomp.goal_path.m:
compiler/prog_mode.m:
	Add a utility predicate.

tests/debugger/tailrec1.exp:
tests/invalid/any_passed_as_ground.err_exp:
tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/state_vars_test1.err_exp:
tests/invalid/state_vars_test4.err_exp:
tests/invalid/try_bad_params.err_exp:
tests/invalid/try_detism.err_exp:
tests/invalid/purity/impure_pred_t1_fixed.err_exp:
tests/invalid/purity/impure_pred_t2.err_exp:
	Update the expected outputs of these test cases to account for
	incidental changes in variable numbers and goal paths after this
	change.

tests/general/state_vars_tests.{m,exp}:
	Remove the code that expected the state var transformation to do
	something that was actually AGAINST the reference manual: treating
	the step from the condition to the then part of an if-then-else
	expression (not a goal) as a sequence point.

tests/general/state_vars_trace.m:
	Add a test case that is not enabled yet, since we don't pass it.

tests/hard_coded/bit_buffer_test.m:
	Fix a bug in the test itself: the introduction of a state var twice
	in the same scope.

tests/hard_coded/try_syntax_6.m:
	Avoid a warning about state var shadowing.

tests/hard_coded/if_then_else_expr_state_var.{m,exp}:
	A new test to check the proper handling of state vars in if-then-else
	expressions.

tests/hard_coded/Mmakefile:
	Enable the new test.
2011-03-07 03:59:43 +00:00
Zoltan Somogyi
543fc6e342 Change the way the typechecker iterates over the predicates of the program.
Estimated hours taken: 12
Branches: main

Change the way the typechecker iterates over the predicates of the program.
We used to do it by looking up each predicate in the module_info,
typechecking it, and putting it back into the module_info. We now do it
by converting the predicate table into a list, iterating over the list
transforming each pred_info in it, converting the updated list back to
a predicate table.

The original intention of this change was to allow different predicates
to be typechecked in parallel by removing a synchronization bottleneck:
the typechecking of a predicate now doesn't have to wait for the typechecking
of the previous predicate to generate the updated version of the module_info.
However, it turned out that the change is good for sequential execution
as well, improving the time on tools/speedtest from 11.33 seconds to 11.08
seconds, a speedup of 2.2%. On tools/speedtest -l, which tests the compilation
of more modules, the speedup is even better: 3.1% (from 32.63 to 31.60s).

compiler/typecheck.m:
	Implement the above change.

compiler/hlds_module.m:
compiler/pred_table.m:
	Add a new operation, setting the list of valid pred_ids, now needed by
	typecheck.m, to both modules.

	Make the names of the predicates for accessing the predicate table
	more expressive, and make them conform to our naming conventions.

compiler/*.m:
	Trivial changes to conform to the change in hlds_module.m.

library/assoc_list.m:
	Add new predicates used by the new version of typecheck.m
	(at some time in its development).

NEWS:
	Mention the new predicates.

library/list.m:
	Improve documentation that is now copied to assoc_list.m.

tools/speedtest:
	Make the test command more easily configurable.
2010-07-30 05:16:26 +00:00
Zoltan Somogyi
c509c49bdc Add a mechanism for generating warnings when the various clauses of a predicate
Estimated hours taken: 24
Branches: main

Add a mechanism for generating warnings when the various clauses of a predicate
or function are not contiguous.

This mechanism consists of two options:

	--warn-non-contiguous-clauses
	--warn-non-contiguous-foreign-procs

The first option generates warnings when the Mercury clauses of a predicate
or function are not contiguous, but it ignores any foreign_procs of that
predicate or function, and thus allows these to be away from the Mercury
clauses and each other. This option is enabled by default.

The second option generating warnings unless both the Mercury clauses and
all the foreign_procs of the predicate or function are all contiguous.
This option is not enabled by default, because many library modules
group foreign_procs not by predicate, but by foreign language. (All C foreign
procs for a group of predicates, then all the Java foreign procs for that group
of predicates, etc.)

compiler/hlds_clauses.m:
	Store, next to the representation of each clause list, information
	about the locations (item numbers and context) of the clauses.
	We store two versions of this information, one version for each option.

	Make the predicates that access the clause list access the location
	information as well, to ensure that any code that adds clauses also
	records their location.

	Add a predicate that tests for non-contiguity.

	Add a specific type to represent the modes that a clause applies to.
	This replaces the error-prone scheme we used to use that represented
	the notion "this clause applies to all modes" with an empty list of
	modes. This allows us to remove the code in add_pragma.m that used
	to replace these empty lists with the list of actual modes they
	represented.

	Change the prefix on the fields of clauses_info to avoid ambiguities.
	Add a prefix to the names of the function symbols of the clauses_rep
	type to avoid ambiguities.

compiler/add_clause.m:
compiler/add_pragma.m:
	When adding Mercury clauses and pragma foreign_procs to a predicate or
	function, record the location of the clause or foreign_procs. We do so
	even if the clause or foreign_proc is overridden by another. For
	example, when compiling to C, a Mercury clause overrides an Erlang
	foreign_proc, and a C foreign_proc overrides a Mercury clause.

	Fix an old bug where a foreign_proc that should override Mercury
	clauses overrode only one Mercury clause, and left the others
	in the predicate, to yield a disjunction with some Mercury disjuncts
	and a foreign_proc disjunct. This disjunction would then yield
	determinism errors if it had outputs.

	The new code that fixes the bug has a much more direct implicit
	correctness argument, and should be significantly easier to understand.
	It also avoids doing unnecessary work. (The old code could make a
	decision to ignore a clause, yet still proceed to transform it,
	just to ignore the result of the transformation.)

compiler/options.m:
	Add the new options.

doc/user_guide.texi:
	Document the new options. Fix an inconsistency between options.m and
	user_guide.texi for a nearby option.

compiler/make_hlds_passes.m:
	Pass the information that add_clause.m and add_pragma.m need.

compiler/typecheck.m:
	Detect non-contiguous clauses and call typecheck_errors to generate
	error messages.

compiler/typecheck_errors.m:
	Add functions for formatting error messages about non-contiguous
	clauses.

compiler/hlds_out.m:
	Do not print the modes to which a clause applies for the usual case,
	in which the clause applies to all modes.

compiler/clause_to_proc.m:
	Simplify some code.

	Rename a predicate to better reflect its purpose.

	Conform to the changes above.

compiler/intermod.m:
	Rename a predicate to avoid ambiguities.

	Conform to the changes above.

compiler/add_class.m:
compiler/add_pred.m:
compiler/add_special_pred.m:
compiler/assertion.m:
compiler/build_mode_constraints.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/goal_path.m:
compiler/headvar_names.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/hlds_pred.m:
compiler/implementation_defined_literals.m:
compiler/mode_constraints.m:
compiler/modes.m:
compiler/ordering_mode_constraints.m:
compiler/polymorphism.m:
compiler/post_typecheck.m:
compiler/proc_gen.m:
compiler/prop_mode_constraints.m:
compiler/purity.m:
compiler/type_constraints.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
	Conform to the changes above.

compiler/goal_form.m:
	The new warnings pointed out a non-contiguous clause in goal_form.m.
	Since this clause happened to be a duplicate of another clause, this
	diff deletes it. The duplicate clause was not detected because the
	predicate is semidet, and has no outputs.

compiler/mlds_to_c.m:
compiler/rbmm.points_to_analysis.m:
deep_profiler/measurements.m:
library/library.m:
library/list.m:
	Fix non-contiguous clauses pointed out by the new warnings.

library/bit_buffer.m:
	Fix programming style.

tests/invalid/types2.err_exp:
	This test has non-contiguous clauses, so expect the new warning.

tests/warnings/warn_contiguous.{m,exp}:
tests/warnings/warn_non_contiguous.{m,exp}:
tests/warnings/warn_non_contiguous_foreign.{m,exp}:
tests/warnings/warn_non_contiguous_foreign_group.{m,exp}:
	New test cases that exercise the new capability.

tests/warnings/Mmakefile:
tests/warnings/Mercury.options:
	Enable and specify the options for the new tests.
2009-08-19 07:45:13 +00:00
Zoltan Somogyi
d69ba1a1f0 Include the type_ctor in cons_ids for user-defined types.
Estimated hours taken: 32
Branches: main

Include the type_ctor in cons_ids for user-defined types. The intention is
two-fold:

- It prepares for a future in which we allow more than one function symbol to
  with the same name to be defined in a module.

- It makes the HLDS code more self-contained. In many places, processing
  construction and deconstruction unifications required knowing which type
  the cons_id belongs to, but until now, code couldn't know that unless it
  kept track of the type of the variable unified with the cons_id.

With this diff, user-defined cons_ids are represented as

	cons(SymName, Arity, TypeCtor)

The last field is filled in during post-typecheck. After that time, any module
qualification in the SymName (which may initially be partial) is redundant,
since it is also available in the TypeCtor.

In the future, we could make all those SymNames be just unqualified(_) at that
time. We could also replace the current maps in HLDS type definitions with
full cons_id keys with just name/arity keys (since the module qualifier is a
given for any given type definition), we could also support partially
qualified cons_ids in source code using a map from name/arity pairs to a list
of all the type_ctors that have function symbols with that name/arity, instead
of our current practice of inserting all possible partially module qualified
version of every cons_id into a single giant table, and we could do the same
thing with the field names table.

This diff also separates tuples out from user-defined types, since in many
respects they are different (they don't have a single type_ctor, for starters).
It also separates out character constants, since they were alreay treated
specially in most places, though not in some places where they *ought* to
have been treated specially. Take the opportunity to give some other cons_ids
better names.

compiler/prog_data.m:
	Make the change described above, and document it.

	Put the implementations of the predicates declared in each part
	of this module next to the declarations, instead of keeping all the
	code until the very end (where it was usually far from their
	declarations).

	Remove three predicates with identical definitions from inst_match.m,
	inst_util.m and mode_constraints.m, and put the common definition
	in prog_data.m.

library/term_io.m:
	Add a new predicate that is basically a reversible version of
	the existing function espaced_char, since the definition of char_consts
	needs reversibilty.

compiler/post_typecheck.m:
	For functors of user-defined types, record their type_ctor. For tuples
	and char constants, record them as such.

compiler/builtin_lib_types.m:
compiler/parse_tree.m:
compiler/notes/compiler_design.html:
	New module to centralize knowledge about builtin types, specially
	handled library types, and their function symbols. Previously,
	the stuff now in this module used to be in several different places,
	including prog_type.m and stm_expand.m, and some of it was duplicated.

mdbcomp/prim_data.m:
	Add some predicates now needed by builtin_lib_types.m.

compiler/builtin_ops.m:
	Factor out some duplicated code.

compiler/add_type.m:
	Include the relevant type_ctors in the cons_ids generated in type
	definitions.

compiler/hlds_data.m:
	Document an existing type better.

	Rename a cons_tag in sync with its corresponding cons_id.

	Put some declarations into logical order.

compiler/hlds_out.m:
	Rename a misleadingly-named predicate.

compiler/prog_ctgc.m:
compiler/term_constr_build.m:
	Add XXXs for questionable existing code.

compiler/add_clause.m:
compiler/add_heap_ops.m:
compiler/add_pragma.m:
compiler/add_pred.m:
compiler/add_trail_ops.m:
compiler/assertion.m:
compiler/bytecode_gen.m:
compiler/closure_analysis.m:
compiler/code_info.m:
compiler/complexity.m:
compiler/ctgc_selector.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/delay_partial_inst.m:
compiler/dependency_graph.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/distance_granularity.m:
compiler/erl_rtti.m:
compiler/erl_unify_gen.m:
compiler/export.m:
compiler/field_access.m:
compiler/foreign.m:
compiler/format_call.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/hlds_code_util.m:
compiler/hlds_desc.m:
compiler/hlds_goal.m:
compiler/implementation_defined_literals.m:
compiler/inst_check.m:
compiler/inst_graph.m:
compiler/inst_match.m:
compiler/inst_util.m:
compiler/instmap.m:
compiler/intermod.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lco.m:
compiler/make_tags.m:
compiler/mercury_compile.m:
compiler/mercury_to_mercury.m:
compiler/middle_rec.m:
compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_switch_gen.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_java.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/mode_ordering.m:
compiler/mode_util.m:
compiler/modecheck_unify.m:
compiler/modes.m:
compiler/module_qual.m:
compiler/polymorphism.m:
compiler/prog_ctgc.m:
compiler/prog_event.m:
compiler/prog_io_util.m:
compiler/prog_mode.m:
compiler/prog_mutable.m:
compiler/prog_out.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/purity.m:
compiler/qual_info.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/rtti.m:
compiler/rtti_out.m:
compiler/rtti_to_mlds.m:
compiler/simplify.m:
compiler/simplify.m:
compiler/special_pred.m:
compiler/ssdebug.m:
compiler/stack_opt.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.direct.detect_garbagem:
compiler/superhomoegenous.m:
compiler/switch_detection.m:
compiler/switch_gen.m:
compiler/switch_util.m:
compiler/table_gen.m:
compiler/term_constr_build.m:
compiler/term_norm.m:
compiler/try_expand.m:
compiler/type_constraints.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/typecheck.m:
compiler/typecheck_errors.m:
compiler/unify_gen.m:
compiler/unify_proc.m:
compiler/unify_modes.m:
compiler/untupling.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Minor changes, mostly to ignore the type_ctor in cons_ids in places
	where it is not needed, take the type_ctor from the cons_id in places
	where it is more convenient, conform to the new names of some cons_ids,
	conform to the changes in hlds_out.m, and/or add now-needed imports
	of builtin_lib_types.m.

	In some places, the handling previously applied to cons/2 (which
	included tuples and character constants as well as user-defined
	function symbols) is now applied only to user-defined function symbols
	or to user-defined function symbols and tuples, as appropriate,
	with character constants being handled more like the other kinds of
	constants.

	In inst_match.m, rename a whole bunch of predicates to avoid
	ambiguities.

	In prog_util.m, remove two predicates that did almost nothing yet were
	far too easy to misuse.
2009-06-11 07:00:38 +00:00
Peter Wang
cc8923b7f7 Add support for `try' goals, syntactic sugar on top of the exception handling
Branches: main

Add support for `try' goals, syntactic sugar on top of the exception handling
predicates in the standard library.  The syntax is documented in the reference
manual.  Currently one of the proposed try parameters, io(!IO), is implemented.

Try goals are implemented by *two* source-to-source transformations, one at the
parse tree level, then a later one on the HLDS.  The reason for this is given
in try_expand.m.


library/ops.m:
	Add three new operators: try, catch, catch_any.

library/exception.m:
	Add forwarding predicates so that code generated by the try goal
	transformation doesn't need to import `univ'.

compiler/prog_item.m:
	Add representations of try goals to parse tree.

compiler/prog_io_goal.m:
	Parse try goals.

	Unrelated: fix spelling of "parameter".

compiler/add_clause.m:
	Perform the initial transformation on try goals.

compiler/hlds_goal.m:
	Add try goals to HLDS, as shorthand goals.

compiler/try_expand.m:
	New module to perform the latter transformation on try goals.

compiler/check_hlds.m:
	Import the new module.

compiler/mercury_compile.m:
	Call the try_expand pass near the end of the front-end pass.

compiler/module_imports.m:
	Implicitly import `exception' if a `try' goal is seen.

compiler/det_analysis.m:
	Conform to addition of try goals in HLDS.  Make it not wrap a commit
	scope around the intermediate try goal (as it might if there are no
	outputs) as it is inappropriate.

compiler/prog_util.m:
	Conform to parse tree changes.

compiler/assertion.m:
compiler/build_mode_constraints.m:
compiler/cse_detection.m:
compiler/delay_partial_inst.m:
compiler/dependency_graph.m:
compiler/det_report.m:
compiler/equiv_type_hlds.m:
compiler/format_call.m:
compiler/goal_form.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/hlds_out.m:
compiler/intermod.m:
compiler/lambda.m:
compiler/make_hlds_warn.m:
compiler/mercury_to_mercury.m:
compiler/mode_util.m:
compiler/modes.m:
compiler/module_qual.m:
compiler/ordering_mode_constraints.m:
compiler/polymorphism.m:
compiler/post_typecheck.m:
compiler/prop_mode_constraints.m:
compiler/purity.m:
compiler/quantification.m:
compiler/simplify.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.lfu.m:
compiler/switch_detection.m:
compiler/type_constraints.m:
compiler/typecheck.m:
compiler/unique_modes.m:
compiler/unused_imports.m:
	Conform to changes in HLDS.

compiler/il_peephole.m:
compiler/ilasm.m:
compiler/ilds.m:
compiler/mlds_to_il.m:
	Rename some symbols to avoid the new keywords.

library/exception.m:
	Add two helper predicates for the try goal transformations.

doc/reference_manual.texi:
NEWS:
	Document and announce the new language feature.

compiler/notes/compiler_design.html:
	Note new compiler module.

tests/hard_coded/Mmakefile:
tests/hard_coded/try_syntax_1.exp:
tests/hard_coded/try_syntax_1.m:
tests/hard_coded/try_syntax_2.exp:
tests/hard_coded/try_syntax_2.m:
tests/hard_coded/try_syntax_3.exp:
tests/hard_coded/try_syntax_3.m:
tests/hard_coded/try_syntax_4.exp:
tests/hard_coded/try_syntax_4.m:
tests/hard_coded/try_syntax_5.exp:
tests/hard_coded/try_syntax_5.m:
tests/hard_coded/try_syntax_6.exp:
tests/hard_coded/try_syntax_6.m:
tests/hard_coded/try_syntax_7.exp:
tests/hard_coded/try_syntax_7.m:
tests/invalid/Mmakefile:
tests/invalid/try_bad_params.err_exp:
tests/invalid/try_bad_params.m:
tests/invalid/try_io_else.err_exp:
tests/invalid/try_io_else.m:
	Add test cases.

tests/debugger/declarative/catch.m:
	The module name is now a keyword so requires brackets.

tests/invalid/trace_goal_env.err_exp:
	Update test case for fixed spelling for "parameter".

vim/syntax/mercury.vim:
	Add try, catch, catch_any as keywords.
2009-03-10 05:00:34 +00:00
Ben Mellor
edda5238a1 Fix two bugs preventing the use of or_else alternatives to atomic
transaction scopes and nested atomic scopes.

The main issue was the code assuming that the inner stm interface variables
were the same for the main goal and the or_else goals, but quantification
renames these variables to be distinct.


library/stm_builtin.m
    Rename stm_from_outer_to_inner_io and stm_from_inner_to_outer_io, dropping
    the _io suffix. They are used when the outer type is stm as well as io.

compiler/hlds_goal.m
    Add a field to the atomic_goal structure to hold the names of the STM
    interface variables used in each or_else alternative, since quantification
    renames these variables to be distinct from those used in the main atomic
    goal.

compiler/purity.m
    Insert calls to the (renamed) dummy predicates stm_from_outer_to_inner and
    stm_from_inner_to_outer regardless of the type of the outer variable,
    instead of using the nonexistent stm_from_*_to_*_stm versions when the
    outer type is stm. Also use the inner STM interface variables specific to
    each or_else alternative when generating these calls.

compiler/quantification.m
    Explicitly rename the inner STM interface variables in each of the or_else
    alternatives before calling implicitly_quantify_disj, so that the new names
    of these variables can be stored in the atomic_goal structure.

compiler/typecheck.m
    Ensure all the inner STM interface variables are typed stm, not just the
    ones in the main goal.

compiler/add_clause.m
compiler/assertion.m
compiler/build_mode_constraints.m
compiler/cse_detection.m
compiler/delay_partial_inst.m
compiler/dependency_graph.m
compiler/det_analysis.m
compiler/det_report.m
compiler/equiv_type_hlds.m
compiler/format_call.m
compiler/goal_form.m
compiler/goal_path.m
compiler/goal_util.m
compiler/hlds_out.m
compiler/intermod.m
compiler/lambda.m
compiler/make_hlds_warn.m
compiler/mode_util.m
compiler/modes.m
compiler/ordering_mode_constraints.m
compiler/polymorphism.m
compiler/post_typecheck.m
compiler/prop_mode_constraints.m
compiler/simplify.m
compiler/stm_expand.m
compiler/stratify.m
compiler/structure_reuse.lfu.m
compiler/switch_detection.m
compiler/unique_modes.m
compiler/unused_imports.m
    Updated to reflect the extra field in atomic_goal.
2009-02-26 23:43:08 +00:00
Zoltan Somogyi
5ad9a27793 Speed up the compiler's handling of code that constructs large ground terms
Estimated hours taken: 80
Branches: main

Speed up the compiler's handling of code that constructs large ground terms
by specializing the treatment of such code.

This diff reduces the compilation time for training_cars_full.m from 106.9
seconds to 30.3 seconds on alys, my laptop. The time on tools/speedtest
stays pretty much the same.

compiler/hlds_goal.m:
	Record the classification of from_ground_term scopes as purely
	constructing terms, purely deconstructing them or something other.

	Fix an old potential bug: variables inside the construct_how fields
	of unifications weren't being renamed along with other variables.
	This is a bug if any part of the compiler later looks at those
	variables. (I am not sure whether or not this happens.)

compiler/superhomogenous.m:
	Provisionally mark newly constructed static terms as being
	from_ground_term_construct. Mode checking will either confirm this
	or change the scope kind.

compiler/options.m:
compiler/handle_options.m:
	Add a new option, from_ground_term_threshold, that allows the user to
	set the boundary between ground terms that get scopes and ground terms
	do not. I plan to experiment with different settings later.

compiler/modes.m:
	Make this classification. For scopes that construct ground terms,
	use a specialized algorithm that avoids quadratic behavior.
	(It does not access the unify_inst_table, which is where the
	factor of N other than the length of the goal list came from.)
	The total size of the instmap_deltas, if printed out, still looks like
	O(N^2) in size, but due to structure sharing it needs only O(N) memory.

	For scopes that construct ground terms, set the determinism information
	so that det_analysis.m doesn't have to traverse such scopes.

	When handling disjunctions, check whether some nonlocals of the
	disjunctions are constructed by from_ground_term_construct scopes.
	For any such nonlocals, set their insts to just ground, throwing away
	the precise information we have about exactly what function symbols
	they and ALL their subterms are bound to. This is HUGE win, since
	it allows us avoid spending a lot of time building a huge merge_inst
	table, which later passes of the compiler (e.g. equiv_type_hlds) would
	then have to spend similarly huge times traversing.

	This approach does have a down side. If lots of arms of a disjunction
	bind a nonlocal to a large ground term, but a few bind it to a SMALL
	ground term, a term below the from_ground_term_threshold, this
	optimization won't kick in. That could be one purpose of the new
	option. It isn't documented yet; I will seek feedback about its
	usefulness first.

compiler/modecheck_unify.m:
	Handle the three different kinds of right hand sides separately.
	This yields a small speedup, because now we don't test rhs_vars and
	rhs_functors (the common right hand sides) for a special case
	(goals containing "any" insts) that is applicable only to
	rhs_lambda_goals.

compiler/unique_modes.m:
	Don't traverse scopes that construct ground terms, since modes.m has
	already done everything that needs to be done.

compiler/det_analysis.m:
	Don't traverse scopes that construct ground terms, since modes.m has
	already done the needed work.

compiler/instmap.m:
	Add a new predicate for use by modes.m.

	Many predicate names in this module were quite uninformative; give them
	informative names.

compiler/polymorphism.m:
	If this pass invalidates the from_ground_term_construct invariants,
	then mark the relevant scope as from_ground_term_other.

	Delete two unused access predicates.

compiler/equiv_type_hlds.m:
	Don't traverse scopes that construct ground terms, since modes.m
	ensures that their instmap deltas do not contain typed insts, and
	thus the scope cannot contain types that need to be expanded.

	Convert some predicates to single clauses.

compiler/goal_form.m:
compiler/goal_util.m:
	In predicates that test goals for various properties, don't traverse
	scopes that construct ground terms when the outcome of the test
	is the same for all such scopes.

	Convert some predicates to single clauses.

compiler/simplify.m:
	Do not look for common structs in from_ground_term_construct scopes,
	both because this speeds up the compiler, and because retaining
	references to ground terms is in fact a pessimization, not an
	optimization. This is because (a) those references need to be stored in
	stack slots across calls, and (b) the C code generators ensure that
	the cells representing ground terms will be shared as needed.

	If all arms of a switch are from_ground_term_construct scopes,
	do not merge the instmap_deltas from those arms, since this is
	both time-consuming (even after the other changes in this diff)
	and extremely unlikely to improve the instmap_delta.

	Disable common_struct in from_ground_term_construct scopes,
	since for these scopes, it is actually a pessimization.

	Do not delete from_ground_term_construct scopes, since many
	compiler passes can now use them.

	Do some manual deforestation, break up some large predicates,
	and give better names to some.

compiler/liveness.m
	Special-case the handling from_ground_term_construct scopes. This
	allows us to traverse them just once instead of three times, and this
	traversal is simpler and faster than any of the three.

	In some traversals, we were switching on the goal type twice; once
	in e.g. detect_liveness_in_goal_2, and once by calling
	goal_expr_has_subgoals. Eliminate the double switching by merging
	the relevant predicates. (The double-switching structure was easier
	to work with before we had multi-cons-id switches.)

compiler/typecheck.m:
	Move a lookup after a test, so we don't have to do it if the test
	fails.

	Provide a specialized mode for a predicate. This should allow the
	compiler to eliminate an argument and a test in the common case.

	Note a possible chance for a speedup.

compiler/typecheck_info.m:
	Don't apply empty substitutions to the types of a possibly very large
	set of variables.

compiler/quantification.m:
	Don't quantify from_ground_term_construct scopes. They are created
	correctly quantified, and any compiler pass that invalidates that
	quantification also removes the from_ground_term_construct mark.

	Don't apply empty renamings to a possibly very large set of variables.

	Move the code for handling scopes to its own predicate, to avoid
	overwhelming the code that handles other kinds of goals. Even from
	this, factor out the renaming code, since it is needed only for
	some kinds of scopes.

	Make some predicate names better reflect what the predicate does.

compiler/pd_cost.m:
	For from_ground_term_construct scopes, instead of computing their cost
	by adding up the costs of the goals inside, make their cost a constant,
	since binding a variable to a static term takes constant time.

compiler/pd_info.m:
	Add prefixes on field names to avoid ambiguities.

compiler/add_heap_ops.m:
compiler/add_trail_ops.m:
compiler/closure_analysis.m:
compiler/constraint.m:
compiler/cse_detection.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/distance_granularity.m:
compiler/exception_analysis.m:
compiler/follow_code.m:
compiler/follow_vars.m:
compiler/format_call.m:
compiler/granularity.m:
compiler/higher_order.m:
compiler/implicit_parallelism.m:
compiler/inlining.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/loop_inv.m:
compiler/middle_rec.m:
compiler/mode_util.m:
compiler/parallel_to_plain_conj.m:
compiler/saved_vars.m:
compiler/stm_expand.m:
compiler/store_alloc.m:
compiler/stratify.m:
compiler/structure_reuse.direct.detect_garbage.m:
compiler/structure_reuse.lbu.m:
compiler/structure_sharing.analysis.m:
compiler/switch_detection.analysis.m:
compiler/trail_analysis.m:
compiler/term_pass1.m:
compiler/tupling.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
	These passes have nothing to do in from_ground_term_construct scopes,
	so don't traverse them.

	In some modules (e.g. dead_proc_elim), some traversals had to be kept.

	In loop_inv.m, replace a code structure that updated accumulators
	with functions (which prevented the natural use of state variables),
	that in lots of places reconstructed the term it had just
	deconstructed, and obscured the identical handling of different kinds
	of goals, with a structure based on predicates, state variables and
	shared code for different goal types where possible.

	In store_alloc.m, avoid some double switching on the same value.

	In stratify.m, unneeded_code.m and unused_args.m, rename predicates
	to avoid ambiguities.

compiler/goal_path.m:
compiler/goal_util.m:
compiler/implementation_defined_literals.m:
compiler/intermode.m:
compiler/mark_static_terms.m:
compiler/ml_code_gen.m:
compiler/mode_ordering.m:
compiler/ordering_mode_constraints.m:
compiler/prop_mode_constraints.m:
compiler/purity.m:
compiler/rbmm.actual_region_arguments.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.region_transformation.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/term_const_build.m:
compiler/term_traversal.m:
compiler/unused_imports.m:
	Mark places where we cannot (yet) special case
	from_ground_term_construct scopes.

	In structure_reuse.lfu.m, turn nested if-then-elses into a switch in.

compiler/size_prof.m:
	Turn from_ground_term_construct scopes into from_ground_term_other
	scopes, since in term size profiling grades, we need to attach sizes to
	terms.

	Give predicates better names.

compiler/*.m:
	Minor changes to conform to the changes above.

compiler/make_hlds_passes.m:
	With -S, print statistics after the third pass over items, since
	this is the time-consuming one.

compiler/mercury_compile.m:
	Conform to the new names of some predicates.

	When declining to output a HLDS dump because it would be identical to
	the previous dump, don't confuse the user either by being silent about
	the decision, or by leaving an old dump laying around that could be
	mistaken for a new one.

tools/binary:
tools/binary_step:
	Bring these tools up to date.

compiler/Mmakefile:
	Add an int3s target for use by the new code in the tools. The
	Mmakefiles in the other directories with Mercury code already have
	such a target.

compiler/notes/allocation.html:
	Fix an out-of-date reference.

tests/debugger/polymorphic_ground_term.{m,inp,exp}:
	New test case to check whether liveness.m handles typeinfo liveness
	of ground terms correctly.

tests/debugger/Mmakefile:
	Enable the new test case.

tests/debugger/polymorphic_output.{m,exp}:
	Fix tab/space mixup.
2008-12-23 01:38:03 +00:00
Peter Wang
fffedfba7c Add support for "implementation-defined literals" $file, $line, $module,
Estimated hours taken: 16
Branches: main

Add support for "implementation-defined literals" $file, $line, $module,
$pred, $grade which are replaced constants by the compiler.

library/lexer.m:
	Add a new type of token.

	Read "$foo" as a `implementation_defined' token instead of two name
	tokens.

library/term.m:
library/term_io.m:
	Add a new type of constant, `implementation_defined'.

library/parser.m:
	Handle `implementation_defined' tokens from the lexer.

compiler/check_hlds.m:
compiler/implementation_defined_literals.m:
compiler/mercury_compile.m:
	Add a new pass to replace implementation-defined literals in program
	clauses.

	Call the new pass.

compiler/notes/compiler_design.html:
	Document the new module.

compiler/prog_data.m:
	Add a new option to `cons_id', namely `implementation_defined_const'.

compiler/typecheck.m:
	Tell the typechecker the types of the supported implementation-defined
	literals.

compiler/prog_io_util.m:
	Make `convert_bound_inst' fail if implementation-defined literals
	appear in inst definitions so that an error will be issued.

compiler/bytecode_gen.m:
compiler/ctgc.selector.m:
compiler/dead_proc_elim.m:
compiler/dependency_graph.m:
compiler/erl_unify_gen.m:
compiler/fact_table.m:
compiler/higher_order.m:
compiler/hlds_code_util.m:
compiler/hlds_out.m:
compiler/inst_check.m:
compiler/mercury_to_mercury.m:
compiler/mode_util.m:
compiler/module_qual.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/rbmm.execution_path.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Conform to addition of `implementation_defined_const'.

doc/reference_manual.texi:
	Document implementation-defined literals.

NEWS:
	Announce the new feature.

tests/hard_coded/Mmakefile:
tests/hard_coded/impl_def_lex.exp:
tests/hard_coded/impl_def_lex.m:
tests/hard_coded/impl_def_lex_string.exp:
tests/hard_coded/impl_def_lex_string.m:
tests/hard_coded/impl_def_literal.exp:
tests/hard_coded/impl_def_literal.m:
tests/invalid/Mmakefile:
tests/invalid/impl_def_literal_syntax.err_exp:
tests/invalid/impl_def_literal_syntax.m:
tests/invalid/undef_impl_def_literal.err_exp:
tests/invalid/undef_impl_def_literal.m:
	Add test cases.
2008-04-03 05:26:48 +00:00
Zoltan Somogyi
b000cb322e Provide compiler support for Software Transactional Memory through the new
Estimated hours taken: 80 by zs, and lots more by lmika
Branches: main

Provide compiler support for Software Transactional Memory through the new
atomic goal. This work was done by Leon Mika; I merely brought it up to date,
resolved conflicts, and cleaned up a few things. There are still several
aspects that are as yet incomplete.

library/ops.m:
	Add the operators needed for the syntax of atomic scopes.

library/stm_builtin.m:
	Add the builtin operations needed for the implementation of atomic
	goals.

compiler/hlds_goal.m:
	Add a new HLDS goal type, which represents an atomic goal and its
	possible fallbacks (in case an earlier goal throws an exception).

	Rename the predicate goal_is_atomic as goal_expr_has_subgoals,
	since now its old name would be misleading.

compiler/prog_data.m:
compiler/prog_item.m:
	Add a parse tree representation of the new kind of goal.

compiler/prog_io_goal.m:
	Parse the new kind of goal.

compiler/add_clause.m:
	Translate atomic goals from parse tree form to HLDS.

compiler/typecheck.m:
compiler/typecheck_errors.m:
	Do type checking of atomic goals.

compiler/modes.m:
	Do mode checking of atomic goals, and determine whether they are nested
	or not.

compiler/unique_modes.m:
	Do unique mode checking of atomic goals.

compiler/stm_expand.m:
	New module to expand atomic goals into sequences of simpler goals.

library/stm_builtin.m:
	Add the primitives needed by the transformation.

	Improve the existing debugging support.

mdbcomp/prim_data.m:
	Add utility functions to allow stm_expand.m to refer to modules in the
	library.

mdbcomp/program_representation.m:
	Expand the goal_path type to allow the representation of components of
	atomic goals.

compiler/notes/compiler_design.html:
	Document the new module.

compiler/transform_hlds.m:
	Include the new module in the compiler.

compiler/mercury_compile.m:
	Invoke the STM transformation.

compiler/hlds_module.m:
	Add an auxiliary counter used by the STM transformation.

compiler/hlds_pred.m:
	Add a new predicate origin: the STM transformation.

compiler/modules.m:
	Import the STM builtin module automatically if the module contains any
	atomic goals.

compiler/assertion.m:
compiler/bytecode_gen.m:
compiler/clause_to_proc.m:
compiler/code_gen.m:
compiler/code_info.m:
compiler/code_util.m:
compiler/constraint.m:
compiler/cse_detection.m:
compiler/deep_profiling.m:
compiler/code_util.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/distance_granularity.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/exception_analysis.m:
compiler/follow_code.m:
compiler/format_call.m:
compiler/goal_form.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/granularity.m:
compiler/hlds_out.m:
compiler/implicit_parallelism.m:
compiler/inlining.m:
compiler/intermod.m:
compiler/lambda.m:
compiler/layout_out.m:
compiler/lco.m:
compiler/lookup_switch.m:
compiler/make_hlds_warn.m:
compiler/mark_static_terms.m:
compiler/mercury_to_mercury.m:
compiler/middle_rec.m:
compiler/ml_code_gen.m:
compiler/mode_constraint_robdd.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/mode_info.m:
compiler/mode_util.m:
compiler/ordering_mode_constraints.m:
compiler/pd_cost.m:
compiler/pd_util.m:
compiler/polymorphism.m:
compiler/post_typecheck.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prop_mode_constraints.m:
compiler/rbmm.actual_region_arguments.m:
compiler/rbmm.add_rbmm_goal_info.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_transformation.m:
compiler/saved_vars.m:
compiler/simplify.m:
compiler/size_prog.m:
compiler/smm_common.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.direct.detect_garbage.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lbu.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/switch_detection.m:
compiler/unused_imports.m:
compiler/granularity.m:
compiler/granularity.m:
	Conform to the changes above. Mostly this means handling the new
	kind of goal.

compiler/add_heap_ops.m:
compiler/add_trail_ops.m:
compiler/build_mode_constraints.m:
compiler/closure_analysis.m:
compiler/dead_proc_elim.m:
compiler/deforest.m:
compiler/follow_vars.m:
compiler/higher_order.m:
compiler/live_vars.m:
compiler/liveness.m:
compiler/loop_inv.m:
compiler/module_qual.m:
compiler/prog_util.m:
compiler/purity.m:
compiler/quantification.m:
compiler/store_alloc.m:
compiler/stratify.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_pass1.m:
compiler/term_traversal.m:
compiler/trailing_analysis.m:
	Conform to the changes above. Mostly this means handling the new
	kind of goal.

	Switch syntax from clauses to disj.

runtime/mercury_stm.[ch]:
	Implement the primitives needed by the STM transformation.

	Add more debugging support to the existing primitives.

library/term.m:
	Generalize get_term_context to work on terms of all kinds.
2008-02-27 07:23:57 +00:00
Zoltan Somogyi
cc88711d63 Implement true multi-cons_id arm switches, i.e. switches in which we associate
Estimated hours taken: 40
Branches: main

Implement true multi-cons_id arm switches, i.e. switches in which we associate
more than one cons_id with a switch arm. Previously, for switches like this:

	(
		X = a,
		goal1
	;
		( X = b
		; X = c
		),
		goal2
	)

we duplicated goal2. With this diff, goal2 won't be duplicated. We still
duplicate goals when that is necessary, i.e. in cases which the inner
disjunction contains code other than a functor test on the switched-on var,
like this:

	(
		X = a,
		goal1
	;
		(
			X = b,
			goalb
		;
			X = c
			goalc
		),
		goal2
	)

For now, true multi-cons_id arm switches are supported only by the LLDS
backend. Supporting them on the MLDS backend is trickier, because some MLDS
target languages (e.g. Java) don't support the concept at all. So when
compiling to MLDS, we still duplicate the goal in switch detection (although
we could delay the duplication to just before code generation, if we wanted.)

compiler/options.m:
	Add an internal option that tells switch detection whether to look for
	multi-cons_id switch arms.

compiler/handle_options.m:
	Set this option based on the back end.

	Add a version of the "trans" dump level that doesn't print unification
	details.

compiler/hlds_goal.m:
	Extend the representation of switch cases to allow more than one
	cons_id for a switch arm.

	Add a type for representing switches that also includes tag information
	(for use by the backends).

compiler/hlds_data.m:
	For du types, record whether it is possible to speed up tests for one
	cons_id (e.g. cons) by testing for the other (nil) and negating the
	result. Recording this information once is faster than having
	unify_gen.m trying to compute it from scratch for every single
	tag test.

	Add a type for representing a cons_id together with its tag.

compiler/hlds_out.m:
	Print out the cheaper_tag_test information for types, and possibly
	several cons_ids for each switch arm.

	Add some utility predicates for describing switch arms in terms of
	which cons_ids they are for.

	Replace some booleans with purpose-specific types.

	Make hlds_out honor is documentation, and not print out detailed
	information about unifications (e.g. uniqueness and static allocation)
	unless the right character ('u') is present in the control string.

compiler/add_type.m:
	Fill in the information about cheaper tag tests when adding a du type.

compiler/switch_detection.m:
	Extend the switch detection algorithm to detect multi-cons_id switch
	arms.

	When entering a switch arm, update the instmap to reflect that the
	switched-on variable can now be bound only to the cons_ids that this
	switch arm is for. We now need to do this, because if the arm contains
	another switch on the same variable, computing the can_fail field of
	that switch correctly requires us to know this information.
	(Obviously, an arm for a single cons_id is unlikely to have switch on
	the same variable, and for arms for several cons_ids, we previously
	duplicated the arm and left the unification with the cons_id in each
	copy, and this unification allowed the correct handling of any later
	switches. However, the code of a multi-cons_id switch arm obviously
	cannot have a unification with each cons_id in it, which is why
	we now need to get the binding information from the switch itself.)

	Replace some booleans with purpose-specific types, and give some
	predicates better names.

compiler/instmap.m:
	Provide predicates for recording that a switched-on variable has
	one of several given cons_ids, for use at the starts of switch arms.

	Give some predicates better names.

compiler/modes.m:
	Provide predicates for updating the mode_info at the start of a
	multi-cons_id switch arm.

compiler/det_report.m:
	Handle multi-cons_id switch arms.

	Update the instmap when entering each switch arm, since this is needed
	to provide good (i.e. non-misleading) error messages when one switch on
	a variable exists inside another switch on the same variable.

	Since updating the instmap requires updating the module_info (since
	the new inst may require a new entry in an inst table), thread the
	det_info through as updateable state.

	Replace some multi-clause predicate definitions with single clauses,
	to make it easier to print the arguments in mdb.

	Fix some misleading variable names.

compiler/det_analysis.m:
	Update the instmap when entering each switch arm and thread the
	det_info through as updateable state, since the predicates we call
	in det_report.m require this.

compiler/det_util.m:
	Handle multi-cons_id switch arms.

	Rationalize the argument order of some access predicates.

compiler/switch_util.m:
	Change the parts of this module that deal with string and tag switches
	to optionally convert each arm to an arbitrary representation of the
	arm. In the LLDS backend, the conversion process generated code for
	the arm, and the arm's representation is the label at the start of
	this code. This way, we can duplicate the label without duplicating
	the code.

	Add a new part of this module that associates each cons_id with its
	tag, and (during the same pass) checks whether all the cons_ids are
	integers, and if so what are min and max of these integers (needed
	for dense switches). This scan is needed because the old way of making
	this test had single-cons_id switch arms as one of its basic
	assumptions, and doing it while adding tags to each case reduces
	the number of traversals required.

	Give better names to some predicates.

compiler/switch_case.m:
	New module to handle the tasks associated with managing multi-cons_id
	switch arms, including representing them for switch_util.m.

compiler/ll_backend.m:
	Include the new module.

compiler/notes/compiler_design.html:
	Note the new module.

compiler/llds.m:
	Change the computed goto instruction to take a list of maybe labels
	instead of a list of labels, with any missing labels meaning "not
	reached".

compiler/string_switch.m:
compiler/tag_switch.m:
	Reorganize the way these modules work. We can't generate the code of
	each arm in place anymore, since it is now possible for more than one
	cons_id to call for the execution of the same code. Instead, in
	string_switch.m, we generate the codes of all the arms all at once,
	and construct the hash index afterwards. (This approach simplifies
	the code significantly.)

	In tag switches (unlike string switches), we can get locality benefits
	if the code testing for a cons_id is close to the code for that
	cons_id, so we still try to put them next to each other when such
	a locality benefit is available.

	In both modules, the new approach uses a utility predicate in
	switch_case.m to actually generate the code of each switch arm,
	eliminating several copies the same code in the old versions of these
	modules.

	In tag_switch.m, don't create a local label that simply jumps to the
	code address do_not_reached. Previously, we had to do this for
	positions in jump tables that corresponded to cons_ids that the switch
	variable could not be bound to. With the change to llds.m, we now
	simply generate a "no" instead.

compiler/lookup_switch.m:
	Get the info about int switch limits from our caller; don't compute it
	here.

	Give some variables better names.

compiler/dense_switch.m:
	Generate the codes of the cases all at once, then assemble the table,
	duplicate the labels as needed. This separation of concerns allows
	significant simplifications.

	Pack up all the information shared between the predicate that detects
	whether a dense switch is appropriate and the predicate that actually
	generates the dense switch.

	Move some utility predicates to switch_util.

compiler/switch_gen.m:
	Delete the code for tagging cons_ids, since that functionality is now
	in switch_util.m.

	The old version of this module could call the code generator to produce
	(i.e. materialize) the switched-on variable repeatedly. We now produce
	the variable once, and do the switch on the resulting rval.

compiler/unify_gen.m:
	Use the information about cheaper tag tests in the type constructor's
	entry in the HLDS type table, instead of trying to recompute it
	every time.

	Provide the predicates switch_gen.m now needs to perform tag tests
	on rvals, as opposed to variables, and against possible more than one
	cons_id.

	Allow the caller to provide the tag corresponding to the cons_id(s)
	in tag tests, since when we are generating code for switches, the
	required computations have already been done.

	Factor out some code to make all this possible.

	Give better names to some predicates.

compiler/code_info.m:
	Provide some utility predicates for the new code in other modules.
	Give better names to some existing predicates.

compiler/hlds_code_util.m:
	Rationalize the argument order of some predicates.

	Replace some multi-clause predicate definitions with single clauses,
	to make it easier to print the arguments in mdb.

compiler/accumulator.m:
compiler/add_heap_ops.m:
compiler/add_pragma.m:
compiler/add_trail_ops.m:
compiler/assertion.m:
compiler/build_mode_constraints.m:
compiler/check_typeclass.m:
compiler/closure_analysis.m:
compiler/code_util.m:
compiler/constraint.m:
compiler/cse_detection.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/distance_granularity.m:
compiler/dupproc.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/exception_analysis.m:
compiler/export.m:
compiler/follow_code.m:
compiler/follow_vars.m:
compiler/foreign.m:
compiler/format_call.m:
compiler/frameopt.m:
compiler/goal_form.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/granularity.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/implicit_parallelism.m:
compiler/inlining.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lambda.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/livemap.m:
compiler/liveness.m:
compiler/llds_out.m:
compiler/llds_to_x86_64.m:
compiler/loop_inv.m:
compiler/make_hlds_warn.m:
compiler/mark_static_terms.m:
compiler/middle_rec.m:
compiler/ml_tag_switch.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/mode_util.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/pd_cost.m:
compiler/pd_into.m:
compiler/pd_util.m:
compiler/peephole.m:
compiler/polymorphism.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/purity.m:
compiler/quantification.m:
compiler/rbmm.actual_region_arguments.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_paths.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/saved_vars.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/store_alloc.m:
compiler/stratify.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lbu.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_norm.m:
compiler/term_pass1.m:
compiler/term_traversal.m:
compiler/trailing_analysis.m:
compiler/transform_llds.m:
compiler/tupling.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/unify_proc.m:
compiler/unique_modes.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches.

compiler/ml_string_switch.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches.

	Give some predicates better names.

compiler/dependency_graph.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches. Change the order of arguments
	of some predicates to make this easier.

compiler/bytecode.m:
compiler/bytecode_data.m:
compiler/bytecode_gen.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches. (The bytecode interpreter
	has not been updated.)

compiler/prog_rep.m:
mdbcomp/program_representation.m:
	Change the byte sequence representation of goals to allow switch arms
	with more than one cons_id. compiler/prog_rep.m now writes out the
	updated representation, while mdbcomp/program_representation.m reads in
	the updated representation.

deep_profiler/mdbprof_procrep.m:
	Conform to the updated program representation.

tools/binary:
	Fix a bug: if the -D option was given, the stage 2 directory wasn't
	being initialized.

	Abort if users try to give that option more than once.

compiler/Mercury.options:
	Work around bug #32 in Mantis.
2007-12-30 08:24:23 +00:00
Julien Fischer
128349a922 Replace more if-then-elses with switches (particularly on the
Estimated hours taken: 0.5
Branches: main

Replace more if-then-elses with switches (particularly on the
type builtin_state/0).

compiler/cse_detection.m:
compiler/deep_profiling.m:
compiler/dependency_graph.m:
compiler/follow_vars.m:
compiler/goal_form.m:
compiler/interval.m:
compiler/live_vars.m:
compiler/lp_rational.m:
compiler/pd_cost.m:
compiler/simplify.m:
compiler/tupling.m:
	As above.
2007-10-10 14:35:27 +00:00
Mark Brown
0dfb9c4874 New module for directed graphs. This is essentially the relation
Estimated hours taken: 12
Branches: main

library/digraph.m:
	New module for directed graphs.  This is essentially the relation
	module but with more consistent terminology, and with argument
	ordering that suits state variables.  Other differences with the
	relation module:

	- The digraph_key type has a phantom type parameter, which helps to
	  ensure that keys from one digraph are not used with another digraph.

	- Exports a version of digraph.reduced which also returns the mapping
	  between the original digraph keys and the new ones.

	- The implementation of compose/3 doesn't try to use the "domain" and
	  "range" of the graphs (which is meaningless in the relation module
	  anyway).

	- New, more efficient algorithm for is_dag/1.  Correctness proof is
	  documented.

	- components/2 uses a more efficient data representation, and avoids
	  some intermediate data structures.

	- reduced/{2,3} avoids some intermediate data structures.

	- tc/2 avoids some intermediate data structures.

library/library.m:
	Add the new module.

library/relation.m:
	Document that this module is deprecated in favour of digraph.

	Flag relation.init/{0,1} as obsolete (it would be better to flag
	the entire module, or the relation/1 type as obsolete, but Mercury
	does not support this).

NEWS:
	Mention that the new module supersedes relation.m and svrelation.m.

compiler/*.m:
profiler/*.m:
	Use the digraph module rather than the relation module.
2007-09-07 15:08:21 +00:00
Zoltan Somogyi
27aaaf412c Fix the failure of the invalid/modes_erroneous test case, whose symptom was
Estimated hours taken: 5
Branches: main

Fix the failure of the invalid/modes_erroneous test case, whose symptom was
an error message about a "mode error in unification of `X' and `X'".
The root cause of the problem was that the renaming of head variables
computed by headvar_names.m was being applied too early, during typechecking.
The fix is to apply it after the frontend (all the passes that can generate
error messages).

To avoid slowdowns from larger pred_infos, this diff also moves the least
frequently used fields of pred_infos to a subterm. (Proc_infos already had
a subterm.) This leads to an almost 3% speedup.

compiler/headvar_names.m:
	Store the renaming instead of applying it.

compiler/simplify.m:
	Apply the renaming in invocations after the front end, since doing so
	may allow some excess assignments to be eliminated.

compiler/hlds_pred.m:
	Add fields to pred_infos and proc_infos for the renaming.

	Move the least frequently used fields of pred_infos into a
	pred_sub_info.

	Some fields of pred_infos were being accessed using predicates
	that did not follow our naming conventions, and some were accessed
	using field access functions that are now inappropriate; fix them all.

	Require the caller to provide the renaming when creating new pred_infos
	and proc_infos. This is to force the compiler components that do this
	to propagate the renaming fields of the original predicates and/or
	procedures to their modified versions.

	Convert that some old code that used if-then-elses to use switches
	instead.

compiler/hlds_out.m:
	Write out the new pred_info and proc_info fields.

compiler/*.m:
	Conform to the changes in hlds_pred.m.

compiler/hlds_clauses.m:
	Avoid ambiguity by giving a prefix to the fields of the clauses_info
	type.

tests/invalid/ho_type_mode_bug.err_exp:
tests/invalid/merge_ground_any.err_exp:
	Don't expect error messages about "X = X" anymore.
2007-05-17 03:53:13 +00:00
Zoltan Somogyi
b56885be93 Fix a bug that caused bootchecks with --optimize-constructor-last-call to fail.
Estimated hours taken: 12
Branches: main

Fix a bug that caused bootchecks with --optimize-constructor-last-call to fail.

The problem was not in lco.m, but in follow_code.m. In some cases,
(specifically, the LCMC version of insert_2 in sparse_bitset.m),
follow_code.m moved an impure goal (store_at_ref) into the arms of an
if-then-else without marking those arms, or the if-then-else, as impure.
The next pass, simplify, then deleted the entire if-then-else, since it
had no outputs. (The store_at_ref that originally appeared after the
if-then-else was the only consumer of its only output.)

The fix is to get follow_code.m to make branched control structures such as
if-then-elses, as well as their arms, semipure or impure if a goal being moved
into them is semipure or impure, or if they came from an semipure or impure
conjunction.

Improve the optimization of the LCMC version of sparse_bitset.insert_2, which
had a foreign_proc invocation of bits_per_int in it: replace such invocations
with a unification of the bits_per_int constant if not cross compiling.

Add a new option, --optimize-constructor-last-call-null. When set, LCMC will
assign NULLs to the fields not yet filled in, to avoid any junk happens to be
there from being followed by the garbage collector's mark phase.

This diff also makes several other changes that helped me to track down
the bug above.

compiler/follow_code.m:
	Make the fix described above.

	Delete all the provisions for --prev-code; it won't be implemented.

	Don't export a predicate that is not now used anywhere else.

compiler/simplify.m:
	Make the optimization described above.

compiler/lco.m:
	Make sure that the LCMC specialized procedure is a predicate, not a
	function: having a function with the mode LCMC_insert_2(in, in) = in
	looks wrong.

	To avoid name collisions when a function and a predicate with the same
	name and arity have LCMC applied to them, include the predicate vs
	function status of the original procedure included in the name of the
	new procedure.

	Update the sym_name of calls to LCMC variants, not just the pred_id,
	because without that, the HLDS dump looks misleading.

compiler/pred_table.m:
	Don't have optimizations like LCMC insert new predicates at the front
	of the list of predicates. Maintain the list of predicates in the
	module as a two part list, to allow efficient addition of new pred_ids
	at the (logical) end without using O(N^2) algorithms. Having predicates
	in chronological order makes it easier to look at HLDS dumps and
	.c files.

compiler/hlds_module.m:
	Make module_info_predids return a module_info that is physically
	updated though logically unchanged.

compiler/options.m:
	Add --optimize-constructor-last-call-null.

	Make the options --dump-hlds-pred-id, --debug-opt-pred-id and
	--debug-opt-pred-name into accumulating options, to allow the user
	to specify more than one predicate to be dumped (e.g. insert_2 and
	its LCMC variant).

	Delete --prev-code.

doc/user_guide.texi:
	Document the changes in options.m.

compiler/code_info.m:
	Record the value of --optimize-constructor-last-call-null in the
	code_info, to avoid lookup at every cell construction.

compiler/unify_gen.m:
compiler/var_locn.m:
	When deciding whether a cell can be static or not, make sure that
	we never make static a cell that has some fields initialized with
	dummy zeros, to be filled in for real later.

compiler/hlds_out.m:
	For goals that are semipure or impure, note this fact. This info was
	lost when I changed the representation of impurity from markers to a
	field.

mdbcomp/prim_data.m:
	Rename some ambiguous function symbols.

compiler/intermod.m:
compiler/trans_opt.m:
	Rename the main predicates (and some function symbols) of these modules
	to avoid ambiguity and to make them more expressive.

compiler/llds.m:
	Don't print line numbers for foreign_code fragments if the user has
	specified --no-line-numbers.

compiler/make.dependencies.m:
compiler/mercury_to_mercury.m:
compiler/recompilation.usage.m:
	Don't use io.write to write out information to files we may need to
	parse again, because this is vulnerable to changes to the names of
	function symbols (e.g. the one to mdbcomp/prim_data.m).

	The compiler still contains some uses of io.write, but they are
	for debugging. I added an item to the todo list of the one exception,
	ilasm.m.

compiler/recompilation.m:
	Rename a misleading function symbol name.

compiler/parse_tree.m:
	Don't import recompilation.m here. It is not needed (all the components
	of parse_tree that need recompilation.m already import it themselves),
	and deleting the import avoids recompiling almost everything when
	recompilation.m changes.

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

compiler/*.m:
browser/*.m:
slice/*.m:
	Conform to the change to mdbcomp.

library/sparse_bitset.m:
	Use some better variable names.
2007-01-19 07:05:06 +00:00
Zoltan Somogyi
ba93a52fe7 This diff changes a few types from being defined as equivalent to a pair
Estimated hours taken: 10
Branches: main

This diff changes a few types from being defined as equivalent to a pair
to being discriminated union types with their own function symbol. This
was motivated by an error message (one of many, but the one that broke
the camel's back) about "-" being used in an ambiguous manner. It will
reduce the number of such messages in the future, and will make compiler
data structures easier to inspect in the debugger.

The most important type changed by far is hlds_goal, whose function symbol
is now "hlds_goal". Second and third in importance are llds.instruction
(function symbol "llds_instr") and prog_item.m's item_and_context (function
symbol "item_and_context"). There are some others as well.

In several places, I rearranged predicates to factor the deconstruction of
goals into hlds_goal_expr and hlds_goal_into out of each clause into a single
point. In many places, I changed variable names that used "Goal" to refer
to just hlds_goal_exprs to use "GoalExpr" instead. I also changed variable
names that used "Item" to refer to item_and_contexts to use "ItemAndContext"
instead. This should make reading such code less confusing.

I renamed some function symbols and predicates to avoid ambiguities.

I only made one algorithmic change (at least intentionally).
In assertion.m, comparing two goals for equality now ignores goal_infos
for all kinds of goals, whereas previously it ignored them for most kinds
of goals, but for shorthand goals it was insisting on them being equal.
This seemed to me to be a bug. Pete, can you confirm this?
2007-01-06 09:23:59 +00:00
Julien Fischer
b4c3bb1387 Clean up in unused module imports in the Mercury system detected
Estimated hours taken: 3
Branches: main

Clean up in unused module imports in the Mercury system detected
by --warn-unused-imports.

analysis/*.m:
browser/*.m:
deep_profiler/*.m:
compiler/*.m:
library/*.m:
mdbcomp/*.m:
profiler/*.m:
slice/*.m:
	Remove unused module imports.

	Fix some minor departures from our coding standards.

analysis/Mercury.options:
browser/Mercury.options:
deep_profiler/Mercury.options:
compiler/Mercury.options:
library/Mercury.options:
mdbcomp/Mercury.options:
profiler/Mercury.options:
slice/Mercury.options:
	Set --no-warn-unused-imports for those modules that are used as
	packages or otherwise break --warn-unused-imports, e.g. because they
	contain predicates with both foreign and Mercury clauses and some of
	the imports only depend on the latter.
2006-12-01 15:04:40 +00:00
Zoltan Somogyi
82eab0e65e Add an optional pass that tries to avoid generating too many parallel goals.
Estimated hours taken: 12
Branches: main

Add an optional pass that tries to avoid generating too many parallel goals.
The first transformation implemented by this pass is to transform parallel
conjunctions into goals of the form

	( queues already contain lot of work ->
		sequential version of parallel conjunction
	;
		parallel conjunction as before
	)

if they contain recursive calls.

The effect of this transformation is to reduce the overhead of the new par_fib
test case from:

	fib(35): sequential 189 vs parallel 5770

to

	fib(35): sequential 189 vs parallel 1090

i.e. a speedup of more than a factor of five.

compiler/granularity.m:
	New module that implements this transformation. I intend to add other,
	more sophisticated transformations in the future.

compiler/transform_hlds.m:
	Add granularity.m as one of the submodules of transform_hlds.m.

compiler/mercury_compile.m:
	Invoke the new pass.

	Invoke dep_par_conj only if needed.

	Fix some stage numbers.

compiler/notes/compiler_design.html:
	Document the new module.

	Document some modules that should have been documented earlier.

	Fix a hurried deletion of a reference to the Aditi backend.

compiler/goal_util.m:
	Add some utility functions for use by the new module.

compiler/simplify.m:
	Record the information mercury_compile.m needs in order to check
	whether we have any parallelism for granularity.m and dep_par_conj.m
	to process.

compiler/hlds_module.m:
	Add a slot to the module_info to record the information from
	simplify.m. Clean up some interfaces.

compiler/add_type.m:
	Conform to the change to hlds_module.m.

compiler/dependency_graph.m:
	Delete unnecessary module qualifications, and rename some predicates
	to avoid potential ambiguities.

compiler/options.m:
	Add the options required for controlling the new transformation.

	Rename an option's internal name to avoid conflict with a language
	keyword (the user-visible name remains unchanged).

	Move some options around to put them in logical groups.

doc/user_guide.texi:
	Document the new options.

	Fix some omissions in some earlier options.

compiler/handle_options.m:
compiler/termination.m:
	Conform to the option rename.

compiler/quantification.m:
	Rename some predicates to avoid ambiguities.

library/par_builtin.m:
	Add a predicate for use by the new transformation.

tests/par_conj/par_fib.{m,exp}:
	A new test case: a version of fib for use in testing parallelism.

tests/par_conj/Mmakefile:
	Enable the new test case.
2006-11-03 08:31:22 +00:00
Zoltan Somogyi
2b2f3d3cbe This diff contains no algorithmic changes.
Estimated hours taken: 8
Branches: main

This diff contains no algorithmic changes. It merely renames apart a bunch of
function symbols to reduce ambiguity. Basically I went through prog_data.m,
prog_item.m, hlds_data.m, hlds_goal.m and hlds_pred.m looking for type
definitions containing function symbol names that were either language
"keywords" (e.g. "terminates", which is an annotation on foreign_procs),
used with slightly different meanings in several types (e.g. "sym"),
or both (e.g. "call"). When I found such type definitions, I changed the
names of the function symbols, usually by adding a prefix or suffix
indicating the type to all function symbols of the type. For example,
the old function symbol "foreign_proc" in type "pragma_type" is now named
"pragma_foreign_proc", and the names of all other function symbols in that
type also start with "pragma_".

All of this should yield simpler compiler error messages when we make mistakes,
and will make it more likely that looking up a function symbol using a tags
file will take you to the actual definition of the relevant instance of that
function symbol. However, the most important benefit is the increase in
the readability of unfamiliar code; the reader won't have to emulate the
compiler's type ambiguity resolution algorithm (which in many cases used to
require distinguishing between f/14 and f/15 by counting the arguments,
e.g. for "pred_or_func").

compiler/prog_data.m:
compiler/prog_item.m:
compiler/hlds_data.m:
compiler/hlds_goal.m:
compiler/hlds_pred.m:
	Rename function symbols as explained above.

compiler/*.m:
	Conform to the function symbol renames.

	In some cases, rename other function symbols as well.

	Minor style fixes, e.g. replace if-then-elses with switches,
	or simple det predicates with functions.
2006-08-20 08:21:36 +00:00