From a1b27a9359931dfd8820acd0c5037b3e6f471a34 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sun, 11 Jan 2026 23:28:07 +0800 Subject: [PATCH] scf: update native/x64 --- html/Makefile | 1 + js/abc_libjs.so | Bin 64456 -> 64496 bytes js/elf/scf_elf.c | 16 ++ js/elf/scf_elf.h | 6 +- js/elf/scf_elf_naja.c | 4 +- js/elf/scf_elf_naja_so.c | 41 ++-- js/native/risc/scf_naja.c | 277 +++++++++++----------- js/native/risc/scf_risc_inst.c | 3 + js/native/risc/scf_risc_reg.h | 1 - js/native/risc/scf_risc_reg_naja.c | 351 +++++++++++----------------- js/native/risc/scf_risc_util.h | 20 +- js/native/scf_instruction.c | 76 ++++++ js/native/scf_instruction.h | 114 +++++++++ js/native/scf_native.c | 46 ---- js/native/scf_native.h | 98 +------- js/native/x64/scf_x64.c | 14 +- js/native/x64/scf_x64.h | 64 +---- js/native/x64/scf_x64_inst.c | 175 +++++++------- js/native/x64/scf_x64_inst_binary.c | 68 +++--- js/native/x64/scf_x64_inst_cmp.c | 20 +- js/native/x64/scf_x64_inst_common.c | 52 ++--- js/native/x64/scf_x64_inst_div.c | 17 +- js/native/x64/scf_x64_inst_mul.c | 26 +-- js/native/x64/scf_x64_inst_shift.c | 21 +- js/native/x64/scf_x64_inst_unary.c | 2 +- js/native/x64/scf_x64_inst_util.c | 150 +++++++++++- js/native/x64/scf_x64_opcode.c | 55 +++-- js/native/x64/scf_x64_opcode.h | 88 ++++++- js/native/x64/scf_x64_reg.c | 210 ++--------------- js/native/x64/scf_x64_reg.h | 81 +------ js/native/x64/scf_x64_reg_util.c | 166 +++++++++++++ js/native/x64/scf_x64_reg_util.h | 85 +++++++ js/native/x64/scf_x64_util.h | 3 +- ui/Makefile | 1 + 34 files changed, 1259 insertions(+), 1093 deletions(-) create mode 100644 js/native/scf_instruction.c create mode 100644 js/native/scf_instruction.h create mode 100644 js/native/x64/scf_x64_reg_util.c create mode 100644 js/native/x64/scf_x64_reg_util.h diff --git a/html/Makefile b/html/Makefile index 67b5bad..b04c19b 100644 --- a/html/Makefile +++ b/html/Makefile @@ -18,6 +18,7 @@ CFILES += ../js/parse/scf_operator_handler_expr.c CFILES += ../js/parse/scf_operator_handler_const.c CFILES += ../js/native/scf_native.c +CFILES += ../js/native/scf_instruction.c CFILES += ../js/native/x64/scf_x64.c CFILES += ../js/native/x64/scf_x64_inst.c CFILES += ../js/native/x64/scf_x64_inst_util.c diff --git a/js/abc_libjs.so b/js/abc_libjs.so index 5dade4ecfb408a438cae51e889216ac9960d6494..376eaa007efa1e1da105f67eaf01a94be84e17be 100755 GIT binary patch delta 15143 zcmZX533OA{_Ws%D-Xu3;(>6`gHf@tMZPKPKP0LWAK!E}+^YoZRnF<0TAd}2N8C6u^ zJ{4!2aYjKzA1W$}D316P#R12sh`;AkpR>>Zw@(VX*1v0UbG|d|vxjf*d+tqm;YaP8 zAGIwB+VxPygVHyD&GKiHhi{W}vsPU&ZNtcBSBIvDcAt{}>FN2ZMF@)^L*TFDm*!W! zJEUDbgb3)iYC<@UsF>V`RJBV-rqcnVA0)&<@$(j5L?mwzx52vfBHAJnRIR zYP^Q=gTlHcfl%1;e}|wh*RgAp9!HybVRI?{VP4-{ojL?k*a~tNVB9(bL9rF)Zsqn? z=xGFVCvp28u*4|vt>^Za=FiQ=X*Xi-aOAvz9%ok#@f6uYc%4pT0f5}xfNrmgM}=I1 z&^Yt*PM~dwxZw(ESM!|N(92bv!^{)16^?Y3D4>h5;mWt~!G!CiSNg5U|ZQ*kcn>dE)Aj=CI-C7uE`Vx;9&E})Paa>)gl;R&lDuUu>Z zMD6Eckx%-Nf1XkW{PNlk05RWBP#`Gmlc80u`yAsF-gXFh<({1Xl+w2l{B=)Jz>r5l z9`{r)&w^~I>YnEPBo`@OkW5~e0xlVW6TNN)ywU_sydDL7ax*H5H%oz_ ztj+=ODiD%sDCo^rAS@5U)n15k1wtdxG%p1 zeP3`NUtHgE-`}CP^Ed9}i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TwTqIRU31OB%X`#r?TrtZ{2QYWGZ!dRk1Z0lp9C=w{}Pn``w<3}&7C&~A4p-u zQ_G8KrFnFD(prfbkvcPHMT|<#ZYv^9b0JWfn<<{svddf{< zE|1!}_ZF8=6E`LJ+Y)Wv;l)1u21u;4om;4Mmsxdf{^a-0&8rEf=j}<^w3{3Fn@o8v1|?oZy2#(pIj$z`|DOq``k_Lr8FdsqH-mq2 zSfgg#c4h`@^_eZIa1<7_n7&o<6#tKcYHkI^&&?eT8zO4%(Er(ue-f!CM5yB2gvY@2 z5;dXneG zsAbnvYmEF|uiX8Rb=T^@R|cFT`t%=vVXxuidpDiG>*42r3GLeXdC*Ho%r}1RL=Tzv v-+Iz^v+1{<_O-XB1L`-|{nm-Lng`L-+q9qRNwdr*JOk!xJoC(hr=tH4Ijz6% diff --git a/js/elf/scf_elf.c b/js/elf/scf_elf.c index 76e5f29..1d5c020 100644 --- a/js/elf/scf_elf.c +++ b/js/elf/scf_elf.c @@ -124,6 +124,22 @@ int scf_elf_add_sym(scf_elf_context_t* elf, const scf_elf_sym_t* sym, const char return -1; } +int scf_elf_add_syms(scf_elf_context_t* elf, const scf_vector_t* syms, const char* sh_name) +{ + scf_elf_sym_t* sym; + int i; + + for (i = 0; i < syms->size; i++) { + sym = syms->data[i]; + + int ret = scf_elf_add_sym(elf, sym, sh_name); + if (ret < 0) + return ret; + } + + return 0; +} + int scf_elf_add_section(scf_elf_context_t* elf, const scf_elf_section_t* section) { if (elf && section) { diff --git a/js/elf/scf_elf.h b/js/elf/scf_elf.h index cd3a606..b3599aa 100644 --- a/js/elf/scf_elf.h +++ b/js/elf/scf_elf.h @@ -92,10 +92,10 @@ int scf_elf_open (scf_elf_context_t** pelf, const char* machine, const char* pat int scf_elf_open2(scf_elf_context_t* elf, const char* machine); int scf_elf_close(scf_elf_context_t* elf); -int scf_elf_add_sym (scf_elf_context_t* elf, const scf_elf_sym_t* sym, const char* sh_name); - -int scf_elf_add_section(scf_elf_context_t* elf, const scf_elf_section_t* section); +int scf_elf_add_sym (scf_elf_context_t* elf, const scf_elf_sym_t* sym, const char* sh_name); +int scf_elf_add_syms(scf_elf_context_t* elf, const scf_vector_t* syms, const char* sh_name); +int scf_elf_add_section (scf_elf_context_t* elf, const scf_elf_section_t* section); int scf_elf_add_rela_section(scf_elf_context_t* elf, const scf_elf_section_t* section, scf_vector_t* relas); int scf_elf_add_dyn_need(scf_elf_context_t* elf, const char* soname); int scf_elf_add_dyn_rela(scf_elf_context_t* elf, const scf_elf_rela_t* rela); diff --git a/js/elf/scf_elf_naja.c b/js/elf/scf_elf_naja.c index a0ccf87..d399243 100644 --- a/js/elf/scf_elf_naja.c +++ b/js/elf/scf_elf_naja.c @@ -60,7 +60,7 @@ static int _naja_elf_link_cs(elf_native_t* naja, elf_section_t* s, elf_section_t case R_AARCH64_ADR_PREL_PG_HI21: - offset >>= 14; + offset >>= 12; offset &= 0x1fffff; *(uint32_t*)(s->data + rela->r_offset) |= offset; @@ -68,7 +68,7 @@ static int _naja_elf_link_cs(elf_native_t* naja, elf_section_t* s, elf_section_t case R_AARCH64_ADD_ABS_LO12_NC: - *(uint32_t*)(s->data + rela->r_offset) |= (sym->sym.st_value & 0x3fff) << 5; + *(uint32_t*)(s->data + rela->r_offset) |= (sym->sym.st_value & 0xfff) << 4; break; default: diff --git a/js/elf/scf_elf_naja_so.c b/js/elf/scf_elf_naja_so.c index 04d800c..073e962 100644 --- a/js/elf/scf_elf_naja_so.c +++ b/js/elf/scf_elf_naja_so.c @@ -1,24 +1,25 @@ #include"scf_elf_naja.h" #include"scf_elf_link.h" -static uint32_t naja_plt_lazy[8] = { - // str x16, lr, [sp, #-16]! - (7 << 26) | (16 << 21) | (3 << 19) | 0x1e, - (7 << 26) | (29 << 21) | (3 << 19) | 0x1e, - (0x2a << 26) | (16 << 21), // adrp x16, 0 - (0 << 26) | (16 << 21) | (3 << 19) | 16, // add x16, x16, #0 - - (4 << 26) | (17 << 21) | (3 << 19) | 16, // ldr x17, [x16, #0] - (0xb << 26) | (17 << 21), // jmp *x17 - (0xf << 26) | (1 << 16), // nop, mov r0, r0 - (0xf << 26) | (1 << 16), // nop, mov r0, r0 +static uint32_t naja_plt_lazy[8] = +{ + (9 << 26) | (10 << 21) | (3 << 18) | 0xe, // push r10 + (9 << 26) | (13 << 21) | (3 << 18) | 0xe, // push lr + (0x35 << 26) | (10 << 21), // adrp r10, 0 + (0 << 26) | (10 << 21) | (3 << 18) | (3 << 16) | 10, // add r10, r10, #0 + + (4 << 26) | (11 << 21) | (3 << 18) | 10, // ldr r11, [r10, #0] + (0x32 << 26) | (11 << 21), // jmp *r11 + (0xc << 26) | (3 << 18) | (0xf << 4), // nop, mov r0, r0 + (0xc << 26) | (3 << 18) | (0xf << 4), // nop, mov r0, r0 }; -static uint32_t naja_plt[4] = { - (0x2a << 26) | (16 << 21), // adrp x16, 0 - (0 << 26) | (16 << 21) | (3 << 19) | 16, // add x16, x16, #0 - (4 << 26) | (17 << 21) | (3 << 19) | 16, // ldr x17, [x16, #0] - (0xb << 26) | (17 << 21), // jmp *x17 +static uint32_t naja_plt[4] = +{ + (0x35 << 26) | (10 << 21), // adrp r10, 0 + (0 << 26) | (10 << 21) | (3 << 18) | (3 << 16) | 10, // add r10, r10, #0 + (4 << 26) | (11 << 21) | (3 << 18) | 10, // ldr r11, [r10, #0] + (0x32 << 26) | (11 << 21), // jmp *r11 }; @@ -693,8 +694,8 @@ int __naja_elf_post_dyn(elf_native_t* naja, uint64_t rx_base, uint64_t rw_base, scf_logi("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset); - plt[2] |= (offset >> 14) & 0x1fffff; - plt[3] |= (got_addr & 0x3fff) << 5; + plt[2] |= (offset >> 12) & 0x1fffff; + plt[3] |= (got_addr & 0xfff) << 4; got_addr += 8; plt_addr += sizeof(naja_plt_lazy); @@ -712,8 +713,8 @@ int __naja_elf_post_dyn(elf_native_t* naja, uint64_t rx_base, uint64_t rw_base, scf_logi("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset); - plt[0] |= (offset >> 14) & 0x1fffff; - plt[1] |= (got_addr & 0x3fff) << 5; + plt[0] |= (offset >> 12) & 0x1fffff; + plt[1] |= (got_addr & 0xfff) << 4; plt += sizeof(naja_plt) / sizeof(naja_plt[0]); plt_addr += sizeof(naja_plt); diff --git a/js/native/risc/scf_naja.c b/js/native/risc/scf_naja.c index 791519a..ef56dff 100644 --- a/js/native/risc/scf_naja.c +++ b/js/native/risc/scf_naja.c @@ -1,5 +1,16 @@ #include"scf_risc.h" +static uint32_t naja_shift(int bytes) +{ + if (bytes <= 1) + return 0; + else if (bytes <= 2) + return 1; + else if (bytes <= 4) + return 2; + return 3; +} + int naja_inst_I2G(scf_3ac_code_t* c, scf_register_t* rd, uint64_t imm, int bytes) { scf_instruction_t* inst; @@ -10,13 +21,13 @@ int naja_inst_I2G(scf_3ac_code_t* c, scf_register_t* rd, uint64_t imm, int bytes if (0 == (invert >> 32)) { // mvn rd, invert[15:0] - opcode = (0xf << 26) | (rd->id << 21) | (1 << 18) | (0x3 << 16) | (invert & 0xffff); + opcode = (0xe << 26) | (rd->id << 21) | (0x3 << 18) | (0x3 << 16) | (invert & 0xffff); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); if (invert >> 16) { - // movk rd, imm[31:16] - opcode = (0xf << 26) | (rd->id << 21) | (1 << 19) | (0x3 << 16)| ((imm >> 16) & 0xffff); + // movt rd, imm[31:16] + opcode = (0xe << 26) | (rd->id << 21) | (0x1 << 18) | (0x2 << 16)| ((imm >> 16) & 0xffff); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); } @@ -25,15 +36,15 @@ int naja_inst_I2G(scf_3ac_code_t* c, scf_register_t* rd, uint64_t imm, int bytes } // mov rd, imm[15:0] - opcode = (0xf << 26) | (rd->id << 21) | (0x3 << 16) | (imm & 0xffff); + opcode = (0xe << 26) | (rd->id << 21) | (0x2 << 16) | (imm & 0xffff); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); imm >>= 16; if (imm & 0xffff) { - // movk rd, imm[31:16] - opcode = (0xf << 26) | (rd->id << 21) | (1 << 19) | (0x3 << 16) | (imm & 0xffff); + // movt rd, imm[31:16] + opcode = (0xe << 26) | (rd->id << 21) | (1 << 18) | (0x2 << 16) | (imm & 0xffff); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); } @@ -42,7 +53,7 @@ int naja_inst_I2G(scf_3ac_code_t* c, scf_register_t* rd, uint64_t imm, int bytes if (imm & 0xffff) { // movk rd, imm[47:32] - opcode = (0xf << 26) | (rd->id << 21) | (2 << 19) | (0x3 << 16) | (imm & 0xffff); + opcode = (0xe << 26) | (rd->id << 21) | (2 << 18) | (0x2 << 16) | (imm & 0xffff); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); } @@ -51,7 +62,7 @@ int naja_inst_I2G(scf_3ac_code_t* c, scf_register_t* rd, uint64_t imm, int bytes if (imm & 0xffff) { // movk rd, imm[63:48] - opcode = (0xf << 26) | (rd->id << 21) | (3 << 19) | (0x3 << 16) | (imm & 0xffff); + opcode = (0xe << 26) | (rd->id << 21) | (3 << 18) | (0x2 << 16) | (imm & 0xffff); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); } @@ -76,20 +87,20 @@ int naja_inst_ADR2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, sc offset = vs->bp_offset; - if (offset >= 0 && offset <= 0x3fff) + if (offset >= 0 && offset <= 0xfff) - opcode = (0 << 26) | (rd->id << 21) | (0x3 << 19) | (offset << 5) | fp->id; + opcode = (0 << 26) | (rd->id << 21) | (0x3 << 18) | (0x3 << 16) | (offset << 4) | fp->id; else if (offset < 0 && -offset <= 0x3fff) - opcode = (1 << 26) | (rd->id << 21) | (0x3 << 19) | ((-offset) << 5) | fp->id; + opcode = (1 << 26) | (rd->id << 21) | (0x3 << 18) | (0x3 << 16) | ((-offset) << 4) | fp->id; else { int ret = naja_inst_I2G(c, rd, offset, 8); if (ret < 0) return ret; - opcode = (0 << 26) | (rd->id << 21) | (rd->id << 5) | fp->id; + opcode = (0 << 26) | (rd->id << 21) | (0x3 << 18) | (rd->id << 4) | fp->id; } inst = risc_make_inst(c, opcode); @@ -98,13 +109,13 @@ int naja_inst_ADR2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, sc } else if (vs->global_flag) { offset = 0; - opcode = (0x2a << 26) | (rd->id << 21); + opcode = (0x35 << 26) | (rd->id << 21); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); RISC_RELA_ADD_CHECK(f->data_relas, rela, c, vs, NULL); rela->type = R_AARCH64_ADR_PREL_PG_HI21; - opcode = (0 << 26) | (rd->id << 21) | (0x3 << 19) | rd->id; + opcode = (0 << 26) | (rd->id << 21) | (0x3 << 18) | (0x3 << 16) | rd->id; inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); RISC_RELA_ADD_CHECK(f->data_relas, rela, c, vs, NULL); @@ -194,7 +205,7 @@ int naja_inst_M2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_ if (offset >= -0xfff && offset <= 0xfff) - opcode = (0x4 << 26) | ((offset & 0x1fff) << 5) | rb->id; + opcode = (0x4 << 26) | ((offset & 0x1fff) << 4) | rb->id; else { int ret = risc_select_free_reg(&ri, c, f, 0); if (ret < 0) { @@ -206,18 +217,18 @@ int naja_inst_M2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_ if (ret < 0) return ret; - opcode = (0xd << 26) | (SIZE << 10) | (ri->id << 5) | rb->id; + opcode = (0xa << 26) | (SIZE << 18) | (ri->id << 4) | rb->id; } if (rd->bytes > size && scf_variable_signed(vs)) - opcode |= 0x1 << 18; + opcode |= 0x1 << 17; - else if (scf_variable_float(vs) && 4 == size) - opcode |= 0x1 << 18; +// else if (scf_variable_float(vs) && 4 == size) +// opcode |= 0x1 << 18; scf_loge("SIZE: %d, size: %d\n", SIZE, size); - opcode |= (rd->id << 21) | SIZE << 19; + opcode |= (rd->id << 21) | SIZE << 18; opcode |= RISC_COLOR_TYPE(rd->color) << 30; inst = risc_make_inst(c, opcode); @@ -308,7 +319,7 @@ int naja_inst_G2M(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_ scf_loge("offset: %ld, SIZE: %d\n", offset, SIZE); if (offset >= -0xfff && offset <= 0xfff) - opcode = (0x6 << 26) | ((offset & 0x1fff) << 5) | rb->id; + opcode = (0x6 << 26) | ((offset & 0x1fff) << 4) | rb->id; else { int ret = risc_select_free_reg(&ri, c, f, 0); if (ret < 0) { @@ -320,14 +331,14 @@ int naja_inst_G2M(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_ if (ret < 0) return ret; - opcode = (0xe << 26) | (SIZE << 10) | (ri->id << 5) | rb->id; + opcode = (0xb << 26) | (SIZE << 18) | (ri->id << 4) | rb->id; } - opcode |= (rs->id << 21) | SIZE << 19; + opcode |= (rs->id << 21) | SIZE << 18; opcode |= RISC_COLOR_TYPE(rs->color) << 30; - if (scf_variable_float(vs) && 4 == size) - opcode |= (1 << 18); +// if (scf_variable_float(vs) && 4 == size) +// opcode |= (1 << 18); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); @@ -351,13 +362,13 @@ int naja_inst_ISTR2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, s uint32_t opcode; - opcode = (0x2a << 26) | (rd->id << 21); + opcode = (0x35 << 26) | (rd->id << 21); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); RISC_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); rela->type = R_AARCH64_ADR_PREL_PG_HI21; - opcode = (0 << 26) | (rd->id << 21) | (0x3 << 19) | rd->id; + opcode = (0 << 26) | (rd->id << 21) | (0x3 << 18) | (0x3 << 16) | rd->id; inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); RISC_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); @@ -368,8 +379,8 @@ int naja_inst_ISTR2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, s int naja_inst_G2P(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_register_t* rb, int32_t offset, int size) { - scf_register_t* ri = NULL; - scf_instruction_t* inst = NULL; + scf_register_t* ri = NULL; + scf_instruction_t* inst = NULL; uint32_t opcode; uint32_t SIZE = 0; @@ -413,7 +424,7 @@ int naja_inst_G2P(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_ return -EINVAL; if (offset >= -0xfff && offset <= 0xfff) - opcode = (0x6 << 26) | ((offset & 0x1fff) << 5) | rb->id; + opcode = (0x6 << 26) | ((offset & 0x1fff) << 4) | rb->id; else { int ret = risc_select_free_reg(&ri, c, f, 0); if (ret < 0) { @@ -425,10 +436,10 @@ int naja_inst_G2P(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_ if (ret < 0) return ret; - opcode = (0xe << 26) | (SIZE << 10) | (ri->id << 5) | rb->id; + opcode = (0xb << 26) | (SIZE << 18) | (ri->id << 4) | rb->id; } - opcode |= (rs->id << 21) | SIZE << 19; + opcode |= (rs->id << 21) | SIZE << 18; opcode |= RISC_COLOR_TYPE(rs->color) << 30; inst = risc_make_inst(c, opcode); @@ -439,8 +450,8 @@ int naja_inst_G2P(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_ int naja_inst_P2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_register_t* rb, int32_t offset, int size) { - scf_register_t* ri = NULL; - scf_instruction_t* inst = NULL; + scf_register_t* ri = NULL; + scf_instruction_t* inst = NULL; uint32_t opcode; uint32_t SIZE = 0; @@ -484,7 +495,7 @@ int naja_inst_P2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_ return -EINVAL; if (offset >= -0xfff && offset <= 0xfff) - opcode = (0x4 << 26) | ((offset & 0x1fff) << 5) | rb->id; + opcode = (0x4 << 26) | ((offset & 0x1fff) << 4) | rb->id; else { int ret = risc_select_free_reg(&ri, c, f, 0); if (ret < 0) { @@ -496,10 +507,10 @@ int naja_inst_P2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_ if (ret < 0) return ret; - opcode = (0xd << 26) | (SIZE << 10) | (ri->id << 5) | rb->id; + opcode = (0xa << 26) | (SIZE << 18) | (ri->id << 4) | rb->id; } - opcode |= (rd->id << 21) | SIZE << 19; + opcode |= (rd->id << 21) | SIZE << 18; opcode |= RISC_COLOR_TYPE(rd->color) << 30; inst = risc_make_inst(c, opcode); @@ -514,11 +525,11 @@ int naja_inst_ADRP2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, s uint32_t opcode = 0; - if (offset >= 0 && offset <= 0x3fff) - opcode = (0 << 26) | (rd->id << 21) | (3 << 19) | (offset << 5) | rb->id; + if (offset >= 0 && offset <= 0xfff) + opcode = (0 << 26) | (rd->id << 21) | (3 << 18) | (3 << 16) | (offset << 4) | rb->id; - else if (offset < 0 && offset >= -0x3fff) - opcode = (1 << 26) | (rd->id << 21) | (3 << 19) | ((-offset) << 5) | rb->id; + else if (offset < 0 && offset >= -0xfff) + opcode = (1 << 26) | (rd->id << 21) | (3 << 18) | (3 << 16) | ((-offset) << 4) | rb->id; else { int ret = risc_select_free_reg(&r, c, f, 0); @@ -529,7 +540,7 @@ int naja_inst_ADRP2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, s if (ret < 0) return ret; - opcode = (0 << 26) | (rd->id << 21) | (r->id << 5) | rb->id; + opcode = (0 << 26) | (rd->id << 21) | (3 << 18) | (r->id << 4) | rb->id; } inst = risc_make_inst(c, opcode); @@ -539,9 +550,9 @@ int naja_inst_ADRP2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, s int naja_inst_ADRSIB2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_sib_t* sib) { - scf_register_t* rb = sib->base; - scf_register_t* ri = sib->index; - scf_instruction_t* inst = NULL; + scf_register_t* rb = sib->base; + scf_register_t* ri = sib->index; + scf_instruction_t* inst = NULL; assert(0 == sib->disp); @@ -565,7 +576,7 @@ int naja_inst_ADRSIB2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, else return -EINVAL; - opcode = (0 << 26) | (rd->id << 21) | (SH << 10) | (ri->id << 5) | rb->id; + opcode = (0 << 26) | (rd->id << 21) | (3 << 18) | (SH << 16) | (ri->id << 4) | rb->id; inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); return 0; @@ -573,9 +584,9 @@ int naja_inst_ADRSIB2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, int naja_inst_SIB2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, scf_sib_t* sib) { - scf_register_t* rb = sib->base; - scf_register_t* ri = sib->index; - scf_instruction_t* inst = NULL; + scf_register_t* rb = sib->base; + scf_register_t* ri = sib->index; + scf_instruction_t* inst = NULL; assert(0 == sib->disp); @@ -602,8 +613,8 @@ int naja_inst_SIB2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, sc else return -EINVAL; - opcode = (0xd << 26) | (rd->id << 21) | (SIZE << 10) | (ri->id << 5) | rb->id; - opcode |= SIZE << 19; + opcode = (0xa << 26) | (rd->id << 21) | (SIZE << 10) | (ri->id << 4) | rb->id; + opcode |= SIZE << 18; opcode |= RISC_COLOR_TYPE(rd->color) << 30; inst = risc_make_inst(c, opcode); @@ -614,9 +625,9 @@ int naja_inst_SIB2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, sc int naja_inst_G2SIB(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_sib_t* sib) { - scf_register_t* rb = sib->base; - scf_register_t* ri = sib->index; - scf_instruction_t* inst = NULL; + scf_register_t* rb = sib->base; + scf_register_t* ri = sib->index; + scf_instruction_t* inst = NULL; assert(0 == sib->disp); @@ -643,9 +654,9 @@ int naja_inst_G2SIB(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, sc else return -EINVAL; - opcode = (0xe << 26) | (rs->id << 21) | (SIZE << 10) | (ri->id << 5) | rb->id; - opcode |= SIZE << 19; - opcode |= RISC_COLOR_TYPE(rs->color) << 26; + opcode = (0xb << 26) | (rs->id << 21) | (SIZE << 10) | (ri->id << 4) | rb->id; + opcode |= SIZE << 18; + opcode |= RISC_COLOR_TYPE(rs->color) << 16; inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); @@ -663,8 +674,9 @@ scf_instruction_t* naja_inst_PUSH(scf_3ac_code_t* c, scf_register_t* r) scf_instruction_t* inst; uint32_t opcode; - opcode = (0x7 << 26) | (r->id << 21) | (3 << 19) | 0x1e; - inst = risc_make_inst(c, opcode); + opcode = (0x9 << 26) | (r->id << 21) | (3 << 18) | 0xe; + opcode |= RISC_COLOR_TYPE(r->color) << 17; + inst = risc_make_inst(c, opcode); return inst; } @@ -674,8 +686,9 @@ scf_instruction_t* naja_inst_POP(scf_3ac_code_t* c, scf_register_t* r) scf_instruction_t* inst; uint32_t opcode; - opcode = (0x5 << 26) | (r->id << 21) | (3 << 19) | 0x1e; - inst = risc_make_inst(c, opcode); + opcode = (0x8 << 26) | (r->id << 21) | (3 << 18) | 0xe; + opcode |= RISC_COLOR_TYPE(r->color) << 17; + inst = risc_make_inst(c, opcode); return inst; } @@ -685,7 +698,7 @@ scf_instruction_t* naja_inst_RET(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = 0x38 << 26; + opcode = 0x36 << 26; inst = risc_make_inst(c, opcode); return inst; @@ -696,7 +709,7 @@ scf_instruction_t* naja_inst_MOV_SP(scf_3ac_code_t* c, scf_register_t* rd, scf_r scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (0x1 << 16) | rs->id; + opcode = (0xc << 26) | (rd->id << 21) | (3 << 18) | (0xf << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -707,7 +720,7 @@ scf_instruction_t* naja_inst_MOV_G(scf_3ac_code_t* c, scf_register_t* rd, scf_re scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (0x1 << 16) | rs->id; + opcode = (0xc << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (0xf << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -718,7 +731,7 @@ scf_instruction_t* naja_inst_MVN(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (0x3 << 19) | (0x2 << 16) | rs->id; + opcode = (0xe << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (1 << 16) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -729,7 +742,7 @@ scf_instruction_t* naja_inst_FMOV_G(scf_3ac_code_t* c, scf_register_t* rd, scf_r scf_instruction_t* inst; uint32_t opcode; - opcode = (0x1f << 26) | (rd->id << 21) | (0x3 << 19) | (0x3 << 16) | rs->id; + opcode = (0x1c << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -750,7 +763,7 @@ scf_instruction_t* naja_inst_MOVSX(scf_3ac_code_t* c, scf_register_t* rd, scf_re else return NULL; - opcode = (0xf << 26) | (rd->id << 21) | (SH << 19) | (1 << 18) | (0x2 << 16) | rs->id; + opcode = (0xd << 26) | (rd->id << 21) | (SH << 18) | (1 << 17) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -771,7 +784,7 @@ scf_instruction_t* naja_inst_MOVZX(scf_3ac_code_t* c, scf_register_t* rd, scf_re else return NULL; - opcode = (0xf << 26) | (rd->id << 21) | (SH << 19) | (0x2 << 16)| rs->id; + opcode = (0xd << 26) | (rd->id << 21) | (SH << 18) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -783,7 +796,7 @@ scf_instruction_t* naja_inst_CVTSS2SD(scf_3ac_code_t* c, scf_register_t* rd, scf uint32_t opcode; uint32_t S; - opcode = (0x1f << 26) | (rd->id << 21) | (2 << 19) | rs->id; + opcode = (0x1d << 26) | (rd->id << 21) | (3 << 18) | (1 << 12) | (1 << 6) | (2 << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -794,7 +807,7 @@ scf_instruction_t* naja_inst_CVTSD2SS(scf_3ac_code_t* c, scf_register_t* rd, scf scf_instruction_t* inst; uint32_t opcode; - opcode = (0x1f << 26) | (rd->id << 21) | (3 << 19) | rs->id; + opcode = (0x1d << 26) | (rd->id << 21) | (2 << 18) | (1 << 12) | (1 << 6) | (3 << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -811,7 +824,7 @@ scf_instruction_t* naja_inst_CVTF2SI(scf_3ac_code_t* c, scf_register_t* rd, scf_ else SH = 3; - opcode = (0x1f << 26) | (rd->id << 21) | (SH << 19) | (1 << 18) | (0x1 << 16) | rs->id; + opcode = (0x1d << 26) | (rd->id << 21) | (SH << 18) | (3 << 12) | (1 << 6) | (naja_shift(rs->bytes) << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -828,7 +841,7 @@ scf_instruction_t* naja_inst_CVTF2UI(scf_3ac_code_t* c, scf_register_t* rd, scf_ else SH = 3; - opcode = (0x1f << 26) | (rd->id << 21) | (SH << 19) | (0x1 << 16) | rs->id; + opcode = (0x1d << 26) | (rd->id << 21) | (SH << 18) | (2 << 12) | (1 << 6) | (naja_shift(rs->bytes) << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -845,7 +858,7 @@ scf_instruction_t* naja_inst_CVTSI2F(scf_3ac_code_t* c, scf_register_t* rd, scf_ else SH = 3; - opcode = (0x1f << 26) | (rd->id << 21) | (SH << 19) | (1 << 18) | (0x2 << 16) | rs->id; + opcode = (0x1d << 26) | (rd->id << 21) | (SH << 18) | (1 << 12) | (3 << 6) | (naja_shift(rs->bytes) << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -862,7 +875,7 @@ scf_instruction_t* naja_inst_CVTUI2F(scf_3ac_code_t* c, scf_register_t* rd, scf_ else SH = 3; - opcode = (0x1f << 26) | (rd->id << 21) | (SH << 19) | (0x2 << 16) | rs->id; + opcode = (0x1d << 26) | (rd->id << 21) | (SH << 18) | (1 << 12) | (2 << 6) | (naja_shift(rs->bytes) << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -873,12 +886,12 @@ scf_instruction_t* naja_inst_SUB_IMM(scf_3ac_code_t* c, scf_function_t* f, scf_r scf_instruction_t* inst; uint32_t opcode; - if (imm > 0x3fff) { + if (imm > 0xfff) { scf_loge("NOT support too big imm: %#lx\n", imm); return NULL; } - opcode = (1 << 26) | (rd->id << 21) | (3 << 19) | (imm << 5) | rs->id; + opcode = (1 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (3 << 16) | (imm << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -889,12 +902,12 @@ scf_instruction_t* naja_inst_CMP_IMM(scf_3ac_code_t* c, scf_function_t* f, scf_r scf_instruction_t* inst; uint32_t opcode; - if (imm > 0x3fff) { + if (imm > 0xfff) { scf_loge("NOT support too big imm: %#lx\n", imm); return NULL; } - opcode = (1 << 26) | (0x1f << 21) | (3 << 19) | (imm << 5) | rs->id; + opcode = (1 << 26) | (0xf << 21) | (naja_shift(rs->bytes) << 18) | (3 << 16) | (imm << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -905,12 +918,12 @@ scf_instruction_t* naja_inst_ADD_IMM(scf_3ac_code_t* c, scf_function_t* f, scf_r scf_instruction_t* inst; uint32_t opcode; - if (imm > 0x3fff) { + if (imm > 0xfff) { scf_loge("NOT support too big imm: %#lx\n", imm); return NULL; } - opcode = (0 << 26) | (rd->id << 21) | (3 << 19) | (imm << 5) | rs->id; + opcode = (0 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (3 << 16) | (imm << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -921,7 +934,7 @@ scf_instruction_t* naja_inst_ADD_G(scf_3ac_code_t* c, scf_register_t* rd, scf_re scf_instruction_t* inst; uint32_t opcode; - opcode = (0 << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (0 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -932,7 +945,7 @@ scf_instruction_t* naja_inst_SHL(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (0xc << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -943,7 +956,7 @@ scf_instruction_t* naja_inst_SHR(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (1 << 19) | (rs1->id << 5) | rs0->id; + opcode = (0xc << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (1 << 16) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -954,7 +967,7 @@ scf_instruction_t* naja_inst_ASR(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (2 << 19) | (rs1->id << 5) | rs0->id; + opcode = (0xc << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (2 << 16) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -965,7 +978,7 @@ scf_instruction_t* naja_inst_AND_G(scf_3ac_code_t* c, scf_register_t* rd, scf_re scf_instruction_t* inst; uint32_t opcode; - opcode = (0x8 << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (0x5 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -976,7 +989,7 @@ scf_instruction_t* naja_inst_OR_G(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x9 << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (0x7 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -987,7 +1000,7 @@ scf_instruction_t* naja_inst_SUB_G(scf_3ac_code_t* c, scf_register_t* rd, scf_re scf_instruction_t* inst; uint32_t opcode; - opcode = (0x1 << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (1 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -998,7 +1011,7 @@ scf_instruction_t* naja_inst_CMP_G(scf_3ac_code_t* c, scf_register_t* rs0, scf_r scf_instruction_t* inst; uint32_t opcode; - opcode = (0x1 << 26) | (0x1f << 21) | (rs1->id << 5) | rs0->id; + opcode = (1 << 26) | (0xf << 21) | (naja_shift(rs0->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1009,7 +1022,7 @@ scf_instruction_t* naja_inst_FCMP(scf_3ac_code_t* c, scf_register_t* rs0, scf_re scf_instruction_t* inst; uint32_t opcode; - opcode = (0x11 << 26) | (0x1f << 21) | (rs1->id << 5) | rs0->id; + opcode = (0x11 << 26) | (0xf << 21) | (naja_shift(rs0->bytes) << 18) | (1 << 16) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1020,7 +1033,7 @@ scf_instruction_t* naja_inst_NEG(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0xf << 26) | (rd->id << 21) | (0x7 << 18) | (0x2 << 16) | rs->id; + opcode = (0xe << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -1031,7 +1044,7 @@ scf_instruction_t* naja_inst_TEQ(scf_3ac_code_t* c, scf_register_t* rs) scf_instruction_t* inst; uint32_t opcode; - opcode = (0x8 << 26) | (0x1f << 21) | (rs->id << 5) | rs->id; + opcode = (0x5 << 26) | (0xf << 21) | (naja_shift(rs->bytes) << 18) | (rs->id << 4) | rs->id; inst = risc_make_inst(c, opcode); return inst; @@ -1042,7 +1055,7 @@ scf_instruction_t* naja_inst_FADD(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x10 << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (0x10 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1053,7 +1066,7 @@ scf_instruction_t* naja_inst_FSUB(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x11 << 26) | (rd->id << 21) | (rs1->id << 5) | rs0->id; + opcode = (0x11 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1064,7 +1077,7 @@ scf_instruction_t* naja_inst_MUL(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0x2 << 26) | (rd->id << 21) | (2 << 19) | (rs1->id << 5) | rs0->id; + opcode = (2 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (0xf << 12) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1075,7 +1088,7 @@ scf_instruction_t* naja_inst_FMUL(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x12 << 26) | (rd->id << 21) | (2 << 19) | (1 << 18) | (rs1->id << 5) | rs0->id; + opcode = (0x12 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (0xf << 12) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1086,7 +1099,7 @@ scf_instruction_t* naja_inst_FDIV(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x13 << 26) | (rd->id << 21) | (2 << 19) | (1 << 18) | (rs1->id << 5) | rs0->id; + opcode = (0x13 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1097,7 +1110,7 @@ scf_instruction_t* naja_inst_DIV(scf_3ac_code_t* c, scf_register_t* rd, scf_regi scf_instruction_t* inst; uint32_t opcode; - opcode = (0x3 << 26) | (rd->id << 21) | (2 << 19) | (rs1->id << 5) | rs0->id; + opcode = (3 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1108,7 +1121,7 @@ scf_instruction_t* naja_inst_SDIV(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x3 << 26) | (rd->id << 21) | (2 << 19) | (1 << 18) | (rs1->id << 5) | rs0->id; + opcode = (3 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (1 << 17) | (rs1->id << 4) | rs0->id; inst = risc_make_inst(c, opcode); return inst; @@ -1119,7 +1132,7 @@ scf_instruction_t* naja_inst_MSUB(scf_3ac_code_t* c, scf_register_t* rd, scf_reg scf_instruction_t* inst; uint32_t opcode; - opcode = (0x2 << 26) | (rd->id << 21) | (1 << 19) | (1 << 18) | (ra->id << 10) | (rm->id << 5) | rn->id; + opcode = (2 << 26) | (rd->id << 21) | (naja_shift(rd->bytes) << 18) | (1 << 16) | (ra->id << 12) | (rm->id << 4) | rn->id; inst = risc_make_inst(c, opcode); return inst; @@ -1131,7 +1144,7 @@ int naja_inst_BL(scf_3ac_code_t* c, scf_function_t* f, scf_function_t* pf) scf_rela_t* rela; uint32_t opcode; - opcode = (0x1a << 26); + opcode = (0x31 << 26); inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); @@ -1146,7 +1159,7 @@ scf_instruction_t* naja_inst_BLR(scf_3ac_code_t* c, scf_register_t* r) scf_instruction_t* inst; uint32_t opcode; - opcode = (0x1b << 26) | (r->id << 21); + opcode = (0x33 << 26) | (r->id << 21); inst = risc_make_inst(c, opcode); return inst; @@ -1158,7 +1171,7 @@ scf_instruction_t* naja_inst_SETZ(scf_3ac_code_t* c, scf_register_t* rd) uint32_t opcode; uint32_t cc = 1; - opcode = (0xc << 26) | (rd->id << 21); + opcode = (0x34 << 26) | (rd->id << 21); inst = risc_make_inst(c, opcode); return inst; @@ -1169,7 +1182,7 @@ scf_instruction_t* naja_inst_SETNZ(scf_3ac_code_t* c, scf_register_t* rd) uint32_t opcode; uint32_t cc = 0; - opcode = (0xc << 26) | (rd->id << 21) | (1 << 1); + opcode = (0x34 << 26) | (rd->id << 21) | (1 << 1); inst = risc_make_inst(c, opcode); return inst; @@ -1179,7 +1192,7 @@ scf_instruction_t* naja_inst_SETGT(scf_3ac_code_t* c, scf_register_t* rd) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xc << 26) | (rd->id << 21) | (3 << 1); + opcode = (0x34 << 26) | (rd->id << 21) | (3 << 1); inst = risc_make_inst(c, opcode); return inst; @@ -1189,7 +1202,7 @@ scf_instruction_t* naja_inst_SETGE(scf_3ac_code_t* c, scf_register_t* rd) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xc << 26) | (rd->id << 21) | (2 << 1); + opcode = (0x34 << 26) | (rd->id << 21) | (2 << 1); inst = risc_make_inst(c, opcode); return inst; @@ -1199,7 +1212,7 @@ scf_instruction_t* naja_inst_SETLT(scf_3ac_code_t* c, scf_register_t* rd) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (rd->id << 21) | (5 << 1); + opcode = (0x34 << 26) | (rd->id << 21) | (5 << 1); inst = risc_make_inst(c, opcode); return inst; @@ -1209,7 +1222,7 @@ scf_instruction_t* naja_inst_SETLE(scf_3ac_code_t* c, scf_register_t* rd) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (rd->id << 21) | (4 << 1); + opcode = (0x34 << 26) | (rd->id << 21) | (4 << 1); inst = risc_make_inst(c, opcode); return inst; @@ -1220,7 +1233,7 @@ scf_instruction_t* naja_inst_JMP(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = 0xa << 26; + opcode = 0x30 << 26; inst = risc_make_inst(c, opcode); return inst; @@ -1231,7 +1244,7 @@ scf_instruction_t* naja_inst_JZ(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | 1; + opcode = (0x32 << 26) | 1; inst = risc_make_inst(c, opcode); return inst; @@ -1242,7 +1255,7 @@ scf_instruction_t* naja_inst_JNZ(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (1 << 1) | 1; + opcode = (0x32 << 26) | (1 << 1) | 1; inst = risc_make_inst(c, opcode); return inst; @@ -1253,7 +1266,7 @@ scf_instruction_t* naja_inst_JGT(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (3 << 1) | 1; + opcode = (0x32 << 26) | (3 << 1) | 1; inst = risc_make_inst(c, opcode); return inst; @@ -1264,7 +1277,7 @@ scf_instruction_t* naja_inst_JGE(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (2 << 1) | 1; + opcode = (0x32 << 26) | (2 << 1) | 1; inst = risc_make_inst(c, opcode); return inst; @@ -1275,7 +1288,7 @@ scf_instruction_t* naja_inst_JLT(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (5 << 1) | 1; + opcode = (0x32 << 26) | (5 << 1) | 1; inst = risc_make_inst(c, opcode); return inst; @@ -1286,7 +1299,7 @@ scf_instruction_t* naja_inst_JLE(scf_3ac_code_t* c) scf_instruction_t* inst; uint32_t opcode; - opcode = (0xb << 26) | (4 << 1) | 1; + opcode = (0x32 << 26) | (4 << 1) | 1; inst = risc_make_inst(c, opcode); return inst; @@ -1318,7 +1331,7 @@ void naja_set_jmp_offset(scf_instruction_t* inst, int32_t bytes) opcode |= inst->code[2] << 16; opcode |= inst->code[3] << 24; - if (0xb == (opcode >> 26) && 1 == (opcode & 1)) { + if (0x32 == (opcode >> 26) && 1 == (opcode & 1)) { if (bytes >= 0 && bytes < (0x1 << 20)) { bytes >>= 2; @@ -1338,7 +1351,7 @@ void naja_set_jmp_offset(scf_instruction_t* inst, int32_t bytes) inst->code[3] |= 0x3 & (bytes >> 24); } else { - assert(0xa == (opcode >> 26)); + assert(0x30 == (opcode >> 26)); bytes >>= 2; @@ -1354,8 +1367,8 @@ void naja_set_jmp_offset(scf_instruction_t* inst, int32_t bytes) int naja_cmp_update(scf_3ac_code_t* c, scf_function_t* f, scf_instruction_t* cmp) { scf_instruction_t* inst; - scf_register_t* r16 = f->rops->find_register_type_id_bytes(0, 16, 8); - scf_register_t* r17 = f->rops->find_register_type_id_bytes(0, 17, 8); + scf_register_t* r10 = f->rops->find_register_type_id_bytes(0, 10, 8); + scf_register_t* r11 = f->rops->find_register_type_id_bytes(0, 11, 8); scf_register_t* r0; uint32_t opcode; @@ -1371,34 +1384,34 @@ int naja_cmp_update(scf_3ac_code_t* c, scf_function_t* f, scf_instruction_t* cmp switch (opcode >> 21) { - case 0x3f: - SH = (opcode >> 19) & 0x3; + case 0x2f: + SH = (opcode >> 16) & 0x3; if (0x3 == SH) { - i0 = opcode & 0x1f; + i0 = opcode & 0xf; r0 = f->rops->find_register_type_id_bytes(0, i0, 8); - inst = f->iops->MOV_G(c, r16, r0); // use r16 to backup r0 + inst = f->iops->MOV_G(c, r10, r0); // use r10 to backup r0 RISC_INST_ADD_CHECK(c->instructions, inst); - opcode &= ~0x1f; - opcode |= 0x10; + opcode &= ~0xf; + opcode |= 0xa; } else { - i0 = opcode & 0x1f; - i1 = (opcode >> 5) & 0x1f; + i0 = opcode & 0xf; + i1 = (opcode >> 4) & 0xf; r0 = f->rops->find_register_type_id_bytes(0, i0, 8); - inst = f->iops->MOV_G(c, r16, r0); // use r16 to backup r0 + inst = f->iops->MOV_G(c, r10, r0); // use r10 to backup r0 RISC_INST_ADD_CHECK(c->instructions, inst); r0 = f->rops->find_register_type_id_bytes(0, i1, 8); - inst = f->iops->MOV_G(c, r17, r0); // use r17 to backup r1 + inst = f->iops->MOV_G(c, r11, r0); // use r11 to backup r1 RISC_INST_ADD_CHECK(c->instructions, inst); - opcode &= ~0x1f; - opcode |= 0x10; + opcode &= ~0xf; + opcode |= 0xa; - opcode &= ~(0x1f << 5); - opcode |= (0x11 << 5); + opcode &= ~(0xf << 4); + opcode |= (0xb << 4); } break; default: diff --git a/js/native/risc/scf_risc_inst.c b/js/native/risc/scf_risc_inst.c index 221af0e..2d64b8d 100644 --- a/js/native/risc/scf_risc_inst.c +++ b/js/native/risc/scf_risc_inst.c @@ -442,6 +442,9 @@ static int _risc_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) scf_register_t* sp = f->rops->find_register("sp"); scf_register_t* x0 = f->rops->find_register("x0"); + if (!x0) + x0 = f->rops->find_register("r0"); + lr->used = 1; sp->used = 1; diff --git a/js/native/risc/scf_risc_reg.h b/js/native/risc/scf_risc_reg.h index 4a43cc7..abc0d7c 100644 --- a/js/native/risc/scf_risc_reg.h +++ b/js/native/risc/scf_risc_reg.h @@ -56,4 +56,3 @@ int risc_array_index_reg(scf_sib_t* sib, scf_dag_node_t* base, s void risc_call_rabi(int* p_nints, int* p_nfloats, scf_3ac_code_t* c, scf_function_t* f); #endif - diff --git a/js/native/risc/scf_risc_reg_naja.c b/js/native/risc/scf_risc_reg_naja.c index bd3c8ac..9b6ff13 100644 --- a/js/native/risc/scf_risc_reg_naja.c +++ b/js/native/risc/scf_risc_reg_naja.c @@ -1,237 +1,166 @@ #include"scf_risc.h" -#define SCF_RISC_REG_FP 28 -#define SCF_RISC_REG_LR 29 -#define SCF_RISC_REG_SP 30 -#define SCF_RISC_REG_NULL 31 - -scf_register_t naja_registers[] = { - - {0, 4, "w0", RISC_COLOR(0, 0, 0xf), NULL, 0, 0}, - {0, 8, "x0", RISC_COLOR(0, 0, 0xff), NULL, 0, 0}, - - {1, 4, "w1", RISC_COLOR(0, 1, 0xf), NULL, 0, 0}, - {1, 8, "x1", RISC_COLOR(0, 1, 0xff), NULL, 0, 0}, - - {2, 4, "w2", RISC_COLOR(0, 2, 0xf), NULL, 0, 0}, - {2, 8, "x2", RISC_COLOR(0, 2, 0xff), NULL, 0, 0}, - - {3, 4, "w3", RISC_COLOR(0, 3, 0xf), NULL, 0, 0}, - {3, 8, "x3", RISC_COLOR(0, 3, 0xff), NULL, 0, 0}, - - {4, 4, "w4", RISC_COLOR(0, 4, 0xf), NULL, 0, 0}, - {4, 8, "x4", RISC_COLOR(0, 4, 0xff), NULL, 0, 0}, - - {5, 4, "w5", RISC_COLOR(0, 5, 0xf), NULL, 0, 0}, - {5, 8, "x5", RISC_COLOR(0, 5, 0xff), NULL, 0, 0}, - - {6, 4, "w6", RISC_COLOR(0, 6, 0xf), NULL, 0, 0}, - {6, 8, "x6", RISC_COLOR(0, 6, 0xff), NULL, 0, 0}, - - {7, 4, "w7", RISC_COLOR(0, 7, 0xf), NULL, 0, 0}, - {7, 8, "x7", RISC_COLOR(0, 7, 0xff), NULL, 0, 0}, - -// not use x8 - -// {8, 4, "w8", RISC_COLOR(0, 8, 0xf), NULL, 0}, -// {8, 8, "x8", RISC_COLOR(0, 8, 0xff), NULL, 0}, - - {9, 4, "w9", RISC_COLOR(0, 9, 0xf), NULL, 0, 0}, - {9, 8, "x9", RISC_COLOR(0, 9, 0xff), NULL, 0, 0}, - - {10, 4, "w10", RISC_COLOR(0, 10, 0xf), NULL, 0, 0}, - {10, 8, "x10", RISC_COLOR(0, 10, 0xff), NULL, 0, 0}, - - {11, 4, "w11", RISC_COLOR(0, 11, 0xf), NULL, 0, 0}, - {11, 8, "x11", RISC_COLOR(0, 11, 0xff), NULL, 0, 0}, - - {12, 4, "w12", RISC_COLOR(0, 12, 0xf), NULL, 0, 0}, - {12, 8, "x12", RISC_COLOR(0, 12, 0xff), NULL, 0, 0}, - - {13, 4, "w13", RISC_COLOR(0, 13, 0xf), NULL, 0, 0}, - {13, 8, "x13", RISC_COLOR(0, 13, 0xff), NULL, 0, 0}, - - {14, 4, "w14", RISC_COLOR(0, 14, 0xf), NULL, 0, 0}, - {14, 8, "x14", RISC_COLOR(0, 14, 0xff), NULL, 0, 0}, - - {15, 4, "w15", RISC_COLOR(0, 15, 0xf), NULL, 0, 0}, - {15, 8, "x15", RISC_COLOR(0, 15, 0xff), NULL, 0, 0}, - -// not use x16, x17, x18 - - {16, 4, "w16", RISC_COLOR(0, 16, 0xf), NULL, 0, 0}, - {16, 8, "x16", RISC_COLOR(0, 16, 0xff), NULL, 0, 0}, - - {17, 4, "w17", RISC_COLOR(0, 17, 0xf), NULL, 0, 0}, - {17, 8, "x17", RISC_COLOR(0, 17, 0xff), NULL, 0, 0}, - -// {18, 4, "w18", RISC_COLOR(0, 18, 0xf), NULL, 0, 0}, -// {18, 8, "x18", RISC_COLOR(0, 18, 0xff), NULL, 0, 0}, - - {19, 4, "w19", RISC_COLOR(0, 19, 0xf), NULL, 0, 0}, - {19, 8, "x19", RISC_COLOR(0, 19, 0xff), NULL, 0, 0}, - - {20, 4, "w20", RISC_COLOR(0, 20, 0xf), NULL, 0, 0}, - {20, 8, "x20", RISC_COLOR(0, 20, 0xff), NULL, 0, 0}, - - {21, 4, "w21", RISC_COLOR(0, 21, 0xf), NULL, 0, 0}, - {21, 8, "x21", RISC_COLOR(0, 21, 0xff), NULL, 0, 0}, - - {22, 4, "w22", RISC_COLOR(0, 22, 0xf), NULL, 0, 0}, - {22, 8, "x22", RISC_COLOR(0, 22, 0xff), NULL, 0, 0}, - - {23, 4, "w23", RISC_COLOR(0, 23, 0xf), NULL, 0, 0}, - {23, 8, "x23", RISC_COLOR(0, 23, 0xff), NULL, 0, 0}, - - {24, 4, "w24", RISC_COLOR(0, 24, 0xf), NULL, 0, 0}, - {24, 8, "x24", RISC_COLOR(0, 24, 0xff), NULL, 0, 0}, - - {25, 4, "w25", RISC_COLOR(0, 25, 0xf), NULL, 0, 0}, - {25, 8, "x25", RISC_COLOR(0, 25, 0xff), NULL, 0, 0}, - - {26, 4, "w26", RISC_COLOR(0, 26, 0xf), NULL, 0, 0}, - {26, 8, "x26", RISC_COLOR(0, 26, 0xff), NULL, 0, 0}, - - {27, 4, "w27", RISC_COLOR(0, 27, 0xf), NULL, 0, 0}, - {27, 8, "x27", RISC_COLOR(0, 27, 0xff), NULL, 0, 0}, - -// fp = x28 = bp - {28, 4, "w28", RISC_COLOR(0, 28, 0xf), NULL, 0, 0}, - {28, 8, "fp", RISC_COLOR(0, 28, 0xff), NULL, 0, 0}, -// lr = x29 - {29, 4, "w29", RISC_COLOR(0, 29, 0xf), NULL, 0, 0}, - {29, 8, "lr", RISC_COLOR(0, 29, 0xff), NULL, 0, 0}, - {30, 8, "sp", RISC_COLOR(0, 30, 0xff), NULL, 0, 0}, -// {31, 8, "null", RISC_COLOR(0, 31, 0xff), NULL, 0, 0}, - +#define SCF_RISC_REG_FP 12 +#define SCF_RISC_REG_LR 13 +#define SCF_RISC_REG_SP 14 +#define SCF_RISC_REG_NULL 15 +scf_register_t naja_registers[] = +{ + {0, 1, "r0b", RISC_COLOR(0, 0, 0x1), NULL, 0, 0}, + {0, 2, "r0w", RISC_COLOR(0, 0, 0x3), NULL, 0, 0}, + {0, 4, "r0d", RISC_COLOR(0, 0, 0xf), NULL, 0, 0}, + {0, 8, "r0", RISC_COLOR(0, 0, 0xff), NULL, 0, 0}, + + {1, 1, "r1b", RISC_COLOR(0, 1, 0x1), NULL, 0, 0}, + {1, 2, "r1w", RISC_COLOR(0, 1, 0x3), NULL, 0, 0}, + {1, 4, "r1d", RISC_COLOR(0, 1, 0xf), NULL, 0, 0}, + {1, 8, "r1", RISC_COLOR(0, 1, 0xff), NULL, 0, 0}, + + {2, 1, "r2b", RISC_COLOR(0, 2, 0x1), NULL, 0, 0}, + {2, 2, "r2w", RISC_COLOR(0, 2, 0x3), NULL, 0, 0}, + {2, 4, "r2d", RISC_COLOR(0, 2, 0xf), NULL, 0, 0}, + {2, 8, "r2", RISC_COLOR(0, 2, 0xff), NULL, 0, 0}, + + {3, 1, "r3b", RISC_COLOR(0, 3, 0x1), NULL, 0, 0}, + {3, 2, "r3w", RISC_COLOR(0, 3, 0x3), NULL, 0, 0}, + {3, 4, "r3d", RISC_COLOR(0, 3, 0xf), NULL, 0, 0}, + {3, 8, "r3", RISC_COLOR(0, 3, 0xff), NULL, 0, 0}, + + {4, 1, "r4b", RISC_COLOR(0, 4, 0x1), NULL, 0, 0}, + {4, 2, "r4w", RISC_COLOR(0, 4, 0x3), NULL, 0, 0}, + {4, 4, "r4d", RISC_COLOR(0, 4, 0xf), NULL, 0, 0}, + {4, 8, "r4", RISC_COLOR(0, 4, 0xff), NULL, 0, 0}, + + {5, 1, "r5b", RISC_COLOR(0, 5, 0x1), NULL, 0, 0}, + {5, 2, "r5w", RISC_COLOR(0, 5, 0x3), NULL, 0, 0}, + {5, 4, "r5d", RISC_COLOR(0, 5, 0xf), NULL, 0, 0}, + {5, 8, "r5", RISC_COLOR(0, 5, 0xff), NULL, 0, 0}, + + {6, 1, "r6b", RISC_COLOR(0, 6, 0x1), NULL, 0, 0}, + {6, 2, "r6w", RISC_COLOR(0, 6, 0x3), NULL, 0, 0}, + {6, 4, "r6d", RISC_COLOR(0, 6, 0xf), NULL, 0, 0}, + {6, 8, "r6", RISC_COLOR(0, 6, 0xff), NULL, 0, 0}, + + {7, 1, "r7b", RISC_COLOR(0, 7, 0x1), NULL, 0, 0}, + {7, 2, "r7w", RISC_COLOR(0, 7, 0x3), NULL, 0, 0}, + {7, 4, "r7d", RISC_COLOR(0, 7, 0xf), NULL, 0, 0}, + {7, 8, "r7", RISC_COLOR(0, 7, 0xff), NULL, 0, 0}, + + {8, 1, "r8b", RISC_COLOR(0, 8, 0x1), NULL, 0, 0}, + {8, 2, "r8w", RISC_COLOR(0, 8, 0x3), NULL, 0, 0}, + {8, 4, "r8d", RISC_COLOR(0, 8, 0xf), NULL, 0, 0}, + {8, 8, "r8", RISC_COLOR(0, 8, 0xff), NULL, 0, 0}, + + {9, 1, "r9b", RISC_COLOR(0, 9, 0x1), NULL, 0, 0}, + {9, 2, "r9w", RISC_COLOR(0, 9, 0x3), NULL, 0, 0}, + {9, 4, "r9d", RISC_COLOR(0, 9, 0xf), NULL, 0, 0}, + {9, 8, "r9", RISC_COLOR(0, 9, 0xff), NULL, 0, 0}, + +// not use r10, r11 + {10, 1, "r10b", RISC_COLOR(0, 10, 0x1), NULL, 0, 0}, + {10, 2, "r10w", RISC_COLOR(0, 10, 0x3), NULL, 0, 0}, + {10, 4, "r10d", RISC_COLOR(0, 10, 0xf), NULL, 0, 0}, + {10, 8, "r10", RISC_COLOR(0, 10, 0xff), NULL, 0, 0}, + + {11, 1, "r11b", RISC_COLOR(0, 11, 0x1), NULL, 0, 0}, + {11, 2, "r11w", RISC_COLOR(0, 11, 0x3), NULL, 0, 0}, + {11, 4, "r11d", RISC_COLOR(0, 11, 0xf), NULL, 0, 0}, + {11, 8, "r11", RISC_COLOR(0, 11, 0xff), NULL, 0, 0}, + + {12, 1, "r12b", RISC_COLOR(0, 12, 0x1), NULL, 0, 0}, + {12, 2, "r12w", RISC_COLOR(0, 12, 0x3), NULL, 0, 0}, + {12, 4, "r12d", RISC_COLOR(0, 12, 0xf), NULL, 0, 0}, + {12, 8, "fp", RISC_COLOR(0, 12, 0xff), NULL, 0, 0}, + + {13, 1, "r13b", RISC_COLOR(0, 13, 0x1), NULL, 0, 0}, + {13, 2, "r13w", RISC_COLOR(0, 13, 0x3), NULL, 0, 0}, + {13, 4, "r13d", RISC_COLOR(0, 13, 0xf), NULL, 0, 0}, + {13, 8, "lr", RISC_COLOR(0, 13, 0xff), NULL, 0, 0}, + + {14, 8, "sp", RISC_COLOR(0, 14, 0xff), NULL, 0, 0}, +// {15, 8, "null", RISC_COLOR(0, 15, 0xff), NULL, 0, 0}, + + + {0, 1, "b0", RISC_COLOR(1, 0, 0x1), NULL, 0, 0}, {0, 2, "h0", RISC_COLOR(1, 0, 0x3), NULL, 0, 0}, {0, 4, "s0", RISC_COLOR(1, 0, 0xf), NULL, 0, 0}, {0, 8, "d0", RISC_COLOR(1, 0, 0xff), NULL, 0, 0}, + {1, 1, "b1", RISC_COLOR(1, 1, 0x1), NULL, 0, 0}, {1, 2, "h1", RISC_COLOR(1, 1, 0x3), NULL, 0, 0}, {1, 4, "s1", RISC_COLOR(1, 1, 0xf), NULL, 0, 0}, {1, 8, "d1", RISC_COLOR(1, 1, 0xff), NULL, 0, 0}, + {2, 1, "b2", RISC_COLOR(1, 2, 0x1), NULL, 0, 0}, {2, 2, "h2", RISC_COLOR(1, 2, 0x3), NULL, 0, 0}, {2, 4, "s2", RISC_COLOR(1, 2, 0xf), NULL, 0, 0}, {2, 8, "d2", RISC_COLOR(1, 2, 0xff), NULL, 0, 0}, + {3, 1, "b3", RISC_COLOR(1, 3, 0x1), NULL, 0, 0}, {3, 2, "h3", RISC_COLOR(1, 3, 0x3), NULL, 0, 0}, {3, 4, "s3", RISC_COLOR(1, 3, 0xf), NULL, 0, 0}, {3, 8, "d3", RISC_COLOR(1, 3, 0xff), NULL, 0, 0}, + {4, 1, "b4", RISC_COLOR(1, 4, 0x1), NULL, 0, 0}, {4, 2, "h4", RISC_COLOR(1, 4, 0x3), NULL, 0, 0}, {4, 4, "s4", RISC_COLOR(1, 4, 0xf), NULL, 0, 0}, {4, 8, "d4", RISC_COLOR(1, 4, 0xff), NULL, 0, 0}, + {5, 1, "b5", RISC_COLOR(1, 5, 0x1), NULL, 0, 0}, {5, 2, "h5", RISC_COLOR(1, 5, 0x3), NULL, 0, 0}, {5, 4, "s5", RISC_COLOR(1, 5, 0xf), NULL, 0, 0}, {5, 8, "d5", RISC_COLOR(1, 5, 0xff), NULL, 0, 0}, + {6, 1, "b6", RISC_COLOR(1, 6, 0x1), NULL, 0, 0}, {6, 2, "h6", RISC_COLOR(1, 6, 0x3), NULL, 0, 0}, {6, 4, "s6", RISC_COLOR(1, 6, 0xf), NULL, 0, 0}, {6, 8, "d6", RISC_COLOR(1, 6, 0xff), NULL, 0, 0}, + {7, 1, "b7", RISC_COLOR(1, 7, 0x1), NULL, 0, 0}, {7, 2, "h7", RISC_COLOR(1, 7, 0x3), NULL, 0, 0}, {7, 4, "s7", RISC_COLOR(1, 7, 0xf), NULL, 0, 0}, {7, 8, "d7", RISC_COLOR(1, 7, 0xff), NULL, 0, 0}, + {8, 1, "b8", RISC_COLOR(1, 8, 0x1), NULL, 0, 0}, {8, 2, "h8", RISC_COLOR(1, 8, 0x3), NULL, 0, 0}, {8, 4, "s8", RISC_COLOR(1, 8, 0xf), NULL, 0, 0}, {8, 8, "d8", RISC_COLOR(1, 8, 0xff), NULL, 0, 0}, + {9, 1, "b9", RISC_COLOR(1, 9, 0x1), NULL, 0, 0}, {9, 2, "h9", RISC_COLOR(1, 9, 0x3), NULL, 0, 0}, {9, 4, "s9", RISC_COLOR(1, 9, 0xf), NULL, 0, 0}, {9, 8, "d9", RISC_COLOR(1, 9, 0xff), NULL, 0, 0}, + {10, 1, "b10", RISC_COLOR(1, 10, 0x1), NULL, 0, 0}, {10, 2, "h10", RISC_COLOR(1, 10, 0x3), NULL, 0, 0}, {10, 4, "s10", RISC_COLOR(1, 10, 0xf), NULL, 0, 0}, {10, 8, "d10", RISC_COLOR(1, 10, 0xff), NULL, 0, 0}, + {11, 1, "b11", RISC_COLOR(1, 11, 0x1), NULL, 0, 0}, {11, 2, "h11", RISC_COLOR(1, 11, 0x3), NULL, 0, 0}, {11, 4, "s11", RISC_COLOR(1, 11, 0xf), NULL, 0, 0}, {11, 8, "d11", RISC_COLOR(1, 11, 0xff), NULL, 0, 0}, + {12, 1, "b12", RISC_COLOR(1, 12, 0x1), NULL, 0, 0}, {12, 2, "h12", RISC_COLOR(1, 12, 0x3), NULL, 0, 0}, {12, 4, "s12", RISC_COLOR(1, 12, 0xf), NULL, 0, 0}, {12, 8, "d12", RISC_COLOR(1, 12, 0xff), NULL, 0, 0}, + {13, 1, "b13", RISC_COLOR(1, 13, 0x1), NULL, 0, 0}, {13, 2, "h13", RISC_COLOR(1, 13, 0x3), NULL, 0, 0}, {13, 4, "s13", RISC_COLOR(1, 13, 0xf), NULL, 0, 0}, {13, 8, "d13", RISC_COLOR(1, 13, 0xff), NULL, 0, 0}, + {14, 1, "b14", RISC_COLOR(1, 14, 0x1), NULL, 0, 0}, {14, 2, "h14", RISC_COLOR(1, 14, 0x3), NULL, 0, 0}, {14, 4, "s14", RISC_COLOR(1, 14, 0xf), NULL, 0, 0}, {14, 8, "d14", RISC_COLOR(1, 14, 0xff), NULL, 0, 0}, + {15, 1, "b15", RISC_COLOR(1, 15, 0x1), NULL, 0, 0}, {15, 2, "h15", RISC_COLOR(1, 15, 0x3), NULL, 0, 0}, {15, 4, "s15", RISC_COLOR(1, 15, 0xf), NULL, 0, 0}, {15, 8, "d15", RISC_COLOR(1, 15, 0xff), NULL, 0, 0}, - - {16, 2, "h16", RISC_COLOR(1, 16, 0x3), NULL, 0, 0}, - {16, 4, "s16", RISC_COLOR(1, 16, 0xf), NULL, 0, 0}, - {16, 8, "d16", RISC_COLOR(1, 16, 0xff), NULL, 0, 0}, - - {17, 2, "h17", RISC_COLOR(1, 17, 0x3), NULL, 0, 0}, - {17, 4, "s17", RISC_COLOR(1, 17, 0xf), NULL, 0, 0}, - {17, 8, "d17", RISC_COLOR(1, 17, 0xff), NULL, 0, 0}, - - {18, 2, "h18", RISC_COLOR(1, 18, 0x3), NULL, 0, 0}, - {18, 4, "s18", RISC_COLOR(1, 18, 0xf), NULL, 0, 0}, - {18, 8, "d18", RISC_COLOR(1, 18, 0xff), NULL, 0, 0}, - - {19, 2, "h19", RISC_COLOR(1, 19, 0x3), NULL, 0, 0}, - {19, 4, "s19", RISC_COLOR(1, 19, 0xf), NULL, 0, 0}, - {19, 8, "d19", RISC_COLOR(1, 19, 0xff), NULL, 0, 0}, - - {20, 2, "h20", RISC_COLOR(1, 20, 0x3), NULL, 0, 0}, - {20, 4, "s20", RISC_COLOR(1, 20, 0xf), NULL, 0, 0}, - {20, 8, "d20", RISC_COLOR(1, 20, 0xff), NULL, 0, 0}, - - {21, 2, "h21", RISC_COLOR(1, 21, 0x3), NULL, 0, 0}, - {21, 4, "s21", RISC_COLOR(1, 21, 0xf), NULL, 0, 0}, - {21, 8, "d21", RISC_COLOR(1, 21, 0xff), NULL, 0, 0}, - - {22, 2, "h22", RISC_COLOR(1, 22, 0x3), NULL, 0, 0}, - {22, 4, "s22", RISC_COLOR(1, 22, 0xf), NULL, 0, 0}, - {22, 8, "d22", RISC_COLOR(1, 22, 0xff), NULL, 0, 0}, - - {23, 2, "h23", RISC_COLOR(1, 23, 0x3), NULL, 0, 0}, - {23, 4, "s23", RISC_COLOR(1, 23, 0xf), NULL, 0, 0}, - {23, 8, "d23", RISC_COLOR(1, 23, 0xff), NULL, 0, 0}, - - {24, 2, "h24", RISC_COLOR(1, 24, 0x3), NULL, 0, 0}, - {24, 4, "s24", RISC_COLOR(1, 24, 0xf), NULL, 0, 0}, - {24, 8, "d24", RISC_COLOR(1, 24, 0xff), NULL, 0, 0}, - - {25, 2, "h25", RISC_COLOR(1, 25, 0x3), NULL, 0, 0}, - {25, 4, "s25", RISC_COLOR(1, 25, 0xf), NULL, 0, 0}, - {25, 8, "d25", RISC_COLOR(1, 25, 0xff), NULL, 0, 0}, - - {26, 2, "h26", RISC_COLOR(1, 26, 0x3), NULL, 0, 0}, - {26, 4, "s26", RISC_COLOR(1, 26, 0xf), NULL, 0, 0}, - {26, 8, "d26", RISC_COLOR(1, 26, 0xff), NULL, 0, 0}, - - {27, 2, "h27", RISC_COLOR(1, 27, 0x3), NULL, 0, 0}, - {27, 4, "s27", RISC_COLOR(1, 27, 0xf), NULL, 0, 0}, - {27, 8, "d27", RISC_COLOR(1, 27, 0xff), NULL, 0, 0}, - - {28, 2, "h28", RISC_COLOR(1, 28, 0x3), NULL, 0, 0}, - {28, 4, "s28", RISC_COLOR(1, 28, 0xf), NULL, 0, 0}, - {28, 8, "d28", RISC_COLOR(1, 28, 0xff), NULL, 0, 0}, - - {29, 2, "h29", RISC_COLOR(1, 29, 0x3), NULL, 0, 0}, - {29, 4, "s29", RISC_COLOR(1, 29, 0xf), NULL, 0, 0}, - {29, 8, "d29", RISC_COLOR(1, 29, 0xff), NULL, 0, 0}, - - {30, 2, "h30", RISC_COLOR(1, 30, 0x3), NULL, 0, 0}, - {30, 4, "s30", RISC_COLOR(1, 30, 0xf), NULL, 0, 0}, - {30, 8, "d30", RISC_COLOR(1, 30, 0xff), NULL, 0, 0}, - - {31, 2, "h31", RISC_COLOR(1, 31, 0x3), NULL, 0, 0}, - {31, 4, "s31", RISC_COLOR(1, 31, 0xf), NULL, 0, 0}, - {31, 8, "d31", RISC_COLOR(1, 31, 0xff), NULL, 0, 0}, }; static uint32_t naja_abi_regs[] = @@ -242,8 +171,6 @@ static uint32_t naja_abi_regs[] = SCF_RISC_REG_X3, SCF_RISC_REG_X4, SCF_RISC_REG_X5, - SCF_RISC_REG_X6, - SCF_RISC_REG_X7, }; static uint32_t naja_abi_float_regs[] = @@ -274,31 +201,16 @@ static uint32_t naja_abi_caller_saves[] = SCF_RISC_REG_X3, SCF_RISC_REG_X4, SCF_RISC_REG_X5, - SCF_RISC_REG_X6, - SCF_RISC_REG_X7, - - SCF_RISC_REG_X9, - SCF_RISC_REG_X10, - SCF_RISC_REG_X11, - SCF_RISC_REG_X12, - SCF_RISC_REG_X13, - SCF_RISC_REG_X14, - SCF_RISC_REG_X15, }; static uint32_t naja_abi_callee_saves[] = { - SCF_RISC_REG_X19, - SCF_RISC_REG_X20, - SCF_RISC_REG_X21, - SCF_RISC_REG_X22, - SCF_RISC_REG_X23, - SCF_RISC_REG_X24, - SCF_RISC_REG_X25, - SCF_RISC_REG_X26, - SCF_RISC_REG_X27, - SCF_RISC_REG_X28, - SCF_RISC_REG_X29, + SCF_RISC_REG_X6, + SCF_RISC_REG_X7, + SCF_RISC_REG_X8, + SCF_RISC_REG_X9, + SCF_RISC_REG_FP, + SCF_RISC_REG_LR, }; static int naja_color_conflict(intptr_t c0, intptr_t c1) @@ -317,7 +229,7 @@ static int naja_variable_size(scf_variable_t* v) if (v->type >= SCF_STRUCT && 0 == v->nb_pointers) return 8; - return v->size < 4 ? 4 : v->size; + return v->size; } scf_register_t* naja_find_register(const char* name) @@ -386,8 +298,8 @@ scf_vector_t* naja_register_colors() if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; int ret = scf_vector_add(colors, (void*)r->color); @@ -421,8 +333,8 @@ int naja_reg_cached_vars(scf_register_t* r) if (SCF_RISC_REG_SP == r2->id || SCF_RISC_REG_FP == r2->id || SCF_RISC_REG_LR == r2->id - || SCF_RISC_REG_X16 == r2->id - || SCF_RISC_REG_X17 == r2->id) + || SCF_RISC_REG_X10 == r2->id + || SCF_RISC_REG_X11 == r2->id) continue; if (!naja_color_conflict(r->color, r2->color)) @@ -444,8 +356,8 @@ int naja_registers_init() if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; assert(!r->dag_nodes); @@ -470,8 +382,8 @@ void naja_registers_clear() if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; if (r->dag_nodes) { @@ -534,8 +446,8 @@ int naja_caller_save_regs(scf_3ac_code_t* c, scf_function_t* f, uint32_t* regs, if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; if (0 == r->dag_nodes->size) @@ -607,8 +519,8 @@ int naja_pop_regs(scf_3ac_code_t* c, scf_function_t* f, scf_register_t** regs, i if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; if (0 == r->dag_nodes->size) @@ -650,8 +562,8 @@ int naja_registers_reset() if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; if (!r->dag_nodes) @@ -692,8 +604,8 @@ int naja_overflow_reg(scf_register_t* r, scf_3ac_code_t* c, scf_function_t* f) if (SCF_RISC_REG_SP == r2->id || SCF_RISC_REG_FP == r2->id || SCF_RISC_REG_LR == r2->id - || SCF_RISC_REG_X16 == r2->id - || SCF_RISC_REG_X17 == r2->id) + || SCF_RISC_REG_X10 == r2->id + || SCF_RISC_REG_X11 == r2->id) continue; if (!naja_color_conflict(r->color, r2->color)) @@ -725,8 +637,8 @@ int naja_overflow_reg2(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, if (SCF_RISC_REG_SP == r2->id || SCF_RISC_REG_FP == r2->id || SCF_RISC_REG_LR == r2->id - || SCF_RISC_REG_X16 == r2->id - || SCF_RISC_REG_X17 == r2->id) + || SCF_RISC_REG_X10 == r2->id + || SCF_RISC_REG_X11 == r2->id) continue; if (!naja_color_conflict(r->color, r2->color)) @@ -767,8 +679,8 @@ int naja_overflow_reg3(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, if (SCF_RISC_REG_SP == r2->id || SCF_RISC_REG_FP == r2->id || SCF_RISC_REG_LR == r2->id - || SCF_RISC_REG_X16 == r2->id - || SCF_RISC_REG_X17 == r2->id) + || SCF_RISC_REG_X10 == r2->id + || SCF_RISC_REG_X11 == r2->id) continue; if (!naja_color_conflict(r->color, r2->color)) @@ -830,8 +742,8 @@ int naja_reg_used(scf_register_t* r, scf_dag_node_t* dn) if (SCF_RISC_REG_SP == r2->id || SCF_RISC_REG_FP == r2->id || SCF_RISC_REG_LR == r2->id - || SCF_RISC_REG_X16 == r2->id - || SCF_RISC_REG_X17 == r2->id) + || SCF_RISC_REG_X10 == r2->id + || SCF_RISC_REG_X11 == r2->id) continue; if (!naja_color_conflict(r->color, r2->color)) @@ -880,8 +792,8 @@ scf_register_t* naja_select_overflowed_reg(scf_dag_node_t* dn, scf_3ac_code_t* c if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; if (r->bytes < bytes || RISC_COLOR_TYPE(r->color) != is_float) @@ -920,8 +832,8 @@ scf_register_t* naja_select_overflowed_reg(scf_dag_node_t* dn, scf_3ac_code_t* c if (SCF_RISC_REG_SP == r->id || SCF_RISC_REG_FP == r->id || SCF_RISC_REG_LR == r->id - || SCF_RISC_REG_X16 == r->id - || SCF_RISC_REG_X17 == r->id) + || SCF_RISC_REG_X10 == r->id + || SCF_RISC_REG_X11 == r->id) continue; if (r->bytes < bytes || RISC_COLOR_TYPE(r->color) != is_float) @@ -1057,6 +969,7 @@ int naja_push_callee_regs(scf_3ac_code_t* c, scf_function_t* f) int i; for (i = 0; i < f->rops->ABI_CALLEE_SAVES_NB; i++) { + scf_logi("f->rops->abi_callee_saves[%d]: %d\n", i, f->rops->abi_callee_saves[i]); r = f->rops->find_register_type_id_bytes(0, f->rops->abi_callee_saves[i], 8); if (!r->used) { @@ -1085,6 +998,8 @@ int naja_pop_callee_regs(scf_3ac_code_t* c, scf_function_t* f) r = f->rops->find_register_type_id_bytes(0, f->rops->abi_callee_saves[i], 8); + scf_logi("r: %p, f->rops->abi_callee_saves[%d]: %d\n", r, i, f->rops->abi_callee_saves[i]); + if (!r->used) { r = f->rops->find_register_type_id_bytes(0, f->rops->abi_callee_saves[i], 4); diff --git a/js/native/risc/scf_risc_util.h b/js/native/risc/scf_risc_util.h index c64aa7a..d8209eb 100644 --- a/js/native/risc/scf_risc_util.h +++ b/js/native/risc/scf_risc_util.h @@ -3,8 +3,9 @@ #include"scf_def.h" -enum scf_risc_OpCode_types { - SCF_RISC_MOV = 0, +enum scf_risc_OpCode_types +{ + SCF_RISC_MOV = 0, SCF_RISC_MOVSX, SCF_RISC_MOVZX, @@ -117,7 +118,8 @@ enum scf_risc_OpCode_types { SCF_RISC_NB }; -enum scf_risc_REGs { +enum scf_risc_REGs +{ SCF_RISC_REG_W0 = 0, SCF_RISC_REG_X0 = 0, SCF_RISC_REG_S0 = 0, @@ -238,15 +240,15 @@ enum scf_risc_REGs { SCF_RISC_REG_X31 = 31, }; -enum scf_risc_EG_types { - SCF_RISC_G = 0, - SCF_RISC_I = 1, - SCF_RISC_G2E = 2, +enum scf_risc_EG_types +{ + SCF_RISC_G = 0, + SCF_RISC_I = 1, + SCF_RISC_G2E = 2, SCF_RISC_E2G = 3, SCF_RISC_I2E = 4, SCF_RISC_I2G = 5, - SCF_RISC_E = 6, + SCF_RISC_E = 6, }; #endif - diff --git a/js/native/scf_instruction.c b/js/native/scf_instruction.c new file mode 100644 index 0000000..92519a8 --- /dev/null +++ b/js/native/scf_instruction.c @@ -0,0 +1,76 @@ +#include"scf_instruction.h" + +void scf_rela_free(scf_rela_t* rela) +{ + if (rela) { + if (rela->name) + scf_string_free(rela->name); + + free(rela); + } +} + +void scf_instruction_free(scf_instruction_t* inst) +{ + if (inst) { + if (inst->label) + scf_lex_word_free(inst->label); + + if (inst->bin) + scf_string_free(inst->bin); + + free(inst); + } +} + +void scf_instruction_print(scf_instruction_t* inst) +{ + if (inst->label) + printf("%s: ", inst->label->text->data); + + if (inst->OpCode) + printf("%s ", inst->OpCode->name); + + if (1 == inst->src.flag) { + if (inst->src.index) + printf("%d(%s, %s, %d), ", inst->src.disp, inst->src.base->name, + inst->src.index->name, inst->src.scale); + + else if (inst->src.base) { + if (inst->src.disp < 0) + printf("-%#x(%s), ", -inst->src.disp, inst->src.base->name); + else + printf("%#x(%s), ", inst->src.disp, inst->src.base->name); + } else + printf("%d(rip), ", inst->dst.disp); + + } else if (inst->src.base) + printf("%s, ", inst->src.base->name); + + else if (inst->src.imm_size > 0) + printf("%d, ", (int)inst->src.imm); + + if (1 == inst->dst.flag) { + if (inst->dst.index) + printf("%d(%s, %s, %d)", inst->dst.disp, inst->dst.base->name, + inst->dst.index->name, inst->dst.scale); + + else if (inst->dst.base) { + if (inst->dst.disp < 0) + printf("-%#x(%s)", -inst->dst.disp, inst->dst.base->name); + else + printf("%#x(%s)", inst->dst.disp, inst->dst.base->name); + } else + printf("%d(rip)", inst->dst.disp); + + } else if (inst->dst.base) + printf("%s", inst->dst.base->name); + + else if (inst->dst.imm_size > 0) + printf("%d", (int)inst->dst.imm); + + int i; + for (i = 0; i < inst->len; i++) + printf(" %#x", inst->code[i]); + printf("\n"); +} diff --git a/js/native/scf_instruction.h b/js/native/scf_instruction.h new file mode 100644 index 0000000..37c0b0e --- /dev/null +++ b/js/native/scf_instruction.h @@ -0,0 +1,114 @@ +#ifndef SCF_INSTRUCTION_H +#define SCF_INSTRUCTION_H + +#include"scf_lex_word.h" +#include"scf_core_types.h" + +typedef struct scf_instruction_s scf_instruction_t; + +struct scf_register_s +{ + uint32_t id; + int bytes; + char* name; + + intptr_t color; + + scf_vector_t* dag_nodes; + + uint32_t updated; + uint32_t used; +}; +#define SCF_COLOR_CONFLICT(c0, c1) ((c0) >> 16 == (c1) >> 16 && (c0) & (c1) & 0xffff) + +struct scf_OpCode_s +{ + int type; + char* name; +}; + +typedef struct { + scf_register_t* base; + scf_register_t* index; + + int32_t scale; + int32_t disp; + int32_t size; +} scf_sib_t; + +typedef struct { + scf_register_t* base; + scf_register_t* index; + int scale; + int disp; + + scf_lex_word_t* label; + uint64_t imm; + int imm_size; + + uint8_t flag; +} scf_inst_data_t; + +struct scf_instruction_s +{ + scf_3ac_code_t* c; + + scf_OpCode_t* OpCode; + scf_instruction_t* next; // only for jcc, jmp, call + + scf_inst_data_t src; + scf_inst_data_t dst; + + scf_lex_word_t* label; // asm label + scf_string_t* bin; // asm binary data, maybe in .text or .data + int offset; // asm offset, maybe in .text or .data + + int len; + uint8_t code[32]; + + int flag; // asm jcc back or front + int nb_used; +}; + +typedef struct { + scf_3ac_code_t* code; // related 3ac code + scf_function_t* func; + scf_variable_t* var; + scf_string_t* name; + + scf_instruction_t* inst; + int inst_offset; // byte offset in instruction + int64_t text_offset; // byte offset in .text segment + uint64_t type; + int addend; +} scf_rela_t; + +static inline int scf_inst_data_same(scf_inst_data_t* id0, scf_inst_data_t* id1) +{ + // global var, are considered as different. + if ((id0->flag && !id0->base) || (id1->flag && !id1->base)) + return 0; + + if (id0->scale == id1->scale + && id0->disp == id1->disp + && id0->flag == id1->flag + && id0->imm == id1->imm + && id0->imm_size == id1->imm_size) { + + if (id0->base == id1->base + || (id0->base && id1->base && SCF_COLOR_CONFLICT(id0->base->color, id1->base->color))) { + + if (id0->index == id1->index + || (id0->index && id1->index && SCF_COLOR_CONFLICT(id0->index->color, id1->index->color))) + return 1; + } + } + return 0; +} + +void scf_rela_free(scf_rela_t* rela); + +void scf_instruction_free (scf_instruction_t* inst); +void scf_instruction_print(scf_instruction_t* inst); + +#endif diff --git a/js/native/scf_native.c b/js/native/scf_native.c index a80bcdc..66eba9a 100644 --- a/js/native/scf_native.c +++ b/js/native/scf_native.c @@ -3,52 +3,6 @@ extern scf_native_ops_t native_ops_x64; extern scf_native_ops_t native_ops_risc; -void scf_instruction_print(scf_instruction_t* inst) -{ - if (inst->OpCode) - printf("%s ", inst->OpCode->name); - - if (1 == inst->src.flag) { - if (inst->src.index) - printf("%d(%s, %s, %d), ", inst->src.disp, inst->src.base->name, - inst->src.index->name, inst->src.scale); - - else if (inst->src.base) { - if (inst->src.disp < 0) - printf("-%#x(%s), ", -inst->src.disp, inst->src.base->name); - else - printf("%#x(%s), ", inst->src.disp, inst->src.base->name); - } else - printf("%d(rip), ", inst->dst.disp); - - } else if (inst->src.base) - printf("%s, ", inst->src.base->name); - - else if (inst->src.imm_size > 0) - printf("%d, ", (int)inst->src.imm); - - if (1 == inst->dst.flag) { - if (inst->dst.index) - printf("%d(%s, %s, %d), ", inst->dst.disp, inst->dst.base->name, - inst->dst.index->name, inst->dst.scale); - - else if (inst->dst.base) { - if (inst->dst.disp < 0) - printf("-%#x(%s), ", -inst->dst.disp, inst->dst.base->name); - else - printf("%#x(%s), ", inst->dst.disp, inst->dst.base->name); - } else - printf("%d(rip), ", inst->dst.disp); - - } else if (inst->dst.base) - printf("%s, ", inst->dst.base->name); - - else if (inst->dst.imm_size > 0) - printf("%d, ", (int)inst->dst.imm); - - printf("\n"); -} - int scf_native_open(scf_native_t** pctx, const char* name) { scf_native_t* ctx = calloc(1, sizeof(scf_native_t)); diff --git a/js/native/scf_native.h b/js/native/scf_native.h index f095717..29c1626 100644 --- a/js/native/scf_native.h +++ b/js/native/scf_native.h @@ -3,78 +3,9 @@ #include"scf_3ac.h" #include"scf_parse.h" +#include"scf_instruction.h" -typedef struct scf_native_ops_s scf_native_ops_t; - -struct scf_register_s -{ - uint32_t id; - int bytes; - char* name; - - intptr_t color; - - scf_vector_t* dag_nodes; - - uint32_t updated; - uint32_t used; -}; -#define SCF_COLOR_CONFLICT(c0, c1) ((c0) >> 16 == (c1) >> 16 && (c0) & (c1) & 0xffff) - -struct scf_OpCode_s -{ - int type; - char* name; -}; - -typedef struct { - scf_register_t* base; - scf_register_t* index; - - int32_t scale; - int32_t disp; - int32_t size; -} scf_sib_t; - -typedef struct { - scf_register_t* base; - scf_register_t* index; - int scale; - int disp; - - uint64_t imm; - int imm_size; - - uint8_t flag; -} scf_inst_data_t; - -typedef struct { - scf_3ac_code_t* c; - - scf_OpCode_t* OpCode; - - scf_inst_data_t src; - scf_inst_data_t dst; - - uint8_t code[32]; - int len; - - int nb_used; - -} scf_instruction_t; - -typedef struct { - scf_3ac_code_t* code; // related 3ac code - scf_function_t* func; - scf_variable_t* var; - scf_string_t* name; - - scf_instruction_t* inst; - int inst_offset; // byte offset in instruction - int64_t text_offset; // byte offset in .text segment - uint64_t type; - int addend; -} scf_rela_t; +typedef struct scf_native_ops_s scf_native_ops_t; typedef struct { scf_native_ops_t* ops; @@ -239,31 +170,6 @@ struct scf_inst_ops_s void (*set_jmp_offset)(scf_instruction_t* inst, int32_t bytes); }; -static inline int scf_inst_data_same(scf_inst_data_t* id0, scf_inst_data_t* id1) -{ - // global var, are considered as different. - if ((id0->flag && !id0->base) || (id1->flag && !id1->base)) - return 0; - - if (id0->scale == id1->scale - && id0->disp == id1->disp - && id0->flag == id1->flag - && id0->imm == id1->imm - && id0->imm_size == id1->imm_size) { - - if (id0->base == id1->base - || (id0->base && id1->base && SCF_COLOR_CONFLICT(id0->base->color, id1->base->color))) { - - if (id0->index == id1->index - || (id0->index && id1->index && SCF_COLOR_CONFLICT(id0->index->color, id1->index->color))) - return 1; - } - } - return 0; -} - -void scf_instruction_print(scf_instruction_t* inst); - int scf_native_open(scf_native_t** pctx, const char* name); int scf_native_close(scf_native_t* ctx); diff --git a/js/native/x64/scf_x64.c b/js/native/x64/scf_x64.c index ced05ba..7ec69c3 100644 --- a/js/native/x64/scf_x64.c +++ b/js/native/x64/scf_x64.c @@ -154,7 +154,7 @@ static int _x64_save_rabi(scf_function_t* f) #define X64_SAVE_RABI(offset, rabi) \ do { \ inst = x64_make_inst_G2P(mov, rbp, offset, rabi); \ - X64_INST_ADD_CHECK(f->init_code->instructions, inst); \ + X64_INST_ADD_CHECK(f->init_code->instructions, inst, NULL); \ f->init_code_bytes += inst->len; \ } while (0) @@ -220,12 +220,12 @@ static int _x64_function_finish(scf_function_t* f) if (f->bp_used_flag || f->vla_flag || f->call_flag) { inst = x64_make_inst_G2E(mov, rsp, rbp); - X64_INST_ADD_CHECK(end->instructions, inst); + X64_INST_ADD_CHECK(end->instructions, inst, NULL); end->inst_bytes += inst->len; bb ->code_bytes += inst->len; inst = x64_make_inst_G(pop, rbp); - X64_INST_ADD_CHECK(end->instructions, inst); + X64_INST_ADD_CHECK(end->instructions, inst, NULL); end->inst_bytes += inst->len; bb ->code_bytes += inst->len; } @@ -245,11 +245,11 @@ static int _x64_function_finish(scf_function_t* f) if (f->bp_used_flag || f->vla_flag || f->call_flag) { inst = x64_make_inst_G(push, rbp); - X64_INST_ADD_CHECK(f->init_code->instructions, inst); + X64_INST_ADD_CHECK(f->init_code->instructions, inst, NULL); f->init_code_bytes += inst->len; inst = x64_make_inst_G2E(mov, rbp, rsp); - X64_INST_ADD_CHECK(f->init_code->instructions, inst); + X64_INST_ADD_CHECK(f->init_code->instructions, inst, NULL); f->init_code_bytes += inst->len; if (f->callee_saved_size & 0xf) { @@ -264,7 +264,7 @@ static int _x64_function_finish(scf_function_t* f) local, f->local_vars_size, f->callee_saved_size); inst = x64_make_inst_I2E(sub, rsp, (uint8_t*)&local, 4); - X64_INST_ADD_CHECK(f->init_code->instructions, inst); + X64_INST_ADD_CHECK(f->init_code->instructions, inst, NULL); f->init_code_bytes += inst->len; int err = _x64_save_rabi(f); @@ -273,7 +273,7 @@ static int _x64_function_finish(scf_function_t* f) } inst = x64_make_inst(ret, 8); - X64_INST_ADD_CHECK(end->instructions, inst); + X64_INST_ADD_CHECK(end->instructions, inst, NULL); end->inst_bytes += inst->len; bb ->code_bytes += inst->len; diff --git a/js/native/x64/scf_x64.h b/js/native/x64/scf_x64.h index 6ce1a69..e30405c 100644 --- a/js/native/x64/scf_x64.h +++ b/js/native/x64/scf_x64.h @@ -8,38 +8,6 @@ #include"scf_graph.h" #include"scf_elf.h" -#define X64_INST_ADD_CHECK(vec, inst) \ - do { \ - if (!(inst)) { \ - scf_loge("\n"); \ - return -ENOMEM; \ - } \ - int ret = scf_vector_add((vec), (inst)); \ - if (ret < 0) { \ - scf_loge("\n"); \ - free(inst); \ - return ret; \ - } \ - } while (0) - -#define X64_RELA_ADD_CHECK(vec, rela, c, v, f) \ - do { \ - if (rela) { \ - (rela)->code = (c); \ - (rela)->var = (v); \ - (rela)->func = (f); \ - (rela)->inst = (c)->instructions->data[(c)->instructions->size - 1]; \ - (rela)->addend = -4; \ - (rela)->type = R_X86_64_PC32; \ - int ret = scf_vector_add((vec), (rela)); \ - if (ret < 0) { \ - scf_loge("\n"); \ - free(rela); \ - return ret; \ - } \ - } \ - } while (0) - #define X64_PEEPHOLE_DEL 1 #define X64_PEEPHOLE_OK 0 @@ -90,43 +58,15 @@ int x64_load_bb_colors (scf_basic_block_t* bb, scf_bb_group_t* bbg, scf_function int x64_load_bb_colors2(scf_basic_block_t* bb, scf_bb_group_t* bbg, scf_function_t* f); int x64_init_bb_colors (scf_basic_block_t* bb); - -scf_instruction_t* x64_make_inst (scf_x64_OpCode_t* OpCode, int size); -scf_instruction_t* x64_make_inst_G(scf_x64_OpCode_t* OpCode, scf_register_t* r); -scf_instruction_t* x64_make_inst_E(scf_x64_OpCode_t* OpCode, scf_register_t* r); -scf_instruction_t* x64_make_inst_I(scf_x64_OpCode_t* OpCode, uint8_t* imm, int size); -void x64_make_inst_I2(scf_instruction_t* inst, scf_x64_OpCode_t* OpCode, uint8_t* imm, int size); - -scf_instruction_t* x64_make_inst_I2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, uint8_t* imm, int size); -scf_instruction_t* x64_make_inst_I2E(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, uint8_t* imm, int size); - scf_instruction_t* x64_make_inst_M (scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_variable_t* v, scf_register_t* r_base); scf_instruction_t* x64_make_inst_I2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_variable_t* v_dst, scf_register_t* r_base, uint8_t* imm, int32_t size); scf_instruction_t* x64_make_inst_G2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_variable_t* v_dst, scf_register_t* r_base, scf_register_t* r_src); scf_instruction_t* x64_make_inst_M2G(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_base, scf_variable_t* v_src); -scf_instruction_t* x64_make_inst_G2E(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_src); -scf_instruction_t* x64_make_inst_E2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_src); - -scf_instruction_t* x64_make_inst_P2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_base, int32_t offset); -scf_instruction_t* x64_make_inst_G2P(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, int32_t offset, scf_register_t* r_src); -scf_instruction_t* x64_make_inst_I2P(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, int32_t offset, uint8_t* imm, int size); - -scf_instruction_t* x64_make_inst_SIB2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp); -scf_instruction_t* x64_make_inst_G2SIB(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp, scf_register_t* r_src); -scf_instruction_t* x64_make_inst_I2SIB(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp, uint8_t* imm, int32_t size); - -scf_instruction_t* x64_make_inst_SIB(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp, int size); -scf_instruction_t* x64_make_inst_P (scf_x64_OpCode_t* OpCode, scf_register_t* r_base, int32_t offset, int size); - int x64_float_OpCode_type(int OpCode_type, int var_type); - -int x64_shift(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type); - -int x64_shift_assign(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type); - - +int x64_shift (scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type); +int x64_shift_assign (scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type); int x64_binary_assign(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type); int x64_assign_dereference(scf_native_t* ctx, scf_3ac_code_t* c); diff --git a/js/native/x64/scf_x64_inst.c b/js/native/x64/scf_x64_inst.c index c98b96f..51c6b9b 100644 --- a/js/native/x64/scf_x64_inst.c +++ b/js/native/x64/scf_x64_inst.c @@ -107,7 +107,6 @@ static int _x64_inst_call_stack_size(scf_3ac_code_t* c) static int _x64_inst_call_argv(scf_3ac_code_t* c, scf_function_t* f) { scf_register_t* rsp = x64_find_register("rsp"); - scf_x64_OpCode_t* lea; scf_x64_OpCode_t* mov; scf_x64_OpCode_t* movx; @@ -205,11 +204,11 @@ static int _x64_inst_call_argv(scf_3ac_code_t* c, scf_function_t* f) if (!rd) { if (movx) { inst = x64_make_inst_E2G(movx, rs, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } inst = x64_make_inst_G2P(mov, rsp, v->sp_offset, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); continue; } @@ -222,12 +221,12 @@ static int _x64_inst_call_argv(scf_3ac_code_t* c, scf_function_t* f) if (!X64_COLOR_CONFLICT(rd->color, rs->color)) { if (movx) { inst = x64_make_inst_E2G(movx, rs, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } rd = x64_find_register_color_bytes(rd->color, rs->bytes); inst = x64_make_inst_G2E(mov, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else if (movx) { inst = x64_make_inst_E2G(movx, rs, rs); @@ -241,7 +240,7 @@ static int _x64_inst_call_argv(scf_3ac_code_t* c, scf_function_t* f) } for (i = 0; i < nb_movx; i++) { - X64_INST_ADD_CHECK(c->instructions, inst_movx[i]); + X64_INST_ADD_CHECK(c->instructions, inst_movx[i], NULL); } return nb_floats; @@ -307,7 +306,6 @@ static int _x64_call_update_dsts(scf_3ac_code_t* c, scf_function_t* f, scf_regis scf_3ac_operand_t* dst; scf_dag_node_t* dn; scf_variable_t* v; - scf_register_t* rd; scf_register_t* rs; scf_x64_OpCode_t* mov; @@ -401,7 +399,7 @@ static int _x64_call_update_dsts(scf_3ac_code_t* c, scf_function_t* f, scf_regis int valid = _x64_dst_reg_valid(rd, updated_regs, nb_updated, idx_int, nb_int); if (valid) { inst = x64_make_inst_G2E(mov, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); assert(nb_updated < max_updated); @@ -428,8 +426,6 @@ static int _x64_call_update_dsts(scf_3ac_code_t* c, scf_function_t* f, scf_regis dn->color = rs->color; dn->loaded = 1; } else { - scf_rela_t* rela = NULL; - if (0 == v->bp_offset && !v->global_flag && !v->local_flag) { int size = f->local_vars_size + dst_size; @@ -445,8 +441,10 @@ static int _x64_call_update_dsts(scf_3ac_code_t* c, scf_function_t* f, scf_regis scf_logd("v->bp_offset: %d, local_flag: %d, tmp_flag: %d, rs->name: %s\n", v->bp_offset, v->local_flag, v->tmp_flag, rs->name); } + scf_rela_t* rela = NULL; + inst = x64_make_inst_G2M(&rela, mov, dn->var, NULL, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dn->var, NULL); } } @@ -462,7 +460,6 @@ static int _x64_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) scf_x64_context_t* x64 = ctx->priv; scf_function_t* f = x64->f; - scf_3ac_operand_t* src0 = c->srcs->data[0]; scf_variable_t* var_pf = src0->dag_node->var; scf_function_t* pf = var_pf->func_ptr; @@ -514,9 +511,9 @@ static int _x64_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) int32_t stack_size = _x64_inst_call_stack_size(c); if (stack_size > 0) { - sub = x64_find_OpCode(SCF_X64_SUB, 4,4, SCF_X64_I2E); + sub = x64_find_OpCode(SCF_X64_SUB, 4,4, SCF_X64_I2E); inst_rsp = x64_make_inst_I2E(sub, rsp, (uint8_t*)&stack_size, 4); - X64_INST_ADD_CHECK(c->instructions, inst_rsp); + X64_INST_ADD_CHECK(c->instructions, inst_rsp, NULL); } ret = _x64_inst_call_argv(c, f); @@ -528,7 +525,7 @@ static int _x64_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) mov = x64_find_OpCode(SCF_X64_MOV, 4,4, SCF_X64_I2G); inst = x64_make_inst_I2G(mov, eax, (uint8_t*)&imm, sizeof(imm)); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); scf_register_t* saved_regs[X64_ABI_CALLER_SAVES_NB]; @@ -550,7 +547,7 @@ static int _x64_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) int32_t offset = 0; call = x64_find_OpCode(SCF_X64_CALL, 4,4, SCF_X64_I); inst = x64_make_inst_I(call, (uint8_t*)&offset, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst->OpCode = (scf_OpCode_t*)call; @@ -576,14 +573,14 @@ static int _x64_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) } inst = x64_make_inst_E(call, r_pf); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst->OpCode = (scf_OpCode_t*)call; } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M(&rela, call, var_pf, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->text_relas, rela, c, NULL, pf); inst->OpCode = (scf_OpCode_t*)call; @@ -593,7 +590,7 @@ static int _x64_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (stack_size > 0) { add = x64_find_OpCode(SCF_X64_ADD, 4, 4, SCF_X64_I2E); inst = x64_make_inst_I2E(add, rsp, (uint8_t*)&stack_size, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } int nb_updated = 0; @@ -654,8 +651,8 @@ static int _x64_inst_unary(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type scf_instruction_t* inst = NULL; scf_register_t* rd = NULL; scf_variable_t* var = dst->dag_node->var; - scf_x64_OpCode_t* OpCode = x64_find_OpCode(OpCode_type, var->size, var->size, SCF_X64_E); + if (!OpCode) { scf_loge("\n"); return -1; @@ -664,13 +661,12 @@ static int _x64_inst_unary(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type if (dst->dag_node->color > 0) { X64_SELECT_REG_CHECK(&rd, dst->dag_node, c, f, 0); inst = x64_make_inst_E(OpCode, rd); - X64_INST_ADD_CHECK(c->instructions, inst); - + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M(&rela, OpCode, var, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, var, NULL); } @@ -698,8 +694,8 @@ static int _x64_inst_unary_assign(scf_native_t* ctx, scf_3ac_code_t* c, int OpCo scf_instruction_t* inst = NULL; scf_register_t* rs = NULL; scf_variable_t* var = src->dag_node->var; - scf_x64_OpCode_t* OpCode = x64_find_OpCode(OpCode_type, var->size, var->size, SCF_X64_E); + if (!OpCode) { scf_loge("\n"); return -1; @@ -708,7 +704,7 @@ static int _x64_inst_unary_assign(scf_native_t* ctx, scf_3ac_code_t* c, int OpCo if (src->dag_node->color > 0) { X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_E(OpCode, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else if (0 == src->dag_node->color) { scf_loge("\n"); @@ -717,7 +713,7 @@ static int _x64_inst_unary_assign(scf_native_t* ctx, scf_3ac_code_t* c, int OpCo scf_rela_t* rela = NULL; inst = x64_make_inst_M(&rela, OpCode, var, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, var, NULL); } @@ -776,17 +772,14 @@ static int _x64_inst_neg_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_SELECT_REG_CHECK(&rd, dst->dag_node, c, f, 0); inst = x64_make_inst_E2G(pxor, rd, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); if (src->dag_node->color > 0) { X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_E2G(sub, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); - + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { - scf_rela_t* rela = NULL; - v = src->dag_node->var; if (0 == src->dag_node->color) { @@ -795,8 +788,10 @@ static int _x64_inst_neg_handler(scf_native_t* ctx, scf_3ac_code_t* c) v->tmp_flag = 0; } + scf_rela_t* rela = NULL; + inst = x64_make_inst_M2G(&rela, sub, rd, NULL, v); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); } @@ -851,7 +846,7 @@ static int _x64_inst_inc_float(scf_function_t* f, scf_3ac_code_t* c, int INC) X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_M2G(&rela, OpCode, rs, NULL, v1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v1, NULL); return 0; } @@ -903,12 +898,12 @@ static int _x64_inst_inc(scf_native_t* ctx, scf_3ac_code_t* c, int INC, int ADD) if (src->dag_node->color > 0) { X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_I2E(OpCode, rs, (uint8_t*)&v->data_size, imm_size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_I2M(&rela, OpCode, v, NULL, (uint8_t*)&v->data_size, imm_size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); } @@ -916,12 +911,12 @@ static int _x64_inst_inc(scf_native_t* ctx, scf_3ac_code_t* c, int INC, int ADD) if (src->dag_node->color > 0) { X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_E(OpCode, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M(&rela, OpCode, v, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); } } @@ -1017,7 +1012,7 @@ static int _x64_inst_assign_array_index(scf_native_t* ctx, scf_3ac_code_t* c, in else inst = x64_make_inst_I2P(OpCode, sib.base, sib.disp, (uint8_t*)&vs->data, size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -1043,10 +1038,10 @@ static int _x64_inst_assign_array_index(scf_native_t* ctx, scf_3ac_code_t* c, in if (sib.index) { inst = x64_make_inst_G2SIB(OpCode, sib.base, sib.index, sib.scale, sib.disp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_G2P(OpCode, sib.base, sib.disp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; } @@ -1126,10 +1121,10 @@ static int _x64_inst_array_index(scf_native_t* ctx, scf_3ac_code_t* c, int lea_f if (sib.index) { inst = x64_make_inst_SIB2G(OpCode, rd, sib.base, sib.index, sib.scale, sib.disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_P2G(OpCode, rd, sib.base, sib.disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; } @@ -1194,7 +1189,7 @@ static int _x64_inst_address_of_handler(scf_native_t* ctx, scf_3ac_code_t* c) lea = x64_find_OpCode(SCF_X64_LEA, 8,8, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, lea, rd, NULL, src->dag_node->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->dag_node->var, NULL); return 0; } @@ -1533,21 +1528,21 @@ static int _x64_inst_dump_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_G2E(mov, rsi, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); scf_rela_t* rela = NULL; inst = x64_make_inst_M2G(&rela, lea, rdi, NULL, msg->dag_node->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, msg->dag_node->var, NULL); inst = x64_make_inst_G2E(xor, rax, rax); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); // call printf() to show msg uint32_t imm = 0; inst = x64_make_inst_I(call, (uint8_t*)&imm, sizeof(imm)); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); rela = calloc(1, sizeof(scf_rela_t)); if (!rela) @@ -1556,10 +1551,10 @@ static int _x64_inst_dump_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_RELA_ADD_CHECK(f->text_relas, rela, c, NULL, logf->dag_node->var->func_ptr); inst = x64_make_inst_G2E(xor, rax, rax); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rax, 0, rax); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -1602,14 +1597,14 @@ static int _x64_inst_vla_alloc_handler(scf_native_t* ctx, scf_3ac_code_t* c) uint32_t imm = 0xf; inst = x64_make_inst_I2E(add, rs, (uint8_t*)&imm, sizeof(imm)); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); imm = ~0xf; inst = x64_make_inst_I2E(and, rs, (uint8_t*)&imm, sizeof(imm)); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_E2G(sub, rsp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); int ret = x64_save_var(src->dag_node, c, f); if (ret < 0) @@ -1618,7 +1613,7 @@ static int _x64_inst_vla_alloc_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_SELECT_REG_CHECK(&rd, dst->dag_node, c, f, 0); inst = x64_make_inst_G2E(mov, rd, rsp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -1652,10 +1647,10 @@ static int _x64_inst_vla_free_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); inst = x64_make_inst_E2G(add, rsp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2E(xor, rs, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return x64_save_var(src->dag_node, c, f); } @@ -1756,14 +1751,14 @@ static int _x64_inst_return_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (rd->bytes > size) { inst = x64_make_inst_E2G(mov, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else { X64_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); if (!X64_COLOR_CONFLICT(rd->color, rs->color) || rd->bytes > size) { inst = x64_make_inst_E2G(mov, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } } else if (0 == src->dag_node->color) { @@ -1774,10 +1769,10 @@ static int _x64_inst_return_handler(scf_native_t* ctx, scf_3ac_code_t* c) mov = x64_find_OpCode(SCF_X64_MOV, rd->bytes, rd->bytes, SCF_X64_I2G); inst = x64_make_inst_I2G(mov, rd, (uint8_t*)&v->data, rd->bytes); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_M2G(&rela, mov, rd, NULL, v); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); } } @@ -1829,7 +1824,7 @@ static int _x64_inst_memset_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == dn->color) { \ mov = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_I2G); \ inst = x64_make_inst_I2G(mov, r, (uint8_t*)&dn->var->data, size); \ - X64_INST_ADD_CHECK(c->instructions, inst); \ + X64_INST_ADD_CHECK(c->instructions, inst, NULL); \ \ } else { \ if (dn->color < 0) \ @@ -1839,7 +1834,7 @@ static int _x64_inst_memset_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (!X64_COLOR_CONFLICT(rd->color, r->color)) { \ mov = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_G2E); \ inst = x64_make_inst_G2E(mov, r, rd); \ - X64_INST_ADD_CHECK(c->instructions, inst); \ + X64_INST_ADD_CHECK(c->instructions, inst, NULL); \ } \ } \ } while (0) @@ -1850,7 +1845,7 @@ static int _x64_inst_memset_handler(scf_native_t* ctx, scf_3ac_code_t* c) stos = x64_find_OpCode(SCF_X64_STOS, 1, 8, SCF_X64_G); inst = x64_make_inst(stos, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -2094,13 +2089,13 @@ static int _x64_inst_push_rets_handler(scf_native_t* ctx, scf_3ac_code_t* c) r = x64_find_register_type_id_bytes(0, x64_abi_ret_regs[i], 8); inst = x64_make_inst_G(push, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } if (n & 0x1) { r = x64_find_register_type_id_bytes(0, x64_abi_ret_regs[n - 1], 8); inst = x64_make_inst_G(push, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; } @@ -2132,14 +2127,14 @@ static int _x64_inst_pop_rets_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (n & 0x1) { r = x64_find_register_type_id_bytes(0, x64_abi_ret_regs[n - 1], 8); inst = x64_make_inst_G(pop, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } for (i = n - 1; i >= 0; i--) { r = x64_find_register_type_id_bytes(0, x64_abi_ret_regs[i], 8); inst = x64_make_inst_G(pop, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; @@ -2194,33 +2189,32 @@ static int _x64_inst_va_start_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_SELECT_REG_CHECK(&rptr, ptr->dag_node, c, f, 0); inst = x64_make_inst_P2G(lea, rptr, rbp, offset_int); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 0, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_P2G(lea, rptr, rbp, offset_float); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 8, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_P2G(lea, rptr, rbp, offset_others); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 16, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); mov = x64_find_OpCode(SCF_X64_MOV, 4, 8, SCF_X64_I2E); - inst = x64_make_inst_I2P(mov, rap, 24, (uint8_t*)&f->args_int, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_I2P(mov, rap, 32, (uint8_t*)&f->args_float, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -2259,22 +2253,22 @@ static int _x64_inst_va_end_handler(scf_native_t* ctx, scf_3ac_code_t* c) X64_SELECT_REG_CHECK(&rptr, ptr->dag_node, c, f, 0); inst = x64_make_inst_G2E(xor, rptr, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 0, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 8, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 16, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 24, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(mov, rap, 32, rptr); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); ptr->dag_node->var->tmp_flag = 0; ptr->dag_node->color = 0; @@ -2342,22 +2336,22 @@ static int _x64_inst_va_arg_handler(scf_native_t* ctx, scf_3ac_code_t* c) } inst = x64_make_inst_I2P(cmp, rap, idx_offset, (uint8_t*)&nints, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst_jge = x64_make_inst_I(jge, (uint8_t*)&offset, sizeof(offset)); - X64_INST_ADD_CHECK(c->instructions, inst_jge); + X64_INST_ADD_CHECK(c->instructions, inst_jge, NULL); inst = x64_make_inst_P2G(mov, rptr, rap, ptr_offset); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); offset += inst->len; inst = x64_make_inst_I2P(sub, rap, ptr_offset, (uint8_t*)&incptr, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); offset += inst->len; inst_jmp = x64_make_inst_I(jmp, (uint8_t*)&offset, sizeof(offset)); - X64_INST_ADD_CHECK(c->instructions, inst_jmp); + X64_INST_ADD_CHECK(c->instructions, inst_jmp, NULL); offset += inst_jmp->len; uint8_t* p = (uint8_t*)&offset; @@ -2367,18 +2361,18 @@ static int _x64_inst_va_arg_handler(scf_native_t* ctx, scf_3ac_code_t* c) offset = 0; inst = x64_make_inst_P2G(mov, rptr, rap, 16); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); offset += inst->len; inst = x64_make_inst_I2P(add, rap, 16, (uint8_t*)&incptr, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); offset += inst->len; for (i = 0; i < 4; i++) inst_jmp->code[jmp->nb_OpCodes + i] = p[i]; inst = x64_make_inst_P(inc, rap, idx_offset, 8); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); if (is_float) { if (4 == size) @@ -2391,8 +2385,7 @@ static int _x64_inst_va_arg_handler(scf_native_t* ctx, scf_3ac_code_t* c) mov2 = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_E2G); inst = x64_make_inst_P2G(mov2, rd, rptr, 0); - X64_INST_ADD_CHECK(c->instructions, inst); - + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } diff --git a/js/native/x64/scf_x64_inst_binary.c b/js/native/x64/scf_x64_inst_binary.c index c2c5f5b..a292950 100644 --- a/js/native/x64/scf_x64_inst_binary.c +++ b/js/native/x64/scf_x64_inst_binary.c @@ -36,16 +36,17 @@ static int _binary_assign_sib_float(scf_register_t* rb, scf_register_t* ri, int3 return ret; } - mov = x64_find_OpCode(mov_type, v->size, v->size, SCF_X64_E2G); + mov = x64_find_OpCode(mov_type, v->size, v->size, SCF_X64_E2G); if (ri) inst = x64_make_inst_SIB2G(mov, rs, rb, ri, scale, disp); else inst = x64_make_inst_P2G(mov, rs, rb, disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); OpCode = x64_find_OpCode(OpCode_type, v->size, v->size, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, OpCode, rs, NULL, src->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); + X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); end: mov = x64_find_OpCode(mov_type, v->size, v->size, SCF_X64_G2E); @@ -53,19 +54,18 @@ end: inst = x64_make_inst_G2SIB(mov, rb, ri, scale, disp, rs); else inst = x64_make_inst_G2P(mov, rb, disp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } static int _binary_assign_sib_int(x64_sib_t* sib, scf_dag_node_t* src, scf_3ac_code_t* c, scf_function_t* f, int OpCode_type) { - scf_variable_t* v = src->var; - scf_register_t* rs = NULL; - - scf_register_t* rb = sib->base; - scf_register_t* ri = sib->index; - int32_t scale = sib->scale; - int32_t disp = sib->disp; + scf_variable_t* v = src->var; + scf_register_t* rs = NULL; + scf_register_t* rb = sib->base; + scf_register_t* ri = sib->index; + int32_t scale = sib->scale; + int32_t disp = sib->disp; scf_x64_OpCode_t* OpCode; scf_instruction_t* inst; @@ -82,7 +82,7 @@ static int _binary_assign_sib_int(x64_sib_t* sib, scf_dag_node_t* src, scf_3ac_c inst = x64_make_inst_I2SIB(OpCode, rb, ri, scale, disp, (uint8_t*)&v->data, dsize); else inst = x64_make_inst_I2P(OpCode, rb, disp, (uint8_t*)&v->data, dsize); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -108,7 +108,7 @@ static int _binary_assign_sib_int(x64_sib_t* sib, scf_dag_node_t* src, scf_3ac_c inst = x64_make_inst_G2SIB(OpCode, rb, ri, scale, disp, rs); else inst = x64_make_inst_G2P(OpCode, rb, disp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -122,7 +122,6 @@ static int _binary_SIB2G(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type, scf_x64_context_t* x64 = ctx->priv; scf_function_t* f = x64->f; - scf_3ac_operand_t* dst = c->dsts->data[0]; scf_3ac_operand_t* base = c->srcs->data[0]; scf_3ac_operand_t* index = c->srcs->data[c->srcs->size - 1]; @@ -142,7 +141,6 @@ static int _binary_SIB2G(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type, scf_variable_t* vd = dst ->dag_node->var; scf_variable_t* vb = base ->dag_node->var; scf_variable_t* vi = index->dag_node->var; - scf_register_t* rd = NULL; x64_sib_t sib = {0}; @@ -165,18 +163,18 @@ static int _binary_SIB2G(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type, int is_float = scf_variable_float(vd); if (is_float) { if (SCF_VAR_FLOAT == vd->type) - mov = x64_find_OpCode(SCF_X64_MOVSS, rd->bytes, rd->bytes, SCF_X64_E2G); + mov = x64_find_OpCode(SCF_X64_MOVSS, rd->bytes, rd->bytes, SCF_X64_E2G); else if (SCF_VAR_DOUBLE == vd->type) - mov = x64_find_OpCode(SCF_X64_MOVSD, rd->bytes, rd->bytes, SCF_X64_E2G); + mov = x64_find_OpCode(SCF_X64_MOVSD, rd->bytes, rd->bytes, SCF_X64_E2G); } else - mov = x64_find_OpCode(SCF_X64_MOV, rd->bytes, rd->bytes, SCF_X64_E2G); + mov = x64_find_OpCode(SCF_X64_MOV, rd->bytes, rd->bytes, SCF_X64_E2G); if (sib.index) { inst = x64_make_inst_SIB2G(mov, rd, sib.base, sib.index, sib.scale, sib.disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_P2G(mov, rd, sib.base, sib.disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; } @@ -233,18 +231,18 @@ int x64_assign_dereference(scf_native_t* ctx, scf_3ac_code_t* c) if (!src || !src->dag_node) return -EINVAL; - scf_variable_t* b = base->dag_node->var; - assert(b->nb_pointers > 0 || b->nb_dimentions > 0 || b->type >= SCF_STRUCT); - if (!c->instructions) { c->instructions = scf_vector_alloc(); if (!c->instructions) return -ENOMEM; } + scf_variable_t* b = base->dag_node->var; scf_variable_t* v = src->dag_node->var; x64_sib_t sib = {0}; + assert(b->nb_pointers > 0 || b->nb_dimentions > 0 || b->type >= SCF_STRUCT); + int ret = x64_dereference_reg(&sib, base->dag_node, NULL, c, f); if (ret < 0) return ret; @@ -334,19 +332,19 @@ int x64_assign_pointer(scf_native_t* ctx, scf_3ac_code_t* c) r = x64_find_register_color_bytes(rs->color, 8); inst = x64_make_inst_G(push, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_I2G(mov, rs, (uint8_t*)&mask, dsize); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); if (sib.index) inst = x64_make_inst_G2SIB(and, sib.base, sib.index, sib.scale, sib.disp, rs); else inst = x64_make_inst_G2P(and, sib.base, sib.disp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G(pop, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); int imm = (rs->bytes << 3) - vm->bit_size; assert(imm > 0); @@ -355,14 +353,14 @@ int x64_assign_pointer(scf_native_t* ctx, scf_3ac_code_t* c) shr = x64_find_OpCode(SCF_X64_SHR, 1, rs->bytes, SCF_X64_I2E); inst = x64_make_inst_I2E(shl, rs, (uint8_t*)&imm, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); imm -= vm->bit_offset; assert(imm >= 0); if (imm > 0) { inst = x64_make_inst_I2E(shr, rs, (uint8_t*)&imm, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } mov = x64_find_OpCode(SCF_X64_OR, dsize, dsize, SCF_X64_G2E); @@ -377,7 +375,7 @@ int x64_assign_pointer(scf_native_t* ctx, scf_3ac_code_t* c) inst = x64_make_inst_G2SIB(mov, sib.base, sib.index, sib.scale, sib.disp, rs); else inst = x64_make_inst_G2P(mov, sib.base, sib.disp, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -396,7 +394,6 @@ int x64_inst_pointer(scf_native_t* ctx, scf_3ac_code_t* c, int lea_flag) scf_x64_context_t* x64 = ctx->priv; scf_function_t* f = x64->f; - scf_3ac_operand_t* dst = c->dsts->data[0]; scf_3ac_operand_t* base = c->srcs->data[0]; scf_3ac_operand_t* member = c->srcs->data[1]; @@ -416,7 +413,6 @@ int x64_inst_pointer(scf_native_t* ctx, scf_3ac_code_t* c, int lea_flag) scf_variable_t* vd = dst->dag_node->var; scf_variable_t* vb = base ->dag_node->var; scf_variable_t* vm = member->dag_node->var; - scf_register_t* rd = NULL; x64_sib_t sib = {0}; @@ -456,10 +452,10 @@ int x64_inst_pointer(scf_native_t* ctx, scf_3ac_code_t* c, int lea_flag) if (sib.index) { inst = x64_make_inst_SIB2G(mov, rd, sib.base, sib.index, sib.scale, sib.disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_P2G(mov, rd, sib.base, sib.disp); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } if (vm->bit_size > 0) { @@ -471,12 +467,12 @@ int x64_inst_pointer(scf_native_t* ctx, scf_3ac_code_t* c, int lea_flag) if (imm > 0) { inst = x64_make_inst_I2E(shl, rd, (uint8_t*)&imm, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } imm += vm->bit_offset; inst = x64_make_inst_I2E(shr, rd, (uint8_t*)&imm, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; diff --git a/js/native/x64/scf_x64_inst_cmp.c b/js/native/x64/scf_x64_inst_cmp.c index 84c40f9..55d8711 100644 --- a/js/native/x64/scf_x64_inst_cmp.c +++ b/js/native/x64/scf_x64_inst_cmp.c @@ -13,8 +13,8 @@ static int _inst_cmp(scf_dag_node_t* src0, scf_dag_node_t* src1, scf_3ac_code_t* scf_x64_OpCode_t* cmp; scf_instruction_t* inst; - scf_register_t* rs1; - scf_register_t* rs0 = NULL; + scf_register_t* rs1; + scf_register_t* rs0 = NULL; scf_rela_t* rela = NULL; X64_SELECT_REG_CHECK(&rs0, src0, c, f, 1); @@ -35,7 +35,7 @@ static int _inst_cmp(scf_dag_node_t* src0, scf_dag_node_t* src1, scf_3ac_code_t* X64_SELECT_REG_CHECK(&rs1, src1, c, f, 1); inst = x64_make_inst_E2G(cmp, rs0, rs1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } } else { @@ -44,7 +44,7 @@ static int _inst_cmp(scf_dag_node_t* src0, scf_dag_node_t* src1, scf_3ac_code_t* if (cmp) { inst = x64_make_inst_I2E(cmp, rs0, (uint8_t*)&src1->var->data, src1_size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -54,7 +54,7 @@ static int _inst_cmp(scf_dag_node_t* src0, scf_dag_node_t* src1, scf_3ac_code_t* cmp = x64_find_OpCode(SCF_X64_CMP, rs0->bytes, src1_size, SCF_X64_G2E); inst = x64_make_inst_G2E(cmp, rs0, rs1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); src1->loaded = 0; src1->color = 0; @@ -68,10 +68,10 @@ static int _inst_cmp(scf_dag_node_t* src0, scf_dag_node_t* src1, scf_3ac_code_t* if (src1->color > 0) { X64_SELECT_REG_CHECK(&rs1, src1, c, f, 1); inst = x64_make_inst_E2G(cmp, rs0, rs1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_M2G(&rela, cmp, rs0, NULL, src1->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src1->var, NULL); } @@ -98,13 +98,13 @@ static int _inst_set(int setcc_type, scf_dag_node_t* dst, scf_3ac_code_t* c, scf mov = x64_find_OpCode(SCF_X64_MOV, rd->bytes, rd->bytes, SCF_X64_I2G); inst = x64_make_inst_I2G(mov, rd, (uint8_t*)&imm, rd->bytes); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); rd = x64_find_register_color_bytes(rd->color, 1); } inst = x64_make_inst_E(setcc, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -141,7 +141,7 @@ int x64_inst_teq(scf_native_t* ctx, scf_3ac_code_t* c) test = x64_find_OpCode(SCF_X64_TEST, v->size, v->size, SCF_X64_G2E); inst = x64_make_inst_G2E(test, rs, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } diff --git a/js/native/x64/scf_x64_inst_common.c b/js/native/x64/scf_x64_inst_common.c index c5df00f..cdd4fbd 100644 --- a/js/native/x64/scf_x64_inst_common.c +++ b/js/native/x64/scf_x64_inst_common.c @@ -56,8 +56,8 @@ static int _x64_inst_op2_imm(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_ { scf_x64_OpCode_t* OpCode; scf_instruction_t* inst; - scf_register_t* rd = NULL; - scf_register_t* rs = NULL; + scf_register_t* rd = NULL; + scf_register_t* rs = NULL; scf_rela_t* rela = NULL; assert( scf_variable_const(src->var)); @@ -81,7 +81,6 @@ static int _x64_inst_op2_imm(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_ } if (dst->color > 0) { - if (SCF_X64_MOV == OpCode_type) X64_SELECT_REG_CHECK(&rd, dst, c, f, 0); else @@ -90,14 +89,14 @@ static int _x64_inst_op2_imm(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_ OpCode = x64_find_OpCode(OpCode_type, src_size, dst_size, SCF_X64_I2G); if (OpCode) { inst = x64_make_inst_I2G(OpCode, rd, (uint8_t*)&src->var->data, src_size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } OpCode = x64_find_OpCode(OpCode_type, src_size, dst_size, SCF_X64_I2E); if (OpCode) { inst = x64_make_inst_I2E(OpCode, rd, (uint8_t*)&src->var->data, src_size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } @@ -112,7 +111,7 @@ static int _x64_inst_op2_imm(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_ X64_SELECT_REG_CHECK(&rs, src, c, f, 1); inst = x64_make_inst_G2E(OpCode, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); src->color = 0; src->loaded = 0; @@ -124,7 +123,7 @@ static int _x64_inst_op2_imm(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_ OpCode = x64_find_OpCode(OpCode_type, src_size, dst_size, SCF_X64_I2E); if (OpCode) { inst = x64_make_inst_I2M(&rela, OpCode, dst->var, NULL, (uint8_t*)&src->var->data, src_size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); return 0; } @@ -140,7 +139,7 @@ static int _x64_inst_op2_imm(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_ X64_SELECT_REG_CHECK(&rs, src, c, f, 1); inst = x64_make_inst_G2M(&rela, OpCode, dst->var, NULL, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); src->color = 0; @@ -157,7 +156,7 @@ static int _x64_inst_op2_imm_str(int OpCode_type, scf_dag_node_t* dst, scf_dag_n return -EINVAL; } - scf_register_t* rd = NULL; + scf_register_t* rd = NULL; scf_instruction_t* inst = NULL; scf_x64_OpCode_t* lea = x64_find_OpCode(SCF_X64_LEA, 8, 8, SCF_X64_E2G); scf_rela_t* rela = NULL; @@ -175,7 +174,7 @@ static int _x64_inst_op2_imm_str(int OpCode_type, scf_dag_node_t* dst, scf_dag_n src->var->tmp_flag = 0; inst = x64_make_inst_M2G(&rela, lea, rd, NULL, src->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); return 0; } @@ -223,8 +222,7 @@ int x64_inst_op2(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_t* src, scf_ OpCode = x64_find_OpCode(OpCode_type, src_size, dst_size, SCF_X64_G2E); if (OpCode) { inst = x64_make_inst_G2E(OpCode, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); - + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { OpCode = x64_find_OpCode(OpCode_type, src_size, dst_size, SCF_X64_E2G); if (!OpCode) { @@ -233,7 +231,7 @@ int x64_inst_op2(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_t* src, scf_ } inst = x64_make_inst_E2G(OpCode, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else { OpCode = x64_find_OpCode(OpCode_type, src_size, dst_size, SCF_X64_E2G); @@ -243,7 +241,7 @@ int x64_inst_op2(int OpCode_type, scf_dag_node_t* dst, scf_dag_node_t* src, scf_ } inst = x64_make_inst_M2G(&rela, OpCode, rd, NULL, src->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); } @@ -259,8 +257,8 @@ int x64_inst_movx(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c, s scf_x64_OpCode_t* xor; scf_instruction_t* inst; - scf_register_t* rs; - scf_register_t* rd = NULL; + scf_register_t* rs; + scf_register_t* rd = NULL; X64_SELECT_REG_CHECK(&rd, dst, c, f, 0); @@ -274,7 +272,7 @@ int x64_inst_movx(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c, s assert(4 == src->var->size); xor = x64_find_OpCode(SCF_X64_XOR, 8, 8, SCF_X64_G2E); inst = x64_make_inst_G2E(xor, rd, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); movx = x64_find_OpCode(SCF_X64_MOV, 4, 4, SCF_X64_E2G); } @@ -282,7 +280,7 @@ int x64_inst_movx(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c, s if (src->color > 0) { X64_SELECT_REG_CHECK(&rs, src, c, f, 0); inst = x64_make_inst_E2G(movx, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else if (0 == src->color) { // get the rd's low bits register @@ -290,16 +288,16 @@ int x64_inst_movx(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c, s mov = x64_find_OpCode(SCF_X64_MOV, src->var->size, src->var->size, SCF_X64_I2G); inst = x64_make_inst_I2G(mov, rs, (uint8_t*)&src->var->data, src->var->size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_E2G(movx, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M2G(&rela, movx, rd, NULL, src->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); } @@ -309,7 +307,6 @@ int x64_inst_movx(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c, s int x64_inst_float_cast(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c, scf_function_t* f) { scf_x64_OpCode_t* OpCode; - scf_instruction_t* inst; scf_register_t* rs = NULL; scf_register_t* rd = NULL; @@ -351,19 +348,19 @@ int x64_inst_float_cast(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t X64_SELECT_REG_CHECK(&rs, src, c, f, 1); - inst = x64_make_inst_E2G(OpCode, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + inst = x64_make_inst_E2G(OpCode, rd, rs); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else if (src->color > 0) { X64_SELECT_REG_CHECK(&rs, src, c, f, 1); inst = x64_make_inst_E2G(OpCode, rd, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M2G(&rela, OpCode, rd, NULL, src->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); } @@ -392,7 +389,6 @@ int x64_inst_jmp(scf_native_t* ctx, scf_3ac_code_t* c, int OpCode_type) scf_instruction_t* inst = x64_make_inst_I(jcc, (uint8_t*)&offset, sizeof(offset)); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } - diff --git a/js/native/x64/scf_x64_inst_div.c b/js/native/x64/scf_x64_inst_div.c index 5400f1d..9de5722 100644 --- a/js/native/x64/scf_x64_inst_div.c +++ b/js/native/x64/scf_x64_inst_div.c @@ -44,7 +44,7 @@ int x64_inst_int_div(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c mov = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_G2E); inst = x64_make_inst_G2E(mov, rl, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } if (rd->id != rh->id) { @@ -65,7 +65,7 @@ int x64_inst_int_div(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c mov = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, mov, rl, NULL, dst->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); } @@ -73,24 +73,23 @@ int x64_inst_int_div(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c div = x64_find_OpCode(SCF_X64_IDIV, size, size, SCF_X64_E); cdq = x64_find_OpCode_by_type(SCF_X64_CDQ); inst = x64_make_inst(cdq, size << 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { div = x64_find_OpCode(SCF_X64_DIV, size, size, SCF_X64_E); xor = x64_find_OpCode(SCF_X64_XOR, size, size, SCF_X64_G2E); inst = x64_make_inst_G2E(xor, rh, rh); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } if (src->color > 0) { X64_SELECT_REG_CHECK(&rs, src, c, f, 1); inst = x64_make_inst_E(div, rs); - X64_INST_ADD_CHECK(c->instructions, inst); - + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M(&rela, div, src->var, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); } @@ -104,14 +103,14 @@ int x64_inst_int_div(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c if (rd->id != result->id) { mov = x64_find_OpCode(SCF_X64_MOV, rd->bytes, rd->bytes, SCF_X64_G2E); inst = x64_make_inst_G2E(mov, rd, result); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else { scf_rela_t* rela = NULL; mov = x64_find_OpCode(SCF_X64_MOV, dst->var->size, dst->var->size, SCF_X64_G2E); inst = x64_make_inst_G2M(&rela, mov, dst->var, NULL, result); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); } diff --git a/js/native/x64/scf_x64_inst_mul.c b/js/native/x64/scf_x64_inst_mul.c index 72527f5..6e20ce8 100644 --- a/js/native/x64/scf_x64_inst_mul.c +++ b/js/native/x64/scf_x64_inst_mul.c @@ -12,15 +12,15 @@ static int _int_mul_src(scf_x64_OpCode_t* mul, scf_register_t* rh, scf_dag_node_ mov = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_I2G); inst = x64_make_inst_I2G(mov, rh, (uint8_t*)&src->var->data, size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_E(mul, rh); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { scf_rela_t* rela = NULL; inst = x64_make_inst_M(&rela, mul, src->var, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, src->var, NULL); } @@ -87,23 +87,23 @@ int x64_inst_int_mul(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c if (rd->id == rl->id) { inst = x64_make_inst_E(mul, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else if (rs->id == rl->id) { inst = x64_make_inst_E(mul, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { inst = x64_make_inst_G2E(mov, rl, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_E(mul, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else { if (rd->id != rl->id) { inst = x64_make_inst_G2E(mov, rl, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } int ret = _int_mul_src(mul, rh, src, c, f); @@ -116,18 +116,18 @@ int x64_inst_int_mul(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c if (rs->id != rl->id) { inst = x64_make_inst_G2E(mov, rl, rs); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } inst = x64_make_inst_M(&rela, mul, dst->var, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); } else { mov2 = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, mov2, rl, NULL, dst->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); int ret = _int_mul_src(mul, rh, src, c, f); @@ -139,11 +139,11 @@ int x64_inst_int_mul(scf_dag_node_t* dst, scf_dag_node_t* src, scf_3ac_code_t* c if (rd) { if (rd->id != rl->id) { inst = x64_make_inst_G2E(mov, rd, rl); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else { inst = x64_make_inst_G2M(&rela, mov, dst->var, NULL, rl); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); } diff --git a/js/native/x64/scf_x64_inst_shift.c b/js/native/x64/scf_x64_inst_shift.c index 9c801ae..855ad92 100644 --- a/js/native/x64/scf_x64_inst_shift.c +++ b/js/native/x64/scf_x64_inst_shift.c @@ -25,20 +25,20 @@ static int _shift_count(scf_dag_node_t* count, scf_3ac_code_t* c, scf_function_t mov = x64_find_OpCode(SCF_X64_MOV, 1, 1, SCF_X64_G2E); inst = x64_make_inst_G2E(mov, cl, rc); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else if (count->color < 0) { - scf_rela_t* rela = NULL; - ret = x64_overflow_reg(cl, c, f); if (ret < 0) { scf_loge("\n"); return ret; } + scf_rela_t* rela = NULL; + mov = x64_find_OpCode(SCF_X64_MOV, 1, 1, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, mov, cl, NULL, count->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, count->var, NULL); } @@ -59,8 +59,7 @@ static int _x64_shift(scf_native_t* ctx, scf_3ac_code_t* c, scf_dag_node_t* dst, return -ENOMEM; } - scf_register_t* rd = NULL; - + scf_register_t* rd = NULL; scf_instruction_t* inst; scf_x64_OpCode_t* mov; scf_x64_OpCode_t* shift; @@ -77,11 +76,11 @@ static int _x64_shift(scf_native_t* ctx, scf_3ac_code_t* c, scf_dag_node_t* dst, if (0 != count->color) { shift = x64_find_OpCode(OpCode_type, 1, dst->var->size, SCF_X64_G2E); inst = x64_make_inst_E(shift, rd); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { shift = x64_find_OpCode(OpCode_type, 1, dst->var->size, SCF_X64_I2E); - inst = x64_make_inst_I2E(shift, rd, (uint8_t*)&count->var->data, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + inst = x64_make_inst_I2E(shift, rd, (uint8_t*)&count->var->data, 1); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else { scf_rela_t* rela = NULL; @@ -89,12 +88,12 @@ static int _x64_shift(scf_native_t* ctx, scf_3ac_code_t* c, scf_dag_node_t* dst, if (0 != count->color) { shift = x64_find_OpCode(OpCode_type, 1, dst->var->size, SCF_X64_G2E); inst = x64_make_inst_M(&rela, shift, dst->var, NULL); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); } else { shift = x64_find_OpCode(OpCode_type, 1, dst->var->size, SCF_X64_I2E); inst = x64_make_inst_I2M(&rela, shift, dst->var, NULL, (uint8_t*)&count->var->data, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dst->var, NULL); } } diff --git a/js/native/x64/scf_x64_inst_unary.c b/js/native/x64/scf_x64_inst_unary.c index 708505f..8ee5358 100644 --- a/js/native/x64/scf_x64_inst_unary.c +++ b/js/native/x64/scf_x64_inst_unary.c @@ -15,7 +15,7 @@ static int _unary_assign_sib(x64_sib_t* sib, int size, scf_3ac_code_t* c, scf_fu inst = x64_make_inst_SIB(OpCode, sib->base, sib->index, sib->scale, sib->disp, size); else inst = x64_make_inst_P(OpCode, sib->base, sib->disp, size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); return 0; } diff --git a/js/native/x64/scf_x64_inst_util.c b/js/native/x64/scf_x64_inst_util.c index 6074843..1c3f461 100644 --- a/js/native/x64/scf_x64_inst_util.c +++ b/js/native/x64/scf_x64_inst_util.c @@ -39,6 +39,7 @@ static scf_instruction_t* _x64_make_OpCode(scf_x64_OpCode_t* OpCode, int bytes, case SCF_X64_POP: case SCF_X64_RET: case SCF_X64_CALL: + case SCF_X64_SYSCALL: case SCF_X64_CVTSS2SD: break; @@ -155,14 +156,23 @@ static int _x64_make_disp(scf_rela_t** prela, scf_instruction_t* inst, uint32_t scf_ModRM_setRM(&ModRM, base); - if (SCF_X64_RM_EBP != base - && SCF_X64_RM_ESP != base - && SCF_X64_RM_R12 != base - && SCF_X64_RM_R13 != base - && 0 == disp) { - scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE); - inst->code[inst->len++] = ModRM; - return 0; + if (0 == disp) { + if (SCF_X64_RM_ESP == base || SCF_X64_RM_R12 == base) { + scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE); + inst->code[inst->len++] = ModRM; + + uint8_t SIB = 0; + scf_SIB_setBase(&SIB, base); + scf_SIB_setIndex(&SIB, base); + scf_SIB_setScale(&SIB, SCF_X64_SIB_SCALE1); + inst->code[inst->len++] = SIB; + return 0; + + } else if (SCF_X64_RM_EBP != base && SCF_X64_RM_R13 != base) { + scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE); + inst->code[inst->len++] = ModRM; + return 0; + } } if (disp <= 127 && disp >= -128) { @@ -315,6 +325,46 @@ scf_instruction_t* x64_make_inst_I2E(scf_x64_OpCode_t* OpCode, scf_register_t* r return inst; } +scf_instruction_t* x64_make_inst_L(scf_rela_t** prela, scf_x64_OpCode_t* OpCode) +{ + uint8_t reg = 0; + + if (OpCode->ModRM_OpCode_used) + reg = OpCode->ModRM_OpCode; + + scf_instruction_t* inst = _x64_make_OpCode(OpCode, OpCode->OpBytes, NULL, NULL, NULL); + if (!inst) + return NULL; + + if (_x64_make_disp(prela, inst, reg, -1, 0) < 0) { + free(inst); + return NULL; + } + + if (SCF_X64_INC == OpCode->type || SCF_X64_INC == OpCode->type) { + + inst->src.base = NULL; + inst->src.disp = 0; + inst->src.flag = 1; + + inst->dst.base = NULL; + inst->dst.disp = 0; + inst->dst.flag = 1; + + } else if (SCF_X64_MUL == OpCode->type + || SCF_X64_DIV == OpCode->type + || SCF_X64_IMUL == OpCode->type + || SCF_X64_IDIV == OpCode->type + || SCF_X64_CALL == OpCode->type) { + + inst->src.base = NULL; + inst->src.disp = 0; + inst->src.flag = 1; + } + + return inst; +} + scf_instruction_t* x64_make_inst_M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_variable_t* v, scf_register_t* r_base) { scf_register_t* rbp = x64_find_register("rbp"); @@ -382,6 +432,42 @@ scf_instruction_t* x64_make_inst_M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, return inst; } +scf_instruction_t* x64_make_inst_I2L(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, uint8_t* imm, int32_t size) +{ + uint8_t reg = 0; + + if (OpCode->ModRM_OpCode_used) + reg = OpCode->ModRM_OpCode; + + scf_instruction_t* inst = _x64_make_OpCode(OpCode, OpCode->OpBytes, NULL, NULL, NULL); + if (!inst) + return NULL; + + if (_x64_make_disp(prela, inst, reg, -1, 0) < 0) { + free(inst); + return NULL; + } + + size = size > OpCode->OpBytes ? OpCode->OpBytes : size; + + uint8_t* p = (uint8_t*)&inst->src.imm; + int i; + + inst->src.imm = 0; + + for (i = 0; i < size; i++) { + inst->code[inst->len++] = imm[i]; + p[i] = imm[i]; + } + + inst->dst.base = NULL; + inst->dst.disp = 0; + inst->dst.flag = 1; + + inst->src.imm_size = size; + return inst; +} + scf_instruction_t* x64_make_inst_I2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_variable_t* v_dst, scf_register_t* r_base, uint8_t* imm, int32_t size) { scf_register_t* rbp = x64_find_register("rbp"); @@ -445,6 +531,30 @@ scf_instruction_t* x64_make_inst_I2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCod return inst; } +scf_instruction_t* x64_make_inst_G2L(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_register_t* r_src) +{ + if (OpCode->ModRM_OpCode_used) { + scf_loge("ModRM opcode invalid\n"); + return NULL; + } + + scf_instruction_t* inst = _x64_make_OpCode(OpCode, OpCode->OpBytes, r_src, NULL, NULL); + if (!inst) + return NULL; + + if (_x64_make_disp(prela, inst, r_src->id, -1, 0) < 0) { + free(inst); + return NULL; + } + + inst->src.base = r_src; + inst->dst.base = NULL; + inst->dst.disp = 0; + inst->dst.flag = 1; + + return inst; +} + scf_instruction_t* x64_make_inst_G2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_variable_t* v_dst, scf_register_t* r_base, scf_register_t* r_src) { if (OpCode->ModRM_OpCode_used) { @@ -499,6 +609,30 @@ scf_instruction_t* x64_make_inst_G2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCod return inst; } +scf_instruction_t* x64_make_inst_L2G(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_register_t* r_dst) +{ + if (OpCode->ModRM_OpCode_used) { + scf_loge("ModRM opcode invalid\n"); + return NULL; + } + + scf_instruction_t* inst = _x64_make_OpCode(OpCode, r_dst->bytes, r_dst, NULL, NULL); + if (!inst) + return NULL; + + if (_x64_make_disp(prela, inst, r_dst->id, -1, 0) < 0) { + free(inst); + return NULL; + } + + inst->dst.base = r_dst; + inst->src.base = NULL; + inst->src.disp = 0; + inst->src.flag = 1; + + return inst; +} + scf_instruction_t* x64_make_inst_M2G(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_base, scf_variable_t* v_src) { if (OpCode->ModRM_OpCode_used) { diff --git a/js/native/x64/scf_x64_opcode.c b/js/native/x64/scf_x64_opcode.c index 1b51082..f67b5d2 100644 --- a/js/native/x64/scf_x64_opcode.c +++ b/js/native/x64/scf_x64_opcode.c @@ -22,6 +22,16 @@ scf_x64_OpCode_t x64_OpCodes[] = { {SCF_X64_XOR, "xor", 2, {0x33, 0x0, 0x0},1, 2,2, SCF_X64_E2G, 0,0, 0,{0,0}}, {SCF_X64_XOR, "xor", 2, {0x33, 0x0, 0x0},1, 4,4, SCF_X64_E2G, 0,0, 0,{0,0}}, {SCF_X64_XOR, "xor", 2, {0x33, 0x0, 0x0},1, 8,8, SCF_X64_E2G, 0,0, 0,{0,0}}, + + {SCF_X64_XOR, "xorb", 2, {0x30, 0x0, 0x0},1, 1,1, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_XOR, "xorw", 2, {0x31, 0x0, 0x0},1, 2,2, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_XOR, "xorl", 2, {0x31, 0x0, 0x0},1, 4,4, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_XOR, "xorq", 2, {0x31, 0x0, 0x0},1, 8,8, SCF_X64_G2E, 0,0, 0,{0,0}}, + + {SCF_X64_XOR, "xorb", 2, {0x32, 0x0, 0x0},1, 1,1, SCF_X64_E2G, 0,0, 0,{0,0}}, + {SCF_X64_XOR, "xorw", 2, {0x33, 0x0, 0x0},1, 2,2, SCF_X64_E2G, 0,0, 0,{0,0}}, + {SCF_X64_XOR, "xorl", 2, {0x33, 0x0, 0x0},1, 4,4, SCF_X64_E2G, 0,0, 0,{0,0}}, + {SCF_X64_XOR, "xorq", 2, {0x33, 0x0, 0x0},1, 8,8, SCF_X64_E2G, 0,0, 0,{0,0}}, #if 0 {SCF_X64_XOR, "xor", 2, {0x34, 0x0, 0x0},1, 1,1, SCF_X64_I2G, 0,0, 1,{0,0}}, {SCF_X64_XOR, "xor", 2, {0x35, 0x0, 0x0},1, 2,2, SCF_X64_I2G, 0,0, 1,{0,0}}, @@ -161,6 +171,12 @@ scf_x64_OpCode_t x64_OpCodes[] = { {SCF_X64_NOT, "not", 2, {0xf7, 0x0, 0x0},1, 8,8, SCF_X64_E, 2,1, 0,{0,0}}, {SCF_X64_LEA, "lea", 1, {0x8d, 0x0, 0x0},1, 8,8, SCF_X64_E2G, 0,0, 0,{0,0}}, + {SCF_X64_LEA, "leaq", 1, {0x8d, 0x0, 0x0},1, 8,8, SCF_X64_E2G, 0,0, 0,{0,0}}, + + {SCF_X64_MOV, "movb", 2, {0x88, 0x0, 0x0},1, 1,1, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_MOV, "movw", 2, {0x89, 0x0, 0x0},1, 2,2, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_MOV, "movl", 2, {0x89, 0x0, 0x0},1, 4,4, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_MOV, "movq", 2, {0x89, 0x0, 0x0},1, 8,8, SCF_X64_G2E, 0,0, 0,{0,0}}, {SCF_X64_MOV, "mov", 2, {0x88, 0x0, 0x0},1, 1,1, SCF_X64_G2E, 0,0, 0,{0,0}}, {SCF_X64_MOV, "mov", 2, {0x89, 0x0, 0x0},1, 2,2, SCF_X64_G2E, 0,0, 0,{0,0}}, @@ -222,6 +238,11 @@ scf_x64_OpCode_t x64_OpCodes[] = { {SCF_X64_CMP, "cmp", 2, {0x83, 0x0, 0x0},1, 1,4, SCF_X64_I2E, 7,1, 0,{0,0}}, {SCF_X64_CMP, "cmp", 2, {0x83, 0x0, 0x0},1, 1,8, SCF_X64_I2E, 7,1, 0,{0,0}}, + {SCF_X64_TEST, "testb", 2, {0x84, 0x0, 0x0},1, 1,1, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_TEST, "testw", 2, {0x85, 0x0, 0x0},1, 2,2, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_TEST, "testl", 2, {0x85, 0x0, 0x0},1, 4,4, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_TEST, "testq", 2, {0x85, 0x0, 0x0},1, 8,8, SCF_X64_G2E, 0,0, 0,{0,0}}, + {SCF_X64_TEST, "test", 2, {0x84, 0x0, 0x0},1, 1,1, SCF_X64_G2E, 0,0, 0,{0,0}}, {SCF_X64_TEST, "test", 2, {0x85, 0x0, 0x0},1, 2,2, SCF_X64_G2E, 0,0, 0,{0,0}}, {SCF_X64_TEST, "test", 2, {0x85, 0x0, 0x0},1, 4,4, SCF_X64_G2E, 0,0, 0,{0,0}}, @@ -283,11 +304,15 @@ scf_x64_OpCode_t x64_OpCodes[] = { {SCF_X64_CVTTSD2SI, "cvttsd2si", 8, {0xf2, 0x0f, 0x2c},3, 8,4, SCF_X64_E2G, 0,0, 0,{0,0}}, {SCF_X64_CVTTSD2SI, "cvttsd2si", 8, {0xf2, 0x0f, 0x2c},3, 8,8, SCF_X64_E2G, 0,0, 0,{0,0}}, - {SCF_X64_JZ, "jz", 2, {0x74, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, + {SCF_X64_JZ, "jz", 2, {0x74, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, {SCF_X64_JZ, "jz", 6, {0x0f, 0x84, 0x0},2, 4,4, SCF_X64_I, 0,0, 0,{0,0}}, + {SCF_X64_JZ, "je", 2, {0x74, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, + {SCF_X64_JZ, "je", 6, {0x0f, 0x84, 0x0},2, 4,4, SCF_X64_I, 0,0, 0,{0,0}}, - {SCF_X64_JNZ, "jnz", 2, {0x75, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, + {SCF_X64_JNZ, "jnz", 2, {0x75, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, {SCF_X64_JNZ, "jnz", 6, {0x0f, 0x85, 0x0},2, 4,4, SCF_X64_I, 0,0, 0,{0,0}}, + {SCF_X64_JNZ, "jne", 2, {0x75, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, + {SCF_X64_JNZ, "jne", 6, {0x0f, 0x85, 0x0},2, 4,4, SCF_X64_I, 0,0, 0,{0,0}}, {SCF_X64_JG, "jg", 2, {0x7f, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, {SCF_X64_JG, "jg", 6, {0x0f, 0x8f,0x0},1, 4,4, SCF_X64_I, 0,0, 0,{0,0}}, @@ -315,10 +340,11 @@ scf_x64_OpCode_t x64_OpCodes[] = { {SCF_X64_JMP, "jmp", 2, {0xeb, 0x0, 0x0},1, 1,1, SCF_X64_I, 0,0, 0,{0,0}}, {SCF_X64_JMP, "jmp", 5, {0xe9, 0x0, 0x0},1, 4,4, SCF_X64_I, 0,0, 0,{0,0}}, - {SCF_X64_JMP, "jmp", 2, {0xff, 0x0, 0x0},1, 8,8, SCF_X64_E, 4,1, 0,{0,0}}, + + {SCF_X64_SYSCALL, "syscall", 2, {0xf, 0x5, 0x0},2, 8,8, SCF_X64_G, 0,0, 0,{0,0}}, }; -scf_x64_OpCode_t* x64_find_OpCode_by_type(const int type) +scf_x64_OpCode_t* x64_find_OpCode_by_type(const int type) { int i; for (i = 0; i < sizeof(x64_OpCodes) / sizeof(x64_OpCodes[0]); i++) { @@ -330,23 +356,19 @@ scf_x64_OpCode_t* x64_find_OpCode_by_type(const int type) return NULL; } -scf_x64_OpCode_t* x64_find_OpCode(const int type, const int OpBytes, const int RegBytes, const int EG) +scf_x64_OpCode_t* x64_find_OpCode_by_name(const char* name) { int i; for (i = 0; i < sizeof(x64_OpCodes) / sizeof(x64_OpCodes[0]); i++) { scf_x64_OpCode_t* OpCode = &(x64_OpCodes[i]); - - if (type == OpCode->type - && OpBytes == OpCode->OpBytes - && RegBytes == OpCode->RegBytes - && EG == OpCode->EG) + if (!strcmp(OpCode->name, name)) return OpCode; } return NULL; } -int x64_find_OpCodes(scf_vector_t* results, const int type, const int OpBytes, const int RegBytes, const int EG) +scf_x64_OpCode_t* x64_find_OpCode(const int type, const int OpBytes, const int RegBytes, const int EG) { int i; for (i = 0; i < sizeof(x64_OpCodes) / sizeof(x64_OpCodes[0]); i++) { @@ -356,13 +378,8 @@ int x64_find_OpCodes(scf_vector_t* results, const int type, const int OpBytes, c if (type == OpCode->type && OpBytes == OpCode->OpBytes && RegBytes == OpCode->RegBytes - && EG == OpCode->EG) { - - int ret = scf_vector_add(results, OpCode); - if (ret < 0) - return ret; - } + && EG == OpCode->EG) + return OpCode; } - return 0; + return NULL; } - diff --git a/js/native/x64/scf_x64_opcode.h b/js/native/x64/scf_x64_opcode.h index c471e2d..0e45f98 100644 --- a/js/native/x64/scf_x64_opcode.h +++ b/js/native/x64/scf_x64_opcode.h @@ -3,6 +3,7 @@ #include"scf_native.h" #include"scf_x64_util.h" +#include"scf_elf.h" typedef struct { int type; @@ -27,10 +28,91 @@ typedef struct { uint32_t regs[2]; } scf_x64_OpCode_t; -scf_x64_OpCode_t* x64_find_OpCode_by_type(const int type); -scf_x64_OpCode_t* x64_find_OpCode(const int type, const int OpBytes, const int RegBytes, const int EG); +scf_x64_OpCode_t* x64_find_OpCode (const int type, const int OpBytes, const int RegBytes, const int EG); +scf_x64_OpCode_t* x64_find_OpCode_by_type(const int type); +scf_x64_OpCode_t* x64_find_OpCode_by_name(const char* name); -int x64_find_OpCodes(scf_vector_t* results, const int type, const int OpBytes, const int RegBytes, const int EG); +scf_instruction_t* x64_make_inst (scf_x64_OpCode_t* OpCode, int size); +scf_instruction_t* x64_make_inst_G(scf_x64_OpCode_t* OpCode, scf_register_t* r); +scf_instruction_t* x64_make_inst_E(scf_x64_OpCode_t* OpCode, scf_register_t* r); +scf_instruction_t* x64_make_inst_I(scf_x64_OpCode_t* OpCode, uint8_t* imm, int size); +void x64_make_inst_I2(scf_instruction_t* inst, scf_x64_OpCode_t* OpCode, uint8_t* imm, int size); + +scf_instruction_t* x64_make_inst_I2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, uint8_t* imm, int size); +scf_instruction_t* x64_make_inst_I2E(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, uint8_t* imm, int size); + +scf_instruction_t* x64_make_inst_G2E(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_src); +scf_instruction_t* x64_make_inst_E2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_src); + +scf_instruction_t* x64_make_inst_P2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_base, int32_t offset); +scf_instruction_t* x64_make_inst_G2P(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, int32_t offset, scf_register_t* r_src); +scf_instruction_t* x64_make_inst_I2P(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, int32_t offset, uint8_t* imm, int size); + +scf_instruction_t* x64_make_inst_SIB2G(scf_x64_OpCode_t* OpCode, scf_register_t* r_dst, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp); +scf_instruction_t* x64_make_inst_G2SIB(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp, scf_register_t* r_src); +scf_instruction_t* x64_make_inst_I2SIB(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp, uint8_t* imm, int32_t size); + +scf_instruction_t* x64_make_inst_SIB(scf_x64_OpCode_t* OpCode, scf_register_t* r_base, scf_register_t* r_index, int32_t scale, int32_t disp, int size); +scf_instruction_t* x64_make_inst_P (scf_x64_OpCode_t* OpCode, scf_register_t* r_base, int32_t offset, int size); + +scf_instruction_t* x64_make_inst_L (scf_rela_t** prela, scf_x64_OpCode_t* OpCode); +scf_instruction_t* x64_make_inst_I2L(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, uint8_t* imm, int32_t size); +scf_instruction_t* x64_make_inst_G2L(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_register_t* r_src); +scf_instruction_t* x64_make_inst_L2G(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, scf_register_t* r_dst); + +#define X64_INST_ADD_CHECK(vec, inst, rela) \ + do { \ + if (!(inst)) { \ + scf_loge("\n"); \ + return -ENOMEM; \ + } \ + int ret = scf_vector_add((vec), (inst)); \ + if (ret < 0) { \ + scf_loge("\n"); \ + scf_instruction_free(inst); \ + scf_rela_free(rela); \ + return ret; \ + } \ + } while (0) + +#define X64_RELA_ADD_CHECK(vec, rela, c, v, f) \ + do { \ + if (rela) { \ + (rela)->code = (c); \ + (rela)->var = (v); \ + (rela)->func = (f); \ + (rela)->inst = (c)->instructions->data[(c)->instructions->size - 1]; \ + (rela)->addend = -4; \ + (rela)->type = R_X86_64_PC32; \ + int ret = scf_vector_add((vec), (rela)); \ + if (ret < 0) { \ + scf_loge("\n"); \ + scf_rela_free(rela); \ + return ret; \ + } \ + } \ + } while (0) + +#define X64_RELA_ADD_LABEL(vec, rela, _inst, _label) \ + do { \ + if (rela) { \ + (rela)->inst = (_inst); \ + (rela)->addend = -4; \ + (rela)->type = R_X86_64_PC32; \ + (rela)->name = scf_string_clone(_label); \ + if (!(rela)->name) { \ + scf_loge("\n"); \ + scf_rela_free(rela); \ + return -ENOMEM; \ + } \ + int ret = scf_vector_add((vec), (rela)); \ + if (ret < 0) { \ + scf_loge("\n"); \ + scf_rela_free(rela); \ + return ret; \ + } \ + } \ + } while (0) #endif diff --git a/js/native/x64/scf_x64_reg.c b/js/native/x64/scf_x64_reg.c index 36b1bd7..efcc9aa 100644 --- a/js/native/x64/scf_x64_reg.c +++ b/js/native/x64/scf_x64_reg.c @@ -1,117 +1,5 @@ #include"scf_x64.h" - -scf_register_t x64_registers[] = { - - {0, 1, "al", X64_COLOR(0, 0, 0x1), NULL, 0}, - {0, 2, "ax", X64_COLOR(0, 0, 0x3), NULL, 0}, - {0, 4, "eax", X64_COLOR(0, 0, 0xf), NULL, 0}, - {0, 8, "rax", X64_COLOR(0, 0, 0xff), NULL, 0}, - - {1, 1, "cl", X64_COLOR(0, 1, 0x1), NULL, 0}, - {1, 2, "cx", X64_COLOR(0, 1, 0x3), NULL, 0}, - {1, 4, "ecx", X64_COLOR(0, 1, 0xf), NULL, 0}, - {1, 8, "rcx", X64_COLOR(0, 1, 0xff), NULL, 0}, - - {2, 1, "dl", X64_COLOR(0, 2, 0x1), NULL, 0}, - {2, 2, "dx", X64_COLOR(0, 2, 0x3), NULL, 0}, - {2, 4, "edx", X64_COLOR(0, 2, 0xf), NULL, 0}, - {2, 8, "rdx", X64_COLOR(0, 2, 0xff), NULL, 0}, - - {3, 1, "bl", X64_COLOR(0, 3, 0x1), NULL, 0}, - {3, 2, "bx", X64_COLOR(0, 3, 0x3), NULL, 0}, - {3, 4, "ebx", X64_COLOR(0, 3, 0xf), NULL, 0}, - {3, 8, "rbx", X64_COLOR(0, 3, 0xff), NULL, 0}, - - {4, 2, "sp", X64_COLOR(0, 4, 0x3), NULL, 0}, - {4, 4, "esp", X64_COLOR(0, 4, 0xf), NULL, 0}, - {4, 8, "rsp", X64_COLOR(0, 4, 0xff), NULL, 0}, - - {5, 2, "bp", X64_COLOR(0, 5, 0x3), NULL, 0}, - {5, 4, "ebp", X64_COLOR(0, 5, 0xf), NULL, 0}, - {5, 8, "rbp", X64_COLOR(0, 5, 0xff), NULL, 0}, - - {6, 1, "sil", X64_COLOR(0, 6, 0x1), NULL, 0}, - {6, 2, "si", X64_COLOR(0, 6, 0x3), NULL, 0}, - {6, 4, "esi", X64_COLOR(0, 6, 0xf), NULL, 0}, - {6, 8, "rsi", X64_COLOR(0, 6, 0xff), NULL, 0}, - - {7, 1, "dil", X64_COLOR(0, 7, 0x1), NULL, 0}, - {7, 2, "di", X64_COLOR(0, 7, 0x3), NULL, 0}, - {7, 4, "edi", X64_COLOR(0, 7, 0xf), NULL, 0}, - {7, 8, "rdi", X64_COLOR(0, 7, 0xff), NULL, 0}, - - {8, 1, "r8b", X64_COLOR(0, 8, 0x1), NULL, 0}, - {8, 2, "r8w", X64_COLOR(0, 8, 0x3), NULL, 0}, - {8, 4, "r8d", X64_COLOR(0, 8, 0xf), NULL, 0}, - {8, 8, "r8", X64_COLOR(0, 8, 0xff), NULL, 0}, - - {9, 1, "r9b", X64_COLOR(0, 9, 0x1), NULL, 0}, - {9, 2, "r9w", X64_COLOR(0, 9, 0x3), NULL, 0}, - {9, 4, "r9d", X64_COLOR(0, 9, 0xf), NULL, 0}, - {9, 8, "r9", X64_COLOR(0, 9, 0xff), NULL, 0}, - - {10, 1, "r10b", X64_COLOR(0, 10, 0x1), NULL, 0}, - {10, 2, "r10w", X64_COLOR(0, 10, 0x3), NULL, 0}, - {10, 4, "r10d", X64_COLOR(0, 10, 0xf), NULL, 0}, - {10, 8, "r10", X64_COLOR(0, 10, 0xff), NULL, 0}, - - {11, 1, "r11b", X64_COLOR(0, 11, 0x1), NULL, 0}, - {11, 2, "r11w", X64_COLOR(0, 11, 0x3), NULL, 0}, - {11, 4, "r11d", X64_COLOR(0, 11, 0xf), NULL, 0}, - {11, 8, "r11", X64_COLOR(0, 11, 0xff), NULL, 0}, - - {12, 1, "r12b", X64_COLOR(0, 12, 0x1), NULL, 0}, - {12, 2, "r12w", X64_COLOR(0, 12, 0x3), NULL, 0}, - {12, 4, "r12d", X64_COLOR(0, 12, 0xf), NULL, 0}, - {12, 8, "r12", X64_COLOR(0, 12, 0xff), NULL, 0}, - - {13, 1, "r13b", X64_COLOR(0, 13, 0x1), NULL, 0}, - {13, 2, "r13w", X64_COLOR(0, 13, 0x3), NULL, 0}, - {13, 4, "r13d", X64_COLOR(0, 13, 0xf), NULL, 0}, - {13, 8, "r13", X64_COLOR(0, 13, 0xff), NULL, 0}, - - {14, 1, "r14b", X64_COLOR(0, 14, 0x1), NULL, 0}, - {14, 2, "r14w", X64_COLOR(0, 14, 0x3), NULL, 0}, - {14, 4, "r14d", X64_COLOR(0, 14, 0xf), NULL, 0}, - {14, 8, "r14", X64_COLOR(0, 14, 0xff), NULL, 0}, - - {15, 1, "r15b", X64_COLOR(0, 15, 0x1), NULL, 0}, - {15, 2, "r15w", X64_COLOR(0, 15, 0x3), NULL, 0}, - {15, 4, "r15d", X64_COLOR(0, 15, 0xf), NULL, 0}, - {15, 8, "r15", X64_COLOR(0, 15, 0xff), NULL, 0}, - - {4, 1, "ah", X64_COLOR(0, 0, 0x2), NULL, 0}, - {5, 1, "ch", X64_COLOR(0, 1, 0x2), NULL, 0}, - {6, 1, "dh", X64_COLOR(0, 2, 0x2), NULL, 0}, - {7, 1, "bh", X64_COLOR(0, 3, 0x2), NULL, 0}, - - {0, 4, "mm0", X64_COLOR(1, 0, 0xf), NULL, 0}, - {0, 8, "xmm0", X64_COLOR(1, 0, 0xff), NULL, 0}, - - {1, 4, "mm1", X64_COLOR(1, 1, 0xf), NULL, 0}, - {1, 8, "xmm1", X64_COLOR(1, 1, 0xff), NULL, 0}, - - {2, 4, "mm2", X64_COLOR(1, 2, 0xf), NULL, 0}, - {2, 8, "xmm2", X64_COLOR(1, 2, 0xff), NULL, 0}, - - {3, 4, "mm3", X64_COLOR(1, 3, 0xf), NULL, 0}, - {3, 8, "xmm3", X64_COLOR(1, 3, 0xff), NULL, 0}, - - {4, 4, "mm4", X64_COLOR(1, 4, 0xf), NULL, 0}, - {4, 8, "xmm4", X64_COLOR(1, 4, 0xff), NULL, 0}, - - {5, 4, "mm5", X64_COLOR(1, 5, 0xf), NULL, 0}, - {5, 8, "xmm5", X64_COLOR(1, 5, 0xff), NULL, 0}, - - {6, 4, "mm6", X64_COLOR(1, 6, 0xf), NULL, 0}, - {6, 8, "xmm6", X64_COLOR(1, 6, 0xff), NULL, 0}, - - {7, 4, "mm7", X64_COLOR(1, 7, 0xf), NULL, 0}, - {7, 8, "xmm7", X64_COLOR(1, 7, 0xff), NULL, 0}, - - - {0xf, 8, "rip", X64_COLOR(0, 7, 0xff), NULL, 0}, -}; +#include"scf_x64_reg_util.c" int x64_reg_cached_vars(scf_register_t* r) { @@ -271,7 +159,7 @@ int x64_caller_save_regs(scf_3ac_code_t* c, const char* regs[], int nb_regs, int inst = x64_make_inst_G2P(movsd, rsp, size + stack_size, r2); else { inst = x64_make_inst_I2E(sub, rsp, (uint8_t*)&imm, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(movsd, rsp, 0, r2); } @@ -281,7 +169,7 @@ int x64_caller_save_regs(scf_3ac_code_t* c, const char* regs[], int nb_regs, int else inst = x64_make_inst_G(push, r2); } - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); saved_regs[n++] = r2; size += 8; @@ -295,7 +183,7 @@ int x64_caller_save_regs(scf_3ac_code_t* c, const char* regs[], int nb_regs, int inst = x64_make_inst_G2P(movsd, rsp, size + stack_size, r2); else { inst = x64_make_inst_I2E(sub, rsp, (uint8_t*)&imm, 4); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2P(movsd, rsp, 0, r2); } @@ -305,7 +193,7 @@ int x64_caller_save_regs(scf_3ac_code_t* c, const char* regs[], int nb_regs, int else inst = x64_make_inst_G(push, r2); } - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); saved_regs[n++] = r2; size += 8; @@ -366,15 +254,15 @@ int x64_pop_regs(scf_vector_t* instructions, scf_register_t** regs, int nb_regs, if (i == nb_updated) { if (X64_COLOR_TYPE(r2->color)) { inst = x64_make_inst_P2G(movsd, r2, rsp, 0); - X64_INST_ADD_CHECK(instructions, inst); + X64_INST_ADD_CHECK(instructions, inst, NULL); inst = x64_make_inst_I2E(add, rsp, (uint8_t*)&imm, 4); } else inst = x64_make_inst_G(pop, r2); - X64_INST_ADD_CHECK(instructions, inst); + X64_INST_ADD_CHECK(instructions, inst, NULL); } else { inst = x64_make_inst_I2E(add, rsp, (uint8_t*)&imm, 4); - X64_INST_ADD_CHECK(instructions, inst); + X64_INST_ADD_CHECK(instructions, inst, NULL); } } return 0; @@ -416,58 +304,6 @@ int x64_registers_reset() return 0; } -scf_register_t* x64_find_register(const char* name) -{ - int i; - for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { - - scf_register_t* r = &(x64_registers[i]); - - if (!strcmp(r->name, name)) - return r; - } - return NULL; -} - -scf_register_t* x64_find_register_type_id_bytes(uint32_t type, uint32_t id, int bytes) -{ - int i; - for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { - - scf_register_t* r = &(x64_registers[i]); - - if (X64_COLOR_TYPE(r->color) == type && r->id == id && r->bytes == bytes) - return r; - } - return NULL; -} - -scf_register_t* x64_find_register_color(intptr_t color) -{ - int i; - for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { - - scf_register_t* r = &(x64_registers[i]); - - if (r->color == color) - return r; - } - return NULL; -} - -scf_register_t* x64_find_register_color_bytes(intptr_t color, int bytes) -{ - int i; - for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { - - scf_register_t* r = &(x64_registers[i]); - - if (X64_COLOR_CONFLICT(r->color, color) && r->bytes == bytes) - return r; - } - return NULL; -} - scf_vector_t* x64_register_colors() { scf_vector_t* colors = scf_vector_alloc(); @@ -572,7 +408,7 @@ int x64_save_var2(scf_dag_node_t* dn, scf_register_t* r, scf_3ac_code_t* c, scf_ } inst = x64_make_inst_G2M(&rela, mov, v, NULL, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); end: @@ -878,7 +714,7 @@ int x64_load_const(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, scf lea = x64_find_OpCode(SCF_X64_LEA, size, size, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, lea, r, NULL, v); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->text_relas, rela, c, NULL, v->func_ptr); } else { @@ -886,7 +722,7 @@ int x64_load_const(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, scf xor = x64_find_OpCode(SCF_X64_XOR, size, size, SCF_X64_G2E); inst = x64_make_inst_G2E(xor, r, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } } else if (scf_variable_const_string(v)) { @@ -900,7 +736,7 @@ int x64_load_const(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, scf lea = x64_find_OpCode(SCF_X64_LEA, size, size, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, lea, r, NULL, v); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); } else if (v->nb_dimentions > 0) { @@ -911,13 +747,13 @@ int x64_load_const(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, scf lea = x64_find_OpCode(SCF_X64_LEA, size, size, SCF_X64_E2G); inst = x64_make_inst_M2G(&rela, lea, r, NULL, v); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, v, NULL); } else { mov = x64_find_OpCode(SCF_X64_MOV, size, size, SCF_X64_I2G); inst = x64_make_inst_I2G(mov, r, (uint8_t*)&v->data, size); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } return 0; @@ -993,7 +829,7 @@ int x64_load_reg(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, scf_f } inst = x64_make_inst_M2G(&rela, mov, r, NULL, dn->var); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, rela); X64_RELA_ADD_CHECK(f->data_relas, rela, c, dn->var, NULL); dn->loaded = 1; @@ -1223,12 +1059,12 @@ int x64_array_index_reg(x64_sib_t* sib, scf_dag_node_t* base, scf_dag_node_t* in if (scf_variable_signed(index->var)) { mov = x64_find_OpCode(SCF_X64_MOVSX, ri->bytes, ri2->bytes, SCF_X64_E2G); inst = x64_make_inst_E2G(mov, ri2, ri); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else if (ri->bytes <= 2) { mov = x64_find_OpCode(SCF_X64_MOVZX, ri->bytes, ri2->bytes, SCF_X64_E2G); inst = x64_make_inst_E2G(mov, ri2, ri); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } else { assert(4 == ri->bytes); @@ -1260,7 +1096,7 @@ int x64_array_index_reg(x64_sib_t* sib, scf_dag_node_t* base, scf_dag_node_t* in mov = x64_find_OpCode(SCF_X64_MOV, 8, 8, SCF_X64_G2E); inst = x64_make_inst_G2E(mov, rs, ri2); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); shl = x64_find_OpCode(SCF_X64_SHL, 1, 8, SCF_X64_I2E); add = x64_find_OpCode(SCF_X64_ADD, 8, 8, SCF_X64_G2E); @@ -1283,17 +1119,17 @@ int x64_array_index_reg(x64_sib_t* sib, scf_dag_node_t* base, scf_dag_node_t* in count -= i; inst = x64_make_inst_I2E(shl, rs, (uint8_t*)&count, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); inst = x64_make_inst_G2E(add, rs, ri2); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); count = i; } if (count > 0) { inst = x64_make_inst_I2E(shl, rs, (uint8_t*)&count, 1); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); } ri = rs; @@ -1369,7 +1205,7 @@ int x64_push_callee_regs(scf_3ac_code_t* c, scf_function_t* f) if (j < N) { inst = x64_make_inst_G(push, r); - X64_INST_ADD_CHECK(f->init_code->instructions, inst); + X64_INST_ADD_CHECK(f->init_code->instructions, inst, NULL); f->init_code_bytes += inst->len; } @@ -1406,7 +1242,7 @@ int x64_pop_callee_regs(scf_3ac_code_t* c, scf_function_t* f) if (j < N) { inst = x64_make_inst_G(pop, r); - X64_INST_ADD_CHECK(c->instructions, inst); + X64_INST_ADD_CHECK(c->instructions, inst, NULL); bb->code_bytes += inst->len; f->callee_saved_size += 8; diff --git a/js/native/x64/scf_x64_reg.h b/js/native/x64/scf_x64_reg.h index 40c5967..0e59579 100644 --- a/js/native/x64/scf_x64_reg.h +++ b/js/native/x64/scf_x64_reg.h @@ -1,25 +1,7 @@ #ifndef SCF_X64_REG_H #define SCF_X64_REG_H -#include"scf_native.h" -#include"scf_x64_util.h" - -#define X64_COLOR(type, id, mask) ((type) << 24 | (id) << 16 | (mask)) -#define X64_COLOR_TYPE(c) ((c) >> 24) -#define X64_COLOR_ID(c) (((c) >> 16) & 0xff) -#define X64_COLOR_MASK(c) ((c) & 0xffff) -#define X64_COLOR_CONFLICT(c0, c1) ( (c0) >> 16 == (c1) >> 16 && (c0) & (c1) & 0xffff ) - -#define X64_COLOR_BYTES(c) \ - ({ \ - int n = 0;\ - intptr_t minor = (c) & 0xffff; \ - while (minor) { \ - minor &= minor - 1; \ - n++;\ - } \ - n;\ - }) +#include"scf_x64_reg_util.h" #define X64_SELECT_REG_CHECK(pr, dn, c, f, load_flag) \ do {\ @@ -98,15 +80,6 @@ static const char* x64_abi_callee_saves[] = }; #define X64_ABI_CALLEE_SAVES_NB (sizeof(x64_abi_callee_saves) / sizeof(x64_abi_callee_saves[0])) -typedef struct { - scf_register_t* base; - scf_register_t* index; - - int32_t scale; - int32_t disp; - int32_t size; -} x64_sib_t; - static inline int x64_variable_size(scf_variable_t* v) { if (v->nb_dimentions > 0) @@ -126,14 +99,6 @@ void x64_registers_clear(); void x64_registers_print(); scf_vector_t* x64_register_colors(); -scf_register_t* x64_find_register(const char* name); - -scf_register_t* x64_find_register_type_id_bytes(uint32_t type, uint32_t id, int bytes); - -scf_register_t* x64_find_register_color(intptr_t color); - -scf_register_t* x64_find_register_color_bytes(intptr_t color, int bytes); - scf_register_t* x64_find_abi_register(int index, int bytes); scf_register_t* x64_select_overflowed_reg(scf_dag_node_t* dn, scf_3ac_code_t* c); @@ -186,48 +151,4 @@ static inline int x64_reg_is_retval(scf_register_t* r) return 0; } -static inline int x64_inst_data_is_reg(scf_inst_data_t* id) -{ - scf_register_t* rsp = x64_find_register("rsp"); - scf_register_t* rbp = x64_find_register("rbp"); - - if (!id->flag && id->base && id->base != rsp && id->base != rbp && 0 == id->imm_size) - return 1; - return 0; -} - -static inline int x64_inst_data_is_local(scf_inst_data_t* id) -{ - scf_register_t* rbp = x64_find_register("rbp"); - scf_register_t* rsp = x64_find_register("rsp"); - - if (id->flag && (id->base == rbp || id->base == rsp)) - return 1; - return 0; -} - -static inline int x64_inst_data_is_global(scf_inst_data_t* id) -{ - if (id->flag && !id->base) - return 1; - return 0; -} - -static inline int x64_inst_data_is_const(scf_inst_data_t* id) -{ - if (!id->flag && id->imm_size > 0) - return 1; - return 0; -} - -static inline int x64_inst_data_is_pointer(scf_inst_data_t* id) -{ - scf_register_t* rbp = x64_find_register("rbp"); - scf_register_t* rsp = x64_find_register("rsp"); - - if (id->flag && id->base && id->base != rbp && id->base != rsp) - return 1; - return 0; -} - #endif diff --git a/js/native/x64/scf_x64_reg_util.c b/js/native/x64/scf_x64_reg_util.c new file mode 100644 index 0000000..36556bd --- /dev/null +++ b/js/native/x64/scf_x64_reg_util.c @@ -0,0 +1,166 @@ +#include"scf_x64.h" + +scf_register_t x64_registers[] = { + + {0, 1, "al", X64_COLOR(0, 0, 0x1), NULL, 0}, + {0, 2, "ax", X64_COLOR(0, 0, 0x3), NULL, 0}, + {0, 4, "eax", X64_COLOR(0, 0, 0xf), NULL, 0}, + {0, 8, "rax", X64_COLOR(0, 0, 0xff), NULL, 0}, + + {1, 1, "cl", X64_COLOR(0, 1, 0x1), NULL, 0}, + {1, 2, "cx", X64_COLOR(0, 1, 0x3), NULL, 0}, + {1, 4, "ecx", X64_COLOR(0, 1, 0xf), NULL, 0}, + {1, 8, "rcx", X64_COLOR(0, 1, 0xff), NULL, 0}, + + {2, 1, "dl", X64_COLOR(0, 2, 0x1), NULL, 0}, + {2, 2, "dx", X64_COLOR(0, 2, 0x3), NULL, 0}, + {2, 4, "edx", X64_COLOR(0, 2, 0xf), NULL, 0}, + {2, 8, "rdx", X64_COLOR(0, 2, 0xff), NULL, 0}, + + {3, 1, "bl", X64_COLOR(0, 3, 0x1), NULL, 0}, + {3, 2, "bx", X64_COLOR(0, 3, 0x3), NULL, 0}, + {3, 4, "ebx", X64_COLOR(0, 3, 0xf), NULL, 0}, + {3, 8, "rbx", X64_COLOR(0, 3, 0xff), NULL, 0}, + + {4, 2, "sp", X64_COLOR(0, 4, 0x3), NULL, 0}, + {4, 4, "esp", X64_COLOR(0, 4, 0xf), NULL, 0}, + {4, 8, "rsp", X64_COLOR(0, 4, 0xff), NULL, 0}, + + {5, 2, "bp", X64_COLOR(0, 5, 0x3), NULL, 0}, + {5, 4, "ebp", X64_COLOR(0, 5, 0xf), NULL, 0}, + {5, 8, "rbp", X64_COLOR(0, 5, 0xff), NULL, 0}, + + {6, 1, "sil", X64_COLOR(0, 6, 0x1), NULL, 0}, + {6, 2, "si", X64_COLOR(0, 6, 0x3), NULL, 0}, + {6, 4, "esi", X64_COLOR(0, 6, 0xf), NULL, 0}, + {6, 8, "rsi", X64_COLOR(0, 6, 0xff), NULL, 0}, + + {7, 1, "dil", X64_COLOR(0, 7, 0x1), NULL, 0}, + {7, 2, "di", X64_COLOR(0, 7, 0x3), NULL, 0}, + {7, 4, "edi", X64_COLOR(0, 7, 0xf), NULL, 0}, + {7, 8, "rdi", X64_COLOR(0, 7, 0xff), NULL, 0}, + + {8, 1, "r8b", X64_COLOR(0, 8, 0x1), NULL, 0}, + {8, 2, "r8w", X64_COLOR(0, 8, 0x3), NULL, 0}, + {8, 4, "r8d", X64_COLOR(0, 8, 0xf), NULL, 0}, + {8, 8, "r8", X64_COLOR(0, 8, 0xff), NULL, 0}, + + {9, 1, "r9b", X64_COLOR(0, 9, 0x1), NULL, 0}, + {9, 2, "r9w", X64_COLOR(0, 9, 0x3), NULL, 0}, + {9, 4, "r9d", X64_COLOR(0, 9, 0xf), NULL, 0}, + {9, 8, "r9", X64_COLOR(0, 9, 0xff), NULL, 0}, + + {10, 1, "r10b", X64_COLOR(0, 10, 0x1), NULL, 0}, + {10, 2, "r10w", X64_COLOR(0, 10, 0x3), NULL, 0}, + {10, 4, "r10d", X64_COLOR(0, 10, 0xf), NULL, 0}, + {10, 8, "r10", X64_COLOR(0, 10, 0xff), NULL, 0}, + + {11, 1, "r11b", X64_COLOR(0, 11, 0x1), NULL, 0}, + {11, 2, "r11w", X64_COLOR(0, 11, 0x3), NULL, 0}, + {11, 4, "r11d", X64_COLOR(0, 11, 0xf), NULL, 0}, + {11, 8, "r11", X64_COLOR(0, 11, 0xff), NULL, 0}, + + {12, 1, "r12b", X64_COLOR(0, 12, 0x1), NULL, 0}, + {12, 2, "r12w", X64_COLOR(0, 12, 0x3), NULL, 0}, + {12, 4, "r12d", X64_COLOR(0, 12, 0xf), NULL, 0}, + {12, 8, "r12", X64_COLOR(0, 12, 0xff), NULL, 0}, + + {13, 1, "r13b", X64_COLOR(0, 13, 0x1), NULL, 0}, + {13, 2, "r13w", X64_COLOR(0, 13, 0x3), NULL, 0}, + {13, 4, "r13d", X64_COLOR(0, 13, 0xf), NULL, 0}, + {13, 8, "r13", X64_COLOR(0, 13, 0xff), NULL, 0}, + + {14, 1, "r14b", X64_COLOR(0, 14, 0x1), NULL, 0}, + {14, 2, "r14w", X64_COLOR(0, 14, 0x3), NULL, 0}, + {14, 4, "r14d", X64_COLOR(0, 14, 0xf), NULL, 0}, + {14, 8, "r14", X64_COLOR(0, 14, 0xff), NULL, 0}, + + {15, 1, "r15b", X64_COLOR(0, 15, 0x1), NULL, 0}, + {15, 2, "r15w", X64_COLOR(0, 15, 0x3), NULL, 0}, + {15, 4, "r15d", X64_COLOR(0, 15, 0xf), NULL, 0}, + {15, 8, "r15", X64_COLOR(0, 15, 0xff), NULL, 0}, + + {4, 1, "ah", X64_COLOR(0, 0, 0x2), NULL, 0}, + {5, 1, "ch", X64_COLOR(0, 1, 0x2), NULL, 0}, + {6, 1, "dh", X64_COLOR(0, 2, 0x2), NULL, 0}, + {7, 1, "bh", X64_COLOR(0, 3, 0x2), NULL, 0}, + + {0, 4, "mm0", X64_COLOR(1, 0, 0xf), NULL, 0}, + {0, 8, "xmm0", X64_COLOR(1, 0, 0xff), NULL, 0}, + + {1, 4, "mm1", X64_COLOR(1, 1, 0xf), NULL, 0}, + {1, 8, "xmm1", X64_COLOR(1, 1, 0xff), NULL, 0}, + + {2, 4, "mm2", X64_COLOR(1, 2, 0xf), NULL, 0}, + {2, 8, "xmm2", X64_COLOR(1, 2, 0xff), NULL, 0}, + + {3, 4, "mm3", X64_COLOR(1, 3, 0xf), NULL, 0}, + {3, 8, "xmm3", X64_COLOR(1, 3, 0xff), NULL, 0}, + + {4, 4, "mm4", X64_COLOR(1, 4, 0xf), NULL, 0}, + {4, 8, "xmm4", X64_COLOR(1, 4, 0xff), NULL, 0}, + + {5, 4, "mm5", X64_COLOR(1, 5, 0xf), NULL, 0}, + {5, 8, "xmm5", X64_COLOR(1, 5, 0xff), NULL, 0}, + + {6, 4, "mm6", X64_COLOR(1, 6, 0xf), NULL, 0}, + {6, 8, "xmm6", X64_COLOR(1, 6, 0xff), NULL, 0}, + + {7, 4, "mm7", X64_COLOR(1, 7, 0xf), NULL, 0}, + {7, 8, "xmm7", X64_COLOR(1, 7, 0xff), NULL, 0}, + + + {0xf, 8, "rip", X64_COLOR(0, 7, 0xff), NULL, 0}, +}; + +scf_register_t* x64_find_register(const char* name) +{ + int i; + for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { + + scf_register_t* r = &(x64_registers[i]); + + if (!strcmp(r->name, name)) + return r; + } + return NULL; +} + +scf_register_t* x64_find_register_type_id_bytes(uint32_t type, uint32_t id, int bytes) +{ + int i; + for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { + + scf_register_t* r = &(x64_registers[i]); + + if (X64_COLOR_TYPE(r->color) == type && r->id == id && r->bytes == bytes) + return r; + } + return NULL; +} + +scf_register_t* x64_find_register_color(intptr_t color) +{ + int i; + for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { + + scf_register_t* r = &(x64_registers[i]); + + if (r->color == color) + return r; + } + return NULL; +} + +scf_register_t* x64_find_register_color_bytes(intptr_t color, int bytes) +{ + int i; + for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) { + + scf_register_t* r = &(x64_registers[i]); + + if (X64_COLOR_CONFLICT(r->color, color) && r->bytes == bytes) + return r; + } + return NULL; +} diff --git a/js/native/x64/scf_x64_reg_util.h b/js/native/x64/scf_x64_reg_util.h new file mode 100644 index 0000000..1f23aaf --- /dev/null +++ b/js/native/x64/scf_x64_reg_util.h @@ -0,0 +1,85 @@ +#ifndef SCF_X64_REG_UTIL_H +#define SCF_X64_REG_UTIL_H + +#include"scf_native.h" +#include"scf_x64_util.h" + +#define X64_COLOR(type, id, mask) ((type) << 24 | (id) << 16 | (mask)) +#define X64_COLOR_TYPE(c) ((c) >> 24) +#define X64_COLOR_ID(c) (((c) >> 16) & 0xff) +#define X64_COLOR_MASK(c) ((c) & 0xffff) +#define X64_COLOR_CONFLICT(c0, c1) ( (c0) >> 16 == (c1) >> 16 && (c0) & (c1) & 0xffff ) + +#define X64_COLOR_BYTES(c) \ + ({ \ + int n = 0;\ + intptr_t minor = (c) & 0xffff; \ + while (minor) { \ + minor &= minor - 1; \ + n++;\ + } \ + n;\ + }) + +typedef struct { + scf_register_t* base; + scf_register_t* index; + + int32_t scale; + int32_t disp; + int32_t size; +} x64_sib_t; + +scf_register_t* x64_find_register(const char* name); + +scf_register_t* x64_find_register_type_id_bytes(uint32_t type, uint32_t id, int bytes); + +scf_register_t* x64_find_register_color(intptr_t color); + +scf_register_t* x64_find_register_color_bytes(intptr_t color, int bytes); + +static inline int x64_inst_data_is_reg(scf_inst_data_t* id) +{ + scf_register_t* rsp = x64_find_register("rsp"); + scf_register_t* rbp = x64_find_register("rbp"); + + if (!id->flag && id->base && id->base != rsp && id->base != rbp && 0 == id->imm_size) + return 1; + return 0; +} + +static inline int x64_inst_data_is_local(scf_inst_data_t* id) +{ + scf_register_t* rbp = x64_find_register("rbp"); + scf_register_t* rsp = x64_find_register("rsp"); + + if (id->flag && (id->base == rbp || id->base == rsp)) + return 1; + return 0; +} + +static inline int x64_inst_data_is_global(scf_inst_data_t* id) +{ + if (id->flag && !id->base) + return 1; + return 0; +} + +static inline int x64_inst_data_is_const(scf_inst_data_t* id) +{ + if (!id->flag && id->imm_size > 0) + return 1; + return 0; +} + +static inline int x64_inst_data_is_pointer(scf_inst_data_t* id) +{ + scf_register_t* rbp = x64_find_register("rbp"); + scf_register_t* rsp = x64_find_register("rsp"); + + if (id->flag && id->base && id->base != rbp && id->base != rsp) + return 1; + return 0; +} + +#endif diff --git a/js/native/x64/scf_x64_util.h b/js/native/x64/scf_x64_util.h index 837c30a..caa7775 100644 --- a/js/native/x64/scf_x64_util.h +++ b/js/native/x64/scf_x64_util.h @@ -126,6 +126,8 @@ enum scf_x64_OpCode_types { SCF_X64_JMP, + SCF_X64_SYSCALL, + SCF_X64_NB }; @@ -325,4 +327,3 @@ static inline void scf_SIB_setBase(uint8_t* SIB, uint8_t base) } #endif - diff --git a/ui/Makefile b/ui/Makefile index d6ce2b0..f9c56cc 100644 --- a/ui/Makefile +++ b/ui/Makefile @@ -69,6 +69,7 @@ CFILES += ../js/parse/scf_operator_handler_expr.c CFILES += ../js/parse/scf_operator_handler_const.c CFILES += ../js/native/scf_native.c +CFILES += ../js/native/scf_instruction.c CFILES += ../js/native/x64/scf_x64.c CFILES += ../js/native/x64/scf_x64_inst.c CFILES += ../js/native/x64/scf_x64_inst_util.c -- 2.25.1