annotate python/ir/module.py @ 158:9683a4cd848f

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