backendconvert.icl 81.5 KB
Newer Older
1
2
3
/*
	module owner: Ronny Wichers Schreur
*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
4
5
6
implementation module backendconvert

import code from library "backend_library"
7
import compilerSwitches
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
8
9
10
11
12
13

import StdEnv

import frontend
import backend
import backendsupport, backendpreprocess
14

clean's avatar
clean committed
15
16
17
// trace macro
(-*->) infixl
(-*->) value trace
18
	:==	value //---> trace
19
20
21
22
23
24
25
26
27
28
29
30
/*
sfoldr op r l
	:== foldr l
	where
		foldr [] = r
		foldr [a:x] = \s -> op a (foldr x) s
*/
sfoldr op r l s
	:== foldr l s
	where
		foldr [] = r
		foldr [a:x] = op a (foldr x)
clean's avatar
clean committed
31

32
::	FunctionPattern	= FP_Algebraic !(Global DefinedSymbol) ![FunctionPattern]
33
34
					| FP_Variable !FreeVar

35
:: BEMonad a :== *BackEndState -> *(!a,!*BackEndState)
36
:: BackEnder :== *BackEndState -> *BackEndState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
37

38
//
39
:: *BackEndState = {bes_backEnd :: !BackEnd, bes_varHeap :: !*VarHeap, bes_attrHeap :: !*AttrVarHeap, bes_attr_number :: !Int}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
40

41
42
43
44
appBackEnd f beState
	:== {beState & bes_backEnd = bes_backEnd}
	where
		bes_backEnd = f beState.bes_backEnd
45

46
47
48
49
50
51
52
accBackEnd f beState
	:== accBackEnd
	where
		accBackEnd
			# (result, bes_backEnd) =	f beState.bes_backEnd
			#! beState2 = {beState & bes_backEnd = bes_backEnd}
			= (result,beState2)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
53

54
55
accVarHeap f beState
	:== (result, {beState & bes_varHeap = varHeap})
56
	where
57
		(result, varHeap) =	f beState.bes_varHeap
58

59
60
61
62
63
64
65
accAttrHeap f beState
	:== (result, {beState & bes_attrHeap = attrHeap})
	where
		(result, attrHeap) =	f beState.bes_attrHeap


read_from_var_heap :: VarInfoPtr BackEndState -> (VarInfo, BackEndState)
66
read_from_var_heap ptr beState
67
68
69
	= (result, {beState & bes_varHeap = varHeap})
where
		(result, varHeap) =	readPtr ptr beState.bes_varHeap
70

71
72
write_to_var_heap ptr v beState
	= {beState & bes_varHeap = writePtr ptr v beState.bes_varHeap}
73
74
75
76
77
78
79
80

read_from_attr_heap ptr beState
	= (result, {beState & bes_attrHeap = attrHeap})
where
		(result, attrHeap) =	readPtr ptr beState.bes_attrHeap

write_to_attr_heap ptr v beState
	= {beState & bes_attrHeap = writePtr ptr v beState.bes_attrHeap}
81
82
83
/*
read_from_var_heap ptr heap be
	= (sreadPtr ptr heap,be)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
84

85
86
87
88
::	*BackEndState :== BackEnd

appBackEnd f beState :== f beState
accBackEnd f beState :== f beState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
89
accVarHeap f beState :== f beState
90
*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
91

92
beApFunction0 f
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
93
	:== appBackEnd f
94
beApFunction1 f m1
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
95
96
	:== m1 ==> \a1
	->	appBackEnd (f a1)
97
beApFunction2 f m1 m2
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
98
99
100
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	appBackEnd (f a1 a2)
101
beApFunction3 f m1 m2 m3
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
102
103
104
105
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	appBackEnd (f a1 a2 a3)
106
beApFunction4 f m1 m2 m3 m4
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
107
108
109
110
111
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	appBackEnd (f a1 a2 a3 a4)
112
beApFunction5 f m1 m2 m3 m4 m5
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
113
114
115
116
117
118
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	appBackEnd (f a1 a2 a3 a4 a5)
119
beApFunction6 f m1 m2 m3 m4 m5 m6
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
120
121
122
123
124
125
126
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	m6 ==> \a6
	->	appBackEnd (f a1 a2 a3 a4 a5 a6)
127
beApFunction7 f m1 m2 m3 m4 m5 m6 m7
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
128
129
130
131
132
133
134
135
136
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	m6 ==> \a6
	->	m7 ==> \a7
	->	appBackEnd (f a1 a2 a3 a4 a5 a6 a7)

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
beFunction0 f
	:== accBackEnd f
beFunction1 f m1
	:== m1 ==> \a1
	->	accBackEnd (f a1)
beFunction2 f m1 m2
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	accBackEnd (f a1 a2)
beFunction3 f m1 m2 m3
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	accBackEnd (f a1 a2 a3)
beFunction4 f m1 m2 m3 m4
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	accBackEnd (f a1 a2 a3 a4)
beFunction5 f m1 m2 m3 m4 m5
	:== m1 ==> \a1
	->	m2 ==> \a2
	->	m3 ==> \a3
	->	m4 ==> \a4
	->	m5 ==> \a5
	->	accBackEnd (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
	->	accBackEnd (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
	->	accBackEnd (f a1 a2 a3 a4 a5 a6 a7)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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)
199
200
201
202

beOverloadedConsSymbol moduleIndex constructorIndex deconsModuleIndex deconsIndex
	:==	beFunction0 (BEOverloadedConsSymbol constructorIndex moduleIndex deconsIndex deconsModuleIndex)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
203
204
205
206
beFieldSymbol fieldIndex moduleIndex
	:==	beFunction0 (BEFieldSymbol fieldIndex moduleIndex)
beTypeSymbol typeIndex moduleIndex
	:==	beFunction0 (BETypeSymbol typeIndex moduleIndex)
207
208
beBasicSymbol symbolIndex
	:==	beFunction0 (BEBasicSymbol symbolIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
231
232
beAddForAllTypeVariables
	:==	beFunction2 BEAddForAllTypeVariables
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
233
234
235
236
237
238
239
240
241
beVarTypeNode name
	:==	beFunction0 (BEVarTypeNode name)
beRuleAlt lineNumber
	:==	beFunction5 (BERuleAlt lineNumber)
beNoRuleAlts
	:==	beFunction0 BENoRuleAlts
beRuleAlts
	:==	beFunction2 BERuleAlts
beTypeAlt
242
	:==	beFunction3 BETypeAlt
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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)
281
282
beAttributeTypeNode
	:==	beFunction2 BEAttributeTypeNode
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
283
beDeclareRuleType functionIndex moduleIndex name
284
	:==	beApFunction0 (BEDeclareRuleType functionIndex moduleIndex name)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
285
beDefineRuleType functionIndex moduleIndex
286
	:==	beApFunction1 (BEDefineRuleType functionIndex moduleIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
306
307
308
	:==	beApFunction0 (BEDeclareNodeId number lhsOrRhs name)
beAdjustArrayFunction backendId functionIndex moduleIndex
	:==	beApFunction0 (BEAdjustArrayFunction backendId functionIndex moduleIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
309
beFlatType
310
	:==	beFunction3 BEFlatType
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
311
312
313
314
315
316
beNoTypeVars
	:==	beFunction0 BENoTypeVars
beTypeVars
	:==	beFunction2 BETypeVars
beTypeVar name
	:==	beFunction0 (BETypeVar name)
317
318
beTypeVarListElem
	:==	beFunction2 BETypeVarListElem
319
320
321
322
323
324
325
326
beExportType isDictionary typeIndex
	:==	beApFunction0 (BEExportType isDictionary typeIndex)
beExportConstructor constructorIndex
	:==	beApFunction0 (BEExportConstructor constructorIndex)
beExportField isDictionaryField fieldIndex
	:==	beApFunction0 (BEExportField isDictionaryField fieldIndex)
beExportFunction functionIndex
	:==	beApFunction0 (BEExportFunction functionIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
327
328
329
330
331
beTupleSelectNode arity index
	:==	beFunction1 (BETupleSelectNode arity index)
beMatchNode arity
	:==	beFunction2 (BEMatchNode arity)
beDefineImportedObjsAndLibs
332
	:== beApFunction2 BEDefineImportedObjsAndLibs
clean's avatar
clean committed
333
beAbsType
334
	:== beApFunction1 BEAbsType
335
336
337
338
339
340
341
342
343
344
345
346
347
348
beSwitchNode
	:==	beFunction2 BESwitchNode
beCaseNode symbolArity
	:== beFunction4 (BECaseNode symbolArity)
bePushNode symbolArity
	:== beFunction3 (BEPushNode symbolArity)
beDefaultNode
	:==	beFunction3 BEDefaultNode
beNoNodeIds
	:==	beFunction0 BENoNodeIds
beNodeIds
	:==	beFunction2 BENodeIds
beNodeIdListElem
	:==	beFunction1 BENodeIdListElem
349
350
351
352
353
354
355
356
357
358
359
360
beAttributeKind
	:== beFunction1 BEAttributeKind
beNoAttributeKinds
	:== beFunction0 BENoAttributeKinds
beAttributeKinds
	:== beFunction2 BEAttributeKinds
beUniVarEquation
	:== beFunction2 BEUniVarEquation
beNoUniVarEquations
	:== beFunction0 BENoUniVarEquations
beUniVarEquationsList
	:== beFunction2 BEUniVarEquationsList
361
362
363
364
beBindSpecialModule specialIdentIndex moduleIndex
	:== beApFunction0 (BEBindSpecialModule specialIdentIndex moduleIndex)
beBindSpecialFunction specialIdentIndex functionIndex moduleIndex
	:== beApFunction0 (BEBindSpecialFunction specialIdentIndex functionIndex moduleIndex)
365

366
367
368
// temporary hack
beDynamicTempTypeSymbol
	:== beFunction0 BEDynamicTempTypeSymbol
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
369
370
371

notYetImplementedExpr :: Expression
notYetImplementedExpr
372
	=	(BasicExpr (BVS "\"error in compiler (something was not implemented by lazy Ronny)\""))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
373

374
backEndConvertModules :: PredefinedSymbols FrontEndSyntaxTree !Int *VarHeap *AttrVarHeap *BackEnd -> (!*VarHeap, *AttrVarHeap, !*BackEnd)
375
376
377
378
/*
backEndConvertModules p s main_dcl_module_n v be
	= (newHeap,backEndConvertModulesH p s v be)
*/
379
380
381
backEndConvertModules p s main_dcl_module_n var_heap attr_var_heap be
	# {bes_varHeap,bes_attrHeap,bes_backEnd} = backEndConvertModulesH p s main_dcl_module_n {bes_varHeap=var_heap,bes_attrHeap=attr_var_heap,bes_backEnd=be, bes_attr_number = 0}
	= (bes_varHeap,bes_attrHeap,bes_backEnd)
382

383
backEndConvertModulesH :: PredefinedSymbols FrontEndSyntaxTree !Int *BackEndState -> *BackEndState
384
backEndConvertModulesH predefs {fe_icl = 
John van Groningen's avatar
John van Groningen committed
385
	fe_icl =: {icl_name, icl_functions, icl_common,icl_global_functions,icl_imported_objects,icl_foreign_exports,icl_used_module_numbers, icl_modification_time},
386
	fe_components, fe_dcls, fe_arrayInstances}
387
	main_dcl_module_n backEnd
388
	// sanity check ...
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
389
390
391
392
393
394
395
//	| cIclModIndex <> kIclModuleIndex || cPredefinedModuleIndex <> kPredefinedModuleIndex
//		=	undef <<- "backendconvert, backEndConvertModules: module index mismatch"
	// ... sanity check
/*
	#  backEnd
		=	ruleDoesNotMatch 1 backEnd
			with
396
397
				ruleDoesNotMatch 0 backEnd
					=	backEnd
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
398
399
	#  backEnd
		=	abort "front end abort" backEnd
400
*/	#! backEnd = appBackEnd (BESetMainDclModuleN main_dcl_module_n) backEnd
401
402
403
	#! backEnd
		=	appBackEnd (BEDeclareModules (size fe_dcls)) backEnd
	#! backEnd
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
404
		=	predefineSymbols fe_dcls.[cPredefinedModuleIndex] predefs backEnd
clean's avatar
clean committed
405
406

	#  currentDcl
407
	   	=	fe_dcls.[main_dcl_module_n]
clean's avatar
clean committed
408
409
410
411
412
413
/*
	#  backEnd
		=	backEnd ->>
				(	"dcl conversions"
				,	currentDcl.dcl_conversions
				,	"dcl constructors"
414
				,	[constructor.cons_ident.id_name \\ constructor <-: currentDcl.dcl_common.com_cons_defs]
clean's avatar
clean committed
415
				,	"dcl selectors"
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
416
				,	[selector.sd_ident.id_name \\ selector <-: currentDcl.dcl_common.com_selector_defs]
clean's avatar
clean committed
417
				,	"dcl types"
418
				,	[type.td_ident.id_name \\ type <-: currentDcl.dcl_common.com_type_defs]
419
				,	"icl constructors"
420
				,	[constructor.cons_ident.id_name \\ constructor <-: icl_common.com_cons_defs]
421
				,	"icl selectors"
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
422
				,	[selector.sd_ident.id_name \\ selector <-: icl_common.com_selector_defs]
clean's avatar
clean committed
423
				,	"icl types"
424
				,	[type.td_ident.id_name \\ type <-: icl_common.com_type_defs]
clean's avatar
clean committed
425
426
				)
*/
427
428
429
430
	#! backEnd
		=	declareCurrentDclModule fe_icl fe_dcls.[main_dcl_module_n] main_dcl_module_n (backEnd -*-> "declareCurrentDclModule")
	#! backEnd
		=	declareOtherDclModules fe_dcls main_dcl_module_n icl_used_module_numbers (backEnd -*-> "declareOtherDclModules")
431
432
433
434
435

// tempory hack
	#! backEnd
		=	declareDynamicTemp predefs (backEnd -*-> "declareDynamicTemp")

436
	#! backEnd
437
		=	defineDclModule main_dcl_module_n fe_dcls.[main_dcl_module_n] (backEnd -*-> "defineDclModule(cIclMoIndex)")
438
	#! backEnd
439
		=	defineOtherDclModules fe_dcls main_dcl_module_n icl_used_module_numbers (backEnd -*-> "defineOtherDclModules")
clean's avatar
clean committed
440

441
	#! backEnd
442
		=	appBackEnd (BEDeclareIclModule icl_name.id_name icl_modification_time (size icl_functions) (size icl_common.com_type_defs) (size icl_common.com_cons_defs) (size icl_common.com_selector_defs)) (backEnd -*-> "BEDeclareIclModule")
443
	#! backEnd
444
		=	declareFunctionSymbols icl_functions functionIndices icl_global_functions (backEnd -*-> "declareFunctionSymbols")
445
	#! backEnd
446
		=	declare main_dcl_module_n icl_common (backEnd -*-> "declare (main_dcl_module_n)")
447
	#! backEnd
448
449
450
451
452
453
454
		=	declareArrayInstances /*fe_arrayInstances.ali_instances_range*/fe_arrayInstances.ali_array_first_instance_indices predefs main_dcl_module_n icl_functions fe_dcls (backEnd -*-> "declareArrayInstances")
	#! backEnd
		=	declareListInstances fe_arrayInstances.ali_list_first_instance_indices PD_UListClass predefs main_dcl_module_n icl_functions fe_dcls backEnd
	#! backEnd
		=	declareListInstances fe_arrayInstances.ali_tail_strict_list_first_instance_indices PD_UTSListClass predefs main_dcl_module_n icl_functions fe_dcls backEnd
	#! backEnd
		=	adjustArrayFunctions /*fe_arrayInstances.ali_instances_range*/fe_arrayInstances.ali_array_first_instance_indices predefs main_dcl_module_n icl_functions fe_dcls icl_common.com_instance_defs icl_used_module_numbers (backEnd -*-> "adjustArrayFunctions")
455
	#! backEnd
456
		=	adjustStrictListFunctions fe_arrayInstances.ali_list_first_instance_indices fe_arrayInstances.ali_tail_strict_list_first_instance_indices predefs fe_dcls icl_used_module_numbers main_dcl_module_n backEnd;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
457
	#! (rules, backEnd)
458
		=	convertRules [(index, icl_functions.[index]) \\ (_, index) <- functionIndices] main_dcl_module_n predefined_idents.[PD_DummyForStrictAliasFun] (backEnd -*-> "convertRules")
459
460
	#! backEnd
		=	appBackEnd (BEDefineRules rules) (backEnd -*-> "BEDefineRules")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
461
462
463
464
	#! backEnd
		=	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
465
				(backEnd -*-> "beDefineImportedObjsAndLibs")
John van Groningen's avatar
John van Groningen committed
466
	#! backEnd = appBackEnd (convertForeignExports icl_foreign_exports main_dcl_module_n) backEnd
467
	#! backEnd
468
		=	markExports fe_dcls.[main_dcl_module_n] dcl_common.com_class_defs dcl_common.com_type_defs icl_common.com_class_defs icl_common.com_type_defs (backEnd -*-> "markExports")
clean's avatar
clean committed
469
470
471
			with
				dcl_common
					=	currentDcl.dcl_common
472
	# backEnd = bindSpecialIdents predefs icl_used_module_numbers backEnd
473
474
	#! backEnd = removeExpandedTypesFromDclModules fe_dcls icl_used_module_numbers backEnd
	=	(backEnd -*-> "backend done")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
475
476
	where
		functionIndices
477
			=	flatten [[(componentIndex, member) \\ member <- group.group_members] \\ group <-: fe_components & componentIndex <- [1..]]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
478

479
declareOtherDclModules :: {#DclModule} Int NumberSet -> BackEnder
480
declareOtherDclModules dcls main_dcl_module_n used_module_numbers
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
481
	=	foldStateWithIndexA declareOtherDclModule dcls
482
483
484
where
	declareOtherDclModule :: ModuleIndex DclModule -> BackEnder
	declareOtherDclModule moduleIndex dclModule
485
		| moduleIndex == main_dcl_module_n || moduleIndex == cPredefinedModuleIndex || not (inNumberSet moduleIndex used_module_numbers)
486
487
488
489
			=	identity
		// otherwise
			=	declareDclModule moduleIndex dclModule

490
491
492
defineOtherDclModules :: {#DclModule} Int NumberSet -> BackEnder
defineOtherDclModules dcls main_dcl_module_n used_module_numbers
	=	foldStateWithIndexA defineOtherDclModule dcls
493
where
494
495
	defineOtherDclModule :: ModuleIndex DclModule -> BackEnder
	defineOtherDclModule moduleIndex dclModule
496
		| moduleIndex == main_dcl_module_n || moduleIndex == cPredefinedModuleIndex || not (inNumberSet moduleIndex used_module_numbers)
497
			=	identity		
498
		// otherwise
499
			=	defineDclModule moduleIndex dclModule
500

501
502
503
504
505
506
507
508
isSystem :: ModuleKind -> Bool
isSystem MK_System
	=	True
isSystem MK_Module
	=	False
isSystem _
	=	abort "backendconvert:isSystem, unknown module kind"

509
declareCurrentDclModule :: IclModule DclModule Int -> BackEnder
510
511
declareCurrentDclModule _ {dcl_module_kind=MK_None} _
	=	identity
512
513
declareCurrentDclModule {icl_common} {dcl_name, dcl_modification_time, dcl_functions, dcl_module_kind, dcl_common} main_dcl_module_n
	=	appBackEnd (BEDeclareDclModule main_dcl_module_n dcl_name.id_name dcl_modification_time  (isSystem dcl_module_kind) (size dcl_functions) (size icl_common.com_type_defs) (size dcl_common.com_cons_defs) (size dcl_common.com_selector_defs))
514

515
declareDclModule :: ModuleIndex DclModule -> BackEnder
516
517
declareDclModule moduleIndex {dcl_name, dcl_modification_time, dcl_common, dcl_functions, dcl_module_kind}
	=	appBackEnd (BEDeclareDclModule moduleIndex dcl_name.id_name dcl_modification_time (isSystem dcl_module_kind) (size dcl_functions) (size dcl_common.com_type_defs) (size dcl_common.com_cons_defs) (size dcl_common.com_selector_defs))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
518

519
520
521
522
defineDclModule :: ModuleIndex DclModule -> BackEnder
defineDclModule moduleIndex {dcl_name, dcl_common, dcl_functions,dcl_instances}
	=	declare moduleIndex dcl_common
	o`	declareFunTypes moduleIndex dcl_functions dcl_instances.ir_from
523

524
removeExpandedTypesFromDclModules :: {#DclModule} NumberSet -> BackEnder
525
526
527
528
529
removeExpandedTypesFromDclModules dcls used_module_numbers
	=	foldStateWithIndexA removeExpandedTypesFromDclModule dcls
where
	removeExpandedTypesFromDclModule :: ModuleIndex DclModule -> BackEnder
	removeExpandedTypesFromDclModule moduleIndex dclModule=:{dcl_functions}
530
		| moduleIndex == cPredefinedModuleIndex || not (inNumberSet moduleIndex used_module_numbers)
531
532
533
534
			= identity
			= foldStateWithIndexA (removeExpandedTypesFromFunType moduleIndex)  dcl_functions
			where
				removeExpandedTypesFromFunType :: ModuleIndex Index FunType -> BackEnder
535
				removeExpandedTypesFromFunType moduleIndex functionIndex {ft_ident, ft_type_ptr}
536
					= \be0 ->	let (ft_type,be) = read_from_var_heap ft_type_ptr be0 in
537
538
539
540
541
						(case ft_type of
							VI_ExpandedType expandedType
								->	write_to_var_heap ft_type_ptr VI_Empty	
							_
								->	identity) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
542

543
:: DeclVarsInput :== Ident
544

545
class declareVars a :: a !DeclVarsInput -> BackEnder
546
547

instance declareVars [a] | declareVars a where
548
	declareVars :: [a] !DeclVarsInput -> BackEnder | declareVars a
549
550
551
552
	declareVars list dvInput
		=	foldState (flip declareVars dvInput) list

instance declareVars (Ptr VarInfo) where
553
	declareVars varInfoPtr _
554
		=	declareVariable BELhsNodeId varInfoPtr "_var???"	// +++ name
555
556

instance declareVars FreeVar where
557
	declareVars :: FreeVar !DeclVarsInput -> BackEnder
558
	declareVars freeVar _
559
		=	declareVariable BELhsNodeId freeVar.fv_info_ptr freeVar.fv_ident.id_name
560

561
562
instance declareVars LetBind where
	declareVars :: LetBind !DeclVarsInput -> BackEnder
563
	declareVars {lb_src=App {app_symb, app_args=[Var _:_]}, lb_dst=freeVar} aliasDummyId
564
		| not (isNilPtr app_symb.symb_ident.id_info) && app_symb.symb_ident==aliasDummyId
565
			= identity		// we have an alias. Don't declare the same variable twice
566
		= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_ident.id_name
567
	declareVars {lb_dst=freeVar} _
568
		= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_ident.id_name
569

570
571
572
declareVariable :: Int (Ptr VarInfo) {#Char} -> BackEnder
declareVariable lhsOrRhs varInfoPtr name
	= \be0 -> let (variable_sequence_number,be) = getVariableSequenceNumber varInfoPtr be0 in
573
		beDeclareNodeId variable_sequence_number lhsOrRhs name be
574
575

instance declareVars (Optional a) | declareVars a where
576
	declareVars :: (Optional a) !DeclVarsInput -> BackEnder | declareVars a
577
578
579
580
581
582
	declareVars (Yes x) dvInput
		=	declareVars x dvInput
	declareVars No _
		=	identity

instance declareVars FunctionPattern where
583
	declareVars :: FunctionPattern !DeclVarsInput -> BackEnder
584
	declareVars (FP_Algebraic _ freeVars) dvInput
585
586
587
588
589
		=	declareVars freeVars dvInput
	declareVars (FP_Variable freeVar) dvInput
		=	declareVars freeVar dvInput

instance declareVars Expression where
590
	declareVars :: Expression !DeclVarsInput -> BackEnder
591
592
593
594
	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
John van Groningen's avatar
John van Groningen committed
595
596
597
	declareVars (Conditional {if_cond, if_then, if_else}) dvInput
		=	declareVars if_cond dvInput
		o`	declareVars if_then dvInput
598
		o`	declareVars if_else dvInput
599
600
	declareVars (Case caseExpr) dvInput
		=	declareVars caseExpr dvInput
601
602
	declareVars (AnyCodeExpr _ outParams _) _
		=	foldState declVar outParams 
603
	  where
604
		declVar {bind_dst=freeVar} 
605
			= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_ident.id_name
606
607
608
609
	declareVars _ _
		=	identity

instance declareVars TransformedBody where
610
	declareVars :: TransformedBody !DeclVarsInput -> BackEnder
611
612
613
614
	declareVars {tb_args, tb_rhs} dvInput
		=	declareVars tb_args dvInput
		o`	declareVars tb_rhs dvInput

615
616
617
618
619
620
621
622
623
624
instance declareVars Case where
	declareVars {case_expr, case_guards, case_default} dvInput
		=	declareVars case_guards dvInput
		o`	declareVars case_default dvInput

instance declareVars CasePatterns where
	declareVars (AlgebraicPatterns _ patterns) dvInput
		=	declareVars patterns dvInput
	declareVars (BasicPatterns _ patterns) dvInput
		=	declareVars patterns dvInput
625
626
	declareVars (OverloadedListPatterns _ decons_expr patterns) dvInput
		=	declareVars patterns dvInput
627
628
629
630
631
632
633
634
635
636

instance declareVars AlgebraicPattern where
	declareVars {ap_vars, ap_expr} dvInput
		=	declareVars ap_vars dvInput
		o`	declareVars ap_expr dvInput

instance declareVars BasicPattern where
	declareVars {bp_expr} dvInput
		=	declareVars bp_expr dvInput

637
class declare a :: ModuleIndex a  -> BackEnder
638

639
class declareWithIndex a :: Index ModuleIndex a -> BackEnder
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
640

641
instance declare {#a} | declareWithIndex a & Array {#} a where
642
643
644
	declare :: ModuleIndex  {#a} -> BackEnder | declareWithIndex a & Array {#} a 
	declare moduleIndex array
		=	foldStateWithIndexA (\i -> declareWithIndex i moduleIndex) array
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
645

646
647
648
declareFunctionSymbols :: {#FunDef} [(Int, Int)] [IndexRange] *BackEndState -> *BackEndState
declareFunctionSymbols functions functionIndices globalFunctions backEnd
	=	foldl declare backEnd [(functionIndex, componentIndex, functions.[functionIndex]) \\ (componentIndex, functionIndex) <- functionIndices]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
649
	where
650
		declare backEnd (functionIndex, componentIndex, function)
651
			=	appBackEnd (BEDeclareFunction (functionName function.fun_ident.id_name functionIndex globalFunctions) 
652
					function.fun_arity functionIndex componentIndex) backEnd
653
			where
654
655
656
657
				functionName :: {#Char} Int [IndexRange] -> {#Char}
				functionName name functionIndex icl_global_functions
//					| trace_t ("|"+++toString functionIndex)
					| index_in_ranges functionIndex icl_global_functions
658
						=	name
659
660
661
662
663
664
						=	(name +++ ";" +++ toString functionIndex)
					where
						index_in_ranges index [{ir_from, ir_to}:ranges]
							= (index>=ir_from && index < ir_to) || index_in_ranges index ranges;
						index_in_ranges index []
							= False
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
665
666
667
668
669
670
671
672
673
674
675
676

// 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)

677
678
679
680
681
682
683
684
685
folds op l r :== folds l r
	where
		folds [] r = r
		folds [a:x]	r = folds x (op a r)

declareArrayInstances :: [Int] /*IndexRange*/ PredefinedSymbols Int {#FunDef} {#DclModule} -> BackEnder
declareArrayInstances [] predefs main_dcl_module_n functions dcls
	= identity
declareArrayInstances array_first_instance_indices /*{ir_from, ir_to}*/ predefs main_dcl_module_n functions dcls
clean's avatar
clean committed
686
//	| trace_tn ("declareArrayInstances "+++toString ir_from+++" "+++toString ir_to)
687
688
//	=	foldStateWithIndexRangeA declareArrayInstance ir_from ir_to functions
	= folds (declareArrayInstances 0) array_first_instance_indices
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
689
	where
690
691
692
693
694
695
696
697
698
699
700
701
702
703
		arrayModuleIndex = predefs.[PD_StdArray].pds_def
		arrayClassIndex = predefs.[PD_ArrayClass].pds_def
		stdArray = dcls.[arrayModuleIndex]
		arrayClass = stdArray.dcl_common.com_class_defs.[arrayClassIndex]
		n_array_class_members=size arrayClass.class_members

		declareArrayInstances :: Int Index *BackEndState -> *BackEndState
		declareArrayInstances member_n first_member_index backend
			| member_n==n_array_class_members
				= backend
				# function_index=first_member_index+member_n
				# backend = declareArrayInstance function_index functions.[function_index] backend
				= declareArrayInstances (member_n+1) first_member_index backend

704
		declareArrayInstance :: Index FunDef -> BackEnder
705
		declareArrayInstance index {fun_ident={id_name}, fun_type=Yes type}
706
707
			=	beDeclareRuleType index main_dcl_module_n (id_name +++ ";" +++ toString index)
			o`	beDefineRuleType index main_dcl_module_n (convertTypeAlt index main_dcl_module_n type)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
708

709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
declareListInstances :: [Int] Int PredefinedSymbols Int {#FunDef} {#DclModule} -> BackEnder
declareListInstances [] predef_list_class_index predefs main_dcl_module_n functions dcls
	= identity
declareListInstances array_first_instance_indices predef_list_class_index predefs main_dcl_module_n functions dcls
	= folds (declareListInstances 0) array_first_instance_indices
	where
		strictListModuleIndex = predefs.[PD_StdStrictLists].pds_def
		listClassIndex = predefs.[predef_list_class_index].pds_def
		stdStrictLists = dcls.[strictListModuleIndex]
		listClass = stdStrictLists.dcl_common.com_class_defs.[listClassIndex]
		n_list_class_members=size listClass.class_members

		declareListInstances :: Int Index *BackEndState -> *BackEndState
		declareListInstances member_n first_member_index backend
			| member_n==n_list_class_members
				= backend
				# function_index=first_member_index+member_n
				# backend = declareListInstance function_index functions.[function_index] backend
				= declareListInstances (member_n+1) first_member_index backend

		declareListInstance :: Index FunDef -> BackEnder
730
		declareListInstance index {fun_ident={id_name}, fun_type=Yes type}
731
732
733
734
//			| trace_tn ("declareListInstance "+++toString index+++" "+++toString main_dcl_module_n)
			=	beDeclareRuleType index main_dcl_module_n (id_name +++ ";" +++ toString index)
			o`	beDefineRuleType index main_dcl_module_n (convertTypeAlt index main_dcl_module_n type)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
735
instance declare CommonDefs where
736
737
738
739
	declare :: ModuleIndex CommonDefs -> BackEnder
	declare moduleIndex {com_cons_defs, com_type_defs, com_selector_defs, com_class_defs}
		=	declare moduleIndex com_type_defs
		o`	defineTypes moduleIndex com_cons_defs com_selector_defs com_type_defs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
740

clean's avatar
clean committed
741
instance declareWithIndex (TypeDef a) where
742
	declareWithIndex :: Index ModuleIndex (TypeDef a) -> BackEnder
743
744
	declareWithIndex typeIndex moduleIndex {td_ident}
		=	appBackEnd (BEDeclareType typeIndex moduleIndex td_ident.id_name)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
745

746
747
748
declareFunTypes :: ModuleIndex {#FunType} Int -> BackEnder
declareFunTypes moduleIndex funTypes nrOfDclFunctions
		=	foldStateWithIndexA (declareFunType moduleIndex nrOfDclFunctions) funTypes
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
749

750
declareFunType :: ModuleIndex Index Int FunType -> BackEnder
751
declareFunType moduleIndex nrOfDclFunctions functionIndex {ft_ident, ft_type_ptr}
752
	= \be0 -> let (vi,be) = read_from_var_heap ft_type_ptr be0 in
753
754
					(case vi of
						VI_ExpandedType expandedType
755
							->	beDeclareRuleType functionIndex moduleIndex (functionName ft_ident.id_name functionIndex nrOfDclFunctions)
756
757
758
							o`	beDefineRuleType functionIndex moduleIndex (convertTypeAlt functionIndex moduleIndex expandedType)
						_
							->	identity) be
759
760
761
		where
			functionName :: {#Char} Int Int -> {#Char}
			functionName name functionIndex nrOfDclFunctions 
762
763
//				| trace_tn (name+++(if (functionIndex < nrOfDclFunctions) "" (";" +++ toString functionIndex)))

764
765
766
767
				| functionIndex < nrOfDclFunctions
					=	name
				// otherwise
					=	name +++ ";" +++ toString functionIndex
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
768

769
770
771
defineTypes :: ModuleIndex {#ConsDef} {#SelectorDef} {#CheckedTypeDef} -> BackEnder
defineTypes moduleIndex constructors selectors types
	=	foldStateWithIndexA (defineType moduleIndex constructors selectors) types
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
772

773
774
convertTypeLhs :: ModuleIndex Index TypeAttribute [ATypeVar] -> BEMonad BEFlatTypeP
convertTypeLhs moduleIndex typeIndex attribute args
775
	=	beFlatType (beTypeSymbol typeIndex moduleIndex) (convertAttribution attribute) (convertTypeVars args)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
776
777
778

convertTypeVars :: [ATypeVar] -> BEMonad BETypeVarListP
convertTypeVars typeVars
779
	=	sfoldr (beTypeVars o convertTypeVar) beNoTypeVars typeVars
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
780

781
convertTypeVar :: ATypeVar -> BEMonad BETypeVarListP
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
782
convertTypeVar typeVar
783
	=	beTypeVarListElem (beTypeVar typeVar.atv_variable.tv_ident.id_name) (convertAttribution typeVar.atv_attribute)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
784

785
defineType :: ModuleIndex {#ConsDef} {#SelectorDef} Index CheckedTypeDef *BackEndState -> *BackEndState
786
defineType moduleIndex constructors _ typeIndex {td_ident, td_attribute, td_args, td_rhs=AlgType constructorSymbols} be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
787
	# (flatType, be)
788
		=	convertTypeLhs moduleIndex typeIndex td_attribute td_args be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
789
	# (constructors, be)
790
		=	convertConstructors typeIndex td_ident.id_name moduleIndex constructors constructorSymbols be
791
	=	appBackEnd (BEAlgebraicType flatType constructors) be
792
defineType moduleIndex constructors selectors typeIndex {td_attribute, td_args, td_rhs=RecordType {rt_constructor, rt_fields, rt_is_boxed_record}} be
793
//	| trace_tn constructorDef.cons_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
794
	# (flatType, be)
795
		=	convertTypeLhs moduleIndex typeIndex td_attribute td_args be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
796
	# (fields, be)
797
		=	convertSelectors moduleIndex selectors rt_fields constructorDef.cons_type.st_args_strictness be
798
	# (constructorType,be) = constructorTypeFunction be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
799
800
801
802
803
	# (constructorTypeNode, be)
		=	beNormalTypeNode
				(beConstructorSymbol moduleIndex constructorIndex)
				(convertSymbolTypeArgs constructorType)
				be
804
	=	appBackEnd (BERecordType moduleIndex flatType constructorTypeNode (if rt_is_boxed_record 1 0) fields) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
805
806
807
808
809
	where
		constructorIndex
			=	rt_constructor.ds_index
		constructorDef
			=	constructors.[constructorIndex]
810
		constructorTypeFunction be0
811
			= let (cons_type,be) = read_from_var_heap constructorDef.cons_type_ptr be0 in
812
813
814
815
816
					(case cons_type of
						VI_ExpandedType expandedType
							->	(expandedType,be)
						_
							->	(constructorDef.cons_type,be))
817
818
819
820
defineType moduleIndex _ _ typeIndex {td_attribute, td_args, td_rhs=AbstractType _} be
 	=	beAbsType (convertTypeLhs moduleIndex typeIndex td_attribute td_args) be
defineType moduleIndex _ _ typeIndex {td_attribute, td_args, td_rhs=AbstractSynType _ _} be
 	=	beAbsType (convertTypeLhs moduleIndex typeIndex td_attribute td_args) be
821
defineType _ _ _ _ _ be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
822
823
	=	be

824
825
826
convertConstructors :: Int {#Char} ModuleIndex {#ConsDef} [DefinedSymbol] -> BEMonad BEConstructorListP
convertConstructors typeIndex typeName moduleIndex constructors symbols
	=	sfoldr (beConstructors o convertConstructor typeIndex typeName moduleIndex constructors) beNoConstructors symbols
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
827

828
829
convertConstructor :: Int {#Char} ModuleIndex {#ConsDef} DefinedSymbol -> BEMonad BEConstructorListP
convertConstructor typeIndex typeName moduleIndex constructorDefs {ds_index}
830
	= \be0 -> let (constructorType,be) = constructorTypeFunction be0 in
831
		(appBackEnd (BEDeclareConstructor ds_index moduleIndex constructorDef.cons_ident.id_name) // +++ remove declare
832
		o`	beConstructor
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
833
834
			(beNormalTypeNode
				(beConstructorSymbol moduleIndex ds_index)
835
				(convertSymbolTypeArgs constructorType))) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
836
837
838
	where
		constructorDef
			=	constructorDefs.[ds_index]
839
		constructorTypeFunction be0
840
			= let (cons_type,be) = read_from_var_heap constructorDef.cons_type_ptr be0 in
841
842
					(case cons_type of
						VI_ExpandedType expandedType
843
							->	(expandedType,be) // ->> (typeName, typeIndex, constructorDef.cons_ident.id_name, ds_index, expandedType)
844
						_
845
							->	(constructorDef.cons_type,be)) // ->> (typeName, typeIndex, constructorDef.cons_ident.id_name, ds_index, constructorDef.cons_type)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
846
847


848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
foldrAi function result array
	:== foldrA 0
	where
		arraySize
			=	size array
		foldrA index
			| index == arraySize
				=	result
			// otherwise
				=	function index array.[index] (foldrA (index+1))

//convertSelectors :: ModuleIndex {#SelectorDef} {#FieldSymbol} -> BEMonad BEFieldListP
convertSelectors :: ModuleIndex {#SelectorDef} {#FieldSymbol} StrictnessList -> BEMonad BEFieldListP
convertSelectors moduleIndex selectors symbols strictness
	=	foldrAi (\i -> (beFields o convertSelector moduleIndex selectors (arg_is_strict i strictness))) beNoFields symbols

convertSelector :: ModuleIndex {#SelectorDef} Bool FieldSymbol -> BEMonad BEFieldListP
convertSelector moduleIndex selectorDefs is_strict {fs_index}
866
	= \be0 -> let (selectorType,be) = selectorTypeFunction be0 in
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
867
		(	appBackEnd (BEDeclareField fs_index moduleIndex selectorDef.sd_ident.id_name)
868
		o`	beField fs_index moduleIndex (convertAnnotAndTypeNode (if is_strict AN_Strict AN_None) (selectorType.st_result))) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
869
870
871
	where
		selectorDef
			=	selectorDefs.[fs_index]
872
		selectorTypeFunction be0
873
			= let (sd_type,be) = read_from_var_heap selectorDef.sd_type_ptr be0 in
874
				(case sd_type of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
875
					VI_ExpandedType expandedType
876
						->	(expandedType,be)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
877
					_
878
						->	(selectorDef.sd_type,be))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
879

880
881
declareDynamicTemp :: PredefinedSymbols -> BackEnder
declareDynamicTemp predefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
882
	=	appBackEnd (BEDeclareDynamicTypeSymbol predefs.[PD_StdDynamic].pds_def predefs.[PD_Dyn_DynamicTemp].pds_def)
883

884
predefineSymbols :: DclModule PredefinedSymbols -> BackEnder
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
885
predefineSymbols {dcl_common} predefs
886
	=	appBackEnd (BEDeclarePredefinedModule (size dcl_common.com_type_defs) (size dcl_common.com_cons_defs))
887
	o`	foldState predefine_list_type list_types
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
888
	o`	foldState predefineType types
889
	o`	foldState predefine_list_constructor list_constructors
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
890
891
	o`	foldState predefineConstructor constructors
	where
892
893
894
895
896
897
898
899
900
901
		list_types :: [(Int,Int,Int)]
		list_types
			=	[
					(PD_ListType,0,0),
					(PD_StrictListType,2,0),
					(PD_UnboxedListType,3,0),
					(PD_TailStrictListType,0,1),
					(PD_StrictTailStrictListType,2,1),
					(PD_UnboxedTailStrictListType,3,1)
				]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
902

903
		predefine_list_type (index,head_strictness,tail_strictness)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
904
905
			// sanity check ...
			| predefs.[index].pds_def == NoIndex
906
				=	abort "backendconvert, predefineSymbols predef is not a type"
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
907
			// ... sanity check
908
			=	appBackEnd (BEPredefineListTypeSymbol predefs.[index].pds_def cPredefinedModuleIndex BEListType head_strictness tail_strictness) // id
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
909
910
911

		types :: [(Int, Int, BESymbKind)]
		types
912
913
			=	[	
					(PD_LazyArrayType, 1, BEArrayType)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
914
915
916
917
918
				,	(PD_StrictArrayType, 1, BEStrictArrayType)
				,	(PD_UnboxedArrayType, 1, BEUnboxedArrayType)
				:	[(index, index-PD_Arity2TupleType+2, BETupleType) \\ index <- [PD_Arity2TupleType..PD_Arity32TupleType]]
				]

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
		predefineType (index, arity, symbolKind)
			// sanity check ...
			| predefs.[index].pds_def == NoIndex
				=	abort "backendconvert, predefineSymbols predef is not a type"
			// ... sanity check
			=	appBackEnd (BEPredefineTypeSymbol arity predefs.[index].pds_def cPredefinedModuleIndex symbolKind)

		list_constructors :: [(Int,BESymbKind,Int,Int)]
		list_constructors
			=	[
					(PD_NilSymbol, BENilSymb,0,0),
					(PD_StrictNilSymbol, BENilSymb,2,0),
					(PD_UnboxedNilSymbol, BENilSymb,4/*3*/,0),
					(PD_TailStrictNilSymbol, BENilSymb,0,1),
					(PD_StrictTailStrictNilSymbol, BENilSymb,2,1),
					(PD_UnboxedTailStrictNilSymbol, BENilSymb,4/*3*/,1),
					(PD_OverloadedNilSymbol, BENilSymb,0,0),
					(PD_ConsSymbol, BEConsSymb,0,0),
					(PD_StrictConsSymbol, BEConsSymb,2,0),
					(PD_UnboxedConsSymbol, BEConsSymb,3,0),
					(PD_TailStrictConsSymbol, BEConsSymb,0,1),
					(PD_StrictTailStrictConsSymbol, BEConsSymb,2,1),
					(PD_UnboxedTailStrictConsSymbol, BEConsSymb,3,1),
					(PD_OverloadedConsSymbol, BEConsSymb,1,0)
				]

		predefine_list_constructor (index,symbolKind,head_strictness,tail_strictness)
			// sanity check ...
			| predefs.[index].pds_def == NoIndex
				=	abort "backendconvert, predefineSymbols predef is not a constructor"
			// ... sanity check
			= appBackEnd (BEPredefineListConstructorSymbol predefs.[index].pds_def cPredefinedModuleIndex symbolKind head_strictness tail_strictness) // id
		
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
952
953
		constructors :: [(Int, Int, BESymbKind)]
		constructors
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
954
			=	[(index, index-PD_Arity2TupleSymbol+2, BETupleSymb) \\ index <- [PD_Arity2TupleSymbol..PD_Arity32TupleSymbol]]
955
956
957
958
959
960
961
962
963

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

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

bindSpecialIdents :: PredefinedSymbols NumberSet -> BackEnder
bindSpecialIdents predefs usedModules
	=	foldState (bindSpecialModule predefs usedModules) specialModules
	where
		bindSpecialModule :: PredefinedSymbols NumberSet (Int, BESpecialIdentIndex, [(Int, BESpecialIdentIndex)]) -> BackEnder
		bindSpecialModule predefs usedModules (predefIndex, specialIdentIndex, specialFunctions)
			| moduleIndex == NoIndex || not (inNumberSet moduleIndex usedModules)
				=	identity
			// otherwise
				=	beBindSpecialModule specialIdentIndex moduleIndex
				o`	foldState (bindSpecialFunction predefs) specialFunctions
				where
					predef
						=	predefs.[predefIndex]
					moduleIndex
						=	predef.pds_def

		bindSpecialFunction :: PredefinedSymbols (Int, BESpecialIdentIndex) -> BackEnder
		bindSpecialFunction predefs (predefIndex, specialIdentIndex)
			| predef.pds_def == NoIndex
				=	identity
			// otherwise
				=	beBindSpecialFunction specialIdentIndex predef.pds_def predef.pds_module
				where
					predef
						=	predefs.[predefIndex]

		specialModules
			=	[	(PD_StdMisc, BESpecialIdentStdMisc,
						[	(PD_abort,	BESpecialIdentAbort)
						,	(PD_undef,	BESpecialIdentUndef)
						]
					)
				,	(PD_StdBool, BESpecialIdentStdBool,
						[	(PD_AndOp,	BESpecialIdentAnd)
						,	(PD_OrOp,	BESpecialIdentOr)
						]
					)
				]

1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
adjustStrictListFunctions :: [Int] [Int] {#PredefinedSymbol} {#DclModule} NumberSet Int *BackEndState -> *BackEndState;
adjustStrictListFunctions list_first_instance_indices tail_strict_list_first_instance_indices predefs dcls used_module_numbers main_dcl_module_n backEnd
	| std_strict_list_module_index==NoIndex || not (inNumberSet std_strict_list_module_index used_module_numbers)
		|| std_strict_list_module_index==main_dcl_module_n
		= backEnd
		# std_strict_lists_instances=std_strict_lists.dcl_common.com_instance_defs
		# backEnd = adjust_strict_list_instances 0 std_strict_lists_instances backEnd
		# std_strict_lists_nil_functions=std_strict_lists.dcl_functions
		# first_instance_index=std_strict_lists.dcl_instances.ir_from;
		# backEnd=adjust_overloaded_nil_functions 0 first_instance_index std_strict_lists_nil_functions backEnd
		# backEnd=adjustRecordListInstances list_first_instance_indices backEnd
		= adjustRecordListInstances tail_strict_list_first_instance_indices backEnd
where
	std_strict_lists=dcls.[std_strict_list_module_index]
	std_strict_list_module_index=predefs.[PD_StdStrictLists].pds_def

	adjust_strict_list_instances i instances backEnd
		| i<size instances
			# instance_i = instances.[i]
			| isEmpty instance_i.ins_type.it_context // && trace_t ("instance: "+++toString instance_i.ins_ident+++" ") && trace_t (types_to_string instance_i.ins_type.it_types+++" ")
				# backEnd = adjust_strict_list_members 0 instance_i.ins_members backEnd
				= adjust_strict_list_instances (i+1) instances backEnd
				= adjust_strict_list_instances (i+1) instances backEnd
			= backEnd
	where
		adjust_strict_list_members i members backEnd
			| i<size members
				# member=members