Skip to content

Commit 1361cd1

Browse files
committed
Fix exit code issues for supported backends
This resolves critical ARM/RISC-V backend issues: - Fix stage2 segmentation fault during bootstrap compilation - Fix exit codes always returning 1 instead of main's return value - Reorganize code generation to place __syscall trampoline at correct offset
1 parent 4debd6d commit 1361cd1

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/arm-codegen.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void cfg_flatten(void)
147147
}
148148

149149
/* prepare 'argc' and 'argv', then proceed to 'main' function */
150-
elf_offset += 24;
150+
elf_offset += 32; /* 6 insns for main call + 2 for exit */
151151

152152
for (func = FUNC_LIST.head; func; func = func->next) {
153153
/* reserve stack */
@@ -488,7 +488,11 @@ void code_generate(void)
488488
emit(__add_r(__AL, __r8, __r12, __r8));
489489
emit(__lw(__AL, __r0, __r8, 0));
490490
emit(__add_i(__AL, __r1, __r8, 4));
491-
emit(__b(__AL, MAIN_BB->elf_offset - elf_code->size));
491+
emit(__bl(__AL, MAIN_BB->elf_offset - elf_code->size));
492+
493+
/* exit with main's return value */
494+
emit(__mov_i(__AL, __r7, 1));
495+
emit(__svc());
492496

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

src/riscv-codegen.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
110110
void cfg_flatten(void)
111111
{
112112
func_t *func = find_func("__syscall");
113-
func->bbs->elf_offset = 48; /* offset of start + exit in codegen */
113+
/* Prologue ~ 6 instructions (24 bytes). Place __syscall right after. */
114+
func->bbs->elf_offset = 24;
114115

115-
elf_offset = 84; /* offset of start + exit + syscall in codegen */
116+
/* Reserve space for prologue (24) + syscall trampoline (36) = 60 bytes. */
117+
elf_offset = 60;
116118
GLOBAL_FUNC->bbs->elf_offset = elf_offset;
117119

118120
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
@@ -437,24 +439,17 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
437439
void code_generate(void)
438440
{
439441
elf_data_start = elf_code_start + elf_offset;
442+
func_t *func;
440443

441-
/* start */
444+
/* start: save original sp in s0; allocate global stack; run init */
445+
emit(__addi(__s0, __sp, 0));
442446
emit(__lui(__t0, rv_hi(GLOBAL_FUNC->stack_size)));
443447
emit(__addi(__t0, __t0, rv_lo(GLOBAL_FUNC->stack_size)));
444448
emit(__sub(__sp, __sp, __t0));
445-
emit(__addi(__gp, __sp, 0));
449+
emit(__addi(__gp, __sp, 0)); /* Set up global pointer */
446450
emit(__jal(__ra, GLOBAL_FUNC->bbs->elf_offset - elf_code->size));
447451

448-
/* exit */
449-
emit(__lui(__t0, rv_hi(GLOBAL_FUNC->stack_size)));
450-
emit(__addi(__t0, __t0, rv_lo(GLOBAL_FUNC->stack_size)));
451-
emit(__add(__gp, __gp, __t0));
452-
emit(__addi(__sp, __gp, 0));
453-
emit(__addi(__a0, __a0, 0));
454-
emit(__addi(__a7, __zero, 93));
455-
emit(__ecall());
456-
457-
/* syscall */
452+
/* syscall trampoline for __syscall - must be at offset 24 */
458453
emit(__addi(__a7, __a0, 0));
459454
emit(__addi(__a0, __a1, 0));
460455
emit(__addi(__a1, __a2, 0));
@@ -471,12 +466,15 @@ void code_generate(void)
471466
emit_ph2_ir(ph2_ir);
472467

473468
/* prepare 'argc' and 'argv', then proceed to 'main' function */
474-
emit(__lui(__t0, rv_hi(GLOBAL_FUNC->stack_size)));
475-
emit(__addi(__t0, __t0, rv_lo(GLOBAL_FUNC->stack_size)));
476-
emit(__add(__t0, __gp, __t0));
469+
/* use original sp saved in s0 to get argc/argv */
470+
emit(__addi(__t0, __s0, 0));
477471
emit(__lw(__a0, __t0, 0));
478472
emit(__addi(__a1, __t0, 4));
479-
emit(__jal(__zero, MAIN_BB->elf_offset - elf_code->size));
473+
emit(__jal(__ra, MAIN_BB->elf_offset - elf_code->size));
474+
475+
/* exit with main's return value in a0 */
476+
emit(__addi(__a7, __zero, 93));
477+
emit(__ecall());
480478

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

0 commit comments

Comments
 (0)