Files
mercury/compiler/vn_type.m
Zoltan Somogyi d1855187e5 Implement new methods of handling failures and the end points of branched
Estimated hours taken: 260

Implement new methods of handling failures and the end points of branched
control structures.

compiler/notes/failure.html:
	Fix an omission about the handling of resume_is_known in if-then-elses.
	(This omission lead to a bug in the implementation.)

	Optimize cuts across multi goals when curfr is known to be equal
	to maxfr.

	Clarify the wording in several places.

compiler/code_info.m:
	Completely rewrite the methods for handling failure.

	Separate the fields of code_info into three classes: those which
	do not change after initialization, those which record state that
	depends on where in the HLDS goal we are, and those which contain
	persistent data such as label and cell counters.

	Rename grab_code_info and slap_code_info as remember_position
	and reset_to_position, and add a wrapper around the remembered
	code_info to make it harder to make mistakes in its use.
	(Only the location-dependent fields of the remembered code_info
	are used, but putting only them into a separate data structure would
	result in more, not less, memory being allocated.)

	Gather the predicates that deal with handling branched control
	structures into a submodule.

	Reorder the declarations and definitions of access predicates
	to conform to the new order of fields.

	Reorder the declarations and definitions of the failure handling
	submodule to better reflect the separation of higher-level and
	lower-level predicates.

compiler/code_gen.m:
	Replace code_gen__generate_{det,semi,non}_goal_2 with a single
	predicate, since for most HLDS constructs the code here is the same
	anyway (the called preds check the code model when needed).

	Move classification of the various kinds of unifications to unify_gen,
	since that is where it belongs.

	Move responsibility for initializing the code generator's trace
	info to code_info.

	Move the generation of code for negations to ite_gen, since the
	handling of negations is a cut-down version of the handling of
	negations. This should make the required double maintenance easier,
	and more likely to happen.

compiler/disj_gen.m:
compiler/ite_gen.m:
	These are the two modules that handle most failures; they have
	undergone a significant rewrite. As part of this rewrite, factor
	out the remaining common code between model_non and model_{det,semi}
	goals.

compiler/unify_gen.m:
	Move classification of the various kinds of unifications here from
	code_gen. This allows us to keep several previously exported
	predicates private.

compiler/call_gen.m:
	Factor out some code that was common to ordinary calls, higher order
	calls and method calls. Move the common code that checks whether
	we are doing tracing to trace.m.

	Replace call_gen__generate_{det,semi,nondet}_builtin with a single
	predicate.

	Delete the commented out call_gen__generate_complicated_unify,
	since it will never be needed and in any case suffered from
	significant code rot.

compiler/llds.m:
	Change the mkframe instruction so that depending on one of its
	arguments, it can create either ordinary frames, or the cut-down
	frames used by the new failure handling algorithm (they have only
	three fixed fields: prevfr, redoip and redofr).

compiler/llds_out.m:
	Emit a #define MR_USE_REDOFR before including mercury_imp.h, to
	tell the runtime we are using the new failure handling scheme.
	This effectively changes the grade of the compiled module.

	Emit MR_stackvar and MR_framevar instead of detstackvar and framevar.
	This is a step towards cleaning up the name-space, and a step towards
	making both start numbering at 0. For the time being, the compiler
	internally still starts counting framevars at 0; the code in llds_out.m
	adds a +1 offset.

compiler/trace.m:
	Change the way trace info is initialized to fit in with the new
	requirements of code_info.m.

	Move the "are we tracing" check from the callers to the implementation
	of trace__prepare_for_call.

compiler/*.m:
	Minor changes in accordance with the major ones above.

compiler/options.m:
	Introduce a new option, allow_hijacks, which is set to "yes" by
	default. It is not used yet, but the idea is that when it is set to no,
	the code generator will not generate code that hijacks the nondet
	stack frame of another procedure invocation; instead, it will create
	a new temporary nondet stack frame. If the current procedure is
	model_non, it will have three fields: prevfr, redoip and redofr.
	If the current procedure is model_det or model_semi, it will have
	a fourth field that is set to the value of MR_sp. The idea is that
	the runtime system, which will be able to distinguish between
	ordinary frames (whose size is at least 5 words), 3-word and 4-word
	temporary frames, will now be able to use the redofr slots of
	all three kinds of frames and the fourth slot values of 4-word
	temporary frames as the addresses relative to which framevars
	and detstackvars respectively ought to be offset in stack layouts.

compiler/handle_options.m:
	Turn off allow_hijacks if the gc method is accurate.

runtime/mercury_stacks.h:
	Change the definitions for the nondet stack handling macros
	to accommodate the new nondet stack handling discipline.
	Define a new macro for creating temp nondet frames.

	Define MR_based_stackvar and MR_based_framevar (both of which start
	numbering slots at 1), and express other references, including
	MR_stackvar and MR_framevar and backward compatible definitions of
	detstackvar and framevar for hand-written C code, in terms of those
	two.

runtime/mercury_stack_trace.[ch]:
	Add a new function to print a dump of the fixed elements nondet stack,
	for debugging my changes. (The dump does not include variable values.)

runtime/mercury_trace_internal.c:
	Add a new undocumented command "D" for dumping the nondet stack
	(users should not know about this command, since the output is
	intelligible only to implementors).

	Add a new command "toggle_echo" that can cause the debugger to echo
	all commands. When the input to the debugger is redirected, this
	echo causes the output of the session to be much more readable.

runtime/mercury_wrapper.c:
	Save the address of the artificial bottom nondet stack frame,
	so that the new function in mercury_stack_trace.c can find out
	where to stop.

runtime/mercury_engine.c:
runtime/mercury_wrapper.c:
	Put MR_STACK_TRACE_THIS_MODULE at the tops of these modules, so that
	the labels they define (e.g. do_fail and global_success) are registered
	in the label table when their module initialization functions are
	called. This is necessary for a meaningful nondet stack dump.

runtime/mercury_grade.h:
	Add a new component to the grade string that specifies whether
	the code was compiled with the old or the new method of handling
	the nondet stack. This is important, because modules compiled
	with different nondet stack handling disciplines are not compatible.
	This component depends on whether MR_USE_REDOFR is defined or not.

runtime/mercury_imp.h:
	If MR_DISABLE_REDOFR is defined, undefine off MR_USE_REDOFR before
	including mercury_grade.h. This is to allow people to continue
	working on un-updated workspaces after this change is installed;
	they should put "EXTRA_CFLAGS = -DMR_DISABLE_REDOFR" into
	Mmake.stage.params. (This way their stage1 will use the new method
	of handling failure, while their stage2 2&3 will use the old one.)

	This change should be undone once all our workspaces have switched
	over to the new failure handling method.

tests/hard_coded/cut_test.{m,exp}:
	A new test case to tickle the various ways of handling cuts in the
	new code generator.

tests/hard_coded/Mmakefile:
	Enable the new test case.
1998-07-20 10:04:02 +00:00

210 lines
6.7 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1995-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.
%-----------------------------------------------------------------------------%
% vn_type.m - types for value numbering.
% Author: zs.
%-----------------------------------------------------------------------------%
:- module vn_type.
:- interface.
:- import_module llds, livemap, options.
:- import_module bool, getopt, map, set, list, std_util.
:- type vn == int.
:- type vnlval ---> vn_reg(reg_type, int)
; vn_temp(reg_type, int)
; vn_stackvar(int)
; vn_framevar(int)
; vn_succip
; vn_maxfr
; vn_curfr
; vn_succfr(vn)
; vn_prevfr(vn)
; vn_redofr(vn)
; vn_redoip(vn)
; vn_succip(vn)
; vn_hp
; vn_sp
; vn_field(maybe(tag), vn, vn)
; vn_mem_ref(vn).
% these lvals do not have vnlval parallels
% lvar(var)
:- type vnrval ---> vn_origlval(vnlval)
; vn_mkword(tag, vn)
; vn_const(rval_const)
; vn_create(tag, list(maybe(rval)),
bool, int, string)
; vn_unop(unary_op, vn)
; vn_binop(binary_op, vn, vn)
; vn_stackvar_addr(int)
; vn_framevar_addr(int)
; vn_heap_addr(vn, int, int).
% these rvals do not have vnrval parallels
% var(var)
% given a vnlval, figure out its type
:- pred vn_type__vnlval_type(vnlval::in, llds_type::out) is det.
% given a vnrval, figure out its type
:- pred vn_type__vnrval_type(vnrval::in, llds_type::out) is det.
:- type vn_src ---> src_ctrl(int)
; src_liveval(vnlval)
; src_access(vnlval)
; src_vn(int).
:- type vn_node ---> node_shared(vn)
; node_lval(vnlval)
; node_origlval(vnlval)
; node_ctrl(int).
:- type vn_instr ---> vn_livevals(lvalset)
; vn_call(code_addr, code_addr,
list(liveinfo), call_model)
; vn_mkframe(nondet_frame_info, code_addr)
; vn_label(label)
; vn_goto(code_addr)
; vn_computed_goto(vn, list(label))
; vn_if_val(vn, code_addr)
; vn_mark_hp(vnlval)
; vn_restore_hp(vn)
; vn_store_ticket(vnlval)
; vn_reset_ticket(vn, reset_trail_reason)
; vn_discard_ticket
; vn_mark_ticket_stack(vnlval)
; vn_discard_tickets_to(vn)
; vn_incr_sp(int, string)
; vn_decr_sp(int).
:- type parentry == pair(lval, list(rval)).
:- type parallel ---> parallel(label, label, list(parentry)).
:- type vnlvalset == set(vnlval).
:- type ctrlmap == map(int, vn_instr).
:- type flushmap == map(int, flushmapentry).
:- type flushmapentry == map(vnlval, vn).
:- type parmap == map(int, list(parallel)).
:- type vn_ctrl_tuple ---> tuple(int, ctrlmap, flushmap, int, parmap).
:- type vn_params.
:- pred vn_type__init_params(option_table(option), vn_params).
:- mode vn_type__init_params(in, out) is det.
:- pred vn_type__bytes_per_word(vn_params, int).
:- mode vn_type__bytes_per_word(in, out) is det.
:- pred vn_type__real_r_regs(vn_params, int).
:- mode vn_type__real_r_regs(in, out) is det.
:- pred vn_type__real_f_regs(vn_params, int).
:- mode vn_type__real_f_regs(in, out) is det.
:- pred vn_type__real_r_temps(vn_params, int).
:- mode vn_type__real_r_temps(in, out) is det.
:- pred vn_type__real_f_temps(vn_params, int).
:- mode vn_type__real_f_temps(in, out) is det.
:- pred vn_type__costof_assign(vn_params, int).
:- mode vn_type__costof_assign(in, out) is det.
:- pred vn_type__costof_intops(vn_params, int).
:- mode vn_type__costof_intops(in, out) is det.
:- pred vn_type__costof_stackref(vn_params, int).
:- mode vn_type__costof_stackref(in, out) is det.
:- pred vn_type__costof_heapref(vn_params, int).
:- mode vn_type__costof_heapref(in, out) is det.
:- implementation.
:- import_module int.
:- type vn_params ---> vn_params(
int, % word size in bytes
% needed for incr_hp; incr_hp
int, % number of real r regs
int, % number of real f regs
int, % number of real r temps
int, % number of real f temps
int, % cost of assign
int, % cost of int operation
int, % cost of stack reference
int % cost of heap reference
).
vn_type__init_params(OptionTable, VnParams) :-
getopt__lookup_int_option(OptionTable, num_real_r_regs, RealRRegs),
getopt__lookup_int_option(OptionTable, num_real_f_regs, RealFRegs),
getopt__lookup_int_option(OptionTable, num_real_r_temps, RealRTemps),
getopt__lookup_int_option(OptionTable, num_real_f_temps, RealFTemps),
getopt__lookup_int_option(OptionTable, bytes_per_word, WordBytes),
VnParams = vn_params(WordBytes, RealRRegs, RealFRegs,
RealRTemps, RealFTemps, 1, 1, 2, 2).
vn_type__bytes_per_word(vn_params(BytesPerWord, _, _, _, _, _, _, _, _),
BytesPerWord).
vn_type__real_r_regs(vn_params(_, RealRRegs, _, _, _, _, _, _, _),
RealRRegs).
vn_type__real_f_regs(vn_params(_, _, RealFRegs, _, _, _, _, _, _),
RealFRegs).
vn_type__real_r_temps(vn_params(_, _, _, RealRTemps, _, _, _, _, _),
RealRTemps).
vn_type__real_f_temps(vn_params(_, _, _, _, RealFTemps, _, _, _, _),
RealFTemps).
vn_type__costof_assign(vn_params(_, _, _, _, _, AssignCost, _, _, _),
AssignCost).
vn_type__costof_intops(vn_params(_, _, _, _, _, _, IntOpCost, _, _),
IntOpCost).
vn_type__costof_stackref(vn_params(_, _, _, _, _, _, _, StackCost, _),
StackCost).
vn_type__costof_heapref(vn_params(_, _, _, _, _, _, _, _, HeapCost),
HeapCost).
vn_type__vnrval_type(vn_origlval(Lval), Type) :-
vn_type__vnlval_type(Lval, Type).
vn_type__vnrval_type(vn_create(_, _, _, _, _), data_ptr).
vn_type__vnrval_type(vn_mkword(_, _), data_ptr). % see comment in llds.m
vn_type__vnrval_type(vn_const(Const), Type) :-
llds__const_type(Const, Type).
vn_type__vnrval_type(vn_unop(UnOp, _), Type) :-
llds__unop_return_type(UnOp, Type).
vn_type__vnrval_type(vn_binop(BinOp, _, _), Type) :-
llds__binop_return_type(BinOp, Type).
vn_type__vnrval_type(vn_stackvar_addr(_), data_ptr).
vn_type__vnrval_type(vn_framevar_addr(_), data_ptr).
vn_type__vnrval_type(vn_heap_addr(_, _, _), data_ptr).
vn_type__vnlval_type(vn_reg(RegType, _), Type) :-
llds__register_type(RegType, Type).
vn_type__vnlval_type(vn_succip, code_ptr).
vn_type__vnlval_type(vn_maxfr, data_ptr).
vn_type__vnlval_type(vn_curfr, data_ptr).
vn_type__vnlval_type(vn_hp, data_ptr).
vn_type__vnlval_type(vn_sp, data_ptr).
vn_type__vnlval_type(vn_temp(RegType, _), Type) :-
llds__register_type(RegType, Type).
vn_type__vnlval_type(vn_stackvar(_), word).
vn_type__vnlval_type(vn_framevar(_), word).
vn_type__vnlval_type(vn_succip(_), code_ptr).
vn_type__vnlval_type(vn_redoip(_), code_ptr).
vn_type__vnlval_type(vn_redofr(_), data_ptr).
vn_type__vnlval_type(vn_succfr(_), data_ptr).
vn_type__vnlval_type(vn_prevfr(_), data_ptr).
vn_type__vnlval_type(vn_field(_, _, _), word).
vn_type__vnlval_type(vn_mem_ref(_), word).