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
17 * along with this program; if not, write to the Free Software17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */19 */
20#include "tcclib.h"20#include <tcclib.h>
21 21
22#define INCLUDE_PATH "/usr/include"22//#define DEBUG
23/* preprocessor debug */
24//#define PP_DEBUG
23 25
24#define TEXT_SIZE 5000026#define TEXT_SIZE 50000
25#define DATA_SIZE 5000027#define DATA_SIZE 50000
26#define SYM_TABLE_SIZE 10000
27#define MACRO_STACK_SIZE 32
28#define INCLUDE_STACK_SIZE 3228#define INCLUDE_STACK_SIZE 32
29#define IFDEF_STACK_SIZE 6429#define IFDEF_STACK_SIZE 64
30#define VSTACK_SIZE 6430#define VSTACK_SIZE 64
31#define STRING_MAX_SIZE 1024
32#define INCLUDE_PATHS_MAX 32
31 33
32#define NB_REGS 334#define NB_REGS 3
33 35
36/* token symbol management */
37typedef struct TokenSym {
38 struct TokenSym *next;
39 int tok; /* token number */
40 int len;
41 char str[1];
42} TokenSym;
43
34/* symbol management */44/* symbol management */
35typedef struct Sym {45typedef struct Sym {
36 int v; /* symbol token */46 int v; /* symbol token */
@@ -47,6 +57,10 @@ typedef struct Sym { · 10 unmodified lines
47#define FUNC_OLD 2 /* old function prototype */57#define FUNC_OLD 2 /* old function prototype */
48#define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */58#define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
49 59
60/* field t for macros */
61#define MACRO_OBJ 0 /* object like macro */
62#define MACRO_FUNC 1 /* function like macro */
63
50typedef struct {64typedef struct {
51 FILE *file;65 FILE *file;
52 char *filename;66 char *filename;
@@ -62,14 +76,18 @@ typedef struct { · 9 unmodified lines
62*/76*/
63FILE *file;77FILE *file;
64int tok, tok1, tokc, rsym, anon_sym,78int tok, tok1, tokc, rsym, anon_sym,
65 prog, ind, loc, glo, vt, vc, line_num;79 prog, ind, loc, glo, vt, vc, const_wanted, line_num;
66char *idtable, *idptr, *filename;80TokenSym *first_ident;
81char token_buf[STRING_MAX_SIZE + 1];
82char *filename;
67Sym *define_stack, *global_stack, *local_stack, *label_stack;83Sym *define_stack, *global_stack, *local_stack, *label_stack;
68 84
69int vstack[VSTACK_SIZE], *vstack_ptr;85int vstack[VSTACK_SIZE], *vstack_ptr;
70char *macro_stack[MACRO_STACK_SIZE], **macro_stack_ptr, *macro_ptr;86int *macro_ptr, *macro_ptr_allocated;
71IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr;87IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr;
72int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr;88int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr;
89char *include_paths[INCLUDE_PATHS_MAX];
90int nb_include_paths;
73 91
74/* The current value can be: */92/* The current value can be: */
75#define VT_VALMASK 0x000f93#define VT_VALMASK 0x000f
@@ -132,7 +150,11 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 56 unmodified lines
132#define TOK_UMOD 0xb1 /* unsigned modulo */150#define TOK_UMOD 0xb1 /* unsigned modulo */
133#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */151#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
134#define TOK_NUM 0xb3 /* number in tokc */152#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 */
135 155
156#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
157
136#define TOK_SHL 0x01 /* shift left */158#define TOK_SHL 0x01 /* shift left */
137#define TOK_SAR 0x02 /* signed shift right */159#define TOK_SAR 0x02 /* signed shift right */
138 160
@@ -148,8 +170,11 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 9 unmodified lines
148#define TOK_A_SHL 0x81170#define TOK_A_SHL 0x81
149#define TOK_A_SAR 0x82171#define TOK_A_SAR 0x82
150 172
173/* all identificators and strings have token above that */
174#define TOK_IDENT 256
175
151enum {176enum {
152 TOK_INT = 256,177 TOK_INT = TOK_IDENT,
153 TOK_VOID,178 TOK_VOID,
154 TOK_CHAR,179 TOK_CHAR,
155 TOK_IF,180 TOK_IF,
@@ -180,6 +205,7 @@ enum { · 24 unmodified lines
180 TOK_FLOAT,205 TOK_FLOAT,
181 TOK_DOUBLE,206 TOK_DOUBLE,
182 207
208 TOK_SHORT,
183 TOK_STRUCT,209 TOK_STRUCT,
184 TOK_UNION,210 TOK_UNION,
185 TOK_TYPEDEF,211 TOK_TYPEDEF,
@@ -195,6 +221,7 @@ enum { · 9 unmodified lines
195 TOK_ELIF,221 TOK_ELIF,
196 TOK_ENDIF,222 TOK_ENDIF,
197 TOK_DEFINED,223 TOK_DEFINED,
224 TOK_UNDEF,
198 225
199 /* special identifiers */226 /* special identifiers */
200 TOK_MAIN,227 TOK_MAIN,
@@ -202,6 +229,7 @@ enum { · 1 unmodified line
202 229
203void sum();230void sum();
204void next();231void next();
232void next_nomacro();
205int expr_const();233int expr_const();
206void expr_eq();234void expr_eq();
207void expr();235void expr();
@@ -209,6 +237,14 @@ void decl(); · 1 unmodified line
209int gv();237int gv();
210void move_reg();238void move_reg();
211void save_reg();239void save_reg();
240void macro_subst(int **tok_str, int *tok_len,
241 Sym **nested_list, int *macro_str);
242int save_reg_forced(int r);
243int type_size(int t, int *a);
244int pointed_type(int t);
245int pointed_size(int t);
246int ist(void);
247int typ(int *v, int t);
212 248
213int isid(c)249int isid(c)
214{250{
@@ -222,8 +258,6 @@ int isnum(c) · 7 unmodified lines
222 return c >= '0' & c <= '9';258 return c >= '0' & c <= '9';
223}259}
224 260
225#ifndef TINY
226
227void printline()261void printline()
228{262{
229 IncludeFile *f;263 IncludeFile *f;
@@ -269,26 +303,98 @@ void test_lvalue() · 39 unmodified lines
269 expect("lvalue");303 expect("lvalue");
270}304}
271 305
272#else306TokenSym *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);
273 329
274#define skip(c) next()330 *pts = ts;
275#define test_lvalue() 331 return ts;
332}
276 333
277#endif334void 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}
278 356
279char *get_tok_str(int v)357/* XXX: buffer overflow */
358char *get_tok_str(int v, int c)
280{359{
281 int t;360 static char buf[STRING_MAX_SIZE + 1];
361 TokenSym *ts;
282 char *p;362 char *p;
283 p = idtable;363 int i;
284 t = 256;364
285 while (t != v) {365 if (v == TOK_NUM) {
286 if (p >= idptr)366 sprintf(buf, "%d", c);
287 return 0;367 return buf;
288 while (*p++);368 } else if (v == TOK_CCHAR) {
289 t++;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;
290 }397 }
291 return p;
292}398}
293 399
294/* find a symbol and return its associated structure. 's' is the top400/* 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
312 s->v = v;418 s->v = v;
313 s->t = t;419 s->t = t;
314 s->c = c;420 s->c = c;
315 s->next = 0;421 s->next = NULL;
316 s->prev = *ps;422 s->prev = *ps;
317 *ps = s;423 *ps = s;
318 return s;424 return s;
@@ -331,7 +437,6 @@ Sym *sym_find(int v) · 12 unmodified lines
331/* push a given symbol on the symbol stack */437/* push a given symbol on the symbol stack */
332Sym *sym_push(int v, int t, int c)438Sym *sym_push(int v, int t, int c)
333{439{
334 // printf("sym_push: %x %s type=%x\n", v, get_tok_str(v), t);
335 if (local_stack)440 if (local_stack)
336 return sym_push1(&local_stack, v, t, c);441 return sym_push1(&local_stack, v, t, c);
337 else442 else
@@ -346,7 +451,6 @@ void sym_pop(Sym **ps, Sym *b) · 8 unmodified lines
346 s = *ps;451 s = *ps;
347 while(s != b) {452 while(s != b) {
348 ss = s->prev;453 ss = s->prev;
349 // printf("sym_pop: %x %s type=%x\n", s->v, get_tok_str(s->v), s->t);
350 free(s);454 free(s);
351 s = ss;455 s = ss;
352 }456 }
@@ -358,8 +462,6 @@ int ch, ch1; · 5 unmodified lines
358/* read next char from current input file */462/* read next char from current input file */
359void inp()463void inp()
360{464{
361 int c;
362
363 redo:465 redo:
364 ch1 = fgetc(file);466 ch1 = fgetc(file);
365 if (ch1 == -1) {467 if (ch1 == -1) {
@@ -379,26 +481,17 @@ void inp() · 13 unmodified lines
379 // printf("ch1=%c 0x%x\n", ch1, ch1);481 // printf("ch1=%c 0x%x\n", ch1, ch1);
380}482}
381 483
382/* input with '\\n' handling and macro subtitution if in macro state */484/* input with '\\n' handling */
383void minp()485void minp()
384{486{
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 {
393 redo:487 redo:
394 ch = ch1;488 ch = ch1;
489 inp();
490 if (ch == '\\' && ch1 == '\n') {
395 inp();491 inp();
396 if (ch == '\\' && ch1 == '\n') {492 goto redo;
397 inp();
398 goto redo;
399 }
400 }493 }
401 // printf("ch=%c 0x%x\n", ch, ch);494 //printf("ch=%c 0x%x\n", ch, ch);
402}495}
403 496
404/* same as minp, but also skip comments */497/* same as minp, but also skip comments */
@@ -458,7 +551,7 @@ void preprocess_skip() · 53 unmodified lines
458 skip_spaces();551 skip_spaces();
459 if (ch == '#') {552 if (ch == '#') {
460 cinp();553 cinp();
461 next();554 next_nomacro();
462 if (a == 0 && 555 if (a == 0 &&
463 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))556 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
464 break;557 break;
@@ -497,53 +590,145 @@ char *get_str(c) · 32 unmodified lines
497 return str;590 return str;
498}591}
499 592
500/* return next token without macro substitution */593void tok_add(int **tok_str, int *tok_len, int t)
501void next_nomacro()
502{594{
503 Sym *s;595 int len, *str;
504 /* hack to avoid replacing id by defined value */596 len = *tok_len;
505 s = define_stack;597 str = *tok_str;
506 define_stack = 0;598 if ((len & 63) == 0) {
507 next();599 str = realloc(str, (len + 64) * sizeof(int));
508 define_stack = s;600 if (!str)
601 return;
602 *tok_str = str;
603 }
604 str[len++] = t;
605 *tok_len = len;
509}606}
510 607
511/* XXX: not correct yet (need to ensure it is constant) */608void tok_add2(int **tok_str, int *tok_len, int t, int c)
512int expr_preprocess()
513{609{
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}
515 614
516 if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE)615/* eval an expression for #if/#elif */
517 error("too many nested macros");616int expr_preprocess()
518 *macro_stack_ptr++ = (char *)'\n';617{
519 *macro_stack_ptr++ = macro_ptr;618 int *str, len, c, t;
520 macro_ptr = get_str(';');619 int *saved_macro_ptr;
521 cinp();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;
522 next();650 next();
523 c = expr_const();651 c = expr_const();
524 ch = '\n';652 macro_ptr = saved_macro_ptr;
653 free(str);
525 return c != 0;654 return c != 0;
526}655}
527 656
657#ifdef DEBUG
658void 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
528void preprocess()675void preprocess()
529{676{
530 char *str;677 int size, i, c, v, t, *str, len;
531 int size, n, c, v;
532 char buf[1024], *q, *p;678 char buf[1024], *q, *p;
533 char buf1[1024];679 char buf1[1024];
534 FILE *f;680 FILE *f;
681 Sym **ps, *first, *s;
535 682
536 cinp();683 cinp();
537 next_nomacro();684 next_nomacro();
538 redo:685 redo:
539 if (tok == TOK_DEFINE) {686 if (tok == TOK_DEFINE) {
540 next_nomacro(); /* XXX: should pass parameter to avoid macro subst */687 next_nomacro();
541 skip_spaces();688 v = tok;
542 /* now 'tok' is the macro symbol */689 /* XXX: should check if same macro (ANSI) */
543 /* a space is inserted after each macro */690 if (sym_find1(define_stack, v))
544 str = get_str(' ');691 warning("macro redefinition");
545 // printf("define %s '%s'\n", get_tok_str(tok), str);692 first = NULL;
546 sym_push1(&define_stack, tok, 0, (int)str);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;
547 } else if (tok == TOK_INCLUDE) {732 } else if (tok == TOK_INCLUDE) {
548 skip_spaces();733 skip_spaces();
549 if (ch == '<') {734 if (ch == '<') {
@@ -577,12 +762,16 @@ void preprocess() · 27 unmodified lines
577 goto found;762 goto found;
578 }763 }
579 /* now search in standard include path */764 /* now search in standard include path */
580 strcpy(buf1, INCLUDE_PATH);765 for(i=nb_include_paths - 1;i>=0;i--) {
581 strcat(buf1, "/");766 strcpy(buf1, include_paths[i]);
582 strcat(buf1, buf);767 strcat(buf1, "/");
583 f = fopen(buf1, "r");768 strcat(buf1, buf);
584 if (!f)769 f = fopen(buf1, "r");
585 error("include file not found");770 if (f)
771 goto found;
772 }
773 error("include file not found");
774 f = NULL;
586 found:775 found:
587 /* push current file in stack */776 /* push current file in stack */
588 /* XXX: fix current line init */777 /* XXX: fix current line init */
@@ -659,19 +848,46 @@ int getn(b) · 70 unmodified lines
659 return n;848 return n;
660}849}
661 850
662void next()851/* read a character for string or char constant and eval escape codes */
852int getq()
663{853{
664 int v, b;854 int c;
665 char *q, *p;
666 Sym *s;
667 855
668 /* special 'ungettok' case for label parsing */856 c = ch;
669 if (tok1) {857 minp();
670 tok = tok1;858 if (c == '\\') {
671 tok1 = 0;859 if (isnum(ch)) {
672 return;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 }
673 }880 }
674 redo:881 return c;
882}
883
884/* return next token without macro substitution */
885void next_nomacro1()
886{
887 int b;
888 char *q;
889 TokenSym *ts;
890
675 /* skip spaces */891 /* skip spaces */
676 while(1) {892 while(1) {
677 while (ch == '\n') {893 while (ch == '\n') {
@@ -684,58 +900,61 @@ void next() · 6 unmodified lines
684 preprocess();900 preprocess();
685 }901 }
686 }902 }
687 if (ch != ' ' && ch != 9)903 if (ch != ' ' && ch != '\t' && ch != '\f')
688 break;904 break;
689 cinp();905 cinp();
690 }906 }
691 if (isid(ch)) {907 if (isid(ch)) {
692 q = idptr;908 q = token_buf;
693 while(isid(ch) | isnum(ch)) {909 while (isid(ch) | isnum(ch)) {
910 if (q >= token_buf + STRING_MAX_SIZE)
911 error("ident too long");
694 *q++ = ch;912 *q++ = ch;
695 cinp();913 cinp();
696 }914 }
697 *q++ = '\0';915 *q = '\0';
698 p = idtable;916 ts = tok_alloc(token_buf, q - token_buf);
699 tok = 256;917 tok = ts->tok;
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 }
720 } else if (isnum(ch)) {918 } else if (isnum(ch)) {
721 /* number */919 /* number */
722 b = 10;920 b = 10;
723 if (ch == '0') {921 if (ch == '0') {
724 cinp();922 cinp();
725 b = 8;923 b = 8;
726 if (ch == 'x') {924 if (ch == 'x' || ch == 'X') {
727 cinp();925 cinp();
728 b = 16;926 b = 16;
729 }927 }
730 }928 }
731 tokc = getn(b);929 tokc = getn(b);
930 /* XXX: add unsigned constant support (ANSI) */
931 while (ch == 'L' || ch == 'l' || ch == 'U' || ch == 'u')
932 cinp();
732 tok = TOK_NUM;933 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();
733 } else {956 } else {
734#ifdef TINY957 q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250##\266";
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
739 /* two chars */958 /* two chars */
740 tok = ch;959 tok = ch;
741 cinp();960 cinp();
@@ -764,7 +983,278 @@ void next() · 22 unmodified lines
764 else if (tok == '>')983 else if (tok == '>')
765 tok = TOK_GT;984 tok = TOK_GT;
766 }985 }
767 // printf("tok=%x\n", tok);986}
987
988/* return next token without macro substitution. Can read input from
989 macro_ptr buffer */
990void 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 */
1005int *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 */
1072int *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. */
1125void 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 */
1220void 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
768}1258}
769 1259
770void swap(int *p, int *q)1260void swap(int *p, int *q)
@@ -1004,7 +1494,7 @@ void gen_op1(op, r, fr) · 233 unmodified lines
1004/* end of X86 code generator */1494/* end of X86 code generator */
1005/*************************************************************/1495/*************************************************************/
1006 1496
1007int save_reg_forced(r)1497int save_reg_forced(int r)
1008{1498{
1009 int i, l, *p, t;1499 int i, l, *p, t;
1010 /* store register */1500 /* store register */
@@ -1156,6 +1646,20 @@ void gen_opc(op) · 145 unmodified lines
1156 case TOK_SHL: vc <<= fc; break;1646 case TOK_SHL: vc <<= fc; break;
1157 case TOK_SHR: vc = (unsigned)vc >> fc; break;1647 case TOK_SHR: vc = (unsigned)vc >> fc; break;
1158 case TOK_SAR: vc >>= fc; break;1648 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;
1159 default:1663 default:
1160 goto general_case;1664 goto general_case;
1161 }1665 }
@@ -1208,13 +1712,13 @@ void gen_opc(op) · 46 unmodified lines
1208 }1712 }
1209}1713}
1210 1714
1211int pointed_size(t)1715int pointed_size(int t)
1212{1716{
1213 return type_size(pointed_type(t), &t);1717 return type_size(pointed_type(t), &t);
1214}1718}
1215 1719
1216/* generic gen_op: handles types problems */1720/* generic gen_op: handles types problems */
1217void gen_op(op)1721void gen_op(int op)
1218{1722{
1219 int u, t1, t2;1723 int u, t1, t2;
1220 1724
@@ -1354,14 +1858,6 @@ void inc(post, c) · 133 unmodified lines
1354 vpop(&vt, &vc);1858 vpop(&vt, &vc);
1355}1859}
1356 1860
1357int expr_const()
1358{
1359 expr_eq();
1360 if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST)
1361 expect("constant");
1362 return vc;
1363}
1364
1365/* enum/struct/union declaration */1861/* enum/struct/union declaration */
1366int struct_decl(u)1862int struct_decl(u)
1367{1863{
@@ -1393,7 +1889,7 @@ int struct_decl(u) · 25 unmodified lines
1393 /* cannot be empty */1889 /* cannot be empty */
1394 c = 0;1890 c = 0;
1395 maxalign = 0;1891 maxalign = 0;
1396 slast = 0;1892 slast = NULL;
1397 while (1) {1893 while (1) {
1398 if (a == TOK_ENUM) {1894 if (a == TOK_ENUM) {
1399 v = tok;1895 v = tok;
@@ -1449,30 +1945,27 @@ int struct_decl(u) · 49 unmodified lines
1449 and skip it. 1945 and skip it.
1450 XXX: A '2' is ored to ensure non zero return if int type.1946 XXX: A '2' is ored to ensure non zero return if int type.
1451 */1947 */
1452int ist()1948int ist(void)
1453{1949{
1454 int t, n, v;1950 int t;
1455 Sym *s;1951 Sym *s;
1456 1952
1457 t = 0;1953 t = 0;
1458 while(1) {1954 while(1) {
1459#ifndef TINY
1460 if (tok == TOK_ENUM) {1955 if (tok == TOK_ENUM) {
1461 t |= struct_decl(VT_ENUM);1956 t |= struct_decl(VT_ENUM);
1462 } else if (tok == TOK_STRUCT || tok == TOK_UNION) {1957 } else if (tok == TOK_STRUCT || tok == TOK_UNION) {
1463 t |= struct_decl(VT_STRUCT);1958 t |= struct_decl(VT_STRUCT);
1464 } else1959 } else {
1465#endif
1466 {
1467 if (tok == TOK_CHAR) {1960 if (tok == TOK_CHAR) {
1468 t |= VT_BYTE;1961 t |= VT_BYTE;
1469 } else if (tok == TOK_VOID) {1962 } else if (tok == TOK_VOID) {
1470 t |= VT_VOID;1963 t |= VT_VOID;
1471 } else if (tok == TOK_INT |1964 } else if (tok == TOK_INT | tok == TOK_SHORT |
1472 (tok >= TOK_CONST & tok <= TOK_INLINE)) {1965 (tok >= TOK_CONST & tok <= TOK_INLINE)) {
1473 /* ignored types */1966 /* ignored types */
1474 } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) {1967 } else if (tok == TOK_FLOAT || tok == TOK_DOUBLE) {
1475 error("floats not supported");1968 warning("floats not supported");
1476 } else if (tok == TOK_EXTERN) {1969 } else if (tok == TOK_EXTERN) {
1477 t |= VT_EXTERN;1970 t |= VT_EXTERN;
1478 } else if (tok == TOK_STATIC) {1971 } else if (tok == TOK_STATIC) {
@@ -1618,11 +2111,11 @@ int typ(int *v, int t) · 139 unmodified lines
1618/* define a new external reference to a function 'v' of type 'u' */2111/* define a new external reference to a function 'v' of type 'u' */
1619Sym *external_func(v, u)2112Sym *external_func(v, u)
1620{2113{
1621 int t, n, p;2114 int n;
1622 Sym *s;2115 Sym *s;
1623 s = sym_find(v);2116 s = sym_find(v);
1624 if (!s) {2117 if (!s) {
1625 n = dlsym(0, get_tok_str(v));2118 n = (int)dlsym(0, get_tok_str(v, 0));
1626 if (n == 0) {2119 if (n == 0) {
1627 /* used to generate symbol list */2120 /* used to generate symbol list */
1628 s = sym_push1(&global_stack, 2121 s = sym_push1(&global_stack,
@@ -1636,31 +2129,6 @@ Sym *external_func(v, u) · 7 unmodified lines
1636 return s;2129 return s;
1637}2130}
1638 2131
1639/* read a character for string or char constant and eval escape codes */
1640int 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
1664void indir()2132void indir()
1665{2133{
1666 if (vt & VT_LVAL)2134 if (vt & VT_LVAL)
@@ -1677,37 +2145,20 @@ void unary() · 10 unmodified lines
1677 int n, t, ft, fc, p, r;2145 int n, t, ft, fc, p, r;
1678 Sym *s;2146 Sym *s;
1679 2147
1680 if (tok == TOK_NUM) {2148 if (tok == TOK_NUM || tok == TOK_CCHAR) {
1681 vset(VT_CONST, tokc);2149 vset(VT_CONST, tokc);
1682 next();2150 next();
1683 } else 2151 } else if (tok == TOK_STR) {
1684 if (tok == '\'') {2152 TokenSym *ts;
1685 vset(VT_CONST, getq());
1686 next(); /* skip char */
1687 skip('\'');
1688 } else if (tok == '\"') {
1689 /* generate (char *) type */2153 /* generate (char *) type */
1690 vset(VT_CONST | mk_pointer(VT_TYPE), glo);2154 vset(VT_CONST | mk_pointer(VT_TYPE), glo);
1691 while (tok == '\"') {2155 while (tok == TOK_STR) {
1692 while (ch != '\"') {2156 ts = (TokenSym *)tokc;
1693 if (ch == -1)2157 memcpy((void *)glo, ts->str, ts->len);
1694 error("unterminated string");2158 glo += ts->len;
1695 *(char *)glo++ = getq();
1696 }
1697 minp();
1698 next();2159 next();
1699 }2160 }
1700 *(char *)glo++ = 0;2161 *(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(')');
1711 } else {2162 } else {
1712 t = tok;2163 t = tok;
1713 next();2164 next();
@@ -1732,11 +2183,13 @@ void unary() · 18 unmodified lines
1732 } else2183 } else
1733 if (t == '!') {2184 if (t == '!') {
1734 unary();2185 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)
1736 vc = vc ^ 1;2189 vc = vc ^ 1;
1737 else2190 else
1738 vset(VT_JMP, gtst(1, 0));2191 vset(VT_JMP, gtst(1, 0));
1739 } else 2192 } else
1740 if (t == '~') {2193 if (t == '~') {
1741 unary();2194 unary();
1742 vpush();2195 vpush();
@@ -1818,7 +2271,10 @@ void unary() · 75 unmodified lines
1818 vset(VT_CONST, s->c);2271 vset(VT_CONST, s->c);
1819 gen_op('+');2272 gen_op('+');
1820 /* change type to field type, and set to lvalue */2273 /* 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;
1822 next();2278 next();
1823 } else if (tok == '[') {2279 } else if (tok == '[') {
1824 next();2280 next();
@@ -1912,7 +2368,7 @@ void uneq() · 87 unmodified lines
1912 2368
1913void sum(l)2369void sum(l)
1914{2370{
1915 int ft, fc, t;2371 int t;
1916 2372
1917 if (l == 0)2373 if (l == 0)
1918 uneq();2374 uneq();
@@ -1926,7 +2382,9 @@ void sum(l) · 7 unmodified lines
1926 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |2382 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |
1927 (l == 5 & tok == '&') |2383 (l == 5 & tok == '&') |
1928 (l == 6 & tok == '^') |2384 (l == 6 & tok == '^') |
1929 (l == 7 & tok == '|')) {2385 (l == 7 & tok == '|') |
2386 (l == 8 & tok == TOK_LAND) |
2387 (l == 9 & tok == TOK_LOR)) {
1930 vpush();2388 vpush();
1931 t = tok;2389 t = tok;
1932 next();2390 next();
@@ -1936,12 +2394,7 @@ void sum(l) · 3 unmodified lines
1936 }2394 }
1937}2395}
1938 2396
1939#ifdef TINY 2397/* only used if non constant */
1940void expr()
1941{
1942 sum(8);
1943}
1944#else
1945void eand()2398void eand()
1946{2399{
1947 int t;2400 int t;
@@ -1964,7 +2417,7 @@ void eand() · 16 unmodified lines
1964 2417
1965void eor()2418void eor()
1966{2419{
1967 int t, u;2420 int t;
1968 2421
1969 eand();2422 eand();
1970 t = 0;2423 t = 0;
@@ -1982,22 +2435,37 @@ void eor() · 11 unmodified lines
1982 }2435 }
1983}2436}
1984 2437
2438/* XXX: better constant handling */
1985void expr_eq()2439void expr_eq()
1986{2440{
1987 int t, u;2441 int t, u, c;
1988 2442
1989 eor();2443 if (const_wanted) {
1990 if (tok == '?') {2444 sum(10);
1991 next();2445 if (tok == '?') {
1992 t = gtst(1, 0);2446 c = vc;
1993 expr();2447 next();
1994 gv();2448 expr();
1995 skip(':');2449 t = vc;
1996 u = gjmp(0);2450 skip(':');
1997 gsym(t);2451 expr_eq();
1998 expr_eq();2452 if (c)
1999 gv();2453 vc = t;
2000 gsym(u);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 }
2001 }2469 }
2002}2470}
2003 2471
@@ -2011,7 +2479,17 @@ void expr() · 7 unmodified lines
2011 }2479 }
2012}2480}
2013 2481
2014#endif2482int 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}
2015 2493
2016void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)2494void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
2017{2495{
@@ -2079,9 +2557,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 61 unmodified lines
2079 *csym = gjmp(*csym);2557 *csym = gjmp(*csym);
2080 next();2558 next();
2081 skip(';');2559 skip(';');
2082 } else 2560 } else if (tok == TOK_FOR) {
2083#ifndef TINY
2084 if (tok == TOK_FOR) {
2085 int e;2561 int e;
2086 next();2562 next();
2087 skip('(');2563 skip('(');
@@ -2180,9 +2656,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 92 unmodified lines
2180 oad(0xe9, s->c - ind - 5); /* jmp xxx */2656 oad(0xe9, s->c - ind - 5); /* jmp xxx */
2181 next();2657 next();
2182 skip(';');2658 skip(';');
2183 } else2659 } else {
2184#endif
2185 {
2186 b = tok;2660 b = tok;
2187 next();2661 next();
2188 if (tok == ':') {2662 if (tok == ':') {
@@ -2272,7 +2746,7 @@ void decl(l) · 83 unmodified lines
2272 if (t & VT_EXTERN) {2746 if (t & VT_EXTERN) {
2273 /* external variable */2747 /* external variable */
2274 /* XXX: factorize with external function def */2748 /* XXX: factorize with external function def */
2275 n = dlsym(NULL, get_tok_str(v));2749 n = (int)dlsym(NULL, get_tok_str(v, 0));
2276 if (!n)2750 if (!n)
2277 error("unknown external variable");2751 error("unknown external variable");
2278 sym_push(v, VT_CONST | t, n);2752 sym_push(v, VT_CONST | t, n);
@@ -2306,16 +2780,47 @@ void decl(l) · 27 unmodified lines
2306 }2780 }
2307}2781}
2308 2782
2309int main(int c, char **v)2783int main(int argc, char **argv)
2310{2784{
2311 Sym *s;2785 Sym *s;
2312 int (*t)();2786 int (*t)();
2313 if (c < 2) {2787 char *p, *r;
2314 printf("usage: tcc source ...\n");2788 int optind;
2315 return 1;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;
2316 }2802 }
2317 v++;2803
2318 filename = *v;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];
2319 line_num = 1;2824 line_num = 1;
2320 file = fopen(filename, "r");2825 file = fopen(filename, "r");
2321 if (!file) {2826 if (!file) {
@@ -2325,16 +2830,10 @@ int main(int c, char **v) · 3 unmodified lines
2325 include_stack_ptr = include_stack;2830 include_stack_ptr = include_stack;
2326 ifdef_stack_ptr = ifdef_stack;2831 ifdef_stack_ptr = ifdef_stack;
2327 2832
2328 idtable = malloc(SYM_TABLE_SIZE);2833 glo = (int)malloc(DATA_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);
2334 memset((void *)glo, 0, DATA_SIZE);2834 memset((void *)glo, 0, DATA_SIZE);
2335 prog = malloc(TEXT_SIZE);2835 prog = (int)malloc(TEXT_SIZE);
2336 vstack_ptr = vstack;2836 vstack_ptr = vstack;
2337 macro_stack_ptr = macro_stack;
2338 anon_sym = 1 << (31 - VT_STRUCT_SHIFT); 2837 anon_sym = 1 << (31 - VT_STRUCT_SHIFT);
2339 ind = prog;2838 ind = prog;
2340 inp();2839 inp();
@@ -2356,6 +2855,6 @@ int main(int c, char **v) · 15 unmodified lines
2356 if (!s)2855 if (!s)
2357 error("main() not defined");2856 error("main() not defined");
2358 t = s->c;2857 t = s->c;
2359 return (*t)(c - 1, v);2858 return (*t)(argc - optind, argv + optind);
2360#endif2859#endif
2361}2860}

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