backend.c 90.1 KB
Newer Older
clean's avatar
clean committed
1
2
3
4
#define CODE_INLINE_FLAG
#define DYNAMIC_TYPE 1

# include "compiledefines.h"
5
6
# include "types.t"
# include "system.h"
clean's avatar
clean committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# include "syntaxtr.t"
# include "codegen_types.h"
# include "statesgen.h"
# include "codegen.h"
# include "codegen2.h"
# include "instructions.h"
# include "sizes.h"
# include "set_scope_numbers.h"

# include "scanner.h"		/* InitScanner, ScanInitIdentStringTable */
# include "checker.h"		/* scc_dependency_list, ClearOpenDefinitionModules, AddOpenDefinitionModule */
# include "comsupport.h" 	/* CurrentModule */
# include "buildtree.h"		/* TupleSymbol, ApplySymbol */

# include "backendsupport.h"
# define Clean(ignore)

# include "backend.h"

# include <limits.h>

28
# if 0
29
30
31
# include "dbprint.h"
# endif

clean's avatar
clean committed
32
33
34
35
36
37
38
39
void
BEGetVersion (int *current, int *oldestDefinition, int *oldestImplementation)
{
	*current				= kBEVersionCurrent;
	*oldestDefinition		= kBEVersionOldestDefinition;
	*oldestImplementation	= kBEVersionOldestImplementation;
}

40
41
42
#if STRICT_LISTS
 PolyList unboxed_record_cons_list,unboxed_record_decons_list;
#endif
clean's avatar
clean committed
43
44

extern PolyList UserDefinedArrayFunctions;	/* typechecker.c */
45
extern int StdOutReopened, StdErrorReopened; /* cocl.c */
clean's avatar
clean committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

static char *
ConvertCleanString (CleanString string)
{
	int		length;
	char	*copy;

	length	= string->length;
	copy	= ConvertAlloc (length+1);
	strncpy (copy, string->chars, length);
	copy [length]	= '\0';

	return (copy);
} /* ConvertCleanString */

static short
CountTypeArgs (BETypeArgP args)
{
	short	n;

	n	= 0;
	for (; args != NULL; args = args->type_arg_next)
		n++;

	return (n);
} /* CountTypeArgs */

static short
CountArgs (BEArgP args)
{
	short	n;

	n	= 0;
	for (; args != NULL; args = args->arg_next)
		n++;

	return (n);
} /* CountArgs */

/*
	BE routines
*/
STRUCT (be_module, BEModule)
{
	char			*bem_name;
	Bool			bem_isSystemModule;

	unsigned int	bem_nFunctions;
	SymbolP			bem_functions;
	unsigned int	bem_nTypes;
96
97
98
	SymbolP			bem_types;
	unsigned int	bem_nConstructors;
	SymbolP			bem_constructors;
clean's avatar
clean committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	unsigned int	bem_nFields;
	SymbolP			bem_fields;
};

STRUCT (be_icl_module, BEIcl)
{
	ImpMod			beicl_module;
	BEModuleS		beicl_dcl_module;

	// +++ remove this (build deps list separately)
	SymbDefP		*beicl_depsP;
};

STRUCT (be_state, BEState)
{
	Bool			be_initialised;

	char			**be_argv;
	int				be_argc;
	int				be_argi;

	BEModuleP		be_modules;

	BEIclS			be_icl;
	unsigned int	be_nModules;

125
	SymbolP			be_function_symbols;
clean's avatar
clean committed
126
127
128
	SymbolP			be_dontCareSymbol;
	SymbolP			be_dictionarySelectFunSymbol;
	SymbolP			be_dictionaryUpdateFunSymbol;
129
130
131
132

	// temporary hack
	int				be_dynamicTypeIndex;
	int				be_dynamicModuleIndex;
clean's avatar
clean committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
};

static BEStateS	gBEState = {False /* ... */};

STRUCT (be_locally_generated_function_info, BELocallyGeneratedFunction)
{
	char	*lgf_name;
	int		lgf_arity;
};

static BELocallyGeneratedFunctionS gLocallyGeneratedFunctions[] = {{"_dictionary_select", 3}, {"_dictionary_update", 4}};
# define	kDictionarySelect	0
# define	kDictionaryUpdate	1

// +++ put in gBEState
148
149
static NodeIdP (*gCurrentNodeIds)=NULL;
static int n_gCurrentNodeIds = 0;
clean's avatar
clean committed
150
151
152
153
154
static SymbolP	gBasicSymbols [Nr_Of_Predef_FunsOrConses];
static SymbolP	gTupleSelectSymbols [MaxNodeArity];

static int number_of_node_ids=0;

155
156
157
typedef IdentP *IdentH;
static IdentH gSpecialIdents[BESpecialIdentCount];

clean's avatar
clean committed
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
static IdentP
Identifier (char *name)
{
	IdentP	ident;

	ident	= ConvertAllocType (IdentS);
	ident->ident_name	= name;

	return (ident);
} /* Identifier */

static SymbolP
PredefinedSymbol (SymbKind symbolKind, int arity)
{
	SymbolP	symbol;

	symbol	= ConvertAllocType (SymbolS);

	symbol->symb_kind	= symbolKind;
	symbol->symb_arity	= arity;

	return (symbol);
} /* PredefinedSymbol */

static SymbolP
183
AllocateSymbols (int nFunctions, int nTypesConstructorsAndFields, SymbolP *function_symbols_h)
clean's avatar
clean committed
184
{
185
	int	nSymbols;
clean's avatar
clean committed
186
	
187
	nSymbols = nFunctions+nTypesConstructorsAndFields;
188
	if (nSymbols!=0){
189
		SymbolP	symbols;
190
		int i;
191
192

		symbols	= (SymbolP) ConvertAlloc (nSymbols * sizeof (SymbolS));
193
		i = 0;
194

195
196
197
198
199
200
201
202
203
		if (nFunctions>0){
			for (; i < nFunctions; ++i){
				symbols [i].symb_kind	= erroneous_symb;
				symbols [i].symb_next	= &symbols [i+1];
			}
		
			symbols [nFunctions-1].symb_next = *function_symbols_h;
			*function_symbols_h = symbols;
		}
204
		
205
		for (; i < nSymbols; i++){
206
207
208
			symbols [i].symb_kind	= erroneous_symb;
			symbols [i].symb_next	= NULL;
		}
209
210
211
212
		
		return symbols;
	} else
		return NULL;
clean's avatar
clean committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
} /* AllocateSymbols */

static void
InitPredefinedSymbols (void)
{
	int	i;

	gBasicSymbols [int_type]		= PredefinedSymbol (int_type, 0);
	gBasicSymbols [bool_type]		= PredefinedSymbol (bool_type, 0);
	gBasicSymbols [char_type]		= PredefinedSymbol (char_type, 0);
	gBasicSymbols [real_type]		= PredefinedSymbol (real_type, 0);
	gBasicSymbols [file_type]		= PredefinedSymbol (file_type, 0);
	gBasicSymbols [world_type]		= PredefinedSymbol (world_type, 0);
#if DYNAMIC_TYPE
	gBasicSymbols [dynamic_type]= PredefinedSymbol (dynamic_type, 0);
#endif
	gBasicSymbols [array_type]			= PredefinedSymbol (array_type, 1);
	gBasicSymbols [strict_array_type]	= PredefinedSymbol (strict_array_type, 1);
	gBasicSymbols [unboxed_array_type]	= PredefinedSymbol (unboxed_array_type, 1);

	gBasicSymbols [fun_type]	= PredefinedSymbol (fun_type, 2);

	ApplySymbol	= PredefinedSymbol (apply_symb, 2);
236
	ApplySymbol->symb_instance_apply = 0;
clean's avatar
clean committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
	gBasicSymbols [apply_symb]	= ApplySymbol;

	TupleSymbol	= PredefinedSymbol (tuple_symb, 2); /* arity doesn't matter */
	gBasicSymbols [tuple_symb]	= TupleSymbol;
	gBasicSymbols [tuple_type]	= PredefinedSymbol (tuple_type, 2);

	gBasicSymbols [if_symb]		= PredefinedSymbol (if_symb, 3);
	gBasicSymbols [fail_symb]	= PredefinedSymbol (fail_symb, 0);

	gBasicSymbols [nil_symb]	= PredefinedSymbol (nil_symb, 0);
	gBasicSymbols [cons_symb]	= PredefinedSymbol (cons_symb, 2);

	for (i = 0; i < MaxNodeArity; i++)
		gTupleSelectSymbols [i]	= NULL;

} /* InitPredefinedSymbols */

static void
AddUserDefinedArrayFunction (SymbolP functionSymbol)
{
	PolyList	elem;

	elem	= ConvertAllocType (struct poly_list);

	elem->pl_elem	= functionSymbol;
	elem->pl_next	= UserDefinedArrayFunctions;
	UserDefinedArrayFunctions	= elem;
} /* AddUserDefinedArrayFunction */

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
static Node
NewGuardNode (NodeP ifNode, NodeP node, NodeDefP nodeDefs, StrictNodeIdP stricts)
{
	NodeP	guardNode;
	
	guardNode	= ConvertAllocType (NodeS);
	
	guardNode->node_kind		= GuardNode;
	guardNode->node_node_defs	= nodeDefs;
	guardNode->node_arity		= 2;
	guardNode->node_guard_strict_node_ids	= stricts;

	guardNode->node_arguments	= BEArgs (ifNode, BEArgs (node, NULL));
	
	return (guardNode);
} /* NewGuardNode */

clean's avatar
clean committed
283
284
285
286
287
static void
DeclareModule (int moduleIndex, char *name, Bool isSystemModule, int nFunctions,
											int nTypes, int nConstructors, int nFields)
{
	BEModuleP	module;
288
	SymbolP		symbols;
clean's avatar
clean committed
289

290
	symbols = AllocateSymbols (nFunctions, nTypes + nConstructors + nFields, &gBEState.be_function_symbols);
clean's avatar
clean committed
291
292
293
294
295
296
297
298
299
300
301
302

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	module->bem_name			= name;
	module->bem_isSystemModule	= isSystemModule;

	module->bem_nFunctions	= (unsigned int) nFunctions;
	module->bem_functions	= symbols;
	symbols	+=	nFunctions;

	module->bem_nTypes	= (unsigned int) nTypes;
303
	module->bem_types = symbols;
clean's avatar
clean committed
304
305
306
307
308
309
310
311
	{
		/* +++ change this */
		int		i;
		for (i = 0; i < nTypes; i++)
		{
			SymbDef	newSymbDef;

			newSymbDef	= ConvertAllocType (SymbDefS);
312
			newSymbDef->sdef_mark		= 0;
clean's avatar
clean committed
313
314
315
316
317
318
319
			newSymbDef->sdef_isused	= False;
			symbols [i].symb_def	= newSymbDef;
		}
	}
	symbols	+=	nTypes;

	module->bem_nConstructors	= (unsigned int) nConstructors;
320
	module->bem_constructors = symbols;
clean's avatar
clean committed
321
322
323
324
325
326
327
	symbols	+=	nConstructors;

	module->bem_nFields	= (unsigned int) nFields;
	module->bem_fields	= symbols;
	symbols	+=	nFields;
} /* DeclareModule */

328
329
static int main_dcl_module_n=0;

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
330
331
void
BESetMainDclModuleN (int main_dcl_module_n_parameter)
332
333
334
335
{
	main_dcl_module_n=main_dcl_module_n_parameter;
}

336
337
static DefMod im_def_module;

338
339
340
341
static void	DeclareFunctionC (char *name, int arity, int functionIndex, unsigned int ancestor);
static BESymbolP CreateDictionarySelectFunSymbol (void);
static BESymbolP CreateDictionaryUpdateFunSymbol (void);

clean's avatar
clean committed
342
void
343
BEDeclareIclModule (CleanString name, CleanString modificationTime, int nFunctions, int nTypes, int nConstructors, int nFields)
clean's avatar
clean committed
344
345
346
347
348
349
{
	int		i;
	char	*cName;
	ImpMod	iclModule;
	BEIclP	icl;

350
	cName	= gBEState.be_modules [main_dcl_module_n].bem_name;
clean's avatar
clean committed
351

352
353
354
	if (cName == NULL)
		cName	= ConvertCleanString (name);

355
356
/*	Assert (strcmp (gBEState.be_modules [main_dcl_module_n].bem_name, cName) == 0); */
	Assert (strncmp (cName, name->chars, name->length) == 0);
clean's avatar
clean committed
357
358
359
360

	icl	= &gBEState.be_icl;

	icl->beicl_module		= ConvertAllocType (ImpRepr);
361
	icl->beicl_dcl_module	= gBEState.be_modules [main_dcl_module_n];
clean's avatar
clean committed
362
363
364
	scc_dependency_list	= NULL;
	icl->beicl_depsP	= &scc_dependency_list;

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
	{
		struct module_type_symbols *dcl_type_symbols_a;
		int n_dcl_type_symbols;
		struct def_list *def_mod;
		
		dcl_type_symbols_a = (struct module_type_symbols*) ConvertAlloc (gBEState.be_nModules * sizeof (struct module_type_symbols));
		n_dcl_type_symbols=0;

		for (def_mod=OpenDefinitionModules; def_mod!=NULL; def_mod=def_mod->mod_next){
			int module_n;
			
			module_n = def_mod->mod_body->dm_module_n;
			if (module_n!=main_dcl_module_n){
				dcl_type_symbols_a[n_dcl_type_symbols].mts_n_types       = gBEState.be_modules [module_n].bem_nTypes;
				dcl_type_symbols_a[n_dcl_type_symbols].mts_type_symbol_a = gBEState.be_modules [module_n].bem_types;
				++n_dcl_type_symbols;
			}
		}
		
		dcl_type_symbols_a[n_dcl_type_symbols].mts_n_types       = gBEState.be_modules [kPredefinedModuleIndex].bem_nTypes;
		dcl_type_symbols_a[n_dcl_type_symbols].mts_type_symbol_a = gBEState.be_modules [kPredefinedModuleIndex].bem_types;
		++n_dcl_type_symbols;

		Assert (n_dcl_type_symbols<=gBEState.be_nModules);

		icl->beicl_module->im_dcl_type_symbols_a = dcl_type_symbols_a;
		icl->beicl_module->im_size_dcl_type_symbols_a = n_dcl_type_symbols;
	}

clean's avatar
clean committed
394
	nFunctions	+= ArraySize (gLocallyGeneratedFunctions);
395
	DeclareModule (main_dcl_module_n, cName, False, nFunctions, nTypes, nConstructors, nFields);
clean's avatar
clean committed
396
397

	iclModule	= icl->beicl_module;
398
	iclModule->im_name			= cName;
399
	iclModule->im_modification_time	= ConvertCleanString (modificationTime);
400
	iclModule->im_def_module	= im_def_module;
clean's avatar
clean committed
401
402
	iclModule->im_rules			= NULL;
	iclModule->im_start			= NULL;
403
404
	iclModule->im_type_symbols.mts_n_types = nTypes;
	iclModule->im_type_symbols.mts_type_symbol_a = gBEState.be_modules [main_dcl_module_n].bem_types;
405
	iclModule->im_function_symbols = gBEState.be_function_symbols;
clean's avatar
clean committed
406
407
408
409
# if IMPORT_OBJ_AND_LIB
	iclModule->im_imported_objs	= NULL;
	iclModule->im_imported_libs	= NULL;
# endif
John van Groningen's avatar
John van Groningen committed
410
	iclModule->im_foreign_exports=NULL;
clean's avatar
clean committed
411
412
413
414
415
416
417
418
419

	CurrentModule	= cName;

	for (i = 0; i < ArraySize (gLocallyGeneratedFunctions); i++)
	{
		BELocallyGeneratedFunctionP	locallyGeneratedFunction;

		locallyGeneratedFunction	= &gLocallyGeneratedFunctions [i];

420
		DeclareFunctionC (locallyGeneratedFunction->lgf_name, locallyGeneratedFunction->lgf_arity, nFunctions-ArraySize(gLocallyGeneratedFunctions)+i,0);
clean's avatar
clean committed
421
422
423
424
425
426
427
428
429
430
	}

	/* +++ hack */
	{
		gBEState.be_dictionarySelectFunSymbol	= CreateDictionarySelectFunSymbol ();
		gBEState.be_dictionaryUpdateFunSymbol	= CreateDictionaryUpdateFunSymbol ();
	}
} /* BEDeclareIclModule */

void
431
BEDeclareDclModule (int moduleIndex, CleanString name, CleanString modificationTime, int isSystemModule, int nFunctions, int nTypes, int nConstructors, int nFields)
clean's avatar
clean committed
432
433
434
435
{
	char	*cName;
	SymbolP	moduleNameSymbol;
	DefMod	dclModule;
436
	SymbolP	saved_function_symbols,previous_all_symbols;
clean's avatar
clean committed
437
438
439
440
441
442

	cName	= ConvertCleanString (name);

	moduleNameSymbol	= ConvertAllocType (SymbolS);
	moduleNameSymbol->symb_ident	= Identifier (cName);

443
444
445
	if (moduleIndex == main_dcl_module_n){
		saved_function_symbols = gBEState.be_function_symbols;
		gBEState.be_function_symbols = NULL;
446
447
	}

448
	previous_all_symbols = gBEState.be_function_symbols;
449

clean's avatar
clean committed
450
451
452
	DeclareModule (moduleIndex, cName, isSystemModule, nFunctions, nTypes, nConstructors, nFields);

	dclModule	= ConvertAllocType (DefRepr);
453
	dclModule->dm_name			= cName;
454
	dclModule->dm_module_n = moduleIndex;
455
	dclModule->dm_modification_time	= ConvertCleanString (modificationTime);
clean's avatar
clean committed
456
	dclModule->dm_system_module	= isSystemModule;
457
	dclModule->dm_function_symbols = gBEState.be_function_symbols;
458
	dclModule->dm_symbols_end = previous_all_symbols;
459
	dclModule->dm_system_module_table_kind = FirstSystemModuleTable + moduleIndex;
clean's avatar
clean committed
460

461
462
	AddOpenDefinitionModule (moduleNameSymbol, dclModule);

463
	if (moduleIndex == main_dcl_module_n){
464
		gBEState.be_function_symbols = saved_function_symbols;
465
466
		im_def_module=dclModule;
	}
clean's avatar
clean committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
} /* BEDeclareDclModule */

void
BEDeclarePredefinedModule (int nTypes, int nConstructors)
{
	char	*cName;

	cName	= "_predef";

	DeclareModule (kPredefinedModuleIndex, cName, False, 0, nTypes, nConstructors, 0);
} /* BEDeclarePredefinedModule */

void
BEDeclareModules (int nModules)
{
482
483
	int	i;

clean's avatar
clean committed
484
485
486
487
	Assert (gBEState.be_modules == NULL);

	gBEState.be_nModules	= (unsigned int) nModules;
	gBEState.be_modules		= (BEModuleP) ConvertAlloc (nModules * sizeof (BEModuleS));
488
489

	for (i = 0; i < nModules; i++)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
490
	{
491
		gBEState.be_modules [i].bem_name	= NULL;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
492
493
		gBEState.be_modules [i].bem_nFunctions	= 0;
	}
clean's avatar
clean committed
494
495
496
497
498
499
500
501
502
503
504
505
506
} /* BEDeclareModules */

BESymbolP
BEFunctionSymbol (int functionIndex, int moduleIndex)
{
	BEModuleP	module;
	SymbolP		functionSymbol;

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) functionIndex < module->bem_nFunctions);
	functionSymbol	= &module->bem_functions [functionIndex];
507
	Assert (functionSymbol->symb_kind == definition || functionSymbol->symb_kind == cons_symb || functionSymbol->symb_kind == nil_symb
clean's avatar
clean committed
508
509
				|| (moduleIndex == kPredefinedModuleIndex && functionSymbol->symb_kind != erroneous_symb));

510
511
	if (functionSymbol->symb_kind!=cons_symb && functionSymbol->symb_kind!=nil_symb)
		functionSymbol->symb_def->sdef_isused	= True;
clean's avatar
clean committed
512
513
514
515

	return (functionSymbol);
} /* BEFunctionSymbol */

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
void
BEBindSpecialModule (BESpecialIdentIndex index, int moduleIndex)
{
	BEModuleP	module;

	Assert (index >= 0 && index < BESpecialIdentCount);

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	(*gSpecialIdents [index])->ident_name	= module->bem_name;
} /* BEBindSpecialModule */

void
BEBindSpecialFunction (BESpecialIdentIndex index, int functionIndex, int moduleIndex)
{
	SymbolP		functionSymbol;
	BEModuleP	module;

	Assert (index >= 0 && index < BESpecialIdentCount);

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) functionIndex < module->bem_nFunctions);
	functionSymbol	= &module->bem_functions [functionIndex];

John van Groningen's avatar
add seq    
John van Groningen committed
543
	if (functionSymbol->symb_kind == definition){
544
		*gSpecialIdents [index]	= functionSymbol->symb_def->sdef_ident;
John van Groningen's avatar
add seq    
John van Groningen committed
545
546
547
548
549
		
		if (index==BESpecialIdentSeq && moduleIndex!=main_dcl_module_n){
			functionSymbol->symb_kind=seq_symb;
		}
	}
550
551
} /* BEBindSpecialFunction */

552
553
554
555
556
557
558
559
extern SymbDefP special_types[]; /* defined in statesgen */

void BEBindSpecialType (int special_type_n,int type_index,int module_index)
{
	SymbolP		type_symbol_p;
	BEModuleP	module;

	module	= &gBEState.be_modules [module_index];
560
	type_symbol_p = &module->bem_types [type_index];
561
562
563
564
565
566
	if (type_symbol_p->symb_kind==definition)
		special_types[special_type_n] = type_symbol_p->symb_def;
	else
		special_types[special_type_n] = NULL;
}

clean's avatar
clean committed
567
568
569
570
BESymbolP
BESpecialArrayFunctionSymbol (BEArrayFunKind arrayFunKind, int functionIndex, int moduleIndex)
{
	BEModuleP	module;
571
	SymbolP		functionSymbol,previousFunctionSymbol;
clean's avatar
clean committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
	SymbDefP	originalsdef;
	TypeAlt		*typeAlt;
	TypeNode	elementType, arrayType;

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) functionIndex < module->bem_nFunctions);
	functionSymbol	= &module->bem_functions [functionIndex];
	Assert (functionSymbol->symb_kind == definition
				|| (moduleIndex == kPredefinedModuleIndex && functionSymbol->symb_kind != erroneous_symb));

	originalsdef	= functionSymbol->symb_def;

	typeAlt	= originalsdef->sdef_rule_type->rule_type_rule;
	switch (arrayFunKind)
	{
		case BE_UnqArraySelectFun:
		case BE_UnqArraySelectLastFun:
			Assert (typeAlt->type_alt_lhs->type_node_arity == 2);
			elementType	= typeAlt->type_alt_rhs;
			arrayType	= typeAlt->type_alt_lhs->type_node_arguments->type_arg_node;
			Assert (originalsdef->sdef_arfun == BEArraySelectFun);
			break;
		case BE_ArrayUpdateFun:
			elementType	= typeAlt->type_alt_lhs->type_node_arguments->type_arg_next->type_arg_next->type_arg_node;
			arrayType	= typeAlt->type_alt_lhs->type_node_arguments->type_arg_node;
			Assert (originalsdef->sdef_arfun == BEArrayUpdateFun);
			break;
		default:
			Assert (False);
603
			return (functionSymbol);
clean's avatar
clean committed
604
605
	}

606
607
	previousFunctionSymbol	= functionSymbol;
	functionSymbol	= functionSymbol->symb_next;
clean's avatar
clean committed
608

609
610
611
	if (functionSymbol != NULL && functionSymbol->symb_kind == definition){
		if (functionSymbol->symb_def->sdef_arfun == (ArrayFunKind) arrayFunKind)
			return functionSymbol;
clean's avatar
clean committed
612

613
614
615
616
617
618
		if (arrayFunKind == BE_UnqArraySelectLastFun && functionSymbol->symb_def->sdef_arfun == BE_UnqArraySelectFun){
			previousFunctionSymbol	= functionSymbol;
			functionSymbol	= functionSymbol->symb_next;

			if (functionSymbol != NULL && functionSymbol->symb_kind == definition &&
				functionSymbol->symb_def->sdef_arfun == (ArrayFunKind) arrayFunKind)
clean's avatar
clean committed
619
			{
620
				return functionSymbol;
clean's avatar
clean committed
621
622
			}
		}
623
	}
clean's avatar
clean committed
624

625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
	{
		char		*functionName, *functionPrefix;
		TypeAlt		*newTypeAlt;
		IdentP		newIdent;
		SymbDefP	newsdef;
		SymbolP		newFunctionSymbol;
		RuleTypes	newRuleType;
		TypeArgs	lhsArgs;
		TypeNode	rhs;

		newFunctionSymbol	= ConvertAllocType (SymbolS);
		newsdef				= ConvertAllocType (SymbDefS);
		newIdent			= ConvertAllocType (IdentS);

		newTypeAlt	= ConvertAllocType (TypeAlt);

		Assert (!arrayType->type_node_is_var);
		switch (arrayType->type_node_symbol->symb_kind)
		{
			case strict_array_type:
			case unboxed_array_type:
				elementType->type_node_annotation	= StrictAnnot;
				break;
			case array_type:
				break;
			default:
				Assert (False);
				break;
		}

		switch (arrayFunKind)
clean's avatar
clean committed
656
		{
657
658
659
660
661
662
663
			case BE_UnqArraySelectFun:
				rhs	= BESymbolTypeNode (NoAnnot,NoUniAttr,gBasicSymbols [tuple_type],
											BETypeArgs (elementType, BETypeArgs (arrayType, NULL)));
				lhsArgs	= BETypeArgs (arrayType, BETypeArgs (BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [int_type], NULL), NULL));
				functionPrefix	= "_uselectf";
				break;
			case BE_UnqArraySelectLastFun:
clean's avatar
clean committed
664
			{
665
666
667
668
669
670
671
672
673
674
675
				TypeNode			rType;

				rType	= BEVar0TypeNode (StrictAnnot,NoUniAttr);
				rhs	= BESymbolTypeNode (NoAnnot,NoUniAttr,gBasicSymbols [tuple_type],
											BETypeArgs (elementType, BETypeArgs (rType, NULL)));
				lhsArgs	= BETypeArgs (
							BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [tuple_type],
									BETypeArgs (arrayType, BETypeArgs (rType, NULL))),
							BETypeArgs (BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [int_type], NULL), NULL));
				functionPrefix	= "_uselectl";
				break;
clean's avatar
clean committed
676
			}
677
			case BE_ArrayUpdateFun:
clean's avatar
clean committed
678
			{
679
680
681
682
683
684
685
686
687
688
689
690
				TypeNode			rType;

				rType	= BEVar0TypeNode (StrictAnnot,NoUniAttr);
				rhs	= rType;
				lhsArgs	= BETypeArgs (
							BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [tuple_type],
									BETypeArgs (arrayType, BETypeArgs (rType, NULL))),
							BETypeArgs (BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [int_type], NULL),
							BETypeArgs (elementType,
							NULL)));
				functionPrefix	= "_updatei";
				break;
clean's avatar
clean committed
691
			}
692
693
694
695
			default:
				Assert (False);
				break;
		}
clean's avatar
clean committed
696

697
698
699
700
		functionName	= ConvertAlloc (strlen (functionPrefix) + 1 + strlen (originalsdef->sdef_ident->ident_name) + 1);
		strcpy (functionName, functionPrefix);
		strcat (functionName, ";");
		strcat (functionName, originalsdef->sdef_ident->ident_name);
clean's avatar
clean committed
701

702
703
704
		newTypeAlt->type_alt_lhs	= BESymbolTypeNode (NoAnnot,NoUniAttr,newFunctionSymbol, lhsArgs);
		newTypeAlt->type_alt_rhs	= rhs;
		newTypeAlt->type_alt_strict_positions	= NULL;
clean's avatar
clean committed
705

706
707
		newIdent->ident_symbol	= newFunctionSymbol;
		newIdent->ident_name	= functionName;
clean's avatar
clean committed
708

709
710
		newRuleType	= ConvertAllocType (struct rule_type);
		newRuleType->rule_type_rule	= newTypeAlt;
clean's avatar
clean committed
711

712
713
714
715
716
717
718
719
720
721
722
		newsdef->sdef_ident			= newIdent;
		newsdef->sdef_module		= gBEState.be_icl.beicl_module->im_name;
		newsdef->sdef_mark		= 0;
		newsdef->sdef_isused		= True;
		newsdef->sdef_exported		= False;
		newsdef->sdef_arity			= newTypeAlt->type_alt_lhs->type_node_arity;
		newsdef->sdef_arfun			= arrayFunKind;
		newsdef->sdef_kind 			= SYSRULE;
		newsdef->sdef_rule_type		= newRuleType;
		newsdef->sdef_ident			= newIdent;
		newsdef->sdef_mark			= 0;
clean's avatar
clean committed
723

724
725
		newFunctionSymbol->symb_kind	= definition;
		newFunctionSymbol->symb_def		= newsdef;
clean's avatar
clean committed
726

727
728
729
		functionSymbol						= previousFunctionSymbol->symb_next;
		previousFunctionSymbol->symb_next	= newFunctionSymbol;
		newFunctionSymbol->symb_next		= functionSymbol;
clean's avatar
clean committed
730

731
		AddUserDefinedArrayFunction (newFunctionSymbol);
clean's avatar
clean committed
732

733
		functionSymbol	= newFunctionSymbol;
clean's avatar
clean committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
	}

	return (functionSymbol);
} /* BESpecialArrayFunctionSymbol */

static SymbolP
CreateLocallyDefinedFunction (int index, char ** abcCode, TypeArgs lhsArgs, TypeNode rhsType)
{
	int				i, arity, functionIndex;
	NodeP			lhs;
	BEStringListP	instructions, *instructionsP;
	BECodeBlockP	codeBlock;
	RuleAltP		ruleAlt;
	SymbolP			functionSymbol;
	TypeAlt			*typeAlt;
	ArgP			args;

751
752
	functionIndex	= gBEState.be_modules[main_dcl_module_n].bem_nFunctions - ArraySize (gLocallyGeneratedFunctions) + index;
	functionSymbol	= BEFunctionSymbol (functionIndex, main_dcl_module_n);
clean's avatar
clean committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
	functionSymbol->symb_def->sdef_isused	= False;

	instructionsP	= &instructions;
	for (i = 0; abcCode [i] != NULL; i++)
	{
		BEStringListP	string;

		string	= ConvertAllocType (struct string_list);

		string->sl_string	= abcCode [i];
		string->sl_next		= instructions;

		*instructionsP	= string;
		instructionsP	= &string->sl_next;
	}
	*instructionsP	=	NULL;

	codeBlock	= BEAbcCodeBlock (False, instructions);
		
	lhs		= BENormalNode (functionSymbol, NULL);
	arity	= CountTypeArgs (lhsArgs);

	args	= NULL;
	for (i = 0; i < arity; i++)
		args	= BEArgs (BENodeIdNode (BEWildCardNodeId (), NULL), args);

	lhs->node_arguments	= args;
	lhs->node_arity		= arity;

	Assert (arity == functionSymbol->symb_def->sdef_arity);

	ruleAlt		= BECodeAlt (0, NULL, lhs, codeBlock);

	typeAlt	= ConvertAllocType (TypeAlt);

788
	typeAlt->type_alt_lhs	= BESymbolTypeNode (NoAnnot,NoUniAttr,functionSymbol, lhsArgs);
clean's avatar
clean committed
789
	typeAlt->type_alt_rhs	= rhsType;
790
	typeAlt->type_alt_strict_positions	= NULL;
clean's avatar
clean committed
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828

	BERule (functionIndex, BEIsNotACaf, typeAlt, ruleAlt);
	
	return (functionSymbol);
} /* CreateLocallyDefinedFunction */

static BESymbolP
CreateDictionarySelectFunSymbol (void)
{
	TypeNode		rhsType;
	TypeArgs		lhsArgs;

	/* selectl :: !((a e) Int -> e) !(!a e, !r) !Int -> (e, !r) */
	/* select _ _ _ = code */
	static char *abcCode[] = {
		"push_a 1",
		"push_a 1",
		"build e_system_dAP 2 e_system_nAP",
		"buildI_b 0",
		"push_a 1",
		"update_a 1 2",
		"update_a 0 1",
		"pop_a 1",
		"build e_system_dAP 2 e_system_nAP",
		"push_a 3",
		"push_a 1",
		"update_a 1 2",
		"update_a 0 1",
		"pop_a 1",
		"update_a 1 4",
		"update_a 0 3",
		"pop_a 3",
		"pop_b 1",
		NULL
	};

	/*	actual type simplified to !a !(!a,!a) !Int -> (a,!a) */
	lhsArgs	=	BETypeArgs (
829
					BEVar0TypeNode (StrictAnnot,NoUniAttr),
clean's avatar
clean committed
830
				BETypeArgs (
831
					BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [tuple_type],
clean's avatar
clean committed
832
								BETypeArgs (
833
									BEVar0TypeNode (StrictAnnot,NoUniAttr),
clean's avatar
clean committed
834
								BETypeArgs (
835
836
									BEVar0TypeNode (StrictAnnot,NoUniAttr),
								NULL))),
clean's avatar
clean committed
837
				BETypeArgs (
838
					BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [int_type], NULL),
clean's avatar
clean committed
839
				NULL)));
840
841
	rhsType	= BESymbolTypeNode (NoAnnot,NoUniAttr,gBasicSymbols [tuple_type],
								BETypeArgs (BEVar0TypeNode (NoAnnot,NoUniAttr), BETypeArgs (BEVar0TypeNode (StrictAnnot,NoUniAttr), NULL)));
clean's avatar
clean committed
842
843
844
845
846
847
848
849
850
851
852
853
854

	return (CreateLocallyDefinedFunction (kDictionarySelect, abcCode, lhsArgs, rhsType));
} /* CreateDictionarySelectFunSymbol */

static BESymbolP
CreateDictionaryUpdateFunSymbol (void)
{
	TypeNode		rhsType;
	TypeArgs		lhsArgs;

	/* updatei :: !(*(a .e) -> *(!Int -> *(.e -> .(a .e)))) !(!*(a .e), !*r) !Int .e -> *r // !(!.(a .e), !*r) */
	/* updatei _ _ _ _ = code */
	static char *abcCode[] = {
855
856
857
858
859
860
861
862
863
864
865
866
		"push_a 3",
		"buildI_b 0",
		"push_a 3",
		"push_a 3",
		"pop_b 1",
		"update_a 6 7",
		"update_a 3 6",
		"update_a 2 5",
		"update_a 1 4",
		"updatepop_a 0 3",
		"jsr_ap 3",
		"pop_a 1",
clean's avatar
clean committed
867
868
869
870
871
		NULL
	};

	/*	actual type simplified to !a !(!a,!a) !Int a -> a */
	lhsArgs	=	BETypeArgs (
872
					BEVar0TypeNode (StrictAnnot,NoUniAttr),
clean's avatar
clean committed
873
				BETypeArgs (
874
					BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [tuple_type],
clean's avatar
clean committed
875
								BETypeArgs (
876
									BEVar0TypeNode (StrictAnnot,NoUniAttr),
clean's avatar
clean committed
877
								BETypeArgs (
878
879
									BEVar0TypeNode (StrictAnnot,NoUniAttr),
								NULL))),
clean's avatar
clean committed
880
				BETypeArgs (
881
					BESymbolTypeNode (StrictAnnot,NoUniAttr,gBasicSymbols [int_type], NULL),
clean's avatar
clean committed
882
				BETypeArgs (
883
					BEVar0TypeNode (NoAnnot,NoUniAttr),
clean's avatar
clean committed
884
885
				NULL))));

886
	rhsType	= BEVar0TypeNode (NoAnnot,NoUniAttr);
clean's avatar
clean committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917

	return (CreateLocallyDefinedFunction (kDictionaryUpdate, abcCode, lhsArgs, rhsType));
} /* CreateDictionaryUpdateFunSymbol */

BESymbolP
BEDictionarySelectFunSymbol (void)
{
	gBEState.be_dictionarySelectFunSymbol->symb_def->sdef_isused	= True;

	return (gBEState.be_dictionarySelectFunSymbol);
} /* BEDictionarySelectFunSymbol */

BESymbolP
BEDictionaryUpdateFunSymbol (void)
{
	gBEState.be_dictionaryUpdateFunSymbol->symb_def->sdef_isused	= True;

	return (gBEState.be_dictionaryUpdateFunSymbol);
} /* BEDictionaryUpdateFunSymbol */

BESymbolP
BETypeSymbol (int typeIndex, int moduleIndex)
{
	BEModuleP	module;
	SymbolP		typeSymbol;

	if ((unsigned int) moduleIndex >= gBEState.be_nModules)
		Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) typeIndex < module->bem_nTypes);
918
	typeSymbol	= &module->bem_types [typeIndex];
clean's avatar
clean committed
919
920
921
/*	Assert (typeSymbol->symb_kind == definition
				|| (moduleIndex == kPredefinedModuleIndex && typeSymbol->symb_kind != erroneous_symb));
*/
922
	if (moduleIndex == main_dcl_module_n)
clean's avatar
clean committed
923
924
925
926
927
		typeSymbol->symb_def->sdef_isused	= True;

	return (typeSymbol);
} /* BETypeSymbol */

John van Groningen's avatar
John van Groningen committed
928
929
BESymbolP BETypeSymbolNoMark (int typeIndex, int moduleIndex)
{
930
	return &gBEState.be_modules [moduleIndex].bem_types [typeIndex];
John van Groningen's avatar
John van Groningen committed
931
932
}

clean's avatar
clean committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
BESymbolP
BEDontCareDefinitionSymbol (void)
{
	SymbolP	symbol;

	symbol	= gBEState.be_dontCareSymbol;
	if (symbol == NULL)
	{
		SymbDefP	symbDef;

		symbDef	= ConvertAllocType (SymbDefS);
		symbDef->sdef_kind	= ABSTYPE;

		symbDef->sdef_ident	= Identifier ("_Don'tCare"); /* +++ name */

		symbol	= ConvertAllocType (SymbolS);
		symbol->symb_kind	= definition;
		symbol->symb_def	= symbDef;

		gBEState.be_dontCareSymbol	= symbol;
	}

	return (symbol);
} /* BEDontCareDefinitionSymbol */

BESymbolP
BEConstructorSymbol (int constructorIndex, int moduleIndex)
{
	BEModuleP	module;
	SymbolP		constructorSymbol;

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) constructorIndex < module->bem_nConstructors);
968
	constructorSymbol = &module->bem_constructors [constructorIndex];
clean's avatar
clean committed
969

970
	Assert (constructorSymbol->symb_kind == definition || constructorSymbol->symb_kind == cons_symb
clean's avatar
clean committed
971
972
				|| (moduleIndex == kPredefinedModuleIndex && constructorSymbol->symb_kind != erroneous_symb));

973
	if (moduleIndex != kPredefinedModuleIndex && constructorSymbol->symb_kind!=cons_symb)
clean's avatar
clean committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
		constructorSymbol->symb_def->sdef_isused	= True;

	return (constructorSymbol);
} /* BEConstructorSymbol */

BESymbolP
BEFieldSymbol (int fieldIndex, int moduleIndex)
{
	BEModuleP	module;
	SymbolP		fieldSymbol;

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) fieldIndex < module->bem_nFields);
	fieldSymbol	= &module->bem_fields [fieldIndex];
	Assert (fieldSymbol->symb_kind == definition);

	fieldSymbol->symb_def->sdef_isused	= True;

	return (fieldSymbol);
} /* BEFieldSymbol */

BESymbolP
BEBoolSymbol (int value)
{
	if (value)
		return TrueSymbol;
	else
		return FalseSymbol;
} /* BEBoolSymbol */

BESymbolP
BELiteralSymbol (BESymbKind kind, CleanString value)
{
	SymbolP	symbol;

	symbol	= ConvertAllocType (SymbolS);
	symbol->symb_kind	= kind;
	symbol->symb_int	= ConvertCleanString (value);

	return (symbol);
} /* BELiteralSymbol */

1018
1019
# define	nid_ref_count_sign	nid_scope

1020
#if STRICT_LISTS
1021
1022
1023
1024
1025
static SymbolS unboxed_list_symbols[Nr_Of_Predef_Types][2];

static SymbolP strict_list_cons_symbols[8];

void BEPredefineListConstructorSymbol (int constructorIndex,int moduleIndex,BESymbKind symbolKind,int head_strictness,int tail_strictness)
1026
1027
1028
1029
1030
1031
1032
{
	BEModuleP	module;
	SymbolP symbol_p;

	Assert (moduleIndex == kPredefinedModuleIndex);

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
1033
	module = &gBEState.be_modules [moduleIndex];
1034
1035

	Assert ((unsigned int) constructorIndex < module->bem_nConstructors);
1036
	symbol_p=&module->bem_constructors [constructorIndex];
1037
1038
1039
	symbol_p->symb_kind	= symbolKind;
	symbol_p->symb_head_strictness=head_strictness;
	symbol_p->symb_tail_strictness=tail_strictness;
1040
1041
1042

	if (symbolKind==BEConsSymb && head_strictness<4)
		strict_list_cons_symbols[(head_strictness<<1)+tail_strictness]=symbol_p;
1043
1044
}

1045
void BEPredefineListTypeSymbol (int typeIndex,int moduleIndex,BESymbKind symbolKind,int head_strictness,int tail_strictness)
1046
1047
1048
1049
1050
1051
1052
{
	BEModuleP	module;
	SymbolP symbol_p;

	Assert (moduleIndex == kPredefinedModuleIndex);

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
1053
	module = &gBEState.be_modules [moduleIndex];
1054
1055

	Assert ((unsigned int) typeIndex < module->bem_nTypes);
1056
	symbol_p=&module->bem_types [typeIndex];
1057
1058
	symbol_p->symb_kind = symbolKind;
	symbol_p->symb_arity = 1;
1059
1060
1061
	symbol_p->symb_head_strictness=head_strictness;
	symbol_p->symb_tail_strictness=tail_strictness;
}
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201

void BEAdjustStrictListConsInstance (int functionIndex,int moduleIndex)
{
	SymbolP symbol_p;

	symbol_p=&gBEState.be_modules[moduleIndex].bem_functions[functionIndex];

	if (symbol_p->symb_kind==definition){
		TypeNode element_type_p,list_type_p;
		SymbDef sdef;
		TypeArgs type_args_p;
		
		sdef=symbol_p->symb_def;
		type_args_p=sdef->sdef_rule_type->rule_type_rule->type_alt_lhs->type_node_arguments;
		element_type_p=type_args_p->type_arg_node;
		list_type_p=type_args_p->type_arg_next->type_arg_node;
		
		Assert (list_type_p->type_node_is_var==0);
		Assert (list_type_p->type_node_symbol->symb_kind==list_type);

		symbol_p->symb_head_strictness=list_type_p->type_node_symbol->symb_head_strictness;
		symbol_p->symb_tail_strictness=list_type_p->type_node_symbol->symb_tail_strictness;

		if (list_type_p->type_node_symbol->symb_head_strictness==3){
			int element_symbol_kind;
			struct unboxed_cons *unboxed_cons_p;

			Assert (element_type_p->type_node_is_var==0);

			element_symbol_kind=element_type_p->type_node_symbol->symb_kind;

			symbol_p->symb_head_strictness=4;

			unboxed_cons_p=ConvertAllocType (struct unboxed_cons);

			unboxed_cons_p->unboxed_cons_sdef_p=sdef;

			if (element_symbol_kind < Nr_Of_Predef_Types)
				unboxed_cons_p->unboxed_cons_state_p = unboxed_list_symbols[element_symbol_kind][symbol_p->symb_tail_strictness].symb_state_p;
			else if (element_symbol_kind==definition && element_type_p->type_node_symbol->symb_def->sdef_kind==RECORDTYPE){
				PolyList new_unboxed_record_cons_element;
				SymbDef record_sdef;
				
				record_sdef=element_type_p->type_node_symbol->symb_def;
				record_sdef->sdef_isused=True;
				sdef->sdef_isused=True;
				unboxed_cons_p->unboxed_cons_state_p = &record_sdef->sdef_record_state;
				
				new_unboxed_record_cons_element=ConvertAllocType (struct poly_list);
				new_unboxed_record_cons_element->pl_elem = sdef;
				new_unboxed_record_cons_element->pl_next = unboxed_record_cons_list;
				unboxed_record_cons_list = new_unboxed_record_cons_element;
				
				sdef->sdef_module=NULL;
			} else
				unboxed_cons_p->unboxed_cons_state_p = &StrictState;
			
			symbol_p->symb_unboxed_cons_p=unboxed_cons_p;
		}
	} else {
		Assert (symbol_p->symb_kind==definition);
		
		debug_message ("BEAdjustStrictListInstance: !(symbol_p->symb_kind==definition) %d %d %d\n",functionIndex,moduleIndex,symbol_p->symb_kind);

		symbol_p->symb_head_strictness=0;
		symbol_p->symb_tail_strictness=0;
	}
	
	symbol_p->symb_kind = cons_symb;
	/* symbol_p->symb_arity = 2; no symb_arity for cons_symb, because symb_state_p is used of this union */
}

void BEAdjustUnboxedListDeconsInstance (int functionIndex,int moduleIndex)
{
	SymbolP symbol_p,cons_symbol_p;
	SymbDefP sdef_p;
	TypeNode element_type_p,list_type_p;
	PolyList new_unboxed_record_decons_element;

	symbol_p=&gBEState.be_modules[moduleIndex].bem_functions[functionIndex];

	Assert (symbol_p->symb_kind==definition);
	sdef_p=symbol_p->symb_def;
	
	list_type_p=sdef_p->sdef_rule_type->rule_type_rule->type_alt_lhs->type_node_arguments->type_arg_node;
	element_type_p=list_type_p->type_node_arguments->type_arg_node;
	
	Assert (list_type_p->type_node_is_var==0);
	Assert (list_type_p->type_node_symbol->symb_kind==list_type);
	Assert (list_type_p->type_node_symbol->symb_head_strictness==3);
	Assert (element_type_p->type_node_symbol->symb_def->sdef_kind==RECORDTYPE);
	
	cons_symbol_p=ConvertAllocType (SymbolS);

	cons_symbol_p->symb_kind = cons_symb;
	cons_symbol_p->symb_head_strictness=4;
	cons_symbol_p->symb_tail_strictness=list_type_p->type_node_symbol->symb_tail_strictness;
	cons_symbol_p->symb_state_p=&element_type_p->type_node_symbol->symb_def->sdef_record_state;

	sdef_p->sdef_unboxed_cons_symbol=cons_symbol_p;
	
	new_unboxed_record_decons_element=ConvertAllocType (struct poly_list);
	new_unboxed_record_decons_element->pl_elem = sdef_p;
	new_unboxed_record_decons_element->pl_next = unboxed_record_decons_list;
	unboxed_record_decons_list = new_unboxed_record_decons_element;
}

void BEAdjustOverloadedNilFunction (int functionIndex,int moduleIndex)
{
	SymbolP symbol_p;

	symbol_p=&gBEState.be_modules[moduleIndex].bem_functions[functionIndex];

	symbol_p->symb_head_strictness=1;
	symbol_p->symb_tail_strictness=0;
	
	symbol_p->symb_kind = nil_symb;
}

BESymbolP BEOverloadedConsSymbol (int constructorIndex,int moduleIndex,int deconsIndex,int deconsModuleIndex)
{
	BEModuleP module,decons_module;
	SymbolP constructor_symbol,decons_symbol,list_type_symbol;
	TypeNode list_type,element_type;

	Assert ((unsigned int) deconsModuleIndex < gBEState.be_nModules);
	decons_module = &gBEState.be_modules [deconsModuleIndex];

	Assert ((unsigned int) deconsIndex < decons_module->bem_nFunctions);
	decons_symbol = &decons_module->bem_functions [deconsIndex];

	Assert (decons_symbol->symb_kind==definition);
	
	list_type=decons_symbol->symb_def->sdef_rule_type->rule_type_rule->type_alt_lhs->type_node_arguments->type_arg_node;
	element_type=list_type->type_node_arguments->type_arg_node;
	
	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module = &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) constructorIndex < module->bem_nConstructors);
1202
	constructor_symbol = &module->bem_constructors [constructorIndex];
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251

	Assert (constructor_symbol->symb_kind==definition
				|| (moduleIndex==kPredefinedModuleIndex && constructor_symbol->symb_kind!=erroneous_symb));

	if (moduleIndex != kPredefinedModuleIndex)
		constructor_symbol->symb_def->sdef_isused = True;

	list_type_symbol=list_type->type_node_symbol;

	if (constructor_symbol->symb_head_strictness==1 && list_type_symbol->symb_head_strictness<4)
		constructor_symbol=strict_list_cons_symbols[(list_type_symbol->symb_head_strictness<<1)+list_type_symbol->symb_tail_strictness];

	if (list_type_symbol->symb_head_strictness==3){
		int element_symbol_kind;
		
		Assert (element_type->type_node_is_var==0);

		element_symbol_kind=element_type->type_node_symbol->symb_kind;

		if (element_symbol_kind<Nr_Of_Predef_Types)
			constructor_symbol=&unboxed_list_symbols[element_symbol_kind][list_type_symbol->symb_tail_strictness];
		else if (element_symbol_kind==definition && element_type->type_node_symbol->symb_def->sdef_kind==RECORDTYPE)
			constructor_symbol=decons_symbol->symb_def->sdef_unboxed_cons_symbol;
	}
	
	return constructor_symbol;
}

BENodeP BEOverloadedPushNode (int arity,BESymbolP symbol,BEArgP arguments,BENodeIdListP nodeIds,BENodeP decons_node)
{
	NodeP	push_node;

	push_node	= ConvertAllocType (NodeS);

	push_node->node_kind		= PushNode;
	push_node->node_arity		= arity;
	push_node->node_arguments	= arguments;
	push_node->node_push_symbol = symbol;
	push_node->node_decons_node = decons_node;
	push_node->node_node_ids	= nodeIds;
	push_node->node_number		= 0;

	Assert (arguments->arg_node->node_kind == NodeIdNode);
	Assert (arguments->arg_node->node_node_id->nid_ref_count_sign == -1);
		
	arguments->arg_node->node_node_id->nid_refcount++;
	
	return push_node;
}
1252
1253
#endif

clean's avatar
clean committed
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
void
BEPredefineConstructorSymbol (int arity, int constructorIndex, int moduleIndex, BESymbKind symbolKind)
{
	BEModuleP	module;

	Assert (moduleIndex == kPredefinedModuleIndex);

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) constructorIndex < module->bem_nConstructors);
1265
	Assert (module->bem_constructors [constructorIndex].symb_kind == erroneous_symb);
clean's avatar
clean committed
1266

1267
1268
	module->bem_constructors [constructorIndex].symb_kind	= symbolKind;
	module->bem_constructors [constructorIndex].symb_arity	= arity;
clean's avatar
clean committed
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
} /* BEPredefineConstructorSymbol */

void
BEPredefineTypeSymbol (int arity, int typeIndex, int moduleIndex, BESymbKind symbolKind)
{
	BEModuleP	module;

	Assert (moduleIndex == kPredefinedModuleIndex);

	Assert ((unsigned int) moduleIndex < gBEState.be_nModules);
	module	= &gBEState.be_modules [moduleIndex];

	Assert ((unsigned int) typeIndex < module->bem_nTypes);
1282
	Assert (module->bem_types [typeIndex].symb_kind == erroneous_symb);
clean's avatar
clean committed
1283

1284
1285
	module->bem_types [typeIndex].symb_kind		= symbolKind;
	module->bem_types [typeIndex].symb_arity	= arity;
clean's avatar
clean committed
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
} /* BEPredefineTypeSymbol */

BESymbolP
BEBasicSymbol (BESymbKind kind)
{
	Assert (gBasicSymbols [kind] != NULL);

	return (gBasicSymbols [kind]);
} /* BEBasicSymbol */

BETypeNodeP
1297
BEVar0TypeNode (BEAnnotation annotation, BEAttribution attribution)
clean's avatar
clean committed
1298
{
1299
	TypeNode node;
clean's avatar
clean committed
1300

1301
	node = ConvertAllocType (struct type_node);
clean's avatar
clean committed
1302
1303

	node->type_node_is_var		= True;
1304
	node->type_node_tv_argument_n = 0;
clean's avatar
clean committed
1305
	node->type_node_arity		= 0;
1306
1307
	node->type_node_annotation	= annotation;
	node->type_node_attribute	= attribution;
clean's avatar
clean committed
1308

1309
1310
	return node;
}
clean's avatar
clean committed
1311

1312
BETypeNodeP
1313
BEVarNTypeNode (BEAnnotation annotation, BEAttribution attribution, int argument_n)
1314
{
1315
	TypeNode node;
1316

1317
	node = ConvertAllocType (struct type_node);
1318
1319

	node->type_node_is_var		= True;
1320
	node->type_node_tv_argument_n = argument_n;
1321
	node->type_node_arity		= 0;
1322
1323
	node->type_node_annotation	= annotation;
	node->type_node_attribute	= attribution;
1324
1325
1326
1327

	return node;
}

clean's avatar
clean committed
1328
BETypeNodeP
1329
BESymbolTypeNode (BEAnnotation annotation, BEAttribution attribution, BESymbolP symbol, BETypeArgP args)
clean's avatar
clean committed
1330
1331
1332
1333
1334
1335
1336
{
	TypeNode	node;

	node	= ConvertAllocType (struct type_node);

	node->type_node_is_var		= False;
	node->type_node_arity		= CountTypeArgs (args);
1337
1338
	node->type_node_annotation	= annotation;
	node->type_node_attribute	= attribution;
clean's avatar
clean committed
1339
1340
1341
	node->type_node_symbol		= symbol;
	node->type_node_arguments	= args;

1342
1343
	return node;
}
clean's avatar
clean committed
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364

BETypeArgP
BENoTypeArgs (void)
{
	return (NULL);
} /* BENoTypeArgs */

BETypeArgP
BETypeArgs (BETypeNodeP node, BETypeArgP nextArgs)
{
	TypeArgs	arg;

	arg	= ConvertAllocType (TypeArg);

	arg->type_arg_node	= node;
	arg->type_arg_next	= nextArgs;

	return (arg);
} /* BETypeArgs */

BETypeAltP
1365
BETypeAlt (BETypeNodeP lhs, BETypeNodeP rhs)
clean's avatar
clean committed
1366
1367
1368
1369
1370
1371
1372
1373
{
	TypeAlt	*alt;

	alt	= ConvertAllocType (struct type_alt);

	alt->type_alt_lhs	= lhs;
	alt->type_alt_rhs	= rhs;

1374
	alt->type_alt_strict_positions	= NULL;
clean's avatar
clean committed
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429

	return (alt);
} /* BETypeAlt */

static Node
GenerateApplyNodes (Node root, int offarity, int demarity)
{
	if (offarity > demarity)
	{
		int		i;
		Args	lastarg, nextarg;
		
		if (demarity != 0)
		{
			for (i=1, lastarg = root->node_arguments; i < demarity; i++)
				lastarg = lastarg->arg_next;
		
			nextarg = lastarg->arg_next;
			lastarg->arg_next = NULL;
		}
		else
		{
			nextarg = root->node_arguments;
			root->node_arguments = NULL;
		}
		root->node_arity = (short) demarity;
		
		for (i=demarity+1; i<=offarity; i++)
		{
			Args	arg;

			arg	= BEArgs (root, nextarg);

   			nextarg	= nextarg->arg_next;
			arg->arg_next->arg_next = NULL;

			root	= BENormalNode (gBasicSymbols [apply_symb], arg);
		}
	}

	return (root);
} /* GenerateApplyNodes */

BENodeP
BENormalNode (BESymbolP symbol, BEArgP args)
{
	NodeP	node;

	node	= ConvertAllocType (NodeS);

	node->node_annotation	= NoAnnot;
	node->node_kind			= NormalNode;
	node->node_symbol		= symbol;
	node->node_arity		= CountArgs (args);
	node->node_arguments	= args;
clean's avatar
clean committed
1430
	node->node_number=0;
clean's avatar
clean committed
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453

	/* +++ hackerdiehack */
	if (symbol->symb_kind == definition)
		node	= GenerateApplyNodes (node, node->node_arity, symbol->symb_def->sdef_arity);

	return (node);
} /* BENormalNode */

BENodeP
BEMatchNode (int arity, BESymbolP symbol, BENodeP node)
{
	NodeP	matchNode;

	if (symbol->symb_kind == tuple_symb)
		return (node);

	matchNode	= ConvertAllocType (NodeS);

	matchNode->node_annotation	= NoAnnot;
	matchNode->node_kind		= MatchNode;
	matchNode->node_symbol		= symbol;
	matchNode->node_arity		= arity;
	matchNode->node_arguments	= BEArgs (node, NULL);
clean's avatar
clean committed
1454
	matchNode->node_number=0;
clean's avatar
clean committed
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481

	return (matchNode);
} /* BEMatchNode */

BENodeP
BETupleSelectNode (int arity, int index, BENodeP node)
{
	SymbolP symbol;
	NodeP	select;

	if ((symbol = gTupleSelectSymbols [arity-1]) == NULL)
	{
		symbol	= ConvertAllocType (SymbolS);
	
		symbol->symb_kind	= select_symb;
		symbol->symb_arity	= arity;

		gTupleSelectSymbols [arity-1]	= symbol;
	}

	select	= ConvertAllocType (NodeS);

	select->node_annotation	= NoAnnot;
	select->node_kind		= NormalNode;
	select->node_symbol		= symbol;
	select->node_arity		= index+1;
	select->node_arguments	= BEArgs (node, NULL);
1482
	select->node_number		= 0;
clean's avatar
clean committed
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496

	return (select);
} /* BETupleSelectNode */

BENodeP
BEIfNode (BENodeP cond, BENodeP then, BENodeP elsje)
{
	NodeP	node;

	node	= ConvertAllocType (NodeS);

	node->node_annotation	= NoAnnot;
	node->node_kind			= NormalNode;
	node->node_symbol		= gBasicSymbols [if_symb];
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1497
	node->node_arguments	= BEArgs (cond, BEArgs (then, BEArgs (elsje, NULL)));
clean's avatar
clean committed
1498
	node->node_arity		= 3;
1499
	node->node_number		= 0;
clean's avatar
clean committed
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521

	return (node);
} /* BEIfNode */

BENodeP
BEGuardNode (BENodeP cond, BENodeDefP thenNodeDefs, BEStrictNodeIdP thenStricts, BENodeP then, BENodeDefP elseNodeDefs, BEStrictNodeIdP elseStricts, BENodeP elsje)
{
	NodeP	node;
	struct if_node_contents *thenElseInfo;

	thenElseInfo = ConvertAllocType (struct if_node_contents);

	thenElseInfo->if_then_node_defs			= thenNodeDefs;
	thenElseInfo->if_then_strict_node_ids	= thenStricts;
	thenElseInfo->if_else_node_defs			= elseNodeDefs;
	thenElseInfo->if_else_strict_node_ids	= elseStricts;

	node	= ConvertAllocType (NodeS);

	node->node_annotation			= NoAnnot;
	node->node_kind					= IfNode;
	node->node_contents.contents_if	= thenElseInfo;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1522
	node->node_arguments			= BEArgs (cond, BEArgs (then, BEArgs (elsje, NULL)));
1523
	node->node_number				= 0;
clean's avatar
clean committed
1524

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
	switch (elsje->node_kind)
	{
		case SwitchNode:
			thenElseInfo->if_else_node_defs			= NULL;
			thenElseInfo->if_else_strict_node_ids	= NULL;
			node->node_arguments->arg_next->arg_next->arg_node
								= BENormalNode (BEBasicSymbol (BEFailSymb), BENoArgs ());

			node	= NewGuardNode (node, elsje, elseNodeDefs, elseStricts);
			break;
		case GuardNode:
			/* move the GuardNode to the top */
			node->node_arguments->arg_next->arg_next->arg_node
								= elsje->node_arguments->arg_node;
			elsje->node_arguments->arg_node	=	node;
			node	= elsje;
			break;
		default:
			break;
	}

clean's avatar
clean committed
1546
1547
1548
	return (node);
} /* BEGuardNode */

1549
1550
1551
1552
1553
1554
1555
1556