|
| 1 | +(ns cljwit.component |
| 2 | + "S4's guest-side marshalling, first slice (`doc/design/0030`): the |
| 3 | + linear-memory scaffolding a component needs — `memory`, |
| 4 | + `cabi_realloc` as a bump arena with a grow-last-block fast path, |
| 5 | + reset at each export entry — and the echo trampoline whose core |
| 6 | + signature the note pins: a lifted `func(string) -> string` is |
| 7 | + `(param i32 i32) (result i32)`, and the callee *returns* the |
| 8 | + pointer to its (ptr, code-units) pair. |
| 9 | +
|
| 10 | + Order constraint the arena depends on: a trampoline lifts every |
| 11 | + argument out of linear memory into the GC heap *before* its first |
| 12 | + lowering allocation — the entry reset moves the bump pointer under |
| 13 | + the caller's argument bytes, which stay intact only until the next |
| 14 | + allocation. Lifting allocates nothing linear, so lift-all-then- |
| 15 | + lower-all keeps it sound." |
| 16 | + (:require [clojure.string :as str])) |
| 17 | + |
| 18 | +(set! *warn-on-reflection* true) |
| 19 | + |
| 20 | +(def ^:private arena |
| 21 | + ;; Offsets 0..15 stay unused so 0 keeps meaning null. The grow-last |
| 22 | + ;; fast path is what the Canonical ABI's optimistic utf8 transcode |
| 23 | + ;; calls for (0030 §3); realloc of a non-last block copies. |
| 24 | + " (memory (export \"memory\") 1) |
| 25 | + (global $next (mut i32) (i32.const 16)) |
| 26 | + (global $last (mut i32) (i32.const 0)) |
| 27 | + (func $ensure (param $end i32) |
| 28 | + (block $ok |
| 29 | + (loop $grow |
| 30 | + (br_if $ok (i32.le_u (local.get $end) |
| 31 | + (i32.mul (memory.size) (i32.const 65536)))) |
| 32 | + (if (i32.eq (memory.grow (i32.const 1)) (i32.const -1)) |
| 33 | + (then (unreachable))) |
| 34 | + (br $grow)))) |
| 35 | + (func $cabi_realloc (export \"cabi_realloc\") |
| 36 | + (param $old i32) (param $old-size i32) (param $align i32) (param $new-size i32) |
| 37 | + (result i32) |
| 38 | + (local $p i32) (local $i i32) |
| 39 | + ;; grow-last fast path: the newest block resizes in place. |
| 40 | + (if (i32.and (i32.ne (local.get $old) (i32.const 0)) |
| 41 | + (i32.eq (local.get $old) (global.get $last))) |
| 42 | + (then |
| 43 | + (call $ensure (i32.add (local.get $old) (local.get $new-size))) |
| 44 | + (global.set $next (i32.add (local.get $old) (local.get $new-size))) |
| 45 | + (return (local.get $old)))) |
| 46 | + (local.set $p (i32.and (i32.add (global.get $next) |
| 47 | + (i32.sub (local.get $align) (i32.const 1))) |
| 48 | + (i32.xor (i32.sub (local.get $align) (i32.const 1)) |
| 49 | + (i32.const -1)))) |
| 50 | + (call $ensure (i32.add (local.get $p) (local.get $new-size))) |
| 51 | + (global.set $next (i32.add (local.get $p) (local.get $new-size))) |
| 52 | + (global.set $last (local.get $p)) |
| 53 | + ;; A non-last realloc preserves the old contents, per realloc. |
| 54 | + (if (i32.ne (local.get $old) (i32.const 0)) |
| 55 | + (then |
| 56 | + (local.set $i (i32.const 0)) |
| 57 | + (block $done |
| 58 | + (loop $copy |
| 59 | + (br_if $done (i32.ge_u (local.get $i) |
| 60 | + (select (local.get $old-size) (local.get $new-size) |
| 61 | + (i32.lt_u (local.get $old-size) (local.get $new-size))))) |
| 62 | + (i32.store8 (i32.add (local.get $p) (local.get $i)) |
| 63 | + (i32.load8_u (i32.add (local.get $old) (local.get $i)))) |
| 64 | + (local.set $i (i32.add (local.get $i) (i32.const 1))) |
| 65 | + (br $copy))))) |
| 66 | + (local.get $p)) |
| 67 | + (func $arena-reset |
| 68 | + (global.set $next (i32.const 16)) |
| 69 | + (global.set $last (i32.const 0))) |
| 70 | +") |
| 71 | + |
| 72 | +(def echo-wat |
| 73 | + "The echo component's core module: utf8 string in, the same bytes |
| 74 | + out — marshalling only, the body is identity on the GC value |
| 75 | + (`0030` §5)." |
| 76 | + (str "(module\n" |
| 77 | + " (type $Bytes (array (mut i8)))\n" |
| 78 | + arena |
| 79 | + " (func (export \"echo\") (param $ptr i32) (param $len i32) (result i32)\n" |
| 80 | + " (local $arr (ref $Bytes)) (local $i i32) (local $dst i32) (local $ret i32)\n" |
| 81 | + " (call $arena-reset)\n" |
| 82 | + " ;; lift: caller bytes -> GC array, before any lowering allocation\n" |
| 83 | + " (local.set $arr (array.new_default $Bytes (local.get $len)))\n" |
| 84 | + " (local.set $i (i32.const 0))\n" |
| 85 | + " (block $done1\n" |
| 86 | + " (loop $l1\n" |
| 87 | + " (br_if $done1 (i32.ge_u (local.get $i) (local.get $len)))\n" |
| 88 | + " (array.set $Bytes (local.get $arr) (local.get $i)\n" |
| 89 | + " (i32.load8_u (i32.add (local.get $ptr) (local.get $i))))\n" |
| 90 | + " (local.set $i (i32.add (local.get $i) (i32.const 1)))\n" |
| 91 | + " (br $l1)))\n" |
| 92 | + " ;; body: identity\n" |
| 93 | + " ;; lower: GC array -> fresh arena block\n" |
| 94 | + " (local.set $dst (call $cabi_realloc (i32.const 0) (i32.const 0)\n" |
| 95 | + " (i32.const 1) (array.len (local.get $arr))))\n" |
| 96 | + " (local.set $i (i32.const 0))\n" |
| 97 | + " (block $done2\n" |
| 98 | + " (loop $l2\n" |
| 99 | + " (br_if $done2 (i32.ge_u (local.get $i) (array.len (local.get $arr))))\n" |
| 100 | + " (i32.store8 (i32.add (local.get $dst) (local.get $i))\n" |
| 101 | + " (array.get_u $Bytes (local.get $arr) (local.get $i)))\n" |
| 102 | + " (local.set $i (i32.add (local.get $i) (i32.const 1)))\n" |
| 103 | + " (br $l2)))\n" |
| 104 | + " ;; the callee RETURNS the retptr to its 4-aligned (ptr, code-units)\n" |
| 105 | + " ;; pair -- 0030 §4's worked signature; a return-area param here is\n" |
| 106 | + " ;; zwasm's recorded bug.\n" |
| 107 | + " (local.set $ret (call $cabi_realloc (i32.const 0) (i32.const 0)\n" |
| 108 | + " (i32.const 4) (i32.const 8)))\n" |
| 109 | + " (i32.store (local.get $ret) (local.get $dst))\n" |
| 110 | + " (i32.store (i32.add (local.get $ret) (i32.const 4)) (array.len (local.get $arr)))\n" |
| 111 | + " (local.get $ret)))\n")) |
| 112 | + |
| 113 | +(defn wit |
| 114 | + "The echo world, utf8 declared by the lift (0030 §1 — utf8 is also |
| 115 | + the embed default)." |
| 116 | + [] |
| 117 | + (str/join "\n" |
| 118 | + ["package cljwit:s4;" |
| 119 | + "" |
| 120 | + "world echo-world {" |
| 121 | + " export echo: func(s: string) -> string;" |
| 122 | + "}"])) |
0 commit comments