Skip to content

Commit dd9c3aa

Browse files
authored
Merge pull request #269 from sysprog21/improve-regalloc
Introduce Virtual Register abstraction to improve register allocation
2 parents 1611d23 + 26fd44c commit dd9c3aa

File tree

5 files changed

+200
-58
lines changed

5 files changed

+200
-58
lines changed

src/arm-codegen.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,18 @@ void code_generate(void)
485485
emit_ph2_ir(ph2_ir);
486486

487487
/* prepare 'argc' and 'argv', then proceed to 'main' function */
488-
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
489-
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
490-
emit(__add_r(__AL, __r8, __r12, __r8));
491-
emit(__lw(__AL, __r0, __r8, 0));
492-
emit(__add_i(__AL, __r1, __r8, 4));
493-
emit(__bl(__AL, MAIN_BB->elf_offset - elf_code->size));
488+
if (MAIN_BB) {
489+
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
490+
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
491+
emit(__add_r(__AL, __r8, __r12, __r8));
492+
emit(__lw(__AL, __r0, __r8, 0));
493+
emit(__add_i(__AL, __r1, __r8, 4));
494+
emit(__bl(__AL, MAIN_BB->elf_offset - elf_code->size));
494495

495-
/* exit with main's return value - r0 already has the return value */
496-
emit(__mov_i(__AL, __r7, 1));
497-
emit(__svc());
496+
/* exit with main's return value - r0 already has the return value */
497+
emit(__mov_i(__AL, __r7, 1));
498+
emit(__svc());
499+
}
498500

499501
for (int i = 0; i < ph2_ir_idx; i++) {
500502
ph2_ir = PH2_IR_FLATTEN[i];

src/defs.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,14 @@ struct var {
371371
int consumed;
372372
bool is_ternary_ret;
373373
bool is_logical_ret;
374-
bool is_const; /* whether a constant representaion or not */
374+
bool is_const; /* whether a constant representaion or not */
375+
int vreg_id; /* Virtual register ID */
376+
int phys_reg; /* Physical register assignment (-1 if unassigned) */
377+
int vreg_flags; /* VReg flags */
378+
int first_use; /* First instruction index where variable is used */
379+
int last_use; /* Last instruction index where variable is used */
380+
int loop_depth; /* Nesting depth if variable is in a loop */
381+
int use_count; /* Number of times variable is used */
375382
};
376383

377384
typedef struct {

src/parser.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ var_t *require_var(block_t *blk)
6363
var_t *var = arena_calloc(BLOCK_ARENA, 1, sizeof(var_t));
6464
var_list->elements[var_list->size++] = var;
6565
var->consumed = -1;
66+
var->phys_reg = -1;
67+
var->first_use = -1;
68+
var->last_use = -1;
69+
var->loop_depth = 0;
70+
var->use_count = 0;
6671
var->base = var;
6772
var->type = TY_int;
6873
return var;

0 commit comments

Comments
 (0)