Skip to content

Commit 1db53db

Browse files
Rebase interpreter (#214)
* wip eval * first full interpreter attempt * remove unshared token * fix wrapping behaviour of cmpxchg * fix thread text parsing * add threading tests * restore seeding flags * fix instance visibility in init code * fix binary test --------- Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
1 parent 2c42587 commit 1db53db

37 files changed

Lines changed: 1288 additions & 438 deletions

interpreter/binary/decode.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ let rec sN n s =
9898
then (if b land 0x40 = 0 then x else Int64.(logor x (logxor (-1L) 0x7fL)))
9999
else Int64.(logor x (shift_left (sN (n - 7) s) 7))
100100

101-
let u1 s = Int64.to_int (uN 1 s)
102101
let u32 s = Int64.to_int32 (uN 32 s)
103102
let s7 s = Int64.to_int (sN 7 s)
104103
let s32 s = Int64.to_int32 (sN 32 s)
@@ -114,7 +113,6 @@ let len32 s =
114113
if I32.le_u n (Int32.of_int (len s - pos)) then Int32.to_int n else
115114
error s pos "length out of bounds"
116115

117-
let bool s = (u1 s = 1)
118116
let string s = let n = len32 s in get_string n s
119117
let rec list f n s = if n = 0 then [] else let x = f s in x :: list f (n - 1) s
120118
let opt f b s = if b then Some (f s) else None
@@ -180,20 +178,24 @@ let func_type s =
180178
FuncType (ts1, ts2)
181179
| _ -> error s (pos s - 1) "malformed function type"
182180

183-
let limits uN s =
184-
let has_max = bool s in
185-
let min = uN s in
186-
let max = opt uN has_max s in
187-
{min; max}
181+
let limits vu s =
182+
let flags = byte s in
183+
require (flags land 0xfc = 0) s (pos s - 1) "malformed limits flags";
184+
let has_max = (flags land 1 = 1) in
185+
let shared = (flags land 2 = 2) in
186+
let min = vu s in
187+
let max = opt vu has_max s in
188+
{min; max}, shared
188189

189190
let table_type s =
190191
let t = ref_type s in
191-
let lim = limits u32 s in
192+
let lim, shared = limits u32 s in
193+
require (not shared) s (pos s - 1) "tables cannot be shared (yet)";
192194
TableType (lim, t)
193195

194196
let memory_type s =
195-
let lim = limits u32 s in
196-
MemoryType lim
197+
let lim, shared = limits u32 s in
198+
MemoryType (lim, if shared then Shared else Unshared)
197199

198200
let mutability s =
199201
match byte s with

interpreter/binary/encode.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ struct
7373
Code.error Source.no_region "length out of bounds";
7474
u32 (Int32.of_int i)
7575

76-
let bool b = u1 (if b then 1 else 0)
7776
let string bs = len (String.length bs); put_string s bs
7877
let name n = string (Utf8.encode n)
7978
let list f xs = List.iter f xs
@@ -118,14 +117,16 @@ struct
118117
s7 (-0x20); vec value_type ts1; vec value_type ts2
119118

120119

121-
let limits vu {min; max} =
122-
bool (max <> None); vu min; opt vu max
120+
let limits vu {min; max} shared =
121+
let int_of_bool b = if b then 1 else 0 in
122+
let flags = int_of_bool shared lsl 1 lor int_of_bool (max <> None) in
123+
byte flags; vu min; opt vu max
123124

124125
let table_type = function
125-
| TableType (lim, t) -> ref_type t; limits u32 lim
126+
| TableType (lim, t) -> ref_type t; limits u32 lim false
126127

127128
let memory_type = function
128-
| MemoryType lim -> limits u32 lim
129+
| MemoryType (lim, shared) -> limits u32 lim (shared = Shared)
129130

130131
let mutability = function
131132
| Immutable -> byte 0

0 commit comments

Comments
 (0)