History

added empty structs support - changed double align to 4 (i386 default ABI) - added align support for structs - added GCC defines for basic types

bellard committed 23 years ago

Commit 7b2721f1 parent 0841db01 file · +59 −39Browse files

Unified · Split

tcc.c+59−39
@@ -587,7 +587,7 @@ static Sym *sym_find(int v); · 586 unmodified lines
587587 static Sym *sym_push(int v, CType *type, int r, int c);
588588
589589 /* type handling */
590−int type_size(CType *type, int *a);
590+static int type_size(CType *type, int *a);
591591 static inline CType *pointed_type(CType *type);
592592 static int pointed_size(CType *type);
593593 static int lvalue_type(int t);
@@ -2554,6 +2554,9 @@ static void preprocess(int is_bof) · 1960 unmodified lines
25542554 else
25552555 warning("#warning %s", buf);
25562556 break;
2557+ case TOK_PRAGMA:
2558+ /* ignored */
2559+ break;
25572560 default:
25582561 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
25592562 /* '!' is ignored to allow C scripts. numbers are ignored
@@ -5154,7 +5157,7 @@ static void gen_cast(CType *type) · 2594 unmodified lines
51545157 }
51555158
51565159 /* return type size. Put alignment at 'a' */
5157−int type_size(CType *type, int *a)
5160+static int type_size(CType *type, int *a)
51585161 {
51595162 Sym *s;
51605163 int bt;
@@ -5163,7 +5166,7 @@ int type_size(CType *type, int *a) · 2 unmodified lines
51635166 if (bt == VT_STRUCT) {
51645167 /* struct/union */
51655168 s = type->ref;
5166− *a = 4; /* XXX: cannot store it yet. Doing that is safe */
5169+ *a = s->r;
51675170 return s->c;
51685171 } else if (bt == VT_PTR) {
51695172 if (type->t & VT_ARRAY) {
@@ -5177,7 +5180,7 @@ int type_size(CType *type, int *a) · 7 unmodified lines
51775180 *a = LDOUBLE_ALIGN;
51785181 return LDOUBLE_SIZE;
51795182 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5180− *a = 8;
5183+ *a = 4; /* XXX: i386 specific */
51815184 return 8;
51825185 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
51835186 *a = 4;
@@ -5634,12 +5637,9 @@ static void struct_decl(CType *type, int u) · 450 unmodified lines
56345637 error("struct/union/enum already defined");
56355638 /* cannot be empty */
56365639 c = 0;
5637− maxalign = 0;
5638− ps = &s->next;
5639− bit_pos = 0;
5640− offset = 0;
5641− while (1) {
5642− if (a == TOK_ENUM) {
5640+ /* non empty enums are not allowed */
5641+ if (a == TOK_ENUM) {
5642+ for(;;) {
56435643 v = tok;
56445644 next();
56455645 if (tok == '=') {
@@ -5652,7 +5652,16 @@ static void struct_decl(CType *type, int u) · 6 unmodified lines
56525652 if (tok == ',')
56535653 next();
56545654 c++;
5655− } else {
5655+ if (tok == '}')
5656+ break;
5657+ }
5658+ skip('}');
5659+ } else {
5660+ maxalign = 1;
5661+ ps = &s->next;
5662+ bit_pos = 0;
5663+ offset = 0;
5664+ while (tok != '}') {
56565665 parse_btype(&btype, &ad);
56575666 while (1) {
56585667 bit_size = -1;
@@ -5682,7 +5691,8 @@ static void struct_decl(CType *type, int u) · 23 unmodified lines
56825691 bt = type1.t & VT_BTYPE;
56835692 if (bt != VT_INT &&
56845693 bt != VT_BYTE &&
5685− bt != VT_SHORT)
5694+ bt != VT_SHORT &&
5695+ bt != VT_ENUM)
56865696 error("bitfields must have scalar type");
56875697 bsize = size * 8;
56885698 if (bit_size > bsize) {
@@ -5741,18 +5751,17 @@ static void struct_decl(CType *type, int u) · 52 unmodified lines
57415751 *ps = ss;
57425752 ps = &ss->next;
57435753 }
5744− if (tok == ';' || tok == -1)
5754+ if (tok == ';' || tok == TOK_EOF)
57455755 break;
57465756 skip(',');
57475757 }
57485758 skip(';');
57495759 }
5750− if (tok == '}')
5751− break;
5760+ skip('}');
5761+ /* store size and alignment */
5762+ s->c = (c + maxalign - 1) & -maxalign;
5763+ s->r = maxalign;
57525764 }
5753− skip('}');
5754− /* size for struct/union, dummy for enum */
5755− s->c = (c + maxalign - 1) & -maxalign;
57565765 }
57575766 }
57585767
@@ -6653,18 +6662,17 @@ static void expr_land(void) · 894 unmodified lines
66536662 int t;
66546663
66556664 expr_or();
6656− t = 0;
6657− while (1) {
6658− if (tok != TOK_LAND) {
6659− if (t) {
6660− t = gtst(1, t);
6665+ if (tok == TOK_LAND) {
6666+ t = 0;
6667+ for(;;) {
6668+ t = gtst(1, t);
6669+ if (tok != TOK_LAND) {
66616670 vseti(VT_JMPI, t);
6671+ break;
66626672 }
6663− break;
6673+ next();
6674+ expr_or();
66646675 }
6665− t = gtst(1, t);
6666− next();
6667− expr_or();
66686676 }
66696677 }
66706678
@@ -6673,18 +6681,17 @@ static void expr_lor(void) · 2 unmodified lines
66736681 int t;
66746682
66756683 expr_land();
6676− t = 0;
6677− while (1) {
6678− if (tok != TOK_LOR) {
6679− if (t) {
6680− t = gtst(0, t);
6684+ if (tok == TOK_LOR) {
6685+ t = 0;
6686+ for(;;) {
6687+ t = gtst(0, t);
6688+ if (tok != TOK_LOR) {
66816689 vseti(VT_JMP, t);
6690+ break;
66826691 }
6683− break;
6692+ next();
6693+ expr_land();
66846694 }
6685− t = gtst(0, t);
6686− next();
6687− expr_land();
66886695 }
66896696 }
66906697
@@ -6714,8 +6721,16 @@ static void expr_eq(void) · 23 unmodified lines
67146721 expr_lor();
67156722 if (tok == '?') {
67166723 next();
6717− save_regs(1); /* we need to save all registers here except
6718− at the top because it is a branch point */
6724+ if (vtop != vstack) {
6725+ /* needed to avoid having different registers saved in
6726+ each branch */
6727+ if (is_float(vtop->type.t))
6728+ rc = RC_FLOAT;
6729+ else
6730+ rc = RC_INT;
6731+ gv(rc);
6732+ save_regs(1);
6733+ }
67196734 tt = gtst(1, 0);
67206735 gexpr();
67216736 type1 = vtop->type;
@@ -6723,7 +6738,6 @@ static void expr_eq(void) · 1 unmodified line
67236738 vtop--; /* no vpop so that FP stack is not flushed */
67246739 skip(':');
67256740 u = gjmp(0);
6726−
67276741 gsym(tt);
67286742 expr_eq();
67296743 type2 = vtop->type;
@@ -6776,6 +6790,7 @@ static void expr_eq(void) · 46 unmodified lines
67766790 to handle a complicated move */
67776791 rc = RC_IRET;
67786792 }
6793+
67796794 r2 = gv(rc);
67806795 /* this is horrible, but we must also convert first
67816796 operand */
@@ -8509,6 +8524,11 @@ TCCState *tcc_new(void) · 1727 unmodified lines
85098524 #endif
85108525 /* tiny C specific defines */
85118526 tcc_define_symbol(s, "__TINYC__", NULL);
8527+
8528+ /* tiny C & gcc defines */
8529+ tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
8530+ tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
8531+ tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
85128532
85138533 /* default library paths */
85148534 tcc_add_library_path(s, "/usr/local/lib");

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