Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
- moved code around
- removed dead code
- fixed potential IR_COPY bug for x86
  • Loading branch information
realchonk committed Jul 10, 2021
1 parent 6ddf1d5 commit cb2c746
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 93 deletions.
5 changes: 5 additions & 0 deletions include/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,9 @@ enum ir_value_size vt2irs(const struct value_type*);
bool ir_is(ir_node_t*, enum ir_node_type);
bool ir_isv(ir_node_t*, ...); // must be terminated with NUM_IR_NODES

#define IRR_NONSENSE ((ir_reg_t)-1)
ir_reg_t ir_get_target(const ir_node_t*);
bool ir_is_source(const ir_node_t*, ir_reg_t);
bool ir_is_binary(const enum ir_node_type);

#endif /* FILE_IR_H */
1 change: 0 additions & 1 deletion src/i386/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ static void emit_begin(void) {
if (defined) buf_free(defined);
emit("default rel");
emit("section .text");
emit("extern memcpy");
for (size_t i = 0; i < buf_len(cunit.vars); ++i) {
const struct variable* v = &cunit.vars[i];
if (!(v->attrs & ATTR_EXTERN))
Expand Down
1 change: 1 addition & 0 deletions src/i386/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ static ir_node_t* emit_ir(const ir_node_t* n) {
emit("call memcpy");
emit("add esp, %zu", 12 + align);
#endif
add_unresolved(strint("memcpy"));
return n->next;
}
case IR_FLOOKUP:
Expand Down
85 changes: 85 additions & 0 deletions src/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,88 @@ bool ir_isv(ir_node_t* n, ...) {
return success;
}

ir_reg_t get_target(const ir_node_t* n) {
if (ir_is_binary(n->type))
return n->binary.dest;

switch (n->type) {
case IR_MOVE:
case IR_READ:
return n->move.dest;
case IR_LOAD:
return n->load.dest;
case IR_IICAST:
return n->iicast.dest;
case IR_IFCALL:
return n->ifcall.dest;
case IR_IRCALL:
return n->rcall.dest;
case IR_LSTR:
case IR_FLOOKUP:
return n->lstr.reg;
case IR_LOOKUP:
case IR_ARRAYLEN:
return n->lookup.reg;
case IR_FPARAM:
return n->fparam.reg;
default:
return IRR_NONSENSE;
}
}
bool ir_is_source(const ir_node_t* n, ir_reg_t r) {
if (ir_is_binary(n->type))
return (n->binary.a.type == IRT_REG && n->binary.a.reg == r)
|| (n->binary.b.type == IRT_REG && n->binary.b.reg == r);
switch (n->type) {
case IR_READ:
case IR_MOVE: return n->move.src == r;
case IR_WRITE: return n->move.dest == r;
case IR_INEG:
case IR_INOT:
case IR_BNOT:
case IR_IRET:
return n->unary.reg == r;
case IR_IICAST:
return n->iicast.src == r;
case IR_ALLOCA:
return n->alloca.size.type == IRT_REG && n->alloca.size.reg == r;
case IR_COPY:
return n->copy.src == r || n->copy.dest == r;
default:
return false;
}
}
bool ir_is_binary(const enum ir_node_type t) {
switch (t) {
case IR_IADD:
case IR_ISUB:
case IR_IAND:
case IR_IOR:
case IR_IXOR:
case IR_ILSL:
case IR_ILSR:
case IR_IASR:
case IR_IMUL:
case IR_IDIV:
case IR_IMOD:
case IR_UMUL:
case IR_UDIV:
case IR_UMOD:
case IR_INEG:
case IR_INOT:
case IR_BNOT:
case IR_ISTEQ:
case IR_ISTNE:
case IR_ISTGR:
case IR_ISTGE:
case IR_ISTLT:
case IR_ISTLE:
case IR_USTGR:
case IR_USTGE:
case IR_USTLT:
case IR_USTLE:
return true;
default:
return false;
}
}
95 changes: 4 additions & 91 deletions src/optim_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,100 +6,13 @@

// utility functions

#define IRR_NONSENSE ((ir_reg_t)-1)

static ir_reg_t get_target(const ir_node_t* n) {
switch (n->type) {
case IR_MOVE:
case IR_READ:
return n->move.dest;
case IR_IADD:
case IR_ISUB:
case IR_IAND:
case IR_IOR:
case IR_IXOR:
case IR_ILSL:
case IR_ILSR:
case IR_IASR:
case IR_IMUL:
case IR_IDIV:
case IR_IMOD:
case IR_UMUL:
case IR_UDIV:
case IR_UMOD:
case IR_INEG:
case IR_INOT:
case IR_BNOT:
case IR_ISTEQ:
case IR_ISTNE:
case IR_ISTGR:
case IR_ISTGE:
case IR_ISTLT:
case IR_ISTLE:
case IR_USTGR:
case IR_USTGE:
case IR_USTLT:
case IR_USTLE:
return n->binary.dest;
case IR_IICAST:
return n->iicast.dest;
case IR_IFCALL:
return n->ifcall.dest;
case IR_IRCALL:
return n->rcall.dest;
case IR_LSTR:
return n->lstr.reg;
default:
return IRR_NONSENSE;
}
}
static bool reg_is_referenced(const ir_node_t* n, ir_reg_t reg) {
while (n) {
switch (n->type) {
case IR_MOVE:
case IR_READ:
if (n->move.src == reg) return true;
else if (n->move.dest == reg) return false;
break;
case IR_LOAD:
if (n->load.dest == reg) return true;
break;
case IR_IADD:
case IR_ISUB:
case IR_IAND:
case IR_IOR:
case IR_IXOR:
case IR_ILSL:
case IR_ILSR:
case IR_IASR:
case IR_IMUL:
case IR_IDIV:
case IR_IMOD:
case IR_UMUL:
case IR_UDIV:
case IR_UMOD:
case IR_ISTEQ:
case IR_ISTNE:
case IR_ISTGR:
case IR_ISTGE:
case IR_ISTLT:
case IR_ISTLE:
case IR_USTGR:
case IR_USTGE:
case IR_USTLT:
case IR_USTLE:
if ((n->binary.a.type == IRT_REG && n->binary.a.reg == reg)
|| (n->binary.b.type == IRT_REG && n->binary.b.reg == reg))
return true;
else if (n->binary.dest == reg)
return false;
break;
default:
break;
}
n = n->next;
if (ir_is_source(n, reg)) return true;
else if (ir_get_target(n) == reg) return false;
else n = n->next;
}
return true; // TODO: change this later
return false;
}

// remove NOPs
Expand Down
1 change: 0 additions & 1 deletion src/vtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,6 @@ struct value_type* get_value_type_impl(struct scope* scope, struct expression* e
}
panic("reached unreachable");
}
return common_value_type(get_value_type(scope, e->binary.left), get_value_type(scope, e->binary.right), true); // TODO
case EXPR_TERNARY:
return common_value_type(get_value_type(scope, e->ternary.true_case), get_value_type(scope, e->ternary.false_case), true);
case EXPR_PREFIX:
Expand Down

0 comments on commit cb2c746

Please sign in to comment.