Skip to content

Refine generated instructions for staticly linked programs of Arm32#321

Merged
jserv merged 1 commit intosysprog21:masterfrom
DrXiao:fix-codegen-for-arm32
Mar 29, 2026
Merged

Refine generated instructions for staticly linked programs of Arm32#321
jserv merged 1 commit intosysprog21:masterfrom
DrXiao:fix-codegen-for-arm32

Conversation

@DrXiao
Copy link
Copy Markdown
Collaborator

@DrXiao DrXiao commented Mar 29, 2026

Previously, for the Arm architecture, the compiler generated the following sequence at the program entry point for staticly linked executables:

  • a 'bl' instruction to call a global initialization function, and then execute the main function.
  • a 'b' instruction.
  • a sequence of instructions to invoke the '_exit' system all.

After further investigation, since the main function inherently invokes '_exit' upon completion, the entry sequence can be simplified. We now use a single 'b' instruction to perform an unconditional jump to the global initialization and the main routines.

Additionally, the implementation of the '__syscall' function has been corrected. Previously, it modified registers r4-r7 without preserving their original values for the caller. This commit ensures that these callee-saved registers are properly saved and restored.

Finally, This refinement improves the efficiency and correctness of the generated instructions for statically linked programs.


Summary by cubic

Simplifies ARM32 static entry by branching directly to global init + main, and fixes __syscall to preserve callee-saved registers. Fewer startup instructions and correct ABI behavior.

  • Bug Fixes

    • __syscall now saves/restores r4–r7 with stmdb/ldm.
    • Extra args are loaded from the correct stack offsets after the push.
  • Refactors

    • Replaced the entry sequence with a single b; removed explicit _exit code.
    • Recomputed ELF offsets to match the shorter static entry path.

Written for commit fc28ca9. Summary will update on new commits.

@jserv jserv requested a review from vacantron March 29, 2026 08:07
Comment on lines +672 to +678
/* Jump directly to the main preparation and then execute the
* main function.
*
* In static linking mode, when the main function completes its
* execution, it will invoke the '_exit' syscall to terminate
* the program.
*/
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consolidate the comments, illustrating with Arm instructions.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/arm-codegen.c">

<violation number="1" location="src/arm-codegen.c:695">
P2: Dead instruction: `r7` is loaded from `[sp+28]` but immediately overwritten by `mov r7, r0` on the next line. This wastes one instruction (4 bytes in the binary) and reads from a stack slot that has no corresponding argument. Remove this load to match the actual 7-parameter signature of `__syscall`.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Previously, for the Arm architecture, the compiler generated the
following sequence at the program entry point for staticly linked
executables:
- a 'bl' instruction to call a global initialization function, and
  then execute the main function.
- a 'b' instruction.
- a sequence of instructions to invoke the '_exit' system all.

After further investigation, since the main function inherently invokes
'_exit' upon completion, the entry sequence can be simplified. We now
use a single 'b' instruction to perform an unconditional jump to the
global initialization and the main routines.

Additionally, the implementation of the '__syscall' function has been
corrected. Previously, it modified registers r4-r7 without preserving
their original values for the caller. This commit ensures that these
callee-saved registers are properly saved and restored.

Finally, This refinement improves the efficiency and correctness of
the generated instructions for statically linked programs.
@DrXiao DrXiao force-pushed the fix-codegen-for-arm32 branch from 34d6a27 to fc28ca9 Compare March 29, 2026 11:21
@jserv jserv merged commit 0d07d85 into sysprog21:master Mar 29, 2026
13 checks passed
@jserv
Copy link
Copy Markdown
Collaborator

jserv commented Mar 29, 2026

Thank @DrXiao for contributing!

@DrXiao DrXiao deleted the fix-codegen-for-arm32 branch March 29, 2026 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants