def field_at_offset(compinfo: "BCCompInfo", offsetvalue: int, astree: AbstractSyntaxTree) -> AST.ASTOffset: (finfo, r) = compinfo.field_at_offset(offsetvalue) if finfo.fieldtype.is_struct: fcompinfo = cast("BCTypComp", finfo.fieldtype).compinfo foffset = field_at_offset(fcompinfo, r, astree) return astree.mk_field_offset(finfo.fieldname, finfo.fieldtype, offset=foffset) elif r == 0: return astree.mk_field_offset(finfo.fieldname, finfo.fieldtype) elif finfo.fieldtype.is_array: ftype = cast("BCTypArray", finfo.fieldtype) elsize = ftype.tgttyp.byte_size() index = r // elsize ioffset = astree.mk_scalar_index_offset(index) return astree.mk_field_offset(finfo.fieldname, finfo.fieldtype, offset=ioffset) else: raise UF.CHBError("No field found at offset: " + str(offsetvalue) + " in struct " + compinfo.cname + " (Offsets found: " + ", ".join( (str(f[0]) + ":" + str(f[1].fieldtype) + " " + f[1].fieldname) for f in compinfo.fieldoffsets()) + ")")
def basevar_variable_to_ast_lval(basevar: "X.XVariable", offset: "VMemoryOffset", astree: AbstractSyntaxTree) -> AST.ASTLval: if offset.is_constant_value_offset: offsetvalue = offset.offsetvalue() baselval = xvariable_to_ast_lval(basevar, astree) basetype = baselval.ctype if basetype is not None: if basetype.is_array: elttype = cast("BCTypArray", basetype).tgttyp eltsize = elttype.byte_size() index = offsetvalue // eltsize indexoffset = astree.mk_scalar_index_offset(index) return astree.mk_lval(baselval.lhost, indexoffset) elif basetype.is_pointer: tgttype = cast("BCTypPtr", basetype).tgttyp basexpr = astree.mk_lval_expr(baselval) if tgttype.is_scalar: tgtsize = tgttype.byte_size() index = offsetvalue // tgtsize indexoffset = astree.mk_scalar_index_offset(index) return astree.mk_lval(baselval.lhost, indexoffset) elif tgttype.is_struct: compinfo = cast("BCTypComp", tgttype).compinfo fieldoffset = field_at_offset(compinfo, offsetvalue, astree) return astree.mk_memref_lval(basexpr, fieldoffset) elif tgttype.is_void: index = offsetvalue indexoffset = astree.mk_scalar_index_offset(index) return astree.mk_lval(baselval.lhost, indexoffset) elif offsetvalue == 0: return astree.mk_memref_lval(basexpr) else: print(" -> Basevar with basetype") else: index = offsetvalue indexoffset = astree.mk_scalar_index_offset(index) return astree.mk_lval(baselval.lhost, indexoffset) print("Basevar_variable: " + str(basevar) + "; offset: " + str(offset)) return astree.mk_variable_lval(str(basevar) + str(offset))