backendconvert.icl 54.4 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
4
5
6
7
8
9
10
11
implementation module backendconvert

import code from library "backend_library"

import StdEnv

import frontend
import backend
import backendsupport, backendpreprocess
import RWSDebug

clean's avatar
clean committed
12
13
14
15
16
17
// trace macro
(-*->) infixl
(-*->) value trace
	:==	value // ---> trace


Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
18
19
20
21
22
:: BEMonad a :== St !*BackEnd !a


:: Backender :== *BackEnd -> *BackEnd

23
24
25
26
27
28
29
30
31
// foldr` :: (.a -> .(.b -> .b)) .b ![.a] -> .b	//	op e0 (op e1(...(op r e##)...)
foldr` op r l :== foldr l
	where
		foldr []	= r
		foldr [a:x]	= op a (foldr x)

flip` f x y
	:==	f y x

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
32
33
34
35
36
37
38
39
40
41
42
43
/* +++
:: *BackEndState = {bes_backEnd :: BackEnd, bes_varHeap :: *VarHeap}

appBackEnd f beState
	#	(result, bes_backEnd)
		=	f beState.bes_backEnd
	=	(result, {beState & bes_backEnd = bes_backEnd})
accVarHeap f beState
	#	(result, varHeap)
		=	f beState.bes_varHeap
	=	(result, {beState & bes_varHeap = varHeap})
*/
44
appBackEnd f :== f
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
accVarHeap f beState :== f beState

beFunction0 f
	:== appBackEnd f
beFunction1 f m1
	:== m1 ==> \a1
	->	appBackEnd (f a1)
beFunction2 f m1 m2
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	appBackEnd (f a1 a2)
beFunction3 f m1 m2 m3
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	appBackEnd (f a1 a2 a3)
beFunction4 f m1 m2 m3 m4
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	appBackEnd (f a1 a2 a3 a4)
beFunction5 f m1 m2 m3 m4 m5
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	appBackEnd (f a1 a2 a3 a4 a5)
beFunction6 f m1 m2 m3 m4 m5 m6
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	m6 ==> \a6
	->	appBackEnd (f a1 a2 a3 a4 a5 a6)
beFunction7 f m1 m2 m3 m4 m5 m6 m7
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	m6 ==> \a6
	->	m7 ==> \a7
	->	appBackEnd (f a1 a2 a3 a4 a5 a6 a7)

changeArrayFunctionIndex selectIndex
	:== selectIndex

beBoolSymbol value
	:==	beFunction0 (BEBoolSymbol value)
beLiteralSymbol type value
	:==	beFunction0 (BELiteralSymbol type value)
beFunctionSymbol functionIndex moduleIndex
	:==	beFunction0 (BEFunctionSymbol functionIndex moduleIndex)
beSpecialArrayFunctionSymbol arrayFunKind functionIndex moduleIndex
	:==	beFunction0 (BESpecialArrayFunctionSymbol arrayFunKind (changeArrayFunctionIndex functionIndex) moduleIndex)
beDictionarySelectFunSymbol
	:==	beFunction0 BEDictionarySelectFunSymbol
beDictionaryUpdateFunSymbol
	:==	beFunction0 BEDictionaryUpdateFunSymbol
beConstructorSymbol moduleIndex constructorIndex
	:==	beFunction0 (BEConstructorSymbol constructorIndex moduleIndex)
beFieldSymbol fieldIndex moduleIndex
	:==	beFunction0 (BEFieldSymbol fieldIndex moduleIndex)
beTypeSymbol typeIndex moduleIndex
	:==	beFunction0 (BETypeSymbol typeIndex moduleIndex)
beBasicSymbol typeSymbolIndex
	:==	beFunction0 (BEBasicSymbol typeSymbolIndex)
beDontCareDefinitionSymbol
	:==	beFunction0 BEDontCareDefinitionSymbol
beNoArgs
	:==	beFunction0 BENoArgs
beArgs
	:==	beFunction2 BEArgs
beNoTypeArgs
	:==	beFunction0 BENoTypeArgs
beTypeArgs
	:==	beFunction2 BETypeArgs
beNormalNode
	:==	beFunction2 BENormalNode
beIfNode
	:==	beFunction3 BEIfNode
beGuardNode
	:==	beFunction7 BEGuardNode
beSelectorNode selectorKind
	:==	beFunction2 (BESelectorNode selectorKind)
beUpdateNode
	:==	beFunction1 BEUpdateNode
beNormalTypeNode
	:==	beFunction2 BENormalTypeNode
beVarTypeNode name
	:==	beFunction0 (BEVarTypeNode name)
beRuleAlt lineNumber
	:==	beFunction5 (BERuleAlt lineNumber)
beNoRuleAlts
	:==	beFunction0 BENoRuleAlts
beRuleAlts
	:==	beFunction2 BERuleAlts
beTypeAlt
	:==	beFunction2 BETypeAlt
beRule index isCaf
	:==	beFunction2 (BERule index isCaf)
beNoRules
	:==	beFunction0 BENoRules
beRules
	:==	beFunction2 BERules
beNodeDef sequenceNumber
	:==	beFunction1 (BENodeDef sequenceNumber)
beNoNodeDefs
	:==	beFunction0 BENoNodeDefs
beNodeDefs
	:==	beFunction2 BENodeDefs
beStrictNodeId
	:==	beFunction1 BEStrictNodeId
beNoStrictNodeIds
	:==	beFunction0 BENoStrictNodeIds
beStrictNodeIds
	:==	beFunction2 BEStrictNodeIds
beNodeIdNode
	:==	beFunction2 BENodeIdNode
beNodeId sequenceNumber
	:==	beFunction0 (BENodeId sequenceNumber)
beWildCardNodeId
	:==	beFunction0 BEWildCardNodeId
beConstructor
	:==	beFunction1 BEConstructor
beNoConstructors
	:==	beFunction0 BENoConstructors
beConstructors
	:==	beFunction2 BEConstructors
beNoFields
	:==	beFunction0 BENoFields
beFields
	:==	beFunction2 BEFields
beField fieldIndex moduleIndex 
	:==	beFunction1 (BEField fieldIndex moduleIndex)
beAnnotateTypeNode annotation
	:==	beFunction1 (BEAnnotateTypeNode annotation)
beAttributeTypeNode attribution
	:==	beFunction1 (BEAttributeTypeNode attribution)
beDeclareRuleType functionIndex moduleIndex name
	:==	beFunction0 (BEDeclareRuleType functionIndex moduleIndex name)
beDefineRuleType functionIndex moduleIndex
	:==	beFunction1 (BEDefineRuleType functionIndex moduleIndex)
beCodeAlt lineNumber
	:==	beFunction3 (BECodeAlt lineNumber)
beString string
	:==	beFunction0 (BEString string)
beStrings
	:==	beFunction2 BEStrings
beNoStrings
	:==	beFunction0 BENoStrings
beCodeParameter location
	:==	beFunction1 (BECodeParameter location)
beCodeParameters
	:==	beFunction2 BECodeParameters
beNoCodeParameters
	:==	beFunction0 BENoCodeParameters
beAbcCodeBlock inline
	:==	beFunction1 (BEAbcCodeBlock inline)
beAnyCodeBlock
	:==	beFunction3 BEAnyCodeBlock
beDeclareNodeId number lhsOrRhs name
	:==	beFunction0 (BEDeclareNodeId number lhsOrRhs name)
beAdjustArrayFunction backendId functionIndex moduleIndex
	:==	beFunction0 (BEAdjustArrayFunction backendId functionIndex moduleIndex)
beFlatType
	:==	beFunction2 BEFlatType
beNoTypeVars
	:==	beFunction0 BENoTypeVars
beTypeVars
	:==	beFunction2 BETypeVars
beTypeVar name
	:==	beFunction0 (BETypeVar name)
clean's avatar
clean committed
221
222
223
224
225
226
beExportType dclTypeIndex iclTypeIndex
	:==	beFunction0 (BEExportType dclTypeIndex iclTypeIndex)
beExportConstructor dclConstructorIndex iclConstructorIndex
	:==	beFunction0 (BEExportConstructor dclConstructorIndex iclConstructorIndex)
beExportField dclFieldIndex iclFieldIndex
	:==	beFunction0 (BEExportField dclFieldIndex iclFieldIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
227
228
229
230
231
232
233
234
beExportFunction dclIndexFunctionIndex iclFunctionIndex
	:==	beFunction0 (BEExportFunction dclIndexFunctionIndex iclFunctionIndex)
beTupleSelectNode arity index
	:==	beFunction1 (BETupleSelectNode arity index)
beMatchNode arity
	:==	beFunction2 (BEMatchNode arity)
beDefineImportedObjsAndLibs
	:== beFunction2 BEDefineImportedObjsAndLibs
clean's avatar
clean committed
235
236
beAbsType
	:== beFunction1 BEAbsType
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
237
238
239
240
241
242

notYetImplementedExpr :: Expression
notYetImplementedExpr
	=	(BasicExpr (BVS "\"error in compiler (something was not implemented by lazy Ronny)\"") BT_Int)

backEndConvertModules :: PredefinedSymbols FrontEndSyntaxTree VarHeap *BackEnd -> *BackEnd
243
244
backEndConvertModules predefs {fe_icl = fe_icl =: {icl_name, icl_functions, icl_common, icl_imported_objects}, fe_components, fe_dcls, fe_arrayInstances, fe_dclIclConversions, fe_iclDclConversions, fe_globalFunctions} varHeap backEnd
// sanity check ...
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
245
246
247
248
249
250
251
252
253
254
255
256
//	| cIclModIndex <> kIclModuleIndex || cPredefinedModuleIndex <> kPredefinedModuleIndex
//		=	undef <<- "backendconvert, backEndConvertModules: module index mismatch"
	// ... sanity check
/*
	#  backEnd
		=	ruleDoesNotMatch 1 backEnd
			with
				ruleDoesNotMatch 0 backend
					=	backend
	#  backEnd
		=	abort "front end abort" backEnd
*/
257
	# backEnd
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
258
		=	BEDeclareModules (size fe_dcls) backEnd
259
	# backEnd
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
260
		=	predefineSymbols fe_dcls.[cPredefinedModuleIndex] predefs backEnd
clean's avatar
clean committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

	#  currentDcl
	   	=	fe_dcls.[cIclModIndex]
	   typeConversions
		=	currentModuleTypeConversions icl_common.com_class_defs currentDcl.dcl_common.com_class_defs currentDcl.dcl_conversions
/*
	# 	rstypes = reshuffleTypes (size icl_common.com_type_defs) typeConversions {type.td_name.id_name \\ type <-: currentDcl.dcl_common.com_type_defs}
		types = {type.td_name.id_name \\ type <-: icl_common.com_type_defs}
	#  backEnd
		=	backEnd ->>
				(	"dcl conversions"
				,	currentDcl.dcl_conversions
				,	"dcl constructors"
				,	[constructor.cons_symb.id_name \\ constructor <-: currentDcl.dcl_common.com_cons_defs]
				,	"dcl selectors"
				,	[selector.sd_symb.id_name \\ selector <-: currentDcl.dcl_common.com_selector_defs]
				,	"dcl types"
				,	[type.td_name.id_name \\ type <-: currentDcl.dcl_common.com_type_defs]
				,	"icl selectors"
				,	[constructor.cons_symb.id_name \\ constructor <-: icl_common.com_cons_defs]
				,	"icl fields"
				,	[selector.sd_symb.id_name \\ selector <-: icl_common.com_selector_defs]
				,	"icl types"
				,	[type.td_name.id_name \\ type <-: icl_common.com_type_defs]
				,	"compare names"
				,	(rstypes, types)
				)
*/
289
	# backEnd
clean's avatar
clean committed
290
		=	declareCurrentDclModule fe_icl fe_dcls.[cIclModIndex] (backEnd -*-> "declareCurrentDclModule")
291
	# backEnd
clean's avatar
clean committed
292
		=	declareOtherDclModules fe_dcls (backEnd -*-> "declareOtherDclModules")
293
	# backEnd
clean's avatar
clean committed
294
		=	defineDclModule varHeap cIclModIndex fe_dcls.[cIclModIndex] (backEnd -*-> "defineDclModule(cIclMoIndex)")
295
	# backEnd
clean's avatar
clean committed
296
		=	reshuffleTypes (size icl_common.com_type_defs) typeConversions (backEnd -*-> "reshuffleTypes")
297
	# backEnd
clean's avatar
clean committed
298
299
		=	defineOtherDclModules fe_dcls varHeap (backEnd -*-> "defineOtherDclModules")

300
	# backEnd
clean's avatar
clean committed
301
		=	BEDeclareIclModule icl_name.id_name (size icl_functions) (size icl_common.com_type_defs) (size icl_common.com_cons_defs) (size icl_common.com_selector_defs) (backEnd -*-> "BEDeclareIclModule")
302
303
	# backEnd
		=	declareFunctionSymbols icl_functions (getConversions fe_iclDclConversions) functionIndices fe_globalFunctions (backEnd -*-> "declareFunctionSymbols")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
304
305
306
307
308
309
		with
			getConversions :: (Optional {#Int}) -> {#Int}
			getConversions No
				=	{}
			getConversions (Yes conversions)
				=	conversions
310
	# backEnd
clean's avatar
clean committed
311
		=	declare cIclModIndex varHeap icl_common (backEnd -*-> "declare (cIclModIndex)")
312
	# backEnd
clean's avatar
clean committed
313
		=	declareArrayInstances fe_arrayInstances icl_functions (backEnd -*-> "declareArrayInstances")
314
	# backEnd
clean's avatar
clean committed
315
		=	adjustArrayFunctions predefs fe_arrayInstances icl_functions fe_dcls varHeap (backEnd -*-> "adjustArrayFunctions")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
316
	#! (rules, backEnd)
317
318
319
		=	convertRules predefs.[PD_DummyForStrictAliasFun].pds_ident
					[(index, icl_functions.[index]) \\ (_, index) <- functionIndices]
					varHeap (backEnd -*-> "convertRules")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
320
	#! backEnd
clean's avatar
clean committed
321
		=	BEDefineRules rules (backEnd -*-> "BEDefineRules")
322
	# backEnd
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
323
324
325
		=	beDefineImportedObjsAndLibs
				(convertStrings [imported.io_name \\ imported <- icl_imported_objects | not imported.io_is_library])
				(convertStrings [imported.io_name \\ imported <- icl_imported_objects | imported.io_is_library])
clean's avatar
clean committed
326
				(backEnd -*-> "beDefineImportedObjsAndLibs")
327
	# backEnd
clean's avatar
clean committed
328
329
330
331
332
		=	markExports fe_dcls.[cIclModIndex] dcl_common.com_class_defs dcl_common.com_type_defs icl_common.com_class_defs icl_common.com_type_defs fe_dclIclConversions (backEnd -*-> "markExports")
			with
				dcl_common
					=	currentDcl.dcl_common
	=	(backEnd -*-> "backend done")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
	where
		componentCount
			=	length functionIndices
		functionIndices
			=	flatten [[(componentIndex, member) \\ member <- group.group_members] \\ group <-: fe_components & componentIndex <- [0..]]

declareOtherDclModules :: {#DclModule} -> Backender
declareOtherDclModules dcls
	=	foldStateWithIndexA declareOtherDclModule dcls

defineOtherDclModules :: {#DclModule} VarHeap -> Backender
defineOtherDclModules dcls varHeap
	=	foldStateWithIndexA (defineOtherDclModule varHeap) dcls

declareCurrentDclModule :: IclModule DclModule -> Backender
declareCurrentDclModule {icl_common} {dcl_name, dcl_functions, dcl_is_system, dcl_common}
	=	BEDeclareDclModule cIclModIndex dcl_name.id_name dcl_is_system (size dcl_functions) (size icl_common.com_type_defs) (size dcl_common.com_cons_defs) (size dcl_common.com_selector_defs)
	
declareOtherDclModule :: ModuleIndex DclModule -> Backender
declareOtherDclModule moduleIndex dclModule
	| moduleIndex == cIclModIndex || moduleIndex == cPredefinedModuleIndex
		=	identity
	// otherwise
		=	declareDclModule moduleIndex dclModule

declareDclModule :: ModuleIndex DclModule -> Backender
declareDclModule moduleIndex {dcl_name, dcl_common, dcl_functions, dcl_is_system}
	=	BEDeclareDclModule moduleIndex dcl_name.id_name dcl_is_system (size dcl_functions) (size dcl_common.com_type_defs) (size dcl_common.com_cons_defs) (size dcl_common.com_selector_defs)

clean's avatar
clean committed
362
363
364
defineCurrentDclModule :: VarHeap IclModule DclModule {#Int} -> Backender
defineCurrentDclModule varHeap {icl_common} {dcl_name, dcl_common, dcl_functions, dcl_is_system, dcl_conversions} typeConversions
	=	declareCurrentDclModuleTypes icl_common.com_type_defs typeConversions varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
365
366
367
368
369
370
371
372
373
374
	o`	defineCurrentDclModuleTypes dcl_common.com_cons_defs dcl_common.com_selector_defs dcl_common.com_type_defs typeConversions varHeap

defineOtherDclModule :: VarHeap ModuleIndex DclModule -> Backender
defineOtherDclModule varHeap moduleIndex dclModule
	| moduleIndex == cIclModIndex || moduleIndex == cPredefinedModuleIndex
		=	identity
	// otherwise
		=	defineDclModule varHeap moduleIndex dclModule

defineDclModule :: VarHeap ModuleIndex DclModule -> Backender
375
defineDclModule varHeap moduleIndex {dcl_name, dcl_common, dcl_instances, dcl_functions, dcl_is_system}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
376
	=	declare moduleIndex varHeap dcl_common
377
	o`	declareFunTypes moduleIndex dcl_functions dcl_instances.ir_from varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
378

clean's avatar
clean committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// move types from their dcl to icl positions

class swapTypes a :: Int Int *a -> *a

instance swapTypes BackEnd where
	swapTypes i j be
		=	BESwapTypes i j be

instance swapTypes {{#Char}} where
	swapTypes i j a
		=	swap i j a

swap i j a
	#! iValue = a.[i]
	#! jValue = a.[j]
	=	{a & [i] = jValue, [j] = iValue}

reshuffleTypes :: Int {#Int} *a -> *a | swapTypes a
reshuffleTypes nIclTypes dclIclConversions be
clean's avatar
clean committed
398
	=	thd3 (foldStateWithIndexA (swapType nDclTypes) dclIclConversions (idP nDclTypes, idP nIclTypes, be))
clean's avatar
clean committed
399
	where
clean's avatar
clean committed
400
401
402
		nDclTypes
			=	size dclIclConversions

clean's avatar
clean committed
403
404
405
406
		idP :: Int -> .{#Int}
		idP n
			=	{i \\ i <- [0 .. n-1]}

clean's avatar
clean committed
407
408
		swapType :: Int Int Int (*{#Int}, *{#Int},  *a) -> (*{#Int}, *{#Int},  *a) | swapTypes a
		swapType nDclTypes dclIndex iclIndex state=:(p,p`,be)
clean's avatar
clean committed
409
410
411
412
413
414
415
416
417
			#! frm
				=	p.[dclIndex]
			#! to
				=	iclIndex
			| frm == to
				=	state
			// otherwise
				#! frm` = dclIndex
				#! to` = p`.[iclIndex]
clean's avatar
clean committed
418
				#! to` = if (to` >= nDclTypes) frm` to`
clean's avatar
clean committed
419
420
				=	(swap frm` to` p, swap frm to p`, swapTypes frm to be)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
421

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
:: DeclVarsInput :== (!Ident, !VarHeap)

class declareVars a :: a !DeclVarsInput -> Backender

instance declareVars [a] | declareVars a where
	declareVars :: [a] !DeclVarsInput -> Backender | declareVars a
	declareVars list dvInput
		=	foldState (flip declareVars dvInput) list

instance declareVars (Ptr VarInfo) where
	declareVars varInfoPtr (_, varHeap)
		=	declareVariable BELhsNodeId varInfoPtr "_var???" varHeap	// +++ name

instance declareVars FreeVar where
	declareVars :: FreeVar !DeclVarsInput -> Backender
	declareVars freeVar (_, varHeap)
		=	declareVariable BELhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name varHeap

instance declareVars (Bind Expression FreeVar) where
	declareVars :: (Bind Expression FreeVar) !DeclVarsInput -> Backender
	declareVars {bind_src=App {app_symb, app_args=[Var _:_]}, bind_dst=freeVar} (aliasDummyId, varHeap)
		| app_symb.symb_name==aliasDummyId
			= identity		// we have an alias. Don't declare the same variable twice
		= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name varHeap
	declareVars {bind_dst=freeVar} (_, varHeap)
		= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name varHeap

declareVariable :: Int (Ptr VarInfo) {#Char} !VarHeap -> Backender
declareVariable lhsOrRhs varInfoPtr name varHeap
	=	beDeclareNodeId (getVariableSequenceNumber varInfoPtr varHeap) lhsOrRhs name

instance declareVars (Optional a) | declareVars a where
	declareVars :: (Optional a) !DeclVarsInput -> Backender | declareVars a
	declareVars (Yes x) dvInput
		=	declareVars x dvInput
	declareVars No _
		=	identity

instance declareVars FunctionPattern where
	declareVars :: FunctionPattern !DeclVarsInput -> Backender
	declareVars (FP_Algebraic _ freeVars optionalVar) dvInput
		=	declareVars freeVars dvInput
		o`	declareVars optionalVar dvInput
	declareVars (FP_Variable freeVar) dvInput
		=	declareVars freeVar dvInput
	declareVars (FP_Basic _ optionalVar) dvInput
		=	declareVars optionalVar dvInput
	declareVars FP_Empty dvInput
		=	identity

instance declareVars Expression where
	declareVars :: Expression !DeclVarsInput -> Backender
	declareVars (Let {let_strict_binds, let_lazy_binds, let_expr}) dvInput
		=	declareVars let_strict_binds dvInput
		o`	declareVars let_lazy_binds dvInput
		o`	declareVars let_expr dvInput
	declareVars (Conditional {if_then, if_else}) dvInput
		=	declareVars if_then dvInput
		o`	declareVars if_else dvInput
	declareVars (AnyCodeExpr _ outParams _) (_, varHeap)
		=	foldState (declVar varHeap) outParams 
	  where
		declVar varHeap {bind_dst=freeVar} 
			= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name varHeap
	declareVars _ _
		=	identity

instance declareVars TransformedBody where
	declareVars :: TransformedBody !DeclVarsInput -> Backender
	declareVars {tb_args, tb_rhs} dvInput
		=	declareVars tb_args dvInput
		o`	declareVars tb_rhs dvInput

instance declareVars BackendBody where
	declareVars :: BackendBody !DeclVarsInput -> Backender
	declareVars {bb_args, bb_rhs} dvInput
		=	declareVars bb_args dvInput
		o`	declareVars bb_rhs dvInput

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
501
502
503
504
505
506

:: ModuleIndex :== Index

class declare a :: ModuleIndex !VarHeap a  -> Backender
class declareWithIndex a :: Index ModuleIndex !VarHeap a -> Backender

507
//1.3
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
508
509
instance declare {#a} | declareWithIndex a & ArrayElem a where
	declare :: ModuleIndex  VarHeap {#a} -> Backender | declareWithIndex a & ArrayElem a 
510
511
512
513
514
//3.1
/*2.0
instance declare {#a} | declareWithIndex a & Array {#} a where
	declare :: ModuleIndex  VarHeap {#a} -> Backender | declareWithIndex a & Array {#} a 
0.2*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
515
516
517
	declare moduleIndex varHeap array
		=	foldStateWithIndexA (\i -> declareWithIndex i moduleIndex varHeap) array

518
519
520
declareFunctionSymbols :: {#FunDef} {#Int} [(Int, Int)] IndexRange *BackEnd -> *BackEnd
declareFunctionSymbols functions iclDclConversions functionIndices globalFunctions backEnd
	=	foldr` (declare iclDclConversions) backEnd [(functionIndex, componentIndex, functions.[functionIndex]) \\ (componentIndex, functionIndex) <- functionIndices]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
521
522
523
	where
		declare :: {#Int} (Int, Int, FunDef) *BackEnd -> *BackEnd
		declare iclDclConversions (functionIndex, componentIndex, function) backEnd
524
525
526
527
528
529
530
531
532
533
			=	BEDeclareFunction
					(functionName function.fun_symb.id_name functionIndex iclDclConversions globalFunctions)
					function.fun_arity functionIndex componentIndex backEnd
			where
				functionName :: {#Char} Int {#Int} IndexRange -> {#Char}
				functionName name functionIndex iclDclConversions {ir_from, ir_to}
					| functionIndex >= ir_to || functionIndex < ir_from
						=	(name +++ ";" +++ toString iclDclConversions.[functionIndex])
					// otherwise
						=	name
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

// move to backendsupport
foldStateWithIndexRangeA function frm to array
	:== foldStateWithIndexRangeA frm
	where
		foldStateWithIndexRangeA index
			| index == to
				=	identity
			// otherwise
				=	function index array.[index]
				o`	foldStateWithIndexRangeA (index+1)

declareArrayInstances :: IndexRange {#FunDef} -> Backender
declareArrayInstances {ir_from, ir_to} functions
	=	foldStateWithIndexRangeA declareArrayInstance ir_from ir_to functions
	where
		declareArrayInstance :: Index FunDef -> Backender
		declareArrayInstance index {fun_symb={id_name}, fun_type=Yes type}
			=	beDeclareRuleType index cIclModIndex (id_name +++ ";" +++ toString index)
			o`	beDefineRuleType index cIclModIndex (convertTypeAlt index cIclModIndex type)

instance declare CommonDefs where
	declare :: ModuleIndex VarHeap CommonDefs -> Backender
	declare moduleIndex varHeap {com_cons_defs, com_type_defs, com_selector_defs, com_class_defs}
		=	declare moduleIndex varHeap com_type_defs
		o`	defineTypes moduleIndex com_cons_defs com_selector_defs com_type_defs varHeap

clean's avatar
clean committed
561
instance declareWithIndex (TypeDef a) where
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
562
563
564
565
	declareWithIndex :: Index ModuleIndex VarHeap (TypeDef a) -> Backender
	declareWithIndex typeIndex moduleIndex _ {td_name}
		=	BEDeclareType typeIndex moduleIndex td_name.id_name

566
567
568
declareFunTypes :: ModuleIndex {#FunType} Int VarHeap -> Backender
declareFunTypes moduleIndex funTypes nrOfDclFunctions varHeap
		=	foldStateWithIndexA (declareFunType moduleIndex varHeap nrOfDclFunctions) funTypes
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
569

570
571
declareFunType :: ModuleIndex VarHeap Index Int FunType -> Backender
declareFunType moduleIndex varHeap nrOfDclFunctions functionIndex {ft_symb, ft_type_ptr}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
572
573
	=	case (sreadPtr ft_type_ptr varHeap) of
			VI_ExpandedType expandedType
574
				->	beDeclareRuleType functionIndex moduleIndex (functionName ft_symb.id_name functionIndex nrOfDclFunctions)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
575
576
577
				o`	beDefineRuleType functionIndex moduleIndex (convertTypeAlt functionIndex moduleIndex expandedType)
			_
				->	identity
578
579
580
581
582
583
584
		where
			functionName :: {#Char} Int Int -> {#Char}
			functionName name functionIndex nrOfDclFunctions 
				| functionIndex < nrOfDclFunctions
					=	name
				// otherwise
					=	name +++ ";" +++ toString functionIndex
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
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

currentModuleTypeConversions :: {#ClassDef} {#ClassDef} (Optional ConversionTable) -> {#Int}
currentModuleTypeConversions iclClasses dclClasses (Yes conversionTable)
	// sanity check ...
	| sort [dclClass.class_dictionary.ds_index \\ dclClass <-: dclClasses]
				<> [size typeConversions .. size typeConversions + size dclClasses - 1]
		=	abort "backendconvert, currentModuleTypeConversions wrong index range for dcl dictionary types"
	// ... sanity check
	| nDclClasses == 0
		=	typeConversions
	// otherwise
		=	{createArray (nDclTypes + nDclClasses) NoIndex
				& [i] = typeConversion
					\\ typeConversion <-: typeConversions & i <- [0..]}
			:-  foldStateWithIndexA (updateDictionaryTypeIndex classConversions) classConversions
	where
		typeConversions
			=	conversionTable.[cTypeDefs]
		nDclTypes
			=	size typeConversions
		classConversions
			=	conversionTable.[cClassDefs]
		nDclClasses
			=	size classConversions

		updateDictionaryTypeIndex :: {#Int} Int Int *{#Int} -> *{#Int}
		updateDictionaryTypeIndex classConversions dclClassIndex iclClassIndex allTypeConversions
			// sanity check ...
			# (oldIndex, allTypeConversions)
				=	uselect allTypeConversions dclTypeIndex
			| oldIndex <> NoIndex
				=	abort "backendconvert, updateDictionaryTypeIndex wrong index overwritten"
			// ... sanity chechk
			=	{allTypeConversions & [dclTypeIndex] = iclTypeIndex}
			where
				dclTypeIndex
					=	dclClasses.[dclClassIndex].class_dictionary.ds_index
				iclClassIndex
					=	classConversions.[dclClassIndex]
				iclTypeIndex
					=	iclClasses.[iclClassIndex].class_dictionary.ds_index
currentModuleTypeConversions _ _ No
	=	{}

declareCurrentDclModuleTypes :: {#CheckedTypeDef} {#Int} VarHeap -> Backender
declareCurrentDclModuleTypes dclTypes typeConversions varHeap
	=	foldStateWithIndexA (declareConvertedType dclTypes varHeap) typeConversions
	where
		declareConvertedType :: {#CheckedTypeDef} VarHeap Index Index -> Backender
		declareConvertedType dclTypes varHeap dclIndex iclIndex
			=	declareWithIndex iclIndex cIclModIndex varHeap dclTypes.[dclIndex]

defineCurrentDclModuleTypes :: {#ConsDef} {#SelectorDef} {#CheckedTypeDef} {#Int} VarHeap -> Backender
defineCurrentDclModuleTypes dclConstructors dclSelectors dclTypes typeConversions varHeap
	=	foldStateWithIndexA (defineConvertedType dclTypes varHeap) typeConversions
	where
		defineConvertedType :: {#CheckedTypeDef} VarHeap Index Index -> Backender
		defineConvertedType dclTypes varHeap dclIndex iclIndex
			=	defineType cIclModIndex dclConstructors dclSelectors varHeap iclIndex dclTypes.[dclIndex]

defineTypes :: ModuleIndex {#ConsDef} {#SelectorDef} {#CheckedTypeDef} VarHeap -> Backender
defineTypes moduleIndex constructors selectors types varHeap
	=	foldStateWithIndexA (defineType moduleIndex constructors selectors varHeap) types

convertTypeLhs :: ModuleIndex Index  [ATypeVar] -> BEMonad BEFlatTypeP
convertTypeLhs moduleIndex typeIndex args
	=	beFlatType (beTypeSymbol typeIndex moduleIndex) (convertTypeVars args)

convertTypeVars :: [ATypeVar] -> BEMonad BETypeVarListP
convertTypeVars typeVars
655
	=	foldr` (beTypeVars o convertTypeVar) beNoTypeVars typeVars
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
656
657
658
659
660
661

convertTypeVar :: ATypeVar -> BEMonad BETypeVarP
convertTypeVar typeVar
	=	beTypeVar typeVar.atv_variable.tv_name.id_name

defineType :: ModuleIndex {#ConsDef} {#SelectorDef} VarHeap Index CheckedTypeDef *BackEnd -> *BackEnd
clean's avatar
clean committed
662
defineType moduleIndex constructors _ varHeap typeIndex {td_name, td_args, td_rhs=AlgType constructorSymbols} be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
663
664
665
	# (flatType, be)
		=	convertTypeLhs moduleIndex typeIndex td_args be
	# (constructors, be)
clean's avatar
clean committed
666
667
		=	convertConstructors typeIndex td_name.id_name moduleIndex constructors constructorSymbols varHeap be
	=	BEAlgebraicType flatType constructors be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
668
669
670
671
672
673
674
675
676
677
defineType moduleIndex constructors selectors varHeap typeIndex {td_args, td_rhs=RecordType {rt_constructor, rt_fields}} be
	# (flatType, be)
		=	convertTypeLhs moduleIndex typeIndex td_args be
	# (fields, be)
		=	convertSelectors moduleIndex selectors rt_fields varHeap be
	# (constructorTypeNode, be)
		=	beNormalTypeNode
				(beConstructorSymbol moduleIndex constructorIndex)
				(convertSymbolTypeArgs constructorType)
				be
clean's avatar
clean committed
678
	=	BERecordType moduleIndex flatType constructorTypeNode fields be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
679
680
681
682
683
684
685
686
687
688
689
	where
		constructorIndex
			=	rt_constructor.ds_index
		constructorDef
			=	constructors.[constructorIndex]
		constructorType
			=	case (sreadPtr constructorDef.cons_type_ptr varHeap) of
					VI_ExpandedType expandedType
						->	expandedType
					_
						->	constructorDef.cons_type
clean's avatar
clean committed
690
691
defineType moduleIndex _ _ _ typeIndex {td_args, td_rhs=AbstractType _} be
 	=	beAbsType (convertTypeLhs moduleIndex typeIndex td_args) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
692
693
694
defineType _ _ _ _ _ _ be
	=	be

clean's avatar
clean committed
695
696
convertConstructors :: Int {#Char} ModuleIndex {#ConsDef} [DefinedSymbol] VarHeap -> BEMonad BEConstructorListP
convertConstructors typeIndex typeName moduleIndex constructors symbols varHeap
697
	=	foldr` (beConstructors o convertConstructor typeIndex typeName moduleIndex constructors varHeap) beNoConstructors symbols
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
698

clean's avatar
clean committed
699
700
convertConstructor :: Int {#Char} ModuleIndex {#ConsDef} VarHeap DefinedSymbol -> BEMonad BEConstructorListP
convertConstructor typeIndex typeName moduleIndex constructorDefs varHeap {ds_index}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
701
702
703
704
705
706
707
708
709
710
711
	=	BEDeclareConstructor ds_index moduleIndex constructorDef.cons_symb.id_name // +++ remove declare
	o`	beConstructor
			(beNormalTypeNode
				(beConstructorSymbol moduleIndex ds_index)
				(convertSymbolTypeArgs constructorType)) 
	where
		constructorDef
			=	constructorDefs.[ds_index]
		constructorType
			=	case (sreadPtr constructorDef.cons_type_ptr varHeap) of
					VI_ExpandedType expandedType
clean's avatar
clean committed
712
						->	expandedType // ->> (typeName, typeIndex, constructorDef.cons_symb.id_name, ds_index, expandedType)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
713
					_
clean's avatar
clean committed
714
						->	constructorDef.cons_type // ->> (typeName, typeIndex, constructorDef.cons_symb.id_name, ds_index, constructorDef.cons_type)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863

convertSelectors :: ModuleIndex {#SelectorDef} {#FieldSymbol} VarHeap -> BEMonad BEFieldListP
convertSelectors moduleIndex selectors symbols varHeap
	=	foldrA (beFields o convertSelector moduleIndex selectors varHeap) beNoFields symbols

convertSelector :: ModuleIndex {#SelectorDef} VarHeap FieldSymbol -> BEMonad BEFieldListP
convertSelector moduleIndex selectorDefs varHeap {fs_index}
	=	BEDeclareField fs_index moduleIndex selectorDef.sd_symb.id_name
	o`	beField fs_index moduleIndex (convertAnnotTypeNode (selectorType.st_result))
	where
		selectorDef
			=	selectorDefs.[fs_index]
		selectorType
			=	case (sreadPtr selectorDef.sd_type_ptr varHeap) of
					VI_ExpandedType expandedType
						->	expandedType
					_
						->	selectorDef.sd_type

predefineSymbols :: DclModule PredefinedSymbols -> Backender
predefineSymbols {dcl_common} predefs
	=	BEDeclarePredefinedModule (size dcl_common.com_type_defs) (size dcl_common.com_cons_defs)
	o`	foldState predefineType types
	o`	foldState predefineConstructor constructors
	where
		predefineType (index, arity, symbolKind)
			// sanity check ...
			| predefs.[index].pds_def == NoIndex
				=	abort "backendconvert, predefineSymbols predef is not a type"
			// ... sanity check
			=	BEPredefineTypeSymbol arity predefs.[index].pds_def cPredefinedModuleIndex symbolKind

		predefineConstructor (index, arity, symbolKind)
			// sanity check ...
			| predefs.[index].pds_def == NoIndex
				=	abort "backendconvert, predefineSymbols predef is not a constructor"
			// ... sanity check
			=	BEPredefineConstructorSymbol arity predefs.[index].pds_def cPredefinedModuleIndex symbolKind

		types :: [(Int, Int, BESymbKind)]
		types
			=	[	(PD_ListType, 1, BEListType)
				,	(PD_LazyArrayType, 1, BEArrayType)
				,	(PD_StrictArrayType, 1, BEStrictArrayType)
				,	(PD_UnboxedArrayType, 1, BEUnboxedArrayType)
				:	[(index, index-PD_Arity2TupleType+2, BETupleType) \\ index <- [PD_Arity2TupleType..PD_Arity32TupleType]]
				]

		constructors :: [(Int, Int, BESymbKind)]
		constructors
			=	[	(PD_NilSymbol, 0, BENilSymb)
				,	(PD_ConsSymbol, 2, BEConsSymb)
				:	[(index, index-PD_Arity2TupleSymbol+2, BETupleSymb) \\ index <- [PD_Arity2TupleSymbol..PD_Arity32TupleSymbol]]
				]

:: AdjustStdArrayInfo =
	{	asai_moduleIndex	:: !Int
	,	asai_mapping 		:: !{#BEArrayFunKind}
	,	asai_funs			:: !{#FunType}
	,	asai_varHeap	 	:: !VarHeap
	}

adjustArrayFunctions :: PredefinedSymbols IndexRange {#FunDef} {#DclModule} VarHeap -> Backender
adjustArrayFunctions predefs arrayInstancesRange functions dcls varHeap
	=	adjustStdArray arrayInfo predefs stdArray.dcl_common.com_instance_defs
	o`	adjustIclArrayInstances arrayInstancesRange arrayMemberMapping functions
	where
		arrayModuleIndex
			=	predefs.[PD_StdArray].pds_def
		arrayClassIndex
			=	predefs.[PD_ArrayClass].pds_def
		arrayClass
			=	stdArray.dcl_common.com_class_defs.[arrayClassIndex]
		stdArray
			=	dcls.[arrayModuleIndex]
		arrayMemberMapping
			=	getArrayMemberMapping predefs arrayClass.class_members
		arrayInfo
			=	{	asai_moduleIndex	= arrayModuleIndex
				,	asai_mapping 		= arrayMemberMapping
				,	asai_funs			= stdArray.dcl_functions
				,	asai_varHeap		= varHeap
				}

		getArrayMemberMapping :: PredefinedSymbols {#DefinedSymbol} -> {#BEArrayFunKind}
		getArrayMemberMapping predefs members
			// sanity check ...
			| size members <> length (memberIndexMapping predefs)
				=	abort "backendconvert, arrayMemberMapping: incorrect number of members"
			// ... sanity check
			=	{	createArray (size members) BENoArrayFun
				&	[i] = backEndFunKind member.ds_index (memberIndexMapping predefs) \\ member <-: members & i <- [0..]
				}				
			where
				memberIndexMapping :: PredefinedSymbols -> [(!Index, !BEArrayFunKind)]
				memberIndexMapping predefs
					=	[(predefs.[predefIndex].pds_def, backEndArrayFunKind) \\ (predefIndex, backEndArrayFunKind) <- predefMapping]
					where
						predefMapping 
							=	[	(PD_CreateArrayFun,		BECreateArrayFun)
								,	(PD_ArraySelectFun,		BEArraySelectFun)
								,	(PD_UnqArraySelectFun,	BEUnqArraySelectFun)
								,	(PD_ArrayUpdateFun,		BEArrayUpdateFun)
								,	(PD_ArrayReplaceFun,	BEArrayReplaceFun)
								,	(PD_ArraySizeFun,		BEArraySizeFun)
								,	(PD_UnqArraySizeFun,	BEUnqArraySizeFun)
								,	(PD__CreateArrayFun,	BE_CreateArrayFun)
								]

				backEndFunKind :: Index [(!Index, !BEArrayFunKind)] -> BEArrayFunKind
				backEndFunKind memberIndex predefMapping
					=	hd [back \\ (predefMemberIndex, back) <- predefMapping | predefMemberIndex == memberIndex]

		adjustStdArray :: AdjustStdArrayInfo PredefinedSymbols {#ClassInstance} -> Backender
		adjustStdArray arrayInfo predefs instances
			| arrayModuleIndex == NoIndex
				=	identity
			// otherwise
				=	foldStateA (adjustStdArrayInstance arrayClassIndex arrayInfo) instances
			where
				adjustStdArrayInstance :: Index AdjustStdArrayInfo ClassInstance -> Backender
				adjustStdArrayInstance arrayClassIndex arrayInfo=:{asai_moduleIndex} instance`=:{ins_class}
					| ins_class.glob_object.ds_index == arrayClassIndex && ins_class.glob_module == asai_moduleIndex
						=	adjustArrayClassInstance arrayInfo instance`
					// otherwise
						=	identity
					where
						adjustArrayClassInstance :: AdjustStdArrayInfo ClassInstance -> Backender
						adjustArrayClassInstance arrayInfo {ins_members}
							=	foldStateWithIndexA (adjustMember arrayInfo) ins_members
						where
							adjustMember :: AdjustStdArrayInfo Int DefinedSymbol -> Backender
							adjustMember {asai_moduleIndex, asai_mapping, asai_funs, asai_varHeap} offset {ds_index}
								=	case (sreadPtr asai_funs.[ds_index].ft_type_ptr asai_varHeap) of
										VI_ExpandedType _
											->	beAdjustArrayFunction asai_mapping.[offset] ds_index asai_moduleIndex
										_
											->	identity

		adjustIclArrayInstances :: IndexRange {#BEArrayFunKind} {#FunDef} -> Backender
		adjustIclArrayInstances  {ir_from, ir_to} mapping instances
			=	foldStateWithIndexRangeA (adjustIclArrayInstance mapping) ir_from ir_to instances
			where
				adjustIclArrayInstance :: {#BEArrayFunKind} Index FunDef -> Backender
				// for array functions fun_index is not the index in the FunDef array,
				// but its member index in the Array class
				adjustIclArrayInstance mapping index {fun_index}
					=	beAdjustArrayFunction mapping.[fun_index] index cIclModIndex

864
865
convertRules :: Ident [(Int, FunDef)] VarHeap *BackEnd -> (BEImpRuleP, *BackEnd)
convertRules aliasDummyId rules varHeap be
clean's avatar
clean committed
866
867
868
	# (null, be)
		=	BENoRules be
	=	convert rules varHeap null be
869
//	=	foldr` (beRules o flip` convertRule varHeap) beNoRules rules
clean's avatar
clean committed
870
871
872
873
874
875
	where
		convert :: [(Int, FunDef)] VarHeap BEImpRuleP *BackEnd -> (BEImpRuleP, *BackEnd)
		convert [] _ rulesP be
			=	(rulesP, be)
		convert [h:t] varHeap rulesP be
			# (ruleP, be)
876
				=	convertRule aliasDummyId h varHeap be
clean's avatar
clean committed
877
878
879
			# (rulesP, be)
				=	BERules ruleP rulesP be
			=	convert t varHeap rulesP be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
880

881
882
883
884
885
convertRule :: Ident (Int,FunDef) VarHeap -> BEMonad BEImpRuleP
convertRule aliasDummyId (index, {fun_type=Yes type, fun_body=body, fun_pos, fun_kind, fun_symb}) varHeap
	=	beRule index (cafness fun_kind)
			(convertTypeAlt index cIclModIndex (type /* ->> ("convertRule", fun_symb.id_name, index, type) */))
			(convertFunctionBody index (positionToLineNumber fun_pos) aliasDummyId body varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
	where
		cafness :: FunKind -> Int
		cafness (FK_Function _)
			=	BEIsNotACaf
		cafness FK_Macro
			=	BEIsNotACaf
		cafness FK_Caf
			=	BEIsACaf
		cafness funKind
			=	BEIsNotACaf <<- ("backendconvert, cafness: unknown fun kind", funKind)

		positionToLineNumber :: Position -> Int
		positionToLineNumber (FunPos  _ lineNumber _)
			=	lineNumber
		positionToLineNumber (LinePos _ lineNumber)
			=	lineNumber
		positionToLineNumber _
			=	-1

905
906
907
convertFunctionBody :: Int Int Ident FunctionBody VarHeap -> BEMonad BERuleAltP
convertFunctionBody functionIndex lineNumber aliasDummyId (BackendBody bodies) varHeap
	=	convertBackendBodies functionIndex lineNumber aliasDummyId bodies varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
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

convertTypeAlt :: Int ModuleIndex SymbolType -> BEMonad BETypeAltP
convertTypeAlt functionIndex moduleIndex symbol=:{st_result}
	=	beTypeAlt (beNormalTypeNode (beFunctionSymbol functionIndex moduleIndex) (convertSymbolTypeArgs symbol)) (convertAnnotTypeNode st_result)

convertSymbolTypeArgs :: SymbolType -> BEMonad BETypeArgP
convertSymbolTypeArgs {st_args}
	=	convertTypeArgs st_args

convertBasicTypeKind :: BasicType -> BESymbKind
convertBasicTypeKind BT_Int
	=	BEIntType
convertBasicTypeKind BT_Char
	=	BECharType
convertBasicTypeKind BT_Real
	=	BERealType
convertBasicTypeKind BT_Bool
	=	BEBoolType
convertBasicTypeKind BT_File
	=	BEFileType
convertBasicTypeKind BT_World
	=	BEWorldType
convertBasicTypeKind BT_Dynamic
	=	BEDynamicType
convertBasicTypeKind (BT_String _)
	=	undef <<- "convertBasicTypeKind (BT_String _) shouldn't occur"

convertAnnotation :: Annotation -> BEAnnotation
convertAnnotation AN_None
	=	BENoAnnot
convertAnnotation AN_Strict
	=	BEStrictAnnot

convertAttribution :: TypeAttribute -> BEAttribution
convertAttribution TA_Unique
	=	BEUniqueAttr
convertAttribution _ // +++ uni vars, etc.
	=	BENoUniAttr

convertAnnotTypeNode :: AType -> BEMonad BETypeNodeP
convertAnnotTypeNode {at_type, at_annotation, at_attribute}
	=	convertTypeNode at_type
	:-	beAnnotateTypeNode (convertAnnotation at_annotation)
	:-	beAttributeTypeNode (convertAttribution (at_attribute))

convertTypeNode :: Type -> BEMonad BETypeNodeP
convertTypeNode (TB (BT_String type))
	=	convertTypeNode type
convertTypeNode (TB basicType)
	=	beNormalTypeNode (beBasicSymbol (convertBasicTypeKind basicType)) beNoTypeArgs
convertTypeNode (TA typeSymbolIdent typeArgs)
	=	beNormalTypeNode (convertTypeSymbolIdent typeSymbolIdent) (convertTypeArgs typeArgs)
convertTypeNode (TV {tv_name})
	=	beVarTypeNode tv_name.id_name
convertTypeNode (TempQV n)
	=	beVarTypeNode ("_tqv" +++ toString n)
convertTypeNode (TempV n)
	=	beVarTypeNode ("_tv" +++ toString n)
convertTypeNode (a --> b)
	=	beNormalTypeNode (beBasicSymbol BEFunType) (convertTypeArgs [a, b])
convertTypeNode (a :@: b)
	=	beNormalTypeNode (beBasicSymbol BEApplySymb) (convertTypeArgs [{at_attribute=TA_Multi, at_annotation=AN_None, at_type = consVariableToType a} : b])
convertTypeNode TE
	=	beNormalTypeNode beDontCareDefinitionSymbol beNoTypeArgs
convertTypeNode typeNode
	=	undef <<- ("backendconvert, convertTypeNode: unknown type node", typeNode)

consVariableToType :: ConsVariable -> Type
consVariableToType (CV typeVar)
	=	TV typeVar
consVariableToType (TempCV varId)
	=	TempV varId
consVariableToType (TempQCV varId)
	=	TempQV varId

convertTypeArgs :: [AType] -> BEMonad BETypeArgP
convertTypeArgs args
985
	=	foldr` (beTypeArgs o convertAnnotTypeNode) beNoTypeArgs args
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
986

987
988
989
990
convertBackendBodies :: Int Int Ident [BackendBody] VarHeap -> BEMonad BERuleAltP
convertBackendBodies functionIndex lineNumber aliasDummyId bodies varHeap
	=	foldr (beRuleAlts o (flip (convertBackendBody functionIndex lineNumber aliasDummyId)) varHeap)
				beNoRuleAlts bodies
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
991

992
993
convertBackendBody :: Int Int Ident BackendBody VarHeap -> BEMonad BERuleAltP
convertBackendBody functionIndex lineNumber aliasDummyId body=:{bb_args, bb_rhs=ABCCodeExpr instructions inline} varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
994
	=	beNoNodeDefs ==> \noNodeDefs
995
	->	declareVars body (aliasDummyId, varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
996
997
998
999
1000
	o`	beCodeAlt
			lineNumber
			(convertLhsNodeDefs bb_args noNodeDefs varHeap)
			(convertBackendLhs functionIndex bb_args varHeap)
			(beAbcCodeBlock inline (convertStrings instructions))
1001
convertBackendBody functionIndex lineNumber aliasDummyId body=:{bb_args, bb_rhs=AnyCodeExpr inParams outParams instructions} varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1002
	=	beNoNodeDefs ==> \noNodeDefs
1003
	->	declareVars body (aliasDummyId, varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1004
1005
1006
1007
1008
	o`	beCodeAlt
			lineNumber
			(convertLhsNodeDefs bb_args noNodeDefs varHeap)
			(convertBackendLhs functionIndex bb_args varHeap)
			(beAnyCodeBlock (convertCodeParameters inParams varHeap) (convertCodeParameters outParams varHeap) (convertStrings instructions))
1009
convertBackendBody functionIndex lineNumber aliasDummyId body=:{bb_args, bb_rhs} varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1010
	=	beNoNodeDefs ==> \noNodeDefs
1011
	->	declareVars body (aliasDummyId, varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1012
1013
1014
1015
	o`	beRuleAlt
			lineNumber
			(convertLhsNodeDefs bb_args noNodeDefs varHeap)
			(convertBackendLhs functionIndex bb_args varHeap)
1016
			(convertRhsNodeDefs aliasDummyId bb_rhs varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1017
			(convertRhsStrictNodeIds bb_rhs varHeap)
1018
			(convertRootExpr aliasDummyId bb_rhs varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1019
1020
1021

convertStrings :: [{#Char}] -> BEMonad BEStringListP
convertStrings strings
1022
	=	foldr` (beStrings o beString) beNoStrings strings
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1023
1024
1025

convertCodeParameters :: (CodeBinding a) VarHeap -> BEMonad BECodeParameterP | varInfoPtr a
convertCodeParameters codeParameters varHeap
1026
	=	foldr` (beCodeParameters o flip` convertCodeParameter varHeap) beNoCodeParameters codeParameters
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051

class varInfoPtr a :: a -> VarInfoPtr

instance varInfoPtr BoundVar where
	varInfoPtr boundVar
		=	boundVar.var_info_ptr

instance varInfoPtr FreeVar where
	varInfoPtr freeVar
		=	freeVar.fv_info_ptr

convertCodeParameter :: (Bind String a) VarHeap -> BEMonad BECodeParameterP | varInfoPtr a
convertCodeParameter {bind_src, bind_dst} varHeap
		=	beCodeParameter bind_src (convertVar (varInfoPtr bind_dst) varHeap)

convertTransformedLhs :: Int [FreeVar] VarHeap -> BEMonad BENodeP
convertTransformedLhs functionIndex freeVars varHeap
	=	beNormalNode (beFunctionSymbol functionIndex cIclModIndex) (convertLhsArgs freeVars varHeap)

convertBackendLhs :: Int [FunctionPattern] VarHeap -> BEMonad BENodeP
convertBackendLhs functionIndex patterns varHeap
	=	beNormalNode (beFunctionSymbol functionIndex cIclModIndex) (convertPatterns patterns varHeap)

convertPatterns :: [FunctionPattern] VarHeap -> BEMonad BEArgP
convertPatterns patterns varHeap
1052
	=	foldr` (beArgs o flip` convertPattern varHeap) beNoArgs patterns
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

convertPattern :: FunctionPattern VarHeap -> BEMonad BENodeP
convertPattern (FP_Variable freeVar) varHeap
	=	convertFreeVarPattern freeVar varHeap
convertPattern (FP_Basic _ (Yes freeVar)) varHeap
	=	convertFreeVarPattern freeVar varHeap
convertPattern (FP_Basic value No) _
	=	beNormalNode (convertLiteralSymbol value) beNoArgs
convertPattern (FP_Algebraic _ freeVars (Yes freeVar)) varHeap
	=	convertFreeVarPattern freeVar varHeap
convertPattern (FP_Algebraic {glob_module, glob_object={ds_index}} subpatterns No) varHeap
	=	beNormalNode (beConstructorSymbol glob_module ds_index) (convertPatterns subpatterns varHeap)
convertPattern (FP_Dynamic _ _ _ (Yes freeVar)) varHeap
	=	convertFreeVarPattern freeVar varHeap
convertPattern FP_Empty varHeap
	=	beNodeIdNode beWildCardNodeId beNoArgs

convertFreeVarPattern :: FreeVar  VarHeap -> BEMonad BENodeP
convertFreeVarPattern freeVar varHeap
	=	beNodeIdNode (convertVar freeVar.fv_info_ptr varHeap) beNoArgs

convertLhsArgs :: [FreeVar] VarHeap -> BEMonad BEArgP
convertLhsArgs freeVars varHeap
1076
	=	foldr` (beArgs o (flip` convertFreeVarPattern) varHeap) beNoArgs freeVars
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1077
1078
1079
1080
1081
1082
1083

convertVarPtr :: VarInfoPtr  VarHeap -> BEMonad BENodeP
convertVarPtr var varHeap
	=	beNodeIdNode (convertVar var varHeap) beNoArgs

convertVars :: [VarInfoPtr] VarHeap -> BEMonad BEArgP
convertVars vars varHeap
1084
	=	foldr` (beArgs o flip` convertVarPtr varHeap) beNoArgs vars
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1085

1086
1087
1088
1089
1090
1091
1092
1093
convertRootExpr :: Ident Expression VarHeap -> BEMonad BENodeP
convertRootExpr aliasDummyId (Let {let_expr}) varHeap
	=	convertRootExpr aliasDummyId let_expr varHeap
convertRootExpr aliasDummyId (Conditional {if_cond=cond, if_then=then, if_else=Yes else}) varHeap
	=	convertConditional aliasDummyId cond then else varHeap
	where
		convertConditional :: Ident Expression Expression Expression VarHeap -> BEMonad BENodeP
		convertConditional aliasDummyId cond then else varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1094
1095
			=	beGuardNode
					(convertExpr cond varHeap)
1096
					(convertRhsNodeDefs aliasDummyId then varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1097
					(convertRhsStrictNodeIds then varHeap)
1098
1099
					(convertRootExpr aliasDummyId then varHeap)
					(convertRhsNodeDefs aliasDummyId else varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1100
					(convertRhsStrictNodeIds else varHeap)
1101
1102
					(convertRootExpr aliasDummyId else varHeap)
convertRootExpr aliasDummyId (Conditional {if_cond=cond, if_then=then, if_else=No}) varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1103
1104
		=	beGuardNode
				(convertExpr cond varHeap)
1105
				(convertRhsNodeDefs aliasDummyId then varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1106
				(convertRhsStrictNodeIds then varHeap)
1107
				(convertRootExpr aliasDummyId then varHeap)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1108
1109
1110
				beNoNodeDefs
				beNoStrictNodeIds
				(beNormalNode (beBasicSymbol BEFailSymb) beNoArgs)
1111
convertRootExpr _ expr varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
	=	convertExpr expr varHeap

// RWS +++ rewrite
convertLhsNodeDefs :: [FunctionPattern] BENodeDefP VarHeap -> BEMonad BENodeDefP
convertLhsNodeDefs [FP_Basic value (Yes freeVar) : patterns] nodeDefs varHeap
	=	convertLhsNodeDefs patterns nodeDefs varHeap ==> \nodeDefs
	->	defineLhsNodeDef freeVar (FP_Basic value No) nodeDefs varHeap
convertLhsNodeDefs [FP_Algebraic symbol subpatterns (Yes freeVar) : patterns] nodeDefs varHeap
	=	convertLhsNodeDefs subpatterns nodeDefs varHeap ==> \nodeDefs
	->	convertLhsNodeDefs patterns nodeDefs varHeap ==> \nodeDefs
	->	defineLhsNodeDef freeVar (FP_Algebraic symbol subpatterns No) nodeDefs varHeap
convertLhsNodeDefs [FP_Algebraic symbol subpatterns No : patterns] nodeDefs varHeap
	=	convertLhsNodeDefs subpatterns nodeDefs varHeap ==> \nodeDefs
	->	convertLhsNodeDefs patterns nodeDefs varHeap
convertLhsNodeDefs [FP_Dynamic varPtrs var typeCode (Yes freeVar) : patterns] nodeDefs varHeap
	=	convertLhsNodeDefs patterns nodeDefs varHeap ==> \nodeDefs
	->	defineLhsNodeDef freeVar (FP_Dynamic varPtrs var typeCode No) nodeDefs varHeap
convertLhsNodeDefs [_ : patterns] nodeDefs varHeap
	=	convertLhsNodeDefs patterns nodeDefs varHeap
convertLhsNodeDefs [] nodeDefs varHeap
	=	return nodeDefs

defineLhsNodeDef :: FreeVar FunctionPattern BENodeDefP VarHeap -> BEMonad BENodeDefP
defineLhsNodeDef freeVar pattern nodeDefs varHeap
	=	beNodeDefs
			(beNodeDef (getVariableSequenceNumber freeVar.fv_info_ptr varHeap) (convertPattern pattern varHeap))
			(return nodeDefs)

1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
collectNodeDefs :: Ident Expression -> [Bind Expression FreeVar]
collectNodeDefs aliasDummyId (Let {let_strict_binds, let_lazy_binds})
	= filterStrictAlias let_strict_binds let_lazy_binds
  where
	filterStrictAlias [] let_lazy_binds
		= let_lazy_binds
	filterStrictAlias [strict_bind=:{bind_src=App app}:strict_binds] let_lazy_binds
		| app.app_symb.symb_name==aliasDummyId
			// the compiled source was a strict alias like "#! x = y"
			= case hd app.app_args of
				Var _
					// the node is still such an alias and must be ignored
					-> filterStrictAlias strict_binds let_lazy_binds
				hd_app_args
					// the node is not an alias anymore: remove just the _dummyForStrictAlias call
					-> [{ strict_bind & bind_src = hd_app_args } : filterStrictAlias strict_binds let_lazy_binds]
	filterStrictAlias [strict_bind:strict_binds] let_lazy_binds
		= [strict_bind: filterStrictAlias strict_binds let_lazy_binds]
collectNodeDefs _ _
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1159
1160
	=	[]

1161
1162
1163
convertRhsNodeDefs :: Ident Expression VarHeap -> BEMonad BENodeDefP
convertRhsNodeDefs aliasDummyId expr varHeap
	=	convertNodeDefs (collectNodeDefs aliasDummyId expr) varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1164
1165
1166
1167
1168
1169
1170

convertNodeDef :: !(Bind Expression FreeVar) VarHeap -> BEMonad BENodeDefP
convertNodeDef {bind_src=expr, bind_dst=freeVar} varHeap
	=	beNodeDef (getVariableSequenceNumber freeVar.fv_info_ptr varHeap) (convertExpr expr varHeap)

convertNodeDefs :: [Bind Expression FreeVar] VarHeap -> BEMonad BENodeDefP
convertNodeDefs binds varHeap
1171
	=	foldr` (beNodeDefs o flip` convertNodeDef varHeap) beNoNodeDefs binds
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184

collectStrictNodeIds :: Expression -> [FreeVar]
collectStrictNodeIds (Let {let_strict_binds, let_expr})
	=	[bind_dst \\ {bind_dst} <- let_strict_binds]
collectStrictNodeIds _
	=	[]

convertStrictNodeId :: FreeVar VarHeap -> BEMonad BEStrictNodeIdP
convertStrictNodeId freeVar varHeap
	=	beStrictNodeId (convertVar freeVar.fv_info_ptr varHeap)

convertStrictNodeIds :: [FreeVar] VarHeap -> BEMonad BEStrictNodeIdP
convertStrictNodeIds freeVars varHeap
1185
	=	foldr` (beStrictNodeIds o flip` convertStrictNodeId varHeap) beNoStrictNodeIds freeVars
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204

convertRhsStrictNodeIds :: Expression VarHeap -> BEMonad BEStrictNodeIdP
convertRhsStrictNodeIds expression varHeap
	=	convertStrictNodeIds (collectStrictNodeIds expression) varHeap

convertLiteralSymbol :: BasicValue -> BEMonad BESymbolP
convertLiteralSymbol (BVI string)
	=	beLiteralSymbol BEIntDenot string
convertLiteralSymbol (BVB bool)
	=	beBoolSymbol bool
convertLiteralSymbol (BVC string)
	=	beLiteralSymbol BECharDenot string
convertLiteralSymbol (BVR string)
	=	beLiteralSymbol BERealDenot string
convertLiteralSymbol (BVS string)
	=	beLiteralSymbol BEStringDenot string

convertArgs :: [Expression] VarHeap -> BEMonad BEArgP
convertArgs exprs varHeap
1205
	=	foldr` (beArgs o flip` convertExpr varHeap) beNoArgs exprs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1206
1207
1208
1209
1210
1211
1212

convertSymbol :: !SymbIdent -> BEMonad BESymbolP
convertSymbol {symb_kind=SK_Function {glob_module, glob_object}}
	=	beFunctionSymbol glob_object glob_module
convertSymbol {symb_kind=SK_GeneratedFunction _ index}
	=	beFunctionSymbol index cIclModIndex
convertSymbol {symb_kind=SK_Constructor {glob_module, glob_object}}
clean's avatar
clean committed
1213
	=	beConstructorSymbol glob_module glob_object // ->> ("convertSymbol", (glob_module, glob_object))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1214
1215
1216
1217
1218
convertSymbol symbol
	=	undef <<- ("backendconvert, convertSymbol: unknown symbol", symbol)

convertTypeSymbolIdent :: TypeSymbIdent -> BEMonad BESymbolP
convertTypeSymbolIdent {type_index={glob_module, glob_object}}
clean's avatar
clean committed
1219
	=	beTypeSymbol glob_object glob_module // ->> ("convertTypeSymbolIdent", (glob_module, glob_object))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250

convertExpr :: Expression VarHeap -> BEMonad BENodeP
convertExpr  (BasicExpr value _) varHeap
	=	beNormalNode (convertLiteralSymbol value) beNoArgs
convertExpr  (App {app_symb, app_args}) varHeap
	=	beNormalNode (convertSymbol app_symb) (convertArgs app_args varHeap)
convertExpr (Var var) varHeap
	=	beNodeIdNode (convertVar var.var_info_ptr varHeap) beNoArgs
convertExpr (f @ [a]) varHeap
	=	beNormalNode (beBasicSymbol BEApplySymb) (convertArgs [f, a] varHeap)
convertExpr (f @ [a:as]) varHeap
	=	convertExpr (f @ [a] @ as) varHeap
convertExpr (Selection isUnique expression selections) varHeap
	=	convertSelections (convertExpr expression varHeap) varHeap (addKinds isUnique selections)
	where
		addKinds No selections
			=	[(BESelector, selection) \\ selection <- selections]
		addKinds _ [selection]
			=	[(BESelector_U, selection)]
		addKinds _ [selection : selections]
			=	[(BESelector_F, selection) : addMoreKinds selections]
			where
				addMoreKinds []
					=	[]
				addMoreKinds [selection]
					=	[(BESelector_L, selection)]
				addMoreKinds [selection : selections]
					=	[(BESelector_N, selection) : addMoreKinds selections]
		addKinds _ []
			=	[]
convertExpr (RecordUpdate _ expr updates) varHeap
1251
	=	foldl (convertUpdate varHeap) (convertExpr expr varHeap) updates -*-> "be: RecordUpdate"
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1252
	where
clean's avatar
clean committed
1253
		convertUpdate varHeap  expr {bind_src=NoBind _}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
			=	expr
		convertUpdate varHeap expr {bind_src, bind_dst=bind_dst=:{glob_module, glob_object={fs_index}}}
			=	beUpdateNode
					(beArgs
						expr
						(beArgs
							(beSelectorNode BESelector (beFieldSymbol fs_index glob_module)
							(beArgs (convertExpr bind_src varHeap)
							beNoArgs))
						beNoArgs))
convertExpr (Update expr1 [singleSelection] expr2) varHeap
	=	case singleSelection of
			RecordSelection _ _
1267
				->	beUpdateNode (convertArgs [expr1, Selection No expr2 [singleSelection]] varHeap) -*-> "be: Update [single]"
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1268
			ArraySelection {glob_object={ds_index}, glob_module} _ index
clean's avatar
clean committed
1269
// RWS not used?, eleminate beSpecialArrayFunctionSymbol?
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1270
1271
1272
				->	beNormalNode
						(beSpecialArrayFunctionSymbol BEArrayUpdateFun ds_index glob_module)
						(convertArgs [expr1, index, expr2] varHeap)
clean's avatar
clean committed
1273
//
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
			DictionarySelection dictionaryVar dictionarySelections _ index
				->	convertExpr (Selection No (Var dictionaryVar) dictionarySelections @ [expr1, index, expr2]) varHeap
convertExpr (Update expr1 selections expr2) varHeap
	=	case lastSelection of
			RecordSelection _ _
				->	beUpdateNode (beArgs selection (convertArgs [Selection No expr2 [lastSelection]] varHeap))
			ArraySelection {glob_object={ds_index}, glob_module} _ index
				->	beNormalNode (beSpecialArrayFunctionSymbol BE_ArrayUpdateFun ds_index glob_module) (beArgs selection (convertArgs [index, expr2] varHeap))
			DictionarySelection dictionaryVar dictionarySelections _ index
				->	beNormalNode beDictionaryUpdateFunSymbol
							(beArgs dictionary (beArgs selection (convertArgs [index, expr2] varHeap)))
					with
						dictionary
							=	convertExpr (Selection No (Var dictionaryVar) dictionarySelections) varHeap
	where
		lastSelection
			=	last selections
		selection
			=	convertSelections (convertExpr expr1 varHeap) varHeap (addKinds (init selections))
		addKinds [selection : selections]
			=	[(BESelector_F, selection) : addMoreKinds selections]
			where
				addMoreKinds selections
					=	[(BESelector_U, selection) \\ selection <- selections]
		addKinds []
			=	[]
convertExpr (TupleSelect {ds_arity} n expr) varHeap
	=	beTupleSelectNode ds_arity n (convertExpr expr varHeap)
convertExpr (MatchExpr optionalTuple {glob_module, glob_object={ds_index}} expr) varHeap
	=	beMatchNode (arity optionalTuple) (beConstructorSymbol glob_module ds_index) (convertExpr expr varHeap)
	where
		arity :: (Optional (Global DefinedSymbol)) -> Int
		arity No
			=	1
		arity (Yes {glob_object={ds_arity}})
			=	ds_arity
convertExpr (Conditional {if_cond=cond, if_then, if_else=Yes else}) varHeap
	=	beIfNode (convertExpr cond varHeap) (convertExpr if_then varHeap) (convertExpr else varHeap)
convertExpr  expr _
	=	undef <<- ("backendconvert, convertExpr: unknown expression", expr)


convertSelections :: (BEMonad BENodeP) VarHeap [(BESelectorKind, Selection)] -> (BEMonad BENodeP)
convertSelections expression varHeap selections
	=	foldl (convertSelection varHeap) expression selections

convertSelection :: VarHeap (BEMonad BENodeP) (BESelectorKind, Selection) -> (BEMonad BENodeP)
convertSelection varHeap expression (kind, RecordSelection {glob_object={ds_index}, glob_module} _)
	=	beSelectorNode kind (beFieldSymbol ds_index glob_module) (beArgs expression beNoArgs)
convertSelection varHeap expression (kind, ArraySelection {glob_object={ds_index}, glob_module} _ index)
	=	beNormalNode (beSpecialArrayFunctionSymbol (selectionKindToArrayFunKind kind) ds_index glob_module) (beArgs expression (convertArgs [index] varHeap))
convertSelection varHeap expression (kind, DictionarySelection dictionaryVar dictionarySelections _ index)
	=	case kind of
			BESelector
				->	beNormalNode (beBasicSymbol BEApplySymb)
							(beArgs
								(beNormalNode (beBasicSymbol BEApplySymb)
								(beArgs dictionary
									(beArgs expression beNoArgs)))
							(convertArgs [index] varHeap))
			_
				->	beNormalNode beDictionarySelectFunSymbol
							(beArgs dictionary (beArgs expression (convertArgs [index] varHeap)))
		where
			dictionary
				=	convertExpr (Selection No (Var dictionaryVar) dictionarySelections) varHeap

selectionKindToArrayFunKind BESelector
	=	BEArraySelectFun
selectionKindToArrayFunKind BESelector_U
	=	BE_UnqArraySelectFun
selectionKindToArrayFunKind BESelector_F
	=	BE_UnqArraySelectFun
selectionKindToArrayFunKind BESelector_L
	=	BE_UnqArraySelectLastFun
selectionKindToArrayFunKind BESelector_N
	=	BE_UnqArraySelectLastFun

convertVar :: VarInfoPtr VarHeap -> BEMonad BENodeIdP
convertVar varInfo varHeap
	=	beNodeId (getVariableSequenceNumber varInfo varHeap)

getVariableSequenceNumber :: VarInfoPtr VarHeap -> Int
1357
1358
1359
1360
1361
1362
1363
getVariableSequenceNumber varInfoPtr varHeap
	# vi = sreadPtr varInfoPtr varHeap
	= case vi of
		VI_SequenceNumber sequenceNumber
			-> sequenceNumber
		VI_Alias {var_info_ptr}
			-> getVariableSequenceNumber var_info_ptr varHeap
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1364

clean's avatar
clean committed
1365
1366
1367
1368
1369
1370
markExports :: DclModule {#ClassDef} {#CheckedTypeDef} {#ClassDef} {#CheckedTypeDef} (Optional {#Int}) -> Backender
markExports {dcl_conversions = Yes conversionTable} dclClasses dclTypes iclClasses iclTypes (Yes functionConversions)
	=	foldStateA (\icl -> beExportType icl icl) conversionTable.[cTypeDefs]
	o	foldStateWithIndexA beExportConstructor conversionTable.[cConstructorDefs]
	o	foldStateWithIndexA beExportField conversionTable.[cSelectorDefs]
	o	foldStateWithIndexA (exportDictionary iclClasses iclTypes) conversionTable.[cClassDefs]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1371
1372
	o	foldStateWithIndexA beExportFunction functionConversions
	where
clean's avatar
clean committed
1373
1374
1375
		exportDictionary :: {#ClassDef} {#CheckedTypeDef} Index Index -> Backender
		exportDictionary iclClasses iclTypes dclClassIndex iclClassIndex
			=	beExportType (-1) iclTypeIndex	// remove -1 hack
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1376
1377
			o	foldStateA exportDictionaryField rt_fields
			where
clean's avatar
clean committed
1378
1379
1380
1381
				dclTypeIndex
					=	dclClasses.[dclClassIndex].class_dictionary.ds_index
				iclTypeIndex
					=	iclClasses.[iclClassIndex].class_dictionary.ds_index
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1382
				{td_rhs = RecordType {rt_fields}}
clean's avatar
clean committed
1383
					=	iclTypes.[iclTypeIndex]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1384
1385
1386

				exportDictionaryField :: FieldSymbol -> Backender
				exportDictionaryField {fs_index}
clean's avatar
clean committed
1387
1388
					=	beExportField (-1) fs_index	// remove -1 hack
markExports _ _ _ _ _ _
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1389
	=	identity