History

new preprocessing: macro function handling and better ANSI conformance

bellard committed 25 years ago

Commit 507a2211 parent cd526481 file · +741 −242Browse files

Unified · Split

tcc.c+741−242
@@ -17,20 +17,30 @@ · 16 unmodified lines
1717 * along with this program; if not, write to the Free Software
1818 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1919 */
20−#include "tcclib.h"
20+#include <tcclib.h>
2121
22−#define INCLUDE_PATH "/usr/include"
22+//#define DEBUG
23+/* preprocessor debug */
24+//#define PP_DEBUG
2325
2426 #define TEXT_SIZE 50000
2527 #define DATA_SIZE 50000
26−#define SYM_TABLE_SIZE 10000
27−#define MACRO_STACK_SIZE 32
2828 #define INCLUDE_STACK_SIZE 32
2929 #define IFDEF_STACK_SIZE 64
3030 #define VSTACK_SIZE 64
31+#define STRING_MAX_SIZE 1024
32+#define INCLUDE_PATHS_MAX 32
3133
3234 #define NB_REGS 3
3335
36+/* token symbol management */
37+typedef struct TokenSym {
38+ struct TokenSym *next;
39+ int tok; /* token number */
40+ int len;
41+ char str[1];
42+} TokenSym;
43+
3444 /* symbol management */
3545 typedef struct Sym {
3646 int v; /* symbol token */
@@ -47,6 +57,10 @@ typedef struct Sym { · 10 unmodified lines
4757 #define FUNC_OLD 2 /* old function prototype */
4858 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
4959
60+/* field t for macros */
61+#define MACRO_OBJ 0 /* object like macro */
62+#define MACRO_FUNC 1 /* function like macro */
63+
5064 typedef struct {
5165 FILE *file;
5266 char *filename;
@@ -62,14 +76,18 @@ typedef struct { · 9 unmodified lines
6276 */
6377 FILE *file;
6478 int tok, tok1, tokc, rsym, anon_sym,
65− prog, ind, loc, glo, vt, vc, line_num;
66−char *idtable, *idptr, *filename;
79+ prog, ind, loc, glo, vt, vc, const_wanted, line_num;
80+TokenSym *first_ident;
81+char token_buf[STRING_MAX_SIZE + 1];
82+char *filename;
6783 Sym *define_stack, *global_stack, *local_stack, *label_stack;
6884
6985 int vstack[VSTACK_SIZE], *vstack_ptr;
70−char *macro_stack[MACRO_STACK_SIZE], **macro_stack_ptr, *macro_ptr;
86+int *macro_ptr, *macro_ptr_allocated;
7187 IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr;
7288 int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr;
89+char *include_paths[INCLUDE_PATHS_MAX];
90+int nb_include_paths;
7391
7492 /* The current value can be: */
7593 #define VT_VALMASK 0x000f
@@ -132,7 +150,11 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 56 unmodified lines
132150 #define TOK_UMOD 0xb1 /* unsigned modulo */
133151 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
134152 #define TOK_NUM 0xb3 /* number in tokc */
153+#define TOK_CCHAR 0xb4 /* char constant in tokc */
154+#define TOK_STR 0xb5 /* pointer to string in tokc */
135155
156+#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
157+
136158 #define TOK_SHL 0x01 /* shift left */
137159 #define TOK_SAR 0x02 /* signed shift right */
138160
@@ -148,8 +170,11 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 9 unmodified lines
148170 #define TOK_A_SHL 0x81
149171 #define TOK_A_SAR 0x82
150172
173+/* all identificators and strings have token above that */
174+#define TOK_IDENT 256
175+
151176 enum {
152− TOK_INT = 256,
177+ TOK_INT = TOK_IDENT,
153178 TOK_VOID,
154179 TOK_CHAR,
155180 TOK_IF,
@@ -180,6 +205,7 @@ enum { · 24 unmodified lines
180205 TOK_FLOAT,
181206 TOK_DOUBLE,
182207
208+ TOK_SHORT,
183209 TOK_STRUCT,
184210 TOK_UNION,
185211 TOK_TYPEDEF,
@@ -195,6 +221,7 @@ enum { · 9 unmodified lines
195221 TOK_ELIF,
196222 TOK_ENDIF,
197223 TOK_DEFINED,
224+ TOK_UNDEF,
198225
199226 /* special identifiers */
200227 TOK_MAIN,
@@ -202,6 +229,7 @@ enum { · 1 unmodified line
202229
203230 void sum();
204231 void next();
232+void next_nomacro();
205233 int expr_const();
206234 void expr_eq();
207235 void expr();
@@ -209,6 +237,14 @@ void decl(); · 1 unmodified line
209237 int gv();
210238 void move_reg();
211239 void save_reg();
240+void macro_subst(int **tok_str, int *tok_len,
241+ Sym **nested_list, int *macro_str);
242+int save_reg_forced(int r);
243+int type_size(int t, int *a);
244+int pointed_type(int t);
245+int pointed_size(int t);
246+int ist(void);
247+int typ(int *v, int t);
212248
213249 int isid(c)
214250 {
@@ -222,8 +258,6 @@ int isnum(c) · 7 unmodified lines
222258 return c >= '0' & c <= '9';
223259 }
224260
225−#ifndef TINY
226−
227261 void printline()
228262 {
229263 IncludeFile *f;
@@ -269,26 +303,98 @@ void test_lvalue() · 39 unmodified lines
269303 expect("lvalue");
270304 }
271305
272−#else
306+TokenSym *tok_alloc(char *str, int len)
307+{
308+ TokenSym *ts, **pts;
309+ int t;
310+
311+ t = TOK_IDENT;
312+ pts = &first_ident;
313+ while (1) {
314+ ts = *pts;
315+ if (!ts)
316+ break;
317+ if (ts->len == len && !memcmp(ts->str, str, len))
318+ return ts;
319+ t++;
320+ pts = &(ts->next);
321+ }
322+ ts = malloc(sizeof(TokenSym) + len);
323+ if (!ts)
324+ error("memory full");
325+ ts->tok = t;
326+ ts->len = len;
327+ ts->next = NULL;
328+ memcpy(ts->str, str, len + 1);
273329
274−#define skip(c) next()
275−#define test_lvalue()
330+ *pts = ts;
331+ return ts;
332+}
276333
277−#endif
334+void add_char(char **pp, int c)
335+{
336+ char *p;
337+ p = *pp;
338+ if (c == '\'' || c == '\"' || c == '\\') {
339+ /* XXX: could be more precise if char or string */
340+ *p++ = '\\';
341+ }
342+ if (c >= 32 && c <= 126) {
343+ *p++ = c;
344+ } else {
345+ *p++ = '\\';
346+ if (c == '\n') {
347+ *p++ = 'n';
348+ } else {
349+ *p++ = '0' + ((c >> 6) & 7);
350+ *p++ = '0' + ((c >> 3) & 7);
351+ *p++ = '0' + (c & 7);
352+ }
353+ }
354+ *pp = p;
355+}
278356
279−char *get_tok_str(int v)
357+/* XXX: buffer overflow */
358+char *get_tok_str(int v, int c)
280359 {
281− int t;
360+ static char buf[STRING_MAX_SIZE + 1];
361+ TokenSym *ts;
282362 char *p;
283− p = idtable;
284− t = 256;
285− while (t != v) {
286− if (p >= idptr)
287− return 0;
288− while (*p++);
289− t++;
363+ int i;
364+
365+ if (v == TOK_NUM) {
366+ sprintf(buf, "%d", c);
367+ return buf;
368+ } else if (v == TOK_CCHAR) {
369+ p = buf;
370+ *p++ = '\'';
371+ add_char(&p, c);
372+ *p++ = '\'';
373+ *p = '\0';
374+ return buf;
375+ } else if (v == TOK_STR) {
376+ ts = (TokenSym *)c;
377+ p = buf;
378+ *p++ = '\"';
379+ for(i=0;i<ts->len;i++)
380+ add_char(&p, ts->str[i]);
381+ *p++ = '\"';
382+ *p = '\0';
383+ return buf;
384+ } else if (v < TOK_IDENT) {
385+ p = buf;
386+ *p++ = v;
387+ *p = '\0';
388+ return buf;
389+ } else {
390+ ts = first_ident;
391+ while (ts != NULL) {
392+ if (ts->tok == v)
393+ return ts->str;
394+ ts = ts->next;
395+ }
396+ return NULL;
290397 }
291− return p;
292398 }
293399
294400 /* find a symbol and return its associated structure. 's' is the top
@@ -312,7 +418,7 @@ Sym *sym_push1(Sym **ps, int v, int t, int c) · 17 unmodified lines
312418 s->v = v;
313419 s->t = t;
314420 s->c = c;
315− s->next = 0;
421+ s->next = NULL;
316422 s->prev = *ps;
317423 *ps = s;
318424 return s;
@@ -331,7 +437,6 @@ Sym *sym_find(int v) · 12 unmodified lines
331437 /* push a given symbol on the symbol stack */
332438 Sym *sym_push(int v, int t, int c)
333439 {
334− // printf("sym_push: %x %s type=%x\n", v, get_tok_str(v), t);
335440 if (local_stack)
336441 return sym_push1(&local_stack, v, t, c);
337442 else
@@ -346,7 +451,6 @@ void sym_pop(Sym **ps, Sym *b) · 8 unmodified lines
346451 s = *ps;
347452 while(s != b) {
348453 ss = s->prev;
349− // printf("sym_pop: %x %s type=%x\n", s->v, get_tok_str(s->v), s->t);
350454 free(s);
351455 s = ss;
352456 }
@@ -358,8 +462,6 @@ int ch, ch1; · 5 unmodified lines
358462 /* read next char from current input file */
359463 void inp()
360464 {
361− int c;
362−
363465 redo:
364466 ch1 = fgetc(file);
365467 if (ch1 == -1) {
@@ -379,26 +481,17 @@ void inp() · 13 unmodified lines
379481 // printf("ch1=%c 0x%x\n", ch1, ch1);
380482 }
381483
382−/* input with '\\n' handling and macro subtitution if in macro state */
484+/* input with '\\n' handling */
383485 void minp()
384486 {
385− if (macro_ptr != 0) {
386− ch = *macro_ptr++;
387− /* end of macro ? */
388− if (ch == '\0') {
389− macro_ptr = *--macro_stack_ptr;
390− ch = (int)*--macro_stack_ptr;
391− }
392− } else {
393487 redo:
394− ch = ch1;
488+ ch = ch1;
489+ inp();
490+ if (ch == '\\' && ch1 == '\n') {
395491 inp();
396− if (ch == '\\' && ch1 == '\n') {
397− inp();
398− goto redo;
399− }
492+ goto redo;
400493 }
401− // printf("ch=%c 0x%x\n", ch, ch);
494+ //printf("ch=%c 0x%x\n", ch, ch);
402495 }
403496
404497 /* same as minp, but also skip comments */
@@ -458,7 +551,7 @@ void preprocess_skip() · 53 unmodified lines
458551 skip_spaces();
459552 if (ch == '#') {
460553 cinp();
461− next();
554+ next_nomacro();
462555 if (a == 0 &&
463556 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
464557 break;
@@ -497,53 +590,145 @@ char *get_str(c) · 32 unmodified lines
497590 return str;
498591 }
499592
500−/* return next token without macro substitution */
501−void next_nomacro()
593+void tok_add(int **tok_str, int *tok_len, int t)
502594 {
503− Sym *s;
504− /* hack to avoid replacing id by defined value */
505− s = define_stack;
506− define_stack = 0;
507− next();
508− define_stack = s;
595+ int len, *str;
596+ len = *tok_len;
597+ str = *tok_str;
598+ if ((len & 63) == 0) {
599+ str = realloc(str, (len + 64) * sizeof(int));
600+ if (!str)
601+ return;
602+ *tok_str = str;
603+ }
604+ str[len++] = t;
605+ *tok_len = len;
509606 }
510607
511−/* XXX: not correct yet (need to ensure it is constant) */
512−int expr_preprocess()
608+void tok_add2(int **tok_str, int *tok_len, int t, int c)
513609 {
514− int c;
610+ tok_add(tok_str, tok_len, t);
611+ if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR)
612+ tok_add(tok_str, tok_len, c);
613+}
515614
516− if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE)
517− error("too many nested macros");
518− *macro_stack_ptr++ = (char *)'\n';
519− *macro_stack_ptr++ = macro_ptr;
520− macro_ptr = get_str(';');
521− cinp();
615+/* eval an expression for #if/#elif */
616+int expr_preprocess()
617+{
618+ int *str, len, c, t;
619+ int *saved_macro_ptr;
620+
621+ str = NULL;
622+ len = 0;
623+ while (1) {
624+ skip_spaces();
625+ if (ch == '\n')
626+ break;
627+ next(); /* do macro subst */
628+ if (tok == TOK_DEFINED) {
629+ next_nomacro();
630+ t = tok;
631+ if (t == '(')
632+ next_nomacro();
633+ c = sym_find1(define_stack, tok) != 0;
634+ if (t == '(')
635+ next_nomacro();
636+ tok = TOK_NUM;
637+ tokc = c;
638+ } else if (tok >= TOK_IDENT) {
639+ /* if undefined macro */
640+ tok = TOK_NUM;
641+ tokc = 0;
642+ }
643+ tok_add2(&str, &len, tok, tokc);
644+ }
645+ tok_add(&str, &len, -1); /* simulate end of file */
646+ tok_add(&str, &len, 0);
647+ /* now evaluate C constant expression */
648+ saved_macro_ptr = macro_ptr;
649+ macro_ptr = str;
522650 next();
523651 c = expr_const();
524− ch = '\n';
652+ macro_ptr = saved_macro_ptr;
653+ free(str);
525654 return c != 0;
526655 }
527656
657+#ifdef DEBUG
658+void tok_print(int *str)
659+{
660+ int t, c;
661+
662+ while (1) {
663+ t = *str++;
664+ if (!t)
665+ break;
666+ c = 0;
667+ if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR)
668+ c = *str++;
669+ printf(" %s", get_tok_str(t, c));
670+ }
671+ printf("\n");
672+}
673+#endif
674+
528675 void preprocess()
529676 {
530− char *str;
531− int size, n, c, v;
677+ int size, i, c, v, t, *str, len;
532678 char buf[1024], *q, *p;
533679 char buf1[1024];
534680 FILE *f;
681+ Sym **ps, *first, *s;
535682
536683 cinp();
537684 next_nomacro();
538685 redo:
539686 if (tok == TOK_DEFINE) {
540− next_nomacro(); /* XXX: should pass parameter to avoid macro subst */
541− skip_spaces();
542− /* now 'tok' is the macro symbol */
543− /* a space is inserted after each macro */
544− str = get_str(' ');
545− // printf("define %s '%s'\n", get_tok_str(tok), str);
546− sym_push1(&define_stack, tok, 0, (int)str);
687+ next_nomacro();
688+ v = tok;
689+ /* XXX: should check if same macro (ANSI) */
690+ if (sym_find1(define_stack, v))
691+ warning("macro redefinition");
692+ first = NULL;
693+ t = MACRO_OBJ;
694+ /* '(' must be just after macro definition for MACRO_FUNC */
695+ if (ch == '(') {
696+ next_nomacro();
697+ next_nomacro();
698+ ps = &first;
699+ while (tok != ')') {
700+ s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
701+ *ps = s;
702+ ps = &s->next;
703+ next_nomacro();
704+ if (tok != ',')
705+ break;
706+ next_nomacro();
707+ }
708+ t = MACRO_FUNC;
709+ }
710+ str = NULL;
711+ len = 0;
712+ while (1) {
713+ skip_spaces();
714+ if (ch == '\n' || ch == -1)
715+ break;
716+ next_nomacro();
717+ tok_add2(&str, &len, tok, tokc);
718+ }
719+ tok_add(&str, &len, 0);
720+#ifdef PP_DEBUG
721+ printf("define %s %d: ", get_tok_str(v, 0), t);
722+ tok_print(str);
723+#endif
724+ s = sym_push1(&define_stack, v, t, (int)str);
725+ s->next = first;
726+ } else if (tok == TOK_UNDEF) {
727+ next_nomacro();
728+ s = sym_find1(define_stack, tok);
729+ /* undefine symbol by putting an invalid name */
730+ if (s)
731+ s->v = 0;
547732 } else if (tok == TOK_INCLUDE) {
548733 skip_spaces();
549734 if (ch == '<') {
@@ -577,12 +762,16 @@ void preprocess() · 27 unmodified lines
577762 goto found;
578763 }
579764 /* now search in standard include path */
580− strcpy(buf1, INCLUDE_PATH);
581− strcat(buf1, "/");
582− strcat(buf1, buf);
583− f = fopen(buf1, "r");
584− if (!f)
585− error("include file not found");
765+ for(i=nb_include_paths - 1;i>=0;i--) {
766+ strcpy(buf1, include_paths[i]);
767+ strcat(buf1, "/");
768+ strcat(buf1, buf);
769+ f = fopen(buf1, "r");
770+ if (f)
771+ goto found;
772+ }
773+ error("include file not found");
774+ f = NULL;
586775 found:
587776 /* push current file in stack */
588777 /* XXX: fix current line init */
@@ -659,19 +848,46 @@ int getn(b) · 70 unmodified lines
659848 return n;
660849 }
661850
662−void next()
851+/* read a character for string or char constant and eval escape codes */
852+int getq()
663853 {
664− int v, b;
665− char *q, *p;
666− Sym *s;
854+ int c;
667855
668− /* special 'ungettok' case for label parsing */
669− if (tok1) {
670− tok = tok1;
671− tok1 = 0;
672− return;
856+ c = ch;
857+ minp();
858+ if (c == '\\') {
859+ if (isnum(ch)) {
860+ return getn(8);
861+ } else {
862+ if (ch == 'a')
863+ c = '\a';
864+ else if (ch == 'b')
865+ c = '\b';
866+ else if (ch == 'f')
867+ c = '\f';
868+ else if (ch == 'n')
869+ c = '\n';
870+ else if (ch == 'r')
871+ c = '\r';
872+ else if (ch == 't')
873+ c = '\t';
874+ else if (ch == 'v')
875+ c = '\v';
876+ else
877+ c = ch;
878+ minp();
879+ }
673880 }
674− redo:
881+ return c;
882+}
883+
884+/* return next token without macro substitution */
885+void next_nomacro1()
886+{
887+ int b;
888+ char *q;
889+ TokenSym *ts;
890+
675891 /* skip spaces */
676892 while(1) {
677893 while (ch == '\n') {
@@ -684,58 +900,61 @@ void next() · 6 unmodified lines
684900 preprocess();
685901 }
686902 }
687− if (ch != ' ' && ch != 9)
903+ if (ch != ' ' && ch != '\t' && ch != '\f')
688904 break;
689905 cinp();
690906 }
691907 if (isid(ch)) {
692− q = idptr;
693− while(isid(ch) | isnum(ch)) {
908+ q = token_buf;
909+ while (isid(ch) | isnum(ch)) {
910+ if (q >= token_buf + STRING_MAX_SIZE)
911+ error("ident too long");
694912 *q++ = ch;
695913 cinp();
696914 }
697− *q++ = '\0';
698− p = idtable;
699− tok = 256;
700− while (p < idptr) {
701− if (strcmp(p, idptr) == 0)
702− break;
703− while (*p++);
704− tok++;
705− }
706− // printf("id=%s\n", idptr);
707− /* if not found, add symbol */
708− if (p == idptr)
709− idptr = q;
710− /* if symbol is a define, prepare substitution */
711− if (s = sym_find1(define_stack, tok)) {
712− if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE)
713− error("too many nested macros");
714− *macro_stack_ptr++ = (char *)ch;
715− *macro_stack_ptr++ = macro_ptr;
716− macro_ptr = (char *)s->c;
717− cinp();
718− goto redo;
719− }
915+ *q = '\0';
916+ ts = tok_alloc(token_buf, q - token_buf);
917+ tok = ts->tok;
720918 } else if (isnum(ch)) {
721919 /* number */
722920 b = 10;
723921 if (ch == '0') {
724922 cinp();
725923 b = 8;
726− if (ch == 'x') {
924+ if (ch == 'x' || ch == 'X') {
727925 cinp();
728926 b = 16;
729927 }
730928 }
731929 tokc = getn(b);
930+ /* XXX: add unsigned constant support (ANSI) */
931+ while (ch == 'L' || ch == 'l' || ch == 'U' || ch == 'u')
932+ cinp();
732933 tok = TOK_NUM;
934+ } else if (ch == '\'') {
935+ minp();
936+ tokc = getq();
937+ tok = TOK_CCHAR;
938+ if (ch != '\'')
939+ expect("\'");
940+ minp();
941+ } else if (ch == '\"') {
942+ minp();
943+ q = token_buf;
944+ while (ch != '\"') {
945+ b = getq();
946+ if (ch == -1)
947+ error("unterminated string");
948+ if (q >= token_buf + STRING_MAX_SIZE)
949+ error("string too long");
950+ *q++ = b;
951+ }
952+ *q = '\0';
953+ tokc = (int)tok_alloc(token_buf, q - token_buf);
954+ tok = TOK_STR;
955+ minp();
733956 } else {
734−#ifdef TINY
735− q = "<=\236>=\235!=\225++\244--\242==\224";
736−#else
737− q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250";
738−#endif
957+ q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250##\266";
739958 /* two chars */
740959 tok = ch;
741960 cinp();
@@ -764,7 +983,278 @@ void next() · 22 unmodified lines
764983 else if (tok == '>')
765984 tok = TOK_GT;
766985 }
767− // printf("tok=%x\n", tok);
986+}
987+
988+/* return next token without macro substitution. Can read input from
989+ macro_ptr buffer */
990+void next_nomacro()
991+{
992+ if (macro_ptr) {
993+ tok = *macro_ptr;
994+ if (tok) {
995+ macro_ptr++;
996+ if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_STR)
997+ tokc = *macro_ptr++;
998+ }
999+ } else {
1000+ next_nomacro1();
1001+ }
1002+}
1003+
1004+/* substitute args in macro_str and return allocated string */
1005+int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
1006+{
1007+ int *st, last_tok, t, c, notfirst, *str, len;
1008+ Sym *s;
1009+ TokenSym *ts;
1010+
1011+ str = NULL;
1012+ len = 0;
1013+ last_tok = 0;
1014+ while(1) {
1015+ t = *macro_str++;
1016+ if (!t)
1017+ break;
1018+ if (t == '#') {
1019+ /* stringize */
1020+ t = *macro_str++;
1021+ if (!t)
1022+ break;
1023+ s = sym_find1(args, t);
1024+ if (s) {
1025+ token_buf[0] = '\0';
1026+ st = (int *)s->c;
1027+ /* XXX: buffer overflow */
1028+ notfirst = 0;
1029+ while (*st) {
1030+ if (notfirst)
1031+ strcat(token_buf, " ");
1032+ t = *st++;
1033+ c = 0;
1034+ if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR)
1035+ c = *st++;
1036+ strcat(token_buf, get_tok_str(t, c));
1037+ notfirst = 1;
1038+ }
1039+#ifdef PP_DEBUG
1040+ printf("stringize: %s\n", token_buf);
1041+#endif
1042+ /* add string */
1043+ ts = tok_alloc(token_buf, strlen(token_buf));
1044+ tok_add2(&str, &len, TOK_STR, (int)ts);
1045+ } else {
1046+ tok_add(&str, &len, t);
1047+ }
1048+ } else if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) {
1049+ tok_add2(&str, &len, t, *macro_ptr++);
1050+ } else {
1051+ s = sym_find1(args, t);
1052+ if (s) {
1053+ st = (int *)s->c;
1054+ /* if '##' is present before or after , no arg substitution */
1055+ if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
1056+ while (*st)
1057+ tok_add(&str, &len, *st++);
1058+ } else {
1059+ macro_subst(&str, &len, nested_list, st);
1060+ }
1061+ } else {
1062+ tok_add(&str, &len, t);
1063+ }
1064+ }
1065+ last_tok = t;
1066+ }
1067+ tok_add(&str, &len, 0);
1068+ return str;
1069+}
1070+
1071+/* handle the '##' operator */
1072+int *macro_twosharps(int *macro_str)
1073+{
1074+ TokenSym *ts;
1075+ int *macro_str1, macro_str1_len, *macro_ptr1;
1076+ int t, c;
1077+ char *p;
1078+
1079+ macro_str1 = NULL;
1080+ macro_str1_len = 0;
1081+ tok = 0;
1082+ while (1) {
1083+ next_nomacro();
1084+ if (tok == 0)
1085+ break;
1086+ if (*macro_ptr == TOK_TWOSHARPS) {
1087+ macro_ptr++;
1088+ macro_ptr1 = macro_ptr;
1089+ t = *macro_ptr;
1090+ if (t) {
1091+ macro_ptr++;
1092+ c = 0;
1093+ if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR)
1094+ c = *macro_ptr++;
1095+ /* XXX: we handle only most common cases:
1096+ ident + ident or ident + number */
1097+ if (tok >= TOK_IDENT &&
1098+ (t >= TOK_IDENT || t == TOK_NUM)) {
1099+ /* XXX: buffer overflow */
1100+ p = get_tok_str(tok, tokc);
1101+ strcpy(token_buf, p);
1102+ p = get_tok_str(t, c);
1103+ strcat(token_buf, p);
1104+ ts = tok_alloc(token_buf, strlen(token_buf));
1105+ tok_add2(&macro_str1, &macro_str1_len, ts->tok, 0);
1106+ } else {
1107+ /* cannot merge tokens: skip '##' */
1108+ macro_ptr = macro_ptr1;
1109+ }
1110+ }
1111+ } else {
1112+ tok_add2(&macro_str1, &macro_str1_len, tok, tokc);
1113+ }
1114+ }
1115+ tok_add(&macro_str1, &macro_str1_len, 0);
1116+ return macro_str1;
1117+}
1118+
1119+
1120+
1121+/* do macro substitution of macro_str and add result to
1122+ (tok_str,tok_len). If macro_str is NULL, then input stream token is
1123+ substituted. 'nested_list' is the list of all macros we got inside
1124+ to avoid recursing. */
1125+void macro_subst(int **tok_str, int *tok_len,
1126+ Sym **nested_list, int *macro_str)
1127+{
1128+ Sym *s, *args, *sa;
1129+ int *str, parlevel, len, *mstr, t, *saved_macro_ptr;
1130+ int mstr_allocated, *macro_str1;
1131+
1132+ saved_macro_ptr = macro_ptr;
1133+ macro_ptr = macro_str;
1134+ macro_str1 = NULL;
1135+ if (macro_str) {
1136+ /* first scan for '##' operator handling */
1137+ macro_str1 = macro_twosharps(macro_str);
1138+ macro_ptr = macro_str1;
1139+ }
1140+
1141+ while (1) {
1142+ next_nomacro();
1143+ if (tok == 0)
1144+ break;
1145+ /* if symbol is a macro, prepare substitution */
1146+ s = sym_find1(define_stack, tok);
1147+ if (s) {
1148+ /* if nested substitution, do nothing */
1149+ if (sym_find1(*nested_list, tok))
1150+ goto no_subst;
1151+ mstr = (int *)s->c;
1152+ mstr_allocated = 0;
1153+ if (s->t == MACRO_FUNC) {
1154+ /* NOTE: we do not use next_nomacro to avoid eating the
1155+ next token. XXX: find better solution */
1156+ if (macro_ptr) {
1157+ t = *macro_ptr;
1158+ } else {
1159+ while (ch == ' ' || ch == '\t' || ch == '\n')
1160+ cinp();
1161+ t = ch;
1162+ }
1163+ if (t != '(') /* no macro subst */
1164+ goto no_subst;
1165+
1166+ /* argument macro */
1167+ next_nomacro();
1168+ next_nomacro();
1169+ args = NULL;
1170+ sa = s->next;
1171+ while (tok != ')' && sa) {
1172+ len = 0;
1173+ str = NULL;
1174+ parlevel = 0;
1175+ while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
1176+ tok != -1) {
1177+ if (tok == '(')
1178+ parlevel++;
1179+ else if (tok == ')')
1180+ parlevel--;
1181+ tok_add2(&str, &len, tok, tokc);
1182+ next_nomacro();
1183+ }
1184+ tok_add(&str, &len, 0);
1185+ sym_push1(&args, sa->v & ~SYM_FIELD, 0, (int)str);
1186+ if (tok != ',')
1187+ break;
1188+ next_nomacro();
1189+ sa = sa->next;
1190+ }
1191+ if (tok != ')')
1192+ expect(")");
1193+ /* now subst each arg */
1194+ mstr = macro_arg_subst(nested_list, mstr, args);
1195+ sym_pop(&args, NULL);
1196+ mstr_allocated = 1;
1197+ }
1198+ sym_push1(nested_list, s->v, 0, 0);
1199+ macro_subst(tok_str, tok_len, nested_list, mstr);
1200+ sym_pop(nested_list, (*nested_list)->prev);
1201+ if (mstr_allocated)
1202+ free(mstr);
1203+ /* only replace one macro while parsing input stream */
1204+ if (!macro_str)
1205+ return;
1206+ } else {
1207+ no_subst:
1208+ /* no need to add if reading input stream */
1209+ if (!macro_str)
1210+ return;
1211+ tok_add2(tok_str, tok_len, tok, tokc);
1212+ }
1213+ }
1214+ macro_ptr = saved_macro_ptr;
1215+ if (macro_str1)
1216+ free(macro_str1);
1217+}
1218+
1219+/* return next token with macro substitution */
1220+void next()
1221+{
1222+ int len, *ptr;
1223+ Sym *nested_list;
1224+
1225+ /* special 'ungettok' case for label parsing */
1226+ if (tok1) {
1227+ tok = tok1;
1228+ tok1 = 0;
1229+ } else {
1230+ redo:
1231+ if (!macro_ptr) {
1232+ /* if not reading from macro substuted string, then try to substitute */
1233+ len = 0;
1234+ ptr = NULL;
1235+ nested_list = NULL;
1236+ macro_subst(&ptr, &len, &nested_list, NULL);
1237+ if (ptr) {
1238+ tok_add(&ptr, &len, 0);
1239+ macro_ptr = ptr;
1240+ macro_ptr_allocated = ptr;
1241+ goto redo;
1242+ }
1243+ if (tok == 0)
1244+ goto redo;
1245+ } else {
1246+ next_nomacro();
1247+ if (tok == 0) {
1248+ /* end of macro string: free it */
1249+ free(macro_ptr_allocated);
1250+ macro_ptr = NULL;
1251+ goto redo;
1252+ }
1253+ }
1254+ }
1255+#ifdef DEBUG
1256+ printf("token = %s\n", get_tok_str(tok, tokc));
1257+#endif
7681258 }
7691259
7701260 void swap(int *p, int *q)
@@ -1004,7 +1494,7 @@ void gen_op1(op, r, fr) · 233 unmodified lines
10041494 /* end of X86 code generator */
10051495 /*************************************************************/
10061496
1007−int save_reg_forced(r)
1497+int save_reg_forced(int r)
10081498 {
10091499 int i, l, *p, t;
10101500 /* store register */
@@ -1156,6 +1646,20 @@ void gen_opc(op) · 145 unmodified lines
11561646 case TOK_SHL: vc <<= fc; break;
11571647 case TOK_SHR: vc = (unsigned)vc >> fc; break;
11581648 case TOK_SAR: vc >>= fc; break;
1649+ /* tests */
1650+ case TOK_ULT: vc = (unsigned)vc < (unsigned)fc; break;
1651+ case TOK_UGE: vc = (unsigned)vc >= (unsigned)fc; break;
1652+ case TOK_EQ: vc = vc == fc; break;
1653+ case TOK_NE: vc = vc != fc; break;
1654+ case TOK_ULE: vc = (unsigned)vc <= (unsigned)fc; break;
1655+ case TOK_UGT: vc = (unsigned)vc > (unsigned)fc; break;
1656+ case TOK_LT: vc = vc < fc; break;
1657+ case TOK_GE: vc = vc >= fc; break;
1658+ case TOK_LE: vc = vc <= fc; break;
1659+ case TOK_GT: vc = vc > fc; break;
1660+ /* logical */
1661+ case TOK_LAND: vc = vc && fc; break;
1662+ case TOK_LOR: vc = vc || fc; break;
11591663 default:
11601664 goto general_case;
11611665 }
@@ -1208,13 +1712,13 @@ void gen_opc(op) · 46 unmodified lines
12081712 }
12091713 }
12101714
1211−int pointed_size(t)
1715+int pointed_size(int t)
12121716 {
12131717 return type_size(pointed_type(t), &t);
12141718 }
12151719
12161720 /* generic gen_op: handles types problems */
1217−void gen_op(op)
1721+void gen_op(int op)
12181722 {
12191723 int u, t1, t2;
12201724
@@ -1354,14 +1858,6 @@ void inc(post, c) · 133 unmodified lines
13541858 vpop(&vt, &vc);
13551859 }
13561860
1357−int expr_const()
1358−{
1359− expr_eq();
1360− if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST)
1361− expect("constant");
1362− return vc;
1363−}
1364−
13651861 /* enum/struct/union declaration */
13661862 int struct_decl(u)
13671863 {
@@ -1393,7 +1889,7 @@ int struct_decl(u) · 25 unmodified lines
13931889 /* cannot be empty */
13941890 c = 0;
13951891 maxalign = 0;
1396− slast = 0;
1892+ slast = NULL;
13971893 while (1) {
13981894 if (a == TOK_ENUM) {
13991895 v = tok;
@@ -1449,30 +1945,27 @@ int struct_decl(u) · 49 unmodified lines
14491945 and skip it.
14501946 XXX: A '2' is ored to ensure non zero return if int type.
14511947 */
1452−int ist()
1948+int ist(void)
14531949 {
1454− int t, n, v;
1950+ int t;
14551951 Sym *s;
14561952
14571953 t = 0;
14581954 while(1) {
1459−#ifndef TINY
14601955 if (tok == TOK_ENUM) {
14611956 t |= struct_decl(VT_ENUM);
14621957 } else if (tok == TOK_STRUCT || tok == TOK_UNION) {
14631958 t |= struct_decl(VT_STRUCT);
1464− } else
1465−#endif
1466− {
1959+ } else {
14671960 if (tok == TOK_CHAR) {
14681961 t |= VT_BYTE;
14691962 } else if (tok == TOK_VOID) {
14701963 t |= VT_VOID;
1471− } else if (tok == TOK_INT |
1964+ } else if (tok == TOK_INT | tok == TOK_SHORT |
14721965 (tok >= TOK_CONST & tok <= TOK_INLINE)) {
14731966 /* ignored types */
1474− } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) {
1475− error("floats not supported");
1967+ } else if (tok == TOK_FLOAT || tok == TOK_DOUBLE) {
1968+ warning("floats not supported");
14761969 } else if (tok == TOK_EXTERN) {
14771970 t |= VT_EXTERN;
14781971 } else if (tok == TOK_STATIC) {
@@ -1618,11 +2111,11 @@ int typ(int *v, int t) · 139 unmodified lines
16182111 /* define a new external reference to a function 'v' of type 'u' */
16192112 Sym *external_func(v, u)
16202113 {
1621− int t, n, p;
2114+ int n;
16222115 Sym *s;
16232116 s = sym_find(v);
16242117 if (!s) {
1625− n = dlsym(0, get_tok_str(v));
2118+ n = (int)dlsym(0, get_tok_str(v, 0));
16262119 if (n == 0) {
16272120 /* used to generate symbol list */
16282121 s = sym_push1(&global_stack,
@@ -1636,31 +2129,6 @@ Sym *external_func(v, u) · 7 unmodified lines
16362129 return s;
16372130 }
16382131
1639−/* read a character for string or char constant and eval escape codes */
1640−int getq()
1641−{
1642− int c;
1643−
1644− c = ch;
1645− minp();
1646− if (c == '\\') {
1647− if (isnum(ch)) {
1648− return getn(8);
1649− } else {
1650− if (ch == 'n')
1651− c = '\n';
1652− else if (ch == 'r')
1653− c = '\r';
1654− else if (ch == 't')
1655− c = '\t';
1656− else
1657− c = ch;
1658− minp();
1659− }
1660− }
1661− return c;
1662−}
1663−
16642132 void indir()
16652133 {
16662134 if (vt & VT_LVAL)
@@ -1677,37 +2145,20 @@ void unary() · 10 unmodified lines
16772145 int n, t, ft, fc, p, r;
16782146 Sym *s;
16792147
1680− if (tok == TOK_NUM) {
2148+ if (tok == TOK_NUM || tok == TOK_CCHAR) {
16812149 vset(VT_CONST, tokc);
16822150 next();
1683− } else
1684− if (tok == '\'') {
1685− vset(VT_CONST, getq());
1686− next(); /* skip char */
1687− skip('\'');
1688− } else if (tok == '\"') {
2151+ } else if (tok == TOK_STR) {
2152+ TokenSym *ts;
16892153 /* generate (char *) type */
16902154 vset(VT_CONST | mk_pointer(VT_TYPE), glo);
1691− while (tok == '\"') {
1692− while (ch != '\"') {
1693− if (ch == -1)
1694− error("unterminated string");
1695− *(char *)glo++ = getq();
1696− }
1697− minp();
2155+ while (tok == TOK_STR) {
2156+ ts = (TokenSym *)tokc;
2157+ memcpy((void *)glo, ts->str, ts->len);
2158+ glo += ts->len;
16982159 next();
16992160 }
17002161 *(char *)glo++ = 0;
1701− } else if (tok == TOK_DEFINED) {
1702− /* XXX: should only be used in preprocess expr parsing */
1703− next_nomacro();
1704− t = tok;
1705− if (t == '(')
1706− next_nomacro();
1707− vset(VT_CONST, sym_find1(define_stack, tok) != 0);
1708− next();
1709− if (t == '(')
1710− skip(')');
17112162 } else {
17122163 t = tok;
17132164 next();
@@ -1732,11 +2183,13 @@ void unary() · 18 unmodified lines
17322183 } else
17332184 if (t == '!') {
17342185 unary();
1735− if ((vt & VT_VALMASK) == VT_CMP)
2186+ if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST)
2187+ vc = !vc;
2188+ else if ((vt & VT_VALMASK) == VT_CMP)
17362189 vc = vc ^ 1;
17372190 else
17382191 vset(VT_JMP, gtst(1, 0));
1739− } else
2192+ } else
17402193 if (t == '~') {
17412194 unary();
17422195 vpush();
@@ -1818,7 +2271,10 @@ void unary() · 75 unmodified lines
18182271 vset(VT_CONST, s->c);
18192272 gen_op('+');
18202273 /* change type to field type, and set to lvalue */
1821− vt = (vt & VT_TYPEN) | VT_LVAL | s->t;
2274+ vt = (vt & VT_TYPEN) | s->t;
2275+ /* an array is never an lvalue */
2276+ if (!(vt & VT_ARRAY))
2277+ vt |= VT_LVAL;
18222278 next();
18232279 } else if (tok == '[') {
18242280 next();
@@ -1912,7 +2368,7 @@ void uneq() · 87 unmodified lines
19122368
19132369 void sum(l)
19142370 {
1915− int ft, fc, t;
2371+ int t;
19162372
19172373 if (l == 0)
19182374 uneq();
@@ -1926,7 +2382,9 @@ void sum(l) · 7 unmodified lines
19262382 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |
19272383 (l == 5 & tok == '&') |
19282384 (l == 6 & tok == '^') |
1929− (l == 7 & tok == '|')) {
2385+ (l == 7 & tok == '|') |
2386+ (l == 8 & tok == TOK_LAND) |
2387+ (l == 9 & tok == TOK_LOR)) {
19302388 vpush();
19312389 t = tok;
19322390 next();
@@ -1936,12 +2394,7 @@ void sum(l) · 3 unmodified lines
19362394 }
19372395 }
19382396
1939−#ifdef TINY
1940−void expr()
1941−{
1942− sum(8);
1943−}
1944−#else
2397+/* only used if non constant */
19452398 void eand()
19462399 {
19472400 int t;
@@ -1964,7 +2417,7 @@ void eand() · 16 unmodified lines
19642417
19652418 void eor()
19662419 {
1967− int t, u;
2420+ int t;
19682421
19692422 eand();
19702423 t = 0;
@@ -1982,22 +2435,37 @@ void eor() · 11 unmodified lines
19822435 }
19832436 }
19842437
2438+/* XXX: better constant handling */
19852439 void expr_eq()
19862440 {
1987− int t, u;
1988−
1989− eor();
1990− if (tok == '?') {
1991− next();
1992− t = gtst(1, 0);
1993− expr();
1994− gv();
1995− skip(':');
1996− u = gjmp(0);
1997− gsym(t);
1998− expr_eq();
1999− gv();
2000− gsym(u);
2441+ int t, u, c;
2442+
2443+ if (const_wanted) {
2444+ sum(10);
2445+ if (tok == '?') {
2446+ c = vc;
2447+ next();
2448+ expr();
2449+ t = vc;
2450+ skip(':');
2451+ expr_eq();
2452+ if (c)
2453+ vc = t;
2454+ }
2455+ } else {
2456+ eor();
2457+ if (tok == '?') {
2458+ next();
2459+ t = gtst(1, 0);
2460+ expr();
2461+ gv();
2462+ skip(':');
2463+ u = gjmp(0);
2464+ gsym(t);
2465+ expr_eq();
2466+ gv();
2467+ gsym(u);
2468+ }
20012469 }
20022470 }
20032471
@@ -2011,7 +2479,17 @@ void expr() · 7 unmodified lines
20112479 }
20122480 }
20132481
2014−#endif
2482+int expr_const()
2483+{
2484+ int a;
2485+ a = const_wanted;
2486+ const_wanted = 1;
2487+ expr_eq();
2488+ if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST)
2489+ expect("constant");
2490+ const_wanted = a;
2491+ return vc;
2492+}
20152493
20162494 void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
20172495 {
@@ -2079,9 +2557,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 61 unmodified lines
20792557 *csym = gjmp(*csym);
20802558 next();
20812559 skip(';');
2082− } else
2083−#ifndef TINY
2084− if (tok == TOK_FOR) {
2560+ } else if (tok == TOK_FOR) {
20852561 int e;
20862562 next();
20872563 skip('(');
@@ -2180,9 +2656,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 92 unmodified lines
21802656 oad(0xe9, s->c - ind - 5); /* jmp xxx */
21812657 next();
21822658 skip(';');
2183− } else
2184−#endif
2185− {
2659+ } else {
21862660 b = tok;
21872661 next();
21882662 if (tok == ':') {
@@ -2272,7 +2746,7 @@ void decl(l) · 83 unmodified lines
22722746 if (t & VT_EXTERN) {
22732747 /* external variable */
22742748 /* XXX: factorize with external function def */
2275− n = dlsym(NULL, get_tok_str(v));
2749+ n = (int)dlsym(NULL, get_tok_str(v, 0));
22762750 if (!n)
22772751 error("unknown external variable");
22782752 sym_push(v, VT_CONST | t, n);
@@ -2306,16 +2780,47 @@ void decl(l) · 27 unmodified lines
23062780 }
23072781 }
23082782
2309−int main(int c, char **v)
2783+int main(int argc, char **argv)
23102784 {
23112785 Sym *s;
23122786 int (*t)();
2313− if (c < 2) {
2314− printf("usage: tcc source ...\n");
2315− return 1;
2787+ char *p, *r;
2788+ int optind;
2789+
2790+ include_paths[0] = "/usr/include";
2791+ include_paths[1] = "/usr/lib/tcc";
2792+ include_paths[2] = "/usr/local/lib/tcc";
2793+ nb_include_paths = 3;
2794+
2795+ /* add all tokens */
2796+ p = "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0main\0";
2797+ while (*p) {
2798+ r = p;
2799+ while (*r++);
2800+ tok_alloc(p, r - p - 1);
2801+ p = r;
23162802 }
2317− v++;
2318− filename = *v;
2803+
2804+ optind = 1;
2805+ while (1) {
2806+ if (optind >= argc) {
2807+ printf("usage: tcc [-Idir] infile [infile_arg...]\n");
2808+ return 1;
2809+ }
2810+ r = argv[optind];
2811+ if (r[0] != '-')
2812+ break;
2813+ if (r[1] == 'I') {
2814+ if (nb_include_paths >= INCLUDE_PATHS_MAX)
2815+ error("too many include paths");
2816+ include_paths[nb_include_paths++] = r + 2;
2817+ } else {
2818+ error("invalid option");
2819+ }
2820+ optind++;
2821+ }
2822+
2823+ filename = argv[optind];
23192824 line_num = 1;
23202825 file = fopen(filename, "r");
23212826 if (!file) {
@@ -2325,16 +2830,10 @@ int main(int c, char **v) · 3 unmodified lines
23252830 include_stack_ptr = include_stack;
23262831 ifdef_stack_ptr = ifdef_stack;
23272832
2328− idtable = malloc(SYM_TABLE_SIZE);
2329− memcpy(idtable,
2330− "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0float\0double\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0main", 251);
2331− idptr = idtable + 251;
2332−
2333− glo = malloc(DATA_SIZE);
2833+ glo = (int)malloc(DATA_SIZE);
23342834 memset((void *)glo, 0, DATA_SIZE);
2335− prog = malloc(TEXT_SIZE);
2835+ prog = (int)malloc(TEXT_SIZE);
23362836 vstack_ptr = vstack;
2337− macro_stack_ptr = macro_stack;
23382837 anon_sym = 1 << (31 - VT_STRUCT_SHIFT);
23392838 ind = prog;
23402839 inp();
@@ -2356,6 +2855,6 @@ int main(int c, char **v) · 15 unmodified lines
23562855 if (!s)
23572856 error("main() not defined");
23582857 t = s->c;
2359− return (*t)(c - 1, v);
2858+ return (*t)(argc - optind, argv + optind);
23602859 #endif
23612860 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t