History

added local bound generation - fixed binary ops - added bound check dereference

bellard committed 24 years ago

Commit b7fed2f1 parent 7e51b661 file · +98 −33Browse files

Unified · Split

i386-gen.c+98−33
@@ -21,15 +21,18 @@ · 20 unmodified lines
2121 /* number of available registers */
2222 #define NB_REGS 4
2323
24−/* a register can belong to several classes */
24+/* a register can belong to several classes. The classes must be
25+ sorted from more general to more precise (see gv2() code which does
26+ assumptions on it). */
2527 #define RC_INT 0x0001 /* generic integer register */
2628 #define RC_FLOAT 0x0002 /* generic float register */
2729 #define RC_EAX 0x0004
28−#define RC_FRET 0x0008 /* function return: float register */
30+#define RC_ST0 0x0008
2931 #define RC_ECX 0x0010
3032 #define RC_EDX 0x0020
3133 #define RC_IRET RC_EAX /* function return: integer register */
3234 #define RC_LRET RC_EDX /* function return: second integer register */
35+#define RC_FRET RC_ST0 /* function return: float register */
3336
3437 /* pretty names for the registers */
3538 enum {
@@ -40,10 +43,10 @@ enum { · 4 unmodified lines
4043 };
4144
4245 int reg_classes[NB_REGS] = {
43− /* eax */ RC_INT | RC_IRET,
46+ /* eax */ RC_INT | RC_EAX,
4447 /* ecx */ RC_INT | RC_ECX,
4548 /* edx */ RC_INT | RC_EDX,
46− /* st0 */ RC_FLOAT | RC_FRET,
49+ /* st0 */ RC_FLOAT | RC_ST0,
4750 };
4851
4952 /* return registers for function */
@@ -73,6 +76,7 @@ typedef struct GFuncContext { · 23 unmodified lines
7376 /******************************************************/
7477
7578 static int *func_sub_sp_ptr;
79+static unsigned char *func_bound_ptr;
7680
7781 void g(int c)
7882 {
@@ -389,14 +393,39 @@ void gfunc_prolog(int t) · 310 unmodified lines
389393 }
390394 o(0xe58955); /* push %ebp, mov %esp, %ebp */
391395 func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
396+ /* leave some room for bound checking code */
397+ if (do_bounds_check) {
398+ oad(0xb8, 0); /* lbound section pointer */
399+ oad(0xb8, 0); /* call to function */
400+ func_bound_ptr = lbounds_section->data_ptr;
401+ }
392402 }
393403
394404 /* generate function epilog */
395405 void gfunc_epilog(void)
396406 {
407+ if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
408+ int saved_ind;
409+ int *bounds_ptr;
410+ /* add end of table info */
411+ bounds_ptr = (int *)lbounds_section->data_ptr;
412+ *bounds_ptr++ = 0;
413+ lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
414+ /* generate bound local allocation */
415+ saved_ind = ind;
416+ ind = (int)func_sub_sp_ptr + 4;
417+ oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
418+ oad(0xe8, (int)__bound_local_new - ind - 5);
419+ ind = saved_ind;
420+ /* generate bound check local freeing */
421+ o(0x5250); /* save returned value, if any */
422+ oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
423+ oad(0xe8, (int)__bound_local_delete - ind - 5);
424+ o(0x585a); /* restore returned value, if any */
425+ }
397426 o(0xc3c9); /* leave, ret */
398− *func_sub_sp_ptr = (-loc + 3) & -4; /* align local size to word &
399− save local variables */
427+ /* align local size to word & save local variables */
428+ *func_sub_sp_ptr = (-loc + 3) & -4;
400429 }
401430
402431 int gjmp(int t)
@@ -457,11 +486,11 @@ void gen_opi(int op) · 54 unmodified lines
457486 case TOK_ADDC1: /* add with carry generation */
458487 opc = 0;
459488 gen_op8:
460− vswap();
461− r = gv(RC_INT);
462− vswap();
463489 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
464490 /* constant case */
491+ vswap();
492+ r = gv(RC_INT);
493+ vswap();
465494 c = vtop->c.i;
466495 if (c == (char)c) {
467496 /* XXX: generate inc and dec for smaller code ? */
@@ -473,7 +502,9 @@ void gen_opi(int op) · 5 unmodified lines
473502 oad(0xc0 | (opc << 3) | r, c);
474503 }
475504 } else {
476− fr = gv(RC_INT);
505+ gv2(RC_INT, RC_INT);
506+ r = vtop[-1].r;
507+ fr = vtop[0].r;
477508 o((opc << 3) | 0x01);
478509 o(0xc0 + r + fr * 8);
479510 }
@@ -503,10 +534,9 @@ void gen_opi(int op) · 23 unmodified lines
503534 opc = 1;
504535 goto gen_op8;
505536 case '*':
506− vswap();
507− r = gv(RC_INT);
508− vswap();
509− fr = gv(RC_INT);
537+ gv2(RC_INT, RC_INT);
538+ r = vtop[-1].r;
539+ fr = vtop[0].r;
510540 vtop--;
511541 o(0xaf0f); /* imul fr, r */
512542 o(0xc0 + fr + r * 8);
@@ -520,29 +550,24 @@ void gen_opi(int op) · 7 unmodified lines
520550 case TOK_SAR:
521551 opc = 7;
522552 gen_shift:
523− vswap();
524− r = gv(RC_INT);
525− vswap();
526553 opc = 0xc0 | (opc << 3);
527554 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
528555 /* constant case */
556+ vswap();
557+ r = gv(RC_INT);
558+ vswap();
529559 c = vtop->c.i & 0x1f;
530560 o(0xc1); /* shl/shr/sar $xxx, r */
531561 o(opc | r);
532562 g(c);
533563 } else {
534564 /* we generate the shift in ecx */
535− gv(RC_ECX);
536− /* the first op may have been spilled, so we reload it if
537− needed */
538− vswap();
539− r = gv(RC_INT);
540− vswap();
565+ gv2(RC_INT, RC_ECX);
566+ r = vtop[-1].r;
541567 o(0xd3); /* shl/shr/sar %cl, r */
542568 o(opc | r);
543569 }
544570 vtop--;
545− vtop->r = r;
546571 break;
547572 case '/':
548573 case TOK_UDIV:
@@ -550,14 +575,11 @@ void gen_opi(int op) · 1 unmodified line
550575 case '%':
551576 case TOK_UMOD:
552577 case TOK_UMULL:
553− vswap();
554− r = gv(RC_EAX); /* first operand must be in eax */
555− vswap();
556− /* XXX: need better constraint */
557− fr = gv(RC_ECX); /* second operand in ecx */
558− vswap();
559− r = gv(RC_EAX); /* reload first operand if flushed */
560− vswap();
578+ /* first operand must be in eax */
579+ /* XXX: need better constraint for second operand */
580+ gv2(RC_EAX, RC_ECX);
581+ r = vtop[-1].r;
582+ fr = vtop[0].r;
561583 vtop--;
562584 save_reg(REG_EDX);
563585 if (op == TOK_UMULL) {
@@ -588,7 +610,7 @@ void gen_opi(int op) · 24 unmodified lines
588610
589611 /* generate a floating point operation 'v = t1 op t2' instruction. The
590612 two operands are guaranted to have the same floating point type */
591−/* NOTE: currently floats can only be lvalues */
613+/* XXX: need to use ST1 too */
592614 void gen_opf(int op)
593615 {
594616 int a, ft, fc, swapped;
@@ -759,6 +781,49 @@ void gen_cvt_ftof(int t) · 164 unmodified lines
759781 gv(RC_FLOAT);
760782 }
761783
784+/* bound check support functions */
785+
786+/* generate first part of bounded pointer addition */
787+void gen_bounded_ptr_add1(void)
788+{
789+ /* prepare fast i386 function call (args in eax and edx) */
790+ gv2(RC_EAX, RC_EDX);
791+ /* save all temporary registers */
792+ vtop--;
793+ vtop->r = VT_CONST;
794+ save_regs();
795+}
796+
797+/* if deref is true, then also test dereferencing */
798+void gen_bounded_ptr_add2(int deref)
799+{
800+ void *func;
801+ int size, align;
802+
803+ if (deref) {
804+ size = type_size(vtop->t, &align);
805+ switch(size) {
806+ case 1: func = __bound_ptr_indir1; break;
807+ case 2: func = __bound_ptr_indir2; break;
808+ case 4: func = __bound_ptr_indir4; break;
809+ case 8: func = __bound_ptr_indir8; break;
810+ case 12: func = __bound_ptr_indir12; break;
811+ case 16: func = __bound_ptr_indir16; break;
812+ default:
813+ error("unhandled size when derefencing bounded pointer");
814+ func = NULL;
815+ break;
816+ }
817+ } else {
818+ func = __bound_ptr_add;
819+ }
820+
821+ /* do a fast function call */
822+ oad(0xe8, (int)func - ind - 5);
823+ /* return pointer is there */
824+ vtop->r = REG_EAX;
825+}
826+
762827 /* end of X86 code generator */
763828 /*************************************************************/
764829

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