Skip to content

Commit

Permalink
Refine comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vacantron committed Nov 21, 2023
1 parent e771c22 commit edde5a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/arm-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ void emit(int code)
void cfg_flatten()
{
func_t *func = find_func("__syscall");
func->fn->bbs->elf_offset = 44;
func->fn->bbs->elf_offset = 44; /* offset of start + exit in codegen */

elf_offset = 80;
elf_offset = 80; /* offset of start + exit + syscall in codegen */
GLOBAL_FUNC.fn->bbs->elf_offset = elf_offset;

ph2_ir_t *ph2_ir;
Expand All @@ -37,6 +37,7 @@ void cfg_flatten()
for (fn = FUNC_LIST.head; fn; fn = fn->next) {
ph2_ir_t *flatten_ir;

/* reserve stack */
flatten_ir = add_ph2_ir(OP_define);
flatten_ir->src0 = fn->func->stack_size;

Expand Down Expand Up @@ -171,19 +172,22 @@ void code_generate()
{
int elf_data_start = elf_code_start + elf_offset;

/* start */
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__sub_r(__AL, __sp, __sp, __r8));
emit(__mov_r(__AL, __r12, __sp));
emit(__bl(__AL, GLOBAL_FUNC.fn->bbs->elf_offset - elf_code_idx));

/* exit */
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__add_r(__AL, __sp, __sp, __r8));
emit(__mov_r(__AL, __r0, __r0));
emit(__mov_i(__AL, __r7, 1));
emit(__svc());

/* syscall */
emit(__mov_r(__AL, __r7, __r0));
emit(__mov_r(__AL, __r0, __r1));
emit(__mov_r(__AL, __r1, __r2));
Expand Down Expand Up @@ -219,6 +223,7 @@ void code_generate()
abort();
}
}
/* jump to main */
emit(__b(__AL, MAIN_BB->elf_offset - elf_code_idx));

int i;
Expand Down
12 changes: 12 additions & 0 deletions src/cfront.c
Original file line number Diff line number Diff line change
Expand Up @@ -2622,6 +2622,7 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
lex_expect(T_colon);

if (is_default)
/* there's no condition if it is a default label */
bb_connect(bb, true_body_, NEXT);
else
bb_connect(bb, true_body_, THEN);
Expand All @@ -2644,7 +2645,13 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
true_body_ = read_body_statement(parent, true_body_);
control = 1;
}

if (control && true_body_) {
/**
* Create a new body block for next case, and connect the last
* body block which lacks `break` to it to make that one ignore
* the upcoming cases.
*/
basic_block_t *n = bb_create(parent);
bb_connect(true_body_, n, NEXT);
true_body_ = n;
Expand All @@ -2659,9 +2666,13 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
if (is_default)
error("Label default should be the last one");

/* create a new conditional block for next case */
basic_block_t *n = bb_create(parent);
bb_connect(bb, n, ELSE);
bb = n;

/* create a new body block for next case if the last body block
* exits `switch` */
if (!true_body_)
true_body_ = bb_create(parent);
} else if (!is_default) {
Expand All @@ -2678,6 +2689,7 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
lex_expect(T_close_curly);

if (true_body_)
/* if the last label has no explicit break, connect it to the end */
bb_connect(true_body_, switch_end, NEXT);

strcpy(var_break->var_name, vd->var_name);
Expand Down
11 changes: 7 additions & 4 deletions src/riscv-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ void emit(int code)
void cfg_flatten()
{
func_t *func = find_func("__syscall");
func->fn->bbs->elf_offset = 32;
func->fn->bbs->elf_offset = 32; /* offset of start + exit in codegen */

elf_offset = 68;
elf_offset = 68; /* offset of start + exit + syscall in codegen */
GLOBAL_FUNC.fn->bbs->elf_offset = elf_offset;

ph2_ir_t *ph2_ir;
Expand All @@ -37,6 +37,7 @@ void cfg_flatten()
for (fn = FUNC_LIST.head; fn; fn = fn->next) {
ph2_ir_t *flatten_ir;

/* reserve stack */
flatten_ir = add_ph2_ir(OP_define);
flatten_ir->src0 = fn->func->stack_size;

Expand Down Expand Up @@ -139,18 +140,19 @@ void code_generate()
{
int elf_data_start = elf_code_start + elf_offset;

/* start */
emit(__addi(__sp, __sp, -GLOBAL_FUNC.stack_size));
emit(__addi(__gp, __sp, 0));

emit(__jal(__ra, GLOBAL_FUNC.fn->bbs->elf_offset - elf_code_idx));

/* exit */
emit(__addi(__gp, __gp, GLOBAL_FUNC.stack_size));
emit(__addi(__sp, __gp, 0));

emit(__addi(__a0, __a0, 0));
emit(__addi(__a7, __zero, 93));
emit(__ecall());

/* syscall */
emit(__addi(__a7, __a0, 0));
emit(__addi(__a0, __a1, 0));
emit(__addi(__a1, __a2, 0));
Expand Down Expand Up @@ -186,6 +188,7 @@ void code_generate()
abort();
}
}
/* jump to main */
emit(__jal(__zero, MAIN_BB->elf_offset - elf_code_idx));

int i;
Expand Down

0 comments on commit edde5a8

Please sign in to comment.