Skip to content

Commit 5e3a6f8

Browse files
committed
Fix ARM backend control flow for global init
Apply control flow fix from dev-compound-literal branch to ensure proper execution order after global variable initialization. - Adjust syscall trampoline offset to 48 (was 44) - Adjust global init offset to 84 (was 80) - Add branch instruction after global init to skip early exit code - Clarify that r0 already contains main's return value at exit
1 parent c684f74 commit 5e3a6f8

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/arm-codegen.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
136136
void cfg_flatten(void)
137137
{
138138
func_t *func = find_func("__syscall");
139-
func->bbs->elf_offset = 44; /* offset of start + exit in codegen */
139+
func->bbs->elf_offset = 48; /* offset of start + branch + exit in codegen */
140140

141-
elf_offset = 80; /* offset of start + exit + syscall in codegen */
141+
elf_offset = 84; /* offset of start + branch + exit + syscall in codegen */
142142
GLOBAL_FUNC->bbs->elf_offset = elf_offset;
143143

144144
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
@@ -457,6 +457,8 @@ void code_generate(void)
457457
emit(__sub_r(__AL, __sp, __sp, __r8));
458458
emit(__mov_r(__AL, __r12, __sp));
459459
emit(__bl(__AL, GLOBAL_FUNC->bbs->elf_offset - elf_code->size));
460+
/* After global init, jump to main preparation */
461+
emit(__b(__AL, 56)); /* PC+8: skip exit (24) + syscall (36) + ret (4) - 8 */
460462

461463
/* exit */
462464
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
@@ -490,7 +492,7 @@ void code_generate(void)
490492
emit(__add_i(__AL, __r1, __r8, 4));
491493
emit(__bl(__AL, MAIN_BB->elf_offset - elf_code->size));
492494

493-
/* exit with main's return value */
495+
/* exit with main's return value - r0 already has the return value */
494496
emit(__mov_i(__AL, __r7, 1));
495497
emit(__svc());
496498

0 commit comments

Comments
 (0)