105
|
1 from .symboltable import SymbolTable
|
104
|
2
|
157
|
3 # Types:
|
|
4 class Type:
|
|
5 def __init__(self):
|
|
6 pass
|
|
7
|
|
8 class IntegerType(Type):
|
|
9 def __init__(self, bits):
|
|
10 super().__init__()
|
|
11 self.bits = bits
|
|
12
|
|
13 class VoidType(Type):
|
|
14 pass
|
|
15
|
|
16 class FunctionType(Type):
|
|
17 def __init__(self, resultType, parameterTypes):
|
|
18 super().__init__()
|
|
19 assert type(parameterTypes) is list
|
|
20 self.resultType = resultType
|
|
21 self.parameterTypes = parameterTypes
|
|
22
|
|
23 # Default types:
|
|
24 i8 = IntegerType(8)
|
|
25 i16 = IntegerType(16)
|
|
26 i32 = IntegerType(32)
|
|
27 void = VoidType()
|
|
28
|
|
29 # IR-Structures:
|
|
30
|
155
|
31 class Module:
|
|
32 """ Main container for a piece of code. Contains globals and functions. """
|
|
33 def __init__(self, name):
|
|
34 self.name = name
|
106
|
35 self.functions = [] # Do functions come out of symbol table?
|
158
|
36 self.globs = [] # TODO: are globals in symbol table?
|
105
|
37 self.symtable = SymbolTable()
|
158
|
38 Globals = property(lambda self: self.globs)
|
105
|
39 Functions = property(lambda self: self.functions)
|
158
|
40 def __repr__(self):
|
|
41 return 'IR-mod {0}'.format(self.name)
|
155
|
42
|
|
43 class Argument:
|
|
44 def __init__(self, argtype, name, function):
|
|
45 self.t = argtype
|
|
46 self.name = name
|
|
47 self.function = function
|
|
48
|
|
49 class Function:
|
156
|
50 def __init__(self, name, functiontype):
|
155
|
51 super().__init__()
|
156
|
52 self.name = name
|
155
|
53 self.functiontype = functiontype
|
|
54
|
|
55 self.basicblocks = []
|
|
56 self.arguments = []
|
|
57 BasicBlocks = property(lambda self: self.basicblocks)
|
|
58 Arguments = property(lambda self: self.arguments)
|
|
59 ReturnType = property(lambda self: self.functiontype.returnType)
|
|
60 FunctionType = property(lambda self: self.functiontype)
|
158
|
61 def __repr__(self):
|
|
62 return 'FUNC {0}'.format(self.name)
|
94
|
63
|
155
|
64 class BasicBlock:
|
|
65 """
|
|
66 A basic block represents a sequence of instructions without
|
|
67 jumps and branches.
|
|
68 """
|
|
69 def __init__(self):
|
|
70 super().__init__()
|
|
71 self.instructions = []
|
158
|
72 self.label = None
|
155
|
73 def getInstructions(self):
|
|
74 return self.instructions
|
|
75 Instructions = property(getInstructions)
|
|
76
|
158
|
77 def printIr(md):
|
|
78 print(md)
|
|
79 for g in md.Globals:
|
|
80 print(g)
|
|
81 for f in md.Functions:
|
|
82 print(f)
|
|
83 for bb in f.BasicBlocks:
|
|
84 print('{0}:'.format(bb))
|
|
85 for ins in bb.Instructions:
|
|
86 print(' {0}'.format(ins))
|
|
87 print()
|
|
88
|