Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/interpreter/bytecode-array-writer.h"
6 :
7 : #include "src/api.h"
8 : #include "src/interpreter/bytecode-jump-table.h"
9 : #include "src/interpreter/bytecode-label.h"
10 : #include "src/interpreter/bytecode-node.h"
11 : #include "src/interpreter/bytecode-register.h"
12 : #include "src/interpreter/bytecode-source-info.h"
13 : #include "src/interpreter/constant-array-builder.h"
14 : #include "src/log.h"
15 : #include "src/objects-inl.h"
16 :
17 : namespace v8 {
18 : namespace internal {
19 : namespace interpreter {
20 :
21 : STATIC_CONST_MEMBER_DEFINITION const size_t
22 : BytecodeArrayWriter::kMaxSizeOfPackedBytecode;
23 :
24 2154794 : BytecodeArrayWriter::BytecodeArrayWriter(
25 : Zone* zone, ConstantArrayBuilder* constant_array_builder,
26 : SourcePositionTableBuilder::RecordingMode source_position_mode)
27 : : bytecodes_(zone),
28 : unbound_jumps_(0),
29 : source_position_table_builder_(zone, source_position_mode),
30 : constant_array_builder_(constant_array_builder),
31 : last_bytecode_(Bytecode::kIllegal),
32 : last_bytecode_offset_(0),
33 : last_bytecode_had_source_info_(false),
34 : elide_noneffectful_bytecodes_(FLAG_ignition_elide_noneffectful_bytecodes),
35 4309588 : exit_seen_in_block_(false) {
36 2154795 : bytecodes_.reserve(512); // Derived via experimentation.
37 2154798 : }
38 :
39 2153876 : Handle<BytecodeArray> BytecodeArrayWriter::ToBytecodeArray(
40 2153876 : Isolate* isolate, int register_count, int parameter_count,
41 2153876 : Handle<FixedArray> handler_table) {
42 : DCHECK_EQ(0, unbound_jumps_);
43 :
44 4307752 : int bytecode_size = static_cast<int>(bytecodes()->size());
45 2153876 : int frame_size = register_count * kPointerSize;
46 : Handle<FixedArray> constant_pool =
47 2153876 : constant_array_builder()->ToFixedArray(isolate);
48 : Handle<ByteArray> source_position_table =
49 2153876 : source_position_table_builder()->ToSourcePositionTable(isolate);
50 : Handle<BytecodeArray> bytecode_array = isolate->factory()->NewBytecodeArray(
51 : bytecode_size, &bytecodes()->front(), frame_size, parameter_count,
52 2153876 : constant_pool);
53 2153875 : bytecode_array->set_handler_table(*handler_table);
54 2153874 : bytecode_array->set_source_position_table(*source_position_table);
55 2154141 : LOG_CODE_EVENT(isolate, CodeLinePosInfoRecordEvent(
56 : *Handle<AbstractCode>::cast(bytecode_array),
57 : *source_position_table));
58 2153876 : return bytecode_array;
59 : }
60 :
61 203590151 : void BytecodeArrayWriter::Write(BytecodeNode* node) {
62 : DCHECK(!Bytecodes::IsJump(node->bytecode()));
63 :
64 136028341 : if (exit_seen_in_block_) return; // Don't emit dead code.
65 : UpdateExitSeenInBlock(node->bytecode());
66 203364012 : MaybeElideLastBytecode(node->bytecode(), node->source_info().is_valid());
67 :
68 67788113 : UpdateSourcePositionTable(node);
69 67788109 : EmitBytecode(node);
70 : }
71 :
72 7322892 : void BytecodeArrayWriter::WriteJump(BytecodeNode* node, BytecodeLabel* label) {
73 : DCHECK(Bytecodes::IsJump(node->bytecode()));
74 :
75 : // TODO(rmcilroy): For forward jumps we could also mark the label as dead,
76 : // thereby avoiding emitting dead code when we bind the label.
77 4899256 : if (exit_seen_in_block_) return; // Don't emit dead code.
78 : UpdateExitSeenInBlock(node->bytecode());
79 7309896 : MaybeElideLastBytecode(node->bytecode(), node->source_info().is_valid());
80 :
81 2436633 : UpdateSourcePositionTable(node);
82 2436633 : EmitJump(node, label);
83 : }
84 :
85 85316 : void BytecodeArrayWriter::WriteSwitch(BytecodeNode* node,
86 : BytecodeJumpTable* jump_table) {
87 : DCHECK(Bytecodes::IsSwitch(node->bytecode()));
88 :
89 : // TODO(rmcilroy): For jump tables we could also mark the table as dead,
90 : // thereby avoiding emitting dead code when we bind the entries.
91 57388 : if (exit_seen_in_block_) return; // Don't emit dead code.
92 : UpdateExitSeenInBlock(node->bytecode());
93 84933 : MaybeElideLastBytecode(node->bytecode(), node->source_info().is_valid());
94 :
95 28311 : UpdateSourcePositionTable(node);
96 : EmitSwitch(node, jump_table);
97 : }
98 :
99 3003383 : void BytecodeArrayWriter::BindLabel(BytecodeLabel* label) {
100 3003383 : size_t current_offset = bytecodes()->size();
101 3003383 : if (label->is_forward_target()) {
102 : // An earlier jump instruction refers to this label. Update it's location.
103 2215206 : PatchJump(current_offset, label->offset());
104 : // Now treat as if the label will only be back referred to.
105 : }
106 : label->bind_to(current_offset);
107 : InvalidateLastBytecode();
108 3003383 : exit_seen_in_block_ = false; // Starting a new basic block.
109 3003383 : }
110 :
111 0 : void BytecodeArrayWriter::BindLabel(const BytecodeLabel& target,
112 : BytecodeLabel* label) {
113 : DCHECK(!label->is_bound());
114 : DCHECK(target.is_bound());
115 0 : if (label->is_forward_target()) {
116 : // An earlier jump instruction refers to this label. Update it's location.
117 0 : PatchJump(target.offset(), label->offset());
118 : // Now treat as if the label will only be back referred to.
119 : }
120 : label->bind_to(target.offset());
121 : InvalidateLastBytecode();
122 : // exit_seen_in_block_ was reset when target was bound, so shouldn't be
123 : // changed here.
124 0 : }
125 :
126 188499 : void BytecodeArrayWriter::BindJumpTableEntry(BytecodeJumpTable* jump_table,
127 62833 : int case_value) {
128 : DCHECK(!jump_table->is_bound(case_value));
129 :
130 62833 : size_t current_offset = bytecodes()->size();
131 62833 : size_t relative_jump = current_offset - jump_table->switch_bytecode_offset();
132 :
133 : constant_array_builder()->SetJumpTableSmi(
134 : jump_table->ConstantPoolEntryFor(case_value),
135 125666 : Smi::FromInt(static_cast<int>(relative_jump)));
136 : jump_table->mark_bound(case_value);
137 :
138 : InvalidateLastBytecode();
139 62833 : exit_seen_in_block_ = false; // Starting a new basic block.
140 62833 : }
141 :
142 70253036 : void BytecodeArrayWriter::UpdateSourcePositionTable(
143 : const BytecodeNode* const node) {
144 140506072 : int bytecode_offset = static_cast<int>(bytecodes()->size());
145 97747355 : const BytecodeSourceInfo& source_info = node->source_info();
146 70253036 : if (source_info.is_valid()) {
147 : source_position_table_builder()->AddPosition(
148 : bytecode_offset, SourcePosition(source_info.source_position()),
149 54988638 : source_info.is_statement());
150 : }
151 70253035 : }
152 :
153 0 : void BytecodeArrayWriter::UpdateExitSeenInBlock(Bytecode bytecode) {
154 : switch (bytecode) {
155 : case Bytecode::kReturn:
156 : case Bytecode::kThrow:
157 : case Bytecode::kReThrow:
158 : case Bytecode::kAbort:
159 : case Bytecode::kJump:
160 : case Bytecode::kJumpConstant:
161 3301459 : exit_seen_in_block_ = true;
162 0 : break;
163 : default:
164 : break;
165 : }
166 0 : }
167 :
168 70252936 : void BytecodeArrayWriter::MaybeElideLastBytecode(Bytecode next_bytecode,
169 : bool has_source_info) {
170 140505942 : if (!elide_noneffectful_bytecodes_) return;
171 :
172 : // If the last bytecode loaded the accumulator without any external effect,
173 : // and the next bytecode clobbers this load without reading the accumulator,
174 : // then the previous bytecode can be elided as it has no effect.
175 156630065 : if (Bytecodes::IsAccumulatorLoadWithoutEffects(last_bytecode_) &&
176 70649442 : Bytecodes::GetAccumulatorUse(next_bytecode) == AccumulatorUse::kWrite &&
177 398130 : (!last_bytecode_had_source_info_ || !has_source_info)) {
178 : DCHECK_GT(bytecodes()->size(), last_bytecode_offset_);
179 395073 : bytecodes()->resize(last_bytecode_offset_);
180 : // If the last bytecode had source info we will transfer the source info
181 : // to this bytecode.
182 395073 : has_source_info |= last_bytecode_had_source_info_;
183 : }
184 70252975 : last_bytecode_ = next_bytecode;
185 70252975 : last_bytecode_had_source_info_ = has_source_info;
186 140505950 : last_bytecode_offset_ = bytecodes()->size();
187 : }
188 :
189 0 : void BytecodeArrayWriter::InvalidateLastBytecode() {
190 3066216 : last_bytecode_ = Bytecode::kIllegal;
191 0 : }
192 :
193 140506027 : void BytecodeArrayWriter::EmitBytecode(const BytecodeNode* const node) {
194 : DCHECK_NE(node->bytecode(), Bytecode::kIllegal);
195 :
196 : Bytecode bytecode = node->bytecode();
197 : OperandScale operand_scale = node->operand_scale();
198 :
199 70253013 : if (operand_scale != OperandScale::kSingle) {
200 11414718 : Bytecode prefix = Bytecodes::OperandScaleToPrefixBytecode(operand_scale);
201 22829436 : bytecodes()->push_back(Bytecodes::ToByte(prefix));
202 : }
203 140506027 : bytecodes()->push_back(Bytecodes::ToByte(bytecode));
204 :
205 70253014 : const uint32_t* const operands = node->operands();
206 : const int operand_count = node->operand_count();
207 : const OperandSize* operand_sizes =
208 : Bytecodes::GetOperandSizes(bytecode, operand_scale);
209 173007811 : for (int i = 0; i < operand_count; ++i) {
210 102754850 : switch (operand_sizes[i]) {
211 : case OperandSize::kNone:
212 0 : UNREACHABLE();
213 : break;
214 : case OperandSize::kByte:
215 166307133 : bytecodes()->push_back(static_cast<uint8_t>(operands[i]));
216 83153540 : break;
217 : case OperandSize::kShort: {
218 19297712 : uint16_t operand = static_cast<uint16_t>(operands[i]);
219 : const uint8_t* raw_operand = reinterpret_cast<const uint8_t*>(&operand);
220 19297712 : bytecodes()->push_back(raw_operand[0]);
221 19297712 : bytecodes()->push_back(raw_operand[1]);
222 : break;
223 : }
224 : case OperandSize::kQuad: {
225 : const uint8_t* raw_operand =
226 303607 : reinterpret_cast<const uint8_t*>(&operands[i]);
227 303607 : bytecodes()->push_back(raw_operand[0]);
228 303607 : bytecodes()->push_back(raw_operand[1]);
229 303607 : bytecodes()->push_back(raw_operand[2]);
230 303607 : bytecodes()->push_back(raw_operand[3]);
231 303607 : break;
232 : }
233 : }
234 : }
235 70252961 : }
236 :
237 : // static
238 32415 : Bytecode GetJumpWithConstantOperand(Bytecode jump_bytecode) {
239 32415 : switch (jump_bytecode) {
240 : case Bytecode::kJump:
241 : return Bytecode::kJumpConstant;
242 : case Bytecode::kJumpIfTrue:
243 10053 : return Bytecode::kJumpIfTrueConstant;
244 : case Bytecode::kJumpIfFalse:
245 5602 : return Bytecode::kJumpIfFalseConstant;
246 : case Bytecode::kJumpIfToBooleanTrue:
247 3164 : return Bytecode::kJumpIfToBooleanTrueConstant;
248 : case Bytecode::kJumpIfToBooleanFalse:
249 849 : return Bytecode::kJumpIfToBooleanFalseConstant;
250 : case Bytecode::kJumpIfNull:
251 224 : return Bytecode::kJumpIfNullConstant;
252 : case Bytecode::kJumpIfNotNull:
253 1 : return Bytecode::kJumpIfNotNullConstant;
254 : case Bytecode::kJumpIfUndefined:
255 509 : return Bytecode::kJumpIfUndefinedConstant;
256 : case Bytecode::kJumpIfNotUndefined:
257 33 : return Bytecode::kJumpIfNotUndefinedConstant;
258 : case Bytecode::kJumpIfJSReceiver:
259 1 : return Bytecode::kJumpIfJSReceiverConstant;
260 : default:
261 0 : UNREACHABLE();
262 : }
263 : }
264 :
265 2148411 : void BytecodeArrayWriter::PatchJumpWith8BitOperand(size_t jump_location,
266 2148411 : int delta) {
267 2148411 : Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location));
268 : DCHECK(Bytecodes::IsForwardJump(jump_bytecode));
269 : DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode));
270 : DCHECK_EQ(Bytecodes::GetOperandType(jump_bytecode, 0), OperandType::kUImm);
271 : DCHECK_GT(delta, 0);
272 2148411 : size_t operand_location = jump_location + 1;
273 : DCHECK_EQ(bytecodes()->at(operand_location), k8BitJumpPlaceholder);
274 4296822 : if (Bytecodes::ScaleForUnsignedOperand(delta) == OperandScale::kSingle) {
275 : // The jump fits within the range of an UImm8 operand, so cancel
276 : // the reservation and jump directly.
277 2116002 : constant_array_builder()->DiscardReservedEntry(OperandSize::kByte);
278 2116002 : bytecodes()->at(operand_location) = static_cast<uint8_t>(delta);
279 : } else {
280 : // The jump does not fit within the range of an UImm8 operand, so
281 : // commit reservation putting the offset into the constant pool,
282 : // and update the jump instruction and operand.
283 : size_t entry = constant_array_builder()->CommitReservedEntry(
284 32409 : OperandSize::kByte, Smi::FromInt(delta));
285 : DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(static_cast<uint32_t>(entry)),
286 : OperandSize::kByte);
287 32409 : jump_bytecode = GetJumpWithConstantOperand(jump_bytecode);
288 32409 : bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode);
289 32409 : bytecodes()->at(operand_location) = static_cast<uint8_t>(entry);
290 : }
291 2148411 : }
292 :
293 66789 : void BytecodeArrayWriter::PatchJumpWith16BitOperand(size_t jump_location,
294 66789 : int delta) {
295 66789 : Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location));
296 : DCHECK(Bytecodes::IsForwardJump(jump_bytecode));
297 : DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode));
298 : DCHECK_EQ(Bytecodes::GetOperandType(jump_bytecode, 0), OperandType::kUImm);
299 : DCHECK_GT(delta, 0);
300 66789 : size_t operand_location = jump_location + 1;
301 : uint8_t operand_bytes[2];
302 133578 : if (Bytecodes::ScaleForUnsignedOperand(delta) <= OperandScale::kDouble) {
303 : // The jump fits within the range of an Imm16 operand, so cancel
304 : // the reservation and jump directly.
305 66783 : constant_array_builder()->DiscardReservedEntry(OperandSize::kShort);
306 66783 : WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(delta));
307 : } else {
308 : // The jump does not fit within the range of an Imm16 operand, so
309 : // commit reservation putting the offset into the constant pool,
310 : // and update the jump instruction and operand.
311 : size_t entry = constant_array_builder()->CommitReservedEntry(
312 6 : OperandSize::kShort, Smi::FromInt(delta));
313 6 : jump_bytecode = GetJumpWithConstantOperand(jump_bytecode);
314 6 : bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode);
315 6 : WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(entry));
316 : }
317 : DCHECK(bytecodes()->at(operand_location) == k8BitJumpPlaceholder &&
318 : bytecodes()->at(operand_location + 1) == k8BitJumpPlaceholder);
319 133578 : bytecodes()->at(operand_location++) = operand_bytes[0];
320 66789 : bytecodes()->at(operand_location) = operand_bytes[1];
321 66789 : }
322 :
323 6 : void BytecodeArrayWriter::PatchJumpWith32BitOperand(size_t jump_location,
324 6 : int delta) {
325 : DCHECK(Bytecodes::IsJumpImmediate(
326 : Bytecodes::FromByte(bytecodes()->at(jump_location))));
327 6 : constant_array_builder()->DiscardReservedEntry(OperandSize::kQuad);
328 : uint8_t operand_bytes[4];
329 6 : WriteUnalignedUInt32(operand_bytes, static_cast<uint32_t>(delta));
330 6 : size_t operand_location = jump_location + 1;
331 : DCHECK(bytecodes()->at(operand_location) == k8BitJumpPlaceholder &&
332 : bytecodes()->at(operand_location + 1) == k8BitJumpPlaceholder &&
333 : bytecodes()->at(operand_location + 2) == k8BitJumpPlaceholder &&
334 : bytecodes()->at(operand_location + 3) == k8BitJumpPlaceholder);
335 12 : bytecodes()->at(operand_location++) = operand_bytes[0];
336 12 : bytecodes()->at(operand_location++) = operand_bytes[1];
337 12 : bytecodes()->at(operand_location++) = operand_bytes[2];
338 6 : bytecodes()->at(operand_location) = operand_bytes[3];
339 6 : }
340 :
341 2215206 : void BytecodeArrayWriter::PatchJump(size_t jump_target, size_t jump_location) {
342 2215206 : Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location));
343 2215206 : int delta = static_cast<int>(jump_target - jump_location);
344 : int prefix_offset = 0;
345 : OperandScale operand_scale = OperandScale::kSingle;
346 2215206 : if (Bytecodes::IsPrefixScalingBytecode(jump_bytecode)) {
347 : // If a prefix scaling bytecode is emitted the target offset is one
348 : // less than the case of no prefix scaling bytecode.
349 66795 : delta -= 1;
350 : prefix_offset = 1;
351 66795 : operand_scale = Bytecodes::PrefixBytecodeToOperandScale(jump_bytecode);
352 : jump_bytecode =
353 66795 : Bytecodes::FromByte(bytecodes()->at(jump_location + prefix_offset));
354 : }
355 :
356 : DCHECK(Bytecodes::IsJump(jump_bytecode));
357 2215206 : switch (operand_scale) {
358 : case OperandScale::kSingle:
359 2148411 : PatchJumpWith8BitOperand(jump_location, delta);
360 2148411 : break;
361 : case OperandScale::kDouble:
362 66789 : PatchJumpWith16BitOperand(jump_location + prefix_offset, delta);
363 66789 : break;
364 : case OperandScale::kQuadruple:
365 6 : PatchJumpWith32BitOperand(jump_location + prefix_offset, delta);
366 6 : break;
367 : default:
368 0 : UNREACHABLE();
369 : }
370 2215206 : unbound_jumps_--;
371 2215206 : }
372 :
373 7309899 : void BytecodeArrayWriter::EmitJump(BytecodeNode* node, BytecodeLabel* label) {
374 : DCHECK(Bytecodes::IsJump(node->bytecode()));
375 : DCHECK_EQ(0u, node->operand(0));
376 :
377 2436633 : size_t current_offset = bytecodes()->size();
378 :
379 2436633 : if (label->is_bound()) {
380 221428 : CHECK_GE(current_offset, label->offset());
381 221428 : CHECK_LE(current_offset, static_cast<size_t>(kMaxUInt32));
382 : // Label has been bound already so this is a backwards jump.
383 221428 : uint32_t delta = static_cast<uint32_t>(current_offset - label->offset());
384 : OperandScale operand_scale = Bytecodes::ScaleForUnsignedOperand(delta);
385 221428 : if (operand_scale > OperandScale::kSingle) {
386 : // Adjust for scaling byte prefix for wide jump offset.
387 9287 : delta += 1;
388 : }
389 : DCHECK_EQ(Bytecode::kJumpLoop, node->bytecode());
390 221428 : node->update_operand0(delta);
391 : } else {
392 : // The label has not yet been bound so this is a forward reference
393 : // that will be patched when the label is bound. We create a
394 : // reservation in the constant pool so the jump can be patched
395 : // when the label is bound. The reservation means the maximum size
396 : // of the operand for the constant is known and the jump can
397 : // be emitted into the bytecode stream with space for the operand.
398 2215205 : unbound_jumps_++;
399 : label->set_referrer(current_offset);
400 : OperandSize reserved_operand_size =
401 2215205 : constant_array_builder()->CreateReservedEntry();
402 : DCHECK_NE(Bytecode::kJumpLoop, node->bytecode());
403 2215205 : switch (reserved_operand_size) {
404 : case OperandSize::kNone:
405 0 : UNREACHABLE();
406 : break;
407 : case OperandSize::kByte:
408 2148410 : node->update_operand0(k8BitJumpPlaceholder);
409 2148410 : break;
410 : case OperandSize::kShort:
411 66789 : node->update_operand0(k16BitJumpPlaceholder);
412 66789 : break;
413 : case OperandSize::kQuad:
414 6 : node->update_operand0(k32BitJumpPlaceholder);
415 6 : break;
416 : }
417 : }
418 2436633 : EmitBytecode(node);
419 2436632 : }
420 :
421 28311 : void BytecodeArrayWriter::EmitSwitch(BytecodeNode* node,
422 : BytecodeJumpTable* jump_table) {
423 : DCHECK(Bytecodes::IsSwitch(node->bytecode()));
424 :
425 28311 : size_t current_offset = bytecodes()->size();
426 28311 : if (node->operand_scale() > OperandScale::kSingle) {
427 : // Adjust for scaling byte prefix.
428 121 : current_offset += 1;
429 : }
430 : jump_table->set_switch_bytecode_offset(current_offset);
431 :
432 28311 : EmitBytecode(node);
433 0 : }
434 :
435 : } // namespace interpreter
436 : } // namespace internal
437 : } // namespace v8
|