Files
mercury/compiler/store_alloc.m
Simon Taylor 2725b1a331 Aditi update syntax, type and mode checking.
Estimated hours taken: 220

Aditi update syntax, type and mode checking.

Change the hlds_goal for constructions in preparation for
structure reuse to avoid making multiple conflicting changes.

compiler/hlds_goal.m:
	Merge `higher_order_call' and `class_method_call' into a single
	`generic_call' goal type. This also has alternatives for the
	various Aditi builtins for which type declarations can't
	be written.

	Remove the argument types field from higher-order/class method calls.
	It wasn't used often, and wasn't updated by optimizations
	such as inlining. The types can be obtained from the vartypes
	field of the proc_info.

	Add a `lambda_eval_method' field to lambda_goals.

	Add a field to constructions to identify which RL code fragment should
	be used for an top-down Aditi closure.

	Add fields to constructions to hold structure reuse information.
	This is currently ignored -- the changes to implement structure
	reuse will be committed to the alias branch.
	This is included here to avoid lots of CVS conflicts caused by
	changing the definition of `hlds_goal' twice.

	Add a field to `some' goals to specify whether the quantification
	can be removed. This is used to make it easier to ensure that
	indexes are used for updates.

	Add a field to lambda_goals to describe whether the modes were
	guessed by the compiler and may need fixing up after typechecking
	works out the argument types.

	Add predicate `hlds_goal__generic_call_id' to work out a call_id
	for a generic call for use in error messages.

compiler/purity.m:
compiler/post_typecheck.m:
	Fill in the modes of Aditi builtin calls and closure constructions.
	This needs to know which are the `aditi__state' arguments, so
	it must be done after typechecking.

compiler/prog_data.m:
	Added `:- type sym_name_and_arity ---> sym_name/arity'.

	Add a type `lambda_eval_method', which describes how a closure
	is to be executed. The alternatives are normal Mercury execution,
	bottom-up execution by Aditi and top-down execution by Aditi.

compiler/prog_out.m:
	Add predicate `prog_out__write_sym_name_and_arity', which
	replaces duplicated inline code in a few places.

compiler/hlds_data.m:
	Add a `lambda_eval_method' field to `pred_const' cons_ids and
	`pred_closure_tag' cons_tags.

compiler/hlds_pred.m:
	Remove type `pred_call_id', replace it with type `simple_call_id',
	which combines a `pred_or_func' and a `sym_name_and_arity'.

	Add a type `call_id' which describes all the different types of call,
	including normal calls, higher-order and class-method calls
	and Aditi builtins.

	Add `aditi_top_down' to the type `marker'.

	Remove `aditi_interface' from type `marker'. Interfacing to
	Aditi predicates is now handled by `generic_call' hlds_goals.

	Add a type `rl_exprn_id' which identifies a predicate to
	be executed top-down by Aditi.
	Add a `maybe(rl_exprn_id)'  field to type `proc_info'.

	Add predicate `adjust_func_arity' to convert between the arity
	of a function to its arity as a predicate.

	Add predicates `get_state_args' and `get_state_args_det' to
	extract the DCG state arguments from an argument list.

	Add predicate `pred_info_get_call_id' to get a `simple_call_id'
	for a predicate for use in error messages.

compiler/hlds_out.m:
	Write the new representation for call_ids.

	Add a predicate `hlds_out__write_call_arg_id' which
	replaces similar code in mode_errors.m and typecheck.m.

compiler/prog_io_goal.m:
	Add support for `aditi_bottom_up' and `aditi_top_down' annotations
	on pred expressions.

compiler/prog_io_util.m:
compiler/prog_io_pragma.m:
	Add predicates
	- `prog_io_util:parse_name_and_arity' to parse `SymName/Arity'
		(moved from prog_io_pragma.m).
	- `prog_io_util:parse_pred_or_func_name_and_arity to parse
		`pred SymName/Arity' or `func SymName/Arity'.
	- `prog_io_util:parse_pred_or_func_and_args' to parse terms resembling
		a clause head (moved from prog_io_pragma.m).

compiler/type_util.m:
	Add support for `aditi_bottom_up' and `aditi_top_down' annotations
	on higher-order types.

	Add predicates `construct_higher_order_type',
	`construct_higher_order_pred_type' and
	`construct_higher_order_func_type' to avoid some code duplication.

compiler/mode_util.m:
	Add predicate `unused_mode/1', which returns `builtin:unused'.
	Add functions `aditi_di_mode/0', `aditi_ui_mode/0' and
	`aditi_uo_mode/0' which return `in', `in', and `out', but will
	be changed to return `di', `ui' and `uo' when alias tracking
	is implemented.

compiler/goal_util.m:
	Add predicate `goal_util__generic_call_vars' which returns
	any arguments to a generic_call which are not in the argument list,
	for example the closure passed to a higher-order call or
	the typeclass_info for a class method call.

compiler/llds.m:
compiler/exprn_aux.m:
compiler/dupelim.m:
compiler/llds_out.m:
compiler/opt_debug.m:
	Add builtin labels for the Aditi update operations.

compiler/hlds_module.m:
	Add predicate predicate_table_search_pf_sym, used for finding
	possible matches for a call with the wrong number of arguments.

compiler/intermod.m:
	Don't write predicates which build `aditi_top_down' goals,
	because there is currently no way to tell importing modules
	which RL code fragment to use.

compiler/simplify.m:
	Obey the `cannot_remove' field of explicit quantification goals.

compiler/make_hlds.m:
	Parse Aditi updates.

	Don't typecheck clauses for which syntax errors in Aditi updates
	are found - this avoids spurious "undefined predicate `aditi_insert/3'"
	errors.

	Factor out some common code to handle terms of the form `Head :- Body'.
	Factor out common code in the handling of pred and func expressions.

compiler/typecheck.m:
	Typecheck Aditi builtins.

	Allow the argument types of matching predicates to be adjusted
	when typechecking the higher-order arguments of Aditi builtins.

	Change `typecheck__resolve_pred_overloading' to take a list of
	argument types rather than a `map(var, type)' and a list of
	arguments to allow a transformation to be performed on the
	argument types before passing them.

compiler/error_util.m:
	Move the part of `report_error_num_args' which writes
	"wrong number of arguments (<x>; expected <y>)" from
	typecheck.m for use by make_hlds.m when reporting errors
	for Aditi builtins.

compiler/modes.m:
compiler/unique_modes.m:
compiler/modecheck_call.m:
	Modecheck Aditi builtins.

compiler/lambda.m:
	Handle the markers for predicates introduced for
	`aditi_top_down' and `aditi_bottom_up' lambda expressions.

compiler/polymorphism.m:
	Add extra type_infos to `aditi_insert' calls
	describing the tuple to insert.

compiler/call_gen.m:
	Generate code for Aditi builtins.

compiler/unify_gen.m:
compiler/bytecode_gen.m:
	Abort on `aditi_top_down' and `aditi_bottom_up' lambda
	expressions - code generation for them is not yet implemented.

compiler/magic.m:
	Use the `aditi_call' generic_call rather than create
	a new procedure for each Aditi predicate called from C.

compiler/rl_out.pp:
compiler/rl_gen.m:
compiler/rl.m:
	Move some utility code used by magic.m and call_gen.m into rl.m.

	Remove an XXX comment about reference counting being not yet
	implemented - Evan has fixed that.

library/ops.m:
compiler/mercury_to_mercury.m:
doc/transition_guide.texi:
	Add unary prefix operators `aditi_bottom_up' and `aditi_top_down',
	used as qualifiers on lambda expressions.
	Add infix operator `==>' to separate the tuples in an
	`aditi_modify' call.

compiler/follow_vars.m:
	Thread a `map(prog_var, type)' through, needed because
	type information is no longer held in higher-order call goals.

compiler/table_gen.m:
	Use the `make_*_construction' predicates in hlds_goal.m
	to construct constants.

compiler/*.m:
	Trivial changes to add extra fields to hlds_goal structures.

doc/reference_manual.texi:
	Document Aditi updates.

	Use @samp{pragma base_relation} instead of
	@samp{:- pragma base_relation} throughout the Aditi documentation
	to be consistent with other parts of the reference manual.

tests/valid/Mmakefile:
tests/valid/aditi_update.m:
tests/valid/aditi.m:
	Test case.

tests/valid/Mmakefile:
	Remove some hard-coded --intermodule-optimization rules which are
	no longer needed because `mmake depend' is now run in this directory.

tests/invalid/*.err_exp:
	Fix expected output for changes in reporting of call_ids
	in typecheck.m.

tests/invalid/Mmakefile
tests/invalid/aditi_update_errors.{m,err_exp}:
tests/invalid/aditi_update_mode_errors.{m,err_exp}:
	Test error messages for Aditi updates.

tests/valid/aditi.m:
tests/invalid/aditi.m:
	Cut down version of extras/aditi/aditi.m to provide basic declarations
	for Aditi compilation such as `aditi__state' and the modes
	`aditi_di', `aditi_uo' and `aditi_ui'. Installing extras/aditi/aditi.m
	somewhere would remove the need for these.
1999-07-13 08:55:28 +00:00

439 lines
16 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1994-1999 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.
%-----------------------------------------------------------------------------%
% Original author: conway.
% Extensive modification by zs.
% Allocates the storage location for each live variable at the end of
% each branched structure, so that the code generator will generate code
% which puts the variable in the same place in each branch.
% This module requires arg_infos and livenesses to have already been computed,
% and stack slots allocated.
% If the appropriate option is set, the code calls the follow_vars module
% to help guide its decisions.
% See compiler/notes/allocation.html for a description of the framework that
% this pass operates within.
%-----------------------------------------------------------------------------%
:- module store_alloc.
:- interface.
:- import_module hlds_module, hlds_pred.
:- pred store_alloc_in_proc(proc_info, pred_id, module_info, proc_info).
:- mode store_alloc_in_proc(in, in, in, out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module follow_vars, liveness, hlds_goal, llds, prog_data.
:- import_module options, globals, goal_util, mode_util, instmap, trace.
:- import_module list, map, set, std_util, assoc_list.
:- import_module bool, int, require.
:- type stack_slot_info
---> stack_slot_info(
bool, % was follow_vars run?
int, % the number of real r regs
stack_slots % maps each var to its stack slot
% (if it has one)
).
%-----------------------------------------------------------------------------%
store_alloc_in_proc(ProcInfo0, PredId, ModuleInfo, ProcInfo) :-
module_info_globals(ModuleInfo, Globals),
globals__lookup_bool_option(Globals, follow_vars, ApplyFollowVars),
( ApplyFollowVars = yes ->
proc_info_goal(ProcInfo0, Goal0),
find_final_follow_vars(ProcInfo0, FollowVars0),
proc_info_vartypes(ProcInfo0, VarTypes),
find_follow_vars_in_goal(Goal0, VarTypes, ModuleInfo,
FollowVars0, Goal1, FollowVars),
Goal1 = GoalExpr1 - GoalInfo1,
goal_info_set_follow_vars(GoalInfo1, yes(FollowVars),
GoalInfo2),
Goal2 = GoalExpr1 - GoalInfo2
;
proc_info_goal(ProcInfo0, Goal2)
),
initial_liveness(ProcInfo0, PredId, ModuleInfo, Liveness0),
globals__get_trace_level(Globals, TraceLevel),
( TraceLevel \= none ->
trace__fail_vars(ModuleInfo, ProcInfo0, ResumeVars0)
;
set__init(ResumeVars0)
),
globals__lookup_int_option(Globals, num_real_r_regs, NumRealRRegs),
proc_info_stack_slots(ProcInfo0, StackSlots),
StackSlotsInfo = stack_slot_info(ApplyFollowVars, NumRealRRegs,
StackSlots),
store_alloc_in_goal(Goal2, Liveness0, ResumeVars0, ModuleInfo,
StackSlotsInfo, Goal, _),
proc_info_set_goal(ProcInfo0, Goal, ProcInfo).
%-----------------------------------------------------------------------------%
:- pred store_alloc_in_goal(hlds_goal, liveness_info, set(prog_var),
module_info, stack_slot_info, hlds_goal, liveness_info).
:- mode store_alloc_in_goal(in, in, in, in, in, out, out) is det.
store_alloc_in_goal(Goal0 - GoalInfo0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal - GoalInfo0, Liveness) :-
% note: we must be careful to apply deaths before births
goal_info_get_pre_deaths(GoalInfo0, PreDeaths),
goal_info_get_pre_births(GoalInfo0, PreBirths),
goal_info_get_post_deaths(GoalInfo0, PostDeaths),
goal_info_get_post_births(GoalInfo0, PostBirths),
set__difference(Liveness0, PreDeaths, Liveness1),
set__union(Liveness1, PreBirths, Liveness2),
store_alloc_in_goal_2(Goal0, Liveness2, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal1, Liveness3),
set__difference(Liveness3, PostDeaths, Liveness4),
% If any variables magically become live in the PostBirths,
% then they have to mundanely become live in a parallel goal,
% so we don't need to allocate anything for them here.
%
% Any variables that become magically live at the end of the goal
% should not be included in the store map.
set__union(Liveness4, PostBirths, Liveness),
(
Goal1 = switch(Var, CanFail, Cases, FollowVars)
->
set__union(Liveness4, ResumeVars0, MappedSet),
set__to_sorted_list(MappedSet, MappedVars),
store_alloc_allocate_storage(MappedVars, FollowVars,
StackSlotInfo, StoreMap),
Goal = switch(Var, CanFail, Cases, StoreMap)
;
Goal1 = if_then_else(Vars, Cond, Then, Else, FollowVars)
->
set__union(Liveness4, ResumeVars0, MappedSet),
set__to_sorted_list(MappedSet, MappedVars),
store_alloc_allocate_storage(MappedVars, FollowVars,
StackSlotInfo, StoreMap),
Goal = if_then_else(Vars, Cond, Then, Else, StoreMap)
;
Goal1 = disj(Disjuncts, FollowVars)
->
set__union(Liveness4, ResumeVars0, MappedSet),
set__to_sorted_list(MappedSet, MappedVars),
store_alloc_allocate_storage(MappedVars, FollowVars,
StackSlotInfo, StoreMap),
Goal = disj(Disjuncts, StoreMap)
;
Goal = Goal1
).
%-----------------------------------------------------------------------------%
% Here we process each of the different sorts of goals.
:- pred store_alloc_in_goal_2(hlds_goal_expr, liveness_info,
set(prog_var), module_info, stack_slot_info, hlds_goal_expr,
liveness_info).
:- mode store_alloc_in_goal_2(in, in, in, in, in, out, out) is det.
store_alloc_in_goal_2(conj(Goals0), Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, conj(Goals), Liveness) :-
store_alloc_in_conj(Goals0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, Liveness).
store_alloc_in_goal_2(par_conj(Goals0, SM), Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, par_conj(Goals, SM), Liveness) :-
store_alloc_in_par_conj(Goals0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, Liveness).
store_alloc_in_goal_2(disj(Goals0, FV), Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, disj(Goals, FV), Liveness) :-
store_alloc_in_disj(Goals0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, Liveness).
store_alloc_in_goal_2(not(Goal0), Liveness0, _ResumeVars0, ModuleInfo,
StackSlotInfo, not(Goal), Liveness) :-
Goal0 = _ - GoalInfo0,
goal_info_get_resume_point(GoalInfo0, ResumeNot),
goal_info_resume_vars_and_loc(ResumeNot, ResumeNotVars, _),
store_alloc_in_goal(Goal0, Liveness0, ResumeNotVars, ModuleInfo,
StackSlotInfo, Goal, Liveness).
store_alloc_in_goal_2(switch(Var, Det, Cases0, FV), Liveness0, ResumeVars0,
ModuleInfo, StackSlotInfo,
switch(Var, Det, Cases, FV), Liveness) :-
store_alloc_in_cases(Cases0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Cases, Liveness).
store_alloc_in_goal_2(if_then_else(Vars, Cond0, Then0, Else0, FV),
Liveness0, ResumeVars0, ModuleInfo, StackSlotInfo,
if_then_else(Vars, Cond, Then, Else, FV), Liveness) :-
Cond0 = _ - CondGoalInfo0,
goal_info_get_resume_point(CondGoalInfo0, ResumeCond),
goal_info_resume_vars_and_loc(ResumeCond, ResumeCondVars, _),
store_alloc_in_goal(Cond0, Liveness0, ResumeCondVars, ModuleInfo,
StackSlotInfo, Cond, Liveness1),
store_alloc_in_goal(Then0, Liveness1, ResumeVars0, ModuleInfo,
StackSlotInfo, Then, Liveness),
store_alloc_in_goal(Else0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Else, _Liveness2).
store_alloc_in_goal_2(some(Vars, CanRemove, Goal0), Liveness0, ResumeVars0,
ModuleInfo,
StackSlotInfo, some(Vars, CanRemove, Goal), Liveness) :-
store_alloc_in_goal(Goal0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal, Liveness).
store_alloc_in_goal_2(generic_call(A, B, C, D), Liveness, _, _,
_, generic_call(A, B, C, D), Liveness).
store_alloc_in_goal_2(call(A, B, C, D, E, F), Liveness, _, _,
_, call(A, B, C, D, E, F), Liveness).
store_alloc_in_goal_2(unify(A,B,C,D,E), Liveness, _, _,
_, unify(A,B,C,D,E), Liveness).
store_alloc_in_goal_2(pragma_c_code(A, B, C, D, E, F, G), Liveness, _, _,
_, pragma_c_code(A, B, C, D, E, F, G), Liveness).
%-----------------------------------------------------------------------------%
:- pred store_alloc_in_conj(list(hlds_goal), liveness_info, set(prog_var),
module_info, stack_slot_info, list(hlds_goal), liveness_info).
:- mode store_alloc_in_conj(in, in, in, in, in, out, out) is det.
store_alloc_in_conj([], Liveness, _, _, _, [], Liveness).
store_alloc_in_conj([Goal0 | Goals0], Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, [Goal | Goals], Liveness) :-
(
% XXX should be threading the instmap
Goal0 = _ - GoalInfo,
goal_info_get_instmap_delta(GoalInfo, InstMapDelta),
instmap_delta_is_unreachable(InstMapDelta)
->
store_alloc_in_goal(Goal0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal, Liveness),
Goals = Goals0
;
store_alloc_in_goal(Goal0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal, Liveness1),
store_alloc_in_conj(Goals0, Liveness1, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, Liveness)
).
%-----------------------------------------------------------------------------%
:- pred store_alloc_in_par_conj(list(hlds_goal), liveness_info, set(prog_var),
module_info, stack_slot_info, list(hlds_goal), liveness_info).
:- mode store_alloc_in_par_conj(in, in, in, in, in, out, out) is det.
store_alloc_in_par_conj([], Liveness, _, _, _, [], Liveness).
store_alloc_in_par_conj([Goal0 | Goals0], Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, [Goal | Goals], Liveness) :-
store_alloc_in_goal(Goal0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal, Liveness),
store_alloc_in_par_conj(Goals0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, _Liveness1).
%-----------------------------------------------------------------------------%
:- pred store_alloc_in_disj(list(hlds_goal), liveness_info, set(prog_var),
module_info, stack_slot_info, list(hlds_goal), liveness_info).
:- mode store_alloc_in_disj(in, in, in, in, in, out, out) is det.
store_alloc_in_disj([], Liveness, _, _, _, [], Liveness).
store_alloc_in_disj([Goal0 | Goals0], Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, [Goal | Goals], Liveness) :-
Goal0 = _ - GoalInfo0,
goal_info_get_resume_point(GoalInfo0, ResumeGoal),
(
ResumeGoal = no_resume_point,
ResumeGoalVars = ResumeVars0
;
ResumeGoal = resume_point(ResumeGoalVars, _)
),
store_alloc_in_goal(Goal0, Liveness0, ResumeGoalVars, ModuleInfo,
StackSlotInfo, Goal, Liveness),
store_alloc_in_disj(Goals0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, _Liveness1).
%-----------------------------------------------------------------------------%
:- pred store_alloc_in_cases(list(case), liveness_info, set(prog_var),
module_info, stack_slot_info, list(case), liveness_info).
:- mode store_alloc_in_cases(in, in, in, in, in, out, out) is det.
store_alloc_in_cases([], Liveness, _, _, _, [], Liveness).
store_alloc_in_cases([case(Cons, Goal0) | Goals0], Liveness0, ResumeVars0,
ModuleInfo, StackSlotInfo,
[case(Cons, Goal) | Goals], Liveness) :-
store_alloc_in_goal(Goal0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goal, Liveness),
store_alloc_in_cases(Goals0, Liveness0, ResumeVars0, ModuleInfo,
StackSlotInfo, Goals, _Liveness1).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Given a follow_map which
%
% 1 may contain entries for non-live variables,
%
% 2 may contain no entry for a live variable,
%
% 3 which may map two live variables to one lval, and/or
%
% 4 map an lval to the artificial location reg(r(-1)),
%
% generate a store map that maps every live variable to its own
% real location.
:- pred store_alloc_allocate_storage(list(prog_var), follow_vars,
stack_slot_info, store_map).
:- mode store_alloc_allocate_storage(in, in, in, out) is det.
store_alloc_allocate_storage(LiveVars, FollowVars, StackSlotInfo, StoreMap) :-
% This addresses point 1
map__keys(FollowVars, FollowKeys),
store_alloc_remove_nonlive(FollowKeys, LiveVars, FollowVars, StoreMap0),
% This addresses points 3 and 4
map__keys(StoreMap0, StoreVars),
set__init(SeenLvals0),
store_alloc_handle_conflicts_and_nonreal(StoreVars, 1, N,
SeenLvals0, SeenLvals, StoreMap0, StoreMap1),
% This addresses point 2
store_alloc_allocate_extras(LiveVars, N, SeenLvals, StackSlotInfo,
StoreMap1, StoreMap).
:- pred store_alloc_remove_nonlive(list(prog_var), list(prog_var),
store_map, store_map).
:- mode store_alloc_remove_nonlive(in, in, in, out) is det.
store_alloc_remove_nonlive([], _LiveVars, StoreMap, StoreMap).
store_alloc_remove_nonlive([Var | Vars], LiveVars, StoreMap0, StoreMap) :-
( list__member(Var, LiveVars) ->
StoreMap1 = StoreMap0
;
map__delete(StoreMap0, Var, StoreMap1)
),
store_alloc_remove_nonlive(Vars, LiveVars, StoreMap1, StoreMap).
:- pred store_alloc_handle_conflicts_and_nonreal(list(prog_var),
int, int, set(lval), set(lval), store_map, store_map).
:- mode store_alloc_handle_conflicts_and_nonreal(in, in, out, in, out, in, out)
is det.
store_alloc_handle_conflicts_and_nonreal([], N, N, SeenLvals, SeenLvals,
StoreMap, StoreMap).
store_alloc_handle_conflicts_and_nonreal([Var | Vars], N0, N,
SeenLvals0, SeenLvals, StoreMap0, StoreMap) :-
map__lookup(StoreMap0, Var, Lval),
(
( artificial_lval(Lval)
; set__member(Lval, SeenLvals0)
)
->
next_free_reg(N0, SeenLvals0, N1),
FinalLval = reg(r, N1),
map__det_update(StoreMap0, Var, FinalLval, StoreMap1)
;
N1 = N0,
FinalLval = Lval,
StoreMap1 = StoreMap0
),
set__insert(SeenLvals0, FinalLval, SeenLvals1),
store_alloc_handle_conflicts_and_nonreal(Vars, N1, N,
SeenLvals1, SeenLvals, StoreMap1, StoreMap).
:- pred store_alloc_allocate_extras(list(prog_var), int, set(lval),
stack_slot_info, store_map, store_map).
:- mode store_alloc_allocate_extras(in, in, in, in, in, out) is det.
store_alloc_allocate_extras([], _, _, _, StoreMap, StoreMap).
store_alloc_allocate_extras([Var | Vars], N0, SeenLvals0, StackSlotInfo,
StoreMap0, StoreMap) :-
(
map__contains(StoreMap0, Var)
->
% We have already allocated a slot for this variable.
N1 = N0,
StoreMap1 = StoreMap0,
SeenLvals1 = SeenLvals0
;
% We have not yet allocated a slot for this variable,
% which means it is not in the follow vars (if any).
StackSlotInfo = stack_slot_info(FollowVars, NumRealRRegs,
StackSlots),
(
map__search(StackSlots, Var, StackSlot),
\+ set__member(StackSlot, SeenLvals0),
(
FollowVars = yes
% If follow_vars was run, then the only
% reason why a var would not be in the
% follow_vars set is if it was supposed to
% be in its stack slot.
;
FollowVars = no,
% If follow_vars was not run, then we
% prefer to put the variable in a register,
% provided it is a real register.
next_free_reg(N0, SeenLvals0, TentativeReg),
TentativeReg =< NumRealRRegs
)
->
Locn = StackSlot,
N1 = N0
;
next_free_reg(N0, SeenLvals0, N1),
Locn = reg(r, N1)
),
map__det_insert(StoreMap0, Var, Locn, StoreMap1),
set__insert(SeenLvals0, Locn, SeenLvals1)
),
store_alloc_allocate_extras(Vars, N1, SeenLvals1, StackSlotInfo,
StoreMap1, StoreMap).
%-----------------------------------------------------------------------------%
% The follow_vars pass maps some variables r(-1) as a hint to the
% code generator to put them in any free register. Since store maps
% require real locations, we can't use such hints directly.
% For robustness, we check for N < 1 instead of N = -1.
:- pred artificial_lval(lval).
:- mode artificial_lval(in) is semidet.
artificial_lval(reg(_Type, Num)) :-
Num < 1.
%-----------------------------------------------------------------------------%
:- pred next_free_reg(int, set(lval), int).
:- mode next_free_reg(in, in, out) is det.
next_free_reg(N0, Values, N) :-
( set__member(reg(r, N0), Values) ->
N1 is N0 + 1,
next_free_reg(N1, Values, N)
;
N = N0
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%