Documentation nits; Sematnics syntax cleanup

This commit is contained in:
Dimo
2017-07-21 16:46:20 -07:00
committed by Jakob Stoklund Olesen
parent 40c86d58b9
commit 9258283e14
10 changed files with 103 additions and 94 deletions

View File

@@ -102,15 +102,17 @@ class Def(object):
def vars(self):
# type: () -> Set[Var]
""" Return the set of all Vars that appear in self"""
"""Return the set of all Vars in self that correspond to SSA values"""
return self.definitions().union(self.uses())
def substitution(self, other, s):
# type: (Def, VarMap) -> Optional[VarMap]
"""
If the Defs self and other agree structurally, return a variable
substitution to transform self ot other. Two Defs agree structurally
if the contained Apply's agree structurally.
substitution to transform self to other. Otherwise return None. Two
Defs agree structurally if there exists a Var substitution, that can
transform one into the other. See Apply.substitution() for more
details.
"""
s = self.expr.substitution(other.expr, s)
@@ -378,7 +380,7 @@ class Apply(Expr):
def vars(self):
# type: () -> Set[Var]
""" Return the set of all Vars that appear in self"""
"""Return the set of all Vars in self that correspond to SSA values"""
res = set()
for i in self.inst.value_opnums:
arg = self.args[i]
@@ -390,8 +392,8 @@ class Apply(Expr):
# type: (Apply, VarMap) -> Optional[VarMap]
"""
If the application self and other agree structurally, return a variable
substitution to transform self ot other. Two applications agree
structurally if:
substitution to transform self to other. Otherwise return None. Two
applications agree structurally if:
1) They are over the same instruction
2) Every Var v in self, maps to a single Var w in other. I.e for
each use of v in self, w is used in the corresponding place in

View File

@@ -9,15 +9,16 @@ try:
from typing import Union, Sequence, List, Tuple, Any, TYPE_CHECKING # noqa
from typing import Dict # noqa
if TYPE_CHECKING:
from .ast import Expr, Apply, Var # noqa
from .ast import Expr, Apply, Var, Def # noqa
from .typevar import TypeVar # noqa
from .ti import TypeConstraint # noqa
from .xform import XForm
from .xform import XForm, Rtl
# List of operands for ins/outs:
OpList = Union[Sequence[Operand], Operand]
ConstrList = Union[Sequence[TypeConstraint], TypeConstraint]
MaybeBoundInst = Union['Instruction', 'BoundInstruction']
InstructionSemantics = List[XForm]
InstructionSemantics = Sequence[XForm]
RtlCase = Union[Rtl, Tuple[Rtl, Sequence[TypeConstraint]]]
except ImportError:
pass
@@ -336,15 +337,23 @@ class Instruction(object):
from .ast import Apply # noqa
return Apply(self, args)
def set_semantics(self, sem):
# type: (Union[XForm, InstructionSemantics]) -> None
def set_semantics(self, src, *dsts):
# type: (Union[Def, Apply], *RtlCase) -> None
"""Set our semantics."""
from semantics import verify_semantics
from .xform import XForm, Rtl
if not isinstance(sem, list):
sem = [sem]
sem = [] # type: List[XForm]
for dst in dsts:
if isinstance(dst, Rtl):
sem.append(XForm(Rtl(src).copy({}), dst))
else:
assert isinstance(dst, tuple)
sem.append(XForm(Rtl(src).copy({}), dst[0],
constraints=dst[1]))
verify_semantics(self, Rtl(src), sem)
verify_semantics(self, sem)
self.semantics = sem

View File

@@ -89,10 +89,7 @@ class ScalarType(ValueType):
self._vectors = dict() # type: Dict[int, VectorType]
# Assign numbers starting from 1. (0 is VOID).
ValueType.all_scalars.append(self)
# Numbers are only valid for Cretone types that get emitted to Rust.
# This excludes BVTypes
self.number = len([x for x in ValueType.all_scalars
if not isinstance(x, BVType)])
self.number = len(ValueType.all_scalars)
assert self.number < 16, 'Too many scalar types'
def __repr__(self):
@@ -249,7 +246,7 @@ class BoolType(ScalarType):
return self.bits
class BVType(ScalarType):
class BVType(ValueType):
"""A flat bitvector type. Used for semantics description only."""
def __init__(self, bits):
@@ -268,7 +265,11 @@ class BVType(ScalarType):
@staticmethod
def with_bits(bits):
# type: (int) -> BVType
typ = ValueType.by_name('bv{:d}'.format(bits))
name = 'bv{:d}'.format(bits)
if name not in ValueType._registry:
return BVType(bits)
typ = ValueType.by_name(name)
if TYPE_CHECKING:
return cast(BVType, typ)
else:
@@ -278,3 +279,8 @@ class BVType(ScalarType):
# type: () -> int
"""Return the number of bits in a lane."""
return self.bits
def lane_count(self):
# type: () -> int
"""Return the number of lane. For BVtypes always 1."""
return 1

View File

@@ -501,7 +501,8 @@ class TypeSet(object):
for bits in self.bools:
yield by(types.BoolType.with_bits(bits), nlanes)
for bits in self.bitvecs:
yield by(types.BVType.with_bits(bits), nlanes)
assert nlanes == 1
yield types.BVType.with_bits(bits)
def get_singleton(self):
# type: () -> types.ValueType
@@ -572,12 +573,17 @@ class TypeVar(object):
def singleton(typ):
# type: (types.ValueType) -> TypeVar
"""Create a type variable that can only assume a single type."""
scalar = None # type: ValueType
if isinstance(typ, types.VectorType):
scalar = typ.base
lanes = (typ.lanes, typ.lanes)
elif isinstance(typ, types.ScalarType):
scalar = typ
lanes = (1, 1)
else:
assert isinstance(typ, types.BVType)
scalar = typ
lanes = (1, 1)
ints = None
floats = None

View File

@@ -55,7 +55,7 @@ class Rtl(object):
def vars(self):
# type: () -> Set[Var]
""" Return the set of all Vars that appear in self"""
"""Return the set of all Vars in self that correspond to SSA values"""
return reduce(lambda x, y: x.union(y),
[d.vars() for d in self.rtl],
set([]))