mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-18 07:15:19 +00:00
Estimated hours taken: 32
Branches: main
Major improvements to tabling, of two types. The first is the implementation
of the loopcheck and memo forms of tabling for model_non procedures, and the
second is a start on the implementation of a new method of implementing
minimal model tabling, one that has the potential for a proper fix of the
problem that we currently merely detect with the pneg stack (the detection
is followed by a runtime abort). Since this new method relies on giving each
own generator its own stack, the grade component denoting it is "mmos"
(minimal model own stack). The true name of the existing method is changed
from "mm" to "mmsc" (minimal model stack copy). The grade component "mm"
is now a shorthand for "mmsc"; when the new method works, "mm" will be changed
to be a shorthand for "mmos".
configure.in:
scripts/canonical_grade.sh-subr:
scripts/init_grade_options.sh-subr:
scripts/parse_grade_options.sh-subr:
scripts/final_grade_options.sh-subr:
compiler/options.m:
Handle the new way of handling minimal model grades.
scripts/mgnuc.in:
compiler/compile_target_code.m:
Conform to the changes in minimal model grade options.
compiler/table_gen.m:
Implement the transformations required by the loopcheck and memo
tabling of model_non procedures, and the minimal model own stack
transformation.
The new implementation transformations use foreign_procs with extra
args, since there is no point in implementing them both that way and
with separate calls to library predicates. This required making the
choice of which method to use at the top level of each transformation.
Fix an oversight that hasn't caused problems yet but may in the future:
mark goals wrapping the original goals as not impure for determinism
computations.
compiler/handle_options.m:
Handle the new arrangement of the options for minimal model tabling.
Detect simultaneous calls for both forms of minimal model tabling,
and generate an error message. Allow for more than one error message
generated at once; report them all once rather than separately.
compiler/globals.m:
Add a mechanism to allow a fix a problem detected by the changes
to handle_options: the fact that we currently may generate a usage
message more than once for invocations with more than one error.
compiler/mercury_compile.m:
compiler/make.program_target.m:
compiler/make.util.m:
Use the new mechanism in handle_options to avoid generating duplicate
usage messages.
compiler/error_util.m:
Add a utility predicate for use by handle_options.
compiler/hlds_pred.m:
Allow memo tabling for model_non predicates, and handle own stack
tabling.
compiler/hlds_out.m:
Print information about the modes of the arguments of foreign_procs,
since this is useful in debugging transformations such as tabling
that generate them.
compiler/prog_data.m:
compiler/layout_out.m:
compiler/prog_out.m:
runtime/mercury_stack_layout.h:
Mention the new evaluation method.
compiler/goal_util.m:
Change the predicates for creating calls and foreign_procs to allow
more than one goal feature to be attached to the new goal. table_gen.m
now uses this capability.
compiler/add_heap_ops.m:
compiler/add_trail_ops.m:
compiler/polymorphism.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/typecheck.m:
compiler/unify_proc.m:
Conform to the changes in goal_util.
compiler/code_info.m:
compiler/make_hlds.m:
compiler/modules.m:
compiler/prog_io_pragma.m:
Conform to the new the options controlling minimal model
tabling.
compiler/prog_util.m:
Add a utility predicate for use by table_gen.m.
library/std_util.m:
Conform to the changes in the macros for minimal model tabling grades.
library/table_builtin.m:
Add the types and predicates required by the new transformations.
Delete an obsolete comment.
runtime/mercury_grade.h:
Handle the new minimal model grade component.
runtime/mercury_conf_param.h:
List macros controlling minimal model grades.
runtime/mercury_tabling.[ch]:
Define the types needed by the new transformations,
Implement the performance-critical predicates that need to be
hand-written for memo tabling of model_non predicates.
Add utility predicates for debugging.
runtime/mercury_tabling_preds.h:
Add the implementations of the predicates required by the new
transformations.
runtime/mercury_mm_own_stacks.[ch]:
This new module contains the first draft of the implementation
of the own stack implementation of minimal model tabling.
runtime/mercury_imp.h:
Include the new file if the grade needs it.
runtime/Mmakefile:
Mention the new files, and sort the lists of filenames.
runtime/mercury_tabling_macros.h:
Add a macro for allocating answer blocks without requiring them to be
pointed to directly by trie nodes.
runtime/mercury_minimal_model.[ch]:
The structure type holding answer lists is now in mercury_tabling.h,
since it is now also needed by memo tabling of model_non predicates.
It no longer has a field for an answer num, because while it is ok
to require a separate grade for debugging minimal model tabling,
it is not ok to require a separate grade for debugging memo tabling
of model_non predicates. Instead of printing the answer numbers,
print the answers themselves when we need to identify solutions
for debugging.
Change function names, macro names, error messages etc where this is
useful to distinguish the two kinds of minimal model tabling.
Fix some oversights wrt transient registers.
runtime/mercury_context.[ch]:
runtime/mercury_engine.[ch]:
runtime/mercury_memory.[ch]:
runtime/mercury_wrapper.[ch]:
With own stack tabling, each subgoal has its own context, so record
the identity of the subgoal owning a context in the context itself.
The main computation's context is the exception: it has no owner.
Record not just the main context, but also the contexts of subgoals
in the engine.
Add variables for holding the sizes of the det and nondet stacks
of the contexts of subgoals (which should in general be smaller
than the sizes of the corresponding stacks of the main context),
and initialize them as needed.
Initialize the variables holding the sizes of the gen, cut and pneg
stacks, even in grades where the stacks are not used, for safety.
Fix some out-of-date documentation, and conform to our coding
guidelines.
runtime/mercury_memory_zones.[ch]:
Add a function to test whether a pointer is in a zone, to help
debugging.
runtime/mercury_debug.[ch]:
Add some functions to help debugging in the presence of multiple
contexts, and factor out some common code to help with this.
Delete the obsolete, unused function MR_printdetslot_as_label.
runtime/mercury_context.h:
runtime/mercury_bootstrap.h:
Move a bootstrapping #define from mercury_context.h to
mercury_bootstrap.h.
runtime/mercury_context.h:
runtime/mercury_bootstrap.h:
Move a bootstrapping #define from mercury_context.h to
mercury_bootstrap.h.
runtime/mercury_types.h:
Add some more forward declarations of type names.
runtime/mercury_dlist.[ch]:
Rename a field to avoid assignments that dereference NULL.
runtime/mercury_debug.c:
runtime/mercury_memory.c:
runtime/mercury_ml_expand_body.h:
runtime/mercury_stack_trace.c:
runtime/mercury_stacks.[ch]:
trace/mercury_trace_util.c
Update uses of the macros that control minimal model tabling.
runtime/mercury_stack_trace.c:
Provide a mechanism to allow stack traces to be suppressed entirely.
The intention is that by using this mechanism, by the testing system
won't have to provide separate .exp files for hlc grades, nondebug
LLDS grades and debug LLDS grades, as we do currently. The mechanism
is the environment variable MERCURY_SUPPRESS_STACK_TRACE.
tools/bootcheck:
tools/test_mercury:
Specify MERCURY_SUPPRESS_STACK_TRACE.
trace/mercury_trace.c:
When performing retries across tabled calls, handle memo tabled
model_non predicates, for which the call table tip variable holds
a record with a back pointer to a trie node, instead of the trie node
itself.
trace/mercury_trace_internal.c:
When printing tables, handle memo tabled model_non predicates. Delete
the code now moved to runtime/mercury_tabling.c.
Add functions for printing the data structures for own stack minimal
model tabling.
tests/debugger/print_table.{m,inp,exp}:
Update this test case to also test the printing of tables for
memo tabled model_non predicates.
tests/debugger/retry.{m,inp,exp}:
Update this test case to also test retries across memo tabled
model_non predicates.
tests/tabling/loopcheck_nondet.{m,exp}:
tests/tabling/loopcheck_nondet_non_loop.{m,exp}:
New test cases to test loopcheck tabled model_non predicates.
One test case has a loop to detect, one doesn't.
tests/tabling/memo_non.{m,exp}:
tests/tabling/tc_memo.{m,exp}:
tests/tabling/tc_memo2.{m,exp}:
New test cases to test memo tabled model_non predicates.
One test case has a loop to detect, one has a need for minimal model
tabling to detect, and the third doesn't have either.
tests/tabling/Mmakefile:
Add the new test cases, and reenable the existing tc_loop test case.
Rename some make variables and targets to make them better reflect
their meaning.
tests/tabling/test_mercury:
Conform to the change in the name of the make target.
454 lines
14 KiB
Mathematica
454 lines
14 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 1994-2004 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: ite_gen.m
|
|
%
|
|
% Main authors: conway, fjh, zs.
|
|
%
|
|
% The predicates of this module generate code for if-then-elses, and for
|
|
% negations (which are cut-down versions of if-then-elses, since not(G)
|
|
% is equivalent to (G -> fail ; true)).
|
|
%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module ll_backend__ite_gen.
|
|
|
|
:- interface.
|
|
|
|
:- import_module hlds__code_model.
|
|
:- import_module hlds__hlds_goal.
|
|
:- import_module ll_backend__code_info.
|
|
:- import_module ll_backend__llds.
|
|
|
|
:- pred ite_gen__generate_ite(code_model::in, hlds_goal::in, hlds_goal::in,
|
|
hlds_goal::in, hlds_goal_info::in, code_tree::out,
|
|
code_info::in, code_info::out) is det.
|
|
|
|
:- pred ite_gen__generate_negation(code_model::in, hlds_goal::in,
|
|
hlds_goal_info::in, code_tree::out, code_info::in, code_info::out)
|
|
is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module backend_libs__builtin_ops.
|
|
:- import_module hlds__goal_form.
|
|
:- import_module hlds__hlds_llds.
|
|
:- import_module hlds__instmap.
|
|
:- import_module libs__globals.
|
|
:- import_module libs__options.
|
|
:- import_module libs__tree.
|
|
:- import_module ll_backend__code_gen.
|
|
:- import_module ll_backend__trace.
|
|
:- import_module parse_tree__prog_data.
|
|
|
|
:- import_module bool, string, set, term, list, map, std_util, require.
|
|
|
|
ite_gen__generate_ite(CodeModel, CondGoal0, ThenGoal, ElseGoal, IteGoalInfo,
|
|
Code, !CI) :-
|
|
CondGoal0 = CondExpr - CondInfo0,
|
|
goal_info_get_code_model(CondInfo0, CondCodeModel),
|
|
(
|
|
CodeModel = model_non,
|
|
CondCodeModel \= model_non
|
|
->
|
|
EffCodeModel = model_semi
|
|
;
|
|
EffCodeModel = CodeModel
|
|
),
|
|
|
|
goal_info_get_resume_point(CondInfo0, Resume),
|
|
(
|
|
Resume = resume_point(ResumeVarsPrime, ResumeLocsPrime)
|
|
->
|
|
ResumeVars = ResumeVarsPrime,
|
|
ResumeLocs = ResumeLocsPrime,
|
|
% The pre_goal_update sanity check insists on
|
|
% no_resume_point, to make sure that all resume
|
|
% points have been handled by surrounding code.
|
|
goal_info_set_resume_point(CondInfo0, no_resume_point,
|
|
CondInfo),
|
|
CondGoal = CondExpr - CondInfo
|
|
;
|
|
error("condition of an if-then-else has no resume point")
|
|
),
|
|
|
|
% Make sure that the variables whose values will be needed
|
|
% on backtracking to the else part are materialized into
|
|
% registers or stack slots. Their locations are recorded
|
|
% in ResumeMap.
|
|
code_info__produce_vars(ResumeVars, ResumeMap, FlushCode, !CI),
|
|
|
|
% Maybe save the heap state current before the condition.
|
|
% This is after code_info__produce_vars since code that
|
|
% flushes the cache may allocate memory we must not "recover".
|
|
code_info__get_globals(!.CI, Globals),
|
|
(
|
|
globals__lookup_bool_option(Globals,
|
|
reclaim_heap_on_semidet_failure, yes),
|
|
goal_may_allocate_heap(CondGoal)
|
|
->
|
|
ReclaimHeap = yes
|
|
;
|
|
ReclaimHeap = no
|
|
),
|
|
code_info__maybe_save_hp(ReclaimHeap, SaveHpCode, MaybeHpSlot, !CI),
|
|
|
|
% Maybe save the current trail state before the condition
|
|
globals__lookup_bool_option(Globals, use_trail, UseTrail),
|
|
code_info__maybe_save_ticket(UseTrail, SaveTicketCode,
|
|
MaybeTicketSlot, !CI),
|
|
|
|
code_info__remember_position(!.CI, BranchStart),
|
|
|
|
code_info__prepare_for_ite_hijack(EffCodeModel, HijackInfo,
|
|
PrepareHijackCode, !CI),
|
|
|
|
code_info__make_resume_point(ResumeVars, ResumeLocs, ResumeMap,
|
|
ResumePoint, !CI),
|
|
code_info__effect_resume_point(ResumePoint, EffCodeModel,
|
|
EffectResumeCode, !CI),
|
|
|
|
% Generate the condition
|
|
trace__maybe_generate_internal_event_code(CondGoal, IteGoalInfo,
|
|
CondTraceCode, !CI),
|
|
code_gen__generate_goal(CondCodeModel, CondGoal, CondCode, !CI),
|
|
|
|
code_info__ite_enter_then(HijackInfo, ThenNeckCode, ElseNeckCode, !CI),
|
|
|
|
% Kill again any variables that have become zombies
|
|
code_info__pickup_zombies(Zombies, !CI),
|
|
code_info__make_vars_forward_dead(Zombies, !CI),
|
|
|
|
% Discard hp and prune trail ticket if the condition succeeded
|
|
( CondCodeModel = model_non ->
|
|
% We cannot release the stack slots used for the heap pointer
|
|
% and the trail ticket if the condition can be backtracked
|
|
% into. Nor can we prune the trail ticket that we allocated,
|
|
% since the condition may have allocated other trail tickets
|
|
% since then which have not yet been pruned.
|
|
code_info__maybe_reset_ticket(
|
|
MaybeTicketSlot, solve, ResetTicketCode)
|
|
;
|
|
code_info__maybe_release_hp(MaybeHpSlot, !CI),
|
|
code_info__maybe_reset_prune_and_release_ticket(
|
|
MaybeTicketSlot, commit, ResetTicketCode, !CI)
|
|
),
|
|
|
|
goal_info_get_store_map(IteGoalInfo, StoreMap),
|
|
code_info__get_instmap(!.CI, EndCondInstMap),
|
|
( instmap__is_unreachable(EndCondInstMap) ->
|
|
% If the instmap indicates we cannot reach the then part,
|
|
% do not attempt to generate it (may cause aborts).
|
|
ThenTraceCode = empty,
|
|
ThenCode = empty,
|
|
map__init(EmptyStoreMap),
|
|
code_info__generate_branch_end(EmptyStoreMap, no,
|
|
MaybeEnd0, ThenSaveCode, !CI)
|
|
;
|
|
% Generate the then branch
|
|
trace__maybe_generate_internal_event_code(ThenGoal,
|
|
IteGoalInfo, ThenTraceCode, !CI),
|
|
code_gen__generate_goal(CodeModel, ThenGoal, ThenCode, !CI),
|
|
code_info__generate_branch_end(StoreMap, no,
|
|
MaybeEnd0, ThenSaveCode, !CI)
|
|
),
|
|
|
|
% Generate the entry to the else branch
|
|
code_info__reset_to_position(BranchStart, !CI),
|
|
code_info__generate_resume_point(ResumePoint, ResumeCode, !CI),
|
|
|
|
% Restore the heap pointer and solver state if necessary.
|
|
code_info__maybe_restore_and_release_hp(MaybeHpSlot, RestoreHpCode,
|
|
!CI),
|
|
code_info__maybe_reset_discard_and_release_ticket(
|
|
MaybeTicketSlot, undo, RestoreTicketCode, !CI),
|
|
|
|
% Generate the else branch
|
|
trace__maybe_generate_internal_event_code(ElseGoal, IteGoalInfo,
|
|
ElseTraceCode, !CI),
|
|
code_gen__generate_goal(CodeModel, ElseGoal, ElseCode, !CI),
|
|
code_info__generate_branch_end(StoreMap, MaybeEnd0, MaybeEnd,
|
|
ElseSaveCode, !CI),
|
|
|
|
code_info__get_next_label(EndLabel, !CI),
|
|
JumpToEndCode = node([
|
|
goto(label(EndLabel))
|
|
- "Jump to the end of if-then-else"
|
|
]),
|
|
EndLabelCode = node([
|
|
label(EndLabel)
|
|
- "end of if-then-else"
|
|
]),
|
|
make_pneg_context_wrappers(Globals, PNegCondCode, PNegThenCode,
|
|
PNegElseCode),
|
|
Code =
|
|
tree(FlushCode,
|
|
tree(SaveHpCode,
|
|
tree(SaveTicketCode,
|
|
tree(PrepareHijackCode,
|
|
tree(EffectResumeCode,
|
|
tree(CondTraceCode,
|
|
tree(PNegCondCode,
|
|
tree(CondCode,
|
|
tree(ThenNeckCode,
|
|
tree(ResetTicketCode,
|
|
tree(ThenTraceCode,
|
|
tree(PNegThenCode,
|
|
tree(ThenCode,
|
|
tree(ThenSaveCode,
|
|
tree(JumpToEndCode,
|
|
tree(ResumeCode,
|
|
tree(ElseNeckCode,
|
|
tree(RestoreHpCode,
|
|
tree(RestoreTicketCode,
|
|
tree(ElseTraceCode,
|
|
tree(PNegElseCode,
|
|
tree(ElseCode,
|
|
tree(ElseSaveCode,
|
|
EndLabelCode))))))))))))))))))))))),
|
|
code_info__after_all_branches(StoreMap, MaybeEnd, !CI).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
ite_gen__generate_negation(CodeModel, Goal0, NotGoalInfo, Code, !CI) :-
|
|
( CodeModel = model_non ->
|
|
error("nondet negation")
|
|
;
|
|
true
|
|
),
|
|
|
|
Goal0 = GoalExpr - GoalInfo0,
|
|
goal_info_get_resume_point(GoalInfo0, Resume),
|
|
( Resume = resume_point(ResumeVarsPrime, ResumeLocsPrime) ->
|
|
ResumeVars = ResumeVarsPrime,
|
|
ResumeLocs = ResumeLocsPrime,
|
|
goal_info_set_resume_point(GoalInfo0, no_resume_point,
|
|
GoalInfo),
|
|
Goal = GoalExpr - GoalInfo
|
|
;
|
|
error("negated goal has no resume point")
|
|
),
|
|
|
|
% For a negated simple test, we can generate better code
|
|
% than the general mechanism, because we don't have to
|
|
% flush the cache.
|
|
(
|
|
CodeModel = model_semi,
|
|
GoalExpr = unify(_, _, _, simple_test(L, R), _),
|
|
code_info__failure_is_direct_branch(!.CI, CodeAddr),
|
|
code_info__get_globals(!.CI, Globals),
|
|
globals__lookup_bool_option(Globals, simple_neg, yes)
|
|
->
|
|
% Because we are generating the negated goal ourselves,
|
|
% we need to apply the pre- and post-goal updates
|
|
% that would normally be applied by
|
|
% code_gen__generate_goal.
|
|
|
|
code_info__enter_simple_neg(ResumeVars, GoalInfo, SimpleNeg,
|
|
!CI),
|
|
code_info__produce_variable(L, CodeL, ValL, !CI),
|
|
code_info__produce_variable(R, CodeR, ValR, !CI),
|
|
Type = code_info__variable_type(!.CI, L),
|
|
( Type = term__functor(term__atom("string"), [], _) ->
|
|
Op = str_eq
|
|
; Type = term__functor(term__atom("float"), [], _) ->
|
|
Op = float_eq
|
|
;
|
|
Op = eq
|
|
),
|
|
TestCode = node([
|
|
if_val(binop(Op, ValL, ValR), CodeAddr) -
|
|
"test inequality"
|
|
]),
|
|
code_info__leave_simple_neg(GoalInfo, SimpleNeg, !CI),
|
|
Code = tree(tree(CodeL, CodeR), TestCode)
|
|
;
|
|
generate_negation_general(CodeModel, Goal, NotGoalInfo,
|
|
ResumeVars, ResumeLocs, Code, !CI)
|
|
).
|
|
|
|
% The code of generate_negation_general is a cut-down version
|
|
% of the code for if-then-elses.
|
|
|
|
:- pred generate_negation_general(code_model::in, hlds_goal::in,
|
|
hlds_goal_info::in, set(prog_var)::in, resume_locs::in, code_tree::out,
|
|
code_info::in, code_info::out) is det.
|
|
|
|
generate_negation_general(CodeModel, Goal, NotGoalInfo, ResumeVars, ResumeLocs,
|
|
Code, !CI) :-
|
|
|
|
code_info__produce_vars(ResumeVars, ResumeMap, FlushCode, !CI),
|
|
|
|
% Maybe save the heap state current before the condition;
|
|
% this ought to be after we make the failure continuation
|
|
% because that causes the cache to get flushed
|
|
code_info__get_globals(!.CI, Globals),
|
|
(
|
|
globals__lookup_bool_option(Globals,
|
|
reclaim_heap_on_semidet_failure, yes),
|
|
goal_may_allocate_heap(Goal)
|
|
->
|
|
ReclaimHeap = yes
|
|
;
|
|
ReclaimHeap = no
|
|
),
|
|
code_info__maybe_save_hp(ReclaimHeap, SaveHpCode, MaybeHpSlot, !CI),
|
|
|
|
globals__lookup_bool_option(Globals, use_trail, UseTrail),
|
|
code_info__maybe_save_ticket(UseTrail, SaveTicketCode,
|
|
MaybeTicketSlot, !CI),
|
|
|
|
code_info__prepare_for_ite_hijack(CodeModel, HijackInfo,
|
|
PrepareHijackCode, !CI),
|
|
|
|
code_info__make_resume_point(ResumeVars, ResumeLocs, ResumeMap,
|
|
ResumePoint, !CI),
|
|
code_info__effect_resume_point(ResumePoint, CodeModel,
|
|
EffectResumeCode, !CI),
|
|
|
|
% Generate the negated goal as a semi-deterministic goal;
|
|
% it cannot be nondet, since mode correctness requires it
|
|
% to have no output vars.
|
|
trace__maybe_generate_internal_event_code(Goal, NotGoalInfo,
|
|
EnterTraceCode, !CI),
|
|
code_gen__generate_goal(model_semi, Goal, GoalCode, !CI),
|
|
|
|
code_info__ite_enter_then(HijackInfo, ThenNeckCode, ElseNeckCode, !CI),
|
|
|
|
% Kill again any variables that have become zombies
|
|
code_info__pickup_zombies(Zombies, !CI),
|
|
code_info__make_vars_forward_dead(Zombies, !CI),
|
|
|
|
code_info__get_forward_live_vars(!.CI, LiveVars),
|
|
|
|
( CodeModel = model_det ->
|
|
% the then branch will never be reached
|
|
PruneTicketCode = empty,
|
|
FailTraceCode = empty,
|
|
FailCode = empty
|
|
;
|
|
code_info__remember_position(!.CI, AfterNegatedGoal),
|
|
% The call to reset_ticket(..., commit) here is necessary
|
|
% in order to properly detect floundering.
|
|
code_info__maybe_release_hp(MaybeHpSlot, !CI),
|
|
code_info__maybe_reset_prune_and_release_ticket(
|
|
MaybeTicketSlot, commit, PruneTicketCode, !CI),
|
|
trace__maybe_generate_negated_event_code(Goal, NotGoalInfo,
|
|
neg_failure, FailTraceCode, !CI),
|
|
code_info__generate_failure(FailCode, !CI),
|
|
% We want liveness after not(G) to be the same as
|
|
% after G. Information about what variables are where
|
|
% will be set by code_info__generate_resume_point.
|
|
code_info__reset_to_position(AfterNegatedGoal, !CI)
|
|
),
|
|
|
|
% Generate the entry to the else branch
|
|
code_info__generate_resume_point(ResumePoint, ResumeCode, !CI),
|
|
|
|
code_info__set_forward_live_vars(LiveVars, !CI),
|
|
|
|
% Restore the heap pointer and solver state if necessary.
|
|
code_info__maybe_restore_and_release_hp(MaybeHpSlot, RestoreHpCode,
|
|
!CI),
|
|
code_info__maybe_reset_discard_and_release_ticket(
|
|
MaybeTicketSlot, undo, RestoreTicketCode, !CI),
|
|
trace__maybe_generate_negated_event_code(Goal, NotGoalInfo,
|
|
neg_success, SuccessTraceCode, !CI),
|
|
|
|
make_pneg_context_wrappers(Globals, PNegCondCode, PNegThenCode,
|
|
PNegElseCode),
|
|
Code =
|
|
tree(FlushCode,
|
|
tree(PrepareHijackCode,
|
|
tree(EffectResumeCode,
|
|
tree(SaveHpCode,
|
|
tree(SaveTicketCode,
|
|
tree(EnterTraceCode,
|
|
tree(PNegCondCode,
|
|
tree(GoalCode,
|
|
tree(ThenNeckCode,
|
|
tree(PruneTicketCode,
|
|
tree(FailTraceCode,
|
|
tree(PNegThenCode,
|
|
tree(FailCode,
|
|
tree(ResumeCode,
|
|
tree(ElseNeckCode,
|
|
tree(RestoreTicketCode,
|
|
tree(RestoreHpCode,
|
|
tree(SuccessTraceCode,
|
|
PNegElseCode)))))))))))))))))).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
% If the code in the condition depends on a consumer
|
|
% of a generator that is not complete by the time we finish
|
|
% executing the condition, then failure out of the condition
|
|
% does not necessarily mean that the condition has no solution;
|
|
% it may mean simply that the condition's solution depends on
|
|
% a generator solution that hasn't been produced yet and thus
|
|
% hasn't been given to the consumer yet.
|
|
%
|
|
% Detecting such situations requires knowing whether tabled
|
|
% subgoals (both generators and consumers) are started inside
|
|
% possibly negated contexts or not, which is why we wrap the
|
|
% condition inside MR_pneg_enter_{cond,then,exit}.
|
|
|
|
:- pred make_pneg_context_wrappers(globals::in, code_tree::out, code_tree::out,
|
|
code_tree::out) is det.
|
|
|
|
make_pneg_context_wrappers(Globals, PNegCondCode, PNegThenCode, PNegElseCode)
|
|
:-
|
|
globals__lookup_bool_option(Globals, use_minimal_model_stack_copy,
|
|
UseMinimalModelStackCopy),
|
|
(
|
|
UseMinimalModelStackCopy = yes,
|
|
|
|
PNegCondComponents = [
|
|
pragma_c_raw_code(
|
|
wrap_transient("\t\tMR_pneg_enter_cond();\n"),
|
|
live_lvals_info(set__init))
|
|
],
|
|
PNegThenComponents = [
|
|
pragma_c_raw_code(
|
|
wrap_transient("\t\tMR_pneg_enter_then();\n"),
|
|
live_lvals_info(set__init))
|
|
],
|
|
PNegElseComponents = [
|
|
pragma_c_raw_code(
|
|
wrap_transient("\t\tMR_pneg_enter_else();\n"),
|
|
live_lvals_info(set__init))
|
|
],
|
|
PNegCondCode = node([
|
|
pragma_c([], PNegCondComponents, will_not_call_mercury,
|
|
no, no, no, no, yes, yes) - ""
|
|
]),
|
|
PNegThenCode = node([
|
|
pragma_c([], PNegThenComponents, will_not_call_mercury,
|
|
no, no, no, no, yes, yes) - ""
|
|
]),
|
|
PNegElseCode = node([
|
|
pragma_c([], PNegElseComponents, will_not_call_mercury,
|
|
no, no, no, no, yes, yes) - ""
|
|
])
|
|
;
|
|
UseMinimalModelStackCopy = no,
|
|
PNegCondCode = empty,
|
|
PNegThenCode = empty,
|
|
PNegElseCode = empty
|
|
).
|
|
|
|
:- func wrap_transient(string) = string.
|
|
|
|
wrap_transient(Code) =
|
|
string__append_list([
|
|
"\t\tMR_save_transient_registers();\n",
|
|
Code,
|
|
"\t\tMR_restore_transient_registers();\n"]).
|
|
|
|
%---------------------------------------------------------------------------%
|