optimisations.c 130 KB
Newer Older
clean's avatar
clean committed
1
2
3
4
5
/*
	File:	optimisations.c
	Author:	John van Groningen
*/

6
7
#include "compiledefines.h"
#include "types.t"
clean's avatar
clean committed
8
9
10
11
12
13
14
15
#include "system.h"
#include "syntaxtr.t"
#include "comsupport.h"
#include "statesgen.h"
#include "checker.h"
#include "scanner.h"
#include "buildtree.h"
#include "codegen_types.h"
John van Groningen's avatar
John van Groningen committed
16
#include "optimisations.h"
clean's avatar
clean committed
17
18
19
20
21
22
23
24
25
26
#include "codegen1.h"
#include "codegen2.h"
#include "sa.h"
#include "settings.h"
#include "pattern_match.h"

#define STRICT_STATE_FOR_LAZY_TUPLE_CONSTRUCTORS
#define UNTUPLE_STRICT_TUPLES /* also in statesgen.c */
#define MOVE_TUPLE_RECORD_AND_ARRAY_RESULT_FUNCTION_ARGUMENT_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
#define MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
27
28
#define THUNK_LIFT_U_RECORD_SELECTORS
#define THUNK_LIFT_0_CONSTRUCTORS
clean's avatar
clean committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

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

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

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

#define MAX_N_VERSIONS 3

static int function_changed;

static int tuple_state_has_more_strictness (StateS *state_p,TypeNode type_node,StateS *function_state_p)
{
	StateS *arg_state_p,*function_arg_state_p;
	TypeArg *type_arg;

	if (type_node->type_node_is_var || type_node->type_node_symbol->symb_kind!=tuple_type)
		return 0;
	
	if (type_node->type_node_arity!=state_p->state_arity || type_node->type_node_symbol->symb_arity!=state_p->state_arity)
		return 0;
	
	type_arg=type_node->type_node_arguments;
	arg_state_p=state_p->state_tuple_arguments;
	function_arg_state_p=function_state_p->state_tuple_arguments;
	
	while (type_arg!=NULL){
		switch (arg_state_p->state_type){
			case SimpleState:
				if (! IsLazyStateKind (arg_state_p->state_kind))
					if (IsLazyState (*function_arg_state_p))
						return 1;
				break;
			case TupleState:
				if (IsLazyState (*function_arg_state_p))
					return 1;
				
				if (function_arg_state_p->state_type==TupleState)
					if (tuple_state_has_more_strictness (arg_state_p,type_arg->type_arg_node,function_arg_state_p))
						return 1;
				break;
			case ArrayState:
			case RecordState:
				if (IsLazyState (*function_arg_state_p))
					return 1;
				break;
		}
		
		type_arg=type_arg->type_arg_next;
		++arg_state_p;
		++function_arg_state_p;
	}
	
89
	return 0;
clean's avatar
clean committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
281
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
}

static int equal_strictness_in_types (TypeNode lazy_type_node,TypeNode strict_type_node)
{
	TypeArg *lazy_type_arg,*strict_type_arg;

	if (lazy_type_node->type_node_is_var || lazy_type_node->type_node_symbol->symb_kind!=tuple_type)
		return 0;
	
	for_l_l (lazy_type_arg,lazy_type_node->type_node_arguments,type_arg_next,
			strict_type_arg,strict_type_node->type_node_arguments,type_arg_next)
	{
		TypeNode lazy_type_arg_node,strict_type_arg_node;

		lazy_type_arg_node=lazy_type_arg->type_arg_node;
		strict_type_arg_node=strict_type_arg->type_arg_node;
				
		if (lazy_type_arg_node->type_node_annotation==StrictAnnot != strict_type_arg_node->type_node_annotation==StrictAnnot)
			return 0;

		if (!lazy_type_arg_node->type_node_is_var && lazy_type_arg_node->type_node_symbol->symb_kind==tuple_type)
			if (!equal_strictness_in_types (lazy_type_arg_node,strict_type_arg_node))
				return 0;				
	}
	
	return 1;
}

static int type_and_strictness_in_state_equals_type (TypeNode lazy_type_node,StateS *state_p,TypeNode strict_type_node)
{
	StateS *arg_state_p;
	TypeArg *lazy_type_arg,*strict_type_arg;

	if (lazy_type_node->type_node_is_var || lazy_type_node->type_node_symbol->symb_kind!=tuple_type)
		return 0;
	
	if (lazy_type_node->type_node_arity!=state_p->state_arity || lazy_type_node->type_node_symbol->symb_arity!=state_p->state_arity)
		return 0;
	
	arg_state_p=state_p->state_tuple_arguments;
	lazy_type_arg=lazy_type_node->type_node_arguments;
	strict_type_arg=strict_type_node->type_node_arguments;

	while (lazy_type_arg!=NULL){
		TypeNode lazy_type_arg_node,strict_type_arg_node;
		int strict;

		lazy_type_arg_node=lazy_type_arg->type_arg_node;
		strict_type_arg_node=strict_type_arg->type_arg_node;
		
		strict = lazy_type_arg_node->type_node_annotation==StrictAnnot || !IsLazyState (*arg_state_p);
		
		if (strict != strict_type_arg_node->type_node_annotation==StrictAnnot)
			return 0;

		if (!lazy_type_arg_node->type_node_is_var && lazy_type_arg_node->type_node_symbol->symb_kind==tuple_type)
			if (arg_state_p->state_type==TupleState){
				if (!type_and_strictness_in_state_equals_type (lazy_type_arg_node,arg_state_p,strict_type_arg_node))
					return 0;
			} else {
				if (!equal_strictness_in_types (lazy_type_arg_node,strict_type_arg_node))
					return 0;				
			}

		++arg_state_p;		
		lazy_type_arg=lazy_type_arg->type_arg_next;
		strict_type_arg=strict_type_arg->type_arg_next;
	}
	
	return 1;
}

static void add_strictness_in_state_to_type (StateS *state_p,TypeNode type_node)
{
	StateS *arg_state_p;
	TypeArg *type_arg;

	if (type_node->type_node_is_var || type_node->type_node_symbol->symb_kind!=tuple_type)
		return;
	
	if (type_node->type_node_arity!=state_p->state_arity || type_node->type_node_symbol->symb_arity!=state_p->state_arity)
		return;
	
	arg_state_p=state_p->state_tuple_arguments;
	type_arg=type_node->type_node_arguments;

	while (type_arg!=NULL){
		TypeNode type_arg_node;

		type_arg_node=type_arg->type_arg_node;

		switch (arg_state_p->state_type){
			case SimpleState:
				if (IsLazyStateKind (arg_state_p->state_kind))
					break;
			case ArrayState:
			case RecordState:
				if (type_arg_node->type_node_annotation==NoAnnot)
					type_arg_node->type_node_annotation=StrictAnnot;
				break;
			case TupleState:
				if (type_arg_node->type_node_annotation==NoAnnot)
					type_arg_node->type_node_annotation=StrictAnnot;
				
				if (!type_arg_node->type_node_is_var && type_arg_node->type_node_symbol->symb_kind==tuple_type)
					add_strictness_in_state_to_type (arg_state_p,type_arg_node);
				break;
		}
				
		++arg_state_p;		
		type_arg=type_arg->type_arg_next;
	}
}

static TypeNode copy_type (TypeNode old_type)
{
	TypeNode new_type;
	
	new_type=CompAllocType (struct type_node);
	*new_type=*old_type;

#if 0
	ConvertAnnotationToStateForTypeNode (new_type);
#endif
	
	if (!old_type->type_node_is_var){
		TypeArgs old_arg,*next_p;
		
		next_p=&new_type->type_node_arguments;
		for_l (old_arg,old_type->type_node_arguments,type_arg_next){
			TypeArgs new_arg;
			
			new_arg=CompAllocType (TypeArg);
			new_arg->type_arg_node=copy_type (old_arg->type_arg_node);
			*next_p=new_arg;
			next_p=&new_arg->type_arg_next;
		}
		
		*next_p=NULL;
	}
	
	return new_type;
}

static TypeAlts copy_rule_type (TypeAlts old_rule_alt)
{
	TypeAlts new_rule_alt;
	
	new_rule_alt=CompAllocType (TypeAlt);
	*new_rule_alt=*old_rule_alt;
	
	new_rule_alt->type_alt_lhs = copy_type (old_rule_alt->type_alt_lhs);
	new_rule_alt->type_alt_rhs = copy_type (old_rule_alt->type_alt_rhs);
	
	return new_rule_alt;
}

SymbolP copy_imp_rule_and_type (SymbDef old_sdef)
{
	SymbDef new_sdef;
	ImpRuleP new_rule,old_rule;
	Symbol new_symbol;

	new_sdef = CompAllocType (SymbDefS);

	new_symbol=NewSymbol (definition);
	new_symbol->symb_def=new_sdef;
		
	*new_sdef = *old_sdef;
	new_sdef->sdef_mark &= ~(SDEF_USED_STRICTLY_MASK | SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK | 
							 SDEF_NEXT_IMP_RULE_VERSION_MASK | SDEF_HAS_IMP_RULE_VERSIONS_MASK);
	new_sdef->sdef_exported=False;
	new_sdef->sdef_sa_fun=NULL;

	new_rule = CompAllocType (ImpRuleS);
	new_sdef->sdef_rule=new_rule;

	old_rule=old_sdef->sdef_rule;

	new_rule->rule_type=copy_rule_type (old_rule->rule_type);
	new_rule->rule_type->type_alt_lhs->type_node_symbol=new_symbol;
			
	return new_symbol;	
}

static Node copy_node (Node old, Bool lhs);

static NodeId copy_node_id (NodeId old_node_id)
{
	NodeId new_node_id;
	
	new_node_id = CompAllocType (NodeIdS);
	
	*new_node_id = *old_node_id;

	new_node_id->nid_mark &= ~SHARED_NODES_COLLECTED_MASK;

#if OPTIMIZE_LAZY_TUPLE_RECURSION
	new_node_id->nid_mark2 &= ~NID_CALL_VIA_LAZY_SELECTIONS_ONLY;
#endif
	new_node_id->nid_ref_count_copy_=new_node_id->nid_refcount;
	new_node_id->nid_exp_=NULL;

	old_node_id->nid_forward_node_id_ = new_node_id;
		
	return new_node_id;
}

static NodeDefP copy_lhs_node_ids_of_node_defs (NodeDefs old_node_defs)
{
	NodeDefP old_def_p,first_p,*next_h;
	
	next_h=&first_p;
	
	for_l (old_def_p,old_node_defs,def_next){
		NodeDefs new_node_def;
	
		new_node_def = CompAllocType (NodeDefS);

		new_node_def->def_id = copy_node_id (old_def_p->def_id);
		new_node_def->def_node = old_def_p->def_node;
		new_node_def->def_mark = 0;

		*next_h=new_node_def;
		next_h=&new_node_def->def_next;
	}

	*next_h=NULL;
	
	return first_p;
}

322
static NodeDefP copy_rhs_node_ids_of_node_defs (NodeDefs old_node_defs)
clean's avatar
clean committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
416
{
	NodeDefP old_def_p,first_p,*next_h;
	
	next_h=&first_p;
	
	for_l (old_def_p,old_node_defs,def_next){
		NodeDefs new_node_def;
		NodeId new_node_id;

		if (old_def_p->def_node!=NULL)
			new_node_id = copy_node_id (old_def_p->def_id);
		else
			new_node_id = old_def_p->def_id->nid_forward_node_id;

		new_node_def = CompAllocType (NodeDefS);
		
		new_node_def->def_id=new_node_id;
		new_node_id->nid_node_def_=new_node_def;
		new_node_def->def_mark=0;
		
		new_node_def->def_node = old_def_p->def_node;
		*next_h=new_node_def;
		next_h=&new_node_def->def_next;
	}

	*next_h=NULL;
	
	return first_p;
}

static void copy_nodes_of_node_defs (NodeDefs node_defs,Bool lhs)
{
	NodeDefS *node_def;
	
	for_l (node_def,node_defs,def_next){
		if (node_def->def_node!=NULL)
			node_def->def_node = copy_node (node_def->def_node,lhs);
		node_def->def_id->nid_node=node_def->def_node;
	}
}

static StrictNodeIdP copy_strict_node_ids (StrictNodeIdP old_strict_node_ids)
{
	StrictNodeIdP old_p,first_p,*next_h;
	
	next_h=&first_p;
	
	for_l (old_p,old_strict_node_ids,snid_next){
		StrictNodeIdP new;
		
		new = CompAllocType (StrictNodeIdS);

#ifdef OBSERVE_ARRAY_SELECTS_IN_PATTERN
		new->snid_array_select_in_pattern=old_p->snid_array_select_in_pattern;
#endif
		new->snid_node_id = old_p->snid_node_id->nid_forward_node_id;

		*next_h = new;
		next_h = &new->snid_next;
	}
	*next_h = NULL;
	
	return first_p;
}

#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static Node copy_root_node (Node old)
{
	if (old->node_kind==IfNode){
		struct if_node_contents *new_then_else_info,*old_then_else_info;
		ArgS *previous_arg,*new_arg,*old_arg;
		Node new;
		
		new = CompAllocType (NodeS);

		*new = *old;
		
		DetermineNodeState (new);

		new_then_else_info = CompAllocType (IfNodeContentsS);

		old_then_else_info = old->node_contents.contents_if;
		new->node_contents.contents_if = new_then_else_info;

		old_arg=old->node_arguments;

		new_arg = CompAllocType (ArgS);
		new_arg->arg_node = copy_root_node (old_arg->arg_node);
		new_arg->arg_state = LazyState;
		new->node_arguments = new_arg;
		previous_arg = new_arg;

		old_arg=old_arg->arg_next;

417
		new_then_else_info->if_then_node_defs=copy_rhs_node_ids_of_node_defs (old_then_else_info->if_then_node_defs);
clean's avatar
clean committed
418
419
420
421
422
423
424
425
426
427
			
		new_arg = CompAllocType (ArgS);
		new_arg->arg_state = LazyState;
		new_arg->arg_node = copy_root_node (old_arg->arg_node);
		previous_arg->arg_next = new_arg;
		previous_arg = new_arg;

		copy_nodes_of_node_defs (new_then_else_info->if_then_node_defs,False);
		new_then_else_info->if_then_strict_node_ids=copy_strict_node_ids (old_then_else_info->if_then_strict_node_ids);

428
		new_then_else_info->if_else_node_defs=copy_rhs_node_ids_of_node_defs (old_then_else_info->if_else_node_defs);
clean's avatar
clean committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

		old_arg=old_arg->arg_next;
			
		new_arg = CompAllocType (ArgS);
		new_arg->arg_state = LazyState;
		new_arg->arg_node = copy_root_node (old_arg->arg_node);
		previous_arg->arg_next = new_arg;
		new_arg->arg_next=NULL;

		copy_nodes_of_node_defs (new_then_else_info->if_else_node_defs,False);
		new_then_else_info->if_else_strict_node_ids=copy_strict_node_ids (old_then_else_info->if_else_strict_node_ids);
		
		new_then_else_info->if_local_scope=old_then_else_info->if_local_scope;

		return new;
	} else if (old->node_kind==SwitchNode){
		Args *next,old_arg;
		NodeP new;
		
		new = CompAllocType (NodeS);
		*new = *old;

		{
			NodeIdP old_node_id,new_node_id;
			
			old_node_id = old->node_node_id;
			
			new_node_id=old_node_id->nid_forward_node_id;
			
			if (new_node_id==NULL)
				error_in_function ("copy_root_node");
			
			new->node_node_id=new_node_id;
		}
		
		next = &new->node_arguments;
		for_l (old_arg,old->node_arguments,arg_next){
			NodeP case_node_p,new_case_node_p;
			ArgP new_arg,case_node_arg_p,new_case_node_arg_p;
			
			new_arg = CompAllocType (ArgS);
			new_arg->arg_state = LazyState;
			
			*next = new_arg;
			next = &new_arg->arg_next;

			case_node_p=old_arg->arg_node;

			new_case_node_p = CompAllocType (NodeS);
			*new_case_node_p = *case_node_p;

			new_arg->arg_node = new_case_node_p;
			
			new_case_node_arg_p=CompAllocType (ArgS);
			new_case_node_arg_p->arg_state=LazyState;
			
			new_case_node_p->node_arguments=new_case_node_arg_p;
			new_case_node_arg_p->arg_next=NULL;

			case_node_arg_p=case_node_p->node_arguments;

			if (case_node_p->node_kind==CaseNode){
491
492
				new_case_node_p->node_su.su_u.u_case=CompAllocType (CaseNodeContentsS);

493
				new_case_node_p->node_node_defs = copy_rhs_node_ids_of_node_defs (case_node_p->node_node_defs);
clean's avatar
clean committed
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
522
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
				
				if (case_node_arg_p->arg_node->node_kind==PushNode){
					ArgP push_node_arg_1,new_push_node_arg_1,new_push_node_arg_2;
					NodeP push_node_arg_2_node,push_node,new_push_node;
					NodeIdListElementP node_id_list,*new_node_id_list_p;
					
					push_node=case_node_arg_p->arg_node;

					new_push_node=CompAllocType (NodeS);
					*new_push_node=*push_node;
					
					new_case_node_arg_p->arg_node=new_push_node;
					push_node_arg_1=push_node->node_arguments;

					new_node_id_list_p=&new_push_node->node_node_ids;
					
					if (push_node_arg_1->arg_node->node_node_id->nid_node!=NULL){
						/* unboxable lhs tuple or record */
						for_l (node_id_list,push_node->node_node_ids,nidl_next){
							NodeIdListElementP new_node_id_list;
							
							new_node_id_list=CompAllocType (NodeIdListElementS);
							
							new_node_id_list->nidl_node_id=node_id_list->nidl_node_id->nid_forward_node_id;
									
							*new_node_id_list_p=new_node_id_list;
							new_node_id_list_p=&new_node_id_list->nidl_next;
						}

					} else {
						for_l (node_id_list,push_node->node_node_ids,nidl_next){
							NodeIdListElementP new_node_id_list;
							
							new_node_id_list=CompAllocType (NodeIdListElementS);
							
							new_node_id_list->nidl_node_id=copy_node_id (node_id_list->nidl_node_id);
																
							*new_node_id_list_p=new_node_id_list;
							new_node_id_list_p=&new_node_id_list->nidl_next;
						}
					}
					
					*new_node_id_list_p=NULL;

					push_node_arg_2_node=push_node_arg_1->arg_next->arg_node;
					
					new_push_node_arg_1=CompAllocType (ArgS);
					new_push_node_arg_1->arg_state=LazyState;
					
					new_push_node_arg_2=CompAllocType (ArgS);
					new_push_node_arg_2->arg_state=LazyState;
					
					new_push_node->node_arguments=new_push_node_arg_1;
					new_push_node_arg_1->arg_next=new_push_node_arg_2;
					new_push_node_arg_2->arg_next=NULL;
					
					copy_nodes_of_node_defs (new_case_node_p->node_node_defs,False);
					new_push_node_arg_1->arg_node = copy_node (push_node_arg_1->arg_node,False);
					new_push_node_arg_2->arg_node = copy_root_node (push_node_arg_2_node);
553
554
555
556
557
					
#if STRICT_LISTS
					if (new_push_node->node_push_symbol->symb_kind==cons_symb && (new_push_node->node_push_symbol->symb_head_strictness & 1))
						new_push_node->node_decons_node=copy_node (new_push_node->node_decons_node,False);
#endif
clean's avatar
clean committed
558
559
560
561
562
				} else {
					copy_nodes_of_node_defs (new_case_node_p->node_node_defs,False);
					new_case_node_arg_p->arg_node = copy_root_node (case_node_arg_p->arg_node);
				}
			} else if (case_node_p->node_kind==DefaultNode){
563
564
				new_case_node_p->node_su.su_u.u_case=CompAllocType (CaseNodeContentsS);

565
				new_case_node_p->node_node_defs = copy_rhs_node_ids_of_node_defs (case_node_p->node_node_defs);
566
567
568
569
570
571
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
				copy_nodes_of_node_defs (new_case_node_p->node_node_defs,False);
				new_case_node_arg_p->arg_node = copy_root_node (case_node_arg_p->arg_node);

			} else if (case_node_p->node_kind==OverloadedCaseNode){
				NodeP new_overloaded_case_node_p;
				ArgP new_overloaded_case_node_arg2_p;

				new_overloaded_case_node_arg2_p=CompAllocType (ArgS);
				new_overloaded_case_node_arg2_p->arg_state=LazyState;

				new_overloaded_case_node_arg2_p->arg_next=NULL;
				new_case_node_arg_p->arg_next=new_overloaded_case_node_arg2_p;

				new_case_node_arg_p->arg_node=copy_node (case_node_arg_p->arg_node,False);
				new_overloaded_case_node_arg2_p->arg_node=copy_node (case_node_arg_p->arg_next->arg_node,False);

				case_node_p=case_node_p->node_node;
				new_overloaded_case_node_p=new_case_node_p;

				new_case_node_p = CompAllocType (NodeS);
				*new_case_node_p = *case_node_p;

				new_overloaded_case_node_p->node_node = new_case_node_p;
				
				new_case_node_arg_p=CompAllocType (ArgS);
				new_case_node_arg_p->arg_state=LazyState;
				
				new_case_node_p->node_arguments=new_case_node_arg_p;
				new_case_node_arg_p->arg_next=NULL;

				case_node_arg_p=case_node_p->node_arguments;

				new_case_node_p->node_su.su_u.u_case=CompAllocType (CaseNodeContentsS);

600
				new_case_node_p->node_node_defs = copy_rhs_node_ids_of_node_defs (case_node_p->node_node_defs);
601

clean's avatar
clean committed
602
603
				copy_nodes_of_node_defs (new_case_node_p->node_node_defs,False);
				new_case_node_arg_p->arg_node = copy_root_node (case_node_arg_p->arg_node);
604
605


clean's avatar
clean committed
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
			} else
				error_in_function ("copy_root_node");

			{
				NodeIdRefCountListP node_id_ref_count_elem_p,new_node_id_ref_count_elem_p,*node_id_ref_count_elem_h;

				node_id_ref_count_elem_h=&new_case_node_p->node_node_id_ref_counts;
				
				for_l (node_id_ref_count_elem_p,case_node_p->node_node_id_ref_counts,nrcl_next){
					new_node_id_ref_count_elem_p=CompAllocType (NodeIdRefCountListS);
					
					*node_id_ref_count_elem_h=new_node_id_ref_count_elem_p;
					new_node_id_ref_count_elem_p->nrcl_ref_count = node_id_ref_count_elem_p->nrcl_ref_count;
					new_node_id_ref_count_elem_p->nrcl_node_id = node_id_ref_count_elem_p->nrcl_node_id->nid_forward_node_id;

					node_id_ref_count_elem_h=&new_node_id_ref_count_elem_p->nrcl_next;
				}
				*node_id_ref_count_elem_h=NULL;
			}
			{				
				StrictNodeIdP strict_node_id_p,new_strict_node_id,*strict_node_id_h;

				strict_node_id_h=&new_case_node_p->node_strict_node_ids;

				for_l (strict_node_id_p,case_node_p->node_strict_node_ids,snid_next){
					new_strict_node_id=CompAllocType (StrictNodeIdS);
					
					*strict_node_id_h=new_strict_node_id;
					new_strict_node_id->snid_node_id = strict_node_id_p->snid_node_id->nid_forward_node_id;
					
					strict_node_id_h=&new_strict_node_id->snid_next;
				}
				
				*strict_node_id_h=NULL;
			}
		}
		
		*next = NULL;

		return new;
	} else if (old->node_kind==GuardNode){
		NodeP new;
		ArgP arg_1,arg_2;
		
		new = CompAllocType (NodeS);
		*new = *old;

		arg_1 = CompAllocType (ArgS);
		arg_1->arg_state = LazyState;
			
		arg_2 = CompAllocType (ArgS);
		arg_2->arg_state = LazyState;
		
		new->node_arguments=arg_1;
		arg_1->arg_next=arg_2;
		arg_2->arg_next=NULL;
		
		arg_1->arg_node = copy_root_node (old->node_arguments->arg_node);

665
		new->node_node_defs = copy_rhs_node_ids_of_node_defs (old->node_node_defs);
clean's avatar
clean committed
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

		arg_2->arg_node = copy_root_node (old->node_arguments->arg_next->arg_node);

		copy_nodes_of_node_defs (new->node_node_defs,False);

		{	
			StrictNodeIdP strict_node_id_p,new_strict_node_id,*strict_node_id_h;

			strict_node_id_h=&new->node_guard_strict_node_ids;

			for_l (strict_node_id_p,old->node_guard_strict_node_ids,snid_next){
				new_strict_node_id=CompAllocType (StrictNodeIdS);
				
				*strict_node_id_h=new_strict_node_id;
				new_strict_node_id->snid_node_id = strict_node_id_p->snid_node_id->nid_forward_node_id;
				
				strict_node_id_h=&new_strict_node_id->snid_next;
			}
			
			*strict_node_id_h=NULL;
		}
				
		return new;
	} else
		return copy_node (old,False);
}
#endif

static Node copy_node (Node old,Bool lhs)
{
	Node new;
	
	new = CompAllocType (NodeS);

	*new = *old;
	
	if (old->node_kind==NodeIdNode){
		NodeId old_nid,new_node_id;
		
		old_nid = old->node_node_id;
		
		if (lhs && old_nid->nid_node==NULL)
			new_node_id=copy_node_id (old_nid);
		else
			new_node_id=old_nid->nid_forward_node_id;
		
		if (new_node_id==NULL)
			error_in_function ("copy_node");
		
		new->node_node_id=new_node_id;
		
		return new;
	}
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
	else if (old->node_kind==IfNode){
		struct if_node_contents *new_then_else_info,*old_then_else_info;
		ArgS *previous_arg,*new_arg,*old_arg;

		DetermineNodeState (new);

		new_then_else_info = CompAllocType (IfNodeContentsS);

		old_then_else_info = old->node_contents.contents_if;
		new->node_contents.contents_if = new_then_else_info;

731
732
733
		new_then_else_info->if_then_rules = NULL;
		new_then_else_info->if_else_rules = NULL;

clean's avatar
clean committed
734
735
736
737
738
739
740
741
742
743
		old_arg=old->node_arguments;

		new_arg = CompAllocType (ArgS);
		new_arg->arg_node = copy_node (old_arg->arg_node,lhs);
		new_arg->arg_state = LazyState;
		new->node_arguments = new_arg;
		previous_arg = new_arg;

		old_arg=old_arg->arg_next;

744
		new_then_else_info->if_then_node_defs=copy_rhs_node_ids_of_node_defs (old_then_else_info->if_then_node_defs);
clean's avatar
clean committed
745
746
747
748
749
750
751
752
753
754
		
		new_arg = CompAllocType (ArgS);
		new_arg->arg_state = LazyState;
		new_arg->arg_node = copy_node (old_arg->arg_node,lhs);
		previous_arg->arg_next = new_arg;
		previous_arg = new_arg;

		copy_nodes_of_node_defs (new_then_else_info->if_then_node_defs,False);
		new_then_else_info->if_then_strict_node_ids=copy_strict_node_ids (old_then_else_info->if_then_strict_node_ids);

755
		new_then_else_info->if_else_node_defs=copy_rhs_node_ids_of_node_defs (old_then_else_info->if_else_node_defs);
clean's avatar
clean committed
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795

		old_arg=old_arg->arg_next;
			
		new_arg = CompAllocType (ArgS);
		new_arg->arg_state = LazyState;
		new_arg->arg_node = copy_node (old_arg->arg_node,lhs);
		previous_arg->arg_next = new_arg;
		new_arg->arg_next=NULL;

		copy_nodes_of_node_defs (new_then_else_info->if_else_node_defs,False);
		new_then_else_info->if_else_strict_node_ids=copy_strict_node_ids (old_then_else_info->if_else_strict_node_ids);
		
		new_then_else_info->if_local_scope=old_then_else_info->if_local_scope;

		return new;
	}
#endif
	else if (!lhs)
		DetermineNodeState (new);

	if (old->node_arguments!=NULL){
		Args *next,old_arg;
	
		next = &new->node_arguments;
		for_l (old_arg,old->node_arguments,arg_next){
			Args new_arg;
			
			new_arg = CompAllocType (ArgS);
			new_arg->arg_node = copy_node (old_arg->arg_node,lhs);
			new_arg->arg_state = LazyState;
			
			*next = new_arg;
			next = &new_arg->arg_next;
		}
		*next = NULL;
	}
	
	return new;
}

796
static RuleAltP copy_alt (RuleAltP old_alts,Symbol new_symbol)
clean's avatar
clean committed
797
{
798
	RuleAltP old,new;
clean's avatar
clean committed
799
	
800
801
802
803
804
805
806
807
808
809
	old=old_alts;
	
	new = CompAllocType (RuleAltS);
	
	new->alt_lhs_defs=copy_lhs_node_ids_of_node_defs (old->alt_lhs_defs);
	new->alt_lhs_root = copy_node (old->alt_lhs_root, True);
	new->alt_lhs_root->node_symbol=new_symbol;
	copy_nodes_of_node_defs (new->alt_lhs_defs,True);
	
 	new->alt_rhs_defs=copy_rhs_node_ids_of_node_defs (old->alt_rhs_defs);
clean's avatar
clean committed
810
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
811
	new->alt_rhs_root = copy_root_node (old->alt_rhs_root);
clean's avatar
clean committed
812
#else
813
	new->alt_rhs_root = copy_node (old->alt_rhs_root, False);
clean's avatar
clean committed
814
#endif
815
816
817
818
819
	copy_nodes_of_node_defs (new->alt_rhs_defs,False);
	new->alt_strict_node_ids=copy_strict_node_ids (old->alt_strict_node_ids);
	
	new->alt_line = old->alt_line;
	new->alt_kind = old->alt_kind;
clean's avatar
clean committed
820

821
	return new;
clean's avatar
clean committed
822
823
824
825
}

void copy_imp_rule_nodes (ImpRuleP old_rule_p,ImpRuleP new_rule_p)
{
826
	new_rule_p->rule_alts = copy_alt (old_rule_p->rule_alts,new_rule_p->rule_type->type_alt_lhs->type_node_symbol);
clean's avatar
clean committed
827
	new_rule_p->rule_root = new_rule_p->rule_alts->alt_lhs_root;
828
	new_rule_p->rule_mark = old_rule_p->rule_mark & RULE_CAF_MASK;
clean's avatar
clean committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
}

static ImpRules new_strict_result_rules;

int optimise_tuple_result_function (Node node,StateS demanded_state)
{
	Symbol symbol;
	TypeNode result_type;
	Symbol new_function_symbol;
	SymbDef sdef,new_sdef,last_version;
	ImpRuleP new_rule_p;

	symbol=node->node_symbol;
	sdef=symbol->symb_def;

844
	if (sdef->sdef_kind!=IMPRULE || node->node_arity!=sdef->sdef_arity)
clean's avatar
clean committed
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
		return 0;
	
	result_type=sdef->sdef_rule->rule_type->type_alt_rhs;
	if (! tuple_state_has_more_strictness (&demanded_state,result_type,&sdef->sdef_rule->rule_state_p[-1]))
		return 0;

	if (sdef->sdef_mark & SDEF_HAS_IMP_RULE_VERSIONS_MASK){
		while (sdef->sdef_mark & SDEF_NEXT_IMP_RULE_VERSION_MASK)
			sdef=sdef->sdef_next_version;
		last_version=sdef;
		sdef=sdef->sdef_next_version;
	} else
		last_version=sdef;
	
	if (sdef->sdef_mark & SDEF_HAS_IMP_RULE_VERSIONS_MASK){
		SymbDef version;
		int n_versions;

		version=sdef;
		n_versions=0;

		do {
			version=version->sdef_next_version;
			++n_versions;

			if (type_and_strictness_in_state_equals_type (result_type,&demanded_state,version->sdef_rule->rule_type->type_alt_rhs)){
				if (symbol!=version->sdef_rule->rule_type->type_alt_lhs->type_node_symbol){
					node->node_symbol=version->sdef_rule->rule_type->type_alt_lhs->type_node_symbol;
					function_changed=1;

					return 1;
				}
				return 0;
			}
		} while (version->sdef_mark & SDEF_NEXT_IMP_RULE_VERSION_MASK);

		if (n_versions>=MAX_N_VERSIONS)
			return 0;
	}
	
	new_function_symbol = copy_imp_rule_and_type (sdef);
	new_sdef=new_function_symbol->symb_def;
	new_rule_p=new_sdef->sdef_rule;
	
	new_rule_p->rule_next_changed_function=sdef->sdef_rule;

	new_rule_p->rule_next=new_strict_result_rules;
	new_strict_result_rules=new_rule_p;
	
	add_strictness_in_state_to_type (&demanded_state,new_rule_p->rule_type->type_alt_rhs);

	new_rule_p->rule_state_p=NULL;
897

clean's avatar
clean committed
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
	node->node_symbol=new_function_symbol;
	function_changed=1;
	
	last_version->sdef_mark |= SDEF_NEXT_IMP_RULE_VERSION_MASK | SDEF_HAS_IMP_RULE_VERSIONS_MASK;
	last_version->sdef_next_version=new_sdef;
	new_sdef->sdef_next_version=sdef;
	new_sdef->sdef_mark |= SDEF_HAS_IMP_RULE_VERSIONS_MASK;
	
	return 1;
}

#if 0
#include "dbprint.h"
#endif

void generate_states (ImpRuleS *rules,int do_strictness_analysis)
{
	ImpRuleS *rule,*changed_functions,**last_changed_function_l,**rule_p;
	
	new_strict_result_rules=NULL;
	changed_functions=NULL;
	last_changed_function_l=&changed_functions;
	
	for (rule_p=&rules; (rule=*rule_p)!=NULL; rule_p=&rule->rule_next){
		function_changed=0;

		GenerateStatesForRule (rule);
		
		if (function_changed){
			*last_changed_function_l=rule;
			last_changed_function_l=&rule->rule_next_changed_function;
			*last_changed_function_l=NULL;
		}
	}
	
	do {
		ImpRuleS *rule;
		
		while (new_strict_result_rules!=NULL){
			for_l (rule,new_strict_result_rules,rule_next){
				copy_imp_rule_nodes (rule->rule_next_changed_function,rule);
#if 0
				PrintRuleAlt (rule->rule_alts,4,StdOut);
#endif
			}
			
			if (do_strictness_analysis)
				if (StrictnessAnalysisConvertRules (new_strict_result_rules)){
					for_l (rule,new_strict_result_rules,rule_next)
						StrictnessAnalysisForRule (rule->rule_root->node_symbol->symb_def);

					free_unused_sa_blocks();
				} else
					do_strictness_analysis=0;

953
			for_l (rule,new_strict_result_rules,rule_next)
clean's avatar
clean committed
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
				ExamineTypesAndLhsOfSymbolDefinition (rule->rule_root->node_symbol->symb_def);
			
			rule=new_strict_result_rules;
			new_strict_result_rules=NULL;

			*rule_p=rule;

			while (rule!=NULL){
				SymbDef sdef;
								
				sdef=rule->rule_root->node_symbol->symb_def;
						
				function_changed=0;

				GenerateStatesForRule (rule);
				
				if (function_changed){
					*last_changed_function_l=rule;
					last_changed_function_l=&rule->rule_next_changed_function;
					*last_changed_function_l=NULL;
				}

				rule_p=&rule->rule_next;
				rule=*rule_p;
			}	
		}
		
		while (new_strict_result_rules==NULL && changed_functions!=NULL){
			SymbDef sdef;

			rule=changed_functions;
										
			sdef=rule->rule_root->node_symbol->symb_def;
						
			reset_states_and_ref_count_copies (rule);

			ExamineTypesAndLhsOfImpRuleSymbolDefinitionAgain (sdef);
			
			function_changed=0;
			
			GenerateStatesForRule (rule);
			
			if (!function_changed)
				changed_functions=changed_functions->rule_next_changed_function;
		}
	} while (changed_functions!=NULL || new_strict_result_rules!=NULL);
}

static ImpRules new_rules;
static int next_function_n;

static Symbol new_rule_symbol (char *function_name)
{
	SymbDef function_sdef;
	Symbol function_symbol;
	Ident function_ident;

1011
	function_ident=NewIdent (function_name);
clean's avatar
clean committed
1012
1013
	function_sdef=MakeNewSymbolDefinition (CurrentModule,function_ident,0,IMPRULE);

1014
	function_sdef->sdef_ancestor = ~next_def_number;
clean's avatar
clean committed
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
	function_sdef->sdef_number=next_def_number++;
	function_sdef->sdef_isused=True;

	function_sdef->sdef_mark |= SDEF_OPTIMISED_FUNCTION_MASK;
	function_sdef->sdef_returnsnode=True;
	function_sdef->sdef_calledwithrootnode=True;
		
	function_symbol=NewSymbol (definition);
	function_symbol->symb_def=function_sdef;
	
	return function_symbol;
}

#ifdef MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
static StateS apply_symb_function_states[3];
static StateP apply_symb_function_state_p=NULL;

static void init_apply_symb_function_state_p()
{
	SetUnaryState (&apply_symb_function_states[0],StrictRedirection,UnknownObj);
	SetUnaryState (&apply_symb_function_states[1],StrictOnA,UnknownObj);
	SetUnaryState (&apply_symb_function_states[2],OnA,UnknownObj);
	apply_symb_function_state_p=&apply_symb_function_states[1];
}
#endif

1041
#define cTypeDelimiter	';'		/* also in checksupport.h */
1042
#ifndef CLEAN2
1043
#define _ANALYSE_IDENT_			/* also in checksupport.c */
1044
#endif
clean's avatar
clean committed
1045
1046
1047
1048
1049
1050
1051
1052

static int compute_length_before_type_delimiter (char *fname)
{
	char *p;
	unsigned int c;	

	p=fname;
	
1053
#ifdef _ANALYSE_IDENT_
clean's avatar
clean committed
1054
1055
1056
1057
1058
	--p;
	do {
		c=*++p;
	} while (c!=cTypeDelimiter && c!='\0');

1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
	if (c == cTypeDelimiter && *(p+1) != '\0')
	{
		p++;

		if (isdigit (*p))
		{	
			for (p = p+1; *p != cTypeDelimiter && *p != '\0'; p++)
				 ;
		}
	}
#else /* ifndef _ANALYSE_IDENT_ */
	--p;
	do {
		c=*++p;
	} while (c!='\0');

#endif /* _ANALYSE_IDENT_ */

clean's avatar
clean committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
	return 	p-fname;
}

static char *append_n_chars (char *dest,const char *src,int length)
{
	while (length>0){
		*dest++ = *src++;
		--length;
	}

	return dest;
}

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

1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
#ifdef THUNK_LIFT_U_RECORD_SELECTORS
static StateP selector_l_or_n_state_p (StateP tuple_state_p,StateP tuple_arg_states,StateP selector_arg_state_p)
{
	tuple_arg_states[0]=*selector_arg_state_p;
	tuple_arg_states[1]=StrictState;

	tuple_state_p->state_arity = 2;
	tuple_state_p->state_tuple_arguments = tuple_arg_states;
	tuple_state_p->state_type = TupleState;
	tuple_state_p->state_mark = 0;
	
	return tuple_state_p;
}
#endif

clean's avatar
clean committed
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
#define MAX_N_FUNCTION_ARGUMENTS 32

static int add_n_new_arguments_for_local_function (ArgP arg_p,int n_arguments)
{
	for ( ; arg_p!=NULL; arg_p=arg_p->arg_next){
		NodeP arg_node_p;

		arg_node_p=arg_p->arg_node;
		
		if (arg_node_p->node_kind==NodeIdNode){
			if (arg_node_p->node_node_id->nid_mark & NID_LIFTED_BY_OPTIMISE){
				continue;
			} else {
				arg_node_p->node_node_id->nid_mark |= NID_LIFTED_BY_OPTIMISE;
				arg_node_p->node_node_id->nid_forward_node_id=NULL;
1122
1123
							
				n_arguments=add_n_new_arguments_for_local_function (arg_p->arg_next,n_arguments+1);
clean's avatar
clean committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
				
				if (n_arguments>MAX_N_FUNCTION_ARGUMENTS)
					arg_node_p->node_node_id->nid_mark &= ~NID_LIFTED_BY_OPTIMISE;
				
				return n_arguments;
			}	
		} else if (arg_node_p->node_kind==NormalNode){
			switch (arg_node_p->node_symbol->symb_kind){
				case int_denot:
				case bool_denot:
				case char_denot:
				case string_denot:
				case real_denot:
					continue;
			}		
		}
		
		++n_arguments;
	}
	
	return n_arguments;
}

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
1202
1203
1204
1205
1206
static struct arg *remove_apply_nodes (struct node *node,int n_apply_args)
{
	struct arg *old_arg_p,*new_arg_list,*arg_p;
	int arg_n;

	new_arg_list=NULL;
	arg_p=node->node_arguments;
	old_arg_p=arg_p;

	for (arg_n=n_apply_args; arg_n>1; --arg_n){
		struct arg *arg_next_p;

		arg_next_p=arg_p->arg_next;
		arg_next_p->arg_next=new_arg_list;
		new_arg_list=arg_next_p;
		arg_p=arg_p->arg_node->node_arguments;
	}

	arg_p->arg_next->arg_next=new_arg_list;
	node->node_arguments=arg_p;

	return old_arg_p;
}

static void restore_removed_apply_nodes (struct node *node,struct arg *old_arg_p,int n_apply_args)
{
	struct arg *arg_p;
	int arg_n;
	
	node->node_arguments=old_arg_p;
	arg_p=old_arg_p;

	for (arg_n=n_apply_args; arg_n>1; --arg_n){
		arg_p->arg_next->arg_next=NULL;
		arg_p=arg_p->arg_node->node_arguments;
	}

	arg_p->arg_next->arg_next=NULL;
}

static struct arg *store_args_in_apply_nodes (int n_apply_args,struct arg *arg_p,struct node *apply_node)
{
	struct arg *remaining_args;

	if (n_apply_args==1){
		apply_node->node_arguments=arg_p;
		remaining_args=arg_p->arg_next->arg_next;
		arg_p->arg_next->arg_next=NULL;
		return remaining_args;
	} else {
		struct arg *first_arg_p,*next_arg_p;
		first_arg_p=apply_node->node_arguments;
		next_arg_p=store_args_in_apply_nodes (n_apply_args-1,arg_p,first_arg_p->arg_node);
		remaining_args=next_arg_p->arg_next;
		next_arg_p->arg_next=NULL;
		first_arg_p->arg_next=next_arg_p;
		return remaining_args;
	}
}

1207
static char *create_arguments_for_local_function (NodeP node_p,ArgS ***arg_h,ArgS ***lhs_arg_h,ArgS **rhs_arg_p,StateP arg_state_p,int *arity_p,char *function_name_p,char *end_function_name,int *n_arguments_p)
clean's avatar
clean committed
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
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
{
	NodeIdP arg_node_id;
	StateP call_state_p;
	ArgP arg;
	
	if (function_name_p!=NULL && node_p->node_symbol->symb_kind==definition){
		int length_before_type_delimiter;
		char *f_name;
		
		f_name=node_p->node_symbol->symb_def->sdef_ident->ident_name;
		length_before_type_delimiter=compute_length_before_type_delimiter (f_name);
		
		if (function_name_p+2+length_before_type_delimiter < end_function_name){
			*function_name_p++='.';
			function_name_p=append_n_chars (function_name_p,f_name,length_before_type_delimiter);
		} else
			end_function_name=function_name_p;
	}

	for_l (arg,node_p->node_arguments,arg_next){
		ArgP lhs_arg,rhs_arg;
		NodeP arg_node;

		arg_node=arg->arg_node;
		
		if (arg_node->node_kind==NormalNode)
			switch (arg_node->node_symbol->symb_kind){
				case int_denot:
				case bool_denot:
				case char_denot:
				case string_denot:
				case real_denot:
				{
					NodeP function_node;
					ArgP new_arg;
					
					function_node=NewNode (arg_node->node_symbol,NULL,arg_node->node_arity);
					function_node->node_state=LazyState;
					function_node->node_number=0;
					
					new_arg=NewArgument (function_node);
					new_arg->arg_state=LazyState;
					*rhs_arg_p=new_arg;
					rhs_arg_p=&new_arg->arg_next;

					++arg_state_p;
					continue;
				}
				case definition:
				{
					if ((arg_state_p->state_type==SimpleState && arg_state_p->state_kind==OnB)
#ifdef MOVE_TUPLE_RECORD_AND_ARRAY_RESULT_FUNCTION_ARGUMENT_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
						|| arg_state_p->state_type==TupleState || arg_state_p->state_type==RecordState || arg_state_p->state_type==ArrayState
#endif
					){
						SymbDef sdef;
						
						sdef=arg_node->node_symbol->symb_def;
						
1267
						if (arg_node->node_arity==sdef->sdef_arity){
clean's avatar
clean committed
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
							Bool is_rule;
							StateP function_state_p;

							switch (sdef->sdef_kind){
								case IMPRULE:
									is_rule=True;
									function_state_p=sdef->sdef_rule->rule_state_p;
									break;
								case DEFRULE:
								case SYSRULE:
									is_rule=True;
									function_state_p=sdef->sdef_rule_type->rule_type_state_p;
									break;
								case RECORDTYPE:
									if (sdef->sdef_strict_constructor){
										is_rule=True;
										function_state_p=sdef->sdef_record_state.state_record_arguments;
									} else
										is_rule=False;
									break;
								default:
									is_rule=False;
							}
						
							if (is_rule){
								Node function_node;
								ArgP new_arg;
								int new_n_arguments;
								
1297
								new_n_arguments=add_n_new_arguments_for_local_function (arg_node->node_arguments,*n_arguments_p-1);
clean's avatar
clean committed
1298
1299
1300
1301
								
								if (new_n_arguments>MAX_N_FUNCTION_ARGUMENTS)
									break;
									
1302
								*n_arguments_p=new_n_arguments;
clean's avatar
clean committed
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313

								function_node=NewNode (arg_node->node_symbol,NULL,arg_node->node_arity);
								function_node->node_state=LazyState;
								function_node->node_number=0;
								
								new_arg=NewArgument (function_node);
								new_arg->arg_state=LazyState;
								*rhs_arg_p=new_arg;
								rhs_arg_p=&new_arg->arg_next;

								function_name_p = create_arguments_for_local_function (arg_node,arg_h,lhs_arg_h,&function_node->node_arguments,
1314
																						 function_state_p,arity_p,function_name_p,end_function_name,n_arguments_p);
clean's avatar
clean committed
1315
1316
1317
1318
1319
1320
								
								++arg_state_p;
								continue;
							}
						}
					}
1321
#ifdef THUNK_LIFT_0_CONSTRUCTORS
1322
1323
1324
1325
					else if (arg_node->node_arity==0 &&
						(arg_node->node_symbol->symb_def->sdef_kind==CONSTRUCTOR ||
						 (arg_node->node_symbol->symb_def->sdef_kind!=RECORDTYPE && arg_node->node_symbol->symb_def->sdef_arity>0))
					){
1326
1327
1328
						NodeP function_node;
						ArgP new_arg;
						
1329
						function_node=NewNode (arg_node->node_symbol,NULL,0);
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
						function_node->node_state=LazyState;
						function_node->node_number=0;
						
						new_arg=NewArgument (function_node);
						new_arg->arg_state=LazyState;
						*rhs_arg_p=new_arg;
						rhs_arg_p=&new_arg->arg_next;
						
						++arg_state_p;
						continue;
					}
#endif
clean's avatar
clean committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
					break;
				}
#ifdef UNTUPLE_STRICT_TUPLES
				case tuple_symb:
				{
					if (arg_state_p->state_type==TupleState){
						NodeP tuple_node;
						ArgP new_arg;
						int new_n_arguments;
						
1352
						new_n_arguments=add_n_new_arguments_for_local_function (arg_node->node_arguments,*n_arguments_p-1);
clean's avatar
clean committed
1353
1354
1355
1356
								
						if (new_n_arguments>MAX_N_FUNCTION_ARGUMENTS)
							break;

1357
						*n_arguments_p=new_n_arguments;
clean's avatar
clean committed
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
						
						tuple_node=NewNode (arg_node->node_symbol,NULL,arg_node->node_arity);
						tuple_node->node_state=LazyState;
						tuple_node->node_number=0;
						
						new_arg=NewArgument (tuple_node);
						new_arg->arg_state=LazyState;
						*rhs_arg_p=new_arg;
						rhs_arg_p=&new_arg->arg_next;
						
						function_name_p = create_arguments_for_local_function (arg_node,arg_h,lhs_arg_h,&tuple_node->node_arguments,
1369
																				arg_state_p->state_tuple_arguments,arity_p,function_name_p,end_function_name,n_arguments_p);
clean's avatar
clean committed
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379

						++arg_state_p;

						continue;						
					}
					break;
				}
#endif
#ifdef MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
				case apply_symb:
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
					if (arg_node->node_arity==2){
						struct arg *arg_p;
						int n_apply_args;
				
						n_apply_args=1;
						arg_p=arg_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;
						}
						if (arg_p!=NULL && arg_p->arg_node->node_kind==SelectorNode && arg_p->arg_node->node_arity==1){
							struct node *selector_node_p;

							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){
									struct symbol *new_symbol_p;

									new_symbol_p = CompAlloc (sizeof (struct symbol));
									*new_symbol_p = *arg_node->node_symbol;
									new_symbol_p->symb_instance_apply = 1;
1408
1409
									new_symbol_p->symb_apply_instance_field_def = selector_node_p->node_symbol->symb_def;

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
									arg_node->node_symbol = new_symbol_p;

									if (arg_state_p->state_type==SimpleState && (arg_state_p->state_kind==StrictOnA || arg_state_p->state_kind==StrictRedirection)
										&& OptimizeInstanceCalls)
									{
										struct state *member_states_of_field;
										struct arg *old_arg_p;
										Node function_node;
										ArgP new_arg;
										int new_n_arguments;
										
										member_states_of_field=field_sdef->sdef_member_states_of_field;

										old_arg_p = remove_apply_nodes (arg_node,n_apply_args);

										new_n_arguments=add_n_new_arguments_for_local_function (arg_node->node_arguments,*n_arguments_p-1);
										
										if (new_n_arguments>MAX_N_FUNCTION_ARGUMENTS){
											restore_removed_apply_nodes (arg_node,old_arg_p,n_apply_args);
											break;
										}
										
										*n_arguments_p=new_n_arguments;

										function_node=NewNode (arg_node->node_symbol,NULL,arg_node->node_arity);
										function_node->node_state=LazyState;
										function_node->node_number=0;
										
										new_arg=NewArgument (function_node);
										new_arg->arg_state=LazyState;
										*rhs_arg_p=new_arg;
										rhs_arg_p=&new_arg->arg_next;
										
										function_name_p = create_arguments_for_local_function (arg_node,arg_h,lhs_arg_h,&function_node->node_arguments,
																								 member_states_of_field,arity_p,function_name_p,end_function_name,n_arguments_p);
										
										++arg_state_p;

										arg_p=function_node->node_arguments;
										function_node->node_arguments=old_arg_p;
										store_args_in_apply_nodes (n_apply_args,arg_p,function_node);

										continue;
									}
								}
							}
						}
					}

clean's avatar
clean committed
1459
1460
1461
1462
1463
					if (arg_state_p->state_type==SimpleState && (arg_state_p->state_kind==StrictOnA || arg_state_p->state_kind==StrictRedirection)){
						Node function_node;
						ArgP new_arg;
						int new_n_arguments;
						
1464
						new_n_arguments=add_n_new_arguments_for_local_function (arg_node->node_arguments,*n_arguments_p-1);
clean's avatar
clean committed
1465
1466
1467
1468
								
						if (new_n_arguments>MAX_N_FUNCTION_ARGUMENTS)
							break;
						
1469
						*n_arguments_p=new_n_arguments;
clean's avatar
clean committed
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483

						function_node=NewNode (arg_node->node_symbol,NULL,arg_node->node_arity);
						function_node->node_state=LazyState;
						function_node->node_number=0;
						
						new_arg=NewArgument (function_node);
						new_arg->arg_state=LazyState;
						*rhs_arg_p=new_arg;
						rhs_arg_p=&new_arg->arg_next;
						
						if (apply_symb_function_state_p==NULL)
							init_apply_symb_function_state_p();

						function_name_p = create_arguments_for_local_function (arg_node,arg_h,lhs_arg_h,&function_node->node_arguments,
1484
																				 apply_symb_function_state_p,arity_p,function_name_p,end_function_name,n_arguments_p);
clean's avatar
clean committed
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
						
						++arg_state_p;
						continue;
					}
					break;
#endif
#ifdef THUNK_LIFT_SELECTORS
				case select_symb:
				{
					NodeP tuple_node_p;
					NodeDefP node_def_p;
					
					if (arg_node->node_arguments->arg_node->node_kind==NodeIdNode &&
1498
						!IsLazyState (*arg_state_p) &&
clean's avatar
clean committed
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
						arg_node->node_arguments->arg_node->node_node_id->nid_refcount>0 &&
						IsLazyState ((tuple_node_p=(node_def_p=arg_node->node_arguments->arg_node->node_node_id->nid_node_def)->def_node)->node_state) &&
						tuple_node_p->node_kind==NormalNode &&
						tuple_node_p->node_symbol->symb_kind==definition &&
						(tuple_node_p->node_symbol->symb_def->sdef_kind==IMPRULE ||
						 tuple_node_p->node_symbol->symb_def->sdef_kind==DEFRULE ||
						 tuple_node_p->node_symbol->symb_def->sdef_kind==SYSRULE) &&
						tuple_node_p->node_arity==tuple_node_p->node_symbol->symb_def->sdef_arity)
					{
						Node function_node;
						ArgP new_arg;
						int new_n_arguments;
						
1512
						new_n_arguments=add_n_new_arguments_for_local_function (arg_node->node_arguments,*n_arguments_p-1);
clean's avatar
clean committed
1513
1514
1515
						
						if (new_n_arguments>MAX_N_FUNCTION_ARGUMENTS)
							break;
1516
						
1517
						*n_arguments_p=new_n_arguments;
clean's avatar
clean committed
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530

						function_node=NewNode (arg_node->node_symbol,NULL,arg_node->node_arity);
						function_node->node_state=LazyState;
						function_node->node_number=1;
						
						node_def_p->def_mark |= NODE_DEF_SELECT_AND_REMOVE_MASK;
						
						new_arg=NewArgument (function_node);
						new_arg->arg_state=LazyState;
						*rhs_arg_p=new_arg;
						rhs_arg_p=&new_arg->arg_next;

						function_name_p = create_arguments_for_local_function (arg_node,arg_h,lhs_arg_h,&function_node->node_arguments,
1531
																				 &StrictState,arity_p,function_name_p,end_function_name,n_arguments_p);
clean's avatar
clean committed
1532
1533
1534
1535
1536
1537
1538
1539
						
						++arg_state_p;	
						continue;
					}
					break;
				}
#endif
			}
1540
1541
1542
1543
#ifdef THUNK_LIFT_U_RECORD_SELECTORS
		else if (arg_node->node_kind==SelectorNode && arg_node->node_arity>=SELECTOR_U && arg_state_p->state_type==TupleState){
			int new_n_arguments;

1544
			new_n_arguments=add_n_new_arguments_for_local_function (arg_node->node_arguments,*n_arguments_p-1);
1545
1546
1547
1548
1549
1550
			
			if (new_n_arguments<=MAX_N_FUNCTION_ARGUMENTS){
				Node selector_node;
				ArgP new_arg;
				StateP selector_arg_state_p;
				StateS tuple_state,tuple_arg_states[2];
1551

1552
				*n_arguments_p=new_n_arguments;
1553
1554
1555
1556
1557
1558
1559
1560
1561

				selector_node=NewSelectorNode (arg_node->node_symbol,NULL,arg_node->node_arity);
				selector_node->node_state=LazyState;
				selector_node->node_number=0;

				new_arg=NewArgument (selector_node);
				new_arg->arg_state=LazyState;
				*rhs_arg_p=new_arg;
				rhs_arg_p=&new_arg->arg_next;
1562

1563
				selector_arg_state_p=&arg_node->node_symbol->symb_def->sdef_type->type_symbol->symb_def->sdef_record_state;
1564
1565
1566

				if (arg_node->node_arity>=SELECTOR_L)
					selector_arg_state_p=selector_l_or_n_state_p (&tuple_state,tuple_arg_states,selector_arg_state_p);
clean's avatar
clean committed
1567

1568
				function_name_p = create_arguments_for_local_function (arg_node,arg_h,lhs_arg_h,&selector_node->node_arguments,
1569
1570
																		 selector_arg_state_p,arity_p,function_name_p,end_function_name,n_arguments_p);

1571
1572
				++arg_state_p;	
				continue;
1573
			}
1574
1575
		}
#endif
1576

clean's avatar
clean committed
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
		if (arg_node->node_kind==NodeIdNode && (arg_node->node_node_id->nid_mark & NID_LIFTED_BY_OPTIMISE) && arg_node->node_node_id->nid_forward_node_id!=NULL){
			arg_node_id=arg_node->node_node_id->nid_forward_node_id;
			--arg_node_id->nid_refcount;
			--arg_node_id->nid_ref_count_copy__;
		} else {
			arg_node_id=NewNodeId (NULL);
			arg_node_id->nid_refcount=-2;
			arg_node_id->nid_ref_count_copy__=-2;
			
			if (arg_node->node_kind==NodeIdNode){
				NodeIdP node_id;
				
				node_id=arg_node->node_node_id;

				node_id->nid_forward_node_id_=arg_node_id;
				arg_node_id->nid_forward_node_id_=node_id;
				node_id->nid_mark |= NID_LIFTED_BY_OPTIMISE;
				arg_node_id->nid_mark |= NID_LIFTED_BY_OPTIMISE;

				if (node_id->nid_refcount<0){
					call_state_p=node_id->nid_lhs_state_p;
				} else
					call_state_p=&node_id->nid_node->node_state;
1600
			} else if (arg_node->node_kind==NormalNode){
clean's avatar
clean committed
1601
#ifdef STRICT_STATE_FOR_LAZY_TUPLE_CONSTRUCTORS
1602
				if (BETWEEN (tuple_symb,nil_symb,arg_node->node_symbol->symb_kind)
clean's avatar
clean committed
1603
1604
1605
1606
1607
					&& arg_node->node_state.state_type==SimpleState && arg_node->node_state.state_kind==OnA)
				{
					call_state_p=&StrictState;
				} else
#endif
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
				if (arg_node->node_symbol->symb_kind==definition
					&& arg_node->node_state.state_type==SimpleState && arg_node->node_state.state_kind==OnA)
				{
					SymbDef sdef;

					sdef=arg_node->node_symbol->symb_def;

					if (sdef->sdef_kind!=RECORDTYPE && arg_node->node_arity<sdef->sdef_arity)
						call_state_p=&StrictState;
					else
						call_state_p=&arg_node->node_state;
				} else
					call_state_p=&arg_node->node_state;
			} else
clean's avatar
clean committed
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
				call_state_p=&arg_node->node_state;

			lhs_arg=NewArgument (NewNodeIdNode (arg_node_id));
			lhs_arg->arg_state=LazyState;
			if (!IsLazyState (*call_state_p)){
				if (call_state_p->state_type==SimpleState && call_state_p->state_kind==OnB)
					lhs_arg->arg_state=*call_state_p;
				else if (call_state_p->state_type==ArrayState)
					lhs_arg->arg_state=*call_state_p;
				else
					lhs_arg->arg_state.state_kind=StrictOnA;
			}
			
			arg_node_id->nid_lhs_state_p_=&lhs_arg->arg_state;

			++*arity_p;

			**lhs_arg_h=lhs_arg;
			*lhs_arg_h=&lhs_arg->arg_next;
1641
			
clean's avatar
clean committed
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
			**arg_h=arg;
			*arg_h=&arg->arg_next;
		}

		++arg_state_p;

		rhs_arg=NewArgument (NewNodeIdNode (arg_node_id));
		rhs_arg->arg_state=LazyState;
		*rhs_arg_p=rhs_arg;
		rhs_arg_p=&rhs_arg->arg_next;
	}
	
	*rhs_arg_p=NULL;
1655
		
clean's avatar
clean committed
1656
1657
1658
	return function_name_p;
}

1659
static struct node *create_new_local_function (Node node,StateP function_state_p)
clean's avatar
clean committed
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
{
	static char function_name[64];
	Symbol function_symbol;
	int n_arguments,function_arity;
	ImpRuleS *imp_rule;
	ArgS **lhs_arg_p,**arg_p;
	Node lhs_root,rhs_root;
	char *function_name_p,*end_function_name;

	n_arguments = add_n_new_arguments_for_local_function (node->node_arguments,0);
	
	if (n_arguments>MAX_N_FUNCTION_ARGUMENTS)
1672
		return NULL;
clean's avatar
clean committed
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

	sprintf (function_name,"_f%d",next_function_n);
	++next_function_n;

	if (DoTimeProfiling || DoProfiling){
		char *f_name;
		int length_before_type_delimiter;
	
		end_function_name=function_name+sizeof (function_name);
		function_name_p=&function_name[strlen (function_name)];
		
		f_name=CurrentSymbol->symb_def->sdef_ident->ident_name;
		length_before_type_delimiter=compute_length_before_type_delimiter (f_name);
		
		if (function_name_p+2+length_before_type_delimiter < end_function_name){
			*function_name_p++='.';
			function_name_p=append_n_chars (function_name_p,f_name,length_before_type_delimiter);
		} else
			end_function_name=function_name_p;
	} else {
		function_name_p=NULL;
		end_function_name=NULL;
	}

	lhs_root=NewNode (NULL,NULL,0);
	lhs_root->node_state=StrictState;
1699
1700
1701
1702

#ifdef THUNK_LIFT_U_RECORD_SELECTORS
	rhs_root=NewNodeByKind (node->node_kind,node->node_symbol,NULL,node->node_arity);
#else
clean's avatar
clean committed
1703
	rhs_root=NewNode (node->node_symbol,NULL,node->node_arity);
1704
#endif
clean's avatar
clean committed
1705
1706
1707
1708
1709
1710
1711
1712
1713
	rhs_root->node_state=LazyState;
	rhs_root->node_number=0;
	
	function_arity=0;

	lhs_arg_p=&lhs_root->node_arguments;
	arg_p=&node->node_arguments;

	function_name_p = create_arguments_for_local_function (node,&arg_p,&lhs_arg_p,&rhs_root->node_arguments,function_state_p,
1714
															&function_arity,function_name_p,end_function_name,&n_arguments);
clean's avatar
clean committed
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
	
	if (function_name_p!=NULL)
		*function_name_p='\0';

	function_symbol=new_rule_symbol (function_name);
	lhs_root->node_symbol=function_symbol;
	
	*lhs_arg_p=NULL;
	*arg_p=NULL;

	{
		ArgP arg;
		
		for_l (arg,lhs_root->node_arguments,arg_next){
			NodeIdP lhs_node_id,rhs_node_id;
			
			lhs_node_id=arg->arg_node->node_node_id;
			if (lhs_node_id->nid_mark & NID_LIFTED_BY_OPTIMISE){
				rhs_node_id=lhs_node_id->nid_forward_node_id;
				lhs_node_id->nid_mark &= ~NID_LIFTED_BY_OPTIMISE;
				rhs_node_id->nid_mark &= ~NID_LIFTED_BY_OPTIMISE;
			}
		}
	}

	lhs_root->node_arity=function_arity;
	function_symbol->symb_def->sdef_arity=function_arity;
	
1743
1744
1745
#ifdef THUNK_LIFT_U_RECORD_SELECTORS
	node->node_kind=NormalNode;
#endif
clean's avatar
clean committed
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
	node->node_symbol=function_symbol;
	node->node_arity=function_arity;

	imp_rule=create_simple_imp_rule (lhs_root,rhs_root,function_symbol->symb_def);
	
	{
		StateP arg_state_p;
		ArgP arg_p;
		
		arg_state_p=allocate_function_state (function_arity);

		imp_rule->rule_state_p=arg_state_p;

		arg_state_p[-1]=StrictState;
		
		for_l (arg_p,lhs_root->node_arguments,arg_next)
			*arg_state_p++ = arg_p->arg_state;
	}

	imp_rule->rule_next=new_rules;
	new_rules=imp_rule;
1767
1768
	
	return rhs_root;
clean's avatar
clean committed
1769
1770
}

1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
static int is_optimisable_argument (NodeP arg_node,StateP function_arg_state_p)
{
	if (arg_node->node_kind==NormalNode){
#ifdef THUNK_LIFT_SELECTORS
		NodeP tuple_node_p;
#endif
		if (arg_node->node_symbol->symb_kind==definition){
			if ((function_arg_state_p->state_type==SimpleState && function_arg_state_p->state_kind==OnB)
#ifdef MOVE_TUPLE_RECORD_AND_ARRAY_RESULT_FUNCTION_ARGUMENT_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
				|| function_arg_state_p->state_type==TupleState || function_arg_state_p->state_type==RecordState || function_arg_state_p->state_type==ArrayState
#endif
			){
				SymbDef sdef;
				
				unsigned kind;
					
				sdef=arg_node->node_symbol->symb_def;
				kind=sdef->sdef_kind;
				
1790
				if (arg_node->node_arity==sdef->sdef_arity){
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
					if (kind==IMPRULE || kind==DEFRULE || kind==SYSRULE || (kind==RECORDTYPE && sdef->sdef_strict_constructor))
						return 1;
				}
			}
		}
#ifdef UNTUPLE_STRICT_TUPLES
		else if (arg_node->node_symbol->symb_kind==tuple_symb && function_arg_state_p->state_type==TupleState)
			return 1;
#endif
#ifdef MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
		else if (arg_node->node_symbol->symb_kind==apply_symb && function_arg_state_p->state_type==SimpleState &&
				(function_arg_state_p->state_kind==StrictOnA || function_arg_state_p->state_kind==StrictRedirection))
			return 1;
#endif
#ifdef THUNK_LIFT_SELECTORS
		else if (arg_node->node_symbol->symb_kind==select_symb && 
				 !IsLazyState (*function_state_p) &&
			 	 arg_node->node_arguments->arg_node->node_kind==NodeIdNode &&
				 arg_node->node_arguments->arg_node->node_node_id->nid_refcount>0 &&
				 IsLazyState ((tuple_node_p=arg_node->node_arguments->arg_node->node_node_id->nid_node_def->def_node)->node_state) &&
				 tuple_node_p->node_kind==NormalNode && tuple_node_p->node_symbol->symb_kind==definition &&
				 (tuple_node_p->node_symbol->symb_def->sdef_kind==IMPRULE ||
				  tuple_node_p->node_symbol->symb_def->sdef_kind==DEFRULE ||
				  tuple_node_p->node_symbol->symb_def->sdef_kind==SYSRULE) &&
				 tuple_node_p->node_arity==tuple_node_p->node_symbol->symb_def->sdef_arity)
		{
			return 1;
		}
#endif
	}
#ifdef THUNK_LIFT_U_RECORD_SELECTORS
	else if (arg_node->node_kind==SelectorNode && arg_node->node_arity>=SELECTOR_U && function_arg_state_p->state_type==TupleState)
		return 1;
#endif
	
	return 0;
}

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
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
1903
1904
1905
1906
1907
1908
1909
1910
static int has_optimisable_argument (NodeP node,StateP function_state_p)
{
	ArgP arg;
	int arg_n;

	arg=node->node_arguments;

	for (arg_n=0; arg_n<node->node_arity; ++arg_n){
		if (is_optimisable_argument (arg->arg_node,&function_state_p[arg_n]))
			return 1;

		arg=arg->arg_next;
	}
	
	return 0;
}

static int can_build_strict_constructor_or_record_in_lazy_context (NodeP node_p,StateP demanded_states)
{
	ArgP offered_arg;
	StateP demanded_state_p;
	
	for_la (offered_arg,demanded_state_p,node_p->node_arguments,demanded_states,arg_next){
		Node arg_node;
		NodeKind node_kind;
		
		arg_node=offered_arg->arg_node;
		node_kind=(NodeKind)arg_node->node_kind;

		if (node_kind!=NodeIdNode){
			if (node_kind==NormalNode){
				Symbol symbol;

				symbol=arg_node->node_symbol;

				if (BETWEEN (int_denot,real_denot,symbol->symb_kind) || symbol->symb_kind==string_denot)
					continue;

				if (symbol->symb_kind==definition){
					SymbDef sdef;

					sdef=symbol->symb_def;
					if (sdef->sdef_kind!=RECORDTYPE){
						if (arg_node->node_arity<sdef->sdef_arity || (arg_node->node_arity==0 && sdef->sdef_kind==CONSTRUCTOR))
							continue;
					} else {
						if (demanded_state_p->state_type==RecordState && arg_node->node_state.state_type==SimpleState){
							if (arg_node->node_state.state_kind==StrictOnA)
								continue;

							if (arg_node->node_state.state_kind==OnA){
								if (!sdef->sdef_strict_constructor)
									continue;
								if (can_build_strict_constructor_or_record_in_lazy_context (arg_node,sdef->sdef_record_state.state_record_arguments))
									continue;
							}
						}
					}
				}
			}
			if (!FirstStateIsStricter (arg_node->node_state,*demanded_state_p))
				return 0;
		} else {
			struct node_id *node_id;

			node_id=arg_node->node_node_id;
			if (node_id->nid_refcount<0){
				if (!FirstStateIsStricter (*node_id->nid_lhs_state_p,*demanded_state_p))
					return 0;
			} else {
				if (node_id->nid_node==NULL)
					error_in_function ("can_build_strict_constructor_or_record_in_lazy_context");

				if (!FirstStateIsStricter (node_id->nid_node->node_state,*demanded_state_p))
					return 0;
			}
		}
	}
	
	return 1;
}

1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
#ifdef MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
static void optimise_instance_call (NodeP node,int n_apply_args,struct state *member_states_of_field)
{
	int arg_n;
	
	arg_n=n_apply_args;
	if (arg_n>1 || is_optimisable_argument (node->node_arguments->arg_next->arg_node,&member_states_of_field[1])){
		struct arg *old_arg_p;
		struct node *rhs_root_of_function;
		
		old_arg_p = remove_apply_nodes (node,n_apply_args);
		
		rhs_root_of_function = create_new_local_function (node,member_states_of_field);
		
		if (rhs_root_of_function!=NULL){
			struct arg *arg_p;
			
			arg_p=rhs_root_of_function->node_arguments;
			rhs_root_of_function->node_arguments=old_arg_p;
			store_args_in_apply_nodes (n_apply_args,arg_p,rhs_root_of_function);
		} else
			restore_removed_apply_nodes (node,old_arg_p,n_apply_args);
	}
}
#endif

clean's avatar
clean committed
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
static void optimise_normal_node (Node node)
{	
	Symbol symbol;
	StateP function_state_p;

	symbol=node->node_symbol;

	if (node->node_state.state_type!=SimpleState || node->node_state.state_kind!=OnA)
		return;

	if (symbol->symb_kind!=definition){
#ifndef STRICT_STATE_FOR_LAZY_TUPLE_CONSTRUCTORS
		if (BETWEEN (int_denot,real_denot,symbol->symb_kind) || symbol->symb_kind==string_denot){
#else
		if ((BETWEEN (int_denot,real_denot,symbol->symb_kind)
			 || symbol->symb_kind==string_denot
1953
# if STRICT_LISTS
1954
			 || (BETWEEN (tuple_symb,nil_symb,symbol->symb_kind) && !(symbol->symb_kind==cons_symb && (symbol->symb_head_strictness>1 || symbol->symb_tail_strictness)))
1955
# else
clean's avatar
clean committed
1956
			 || BETWEEN (tuple_symb,nil_symb,symbol->symb_kind)
1957
# endif
clean's avatar
clean committed
1958
1959
1960
1961
1962
1963
1964
			) && node->node_state.state_kind==OnA){
#endif
			node->node_state.state_kind=StrictOnA;
			return;
		}
#ifdef MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
		else if (symbol->symb_kind==apply_symb){
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
			if (node->node_arity==2 && node->node_symbol->symb_instance_apply==0){
				struct arg *arg_p;
				int n_apply_args;
				
				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;
				}
				if (arg_p!=NULL && arg_p->arg_node->node_kind==SelectorNode && arg_p->arg_node->node_arity==1){
					struct node *selector_node_p;
					
					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){
							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;
1993
							new_symbol_p->symb_apply_instance_field_def = selector_node_p->node_symbol->symb_def;
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
							node->node_symbol = new_symbol_p;

							if (OptimizeInstanceCalls){
								optimise_instance_call (node,n_apply_args,field_sdef->sdef_member_states_of_field);
								return;
							}
						}
					}
				}
			}

clean's avatar
clean committed
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
			if (apply_symb_function_state_p==NULL)
				init_apply_symb_function_state_p();
			function_state_p=apply_symb_function_state_p;
		} else
#endif		
		return;
	}
#ifdef MOVE_APPLY_NODES_IN_LAZY_CONTEXT_TO_NEW_FUNCTION
	else
#endif
	{
		SymbDef sdef;

		sdef=symbol->symb_def;

2020
2021
2022
		if (node->node_arity!=sdef->sdef_arity){
			if (sdef->sdef_kind!=RECORDTYPE && node->node_arity<sdef->sdef_arity)
				node->node_state.state_kind=StrictOnA;
clean's avatar
clean committed
2023
			return;
2024
		}
clean's avatar
clean committed
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040

		switch (sdef->sdef_kind){
			case IMPRULE:
# if OPTIMIZE_LAZY_TUPLE_RECURSION
				if (sdef->sdef_rule->rule_mark & RULE_CALL_VIA_LAZY_SELECTIONS_ONLY)
					return;
# endif
				function_state_p=sdef->sdef_rule->rule_state_p;
				break;
			case DEFRULE:
			case SYSRULE:
				function_state_p=sdef->sdef_rule_type->rule_type_state_p;
				break;
			case CONSTRUCTOR:
				if (sdef->sdef_strict_constructor){
					function_state_p=sdef->sdef_constructor->cl_state_p;
2041
2042
2043
2044
2045
2046

					if (has_optimisable_argument (node,function_state_p)
						&& !can_build_strict_constructor_or_record_in_lazy_context (node,function_state_p))
						create_new_local_function (node,function_state_p);

					return;
clean's avatar
clean committed
2047
2048
2049
2050
2051
2052
2053
				} else
					return;
			default:
				return;
		}
	}

2054
2055
	if (has_optimisable_argument (node,function_state_p))
		create_new_local_function (node,function_state_p);
clean's avatar
clean committed
2056
2057
2058
2059
}

static int ChangeArgumentNodeStatesIfStricter (NodeP node_p,StateP demanded_states)
{
2060
	ArgP offered_arg;
clean's avatar
clean committed
2061
2062
	StateP demanded_state_p;
	
2063
	for_la (offered_arg,demanded_state_p,node_p->node_arguments,demanded_states,arg_next){
clean's avatar
clean committed
2064
2065
2066
		Node arg_node;
		NodeKind node_kind;
		
2067
		arg_node=offered_arg->arg_node;
clean's avatar
clean committed
2068
2069
2070
2071 </