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
|
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/deoptimizer/translation-array.h"
#include "src/deoptimizer/translated-state.h"
#include "src/objects/fixed-array-inl.h"
#include "third_party/zlib/google/compression_utils_portable.h"
namespace v8 {
namespace internal {
namespace {
// Constants describing compressed TranslationArray layout. Only relevant if
// --turbo-compress-translation-arrays is enabled.
constexpr int kUncompressedSizeOffset = 0;
constexpr int kUncompressedSizeSize = kInt32Size;
constexpr int kCompressedDataOffset =
kUncompressedSizeOffset + kUncompressedSizeSize;
constexpr int kTranslationArrayElementSize = kInt32Size;
// Encodes the return type of a Wasm function as the integer value of
// wasm::ValueKind, or kNoWasmReturnType if the function returns void.
int EncodeWasmReturnType(base::Optional<wasm::ValueKind> return_type) {
return return_type ? static_cast<int>(return_type.value())
: kNoWasmReturnType;
}
} // namespace
TranslationArrayIterator::TranslationArrayIterator(TranslationArray buffer,
int index)
: buffer_(buffer), index_(index) {
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
const int size = buffer_.get_int(kUncompressedSizeOffset);
uncompressed_contents_.insert(uncompressed_contents_.begin(), size, 0);
uLongf uncompressed_size = size * kTranslationArrayElementSize;
CHECK_EQ(
zlib_internal::UncompressHelper(
zlib_internal::ZRAW,
bit_cast<Bytef*>(uncompressed_contents_.data()), &uncompressed_size,
buffer_.GetDataStartAddress() + kCompressedDataOffset,
buffer_.DataSize()),
Z_OK);
DCHECK(index >= 0 && index < size);
} else {
DCHECK(index >= 0 && index < buffer.length());
}
}
int32_t TranslationArrayIterator::Next() {
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
return uncompressed_contents_[index_++];
} else {
// Run through the bytes until we reach one with a least significant
// bit of zero (marks the end).
uint32_t bits = 0;
for (int i = 0; true; i += 7) {
DCHECK(HasNext());
uint8_t next = buffer_.get(index_++);
bits |= (next >> 1) << i;
if ((next & 1) == 0) break;
}
// The bits encode the sign in the least significant bit.
bool is_negative = (bits & 1) == 1;
int32_t result = bits >> 1;
return is_negative ? -result : result;
}
}
bool TranslationArrayIterator::HasNext() const {
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
return index_ < static_cast<int>(uncompressed_contents_.size());
} else {
return index_ < buffer_.length();
}
}
void TranslationArrayBuilder::Add(int32_t value) {
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
contents_for_compression_.push_back(value);
} else {
// This wouldn't handle kMinInt correctly if it ever encountered it.
DCHECK_NE(value, kMinInt);
// Encode the sign bit in the least significant bit.
bool is_negative = (value < 0);
uint32_t bits = (static_cast<uint32_t>(is_negative ? -value : value) << 1) |
static_cast<uint32_t>(is_negative);
// Encode the individual bytes using the least significant bit of
// each byte to indicate whether or not more bytes follow.
do {
uint32_t next = bits >> 7;
contents_.push_back(((bits << 1) & 0xFF) | (next != 0));
bits = next;
} while (bits != 0);
}
}
Handle<TranslationArray> TranslationArrayBuilder::ToTranslationArray(
Factory* factory) {
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
const int input_size = SizeInBytes();
uLongf compressed_data_size = compressBound(input_size);
ZoneVector<byte> compressed_data(compressed_data_size, zone());
CHECK_EQ(
zlib_internal::CompressHelper(
zlib_internal::ZRAW, compressed_data.data(), &compressed_data_size,
bit_cast<const Bytef*>(contents_for_compression_.data()),
input_size, Z_DEFAULT_COMPRESSION, nullptr, nullptr),
Z_OK);
const int translation_array_size =
static_cast<int>(compressed_data_size) + kUncompressedSizeSize;
Handle<TranslationArray> result =
factory->NewByteArray(translation_array_size, AllocationType::kOld);
result->set_int(kUncompressedSizeOffset, Size());
std::memcpy(result->GetDataStartAddress() + kCompressedDataOffset,
compressed_data.data(), compressed_data_size);
return result;
} else {
Handle<TranslationArray> result =
factory->NewByteArray(SizeInBytes(), AllocationType::kOld);
memcpy(result->GetDataStartAddress(), contents_.data(),
contents_.size() * sizeof(uint8_t));
return result;
}
}
void TranslationArrayBuilder::BeginBuiltinContinuationFrame(
BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
auto opcode = TranslationOpcode::BUILTIN_CONTINUATION_FRAME;
Add(opcode);
Add(bytecode_offset.ToInt());
Add(literal_id);
Add(height);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}
void TranslationArrayBuilder::BeginJSToWasmBuiltinContinuationFrame(
BytecodeOffset bytecode_offset, int literal_id, unsigned height,
base::Optional<wasm::ValueKind> return_type) {
auto opcode = TranslationOpcode::JS_TO_WASM_BUILTIN_CONTINUATION_FRAME;
Add(opcode);
Add(bytecode_offset.ToInt());
Add(literal_id);
Add(height);
Add(EncodeWasmReturnType(return_type));
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 4);
}
void TranslationArrayBuilder::BeginJavaScriptBuiltinContinuationFrame(
BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
auto opcode = TranslationOpcode::JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME;
Add(opcode);
Add(bytecode_offset.ToInt());
Add(literal_id);
Add(height);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}
void TranslationArrayBuilder::BeginJavaScriptBuiltinContinuationWithCatchFrame(
BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
auto opcode =
TranslationOpcode::JAVA_SCRIPT_BUILTIN_CONTINUATION_WITH_CATCH_FRAME;
Add(opcode);
Add(bytecode_offset.ToInt());
Add(literal_id);
Add(height);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}
void TranslationArrayBuilder::BeginConstructStubFrame(
BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
auto opcode = TranslationOpcode::CONSTRUCT_STUB_FRAME;
Add(opcode);
Add(bytecode_offset.ToInt());
Add(literal_id);
Add(height);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}
void TranslationArrayBuilder::BeginArgumentsAdaptorFrame(int literal_id,
unsigned height) {
auto opcode = TranslationOpcode::ARGUMENTS_ADAPTOR_FRAME;
Add(opcode);
Add(literal_id);
Add(height);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 2);
}
void TranslationArrayBuilder::BeginInterpretedFrame(
BytecodeOffset bytecode_offset, int literal_id, unsigned height,
int return_value_offset, int return_value_count) {
auto opcode = TranslationOpcode::INTERPRETED_FRAME;
Add(opcode);
Add(bytecode_offset.ToInt());
Add(literal_id);
Add(height);
Add(return_value_offset);
Add(return_value_count);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 5);
}
void TranslationArrayBuilder::ArgumentsElements(CreateArgumentsType type) {
auto opcode = TranslationOpcode::ARGUMENTS_ELEMENTS;
Add(opcode);
Add(static_cast<uint8_t>(type));
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::ArgumentsLength() {
auto opcode = TranslationOpcode::ARGUMENTS_LENGTH;
Add(opcode);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 0);
}
void TranslationArrayBuilder::BeginCapturedObject(int length) {
auto opcode = TranslationOpcode::CAPTURED_OBJECT;
Add(opcode);
Add(length);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::DuplicateObject(int object_index) {
auto opcode = TranslationOpcode::DUPLICATED_OBJECT;
Add(opcode);
Add(object_index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreRegister(Register reg) {
auto opcode = TranslationOpcode::REGISTER;
Add(opcode);
Add(reg.code());
}
void TranslationArrayBuilder::StoreInt32Register(Register reg) {
auto opcode = TranslationOpcode::INT32_REGISTER;
Add(opcode);
Add(reg.code());
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreInt64Register(Register reg) {
auto opcode = TranslationOpcode::INT64_REGISTER;
Add(opcode);
Add(reg.code());
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreUint32Register(Register reg) {
auto opcode = TranslationOpcode::UINT32_REGISTER;
Add(opcode);
Add(reg.code());
}
void TranslationArrayBuilder::StoreBoolRegister(Register reg) {
auto opcode = TranslationOpcode::BOOL_REGISTER;
Add(opcode);
Add(reg.code());
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreFloatRegister(FloatRegister reg) {
auto opcode = TranslationOpcode::FLOAT_REGISTER;
Add(opcode);
Add(reg.code());
}
void TranslationArrayBuilder::StoreDoubleRegister(DoubleRegister reg) {
auto opcode = TranslationOpcode::DOUBLE_REGISTER;
Add(opcode);
Add(reg.code());
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreStackSlot(int index) {
auto opcode = TranslationOpcode::STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreInt32StackSlot(int index) {
auto opcode = TranslationOpcode::INT32_STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreInt64StackSlot(int index) {
auto opcode = TranslationOpcode::INT64_STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreUint32StackSlot(int index) {
auto opcode = TranslationOpcode::UINT32_STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreBoolStackSlot(int index) {
auto opcode = TranslationOpcode::BOOL_STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreFloatStackSlot(int index) {
auto opcode = TranslationOpcode::FLOAT_STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreDoubleStackSlot(int index) {
auto opcode = TranslationOpcode::DOUBLE_STACK_SLOT;
Add(opcode);
Add(index);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::StoreLiteral(int literal_id) {
auto opcode = TranslationOpcode::LITERAL;
Add(opcode);
Add(literal_id);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}
void TranslationArrayBuilder::AddUpdateFeedback(int vector_literal, int slot) {
auto opcode = TranslationOpcode::UPDATE_FEEDBACK;
Add(opcode);
Add(vector_literal);
Add(slot);
DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 2);
}
void TranslationArrayBuilder::StoreJSFrameFunction() {
StoreStackSlot((StandardFrameConstants::kCallerPCOffset -
StandardFrameConstants::kFunctionOffset) /
kSystemPointerSize);
}
} // namespace internal
} // namespace v8
|