Mercurial > lcfOS
comparison python/ppci/target/x86/x86.py @ 342:86b02c98a717 devel
Moved target directory
author | Windel Bouwman |
---|---|
date | Sat, 01 Mar 2014 15:40:31 +0100 |
parents | python/target/x86.py@7b38782ed496 |
children |
comparison
equal
deleted
inserted
replaced
341:4d204f6f7d4e | 342:86b02c98a717 |
---|---|
1 import ppci | |
2 import ir | |
3 | |
4 class X86CodeGenSimple: | |
5 """ | |
6 Inefficient code generation, assume stack machine | |
7 backend | |
8 """ | |
9 def __init__(self, diag): | |
10 self.diag = diag | |
11 | |
12 def emit(self, i): | |
13 self.asm.append(i) | |
14 | |
15 def genBin(self, ir): | |
16 self.asm = [] | |
17 self.genModule(ir) | |
18 return self.asm | |
19 | |
20 def genModule(self, ir): | |
21 for f in ir.Functions: | |
22 self.genFunction(f) | |
23 def genFunction(self, f): | |
24 self.emit('global {0}'.format(f.name)) | |
25 self.emit('{0}:'.format(f.name)) | |
26 self.emit('jmp {0}'.format(f.entry.name)) | |
27 for bb in f.BasicBlocks: | |
28 self.genBB(bb) | |
29 def genBB(self, bb): | |
30 self.emit('{0}:'.format(bb.name)) | |
31 for i in bb.Instructions: | |
32 self.genIns(i) | |
33 def genIns(self, i): | |
34 if type(i) is ir.BinaryOperator: | |
35 ops = {'+':'add', '-':'sub', '*':'mul'} | |
36 if i.operation in ops: | |
37 i.result.reg = 'rax' | |
38 i.value1.reg = 'rbx' | |
39 i.value2.reg = 'rbx' | |
40 self.emit('mov {0}, {1}'.format(i.result.reg, i.value1.reg)) | |
41 self.emit('{0} {1}, {2}'.format(ops[i.operation], i.result.reg, i.value2.reg)) | |
42 else: | |
43 raise NotImplementedError('op {0}'.format(i.operation)) | |
44 elif type(i) is ir.Load: | |
45 self.emit('mov {0}, [{1}]'.format(i.value, i.location)) | |
46 elif type(i) is ir.Return: | |
47 self.emit('ret') | |
48 elif type(i) is ir.Call: | |
49 self.emit('call') | |
50 elif type(i) is ir.ImmLoad: | |
51 self.emit('mov {0}, {1}'.format(i.target, i.value)) | |
52 elif type(i) is ir.Store: | |
53 self.emit('mov [{0}], {1}'.format(i.location, i.value)) | |
54 elif type(i) is ir.ConditionalBranch: | |
55 self.emit('cmp {0}, {1}'.format(i.a, i.b)) | |
56 jmps = {'>':'jg', '<':'jl', '==':'je'} | |
57 if i.cond in jmps: | |
58 j = jmps[i.cond] | |
59 self.emit('{0} {1}'.format(j, i.lab1.name)) | |
60 else: | |
61 raise NotImplementedError('condition {0}'.format(i.cond)) | |
62 self.emit('jmp {0}'.format(i.lab2.name)) | |
63 elif type(i) is ir.Branch: | |
64 self.emit('jmp {0}'.format(i.target.name)) | |
65 elif type(i) is ir.Alloc: | |
66 pass | |
67 else: | |
68 raise NotImplementedError('{0}'.format(i)) | |
69 |