instructions.c 86.1 KB
Newer Older
clean's avatar
clean committed
1
2
3
4
5
6
7
8
/*
	(Concurrent) Clean Compiler: ABC instructions
	Authors:  Sjaak Smetsers & John van Groningen
*/

#pragma segment instructions

#include "compiledefines.h"
9
#include "comsupport.h"
clean's avatar
clean committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <ctype.h>

#include "syntaxtr.t"
#include "checksupport.h"

#include "settings.h"
#include "sizes.h"
#include "codegen_types.h"
#include "codegen1.h"
#include "codegen2.h"
#include "instructions.h"
#include "statesgen.h"
#include "version.h"

John van Groningen's avatar
John van Groningen committed
25
26
#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)

clean's avatar
clean committed
27
28
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
89
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
#define BINARY_ABC 0
#undef MEMORY_PROFILING_WITH_N_STRING

#define PutSOutFile(s) FPutS ((s),OutFile)
#define PutCOutFile(s) FPutC ((s),OutFile)

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

/* also defined in project.c, only needed for stand alone compiler */

#define N_DoDebug				0
#define N_DoReuseUniqueNodes	1
#define N_DoParallel			2

#define N_NoDescriptors 3
/*
#define N_NoMemoryProfiling		3
*/
#define N_DoStrictnessAnalysis	4
#define N_NoTimeProfiling		5

#define N_ExportLocalLabels 6
/*
#define N_DoVerbose				6
*/
#define N_DoWarning				7
#define N_System				8

static void ConvertOptionsToString (CompilerOptions options,char *optstring)
{
	optstring[N_DoDebug]              = DoDebug ? '1' : '0';
	optstring[N_DoReuseUniqueNodes]   = !DoReuseUniqueNodes ? '1' : '0';
	optstring[N_DoParallel]           = DoParallel ? '1' : '0';

	optstring[N_NoDescriptors] = !DoDescriptors ? '1' : '0';
/*
	optstring[N_NoMemoryProfiling]    = !DoProfiling ? '1' : '0';
*/
	optstring[N_DoStrictnessAnalysis] = DoStrictnessAnalysis ? '1' : '0';

	optstring[N_NoTimeProfiling]      = !DoTimeProfiling ? '1' : '0';
	optstring[N_ExportLocalLabels] = ExportLocalLabels ? '1' : '0';
/*
	optstring[N_DoVerbose]            = DoVerbose ? '1' : '0';
*/
	optstring[N_DoWarning]            = DoWarning ? '1' : '0';
	optstring[N_System]               = '0';
	optstring[NR_OPTIONS]             = '\0';
}

#define D_PREFIX "d"
#define N_PREFIX "n"
#define L_PREFIX "l"

#define EA_PREFIX "ea"
#define S_PREFIX "s"

#define R_PREFIX "r"
#define RECORD_N_PREFIX "c"
#define RECORD_D_PREFIX "t"
#define CONSTRUCTOR_R_PREFIX "k"

#define LOCAL_D_PREFIX "d"

File OutFile;
char *ABCFileName;

Bool OpenABCFile (char *fname)
{
	OutFile = FOpen (fname, abcFile, "w");

	if (OutFile!=NULL){
#if defined (THINK_C) || defined (POWER)
		setvbuf ((FILE*) OutFile, NULL, _IOFBF, 8192);
#endif
		OpenedFile = OutFile;
		ABCFileName = fname;
		return True;
	} else
		return False;
}

void WriteLastNewlineToABCFile (void)
{
	FPutC ('\n',OutFile);
}

void CloseABCFile (char *fname)
{
	if (OutFile){
#ifdef THINK_C
		int file_io_error;
		
		file_io_error=ferror (OutFile);
#endif
		if (FClose (OutFile) != 0
#ifdef THINK_C
		||	file_io_error
#endif
		){
			CompilerError = True;
			CurrentLine = 0;
			
			StaticMessage (True, "<open file>", "Write error (disk full?)");
		}
		if (CompilerError)
			FDelete (fname, abcFile); 
		OpenedFile = (File) NIL;
	}
}

static Bool DescriptorNeeded (SymbDef sdef)
{
	return (sdef->sdef_exported || 
			(sdef->sdef_kind!=IMPRULE && sdef->sdef_kind!=SYSRULE) || 
			sdef->sdef_mark & SDEF_USED_CURRIED_MASK) ||
			((DoParallel || DoDescriptors) && (sdef->sdef_mark & (SDEF_USED_CURRIED_MASK | SDEF_USED_LAZILY_MASK)));
}

static void GenLabel (Label label)
{
	if (label->lab_issymbol){
		SymbDef def;
		char *module_name;

		def=label->lab_symbol;
		module_name = label->lab_mod;
		
		if (module_name!=NULL)
			FPrintF (OutFile,"e_%s_%s%s",module_name,label->lab_pref,def->sdef_ident->ident_name);
		else if (DoDebug){
			if (def->sdef_kind==IMPRULE)
				FPrintF (OutFile, "%s%s.%u",label->lab_pref,def->sdef_ident->ident_name,def->sdef_number);
			else
				FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_ident->ident_name);
		} else if (def->sdef_number==0)
			FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_ident->ident_name);
		else if (label->lab_pref[0] == '\0')
			FPrintF (OutFile,LOCAL_D_PREFIX "%u",def->sdef_number);
		else
			FPrintF (OutFile,"%s%u",label->lab_pref,def->sdef_number);
	} else {
		FPutS (label->lab_pref,OutFile);
		FPutS (label->lab_name,OutFile);
	}
	if (label->lab_post!=0)
		FPrintF (OutFile,".%u",label->lab_post);
}

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
static void GenDescriptorOrNodeEntryLabel (Label label)
{
	if (label->lab_issymbol){
		SymbDef def;
		char *module_name;

		def=label->lab_symbol;
		module_name = label->lab_mod;
		
		if (module_name!=NULL)
			FPrintF (OutFile,"e_%s_%s%s",module_name,label->lab_pref,def->sdef_ident->ident_name);
		else if (ExportLocalLabels){
			if (def->sdef_kind==IMPRULE)
				FPrintF (OutFile,"e_%s_%s%s.%u",CurrentModule,label->lab_pref,def->sdef_ident->ident_name,def->sdef_number);
			else
				FPrintF (OutFile,"e_%s_%s%s",CurrentModule,label->lab_pref,def->sdef_ident->ident_name);
		} else if (DoDebug){
			if (def->sdef_kind==IMPRULE)
				FPrintF (OutFile, "%s%s.%u",label->lab_pref,def->sdef_ident->ident_name,def->sdef_number);
			else
				FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_ident->ident_name);
		} else if (def->sdef_number==0)
			FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_ident->ident_name);
		else if (label->lab_pref[0] == '\0')
			FPrintF (OutFile,LOCAL_D_PREFIX "%u",def->sdef_number);
		else
			FPrintF (OutFile,"%s%u",label->lab_pref,def->sdef_number);
	} else {
		FPutS (label->lab_pref,OutFile);
		FPutS (label->lab_name,OutFile);
	}
	if (label->lab_post!=0)
		FPrintF (OutFile,".%u",label->lab_post);
}

clean's avatar
clean committed
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
static void GenGetWL (int offset)
{
	FPrintF (OutFile, "\n\tgetWL %d", offset);
}

static void GenPutWL (int offset)
{
	FPrintF (OutFile, "\n\tputWL %d", offset);
}

static void GenRelease (void)
{
	FPutS ("\n\trelease", OutFile);
}

static void TreatWaitListBeforeFill (int offset, FillKind fkind)
{
	if (DoParallel && fkind != NormalFill)
		GenGetWL (offset);	
}

static void TreatWaitListAfterFill (int offset, FillKind fkind)
{
	if (DoParallel){
		switch (fkind){
			case ReleaseAndFill:GenRelease ();		break;
			case PartialFill:	GenPutWL (offset);	break;
			default:							break;
		}
	}
}

#if !BINARY_ABC

#define put_instructionb(a) put_instruction(I##a)
#define put_instruction_b(a) put_instruction_(I##a)
#define put_directive_b(a) put_directive_(D##a)
#define put_arguments_i_b(i1) FPrintF (OutFile,"%s",(i1))
#define put_arguments_in_b(i1,n1) FPrintF (OutFile,"%s %d",(i1),(n1))
#define put_arguments_n_b(n1) FPrintF (OutFile,"%d",(n1))
#define put_arguments_nn_b(n1,n2) FPrintF (OutFile,"%d %d",(n1),(n2))
#define put_arguments_nnn_b(n1,n2,n3) FPrintF (OutFile,"%d %d %d",(n1),(n2),(n3))
#define put_arguments_nnnn_b(n1,n2,n3,n4) FPrintF (OutFile,"%d %d %d %d",(n1),(n2),(n3),(n4))
#define put_arguments_nnnnn_b(n1,n2,n3,n4,n5) FPrintF (OutFile,"%d %d %d %d %d",(n1),(n2),(n3),(n4),(n5))
258
#define put_arguments_nnnnnnn_b(n1,n2,n3,n4,n5,n6,n7) FPrintF (OutFile,"%d %d %d %d %d %d %d",(n1),(n2),(n3),(n4),(n5),(n6),(n7))
clean's avatar
clean committed
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
322
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
417
418
419
420
421
422
423
424
425
426
427
428
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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#define put_arguments_n__b(n1) FPrintF (OutFile,"%d ",(n1))
#define put_arguments_nn__b(n1,n2) FPrintF (OutFile,"%d %d ",(n1),(n2))
#define put_arguments__n_b(n1) FPrintF (OutFile," %d",(n1))
#define put_arguments__nn_b(n1,n2) FPrintF (OutFile," %d %d",(n1),(n2))
#define put_arguments__n__b(n1) FPrintF (OutFile," %d ",(n1))

#else

/*
#define put_instructionb(a) put_instruction_code(C##a)
#define put_instruction_b(a) put_instruction_code(C##a)
#define put_directive_b(a) put_instruction_code(C##a)
*/

#define put_instructionb(a) if (DoDebug) put_instruction(I##a); else put_instruction_code(C##a)
#define put_instruction_b(a) if (DoDebug) put_instruction_(I##a); else put_instruction_code(C##a)
#define put_directive_b(a) if (DoDebug) put_directive_(D##a); else put_instruction_code(C##a)

static void put_n (long n)
{
	while (!(n>=-64 && n<=63)){
		FPutC (128+(n & 127),OutFile);
		n=n>>7;
	}

	FPutC (n+64,OutFile);
}

static long integer_string_to_integer (char *s_p)
{
	long integer;
	int minus_sign,last_char;
	
	minus_sign=0;
	last_char=*s_p++;
	if (last_char=='+' || last_char=='-'){
		if (last_char=='-')
			minus_sign=!minus_sign;
		last_char=*s_p++;;
	}
			
	integer=last_char-'0';
	last_char=*s_p++;;
	
	while ((unsigned)(last_char-'0')<10u){
		integer*=10;
		integer+=last_char-'0';
		last_char=*s_p++;;
	}
		
	if (minus_sign)
		integer=-integer;
	
	return integer;
}

static void put_arguments_i_b (char *i1)
{
	if (DoDebug)
		FPrintF (OutFile,"%s",(i1));
	else
		put_n (integer_string_to_integer (i1));
}

static void put_arguments_in_b (char *i1,long n1)
{
	if (DoDebug)
		FPrintF (OutFile,"%s %d",(i1),(n1));
	else {
		put_n (integer_string_to_integer (i1));
		put_n (n1);
	}
}

static void put_arguments_n_b (long n1)
{
	if (DoDebug)
		FPrintF (OutFile,"%d",(n1));
	else
		put_n (n1);
}

static void put_arguments_nn_b (long n1,long n2)
{
	if (DoDebug)
		FPrintF (OutFile,"%d %d",(n1),(n2));
	else {
		put_n (n1);
		put_n (n2);
	}
}

static void put_arguments_nnn_b (long n1,long n2,long n3)
{
	if (DoDebug)
		FPrintF (OutFile,"%d %d %d",(n1),(n2),(n3));
	else {
		put_n (n1);
		put_n (n2);
		put_n (n3);
	}
}

static void put_arguments_nnnn_b (long n1,long n2,long n3,long n4)
{
	if (DoDebug)	
		FPrintF (OutFile,"%d %d %d %d",(n1),(n2),(n3),(n4));
	else {
		put_n (n1);
		put_n (n2);
		put_n (n3);
		put_n (n4);
	}
}

static void put_arguments_nnnnn_b (long n1,long n2,long n3,long n4,long n5)
{
	if (DoDebug)
		FPrintF (OutFile,"%d %d %d %d %d",(n1),(n2),(n3),(n4),(n5));
	else {
		put_n (n1);
		put_n (n2);
		put_n (n3);
		put_n (n4);
		put_n (n5);
	}
}

static void put_arguments_n__b (long n1)
{
	if (DoDebug)
		FPrintF (OutFile,"%d ",(n1));
	else
		put_n (n1);
}

static void put_arguments__n_b (long n1)
{
	if (DoDebug)
		FPrintF (OutFile," %d",(n1));
	else {
		FPutC (' ',OutFile);
		put_n (n1);
	}
}

static void put_arguments__n__b (long n1)
{
	if (DoDebug)
		FPrintF (OutFile," %d ",(n1));
	else {
		FPutC (' ',OutFile);
		put_n (n1);
	}
}

static void put_arguments_nn__b (long n1,long n2)
{
	if (DoDebug)
		FPrintF (OutFile,"%d %d ",(n1),(n2));
	else {
		put_n (n1);
		put_n (n2);
	}
}

static void put_arguments__nn_b (long n1,long n2)
{
	if (DoDebug)
		FPrintF (OutFile," %d %d",(n1),(n2));
	else {
		FPutC (' ',OutFile);
		put_n (n1);
		put_n (n2);
	}
}

enum {
	Cbuild=136,
	Cbuildh,
	CbuildI,
	CbuildB_b,
	CbuildC_b,
	CbuildI_b,
	CbuildR_b,
	CbuildF_b,
	Ceq_desc,
	CeqD_b,
	CeqI_a,
	CeqI_b,
	Cfill,
	Cfillh,
	CfillI,
	CfillB_b,
	CfillC_b,
	CfillF_b,
	CfillI_b,
	CfillR_b,
	Cfill_a,
	Cjmp,
	Cjmp_false,
	Cjmp_true,
	Cjsr,
	Cjsr_eval,
	Cpop_a,
	Cpop_b,
	CpushB_a,
	CpushC_a,
	CpushI_a,
	CpushF_a,
	CpushR_a,
	CpushD,
	CpushI,
	Cpush_a,
	Cpush_b,
	Cpush_arg,
	Cpush_args,
	Cpush_args_u,
	Cpush_node,
	Cpush_node_u,
	Cpush_r_args,
	Cpush_r_args_a,
	Cpush_r_args_b,
	Cpush_r_args_u,
	Crepl_arg,
	Crepl_args,
	Crepl_r_args,
	Crepl_r_args_a,
	Crtn,
	Cupdate_a,
	Cupdate_b,
	Cupdatepop_a,
	Cupdatepop_b,
	
	Cd,
	Co,
	Cimpdesc,
	Cimplab,
	Cn
};
#endif

#define IbuildB "buildB"
#define IbuildC "buildC"
#define IbuildI "buildI"
#define IbuildR "buildR"
#define IbuildS "buildS"

#define IbuildB_b "buildB_b"
#define IbuildC_b "buildC_b"
#define IbuildF_b "buildF_b"
#define IbuildI_b "buildI_b"
#define IbuildR_b "buildR_b"

#define IfillB "fillB"
#define IfillC "fillC"
#define IfillI "fillI"
#define IfillR "fillR"
#define IfillS "fillS"

#define IfillB_b "fillB_b"
#define IfillC_b "fillC_b"
#define IfillI_b "fillI_b"
#define IfillR_b "fillR_b"
#define IfillF_b "fillF_b"

#define IeqB_a "eqB_a"
#define IeqC_a "eqC_a"
#define IeqI_a "eqI_a"
#define IeqR_a "eqR_a"
#define IeqS_a "eqS_a"

#define IeqAC_a "eqAC_a"

#define IeqB_b "eqB_b"
#define IeqC_b "eqC_b"
#define IeqI_b "eqI_b"
#define IeqR_b "eqR_b"

538
539
#define InotB "notB"

clean's avatar
clean committed
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
#define IpushB "pushB"
#define IpushI "pushI"
#define IpushC "pushC"
#define IpushR "pushR"

#define IpushD "pushD"

#define IpushB_a "pushB_a"
#define IpushC_a "pushC_a"
#define IpushI_a "pushI_a"
#define IpushR_a "pushR_a"
#define IpushF_a "pushF_a"

#define IpushD_a "pushD_a"

#define Ipush_array "push_array"
#define Ipush_arraysize "push_arraysize"
#define Iselect "select"
#define Iupdate "update"
#define Ireplace "replace"

#define Ipush_arg "push_arg"
#define Ipush_args "push_args"
#define Ipush_args_u "push_args_u"
#define Ipush_r_args "push_r_args"
#define Ipush_r_args_u "push_r_args_u"
#define Ipush_r_args_a "push_r_args_a"
#define Ipush_r_args_b "push_r_args_b"
568
#define Ipush_r_arg_u "push_r_arg_u"
clean's avatar
clean committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#define Irepl_arg "repl_arg"
#define Irepl_args "repl_args"
#define Irepl_r_args "repl_r_args"
#define Irepl_r_args_a "repl_r_args_a"

#define Ipush_node "push_node"
#define Ipush_node_u "push_node_u"

#define Ifill "fill"
#define Ifillcp "fillcp"
#define Ifill_u "fill_u"
#define Ifillcp_u "fillcp_u"
#define Ifillh "fillh"
#define Ifill1 "fill1"
#define Ifill2 "fill2"
584
#define Ifill3 "fill3"
clean's avatar
clean committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

#define Ibuild "build"
#define Ibuildh "buildh"
#define Ibuild_u "build_u"
#define IbuildAC "buildAC"

#define Ifill_r "fill_r"
#define Ifill1_r "fill1_r"
#define Ifill2_r "fill2_r"
#define Ifill3_r "fill3_r"

#define Ibuild_r "build_r"

#define Ifill_a "fill_a"

#define Ipush_a "push_a"
#define Ipush_b "push_b"

#define Ijsr_eval "jsr_eval"
604
#define Ijsr_ap "jsr_ap"
clean's avatar
clean committed
605
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

#define Ipop_a "pop_a"
#define Ipop_b "pop_b"
#define Ieq_desc "eq_desc"
#define IeqD_b "eqD_b"

#define Ijmp_false "jmp_false"
#define Ijmp_true "jmp_true"
#define Ijmp "jmp"
#define Ijsr "jsr"

#define Icreate "create"
#define Iprint "print"

#define Iupdate_a "update_a"
#define Iupdate_b "update_b"
#define Iupdatepop_a "updatepop_a"
#define Iupdatepop_b "updatepop_b"
#define Iupdate_b "update_b"
#define Ipop_a "pop_a"
#define Ipop_b "pop_b"

#define Iget_node_arity "get_node_arity"
#define Iget_desc_arity "get_desc_arity"

#define Ipush_arg_b "push_arg_b"

#define Irtn "rtn"

#define Ijmp_eval "jmp_eval"
#define Ijmp_eval_upd "jmp_eval_upd"
636
#define Ijmp_ap "jmp_ap"
clean's avatar
clean committed
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
665
666

#define Ihalt "halt"

#define Itestcaf "testcaf"
#define Ipushcaf "pushcaf"
#define Ifillcaf "fillcaf"

#define Iin "in"
#define Iout "out"

static void put_instruction (char *instruction)
{
	FPutC ('\n',OutFile);
	FPutC ('\t',OutFile);
	FPutS (instruction,OutFile);
}

static void put_instruction_ (char *instruction)
{
	FPutC ('\n',OutFile);
	FPutC ('\t',OutFile);
	FPutS (instruction,OutFile);
	FPutC (' ',OutFile);
}

static void put_instruction_code (int instruction_code)
{
	FPutC (instruction_code,OutFile);
}

667
#define Da "a"
clean's avatar
clean committed
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
737
738
739
740
741
742
743
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
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
929
930
931
#define Dkeep "keep"
#define Dd "d"
#define Do "o"
#define Dimpdesc "impdesc"
#define Dimplab "implab"
#define Dexport "export"
#define Dn "n"
#define Dnu "nu"
#define Dn_string "n_string"
#define Ddesc "desc"
#define Ddescn "descn"
#define Ddescexp "descexp"
#define Drecord "record"
#define Dmodule "module"
#define Ddepend "depend"
#define Dcomp "comp"
#define Dcode "code"
#define Dstart "start"
#define Dstring "string"
#define Dcaf "caf"
#define Dendinfo "endinfo"

#define Dpb "pb"
#define Dpd "pd"
#define Dpn "pn"
#define Dpl "pl"
#define Dpld "pld"
#define Dpt "pt"
#define Dpe "pe"

static void put_directive (char *directive)
{
	FPutC ('\n',OutFile);
	FPutC ('.',OutFile);
	FPutS (directive,OutFile);
}

static void put_directive_ (char *directive)
{
	FPutC ('\n',OutFile);
	FPutC ('.',OutFile);
	FPutS (directive,OutFile);
	FPutC (' ',OutFile);
}

static void put_first_directive_ (char *directive)
{
	FPutC ('.',OutFile);
	FPutS (directive,OutFile);
	FPutC (' ',OutFile);
}

void BuildBasicFromB (ObjectKind kind,int b_offset)
{
	switch (kind){
		case IntObj:
		case ProcIdObj:
		case RedIdObj:
			put_instruction_b (buildI_b); break;
		case BoolObj:
			put_instruction_b (buildB_b); break;
		case CharObj:
			put_instruction_b (buildC_b); break;
		case RealObj:
			put_instruction_b (buildR_b); break;
		case FileObj:
			put_instruction_b (buildF_b); break;
		default:
			error_in_function ("BuildBasicFromB");
			return;
	}

	put_arguments_n_b (b_offset);
}

void FillBasicFromB (ObjectKind kind, int boffs, int aoffs, FillKind fkind)
{
	TreatWaitListBeforeFill (aoffs, fkind);
	switch (kind){
		case IntObj:
		case ProcIdObj:			/* we assume proc_id and red_id	*/
		case RedIdObj:			/* to be integers				*/
			put_instruction_b (fillI_b); break;
		case BoolObj:
			put_instruction_b (fillB_b); break;
		case CharObj:
			put_instruction_b (fillC_b); break;
		case RealObj:
			put_instruction_b (fillR_b); break;
		case FileObj:
			put_instruction_b (fillF_b); break;
		default:
			error_in_function ("FillBasicFromB");
			return;
	}
	put_arguments_nn_b (boffs,aoffs);
	TreatWaitListAfterFill (aoffs, fkind);
}
	
void BuildBasic (ObjectKind obj,SymbValue val)
{
	switch (obj){
		case IntObj:
			put_instruction_b (buildI);
			put_arguments_i_b (val.val_int);
			break;
		case BoolObj:
			put_instruction_ (IbuildB);
			if (val.val_bool)
				FPrintF (OutFile, "TRUE");
			else
				FPrintF (OutFile, "FALSE");
			break;
		case CharObj:
			put_instruction_ (IbuildC);
			FPrintF (OutFile, "%s", val.val_char);
			break;
		case RealObj:
			put_instruction_ (IbuildR);
			FPrintF (OutFile, "%s", val.val_real);
			break;
		case StringObj:
			put_instruction_ (IbuildS);
			FPrintF (OutFile, "%s", val.val_string);
			break;
		default:
			error_in_function ("BuildBasic");
			return;
	}
}

void FillBasic (ObjectKind obj, SymbValue val, int offset, FillKind fkind)
{
	TreatWaitListBeforeFill (offset, fkind);
	switch (obj){
		case IntObj:
			put_instruction_b (fillI);
			put_arguments_in_b (val.val_int,offset);
			break;
		case BoolObj:
			put_instruction_ (IfillB);
			if (val.val_bool)
				FPrintF (OutFile, "TRUE %d", offset);
			else
				FPrintF (OutFile, "FALSE %d", offset);
			break;
		case CharObj:
			put_instruction_ (IfillC);
			FPrintF (OutFile, "%s %d", val.val_char, offset);
			break;
		case RealObj:
			put_instruction_ (IfillR);
			FPrintF (OutFile, "%s %d", val.val_real, offset);
			break;
		case StringObj:
			put_instruction_ (IfillS);
			FPrintF (OutFile, "%s %d", val.val_string, offset);
			break;
		default:
			error_in_function ("FillBasic");
			return;
	}
	TreatWaitListAfterFill (offset, fkind);
}

void IsBasic (ObjectKind obj, SymbValue val, int offset)
{
	switch (obj){
		case IntObj:
			put_instruction_b (eqI_a);
			put_arguments_in_b (val.val_int,offset);
			break;
		case BoolObj:
			put_instruction_ (IeqB_a);
			if (val.val_bool)
				FPrintF (OutFile, "TRUE %d", offset);
			else
				FPrintF (OutFile, "FALSE %d", offset);
			break;
		case CharObj:
			put_instruction_ (IeqC_a);
			FPrintF (OutFile, "%s %d", val.val_char, offset); break;
		case RealObj:
			put_instruction_ (IeqR_a);
			FPrintF (OutFile, "%s %d", val.val_real, offset); break;
		case StringObj:
			put_instruction_ (IeqS_a);
			FPrintF (OutFile, "%s %d", val.val_string, offset); break;
		default:
			error_in_function ("IsBasic");
			return;
	}
}

void IsString (SymbValue val)
{
	put_instruction_ (IeqAC_a);
	FPrintF (OutFile, "%s",val.val_string);
}

void PushBasic (ObjectKind obj, SymbValue val)
{
	switch (obj){
		case IntObj:
			put_instruction_b (pushI);
			put_arguments_i_b (val.val_int);
			break;
		case BoolObj:
			put_instruction_ (IpushB);
			if (val.val_bool)
				FPutS ("TRUE", OutFile);
			else
				FPutS ("FALSE", OutFile);
			break;
		case CharObj:
			put_instruction_ (IpushC);
			FPrintF (OutFile, "%s", val.val_char); break;
		case RealObj:
			put_instruction_ (IpushR);
			FPrintF (OutFile, "%s", val.val_real); break;
		default:
			error_in_function ("PushBasic");
			return;
	}
}

void GenPushReducerId (int i)
{
	put_instruction_b (pushI);
	put_arguments_n_b (i);
}

void GenPushArgNr (int argnr)
{
	put_instruction_b (pushI);
	put_arguments_n_b (argnr);
}

void EqBasic (ObjectKind obj, SymbValue val, int offset)
{
	switch (obj){
		case IntObj:
			put_instruction_b (eqI_b);
			put_arguments_in_b (val.val_int,offset);
			break;
		case BoolObj:
			put_instruction_ (IeqB_b);
			if (val.val_bool)
				FPrintF (OutFile, "TRUE %d", offset);
			else
				FPrintF (OutFile, "FALSE %d", offset);
			break;
		case CharObj:
			put_instruction_ (IeqC_b);
			FPrintF (OutFile, "%s %d", val.val_char, offset); break;
		case RealObj:
			put_instruction_ (IeqR_b);
			FPrintF (OutFile, "%s %d", val.val_real, offset); break;
		default:
			error_in_function ("EqBasic");
			return;
	}
}

932
933
934
935
936
void GenNotB (void)
{
	put_instruction (InotB);
}

clean's avatar
clean committed
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
void PushBasicFromAOnB (ObjectKind kind,int offset)
{
	switch (kind){
		case IntObj:
		case ProcIdObj:
		case RedIdObj:
			put_instruction_b (pushI_a);
			break;
		case BoolObj:
			put_instruction_b (pushB_a);
			break;
		case CharObj:
			put_instruction_b (pushC_a);
			break;
		case RealObj:
			put_instruction_b (pushR_a);
			break;
		case FileObj:
			put_instruction_b (pushF_a);
			break;
		default:
			error_in_function ("PushBasicFromAOnB");
			return;
	}
	put_arguments_n_b (offset);
}

void GenPushD_a (int a_offset)
{
	put_instruction_ (IpushD_a);
	FPrintF (OutFile,"%d",a_offset);
}
	
void PushBasicOnB (ObjectKind obj, int offset)
{
	int i;

	for (i = ObjectSizes[obj]; i > 0; i--)
		GenPushB (offset + ObjectSizes[obj] - 1);
}

void UpdateBasic (int size, int srcoffset, int dstoffset)
{
	if (srcoffset < dstoffset){
		int i;
		
		for (i=size-1; i >= 0; i--)
			GenUpdateB (srcoffset+i, dstoffset+i);
	} else if (srcoffset > dstoffset){
		int i;
		
		for (i=0; i < size; i++)
			GenUpdateB (srcoffset+i, dstoffset+i);
	}
}

static Bool IsDirective (Instructions instruction, char *directive)
{
	char *s;

	s=instruction->instr_this;
	while (isspace(*s))
		++s;
	if (*s!='.')
		return False;

	for (; *directive; ++directive)
		if (*directive!=*++s)
			return False;

	return True;
}

static Bool IsInlineFromCurrentModule (SymbDef def)
{
	RuleAlts alt;
	Instructions instruction, next;
	/*
	if (def->sdef_kind!=IMPRULE)
		return False;
	*/
	alt=def->sdef_rule->rule_alts;

	if (alt->alt_kind!=ExternalCall || !alt->alt_rhs_code->co_is_abc_code)
		return False;
	
	instruction=alt->alt_rhs_code->co_instr;

	if (!IsDirective(instruction, "inline"))
		return False;

	for (instruction=instruction->instr_next;(next=instruction->instr_next)!=NULL;instruction=next)
		;

	return (IsDirective(instruction, "end"));
}

/*
	For ABC to target machine code generation we supply the abc code
	with special stack layout directives. The routines for doing this
	are 'GenBStackElems', 'GenStackLayoutOfNode' and 'GenStackLayoutOfState'.
*/

1040
static char BElems[] = BASIC_ELEMS_STRING;
clean's avatar
clean committed
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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260

static void GenBStackElems (StateS state)
{
	if (IsSimpleState (state)){
		if (state.state_kind == OnB)
			FPutC (BElems [(int) state.state_object], OutFile);			
	} else {
		int arity;
		States argstates;
		
		switch (state.state_type){
			case TupleState:
				argstates = state.state_tuple_arguments;
				break;
			case RecordState:
				argstates = state.state_record_arguments;
				break;
			case ArrayState:
				return;
			default:
				error_in_function ("GenBStackElems");
				return;
		}
		for (arity=0; arity < state.state_arity; ++arity)
			GenBStackElems (argstates[arity]);
	}
}

static void GenABStackElems (StateS state)
{
	if (IsSimpleState (state)){
		if (state.state_kind == OnB)
			FPutC (BElems [(int) state.state_object], OutFile);
		else
			FPutC ('a', OutFile);
			
	} else {
		int arity;
		States argstates;
		
		switch (state.state_type){
			case TupleState:
				argstates = state.state_tuple_arguments;
				break;
			case RecordState:
				argstates = state.state_record_arguments;
				break;
			case ArrayState:
				FPutC ('a', OutFile);
				return;
			default:
				error_in_function ("GenABStackElems");
				return;
		}
		for (arity=0; arity < state.state_arity; arity++)
			GenABStackElems (argstates[arity]);
	}
}

void GenDStackLayout (int asize,int bsize,Args fun_args)
{
	if (DoStackLayout){
		put_directive_b (d);
		if (bsize > 0){
			put_arguments_nn__b (asize,bsize);

			while (fun_args!=NULL){
				GenBStackElems (fun_args->arg_state);
				fun_args=fun_args->arg_next;
			}
		} else
			put_arguments_nn_b (asize,0);
	}
}

void GenOStackLayout (int asize,int bsize,Args fun_args)
{
	if (DoStackLayout){
		put_directive_b (o);
		if (bsize > 0){
			put_arguments_nn__b (asize,bsize);

			while (fun_args!=NULL){
				GenBStackElems (fun_args->arg_state);
				fun_args=fun_args->arg_next;
			}
		} else
			put_arguments_nn_b (asize,0);
	}
}

static void CallFunction2 (Label label, SymbDef def, Bool isjsr, StateS root_state, Args fun_args, int arity)
{
	int ain,aout,bin,bout;
	Args arg;
	
	ain=0;
	bin=0;
	
	if (fun_args != NULL){
		for (arg = fun_args; arg; arg = arg -> arg_next)
			AddSizeOfState  (arg -> arg_state, &ain, &bin);
	} else
		ain = arity;
	DetermineSizeOfState (root_state, &aout, &bout);

	if (IsSimpleState (root_state) && (root_state.state_kind!=OnB && root_state.state_kind!=StrictRedirection))
		ain++;

	if (label->lab_mod && label->lab_mod==CurrentModule)
		label->lab_mod = NULL;

	label->lab_pref = s_pref;

	if (def->sdef_kind==SYSRULE){
		char  *instr;
	
		instr= def->sdef_ident->ident_instructions;

		if (instr && *instr!='\0'){
			char *tail;

			for (; *instr != '\0'; instr = tail){
				for (tail = instr; *tail != '\n'; tail++)
					;
				*tail = '\0';
				FPrintF (OutFile, "\n%s", instr);
				*tail++ = '\n';
			}
			if (!isjsr)
				GenRtn (aout, bout, root_state);
		} else {
			if (def->sdef_ident->ident_environ && instr==NULL)
			{	char *previous_module = CurrentModule;
				char *previous_ext = CurrentExt;
			
				CurrentModule = def->sdef_module;
				CurrentExt    = GetFileExtension (abcFile);
				
				StaticMessage (False, "%D", "no inline code for this rule", def);

				CurrentModule = previous_module;
				CurrentExt = previous_ext;

               	def->sdef_ident->ident_environ = (char *) NIL;	
			}
			GenDStackLayout (ain, bin, fun_args);
			if (isjsr){
				GenJsr (label);
				GenOStackLayoutOfState (aout, bout, root_state);
			} else
				GenJmp (label);
		}
	} else if (def->sdef_kind==IMPRULE && IsInlineFromCurrentModule (def)){
		Instructions instruction, last, first, next;

		instruction=def->sdef_rule->rule_alts->alt_rhs_code->co_instr;
		instruction=instruction->instr_next;
		first=instruction;

		last=NULL;
		for (;(next=instruction->instr_next)!=NULL;instruction=next)
			last=instruction;

		last->instr_next=NULL;
		GenInstructions (first);
		last->instr_next=instruction;

		if (!isjsr)
			GenRtn (aout, bout, root_state);
	} else {
		GenDStackLayout (ain, bin, fun_args);
		if (isjsr){
			GenJsr (label);
			GenOStackLayoutOfState (aout, bout, root_state);
		} else
			GenJmp (label);
	}	
}

void CallFunction (Label label, SymbDef def, Bool isjsr, Node root)
{
	if (def->sdef_arfun<NoArrayFun)
		CallArrayFunction (def,isjsr,&root->node_state);
	else
		CallFunction2 (label, def, isjsr, root->node_state, root->node_arguments, root->node_arity);
}

static void GenArraySize (Label elemdesc, int asize, int bsize)
{
	put_instruction_ (Ipush_arraysize);
	GenLabel (elemdesc);
	FPrintF (OutFile, " %d %d", asize, bsize);
}

static void GenArraySelect (Label elemdesc, int asize, int bsize)
{
	put_instruction_ (Iselect);
	GenLabel (elemdesc);
	FPrintF (OutFile, " %d %d", asize, bsize);
}

static void GenArrayUpdate (Label elemdesc, int asize, int bsize)
{
	put_instruction_ (Iupdate);
	GenLabel (elemdesc);
	FPrintF (OutFile, " %d %d", asize, bsize);	
}

static void GenArrayReplace (Label elemdesc, int asize, int bsize)
{
	put_instruction_ (Ireplace);
	GenLabel (elemdesc);
	FPrintF (OutFile, " %d %d", asize, bsize);
}
	
extern Symbol UnboxedArrayFunctionSymbols [];

static 	Label	ApplyLabel;
static 	StateS  	ApplyState;
1261
1262
1263
#if CLEAN2
static int CaseFailNumber;
#endif
clean's avatar
clean committed
1264
1265
1266
1267
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
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323

extern SymbDef ApplyDef; /* from codegen2.c */

static void ApplyOperatorToArrayAndIndex (Bool is_jsr)
{
	GenPushA (0);
	GenCreate (-1);
	GenFillArray (3, 0, NormalFill);

	GenUpdateA (0, 2);

	BuildBasicFromB (IntObj,0);

	GenPopB (SizeOfInt);

	GenUpdateA (0, 4);

	GenPopA (2);

	CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);
	CallFunction2	(ApplyLabel, ApplyDef, is_jsr, ApplyState, NULL, 2);
}

static void ApplyOperatorToArrayElem (int asize, int bsize, ObjectKind kind)
{
	if (asize==0){
		GenPushA (0);

		BuildBasicFromB (kind,0);

		GenUpdateA (0, 2);
		GenPopB (bsize);
		GenPopA (1);
	}

	CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);
}

static void UnpackResultTuple (int asize,int bsize,ObjectKind kind)
{
	GenReplArgs (2, 2);
	if (asize==0){
		PushBasicFromAOnB (kind, 0);
		GenPopA (1);
	}
	GenPushArray (asize);
	GenUpdateA (0,1+asize);
	GenPopA (1);
}

void CallArrayFunction (SymbDef array_def,Bool is_jsr,StateP node_state_p)
{
	LabDef elem_desc;
	int asize, bsize;
	Bool elem_is_lazy;
	StateS array_state;
	ArrayFunKind fkind;
	StateP function_state_p;
	
	fkind = (ArrayFunKind)array_def->sdef_arfun;
clean's avatar
clean committed
1324
/* RWS ...
clean's avatar
clean committed
1325
	function_state_p = array_def->sdef_rule_type->rule_type_state_p;
clean's avatar
clean committed
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
*/
	switch (array_def->sdef_kind)
	{
		case DEFRULE:
		case SYSRULE:
			function_state_p = array_def->sdef_rule_type->rule_type_state_p;
			break;
		case IMPRULE:
			function_state_p = array_def->sdef_rule->rule_state_p;
			break;
		default:
1337
			error_in_function ("CallArrayFunction");
clean's avatar
clean committed
1338
1339
1340
			break;
	}
/* RWS */
clean's avatar
clean committed
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
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
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
1646
1647
1648
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
1702
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
		
	if (function_state_p[0].state_type==SimpleState && function_state_p[0].state_object==UnknownObj){
		StateS	elem_state;

		switch (fkind){
			case CreateArrayFun:
			case _CreateArrayFun:
				array_state=function_state_p[-1];
				break;
		 	case _UnqArraySelectNextFun:
			case _UnqArraySelectLastFun:
			case _ArrayUpdateFun:
				if (function_state_p[1].state_type==TupleState)
					array_state=function_state_p[1].state_tuple_arguments[0];
				else
					error_in_function ("CallArrayFunction");
				break;
			default:
				array_state=function_state_p[1];
		}

		elem_state = array_state.state_array_arguments [0];

		if (array_state.state_type==ArrayState && (array_state.state_mark & STATE_UNBOXED_ARRAY_MASK)){
			if (ApplyLabel == NULL){
				ApplyLabel = CompAllocType (LabDef);
				ConvertSymbolToLabel (ApplyLabel, ApplyDef);
			}

			switch (fkind){
			case CreateArrayFun:
			case _CreateArrayFun:
				GenPushA (0);
				BuildBasicFromB (IntObj,0);
				GenPopB (SizeOfInt);
	
				GenUpdateA (0, 2);
				GenPopA (1);
	
				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);

				if (fkind!=_CreateArrayFun)
					CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);
				
				GenPushArray (0);
				GenUpdateA (0, 1);
				GenPopA (1);
				
				break;
			case ArraySelectFun:
				if (elem_state.state_kind==StrictOnA)
					ApplyOperatorToArrayAndIndex (is_jsr);
				else {
					ApplyOperatorToArrayAndIndex (True);
					PushBasicFromAOnB	(elem_state.state_object, 0);
					GenPopA				(1);
				}
				break;
			case UnqArraySelectFun:
#ifdef OBSERVE_ARRAY_SELECTS_IN_PATTERN
				DetermineSizeOfState (elem_state,&asize,&bsize);
				ApplyOperatorToArrayAndIndex (True);
				if (node_state_p->state_type==TupleState
					&& node_state_p->state_tuple_arguments[1].state_type==SimpleState
					&& node_state_p->state_tuple_arguments[1].state_kind==Undefined)
				{
					GenReplArg (2,1);
					if (asize==0){
						PushBasicFromAOnB (elem_state.state_object,0);
						GenPopA (1);
					}
				} else
					UnpackResultTuple (asize,bsize,elem_state.state_object);
				break;
#endif
			case _UnqArraySelectFun:
				DetermineSizeOfState (elem_state,&asize,&bsize);
				ApplyOperatorToArrayAndIndex (True);
				UnpackResultTuple (asize, bsize, elem_state.state_object);
				break;
			case _UnqArraySelectNextFun:
			case _UnqArraySelectLastFun:
				DetermineSizeOfState (elem_state,&asize,&bsize);
				
				GenCreate (-1);
				GenFillArray (3, 0, NormalFill);
				GenCreate (-1);
				GenFillArray (3, 0, NormalFill);
				GenBuildh (&tuple_lab,2);

				GenUpdateA (0, 2);

				BuildBasicFromB (IntObj,0);

				GenPopB (SizeOfInt);

				GenUpdateA (0, 4);

				GenPopA (2);

				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);
				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);

				UnpackResultTuple (asize, bsize, elem_state.state_object);
				break;

			case _ArrayUpdateFun:
				DetermineSizeOfState (elem_state,&asize,&bsize);

				GenCreate (-1);
				GenFillArray (3, 0, NormalFill);
				GenCreate (-1);
				GenFillArray (3, 0, NormalFill);
				GenBuildh (&tuple_lab,2);

				GenUpdateA (0, 2);

				BuildBasicFromB (IntObj,0);
				GenPopB (SizeOfInt);

				GenUpdateA (0, 4);

				GenPopA (2);

				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);
				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);

				ApplyOperatorToArrayElem (asize, bsize, elem_state.state_object);

				GenPushArray (0);
				GenUpdateA (0, 1);
				GenPopA (1);
				break;
			case ArrayUpdateFun:
				DetermineSizeOfState (elem_state,&asize,&bsize);

				ApplyOperatorToArrayAndIndex (True);
				ApplyOperatorToArrayElem (asize,bsize,elem_state.state_object);

				GenPushArray (0);
				GenUpdateA (0, 1);
				GenPopA (1);
				break;
			case ArrayReplaceFun:
				DetermineSizeOfState (elem_state,&asize,&bsize);

				ApplyOperatorToArrayAndIndex (True);
				ApplyOperatorToArrayElem (asize,bsize,elem_state.state_object);
				UnpackResultTuple (asize,bsize,elem_state.state_object);
				break;
			case ArraySizeFun:
				GenCreate (-1);
				GenFillArray (2, 0, NormalFill);
				GenUpdateA (0, 2);

				GenPopA (1);

				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);

				PushBasicFromAOnB (IntObj, 0);
				GenPopA (1);
				break;
			case UnqArraySizeFun:
				GenCreate (-1);
				GenFillArray (2, 0, NormalFill);
				GenUpdateA (0, 2);

				GenPopA (1);

				CallFunction2	(ApplyLabel, ApplyDef, True, ApplyState, NULL, 2);

				UnpackResultTuple (0, SizeOfInt, IntObj);

				break;
		
			}
			if (! is_jsr){
				DetermineSizeOfState (function_state_p[-1], & asize, & bsize);
				GenRtn (asize,bsize,function_state_p[-1]);
			}
			return;
		}	
		else
		{	GenPopA (1);
			elem_desc = BasicDescriptors [UnknownObj];
			asize = 1;
			bsize = 0;
			elem_is_lazy = elem_state.state_type==SimpleState && elem_state.state_kind==OnA;
		}
	} else {
		switch (fkind){
			case CreateArrayFun:
			case _CreateArrayFun:
				array_state = function_state_p[-1];
				break;
		 	case _UnqArraySelectNextFun:
			case _UnqArraySelectLastFun:
			case _ArrayUpdateFun:
				if (function_state_p[0].state_type==TupleState)
					array_state=function_state_p[0].state_tuple_arguments[0];
				else
					error_in_function ("CallArrayFunction");
				break;
			default:
				array_state = function_state_p[0];
		}

		if (array_state.state_type == ArrayState){
			StateS elem_state = array_state.state_array_arguments [0];
			DetermineArrayElemDescr (elem_state, & elem_desc);
			DetermineSizeOfState	(elem_state, & asize, & bsize);
			elem_is_lazy = elem_state.state_type==SimpleState && elem_state.state_kind==OnA;
		} else
			error_in_function ("CallArrayFunction");
	}

	switch (fkind){
		case CreateArrayFun:
			put_instruction_ ("create_array");
			GenLabel (&elem_desc);
			FPrintF (OutFile," %d %d",asize,bsize);
			break;
		case _CreateArrayFun:
			put_instruction_ ("create_array_");
			GenLabel (&elem_desc);
			FPrintF (OutFile," %d %d",asize,bsize);
			break;
		case ArraySelectFun:
			GenArraySelect (&elem_desc,asize,bsize);
			if (elem_is_lazy){
				if (is_jsr)
					GenJsrEval (0);
				else {
					GenJmpEval ();
					return;
				}
			}
			break;
		case UnqArraySelectFun:
#ifdef OBSERVE_ARRAY_SELECTS_IN_PATTERN
			if (! (node_state_p->state_type==TupleState
				&& node_state_p->state_tuple_arguments[1].state_type==SimpleState
				&& node_state_p->state_tuple_arguments[1].state_kind==Undefined))
			{
				GenPushA (0);
			}
			GenArraySelect (&elem_desc,asize,bsize);
			break;
#endif
		case _UnqArraySelectFun:
			GenPushA (0);
			GenArraySelect (&elem_desc,asize,bsize);
			break;
		case _UnqArraySelectNextFun:
		case _UnqArraySelectLastFun:
		{
			int record_or_array_a_size,record_or_array_b_size;
			
			if (node_state_p->state_type!=TupleState)
				error_in_function ("CallArrayFunction");

			DetermineSizeOfState (node_state_p->state_tuple_arguments[1],&record_or_array_a_size,&record_or_array_b_size);
			
			if (record_or_array_b_size>0){
				int i;
				
				GenPushB (record_or_array_b_size);
				
				for (i=record_or_array_b_size; i>=0; --i)
					GenUpdateB (i,i+1);
				
				GenPopB (1);
			}

			GenArraySelect (&elem_desc,asize,bsize);
			break;
		}
		case _ArrayUpdateFun:
		{
			int i,result_a_size,result_b_size;
			
			DetermineSizeOfState (*node_state_p,&result_a_size,&result_b_size);

			if (asize!=0){
				for (i=0; i<asize; ++i)
					GenPushA (result_a_size+asize);
			
				for (i=result_a_size-1; i>=0; --i)
					GenUpdateA (i+asize+1,i+asize+1+asize);

				for (i=asize-1; i>=0; --i)
					GenUpdateA (i,i+1+asize);

				GenPopA (asize);
			}			
			
			if (result_b_size!=0){
				int b_size_with_index;

				b_size_with_index=bsize+1;

				for (i=0; i<b_size_with_index; ++i)
					GenPushB (result_b_size+b_size_with_index-1);
				
				for (i=result_b_size-1; i>=0; --i)
					GenUpdateB (i+b_size_with_index,i+b_size_with_index+b_size_with_index);

				for (i=b_size_with_index-1; i>=0; --i)
					GenUpdateB (i,i+b_size_with_index);

				GenPopB (b_size_with_index);				
			}

			GenArrayUpdate (&elem_desc,asize,bsize);
							
			for (i=0; i<result_a_size; ++i)
				GenKeep (0,i+1);
			
			GenPopA (1);

			break;
		}
		case ArrayUpdateFun:
			GenArrayUpdate (& elem_desc, asize, bsize);
			break;
		case ArrayReplaceFun:
			GenArrayReplace (& elem_desc, asize, bsize);
			break;
		case ArraySizeFun:
			GenArraySize (& elem_desc, asize, bsize);
			break;
		case UnqArraySizeFun:
			GenPushA (0);
			GenArraySize (& elem_desc, asize, bsize);
			break;
	}
	
	if (! is_jsr){
		DetermineSizeOfState (*node_state_p,&asize,&bsize);
		GenRtn (asize,bsize,*node_state_p);
	}	
}

void GenNewContext (Label contlab, int offset)
{
	FPrintF (OutFile, "\n\tset_entry ");
	GenLabel (contlab);
	FPrintF (OutFile, " %d", offset);
}

void GenSetDefer (int offset)
{
	FPrintF (OutFile, "\n\tset_defer %d", offset);
}

void GenReplArgs (int arity, int nrargs)
{
	if (nrargs > 0){
		put_instruction_b (repl_args);
		put_arguments_nn_b (arity,nrargs);
	} else
		GenPopA (1);
}

void GenReplArg (int arity, int argnr)
{
	put_instruction_b (repl_arg);
	put_arguments_nn_b (arity,argnr);
}

void GenPushArgs (int offset, int arity, int nrargs)
{
	if (nrargs > 0){
		put_instruction_b (push_args);
		put_arguments_nnn_b (offset,arity,nrargs);
	}
}

void GenPushArgsU (int offset, int arity, int nrargs)
{
	if (nrargs > 0){
		put_instruction_b (push_args_u);
		put_arguments_nnn_b (offset,arity,nrargs);
	}
}

void GenPushArg (int offset, int arity, int argnr)
{
	put_instruction_b (push_arg);
	put_arguments_nnn_b (offset,arity,argnr);
}

void GenPushRArgs (int offset, int nr_a_args, int nr_b_args)
{
	if (nr_a_args + nr_b_args > 0){
		put_instruction_b (push_r_args);
		put_arguments_nnn_b (offset,nr_a_args,nr_b_args);
	}
}

void GenPushRArgsU (int offset,int n_a_args,int n_b_args)
{
	if (n_a_args + n_b_args > 0){
		put_instruction_b (push_r_args_u);
		put_arguments_nnn_b (offset,n_a_args,n_b_args);
	}
}

1749
void GenPushRArgA (int offset,int tot_nr_a_args,int tot_nr_b_args,int args_nr,int nr_a_args)
clean's avatar
clean committed
1750
1751
1752
1753
1754
1755
1756
{
	if (nr_a_args > 0){
		put_instruction_b (push_r_args_a);
		put_arguments_nnnnn_b (offset,tot_nr_a_args,tot_nr_b_args,args_nr,nr_a_args);
	}
}

1757
void GenPushRArgB (int offset,int tot_nr_a_args,int tot_nr_b_args,int args_nr,int nr_b_args)
clean's avatar
clean committed
1758
1759
1760
1761
1762
1763
1764
{
	if (nr_b_args > 0){
		put_instruction_b (push_r_args_b);
		put_arguments_nnnnn_b (offset,tot_nr_a_args,tot_nr_b_args,args_nr,nr_b_args);
	}
}

1765
1766
1767
1768
1769
1770
void GenPushRArgU (int offset,int tot_nr_a_args,int tot_nr_b_args,int args_a_nr,int nr_a_args,int args_b_nr,int nr_b_args)
{
	put_instruction_b (push_r_arg_u);
	put_arguments_nnnnnnn_b (offset,tot_nr_a_args,tot_nr_b_args,args_a_nr,nr_a_args,args_b_nr,nr_b_args);
}

clean's avatar
clean committed
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
void GenReplRArgs (int nr_a_args, int nr_b_args)
{
	if (nr_a_args +  nr_b_args > 0){
		put_instruction_b (repl_r_args);
		put_arguments_nn_b (nr_a_args,nr_b_args);
	} else
		GenPopA (1);
}

void GenReplRArgA (int tot_nr_a_args, int tot_nr_b_args, int args_nr, int nr_a_args)
{
	if (nr_a_args > 0){
		put_instruction_b (repl_r_args_a);
		put_arguments_nnnn_b (tot_nr_a_args,tot_nr_b_args,args_nr,nr_a_args);
	} else
		GenPopA (1);
}

void GenPushNode (Label contlab, int arity)
{
	put_instruction_b (push_node);
	GenLabel (contlab);
	put_arguments__n_b (arity);
}

void GenPushNodeU (Label contlab,int a_size,int b_size)
{
	put_instruction_b (push_node_u);
	GenLabel (contlab);
	put_arguments__nn_b (a_size,b_size);
}

1803
void GenFill (Label symblab,int arity,Label contlab,int offset,FillKind fkind)
clean's avatar
clean committed
1804
1805
1806
1807
1808
1809
{
	TreatWaitListBeforeFill (offset, fkind);

	put_instruction_b (fill);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1810
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
1811
1812
1813
1814
1815
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	put_arguments__n__b (arity);
	
1816
	GenDescriptorOrNodeEntryLabel (contlab);
clean's avatar
clean committed
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
	
	put_arguments__n_b (offset);

	if (arity < 0)
		arity = 1;
	TreatWaitListAfterFill (offset-arity, fkind);
}

void GenFillU (Label symblab,int a_size,int b_size,Label contlab,int offset)
{
	put_instruction_ (Ifill_u);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1830
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
1831
1832
1833
1834
1835
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	FPrintF (OutFile," %d %d ",a_size,b_size);
		
1836
	GenDescriptorOrNodeEntryLabel (contlab);
clean's avatar
clean committed
1837
1838
1839
1840
1841
1842
1843
1844
1845
	
	put_arguments__n_b (offset);
}

void GenFillcp (Label symblab,int arity,Label contlab,int offset,char bits[])
{
	put_instruction_b (fillcp);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1846
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
1847
1848
1849
1850
1851
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	put_arguments__n__b (arity);
	
1852
	GenDescriptorOrNodeEntryLabel (contlab);
clean's avatar
clean committed
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
	
	put_arguments__n_b (offset);

	FPrintF (OutFile," %s",bits);
}

void GenFillcpU (Label symblab,int a_size,int b_size,Label contlab,int offset,char bits[])
{
	put_instruction_b (fillcp_u);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1864
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
1865
1866
1867
1868
1869
	else
		FPutS (empty_lab.lab_name, OutFile);
		
	FPrintF (OutFile," %d %d ",a_size,b_size);

1870
	GenDescriptorOrNodeEntryLabel (contlab);
clean's avatar
clean committed
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
	
	put_arguments__n_b (offset);

	FPrintF (OutFile," %s",bits);
}

void GenFillh (Label symblab, int arity, int offset, FillKind fkind)
{
	TreatWaitListBeforeFill (offset, fkind);

	put_instruction_b (fillh);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1884
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
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
1911
1912
1913
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	put_arguments__nn_b (arity,offset);

	if (arity < 0)
		arity = 1;
	TreatWaitListAfterFill (offset-arity, fkind);
}

void GenFill1 (Label symblab,int arity,int offset,char bits[])
{
	put_instruction_ (Ifill1);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	FPrintF (OutFile," %d %d %s",arity,offset,bits);
}

void GenFill2 (Label symblab,int arity,int offset,char bits[])
{	
	put_instruction_ (Ifill2);
	GenLabel (symblab);
	FPrintF (OutFile," %d %d %s",arity,offset,bits);
}

1914
1915
1916
1917
1918
1919
1920
void GenFill3 (Label symblab,int arity,int offset,char bits[])
{	
	put_instruction_ (Ifill3);
	GenLabel (symblab);	
	FPrintF (OutFile, " %d %d %s",arity,offset,bits);
}

clean's avatar
clean committed
1921
1922
1923
1924
1925
void GenBuild (Label symblab,int arity,Label contlab)
{
	put_instruction_b (build);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1926
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
1927
1928
1929
1930
1931
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	put_arguments__n__b (arity);
	
1932
	GenDescriptorOrNodeEntryLabel (contlab);
clean's avatar
clean committed
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
}

void GenBuildh (Label symblab,int arity)
{
	put_instruction_b (buildh);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	put_arguments__n_b (arity);
}

1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
void GenBuildPartialFunctionh (Label symblab,int arity)
{
	put_instruction_b (buildh);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenDescriptorOrNodeEntryLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	put_arguments__n_b (arity);
}

clean's avatar
clean committed
1959
1960
1961
1962
1963
void GenBuildU (Label symblab,int a_size,int b_size,Label contlab)
{
	put_instruction_ (Ibuild_u);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
1964
		GenDescriptorOrNodeEntryLabel (symblab);
clean's avatar
clean committed
1965
1966
1967
1968
1969
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	FPrintF (OutFile," %d %d ",a_size,b_size);
	
1970
	GenDescriptorOrNodeEntryLabel (contlab);
clean's avatar
clean committed
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
}

void GenBuildArray (int argoffset)
{
	GenPushA (argoffset);
	GenBuildh (& BasicDescriptors [ArrayObj], 1);
}

void GenBuildString (SymbValue val)
{
	put_instruction_ (IbuildAC);
	FPrintF (OutFile, "%s", val.val_string);
}

static void GenFieldLabel (Label label,char *record_name)
{
	SymbDef def;
	
	def = (SymbDef) label->lab_name;
		
	if (label->lab_mod)
1992
1993
1994
		FPrintF (OutFile,"e_%s_%s%s.%s",label->lab_mod,label->lab_pref,record_name,def->sdef_ident->ident_name);
	else if (ExportLocalLabels)
		FPrintF (OutFile,"e_%s_%s%s.%s",CurrentModule,label->lab_pref,record_name,def->sdef_ident->ident_name);
clean's avatar
clean committed
1995
1996
	else if (DoDebug){
		if (def->sdef_kind==IMPRULE)
1997
			FPrintF (OutFile, "%s%s.%s.%u",label->lab_pref,record_name,def->sdef_ident->ident_name,def->sdef_number);
clean's avatar
clean committed
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
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
2106
2107
2108
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
2134
2135
2136
2137
2138
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
		else
			FPrintF (OutFile, "%s%s.%s",label->lab_pref,record_name,def->sdef_ident->ident_name);
	} else if (def->sdef_number==0)
		FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_ident->ident_name);
	else if (label->lab_pref[0] == '\0')
		FPrintF (OutFile,LOCAL_D_PREFIX "%u",def->sdef_number);
	else
		FPrintF (OutFile,"%s%u",label->lab_pref,def->sdef_number);
}

void GenBuildFieldSelector (Label symblab,Label contlab,char *record_name,int arity)
{
	put_instruction_b (build);

	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenFieldLabel (symblab,record_name);
	else
		FPutS (empty_lab.lab_name, OutFile);

	put_arguments__n__b (arity);

	GenFieldLabel (contlab,record_name);
}

void GenFieldLabelDefinition (Label label,char *record_name)
{
	FPutS ("\n", OutFile);
	GenFieldLabel (label,record_name);
}

void GenFillFieldSelector (Label symblab,Label contlab,char *record_name,int arity,int offset,FillKind fkind)
{
	TreatWaitListBeforeFill (offset,fkind);
	
	put_instruction_b (fill);

	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenFieldLabel (symblab,record_name);
	else
		FPutS (empty_lab.lab_name, OutFile);

	put_arguments__n__b (arity);

	GenFieldLabel (contlab,record_name);

	put_arguments__n_b (offset);

	TreatWaitListAfterFill (offset-1,fkind);
}

void GenFillR (Label symblab,int nr_a_args,int nr_b_args,int rootoffset,int a_offset,int b_offset,FillKind fkind,Bool pop_args)
{
	TreatWaitListBeforeFill (rootoffset, fkind);
	
	put_instruction_ (Ifill_r);
	
	if (! symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);

	if (nr_a_args==0)
		a_offset=0;
	if (nr_b_args==0)
		b_offset=0;
	
	FPrintF (OutFile, " %d %d %d %d %d",nr_a_args,nr_b_args,rootoffset,a_offset,b_offset);
	
	if (pop_args){
		GenPopA (nr_a_args);
		GenPopB (nr_b_args);
		TreatWaitListAfterFill (rootoffset-nr_a_args, fkind);		
	} else
		TreatWaitListAfterFill (rootoffset, fkind);
}

void GenFill1R (Label symblab,int n_a_args,int n_b_args,int rootoffset,char bits[])
{	
	put_instruction_ (Ifill1_r);
	
	if (! symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	FPrintF (OutFile, " %d %d %d %s",n_a_args,n_b_args,rootoffset,bits);	
}

void GenFill2R (Label symblab,int n_a_args,int n_b_args,int rootoffset,char bits[])
{	
	put_instruction_ (Ifill2_r);
	
	if (! symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	FPrintF (OutFile, " %d %d %d %s",n_a_args,n_b_args,rootoffset,bits);
}

void GenFill3R (Label symblab,int n_a_args,int n_b_args,int rootoffset,char bits[])
{	
	put_instruction_ (Ifill3_r);
	
	if (! symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);
	
	FPrintF (OutFile, " %d %d %d %s",n_a_args,n_b_args,rootoffset,bits);
}

void GenBuildR (Label symblab,int nr_a_args,int nr_b_args,int a_offset,int b_offset,Bool pop_args)
{
	put_instruction_ (Ibuild_r);
	
	if (!symblab->lab_issymbol || DescriptorNeeded (symblab->lab_symbol))
		GenLabel (symblab);
	else
		FPutS (empty_lab.lab_name, OutFile);

	if (nr_a_args==0)
		a_offset=0;
	if (nr_b_args==0)
		b_offset=0;

	FPrintF (OutFile, " %d %d %d %d",nr_a_args,nr_b_args,a_offset,b_offset);

	if (pop_args){
		if (nr_a_args>0){
			GenUpdateA (0,nr_a_args);
			GenPopA (nr_a_args);
		}
		GenPopB (nr_b_args);
	}
}

void GenFillFromA (int src, int dst, FillKind fkind)
{
	if (src == dst)
		return;
	
	TreatWaitListBeforeFill (dst, fkind);
	put_instruction_b (fill_a);
	put_arguments_nn_b (src,dst);
	TreatWaitListAfterFill (dst, fkind);
}

void GenFillArray (int argoffset, int rootoffset, FillKind fkind)
{
	GenPushA (argoffset);
	GenFillh (&BasicDescriptors [ArrayObj], 1, rootoffset+1, fkind);
}

void GenPushArray (int rootoffset)
{
	put_instruction_ (Ipush_array);
	FPrintF (OutFile, "%d", rootoffset);
}

void GenRtn (int asize, int bsize, StateS resultstate)
{
	GenDStackLayoutOfState (asize, bsize, resultstate);
	put_instructionb (rtn);
}

void GenPushA (int offset)
{
	if (offset<0)
		error_in_function ("GenPushA");

	put_instruction_b (push_a);
	put_arguments_n_b (offset);
}

void GenPushB (int offset)
{
	if (offset<0)
		error_in_function ("GenPushB");

	put_instruction_b (push_b);
	put_arguments_n_b (offset);
}

void GenJsrEval (int offset)
{
	put_instruction_b (jsr_eval);
	put_arguments_n_b (offset);
}

2188
2189
2190
2191
2192
2193
void GenJsrAp (int n_args)
{
	put_instruction_b (jsr_ap);
	put_arguments_n_b (n_args);
}

clean's avatar
clean committed
2194
2195
2196
2197
2198
void GenJmpEval (void)
{
	put_instruction (Ijmp_eval);
}

2199
2200
2201
2202
2203
2204
void GenJmpAp (int n_args)
{
	put_instruction_b (jmp_ap);
	put_arguments_n_b (n_args);
}

clean's avatar
clean committed
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
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
void GenPopA (int nr)
{
	if (nr > 0){
		put_instruction_b (pop_a);
		put_arguments_n_b (nr);
	}
}

void GenPopB (int nr)
{
	if (nr > 0){
		put_instruction_b (pop_b);
		put_arguments_n_b (nr);
	}
}

void GenEqDesc (Label symblab,int arity,int offset)
{
	put_instruction_b (eq_desc);
	GenLabel (symblab);
	put_arguments__nn_b (arity,offset);
}

void GenEqD_b (Label symblab,int arity)
{
	put_instruction_b (eqD_b);
	GenLabel (symblab);
	put_arguments__n_b (arity);
}

void GenExitFalse (Label to)
{
	put_instruction_ ("exit_false");
	GenLabel (to);
}

void GenJmpFalse (Label to)
{
	put_instruction_b (jmp_false);
	GenLabel (to);
}

void GenJmpTrue (Label to)
{
	put_instruction_b (jmp_true);
	GenLabel (to);
}

void GenJmp (Label tolab)
{
	put_instruction_b (jmp);
	GenLabel (tolab);
}

void GenJsr (Label tolab)
{
	put_instruction_b (jsr);
	GenLabel (tolab);
}

void GenCreate (int arity)
{
	if (arity == -1)
		put_instruction (Icreate);
	else {
		put_instruction_ (Icreate);
		FPrintF (OutFile, "%d", arity);
	}
}

void GenDumpString (char *str)
{
	put_instruction_ (Iprint);
	FPrintF (OutFile, "\"%s\"", str);
	put_instruction (Ihalt);
}

void GenLabelDefinition (Label lab)
{
	if (lab){
		FPutC ('\n', OutFile);
		GenLabel (lab);
	}
}

2290
2291
2292
2293
2294
2295
void GenNodeEntryLabelDefinition (Label lab)
{
	FPutC ('\n', OutFile);
	GenDescriptorOrNodeEntryLabel (lab);
}

clean's avatar
clean committed
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
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
void GenUpdateA (int src, int dst)
{
	if (src != dst){
		put_instruction_b (update_a);
		put_arguments_nn_b (src,dst);
	}
}

void GenUpdatePopA (int src, int dst)
{
	if (src!=dst){
		if (dst!=0){
			put_instruction_b (updatepop_a);
			put_arguments_nn_b (src,dst);
		} else {
			put_instruction_b (update_a);
			put_arguments_nn_b (src,dst);
		}
	} else
		if (dst > 0){
			put_instruction_b (pop_a);
			put_arguments_n_b (dst);
		}
}

void GenUpdateB (int src, int dst)
{
	if (src != dst){
		put_instruction_b (update_b);
		put_arguments_nn_b (src,dst);
	}
}

void GenUpdatePopB (int src, int dst)
{
	if (src!=dst){
		if (dst!=0){
			put_instruction_b (updatepop_b);
			put_arguments_nn_b (src,dst);
		} else {
			put_instruction_b (update_b);
			put_arguments_nn_b (src,dst);
		}
	} else
		if (dst > 0) {
			put_instruction_b (pop_b);
			put_arguments_n_b (dst);
		}
}

void GenHalt (void)
{
	put_instruction (Ihalt);
}

void GenSetRedId (int offset)
{
	FPrintF (OutFile, "\n\tset_red_id %d", offset);
}

void GenNewParallelReducer (int offset, char *reducer_code)
{
	FPrintF (OutFile, "\n\tnew_ext_reducer %s %d", reducer_code, offset);
}

void GenNewContInterleavedReducer (int offset)
{
	FPrintF (OutFile, "\n\tnew_int_reducer _cont_reducer %d", offset);
	FPrintF (OutFile, "\n\tforce_cswitch", offset);
}

void GenNewInterleavedReducer (int offset, char *reducer_code)
{
	FPrintF (OutFile, "\n\tnew_int_reducer %s %d", reducer_code, offset);
}

void GenSendGraph (char *code, int graphoffs, int chanoffs)
{
	FPrintF (OutFile, "\n\tsend_graph %s %d %d", code, graphoffs, chanoffs);
}

void GenCreateChannel (char *code)
{
	FPrintF (OutFile, "\n\tcreate_channel %s", code);
}

void GenNewP (void)
{
	FPutS ("\n\tnewP", OutFile);
}

void SetContinue (int offset)
{
	FPrintF (OutFile, "\n\tset_continue %d", offset);
}

void SetContinueOnReducer (int offset)
{
	FPrintF (OutFile, "\n\tset_continue2 %d", offset);
}

void GenGetNodeArity (int offset)
{
	put_instruction_ (Iget_node_arity);
	FPrintF (OutFile, "%d", offset);
}

static void GenGetDescArity (int offset)
{
	put_instruction_ (Iget_desc_arity);
	FPrintF (OutFile, "%d", offset);
}

void GenPushArgB (int offset)
{
	put_instruction_ (Ipush_arg_b);
	FPrintF (OutFile, "%d", offset);
}

void GenImport (SymbDef sdef)
{
	if (DoStackLayout){	
		char *name;
		
		name = sdef->sdef_ident->ident_name;

		switch (sdef->sdef_kind){
			case DEFRULE:
			case SYSRULE:
				if (sdef->sdef_mark & (SDEF_USED_CURRIED_MASK | SDEF_USED_LAZILY_MASK)){
					put_directive_b (impdesc);
					FPrintF (OutFile, "e_%s_" D_PREFIX "%s",sdef->sdef_module,name);
				}
				if (sdef->sdef_mark & SDEF_USED_STRICTLY_MASK && sdef->sdef_arfun==NoArrayFun){
					put_directive_b (implab);
					FPrintF (OutFile,"e_%s_" S_PREFIX "%s",sdef->sdef_module,name);
				}
				break;
			case FIELDSELECTOR:
				if (sdef->sdef_mark & SDEF_USED_LAZILY_MASK){
					char *record_name;
	
					record_name	= sdef->sdef_type->type_lhs->ft_symbol->symb_def->sdef_ident->ident_name;
	
					put_directive_b (impdesc);
					FPrintF (OutFile, "e_%s_" D_PREFIX "%s.%s",sdef->sdef_module,record_name,name);	

					put_directive_b (implab);
				
					FPrintF (OutFile, "e_%s_" N_PREFIX "%s.%s",sdef->sdef_module,record_name,name);
				
					if (sdef->sdef_calledwithrootnode)
						FPrintF (OutFile, " e_%s_" EA_PREFIX "%s.%s",sdef->sdef_module,record_name,name);
					else if (sdef->sdef_returnsnode)
						FPutS (" _",OutFile);
				}					
				return;
			case RECORDTYPE:
				if (sdef->sdef_mark & (SDEF_USED_STRICTLY_MASK | SDEF_USED_LAZILY_MASK)){
					put_directive_b (impdesc);
					FPrintF (OutFile, "e_%s_" R_PREFIX "%s",sdef->sdef_module,name);
				}
				
				if (!sdef->sdef_strict_constructor)
					return;

				if (sdef->sdef_mark & SDEF_USED_LAZILY_MASK){
					put_directive_b (impdesc);
					FPrintF (OutFile, "e_%s_" RECORD_D_PREFIX "%s", sdef->sdef_module,name);
					put_directive_b (implab);
					FPrintF (OutFile, "e_%s_" RECORD_N_PREFIX "%s",sdef->sdef_module,name);
				}
				return;
			case CONSTRUCTOR:
				if (!sdef->sdef_strict_constructor)
					return;

				if (sdef->sdef_mark & (SDEF_USED_STRICTLY_MASK | SDEF_USED_LAZILY_MASK)){
					put_directive_b (impdesc);
					FPrintF (OutFile, "e_%s_" CONSTRUCTOR_R_PREFIX "%s",sdef->sdef_module,name);
				}
				if (sdef->sdef_mark & SDEF_USED_LAZILY_MASK){
					put_directive_b (impdesc);
					FPrintF (OutFile, "e_%s_" D_PREFIX "%s", sdef->sdef_module,name);
				}
				break;
			default:
				return;
		}

		if (sdef->sdef_mark & SDEF_USED_LAZILY_MASK){
			put_directive_b (implab);
			FPrintF (OutFile, "e_%s_" N_PREFIX "%s",sdef->sdef_module,name);
			if ((sdef->sdef_calledwithrootnode || sdef->sdef_returnsnode) && 
				!(sdef->sdef_kind==CONSTRUCTOR && !sdef->sdef_strict_constructor))
			{
				if (sdef->sdef_calledwithrootnode)
					FPrintF (OutFile, " e_%s_" EA_PREFIX "%s",sdef->sdef_module,name);
				else
					FPutS (" _",OutFile);
			}
		}
	}	
}

void GenExportStrictAndEaEntry (SymbDef sdef)
{
	char *name;
	
	name = sdef->sdef_ident->ident_name;
	
	put_directive_ (Dexport);
	FPrintF (OutFile, "e_%s_" S_PREFIX "%s", CurrentModule,name);

	if (sdef->sdef_calledwithrootnode){
		put_directive_ (Dexport);
		FPrintF (OutFile, "e_%s_%s%s", CurrentModule, ea_pref, name);
	}
}

void GenExportFieldSelector (SymbDef sdef)
{
	char *name;
	char *record_name;
	
	name = sdef->sdef_ident->ident_name;
	
	record_name=sdef->sdef_type->type_lhs->ft_symbol->symb_def->sdef_ident->ident_name;

	put_directive_ (Dexport);
	FPrintF (OutFile,"e_%s_" D_PREFIX "%s.%s",CurrentModule,record_name,name);
	put_directive_ (Dexport);
	FPrintF (OutFile,"e_%s_" N_PREFIX "%s.%s",CurrentModule,record_name,name);

	if (sdef->sdef_calledwithrootnode){
		put_directive_ (Dexport);
		FPrintF (OutFile,"e_%s_" EA_PREFIX "%s.%s",CurrentModule,record_name,name);
	}
}

void GenExportEaEntry (SymbDef sdef)
{
	if (sdef->sdef_calledwithrootnode){
		put_directive_ (Dexport);
		FPrintF (OutFile,"e_%s_" EA_PREFIX "%s",CurrentModule,sdef->sdef_ident->ident_name);
	}
}

void GenDAStackLayout (int asize)
{
	if (DoStackLayout){
		put_directive_b (d);
		put_arguments_nn_b (asize,0);
	}
}

void GenOAStackLayout (int asize)
{
	if (DoStackLayout){
		put_directive_b (o);
		put_arguments_nn_b (asize,0);
	}
}

void GenDStackLayoutOfStates (int asize,int bsize,int n_states,StateP state_p)
{
	if (DoStackLayout){
		put_directive_b (d);
		if (bsize > 0){
			int i;
	
			put_arguments_nn__b (asize,bsize);

			for (i=0; i<n_states; ++i)
				GenBStackElems (state_p[i]);
		} else
			put_arguments_nn_b (asize,0);
	}
}

void GenOStackLayoutOfStates (int asize,int bsize,int n_states,StateP state_p)
{
	if (DoStackLayout){
		put_directive_b (o);
		if (bsize > 0){
			int i;
			
			put_arguments_nn__b (asize,bsize);

			for (i=0; i<n_states; ++i)
				GenBStackElems (state_p[i]);
		} else
			put_arguments_nn_b (asize,0);
	}
}

void GenDStackLayoutOfState (int asize, int bsize, StateS resultstate)
{
	if (DoStackLayout){
		put_directive_b (d);
		if (bsize > 0){
			put_arguments_nn__b (asize,bsize);
			GenBStackElems (resultstate);
		} else
			put_arguments_nn_b (asize,0);
	}		
}

void GenOStackLayoutOfState (int asize, int bsize, StateS resultstate)
{
	if (DoStackLayout){
		put_directive_b (o);
		if (bsize > 0){
			put_arguments_nn__b (asize,bsize);
			GenBStackElems (resultstate);
		} else
			put_arguments_nn_b (asize,0);
	}		
}

void GenJmpEvalUpdate (void)
{
	put_instruction (Ijmp_eval_upd);
}

void GenNodeEntryDirective (int arity,Label label,Label label2)
{
	if (DoStackLayout){
		put_directive_b (n);
		put_arguments_n__b (arity);

		if (DescriptorNeeded (label->lab_symbol))
2628
			GenDescriptorOrNodeEntryLabel (label);
clean's avatar
clean committed
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
		else
			FPutS (empty_lab.lab_name, OutFile);

		if (label2){
			FPutC (' ', OutFile);
			GenLabel (label2);
		}
#ifdef MEMORY_PROFILING_WITH_N_STRING
		if (DoProfiling && arity>=0 && !DoParallel){
			put_directive_ (Dn_string);
			FPrintF (OutFile,"\"%s\"",label->lab_symbol->sdef_ident->ident_name);
		}
#endif
	}
}

2645
#ifdef NEW_APPLY
2646
2647
2648
2649
2650
2651
void GenApplyEntryDirective (int arity,Label label)
{
	put_directive_b (a);
	put_arguments_n__b (arity);
	GenLabel (label);
}
2652
#endif
2653