transform.icl 82 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
implementation module transform

3
import syntax, check, StdCompare, utilities; //, RWSDebug
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
4
5
6

::	LiftState =
	{	ls_var_heap		:: !.VarHeap
7
	,	ls_x :: !.LiftStateX
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
8
9
10
	,	ls_expr_heap	:: !.ExpressionHeap
	}
	
11
12
13
14
15
::	LiftStateX = {
		x_fun_defs :: !.{#FunDef},
		x_main_dcl_module_n :: !Int
	}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class lift a :: !a !*LiftState -> (!a, !*LiftState)

instance lift [a] | lift a
where
	lift l ls = mapSt lift l ls

instance lift (a,b) | lift a & lift b
where
	lift t ls = app2St (lift,lift) t ls

instance lift (Optional a) | lift a
where
	lift (Yes x) ls
		# (x, ls) = lift x ls
		= (Yes x, ls)
	lift no ls
		= (no, ls)
	
34
35
36
37
38
39
instance lift CheckedAlternative
where
	lift ca=:{ca_rhs} ls
		# (ca_rhs, ls) = lift ca_rhs ls
		= ({ ca & ca_rhs = ca_rhs }, ls)
	
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
instance lift Expression
where
	lift (FreeVar {fv_name,fv_info_ptr}) ls=:{ls_var_heap}
		#! var_info = sreadPtr fv_info_ptr ls_var_heap
		= case var_info of
			 VI_LiftedVariable var_info_ptr
			 	# (var_expr_ptr, ls_expr_heap) = newPtr EI_Empty ls.ls_expr_heap
			 	-> (Var { var_name = fv_name, var_info_ptr = var_info_ptr, var_expr_ptr = var_expr_ptr }, { ls & ls_expr_heap = ls_expr_heap})
			 _
			 	# (var_expr_ptr, ls_expr_heap) = newPtr EI_Empty ls.ls_expr_heap
			 	-> (Var { var_name = fv_name, var_info_ptr = fv_info_ptr, var_expr_ptr = var_expr_ptr }, { ls & ls_expr_heap = ls_expr_heap})
	lift (App app) ls
		# (app, ls) = lift app ls
		= (App app, ls)
	lift (expr @ exprs) ls
		# ((expr,exprs), ls) = lift (expr,exprs) ls
		= (expr @ exprs, ls)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
57
58
59
60
61
	lift (Let lad=:{let_strict_binds, let_lazy_binds, let_expr}) ls
		# (let_strict_binds, ls) = lift let_strict_binds ls
		  (let_lazy_binds, ls) = lift let_lazy_binds ls
		  (let_expr, ls) = lift let_expr ls
		= (Let {lad & let_strict_binds = let_strict_binds, let_lazy_binds = let_lazy_binds, let_expr = let_expr}, ls)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
	lift (Case case_expr) ls
		# (case_expr, ls) = lift case_expr ls
		= (Case case_expr, ls)
	lift (Selection is_unique expr selectors) ls
		# (selectors, ls) = lift selectors ls
		  (expr, ls) = lift expr ls
		= (Selection is_unique expr selectors, ls)
	lift (Update expr1 selectors expr2) ls
		# (selectors, ls) = lift selectors ls
		  (expr1, ls) = lift expr1 ls
		  (expr2, ls) = lift expr2 ls
		= (Update expr1 selectors expr2, ls)
	lift (RecordUpdate cons_symbol expression expressions) ls
		# (expression, ls) = lift expression ls
		  (expressions, ls) = lift expressions ls
		= (RecordUpdate cons_symbol expression expressions, ls)
	lift (TupleSelect symbol argn_nr expr) ls
		# (expr, ls) = lift expr ls
		= (TupleSelect symbol argn_nr expr, ls)
	lift (Lambda vars expr) ls
		# (expr, ls) = lift expr ls
		= (Lambda vars expr, ls)
	lift (MatchExpr opt_tuple cons_symb expr) ls
		# (expr, ls) = lift expr ls
		= (MatchExpr opt_tuple cons_symb expr, ls)
	lift expr ls
		= (expr, ls)

instance lift Selection
where
	lift (ArraySelection array_select expr_ptr index_expr) ls
		# (index_expr, ls) = lift index_expr ls
		= (ArraySelection array_select expr_ptr index_expr, ls)
	lift record_selection ls
		= (record_selection, ls)

instance lift App
where
	lift app=:{app_symb = app_symbol=:{symb_arity,symb_kind = SK_Function {glob_object,glob_module}}, app_args} ls
		# (app_args, ls) = lift app_args ls
102
103
104
		| glob_module == ls.ls_x.LiftStateX.x_main_dcl_module_n
//			#! fun_def = ls.ls_fun_defs.[glob_object]
			#! fun_def = ls.ls_x.x_fun_defs.[glob_object]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
			# {fun_info={fi_free_vars}} = fun_def
			  fun_lifted = length fi_free_vars
			| fun_lifted > 0
				# (app_args, ls_var_heap, ls_expr_heap) = add_free_variables fi_free_vars app_args ls.ls_var_heap ls.ls_expr_heap
				= ({ app & app_args = app_args, app_symb = { app_symbol & symb_arity = symb_arity + fun_lifted }},
						{ ls & ls_var_heap = ls_var_heap, ls_expr_heap = ls_expr_heap })
				= ({ app & app_args = app_args }, ls)
			= ({ app & app_args = app_args }, ls)
	where
		add_free_variables :: ![FreeVar] ![Expression] !u:VarHeap !*ExpressionHeap -> (![Expression],!u:VarHeap,!*ExpressionHeap)
		add_free_variables [] app_args var_heap expr_heap
			= (app_args, var_heap, expr_heap)
		add_free_variables [{fv_name, fv_info_ptr} : free_vars] app_args var_heap expr_heap
			#! var_info = sreadPtr fv_info_ptr var_heap
			= case var_info of
				VI_LiftedVariable var_info_ptr
				 	# (var_expr_ptr, expr_heap) = newPtr EI_Empty expr_heap
					-> add_free_variables free_vars [Var { var_name = fv_name, var_info_ptr = var_info_ptr, var_expr_ptr = var_expr_ptr } : app_args]
							var_heap expr_heap
				_
				 	# (var_expr_ptr, expr_heap) = newPtr EI_Empty expr_heap
					-> add_free_variables free_vars [Var { var_name = fv_name, var_info_ptr = fv_info_ptr, var_expr_ptr = var_expr_ptr } : app_args]
							var_heap expr_heap

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
	lift app=:{app_symb = app_symbol=:{symb_arity,symb_kind = SK_LocalMacroFunction glob_object}, app_args} ls
		# (app_args, ls) = lift app_args ls
//		#! fun_def = ls.ls_fun_defs.[glob_object]
		#! fun_def = ls.ls_x.x_fun_defs.[glob_object]
		# {fun_info={fi_free_vars}} = fun_def
		  fun_lifted = length fi_free_vars
		| fun_lifted > 0
			# (app_args, ls_var_heap, ls_expr_heap) = add_free_variables fi_free_vars app_args ls.ls_var_heap ls.ls_expr_heap
			= ({ app & app_args = app_args, app_symb = { app_symbol & symb_arity = symb_arity + fun_lifted }},
					{ ls & ls_var_heap = ls_var_heap, ls_expr_heap = ls_expr_heap })
			= ({ app & app_args = app_args }, ls)
	where
		add_free_variables :: ![FreeVar] ![Expression] !u:VarHeap !*ExpressionHeap -> (![Expression],!u:VarHeap,!*ExpressionHeap)
		add_free_variables [] app_args var_heap expr_heap
			= (app_args, var_heap, expr_heap)
		add_free_variables [{fv_name, fv_info_ptr} : free_vars] app_args var_heap expr_heap
			#! var_info = sreadPtr fv_info_ptr var_heap
			= case var_info of
				VI_LiftedVariable var_info_ptr
				 	# (var_expr_ptr, expr_heap) = newPtr EI_Empty expr_heap
					-> add_free_variables free_vars [Var { var_name = fv_name, var_info_ptr = var_info_ptr, var_expr_ptr = var_expr_ptr } : app_args]
							var_heap expr_heap
				_
				 	# (var_expr_ptr, expr_heap) = newPtr EI_Empty expr_heap
					-> add_free_variables free_vars [Var { var_name = fv_name, var_info_ptr = fv_info_ptr, var_expr_ptr = var_expr_ptr } : app_args]
							var_heap expr_heap

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
156
157
158
159
	lift app=:{app_args} ls
		# (app_args, ls) = lift app_args ls
		= ({ app & app_args = app_args }, ls)

160
161
162
163
164
165
instance lift LetBind
where
	lift bind=:{lb_src} ls
		# (lb_src, ls) = lift lb_src ls
		= ({ bind & lb_src = lb_src }, ls)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
instance lift (Bind a b) | lift a
where
	lift bind=:{bind_src} ls
		# (bind_src, ls) = lift bind_src ls
		= ({ bind & bind_src = bind_src }, ls)

instance lift Case
where
	lift kees=:{ case_expr,case_guards,case_default } ls
		# ((case_expr,(case_guards,case_default)), ls) = lift (case_expr,(case_guards,case_default)) ls
		= ({ kees & case_expr = case_expr,case_guards = case_guards, case_default = case_default }, ls)

instance lift CasePatterns
where
	lift (AlgebraicPatterns type patterns) ls
		# (patterns, ls) = lift patterns ls
		= (AlgebraicPatterns type patterns, ls)
	lift (BasicPatterns type patterns) ls
		# (patterns, ls) = lift patterns ls
		= (BasicPatterns type patterns, ls)
	lift (DynamicPatterns patterns) ls
		# (patterns, ls) = lift patterns ls
		= (DynamicPatterns patterns, ls)

instance lift AlgebraicPattern
where
	lift pattern=:{ap_expr} ls
		# (ap_expr, ls) = lift ap_expr ls
		= ({ pattern & ap_expr = ap_expr }, ls)

instance lift BasicPattern
where
	lift pattern=:{bp_expr} ls
		# (bp_expr, ls) = lift bp_expr ls
		= ({ pattern & bp_expr = bp_expr }, ls)

instance lift DynamicPattern
where
	lift pattern=:{dp_rhs} ls
		# (dp_rhs, ls) = lift dp_rhs ls
		= ({ pattern & dp_rhs = dp_rhs }, ls)

::	UnfoldState =
209
210
	{	us_var_heap				:: !.VarHeap
	,	us_symbol_heap			:: !.ExpressionHeap
211
212
	,	us_opt_type_heaps		:: !.Optional .TypeHeaps,
		us_cleanup_info			:: ![ExprInfoPtr]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
213
214
	}

215
216
217
218
219
::	UnfoldInfo =
	{	ui_handle_aci_free_vars	:: !AciFreeVarHandleMode,
		ui_convert_module_n :: !Int, // -1 if no conversion
		ui_conversion_table :: !Optional ConversionTable
	}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
220

221
:: AciFreeVarHandleMode = LeaveThem | RemoveThem | SubstituteThem
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
222

223
class unfold a :: !a !UnfoldInfo !*UnfoldState -> (!a, !*UnfoldState)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
224
225

unfoldVariable :: !BoundVar !*UnfoldState -> (!Expression, !*UnfoldState)
226
227
unfoldVariable var=:{var_name,var_info_ptr} us
	#! (var_info, us) = readVarInfo var_info_ptr us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
228
229
230
231
	= case var_info of 
		VI_Expression expr
			-> (expr, us)
		VI_Variable var_name var_info_ptr
232
		 	# (var_expr_ptr, us_symbol_heap) = newPtr EI_Empty us.us_symbol_heap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
233
			-> (Var {var_name = var_name, var_info_ptr = var_info_ptr, var_expr_ptr = var_expr_ptr}, { us & us_symbol_heap = us_symbol_heap})
234
235
236
237
238
		VI_Body fun_symb _ vars
			-> (App {	app_symb = fun_symb,
						app_args = [ Var { var_name=fv_name, var_info_ptr=fv_info_ptr, var_expr_ptr=nilPtr }
									\\ {fv_name,fv_info_ptr}<-vars],
						app_info_ptr = nilPtr }, us)
239
240
241
		VI_Dictionary app_symb app_args class_type
			# (new_class_type, us_opt_type_heaps) = substitute_class_types class_type us.us_opt_type_heaps
			  (new_info_ptr, us_symbol_heap) = newPtr (EI_DictionaryType new_class_type) us.us_symbol_heap
242
243
			-> (App { app_symb = app_symb, app_args = app_args, app_info_ptr = new_info_ptr },
				{ us & us_opt_type_heaps = us_opt_type_heaps, us_symbol_heap = us_symbol_heap })
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
244
245
		_
			-> (Var var, us)
246
247
248
249
  where
	substitute_class_types class_types no=:No
		= (class_types, no)
	substitute_class_types class_types (Yes type_heaps)
Martin Wierich's avatar
Martin Wierich committed
250
		# (_, new_class_types, type_heaps) = substitute class_types type_heaps
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		= (new_class_types, Yes type_heaps)

readVarInfo var_info_ptr us
	#! var_info = sreadPtr var_info_ptr us.us_var_heap
	= case var_info of
		VI_Extended _ original	-> (original, us)
		_						-> (var_info, us)

writeVarInfo :: VarInfoPtr VarInfo *VarHeap -> *VarHeap
writeVarInfo var_info_ptr new_var_info var_heap
	# (old_var_info, var_heap) = readPtr var_info_ptr var_heap
	= case old_var_info of
		VI_Extended extensions _	-> writePtr var_info_ptr (VI_Extended extensions new_var_info) var_heap
		_							-> writePtr var_info_ptr new_var_info var_heap

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
266
267
instance unfold Expression
where
268
	unfold (Var var) ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
269
		= unfoldVariable var us
270
271
	unfold (App app) ui us
		# (app, us) = unfold app ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
272
		= (App app, us)
273
274
	unfold (expr @ exprs) ui us
		# ((expr,exprs), us) = unfold (expr,exprs) ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
275
		= (expr @ exprs, us)
276
277
	unfold (Let lad) ui us
		# (lad, us) = unfold lad ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
278
		= (Let lad, us)
279
280
	unfold (Case case_expr) ui us
		# (case_expr, us) = unfold case_expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
281
		= (Case case_expr, us)
282
283
	unfold (Selection is_unique expr selectors) ui us
		# ((expr, selectors), us) = unfold (expr, selectors) ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
284
		= (Selection is_unique expr selectors, us)
285
286
	unfold (Update expr1 selectors expr2) ui us
		# (((expr1, expr2), selectors), us) = unfold ((expr1, expr2), selectors) ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
287
		= (Update expr1 selectors expr2, us)
288
289
	unfold (RecordUpdate cons_symbol expression expressions) ui us
		# ((expression, expressions), us) = unfold (expression, expressions) ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
290
		= (RecordUpdate cons_symbol expression expressions, us)
291
292
	unfold (TupleSelect symbol argn_nr expr) ui us
		# (expr, us) = unfold expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
293
		= (TupleSelect symbol argn_nr expr, us)
294
295
	unfold (Lambda vars expr) ui us
		# (expr, us) = unfold expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
296
		= (Lambda vars expr, us)
297
298
	unfold (MatchExpr opt_tuple cons_symb expr) ui us
		# (expr, us) = unfold expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
299
		= (MatchExpr opt_tuple cons_symb expr, us)
300
301
	unfold (DynamicExpr expr) ui us
		# (expr, us) = unfold expr ui us
Martin Wierich's avatar
Martin Wierich committed
302
		= (DynamicExpr expr, us)
303
	unfold expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
304
305
		= (expr, us)

Martin Wierich's avatar
Martin Wierich committed
306
307
instance unfold DynamicExpr
where
308
309
	unfold expr=:{dyn_expr} ui us
		# (dyn_expr, us) = unfold dyn_expr ui us
Martin Wierich's avatar
Martin Wierich committed
310
311
		= ({ expr & dyn_expr = dyn_expr }, us)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
312
313
instance unfold Selection
where
314
	unfold (ArraySelection array_select expr_ptr index_expr) ui us=:{us_symbol_heap}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
315
		# (new_ptr, us_symbol_heap) = newPtr EI_Empty us_symbol_heap
316
		  (index_expr, us) = unfold index_expr ui { us & us_symbol_heap = us_symbol_heap}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
317
		= (ArraySelection array_select new_ptr index_expr, us)
318
	unfold (DictionarySelection var selectors expr_ptr index_expr) ui us=:{us_symbol_heap}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
319
		# (new_ptr, us_symbol_heap) = newPtr EI_Empty us_symbol_heap
320
		  (index_expr, us) = unfold index_expr ui { us & us_symbol_heap = us_symbol_heap}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
321
322
323
324
325
326
		  (var_expr, us) = unfoldVariable var us
		= case var_expr of 
			App {app_symb={symb_kind= SK_Constructor _ }, app_args}
				# [RecordSelection _ field_index:_] = selectors
				  (App { app_symb = {symb_name, symb_kind = SK_Function array_select}}) =  app_args !! field_index
				-> (ArraySelection { array_select & glob_object = { ds_ident = symb_name, ds_arity = 2, ds_index = array_select.glob_object}}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
327
							new_ptr index_expr, us)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
328
			Var var
Sjaak Smetsers's avatar
Sjaak Smetsers committed
329
				-> (DictionarySelection var selectors new_ptr index_expr, us)
330
331
	unfold record_selection ui us
		= (record_selection, us)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
332
333
334

instance unfold FreeVar
where
335
	unfold fv=:{fv_info_ptr,fv_name} ui us=:{us_var_heap}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
336
337
338
339
340
		# (new_info_ptr, us_var_heap) = newPtr VI_Empty us_var_heap
		= ({ fv & fv_info_ptr = new_info_ptr }, { us & us_var_heap = writePtr fv_info_ptr (VI_Variable fv_name new_info_ptr) us_var_heap })

instance unfold App
where
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
	unfold app=:{app_symb={symb_kind}, app_args, app_info_ptr} ui=:{ui_convert_module_n,ui_conversion_table} us
		= case symb_kind of
			SK_Function 	{glob_module,glob_object}
				| ui_convert_module_n==glob_module 
					# (Yes conversion_table) = ui_conversion_table
					# app={app & app_symb.symb_kind=SK_Function {glob_module=glob_module,glob_object=conversion_table.[cFunctionDefs].[glob_object]}}
					-> unfold_function_app app ui us
					-> unfold_function_app app ui us
			SK_Macro {glob_module,glob_object}
				| ui_convert_module_n==glob_module
					# (Yes conversion_table) = ui_conversion_table
					# app={app & app_symb.symb_kind=SK_Macro {glob_module=glob_module,glob_object=conversion_table.[cMacroDefs].[glob_object]}}
					-> unfold_function_app app ui us
					-> unfold_function_app app ui us
			SK_OverloadedFunction {glob_module,glob_object}
				| ui_convert_module_n==glob_module
					# (Yes conversion_table) = ui_conversion_table
					# app={app & app_symb.symb_kind=SK_OverloadedFunction {glob_module=glob_module,glob_object=conversion_table.[cFunctionDefs].[glob_object]}}
					-> unfold_function_app app ui us
					-> unfold_function_app app ui us
			SK_LocalMacroFunction _
				-> unfold_function_app app ui us
			SK_Constructor _
				| not (isNilPtr app_info_ptr)
					# (app_info, us_symbol_heap) = readPtr app_info_ptr us.us_symbol_heap
					  (new_app_info, us_opt_type_heaps) = substitute_EI_DictionaryType app_info us.us_opt_type_heaps
					  (new_info_ptr, us_symbol_heap) = newPtr new_app_info us_symbol_heap
					  us={ us & us_symbol_heap = us_symbol_heap, us_opt_type_heaps = us_opt_type_heaps }
					  (app_args, us) = unfold app_args ui us
					-> ({ app & app_args = app_args, app_info_ptr = new_info_ptr}, us) 
					# (app_args, us) = unfold app_args ui us
					-> ({ app & app_args = app_args}, us)
			_
				# (app_args, us) = unfold app_args ui us
				-> ({ app & app_args = app_args, app_info_ptr = nilPtr}, us) 
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
376
	where
377
378
379
380
381
		unfold_function_app app=:{app_args, app_info_ptr} ui us
			# (new_info_ptr, us_symbol_heap) = newPtr EI_Empty us.us_symbol_heap
			# us={ us & us_symbol_heap = us_symbol_heap }
			# (app_args, us) = unfold app_args ui us
			= ({ app & app_args = app_args, app_info_ptr = new_info_ptr}, us) 
Sjaak Smetsers's avatar
Sjaak Smetsers committed
382

383
		substitute_EI_DictionaryType (EI_DictionaryType class_type) (Yes type_heaps)
Martin Wierich's avatar
Martin Wierich committed
384
			# (_, new_class_type, type_heaps) = substitute class_type type_heaps
385
386
			= (EI_DictionaryType new_class_type, Yes type_heaps)
		substitute_EI_DictionaryType x opt_type_heaps
387
			= (x, opt_type_heaps)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
388

389
390
instance unfold LetBind
where
391
392
	unfold bind=:{lb_src} ui us
		# (lb_src, us) = unfold lb_src ui us
393
394
		= ({ bind & lb_src = lb_src }, us)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
395
396
instance unfold (Bind a b) | unfold a
where
397
398
	unfold bind=:{bind_src} ui us
		# (bind_src, us) = unfold bind_src ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
399
400
401
402
		= ({ bind & bind_src = bind_src }, us)

instance unfold Case
where
403
	unfold kees=:{ case_expr,case_guards,case_default,case_info_ptr} ui us=:{us_cleanup_info}
404
405
406
		# (old_case_info, us_symbol_heap) = readPtr case_info_ptr us.us_symbol_heap
		  (new_case_info, us_opt_type_heaps) = substitute_let_or_case_type old_case_info us.us_opt_type_heaps
		  (new_info_ptr, us_symbol_heap) = newPtr new_case_info us_symbol_heap
407
408
409
		  us_cleanup_info = case old_case_info of
								EI_Extended _ _	-> [new_info_ptr:us_cleanup_info]
								_				-> us_cleanup_info
410
		  us = { us & us_symbol_heap = us_symbol_heap, us_opt_type_heaps = us_opt_type_heaps, us_cleanup_info=us_cleanup_info }
411
		  ((case_guards,case_default), us) = unfold (case_guards,case_default) ui us
412
413
414
		  (case_expr, us) = update_active_case_info_and_unfold case_expr new_info_ptr us
		= ({ kees & case_expr = case_expr,case_guards = case_guards, case_default = case_default, case_info_ptr =  new_info_ptr}, us)
	where
415
		update_active_case_info_and_unfold case_expr=:(Var {var_info_ptr}) case_info_ptr us
416
417
			# (case_info, us_symbol_heap) = readPtr case_info_ptr us.us_symbol_heap
			  us = { us & us_symbol_heap = us_symbol_heap }
418
419
			= case case_info of
				EI_Extended (EEI_ActiveCase aci=:{aci_free_vars}) ei
420
					#!(new_aci_free_vars, us) = case ui.ui_handle_aci_free_vars of
421
422
423
424
425
426
													LeaveThem		-> (aci_free_vars, us)
													RemoveThem		-> (No, us)
													SubstituteThem	-> case aci_free_vars of
																		No		-> (No, us)
																		Yes fvs	# (fvs_subst, us) = mapSt unfoldBoundVar fvs us
																				-> (Yes fvs_subst, us)
427
428
					  (var_info, us_var_heap) = readPtr var_info_ptr us.us_var_heap
					  us = { us & us_var_heap = us_var_heap }
429
430
431
432
433
					-> case var_info of
						VI_Body fun_symb {tb_args, tb_rhs} new_aci_params
							# tb_args_ptrs = [ fv_info_ptr \\ {fv_info_ptr}<-tb_args ] 
							  (original_bindings, us_var_heap) = mapSt readPtr tb_args_ptrs us.us_var_heap
							  us_var_heap = fold2St bind tb_args_ptrs new_aci_params us_var_heap
434
							  (tb_rhs, us) = unfold tb_rhs ui { us & us_var_heap = us_var_heap }
435
436
437
438
439
440
441
							  us_var_heap = fold2St writePtr tb_args_ptrs original_bindings us.us_var_heap
							  new_aci = { aci & aci_params = new_aci_params, aci_opt_unfolder = Yes fun_symb, aci_free_vars = new_aci_free_vars }
							  new_eei = (EI_Extended (EEI_ActiveCase new_aci) ei)
							  us_symbol_heap = writePtr case_info_ptr new_eei us.us_symbol_heap
							-> (tb_rhs, { us & us_var_heap = us_var_heap, us_symbol_heap = us_symbol_heap })
						_	# new_eei = EI_Extended (EEI_ActiveCase { aci & aci_free_vars = new_aci_free_vars }) ei
							  us_symbol_heap = writePtr case_info_ptr new_eei us.us_symbol_heap
442
443
							-> unfold case_expr ui { us & us_symbol_heap = us_symbol_heap }
				_	-> unfold case_expr ui us	
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
		  where 
			// XXX consider to store BoundVars in VI_Body
			bind fv_info_ptr {fv_name=name, fv_info_ptr=info_ptr} var_heap
				= writeVarInfo fv_info_ptr (VI_Expression (Var {var_name=name, var_info_ptr=info_ptr, var_expr_ptr = nilPtr})) var_heap
/*
			bind ({fv_info_ptr}, var_bound_var) var_heap
				= writeVarInfo fv_info_ptr (VI_Expression var_bound_var) var_heap
*/

/*		update_active_case_info_and_unfold case_expr=:(Var {var_info_ptr}) case_info_ptr us
			#! var_info = sreadPtr var_info_ptr us.us_var_heap
			= case var_info of
				VI_Body fun_symb fun_body new_aci_var_info_ptr
					# (fun_body, us) = unfold fun_body us
					  (EI_Extended (EEI_ActiveCase aci) ei, us_symbol_heap) = readPtr case_info_ptr us.us_symbol_heap
					  new_aci = { aci & aci_var_info_ptr = new_aci_var_info_ptr, aci_opt_unfolder = Yes fun_symb }
					  us_symbol_heap = writePtr case_info_ptr (EI_Extended (EEI_ActiveCase new_aci) ei) us_symbol_heap
					-> (fun_body, { us & us_symbol_heap = us_symbol_heap })
				_	-> unfold case_expr us
*/
		update_active_case_info_and_unfold case_expr _ us
465
			= unfold case_expr ui us
466
467

		unfoldBoundVar {var_info_ptr} us
468
469
			# (VI_Expression (Var act_var), us_var_heap) = readPtr var_info_ptr us.us_var_heap
			= (act_var, { us & us_var_heap = us_var_heap })
470

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
471
472
instance unfold Let
where
473
	unfold lad=:{let_strict_binds, let_lazy_binds, let_expr, let_info_ptr} ui us
Sjaak Smetsers's avatar
Sjaak Smetsers committed
474
475
		# (let_strict_binds, us) = copy_bound_vars let_strict_binds us
		# (let_lazy_binds, us) = copy_bound_vars let_lazy_binds us
476
477
478
		# (let_strict_binds, us) = unfold let_strict_binds ui us
		# (let_lazy_binds, us) = unfold let_lazy_binds ui us
		# (let_expr, us) = unfold let_expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
479
		  (old_let_info, us_symbol_heap) = readPtr let_info_ptr us.us_symbol_heap
480
481
		  (new_let_info, us_opt_type_heaps) = substitute_let_or_case_type old_let_info us.us_opt_type_heaps
		  (new_info_ptr, us_symbol_heap) = newPtr new_let_info us_symbol_heap
Sjaak Smetsers's avatar
Sjaak Smetsers committed
482
		= ({lad & let_strict_binds = let_strict_binds, let_lazy_binds = let_lazy_binds, let_expr = let_expr, let_info_ptr = new_info_ptr},
483
			{ us & us_symbol_heap = us_symbol_heap, us_opt_type_heaps = us_opt_type_heaps })
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
484
		where
485
			copy_bound_vars [bind=:{lb_dst} : binds] us
486
				# (lb_dst, us) = unfold lb_dst ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
487
				  (binds, us) = copy_bound_vars binds us
488
				= ([ {bind & lb_dst = lb_dst} : binds ], us)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
489
490
491
			copy_bound_vars [] us
				= ([], us)

492
493
494
495
496
497
substitute_let_or_case_type	expr_info No
	= (expr_info, No)
substitute_let_or_case_type	(EI_Extended extensions expr_info) yes_type_heaps
	# (new_expr_info, yes_type_heaps) = substitute_let_or_case_type expr_info yes_type_heaps
	= (EI_Extended extensions new_expr_info, yes_type_heaps)
substitute_let_or_case_type	(EI_CaseType case_type) (Yes type_heaps)
Martin Wierich's avatar
Martin Wierich committed
498
	# (_, new_case_type, type_heaps) = substitute case_type type_heaps
499
500
501
	= (EI_CaseType new_case_type, Yes type_heaps)
//	= (EI_CaseType case_type, Yes type_heaps)
substitute_let_or_case_type	(EI_LetType let_type) (Yes type_heaps)
Martin Wierich's avatar
Martin Wierich committed
502
	# (_, new_let_type, type_heaps) = substitute let_type type_heaps
503
504
	= (EI_LetType new_let_type, Yes type_heaps)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
505
506
instance unfold CasePatterns
where
507
508
	unfold (AlgebraicPatterns type patterns) ui us
		# (patterns, us) = unfold patterns ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
509
		= (AlgebraicPatterns type patterns, us)
510
511
	unfold (BasicPatterns type patterns) ui us
		# (patterns, us) = unfold patterns ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
512
		= (BasicPatterns type patterns, us)
513
514
	unfold (DynamicPatterns patterns) ui us
		# (patterns, us) = unfold patterns ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
515
516
517
518
		= (DynamicPatterns patterns, us)

instance unfold BasicPattern
where
519
520
	unfold guard=:{bp_expr} ui us
		# (bp_expr, us) = unfold bp_expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
521
522
523
524
		= ({ guard & bp_expr = bp_expr }, us)

instance unfold AlgebraicPattern
where
525
526
527
	unfold guard=:{ap_vars,ap_expr} ui us
		# (ap_vars, us) = unfold ap_vars ui us
		  (ap_expr, us) = unfold ap_expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
528
529
530
531
		= ({ guard & ap_vars = ap_vars, ap_expr = ap_expr }, us)

instance unfold DynamicPattern
where
532
533
534
	unfold guard=:{dp_var,dp_rhs} ui us
		# (dp_var, us) = unfold dp_var ui us
		  (dp_rhs, us) = unfold dp_rhs ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
535
536
		= ({ guard & dp_var = dp_var, dp_rhs = dp_rhs }, us)

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
instance unfold [a] | unfold a
where
	unfold l ui us
		// = mapSt unfold l ui us
		= map_st l us
		where
			map_st [x : xs] s
			 	# (x, s) = unfold x ui s
				  (xs, s) = map_st xs s
				#! s = s
				= ([x : xs], s)
			map_st [] s
			 	= ([], s)

instance unfold (a,b) | unfold a & unfold b
where
//	unfold t ui us = app2St (unfold,unfold) t ui us
	unfold (a,b) ui us
		# (a,us) = unfold a ui us
		# (b,us) = unfold b ui us
		= ((a,b),us)

instance unfold (Optional a) | unfold a
where
	unfold (Yes x) ui us
		# (x, us) = unfold x ui us
		= (Yes x, us)
	unfold no ui us
		= (no, us)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
567
568
569
570
571
572
573
574
575
576
577
updateFunctionCalls :: ![FunCall] ![FunCall] !*{# FunDef} !*SymbolTable
	-> (![FunCall], !*{# FunDef}, !*SymbolTable)
updateFunctionCalls calls collected_calls fun_defs symbol_table
	= foldSt add_function_call calls (collected_calls, fun_defs, symbol_table)
where
	add_function_call fc (collected_calls, fun_defs, symbol_table)
		# ({fun_symb}, fun_defs) = fun_defs![fc.fc_index]
		  (collected_calls, symbol_table) = examineFunctionCall fun_symb fc (collected_calls, symbol_table) 
		= (collected_calls, fun_defs, symbol_table)

examineFunctionCall {id_info} fc=:{fc_index} (calls, symbol_table)
578
	# (entry, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
579
580
581
582
583
584
585
586
587
	= case entry.ste_kind of
		STE_Called indexes
			| isMember fc_index indexes
				-> (calls, symbol_table)
				-> ([ fc : calls ], symbol_table <:= (id_info, { entry & ste_kind = STE_Called [ fc_index : indexes ]}))
		_
			-> ( [ fc : calls ], symbol_table <:=
					(id_info, { ste_kind = STE_Called [fc_index], ste_index = NoIndex, ste_def_level = NotALevel, ste_previous = entry }))

588
unfoldMacro :: !FunDef ![Expression] !*ExpandInfo -> (!Expression, !*ExpandInfo)
589
unfoldMacro {fun_body = TransformedBody {tb_args,tb_rhs}, fun_info = {fi_calls},fun_kind,fun_symb} args (calls, es=:{es_var_heap,es_symbol_heap, es_symbol_table,es_fun_defs,es_expand_in_imp_module,	es_main_dcl_module_n,es_dcl_modules})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
590
	# (let_binds, var_heap) = bind_expressions tb_args args [] es_var_heap
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
	# us = { us_symbol_heap = es_symbol_heap, us_var_heap = var_heap, us_opt_type_heaps = No,us_cleanup_info = []}
	# (result_expr,dcl_modules,us_symbol_heap,us_var_heap) = unfold_and_convert tb_rhs es_dcl_modules us
		with
			unfold_and_convert tb_rhs dcl_modules us
				# is_def_macro=case fun_kind of FK_DefMacro->True; _->False
				| es_expand_in_imp_module && is_def_macro
					# (dcl_mod,dcl_modules) = dcl_modules![es_main_dcl_module_n]
					# ui = {ui_handle_aci_free_vars = RemoveThem,  ui_convert_module_n = es_main_dcl_module_n, ui_conversion_table=dcl_mod.dcl_conversions }
					# (result_expr,{us_symbol_heap,us_var_heap})= unfold tb_rhs ui us
					= (result_expr,dcl_modules,us_symbol_heap,us_var_heap)

					# ui = {ui_handle_aci_free_vars = RemoveThem,  ui_convert_module_n = -1 ,ui_conversion_table=No }
					# (result_expr,{us_symbol_heap,us_var_heap})= unfold tb_rhs ui us
					= (result_expr,dcl_modules,us_symbol_heap,us_var_heap)
	# (calls, fun_defs, es_symbol_table) = updateFunctionCalls fi_calls calls es_fun_defs es_symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
606
	| isEmpty let_binds
607
		= (result_expr, (calls, { es & es_var_heap = us_var_heap, es_symbol_heap = us_symbol_heap, es_symbol_table = es_symbol_table, es_fun_defs=fun_defs,es_dcl_modules=dcl_modules }))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
608
		#  (new_info_ptr, us_symbol_heap) = newPtr EI_Empty us_symbol_heap
609
		= (Let { let_strict_binds = [], let_lazy_binds = let_binds, let_expr = result_expr, let_info_ptr = new_info_ptr, let_expr_position = NoPos }, 
610
					(calls, { es & es_var_heap = us_var_heap, es_symbol_heap = us_symbol_heap, es_symbol_table = es_symbol_table,es_fun_defs=fun_defs,es_dcl_modules=dcl_modules }))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
where
	bind_expressions [var : vars] [expr : exprs] binds var_heap
		# (binds, var_heap) = bind_expressions vars exprs binds var_heap
		= bind_expression var expr binds var_heap
	bind_expressions _ _ binds var_heap
		= (binds, var_heap)

	bind_expression {fv_count} expr binds var_heap
		| fv_count == 0
			= (binds, var_heap)
	bind_expression {fv_info_ptr} (Var {var_name,var_info_ptr}) binds var_heap
		= (binds, writePtr fv_info_ptr (VI_Variable var_name var_info_ptr) var_heap)
	bind_expression {fv_name,fv_info_ptr,fv_count} expr binds var_heap
		| fv_count == 1
			= (binds, writePtr fv_info_ptr (VI_Expression expr) var_heap)
		# (new_info, var_heap) = newPtr VI_Empty var_heap
		  new_var = { fv_name = fv_name, fv_def_level = NotALevel, fv_info_ptr = new_info, fv_count = 0 }
628
		= ([{ lb_src = expr, lb_dst = new_var, lb_position = NoPos} : binds], writePtr fv_info_ptr (VI_Variable fv_name new_info) var_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

::	Group =
	{	group_members	:: ![Int]
//	,	group_number	:: !Int
	}

::	PartitioningInfo = 
	{	pi_symbol_table	:: !.SymbolTable
//	,	pi_marks		:: !.{# Int}
	,	pi_var_heap		:: !.VarHeap
	,	pi_symbol_heap	:: !.ExpressionHeap
	,	pi_error		:: !.ErrorAdmin
	,	pi_next_num		:: !Int
	,	pi_next_group	:: !Int
	,	pi_groups		:: ![[Int]]
	,	pi_deps			:: ![Int]
	}

NotChecked :== -1	

649
650
partitionateMacros :: !IndexRange !Index !PredefinedSymbol !*{# FunDef} !*{# DclModule} !*VarHeap !*ExpressionHeap !*SymbolTable !*ErrorAdmin
	-> (!*{# FunDef}, !.{# DclModule}, !*VarHeap, !*ExpressionHeap, !*SymbolTable, !*ErrorAdmin )
651
partitionateMacros {ir_from,ir_to} mod_index alias_dummy fun_defs modules var_heap symbol_heap symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
652
653
654
655
	#! max_fun_nr = size fun_defs
	# partitioning_info = { pi_var_heap = var_heap, pi_symbol_heap = symbol_heap,
							pi_symbol_table = symbol_table,
							pi_error = error, pi_deps = [], pi_next_num = 0, pi_next_group = 0, pi_groups = [] }
656
	  (fun_defs, modules, {pi_symbol_table, pi_var_heap, pi_symbol_heap, pi_error, pi_next_group, pi_groups, pi_deps})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
657
	  		= iFoldSt (pationate_macro mod_index max_fun_nr) ir_from ir_to (fun_defs, modules, partitioning_info)
Martin Wierich's avatar
Martin Wierich committed
658
	= (foldSt reset_body_of_rhs_macro pi_deps fun_defs, modules, pi_var_heap, pi_symbol_heap, pi_symbol_table, pi_error)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
659
660
661
662
663
664
665
666
667
668
669
where
	reset_body_of_rhs_macro macro_index macro_defs
		# (macro_def, macro_defs) = macro_defs![macro_index]
		= case macro_def.fun_body of
			RhsMacroBody body
				-> { macro_defs & [macro_index] = { macro_def & fun_body = CheckedBody body }}
			_
				-> macro_defs

	pationate_macro mod_index max_fun_nr macro_index (macro_defs, modules, pi)
		# (macro_def, macro_defs) = macro_defs![macro_index]
670
671
//		| macro_def.fun_kind == FK_Macro
		| case macro_def.fun_kind of FK_DefMacro->True ; FK_ImpMacro->True; _ -> False
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
672
673
		 	= case macro_def.fun_body of
		 		CheckedBody body
Sjaak Smetsers's avatar
Sjaak Smetsers committed
674
675
			  		# macros_modules_pi = foldSt (visit_macro mod_index max_fun_nr) macro_def.fun_info.fi_calls
			  					({ macro_defs & [macro_index] = { macro_def & fun_body = PartioningMacro }}, modules, pi)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
676
677
678
679
680
681
682
683
684
685
					-> expand_simple_macro mod_index macro_index macro_def macros_modules_pi
		 		PartioningMacro
		  			# identPos = newPosition macro_def.fun_symb macro_def.fun_pos
		 			-> (macro_defs,  modules, { pi &  pi_error = checkError macro_def.fun_symb "recursive macro definition" (setErrorAdmin identPos pi.pi_error)  })
		 		_
		 			-> (macro_defs, modules, pi)
			= (macro_defs, modules, pi)

	visit_macro mod_index max_fun_nr {fc_index} macros_modules_pi
		= pationate_macro mod_index max_fun_nr fc_index macros_modules_pi
686
	
687
	expand_simple_macro mod_index macro_index macro=:{fun_body = CheckedBody body, fun_info, fun_symb, fun_pos,fun_kind}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
688
689
			(macro_defs, modules, pi=:{pi_symbol_table,pi_symbol_heap,pi_var_heap,pi_error})
		| macros_are_simple fun_info.fi_calls macro_defs
Martin Wierich's avatar
Martin Wierich committed
690
		  	# identPos = newPosition fun_symb fun_pos
691
			# expand_in_imp_module=case fun_kind of FK_ImpMacro->True; _ -> False
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
692
			  es = { es_symbol_table = pi_symbol_table, es_var_heap = pi_var_heap,
693
					 es_symbol_heap = pi_symbol_heap, es_error = setErrorAdmin identPos pi_error,
694
695
					 es_fun_defs=macro_defs, es_main_dcl_module_n = mod_index, es_dcl_modules=modules,
					 es_expand_in_imp_module=expand_in_imp_module
696
					  }
697
			# (tb_args, tb_rhs, local_vars, fi_calls, {es_symbol_table, es_var_heap, es_symbol_heap, es_error,es_dcl_modules,es_fun_defs})
698
					= expandMacrosInBody [] body alias_dummy es
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
699
700
			  macro = { macro & fun_body = TransformedBody { tb_args = tb_args, tb_rhs = tb_rhs},
			  			fun_info = { fun_info & fi_calls = fi_calls, fi_local_vars = local_vars }}
701
			= ({ es_fun_defs & [macro_index] = macro }, es_dcl_modules,
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
702
					{ pi & pi_symbol_table = es_symbol_table, pi_symbol_heap = es_symbol_heap, pi_var_heap = es_var_heap, pi_error = es_error })
Martin Wierich's avatar
Martin Wierich committed
703
			# pi = { pi & pi_deps = [macro_index:pi.pi_deps] }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
704
705
706
707
708
709
710
711
			= ({ macro_defs & [macro_index] = { macro & fun_body = RhsMacroBody body }}, modules, pi)

	macros_are_simple [] macro_defs
		= True
	macros_are_simple [ {fc_index} : calls ] macro_defs
		# {fun_kind,fun_body} = macro_defs.[fc_index]
		= is_a_pattern_macro fun_kind fun_body && macros_are_simple calls macro_defs
	where
712
713
714
		is_a_pattern_macro FK_DefMacro (TransformedBody {tb_args})
			= True
		is_a_pattern_macro FK_ImpMacro (TransformedBody {tb_args})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
715
716
717
			= True
		is_a_pattern_macro _ _
			= False
718
719
720

partitionateAndLiftFunctions :: ![IndexRange] !Index !PredefinedSymbol !*{# FunDef} !*{# DclModule} !*VarHeap !*ExpressionHeap !*SymbolTable !*ErrorAdmin
	-> (!*{! Group}, !*{# FunDef}, !.{# DclModule}, !*VarHeap, !*ExpressionHeap,  !*SymbolTable, !*ErrorAdmin )
721
partitionateAndLiftFunctions ranges main_dcl_module_n alias_dummy fun_defs modules var_heap symbol_heap symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
722
	#! max_fun_nr = size fun_defs
723
724
	# partitioning_info = {	pi_var_heap = var_heap, pi_symbol_heap = symbol_heap, pi_symbol_table = symbol_table,
									pi_error = error, pi_deps = [], pi_next_num = 0, pi_next_group = 0, pi_groups = [] }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
725
	  (fun_defs, modules, {pi_groups, pi_symbol_table, pi_var_heap, pi_symbol_heap, pi_error})
726
	  		= foldSt (partitionate_functions main_dcl_module_n max_fun_nr) ranges (fun_defs, modules, partitioning_info)
727
728
729
	# (reversed_pi_groups,fun_defs) = remove_macros_from_groups_and_reverse pi_groups fun_defs []
	# groups = { {group_members = group} \\ group <- reversed_pi_groups }
//	# groups = { {group_members = group} \\ group <- reverse pi_groups }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
730
731
	= (groups, fun_defs, modules, pi_var_heap, pi_symbol_heap, pi_symbol_table, pi_error)
where
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
	remove_macros_from_groups_and_reverse [group:groups] fun_defs result_groups
		# (group,fun_defs) = remove_macros_from_group group fun_defs
		= case group of
			[]	-> remove_macros_from_groups_and_reverse groups fun_defs result_groups
			_	-> remove_macros_from_groups_and_reverse groups fun_defs [group:result_groups]
	where
		remove_macros_from_group [fun:funs] fun_defs
			# (funs,fun_defs)=remove_macros_from_group funs fun_defs
			| fun_defs.[fun].fun_info.fi_group_index<NoIndex
				= (funs,fun_defs)
				= ([fun:funs],fun_defs)
		remove_macros_from_group [] fun_defs
			= ([],fun_defs);
	remove_macros_from_groups_and_reverse [] fun_defs result_groups
		= (result_groups,fun_defs);

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
	partitionate_functions mod_index max_fun_nr {ir_from,ir_to} funs_modules_pi
		= iFoldSt (partitionate_global_function mod_index max_fun_nr) ir_from ir_to funs_modules_pi
		
	partitionate_global_function mod_index max_fun_nr fun_index funs_modules_pi
		# (_, funs_modules_pi) = partitionate_function mod_index max_fun_nr fun_index funs_modules_pi
		= funs_modules_pi

	partitionate_function mod_index max_fun_nr fun_index (fun_defs, modules, pi)
		# (fun_def, fun_defs) = fun_defs![fun_index]
		= case fun_def.fun_body of
			CheckedBody body
				# fun_number = pi.pi_next_num
				# (min_dep, funs_modules_pi) = foldSt (visit_function mod_index max_fun_nr) fun_def.fun_info.fi_calls
						(max_fun_nr, ({ fun_defs & [fun_index] = { fun_def & fun_body = PartioningFunction body fun_number }}, modules,
							{ pi & pi_next_num = inc fun_number, pi_deps = [fun_index : pi.pi_deps] }))
				-> try_to_close_group mod_index max_fun_nr fun_index fun_number min_dep fun_def.fun_info.fi_def_level funs_modules_pi
			PartioningFunction _ fun_number
				-> (fun_number, (fun_defs, modules, pi))
			TransformedBody _
				| fun_def.fun_info.fi_group_index == NoIndex
Sjaak Smetsers's avatar
Sjaak Smetsers committed
768
					# (fun_defs, pi) =  add_called_macros fun_def.fun_info.fi_calls (fun_defs, pi)
769
770
771
772
773
//					-> (max_fun_nr, ({ fun_defs & [fun_index] = {fun_def & fun_info.fi_group_index = pi.pi_next_group }}, modules,
					-> (max_fun_nr, ({ fun_defs & [fun_index] = {fun_def & fun_info.fi_group_index = -2-pi.pi_next_group }}, modules,
							{pi & pi_next_group = inc pi.pi_next_group, pi_groups = [ [fun_index] : pi.pi_groups]}
//							{pi & pi_next_group = pi.pi_next_group}
							))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
774
					-> (max_fun_nr, (fun_defs, modules, pi))
775
			
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
776
777
778
779
780
781
782
	visit_function mod_index max_fun_nr {fc_index} (min_dep, funs_modules_pi)
		# (next_min, funs_modules_pi) = partitionate_function mod_index max_fun_nr fc_index funs_modules_pi
		= (min next_min min_dep, funs_modules_pi)
		
	try_to_close_group mod_index max_fun_nr fun_index fun_number min_dep def_level (fun_defs, modules,
					pi=:{pi_symbol_table, pi_var_heap, pi_symbol_heap, pi_deps, pi_groups, pi_next_group, pi_error})
		| fun_number <= min_dep
783
			# (pi_deps, functions_in_group, macros_in_group, fun_defs)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
784
					= close_group fun_index pi_deps [] [] max_fun_nr pi_next_group fun_defs
785
			  {ls_x={x_fun_defs=fun_defs}, ls_var_heap=pi_var_heap, ls_expr_heap=pi_symbol_heap}
786
787
788
			  		= liftFunctions def_level (functions_in_group ++ macros_in_group) pi_next_group main_dcl_module_n fun_defs pi_var_heap pi_symbol_heap
			  es	
			  		= expand_macros_in_group macros_in_group
789
			  				{	es_symbol_table = pi_symbol_table, es_var_heap = pi_var_heap, es_symbol_heap = pi_symbol_heap,
790
791
			  					es_fun_defs=fun_defs, es_main_dcl_module_n=mod_index, es_dcl_modules=modules,
			  					es_expand_in_imp_module=False, // function expand_macros fills in correct value
792
793
				  				es_error = pi_error }
			  {es_symbol_table, es_var_heap, es_symbol_heap, es_error,es_dcl_modules,es_fun_defs}
794
			  		= expand_macros_in_group functions_in_group es
795
			= (max_fun_nr, (es_fun_defs, es_dcl_modules, { pi & pi_deps = pi_deps, pi_var_heap = es_var_heap,
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
796
						pi_symbol_table = es_symbol_table, pi_error = es_error, pi_symbol_heap = es_symbol_heap, 
797
798
799
						pi_next_group = inc pi_next_group, 
						pi_groups = [ functions_in_group ++ macros_in_group : pi_groups ] }))
//						pi_groups = if (isEmpty functions_in_group) pi_groups [ functions_in_group : pi_groups ] }))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
800
801
			= (min_dep, (fun_defs, modules, pi))
	where
802
		close_group fun_index [d:ds] functions_in_group macros_in_group nr_of_fun_defs group_number fun_defs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
803
			# (fun_def, fun_defs) = fun_defs![d]
804
805
806
807
808
//			  fun_defs = { fun_defs & [d] = { fun_def & fun_info.fi_group_index = group_number }}
//			| fun_def.fun_kind == FK_Macro
			| case fun_def.fun_kind of FK_DefMacro->True ; FK_ImpMacro->True; _ -> False
				# fun_defs = { fun_defs & [d] = { fun_def & fun_info.fi_group_index = -2-group_number }}
				# macros_in_group = [d : macros_in_group]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
809
				| d == fun_index
810
811
812
813
					= (ds, functions_in_group, macros_in_group, fun_defs)
					= close_group fun_index ds functions_in_group macros_in_group nr_of_fun_defs group_number fun_defs
				# fun_defs = { fun_defs & [d] = { fun_def & fun_info.fi_group_index = group_number }}
				# functions_in_group = [d : functions_in_group]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
814
				| d == fun_index
815
816
					= (ds, functions_in_group, macros_in_group, fun_defs)
					= close_group fun_index ds functions_in_group macros_in_group nr_of_fun_defs group_number fun_defs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
817
		
818
819
		expand_macros_in_group group es
			= foldSt expand_macros group es
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
820

821
822
		expand_macros fun_index es
			# (fun_def,es) = es!es_fun_defs.[fun_index]
823
			  {fun_symb,fun_body = PartioningFunction body _, fun_info, fun_pos,fun_kind} = fun_def
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
824
		  	  identPos = newPosition fun_symb fun_pos
825
826
827
828
			# expand_in_imp_module=case fun_kind of FK_ImpFunction _->True; FK_ImpMacro->True; FK_ImpCaf->True; _ -> False
			  es={ es & es_expand_in_imp_module=expand_in_imp_module, es_error = setErrorAdmin identPos es.es_error }
			# (tb_args, tb_rhs, fi_local_vars, fi_calls, es)
					= expandMacrosInBody fun_info.fi_calls body alias_dummy es
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
829
830
			  fun_def = { fun_def & fun_body = TransformedBody { tb_args = tb_args, tb_rhs = tb_rhs},
			  			fun_info = { fun_info & fi_calls = fi_calls, fi_local_vars = fi_local_vars }}
831
832
			= {es & es_fun_defs.[fun_index] = fun_def }
	
Sjaak Smetsers's avatar
Sjaak Smetsers committed
833
834
835
836
837
838
839
840
841
	add_called_macros calls macro_defs_and_pi
		= foldSt add_called_macro calls macro_defs_and_pi
	where
		add_called_macro {fc_index} (macro_defs, pi)
			# (macro_def, macro_defs) = macro_defs![fc_index]
			= case macro_def.fun_body of
				TransformedBody _
					| macro_def.fun_info.fi_group_index == NoIndex
						# (macro_defs, pi) = add_called_macros macro_def.fun_info.fi_calls (macro_defs, pi)
842
843
844
845
846
//						->	({ macro_defs & [fc_index] = {macro_def & fun_info.fi_group_index = pi.pi_next_group }},
						->	({ macro_defs & [fc_index] = {macro_def & fun_info.fi_group_index = -2-pi.pi_next_group }},
								{pi & pi_next_group = inc pi.pi_next_group,pi_groups = [ [fc_index] : pi.pi_groups]}
//								{pi & pi_next_group = pi.pi_next_group}
								)
Sjaak Smetsers's avatar
Sjaak Smetsers committed
847
848
						-> (macro_defs, pi)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
849
850
851
852
853
addFunctionCallsToSymbolTable calls fun_defs symbol_table
	= foldSt add_function_call_to_symbol_table calls ([], fun_defs, symbol_table)
where
	add_function_call_to_symbol_table fc=:{fc_index} (collected_calls, fun_defs, symbol_table)
		# ({fun_symb = { id_info }, fun_kind}, fun_defs) = fun_defs![fc_index]
854
855
856
857
858
859
860
861
862
//		| fun_kind == FK_Macro
		= case fun_kind of
			FK_DefMacro
				-> (collected_calls, fun_defs, symbol_table)
			FK_ImpMacro
				-> (collected_calls, fun_defs, symbol_table)
			_
				# (entry, symbol_table) = readPtr id_info symbol_table
				-> ([fc : collected_calls], fun_defs,
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
863
864
865
866
867
868
869
					symbol_table <:= (id_info, { ste_kind = STE_Called [fc_index], ste_index = NoIndex, ste_def_level = NotALevel, ste_previous = entry }))

removeFunctionCallsFromSymbolTable calls fun_defs symbol_table
	= foldSt remove_function_call_from_symbol_table calls (fun_defs, symbol_table)
where
	remove_function_call_from_symbol_table {fc_index} (fun_defs, symbol_table)
		# ({fun_symb = { id_info }}, fun_defs) = fun_defs![fc_index]
870
871
872
873
874
875
		  (entry, symbol_table) = readPtr id_info symbol_table
		= case entry.ste_kind of
			STE_Called indexes
				-> (fun_defs, symbol_table <:= (id_info, entry.ste_previous))
			_
				-> (fun_defs, symbol_table)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
876

877
878
expandMacrosInBody :: [.FunCall] CheckedBody PredefinedSymbol *ExpandState -> ([FreeVar],Expression,[FreeVar],[FunCall],.ExpandState);
expandMacrosInBody fi_calls {cb_args,cb_rhs} alias_dummy es=:{es_symbol_table,es_fun_defs}
879
	# (prev_calls, fun_defs, es_symbol_table)
880
881
882
883
884
			= addFunctionCallsToSymbolTable fi_calls es_fun_defs es_symbol_table
	  ([rhs:rhss], (all_calls, es) )
	  		= mapSt expandCheckedAlternative cb_rhs (prev_calls, { es & es_fun_defs=fun_defs, es_symbol_table = es_symbol_table })
	  (fun_defs, symbol_table)
	  		= removeFunctionCallsFromSymbolTable all_calls es.es_fun_defs es.es_symbol_table
885
886
	  ((merged_rhs, _), es_var_heap, es_symbol_heap, es_error)
	  		= mergeCases rhs rhss es.es_var_heap es.es_symbol_heap es.es_error
887
888
889
890
	  (new_rhs, new_args, local_vars, {cos_error, cos_var_heap, cos_symbol_heap})
	  		= determineVariablesAndRefCounts cb_args merged_rhs
	  				{ cos_error = es_error, cos_var_heap = es_var_heap, cos_symbol_heap = es_symbol_heap,
	  					cos_alias_dummy = alias_dummy }
891
	= (new_args, new_rhs, local_vars, all_calls,
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
892
		{ es & es_error = cos_error, es_var_heap = cos_var_heap, es_symbol_heap = cos_symbol_heap,
893
												es_fun_defs=fun_defs, es_symbol_table = symbol_table })
894
895
//		---> ("expandMacrosInBody", (cb_args, ca_rhs, '\n'), ("merged_rhs", merged_rhs, '\n'), ("new_rhs", new_args, local_vars, (new_rhs, '\n')))

896
897
898
expandCheckedAlternative {ca_rhs, ca_position} ei
	# (ca_rhs, ei) = expand ca_rhs ei
	= ((ca_rhs, ca_position), ei)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
899
900
901
902
903
904
905

cContainsFreeVars 	:== True
cContainsNoFreeVars :== False

cMacroIsCalled 		:== True
cNoMacroIsCalled 	:== False

John van Groningen's avatar
John van Groningen committed
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
class GetSetPatternRhs a
where
	get_pattern_rhs :: !a -> Expression
	set_pattern_rhs :: !a !Expression -> a

instance GetSetPatternRhs AlgebraicPattern
	where
		get_pattern_rhs p = p.ap_expr
		set_pattern_rhs p expr = {p & ap_expr=expr}

instance GetSetPatternRhs BasicPattern
	where
		get_pattern_rhs p = p.bp_expr
		set_pattern_rhs p expr = {p & bp_expr=expr};

instance GetSetPatternRhs DynamicPattern
	where
		get_pattern_rhs p = p.dp_rhs
		set_pattern_rhs p expr = {p & dp_rhs=expr}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
925

926
927
928
929
930
931
932
933
934
mergeCases :: !(!Expression, !Position) ![(!Expression, !Position)] !*VarHeap !*ExpressionHeap !*ErrorAdmin
			-> *(!(!Expression, !Position), !*VarHeap, !*ExpressionHeap, !*ErrorAdmin)
mergeCases expr_and_pos [] var_heap symbol_heap error
	= (expr_and_pos, var_heap, symbol_heap, error)
mergeCases (Let lad=:{let_expr}, pos) exprs var_heap symbol_heap error
	# ((let_expr, _), var_heap, symbol_heap, error) = mergeCases (let_expr, NoPos) exprs var_heap symbol_heap error
	= ((Let {lad & let_expr = let_expr}, pos), var_heap,symbol_heap, error)
mergeCases (case_expr=:(Case first_case=:{case_expr = Var {var_info_ptr}, case_default = No}), case_pos)
			[(expr, expr_pos) : exprs] var_heap symbol_heap error
Sjaak Smetsers's avatar
Sjaak Smetsers committed
935
936
	# (split_result, var_heap, symbol_heap) = split_case var_info_ptr expr var_heap symbol_heap
	= case split_result of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
937
938
		Yes {case_guards,case_default}
			# (case_guards, var_heap, symbol_heap, error) = merge_guards first_case.case_guards case_guards var_heap symbol_heap error
939
940
			-> mergeCases (Case { first_case & case_guards = case_guards, case_default = case_default }, NoPos)
						exprs var_heap symbol_heap error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
941
		No
942
943
944
			# ((case_default, pos), var_heap, symbol_heap, error) = mergeCases (expr, expr_pos) exprs var_heap symbol_heap error
			-> ((Case { first_case & case_default = Yes case_default, case_default_pos = pos }, case_pos),
				var_heap, symbol_heap, error)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
945
946

where
Sjaak Smetsers's avatar
Sjaak Smetsers committed
947
	split_case split_var_info_ptr (Case this_case=:{case_expr = Var {var_info_ptr}, case_guards, case_default}) var_heap symbol_heap
948
		| split_var_info_ptr == skip_alias var_info_ptr var_heap
Sjaak Smetsers's avatar
Sjaak Smetsers committed
949
			= (Yes this_case, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
950
951
952
		| has_no_default case_default
			= case case_guards of
				AlgebraicPatterns type [alg_pattern]
Sjaak Smetsers's avatar
Sjaak Smetsers committed
953
954
					# (split_result, var_heap, symbol_heap) = split_case split_var_info_ptr alg_pattern.ap_expr var_heap symbol_heap
					-> case split_result of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
955
						Yes split_case
John van Groningen's avatar
John van Groningen committed
956
957
958
959
							# (cees,symbol_heap) = push_expression_into_guards_and_default
													( \ guard_expr -> { this_case & case_guards = AlgebraicPatterns type [{ alg_pattern & ap_expr = guard_expr }] } )
														split_case symbol_heap
							-> (Yes cees, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
960
						No
Sjaak Smetsers's avatar
Sjaak Smetsers committed
961
							-> (No, var_heap, symbol_heap) 
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
962
				BasicPatterns type [basic_pattern]
Sjaak Smetsers's avatar
Sjaak Smetsers committed
963
964
					# (split_result, var_heap, symbol_heap) = split_case split_var_info_ptr basic_pattern.bp_expr var_heap symbol_heap
					-> case split_result of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
965
						Yes split_case
John van Groningen's avatar
John van Groningen committed
966
967
968
969
							# (cees,symbol_heap) = push_expression_into_guards_and_default
													( \ guard_expr -> { this_case & case_guards = BasicPatterns type [ { basic_pattern & bp_expr = guard_expr }] })
													split_case symbol_heap
							-> (Yes cees, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
970
						No
Sjaak Smetsers's avatar
Sjaak Smetsers committed
971
							-> (No, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
972
				DynamicPatterns [dynamic_pattern]
Sjaak Smetsers's avatar
Sjaak Smetsers committed
973
974
					# (split_result, var_heap, symbol_heap) = split_case split_var_info_ptr dynamic_pattern.dp_rhs var_heap symbol_heap
					-> case split_result of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
975
						Yes split_case
John van Groningen's avatar
John van Groningen committed
976
977
978
979
							# (cees,symbol_heap) = push_expression_into_guards_and_default
										( \ guard_expr -> { this_case & case_guards = DynamicPatterns [ { dynamic_pattern & dp_rhs = guard_expr }] })
										split_case symbol_heap
							-> (Yes cees, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
980
						No
Sjaak Smetsers's avatar
Sjaak Smetsers committed
981
							-> (No, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
982
				_
Sjaak Smetsers's avatar
Sjaak Smetsers committed
983
					-> (No, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
984
		| otherwise
Sjaak Smetsers's avatar
Sjaak Smetsers committed
985
			= (No, var_heap, symbol_heap)
986
	split_case split_var_info_ptr (Let lad=:{let_expr,let_strict_binds,let_lazy_binds}) var_heap symbol_heap
Sjaak Smetsers's avatar
Sjaak Smetsers committed
987
		| isEmpty let_strict_binds
988
			# var_heap = foldSt set_alias let_lazy_binds var_heap
Sjaak Smetsers's avatar
Sjaak Smetsers committed
989
990
991
992
993
994
995
996
997
998
			# (split_result, var_heap, symbol_heap) = split_case split_var_info_ptr let_expr var_heap symbol_heap
			= case split_result of
				Yes split_case
					# (case_guards, var_heap, symbol_heap) = push_let_expression_into_guards lad split_case.case_guards var_heap symbol_heap
					-> (Yes { split_case & case_guards = case_guards }, var_heap, symbol_heap)
				No
					-> (No, var_heap, symbol_heap)
			= (No, var_heap, symbol_heap)
	split_case split_var_info_ptr expr var_heap symbol_heap
		= (No, var_heap, symbol_heap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
999
1000
1001
	
	has_no_default No 		= True
	has_no_default (Yes _) 	= False
1002
1003
1004
1005
1006
1007
1008
1009
	
	skip_alias var_info_ptr var_heap
		= case sreadPtr var_info_ptr var_heap of
			VI_Alias bv
				-> bv.var_info_ptr
			_
				-> var_info_ptr

1010
	set_alias {lb_src=Var var,lb_dst={fv_info_ptr}} var_heap
1011