forked from aligrudi/neatcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reg.c
152 lines (137 loc) · 2.92 KB
/
reg.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* neatcc register allocation */
#include <stdio.h>
#include <string.h>
#include "gen.h"
#include "ncc.h"
#include "reg.h"
static int l_sz[NLOCALS]; /* size of locals */
static int l_nr[NLOCALS]; /* # of reads */
static int l_nw[NLOCALS]; /* # of writes */
static int l_na[NLOCALS]; /* # of address accesses */
static int l_reg[NLOCALS]; /* register mapped to locals */
static int l_n; /* # of locals */
static int f_argc; /* number of arguments */
static int f_varg; /* function has variable argument list */
static int f_lregs; /* mask of R_TMPS allocated to locals */
static int f_sargs; /* mask of R_ARGS to be saved */
void r_func(int nargs, int varg)
{
int i;
f_varg = varg;
f_argc = nargs;
f_lregs = 0;
l_n = 0;
for (i = 0; i < f_argc; i++)
r_mk(LONGSZ);
f_sargs = f_varg ? R_ARGS : 0;
for (i = 0; i < f_argc && i < N_ARGS; i++)
f_sargs |= 1 << argregs[i];
}
void r_mk(int sz)
{
if (!f_lregs) {
l_sz[l_n] = sz;
l_nr[l_n] = 0;
l_nw[l_n] = 0;
l_na[l_n] = 0;
l_reg[l_n] = -1;
l_n++;
}
}
void r_rm(int id)
{
}
void r_read(int id)
{
l_nr[id]++;
}
void r_write(int id)
{
l_nw[id]++;
}
void r_addr(int id)
{
l_na[id]++;
}
void r_label(int l)
{
}
void r_jmp(int l)
{
}
int r_regmap(int id)
{
return l_reg[id];
}
int r_lregs(void)
{
return f_lregs;
}
int r_sargs(void)
{
return f_sargs;
}
/* sort locals for register allocation based on the number of accesses */
static int *sortedlocals(void)
{
static int ord[NLOCALS];
int i, j;
for (i = 0; i < l_n; i++) {
for (j = i - 1; j >= 0; j--) {
if (l_nr[i] + l_nw[i] <= l_nw[ord[j]] + l_nr[ord[j]])
break;
ord[j + 1] = ord[j];
}
ord[j + 1] = i;
}
return ord;
}
int r_alloc(int leaf, int used)
{
int nlocregs = 0;
int *ord = sortedlocals();
int idx = 0;
int i;
f_lregs = 0;
f_sargs = f_varg ? R_ARGS : 0;
/* except unused arguments, save all arguments on the stack */
for (i = 0; i < f_argc && i < N_ARGS; i++)
if (f_varg || l_nr[i] + l_nw[i] + l_na[i])
f_sargs |= 1 << argregs[i];
/* letting arguments stay in their registers for leaf functions */
if (!f_varg && leaf) {
for (i = 0; i < f_argc && i < N_ARGS; i++) {
if (l_sz[i] > LONGSZ || (1 << argregs[i]) & used)
continue;
if (l_nr[i] + l_nw[i] && !l_na[i]) {
l_reg[i] = argregs[i];
f_sargs &= ~(1 << argregs[i]);
f_lregs |= (1 << argregs[i]);
nlocregs++;
}
}
}
/* try finding a register for each local */
for (i = 0; i < l_n; i++) {
int l = ord[i];
int nmask = (leaf ? 0 : ~R_SAVED) | used | f_lregs;
if (l_reg[l] >= 0 || (f_varg && l < f_argc))
continue;
/* find a free register */
while (idx < N_TMPS && ((1 << tmpregs[idx]) & nmask))
idx++;
if (idx >= N_TMPS)
break;
if (l_sz[l] > LONGSZ || l_na[l])
continue;
if (l_nr[l] + l_nw[l] > (leaf ? 0 : 1)) {
l_reg[l] = tmpregs[idx];
f_lregs |= 1 << tmpregs[idx];
if (l < N_ARGS && l < f_argc)
f_sargs &= ~(1 << argregs[l]);
nlocregs++;
idx++;
}
}
return nlocregs;
}