Skip to content

Commit

Permalink
Type check expression.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfish committed Jan 24, 2025
1 parent 08fdf83 commit 27468f9
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions ailment/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ def likes(self, other):
)

matches = likes
__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash((self.value, self.bits))

@property
def sign_bit(self):
if not self.is_int:
raise TypeError("Sign bit is only available for int constants.")
assert isinstance(self.value, int)
return self.value >> (self.bits - 1)

def copy(self) -> Const:
Expand Down Expand Up @@ -167,7 +170,7 @@ def likes(self, other):
return type(self) is type(other) and self.tmp_idx == other.tmp_idx and self.bits == other.bits

matches = likes
__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(("tmp", self.tmp_idx, self.bits))
Expand Down Expand Up @@ -204,7 +207,7 @@ def __str__(self):
return "%s" % str(self.variable.name)

matches = likes
__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(("reg", self.reg_offset, self.bits, self.idx))
Expand Down Expand Up @@ -269,18 +272,22 @@ def was_tmp(self) -> bool:
@property
def reg_offset(self) -> int:
if self.was_reg:
assert isinstance(self.oident, int)
return self.oident
raise TypeError("Is not a register")

@property
def stack_offset(self) -> int:
if self.was_stack:
assert isinstance(self.oident, int)
return self.oident
raise TypeError("Is not a stack variable")

@property
def tmp_idx(self) -> int | None:
return self.oident if self.was_tmp else None
if self.was_tmp:
assert isinstance(self.oident, int)
return None

@property
def parameter_category(self) -> VirtualVariableCategory | None:
Expand All @@ -292,12 +299,14 @@ def parameter_category(self) -> VirtualVariableCategory | None:
@property
def parameter_reg_offset(self) -> int | None:
if self.was_parameter and self.parameter_category == VirtualVariableCategory.REGISTER:
assert isinstance(self.oident, tuple)
return self.oident[1]
return None

@property
def parameter_stack_offset(self) -> int | None:
if self.was_parameter and self.parameter_category == VirtualVariableCategory.STACK:
assert isinstance(self.oident, tuple)
return self.oident[1]
return None

Expand Down Expand Up @@ -327,7 +336,7 @@ def __repr__(self):
ori_str = f"{{stack {self.oident}}}"
return f"vvar_{self.varid}{ori_str}"

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(("var", self.varid, self.bits, self.category, self.oident))
Expand Down Expand Up @@ -398,7 +407,7 @@ def matches(self, other) -> bool:
and other_vvar is not None
or self_vvar is not None
and other_vvar is None
or not self_vvar.matches(other_vvar)
or (self_vvar is not None and other_vvar is not None and not self_vvar.matches(other_vvar))
):
return False
return True
Expand All @@ -407,7 +416,7 @@ def matches(self, other) -> bool:
def __repr__(self):
return f"𝜙@{self.bits}b {self.src_and_vvars}"

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(("phi", self.bits, tuple(sorted(self.src_and_vvars, key=self._src_and_vvar_filter))))
Expand Down Expand Up @@ -451,7 +460,7 @@ def _src_and_vvar_filter(
if src[1] is None:
src = src[0], -1
vvar_id = vvar.varid if vvar is not None else -1
return src, vvar_id
return src, vvar_id # type: ignore


class Op(Expression):
Expand Down Expand Up @@ -512,7 +521,7 @@ def matches(self, other):
and self.operand.matches(other.operand)
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash((self.op, self.operand, self.bits))
Expand Down Expand Up @@ -627,7 +636,7 @@ def matches(self, other):
and self.rounding_mode == other.rounding_mode
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(
Expand Down Expand Up @@ -735,7 +744,7 @@ def matches(self, other):
and self.operand.matches(other.operand)
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(
Expand Down Expand Up @@ -919,7 +928,7 @@ def matches(self, other):
and self.rounding_mode == other.rounding_mode
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(
Expand Down Expand Up @@ -1106,7 +1115,7 @@ def matches(self, other):
and self.alt == other.alt
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(("Load", self.addr, self.size, self.endness))
Expand Down Expand Up @@ -1185,7 +1194,7 @@ def matches(self, other):
and self.bits == other.bits
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash((ITE, self.cond, self.iffalse, self.iftrue, self.bits))
Expand Down Expand Up @@ -1255,7 +1264,7 @@ def __init__(
maddr: Expression | None = None,
msize: int | None = None,
# TODO: fxstate (guest state effects) is not modeled yet
bits=None,
bits: int,
**kwargs,
):
super().__init__(idx, 1, **kwargs)
Expand Down Expand Up @@ -1302,7 +1311,7 @@ def matches(self, other):
and self.bits == other.bits
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash(
Expand Down Expand Up @@ -1412,7 +1421,7 @@ def matches(self, other):
and all(op1.matches(op2) for op1, op2 in zip(other.operands, self.operands))
)

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash((VEXCCallExpression, self.callee, self.bits, tuple(self.operands)))
Expand Down Expand Up @@ -1443,7 +1452,7 @@ def replace(self, old_expr, new_expr):
new_operands.append(operand)

if replaced:
return True, VEXCCallExpression(self.idx, self.callee, list(new_operands), bits=self.bits, **self.tags)
return True, VEXCCallExpression(self.idx, self.callee, tuple(new_operands), bits=self.bits, **self.tags)
else:
return False, self

Expand All @@ -1470,7 +1479,7 @@ def __init__(self, idx: int | None, stmts: list[Statement], expr: Expression, **
self.expr = expr
self.bits = self.expr.bits

__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash((MultiStatementExpression,) + tuple(self.stmts) + (self.expr,))
Expand Down Expand Up @@ -1587,7 +1596,7 @@ def likes(self, other):
)

matches = likes
__hash__ = TaggedObject.__hash__
__hash__ = TaggedObject.__hash__ # type: ignore

def _hash_core(self):
return stable_hash((self.bits, self.base, self.offset))
Expand Down

0 comments on commit 27468f9

Please sign in to comment.