comparison python/ir/module.py @ 170:4348da5ca307

Cleanup of ir dir
author Windel Bouwman
date Fri, 29 Mar 2013 17:33:17 +0100
parents 9683a4cd848f
children 3eb9b9e2958d
comparison
equal deleted inserted replaced
169:ee0d30533dae 170:4348da5ca307
1 from .symboltable import SymbolTable
2
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: 1 # IR-Structures:
30 2
31 class Module: 3 class Module:
32 """ Main container for a piece of code. Contains globals and functions. """ 4 """ Main container for a piece of code. """
33 def __init__(self, name): 5 def __init__(self, name):
34 self.name = name 6 self.name = name
35 self.functions = [] # Do functions come out of symbol table? 7 self.instructions = []
36 self.globs = [] # TODO: are globals in symbol table?
37 self.symtable = SymbolTable()
38 Globals = property(lambda self: self.globs)
39 Functions = property(lambda self: self.functions)
40 def __repr__(self): 8 def __repr__(self):
41 return 'IR-mod {0}'.format(self.name) 9 return 'IR-module [{0}]'.format(self.name)
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:
50 def __init__(self, name, functiontype):
51 super().__init__()
52 self.name = name
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)
61 def __repr__(self):
62 return 'FUNC {0}'.format(self.name)
63
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 = []
72 self.label = None
73 def getInstructions(self): 10 def getInstructions(self):
74 return self.instructions 11 return self.instructions
75 Instructions = property(getInstructions) 12 Instructions = property(getInstructions)
13 def dump(self):
14 print(self)
15 for i in self.Instructions:
16 print(i)
17 print('END')
76 18
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