summaryrefslogtreecommitdiff
path: root/Function.py
blob: 38d01ea4e285ac6bc234d6d21857d766c9f9eb5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
"""Compiles C into assembly for the practicum processor (PP2)

All rights reserved, you may not redistribute or use this program without prior
permission from Peter Wu or Xander Houtman. Use of this program is entirely
your own risk. In no circumstances can the authors of this program be held
responsible for any damage including, but not limited to, financial damage or
data loss. Modification of this program is not allowed without prior
permission. The generated output (assembly and messages) are not subject to
this license.
"""

__author__ = "Peter Wu"
__copyright__ = "Copyright 2011, Peter Wu"
__credits__ = ["Peter Wu"]
__license__ = "Proprietary"
__version__ = "1.0"
__maintainer__ = "Peter Wu"
__email__ = "uwretep@gmail.com"

class Function(object):
    def __init__(self, decl_node):
        """Initializes an object for holding a function declaration and related
        properties

        Keyword arguments:
        decl_node -- A Node object 
        """
        assert type(decl_node).__name__ == "FuncDecl", ("decl_node is not a"
                                                        " function declaration")
        self.decl_node = decl_node
        self.name = self.decl_node.type.declname
        self.reserved_stack = 0
        self.linked_node = None
    def labelBegin(self):
        """Returns a label pointing to the begin of a function"""
        return "fn_" + self.name
    def labelEnd(self):
        """Returns a label pointing to the end of a function"""
        return "fne_" + self.name
    def allocStack(self, count=1):
        """Reserve some memory (for local variables and params) on the stack
        and return the begin of the allocated memory

        Keyword arguments:
        count -- The number of words to be allocated which should be at least 1
        """
        assert count > 0, "The stack allocation count must be at least 1"
        begin_address = self.reserved_stack + 1
        self.reserved_stack += count
        return begin_address
    def setLinkedNode(self, linked_node):
        """Assigns this function to a function node"""
        assert linked_node.type == "FuncDef", ("Only function definitions can"
                                               " be linked to a function")
        self.linked_node = linked_node
    def isLinked(self):
        """Returns True if this function object is already linked to a node"""
        return self.linked_node is not None