Skip to content

Commit cbedfd1

Browse files
committed
ARM: allow for reverse pointer addition
1 parent a09570a commit cbedfd1

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

chb/arm/opcodes/ARMAdd.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,39 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
214214
hl_rhs1_type = hl_rhs1.ctype(astree.ctyper)
215215
hl_rhs2_type = hl_rhs2.ctype(astree.ctyper)
216216

217-
if hl_rhs1_type is None:
217+
if hl_rhs1_type is None and hl_rhs2_type is None:
218218
chklogger.logger.error(
219219
"Unable to lift pointer arithmetic without type for "
220220
+ "%s at address %s",
221221
str(rhs3), iaddr)
222222
return astree.mk_temp_lval_expression()
223223

224-
if hl_rhs1_type.is_pointer:
224+
if hl_rhs2_type is not None and hl_rhs2_type.is_pointer:
225+
rhs2tgttyp = cast(AST.ASTTypPtr, hl_rhs2_type).tgttyp
226+
tgttypsize = astree.type_size_in_bytes(rhs2tgttyp)
227+
if tgttypsize is None:
228+
chklogger.logger.warning(
229+
"Unable to lift pointer arithmetic without size for "
230+
+ "%s at address %s; set type size to 1",
231+
str(hl_rhs2_type), iaddr)
232+
# return astree.mk_temp_lval_expression()
233+
tgttypsize = 1
234+
235+
if tgttypsize == 1:
236+
return XU.xxpr_to_ast_def_expr(rhs3, xdata, iaddr, astree)
237+
238+
if hl_rhs1.is_integer_constant:
239+
addend = cast(AST.ASTIntegerConstant, hl_rhs1).cvalue
240+
addend = addend // tgttypsize
241+
astaddend: AST.ASTExpr = astree.mk_integer_constant(addend)
242+
annotations.append("scaled by " + str(tgttypsize))
243+
return astree.mk_binary_op("plus", hl_rhs2, astaddend)
244+
245+
scale = astree.mk_integer_constant(tgttypsize)
246+
scaled = astree.mk_binary_op("div", hl_rhs1, scale)
247+
return astree.mk_binary_op("plus", hl_rhs2, scaled)
248+
249+
if hl_rhs1_type is not None and hl_rhs1_type.is_pointer:
225250
rhs1tgttyp = cast(AST.ASTTypPtr, hl_rhs1_type).tgttyp
226251
tgttypsize = astree.type_size_in_bytes(rhs1tgttyp)
227252
if tgttypsize is None:
@@ -250,7 +275,7 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
250275
if hl_rhs2.is_integer_constant:
251276
addend = cast(AST.ASTIntegerConstant, hl_rhs2).cvalue
252277
addend = addend // tgttypsize
253-
astaddend: AST.ASTExpr = astree.mk_integer_constant(addend)
278+
astaddend = astree.mk_integer_constant(addend)
254279
annotations.append("scaled by " + str(tgttypsize))
255280
return astree.mk_binary_op("plus", hl_rhs1, astaddend)
256281

0 commit comments

Comments
 (0)