annotate python/ir/module.py @ 155:b28a11c01dbe

Simplified IR classes
author Windel Bouwman
date Sun, 03 Mar 2013 13:20:03 +0100
parents 4e79484a9d47
children 1b4a85bdd99c
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
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
3 class Module:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
4 """ Main container for a piece of code. Contains globals and functions. """
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
5 def __init__(self, name):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
6 self.name = name
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
7 self.functions = [] # Do functions come out of symbol table?
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
8 self.globals_ = [] # TODO: are globals in symbol table?
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
9 self.symtable = SymbolTable()
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
10
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
11 Globals = property(lambda self: self.globals_)
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
12 Functions = property(lambda self: self.functions)
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
13 Identifier = property(lambda self: self.identifier)
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
14
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
15 class Argument:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
16 def __init__(self, argtype, name, function):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
17 self.t = argtype
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
18 self.name = name
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
19 self.function = function
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
20
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
21 class Function:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
22 def __init__(self, functiontype, name, module):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
23 super().__init__()
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
24 self.functiontype = functiontype
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
25 self.name = name
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
26 self.module = module
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
27
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
28 self.module.Functions.append(self)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
29 self.basicblocks = []
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
30 self.arguments = []
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
31 # Construct formal arguments depending on function type
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
32
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
33 BasicBlocks = property(lambda self: self.basicblocks)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
34 Arguments = property(lambda self: self.arguments)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
35 ReturnType = property(lambda self: self.functiontype.returnType)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
36 FunctionType = property(lambda self: self.functiontype)
94
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
37
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
38 class BasicBlock:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
39 """
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
40 A basic block represents a sequence of instructions without
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
41 jumps and branches.
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
42 """
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
43 def __init__(self):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
44 super().__init__()
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
45 self.instructions = []
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
46 self.name = None
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
47 def getInstructions(self):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
48 return self.instructions
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
49 Instructions = property(getInstructions)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
50