backendconvert.icl 86.7 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

15
import RWSDebug
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
16

clean's avatar
clean committed
17
18
19
20
// trace macro
(-*->) infixl
(-*->) value trace
	:==	value // ---> trace
21
22
23
24
25
26
27
28
29
30
31
32
/*
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
33

34
35
36
37
38
// fix spelling, this will be removed when cases are implemented in the back end
:: BackEndBody :== BackendBody
BackEndBody x :== BackendBody x


39
:: BEMonad a :== St !*BackEndState !a
clean's avatar
clean committed
40

41
:: BackEnder :== *BackEndState -> *BackEndState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
42

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

46
47
48
49
appBackEnd f beState
	:== {beState & bes_backEnd = bes_backEnd}
	where
		bes_backEnd = f beState.bes_backEnd
50

51
52
53
54
55
56
57
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
58

59
60
accVarHeap f beState
	:== (result, {beState & bes_varHeap = varHeap})
61
	where
62
		(result, varHeap) =	f beState.bes_varHeap
63

64
65
66
67
68
69
70
accAttrHeap f beState
	:== (result, {beState & bes_attrHeap = attrHeap})
	where
		(result, attrHeap) =	f beState.bes_attrHeap


read_from_var_heap :: VarInfoPtr BackEndState -> (VarInfo, BackEndState)
71
read_from_var_heap ptr beState
72
73
74
	= (result, {beState & bes_varHeap = varHeap})
where
		(result, varHeap) =	readPtr ptr beState.bes_varHeap
75

76
77
write_to_var_heap ptr v beState
	= {beState & bes_varHeap = writePtr ptr v beState.bes_varHeap}
78
79
80
81
82
83
84
85

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}
86
87
88
/*
read_from_var_heap ptr heap be
	= (sreadPtr ptr heap,be)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
89

90
91
92
93
::	*BackEndState :== BackEnd

appBackEnd f beState :== f beState
accBackEnd f beState :== f beState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
94
accVarHeap f beState :== f beState
95
*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
96

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

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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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)
204
205
206
207

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

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
208
209
210
211
beFieldSymbol fieldIndex moduleIndex
	:==	beFunction0 (BEFieldSymbol fieldIndex moduleIndex)
beTypeSymbol typeIndex moduleIndex
	:==	beFunction0 (BETypeSymbol typeIndex moduleIndex)
212
213
beBasicSymbol symbolIndex
	:==	beFunction0 (BEBasicSymbol symbolIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
236
237
beAddForAllTypeVariables
	:==	beFunction2 BEAddForAllTypeVariables
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
238
239
240
241
242
243
244
245
246
beVarTypeNode name
	:==	beFunction0 (BEVarTypeNode name)
beRuleAlt lineNumber
	:==	beFunction5 (BERuleAlt lineNumber)
beNoRuleAlts
	:==	beFunction0 BENoRuleAlts
beRuleAlts
	:==	beFunction2 BERuleAlts
beTypeAlt
247
	:==	beFunction3 BETypeAlt
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
281
282
283
284
285
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)
286
287
beAttributeTypeNode
	:==	beFunction2 BEAttributeTypeNode
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
288
beDeclareRuleType functionIndex moduleIndex name
289
	:==	beApFunction0 (BEDeclareRuleType functionIndex moduleIndex name)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
290
beDefineRuleType functionIndex moduleIndex
291
	:==	beApFunction1 (BEDefineRuleType functionIndex moduleIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
311
312
313
	:==	beApFunction0 (BEDeclareNodeId number lhsOrRhs name)
beAdjustArrayFunction backendId functionIndex moduleIndex
	:==	beApFunction0 (BEAdjustArrayFunction backendId functionIndex moduleIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
314
315
316
317
318
319
320
321
beFlatType
	:==	beFunction2 BEFlatType
beNoTypeVars
	:==	beFunction0 BENoTypeVars
beTypeVars
	:==	beFunction2 BETypeVars
beTypeVar name
	:==	beFunction0 (BETypeVar name)
322
323
beTypeVarListElem
	:==	beFunction2 BETypeVarListElem
clean's avatar
clean committed
324
beExportType dclTypeIndex iclTypeIndex
325
	:==	beApFunction0 (BEExportType dclTypeIndex iclTypeIndex)
clean's avatar
clean committed
326
beExportConstructor dclConstructorIndex iclConstructorIndex
327
	:==	beApFunction0 (BEExportConstructor dclConstructorIndex iclConstructorIndex)
clean's avatar
clean committed
328
beExportField dclFieldIndex iclFieldIndex
329
	:==	beApFunction0 (BEExportField dclFieldIndex iclFieldIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
330
beExportFunction dclIndexFunctionIndex iclFunctionIndex
331
	:==	beApFunction0 (BEExportFunction dclIndexFunctionIndex iclFunctionIndex)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
332
333
334
335
336
beTupleSelectNode arity index
	:==	beFunction1 (BETupleSelectNode arity index)
beMatchNode arity
	:==	beFunction2 (BEMatchNode arity)
beDefineImportedObjsAndLibs
337
	:== beApFunction2 BEDefineImportedObjsAndLibs
clean's avatar
clean committed
338
beAbsType
339
	:== beApFunction1 BEAbsType
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
354
355
356
357
358
359
360
361
362
363
364
365
beAttributeKind
	:== beFunction1 BEAttributeKind
beNoAttributeKinds
	:== beFunction0 BENoAttributeKinds
beAttributeKinds
	:== beFunction2 BEAttributeKinds
beUniVarEquation
	:== beFunction2 BEUniVarEquation
beNoUniVarEquations
	:== beFunction0 BENoUniVarEquations
beUniVarEquationsList
	:== beFunction2 BEUniVarEquationsList
366
367
368
369
beBindSpecialModule specialIdentIndex moduleIndex
	:== beApFunction0 (BEBindSpecialModule specialIdentIndex moduleIndex)
beBindSpecialFunction specialIdentIndex functionIndex moduleIndex
	:== beApFunction0 (BEBindSpecialFunction specialIdentIndex functionIndex moduleIndex)
370

371
372
373
// temporary hack
beDynamicTempTypeSymbol
	:== beFunction0 BEDynamicTempTypeSymbol
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
374
375
376
377
378

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

379
backEndConvertModules :: PredefinedSymbols FrontEndSyntaxTree !Int *VarHeap *AttrVarHeap *BackEnd -> (!*VarHeap, *AttrVarHeap, !*BackEnd)
380
381
382
383
/*
backEndConvertModules p s main_dcl_module_n v be
	= (newHeap,backEndConvertModulesH p s v be)
*/
384
385
386
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)
387

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

	#  currentDcl
412
	   	=	fe_dcls.[main_dcl_module_n]
clean's avatar
clean committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
	   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)
				)
*/
438
439
440
441
	#! 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")
442
443
444
445
446

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

447
	#! backEnd
448
		=	defineDclModule main_dcl_module_n fe_dcls.[main_dcl_module_n] (backEnd -*-> "defineDclModule(cIclMoIndex)")
449
	#! backEnd
clean's avatar
clean committed
450
		=	reshuffleTypes (size icl_common.com_type_defs) typeConversions (backEnd -*-> "reshuffleTypes")
451
	#! backEnd
452
		=	defineOtherDclModules fe_dcls main_dcl_module_n icl_used_module_numbers (backEnd -*-> "defineOtherDclModules")
clean's avatar
clean committed
453

454
	#! backEnd
455
		=	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")
456
	#! backEnd
457
		=	declareFunctionSymbols icl_functions (getConversions fe_iclDclConversions) functionIndices fe_globalFunctions (backEnd -*-> "declareFunctionSymbols")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
458
459
460
461
462
463
		with
			getConversions :: (Optional {#Int}) -> {#Int}
			getConversions No
				=	{}
			getConversions (Yes conversions)
				=	conversions
464
	#! backEnd
465
		=	declare main_dcl_module_n icl_common (backEnd -*-> "declare (main_dcl_module_n)")
466
	#! backEnd
467
468
469
470
471
472
473
		=	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")
474
	#! backEnd
475
		=	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
476
	#! (rules, backEnd)
477
		=	convertRules [(index, icl_functions.[index]) \\ (_, index) <- functionIndices] main_dcl_module_n predefs.[PD_DummyForStrictAliasFun].pds_ident (backEnd -*-> "convertRules")
478
479
	#! backEnd
		=	appBackEnd (BEDefineRules rules) (backEnd -*-> "BEDefineRules")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
480
481
482
483
	#! 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
484
				(backEnd -*-> "beDefineImportedObjsAndLibs")
485
486
	#! backEnd
		=	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 fe_dclIclConversions (backEnd -*-> "markExports")
clean's avatar
clean committed
487
488
489
			with
				dcl_common
					=	currentDcl.dcl_common
490
	# backEnd = bindSpecialIdents predefs icl_used_module_numbers backEnd
491
492
	#! backEnd = removeExpandedTypesFromDclModules fe_dcls icl_used_module_numbers backEnd
	=	(backEnd -*-> "backend done")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
493
494
	where
		functionIndices
495
			=	flatten [[(componentIndex, member) \\ member <- group.group_members] \\ group <-: fe_components & componentIndex <- [1..]]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
496

497
declareOtherDclModules :: {#DclModule} Int NumberSet -> BackEnder
498
declareOtherDclModules dcls main_dcl_module_n used_module_numbers
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
499
	=	foldStateWithIndexA declareOtherDclModule dcls
500
501
502
where
	declareOtherDclModule :: ModuleIndex DclModule -> BackEnder
	declareOtherDclModule moduleIndex dclModule
503
		| moduleIndex == main_dcl_module_n || moduleIndex == cPredefinedModuleIndex || not (inNumberSet moduleIndex used_module_numbers)
504
505
506
507
			=	identity
		// otherwise
			=	declareDclModule moduleIndex dclModule

508
509
510
defineOtherDclModules :: {#DclModule} Int NumberSet -> BackEnder
defineOtherDclModules dcls main_dcl_module_n used_module_numbers
	=	foldStateWithIndexA defineOtherDclModule dcls
511
where
512
513
	defineOtherDclModule :: ModuleIndex DclModule -> BackEnder
	defineOtherDclModule moduleIndex dclModule
514
		| moduleIndex == main_dcl_module_n || moduleIndex == cPredefinedModuleIndex || not (inNumberSet moduleIndex used_module_numbers)
515
516
			=	identity
		// otherwise
517
			=	defineDclModule moduleIndex dclModule
518

519
520
521
522
523
524
525
526
isSystem :: ModuleKind -> Bool
isSystem MK_System
	=	True
isSystem MK_Module
	=	False
isSystem _
	=	abort "backendconvert:isSystem, unknown module kind"

527
declareCurrentDclModule :: IclModule DclModule Int -> BackEnder
528
529
declareCurrentDclModule _ {dcl_module_kind=MK_None} _
	=	identity
530
531
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))
532

533
declareDclModule :: ModuleIndex DclModule -> BackEnder
534
535
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))
536
/*
537
538
defineCurrentDclModule :: IclModule DclModule {#Int} -> BackEnder
defineCurrentDclModule {icl_common} {dcl_name, dcl_common, dcl_functions, dcl_is_system, dcl_conversions} typeConversions
539
540
	=	declareCurrentDclModuleTypes icl_common.com_type_defs typeConversions
	o`	defineCurrentDclModuleTypes dcl_common.com_cons_defs dcl_common.com_selector_defs dcl_common.com_type_defs typeConversions
541
*/
542
543
544
545
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
546

547
removeExpandedTypesFromDclModules :: {#DclModule} NumberSet -> BackEnder
548
549
550
551
552
removeExpandedTypesFromDclModules dcls used_module_numbers
	=	foldStateWithIndexA removeExpandedTypesFromDclModule dcls
where
	removeExpandedTypesFromDclModule :: ModuleIndex DclModule -> BackEnder
	removeExpandedTypesFromDclModule moduleIndex dclModule=:{dcl_functions}
553
		| moduleIndex == cPredefinedModuleIndex || not (inNumberSet moduleIndex used_module_numbers)
554
555
556
557
558
			= identity
			= foldStateWithIndexA (removeExpandedTypesFromFunType moduleIndex)  dcl_functions
			where
				removeExpandedTypesFromFunType :: ModuleIndex Index FunType -> BackEnder
				removeExpandedTypesFromFunType moduleIndex functionIndex {ft_symb, ft_type_ptr}
559
					= \be0 ->	let (ft_type,be) = read_from_var_heap ft_type_ptr be0 in
560
561
562
563
564
						(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
565

clean's avatar
clean committed
566
567
568
569
// move types from their dcl to icl positions

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

570
571
instance swapTypes BackEndState where
//instance swapTypes BackEnd where
clean's avatar
clean committed
572
	swapTypes i j be
573
		=	appBackEnd (BESwapTypes i j) be
clean's avatar
clean committed
574
575
576
577
578
579
580
581
582
583
584
585

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
586
	=	thd3 (foldStateWithIndexA (swapType nDclTypes) dclIclConversions (idP nDclTypes, idP nIclTypes, be))
clean's avatar
clean committed
587
	where
clean's avatar
clean committed
588
589
590
		nDclTypes
			=	size dclIclConversions

clean's avatar
clean committed
591
592
593
594
		idP :: Int -> .{#Int}
		idP n
			=	{i \\ i <- [0 .. n-1]}

clean's avatar
clean committed
595
596
		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
597
598
599
600
601
602
603
604
605
			#! frm
				=	p.[dclIndex]
			#! to
				=	iclIndex
			| frm == to
				=	state
			// otherwise
				#! frm` = dclIndex
				#! to` = p`.[iclIndex]
clean's avatar
clean committed
606
				#! to` = if (to` >= nDclTypes) frm` to`
clean's avatar
clean committed
607
608
				=	(swap frm` to` p, swap frm to p`, swapTypes frm to be)

609
:: DeclVarsInput :== Ident
610

611
class declareVars a :: a !DeclVarsInput -> BackEnder
612
613

instance declareVars [a] | declareVars a where
614
	declareVars :: [a] !DeclVarsInput -> BackEnder | declareVars a
615
616
617
618
	declareVars list dvInput
		=	foldState (flip declareVars dvInput) list

instance declareVars (Ptr VarInfo) where
619
	declareVars varInfoPtr _
620
		=	declareVariable BELhsNodeId varInfoPtr "_var???"	// +++ name
621
622

instance declareVars FreeVar where
623
	declareVars :: FreeVar !DeclVarsInput -> BackEnder
624
625
	declareVars freeVar _
		=	declareVariable BELhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name
626

627
628
instance declareVars LetBind where
	declareVars :: LetBind !DeclVarsInput -> BackEnder
629
	declareVars {lb_src=App {app_symb, app_args=[Var _:_]}, lb_dst=freeVar} aliasDummyId
John van Groningen's avatar
John van Groningen committed
630
		| not (isNilPtr app_symb.symb_name.id_info) && app_symb.symb_name==aliasDummyId
631
			= identity		// we have an alias. Don't declare the same variable twice
632
633
634
		= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name
	declareVars {lb_dst=freeVar} _
		= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name
635

636
637
638
declareVariable :: Int (Ptr VarInfo) {#Char} -> BackEnder
declareVariable lhsOrRhs varInfoPtr name
	= \be0 -> let (variable_sequence_number,be) = getVariableSequenceNumber varInfoPtr be0 in
639
		beDeclareNodeId variable_sequence_number lhsOrRhs name be
640
641

instance declareVars (Optional a) | declareVars a where
642
	declareVars :: (Optional a) !DeclVarsInput -> BackEnder | declareVars a
643
644
645
646
647
648
	declareVars (Yes x) dvInput
		=	declareVars x dvInput
	declareVars No _
		=	identity

instance declareVars FunctionPattern where
649
	declareVars :: FunctionPattern !DeclVarsInput -> BackEnder
650
651
652
653
654
655
656
657
658
659
660
	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
661
	declareVars :: Expression !DeclVarsInput -> BackEnder
662
663
664
665
	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
666
667
668
	declareVars (Conditional {if_cond, if_then, if_else}) dvInput
		=	declareVars if_cond dvInput
		o`	declareVars if_then dvInput
669
		o`	declareVars if_else dvInput
670
671
	declareVars (Case caseExpr) dvInput
		=	declareVars caseExpr dvInput
672
673
	declareVars (AnyCodeExpr _ outParams _) _
		=	foldState declVar outParams 
674
	  where
675
676
		declVar {bind_dst=freeVar} 
			= declareVariable BERhsNodeId freeVar.fv_info_ptr freeVar.fv_name.id_name
677
678
679
680
	declareVars _ _
		=	identity

instance declareVars TransformedBody where
681
	declareVars :: TransformedBody !DeclVarsInput -> BackEnder
682
683
684
685
686
	declareVars {tb_args, tb_rhs} dvInput
		=	declareVars tb_args dvInput
		o`	declareVars tb_rhs dvInput

instance declareVars BackendBody where
687
	declareVars :: BackendBody !DeclVarsInput -> BackEnder
688
689
690
691
	declareVars {bb_args, bb_rhs} dvInput
		=	declareVars bb_args dvInput
		o`	declareVars bb_rhs dvInput

692
693
694
695
696
697
698
699
700
701
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
702
703
	declareVars (OverloadedListPatterns _ decons_expr patterns) dvInput
		=	declareVars patterns dvInput
704
705
706
707
708
709
710
711
712
713

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

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
714
715
:: ModuleIndex :== Index

716
class declare a :: ModuleIndex a  -> BackEnder
717

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

720
//1.3
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
721
instance declare {#a} | declareWithIndex a & ArrayElem a where
722
	declare :: ModuleIndex  {#a} -> BackEnder | declareWithIndex a & ArrayElem a 
723
724
725
//3.1
/*2.0
instance declare {#a} | declareWithIndex a & Array {#} a where
726
	declare :: ModuleIndex  {#a} -> BackEnder | declareWithIndex a & Array {#} a 
727
0.2*/
728
729
	declare moduleIndex array
		=	foldStateWithIndexA (\i -> declareWithIndex i moduleIndex) array
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
730

731
declareFunctionSymbols :: {#FunDef} {#Int} [(Int, Int)] IndexRange *BackEndState -> *BackEndState
732
declareFunctionSymbols functions iclDclConversions functionIndices globalFunctions backEnd
733
	=	foldl (declare iclDclConversions) backEnd [(functionIndex, componentIndex, functions.[functionIndex]) \\ (componentIndex, functionIndex) <- functionIndices]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
734
	where
735
		declare iclDclConversions backEnd (functionIndex, componentIndex, function)
736
737
			=	appBackEnd (BEDeclareFunction (functionName function.fun_symb.id_name functionIndex iclDclConversions globalFunctions) 
					function.fun_arity functionIndex componentIndex) backEnd
738
739
740
			where
				functionName :: {#Char} Int {#Int} IndexRange -> {#Char}
				functionName name functionIndex iclDclConversions {ir_from, ir_to}
clean's avatar
clean committed
741
//				| trace_t ("|"+++toString functionIndex)
742
743
744
745
					| functionIndex >= ir_to || functionIndex < ir_from
						=	(name +++ ";" +++ toString iclDclConversions.[functionIndex])
					// otherwise
						=	name
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
746
747
748
749
750
751
752
753
754
755
756
757

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

758
759
760
761
762
763
764
765
766
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
767
//	| trace_tn ("declareArrayInstances "+++toString ir_from+++" "+++toString ir_to)
768
769
//	=	foldStateWithIndexRangeA declareArrayInstance ir_from ir_to functions
	= folds (declareArrayInstances 0) array_first_instance_indices
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
770
	where
771
772
773
774
775
776
777
778
779
780
781
782
783
784
		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

785
		declareArrayInstance :: Index FunDef -> BackEnder
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
786
		declareArrayInstance index {fun_symb={id_name}, fun_type=Yes type}
787
788
			=	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
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
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
		declareListInstance index {fun_symb={id_name}, fun_type=Yes type}
//			| 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
816
instance declare CommonDefs where
817
818
819
820
	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
821

clean's avatar
clean committed
822
instance declareWithIndex (TypeDef a) where
823
824
	declareWithIndex :: Index ModuleIndex (TypeDef a) -> BackEnder
	declareWithIndex typeIndex moduleIndex {td_name}
825
		=	appBackEnd (BEDeclareType typeIndex moduleIndex td_name.id_name)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
826

827
828
829
declareFunTypes :: ModuleIndex {#FunType} Int -> BackEnder
declareFunTypes moduleIndex funTypes nrOfDclFunctions
		=	foldStateWithIndexA (declareFunType moduleIndex nrOfDclFunctions) funTypes
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
830

831
832
833
declareFunType :: ModuleIndex Index Int FunType -> BackEnder
declareFunType moduleIndex nrOfDclFunctions functionIndex {ft_symb, ft_type_ptr}
	= \be0 -> let (vi,be) = read_from_var_heap ft_type_ptr be0 in
834
835
836
					(case vi of
						VI_ExpandedType expandedType
							->	beDeclareRuleType functionIndex moduleIndex (functionName ft_symb.id_name functionIndex nrOfDclFunctions)
clean's avatar
clean committed
837
//							->	beDeclareRuleType functionIndex moduleIndex (functionName moduleIndex ft_symb.id_name functionIndex nrOfDclFunctions)
838
839
840
							o`	beDefineRuleType functionIndex moduleIndex (convertTypeAlt functionIndex moduleIndex expandedType)
						_
							->	identity) be
841
		where
842
843
844
845
//			functionName :: Int {#Char} Int Int -> {#Char}
//			functionName moduleIndex name functionIndex nrOfDclFunctions 
//				| trace_t (":"+++toString moduleIndex+++" "+++toString functionIndex)

846
847
848
849
850
851
			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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895

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
	=	{}

896
/*
897
declareCurrentDclModuleTypes :: {#CheckedTypeDef} {#Int} -> BackEnder
898
*/
899
900
901
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
902
903
904
905
906
907
908

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

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

911
convertTypeVar :: ATypeVar -> BEMonad BETypeVarListP
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
912
convertTypeVar typeVar
913
	=	beTypeVarListElem (beTypeVar typeVar.atv_variable.tv_name.id_name) (convertAttribution typeVar.atv_attribute)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
914

915
916
defineType :: ModuleIndex {#ConsDef} {#SelectorDef} Index CheckedTypeDef *BackEndState -> *BackEndState
defineType moduleIndex constructors _ typeIndex {td_name, td_args, td_rhs=AlgType constructorSymbols} be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
917
918
919
	# (flatType, be)
		=	convertTypeLhs moduleIndex typeIndex td_args be
	# (constructors, be)
920
		=	convertConstructors typeIndex td_name.id_name moduleIndex constructors constructorSymbols be
921
	=	appBackEnd (BEAlgebraicType flatType constructors) be
922
defineType moduleIndex constructors selectors typeIndex {td_args, td_rhs=RecordType {rt_constructor, rt_fields}} be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
923
924
925
	# (flatType, be)
		=	convertTypeLhs moduleIndex typeIndex td_args be
	# (fields, be)
926
		=	convertSelectors moduleIndex selectors rt_fields be
927
	# (constructorType,be) = constructorTypeFunction be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
928
929
930
931
932
	# (constructorTypeNode, be)
		=	beNormalTypeNode
				(beConstructorSymbol moduleIndex constructorIndex)
				(convertSymbolTypeArgs constructorType)
				be
933
	=	appBackEnd (BERecordType moduleIndex flatType constructorTypeNode fields) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
934
935
936
937
938
	where
		constructorIndex
			=	rt_constructor.ds_index
		constructorDef
			=	constructors.[constructorIndex]
939
		constructorTypeFunction be0
940
			= let (cons_type,be) = read_from_var_heap constructorDef.cons_type_ptr be0 in
941
942
943
944
945
					(case cons_type of
						VI_ExpandedType expandedType
							->	(expandedType,be)
						_
							->	(constructorDef.cons_type,be))
946
defineType moduleIndex _ _ typeIndex {td_args, td_rhs=AbstractType _} be
clean's avatar
clean committed
947
 	=	beAbsType (convertTypeLhs moduleIndex typeIndex td_args) be
948
defineType _ _ _ _ _ be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
949
950
	=	be

951
952
953
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
954

955
956
convertConstructor :: Int {#Char} ModuleIndex {#ConsDef} DefinedSymbol -> BEMonad BEConstructorListP
convertConstructor typeIndex typeName moduleIndex constructorDefs {ds_index}
957
958
959
	= \be0 -> let (constructorType,be) = constructorTypeFunction be0 in
		(appBackEnd (BEDeclareConstructor ds_index moduleIndex constructorDef.cons_symb.id_name) // +++ remove declare
		o`	beConstructor
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
960
961
			(beNormalTypeNode
				(beConstructorSymbol moduleIndex ds_index)
962
				(convertSymbolTypeArgs constructorType))) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
963
964
965
	where
		constructorDef
			=	constructorDefs.[ds_index]
966
		constructorTypeFunction be0
967
			= let (cons_type,be) = read_from_var_heap constructorDef.cons_type_ptr be0 in
968
969
970
971
972
					(case cons_type of
						VI_ExpandedType expandedType
							->	(expandedType,be) // ->> (typeName, typeIndex, constructorDef.cons_symb.id_name, ds_index, expandedType)
						_
							->	(constructorDef.cons_type,be)) // ->> (typeName, typeIndex, constructorDef.cons_symb.id_name, ds_index, constructorDef.cons_type)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
973

974
975
976
convertSelectors :: ModuleIndex {#SelectorDef} {#FieldSymbol} -> BEMonad BEFieldListP
convertSelectors moduleIndex selectors symbols
	=	foldrA (beFields o convertSelector moduleIndex selectors) beNoFields symbols
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
977

978
979
convertSelector :: ModuleIndex {#SelectorDef} FieldSymbol -> BEMonad BEFieldListP
convertSelector moduleIndex selectorDefs {fs_index}
980
981
982
	= \be0 -> let (selectorType,be) = selectorTypeFunction be0 in
		(	appBackEnd (BEDeclareField fs_index moduleIndex selectorDef.sd_symb.id_name)
		o`	beField fs_index moduleIndex (convertAnnotTypeNode (selectorType.st_result))) be
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
983
984
985
	where
		selectorDef
			=	selectorDefs.[fs_index]
986
		selectorTypeFunction be0
987
			= let (sd_type,be) = read_from_var_heap selectorDef.sd_type_ptr be0 in
988
				(case sd_type of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
989
					VI_ExpandedType expandedType
990
						->	(expandedType,be)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
991
					_
992
						->	(selectorDef.sd_type,be))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
993

994
995
declareDynamicTemp :: PredefinedSymbols -> BackEnder
declareDynamicTemp predefs
996
	=	appBackEnd (BEDeclareDynamicTypeSymbol predefs.[PD_StdDynamic].pds_def predefs.[PD_DynamicTemp].pds_def)
997

998
predefineSymbols :: DclModule PredefinedSymbols -> BackEnder
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
999
predefineSymbols {dcl_common} predefs
1000
	=	appBackEnd (BEDeclarePredefinedModule (size dcl_common.com_type_defs) (size dcl_common.com_cons_defs))
1001
	o`	foldState predefine_list_type list_types
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1002
	o`	foldState predefineType types
1003
	o`	foldState predefine_list_constructor list_constructors
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1004
1005
	o`	foldState predefineConstructor constructors
	where
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
		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
1016

1017
		predefine_list_type (index,head_strictness,tail_strictness)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1018
1019
			// sanity check ...
			| predefs.[index].pds_def == NoIndex
1020
				=	abort "backendconvert, predefineSymbols predef is not a type"
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1021
			// ... sanity check
1022
			=	appBackEnd (BEPredefineListTypeSymbol predefs.[index].pds_def cPredefinedModuleIndex BEListType head_strictness tail_strictness) // id
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1023
1024
1025

		types :: [(Int, Int, BESymbKind)]
		types
1026
1027
1028
1029
			=	[	
//					(PD_ListType, 1, BEListType),
					
					(PD_LazyArrayType, 1, BEArrayType)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1030
1031
1032
1033
1034
				,	(PD_StrictArrayType, 1, BEStrictArrayType)
				,	(PD_UnboxedArrayType, 1, BEUnboxedArrayType)
				:	[(index, index-PD_Arity2TupleType+2, BETupleType) \\ index <- [PD_Arity2TupleType..PD_Arity32TupleType]]
				]

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
		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
1068
1069
		constructors :: [(Int, Int, BESymbKind)]
		constructors
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
			=	
//				[(PD_NilSymbol, 0, BENilSymb), (PD_ConsSymbol, 3, BEConsSymb) : 

				[(index, index-PD_Arity2TupleSymbol+2, BETupleSymb) \\ index <- [PD_Arity2TupleSymbol..PD_Arity32TupleSymbol]]

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