107
|
1
|
|
2 from . import llvmtype
|
110
|
3 from .instruction import BinaryOperator
|
107
|
4 #typeNames[VoidType] = 'void'
|
|
5
|
|
6 class AsmWriter:
|
|
7 def __init__(self):
|
|
8 self.typeNames = {}
|
|
9 self.typeNames[llvmtype.typeID.Void] = 'void'
|
|
10 def printModule(self, module):
|
|
11 if module.Identifier:
|
|
12 print('; ModuleID = {0}'.format(module.Identifier))
|
|
13 # Print functions:
|
|
14 for f in module.Functions:
|
|
15 self.printFunction(f)
|
|
16 def printFunction(self, f):
|
|
17 # TODO: if definition:
|
|
18
|
|
19 t = self.strType(f.ReturnType.tid)
|
|
20 args = '()'
|
|
21 print('define {0} {1}{2}'.format(t, f.name, args))
|
|
22 print('{')
|
|
23 for bb in f.BasicBlocks:
|
110
|
24 print('basic block!')
|
|
25 self.printBasicBlock(bb)
|
107
|
26 print('}')
|
|
27
|
|
28 def strType(self, t):
|
|
29 return self.typeNames[t]
|
|
30
|
|
31 def printBasicBlock(self, bb):
|
|
32 if bb.Name:
|
|
33 # print label
|
|
34 print('{0}:'.format(bb.Name))
|
|
35 for instr in bb.Instructions:
|
|
36 self.printInstruction(instr)
|
|
37 def printInstruction(self, i):
|
110
|
38 print('Instruction!')
|
|
39 if isinstance(i, BinaryOperator):
|
|
40 print(i.operation, i.value1.Name, i.value2.Name)
|
|
41 else:
|
|
42 print(i)
|
107
|
43
|