Fixed up signed const encoding, can now execute my old wat test by copy pasting it into the kraken code!
This commit is contained in:
67
comp_wasm.kp
67
comp_wasm.kp
@@ -4,48 +4,59 @@
|
|||||||
(_ _ out) (cond (!= (len *ARGV*) 3) (error "wrong number of params to comp_wasm (please provide out)")
|
(_ _ out) (cond (!= (len *ARGV*) 3) (error "wrong number of params to comp_wasm (please provide out)")
|
||||||
true *ARGV*)
|
true *ARGV*)
|
||||||
_ (println "out" out)
|
_ (println "out" out)
|
||||||
wasm_code (module
|
wasm_code
|
||||||
(import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
|
(module
|
||||||
|
(import "wasi_unstable" "path_open"
|
||||||
|
(func $path_open (param i32 i32 i32 i32 i32 i64 i64 i32 i32)
|
||||||
|
(result i32)))
|
||||||
|
(import "wasi_unstable" "fd_prestat_dir_name"
|
||||||
|
(func $fd_prestat_dir_name (param i32 i32 i32)
|
||||||
|
(result i32)))
|
||||||
|
(import "wasi_unstable" "fd_read"
|
||||||
|
(func $fd_read (param i32 i32 i32 i32)
|
||||||
|
(result i32)))
|
||||||
|
(import "wasi_unstable" "fd_write"
|
||||||
|
(func $fd_write (param i32 i32 i32 i32)
|
||||||
|
(result i32)))
|
||||||
(memory $mem 1)
|
(memory $mem 1)
|
||||||
(data (i32.const 16) "HellH") ;; adder to put, then data
|
(data (i32.const 16) "HellH") ;; adder to put, then data
|
||||||
(func $add (param $num i32) (result i32)
|
(func $start
|
||||||
(local $tmp1 i32)
|
|
||||||
(local $tmp2 i32)
|
|
||||||
(block $test
|
|
||||||
(i32.const 1337)
|
|
||||||
(i32.const 1338)
|
|
||||||
(i32.store (i32.const 8) (i32.const 16)) ;; adder of data
|
(i32.store (i32.const 8) (i32.const 16)) ;; adder of data
|
||||||
(i32.load (i32.const 8))
|
(i32.store (i32.const 12) (i32.const 5)) ;; len of data
|
||||||
(i64.store (i32.const 8) (i64.const 16)) ;; adder of data
|
;; open file
|
||||||
(i64.load (i32.const 8))
|
(call $path_open
|
||||||
drop
|
(i32.const 3) ;; file descriptor
|
||||||
drop
|
(i32.const 0) ;; lookup flags
|
||||||
drop
|
(i32.const 16) ;; path string *
|
||||||
(block $inner_test
|
(i32.load (i32.const 12)) ;; path string len
|
||||||
(br $inner_test)
|
(i32.const 1) ;; o flags
|
||||||
(br $test)
|
(i64.const 66) ;; base rights
|
||||||
|
(i64.const 66) ;; inheriting rights
|
||||||
|
(i32.const 0) ;; fdflags
|
||||||
|
(i32.const 4) ;; opened fd out ptr
|
||||||
)
|
)
|
||||||
(br $test)
|
|
||||||
drop
|
drop
|
||||||
|
|
||||||
(call $fd_write
|
(call $fd_read
|
||||||
(i32.const 0) ;; file descriptor
|
(i32.const 0) ;; file descriptor
|
||||||
(i32.const 8) ;; *iovs
|
(i32.const 8) ;; *iovs
|
||||||
(i32.const 1) ;; iovs_len
|
(i32.const 1) ;; iovs_len
|
||||||
|
(i32.const 12) ;; nwritten, overwrite buf len with it
|
||||||
|
)
|
||||||
|
drop
|
||||||
|
|
||||||
|
;; print name
|
||||||
|
(call $fd_write
|
||||||
|
(i32.load (i32.const 4)) ;; file descriptor
|
||||||
|
(i32.const 8) ;; *iovs
|
||||||
|
(i32.const 1) ;; iovs_len
|
||||||
(i32.const 4) ;; nwritten
|
(i32.const 4) ;; nwritten
|
||||||
)
|
)
|
||||||
drop
|
drop
|
||||||
)
|
)
|
||||||
(i32.const 11)
|
(export "memory" (memory $mem))
|
||||||
(local.get $tmp2)
|
(export "_start" (func $start))
|
||||||
i32.add
|
|
||||||
(i32.add (local.get $num))
|
|
||||||
)
|
)
|
||||||
; nothing
|
|
||||||
(export "add" (func $add))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
_ (write_file out (wasm_to_binary wasm_code))
|
_ (write_file out (wasm_to_binary wasm_code))
|
||||||
return_code 0
|
return_code 0
|
||||||
) return_code ))
|
) return_code ))
|
||||||
|
|||||||
24
wasm.kp
24
wasm.kp
@@ -3,14 +3,16 @@
|
|||||||
|
|
||||||
; Vectors and Values
|
; Vectors and Values
|
||||||
; Bytes encode themselves
|
; Bytes encode themselves
|
||||||
encode_u_LEB128 (rec-lambda recurse (x)
|
encode_LEB128_helper (rec-lambda recurse (allow_neg x)
|
||||||
(cond (< x 0x80) [x]
|
(cond (and allow_neg (< x 0x80)) [x]
|
||||||
true (cons (| (& x 0x7F) 0x80) (recurse (>> x 7))))
|
(< x 0x40) [x]
|
||||||
|
true (cons (| (& x 0x7F) 0x80) (recurse true (>> x 7))))
|
||||||
)
|
)
|
||||||
encode_s8_LEB128 (lambda (x) (encode_u_LEB128 (& x 0xFF)))
|
encode_u_LEB128 (lambda (x) (encode_LEB128_helper true x))
|
||||||
encode_s32_LEB128 (lambda (x) (encode_u_LEB128 (& x 0xFFFFFFFF)))
|
encode_s8_LEB128 (lambda (x) (encode_LEB128_helper (< x 0) (& x 0xFF)))
|
||||||
encode_s33_LEB128 (lambda (x) (encode_u_LEB128 (& x 0x1FFFFFFFF)))
|
encode_s32_LEB128 (lambda (x) (encode_LEB128_helper (< x 0) (& x 0xFFFFFFFF)))
|
||||||
encode_s64_LEB128 (lambda (x) (encode_u_LEB128 (& x 0xFFFFFFFFFFFFFFFF)))
|
encode_s33_LEB128 (lambda (x) (encode_LEB128_helper (< x 0) (& x 0x1FFFFFFFF)))
|
||||||
|
encode_s64_LEB128 (lambda (x) (encode_LEB128_helper (< x 0) (& x 0xFFFFFFFFFFFFFFFF)))
|
||||||
encode_vector (lambda (enc v)
|
encode_vector (lambda (enc v)
|
||||||
(concat (encode_u_LEB128 (len v)) (flat_map enc v) )
|
(concat (encode_u_LEB128 (len v)) (flat_map enc v) )
|
||||||
)
|
)
|
||||||
@@ -59,7 +61,7 @@
|
|||||||
(encode_name name)
|
(encode_name name)
|
||||||
(cond (= type 'func) (concat [0x00] (encode_u_LEB128 idx))
|
(cond (= type 'func) (concat [0x00] (encode_u_LEB128 idx))
|
||||||
(= type 'table) (concat [0x01] (error "can't encode table type"))
|
(= type 'table) (concat [0x01] (error "can't encode table type"))
|
||||||
(= type 'mem) (concat [0x02] (error "can't encode mem type"))
|
(= type 'memory) (concat [0x02] (error "can't encode memory type"))
|
||||||
(= type 'global) (concat [0x03] (error "can't encode global type"))
|
(= type 'global) (concat [0x03] (error "can't encode global type"))
|
||||||
true (error (str "bad import type" type))))
|
true (error (str "bad import type" type))))
|
||||||
)
|
)
|
||||||
@@ -80,7 +82,7 @@
|
|||||||
) (concat (encode_name name)
|
) (concat (encode_name name)
|
||||||
(cond (= type 'func) [0x00]
|
(cond (= type 'func) [0x00]
|
||||||
(= type 'table) [0x01]
|
(= type 'table) [0x01]
|
||||||
(= type 'mem) [0x02]
|
(= type 'memory) [0x02]
|
||||||
(= type 'global) [0x03]
|
(= type 'global) [0x03]
|
||||||
true (error "bad export type"))
|
true (error "bad export type"))
|
||||||
(encode_u_LEB128 idx)
|
(encode_u_LEB128 idx)
|
||||||
@@ -202,7 +204,9 @@
|
|||||||
(and (= nil pe) (= nil re) (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'result (idx (idx inside i) 0)))
|
(and (= nil pe) (= nil re) (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'result (idx (idx inside i) 0)))
|
||||||
; only one result possible
|
; only one result possible
|
||||||
(recurse (+ i 1) i (+ i 1))
|
(recurse (+ i 1) i (+ i 1))
|
||||||
(= nil pe) (recurse (+ i 1) i i)
|
(and (= nil re) (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'result (idx (idx inside i) 0)))
|
||||||
|
; only one result possible
|
||||||
|
(recurse (+ i 1) pe (+ i 1))
|
||||||
(and (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'local (idx (idx inside i) 0)))
|
(and (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'local (idx (idx inside i) 0)))
|
||||||
(recurse (+ i 1) pe re)
|
(recurse (+ i 1) pe re)
|
||||||
true [ (slice inside 0 (or pe 0)) (slice inside (or pe 0) (or re pe 0)) (slice inside (or re pe 0) i) (slice inside i -1) ]
|
true [ (slice inside 0 (or pe 0)) (slice inside (or pe 0) (or re pe 0)) (slice inside (or re pe 0) i) (slice inside i -1) ]
|
||||||
|
|||||||
Reference in New Issue
Block a user