diff --git a/comp_wasm.kp b/comp_wasm.kp index c3510ac..387c516 100644 --- a/comp_wasm.kp +++ b/comp_wasm.kp @@ -21,7 +21,7 @@ (memory $mem 1) (global $gi i32 (i32.const 8)) (global $gb (mut i64) (i64.const 9)) - (table $tab 8 funcref) + (table $tab 2 funcref) ;(table $tab2 8 16 funcref) (data (i32.const 16) "HellH") ;; adder to put, then data (func $start @@ -82,6 +82,7 @@ ) drop ) + (elem (i32.const 0) $start $start) (export "memory" (memory $mem)) (export "_start" (func $start)) (start $start) diff --git a/wasm.kp b/wasm.kp index a011af9..b234a6c 100644 --- a/wasm.kp +++ b/wasm.kp @@ -187,6 +187,14 @@ ) (concat [0x06] (encode_u_LEB128 (len encoded)) encoded )) ) + ; only supporting one type of element section for now, active funcrefs with offset + encode_element (lambda (x) (concat [0x00] (encode_expr (idx x 0)) (encode_vector encode_u_LEB128 (idx x 1)))) + encode_element_section (lambda (x) + (let ( + encoded (encode_vector encode_element x) + ) (concat [0x09] (encode_u_LEB128 (len encoded)) encoded )) + ) + encode_data (lambda (data) (cond (= 2 (len data)) (concat [0x00] (encode_expr (idx data 0)) (encode_bytes (idx data 1))) (= 1 (len data)) (concat [0x01] (encode_bytes (idx data 0))) (= 3 (len data)) (concat [0x02] (encode_u_LEB128 (idx data 0)) (encode_expr (idx data 1)) (encode_bytes (idx data 2))) @@ -200,8 +208,8 @@ wasm_to_binary (lambda (wasm_code) (let ( - (type_section import_section function_section table_section memory_section global_section export_section start_section code_section data_section) wasm_code - _ (println "type_section" type_section "import_section" import_section "function_section" function_section "memory_section" memory_section "global_section" global_section "export_section" export_section "start_section" start_section "code_section" code_section "data_section" data_section) + (type_section import_section function_section table_section memory_section global_section export_section start_section element_section code_section data_section) wasm_code + _ (println "type_section" type_section "import_section" import_section "function_section" function_section "memory_section" memory_section "global_section" global_section "export_section" export_section "start_section" start_section "element_section" element_section "code_section" code_section "data_section" data_section) magic [ 0x00 0x61 0x73 0x6D ] version [ 0x01 0x00 0x00 0x00 ] type (encode_type_section type_section) @@ -212,28 +220,29 @@ global (encode_global_section global_section) export (encode_export_section export_section) start (encode_start_section start_section) + elem (encode_element_section element_section) code (encode_code_section code_section) data (encode_data_section data_section) ;data_count (let (body (encode_u_LEB128 (len data_section))) (concat [0x0C] (encode_u_LEB128 (len body)) body)) data_count [] - ) (concat magic version type import function table memory global export data_count start code data)) + ) (concat magic version type import function table memory global export data_count start elem code data)) ) module (lambda (& args) (let ( - helper (rec-lambda recurse (entries i name_dict type import function table memory global export start code data) - (if (= i (len entries)) [ type import function table memory global export start code data] + helper (rec-lambda recurse (entries i name_dict type import function table memory global export start elem code data) + (if (= i (len entries)) [ type import function table memory global export start elem code data] (let ( - (n_d t im f ta m g e s c d) ((idx entries i) name_dict type import function table memory global export start code data) - ) (recurse entries (+ i 1) n_d t im f ta m g e s c d)))) - ) (helper args 0 empty_dict [] [] [] [] [] [] [] [] [] []))) + (n_d t im f ta m g e s elm c d) ((idx entries i) name_dict type import function table memory global export start elem code data) + ) (recurse entries (+ i 1) n_d t im f ta m g e s elm c d)))) + ) (helper args 0 empty_dict [] [] [] [] [] [] [] [] [] [] []))) - table (vau de (idx_name & limits_type) (lambda (name_dict type import function table memory global export start code data) - [ (put name_dict idx_name (len table)) type import function (concat table [[ (idx limits_type -1) (map (lambda (x) (eval x de)) (slice limits_type 0 -2)) ]]) memory global export start code data ])) + table (vau de (idx_name & limits_type) (lambda (name_dict type import function table memory global export start elem code data) + [ (put name_dict idx_name (len table)) type import function (concat table [[ (idx limits_type -1) (map (lambda (x) (eval x de)) (slice limits_type 0 -2)) ]]) memory global export start elem code data ])) - memory (vau de (idx_name & limits) (lambda (name_dict type import function table memory global export start code data) - [ (put name_dict idx_name (len memory)) type import function table (concat memory [(map (lambda (x) (eval x de)) limits)]) global export start code data ])) + memory (vau de (idx_name & limits) (lambda (name_dict type import function table memory global export start elem code data) + [ (put name_dict idx_name (len memory)) type import function table (concat memory [(map (lambda (x) (eval x de)) limits)]) global export start elem code data ])) - func (vau de (name & inside) (lambda (name_dict type import function table memory global export start code data) + func (vau de (name & inside) (lambda (name_dict type import function table memory global export start elem code data) (let ( (params result locals body) ((rec-lambda recurse (i pe re) (cond (and (= nil pe) (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'param (idx (idx inside i) 0))) @@ -290,6 +299,8 @@ export ; start start + ; element + elem ; code (concat code [ [ compressed_locals our_code ] ]) ; data @@ -333,35 +344,39 @@ rest (flat_eval_ins flatten de) ) (concat rest [['br_if block_val]]))) call (lambda (f & flatten) (concat (flat_map (lambda (x) x) flatten) [['call f]])) - import (vau de (mod_name name t_idx_typ) (lambda (name_dict type import function table memory global export start code data) (let ( + import (vau de (mod_name name t_idx_typ) (lambda (name_dict type import function table memory global export start elem code data) (let ( _ (if (!= 'func (idx t_idx_typ 0)) (error "only supporting importing functions rn")) (import_type idx_name param_type result_type) t_idx_typ actual_type_idx (len type) actual_type [ (slice param_type 1 -1) (slice result_type 1 -1) ] ) - [ (put name_dict idx_name (len function)) (concat type [actual_type]) (concat import [ [mod_name name import_type actual_type_idx] ]) (concat function [nil]) table memory global export start code data ]) + [ (put name_dict idx_name (len function)) (concat type [actual_type]) (concat import [ [mod_name name import_type actual_type_idx] ]) (concat function [nil]) table memory global export start elem code data ]) )) - global (vau de (idx_name global_type expr) (lambda (name_dict type import function table memory global export start code data) + global (vau de (idx_name global_type expr) (lambda (name_dict type import function table memory global export start elem code data) [ (put name_dict idx_name (len global)) type import function table memory (concat global [[(if (array? global_type) (reverse global_type) [global_type 'const]) (eval expr de) ]]) - export start code data ] + export start elem code data ] )) - export (vau de (name t_v) (lambda (name_dict type import function table memory global export start code data) - [ name_dict type import function table memory global (concat export [ [ name (idx t_v 0) (get-value name_dict (idx t_v 1)) ] ]) start code data ] + export (vau de (name t_v) (lambda (name_dict type import function table memory global export start elem code data) + [ name_dict type import function table memory global (concat export [ [ name (idx t_v 0) (get-value name_dict (idx t_v 1)) ] ]) start elem code data ] )) - start (vau de (name) (lambda (name_dict type import function table memory global export start code data) - [ name_dict type import function table memory global export (concat start [(get-value name_dict name)]) code data ] + start (vau de (name) (lambda (name_dict type import function table memory global export start elem code data) + [ name_dict type import function table memory global export (concat start [(get-value name_dict name)]) elem code data ] )) - data (lambda (& it) (lambda (name_dict type import function table memory global export start code data) - [name_dict type import function table memory global export start code (concat data [it])])) + elem (vau de (offset & entries) (lambda (name_dict type import function table memory global export start elem code data) + [ name_dict type import function table memory global export start (concat elem [[(eval offset de) (map (lambda (x) (get-value name_dict x)) entries)]]) code data ] + )) + + data (lambda (& it) (lambda (name_dict type import function table memory global export start elem code data) + [name_dict type import function table memory global export start elem code (concat data [it])])) ) (provide wasm_to_binary - module import table memory start func global export data + module import table memory start elem func global export data drop i32.const i64.const local.get i32.add i32.load i64.load i32.store i64.store