Skip to content

Commit

Permalink
Allow multiple data segments
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewagner committed Aug 21, 2015
1 parent b2a3120 commit 60a90e7
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 deletions.
7 changes: 5 additions & 2 deletions ml-proto/src/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ and arm' =

(* Functions and Modules *)


type segment = Memory.segment Source.phrase

type func = func' Source.phrase
and func' =
{
Expand All @@ -122,8 +125,8 @@ type table = var list Source.phrase
type modul = modul' Source.phrase
and modul' =
{
memory : int64 * int64;
data : string;
memory : Memory.size * Memory.size;
data : segment list;
funcs : func list;
exports : export list;
tables : table list;
Expand Down
10 changes: 8 additions & 2 deletions ml-proto/src/check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,16 @@ let check_export c ex =
let {name = _; func = x} = ex.it in
ignore (func c x)

let check_data_segment memory prev_end s =
let mem_end = fst memory in
let seg_end = s.it.Memory.addr + String.length s.it.Memory.data in
require (s.it.Memory.addr >= prev_end) s.at "data section not disjoint and ordered";
require (mem_end >= seg_end) s.at "data section does not fit memory";
seg_end

let check_module m =
let {funcs; exports; tables; globals; memory; data} = m.it in
require (fst memory >= Int64.of_int (String.length data)) m.at
"data section does not fit memory";
ignore (List.fold_left (check_data_segment memory) 0 data);
let c = {c0 with funcs = List.map type_func funcs;
globals = List.map it globals} in
let c' = List.fold_left check_table c tables in
Expand Down
4 changes: 2 additions & 2 deletions ml-proto/src/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ and eval_func m f vs =

let init m =
let {Ast.funcs; exports; tables; globals; memory = (n, _); data} = m.it in
let memory = Memory.create (Int64.to_int n) in
Memory.init memory data;
let memory = Memory.create n in
Memory.init memory (List.map (fun seg -> seg.it) data);
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
Expand Down
20 changes: 15 additions & 5 deletions ml-proto/src/memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ open Bigarray
(* Types and view types *)

type address = int
type size = address
type alignment = Aligned | Unaligned
type mem_type =
| SInt8Mem | SInt16Mem | SInt32Mem | SInt64Mem
| UInt8Mem | UInt16Mem | UInt32Mem | UInt64Mem
| Float32Mem | Float64Mem
type segment =
{
addr : address;
data : string
}

type memory = (int, int8_unsigned_elt, c_layout) Array1.t
type t = memory
Expand Down Expand Up @@ -43,13 +49,17 @@ let create n =
Array1.fill mem 0;
mem

let init mem s =
if String.length s > Array1.dim mem then raise Bounds;
(* There currently is no way to blit from a string. *)
for i = 0 to String.length s - 1 do
(view mem : char_view).{i} <- s.[i]
let init_seg mem seg =
(*
* The Check.check_data_segment ensures seg is in bounds.
* There currently is no way to blit from a string.
*)
for i = 0 to String.length seg.data - 1 do
(view mem : char_view).{seg.addr + i} <- seg.data.[i]
done

let init mem segs =
List.iter (init_seg mem) segs

(* Alignment *)

Expand Down
11 changes: 9 additions & 2 deletions ml-proto/src/memory.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
type memory
type t = memory
type address = int
type size = address
type alignment = Aligned | Unaligned
type mem_type =
| SInt8Mem | SInt16Mem | SInt32Mem | SInt64Mem
| UInt8Mem | UInt16Mem | UInt32Mem | UInt64Mem
| Float32Mem | Float64Mem

type segment =
{
addr : address;
data : string
}

exception Bounds
exception Align
exception Address

val create : int -> memory
val init : memory -> string -> unit
val create : size -> memory
val init : memory -> segment list -> unit
val load : memory -> alignment -> address -> mem_type -> Values.value
val store : memory -> alignment -> address -> mem_type -> Values.value -> unit

Expand Down
16 changes: 10 additions & 6 deletions ml-proto/src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ bind_var :
| VAR { $1 @@ at() }
;
segment :
| INT TEXT { {Memory.addr = int_of_string $1; Memory.data = $2} @@ at() }
;
expr :
| LPAR oper RPAR { let at = at() in fun c -> $2 c @@ at }
;
Expand Down Expand Up @@ -258,8 +262,8 @@ export :
module_fields :
| /* empty */
{ fun c -> let memory = (Int64.zero, Int64.zero) in
{memory; data = ""; funcs = []; exports = []; globals = []; tables = []} }
{ fun c ->
{memory = (0, 0); data = []; funcs = []; exports = []; globals = []; tables = []} }
| func module_fields
{ fun c -> let f = $1 c in let m = $2 c in
{m with funcs = f () :: m.funcs} }
Expand All @@ -277,13 +281,13 @@ module_fields :
{m with tables = ($3 c func @@ ati 3) :: m.tables} }
| LPAR MEMORY INT INT RPAR module_fields
{ fun c -> let m = $6 c in
{m with memory = (Int64.of_string $3, Int64.of_string $4)} }
{m with memory = (int_of_string $3, int_of_string $4)} }
| LPAR MEMORY INT RPAR module_fields /* Sugar */
{ fun c -> let m = $5 c in
{m with memory = (Int64.of_string $3, Int64.of_string $3)} }
| LPAR DATA TEXT RPAR module_fields
{m with memory = (int_of_string $3, int_of_string $3)} }
| LPAR DATA segment RPAR module_fields
{ fun c -> let m = $5 c in
{m with data = $3 ^ m.data} }
{m with data = $3 :: m.data} }
;
modul :
| LPAR MODULE module_fields RPAR { $3 c0 @@ at() }
Expand Down
1 change: 0 additions & 1 deletion ml-proto/src/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type value_type = Int32Type | Int64Type | Float32Type | Float64Type
type expr_type = value_type list
type func_type = {ins : expr_type; outs : expr_type}


(* String conversion *)

let string_of_value_type = function
Expand Down
13 changes: 10 additions & 3 deletions ml-proto/test/memory.wasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

(module
(memory 1024)
(data "ABC\a7D")
(data 0 "ABC\a7D")
(data 20 "WASM")

;; Data section
(func $data (result i32)
(and.i32
(eq.i32 (getnearu.i8 (const.i32 0)) (const.i32 65))
(eq.i32 (getfaru.i8 (const.i64 3)) (const.i32 167))
(and.i32
(eq.i32 (getnearu.i8 (const.i32 0)) (const.i32 65))
(eq.i32 (getfaru.i8 (const.i64 3)) (const.i32 167))
)
(and.i32
(eq.i32 (getnearu.i8 (const.i32 20)) (const.i32 87))
(eq.i32 (getfaru.i8 (const.i64 23)) (const.i32 77))
)
)
)

Expand Down

0 comments on commit 60a90e7

Please sign in to comment.