annotate python/ppci/core/asmwriter.py @ 118:db8aafe00d27

Added elf file read scripts
author Windel Bouwman
date Sat, 12 Jan 2013 09:39:23 +0100
parents 9e552d34bd60
children
rev   line source
107
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
1
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
2 from . import llvmtype
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
3 from .instruction import BinaryOperator
107
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
4 #typeNames[VoidType] = 'void'
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
5
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
6 class AsmWriter:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
7 def __init__(self):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
8 self.typeNames = {}
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
9 self.typeNames[llvmtype.typeID.Void] = 'void'
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
10 def printModule(self, module):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
11 if module.Identifier:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
12 print('; ModuleID = {0}'.format(module.Identifier))
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
13 # Print functions:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
14 for f in module.Functions:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
15 self.printFunction(f)
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
16 def printFunction(self, f):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
17 # TODO: if definition:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
18
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
19 t = self.strType(f.ReturnType.tid)
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
20 args = '()'
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
21 print('define {0} {1}{2}'.format(t, f.name, args))
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
22 print('{')
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
23 for bb in f.BasicBlocks:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
24 print('basic block!')
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
25 self.printBasicBlock(bb)
107
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
26 print('}')
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
27
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
28 def strType(self, t):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
29 return self.typeNames[t]
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
30
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
31 def printBasicBlock(self, bb):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
32 if bb.Name:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
33 # print label
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
34 print('{0}:'.format(bb.Name))
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
35 for instr in bb.Instructions:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
36 self.printInstruction(instr)
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
37 def printInstruction(self, i):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
38 print('Instruction!')
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
39 if isinstance(i, BinaryOperator):
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
40 print(i.operation, i.value1.Name, i.value2.Name)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
41 else:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
42 print(i)
107
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
43