History

fixed \nnn char parsing, added #line, added __VA_ARGS__, added macros in #include, added \xnn parsing, added L'' and L parsing (still not OK), added 0bXX, added ISOC99 initializers handling, added declaration parsing anywhere in block

bellard committed 25 years ago

Commit 44b5c971 parent ef6e8581 file · +289 −57Browse files

Unified · Split

tcc.c+289−57
@@ -233,10 +233,12 @@ enum { · 232 unmodified lines
233 TOK_DEFINED,233 TOK_DEFINED,
234 TOK_UNDEF,234 TOK_UNDEF,
235 TOK_ERROR,235 TOK_ERROR,
236 TOK_LINE,
236 TOK___LINE__,237 TOK___LINE__,
237 TOK___FILE__,238 TOK___FILE__,
238 TOK___DATE__,239 TOK___DATE__,
239 TOK___TIME__,240 TOK___TIME__,
241 TOK___VA_ARGS__,
240 242
241 /* special identifiers */243 /* special identifiers */
242 TOK___FUNC__,244 TOK___FUNC__,
@@ -250,6 +252,7 @@ int expr_const(); · 7 unmodified lines
250void expr_eq();252void expr_eq();
251void expr();253void expr();
252void decl();254void decl();
255void decl_assign(int t, int c, int first);
253int gv();256int gv();
254void move_reg();257void move_reg();
255void save_reg();258void save_reg();
@@ -715,6 +718,8 @@ void preprocess() · 459 unmodified lines
715 next_nomacro();718 next_nomacro();
716 ps = &first;719 ps = &first;
717 while (tok != ')') {720 while (tok != ')') {
721 if (tok == TOK_DOTS)
722 tok = TOK___VA_ARGS__;
718 s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);723 s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
719 *ps = s;724 *ps = s;
720 ps = &s->next;725 ps = &s->next;
@@ -763,44 +768,51 @@ void preprocess() · 42 unmodified lines
763 minp();768 minp();
764 }769 }
765 *q = '\0';770 *q = '\0';
766 if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)771 } else {
767 error("memory full");772 next();
768 if (c == '\"') {773 if (tok != TOK_STR)
769 /* first search in current dir if "header.h" */774 error("#include syntax error");
770 /* XXX: buffer overflow */775 /* XXX: buffer overflow */
771 size = 0;776 strcpy(buf, get_tok_str(tok, tokc));
772 p = strrchr(filename, '/');777 c = '\"';
773 if (p)
774 size = p + 1 - filename;
775 memcpy(buf1, filename, size);
776 buf1[size] = '\0';
777 strcat(buf1, buf);
778 f = fopen(buf1, "r");
779 if (f)
780 goto found;
781 }
782 /* now search in standard include path */
783 for(i=nb_include_paths - 1;i>=0;i--) {
784 strcpy(buf1, include_paths[i]);
785 strcat(buf1, "/");
786 strcat(buf1, buf);
787 f = fopen(buf1, "r");
788 if (f)
789 goto found;
790 }
791 error("include file not found");
792 f = NULL;
793 found:
794 /* push current file in stack */
795 /* XXX: fix current line init */
796 include_stack_ptr->file = file;
797 include_stack_ptr->filename = filename;
798 include_stack_ptr->line_num = line_num;
799 include_stack_ptr++;
800 file = f;
801 filename = strdup(buf1);
802 line_num = 1;
803 }778 }
779 if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)
780 error("memory full");
781 if (c == '\"') {
782 /* first search in current dir if "header.h" */
783 /* XXX: buffer overflow */
784 size = 0;
785 p = strrchr(filename, '/');
786 if (p)
787 size = p + 1 - filename;
788 memcpy(buf1, filename, size);
789 buf1[size] = '\0';
790 strcat(buf1, buf);
791 f = fopen(buf1, "r");
792 if (f)
793 goto found;
794 }
795 /* now search in standard include path */
796 for(i=nb_include_paths - 1;i>=0;i--) {
797 strcpy(buf1, include_paths[i]);
798 strcat(buf1, "/");
799 strcat(buf1, buf);
800 f = fopen(buf1, "r");
801 if (f)
802 goto found;
803 }
804 error("include file not found");
805 f = NULL;
806 found:
807 /* push current file in stack */
808 /* XXX: fix current line init */
809 include_stack_ptr->file = file;
810 include_stack_ptr->filename = filename;
811 include_stack_ptr->line_num = line_num;
812 include_stack_ptr++;
813 file = f;
814 filename = strdup(buf1);
815 line_num = 1;
804 } else if (tok == TOK_IFNDEF) {816 } else if (tok == TOK_IFNDEF) {
805 c = 1;817 c = 1;
806 goto do_ifdef;818 goto do_ifdef;
@@ -838,6 +850,19 @@ void preprocess() · 31 unmodified lines
838 if (ifdef_stack_ptr == ifdef_stack)850 if (ifdef_stack_ptr == ifdef_stack)
839 expect("#if");851 expect("#if");
840 ifdef_stack_ptr--;852 ifdef_stack_ptr--;
853 } else if (tok == TOK_LINE) {
854 next();
855 if (tok != TOK_NUM)
856 error("#line");
857 line_num = tokc;
858 skip_spaces();
859 if (ch != '\n') {
860 next();
861 if (tok != TOK_STR)
862 error("#line");
863 /* XXX: potential memory leak */
864 filename = strdup(get_tok_str(tok, tokc));
865 }
841 } else if (tok == TOK_ERROR) {866 } else if (tok == TOK_ERROR) {
842 error("#error");867 error("#error");
843 }868 }
@@ -877,7 +902,21 @@ int getq() · 33 unmodified lines
877 minp();902 minp();
878 if (c == '\\') {903 if (c == '\\') {
879 if (isnum(ch)) {904 if (isnum(ch)) {
880 return getn(8);905 /* at most three octal digits */
906 c = ch - '0';
907 minp();
908 if (isnum(ch)) {
909 c = c * 8 + ch - '0';
910 minp();
911 if (isnum(ch)) {
912 c = c * 8 + ch - '0';
913 minp();
914 }
915 }
916 return c;
917 } else if (ch == 'x') {
918 minp();
919 return getn(16);
881 } else {920 } else {
882 if (ch == 'a')921 if (ch == 'a')
883 c = '\a';922 c = '\a';
@@ -926,6 +965,16 @@ void next_nomacro1() · 42 unmodified lines
926 }965 }
927 if (isid(ch)) {966 if (isid(ch)) {
928 q = token_buf;967 q = token_buf;
968 *q++ = ch;
969 cinp();
970 if (q[-1] == 'L') {
971 /* XXX: not supported entirely (needs different
972 preprocessor architecture) */
973 if (ch == '\'')
974 goto char_const;
975 if (ch == '\"')
976 goto str_const;
977 }
929 while (isid(ch) | isnum(ch)) {978 while (isid(ch) | isnum(ch)) {
930 if (q >= token_buf + STRING_MAX_SIZE)979 if (q >= token_buf + STRING_MAX_SIZE)
931 error("ident too long");980 error("ident too long");
@@ -944,6 +993,9 @@ void next_nomacro1() · 12 unmodified lines
944 if (ch == 'x' || ch == 'X') {993 if (ch == 'x' || ch == 'X') {
945 cinp();994 cinp();
946 b = 16;995 b = 16;
996 } else if (ch == 'b' || ch == 'B') {
997 cinp();
998 b = 2;
947 }999 }
948 }1000 }
949 tokc = getn(b);1001 tokc = getn(b);
@@ -952,6 +1004,7 @@ void next_nomacro1() · 2 unmodified lines
952 cinp();1004 cinp();
953 tok = TOK_NUM;1005 tok = TOK_NUM;
954 } else if (ch == '\'') {1006 } else if (ch == '\'') {
1007 char_const:
955 minp();1008 minp();
956 tokc = getq();1009 tokc = getq();
957 tok = TOK_CCHAR;1010 tok = TOK_CCHAR;
@@ -959,6 +1012,7 @@ void next_nomacro1() · 1 unmodified line
959 expect("\'");1012 expect("\'");
960 minp();1013 minp();
961 } else if (ch == '\"') {1014 } else if (ch == '\"') {
1015 str_const:
962 minp();1016 minp();
963 q = token_buf;1017 q = token_buf;
964 while (ch != '\"') {1018 while (ch != '\"') {
@@ -1203,7 +1257,10 @@ void macro_subst(int **tok_str, int *tok_len, · 238 unmodified lines
1203 len = 0;1257 len = 0;
1204 str = NULL;1258 str = NULL;
1205 parlevel = 0;1259 parlevel = 0;
1206 while ((parlevel > 0 || (tok != ')' && tok != ',')) && 1260 while ((parlevel > 0 ||
1261 (tok != ')' &&
1262 (tok != ',' ||
1263 sa->v == (TOK___VA_ARGS__ | SYM_FIELD)))) &&
1207 tok != -1) {1264 tok != -1) {
1208 if (tok == '(')1265 if (tok == '(')
1209 parlevel++;1266 parlevel++;
@@ -1916,7 +1973,7 @@ void inc(post, c) · 706 unmodified lines
1916int struct_decl(u)1973int struct_decl(u)
1917{1974{
1918 int a, t, b, v, size, align, maxalign, c;1975 int a, t, b, v, size, align, maxalign, c;
1919 Sym *slast, *s, *ss;1976 Sym *s, *ss, **ps;
1920 1977
1921 a = tok; /* save decl type */1978 a = tok; /* save decl type */
1922 next();1979 next();
@@ -1945,7 +2002,7 @@ int struct_decl(u) · 22 unmodified lines
1945 /* cannot be empty */2002 /* cannot be empty */
1946 c = 0;2003 c = 0;
1947 maxalign = 0;2004 maxalign = 0;
1948 slast = NULL;2005 ps = &s->next;
1949 while (1) {2006 while (1) {
1950 if (a == TOK_ENUM) {2007 if (a == TOK_ENUM) {
1951 v = tok;2008 v = tok;
@@ -1978,8 +2035,8 @@ int struct_decl(u) · 26 unmodified lines
1978 }2035 }
1979 if (align > maxalign)2036 if (align > maxalign)
1980 maxalign = align;2037 maxalign = align;
1981 ss->next = slast;2038 *ps = ss;
1982 slast = ss;2039 ps = &ss->next;
1983 if (tok == ';' || tok == -1)2040 if (tok == ';' || tok == -1)
1984 break;2041 break;
1985 skip(',');2042 skip(',');
@@ -1990,7 +2047,6 @@ int struct_decl(u) · 4 unmodified lines
1990 break;2047 break;
1991 }2048 }
1992 skip('}');2049 skip('}');
1993 s->next = slast;
1994 /* size for struct/union, dummy for enum */2050 /* size for struct/union, dummy for enum */
1995 s->c = (c + maxalign - 1) & -maxalign; 2051 s->c = (c + maxalign - 1) & -maxalign;
1996 }2052 }
@@ -2215,13 +2271,13 @@ void unary() · 218 unmodified lines
2215 } else if (tok == TOK___FUNC__) {2271 } else if (tok == TOK___FUNC__) {
2216 /* special function name identifier */2272 /* special function name identifier */
2217 /* generate (char *) type */2273 /* generate (char *) type */
2218 vset(VT_CONST | mk_pointer(VT_TYPE), glo);2274 vset(VT_CONST | mk_pointer(VT_BYTE), glo);
2219 strcpy((void *)glo, funcname);2275 strcpy((void *)glo, funcname);
2220 glo += strlen(funcname) + 1;2276 glo += strlen(funcname) + 1;
2221 } else if (tok == TOK_STR) {2277 } else if (tok == TOK_STR) {
2222 TokenSym *ts;2278 TokenSym *ts;
2223 /* generate (char *) type */2279 /* generate (char *) type */
2224 vset(VT_CONST | mk_pointer(VT_TYPE), glo);2280 vset(VT_CONST | mk_pointer(VT_BYTE), glo);
2225 while (tok == TOK_STR) {2281 while (tok == TOK_STR) {
2226 ts = (TokenSym *)tokc;2282 ts = (TokenSym *)tokc;
2227 memcpy((void *)glo, ts->str, ts->len);2283 memcpy((void *)glo, ts->str, ts->len);
@@ -2296,7 +2352,7 @@ void unary() · 68 unmodified lines
2296 s = sym_find(t);2352 s = sym_find(t);
2297 if (!s) {2353 if (!s) {
2298 if (tok != '(')2354 if (tok != '(')
2299 error("undefined symbol");2355 error("'%s' undeclared", get_tok_str(t, 0));
2300 /* for simple function calls, we tolerate undeclared2356 /* for simple function calls, we tolerate undeclared
2301 external reference */2357 external reference */
2302 p = anon_sym++; 2358 p = anon_sym++;
@@ -2637,9 +2693,10 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 334 unmodified lines
2637 next();2693 next();
2638 /* declarations */2694 /* declarations */
2639 s = local_stack;2695 s = local_stack;
2640 decl(VT_LOCAL);2696 while (tok != '}') {
2641 while (tok != '}')2697 decl(VT_LOCAL);
2642 block(bsym, csym, case_sym, def_sym, case_reg);2698 block(bsym, csym, case_sym, def_sym, case_reg);
2699 }
2643 /* pop locally defined symbols */2700 /* pop locally defined symbols */
2644 sym_pop(&local_stack, s);2701 sym_pop(&local_stack, s);
2645 next();2702 next();
@@ -2794,10 +2851,164 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 148 unmodified lines
2794 }2851 }
2795}2852}
2796 2853
2854/* t is the array or struct type. c is the array or struct
2855 address. cur_index/cur_field is the pointer to the current value */
2856void decl_designator(int t, int c, int *cur_index, Sym **cur_field)
2857{
2858 Sym *s, *f;
2859 int notfirst, index, align;
2860
2861 notfirst = 0;
2862 while (tok == '[' || tok == '.') {
2863 if (tok == '[') {
2864 if (!(t & VT_ARRAY))
2865 expect("array type");
2866 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
2867 next();
2868 index = expr_const();
2869 if (index < 0 || (s->c >= 0 && index >= s->c))
2870 expect("invalid index");
2871 skip(']');
2872 if (!notfirst)
2873 *cur_index = index;
2874 t = pointed_type(t);
2875 c += index * type_size(t, &align);
2876 } else {
2877 if (!(t & VT_STRUCT))
2878 expect("struct/union type");
2879 next();
2880 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
2881 tok |= SYM_FIELD;
2882 f = s->next;
2883 while (f) {
2884 if (f->v == tok)
2885 break;
2886 f = f->next;
2887 }
2888 if (!f)
2889 expect("field");
2890 next();
2891 if (!notfirst)
2892 *cur_field = f;
2893 t = f->t | (t & VT_TYPEN);
2894 c += f->c;
2895 }
2896 notfirst = 1;
2897 }
2898 if (notfirst) {
2899 skip('=');
2900 } else {
2901 if (t & VT_ARRAY) {
2902 index = *cur_index;
2903 t = pointed_type(t);
2904 c += index * type_size(t, &align);
2905 } else {
2906 f = *cur_field;
2907 if (!f)
2908 error("too many field init");
2909 t = f->t | (t & VT_TYPEN);
2910 c += f->c;
2911 }
2912 }
2913 decl_assign(t, c, 0);
2914}
2915
2916/* 't' contains the type and storage info. c is the address of the
2917 object. 'first' is true if array '{' must be read (multi dimension
2918 implicit array init handling). */
2919void decl_assign(int t, int c, int first)
2920{
2921 int v, index, index_max, t1, n, no_oblock;
2922 Sym *s, *f;
2923 TokenSym *ts;
2924
2925 if (t & VT_ARRAY) {
2926 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
2927 n = s->c;
2928 index_max = 0;
2929 if (tok == TOK_STR) {
2930 t1 = pointed_type(t);
2931 if (!(t1 & VT_BYTE))
2932 error("invalid type");
2933 if ((t & VT_VALMASK) == VT_CONST) {
2934 while (tok == TOK_STR) {
2935 ts = (TokenSym *)tokc;
2936 memcpy((void *)c, ts->str, ts->len);
2937 c += ts->len;
2938 index_max += ts->len;
2939 next();
2940 }
2941 *(char *)c++ = 0;
2942 } else {
2943 error("local string init not handled");
2944 }
2945 /* string init */
2946 } else {
2947 no_oblock = 0;
2948 if (!first && tok != '{')
2949 no_oblock = 1;
2950 else
2951 skip('{');
2952 index = 0;
2953 while (tok != '}') {
2954 decl_designator(t, c, &index, NULL);
2955 if (n >= 0 && index >= n)
2956 error("index too large");
2957 if (index > index_max)
2958 index_max = index;
2959 index++;
2960 /* special test for multi dimensional arrays (may not
2961 be strictly correct if designators are used at the
2962 same time) */
2963 if (index >= n && no_oblock)
2964 break;
2965 if (tok == '}')
2966 break;
2967 skip(',');
2968 }
2969 if (!no_oblock)
2970 skip('}');
2971 }
2972 /* patch type size if needed */
2973 if (n < 0)
2974 s->c = index_max + 1;
2975 } else if (t & VT_STRUCT) {
2976 /* XXX: union needs only one init */
2977 skip('{');
2978 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
2979 f = s->next;
2980 while (tok != '}') {
2981 decl_designator(t, c, NULL, &f);
2982 if (tok == '}')
2983 break;
2984 skip(',');
2985 f = f->next;
2986 }
2987 skip('}');
2988 } else {
2989 if ((t & VT_VALMASK) == VT_CONST) {
2990 v = expr_const();
2991 printf("v=%d\n", v);
2992 if (t & VT_BYTE)
2993 *(char *)c = v;
2994 else if (t & VT_SHORT)
2995 *(short *)c = v;
2996 else
2997 *(int *)c = v;
2998 } else {
2999 vt = t;
3000 vc = c;
3001 vpush();
3002 expr_eq();
3003 vstore();
3004 }
3005 }
3006}
3007
2797/* 'l' is VT_LOCAL or VT_CONST to define default storage type */3008/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
2798void decl(l)3009void decl(l)
2799{3010{
2800 int *a, t, b, size, align, v, u, n;3011 int *a, t, b, size, align, v, u, n, addr;
2801 Sym *sym;3012 Sym *sym;
2802 3013
2803 while (b = ist()) {3014 while (b = ist()) {
@@ -2866,16 +3077,37 @@ void decl(l) · 62 unmodified lines
2866 u = VT_CONST;3077 u = VT_CONST;
2867 u |= t;3078 u |= t;
2868 size = type_size(t, &align);3079 size = type_size(t, &align);
2869 if (size < 0)
2870 error("invalid size");
2871 if ((u & VT_VALMASK) == VT_LOCAL) {3080 if ((u & VT_VALMASK) == VT_LOCAL) {
2872 /* allocate space down on the stack */3081 /* XXX: cannot use implicit size for local
3082 storage */
3083 if (size < 0)
3084 error("size must be known for locals");
2873 loc = (loc - size) & -align;3085 loc = (loc - size) & -align;
2874 sym_push(v, u, loc);3086 addr = loc;
2875 } else {3087 } else {
2876 /* allocate space up in the data space */
2877 glo = (glo + align - 1) & -align;3088 glo = (glo + align - 1) & -align;
2878 sym_push(v, u, glo);3089 addr = glo;
3090 }
3091 if (tok == '=') {
3092 next();
3093 /* special case for non array types */
3094 n = 0;
3095 if (tok == '{' && (u & (VT_ARRAY | VT_STRUCT)) == 0) {
3096 n = 1;
3097 next();
3098 }
3099 decl_assign(u, addr, 1);
3100 if (n)
3101 skip('}');
3102 }
3103 sym_push(v, u, addr);
3104 /* if global, add size */
3105 if ((u & VT_VALMASK) == VT_CONST) {
3106 /* must recompute size if it was an array
3107 with implicit size */
3108 size = type_size(t, &align);
3109 if (size < 0)
3110 error("invalid size");
2879 glo += size;3111 glo += size;
2880 }3112 }
2881 }3113 }
@@ -2916,7 +3148,7 @@ int main(int argc, char **argv) · 34 unmodified lines
2916 nb_include_paths = 3;3148 nb_include_paths = 3;
2917 3149
2918 /* add all tokens */3150 /* add all tokens */
2919 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\0restrict\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0error\0__LINE__\0__FILE__\0__DATE__\0__TIME__\0__func__\0main\0";3151 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\0restrict\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0error\0line\0__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0__func__\0main\0";
2920 while (*p) {3152 while (*p) {
2921 r = p;3153 r = p;
2922 while (*r++);3154 while (*r++);

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