git @ Cat's Eye Technologies SixtyPical / e62fd85
Compile access of defined memory locations. Chris Pressey 6 years ago
4 changed file(s) with 51 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
0 routine add_four
0 routine main
11 inputs a
22 outputs a
33 trashes c, z, n, v
77 from sixtypical.emitter import Label, Byte
88 from sixtypical.gen6502 import (
99 Immediate, Absolute,
10 LDA, LDX, LDY, STA, STX, STY, CLC, SEC, ADC, RTS, JSR
10 LDA, LDX, LDY, STA, STX, STY, CLC, SEC, ADC, RTS, JSR,
11 INC, INX, INY, DEC, DEX, DEY,
1112 )
1213
1314
2324
2425 def compile_program(self, program):
2526 assert isinstance(program, Program)
27
28 for defn in program.defns:
29 label = Label(defn.name)
30 self.labels[defn.name] = label
31
2632 for routine in program.routines:
2733 self.routines[routine.name] = routine
2834 label = Label(routine.name)
3440 for routine in program.routines:
3541 if routine.name != 'main':
3642 self.compile_routine(routine)
43
44 for defn in program.defns:
45 label = self.labels[defn.name]
46 self.emitter.resolve_bss_label(label)
3747
3848 def compile_routine(self, routine):
3949 assert isinstance(routine, Routine)
91101 if isinstance(src, ConstantRef):
92102 self.emitter.emit(ADC(Immediate(Byte(src.value))))
93103 else:
94 self.emitter.emit(ADC(Absolute(src.label)))
104 self.emitter.emit(ADC(Absolute(self.labels[src.name])))
95105 else:
96106 raise UnsupportedOpcodeError(instr)
97107 elif opcode == 'sub':
98 raise NotImplementedError
108 if dest == REG_A:
109 if isinstance(src, ConstantRef):
110 self.emitter.emit(SBC(Immediate(Byte(src.value))))
111 else:
112 self.emitter.emit(SBC(Absolute(self.labels[src.name])))
113 else:
114 raise UnsupportedOpcodeError(instr)
99115 elif opcode == 'inc':
100 raise NotImplementedError
116 if dest == REG_X:
117 self.emitter.emit(INX())
118 elif dest == REG_Y:
119 self.emitter.emit(INY())
120 else:
121 self.emitter.emit(INC(Absolute(self.labels[dest.name])))
101122 elif opcode == 'dec':
102 raise NotImplementedError
123 if dest == REG_X:
124 self.emitter.emit(DEX())
125 elif dest == REG_Y:
126 self.emitter.emit(DEY())
127 else:
128 self.emitter.emit(DEC(Absolute(self.labels[dest.name])))
103129 elif opcode == 'cmp':
104130 raise NotImplementedError
105131 elif opcode == 'and':
9494
9595 def resolve_label(self, label):
9696 label.set_addr(self.addr)
97
98 def resolve_bss_label(self, label):
99 """Set the given label to be at the current address and
100 advance the address for the next label, but don't emit anything."""
101 self.resolve_label(label)
102 self.addr += label.size()
6262 | call foo
6363 | }
6464 = 00c02004c060a900a200a00060
65
66 Access a defined memory location.
67
68 | byte foo
69 |
70 | routine main
71 | trashes a, y, z, n, foo
72 | {
73 | ld y, 0
74 | st y, foo
75 | ld a, foo
76 | }
77 = 00c0a0008c09c0ad09c060