blob: cd67a0a31002709a82b07d040084309126edf402 (
plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#include "compiler/Registers.h"
#include "compiler/RegisterInfo.h"
#include "compiler/CParser.h"
#include "compiler/PCode.h"
#include "compiler/CompilerTools.h"
#include "compiler/objects.h"
int used_virtual_registers[RegClassMax];
int used_nonvolatile_registers[RegClassMax];
int assignable_registers[RegClassMax];
int n_real_registers[RegClassMax];
int n_scratch_registers[RegClassMax];
int scratch_registers[RegClassMax][RegisterMax];
int n_nonvolatile_registers[RegClassMax];
int nonvolatile_registers[RegClassMax][RegisterMax];
UInt8 reg_state[RegClassMax][RegisterMax];
int first_temporary_register[RegClassMax];
int last_temporary_register[RegClassMax];
char *register_class_name[RegClassMax];
char *register_class_format[RegClassMax];
int coloring;
int optimizing;
typedef struct ModifiedRegisters {
struct ModifiedRegisters *next;
Object *obj;
UInt32 mask[RegClassMax];
} ModifiedRegisters;
static ModifiedRegisters *mod_regs_table[128];
void init_registers(void) {
RegClass rclass;
int j;
for (rclass = 0; rclass < RegClassMax; rclass++) {
for (j = 0; j < RegisterMax; j++)
reg_state[rclass][j] = RegState0;
}
for (rclass = 0; rclass < RegClassMax; rclass++)
used_nonvolatile_registers[rclass] = 0;
optimizing = 1;
init_target_registers();
for (rclass = 0; rclass < RegClassMax; rclass++)
used_virtual_registers[rclass] = n_real_registers[rclass];
coloring = 1;
}
void assign_register_to_variable(Object *obj, RegClass rclass) {
VarInfo *vi;
short reg;
vi = Registers_GetVarInfo(obj);
if (optimizing) {
reg = used_virtual_registers[rclass]++;
} else {
reg = obtain_nonvolatile_register(rclass);
retain_register(obj, rclass, reg);
}
vi->flags |= VarInfoFlag2;
vi->rclass = rclass;
vi->reg = reg;
}
void retain_register_for_argument(Object *obj, RegClass rclass, short reg) {
VarInfo *vi = obj->u.var.info;
reg_state[rclass][reg] = RegState1;
vi->flags |= VarInfoFlag2;
vi->rclass = rclass;
vi->reg = reg;
}
int available_registers(RegClass rclass) {
int i;
int count = 0;
for (i = 0; i < n_real_registers[rclass]; i++) {
if (reg_state[rclass][i] == RegState0)
count++;
}
return count;
}
UInt32 volatile_registers(RegClass rclass) {
UInt32 mask = 0;
int i;
int reg;
for (i = 0; i < n_scratch_registers[rclass]; i++) {
reg = scratch_registers[rclass][i];
if (reg_state[rclass][reg] == RegState0)
mask |= 1 << reg;
}
return mask;
}
short obtain_nonvolatile_register(RegClass rclass) {
int best = -1;
while (used_nonvolatile_registers[rclass] < n_nonvolatile_registers[rclass]) {
int tmp = nonvolatile_registers[rclass][used_nonvolatile_registers[rclass]++];
if (reg_state[rclass][tmp] == RegState0) {
best = tmp;
break;
}
}
return best;
}
void open_temp_registers(void) {
RegClass rclass;
for (rclass = 0; rclass < RegClassMax; rclass++) {
int r = used_virtual_registers[rclass];
first_temporary_register[rclass] = last_temporary_register[rclass] = r;
}
}
void check_temp_registers(void) {
RegClass rclass;
if (!optimizing) {
for (rclass = 0; rclass < RegClassMax; rclass++) {
if (used_virtual_registers[rclass] > last_temporary_register[rclass])
last_temporary_register[rclass] = used_virtual_registers[rclass];
if (used_virtual_registers[rclass] > 256)
used_virtual_registers[rclass] = first_temporary_register[rclass];
}
}
}
void close_temp_registers(void) {
RegClass rclass;
for (rclass = 0; rclass < RegClassMax; rclass++) {
if (used_virtual_registers[rclass] < last_temporary_register[rclass])
used_virtual_registers[rclass] = last_temporary_register[rclass];
else
last_temporary_register[rclass] = used_virtual_registers[rclass];
}
}
int count_scratch_registers(void) {
RegClass rclass;
int count;
count = 0;
for (rclass = 0; rclass < RegClassMax; rclass++)
count += n_scratch_registers[rclass];
return count;
}
void init_modified_registers(void) {
int i = 0;
for (i = 0; i < 128; i++)
mod_regs_table[i] = NULL;
}
static void compute_modified_registers(UInt32 *masks) {
RegClass rclass;
PCodeBlock *block;
PCode *pcode;
PCodeArg *op;
int i;
for (rclass = 0; rclass < RegClassMax; rclass++)
masks[rclass] = 0;
for (block = pcbasicblocks; block; block = block->nextBlock) {
for (pcode = block->firstPCode; pcode; pcode = pcode->nextPCode) {
op = pcode->args;
i = pcode->argCount;
while (i--) {
if (op->kind == PCOp_REGISTER && (op->data.reg.effect & EffectWrite))
masks[op->arg] |= 1 << op->data.reg.reg;
op++;
}
}
}
}
void note_modified_registers(Object *obj) {
ModifiedRegisters *mr;
mr = galloc(sizeof(ModifiedRegisters));
mr->obj = obj;
compute_modified_registers(mr->mask);
mr->next = mod_regs_table[obj->name->hashval & 127];
mod_regs_table[obj->name->hashval & 127] = mr;
}
void find_modified_registers(Object *obj, UInt32 *masks) {
RegClass rclass;
ModifiedRegisters *scan;
for (rclass = 0; rclass < RegClassMax; rclass++)
masks[rclass] = 0xFFFFFFFF;
if (CParser_HasInternalLinkage(obj)) {
for (scan = mod_regs_table[obj->name->hashval & 127]; scan; scan = scan->next) {
if (scan->obj == obj)
break;
}
if (scan) {
for (rclass = 0; rclass < RegClassMax; rclass++)
masks[rclass] = scan->mask[rclass];
}
}
}
|