Skip to content

Commit

Permalink
Use "extended" format in disassembly
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Nov 18, 2024
1 parent f5bc8ec commit 2518edc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
24 changes: 14 additions & 10 deletions control_flow/augment_disasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ class _ExtendedInstruction(NamedTuple):
arg: Optional numeric argument to operation (if any). Otherwise, None.
argval: resolved arg value (if known). Otherwise, the same as ``arg``.
argrepr: human-readable description of operation argument.
tos_str: If not None, a string representation of the top of the stack (TOS).
This is obtained by scanning previous instructions and
using information there and in their ``tos_str`` fields.
positions: Optional dis.Positions object holding the start and end locations that
are covered by this instruction. This not implemented yet.
Expand All @@ -140,6 +134,12 @@ class _ExtendedInstruction(NamedTuple):
start_offset: if not None the instruction with the lowest offset that
pushes a stack entry that is consume by this opcode
argrepr: human-readable description of operation argument.
tos_str: If not None, a string representation of the top of the stack (TOS).
This is obtained by scanning previous instructions and
using information there and in their ``tos_str`` fields.
"""

# Numeric code for operation
Expand All @@ -160,7 +160,7 @@ class _ExtendedInstruction(NamedTuple):
argval: Any

# String representation of argval if argval is not None.
argrepr: str
argrepr: Optional[str]

# Offset of the instruction
offset: int
Expand Down Expand Up @@ -204,17 +204,21 @@ class _ExtendedInstruction(NamedTuple):
# returns, raise, and unconditional jumps are not.
fallthrough: Optional[bool] = None

basic_block: BasicBlock = None
dominator: Optional[Node] = None

# NOTE: the following end fields have to match the corresponding end
# fields of an instruction. Disassembly create new instructions
# and assume the last fields are as the are in Instruction.

# If not None, a string representation of the top of the stack (TOS)
tos_str: Optional[str] = None

# Python expressions can be straight-line, operator like-basic block code that take
# items off a stack and push a value onto the stack. In this case, in a linear scan
# we can basically build up an expression tree.
# Note this has to be the last field. Code to set this assumes this.
start_offset: Optional[int] = None

basic_block: BasicBlock = None
dominator: Optional[Node] = None


EXTENDED_OPMAP = {
Expand Down
13 changes: 11 additions & 2 deletions control_flow/build_control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def build_and_analyze_control_flow(

assert cfg.graph is not None
try:
cfg.dom_tree = DominatorTree.compute_dominators_in_cfg(cfg, debug_dict.get("dom", False))
cfg.dom_tree = DominatorTree.compute_dominators_in_cfg(
cfg, debug_dict.get("dom", False)
)
for node in cfg.graph.nodes:
if node.bb.nesting_depth < 0:
node.is_dead_code = True
Expand Down Expand Up @@ -113,7 +115,14 @@ def build_and_analyze_control_flow(
print("=" * 30)
print("Augmented Instructions:")
for inst in augmented_instrs:
print(inst.disassemble(opc))
print(
inst.disassemble(
opc,
line_starts=linestarts,
asm_format="extended",
instructions=augmented_instrs,
)
)

# return cs_str
except Exception:
Expand Down

0 comments on commit 2518edc

Please sign in to comment.