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?
|
|
36 self.globals_ = [] # TODO: are globals in symbol table?
|
105
|
37 self.symtable = SymbolTable()
|
|
38 Globals = property(lambda self: self.globals_)
|
|
39 Functions = property(lambda self: self.functions)
|
106
|
40 Identifier = property(lambda self: self.identifier)
|
155
|
41
|
|
42 class Argument:
|
|
43 def __init__(self, argtype, name, function):
|
|
44 self.t = argtype
|
|
45 self.name = name
|
|
46 self.function = function
|
|
47
|
|
48 class Function:
|
156
|
49 def __init__(self, name, functiontype):
|
155
|
50 super().__init__()
|
156
|
51 self.name = name
|
155
|
52 self.functiontype = functiontype
|
|
53
|
|
54 self.basicblocks = []
|
|
55 self.arguments = []
|
|
56 BasicBlocks = property(lambda self: self.basicblocks)
|
|
57 Arguments = property(lambda self: self.arguments)
|
|
58 ReturnType = property(lambda self: self.functiontype.returnType)
|
|
59 FunctionType = property(lambda self: self.functiontype)
|
94
|
60
|
155
|
61 class BasicBlock:
|
|
62 """
|
|
63 A basic block represents a sequence of instructions without
|
|
64 jumps and branches.
|
|
65 """
|
|
66 def __init__(self):
|
|
67 super().__init__()
|
|
68 self.instructions = []
|
|
69 self.name = None
|
|
70 def getInstructions(self):
|
|
71 return self.instructions
|
|
72 Instructions = property(getInstructions)
|
|
73
|