Permalink
Browse files

Rename expressions to instructions

  • Loading branch information...
rossberg committed Aug 24, 2016
1 parent 4dc09e3 commit cf2d51d13c347f554cb2be32a8093ab16a1415e1
View
@@ -196,20 +196,20 @@ let var x = string_of_int x.it
let value v = string_of_value v.it
let constop v = value_type (type_of v.it) ^ ".const"
-let rec expr e =
+let rec instr e =
match e.it with
| Unreachable -> Atom "unreachable"
| Nop -> Atom "nop"
| Drop -> Atom "drop"
- | Block es -> Node ("block", list expr es)
- | Loop es -> Node ("loop", list expr es)
+ | Block es -> Node ("block", list instr es)
+ | Loop es -> Node ("loop", list instr es)
| Br (n, x) -> Atom ("br " ^ int n ^ " " ^ var x)
| BrIf (n, x) -> Atom ("br_if " ^ int n ^ " " ^ var x)
| BrTable (n, xs, x) ->
Atom ("br_table " ^ int n ^ " " ^ String.concat " " (list var (xs @ [x])))
| Return -> Atom "return"
| If (es1, es2) ->
- Node ("if", [Node ("then", list expr es1); Node ("else", list expr es2)])
+ Node ("if", [Node ("then", list instr es1); Node ("else", list instr es2)])
| Select -> Atom "select"
| Call x -> Atom ("call " ^ var x)
| CallImport x -> Atom ("call_import " ^ var x)
@@ -233,15 +233,15 @@ let rec expr e =
| Trapping msg -> Atom ("trap[\"" ^ String.escaped msg ^ "\"]")
| Label (es_cont, vs, es) ->
let ves = List.map (fun v -> Const (v @@ e.at) @@ e.at) (List.rev vs) in
- Node ("label[...]", list expr (ves @ es))
+ Node ("label[...]", list instr (ves @ es))
| Local (n, vs_local, vs, es) ->
let ves = List.map (fun v -> Const (v @@ e.at) @@ e.at) (List.rev vs) in
Node ("local" ^ string_of_int n ^ "[" ^
String.concat " " (List.map string_of_value vs_local) ^
- "]", list expr (ves @ es))
+ "]", list instr (ves @ es))
let const c =
- list expr c.it
+ list instr c.it
(* Functions *)
@@ -251,7 +251,7 @@ let func i f =
Node ("func $" ^ string_of_int i,
[Node ("type " ^ var ftype, [])] @
decls "local" locals @
- list expr body
+ list instr body
)
let start x = Node ("start " ^ var x, [])
@@ -2,6 +2,6 @@ open Sexpr
val func_type : Types.func_type -> sexpr
-val expr : Ast.expr -> sexpr
+val instr : Ast.instr -> sexpr
val module_ : Ast.module_ -> sexpr
View
@@ -85,8 +85,6 @@ let encode m =
let elem_type = function
| AnyFuncType -> u8 0x20
- let expr_type t = vec1 value_type t
-
let func_type = function
| FuncType (ins, out) -> u8 0x40; vec value_type ins; vec value_type out
@@ -103,15 +101,15 @@ let encode m =
let var x = vu x.it
let var32 x = vu32 (Int32.of_int x.it)
- let rec expr e =
+ let rec instr e =
match e.it with
| Unreachable -> op 0x00
- | Block es -> op 0x01; list expr es; op 0x0f
- | Loop es -> op 0x02; list expr es; op 0x0f
+ | Block es -> op 0x01; list instr es; op 0x0f
+ | Loop es -> op 0x02; list instr es; op 0x0f
| If (es1, es2) ->
- op 0x03; list expr es1;
+ op 0x03; list instr es1;
if es2 <> [] then op 0x04;
- list expr es2; op 0x0f
+ list instr es2; op 0x0f
| Select -> op 0x05
| Br (n, x) -> op 0x06; vu n; var x
| BrIf (n, x) -> op 0x07; vu n; var x
@@ -329,7 +327,7 @@ let encode m =
| Trapping _ | Label _ | Local _ -> assert false
let const c =
- list expr c.it; op 0x0f
+ list instr c.it; op 0x0f
(* Sections *)
@@ -420,7 +418,7 @@ let encode m =
vec local (compress locals);
let g = gap () in
let p = pos s in
- list expr body;
+ list instr body;
patch_gap g (pos s - p)
let code_section fs =
View
@@ -158,14 +158,14 @@ let implicit_decl c t at =
%token<string> TEXT
%token<string> VAR
%token<Types.value_type> VALUE_TYPE
-%token<string Source.phrase -> Ast.expr' * Values.value> CONST
-%token<Ast.expr'> UNARY
-%token<Ast.expr'> BINARY
-%token<Ast.expr'> TEST
-%token<Ast.expr'> COMPARE
-%token<Ast.expr'> CONVERT
-%token<int option -> Memory.offset -> Ast.expr'> LOAD
-%token<int option -> Memory.offset -> Ast.expr'> STORE
+%token<string Source.phrase -> Ast.instr' * Values.value> CONST
+%token<Ast.instr'> UNARY
+%token<Ast.instr'> BINARY
+%token<Ast.instr'> TEST
+%token<Ast.instr'> COMPARE
+%token<Ast.instr'> CONVERT
+%token<int option -> Memory.offset -> Ast.instr'> LOAD
+%token<int option -> Memory.offset -> Ast.instr'> STORE
%token<Memory.offset> OFFSET_EQ_NAT
%token<int> ALIGN_EQ_NAT
View
@@ -5,7 +5,7 @@
*
* x : var
* v : value
- * e : expr
+ * e : instrr
* f : func
* m : module_
*
@@ -73,19 +73,19 @@ type storeop = Memory.mem_size memop
type var = int Source.phrase
type literal = value Source.phrase
-type expr = expr' Source.phrase
-and expr' =
+type instr = instr' Source.phrase
+and instr' =
| Unreachable (* trap unconditionally *)
| Nop (* do nothing *)
| Drop (* forget a value *)
| Select (* branchless conditional *)
- | Block of expr list (* execute in sequence *)
- | Loop of expr list (* loop header *)
+ | Block of instr list (* execute in sequence *)
+ | Loop of instr list (* loop header *)
| Br of int * var (* break to n-th surrounding label *)
| BrIf of int * var (* conditional break *)
| BrTable of int * var list * var (* indexed break *)
| Return (* break from function body *)
- | If of expr list * expr list (* conditional *)
+ | If of instr list * instr list (* conditional *)
| Call of var (* call function *)
| CallImport of var (* call imported function *)
| CallIndirect of var (* call function through table *)
@@ -106,14 +106,14 @@ and expr' =
| GrowMemory (* grow linear memory *)
(* Administrative expressions *)
- | Trapping of string (* trap *)
- | Label of expr list * value list * expr list (* control stack *)
- | Local of int * value list * value list * expr list (* call stack *)
+ | Trapping of string (* trap *)
+ | Label of instr list * value list * instr list (* control stack *)
+ | Local of int * value list * value list * instr list (* call stack *)
(* Globals & Functions *)
-type const = expr list Source.phrase
+type const = instr list Source.phrase
type global = global' Source.phrase
and global' =
@@ -127,7 +127,7 @@ and func' =
{
ftype : var;
locals : value_type list;
- body : expr list;
+ body : instr list;
}
View
@@ -134,12 +134,12 @@ let check_result_arity r at =
| Bot -> ()
(*
- * check_expr : context -> expr_type_future -> expr -> unit
+ * check_instr : context -> instr -> stack_type -> unit
*
* Conventions:
* c : context
- * e : expr
- * es : expr list
+ * e : instr
+ * es : instr list
* v : value
* t : value_type var
* ts : stack_type
@@ -154,7 +154,7 @@ let peek_n n ts =
let m = min n (List.length ts) in
Lib.List.take m ts @ Lib.List.make (n - m) I32Type
-let rec check_expr (c : context) (e : expr) (stack : stack_type) : op_type =
+let rec check_instr (c : context) (e : instr) (stack : stack_type) : op_type =
match e.it with
| Unreachable ->
[] --> Bot
@@ -303,7 +303,7 @@ let rec check_expr (c : context) (e : expr) (stack : stack_type) : op_type =
error e.at "arity mismatch for local result"
| r' -> [] --> r'
-and check_block (c : context) (es : expr list) : result_type =
+and check_block (c : context) (es : instr list) : result_type =
match es with
| [] ->
Stack []
@@ -314,7 +314,7 @@ and check_block (c : context) (es : expr list) : result_type =
match r1 with
| Bot -> Bot
| Stack ts0 ->
- let ts2, r2 = check_expr c e (List.rev ts0) in
+ let ts2, r2 = check_instr c e (List.rev ts0) in
let n1 = max (List.length ts0 - List.length ts2) 0 in
let ts1 = Lib.List.take n1 ts0 in
let ts2' = Lib.List.drop n1 ts0 in
@@ -334,7 +334,7 @@ and check_block (c : context) (es : expr list) : result_type =
* c : context
* m : module_
* f : func
- * e : expr
+ * e : instr
* v : value
* t : value_type
* s : func_type
@@ -356,7 +356,8 @@ let is_const e =
| _ -> false
let check_const (c : context) (const : const) (t : value_type) =
- require (List.for_all is_const const.it) const.at "constant expression required";
+ require (List.for_all is_const const.it) const.at
+ "constant expression required";
match check_block c const.it with
| Stack [t'] when t = t' -> ()
| r -> result_error const.at (Stack [t]) r
View
@@ -126,16 +126,14 @@ let elem_type s =
| 0x20 -> AnyFuncType
| _ -> error s (pos s - 1) "invalid element type"
-let expr_type s = vec1 value_type s
-
let func_type s =
expect 0x40 s "invalid function type";
let ins = vec value_type s in
- let out = expr_type s in
- FuncType (ins, match out with None -> [] | Some t -> [t])
+ let out = vec value_type s in
+ FuncType (ins, out)
-(* Decode expressions *)
+(* Decode instructions *)
open Ast
open Operators
@@ -165,23 +163,23 @@ let args1 b stack s pos =
| [e], stack' -> Some e, stack'
| _ -> assert false
-let rec expr s =
+let rec instr s =
let pos = pos s in
match op s with
| 0x00 -> unreachable
| 0x01 ->
- let es' = expr_block s in
+ let es' = instr_block s in
expect 0x0f s "END opcode expected";
block es'
| 0x02 ->
- let es' = expr_block s in
+ let es' = instr_block s in
expect 0x0f s "END opcode expected";
loop es'
| 0x03 ->
- let es1 = expr_block s in
+ let es1 = instr_block s in
if peek s = Some 0x04 then begin
expect 0x04 s "`else` or `end` opcode expected";
- let es2 = expr_block s in
+ let es2 = instr_block s in
expect 0x0f s "END opcode expected";
if_ es1 es2
end else begin
@@ -394,18 +392,18 @@ let rec expr s =
| b -> error s pos "too few operands for operator"
-and expr_block s = List.rev (expr_block' s [])
-and expr_block' s es =
+and instr_block s = List.rev (instr_block' s [])
+and instr_block' s es =
if eos s then es else
match peek s with
| None | Some (0x04 | 0x0f) -> es
| _ ->
let pos = pos s in
- let e' = expr s in
- expr_block' s (Source.(e' @@ region s pos pos) :: es)
+ let e' = instr s in
+ instr_block' s (Source.(e' @@ region s pos pos) :: es)
let const s =
- let c = at expr_block s in
+ let c = at instr_block s in
expect 0x0f s "`end` opcode expected";
c
@@ -532,7 +530,7 @@ let local s =
let code s =
let locals = List.flatten (vec local s) in
let size = vu s in
- let body = expr_block (substream s (pos s + size)) in
+ let body = instr_block (substream s (pos s + size)) in
{locals; body; ftype = Source.((-1) @@ Source.no_region)}
let code_section s =
View
@@ -102,9 +102,9 @@ let elem c i t at =
(*
* Conventions:
* c : config
- * e : expr
+ * e : instr
* v : value
- * es : expr list
+ * es : instr list
* vs : value list
*)
@@ -118,8 +118,8 @@ let drop n (vs : value stack) at =
try Lib.List.drop n vs with Failure _ ->
Crash.error at "stack underflow"
-let rec step_expr (c : config) (vs : value stack) (e : expr)
- : value stack * expr list =
+let rec step_instr (c : config) (vs : value stack) (e : instr)
+ : value stack * instr list =
match e.it, vs with
| Unreachable, vs ->
vs, [Trapping "unreachable executed" @@ e.at]
@@ -283,7 +283,7 @@ let rec step_expr (c : config) (vs : value stack) (e : expr)
[], [Trapping msg @@ at]
| Label (es_cont, vs', e :: es), vs ->
- let vs'', es' = step_expr c vs' e in
+ let vs'', es' = step_instr c vs' e in
vs, [Label (es_cont, vs'', es' @ es) @@ e.at]
| Local (n, vs_local, vs', []), vs ->
@@ -301,19 +301,19 @@ let rec step_expr (c : config) (vs : value stack) (e : expr)
| Local (n, vs_local, vs', e :: es), vs ->
let c' = {c with locals = List.map ref vs_local; resources = c.resources - 1} in
- let vs'', es' = step_expr c' vs' e in
+ let vs'', es' = step_instr c' vs' e in
vs, [Local (n, List.map (!) c'.locals, vs'', es' @ es) @@ e.at]
| _, _ ->
Crash.error e.at "type error: missing or ill-typed operand on stack"
-let rec eval_block (c : config) (vs : value stack) (es : expr list) : value stack =
+let rec eval_block (c : config) (vs : value stack) (es : instr list) : value stack =
match es with
| [] -> vs
| [{it = Trapping msg; at}] -> Trap.error at msg
| e :: es ->
- let vs', es' = step_expr c vs e in
+ let vs', es' = step_instr c vs e in
eval_block c vs' (es' @ es)

0 comments on commit cf2d51d

Please sign in to comment.