-
Notifications
You must be signed in to change notification settings - Fork 0
/
regman.c
90 lines (81 loc) · 1.84 KB
/
regman.c
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
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
/*
Manages the register allocation and code construction for memory loading
*/
#include <stdlib.h>
#include <stdio.h>
#include "enums.h"
struct GetReg GetRegister()
{
struct GetReg reg;
reg.i = -1;
reg.c = NULL;
int i = 0;
// Returns if reg empty or out-of-scope
// Returns reg if variable level is out-of-scope
for (int j = 0; j < NUM_REG; ++j)
{
i = (j+reg_rot) % NUM_REG;
reg.i = i;
if (TArray[i].snode==NULL)
{
reg_rot++;
reg_rot = reg_rot % NUM_REG;
return reg;
}
else if (TArray[i].snode->id > StackTop->id)
{
reg_rot++;
reg_rot = reg_rot % NUM_REG;
return reg;
}
else if (TArray[i].load_level > Level)
{
reg_rot++;
reg_rot = reg_rot % NUM_REG;
return reg;
}
}
// Removing a variable which is in same scope
for (int j = 0; j < NUM_REG; ++j)
{
i = (j+reg_rot) % NUM_REG;
if ((TArray[i].load_level == Level) && (TArray[i].Group == Variable))
{
reg.i = i;
if (TArray[i].load_level < TArray[i].access_level)
reg.c = Assign("sw $t%i, %lu($sp)\n", i, StackTop->offset - TArray[i].snode->offset + 4);
reg_rot++;
reg_rot = reg_rot % NUM_REG;
return reg;
}
}
// Removing a variable from previous scopes
for (int j = 0; j < NUM_REG; ++j)
{
i = (j + reg_rot) % NUM_REG;
if (TArray[i].Group == Variable)
{
reg.i = i;
if (LoopTop->level < Level)
{
LoopTop->spill[i] = LoopTop->offset - TArray[i].snode->offset + 4;
}
if (TArray[i].load_level < TArray[i].access_level)
reg.c = Assign("sw $t%i, %lu($sp)\n", i, StackTop->offset - TArray[i].snode->offset + 4);
reg_rot++;
reg_rot = reg_rot % NUM_REG;
return reg;
}
}
}
int CheckVarReg(StackNode *snode)
{
for (int j = 0; j < NUM_REG; ++j)
{
if (TArray[j].snode == snode)
{
return j;
}
}
return -1;
}