Skip to content

Commit

Permalink
Rename registers to prepare for the three-address form.
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Aug 30, 2018
1 parent 742be85 commit 3235d3f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 70 deletions.
5 changes: 3 additions & 2 deletions 9cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ typedef struct BB {

typedef struct {
int op;
int lhs;
int rhs;

int r0;
int r2;

int imm;
int imm2;
Expand Down
12 changes: 6 additions & 6 deletions gen_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ static IR *new_ir(int op) {
return ir;
}

static IR *emit(int op, int lhs, int rhs) {
static IR *emit(int op, int r0, int r2) {
IR *ir = new_ir(op);
ir->lhs = lhs;
ir->rhs = rhs;
ir->r0 = r0;
ir->r2 = r2;
return ir;
}

static IR *br(int r, BB *then, BB *els) {
IR *ir = new_ir(IR_BR);
ir->lhs = r;
ir->r0 = r;
ir->bb1 = then;
ir->bb2 = els;
return ir;
Expand All @@ -58,7 +58,7 @@ static void jmp(BB *bb) {

static void imm(int r, int imm) {
IR *ir = new_ir(IR_IMM);
ir->lhs = r;
ir->r0 = r;
ir->imm = imm;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ static int gen_lval(Node *node) {
int r = nreg++;
if (var->is_local) {
IR *ir = new_ir(IR_BPREL);
ir->lhs = r;
ir->r0 = r;
ir->imm = var->offset;
} else {
IR *ir = emit(IR_LABEL_ADDR, r, -1);
Expand Down
66 changes: 33 additions & 33 deletions gen_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ static void emit(char *fmt, ...) {
}

static void emit_cmp(char *insn, IR *ir) {
emit("cmp %s, %s", regs[ir->lhs], regs[ir->rhs]);
emit("%s %s", insn, regs8[ir->lhs]);
emit("movzb %s, %s", regs[ir->lhs], regs8[ir->lhs]);
emit("cmp %s, %s", regs[ir->r0], regs[ir->r2]);
emit("%s %s", insn, regs8[ir->r0]);
emit("movzb %s, %s", regs[ir->r0], regs8[ir->r0]);
}

static char *reg(int r, int size) {
Expand All @@ -55,21 +55,21 @@ static char *argreg(int r, int size) {
}

static void emit_ir(IR *ir, char *ret) {
int lhs = ir->lhs;
int rhs = ir->rhs;
int r0 = ir->r0;
int r2 = ir->r2;

switch (ir->op) {
case IR_IMM:
emit("mov %s, %d", regs[lhs], ir->imm);
emit("mov %s, %d", regs[r0], ir->imm);
break;
case IR_BPREL:
emit("lea %s, [rbp%d]", regs[lhs], ir->imm);
emit("lea %s, [rbp%d]", regs[r0], ir->imm);
break;
case IR_MOV:
emit("mov %s, %s", regs[lhs], regs[rhs]);
emit("mov %s, %s", regs[r0], regs[r2]);
break;
case IR_RETURN:
emit("mov rax, %s", regs[lhs]);
emit("mov rax, %s", regs[r0]);
emit("jmp %s", ret);
break;
case IR_CALL:
Expand All @@ -82,10 +82,10 @@ static void emit_ir(IR *ir, char *ret) {
emit("call %s", ir->name);
emit("pop r11");
emit("pop r10");
emit("mov %s, rax", regs[lhs]);
emit("mov %s, rax", regs[r0]);
break;
case IR_LABEL_ADDR:
emit("lea %s, %s", regs[lhs], ir->name);
emit("lea %s, %s", regs[r0], ir->name);
break;
case IR_EQ:
emit_cmp("sete", ir);
Expand All @@ -100,63 +100,63 @@ static void emit_ir(IR *ir, char *ret) {
emit_cmp("setle", ir);
break;
case IR_AND:
emit("and %s, %s", regs[lhs], regs[rhs]);
emit("and %s, %s", regs[r0], regs[r2]);
break;
case IR_OR:
emit("or %s, %s", regs[lhs], regs[rhs]);
emit("or %s, %s", regs[r0], regs[r2]);
break;
case IR_XOR:
emit("xor %s, %s", regs[lhs], regs[rhs]);
emit("xor %s, %s", regs[r0], regs[r2]);
break;
case IR_SHL:
emit("mov cl, %s", regs8[rhs]);
emit("shl %s, cl", regs[lhs]);
emit("mov cl, %s", regs8[r2]);
emit("shl %s, cl", regs[r0]);
break;
case IR_SHR:
emit("mov cl, %s", regs8[rhs]);
emit("shr %s, cl", regs[lhs]);
emit("mov cl, %s", regs8[r2]);
emit("shr %s, cl", regs[r0]);
break;
case IR_JMP:
emit("jmp .L%d", ir->bb1->label);
break;
case IR_BR:
emit("cmp %s, 0", regs[lhs]);
emit("cmp %s, 0", regs[r0]);
emit("jne .L%d", ir->bb1->label);
emit("jmp .L%d", ir->bb2->label);
break;
case IR_LOAD:
emit("mov %s, [%s]", reg(lhs, ir->size), regs[rhs]);
emit("mov %s, [%s]", reg(r0, ir->size), regs[r2]);
if (ir->size == 1)
emit("movzb %s, %s", regs[lhs], regs8[lhs]);
emit("movzb %s, %s", regs[r0], regs8[r0]);
break;
case IR_STORE:
emit("mov [%s], %s", regs[lhs], reg(rhs, ir->size));
emit("mov [%s], %s", regs[r0], reg(r2, ir->size));
break;
case IR_STORE_ARG:
emit("mov [rbp%d], %s", ir->imm, argreg(ir->imm2, ir->size));
break;
case IR_ADD:
emit("add %s, %s", regs[lhs], regs[rhs]);
emit("add %s, %s", regs[r0], regs[r2]);
break;
case IR_SUB:
emit("sub %s, %s", regs[lhs], regs[rhs]);
emit("sub %s, %s", regs[r0], regs[r2]);
break;
case IR_MUL:
emit("mov rax, %s", regs[rhs]);
emit("imul %s", regs[lhs]);
emit("mov %s, rax", regs[lhs]);
emit("mov rax, %s", regs[r2]);
emit("imul %s", regs[r0]);
emit("mov %s, rax", regs[r0]);
break;
case IR_DIV:
emit("mov rax, %s", regs[lhs]);
emit("mov rax, %s", regs[r0]);
emit("cqo");
emit("idiv %s", regs[rhs]);
emit("mov %s, rax", regs[lhs]);
emit("idiv %s", regs[r2]);
emit("mov %s, rax", regs[r0]);
break;
case IR_MOD:
emit("mov rax, %s", regs[lhs]);
emit("mov rax, %s", regs[r0]);
emit("cqo");
emit("idiv %s", regs[rhs]);
emit("mov %s, rdx", regs[lhs]);
emit("idiv %s", regs[r2]);
emit("mov %s, rdx", regs[r0]);
break;
case IR_NOP:
break;
Expand Down
50 changes: 25 additions & 25 deletions irdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

static char *tostr_call(IR *ir) {
StringBuilder *sb = new_sb();
sb_append(sb, format("r%d = %s(", ir->lhs, ir->name));
sb_append(sb, format("r%d = %s(", ir->r0, ir->name));
for (int i = 0; i < ir->nargs; i++) {
if (i != 0)
sb_append(sb, ", ");
Expand All @@ -13,62 +13,62 @@ static char *tostr_call(IR *ir) {
}

static char *tostr(IR *ir) {
int lhs = ir->lhs;
int rhs = ir->rhs;
int r0 = ir->r0;
int r2 = ir->r2;

switch (ir->op) {
case IR_ADD:
return format("ADD r%d, r%d", lhs, rhs);
return format("ADD r%d, r%d", r0, r2);
case IR_CALL:
return tostr_call(ir);
case IR_DIV:
return format("DIV r%d, r%d", lhs, rhs);
return format("DIV r%d, r%d", r0, r2);
case IR_IMM:
return format("r%d = %d", lhs, ir->imm);
return format("r%d = %d", r0, ir->imm);
case IR_JMP:
return format("JMP .L%d", ir->bb1->label);
case IR_LABEL_ADDR:
return format("r%d = .L%d", lhs, ir->label);
return format("r%d = .L%d", r0, ir->label);
case IR_EQ:
return format("EQ r%d, r%d", lhs, rhs);
return format("EQ r%d, r%d", r0, r2);
case IR_NE:
return format("NE r%d, r%d", lhs, rhs);
return format("NE r%d, r%d", r0, r2);
case IR_LE:
return format("LE r%d, r%d", lhs, rhs);
return format("LE r%d, r%d", r0, r2);
case IR_LT:
return format("LT r%d, r%d", lhs, rhs);
return format("LT r%d, r%d", r0, r2);
case IR_AND:
return format("AND r%d, r%d", lhs, rhs);
return format("AND r%d, r%d", r0, r2);
case IR_OR:
return format("OR r%d, r%d", lhs, rhs);
return format("OR r%d, r%d", r0, r2);
case IR_XOR:
return format("XOR r%d, r%d", lhs, rhs);
return format("XOR r%d, r%d", r0, r2);
case IR_SHL:
return format("SHL r%d, r%d", lhs, rhs);
return format("SHL r%d, r%d", r0, r2);
case IR_SHR:
return format("SHR r%d, r%d", lhs, rhs);
return format("SHR r%d, r%d", r0, r2);
case IR_LOAD:
return format("LOAD%d r%d, r%d", ir->size, lhs, rhs);
return format("LOAD%d r%d, r%d", ir->size, r0, r2);
case IR_MOD:
return format("MOD r%d, r%d", lhs, rhs);
return format("MOD r%d, r%d", r0, r2);
case IR_MOV:
return format("MOV r%d, r%d", lhs, rhs);
return format("MOV r%d, r%d", r0, r2);
case IR_MUL:
return format("MUL r%d, r%d", lhs, rhs);
return format("MUL r%d, r%d", r0, r2);
case IR_NOP:
return "NOP";
case IR_RETURN:
return format("RET r%d", lhs);
return format("RET r%d", r0);
case IR_STORE:
return format("STORE%d r%d, r%d", ir->size, lhs, rhs);
return format("STORE%d r%d, r%d", ir->size, r0, r2);
case IR_STORE_ARG:
return format("STORE_ARG%d %d, %d", ir->size, ir->imm, ir->imm2);
case IR_SUB:
return format("SUB r%d, r%d", lhs, rhs);
return format("SUB r%d, r%d", r0, r2);
case IR_BPREL:
return format("BPREL r%d, %d", lhs, ir->imm);
return format("BPREL r%d, %d", r0, ir->imm);
case IR_BR:
return format("BR r%d, .L%d, L%d", lhs, ir->bb1->label, ir->bb2->label);
return format("BR r%d, .L%d, L%d", r0, ir->bb1->label, ir->bb2->label);
default:
assert(0 && "unknown op");
}
Expand Down
8 changes: 4 additions & 4 deletions regalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ static void kill(int r) {
}

static void visit(IR *ir) {
if (ir->lhs)
ir->lhs = alloc(ir->lhs);
if (ir->r0)
ir->r0 = alloc(ir->r0);

if (ir->rhs)
ir->rhs = alloc(ir->rhs);
if (ir->r2)
ir->r2 = alloc(ir->r2);

if (ir->op == IR_CALL)
for (int i = 0; i < ir->nargs; i++)
Expand Down

0 comments on commit 3235d3f

Please sign in to comment.