History

finished bound check code (still slow) - fixed binary ops code gen - fixed float return

bellard committed 24 years ago

Commit 5f855851 parent ad28d4c1 file · +97 −21Browse files

Unified · Split

tcc.c+97−21
@@ -163,7 +163,9 @@ int section_num; · 162 unmodified lines
163163 Section *text_section, *data_section, *bss_section; /* predefined sections */
164164 Section *cur_text_section; /* current section where function code is
165165 generated */
166+/* bound check related sections */
166167 Section *bounds_section; /* contains global data bound description */
168+Section *lbounds_section; /* contains local data bound description */
167169 /* debug sections */
168170 Section *stab_section, *stabstr_section, *symtab_section, *strtab_section;
169171
@@ -209,18 +211,20 @@ int gnu_ext = 1; · 39 unmodified lines
209211 int tcc_ext = 1;
210212
211213 /* The current value can be: */
212−#define VT_VALMASK 0x00ff
213−#define VT_CONST 0x00f0 /* constant in vc
214+#define VT_VALMASK 0x00ff
215+#define VT_CONST 0x00f0 /* constant in vc
214216 (must be first non register value) */
215−#define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
216−#define VT_LOCAL 0x00f2 /* offset on stack */
217−#define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
218−#define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
219−#define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
220−#define VT_LVAL 0x0100 /* var is an lvalue */
221−#define VT_FORWARD 0x0200 /* value is forward reference */
222−#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
217+#define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
218+#define VT_LOCAL 0x00f2 /* offset on stack */
219+#define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
220+#define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
221+#define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
222+#define VT_LVAL 0x0100 /* var is an lvalue */
223+#define VT_FORWARD 0x0200 /* value is forward reference */
224+#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
223225 char/short stored in integer registers) */
226+#define VT_MUSTBOUND 0x0800 /* bound checking must be done before
227+ dereferencing value */
224228
225229 /* types */
226230 #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */
@@ -402,6 +406,7 @@ void decl(int l); · 175 unmodified lines
402406 void decl_initializer(int t, int r, int c, int first, int size_only);
403407 int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init);
404408 int gv(int rc);
409+void gv2(int rc1, int rc2);
405410 void move_reg(int r, int s);
406411 void save_regs(void);
407412 void save_reg(int r);
@@ -2339,6 +2344,20 @@ void gaddrof(void) · 1931 unmodified lines
23392344 vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;
23402345 }
23412346
2347+/* generate lvalue bound code */
2348+void gbound(void)
2349+{
2350+ vtop->r &= ~VT_MUSTBOUND;
2351+ /* if lvalue, then use checking code before dereferencing */
2352+ if (vtop->r & VT_LVAL) {
2353+ gaddrof();
2354+ vpushi(0);
2355+ gen_bounded_ptr_add1();
2356+ gen_bounded_ptr_add2(1);
2357+ vtop->r |= VT_LVAL;
2358+ }
2359+}
2360+
23422361 /* store vtop a register belonging to class 'rc'. lvalues are
23432362 converted to values. Cannot be used if cannot be converted to
23442363 register value (such as structures). */
@@ -2377,6 +2396,9 @@ int gv(int rc) · 32 unmodified lines
23772396 data_offset += size << 2;
23782397 data_section->data_ptr = (unsigned char *)data_offset;
23792398 }
2399+ if (vtop->r & VT_MUSTBOUND)
2400+ gbound();
2401+
23802402 r = vtop->r & VT_VALMASK;
23812403 /* need to reload if:
23822404 - constant
@@ -2436,6 +2458,33 @@ int gv(int rc) · 53 unmodified lines
24362458 return r;
24372459 }
24382460
2461+/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
2462+void gv2(int rc1, int rc2)
2463+{
2464+ /* generate more generic register first */
2465+ if (rc1 <= rc2) {
2466+ vswap();
2467+ gv(rc1);
2468+ vswap();
2469+ gv(rc2);
2470+ /* test if reload is needed for first register */
2471+ if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
2472+ vswap();
2473+ gv(rc1);
2474+ vswap();
2475+ }
2476+ } else {
2477+ gv(rc2);
2478+ vswap();
2479+ gv(rc1);
2480+ vswap();
2481+ /* test if reload is needed for first register */
2482+ if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
2483+ gv(rc2);
2484+ }
2485+ }
2486+}
2487+
24392488 /* expand long long on stack in two int registers */
24402489 void lexpand(void)
24412490 {
@@ -2454,10 +2503,7 @@ void lexpand(void) · 12 unmodified lines
24542503 /* build a long long from two ints */
24552504 void lbuild(int t)
24562505 {
2457− vswap();
2458− gv(RC_INT);
2459− vswap();
2460− gv(RC_INT);
2506+ gv2(RC_INT, RC_INT);
24612507 vtop[-1].r2 = vtop[0].r;
24622508 vtop[-1].t = t;
24632509 vpop();
@@ -2890,13 +2936,15 @@ void gen_op(int op) · 426 unmodified lines
28902936 vpushi(pointed_size(vtop[-1].t));
28912937 gen_op('*');
28922938 if (do_bounds_check) {
2893− /* if bounded pointers, we generate a special code to test bounds */
2939+ /* if bounded pointers, we generate a special code to
2940+ test bounds */
28942941 if (op == '-') {
28952942 vpushi(0);
28962943 vswap();
28972944 gen_op('-');
28982945 }
2899− gen_bounded_ptr_add();
2946+ gen_bounded_ptr_add1();
2947+ gen_bounded_ptr_add2(0);
29002948 } else {
29012949 gen_opc(op);
29022950 }
@@ -3503,10 +3551,16 @@ void vstore(void) · 600 unmodified lines
35033551 /* store result */
35043552 vstore();
35053553 } else {
3554+ /* bound check case */
3555+ if (vtop[-1].r & VT_MUSTBOUND) {
3556+ vswap();
3557+ gbound();
3558+ vswap();
3559+ }
35063560 rc = RC_INT;
35073561 if (is_float(ft))
35083562 rc = RC_FLOAT;
3509− r = gv(rc); /* generate value (XXX: move that to store code) */
3563+ r = gv(rc); /* generate value */
35103564 /* if lvalue was saved on stack, must read it */
35113565 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
35123566 SValue sv;
@@ -4043,8 +4097,13 @@ void indir(void) · 530 unmodified lines
40434097 if (vtop->r & VT_LVAL)
40444098 gv(RC_INT);
40454099 vtop->t = pointed_type(vtop->t);
4046− if (!(vtop->t & VT_ARRAY)) /* an array is never an lvalue */
4100+ /* an array is never an lvalue */
4101+ if (!(vtop->t & VT_ARRAY)) {
40474102 vtop->r |= VT_LVAL;
4103+ /* if bound checking, the referenced pointer must be checked */
4104+ if (do_bounds_check)
4105+ vtop->r |= VT_MUSTBOUND;
4106+ }
40484107 }
40494108
40504109 /* pass a parameter to a function and do type checking and casting */
@@ -4659,7 +4718,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 608 unmodified lines
46594718 } else {
46604719 gv(RC_IRET);
46614720 }
4662− vpop();
4721+ vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
46634722 }
46644723 skip(';');
46654724 rsym = gjmp(rsym); /* jmp */
@@ -5195,8 +5254,23 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 529 unmodified lines
51955254 if (ad->aligned > align)
51965255 align = ad->aligned;
51975256 if ((r & VT_VALMASK) == VT_LOCAL) {
5257+ if (do_bounds_check && (t & VT_ARRAY))
5258+ loc--;
51985259 loc = (loc - size) & -align;
51995260 addr = loc;
5261+ /* handles bounds */
5262+ /* XXX: currently, since we do only one pass, we cannot track
5263+ '&' operators, so we add only arrays */
5264+ if (do_bounds_check && (t & VT_ARRAY)) {
5265+ int *bounds_ptr;
5266+ /* add padding between regions */
5267+ loc--;
5268+ /* then add local bound info */
5269+ bounds_ptr = (int *)lbounds_section->data_ptr;
5270+ *bounds_ptr++ = addr;
5271+ *bounds_ptr++ = size;
5272+ lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
5273+ }
52005274 } else {
52015275 /* compute section */
52025276 sec = ad->section;
@@ -5219,7 +5293,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 16 unmodified lines
52195293 int *bounds_ptr;
52205294 /* first, we need to add at least one byte between each region */
52215295 data_offset++;
5222− /* then add global data info */
5296+ /* then add global bound info */
52235297 bounds_ptr = (int *)bounds_section->data_ptr;
52245298 *bounds_ptr++ = addr;
52255299 *bounds_ptr++ = size;
@@ -6059,9 +6133,11 @@ int main(int argc, char **argv) · 833 unmodified lines
60596133 } else if (r[1] == 'b') {
60606134 if (!do_bounds_check) {
60616135 do_bounds_check = 1;
6062− /* create bounds section for global data */
6136+ /* create bounds sections */
60636137 bounds_section = new_section(".bounds",
60646138 SHT_PROGBITS, SHF_ALLOC);
6139+ lbounds_section = new_section(".lbounds",
6140+ SHT_PROGBITS, SHF_ALLOC);
60656141 /* debug is implied */
60666142 goto debug_opt;
60676143 }

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