Skip to content

Commit 66db636

Browse files
committed
ARM:AST: astprov conversion for result types
1 parent 36a72c5 commit 66db636

File tree

10 files changed

+289
-112
lines changed

10 files changed

+289
-112
lines changed

chb/arm/opcodes/ARMAdd.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ def ast_prov(
237237

238238
# high-level assignment
239239

240+
def has_cast() -> bool:
241+
return (
242+
astree.has_register_variable_intro(iaddr)
243+
and astree.get_register_variable_intro(iaddr).has_cast())
244+
240245
xd = ARMAddXData(xdata)
241246
if not xdata.is_ok:
242247
chklogger.logger.error("Error value encountered at %s", iaddr)
@@ -252,16 +257,10 @@ def ast_prov(
252257
defuses = xdata.defuses
253258
defuseshigh = xdata.defuseshigh
254259

255-
if rhs3.is_string_reference:
256-
ctype = astree.astree.mk_pointer_type(astree.astree.char_type)
257-
hl_lhs = XU.xvariable_to_ast_lvals(
258-
lhs,
259-
xdata,
260-
astree,
261-
ispointer=True,
262-
ctype=ctype)[0]
263-
else:
264-
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
260+
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
261+
262+
def hl_rhs_default () -> AST.ASTExpr:
263+
return XU.xxpr_to_ast_def_expr(rhs3, xdata, iaddr, astree)
265264

266265
if str(lhs) == "PC":
267266
chklogger.logger.info(
@@ -276,11 +275,11 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
276275
hl_rhs2_type = hl_rhs2.ctype(astree.ctyper)
277276

278277
if hl_rhs1_type is None and hl_rhs2_type is None:
279-
chklogger.logger.error(
278+
chklogger.logger.info(
280279
"Unable to lift pointer arithmetic without type for "
281280
+ "%s at address %s",
282281
str(rhs3), iaddr)
283-
return astree.mk_temp_lval_expression()
282+
return hl_rhs_default()
284283

285284
if hl_rhs2_type is not None and hl_rhs2_type.is_pointer:
286285
rhs2tgttyp = cast(AST.ASTTypPtr, hl_rhs2_type).tgttyp
@@ -290,7 +289,6 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
290289
"Unable to lift pointer arithmetic without size for "
291290
+ "%s at address %s; set type size to 1",
292291
str(hl_rhs2_type), iaddr)
293-
# return astree.mk_temp_lval_expression()
294292
tgttypsize = 1
295293

296294
if tgttypsize == 1:
@@ -311,11 +309,11 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
311309
rhs1tgttyp = cast(AST.ASTTypPtr, hl_rhs1_type).tgttyp
312310
tgttypsize = astree.type_size_in_bytes(rhs1tgttyp)
313311
if tgttypsize is None:
314-
chklogger.logger.error(
312+
chklogger.logger.info(
315313
"Unable to lift pointer arithmetic without size for "
316314
+ "%s at address %s",
317315
str(hl_rhs1_type), iaddr)
318-
return astree.mk_temp_lval_expression()
316+
return hl_rhs_default()
319317

320318
if hl_rhs1.is_ast_startof:
321319
arraylval = cast(AST.ASTStartOf, hl_rhs1).lval
@@ -345,13 +343,13 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
345343
return astree.mk_binary_op("plus", hl_rhs1, scaled)
346344

347345
if hl_rhs2_type is None:
348-
chklogger.logger.error(
346+
chklogger.logger.info(
349347
"Unable to lift pointer arithmetic without type for "
350348
+ "%s at address %s",
351349
str(rhs2), iaddr)
352-
return astree.mk_temp_lval_expression()
350+
return hl_rhs_default()
353351

354-
chklogger.logger.error(
352+
chklogger.logger.info(
355353
"Second operand pointer variable not yet supported for %s at "
356354
+ "address %s; rrhs1: %s; hl_rhs1: %s; hl_rhs2: %s; hl_rhs1_type: %s;"
357355
+ " hl_rhs2_type: %s",
@@ -362,8 +360,7 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
362360
str(hl_rhs2),
363361
str(hl_rhs1_type),
364362
str(hl_rhs2_type))
365-
return astree.mk_temp_lval_expression()
366-
363+
return hl_rhs_default()
367364

368365
# resulting expression is a stack address
369366
if (
@@ -408,7 +405,7 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
408405
if rhs3.is_constant_expression:
409406
astree.set_ssa_value(str(hl_lhs), hl_rhs)
410407
else:
411-
hl_rhs = XU.xxpr_to_ast_def_expr(rhs3, xdata, iaddr, astree)
408+
hl_rhs = hl_rhs_default ()
412409

413410
hl_assign = astree.mk_assign(
414411
hl_lhs,

chb/arm/opcodes/ARMBitFieldInsert.py

Lines changed: 46 additions & 21 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.arm.ARMPseudoCode as APC
@@ -46,6 +46,27 @@
4646

4747
if TYPE_CHECKING:
4848
from chb.arm.ARMDictionary import ARMDictionary
49+
from chb.invariants.XVariable import XVariable
50+
from chb.invariants.XXpr import XXpr
51+
52+
53+
class ARMBitFieldInsertXData(ARMOpcodeXData):
54+
"""BFI <rd> <rn>"""
55+
56+
def __init__(self, xdata: InstrXData) -> None:
57+
ARMOpcodeXData.__init__(self, xdata)
58+
59+
@property
60+
def vrd(self) -> "XVariable":
61+
return self.var(0, "vrd")
62+
63+
@property
64+
def xrd(self) -> "XXpr":
65+
return self.xpr(0, "xrd")
66+
67+
@property
68+
def xrn(self) -> "XXpr":
69+
return self.xpr(1, "xrn")
4970

5071

5172
@armregistry.register_tag("BFI", ARMOpcode)
@@ -102,26 +123,30 @@ def width(self) -> int:
102123
return self.args[2]
103124

104125
def annotation(self, xdata: InstrXData) -> str:
105-
lhs = str(xdata.vars[0])
106-
rhs1 = str(xdata.xprs[0])
107-
rhs2 = str(xdata.xprs[1])
108-
assignment = (
109-
lhs
110-
+ " := bit-field-insert("
111-
+ rhs1
112-
+ ", "
113-
+ rhs2
114-
+ ", lsb:"
115-
+ str(self.lsb)
116-
+ ", width:"
117-
+ str(self.width))
118-
if xdata.has_unknown_instruction_condition():
119-
return "if ? then " + assignment
120-
elif xdata.has_instruction_condition():
121-
c = str(xdata.xprs[1])
122-
return "if " + c + " then " + assignment
126+
xd = ARMBitFieldInsertXData(xdata)
127+
if xd.is_ok:
128+
lhs = str(xd.vrd)
129+
rhs1 = str(xd.xrd)
130+
rhs2 = str(xd.xrn)
131+
assignment = (
132+
lhs
133+
+ " := bit-field-insert("
134+
+ rhs1
135+
+ ", "
136+
+ rhs2
137+
+ ", lsb:"
138+
+ str(self.lsb)
139+
+ ", width:"
140+
+ str(self.width))
141+
if xdata.has_unknown_instruction_condition():
142+
return "if ? then " + assignment
143+
elif xdata.has_instruction_condition():
144+
c = str(xdata.xprs[1])
145+
return "if " + c + " then " + assignment
146+
else:
147+
return assignment
123148
else:
124-
return assignment
149+
return "Error value"
125150

126151
def ast_prov(
127152
self,

chb/arm/opcodes/ARMByteReverseWord.py

Lines changed: 31 additions & 11 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,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
@@ -44,6 +44,28 @@
4444

4545
if TYPE_CHECKING:
4646
from chb.arm.ARMDictionary import ARMDictionary
47+
from chb.invariants.XVariable import XVariable
48+
from chb.invariants.XXpr import XXpr
49+
50+
51+
class ARMByteReverseWordXData(ARMOpcodeXData):
52+
"""REV <rd> <rn>"""
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 xxrn(self) -> "XXpr":
67+
return self.xpr(1, "xxrn")
68+
4769

4870

4971
@armregistry.register_tag("REV", ARMOpcode)
@@ -89,16 +111,14 @@ def opargs(self) -> List[ARMOperand]:
89111
return [self.armd.arm_operand(i) for i in self.args[:-1]]
90112

91113
def annotation(self, xdata: InstrXData) -> str:
92-
lhs = str(xdata.vars[0])
93-
rhs = str(xdata.xprs[1])
94-
assignment = lhs + " := __rev(" + str(rhs) + ") intrinsic"
95-
if xdata.has_unknown_instruction_condition():
96-
return "if ? then " + assignment
97-
elif xdata.has_instruction_condition():
98-
c = str(xdata.xprs[1])
99-
return "if " + c + " then " + assignment
100-
else:
114+
xd = ARMByteReverseWordXData(xdata)
115+
if xd.is_ok:
116+
lhs = str(xd.vrd)
117+
rhs = str(xd.xxrn)
118+
assignment = lhs + " := __rev(" + str(rhs) + ") intrinsic"
101119
return assignment
120+
else:
121+
return "Error value"
102122

103123
# --------------------------------------------------------------------------
104124
# Operation

chb/arm/opcodes/ARMCountLeadingZeros.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2021-2022 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
@@ -44,6 +44,28 @@
4444

4545
if TYPE_CHECKING:
4646
from chb.arm.ARMDictionary import ARMDictionary
47+
from chb.invariants.XVariable import XVariable
48+
from chb.invariants.XXpr import XXpr
49+
50+
51+
class ARMCountLeadingZerosXData(ARMOpcodeXData):
52+
"""CLZ <rd> <rn>"""
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 xxrn(self) -> "XXpr":
67+
return self.xpr(1, "xxrn")
68+
4769

4870

4971
@armregistry.register_tag("CLZ", ARMOpcode)
@@ -82,16 +104,14 @@ def opargs(self) -> List[ARMOperand]:
82104
return [self.armd.arm_operand(i) for i in self.args]
83105

84106
def annotation(self, xdata: InstrXData) -> str:
85-
lhs = str(xdata.vars[0])
86-
rhs = str(xdata.xprs[1])
87-
assignment = lhs + " := __clz(" + rhs + ") (intrinsic)"
88-
if xdata.has_unknown_instruction_condition():
89-
return "if ? then " + assignment
90-
elif xdata.has_instruction_condition():
91-
c = str(xdata.xprs[1])
92-
return "if " + c + " then " + assignment
93-
else:
107+
xd = ARMCountLeadingZerosXData(xdata)
108+
if xd.is_ok:
109+
lhs = str(xd.vrd)
110+
rhs = str(xd.xxrn)
111+
assignment = lhs + " := __clz(" + rhs + ") (intrinsic)"
94112
return assignment
113+
else:
114+
return "Error value"
95115

96116
def ast_prov(
97117
self,

chb/arm/opcodes/ARMLoadRegisterByte.py

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

236-
'''
237-
hl_lhs = XU.xvariable_to_ast_lval(lhs, xdata, iaddr, astree)
238-
hl_rhs = XU.xxpr_to_ast_def_expr(
239-
rhs, xdata, iaddr, astree, size=1, memaddr=xaddr)
240-
'''
241-
242236
hl_assign = astree.mk_assign(
243237
hl_lhs,
244238
hl_rhs,
@@ -269,8 +263,8 @@ def ast_prov(
269263
annotations=annotations)
270264
ll_assigns: List[AST.ASTInstruction] = [ll_assign, ll_addr_assign]
271265

272-
basereg = xdata.vars[1]
273-
newaddr = xdata.xprs[4]
266+
basereg = xd.get_base_update_var()
267+
newaddr = xd.get_base_update_xpr()
274268
hl_addr_lhs = XU.xvariable_to_ast_lval(basereg, xdata, iaddr, astree)
275269
hl_addr_rhs = XU.xxpr_to_ast_def_expr(newaddr, xdata, iaddr, astree)
276270

0 commit comments

Comments
 (0)