Skip to content

Commit

Permalink
Add translating of Type Section to WAT
Browse files Browse the repository at this point in the history
  • Loading branch information
berendbutje committed Jan 4, 2024
1 parent f7a74d2 commit 5cb4e3e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/splat/segtypes/wasm/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from ..common.bin import CommonSegBin

from wasm_tob import Section
from wasm_tob import Section, SEC_TYPE, TypeSection, FuncType
from .wat import type_section_to_wat


class WasmSegTypes(CommonSegBin):
Expand All @@ -26,7 +27,10 @@ def split(self, rom_bytes: bytes):
sec = Section()
sec_len, sec_data, _ = sec.from_raw(None, raw)

print(f"{sec_len} {sec_data}")
if sec_data.id != SEC_TYPE:
# TODO: handle invalidly assigned sections.
pass

with open(out_path, "wb") as f:
f.write(raw) # syke
type_section: TypeSection = sec_data.payload
with open(out_path, "w") as f:
f.write(type_section_to_wat(type_section)) # syke
27 changes: 27 additions & 0 deletions src/splat/segtypes/wasm/wat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from wasm_tob import (
Section,
SEC_TYPE,
TypeSection,
FuncType,
ValueTypeField,
format_lang_type,
)


def func_type_to_wat(index: int, func: FuncType) -> str:
params = ""
if func.param_count > 0:
vals = " ".join(map(format_lang_type, func.param_types))
params += f" (param {vals})"

returns = ""
if func.return_count:
returns += f" (result {format_lang_type(func.return_type)})"

return f"(type (;{index};) (func{params}{returns}))"


def type_section_to_wat(section: TypeSection) -> str:
return "\n".join(
func_type_to_wat(index, entry) for index, entry in enumerate(section.entries)
)

0 comments on commit 5cb4e3e

Please sign in to comment.