File: mark_static_terms.m

package info (click to toggle)
mercury 0.10.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 21,984 kB
  • ctags: 11,923
  • sloc: objc: 187,634; ansic: 66,107; sh: 7,570; lisp: 1,568; cpp: 1,337; makefile: 614; perl: 511; awk: 274; asm: 252; exp: 32; xml: 12; fortran: 3; csh: 1
file content (210 lines) | stat: -rw-r--r-- 7,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%-----------------------------------------------------------------------------%
% Copyright (C) 2000 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.
%-----------------------------------------------------------------------------%

% This module traverses the HLDS, updating the `how_to_construct'
% field of construction unifications.  For each construction which
% can be done statically, i.e. whose arguments are all static,
% it replaces this field with `construct_statically'.
% This field is then used by the MLDS back-end to determine when it can
% generate static initialized constants rather than using
% new_object() MLDS statements.

% Main author: fjh.

:- module mark_static_terms.

:- interface.

:- import_module hlds_pred, hlds_module.

:- pred mark_static_terms(proc_info::in, module_info::in, proc_info::out)
	is det.

%-----------------------------------------------------------------------------%

:- implementation.
:- import_module prog_data, hlds_goal, hlds_data.
:- import_module map, list, bool.

%
% As we traverse the goal, we keep track of which variables are static at
% this point, and for each such variable, we keep information on how to
% construct it.
%
:- type static_info == map(prog_var, static_cons).

:- import_module hlds_goal.
:- import_module int, list, std_util, require.

mark_static_terms(Proc0, _ModuleInfo, Proc) :-
		% The ModuleInfo argument is there just for passes_aux
	proc_info_goal(Proc0, Goal0),
	map__init(StaticInfo0),
	goal_mark_static_terms(Goal0, Goal, StaticInfo0, _StaticInfo),
	proc_info_set_goal(Proc0, Goal, Proc).

:- pred goal_mark_static_terms(hlds_goal::in, hlds_goal::out,
		static_info::in, static_info::out) is det.

goal_mark_static_terms(GoalExpr0 - GoalInfo, GoalExpr - GoalInfo) -->
	goal_expr_mark_static_terms(GoalExpr0, GoalExpr).

:- pred goal_expr_mark_static_terms(hlds_goal_expr::in, hlds_goal_expr::out,
		static_info::in, static_info::out) is det.

goal_expr_mark_static_terms(conj(Goals0), conj(Goals), SI0, SI) :-
	conj_mark_static_terms(Goals0, Goals, SI0, SI).

goal_expr_mark_static_terms(par_conj(Goals0, SM), par_conj(Goals, SM),
		SI0, SI) :-
	% it's OK to treat parallel conjunctions as if they were
	% sequential here, since if we mark any variables as
	% static, the computation of those variables will be
	% done at compile time.
	conj_mark_static_terms(Goals0, Goals, SI0, SI).

goal_expr_mark_static_terms(disj(Goals0, B), disj(Goals, B), SI0, SI0) :-
	% we revert to the original static_info at the end of branched goals
	disj_mark_static_terms(Goals0, Goals, SI0).

goal_expr_mark_static_terms(switch(A, B, Cases0, D), switch(A, B, Cases, D),
		SI0, SI0) :-
	% we revert to the original static_info at the end of branched goals
	cases_mark_static_terms(Cases0, Cases, SI0).

goal_expr_mark_static_terms(not(Goal0), not(Goal), SI0, SI0) :-
	% we revert to the original static_info at the end of the negation
	goal_mark_static_terms(Goal0, Goal, SI0, _SI).

goal_expr_mark_static_terms(some(A, B, Goal0), some(A, B, Goal), SI0, SI) :-
	goal_mark_static_terms(Goal0, Goal, SI0, SI).

goal_expr_mark_static_terms(if_then_else(A, Cond0, Then0, Else0, E),
		if_then_else(A, Cond, Then, Else, E), SI0, SI0) :-
	% we run the Cond and the Then in sequence,
	% and we run the Else in parallel with that,
	% and then we throw away the static_infos we computed
	% and revert to the original static_info at the end,
	% since this was a branched goal.
	goal_mark_static_terms(Cond0, Cond, SI0, SI_Cond),
	goal_mark_static_terms(Then0, Then, SI_Cond, _SI_Then),
	goal_mark_static_terms(Else0, Else, SI0, _SI_Else).

goal_expr_mark_static_terms(call(A,B,C,D,E,F), call(A,B,C,D,E,F), SI, SI).

goal_expr_mark_static_terms(generic_call(A,B,C,D), generic_call(A,B,C,D),
	SI, SI).

goal_expr_mark_static_terms(unify(A,B,C, Unification0, E),
		unify(A,B,C, Unification, E), SI0, SI) :-
	unification_mark_static_terms(Unification0, Unification,
		SI0, SI).

goal_expr_mark_static_terms(pragma_foreign_code(A,B,C,D,E,F,G),
		pragma_foreign_code(A,B,C,D,E,F,G), SI, SI).

goal_expr_mark_static_terms(bi_implication(_, _), _, _, _) :-
	% these should have been expanded out by now
	error("fill_expr_slots: unexpected bi_implication").

:- pred conj_mark_static_terms(hlds_goals::in, hlds_goals::out,
		static_info::in, static_info::out) is det.
conj_mark_static_terms(Goals0, Goals) -->
	list__map_foldl(goal_mark_static_terms, Goals0, Goals).
	
:- pred disj_mark_static_terms(hlds_goals::in, hlds_goals::out,
		static_info::in) is det.
disj_mark_static_terms([], [], _).
disj_mark_static_terms([Goal0 | Goals0], [Goal | Goals], SI0) :-
	% we throw away the static_info obtained after each branch
	goal_mark_static_terms(Goal0, Goal, SI0, _SI),
	disj_mark_static_terms(Goals0, Goals, SI0).

:- pred cases_mark_static_terms(list(case)::in, list(case)::out,
		static_info::in) is det.
cases_mark_static_terms([], [], _SI0).
cases_mark_static_terms([Case0 | Cases0], [Case | Cases], SI0) :-
	Case0 = case(ConsId, Goal0),
	Case = case(ConsId, Goal),
	% we throw away the static_info obtained after each branch
	goal_mark_static_terms(Goal0, Goal, SI0, _SI),
	cases_mark_static_terms(Cases0, Cases, SI0).

:- pred unification_mark_static_terms(unification::in, unification::out,
		static_info::in, static_info::out) is det.
unification_mark_static_terms(Unification0, Unification,
		StaticVars0, StaticVars) :-
	(
		Unification0 = construct(Var, ConsId, ArgVars, D,
			HowToConstruct0, F, G),
		(
			% if all the arguments are static,
			% then the newly constructed variable
			% is static too
			CheckStaticArg = (pred(V::in, C::out) is semidet :-
				map__search(StaticVars0, V, C)),
			list__map(CheckStaticArg, ArgVars, StaticArgs)
		->
			HowToConstruct = construct_statically(StaticArgs),
			map__det_insert(StaticVars0, Var,
				static_cons(ConsId, ArgVars, StaticArgs),
				StaticVars)
		;
			HowToConstruct = HowToConstruct0,
			StaticVars = StaticVars0
		),
		( HowToConstruct = HowToConstruct0 ->
			% this is a minor optimization to improve the
			% efficiency of the compiler: don't bother
			% allocating memory if we don't need to
			Unification = Unification0
		;
			Unification = construct(Var, ConsId, ArgVars, D,
				HowToConstruct, F, G)
		)
	;
		Unification0 = deconstruct(_Var, _ConsId, _ArgVars, _UniModes,
			_CanFail, _CanCGC),
		Unification = Unification0,
		StaticVars = StaticVars0
/*****************
		(
			% if the variable being deconstructed is static,
			% and the deconstruction cannot fail,
			% then the newly extracted argument variables
			% are static too
			% (XXX is the "cannot fail" bit really necessary?)
			map__search(StaticVars0, Var, Data),
			CanFail = cannot_fail
		->
			XXX insert ArgVars into StaticVars0
		;
			StaticVars = StaticVars0
		)
*****************/
	;
		Unification0 = assign(TargetVar, SourceVar),
		Unification = Unification0,
		(
			% if the variable being assign from is static,
			% then the variable being assigned to is static too
			map__search(StaticVars0, SourceVar, Data)
		->
			map__det_insert(StaticVars0, TargetVar, Data,
				StaticVars)
		;
			StaticVars = StaticVars0
		)
	;
		Unification0 = simple_test(_, _),
		Unification = Unification0,
		StaticVars = StaticVars0
	;
		Unification0 = complicated_unify(_, _, _),
		Unification = Unification0,
		StaticVars = StaticVars0
	).