git @ Cat's Eye Technologies SixtyPical / 0b2b5d9
Implement indirect JSRs with trampolines in theory; doesn't work. Chris Pressey 6 years ago
3 changed file(s) with 39 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
1313
1414 import codecs
1515 from optparse import OptionParser
16 from pprint import pprint
1617 import sys
1718 import traceback
1819
8384 compiler = Compiler(emitter)
8485 compiler.compile_program(program)
8586 if options.debug:
86 print repr(emitter.accum)
87 pprint(emitter.accum)
8788 else:
8889 emitter.serialize(fh)
8990
0 vector foo outputs x trashes z, n
0 vector print
1 trashes a, z, n
12
2 routine bar outputs x trashes z, n {
3 ld x, 200
3 routine chrout
4 inputs a
5 trashes a
6 @ 65490
7
8 routine printa
9 trashes a, z, n
10 {
11 ld a, 65
12 call chrout
413 }
514
6 routine main inputs bar outputs x, foo trashes a, z, n {
7 copy bar, foo
8 call foo
15 routine printb
16 trashes a, z, n
17 {
18 ld a, 66
19 call chrout
920 }
21
22 routine main
23 inputs printa, printb
24 trashes print, a, z, n
25 {
26 copy printa, print
27 call print
28 copy printb, print
29 call print
30 }
2929 self.emitter = emitter
3030 self.routines = {}
3131 self.labels = {}
32 self.trampolines = {} # Location -> Label
3233
3334 def compile_program(self, program):
3435 assert isinstance(program, Program)
5152 if routine.name != 'main':
5253 self.compile_routine(routine)
5354
55 for location, label in self.trampolines.iteritems():
56 self.emitter.resolve_label(label)
57 self.emitter.emit(JMP(Indirect(self.labels[location.name])))
58
5459 for defn in program.defns:
5560 if defn.addr is None:
5661 label = self.labels[defn.name]
5762 self.emitter.resolve_bss_label(label)
63
5864
5965 def compile_routine(self, routine):
6066 assert isinstance(routine, Routine)
197203 if isinstance(location.type, RoutineType):
198204 self.emitter.emit(JSR(Absolute(label)))
199205 elif isinstance(location.type, VectorType):
200 # XXX NOT QUITE RIGHT, IS IT?
201 # We need to simulate an indirect JSR!
202 self.emitter.emit(JMP(Indirect(label)))
206 trampoline = self.trampolines.setdefault(
207 location, Label(location.name + '_trampoline')
208 )
209 self.emitter.emit(JSR(Absolute(trampoline)))
203210 else:
204211 raise NotImplementedError
205212 elif opcode == 'goto':