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

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

#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
23 24
#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)

clean's avatar
clean committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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_DoWarning				7
#define N_System				8
John van Groningen's avatar
John van Groningen committed
51
#define N_DoFusion				9
52
#define N_Do64BitArch			10
53
#define N_Dynamics				11
54
#define N_DoGenericFusion		12
clean's avatar
clean committed
55

John van Groningen's avatar
John van Groningen committed
56
#define MINIMUM_N_OPTIONS 9
57
#define N_OPTIONS 13
John van Groningen's avatar
John van Groningen committed
58 59

static void ConvertOptionsToString (char *optstring)
clean's avatar
clean committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{
	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_DoWarning]            = DoWarning ? '1' : '0';
	optstring[N_System]               = '0';
75

76
	if (DoFusion || ObjectSizes[RealObj]!=2 || Dynamics || DoGenericFusion){
77 78
		optstring[N_DoFusion] = DoFusion ? '1' : '0';
		optstring[N_Do64BitArch] = ObjectSizes[RealObj]!=2 ? '1' : '0';
79
		optstring[N_Dynamics] = Dynamics ? '1' : '0';
80
		optstring[N_DoGenericFusion] = DoGenericFusion ? '1' : '0';
John van Groningen's avatar
John van Groningen committed
81 82 83
		optstring[N_OPTIONS]='\0';
	} else
		optstring[MINIMUM_N_OPTIONS]='\0';
clean's avatar
clean committed
84 85 86 87 88 89 90
}

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

#define EA_PREFIX "ea"
91
#define EU_PREFIX "eu"
clean's avatar
clean committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105
#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)
{
106
	OutFile = FOpen (fname, "w");
clean's avatar
clean committed
107 108

	if (OutFile!=NULL){
109
#if defined (POWER)
clean's avatar
clean committed
110 111 112 113 114 115 116 117 118 119 120
		setvbuf ((FILE*) OutFile, NULL, _IOFBF, 8192);
#endif
		OpenedFile = OutFile;
		ABCFileName = fname;
		return True;
	} else
		return False;
}

void WriteLastNewlineToABCFile (void)
{
121
	PutCOutFile ('\n');
clean's avatar
clean committed
122 123 124 125 126
}

void CloseABCFile (char *fname)
{
	if (OutFile){
127
		if (FClose (OutFile) != 0){
clean's avatar
clean committed
128 129 130
			CompilerError = True;
			CurrentLine = 0;
			
131
			StaticMessage_s_s (True, "<open file>", "Write error (disk full?)");
clean's avatar
clean committed
132 133
		}
		if (CompilerError)
134
			FDelete (fname);
clean's avatar
clean committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		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)
157
			FPrintF (OutFile,"e_%s_%s%s",module_name,label->lab_pref,def->sdef_name);
clean's avatar
clean committed
158 159
		else if (DoDebug){
			if (def->sdef_kind==IMPRULE)
160
				FPrintF (OutFile, "%s%s.%u",label->lab_pref,def->sdef_name,def->sdef_number);
clean's avatar
clean committed
161
			else
162
				FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_name);
clean's avatar
clean committed
163
		} else if (def->sdef_number==0)
164
			FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_name);
clean's avatar
clean committed
165 166 167 168 169
		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 {
170 171
		PutSOutFile (label->lab_pref);
		PutSOutFile (label->lab_name);
clean's avatar
clean committed
172 173 174 175 176
	}
	if (label->lab_post!=0)
		FPrintF (OutFile,".%u",label->lab_post);
}

177 178 179 180 181 182 183 184 185 186
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)
187
			FPrintF (OutFile,"e_%s_%s%s",module_name,label->lab_pref,def->sdef_name);
188 189
		else if (ExportLocalLabels){
			if (def->sdef_kind==IMPRULE)
190
				FPrintF (OutFile,"e_%s_%s%s.%u",CurrentModule,label->lab_pref,def->sdef_name,def->sdef_number);
191
			else
192
				FPrintF (OutFile,"e_%s_%s%s",CurrentModule,label->lab_pref,def->sdef_name);
193 194
		} else if (DoDebug){
			if (def->sdef_kind==IMPRULE)
195
				FPrintF (OutFile, "%s%s.%u",label->lab_pref,def->sdef_name,def->sdef_number);
196
			else
197
				FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_name);
198
		} else if (def->sdef_number==0)
199
			FPrintF (OutFile, "%s%s",label->lab_pref,def->sdef_name);
200 201 202 203 204
		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 {
205 206
		PutSOutFile (label->lab_pref);
		PutSOutFile (label->lab_name);
207 208 209 210 211
	}
	if (label->lab_post!=0)
		FPrintF (OutFile,".%u",label->lab_post);
}

clean's avatar
clean committed
212 213 214 215
#if !BINARY_ABC

#define put_instructionb(a) put_instruction(I##a)
#define put_instruction_b(a) put_instruction_(I##a)
216
#define put_directiveb(a) put_directive(D##a)
clean's avatar
clean committed
217
#define put_directive_b(a) put_directive_(D##a)
218 219 220 221
#define put_argumentsi_b(i1) FPrintF (OutFile,"%s",(i1))
#define put_argumentsin_b(i1,n1) FPrintF (OutFile,"%s %d",(i1),(n1))
#define put_argumentsn_b(n1) FPrintF (OutFile,"%d",(n1))
#define put_argumentsnn_b(n1,n2) FPrintF (OutFile,"%d %d",(n1),(n2))
222 223 224
#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))
225
#define put_argumentsn__b(n1) FPrintF (OutFile,"%d ",(n1))
226
#define put_arguments_nn__b(n1,n2) FPrintF (OutFile," %d %d ",(n1),(n2))
227 228 229
#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))
clean's avatar
clean committed
230 231 232 233 234 235 236 237 238 239 240

#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)
241
#define put_directiveb(a) if (DoDebug) put_directive(D##a); else put_instruction_code(C##a)
clean's avatar
clean committed
242 243 244 245 246
#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)){
247
		PutCOutFile (128+(n & 127));
clean's avatar
clean committed
248 249 250
		n=n>>7;
	}

251
	PutCOutFile (n+64);
clean's avatar
clean committed
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
}

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;
}

282
static void put_argumentsi_b (char *i1)
clean's avatar
clean committed
283 284
{
	if (DoDebug)
285
		PutSOutFile (i1);
clean's avatar
clean committed
286 287 288 289
	else
		put_n (integer_string_to_integer (i1));
}

290
static void put_argumentsin_b (char *i1,long n1)
clean's avatar
clean committed
291 292 293 294 295 296 297 298 299
{
	if (DoDebug)
		FPrintF (OutFile,"%s %d",(i1),(n1));
	else {
		put_n (integer_string_to_integer (i1));
		put_n (n1);
	}
}

300
static void put_argumentsn_b (long n1)
clean's avatar
clean committed
301 302 303 304 305 306 307
{
	if (DoDebug)
		FPrintF (OutFile,"%d",(n1));
	else
		put_n (n1);
}

308
static void put_argumentsnn_b (long n1,long n2)
clean's avatar
clean committed
309 310 311 312 313 314 315 316 317
{
	if (DoDebug)
		FPrintF (OutFile,"%d %d",(n1),(n2));
	else {
		put_n (n1);
		put_n (n2);
	}
}

318
static void put_arguments_nnn_b (long n1,long n2,long n3)
clean's avatar
clean committed
319 320
{
	if (DoDebug)
321
		FPrintF (OutFile," %d %d %d",(n1),(n2),(n3));
clean's avatar
clean committed
322 323 324 325 326 327 328
	else {
		put_n (n1);
		put_n (n2);
		put_n (n3);
	}
}

329
static void put_arguments_nnnn_b (long n1,long n2,long n3,long n4)
clean's avatar
clean committed
330 331
{
	if (DoDebug)	
332
		FPrintF (OutFile," %d %d %d %d",(n1),(n2),(n3),(n4));
clean's avatar
clean committed
333 334 335 336 337 338 339 340
	else {
		put_n (n1);
		put_n (n2);
		put_n (n3);
		put_n (n4);
	}
}

341
static void put_arguments_nnnnn_b (long n1,long n2,long n3,long n4,long n5)
clean's avatar
clean committed
342 343
{
	if (DoDebug)
344
		FPrintF (OutFile," %d %d %d %d %d",(n1),(n2),(n3),(n4),(n5));
clean's avatar
clean committed
345 346 347 348 349 350 351 352 353
	else {
		put_n (n1);
		put_n (n2);
		put_n (n3);
		put_n (n4);
		put_n (n5);
	}
}

354
static void put_argumentsn__b (long n1)
clean's avatar
clean committed
355 356 357 358 359 360 361
{
	if (DoDebug)
		FPrintF (OutFile,"%d ",(n1));
	else
		put_n (n1);
}

362
static void put_arguments_n_b (long n1)
clean's avatar
clean committed
363 364 365 366
{
	if (DoDebug)
		FPrintF (OutFile," %d",(n1));
	else {
367
		PutCOutFile (' ',OutFile);
clean's avatar
clean committed
368 369 370 371
		put_n (n1);
	}
}

372
static void put_arguments_n__b (long n1)
clean's avatar
clean committed
373 374 375 376
{
	if (DoDebug)
		FPrintF (OutFile," %d ",(n1));
	else {
377
		PutCOutFile (' ',OutFile);
clean's avatar
clean committed
378 379 380 381
		put_n (n1);
	}
}

382
static void put_arguments_nn__b (long n1,long n2)
clean's avatar
clean committed
383 384
{
	if (DoDebug)
385
		FPrintF (OutFile," %d %d ",(n1),(n2));
clean's avatar
clean committed
386 387 388 389 390 391
	else {
		put_n (n1);
		put_n (n2);
	}
}

392
static void put_arguments_nn_b (long n1,long n2)
clean's avatar
clean committed
393 394 395 396
{
	if (DoDebug)
		FPrintF (OutFile," %d %d",(n1),(n2));
	else {
397
		PutCOutFile (' ',OutFile);
clean's avatar
clean committed
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
		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,
464
	Cimpmod,
clean's avatar
clean committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	Cn
};
#endif

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

#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 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 IeqAC_a "eqAC_a"

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

503 504
#define InotB "notB"

clean's avatar
clean committed
505 506 507 508
#define IpushB "pushB"
#define IpushI "pushI"
#define IpushC "pushC"
#define IpushR "pushR"
509 510
#define IpushZR "pushZR"
#define IpushZ "pushZ"
clean's avatar
clean committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

#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"
535
#define Ipush_r_arg_u "push_r_arg_u"
clean's avatar
clean committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
#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"
551
#define Ifill3 "fill3"
clean's avatar
clean committed
552 553 554 555 556 557 558 559 560 561 562

#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"

563
#define Ibuildhr "buildhr"
clean's avatar
clean committed
564 565 566 567 568 569 570 571
#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"
572
#define Ijsr_ap "jsr_ap"
573
#define Ijsr_i "jsr_i"
clean's avatar
clean committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

#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"
605
#define Ijmp_ap "jmp_ap"
606
#define Ijmp_ap_upd "jmp_ap_upd"
607 608
#define Ijmp_i "jmp_i"
#define Ijmp_not_eqZ "jmp_not_eqZ"
609
#define Ijmp_upd "jmp_upd"
clean's avatar
clean committed
610 611 612 613 614 615 616 617 618 619 620 621

#define Ihalt "halt"

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

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

static void put_instruction (char *instruction)
{
622 623
	PutCOutFile ('\n');
	PutCOutFile ('\t');
624
	PutSOutFile (instruction);
clean's avatar
clean committed
625 626 627 628
}

static void put_instruction_ (char *instruction)
{
629 630
	PutCOutFile ('\n');
	PutCOutFile ('\t');
631
	PutSOutFile (instruction);
632
	PutCOutFile (' ');
clean's avatar
clean committed
633 634 635 636
}

static void put_instruction_code (int instruction_code)
{
637
	PutCOutFile (instruction_code);
clean's avatar
clean committed
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 667 668 669 670 671 672 673
static void GenGetWL (int offset)
{
	put_instruction ("getWL");
	put_arguments_n_b (offset);
}

static void GenPutWL (int offset)
{
	put_instruction ("putWL");
	put_arguments_n_b (offset);
}

static void GenRelease (void)
{
	put_instruction ("release");
}

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;
		}
	}
}

674
#define Da "a"
675
#define Dai "ai"
clean's avatar
clean committed
676 677 678 679 680
#define Dkeep "keep"
#define Dd "d"
#define Do "o"
#define Dimpdesc "impdesc"
#define Dimplab "implab"
681
#define Dimpmod "impmod"
clean's avatar
clean committed
682 683 684 685 686
#define Dexport "export"
#define Dn "n"
#define Dnu "nu"
#define Dn_string "n_string"
#define Ddesc "desc"
687
#define Ddesc0 "desc0"
clean's avatar
clean committed
688
#define Ddescn "descn"
689
#define Ddescs "descs"
clean's avatar
clean committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
#define Ddescexp "descexp"
#define Drecord "record"
#define Dmodule "module"
#define Ddepend "depend"
#define Dcomp "comp"
#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)
{
710 711
	PutCOutFile ('\n');
	PutCOutFile ('.');
712
	PutSOutFile (directive);
clean's avatar
clean committed
713 714 715 716
}

static void put_directive_ (char *directive)
{
717 718
	PutCOutFile ('\n');
	PutCOutFile ('.');
719
	PutSOutFile (directive);
720
	PutCOutFile (' ');
clean's avatar
clean committed
721 722
}

723
static void put_first_directive (char *directive)
clean's avatar
clean committed
724
{
725
	PutCOutFile ('.');
726
	PutSOutFile (directive);
clean's avatar
clean committed
727 728 729 730 731 732 733 734
}

void BuildBasicFromB (ObjectKind kind,int b_offset)
{
	switch (kind){
		case IntObj:
		case ProcIdObj:
		case RedIdObj:
735
			put_instructionb (buildI_b); break;
clean's avatar
clean committed
736
		case BoolObj:
737
			put_instructionb (buildB_b); break;
clean's avatar
clean committed
738
		case CharObj:
739
			put_instructionb (buildC_b); break;
clean's avatar
clean committed
740
		case RealObj:
741
			put_instructionb (buildR_b); break;
clean's avatar
clean committed
742
		case FileObj:
743
			put_instructionb (buildF_b); break;
clean's avatar
clean committed
744 745 746 747 748
		default:
			error_in_function ("BuildBasicFromB");
			return;
	}

749
	put_arguments_n_b (b_offset);
clean's avatar
clean committed
750 751 752 753 754 755 756 757 758
}

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				*/
759
			put_instructionb (fillI_b); break;
clean's avatar
clean committed
760
		case BoolObj:
761
			put_instructionb (fillB_b); break;
clean's avatar
clean committed
762
		case CharObj:
763
			put_instructionb (fillC_b); break;
clean's avatar
clean committed
764
		case RealObj:
765
			put_instructionb (fillR_b); break;
clean's avatar
clean committed
766
		case FileObj:
767
			put_instructionb (fillF_b); break;
clean's avatar
clean committed
768 769 770 771
		default:
			error_in_function ("FillBasicFromB");
			return;
	}
772
	put_arguments_nn_b (boffs,aoffs);
clean's avatar
clean committed
773 774
	TreatWaitListAfterFill (aoffs, fkind);
}
John van Groningen's avatar
John van Groningen committed
775

clean's avatar
clean committed
776 777 778 779 780
void BuildBasic (ObjectKind obj,SymbValue val)
{
	switch (obj){
		case IntObj:
			put_instruction_b (buildI);
781
			put_argumentsi_b (val.val_int);
clean's avatar
clean committed
782 783 784
			break;
		case BoolObj:
			put_instruction_ (IbuildB);
785
			PutSOutFile (val.val_bool ? "TRUE" : "FALSE");
clean's avatar
clean committed
786 787 788
			break;
		case CharObj:
			put_instruction_ (IbuildC);
789
			PutSOutFile (val.val_char);
clean's avatar
clean committed
790 791 792
			break;
		case RealObj:
			put_instruction_ (IbuildR);
793
			PutSOutFile (val.val_real);
clean's avatar
clean committed
794 795 796 797 798 799 800 801 802 803 804 805 806
			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);
807
			put_argumentsin_b (val.val_int,offset);
clean's avatar
clean committed
808 809
			break;
		case BoolObj:
810 811 812
			put_instruction (IfillB);
			PutSOutFile (val.val_bool ? " TRUE" : " FALSE");
			put_arguments_n_b (offset);
clean's avatar
clean committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
			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;
		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);
834
			put_argumentsin_b (val.val_int,offset);
clean's avatar
clean committed
835 836
			break;
		case BoolObj:
837 838 839
			put_instruction (IeqB_a);
			PutSOutFile (val.val_bool ? " TRUE" : " FALSE");
			put_arguments_n_b (offset);
clean's avatar
clean committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
			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;
		default:
			error_in_function ("IsBasic");
			return;
	}
}

void IsString (SymbValue val)
{
	put_instruction_ (IeqAC_a);
856
	PutSOutFile (val.val_string);
clean's avatar
clean committed
857 858 859 860 861 862 863
}

void PushBasic (ObjectKind obj, SymbValue val)
{
	switch (obj){
		case IntObj:
			put_instruction_b (pushI);
864
			put_argumentsi_b (val.val_int);
clean's avatar
clean committed
865 866 867
			break;
		case BoolObj:
			put_instruction_ (IpushB);
868
			PutSOutFile (val.val_bool ? "TRUE" : "FALSE");
clean's avatar
clean committed
869 870 871
			break;
		case CharObj:
			put_instruction_ (IpushC);
872 873
			PutSOutFile (val.val_char);
			break;
clean's avatar
clean committed
874 875
		case RealObj:
			put_instruction_ (IpushR);
876 877
			PutSOutFile (val.val_real);
			break;
clean's avatar
clean committed
878 879 880 881 882 883 884 885
		default:
			error_in_function ("PushBasic");
			return;
	}
}

void GenPushReducerId (int i)
{
886 887
	put_instructionb (pushI);
	put_arguments_n_b (i);
clean's avatar
clean committed
888 889 890 891
}

void GenPushArgNr (int argnr)
{
892 893
	put_instructionb (pushI);
	put_arguments_n_b (argnr);
clean's avatar
clean committed
894 895 896 897 898 899 900
}

void EqBasic (ObjectKind obj, SymbValue val, int offset)
{
	switch (obj){
		case IntObj:
			put_instruction_b (eqI_b);
901
			put_argumentsin_b (val.val_int,offset);
clean's avatar
clean committed
902 903
			break;
		case BoolObj:
904 905 906
			put_instruction (IeqB_b);
			PutSOutFile (val.val_bool ? " TRUE" : " FALSE");
			put_arguments_n_b (offset);
clean's avatar
clean committed
907 908 909
			break;
		case CharObj:
			put_instruction_ (IeqC_b);
910 911
			FPrintF (OutFile, "%s %d", val.val_char, offset);
			break;
clean's avatar
clean committed
912 913
		case RealObj:
			put_instruction_ (IeqR_b);
914 915
			FPrintF (OutFile, "%s %d", val.val_real, offset);
			break;
clean's avatar
clean committed
916 917 918 919 920 921
		default:
			error_in_function ("EqBasic");
			return;
	}
}

922 923 924 925 926
void GenNotB (void)
{
	put_instruction (InotB);
}

clean's avatar
clean committed
927 928 929 930 931 932
void PushBasicFromAOnB (ObjectKind kind,int offset)
{
	switch (kind){
		case IntObj:
		case ProcIdObj:
		case RedIdObj:
933
			put_instructionb (pushI_a);
clean's avatar
clean committed
934 935
			break;
		case BoolObj:
936
			put_instructionb (pushB_a);
clean's avatar
clean committed
937 938
			break;
		case CharObj:
939
			put_instructionb (pushC_a);
clean's avatar
clean committed
940 941
			break;
		case RealObj:
942
			put_instructionb (pushR_a);
clean's avatar
clean committed
943 944
			break;
		case FileObj:
945
			put_instructionb (pushF_a);
clean's avatar
clean committed
946 947 948 949 950
			break;
		default:
			error_in_function ("PushBasicFromAOnB");
			return;
	}
951
	put_arguments_n_b (offset);
clean's avatar
clean committed
952 953 954 955
}

void GenPushD_a (int a_offset)
{
956 957
	put_instruction (IpushD_a);
	put_arguments_n_b (a_offset);
clean's avatar
clean committed
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
}
	
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'.
*/

1030
static char BElems[] = BASIC_ELEMS_STRING;
clean's avatar
clean committed
1031 1032 1033 1034 1035

static void GenBStackElems (StateS state)
{
	if (IsSimpleState (state)){
		if (state.state_kind == OnB)
1036
			PutCOutFile (BElems [(int) state.state_object]);			
clean's avatar
clean committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	} 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)
1063
			PutCOutFile (BElems [(int) state.state_object]);
clean's avatar
clean committed
1064
		else
1065
			PutCOutFile ('a');
clean's avatar
clean committed
1066 1067 1068 1069 1070 1071
	} else {
		int arity;
		States argstates;
		
		switch (state.state_type){
			case TupleState:
1072
				argstates = state.state_tuple_arguments;			
1073
				PutCOutFile ('(');
1074 1075 1076
				if (state.state_arity>0){
					GenABStackElems (argstates[0]);
					for (arity=1; arity < state.state_arity; arity++){
1077
						PutCOutFile (',');
1078 1079 1080
						GenABStackElems (argstates[arity]);
					}
				}
1081
				PutCOutFile (')');
clean's avatar
clean committed
1082 1083 1084
				break;
			case RecordState:
				argstates = state.state_record_arguments;
1085
				PutCOutFile ('(');
1086 1087
				for (arity=0; arity < state.state_arity; arity++)
					GenABStackElems (argstates[arity]);
1088
				PutCOutFile (')');
1089
				return;
clean's avatar
clean committed
1090
			case ArrayState:
1091
				PutCOutFile ('a');
clean's avatar
clean committed
1092 1093 1094 1095 1096 1097 1098 1099
				return;
			default:
				error_in_function ("GenABStackElems");
				return;
		}
	}
}

1100 1101 1102 1103
static void GenABStackElemsForRecordDesc (StateS state)
{
	if (IsSimpleState (state)){
		if (state.state_kind == OnB)
1104
			PutCOutFile (BElems [(int) state.state_object]);
1105
		else
1106
			PutCOutFile ('a');
1107 1108 1109 1110 1111 1112 1113
	} else {
		int arity;
		States argstates;
		
		switch (state.state_type){
			case TupleState:
				argstates = state.state_tuple_arguments;			
1114
				PutCOutFile ('(');
1115 1116 1117
				if (state.state_arity>0){
					GenABStackElemsForRecordDesc (argstates[0]);
					for (arity=1; arity < state.state_arity; ++arity){
1118
						PutCOutFile (',');
1119 1120 1121
						GenABStackElemsForRecordDesc (argstates[arity]);
					}
				}
1122
				PutCOutFile (')');
1123
				return;
1124 1125
			case RecordState:
				argstates = state.state_record_arguments;
1126
				PutCOutFile ('{');
1127
				for (arity=0; arity < state.state_arity; ++arity)
1128
					GenABStackElemsForRecordDesc (argstates[arity]);
1129
				PutCOutFile ('}');
1130 1131
				return;
			case ArrayState:
1132
				PutCOutFile ('a');
1133 1134 1135 1136 1137 1138 1139
				return;
			default:
				error_in_function ("GenABStackElemsForRecordDesc");
		}
	}
}

1140 1141 1142 1143 1144 1145 1146 1147
static void GenABStackElemsOfRecord (StateS state)
{
	if (state.state_type==RecordState){
		int arity;
		States argstates;

		argstates = state.state_record_arguments;
		for (arity=0; arity < state.state_arity; ++arity)
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
			GenABStackElemsForRecordDesc (argstates[arity]);
	} else
		GenABStackElemsForRecordDesc (state);
}

static int AddSizeOfStatesAndImportRecords (int arity, States states, int *asize, int *bsize);

static int AddSizeOfStateAndImportRecords (StateS state, int *asize, int *bsize)
{
	if (IsSimpleState (state)){
		if (state.state_kind == OnB)
			*bsize += ObjectSizes [state.state_object];
		else if (state.state_kind != Undefined)
			*asize += SizeOfAStackElem;
		return 0;
	} else {
		switch (state.state_type){
			case RecordState:
			{
				SymbDef record_sdef;

				record_sdef = state.state_record_symbol;
				if (record_sdef->sdef_exported || record_sdef->sdef_module!=CurrentModule || ExportLocalLabels){
					if ((record_sdef->sdef_mark & SDEF_RECORD_R_LABEL_IMPORTED_MASK)!=0){
						record_sdef->sdef_mark |= SDEF_USED_STRICTLY_MASK;
					} else {
						record_sdef->sdef_mark |= SDEF_USED_STRICTLY_MASK;
						record_sdef->sdef_mark |= SDEF_USED_STRICTLY_MASK | SDEF_RECORD_R_LABEL_IMPORTED_MASK;
1176
						GenImpRecordDesc (record_sdef->sdef_module,record_sdef->sdef_name);
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
					}
				}

				(void) AddSizeOfStatesAndImportRecords (state.state_arity, state.state_record_arguments, asize, bsize);
				return 1;
			}
			case TupleState:
				return AddSizeOfStatesAndImportRecords (state.state_arity, state.state_tuple_arguments, asize, bsize);
			case ArrayState:
				*asize += SizeOfAStackElem;
				return 0;
		}
	}
	return 0;
}

static int AddSizeOfStatesAndImportRecords (int arity, States states, int *asize, int *bsize)
{
	int has_unboxed_record;
	
	has_unboxed_record=0;
	for (; arity; arity--)
		has_unboxed_record |= AddSizeOfStateAndImportRecords (states [arity-1], asize, bsize);
	return has_unboxed_record;
}

static void GenUnboxedRecordLabelsReversed (StateS state)
{
	if (!IsSimpleState (state)){
		int arity;
		States argstates;
		
		switch (state.state_type){
			case TupleState:
				argstates = state.state_tuple_arguments;			
				for (arity=state.state_arity-1; arity>=0; --arity)
					GenUnboxedRecordLabelsReversed (argstates[arity]);
				return;
			case RecordState:
			{
				SymbDef record_sdef;

				argstates = state.state_record_arguments;
				for (arity=state.state_arity-1; arity>=0 ; --arity)
					GenUnboxedRecordLabelsReversed (argstates[arity]);

				record_sdef = state.state_record_symbol;
				if (!record_sdef->sdef_exported && record_sdef->sdef_module==CurrentModule && !ExportLocalLabels){
					if (DoDebug)
1226
						FPrintF (OutFile, R_PREFIX "%s ",record_sdef->sdef_name);
1227 1228 1229
					else
						FPrintF (OutFile, R_PREFIX "%u ",record_sdef->sdef_number);
				} else
1230
					FPrintF (OutFile, "e_%s_" R_PREFIX "%s ",record_sdef->sdef_module,record_sdef->sdef_name);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
				return;
			}
			case ArrayState:
				return;
			default:
				error_in_function ("GenUnboxedRecordLabelsReversed");
				return;
		}
	}
}

static void GenUnboxedRecordLabelsReversedForRecord (StateS state)
{
	if (state.state_type==RecordState){
		int arity;
		States argstates;

		argstates = state.state_record_arguments;
		for (arity=state.state_arity-1; arity>=0; --arity)
			GenUnboxedRecordLabelsReversed (argstates[arity]);
1251
	} else
1252
		GenUnboxedRecordLabelsReversed (state);
1253 1254
}

clean's avatar
clean committed
1255 1256 1257
void GenDStackLayout (int asize,int bsize,Args fun_args)
{
	if (DoStackLayout){
1258
		put_directiveb (d);
clean's avatar
clean committed
1259
		if (bsize > 0){
1260 1261
			put_arguments_nn_b (asize,bsize);
			PutCOutFile (' ');
clean's avatar
clean committed
1262 1263 1264 1265 1266 1267

			while (fun_args!=NULL){
				GenBStackElems (fun_args->arg_state);
				fun_args=fun_args->arg_next;
			}
		} else
1268
			put_arguments_nn_b (asize,0);
clean's avatar
clean committed
1269 1270 1271 1272 1273 1274
	}
}

void GenOStackLayout (int asize,int bsize,Args fun_args)
{
	if (DoStackLayout){
1275
		put_directiveb (o);
clean's avatar
clean committed
1276
		if (bsize > 0){
1277 1278
			put_arguments_nn_b (asize,bsize);
			PutCOutFile (' ');
clean's avatar
clean committed
1279 1280 1281 1282 1283 1284

			while (fun_args!=NULL){
				GenBStackElems (fun_args->arg_state);
				fun_args=fun_args->arg_next;
			}
		} else
1285
			put_arguments_nn_b (asize,0);
clean's avatar
clean committed
1286 1287 1288
	}
}

1289 1290 1291 1292 1293 1294 1295
static void GenABCInstructions (Instructions ilist)
{
	for (; ilist; ilist = ilist->instr_next){
		char *instruction_name;
		
		instruction_name=ilist->instr_this;
		
1296
		PutCOutFile ('\n');
1297
		if (instruction_name[0]==':')
1298
			PutSOutFile (&instruction_name[1]);
1299 1300
		else {
			if (instruction_name[0]!='.')
1301
				PutCOutFile ('\t');
1302
			PutSOutFile (instruction_name);
1303 1304 1305 1306
		}
	}
}

clean's avatar
clean committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322