scf: update native/x64 master
authoryu.dongliang <18588496441@163.com>
Sun, 11 Jan 2026 15:28:07 +0000 (23:28 +0800)
committeryu.dongliang <18588496441@163.com>
Sun, 11 Jan 2026 15:28:07 +0000 (23:28 +0800)
34 files changed:
html/Makefile
js/abc_libjs.so
js/elf/scf_elf.c
js/elf/scf_elf.h
js/elf/scf_elf_naja.c
js/elf/scf_elf_naja_so.c
js/native/risc/scf_naja.c
js/native/risc/scf_risc_inst.c
js/native/risc/scf_risc_reg.h
js/native/risc/scf_risc_reg_naja.c
js/native/risc/scf_risc_util.h
js/native/scf_instruction.c [new file with mode: 0644]
js/native/scf_instruction.h [new file with mode: 0644]
js/native/scf_native.c
js/native/scf_native.h
js/native/x64/scf_x64.c
js/native/x64/scf_x64.h
js/native/x64/scf_x64_inst.c
js/native/x64/scf_x64_inst_binary.c
js/native/x64/scf_x64_inst_cmp.c
js/native/x64/scf_x64_inst_common.c
js/native/x64/scf_x64_inst_div.c
js/native/x64/scf_x64_inst_mul.c
js/native/x64/scf_x64_inst_shift.c
js/native/x64/scf_x64_inst_unary.c
js/native/x64/scf_x64_inst_util.c
js/native/x64/scf_x64_opcode.c
js/native/x64/scf_x64_opcode.h
js/native/x64/scf_x64_reg.c
js/native/x64/scf_x64_reg.h
js/native/x64/scf_x64_reg_util.c [new file with mode: 0644]
js/native/x64/scf_x64_reg_util.h [new file with mode: 0644]
js/native/x64/scf_x64_util.h
ui/Makefile

index 67b5badcda16465860cc99f5c12f0a8a86c38655..b04c19bc3cfa8b5030139aca723599b9a60afb01 100644 (file)
@@ -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
index 5dade4ecfb408a438cae51e889216ac9960d6494..376eaa007efa1e1da105f67eaf01a94be84e17be 100755 (executable)
Binary files a/js/abc_libjs.so and b/js/abc_libjs.so differ
index 76e5f29dc8db6008ef5e0297130e73ed07a16239..1d5c0206a57d80238378ff39aa512d480c5db2e0 100644 (file)
@@ -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) {
index cd3a606a328ad41861d625b268d7a8c5fd6d6b84..b3599aa57326e2b9ffdc4c35e96b84943214a215 100644 (file)
@@ -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);
index a0ccf87d39dc9b6aed3d4bf349e207b9a237927b..d399243d40215f16826377bbcd67e50a243e7755 100644 (file)
@@ -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:
index 04d800c354afc32c00d7614cd5c8a91f248989fe..073e9629ab90be259624f7a7c821fc310e15d395 100644 (file)
@@ -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);
index 791519a167e7307b633893615e67411f7c140bd9..ef56dff56f9a3a091797fa3dc197097f46825cf6 100644 (file)
@@ -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:
index 221af0e4cd39b6c635d60ffa12d90e69c0db6ce1..2d64b8d84369f50e49e4954ce622b68e1cc1c92f 100644 (file)
@@ -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;
 
index 4a43cc77b5069f52d85b4c3b4df70c410b6ed41d..abc0d7c64e7cc7497206d0fb938945a177223d91 100644 (file)
@@ -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
-
index bd3c8acf70078bc269497623dce26ddf3bb06575..9b6ff13ff85c5860e0953e652d089fdeb96502f7 100644 (file)
 #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);
 
index c64aa7ad3ca7b6dfe3608ef5f78436b16a68c6ad..d8209eb137ba570f72e41aa213fd43146b55f570 100644 (file)
@@ -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 (file)
index 0000000..92519a8
--- /dev/null
@@ -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 (file)
index 0000000..37c0b0e
--- /dev/null
@@ -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
index a80bcdcc656755399895542cd65abdc8a2178301..66eba9af18e24cda3ce0d2714857945f7b0469d7 100644 (file)
@@ -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));
index f09571750b31e50f0036da476dd92f48001b6616..29c16265fe074607fdcbea053c55c7f831eed564 100644 (file)
@@ -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);
 
index ced05bad09c9daaa2167086f750156292f67e32b..7ec69c31bb135c8e628d4e96946559daf9352a76 100644 (file)
@@ -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;
 
index 6ce1a692793740cea1121cb6d08ed36105b676c9..e30405c426d4c54bffbe6da9ab2c5fe443478a72 100644 (file)
@@ -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);
index c98b96f2a01abde4599bfb9189f62cb49c827522..51c6b9b49ddd0c6681883d9276c34ec6145f31e9 100644 (file)
@@ -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;
 }
 
index c2c5f5b07ab895afa8ab30c6b59d0dbf4be24576..a292950d9d4bcd170ebad180ccda5a37ad684120 100644 (file)
@@ -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;
index 84c40f956da242913285080895a5dff83b98c01f..55d871170ab94232b4db59204ef72f40a8f21641 100644 (file)
@@ -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;
 }
 
index c5df00fc15a4cd0c4c15fed82b8debce9ba7a5b7..cdd4fbd48e4251742527a7d4e5b70a79291959b6 100644 (file)
@@ -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;
 }
-
index 5400f1dcc34d146a7d3709d13bfcc978bd7d36f4..9de5722937a99409ba3b8f61e7acf51e805e37e8 100644 (file)
@@ -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);
        }
 
index 72527f5fab37682e85c243bdf1e97b930c6a40a8..6e20ce8247357161123203b74af34dd6f93384bf 100644 (file)
@@ -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);
        }
 
index 9c801ae2d84b09378c91ad871f219d37b8ae77b9..855ad926a60c193424c16e54c519b47aec8740a2 100644 (file)
@@ -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);
                }
        }
index 708505fe09ef4eda03f0be43e1f1ac737c405ebf..8ee535856aabbb8f5ea5c8d6e5a98c19d02da87c 100644 (file)
@@ -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;
 }
 
index 6074843077de9cebfa0c64ac054e2f90d6ac44f2..1c3f46164a61f7bce6610ba855e3c2e5cfcfae2a 100644 (file)
@@ -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) {
index 1b510827b70eb1d58fac72f51cb8f1119bb8c263..f67b5d218ac466964fb65e2af6b30dcdfc6269d5 100644 (file)
@@ -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;
 }
-
index c471e2d2772c86af165bd4a982a02376948f84d8..0e45f984262614635a4323b79aec770a9adf87fc 100644 (file)
@@ -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
index 36b1bd70c5aaf8e15c908ad26de0c81b6219c3dd..efcc9aa06277a27d56075ea79863fedebe8c6a87 100644 (file)
@@ -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;
index 40c5967771b5a90a2e3178d900559814df91a874..0e595799571f20c156eb1269a936748fc0be807b 100644 (file)
@@ -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 (file)
index 0000000..36556bd
--- /dev/null
@@ -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 (file)
index 0000000..1f23aaf
--- /dev/null
@@ -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
index 837c30a20ca92c82e7c3e98c6695c7eef624dbde..caa77758fd1395d0c6d63ab9a6f9f1fd428ebe1b 100644 (file)
@@ -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
-
index d6ce2b04188190af68142ee8749221fa816d05c8..f9c56cce9bb46404960f261c070efb2cf901dac2 100644 (file)
@@ -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