Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions interpreter/custom/handler_branch_hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ let get_nth_inst locs idx =
| Some (_, i) -> Some i
| None -> None


(* Decoding *)

(* TODO: make Decode module reusable instead of duplicating code *)
Expand Down Expand Up @@ -144,21 +145,27 @@ let decode_hint locs foff s =

let decode_func_hints locs foff = decode_vec (decode_hint locs foff)

let decode_func m s =
let get_func_start_off f bs =
let s = stream bs in
skip f.at.left.column s;
let _ = decode_u32 s in
pos s

let decode_func m bs s =
let fidx = decode_u32 s in
let f = get_func m fidx in
let foff = Int32.of_int f.at.left.column in
let foff = Int32.of_int (get_func_start_off f bs) in
let locs = flatten_instr_locs f.it.body in
let hs = decode_func_hints locs foff s in
(fidx, List.rev hs)

let decode_funcs m s =
let fs = decode_vec (decode_func m) s in
let decode_funcs m bs s =
let fs = decode_vec (decode_func m bs) s in
IdxMap.add_seq (List.to_seq fs) IdxMap.empty

let decode m _ custom =
let decode m bs custom =
let s = stream custom.it.content in
try { func_hints = decode_funcs m s } @@ custom.at
try { func_hints = decode_funcs m bs s } @@ custom.at
with EOS -> decode_error (pos s) "unexpected end of name section"

(* Encoding *)
Expand Down Expand Up @@ -200,22 +207,22 @@ let encode_hint locs foff buf h =

let encode_func_hints buf locs foff = encode_vec buf (encode_hint locs foff)

let encode_func m buf t =
let encode_func m bs buf t =
let fidx, hs = t in
encode_u32 buf fidx;
let f = get_func m fidx in
let foff = f.at.left.column in
let foff = get_func_start_off f bs in
let locs = flatten_instr_locs f.it.body in
encode_func_hints buf locs foff hs

let encode_funcs buf m fhs =
encode_vec buf (encode_func m) (List.of_seq (IdxMap.to_seq fhs))
let encode_funcs buf m bs fhs =
encode_vec buf (encode_func m bs) (List.of_seq (IdxMap.to_seq fhs))

let encode m bs sec =
let { func_hints } = sec.it in
let m2 = Decode.decode "" bs in
let buf = Buffer.create 200 in
encode_funcs buf m2 func_hints;
encode_funcs buf m2 bs func_hints;
let content = Buffer.contents buf in
{
name = Utf8.decode "metadata.code.branch_hint";
Expand Down
9 changes: 9 additions & 0 deletions test/custom/metadata.code.branch_hint/branch_hint.wast
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
)
)

(module binary
"\00asm" "\01\00\00\00"
"\01\85\80\80\80\00\01\60\01\7f\00"
"\03\82\80\80\80\00\01\00\00"
"\a0\80\80\80\00\19\6d\65\74\61\64\61\74\61\2e\63\6f\64\65\2e\62\72\61\6e\63\68\5f\68\69\6e\74\01\00\01\05\01\00"
"\0a\8f\80\80\80\00\01\89\80\80\80\00\00\02\40\41\00\0d\00\0b\0b"
)

(assert_malformed_custom
(module quote
"(func $test2 (type 0)"
Expand Down Expand Up @@ -97,3 +105,4 @@
)
"@metadata.code.branch_hint annotation: invalid target"
)

Loading