git @ Cat's Eye Technologies SixtyPical / 78a1f29
Simplify and improve Emitter abstraction. Chris Pressey 3 years ago
2 changed file(s) with 15 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
88 TableType, PointerType, RoutineType, VectorType,
99 REG_A, REG_X, REG_Y, FLAG_C
1010 )
11 from sixtypical.emitter import Byte, Word, Table, Label, Offset, LowAddressByte, HighAddressByte, Emitter
11 from sixtypical.emitter import Byte, Word, Table, Label, Offset, LowAddressByte, HighAddressByte
1212 from sixtypical.gen6502 import (
1313 Immediate, Absolute, AbsoluteX, AbsoluteY, ZeroPage, Indirect, IndirectY, Relative,
1414 LDA, LDX, LDY, STA, STX, STY,
159159 assert isinstance(routine, Routine)
160160
161161 self.current_routine = routine
162 saved_emitter = self.emitter
163 self.emitter = Emitter(saved_emitter.addr)
162
164163 if routine.block:
165164 self.emitter.resolve_label(self.get_label(routine.name))
166165 self.compile_block(routine.block)
167166
168167 needs_rts = True
169 if self.emitter.accum:
170 last_op = self.emitter.accum[-1]
171 if isinstance(last_op, JMP):
172 needs_rts = False
173 if isinstance(last_op.operand, Absolute):
174 if isinstance(last_op.operand.value, Label):
175 if next_routine and last_op.operand.value.name == next_routine.name:
176 self.emitter.retract()
168 last_op = self.emitter.get_tail()
169 if isinstance(last_op, JMP):
170 needs_rts = False
171 if isinstance(last_op.operand, Absolute):
172 if isinstance(last_op.operand.value, Label):
173 if next_routine and last_op.operand.value.name == next_routine.name:
174 self.emitter.retract()
177175
178176 if needs_rts:
179177 self.emitter.emit(RTS())
180178
181 saved_emitter.emit(*self.emitter.accum)
182 self.emitter = saved_emitter
183179 self.current_routine = None
184180
185181 def compile_block(self, block):
170170 self.accum.append(thing)
171171 self.addr += thing.size()
172172
173 def get_tail(self):
174 if self.accum:
175 return self.accum[-1]
176 else:
177 return None
178
173179 def retract(self):
174180 thing = self.accum.pop()
175181 self.addr -= thing.size()