mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 22:35:41 +00:00
Estimated hours taken: 500 Branches: main Implement deep profiling; merge the changes on the deep2 branch back onto the trunk. The main documentation on the general architecture of the deep profiler is the deep profiling paper. doc/user_guide.texi: Document how to use the deep profiler. deep_profiler: deep_profiler/Mmakefile: A new directory holding the deep profiler and its mmakefile. Mmakefile: Add targets for the new directory. Add support for removing inappropriate files from directories. deep_profiler/interface.m: The deep profiler consists of two programs: mdprof_cgi.m, which acts as a CGI "script", and mdprof_server.m, which implements the server process that the CGI script talks to. Interface.m defines the interface between them. script/mdprof.in: A shell script template. ../configure uses it to generate mdprof, which is a wrapper around mdprof_cgi that tells it how to find mdprof_server. deep_profiler/mdprof_cgi.m: The CGI "script" program. deep_profiler/mdprof_server.m: The top level predicates of the server. deep_profiler/profile.m: The main data structures of the server and their operations. deep_profiler/read_profile.m: Code for reading in profiling data files. deep_profiler/startup.m: Code for post-processing the information in profiling data files, propagating costs from procedures to their ancestors and performing various kinds of summaries. deep_profiler/server.m: Code for responding to requests from the CGI script. deep_profiler/cliques.m: Code to find cliques in graphs. deep_profiler/array_util.m: deep_profiler/util.m: Utility predicates. deep_profiler/dense_bitset.m: An implementation of (part of) the set ADT with dense bit vectors. deep_profiler/measurements.m: Operations on profiling measurements. deep_profiler/timeout.m: An implementation of a timeout facility. deep_profiler/conf.m: Functions that depend on autoconfigured settings. configure.in: Find out what command to use to find the name of the local host. Install deep profiling versions of the standard library along with the other profiling versions. runtime/mercury_conf.h.in: Add some macros for deep_profiler/conf.m to use. library/profiling_builtin.m: runtime/mercury_deep_call_port_body.h: runtime/mercury_deep_leave_port_body.h: runtime/mercury_deep_redo_port_body.h: A new library module that implements deep profiling primitives. Some of these primitives have many versions, whose common code is factor is factored out in three new include files in the runtime. compiler/deep_profiling.m: New module to perform the program transformations described in the paper. compiler/notes/compiler_design.html: Document the new compiler module. compiler/mercury_compiler.m: Invoke the new module in deep profiling grades. Allow global static data to be generated by deep_profiling.m. compiler/options.m: Add options to turn on deep profiling and (for benchmarking purposes) control its implementation. Add an optiooption disable tailcall optimization in the LLDS backend, to help benchmarking deep profiling. compiler/jumpopt.m: compiler/optimize.m: Obey the option to disable tailcalls. compiler/handle_options.m: Handle the implications of deep profiling. compiler/modules.m: In deep profiling grades, automatically import profiling_builtin.m. compiler/prog_util.m: doc/Makefile: library/library.m: Handle the new builtin module. compiler/export.m: In deep profiling grades, wrap deep profiling code around exported procedures to handle the "unscheduled call" aspects of callbacks to Mercury from the foreign language. compiler/higher_order.m: profiler/demangle.m: util/demangle.c: When creating a name for a higher-order-specialized predicate, include the mode number in the name. compiler/add_trail_ops.m: compiler/type_util.m: Move c_pointer_type from add_trail_ops to type_util, so it can also be used by deep_profiling.m. compiler/hlds_goal.m: Add a new goal feature that marks a tail call, for use by deep_profiling.m. compiler/hlds_pred.m: Add a new field to proc_info structures for use by deep_profiling.m. Add a mechanism for getting proc_ids for procedure clones. Remove next_proc_id, an obsolete and unused predicate. compiler/hlds_data.m: Add a new cons_id to refer to the proc_static structure of a procedure. compiler/bytecode_gen.m: compiler/code_util.m: compiler/dependency_graph.m: compiler/hlds_out.m: compiler/mercury_to_mercury.m: compiler/ml_unify_gen.m: compiler/opt_debug.m: compiler/prog_rep.m: compiler/rl_exprn.m: compiler/switch_util.m: compiler/unify_gen.m: Trivial changes to handle the new cons_id, goal feature and/or proc_info argument. compiler/rtti.m: Add a utility predicate for extracting pred_id and proc_id from an rtti_proc_label, for use by hlds_out.m compiler/layout.m: compiler/layout_out.m: compiler/llds.m: compiler/llds_common.m: Add support for proc_static and call_site_static structures. compiler/layout_out.m: compiler/llds_out.m: Add code for the output of proc_static structures. compiler/code_util.m: Make code_util__make_proc_label_from_rtti a function, and export it. util/mkinit.c: compiler/llds_out.m: compiler/layout.m: compiler/modules.m: Add support for a fourth per-module C function, for writing out proc_static structures (and the call_site_static structures they contains). Since proc_static structures can be referred to from LLDS code (and not just from other static structures and compiler-generated C code), reorganize the declarations of static structures slightly. Change the schema for the name of the first per-module C function slightly, to make it the addition of the fourth function easier. The scheme now is: mercury__<modulename>__init mercury__<modulename>__init_type_tables mercury__<modulename>__init_debugger mercury__<modulename>__write_out_proc_statics Improve formatting of the generated C code. library/*.m: runtime/mercury.c: runtime/mercury_context.c: runtime/mercury_engine.c: runtime/mercury_ho_call.c: runtime/mercury_tabling.c: runtime/mercury_trace_base.c: runtime/mercury_wrapper.c: trace/mercrury_trace.[ch]: trace/mercrury_trace_declarative.c: trace/mercrury_trace_external.c: trace/mercrury_trace_internal.c: Conform to the new scheme for initialization functions for hand-written modules. compiler/mercury_compile.m: library/benchmarking.m: runtime/mercury_conf_param.h: runtime/mercury.h: runtime/mercury_engine.c: runtime/mercury_goto.c: runtime/mercury_grade.h: runtime/mercury_ho_call.c: runtime/mercury_label.[ch]: runtime/mercury_prof.[ch]: Add an MR_MPROF_ prefix in front of the C macros used to control the old profiler. compiler/handle_options.m: runtime/mercury_grade.h: scripts/canonical_grade.sh-subr: scripts/init_grade_options.sh-subr: scripts/parse_grade_options.sh-subr: Make deep profiling completely separate from the old profiling system, by making the deep profiling grade independent of MR_MPROF_PROFILE_TIME and the compiler option --profile-time. library/array.m: library/builtin.m: library/std_util.m: runtime/mercury_hand_unify_body.h: runtime/mercury_hand_compare_body.h: In deep profiling grades, wrap the deep profiling call, exit, fail and redo codes around the bodies of hand-written unification and comparison procedures. Make the reporting of array bounds violations switchable between making them fatal errors, as we currently, and reporting them by throwing an exception. Throwing an exception makes debugging code using arrays easier, but since exceptions aren't (yet) propagated across engine boundaries, we keep the old behaviour as the default; the new behaviour is for implementors. runtime/mercury_deep_profiling_hand.h: New file that defines macros for use in Mercury predicates whose definition is in hand-written C code. library/exception.m: runtime/mercury_exception_catch_body.h: runtime/mercury_stacks.h: In deep profiling grades, wrap the deep profiling call, exit, fail and redo codes around the bodies of the various modes of builtin_catch. Provide a function that C code can use to throw exceptions. library/benchmarking.m: library/exception.m: library/gc.m: library/std_util.m: runtime/mercury_context.[ch]: runtime/mercury_engine.[ch]: runtime/mercury_debug.c: runtime/mercury_deep_copy.c: runtime/mercury_overflow.h: runtime/mercury_regs.h: runtime/mercury_stacks.h: runtime/mercury_thread.c: runtime/mercury_wrapper.c: Add prefixes to the names of the fields in the engine and context structures, to make code using them easier to understand and modify. runtime/mercury_deep_profiling.[ch]: New module containing support functions for deep profiling and functions for writing out a deep profiling data file at the end of execution. runtime/mercury_debug.[ch]: Add support for debugging deep profiling. Add support for watching the value at a given address. Make the buffered/unbuffered nature of debugging output controllable via the -du option. Print register contents only if -dr is specified. runtime/mercury_goto.h: runtime/mercury_std.h: Use the macros in mercury_std.h instead of defining local variants. runtime/mercury_goto.h: runtime/mercury_stack_layout.h: runtime/mercury_stack_trace.c: runtime/mercury_tabling.c: trace/mercury_trace.c: trace/mercury_trace_declarative.c: trace/mercury_trace_external.c: trace/mercury_trace_vars.c: Standardize some of the macro names with those used in the debugger paper. runtime/mercury_heap.h: Add support for memory profiling with the deep profiler. runtime/mercury_prof.[ch]: runtime/mercury_prof_time.[ch]: Move the functionality that both the old profiler and the deep profiler need into the new module mercury_prof_time. Leave mercury_prof containing stuff that is only relevant to the old profiler. runtime/mercury_prof.[ch]: runtime/mercury_strerror.[ch]: Move the definition of strerror from mercury_prof to its own file. runtime/mercury_wrapper.[ch]: Add support for deep profiling. Add suppory for controlling whether debugging output is buffered or not. Add support for watching the value at a given address. runtime/Mmakefile: Mention all the added files. scripts/mgnuc.in: Add an option for turning on deep profiling. Add options for controlling the details of deep profiling. These are not documented because they are intended only for benchmarking the deep profiler itself, for the paper; they are not for general use. tools/bootcheck: Compile the deep_profiler directory as well as the other directories containing Mercury code. Turn off the creation of deep profiling data files during bootcheck, since all but one of these in each directory will be overwritten anyway. Add support for turning on --keep-objs by default in a workspace. tools/speedtest: Preserve any deep profiling data files created by the tests. trace/mercury_trace.c: Trap attempts to perform retries in deep profiling grades, since they would lead to core dumps otherwise. util/Mmakefile: Avoid compile-time warnings when compiling getopt. tests/*/Mmakefile: tests/*/*/Mmakefile: In deep profiling grades, switch off the tests that test features that don't work with deep profiling, either by design or because the combination hasn't been implemented yet.
517 lines
17 KiB
Mathematica
517 lines
17 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2000-2001 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.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Author: fjh.
|
|
%
|
|
% This module is an HLDS-to-HLDS transformation that inserts code to
|
|
% handle trailing. The transformation involves adding calls to impure
|
|
% predicates defined in library/private_builtin.m, which in turn call
|
|
% macros defined in runtime/mercury_trail.h.
|
|
%
|
|
% This pass is currently only used for the MLDS back-end.
|
|
% For some reason (perhaps efficiency?? or more likely just historical?),
|
|
% the LLDS back-end inserts the trail operations as it is generating
|
|
% LLDS code, rather than via an HLDS to HLDS transformation.
|
|
%
|
|
% See compiler/notes/trailing.html for more information about trailing
|
|
% in the Mercury implementation.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% XXX FIXME check goal_infos for correctness
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module add_trail_ops.
|
|
:- interface.
|
|
:- import_module hlds_pred, hlds_module.
|
|
|
|
:- pred add_trail_ops(proc_info::in, module_info::in, proc_info::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module prog_data, prog_util, (inst).
|
|
:- import_module hlds_goal, hlds_data, quantification, modules, type_util.
|
|
:- import_module code_model, instmap.
|
|
|
|
:- import_module bool, string.
|
|
:- import_module assoc_list, list, map, set, varset, std_util, require, term.
|
|
|
|
|
|
%
|
|
% As we traverse the goal, we add new variables to hold the
|
|
% trail tickets (i.e. saved values of the trail pointer)
|
|
% and the saved values of the trail ticket counter.
|
|
% So we need to thread a varset and a vartypes mapping through,
|
|
% to record the names and types of the new variables.
|
|
%
|
|
% We also keep the module_info around, so that we can use
|
|
% the predicate table that it contains to lookup the pred_ids
|
|
% for the builtin procedures that we insert calls to.
|
|
% We do not update the module_info as we're traversing the goal.
|
|
%
|
|
|
|
:- type trail_ops_info --->
|
|
trail_ops_info(
|
|
varset :: prog_varset,
|
|
var_types :: vartypes,
|
|
module_info :: module_info
|
|
).
|
|
|
|
add_trail_ops(Proc0, ModuleInfo0, Proc) :-
|
|
proc_info_goal(Proc0, Goal0),
|
|
proc_info_varset(Proc0, VarSet0),
|
|
proc_info_vartypes(Proc0, VarTypes0),
|
|
TrailOpsInfo0 = trail_ops_info(VarSet0, VarTypes0, ModuleInfo0),
|
|
goal_add_trail_ops(Goal0, Goal, TrailOpsInfo0, TrailOpsInfo),
|
|
TrailOpsInfo = trail_ops_info(VarSet, VarTypes, _),
|
|
proc_info_set_goal(Proc0, Goal, Proc1),
|
|
proc_info_set_varset(Proc1, VarSet, Proc2),
|
|
proc_info_set_vartypes(Proc2, VarTypes, Proc3),
|
|
% The code below does not maintain the non-local variables,
|
|
% so we need to requantify.
|
|
% XXX it would be more efficient to maintain them
|
|
% rather than recomputing them every time.
|
|
requantify_proc(Proc3, Proc).
|
|
|
|
:- pred goal_add_trail_ops(hlds_goal::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
goal_add_trail_ops(GoalExpr0 - GoalInfo, Goal) -->
|
|
goal_expr_add_trail_ops(GoalExpr0, GoalInfo, Goal).
|
|
|
|
:- pred goal_expr_add_trail_ops(hlds_goal_expr::in, hlds_goal_info::in,
|
|
hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
goal_expr_add_trail_ops(conj(Goals0), GI, conj(Goals) - GI) -->
|
|
conj_add_trail_ops(Goals0, Goals).
|
|
|
|
goal_expr_add_trail_ops(par_conj(Goals0, SM), GI, par_conj(Goals, SM) - GI) -->
|
|
conj_add_trail_ops(Goals0, Goals).
|
|
|
|
goal_expr_add_trail_ops(disj([], B), GI, disj([], B) - GI) --> [].
|
|
|
|
goal_expr_add_trail_ops(disj(Goals0, B), GoalInfo, Goal - GoalInfo) -->
|
|
{ Goals0 = [_|_] },
|
|
|
|
{ goal_info_get_context(GoalInfo, Context) },
|
|
{ goal_info_get_code_model(GoalInfo, CodeModel) },
|
|
|
|
%
|
|
% Allocate a new trail ticket so that we can
|
|
% restore things on back-tracking
|
|
%
|
|
new_ticket_var(TicketVar),
|
|
gen_store_ticket(TicketVar, Context, StoreTicketGoal),
|
|
disj_add_trail_ops(Goals0, yes, CodeModel, TicketVar, Goals),
|
|
{ Goal = conj([StoreTicketGoal, disj(Goals, B) - GoalInfo]) }.
|
|
|
|
goal_expr_add_trail_ops(switch(A, B, Cases0, D), GI,
|
|
switch(A, B, Cases, D) - GI) -->
|
|
cases_add_trail_ops(Cases0, Cases).
|
|
|
|
goal_expr_add_trail_ops(not(InnerGoal), OuterGoalInfo, Goal) -->
|
|
%
|
|
% We handle negations by converting them into if-then-elses:
|
|
% not(G) ===> (if G then fail else true)
|
|
%
|
|
{ goal_info_get_context(OuterGoalInfo, Context) },
|
|
{ InnerGoal = _ - InnerGoalInfo },
|
|
{ goal_info_get_determinism(InnerGoalInfo, Determinism) },
|
|
{ determinism_components(Determinism, _CanFail, NumSolns) },
|
|
{ true_goal(Context, True) },
|
|
{ fail_goal(Context, Fail) },
|
|
{ map__init(SM) },
|
|
{ NumSolns = at_most_zero ->
|
|
% The "then" part of the if-then-else will be unreachable,
|
|
% but to preserve the invariants that the MLDS back-end
|
|
% relies on, we need to make sure that it can't fail.
|
|
% So we use `true' rather than `fail' for the "then" part.
|
|
NewOuterGoal = if_then_else([], InnerGoal, True, True, SM)
|
|
;
|
|
NewOuterGoal = if_then_else([], InnerGoal, Fail, True, SM)
|
|
},
|
|
goal_expr_add_trail_ops(NewOuterGoal, OuterGoalInfo, Goal).
|
|
|
|
goal_expr_add_trail_ops(some(A, B, Goal0), OuterGoalInfo,
|
|
Goal - OuterGoalInfo) -->
|
|
{ Goal0 = _ - InnerGoalInfo },
|
|
{ goal_info_get_code_model(InnerGoalInfo, InnerCodeModel) },
|
|
{ goal_info_get_code_model(OuterGoalInfo, OuterCodeModel) },
|
|
(
|
|
{ InnerCodeModel = model_non },
|
|
{ OuterCodeModel \= model_non }
|
|
->
|
|
% handle commits
|
|
|
|
%
|
|
% before executing the goal, we save the ticket counter,
|
|
% and allocate a new trail ticket
|
|
%
|
|
{ goal_info_get_context(OuterGoalInfo, Context) },
|
|
new_ticket_counter_var(SavedTicketCounterVar),
|
|
new_ticket_var(TicketVar),
|
|
gen_mark_ticket_stack(SavedTicketCounterVar, Context,
|
|
MarkTicketStackGoal),
|
|
gen_store_ticket(TicketVar, Context, StoreTicketGoal),
|
|
%
|
|
% next we execute the goal that we're committing across
|
|
%
|
|
goal_add_trail_ops(Goal0, Goal1),
|
|
%
|
|
% if the goal succeeds, then we have committed to that
|
|
% goal, so we need to commit the trail entries
|
|
% and prune any trail tickets that have been allocated
|
|
% since we saved the ticket counter
|
|
%
|
|
gen_reset_ticket_commit(TicketVar, Context,
|
|
ResetTicketCommitGoal),
|
|
gen_prune_tickets_to(SavedTicketCounterVar, Context,
|
|
PruneTicketsToGoal),
|
|
%
|
|
% if the goal fails, then we should undo the trail
|
|
% entries and discard this trail ticket before
|
|
% backtracking over it
|
|
%
|
|
gen_reset_ticket_undo(TicketVar, Context,
|
|
ResetTicketUndoGoal),
|
|
gen_discard_ticket(Context, DiscardTicketGoal),
|
|
{ fail_goal(Context, FailGoal) },
|
|
|
|
% put it all together
|
|
{ Goal2 = some(A, B, Goal1) - OuterGoalInfo },
|
|
{ SuccCode = conj([Goal2, ResetTicketCommitGoal,
|
|
PruneTicketsToGoal]) - OuterGoalInfo },
|
|
( { OuterCodeModel = model_semi } ->
|
|
{ FailGoal = _ - FailGoalInfo },
|
|
{ FailCode = conj([ResetTicketUndoGoal,
|
|
DiscardTicketGoal, FailGoal]) - FailGoalInfo },
|
|
{ map__init(SM) },
|
|
{ Goal3 = disj([SuccCode, FailCode], SM) -
|
|
OuterGoalInfo }
|
|
;
|
|
{ Goal3 = SuccCode }
|
|
),
|
|
{ Goal = conj([MarkTicketStackGoal, StoreTicketGoal, Goal3]) }
|
|
;
|
|
goal_add_trail_ops(Goal0, Goal1),
|
|
{ Goal = some(A, B, Goal1) }
|
|
).
|
|
|
|
goal_expr_add_trail_ops(if_then_else(A, Cond0, Then0, Else0, E), GoalInfo,
|
|
Goal - GoalInfo) -->
|
|
goal_add_trail_ops(Cond0, Cond),
|
|
goal_add_trail_ops(Then0, Then1),
|
|
goal_add_trail_ops(Else0, Else1),
|
|
%
|
|
% Allocate a new trail ticket so that we can
|
|
% restore things if the condition fails.
|
|
%
|
|
new_ticket_var(TicketVar),
|
|
{ goal_info_get_context(GoalInfo, Context) },
|
|
gen_store_ticket(TicketVar, Context, StoreTicketGoal),
|
|
|
|
%
|
|
% Commit the trail ticket entries if the condition
|
|
% succeeds.
|
|
%
|
|
{ Then1 = _ - Then1GoalInfo },
|
|
{ Cond = _ - CondGoalInfo },
|
|
{ goal_info_get_code_model(CondGoalInfo, CondCodeModel) },
|
|
( { CondCodeModel = model_non } ->
|
|
gen_reset_ticket_solve(TicketVar, Context,
|
|
ResetTicketSolveGoal),
|
|
{ Then = conj([ResetTicketSolveGoal, Then1]) - Then1GoalInfo }
|
|
;
|
|
gen_reset_ticket_commit(TicketVar, Context,
|
|
ResetTicketCommitGoal),
|
|
gen_prune_ticket(Context, PruneTicketGoal),
|
|
{ Then = conj([ResetTicketCommitGoal, PruneTicketGoal, Then1])
|
|
- Then1GoalInfo }
|
|
),
|
|
gen_reset_ticket_undo(TicketVar, Context, ResetTicketUndoGoal),
|
|
gen_discard_ticket(Context, DiscardTicketGoal),
|
|
{ Else1 = _ - Else1GoalInfo },
|
|
{ Else = conj([ResetTicketUndoGoal, DiscardTicketGoal, Else1])
|
|
- Else1GoalInfo },
|
|
{ IfThenElse = if_then_else(A, Cond, Then, Else, E) - GoalInfo },
|
|
{ Goal = conj([StoreTicketGoal, IfThenElse]) }.
|
|
|
|
|
|
goal_expr_add_trail_ops(call(A,B,C,D,E,F), GI, call(A,B,C,D,E,F) - GI) --> [].
|
|
|
|
goal_expr_add_trail_ops(generic_call(A,B,C,D), GI, generic_call(A,B,C,D) - GI)
|
|
--> [].
|
|
|
|
goal_expr_add_trail_ops(unify(A,B,C,D,E), GI, unify(A,B,C,D,E) - GI) --> [].
|
|
|
|
goal_expr_add_trail_ops(PragmaForeign, GoalInfo, Goal) -->
|
|
{ PragmaForeign = foreign_proc(_,_,_,_,_,_,Impl) },
|
|
( { Impl = nondet(_,_,_,_,_,_,_,_,_) } ->
|
|
% XXX Implementing trailing for nondet pragma foreign_code
|
|
% via transformation is difficult, because there's nowhere
|
|
% in the HLDS pragma_foreign_code goal where we can insert
|
|
% trailing operations. For now, we don't support this.
|
|
% Instead, we just generate a call to a procedure which
|
|
% will at runtime call error/1 with an appropriate
|
|
% "Sorry, not implemented" error message.
|
|
ModuleInfo =^ module_info,
|
|
{ goal_info_get_context(GoalInfo, Context) },
|
|
{ generate_call("trailed_nondet_pragma_foreign_code",
|
|
[], det, no, [], ModuleInfo, Context,
|
|
SorryNotImplementedCode) },
|
|
{ Goal = SorryNotImplementedCode }
|
|
;
|
|
{ Goal = PragmaForeign - GoalInfo }
|
|
).
|
|
|
|
goal_expr_add_trail_ops(shorthand(_), _, _) -->
|
|
% these should have been expanded out by now
|
|
{ error("goal_expr_add_trail_ops: unexpected shorthand") }.
|
|
|
|
:- pred conj_add_trail_ops(hlds_goals::in, hlds_goals::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
conj_add_trail_ops(Goals0, Goals) -->
|
|
list__map_foldl(goal_add_trail_ops, Goals0, Goals).
|
|
|
|
:- pred disj_add_trail_ops(hlds_goals::in, bool::in, code_model::in,
|
|
prog_var::in, hlds_goals::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
disj_add_trail_ops([], _, _, _, []) --> [].
|
|
disj_add_trail_ops([Goal0 | Goals0], IsFirstBranch, CodeModel, TicketVar,
|
|
[Goal | Goals]) -->
|
|
{ Goal0 = _ - GoalInfo0 },
|
|
{ goal_info_get_context(GoalInfo0, Context) },
|
|
%
|
|
% First undo the effects of any earlier branches
|
|
%
|
|
( { IsFirstBranch = yes } ->
|
|
{ UndoList = [] }
|
|
;
|
|
gen_reset_ticket_undo(TicketVar, Context, ResetTicketUndoGoal),
|
|
( { Goals0 = [] } ->
|
|
%
|
|
% Once we've reached the last disjunct,
|
|
% we can discard the trail ticket
|
|
%
|
|
gen_discard_ticket(Context, DiscardTicketGoal),
|
|
{ UndoList = [ResetTicketUndoGoal, DiscardTicketGoal] }
|
|
;
|
|
{ UndoList = [ResetTicketUndoGoal] }
|
|
)
|
|
),
|
|
%
|
|
% Then execute the disjunct goal
|
|
%
|
|
goal_add_trail_ops(Goal0, Goal1),
|
|
%
|
|
% For model_semi and model_det disjunctions,
|
|
% once we reach the end of the disjunct goal,
|
|
% we're committing to this disjunct, so we need
|
|
% to prune the trail ticket.
|
|
%
|
|
( { CodeModel = model_non } ->
|
|
{ PruneList = [] }
|
|
;
|
|
gen_reset_ticket_commit(TicketVar, Context,
|
|
ResetTicketCommitGoal),
|
|
gen_prune_ticket(Context, PruneTicketGoal),
|
|
{ PruneList = [ResetTicketCommitGoal, PruneTicketGoal] }
|
|
),
|
|
%
|
|
% Package up the stuff we built earlier.
|
|
%
|
|
{ Goal1 = _ - GoalInfo1 },
|
|
{ conj_list_to_goal(UndoList ++ [Goal1] ++ PruneList, GoalInfo1,
|
|
Goal) },
|
|
|
|
% Recursively handle the remaining disjuncts
|
|
disj_add_trail_ops(Goals0, no, CodeModel, TicketVar, Goals).
|
|
|
|
:- pred cases_add_trail_ops(list(case)::in, list(case)::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
cases_add_trail_ops([], []) --> [].
|
|
cases_add_trail_ops([Case0 | Cases0], [Case | Cases]) -->
|
|
{ Case0 = case(ConsId, Goal0) },
|
|
{ Case = case(ConsId, Goal) },
|
|
goal_add_trail_ops(Goal0, Goal),
|
|
cases_add_trail_ops(Cases0, Cases).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred gen_store_ticket(prog_var::in, prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_store_ticket(TicketVar, Context, SaveTicketGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("store_ticket", [TicketVar],
|
|
det, yes(impure),
|
|
[TicketVar - ground_inst],
|
|
ModuleInfo, Context, SaveTicketGoal) }.
|
|
|
|
:- pred gen_reset_ticket_undo(prog_var::in, prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_reset_ticket_undo(TicketVar, Context, ResetTicketGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("reset_ticket_undo", [TicketVar],
|
|
det, yes(impure), [],
|
|
ModuleInfo, Context, ResetTicketGoal) }.
|
|
|
|
:- pred gen_reset_ticket_solve(prog_var::in, prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_reset_ticket_solve(TicketVar, Context, ResetTicketGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("reset_ticket_solve", [TicketVar],
|
|
det, yes(impure), [],
|
|
ModuleInfo, Context, ResetTicketGoal) }.
|
|
|
|
:- pred gen_reset_ticket_commit(prog_var::in, prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_reset_ticket_commit(TicketVar, Context, ResetTicketGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("reset_ticket_commit", [TicketVar],
|
|
det, yes(impure), [],
|
|
ModuleInfo, Context, ResetTicketGoal) }.
|
|
|
|
:- pred gen_prune_ticket(prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_prune_ticket(Context, PruneTicketGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("prune_ticket", [], det, yes(impure), [],
|
|
ModuleInfo, Context, PruneTicketGoal) }.
|
|
|
|
:- pred gen_discard_ticket(prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_discard_ticket(Context, DiscardTicketGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("discard_ticket", [], det, yes(impure), [],
|
|
ModuleInfo, Context, DiscardTicketGoal) }.
|
|
|
|
:- pred gen_mark_ticket_stack(prog_var::in, prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_mark_ticket_stack(SavedTicketCounterVar, Context, MarkTicketStackGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("mark_ticket_stack", [SavedTicketCounterVar],
|
|
det, yes(impure), [],
|
|
ModuleInfo, Context, MarkTicketStackGoal) }.
|
|
|
|
:- pred gen_prune_tickets_to(prog_var::in, prog_context::in, hlds_goal::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
gen_prune_tickets_to(SavedTicketCounterVar, Context, PruneTicketsToGoal) -->
|
|
ModuleInfo =^ module_info,
|
|
{ generate_call("prune_tickets_to", [SavedTicketCounterVar],
|
|
det, yes(impure), [],
|
|
ModuleInfo, Context, PruneTicketsToGoal) }.
|
|
|
|
:- func ground_inst = (inst).
|
|
ground_inst = ground(unique, none).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred new_ticket_var(prog_var::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
new_ticket_var(Var) -->
|
|
new_var("TrailTicket", ticket_type, Var).
|
|
|
|
:- pred new_ticket_counter_var(prog_var::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
new_ticket_counter_var(Var) -->
|
|
new_var("SavedTicketCounter", ticket_counter_type, Var).
|
|
|
|
:- pred new_var(string::in, (type)::in, prog_var::out,
|
|
trail_ops_info::in, trail_ops_info::out) is det.
|
|
|
|
new_var(Name, Type, Var, TOI0, TOI) :-
|
|
VarSet0 = TOI0 ^ varset,
|
|
VarTypes0 = TOI0 ^ var_types,
|
|
varset__new_named_var(VarSet0, Name, Var, VarSet),
|
|
map__det_insert(VarTypes0, Var, Type, VarTypes),
|
|
TOI = ((TOI0 ^ varset := VarSet)
|
|
^ var_types := VarTypes).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- func ticket_type = (type).
|
|
ticket_type = c_pointer_type.
|
|
|
|
:- func ticket_counter_type = (type).
|
|
ticket_counter_type = c_pointer_type.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% XXX copied from table_gen.m
|
|
|
|
:- pred generate_call(string::in, list(prog_var)::in, determinism::in,
|
|
maybe(goal_feature)::in, assoc_list(prog_var, inst)::in,
|
|
module_info::in, term__context::in, hlds_goal::out) is det.
|
|
|
|
generate_call(PredName, Args, Detism, MaybeFeature, InstMap, Module, Context,
|
|
CallGoal) :-
|
|
list__length(Args, Arity),
|
|
mercury_private_builtin_module(BuiltinModule),
|
|
module_info_get_predicate_table(Module, PredTable),
|
|
(
|
|
predicate_table_search_pred_m_n_a(PredTable,
|
|
BuiltinModule, PredName, Arity,
|
|
[PredId0])
|
|
->
|
|
PredId = PredId0
|
|
;
|
|
string__int_to_string(Arity, ArityS),
|
|
string__append_list(["can't locate ", PredName,
|
|
"/", ArityS], ErrorMessage),
|
|
error(ErrorMessage)
|
|
),
|
|
module_info_pred_info(Module, PredId, PredInfo),
|
|
(
|
|
pred_info_procids(PredInfo, [ProcId0])
|
|
->
|
|
ProcId = ProcId0
|
|
;
|
|
string__int_to_string(Arity, ArityS),
|
|
string__append_list(["too many modes for pred ",
|
|
PredName, "/", ArityS], ErrorMessage),
|
|
error(ErrorMessage)
|
|
|
|
),
|
|
Call = call(PredId, ProcId, Args, not_builtin, no,
|
|
qualified(BuiltinModule, PredName)),
|
|
set__init(NonLocals0),
|
|
set__insert_list(NonLocals0, Args, NonLocals),
|
|
determinism_components(Detism, _CanFail, NumSolns),
|
|
(
|
|
NumSolns = at_most_zero
|
|
->
|
|
instmap_delta_init_unreachable(InstMapDelta)
|
|
;
|
|
instmap_delta_from_assoc_list(InstMap, InstMapDelta)
|
|
),
|
|
goal_info_init(NonLocals, InstMapDelta, Detism, CallGoalInfo0),
|
|
goal_info_set_context(CallGoalInfo0, Context, CallGoalInfo1),
|
|
(
|
|
MaybeFeature = yes(Feature),
|
|
goal_info_add_feature(CallGoalInfo1, Feature, CallGoalInfo)
|
|
;
|
|
MaybeFeature = no,
|
|
CallGoalInfo = CallGoalInfo1
|
|
),
|
|
CallGoal = Call - CallGoalInfo.
|
|
|
|
%-----------------------------------------------------------------------------%
|