annotate python/ppci/core/asmwriter.py @ 107:1544e7a4aa98

Improvements
author Windel Bouwman
date Tue, 01 Jan 2013 17:16:05 +0100
parents
children 9e552d34bd60
rev   line source
107
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
1
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
2 from . import llvmtype
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
3 #typeNames[VoidType] = 'void'
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
4
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
5 class AsmWriter:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
6 def __init__(self):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
7 self.typeNames = {}
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
8 self.typeNames[llvmtype.typeID.Void] = 'void'
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
9 def printModule(self, module):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
10 if module.Identifier:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
11 print('; ModuleID = {0}'.format(module.Identifier))
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
12 #
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
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
24 for bb in f.BasicBlocks:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
25 print(bb)
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
26
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
27 print('}')
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
28
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
29 def strType(self, t):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
30 return self.typeNames[t]
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
31
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
32 def printBasicBlock(self, bb):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
33 if bb.Name:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
34 # print label
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
35 print('{0}:'.format(bb.Name))
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
36 for instr in bb.Instructions:
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
37 self.printInstruction(instr)
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
38 def printInstruction(self, i):
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
39 pass
1544e7a4aa98 Improvements
Windel Bouwman
parents:
diff changeset
40