Skip to content

Commit

Permalink
Speed up ArmV7Operand.type assertions (#1313)
Browse files Browse the repository at this point in the history
* Speed up ArmV7Operand.type assertions

So .type will now just return the value instead of building a mapping
and returning a result.

* Update manticore/native/cpu/arm.py

* Revert debug changes
  • Loading branch information
disconnect3d committed Dec 20, 2018
1 parent 94991c8 commit c0b6ce1
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions manticore/native/cpu/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,30 @@ def instruction_implementation(cpu, *args, **kwargs):
return abstract_instruction(instruction_implementation)


_TYPE_MAP = {
cs.arm.ARM_OP_REG: 'register',
cs.arm.ARM_OP_MEM: 'memory',
cs.arm.ARM_OP_IMM: 'immediate',
cs.arm.ARM_OP_PIMM: 'coprocessor',
cs.arm.ARM_OP_CIMM: 'immediate'
}


class Armv7Operand(Operand):
def __init__(self, cpu, op, **kwargs):
super().__init__(cpu, op, **kwargs)
self.__type = _TYPE_MAP[self.op.type]

@property
def type(self):
type_map = {
cs.arm.ARM_OP_REG: 'register',
cs.arm.ARM_OP_MEM: 'memory',
cs.arm.ARM_OP_IMM: 'immediate',
cs.arm.ARM_OP_PIMM: 'coprocessor',
cs.arm.ARM_OP_CIMM: 'immediate'
}

return type_map[self.op.type]
"""
Corresponds to capstone's `operand.type` (cs.arm.ARM_OP_*).
"""
return self.__type

@property
def size(self):
assert self.type == 'register'
assert self.__type == 'register'
if cs.arm.ARM_REG_D0 <= self.op.reg <= cs.arm.ARM_REG_D31:
return 64
else:
Expand All @@ -80,7 +85,7 @@ def size(self):

def read(self, nbits=None, with_carry=False):
carry = self.cpu.regfile.read('APSR_C')
if self.type == 'register':
if self.__type == 'register':
value = self.cpu.regfile.read(self.reg)
# PC in this case has to be set to the instruction after next. PC at this point
# is already pointing to next instruction; we bump it one more.
Expand All @@ -94,17 +99,17 @@ def read(self, nbits=None, with_carry=False):
if with_carry:
return value, carry
return value
elif self.type == 'immediate':
elif self.__type == 'immediate':
imm = self.op.imm
if self.op.subtracted:
imm = -imm
if with_carry:
return imm, self._get_expand_imm_carry(carry)
return imm
elif self.type == 'coprocessor':
elif self.__type == 'coprocessor':
imm = self.op.imm
return imm
elif self.type == 'memory':
elif self.__type == 'memory':
val = self.cpu.read_int(self.address(), nbits)
if with_carry:
return val, carry
Expand All @@ -113,17 +118,17 @@ def read(self, nbits=None, with_carry=False):
raise NotImplementedError("readOperand unknown type", self.op.type)

def write(self, value, nbits=None):
if self.type == 'register':
if self.__type == 'register':
self.cpu.regfile.write(self.reg, value)
elif self.type == 'memory':
elif self.__type == 'memory':
raise NotImplementedError('need to impl arm store mem')
else:
raise NotImplementedError("writeOperand unknown type", self.op.type)

def writeback(self, value):
if self.type == 'register':
if self.__type == 'register':
self.write(value)
elif self.type == 'memory':
elif self.__type == 'memory':
self.cpu.regfile.write(self.mem.base, value)
else:
raise NotImplementedError("writeback Operand unknown type", self.op.type)
Expand All @@ -144,12 +149,12 @@ def is_shifted(self):
return self.op.shift.type != cs.arm.ARM_SFT_INVALID

def address(self):
assert self.type == 'memory'
assert self.__type == 'memory'
addr = self.get_mem_base_addr() + self.get_mem_offset()
return addr & Mask(self.cpu.address_bit_size)

def get_mem_offset(self):
assert self.type == 'memory'
assert self.__type == 'memory'

off = 0
if self.mem.index is not None:
Expand All @@ -164,7 +169,7 @@ def get_mem_offset(self):
return off

def get_mem_base_addr(self):
assert self.type == 'memory'
assert self.__type == 'memory'

base = self.cpu.regfile.read(self.mem.base)

Expand Down

0 comments on commit c0b6ce1

Please sign in to comment.