2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
5 * Function and variable definition and declaration, statement parser.
7 * Copyright (c) ITB CompuPhase, 1997-2003
9 * This software is provided "as-is", without any express or implied
10 * warranty. In no event will the authors be held liable for any
11 * damages arising from the use of this software. Permission is granted
12 * to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to
14 * the following restrictions:
16 * 1. The origin of this software must not be misrepresented;
17 * you must not claim that you wrote the original software.
18 * If you use this software in a product, an acknowledgment in the
19 * product documentation would be appreciated but is not required.
20 * 2. Altered source versions must be plainly marked as such, and
21 * must not be misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
28 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
45 #endif /* HAVE_EVIL */
47 #include "embryo_cc_osdefs.h"
48 #include "embryo_cc_sc.h"
49 #include "embryo_cc_prefix.h"
51 #define VERSION_STR "2.4"
52 #define VERSION_INT 240
54 static void resetglobals(void);
55 static void initglobals(void);
56 static void setopt(int argc, char **argv,
57 char *iname, char *oname,
58 char *pname, char *rname);
59 static void setconfig(char *root);
60 static void about(void);
61 static void setconstants(void);
62 static void parse(void);
63 static void dumplits(void);
64 static void dumpzero(int count);
65 static void declfuncvar(int tok, char *symname,
67 int fstatic, int fstock, int fconst);
68 static void declglb(char *firstname, int firsttag,
69 int fpublic, int fstatic, int stock, int fconst);
70 static int declloc(int fstatic);
71 static void decl_const(int table);
72 static void decl_enum(int table);
73 static cell needsub(int *tag);
74 static void initials(int ident, int tag,
75 cell * size, int dim[], int numdim);
76 static cell initvector(int ident, int tag, cell size, int fillzero);
77 static cell init(int ident, int *tag);
78 static void funcstub(int native);
79 static int newfunc(char *firstname, int firsttag,
80 int fpublic, int fstatic, int stock);
81 static int declargs(symbol * sym);
82 static void doarg(char *name, int ident, int offset,
83 int tags[], int numtags,
84 int fpublic, int fconst, arginfo * arg);
85 static void reduce_referrers(symbol * root);
86 static int testsymbols(symbol * root, int level,
87 int testlabs, int testconst);
88 static void destructsymbols(symbol * root, int level);
89 static constvalue *find_constval_byval(constvalue * table, cell val);
90 static void statement(int *lastindent, int allow_decl);
91 static void compound(void);
92 static void doexpr(int comma, int chkeffect,
93 int allowarray, int mark_endexpr,
94 int *tag, int chkfuncresult);
95 static void doassert(void);
96 static void doexit(void);
97 static void test(int label, int parens, int invert);
98 static void doif(void);
99 static void dowhile(void);
100 static void dodo(void);
101 static void dofor(void);
102 static void doswitch(void);
103 static void dogoto(void);
104 static void dolabel(void);
105 static symbol *fetchlab(char *name);
106 static void doreturn(void);
107 static void dobreak(void);
108 static void docont(void);
109 static void dosleep(void);
110 static void addwhile(int *ptr);
111 static void delwhile(void);
112 static int *readwhile(void);
114 static int lastst = 0; /* last executed statement type */
115 static int nestlevel = 0; /* number of active (open) compound statements */
116 static int rettype = 0; /* the type that a "return" expression should have */
117 static int skipinput = 0; /* number of lines to skip from the first input file */
118 static int wq[wqTABSZ]; /* "while queue", internal stack for nested loops */
119 static int *wqptr; /* pointer to next entry */
120 static char binfname[_MAX_PATH]; /* binary file name */
123 main(int argc, char *argv[], char *env[] __UNUSED__)
125 char argv0[_MAX_PATH];
128 snprintf(argv0, _MAX_PATH, "%s", argv[0]);
129 /* Linux stores the name of the program in argv[0], but not the path.
130 * To adjust this, I store a string with the path in argv[0]. To do
131 * so, I try to get the current path with getcwd(), and if that fails
132 * I search for the PWD= setting in the environment.
134 if (NULL != getcwd(argv0, _MAX_PATH))
137 snprintf(argv0 + i, _MAX_PATH - i, "/%s", argv[0]);
141 char *pwd = getenv("PWD");
144 snprintf(argv0, _MAX_PATH, "%s/%s", pwd, argv[0]);
146 argv[0] = argv0; /* set location to new first parameter */
148 e_prefix_determine(argv0);
150 return sc_compile(argc, argv);
154 sc_error(int number, char *message, char *filename, int firstline,
155 int lastline, va_list argptr)
157 static char *prefix[3] = { "error", "fatal error", "warning" };
163 pre = prefix[number / 100];
165 fprintf(stderr, "%s(%d -- %d) : %s %03d: ", filename, firstline,
166 lastline, pre, number);
168 fprintf(stderr, "%s(%d) : %s %03d: ", filename, lastline, pre,
171 vfprintf(stderr, message, argptr);
177 sc_opensrc(char *filename)
179 return fopen(filename, "rb");
183 sc_closesrc(void *handle)
185 assert(handle != NULL);
186 fclose((FILE *) handle);
190 sc_resetsrc(void *handle, void *position)
192 assert(handle != NULL);
193 fsetpos((FILE *) handle, (fpos_t *) position);
197 sc_readsrc(void *handle, char *target, int maxchars)
199 return fgets(target, maxchars, (FILE *) handle);
203 sc_getpossrc(void *handle)
205 static fpos_t lastpos; /* may need to have a LIFO stack of
208 fgetpos((FILE *) handle, &lastpos);
213 sc_eofsrc(void *handle)
215 return feof((FILE *) handle);
221 return fdopen(fd, "w+");
225 sc_closeasm(void *handle)
228 fclose((FILE *) handle);
232 sc_resetasm(void *handle)
234 fflush((FILE *) handle);
235 fseek((FILE *) handle, 0, SEEK_SET);
239 sc_writeasm(void *handle, char *st)
241 return fputs(st, (FILE *) handle) >= 0;
245 sc_readasm(void *handle, char *target, int maxchars)
247 return fgets(target, maxchars, (FILE *) handle);
251 sc_openbin(char *filename)
253 return fopen(filename, "wb");
257 sc_closebin(void *handle, int deletefile)
259 fclose((FILE *) handle);
265 sc_resetbin(void *handle)
267 fflush((FILE *) handle);
268 fseek((FILE *) handle, 0, SEEK_SET);
272 sc_writebin(void *handle, void *buffer, int size)
274 return (int)fwrite(buffer, 1, size, (FILE *) handle) == size;
278 sc_lengthbin(void *handle)
280 return ftell((FILE *) handle);
283 /* "main" of the compiler
286 sc_compile(int argc, char *argv[])
288 int entry, i, jmpcode, fd_out;
290 char incfname[_MAX_PATH];
291 char reportname[_MAX_PATH];
295 int lcl_packstr, lcl_needsemicolon, lcl_tabsize;
298 /* set global variables to their initial value */
302 errorset(sEXPRRELEASE);
305 /* make sure that we clean up on a fatal error; do this before the
306 * first call to error(). */
307 if ((jmpcode = setjmp(errbuf)) != 0)
310 /* allocate memory for fixed tables */
311 inpfname = (char *)malloc(_MAX_PATH);
312 litq = (cell *) malloc(litmax * sizeof(cell));
314 error(103); /* insufficient memory */
316 error(103); /* insufficient memory */
318 setopt(argc, argv, inpfname, binfname, incfname, reportname);
320 /* open the output file */
323 tmpdir = getenv("TMPDIR");
324 if (!tmpdir) tmpdir = "/tmp";
326 tmpdir = (char *)evil_tmpdir_get();
327 #endif /* ! HAVE_EVIL */
329 snprintf(outfname, _MAX_PATH, "%s/embryo_cc.asm-tmp-XXXXXX", tmpdir);
330 fd_out = mkstemp(outfname);
332 error(101, outfname);
334 unlink (outfname); /* kill this file as soon as it's (f)close'd */
336 setconfig(argv[0]); /* the path to the include files */
337 lcl_ctrlchar = sc_ctrlchar;
338 lcl_packstr = sc_packstr;
339 lcl_needsemicolon = sc_needsemicolon;
340 lcl_tabsize = sc_tabsize;
341 inpf = inpf_org = (FILE *) sc_opensrc(inpfname);
343 error(100, inpfname);
345 outf = (FILE *) sc_openasm(fd_out); /* first write to assembler
346 * file (may be temporary) */
348 error(101, outfname);
349 /* immediately open the binary file, for other programs to check */
350 binf = (FILE *) sc_openbin(binfname);
352 error(101, binfname);
353 setconstants(); /* set predefined constants and tagnames */
354 for (i = 0; i < skipinput; i++) /* skip lines in the input file */
355 if (sc_readsrc(inpf, pline, sLINEMAX) != NULL)
356 fline++; /* keep line number up to date */
358 sc_status = statFIRST;
359 /* do the first pass through the file */
360 inpfmark = sc_getpossrc(inpf);
361 if (incfname[0] != '\0')
363 if (strcmp(incfname, sDEF_PREFIX) == 0)
365 plungefile(incfname, FALSE, TRUE); /* parse "default.inc" */
369 if (!plungequalifiedfile(incfname)) /* parse "prefix" include
371 error(100, incfname); /* cannot read from ... (fatal error) */
374 preprocess(); /* fetch first line */
375 parse(); /* process all input */
378 sc_status = statWRITE; /* set, to enable warnings */
380 /* ??? for re-parsing the listing file instead of the original source
381 * file (and doing preprocessing twice):
382 * - close input file, close listing file
383 * - re-open listing file for reading (inpf)
384 * - open assembler file (outf)
387 /* reset "defined" flag of all functions and global variables */
388 reduce_referrers(&glbtab);
389 delete_symbols(&glbtab, 0, TRUE, FALSE);
390 #if !defined NO_DEFINE
394 sc_ctrlchar = lcl_ctrlchar;
395 sc_packstr = lcl_packstr;
396 sc_needsemicolon = lcl_needsemicolon;
397 sc_tabsize = lcl_tabsize;
399 /* reset the source file */
402 sc_resetsrc(inpf, inpfmark); /* reset file position */
403 fline = skipinput; /* reset line number */
404 lexinit(); /* clear internal flags of lex() */
405 sc_status = statWRITE; /* allow to write --this variable was reset
406 * by resetglobals() */
408 setfile(inpfname, fnumber);
409 if (incfname[0] != '\0')
411 if (strcmp(incfname, sDEF_PREFIX) == 0)
412 plungefile(incfname, FALSE, TRUE); /* parse "default.inc" (again) */
414 plungequalifiedfile(incfname); /* parse implicit include
417 preprocess(); /* fetch first line */
418 parse(); /* process all input */
419 /* inpf is already closed when readline() attempts to pop of a file */
420 writetrailer(); /* write remaining stuff */
422 entry = testsymbols(&glbtab, 0, TRUE, FALSE); /* test for unused
423 * or undefined functions and variables */
425 error(13); /* no entry point (no public functions) */
428 if (inpf != NULL) /* main source file is not closed, do it now */
430 /* write the binary file (the file is already open) */
431 if (errnum == 0 && jmpcode == 0)
433 assert(binf != NULL);
434 sc_resetasm(outf); /* flush and loop back, for reading */
435 assemble(binf, outf); /* assembler file is now input */
440 sc_closebin(binf, errnum != 0);
442 if (inpfname != NULL)
448 assert(jmpcode != 0 || loctab.next == NULL); /* on normal flow,
450 * should already have been deleted */
451 delete_symbols(&loctab, 0, TRUE, TRUE); /* delete local variables
452 * if not yet done (i.e.
453 * on a fatal error) */
454 delete_symbols(&glbtab, 0, TRUE, TRUE);
455 delete_consttable(&tagname_tab);
456 delete_consttable(&libname_tab);
459 #if !defined NO_DEFINE
464 printf("\n%d Error%s.\n", errnum, (errnum > 1) ? "s" : "");
467 else if (warnnum != 0)
469 printf("\n%d Warning%s.\n", warnnum, (warnnum > 1) ? "s" : "");
480 sc_addconstant(char *name, cell value, int tag)
482 errorset(sFORCESET); /* make sure error engine is silenced */
483 sc_status = statIDLE;
484 add_constant(name, value, sGLOBAL, tag);
489 sc_addtag(char *name)
497 /* no tagname was given, check for one */
498 if (lex(&val, &name) != tLABEL)
501 return 0; /* untagged */
506 ptr = tagname_tab.next;
509 tag = (int)(ptr->value & TAGMASK);
510 if (strcmp(name, ptr->name) == 0)
511 return tag; /* tagname is known, return its sequence number */
512 tag &= (int)~FIXEDTAG;
518 /* tagname currently unknown, add it */
519 tag = last + 1; /* guaranteed not to exist already */
521 tag |= (int)FIXEDTAG;
522 append_constval(&tagname_tab, name, (cell) tag, 0);
529 /* reset the subset of global variables that is modified by the
531 curfunc = NULL; /* pointer to current function */
532 lastst = 0; /* last executed statement type */
533 nestlevel = 0; /* number of active (open) compound statements */
534 rettype = 0; /* the type that a "return" expression should have */
535 litidx = 0; /* index to literal table */
536 stgidx = 0; /* index to the staging buffer */
537 labnum = 0; /* number of (internal) labels */
538 staging = 0; /* true if staging output */
539 declared = 0; /* number of local cells declared */
540 glb_declared = 0; /* number of global cells declared */
541 code_idx = 0; /* number of bytes with generated code */
542 ntv_funcid = 0; /* incremental number of native function */
543 curseg = 0; /* 1 if currently parsing CODE, 2 if parsing DATA */
544 freading = FALSE; /* no input file ready yet */
545 fline = 0; /* the line number in the current file */
546 fnumber = 0; /* the file number in the file table (debugging) */
547 fcurrent = 0; /* current file being processed (debugging) */
548 intest = 0; /* true if inside a test */
549 sideeffect = 0; /* true if an expression causes a side-effect */
550 stmtindent = 0; /* current indent of the statement */
551 indent_nowarn = TRUE; /* do not skip warning "217 loose indentation" */
552 sc_allowtags = TRUE; /* allow/detect tagnames */
553 sc_status = statIDLE;
561 skipinput = 0; /* number of lines to skip from the first
563 sc_ctrlchar = CTRL_CHAR; /* the escape character */
564 litmax = sDEF_LITMAX; /* current size of the literal table */
565 errnum = 0; /* number of errors */
566 warnnum = 0; /* number of warnings */
567 /* sc_debug=sCHKBOUNDS; by default: bounds checking+assertions */
568 sc_debug = 0; /* by default: no debug */
569 charbits = 8; /* a "char" is 8 bits */
570 sc_packstr = FALSE; /* strings are unpacked by default */
571 /* sc_compress=TRUE; compress output bytecodes */
572 sc_compress = FALSE; /* compress output bytecodes */
573 sc_needsemicolon = FALSE; /* semicolon required to terminate
576 sc_stksize = sDEF_AMXSTACK; /* default stack size */
577 sc_tabsize = 8; /* assume a TAB is 8 spaces */
578 sc_rationaltag = 0; /* assume no support for rational numbers */
579 rational_digits = 0; /* number of fractional digits */
581 outfname[0] = '\0'; /* output file name */
582 inpf = NULL; /* file read from */
583 inpfname = NULL; /* pointer to name of the file currently
585 outf = NULL; /* file written to */
586 litq = NULL; /* the literal queue */
587 glbtab.next = NULL; /* clear global variables/constants table */
588 loctab.next = NULL; /* " local " / " " */
589 tagname_tab.next = NULL; /* tagname table */
590 libname_tab.next = NULL; /* library table (#pragma library "..."
593 pline[0] = '\0'; /* the line read from the input file */
594 lptr = NULL; /* points to the current position in "pline" */
595 curlibrary = NULL; /* current library */
596 inpf_org = NULL; /* main source file */
598 wqptr = wq; /* initialize while queue pointer */
603 parseoptions(int argc, char **argv, char *iname, char *oname,
604 char *pname __UNUSED__, char *rname __UNUSED__)
610 /* use embryo include dir always */
611 snprintf(str, sizeof(str), "%s/include/", e_prefix_data_get());
615 for (i = 1; i < argc; i++)
617 if (!strcmp (argv[i], "-i") && (i + 1 < argc) && *argv[i + 1])
619 /* include directory */
621 strncpy(str, argv[i], sizeof(str));
624 if (str[len - 1] != DIRSEP_CHAR)
626 str[len] = DIRSEP_CHAR;
632 else if (!strcmp (argv[i], "-o") && (i + 1 < argc) && *argv[i + 1])
636 strcpy(oname, argv[i]); /* FIXME */
638 else if (!strcmp (argv[i], "-S") && (i + 1 < argc) && *argv[i + 1])
642 stack_size = atoi(argv[i]);
645 sc_stksize = (cell) stack_size;
652 strcpy(iname, argv[i]); /* FIXME */
656 /* only allow one input filename */
663 setopt(int argc, char **argv, char *iname, char *oname,
664 char *pname, char *rname)
670 strcpy(pname, sDEF_PREFIX);
672 parseoptions(argc, argv, iname, oname, pname, rname);
673 if (iname[0] == '\0')
678 setconfig(char *root)
680 char path[_MAX_PATH];
684 /* add the default "include" directory */
687 /* path + filename (hopefully) */
688 strncpy(path, root, sizeof(path) - 1);
689 path[sizeof(path) - 1] = 0;
691 /* terminate just behind last \ or : */
692 if ((ptr = strrchr(path, DIRSEP_CHAR)) != NULL
693 || (ptr = strchr(path, ':')) != NULL)
695 /* If there was no terminating "\" or ":",
696 * the filename probably does not
697 * contain the path; so we just don't add it
698 * to the list in that case
701 if (strlen(path) < (sizeof(path) - 1 - 7))
703 strcat(path, "include");
706 path[len] = DIRSEP_CHAR;
707 path[len + 1] = '\0';
715 printf("Usage: embryo_cc <filename> [options]\n\n");
716 printf("Options:\n");
719 (" -A<num> alignment in bytes of the data segment and the\
723 (" -a output assembler code (skip code generation\
727 (" -C[+/-] compact encoding for output file (default=%c)\n",
728 sc_compress ? '+' : '-');
729 printf(" -c8 [default] a character is 8-bits\
730 (ASCII/ISO Latin-1)\n");
732 printf(" -c16 a character is 16-bits (Unicode)\n");
733 #if defined dos_setdrive
734 printf(" -Dpath active directory path\n");
737 (" -d0 no symbolic information, no run-time checks\n");
738 printf(" -d1 [default] run-time checks, no symbolic\
741 (" -d2 full debug information and dynamic checking\n");
742 printf(" -d3 full debug information, dynamic checking,\
745 printf(" -i <name> path for include files\n");
747 printf(" -l create list file (preprocess only)\n");
749 printf(" -o <name> set base name of output file\n");
752 (" -P[+/-] strings are \"packed\" by default (default=%c)\n",
753 sc_packstr ? '+' : '-');
754 printf(" -p<name> set name of \"prefix\" file\n");
759 (" -S <num> stack/heap size in cells (default=%d, min=65)\n",
762 printf(" -s<num> skip lines from the input file\n");
764 (" -t<num> TAB indent size (in character positions)\n");
765 printf(" -\\ use '\\' for escape characters\n");
766 printf(" -^ use '^' for escape characters\n");
767 printf(" -;[+/-] require a semicolon to end each statement\
768 (default=%c)\n", sc_needsemicolon ? '+' : '-');
771 (" sym=val define constant \"sym\" with value \"val\"\n");
772 printf(" sym= define constant \"sym\" with value 0\n");
774 longjmp(errbuf, 3); /* user abort */
782 assert(sc_status == statIDLE);
783 append_constval(&tagname_tab, "_", 0, 0); /* "untagged" */
784 append_constval(&tagname_tab, "bool", 1, 0);
786 add_constant("true", 1, sGLOBAL, 1); /* boolean flags */
787 add_constant("false", 0, sGLOBAL, 1);
788 add_constant("EOS", 0, sGLOBAL, 0); /* End Of String, or '\0' */
789 add_constant("cellbits", 32, sGLOBAL, 0);
790 add_constant("cellmax", INT_MAX, sGLOBAL, 0);
791 add_constant("cellmin", INT_MIN, sGLOBAL, 0);
792 add_constant("charbits", charbits, sGLOBAL, 0);
793 add_constant("charmin", 0, sGLOBAL, 0);
794 add_constant("charmax", (charbits == 16) ? 0xffff : 0xff, sGLOBAL, 0);
796 add_constant("__Small", VERSION_INT, sGLOBAL, 0);
799 if ((sc_debug & (sCHKBOUNDS | sSYMBOLIC)) == (sCHKBOUNDS | sSYMBOLIC))
801 else if ((sc_debug & sCHKBOUNDS) == sCHKBOUNDS)
803 add_constant("debug", debug, sGLOBAL, 0);
806 /* parse - process all input text
808 * At this level, only static declarations and function definitions
814 int tok, tag, fconst, fstock, fstatic;
820 /* first try whether a declaration possibly is native or public */
821 tok = lex(&val, &str); /* read in (new) token */
828 fconst = matchtoken(tCONST);
829 declglb(NULL, 0, FALSE, FALSE, FALSE, fconst);
832 /* This can be a static function or a static global variable;
833 * we know which of the two as soon as we have parsed up to the
834 * point where an opening paranthesis of a function would be
835 * expected. To back out after deciding it was a declaration of
836 * a static variable after all, we have to store the symbol name
839 fstock = matchtoken(tSTOCK);
840 fconst = matchtoken(tCONST);
841 tag = sc_addtag(NULL);
842 tok = lex(&val, &str);
843 if (tok == tNATIVE || tok == tPUBLIC)
845 error(42); /* invalid combination of class specifiers */
848 declfuncvar(tok, str, tag, FALSE, TRUE, fstock, fconst);
857 /* This can be a public function or a public variable;
858 * see the comment above (for static functions/variables)
861 fconst = matchtoken(tCONST);
862 tag = sc_addtag(NULL);
863 tok = lex(&val, &str);
864 if (tok == tNATIVE || tok == tSTOCK || tok == tSTATIC)
866 error(42); /* invalid combination of class specifiers */
869 declfuncvar(tok, str, tag, TRUE, FALSE, FALSE, fconst);
872 /* This can be a stock function or a stock *global) variable;
873 * see the comment above (for static functions/variables) for
876 fstatic = matchtoken(tSTATIC);
877 fconst = matchtoken(tCONST);
878 tag = sc_addtag(NULL);
879 tok = lex(&val, &str);
880 if (tok == tNATIVE || tok == tPUBLIC)
882 error(42); /* invalid combination of class specifiers */
885 declfuncvar(tok, str, tag, FALSE, fstatic, TRUE, fconst);
891 if (!newfunc(NULL, -1, FALSE, FALSE, FALSE))
893 error(10); /* illegal function or declaration */
894 lexclr(TRUE); /* drop the rest of the line */
898 funcstub(TRUE); /* create a dummy function */
904 error(54); /* unmatched closing brace */
907 error(55); /* start of function body without function header */
912 error(10); /* illegal function or declaration */
913 lexclr(TRUE); /* drop the rest of the line */
921 * Dump the literal pool (strings etc.)
923 * Global references: litidx (referred to only)
933 /* should be in the data segment */
936 j = 16; /* 16 values per line */
937 while (j && k < litidx)
939 outval(litq[k], FALSE);
943 if (j == 0 || k >= litidx)
944 stgwrite("\n"); /* force a newline after 10 dumps */
945 /* Note: stgwrite() buffers a line until it is complete. It recognizes
946 * the end of line as a sequence of "\n\0", so something like "\n\t"
947 * so should not be passed to stgwrite().
955 * Dump zero's for default initial values
971 stgwrite((i == 0 || count == 0) ? "\n" : " ");
972 if (i == 0 && count > 0)
978 aligndata(int numbytes)
980 if ((((glb_declared + litidx) * sizeof(cell)) % numbytes) != 0)
982 while ((((glb_declared + litidx) * sizeof(cell)) % numbytes) != 0)
989 declfuncvar(int tok, char *symname, int tag, int fpublic, int fstatic,
990 int fstock, int fconst)
992 char name[sNAMEMAX + 1];
994 if (tok != tSYMBOL && tok != tOPERATOR)
997 error(20, symname); /* invalid symbol name */
1000 if (tok == tOPERATOR)
1003 if (!newfunc(NULL, tag, fpublic, fstatic, fstock))
1004 error(10); /* illegal function or declaration */
1008 assert(strlen(symname) <= sNAMEMAX);
1009 strcpy(name, symname);
1010 if (fconst || !newfunc(name, tag, fpublic, fstatic, fstock))
1011 declglb(name, tag, fpublic, fstatic, fstock, fconst);
1012 /* if not a static function, try a static variable */
1016 /* declglb - declare global symbols
1018 * Declare a static (global) variable. Global variables are stored in
1021 * global references: glb_declared (altered)
1024 declglb(char *firstname, int firsttag, int fpublic, int fstatic,
1025 int stock, int fconst)
1027 int ident, tag, ispublic;
1028 int idxtag[sDIMEN_MAX];
1029 char name[sNAMEMAX + 1];
1030 cell val, size, cidx;
1032 int dim[sDIMEN_MAX];
1041 filenum = fcurrent; /* save file number at the start of the
1045 size = 1; /* single size (no array) */
1046 numdim = 0; /* no dimensions */
1048 if (firstname != NULL)
1050 assert(strlen(firstname) <= sNAMEMAX);
1051 strcpy(name, firstname); /* save symbol name */
1057 tag = sc_addtag(NULL);
1058 if (lex(&val, &str) != tSYMBOL) /* read in (new) token */
1059 error(20, str); /* invalid symbol name */
1060 assert(strlen(str) <= sNAMEMAX);
1061 strcpy(name, str); /* save symbol name */
1063 sym = findglb(name);
1065 sym = findconst(name);
1066 if (sym != NULL && (sym->usage & uDEFINE) != 0)
1067 error(21, name); /* symbol already defined */
1069 if (name[0] == PUBLIC_CHAR)
1071 ispublic = TRUE; /* implicitly public variable */
1072 if (stock || fstatic)
1073 error(42); /* invalid combination of class specifiers */
1075 while (matchtoken('['))
1078 if (numdim == sDIMEN_MAX)
1080 error(53); /* exceeding maximum number of dimensions */
1083 if (numdim > 0 && dim[numdim - 1] == 0)
1084 error(52); /* only last dimension may be variable length */
1085 size = needsub(&idxtag[numdim]); /* get size; size==0 for
1087 #if INT_MAX < LONG_MAX
1089 error(105); /* overflow, exceeding capacity */
1092 error(56, name); /* arrays cannot be public */
1093 dim[numdim++] = (int)size;
1095 /* if this variable is never used (which can be detected only in
1096 * the second stage), shut off code generation; make an exception
1097 * for public variables
1099 cidx = 0; /* only to avoid a compiler warning */
1100 if (sc_status == statWRITE && sym != NULL
1101 && (sym->usage & (uREAD | uWRITTEN | uPUBLIC)) == 0)
1103 sc_status = statSKIP;
1106 glbdecl = glb_declared;
1109 defsymbol(name, ident, sGLOBAL, sizeof(cell) * glb_declared, tag);
1110 begdseg(); /* real (initialized) data in data segment */
1111 assert(litidx == 0); /* literal queue should be empty */
1115 aligndata(sc_dataalign);
1116 dumplits(); /* dump the literal queue */
1117 sc_alignnext = FALSE;
1118 litidx = 0; /* global initial data is dumped, so restart at zero */
1120 initials(ident, tag, &size, dim, numdim); /* stores values in
1121 * the literal queue */
1124 dumplits(); /* dump the literal queue */
1125 dumpzero((int)size - litidx);
1128 { /* define only if not yet defined */
1130 addvariable(name, sizeof(cell) * glb_declared, ident, sGLOBAL,
1131 tag, dim, numdim, idxtag);
1134 { /* if declared but not yet defined, adjust the
1135 * variable's address */
1136 sym->addr = sizeof(cell) * glb_declared;
1137 sym->usage |= uDEFINE;
1140 sym->usage |= uPUBLIC;
1142 sym->usage |= uCONST;
1144 sym->usage |= uSTOCK;
1146 sym->fnumber = filenum;
1147 if (ident == iARRAY)
1148 for (level = 0; level < numdim; level++)
1149 symbolrange(level, dim[level]);
1150 if (sc_status == statSKIP)
1152 sc_status = statWRITE;
1154 assert(glb_declared == glbdecl);
1158 glb_declared += (int)size; /* add total number of cells */
1161 while (matchtoken(',')); /* enddo *//* more? */
1162 needtoken(tTERM); /* if not comma, must be semicolumn */
1165 /* declloc - declare local symbols
1167 * Declare local (automatic) variables. Since these variables are
1168 * relative to the STACK, there is no switch to the DATA segment.
1169 * These variables cannot be initialized either.
1171 * global references: declared (altered)
1172 * funcstatus (referred to only)
1175 declloc(int fstatic)
1178 int idxtag[sDIMEN_MAX];
1179 char name[sNAMEMAX + 1];
1183 value lval = { NULL, 0, 0, 0, 0, NULL };
1185 int dim[sDIMEN_MAX];
1189 fconst = matchtoken(tCONST);
1194 numdim = 0; /* no dimensions */
1195 tag = sc_addtag(NULL);
1196 if (lex(&val, &str) != tSYMBOL) /* read in (new) token */
1197 error(20, str); /* invalid symbol name */
1198 assert(strlen(str) <= sNAMEMAX);
1199 strcpy(name, str); /* save symbol name */
1200 if (name[0] == PUBLIC_CHAR)
1201 error(56, name); /* local variables cannot be public */
1202 /* Note: block locals may be named identical to locals at higher
1203 * compound blocks (as with standard C); so we must check (and add)
1204 * the "nesting level" of local variables to verify the
1205 * multi-definition of symbols.
1207 if ((sym = findloc(name)) != NULL && sym->compound == nestlevel)
1208 error(21, name); /* symbol already defined */
1209 /* Although valid, a local variable whose name is equal to that
1210 * of a global variable or to that of a local variable at a lower
1211 * level might indicate a bug.
1213 if (((sym = findloc(name)) != NULL && sym->compound != nestlevel)
1214 || findglb(name) != NULL)
1215 error(219, name); /* variable shadows another symbol */
1216 while (matchtoken('['))
1219 if (numdim == sDIMEN_MAX)
1221 error(53); /* exceeding maximum number of dimensions */
1224 if (numdim > 0 && dim[numdim - 1] == 0)
1225 error(52); /* only last dimension may be variable length */
1226 size = needsub(&idxtag[numdim]); /* get size; size==0 for "var[]" */
1227 #if INT_MAX < LONG_MAX
1229 error(105); /* overflow, exceeding capacity */
1231 dim[numdim++] = (int)size;
1233 if (ident == iARRAY || fstatic)
1237 aligndata(sc_dataalign);
1238 sc_alignnext = FALSE;
1240 cur_lit = litidx; /* save current index in the literal table */
1241 initials(ident, tag, &size, dim, numdim);
1243 return ident; /* error message already given */
1247 /* reserve memory (on the stack) for the variable */
1250 /* write zeros for uninitialized fields */
1251 while (litidx < cur_lit + size)
1254 addvariable(name, (cur_lit + glb_declared) * sizeof(cell),
1255 ident, sSTATIC, tag, dim, numdim, idxtag);
1256 defsymbol(name, ident, sSTATIC,
1257 (cur_lit + glb_declared) * sizeof(cell), tag);
1261 declared += (int)size; /* variables are put on stack,
1262 * adjust "declared" */
1264 addvariable(name, -declared * sizeof(cell), ident, sLOCAL, tag,
1265 dim, numdim, idxtag);
1266 defsymbol(name, ident, sLOCAL, -declared * sizeof(cell), tag);
1267 modstk(-(int)size * sizeof(cell));
1269 /* now that we have reserved memory for the variable, we can
1270 * proceed to initialize it */
1271 sym->compound = nestlevel; /* for multiple declaration/shadowing */
1273 sym->usage |= uCONST;
1274 if (ident == iARRAY)
1275 for (level = 0; level < numdim; level++)
1276 symbolrange(level, dim[level]);
1278 { /* static variables already initialized */
1279 if (ident == iVARIABLE)
1281 /* simple variable, also supports initialization */
1282 int ctag = tag; /* set to "tag" by default */
1283 int explicit_init = FALSE; /* is the variable explicitly
1285 if (matchtoken('='))
1287 doexpr(FALSE, FALSE, FALSE, FALSE, &ctag, TRUE);
1288 explicit_init = TRUE;
1292 const1(0); /* uninitialized variable, set to zero */
1294 /* now try to save the value (still in PRI) in the variable */
1296 lval.ident = iVARIABLE;
1299 check_userop(NULL, ctag, lval.tag, 2, NULL, &ctag);
1301 endexpr(TRUE); /* full expression ends after the store */
1302 if (!matchtag(tag, ctag, TRUE))
1303 error(213); /* tag mismatch */
1304 /* if the variable was not explicitly initialized, reset the
1305 * "uWRITTEN" flag that store() set */
1307 sym->usage &= ~uWRITTEN;
1312 if (litidx - cur_lit < size)
1313 fillarray(sym, size * sizeof(cell), 0);
1314 if (cur_lit < litidx)
1316 /* check whether the complete array is set to a single value;
1317 * if it is, more compact code can be generated */
1318 cell first = litq[cur_lit];
1321 for (i = cur_lit; i < litidx && litq[i] == first; i++)
1325 /* all values are the same */
1326 fillarray(sym, (litidx - cur_lit) * sizeof(cell),
1328 litidx = cur_lit; /* reset literal table */
1332 /* copy the literals to the array */
1333 const1((cur_lit + glb_declared) * sizeof(cell));
1334 copyarray(sym, (litidx - cur_lit) * sizeof(cell));
1340 while (matchtoken(',')); /* enddo *//* more? */
1341 needtoken(tTERM); /* if not comma, must be semicolumn */
1346 calc_arraysize(int dim[], int numdim, int cur)
1350 return dim[cur] + (dim[cur] * calc_arraysize(dim, numdim, cur + 1));
1355 * Initialize global objects and local arrays.
1356 * size==array cells (count), if 0 on input, the routine counts
1357 * the number of elements
1358 * tag==required tagname id (not the returned tag)
1360 * Global references: litidx (altered)
1363 initials(int ident, int tag, cell * size, int dim[], int numdim)
1366 int curlit = litidx;
1369 if (!matchtoken('='))
1371 if (ident == iARRAY && dim[numdim - 1] == 0)
1373 /* declared as "myvar[];" which is senseless (note: this *does* make
1374 * sense in the case of a iREFARRAY, which is a function parameter)
1376 error(9); /* array has zero length -> invalid size */
1380 /* initialize the indirection tables */
1382 #error Array algorithms for more than 2 dimensions are not implemented
1384 assert(numdim == 2);
1385 *size = calc_arraysize(dim, numdim, 0);
1386 for (d = 0; d < dim[0]; d++)
1387 stowlit((dim[0] + d * (dim[1] - 1)) * sizeof(cell));
1392 if (ident == iVARIABLE)
1396 if (!matchtag(tag, ctag, TRUE))
1397 error(213); /* tag mismatch */
1404 *size = initvector(ident, tag, dim[0], FALSE);
1410 /* The simple algorithm below only works for arrays with one or
1411 * two dimensions. This should be some recursive algorithm.
1413 if (dim[numdim - 1] != 0)
1414 /* set size to (known) full size */
1415 *size = calc_arraysize(dim, numdim, 0);
1416 /* dump indirection tables */
1417 for (d = 0; d < dim[0]; d++)
1419 /* now dump individual vectors */
1422 for (d = 0; d < dim[0]; d++)
1424 litq[curlit + d] = offs * sizeof(cell);
1425 dsize = initvector(ident, tag, dim[1], TRUE);
1429 if (matchtoken('{') || matchtoken(tSTRING))
1430 /* expect a '{' or a string */
1441 *size = litidx - curlit; /* number of elements defined */
1445 * Initialize a single dimensional array
1448 initvector(int ident, int tag, cell size, int fillzero)
1450 cell prev1 = 0, prev2 = 0;
1453 int curlit = litidx;
1455 assert(ident == iARRAY || ident == iREFARRAY);
1456 if (matchtoken('{'))
1460 if (matchtoken('}'))
1461 { /* to allow for trailing ',' after the initialization */
1465 if ((ellips = matchtoken(tELLIPS)) != 0)
1468 prev1 = init(ident, &ctag);
1469 if (!matchtag(tag, ctag, TRUE))
1470 error(213); /* tag mismatch */
1472 while (matchtoken(',')); /* do */
1478 if (!matchtag(tag, ctag, TRUE))
1479 error(213); /* tagname mismatch */
1481 /* fill up the literal queue with a series */
1485 ((litidx - curlit) == 1) ? (cell) 0 : prev1 - prev2;
1486 if (size == 0 || (litidx - curlit) == 0)
1487 error(41); /* invalid ellipsis, array size unknown */
1488 else if ((litidx - curlit) == (int)size)
1489 error(18); /* initialisation data exceeds declared size */
1490 while ((litidx - curlit) < (int)size)
1496 if (fillzero && size > 0)
1498 while ((litidx - curlit) < (int)size)
1503 size = litidx - curlit; /* number of elements defined */
1505 else if (litidx - curlit > (int)size)
1506 { /* e.g. "myvar[3]={1,2,3,4};" */
1507 error(18); /* initialisation data exceeds declared size */
1508 litidx = (int)size + curlit; /* avoid overflow in memory moves */
1515 * Evaluate one initializer.
1518 init(int ident, int *tag)
1522 if (matchtoken(tSTRING))
1524 /* lex() automatically stores strings in the literal table (and
1525 * increases "litidx") */
1526 if (ident == iVARIABLE)
1528 error(6); /* must be assigned to an array */
1529 litidx = 1; /* reset literal queue */
1533 else if (constexpr(&i, tag))
1535 stowlit(i); /* store expression result in literal table */
1542 * Get required array size
1550 if (matchtoken(']')) /* we've already seen "[" */
1551 return 0; /* null size (like "char msg[]") */
1552 constexpr(&val, tag); /* get value (must be constant expression) */
1555 error(9); /* negative array size is invalid; assumed zero */
1559 return val; /* return array size */
1562 /* decl_const - declare a single constant
1566 decl_const(int vclass)
1568 char constname[sNAMEMAX + 1];
1574 tag = sc_addtag(NULL);
1575 if (lex(&val, &str) != tSYMBOL) /* read in (new) token */
1576 error(20, str); /* invalid symbol name */
1577 symbolline = fline; /* save line where symbol was found */
1578 strcpy(constname, str); /* save symbol name */
1580 constexpr(&val, &exprtag); /* get value */
1582 /* add_constant() checks for duplicate definitions */
1583 if (!matchtag(tag, exprtag, FALSE))
1585 /* temporarily reset the line number to where the symbol was
1587 int orgfline = fline;
1590 error(213); /* tagname mismatch */
1593 add_constant(constname, val, vclass, tag);
1596 /* decl_enum - declare enumerated constants
1600 decl_enum(int vclass)
1602 char enumname[sNAMEMAX + 1], constname[sNAMEMAX + 1];
1603 cell val, value, size;
1605 int tok, tag, explicittag;
1606 cell increment, multiplier;
1608 /* get an explicit tag, if any (we need to remember whether an
1609 * explicit tag was passed, even if that explicit tag was "_:", so we
1610 * cannot call sc_addtag() here
1612 if (lex(&val, &str) == tLABEL)
1614 tag = sc_addtag(str);
1621 explicittag = FALSE;
1624 /* get optional enum name (also serves as a tag if no explicit
1626 if (lex(&val, &str) == tSYMBOL)
1627 { /* read in (new) token */
1628 strcpy(enumname, str); /* save enum name (last constant) */
1630 tag = sc_addtag(enumname);
1634 lexpush(); /* analyze again */
1638 /* get increment and multiplier */
1641 if (matchtoken('('))
1643 if (matchtoken(taADD))
1645 constexpr(&increment, NULL);
1647 else if (matchtoken(taMULT))
1649 constexpr(&multiplier, NULL);
1651 else if (matchtoken(taSHL))
1653 constexpr(&val, NULL);
1661 /* go through all constants */
1662 value = 0; /* default starting value */
1665 if (matchtoken('}'))
1666 { /* quick exit if '}' follows ',' */
1670 tok = lex(&val, &str); /* read in (new) token */
1671 if (tok != tSYMBOL && tok != tLABEL)
1672 error(20, str); /* invalid symbol name */
1673 strcpy(constname, str); /* save symbol name */
1674 size = increment; /* default increment of 'val' */
1675 if (tok == tLABEL || matchtoken(':'))
1676 constexpr(&size, NULL); /* get size */
1677 if (matchtoken('='))
1678 constexpr(&value, NULL); /* get value */
1679 /* add_constant() checks whether a variable (global or local) or
1680 * a constant with the same name already exists */
1681 add_constant(constname, value, vclass, tag);
1682 if (multiplier == 1)
1685 value *= size * multiplier;
1687 while (matchtoken(','));
1688 needtoken('}'); /* terminates the constant list */
1689 matchtoken(';'); /* eat an optional ; */
1691 /* set the enum name to the last value plus one */
1692 if (enumname[0] != '\0')
1693 add_constant(enumname, value, vclass, tag);
1697 * Finds a function in the global symbol table or creates a new entry.
1698 * It does some basic processing and error checking.
1701 fetchfunc(char *name, int tag)
1707 if ((sc_debug & sSYMBOLIC) != 0)
1709 offset += opcodes(1) + opargs(3) + nameincells(name);
1710 /* ^^^ The address for the symbol is the code address. But the
1711 * "symbol" instruction itself generates code. Therefore the
1712 * offset is pre-adjusted to the value it will have after the
1713 * symbol instruction.
1716 if ((sym = findglb(name)) != 0)
1717 { /* already in symbol table? */
1718 if (sym->ident != iFUNCTN)
1720 error(21, name); /* yes, but not as a function */
1721 return NULL; /* make sure the old symbol is not damaged */
1723 else if ((sym->usage & uDEFINE) != 0)
1725 error(21, name); /* yes, and it's already defined */
1727 else if ((sym->usage & uNATIVE) != 0)
1729 error(21, name); /* yes, and it is an native */
1731 assert(sym->vclass == sGLOBAL);
1732 if ((sym->usage & uDEFINE) == 0)
1734 /* as long as the function stays undefined, update the address
1742 /* don't set the "uDEFINE" flag; it may be a prototype */
1743 sym = addsym(name, offset, iFUNCTN, sGLOBAL, tag, 0);
1744 /* assume no arguments */
1745 sym->dim.arglist = (arginfo *) malloc(1 * sizeof(arginfo));
1746 sym->dim.arglist[0].ident = 0;
1747 /* set library ID to NULL (only for native functions) */
1753 /* This routine adds symbolic information for each argument.
1760 /* At this point, no local variables have been declared. All
1761 * local symbols are function arguments.
1766 assert(sym->ident != iLABEL);
1767 assert(sym->vclass == sLOCAL);
1768 defsymbol(sym->name, sym->ident, sLOCAL, sym->addr, sym->tag);
1769 if (sym->ident == iREFARRAY)
1775 symbolrange(sub->dim.array.level, sub->dim.array.length);
1776 sub = finddepend(sub);
1784 operatorname(char *name)
1790 assert(name != NULL);
1792 /* check the operator */
1793 opertok = lex(&val, &str);
1806 name[0] = (char)opertok;
1829 error(61); /* operator cannot be redefined
1830 * (or bad operator name) */
1838 operatoradjust(int opertok, symbol * sym, char *opername, int resulttag)
1840 int tags[2] = { 0, 0 };
1843 char tmpname[sNAMEMAX + 1];
1849 /* count arguments and save (first two) tags */
1850 while (arg = &sym->dim.arglist[count], arg->ident != 0)
1854 if (arg->numtags > 1)
1855 error(65, count + 1); /* function argument may only have
1857 else if (arg->numtags == 1)
1858 tags[count] = arg->tags[0];
1860 if (opertok == '~' && count == 0)
1862 if (arg->ident != iREFARRAY)
1863 error(73, arg->name); /* must be an array argument */
1867 if (arg->ident != iVARIABLE)
1868 error(66, arg->name); /* must be non-reference argument */
1870 if (arg->hasdefault)
1871 error(59, arg->name); /* arguments of an operator may not
1872 * have a default value */
1876 /* for '!', '++' and '--', count must be 1
1877 * for '-', count may be 1 or 2
1878 * for '=', count must be 1, and the resulttag is also important
1879 * for all other (binary) operators and the special '~'
1880 * operator, count must be 2
1889 error(62); /* number or placement of the operands does
1890 * not fit the operator */
1893 if (count != 1 && count != 2)
1894 error(62); /* number or placement of the operands does
1895 * not fit the operator */
1899 error(62); /* number or placement of the operands does
1900 * not fit the operator */
1904 && ((opertok != '=' && tags[1] == 0) || (opertok == '=' && resulttag == 0)))
1905 error(64); /* cannot change predefined operators */
1907 /* change the operator name */
1908 assert(opername[0] != '\0');
1909 operator_symname(tmpname, opername, tags[0], tags[1], count, resulttag);
1910 if ((oldsym = findglb(tmpname)) != NULL)
1914 if ((oldsym->usage & uDEFINE) != 0)
1916 char errname[2 * sNAMEMAX + 16];
1918 funcdisplayname(errname, tmpname);
1919 error(21, errname); /* symbol already defined */
1921 sym->usage |= oldsym->usage; /* copy flags from the previous
1923 for (i = 0; i < oldsym->numrefers; i++)
1924 if (oldsym->refer[i] != NULL)
1925 refer_symbol(sym, oldsym->refer[i]);
1926 delete_symbol(&glbtab, oldsym);
1928 if ((sc_debug & sSYMBOLIC) != 0)
1929 sym->addr += nameincells(tmpname) - nameincells(sym->name);
1930 strcpy(sym->name, tmpname);
1931 sym->hash = namehash(sym->name); /* calculate new hash */
1933 /* operators should return a value, except the '~' operator */
1935 sym->usage |= uRETVALUE;
1941 check_operatortag(int opertok, int resulttag, char *opername)
1943 assert(opername != NULL && opername[0] != '\0');
1953 if (resulttag != sc_addtag("bool"))
1955 error(63, opername, "bool:"); /* operator X requires
1956 * a "bool:" result tag */
1963 error(63, opername, "_:"); /* operator "~" requires
1964 * a "_:" result tag */
1973 tag2str(char *dest, int tag)
1977 sprintf(dest, "0%x", tag);
1978 return isdigit(dest[1]) ? &dest[1] : dest;
1982 operator_symname(char *symname, char *opername, int tag1, int tag2,
1983 int numtags, int resulttag)
1985 char tagstr1[10], tagstr2[10];
1988 assert(numtags >= 1 && numtags <= 2);
1989 opertok = (opername[1] == '\0') ? opername[0] : 0;
1991 sprintf(symname, "%s%s%s", tag2str(tagstr1, resulttag), opername,
1992 tag2str(tagstr2, tag1));
1993 else if (numtags == 1 || opertok == '~')
1994 sprintf(symname, "%s%s", opername, tag2str(tagstr1, tag1));
1996 sprintf(symname, "%s%s%s", tag2str(tagstr1, tag1), opername,
1997 tag2str(tagstr2, tag2));
2002 parse_funcname(char *fname, int *tag1, int *tag2, char *opname)
2007 /* tags are only positive, so if the function name starts with a '-',
2008 * the operator is an unary '-' or '--' operator.
2018 *tag1 = (int)strtol(fname, &ptr, 16);
2019 unary = ptr == fname; /* unary operator if it doesn't start
2020 * with a tag name */
2022 assert(!unary || *tag1 == 0);
2023 assert(*ptr != '\0');
2024 for (name = opname; !isdigit(*ptr);)
2027 *tag2 = (int)strtol(ptr, NULL, 16);
2032 funcdisplayname(char *dest, char *funcname)
2036 constvalue *tagsym[2];
2039 if (isalpha(*funcname) || *funcname == '_' || *funcname == PUBLIC_CHAR
2040 || *funcname == '\0')
2042 if (dest != funcname)
2043 strcpy(dest, funcname);
2047 unary = parse_funcname(funcname, &tags[0], &tags[1], opname);
2048 tagsym[1] = find_constval_byval(&tagname_tab, tags[1]);
2049 assert(tagsym[1] != NULL);
2052 sprintf(dest, "operator%s(%s:)", opname, tagsym[1]->name);
2056 tagsym[0] = find_constval_byval(&tagname_tab, tags[0]);
2057 /* special case: the assignment operator has the return value
2059 if (opname[0] == '=' && opname[1] == '\0')
2060 sprintf(dest, "%s:operator%s(%s:)", tagsym[0]->name, opname,
2063 sprintf(dest, "operator%s(%s:,%s:)", opname, tagsym[0]->name,
2070 funcstub(int native)
2075 char symbolname[sNAMEMAX + 1];
2081 litidx = 0; /* clear the literal pool */
2083 tag = sc_addtag(NULL);
2084 tok = lex(&val, &str);
2087 if (tok == tPUBLIC || tok == tSTOCK || tok == tSTATIC ||
2088 (tok == tSYMBOL && *str == PUBLIC_CHAR))
2089 error(42); /* invalid combination of class specifiers */
2093 if (tok == tPUBLIC || tok == tSTATIC)
2094 tok = lex(&val, &str);
2096 if (tok == tOPERATOR)
2098 opertok = operatorname(symbolname);
2100 return; /* error message already given */
2101 check_operatortag(opertok, tag, symbolname);
2105 if (tok != tSYMBOL && freading)
2107 error(10); /* illegal function or declaration */
2110 strcpy(symbolname, str);
2112 needtoken('('); /* only functions may be native/forward */
2114 sym = fetchfunc(symbolname, tag); /* get a pointer to the
2120 sym->usage = uNATIVE | uRETVALUE | uDEFINE;
2121 sym->x.lib = curlibrary;
2125 /* "declargs()" found the ")" */
2126 if (!operatoradjust(opertok, sym, symbolname, tag))
2127 sym->usage &= ~uDEFINE;
2128 /* for a native operator, also need to specify an "exported"
2129 * function name; for a native function, this is optional
2136 lexpush(); /* push back, for matchtoken() to retrieve again */
2138 if (matchtoken('='))
2140 /* allow number or symbol */
2141 if (matchtoken(tSYMBOL))
2143 tokeninfo(&val, &str);
2144 if (strlen(str) > sEXPMAX)
2146 error(220, str, sEXPMAX);
2147 str[sEXPMAX] = '\0';
2149 insert_alias(sym->name, str);
2153 constexpr(&val, NULL);
2156 * ?? Must mark this address, so that it won't be generated again
2157 * and it won't be written to the output file. At the moment,
2158 * I have assumed that this syntax is only valid if val < 0.
2159 * To properly mix "normal" native functions and indexed native
2160 * functions, one should use negative indices anyway.
2161 * Special code for a negative index in sym->addr exists in
2162 * SC4.C (ffcall()) and in SC6.C (the loops for counting the
2163 * number of native variables and for writing them).
2170 litidx = 0; /* clear the literal pool */
2171 /* clear local variables queue */
2172 delete_symbols(&loctab, 0, TRUE, TRUE);
2175 /* newfunc - begin a function
2177 * This routine is called from "parse" and tries to make a function
2178 * out of the following text
2180 * Global references: funcstatus,lastst,litidx
2183 * declared (altered)
2184 * glb_declared (altered)
2185 * sc_alignnext (altered)
2188 newfunc(char *firstname, int firsttag, int fpublic, int fstatic, int stock)
2191 int argcnt, tok, tag, funcline;
2192 int opertok, opererror;
2193 char symbolname[sNAMEMAX + 1];
2195 cell val, cidx, glbdecl;
2198 litidx = 0; /* clear the literal pool ??? */
2200 lastst = 0; /* no statement yet */
2201 cidx = 0; /* just to avoid compiler warnings */
2203 filenum = fcurrent; /* save file number at start of declaration */
2205 if (firstname != NULL)
2207 assert(strlen(firstname) <= sNAMEMAX);
2208 strcpy(symbolname, firstname); /* save symbol name */
2213 tag = (firsttag >= 0) ? firsttag : sc_addtag(NULL);
2214 tok = lex(&val, &str);
2216 if (tok == tNATIVE || (tok == tPUBLIC && stock))
2217 error(42); /* invalid combination of class specifiers */
2218 if (tok == tOPERATOR)
2220 opertok = operatorname(symbolname);
2222 return TRUE; /* error message already given */
2223 check_operatortag(opertok, tag, symbolname);
2227 if (tok != tSYMBOL && freading)
2229 error(20, str); /* invalid symbol name */
2232 assert(strlen(str) <= sNAMEMAX);
2233 strcpy(symbolname, str);
2236 /* check whether this is a function or a variable declaration */
2237 if (!matchtoken('('))
2239 /* so it is a function, proceed */
2240 funcline = fline; /* save line at which the function is defined */
2241 if (symbolname[0] == PUBLIC_CHAR)
2243 fpublic = TRUE; /* implicitly public function */
2245 error(42); /* invalid combination of class specifiers */
2247 sym = fetchfunc(symbolname, tag); /* get a pointer to the
2252 sym->usage |= uPUBLIC;
2254 sym->fnumber = filenum;
2255 /* declare all arguments */
2256 argcnt = declargs(sym);
2257 opererror = !operatoradjust(opertok, sym, symbolname, tag);
2258 if (strcmp(symbolname, uMAINFUNC) == 0)
2261 error(5); /* "main()" function may not have any arguments */
2262 sym->usage |= uREAD; /* "main()" is the program's entry point:
2265 /* "declargs()" found the ")"; if a ";" appears after this, it was a
2267 if (matchtoken(';'))
2269 if (!sc_needsemicolon)
2270 error(218); /* old style prototypes used with optional
2272 delete_symbols(&loctab, 0, TRUE, TRUE); /* prototype is done;
2273 * forget everything */
2276 /* so it is not a prototype, proceed */
2277 /* if this is a function that is not referred to (this can only be
2278 * detected in the second stage), shut code generation off */
2279 if (sc_status == statWRITE && (sym->usage & uREAD) == 0)
2281 sc_status = statSKIP;
2283 glbdecl = glb_declared;
2286 sym->usage |= uDEFINE; /* set the definition flag */
2288 sym->usage |= uREAD; /* public functions are always "used" */
2290 sym->usage |= uSTOCK;
2291 if (opertok != 0 && opererror)
2292 sym->usage &= ~uDEFINE;
2293 defsymbol(sym->name, iFUNCTN, sGLOBAL,
2294 code_idx + opcodes(1) + opargs(3) + nameincells(sym->name), tag);
2295 /* ^^^ The address for the symbol is the code address. But the
2296 * "symbol" instruction itself generates code. Therefore the
2297 * offset is pre-adjusted to the value it will have after the
2298 * symbol instruction.
2300 startfunc(sym->name); /* creates stack frame */
2301 if ((sc_debug & sSYMBOLIC) != 0)
2302 setline(funcline, fcurrent);
2305 alignframe(sc_dataalign);
2306 sc_alignnext = FALSE;
2308 declared = 0; /* number of local cells */
2309 rettype = (sym->usage & uRETVALUE); /* set "return type" variable */
2311 define_args(); /* add the symbolic info for the function arguments */
2312 statement(NULL, FALSE);
2313 if ((rettype & uRETVALUE) != 0)
2314 sym->usage |= uRETVALUE;
2317 /* This happens only in a very special (and useless) case, where a
2318 * function has only a single statement in its body (no compound
2319 * block) and that statement declares a new variable
2321 modstk((int)declared * sizeof(cell)); /* remove all local
2325 if ((lastst != tRETURN) && (lastst != tGOTO))
2329 if ((sym->usage & uRETVALUE) != 0)
2331 char symname[2 * sNAMEMAX + 16]; /* allow space for user
2332 * defined operators */
2333 funcdisplayname(symname, sym->name);
2334 error(209, symname); /* function should return a value */
2339 { /* if there are literals defined */
2340 glb_declared += litidx;
2341 begdseg(); /* flip to DATA segment */
2342 dumplits(); /* dump literal strings */
2345 testsymbols(&loctab, 0, TRUE, TRUE); /* test for unused arguments
2347 delete_symbols(&loctab, 0, TRUE, TRUE); /* clear local variables
2349 assert(loctab.next == NULL);
2351 if (sc_status == statSKIP)
2353 sc_status = statWRITE;
2355 glb_declared = glbdecl;
2361 argcompare(arginfo * a1, arginfo * a2)
2365 result = strcmp(a1->name, a2->name) == 0;
2367 result = a1->ident == a2->ident;
2369 result = a1->usage == a2->usage;
2371 result = a1->numtags == a2->numtags;
2376 for (i = 0; i < a1->numtags && result; i++)
2377 result = a1->tags[i] == a2->tags[i];
2380 result = a1->hasdefault == a2->hasdefault;
2383 if (a1->ident == iREFARRAY)
2386 result = a1->defvalue.array.size == a2->defvalue.array.size;
2389 a1->defvalue.array.arraysize == a2->defvalue.array.arraysize;
2390 /* also check the dimensions of both arrays */
2392 result = a1->numdim == a2->numdim;
2393 for (level = 0; result && level < a1->numdim; level++)
2394 result = a1->dim[level] == a2->dim[level];
2395 /* ??? should also check contents of the default array
2396 * (these troubles go away in a 2-pass compiler that forbids
2397 * double declarations, but Small currently does not forbid them)
2404 if ((a1->hasdefault & uSIZEOF) != 0
2405 || (a1->hasdefault & uTAGOF) != 0)
2406 result = a1->hasdefault == a2->hasdefault
2407 && strcmp(a1->defvalue.size.symname,
2408 a2->defvalue.size.symname) == 0
2409 && a1->defvalue.size.level == a2->defvalue.size.level;
2411 result = a1->defvalue.val == a2->defvalue.val;
2415 result = a1->defvalue_tag == a2->defvalue_tag;
2422 * This routine adds an entry in the local symbol table for each
2423 * argument found in the argument list.
2424 * It returns the number of arguments.
2427 declargs(symbol * sym)
2431 int argcnt, oldargcnt, tok, tags[MAXTAGS], numtags;
2433 arginfo arg, *arglist;
2434 char name[sNAMEMAX + 1];
2435 int ident, fpublic, fconst;
2438 /* if the function is already defined earlier, get the number of
2439 * arguments of the existing definition
2442 if ((sym->usage & uPROTOTYPED) != 0)
2443 while (sym->dim.arglist[oldargcnt].ident != 0)
2445 argcnt = 0; /* zero aruments up to now */
2449 fpublic = (sym->usage & uPUBLIC) != 0;
2450 /* the '(' parantheses has already been parsed */
2451 if (!matchtoken(')'))
2454 { /* there are arguments; process them */
2455 /* any legal name increases argument count (and stack offset) */
2456 tok = lex(&val, &ptr);
2463 if (ident != iVARIABLE || numtags > 0)
2464 error(1, "-identifier-", "&");
2468 if (ident != iVARIABLE || numtags > 0)
2469 error(1, "-identifier-", "const");
2474 error(1, "-identifier-", "-tagname-");
2475 tags[0] = sc_addtag(ptr);
2480 error(1, "-identifier-", "-tagname-");
2482 while (numtags < MAXTAGS)
2484 if (!matchtoken('_') && !needtoken(tSYMBOL))
2486 tokeninfo(&val, &ptr);
2487 tags[numtags++] = sc_addtag(ptr);
2488 if (matchtoken('}'))
2493 tok = tLABEL; /* for outer loop:
2494 * flag that we have seen a tagname */
2497 if (argcnt >= sMAXARGS)
2498 error(45); /* too many function arguments */
2499 strcpy(name, ptr); /* save symbol name */
2500 if (name[0] == PUBLIC_CHAR)
2501 error(56, name); /* function arguments cannot be public */
2503 tags[numtags++] = 0; /* default tag */
2505 * base + 0*sizeof(cell) == previous "base"
2506 * base + 1*sizeof(cell) == function return address
2507 * base + 2*sizeof(cell) == number of arguments
2508 * base + 3*sizeof(cell) == first argument of the function
2509 * So the offset of each argument is:
2510 * "(argcnt+3) * sizeof(cell)".
2512 doarg(name, ident, (argcnt + 3) * sizeof(cell), tags, numtags,
2513 fpublic, fconst, &arg);
2514 if (fpublic && arg.hasdefault)
2515 error(59, name); /* arguments of a public function may not
2516 * have a default value */
2517 if ((sym->usage & uPROTOTYPED) == 0)
2519 /* redimension the argument list, add the entry */
2521 (arginfo *) realloc(sym->dim.arglist,
2522 (argcnt + 2) * sizeof(arginfo));
2523 if (sym->dim.arglist == 0)
2524 error(103); /* insufficient memory */
2525 sym->dim.arglist[argcnt] = arg;
2526 sym->dim.arglist[argcnt + 1].ident = 0; /* keep the list
2531 /* check the argument with the earlier definition */
2532 if (argcnt > oldargcnt
2533 || !argcompare(&sym->dim.arglist[argcnt], &arg))
2534 error(25); /* function definition does not match prototype */
2535 /* may need to free default array argument and the tag list */
2536 if (arg.ident == iREFARRAY && arg.hasdefault)
2537 free(arg.defvalue.array.data);
2538 else if (arg.ident == iVARIABLE
2539 && ((arg.hasdefault & uSIZEOF) != 0
2540 || (arg.hasdefault & uTAGOF) != 0))
2541 free(arg.defvalue.size.symname);
2550 if (ident != iVARIABLE)
2551 error(10); /* illegal function or declaration */
2553 tags[numtags++] = 0; /* default tag */
2554 if ((sym->usage & uPROTOTYPED) == 0)
2556 /* redimension the argument list, add the entry iVARARGS */
2558 (arginfo *) realloc(sym->dim.arglist,
2559 (argcnt + 2) * sizeof(arginfo));
2560 if (sym->dim.arglist == 0)
2561 error(103); /* insufficient memory */
2562 sym->dim.arglist[argcnt + 1].ident = 0; /* keep the list
2564 sym->dim.arglist[argcnt].ident = iVARARGS;
2565 sym->dim.arglist[argcnt].hasdefault = FALSE;
2566 sym->dim.arglist[argcnt].defvalue.val = 0;
2567 sym->dim.arglist[argcnt].defvalue_tag = 0;
2568 sym->dim.arglist[argcnt].numtags = numtags;
2569 sym->dim.arglist[argcnt].tags =
2570 (int *)malloc(numtags * sizeof tags[0]);
2571 if (sym->dim.arglist[argcnt].tags == NULL)
2572 error(103); /* insufficient memory */
2573 memcpy(sym->dim.arglist[argcnt].tags, tags,
2574 numtags * sizeof tags[0]);
2578 if (argcnt > oldargcnt
2579 || sym->dim.arglist[argcnt].ident != iVARARGS)
2580 error(25); /* function definition does not match prototype */
2585 error(10); /* illegal function or declaration */
2588 while (tok == '&' || tok == tLABEL || tok == tCONST || (tok != tELLIPS && matchtoken(','))); /* more? */
2589 /* if the next token is not ",", it should be ")" */
2592 /* resolve any "sizeof" arguments (now that all arguments are known) */
2593 assert(sym->dim.arglist != NULL);
2594 arglist = sym->dim.arglist;
2595 for (idx = 0; idx < argcnt && arglist[idx].ident != 0; idx++)
2597 if ((arglist[idx].hasdefault & uSIZEOF) != 0
2598 || (arglist[idx].hasdefault & uTAGOF) != 0)
2602 /* Find the argument with the name mentioned after the "sizeof".
2603 * Note that we cannot use findloc here because we need the
2604 * arginfo struct, not the symbol.
2606 ptr = arglist[idx].defvalue.size.symname;
2608 altidx < argcnt && strcmp(ptr, arglist[altidx].name) != 0;
2611 if (altidx >= argcnt)
2613 error(17, ptr); /* undefined symbol */
2617 /* check the level against the number of dimensions */
2618 /* the level must be zero for "tagof" values */
2619 assert(arglist[idx].defvalue.size.level == 0
2620 || (arglist[idx].hasdefault & uSIZEOF) != 0);
2621 if (arglist[idx].defvalue.size.level > 0
2622 && arglist[idx].defvalue.size.level >=
2623 arglist[altidx].numdim)
2624 error(28); /* invalid subscript */
2625 if (arglist[altidx].ident != iREFARRAY)
2627 assert(arglist[altidx].ident == iVARIABLE
2628 || arglist[altidx].ident == iREFERENCE);
2629 error(223, ptr); /* redundant sizeof */
2635 sym->usage |= uPROTOTYPED;
2636 errorset(sRESET); /* reset error flag (clear the "panic mode") */
2640 /* doarg - declare one argument type
2642 * this routine is called from "declargs()" and adds an entry in the
2643 * local symbol table for one argument. "fpublic" indicates whether
2644 * the function for this argument list is public.
2645 * The arguments themselves are never public.
2648 doarg(char *name, int ident, int offset, int tags[], int numtags,
2649 int fpublic, int fconst, arginfo * arg)
2653 int idxtag[sDIMEN_MAX];
2655 strcpy(arg->name, name);
2656 arg->hasdefault = FALSE; /* preset (most common case) */
2657 arg->defvalue.val = 0; /* clear */
2658 arg->defvalue_tag = 0;
2660 if (matchtoken('['))
2662 if (ident == iREFERENCE)
2663 error(67, name); /*illegal declaration ("&name[]" is unsupported) */
2666 if (arg->numdim == sDIMEN_MAX)
2668 error(53); /* exceeding maximum number of dimensions */
2671 /* there is no check for non-zero major dimensions here, only if
2672 * the array parameter has a default value, we enforce that all
2673 * array dimensions, except the last, are non-zero
2675 size = needsub(&idxtag[arg->numdim]); /* may be zero here,
2676 *it is a pointer anyway */
2677 #if INT_MAX < LONG_MAX
2679 error(105); /* overflow, exceeding capacity */
2681 arg->dim[arg->numdim] = (int)size;
2684 while (matchtoken('['));
2685 ident = iREFARRAY; /* "reference to array" (is a pointer) */
2686 if (matchtoken('='))
2690 lexpush(); /* initials() needs the "=" token again */
2691 assert(numtags > 0);
2692 /* for the moment, when a default value is given for the array,
2693 * all dimension sizes, except the last, must be non-zero
2694 * (function initials() requires to know the major dimensions)
2696 for (level = 0; level < arg->numdim - 1; level++)
2697 if (arg->dim[level] == 0)
2698 error(52); /* only last dimension may be variable length */
2699 initials(ident, tags[0], &size, arg->dim, arg->numdim);
2700 assert(size >= litidx);
2701 /* allocate memory to hold the initial values */
2702 arg->defvalue.array.data = (cell *) malloc(litidx * sizeof(cell));
2703 if (arg->defvalue.array.data != NULL)
2707 memcpy(arg->defvalue.array.data, litq, litidx * sizeof(cell));
2708 arg->hasdefault = TRUE; /* argument has default value */
2709 arg->defvalue.array.size = litidx;
2710 arg->defvalue.array.addr = -1;
2711 /* calulate size to reserve on the heap */
2712 arg->defvalue.array.arraysize = 1;
2713 for (i = 0; i < arg->numdim; i++)
2714 arg->defvalue.array.arraysize *= arg->dim[i];
2715 if (arg->defvalue.array.arraysize < arg->defvalue.array.size)
2716 arg->defvalue.array.arraysize = arg->defvalue.array.size;
2718 litidx = 0; /* reset */
2723 if (matchtoken('='))
2725 unsigned char size_tag_token;
2727 assert(ident == iVARIABLE || ident == iREFERENCE);
2728 arg->hasdefault = TRUE; /* argument has a default value */
2730 (unsigned char)(matchtoken(tSIZEOF) ? uSIZEOF : 0);
2731 if (size_tag_token == 0)
2733 (unsigned char)(matchtoken(tTAGOF) ? uTAGOF : 0);
2734 if (size_tag_token != 0)
2738 if (ident == iREFERENCE)
2739 error(66, name); /* argument may not be a reference */
2741 while (matchtoken('('))
2743 if (needtoken(tSYMBOL))
2745 /* save the name of the argument whose size id to take */
2749 tokeninfo(&val, &name);
2750 if ((arg->defvalue.size.symname =
2751 strdup(name)) == NULL)
2752 error(103); /* insufficient memory */
2753 arg->defvalue.size.level = 0;
2754 if (size_tag_token == uSIZEOF)
2756 while (matchtoken('['))
2758 arg->defvalue.size.level += (short)1;
2762 if (ident == iVARIABLE) /* make sure we set this only if
2763 * not a reference */
2764 arg->hasdefault |= size_tag_token; /* uSIZEOF or uTAGOF */
2766 while (paranthese--)
2771 constexpr(&arg->defvalue.val, &arg->defvalue_tag);
2772 assert(numtags > 0);
2773 if (!matchtag(tags[0], arg->defvalue_tag, TRUE))
2774 error(213); /* tagname mismatch */
2778 arg->ident = (char)ident;
2779 arg->usage = (char)(fconst ? uCONST : 0);
2780 arg->numtags = numtags;
2781 arg->tags = (int *)malloc(numtags * sizeof tags[0]);
2782 if (arg->tags == NULL)
2783 error(103); /* insufficient memory */
2784 memcpy(arg->tags, tags, numtags * sizeof tags[0]);
2785 argsym = findloc(name);
2788 error(21, name); /* symbol already defined */
2792 if ((argsym = findglb(name)) != NULL && argsym->ident != iFUNCTN)
2793 error(219, name); /* variable shadows another symbol */
2794 /* add details of type and address */
2795 assert(numtags > 0);
2796 argsym = addvariable(name, offset, ident, sLOCAL, tags[0],
2797 arg->dim, arg->numdim, idxtag);
2798 argsym->compound = 0;
2799 if (ident == iREFERENCE)
2800 argsym->usage |= uREAD; /* because references are passed back */
2802 argsym->usage |= uREAD; /* arguments of public functions
2803 * are always "used" */
2805 argsym->usage |= uCONST;
2810 count_referrers(symbol * entry)
2815 for (i = 0; i < entry->numrefers; i++)
2816 if (entry->refer[i] != NULL)
2821 /* Every symbol has a referrer list, that contains the functions that
2822 * use the symbol. Now, if function "apple" is accessed by functions
2823 * "banana" and "citron", but neither function "banana" nor "citron" are
2824 * used by anyone else, then, by inference, function "apple" is not used
2827 reduce_referrers(symbol * root)
2835 for (sym = root->next; sym != NULL; sym = sym->next)
2837 if (sym->parent != NULL)
2838 continue; /* hierarchical data type */
2839 if (sym->ident == iFUNCTN
2840 && (sym->usage & uNATIVE) == 0
2841 && (sym->usage & uPUBLIC) == 0
2842 && strcmp(sym->name, uMAINFUNC) != 0
2843 && count_referrers(sym) == 0)
2845 sym->usage &= ~(uREAD | uWRITTEN); /* erase usage bits if
2846 * there is no referrer */
2847 /* find all symbols that are referred by this symbol */
2848 for (ref = root->next; ref != NULL; ref = ref->next)
2850 if (ref->parent != NULL)
2851 continue; /* hierarchical data type */
2852 assert(ref->refer != NULL);
2853 for (i = 0; i < ref->numrefers && ref->refer[i] != sym;
2856 if (i < ref->numrefers)
2858 assert(ref->refer[i] == sym);
2859 ref->refer[i] = NULL;
2864 else if ((sym->ident == iVARIABLE || sym->ident == iARRAY)
2865 && (sym->usage & uPUBLIC) == 0
2866 && sym->parent == NULL && count_referrers(sym) == 0)
2868 sym->usage &= ~(uREAD | uWRITTEN); /* erase usage bits if
2869 * there is no referrer */
2872 /* after removing a symbol, check whether more can be removed */
2874 while (restart > 0);
2877 /* testsymbols - test for unused local or global variables
2879 * "Public" functions are excluded from the check, since these
2880 * may be exported to other object modules.
2881 * Labels are excluded from the check if the argument 'testlabs'
2882 * is 0. Thus, labels are not tested until the end of the function.
2883 * Constants may also be excluded (convenient for global constants).
2885 * When the nesting level drops below "level", the check stops.
2887 * The function returns whether there is an "entry" point for the file.
2888 * This flag will only be 1 when browsing the global symbol table.
2891 testsymbols(symbol * root, int level, int testlabs, int testconst)
2893 char symname[2 * sNAMEMAX + 16];
2896 symbol *sym = root->next;
2898 while (sym != NULL && sym->compound >= level)
2905 if ((sym->usage & uDEFINE) == 0)
2906 error(19, sym->name); /* not a label: ... */
2907 else if ((sym->usage & uREAD) == 0)
2908 error(203, sym->name); /* symbol isn't used: ... */
2912 if ((sym->usage & (uDEFINE | uREAD | uNATIVE | uSTOCK)) == uDEFINE)
2914 funcdisplayname(symname, sym->name);
2915 if (symname[0] != '\0')
2916 error(203, symname); /* symbol isn't used ...
2917 * (and not native/stock) */
2919 if ((sym->usage & uPUBLIC) != 0
2920 || strcmp(sym->name, uMAINFUNC) == 0)
2921 entry = TRUE; /* there is an entry point */
2924 if (testconst && (sym->usage & uREAD) == 0)
2925 error(203, sym->name); /* symbol isn't used: ... */
2929 if (sym->parent != NULL)
2930 break; /* hierarchical data type */
2931 if ((sym->usage & (uWRITTEN | uREAD | uSTOCK | uPUBLIC)) == 0)
2932 error(203, sym->name); /* symbol isn't used (and not stock
2934 else if ((sym->usage & (uREAD | uSTOCK | uPUBLIC)) == 0)
2935 error(204, sym->name); /* value assigned to symbol is
2937 #if 0 /*// ??? not sure whether it is a good idea to
2938 * force people use "const" */
2939 else if ((sym->usage & (uWRITTEN | uPUBLIC | uCONST)) == 0
2940 && sym->ident == iREFARRAY)
2941 error(214, sym->name); /* make array argument "const" */
2951 calc_array_datasize(symbol * sym, cell * offset)
2955 assert(sym != NULL);
2956 assert(sym->ident == iARRAY || sym->ident == iREFARRAY);
2957 length = sym->dim.array.length;
2958 if (sym->dim.array.level > 0)
2961 calc_array_datasize(finddepend(sym), offset);
2963 *offset = length * (*offset + sizeof(cell));
2965 length *= length * sublength;
2978 destructsymbols(symbol * root, int level)
2981 int savepri = FALSE;
2982 symbol *sym = root->next;
2984 while (sym != NULL && sym->compound >= level)
2986 if (sym->ident == iVARIABLE || sym->ident == iARRAY)
2988 char symbolname[16];
2992 /* check that the '~' operator is defined for this tag */
2993 operator_symname(symbolname, "~", sym->tag, 0, 1, 0);
2994 if ((opsym = findglb(symbolname)) != NULL)
2996 /* save PRI, in case of a return statment */
2999 push1(); /* right-hand operand is in PRI */
3002 /* if the variable is an array, get the number of elements */
3003 if (sym->ident == iARRAY)
3005 elements = calc_array_datasize(sym, &offset);
3006 /* "elements" can be zero when the variable is declared like
3007 * new mytag: myvar[2][] = { {1, 2}, {3, 4} }
3008 * one should declare all dimensions!
3011 error(46, sym->name); /* array size is unknown */
3019 /* call the '~' operator */
3021 addconst(offset); /*add offset to array data to the address */
3023 pushval(2 * sizeof(cell)); /* 2 parameters */
3025 if (sc_status != statSKIP)
3026 markusage(opsym, uREAD); /* do not mark as "used" when this
3027 * call itself is skipped */
3028 if (opsym->x.lib != NULL)
3029 opsym->x.lib->value += 1; /* increment "usage count"
3035 /* restore PRI, if it was saved */
3041 insert_constval(constvalue * prev, constvalue * next, char *name,
3042 cell val, short index)
3046 if ((cur = (constvalue *) malloc(sizeof(constvalue))) == NULL)
3047 error(103); /* insufficient memory (fatal error) */
3048 memset(cur, 0, sizeof(constvalue));
3049 strcpy(cur->name, name);
3058 append_constval(constvalue * table, char *name, cell val, short index)
3060 constvalue *cur, *prev;
3062 /* find the end of the constant table */
3063 for (prev = table, cur = table->next; cur != NULL;
3064 prev = cur, cur = cur->next)
3066 return insert_constval(prev, NULL, name, val, index);
3070 find_constval(constvalue * table, char *name, short index)
3072 constvalue *ptr = table->next;
3076 if (strcmp(name, ptr->name) == 0 && ptr->index == index)
3084 find_constval_byval(constvalue * table, cell val)
3086 constvalue *ptr = table->next;
3090 if (ptr->value == val)
3097 #if 0 /* never used */
3099 delete_constval(constvalue * table, char *name)
3101 constvalue *prev = table;
3102 constvalue *cur = prev->next;
3106 if (strcmp(name, cur->name) == 0)
3108 prev->next = cur->next;
3120 delete_consttable(constvalue * table)
3122 constvalue *cur = table->next, *next;
3130 memset(table, 0, sizeof(constvalue));
3135 * Adds a symbol to the #define symbol table.
3138 add_constant(char *name, cell val, int vclass, int tag)
3142 /* Test whether a global or local symbol with the same name exists. Since
3143 * constants are stored in the symbols table, this also finds previously
3144 * defind constants. */
3145 sym = findglb(name);
3147 sym = findloc(name);
3150 /* silently ignore redefinitions of constants with the same value */
3151 if (sym->ident == iCONSTEXPR)
3153 if (sym->addr != val)
3154 error(201, name); /* redefinition of constant (different value) */
3158 error(21, name); /* symbol already defined */
3163 /* constant doesn't exist yet, an entry must be created */
3164 sym = addsym(name, val, iCONSTEXPR, vclass, tag, uDEFINE);
3165 if (sc_status == statIDLE)
3166 sym->usage |= uPREDEF;
3169 /* statement - The Statement Parser
3171 * This routine is called whenever the parser needs to know what
3172 * statement it encounters (i.e. whenever program syntax requires a
3176 statement(int *lastindent, int allow_decl)
3184 error(36); /* empty statement */
3189 tok = lex(&val, &st);
3191 setline(fline, fcurrent);
3192 /* lex() has set stmtindent */
3193 if (lastindent != NULL && tok != tLABEL)
3196 if (*lastindent >= 0 && *lastindent != stmtindent &&
3197 !indent_nowarn && sc_tabsize > 0)
3198 error(217); /* loose indentation */
3200 *lastindent = stmtindent;
3201 indent_nowarn = TRUE; /* if warning was blocked, re-enable it */
3216 error(3); /* declaration only valid in a block */
3227 error(3); /* declaration only valid in a block */
3231 if (!matchtoken('}')) /* {} is the empty statement */
3233 /* lastst (for "last statement") does not change */
3236 error(36); /* empty statement */
3260 error(14); /* not in switch */
3300 default: /* non-empty expression */
3301 lexpush(); /* analyze token later */
3302 doexpr(TRUE, TRUE, TRUE, TRUE, NULL, FALSE);
3312 cell save_decl = declared;
3315 nestlevel += 1; /* increase compound statement level */
3316 while (matchtoken('}') == 0)
3317 { /* repeat until compound statement is closed */
3320 needtoken('}'); /* gives error: "expected token }" */
3326 && (lastst == tRETURN || lastst == tBREAK
3327 || lastst == tCONTINUE))
3328 error(225); /* unreachable code */
3329 statement(&indent, TRUE); /* do a statement */
3333 if (lastst != tRETURN)
3334 destructsymbols(&loctab, nestlevel);
3335 if (lastst != tRETURN && lastst != tGOTO)
3336 /* delete local variable space */
3337 modstk((int)(declared - save_decl) * sizeof(cell));
3339 testsymbols(&loctab, nestlevel, FALSE, TRUE); /* look for unused
3341 declared = save_decl;
3342 delete_symbols(&loctab, nestlevel, FALSE, TRUE);
3343 /* erase local symbols, but
3344 * retain block local labels
3345 * (within the function) */
3347 nestlevel -= 1; /* decrease compound statement level */
3352 * Global references: stgidx (referred to only)
3355 doexpr(int comma, int chkeffect, int allowarray, int mark_endexpr,
3356 int *tag, int chkfuncresult)
3358 int constant, index, ident;
3359 int localstaging = FALSE;
3364 stgset(TRUE); /* start stage-buffering */
3365 localstaging = TRUE;
3366 assert(stgidx == 0);
3369 errorset(sEXPRMARK);
3372 /* on second round through, mark the end of the previous expression */
3373 if (index != stgidx)
3376 ident = expression(&constant, &val, tag, chkfuncresult);
3377 if (!allowarray && (ident == iARRAY || ident == iREFARRAY))
3378 error(33, "-unknown-"); /* array must be indexed */
3379 if (chkeffect && !sideeffect)
3380 error(215); /* expression has no effect */
3382 while (comma && matchtoken(',')); /* more? */
3384 endexpr(TRUE); /* optionally, mark the end of the expression */
3385 errorset(sEXPRRELEASE);
3389 stgset(FALSE); /* stop staging */
3396 constexpr(cell * val, int *tag)
3398 int constant, index;
3401 stgset(TRUE); /* start stage-buffering */
3402 stgget(&index, &cidx); /* mark position in code generator */
3403 errorset(sEXPRMARK);
3404 expression(&constant, val, tag, FALSE);
3405 stgdel(index, cidx); /* scratch generated code */
3406 stgset(FALSE); /* stop stage-buffering */
3408 error(8); /* must be constant expression */
3409 errorset(sEXPRRELEASE);
3415 * In the case a "simple assignment" operator ("=") is used within a
3416 * test, * the warning "possibly unintended assignment" is displayed.
3417 * This routine sets the global variable "intest" to true, it is
3418 * restored upon termination. In the case the assignment was intended,
3419 * use parantheses around the expression to avoid the warning;
3420 * primary() sets "intest" to 0.
3422 * Global references: intest (altered, but restored upon termination)
3425 test(int label, int parens, int invert)
3429 value lval = { NULL, 0, 0, 0, 0, NULL };
3430 int localstaging = FALSE;
3434 stgset(TRUE); /* start staging */
3435 localstaging = TRUE;
3437 stgget(&index, &cidx); /* should start at zero if started
3443 /* FIXME: 64bit unsafe! putting an int on a stack of void *'s */
3444 pushstk((stkitem) intest);
3450 stgget(&index, &cidx); /* mark position (of last expression) in
3454 tok = matchtoken(',');
3458 while (tok); /* do */
3461 if (lval.ident == iARRAY || lval.ident == iREFARRAY)
3464 (lval.sym->name != NULL) ? lval.sym->name : "-unknown-";
3465 error(33, ptr); /* array must be indexed */
3467 if (lval.ident == iCONSTEXPR)
3468 { /* constant expression */
3469 intest = (int)(long)popstk(); /* restore stack */
3470 stgdel(index, cidx);
3472 { /* code always executed */
3473 error(206); /* redundant test: always non-zero */
3477 error(205); /* redundant code: never executed */
3482 stgout(0); /* write "jumplabel" code */
3483 stgset(FALSE); /* stop staging */
3487 if (lval.tag != 0 && lval.tag != sc_addtag("bool"))
3488 if (check_userop(lneg, lval.tag, 0, 1, NULL, &lval.tag))
3489 invert = !invert; /* user-defined ! operator inverted result */
3491 jmp_ne0(label); /* jump to label if true (different from 0) */
3493 jmp_eq0(label); /* jump to label if false (equal to 0) */
3494 endexpr(TRUE); /* end expression (give optimizer a chance) */
3495 intest = (int)(long)popstk(); /* double typecast to avoid warning
3496 * with Microsoft C */
3499 stgout(0); /* output queue from the very beginning (see
3500 * assert() when localstaging is set to TRUE) */
3501 stgset(FALSE); /* stop staging */
3511 ifindent = stmtindent; /* save the indent of the "if" instruction */
3512 flab1 = getlabel(); /* get label number for false branch */
3513 test(flab1, TRUE, FALSE); /*get expression, branch to flab1 if false */
3514 statement(NULL, FALSE); /* if true, do a statement */
3515 if (matchtoken(tELSE) == 0)
3517 setlabel(flab1); /* no, simple if..., print false label */
3521 /* to avoid the "dangling else" error, we want a warning if the "else"
3522 * has a lower indent than the matching "if" */
3524 if (stmtindent < ifindent && sc_tabsize > 0)
3525 error(217); /* loose indentation */
3528 if ((lastst != tRETURN) && (lastst != tGOTO))
3530 setlabel(flab1); /* print false label */
3531 statement(NULL, FALSE); /* do "else" clause */
3532 setlabel(flab2); /* print true label */
3539 int wq[wqSIZE]; /* allocate local queue */
3541 addwhile(wq); /* add entry to queue for "break" */
3542 setlabel(wq[wqLOOP]); /* loop label */
3543 /* The debugger uses the "line" opcode to be able to "break" out of
3544 * a loop. To make sure that each loop has a line opcode, even for the
3545 * tiniest loop, set it below the top of the loop */
3546 setline(fline, fcurrent);
3547 test(wq[wqEXIT], TRUE, FALSE); /* branch to wq[wqEXIT] if false */
3548 statement(NULL, FALSE); /* if so, do a statement */
3549 jumplabel(wq[wqLOOP]); /* and loop to "while" start */
3550 setlabel(wq[wqEXIT]); /* exit label */
3551 delwhile(); /* delete queue entry */
3555 * Note that "continue" will in this case not jump to the top of the
3556 * loop, but to the end: just before the TRUE-or-FALSE testing code.
3561 int wq[wqSIZE], top;
3563 addwhile(wq); /* see "dowhile" for more info */
3564 top = getlabel(); /* make a label first */
3565 setlabel(top); /* loop label */
3566 statement(NULL, FALSE);
3568 setlabel(wq[wqLOOP]); /* "continue" always jumps to WQLOOP. */
3569 setline(fline, fcurrent);
3570 test(wq[wqEXIT], TRUE, FALSE);
3572 setlabel(wq[wqEXIT]);
3580 int wq[wqSIZE], skiplab;
3582 int save_nestlevel, index;
3585 save_decl = declared;
3586 save_nestlevel = nestlevel;
3589 skiplab = getlabel();
3591 if (matchtoken(';') == 0)
3593 /* new variable declarations are allowed here */
3594 if (matchtoken(tNEW))
3596 /* The variable in expr1 of the for loop is at a
3597 * 'compound statement' level of it own.
3600 declloc(FALSE); /* declare local variable */
3604 doexpr(TRUE, TRUE, TRUE, TRUE, NULL, FALSE); /* expression 1 */
3608 /* Adjust the "declared" field in the "while queue", in case that
3609 * local variables were declared in the first expression of the
3610 * "for" loop. These are deleted in separately, so a "break" or a
3611 * "continue" must ignore these fields.
3614 assert(ptr != NULL);
3615 ptr[wqBRK] = (int)declared;
3616 ptr[wqCONT] = (int)declared;
3617 jumplabel(skiplab); /* skip expression 3 1st time */
3618 setlabel(wq[wqLOOP]); /* "continue" goes to this label: expr3 */
3619 setline(fline, fcurrent);
3620 /* Expressions 2 and 3 are reversed in the generated code:
3621 * expression 3 precedes expression 2.
3622 * When parsing, the code is buffered and marks for
3623 * the start of each expression are insterted in the buffer.
3626 stgset(TRUE); /* start staging */
3627 assert(stgidx == 0);
3629 stgmark(sSTARTREORDER);
3630 stgmark((char)(sEXPRSTART + 0)); /* mark start of 2nd expression
3632 setlabel(skiplab); /*jump to this point after 1st expression */
3633 if (matchtoken(';') == 0)
3635 test(wq[wqEXIT], FALSE, FALSE); /* expression 2
3636 *(jump to wq[wqEXIT] if false) */
3639 stgmark((char)(sEXPRSTART + 1)); /* mark start of 3th expression
3641 if (matchtoken(')') == 0)
3643 doexpr(TRUE, TRUE, TRUE, TRUE, NULL, FALSE); /* expression 3 */
3646 stgmark(sENDREORDER); /* mark end of reversed evaluation */
3648 stgset(FALSE); /* stop staging */
3649 statement(NULL, FALSE);
3650 jumplabel(wq[wqLOOP]);
3651 setlabel(wq[wqEXIT]);
3654 assert(nestlevel >= save_nestlevel);
3655 if (nestlevel > save_nestlevel)
3657 /* Clean up the space and the symbol table for the local
3658 * variable in "expr1".
3660 destructsymbols(&loctab, nestlevel);
3661 modstk((int)(declared - save_decl) * sizeof(cell));
3662 declared = save_decl;
3663 delete_symbols(&loctab, nestlevel, FALSE, TRUE);
3664 nestlevel = save_nestlevel; /* reset 'compound statement'
3669 /* The switch statement is incompatible with its C sibling:
3670 * 1. the cases are not drop through
3671 * 2. only one instruction may appear below each case, use a compound
3672 * instruction to execute multiple instructions
3673 * 3. the "case" keyword accepts a comma separated list of values to
3674 * match, it also accepts a range using the syntax "1 .. 4"
3677 * PRI = expression result
3678 * param = table offset (code segment)
3684 int lbl_table, lbl_exit, lbl_case;
3685 int tok, swdefault, casecount;
3688 constvalue caselist = { NULL, "", 0, 0 }; /*case list starts empty */
3689 constvalue *cse, *csp;
3690 char labelname[sNAMEMAX + 1];
3693 doexpr(TRUE, FALSE, FALSE, TRUE, NULL, FALSE); /* evaluate
3694 * switch expression */
3696 /* generate the code for the switch statement, the label is the
3697 * address of the case table (to be generated later).
3699 lbl_table = getlabel();
3700 lbl_case = 0; /* just to avoid a compiler warning */
3701 ffswitch(lbl_table);
3704 lbl_exit = getlabel(); /*get label number for jumping out of switch */
3709 tok = lex(&val, &str); /* read in (new) token */
3713 if (swdefault != FALSE)
3714 error(15); /* "default" case must be last in switch
3716 lbl_case = getlabel();
3717 sc_allowtags = FALSE; /* do not allow tagnames here */
3722 /* ??? enforce/document that, in a switch, a statement cannot
3723 * start an opening brace (marks the start of a compound
3724 * statement) and search for the right-most colon before that
3726 * Now, by replacing the ':' by a special COLON token, you can
3727 * parse all expressions until that special token.
3730 constexpr(&val, NULL);
3731 /* Search the insertion point (the table is kept in sorted
3732 * order, so that advanced abstract machines can sift the
3733 * case table with a binary search). Check for duplicate
3734 * case values at the same time.
3736 for (csp = &caselist, cse = caselist.next;
3737 cse != NULL && cse->value < val;
3738 csp = cse, cse = cse->next)
3740 if (cse != NULL && cse->value == val)
3741 error(40, val); /* duplicate "case" label */
3742 /* Since the label is stored as a string in the
3743 * "constvalue", the size of an identifier must
3744 * be at least 8, as there are 8
3745 * hexadecimal digits in a 32-bit number.
3748 #error Length of identifier (sNAMEMAX) too small.
3750 insert_constval(csp, cse, itoh(lbl_case), val, 0);
3751 if (matchtoken(tDBLDOT))
3755 constexpr(&end, NULL);
3757 error(50); /* invalid range */
3758 while (++val <= end)
3761 /* find the new insertion point */
3762 for (csp = &caselist, cse = caselist.next;
3763 cse != NULL && cse->value < val;
3764 csp = cse, cse = cse->next)
3766 if (cse != NULL && cse->value == val)
3767 error(40, val); /* duplicate "case" label */
3768 insert_constval(csp, cse, itoh(lbl_case), val, 0);
3772 while (matchtoken(','));
3773 needtoken(':'); /* ':' ends the case */
3774 sc_allowtags = TRUE; /* reset */
3776 statement(NULL, FALSE);
3777 jumplabel(lbl_exit);
3780 if (swdefault != FALSE)
3781 error(16); /* multiple defaults in switch */
3782 lbl_case = getlabel();
3786 statement(NULL, FALSE);
3787 /* Jump to lbl_exit, even thouh this is the last clause in the
3788 *switch, because the jump table is generated between the last
3789 * clause of the switch and the exit label.
3791 jumplabel(lbl_exit);
3794 /* nothing, but avoid dropping into "default" */
3798 indent_nowarn = TRUE; /* disable this check */
3799 tok = '}'; /* break out of the loop after an error */
3805 /* verify that the case table is sorted (unfortunatly, duplicates can
3806 * occur; there really shouldn't be duplicate cases, but the compiler
3807 * may not crash or drop into an assertion for a user error). */
3808 for (cse = caselist.next; cse != NULL && cse->next != NULL; cse = cse->next)
3809 ; /* empty. no idea whether this is correct, but we MUST NOT do
3810 * the setlabel(lbl_table) call in the loop body. doing so breaks
3811 * switch statements that only have one case statement following.
3815 /* generate the table here, before lbl_exit (general jump target) */
3816 setlabel(lbl_table);
3818 if (swdefault == FALSE)
3820 /* store lbl_exit as the "none-matched" label in the switch table */
3821 strcpy(labelname, itoh(lbl_exit));
3825 /* lbl_case holds the label of the "default" clause */
3826 strcpy(labelname, itoh(lbl_case));
3828 ffcase(casecount, labelname, TRUE);
3829 /* generate the rest of the table */
3830 for (cse = caselist.next; cse != NULL; cse = cse->next)
3831 ffcase(cse->value, cse->name, FALSE);
3834 delete_consttable(&caselist); /* clear list of case labels */
3842 value lval = { NULL, 0, 0, 0, 0, NULL };
3844 if ((sc_debug & sCHKBOUNDS) != 0)
3846 flab1 = getlabel(); /* get label number for "OK" branch */
3847 test(flab1, FALSE, TRUE); /* get expression and branch
3848 * to flab1 if true */
3849 setline(fline, fcurrent); /* make sure we abort on the correct
3851 ffabort(xASSERTION);
3856 stgset(TRUE); /* start staging */
3857 stgget(&index, &cidx); /* mark position in code generator */
3862 stgdel(index, cidx); /* just scrap the code */
3864 while (matchtoken(','));
3865 stgset(FALSE); /* stop staging */
3877 if (lex(&val, &st) == tSYMBOL)
3880 jumplabel((int)sym->addr);
3881 sym->usage |= uREAD; /* set "uREAD" bit */
3883 * // ??? if the label is defined (check sym->usage & uDEFINE), check
3884 * // sym->compound (nesting level of the label) against nestlevel;
3885 * // if sym->compound < nestlevel, call the destructor operator
3890 error(20, st); /* illegal symbol name */
3902 tokeninfo(&val, &st); /* retrieve label name again */
3903 if (find_constval(&tagname_tab, st, 0) != NULL)
3904 error(221, st); /* label name shadows tagname */
3906 setlabel((int)sym->addr);
3907 /* since one can jump around variable declarations or out of compound
3908 * blocks, the stack must be manually adjusted
3910 setstk(-declared * sizeof(cell));
3911 sym->usage |= uDEFINE; /* label is now defined */
3916 * Finds a label from the (local) symbol table or adds one to it.
3917 * Labels are local in scope.
3919 * Note: The "_usage" bit is set to zero. The routines that call
3920 * "fetchlab()" must set this bit accordingly.
3923 fetchlab(char *name)
3927 sym = findloc(name); /* labels are local in scope */
3930 if (sym->ident != iLABEL)
3931 error(19, sym->name); /* not a label: ... */
3935 sym = addsym(name, getlabel(), iLABEL, sLOCAL, 0, 0);
3936 sym->x.declared = (int)declared;
3937 sym->compound = nestlevel;
3944 * Global references: rettype (altered)
3951 if (matchtoken(tTERM) == 0)
3953 if ((rettype & uRETNONE) != 0)
3954 error(208); /* mix "return;" and "return value;" */
3955 doexpr(TRUE, FALSE, FALSE, TRUE, &tag, FALSE);
3957 rettype |= uRETVALUE; /* function returns a value */
3958 /* check tagname with function tagname */
3959 assert(curfunc != NULL);
3960 if (!matchtag(curfunc->tag, tag, TRUE))
3961 error(213); /* tagname mismatch */
3965 /* this return statement contains no expression */
3967 if ((rettype & uRETVALUE) != 0)
3969 char symname[2 * sNAMEMAX + 16]; /* allow space for user
3970 * defined operators */
3971 assert(curfunc != NULL);
3972 funcdisplayname(symname, curfunc->name);
3973 error(209, symname); /* function should return a value */
3975 rettype |= uRETNONE; /* function does not return anything */
3977 destructsymbols(&loctab, 0); /*call destructor for *all* locals */
3978 modstk((int)declared * sizeof(cell)); /* end of function, remove
3979 *all* * local variables*/
3988 ptr = readwhile(); /* readwhile() gives an error if not in loop */
3992 destructsymbols(&loctab, nestlevel);
3993 modstk(((int)declared - ptr[wqBRK]) * sizeof(cell));
3994 jumplabel(ptr[wqEXIT]);
4002 ptr = readwhile(); /* readwhile() gives an error if not in loop */
4006 destructsymbols(&loctab, nestlevel);
4007 modstk(((int)declared - ptr[wqCONT]) * sizeof(cell));
4008 jumplabel(ptr[wqLOOP]);
4014 /* find the tag by value in the table, then set the top bit to mark it
4021 assert((tag & PUBLICTAG) == 0);
4022 for (ptr = tagname_tab.next;
4023 ptr != NULL && tag != (int)(ptr->value & TAGMASK); ptr = ptr->next)
4026 ptr->value |= PUBLICTAG;
4035 if (matchtoken(tTERM) == 0)
4037 doexpr(TRUE, FALSE, FALSE, TRUE, &tag, FALSE);
4046 destructsymbols(&loctab, 0); /* call destructor for *all* locals */
4055 if (matchtoken(tTERM) == 0)
4057 doexpr(TRUE, FALSE, FALSE, TRUE, &tag, FALSE);
4074 ptr[wqBRK] = (int)declared; /* stack pointer (for "break") */
4075 ptr[wqCONT] = (int)declared; /* for "continue", possibly adjusted later */
4076 ptr[wqLOOP] = getlabel();
4077 ptr[wqEXIT] = getlabel();
4078 if (wqptr >= (wq + wqTABSZ - wqSIZE))
4079 error(102, "loop table"); /* loop table overflow (too many active loops) */
4082 { /* copy "ptr" to while queue table */
4102 error(24); /* out of context */
4107 return (wqptr - wqSIZE);