Skip to content

Commit

Permalink
Print the instructions in a structured manner
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Nov 15, 2024
1 parent 23e14a2 commit 3bcecdf
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pywasm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def __init__(self, opcode: int, args: typing.List[typing.Any]) -> typing.Self:
self.args = args

def __repr__(self) -> str:
return self.into_single()

def into_single(self) -> str:
seps = [pywasm.opcode.name[self.opcode]]
if self.opcode in [pywasm.opcode.block, pywasm.opcode.loop, pywasm.opcode.if_then]:
seps.append(repr(self.args[0]))
Expand All @@ -241,6 +244,13 @@ def __repr__(self) -> str:
seps.append(repr(e))
return ' '.join(seps)

def into_disasm(self, indent: int) -> None:
prefix = ' ' * indent
pywasm.log.debugln(f'{prefix}{self.into_single()}')
if self.opcode in [pywasm.opcode.block, pywasm.opcode.loop, pywasm.opcode.if_then]:
for e in self.args[1]:
e.into_disasm(indent + 4)

@classmethod
def from_reader(cls, r: typing.BinaryIO) -> typing.Self:
o = Inst(ord(r.read(1)), [])
Expand Down Expand Up @@ -454,6 +464,10 @@ def __init__(self, data: typing.List[Inst]) -> typing.Self:
def __repr__(self) -> str:
return repr(self.data)

def into_disasm(self, indent: int) -> None:
for e in self.data:
e.into_disasm(indent)

@classmethod
def from_reader(cls, r: typing.BinaryIO) -> typing.Self:
s = []
Expand Down Expand Up @@ -1079,6 +1093,7 @@ def from_reader(cls, r: typing.BinaryIO):
func[i].locals = desc.locals
func[i].expr = desc.expr
pywasm.log.debugln(' ', i, desc)
desc.expr.into_disasm(8)
case 0x0b:
pywasm.log.debugln('section data')
for i in range(pywasm.leb128.u.decode_reader(section_reader)[0]):
Expand Down

0 comments on commit 3bcecdf

Please sign in to comment.