1 /* Small compiler - Binary code generation (the "assembler")
3 * Copyright (c) ITB CompuPhase, 1997-2003
5 * This software is provided "as-is", without any express or implied warranty.
6 * In no event will the authors be held liable for any damages arising from
7 * the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software in
15 * a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
21 * Version: $Id: embryo_cc_sc6.c 51023 2010-08-11 20:35:26Z lucas $
31 #include <stdlib.h> /* for macro max() */
34 #include "embryo_cc_sc.h"
36 typedef cell(*OPCODE_PROC) (FILE * fbin, char *params, cell opcode);
42 int segment; /* sIN_CSEG=parse in cseg, sIN_DSEG=parse in dseg */
46 static cell codeindex; /* similar to "code_idx" */
47 static cell *lbltab; /* label table */
48 static int writeerror;
49 static int bytes_in, bytes_out;
51 /* apparently, strtol() does not work correctly on very large (unsigned)
52 * hexadecimal values */
54 hex2long(char *s, char **n)
56 unsigned long result = 0L;
60 /* ignore leading whitespace */
61 while (*s == ' ' || *s == '\t')
64 /* allow a negation sign to create the two's complement of numbers */
71 assert((*s >= '0' && *s <= '9') || (*s >= 'a' && *s <= 'f')
72 || (*s >= 'a' && *s <= 'f'));
75 if (*s >= '0' && *s <= '9')
77 else if (*s >= 'a' && *s <= 'f')
78 digit = *s - 'a' + 10;
79 else if (*s >= 'A' && *s <= 'F')
80 digit = *s - 'A' + 10;
82 break; /* probably whitespace */
83 result = (result << 4) | digit;
89 result = (~result) + 1; /* take two's complement of the result */
90 return (ucell) result;
93 #ifdef WORDS_BIGENDIAN
97 unsigned char *s = (unsigned char *)v;
110 unsigned char *s = (unsigned char *)v;
113 /* swap outer two bytes */
117 /* swap inner two bytes */
124 #define aligncell(v) align16(v)
126 #define aligncell(v) align32(v)
129 #define align16(v) (v)
130 #define align32(v) (v)
131 #define aligncell(v) (v)
135 skipwhitespace(char *str)
137 while (isspace(*str))
143 stripcomment(char *str)
145 char *ptr = strchr(str, ';');
149 *ptr++ = '\n'; /* terminate the line, but leave the '\n' */
156 write_encoded(FILE * fbin, ucell * c, int num)
158 assert(sizeof(cell) <= 4); /* code must be adjusted for larger cells */
159 assert(fbin != NULL);
164 ucell p = (ucell) * c;
165 unsigned char t[5]; /* a 32-bit cell is encoded in max. 5 bytes (3 bytes for a 16-bit cell) */
169 for (index = 0; index < 5; index++)
171 t[index] = (unsigned char)(p & 0x7f); /* store 7 bits */
174 /* skip leading zeros */
175 while (index > 1 && t[index - 1] == 0
176 && (t[index - 2] & 0x40) == 0)
178 /* skip leading -1s *//* ??? for BIT16, check for index==3 && t[index-1]==0x03 */
179 if (index == 5 && t[index - 1] == 0x0f
180 && (t[index - 2] & 0x40) != 0)
182 while (index > 1 && t[index - 1] == 0x7f
183 && (t[index - 2] & 0x40) != 0)
185 /* write high byte first, write continuation bits */
190 (unsigned char)((index == 0) ? t[index]
191 : (t[index] | 0x80));
192 writeerror |= !sc_writebin(fbin, &code, 1);
195 bytes_in += sizeof *c;
196 assert(AMX_EXPANDMARGIN > 2);
197 if (bytes_out - bytes_in >= AMX_EXPANDMARGIN - 2)
198 error(106); /* compression buffer overflow */
202 assert((sc_lengthbin(fbin) % sizeof(cell)) == 0);
203 writeerror |= !sc_writebin(fbin, aligncell(c), sizeof *c);
209 #if defined __BORLANDC__ || defined __WATCOMC__
214 noop(FILE * fbin __UNUSED__, char *params __UNUSED__, cell opcode __UNUSED__)
219 #if defined __BORLANDC__ || defined __WATCOMC__
224 parm0(FILE * fbin, char *params __UNUSED__, cell opcode)
227 write_encoded(fbin, (ucell *) & opcode, 1);
232 parm1(FILE * fbin, char *params, cell opcode)
234 ucell p = hex2long(params, NULL);
238 write_encoded(fbin, (ucell *) & opcode, 1);
239 write_encoded(fbin, &p, 1);
241 return opcodes(1) + opargs(1);
245 parm2(FILE * fbin, char *params, cell opcode)
249 p[0] = hex2long(params, ¶ms);
250 p[1] = hex2long(params, NULL);
253 write_encoded(fbin, (ucell *) & opcode, 1);
254 write_encoded(fbin, p, 2);
256 return opcodes(1) + opargs(2);
259 #if defined __BORLANDC__ || defined __WATCOMC__
264 do_dump(FILE * fbin, char *params, cell opcode __UNUSED__)
269 while (*params != '\0')
271 p = hex2long(params, ¶ms);
273 write_encoded(fbin, &p, 1);
275 while (isspace(*params))
278 return num * sizeof(cell);
282 do_call(FILE * fbin, char *params, cell opcode)
284 char name[sNAMEMAX + 1];
289 for (i = 0; !isspace(*params); i++, params++)
291 assert(*params != '\0');
292 assert(i < sNAMEMAX);
297 /* look up the function address; note that the correct file number must
298 * already have been set (in order for static globals to be found).
302 assert(sym->ident == iFUNCTN || sym->ident == iREFFUNC);
303 assert(sym->vclass == sGLOBAL);
308 write_encoded(fbin, (ucell *) & opcode, 1);
309 write_encoded(fbin, &p, 1);
311 return opcodes(1) + opargs(1);
315 do_jump(FILE * fbin, char *params, cell opcode)
320 i = (int)hex2long(params, NULL);
321 assert(i >= 0 && i < labnum);
325 assert(lbltab != NULL);
327 write_encoded(fbin, (ucell *) & opcode, 1);
328 write_encoded(fbin, &p, 1);
330 return opcodes(1) + opargs(1);
334 do_file(FILE * fbin, char *params, cell opcode)
339 p = hex2long(params, ¶ms);
341 /* remove leading and trailing white space from the filename */
342 while (isspace(*params))
344 len = strlen(params);
345 while (len > 0 && isspace(params[len - 1]))
347 params[len++] = '\0'; /* zero-terminate */
348 while (len % sizeof(cell) != 0)
349 params[len++] = '\0'; /* pad with zeros up to full cell */
350 assert(len > 0 && len < 256);
351 clen = len + sizeof(cell); /* add size of file ordinal */
355 write_encoded(fbin, (ucell *) & opcode, 1);
356 write_encoded(fbin, &clen, 1);
357 write_encoded(fbin, &p, 1);
358 write_encoded(fbin, (ucell *) params, len / sizeof(cell));
360 return opcodes(1) + opargs(1) + clen; /* other argument is in clen */
364 do_symbol(FILE * fbin, char *params, cell opcode)
367 ucell offset, clen, flags;
369 unsigned char mclass, type;
371 for (endptr = params; !isspace(*endptr) && endptr != '\0'; endptr++)
373 assert(*endptr == ' ');
375 len = (int)(endptr - params);
376 assert(len > 0 && len < sNAMEMAX);
377 /* first get the other parameters from the line */
378 offset = hex2long(endptr, &endptr);
379 mclass = (unsigned char)hex2long(endptr, &endptr);
380 type = (unsigned char)hex2long(endptr, NULL);
381 flags = type + 256 * mclass;
382 /* now finish up the name (overwriting the input line) */
383 params[len++] = '\0'; /* zero-terminate */
384 while (len % sizeof(cell) != 0)
385 params[len++] = '\0'; /* pad with zeros up to full cell */
386 clen = len + 2 * sizeof(cell); /* add size of symbol address and flags */
390 write_encoded(fbin, (ucell *) & opcode, 1);
391 write_encoded(fbin, &clen, 1);
392 write_encoded(fbin, &offset, 1);
393 write_encoded(fbin, &flags, 1);
394 write_encoded(fbin, (ucell *) params, len / sizeof(cell));
398 /* function should start right after the symbolic information */
399 if (!fbin && mclass == 0 && type == iFUNCTN)
400 assert(offset == codeindex + opcodes(1) + opargs(1) + clen);
403 return opcodes(1) + opargs(1) + clen; /* other 2 arguments are in clen */
407 do_switch(FILE * fbin, char *params, cell opcode)
412 i = (int)hex2long(params, NULL);
413 assert(i >= 0 && i < labnum);
417 assert(lbltab != NULL);
419 write_encoded(fbin, (ucell *) & opcode, 1);
420 write_encoded(fbin, &p, 1);
422 return opcodes(1) + opargs(1);
425 #if defined __BORLANDC__ || defined __WATCOMC__
430 do_case(FILE * fbin, char *params, cell opcode __UNUSED__)
435 v = hex2long(params, ¶ms);
436 i = (int)hex2long(params, NULL);
437 assert(i >= 0 && i < labnum);
441 assert(lbltab != NULL);
443 write_encoded(fbin, &v, 1);
444 write_encoded(fbin, &p, 1);
446 return opcodes(0) + opargs(2);
449 #if defined __BORLANDC__ || defined __WATCOMC__
454 curfile(FILE * fbin __UNUSED__, char *params, cell opcode __UNUSED__)
456 fcurrent = (int)hex2long(params, NULL);
460 static OPCODE opcodelist[] = {
461 /* node for "invalid instruction" */
463 /* opcodes in sorted order */
464 {78, "add", sIN_CSEG, parm0},
465 {87, "add.c", sIN_CSEG, parm1},
466 {14, "addr.alt", sIN_CSEG, parm1},
467 {13, "addr.pri", sIN_CSEG, parm1},
468 {30, "align.alt", sIN_CSEG, parm1},
469 {29, "align.pri", sIN_CSEG, parm1},
470 {81, "and", sIN_CSEG, parm0},
471 {121, "bounds", sIN_CSEG, parm1},
472 {49, "call", sIN_CSEG, do_call},
473 {50, "call.pri", sIN_CSEG, parm0},
474 {0, "case", sIN_CSEG, do_case},
475 {130, "casetbl", sIN_CSEG, parm0}, /* version 1 */
476 {118, "cmps", sIN_CSEG, parm1},
477 {0, "code", 0, noop},
478 {12, "const.alt", sIN_CSEG, parm1},
479 {11, "const.pri", sIN_CSEG, parm1},
480 {0, "curfile", sIN_CSEG, curfile},
481 {0, "data", 0, noop},
482 {114, "dec", sIN_CSEG, parm1},
483 {113, "dec.alt", sIN_CSEG, parm0},
484 {116, "dec.i", sIN_CSEG, parm0},
485 {112, "dec.pri", sIN_CSEG, parm0},
486 {115, "dec.s", sIN_CSEG, parm1},
487 {0, "dump", sIN_DSEG, do_dump},
488 {95, "eq", sIN_CSEG, parm0},
489 {106, "eq.c.alt", sIN_CSEG, parm1},
490 {105, "eq.c.pri", sIN_CSEG, parm1},
491 {124, "file", sIN_CSEG, do_file},
492 {119, "fill", sIN_CSEG, parm1},
493 {100, "geq", sIN_CSEG, parm0},
494 {99, "grtr", sIN_CSEG, parm0},
495 {120, "halt", sIN_CSEG, parm1},
496 {45, "heap", sIN_CSEG, parm1},
497 {27, "idxaddr", sIN_CSEG, parm0},
498 {28, "idxaddr.b", sIN_CSEG, parm1},
499 {109, "inc", sIN_CSEG, parm1},
500 {108, "inc.alt", sIN_CSEG, parm0},
501 {111, "inc.i", sIN_CSEG, parm0},
502 {107, "inc.pri", sIN_CSEG, parm0},
503 {110, "inc.s", sIN_CSEG, parm1},
504 {86, "invert", sIN_CSEG, parm0},
505 {55, "jeq", sIN_CSEG, do_jump},
506 {60, "jgeq", sIN_CSEG, do_jump},
507 {59, "jgrtr", sIN_CSEG, do_jump},
508 {58, "jleq", sIN_CSEG, do_jump},
509 {57, "jless", sIN_CSEG, do_jump},
510 {56, "jneq", sIN_CSEG, do_jump},
511 {54, "jnz", sIN_CSEG, do_jump},
512 {52, "jrel", sIN_CSEG, parm1}, /* always a number */
513 {64, "jsgeq", sIN_CSEG, do_jump},
514 {63, "jsgrtr", sIN_CSEG, do_jump},
515 {62, "jsleq", sIN_CSEG, do_jump},
516 {61, "jsless", sIN_CSEG, do_jump},
517 {51, "jump", sIN_CSEG, do_jump},
518 {128, "jump.pri", sIN_CSEG, parm0}, /* version 1 */
519 {53, "jzer", sIN_CSEG, do_jump},
520 {31, "lctrl", sIN_CSEG, parm1},
521 {98, "leq", sIN_CSEG, parm0},
522 {97, "less", sIN_CSEG, parm0},
523 {25, "lidx", sIN_CSEG, parm0},
524 {26, "lidx.b", sIN_CSEG, parm1},
525 {125, "line", sIN_CSEG, parm2},
526 {2, "load.alt", sIN_CSEG, parm1},
527 {9, "load.i", sIN_CSEG, parm0},
528 {1, "load.pri", sIN_CSEG, parm1},
529 {4, "load.s.alt", sIN_CSEG, parm1},
530 {3, "load.s.pri", sIN_CSEG, parm1},
531 {10, "lodb.i", sIN_CSEG, parm1},
532 {6, "lref.alt", sIN_CSEG, parm1},
533 {5, "lref.pri", sIN_CSEG, parm1},
534 {8, "lref.s.alt", sIN_CSEG, parm1},
535 {7, "lref.s.pri", sIN_CSEG, parm1},
536 {34, "move.alt", sIN_CSEG, parm0},
537 {33, "move.pri", sIN_CSEG, parm0},
538 {117, "movs", sIN_CSEG, parm1},
539 {85, "neg", sIN_CSEG, parm0},
540 {96, "neq", sIN_CSEG, parm0},
541 {134, "nop", sIN_CSEG, parm0}, /* version 6 */
542 {84, "not", sIN_CSEG, parm0},
543 {82, "or", sIN_CSEG, parm0},
544 {43, "pop.alt", sIN_CSEG, parm0},
545 {42, "pop.pri", sIN_CSEG, parm0},
546 {46, "proc", sIN_CSEG, parm0},
547 {40, "push", sIN_CSEG, parm1},
548 {37, "push.alt", sIN_CSEG, parm0},
549 {39, "push.c", sIN_CSEG, parm1},
550 {36, "push.pri", sIN_CSEG, parm0},
551 {38, "push.r", sIN_CSEG, parm1},
552 {41, "push.s", sIN_CSEG, parm1},
553 {133, "pushaddr", sIN_CSEG, parm1}, /* version 4 */
554 {47, "ret", sIN_CSEG, parm0},
555 {48, "retn", sIN_CSEG, parm0},
556 {32, "sctrl", sIN_CSEG, parm1},
557 {73, "sdiv", sIN_CSEG, parm0},
558 {74, "sdiv.alt", sIN_CSEG, parm0},
559 {104, "sgeq", sIN_CSEG, parm0},
560 {103, "sgrtr", sIN_CSEG, parm0},
561 {65, "shl", sIN_CSEG, parm0},
562 {69, "shl.c.alt", sIN_CSEG, parm1},
563 {68, "shl.c.pri", sIN_CSEG, parm1},
564 {66, "shr", sIN_CSEG, parm0},
565 {71, "shr.c.alt", sIN_CSEG, parm1},
566 {70, "shr.c.pri", sIN_CSEG, parm1},
567 {94, "sign.alt", sIN_CSEG, parm0},
568 {93, "sign.pri", sIN_CSEG, parm0},
569 {102, "sleq", sIN_CSEG, parm0},
570 {101, "sless", sIN_CSEG, parm0},
571 {72, "smul", sIN_CSEG, parm0},
572 {88, "smul.c", sIN_CSEG, parm1},
573 {127, "srange", sIN_CSEG, parm2}, /* version 1 */
574 {20, "sref.alt", sIN_CSEG, parm1},
575 {19, "sref.pri", sIN_CSEG, parm1},
576 {22, "sref.s.alt", sIN_CSEG, parm1},
577 {21, "sref.s.pri", sIN_CSEG, parm1},
578 {67, "sshr", sIN_CSEG, parm0},
579 {44, "stack", sIN_CSEG, parm1},
580 {0, "stksize", 0, noop},
581 {16, "stor.alt", sIN_CSEG, parm1},
582 {23, "stor.i", sIN_CSEG, parm0},
583 {15, "stor.pri", sIN_CSEG, parm1},
584 {18, "stor.s.alt", sIN_CSEG, parm1},
585 {17, "stor.s.pri", sIN_CSEG, parm1},
586 {24, "strb.i", sIN_CSEG, parm1},
587 {79, "sub", sIN_CSEG, parm0},
588 {80, "sub.alt", sIN_CSEG, parm0},
589 {132, "swap.alt", sIN_CSEG, parm0}, /* version 4 */
590 {131, "swap.pri", sIN_CSEG, parm0}, /* version 4 */
591 {129, "switch", sIN_CSEG, do_switch}, /* version 1 */
592 {126, "symbol", sIN_CSEG, do_symbol},
593 {136, "symtag", sIN_CSEG, parm1}, /* version 7 */
594 {123, "sysreq.c", sIN_CSEG, parm1},
595 {135, "sysreq.d", sIN_CSEG, parm1}, /* version 7, not generated directly */
596 {122, "sysreq.pri", sIN_CSEG, parm0},
597 {76, "udiv", sIN_CSEG, parm0},
598 {77, "udiv.alt", sIN_CSEG, parm0},
599 {75, "umul", sIN_CSEG, parm0},
600 {35, "xchg", sIN_CSEG, parm0},
601 {83, "xor", sIN_CSEG, parm0},
602 {91, "zero", sIN_CSEG, parm1},
603 {90, "zero.alt", sIN_CSEG, parm0},
604 {89, "zero.pri", sIN_CSEG, parm0},
605 {92, "zero.s", sIN_CSEG, parm1},
608 #define MAX_INSTR_LEN 30
610 findopcode(char *instr, int maxlen)
612 int low, high, mid, cmp;
613 char str[MAX_INSTR_LEN];
615 if (maxlen >= MAX_INSTR_LEN)
617 strncpy(str, instr, maxlen);
618 str[maxlen] = '\0'; /* make sure the string is zero terminated */
619 /* look up the instruction with a binary search
620 * the assembler is case insensitive to instructions (but case sensitive
623 low = 1; /* entry 0 is reserved (for "not found") */
624 high = (sizeof opcodelist / sizeof opcodelist[0]) - 1;
627 mid = (low + high) / 2;
628 assert(opcodelist[mid].name != NULL);
629 cmp = strcasecmp(str, opcodelist[mid].name);
637 if (strcasecmp(str, opcodelist[low].name) == 0)
638 return low; /* found */
639 return 0; /* not found, return special index */
643 assemble(FILE * fout, FILE * fin)
645 typedef struct tagFUNCSTUB
647 unsigned int address, nameofs;
651 int numpublics, numnatives, numlibraries, numpubvars,
653 long nametablesize, nameofs;
654 char line[256], *instr, *params;
657 symbol *sym, **nativelist;
658 constvalue *constptr;
660 int nametable, tags, libraries, publics, natives, pubvars;
664 /* verify that the opcode list is sorted (skip entry 1; it is reserved
665 * for a non-existant opcode)
667 assert(opcodelist[1].name != NULL);
668 for (i = 2; i < (sizeof opcodelist / sizeof opcodelist[0]); i++)
670 assert(opcodelist[i].name != NULL);
671 assert(strcasecmp(opcodelist[i].name, opcodelist[i - 1].name) > 0);
676 nametablesize = sizeof(short);
681 /* count number of public and native functions and public variables */
682 for (sym = glbtab.next; sym; sym = sym->next)
684 char alias[sNAMEMAX + 1] = "";
687 if (sym->ident == iFUNCTN)
689 assert(strlen(sym->name) <= sNAMEMAX);
690 if ((sym->usage & uNATIVE) != 0 && (sym->usage & uREAD) != 0
693 match = ++numnatives;
694 if (!lookup_alias(alias, sym->name))
695 strcpy(alias, sym->name);
697 if ((sym->usage & uPUBLIC) != 0 && (sym->usage & uDEFINE) != 0)
699 match = ++numpublics;
700 strcpy(alias, sym->name);
702 if (strcmp(sym->name, uMAINFUNC) == 0)
704 assert(sym->vclass == sGLOBAL);
705 mainaddr = sym->addr;
708 else if (sym->ident == iVARIABLE)
710 if ((sym->usage & uPUBLIC) != 0)
712 match = ++numpubvars;
713 strcpy(alias, sym->name);
718 assert(alias[0] != '\0');
719 nametablesize += strlen(alias) + 1;
722 assert(numnatives == ntv_funcid);
724 /* count number of libraries */
726 for (constptr = libname_tab.next; constptr;
727 constptr = constptr->next)
729 if (constptr->value > 0)
731 assert(constptr->name[0] != '\0');
733 nametablesize += strlen(constptr->name) + 1;
737 /* count number of public tags */
739 for (constptr = tagname_tab.next; constptr;
740 constptr = constptr->next)
742 if ((constptr->value & PUBLICTAG) != 0)
744 assert(constptr->name[0] != '\0');
746 nametablesize += strlen(constptr->name) + 1;
750 /* pad the header to sc_dataalign
751 * => thereby the code segment is aligned
752 * => since the code segment is padded to a sc_dataalign boundary, the data segment is aligned
753 * => and thereby the stack top is aligned too
755 assert(sc_dataalign != 0);
756 padding = sc_dataalign - (sizeof hdr + nametablesize) % sc_dataalign;
757 if (padding == sc_dataalign)
760 /* write the abstract machine header */
761 memset(&hdr, 0, sizeof hdr);
762 hdr.magic = (unsigned short)0xF1E0;
763 hdr.file_version = CUR_FILE_VERSION;
764 hdr.amx_version = MIN_AMX_VERSION;
765 hdr.flags = (short)(sc_debug & sSYMBOLIC);
767 hdr.flags |= AMX_FLAG_CHAR16;
769 hdr.flags |= AMX_FLAG_COMPACT;
771 hdr.flags |= AMX_FLAG_NOCHECKS;
772 // #ifdef WORDS_BIGENDIAN
773 // hdr.flags|=AMX_FLAG_BIGENDIAN;
775 defsize = hdr.defsize = sizeof(FUNCSTUB);
776 assert((hdr.defsize % sizeof(cell)) == 0);
777 publics = hdr.publics = sizeof hdr; /* public table starts right after the header */
778 natives = hdr.natives = hdr.publics + numpublics * sizeof(FUNCSTUB);
779 libraries = hdr.libraries = hdr.natives + numnatives * sizeof(FUNCSTUB);
780 pubvars = hdr.pubvars = hdr.libraries + numlibraries * sizeof(FUNCSTUB);
781 tags = hdr.tags = hdr.pubvars + numpubvars * sizeof(FUNCSTUB);
782 nametable = hdr.nametable = hdr.tags + numtags * sizeof(FUNCSTUB);
783 cod = hdr.cod = hdr.nametable + nametablesize + padding;
784 hdr.dat = hdr.cod + code_idx;
785 hdr.hea = hdr.dat + glb_declared * sizeof(cell);
786 hdr.stp = hdr.hea + sc_stksize * sizeof(cell);
788 hdr.size = hdr.hea; /* preset, this is incorrect in case of compressed output */
789 #ifdef WORDS_BIGENDIAN
793 align16(&hdr.defsize);
799 align32(&hdr.publics);
800 align32(&hdr.natives);
801 align32(&hdr.libraries);
802 align32(&hdr.pubvars);
804 align32(&hdr.nametable);
806 sc_writebin(fout, &hdr, sizeof hdr);
808 /* dump zeros up to the rest of the header, so that we can easily "seek" */
809 for (nameofs = sizeof hdr; nameofs < cod; nameofs++)
811 nameofs = nametable + sizeof(short);
813 /* write the public functions table */
815 for (sym = glbtab.next; sym; sym = sym->next)
817 if (sym->ident == iFUNCTN
818 && (sym->usage & uPUBLIC) != 0 && (sym->usage & uDEFINE) != 0)
820 assert(sym->vclass == sGLOBAL);
821 func.address = sym->addr;
822 func.nameofs = nameofs;
823 #ifdef WORDS_BIGENDIAN
824 align32(&func.address);
825 align32(&func.nameofs);
827 fseek(fout, publics + count * sizeof(FUNCSTUB), SEEK_SET);
828 sc_writebin(fout, &func, sizeof func);
829 fseek(fout, nameofs, SEEK_SET);
830 sc_writebin(fout, sym->name, strlen(sym->name) + 1);
831 nameofs += strlen(sym->name) + 1;
836 /* write the natives table */
837 /* The native functions must be written in sorted order. (They are
838 * sorted on their "id", not on their name). A nested loop to find
839 * each successive function would be an O(n^2) operation. But we
840 * do not really need to sort, because the native function id's
841 * are sequential and there are no duplicates. So we first walk
842 * through the complete symbol list and store a pointer to every
843 * native function of interest in a temporary table, where its id
844 * serves as the index in the table. Now we can walk the table and
845 * have all native functions in sorted order.
849 nativelist = (symbol **) malloc(numnatives * sizeof(symbol *));
851 error(103); /* insufficient memory */
853 memset(nativelist, 0, numnatives * sizeof(symbol *)); /* for NULL checking */
855 for (sym = glbtab.next; sym; sym = sym->next)
857 if (sym->ident == iFUNCTN && (sym->usage & uNATIVE) != 0
858 && (sym->usage & uREAD) != 0 && sym->addr >= 0)
860 assert(sym->addr < numnatives);
861 nativelist[(int)sym->addr] = sym;
865 for (i = 0; i < numnatives; i++)
867 char alias[sNAMEMAX + 1];
871 if (!lookup_alias(alias, sym->name))
873 assert(strlen(sym->name) <= sNAMEMAX);
874 strcpy(alias, sym->name);
876 assert(sym->vclass == sGLOBAL);
878 func.nameofs = nameofs;
879 #ifdef WORDS_BIGENDIAN
880 align32(&func.address);
881 align32(&func.nameofs);
883 fseek(fout, natives + count * sizeof(FUNCSTUB), SEEK_SET);
884 sc_writebin(fout, &func, sizeof func);
885 fseek(fout, nameofs, SEEK_SET);
886 sc_writebin(fout, alias, strlen(alias) + 1);
887 nameofs += strlen(alias) + 1;
893 /* write the libraries table */
895 for (constptr = libname_tab.next; constptr;
896 constptr = constptr->next)
898 if (constptr->value > 0)
900 assert(constptr->name[0] != '\0');
902 func.nameofs = nameofs;
903 #ifdef WORDS_BIGENDIAN
904 align32(&func.address);
905 align32(&func.nameofs);
907 fseek(fout, libraries + count * sizeof(FUNCSTUB), SEEK_SET);
908 sc_writebin(fout, &func, sizeof func);
909 fseek(fout, nameofs, SEEK_SET);
910 sc_writebin(fout, constptr->name, strlen(constptr->name) + 1);
911 nameofs += strlen(constptr->name) + 1;
916 /* write the public variables table */
918 for (sym = glbtab.next; sym; sym = sym->next)
920 if (sym->ident == iVARIABLE && (sym->usage & uPUBLIC) != 0)
922 assert((sym->usage & uDEFINE) != 0);
923 assert(sym->vclass == sGLOBAL);
924 func.address = sym->addr;
925 func.nameofs = nameofs;
926 #ifdef WORDS_BIGENDIAN
927 align32(&func.address);
928 align32(&func.nameofs);
930 fseek(fout, pubvars + count * sizeof(FUNCSTUB), SEEK_SET);
931 sc_writebin(fout, &func, sizeof func);
932 fseek(fout, nameofs, SEEK_SET);
933 sc_writebin(fout, sym->name, strlen(sym->name) + 1);
934 nameofs += strlen(sym->name) + 1;
939 /* write the public tagnames table */
941 for (constptr = tagname_tab.next; constptr;
942 constptr = constptr->next)
944 if ((constptr->value & PUBLICTAG) != 0)
946 assert(constptr->name[0] != '\0');
947 func.address = constptr->value & TAGMASK;
948 func.nameofs = nameofs;
949 #ifdef WORDS_BIGENDIAN
950 align32(&func.address);
951 align32(&func.nameofs);
953 fseek(fout, tags + count * sizeof(FUNCSTUB), SEEK_SET);
954 sc_writebin(fout, &func, sizeof func);
955 fseek(fout, nameofs, SEEK_SET);
956 sc_writebin(fout, constptr->name, strlen(constptr->name) + 1);
957 nameofs += strlen(constptr->name) + 1;
962 /* write the "maximum name length" field in the name table */
963 assert(nameofs == nametable + nametablesize);
964 fseek(fout, nametable, SEEK_SET);
966 #ifdef WORDS_BIGENDIAN
969 sc_writebin(fout, &count, sizeof count);
970 fseek(fout, cod, SEEK_SET);
972 /* First pass: relocate all labels */
973 /* This pass is necessary because the code addresses of labels is only known
974 * after the peephole optimization flag. Labels can occur inside expressions
975 * (e.g. the conditional operator), which are optimized.
980 /* only very short programs have zero labels; no first pass is needed
981 * if there are no labels */
982 lbltab = (cell *) malloc(labnum * sizeof(cell));
984 error(103); /* insufficient memory */
987 while (sc_readasm(fin, line, sizeof line))
990 instr = skipwhitespace(line);
991 /* ignore empty lines */
994 if (tolower(*instr) == 'l' && *(instr + 1) == '.')
996 int lindex = (int)hex2long(instr + 2, NULL);
998 assert(lindex < labnum);
999 lbltab[lindex] = codeindex;
1003 /* get to the end of the instruction (make use of the '\n' that fgets()
1004 * added at the end of the line; this way we will *always* drop on a
1005 * whitespace character) */
1006 for (params = instr; *params != '\0' && !isspace(*params);
1009 assert(params > instr);
1010 i = findopcode(instr, (int)(params - instr));
1011 if (!opcodelist[i].name)
1014 error(104, instr); /* invalid assembler instruction */
1016 if (opcodelist[i].segment == sIN_CSEG)
1018 opcodelist[i].func(NULL, skipwhitespace(params),
1019 opcodelist[i].opcode);
1024 /* Second pass (actually 2 more passes, one for all code and one for all data) */
1027 for (pass = sIN_CSEG; pass <= sIN_DSEG; pass++)
1030 while (sc_readasm(fin, line, sizeof line))
1033 instr = skipwhitespace(line);
1034 /* ignore empty lines and labels (labels have a special syntax, so these
1035 * must be parsed separately) */
1036 if (*instr == '\0' || (tolower(*instr) == 'l'
1037 && *(instr + 1) == '.'))
1039 /* get to the end of the instruction (make use of the '\n' that fgets()
1040 * added at the end of the line; this way we will *always* drop on a
1041 * whitespace character) */
1042 for (params = instr; *params != '\0' && !isspace(*params);
1045 assert(params > instr);
1046 i = findopcode(instr, (int)(params - instr));
1047 assert(opcodelist[i].name != NULL);
1048 if (opcodelist[i].segment == pass)
1049 opcodelist[i].func(fout, skipwhitespace(params),
1050 opcodelist[i].opcode);
1053 if (bytes_out - bytes_in > 0)
1054 error(106); /* compression buffer overflow */
1065 error(101, "disk full");
1067 /* adjust the header */
1070 hdr.size = sc_lengthbin(fout);
1071 #ifdef WORDS_BIGENDIAN
1074 sc_resetbin(fout); /* "size" is the very first field */
1075 sc_writebin(fout, &hdr.size, sizeof hdr.size);