statesgen.c 134 KB
Newer Older
clean's avatar
clean committed
1
2
3
4
5
6
7
8
9
10
/*
	 (Concurrent) Clean Compiler:  Generation of states
	 ==================================================

	This module generates the (internal) states that are used during the code generation.

	Authors: Sjaak Smetsers & John van Groningen
	At:		 University of Nijmegen, department of computing science
*/

11
12
#include "compiledefines.h"
#include "types.t"
clean's avatar
clean committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "system.h"
#include "settings.h"
#include "syntaxtr.t"
#include "comsupport.h"
#include "buildtree.h"
#include "codegen_types.h"
#include "codegen1.h"
#include "codegen2.h"
#include "statesgen.h"
#include "checker.h"
#include "instructions.h"
#include "optimisations.h"
#include "sa.h"

/* #include "dbprint.h" */

#define UNBOX_STRICT_CONSTRUCTOR_RECORD
#define UNBOX_STRICT_RECORD_WITH_ONE_FIELD
#define FASTER_STRICT_IF /* also in codegen2.c */
#define FASTER_STRICT_AND_OR
#define UNTUPLE_STRICT_TUPLES /* also in optimisations.c */

#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)
#define for_la(v1,v2,l1,l2,n1) for(v1=(l1),v2=(l2);v1!=NULL;v1=v1->n1,++v2)
#define for_li(v,i,l,n) for(v=(l),i=0;v!=NULL;v=v->n,++i)
#define for_ll(v1,v2,l1,l2,n1,n2) for(v1=(l1),v2=(l2);v1!=NULL;v1=v1->n1,v2=v2->n2)
#define for_lla(v1,v2,v3,l1,l2,a3,n1,n2) for(v1=(l1),v2=(l2),v3=(a3);v1!=NULL;v1=v1->n1,v2=v2->n2,++v3)

static void error_in_function (char *m)
{
	ErrorInCompiler ("statesgen.c",m,"");
}

46
47
48
49
50
static void error_in_function_s (char *m,char *s)
{
	ErrorInCompiler ("statesgen.c",m,s);
}

clean's avatar
clean committed
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 *Elhsannots	  = "annotations are not allowed at this position";
static char *ECodeBlock	  = "missing type specification";
static char *Wtypeannot	  = "only strict annotations in a type rule will be regarded";
static char *Wparannot	  = "parallel annotations ignored in sequential mode";
static char *Wrootannot	  = "root annotations are ignored";
/*
static char *Econflict     = "conflicting parallel annotations";
static char *WEorIoverrule   = "strict annotation overruled by parallel annotation";
static char *WStrictoverrule = "parallel annotation overruled by strict annotation";
static char *Wapplannot = "parallel annotations on implicitly defined nodeids are ignored";
static char *Wnonterm	  = "non-terminating rule specified";
static char *Wunkannot       = "unknown annotation";
*/

/* some routines for setting the state fields of an object */

StateS BasicSymbolStates [Nr_Of_Predef_Types];

int FirstStateIsStricter (StateS offered_state,StateS demanded_state)
{
	if (offered_state.state_type==SimpleState){
		if (IsSimpleState (demanded_state)){
			switch (offered_state.state_kind){
				case OnB:
				case StrictOnA:
				case StrictRedirection:
					return 1;
				case OnA:
				case SemiStrict:
				case LazyRedirection:
				case Parallel:
				case UnderEval:
					return demanded_state.state_kind==OnA;
				default:
					error_in_function ("FirstStateIsStricter");
					return 0;
			}
		} else {
			return 0;
		}
	} else {
		if (IsSimpleState (demanded_state))
			return 1;
		else if (offered_state.state_type==ArrayState && demanded_state.state_type==ArrayState)
			return 1;
96
97
		else if (offered_state.state_type==RecordState && demanded_state.state_type==RecordState)
			return 1;
clean's avatar
clean committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		else
			return 0;
	}
}

#define BETWEEN(l,h,v) ((unsigned)((v)-(l)) <= (unsigned)((h)-(l)))

int FieldArgumentNodeStatesAreStricter (ArgS *offered_args,ArgS *field_args,States record_states)
{
	ArgS *offered_arg,*field_arg;
	
	for_ll (offered_arg,field_arg,offered_args,field_args,arg_next,arg_next){
		int node_kind;
		Node arg_node;
		int field_number;
		
		field_number=field_arg->arg_node->node_symbol->symb_def->sdef_sel_field_number;

		arg_node=offered_arg->arg_node;
		
		node_kind=arg_node->node_kind;
		if (node_kind!=NodeIdNode){
			if (node_kind==NormalNode && (BETWEEN (int_denot,real_denot,arg_node->node_symbol->symb_kind) || arg_node->node_symbol->symb_kind==string_denot))
				;
			else
				if (!FirstStateIsStricter (arg_node->node_state,record_states[field_number]))
					return 0;		
		} else
			if (!FirstStateIsStricter (arg_node->node_node_id->nid_state,record_states[field_number]))
				return 0;
	}

	return 1;
}

void SetUnaryState (StateS *state_p,StateKind kind,ObjectKind object)
{
	state_p->state_arity = 1;
	state_p->state_kind = kind;
	state_p->state_object = object;
	state_p->state_type = SimpleState;
	state_p->state_mark = 0;
}

static void SetTupleState (StateS *state_p,States args,int arity)
{
	state_p->state_arity = arity;
	state_p->state_tuple_arguments = args;
	state_p->state_type = TupleState;
	state_p->state_mark = 0;
}

static void SetUpdateableTupleState (StateS *state_p,States args,int arity)
{
	state_p->state_arity = arity;
	state_p->state_tuple_arguments = args;
	state_p->state_type = TupleState;
	state_p->state_mark = STATE_ELEMENTS_UPDATEABLE_MASK;
}

static void SetRecordState (StateS *state_p,SymbDef record_sdef,int arity)
{
	RecordStateDescr recdesc;
	
	recdesc = (RecordStateDescr) CompAlloc (sizeof (struct record_state_descr) + (arity-1)*sizeof (struct state));
	recdesc->rs_symb = record_sdef;

	state_p->state_arity = arity;
	state_p->state_record_desc = recdesc;
	state_p->state_type = RecordState;
	state_p->state_mark = 0;
}

static void SetUnboxedArrayState (StateS *state_p,States arg)
{
	state_p->state_arity = 0;
	state_p->state_array_arguments = arg;
	state_p->state_type = ArrayState ;
	state_p->state_mark = 0;
}

StateS LazyState,StrictState; 

#define NewArrayOfStates(n)	(States) CompAlloc (sizeof (StateS)*(n))

static States NewArrayOfUnaryStates (int arity, StateKind init)
{
	int	i;
	States argstates;

	argstates = NewArrayOfStates (arity);

	for (i=0; i<arity; i++)
		SetUnaryState (&argstates [i], init, UnknownObj);

	return argstates;
}

void ConvertAnnotationToState (Annotation annot,StateS *state_p)
{
	if (annot==NoAnnot)
		return;
	else if (annot==StrictAnnot){
		*state_p=StrictState;
		return;
	} else {
		SetUnaryState (state_p, DoParallel ? Parallel : OnA, UnknownObj);
		if (DoParallel)
			state_p->state_mark |= STATE_PARALLEL_MASK;
		return;
	}
}

John van Groningen's avatar
John van Groningen committed
211
212
213
214
215
#if 0
# include "dbprint.h"
extern File rules_file;
#endif

clean's avatar
clean committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
static void GenRecordState (SymbDef sdef);

void ConvertTypeToState (TypeNode type,StateS *state_p,StateKind kind)
{
	Symbol symbol;

	symbol=type->type_node_symbol;

	if (symbol->symb_kind < Nr_Of_Predef_Types){
		*state_p = BasicSymbolStates [symbol->symb_kind];
		if (kind!=StrictOnA)
			state_p->state_kind=kind;
	} else if (symbol->symb_kind==definition && symbol->symb_def->sdef_kind==RECORDTYPE){
		if (kind==StrictOnA){
			GenRecordState (symbol->symb_def);
John van Groningen's avatar
John van Groningen committed
231
232
233
234
235
236
#if BOXED_RECORDS
			if (symbol->symb_def->sdef_boxed_record)
				SetUnaryState (state_p,StrictOnA,RecordObj);
			else
#endif
				*state_p=symbol->symb_def->sdef_record_state;
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
		} else
			SetUnaryState (state_p,kind,RecordObj);
	} else
#if ABSTRACT_OBJECT
	if (symbol->symb_kind==definition && symbol->symb_def->sdef_kind==ABSTYPE)
		SetUnaryState (state_p,kind,AbstractObj);
	else
#endif
		SetUnaryState (state_p,kind,UnknownObj);

#ifdef REUSE_UNIQUE_NODES
	if (type->type_node_attribute==UniqueAttr || (symbol->symb_kind==definition && 
			(symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE) &&
			symbol->symb_def->sdef_type->type_lhs->ft_attribute==UniqueAttr))
	{
		state_p->state_mark |= STATE_UNIQUE_MASK;
	}

	if ((state_p->state_mark & STATE_UNIQUE_MASK) && state_p->state_type==SimpleState){
		if (symbol->symb_kind==list_type || symbol->symb_kind==tuple_type || 
			(symbol->symb_kind==definition && (symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE)))
		{
			unsigned long unq_type_args;
			TypeArgs type_arg;
			int i;
			
			unq_type_args=0;
			for_li (type_arg,i,type->type_node_arguments,type_arg_next)
				if (type_arg->type_arg_node->type_node_attribute==UniqueAttr)
					unq_type_args |= 1<<i;
			
			if (unq_type_args!=0){
				state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
				state_p->state_unq_type_args = unq_type_args;
			}
		}
	}
#endif

	if (kind==StrictOnA && IsSimpleState (*state_p)){
		ObjectKind obj_kind;
		
		obj_kind=state_p->state_object;
		if (obj_kind==TupleObj){
281
			int i;
clean's avatar
clean committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
			TypeArgs arg;
			
			SetTupleState (state_p, NewArrayOfStates (type->type_node_arity), type->type_node_arity);

			for_li (arg,i,type->type_node_arguments,type_arg_next){
				TypeNode arg_type_node;
				
				arg_type_node=arg->type_arg_node;

				if (!arg_type_node->type_node_is_var){
					ConvertTypeToState (arg_type_node,&state_p->state_tuple_arguments[i],arg_type_node->type_node_annotation==NoAnnot ? OnA : StrictOnA);
#ifdef UNTUPLE_STRICT_TUPLES_
					arg_type_node->type_node_state=state_p->state_tuple_arguments[i];
#endif
				} else
					state_p->state_tuple_arguments[i] = arg_type_node->type_node_annotation==NoAnnot ? LazyState : StrictState;
			}
		} else if (obj_kind==UnboxedArrayObj || obj_kind==StrictArrayObj || obj_kind==ArrayObj){
			TypeNode element_node;
				
			element_node=type->type_node_arguments->type_arg_node;
			
			state_p->state_arity			= 1;
			state_p->state_array_arguments	= NewArrayOfStates (1);
			state_p->state_type				= ArrayState;
			state_p->state_mark=0;

			switch (obj_kind){
				case ArrayObj:
					SetUnaryState (& state_p->state_array_arguments [0], OnA, UnknownObj);
					break;
				case StrictArrayObj:
					state_p->state_array_arguments [0] = StrictState;
					break;
				case UnboxedArrayObj:
					if (element_node->type_node_is_var)
						state_p->state_array_arguments [0] = StrictState;
					else
						ConvertTypeToState (element_node,&state_p->state_array_arguments [0],StrictOnA);
					state_p->state_mark |= STATE_UNBOXED_ARRAY_MASK;
					break;
			}			
		}
	}
}

static void GenRecordState (SymbDef sdef)
{
	if (sdef->sdef_checkstatus < ConvertingToState){
		Types rectype;
		FieldList fields;
		States fieldstates;
		int i,oldline;
		Symbol oldsymbol;
		FlatType lhs;
		int strict_record;

		rectype = sdef->sdef_type;
		lhs = rectype->type_lhs;
		
		oldline		= CurrentLine;
		oldsymbol	= CurrentSymbol;
		
		CurrentSymbol	= lhs->ft_symbol;
John van Groningen's avatar
John van Groningen committed
346
		CurrentLine		= 0 /*rectype->type_line*/;
clean's avatar
clean committed
347
348

		sdef->sdef_checkstatus = ConvertingToState; /* to detect cyclic strict field dependencies */
349
		SetRecordState (&sdef->sdef_record_state, sdef, sdef->sdef_arity);
clean's avatar
clean committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
		fieldstates=sdef->sdef_record_state.state_record_arguments;

		strict_record=0;
		
		for_li (fields,i,rectype->type_fields,fl_next){
			TypeNode field_type_node;

			field_type_node = fields->fl_type;

			if (field_type_node->type_node_annotation==StrictAnnot){
				strict_record=1;
				if (!field_type_node->type_node_is_var){
					ConvertTypeToState (field_type_node,&fields->fl_state,StrictOnA);
					
					if (fields->fl_state.state_type==RecordState
#ifdef UNBOX_STRICT_RECORD_WITH_ONE_FIELD
						&& !(fields->fl_state.state_arity==1)
#endif
						)
					{
						SetUnaryState (&fieldstates[i], StrictOnA, RecordObj);
#ifdef REUSE_UNIQUE_NODES
						if (field_type_node->type_node_attribute==UniqueAttr)
							fieldstates[i].state_mark |= STATE_UNIQUE_MASK;
#endif	
					} else
						fieldstates[i]=fields->fl_state;
				} else
					fieldstates[i]=fields->fl_state=field_type_node->type_node_annotation==NoAnnot ? LazyState : StrictState;
#ifdef REUSE_UNIQUE_NODES
				if (field_type_node->type_node_attribute==UniqueAttr){
					fieldstates[i].state_mark |= STATE_UNIQUE_MASK;
					fields->fl_state.state_mark |= STATE_UNIQUE_MASK;
				}
#endif	
			} else {
				fieldstates[i] = LazyState;
#ifdef REUSE_UNIQUE_NODES
				if (field_type_node->type_node_attribute==UniqueAttr)
					fieldstates[i].state_mark |= STATE_UNIQUE_MASK;
#endif	
			}

			fields->fl_symbol->symb_def->sdef_sel_field = fields;
		}

		sdef->sdef_strict_constructor=strict_record;
		sdef->sdef_checkstatus = ConvertedToState; /* to detect cyclic strict field dependencies */
				
		CurrentSymbol = oldsymbol;
		CurrentLine = oldline;
	} else if (sdef->sdef_checkstatus == ConvertedToState)
		return;
	else
		StaticMessage (True, "%S", "%s cyclic strict field dependencies are not allowed", CurrentSymbol, sdef->sdef_ident->ident_name);
}

static void GenResultStatesOfLazyFields (SymbDef sdef)
{
	FieldList fields;
	Types rectype;
	int i;

	rectype = sdef->sdef_type;
	
	CurrentSymbol = rectype->type_lhs->ft_symbol;
John van Groningen's avatar
John van Groningen committed
416
	CurrentLine = 0 /*rectype->type_line*/;
clean's avatar
clean committed
417
418
419
	
	for (i=0, fields = rectype->type_fields; fields; i++, fields = fields->fl_next){
		TypeNode field_type_node = fields->fl_type;
420

clean's avatar
clean committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
		if (field_type_node->type_node_annotation!=StrictAnnot){
			if (field_type_node->type_node_is_var || field_type_node->type_node_symbol->symb_kind==apply_symb)
				SetUnaryState (&fields->fl_state, LazyRedirection, UnknownObj);
			else
				ConvertTypeToState (field_type_node,&fields->fl_state,StrictOnA);
		}
	}
}

static void ChangeFieldRecordStateForStrictAbsTypeFields (SymbDef icl_sdef,SymbDef dcl_sdef)
{
	Types icl_type;
	FieldList icl_field;
	StateP icl_fieldstate_p,dcl_fieldstate_p;

	icl_type = icl_sdef->sdef_type;

	CurrentSymbol = icl_type->type_lhs->ft_symbol;
John van Groningen's avatar
John van Groningen committed
439
	CurrentLine = 0 /*icl_type->type_line*/;
clean's avatar
clean committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

	icl_fieldstate_p=icl_sdef->sdef_record_state.state_record_arguments;
	dcl_fieldstate_p=dcl_sdef->sdef_record_state.state_record_arguments;

	for_l (icl_field,icl_type->type_fields,fl_next){
		if (dcl_fieldstate_p->state_type==SimpleState &&
			(icl_fieldstate_p->state_type!=SimpleState ||
			icl_fieldstate_p->state_kind!=dcl_fieldstate_p->state_kind))
		{
			StaticMessage (False, "%S", "%S strict field is boxed because the field type is an abstract type",
				CurrentSymbol, icl_field->fl_symbol);
			
			*icl_fieldstate_p=*dcl_fieldstate_p;
		}
		
		++icl_fieldstate_p;
		++dcl_fieldstate_p;
	}
}

static void GenerateStatesForConstructors (SymbDef sdef)
{
	ConstructorList constructor;

John van Groningen's avatar
John van Groningen committed
464
	CurrentLine	 = 0 /*sdef->sdef_type->type_line*/;
clean's avatar
clean committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

	for_l (constructor,sdef->sdef_type->type_constructors,cl_next){
		int strict_constructor;
		SymbDef constructor_sdef;
		TypeNode type_node;
		TypeArgs arg;
		StateP state_p;

		type_node=constructor->cl_constructor;
		CurrentSymbol=type_node->type_node_symbol;

		constructor_sdef=CurrentSymbol->symb_def;

		state_p = NewArrayOfStates (constructor_sdef->sdef_arity);
		constructor->cl_state_p = state_p;		

		constructor_sdef->sdef_constructor=constructor;

		strict_constructor=0;
	
		for_l (arg,type_node->type_node_arguments,type_arg_next){
			TypeNode type_arg_node;
			
			type_arg_node=arg->type_arg_node;
			
			if (type_arg_node->type_node_annotation==StrictAnnot){	
				strict_constructor=1;
				
				if (!type_arg_node->type_node_is_var){
					ConvertTypeToState (type_arg_node,state_p,StrictOnA);
					
					if (state_p->state_type==RecordState)
#ifdef UNBOX_STRICT_CONSTRUCTOR_RECORD
					if (type_node->type_node_arguments->type_arg_next!=NULL)
#endif
#ifdef UNBOX_STRICT_RECORD_WITH_ONE_FIELD
					if (!(state_p->state_arity==1))
#endif
						SetUnaryState (state_p, StrictOnA, RecordObj);
				} else {
					*state_p=StrictState;
				}
			} else
				*state_p=LazyState;

			++state_p;
		}
		
		constructor_sdef->sdef_strict_constructor=strict_constructor;
	}
}

static void ChangeElementStateForStrictAbsTypeFields (SymbDef icl_sdef,SymbDef dcl_sdef)
{
	Types icl_type = icl_sdef->sdef_type, dcl_type = dcl_sdef->sdef_type;
	ConstructorList icl_cons, dcl_cons;

John van Groningen's avatar
John van Groningen committed
522
	CurrentLine = 0 /*icl_type->type_line*/;
clean's avatar
clean committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569

	for (icl_cons = icl_type->type_constructors, dcl_cons = dcl_type->type_constructors; dcl_cons;
		 icl_cons = icl_cons->cl_next, dcl_cons = dcl_cons->cl_next)
	{
		TypeNode icl_node,dcl_node;
		SymbDef icl_constructor,dcl_constructor;
		
		icl_node=icl_cons->cl_constructor;
		icl_constructor=icl_node->type_node_symbol->symb_def;
		
		if (icl_constructor->sdef_strict_constructor){
			TypeArgs icl_arg,dcl_arg;
			StateP icl_arg_state_p,dcl_arg_state_p;
			
			dcl_node=dcl_cons->cl_constructor;
			CurrentSymbol=dcl_node->type_node_symbol;
			dcl_constructor=CurrentSymbol->symb_def;
					
			icl_arg=icl_node->type_node_arguments;
			dcl_arg=dcl_node->type_node_arguments;
			icl_arg_state_p=icl_cons->cl_state_p;
			dcl_arg_state_p=dcl_cons->cl_state_p;
			
			while (icl_arg!=NULL){
				TypeNode icl_element_node,dcl_element_node;
				
				icl_element_node=icl_arg->type_arg_node;
				dcl_element_node=dcl_arg->type_arg_node;

				if (dcl_arg_state_p->state_type==SimpleState &&
					(icl_arg_state_p->state_type!=SimpleState || icl_arg_state_p->state_kind!=dcl_arg_state_p->state_kind))
				{
					StaticMessage (False, "%S", "%S element is boxed because its type is an abstract type",
						CurrentSymbol, icl_element_node->type_node_symbol);
					
					*icl_arg_state_p=*dcl_arg_state_p;
				}
				
				icl_arg=icl_arg->type_arg_next;
				dcl_arg=dcl_arg->type_arg_next;
				++icl_arg_state_p;
				++dcl_arg_state_p;
			}
		}				
	}
}

570
571
572
573
#ifdef CLEAN2
SymbDefP special_types[2];
#endif

clean's avatar
clean committed
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
603
604
605
606
607
608
609
610
611
612
void GenerateStatesForRecords (Symbol symbols)
{
	Symbol symb;
	
	for_l (symb,symbols,symb_next)
		if (symb->symb_kind==definition){
			SymbDef def;

			def=symb->symb_def;
			if (def->sdef_kind==RECORDTYPE){
				GenRecordState (def);
				GenResultStatesOfLazyFields (def);

				if (def->sdef_exported){
					SymbDef dcl_sdef;
					
					dcl_sdef=def->sdef_dcl_icl;

					if (dcl_sdef!=NULL && dcl_sdef->sdef_kind==RECORDTYPE){
						GenRecordState (dcl_sdef);
						GenResultStatesOfLazyFields (dcl_sdef);
						ChangeFieldRecordStateForStrictAbsTypeFields (def,dcl_sdef);
					}
				}
			} else if (def->sdef_kind==TYPE){
				GenerateStatesForConstructors (def);
				
				if (def->sdef_exported){
					SymbDef dcl_sdef;
					
					dcl_sdef=def->sdef_dcl_icl;
					
					if (dcl_sdef->sdef_kind==TYPE){
						GenerateStatesForConstructors (dcl_sdef);
						ChangeElementStateForStrictAbsTypeFields (def,dcl_sdef);
					}
				}
			}
		}
613
614
615
616
617

#ifdef CLEAN2
	if (special_types[0]!=NULL)
		BasicSymbolStates[integer_denot] = special_types[0]->sdef_record_state;
#endif
clean's avatar
clean committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
}

/*
static Bool AnnotHasDeferAttr (Annotation annotkind)
{
	switch (annotkind){
		case InterleavedAnnot:
		case LazyInterleavedAnnot:
		case ContinueAnnot:
		case DeferAnnot:
		case WaitAnnot:
		case ContInterleavedAnnot:
		case InterleavedNFAnnot:
			return True;
		default:
			return False;
	}
}
*/	

static StateS DetermineStatesOfRuleType (TypeAlts ruletype,StateS *const function_state_p)
{
	TypeNode lhsroot;
	TypeArgs type_arg;
	StateP arg_state_p;

	lhsroot = ruletype->type_alt_lhs;
	
	CurrentSymbol	= lhsroot ->type_node_symbol;
John van Groningen's avatar
John van Groningen committed
647
	CurrentLine = 0 /*ruletype->type_alt_line*/;
clean's avatar
clean committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
	
	if (lhsroot->type_node_annotation!=NoAnnot)
		StaticMessage (False, "%S", Wrootannot, CurrentSymbol);

	arg_state_p=function_state_p;
	for_l (type_arg,lhsroot->type_node_arguments,type_arg_next){
		if (!(type_arg->type_arg_node->type_node_annotation==NoAnnot || type_arg->type_arg_node->type_node_annotation==StrictAnnot))
			StaticMessage (False, "%S", Wtypeannot, CurrentSymbol);
	
		if (!type_arg->type_arg_node->type_node_is_var)
			ConvertTypeToState (type_arg->type_arg_node,arg_state_p,type_arg->type_arg_node->type_node_annotation==NoAnnot ? OnA : StrictOnA);
		else
			*arg_state_p = type_arg->type_arg_node->type_node_annotation==NoAnnot ? LazyState : StrictState;
		
		++arg_state_p;
	}

	if (ruletype->type_alt_rhs->type_node_is_var || ruletype->type_alt_rhs->type_node_symbol->symb_kind==apply_symb){
		function_state_p[-1] = StrictState;
		function_state_p[-1].state_kind = StrictRedirection;
	} else
		ConvertTypeToState (ruletype->type_alt_rhs,&function_state_p[-1],StrictOnA);

	return function_state_p[-1];
}

typedef struct type_node *TypeNodeP;

#ifdef REUSE_UNIQUE_NODES
# ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static void determine_unique_state_of_constructor_argument (StateP result_state_p,StateP type_state_p,TypeNodeP type_arg_node,int lhs_type_attribute)
{
	if (type_arg_node->type_node_is_var){
		if ((type_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
			type_arg_node->type_node_tv->tv_argument_nr>=0 &&
			(type_state_p->state_unq_type_args & (1<<(type_arg_node->type_node_tv->tv_argument_nr))))
		{
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
		}
	} else {
		AttributeKind arg_type_attribute;
		
		arg_type_attribute=type_arg_node->type_node_attribute;
		
		if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute))
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
		
		if ((result_state_p->state_mark & STATE_UNIQUE_MASK) && result_state_p->state_type==SimpleState){
			Symbol symbol;
			
			symbol=type_arg_node->type_node_symbol;

			if (symbol->symb_kind==list_type || symbol->symb_kind==tuple_type || (symbol->symb_kind==definition && 
				(symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE)))
			{
				unsigned long unq_type_args;
				TypeArgs type_arg;
				int i;
				
				unq_type_args=0;
				for_li (type_arg,i,type_arg_node->type_node_arguments,type_arg_next){
					TypeNodeP type_arg_node_p;
					
					type_arg_node_p=type_arg->type_arg_node;
					if (type_arg_node_p->type_node_is_var){
						if ((type_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
							type_arg_node_p->type_node_tv->tv_argument_nr>=0 &&
							(type_state_p->state_unq_type_args & (1<<(type_arg_node_p->type_node_tv->tv_argument_nr))))
						{
							unq_type_args |= 1<<i;
						}
					} else {
						AttributeKind arg_type_attribute;

						arg_type_attribute=type_arg_node_p->type_node_attribute;
						if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute))
							unq_type_args |= 1<<i;
					}
				}
				
				if (unq_type_args!=0){
					result_state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
					result_state_p->state_unq_type_args = unq_type_args;
				}
			}
		}
	}
}
# else
737
738
739
static StateP determine_unique_state_of_constructor_argument
				(StateP type_state_p,TypeNodeP type_arg_node,int lhs_type_attribute,StateP constructor_state_p)
{
clean's avatar
clean committed
740
	if (type_arg_node->type_node_is_var){
741
		if ((constructor_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
clean's avatar
clean committed
742
			type_arg_node->type_node_tv->tv_argument_nr>=0 &&
743
			(constructor_state_p->state_unq_type_args & (1<<(type_arg_node->type_node_tv->tv_argument_nr))) &&
clean's avatar
clean committed
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
			(type_state_p->state_mark & STATE_UNIQUE_MASK)==0)
		{
			StateP result_state_p;
			
			result_state_p=CompAllocType (StateS);
			*result_state_p=*type_state_p;
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
			
			return result_state_p;
		} else
			return type_state_p;
	} else {
		AttributeKind arg_type_attribute;
		StateP result_state_p;
		
		arg_type_attribute=type_arg_node->type_node_attribute;
		
		if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute)){
			result_state_p=CompAllocType (StateS);
			*result_state_p=*type_state_p;
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
			
			type_state_p=result_state_p;
		} else
			result_state_p=NULL;
		
		if ((type_state_p->state_mark & STATE_UNIQUE_MASK) && type_state_p->state_type==SimpleState){
			Symbol symbol;
			
			symbol=type_arg_node->type_node_symbol;

			if (symbol->symb_kind==list_type || symbol->symb_kind==tuple_type || (symbol->symb_kind==definition && 
				(symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE)))
			{
				unsigned long unq_type_args;
				TypeArgs type_arg;
				int i;
				
				unq_type_args=0;
				for_li (type_arg,i,type_arg_node->type_node_arguments,type_arg_next){
					TypeNodeP type_arg_node_p;
					
					type_arg_node_p=type_arg->type_arg_node;
					if (type_arg_node_p->type_node_is_var){
788
						if ((constructor_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
clean's avatar
clean committed
789
							type_arg_node_p->type_node_tv->tv_argument_nr>=0 &&
790
							(constructor_state_p->state_unq_type_args & (1<<(type_arg_node_p->type_node_tv->tv_argument_nr))))
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
						{
							unq_type_args |= 1<<i;
						}
					} else {
						AttributeKind arg_type_attribute;

						arg_type_attribute=type_arg_node_p->type_node_attribute;
						if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute))
							unq_type_args |= 1<<i;
					}
				}
				
				if (unq_type_args!=0){
					if (result_state_p==NULL){
						result_state_p=CompAllocType (StateS);
						*result_state_p=*type_state_p;
					}
					result_state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
					result_state_p->state_unq_type_args = unq_type_args;
					
					return result_state_p;
				}
			}
		}
		
		return type_state_p;
	}
}
# endif
#endif

#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static void GenStatesInLhsSubArguments (Node argnode,StateS states[]);
static void GenStatesInLhsSubArg (Node argnode,StateP arg_state_p);

static void GenStatesInLhsNode (Node node,StateP arg_state_p)
{
	Args arg;

	if (node->node_annotation!=NoAnnot)
		StaticMessage (True, "%S", "%S %s",CurrentSymbol,node->node_symbol, Elhsannots);

	if (node->node_kind==NormalNode){
		Symbol symbol;
		
		symbol=node->node_symbol;
		if (symbol->symb_kind==definition){
			SymbDef sdef;

			sdef=symbol->symb_def;

			if (sdef->sdef_kind==CONSTRUCTOR){
# ifdef REUSE_UNIQUE_NODES
				AttributeKind lhs_type_attribute;
				
				lhs_type_attribute=sdef->sdef_type->type_lhs->ft_attribute;

				if (lhs_type_attribute==UniqueAttr)
					arg_state_p->state_mark |= STATE_UNIQUE_MASK;
				
				if (sdef->sdef_arity==node->node_arity && (arg_state_p->state_mark & STATE_UNIQUE_MASK)){
					if (sdef->sdef_strict_constructor){
						struct type_arg *type_arg_p;
						StateP constructor_arg_state_p;
						ArgS *arg;
						
						for_lla (arg,type_arg_p,constructor_arg_state_p,
								node->node_arguments,sdef->sdef_constructor->cl_constructor->type_node_arguments,symbol->symb_def->sdef_constructor->cl_state_p,
								arg_next,type_arg_next)
						{
							Node arg_node;
							
							arg->arg_state = *constructor_arg_state_p;
							
							determine_unique_state_of_constructor_argument (&arg->arg_state,arg_state_p,type_arg_p->type_arg_node,lhs_type_attribute);
														
							arg_node=arg->arg_node;
							
							if (arg_node->node_kind==NodeIdNode){
								arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
								arg_node=arg_node->node_node_id->nid_node;
								if (!arg_node)
									continue;
							}

							GenStatesInLhsSubArg (arg_node,&arg->arg_state);
						}							
					} else {
						struct type_arg *type_arg_p;
						ArgS *arg;
						
						for_ll (arg,type_arg_p,node->node_arguments,sdef->sdef_constructor->cl_constructor->type_node_arguments,arg_next,type_arg_next){
							Node arg_node;
							
							arg->arg_state=LazyState;							
							
							determine_unique_state_of_constructor_argument (&arg->arg_state,arg_state_p,type_arg_p->type_arg_node,lhs_type_attribute);

							arg_node=arg->arg_node;

							if (arg_node->node_kind==NodeIdNode){
								arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
								arg_node=arg_node->node_node_id->nid_node;
								if (!arg_node)
									continue;
							}
							
							GenStatesInLhsNode (arg_node,&arg->arg_state);
						}						
					}
					return;
				}
# endif				
				if (sdef->sdef_strict_constructor && sdef->sdef_arity==node->node_arity){
					GenStatesInLhsSubArguments (node,symbol->symb_def->sdef_constructor->cl_state_p);
					return;
				}
			} else if (sdef->sdef_kind==RECORDTYPE){
# ifdef REUSE_UNIQUE_NODES
				AttributeKind lhs_type_attribute;
				
				lhs_type_attribute=sdef->sdef_type->type_lhs->ft_attribute;

				if (lhs_type_attribute==UniqueAttr)
					arg_state_p->state_mark |= STATE_UNIQUE_MASK;
				
				if (arg_state_p->state_mark & STATE_UNIQUE_MASK){
					StateP arg_state_p;
					ArgS *arg;
					FieldList field;

					for_lla (arg,field,arg_state_p,node->node_arguments,
							 sdef->sdef_type->type_fields,sdef->sdef_record_state.state_record_arguments,arg_next,fl_next)
					{
						Node arg_node;
						
						arg->arg_state = *arg_state_p;

929
930
						determine_unique_state_of_constructor_argument
							(&arg->arg_state,arg_state_p,field->fl_type,lhs_type_attribute,node_id_state_p);
clean's avatar
clean committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
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
						
						arg_node=arg->arg_node;

						if (arg_node->node_kind==NodeIdNode){
							arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
							arg_node=arg_node->node_node_id->nid_node;
							if (!arg_node)
								continue;
						}

						GenStatesInLhsSubArg (arg_node,&arg->arg_state);
					}							
				} else				
# endif
				GenStatesInLhsSubArguments (node,sdef->sdef_record_state.state_record_arguments);
				return;
			}
		}
# ifdef REUSE_UNIQUE_NODES
		else if (symbol->symb_kind==cons_symb && (arg_state_p->state_mark & STATE_UNIQUE_MASK) && node->node_arity==2){
			Node arg_node;

			arg=node->node_arguments;
			
			arg->arg_state=LazyState;
			if ((arg_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (arg_state_p->state_unq_type_args & 1)){
				arg->arg_state.state_mark |= STATE_UNIQUE_MASK;
			}

			arg_node=arg->arg_node;
			if (arg_node->node_kind==NodeIdNode){
				arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
				arg_node=arg_node->node_node_id->nid_node;
			}
			if (arg_node!=NULL)
				GenStatesInLhsNode (arg_node,&arg->arg_state);

			arg=arg->arg_next;
			
			arg->arg_state=LazyState;
			arg->arg_state.state_mark |= STATE_UNIQUE_MASK;
			if ((arg_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (arg_state_p->state_unq_type_args & 1)){
				arg->arg_state.state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
				arg->arg_state.state_unq_type_args = 1;
			}
			
			arg_node=arg->arg_node;
			if (arg_node->node_kind==NodeIdNode){
				arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
				arg_node=arg_node->node_node_id->nid_node;
			}
			if (arg_node!=NULL)
				GenStatesInLhsNode (arg_node,&arg->arg_state);
		
			return;
		} else if (symbol->symb_kind==tuple_symb && (arg_state_p->state_mark & STATE_UNIQUE_MASK)){
			int i;

			for_li (arg,i,node->node_arguments,arg_next){
				Node arg_node;
				
				arg->arg_state=LazyState;
				if ((arg_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (arg_state_p->state_unq_type_args & (1<<i))){
					arg->arg_state.state_mark |= STATE_UNIQUE_MASK;
				}

				arg_node=arg->arg_node;
				if (arg_node->node_kind==NodeIdNode){
					arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
					arg_node=arg_node->node_node_id->nid_node;
				}
				if (arg_node!=NULL)
					GenStatesInLhsNode (arg_node,&arg->arg_state);
			}
			
			return;
		}
# endif
	}

	for_l (arg,node->node_arguments,arg_next){
		Node arg_node;
		
		arg->arg_state=LazyState;
		
		arg_node=arg->arg_node;

		if (arg_node->node_kind==NodeIdNode){
			arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
			arg_node=arg_node->node_node_id->nid_node;
			if (!arg_node)
				continue;
		}
		
		GenStatesInLhsNode (arg_node,&arg->arg_state);
	}
}

static void GenStatesInLhsSubArg (Node arg_node,StateP arg_state_p)
{
	if (arg_node->node_annotation!=NoAnnot)
		StaticMessage (True, "%S", Elhsannots, CurrentSymbol);

	switch (arg_state_p->state_type){
		case RecordState:
			GenStatesInLhsSubArguments (arg_node,arg_node->node_symbol->symb_def->sdef_record_state.state_record_arguments);
			return;
		case TupleState:
			GenStatesInLhsSubArguments (arg_node,arg_state_p->state_tuple_arguments);
			return;
		default:
			GenStatesInLhsNode (arg_node,arg_state_p);
			return;
	}
}

static void GenStatesInLhsSubArguments (Node node,StateS states[])
{
	StateP arg_state_p;
	ArgS *arg;

	for (arg=node->node_arguments,arg_state_p=states; arg!=NULL; arg=arg->arg_next,++arg_state_p){
		Node arg_node;
		
		arg->arg_state = *arg_state_p;
			
		arg_node=arg->arg_node;

		if (arg_node->node_kind==NodeIdNode){
			arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
			arg_node=arg_node->node_node_id->nid_node;
			if (!arg_node)
				continue;
		}

		GenStatesInLhsSubArg (arg_node,&arg->arg_state);
	}	
}

static void DetermineLhsStatesOfRule (ImpRules rule)
{
	RuleAlts alt;
	StateP function_state_p;
	
	function_state_p=rule->rule_state_p;
	
	CurrentSymbol = rule->rule_root->node_symbol;
	
	for_l (alt,rule->rule_alts,alt_next){
		CurrentLine = alt->alt_line;
		
		GenStatesInLhsSubArguments (alt->alt_lhs_root,function_state_p);

		alt->alt_lhs_root->node_state = function_state_p[-1]; /* i.e. the result kind */
	}
}
#endif

unsigned next_def_number;

void ExamineTypesAndLhsOfImpRuleSymbolDefinitionAgain (SymbDef def)
{
	StateS rootstate;

	rootstate = DetermineStatesOfRuleType (def->sdef_rule->rule_type,def->sdef_rule->rule_state_p);

#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
	DetermineLhsStatesOfRule (def->sdef_rule);
#endif

John van Groningen's avatar
John van Groningen committed
1101
	if (def->sdef_exported && def->sdef_dcl_icl!=NULL)
clean's avatar
clean committed
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
		rootstate=def->sdef_dcl_icl->sdef_rule_type->rule_type_state_p[-1];

	if (IsSimpleState (rootstate)){
		if (rootstate.state_kind == OnA || rootstate.state_kind == StrictOnA){
			def->sdef_calledwithrootnode = True;
			def->sdef_returnsnode = True;
		} else if (rootstate.state_kind == StrictRedirection){
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = True;
		} else {
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = False;
		}
	} else {
		def->sdef_calledwithrootnode = False;
		def->sdef_returnsnode = False;
	}
}

#define allocate_function_state(arity) (((StateP)(CompAlloc (sizeof(StateS)*((arity)+1))))+1)

void ExamineTypesAndLhsOfSymbolDefinition (SymbDef def)
{
	StateS rootstate;

	def->sdef_number = 0;

	if (def->sdef_exported && def->sdef_dcl_icl!=def)
		def->sdef_mark |= SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK;

	switch (def->sdef_kind){
		case SYSRULE:
			def->sdef_ident->ident_symbol = NULL;
			def->sdef_ident->ident_environ = CurrentModule;
		case DEFRULE:
			if (def->sdef_isused){
				def->sdef_rule_type->rule_type_state_p = allocate_function_state (def->sdef_arity);
				rootstate = DetermineStatesOfRuleType (def->sdef_rule_type->rule_type_rule,def->sdef_rule_type->rule_type_state_p);
			} else
				return;
			break;
		case IMPRULE:
			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;

			def->sdef_rule->rule_state_p = allocate_function_state (def->sdef_arity);
			rootstate = DetermineStatesOfRuleType (def->sdef_rule->rule_type,def->sdef_rule->rule_state_p);
John van Groningen's avatar
John van Groningen committed
1149
			
John van Groningen's avatar
John van Groningen committed
1150
			if (def->sdef_exported && def->sdef_dcl_icl!=NULL){
clean's avatar
clean committed
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
				def->sdef_dcl_icl->sdef_rule_type->rule_type_state_p = allocate_function_state (def->sdef_arity);
				rootstate = DetermineStatesOfRuleType (def->sdef_dcl_icl->sdef_rule_type->rule_type_rule,def->sdef_dcl_icl->sdef_rule_type->rule_type_state_p);
			}
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
			DetermineLhsStatesOfRule (def->sdef_rule);
#endif
			break;
		case RECORDTYPE:
		{
			FieldList fields;

			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			for_l (fields,def->sdef_type->type_fields,fl_next)
				ExamineTypesAndLhsOfSymbolDefinition (fields->fl_symbol->symb_def);

1167
1168
1169
1170
1171
1172
			if (def->sdef_boxed_record){
				def->sdef_calledwithrootnode = True;
				def->sdef_returnsnode = True;
				return;
			}

clean's avatar
clean committed
1173
1174
1175
1176
			rootstate = def->sdef_record_state;
			break;
		}
		case FIELDSELECTOR:
1177
1178
1179
1180
1181
			if (def->sdef_mark & SDEF_FIELD_HAS_MEMBER_TYPE){
				def->sdef_member_states_of_field = allocate_function_state (def->sdef_member_type_of_field->type_alt_lhs->type_node_arity);
				(void) DetermineStatesOfRuleType (def->sdef_member_type_of_field,def->sdef_member_states_of_field);
			}

clean's avatar
clean committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
			rootstate = def->sdef_sel_field->fl_state;

			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			if (def->sdef_exported && def->sdef_dcl_icl!=NULL && def->sdef_dcl_icl->sdef_sel_field)
				rootstate = def->sdef_dcl_icl->sdef_sel_field->fl_state;
			break;
		case TYPE:
			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			rootstate = LazyState;
			break;
		case CONSTRUCTOR:
			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			rootstate = OnAState;
			break;
		default:
			rootstate = OnAState;
			break;
	}

	if (IsSimpleState (rootstate)){
		if (rootstate.state_kind == OnA || rootstate.state_kind == StrictOnA){
			def->sdef_calledwithrootnode = True;
			def->sdef_returnsnode = True;
		} else if (rootstate.state_kind == StrictRedirection){
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = True;
		} else {
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = False;
		}
	} else {
		def->sdef_calledwithrootnode = False;
		def->sdef_returnsnode = False;
	}
}

1221
1222
1223
1224
#if STRICT_LISTS
extern PolyList unboxed_record_cons_list,unboxed_record_decons_list;
#endif

clean's avatar
clean committed
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
void ExamineTypesAndLhsOfSymbols (Symbol symbs)
{
	next_def_number = 1;

	while (symbs!=NULL){
		if (symbs->symb_kind==definition)
			ExamineTypesAndLhsOfSymbolDefinition (symbs->symb_def);

		symbs=symbs->symb_next;
	}
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
#if STRICT_LISTS
	{
		PolyList unboxed_record_cons_elem,unboxed_record_decons_elem;
		
		for_l (unboxed_record_cons_elem,unboxed_record_cons_list,pl_next)
			ExamineTypesAndLhsOfSymbolDefinition (unboxed_record_cons_elem->pl_elem);
		for_l (unboxed_record_decons_elem,unboxed_record_decons_list,pl_next)
			ExamineTypesAndLhsOfSymbolDefinition (unboxed_record_decons_elem->pl_elem);
	}
#endif
clean's avatar
clean committed
1245
1246
}

1247
PolyList UserDefinedArrayFunctions;
clean's avatar
clean committed
1248

1249
1250
char *current_imported_module; /* also used by instructions.c */

clean's avatar
clean committed
1251
1252
1253
1254
void ImportSymbols (Symbol symbols)
{
	Symbol symbol;	
	PolyList array_fun;
1255
1256

	current_imported_module = NULL;
clean's avatar
clean committed
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
	
	for_l (array_fun,UserDefinedArrayFunctions,pl_next){
		SymbDef fun_def;
		
		fun_def = ((Symbol) array_fun->pl_elem)->symb_def;
		
		if (fun_def ->sdef_mark & (SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK))
			fun_def -> sdef_module = CurrentModule;
	}

	for_l (symbol,symbols,symb_next){
		SymbDef sdef;

1270
		if (symbol->symb_kind!=definition)
clean's avatar
clean committed
1271
1272
			continue;

1273
		sdef=symbol->symb_def;
clean's avatar
clean committed
1274
		if (sdef->sdef_module!=CurrentModule){
1275
1276
1277
1278
1279
1280
1281
			if (sdef->sdef_isused
				&& sdef->sdef_mark & (SDEF_USED_STRICTLY_MASK | SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK)
			){
				if (sdef->sdef_module!=current_imported_module){
					current_imported_module=sdef->sdef_module;
					GenImpMod (current_imported_module);
				}
clean's avatar
clean committed
1282
				GenImport (sdef);
1283
			}
clean's avatar
clean committed
1284
1285
1286
1287
1288
1289
1290
1291
1292

			if (sdef->sdef_kind==RECORDTYPE){
				FieldList fields;
	
				for_l (fields,sdef->sdef_type->type_fields,fl_next){
					SymbDef field_sdef;

					field_sdef=fields->fl_symbol->symb_def;

1293
1294
1295
1296
1297
					if (field_sdef->sdef_isused && field_sdef->sdef_mark & SDEF_USED_LAZILY_MASK){
						if (sdef->sdef_module!=current_imported_module){
							current_imported_module=sdef->sdef_module;
							GenImpMod (current_imported_module);
						}
clean's avatar
clean committed
1298
						GenImport (field_sdef);
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
					}
				}
			}
		}
	}
}

void import_not_yet_imported_record_r_labels (Symbol symbols)
{
	Symbol symbol;	
	
	for_l (symbol,symbols,symb_next){
		if (symbol->symb_kind==definition){
			SymbDef sdef;

			sdef=symbol->symb_def;
			if ((sdef->sdef_mark & (SDEF_USED_STRICTLY_MASK | SDEF_RECORD_R_LABEL_IMPORTED_MASK))==SDEF_USED_STRICTLY_MASK
				&& sdef->sdef_kind==RECORDTYPE && sdef->sdef_module!=CurrentModule)
			{
				if (sdef->sdef_module!=current_imported_module){
					current_imported_module=sdef->sdef_module;
					GenImpMod (current_imported_module);
clean's avatar
clean committed
1321
				}
1322
				GenImport (sdef);
clean's avatar
clean committed
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
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
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
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
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
			}
		}
	}
}

static Bool ShouldDecrRefCount;

#if OPTIMIZE_LAZY_TUPLE_RECURSION
# define IF_OPTIMIZE_LAZY_TUPLE_RECURSION(a) ,a
#else
# define IF_OPTIMIZE_LAZY_TUPLE_RECURSION(a)
#endif

#if OPTIMIZE_LAZY_TUPLE_RECURSION
static int roots_are_tuples_or_calls_to_this_function (NodeP node_p,NodeDefP node_defs,SymbDef function_sdef_p)
{
	switch (node_p->node_kind){
		case SwitchNode:
		{
			ArgP arg_p;
			
			for_l (arg_p,node_p->node_arguments,arg_next)
				if (!roots_are_tuples_or_calls_to_this_function (arg_p->arg_node->node_arguments->arg_node,arg_p->arg_node->node_node_defs,function_sdef_p))
					return 0;
			
			return 1;
		}
		case PushNode:
			return roots_are_tuples_or_calls_to_this_function (node_p->node_arguments->arg_next->arg_node,node_defs,function_sdef_p);
		case GuardNode:
		{			
			while (node_p->node_kind==GuardNode){
				if (!roots_are_tuples_or_calls_to_this_function (node_p->node_arguments->arg_node,node_defs,function_sdef_p))
					return 0;
		
				node_defs=node_p->node_node_defs;
				node_p=node_p->node_arguments->arg_next->arg_node;
			}

			return roots_are_tuples_or_calls_to_this_function (node_p,node_defs,function_sdef_p);
		}
		case IfNode:
		{
			ArgP then_arg_p;
			NodeP else_node_p;
			
			then_arg_p=node_p->node_arguments->arg_next;
			
			if (!roots_are_tuples_or_calls_to_this_function (then_arg_p->arg_node,node_p->node_then_node_defs,function_sdef_p))
				return 0;
			
			else_node_p=then_arg_p->arg_next->arg_node;

			if (else_node_p->node_kind==NormalNode && else_node_p->node_symbol->symb_kind==fail_symb)
				return 1;

			return roots_are_tuples_or_calls_to_this_function (else_node_p,node_p->node_else_node_defs,function_sdef_p);
		}
		default:
		{
			if (node_p->node_kind==NormalNode){
				SymbolP node_symbol_p;
				
				node_symbol_p=node_p->node_symbol;
				if	(node_symbol_p->symb_kind==tuple_symb)
					return 1;
				else if (node_symbol_p->symb_kind==definition && node_symbol_p->symb_def==function_sdef_p
						&& node_p->node_arity==node_symbol_p->symb_def->sdef_arity)
					return 1;
			}
		}
	}
		
	return 0;
}
#endif

static void DecrRefCountCopiesOfArgs (Args args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(int local_scope));

static void DecrRefCountCopiesOfArg (Args arg IF_OPTIMIZE_LAZY_TUPLE_RECURSION(int local_scope))
{
	Node node;
	
	node=arg->arg_node;
	
	if (node->node_kind!=NodeIdNode){
#if OPTIMIZE_LAZY_TUPLE_RECURSION
		if (OptimizeLazyTupleRecursion && node->node_kind==NormalNode && node->node_symbol->symb_kind==select_symb && node->node_arguments->arg_node->node_kind==NodeIdNode
			&& node->node_arguments->arg_node->node_node_id->nid_scope==local_scope
		){
			NodeId node_id;
		
			node_id=node->node_arguments->arg_node->node_node_id;
			
			if (node_id->nid_mark2 & NID_HAS_LAZY_SELECTOR_COUNTER){				
				++node_id->nid_lazy_selector_ref_count;

				if (node_id->nid_lazy_selector_ref_count==node_id->nid_refcount){
					NodeP node_id_def_node;
					
					node_id_def_node=node_id->nid_node_def->def_node;
					if (node_id_def_node->node_kind==NormalNode && node_id_def_node->node_symbol->symb_kind==definition
						&& node_id_def_node->node_symbol->symb_def->sdef_kind==IMPRULE && IsLazyState (node_id_def_node->node_state)
						&& node_id_def_node->node_symbol==CurrentSymbol
					){
						SymbDef function_sdef_p;
						RuleAltP rule_alt_p;
						
						function_sdef_p=node_id_def_node->node_symbol->symb_def;
						rule_alt_p=function_sdef_p->sdef_rule->rule_alts;
						
						if (roots_are_tuples_or_calls_to_this_function (rule_alt_p->alt_rhs_root,rule_alt_p->alt_rhs_defs,function_sdef_p)){
							node_id->nid_node_def->def_id->nid_mark2 |= NID_CALL_VIA_LAZY_SELECTIONS_ONLY;
							node_id_def_node->node_symbol->symb_def->sdef_rule->rule_mark |= RULE_CALL_VIA_LAZY_SELECTIONS_ONLY;
							if (ListOptimizations)
								printf ("Optimize lazy tuple recursion of %s\n",node_id_def_node->node_symbol->symb_def->sdef_ident->ident_name);
						}
					}
				}
			} else {
				node_id->nid_mark2 |= NID_HAS_LAZY_SELECTOR_COUNTER;
				node_id->nid_lazy_selector_ref_count=1;
			}
			
			if (node_id->nid_ref_count_copy>0 && node_id->nid_node_def)
				--node_id->nid_ref_count_copy__;
		} else
#endif
		DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
	} else {
		NodeId node_id;
		
		node_id=node->node_node_id;
		if (node_id->nid_ref_count_copy>0 && node_id->nid_node_def)
			--node_id->nid_ref_count_copy__;
	}
}

static void DecrRefCountCopiesOfArgs (Args args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(int local_scope))
{
	for (; args; args = args->arg_next)
		DecrRefCountCopiesOfArg (args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
}

static StateS *RemoveUndefinedsFromTupleState (StateS *state_p,int arity)
{
	int n;
	StateS *element_state_p,*new_element_states;

	element_state_p=state_p;
		
	new_element_states=NULL;
		
	for (n=0; n<arity; ++n, ++element_state_p){
		if (IsSimpleState (*element_state_p)){
			if (element_state_p->state_kind==Undefined){
				if (new_element_states==NULL){
					StateS *new_element_state_p,*old_element_state_p;
					int i;
					
					new_element_states=NewArrayOfStates (arity);
					element_state_p=&new_element_states[n];
					
					new_element_state_p=new_element_states;
					old_element_state_p=state_p;
					for (i=0; i<arity; ++i)
						*new_element_state_p++ = *old_element_state_p++;
				}
				element_state_p->state_kind=OnA;
			}
		} else if (element_state_p->state_type==TupleState){
			StateS *new_element_states_2;
			
			new_element_states_2=RemoveUndefinedsFromTupleState
				(element_state_p->state_tuple_arguments,element_state_p->state_arity);
			if (new_element_states_2){
				if (new_element_states==NULL){
					StateS *new_element_state_p,*old_element_state_p;
					int i;
					
					new_element_states=NewArrayOfStates (arity);
					element_state_p=&new_element_states[n];
					
					new_element_state_p=new_element_states;
					old_element_state_p=state_p;
					for (i=0; i<arity; ++i)
						*new_element_state_p++ = *old_element_state_p++;
				}
				element_state_p->state_tuple_arguments=new_element_states_2;				
			}
		}
	}

	return new_element_states;
}

static Bool ChangeState (StateS *old_state_p,StateS newstate)
{
	switch (old_state_p->state_kind){
		case OnA:
		case StrictOnA:		
			*old_state_p = newstate;
			return True;
		case Undefined:
			*old_state_p = newstate;
			return False;
		default:
			return False;
	}
}

static Bool AdjustState (StateS *old_state_p, StateS newstate)
{
	if (IsSimpleState (newstate)){
		if (IsSimpleState (*old_state_p)){
			switch (newstate.state_kind){
				case StrictOnA:
				case OnB:
					return ChangeState (old_state_p, newstate);
				default:
					return False;
			}
		} else
			return False;
	} else if (IsSimpleState (*old_state_p)){
		if (newstate.state_type==TupleState && 
			(old_state_p->state_kind==OnA || old_state_p->state_kind==StrictOnA || old_state_p->state_kind==Undefined))
		{
			StateS *element_states;
		
			element_states=RemoveUndefinedsFromTupleState (newstate.state_tuple_arguments,newstate.state_arity);
			
			*old_state_p=newstate;
			
			if (element_states){
/*				CheckWarning ("undefined state in tuple state removed",NULL); */
				old_state_p->state_tuple_arguments=element_states;
			}
			
			return old_state_p->state_kind!=Undefined;
		}

		return ChangeState (old_state_p,newstate);
	} else if (newstate.state_type==TupleState){
		int i, arity;
		StateS argstate;
		Bool new_arg_states;

		arity = old_state_p->state_arity;
		new_arg_states = False;

		Assume (newstate.state_arity == arity, "statesgen", "AdjustState");

		for (i=0; i<arity; i++){
			argstate = old_state_p->state_tuple_arguments[i];
			if (AdjustState (& argstate, newstate.state_tuple_arguments[i]) && ! new_arg_states){
				int j;
				States argstates;
			
				new_arg_states = True;
				argstates = NewArrayOfStates (arity);
				for (j=0; j<arity; j++)
					argstates[j] = old_state_p->state_tuple_arguments[j];
				old_state_p->state_tuple_arguments = argstates;
			}
			old_state_p->state_tuple_arguments[i] = argstate;
		}
		return new_arg_states;
	} else
		return False;
}

1595
static void DetermineStateOfThenOrElse (Args t_or_e_args, NodeDefs *t_or_e_defs, StateS demstate,int local_scope)
clean's avatar
clean committed
1596
1597
1598
1599
1600
{
	Node node;
	
	node=t_or_e_args->arg_node;
	
1601
	if (node->node_kind==NodeIdNode && *t_or_e_defs==NULL){
clean's avatar
clean committed
1602
1603
1604
1605
1606
		NodeId node_id;
		
		node_id=node->node_node_id;
		if (node_id->nid_ref_count_copy>=0)
			--node_id->nid_ref_count_copy__;	
John van Groningen's avatar
John van Groningen committed
1607
1608
1609
1610
	} else {
#if 0
		FPrintF (rules_file,"DetermineStateOfThenOrElse %d\n",local_scope);
#endif
clean's avatar
clean committed
1611
		DetermineStatesOfRootNodeAndDefs (node,t_or_e_defs,demstate, local_scope);
John van Groningen's avatar
John van Groningen committed
1612
	}
clean's avatar
clean committed
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645

	AdjustState (&t_or_e_args->arg_state,demstate);
}

static void DecrementRefCountCopy (NodeId nid)
{ 
	if (nid->nid_ref_count_copy>0)
		--nid->nid_ref_count_copy__;
}

static Bool AdjustStateOfSharedNode (NodeId nid, StateS demstate, int local_scope)
{
	/*
		Note that if the reference count of a node identifier smaller than one means that the corresponding node 
		has already been treated. In that case it would be dangerous to adjust the node state
	*/
   	
	if (nid->nid_ref_count_copy>=0){
		NodeDefs nodedef;

		nodedef = nid->nid_node_def;

		if (nodedef!=NULL){
			int node_id_scope;
			
			if (ShouldDecrRefCount)
				DecrementRefCountCopy (nid);

			node_id_scope=nid->nid_scope;
			if (node_id_scope<0)
				node_id_scope=-node_id_scope;

#if 0
John van Groningen's avatar
John van Groningen committed
1646
1647
1648
			FPrintF (rules_file,"AdjustStateOfSharedNode ");
			DPrintNodeId (nid,rules_file);
			FPrintF (rules_file," %d %d\n",node_id_scope,local_scope);
clean's avatar
clean committed
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
#endif

			if (node_id_scope>=local_scope){
				Node argnode;
				
				argnode = nodedef->def_node;
				if (nid->nid_mark & ON_A_CYCLE_MASK)
					AdjustState (&argnode->node_state, StrictState);
				else
					AdjustState (&argnode->node_state, demstate);
			}

			if (nodedef->def_node)
				return (nodedef->def_node->node_state.state_mark & STATE_PARALLEL_MASK)!=0;
		}
#if 0
		else {
			printf ("AdjustStateOfSharedNode nid_node_def=NULL ");
			DPrintNodeId (nid,StdOut);			
			printf (" %d %d\n",nid->nid_scope,local_scope);
		}
#endif
	}

	return False;
}

static Bool ArgInAStrictContext (Args arg, StateS demstate, Bool semistrict, int local_scope);

static Bool DetermineStrictArgContext (Args arg, StateS demstate, int local_scope)
{
	AdjustState (&arg->arg_state, demstate);
	
	return ArgInAStrictContext (arg, arg->arg_state, False, local_scope);
}

static StateP GetStateOfArguments (SymbDef sdef,int actual_arity)
{
	int symbol_arity;
	StateP state_p;
	
	switch (sdef->sdef_kind){
		case DEFRULE:
		case SYSRULE:
			state_p=sdef->sdef_rule_type->rule_type_state_p;
			symbol_arity = sdef->sdef_arity;
			break;
		case IMPRULE:
			state_p=sdef->sdef_rule->rule_state_p;
			symbol_arity = sdef->sdef_arity;
			break;
		case RECORDTYPE:
			state_p=sdef->sdef_record_state.state_record_arguments;
1702
			symbol_arity = sdef->sdef_arity;
clean's avatar
clean committed
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
			break;
		default:
			return NULL;
	}
			
	if (symbol_arity==actual_arity)
		return state_p;
	else
		return NULL;
}

static Bool ArgsInAStrictContext (StateP arg_state_p,Args argn, int local_scope)
{
	Bool parallel;
	
	parallel = False;

	for (; argn!=NULL; argn=argn->arg_next){	
		if (! IsLazyState (*arg_state_p)){
			if (DetermineStrictArgContext (argn,*arg_state_p,local_scope))
				parallel = True;
		} else if (ShouldDecrRefCount)
			DecrRefCountCopiesOfArg (argn IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
		
		++arg_state_p;
	}

	return parallel;
}

#if DESTRUCTIVE_RECORD_UPDATES
	static Bool UpdateNodeInAStrictOrSemiStrictContext (Node node,StateP demanded_state_p,int local_scope,Bool semi_strict)
#else
	static Bool UpdateNodeInAStrictOrSemiStrictContext (Node node,int local_scope,Bool semi_strict)
#endif
{
	Bool parallel;
	SymbDef record_sdef;
	int type_arg_number;
	ArgS *arg;
	StateP record_arg_states;

	parallel=False;
	
	record_sdef=node->node_symbol->symb_def;
	
	if (!semi_strict){
#if DESTRUCTIVE_RECORD_UPDATES
		if (demanded_state_p->state_type==SimpleState && 
John van Groningen's avatar
John van Groningen committed
1752
1753
1754
			demanded_state_p->state_kind==StrictOnA
/*			 && demanded_state_p->state_object==RecordObj */
			)
clean's avatar
clean committed
1755
1756
		{
			node->node_state = *demanded_state_p;
John van Groningen's avatar
John van Groningen committed
1757
			node->node_state.state_object = RecordObj;
clean's avatar
clean committed
1758
1759
1760
1761
1762
1763
1764
		} else
#endif	
		node->node_state = record_sdef->sdef_record_state;
	}

	arg=node->node_arguments;
	
John van Groningen's avatar
John van Groningen committed
1765
1766
1767
1768
1769
1770
1771
1772
	if (semi_strict){
		if (ArgInAStrictContext (arg,StrictState,True,local_scope))
			parallel = True;
	} else {

#if BOXED_RECORDS
		if (record_sdef->sdef_boxed_record){
			StateS boxed_record_state;
1773
			
John van Groningen's avatar
John van Groningen committed
1774
1775
1776
1777
1778
1779
1780
			SetUnaryState (&boxed_record_state,StrictOnA,RecordObj);
			if (DetermineStrictArgContext (arg, boxed_record_state,local_scope))
				parallel = True;			
		} else
#endif

		if (DetermineStrictArgContext (arg, record_sdef->sdef_record_state,local_scope))
clean's avatar
clean committed
1781
			parallel = True;
John van Groningen's avatar
John van Groningen committed
1782
	}
clean's avatar
clean committed
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
	
	type_arg_number=0;

	record_arg_states=record_sdef->sdef_record_state.state_record_arguments;
	
	while ((arg=arg->arg_next)!=NULL){
		int selector_number;
		Node selector_node;
			
		selector_node=arg->arg_node;
		selector_number=selector_node->node_symbol->symb_def->sdef_sel_field_number;
1794
1795
1796
1797
1798

#if 1
		type_arg_number=selector_number;
#else
		/* Codewarrior 6 optimizer bug */
clean's avatar
clean committed
1799
1800
1801
		while (type_arg_number!=selector_number){
			++type_arg_number;
		}
1802
1803
#endif

clean's avatar
clean committed
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
		if (!IsLazyState (record_arg_states[type_arg_number])){
			if (semi_strict
				? ArgInAStrictContext (selector_node->node_arguments,StrictState,True,local_scope)
				: DetermineStrictArgContext (selector_node->node_arguments,record_arg_states[type_arg_number],local_scope))
				parallel = True;
		} else if (ShouldDecrRefCount)
			DecrRefCountCopiesOfArg (selector_node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
	}
	
	if (parallel)
		node->node_state.state_mark |= STATE_PARALLEL_MASK;
	
	return parallel;
}

int optimise_strict_tuple_result_functions;

1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
static Bool MemberCallInAStrictContext (Node node,int arg_n,unsigned int arg_strictness,int local_scope)
{
	struct arg *arg1_p,*arg2_p;
	struct node *arg1_of_apply_node_p;
	Bool parallel;
	
	parallel=False;
	
	node->node_state = StrictState;
	node->node_state.state_kind = StrictRedirection;

	arg1_p = node->node_arguments;
	arg1_of_apply_node_p=arg1_p->arg_node;
	--arg_n;
	arg2_p = arg1_p->arg_next;

	if (arg1_of_apply_node_p->node_arity==2){ /* node_arity of SelectorNode == 1 */
		/* arg 1 is apply node */

		AdjustState (&arg1_p->arg_state, StrictState);
		parallel = MemberCallInAStrictContext (arg1_of_apply_node_p,arg_n,arg_strictness,local_scope);
	} else {
		/* arg 1 is SelectorNode */
		parallel = DetermineStrictArgContext (arg1_p, StrictState, local_scope);
	}

	if ((arg_strictness & (1<<arg_n)) &&
		(arg2_p->arg_node->node_kind!=NodeIdNode ||
		 (arg2_p->arg_node->node_node_id->nid_ref_count_copy>=0 && arg2_p->arg_node->node_node_id->nid_node_def)))
	{
		if (DetermineStrictArgContext (arg2_p, StrictState, local_scope))
			parallel = True;
	} else
		if (ShouldDecrRefCount)
			DecrRefCountCopiesOfArg (arg2_p IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
	
	if (parallel)
		node->node_state.state_mark |= STATE_PARALLEL_MASK;

	return parallel;	
}

1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
static Bool OptimizedMemberCallInAStrictContext (Node node,int arg_n,struct state *member_states_of_field,int local_scope)
{
	struct arg *arg1_p,*arg2_p;
	struct node *arg1_of_apply_node_p;
	Bool parallel;
	
	parallel=False;
	
	arg1_p = node->node_arguments;
	arg1_of_apply_node_p=arg1_p->arg_node;
	--arg_n;
	arg2_p = arg1_p->arg_next;

	if (arg1_of_apply_node_p->node_arity==2){ /* node_arity of SelectorNode == 1 */
		/* arg 1 is apply node */

		AdjustState (&arg1_p->arg_state, StrictState);

		arg1_of_apply_node_p->node_state = StrictState;
		arg1_of_apply_node_p->node_state.state_kind = StrictRedirection;

		parallel = OptimizedMemberCallInAStrictContext (arg1_of_apply_node_p,arg_n,member_states_of_field,local_scope);
	} else {
		/* arg 1 is SelectorNode */
		parallel = DetermineStrictArgContext (arg1_p, StrictState, local_scope);
	}

	if (! IsLazyState (member_states_of_field[arg_n+1])){
		if (DetermineStrictArgContext (arg2_p, member_states_of_field[arg_n+1], local_scope))
			parallel = True;
	} else
		if (ShouldDecrRefCount)
			DecrRefCountCopiesOfArg (arg2_p IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
	
	if (parallel)
		node->node_state.state_mark |= STATE_PARALLEL_MASK;

	return parallel;
}

clean's avatar
clean committed
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
static Bool NodeInAStrictContext (Node node,StateS demanded_state,int local_scope)
{
	Bool parallel;
	
	parallel=False;

	if (node->node_kind==NormalNode){
		Symbol rootsymb;
		
		rootsymb = node->node_symbol;
		switch (rootsymb->symb_kind){
			case cons_symb:
1915
1916
#if STRICT_LISTS
				if (node->node_arity==2){
1917
1918
1919
1920
1921
					if (rootsymb->symb_head_strictness>1)
						if (rootsymb->symb_head_strictness==4)
							parallel = DetermineStrictArgContext (node->node_arguments,*rootsymb->symb_unboxed_cons_state_p,local_scope);
						else
							parallel = DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);
1922
					if (rootsymb->symb_tail_strictness)
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
						if (DetermineStrictArgContext (node->node_arguments->arg_next,StrictState,local_scope))
							parallel = True;

					if (ShouldDecrRefCount){
						if (!(rootsymb->symb_head_strictness>1))
							DecrRefCountCopiesOfArg (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
						if (!rootsymb->symb_tail_strictness)
							DecrRefCountCopiesOfArg (node->node_arguments->arg_next IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
					}
				} else
1933
#endif
clean's avatar
clean committed
1934
1935
				if (ShouldDecrRefCount)
					DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
1936
1937
1938

				SetUnaryState (&node->node_state, StrictOnA, ListObj);
				break;
clean's avatar
clean committed
1939
			case nil_symb:
1940
1941
1942
1943
1944
#if STRICT_LISTS
				if (rootsymb->symb_head_strictness & 1)
					parallel = DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);
#endif

clean's avatar
clean committed
1945
1946
1947
				SetUnaryState (&node->node_state, StrictOnA, ListObj);
				break;
			case apply_symb:
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
				if (node->node_symbol->symb_instance_apply){ /* if set by optimisations.c */
					struct symbol_def *field_sdef;
					struct type_alt *member_type_alt;
					int arg_n;
					unsigned int arg_strictness;
					struct type_arg *type_arg_p;
					
					field_sdef = (struct symbol_def *)node->node_symbol->symb_next;
					member_type_alt=field_sdef->sdef_member_type_of_field;

					if (OptimizeInstanceCalls){
						arg_n=member_type_alt->type_alt_lhs->type_node_arity-1;
						node->node_state = field_sdef->sdef_member_states_of_field[-1];
						return OptimizedMemberCallInAStrictContext (node,arg_n,field_sdef->sdef_member_states_of_field,local_scope);
					}

					arg_strictness=0;
					arg_n=0;
					for_l (type_arg_p,member_type_alt->type_alt_lhs->type_node_arguments->type_arg_next,type_arg_next){
						if (type_arg_p->type_arg_node->type_node_annotation==StrictAnnot){
							arg_strictness |= 1<<arg_n;
						}
						++arg_n;
					}

					return MemberCallInAStrictContext (node,arg_n,arg_strictness,local_scope);
				}

1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
				if (node->node_arity==2){
					int n_apply_args;
					struct arg *arg_p;
					
					n_apply_args=1;
					arg_p = node->node_arguments;
					while (arg_p!=NULL && arg_p->arg_node->node_arity==2 && arg_p->arg_node->node_kind==NormalNode &&
						   arg_p->arg_node->node_symbol->symb_kind==apply_symb)
					{
						++n_apply_args;
						arg_p=arg_p->arg_node->node_arguments;
					}
1988
1989
1990
					
					if (arg_p!=NULL && arg_p->arg_node->node_kind==SelectorNode && arg_p->arg_node->node_arity==1){
						struct node *selector_node_p;
1991
						
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
						selector_node_p=arg_p->arg_node;
						if ((selector_node_p->node_symbol->symb_def->sdef_mark & SDEF_FIELD_HAS_MEMBER_TYPE)!=0){
							struct symbol_def *field_sdef;
							struct type_alt *member_type_alt;
							
							field_sdef=selector_node_p->node_symbol->symb_def;
							member_type_alt=field_sdef->sdef_member_type_of_field;
							if (member_type_alt->type_alt_lhs->type_node_arity==n_apply_args+1){
								int arg_n;
								unsigned int arg_strictness;
								struct type_arg *type_arg_p;

								{
									struct symbol *new_symbol_p;

									new_symbol_p = CompAlloc (sizeof (struct symbol));
									*new_symbol_p = *node->node_symbol;
									new_symbol_p->symb_instance_apply = 1;
									new_symbol_p->symb_next = (struct symbol*)field_sdef;
									node->node_symbol = new_symbol_p;
2012
								}
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029

								if (OptimizeInstanceCalls){
									arg_n=member_type_alt->type_alt_lhs->type_node_arity-1;
									node->node_state = field_sdef->sdef_member_states_of_field[-1];
									return OptimizedMemberCallInAStrictContext (node,arg_n,field_sdef->sdef_member_states_of_field,local_scope);
								}

								arg_strictness=0;
								arg_n=0;
								for_l (type_arg_p,member_type_alt->type_alt_lhs->type_node_arguments->type_arg_next,type_arg_next){
									if (type_arg_p->type_arg_node->type_node_annotation==StrictAnnot){
										arg_strictness |= 1<<arg_n;
									}
									++arg_n;
								}
		
								return MemberCallInAStrictContext (node,arg_n,arg_strictness,local_scope);
2030
2031
2032
2033
2034
							}
						}
					}
				}

clean's avatar
clean committed
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
				node->node_state = StrictState;
				node->node_state.state_kind = StrictRedirection;
				parallel = DetermineStrictArgContext (node->node_arguments, StrictState, local_scope);
				if (ShouldDecrRefCount)
					DecrRefCountCopiesOfArg (node->node_arguments->arg_next IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
				break;
			case if_symb:
			{
				Args args;
				
				args = node->node_arguments;
				node->node_state = StrictState;

				if (node->node_arity==3){
					if (DetermineStrictArgContext (args, BasicSymbolStates[bool_type], local_scope))
						parallel = True;

					args = args->arg_next;
#ifdef FASTER_STRICT_IF
					node->node_state=demanded_state;

					if (DetermineStrictArgContext (args,demanded_state,20000/*local_scope+1*/))
						parallel = True;

					args=args->arg_next;
					
					if (DetermineStrictArgContext (args,demanded_state,20000/*local_scope+1*/))
						parallel = True;

					break;
#else
					node->node_state.state_kind = StrictRedirection;
#endif
				}
				if (ShouldDecrRefCount)
					for (; args; args = args->arg_next)
						DecrRefCountCopiesOfArg (args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
				break;
			}
			case select_symb:
			{
				Args arg;
				Node argnode;
				
				arg=node->node_arguments;
				
				SetUpdateableTupleState (&arg->arg_state, NewArrayOfUnaryStates (rootsymb->symb_arity, Undefined), rootsymb->symb_arity);
	
				arg->arg_state.state_tuple_arguments [node->node_arity - 1] = demanded_state;
				
				argnode=arg->arg_node;
				if (argnode->node_kind!=NodeIdNode)
					parallel = NodeInAStrictContext (argnode, arg->arg_state, local_scope);
				else {
					NodeId node_id;
					
					node_id=argnode->node_node_id;

					if (node_id->nid_ref_count_copy>=0 && node_id->nid_node_def){
						int node_id_scope;
					
						argnode = node_id->nid_node_def->def_node;
						
						if (ShouldDecrRefCount)
							DecrementRefCountCopy (node_id);
	
						node_id_scope=node_id->nid_scope;
						if (node_id_scope<0)
							node_id_scope=-node_id_scope;

#if 0
John van Groningen's avatar
John van Groningen committed
2106
2107
2108
						FPrintF (rules_file,"NodeInAStrictContext select_symb %d ",node->node_arity);
						DPrintNodeId (node_id,rules_file);
						FPrintF (rules_file," %d %d\n",node_id_scope,local_scope);
clean's avatar
clean committed
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
#endif
						if (node_id_scope>=local_scope){
							if (IsSimpleState (argnode->node_state)){
								if (argnode->node_state.state_kind!=Parallel){
									SetUpdateableTupleState (&argnode->node_state, NewArrayOfUnaryStates (rootsymb->symb_arity, OnA), rootsymb->symb_arity);
									AdjustState (&argnode->node_state.state_tuple_arguments[node->node_arity-1],demanded_state);
								}
							} else {
								if ((argnode->node_state.state_mark & STATE_ELEMENTS_UPDATEABLE_MASK)==0){
									int i,arity;
									States arg_states;

									arity = argnode->node_state.state_arity;
									arg_states = NewArrayOfStates (arity);

									for (i=0; i<arity; ++i)
										arg_states[i] = argnode->node_state.state_tuple_arguments[i];
									
									argnode->node_state.state_tuple_arguments = arg_states;
									argnode->node_state.state_mark |= STATE_ELEMENTS_UPDATEABLE_MASK;
								}
								
								AdjustState (&argnode->node_state.state_tuple_arguments[node->node_arity-1],demanded_state);
							}
						}
John van Groningen's avatar
John van Groningen committed
2134
2135
2136
2137
2138
						
#if 0
						PrintState (argnode->node_state,rules_file);
						FPrintF (rules_file,"\n");
#endif
clean's avatar
clean committed
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
					}
				}
				node->node_state = demanded_state;
				break;
			}
			case tuple_symb:
				if (IsSimpleState (demanded_state)){
					SetUnaryState (&node->node_state, StrictOnA, TupleObj);
					if (ShouldDecrRefCount)
						DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
				} else {
					Args arg; int i;
					
					for (i=0, arg=node->node_arguments; arg!=NULL; arg=arg->arg_next, i++){
						Bool par;
					
						par = False;
						if (!IsLazyState (demanded_state.state_tuple_arguments[i])){
							if (DetermineStrictArgContext (arg,demanded_state.state_tuple_arguments[i],local_scope))
								par = True;
						} else if (ShouldDecrRefCount)
							DecrRefCountCopiesOfArg (arg IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
						
						arg->arg_state = demanded_state.state_tuple_arguments[i];
						
						if (par){
							arg->arg_state.state_mark |= STATE_PARALLEL_MASK;
							parallel = True;
						}
					}
					node->node_state = demanded_state;
				}
				break;
			case definition:
			{
				StateP definition_state_p;
				SymbDef sdef;
				
				sdef = rootsymb->symb_def;
	
				if (sdef->sdef_arfun!=NoArrayFun
					&& (   (sdef->sdef_arfun==_UnqArraySelectLastFun || sdef->sdef_arfun==_UnqArraySelectNextFun) && node->node_arity==2)
						|| (sdef->sdef_arfun==_ArrayUpdateFun && node->node_arity==3))
				{
					StateP function_state_p;

					function_state_p=sdef->sdef_rule_type->rule_type_state_p;
				
					if (!(function_state_p[0].state_type==SimpleState && function_state_p[0].state_object==UnknownObj)){
						StateP arg_node_state_p;
						NodeP arg_node_p;
						ArgP arg_p;
						
						arg_p=node->node_arguments;

						if (sdef->sdef_arfun!=_ArrayUpdateFun){
							parallel = ArgsInAStrictContext (function_state_p,arg_p,local_scope);
							node->node_state = function_state_p[-1];

							arg_node_p=arg_p->arg_node;
							if (arg_node_p->node_kind!=NodeIdNode){
								arg_node_state_p=&arg_node_p->node_state;
													
								if (arg_node_state_p->state_type==TupleState && arg_node_state_p->state_tuple_arguments[1].state_type!=SimpleState){
									StateP tuple_state_args_p,tuple_result_state_args_p,result_state_p;
									
									result_state_p=&arg_node_state_p->state_tuple_arguments[1];
									
									tuple_state_args_p=CompAllocArray (2,StateS);
									tuple_state_args_p[0]=arg_p->arg_state.state_tuple_arguments[0];							
									tuple_state_args_p[1]=*result_state_p;	
									arg_p->arg_state.state_tuple_arguments=tuple_state_args_p;
									
									tuple_result_state_args_p=CompAllocArray (2,StateS);
									tuple_result_state_args_p[0]=node->node_state.state_tuple_arguments[0];
									tuple_result_state_args_p[1]=*result_state_p;
									node->node_state.state_tuple_arguments=tuple_result_state_args_p;
								}
							}
						} else {
							NodeP arg_node_p;

							if (demanded_state.state_type!=SimpleState){
								StateS arg_states[3],*update_arg_tuple_arg_states;
							
								arg_states[0]=function_state_p[0];
								arg_states[1]=function_state_p[1];
								arg_states[2]=function_state_p[2];
							
								update_arg_tuple_arg_states=NewArrayOfStates (2);
								update_arg_tuple_arg_states[0]=arg_states[0].state_tuple_arguments[0];
								update_arg_tuple_arg_states[1]=demanded_state;
								
								arg_states[0].state_tuple_arguments=update_arg_tuple_arg_states;
						
								parallel = ArgsInAStrictContext (arg_states,arg_p,local_scope);
								node->node_state = demanded_state;
							} else {
								parallel = ArgsInAStrictContext (function_state_p,arg_p,local_scope);
								node->node_state = function_state_p[-1];

								arg_node_p=arg_p->arg_node;
								if (arg_node_p->node_kind!=NodeIdNode){
									arg_node_state_p=&arg_node_p->node_state;
														
									if (arg_node_state_p->state_type==TupleState && arg_node_state_p->state_tuple_arguments[1].state_type!=SimpleState){
										StateP tuple_state_args_p,result_state_p;
										
										result_state_p=&arg_node_state_p->state_tuple_arguments[1];
										
										tuple_state_args_p=CompAllocArray (2,StateS);
										tuple_state_args_p[0]=arg_p->arg_state.state_tuple_arguments[0];							
										tuple_state_args_p[1]=*result_state_p;	
										arg_p->arg_state.state_tuple_arguments=tuple_state_args_p;
										
										node->node_state=*result_state_p;
									}
								}
							}
						}
						break;
					}
				}

				definition_state_p = GetStateOfArguments (sdef,node->node_arity);
2264

clean's avatar
clean committed
2265
2266
				if (definition_state_p!=NULL){
#ifdef FASTER_STRICT_AND_OR
2267
					if (sdef->sdef_module==StdBoolId->ident_name && node->node_arity==2){
clean's avatar
clean committed
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
						if