Skip to content

Commit 340d9e5

Browse files
committed
AST: add predicated property to branch statement
1 parent de9528b commit 340d9e5

File tree

11 files changed

+45
-15
lines changed

11 files changed

+45
-15
lines changed

chb/ast/ASTApplicationInterface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2022-2023 Aarno Labs, LLC
7+
# Copyright (c) 2022-2025 Aarno Labs, LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -42,7 +42,7 @@
4242
from chb.ast.CustomASTSupport import CustomASTSupport
4343

4444

45-
pirversion: str = "0.1.0-20240703"
45+
pirversion: str = "0.1.0-202507024"
4646

4747

4848
class ASTApplicationInterface:

chb/ast/ASTDeserializer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,15 @@ def arg(ix: int) -> Dict[str, Any]:
751751
thenbranch = cast(AST.ASTStmt, mk_node(arg(1)))
752752
elsebranch = cast(AST.ASTStmt, mk_node(arg(2)))
753753
targetaddr = r["destination-addr"]
754+
predicated = r.get("predicated", None)
754755
nodes[id] = astree.mk_branch(
755756
condition,
756757
thenbranch,
757758
elsebranch,
758759
targetaddr,
759760
optstmtid=stmtid,
760-
optlocationid=locationid)
761+
optlocationid=locationid,
762+
predicated=predicated)
761763

762764
elif tag == "block":
763765
stmtid = r["stmtid"]

chb/ast/ASTNode.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,15 @@ def __init__(
595595
elsestmt: "ASTStmt",
596596
tgtaddress: str,
597597
mergeaddress: Optional[str],
598-
labels: List["ASTStmtLabel"] = []) -> None:
598+
labels: List["ASTStmtLabel"] = [],
599+
predicated: Optional[int] = None) -> None:
599600
ASTStmt.__init__(self, stmtid, locationid, labels, "if")
600601
self._cond = cond
601602
self._ifstmt = ifstmt
602603
self._elsestmt = elsestmt
603604
self._tgtaddress = tgtaddress
604605
self._mergeaddress = mergeaddress
606+
self._predicated = predicated
605607

606608
@property
607609
def is_ast_branch(self) -> bool:
@@ -627,6 +629,10 @@ def target_address(self) -> str:
627629
def merge_address(self) -> Optional[str]:
628630
return self._mergeaddress
629631

632+
@property
633+
def predicated(self) -> Optional[int]:
634+
return self._predicated
635+
630636
def accept(self, visitor: "ASTVisitor") -> None:
631637
visitor.visit_branch_stmt(self)
632638

chb/ast/ASTSerializer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2022-2024 Aarno Labs LLC
7+
# Copyright (c) 2022-2025 Aarno Labs LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -373,6 +373,8 @@ def index_branch_stmt(self, stmt: AST.ASTBranch) -> int:
373373
stmt.elsestmt.index(self)])
374374
node["destination-addr"] = stmt.target_address
375375
node["merge-addr"] = stmt.merge_address
376+
if stmt.predicated is not None:
377+
node["predicated"] = stmt.predicated
376378
return self.add(tags, args, node)
377379

378380
def index_instruction_sequence_stmt(self, stmt: AST.ASTInstrSequence) -> int:

chb/ast/ASTViewer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ def visit_loop_stmt(self, stmt: AST.ASTLoop) -> None:
170170

171171
def visit_branch_stmt(self, stmt: AST.ASTBranch) -> None:
172172
name = self.stmt_name(stmt)
173-
self.add_node(
174-
name, labeltxt="if:" + str(stmt.stmtid), color=nodecolors["stmt"])
173+
labeltxt = "if:" + str(stmt.stmtid)
174+
if stmt.predicated is not None:
175+
labeltxt += "\\npredicated:" + str(stmt.predicated)
176+
self.add_node(name, labeltxt=labeltxt, color=nodecolors["stmt"])
175177
self.add_edge(name, self.stmt_name(stmt.ifstmt), labeltxt="then")
176178
self.add_edge(name, self.stmt_name(stmt.elsestmt), labeltxt="else")
177179
self.add_edge(name, self.expr_name(stmt.condition), labeltxt="condition")

chb/ast/AbstractSyntaxTree.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,16 @@ def mk_branch(
421421
mergeaddr: Optional[str] = None,
422422
optstmtid: Optional[int] = None,
423423
optlocationid: Optional[int] = None,
424-
labels: List[AST.ASTStmtLabel] = []) -> AST.ASTBranch:
424+
labels: List[AST.ASTStmtLabel] = [],
425+
predicated: Optional[int] = None) -> AST.ASTBranch:
425426
stmtid = self.get_stmtid(optstmtid)
426427
locationid = self.get_locationid(optlocationid)
427428
if condition is None:
428429
# create a new unknown (uninitialized) variable
429430
condition = self.mk_tmp_lval_expression()
430431
return AST.ASTBranch(
431432
stmtid, locationid, condition, ifbranch, elsebranch,
432-
targetaddr, mergeaddr)
433+
targetaddr, mergeaddr, predicated=predicated)
433434

434435
def mk_goto_stmt(
435436
self,

chb/ast/doc/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on
66
[Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
### version 0.1.0-2025-07-24
10+
11+
- Add property predicated to branch stmt to indicate its origin from a predicated instruction
12+
- Add option to pir inspector to view a single statement
13+
14+
915
### version 0.1.0-2024-07-03
1016

1117
- Add printing of structs to ASTCPrettyPrinter

chb/astinterface/ASTICodeTransformer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def transform_branch_stmt(self, stmt: AST.ASTBranch) -> AST.ASTStmt:
123123
newelse,
124124
stmt.target_address,
125125
mergeaddr=stmt.merge_address,
126-
optlocationid=stmt.locationid)
126+
optlocationid=stmt.locationid,
127+
predicated=stmt.predicated)
127128

128129
def transform_switch_stmt(self, stmt: AST.ASTSwitchStmt) -> AST.ASTStmt:
129130
newcases = stmt.cases.transform(self)

chb/astinterface/ASTInterface.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,14 +699,16 @@ def mk_branch(
699699
elsebranch: AST.ASTStmt,
700700
targetaddr: str,
701701
mergeaddr: Optional[str] = None,
702-
optlocationid: Optional[int] = None) -> AST.ASTStmt:
702+
optlocationid: Optional[int] = None,
703+
predicated: Optional[int] = None) -> AST.ASTStmt:
703704
return self.astree.mk_branch(
704705
condition,
705706
ifbranch,
706707
elsebranch,
707708
targetaddr,
708709
mergeaddr=mergeaddr,
709-
optlocationid=optlocationid)
710+
optlocationid=optlocationid,
711+
predicated=predicated)
710712

711713
def mk_instr_sequence(
712714
self,

chb/astinterface/ASTInterfaceBasicBlock.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ def ast_fragment(
215215
else:
216216
astree.astree.add_expr_span(
217217
brcond.exprid, cinstr.iaddr, cinstr.bytestring)
218-
ifstmt = astree.mk_branch(brcond, thenstmt, elsestmt, cinstr.iaddr)
218+
instrcount = len(theninstrs) + len(elseinstrs)
219+
ifstmt = astree.mk_branch(
220+
brcond, thenstmt, elsestmt, cinstr.iaddr, predicated=instrcount)
219221
astree.add_stmt_span(ifstmt.locationid, spans)
220222
return ifstmt
221223
else:

0 commit comments

Comments
 (0)