-
Notifications
You must be signed in to change notification settings - Fork 1
/
varpool.cpp
121 lines (109 loc) · 3.34 KB
/
varpool.cpp
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
119
120
121
#include "varpool.h"
varPool *varPool_new()
{
varPool *p = (varPool*)calloc(1, sizeof(varPool));
assert(p != NULL);
if (p == NULL)
return p;
p->inMemCnt = 0;
p->inMemCap = 20; //TODO
p->overall = 0;
p->inReg = (Var*)calloc(REG_CNT_ + p->inMemCap, sizeof(Var));
if (p->inReg == NULL) {
free(p->inReg);
free(p);
return NULL;
}
for (size_t i = 0; i < REG_CNT_; ++i) {
p->inReg[i].reg = (REGISTER_)i;
p->inReg[i].offset = -1;
p->inReg[i].num = 0;
}
p->inMem = p->inReg + REG_CNT_;
p->inReg[REG_NONE_].allocated = true;
p->inReg[RAX].allocated = true;
p->inReg[R10].allocated = true;
p->inReg[RSP].allocated = true;
p->inReg[RBP].allocated = true;
return p;
}
varPool *varPool_delete(varPool *p)
{
assert(p != NULL);
free(p->inReg);
free(p);
return NULL;
}
Var *varPool_allocInMem(varPool *p, size_t nodeId)
{
size_t i;
for (i = 0; i < p->inMemCap; ++i) {
if (!p->inMem[i].allocated)
break;
}
if (i == p->inMemCap) {
void *tmp = realloc(p->inReg, (REG_CNT_ + p->inMemCap * 2) * sizeof(Var));
if (tmp == NULL) {
return NULL;
}
p->inReg = (Var*)tmp;
p->inMem = p->inReg + REG_CNT_;
memset(p->inMem + p->inMemCap, 0, p->inMemCap * sizeof(Var));
p->inMemCap *= 2;
}
if (i + 1 > p->inMemCnt)
p->inMemCnt = i + 1;
Var *res = p->inMem + i;
assert(!res->allocated);
res->offset = i;
res->reg = REG_NONE_;
res->allocated = true;
res->temp = (nodeId == -1);
res->nodeId = nodeId;
++p->overall;
return res;
}
Var *varPool_alloc(varPool *p, size_t nodeId)
{
assert(p != NULL);
size_t i;
for (i = 1; i < REG_CNT_ && p->inReg[i].allocated; ++i);
if (i == REG_CNT_)
return varPool_allocInMem(p, nodeId);
Var *res = p->inReg + i;
assert(!res->allocated);
res->allocated = true;
res->temp = (nodeId == -1);
res->nodeId = nodeId;
++p->overall;
return res;
}
Var *varPool_free(varPool *p, Var *v)
{
assert(p != NULL);
assert(v->reg != REG_CNT_ && v->reg != RSP && v->reg != RBP && v->reg != RAX && v->reg != R10);
assert(v->temp);
v->allocated = false;
v->temp = false;
--p->overall;
return NULL;
}
void varPool_dump(varPool *p, FILE *out)
{
assert(p != NULL);
assert(out != NULL);
fprintf(out, "varPool dump:\ninMemCnt = %zu\ninMemCap = %zu\noverall = %zu\n\n", p->inMemCnt, p->inMemCap, p->overall);
for (size_t i = 0; i < REG_CNT_; ++i) {
Var *v = p->inReg + i;
if (v->reg != REG_CNT_ && v->reg != RSP && v->reg != RBP && v->reg != RAX && v->reg != R10) {
fprintf(stderr, "%s (%d)\t| offset = %zu\t| num = %zu\t| temp = %d\t| allocated = %d\t| nodeId = %zu\n", REGISTER_MSG[v->reg], v->reg, v->offset, v->num, v->temp, v->allocated, v->nodeId);
}
}
for (size_t i = 0; i < p->inMemCap; ++i) {
Var *v = p->inMem + i;
if (v->reg != REG_CNT_ && v->reg != RSP && v->reg != RBP && v->reg != RAX && v->reg != R10) {
fprintf(stderr, "%s (%d)\t| offset = %zu\t| num = %zu\t| temp = %d\t| allocated = %d\t| nodeId = %zu\n", REGISTER_MSG[v->reg], v->reg, v->offset, v->num, v->temp, v->allocated, v->nodeId);
}
}
fprintf(stderr, "\n\n\n");
}