Mercurial > lcfOS
view python/x86.py @ 158:9683a4cd848f
Added some functions for code generation
author | Windel Bouwman |
---|---|
date | Fri, 08 Mar 2013 16:52:44 +0100 |
parents | 8f3924b6076e |
children | 10330be89bc2 |
line wrap: on
line source
import ppci import ir class AsmLabel: def __init__(self, lab): self.lab = lab def __repr__(self): return '{0}:'.format(self.lab) class Op: def __init__(self, op, a, b): self.op = op self.a = a self.b = b def __repr__(self): return '{0} {1}, {2}'.format(self.op, self.a, self.b) class X86CodeGen: def __init__(self, diag): self.diag = diag def emit(self, i): self.asm.append(i) def genBin(self, i): self.asm = [] self.genModule(i) def genModule(self, m): for g in m.Globals: self.emit(AsmLabel(g.name)) # Ignore types for now .. self.emit('dw 0') for f in m.Functions: self.genFunction(f) def genFunction(self, f): self.emit('global {0}'.format(f.name)) self.emit(AsmLabel(f.name)) for bb in f.BasicBlocks: self.genBB(bb) def genBB(self, bb): for i in bb.Instructions: self.genIns(i) def genIns(self, i): if type(i) is ir.BinaryOperator: if i.operation == 'fadd': r = 'rax' self.emit(Op('add', r, '11')) elif type(i) is ir.RetInstruction: self.emit('ret') else: print('Unknown ins', i)