Skip to content

Commit 5a8df31

Browse files
committed
ASTI: prune empty if statements
1 parent 29a53d3 commit 5a8df31

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

chb/astinterface/ASTICodeTransformer.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ def transform_loop_stmt(self, stmt: AST.ASTLoop) -> AST.ASTStmt:
6868
optlocationid=stmt.locationid)
6969

7070
def transform_block_stmt(self, stmt: AST.ASTBlock) -> AST.ASTStmt:
71-
newstmts = [s.transform(self) for s in stmt.stmts]
71+
newstmts: List[AST.ASTStmt] = []
72+
for s in stmt.stmts:
73+
newstmt = s.transform(self)
74+
# prune empty blocks that may have been created by the pruning
75+
# of redundant if statements
76+
if newstmt.is_ast_block and len((cast(AST.ASTBlock, newstmt)).stmts) == 0:
77+
continue
78+
newstmts.append(newstmt)
79+
7280
return self.astinterface.mk_block(
7381
newstmts,
7482
labels=stmt.labels,
@@ -117,6 +125,9 @@ def transform_instruction_sequence_stmt(
117125
def transform_branch_stmt(self, stmt: AST.ASTBranch) -> AST.ASTStmt:
118126
newif = stmt.ifstmt.transform(self)
119127
newelse = stmt.elsestmt.transform(self)
128+
if newif.is_empty() and newelse.is_empty():
129+
return self.astinterface.mk_block([])
130+
120131
return self.astinterface.mk_branch(
121132
stmt.condition,
122133
newif,

chb/astinterface/ASTIProvenance.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ def resolve_reaching_defs(self) -> None:
428428
v = str(rd.variable)
429429
addrs = [str(d) for d in rd.deflocations]
430430
for addr in addrs:
431+
if addr == "init":
432+
continue
431433
if addr in self.address_instructions:
432434
instrids = self.address_instructions[addr]
433435
for instrid in instrids:
@@ -442,6 +444,26 @@ def resolve_reaching_defs(self) -> None:
442444
# Allow for change of name of return value
443445
if str(instr.lhs) == v or v == "R0" or v == "S0":
444446
self.add_reaching_definition(xid, instrid)
447+
else:
448+
chklogger.logger.warning(
449+
"Variable names don't match: %s vs %s",
450+
str(instr.lhs), v)
451+
else:
452+
chklogger.logger.warning(
453+
"Expression is defined by unknown instruction: "
454+
+ "var: %s defined by %s",
455+
str(v), str(instr))
456+
else:
457+
chklogger.logger.warning(
458+
"Instruction id in reaching definitions for %s "
459+
+ "not found",
460+
str(v))
461+
else:
462+
chklogger.logger.warning(
463+
"Reaching definition address %s for variable %s "
464+
+ " not found",
465+
str(addr), str(v))
466+
445467

446468
def resolve_flag_reaching_defs(self) -> None:
447469
for (xid, frds) in self.flag_expr_rdefs.items():

chb/astinterface/ASTInterface.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,14 @@ def mk_byte_sum(self, bytes: List[AST.ASTExpr]) -> AST.ASTExpr:
14701470
shift += 8
14711471
return result
14721472

1473+
def mk_doubleword_sum(
1474+
self, hiword: AST.ASTExpr, loword: AST.ASTExpr) -> AST.ASTExpr:
1475+
"""Return concatenation 64 bit hiword:loword."""
1476+
1477+
shift = self.mk_integer_constant(32)
1478+
shifthi = self.mk_binary_op("lsl", hiword, shift)
1479+
return self.mk_binary_op("plus", shifthi, loword)
1480+
14731481
def mk_binary_expression(
14741482
self,
14751483
op: str,

0 commit comments

Comments
 (0)