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.
25 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
34 #include <stdlib.h> /* for macro max() */
37 #include "embryo_cc_sc.h"
39 typedef cell(*OPCODE_PROC) (FILE * fbin, char *params, cell opcode);
45 int segment; /* sIN_CSEG=parse in cseg, sIN_DSEG=parse in dseg */
49 static cell codeindex; /* similar to "code_idx" */
50 static cell *lbltab; /* label table */
51 static int writeerror;
52 static int bytes_in, bytes_out;
54 /* apparently, strtol() does not work correctly on very large (unsigned)
55 * hexadecimal values */
57 hex2long(char *s, char **n)
59 unsigned long result = 0L;
63 /* ignore leading whitespace */
64 while (*s == ' ' || *s == '\t')
67 /* allow a negation sign to create the two's complement of numbers */
74 assert((*s >= '0' && *s <= '9') || (*s >= 'a' && *s <= 'f')
75 || (*s >= 'a' && *s <= 'f'));
78 if (*s >= '0' && *s <= '9')
80 else if (*s >= 'a' && *s <= 'f')
81 digit = *s - 'a' + 10;
82 else if (*s >= 'A' && *s <= 'F')
83 digit = *s - 'A' + 10;
85 break; /* probably whitespace */
86 result = (result << 4) | digit;
92 result = (~result) + 1; /* take two's complement of the result */
93 return (ucell) result;
96 #ifdef WORDS_BIGENDIAN
100 unsigned char *s = (unsigned char *)v;
113 unsigned char *s = (unsigned char *)v;
116 /* swap outer two bytes */
120 /* swap inner two bytes */
127 #define aligncell(v) align16(v)
129 #define aligncell(v) align32(v)
132 #define align16(v) (v)
133 #define align32(v) (v)
134 #define aligncell(v) (v)
138 skipwhitespace(char *str)
140 while (isspace(*str))
146 stripcomment(char *str)
148 char *ptr = strchr(str, ';');
152 *ptr++ = '\n'; /* terminate the line, but leave the '\n' */
159 write_encoded(FILE * fbin, ucell * c, int num)
161 assert(sizeof(cell) <= 4); /* code must be adjusted for larger cells */
162 assert(fbin != NULL);
167 ucell p = (ucell) * c;
168 unsigned char t[5]; /* a 32-bit cell is encoded in max. 5 bytes (3 bytes for a 16-bit cell) */
172 for (index = 0; index < 5; index++)
174 t[index] = (unsigned char)(p & 0x7f); /* store 7 bits */
177 /* skip leading zeros */
178 while (index > 1 && t[index - 1] == 0
179 && (t[index - 2] & 0x40) == 0)
181 /* skip leading -1s *//* ??? for BIT16, check for index==3 && t[index-1]==0x03 */
182 if (index == 5 && t[index - 1] == 0x0f
183 && (t[index - 2] & 0x40) != 0)
185 while (index > 1 && t[index - 1] == 0x7f
186 && (t[index - 2] & 0x40) != 0)
188 /* write high byte first, write continuation bits */
193 (unsigned char)((index == 0) ? t[index]
194 : (t[index] | 0x80));
195 writeerror |= !sc_writebin(fbin, &code, 1);
198 bytes_in += sizeof *c;
199 assert(AMX_EXPANDMARGIN > 2);
200 if (bytes_out - bytes_in >= AMX_EXPANDMARGIN - 2)
201 error(106); /* compression buffer overflow */
205 assert((sc_lengthbin(fbin) % sizeof(cell)) == 0);
206 writeerror |= !sc_writebin(fbin, aligncell(c), sizeof *c);
212 #if defined __BORLANDC__ || defined __WATCOMC__
217 noop(FILE * fbin __UNUSED__, char *params __UNUSED__, cell opcode __UNUSED__)
222 #if defined __BORLANDC__ || defined __WATCOMC__
227 parm0(FILE * fbin, char *params __UNUSED__, cell opcode)
230 write_encoded(fbin, (ucell *) & opcode, 1);
235 parm1(FILE * fbin, char *params, cell opcode)
237 ucell p = hex2long(params, NULL);
241 write_encoded(fbin, (ucell *) & opcode, 1);
242 write_encoded(fbin, &p, 1);
244 return opcodes(1) + opargs(1);
248 parm2(FILE * fbin, char *params, cell opcode)
252 p[0] = hex2long(params, ¶ms);
253 p[1] = hex2long(params, NULL);
256 write_encoded(fbin, (ucell *) & opcode, 1);
257 write_encoded(fbin, p, 2);
259 return opcodes(1) + opargs(2);
262 #if defined __BORLANDC__ || defined __WATCOMC__
267 do_dump(FILE * fbin, char *params, cell opcode __UNUSED__)
272 while (*params != '\0')
274 p = hex2long(params, ¶ms);
276 write_encoded(fbin, &p, 1);
278 while (isspace(*params))
281 return num * sizeof(cell);
285 do_call(FILE * fbin, char *params, cell opcode)
287 char name[sNAMEMAX + 1];
292 for (i = 0; !isspace(*params); i++, params++)
294 assert(*params != '\0');
295 assert(i < sNAMEMAX);
300 /* look up the function address; note that the correct file number must
301 * already have been set (in order for static globals to be found).
305 assert(sym->ident == iFUNCTN || sym->ident == iREFFUNC);
306 assert(sym->vclass == sGLOBAL);
311 write_encoded(fbin, (ucell *) & opcode, 1);
312 write_encoded(fbin, &p, 1);
314 return opcodes(1) + opargs(1);
318 do_jump(FILE * fbin, char *params, cell opcode)
323 i = (int)hex2long(params, NULL);
324 assert(i >= 0 && i < labnum);
328 assert(lbltab != NULL);
330 write_encoded(fbin, (ucell *) & opcode, 1);
331 write_encoded(fbin, &p, 1);
333 return opcodes(1) + opargs(1);
337 do_file(FILE * fbin, char *params, cell opcode)
342 p = hex2long(params, ¶ms);
344 /* remove leading and trailing white space from the filename */
345 while (isspace(*params))
347 len = strlen(params);
348 while (len > 0 && isspace(params[len - 1]))
350 params[len++] = '\0'; /* zero-terminate */
351 while (len % sizeof(cell) != 0)
352 params[len++] = '\0'; /* pad with zeros up to full cell */
353 assert(len > 0 && len < 256);
354 clen = len + sizeof(cell); /* add size of file ordinal */
358 write_encoded(fbin, (ucell *) & opcode, 1);
359 write_encoded(fbin, &clen, 1);
360 write_encoded(fbin, &p, 1);
361 write_encoded(fbin, (ucell *) params, len / sizeof(cell));
363 return opcodes(1) + opargs(1) + clen; /* other argument is in clen */
367 do_symbol(FILE * fbin, char *params, cell opcode)
370 ucell offset, clen, flags;
372 unsigned char mclass, type;
374 for (endptr = params; !isspace(*endptr) && endptr != '\0'; endptr++)
376 assert(*endptr == ' ');
378 len = (int)(endptr - params);
379 assert(len > 0 && len < sNAMEMAX);
380 /* first get the other parameters from the line */
381 offset = hex2long(endptr, &endptr);
382 mclass = (unsigned char)hex2long(endptr, &endptr);
383 type = (unsigned char)hex2long(endptr, NULL);
384 flags = type + 256 * mclass;
385 /* now finish up the name (overwriting the input line) */
386 params[len++] = '\0'; /* zero-terminate */
387 while (len % sizeof(cell) != 0)
388 params[len++] = '\0'; /* pad with zeros up to full cell */
389 clen = len + 2 * sizeof(cell); /* add size of symbol address and flags */
393 write_encoded(fbin, (ucell *) & opcode, 1);
394 write_encoded(fbin, &clen, 1);
395 write_encoded(fbin, &offset, 1);
396 write_encoded(fbin, &flags, 1);
397 write_encoded(fbin, (ucell *) params, len / sizeof(cell));
401 /* function should start right after the symbolic information */
402 if (fbin == NULL && mclass == 0 && type == iFUNCTN)
403 assert(offset == codeindex + opcodes(1) + opargs(1) + clen);
406 return opcodes(1) + opargs(1) + clen; /* other 2 arguments are in clen */
410 do_switch(FILE * fbin, char *params, cell opcode)
415 i = (int)hex2long(params, NULL);
416 assert(i >= 0 && i < labnum);
420 assert(lbltab != NULL);
422 write_encoded(fbin, (ucell *) & opcode, 1);
423 write_encoded(fbin, &p, 1);
425 return opcodes(1) + opargs(1);
428 #if defined __BORLANDC__ || defined __WATCOMC__
433 do_case(FILE * fbin, char *params, cell opcode __UNUSED__)
438 v = hex2long(params, ¶ms);
439 i = (int)hex2long(params, NULL);
440 assert(i >= 0 && i < labnum);
444 assert(lbltab != NULL);
446 write_encoded(fbin, &v, 1);
447 write_encoded(fbin, &p, 1);
449 return opcodes(0) + opargs(2);
452 #if defined __BORLANDC__ || defined __WATCOMC__
457 curfile(FILE * fbin __UNUSED__, char *params, cell opcode __UNUSED__)
459 fcurrent = (int)hex2long(params, NULL);
463 static OPCODE opcodelist[] = {
464 /* node for "invalid instruction" */
466 /* opcodes in sorted order */
467 {78, "add", sIN_CSEG, parm0},
468 {87, "add.c", sIN_CSEG, parm1},
469 {14, "addr.alt", sIN_CSEG, parm1},
470 {13, "addr.pri", sIN_CSEG, parm1},
471 {30, "align.alt", sIN_CSEG, parm1},
472 {29, "align.pri", sIN_CSEG, parm1},
473 {81, "and", sIN_CSEG, parm0},
474 {121, "bounds", sIN_CSEG, parm1},
475 {49, "call", sIN_CSEG, do_call},
476 {50, "call.pri", sIN_CSEG, parm0},
477 {0, "case", sIN_CSEG, do_case},
478 {130, "casetbl", sIN_CSEG, parm0}, /* version 1 */
479 {118, "cmps", sIN_CSEG, parm1},
480 {0, "code", 0, noop},
481 {12, "const.alt", sIN_CSEG, parm1},
482 {11, "const.pri", sIN_CSEG, parm1},
483 {0, "curfile", sIN_CSEG, curfile},
484 {0, "data", 0, noop},
485 {114, "dec", sIN_CSEG, parm1},
486 {113, "dec.alt", sIN_CSEG, parm0},
487 {116, "dec.i", sIN_CSEG, parm0},
488 {112, "dec.pri", sIN_CSEG, parm0},
489 {115, "dec.s", sIN_CSEG, parm1},
490 {0, "dump", sIN_DSEG, do_dump},
491 {95, "eq", sIN_CSEG, parm0},
492 {106, "eq.c.alt", sIN_CSEG, parm1},
493 {105, "eq.c.pri", sIN_CSEG, parm1},
494 {124, "file", sIN_CSEG, do_file},
495 {119, "fill", sIN_CSEG, parm1},
496 {100, "geq", sIN_CSEG, parm0},
497 {99, "grtr", sIN_CSEG, parm0},
498 {120, "halt", sIN_CSEG, parm1},
499 {45, "heap", sIN_CSEG, parm1},
500 {27, "idxaddr", sIN_CSEG, parm0},
501 {28, "idxaddr.b", sIN_CSEG, parm1},
502 {109, "inc", sIN_CSEG, parm1},
503 {108, "inc.alt", sIN_CSEG, parm0},
504 {111, "inc.i", sIN_CSEG, parm0},
505 {107, "inc.pri", sIN_CSEG, parm0},
506 {110, "inc.s", sIN_CSEG, parm1},
507 {86, "invert", sIN_CSEG, parm0},
508 {55, "jeq", sIN_CSEG, do_jump},
509 {60, "jgeq", sIN_CSEG, do_jump},
510 {59, "jgrtr", sIN_CSEG, do_jump},
511 {58, "jleq", sIN_CSEG, do_jump},
512 {57, "jless", sIN_CSEG, do_jump},
513 {56, "jneq", sIN_CSEG, do_jump},
514 {54, "jnz", sIN_CSEG, do_jump},
515 {52, "jrel", sIN_CSEG, parm1}, /* always a number */
516 {64, "jsgeq", sIN_CSEG, do_jump},
517 {63, "jsgrtr", sIN_CSEG, do_jump},
518 {62, "jsleq", sIN_CSEG, do_jump},
519 {61, "jsless", sIN_CSEG, do_jump},
520 {51, "jump", sIN_CSEG, do_jump},
521 {128, "jump.pri", sIN_CSEG, parm0}, /* version 1 */
522 {53, "jzer", sIN_CSEG, do_jump},
523 {31, "lctrl", sIN_CSEG, parm1},
524 {98, "leq", sIN_CSEG, parm0},
525 {97, "less", sIN_CSEG, parm0},
526 {25, "lidx", sIN_CSEG, parm0},
527 {26, "lidx.b", sIN_CSEG, parm1},
528 {125, "line", sIN_CSEG, parm2},
529 {2, "load.alt", sIN_CSEG, parm1},
530 {9, "load.i", sIN_CSEG, parm0},
531 {1, "load.pri", sIN_CSEG, parm1},
532 {4, "load.s.alt", sIN_CSEG, parm1},
533 {3, "load.s.pri", sIN_CSEG, parm1},
534 {10, "lodb.i", sIN_CSEG, parm1},
535 {6, "lref.alt", sIN_CSEG, parm1},
536 {5, "lref.pri", sIN_CSEG, parm1},
537 {8, "lref.s.alt", sIN_CSEG, parm1},
538 {7, "lref.s.pri", sIN_CSEG, parm1},
539 {34, "move.alt", sIN_CSEG, parm0},
540 {33, "move.pri", sIN_CSEG, parm0},
541 {117, "movs", sIN_CSEG, parm1},
542 {85, "neg", sIN_CSEG, parm0},
543 {96, "neq", sIN_CSEG, parm0},
544 {134, "nop", sIN_CSEG, parm0}, /* version 6 */
545 {84, "not", sIN_CSEG, parm0},
546 {82, "or", sIN_CSEG, parm0},
547 {43, "pop.alt", sIN_CSEG, parm0},
548 {42, "pop.pri", sIN_CSEG, parm0},
549 {46, "proc", sIN_CSEG, parm0},
550 {40, "push", sIN_CSEG, parm1},
551 {37, "push.alt", sIN_CSEG, parm0},
552 {39, "push.c", sIN_CSEG, parm1},
553 {36, "push.pri", sIN_CSEG, parm0},
554 {38, "push.r", sIN_CSEG, parm1},
555 {41, "push.s", sIN_CSEG, parm1},
556 {133, "pushaddr", sIN_CSEG, parm1}, /* version 4 */
557 {47, "ret", sIN_CSEG, parm0},
558 {48, "retn", sIN_CSEG, parm0},
559 {32, "sctrl", sIN_CSEG, parm1},
560 {73, "sdiv", sIN_CSEG, parm0},
561 {74, "sdiv.alt", sIN_CSEG, parm0},
562 {104, "sgeq", sIN_CSEG, parm0},
563 {103, "sgrtr", sIN_CSEG, parm0},
564 {65, "shl", sIN_CSEG, parm0},
565 {69, "shl.c.alt", sIN_CSEG, parm1},
566 {68, "shl.c.pri", sIN_CSEG, parm1},
567 {66, "shr", sIN_CSEG, parm0},
568 {71, "shr.c.alt", sIN_CSEG, parm1},
569 {70, "shr.c.pri", sIN_CSEG, parm1},
570 {94, "sign.alt", sIN_CSEG, parm0},
571 {93, "sign.pri", sIN_CSEG, parm0},
572 {102, "sleq", sIN_CSEG, parm0},
573 {101, "sless", sIN_CSEG, parm0},
574 {72, "smul", sIN_CSEG, parm0},
575 {88, "smul.c", sIN_CSEG, parm1},
576 {127, "srange", sIN_CSEG, parm2}, /* version 1 */
577 {20, "sref.alt", sIN_CSEG, parm1},
578 {19, "sref.pri", sIN_CSEG, parm1},
579 {22, "sref.s.alt", sIN_CSEG, parm1},
580 {21, "sref.s.pri", sIN_CSEG, parm1},
581 {67, "sshr", sIN_CSEG, parm0},
582 {44, "stack", sIN_CSEG, parm1},
583 {0, "stksize", 0, noop},
584 {16, "stor.alt", sIN_CSEG, parm1},
585 {23, "stor.i", sIN_CSEG, parm0},
586 {15, "stor.pri", sIN_CSEG, parm1},
587 {18, "stor.s.alt", sIN_CSEG, parm1},
588 {17, "stor.s.pri", sIN_CSEG, parm1},
589 {24, "strb.i", sIN_CSEG, parm1},
590 {79, "sub", sIN_CSEG, parm0},
591 {80, "sub.alt", sIN_CSEG, parm0},
592 {132, "swap.alt", sIN_CSEG, parm0}, /* version 4 */
593 {131, "swap.pri", sIN_CSEG, parm0}, /* version 4 */
594 {129, "switch", sIN_CSEG, do_switch}, /* version 1 */
595 {126, "symbol", sIN_CSEG, do_symbol},
596 {136, "symtag", sIN_CSEG, parm1}, /* version 7 */
597 {123, "sysreq.c", sIN_CSEG, parm1},
598 {135, "sysreq.d", sIN_CSEG, parm1}, /* version 7, not generated directly */
599 {122, "sysreq.pri", sIN_CSEG, parm0},
600 {76, "udiv", sIN_CSEG, parm0},
601 {77, "udiv.alt", sIN_CSEG, parm0},
602 {75, "umul", sIN_CSEG, parm0},
603 {35, "xchg", sIN_CSEG, parm0},
604 {83, "xor", sIN_CSEG, parm0},
605 {91, "zero", sIN_CSEG, parm1},
606 {90, "zero.alt", sIN_CSEG, parm0},
607 {89, "zero.pri", sIN_CSEG, parm0},
608 {92, "zero.s", sIN_CSEG, parm1},
611 #define MAX_INSTR_LEN 30
613 findopcode(char *instr, int maxlen)
615 int low, high, mid, cmp;
616 char str[MAX_INSTR_LEN];
618 if (maxlen >= MAX_INSTR_LEN)
620 strncpy(str, instr, maxlen);
621 str[maxlen] = '\0'; /* make sure the string is zero terminated */
622 /* look up the instruction with a binary search
623 * the assembler is case insensitive to instructions (but case sensitive
626 low = 1; /* entry 0 is reserved (for "not found") */
627 high = (sizeof opcodelist / sizeof opcodelist[0]) - 1;
630 mid = (low + high) / 2;
631 assert(opcodelist[mid].name != NULL);
632 cmp = strcasecmp(str, opcodelist[mid].name);
640 if (strcasecmp(str, opcodelist[low].name) == 0)
641 return low; /* found */
642 return 0; /* not found, return special index */
646 assemble(FILE * fout, FILE * fin)
648 typedef struct tagFUNCSTUB
650 unsigned int address, nameofs;
654 int numpublics, numnatives, numlibraries, numpubvars,
656 long nametablesize, nameofs;
657 char line[256], *instr, *params;
660 symbol *sym, **nativelist;
661 constvalue *constptr;
663 int nametable, tags, libraries, publics, natives, pubvars;
664 int cod, dat, hea, stp, cip, size, defsize;
667 /* verify that the opcode list is sorted (skip entry 1; it is reserved
668 * for a non-existant opcode)
670 assert(opcodelist[1].name != NULL);
671 for (i = 2; i < (sizeof opcodelist / sizeof opcodelist[0]); i++)
673 assert(opcodelist[i].name != NULL);
674 assert(strcasecmp(opcodelist[i].name, opcodelist[i - 1].name) > 0);
679 nametablesize = sizeof(short);
684 /* count number of public and native functions and public variables */
685 for (sym = glbtab.next; sym != NULL; sym = sym->next)
687 char alias[sNAMEMAX + 1] = "";
690 if (sym->ident == iFUNCTN)
692 assert(strlen(sym->name) <= sNAMEMAX);
693 if ((sym->usage & uNATIVE) != 0 && (sym->usage & uREAD) != 0
696 match = ++numnatives;
697 if (!lookup_alias(alias, sym->name))
698 strcpy(alias, sym->name);
700 if ((sym->usage & uPUBLIC) != 0 && (sym->usage & uDEFINE) != 0)
702 match = ++numpublics;
703 strcpy(alias, sym->name);
705 if (strcmp(sym->name, uMAINFUNC) == 0)
707 assert(sym->vclass == sGLOBAL);
708 mainaddr = sym->addr;
711 else if (sym->ident == iVARIABLE)
713 if ((sym->usage & uPUBLIC) != 0)
715 match = ++numpubvars;
716 strcpy(alias, sym->name);
721 assert(alias[0] != '\0');
722 nametablesize += strlen(alias) + 1;
725 assert(numnatives == ntv_funcid);
727 /* count number of libraries */
729 for (constptr = libname_tab.next; constptr != NULL;
730 constptr = constptr->next)
732 if (constptr->value > 0)
734 assert(constptr->name[0] != '\0');
736 nametablesize += strlen(constptr->name) + 1;
740 /* count number of public tags */
742 for (constptr = tagname_tab.next; constptr != NULL;
743 constptr = constptr->next)
745 if ((constptr->value & PUBLICTAG) != 0)
747 assert(constptr->name[0] != '\0');
749 nametablesize += strlen(constptr->name) + 1;
753 /* pad the header to sc_dataalign
754 * => thereby the code segment is aligned
755 * => since the code segment is padded to a sc_dataalign boundary, the data segment is aligned
756 * => and thereby the stack top is aligned too
758 assert(sc_dataalign != 0);
759 padding = sc_dataalign - (sizeof hdr + nametablesize) % sc_dataalign;
760 if (padding == sc_dataalign)
763 /* write the abstract machine header */
764 memset(&hdr, 0, sizeof hdr);
765 hdr.magic = (unsigned short)0xF1E0;
766 hdr.file_version = CUR_FILE_VERSION;
767 hdr.amx_version = MIN_AMX_VERSION;
768 hdr.flags = (short)(sc_debug & sSYMBOLIC);
770 hdr.flags |= AMX_FLAG_CHAR16;
772 hdr.flags |= AMX_FLAG_COMPACT;
774 hdr.flags |= AMX_FLAG_NOCHECKS;
775 // #ifdef WORDS_BIGENDIAN
776 // hdr.flags|=AMX_FLAG_BIGENDIAN;
778 defsize = hdr.defsize = sizeof(FUNCSTUB);
779 assert((hdr.defsize % sizeof(cell)) == 0);
780 publics = hdr.publics = sizeof hdr; /* public table starts right after the header */
781 natives = hdr.natives = hdr.publics + numpublics * sizeof(FUNCSTUB);
782 libraries = hdr.libraries = hdr.natives + numnatives * sizeof(FUNCSTUB);
783 pubvars = hdr.pubvars = hdr.libraries + numlibraries * sizeof(FUNCSTUB);
784 tags = hdr.tags = hdr.pubvars + numpubvars * sizeof(FUNCSTUB);
785 nametable = hdr.nametable = hdr.tags + numtags * sizeof(FUNCSTUB);
786 cod = hdr.cod = hdr.nametable + nametablesize + padding;
787 dat = hdr.dat = hdr.cod + code_idx;
788 hea = hdr.hea = hdr.dat + glb_declared * sizeof(cell);
789 stp = hdr.stp = hdr.hea + sc_stksize * sizeof(cell);
790 cip = hdr.cip = mainaddr;
791 size = hdr.size = hdr.hea; /* preset, this is incorrect in case of compressed output */
792 #ifdef WORDS_BIGENDIAN
796 align16(&hdr.defsize);
802 align32(&hdr.publics);
803 align32(&hdr.natives);
804 align32(&hdr.libraries);
805 align32(&hdr.pubvars);
807 align32(&hdr.nametable);
809 sc_writebin(fout, &hdr, sizeof hdr);
811 /* dump zeros up to the rest of the header, so that we can easily "seek" */
812 for (nameofs = sizeof hdr; nameofs < cod; nameofs++)
814 nameofs = nametable + sizeof(short);
816 /* write the public functions table */
818 for (sym = glbtab.next; sym != NULL; sym = sym->next)
820 if (sym->ident == iFUNCTN
821 && (sym->usage & uPUBLIC) != 0 && (sym->usage & uDEFINE) != 0)
823 assert(sym->vclass == sGLOBAL);
824 func.address = sym->addr;
825 func.nameofs = nameofs;
826 #ifdef WORDS_BIGENDIAN
827 align32(&func.address);
828 align32(&func.nameofs);
830 fseek(fout, publics + count * sizeof(FUNCSTUB), SEEK_SET);
831 sc_writebin(fout, &func, sizeof func);
832 fseek(fout, nameofs, SEEK_SET);
833 sc_writebin(fout, sym->name, strlen(sym->name) + 1);
834 nameofs += strlen(sym->name) + 1;
839 /* write the natives table */
840 /* The native functions must be written in sorted order. (They are
841 * sorted on their "id", not on their name). A nested loop to find
842 * each successive function would be an O(n^2) operation. But we
843 * do not really need to sort, because the native function id's
844 * are sequential and there are no duplicates. So we first walk
845 * through the complete symbol list and store a pointer to every
846 * native function of interest in a temporary table, where its id
847 * serves as the index in the table. Now we can walk the table and
848 * have all native functions in sorted order.
852 nativelist = (symbol **) malloc(numnatives * sizeof(symbol *));
853 if (nativelist == NULL)
854 error(103); /* insufficient memory */
856 memset(nativelist, 0, numnatives * sizeof(symbol *)); /* for NULL checking */
858 for (sym = glbtab.next; sym != NULL; sym = sym->next)
860 if (sym->ident == iFUNCTN && (sym->usage & uNATIVE) != 0
861 && (sym->usage & uREAD) != 0 && sym->addr >= 0)
863 assert(sym->addr < numnatives);
864 nativelist[(int)sym->addr] = sym;
868 for (i = 0; i < numnatives; i++)
870 char alias[sNAMEMAX + 1];
874 if (!lookup_alias(alias, sym->name))
876 assert(strlen(sym->name) <= sNAMEMAX);
877 strcpy(alias, sym->name);
879 assert(sym->vclass == sGLOBAL);
881 func.nameofs = nameofs;
882 #ifdef WORDS_BIGENDIAN
883 align32(&func.address);
884 align32(&func.nameofs);
886 fseek(fout, natives + count * sizeof(FUNCSTUB), SEEK_SET);
887 sc_writebin(fout, &func, sizeof func);
888 fseek(fout, nameofs, SEEK_SET);
889 sc_writebin(fout, alias, strlen(alias) + 1);
890 nameofs += strlen(alias) + 1;
896 /* write the libraries table */
898 for (constptr = libname_tab.next; constptr != NULL;
899 constptr = constptr->next)
901 if (constptr->value > 0)
903 assert(constptr->name[0] != '\0');
905 func.nameofs = nameofs;
906 #ifdef WORDS_BIGENDIAN
907 align32(&func.address);
908 align32(&func.nameofs);
910 fseek(fout, libraries + count * sizeof(FUNCSTUB), SEEK_SET);
911 sc_writebin(fout, &func, sizeof func);
912 fseek(fout, nameofs, SEEK_SET);
913 sc_writebin(fout, constptr->name, strlen(constptr->name) + 1);
914 nameofs += strlen(constptr->name) + 1;
919 /* write the public variables table */
921 for (sym = glbtab.next; sym != NULL; sym = sym->next)
923 if (sym->ident == iVARIABLE && (sym->usage & uPUBLIC) != 0)
925 assert((sym->usage & uDEFINE) != 0);
926 assert(sym->vclass == sGLOBAL);
927 func.address = sym->addr;
928 func.nameofs = nameofs;
929 #ifdef WORDS_BIGENDIAN
930 align32(&func.address);
931 align32(&func.nameofs);
933 fseek(fout, pubvars + count * sizeof(FUNCSTUB), SEEK_SET);
934 sc_writebin(fout, &func, sizeof func);
935 fseek(fout, nameofs, SEEK_SET);
936 sc_writebin(fout, sym->name, strlen(sym->name) + 1);
937 nameofs += strlen(sym->name) + 1;
942 /* write the public tagnames table */
944 for (constptr = tagname_tab.next; constptr != NULL;
945 constptr = constptr->next)
947 if ((constptr->value & PUBLICTAG) != 0)
949 assert(constptr->name[0] != '\0');
950 func.address = constptr->value & TAGMASK;
951 func.nameofs = nameofs;
952 #ifdef WORDS_BIGENDIAN
953 align32(&func.address);
954 align32(&func.nameofs);
956 fseek(fout, tags + count * sizeof(FUNCSTUB), SEEK_SET);
957 sc_writebin(fout, &func, sizeof func);
958 fseek(fout, nameofs, SEEK_SET);
959 sc_writebin(fout, constptr->name, strlen(constptr->name) + 1);
960 nameofs += strlen(constptr->name) + 1;
965 /* write the "maximum name length" field in the name table */
966 assert(nameofs == nametable + nametablesize);
967 fseek(fout, nametable, SEEK_SET);
969 #ifdef WORDS_BIGENDIAN
972 sc_writebin(fout, &count, sizeof count);
973 fseek(fout, cod, SEEK_SET);
975 /* First pass: relocate all labels */
976 /* This pass is necessary because the code addresses of labels is only known
977 * after the peephole optimization flag. Labels can occur inside expressions
978 * (e.g. the conditional operator), which are optimized.
983 /* only very short programs have zero labels; no first pass is needed
984 * if there are no labels */
985 lbltab = (cell *) malloc(labnum * sizeof(cell));
987 error(103); /* insufficient memory */
990 while (sc_readasm(fin, line, sizeof line) != NULL)
993 instr = skipwhitespace(line);
994 /* ignore empty lines */
997 if (tolower(*instr) == 'l' && *(instr + 1) == '.')
999 int lindex = (int)hex2long(instr + 2, NULL);
1001 assert(lindex < labnum);
1002 lbltab[lindex] = codeindex;
1006 /* get to the end of the instruction (make use of the '\n' that fgets()
1007 * added at the end of the line; this way we will *always* drop on a
1008 * whitespace character) */
1009 for (params = instr; *params != '\0' && !isspace(*params);
1012 assert(params > instr);
1013 i = findopcode(instr, (int)(params - instr));
1014 if (opcodelist[i].name == NULL)
1017 error(104, instr); /* invalid assembler instruction */
1019 if (opcodelist[i].segment == sIN_CSEG)
1021 opcodelist[i].func(NULL, skipwhitespace(params),
1022 opcodelist[i].opcode);
1027 /* Second pass (actually 2 more passes, one for all code and one for all data) */
1030 for (pass = sIN_CSEG; pass <= sIN_DSEG; pass++)
1033 while (sc_readasm(fin, line, sizeof line) != NULL)
1036 instr = skipwhitespace(line);
1037 /* ignore empty lines and labels (labels have a special syntax, so these
1038 * must be parsed separately) */
1039 if (*instr == '\0' || (tolower(*instr) == 'l'
1040 && *(instr + 1) == '.'))
1042 /* get to the end of the instruction (make use of the '\n' that fgets()
1043 * added at the end of the line; this way we will *always* drop on a
1044 * whitespace character) */
1045 for (params = instr; *params != '\0' && !isspace(*params);
1048 assert(params > instr);
1049 i = findopcode(instr, (int)(params - instr));
1050 assert(opcodelist[i].name != NULL);
1051 if (opcodelist[i].segment == pass)
1052 opcodelist[i].func(fout, skipwhitespace(params),
1053 opcodelist[i].opcode);
1056 if (bytes_out - bytes_in > 0)
1057 error(106); /* compression buffer overflow */
1068 error(101, "disk full");
1070 /* adjust the header */
1073 hdr.size = sc_lengthbin(fout);
1074 #ifdef WORDS_BIGENDIAN
1077 sc_resetbin(fout); /* "size" is the very first field */
1078 sc_writebin(fout, &hdr.size, sizeof hdr.size);