Repository navigation
[AUTOMATED] fix(p5): a byte or halfword a function returns zero-extended was typed signed on ARM, PowerPC and RISC-V - #864
Merged
Conversation
…ded was typed signed on ARM, PowerPC and RISC-V Sub-variable flow trims a RETURN that reads ZEXT(x) to the narrow x, and the type fold then types x by its own ops: an add or multiply reads as short, an unknown byte as char. Where the calling convention extends a narrow return by the sign of its type (cspec inttype, or the narrowext rule), that zero-extension means the value is unsigned. The RETURN pull now records it and type inference offers the unsigned type of that width for the returned value. Fixes #833 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…rn record CloneBlockOps copied only the upstream addl-flag subset onto a cloned op, so a RETURN that block duplication copies into another exit lost kuna_zextreturn and the function kept the signed type: a PowerPC -O2 loop summing shorts into an unsigned short return still printed `short h_loop(...)`. The clone now carries the record. The round trip gains that loop (h_loop) on every fixture. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
mahaloz
force-pushed
the
fix/arm-ldrh-return-signed
branch
from
October 3, 2026 00:39
f506962 to
0a2cd4a
Compare
mahaloz
added a commit
to phix33/kuna
that referenced
this pull request
Oct 3, 2026
Brings in Noelo-Lab#795 (flowmode) and Noelo-Lab#864. Counts re-derived on a fresh build: 253 settables, tiers 81/105/67, 252 catalog rows, corpus 389; the stages baseline is main's plus the three STACK STORE keys. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
On ARM, PowerPC and RISC-V a function extends a narrow return value by the sign of its type. When it zero-extends one, kuna still types the value by its own arithmetic, often as
shortorchar, and C then sign-extends it in every caller.Scale(100)returns 50 in the binary and -50 through the printed C. #833 shows the same thing for anldrhreload.The fix
inttype(ARM, PowerPC, SPARC) or thenarrowextrule (RISC-V and LoongArch below 32 bits; MIPS and Apple arm64 undernarrowext compiler).Nextnow returnsunsigned short, soNext() * a0zero-extends.// return-dupe) keeps the record, so a PowerPC loop likeunsigned short h_loop(int n, short *a) { short s = 0; for (...) s += a[i]; return s; }also printsunsigned short.What this does not fix: by default, MIPS keeps its old types, as
narrowextalready does for declared MIPS returns, because no ABI document states the extension.--option narrowext compilerapplies the fix there. SPARC returns are still mangled by #796.Tests
kuna-cli/tests/narrow_zext_returns.rsdecompiles clang objects for ARM (A32 at -O0 and -O2, ARMv7, Thumb-2), PowerPC, RV32, RV64 and MIPS (narrowext compiler). It builds the printed callees, plus the ARM callers, with gcc and clang at -O0 and -O2, and compares them with the source over 28 inputs. On main it fails:u16_mul(-1)gives0xfffffffffffffed4where the source gives0xfed4. If a duplicated RETURN drops the record, PowerPCh_loopgives0xfffffffffffffffefor0xfffe.decompile-allover 15 stripped ARM firmware binaries (15,770 functions) changes 319 functions:unsigned shorttouint2in reliable mode.'\0'becomes0,'0'becomes0x30), and a few byte locals split in two. The values do not change.lua_statusnow returningunsigned char.Fixes #833
🤖 Generated with Claude Code
https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt