SimpleAllocations.py 3.29 KB
Newer Older
Matthieu Moy's avatar
Matthieu Moy committed
1
2
3
4
5
6
7
8
9
from TP04.Operands import Temporary, S, GP_REGS, FP
from TP04.Instruction3A import Instru3A
from Errors import AllocationError


class Allocator():
    def __init__(self, function):
        self._function_code = function

Matthieu Moy's avatar
Matthieu Moy committed
10
    def prepare(self):  # pragma: nocover
Matthieu Moy's avatar
Matthieu Moy committed
11
12
        pass

Matthieu Moy's avatar
Matthieu Moy committed
13
    def rewriteCode(self, listcode):  # pragma: nocover
Matthieu Moy's avatar
Matthieu Moy committed
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        pass

    def run(self):
        self.prepare()
        self.rewriteCode(self._function_code)


class NaiveAllocator(Allocator):
    def __init__(self, *args):
        super().__init__(*args)

    # Allocation functions
    def prepare(self):
        """ Allocate all temporaries to registers.
        Fail if there are too many temporaries."""
        regs = list(GP_REGS)  # Get a writable copy
        temp_allocation = dict()
        for tmp in self._function_code._pool._all_temps:
            try:
                reg = regs.pop()
            except IndexError:
                raise AllocationError(
                    "Too many temporaries ({}) for the naive allocation, sorry."
                    .format(len(self._function_code._pool._all_temps)))
            temp_allocation[tmp] = reg
        self._function_code._pool.set_temp_allocation(temp_allocation)

    def rewriteCode(self, listcode):
        # Finally, modify the code to replace temporaries with
        # regs/memory locations
        listcode.iter_instructions(replace_reg)


def replace_reg(old_i):
    """Replace Temporary operands with
    the corresponding allocated register."""
    ins, old_args = old_i.unfold()
    args = []
    for arg in old_args:
        if isinstance(arg, Temporary):
            arg = arg.get_alloced_loc()
        args.append(arg)
    return [Instru3A(ins, args=args)]


class AllInMemAllocator(Allocator):
    def __init__(self, *args):
        super().__init__(*args)

    def prepare(self):
        """Allocate all temporaries to memory. Hypothesis:
        - Expanded instructions can use  s2 and
        s3 (to store the values of temporaries before the actual
        instruction).
        """
        self._function_code._pool.set_temp_allocation(
            {temp: self._function_code.new_offset(FP)
             for temp in self._function_code._pool._all_temps})
        self._function_code._stacksize = self._function_code.get_offset()
        if self._function_code._stacksize > 234:
            raise AllocationError(
                    "Too many temporaries ({}) for the all in memory allocation, sorry."
                    .format(len(self._function_code._pool._all_temps)))

    def rewriteCode(self, listcode):
        # Finally, modify the code to replace temporaries with
        # regs/memory locations
        listcode.iter_instructions(replace_mem)


def replace_mem(old_i):
    """Replace Temporary operands with the corresponding allocated
    memory location. FP points to the stack"""
    before = []
    after = []
    ins, old_args = old_i.unfold()
    args = []
    numreg = 1
    # TODO (Exercise 7): compute before,after,args.
    # TODO (Exercise 7): iterate over old_args, check which argument
    # TODO (Exercise 7): is a temporary (e.g. isinstance(..., Temporary)),
    # TODO (Exercise 7): and if so, generate ld/sd accordingly. Replace the
    # TODO (Exercise 7): temporary with S[1], S[2] or S[3] physical registers.
    i = Instru3A(ins, args=args)
    return before + [i] + after