--- /dev/null
+include "../lib/scf_capi.c";
+
+int* f()
+{
+ return scf__auto_malloc(sizeof(int));
+}
+
+int main()
+{
+ int* p = f();
+
+ *p = 1;
+
+ printf("%d\n", *p);
+ return 0;
+}
static int _x64_save_rabi(scf_function_t* f)
{
- scf_register_t* rbp;
+ scf_register_t* rbp;
scf_instruction_t* inst;
scf_x64_OpCode_t* mov;
assert(v->arg_flag);
- scf_dag_node_t* dn;
- scf_dag_node_t* dn2;
+ scf_dag_node_t* dn;
+ scf_dag_node_t* dn2;
scf_dn_status_t* active;
- scf_register_t* rabi;
+ scf_register_t* rabi;
for (l = scf_list_head(&f->dag_list_head); l != scf_list_sentinel(&f->dag_list_head);
l = scf_list_next(l)) {
int x64_bb_load_dn(intptr_t color, scf_dag_node_t* dn, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_function_t* f)
{
scf_variable_t* v = dn->var;
- scf_register_t* r;
+ scf_register_t* r;
scf_instruction_t* inst;
int inst_bytes;
static int _x64_inst_call_argv(scf_3ac_code_t* c, scf_function_t* f)
{
- scf_register_t* rsp = x64_find_register("rsp");
+ scf_register_t* rsp = x64_find_register("rsp");
scf_x64_OpCode_t* lea;
scf_x64_OpCode_t* mov;
static int _x64_call_save_ret_regs(scf_3ac_code_t* c, scf_function_t* f, scf_function_t* pf)
{
scf_register_t* r;
- scf_variable_t* v;
+ scf_variable_t* v;
int i;
for (i = 0; i < pf->rets->size; i++) {
scf_dag_node_t* dn;
scf_variable_t* v;
- scf_register_t* rd;
- scf_register_t* rs;
+ scf_register_t* rd;
+ scf_register_t* rs;
scf_x64_OpCode_t* mov;
int nb_float = 0;
return -ENOMEM;
}
- scf_register_t* rsp = x64_find_register("rsp");
- scf_register_t* rax = x64_find_register("rax");
+ scf_register_t* rsp = x64_find_register("rsp");
+ scf_register_t* rax = x64_find_register("rax");
// scf_x64_OpCode_t* xor;
scf_x64_OpCode_t* mov;
scf_x64_OpCode_t* sub;
}
scf_instruction_t* inst = NULL;
- scf_register_t* rd = 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);
}
scf_instruction_t* inst = NULL;
- scf_register_t* rs = 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);
return -ENOMEM;
}
- scf_instruction_t* inst = NULL;
- scf_register_t* rs = NULL;
- scf_variable_t* var = src->dag_node->var;
+ scf_instruction_t* inst = NULL;
+ scf_register_t* rs = NULL;
+ scf_variable_t* var = src->dag_node->var;
int ret = x64_inst_op2(SCF_X64_MOV, dst->dag_node, src->dag_node, c, f);
if (ret < 0)
return _x64_inst_unary(ctx, c, SCF_X64_NEG);
scf_instruction_t* inst = NULL;
- scf_register_t* rd = NULL;
- scf_register_t* rs = NULL;
+ scf_register_t* rd = NULL;
+ scf_register_t* rs = NULL;
scf_x64_OpCode_t* pxor = x64_find_OpCode(SCF_X64_PXOR, 8, 8, SCF_X64_E2G);
scf_x64_OpCode_t* sub = x64_find_OpCode(SCF_X64_SUBSS, 4, 4, SCF_X64_E2G);
scf_variable_t* vb = base->dag_node->var;
scf_variable_t* vs = src ->dag_node->var;
- scf_register_t* rs = NULL;
+ scf_register_t* rs = NULL;
x64_sib_t sib = {0};
scf_x64_OpCode_t* OpCode;
scf_variable_t* vi = index->dag_node->var;
scf_variable_t* vs = scale->dag_node->var;
- scf_register_t* rd = NULL;
+ scf_register_t* rd = NULL;
x64_sib_t sib = {0};
scf_x64_OpCode_t* OpCode;
scf_3ac_operand_t* dst = c->dsts->data[0];
scf_3ac_operand_t* src = c->srcs->data[0];
- scf_register_t* rd = NULL;
+ scf_register_t* rd = NULL;
scf_rela_t* rela = NULL;
scf_x64_OpCode_t* lea;
scf_x64_OpCode_t* lea;
scf_instruction_t* inst;
- scf_register_t* rd = NULL;
+ scf_register_t* rd = NULL;
scf_rela_t* rela = NULL;
scf_variable_t* vd = dst->dag_node->var;
scf_x64_context_t* x64 = ctx->priv;
scf_function_t* f = x64->f;
scf_3ac_operand_t* src = NULL;
- scf_variable_t* v = NULL;
scf_instruction_t* inst = NULL;
+ scf_variable_t* v = NULL;
scf_rela_t* rela = NULL;
scf_register_t* rd = NULL;
scf_3ac_operand_t* count = c->srcs->data[2];
scf_instruction_t* inst = NULL;
- scf_register_t* rax = x64_find_register("rax");
- scf_register_t* rcx = x64_find_register("rcx");
- scf_register_t* rdi = x64_find_register("rdi");
- scf_register_t* rd;
+ scf_register_t* rax = x64_find_register("rax");
+ scf_register_t* rcx = x64_find_register("rcx");
+ scf_register_t* rdi = x64_find_register("rdi");
+ scf_register_t* rd;
scf_x64_OpCode_t* mov;
scf_x64_OpCode_t* stos;
if (!c->dsts || c->dsts->size != 1)
return -EINVAL;
- scf_register_t* r = NULL;
+ scf_register_t* r = NULL;
scf_x64_context_t* x64 = ctx->priv;
scf_function_t* f = x64->f;
if (!c->dsts || c->dsts->size != 1)
return -EINVAL;
- scf_register_t* r = NULL;
+ scf_register_t* r = NULL;
scf_x64_context_t* x64 = ctx->priv;
scf_function_t* f = x64->f;
scf_loge("c->srcs->size: %d\n", c->srcs->size);
assert(3 == c->srcs->size);
- scf_register_t* rbp = x64_find_register("rbp");
- scf_register_t* rptr = NULL;
- scf_register_t* rap = NULL;
+ scf_register_t* rbp = x64_find_register("rbp");
+ scf_register_t* rptr = NULL;
+ scf_register_t* rap = NULL;
scf_instruction_t* inst = NULL;
scf_3ac_operand_t* ap = c->srcs->data[0];
scf_3ac_operand_t* ptr = c->srcs->data[2];
assert(2 == c->srcs->size);
- scf_register_t* rbp = x64_find_register("rbp");
- scf_register_t* rptr = NULL;
- scf_register_t* rap = NULL;
+ scf_register_t* rbp = x64_find_register("rbp");
+ scf_register_t* rptr = NULL;
+ scf_register_t* rap = NULL;
scf_instruction_t* inst = NULL;
scf_3ac_operand_t* ap = c->srcs->data[0];
scf_3ac_operand_t* ptr = c->srcs->data[1];
static int _binary_assign_sib_float(scf_register_t* rb, scf_register_t* ri, int32_t scale, int32_t disp, 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* rs = NULL;
scf_rela_t* rela = NULL;
scf_x64_OpCode_t* OpCode;
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* rs = NULL;
- scf_register_t* rb = sib->base;
- scf_register_t* ri = sib->index;
+ 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* vb = base ->dag_node->var;
scf_variable_t* vi = index->dag_node->var;
- scf_register_t* rd = NULL;
+ scf_register_t* rd = NULL;
x64_sib_t sib = {0};
scf_x64_OpCode_t* lea;
inst->code[inst->len - 1] += r->id & 0x7;
if (SCF_X64_PUSH == OpCode->type)
- inst->src.base = (scf_register_t*)r;
+ inst->src.base = r;
else if (SCF_X64_POP == OpCode->type)
- inst->dst.base = (scf_register_t*)r;
+ inst->dst.base = r;
return inst;
}
p[i] = imm[i];
}
- inst->dst.base = (scf_register_t*)r_dst;
+ inst->dst.base = r_dst;
inst->src.imm_size = size;
return inst;
}
inst->code[inst->len++] = ModRM;
if (SCF_X64_INC == OpCode->type || SCF_X64_INC == OpCode->type) {
- inst->src.base = (scf_register_t*)r;
- inst->dst.base = (scf_register_t*)r;
+ inst->src.base = r;
+ inst->dst.base = r;
} 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 = (scf_register_t*)r;
+ inst->src.base = r;
return inst;
}
p[i] = imm[i];
}
- inst->dst.base = (scf_register_t*)r_dst;
+ inst->dst.base = r_dst;
inst->src.imm_size = size;
return inst;
}
if (SCF_X64_INC == OpCode->type || SCF_X64_INC == OpCode->type) {
- inst->src.base = (scf_register_t*)r_base;
+ inst->src.base = r_base;
inst->src.disp = offset;
inst->src.flag = 1;
- inst->dst.base = (scf_register_t*)r_base;
+ inst->dst.base = r_base;
inst->dst.disp = offset;
inst->dst.flag = 1;
|| SCF_X64_IDIV == OpCode->type
|| SCF_X64_CALL == OpCode->type) {
- inst->src.base = (scf_register_t*)r_base;
+ inst->src.base = r_base;
inst->src.disp = offset;
inst->src.flag = 1;
}
p[i] = imm[i];
}
- inst->dst.base = (scf_register_t*)r_base;
+ inst->dst.base = r_base;
inst->dst.disp = offset;
inst->dst.flag = 1;
return NULL;
}
- scf_register_t* rbp = x64_find_register("rbp");
+ scf_register_t* rbp = x64_find_register("rbp");
scf_instruction_t* inst = NULL;
uint32_t base;
return NULL;
}
- inst->src.base = (scf_register_t*)r_src;
- inst->dst.base = (scf_register_t*)r_base;
+ inst->src.base = r_src;
+ inst->dst.base = r_base;
inst->dst.disp = offset;
inst->dst.flag = 1;
return NULL;
}
- scf_register_t* rbp = x64_find_register("rbp");
+ scf_register_t* rbp = x64_find_register("rbp");
scf_instruction_t* inst = NULL;
uint32_t base;
return NULL;
}
- inst->dst.base = (scf_register_t*)r_dst;
- inst->src.base = (scf_register_t*)r_base;
+ inst->dst.base = r_dst;
+ inst->src.base = r_base;
inst->src.disp = offset;
inst->src.flag = 1;
return NULL;
}
- inst->dst.base = (scf_register_t*)r_dst;
- inst->src.base = (scf_register_t*)r_base;
+ inst->dst.base = r_dst;
+ inst->src.base = r_base;
inst->src.disp = offset;
inst->src.flag = 1;
return NULL;
}
- inst->src.base = (scf_register_t*)r_src;
- inst->dst.base = (scf_register_t*)r_base;
+ inst->src.base = r_src;
+ inst->dst.base = r_base;
inst->dst.disp = offset;
inst->dst.flag = 1;
inst->src.imm_size = size;
- inst->dst.base = (scf_register_t*)r_base;
+ inst->dst.base = r_base;
inst->dst.disp = offset;
inst->dst.flag = 1;
inst->code[inst->len++] = ModRM;
- inst->src.base = (scf_register_t*)r_src;
- inst->dst.base = (scf_register_t*)r_dst;
+ inst->src.base = r_src;
+ inst->dst.base = r_dst;
return inst;
}
inst->code[inst->len++] = ModRM;
- inst->src.base = (scf_register_t*)r_src;
- inst->dst.base = (scf_register_t*)r_dst;
+ inst->src.base = r_src;
+ inst->dst.base = r_dst;
return inst;
}
if (!inst)
return NULL;
- inst->dst.base = (scf_register_t*)r_dst;
+ inst->dst.base = r_dst;
- inst->src.base = (scf_register_t*)r_base;
- inst->src.index = (scf_register_t*)r_index;
+ inst->src.base = r_base;
+ inst->src.index = r_index;
inst->src.scale = scale;
inst->src.disp = disp;
inst->src.flag = 1;
if (!inst)
return NULL;
- inst->src.base = (scf_register_t*)r_src;
+ inst->src.base = r_src;
- inst->dst.base = (scf_register_t*)r_base;
- inst->dst.index = (scf_register_t*)r_index;
+ inst->dst.base = r_base;
+ inst->dst.index = r_index;
inst->dst.scale = scale;
inst->dst.disp = disp;
inst->dst.flag = 1;
inst->src.imm_size = size;
- inst->dst.base = (scf_register_t*)r_base;
- inst->dst.index = (scf_register_t*)r_index;
+ inst->dst.base = r_base;
+ inst->dst.index = r_index;
inst->dst.scale = scale;
inst->dst.disp = disp;
inst->dst.flag = 1;
scf_instruction_t* inst2;
scf_instruction_t* std;
+ scf_x64_OpCode_t* OpCode;
int j;
for (j = std_insts->size - 1; j >= 0; j--) {
if (inst->src.flag) {
assert(std->dst.flag);
- inst2 = x64_make_inst_E2G((scf_x64_OpCode_t*)inst->OpCode,
- (scf_register_t*)inst->dst.base,
- (scf_register_t*)std->src.base);
+ if (std->src.base)
+ inst2 = x64_make_inst_E2G((scf_x64_OpCode_t*)inst->OpCode, inst->dst.base, std->src.base);
+ else {
+ OpCode = x64_find_OpCode(inst->OpCode->type, std->src.imm_size, inst->dst.base->bytes, SCF_X64_I2G);
+
+ inst2 = x64_make_inst_I2G(OpCode, inst->dst.base, (uint8_t*)&std->src.imm, std->src.imm_size);
+ }
if (!inst2)
return -ENOMEM;
if (!inst->dst.index)
inst2 = x64_make_inst_G2P((scf_x64_OpCode_t*)inst->OpCode,
- (scf_register_t*)inst->dst.base, inst->dst.disp,
- (scf_register_t*)std->src.base);
+ inst->dst.base, inst->dst.disp,
+ std->src.base);
else
inst2 = x64_make_inst_G2SIB((scf_x64_OpCode_t*)inst->OpCode,
- (scf_register_t*)inst->dst.base,
- (scf_register_t*)inst->dst.index, inst->dst.scale, inst->dst.disp,
- (scf_register_t*)std->src.base);
+ inst->dst.base,
+ inst->dst.index, inst->dst.scale, inst->dst.disp,
+ std->src.base);
if (!inst2)
return -ENOMEM;
if (x64_inst_data_is_reg(&inst->dst)) {
- r0 = (scf_register_t*) std ->src.base;
- r1 = (scf_register_t*) inst->dst.base;
+ r0 = std ->src.base;
+ r1 = inst->dst.base;
if (X64_COLOR_CONFLICT(r0->color, r1->color))
assert(0 == scf_vector_del(std_insts, std));
else
goto check;
- inst2 = x64_make_inst_E2G((scf_x64_OpCode_t*) inst->OpCode,
- (scf_register_t*) inst->dst.base,
- (scf_register_t*) inst->src.base);
+ inst2 = x64_make_inst_E2G((scf_x64_OpCode_t*) inst->OpCode, inst->dst.base, inst->src.base);
if (!inst2)
return -ENOMEM;
if (inst->src.imm_size > 0)
inst2 = x64_make_inst_I2E((scf_x64_OpCode_t*)inst->OpCode,
- (scf_register_t*)inst->dst.base,
+ inst->dst.base,
(uint8_t*)&inst->src.imm, inst->src.imm_size);
else
inst2 = x64_make_inst_G2E((scf_x64_OpCode_t*)inst->OpCode,
- (scf_register_t*)inst->dst.base,
- (scf_register_t*)inst->src.base);
+ inst->dst.base,
+ inst->src.base);
if (!inst2)
return -ENOMEM;
if (x64_inst_data_is_reg(&inst->dst)) {
- r0 = (scf_register_t*)inst->dst.base;
+ r0 = inst->dst.base;
if (X64_COLOR_CONFLICT(rax->color, r0->color))
continue;
if (x64_inst_data_is_reg(&inst->dst)) {
- r0 = (scf_register_t*)inst ->dst.base;
- r1 = (scf_register_t*)inst2->src.base;
+ r0 = inst ->dst.base;
+ r1 = inst2->src.base;
if (SCF_X64_CALL == inst2->OpCode->type) {
if (scf_inst_data_same(&inst->dst, &inst2->src))
break;
- else if (rsp == (scf_register_t*)inst->dst.base)
+ else if (rsp == inst->dst.base)
break;
else if (SCF_OP_VA_START == inst->c->op->type
|| SCF_OP_VA_ARG == inst->c->op->type
static inline int x64_inst_data_is_reg(scf_inst_data_t* id)
{
- scf_register_t* rsp = (scf_register_t*)x64_find_register("rsp");
- scf_register_t* rbp = (scf_register_t*)x64_find_register("rbp");
+ 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;
static inline int x64_inst_data_is_local(scf_inst_data_t* id)
{
- scf_register_t* rbp = (scf_register_t*)x64_find_register("rbp");
- scf_register_t* rsp = (scf_register_t*)x64_find_register("rsp");
+ 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;