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

3
import syntax, check, StdCompare, utilities, mergecases; //, 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
	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)
81
/*	lift (Lambda vars expr) ls
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
82
83
		# (expr, ls) = lift expr ls
		= (Lambda vars expr, ls)
84
*/	lift (MatchExpr opt_tuple cons_symb expr) ls
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
		# (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
		| glob_module == ls.ls_x.LiftStateX.x_main_dcl_module_n
			#! fun_def = ls.ls_x.x_fun_defs.[glob_object]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
104
105
106
			# {fun_info={fi_free_vars}} = fun_def
			  fun_lifted = length fi_free_vars
			| fun_lifted > 0
107
				# (app_args, ls_var_heap, ls_expr_heap) = add_free_variables_in_app fi_free_vars app_args ls.ls_var_heap ls.ls_expr_heap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
108
109
110
111
				= ({ 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)
112
113
114
115
116
117
	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_x.x_fun_defs.[glob_object]
		# {fun_info={fi_free_vars}} = fun_def
		  fun_lifted = length fi_free_vars
		| fun_lifted > 0
118
			# (app_args, ls_var_heap, ls_expr_heap) = add_free_variables_in_app fi_free_vars app_args ls.ls_var_heap ls.ls_expr_heap
119
120
121
			= ({ 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)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
122
123
124
125
	lift app=:{app_args} ls
		# (app_args, ls) = lift app_args ls
		= ({ app & app_args = app_args }, ls)

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
add_free_variables_in_app :: ![FreeVar] ![Expression] !u:VarHeap !*ExpressionHeap -> (![Expression],!u:VarHeap,!*ExpressionHeap)
add_free_variables_in_app [] app_args var_heap expr_heap
	= (app_args, var_heap, expr_heap)
add_free_variables_in_app [{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_in_app 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_in_app 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

141
142
143
144
145
146
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
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
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)

189
190
unfoldVariable :: !BoundVar UnfoldInfo !*UnfoldState -> (!Expression, !*UnfoldState)
unfoldVariable var=:{var_name,var_info_ptr} ui us
191
	#! (var_info, us) = readVarInfo var_info_ptr us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
192
193
194
195
	= case var_info of 
		VI_Expression expr
			-> (expr, us)
		VI_Variable var_name var_info_ptr
196
		 	# (var_expr_ptr, us_symbol_heap) = newPtr EI_Empty us.us_symbol_heap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
197
			-> (Var {var_name = var_name, var_info_ptr = var_info_ptr, var_expr_ptr = var_expr_ptr}, { us & us_symbol_heap = us_symbol_heap})
198
199
200
201
202
		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)
203
204
205
		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
206
207
208
			  app = App { app_symb = app_symb, app_args = app_args, app_info_ptr = new_info_ptr }
			  us = { us & us_opt_type_heaps = us_opt_type_heaps, us_symbol_heap = us_symbol_heap }
			-> unfold app ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
209
210
		_
			-> (Var var, us)
211
  where
212
213
	substitute_class_types class_types No
		= (class_types, No)
214
	substitute_class_types class_types (Yes type_heaps)
215
		# (_,new_class_types, type_heaps) = substitute class_types type_heaps
216
217
		= (new_class_types, Yes type_heaps)

218

219
220
221
222
223
224
225
226
227
228
229
230
231
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

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
::	CopiedLocalFunction = {
		old_function_n :: !Int,
		new_function_n :: !Int
	}

::	CopiedLocalFunctions = {
		copied_local_functions :: [CopiedLocalFunction],
		used_copied_local_functions :: [CopiedLocalFunction],
		new_copied_local_functions :: [CopiedLocalFunction],
		next_local_function_n :: !Int
	}

::	UnfoldState =
	{	us_var_heap				:: !.VarHeap
	,	us_symbol_heap			:: !.ExpressionHeap
	,	us_opt_type_heaps		:: !.Optional .TypeHeaps,
		us_cleanup_info			:: ![ExprInfoPtr],
		us_local_macro_functions :: !Optional CopiedLocalFunctions
	}

::	UnfoldInfo =
	{	ui_handle_aci_free_vars	:: !AciFreeVarHandleMode,
		ui_convert_module_n :: !Int, // -1 if no conversion
		ui_conversion_table :: !Optional ConversionTable
	}

:: AciFreeVarHandleMode = LeaveThem | RemoveThem | SubstituteThem

class unfold a :: !a !UnfoldInfo !*UnfoldState -> (!a, !*UnfoldState)

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

Martin Wierich's avatar
Martin Wierich committed
302
303
instance unfold DynamicExpr
where
304
305
	unfold expr=:{dyn_expr} ui us
		# (dyn_expr, us) = unfold dyn_expr ui us
Martin Wierich's avatar
Martin Wierich committed
306
307
		= ({ expr & dyn_expr = dyn_expr }, us)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
308
309
instance unfold Selection
where
310
	unfold (ArraySelection array_select expr_ptr index_expr) ui us=:{us_symbol_heap}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
311
		# (new_ptr, us_symbol_heap) = newPtr EI_Empty us_symbol_heap
312
		  (index_expr, us) = unfold index_expr ui { us & us_symbol_heap = us_symbol_heap}
Sjaak Smetsers's avatar
Sjaak Smetsers committed
313
		= (ArraySelection array_select new_ptr index_expr, us)
314
	unfold (DictionarySelection var selectors 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}
317
		  (var_expr, us) = unfoldVariable var ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
318
319
320
321
322
		= 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
323
							new_ptr index_expr, us)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
324
			Var var
Sjaak Smetsers's avatar
Sjaak Smetsers committed
325
				-> (DictionarySelection var selectors new_ptr index_expr, us)
326
327
	unfold record_selection ui us
		= (record_selection, us)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
328
329
330

instance unfold FreeVar
where
331
	unfold fv=:{fv_info_ptr,fv_name} ui us=:{us_var_heap}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
332
333
334
335
		# (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
336
337
	unfold app=:{app_symb={symb_kind}, app_args, app_info_ptr} ui=:{ui_convert_module_n,ui_conversion_table} us
		= case symb_kind of
338
			SK_Function {glob_module,glob_object}
339
340
				| ui_convert_module_n==glob_module 
					# (Yes conversion_table) = ui_conversion_table
341
342
//					| glob_object>=size conversion_table.[cFunctionDefs]
//						-> abort ("unfold(App) "+++toString app.app_symb.symb_name+++" "+++toString glob_object+++" "+++toString (size conversion_table.[cFunctionDefs]))
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
					# 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
			SK_LocalMacroFunction local_macro_function_n
				# (us_local_macro_functions,us) = us!us_local_macro_functions
				-> case us_local_macro_functions of
					No
						-> unfold_function_app app ui us
					uslocal_macro_functions=:(Yes local_macro_functions)
						# (new_local_macro_function_n,us_local_macro_functions) = determine_new_local_macro_function_n local_macro_function_n local_macro_functions
							with
								determine_new_local_macro_function_n local_macro_function_n local_macro_functions=:{copied_local_functions,used_copied_local_functions,new_copied_local_functions,next_local_function_n}
									# new_local_macro_function_n = search_new_local_macro_function_n used_copied_local_functions
									| new_local_macro_function_n>=0
										= (new_local_macro_function_n,us_local_macro_functions)
									# (new_local_macro_function_n,used_copied_local_functions) = search_new_local_macro_function_n_and_add_to_used_functions copied_local_functions used_copied_local_functions
									| new_local_macro_function_n>=0
										= (new_local_macro_function_n,Yes {local_macro_functions & used_copied_local_functions=used_copied_local_functions})
									# (new_local_macro_function_n,used_copied_local_functions) = search_new_local_macro_function_n_and_add_to_used_functions new_copied_local_functions used_copied_local_functions
									| new_local_macro_function_n>=0
										= (new_local_macro_function_n,Yes {local_macro_functions & used_copied_local_functions=used_copied_local_functions})
										# new_local_function = {old_function_n=local_macro_function_n,new_function_n=next_local_function_n}
										# new_copied_local_functions=new_copied_local_functions++[new_local_function]
										# us_local_macro_functions=Yes {copied_local_functions=copied_local_functions,
																		new_copied_local_functions=new_copied_local_functions,
																		used_copied_local_functions=[new_local_function:used_copied_local_functions],
																		next_local_function_n=next_local_function_n+1}
										= (next_local_function_n,us_local_macro_functions)
									where
										search_new_local_macro_function_n [{old_function_n,new_function_n}:local_functions]
											| local_macro_function_n==old_function_n
												= new_function_n
											 	= search_new_local_macro_function_n local_functions
										search_new_local_macro_function_n []
											= -1

										search_new_local_macro_function_n_and_add_to_used_functions [copied_local_function=:{old_function_n,new_function_n}:local_functions] used_copied_local_functions
											| local_macro_function_n==old_function_n
												= (new_function_n,[copied_local_function:used_copied_local_functions])
											 	= search_new_local_macro_function_n_and_add_to_used_functions local_functions used_copied_local_functions
										search_new_local_macro_function_n_and_add_to_used_functions [] used_copied_local_functions
											= (-1,used_copied_local_functions)
						# us={us & us_local_macro_functions=us_local_macro_functions}
						# app={app & app_symb.symb_kind=SK_LocalMacroFunction new_local_macro_function_n}
						-> unfold_function_app app ui us
400
401
402
403
404
405
406
407
408
409
410
411
412
			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
413
	where
414
415
416
417
418
		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
419

420
		substitute_EI_DictionaryType (EI_DictionaryType class_type) (Yes type_heaps)
421
			# (_,new_class_type, type_heaps) = substitute class_type type_heaps
422
423
			= (EI_DictionaryType new_class_type, Yes type_heaps)
		substitute_EI_DictionaryType x opt_type_heaps
424
			= (x, opt_type_heaps)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
425

426
427
instance unfold LetBind
where
428
429
	unfold bind=:{lb_src} ui us
		# (lb_src, us) = unfold lb_src ui us
430
431
		= ({ bind & lb_src = lb_src }, us)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
432
433
instance unfold (Bind a b) | unfold a
where
434
435
	unfold bind=:{bind_src} ui us
		# (bind_src, us) = unfold bind_src ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
436
437
438
439
		= ({ bind & bind_src = bind_src }, us)

instance unfold Case
where
440
	unfold kees=:{ case_expr,case_guards,case_default,case_info_ptr} ui us=:{us_cleanup_info}
441
442
443
		# (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
444
445
446
		  us_cleanup_info = case old_case_info of
								EI_Extended _ _	-> [new_info_ptr:us_cleanup_info]
								_				-> us_cleanup_info
447
		  us = { us & us_symbol_heap = us_symbol_heap, us_opt_type_heaps = us_opt_type_heaps, us_cleanup_info=us_cleanup_info }
448
		  ((case_guards,case_default), us) = unfold (case_guards,case_default) ui us
449
450
451
		  (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
452
		update_active_case_info_and_unfold case_expr=:(Var {var_info_ptr}) case_info_ptr us
453
454
			# (case_info, us_symbol_heap) = readPtr case_info_ptr us.us_symbol_heap
			  us = { us & us_symbol_heap = us_symbol_heap }
455
456
			= case case_info of
				EI_Extended (EEI_ActiveCase aci=:{aci_free_vars}) ei
457
					#!(new_aci_free_vars, us) = case ui.ui_handle_aci_free_vars of
458
459
460
461
462
463
													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)
464
					  (var_info, us) = readVarInfo var_info_ptr us
465
466
467
468
469
					-> 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
470
							  (tb_rhs, us) = unfold tb_rhs ui { us & us_var_heap = us_var_heap }
471
472
473
474
475
476
477
							  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
478
479
							-> unfold case_expr ui { us & us_symbol_heap = us_symbol_heap }
				_	-> unfold case_expr ui us	
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
		  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
501
			= unfold case_expr ui us
502
503

		unfoldBoundVar {var_info_ptr} us
504
505
			# (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 })
506

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
507
508
instance unfold Let
where
509
	unfold lad=:{let_strict_binds, let_lazy_binds, let_expr, let_info_ptr} ui us
Sjaak Smetsers's avatar
Sjaak Smetsers committed
510
511
		# (let_strict_binds, us) = copy_bound_vars let_strict_binds us
		# (let_lazy_binds, us) = copy_bound_vars let_lazy_binds us
512
513
514
		# (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
515
		  (old_let_info, us_symbol_heap) = readPtr let_info_ptr us.us_symbol_heap
516
517
		  (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
518
		= ({lad & let_strict_binds = let_strict_binds, let_lazy_binds = let_lazy_binds, let_expr = let_expr, let_info_ptr = new_info_ptr},
519
			{ us & us_symbol_heap = us_symbol_heap, us_opt_type_heaps = us_opt_type_heaps })
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
520
		where
521
			copy_bound_vars [bind=:{lb_dst} : binds] us
522
				# (lb_dst, us) = unfold lb_dst ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
523
				  (binds, us) = copy_bound_vars binds us
524
				= ([ {bind & lb_dst = lb_dst} : binds ], us)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
525
526
527
			copy_bound_vars [] us
				= ([], us)

528
529
530
531
532
533
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)
534
	# (_,new_case_type, type_heaps) = substitute case_type type_heaps
535
536
	= (EI_CaseType new_case_type, Yes type_heaps)
substitute_let_or_case_type	(EI_LetType let_type) (Yes type_heaps)
537
	# (_,new_let_type, type_heaps) = substitute let_type type_heaps
538
539
	= (EI_LetType new_let_type, Yes type_heaps)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
540
541
instance unfold CasePatterns
where
542
543
	unfold (AlgebraicPatterns type patterns) ui us
		# (patterns, us) = unfold patterns ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
544
		= (AlgebraicPatterns type patterns, us)
545
546
	unfold (BasicPatterns type patterns) ui us
		# (patterns, us) = unfold patterns ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
547
		= (BasicPatterns type patterns, us)
548
549
	unfold (DynamicPatterns patterns) ui us
		# (patterns, us) = unfold patterns ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
550
551
552
553
		= (DynamicPatterns patterns, us)

instance unfold BasicPattern
where
554
555
	unfold guard=:{bp_expr} ui us
		# (bp_expr, us) = unfold bp_expr ui us
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
556
557
558
559
		= ({ guard & bp_expr = bp_expr }, us)

instance unfold AlgebraicPattern
where
560
561
562
	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
563
564
565
566
		= ({ guard & ap_vars = ap_vars, ap_expr = ap_expr }, us)

instance unfold DynamicPattern
where
567
568
569
	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
570
571
		= ({ guard & dp_var = dp_var, dp_rhs = dp_rhs }, us)

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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)

602
603
//import StdDebug

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
604
605
606
607
608
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
609
610
611
	add_function_call fc=:{fc_index} (collected_calls, fun_defs, symbol_table)
//		# fc_index = trace ("add_function_call: "+++toString fc_index+++" ") fc_index
		# ({fun_symb}, fun_defs) = fun_defs![fc_index]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
612
613
614
615
		  (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)
616
	# (entry, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
617
618
619
620
621
622
623
624
625
	= 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 }))

626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
copy_macro_and_local_functions :: FunDef (Optional CopiedLocalFunctions) *ExpandState -> (!FunDef,![(CopiedLocalFunction,FunDef)],!Optional CopiedLocalFunctions,!*ExpandState);
copy_macro_and_local_functions macro=:{fun_kind} local_macro_functions es
	# is_def_macro=case fun_kind of FK_DefMacro->True; _->False
	# (macro,local_macro_functions,es) = copy_macro_or_local_macro_function is_def_macro macro local_macro_functions es
	# (new_functions,local_macro_functions,es) = copy_local_functions_of_macro local_macro_functions is_def_macro [] es
	= (macro,new_functions,local_macro_functions,es)

copy_local_functions_of_macro :: (Optional CopiedLocalFunctions) Bool [CopiedLocalFunction] *ExpandState -> (![(CopiedLocalFunction,FunDef)],!Optional CopiedLocalFunctions,!*ExpandState);
copy_local_functions_of_macro local_macro_functions is_def_macro local_functions_to_be_copied es
	# (local_functions_to_be_copied,local_macro_functions) = add_new_local_functions_to_be_copied local_functions_to_be_copied local_macro_functions
		with
			add_new_local_functions_to_be_copied local_functions_to_be_copied local_macro_functions=:(Yes copied_local_macro_functions=:{new_copied_local_functions=[]})
				= (local_functions_to_be_copied,Yes {copied_local_macro_functions & used_copied_local_functions=[]})
			add_new_local_functions_to_be_copied local_functions_to_be_copied (Yes {copied_local_functions,new_copied_local_functions,next_local_function_n})
				# local_macro_functions=Yes {copied_local_functions=copied_local_functions++new_copied_local_functions,
											new_copied_local_functions=[],used_copied_local_functions=[],next_local_function_n=next_local_function_n}
				= (local_functions_to_be_copied++new_copied_local_functions,local_macro_functions)
	= case local_functions_to_be_copied of
		[]
			-> ([],local_macro_functions,es)
		[(old_and_new_function_n=:{old_function_n,new_function_n}):local_functions_to_be_copied]
			# (function,es)=es!es_fun_defs.[old_function_n]
			
			#! function_group_index=function.fun_info.fi_group_index
			# es = {es & es_fun_defs.[old_function_n].fun_info.fi_group_index= if (function_group_index>NoIndex) (-2-function_group_index) function_group_index}
			# function = {function & fun_info.fi_group_index=if (function_group_index<NoIndex) (-2-function_group_index) function_group_index}

			# (function,local_macro_functions,es) = copy_macro_or_local_macro_function is_def_macro function local_macro_functions es
			# function={function & fun_index=new_function_n}
			# (new_functions,local_macro_functions,es) = copy_local_functions_of_macro local_macro_functions is_def_macro local_functions_to_be_copied es
			-> ([(old_and_new_function_n,function):new_functions],local_macro_functions,es)

update_calls calls No
	= calls
update_calls calls (Yes {used_copied_local_functions=[]})
	= calls
update_calls calls (Yes {used_copied_local_functions})
	# calls = remove_old_calls calls
	= add_new_calls used_copied_local_functions calls
where
	remove_old_calls [call=:{fc_index}:calls]
		| contains_old_function_n used_copied_local_functions
//			# calls = trace ("remove_old_calls1: "+++toString fc_index) calls
			= remove_old_calls calls
//			# calls = trace ("remove_old_calls2: "+++toString fc_index) calls
			= [call:remove_old_calls calls]
	where
		contains_old_function_n [{old_function_n}:local_functions]
			= fc_index==old_function_n || contains_old_function_n local_functions
		contains_old_function_n []
			= False
	remove_old_calls []
		= []
	
	add_new_calls [{new_function_n}:local_functions] calls
//		# local_functions = trace ("add_new_calls: "+++toString new_function_n) local_functions
		= add_new_calls local_functions [{fc_index=new_function_n,fc_level=NotALevel}:calls]
	add_new_calls [] calls
		= calls

copy_macro_or_local_macro_function :: !Bool !FunDef !(Optional CopiedLocalFunctions) !*ExpandState -> (!FunDef,!Optional CopiedLocalFunctions,!.ExpandState);
copy_macro_or_local_macro_function is_def_macro macro=:{fun_body = TransformedBody {tb_args,tb_rhs},fun_kind,fun_info={fi_local_vars,fi_calls}} local_macro_functions es=:{es_var_heap,es_symbol_heap,es_expand_in_imp_module,es_main_dcl_module_n,es_dcl_modules}
	# (tb_args,es_var_heap) = create_new_arguments tb_args es_var_heap
		with
			create_new_arguments [var=:{fv_name,fv_info_ptr} : vars] var_heap
				# (new_vars,var_heap) = create_new_arguments vars 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 }
				= ([new_var : new_vars], writePtr fv_info_ptr (VI_Variable fv_name new_info) var_heap)
			create_new_arguments [] var_heap
				= ([],var_heap)
	# us = { us_symbol_heap = es_symbol_heap, us_var_heap = es_var_heap, us_opt_type_heaps = No,us_cleanup_info = [],
			 us_local_macro_functions = local_macro_functions }
	# (result_expr,dcl_modules,{us_local_macro_functions,us_symbol_heap,us_var_heap}) = unfold_and_convert es_dcl_modules us
		with
			unfold_and_convert dcl_modules us
				| 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 }
					# (expr,es) = unfold tb_rhs ui us
					= (expr,dcl_modules,es)
			
					# ui = {ui_handle_aci_free_vars = RemoveThem, ui_convert_module_n = -1 ,ui_conversion_table=No }
					# (expr,es) = unfold tb_rhs ui us
					= (expr,dcl_modules,es)
	# (fi_local_vars,us_var_heap) = update_local_vars fi_local_vars us_var_heap
		with
			update_local_vars :: ![FreeVar] !*(Heap VarInfo) -> (![FreeVar],!*Heap VarInfo);
			update_local_vars [fv=:{fv_info_ptr}:fvs] var_heap
				# (fvs,var_heap)=update_local_vars fvs var_heap
				# (fv_info,var_heap) = readPtr fv_info_ptr var_heap
//				# fv = {fv & fv_info_ptr=case fv_info of (VI_Variable _ info_ptr) -> info_ptr}
				# fv = {fv & fv_info_ptr=case fv_info of 
											(VI_Variable _ info_ptr) -> info_ptr
						}
				= ([fv:fvs],var_heap)
			update_local_vars [] var_heap
				= ([],var_heap)
	# fi_calls = update_calls fi_calls us_local_macro_functions
	= ({macro & fun_body = TransformedBody {tb_args=tb_args,tb_rhs=result_expr},fun_info.fi_local_vars=fi_local_vars,fun_info.fi_calls=fi_calls},us_local_macro_functions,
		{es & es_var_heap=us_var_heap, es_symbol_heap=us_symbol_heap, es_dcl_modules=dcl_modules})

728
unfoldMacro :: !FunDef ![Expression] !*ExpandInfo -> (!Expression, !*ExpandInfo)
729
730
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_fun_defs,es_expand_in_imp_module,es_main_dcl_module_n,es_dcl_modules})
	# is_def_macro=case fun_kind of FK_DefMacro->True; _->False
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
731
	# (let_binds, var_heap) = bind_expressions tb_args args [] es_var_heap
732
733
734
735
	#! size_fun_defs = size es_fun_defs
	# copied_local_functions = Yes { copied_local_functions=[],used_copied_local_functions=[],new_copied_local_functions=[],next_local_function_n=size_fun_defs}
	# us = { us_symbol_heap = es_symbol_heap, us_var_heap = var_heap, us_opt_type_heaps = No,us_cleanup_info = [], us_local_macro_functions = copied_local_functions }
	# (result_expr,dcl_modules,{us_local_macro_functions,us_symbol_heap,us_var_heap}) = unfold_and_convert es_dcl_modules us
736
		with
737
			unfold_and_convert dcl_modules us
738
739
				| es_expand_in_imp_module && is_def_macro
					# (dcl_mod,dcl_modules) = dcl_modules![es_main_dcl_module_n]
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
					# 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) = unfold tb_rhs ui us
					= (result_expr,dcl_modules,us)

					# ui = {ui_handle_aci_free_vars = RemoveThem, ui_convert_module_n = -1 ,ui_conversion_table=No }
					# (result_expr,us) = unfold tb_rhs ui us
					= (result_expr,dcl_modules,us)
					
	# es = {es & es_var_heap = us_var_heap, es_symbol_heap = us_symbol_heap, es_dcl_modules=dcl_modules}
	# fi_calls = update_calls fi_calls us_local_macro_functions
	# (new_functions,us_local_macro_functions,es) = copy_local_functions_of_macro us_local_macro_functions is_def_macro [] es
	# {es_symbol_heap,es_symbol_table,es_fun_defs,es_new_fun_def_numbers} = es
	# (es_fun_defs,es_new_fun_def_numbers) = case new_functions of
		[]
			-> (es_fun_defs,es_new_fun_def_numbers)
		_
			# last_function_index = case us_local_macro_functions of (Yes {next_local_function_n}) -> next_local_function_n-1
			# new_fun_defs = new_fun_defs
				with
					new_fun_defs :: *{!FunDef}
					new_fun_defs => {fun_def \\ (_,fun_def)<-new_functions}
			-> ({if (i<size_fun_defs) es_fun_defs.[i] new_fun_defs.[i-size_fun_defs] \\ i<-[0..last_function_index]} // inefficient
				,[size_fun_defs:es_new_fun_def_numbers])
763
	# (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
764
	| isEmpty let_binds
765
766
767
768
		= (result_expr, (calls, { es & es_symbol_table = es_symbol_table, es_fun_defs=fun_defs,es_new_fun_def_numbers=es_new_fun_def_numbers }))
		# (new_info_ptr, es_symbol_heap) = newPtr EI_Empty es_symbol_heap
		# result_expr=Let { let_strict_binds = [], let_lazy_binds = let_binds, let_expr = result_expr, let_info_ptr = new_info_ptr, let_expr_position = NoPos }
		= (result_expr, (calls, { es & es_symbol_table = es_symbol_table, es_symbol_heap=es_symbol_heap, es_fun_defs=fun_defs,es_new_fun_def_numbers=es_new_fun_def_numbers }))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
769
770
771
772
773
774
775
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)

776
	bind_expression :: FreeVar Expression [LetBind] *(Heap VarInfo) -> (![LetBind],!*Heap VarInfo);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
777
778
779
780
781
782
783
784
785
786
	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 }
787
		= ([{ 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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807

::	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	

John van Groningen's avatar
John van Groningen committed
808
809
810
:: PredefSymbolsForTransform = { predef_alias_dummy :: !PredefinedSymbol, predef_and :: !PredefinedSymbol, predef_or :: !PredefinedSymbol };

partitionateMacros :: !IndexRange !Index !PredefSymbolsForTransform !*{# FunDef} !*{# DclModule} !*VarHeap !*ExpressionHeap !*SymbolTable !*ErrorAdmin
811
	-> (!*{# FunDef}, !.{# DclModule}, !*VarHeap, !*ExpressionHeap, !*SymbolTable, !*ErrorAdmin )
John van Groningen's avatar
John van Groningen committed
812
partitionateMacros {ir_from,ir_to} mod_index predef_symbols_for_transform fun_defs modules var_heap symbol_heap symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
813
814
815
816
	#! 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 = [] }
817
	  (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
818
	  		= iFoldSt (pationate_macro mod_index max_fun_nr) ir_from ir_to (fun_defs, modules, partitioning_info)
819
820
821
822

//	# (size_fun_defs,fun_defs) = usize fun_defs
//	# fun_defs=trace ("size_fun_defs: "+++toString size_fun_defs+++" ") fun_defs;

Martin Wierich's avatar
Martin Wierich committed
823
	= (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
824
825
826
827
828
829
830
831
832
833
834
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]
835
836
//		| 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
837
838
		 	= case macro_def.fun_body of
		 		CheckedBody body
Sjaak Smetsers's avatar
Sjaak Smetsers committed
839
840
			  		# 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
841
842
843
844
845
846
847
848
849
850
					-> 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
851
	
852
	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
853
			(macro_defs, modules, pi=:{pi_symbol_table,pi_symbol_heap,pi_var_heap,pi_error})
854
		| macros_are_simple fun_info.fi_calls macro_defs && has_no_curried_macro body.cb_rhs macro_defs
Martin Wierich's avatar
Martin Wierich committed
855
		  	# identPos = newPosition fun_symb fun_pos
856
			# expand_in_imp_module=case fun_kind of FK_ImpMacro->True; _ -> False
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
857
			  es = { es_symbol_table = pi_symbol_table, es_var_heap = pi_var_heap,
858
					 es_symbol_heap = pi_symbol_heap, es_error = setErrorAdmin identPos pi_error,
859
					 es_fun_defs=macro_defs, es_main_dcl_module_n = mod_index, es_dcl_modules=modules,
860
861
					 es_expand_in_imp_module=expand_in_imp_module,es_new_fun_def_numbers=[]
					  }			
John van Groningen's avatar
John van Groningen committed
862
863
			# (tb_args, tb_rhs, local_vars, fi_calls, fun_info, {es_symbol_table, es_var_heap, es_symbol_heap, es_error,es_dcl_modules,es_fun_defs})
					= expandMacrosInBody [] body predef_symbols_for_transform macro_index es
864
			# macro = { macro & fun_body = TransformedBody { tb_args = tb_args, tb_rhs = tb_rhs},
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
865
			  			fun_info = { fun_info & fi_calls = fi_calls, fi_local_vars = local_vars }}
866
			= ({ es_fun_defs & [macro_index] = macro }, es_dcl_modules,
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
867
					{ 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
868
			# pi = { pi & pi_deps = [macro_index:pi.pi_deps] }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
869
870
			= ({ macro_defs & [macro_index] = { macro & fun_body = RhsMacroBody body }}, modules, pi)

871
	macros_are_simple :: [FunCall] {#FunDef} -> Bool;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
872
873
874
	macros_are_simple [] macro_defs
		= True
	macros_are_simple [ {fc_index} : calls ] macro_defs
875
		# {fun_kind,fun_body, fun_symb} = macro_defs.[fc_index]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
876
877
		= is_a_pattern_macro fun_kind fun_body && macros_are_simple calls macro_defs
	where
878
879
880
		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
881
882
883
			= True
		is_a_pattern_macro _ _
			= False
884

885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
add_new_macros_to_groups :: ![Int] !Int Int *{#FunDef} [Int] [[Int]] -> (!Int,!*{#FunDef},![Int],![[Int]]);
add_new_macros_to_groups [new_macro_fun_def_index] n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
	= add_new_macro_and_local_functions_to_groups new_macro_fun_def_index n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
add_new_macros_to_groups [new_macro_fun_def_index:macro_fun_def_numbers=:[next_macro_fun_def_index:_]] n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
	# (pi_next_group,es_fun_defs,functions_in_group,pi_groups)
		= add_new_macro_and_local_functions_to_groups new_macro_fun_def_index next_macro_fun_def_index pi_next_group es_fun_defs functions_in_group pi_groups
	= add_new_macros_to_groups macro_fun_def_numbers n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
add_new_macros_to_groups [] n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
	= (pi_next_group,es_fun_defs,functions_in_group,pi_groups)

add_new_macro_and_local_functions_to_groups :: !Int !Int Int *{#FunDef} [Int] [[Int]] -> (!Int,!*{#FunDef},![Int],![[Int]]);
add_new_macro_and_local_functions_to_groups new_macro_fun_def_index n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
	# (pi_next_group,es_fun_defs,functions_in_group,macros)
		= add_macros_to_current_group new_macro_fun_def_index n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group []
	# (macros_with_group_numbers,es_fun_defs) = add_group_numbers macros es_fun_defs
		with
			add_group_numbers [fun_def_index:l] es_fun_defs
				# (group_number,es_fun_defs) = es_fun_defs![fun_def_index].fun_info.fi_group_index
//				# group_number=trace ("add_group_numbers: "+++toString fun_def_index+++" "+++toString group_number+++"\n") group_number;
				# (l,es_fun_defs) = add_group_numbers l es_fun_defs
				= ([(fun_def_index,group_number):l],es_fun_defs)
			add_group_numbers [] es_fun_defs
				= ([],es_fun_defs)
	# sorted_macros_with_group_numbers = sortBy (\(_,group_number1) (_,group_number2) -> group_number1<group_number2) macros_with_group_numbers
	# (pi_next_group,pi_groups) = partition_macros_in_groups sorted_macros_with_group_numbers [] (-1) pi_next_group pi_groups
		with
			partition_macros_in_groups [(fun_def_index,fun_def_group_number):l] [] group_number pi_next_group pi_groups
				= partition_macros_in_groups l [fun_def_index] fun_def_group_number pi_next_group pi_groups
			partition_macros_in_groups [(fun_def_index,fun_def_group_number):l] group group_number pi_next_group pi_groups
				| fun_def_group_number==group_number
					= partition_macros_in_groups l [fun_def_index:group] group_number pi_next_group pi_groups
					# pi_groups=[group:pi_groups]
					# pi_next_group=pi_next_group+1
					= partition_macros_in_groups l [fun_def_index] fun_def_group_number pi_next_group pi_groups			
			partition_macros_in_groups [] [] group_number pi_next_group pi_groups
				= (pi_next_group,pi_groups)
			partition_macros_in_groups [] last_group group_number pi_next_group pi_groups
				= (pi_next_group+1,[last_group:pi_groups])
	= (pi_next_group,es_fun_defs,functions_in_group,pi_groups)

add_macros_to_current_group :: !Int !Int Int *{#FunDef} [Int] [Int] -> (!Int,!*{#FunDef},![Int],![Int]);
add_macros_to_current_group new_macro_fun_def_index n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group macros
	| new_macro_fun_def_index>=n_fun_defs_after_expanding_macros
		= (pi_next_group,es_fun_defs,functions_in_group,macros)
	| es_fun_defs.[new_macro_fun_def_index].fun_info.fi_group_index<=NoIndex
		= abort ("add_macros_to_current_group: "+++toString new_macro_fun_def_index)
//				+++" "+++toString es_fun_defs.[new_macro_fun_def_index].fun_info.fi_group_index)

	| es_fun_defs.[new_macro_fun_def_index].fun_info.fi_group_index==pi_next_group
//		# new_macro_fun_def_index=trace ("add_macros_to_current_group1: "+++toString new_macro_fun_def_index+++"\n") new_macro_fun_def_index;
		# functions_in_group=[new_macro_fun_def_index:functions_in_group]
		= add_macros_to_current_group (new_macro_fun_def_index+1) n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group macros

//		# new_macro_fun_def_index=trace ("add_macros_to_current_group2: "+++toString new_macro_fun_def_index+++"\n") new_macro_fun_def_index;
//		# pi_groups=[[new_macro_fun_def_index]:pi_groups]
//		# pi_next_group=pi_next_group+1
		= add_macros_to_current_group (new_macro_fun_def_index+1) n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group [new_macro_fun_def_index:macros]

943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
has_no_curried_macro cb_rhs fun_defs
	= has_no_curried_macro_CheckedAlternative cb_rhs
where
	has_no_curried_macro_CheckedAlternative [{ca_rhs}:cas]
		= has_no_curried_macro_Expression ca_rhs && has_no_curried_macro_CheckedAlternative cas
	has_no_curried_macro_CheckedAlternative []
		= True

	has_no_curried_macro_Expression (App app=:{app_symb={symb_arity, symb_kind = SK_Macro {glob_object,glob_module}}, app_args})
		| fun_defs.[glob_object].fun_arity<>symb_arity
			= False;
			= has_no_curried_macro_Expressions app_args
	has_no_curried_macro_Expression (App app=:{app_args})
		= has_no_curried_macro_Expressions app_args
	has_no_curried_macro_Expression (expr @ exprs)
		= has_no_curried_macro_Expression expr && has_no_curried_macro_Expressions exprs
	has_no_curried_macro_Expression (Let {let_strict_binds, let_lazy_binds, let_expr})
		= has_no_curried_macro_LetBinds let_strict_binds && has_no_curried_macro_LetBinds let_lazy_binds && has_no_curried_macro_Expression let_expr
		where
				has_no_curried_macro_LetBinds [{lb_src}:xs]
					= has_no_curried_macro_Expression lb_src && has_no_curried_macro_LetBinds xs
				has_no_curried_macro_LetBinds []
					= True
	has_no_curried_macro_Expression (Case {case_expr,case_guards,case_default})
		=	has_no_curried_macro_Expression case_expr && has_no_curried_macro_CasePatterns case_guards && has_no_curried_macro_OptionalExpression case_default
		where
			has_no_curried_macro_CasePatterns (AlgebraicPatterns type patterns)
				= has_no_curried_macro_AlgebraicPatterns patterns
			where
				has_no_curried_macro_AlgebraicPatterns [{ap_expr}:patterns]
					= has_no_curried_macro_Expression ap_expr && has_no_curried_macro_AlgebraicPatterns patterns
				has_no_curried_macro_AlgebraicPatterns []
					= True
			has_no_curried_macro_CasePatterns (BasicPatterns type patterns)
				= has_no_curried_macro_BasicPatterns patterns
			where
				has_no_curried_macro_BasicPatterns [{bp_expr}:patterns]
					= has_no_curried_macro_Expression bp_expr && has_no_curried_macro_BasicPatterns patterns
				has_no_curried_macro_BasicPatterns []
					= True
			has_no_curried_macro_CasePatterns (DynamicPatterns patterns)
				= has_no_curried_macro_DynamicPatterns patterns
			where
				has_no_curried_macro_DynamicPatterns [{dp_rhs}:patterns]
					= has_no_curried_macro_Expression dp_rhs && has_no_curried_macro_DynamicPatterns patterns
				has_no_curried_macro_DynamicPatterns []
					= True

			has_no_curried_macro_OptionalExpression (Yes expr)
				= has_no_curried_macro_Expression expr
			has_no_curried_macro_OptionalExpression No
				= True
	has_no_curried_macro_Expression (Selection is_unique expr selectors)
		= has_no_curried_macro_Expression expr && has_no_curried_macro_Selections selectors
	has_no_curried_macro_Expression (Update expr1 selectors expr2)
		= has_no_curried_macro_Expression expr1 && has_no_curried_macro_Expression expr2 && has_no_curried_macro_Selections selectors
	has_no_curried_macro_Expression (RecordUpdate cons_symbol expression expressions)
		= has_no_curried_macro_Expression expression && has_no_curried_macro_Binds expressions
		where
			has_no_curried_macro_Binds [{bind_src}:binds]
				= has_no_curried_macro_Expression bind_src && has_no_curried_macro_Binds binds
			has_no_curried_macro_Binds []
				= True
	has_no_curried_macro_Expression (TupleSelect symbol argn_nr expr)
		= has_no_curried_macro_Expression expr
	has_no_curried_macro_Expression (MatchExpr opt_tuple cons_symb expr)
		= has_no_curried_macro_Expression expr
	has_no_curried_macro_Expression expr
		= True

	has_no_curried_macro_Expressions [x:xs]
		= has_no_curried_macro_Expression x && has_no_curried_macro_Expressions xs
	has_no_curried_macro_Expressions []
		= True

	has_no_curried_macro_Selections [ArraySelection array_select expr_ptr index_expr:selections]
		= has_no_curried_macro_Expression index_expr && has_no_curried_macro_Selections selections
	has_no_curried_macro_Selections [record_selection:selections]
		= has_no_curried_macro_Selections selections
	has_no_curried_macro_Selections []
		= True

John van Groningen's avatar
John van Groningen committed
1025
partitionateAndLiftFunctions :: ![IndexRange] !Index !PredefSymbolsForTransform !*{# FunDef} !*{# DclModule} !*VarHeap !*ExpressionHeap !*SymbolTable !*ErrorAdmin
1026
	-> (!*{! Group}, !*{# FunDef}, !.{# DclModule}, !*VarHeap, !*ExpressionHeap,  !*SymbolTable, !*ErrorAdmin )
John van Groningen's avatar
John van Groningen committed
1027
partitionateAndLiftFunctions ranges main_dcl_module_n predef_symbols_for_transform fun_defs modules var_heap symbol_heap symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1028
	#! max_fun_nr = size fun_defs
1029
1030
	# 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
1031
	  (fun_defs, modules, {pi_groups, pi_symbol_table, pi_var_heap, pi_symbol_heap, pi_error})
1032
	  		= foldSt (partitionate_functions main_dcl_module_n max_fun_nr) ranges (fun_defs, modules, partitioning_info)
1033
1034
1035
	# (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
1036
1037
	= (groups, fun_defs, modules, pi_var_heap, pi_symbol_heap, pi_symbol_table, pi_error)
where
1038
1039
1040
1041
1042
1043
1044
1045
	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
1046

1047
1048
			| fun_defs.[fun].fun_info.fi_group_index<NoIndex
				= (funs,fun_defs)
1049
1050

			= ([fun:funs],fun_defs)
1051
1052
1053
1054
1055
		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
1056
1057
1058
1059
1060
1061
1062
1063
	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)
1064
//		# fun_index = trace ("partitionate_function: "+++toString fun_index+++" ") fun_index
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1065
1066
1067
1068
1069
1070
1071
		# (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] }))
John van Groningen's avatar
John van Groningen committed
1072
				-> try_to_close_group mod_index max_fun_nr fun_index fun_number min_dep funs_modules_pi
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1073
1074
1075
1076
			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
1077
					# (fun_defs, pi) =  add_called_macros fun_def.fun_info.fi_calls (fun_defs, pi)
1078
1079
					-> (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,
1080
1081
1082
							{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
1083
					-> (max_fun_nr, (fun_defs, modules, pi))
1084
			
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1085
1086
1087
	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)
1088

John van Groningen's avatar
John van Groningen committed
1089
	try_to_close_group mod_index max_fun_nr fun_index fun_number min_dep (fun_defs, modules,
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1090
1091
					pi=:{pi_symbol_table, pi_var_heap, pi_symbol_heap, pi_deps, pi_groups, pi_next_group, pi_error})
		| fun_number <= min_dep
1092
			# (pi_deps, functions_in_group, macros_in_group, fun_defs)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1093
					= close_group fun_index pi_deps [] [] max_fun_nr pi_next_group fun_defs
1094
			  {ls_x={x_fun_defs=fun_defs}, ls_var_heap=pi_var_heap, ls_expr_heap=pi_symbol_heap}
John van Groningen's avatar
John van Groningen committed
1095
			  		= liftFunctions (functions_in_group ++ macros_in_group) pi_next_group main_dcl_module_n fun_defs pi_var_heap pi_symbol_heap		  
1096
			# es
1097
			  		= expand_macros_in_group macros_in_group
1098
1099
1100
1101
1102
			  			{	es_symbol_table = pi_symbol_table, es_var_heap = pi_var_heap, es_symbol_heap = pi_symbol_heap,
			  				es_fun_defs=fun_defs, es_main_dcl_module_n=mod_index, es_dcl_modules=modules, es_new_fun_def_numbers=[],
			  				es_expand_in_imp_module=False, // function expand_macros fills in correct value
				  			es_error = pi_error }
			# {es_symbol_table, es_var_heap, es_symbol_heap, es_error,es_dcl_modules,es_fun_defs,es_new_fun_def_numbers}
1103
			  		= expand_macros_in_group functions_in_group es
1104
1105
1106
			# (n_fun_defs_after_expanding_macros,es_fun_defs) = usize es_fun_defs
			# (pi_next_group,es_fun_defs,functions_in_group,pi_groups)
				= add_new_macros_to_groups (reverse es_new_fun_def_numbers) n_fun_defs_after_expanding_macros pi_next_group es_fun_defs functions_in_group pi_groups
1107
			= (max_fun_nr, (es_fun_defs, es_dcl_modules, { pi & pi_deps = pi_deps, pi_var_heap = es_var_heap,