mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 14:25:56 +00:00
Estimated hours taken: 40
Branches: main
Implement true multi-cons_id arm switches, i.e. switches in which we associate
more than one cons_id with a switch arm. Previously, for switches like this:
(
X = a,
goal1
;
( X = b
; X = c
),
goal2
)
we duplicated goal2. With this diff, goal2 won't be duplicated. We still
duplicate goals when that is necessary, i.e. in cases which the inner
disjunction contains code other than a functor test on the switched-on var,
like this:
(
X = a,
goal1
;
(
X = b,
goalb
;
X = c
goalc
),
goal2
)
For now, true multi-cons_id arm switches are supported only by the LLDS
backend. Supporting them on the MLDS backend is trickier, because some MLDS
target languages (e.g. Java) don't support the concept at all. So when
compiling to MLDS, we still duplicate the goal in switch detection (although
we could delay the duplication to just before code generation, if we wanted.)
compiler/options.m:
Add an internal option that tells switch detection whether to look for
multi-cons_id switch arms.
compiler/handle_options.m:
Set this option based on the back end.
Add a version of the "trans" dump level that doesn't print unification
details.
compiler/hlds_goal.m:
Extend the representation of switch cases to allow more than one
cons_id for a switch arm.
Add a type for representing switches that also includes tag information
(for use by the backends).
compiler/hlds_data.m:
For du types, record whether it is possible to speed up tests for one
cons_id (e.g. cons) by testing for the other (nil) and negating the
result. Recording this information once is faster than having
unify_gen.m trying to compute it from scratch for every single
tag test.
Add a type for representing a cons_id together with its tag.
compiler/hlds_out.m:
Print out the cheaper_tag_test information for types, and possibly
several cons_ids for each switch arm.
Add some utility predicates for describing switch arms in terms of
which cons_ids they are for.
Replace some booleans with purpose-specific types.
Make hlds_out honor is documentation, and not print out detailed
information about unifications (e.g. uniqueness and static allocation)
unless the right character ('u') is present in the control string.
compiler/add_type.m:
Fill in the information about cheaper tag tests when adding a du type.
compiler/switch_detection.m:
Extend the switch detection algorithm to detect multi-cons_id switch
arms.
When entering a switch arm, update the instmap to reflect that the
switched-on variable can now be bound only to the cons_ids that this
switch arm is for. We now need to do this, because if the arm contains
another switch on the same variable, computing the can_fail field of
that switch correctly requires us to know this information.
(Obviously, an arm for a single cons_id is unlikely to have switch on
the same variable, and for arms for several cons_ids, we previously
duplicated the arm and left the unification with the cons_id in each
copy, and this unification allowed the correct handling of any later
switches. However, the code of a multi-cons_id switch arm obviously
cannot have a unification with each cons_id in it, which is why
we now need to get the binding information from the switch itself.)
Replace some booleans with purpose-specific types, and give some
predicates better names.
compiler/instmap.m:
Provide predicates for recording that a switched-on variable has
one of several given cons_ids, for use at the starts of switch arms.
Give some predicates better names.
compiler/modes.m:
Provide predicates for updating the mode_info at the start of a
multi-cons_id switch arm.
compiler/det_report.m:
Handle multi-cons_id switch arms.
Update the instmap when entering each switch arm, since this is needed
to provide good (i.e. non-misleading) error messages when one switch on
a variable exists inside another switch on the same variable.
Since updating the instmap requires updating the module_info (since
the new inst may require a new entry in an inst table), thread the
det_info through as updateable state.
Replace some multi-clause predicate definitions with single clauses,
to make it easier to print the arguments in mdb.
Fix some misleading variable names.
compiler/det_analysis.m:
Update the instmap when entering each switch arm and thread the
det_info through as updateable state, since the predicates we call
in det_report.m require this.
compiler/det_util.m:
Handle multi-cons_id switch arms.
Rationalize the argument order of some access predicates.
compiler/switch_util.m:
Change the parts of this module that deal with string and tag switches
to optionally convert each arm to an arbitrary representation of the
arm. In the LLDS backend, the conversion process generated code for
the arm, and the arm's representation is the label at the start of
this code. This way, we can duplicate the label without duplicating
the code.
Add a new part of this module that associates each cons_id with its
tag, and (during the same pass) checks whether all the cons_ids are
integers, and if so what are min and max of these integers (needed
for dense switches). This scan is needed because the old way of making
this test had single-cons_id switch arms as one of its basic
assumptions, and doing it while adding tags to each case reduces
the number of traversals required.
Give better names to some predicates.
compiler/switch_case.m:
New module to handle the tasks associated with managing multi-cons_id
switch arms, including representing them for switch_util.m.
compiler/ll_backend.m:
Include the new module.
compiler/notes/compiler_design.html:
Note the new module.
compiler/llds.m:
Change the computed goto instruction to take a list of maybe labels
instead of a list of labels, with any missing labels meaning "not
reached".
compiler/string_switch.m:
compiler/tag_switch.m:
Reorganize the way these modules work. We can't generate the code of
each arm in place anymore, since it is now possible for more than one
cons_id to call for the execution of the same code. Instead, in
string_switch.m, we generate the codes of all the arms all at once,
and construct the hash index afterwards. (This approach simplifies
the code significantly.)
In tag switches (unlike string switches), we can get locality benefits
if the code testing for a cons_id is close to the code for that
cons_id, so we still try to put them next to each other when such
a locality benefit is available.
In both modules, the new approach uses a utility predicate in
switch_case.m to actually generate the code of each switch arm,
eliminating several copies the same code in the old versions of these
modules.
In tag_switch.m, don't create a local label that simply jumps to the
code address do_not_reached. Previously, we had to do this for
positions in jump tables that corresponded to cons_ids that the switch
variable could not be bound to. With the change to llds.m, we now
simply generate a "no" instead.
compiler/lookup_switch.m:
Get the info about int switch limits from our caller; don't compute it
here.
Give some variables better names.
compiler/dense_switch.m:
Generate the codes of the cases all at once, then assemble the table,
duplicate the labels as needed. This separation of concerns allows
significant simplifications.
Pack up all the information shared between the predicate that detects
whether a dense switch is appropriate and the predicate that actually
generates the dense switch.
Move some utility predicates to switch_util.
compiler/switch_gen.m:
Delete the code for tagging cons_ids, since that functionality is now
in switch_util.m.
The old version of this module could call the code generator to produce
(i.e. materialize) the switched-on variable repeatedly. We now produce
the variable once, and do the switch on the resulting rval.
compiler/unify_gen.m:
Use the information about cheaper tag tests in the type constructor's
entry in the HLDS type table, instead of trying to recompute it
every time.
Provide the predicates switch_gen.m now needs to perform tag tests
on rvals, as opposed to variables, and against possible more than one
cons_id.
Allow the caller to provide the tag corresponding to the cons_id(s)
in tag tests, since when we are generating code for switches, the
required computations have already been done.
Factor out some code to make all this possible.
Give better names to some predicates.
compiler/code_info.m:
Provide some utility predicates for the new code in other modules.
Give better names to some existing predicates.
compiler/hlds_code_util.m:
Rationalize the argument order of some predicates.
Replace some multi-clause predicate definitions with single clauses,
to make it easier to print the arguments in mdb.
compiler/accumulator.m:
compiler/add_heap_ops.m:
compiler/add_pragma.m:
compiler/add_trail_ops.m:
compiler/assertion.m:
compiler/build_mode_constraints.m:
compiler/check_typeclass.m:
compiler/closure_analysis.m:
compiler/code_util.m:
compiler/constraint.m:
compiler/cse_detection.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/distance_granularity.m:
compiler/dupproc.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/exception_analysis.m:
compiler/export.m:
compiler/follow_code.m:
compiler/follow_vars.m:
compiler/foreign.m:
compiler/format_call.m:
compiler/frameopt.m:
compiler/goal_form.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/granularity.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/implicit_parallelism.m:
compiler/inlining.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lambda.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/livemap.m:
compiler/liveness.m:
compiler/llds_out.m:
compiler/llds_to_x86_64.m:
compiler/loop_inv.m:
compiler/make_hlds_warn.m:
compiler/mark_static_terms.m:
compiler/middle_rec.m:
compiler/ml_tag_switch.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/mode_util.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/pd_cost.m:
compiler/pd_into.m:
compiler/pd_util.m:
compiler/peephole.m:
compiler/polymorphism.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/purity.m:
compiler/quantification.m:
compiler/rbmm.actual_region_arguments.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_paths.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/saved_vars.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/store_alloc.m:
compiler/stratify.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lbu.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_norm.m:
compiler/term_pass1.m:
compiler/term_traversal.m:
compiler/trailing_analysis.m:
compiler/transform_llds.m:
compiler/tupling.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/unify_proc.m:
compiler/unique_modes.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
Make the changes necessary to conform to the changes above, principally
to handle multi-cons_id arm switches.
compiler/ml_string_switch.m:
Make the changes necessary to conform to the changes above, principally
to handle multi-cons_id arm switches.
Give some predicates better names.
compiler/dependency_graph.m:
Make the changes necessary to conform to the changes above, principally
to handle multi-cons_id arm switches. Change the order of arguments
of some predicates to make this easier.
compiler/bytecode.m:
compiler/bytecode_data.m:
compiler/bytecode_gen.m:
Make the changes necessary to conform to the changes above, principally
to handle multi-cons_id arm switches. (The bytecode interpreter
has not been updated.)
compiler/prog_rep.m:
mdbcomp/program_representation.m:
Change the byte sequence representation of goals to allow switch arms
with more than one cons_id. compiler/prog_rep.m now writes out the
updated representation, while mdbcomp/program_representation.m reads in
the updated representation.
deep_profiler/mdbprof_procrep.m:
Conform to the updated program representation.
tools/binary:
Fix a bug: if the -D option was given, the stage 2 directory wasn't
being initialized.
Abort if users try to give that option more than once.
compiler/Mercury.options:
Work around bug #32 in Mantis.
231 lines
9.1 KiB
Mathematica
231 lines
9.1 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1998-2001,2003-2007 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: transform_llds.
|
|
% Main author: petdr.
|
|
%
|
|
% This module does source to source transformations of the llds data
|
|
% structure. This is sometimes necessary to avoid limits in some compilers.
|
|
%
|
|
% This module currently transforms computed gotos into a binary search down to
|
|
% smaller computed gotos. This avoids a limitation in the lcc compiler.
|
|
%
|
|
% If accurate GC is enabled, we also append a module containing an end label
|
|
% to the list of comp_gen_c_modules.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module ll_backend.transform_llds.
|
|
:- interface.
|
|
|
|
:- import_module ll_backend.llds.
|
|
:- import_module io.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred transform_llds(c_file::in, c_file::out, io::di, io::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module hlds.hlds_pred.
|
|
:- import_module backend_libs.builtin_ops.
|
|
:- import_module hlds.code_model.
|
|
:- import_module libs.compiler_util.
|
|
:- import_module libs.globals.
|
|
:- import_module libs.options.
|
|
:- import_module mdbcomp.prim_data.
|
|
|
|
:- import_module counter.
|
|
:- import_module int.
|
|
:- import_module list.
|
|
:- import_module maybe.
|
|
:- import_module pair.
|
|
:- import_module set.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
transform_llds(!LLDS, !IO) :-
|
|
globals.io_get_globals(Globals, !IO),
|
|
transform_c_file(!LLDS, Globals).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred transform_c_file(c_file::in, c_file::out, globals::in) is det.
|
|
|
|
transform_c_file(CFile0, CFile, Globals) :-
|
|
ModuleName = CFile0 ^ cfile_modulename,
|
|
Modules0 = CFile0 ^ cfile_code,
|
|
% Split up large computed gotos.
|
|
globals.lookup_int_option(Globals, max_jump_table_size, MaxSize),
|
|
( MaxSize = 0 ->
|
|
Modules1 = Modules0
|
|
;
|
|
transform_c_module_list(Modules0, Modules1, MaxSize)
|
|
),
|
|
% Append an end label for accurate GC.
|
|
globals.get_gc_method(Globals, GC),
|
|
(
|
|
GC = gc_accurate,
|
|
Modules1 = [_ | _]
|
|
->
|
|
list.last_det(Modules1, LastModule),
|
|
LastModule = comp_gen_c_module(LastModuleName, _),
|
|
Modules = Modules1 ++
|
|
[gen_end_label_module(ModuleName, LastModuleName)]
|
|
;
|
|
Modules = Modules1
|
|
),
|
|
CFile = CFile0 ^ cfile_code := Modules.
|
|
|
|
% For LLDS native GC, we need to add a dummy comp_gen_c_module at the end
|
|
% of the list. This dummy module contains only a single dummy procedure
|
|
% which in turn contains only a single label, for which there is no
|
|
% stack layout structure. The point of this is to ensure that the
|
|
% address of this label gets inserted into the entry table, so that
|
|
% we know where the preceding procedure finishes when mapping from
|
|
% instruction pointer values to stack layout entries.
|
|
%
|
|
% Without this, we might think that the following C function was
|
|
% actually part of the last Mercury procedure in the preceding module,
|
|
% and then incorrectly use the stack layout of the Mercury procedure
|
|
% if we happened to get a heap overflow signal (SIGSEGV) while in that
|
|
% C function.
|
|
%
|
|
% Note that it is not sufficient to generate a label at end of the module,
|
|
% because GCC (e.g. GCC 3.2) sometimes reorders code within a single C
|
|
% function, so that a label declared at the end of the module might not
|
|
% be actually have highest address. So we generate a new module (which
|
|
% corresponds to a new C function). XXX Hopefully GCC won't mess with the
|
|
% order of the functions...
|
|
%
|
|
:- func gen_end_label_module(module_name, string) = comp_gen_c_module.
|
|
|
|
gen_end_label_module(ModuleName, LastModule) = EndLabelModule :-
|
|
Arity = 0,
|
|
ProcId = hlds_pred.initial_proc_id,
|
|
PredId = hlds_pred.initial_pred_id,
|
|
PredName = "ACCURATE_GC_END_LABEL",
|
|
ProcLabel = ordinary_proc_label(ModuleName, pf_predicate, ModuleName,
|
|
PredName, Arity, proc_id_to_int(ProcId)),
|
|
Instrs = [llds_instr(label(entry_label(entry_label_local, ProcLabel)),
|
|
"label to indicate end of previous procedure")],
|
|
DummyProc = c_procedure(PredName, Arity, proc(PredId, ProcId), model_det,
|
|
Instrs, ProcLabel, counter.init(0), must_not_alter_rtti, set.init),
|
|
EndLabelModule = comp_gen_c_module(LastModule ++ "_END", [DummyProc]).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred transform_c_module_list(list(comp_gen_c_module)::in,
|
|
list(comp_gen_c_module)::out, int::in) is det.
|
|
|
|
transform_c_module_list([], [], _MaxSize).
|
|
transform_c_module_list([Module0 | Module0s], [Module | Modules], MaxSize) :-
|
|
transform_c_module(Module0, Module, MaxSize),
|
|
transform_c_module_list(Module0s, Modules, MaxSize).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred transform_c_module(comp_gen_c_module::in, comp_gen_c_module::out,
|
|
int::in) is det.
|
|
|
|
transform_c_module(Module0, Module, MaxSize) :-
|
|
Module0 = comp_gen_c_module(Name, Procedures0),
|
|
transform_c_procedure_list(Procedures0, Procedures, MaxSize),
|
|
Module = comp_gen_c_module(Name, Procedures).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred transform_c_procedure_list(list(c_procedure)::in,
|
|
list(c_procedure)::out, int::in) is det.
|
|
|
|
transform_c_procedure_list([], [], _MaxSize).
|
|
transform_c_procedure_list([Proc0 | Proc0s], [Proc | Procs], MaxSize) :-
|
|
transform_c_procedure(Proc0, Proc, MaxSize),
|
|
transform_c_procedure_list(Proc0s, Procs, MaxSize).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred transform_c_procedure(c_procedure::in, c_procedure::out, int::in)
|
|
is det.
|
|
|
|
transform_c_procedure(!Proc, MaxSize) :-
|
|
ProcLabel = !.Proc ^ cproc_proc_label,
|
|
Instrs0 = !.Proc ^ cproc_code,
|
|
C0 = !.Proc ^ cproc_label_nums,
|
|
transform_instructions(Instrs0, Instrs, C0, C, ProcLabel, MaxSize),
|
|
!:Proc = !.Proc ^ cproc_code := Instrs,
|
|
!:Proc = !.Proc ^ cproc_label_nums := C.
|
|
|
|
:- pred transform_instructions(list(instruction)::in, list(instruction)::out,
|
|
counter::in, counter::out, proc_label::in, int::in) is det.
|
|
|
|
transform_instructions([], [], !C, _, _).
|
|
transform_instructions([Instr0 | Instrs0], Instrs, !C, ProcLabel, MaxSize) :-
|
|
transform_instructions(Instrs0, InstrsTail, !C, ProcLabel, MaxSize),
|
|
(
|
|
Instr0 = llds_instr(computed_goto(Rval, Targets), Comment),
|
|
list.length(Targets, NumTargets),
|
|
NumTargets > MaxSize
|
|
->
|
|
split_computed_goto(Rval, Targets, Comment, InstrsHead, !C,
|
|
MaxSize, NumTargets, ProcLabel),
|
|
list.append(InstrsHead, InstrsTail, Instrs)
|
|
;
|
|
Instrs = [Instr0 | InstrsTail]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Given the pieces of a computed_goto instruction, split the table
|
|
% in half as many times as necessary to bring the jump table size
|
|
% below MaxSize, doing a binary search on the way.
|
|
%
|
|
:- pred split_computed_goto(rval::in, list(maybe(label))::in, string::in,
|
|
list(instruction)::out, counter::in, counter::out, int::in, int::in,
|
|
proc_label::in) is det.
|
|
|
|
split_computed_goto(Rval, Targets, Comment, Instrs, !C, MaxSize, NumTargets,
|
|
ProcLabel) :-
|
|
( NumTargets =< MaxSize ->
|
|
Instrs = [llds_instr(computed_goto(Rval, Targets), Comment)]
|
|
;
|
|
counter.allocate(LabelNum, !C),
|
|
Mid = NumTargets // 2,
|
|
( list.split_list(Mid, Targets, StartTargetsPrime, EndTargetsPrime) ->
|
|
StartTargets = StartTargetsPrime,
|
|
EndTargets = EndTargetsPrime
|
|
;
|
|
unexpected(this_file, "split_computed_goto: list.split_list")
|
|
),
|
|
|
|
Index = binop(int_sub, Rval, const(llconst_int(Mid))),
|
|
Test = binop(int_ge, Rval, const(llconst_int(Mid))),
|
|
ElseAddr = code_label(internal_label(LabelNum, ProcLabel)),
|
|
IfInstr = llds_instr(if_val(Test, ElseAddr), "binary search"),
|
|
ElseInstr = llds_instr(label(internal_label(LabelNum, ProcLabel)), ""),
|
|
|
|
split_computed_goto(Rval, StartTargets, Comment ++ " then",
|
|
ThenInstrs, !C, MaxSize, Mid, ProcLabel),
|
|
split_computed_goto(Index, EndTargets, Comment ++ " else",
|
|
ElseInstrs, !C, MaxSize, NumTargets - Mid, ProcLabel),
|
|
|
|
Instrs = [IfInstr | ThenInstrs] ++ [ElseInstr | ElseInstrs]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- func this_file = string.
|
|
|
|
this_file = "transform_llds.m".
|
|
|
|
%-----------------------------------------------------------------------------%
|