Skip to content

Commit 56d9aa3

Browse files
committed
Fix ARM backend exit code handling
Programs were returning exit code 1 instead of actual return values due to incorrect use of BLX instruction instead of BX for function returns. The issue affected both regular function returns and syscall returns: - OP_return was using blx (branch with link exchange) which incorrectly modified the link register, corrupting return values - __syscall was using direct PC manipulation instead of proper BX instruction Add const qualifiers to read-only function param This improves const-correctness by adding const qualifiers to function parameters that are never modified.
1 parent 33e32e0 commit 56d9aa3

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

src/arm-codegen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
117117
elf_offset += 8;
118118
return;
119119
case OP_return:
120-
elf_offset += 24;
120+
elf_offset += 24; /* mov to r0, stack adjustment, lr load, and return */
121121
return;
122122
case OP_trunc:
123123
elf_offset += 4;
@@ -311,7 +311,7 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
311311
emit(__movt(__AL, __r8, ph2_ir->src1 + 4));
312312
emit(__add_r(__AL, __sp, __sp, __r8));
313313
emit(__lw(__AL, __lr, __sp, -4));
314-
emit(__blx(__AL, __lr));
314+
emit(__bx(__AL, __lr));
315315
return;
316316
case OP_add:
317317
emit(__add_r(__AL, rd, rn, rm));
@@ -480,7 +480,7 @@ void code_generate(void)
480480
emit(__mov_r(__AL, __r4, __r5));
481481
emit(__mov_r(__AL, __r5, __r6));
482482
emit(__svc());
483-
emit(__mov_r(__AL, __pc, __lr));
483+
emit(__bx(__AL, __lr));
484484

485485
ph2_ir_t *ph2_ir;
486486
for (ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;

src/arm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ int __bl(arm_cond_t cond, int ofs)
324324
return arm_encode(cond, 176, 0, 0, 0) + (o & 16777215);
325325
}
326326

327+
int __bx(arm_cond_t cond, arm_reg rd)
328+
{
329+
return arm_encode(cond, 18, 15, 15, rd + 3872);
330+
}
331+
327332
int __blx(arm_cond_t cond, arm_reg rd)
328333
{
329334
return arm_encode(cond, 18, 15, 15, rd + 3888);

src/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ int hex_digit_value(char c)
186186
return -1;
187187
}
188188

189-
bool is_numeric(char buffer[])
189+
bool is_numeric(const char buffer[])
190190
{
191191
bool hex = false;
192192
int size = strlen(buffer);

src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ var_t *resize_var(block_t *block, basic_block_t **bb, var_t *from, var_t *to)
308308
return from;
309309
}
310310

311-
int read_numeric_constant(char buffer[])
311+
int read_numeric_constant(const char buffer[])
312312
{
313313
int i = 0;
314314
int value = 0;

src/ssa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ void bb_dump(FILE *fd, func_t *func, basic_block_t *bb)
11621162
bb_dump_connection(fd, bb->prev[i].bb, bb, bb->prev[i].type);
11631163
}
11641164

1165-
void dump_cfg(char name[])
1165+
void dump_cfg(const char name[])
11661166
{
11671167
FILE *fd = fopen(name, "w");
11681168

@@ -1190,7 +1190,7 @@ void dom_dump(FILE *fd, basic_block_t *bb)
11901190
}
11911191
}
11921192

1193-
void dump_dom(char name[])
1193+
void dump_dom(const char name[])
11941194
{
11951195
FILE *fd = fopen(name, "w");
11961196

0 commit comments

Comments
 (0)