mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-20 11:54:02 +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.
256 lines
9.4 KiB
Mathematica
256 lines
9.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2003-2008 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: name_mangle.m.
|
|
%
|
|
% This module defines routines for generating and/or outputting identifiers
|
|
% for modules, predicates/functions, and procedures in forms that are
|
|
% syntactically acceptable in all our target languages, meaning C, Java
|
|
% and MSIL.
|
|
%
|
|
% NOTE: some parts of the name mangling routines are defined in
|
|
% prog_foreign.m because they are required by the frontend of the compiler,
|
|
% for generating makefile fragments etc.
|
|
%
|
|
% Warning: any changes to the name mangling algorithms implemented in this
|
|
% module may also require changes to profiler/demangle.m, util/mdemangle.c and
|
|
% compiler/prog_foreign.m.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module backend_libs.name_mangle.
|
|
:- interface.
|
|
|
|
:- import_module backend_libs.rtti.
|
|
:- import_module mdbcomp.
|
|
:- import_module mdbcomp.prim_data.
|
|
:- import_module mdbcomp.sym_name.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type maybe_add_label_prefix
|
|
---> do_not_add_label_prefix
|
|
; add_label_prefix.
|
|
|
|
% Get a proc label string (used by procs which are exported to C).
|
|
% The boolean controls whether label_prefix is added to the string.
|
|
%
|
|
:- func proc_label_to_c_string(maybe_add_label_prefix, proc_label) = string.
|
|
|
|
% Succeed iff the given name or sym_name doesn't need mangling.
|
|
%
|
|
:- pred name_doesnt_need_mangling(string::in) is semidet.
|
|
:- pred sym_name_doesnt_need_mangling(sym_name::in) is semidet.
|
|
|
|
% Create a name for base_typeclass_info.
|
|
%
|
|
:- func make_base_typeclass_info_name(tc_name, string) = string.
|
|
|
|
:- func make_base_typeclass_info_name_with_data_prefix(tc_name, string)
|
|
= string.
|
|
|
|
% To ensure that Mercury labels don't clash with C symbols, we
|
|
% prefix them with the string returned by this function.
|
|
%
|
|
:- func mercury_label_prefix = string.
|
|
|
|
% To ensure that the names of global variables generated by the Mercury
|
|
% compiler don't clash with C symbols, we prefix them with the string
|
|
% returned by this function.
|
|
%
|
|
:- func mercury_var_prefix = string.
|
|
|
|
% All the C data structures we generate which are either fully static
|
|
% or static after initialization should have one of these prefixes,
|
|
% to ensure that Mercury global variables don't clash with C symbols.
|
|
%
|
|
:- func mercury_data_prefix = string.
|
|
:- func mercury_scalar_common_array_prefix = string.
|
|
:- func mercury_vector_common_array_prefix = string.
|
|
|
|
% All the C types we generate should have this prefix to ensure
|
|
% that they don't clash with C symbols.
|
|
%
|
|
:- func mercury_common_type_prefix = string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module hlds.
|
|
:- import_module hlds.special_pred.
|
|
:- import_module parse_tree.
|
|
:- import_module parse_tree.prog_data.
|
|
:- import_module parse_tree.prog_foreign.
|
|
|
|
:- import_module int.
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
proc_label_to_c_string(AddPrefix, ProcLabel) = ProcLabelString :-
|
|
(
|
|
ProcLabel = ordinary_proc_label(DefiningModule, PredOrFunc, PredModule,
|
|
PredName, Arity, ModeNum),
|
|
LabelName = make_pred_or_func_name(DefiningModule, PredOrFunc,
|
|
PredModule, PredName, Arity, AddPrefix),
|
|
(
|
|
PredOrFunc = pf_function,
|
|
OrigArity = Arity - 1
|
|
;
|
|
PredOrFunc = pf_predicate,
|
|
OrigArity = Arity
|
|
),
|
|
ProcLabelString = string.format("%s_%d_%d",
|
|
[s(LabelName), i(OrigArity), i(ModeNum)])
|
|
;
|
|
ProcLabel = special_proc_label(Module, SpecialPredId, TypeModule,
|
|
TypeName, TypeArity, ModeNum),
|
|
% For a special proc, output a label of the form:
|
|
% mercury____<PredName>___<TypeModule>__<TypeName>_<TypeArity>_<Mode>
|
|
|
|
% Figure out the LabelName.
|
|
DummyArity = -1, % not used by make_pred_or_func_name.
|
|
TypeCtor = type_ctor(qualified(TypeModule, TypeName), TypeArity),
|
|
PredName = special_pred_name(SpecialPredId, TypeCtor),
|
|
LabelName = make_pred_or_func_name(unqualified(""), pf_predicate,
|
|
unqualified(""), PredName, DummyArity, AddPrefix),
|
|
|
|
% Mangle all the relevant names.
|
|
MangledModule = sym_name_mangle(Module),
|
|
MangledTypeModule = sym_name_mangle(TypeModule),
|
|
MangledTypeName = name_mangle(TypeName),
|
|
|
|
% Module-qualify the type name.
|
|
% To handle locally produced unification preds for imported types,
|
|
% we need to qualify it with both the module name of the
|
|
% type, and also (if it is different) the module name of the
|
|
% current module.
|
|
QualifiedMangledTypeName = qualify_name(MangledTypeModule,
|
|
MangledTypeName),
|
|
FullyQualifiedMangledTypeName = maybe_qualify_name(MangledModule,
|
|
QualifiedMangledTypeName),
|
|
|
|
% Join it all together.
|
|
ProcLabelString = string.format("%s_%s_%d_%d",
|
|
[s(LabelName), s(FullyQualifiedMangledTypeName),
|
|
i(TypeArity), i(ModeNum)])
|
|
).
|
|
|
|
% Make a name identifying a predicate or a function, given the
|
|
% defining module, predicate or function indicator, declaring module,
|
|
% predicate name, arity, and whether or not to add the
|
|
% mercury_label_prefix.
|
|
%
|
|
:- func make_pred_or_func_name(module_name, pred_or_func, module_name, string,
|
|
arity, maybe_add_label_prefix) = string.
|
|
|
|
make_pred_or_func_name(DefiningModule, PredOrFunc, DeclaringModule,
|
|
Name0, Arity, AddPrefix) = LabelName :-
|
|
% WARNING: any changes to the name mangling algorithm here may also
|
|
% require changes to profiler/demangle.m, util/mdemangle.c and
|
|
% compiler/prog_foreign.m.
|
|
|
|
DeclaringModuleName = sym_name_mangle(DeclaringModule),
|
|
DefiningModuleName = sym_name_mangle(DefiningModule),
|
|
( if dont_module_qualify_name(Name0, Arity) then
|
|
LabelName0 = Name0
|
|
else
|
|
LabelName0 = qualify_name(DeclaringModuleName, Name0)
|
|
),
|
|
% If this is a specialized version of a predicate defined
|
|
% in some other module, then it needs both module prefixes.
|
|
( if DefiningModule \= DeclaringModule then
|
|
LabelName1 = DefiningModuleName ++ "__" ++ LabelName0
|
|
else
|
|
LabelName1 = LabelName0
|
|
),
|
|
LabelName2 = name_mangle(LabelName1),
|
|
(
|
|
PredOrFunc = pf_function,
|
|
LabelName3 = "fn__" ++ LabelName2
|
|
;
|
|
PredOrFunc = pf_predicate,
|
|
LabelName3 = LabelName2
|
|
),
|
|
(
|
|
AddPrefix = add_label_prefix,
|
|
LabelName = mercury_label_prefix ++ LabelName3
|
|
;
|
|
AddPrefix = do_not_add_label_prefix,
|
|
LabelName = LabelName3
|
|
).
|
|
|
|
% Define the conditions for which labels are printed
|
|
% without module qualification.
|
|
%
|
|
:- pred dont_module_qualify_name(string::in, arity::in) is semidet.
|
|
|
|
dont_module_qualify_name(Name, Arity) :-
|
|
(
|
|
Name = "main",
|
|
Arity = 2
|
|
;
|
|
string.prefix(Name, "__")
|
|
).
|
|
|
|
name_doesnt_need_mangling(Name) :-
|
|
string.is_all_alnum_or_underscore(Name),
|
|
not string.append("f_", _Suffix, Name).
|
|
|
|
sym_name_doesnt_need_mangling(unqualified(Name)) :-
|
|
name_doesnt_need_mangling(Name).
|
|
sym_name_doesnt_need_mangling(qualified(ModuleName, PlainName)) :-
|
|
sym_name_doesnt_need_mangling(ModuleName),
|
|
name_doesnt_need_mangling(PlainName).
|
|
|
|
% Produces a string of the form Module__Name, unless Module__
|
|
% is already a prefix of Name.
|
|
%
|
|
:- func maybe_qualify_name(string, string) = string.
|
|
|
|
maybe_qualify_name(Module0, Name0) = Name :-
|
|
string.append(Module0, "__", UnderscoresModule),
|
|
( if string.append(UnderscoresModule, _, Name0) then
|
|
Name = Name0
|
|
else
|
|
string.append(UnderscoresModule, Name0, Name)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
make_base_typeclass_info_name(TCName, TypeNames) = Str :-
|
|
TCName = tc_name(ModuleName, ClassName, ClassArity),
|
|
ClassSym = qualified(ModuleName, ClassName),
|
|
MangledClassString = sym_name_mangle(ClassSym),
|
|
MangledTypeNames = name_mangle(TypeNames),
|
|
Str = string.format("base_typeclass_info_%s__arity%d__%s",
|
|
[s(MangledClassString), i(ClassArity), s(MangledTypeNames)]).
|
|
|
|
make_base_typeclass_info_name_with_data_prefix(TCName, TypeNames) = Str :-
|
|
Str = mercury_data_prefix ++
|
|
make_base_typeclass_info_name(TCName, TypeNames).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
mercury_label_prefix = "mercury__".
|
|
|
|
mercury_var_prefix = "mercury_var_".
|
|
|
|
mercury_data_prefix = "mercury_data_".
|
|
mercury_scalar_common_array_prefix = "mercury_common_".
|
|
mercury_vector_common_array_prefix = "mercury_vector_common_".
|
|
|
|
mercury_common_type_prefix = "mercury_type_".
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module backend_libs.name_mangle.
|
|
%-----------------------------------------------------------------------------%
|