git @ Cat's Eye Technologies SixtyPical / 71cded0
Move compiler towards being able to compile access to statics. Chris Pressey 3 years ago
2 changed file(s) with 106 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
4141 else:
4242 raise NotImplementedError(index)
4343
44 def compute_length_of_defn(self, defn):
45 length = None
46 type_ = defn.location.type
47 if type_ == TYPE_BYTE:
48 length = 1
49 elif type_ == TYPE_WORD or isinstance(type_, (PointerType, VectorType)):
50 length = 2
51 elif isinstance(type_, TableType):
52 length = type_.size * (1 if type_.of_type == TYPE_BYTE else 2)
53 elif isinstance(type_, BufferType):
54 length = type_.size
55 if length is None:
56 raise NotImplementedError("Need size for type {}".format(type_))
57 return length
58
59 def get_label(self, name):
60 return self.labels[name]
61
4462 # visitor methods
4563
4664 def compile_program(self, program):
4765 assert isinstance(program, Program)
4866
4967 for defn in program.defns:
50 # compute length of memory pointed to. this is awful.
51 length = None
52 type_ = defn.location.type
53 if type_ == TYPE_BYTE:
54 length = 1
55 elif type_ == TYPE_WORD or isinstance(type_, (PointerType, VectorType)):
56 length = 2
57 elif isinstance(type_, TableType):
58 length = type_.size * (1 if type_.of_type == TYPE_BYTE else 2)
59 elif isinstance(type_, BufferType):
60 length = type_.size
61 if length is None:
62 raise NotImplementedError("Need size for type {}".format(type_))
68 length = self.compute_length_of_defn(defn)
6369 self.labels[defn.name] = Label(defn.name, addr=defn.addr, length=length)
6470
6571 for routine in program.routines:
7682
7783 for location, label in self.trampolines.iteritems():
7884 self.emitter.resolve_label(label)
79 self.emitter.emit(JMP(Indirect(self.labels[location.name])))
85 self.emitter.emit(JMP(Indirect(self.get_label(location.name))))
8086 self.emitter.emit(RTS())
8187
8288 # initialized data
8389 for defn in program.defns:
8490 if defn.initial is not None:
85 label = self.labels[defn.name]
91 label = self.get_label(defn.name)
8692 initial_data = None
8793 type_ = defn.location.type
8894 if type_ == TYPE_BYTE:
100106 # uninitialized, "BSS" data
101107 for defn in program.defns:
102108 if defn.initial is None and defn.addr is None:
103 label = self.labels[defn.name]
109 label = self.get_label(defn.name)
104110 self.emitter.resolve_bss_label(label)
105111
106112
107113 def compile_routine(self, routine):
108114 assert isinstance(routine, Routine)
109115 if routine.block:
110 self.emitter.resolve_label(self.labels[routine.name])
116 self.emitter.resolve_label(self.get_label(routine.name))
111117 self.compile_block(routine.block)
112118 self.emitter.emit(RTS())
113119
131137 elif isinstance(src, ConstantRef):
132138 self.emitter.emit(LDA(Immediate(Byte(src.value))))
133139 elif isinstance(src, IndexedRef) and src.index == REG_X:
134 self.emitter.emit(LDA(AbsoluteX(self.labels[src.name])))
140 self.emitter.emit(LDA(AbsoluteX(self.get_label(src.name))))
135141 elif isinstance(src, IndexedRef) and src.index == REG_Y:
136 self.emitter.emit(LDA(AbsoluteY(self.labels[src.name])))
142 self.emitter.emit(LDA(AbsoluteY(self.get_label(src.name))))
137143 elif isinstance(src, IndirectRef) and isinstance(src.ref.type, PointerType):
138 self.emitter.emit(LDA(IndirectY(self.labels[src.ref.name])))
139 else:
140 self.emitter.emit(LDA(Absolute(self.labels[src.name])))
144 self.emitter.emit(LDA(IndirectY(self.get_label(src.ref.name))))
145 else:
146 self.emitter.emit(LDA(Absolute(self.get_label(src.name))))
141147 elif dest == REG_X:
142148 if src == REG_A:
143149 self.emitter.emit(TAX())
144150 elif isinstance(src, ConstantRef):
145151 self.emitter.emit(LDX(Immediate(Byte(src.value))))
146152 elif isinstance(src, IndexedRef) and src.index == REG_Y:
147 self.emitter.emit(LDX(AbsoluteY(self.labels[src.name])))
148 else:
149 self.emitter.emit(LDX(Absolute(self.labels[src.name])))
153 self.emitter.emit(LDX(AbsoluteY(self.get_label(src.name))))
154 else:
155 self.emitter.emit(LDX(Absolute(self.get_label(src.name))))
150156 elif dest == REG_Y:
151157 if src == REG_A:
152158 self.emitter.emit(TAY())
153159 elif isinstance(src, ConstantRef):
154160 self.emitter.emit(LDY(Immediate(Byte(src.value))))
155161 elif isinstance(src, IndexedRef) and src.index == REG_X:
156 self.emitter.emit(LDY(AbsoluteX(self.labels[src.name])))
157 else:
158 self.emitter.emit(LDY(Absolute(self.labels[src.name])))
162 self.emitter.emit(LDY(AbsoluteX(self.get_label(src.name))))
163 else:
164 self.emitter.emit(LDY(Absolute(self.get_label(src.name))))
159165 else:
160166 raise UnsupportedOpcodeError(instr)
161167 elif opcode == 'st':
175181 REG_X: AbsoluteX,
176182 REG_Y: AbsoluteY,
177183 }[dest.index]
178 label = self.labels[dest.ref.name]
184 label = self.get_label(dest.ref.name)
179185 elif isinstance(dest, IndirectRef) and isinstance(dest.ref.type, PointerType):
180186 mode_cls = IndirectY
181 label = self.labels[dest.ref.name]
187 label = self.get_label(dest.ref.name)
182188 else:
183189 mode_cls = Absolute
184 label = self.labels[dest.name]
190 label = self.get_label(dest.name)
185191
186192 if op_cls is None or mode_cls is None:
187193 raise UnsupportedOpcodeError(instr)
191197 if isinstance(src, ConstantRef):
192198 self.emitter.emit(ADC(Immediate(Byte(src.value))))
193199 else:
194 self.emitter.emit(ADC(Absolute(self.labels[src.name])))
200 self.emitter.emit(ADC(Absolute(self.get_label(src.name))))
195201 elif isinstance(dest, LocationRef) and src.type == TYPE_WORD and dest.type == TYPE_WORD:
196202 if isinstance(src, ConstantRef):
197 dest_label = self.labels[dest.name]
203 dest_label = self.get_label(dest.name)
198204 self.emitter.emit(LDA(Absolute(dest_label)))
199205 self.emitter.emit(ADC(Immediate(Byte(src.low_byte()))))
200206 self.emitter.emit(STA(Absolute(dest_label)))
202208 self.emitter.emit(ADC(Immediate(Byte(src.high_byte()))))
203209 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
204210 elif isinstance(src, LocationRef):
205 src_label = self.labels[src.name]
206 dest_label = self.labels[dest.name]
211 src_label = self.get_label(src.name)
212 dest_label = self.get_label(dest.name)
207213 self.emitter.emit(LDA(Absolute(dest_label)))
208214 self.emitter.emit(ADC(Absolute(src_label)))
209215 self.emitter.emit(STA(Absolute(dest_label)))
214220 raise UnsupportedOpcodeError(instr)
215221 elif isinstance(dest, LocationRef) and src.type == TYPE_WORD and isinstance(dest.type, PointerType):
216222 if isinstance(src, ConstantRef):
217 dest_label = self.labels[dest.name]
223 dest_label = self.get_label(dest.name)
218224 self.emitter.emit(LDA(ZeroPage(dest_label)))
219225 self.emitter.emit(ADC(Immediate(Byte(src.low_byte()))))
220226 self.emitter.emit(STA(ZeroPage(dest_label)))
222228 self.emitter.emit(ADC(Immediate(Byte(src.high_byte()))))
223229 self.emitter.emit(STA(ZeroPage(Offset(dest_label, 1))))
224230 elif isinstance(src, LocationRef):
225 src_label = self.labels[src.name]
226 dest_label = self.labels[dest.name]
231 src_label = self.get_label(src.name)
232 dest_label = self.get_label(dest.name)
227233 self.emitter.emit(LDA(ZeroPage(dest_label)))
228234 self.emitter.emit(ADC(Absolute(src_label)))
229235 self.emitter.emit(STA(ZeroPage(dest_label)))
239245 if isinstance(src, ConstantRef):
240246 self.emitter.emit(SBC(Immediate(Byte(src.value))))
241247 else:
242 self.emitter.emit(SBC(Absolute(self.labels[src.name])))
248 self.emitter.emit(SBC(Absolute(self.get_label(src.name))))
243249 elif isinstance(dest, LocationRef) and src.type == TYPE_WORD and dest.type == TYPE_WORD:
244250 if isinstance(src, ConstantRef):
245 dest_label = self.labels[dest.name]
251 dest_label = self.get_label(dest.name)
246252 self.emitter.emit(LDA(Absolute(dest_label)))
247253 self.emitter.emit(SBC(Immediate(Byte(src.low_byte()))))
248254 self.emitter.emit(STA(Absolute(dest_label)))
250256 self.emitter.emit(SBC(Immediate(Byte(src.high_byte()))))
251257 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
252258 elif isinstance(src, LocationRef):
253 src_label = self.labels[src.name]
254 dest_label = self.labels[dest.name]
259 src_label = self.get_label(src.name)
260 dest_label = self.get_label(dest.name)
255261 self.emitter.emit(LDA(Absolute(dest_label)))
256262 self.emitter.emit(SBC(Absolute(src_label)))
257263 self.emitter.emit(STA(Absolute(dest_label)))
268274 elif dest == REG_Y:
269275 self.emitter.emit(INY())
270276 else:
271 self.emitter.emit(INC(Absolute(self.labels[dest.name])))
277 self.emitter.emit(INC(Absolute(self.get_label(dest.name))))
272278 elif opcode == 'dec':
273279 if dest == REG_X:
274280 self.emitter.emit(DEX())
275281 elif dest == REG_Y:
276282 self.emitter.emit(DEY())
277283 else:
278 self.emitter.emit(DEC(Absolute(self.labels[dest.name])))
284 self.emitter.emit(DEC(Absolute(self.get_label(dest.name))))
279285 elif opcode == 'cmp':
280286 cls = {
281287 'a': CMP,
287293 if isinstance(src, ConstantRef):
288294 self.emitter.emit(cls(Immediate(Byte(src.value))))
289295 else:
290 self.emitter.emit(cls(Absolute(self.labels[src.name])))
296 self.emitter.emit(cls(Absolute(self.get_label(src.name))))
291297 elif opcode in ('and', 'or', 'xor',):
292298 cls = {
293299 'and': AND,
298304 if isinstance(src, ConstantRef):
299305 self.emitter.emit(cls(Immediate(Byte(src.value))))
300306 else:
301 self.emitter.emit(cls(Absolute(self.labels[src.name])))
307 self.emitter.emit(cls(Absolute(self.get_label(src.name))))
302308 else:
303309 raise UnsupportedOpcodeError(instr)
304310 elif opcode in ('shl', 'shr'):
312318 raise UnsupportedOpcodeError(instr)
313319 elif opcode == 'call':
314320 location = instr.location
315 label = self.labels[instr.location.name]
321 label = self.get_label(instr.location.name)
316322 if isinstance(location.type, RoutineType):
317323 self.emitter.emit(JSR(Absolute(label)))
318324 elif isinstance(location.type, VectorType):
324330 raise NotImplementedError
325331 elif opcode == 'goto':
326332 location = instr.location
327 label = self.labels[instr.location.name]
333 label = self.get_label(instr.location.name)
328334 if isinstance(location.type, RoutineType):
329335 self.emitter.emit(JMP(Absolute(label)))
330336 elif isinstance(location.type, VectorType):
382388 if isinstance(src, (LocationRef, ConstantRef)) and isinstance(dest, IndirectRef):
383389 if src.type == TYPE_BYTE and isinstance(dest.ref.type, PointerType):
384390 if isinstance(src, ConstantRef):
385 dest_label = self.labels[dest.ref.name]
391 dest_label = self.get_label(dest.ref.name)
386392 self.emitter.emit(LDA(Immediate(Byte(src.value))))
387393 self.emitter.emit(STA(IndirectY(dest_label)))
388394 elif isinstance(src, LocationRef):
389 src_label = self.labels[src.name]
390 dest_label = self.labels[dest.ref.name]
395 src_label = self.get_label(src.name)
396 dest_label = self.get_label(dest.ref.name)
391397 self.emitter.emit(LDA(Absolute(src_label)))
392398 self.emitter.emit(STA(IndirectY(dest_label)))
393399 else:
396402 raise NotImplementedError((src, dest))
397403 elif isinstance(src, IndirectRef) and isinstance(dest, LocationRef):
398404 if dest.type == TYPE_BYTE and isinstance(src.ref.type, PointerType):
399 src_label = self.labels[src.ref.name]
400 dest_label = self.labels[dest.name]
405 src_label = self.get_label(src.ref.name)
406 dest_label = self.get_label(dest.name)
401407 self.emitter.emit(LDA(IndirectY(src_label)))
402408 self.emitter.emit(STA(Absolute(dest_label)))
403409 else:
404410 raise NotImplementedError((src, dest))
405411 elif isinstance(src, AddressRef) and isinstance(dest, LocationRef) and \
406412 isinstance(src.ref.type, BufferType) and isinstance(dest.type, PointerType):
407 src_label = self.labels[src.ref.name]
408 dest_label = self.labels[dest.name]
413 src_label = self.get_label(src.ref.name)
414 dest_label = self.get_label(dest.name)
409415 self.emitter.emit(LDA(Immediate(HighAddressByte(src_label))))
410416 self.emitter.emit(STA(ZeroPage(dest_label)))
411417 self.emitter.emit(LDA(Immediate(LowAddressByte(src_label))))
412418 self.emitter.emit(STA(ZeroPage(Offset(dest_label, 1))))
413419 elif isinstance(src, LocationRef) and isinstance(dest, IndexedRef):
414420 if src.type == TYPE_WORD and TableType.is_a_table_type(dest.ref.type, TYPE_WORD):
415 src_label = self.labels[src.name]
416 dest_label = self.labels[dest.ref.name]
421 src_label = self.get_label(src.name)
422 dest_label = self.get_label(dest.ref.name)
417423 self.emitter.emit(LDA(Absolute(src_label)))
418424 self.emitter.emit(STA(self.addressing_mode_for_index(dest.index)(dest_label)))
419425 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
420426 self.emitter.emit(STA(self.addressing_mode_for_index(dest.index)(Offset(dest_label, 256))))
421427 elif isinstance(src.type, VectorType) and isinstance(dest.ref.type, TableType) and isinstance(dest.ref.type.of_type, VectorType):
422428 # FIXME this is the exact same as above - can this be simplified?
423 src_label = self.labels[src.name]
424 dest_label = self.labels[dest.ref.name]
429 src_label = self.get_label(src.name)
430 dest_label = self.get_label(dest.ref.name)
425431 self.emitter.emit(LDA(Absolute(src_label)))
426432 self.emitter.emit(STA(self.addressing_mode_for_index(dest.index)(dest_label)))
427433 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
428434 self.emitter.emit(STA(self.addressing_mode_for_index(dest.index)(Offset(dest_label, 256))))
429435 elif isinstance(src.type, RoutineType) and isinstance(dest.ref.type, TableType) and isinstance(dest.ref.type.of_type, VectorType):
430 src_label = self.labels[src.name]
431 dest_label = self.labels[dest.ref.name]
436 src_label = self.get_label(src.name)
437 dest_label = self.get_label(dest.ref.name)
432438 self.emitter.emit(LDA(Immediate(HighAddressByte(src_label))))
433439 self.emitter.emit(STA(self.addressing_mode_for_index(dest.index)(dest_label)))
434440 self.emitter.emit(LDA(Immediate(LowAddressByte(src_label))))
437443 raise NotImplementedError
438444 elif isinstance(src, ConstantRef) and isinstance(dest, IndexedRef):
439445 if src.type == TYPE_WORD and TableType.is_a_table_type(dest.ref.type, TYPE_WORD):
440 dest_label = self.labels[dest.ref.name]
446 dest_label = self.get_label(dest.ref.name)
441447 self.emitter.emit(LDA(Immediate(Byte(src.low_byte()))))
442448 self.emitter.emit(STA(self.addressing_mode_for_index(dest.index)(dest_label)))
443449 self.emitter.emit(LDA(Immediate(Byte(src.high_byte()))))
446452 raise NotImplementedError
447453 elif isinstance(src, IndexedRef) and isinstance(dest, LocationRef):
448454 if TableType.is_a_table_type(src.ref.type, TYPE_WORD) and dest.type == TYPE_WORD:
449 src_label = self.labels[src.ref.name]
450 dest_label = self.labels[dest.name]
455 src_label = self.get_label(src.ref.name)
456 dest_label = self.get_label(dest.name)
451457 self.emitter.emit(LDA(self.addressing_mode_for_index(src.index)(src_label)))
452458 self.emitter.emit(STA(Absolute(dest_label)))
453459 self.emitter.emit(LDA(self.addressing_mode_for_index(src.index)(Offset(src_label, 256))))
454460 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
455461 elif isinstance(dest.type, VectorType) and isinstance(src.ref.type, TableType) and isinstance(src.ref.type.of_type, VectorType):
456462 # FIXME this is the exact same as above - can this be simplified?
457 src_label = self.labels[src.ref.name]
458 dest_label = self.labels[dest.name]
463 src_label = self.get_label(src.ref.name)
464 dest_label = self.get_label(dest.name)
459465 self.emitter.emit(LDA(self.addressing_mode_for_index(src.index)(src_label)))
460466 self.emitter.emit(STA(Absolute(dest_label)))
461467 self.emitter.emit(LDA(self.addressing_mode_for_index(src.index)(Offset(src_label, 256))))
469475 if isinstance(src, ConstantRef):
470476 raise NotImplementedError
471477 else:
472 src_label = self.labels[src.name]
473 dest_label = self.labels[dest.name]
478 src_label = self.get_label(src.name)
479 dest_label = self.get_label(dest.name)
474480 self.emitter.emit(LDA(Absolute(src_label)))
475481 self.emitter.emit(STA(Absolute(dest_label)))
476482 elif src.type == TYPE_WORD and dest.type == TYPE_WORD:
477483 if isinstance(src, ConstantRef):
478 dest_label = self.labels[dest.name]
484 dest_label = self.get_label(dest.name)
479485 self.emitter.emit(LDA(Immediate(Byte(src.low_byte()))))
480486 self.emitter.emit(STA(Absolute(dest_label)))
481487 self.emitter.emit(LDA(Immediate(Byte(src.high_byte()))))
482488 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
483489 else:
484 src_label = self.labels[src.name]
485 dest_label = self.labels[dest.name]
490 src_label = self.get_label(src.name)
491 dest_label = self.get_label(dest.name)
486492 self.emitter.emit(LDA(Absolute(src_label)))
487493 self.emitter.emit(STA(Absolute(dest_label)))
488494 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
489495 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
490496 elif isinstance(src.type, VectorType) and isinstance(dest.type, VectorType):
491 src_label = self.labels[src.name]
492 dest_label = self.labels[dest.name]
497 src_label = self.get_label(src.name)
498 dest_label = self.get_label(dest.name)
493499 self.emitter.emit(LDA(Absolute(src_label)))
494500 self.emitter.emit(STA(Absolute(dest_label)))
495501 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
496502 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
497503 elif isinstance(src.type, RoutineType) and isinstance(dest.type, VectorType):
498 src_label = self.labels[src.name]
499 dest_label = self.labels[dest.name]
504 src_label = self.get_label(src.name)
505 dest_label = self.get_label(dest.name)
500506 self.emitter.emit(LDA(Immediate(HighAddressByte(src_label))))
501507 self.emitter.emit(STA(Absolute(dest_label)))
502508 self.emitter.emit(LDA(Immediate(LowAddressByte(src_label))))
945945 = $080D TAX
946946 = $080E LDA #$00
947947 = $0810 RTS
948
949 ### static ###
950
951 Memory locations defined static to a routine are allocated
952 just the same as initialized global storage locations are.
953
954 | define foo routine
955 | inputs x
956 | outputs x
957 | trashes z, n
958 | static byte t : 7
959 | {
960 | st x, t
961 | inc t
962 | ld x, t
963 | }
964 |
965 | define main routine
966 | trashes a, x, z, n
967 | static byte t : 77
968 | {
969 | ld x, t
970 | call foo
971 | }
972 = ???