diff --git a/src/sections.inc b/src/sections.inc index 77ecae6..e6ca116 100644 --- a/src/sections.inc +++ b/src/sections.inc @@ -47,6 +47,15 @@ parse_sections: call print_str pop esi + mov eax, [sec_id] + cmp eax, SECT_TYPE + je .type + jmp .skip + +.type: + call parse_type_section + +.skip: mov esi, [sec_next] jmp .next @@ -66,3 +75,133 @@ print_sec_name: .unknown: pop eax esi ret + +MAX_TYPES = 64 + +parse_type_section: + pushad + mov esi, [sec_content] + + call decode_leb128_u32 + mov [type_count], eax + mov ecx, eax + xor ebx, ebx + +.next_type: + test ecx, ecx + jz .types_done + + cmp byte [esi], 0x60 + jne .types_done + inc esi + + push ecx + call decode_leb128_u32 + mov [type_param_counts + ebx*4], eax + mov [type_param_ptrs + ebx*4], esi + mov ecx, eax + + push esi ecx + mov esi, msg_indent + call print_str + mov esi, msg_type_word + call print_str + mov eax, ebx + call print_dec + mov esi, msg_colon + call print_str + mov esi, msg_oparen + call print_str + pop ecx esi + + push ecx +.print_param: + test ecx, ecx + jz .params_printed + movzx eax, byte [esi] + inc esi + push esi ecx + call print_valtype + pop ecx esi + dec ecx + test ecx, ecx + jz .params_printed + push esi + mov esi, msg_comma + call print_str + pop esi + jmp .print_param +.params_printed: + pop ecx + + push esi + mov esi, msg_arrow_paren + call print_str + pop esi + + call decode_leb128_u32 + mov [type_result_counts + ebx*4], eax + mov [type_result_ptrs + ebx*4], esi + mov ecx, eax + + push ecx +.print_result: + test ecx, ecx + jz .results_printed + movzx eax, byte [esi] + inc esi + push esi ecx + call print_valtype + pop ecx esi + dec ecx + test ecx, ecx + jz .results_printed + push esi + mov esi, msg_comma + call print_str + pop esi + jmp .print_result +.results_printed: + pop ecx + + push esi + mov esi, msg_cparen_nl + call print_str + pop esi + pop ecx + + inc ebx + dec ecx + jmp .next_type + +.types_done: + popad + ret + +print_valtype: + push esi + cmp al, 0x7F + je .i32 + cmp al, 0x7E + je .i64 + cmp al, 0x7D + je .f32 + cmp al, 0x7C + je .f64 + mov esi, msg_vt_unknown + jmp .vt_print +.i32: + mov esi, msg_vt_i32 + jmp .vt_print +.i64: + mov esi, msg_vt_i64 + jmp .vt_print +.f32: + mov esi, msg_vt_f32 + jmp .vt_print +.f64: + mov esi, msg_vt_f64 +.vt_print: + call print_str + pop esi + ret diff --git a/src/wasm.asm b/src/wasm.asm index 907ab1a..3aa4b14 100644 --- a/src/wasm.asm +++ b/src/wasm.asm @@ -207,6 +207,19 @@ msg_newline db 10, 0 msg_sec_hdr db 'Section ', 0 msg_sec_comma_sz db ', size: ', 0 +msg_indent db ' ', 0 +msg_type_word db 'type ', 0 +msg_colon db ': ', 0 +msg_comma db ', ', 0 +msg_oparen db '(', 0 +msg_cparen_nl db ')', 10, 0 +msg_arrow_paren db ') -> (', 0 + +msg_vt_i32 db 'i32', 0 +msg_vt_i64 db 'i64', 0 +msg_vt_f32 db 'f32', 0 +msg_vt_f64 db 'f64', 0 +msg_vt_unknown db '?', 0 sec_name_tbl dd sn_custom, sn_type, sn_import, sn_func, sn_table dd sn_memory, sn_global, sn_export, sn_start, sn_element @@ -237,4 +250,10 @@ sec_size dd 0 sec_content dd 0 sec_next dd 0 +type_count dd 0 +type_param_counts rd MAX_TYPES +type_result_counts rd MAX_TYPES +type_param_ptrs rd MAX_TYPES +type_result_ptrs rd MAX_TYPES + file_buf rb FILE_BUF_SIZE diff --git a/test/run_tests.sh b/test/run_tests.sh index 61f2697..085dfc2 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -29,6 +29,8 @@ check "valid wasm" "Valid wasm file" $WASM test/wasm/valid.wasm check "bad magic" "invalid wasm magic" $WASM test/wasm/badmagic.wasm check "bad version" "unsupported wasm" $WASM test/wasm/badver.wasm check "section list" "(Type)" $WASM test/wasm/valid.wasm +check "type no params" "() -> (i32)" $WASM test/wasm/valid.wasm +check "type two params" "(i32, i32) -> (i32)" $WASM test/wasm/add.wasm echo "" echo "Results: $PASS/$TESTS passed, $FAIL failed" diff --git a/test/wasm/add.wasm b/test/wasm/add.wasm new file mode 100644 index 0000000..357f72d Binary files /dev/null and b/test/wasm/add.wasm differ diff --git a/test/wat/add.wat b/test/wat/add.wat new file mode 100644 index 0000000..cc85eb3 --- /dev/null +++ b/test/wat/add.wat @@ -0,0 +1,6 @@ +(module + (func (export "add") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.add) +)