Skip to content

Commit

Permalink
Implement binary encoding and decoding.
Browse files Browse the repository at this point in the history
The opcodes for try/catch/throw are taken from the provisional
exception-handling proposal.
  • Loading branch information
dhil committed Feb 16, 2021
1 parent b0fcf75 commit 62fc2d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 12 additions & 2 deletions interpreter/binary/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,17 @@ let rec instr s =
end

| 0x05 -> error s pos "misplaced ELSE opcode"
| 0x06| 0x07 | 0x08 | 0x09 | 0x0a as b -> illegal s pos b
| 0x06 ->
let bt = block_type s in
let es1 = instr_block s in
expect 0x07 s "CATCH opcode expected";
let es2 = instr_block s in
end_ s;
try_ bt es1 es2
| 0x07 -> error s pos "misplaced CATCH opcode"
| 0x08 -> throw

| 0x09 | 0x0a as b -> illegal s pos b
| 0x0b -> error s pos "misplaced END opcode"

| 0x0c -> br (at var s)
Expand Down Expand Up @@ -538,7 +548,7 @@ let rec instr s =
and instr_block s = List.rev (instr_block' s [])
and instr_block' s es =
match peek s with
| None | Some (0x05 | 0x0b) -> es
| None | Some (0x05 | 0x07 | 0x0b) -> es
| _ ->
let pos = pos s in
let e' = instr s in
Expand Down
5 changes: 4 additions & 1 deletion interpreter/binary/encode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,10 @@ let encode m =
| Convert (F64 F64Op.DemoteF64) -> assert false
| Convert (F64 F64Op.ReinterpretInt) -> op 0xbf

| Try _ | Throw -> failwith "encode: not yet implemented"
| Try (bt, es1, es2) ->
op 0x06; block_type bt; list instr es1;
op 0x07; list instr es2; end_ ()
| Throw -> op 0x08

let const c =
list instr c.it; end_ ()
Expand Down

0 comments on commit 62fc2d9

Please sign in to comment.