Skip to main content

pepl_codegen/
types.rs

1//! WASM value-representation constants and memory layout.
2//!
3//! Every PEPL value is stored on the linear-memory heap as a 12-byte cell:
4//!
5//! ```text
6//! offset+0 : i32  — tag   (see TAG_* constants)
7//! offset+4 : 8 bytes — payload (interpretation depends on tag)
8//! ```
9//!
10//! # Payload layouts
11//!
12//! | Tag        | Bytes 4..8 (word-1)        | Bytes 8..12 (word-2)       |
13//! |------------|----------------------------|----------------------------|
14//! | NIL        | 0                          | 0                          |
15//! | NUMBER     | f64 occupies all 8 bytes (little-endian)                |
16//! | BOOL       | i32 (0 = false, 1 = true)  | 0 (padding)                |
17//! | STRING     | i32 data-offset            | i32 byte-length            |
18//! | LIST       | i32 array-offset           | i32 element-count          |
19//! | RECORD     | i32 entries-offset          | i32 field-count            |
20//! | VARIANT    | i32 variant-id             | i32 data-ptr (record)      |
21//! | LAMBDA     | i32 func-index             | i32 closure-ptr            |
22//! | COLOR      | i32 rgba-packed            | 0 (padding)                |
23//! | ACTION_REF | i32 action-id              | 0 (padding)                |
24
25/// Size of a single PEPL value cell on the heap (bytes).
26pub const VALUE_SIZE: u32 = 12;
27
28// ── Value tags ───────────────────────────────────────────────────────────────
29
30pub const TAG_NIL: i32 = 0;
31pub const TAG_NUMBER: i32 = 1;
32pub const TAG_BOOL: i32 = 2;
33pub const TAG_STRING: i32 = 3;
34pub const TAG_LIST: i32 = 4;
35pub const TAG_RECORD: i32 = 5;
36pub const TAG_VARIANT: i32 = 6;
37pub const TAG_LAMBDA: i32 = 7;
38pub const TAG_COLOR: i32 = 8;
39pub const TAG_ACTION_REF: i32 = 9;
40
41// ── Global variable indices ──────────────────────────────────────────────────
42// (order must match the global section emission in compiler.rs)
43
44/// Heap allocation pointer — next free byte in linear memory.
45pub const GLOBAL_HEAP_PTR: u32 = 0;
46/// Gas counter — incremented on each tick.
47pub const GLOBAL_GAS: u32 = 1;
48/// Gas limit — set at init, trap when exceeded.
49pub const GLOBAL_GAS_LIMIT: u32 = 2;
50/// Pointer to the state record value.
51pub const GLOBAL_STATE_PTR: u32 = 3;
52
53// ── Imported function indices ────────────────────────────────────────────────
54// (order must match the import section emission in compiler.rs)
55
56/// `env.host_call(cap_id: i32, fn_id: i32, args_ptr: i32) -> i32`
57pub const IMPORT_HOST_CALL: u32 = 0;
58/// `env.log(ptr: i32, len: i32)`
59pub const IMPORT_LOG: u32 = 1;
60/// `env.trap(ptr: i32, len: i32)` — aborts execution with a message.
61pub const IMPORT_TRAP: u32 = 2;
62/// `env.get_timestamp() -> i64` — deterministic event timestamp (milliseconds).
63pub const IMPORT_GET_TIMESTAMP: u32 = 3;
64
65/// Number of imported functions (offset for locally-defined function indices).
66pub const IMPORT_COUNT: u32 = 4;
67
68// ── WASM type indices ────────────────────────────────────────────────────────
69// Fixed type indices in the type section (see compiler.rs emit_types).
70
71/// `() -> ()`
72pub const TYPE_VOID_VOID: u32 = 0;
73/// `() -> i32`
74pub const TYPE_VOID_I32: u32 = 1;
75/// `(i32) -> ()`
76pub const TYPE_I32_VOID: u32 = 2;
77/// `(i32) -> i32`
78pub const TYPE_I32_I32: u32 = 3;
79/// `(i32, i32) -> ()`
80pub const TYPE_I32X2_VOID: u32 = 4;
81/// `(i32, i32) -> i32`
82pub const TYPE_I32X2_I32: u32 = 5;
83/// `(i32, i32, i32) -> i32`
84pub const TYPE_I32X3_I32: u32 = 6;
85/// `(f64) -> i32`
86pub const TYPE_F64_I32: u32 = 7;
87/// `(i32, f64) -> ()`
88pub const TYPE_I32_F64_VOID: u32 = 8;
89/// `() -> i64`
90pub const TYPE_VOID_I64: u32 = 9;
91/// `(i32, i32, i32) -> ()`
92pub const TYPE_I32X3_VOID: u32 = 10;
93
94/// Total number of fixed type signatures.
95pub const TYPE_COUNT: u32 = 11;
96
97// ── Memory ───────────────────────────────────────────────────────────────────
98
99/// Initial linear memory size in pages (64 KiB each).
100pub const INITIAL_MEMORY_PAGES: u64 = 1;
101/// Maximum linear memory pages (16 MiB).
102pub const MAX_MEMORY_PAGES: u64 = 256;
103/// Heap starts after the data segment region. We reserve the first 4 KiB for
104/// static data (string constants, etc.).
105pub const HEAP_START: u32 = 4096;
106
107// ── Custom section ───────────────────────────────────────────────────────────
108
109/// Custom section name for PEPL metadata.
110pub const CUSTOM_SECTION_NAME: &str = "pepl";
111/// Compiler version embedded in the custom section.
112pub const COMPILER_VERSION: &str = env!("CARGO_PKG_VERSION");