annotate python/ir/module.py @ 157:8f3924b6076e

Added some code generator things
author Windel Bouwman
date Sun, 03 Mar 2013 18:14:35 +0100
parents 1b4a85bdd99c
children 9683a4cd848f
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?
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
36 self.globals_ = [] # TODO: are globals in symbol table?
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
37 self.symtable = SymbolTable()
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
38 Globals = property(lambda self: self.globals_)
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
39 Functions = property(lambda self: self.functions)
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
40 Identifier = property(lambda self: self.identifier)
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
41
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
42 class Argument:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
43 def __init__(self, argtype, name, function):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
44 self.t = argtype
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
45 self.name = name
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
46 self.function = function
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
47
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
48 class Function:
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
49 def __init__(self, name, functiontype):
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
50 super().__init__()
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
51 self.name = name
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
52 self.functiontype = functiontype
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
53
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
54 self.basicblocks = []
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
55 self.arguments = []
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
56 BasicBlocks = property(lambda self: self.basicblocks)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
57 Arguments = property(lambda self: self.arguments)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
58 ReturnType = property(lambda self: self.functiontype.returnType)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
59 FunctionType = property(lambda self: self.functiontype)
94
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
60
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
61 class BasicBlock:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
62 """
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
63 A basic block represents a sequence of instructions without
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
64 jumps and branches.
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
65 """
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
66 def __init__(self):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
67 super().__init__()
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
68 self.instructions = []
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
69 self.name = None
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
70 def getInstructions(self):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
71 return self.instructions
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
72 Instructions = property(getInstructions)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
73