mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-18 10:53:40 +00:00
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.
258 lines
9.8 KiB
Mathematica
258 lines
9.8 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1998-2007, 2009-2011 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: pd_debug_m
|
|
% Main author: stayl.
|
|
%
|
|
% Debugging routines for partial deduction.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module transform_hlds.pd_debug.
|
|
:- interface.
|
|
|
|
:- import_module hlds.
|
|
:- import_module hlds.hlds_goal.
|
|
:- import_module hlds.hlds_pred.
|
|
:- import_module parse_tree.
|
|
:- import_module parse_tree.prog_data.
|
|
:- import_module transform_hlds.pd_info.
|
|
|
|
:- import_module bool.
|
|
:- import_module io.
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred pd_debug_do_io(bool::in, pred(io, io)::pred(di, uo) is det,
|
|
io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_output_goal(pd_info::in, string::in, hlds_goal::in,
|
|
io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_search_version_result(pd_info::in, maybe_version::in,
|
|
io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_register_version(pd_info::in, pred_proc_id::in,
|
|
version_info::in, io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_write_instmap(pd_info::in, io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_message(bool::in, string::in,
|
|
list(string.poly_type)::in, io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_message_context(bool::in, prog_context::in, string::in,
|
|
list(string.poly_type)::in, io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_write(bool::in, T::in, io::di, io::uo) is det.
|
|
|
|
:- pred pd_debug_write_pred_proc_id_list(pd_info::in, list(pred_proc_id)::in,
|
|
io::di, io::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module hlds.goal_util.
|
|
:- import_module hlds.hlds_module.
|
|
:- import_module hlds.hlds_out.
|
|
:- import_module hlds.hlds_out.hlds_out_goal.
|
|
:- import_module hlds.hlds_out.hlds_out_mode.
|
|
:- import_module hlds.hlds_out.hlds_out_util.
|
|
:- import_module hlds.instmap.
|
|
:- import_module libs.
|
|
:- import_module libs.globals.
|
|
:- import_module libs.options.
|
|
:- import_module parse_tree.parse_tree_out_info.
|
|
:- import_module parse_tree.parse_tree_out_term.
|
|
:- import_module parse_tree.prog_out.
|
|
|
|
:- import_module set.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_do_io(DebugPD, Pred, !IO) :-
|
|
(
|
|
DebugPD = yes,
|
|
call(Pred, !IO),
|
|
io.flush_output(!IO)
|
|
;
|
|
DebugPD = no
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_search_version_result(PDInfo, MaybeVersion, !IO) :-
|
|
pd_info_get_module_info(PDInfo, ModuleInfo),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
globals.lookup_bool_option(Globals, debug_pd, DebugPD),
|
|
pd_debug_do_io(DebugPD,
|
|
pd_debug_search_version_result_2(ModuleInfo, MaybeVersion), !IO).
|
|
|
|
:- pred pd_debug_search_version_result_2(module_info::in, maybe_version::in,
|
|
io::di, io::uo) is det.
|
|
|
|
pd_debug_search_version_result_2(ModuleInfo, MaybeVersion, !IO) :-
|
|
(
|
|
MaybeVersion = no_version,
|
|
io.write_string("Specialised version not found.\n", !IO)
|
|
;
|
|
MaybeVersion = version(exact, _, _, _, _),
|
|
io.write_string("Exact match found.\n", !IO)
|
|
;
|
|
MaybeVersion = version(more_general, PredProcId, Version, _, _),
|
|
io.write_string("More general version.\n", !IO),
|
|
pd_debug_output_version(ModuleInfo, PredProcId, Version, no, !IO)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_register_version(PDInfo, PredProcId, Version, !IO) :-
|
|
pd_info_get_module_info(PDInfo, ModuleInfo),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
globals.lookup_bool_option(Globals, debug_pd, DebugPD),
|
|
pd_debug_do_io(DebugPD,
|
|
pd_debug_register_version_2(ModuleInfo, PredProcId, Version), !IO).
|
|
|
|
:- pred pd_debug_register_version_2(module_info::in, pred_proc_id::in,
|
|
version_info::in, io::di, io::uo) is det.
|
|
|
|
pd_debug_register_version_2(ModuleInfo, PredProcId, Version, !IO) :-
|
|
io.write_string("Registering version:\n", !IO),
|
|
pd_debug_output_version(ModuleInfo, PredProcId, Version, no, !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred pd_debug_output_version(module_info::in, pred_proc_id::in,
|
|
version_info::in, bool::in, io::di, io::uo) is det.
|
|
|
|
pd_debug_output_version(ModuleInfo, PredProcId, Version, WriteUnfoldedGoal,
|
|
!IO) :-
|
|
io.output_stream(Stream, !IO),
|
|
Version = version_info(Goal, _, Args, _, InstMap,
|
|
InitialCost, CostDelta, Parents, _),
|
|
Goal = hlds_goal(_GoalExpr, GoalInfo),
|
|
PredName = predicate_name(ModuleInfo, PredId),
|
|
PredProcId = proc(PredId, ProcId),
|
|
pred_id_to_int(PredId, PredInt),
|
|
proc_id_to_int(ProcId, ProcInt),
|
|
|
|
io.format(Stream, "%s: (PredProcId :%d-%d)\n",
|
|
[s(PredName), i(PredInt), i(ProcInt)], !IO),
|
|
io.format(Stream, " initial cost: %d\n", [i(InitialCost)], !IO),
|
|
io.format(Stream, " cost delta: %d\n", [i(CostDelta)], !IO),
|
|
NonLocals = goal_info_get_nonlocals(GoalInfo),
|
|
module_info_pred_proc_info(ModuleInfo, PredId, ProcId, _, ProcInfo),
|
|
proc_info_get_varset(ProcInfo, VarSet),
|
|
instmap_restrict(NonLocals, InstMap, InstMap1),
|
|
io.format(Stream, " args: %s\n",
|
|
[s(mercury_vars_to_string(VarSet, print_name_and_num, Args))], !IO),
|
|
write_instmap(Stream, VarSet, print_name_and_num, 1, InstMap1, !IO),
|
|
io.nl(Stream, !IO),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
OutInfo = init_hlds_out_info(Globals, output_debug),
|
|
write_goal(OutInfo, Stream, ModuleInfo, VarSet, print_name_and_num,
|
|
1, "\n", Goal, !IO),
|
|
io.nl(Stream, !IO),
|
|
set.to_sorted_list(Parents, ParentsList),
|
|
ParentStrs = list.map(pred_proc_id_to_string(ModuleInfo), ParentsList),
|
|
ParentsStr = string.join_list(", ", ParentStrs),
|
|
io.format(Stream, "Parents: %s\n", [s(ParentsStr)], !IO),
|
|
(
|
|
WriteUnfoldedGoal = yes,
|
|
proc_info_get_goal(ProcInfo, ProcGoal),
|
|
io.write_string(Stream, "Unfolded goal\n", !IO),
|
|
write_goal(OutInfo, Stream, ModuleInfo, VarSet, print_name_and_num,
|
|
1, "\n", ProcGoal, !IO),
|
|
io.nl(Stream, !IO)
|
|
;
|
|
WriteUnfoldedGoal = no
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_write_instmap(PDInfo, !IO) :-
|
|
io.output_stream(Stream, !IO),
|
|
pd_info_get_instmap(PDInfo, InstMap),
|
|
pd_info_get_proc_info(PDInfo, ProcInfo),
|
|
proc_info_get_varset(ProcInfo, VarSet),
|
|
pd_info_get_module_info(PDInfo, ModuleInfo),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
globals.lookup_bool_option(Globals, debug_pd, DebugPD),
|
|
pd_debug_do_io(DebugPD,
|
|
write_instmap(Stream, VarSet, print_name_and_num, 1, InstMap), !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_write_pred_proc_id_list(PDInfo, PredProcIds, !IO) :-
|
|
pd_info_get_module_info(PDInfo, ModuleInfo),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
globals.lookup_bool_option(Globals, debug_pd, DebugPD),
|
|
pd_debug_do_io(DebugPD,
|
|
pd_debug_write_pred_proc_id_list_2(ModuleInfo, PredProcIds),
|
|
!IO).
|
|
|
|
:- pred pd_debug_write_pred_proc_id_list_2(module_info::in,
|
|
list(pred_proc_id)::in, io::di, io::uo) is det.
|
|
|
|
pd_debug_write_pred_proc_id_list_2(ModuleInfo, PredProcIds, !IO) :-
|
|
ProcStrs = list.map(pred_proc_id_to_string(ModuleInfo), PredProcIds),
|
|
ProcsStr = string.join_list(", ", ProcStrs),
|
|
io.write_string(ProcsStr, !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_output_goal(PDInfo, Msg, Goal, !IO) :-
|
|
pd_info_get_module_info(PDInfo, ModuleInfo),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
globals.lookup_bool_option(Globals, debug_pd, DebugPD),
|
|
pd_debug_do_io(DebugPD, pd_debug_output_goal_2(PDInfo, Msg, Goal), !IO).
|
|
|
|
:- pred pd_debug_output_goal_2(pd_info::in, string::in, hlds_goal::in,
|
|
io::di, io::uo) is det.
|
|
|
|
pd_debug_output_goal_2(PDInfo, Msg, Goal, !IO) :-
|
|
io.output_stream(Stream, !IO),
|
|
Goal = hlds_goal(GoalExpr, GoalInfo),
|
|
pd_info_get_proc_info(PDInfo, ProcInfo),
|
|
proc_info_get_varset(ProcInfo, VarSet),
|
|
pd_info_get_instmap(PDInfo, InstMap),
|
|
pd_info_get_module_info(PDInfo, ModuleInfo),
|
|
io.write_string(Msg, !IO),
|
|
goal_util.goal_vars(hlds_goal(GoalExpr, GoalInfo), Vars),
|
|
instmap_restrict(Vars, InstMap, InstMap1),
|
|
write_instmap(Stream, VarSet, print_name_and_num, 1, InstMap1, !IO),
|
|
io.nl(Stream, !IO),
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
OutInfo = init_hlds_out_info(Globals, output_debug),
|
|
write_goal(OutInfo, Stream, ModuleInfo, VarSet, print_name_and_num,
|
|
1, "\n", Goal, !IO),
|
|
io.nl(Stream, !IO),
|
|
io.flush_output(Stream, !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_message(DebugPD, Fmt, Args, !IO) :-
|
|
pd_debug_do_io(DebugPD, io.format(Fmt, Args), !IO).
|
|
|
|
pd_debug_message_context(DebugPD, Context, Fmt, Args, !IO) :-
|
|
pd_debug_do_io(DebugPD,
|
|
prog_out.write_context_to_cur_stream(Context), !IO),
|
|
pd_debug_do_io(DebugPD, io.format(Fmt, Args), !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pd_debug_write(DebugPD, Thing, !IO) :-
|
|
pd_debug_do_io(DebugPD, io.write(Thing), !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module transform_hlds.pd_debug.
|
|
%-----------------------------------------------------------------------------%
|