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
233233 TOK_DEFINED,
234234 TOK_UNDEF,
235235 TOK_ERROR,
236+ TOK_LINE,
236237 TOK___LINE__,
237238 TOK___FILE__,
238239 TOK___DATE__,
239240 TOK___TIME__,
241+ TOK___VA_ARGS__,
240242
241243 /* special identifiers */
242244 TOK___FUNC__,
@@ -250,6 +252,7 @@ int expr_const(); · 7 unmodified lines
250252 void expr_eq();
251253 void expr();
252254 void decl();
255+void decl_assign(int t, int c, int first);
253256 int gv();
254257 void move_reg();
255258 void save_reg();
@@ -715,6 +718,8 @@ void preprocess() · 459 unmodified lines
715718 next_nomacro();
716719 ps = &first;
717720 while (tok != ')') {
721+ if (tok == TOK_DOTS)
722+ tok = TOK___VA_ARGS__;
718723 s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
719724 *ps = s;
720725 ps = &s->next;
@@ -763,44 +768,51 @@ void preprocess() · 42 unmodified lines
763768 minp();
764769 }
765770 *q = '\0';
766− if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)
767− error("memory full");
768− if (c == '\"') {
769− /* first search in current dir if "header.h" */
770− /* XXX: buffer overflow */
771− size = 0;
772− p = strrchr(filename, '/');
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;
771+ } else {
772+ next();
773+ if (tok != TOK_STR)
774+ error("#include syntax error");
775+ /* XXX: buffer overflow */
776+ strcpy(buf, get_tok_str(tok, tokc));
777+ c = '\"';
803778 }
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;
804816 } else if (tok == TOK_IFNDEF) {
805817 c = 1;
806818 goto do_ifdef;
@@ -838,6 +850,19 @@ void preprocess() · 31 unmodified lines
838850 if (ifdef_stack_ptr == ifdef_stack)
839851 expect("#if");
840852 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+ }
841866 } else if (tok == TOK_ERROR) {
842867 error("#error");
843868 }
@@ -877,7 +902,21 @@ int getq() · 33 unmodified lines
877902 minp();
878903 if (c == '\\') {
879904 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);
881920 } else {
882921 if (ch == 'a')
883922 c = '\a';
@@ -926,6 +965,16 @@ void next_nomacro1() · 42 unmodified lines
926965 }
927966 if (isid(ch)) {
928967 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+ }
929978 while (isid(ch) | isnum(ch)) {
930979 if (q >= token_buf + STRING_MAX_SIZE)
931980 error("ident too long");
@@ -944,6 +993,9 @@ void next_nomacro1() · 12 unmodified lines
944993 if (ch == 'x' || ch == 'X') {
945994 cinp();
946995 b = 16;
996+ } else if (ch == 'b' || ch == 'B') {
997+ cinp();
998+ b = 2;
947999 }
9481000 }
9491001 tokc = getn(b);
@@ -952,6 +1004,7 @@ void next_nomacro1() · 2 unmodified lines
9521004 cinp();
9531005 tok = TOK_NUM;
9541006 } else if (ch == '\'') {
1007+ char_const:
9551008 minp();
9561009 tokc = getq();
9571010 tok = TOK_CCHAR;
@@ -959,6 +1012,7 @@ void next_nomacro1() · 1 unmodified line
9591012 expect("\'");
9601013 minp();
9611014 } else if (ch == '\"') {
1015+ str_const:
9621016 minp();
9631017 q = token_buf;
9641018 while (ch != '\"') {
@@ -1203,7 +1257,10 @@ void macro_subst(int **tok_str, int *tok_len, · 238 unmodified lines
12031257 len = 0;
12041258 str = NULL;
12051259 parlevel = 0;
1206− while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
1260+ while ((parlevel > 0 ||
1261+ (tok != ')' &&
1262+ (tok != ',' ||
1263+ sa->v == (TOK___VA_ARGS__ | SYM_FIELD)))) &&
12071264 tok != -1) {
12081265 if (tok == '(')
12091266 parlevel++;
@@ -1916,7 +1973,7 @@ void inc(post, c) · 706 unmodified lines
19161973 int struct_decl(u)
19171974 {
19181975 int a, t, b, v, size, align, maxalign, c;
1919− Sym *slast, *s, *ss;
1976+ Sym *s, *ss, **ps;
19201977
19211978 a = tok; /* save decl type */
19221979 next();
@@ -1945,7 +2002,7 @@ int struct_decl(u) · 22 unmodified lines
19452002 /* cannot be empty */
19462003 c = 0;
19472004 maxalign = 0;
1948− slast = NULL;
2005+ ps = &s->next;
19492006 while (1) {
19502007 if (a == TOK_ENUM) {
19512008 v = tok;
@@ -1978,8 +2035,8 @@ int struct_decl(u) · 26 unmodified lines
19782035 }
19792036 if (align > maxalign)
19802037 maxalign = align;
1981− ss->next = slast;
1982− slast = ss;
2038+ *ps = ss;
2039+ ps = &ss->next;
19832040 if (tok == ';' || tok == -1)
19842041 break;
19852042 skip(',');
@@ -1990,7 +2047,6 @@ int struct_decl(u) · 4 unmodified lines
19902047 break;
19912048 }
19922049 skip('}');
1993− s->next = slast;
19942050 /* size for struct/union, dummy for enum */
19952051 s->c = (c + maxalign - 1) & -maxalign;
19962052 }
@@ -2215,13 +2271,13 @@ void unary() · 218 unmodified lines
22152271 } else if (tok == TOK___FUNC__) {
22162272 /* special function name identifier */
22172273 /* generate (char *) type */
2218− vset(VT_CONST | mk_pointer(VT_TYPE), glo);
2274+ vset(VT_CONST | mk_pointer(VT_BYTE), glo);
22192275 strcpy((void *)glo, funcname);
22202276 glo += strlen(funcname) + 1;
22212277 } else if (tok == TOK_STR) {
22222278 TokenSym *ts;
22232279 /* generate (char *) type */
2224− vset(VT_CONST | mk_pointer(VT_TYPE), glo);
2280+ vset(VT_CONST | mk_pointer(VT_BYTE), glo);
22252281 while (tok == TOK_STR) {
22262282 ts = (TokenSym *)tokc;
22272283 memcpy((void *)glo, ts->str, ts->len);
@@ -2296,7 +2352,7 @@ void unary() · 68 unmodified lines
22962352 s = sym_find(t);
22972353 if (!s) {
22982354 if (tok != '(')
2299− error("undefined symbol");
2355+ error("'%s' undeclared", get_tok_str(t, 0));
23002356 /* for simple function calls, we tolerate undeclared
23012357 external reference */
23022358 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
26372693 next();
26382694 /* declarations */
26392695 s = local_stack;
2640− decl(VT_LOCAL);
2641− while (tok != '}')
2696+ while (tok != '}') {
2697+ decl(VT_LOCAL);
26422698 block(bsym, csym, case_sym, def_sym, case_reg);
2699+ }
26432700 /* pop locally defined symbols */
26442701 sym_pop(&local_stack, s);
26452702 next();
@@ -2794,10 +2851,164 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 148 unmodified lines
27942851 }
27952852 }
27962853
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 */
2856+void 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). */
2919+void 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+
27973008 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
27983009 void decl(l)
27993010 {
2800− int *a, t, b, size, align, v, u, n;
3011+ int *a, t, b, size, align, v, u, n, addr;
28013012 Sym *sym;
28023013
28033014 while (b = ist()) {
@@ -2866,16 +3077,37 @@ void decl(l) · 62 unmodified lines
28663077 u = VT_CONST;
28673078 u |= t;
28683079 size = type_size(t, &align);
2869− if (size < 0)
2870− error("invalid size");
28713080 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");
28733085 loc = (loc - size) & -align;
2874− sym_push(v, u, loc);
3086+ addr = loc;
28753087 } else {
2876− /* allocate space up in the data space */
28773088 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");
28793111 glo += size;
28803112 }
28813113 }
@@ -2916,7 +3148,7 @@ int main(int argc, char **argv) · 34 unmodified lines
29163148 nb_include_paths = 3;
29173149
29183150 /* 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";
29203152 while (*p) {
29213153 r = p;
29223154 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