Skip to content

Commit dbda8d8

Browse files
committed
ARM: convert astprov to handle result types
1 parent 4617fa4 commit dbda8d8

File tree

7 files changed

+216
-45
lines changed

7 files changed

+216
-45
lines changed

chb/arm/opcodes/ARMAdd.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ def xrn(self) -> "XXpr":
8686
def xrm(self) -> "XXpr":
8787
return self.xpr(1, "xrm")
8888

89+
@property
90+
def jt_xxrn(self) -> "XXpr":
91+
"""Part of jumptable."""
92+
return self.xpr(1, "xxrn")
93+
8994
@property
9095
def result(self) -> "XXpr":
9196
return self.xpr(2, "result")
@@ -117,6 +122,8 @@ def rm_rdef(self) -> Optional["ReachingDefFact"]:
117122

118123
@property
119124
def annotation(self) -> str:
125+
if self.xdata.is_aggregate_jumptable:
126+
return "jump-table: " + str(self.jt_xxrn)
120127
assignment = str(self.vrd) + " := " + self.result_simplified
121128
return self.add_instruction_condition(assignment)
122129

chb/arm/opcodes/ARMBranchExchange.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def rreturnval(self) -> "XXpr":
7777

7878
@property
7979
def annotation(self) -> str:
80+
if self.xdata.is_bx_call:
81+
return "bx-call"
8082
if self.has_return_xpr():
8183
return "return " + str(self.rreturnval())
8284
else:

chb/arm/opcodes/ARMLoadRegisterByte.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,11 @@ def ast_prov(
233233
defuses = xdata.defuses
234234
defuseshigh = xdata.defuseshigh
235235

236+
'''
236237
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
237238
hl_rhs = XU.xxpr_to_ast_def_expr(
238239
rhs, xdata, iaddr, astree, size=1, memaddr=xaddr)
240+
'''
239241

240242
hl_assign = astree.mk_assign(
241243
hl_lhs,

chb/arm/opcodes/ARMMoveRegisterCoprocessor.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2021-2023 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,15 +30,30 @@
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.util.fileutil as UF
37-
3837
from chb.util.IndexedTable import IndexedTableValue
38+
from chb.util.loggingutil import chklogger
3939

4040
if TYPE_CHECKING:
4141
from chb.arm.ARMDictionary import ARMDictionary
42+
from chb.invariants.XVariable import XVariable
43+
44+
45+
class ARMMoveRegisterCoprocessorXData(ARMOpcodeXData):
46+
47+
def __init__(self, xdata: InstrXData) -> None:
48+
ARMOpcodeXData.__init__(self, xdata)
49+
50+
@property
51+
def vrt(self) -> "XVariable":
52+
return self.var(0, "vrt")
53+
54+
@property
55+
def annotation(self) -> str:
56+
return str(self.vrt) + " := ?"
4257

4358

4459
@armregistry.register_tag("MRC", ARMOpcode)
@@ -88,10 +103,8 @@ def operandstring(self) -> str:
88103
+ opc2)
89104

90105
def annotation(self, xdata: InstrXData) -> str:
91-
"""format a:v
92-
93-
vars[0]: lhs; destination register (Rt)
94-
"""
95-
96-
lhs = str(xdata.vars[0])
97-
return lhs + " := ?"
106+
xd = ARMMoveRegisterCoprocessorXData(xdata)
107+
if xd.is_ok:
108+
return xd.annotation
109+
else:
110+
return "Error value"

chb/arm/opcodes/ARMMoveToCoprocessor.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2021-2023 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,15 +30,35 @@
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.util.fileutil as UF
37-
3837
from chb.util.IndexedTable import IndexedTableValue
38+
from chb.util.loggingutil import chklogger
3939

4040
if TYPE_CHECKING:
4141
from chb.arm.ARMDictionary import ARMDictionary
42+
from chb.invariants.XXpr import XXpr
43+
44+
45+
class ARMMoveToCoprocessorXData(ARMOpcodeXData):
46+
47+
def __init__(self, xdata: InstrXData) -> None:
48+
ARMOpcodeXData.__init__(self, xdata)
49+
50+
@property
51+
def xrt(self) -> "XXpr":
52+
return self.xpr(0, "xrt")
53+
54+
@property
55+
def xxrt(self) -> "XXpr":
56+
return self.xpr(1, "xxrt")
57+
58+
@property
59+
def annotation(self) -> str:
60+
return "? := " + str(self.xxrt)
61+
4262

4363

4464
@armregistry.register_tag("MCR", ARMOpcode)
@@ -56,10 +76,7 @@ class ARMMoveToCoprocessor(ARMOpcode):
5676
args[5]: opc2
5777
"""
5878

59-
def __init__(
60-
self,
61-
d: "ARMDictionary",
62-
ixval: IndexedTableValue) -> None:
79+
def __init__(self, d: "ARMDictionary", ixval: IndexedTableValue) -> None:
6380
ARMOpcode.__init__(self, d, ixval)
6481
self.check_key(2, 6, "MoveToCoprocessor")
6582

@@ -88,11 +105,8 @@ def operandstring(self) -> str:
88105
+ opc2)
89106

90107
def annotation(self, xdata: InstrXData) -> str:
91-
"""format a:v
92-
93-
xprs[0]: rhs: source register (Rt)
94-
xprs[1]: rrhs: source register rewritten
95-
"""
96-
97-
rrhs = str(xdata.xprs[1])
98-
return "? := " + str(rrhs)
108+
xd = ARMMoveToCoprocessorXData(xdata)
109+
if xd.is_ok:
110+
return xd.annotation
111+
else:
112+
return "Error value"

chb/arm/opcodes/ARMSignedMultiplyLong.py

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@
2525
# SOFTWARE.
2626
# ------------------------------------------------------------------------------
2727

28-
from typing import List, TYPE_CHECKING
28+
from typing import List, Tuple, TYPE_CHECKING
2929

3030
from chb.app.InstrXData import InstrXData
3131

3232
from chb.arm.ARMDictionaryRecord import armregistry
3333
from chb.arm.ARMOpcode import ARMOpcode, ARMOpcodeXData, simplify_result
3434
from chb.arm.ARMOperand import ARMOperand
3535

36+
import chb.ast.ASTNode as AST
37+
from chb.astinterface.ASTInterface import ASTInterface
38+
39+
import chb.invariants.XXprUtil as XU
40+
3641
import chb.util.fileutil as UF
3742
from chb.util.IndexedTable import IndexedTableValue
3843
from chb.util.loggingutil import chklogger
@@ -119,9 +124,105 @@ def __init__(self, d: "ARMDictionary", ixval: IndexedTableValue) -> None:
119124
def operands(self) -> List[ARMOperand]:
120125
return [self.armd.arm_operand(i) for i in self.args[1:]]
121126

127+
@property
128+
def opargs(self) -> List[ARMOperand]:
129+
return [self.armd.arm_operand(i) for i in self.args[1:]]
130+
122131
def annotation(self, xdata: InstrXData) -> str:
123132
xd = ARMSignedMultiplyLongXData(xdata)
124133
if xd.is_ok:
125134
return xd.annotation
126135
else:
127136
return "Error value"
137+
138+
def ast_prov(
139+
self,
140+
astree: ASTInterface,
141+
iaddr: str,
142+
bytestring: str,
143+
xdata: InstrXData) -> Tuple[
144+
List[AST.ASTInstruction], List[AST.ASTInstruction]]:
145+
146+
annotations: List[str] = [iaddr, "SMULL"]
147+
148+
# low-level assignment
149+
150+
(ll_lhs, _, _) = self.opargs[0].ast_lvalue(astree)
151+
(ll_lhs2, _, _) = self.opargs[1].ast_lvalue(astree)
152+
(ll_op1, _, _) = self.opargs[2].ast_rvalue(astree)
153+
(ll_op2, _, _) = self.opargs[3].ast_rvalue(astree)
154+
ll_rhs = astree.mk_binary_op("mult", ll_op1, ll_op2)
155+
e32 = astree.mk_integer_constant(0x10000000)
156+
ll_rhs = astree.mk_binary_op("div", ll_rhs, e32)
157+
ll_rhs2 = astree.mk_binary_op("mod", ll_rhs, e32)
158+
159+
ll_assign1 = astree.mk_assign(
160+
ll_lhs,
161+
ll_rhs,
162+
iaddr=iaddr,
163+
bytestring=bytestring,
164+
annotations=annotations)
165+
ll_assign2 = astree.mk_assign(
166+
ll_lhs2,
167+
ll_rhs2,
168+
iaddr=iaddr,
169+
bytestring=bytestring,
170+
annotations=annotations)
171+
172+
rdefs = xdata.reachingdefs
173+
174+
astree.add_expr_reachingdefs(ll_op1, [rdefs[0]])
175+
astree.add_expr_reachingdefs(ll_op2, [rdefs[1]])
176+
177+
# high-level assignment
178+
179+
xd = ARMSignedMultiplyLongXData(xdata)
180+
if not xd.is_ok:
181+
chklogger.logger.error(
182+
"Encountered error value at address %s", iaddr)
183+
return ([], [])
184+
185+
lhs = xd.vlo
186+
lhs2 = xd.vhi
187+
rhs1 = xd.xrn
188+
rhs2 = xd.xrm
189+
rhslo = xd.loresult
190+
rhshi = xd.hiresult
191+
192+
defuses = xdata.defuses
193+
defuseshigh = xdata.defuseshigh
194+
195+
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
196+
hl_lhs2 = XU.xvariable_to_ast_lval(lhs2, xdata, iaddr, astree)
197+
hl_rhs = XU.xxpr_to_ast_def_expr(rhslo, xdata, iaddr, astree)
198+
hl_rhs2 = XU.xxpr_to_ast_def_expr(rhshi, xdata, iaddr, astree)
199+
200+
hl_assign1 = astree.mk_assign(
201+
hl_lhs,
202+
hl_rhs,
203+
iaddr=iaddr,
204+
bytestring=bytestring,
205+
annotations=annotations)
206+
hl_assign2 = astree.mk_assign(
207+
hl_lhs2,
208+
hl_rhs2,
209+
iaddr=iaddr,
210+
bytestring=bytestring,
211+
annotations=annotations)
212+
213+
astree.add_instr_mapping(hl_assign1, ll_assign1)
214+
astree.add_instr_mapping(hl_assign2, ll_assign2)
215+
astree.add_instr_address(hl_assign1, [iaddr])
216+
astree.add_instr_address(hl_assign2, [iaddr])
217+
astree.add_expr_mapping(hl_rhs, ll_rhs)
218+
astree.add_expr_mapping(hl_rhs2, ll_rhs2)
219+
astree.add_lval_mapping(hl_lhs, ll_lhs)
220+
astree.add_lval_mapping(hl_lhs2, ll_lhs2)
221+
astree.add_expr_reachingdefs(hl_rhs, rdefs[2:])
222+
astree.add_expr_reachingdefs(ll_rhs, rdefs[:2])
223+
astree.add_lval_defuses(hl_lhs, defuses[0])
224+
astree.add_lval_defuses(hl_lhs2, defuses[1])
225+
astree.add_lval_defuses_high(hl_lhs, defuseshigh[0])
226+
astree.add_lval_defuses_high(hl_lhs2, defuseshigh[1])
227+
228+
return ([hl_assign1, hl_assign2], [ll_assign1, ll_assign2])

0 commit comments

Comments
 (0)