mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-18 15:26:31 +00:00
Estimated hours taken: 50
This change introduces interface tracing, and makes it possible to successfully
bootstrap the compiler with tracing (either interface or full).
compiler/options.m:
Change the bool options --generate-trace into a string option --trace
with three valid values: minimal, interface and full. The last two mean
what they say; the intention is that eventually minimal will mean
no tracing in non-tracing grades and interface tracing in tracing
grades.
compiler/globals.m:
Add a new global for the trace level.
compiler/handle_options.m:
Convert the argument of --trace to a trace level.
Use only consistent 4-space indentation in the deeply nested
if-then-else.
compiler/trace.m:
Implement interface tracing.
Rename trace__generate_depth_reset_code as trace__prepare_for_call,
since it does more than reset the depth if this module is compiled
with interface tracing.
Do not check whether tracing is enabled before calling MR_trace;
let MR_trace make the check. This trades increased non-tracing
execution time for a substantial code size reduction (which may
in turn benefit execution time).
compiler/call_gen.m:
Call trace__generate_depth_reset_code by its new name.
compiler/code_info.m:
Fix a bug in the handling of non/semi commits. When entering a commit,
we used to push a clone of whatever the top failure continuation was.
However, the resume setup for this continuation could have started
with a label that assumed that the resume vars were in their original
locations (which are often registers), whereas the method of
backtracking to that point only guarantees the survival of stack slots,
not registers.
(This bug caused two lines of incorrect code to be generated among
the approx 30 million lines of code in the stage 2 compiler when
compiled with tracing.)
Fix another bug (previously untriggered as far as I know) in the
handling of multi/det commits. This one was breaking the invariant
that the resume vars set of each entry on the failure continuation
stack included the resume vars set of every other entry below it,
which meant that the values of these resume vars were not guaranteed
to be preserved.
compiler/stack_layout.m:
Make layout structures local to their module. They are not (yet)
referred to by name from other modules, and by declaring them
to be global we caused their names to be included even in stripped
executables, adding several megabytes to the size of the binary.
(The names are not stripped because a dynamically linked library
may want to refer to them.)
Change the mercury_data__stack_layout__ prefix on the names of
generated globals vars to just mercury_data__layout__. It is now
merely too long instead of far too long.
Include the label number in the label layout structure and the number
of typeinfo variables in a var_info structure only with native gc.
Their only use is in debugging native gc.
Fix some documentation rot.
compiler/llds.m:
Add a new field to the pragma_c instruction that says whether the
compiler-generated C code fragments access any stack variables.
compiler/frameopt.m:
Use the new field in pragma_c's to avoid a bug. Because frameopt was
assuming that the pragma_c instruction that filled in the stack slots
containing the call sequence number and depth did not access the stack,
it moved the pragma_c before the incr_sp that allocates the frame
(it was trying to get it out of the loop).
compiler/*.m:
Minor changes to set or ignore the extra field in pragma_c, to refer
to layout structures via the new prefix, or to handle the --trace
option.
doc/user_guide.texi:
Update the documentation for --trace.
runtime/mercury_types.h:
Add the type Unsigned.
runtime/mercury_goto.h:
Use the shorter layout prefix.
runtime/mercury_stack_layout.h:
Use the shorter layout prefix, and include the label number only with
native gc.
runtime/mercury_trace.[ch]:
runtime/mercury_trace_internal.[ch]:
runtime/mercury_trace_external.[ch]:
runtime/mercury_trace_util.[ch]:
Divide the old mercury_trace.[ch] into several components, with one
module for the internal debugger, one for the interface to the
external debugger, one for utilities needed by both. Mercury_trace.c
now has only the top-level stuff that steers between the two
debuggers.
runtime/mercury_trace.[ch]:
Add the new global variable MR_trace_from_full. Before each call,
the calling procedure assigns TRUE to this variable if the caller
is fully traced, and FALSE otherwise. Interface traced procedures
generate trace events only if this variable is TRUE when they are
called (fully traced callee procedures ignore the initial value of
the variable).
Make MR_trace return immediately without doing anything unless
tracing is enabled and a new extra argument to MR_trace is TRUE.
This extra argument is always TRUE for trace events in fully traced
procedures, while for trace events from interface traced procedures,
its value is set from the value of MR_trace_from_full at the time
that the procedure was called (i.e. the event is ignored unless the
interface traced procedure was called from a fully traced procedure).
runtime/mercury_trace.[ch]:
runtime/mercury_trace_internal.[ch]:
For global variables that are stored in stack slots, make their type
Word rather than int.
Use a new function MR_trace_event_report instead of calling
MR_trace_event with a NULL command structure pointer to indicate
that the event is to be reported but there is to be no user
interaction.
Use %ld formats in printfs and casts to long for better portability.
runtime/mercury_trace_internal.c:
Save trace-related globals across calls to Mercury library code
in the debugger, since otherwise any trace events in this code
could screw up e.g. the event number or the call number sequence.
Create separate functions for printing port names and determinisms.
runtime/mercury_wrapper.h:
Disable the tracing of the initialization and finalization code
written in Mercury.
runtime/Mmakefile:
Update for the new source and header files.
tests/debugger/{debugger_regs,interpreter,queens}_lib.{m,inp,exp}:
One new copy of each existing test case. These ones are intended
to be used when the stage 2 library is compiled with tracing, which
affects the tests by adding events for the library procedures called
from the test programs.
The .m files are the same as before; one of the .inp files is a bit
different; the .exp files reflect the correct output when the library
is compiled with full tracing.
tests/debugger/Mmakefile:
Provide separate targets for the new set of test cases.
Use --trace full instead of --generate-trace.
tests/debugger/runtests:
Try both the new set of test cases if the old set fails, and report
failure only if both sets fail. This is simpler than trying to figure
out which set should be really tested, and the probability of a false
positive is negligible.
828 lines
27 KiB
Mathematica
828 lines
27 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 1994-1998 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: call_gen.m
|
|
%
|
|
% main author: conway.
|
|
%
|
|
% This module provides predicates for generating procedure calls,
|
|
% including calls to higher-order pred variables.
|
|
%
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module call_gen.
|
|
|
|
:- interface.
|
|
|
|
:- import_module prog_data, hlds_pred, hlds_data, hlds_goal, llds, code_info.
|
|
:- import_module term, list, set, assoc_list, std_util.
|
|
|
|
:- pred call_gen__generate_higher_order_call(code_model, var, list(var),
|
|
list(type), list(mode), determinism, hlds_goal_info,
|
|
code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_higher_order_call(in, in, in, in, in, in, in, out,
|
|
in, out) is det.
|
|
|
|
:- pred call_gen__generate_class_method_call(code_model, var, int, list(var),
|
|
list(type), list(mode), determinism, hlds_goal_info,
|
|
code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_class_method_call(in, in, in, in, in, in, in, in,
|
|
out, in, out) is det.
|
|
|
|
:- pred call_gen__generate_call(code_model, pred_id, proc_id, list(var),
|
|
hlds_goal_info, code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_call(in, in, in, in, in, out, in, out) is det.
|
|
|
|
:- pred call_gen__generate_det_builtin(pred_id, proc_id, list(var),
|
|
code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_det_builtin(in, in, in, out, in, out) is det.
|
|
|
|
:- pred call_gen__generate_semidet_builtin(pred_id, proc_id, list(var),
|
|
code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_semidet_builtin(in, in, in, out, in, out) is det.
|
|
|
|
:- pred call_gen__generate_nondet_builtin(pred_id, proc_id, list(var),
|
|
code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_nondet_builtin(in, in, in, out, in, out) is
|
|
erroneous.
|
|
|
|
/* DEAD CODE
|
|
:- pred call_gen__generate_complicated_unify(var, var, uni_mode, can_fail,
|
|
code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_complicated_unify(in, in, in, in, out, in, out)
|
|
is det.
|
|
*/
|
|
|
|
:- pred call_gen__partition_args(assoc_list(var, arg_info),
|
|
list(var), list(var)).
|
|
:- mode call_gen__partition_args(in, out, out) is det.
|
|
|
|
:- pred call_gen__input_arg_locs(list(pair(var, arg_info)),
|
|
list(pair(var, arg_loc))).
|
|
:- mode call_gen__input_arg_locs(in, out) is det.
|
|
|
|
:- pred call_gen__output_arg_locs(list(pair(var, arg_info)),
|
|
list(pair(var, arg_loc))).
|
|
:- mode call_gen__output_arg_locs(in, out) is det.
|
|
|
|
:- pred call_gen__save_variables(set(var), code_tree,
|
|
code_info, code_info).
|
|
:- mode call_gen__save_variables(in, out, in, out) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module hlds_module, code_util.
|
|
:- import_module arg_info, type_util, mode_util, unify_proc, instmap.
|
|
:- import_module trace, globals, options.
|
|
:- import_module bool, int, tree, map.
|
|
:- import_module varset, require.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__generate_call(CodeModel, PredId, ModeId, Arguments, GoalInfo, Code)
|
|
-->
|
|
|
|
% find out which arguments are input and which are output
|
|
code_info__get_pred_proc_arginfo(PredId, ModeId, ArgInfo),
|
|
{ assoc_list__from_corresponding_lists(Arguments, ArgInfo, ArgsInfos) },
|
|
|
|
% save the known variables on the stack, except those
|
|
% generated by this call
|
|
{ call_gen__select_out_args(ArgsInfos, OutArgs) },
|
|
call_gen__save_variables(OutArgs, SaveCode),
|
|
|
|
% save possibly unknown variables on the stack as well
|
|
% if they may be needed on backtracking, and figure out the
|
|
% call model
|
|
call_gen__prepare_for_call(CodeModel, FlushCode, CallModel, _),
|
|
|
|
% move the input arguments to their registers
|
|
code_info__setup_call(ArgsInfos, caller, SetupCode),
|
|
|
|
code_info__get_maybe_trace_info(MaybeTraceInfo),
|
|
( { MaybeTraceInfo = yes(TraceInfo) } ->
|
|
{ trace__prepare_for_call(TraceInfo, TraceCode) }
|
|
;
|
|
{ TraceCode = empty }
|
|
),
|
|
|
|
% figure out what locations are live at the call point,
|
|
% for use by the value numbering optimization
|
|
{ call_gen__input_args(ArgInfo, InputArguments) },
|
|
call_gen__generate_call_livevals(OutArgs, InputArguments, LiveCode),
|
|
|
|
% figure out what variables will be live at the return point,
|
|
% and where, for use in the accurate garbage collector
|
|
code_info__get_instmap(InstMap),
|
|
{ goal_info_get_instmap_delta(GoalInfo, InstMapDelta) },
|
|
{ instmap__apply_instmap_delta(InstMap, InstMapDelta,
|
|
AfterCallInstMap) },
|
|
{ call_gen__output_arg_locs(ArgsInfos, OutputArguments) },
|
|
call_gen__generate_return_livevals(OutArgs, OutputArguments,
|
|
AfterCallInstMap, OutLiveVals),
|
|
|
|
% make the call
|
|
code_info__get_module_info(ModuleInfo),
|
|
code_info__make_entry_label(ModuleInfo, PredId, ModeId, yes, Address),
|
|
code_info__get_next_label(ReturnLabel),
|
|
{ call_gen__call_comment(CodeModel, CallComment) },
|
|
{ CallCode = node([
|
|
call(Address, label(ReturnLabel), OutLiveVals, CallModel)
|
|
- CallComment,
|
|
label(ReturnLabel)
|
|
- "continuation label"
|
|
]) },
|
|
|
|
call_gen__rebuild_registers(ArgsInfos),
|
|
call_gen__handle_failure(CodeModel, FailHandlingCode),
|
|
|
|
{ Code =
|
|
tree(SaveCode,
|
|
tree(FlushCode,
|
|
tree(SetupCode,
|
|
tree(TraceCode,
|
|
tree(LiveCode,
|
|
tree(CallCode,
|
|
FailHandlingCode))))))
|
|
}.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
%
|
|
% for a higher-order call,
|
|
% we split the arguments into inputs and outputs, put the inputs
|
|
% in the locations expected by do_call_<detism>_closure in
|
|
% runtime/mercury_ho_call.c, generate the call to
|
|
% do_call_<detism>_closure, and pick up the outputs from the
|
|
% locations that we know runtime/mercury_ho_call.c leaves them in.
|
|
%
|
|
% lambda.m ensures that procedures which are directly
|
|
% higher-order-called use the compact argument convertion,
|
|
% so that runtime/mercury_ho_call.c doesn't have trouble
|
|
% figuring out which registers the arguments go in.
|
|
%
|
|
|
|
call_gen__generate_higher_order_call(_OuterCodeModel, PredVar, Args, Types,
|
|
Modes, Det, GoalInfo, Code) -->
|
|
{ determinism_to_code_model(Det, CodeModel) },
|
|
code_info__get_module_info(ModuleInfo),
|
|
{ module_info_globals(ModuleInfo, Globals) },
|
|
{ arg_info__ho_call_args_method(Globals, ArgsMethod) },
|
|
{ make_arg_infos(ArgsMethod, Types, Modes, CodeModel, ModuleInfo,
|
|
ArgInfos) },
|
|
{ assoc_list__from_corresponding_lists(Args, ArgInfos, ArgsInfos) },
|
|
{ call_gen__partition_args(ArgsInfos, InVars, OutVars) },
|
|
code_info__succip_is_used,
|
|
{ set__list_to_set(OutVars, OutArgs) },
|
|
call_gen__save_variables(OutArgs, SaveCode),
|
|
|
|
call_gen__prepare_for_call(CodeModel, FlushCode, CallModel,
|
|
RuntimeAddr),
|
|
|
|
% place the immediate input arguments in registers
|
|
% starting at r4.
|
|
call_gen__generate_immediate_args(InVars, 4, InLocs, ImmediateCode),
|
|
code_info__generate_stack_livevals(OutArgs, LiveVals0),
|
|
{ set__insert_list(LiveVals0,
|
|
[reg(r, 1), reg(r, 2), reg(r, 3) | InLocs], LiveVals) },
|
|
(
|
|
{ CodeModel = model_semi }
|
|
->
|
|
{ FirstArg = 2 }
|
|
;
|
|
{ FirstArg = 1 }
|
|
),
|
|
|
|
{ call_gen__outvars_to_outargs(OutVars, FirstArg, OutArguments) },
|
|
{ call_gen__output_arg_locs(OutArguments, OutLocs) },
|
|
code_info__get_instmap(InstMap),
|
|
{ goal_info_get_instmap_delta(GoalInfo, InstMapDelta) },
|
|
{ instmap__apply_instmap_delta(InstMap, InstMapDelta,
|
|
AfterCallInstMap) },
|
|
call_gen__generate_return_livevals(OutArgs, OutLocs, AfterCallInstMap,
|
|
OutLiveVals),
|
|
|
|
code_info__produce_variable(PredVar, PredVarCode, PredRVal),
|
|
(
|
|
{ PredRVal = lval(reg(r, 1)) }
|
|
->
|
|
{ CopyCode = empty }
|
|
;
|
|
{ CopyCode = node([
|
|
assign(reg(r, 1), PredRVal) - "Copy pred-term"
|
|
]) }
|
|
),
|
|
|
|
{ list__length(InVars, NInVars) },
|
|
{ list__length(OutVars, NOutVars) },
|
|
{ ArgNumCode = node([
|
|
assign(reg(r, 2), const(int_const(NInVars))) -
|
|
"Assign number of immediate input arguments",
|
|
assign(reg(r, 3), const(int_const(NOutVars))) -
|
|
"Assign number of output arguments"
|
|
]) },
|
|
|
|
code_info__get_maybe_trace_info(MaybeTraceInfo),
|
|
( { MaybeTraceInfo = yes(TraceInfo) } ->
|
|
{ trace__prepare_for_call(TraceInfo, TraceCode) }
|
|
;
|
|
{ TraceCode = empty }
|
|
),
|
|
|
|
code_info__get_next_label(ReturnLabel),
|
|
{ CallCode = node([
|
|
livevals(LiveVals)
|
|
- "",
|
|
call(RuntimeAddr, label(ReturnLabel), OutLiveVals, CallModel)
|
|
- "setup and call higher order pred",
|
|
label(ReturnLabel)
|
|
- "Continuation label"
|
|
]) },
|
|
|
|
call_gen__rebuild_registers(OutArguments),
|
|
call_gen__handle_failure(CodeModel, FailHandlingCode),
|
|
|
|
{ Code =
|
|
tree(SaveCode,
|
|
tree(FlushCode,
|
|
tree(ImmediateCode,
|
|
tree(PredVarCode,
|
|
tree(CopyCode,
|
|
tree(ArgNumCode,
|
|
tree(TraceCode,
|
|
tree(CallCode,
|
|
FailHandlingCode))))))))
|
|
}.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
%
|
|
% for a class method call,
|
|
% we split the arguments into inputs and outputs, put the inputs
|
|
% in the locations expected by do_call_<detism>_class_method in
|
|
% runtime/mercury_ho_call.c, generate the call to
|
|
% do_call_<detism>_class_method, and pick up the outputs from the
|
|
% locations that we know runtime/mercury_ho_call.c leaves them in.
|
|
%
|
|
call_gen__generate_class_method_call(_OuterCodeModel, TCVar, MethodNum, Args,
|
|
Types, Modes, Det, GoalInfo, Code) -->
|
|
{ determinism_to_code_model(Det, InnerCodeModel) },
|
|
code_info__get_globals(Globals),
|
|
code_info__get_module_info(ModuleInfo),
|
|
|
|
{ globals__get_args_method(Globals, ArgsMethod) },
|
|
( { ArgsMethod = compact } ->
|
|
[]
|
|
;
|
|
{ error("Sorry, typeclasses with simple args_method not yet implemented") }
|
|
),
|
|
{ make_arg_infos(ArgsMethod, Types, Modes, InnerCodeModel, ModuleInfo,
|
|
ArgInfo) },
|
|
{ assoc_list__from_corresponding_lists(Args, ArgInfo, ArgsAndArgInfo) },
|
|
{ call_gen__partition_args(ArgsAndArgInfo, InVars, OutVars) },
|
|
call_gen__generate_class_method_call_2(InnerCodeModel, TCVar,
|
|
MethodNum, InVars, OutVars, GoalInfo, Code).
|
|
|
|
:- pred call_gen__generate_class_method_call_2(code_model, var, int, list(var),
|
|
list(var), hlds_goal_info, code_tree, code_info, code_info).
|
|
:- mode call_gen__generate_class_method_call_2(in, in, in, in, in, in, out, in,
|
|
out) is det.
|
|
|
|
call_gen__generate_class_method_call_2(CodeModel, TCVar, Index,
|
|
InVars, OutVars, GoalInfo, Code) -->
|
|
code_info__succip_is_used,
|
|
{ set__list_to_set(OutVars, OutArgs) },
|
|
call_gen__save_variables(OutArgs, SaveCode),
|
|
(
|
|
{ CodeModel = model_det },
|
|
{ CallModel = det },
|
|
{ RuntimeAddr = do_det_class_method },
|
|
{ FlushCode = empty }
|
|
;
|
|
{ CodeModel = model_semi },
|
|
{ CallModel = semidet },
|
|
{ RuntimeAddr = do_semidet_class_method },
|
|
{ FlushCode = empty }
|
|
;
|
|
{ CodeModel = model_non },
|
|
code_info__may_use_nondet_tailcall(TailCall),
|
|
{ CallModel = nondet(TailCall) },
|
|
{ RuntimeAddr = do_nondet_class_method },
|
|
code_info__unset_failure_cont(FlushCode)
|
|
),
|
|
% place the immediate input arguments in registers
|
|
% starting at r5.
|
|
call_gen__generate_immediate_args(InVars, 5, InLocs, ImmediateCode),
|
|
code_info__generate_stack_livevals(OutArgs, LiveVals0),
|
|
{ set__insert_list(LiveVals0,
|
|
[reg(r, 1), reg(r, 2), reg(r, 3), reg(r, 4) | InLocs],
|
|
LiveVals) },
|
|
(
|
|
{ CodeModel = model_semi }
|
|
->
|
|
{ FirstArg = 2 }
|
|
;
|
|
{ FirstArg = 1 }
|
|
),
|
|
{ call_gen__outvars_to_outargs(OutVars, FirstArg, OutArguments) },
|
|
{ call_gen__output_arg_locs(OutArguments, OutLocs) },
|
|
|
|
code_info__get_instmap(InstMap),
|
|
{ goal_info_get_instmap_delta(GoalInfo, InstMapDelta) },
|
|
{ instmap__apply_instmap_delta(InstMap, InstMapDelta,
|
|
AfterCallInstMap) },
|
|
|
|
call_gen__generate_return_livevals(OutArgs, OutLocs, AfterCallInstMap,
|
|
OutLiveVals),
|
|
code_info__produce_variable(TCVar, TCVarCode, TCVarRVal),
|
|
(
|
|
{ TCVarRVal = lval(reg(r, 1)) }
|
|
->
|
|
{ CopyCode = empty }
|
|
;
|
|
{ CopyCode = node([
|
|
assign(reg(r, 1), TCVarRVal) - "Copy typeclass info"
|
|
])}
|
|
),
|
|
{ list__length(InVars, NInVars) },
|
|
{ list__length(OutVars, NOutVars) },
|
|
{ SetupCode = tree(CopyCode, node([
|
|
assign(reg(r, 2), const(int_const(Index))) -
|
|
"Index of class method in typeclass info",
|
|
assign(reg(r, 3), const(int_const(NInVars))) -
|
|
"Assign number of immediate input arguments",
|
|
assign(reg(r, 4), const(int_const(NOutVars))) -
|
|
"Assign number of output arguments"
|
|
])
|
|
) },
|
|
code_info__get_next_label(ReturnLabel),
|
|
{ TryCallCode = node([
|
|
livevals(LiveVals) - "",
|
|
call(RuntimeAddr, label(ReturnLabel), OutLiveVals, CallModel)
|
|
- "setup and call class method",
|
|
label(ReturnLabel) - "Continuation label"
|
|
]) },
|
|
call_gen__rebuild_registers(OutArguments),
|
|
(
|
|
{ CodeModel = model_semi }
|
|
->
|
|
code_info__generate_failure(FailCode),
|
|
code_info__get_next_label(ContLab),
|
|
{ TestSuccessCode = node([
|
|
if_val(lval(reg(r, 1)), label(ContLab)) -
|
|
"Test for success"
|
|
]) },
|
|
{ ContLabelCode = node([label(ContLab) - ""]) },
|
|
{ CallCode =
|
|
tree(TryCallCode,
|
|
tree(TestSuccessCode,
|
|
tree(FailCode,
|
|
ContLabelCode))) }
|
|
;
|
|
{ CallCode = TryCallCode }
|
|
),
|
|
{ Code =
|
|
tree(SaveCode,
|
|
tree(FlushCode,
|
|
tree(ImmediateCode,
|
|
tree(TCVarCode,
|
|
tree(SetupCode,
|
|
CallCode)))))
|
|
}.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__prepare_for_call(code_model, code_tree, call_model,
|
|
code_addr, code_info, code_info).
|
|
:- mode call_gen__prepare_for_call(in, out, out, out, in, out) is det.
|
|
|
|
call_gen__prepare_for_call(CodeModel, FlushCode, CallModel, RuntimeAddr) -->
|
|
(
|
|
{ CodeModel = model_det },
|
|
{ CallModel = det },
|
|
{ RuntimeAddr = do_det_closure },
|
|
{ FlushCode = empty }
|
|
;
|
|
{ CodeModel = model_semi },
|
|
{ CallModel = semidet },
|
|
{ RuntimeAddr = do_semidet_closure },
|
|
{ FlushCode = empty }
|
|
;
|
|
{ CodeModel = model_non },
|
|
code_info__may_use_nondet_tailcall(TailCall),
|
|
{ CallModel = nondet(TailCall) },
|
|
{ RuntimeAddr = do_nondet_closure },
|
|
code_info__unset_failure_cont(FlushCode)
|
|
).
|
|
|
|
:- pred call_gen__handle_failure(code_model, code_tree, code_info, code_info).
|
|
:- mode call_gen__handle_failure(in, out, in, out ) is det.
|
|
|
|
call_gen__handle_failure(CodeModel, FailHandlingCode) -->
|
|
( { CodeModel = model_semi } ->
|
|
code_info__generate_failure(FailCode),
|
|
code_info__get_next_label(ContLab),
|
|
{ FailTestCode = node([
|
|
if_val(lval(reg(r, 1)), label(ContLab))
|
|
- "test for success"
|
|
]) },
|
|
{ ContLabelCode = node([label(ContLab) - ""]) },
|
|
{ FailHandlingCode =
|
|
tree(FailTestCode,
|
|
tree(FailCode,
|
|
ContLabelCode))
|
|
}
|
|
;
|
|
{ FailHandlingCode = empty }
|
|
).
|
|
|
|
:- pred call_gen__call_comment(code_model, string).
|
|
:- mode call_gen__call_comment(in, out) is det.
|
|
|
|
call_gen__call_comment(model_det, "branch to det procedure").
|
|
call_gen__call_comment(model_semi, "branch to semidet procedure").
|
|
call_gen__call_comment(model_non, "branch to nondet procedure").
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__save_variables(Args, Code) -->
|
|
code_info__get_known_variables(Variables0),
|
|
{ set__list_to_set(Variables0, Vars0) },
|
|
{ set__difference(Vars0, Args, Vars1) },
|
|
code_info__get_globals(Globals),
|
|
{ globals__lookup_bool_option(Globals, typeinfo_liveness,
|
|
TypeinfoLiveness) },
|
|
(
|
|
{ TypeinfoLiveness = yes }
|
|
->
|
|
code_info__get_proc_info(ProcInfo),
|
|
{ proc_info_get_typeinfo_vars_setwise(ProcInfo, Vars1,
|
|
TypeInfoVars) },
|
|
{ set__union(Vars1, TypeInfoVars, Vars) }
|
|
;
|
|
{ Vars = Vars1 }
|
|
),
|
|
{ set__to_sorted_list(Vars, Variables) },
|
|
call_gen__save_variables_2(Variables, Code).
|
|
|
|
:- pred call_gen__save_variables_2(list(var), code_tree, code_info, code_info).
|
|
:- mode call_gen__save_variables_2(in, out, in, out) is det.
|
|
|
|
call_gen__save_variables_2([], empty) --> [].
|
|
call_gen__save_variables_2([Var | Vars], Code) -->
|
|
code_info__save_variable_on_stack(Var, CodeA),
|
|
call_gen__save_variables_2(Vars, CodeB),
|
|
{ Code = tree(CodeA, CodeB) }.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__rebuild_registers(assoc_list(var, arg_info),
|
|
code_info, code_info).
|
|
:- mode call_gen__rebuild_registers(in, in, out) is det.
|
|
|
|
call_gen__rebuild_registers(Args) -->
|
|
code_info__clear_all_registers,
|
|
call_gen__rebuild_registers_2(Args).
|
|
|
|
:- pred call_gen__rebuild_registers_2(assoc_list(var, arg_info),
|
|
code_info, code_info).
|
|
:- mode call_gen__rebuild_registers_2(in, in, out) is det.
|
|
|
|
call_gen__rebuild_registers_2([]) --> [].
|
|
call_gen__rebuild_registers_2([Var - arg_info(ArgLoc, Mode) | Args]) -->
|
|
(
|
|
{ Mode = top_out }
|
|
->
|
|
{ code_util__arg_loc_to_register(ArgLoc, Register) },
|
|
code_info__set_var_location(Var, Register)
|
|
;
|
|
{ true }
|
|
),
|
|
call_gen__rebuild_registers_2(Args).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__generate_det_builtin(PredId, ProcId, Args, Code) -->
|
|
code_info__get_module_info(ModuleInfo),
|
|
{ predicate_module(ModuleInfo, PredId, ModuleName) },
|
|
{ predicate_name(ModuleInfo, PredId, PredName) },
|
|
(
|
|
{ code_util__translate_builtin(ModuleName, PredName, ProcId,
|
|
Args, no, yes(Var - Rval)) }
|
|
->
|
|
code_info__cache_expression(Var, Rval),
|
|
{ Code = empty }
|
|
;
|
|
{ error("Unknown builtin predicate") }
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__generate_semidet_builtin(PredId, ProcId, Args, Code) -->
|
|
code_info__get_module_info(ModuleInfo),
|
|
{ predicate_module(ModuleInfo, PredId, ModuleName) },
|
|
{ predicate_name(ModuleInfo, PredId, PredName) },
|
|
(
|
|
{ code_util__translate_builtin(ModuleName, PredName, ProcId,
|
|
Args, yes(Rval0), Assign) }
|
|
->
|
|
( { Rval0 = binop(BinOp, X0, Y0) } ->
|
|
call_gen__generate_builtin_arg(X0, X, CodeX),
|
|
call_gen__generate_builtin_arg(Y0, Y, CodeY),
|
|
{ Rval = binop(BinOp, X, Y) },
|
|
{ ArgCode = tree(CodeX, CodeY) }
|
|
; { Rval0 = unop(UnOp, X0) } ->
|
|
call_gen__generate_builtin_arg(X0, X, ArgCode),
|
|
{ Rval = unop(UnOp, X) }
|
|
;
|
|
{ error("Unknown builtin predicate") }
|
|
),
|
|
code_info__fail_if_rval_is_false(Rval, TestCode),
|
|
( { Assign = yes(Var - AssignRval) } ->
|
|
code_info__cache_expression(Var, AssignRval)
|
|
;
|
|
[]
|
|
),
|
|
{ Code = tree(ArgCode, TestCode) }
|
|
;
|
|
{ error("Unknown builtin predicate") }
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__generate_nondet_builtin(_PredId, _ProcId, _Args, _Code) -->
|
|
% there aren't any nondet builtins
|
|
{ error("Unknown nondet builtin predicate") }.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__generate_builtin_arg(rval, rval, code_tree,
|
|
code_info, code_info).
|
|
:- mode call_gen__generate_builtin_arg(in, out, out, in, out) is det.
|
|
|
|
call_gen__generate_builtin_arg(Rval0, Rval, Code) -->
|
|
( { Rval0 = var(Var) } ->
|
|
code_info__produce_variable(Var, Code, Rval)
|
|
;
|
|
{ Rval = Rval0 },
|
|
{ Code = empty }
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__partition_args([], [], []).
|
|
call_gen__partition_args([V - arg_info(_Loc,Mode) | Rest], Ins, Outs) :-
|
|
(
|
|
Mode = top_in
|
|
->
|
|
call_gen__partition_args(Rest, Ins0, Outs),
|
|
Ins = [V | Ins0]
|
|
;
|
|
call_gen__partition_args(Rest, Ins, Outs0),
|
|
Outs = [V | Outs0]
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
/* DEAD CODE
|
|
call_gen__generate_complicated_unify(Var1, Var2, UniMode, CanFail, Code) -->
|
|
{ determinism_components(Det, CanFail, at_most_one) },
|
|
{ determinism_to_code_model(Det, CodeModel) },
|
|
code_info__get_globals(Globals),
|
|
{ globals__get_args_method(Globals, ArgsMethod) },
|
|
{ arg_info__unify_arg_info(ArgsMethod, CodeModel, ArgInfo) },
|
|
{ Arguments = [Var1, Var2] },
|
|
{ assoc_list__from_corresponding_lists(Arguments, ArgInfo, Args) },
|
|
{ call_gen__select_out_args(Args, OutArgs) },
|
|
call_gen__save_variables(OutArgs, CodeA),
|
|
code_info__setup_call(Args, caller, CodeB),
|
|
code_info__get_next_label(ReturnLabel),
|
|
code_info__get_module_info(ModuleInfo),
|
|
code_info__variable_type(Var1, VarType),
|
|
( { type_to_type_id(VarType, VarTypeId, _) } ->
|
|
{ unify_proc__lookup_mode_num(ModuleInfo, VarTypeId, UniMode,
|
|
Det, ModeNum) },
|
|
{ call_gen__input_args(ArgInfo, InputArguments) },
|
|
call_gen__generate_call_livevals(OutArgs, InputArguments,
|
|
CodeC0),
|
|
{ call_gen__output_arg_locs(Args, OutputArguments) },
|
|
call_gen__generate_return_livevals(OutArgs, OutputArguments,
|
|
GoalInfo, OutLiveVals),
|
|
{ code_util__make_uni_label(ModuleInfo, VarTypeId, ModeNum,
|
|
UniLabel) },
|
|
{ Address = imported(UniLabel) },
|
|
/\************
|
|
% Currently we just conservatively assume the address
|
|
% of a unification predicate is imported. For
|
|
% non-standard modes, we could do better, if
|
|
% procs_per_c_function is zero (meaning infinity),
|
|
% or if it is a recursive call.
|
|
% But the code below doesn't work if procs_per_c_function
|
|
% is non-zero and it's not a recursive call.
|
|
{ ModeNum = 0 ->
|
|
Address = imported(UniLabel)
|
|
;
|
|
Address = label(local(UniLabel))
|
|
},
|
|
**************\/
|
|
(
|
|
{ CanFail = can_fail }
|
|
->
|
|
{ CallModel = semidet }
|
|
;
|
|
{ CallModel = det }
|
|
),
|
|
{ CodeC1 = node([
|
|
call(Address, label(ReturnLabel),
|
|
OutLiveVals, CallModel)
|
|
- "branch to out-of-line unification procedure",
|
|
label(ReturnLabel) - "Continuation label"
|
|
]) }
|
|
;
|
|
% `type_to_type_id' failed - the type must be a type variable,
|
|
% i.e. it is a polymorphic unification.
|
|
% However, these sorts of unifications should have been changed
|
|
% into calls to unify/2 by polymorphism.m, so if we encounter
|
|
% any here, it's an internal error.
|
|
{ error("unexpected polymorphic unification") }
|
|
),
|
|
(
|
|
{ CanFail = can_fail }
|
|
->
|
|
code_info__get_next_label(ContLab),
|
|
call_gen__rebuild_registers(Args),
|
|
code_info__generate_failure(FailCode),
|
|
{ CodeD = tree(node([
|
|
if_val(lval(reg(r(1))), label(ContLab)) -
|
|
"Test for success"
|
|
]), tree(FailCode, node([ label(ContLab) - "" ]))) }
|
|
;
|
|
call_gen__rebuild_registers(Args),
|
|
{ CodeD = empty }
|
|
),
|
|
|
|
{ Code = tree(CodeA, tree(CodeB, tree(tree(CodeC0, CodeC1), CodeD))) }.
|
|
*/
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__select_out_args(assoc_list(var, arg_info), set(var)).
|
|
:- mode call_gen__select_out_args(in, out) is det.
|
|
|
|
call_gen__select_out_args([], Out) :-
|
|
set__init(Out).
|
|
call_gen__select_out_args([V - arg_info(_Loc, Mode) | Rest], Out) :-
|
|
call_gen__select_out_args(Rest, Out0),
|
|
(
|
|
Mode = top_out
|
|
->
|
|
set__insert(Out0, V, Out)
|
|
;
|
|
Out = Out0
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__input_args(list(arg_info), list(arg_loc)).
|
|
:- mode call_gen__input_args(in, out) is det.
|
|
|
|
call_gen__input_args([], []).
|
|
call_gen__input_args([arg_info(Loc, Mode) | Args], Vs) :-
|
|
(
|
|
Mode = top_in
|
|
->
|
|
Vs = [Loc |Vs0]
|
|
;
|
|
Vs = Vs0
|
|
),
|
|
call_gen__input_args(Args, Vs0).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
call_gen__input_arg_locs([], []).
|
|
call_gen__input_arg_locs([Var - arg_info(Loc, Mode) | Args], Vs) :-
|
|
(
|
|
Mode = top_in
|
|
->
|
|
Vs = [Var - Loc | Vs0]
|
|
;
|
|
Vs = Vs0
|
|
),
|
|
call_gen__input_arg_locs(Args, Vs0).
|
|
|
|
call_gen__output_arg_locs([], []).
|
|
call_gen__output_arg_locs([Var - arg_info(Loc, Mode) | Args], Vs) :-
|
|
(
|
|
Mode = top_out
|
|
->
|
|
Vs = [Var - Loc | Vs0]
|
|
;
|
|
Vs = Vs0
|
|
),
|
|
call_gen__output_arg_locs(Args, Vs0).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__generate_call_livevals(set(var), list(arg_loc), code_tree,
|
|
code_info, code_info).
|
|
:- mode call_gen__generate_call_livevals(in, in, out, in, out) is det.
|
|
|
|
call_gen__generate_call_livevals(OutArgs, InputArgs, Code) -->
|
|
code_info__generate_stack_livevals(OutArgs, LiveVals0),
|
|
{ call_gen__insert_arg_livevals(InputArgs, LiveVals0, LiveVals) },
|
|
{ Code = node([
|
|
livevals(LiveVals) - ""
|
|
]) }.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__insert_arg_livevals(list(arg_loc),
|
|
set(lval), set(lval)).
|
|
:- mode call_gen__insert_arg_livevals(in, in, out) is det.
|
|
|
|
call_gen__insert_arg_livevals([], LiveVals, LiveVals).
|
|
call_gen__insert_arg_livevals([L | As], LiveVals0, LiveVals) :-
|
|
code_util__arg_loc_to_register(L, R),
|
|
set__insert(LiveVals0, R, LiveVals1),
|
|
call_gen__insert_arg_livevals(As, LiveVals1, LiveVals).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__generate_return_livevals(set(var), list(pair(var, arg_loc)),
|
|
instmap, list(liveinfo), code_info, code_info).
|
|
:- mode call_gen__generate_return_livevals(in, in, in, out, in, out) is det.
|
|
|
|
call_gen__generate_return_livevals(OutArgs, OutputArgs, AfterCallInstMap,
|
|
LiveVals) -->
|
|
code_info__generate_stack_livelvals(OutArgs, AfterCallInstMap,
|
|
LiveVals0),
|
|
code_info__get_globals(Globals),
|
|
{ globals__get_gc_method(Globals, GC_Method) },
|
|
call_gen__insert_arg_livelvals(OutputArgs, GC_Method, AfterCallInstMap,
|
|
LiveVals0, LiveVals).
|
|
|
|
|
|
% Maybe a varlist to type_id list would be a better way to do this...
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__insert_arg_livelvals(list(pair(var, arg_loc)), gc_method,
|
|
instmap, list(liveinfo), list(liveinfo), code_info, code_info).
|
|
:- mode call_gen__insert_arg_livelvals(in, in, in, in, out, in, out) is det.
|
|
|
|
call_gen__insert_arg_livelvals([], _, _, LiveVals, LiveVals) --> [].
|
|
call_gen__insert_arg_livelvals([Var - L | As], GC_Method, AfterCallInstMap,
|
|
LiveVals0, LiveVals) -->
|
|
code_info__get_varset(VarSet),
|
|
{ varset__lookup_name(VarSet, Var, Name) },
|
|
(
|
|
{ GC_Method = accurate }
|
|
->
|
|
{ instmap__lookup_var(AfterCallInstMap, Var, Inst) },
|
|
|
|
code_info__variable_type(Var, Type),
|
|
{ type_util__vars(Type, TypeVars) },
|
|
code_info__find_type_infos(TypeVars, TypeParams),
|
|
{ LiveVal = live_lvalue(R, var(Type, Inst), Name, TypeParams) }
|
|
;
|
|
{ LiveVal = live_lvalue(R, unwanted, Name, []) }
|
|
),
|
|
{ code_util__arg_loc_to_register(L, R) },
|
|
call_gen__insert_arg_livelvals(As, GC_Method, AfterCallInstMap,
|
|
[LiveVal | LiveVals0], LiveVals).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__generate_immediate_args(list(var), int, list(lval), code_tree,
|
|
code_info, code_info).
|
|
:- mode call_gen__generate_immediate_args(in, in, out, out, in, out) is det.
|
|
|
|
call_gen__generate_immediate_args([], _N, [], empty) --> [].
|
|
call_gen__generate_immediate_args([V | Vs], N0, [Lval | Lvals], Code) -->
|
|
{ Lval = reg(r, N0) },
|
|
code_info__place_var(V, Lval, Code0),
|
|
{ N1 is N0 + 1 },
|
|
call_gen__generate_immediate_args(Vs, N1, Lvals, Code1),
|
|
{ Code = tree(Code0, Code1) }.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- pred call_gen__outvars_to_outargs(list(var), int, assoc_list(var,arg_info)).
|
|
:- mode call_gen__outvars_to_outargs(in, in, out) is det.
|
|
|
|
call_gen__outvars_to_outargs([], _N, []).
|
|
call_gen__outvars_to_outargs([V | Vs], N0, [V - Arg | ArgInfos]) :-
|
|
Arg = arg_info(N0, top_out),
|
|
N1 is N0 + 1,
|
|
call_gen__outvars_to_outargs(Vs, N1, ArgInfos).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|