Skip to content

Commit ec0c132

Browse files
committed
ARM: convert astprov to handle result types
1 parent 9ca411c commit ec0c132

11 files changed

+520
-180
lines changed

chb/arm/ARMOpcode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def add_instruction_condition(self, s: str) -> str:
141141
else:
142142
return s
143143

144+
@property
144145
def is_writeback(self) -> bool:
145146
return self.xdata.has_base_update()
146147

chb/arm/opcodes/ARMBitwiseAnd.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2021-2024 Aarno Labs LLC
7+
# Copyright (c) 2021-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
@@ -30,7 +30,7 @@
3030
from chb.app.InstrXData import InstrXData
3131

3232
from chb.arm.ARMDictionaryRecord import armregistry
33-
from chb.arm.ARMOpcode import ARMOpcode, simplify_result
33+
from chb.arm.ARMOpcode import ARMOpcode, ARMOpcodeXData, simplify_result
3434
from chb.arm.ARMOperand import ARMOperand
3535

3636
import chb.ast.ASTNode as AST
@@ -39,11 +39,50 @@
3939
import chb.invariants.XXprUtil as XU
4040

4141
import chb.util.fileutil as UF
42-
4342
from chb.util.IndexedTable import IndexedTableValue
43+
from chb.util.loggingutil import chklogger
44+
4445

4546
if TYPE_CHECKING:
4647
from chb.arm.ARMDictionary import ARMDictionary
48+
from chb.invariants.XVariable import XVariable
49+
from chb.invariants.XXpr import XXpr
50+
51+
52+
class ARMBitwiseAndXData(ARMOpcodeXData):
53+
54+
def __init__(self, xdata: InstrXData) -> None:
55+
ARMOpcodeXData.__init__(self, xdata)
56+
57+
@property
58+
def vrd(self) -> "XVariable":
59+
return self.var(0, "vrd")
60+
61+
@property
62+
def xrn(self) -> "XXpr":
63+
return self.xpr(0, "xrn")
64+
65+
@property
66+
def xrm(self) -> "XXpr":
67+
return self.xpr(1, "xrm")
68+
69+
@property
70+
def result(self) -> "XXpr":
71+
return self.xpr(2, "result")
72+
73+
@property
74+
def rresult(self) -> "XXpr":
75+
return self.xpr(3, "rresult")
76+
77+
@property
78+
def result_simplified(self) -> str:
79+
return simplify_result(
80+
self.xdata.args[3], self.xdata.args[4], self.result, self.rresult)
81+
82+
@property
83+
def annotation(self) -> str:
84+
assignment = str(self.vrd) + " := " + self.result_simplified
85+
return self.add_instruction_condition(assignment)
4786

4887

4988
@armregistry.register_tag("AND", ARMOpcode)
@@ -96,18 +135,11 @@ def mnemonic_extension(self) -> str:
96135
return cc + wide
97136

98137
def annotation(self, xdata: InstrXData) -> str:
99-
lhs = str(xdata.vars[0])
100-
result = xdata.xprs[2]
101-
rresult = xdata.xprs[3]
102-
xresult = simplify_result(xdata.args[3], xdata.args[4], result, rresult)
103-
assignment = lhs + " := " + xresult
104-
if xdata.has_unknown_instruction_condition():
105-
return "if ? then " + assignment
106-
elif xdata.has_instruction_condition():
107-
c = str(xdata.xprs[1])
108-
return "if " + c + " then " + assignment
138+
xd = ARMBitwiseAndXData(xdata)
139+
if xd.is_ok:
140+
return xd.annotation
109141
else:
110-
return assignment
142+
return "Error value"
111143

112144
def ast_prov(
113145
self,
@@ -140,8 +172,14 @@ def ast_prov(
140172

141173
# high-level assignment
142174

143-
lhs = xdata.vars[0]
144-
rhs = xdata.xprs[3]
175+
xd = ARMBitwiseAndXData(xdata)
176+
if not xd.is_ok:
177+
chklogger.logger.error(
178+
"Encountered error value at address %s", iaddr)
179+
return ([], [])
180+
181+
lhs = xd.vrd
182+
rhs = xd.rresult
145183

146184
defuses = xdata.defuses
147185
defuseshigh = xdata.defuseshigh

chb/arm/opcodes/ARMCompare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ def ast_prov(
132132
List[AST.ASTInstruction], List[AST.ASTInstruction]]:
133133
"""Creates assignments of the subtraction performed with lhs ignored."""
134134

135-
xd = ARMCompareXData(xdata)
136135
annotations: List[str] = [iaddr, "CMP"]
137136

138137
# low-level assignment
@@ -150,6 +149,7 @@ def ast_prov(
150149

151150
# high-level assignment
152151

152+
xd = ARMCompareXData(xdata)
153153
if not xd.is_ok:
154154
chklogger.logger.error(
155155
"Error value encountered at address %s", iaddr)

chb/arm/opcodes/ARMCompareNegative.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2021-2024 Aarno Labs LLC
7+
# Copyright (c) 2021-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
@@ -30,7 +30,7 @@
3030
from chb.app.InstrXData import InstrXData
3131

3232
from chb.arm.ARMDictionaryRecord import armregistry
33-
from chb.arm.ARMOpcode import ARMOpcode, simplify_result
33+
from chb.arm.ARMOpcode import ARMOpcode, ARMOpcodeXData, simplify_result
3434
from chb.arm.ARMOperand import ARMOperand
3535

3636
import chb.ast.ASTNode as AST
@@ -39,11 +39,38 @@
3939
import chb.invariants.XXprUtil as XU
4040

4141
import chb.util.fileutil as UF
42-
4342
from chb.util.IndexedTable import IndexedTableValue
43+
from chb.util.loggingutil import chklogger
44+
4445

4546
if TYPE_CHECKING:
46-
import chb.arm.ARMDictionary
47+
from chb.arm.ARMDictionary import ARMDictionary
48+
from chb.invariants.XVariable import XVariable
49+
from chb.invariants.XXpr import XXpr
50+
51+
52+
class ARMCompareNegativeXData(ARMOpcodeXData):
53+
54+
def __init__(self, xdata: InstrXData) -> None:
55+
ARMOpcodeXData.__init__(self, xdata)
56+
57+
@property
58+
def xrn(self) -> "XXpr":
59+
return self.xpr(0, "xrn")
60+
61+
@property
62+
def xrm(self) -> "XXpr":
63+
return self.xpr(1, "xrm")
64+
65+
@property
66+
def result(self) -> "XXpr":
67+
return self.xpr(2, "result")
68+
69+
@property
70+
def annotation(self) -> str:
71+
ann = "compare-negative " + str(self.xrn) + " and " + str(self.xrm)
72+
ann += " (" + str(self.result) + ")"
73+
return self.add_instruction_condition(ann)
4774

4875

4976
@armregistry.register_tag("CMN", ARMOpcode)
@@ -65,10 +92,7 @@ class ARMCompareNegative(ARMOpcode):
6592
rdefs[1]: xrm
6693
"""
6794

68-
def __init__(
69-
self,
70-
d: "chb.arm.ARMDictionary.ARMDictionary",
71-
ixval: IndexedTableValue) -> None:
95+
def __init__(self, d: "ARMDictionary", ixval: IndexedTableValue) -> None:
7296
ARMOpcode.__init__(self, d, ixval)
7397
self.check_key(2, 2, "CompareNegative")
7498

@@ -81,23 +105,11 @@ def opargs(self) -> List[ARMOperand]:
81105
return [self.armd.arm_operand(i) for i in self.args]
82106

83107
def annotation(self, xdata: InstrXData) -> str:
84-
rhs1 = str(xdata.xprs[0])
85-
rhs2 = str(xdata.xprs[1])
86-
result = str(xdata.xprs[2])
87-
ann = (
88-
"compare-negative "
89-
+ str(rhs1)
90-
+ " and "
91-
+ str(rhs2)
92-
+ " ("
93-
+ str(result))
94-
if xdata.has_unknown_instruction_condition():
95-
return "if ? then " + ann
96-
elif xdata.has_instruction_condition():
97-
c = str(xdata.xprs[1])
98-
return "if " + c + " then " + ann
108+
xd = ARMCompareNegativeXData(xdata)
109+
if xd.is_ok:
110+
return xd.annotation
99111
else:
100-
return ann
112+
return "Error value"
101113

102114
def ast_prov(
103115
self,
@@ -124,7 +136,13 @@ def ast_prov(
124136

125137
# high-level assignment
126138

127-
rhs = xdata.xprs[2]
139+
xd = ARMCompareNegativeXData(xdata)
140+
if not xd.is_ok:
141+
chklogger.logger.error(
142+
"Encountered error value at address %s", iaddr)
143+
return ([], [])
144+
145+
rhs = xd.result
128146
rdefs = xdata.reachingdefs
129147

130148
hl_rhs = XU.xxpr_to_ast_def_expr(rhs, xdata, iaddr, astree)

0 commit comments

Comments
 (0)