bcgen_instructions.c 135 KB
Newer Older
1
#include "bcgen_instructions.h"
Erin van der Veen's avatar
Erin van der Veen committed
2

3 4 5 6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

7 8 9
#include "abc_instructions.h"
#include "bcgen.h"
#include "util.h"
10

11
#define max_implemented_instruction_n CMAX-1
12

13
struct program pgrm;
14
uint32_t last_d, last_jsr_with_d;
15

16
struct label_node *labels;
Camil Staps's avatar
Camil Staps committed
17
uint32_t label_id;
18 19
uint32_t global_label_count;
uint32_t global_label_string_count;
20
static struct label *start_label;
21

22
uint32_t module_n;
23

24
uint32_t list_code = 0;
25

26 27
uint32_t allocated_code_size;
uint32_t allocated_data_size;
28
uint32_t allocated_strings_size;
29 30
uint32_t allocated_code_relocations_size;
uint32_t allocated_data_relocations_size;
31

32
#define N_ADD_ARG_LABELS 32
33
static uint32_t Fadd_arg_label_used[N_ADD_ARG_LABELS];
34
#ifndef LINK_CLEAN_RUNTIME
35
static int general_add_arg_label_used=0;
36
#endif
37

Camil Staps's avatar
Camil Staps committed
38
#ifndef LINK_CLEAN_RUNTIME
39 40 41 42 43 44 45 46 47
int16_t warned_unsupported_instructions[128]={-1};
int warned_unsupported_instructions_i=0;
void unsupported_instruction_warning(int16_t instruction) {
	for (int i=0; i<warned_unsupported_instructions_i; i++)
		if (warned_unsupported_instructions[i] == instruction)
			return;
	warned_unsupported_instructions[warned_unsupported_instructions_i++] = instruction;
	fprintf(stderr,"Warning: instruction %s is not supported by the interpreter\n",instruction_name(instruction));
}
Camil Staps's avatar
Camil Staps committed
48
#endif
49

50
struct program *initialize_code(void) {
51 52 53 54 55
	int i;
	for(i = 0; i < N_ADD_ARG_LABELS; ++i)
		Fadd_arg_label_used[i] = 0;

	labels = NULL;
Camil Staps's avatar
Camil Staps committed
56
	label_id = 0;
57 58
	global_label_count = 0;
	global_label_string_count = 0;
Camil Staps's avatar
Camil Staps committed
59

60 61
	module_n = 0;

62 63 64 65 66
	pgrm = (struct program) {
		0,
		0,
		0,
		0,
67
		0,
68
		0,
69
		0,
70
		(struct word*) safe_malloc(512 * sizeof(struct word)),
71
		(uint32_t*) safe_malloc(512 * sizeof(uint32_t)),
72
		(uint64_t*) safe_malloc(512 * sizeof(uint64_t)),
73 74 75 76
		(relocation*) safe_malloc(512 * sizeof(relocation)),
		(relocation*) safe_malloc(512 * sizeof(relocation)),
		};

77 78
	allocated_code_size = 512;
	allocated_data_size = 512;
79
	allocated_strings_size = 512;
80 81
	allocated_code_relocations_size = 512;
	allocated_data_relocations_size = 512;
82 83

	return &pgrm;
84 85
}

Camil Staps's avatar
Camil Staps committed
86
#ifndef LINK_CLEAN_RUNTIME
87 88
void code_next_module(void) {
	module_n++;
89 90 91

	last_d = 0;
	last_jsr_with_d = 0;
92
}
Camil Staps's avatar
Camil Staps committed
93
#endif
94 95 96

static void realloc_code(void) {
	allocated_code_size *= 2;
97
	pgrm.code = (struct word*) safe_realloc(pgrm.code, allocated_code_size * sizeof(struct word));
98 99
}

100 101
static void realloc_strings(void) {
	allocated_strings_size *= 2;
102
	pgrm.strings = (uint32_t*) safe_realloc(pgrm.strings, allocated_strings_size * sizeof(uint32_t));
103 104
}

105 106
static void realloc_data(void) {
	allocated_data_size *= 2;
107
	pgrm.data = (uint64_t*) safe_realloc(pgrm.data, allocated_data_size * sizeof(uint64_t));
108 109
}

110
void store_code_elem(uint8_t bytewidth, uint64_t value) {
111 112 113
	if (pgrm.code_size >= allocated_code_size)
		realloc_code();

114
	pgrm.code[pgrm.code_size++] = (struct word) {bytewidth, value};
115
	pgrm.code_byte_size+=bytewidth;
116 117 118 119 120 121 122 123 124
}

struct label *enter_label(char *label_name) {
	struct label_node **label_node_pp;

	label_node_pp = &labels;
	while (*label_node_pp!=NULL) {
		struct label_node *label_node_p;
		int r;
Camil Staps's avatar
Camil Staps committed
125

126 127 128 129 130 131
		label_node_p=*label_node_pp;
		r=strcmp(label_name,label_node_p->label_node_label_p->label_name);
		if (r==0) {
			if (label_node_p->label_node_label_p->label_module_n>=0 && label_node_p->label_node_label_p->label_module_n!=module_n) {
				if (label_node_p->label_node_label_p->label_offset<0) {
					label_node_p->label_node_label_p->label_module_n = -1;
Camil Staps's avatar
Camil Staps committed
132

133 134 135
					return label_node_p->label_node_label_p;
				} else {
					struct label *new_label_p;
Camil Staps's avatar
Camil Staps committed
136

137
					new_label_p = (struct label*) safe_malloc(sizeof(struct label));
138
					new_label_p->label_name=label_node_p->label_node_label_p->label_name;
Camil Staps's avatar
Camil Staps committed
139
					new_label_p->label_id=label_id++;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
					new_label_p->label_module_n=module_n;
					new_label_p->label_offset=-1;

					label_node_p->label_node_label_p=new_label_p;
					return new_label_p;
				}
			}

			return label_node_p->label_node_label_p;
		}
		if (r<0)
			label_node_pp=&label_node_p->label_node_left;
		else
			label_node_pp=&label_node_p->label_node_right;
	}

	struct label_node *new_label_node_p;
	struct label *new_label_p;

159 160
	new_label_node_p = (struct label_node*) safe_malloc(sizeof(struct label_node));
	new_label_p = (struct label*) safe_malloc(sizeof(struct label));
161 162 163
	new_label_node_p->label_node_left = NULL;
	new_label_node_p->label_node_right = NULL;
	new_label_node_p->label_node_label_p = new_label_p;
164
	new_label_p->label_name = (char*) safe_malloc(strlen(label_name) + 1);
Camil Staps's avatar
Camil Staps committed
165
	new_label_p->label_id=label_id++;
166 167 168
	strcpy(new_label_p->label_name, label_name);
	new_label_p->label_module_n = module_n;
	new_label_p->label_offset = -1;
Camil Staps's avatar
Camil Staps committed
169

170 171 172 173
	*label_node_pp = new_label_node_p;
	return new_label_p;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#ifdef LINKER
struct label *find_label(char *label_name) {
	struct label_node *label_node_p=labels;

	while (label_node_p!=NULL) {
		int r=strcmp(label_name,label_node_p->label_node_label_p->label_name);
		if (r==0)
			return label_node_p->label_node_label_p;
		else if (r<0)
			label_node_p=label_node_p->label_node_left;
		else
			label_node_p=label_node_p->label_node_right;
	}

	return NULL;
}

191 192
static struct label_node **move_label_tree_to_list(
		struct label_node **dest, struct label_node *tree) {
193
	if (tree->label_node_left!=NULL)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		dest=move_label_tree_to_list(dest,tree->label_node_left);
	*dest++=tree;
	if (tree->label_node_right!=NULL)
		dest=move_label_tree_to_list(dest,tree->label_node_right);
	return dest;
}

static struct label_node **merge_label_tree_with_list(
		struct label_node **dest,struct label_node *tree,
		struct label_node **list,unsigned int *list_i,unsigned int list_size) {
	int i=*list_i;

	if (i>=list_size)
		return move_label_tree_to_list(dest,tree);

	int r=strcmp(list[i]->label_node_label_p->label_name,tree->label_node_label_p->label_name);
	if (r>0) { /* prune: left side does not need to consider label list */
		if (tree->label_node_left!=NULL)
			dest=move_label_tree_to_list(dest,tree->label_node_left);
	} else {
		if (tree->label_node_left!=NULL)
			dest=merge_label_tree_with_list(dest,tree->label_node_left,list,list_i,list_size);

		i=*list_i;
		while (i<list_size && (r=strcmp(list[i]->label_node_label_p->label_name,tree->label_node_label_p->label_name))<0)
			*dest++=list[i++];
		*list_i=i;

		if (r==0) {
			EPRINTF("internal error: %s redefined\n",tree->label_node_label_p->label_name);
			exit(-1);
		}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	}

	*dest++=tree;

	if (tree->label_node_right!=NULL)
		dest=merge_label_tree_with_list(dest,tree->label_node_right,list,list_i,list_size);

	return dest;
}

static struct label_node *update_label_node_pointers(
		struct label_node **sorted_list,int lft,int rgt) {
	if (rgt<lft)
		return NULL;

	if (rgt==lft) {
		sorted_list[lft]->label_node_left=NULL;
		sorted_list[lft]->label_node_right=NULL;
		return sorted_list[lft];
	}

	int middle=lft+(rgt-lft)/2;
	sorted_list[middle]->label_node_left =update_label_node_pointers(sorted_list,lft,middle-1);
	sorted_list[middle]->label_node_right=update_label_node_pointers(sorted_list,middle+1,rgt);
	return sorted_list[middle];
}

void merge_new_labels_and_rebalance(struct label_node **new_labels,unsigned int n_new_labels) {
	for (int i=0; i<n_new_labels; i++) {
		struct label *label=new_labels[i]->label_node_label_p;
		label->label_id=label_id++;
		make_label_global(label);
	}

	if (labels==NULL) {
		labels=update_label_node_pointers(new_labels,0,label_id-1);
		return;
	}

	struct label_node **temp_list=safe_malloc(label_id*sizeof(struct label_node*));
	unsigned int list_i=0;
	struct label_node **temp_list_end=merge_label_tree_with_list(temp_list,labels,new_labels,&list_i,n_new_labels);
	while (list_i<n_new_labels)
		*temp_list_end++=new_labels[list_i++];

	labels=update_label_node_pointers(temp_list,0,label_id-1);

	free(temp_list);
}
#endif

277 278 279 280 281 282 283 284 285
struct label *new_label_at_offset(uint32_t offset) {
	char name[16];
	snprintf(name, 16, "i%x", offset);
	name[15] = '\0';
	struct label *label = enter_label(name);
	label->label_offset = offset;
	return label;
}

Camil Staps's avatar
Camil Staps committed
286 287 288
int internal_label_id = 0;
struct label *new_internal_label(void) {
	char label_name[14];
289
	sprintf(label_name,"%%%x",internal_label_id++);
Camil Staps's avatar
Camil Staps committed
290 291 292 293
	struct label *label = enter_label(label_name);
	return label;
}

294
void make_label_global(struct label *label) {
295 296 297 298 299
	if (label->label_module_n != -1) {
		label->label_module_n = -1;
		global_label_count++;
		global_label_string_count+=strlen(label->label_name);
	}
300 301
}

302
void store_data_l(uint64_t v) {
303 304
	if (pgrm.data_size >= allocated_data_size)
		realloc_data();
305
	*(uint64_t*)&pgrm.data[pgrm.data_size++] = v;
306 307
}

308 309 310 311 312
struct relocation *add_code_relocation(struct label *label, uint32_t offset) {
	if (pgrm.code_reloc_size>=allocated_code_relocations_size) {
		allocated_code_relocations_size *= 2;
		pgrm.code_relocations = (relocation*) safe_realloc(pgrm.code_relocations, allocated_code_relocations_size * sizeof(relocation));
	}
313

314 315 316 317 318 319 320 321
	struct relocation *relocation_p;

	relocation_p=&pgrm.code_relocations[pgrm.code_reloc_size++];
	relocation_p->relocation_offset=offset;
	relocation_p->relocation_label=label;

	return relocation_p;
}
Camil Staps's avatar
Camil Staps committed
322

323 324 325 326
struct relocation *add_data_relocation(struct label *label, uint32_t offset) {
	if (pgrm.data_reloc_size>=allocated_data_relocations_size) {
		allocated_data_relocations_size *= 2;
		pgrm.data_relocations = (relocation*) safe_realloc(pgrm.data_relocations, allocated_data_relocations_size * sizeof(relocation));
327 328
	}

329 330 331 332 333 334 335 336 337
	struct relocation *relocation_p;

	relocation_p=&pgrm.data_relocations[pgrm.data_reloc_size++];
	relocation_p->relocation_offset=offset;
	relocation_p->relocation_label=label;

	return relocation_p;
}

Camil Staps's avatar
Camil Staps committed
338
#ifndef LINK_CLEAN_RUNTIME
339 340
void store_code_internal_label_value(struct label *label,uint32_t offset) {
	add_code_relocation(label, pgrm.code_size);
341
	store_code_elem(2, offset);
342 343
}

344
void store_code_label_value(char *label_name,int32_t offset) {
345
	struct label *label;
Camil Staps's avatar
Camil Staps committed
346

347 348
	label=enter_label(label_name);

349
	add_code_relocation(label, pgrm.code_size);
350

351
	store_code_elem(2, offset);
352 353
}

354
static void store_data_label_value_of_label(struct label *label,uint32_t offset) {
355
	add_data_relocation(label, pgrm.data_size);
356 357 358
	store_data_l(offset);
}

359
void store_data_label_value(char *label_name,uint32_t offset) {
360 361 362 363 364 365
	store_data_label_value_of_label(enter_label (label_name),offset);
}

void add_data2_to_code(int i) {
	if (list_code)
		printf("%d\t.data2 %d\n",pgrm.code_size,i);
Camil Staps's avatar
Camil Staps committed
366

367
	store_code_elem(2, i);
368 369
}

370
void add_data8_to_code(int i) {
371
	if (list_code)
372
		printf("%d\t.data8 %d\n",pgrm.code_size,i);
Camil Staps's avatar
Camil Staps committed
373

374
	store_code_elem(8, i);
375 376
}

377
void add_instruction(int16_t i) {
378 379 380
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s\n",pgrm.code_size,instruction_name (i));

381
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
382 383 384 385
}

void add_code_and_data_offsets(void) {
	int i;
Camil Staps's avatar
Camil Staps committed
386
	size_t code_offset,data_offset;
Camil Staps's avatar
Camil Staps committed
387

Camil Staps's avatar
Camil Staps committed
388 389
	code_offset = (size_t) pgrm.code;
	data_offset = (size_t) pgrm.data;
390

391
	for(i=0; i<pgrm.code_reloc_size; ++i) {
392 393
		struct relocation *relocation_p;

Camil Staps's avatar
Camil Staps committed
394
		relocation_p=&pgrm.code_relocations[i];
395
		if ((relocation_p->relocation_label->label_offset & 1)==0)
396
			*(int32_t*)&pgrm.code[relocation_p->relocation_offset] += code_offset;
397
		else
398
			*(int32_t*)&pgrm.code[relocation_p->relocation_offset] += data_offset;
399 400
	}

401
	for(i=0; i<pgrm.data_reloc_size; ++i) {
402 403
		struct relocation *relocation_p;

Camil Staps's avatar
Camil Staps committed
404
		relocation_p=&pgrm.data_relocations[i];
405
		if ((relocation_p->relocation_label->label_offset & 1)==0)
406
			*(int32_t*)&pgrm.data[relocation_p->relocation_offset] += code_offset;
407
		else
408
			*(int32_t*)&pgrm.data[relocation_p->relocation_offset] += data_offset;
409 410 411
	}
}

412
void add_instruction_label(uint16_t i,char *label_name) {
413 414 415
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s\n",pgrm.code_size,instruction_name (i),label_name);

416
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
417 418 419
	store_code_label_value(label_name,0);
}

420
void add_instruction_label_offset(uint16_t i,char *label_name,uint32_t offset) {
421 422 423
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s%+d\n",pgrm.code_size,instruction_name (i),label_name,offset);

424
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
425 426 427
	store_code_label_value(label_name,offset);
}

428
void add_instruction_label_offset_label(int16_t i,char *label_name1,uint32_t offset,char *label_name2) {
429 430 431
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s%+d %s\n",pgrm.code_size,instruction_name (i),label_name1,offset,label_name2);

432
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
433 434 435 436
	store_code_label_value(label_name1,offset);
	store_code_label_value(label_name2,0);
}

437
void add_instruction_label_offset_w(uint16_t i,char *label_name,uint32_t offset,int32_t n1) {
438 439 440
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s%+d %d\n",pgrm.code_size,instruction_name (i),label_name,offset,(int)n1);

441
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
442
	store_code_label_value(label_name,offset);
443
	store_code_elem(2, n1);
444 445
}

446
void add_instruction_label_offset_w_label(uint16_t i,char *label_name1,uint32_t offset,int32_t n1,char *label_name2) {
447 448 449
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s%+d %d %s\n",pgrm.code_size,instruction_name (i),label_name1,offset,(int)n1,label_name2);

450
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
451
	store_code_label_value(label_name1,offset);
452
	store_code_elem(2, n1);
453 454 455 456
	store_code_label_value(label_name2,0);
}

void add_instruction_label_offset_label_label_offset_label
457
	(int16_t i,char *label_name1,uint32_t offset1,char *label_name2,char *label_name3,uint32_t offset2,char *label_name4) {
458 459 460 461
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s%+d %s %s%+d %s\n",pgrm.code_size,instruction_name (i),label_name1,offset1,label_name2,
																			label_name3,offset2,label_name4);

462
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
463 464 465 466 467 468
	store_code_label_value(label_name1,offset1);
	store_code_label_value(label_name2,0);
	store_code_label_value(label_name3,offset2);
	store_code_label_value(label_name4,0);
}

469
void add_instruction_label_w_w(int16_t i,char *label_name,int32_t n1,int32_t n2) {
470 471 472
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %s %d %d\n",pgrm.code_size,instruction_name (i),label_name,(int)n1,(int)n2);

473
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
474
	store_code_label_value(label_name,0);
475 476
	store_code_elem(2, n1);
	store_code_elem(2, n2);
477 478
}

479
void add_instruction_c(int16_t i,char n) {
480 481 482 483 484 485 486
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d\n",pgrm.code_size,instruction_name (i),(int)n);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(1, n);
}

487 488 489 490 491 492 493 494 495
void add_instruction_c_label(int16_t i,char n,char *label_name) {
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s\n",pgrm.code_size,instruction_name (i),(int)n,label_name);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(1, n);
	store_code_label_value(label_name,0);
}

496
void add_instruction_w(int16_t i,int32_t n) {
497 498 499
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d\n",pgrm.code_size,instruction_name (i),(int)n);

500 501 502 503
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n);
}

504
void add_instruction_w_i(int16_t i,int32_t n1,int64_t n2) {
505
	if (list_code || i>max_implemented_instruction_n)
506
		printf("%d\t%s %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2);
507

508
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
509
	store_code_elem(2, n1);
510
	store_code_elem(8, n2);
511 512
}

513
void add_instruction_w_label(int16_t i,int32_t n,char *label_name) {
514 515 516
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s\n",pgrm.code_size,instruction_name (i),(int)n,label_name);

517 518 519 520 521
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n);
	store_code_label_value(label_name,0);
}

522
void add_instruction_w_label_offset(int16_t i,int32_t n,char *label_name,uint32_t offset) {
523 524 525
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s%+d\n",pgrm.code_size,instruction_name (i),(int)n,label_name,offset);

526
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
527
	store_code_elem(2, n);
528 529 530
	store_code_label_value(label_name,offset);
}

531
void add_instruction_w_label_offset_label(int16_t i,int32_t n,char *label_name1,uint32_t offset,char *label_name2) {
532 533 534
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s%+d %s\n",pgrm.code_size,instruction_name (i),(int)n,label_name1,offset,label_name2);

535
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
536
	store_code_elem(2, n);
537 538 539 540
	store_code_label_value(label_name1,offset);
	store_code_label_value(label_name2,0);
}

541
void add_instruction_w_label_w(int16_t i,int32_t n1,char *label_name,int32_t n2) {
542 543 544
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s %d\n",pgrm.code_size,instruction_name (i),(int)n1,label_name,(int)n2);

545
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
546
	store_code_elem(2, n1);
547
	store_code_label_value(label_name,0);
548
	store_code_elem(2, n2);
549 550 551
}

void add_instruction_w_label_offset_label_label_offset_label
552
	(int16_t i,int32_t n1,char *label_name1,uint32_t offset1,char *label_name2,char *label_name3,uint32_t offset2,char *label_name4) {
553 554 555 556
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s%+d %s %s%+d %s\n",pgrm.code_size,instruction_name (i),n1,label_name1,offset1,label_name2,
																				  label_name3,offset2,label_name4);

557
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
558
	store_code_elem(2, n1);
559 560 561 562 563 564
	store_code_label_value(label_name1,offset1);
	store_code_label_value(label_name2,0);
	store_code_label_value(label_name3,offset2);
	store_code_label_value(label_name4,0);
}

565
void add_instruction_w_label_offset_w(int16_t i,int32_t n1,char *label_name,uint32_t offset,int32_t n2) {
566 567 568
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s+%d %d\n",pgrm.code_size,instruction_name (i),(int)n1,label_name,offset,(int)n2);

569
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
570
	store_code_elem(2, n1);
571
	store_code_label_value(label_name,offset);
572
	store_code_elem(2, n2);
573 574
}

575
void add_instruction_w_label_offset_w_w(int16_t i,int32_t n1,char *label_name,uint32_t offset,int32_t n2,int32_t n3) {
576 577 578
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s+%d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,label_name,offset,(int)n2,(int)n3);

579
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
580
	store_code_elem(2, n1);
581
	store_code_label_value(label_name,offset);
582 583
	store_code_elem(2, n2);
	store_code_elem(2, n3);
584 585
}

586
void add_instruction_w_label_offset_w_w_w(int16_t i,int32_t n1,char *label_name,uint32_t offset,int32_t n2,int32_t n3,int32_t n4) {
587 588 589
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s+%d %d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,label_name,offset,(int)n2,(int)n3,(int)n4);

590
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
591
	store_code_elem(2, n1);
592
	store_code_label_value(label_name,offset);
593 594 595
	store_code_elem(2, n2);
	store_code_elem(2, n3);
	store_code_elem(2, n4);
596 597
}

598
void add_instruction_w_c(int16_t i,int32_t n1,char n2) {
599 600 601 602 603 604 605 606
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n1);
	store_code_elem(1, n2);
}

607
void add_instruction_w_w(int16_t i,int32_t n1,int32_t n2) {
608 609
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2);
Camil Staps's avatar
Camil Staps committed
610

611
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
612 613
	store_code_elem(2, n1);
	store_code_elem(2, n2);
614 615
}

616
void add_instruction_i_w(int16_t i,int64_t n1,int32_t n2) {
617 618
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2);
Camil Staps's avatar
Camil Staps committed
619

620 621 622 623 624
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(8, n1);
	store_code_elem(2, n2);
}

625 626 627 628 629 630 631 632 633
void add_instruction_i_label(int16_t i,int64_t n,char *label_name) {
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %s\n",pgrm.code_size,instruction_name (i),(int)n,label_name);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(8, n);
	store_code_label_value(label_name,0);
}

634
void add_instruction_w_c_label(int16_t i,int32_t n1,char n2,char *label_name) {
635 636 637 638 639 640 641 642 643
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n1);
	store_code_elem(1, n2);
	store_code_label_value(label_name,0);
}

644
void add_instruction_w_w_label(int16_t i,int32_t n1,int32_t n2,char *label_name) {
645 646
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name);
647 648 649 650

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n1);
	store_code_elem(2, n2);
651
	store_code_label_value(label_name,0);
652 653
}

654
void add_instruction_w_i_label(int16_t i,int32_t n1,int64_t n2,char *label_name) {
655 656 657
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name);

658
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
659
	store_code_elem(2, n1);
660
	store_code_elem(8, n2);
661 662 663
	store_code_label_value(label_name,0);
}

664
void add_instruction_w_w_label_offset(int32_t i,int32_t n1,int32_t n2,char *label_name,uint32_t offset) {
665 666 667
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s%+d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name,offset);

668
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
669 670
	store_code_elem(2, n1);
	store_code_elem(2, n2);
671 672 673
	store_code_label_value(label_name,offset);
}

674
void add_instruction_w_w_label_w(int16_t i,int32_t n1,int32_t n2,char *label_name,int32_t n3) {
675 676 677
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name,(int)n3);

678
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
679 680
	store_code_elem(2, n1);
	store_code_elem(2, n2);
681
	store_code_label_value(label_name,0);
682
	store_code_elem(2, n3);
683 684
}

685
void add_instruction_w_c_label_c_label(int16_t i,int32_t n1,char n2,char *label_name1,char n3,char *label_name2) {
686 687 688 689 690 691 692 693 694 695 696
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name1,(int)n3,label_name2);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n1);
	store_code_elem(1, n2);
	store_code_label_value(label_name1,0);
	store_code_elem(1, n3);
	store_code_label_value(label_name2,0);
}

697
void add_instruction_w_w_label_w_label(int16_t i,int32_t n1,int32_t n2,char *label_name1,int32_t n3,char *label_name2) {
698 699 700
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name1,(int)n3,label_name2);

701
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
702 703
	store_code_elem(2, n1);
	store_code_elem(2, n2);
704
	store_code_label_value(label_name1,0);
705
	store_code_elem(2, n3);
706 707 708
	store_code_label_value(label_name2,0);
}

709
void add_instruction_w_i_label_i_label(int16_t i,int32_t n1,int64_t n2,char *label_name1,int64_t n3,char *label_name2) {
710 711 712 713 714 715 716 717 718 719 720
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name1,(int)n3,label_name2);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n1);
	store_code_elem(8, n2);
	store_code_label_value(label_name1,0);
	store_code_elem(8, n3);
	store_code_label_value(label_name2,0);
}

721
void add_instruction_w_w_label_offset_w(int16_t i,int32_t n1,int32_t n2,char *label_name,uint32_t offset,int32_t n3) {
722 723 724
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s+%d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name,offset,(int)n3);

725
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
726 727
	store_code_elem(2, n1);
	store_code_elem(2, n2);
728
	store_code_label_value(label_name,offset);
729
	store_code_elem(2, n3);
730 731
}

732
void add_instruction_w_w_label_w_w(int16_t i,int32_t n1,int32_t n2,char *label_name,int32_t n3,int32_t n4) {
733 734 735
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name,(int)n3,(int)n4);

736
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
737 738
	store_code_elem(2, n1);
	store_code_elem(2, n2);
739
	store_code_label_value(label_name,0);
740 741
	store_code_elem(2, n3);
	store_code_elem(2, n4);
742 743
}

744
void add_instruction_w_w_label_offset_w_w(int16_t i,int32_t n1,int32_t n2,char *label_name,uint32_t offset,int32_t n3,int32_t n4) {
745 746 747
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s+%d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,label_name,offset,(int)n3,(int)n4);

748
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
749 750
	store_code_elem(2, n1);
	store_code_elem(2, n2);
751
	store_code_label_value(label_name,offset);
752 753
	store_code_elem(2, n3);
	store_code_elem(2, n4);
754 755
}

756
void add_instruction_w_w_w(int16_t i,int32_t n1,int32_t n2,int32_t n3) {
757 758 759
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3);

760
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
761 762 763
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
764 765
}

766
void add_instruction_w_w_w_label(int16_t i,int32_t n1,int32_t n2,int32_t n3,char *label_name) {
767 768 769
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %s\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,label_name);

770
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
771 772 773
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
774 775 776
	store_code_label_value(label_name,0);
}

777
void add_instruction_w_w_w_label_offset(int16_t i,int32_t n1,int32_t n2,int32_t n3,char *label_name,uint32_t offset) {
778 779 780
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %s+%d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,label_name,offset);

781
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
782 783 784
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
785 786 787
	store_code_label_value(label_name,offset);
}

788
void add_instruction_w_w_w_label_w(int16_t i,int32_t n1,int32_t n2,int32_t n3,char *label_name,int32_t n4) {
789 790 791
	if (list_code || i>max_implemented_instruction_n)
		printf("\t%s %d %d %d %s %d\n",instruction_name (i),(int)n1,(int)n2,(int)n3,label_name,(int)n4);

792
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
793 794 795
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
796
	store_code_label_value(label_name,0);
797
	store_code_elem(2, n4);
798 799
}

800
void add_instruction_w_w_w_label_offset_w(int16_t i,int32_t n1,int32_t n2,int32_t n3,char *label_name,uint32_t offset,int32_t n4) {
801 802 803
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %s+%d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,label_name,offset,(int)n4);

804
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
805 806 807
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
808
	store_code_label_value(label_name,offset);
809
	store_code_elem(2, n4);
810 811
}

812
void add_instruction_w_w_w_label_w_w(int16_t i,int32_t n1,int32_t n2,int32_t n3,char *label_name,int32_t n4,int32_t n5) {
813 814 815
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %s %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,label_name,(int)n4,(int)n5);

816
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
817 818 819
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
820
	store_code_label_value(label_name,0);
821 822
	store_code_elem(2, n4);
	store_code_elem(2, n5);
823 824
}

825
void add_instruction_w_w_w_label_offset_w_w(int16_t i,int32_t n1,int32_t n2,int32_t n3,char *label_name,uint32_t offset,int32_t n4,int32_t n5) {
826 827 828
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %s+%d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,label_name,offset,(int)n4,(int)n5);

829
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
830 831 832
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
833
	store_code_label_value(label_name,offset);
834 835
	store_code_elem(2, n4);
	store_code_elem(2, n5);
836 837
}

838
void add_instruction_w_w_w_w(int16_t i,int32_t n1,int32_t n2,int32_t n3,int32_t n4) {
839 840 841
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,(int)n4);

842
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
843 844 845 846
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
	store_code_elem(2, n4);
847 848
}

849
void add_instruction_w_w_w_w_w(int16_t i,int32_t n1,int32_t n2,int32_t n3,int32_t n4,int32_t n5) {
850 851 852
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %d %d %d\n",pgrm.code_size,instruction_name (i),(int)n1,(int)n2,(int)n3,(int)n4,(int)n5);

853
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
854 855 856 857 858
	store_code_elem(2, n1);
	store_code_elem(2, n2);
	store_code_elem(2, n3);
	store_code_elem(2, n4);
	store_code_elem(2, n5);
859 860
}

861
void add_instruction_i(int16_t i,int64_t n) {
862
	if (list_code || i>max_implemented_instruction_n)
863
		printf("%d\t%s %d\n",pgrm.code_size,instruction_name (i),(int)n);
864

865
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
866
	store_code_elem(8, n);
867 868
}

869
void add_instruction_r(int16_t i,double r) {
870 871 872 873 874 875 876
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %f\n",pgrm.code_size,instruction_name (i),r);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(8, *((uint64_t*)(&r)));
}

877 878 879 880 881 882 883 884 885
void add_instruction_w_r(int16_t i,int32_t n,double r) {
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %f\n",pgrm.code_size,instruction_name (i),r);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_elem(2, n);
	store_code_elem(8, *((uint64_t*)(&r)));
}

886
void add_instruction_internal_label(int16_t i,struct label *label) {
887 888 889
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d\n",pgrm.code_size,instruction_name (i),label->label_offset);

890
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
891 892 893
	store_code_internal_label_value(label,0);
}

894 895 896 897 898 899 900 901 902
void add_instruction_internal_label_internal_label(int16_t i,struct label *label1,struct label *label2) {
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d\n",pgrm.code_size,instruction_name (i),label1->label_offset,label2->label_offset);

	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
	store_code_internal_label_value(label1,0);
	store_code_internal_label_value(label2,0);
}

903
void add_instruction_w_internal_label_label(int16_t i,int32_t n1,struct label *label,char *label_name) {
904 905 906
	if (list_code || i>max_implemented_instruction_n)
		printf("%d\t%s %d %d %s\n",pgrm.code_size,instruction_name (i),n1,label->label_offset,label_name);

907
	store_code_elem(BYTEWIDTH_INSTRUCTION, i);
908
	store_code_elem(2, n1);
909 910 911 912
	store_code_internal_label_value(label,0);
	store_code_label_value(label_name,0);
}

913 914 915 916 917
struct word *add_add_arg_labels(void) {
	int i;

	for(i=0; i<N_ADD_ARG_LABELS; ++i)
		if (Fadd_arg_label_used[i]) {
918 919 920 921 922 923 924 925 926 927 928
			if (i>0) {
				/* Three instructions above add_arg is the code used for fast
				 * applies of constructors. build_node returns; notB is not used. */
				if (i==1) {
					add_instruction(Cbuild_node2_rtn);
					add_instruction(CnotB);
				} else {
					add_instruction_w(Cbuild_node_rtn,i-1);
				}
				add_instruction(CnotB);
			}
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
			char label_name[11];
			sprintf(label_name,"_add_arg%d",i);
			struct label *label = enter_label(label_name);
			if (label->label_module_n != -1) {
				make_label_global(label);
				label->label_offset = pgrm.code_size<<2;
			}
			add_instruction(Cadd_arg0+i);
		}

	if (general_add_arg_label_used) {
		fprintf(stderr,"Warning: currying of functions with more than 32 arguments is not implemented.\n");

		struct label *label=enter_label("_add_arg");
		if (label->label_module_n!=-1) {
			make_label_global(label);
			label->label_offset=pgrm.code_size<<2;
		}
		add_instruction(Cadd_arg);
	}

	return pgrm.code;
}

953 954 955 956
struct specialized_jsr {
	const char *label;
	int instruction;
	int flags;
957
	int warned_flags;
958 959
};

960
#define SPECIALIZED(instr,flags) {#instr, C ## instr, flags, 0},
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
#define S_UNSUPPORTED 1
#define S_IO 2

static struct specialized_jsr specialized_jsr_labels[]={
	SPECIALIZED(eqAC,0)
	SPECIALIZED(cmpAC,0)
	SPECIALIZED(catAC,0)
	SPECIALIZED(sliceAC,0)
	SPECIALIZED(updateAC,0)
	SPECIALIZED(ItoAC,0)
	SPECIALIZED(BtoAC,0)
	SPECIALIZED(RtoAC,0)
	{"print__string__",Cprint_string,0},

	SPECIALIZED(closeF,       S_IO)
	SPECIALIZED(endF,         S_IO)
	SPECIALIZED(endSF,        S_IO | S_UNSUPPORTED)
	SPECIALIZED(errorF,       S_IO)
979
	SPECIALIZED(flushF,       S_IO)
980 981
	SPECIALIZED(openF,        S_IO)
	SPECIALIZED(openSF,       S_IO | S_UNSUPPORTED)
982
	SPECIALIZED(positionF,    S_IO)
983 984
	SPECIALIZED(positionSF,   S_IO | S_UNSUPPORTED)
	SPECIALIZED(readFC,       S_IO)
985 986 987
	SPECIALIZED(readFI,       S_IO)
	SPECIALIZED(readFR,       S_IO)
	SPECIALIZED(readFS,       S_IO)
988 989 990 991 992 993 994 995
	SPECIALIZED(readFString,  S_IO | S_UNSUPPORTED)
	SPECIALIZED(readLineF,    S_IO)
	SPECIALIZED(readLineSF,   S_IO | S_UNSUPPORTED)
	SPECIALIZED(readSFC,      S_IO | S_UNSUPPORTED)
	SPECIALIZED(readSFI,      S_IO | S_UNSUPPORTED)
	SPECIALIZED(readSFR,      S_IO | S_UNSUPPORTED)
	SPECIALIZED(readSFS,      S_IO | S_UNSUPPORTED)
	SPECIALIZED(reopenF,      S_IO | S_UNSUPPORTED)
996
	SPECIALIZED(seekF,        S_IO)
997 998
	SPECIALIZED(seekSF,       S_IO | S_UNSUPPORTED)
	SPECIALIZED(shareF,       S_IO | S_UNSUPPORTED)
999
	SPECIALIZED(stderrF,      S_IO)
1000
	SPECIALIZED(stdioF,       S_IO)
1001
	SPECIALIZED(writeFC,      S_IO)
1002 1003
	SPECIALIZED(writeFI,      S_IO)
	SPECIALIZED(writeFR,      S_IO)
1004
	SPECIALIZED(writeFS,      S_IO)
1005
	SPECIALIZED(writeFString, S_IO)
1006 1007 1008 1009 1010
};

static int get_specialized_jsr_label_n(char label_name[]) {
	int i,n;

1011
	n=sizeof(specialized_jsr_labels)/sizeof(struct specialized_jsr);
1012
	for(i=0; i<n; ++i)
1013
		if (!strcmp(label_name,specialized_jsr_labels[i].label))
1014 1015 1016 1017 1018 1019
			return i;

	return -1;
}

void add_specialized_jsr_instruction(unsigned int n) {
1020 1021 1022
	if (n>=sizeof(specialized_jsr_labels)/sizeof(struct specialized_jsr)) {
		fprintf(stderr,"internal error in add_specialized_jsr_instruction: %d\n",n);
		exit(1);
1023
	}
1024 1025 1026 1027

	struct specialized_jsr *entry=&specialized_jsr_labels[n];
	if (entry->flags & S_UNSUPPORTED)
		unsupported_instruction_warning(entry->instruction);
1028 1029 1030 1031 1032 1033
	else if (entry->flags & S_IO) {
		if (!(entry->warned_flags & S_IO)) {
			fprintf(stderr,"Warning: jsr %s requires file IO\n",entry->label);
			entry->warned_flags|=S_IO;
		}
	}
1034 1035

	add_instruction(entry->instruction);
1036 1037
}

1038 1039 1040 1041 1042 1043 1044
void add_label(char *label_name) {
	if (list_code)
		printf("%d\t.label %s\n",pgrm.code_size,label_name);

	store_code_label_value(label_name,0);
}

1045 1046 1047 1048 1049 1050 1051 1052
void code_absR(void) {
	add_instruction(CabsR);
}

void code_acosR(void) {
	add_instruction(CacosR);
}

1053 1054 1055 1056
void code_addI(void) {
	add_instruction(CaddI);
}

1057 1058 1059 1060 1061 1062 1063 1064
void code_addIo(void) {
	add_instruction(CaddIo);
}

void code_addLU(void) {
	add_instruction(CaddLU);
}

1065 1066 1067 1068
void code_addR(void) {
	add_instruction(CaddR);
}

1069 1070 1071 1072
void code_and(void) {
	add_instruction(CandI);
}

1073 1074 1075 1076 1077 1078 1079 1080
void code_asinR(void) {
	add_instruction(CasinR);
}

void code_atanR(void) {
	add_instruction(CatanR);
}

1081 1082 1083 1084 1085 1086 1087 1088 1089
void code_buildh(char descriptor_name[],int arity);

void code_build(char descriptor_name[],int arity,char *code_name) {
	if (code_name[0]=='_' && code_name[1]=='_' && code_name[2]=='h' && code_name[3]=='n' && code_name[4]=='f' && code_name[5]=='\0') {
		code_buildh(descriptor_name,arity);
		return;
	}
/*
	if (! (descriptor_name[0]=='_' && descriptor_name[1]=='_' && descriptor_name[2]=='\0')) {
1090
		fprintf(stderr, "Error: build %s %d %s\n",descriptor_name,arity,code_name);
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
		exit(1);
	}
*/
	switch(arity) {
		case 0:
			add_instruction_label(Cbuild0,code_name);
			break;
		case 1:
			add_instruction_label(Cbuild1,code_name);
			break;
		case 2:
			add_instruction_label(Cbuild2,code_name);
			break;
		case 3:
			add_instruction_label(Cbuild3,code_name);
			break;
		case 4:
			add_instruction_label(Cbuild4,code_name);
			break;
		default:
			if (arity<1)
				add_instruction_label(Cbuild1,code_name);
			else
				add_instruction_w_label(Cbuild,arity,code_name);
	}
}

void code_buildh(char descriptor_name[],int arity) {
	switch(arity) {
		case 0:
			add_instruction_label_offset(Cbuildh0,descriptor_name,-4);
			return;
		case 1:
			add_instruction_label_offset(Cbuildh1,descriptor_name,(1<<3)+2);
			return;
		case 2:
			add_instruction_label_offset(Cbuildh2,descriptor_name,(2<<3)+2);
			return;
		case 3:
			add_instruction_label_offset(Cbuildh3,descriptor_name,(3<<3)+2);
			return;
		case 4:
			add_instruction_label_offset(Cbuildh4,descriptor_name,(4<<3)+2);
			return;
		default:
			add_instruction_w_label_offset(Cbuildh,arity+2,descriptor_name,(arity<<3)+2);
			return;
	}
}

void code_buildB(int value) {
	if (value==0)
		add_instruction(CbuildBFALSE);
	else
		add_instruction(CbuildBTRUE);
}
Camil Staps's avatar
Camil Staps committed
1147
#endif
1148 1149 1150 1151

void print_string_directive(char *string,int string_length) {
	int i;

Camil Staps's avatar
Camil Staps committed
1152
	PRINTF("%d\t.string \"",pgrm.data_size<<2);
1153 1154
	for(i=0; i<string_length; ++i)
		putchar(string[i]);
Camil Staps's avatar
Camil Staps committed
1155
	PRINTF("\"\n");
1156 1157
}

1158 1159
void add_words_in_strings(uint32_t val) {
	pgrm.words_in_strings += val;
1160 1161 1162
}

void add_string_information(uint32_t data_offset) {
1163
	if (pgrm.strings_size >= allocated_strings_size)
1164 1165
		realloc_strings();

1166 1167 1168
	pgrm.strings[pgrm.strings_size++] = data_offset;
}

1169
void store_string(char *string,int string_length,int include_terminator) {
1170
	add_string_information(pgrm.data_size);
1171 1172

	if (list_code)
Camil Staps's avatar
Camil Staps committed
1173
		PRINTF("%d\t.data4 %d\n",pgrm.data_size<<2,string_length);
1174
	store_data_l(string_length+include_terminator);
1175 1176 1177

	if (list_code)
		print_string_directive(string,string_length);
1178

1179 1180 1181
	int i=0;
	while(i+8<=string_length) {
		store_data_l(
1182 1183 1184 1185 1186 1187 1188 1189
				(unsigned char)string[i] |
				((uint64_t)(unsigned char)string[i+1]<<8) |
				((uint64_t)(unsigned char)string[i+2]<<16) |
				((uint64_t)(unsigned char)string[i+3]<<24) |
				((uint64_t)(unsigned char)string[i+4]<<32) |
				((uint64_t)(unsigned char)string[i+5]<<40) |
				((uint64_t)(unsigned char)string[i+6]<<48) |
				((uint64_t)(unsigned char)string[i+7]<<56));
1190
		i+=8;
1191 1192
	}
	if (i!=string_length) {
1193 1194
		uint64_t n = 0;
		int s = 0;
1195 1196

		while(i<string_length) {
1197
			n |= (uint64_t)(unsigned char)string[i]<<s;
1198