A little awkward, but analyze byte table access correctly.
Chris Pressey
6 years ago
1 | 1 | |
2 | 2 | from sixtypical.ast import Program, Routine, Block, Instr |
3 | 3 | from sixtypical.model import ( |
4 | TYPE_BYTE, TYPE_BYTE_TABLE, | |
4 | 5 | ConstantRef, LocationRef, FLAG_Z, FLAG_N, FLAG_V, FLAG_C |
5 | 6 | ) |
6 | 7 | |
30 | 31 | |
31 | 32 | |
32 | 33 | class UsageClashError(StaticAnalysisError): |
34 | pass | |
35 | ||
36 | ||
37 | class TypeMismatchError(StaticAnalysisError): | |
33 | 38 | pass |
34 | 39 | |
35 | 40 | |
136 | 141 | src = instr.src |
137 | 142 | |
138 | 143 | if opcode == 'ld': |
144 | if instr.index: | |
145 | if src.type == TYPE_BYTE_TABLE and dest.type == TYPE_BYTE: | |
146 | pass | |
147 | else: | |
148 | raise TypeMismatchError((src, dest)) | |
149 | elif src.type != dest.type: | |
150 | raise TypeMismatchError((src, dest)) | |
139 | 151 | context.assert_initialized(src) |
140 | 152 | context.assert_writeable(dest, FLAG_Z, FLAG_N) |
141 | 153 | context.set_initialized(dest, FLAG_Z, FLAG_N) |
142 | 154 | elif opcode == 'st': |
155 | if instr.index: | |
156 | if src.type == TYPE_BYTE and dest.type == TYPE_BYTE_TABLE: | |
157 | pass | |
158 | else: | |
159 | raise TypeMismatchError((src, dest)) | |
160 | elif src.type != dest.type: | |
161 | raise TypeMismatchError((src, dest)) | |
143 | 162 | context.assert_initialized(src) |
144 | 163 | context.assert_writeable(dest) |
145 | 164 | context.set_initialized(dest) |
193 | 193 | | st 0, lives |
194 | 194 | | } |
195 | 195 | ? IllegalWriteError: lives |
196 | ||
197 | Storing to a table, you must use an index, and vice-versa. | |
198 | ||
199 | | byte one | |
200 | | byte table many | |
201 | | | |
202 | | routine main | |
203 | | outputs one | |
204 | | trashes a, x, n, z | |
205 | | { | |
206 | | ld x, 0 | |
207 | | ld a, 0 | |
208 | | st a, one | |
209 | | } | |
210 | = ok | |
211 | ||
212 | | byte one | |
213 | | byte table many | |
214 | | | |
215 | | routine main | |
216 | | outputs many | |
217 | | trashes a, x, n, z | |
218 | | { | |
219 | | ld x, 0 | |
220 | | ld a, 0 | |
221 | | st a, many | |
222 | | } | |
223 | ? TypeMismatchError | |
224 | ||
225 | | byte one | |
226 | | byte table many | |
227 | | | |
228 | | routine main | |
229 | | outputs one | |
230 | | trashes a, x, n, z | |
231 | | { | |
232 | | ld x, 0 | |
233 | | ld a, 0 | |
234 | | st a, one + x | |
235 | | } | |
236 | ? TypeMismatchError | |
237 | ||
238 | | byte one | |
239 | | byte table many | |
240 | | | |
241 | | routine main | |
242 | | outputs many | |
243 | | trashes a, x, n, z | |
244 | | { | |
245 | | ld x, 0 | |
246 | | ld a, 0 | |
247 | | st a, many + x | |
248 | | } | |
249 | = ok | |
250 | ||
251 | Reading from a table, you must use an index, and vice-versa. | |
252 | ||
253 | | byte one | |
254 | | byte table many | |
255 | | | |
256 | | routine main | |
257 | | outputs one | |
258 | | trashes a, x, n, z | |
259 | | { | |
260 | | ld x, 0 | |
261 | | st x, one | |
262 | | ld a, one | |
263 | | } | |
264 | = ok | |
265 | ||
266 | | byte one | |
267 | | byte table many | |
268 | | | |
269 | | routine main | |
270 | | outputs one | |
271 | | trashes a, x, n, z | |
272 | | { | |
273 | | ld x, 0 | |
274 | | st x, one | |
275 | | ld a, one + x | |
276 | | } | |
277 | ? TypeMismatchError | |
278 | ||
279 | | byte one | |
280 | | byte table many | |
281 | | | |
282 | | routine main | |
283 | | outputs many | |
284 | | trashes a, x, n, z | |
285 | | { | |
286 | | ld x, 0 | |
287 | | ld a, 0 | |
288 | | st a, many + x | |
289 | | ld a, many | |
290 | | } | |
291 | ? TypeMismatchError | |
292 | ||
293 | | byte one | |
294 | | byte table many | |
295 | | | |
296 | | routine main | |
297 | | outputs many | |
298 | | trashes a, x, n, z | |
299 | | { | |
300 | | ld x, 0 | |
301 | | ld a, 0 | |
302 | | st a, many + x | |
303 | | ld a, many + x | |
304 | | } | |
305 | = ok | |
196 | 306 | |
197 | 307 | ### add ### |
198 | 308 |