Files
mercury/compiler/goal_path.m
Zoltan Somogyi 5013dd9c76 Implement nondet pragma C codes.
Estimated hours taken: 40

Implement nondet pragma C codes.

runtime/mercury_stacks.h:
	Define a new macro, mkpragmaframe, for use in the implementation
	of nondet pragma C codes. This new macro includes space for a
	struct with a given sruct tag in the nondet stack frame being created.

compiler/{prog_data.m,hlds_goal.m}:
	Revise the representation of pragma C codes, both as the item and
	in the HLDS.

compiler/prog_io_pragma.m:
	Parse nondet pragma C declarations.

	Fix the indentation in some places.

compiler/llds.m:
	Include an extra argument in mkframe instructions. This extra argument
	gives the details of the C structure (if any) to be included in the
	nondet stack frame to be created.

	Generalize the LLDS representation of pragma C codes. Instead of a
	fixed sequence of <assign from inputs, user c code, assign to outputs>,
	let the sequence contain these elements, as well as arbitrary
	compiler-generated C code, in any order and possibly with repetitions.
	This flexibility is needed for nondet pragma C codes.

	Add a field to pragma C codes to say whether they can call Mercury.
	Some optimizations can do a better job if they know that a pragma C
	code cannot call Mercury.

	Add another field to pragma C codes to give the name of the label
	they refer to (if any). This is needed to prevent labelopt from
	incorrectly optimizing away the label definition.

	Add a new alternative to the type pragma_c_decl, to describe the
	declaration of the local variable that points to the save struct.

compiler/llds_out.m:
	Output mkframe instructions that specify a struct as invoking the new
	mkpragmaframe macro, and make sure that the struct is declared just
	before the procedure that uses it.

	Other minor changes to keep up with the changes to the representation
	of pragma C code in the LLDS, and to make the output look a bit nicer.

compiler/pragma_c_gen.m:
	Add code to generate code for nondet pragma C codes. Revise the utility
	predicates and their data structures a bit to make this possible.

compiler/code_gen.m:
	Add code for the necessary special handling of prologs and epilogs
	of procedures defined by nondet pragma C codes. The prologs need
	to be modified to include a programmer-defined C structure in the
	nondet stack frame and to communicate the location of this structure
	to the pragma C code, whereas the functionality of the epilog is
	taken care of by the pragma C code itself.

compiler/make_hlds.m:
	When creating a proc_info for a procedure defined by a pragma C code,
	we used to insert unifications between the headvars and the vars of
	the pragma C code into the body goal. We now perform substitutions
	instead. This removes a factor that would complicate the generation
	of code for nondet pragma C codes.

	Pass a moduleinfo down the procedures that warn about singletons
	(and other basic scope errors). When checking whether to warn about
	an argument of a pragma C code not being mentioned in the C code
	fragment, we need to know whether the argument is input or output,
	since input variables should appear in some code fragments in a
	nondet pragma C code and must not appear in others. The
	mode_is_{in,out}put checks need the moduleinfo.

	(We do not need to check for any variables being mentioned where
	they shouldn't be. The C compiler will fail in the presence of any
	errors of that type, and since those variables could be referred
	to via macros whose definitions we do not see, we couldn't implement
	a reliable test anyway.)

compiler/opt_util.m:
	Recognize that some sorts of pragma_c codes cannot affect the data
	structures that control backtracking. This allows peepholing to
	do a better job on code sequences produced for nondet pragma C codes.

	Recognize that the C code strings inside some pragma_c codes refer to
	other labels in the procedure. This prevents labelopt from incorrectly
	optimizing away these labels.

compiler/dupelim.m:
	If a label is referred to from within a C code string, then do not
	attempt to optimize it away.

compiler/det_analysis.m:
	Remove a now incorrect part of an error message.

compiler/*.m:
	Minor changes to conform to changes to the HLDS and LLDS data
	structures.
1998-01-13 10:14:23 +00:00

92 lines
3.3 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998 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.
%-----------------------------------------------------------------------------%
% This module looks after goal paths, which associate each goal
% with its position in a procedure definition,
% Main author: zs.
:- module goal_path.
:- interface.
:- import_module hlds_pred, hlds_module.
:- pred goal_path__fill_slots(proc_info::in, module_info::in, proc_info::out)
is det.
:- implementation.
:- import_module hlds_goal.
:- import_module int, list, std_util.
goal_path__fill_slots(Proc0, _ModuleInfo, Proc) :-
% The ModuleInfo argument is there just for passes_aux
proc_info_goal(Proc0, Goal0),
fill_goal_slots(Goal0, [], Goal),
proc_info_set_goal(Proc0, Goal, Proc).
:- pred fill_goal_slots(hlds_goal::in, goal_path::in, hlds_goal::out) is det.
fill_goal_slots(Expr0 - Info0, Path0, Expr - Info) :-
goal_info_set_goal_path(Info0, Path0, Info),
fill_expr_slots(Expr0, Path0, Expr).
:- pred fill_expr_slots(hlds_goal_expr::in, goal_path::in,
hlds_goal_expr::out) is det.
fill_expr_slots(conj(Goals0), Path0, conj(Goals)) :-
fill_conj_slots(Goals0, Path0, 0, Goals).
fill_expr_slots(disj(Goals0, B), Path0, disj(Goals, B)) :-
fill_disj_slots(Goals0, Path0, 0, Goals).
fill_expr_slots(switch(A, B, Cases0, D), Path0, switch(A, B, Cases, D)) :-
fill_switch_slots(Cases0, Path0, 0, Cases).
fill_expr_slots(not(Goal0), Path0, not(Goal)) :-
fill_goal_slots(Goal0, [neg | Path0], Goal).
fill_expr_slots(some(A, Goal0), Path0, some(A, Goal)) :-
fill_goal_slots(Goal0, [exist | Path0], Goal).
fill_expr_slots(if_then_else(A, Cond0, Then0, Else0, E), Path0,
if_then_else(A, Cond, Then, Else, E)) :-
fill_goal_slots(Cond0, [ite_cond | Path0], Cond),
fill_goal_slots(Then0, [ite_then | Path0], Then),
fill_goal_slots(Else0, [ite_else | Path0], Else).
fill_expr_slots(call(A,B,C,D,E,F), _Path0, call(A,B,C,D,E,F)).
fill_expr_slots(higher_order_call(A,B,C,D,E,F), _Path0,
higher_order_call(A,B,C,D,E,F)).
fill_expr_slots(class_method_call(A,B,C,D,E,F), _Path0,
class_method_call(A,B,C,D,E,F)).
fill_expr_slots(unify(A,B,C,D,E), _Path0, unify(A,B,C,D,E)).
fill_expr_slots(pragma_c_code(A,B,C,D,E,F,G), _Path0,
pragma_c_code(A,B,C,D,E,F,G)).
:- pred fill_conj_slots(list(hlds_goal)::in, goal_path::in, int::in,
list(hlds_goal)::out) is det.
fill_conj_slots([], _, _, []).
fill_conj_slots([Goal0 | Goals0], Path0, N0, [Goal | Goals]) :-
N1 is N0 + 1,
fill_goal_slots(Goal0, [conj(N1) | Path0], Goal),
fill_conj_slots(Goals0, Path0, N1, Goals).
:- pred fill_disj_slots(list(hlds_goal)::in, goal_path::in, int::in,
list(hlds_goal)::out) is det.
fill_disj_slots([], _, _, []).
fill_disj_slots([Goal0 | Goals0], Path0, N0, [Goal | Goals]) :-
N1 is N0 + 1,
fill_goal_slots(Goal0, [disj(N1) | Path0], Goal),
fill_disj_slots(Goals0, Path0, N1, Goals).
:- pred fill_switch_slots(list(case)::in, goal_path::in, int::in,
list(case)::out) is det.
fill_switch_slots([], _, _, []).
fill_switch_slots([case(A, Goal0) | Cases0], Path0, N0,
[case(A, Goal) | Cases]) :-
N1 is N0 + 1,
fill_goal_slots(Goal0, [switch(N1) | Path0], Goal),
fill_switch_slots(Cases0, Path0, N1, Cases).