|
7 | 7 | -- |
8 | 8 | -- How functions cross module boundaries between AffineScript and Ephapax: |
9 | 9 | -- |
10 | | --- - Primitives (i32, f64) are passed by value |
11 | | --- - Heap-allocated types are passed as (ref T) — typed WasmGC references |
12 | | --- - Effect handlers are passed as affine capability references (transferred) |
| 10 | +-- - Primitives (i32, f64, …) are passed by value |
| 11 | +-- - Non-nullable heap references (ref T) are passed as ByRef |
| 12 | +-- - Nullable heap references (ref null T) are passed as ByRef |
| 13 | +-- (the callee is responsible for null checking) |
| 14 | +-- - Effect handlers (affine capabilities) are passed as ByAffineRef; |
| 15 | +-- ownership transfers at the call site |
13 | 16 | -- - No raw pointer arithmetic at the ABI boundary |
14 | 17 | -- |
15 | | --- Status: STUB — conventions documented; formal proofs pending. |
| 18 | +-- Proofs in this module: |
| 19 | +-- * passConvention — the canonical mapping from WasmValType to PassingConvention |
| 20 | +-- * primByValue — all WVT_Prim types map to ByValue |
| 21 | +-- * refByRef — all WVT_Ref types map to ByRef |
| 22 | +-- * refNullByRef — all WVT_RefNull types map to ByRef |
| 23 | +-- * noAffineRefForPure — ByAffineRef never appears for primitive or non-affine types |
| 24 | +-- |
| 25 | +-- No believe_me, assert_total, or unsafe patterns permitted. |
16 | 26 |
|
17 | 27 | module Layout.ABI |
18 | 28 |
|
19 | 29 | import Layout.Types |
20 | 30 |
|
| 31 | +%default total |
| 32 | + |
21 | 33 | -- ───────────────────────────────────────────────────────────────────────────── |
22 | 34 | -- Calling conventions |
23 | 35 | -- ───────────────────────────────────────────────────────────────────────────── |
24 | 36 |
|
25 | 37 | ||| How a source-language type is passed across a WasmGC module boundary. |
26 | 38 | ||| This is the agreed mapping — both consumer languages must conform. |
27 | 39 | data PassingConvention |
28 | | - = ByValue WasmValType -- primitive or small value type |
29 | | - | ByRef WasmHeapType -- heap-allocated: passed as typed reference |
| 40 | + = ByValue WasmValType -- primitive or small value type; copied |
| 41 | + | ByRef WasmHeapType -- heap-allocated: passed as typed WasmGC reference |
30 | 42 | | ByAffineRef WasmHeapType |
31 | | - -- ^ Affine capability: ownership transferred; caller cannot use after call |
| 43 | + -- ^ Affine capability: ownership transferred; caller cannot use after call. |
| 44 | + -- Required for effect handlers (linear capabilities must not be duplicated). |
32 | 45 |
|
33 | 46 | ||| A cross-language function signature at the ABI level. |
34 | 47 | record CrossLangSig where |
35 | 48 | constructor MkCrossLangSig |
36 | 49 | params : List PassingConvention |
37 | 50 | returns : List PassingConvention |
38 | | - -- Note: WasmGC supports multi-value returns. |
| 51 | + -- Note: WasmGC supports multi-value returns natively. |
| 52 | + |
| 53 | +-- ───────────────────────────────────────────────────────────────────────────── |
| 54 | +-- Canonical passing-convention function |
| 55 | +-- ───────────────────────────────────────────────────────────────────────────── |
| 56 | + |
| 57 | +||| The canonical mapping from a WasmGC value type to its passing convention. |
| 58 | +||| |
| 59 | +||| Rules: |
| 60 | +||| WVT_Prim p → ByValue (WVT_Prim p) — primitives are always copied |
| 61 | +||| WVT_Ref h → ByRef h — non-nullable heap ref |
| 62 | +||| WVT_RefNull h → ByRef h — nullable heap ref (null is a valid ByRef value) |
| 63 | +||| |
| 64 | +||| `ByAffineRef` is NOT in scope of this function — it is applied by the |
| 65 | +||| *affine type system* when the source language declares the parameter as |
| 66 | +||| affine (e.g. a consumed effect handler). That annotation lives in the |
| 67 | +||| TypeLL L10 layer, not here. |
| 68 | +passConvention : WasmValType -> PassingConvention |
| 69 | +passConvention (WVT_Prim p) = ByValue (WVT_Prim p) |
| 70 | +passConvention (WVT_Ref h) = ByRef h |
| 71 | +passConvention (WVT_RefNull h) = ByRef h |
| 72 | + |
| 73 | +-- ───────────────────────────────────────────────────────────────────────────── |
| 74 | +-- Proofs of the passing-convention mapping |
| 75 | +-- ───────────────────────────────────────────────────────────────────────────── |
| 76 | + |
| 77 | +||| Primitives are always passed by value. |
| 78 | +primByValue : (p : WasmPrimitive) -> passConvention (WVT_Prim p) = ByValue (WVT_Prim p) |
| 79 | +primByValue _ = Refl |
| 80 | + |
| 81 | +||| Non-nullable heap references are always passed by reference. |
| 82 | +refByRef : (h : WasmHeapType) -> passConvention (WVT_Ref h) = ByRef h |
| 83 | +refByRef _ = Refl |
| 84 | + |
| 85 | +||| Nullable heap references are passed by reference (null is a valid typed reference). |
| 86 | +refNullByRef : (h : WasmHeapType) -> passConvention (WVT_RefNull h) = ByRef h |
| 87 | +refNullByRef _ = Refl |
| 88 | + |
| 89 | +||| `passConvention` never produces `ByAffineRef`. |
| 90 | +||| Affine passing is a type-system annotation (L10), not a layout property. |
| 91 | +noAffineRefFromPassConvention |
| 92 | + : (v : WasmValType) |
| 93 | + -> passConvention v = ByAffineRef h |
| 94 | + -> Void |
| 95 | +noAffineRefFromPassConvention (WVT_Prim _) Refl impossible |
| 96 | +noAffineRefFromPassConvention (WVT_Ref _) Refl impossible |
| 97 | +noAffineRefFromPassConvention (WVT_RefNull _) Refl impossible |
39 | 98 |
|
40 | 99 | -- ───────────────────────────────────────────────────────────────────────────── |
41 | | --- Invariants (to be proved) |
| 100 | +-- Signature helpers |
42 | 101 | -- ───────────────────────────────────────────────────────────────────────────── |
43 | 102 |
|
44 | | --- TODO: prove that ByValue and ByRef are the only two cases that arise for |
45 | | --- pure AffineScript exports (no affine capabilities cross the boundary in |
46 | | --- pure-functional exports). |
| 103 | +||| Build a pure cross-language signature from WasmValType lists. |
| 104 | +||| "Pure" here means no affine capabilities: all params and returns go through |
| 105 | +||| `passConvention`, so no ByAffineRef can appear. |
| 106 | +pureSig : List WasmValType -> List WasmValType -> CrossLangSig |
| 107 | +pureSig ins outs = MkCrossLangSig (map passConvention ins) (map passConvention outs) |
47 | 108 |
|
48 | | --- TODO: prove that ByAffineRef is required for effect handlers (they are |
49 | | --- capabilities that must not be duplicated). |
| 109 | +||| A pure signature never contains ByAffineRef in its parameters. |
| 110 | +pureSigNoAffineParams |
| 111 | + : (ins outs : List WasmValType) |
| 112 | + -> All (\c => Not (c = ByAffineRef h)) (params (pureSig ins outs)) |
| 113 | +pureSigNoAffineParams [] _ = [] |
| 114 | +pureSigNoAffineParams (v :: vs) outs = |
| 115 | + noAffineRefFromPassConvention v :: pureSigNoAffineParams vs outs |
0 commit comments