Skip to content

Commit 8c871a4

Browse files
committed
ARM: updates for result types / c expressions
1 parent 8e44de2 commit 8c871a4

File tree

9 files changed

+165
-103
lines changed

9 files changed

+165
-103
lines changed

chb/arm/opcodes/ARMBitwiseAnd.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,15 @@ def operands(self) -> List[ARMOperand]:
160160
def opargs(self) -> List[ARMOperand]:
161161
return [self.armd.arm_operand(i) for i in self.args[1:-1]]
162162

163+
@property
164+
def writeback(self) -> bool:
165+
return self.args[0] == 1
166+
163167
def mnemonic_extension(self) -> str:
168+
wb = "S" if self.writeback else ""
164169
cc = ARMOpcode.mnemonic_extension(self)
165170
wide = ".W" if self.args[4] == 1 else ""
166-
return cc + wide
171+
return wb + cc + wide
167172

168173
def annotation(self, xdata: InstrXData) -> str:
169174
xd = ARMBitwiseAndXData(xdata)

chb/arm/opcodes/ARMBranchExchange.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ def has_creturnval(self) -> bool:
8989
def creturnval(self) -> "XXpr":
9090
return self.xdata.get_return_cxpr()
9191

92-
def has_instruction_condition(self) -> bool:
93-
return self.xdata.has_instruction_condition()
94-
95-
def get_instruction_condition(self) -> "XXpr":
96-
return self.xdata.get_instruction_condition()
97-
98-
def has_valid_instruction_c_condition(self) -> bool:
99-
return self.xdata.has_valid_instruction_c_condition()
100-
101-
def get_instruction_c_condition(self) -> "XXpr":
102-
return self.xdata.get_instruction_c_condition()
103-
10492
@property
10593
def annotation(self) -> str:
10694
if self.xdata.is_bx_call:

chb/arm/opcodes/ARMByteReversePackedHalfword.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
from chb.astinterface.ASTInterface import ASTInterface
3939

4040
import chb.invariants.XXprUtil as XU
41-
import chb.util.fileutil as UF
4241

42+
import chb.util.fileutil as UF
4343
from chb.util.IndexedTable import IndexedTableValue
44+
from chb.util.loggingutil import chklogger
45+
4446

4547
if TYPE_CHECKING:
4648
from chb.arm.ARMDictionary import ARMDictionary
@@ -155,11 +157,7 @@ def ast_prov(
155157

156158
annotations: List[str] = [iaddr, "REV16"]
157159

158-
lhs = xdata.vars[0]
159-
rhs = xdata.xprs[0]
160-
rdefs = xdata.reachingdefs
161-
defuses = xdata.defuses
162-
defuseshigh = xdata.defuseshigh
160+
# low-level assignment
163161

164162
(ll_lhs, _, _) = self.opargs[0].ast_lvalue(astree)
165163
(ll_rhs, _, _) = self.opargs[1].ast_rvalue(astree)
@@ -171,30 +169,23 @@ def ast_prov(
171169
iaddr=iaddr,
172170
bytestring=bytestring)
173171

174-
lhsasts = XU.xvariable_to_ast_lvals(lhs, xdata, astree)
175-
if len(lhsasts) == 0:
176-
raise UF.CHBError(
177-
"ByteReversePatckedHalfword (REV16): no lval found")
172+
# high-level assignment
178173

179-
if len(lhsasts) > 1:
180-
raise UF.CHBError(
181-
"ByteReversePackedHalfword (REV16): multiple lvals in ast: "
182-
+ ", ".join(str(v) for v in lhsasts))
183-
184-
hl_lhs = lhsasts[0]
174+
xd = ARMByteReversePackedHalfwordXData(xdata)
175+
if not xd.is_ok:
176+
chklogger.logger.error(
177+
"Encountered error value at address %s", iaddr)
178+
return ([], [])
185179

186-
rhsasts = XU.xxpr_to_ast_def_exprs(rhs, xdata, iaddr, astree)
187-
if len(rhsasts) == 0:
188-
raise UF.CHBError(
189-
"ByteReversePackedHalfword (REV16): no argument value found")
180+
lhs = xd.vrd
181+
rhs = xd.xxrm
190182

191-
if len(rhsasts) > 1:
192-
raise UF.CHBError(
193-
"ByteReversePackedHalfword (REV16): "
194-
+ "multiple argument values in asts: "
195-
+ ", ".join(str(x) for x in rhsasts))
183+
rdefs = xdata.reachingdefs
184+
defuses = xdata.defuses
185+
defuseshigh = xdata.defuseshigh
196186

197-
hl_rhs = rhsasts[0]
187+
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
188+
hl_rhs = XU.xxpr_to_ast_def_expr(rhs, xdata, iaddr, astree)
198189

199190
if astree.has_variable_intro(iaddr):
200191
vname = astree.get_variable_intro(iaddr)

chb/arm/opcodes/ARMByteReverseWord.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
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
@@ -157,11 +158,7 @@ def ast_prov(
157158

158159
annotations: List[str] = [iaddr, "REV"]
159160

160-
lhs = xdata.vars[0]
161-
rhs = xdata.xprs[0]
162-
rdefs = xdata.reachingdefs
163-
defuses = xdata.defuses
164-
defuseshigh = xdata.defuseshigh
161+
# low-level assignment
165162

166163
(ll_lhs, _, _) = self.opargs[0].ast_lvalue(astree)
167164
(ll_rhs, _, _) = self.opargs[1].ast_rvalue(astree)
@@ -173,27 +170,22 @@ def ast_prov(
173170
iaddr=iaddr,
174171
bytestring=bytestring)
175172

176-
lhsasts = XU.xvariable_to_ast_lvals(lhs, xdata, astree)
177-
if len(lhsasts) == 0:
178-
raise UF.CHBError("ByteReverseWord (REV): no lval found")
173+
# high-level assignment
179174

180-
if len(lhsasts) > 1:
181-
raise UF.CHBError(
182-
"ByteReverseWord (REV): multiple lvals in ast: "
183-
+ ", ".join(str(v) for v in lhsasts))
184-
185-
hl_lhs = lhsasts[0]
175+
xd = ARMByteReverseWordXData(xdata)
176+
if not xd.is_ok:
177+
chklogger.logger.error(
178+
"REV: Encountered error value at address %s", iaddr)
186179

187-
rhsasts = XU.xxpr_to_ast_def_exprs(rhs, xdata, iaddr, astree)
188-
if len(rhsasts) == 0:
189-
raise UF.CHBError("ByteReverseWord (REV): no argument value found")
180+
lhs = xd.vrd
181+
rhs = xd.xxrn
190182

191-
if len(rhsasts) > 1:
192-
raise UF.CHBError(
193-
"ByteReverseWord (REV): multiple argument values in asts: "
194-
+ ", ".join(str(x) for x in rhsasts))
183+
rdefs = xdata.reachingdefs
184+
defuses = xdata.defuses
185+
defuseshigh = xdata.defuseshigh
195186

196-
hl_rhs = rhsasts[0]
187+
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
188+
hl_rhs = XU.xxpr_to_ast_def_expr(rhs, xdata, iaddr, astree)
197189

198190
if astree.has_variable_intro(iaddr):
199191
vname = astree.get_variable_intro(iaddr)

chb/arm/opcodes/ARMLoadMultipleIncrementAfter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def annotation(self) -> str:
193193
else:
194194
pairs = []
195195
if len(pairs) > 0:
196-
assigns = "; ".join(str(v) + " := " + str(x) for (x, v) in pairs)
196+
assigns = "; ".join(str(v) + " := " + str(x) for (v, x) in pairs)
197197
else:
198198
assigns = "unknown rhs memory"
199199
wbu = self.writeback_update()

chb/arm/opcodes/ARMLoadRegister.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def annotation(self) -> str:
152152
str(self.vrt) + " := *(" + str(self.xaddr) + ")" + cx + caddr)
153153
else:
154154
assignment = "Error value"
155+
pcwarning = "**indirect call** " if str(self.vrt) == "PC" else ""
156+
assignment = pcwarning + assignment
155157
return self.add_instruction_condition(assignment) + wbu
156158

157159

@@ -251,6 +253,12 @@ def has_cast() -> bool:
251253

252254
lhs = xd.vrt
253255

256+
if str(lhs) == "PC":
257+
chklogger.logger.error(
258+
"LDR: Indirect call via Load to PC not yet handled at %s",
259+
iaddr)
260+
return ([], (ll_pre + [ll_assign] + ll_post))
261+
254262
if xd.is_ok:
255263
rhs = xd.cxrmem
256264
rhsval = None if has_cast() else xd.cxrmem

chb/arm/opcodes/ARMLogicalShiftLeft.py

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@
4949

5050

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

5366
def __init__(self, xdata: InstrXData) -> None:
5467
ARMOpcodeXData.__init__(self, xdata)
@@ -69,18 +82,41 @@ def xrm(self) -> "XXpr":
6982
def result(self) -> "XXpr":
7083
return self.xpr(2, "result")
7184

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

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

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

86122

@@ -156,10 +192,7 @@ def lsl_xdata(self, xdata: InstrXData) -> ARMLogicalShiftLeftXData:
156192

157193
def annotation(self, xdata: InstrXData) -> str:
158194
xd = ARMLogicalShiftLeftXData(xdata)
159-
if xd.is_ok:
160-
return xd.annotation
161-
else:
162-
return "Error value"
195+
return xd.annotation
163196

164197
def ast_prov(
165198
self,
@@ -185,24 +218,35 @@ def ast_prov(
185218
bytestring=bytestring,
186219
annotations=annotations)
187220

221+
rdefs = xdata.reachingdefs
222+
223+
astree.add_expr_reachingdefs(ll_rhs1, [rdefs[0]])
224+
astree.add_expr_reachingdefs(ll_rhs2, [rdefs[1]])
225+
188226
# high-level assignment
189227

190228
xd = ARMLogicalShiftLeftXData(xdata)
191-
if not xd.is_ok:
229+
230+
if xd.is_cresult_ok and xd.is_rresult_ok:
231+
rhs = xd.cresult
232+
233+
elif xd.is_rresult_ok:
234+
rhs = xd.rresult
235+
236+
elif xd.is_result_ok:
237+
rhs = xd.result
238+
239+
else:
192240
chklogger.logger.error(
193-
"Encountered error value at address %s", iaddr)
194-
return ([], [])
241+
"LSL: Encountered error value for rhs address %s", iaddr)
242+
return ([], [ll_assign])
195243

196244
lhs = xd.vrd
197-
rhs1 = xd.xrn
198-
rhs2 = xd.xrm
199-
rresult = xd.rresult
200-
rdefs = xdata.reachingdefs
201245
defuses = xdata.defuses
202246
defuseshigh = xdata.defuseshigh
203247

204248
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
205-
hl_rhs = XU.xxpr_to_ast_def_expr(rresult, xdata, iaddr, astree)
249+
hl_rhs = XU.xxpr_to_ast_def_expr(rhs, xdata, iaddr, astree)
206250

207251
hl_assign = astree.mk_assign(
208252
hl_lhs,
@@ -216,8 +260,6 @@ def ast_prov(
216260
astree.add_expr_mapping(hl_rhs, ll_rhs)
217261
astree.add_lval_mapping(hl_lhs, ll_lhs)
218262
astree.add_expr_reachingdefs(ll_rhs, [rdefs[0], rdefs[1]])
219-
astree.add_expr_reachingdefs(ll_rhs1, [rdefs[0]])
220-
astree.add_expr_reachingdefs(ll_rhs2, [rdefs[1]])
221263
astree.add_expr_reachingdefs(hl_rhs, rdefs[2:])
222264
astree.add_lval_defuses(hl_lhs, defuses[0])
223265
astree.add_lval_defuses_high(hl_lhs, defuseshigh[0])

0 commit comments

Comments
 (0)