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