Skip to content

Commit 2416d9f

Browse files
committed
ARM:SUB: update for conversion to C expressions
1 parent e876c78 commit 2416d9f

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

chb/arm/opcodes/ARMSubtract.py

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050

5151

5252
class ARMSubtractXData(ARMOpcodeXData):
53+
"""Data format:
54+
- variables:
55+
0: vrd
56+
57+
- expressions:
58+
0: xrn
59+
1: xrm
60+
2: result
61+
3: rresult (result rewritten)
62+
63+
- c expressions:
64+
0: cresult
65+
"""
5366

5467
def __init__(self, xdata: InstrXData) -> None:
5568
ARMOpcodeXData.__init__(self, xdata)
@@ -70,18 +83,41 @@ def xrm(self) -> "XXpr":
7083
def result(self) -> "XXpr":
7184
return self.xpr(2, "result")
7285

86+
@property
87+
def is_result_ok(self) -> bool:
88+
return self.is_xpr_ok(2)
89+
7390
@property
7491
def rresult(self) -> "XXpr":
7592
return self.xpr(3, "rresult")
7693

94+
@property
95+
def is_rresult_ok(self) -> bool:
96+
return self.is_xpr_ok(3)
97+
98+
@property
99+
def cresult(self) -> "XXpr":
100+
return self.cxpr(0, "cresult")
101+
102+
@property
103+
def is_cresult_ok(self) -> bool:
104+
return self.is_cxpr_ok(0)
105+
77106
@property
78107
def result_simplified(self) -> str:
79-
return simplify_result(
80-
self.xdata.args[3], self.xdata.args[4], self.result, self.rresult)
108+
if self.is_result_ok and self.is_rresult_ok:
109+
return simplify_result(
110+
self.xdata.args[3], self.xdata.args[4], self.result, self.rresult)
111+
else:
112+
return str(self.xrn) + " - " + str(self.xrm)
81113

82114
@property
83115
def annotation(self) -> str:
84-
assignment = str(self.vrd) + " := " + self.result_simplified
116+
cresult = (
117+
" (C: "
118+
+ (str(self.cresult) if self.is_cresult_ok else "None")
119+
+ ")")
120+
assignment = str(self.vrd) + " := " + self.result_simplified + cresult
85121
return self.add_instruction_condition(assignment)
86122

87123

@@ -100,13 +136,8 @@ class ARMSubtract(ARMOpcode):
100136
args[4]: is-wide (thumb)
101137
args[5]: wide
102138
103-
xdata format: a:vxxxxrrdh
104-
-------------------------
105-
vars[0]: lhs (Rd)
106-
xprs[0]: rhs1 (Rn)
107-
xprs[1]: rhs2 (Rm)
108-
xprs[2]: rhs1 - rhs2
109-
xprs[3]: rhs1 - rhs2 (simplified)
139+
xdata format:
140+
-------------
110141
rdefs[0]: rhs1
111142
rdefs[1]: rhs2
112143
rdefs[2..]: reaching definitions for simplified result
@@ -146,10 +177,7 @@ def is_writeback(self) -> bool:
146177

147178
def annotation(self, xdata: InstrXData) -> str:
148179
xd = ARMSubtractXData(xdata)
149-
if xd.is_ok:
150-
return xd.annotation
151-
else:
152-
return "Error value"
180+
return xd.annotation
153181

154182
def ast_prov(
155183
self,
@@ -183,37 +211,50 @@ def ast_prov(
183211
# high-level assignment
184212

185213
xd = ARMSubtractXData(xdata)
186-
if not xd.is_ok:
187-
chklogger.logger.error("Error value encountered at %s", iaddr)
188-
return ([], [])
214+
215+
if xd.is_cresult_ok and xd.is_rresult_ok:
216+
rhs = xd.cresult
217+
xrhs = xd.rresult
218+
219+
elif xd.is_rresult_ok:
220+
rhs = xd.rresult
221+
xrhs = xd.rresult
222+
223+
elif xd.is_result_ok:
224+
rhs = xd.result
225+
xrhs = xd.result
226+
227+
else:
228+
chklogger.logger.error(
229+
"SUB: Encountered error value for rhs at address %s", iaddr)
230+
return ([], [ll_assign])
189231

190232
lhs = xd.vrd
191233
rhs1 = xd.xrn
192234
rhs2 = xd.xrm
193-
rhs3 = xd.rresult
194235

195236
defuses = xdata.defuses
196237
defuseshigh = xdata.defuseshigh
197238

198239
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
199240

200241
# resulting expression is a stack address
201-
if str(rhs1) == "SP" and rhs3.is_stack_address:
242+
if str(rhs1) == "SP" and xrhs.is_stack_address:
202243
annotations.append("stack address")
203-
rhs3 = cast("XprCompound", rhs3)
204-
stackoffset = rhs3.stack_address_offset()
244+
xrhs = cast("XprCompound", xrhs)
245+
stackoffset = xrhs.stack_address_offset()
205246
rhslval = astree.mk_stack_variable_lval(stackoffset)
206247
hl_rhs: AST.ASTExpr = astree.mk_address_of(rhslval)
207248

208249
elif str(rhs1) == "PC" or str(rhs2) == "PC":
209250
annotations.append("PC-relative")
210-
if rhs3.is_int_constant:
211-
rhsval = cast("XprConstant", rhs3).intvalue
251+
if xrhs.is_int_constant:
252+
rhsval = cast("XprConstant", xrhs).intvalue
212253
rhsast = astree.mk_integer_constant(rhsval)
213254
else:
214-
hl_rhs = XU.xxpr_to_ast_def_expr(rhs3, xdata, iaddr, astree)
255+
hl_rhs = XU.xxpr_to_ast_def_expr(rhs, xdata, iaddr, astree)
215256
else:
216-
hl_rhs = XU.xxpr_to_ast_def_expr(rhs3, xdata, iaddr, astree)
257+
hl_rhs = XU.xxpr_to_ast_def_expr(rhs, xdata, iaddr, astree)
217258

218259
hl_assign = astree.mk_assign(
219260
hl_lhs,

0 commit comments

Comments
 (0)