mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-16 18:03:36 +00:00
The code_info type was originally designed to be a single data structure
that holds all of the state of the LLDS code generator. It HAD to be a single
data structure then, because the DCGs we used to pass state around only
supported passing around ONE piece of state. Nevertheless, it contained
three separate kinds of information:
1 static information, which never changed during the lifetime of a code_info
structure (such as the pred and proc id of the procedure being compiled),
2 persistent information, whose updates were never undone (such as the maximum
number of temporaries that were ever needed at any one time), and
3 location dependent information, such as "which variables are stored where",
whose updates *can* be undone when the code generator jumps back to
a previously visited point in the code, e.g. to start generating code
for the next disjunct in a disjunction.
Originally, these three kinds of fields were all jumbled up together, but
about ten years ago, I grouped all the fields of the same kind together,
into substructures of code_info named code_info_static, code_info_persistent
and code_info_loc_dep respectively. This improved matters, but some problems
remained, the most important of which is that the code_info always contained
the location dependent information, even when it wasn't meaningful, and there
was no way of indicating this fact. (After initialization, the other two parts
are always meaningful.)
This diff separates out the location dependent part of the code_info
into a new type, code_loc_dep, that can be passed around independently
of the code_info, which now contains only the first two kinds of information
above. In places where the location-dependent information is not meaningful,
you don't need to have a current code_loc_dep.
This separation also makes it easier to see what updates to the code generator
state change only the persistent part (the updated code_info type), only
the location-dependent part (the new code_loc_dep type), or both.
In the process of making this change, I found several places where the
location-dependent part of the code_info (now the code_loc_dep) was being
updated, only for those updates to be thrown away, unread, a short time later.
This happened at the ends of branches in e.g. switches, with the updated
code_loc_deps being thrown away when code generation started working on
the next branch.
compiler/code_loc_dep.m:
New module containing the location-dependent part of the LLDS code
generator state. Its contents are derived from those parts of the
old contents of code_info.m that deal with location-dependent state.
Many of the predicates moved here work on only on the code_loc_dep
structure, some others work on both the code_info and code_loc_dep
structure, and a few work only on the code_info. Predicates in the last
category are in code_loc_dep.m only if they are either (a) used only
in this module, or (b) used only with other predicates in this module.
compiler/ll_backend.m:
compiler/notes/compiler_design.html:
Mention the new module.
compiler/code_info.m:
Delete the code now in code_loc_dep.m.
Make the vartypes a field in the static part of the code_info, since it is
used quite often. (We used to look it up in the proc_info every time.)
Put the declarations and definitions of the access predicates in an order
that is consistent with the order of the fields they work on.
Give some fields and predicates more descriptive names.
compiler/call_gen.m:
compiler/code_gen.m:
compiler/commit_gen.m:
compiler/dense_switch.m:
compiler/disj_gen.m:
compiler/ite_gen.m:
compiler/ll_backend.m:
compiler/lookup_switch.m:
compiler/lookup_util.m:
compiler/middle_rec.m:
compiler/par_conj_gen.m:
compiler/pragma_c_gen.m:
compiler/proc_gen.m:
compiler/string_switch.m:
compiler/switch_case.m:
compiler/switch_gen.m:
compiler/tag_switch.m:
compiler/trace_gen.m:
compiler/unify_gen.m:
compiler/var_locn.m:
Conform to and take advantage of the changes above.
Often this required passing an in/out pair of code_loc_deps as well as
an in/out pair of code_infos, but in many cases, one or more of these
would not be needed.
Don't make changes to the code_loc_dep at the end of an arm of a branched
control structure if those updates are about be thrown away when we
start generating code for the next arm.
In several cases, switch to a strategy of taking a snapshot of the
code_loc_dep before entering a branched control structure as a whole,
and restoring that state at the start of each arm. We used to take
a snapshot at the start of each branch, and restore it at its end,
to be ready for the next branch. The former is easier to make
correctness arguments about, since the code_loc_dep in an arm
often has limited scope.
Make some minor unrelated improvements, such as eliminating the
unnecessary use of solutions/2, and reordering tests for slightly
better performance.
365 lines
14 KiB
Mathematica
365 lines
14 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 1994-2012 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: code_gen.m.
|
|
% Main authors: conway, zs.
|
|
%
|
|
% The task of this module is to provide a generic predicate that can be called
|
|
% from anywhere in the code generator to generate code for a goal. We forward
|
|
% most of the actual construction of code for particular types of goals
|
|
% to other modules. The generation of code for unifications is done
|
|
% by unify_gen, for calls, higher-order calls and method calls by call_gen,
|
|
% for commits by commit_gen, for if-then-elses and negations by ite_gen,
|
|
% for switches by switch_gen and its subsidiary modules, for disjunctions
|
|
% by disj_gen, for parallel conjunctions by par_conj_gen, and for foreign_procs
|
|
% by pragma_c_gen. The only goals handled directly by code_gen are sequential
|
|
% conjunctions.
|
|
%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module ll_backend.code_gen.
|
|
:- interface.
|
|
|
|
:- import_module hlds.code_model.
|
|
:- import_module hlds.hlds_goal.
|
|
:- import_module ll_backend.code_info.
|
|
:- import_module ll_backend.code_loc_dep.
|
|
:- import_module ll_backend.llds.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
% Translate a HLDS goal to LLDS.
|
|
%
|
|
:- pred generate_goal(code_model::in, hlds_goal::in, llds_code::out,
|
|
code_info::in, code_info::out, code_loc_dep::in, code_loc_dep::out) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module hlds.goal_form.
|
|
:- import_module hlds.hlds_desc.
|
|
:- import_module hlds.hlds_pred.
|
|
:- import_module hlds.instmap.
|
|
:- import_module libs.globals.
|
|
:- import_module ll_backend.call_gen.
|
|
:- import_module ll_backend.commit_gen.
|
|
:- import_module ll_backend.disj_gen.
|
|
:- import_module ll_backend.ite_gen.
|
|
:- import_module ll_backend.opt_debug.
|
|
:- import_module ll_backend.par_conj_gen.
|
|
:- import_module ll_backend.pragma_c_gen.
|
|
:- import_module ll_backend.switch_gen.
|
|
:- import_module ll_backend.unify_gen.
|
|
:- import_module parse_tree.prog_data.
|
|
:- import_module parse_tree.set_of_var.
|
|
|
|
:- import_module bool.
|
|
:- import_module cord.
|
|
:- import_module int.
|
|
:- import_module io.
|
|
:- import_module list.
|
|
:- import_module map.
|
|
:- import_module maybe.
|
|
:- import_module require.
|
|
:- import_module set.
|
|
:- import_module string.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
generate_goal(ContextModel, Goal, Code, !CI, !CLD) :-
|
|
% Generate a goal. This predicate arranges for the necessary updates of
|
|
% the generic data structures before and after the actual code generation,
|
|
% which is delegated to goal-specific predicates.
|
|
|
|
trace [compiletime(flag("codegen_goal")), io(!IO)] (
|
|
some [ModuleInfo, VarSet, GoalDesc] (
|
|
code_info.get_module_info(!.CI, ModuleInfo),
|
|
code_info.get_varset(!.CI, VarSet),
|
|
GoalDesc = describe_goal(ModuleInfo, VarSet, Goal),
|
|
|
|
( should_trace_code_gen(!.CI) ->
|
|
io.format("\nGOAL START: %s\n", [s(GoalDesc)], !IO)
|
|
;
|
|
true
|
|
)
|
|
)
|
|
),
|
|
|
|
% Make any changes to liveness before Goal.
|
|
get_forward_live_vars(!.CLD, ForwardLiveVarsBeforeGoal),
|
|
Goal = hlds_goal(GoalExpr, GoalInfo),
|
|
HasSubGoals = goal_expr_has_subgoals(GoalExpr),
|
|
pre_goal_update(GoalInfo, HasSubGoals, !CLD),
|
|
get_instmap(!.CLD, InstMap),
|
|
( instmap_is_reachable(InstMap) ->
|
|
CodeModel = goal_info_get_code_model(GoalInfo),
|
|
% Sanity check: code of some code models should occur
|
|
% only in limited contexts.
|
|
(
|
|
CodeModel = model_det
|
|
;
|
|
CodeModel = model_semi,
|
|
(
|
|
ContextModel = model_det,
|
|
unexpected($module, $pred, "semidet model in det context")
|
|
;
|
|
( ContextModel = model_semi
|
|
; ContextModel = model_non
|
|
)
|
|
)
|
|
;
|
|
CodeModel = model_non,
|
|
(
|
|
( ContextModel = model_det
|
|
; ContextModel = model_semi
|
|
),
|
|
unexpected($module, $pred,
|
|
"nondet model in det/semidet context")
|
|
;
|
|
ContextModel = model_non
|
|
)
|
|
),
|
|
|
|
generate_goal_expr(GoalExpr, GoalInfo, CodeModel,
|
|
ForwardLiveVarsBeforeGoal, GoalCode, !CI, !CLD),
|
|
Features = goal_info_get_features(GoalInfo),
|
|
get_proc_info(!.CI, ProcInfo),
|
|
|
|
% If the predicate's evaluation method is memo, loopcheck or minimal
|
|
% model, the goal generated the variable that represents the call table
|
|
% tip, *and* tracing is enabled, then we save this variable to its
|
|
% stack slot. This is necessary to enable retries across this procedure
|
|
% to reset the call table entry to uninitialized, effectively removing
|
|
% the call table entry.
|
|
%
|
|
% If tracing is not enabled, then CallTableVar isn't guaranteed
|
|
% to have a stack slot.
|
|
(
|
|
set.member(feature_call_table_gen, Features),
|
|
get_proc_info(!.CI, ProcInfo),
|
|
proc_info_get_call_table_tip(ProcInfo, MaybeCallTableVar),
|
|
MaybeCallTableVar = yes(CallTableVar),
|
|
get_maybe_trace_info(!.CI, yes(_))
|
|
->
|
|
save_variables_on_stack([CallTableVar], TipSaveCode, !.CI, !CLD),
|
|
CodeUptoTip = GoalCode ++ TipSaveCode
|
|
;
|
|
CodeUptoTip = GoalCode
|
|
),
|
|
% After the goal that generates the variables needed at the exception
|
|
% port, on which deep_profiling.m puts the save_deep_excp_vars feature,
|
|
% save those variables in their stack slots. The procedure layout
|
|
% structure gives the identity of their slots, and exception.m
|
|
% expects to find the variables in their stack slots.
|
|
%
|
|
% These variables are computed by the call port code and are needed
|
|
% by the exit and fail port codes, so their lifetime is the entire
|
|
% procedure invocation. If the procedure makes any calls other than
|
|
% the ones inserted by deep profiling, then all the variables will have
|
|
% stack slots, and we save them all on the stack. If the procedure
|
|
% doesn't make any such calls, then the variables won't have stack
|
|
% slots, but they won't *need* stack slots either, since there is no
|
|
% way for such a leaf procedure to throw an exception. (Throwing
|
|
% requires calling exception.throw, directly or indirectly.)
|
|
( set.member(feature_save_deep_excp_vars, Features) ->
|
|
DeepSaveVars = compute_deep_save_excp_vars(ProcInfo),
|
|
save_variables_on_stack(DeepSaveVars, DeepSaveCode, !.CI, !CLD),
|
|
Code = CodeUptoTip ++ DeepSaveCode
|
|
;
|
|
Code = CodeUptoTip
|
|
),
|
|
|
|
% Make live any variables which subsequent goals will expect to be
|
|
% live, but were not generated.
|
|
set_instmap(InstMap, !CLD),
|
|
post_goal_update(GoalInfo, !.CI, !CLD)
|
|
;
|
|
Code = empty
|
|
),
|
|
trace [compiletime(flag("codegen_goal")), io(!IO)] (
|
|
some [ModuleInfo, VarSet, GoalDesc] (
|
|
code_info.get_module_info(!.CI, ModuleInfo),
|
|
code_info.get_varset(!.CI, VarSet),
|
|
GoalDesc = describe_goal(ModuleInfo, VarSet, Goal),
|
|
|
|
( should_trace_code_gen(!.CI) ->
|
|
io.format("\nGOAL FINISH: %s\n", [s(GoalDesc)], !IO),
|
|
Instrs = cord.list(Code),
|
|
write_instrs(Instrs, no, yes, !IO)
|
|
;
|
|
true
|
|
)
|
|
)
|
|
).
|
|
|
|
:- func compute_deep_save_excp_vars(proc_info) = list(prog_var).
|
|
|
|
compute_deep_save_excp_vars(ProcInfo) = DeepSaveVars :-
|
|
proc_info_get_maybe_deep_profile_info(ProcInfo, MaybeDeepProfInfo),
|
|
(
|
|
MaybeDeepProfInfo = yes(DeepProfInfo),
|
|
MaybeDeepLayout = DeepProfInfo ^ deep_layout,
|
|
MaybeDeepLayout = yes(DeepLayout)
|
|
->
|
|
ExcpVars = DeepLayout ^ deep_layout_excp,
|
|
ExcpVars = hlds_deep_excp_vars(TopCSDVar, MiddleCSDVar,
|
|
MaybeOldOutermostVar),
|
|
proc_info_get_stack_slots(ProcInfo, StackSlots),
|
|
( map.search(StackSlots, TopCSDVar, _) ->
|
|
% If one of these variables has a stack slot, the others must
|
|
% have one too.
|
|
(
|
|
MaybeOldOutermostVar = yes(OldOutermostVar),
|
|
DeepSaveVars = [TopCSDVar, MiddleCSDVar, OldOutermostVar]
|
|
;
|
|
MaybeOldOutermostVar = no,
|
|
DeepSaveVars = [TopCSDVar, MiddleCSDVar]
|
|
)
|
|
;
|
|
DeepSaveVars = []
|
|
)
|
|
;
|
|
unexpected($module, $pred, "inconsistent proc_info")
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred generate_goal_expr(hlds_goal_expr::in, hlds_goal_info::in,
|
|
code_model::in, set_of_progvar::in, llds_code::out,
|
|
code_info::in, code_info::out, code_loc_dep::in, code_loc_dep::out) is det.
|
|
|
|
generate_goal_expr(GoalExpr, GoalInfo, CodeModel, ForwardLiveVarsBeforeGoal,
|
|
Code, !CI, !CLD) :-
|
|
(
|
|
GoalExpr = unify(_, _, _, Uni, _),
|
|
unify_gen.generate_unification(CodeModel, Uni, GoalInfo, Code,
|
|
!CI, !CLD)
|
|
;
|
|
GoalExpr = conj(ConjType, Goals),
|
|
(
|
|
ConjType = plain_conj,
|
|
generate_conj(Goals, CodeModel, cord.init, Code, !CI, !CLD)
|
|
;
|
|
ConjType = parallel_conj,
|
|
par_conj_gen.generate_par_conj(Goals, GoalInfo, CodeModel, Code,
|
|
!CI, !CLD)
|
|
)
|
|
;
|
|
GoalExpr = disj(Goals),
|
|
disj_gen.generate_disj(CodeModel, Goals, GoalInfo, Code, !CI, !CLD)
|
|
;
|
|
GoalExpr = negation(Goal),
|
|
ite_gen.generate_negation(CodeModel, Goal, GoalInfo, Code, !CI, !CLD)
|
|
;
|
|
GoalExpr = if_then_else(_Vars, Cond, Then, Else),
|
|
ite_gen.generate_ite(CodeModel, Cond, Then, Else, GoalInfo, Code,
|
|
!CI, !CLD)
|
|
;
|
|
GoalExpr = switch(Var, CanFail, CaseList),
|
|
switch_gen.generate_switch(CodeModel, Var, CanFail, CaseList, GoalInfo,
|
|
Code, !CI, !CLD)
|
|
;
|
|
GoalExpr = scope(Reason, SubGoal),
|
|
( Reason = from_ground_term(TermVar, from_ground_term_construct) ->
|
|
unify_gen.generate_ground_term(TermVar, SubGoal, !CI, !CLD),
|
|
Code = empty
|
|
; Reason = loop_control(LCVar, LCSVar, UseParentStack) ->
|
|
par_conj_gen.generate_lc_spawn_off(SubGoal, LCVar, LCSVar,
|
|
UseParentStack, Code, !CI, !CLD)
|
|
;
|
|
commit_gen.generate_scope(Reason, CodeModel, GoalInfo,
|
|
ForwardLiveVarsBeforeGoal, SubGoal, Code, !CI, !CLD)
|
|
)
|
|
;
|
|
GoalExpr = generic_call(GenericCall, Args, Modes, MaybeRegTypes, Det),
|
|
call_gen.generate_generic_call(CodeModel, GenericCall, Args,
|
|
Modes, MaybeRegTypes, Det, GoalInfo, Code, !CI, !CLD)
|
|
;
|
|
GoalExpr = plain_call(PredId, ProcId, Args, BuiltinState, _, _),
|
|
(
|
|
BuiltinState = not_builtin,
|
|
call_gen.generate_call(CodeModel, PredId, ProcId, Args, GoalInfo,
|
|
Code, !CI, !CLD)
|
|
;
|
|
( BuiltinState = inline_builtin
|
|
; BuiltinState = out_of_line_builtin
|
|
),
|
|
call_gen.generate_builtin(CodeModel, PredId, ProcId, Args,
|
|
Code, !CI, !CLD)
|
|
)
|
|
;
|
|
GoalExpr = call_foreign_proc(Attributes, PredId, ProcId,
|
|
Args, ExtraArgs, MaybeTraceRuntimeCond, PragmaCode),
|
|
Lang = get_foreign_language(Attributes),
|
|
(
|
|
Lang = lang_c,
|
|
generate_foreign_proc_code(CodeModel, Attributes,
|
|
PredId, ProcId, Args, ExtraArgs, MaybeTraceRuntimeCond,
|
|
PragmaCode, GoalInfo, Code, !CI, !CLD)
|
|
;
|
|
( Lang = lang_java
|
|
; Lang = lang_csharp
|
|
; Lang = lang_il
|
|
; Lang = lang_erlang
|
|
),
|
|
unexpected($module, $pred, "foreign code other than C")
|
|
)
|
|
;
|
|
GoalExpr = shorthand(_),
|
|
% These should have been expanded out by now.
|
|
unexpected($module, $pred, "shorthand")
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
% Generate a conjoined series of goals. State information flows directly
|
|
% from one conjunct to the next.
|
|
%
|
|
% We call generate_conj_inner to generate code for up to 1000 goals.
|
|
% If there are any more goals left after that, we let generate_conj_inner
|
|
% give up all its stack frames before calling it again. This allows us
|
|
% to generate code for *very* long sequences of goals even if the compiler
|
|
% is compiled in a grade that does not allow tail recursion.
|
|
%
|
|
:- pred generate_conj(list(hlds_goal)::in, code_model::in,
|
|
llds_code::in, llds_code::out,
|
|
code_info::in, code_info::out, code_loc_dep::in, code_loc_dep::out) is det.
|
|
|
|
generate_conj([], _, !Code, !CI, !CLD).
|
|
generate_conj(Goals @ [_ | _], CodeModel, !Code, !CI, !CLD) :-
|
|
generate_conj_inner(Goals, 1000, CodeModel, LeftOverGoals, !Code,
|
|
!CI, !CLD),
|
|
generate_conj(LeftOverGoals, CodeModel, !Code, !CI, !CLD).
|
|
|
|
:- pred generate_conj_inner(list(hlds_goal)::in, int::in, code_model::in,
|
|
list(hlds_goal)::out, llds_code::in, llds_code::out,
|
|
code_info::in, code_info::out, code_loc_dep::in, code_loc_dep::out) is det.
|
|
|
|
generate_conj_inner([], _, _, [], !Code, !CI, !CLD).
|
|
generate_conj_inner([Goal | Goals], N, CodeModel, LeftOverGoals, !Code,
|
|
!CI, !CLD) :-
|
|
( N > 0 ->
|
|
generate_goal(CodeModel, Goal, GoalCode, !CI, !CLD),
|
|
!:Code = !.Code ++ GoalCode,
|
|
get_instmap(!.CLD, Instmap),
|
|
( instmap_is_unreachable(Instmap) ->
|
|
LeftOverGoals = []
|
|
;
|
|
generate_conj_inner(Goals, N - 1, CodeModel, LeftOverGoals, !Code,
|
|
!CI, !CLD)
|
|
)
|
|
;
|
|
LeftOverGoals = [Goal | Goals]
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
:- end_module ll_backend.code_gen.
|
|
%---------------------------------------------------------------------------%
|