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

DSL for module semantics #32

Merged
merged 50 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
152b2d0
Change instance representation as record
ShinWonho Jun 13, 2023
a1c51f9
Add limits check in grow reduction rules
ShinWonho Jun 13, 2023
5380d76
Add type field in eleminst
ShinWonho Jun 14, 2023
2b82dcc
Type check from updated store
Jun 15, 2023
6b6bd32
Consider free variables in premises of DefD
ShinWonho Jun 16, 2023
835bf0d
Add grow helper functions
ShinWonho Jun 16, 2023
7d757da
Fix typos
ShinWonho Jun 16, 2023
7aaff60
Fix typo in data segment
Jun 22, 2023
cecacb5
Draft for module semantics
Jun 22, 2023
2a158db
Add premises for updating store
Jun 22, 2023
066f8c3
Add premises for start function
Jun 22, 2023
6c7d379
Add export allocation
Jun 22, 2023
97d3fe6
Add import allocation
Jun 22, 2023
68c6067
Add 7-module.watsup
ShinWonho Jun 26, 2023
b8681db
Update TEST.md
ShinWonho Jun 26, 2023
9b3e6e3
Fix typo in instantiation
ShinWonho Jun 26, 2023
030a3a8
Update grow helper functions
ShinWonho Jun 28, 2023
d967107
Update spectec/src/frontend/elab.ml
ShinWonho Jun 28, 2023
8f6c590
Update spectec/src/il/validation.ml
ShinWonho Jun 28, 2023
0f48772
Refactor module semantics
ShinWonho Jun 29, 2023
35be3dd
Minor changes
Jun 29, 2023
edda75e
Minor changes
Jun 29, 2023
ff216fb
Update module semantics
Jun 30, 2023
21e0a94
Minor change
Jun 30, 2023
236de04
Minor change
ShinWonho Jul 3, 2023
1560490
Fix typo
ShinWonho Jul 4, 2023
e7a5252
Add iter with index
ShinWonho Jul 5, 2023
6bc75a6
Update elab and validation
ShinWonho Jul 5, 2023
506306e
Compute address in alloc func
ShinWonho Jul 7, 2023
eb57512
Minor changes
ShinWonho Jul 13, 2023
5aeb393
Add iter with an index
ShinWonho Jul 12, 2023
a453c69
Allow opt iter to have list iter type
ShinWonho Jul 7, 2023
88fe94b
Fix location of \land for conditions of funcdef
f52985 Jun 28, 2023
42b276f
Filter with recursive
ShinWonho Jul 14, 2023
65e6777
Minor change
ShinWonho Jul 14, 2023
edb1fbf
Merge two render_condition functions
ShinWonho Jul 19, 2023
8bb5fe0
Merge ListN
ShinWonho Jul 20, 2023
6872abe
Fix bug in iter with index
ShinWonho Jul 21, 2023
616eded
Minor changes
ShinWonho Jul 24, 2023
d764e97
Minor change
ShinWonho Jul 24, 2023
5c9aed2
Check iter with index
ShinWonho Jul 24, 2023
ba5b056
Minor changes
ShinWonho Jul 26, 2023
31213dd
Strip index variable in iter
ShinWonho Jul 26, 2023
474c65f
Add type check for index variable
ShinWonho Jul 26, 2023
384016d
Remove wrong assertion
ShinWonho Jul 26, 2023
fd8ec1f
Update spectec/src/frontend/elab.ml
ShinWonho Jul 26, 2023
97e95c3
Add validation for iter with index
ShinWonho Jul 26, 2023
0e45bef
Update spectec/src/il/validation.ml
ShinWonho Jul 26, 2023
199efd1
Minor change
ShinWonho Jul 26, 2023
cb45bce
Merge branch 'main' into module-semantics
ShinWonho Jul 26, 2023
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
2 changes: 1 addition & 1 deletion spectec/spec/1-syntax.watsup
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ syntax mem hint(desc "memory") =
syntax elem hint(desc "table segment") =
ELEM reftype expr* elemmode?
syntax data hint(desc "memory segment") =
DATA (byte*)* datamode?
DATA byte* datamode?
syntax start hint(desc "start function") =
START funcidx

Expand Down
2 changes: 1 addition & 1 deletion spectec/spec/3-typing.watsup
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ rule Elem_ok:
-- (Elemmode_ok: C |- elemmode : rt)?

rule Data_ok:
C |- DATA (b*)* datamode? : OK
C |- DATA b* datamode? : OK
-- (Datamode_ok: C |- datamode : OK)?

rule Elemmode_ok/active:
Expand Down
81 changes: 59 additions & 22 deletions spectec/spec/4-runtime.watsup
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,26 @@ def $default_(EXTERNREF) = (REF.NULL EXTERNREF) ;; TODO: All reference types sho

;; Configurations

syntax funcinst hint(desc "function instance") = moduleinst; func
syntax globalinst hint(desc "global instance") = val
syntax tableinst hint(desc "table instance") = ref*
syntax meminst hint(desc "memory instance") = byte*
syntax eleminst hint(desc "element instance") = ref*
syntax datainst hint(desc "data instance") = byte*
syntax exportinst hint(desc "export instance") = EXPORT name externval
syntax funcinst hint(desc "function instance") =
{ MODULE moduleinst,
CODE func }
syntax globalinst hint(desc "global instance") =
{ TYPE globaltype,
VALUE val }
syntax tableinst hint(desc "table instance") =
{ TYPE tabletype,
ELEM ref* }
syntax meminst hint(desc "memory instance") =
{ TYPE memtype,
DATA byte* }
syntax eleminst hint(desc "element instance") =
{ TYPE elemtype,
ELEM ref* }
syntax datainst hint(desc "data instance") =
{ DATA byte* }
syntax exportinst hint(desc "export instance") =
{ NAME name,
VALUE externval }

syntax store hint(desc "store") =
{ FUNC funcinst*,
Expand Down Expand Up @@ -103,7 +116,18 @@ def $funcaddr(state) : funcaddr* hint(show %.MODULE.FUNC)
def $funcaddr((s; f)) = f.MODULE.FUNC

def $funcinst(state) : funcinst* hint(show %.FUNC)
def $globalinst(state) : globalinst* hint(show %.GLOBAL)
def $tableinst(state) : tableinst* hint(show %.TABLE)
def $meminst(state) : meminst* hint(show %.MEM)
def $eleminst(state) : eleminst* hint(show %.ELEM)
def $datainst(state) : datainst* hint(show %.DATA)

def $funcinst((s; f)) = s.FUNC
def $globalinst((s; f)) = s.GLOBAL
def $tableinst((s; f)) = s.TABLE
def $meminst((s; f)) = s.MEM
def $eleminst((s; f)) = s.ELEM
def $datainst((s; f)) = s.DATA

def $func(state, funcidx) : funcinst hint(show %.FUNC#`[%])
def $global(state, globalidx) : globalinst hint(show %.GLOBAL#`[%])
Expand All @@ -123,23 +147,36 @@ def $local((s; f), x) = f.LOCAL[x]


def $with_local(state, localidx, val) : state hint(show %[.LOCAL[%] = %])
def $with_global(state, globalidx, val) : state hint(show %[.GLOBAL[%] = %])
def $with_table(state, tableidx, nat, ref) : state hint(show %[.TABLE[%][%] = %])
def $with_tableext(state, tableidx, ref*) : state hint(show %[.TABLE[%] =.. %])
def $with_mem(state, tableidx, nat, nat, byte*) : state hint(show %[.MEM[%][% : %] = %])
def $with_memext(state, tableidx, byte*) : state hint(show %[.MEM[%] =.. %])
def $with_elem(state, elemidx, ref*) : state hint(show %[.ELEM[%] = %])
def $with_data(state, dataidx, byte*) : state hint(show %[.DATA[%] = %])
def $with_global(state, globalidx, val) : state hint(show %[.GLOBAL[%].VALUE = %])
def $with_table(state, tableidx, nat, ref) : state hint(show %[.TABLE[%].ELEM[%] = %])
def $with_tableinst(state, tableidx, tableinst) : state hint(show %[.TABLE[%] = %])
def $with_mem(state, memidx, nat, nat, byte*) : state hint(show %[.MEM[%].DATA[% : %] = %])
def $with_meminst(state, memidx, meminst) : state hint(show %[.MEM[%] = %])
def $with_elem(state, elemidx, ref*) : state hint(show %[.ELEM[%].ELEM = %])
def $with_data(state, dataidx, byte*) : state hint(show %[.DATA[%].DATA = %])

def $with_local((s; f), x, v) = s; f[.LOCAL[x] = v]
def $with_global((s; f), x, v) = s[.GLOBAL[f.MODULE.GLOBAL[x]] = v]; f
def $with_table((s; f), x, i, r) = s[.TABLE[f.MODULE.TABLE[x]][i] = r]; f
def $with_tableext((s; f), x, r*) = s[.TABLE[f.MODULE.TABLE[x]] =.. r*]; f
def $with_mem((s; f), x, i, j, b*) = s[.MEM[f.MODULE.MEM[x]][i : j] = b*]; f
def $with_memext((s; f), x, b*) = s[.MEM[f.MODULE.MEM[x]] =.. b*]; f
def $with_elem((s; f), x, r*) = s[.ELEM[f.MODULE.ELEM[x]] = r*]; f
def $with_data((s; f), x, b*) = s[.DATA[f.MODULE.DATA[x]] = b*]; f

def $with_global((s; f), x, v) = s[.GLOBAL[f.MODULE.GLOBAL[x]].VALUE = v]; f
def $with_table((s; f), x, i, r) = s[.TABLE[f.MODULE.TABLE[x]].ELEM[i] = r]; f
def $with_tableinst((s; f), x, ti) = s[.TABLE[f.MODULE.TABLE[x]] = ti]; f
def $with_mem((s; f), x, i, j, b*) = s[.MEM[f.MODULE.MEM[x]].DATA[i : j] = b*]; f
def $with_meminst((s; f), x, mi) = s[.MEM[f.MODULE.MEM[x]] = mi]; f
def $with_elem((s; f), x, r*) = s[.ELEM[f.MODULE.ELEM[x]].ELEM = r*]; f
def $with_data((s; f), x, b*) = s[.DATA[f.MODULE.DATA[x]].DATA = b*]; f

def $grow_table(tableinst, nat, ref) : tableinst hint(partial)
def $grow_memory(meminst, nat) : meminst hint(partial)

def $grow_table(ti, n, r) = ti'
-- if ti = { TYPE `[i .. j] rt, ELEM r'* }
-- if i' = $(|r'*| + n)
-- if ti' = { TYPE `[i' .. j] rt, ELEM r'* r^n }
-- Tabletype_ok: |- ti'.TYPE : OK
def $grow_memory(mi, n) = mi'
-- if mi = { TYPE (`[i .. j] I8), DATA b* }
-- if i' = $(|b*| / (64 * $Ki) + n)
-- if mi' = { TYPE (`[i' .. j] I8), DATA b* 0^(n * 64 * $Ki) }
-- Memtype_ok: |- mi'.TYPE : OK

;; Administrative Instructions

Expand Down
57 changes: 30 additions & 27 deletions spectec/spec/6-reduction.watsup
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ rule Step_read/call:

rule Step_read/call_indirect-call:
z; (CONST I32 i) (CALL_INDIRECT x ft) ~> (CALL_ADDR a)
-- if $table(z, x)[i] = (REF.FUNC_ADDR a)
-- if $funcinst(z)[a] = m; FUNC ft' t* instr*
-- if $table(z, x).ELEM[i] = (REF.FUNC_ADDR a)
-- if $funcinst(z)[a].CODE = FUNC ft' t* instr*
-- if ft = ft'

rule Step_read/call_indirect-trap:
Expand All @@ -94,7 +94,8 @@ rule Step_read/call_indirect-trap:

rule Step_read/call_addr:
z; val^k (CALL_ADDR a) ~> (FRAME_ n `{f} (LABEL_ n `{epsilon} instr*))
-- if $funcinst(z)[a] = m; FUNC (t_1^k -> t_2^n) t* instr*
-- if $funcinst(z)[a] = {MODULE m, CODE func}
-- if func = FUNC (t_1^k -> t_2^n) t* instr*
-- if f = {LOCAL val^k ($default_(t))*, MODULE m}


Expand Down Expand Up @@ -172,44 +173,45 @@ rule Step_pure/local.tee:


rule Step_read/global.get:
z; (GLOBAL.GET x) ~> $global(z, x)
z; (GLOBAL.GET x) ~> $global(z, x).VALUE

rule Step/global.set:
z; val (GLOBAL.SET x) ~> $with_global(z, x, val); epsilon


rule Step_read/table.get-trap:
z; (CONST I32 i) (TABLE.GET x) ~> TRAP
-- if i >= |$table(z, x)|
-- if i >= |$table(z, x).ELEM|

rule Step_read/table.get-val:
z; (CONST I32 i) (TABLE.GET x) ~> $table(z,x)[i]
-- if i < |$table(z, x)|
z; (CONST I32 i) (TABLE.GET x) ~> $table(z,x).ELEM[i]
-- if i < |$table(z, x).ELEM|

rule Step/table.set-trap:
z; (CONST I32 i) ref (TABLE.GET x) ~> z; TRAP
-- if i >= |$table(z, x)|
-- if i >= |$table(z, x).ELEM|

rule Step/table.set-val:
z; (CONST I32 i) ref (TABLE.GET x) ~> $with_table(z, x, i, ref); epsilon
-- if i < |$table(z, x)|
-- if i < |$table(z, x).ELEM|


rule Step_read/table.size:
z; (TABLE.SIZE x) ~> (CONST I32 n)
-- if |$table(z, x)| = n ;; TODO: inline this
-- if |$table(z, x).ELEM| = n ;; TODO: inline this


rule Step/table.grow-succeed:
z; ref (CONST I32 n) (TABLE.GROW x) ~> $with_tableext(z, x, ref^n); (CONST I32 $(|$table(z, x)|))
z; ref (CONST I32 n) (TABLE.GROW x) ~> $with_tableinst(z, x, ti); (CONST I32 $(|$table(z, x).ELEM|))
-- if $grow_table($table(z, x), n, ref) = ti

rule Step/table.grow-fail:
z; ref (CONST I32 n) (TABLE.GROW x) ~> z; (CONST I32 $(-1))


rule Step_read/table.fill-trap:
z; (CONST I32 i) val (CONST I32 n) (TABLE.FILL x) ~> TRAP
-- if $(i + n) > |$table(z, x)|
-- if $(i + n) > |$table(z, x).ELEM|

rule Step_read/table.fill-zero:
z; (CONST I32 i) val (CONST I32 n) (TABLE.FILL x) ~> epsilon
Expand All @@ -225,7 +227,7 @@ rule Step_read/table.fill-succ:

rule Step_read/table.copy-trap:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (TABLE.COPY x y) ~> TRAP
-- if $(i + n) > |$table(z, y)| \/ $(j + n) > |$table(z, x)|
-- if $(i + n) > |$table(z, y).ELEM| \/ $(j + n) > |$table(z, x).ELEM|

rule Step_read/table.copy-zero:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (TABLE.COPY x y) ~> epsilon
Expand All @@ -248,7 +250,7 @@ rule Step_read/table.copy-gt:

rule Step_read/table.init-trap:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (TABLE.INIT x y) ~> TRAP
-- if $(i + n) > |$elem(z, y)| \/ $(j + n) > |$table(z, x)|
-- if $(i + n) > |$elem(z, y).ELEM| \/ $(j + n) > |$table(z, x).ELEM|

rule Step_read/table.init-zero:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (TABLE.INIT x y) ~> epsilon
Expand All @@ -257,7 +259,7 @@ rule Step_read/table.init-zero:

rule Step_read/table.init-succ:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (TABLE.INIT x y) ~>
(CONST I32 j) $elem(z,y)[i] (TABLE.SET x)
(CONST I32 j) $elem(z,y).ELEM[i] (TABLE.SET x)
(CONST I32 $(j+1)) (CONST I32 $(i+1)) (CONST I32 $(n-1)) (TABLE.INIT x y)
-- otherwise

Expand All @@ -268,32 +270,32 @@ rule Step/elem.drop:

rule Step_read/load-num-trap:
z; (CONST I32 i) (LOAD nt n_A n_O) ~> TRAP
-- if $(i + n_O + $size(nt)/8 > |$mem(z, 0)|)
-- if $(i + n_O + $size(nt)/8 > |$mem(z, 0).DATA|)

rule Step_read/load-num-val:
z; (CONST I32 i) (LOAD nt n_A n_O) ~> (CONST nt c)
-- if $bytes_($size(nt), c) = $mem(z, 0)[i + n_O : $size(nt)/8]
-- if $bytes_($size(nt), c) = $mem(z, 0).DATA[i + n_O : $size(nt)/8]

rule Step_read/load-pack-trap:
z; (CONST I32 i) (LOAD nt (n _ sx) n_A n_O) ~> TRAP
-- if $(i + n_O + n/8 > |$mem(z, 0)|)
-- if $(i + n_O + n/8 > |$mem(z, 0).DATA|)

rule Step_read/load-pack-val:
z; (CONST I32 i) (LOAD nt (n _ sx) n_A n_O) ~> (CONST nt $ext(n, $size(nt), sx, c))
-- if $bytes_(n, c) = $mem(z, 0)[i + n_O : n/8]
-- if $bytes_(n, c) = $mem(z, 0).DATA[i + n_O : n/8]


rule Step/store-num-trap:
z; (CONST I32 i) (CONST nt c) (STORE nt n_A n_O) ~> z; TRAP
-- if $(i + n_O + $size(nt)/8) > |$mem(z, 0)|
-- if $(i + n_O + $size(nt)/8) > |$mem(z, 0).DATA|

rule Step/store-num-val:
z; (CONST I32 i) (CONST nt c) (STORE nt n_A n_O) ~> $with_mem(z, 0, $(i + n_O), $($size(nt)/8), b*); epsilon
-- if b* = $bytes_($size(nt), c)

rule Step/store-pack-trap:
z; (CONST I32 i) (CONST nt c) (STORE nt n n_A n_O) ~> z; TRAP
-- if $(i + n_O + n/8) > |$mem(z, 0)|
-- if $(i + n_O + n/8) > |$mem(z, 0).DATA|

rule Step/store-pack-val:
z; (CONST I32 i) (CONST nt c) (STORE nt n n_A n_O) ~> $with_mem(z, 0, $(i + n_O), $(n/8), b*); epsilon
Expand All @@ -302,19 +304,20 @@ rule Step/store-pack-val:

rule Step_read/memory.size:
z; (MEMORY.SIZE) ~> (CONST I32 n)
-- if $(n * 64 * $Ki) = |$mem(z, 0)|
-- if $(n * 64 * $Ki) = |$mem(z, 0).DATA|


rule Step/memory.grow-succeed:
z; (CONST I32 n) (MEMORY.GROW) ~> $with_memext(z, 0, 0^(n * 64 * $Ki)); (CONST I32 $(|$mem(z, 0)| / (64 * $Ki)))
z; (CONST I32 n) (MEMORY.GROW) ~> $with_meminst(z, 0, mi); (CONST I32 $(|$mem(z, 0).DATA| / (64 * $Ki)))
-- if $grow_memory($mem(z, 0), n) = mi

rule Step/memory.grow-fail:
z; (CONST I32 n) (MEMORY.GROW) ~> z; (CONST I32 $(-1))


rule Step_read/memory.fill-trap:
z; (CONST I32 i) val (CONST I32 n) (MEMORY.FILL) ~> TRAP
-- if $(i + n) > |$mem(z, 0)|
-- if $(i + n) > |$mem(z, 0).DATA|

rule Step_read/memory.fill-zero:
z; (CONST I32 i) val (CONST I32 n) (MEMORY.FILL) ~> epsilon
Expand All @@ -330,7 +333,7 @@ rule Step_read/memory.fill-succ:

rule Step_read/memory.copy-trap:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (MEMORY.COPY) ~> TRAP
-- if $(i + n) > |$mem(z, 0)| \/ $(j + n) > |$mem(z, 0)|
-- if $(i + n) > |$mem(z, 0).DATA| \/ $(j + n) > |$mem(z, 0).DATA|

rule Step_read/memory.copy-zero:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (MEMORY.COPY) ~> epsilon
Expand All @@ -353,7 +356,7 @@ rule Step_read/memory.copy-gt:

rule Step_read/memory.init-trap:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (MEMORY.INIT x) ~> TRAP
-- if $(i + n) > |$data(z, x)| \/ $(j + n) > |$mem(z, 0)|
-- if $(i + n) > |$data(z, x).DATA| \/ $(j + n) > |$mem(z, 0).DATA|

rule Step_read/memory.init-zero:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (MEMORY.INIT x) ~> epsilon
Expand All @@ -362,7 +365,7 @@ rule Step_read/memory.init-zero:

rule Step_read/memory.init-succ:
z; (CONST I32 j) (CONST I32 i) (CONST I32 n) (MEMORY.INIT x) ~>
(CONST I32 j) (CONST I32 $data(z,x)[i]) (STORE I32 8 0 0)
(CONST I32 j) (CONST I32 $data(z,x).DATA[i]) (STORE I32 8 0 0)
(CONST I32 $(j+1)) (CONST I32 $(i+1)) (CONST I32 $(n-1)) (MEMORY.INIT x)
-- otherwise

Expand Down
Loading