Skip to content

[Fulminate] Unboxed ghost types - #694

Open
rbanerjee20 wants to merge 6 commits into
rems-project:mainfrom
rbanerjee20:unboxed-runtime
Open

rbanerjee20 wants to merge 6 commits into
rems-project:mainfrom
rbanerjee20:unboxed-runtime

Conversation

@rbanerjee20

Copy link
Copy Markdown
Contributor

No description provided.

rbanerjee20 and others added 3 commits November 5, 2026 14:09
The runtime represented every CN value as a bump-allocated struct passed by
pointer (`typedef struct cn_bits_i32 { int32_t val; } cn_bits_i32;`, used as
`cn_bits_i32*`). The scalar CN types are now the C scalars themselves, passed
by value:

  typedef int32_t cn_bits_i32;   (and i8/i16/i64, u8..u64)
  typedef int64_t cn_integer;
  typedef void*   cn_pointer;
  typedef bool    cn_bool;
  typedef uint8_t cn_alloc_id;

so no allocation happens for arithmetic, comparisons, conversions or casts.
Aggregates keep their representation: maps are still `cn_map*` (a hash
table), and the structs generated for C structs, records and datatypes are
still bump-allocated and passed as pointers, with scalar members by value.

Where a generic `void*` slot must hold a scalar, it is boxed explicitly
(`<cntype>_box`):
- maps: `cn_map_set_<cntype>` boxes the value, `cn_map_get_<cntype>` unboxes
  it (or returns the type's default); `convert_to_cn_map` takes the setter;
  `cn_map_equality` takes `<cntype>_ptr_equality` for scalar values;
- ghost arguments: `add_<cntype>_to_ghost_frame` /
  `load_<cntype>_from_ghost_frame`.

`cn_bool_inplace_and` is gone and `<cntype>_increment` returns the successor:
by-value callers assign. `cn_ite` is a conditional expression. The pointer
functions, `cn_array_shift`, `cn_member_shift` and `cn_pointer_deref` work on
the `void*` directly.

The generator is adapted in the next commit; the two only build together.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- `cn_to_ail_base_type` no longer wraps the scalar types (bitvectors,
  integer, bool, pointer, allocation id) in a pointer; maps, structs, records
  and datatypes stay pointers. `get_typedef_string` reads the typedef
  annotation on a by-value type too.
- `each` loops assign instead of mutating in place
  (`b = cn_bool_and(b, e)`, `i = cn_bits_i32_increment(i)`).
- Map updates use `cn_map_set_<cntype>` for scalar values and `cn_map_set`
  for aggregates (`map_set_fn_str_of_bt`); map equality passes
  `<cntype>_ptr_equality` for scalar values; `convert_to_cn_map` is given the
  setter.
- Ghost arguments of scalar type go through the typed
  `add_<cntype>_to_ghost_frame` / `load_<cntype>_from_ghost_frame`.
- The generated ownership functions and the per-struct map getters use the
  `cn_pointer` / `cn_integer` arguments directly instead of their `->ptr` /
  `->val` members; the local `<x>_addr_cn` variables are `cn_pointer`s.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
It failed to compile because the generator passes a function's address raw
where a boxed `cn_pointer*` was expected; with `cn_pointer` being `void*` it
now compiles and passes.

tests/run-cn-exec.sh: 213 pass, 0 fail (upstream main 831e973: 213 pass).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
rbanerjee20 and others added 2 commits November 5, 2026 14:17
…alue types

`map_set_fn_str_of_ctype`, the map value equality and the ghost-argument
add/load picked the typed, boxing helper whenever the type had a typedef
name, but `cn_map` has one too and is passed as a pointer: a map-valued map
called the undefined `cn_map_set_cn_map`, and a map ghost argument the
undefined `add_cn_map_to_ghost_frame`. `scalar_typedef_string` answers only
for the non-pointer CN C types, so maps and the generated structs take the
generic `void*` path, as before.

Found by review; e.g.

  let inner = mm[0i32][2i32: 3i32];
  let mm2 = mm[1i32: inner];         // mm : map<i32, map<i32, i32>>

now compiles and runs. (A map ghost argument still fails for a pre-existing
reason: the ghost tag enum name for `map<i32, i32>` is printed with a space,
`MAP_I32_ I32_`, on upstream main too.)

tests/run-cn-exec.sh: 213 pass, 0 fail.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…sed cn_ite

cn_alloc_id had the boxing, conversion, equality and default functions but
not the typed map set/get and ghost-frame add/load the generator emits for
every by-value type, so an alloc_id ghost argument or an alloc_id-valued map
did not compile (`add_cn_alloc_id_to_ghost_frame` undeclared). Found by
review.

`cn_ite` has no user: the generator lowers if-then-else to an if statement.

tests/run-cn-exec.sh: 213 pass, 0 fail.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@rbanerjee20
rbanerjee20 marked this pull request as ready for review November 12, 2026 00:38
…ck to one shape

`wrap_with_convert` converts between C values and the CN runtime types and
`~convert_from` is its only switch; the previous commit threaded the choice
of map setter (boxing for a scalar element, plain for an aggregate) through
its array branch, splitting on that flag, which is not what the function is
for.

Every CN type now has a setter under the name `cn_map_set_<cntype>`, as it
has a getter under `cn_map_get_<cntype>`: the runtime's CN_GEN_MAP_SET for the
scalars, and a generated `cn_map_set_struct_<sym>` (a typed wrapper over
`cn_map_set`) next to each generated getter for C structs, records and
datatypes. `convert_to_cn_map(arr, cntype, n)` pastes both the setter and
the conversion from the type name, as `convert_from_cn_map` already pasted
the getter, and `wrap_with_convert` passes the type name in both directions
with no setter argument. `map_set_fn_str_of_bt` remains only at the map
update sites and the `each` resource loops, where choosing a setter is the
point.

tests/run-cn-exec.sh: 213 pass, 0 fail. Checked end to end on a struct with
an `int[3]` and a `struct pair[2]` member, which converts through
`convert_to_cn_map(s.xs, cn_integer, 3)` and
`convert_to_cn_map(s.ps, struct_pair_cn, 2)`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant