Skip to content

[AUTOMATED] Name an address read at two types once, keep walk bounds as addresses, and name Cortex-M flash data - #2143

Merged
mahaloz merged 12 commits into
mainfrom
feat/dataconst
Dec 4, 2026
Merged

mahaloz merged 12 commits into
mainfrom
feat/dataconst

Conversation

@mahaloz

@mahaloz mahaloz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

A constant address that a function uses at two pointer types, or at another type than it reads the global at directly, keeps a raw (T *)0x... cast, and the end bound of a pointer walk is named as whatever object follows the array:

$ cat > t.c <<'C'
#include <stdio.h>
#include <string.h>
int tab[4] = {1, 2, 3, 4};
long after = 5;
char rec[12];
__attribute__((noinline)) void zero(char *p, int n) { memset(p, 0, n); }
__attribute__((noinline)) void seti(unsigned *p, unsigned v) { *p = v; }
__attribute__((noinline)) long sum(void) { long s = 0; for (int *p = tab; p != tab + 4; p++) s += *p; return s; }
__attribute__((noinline)) int two(unsigned v) { zero(rec, 12); seti((unsigned *)rec, v); return *(int *)rec + after; }
int main(int c, char **v) { printf("%ld %d\n", sum(), two(c)); return 0; }
C
$ gcc -O0 -o t t.c && strip t && kuna decompile-all t | grep -E 'v2 != |4030'    # gcc 11, x86-64
  for (v2 = &dat_4010; v2 != &dat_4020; v2 = &v2[1]) {     # before: the bound is `after`, a long
  sub_1169((void *)0x4030,0xc);
  sub_1196((unsigned int *)0x4030,a0);
  return (int)dat_4020 + dat_4030;

  for (v2 = &dat_4010; v2 != (int *)0x4020; v2 = &v2[1]) {   # after
  sub_1169(&dat_4030,0xc);
  sub_1196((unsigned int *)&dat_4030,a0);
  return (int)dat_4020 + dat_4030;
  • globalref gets a third value, views, now the default (on is the previous default, off the raw casts). Under views an address used at several types is named once and declared at the type its direct accesses agree on (int dat_4030 above), or as the unknown byte when the function never reads it directly; each reader of another type reads the name through a cast. A view never changes the type a direct access is declared at, since that type is what the access computes with (a compare's width, the extension of an argument passed to a wider parameter).
  • views adds nothing where on refuses an indexed base, an address added to or subtracted from (tab - 1 need not start an object), or storage a direct access overlaps at another start or type: those keep their cast, e.g. ((short *)0x4018)[i] for a table also read directly as unsigned short.
  • The end bound of a pointer walk (an ordering compare, or an equality against a cursor stepped by a constant) keeps its address. A list walk's p != &head still names the head.
  • On an M-profile ARM image (Thumb only) the linker puts constant tables in the flash .text section, so mix((void *)0x1012c,a0) stayed a cast; even addresses there can now hold objects (mix(&dat_1012c,a0)), odd ones are Thumb code pointers and keep their spelling. Other targets are unaffected.
  • --option globalref on reproduces the previous output byte for byte on the decbench corpus.

On that corpus (87,514 functions, x86-64 and Cortex-M, O0/O2/O2-noinline) 1,702 bodies change and no variable or type does: 893 constant-address casts become a bare &dat_X, 2,740 more addresses are named under the cast their reader needs, and 460 walk bounds keep their address; 421 fewer casts in all, and the 308 functions with more casts are all walk bounds. No newly named address is a function entry or inside a function (checked against the unstripped builds), no constant spelled as an integer changes, and the crazyflie project headers declare no directly read global at another width or signedness than before. Instruction counts move by at most +0.07%.

Limits: a Thumb function's own bytes read as data (a checksum over code) are named &dat_X too, since the flash sections hold code as well; a string in Cortex-M flash prints as &dat_X, not as the literal; and in a decompile-project export a function that only takes an address at two pointer types now names it, while the header still leaves an address read at two types undeclared, so that function needs the name declared to compile (as on already does for a one-type reader; no function of the decbench corpus that compiled before is affected).

Tests: tests/stages/kuna-globalref-views.xml (two passes, on then the default, on an x86-64 and a Cortex-M fixture); an_address_read_at_two_types_round_trips_through_the_printed_c compiles the printed callers under on and views with gcc and clang and runs them, including a narrow read of a wider global, an unsigned int passed to a long, an indexed unsigned short table and a table read as a scalar; the existing globalref round trip gains a views arm; unit tests for the flash ranges, the direct-object list and the option values.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT

mahaloz and others added 5 commits December 4, 2026 17:08
A caller that refused or withdrew its callee's callrettype statement still
compared the result against an unknown of its width, so it printed
`v1 = (long *)xalloc(0x18)` beside `void * xalloc(..)` and
`v2 = (char *)savestr(a0)` beside `char * savestr(..)`. The output token is
now the caller's own type when it is the type the callee states, or when an
assigned result meets a `void *` on one side and an object pointer on the
other. Variable types are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…and Cortex-M flash data

`option globalref` gains a value, `views`, now the default. An address the
function uses at more than one pointed-to type, or at another type than it
reads the storage at directly, is named once: the object is declared at the
type its direct accesses agree on (or as the unknown byte) and each reader of
another type reads it through a cast. An integer direct access at the
object's start and width whose uses keep their bits under any integer type
does not block an integer declaration. The end bound of a pointer walk is no
longer named as the object after the array. On an M-profile (Thumb-only) ARM
image, even addresses in the executable flash sections can hold objects.

Work in progress: options.md, the stages baseline and measurements follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…stages baseline

Re-records docs/baseline-stages.json for the new kuna-globalref-views stage
test and the two kuna-globalref assertions the default changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…stage test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Dec 4, 2026
mahaloz and others added 7 commits December 4, 2026 19:15
…eclared type

Drop the rule that let a direct integer read whose uses looked bit-agnostic
take the type of a reader of another type. It declared the read at the
reader's type, dropped it from the function's direct objects, and let the
header pick a wider type another function took the address at (an 8-byte
compare of a 4-byte read), an array (a scalar compare against a decayed
array), or the other signedness (an unsigned int passed to a long parameter
sign-extended). It also let an indexed base through as the scalar's address,
`(&dat_X)[i]`. Every direct access now stays an object of its own type, and a
reader of another type takes the cast; an indexed base keeps on's verdict.

The views fixture gains the four shapes, run natively by the round trip, and
the stage test pins the cast on the reader and on the indexed base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
C converts a `void *` to or from a pointer to an object on assignment, but
Rust and Go do not: under `--option outlang rust` a refused call result
printed `v1 = xalloc(0x18);` into a `*mut i64` (E0308), and under
`outlang go` `v1 = xalloc(0x18)` into a `*int64`. The `void *` case now
applies to C output only; a result held at the callee's own type still
prints no cast in every language.

The arms of a conditional expression are brought to one type before the
assignment, so a `char *` arm beside a `struct_0 *` arm is not valid C.
A call that is an arm of a diamond `iteregion` prints as `?:` keeps its
cast: `v2 = (a0 & 1) ? (void *)mk_str(a0) : (void *)mk_pt(a0);`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
Corpus file count 1032; stage baseline merged by key (6,366).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
@mahaloz
mahaloz merged commit eb91b37 into main Dec 4, 2026
8 checks passed
@mahaloz
mahaloz deleted the feat/dataconst branch December 4, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant