git @ Cat's Eye Technologies SixtyPical / 7dfd2cf
Allow copying a routine directly into a vector table. Chris Pressey 3 years ago
3 changed file(s) with 87 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
22 from sixtypical.ast import Program, Routine, Block, Instr
33 from sixtypical.model import (
44 TYPE_BYTE, TYPE_WORD,
5 TableType, BufferType, PointerType, VectorType, ExecutableType,
5 TableType, BufferType, PointerType, VectorType, ExecutableType, RoutineType,
66 ConstantRef, LocationRef, IndirectRef, IndexedRef, AddressRef,
77 REG_A, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C
88 )
140140
141141 def set_touched(self, *refs):
142142 for ref in refs:
143 # FIXME review the whole "touched" thing. what does it even mean? how is it different from "written"?
144 if isinstance(ref.type, RoutineType):
145 continue
143146 self._touched.add(ref)
144147
145148 def set_meaningful(self, *refs):
363366 elif isinstance(src, (LocationRef, ConstantRef)) and isinstance(dest, IndexedRef):
364367 if src.type == TYPE_WORD and TableType.is_a_table_type(dest.ref.type, TYPE_WORD):
365368 pass
366 elif isinstance(src.type, VectorType) and isinstance(dest.ref.type, TableType) and dest.ref.type.of_type == src.type:
367 # TODO ideally we'd check if the vectors in the table are compatible, rather than equal, to the src
369 elif (isinstance(src.type, ExecutableType) and isinstance(dest.ref.type, TableType) and
370 ExecutableType.executable_types_compatible(src.type, dest.ref.type.of_type)):
368371 pass
369372 else:
370373 raise TypeMismatchError((src, dest))
372375 elif isinstance(src, IndexedRef) and isinstance(dest, LocationRef):
373376 if TableType.is_a_table_type(src.ref.type, TYPE_WORD) and dest.type == TYPE_WORD:
374377 pass
375 elif isinstance(dest.type, VectorType) and isinstance(src.ref.type, TableType) and src.ref.type.of_type == dest.type:
376 # TODO ideally we'd check if the vectors in the table are compatible, rather than equal, to the src
378 elif (isinstance(src.ref.type, TableType) and isinstance(dest.type, VectorType) and
379 ExecutableType.executable_types_compatible(src.ref.type.of_type, dest.type)):
377380 pass
378381 else:
379382 raise TypeMismatchError((src, dest))
4545
4646 def __hash__(self):
4747 return hash(self.name) ^ hash(self.inputs) ^ hash(self.outputs) ^ hash(self.trashes)
48
49 @classmethod
50 def executable_types_compatible(cls_, src, dest):
51 """Returns True iff a value of type `src` can be assigned to a storage location of type `dest`."""
52 if isinstance(src, ExecutableType) and isinstance(dest, VectorType):
53 # TODO: I'm sure we can replace some of these with subset-containment, but that requires thought
54 return (
55 src.inputs == dest.inputs and
56 src.outputs == dest.outputs and
57 src.trashes == dest.trashes
58 )
59 else:
60 return False
4861
4962
5063 class RoutineType(ExecutableType):
18911891 | }
18921892 = ok
18931893
1894 ### vector table ###
1895
1896 Copying to and from a vector table.
1894 ### Vector tables ###
1895
1896 A vector can be copied into a vector table.
18971897
18981898 | vector one
18991899 | outputs x
19141914 | ld x, 0
19151915 | copy bar, one
19161916 | copy one, many + x
1917 | }
1918 = ok
1919
1920 A vector can be copied out of a vector table.
1921
1922 | vector one
1923 | outputs x
1924 | trashes a, z, n
1925 | vector table[256] many
1926 | outputs x
1927 | trashes a, z, n
1928 |
1929 | routine bar outputs x trashes a, z, n {
1930 | ld x, 200
1931 | }
1932 |
1933 | routine main
1934 | inputs one, many
1935 | outputs one, many
1936 | trashes a, x, n, z
1937 | {
1938 | ld x, 0
19171939 | copy many + x, one
19181940 | call one
19191941 | }
19201942 = ok
1943
1944 A routine can be copied into a vector table.
1945
1946 | vector table[256] many
1947 | outputs x
1948 | trashes a, z, n
1949 |
1950 | routine bar outputs x trashes a, z, n {
1951 | ld x, 200
1952 | }
1953 |
1954 | routine main
1955 | inputs many
1956 | outputs many
1957 | trashes a, x, n, z
1958 | {
1959 | ld x, 0
1960 | copy bar, many + x
1961 | }
1962 = ok
1963
1964 A vector in a vector table cannot be directly called.
1965
1966 | vector table[256] many
1967 | outputs x
1968 | trashes a, z, n
1969 |
1970 | routine bar outputs x trashes a, z, n {
1971 | ld x, 200
1972 | }
1973 |
1974 | routine main
1975 | inputs many
1976 | outputs many
1977 | trashes a, x, n, z
1978 | {
1979 | ld x, 0
1980 | copy bar, many + x
1981 | call many + x
1982 | }
1983 ? ValueError