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
21/* number of available registers */21/* number of available registers */
22#define NB_REGS 422#define NB_REGS 4
23 23
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). */
25#define RC_INT 0x0001 /* generic integer register */27#define RC_INT 0x0001 /* generic integer register */
26#define RC_FLOAT 0x0002 /* generic float register */28#define RC_FLOAT 0x0002 /* generic float register */
27#define RC_EAX 0x000429#define RC_EAX 0x0004
28#define RC_FRET 0x0008 /* function return: float register */30#define RC_ST0 0x0008
29#define RC_ECX 0x001031#define RC_ECX 0x0010
30#define RC_EDX 0x002032#define RC_EDX 0x0020
31#define RC_IRET RC_EAX /* function return: integer register */33#define RC_IRET RC_EAX /* function return: integer register */
32#define RC_LRET RC_EDX /* function return: second integer register */34#define RC_LRET RC_EDX /* function return: second integer register */
35#define RC_FRET RC_ST0 /* function return: float register */
33 36
34/* pretty names for the registers */37/* pretty names for the registers */
35enum {38enum {
@@ -40,10 +43,10 @@ enum { · 4 unmodified lines
40};43};
41 44
42int reg_classes[NB_REGS] = {45int reg_classes[NB_REGS] = {
43 /* eax */ RC_INT | RC_IRET,46 /* eax */ RC_INT | RC_EAX,
44 /* ecx */ RC_INT | RC_ECX,47 /* ecx */ RC_INT | RC_ECX,
45 /* edx */ RC_INT | RC_EDX,48 /* edx */ RC_INT | RC_EDX,
46 /* st0 */ RC_FLOAT | RC_FRET,49 /* st0 */ RC_FLOAT | RC_ST0,
47};50};
48 51
49/* return registers for function */52/* return registers for function */
@@ -73,6 +76,7 @@ typedef struct GFuncContext { · 23 unmodified lines
73/******************************************************/76/******************************************************/
74 77
75static int *func_sub_sp_ptr;78static int *func_sub_sp_ptr;
79static unsigned char *func_bound_ptr;
76 80
77void g(int c)81void g(int c)
78{82{
@@ -389,14 +393,39 @@ void gfunc_prolog(int t) · 310 unmodified lines
389 }393 }
390 o(0xe58955); /* push %ebp, mov %esp, %ebp */394 o(0xe58955); /* push %ebp, mov %esp, %ebp */
391 func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */395 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 }
392}402}
393 403
394/* generate function epilog */404/* generate function epilog */
395void gfunc_epilog(void)405void gfunc_epilog(void)
396{406{
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 }
397 o(0xc3c9); /* leave, ret */426 o(0xc3c9); /* leave, ret */
398 *func_sub_sp_ptr = (-loc + 3) & -4; /* align local size to word & 427 /* align local size to word & save local variables */
399 save local variables */428 *func_sub_sp_ptr = (-loc + 3) & -4;
400}429}
401 430
402int gjmp(int t)431int gjmp(int t)
@@ -457,11 +486,11 @@ void gen_opi(int op) · 54 unmodified lines
457 case TOK_ADDC1: /* add with carry generation */486 case TOK_ADDC1: /* add with carry generation */
458 opc = 0;487 opc = 0;
459 gen_op8:488 gen_op8:
460 vswap();
461 r = gv(RC_INT);
462 vswap();
463 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {489 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
464 /* constant case */490 /* constant case */
491 vswap();
492 r = gv(RC_INT);
493 vswap();
465 c = vtop->c.i;494 c = vtop->c.i;
466 if (c == (char)c) {495 if (c == (char)c) {
467 /* XXX: generate inc and dec for smaller code ? */496 /* XXX: generate inc and dec for smaller code ? */
@@ -473,7 +502,9 @@ void gen_opi(int op) · 5 unmodified lines
473 oad(0xc0 | (opc << 3) | r, c);502 oad(0xc0 | (opc << 3) | r, c);
474 }503 }
475 } else {504 } else {
476 fr = gv(RC_INT);505 gv2(RC_INT, RC_INT);
506 r = vtop[-1].r;
507 fr = vtop[0].r;
477 o((opc << 3) | 0x01);508 o((opc << 3) | 0x01);
478 o(0xc0 + r + fr * 8); 509 o(0xc0 + r + fr * 8);
479 }510 }
@@ -503,10 +534,9 @@ void gen_opi(int op) · 23 unmodified lines
503 opc = 1;534 opc = 1;
504 goto gen_op8;535 goto gen_op8;
505 case '*':536 case '*':
506 vswap();537 gv2(RC_INT, RC_INT);
507 r = gv(RC_INT);538 r = vtop[-1].r;
508 vswap();539 fr = vtop[0].r;
509 fr = gv(RC_INT);
510 vtop--;540 vtop--;
511 o(0xaf0f); /* imul fr, r */541 o(0xaf0f); /* imul fr, r */
512 o(0xc0 + fr + r * 8);542 o(0xc0 + fr + r * 8);
@@ -520,29 +550,24 @@ void gen_opi(int op) · 7 unmodified lines
520 case TOK_SAR:550 case TOK_SAR:
521 opc = 7;551 opc = 7;
522 gen_shift:552 gen_shift:
523 vswap();
524 r = gv(RC_INT);
525 vswap();
526 opc = 0xc0 | (opc << 3);553 opc = 0xc0 | (opc << 3);
527 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {554 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
528 /* constant case */555 /* constant case */
556 vswap();
557 r = gv(RC_INT);
558 vswap();
529 c = vtop->c.i & 0x1f;559 c = vtop->c.i & 0x1f;
530 o(0xc1); /* shl/shr/sar $xxx, r */560 o(0xc1); /* shl/shr/sar $xxx, r */
531 o(opc | r);561 o(opc | r);
532 g(c);562 g(c);
533 } else {563 } else {
534 /* we generate the shift in ecx */564 /* we generate the shift in ecx */
535 gv(RC_ECX);565 gv2(RC_INT, RC_ECX);
536 /* the first op may have been spilled, so we reload it if566 r = vtop[-1].r;
537 needed */
538 vswap();
539 r = gv(RC_INT);
540 vswap();
541 o(0xd3); /* shl/shr/sar %cl, r */567 o(0xd3); /* shl/shr/sar %cl, r */
542 o(opc | r);568 o(opc | r);
543 }569 }
544 vtop--;570 vtop--;
545 vtop->r = r;
546 break;571 break;
547 case '/':572 case '/':
548 case TOK_UDIV:573 case TOK_UDIV:
@@ -550,14 +575,11 @@ void gen_opi(int op) · 1 unmodified line
550 case '%':575 case '%':
551 case TOK_UMOD:576 case TOK_UMOD:
552 case TOK_UMULL:577 case TOK_UMULL:
553 vswap();578 /* first operand must be in eax */
554 r = gv(RC_EAX); /* first operand must be in eax */579 /* XXX: need better constraint for second operand */
555 vswap();580 gv2(RC_EAX, RC_ECX);
556 /* XXX: need better constraint */581 r = vtop[-1].r;
557 fr = gv(RC_ECX); /* second operand in ecx */582 fr = vtop[0].r;
558 vswap();
559 r = gv(RC_EAX); /* reload first operand if flushed */
560 vswap();
561 vtop--;583 vtop--;
562 save_reg(REG_EDX);584 save_reg(REG_EDX);
563 if (op == TOK_UMULL) {585 if (op == TOK_UMULL) {
@@ -588,7 +610,7 @@ void gen_opi(int op) · 24 unmodified lines
588 610
589/* generate a floating point operation 'v = t1 op t2' instruction. The611/* generate a floating point operation 'v = t1 op t2' instruction. The
590 two operands are guaranted to have the same floating point type */612 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 */
592void gen_opf(int op)614void gen_opf(int op)
593{615{
594 int a, ft, fc, swapped;616 int a, ft, fc, swapped;
@@ -759,6 +781,49 @@ void gen_cvt_ftof(int t) · 164 unmodified lines
759 gv(RC_FLOAT);781 gv(RC_FLOAT);
760}782}
761 783
784/* bound check support functions */
785
786/* generate first part of bounded pointer addition */
787void 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 */
798void 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
762/* end of X86 code generator */827/* end of X86 code generator */
763/*************************************************************/828/*************************************************************/
764 829

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