Commit Graph

38 Commits

Author SHA1 Message Date
Zoltan Somogyi
a47de48c4d s/input_stream/text_input_stream/ ...
... and the same for output streams.
2023-04-24 14:59:20 +10:00
Zoltan Somogyi
6bdd8b84ee Move maybe_changed to maybe_succeeded.m and rename it.
compiler/maybe_util.m:
    Move the maybe_changed type from several modules of the compiler
    to maybe_succeeded.m, and rename it to maybe_util.m.

compiler/libs.m:
compiler/notes/compiler_design.html:
    Implement and document the rename.

compiler/common.m:
compiler/compile_target_code.m:
compiler/decide_type_repn.m:
compiler/det_analysis.m:
compiler/det_util.m:
compiler/equiv_type.m:
compiler/equiv_type_hlds.m:
compiler/file_util.m:
compiler/llds_out_file.m:
compiler/make.build.m:
compiler/make.dependencies.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.top_level.m:
compiler/make.track_flags.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_mlds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_type.m:
compiler/mlds_to_cs_file.m:
compiler/mlds_to_java_file.m:
compiler/module_cmds.m:
compiler/parse_tree_out.m:
compiler/process_util.m:
compiler/recompilation.version.m:
compiler/write_module_interface_files.m:
    Conform to the changes above.
2023-04-21 17:24:30 +10:00
Zoltan Somogyi
6f82724091 Pass streams explicitly at the top levels.
compiler/mercury_compile_main.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_make_hlds.m:
compiler/mercury_compile_middle_passes.m:
compiler/mercury_compile_mlds_back_end.m:
    Pass progress and error streams explicitly in these top modules
    of the compiler. Use "XXX STREAM" to mark places where we could switch
    from using stderr for both the progress and error streams to using
    module-specific files as the progress and/or error streams.

compiler/passes_aux.m:
    Add a "maybe_" prefix to the names of the predicates that print progress
    messages at the appropriate verbosity levels, as their printing of those
    messages is conditional.

    Provide versions of those predicates that take explicitly specified
    streams to write to, and mark the versions that write to the current
    output stream as obsolete.

    The predicate that wrote progress messages for procedures
    used to have two versions, one taking a pred_proc_id, and one taking
    a pred_id/proc_id pair. Delete the latter, because the arity difference
    that differentiated the two versions is now needed for the difference
    between supplying and not supplying an explicit stream.

compiler/file_util.m:
compiler/hlds_error_util.m:
compiler/write_error_spec.m:
    Delete several predicates that wrote to the current output stream,
    since all their callers now use the versions that specify an explicit
    output stream.

compiler/check_promise.m:
compiler/check_typeclass.m:
compiler/closure_analysis.m:
compiler/complexity.m:
compiler/cse_detection.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/deps_map.m:
compiler/direct_arg_in_out.m:
compiler/grab_modules.m:
compiler/handle_options.m:
compiler/hhf.m:
compiler/inlining.m:
compiler/make.module_dep_file.m:
compiler/ml_proc_gen.m:
compiler/ml_top_gen.m:
compiler/mode_constraints.m:
compiler/modes.m:
compiler/polymorphism.m:
compiler/purity.m:
compiler/read_modules.m:
compiler/recompilation.check.m:
compiler/saved_vars.m:
compiler/simplify_proc.m:
compiler/size_prof.m:
compiler/stack_opt.m:
compiler/switch_detection.m:
compiler/typecheck.m:
compiler/unique_modes.m:
compiler/unneeded_code.m:
compiler/write_module_interface_files.m:
    Get these modules to take an explicitly specified stream to which
    to write progress messages when they are invoked from mercury_compile_*.m.

    For predicates in these modules that can be invoked both directly
    by mercury_compile_*.m *and* by other modules, the latter effectively
    as a subcontractor, make them take a maybe(stream), with the intention
    being that all the other modules that use the predicate as a subcontractor
    would pass a "no". This avoids the need to pass progress streams
    down to the internals of other passes, and also avoids overwhelming
    the user invoking the compiler with unnecessary details.

    As above, and also delete a progress message that shouldn't be needed
    anymore.

    Move a test of option value compatibility from
    mercury_compile_middle_passes.m to handle_options.m, where it belongs.

compiler/float_regs.m:
    Write a debug message to the debug stream.

compiler/pd_info.m:
    Include the progress stream in the pd_info structure, because this is
    the simplest way to ensure that all parts of the partial deduction pass
    have access to it.

compiler/make.build.m:
compiler/make.program_target.m:
compiler/make.track_flags.m:
    Make the minimal changes needed to conform to the changes above.
    The rest can be done when the make package is converted to consistently
    use explicit streams.

compiler/bytecode_gen.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
    Make the minimal changes needed to conform to the changes above.
    The rest can be done when these modules start being maintained again.

compiler/Mercury.options:
    Stop specifying --no-warn-implicit-stream-calls for mercury_compile_*.m,
    since this diff makes that unnecessary.

    Start specifying --no-warn-implicit-stream-calls for some modules that
    are not currently being actively maintained, because the addition of
    progress-reporting predicates that take explicitly specified streams
    would otherwise cause the generation of such warnings for them.
2022-11-01 11:33:41 +11:00
Zoltan Somogyi
307b1dc148 Split up error_util.m into five modules.
compiler/error_spec.m:
    This new module contains the part of the old error_util.m that defines
    the error_spec type, and some functions that can help construct pieces
    of error_specs. Most modules of the compiler that deal with errors
    will need to import only this part of the old error_util.m.

    This change also renames the format_component type to format_piece,
    which matches our long-standing naming convention for variables containing
    (lists of) values of this type.

compiler/write_error_spec.m:
    This new module contains the part of the old error_util.m that
    writes out error specs, and converts them to strings.

    This diff marks as obsolete the versions of predicates that
    write out error specs to the current output stream, without
    *explicitly* specifying the intended stream.

compiler/error_sort.m:
    This new module contains the part of the old error_util.m that
    sorts lists of error specs and error msgs.

compiler/error_type_util.m:
    This new module contains the part of the old error_util.m that
    convert types to format_pieces that generate readable output.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Include and document the new modules.

compiler/error_util.m:
    The code remaining in the original error_util.m consists of
    general utility predicates and functions that don't fit into
    any of the modules above.

    Delete an unneeded pair of I/O states from the argument list
    of a predicate.

compiler/file_util.m:
    Move the unable_to_open_file predicate here from error_util.m,
    since it belongs here. Mark another predicate that writes
    to the current output stream as obsolete.

compiler/hlds_error_util.m:
    Mark two predicates that wrote out error_spec to the current output
    stream as obsolete, and add versions that take an explicit output stream.

compiler/Mercury.options:
    Compile the modules that call the newly obsoleted predicates
    with --no-warn-obsolete, for the time being.

compiler/*.m:
    Conform to the changes above, mostly by updating import_module
    declarations, and renaming format_component to format_piece.
2022-10-12 20:50:16 +11:00
Julien Fischer
cedbee7190 Move quote_arg/1 out of options.m.
Move the function quote_arg/1, which is used to quote arguments to shell
commands, out of options.m and into its own module. It is called from
several other places other than options.m and its implementation details
have nothing to do with the other contents of options.m.

Rename the function to quote_shell_cmd_arg/1.

compiler/shell_util.m:
    New module for quote_arg/1 and its supporting predicates.

compiler/libs.m:
    Include the new module.

compiler/options.m:
compiler/compile_target_code.m:
compiler/file_util.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
    Import the new module where necessary.

compiler/notes/compiler_design.m:
    Update this document.
2022-07-25 23:43:25 +10:00
Zoltan Somogyi
4ea9695030 Move predicates that find modules' files to find_module.m.
compiler/file_util.m:
compiler/find_module.m:
    As above. Users may specify a module by its module name or
    by its file name, but having the predicates that search
    for a module's source, interface or optimization files by file name
    be in file_util.m while similar predicates that search by module name
    be in find_module.m is suboptimal, since it prevents factoring out
    any commonalities, and makes it harder to spot things that *should*
    be common but arent't.

compiler/check_libgrades.m:
compiler/compile_target_code.m:
compiler/make.module_dep_file.m:
compiler/make.util.m:
compiler/mercury_compile_front_end.m:
compiler/mmc_analysis.m:
compiler/options_file.m:
compiler/parse_module.m:
    Update module imports.
2022-05-08 21:33:20 +10:00
Zoltan Somogyi
297dab88f8 Construct error messages using more readable code.
compiler/file_util.m:
compiler/find_module.m:
    As above.

    Also, put quotes around directory names where that wasn't done before.

tests/invalid_options_file/nonexistent_file.err_exp:
    Expect quotes around a directory name.
2022-04-29 04:40:09 +10:00
Zoltan Somogyi
25b4b67403 Carve io.file.m out of io.m.
library/io.file.m:
library/io.m:
    Move two sections of io.m, the "file handling predicates" section
    and the "handling temporary files" section to the new submodule io.file.m.

    Leave behind in io.m "forwarding predicates", predicates that do nothing
    except call the moved predicates in io.file.m, to provide backward
    compatibility. But do mark the forwarding predicates as obsolete,
    to tell people to update their (at their leisure, since the obsoleteness
    warning can be turned off).

    Also leave behind in io.m the definitions of the two types used
    by some parameters of some of the moved predicates. Document the reason
    why this is done.

library/MODULES_DOC:
    List the new module among the documented modules.

NEWS:
    Announce the changes.

browser/browse.m:
browser/interactive_query.m:
browser/listing.m:
compiler/analysis.file.m:
compiler/compile_target_code.m:
compiler/export.m:
compiler/fact_table.m:
compiler/file_util.m:
compiler/handle_options.m:
compiler/make.build.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.util.m:
compiler/mercury_compile_main.m:
compiler/module_cmds.m:
compiler/parse_module.m:
compiler/passes_aux.m:
compiler/prog_event.m:
compiler/recompilation.check.m:
compiler/write_deps_file.m:
compiler/write_module_interface_files.m:
deep_profiler/conf.m:
deep_profiler/mdprof_cgi.m:
library/dir.m:
mdbcomp/program_representation.m:
ssdb/ssdb.m:
    Call the file operation predicates directly in io.file.m, not indirectly
    through io.m.

    In two modules, add a #include of fcntl.h in C code. These modules contain
    C code that needs this #include, but until now, they got it via a copy
    in an automatically generated C header file of a foreign_decl pragma
    in io.m that contained that #include. This diff moves that foreign_decl
    to io.file.m, removing that crutch.

tests/debugger/browser_test.m:
tests/hard_coded/bit_buffer_test.m:
tests/hard_coded/bitmap_test.m:
tests/hard_coded/construct_bug.m:
tests/hard_coded/dir_fold.m:
tests/hard_coded/dir_test.m:
tests/hard_coded/read_binary_int16.m:
tests/hard_coded/read_binary_int32.m:
tests/hard_coded/read_binary_int64.m:
tests/hard_coded/read_binary_uint16.m:
tests/hard_coded/read_binary_uint32.m:
tests/hard_coded/read_binary_uint64.m:
tests/hard_coded/read_bitmap_size.m:
tests/hard_coded/remove_file.m:
tests/hard_coded/write_binary.m:
tests/hard_coded/write_binary_int8.m:
tests/hard_coded/write_binary_multibyte_int.m:
tests/hard_coded/write_binary_uint8.m:
    Call the file operation predicates directly in io.file.m, not indirectly
    through io.m.
2022-03-08 06:01:21 +11:00
Zoltan Somogyi
1200014358 Move code to print stats from io to benchmarking.
library/benchmarking.m:
    Move the code of the predicates that print execution statistics
    here from io.m.

    Now that their pure replacements are here, mark the impure predicates
    report_stats and report_full_memory_stats as obsolete.

library/io.m:
    Replace the bodies of the predicates moved to benchmarking.m with
    forwarding calls to benchmarking.m, and mark the predicates themselves
    obsolete in favor of their moved versions.

NEWS:
    Mention the changes above.

compiler/file_util.m:
compiler/mercury_compile_main.m:
    Conform to the changes above.
2022-03-07 15:49:23 +11:00
Zoltan Somogyi
775dad0599 Move the maybe_succeeded type to maybe_succeeded.m.
compiler/maybe_succeeded.m:
    The new module.

compiler/libs.m:
    Include the new module in the libs package.

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

compiler/check_libgrades.m:
compiler/compile_target_code.m:
compiler/file_util.m:
compiler/llds_out_file.m:
compiler/make.build.m:
compiler/make.dependencies.m:
compiler/make.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_mlds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_cs_file.m:
compiler/mlds_to_java_file.m:
compiler/module_cmds.m:
compiler/process_util.m:
    Conform to the move.
2021-08-13 03:08:19 +10:00
Zoltan Somogyi
cb340cf714 Replace bools with maybe_succeeded ...
... and other bespoke types.

compiler/process_util.m:
    Define the maybe_succeeded type, and two operations on it.

compiler/make.m:
    Define the maybe_keep_going and maybe_invoked_by_mmc_make types.

    Use the new types instead of bools where applicable.

compiler/check_libgrades.m:
compiler/compile_target_code.m:
compiler/file_util.m:
compiler/llds_out_file.m:
compiler/make.build.m:
compiler/make.dependencies.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_mlds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_cs_file.m:
compiler/mlds_to_java_file.m:
compiler/module_cmds.m:
    Use the new types instead of bools where applicable.

    Do not look up the value of the keep_going option in the globals
    when its value (converted to the maybe_keep_going type) is available
    in the make_info.

    Use standard terminology in variable names.
2021-08-12 06:06:39 +10:00
Zoltan Somogyi
1557cec62d Use explicit streams in two more modules.
compiler/higher_order.m:
compiler/hlds_goal.m:
    As above.

compiler/Mercury.options:
    Don't specify --no-warn-implicit-stream-calls for the above modules.

compiler/file_util.m:
    Add new forms of some utility predicates to make using explicit streams
    more convenient.
2021-06-11 15:25:27 +10:00
Zoltan Somogyi
4455f0450e Specify output streams in some places.
Besides this main purpose, this diff also replaces code that calls
io.write_string several times in a row with code that prints the
thing to be printed in one go with io.format. In a couple of places,
this has caught (and fixed) bugs where we wanted to put `' quotes
around a filename, but printed only one of the two quotes.

compiler/file_util.m:
    Provide alternatives to the existing maybe_report_stats,
    maybe_write_string and maybe_flush_output predicates that explicitly
    specify the output stream.

    Rename report_error_to_stream as report_error, to allow
    --warn-implicit-stream-calls to report calls to the existing report_error
    predicate, which does not take an explicit output stream.

    Add a module_name argument to the output_to_file_stream predicate,
    to allow its code to figure out where to print both progress and
    error messages.

compiler/module_cmds.m:
    Add a module_name argument to the predicates that update interface,
    to allow their code to figure out where to print both progress and
    error messages.

    For now, leave the predicates that issue commands that are not
    clearly linked to a single module using implicit streams.

compiler/pd_debug.m:
compiler/analysis.file.m:
    Specify output streams in some places.

    In other places, doing so would require redoing the whole debug
    infrastructure, since the current one is based on higher order predicates
    that always write to the non-explicitly-specified *current* output stream.

compiler/passes_aux.m:
    Provide predicates that get progress, debug and error streams
    given a module_info, by extracting the globals and the module name
    from the module_info, and then calling the predicates in globals.m
    to get those streams. Doing this sequence of actions here factors out
    what would otherwise be repeated code in many other parts of the compiler.

    Delete two predicates that were not used anywhere in the compiler.

compiler/deforest.m:
compiler/export.m:
compiler/intermod.m:
compiler/llds_out_file.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_cs_file.m:
compiler/mlds_to_java_file.m:
compiler/recompilation.usage.m:
compiler/simplify_goal_conj.m:
compiler/type_assign.m:
compiler/typecheck.m:
compiler/write_deps_file.m:
compiler/write_module_interface_files.m:
    Use explicit streams everywhere where --warn-implicit-stream-calls
    says this is possible.

compiler/Mercury.options:
    Specify --warn-implicit-stream-calls for the modules above
    with the listed exceptions, and with the exception of the modules
    for which it was already specified.

compiler/compile_target_code.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_middle_passes.m:
    Conform to the changes above.
2021-03-21 23:07:59 +11:00
Zoltan Somogyi
9cf3f1097b Allow reporting of stats to a stream for C.
Until now, all the predicates that reported statistics wrote their output
to stderr; users could not choose another stream. This diff adds ways
to report those same statistics to a stream chosen by the user, though
for now, only when targeting C.

The code for reporting statistics used to be in benchmarking.m, in the
form of a foreign_decl/foreign_code pair that declared and defined the
needed data structures and functions, and foreign_procs that called them.
All this code had stderr hard-coded as the destination stream.

As long as the output is done by C code (as opposed to the C code
constructing a big string containing all the stuff to be output),
directing the output to a user-supplied io.text_output_stream requires
converting that text_output_stream to a value of the C "FILE *" type.
This requires access to the actual definition of the io.text_output_stream
type, which is private to io.m. We could export it to benchmarking.m,
but that is undesirable. Instead, this diff moves the code that used
to be in benchmarking.m's foreign_decl/foreign_code pair to a new
module in the runtime, mercury_report_stats.[ch], which allows
foreign_procs in both benchmarking.m and io.m to use it.

We used to have

    impure report_stats/0       in benchmarking.m
    report_stats/2              in io.m
    report_stats/3              in io.m

The difference between the two versions in io.m is a "selector" argument
that specifies which kind of stats to print. Adding a text_output_stream
argument to report_stats/2 would create report_stats/3, which clashes
with this existing name/arity pair. This diff therefore renames
io.report_stats/2 as io.report_standard_stats.

library/benchmarking.m:
    Delete the code moved to mercury_report_stats.[ch]. Call
    ML_report_full_memory_stats even if MR_MPROF_PROFILE_MEMORY is not defined
    (see next entry).

runtime/mercury_report_stats.[ch]:
    New module containing the code moved from benchmarking.m, with
    ML_report_stats renamed to ML_report_standard_stats, and then
    all ML_ prefixes replaced with MR_ prefixes.

    There is one substantive change. In benchmarking.m, the
    ML_report_full_memory_stats function was always declared, but it was
    defined, and used, only inside "#ifdef MR_MPROF_PROFILE_MEMORY".
    Change this so that it is always defined and used, but prints
    a message about memory profiling not being available if
    MR_MPROF_PROFILE_MEMORY is not defined. This is preferable to the empty
    output that calls to report_full_memory_stats used to generate in such
    situations.

runtime/Mmakefile:
    Add the new header and source files to the lists of header and source
    files respectively.

library/io.m:
    Make the changes described above.

    Provide specific predicates for the full_memory and tabling stats,
    as well as for the standard stats. For now, these are implemented
    only for C.

    Fix inconsistent indentation.

NEWS:
    Announce the rename of io.report_stats/2 to io.report_standard_stats/2,
    but do not announce the other new predicates in io.m yet. That should
    happen when they work for C# and Java as well.

compiler/file_util.m:
    Conform to the change from report_stats to report_standard_stats.

 	runtime/.r1
2021-03-21 17:30:53 +11:00
Zoltan Somogyi
24078e6e75 Write to explicitly named streams in the MLDS backend.
compiler/mlds_to_c_class.m:
compiler/mlds_to_c_data.m:
compiler/mlds_to_c_export.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_func.m:
compiler/mlds_to_c_global.m:
compiler/mlds_to_c_name.m:
compiler/mlds_to_c_stmt.m:
compiler/mlds_to_c_type.m:
compiler/mlds_to_c_util.m:
compiler/mlds_to_cs_class.m:
compiler/mlds_to_cs_data.m:
compiler/mlds_to_cs_export.m:
compiler/mlds_to_cs_file.m:
compiler/mlds_to_cs_func.m:
compiler/mlds_to_cs_global.m:
compiler/mlds_to_cs_name.m:
compiler/mlds_to_cs_stmt.m:
compiler/mlds_to_cs_type.m:
compiler/mlds_to_cs_util.m:
compiler/mlds_to_java_class.m:
compiler/mlds_to_java_data.m:
compiler/mlds_to_java_export.m:
compiler/mlds_to_java_file.m:
compiler/mlds_to_java_func.m:
compiler/mlds_to_java_global.m:
compiler/mlds_to_java_name.m:
compiler/mlds_to_java_stmt.m:
compiler/mlds_to_java_type.m:
compiler/mlds_to_java_util.m:
compiler/mlds_to_target_util.m:
    As above.

compiler/Mercury.options:
    Specify --warn-impliciy-stream-calls for all the modules above.

compiler/file_util.m:
    Delete a utility predicate that expects its clients to operate on
    implicit streams, since the changes above replaced the last calls to it
    with the variant that expects its client to operate on explicit streams.
2020-11-17 11:20:43 +11:00
Zoltan Somogyi
5f50259d16 Write to explicitly named streams in many modules.
Right now, most parts of the compiler write to the "current output stream".
This was a pragmatic choice at the time, but has not aged well. The problem
is that the answer to the question "where is the current output stream going?"
is not obvious in *all* places in the compiler (although it is obvious in
most). When using such implicit streams, finding where the output is going
to in a given predicate requires inspecting not just the ancestors of that
predicate, but also all their older siblings (since any of them could have
changed the current stream), *including* their entire call trees. This is
usually an infeasible task. By constrast, if we explicitly pass streams
to all output operations, we need only follow the places where the variable
representing that stream is bound, which the mode system makes easy.

This diff switches large parts of the compiler over to doing output only
to explicitly passed streams, never to the implicit "current output stream".
The parts it switches over are the parts that rely to a significant degree
on the innermost change, which is to the "output" typeclass in
parse_tree_out_info.m. This is the part that has to be switched over to
explicit streams first, because (a) many modules such as mercury_to_mercury.m
rely on the output typeclass, and (b) most other modules that do output
call predicates in these modules. Starting anywhere else would be like
building a skyscraper starting at the top.

This typeclass, output(U), has two instances: output(io), and output(string),
so you could output either to the current output stream, or to a string.
To allow the specification of the destination stream in the first case,
this diff changes the typeclass to output(S, U) with a functional dependency
from U to S, with the two instances being output(io.text_output_stream, io)
and output(unit, string). (The unit arg is ignored in the second case.)

There is a complication with the output typeclass method, add_list, that
outputs a list of items. The complication is that each item is output
by a predicate supplied by the caller, but the separator between the items
(usually a comma) is output by add_list itself. We don't want to give
callers of this method the opportunity to screw up by specifying (possibly
implicitly) two different output streams for these two purposes, so we want
(a) the caller to tell add_list where to put the separators, and then
(b) for add_list, not its caller, tell the user-supplied predicate what
stream to write to. This works only if the stream argument is just before
the di,uo pair of I/O state arguments, which differs from our usual practice
of passing the stream at or near the left edge of the argument list,
not near the right. The result of this complication is that two categories
of predicates that are and are not used to print items in a list differ
in where they put the stream in their argument lists. This makes it easy
to pass the stream in the wrong argument position if you call a predicate
without looking up its signature, and may require *changing* the argument
order when a predicate is used to print an item in a list for the first time.
A complete switch over to always passing the stream just before !IO
would fix this inconsistency, but is far to big a change to make all at once.

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

    Add write_out_list, which is a variant of io.write_list specifically
    designed to address the "complication" described above. It also has
    the arguments in an order that is better suited for higher-order use.

    Make the same change to argument order in the class method add_list
    as well.

Almost all of the following changes consist of passing an extra stream
argument to output predicates. In some places, where I thought this would
aid readability, I replaced sequences of calls to output predicates
with a single io.format.

compiler/prog_out.m:
    This module had many predicates that wrote things to the current output
    stream. This diff adds versions of these predicates that take an
    explicit stream argument.

    If the originals are still needed after the changes to the other modules,
    keep them, but add "_to_cur_stream" to the end of their names.
    Otherwise, delete them. (Many of the changes below replace
    write_xyz(..., !IO) with io.write_string(Stream, xyz_to_string(...), !IO),
    especially when write_xyz did nothing except call xyz_to_string
    and wrote out the result.)

compiler/c_util.m:
    Add either an explicit stream argument to the argument list, or a
    "_current_stream" suffix to the name, of every predicate defined
    in this module that does output.

    Add a new predicate to print out the block comment containing
    input for mkinit. This factors out common code in the LLDS and MLDS
    backends.

compiler/name_mangle.m:
    Delete all predicates that used to write to the current output stream,
    after replacing them if necessary with functions that return a string,
    which the caller can print to wherever it wants. (The "if necessary"
    part is there because some of the "replacement" functions already
    existed.)

    When converting a proc_label to a string, *always* require the caller
    to say whether the label prefix should be added to the string,
    instead of silently assuming "yes, add it", as calls to one of the old,
    now deleted predicates had it.

compiler/file_util.m:
    Add output_to_file_stream, a version of output_to_file which
    simply passes the output file stream it opens to the predicate
    that is intended to define the contents of the newly created or
    updated file. The existing output_to_file, which instead sets
    and resets the current output stream around the equivalent
    predicate call, is still needed e.g. by the MLDS backend,
    but hopefully for not too long.

compiler/mercury_to_mercury.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_clause.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_pragma.m:
compiler/parse_tree_out_pred_decl.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_out_type_repn.m:
    Change the code writing out parse trees to explicitly pass a stream
    to every predicate that does output.

    In some places, this allows us to avoid changing the identity
    of the current output stream.

compiler/hlds_out.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
    Change the code writing out HLDS code to explicitly pass a stream
    to every predicate that does output. (The changes to these modules
    belong in this diff because these modules call many of the output
    predicates in the parse tree package.)

    In hlds_out_util.m, delete some write_to_xyz(...) predicates that wrote
    the result of xyz_to_string(...) to the current output stream.
    Replace calls to the deleted predicates with calls to io.write_string
    with the string being written being computed by xyz_to_string.

    Add a predicate to hlds_out_util.m that outputs a comment containing
    the current context, if it is valid. This factors out code that used
    to be common to several of the other modules.

    In a few places in hlds_out_module.m, the new code generates a
    slighly different set of blank lines, but this should not be a problem.

compiler/layout_out.m:
compiler/llds_out_code_addr.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_global.m:
compiler/llds_out_instr.m:
compiler/llds_out_util.m:
compiler/opt_debug.m:
compiler/rtti_out.m:
    Change the code writing out the LLDS to explicitly pass a stream
    to every predicate that does output. (The changes to these modules
    belong in this diff because layout_out.m and rtti_out.m call
    many of the output predicates in the parse tree package,
    and through them, the rest of the LLDS backend is affected as well.)

compiler/make.module_dep_file.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
    Replace code that sets and resets the current output stream
    with code that simply passes an explicit output stream to a
    predicate that now *takes* an explicit stream as an argument.

compiler/accumulator.m:
compiler/add_clause.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/cse_detection.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/error_msg_inst.m:
compiler/export.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/ite_gen.m:
compiler/lco.m:
compiler/liveness.m:
compiler/lp_rational.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_global.m:
compiler/mode_debug.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/optimize.m:
compiler/passes_aux.m:
compiler/pd_debug.m:
compiler/pragma_c_gen.m:
compiler/proc_gen.m:
compiler/prog_ctgc.m:
compiler/push_goals_together.m:
compiler/rat.m:
compiler/recompilation.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
compiler/rtti.m:
compiler/saved_vars.m:
compiler/simplify_goal_conj.m:
compiler/stack_opt.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/superhomogeneous.m:
compiler/term_constr_build.m:
compiler/term_constr_data.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_pass2.m:
compiler/term_constr_util.m:
compiler/tupling.m:
compiler/type_assign.m:
compiler/unneeded_code.m:
compiler/write_deps_file.m:
    Conform to the changes above, mostly by passing streams explicitly.

compiler/hlds_dependency_graph.m:
    Conform to the changes above, mostly by passing streams explicitly.
    Move a predicate's definition next it only use.

compiler/Mercury.options:
    Specify --warn-implicit-stream-calls for all the modules in which
    this diff has replaced all implicit streams with explicit streams.
    (Unfortunately, debugging this diff has shown that --warn-implicit-
    stream-calls detects only *some*, and not *all*, uses of implicit
    streams.)

library/term_io.m:
    Fix documentation.
2020-11-14 15:07:55 +11:00
Zoltan Somogyi
ac50b3cbd1 Do not use exceptions in options_file.m.
And add tests for how the compiler handles both valid and invalid
options files.

compiler/options_file.m:
    This diff rewrites options_file.m in a straightforward, direct style that
    returns indications of errors as error_specs rather than as exceptions.
    A recent diff started on this task; this diff finishes it.

    The new approach has several advantages.

    - The control flow is much simpler, and therefore more understandable.
      Correctness arguments for propositions such as "this code closes
      all the file streams that it opens" are now much simpler to make.

    - We now report errors using error_specs, which contain context
      information, while previously, each error was described only
      by a string, without context info.

    - Once we detect and report one error, we can continue to read the
      rest of the input. This allows a single compiler invocation to find
      and report several errors, not just the first.

    - Since we now return the gathered set of error_specs instead of printing
      them, the predicates of this file don't have to take globals structures
      as arguments, which allows our callers to avoid constructing those
      structures.

    - Deep profiling, which cannot handle exceptions, now works on
      the code of this module.

    Change over to using trace goals for debugging prints, since continuing
    to use debug_make_msg would require a globals structure.

    Add an XXX on a likely bug.

    Add a mechanism for writing out a database of variable names and values.

compiler/mercury_compile_main.m:
    Conform to the changes in options_file.m. Document where exactly
    we could avoid constructing a globals just for options_file.m.

    If the right option is given, get options_file to write out the database
    of variable names and values it has just read in, to enable the
    functionality of this module to be tested.

compiler/options.m:
doc/user_guide.texi:
    Add a new developer option, --dump-options-file, to control the above.

compiler/make.build.m:
compiler/make.m:
compiler/make.program_target.m:
    Conform to the changes in options_file.m.

compiler/file_util.m:
    Fix an error message.

tests/Mmakefile:
tools/bootcheck:
    List options_file and invalid_options_file as two new test directories.

    Fix a command in bootcheck.

tests/options_file/Mmakefile:
    Add a mechanism for testing whether options_file.m builds mapping
    from make variable names to values that we expect.

tests/Mmake.common:
    Provide a mechanism for comparing dumped options_files against
    their expected contents, for use by tests/invalid_options_file/Mmakefile.

    Fix a comment.

tests/options_file/basic_test.m:
tests/options_file/basic_test.optfile_exp:
tests/options_file/basic_test.options_file:
tests/options_file/basic_test.options_file.sub0:
tests/options_file/basic_test.options_file.sub1:
    A simple test case for exercising all the usual options_file constructs.

tests/invalid_options_file/Mmakefile:
    Add a mechanism for testing whether options_file.m generates
    the error messages we expect for various kinds of errors in options files.

tests/invalid_options_file/no_assign.{m,options_file,err_exp}:
tests/invalid_options_file/no_var.{m,options_file,err_exp}:
tests/invalid_options_file/nonexistent_file.{m,options_file,err_exp}:
tests/invalid_options_file/undefined_var.{m,options_file,err_exp}:
tests/invalid_options_file/unterminated_string.{m,options_file,err_exp}:
tests/invalid_options_file/unterminated_var.{m,options_file,err_exp}:
    Six test cases to test six different kinds of errors that can be
    detected by options_file.m.
2020-06-12 04:14:00 +10:00
Peter Wang
a5da615a9d Don't use exceptions internally for foreign include file errors.
Use normal return values to signal errors internally if a file
referenced by an `include_file' in a foreign_decl or foreign_code
declaration is missing or there is some error reading from it,
instead of throwing and catching an exception. This allows a compiler
built in a deep profiling grade to continue without aborting.

compiler/file_util.m
    Make the closure argument to output_to_file return a list of error
    strings, and make output_to_file return a boolean indicating if
    there were any errors.

    Delete output_to_file_return_result as it was only used to implement
    output_to_file.

    Make write_include_file_contents and
    write_include_file_contents_cur_stream return an error instead of
    throwing an exception if the include file could not be opened or
    copied to the output stream.

    Delete the include_file_error type. Don't throw or catch exceptions
    of that type.

compiler/compiler_util.m:
    Add helper predicate maybe_is_error.

compiler/elds_to_erlang.m
compiler/llds_out_file.m
compiler/mlds_to_c_file.m
compiler/mlds_to_cs_file.m
compiler/mlds_to_java_file.m
    Return a list of errors when writing out target code or header
    files. Currently the only errors will be due to missing or
    unreadable foreign include files.

compiler/export.m
    Report errors due to missing or unreachable foreign include files.

    Delete the temporary .mh.tmp file if there was an error while
    generating it.
2018-08-18 10:38:54 +10:00
Zoltan Somogyi
bc336763d7 Add a XXX. 2016-10-16 00:08:57 +11:00
Peter Wang
00a4cd0474 Reduce use of foreign-exported procs in io.m.
library/io.m:
    Use foreign-exported enum for `file_type', replacing many
    foreign-exported procs.

    Move construction/deconstruction of Mercury structures out of
    `file_modification_time_2', `file_type_2', `check_file_accessibility_2',
    `check_directory_accessibility_dotnet', etc., removing the need for many
    foreign-exported procs.

    Delete `file_type_implemented'. `file_type_2' will return an error
    code if necessary.

compiler/file_util.m:
    Conform to error message change in io.file_type.
2016-10-06 11:45:30 +11:00
Zoltan Somogyi
31ad78c1a9 Make c_util.m and file_util.m use explicit streams.
compiler/c_util.m:
compiler/file_util.m:
    Replace every predicate that implicitly wrote to the current output stream
    with two predicates: a predicate with the old name that takes an explicit
    output stream parameter, and a predicate that still writes to the current
    output stream, but whose name makes this fact clear.

    Make some other minor improvements.

compiler/bytecode.m:
compiler/elds_to_erlang.m:
compiler/export.m:
compiler/fact_table.m:
compiler/layout_out.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_global.m:
compiler/llds_out_instr.m:
compiler/llds_out_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_cs.m:
compiler/mlds_to_java.m:
compiler/rtti_out.m:
    Conform to the changes above.
2016-09-06 20:01:14 +10:00
Zoltan Somogyi
66356bae7b Make the predicates that find files handle streams explicitly.
compiler/file_util.m:
    The predicates in this module that find the file containing a Mercury
    module used to allow the caller to specify that if the file can be
    successfully opened, then the current input stream should be set to
    the resulting stream. This meant that callers had to save the original
    current input stream *before* calling this predicate, and later restore it,
    *without* anything in between that would signal to readers of the code
    that the current input stream had ever been changed.

    Replace each of these predicates with two predicates. One returns
    the stream as an explicit part of an output argument, letting the caller
    do with the stream what it wished (in some cases, that would mean
    using it in I/O operations as *explicitly* passed parameters, which
    would not require touching the identity of the current input stream),
    giving it the responsibility to close the stream when it is done using it.
    The other would close the stream immediately, not letting the caller know
    that the file was ever opened. The two versions are distinguised both
    by name and by return type.

compiler/find_module.m:
    Make the same change to search_for_module_source. (This predicate was
    the motivation for this change, because unlike the predicates in
    file_util.m, its signature did NOT provide readers with any sort of clue
    that the searched-for file would actually be opened, and that the
    resulting stream would become the new current input stream.)

    Clarify the code that tries to find the source file for a module
    by dropping qualifiers from the module name by (a) giving the predicate
    a more explicit name, and (b) separating the search from the code
    that handles the failure of the search.

compiler/parse_module.m:
compiler/read_modules.m:
    Change part of the interface between these two modules. This used to be
    that code in read_modules.m passed closures to parse_module.m for it
    to invoke, closures whose predicates were always one of the predicates
    affected by the changes above. Simplify the interface by making
    read_modules.m invoke those predicates directly, and simply pass
    the results.

compiler/compile_target_code.m:
compiler/make.module_dep_file.m:
compiler/make.program_target.m:
compiler/mercury_compile_front_end.m:
compiler/mmc_analysis.m:
compiler/module_cmds.m:
compiler/options_file.m:
compiler/write_deps_file.m:
    Conform to the changes above.
2016-09-06 17:10:14 +10:00
Paul Bone
d4d9ad8412 Refactor code that works with temporary files.
It is common to create a temporary file then open it for writing; or create
the temporary file, call another program, then open the file for reading.

This patch implements both these patterns as predicates in file_util.m

compiler/file_util.m:
    Add new utility predicates.

compiler/compile_target_code.m:
compiler/fact_table.m:
compiler/make.util.m:
compiler/prog_event.m:
    Make use of open_temp_input.
2016-04-27 17:20:23 +10:00
Julien Fischer
6143c01e03 Delete an obsolete XXX comment.
compiler/file_util.m:
    Delete the XXX comment about cp's -R option: the issue was fixed
    some time ago.

    Fix an incorrect function name in a comment.
2015-12-16 10:03:46 +11:00
Zoltan Somogyi
d08f1fbe6c Move the code that computes the grade to a new module.
compiler/compute_grade.m:
    New module containing all the code previously in handle_options.m
    that concern grades.

compiler/libs.m:
compiler/notes/compiler_design.html:
    Include and document the new module.

compiler/compiler_util.m:
    Since both handle_options.m and compute_grade.m now need to be able
    to record errors, move the predicates that handle_options.m used to use
    for this purpose to this module, which already contains related predicates.
    Generalize the moved predicates, so they can be used from phases other than
    option processing.

compiler/handle_options.m
    Remove the moved code.

compiler/exception_analysis.m:
compiler/file_names.m:
compiler/file_util.m:
compiler/implementation_defined_literals.m:
compiler/inst_user.m:
compiler/make.program_target.m:
compiler/mercury_compile.m:
compiler/mercury_compile_front_end.m:
compiler/module_cmds.m:
compiler/options.m:
compiler/tabling_analysis.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_initial.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trailing_analysis.m:
    Conform to the above, and/or delete unneeded imports.
2015-10-17 23:09:22 +11:00
Zoltan Somogyi
ecf0a6cb5e Add an XXX about a possible improvement. 2015-08-13 10:43:27 +10:00
Paul Bone
76b4d47f47 Merge branch 'version-14_01-branch'
Merge:
    Handle exit codes of forked processes on windows safely.
2015-02-20 13:33:00 +11:00
Paul Bone
e3559f3e9b Handle exit codes of forked processes on windows safely.
MSVC doesn't provide the WIFEXITED and other macros that help a programmer
determine if a child process exited with an error message or was killed by a
signal.  In these cases Mercury assumes that positive numbers are exit codes
and negative numbers mean that a process was killed by a signal.

When "cl" exited with errors, returning the error code -2, mmc --make
thought that it was killed by a signal and failed to display the compilation
errors.

This patch removes the existing code that tries to detect signals when these
macros are not available.  As far as I know platforms like windows don't
have a concept of SysV signals, therefore signals can only occur when using
cygwin or some other compatibility layer which will then provide the
WIFEXITED and other macros.

I considered using the windows API directly however it didn't (as far as I
could find) provide any concept of signals, so I rejected this idea.

library/io.m:
    As above.

    Also avoid the use of magic numbers such as 127 for failure.

    Rename the handle_system_command_exit_status/1 function to
    decode_system_command_exit_code,

compiler/process_util.m:
    Conform to changes in io.m

compiler/module_cmds.m:
    If a subprocess is killed by a signal then report this both to the
    desired stream (the error stream for the subtask) and to standard
    output.

    After reporting the error then re-raise the signal, not before.

compiler/file_util.m:
    Flush output after reporting an error to make sure that it is seen.
2015-02-20 11:48:03 +11:00
Zoltan Somogyi
13b6f03f46 Module qualify end_module declarations.
compiler/*.m:
    Module qualify the end_module declarations. In some cases, add them.

compiler/table_gen.m:
    Remove an unused predicate, and inline another in the only place
    where it is used.

compiler/add_pragma.m:
    Give some predicates more meaningful names.
2014-09-04 00:24:52 +02:00
Peter Wang
14cc6c83e8 Allow external files to be included in foreign_decl and foreign_code.
Allow external files to be included in pragma foreign_decl and pragma
foreign_code declarations:

    :- pragma foreign_decl("Lang", include_file("Path")).
    :- pragma foreign_decl("Lang", local, include_file("Path")).
    :- pragma foreign_code("Lang", include_file("Path")).

where Path may be an absolute path to a file or a path relative to the
directory that contains the source file of the module containing the
declaration.

mmc --make takes include_file into account when computing dependencies.
Mmake is unchanged yet.

compiler/prog_data.m:
	Add types foreign_literal_or_include and foreign_include_file_info.

	Rename "foreign_code" where "foreign_proc" is meant.

compiler/prog_foreign.m:
	Use foreign_literal_or_include where we now want to allow include_file
	directives.

compiler/prog_io_pragma.m:
	Parse include_file forms of pragma foreign_decl and pragma
	foreign_code declarations.

	Rename "foreign_code" where "foreign_proc" is meant.

compiler/prog_io_typeclass.m:
	Use foreign_literal_or_include where we want to allow include_file.

	Make get_item_list_foreign_code return a list of foreign include
	files.

	Rename "foreign_code" where "foreign_proc" is meant.

compiler/file_names.m:
	Add a predicate to get a module's source file name.

	Add make_include_file_path.

	Reorder a predicate within the module.

compiler/file_util.m:
	Add write_include_file_contents, a common predicate for copying
	the contents of an included file into the output.

	Make output_to_file catch and report exceptions from
	write_include_file_contents, and return a success code.

compiler/export.m:
compiler/llds_out_file.m:
compiler/llds_out_util.m:
compiler/mercury_compile_llds_back_end.m:
	Handle include_file directives when generating target code.

	Make output_llds use output_to_file, and propagate its success code.

	Conform to changes.

compiler/mercury_compile_mlds_back_end.m:
compiler/mlds_to_c.m:
compiler/mlds_to_cs.m:
compiler/mlds_to_ilasm.m:
compiler/mlds_to_java.m:
compiler/mlds_to_managed.m:
	Handle include_file directives when generating target code.

	Propagate success codes from output_to_file.

compiler/elds_to_erlang.m:
compiler/mercury_compile_erl_back_end.m:
	Handle include_file directives when generating target code.

	Propagate success code from output_to_file.

	Fix a tiny bug: the "do not edit" comment names the .erl file
	as the source file.

compiler/mercury_compile.m:
	Take account of the success codes now returned by target code
	generators, stopping on failure.

compiler/mercury_to_mercury.m:
	Print out include_file directives.

compiler/module_imports.m:
	Record in module_imports the list of included files.

compiler/make.dependencies.m:
	Add files referenced by include_file directives as dependencies of
	the module's compiled-code target.

compiler/make.module_dep_file.m:
	Introduce .module_dep file format version 2, which has an additional
	field: the list of included files that the module depends on.

	Refactor the .module_dep file parsing code.

compiler/add_pragma.m:
compiler/add_solver.m:
compiler/coverage_profiling.m:
compiler/det_analysis.m:
compiler/erl_call_gen.m:
compiler/erl_code_gen.m:
compiler/foreign.m:
compiler/goal_util.m:
compiler/hlds_goal.m:
compiler/hlds_module.m:
compiler/hlds_out_goal.m:
compiler/intermod.m:
compiler/make_hlds_passes.m:
compiler/make_hlds_warn.m:
compiler/ml_code_gen.m:
compiler/modecheck_goal.m:
compiler/modules.m:
compiler/polymorphism.m:
compiler/pragma_c_gen.m:
compiler/prog_item.m:
compiler/typecheck.m:
compiler/unique_modes.m:
compiler/write_deps_file.m:
	Conform to changes.

	Rename "foreign_code" where "foreign_proc" is meant.

doc/reference_manual.texi:
	Document include_file as a lanaguage extension.

tests/invalid/Mmakefile:
tests/invalid/foreign_include_file_missing.err_exp:
tests/invalid/foreign_include_file_missing.m:
tests/mmc_make/Mmakefile:
tests/mmc_make/inc/code.c:
tests/mmc_make/inc/code.cs:
tests/mmc_make/inc/code.erl:
tests/mmc_make/inc/code.java:
tests/mmc_make/inc/decl.cs:
tests/mmc_make/inc/decl.erl:
tests/mmc_make/inc/decl.h:
tests/mmc_make/inc/decl.java:
tests/mmc_make/include_file.exp:
tests/mmc_make/include_file.m:
	Add test cases.

NEWS:
	Announce the change.
2014-03-31 11:27:57 +11:00
Julien Fischer
dd1ff6ac5e Automatically decide the value of --install-command (part 1).
Allow the possibility of the compiler automatically deciding what command(s) to
use to install files and directories instead of using the value of
--install-command (which defaults to "cp").  The eventual intention (NYI) is
that in the absence of a value for --install-command, the compiler will choose
an appropriate command based on the setting of --host-env-type (and possibly
some other stuff, for the example the OS version).

The rationale for all this to try to improve the current situation on Windows
(in the absence of Cygwin or MSYS) where file copying is a bit of a mess.  It
should also mean that the same Mercury installation can be more easily used
from both MSYS and cmd.exe.

NOTE: for the moment --install-command is still specified in Mercury.config.
This will be removed in a later change and the compiler will default to
choosing automatically.

compiler/globals.m:
	Add a type whose values describe how we should install files and
	directories.

	Add a field of that type, file_install_cmd, to the globals structure.

	Add access predicates.

compiler/handle_options.m:
	Set the new field in the globals.

compiler/file_util.m:
	Provide functions for constructing a command line for installing files
	and directories based on the value of the file_install_cmd field in
	the globals.

compiler/make.program_target.m:
compiler/module_cmds.m:
	Use the new functions in file_util.m.
2013-07-30 13:17:15 +10: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
Zoltan Somogyi
1c3bc03415 Make the system compiler with --warn-unused-imports.
Estimated hours taken: 2
Branches: main, release

Make the system compiler with --warn-unused-imports.

browser/*.m:
library/*.m:
compiler/*.m:
	Remove unnecesary imports as flagged by --warn-unused-imports.

	In some files, do some minor cleanup along the way.
2010-12-30 11:18:04 +00:00
Julien Fischer
1dbc337297 Fix bug #176: Mercury libraries built with mmake on Mac OS X were
Branches: main, 11.01

Fix bug #176: Mercury libraries built with mmake on Mac OS X were
being built with incorrect install names.

compiler/file_util.m:
	Don't escape special characters in the shared library name
	part of the install name when generating the rule to invoke the
	ml script in a .dep file.
2010-12-20 04:59:53 +00:00
Zoltan Somogyi
4ebe3d0d7e Stop storing globals in the I/O state, and divide mercury_compile.m
Estimated hours taken: 60
Branches: main

Stop storing globals in the I/O state, and divide mercury_compile.m
into smaller, more cohesive modules. (This diff started out as doing
only the latter, but it became clear that this was effectively impossible
without the former, and the former ended up accounting for the bulk of the
changes.)

Taking the globals out of the I/O state required figuring out how globals
data flowed between pieces of code that were often widely separated.
Such flows were invisible when globals could be hidden in the I/O state,
but now they are visible, because the affected code now passes around
globals structures explicitly.

In some cases, the old flow looked buggy, as when one job invoked by
mmc --make could affect the globals value of its parent or the globals value
passed to the next job. I tried to fix such problems when I saw them. I am
not 100% sure I succeeded in every case (I may have replaced old bugs with
new ones), but at least now the flow is out in the open, and any bugs
should be much easier to track down and fix.

In most cases, changes the globals after the initial setup are intended to be
in effect only during the invocation of a few calls. This used to be done
by remembering the initial values of the to-be-changed options, changing their
values in the globals in the I/O state, making the calls, and restoring the old
values of the options. We now simply create a new version of the globals
structure, pass it to the calls to be affected, and then discard it.

In two cases, when discovering reasons why (1) smart recompilation should
not be done or (2) item version numbers should not be generated, the record
of the discovery needs to survive this discarding. This is why in those cases,
we record the discovery by setting a mutable attached to the I/O state.
We use pure code (with I/O states) both to read and to write the mutables,
so this is no worse semantically than storing the information in the globals
structure inside the I/O state. (Also, we were already using such a mutable
for recording whether -E could add more information.)

In many modules, the globals information had to be threaded through
several predicates in the module. In some places, this was made more
difficult by predicates being defined by many clauses. In those cases,
this diff converts those predicates to using explicit disjunctions.

compiler/globals.m:
	Stop storing the globals structure in the I/O state, and remove
	the predicates that accessed it there.

	Move a mutable and its access predicate here from handle_options.m,
	since here is when the mutables treated the same way are.

	In a couple of cases, the value of an option is available in a mutable
	for speed of access from inside performance-critical code. Set the
	values of those mutables from the option when the processing of option
	values is finished, not when it is starting, since otherwise the copies
	of each option could end up inconsistent.

	Validate the reuse strategy option here, since doing it during ctgc
	analysis (a) is too late, and (b) would require an update to the
	globals to be done at an otherwise inconvenient place in the code.
	Put the reuse strategy into the globals structure.

	Two fields in the globals structure were unused. One
	(have_printed_usage) was made redundant when the one predicate
	that used it itself became unused; the other (source_file_map)
	was effectively replaced by a mutable some time ago. Delete
	these fields from the globals.

	Give the fields of the globals structure a distinguishing prefix.

	Put the type declarations, predicate declarations and predicate
	definitions in a consistent order.

compiler/source_file_map.m:
	Record this module's results only in the mutable (it serves as a
	cache), not in globals structure. Use explicitly passed globals
	structure for other purposes.

compiler/handle_options.m:
	Rename handle_options as handle_given_options, since it does not
	process THE options to the program, but the options it is given,
	and even during the processing of a single module, it can be invoked
	up the three times in a row, each time being given different options.
	(It was up to four times in a row before this diff.)

	Make handle_given_options explicitly return the globals structure it
	creates. Since it does not take an old global structure as input
	and globals are not stored in the I/O state, it is now clear that
	the globals structure it returns is affected only by the default values
	of the options and the options it processes. Before this diff,
	in the presence of errors in the options, handle_options *could*
	return (implicitly, in the I/O state) the globals structure that
	happened to be in the I/O state when it was invoked.

	Provide a separate predicate for generating a dummy globals based only
	on the default values of options. This allows by mercury_compile.m
	to stop abusing a more general-purpose predicate from handle_options.m,
	which we no longer export.

	Remove the mutable and access predicate moved to globals.m.

compiler/options.m:
	Document the fact that two options, smart_recompilation and
	generate_item_version_numbers, should not be used without seeing
	whether the functionalities they call for have been disabled.

compiler/mercury_compile_front_end.m:
compiler/mercury_compile_middle_passes.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_mlds_back_end.m:
compiler/mercury_compile_erl_back_end.m:
	New modules carved out of the old mercury_compile.m. They each cover
	exactly the areas suggested by their names.

	Each of the modules is more cohesive than the old mercury_compile.m.
	Their code is also arranged in a more logical order, with predicates
	representing compiler passes being defined in the order of their
	invocation.

	Some of these modules export predicates for use by their siblings,
	showing the dependencies between the groups of passes.

compiler/top_level.m:
compiler/notes/compiler_design.html:
	Add the new modules.

compiler/mark_static_terms.m:
	Move this module from the ml_backend package to the hlds package,
	since (a) it does not depend on the MLDS in any way, and (b) it is
	also needed by a compiler pass (loop invariants) in the middle passes.

compiler/hlds.m:
compiler/ml_backend.m:
compiler/notes/compiler_design.html:
	Reflect mark_static_terms.m's change of package.

compiler/passes_aux.m:
	Move the predicates for dumping out the hLDS here from
	mercury_compile.m, since the new modules also need them.

	Look up globals in the HLDS, not the I/O state.

compiler/hlds_module.m:
	Store the prefix (common part) of HLDS dump file names in the HLDS
	itself, so that the code moved to passes_aux.m can figure out the
	file name for a HLDS dump without doing system calls.

	Give the field names of some structures prefixes to avoid ambiguity.

compiler/mercury_compile.m:
	Remove the code moved to the other modules. This module now looks
	after only option handling (such as deciding whether to generate .int3
	files, .int files, .opt files etc), and the compilation passes
	up to and including the creation of the first version of the HLDS.
	Everything after that is subcontracted to the new modules.

	Simplify and make explicit the flow of globals information.
	When invoking predicates that could disable smart recompilation,
	check whether they have done so, and if yes, update the globals
	accordingly.

	When compiling via gcc, we need to link into the executable
	the object files of any separate C files we generate for C code
	foreign_procs, which we cannot translate into gcc's internal
	structures without becoming a C compiler as well as a Mercury compiler.
	Instead of adding such files to the accumulating option for extra
	object files in the globals structure, we return their names using
	the already existing mechanism we have always used to link the object
	files of fact tables into the executable.

	Give several predicates more descriptive names. Put predicates
	in a more logical order.

compiler/make.m:
compiler/make.dependencies.m:
compiler/make.module_target.m:
compiler/make.module_dep_file.m:
compiler/make.program_target.m:
compiler/make.util.m:
	Require callers to supply globals structures explicitly, not via the
	I/O state. Afterward pass them around explicitly, passing modified
	versions to mercury_compile.m when invoking it with module- and/or
	task-specific options.

	Due the extensive use of partial application for higher order code
	in these modules, passing around the globals structures explicitly
	is quite tricky here. There may be cases where a predicate uses
	an old globals structure it got from a closure instead of the updated
	module- and/or task-specific globals it should be using, or vice versa.
	However, it is just as likely that, this diff fixes old problems
	by preventing the implicit flow of updated-only-for-one-invocation
	globals structures back to the original invoking context.

	Although I have tried to be careful about this, it is also possible
	that in some places, the code is using an updated-for-an-invocation
	globals structure in some but not all of the places where it
	SHOULD be used.

compiler/c_util.m:
compiler/compile_target_code.m:
compiler/compiler_util.m:
compiler/error_util.m:
compiler/file_names.m:
compiler/file_util.m:
compiler/ilasm.m:
compiler/ml_optimize.m:
compiler/mlds_to_managed.m:
compiler/module_cmds.m:
compiler/modules.m:
compiler/options_file.m:
compiler/pd_debug.m:
compiler/prog_io.m:
compiler/transform_llds.m:
compiler/write_deps_file.m:
	Require callers to supply globals structures explicitly, not via the
	I/O state.

	In some cases, the explicit globals structure argument allows
	a predicate to dispense with the I/O states previously passed to it.

	In some modules, rename some predicates, types and/or function symbols
	to avoid ambiguity.

compiler/read_modules.m:
	Require callers to supply globals structures explicitly, not via the
	I/O state.

	Record when smart recompilation and the generation of item version
	numbers should be disabled.

compiler/opt_debug.m:
compiler/process_util.m:
	Require callers to supply the needed options explicitly, not via the
	globals in the I/O state.

compiler/analysis.m:
compiler/analysis.file.m:
compiler/mmc_analysis.m:
	Make the analysis framework's methods take their global structures
	as explicit arguments, not as implicit data stored in the I/O state.

	Stop using `with_type` and `with_inst` declarations unnecessarily.

	Rename some predicates to avoid ambiguity.

compiler/hlds_out.m:
compiler/llds_out.m:
compiler/mercury_to_mercury.m:
compiler/mlds_to_c.m:
compiler/mlds_to_java.m:
compiler/optimize.m:
	Make these modules stop accessing the globals from the I/O state.
	Do this by requiring the callers of their top predicates to explicitly
	supply a globals structure. To compensate for the cost of having to
	pass around a representation of the options, look up the values of the
	options of interest just once, to make further access much faster.

	(In the case of mlds_to_c.m, the code already did much of this,
	but it still had a few accesses to globals in the I/O state that
	this diff eliminates.)

	If the module exports a predicate that needs these pre-looked-up
	options, then export the type of this data structure and its
	initialization function.

compiler/frameopt.m:
	Since this module needs only one option from the globals, pass that
	option instead of the globals.

compiler/accumulator.m:
compiler/add_clause.m:
compiler/closure_analysis.m:
compiler/complexity.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/elds_to_erlang.m:
compiler/exception_analysis.m:
compiler/fact_table.m:
compiler/intermod.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/pd_util.m:
compiler/post_term_analysis.m:
compiler/recompilation.usage.m:
compiler/size_prof.usage.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.direct.m:
compiler/structure_sharing.analysis.m:
compiler/tabling_analysis.m:
compiler/term_constr_errors.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_initial.m:
compiler/term_constr_main.m:
compiler/term_constr_util.m:
compiler/trailing_analysis.m:
compiler/trans_opt.m:
compiler/typecheck_info.m:
	Look up globals information from the HLDS, not the I/O state.

	Conform to the changes above.

compiler/gcc.m:
compiler/maybe_mlds_to_gcc.pp:
compiler/mlds_to_gcc.m:
	Look up globals information from the HLDS, not the I/O state.

	Conform to the changes above.

	Convert these modules to our current programming style.

compiler/termination.m:
	Look up globals information from the HLDS, not the I/O state.

	Conform to the changes above.

	Report some warnings with error_specs, instead of immediately
	printing them out.

compiler/export.m:
compiler/il_peephole.m:
compiler/layout_out.m:
compiler/rtti_out.m:
compiler/liveness.m:
compiler/make_hlds.m:
compiler/make_hlds_passes.m:
compiler/mlds_to_il.m:
compiler/mlds_to_ilasm.m:
compiler/recompilation.check.m:
compiler/stack_opt.m:
compiler/superhomogeneous.m:
compiler/tupling..m:
compiler/unneeded_code.m:
compiler/unused_args.m:
compiler/unused_import.m:
compiler/xml_documentation.m:
	Conform to the changes above.

compiler/equiv_type_hlds.m:
	Give the field names of a structure prefixes to avoid ambiguity.

	Stop using `with_type` and `with_inst` declarations unnecessarily.

compiler/loop_inv.m:
compiler/pd_info.m:
compiler/stack_layout.m:
	Give the field names of some structures prefixes to avoid ambiguity.

compiler/add_pragma.m:
	Add notes.

compiler/string.m:
NEWS:
	Add a det version of remove_suffix, for use by new code above.
2009-10-14 05:28:53 +00:00
Peter Wang
fc95b27671 Add search_for_file_mod_time which returns the last modification time
Branches: main

compiler/file_util.m:
        Add search_for_file_mod_time which returns the last modification time
        of the found file.

        Add parameters to search_for_file and search_for_file_returning_dir
        that chooses whether the found file should be left open as the input
        stream on success.

        Avoid dir.make_path_name where possible as it is slow.

compiler/make.util.m:
        Use search_for_file_mod_time instead of something complicated.

compiler/file_names.m:
        Make file_is_arch_or_grade_dependent not consider further clauses after
        stripping the ".tmp" suffix off a filename.

compiler/compile_target_code.m:
compiler/make.module_dep_file.m:
compiler/make.program_target.m:
compiler/mercury_compile.m:
compiler/mmc_analysis.m:
compiler/module_cmds.m:
compiler/options_file.m:
compiler/prog_io.m:
compiler/read_modules.m:
compiler/write_deps_file.m:
        Conform to changes.
2009-07-31 05:32:08 +00:00
Julien Fischer
b6aa63de3e Fix a bug with mmc --make on Mac OS X.
compiler/file_util.m:
	Quote the directory passed to the C compiler's
	-install-name option in case it contains spaces.

compiler/make.program_target.m:
	Update a comment.
2008-12-11 13:03:07 +00:00
Zoltan Somogyi
a00596c283 The file modules.m contains lots of different kinds of functionality.
Estimated hours taken: 16
Branches: main

The file modules.m contains lots of different kinds of functionality.
While much of it belongs together, much of it does not. This diff moves
most of the functionality that does not belong with the rest to several
new modules:

	libs.file_util
	parse_tree.deps_map
	parse_tree.file_names
	parse_tree.module_cmds
	parse_tree.module_imports
	parse_tree.read_module
	parse_tree.write_deps_file

To make them coherent, move some predicates from hlds.passes_aux,
parse_tree.prog_io and parse_tree.prog_out to the new modules, making them
more accessible, reducing the required access from the hlds package to
parse_tree, or from the parse_tree package to libs.

In the same spirit, this diff also moves some simple predicates and functions
dealing with sym_names from prog_util.m to mdbcomp/prim_data.m. This allows
several modules to avoid depending on parse_tree.prog_util.

Rename some of the moved predicates and function symbols where this avoids
ambiguity. (There were several that differed from other predicates or function
symbols only in arity.)

Replace several uses of bools with purpose-specific types. This makes some
of the code significantly easier to read.

This diff moves modules.m from being by far the largest module, to being
only the seventh largest, from 8900+ lines to just 4200+. It also reduces
the number of modules that import parse_tree.modules considerably; most
modules that imported it now import only one or two of the new modules instead.

Despite the size of the diff, there should be no algorithmic changes.

compiler/modules.m:
compiler/passes_aux.m:
compiler/prog_io.m:
compiler/prog_out.m:
	Delete the moved functionality.

compiler/file_util.m:
	New module in the libs package. Its predicates search for files
	and do simple error or progress reporting.

compiler/file_names.m:
	New module in the parse_tree package. It contains predicates for
	converting module names to file names.

compiler/module_cmds.m:
	New module in the parse_tree package. Its predicates handle the
	commands for manipulating interface files of various kinds.

compiler/module_import.m:
	New module in the parse_tree package. It contains the module_imports
	type and its access predicates, and the predicates that compute
	various sorts of direct dependencies (those caused by imports)
	between modules.

compiler/deps_map.m:
	New module in the parse_tree package. It contains the data structure
	for recording indirect dependencies between modules, and the predicates
	for creating it.

compiler/read_module.m:
	New module in the parse_tree package. Its job is reading in modules,
	both human-written and machine-written (such as interface and
	optimization files).

compiler/write_deps_file.m:
	New module in the parse_tree package. Its job is writing out
	makefile fragments.

compiler/libs.m:
compiler/parse_tree.m:
	Include the new modules.

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

mdbcomp/prim_data.m:
compiler/prog_util.m:
	Move the predicates that operate on nothing but sym_names from
	prog_util to prim_data.

	Move get_ancestors from modules to prim_data.

compiler/prog_item.m:
	Move stuff that looks for foreign code in a list of items here from
	modules.m.

compiler/source_file_map.m:
	Note why this module needs to be in the parse_tree package.

compiler/add_pred.m:
compiler/add_special_pred.m:
compiler/analysis.file.m:
compiler/analysis.m:
compiler/assertion.m:
compiler/check_typeclass.m:
compiler/compile_target_code.m:
compiler/cse_detection.m:
compiler/det_analysis.m:
compiler/elds_to_erlang.m:
compiler/exception_analysis.m:
compiler/export.m:
compiler/fact_table.m:
compiler/higher_order.m:
compiler/hlds_module.m:
compiler/hlds_pred.m:
compiler/intermod.m:
compiler/llds_out.m:
compiler/make.dependencies.m:
compiler/make.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.util.m:
compiler/make_hlds_passes.m:
compiler/maybe_mlds_to_gcc.pp:
compiler/mercury_compile.m:
compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_ilasm.m:
compiler/mlds_to_java.m:
compiler/mmc_analysis.m:
compiler/mode_constraints.m:
compiler/mode_debug.m:
compiler/modes.m:
compiler/module_qual.m:
compiler/optimize.m:
compiler/passes_aux.m:
compiler/proc_gen.m:
compiler/prog_foreign.m:
compiler/prog_io.m:
compiler/prog_io_util.m:
compiler/prog_mutable.m:
compiler/prog_out.m:
compiler/pseudo_type_info.m:
compiler/purity.m:
compiler/recompilation.check.m:
compiler/recompilation.usage.m:
compiler/simplify.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.detect_garbage.m:
compiler/structure_reuse.direct.m:
compiler/structure_sharing.analysis.m:
compiler/tabling_analysis.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/trans_opt.m:
compiler/type_util.m:
compiler/typecheck.m:
compiler/typecheck_info.m:
compiler/unify_proc.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Minor changes to conform to the changes above.
2008-07-21 03:10:29 +00:00