Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove globals #71

Merged
merged 1 commit into from
Sep 17, 2015
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
4 changes: 0 additions & 4 deletions ml-proto/src/host/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ rule token = parse
| "get_local" { GETLOCAL }
| "set_local" { SETLOCAL }

| "load_global" { LOADGLOBAL }
| "store_global" { STOREGLOBAL }

| (nxx as t)".load" { LOAD (loadop t ' ' "") }
| (nxx as t)".load/"(align as a) { LOAD (loadop t ' ' a) }
| (ixx)".load"(width as w)"_"(sign as s) { LOAD (loadop ("i" ^ w) s "") }
Expand Down Expand Up @@ -246,7 +243,6 @@ rule token = parse
| "module" { MODULE }
| "memory" { MEMORY }
| "segment" { SEGMENT }
| "global" { GLOBAL }
| "import" { IMPORT }
| "export" { EXPORT }
| "table" { TABLE }
Expand Down
23 changes: 5 additions & 18 deletions ml-proto/src/host/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ module VarMap = Map.Make(String)
type space = {mutable map : int VarMap.t; mutable count : int}

type context =
{funcs : space; imports : space; globals : space; locals : space;
labels : int VarMap.t}
{funcs : space; imports : space; locals : space; labels : int VarMap.t}

let empty () = {map = VarMap.empty; count = 0}
let c0 () =
{funcs = empty (); imports = empty ();
globals = empty (); locals = empty ();
labels = VarMap.empty}
locals = empty (); labels = VarMap.empty}

let enter_func c =
assert (VarMap.is_empty c.labels);
Expand All @@ -66,7 +64,6 @@ let lookup category space x =

let func c x = lookup "function" c.funcs x
let import c x = lookup "import" c.imports x
let global c x = lookup "global" c.globals x
let local c x = lookup "local" c.locals x
let table c x = lookup "table" (empty ()) x
let label c x =
Expand All @@ -81,7 +78,6 @@ let bind category space x =

let bind_func c x = bind "function" c.funcs x
let bind_import c x = bind "import" c.imports x
let bind_global c x = bind "global" c.globals x
let bind_local c x = bind "local" c.locals x
let bind_label c x =
if VarMap.mem x.it c.labels then
Expand All @@ -92,17 +88,16 @@ let anon space n = space.count <- space.count + n

let anon_func c = anon c.funcs 1
let anon_import c = anon c.imports 1
let anon_globals c ts = anon c.globals (List.length ts)
let anon_locals c ts = anon c.locals (List.length ts)
let anon_label c = {c with labels = VarMap.map ((+) 1) c.labels}
%}

%token INT FLOAT TEXT VAR TYPE LPAR RPAR
%token NOP BLOCK IF LOOP LABEL BREAK SWITCH CASE FALLTHROUGH
%token CALL CALLIMPORT CALLINDIRECT RETURN
%token GETLOCAL SETLOCAL LOADGLOBAL STOREGLOBAL LOAD STORE
%token GETLOCAL SETLOCAL LOAD STORE
%token CONST UNARY BINARY COMPARE CONVERT
%token FUNC PARAM RESULT LOCAL MODULE MEMORY SEGMENT GLOBAL IMPORT EXPORT TABLE
%token FUNC PARAM RESULT LOCAL MODULE MEMORY SEGMENT IMPORT EXPORT TABLE
%token PAGESIZE MEMORYSIZE RESIZEMEMORY
%token ASSERTINVALID ASSERTEQ ASSERTFAULT INVOKE
%token EOF
Expand Down Expand Up @@ -182,8 +177,6 @@ oper :
| RETURN expr_opt { fun c -> Return ($2 c) }
| GETLOCAL var { fun c -> GetLocal ($2 c local) }
| SETLOCAL var expr { fun c -> SetLocal ($2 c local, $3 c) }
| LOADGLOBAL var { fun c -> LoadGlobal ($2 c global) }
| STOREGLOBAL var expr { fun c -> StoreGlobal ($2 c global, $3 c) }
| LOAD expr { fun c -> Load ($1, $2 c) }
| STORE expr expr { fun c -> Store ($1, $2 c, $3 c) }
| CONST literal { fun c -> Const (literal (ati 2) $2 $1) }
Expand Down Expand Up @@ -312,7 +305,7 @@ export :
module_fields :
| /* empty */
{ fun c ->
{imports = []; exports = []; globals = []; tables = []; funcs = [];
{imports = []; exports = []; tables = []; funcs = [];
memory = None} }
| func module_fields
{ fun c -> let f = $1 c in let m = $2 c in
Expand All @@ -323,12 +316,6 @@ module_fields :
| export module_fields
{ fun c -> let m = $2 c in
{m with exports = $1 c :: m.exports} }
| LPAR GLOBAL value_type_list RPAR module_fields
{ fun c -> anon_globals c $3; let m = $5 c in
{m with globals = $3 @ m.globals} }
| LPAR GLOBAL bind_var value_type RPAR module_fields /* Sugar */
{ fun c -> bind_global c $3; let m = $6 c in
{m with globals = $4 :: m.globals} }
| LPAR TABLE var_list RPAR module_fields
{ fun c -> let m = $5 c in
{m with tables = ($3 c func @@ ati 3) :: m.tables} }
Expand Down
6 changes: 1 addition & 5 deletions ml-proto/src/host/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ let print_table_sig prefix i t_opt =
let print_func i f =
print_func_sig "func" i f

let print_global i t =
print_var_sig "global" i t

let print_export m i ex =
print_export_sig "export" ex.it.name (List.nth m.it.funcs ex.it.func.it)

Expand All @@ -53,9 +50,8 @@ let print_table m i tab =


let print_module m =
let {funcs; globals; exports; tables} = m.it in
let {funcs; exports; tables} = m.it in
List.iteri print_func funcs;
List.iteri print_global globals;
List.iteri (print_export m) exports;
List.iteri (print_table m) tables;
flush_all ()
Expand Down
3 changes: 0 additions & 3 deletions ml-proto/src/spec/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ and expr' =
| Return of expr option
| GetLocal of var
| SetLocal of var * expr
| LoadGlobal of var
| StoreGlobal of var * expr
| Load of loadop * expr
| Store of storeop * expr * expr
| Const of literal
Expand Down Expand Up @@ -151,5 +149,4 @@ and modul' =
imports : import list;
exports : export list;
tables : table list;
globals : value_type list
}
16 changes: 3 additions & 13 deletions ml-proto/src/spec/check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ type context =
{
funcs : func_type list;
imports : func_type list;
globals : value_type list;
tables : func_type list;
locals : value_type list;
return : expr_type;
labels : expr_type list
}

let c0 =
{funcs = []; imports = []; globals = []; tables = [];
{funcs = []; imports = []; tables = [];
locals = []; return = None; labels = []}

let lookup category list x =
Expand All @@ -37,7 +36,6 @@ let lookup category list x =
let func c x = lookup "function" c.funcs x
let import c x = lookup "import" c.imports x
let local c x = lookup "local" c.locals x
let global c x = lookup "global" c.globals x
let table c x = lookup "table" c.tables x
let label c x = lookup "label" c.labels x

Expand Down Expand Up @@ -184,13 +182,6 @@ let rec check_expr c et e =
check_expr c (Some (local c x)) e1;
check_type None et e.at

| LoadGlobal x ->
check_type (Some (global c x)) et e.at

| StoreGlobal (x, e1) ->
check_expr c (Some (global c x)) e1;
check_type None et e.at

| Load (loadop, e1) ->
check_align loadop.align e.at;
check_expr c (Some Int32Type) e1;
Expand Down Expand Up @@ -311,11 +302,10 @@ let check_memory memory =
ignore (List.fold_left (check_segment memory.it.initial) 0 memory.it.segments)

let check_module m =
let {imports; exports; globals; tables; funcs; memory} = m.it in
let {imports; exports; tables; funcs; memory} = m.it in
Lib.Option.app check_memory memory;
let c = {c0 with funcs = List.map type_func funcs;
imports = List.map type_import imports;
globals = List.map it globals} in
imports = List.map type_import imports} in
let c' = List.fold_left check_table c tables in
List.iter (check_func c') funcs;
ignore (List.fold_left (check_export c') NameSet.empty exports)
23 changes: 4 additions & 19 deletions ml-proto/src/spec/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type instance =
imports : import list;
exports : export_map;
tables : func list list;
globals : value ref list;
memory : Memory.t;
host : host_params
}
Expand All @@ -52,7 +51,6 @@ let lookup category list x =

let func c x = lookup "function" c.modul.funcs x
let import c x = lookup "import" c.modul.imports x
let global c x = lookup "global" c.modul.globals x
let table c x y = lookup "entry" (lookup "table" c.modul.tables x) y
let local c x = lookup "local" c.locals x
let label c x = lookup "label" c.labels x
Expand Down Expand Up @@ -160,14 +158,6 @@ let rec eval_expr (c : config) (e : expr) =
local c x := v1;
None

| LoadGlobal x ->
Some !(global c x)

| StoreGlobal (x, e1) ->
let v1 = some (eval_expr c e1) e1.at in
global c x := v1;
None

| Load ({mem; ext; align = _}, e1) ->
let v1 = some (eval_expr c e1) e1.at in
(try Some (Memory.load c.modul.memory (Memory.address_of_value v1) mem ext)
Expand Down Expand Up @@ -237,13 +227,10 @@ and eval_arm c vo stage arm =
| `Seek, false | `Done _, _ ->
stage

and eval_decl t =
ref (default_value t.it)

and eval_func (m : instance) (f : func) (evs : value list) =
let module Return = MakeLabel () in
let args = List.map ref evs in
let vars = List.map eval_decl f.it.locals in
let vars = List.map (fun t -> ref (default_value t.it)) f.it.locals in
let locals = args @ vars in
let c = {modul = m; locals; labels = []; return = Return.label} in
try eval_expr c f.it.body
Expand All @@ -265,14 +252,13 @@ let init m imports host =
assert (List.length imports = List.length m.it.Ast.imports);
assert (host.page_size > 0);
assert (Lib.Int.is_power_of_two host.page_size);
let {Ast.exports; globals; tables; funcs; memory; _} = m.it in
let {Ast.exports; tables; funcs; memory; _} = m.it in
let mem = init_memory memory in
let func x = List.nth funcs x.it in
let export ex = ExportMap.add ex.it.name (func ex.it.func) in
let exports = List.fold_right export exports ExportMap.empty in
let tables = List.map (fun tab -> List.map func tab.it) tables in
let globals = List.map eval_decl globals in
{funcs; imports; exports; tables; globals; memory = mem; host}
{funcs; imports; exports; tables; memory = mem; host}

let invoke m name vs =
let f = export m (name @@ no_region) in
Expand All @@ -284,6 +270,5 @@ let eval e =
let memory = Memory.create 0 in
let exports = ExportMap.singleton "eval" f in
let host = {page_size = 1} in
let m = {imports = []; exports; globals = []; tables = []; funcs = [f];
memory; host} in
let m = {imports = []; exports; tables = []; funcs = [f]; memory; host} in
eval_func m f []
3 changes: 0 additions & 3 deletions ml-proto/test/forward.wasm
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
)

(func $odd (param $n i32) (result i32)
(store_global $scratch (get_local $n))
(if (i32.eq (get_local $n) (i32.const 0))
(i32.const 0)
(call $even (i32.sub (get_local $n) (i32.const 1)))
)
)

(global $scratch i32)
)

(assert_eq (invoke "even" (i32.const 13)) (i32.const 0))
Expand Down