diff --git a/src/sixtypical/compiler.py b/src/sixtypical/compiler.py new file mode 100644 index 0000000..1000fd1 --- /dev/null +++ b/src/sixtypical/compiler.py @@ -0,0 +1,83 @@ +# encoding: UTF-8 + +from sixtypical.ast import Program, Routine, Block, Instr +from sixtypical.model import ( + ConstantRef, LocationRef, + REG_A, REG_X, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C +) +from sixtypical.gen6502 import Generator + + +def compile_program(program): + assert isinstance(program, Program) + generator = Generator(49152) + routines = {r.name: r for r in program.routines} + for routine in program.routines: + compile_routine(routine, generator, routines) + + +def compile_routine(routine, generator, routines): + assert isinstance(routine, Routine) + label = generator.emitter.make_label(routine.name) + compile_block(routine.block, generator, routines) + return label + + +def compile_block(block, generator, routines): + assert isinstance(block, Block) + label = generator.emitter.make_label() + for instr in block.instrs: + compile_instr(instr, generator, routines) + return label + + +def compile_instr(instr, generator, routines): + assert isinstance(instr, Instr) + opcode = instr.opcode + dest = instr.dest + src = instr.src + + if opcode == 'ld': + if dest == REG_A: + if isinstance(src, ConstantRef): + # LDA #... + pass + elif: + # LDA abs + pass + elif dest == REG_X: + elif dest == REG_Y: + else: + raise KeyError + elif opcode == 'st': + if src == REG_A: + # assert isinstance(dest, MemoryRef) + # generate STA + else: + raise KeyError + elif opcode == 'add': + raise NotImplementedError + elif opcode == 'sub': + raise NotImplementedError + elif opcode == 'inc': + raise NotImplementedError + elif opcode == 'dec': + raise NotImplementedError + elif opcode == 'cmp': + raise NotImplementedError + elif opcode == 'and': + raise NotImplementedError + elif opcode == 'or': + raise NotImplementedError + elif opcode == 'xor': + raise NotImplementedError + elif opcode == 'shl': + raise NotImplementedError + elif opcode == 'shr': + raise NotImplementedError + elif opcode == 'call': + raise NotImplementedError + elif opcode == 'if': + raise NotImplementedError + else: + raise NotImplementedError diff --git a/src/sixtypical/model.py b/src/sixtypical/model.py index 6d97dd7..b869601 100644 --- a/src/sixtypical/model.py +++ b/src/sixtypical/model.py @@ -7,6 +7,9 @@ class LocationRef(object): def __repr__(self): return 'LocationRef(%r)' % self.name + def __eq__(self, other): + return isinstance(other, LocationRef) and other.name == self.name + class ConstantRef(object): def __init__(self, value): @@ -15,6 +18,9 @@ class ConstantRef(object): def __repr__(self): return 'ConstantRef(%r)' % self.value + def __eq__(self, other): + return isinstance(other, ConstantRef) and other.value == self.value + # TODO type=byte