-
Notifications
You must be signed in to change notification settings - Fork 392
/
TranslateTable.cpp
367 lines (333 loc) · 11.6 KB
/
TranslateTable.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "optimizer/TranslateTable.hpp"
#include <stdint.h>
#include <string.h>
#include "compile/Compilation.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "env/TRMemory.hpp"
#include "il/DataTypes.hpp"
#include "infra/Assert.hpp"
namespace TR { class SymbolReference; }
uint8_t* TR_TranslateTable::data()
{
return _table->data;
}
TR_TranslateTable::TR_TranslateTableData* TR_TranslateTable::table()
{
return _table;
}
void TR_TranslateTable::setTable(TR_TranslateTableData* match)
{
_table = match;
}
void TR_TranslateTable::dumpTable()
{
int count = tableSize(table()->inSize, table()->outSize);
dumpOptDetails(comp(), "\n\nTranslation table at address %p, size %d\n", data(), count);
dumpOptDetails(comp(), "\n Range %d to %d and %d to %d default value %d\n", table()->startA, table()->endA, table()->startB, table()->endB, table()->defaultValue);
if (table()->outSize == 16)
{
for (int i=0; i<count*2; i+=2)
{
if (i % 16 == 0) dumpOptDetails(comp(), "\n");
dumpOptDetails(comp(), "%02x%02x ", data()[i], data()[i+1]);
}
}
else
{
for (int i=0; i<count; i+=2)
{
if (i % 16 == 0) dumpOptDetails(comp(), "\n");
dumpOptDetails(comp(), "%2x %2x ", data()[i], data()[i+1]);
}
}
}
TR::SymbolReference * TR_TranslateTable::createSymbolRef()
{
if (!_symRef)
{
_symRef = _comp->getSymRefTab()->createKnownStaticDataSymbolRef(data(), TR::Address);
}
return _symRef;
}
int TR_TranslateTable::tableSize(uint8_t inputSize, uint8_t outputSize)
{
if (inputSize == 8 && outputSize == 8)
{
return 256;
}
else if (inputSize == 16 && outputSize == 8)
{
return 65536;
}
else if (inputSize == 8 && outputSize == 16)
{
// return the number of elements instead
// of bytes to be allocated
return 256;
///return 512;
}
else if (inputSize == 16 && outputSize == 16)
{
// return the number of elements instead
// of bytes to be allocated
return 65536;
///return 131072;
}
else
{
TR_ASSERT(0, "Invalid input and output sizes specified\n");
return 0; // keep compiler happy
}
}
TR_TranslateTable::TR_TranslateTableData* TR_TranslateTable::createTable(uint32_t startA, uint32_t endA, uint8_t inputSize, uint8_t outputSize, uint16_t defaultValue)
{
return createTable(startA, endA, 0, 0, inputSize, outputSize, defaultValue);
}
TR_TranslateTable::TR_TranslateTableData* TR_TranslateTable::createTable(uint16_t startA, uint16_t endA, uint8_t inputSize, uint8_t outputSize, uint16_t defaultValue)
{
return createTable((uint32_t)startA, (uint32_t)endA, 0, 0, inputSize, outputSize, defaultValue);
}
TR_TranslateTable::TR_TranslateTableData* TR_TranslateTable::createTable(uint16_t startA, uint16_t endA, uint16_t startB, uint16_t endB, uint8_t inputSize, uint8_t outputSize, uint16_t defaultValue)
{
return createTable((uint32_t)startA, (uint32_t)endA, (uint32_t)startB, (uint32_t)endB, inputSize, outputSize, defaultValue);
}
TR_TranslateTable::TR_TranslateTableData* TR_TranslateTable::createTable(uint32_t startA, uint32_t endA, uint32_t startB, uint32_t endB, uint8_t inputSize, uint8_t outputSize, uint16_t defaultValue)
{
TR_TranslateTableData * match = matchTable(startA, endA, startB, endB, inputSize, outputSize, defaultValue);
if (match)
{
_table = match;
return _table;
}
int totalSize = tableSize(inputSize, outputSize);
int allocSize = totalSize;
if (allocSize > 4096)
{
allocSize += 4096;
}
else
{
allocSize += 8;
}
// msf - methods that update _head will need a lock when we move to a true async compilation (re-entrant) model
// double the allocation size
// if required
if (outputSize == 16)
allocSize += allocSize;
_table = new (PERSISTENT_NEW) TR_TranslateTable::TR_TranslateTableData();
uint8_t* data = (uint8_t*)jitPersistentAlloc(allocSize);
if (allocSize > 4096)
{
data = (uint8_t*)(((((uint64_t) (data)) + 4096) >> 12) << 12);
}
else
{
data = (uint8_t*)(((((uint64_t) (data)) + 8) >> 3) << 3);
}
if (startA > startB)
{
uint16_t tempStart = startA;
uint16_t tempEnd = endA;
startA = startB;
endA = endB;
startB = tempStart;
endB = tempEnd;
}
int i;
if (outputSize == 16)
{
uint16_t* cTable = (uint16_t*) data;
for (i=0; i<startA; ++i)
{
cTable[i] = defaultValue;
}
for (i=startA; i<endA; ++i)
{
cTable[i] = i;
}
if (startB != endB)
{
for (i=endA; i<startB; ++i)
{
cTable[i] = defaultValue;
}
for (i=startB; i<endB; ++i)
{
cTable[i] = i;
}
for (i=endB; i<totalSize; ++i)
{
cTable[i] = defaultValue;
}
}
else
{
for (i=endA; i<totalSize; ++i)
{
cTable[i] = defaultValue;
}
}
}
else // outputSize is 8
{
uint8_t* bTable = (uint8_t*) data;
uint8_t bDefaultValue = (uint8_t) defaultValue;
for (i=0; i<startA; ++i)
{
bTable[i] = bDefaultValue;
}
for (i=startA; i<endA; ++i)
{
bTable[i] = i;
}
if (startB != endB)
{
for (i=endA; i<startB; ++i)
{
bTable[i] = bDefaultValue;
}
for (i=startB; i<endB; ++i)
{
bTable[i] = i;
}
for (i=endB; i<totalSize; ++i)
{
bTable[i] = bDefaultValue;
}
}
else
{
for (i=endA; i<totalSize; ++i)
{
bTable[i] = static_cast<uint8_t>(defaultValue);
}
}
}
_table->data = data;
_table->inSize = inputSize;
_table->outSize = outputSize;
_table->startA = startA;
_table->endA = endA;
_table->startB = startB;
_table->endB = endB;
_table->defaultValue = defaultValue;
updateTable();
return _table;
}
TR_TranslateTable::TR_TranslateTableData* TR_TranslateTable::_head = 0;
void TR_TranslateTable::updateTable()
{
_table->next = _head;
_head = _table;
}
// can do something smarter if number of tables gets big - expected to be quite small (1 or 2 tables in practice)
TR_TranslateTable::TR_TranslateTableData * TR_TranslateTable::matchTable(uint32_t startA, uint32_t endA, uint32_t startB, uint32_t endB, uint8_t inputSize, uint8_t outputSize, uint16_t defaultValue)
{
if (startA == 0 && endA == tableSize(inputSize, outputSize))
{
// this is a set - it can not be shared
return NULL;
}
TR_TranslateTable::TR_TranslateTableData * curr = _head;
while (curr)
{
if (curr->startA == startA && curr->endA == endA &&
curr->startB == startB && curr->endB == endB &&
curr->inSize == inputSize && curr->outSize == outputSize &&
curr->defaultValue == defaultValue)
{
return curr;
}
curr = curr->next;
}
return curr;
}
// searching SetTranslateTable
TR_TranslateTable::TR_TranslateTableData * TR_TranslateTable::matchTable(uint8_t inputSize, uint8_t outputSize, void *array)
{
int tableSizeParm = tableSize(inputSize, outputSize);
int tableByteSize = tableSizeParm * (outputSize/8);
TR_TranslateTable::TR_TranslateTableData * curr = _head;
while (curr)
{
if (curr->inSize == inputSize && curr->outSize == outputSize)
{
if (memcmp(array, curr->data, tableByteSize) == 0)
{
return curr;
}
}
curr = curr->next;
}
return curr;
}
TR_RangeTranslateTable::TR_RangeTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, uint16_t start, uint16_t end, uint16_t defaultValue) : TR_TranslateTable(comp)
{
createTable(start, end, inputSize, outputSize, defaultValue);
}
TR_RangeTranslateTable::TR_RangeTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, uint16_t startA, uint16_t endA, uint16_t startB, uint16_t endB, uint16_t defaultValue) : TR_TranslateTable(comp)
{
createTable(startA, endA, startB, endB, inputSize, outputSize, defaultValue);
}
TR_RangeTranslateTable::TR_RangeTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, uint32_t start, uint32_t end, uint16_t defaultValue) : TR_TranslateTable(comp)
{
createTable(start, end, inputSize, outputSize, defaultValue);
}
TR_RangeTranslateTable::TR_RangeTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, uint32_t startA, uint32_t endA, uint32_t startB, uint32_t endB, uint16_t defaultValue) : TR_TranslateTable(comp)
{
createTable(startA, endA, startB, endB, inputSize, outputSize, defaultValue);
}
TR_SetTranslateTable::TR_SetTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, uint8_t set[]) : TR_TranslateTable(comp) // input and output size in bits
{
createTable((uint16_t)0, tableSize(inputSize, outputSize), inputSize, outputSize, (uint16_t)0);
for (int i=0; set[i] > 0; ++i)
{
data()[i] = set[i];
}
}
TR_SetTranslateTable::TR_SetTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, uint16_t set[]) : TR_TranslateTable(comp) // input and output size in bits
{
createTable((uint32_t)0, tableSize(inputSize, outputSize), inputSize, outputSize, (uint32_t)0);
for (int i=0; set[i] > 0; ++i)
{
((uint16_t*)data())[i] = set[i];
}
}
TR_SetTranslateTable::TR_SetTranslateTable(TR::Compilation* comp, uint8_t inputSize, uint8_t outputSize, void *set, int copyElemSize) : TR_TranslateTable(comp) // input and output size in bits
{
int tableSizeParm = tableSize(inputSize, outputSize);
int tableByteSize = tableSizeParm * (outputSize/8);
int copyByteSize = copyElemSize * (outputSize/8);
TR_ASSERT(copyByteSize <= tableByteSize, "error!");
if (tableByteSize == copyByteSize)
{
TR_TranslateTableData * match = TR_TranslateTable::matchTable(inputSize, outputSize, set);
if (match)
{
setTable(match);
return;
}
}
createTable((uint32_t)0, tableSizeParm, inputSize, outputSize, (uint32_t)0);
memcpy(data(), set, copyByteSize);
}