2015-10-16 09:40:38 +00:00
|
|
|
"""Data/storage model for SixtyPical."""
|
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
from collections import namedtuple
|
2017-12-11 10:42:42 +00:00
|
|
|
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class BitType(namedtuple('BitType', ['typename'])):
|
|
|
|
max_range = (0, 1)
|
|
|
|
def __new__(cls):
|
|
|
|
return super(BitType, cls).__new__(cls, 'bit')
|
|
|
|
TYPE_BIT = BitType()
|
2015-10-16 09:40:38 +00:00
|
|
|
|
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class ByteType(namedtuple('ByteType', ['typename'])):
|
|
|
|
max_range = (0, 255)
|
|
|
|
def __new__(cls):
|
|
|
|
return super(ByteType, cls).__new__(cls, 'byte')
|
|
|
|
TYPE_BYTE = ByteType()
|
2015-10-18 16:23:01 +00:00
|
|
|
|
2015-10-19 17:18:06 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class WordType(namedtuple('WordType', ['typename'])):
|
|
|
|
max_range = (0, 65535)
|
|
|
|
def __new__(cls):
|
|
|
|
return super(WordType, cls).__new__(cls, 'word')
|
|
|
|
TYPE_WORD = WordType()
|
2015-10-19 17:18:06 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
|
|
|
|
class RoutineType(namedtuple('RoutineType', ['typename', 'inputs', 'outputs', 'trashes'])):
|
2018-02-05 17:01:25 +00:00
|
|
|
"""This memory location contains the code for a routine."""
|
2019-04-10 07:50:13 +00:00
|
|
|
max_range = (0, 0)
|
|
|
|
|
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(RoutineType, cls).__new__(cls, 'routine', *args)
|
2015-10-19 17:18:06 +00:00
|
|
|
|
2018-02-05 12:04:28 +00:00
|
|
|
@classmethod
|
|
|
|
def executable_types_compatible(cls_, src, dest):
|
|
|
|
"""Returns True iff a value of type `src` can be assigned to a storage location of type `dest`."""
|
2018-02-05 17:01:25 +00:00
|
|
|
if isinstance(src, VectorType):
|
|
|
|
src = src.of_type
|
|
|
|
if isinstance(dest, VectorType):
|
|
|
|
dest = dest.of_type
|
|
|
|
if isinstance(src, RoutineType) and isinstance(dest, RoutineType):
|
2018-02-05 12:04:28 +00:00
|
|
|
# TODO: I'm sure we can replace some of these with subset-containment, but that requires thought
|
|
|
|
return (
|
|
|
|
src.inputs == dest.inputs and
|
|
|
|
src.outputs == dest.outputs and
|
|
|
|
src.trashes == dest.trashes
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-10-19 17:18:06 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class VectorType(namedtuple('VectorType', ['typename', 'of_type'])):
|
2018-02-05 17:01:25 +00:00
|
|
|
"""This memory location contains the address of some other type (currently, only RoutineType)."""
|
2019-04-10 07:50:13 +00:00
|
|
|
max_range = (0, 65535)
|
2018-02-05 17:01:25 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(VectorType, cls).__new__(cls, 'vector', *args)
|
2015-10-18 16:23:01 +00:00
|
|
|
|
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class TableType(namedtuple('TableType', ['typename', 'of_type', 'size'])):
|
2018-02-02 16:31:23 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(TableType, cls).__new__(cls, 'table', *args)
|
2019-04-08 10:50:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_range(self):
|
|
|
|
return (0, self.size - 1)
|
|
|
|
|
2018-02-02 17:05:15 +00:00
|
|
|
@classmethod
|
|
|
|
def is_a_table_type(cls_, x, of_type):
|
|
|
|
return isinstance(x, TableType) and x.of_type == of_type
|
|
|
|
|
2018-02-02 16:31:23 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class PointerType(namedtuple('PointerType', ['typename'])):
|
|
|
|
max_range = (0, 65535)
|
2017-11-24 11:30:20 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
def __new__(cls):
|
|
|
|
return super(PointerType, cls).__new__(cls, 'pointer')
|
2018-02-14 13:31:03 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
# --------------------------------------------------------
|
2015-10-18 16:23:01 +00:00
|
|
|
|
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class LocationRef(namedtuple('LocationRef', ['reftype', 'name'])):
|
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(LocationRef, cls).__new__(cls, 'location', *args)
|
2018-02-14 13:31:03 +00:00
|
|
|
|
2017-12-12 12:45:47 +00:00
|
|
|
@classmethod
|
|
|
|
def format_set(cls, location_refs):
|
2018-09-06 13:21:29 +00:00
|
|
|
return '{%s}' % ', '.join([str(loc) for loc in sorted(location_refs, key=lambda x: x.name)])
|
2017-12-12 12:45:47 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class IndirectRef(namedtuple('IndirectRef', ['reftype', 'ref'])):
|
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(IndirectRef, cls).__new__(cls, 'indirect', *args)
|
2017-12-01 11:44:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return '[{}]+y'.format(self.ref.name)
|
|
|
|
|
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class IndexedRef(namedtuple('IndexedRef', ['reftype', 'ref', 'offset', 'index'])):
|
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(IndexedRef, cls).__new__(cls, 'indexed', *args)
|
2017-12-08 13:41:48 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2019-04-08 10:50:54 +00:00
|
|
|
return '{}+{}+{}'.format(self.ref.name, self.offset, self.index.name)
|
2017-12-08 13:41:48 +00:00
|
|
|
|
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
class ConstantRef(namedtuple('ConstantRef', ['reftype', 'type', 'value'])):
|
|
|
|
def __new__(cls, *args):
|
|
|
|
return super(ConstantRef, cls).__new__(cls, 'constant', *args)
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2017-12-07 12:48:56 +00:00
|
|
|
def high_byte(self):
|
|
|
|
return (self.value >> 8) & 255
|
|
|
|
|
|
|
|
def low_byte(self):
|
|
|
|
return self.value & 255
|
|
|
|
|
2018-03-14 16:27:33 +00:00
|
|
|
def pred(self):
|
|
|
|
assert self.type == TYPE_BYTE
|
|
|
|
value = self.value - 1
|
|
|
|
while value < 0:
|
|
|
|
value += 256
|
|
|
|
return ConstantRef(self.type, value)
|
|
|
|
|
|
|
|
def succ(self):
|
|
|
|
assert self.type == TYPE_BYTE
|
|
|
|
value = self.value + 1
|
|
|
|
while value > 255:
|
|
|
|
value -= 256
|
|
|
|
return ConstantRef(self.type, value)
|
|
|
|
|
2019-04-08 10:50:54 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return 'constant({})'.format(self.value)
|
|
|
|
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
REG_A = LocationRef('a')
|
|
|
|
REG_X = LocationRef('x')
|
|
|
|
REG_Y = LocationRef('y')
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2019-04-10 07:50:13 +00:00
|
|
|
FLAG_Z = LocationRef('z')
|
|
|
|
FLAG_C = LocationRef('c')
|
|
|
|
FLAG_N = LocationRef('n')
|
|
|
|
FLAG_V = LocationRef('v')
|