-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackframe.c
118 lines (110 loc) · 2.26 KB
/
stackframe.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This contains the code for handling the stack frames
formed due to blocks and function calls.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "enums.h"
StackNode * CheckVar(char * name)
{
StackNode *curr = StackTop;
while(curr!=NULL){
if (curr->Group==Variable)
{
if(strcmp(name, (curr->variable).Name)==0)
{
return curr;
}
else
{
curr = curr->prev;
}
}
else
{
curr = curr->prev;
}
}
return NULL;
}
StackNode * AddNode()
{
StackNode *s = calloc(1, sizeof(StackNode));
s->id = StackTop->id +1;
s->level = Level;
s->offset = StackTop->offset;
StackTop->next = s;
s->prev = StackTop;
StackTop = s;
s->next = NULL;
return s;
}
void RemoveNode(StackNode *s)
{
if (s==StackTop)
{
StackTop = s->prev;
free(s);
StackTop->next = NULL;
}
else if (s->Group==Codeblock)
{
(s->next)->prev = s->prev;
(s->prev)->next = s->next;
free(s);
}
}
// Loop Node Add and Remove
void LoopAdd()
{
struct LoopNode * t = calloc(1, sizeof(struct LoopNode));
t->level = Level;
t->prev = LoopTop;
for (int i = 0; i < NUM_REG; ++i)
{
t->spill[i]=0;
}
t->offset = StackTop->offset;
LoopTop = t;
}
codeBlock * LoopRemove()
{
struct LoopNode * t = LoopTop->prev;
codeBlock *c = Assign("");
for (int i = 0; i < NUM_REG; ++i)
{
if (TArray[i].Group == Variable && TArray[i].snode->level < Level && TArray[i].load_level < TArray[i].access_level)
{
if (LoopTop->offset +4 > TArray[i].snode->offset)
Concat(c, Assign("sw $t%i, %lu($sp)\n", i, (LoopTop->offset - TArray[i].snode->offset + 4)));
}
}
for (int i = 0; i < NUM_REG; ++i)
{
for (int j = 0; j < NUM_REG; ++j)
{
if (TArray[j].Group == Variable && TArray[j].snode->level < Level && TArray[j].load_level < TArray[j].access_level)
{
if (LoopTop->spill[i]==(StackTop->offset - TArray[j].snode->offset + 4))
{
if (i!=j)
{
Concat(c, Assign("move $t%i, $t%i\n", i, j));
LoopTop->spill[i]=0;
break;
}
LoopTop->spill[i]=0;
break;
}
}
}
if (LoopTop->spill[i]>0)
{
Concat(c, Assign("lw $t%i, %lu($sp)\n", i, LoopTop->spill[i]));
}
}
free(LoopTop);
LoopTop = t;
return c;
}