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 : #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_
6 : #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_
7 :
8 : #include "src/ast/ast.h"
9 : #include "src/base/compiler-specific.h"
10 : #include "src/globals.h"
11 : #include "src/interpreter/bytecode-array-writer.h"
12 : #include "src/interpreter/bytecode-flags.h"
13 : #include "src/interpreter/bytecode-register-allocator.h"
14 : #include "src/interpreter/bytecode-register.h"
15 : #include "src/interpreter/bytecode-source-info.h"
16 : #include "src/interpreter/bytecodes.h"
17 : #include "src/interpreter/constant-array-builder.h"
18 : #include "src/interpreter/handler-table-builder.h"
19 : #include "src/zone/zone-containers.h"
20 :
21 : namespace v8 {
22 : namespace internal {
23 :
24 : class FeedbackVectorSpec;
25 : class Isolate;
26 :
27 : namespace interpreter {
28 :
29 : class BytecodeLabel;
30 : class BytecodeLoopHeader;
31 : class BytecodeNode;
32 : class BytecodeRegisterOptimizer;
33 : class BytecodeJumpTable;
34 : class Register;
35 :
36 4272999 : class V8_EXPORT_PRIVATE BytecodeArrayBuilder final {
37 : public:
38 : BytecodeArrayBuilder(
39 : Zone* zone, int parameter_count, int locals_count,
40 : FeedbackVectorSpec* feedback_vector_spec = nullptr,
41 : SourcePositionTableBuilder::RecordingMode source_position_mode =
42 : SourcePositionTableBuilder::RECORD_SOURCE_POSITIONS);
43 :
44 : Handle<BytecodeArray> ToBytecodeArray(Isolate* isolate);
45 :
46 : // Get the number of parameters expected by function.
47 : int parameter_count() const {
48 : DCHECK_GE(parameter_count_, 0);
49 : return parameter_count_;
50 : }
51 :
52 : // Get the number of locals required for bytecode array.
53 : int locals_count() const {
54 : DCHECK_GE(local_register_count_, 0);
55 : return local_register_count_;
56 : }
57 :
58 : // Returns the number of fixed (non-temporary) registers.
59 : int fixed_register_count() const { return locals_count(); }
60 :
61 : // Returns the number of fixed and temporary registers.
62 : int total_register_count() const {
63 : DCHECK_LE(fixed_register_count(),
64 : register_allocator()->maximum_register_count());
65 : return register_allocator()->maximum_register_count();
66 : }
67 :
68 : Register Local(int index) const;
69 : Register Parameter(int parameter_index) const;
70 : Register Receiver() const;
71 :
72 : // Constant loads to accumulator.
73 : BytecodeArrayBuilder& LoadConstantPoolEntry(size_t entry);
74 : BytecodeArrayBuilder& LoadLiteral(Smi value);
75 : BytecodeArrayBuilder& LoadLiteral(double value);
76 : BytecodeArrayBuilder& LoadLiteral(const AstRawString* raw_string);
77 : BytecodeArrayBuilder& LoadLiteral(const Scope* scope);
78 : BytecodeArrayBuilder& LoadLiteral(AstBigInt bigint);
79 : BytecodeArrayBuilder& LoadLiteral(AstSymbol symbol);
80 : BytecodeArrayBuilder& LoadUndefined();
81 : BytecodeArrayBuilder& LoadNull();
82 : BytecodeArrayBuilder& LoadTheHole();
83 : BytecodeArrayBuilder& LoadTrue();
84 : BytecodeArrayBuilder& LoadFalse();
85 : BytecodeArrayBuilder& LoadBoolean(bool value);
86 :
87 : // Global loads to the accumulator and stores from the accumulator.
88 : BytecodeArrayBuilder& LoadGlobal(const AstRawString* name, int feedback_slot,
89 : TypeofMode typeof_mode);
90 : BytecodeArrayBuilder& StoreGlobal(const AstRawString* name,
91 : int feedback_slot);
92 :
93 : // Load the object at |slot_index| at |depth| in the context chain starting
94 : // with |context| into the accumulator.
95 : enum ContextSlotMutability { kImmutableSlot, kMutableSlot };
96 : BytecodeArrayBuilder& LoadContextSlot(Register context, int slot_index,
97 : int depth,
98 : ContextSlotMutability immutable);
99 :
100 : // Stores the object in the accumulator into |slot_index| at |depth| in the
101 : // context chain starting with |context|.
102 : BytecodeArrayBuilder& StoreContextSlot(Register context, int slot_index,
103 : int depth);
104 :
105 : // Load from a module variable into the accumulator. |depth| is the depth of
106 : // the current context relative to the module context.
107 : BytecodeArrayBuilder& LoadModuleVariable(int cell_index, int depth);
108 :
109 : // Store from the accumulator into a module variable. |depth| is the depth of
110 : // the current context relative to the module context.
111 : BytecodeArrayBuilder& StoreModuleVariable(int cell_index, int depth);
112 :
113 : // Register-accumulator transfers.
114 : BytecodeArrayBuilder& LoadAccumulatorWithRegister(Register reg);
115 : BytecodeArrayBuilder& StoreAccumulatorInRegister(Register reg);
116 :
117 : // Register-register transfer.
118 : BytecodeArrayBuilder& MoveRegister(Register from, Register to);
119 :
120 : // Named load property.
121 : BytecodeArrayBuilder& LoadNamedProperty(Register object,
122 : const AstRawString* name,
123 : int feedback_slot);
124 : // Named load property without feedback
125 : BytecodeArrayBuilder& LoadNamedPropertyNoFeedback(Register object,
126 : const AstRawString* name);
127 :
128 : // Keyed load property. The key should be in the accumulator.
129 : BytecodeArrayBuilder& LoadKeyedProperty(Register object, int feedback_slot);
130 : // Named load property of the @@iterator symbol.
131 : BytecodeArrayBuilder& LoadIteratorProperty(Register object,
132 : int feedback_slot);
133 : // Named load property of the @@asyncIterator symbol.
134 : BytecodeArrayBuilder& LoadAsyncIteratorProperty(Register object,
135 : int feedback_slot);
136 :
137 : // Store properties. Flag for NeedsSetFunctionName() should
138 : // be in the accumulator.
139 : BytecodeArrayBuilder& StoreDataPropertyInLiteral(
140 : Register object, Register name, DataPropertyInLiteralFlags flags,
141 : int feedback_slot);
142 :
143 : // Collect type information for developer tools. The value for which we
144 : // record the type is stored in the accumulator.
145 : BytecodeArrayBuilder& CollectTypeProfile(int position);
146 :
147 : // Store a property named by a property name. The value to be stored should be
148 : // in the accumulator.
149 : BytecodeArrayBuilder& StoreNamedProperty(Register object,
150 : const AstRawString* name,
151 : int feedback_slot,
152 : LanguageMode language_mode);
153 :
154 : // Store a property named by a property name without feedback slot. The value
155 : // to be stored should be in the accumulator.
156 : BytecodeArrayBuilder& StoreNamedPropertyNoFeedback(
157 : Register object, const AstRawString* name, LanguageMode language_mode);
158 :
159 : // Store a property named by a constant from the constant pool. The value to
160 : // be stored should be in the accumulator.
161 : BytecodeArrayBuilder& StoreNamedProperty(Register object,
162 : size_t constant_pool_entry,
163 : int feedback_slot,
164 : LanguageMode language_mode);
165 : // Store an own property named by a constant from the constant pool. The
166 : // value to be stored should be in the accumulator.
167 : BytecodeArrayBuilder& StoreNamedOwnProperty(Register object,
168 : const AstRawString* name,
169 : int feedback_slot);
170 : // Store a property keyed by a value in a register. The value to be stored
171 : // should be in the accumulator.
172 : BytecodeArrayBuilder& StoreKeyedProperty(Register object, Register key,
173 : int feedback_slot,
174 : LanguageMode language_mode);
175 : // Store an own element in an array literal. The value to be stored should be
176 : // in the accumulator.
177 : BytecodeArrayBuilder& StoreInArrayLiteral(Register array, Register index,
178 : int feedback_slot);
179 : // Store the home object property. The value to be stored should be in the
180 : // accumulator.
181 : BytecodeArrayBuilder& StoreHomeObjectProperty(Register object,
182 : int feedback_slot,
183 : LanguageMode language_mode);
184 :
185 : // Store the class fields property. The initializer to be stored should
186 : // be in the accumulator.
187 : BytecodeArrayBuilder& StoreClassFieldsInitializer(Register constructor,
188 : int feedback_slot);
189 :
190 : // Load class fields property.
191 : BytecodeArrayBuilder& LoadClassFieldsInitializer(Register constructor,
192 : int feedback_slot);
193 :
194 : // Lookup the variable with |name|.
195 : BytecodeArrayBuilder& LoadLookupSlot(const AstRawString* name,
196 : TypeofMode typeof_mode);
197 :
198 : // Lookup the variable with |name|, which is known to be at |slot_index| at
199 : // |depth| in the context chain if not shadowed by a context extension
200 : // somewhere in that context chain.
201 : BytecodeArrayBuilder& LoadLookupContextSlot(const AstRawString* name,
202 : TypeofMode typeof_mode,
203 : int slot_index, int depth);
204 :
205 : // Lookup the variable with |name|, which has its feedback in |feedback_slot|
206 : // and is known to be global if not shadowed by a context extension somewhere
207 : // up to |depth| in that context chain.
208 : BytecodeArrayBuilder& LoadLookupGlobalSlot(const AstRawString* name,
209 : TypeofMode typeof_mode,
210 : int feedback_slot, int depth);
211 :
212 : // Store value in the accumulator into the variable with |name|.
213 : BytecodeArrayBuilder& StoreLookupSlot(
214 : const AstRawString* name, LanguageMode language_mode,
215 : LookupHoistingMode lookup_hoisting_mode);
216 :
217 : // Create a new closure for a SharedFunctionInfo which will be inserted at
218 : // constant pool index |shared_function_info_entry|.
219 : BytecodeArrayBuilder& CreateClosure(size_t shared_function_info_entry,
220 : int slot, int flags);
221 :
222 : // Create a new local context for a |scope|.
223 : BytecodeArrayBuilder& CreateBlockContext(const Scope* scope);
224 :
225 : // Create a new context for a catch block with |exception| and |scope|.
226 : BytecodeArrayBuilder& CreateCatchContext(Register exception,
227 : const Scope* scope);
228 :
229 : // Create a new context with the given |scope| and size |slots|.
230 : BytecodeArrayBuilder& CreateFunctionContext(const Scope* scope, int slots);
231 :
232 : // Create a new eval context with the given |scope| and size |slots|.
233 : BytecodeArrayBuilder& CreateEvalContext(const Scope* scope, int slots);
234 :
235 : // Creates a new context with the given |scope| for a with-statement
236 : // with the |object| in a register.
237 : BytecodeArrayBuilder& CreateWithContext(Register object, const Scope* scope);
238 :
239 : // Create a new arguments object in the accumulator.
240 : BytecodeArrayBuilder& CreateArguments(CreateArgumentsType type);
241 :
242 : // Literals creation. Constant elements should be in the accumulator.
243 : BytecodeArrayBuilder& CreateRegExpLiteral(const AstRawString* pattern,
244 : int literal_index, int flags);
245 : BytecodeArrayBuilder& CreateArrayLiteral(size_t constant_elements_entry,
246 : int literal_index, int flags);
247 : BytecodeArrayBuilder& CreateEmptyArrayLiteral(int literal_index);
248 : BytecodeArrayBuilder& CreateArrayFromIterable();
249 : BytecodeArrayBuilder& CreateObjectLiteral(size_t constant_properties_entry,
250 : int literal_index, int flags);
251 : BytecodeArrayBuilder& CreateEmptyObjectLiteral();
252 : BytecodeArrayBuilder& CloneObject(Register source, int flags,
253 : int feedback_slot);
254 :
255 : // Gets or creates the template for a TemplateObjectDescription which will
256 : // be inserted at constant pool index |template_object_description_entry|.
257 : BytecodeArrayBuilder& GetTemplateObject(
258 : size_t template_object_description_entry, int feedback_slot);
259 :
260 : // Push the context in accumulator as the new context, and store in register
261 : // |context|.
262 : BytecodeArrayBuilder& PushContext(Register context);
263 :
264 : // Pop the current context and replace with |context|.
265 : BytecodeArrayBuilder& PopContext(Register context);
266 :
267 : // Call a JS function which is known to be a property of a JS object. The
268 : // JSFunction or Callable to be called should be in |callable|. The arguments
269 : // should be in |args|, with the receiver in |args[0]|. The call type of the
270 : // expression is in |call_type|. Type feedback is recorded in the
271 : // |feedback_slot| in the type feedback vector.
272 : BytecodeArrayBuilder& CallProperty(Register callable, RegisterList args,
273 : int feedback_slot);
274 :
275 : // Call a JS function with an known undefined receiver. The JSFunction or
276 : // Callable to be called should be in |callable|. The arguments should be in
277 : // |args|, with no receiver as it is implicitly set to undefined. Type
278 : // feedback is recorded in the |feedback_slot| in the type feedback vector.
279 : BytecodeArrayBuilder& CallUndefinedReceiver(Register callable,
280 : RegisterList args,
281 : int feedback_slot);
282 :
283 : // Call a JS function with an any receiver, possibly (but not necessarily)
284 : // undefined. The JSFunction or Callable to be called should be in |callable|.
285 : // The arguments should be in |args|, with the receiver in |args[0]|. Type
286 : // feedback is recorded in the |feedback_slot| in the type feedback vector.
287 : BytecodeArrayBuilder& CallAnyReceiver(Register callable, RegisterList args,
288 : int feedback_slot);
289 :
290 : // Call a JS function with an any receiver, possibly (but not necessarily)
291 : // undefined. The JSFunction or Callable to be called should be in |callable|.
292 : // The arguments should be in |args|, with the receiver in |args[0]|.
293 : BytecodeArrayBuilder& CallNoFeedback(Register callable, RegisterList args);
294 :
295 : // Tail call into a JS function. The JSFunction or Callable to be called
296 : // should be in |callable|. The arguments should be in |args|, with the
297 : // receiver in |args[0]|. Type feedback is recorded in the |feedback_slot| in
298 : // the type feedback vector.
299 : BytecodeArrayBuilder& TailCall(Register callable, RegisterList args,
300 : int feedback_slot);
301 :
302 : // Call a JS function. The JSFunction or Callable to be called should be in
303 : // |callable|, the receiver in |args[0]| and the arguments in |args[1]|
304 : // onwards. The final argument must be a spread.
305 : BytecodeArrayBuilder& CallWithSpread(Register callable, RegisterList args,
306 : int feedback_slot);
307 :
308 : // Call the Construct operator. The accumulator holds the |new_target|.
309 : // The |constructor| is in a register and arguments are in |args|.
310 : BytecodeArrayBuilder& Construct(Register constructor, RegisterList args,
311 : int feedback_slot);
312 :
313 : // Call the Construct operator for use with a spread. The accumulator holds
314 : // the |new_target|. The |constructor| is in a register and arguments are in
315 : // |args|. The final argument must be a spread.
316 : BytecodeArrayBuilder& ConstructWithSpread(Register constructor,
317 : RegisterList args,
318 : int feedback_slot);
319 :
320 : // Call the runtime function with |function_id| and arguments |args|.
321 : BytecodeArrayBuilder& CallRuntime(Runtime::FunctionId function_id,
322 : RegisterList args);
323 : // Call the runtime function with |function_id| with single argument |arg|.
324 : BytecodeArrayBuilder& CallRuntime(Runtime::FunctionId function_id,
325 : Register arg);
326 : // Call the runtime function with |function_id| with no arguments.
327 : BytecodeArrayBuilder& CallRuntime(Runtime::FunctionId function_id);
328 :
329 : // Call the runtime function with |function_id| and arguments |args|, that
330 : // returns a pair of values. The return values will be returned in
331 : // |return_pair|.
332 : BytecodeArrayBuilder& CallRuntimeForPair(Runtime::FunctionId function_id,
333 : RegisterList args,
334 : RegisterList return_pair);
335 : // Call the runtime function with |function_id| with single argument |arg|
336 : // that returns a pair of values. The return values will be returned in
337 : // |return_pair|.
338 : BytecodeArrayBuilder& CallRuntimeForPair(Runtime::FunctionId function_id,
339 : Register arg,
340 : RegisterList return_pair);
341 :
342 : // Call the JS runtime function with |context_index| and arguments |args|,
343 : // with no receiver as it is implicitly set to undefined.
344 : BytecodeArrayBuilder& CallJSRuntime(int context_index, RegisterList args);
345 :
346 : // Operators (register holds the lhs value, accumulator holds the rhs value).
347 : // Type feedback will be recorded in the |feedback_slot|
348 : BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg,
349 : int feedback_slot);
350 : // Same as above, but lhs in the accumulator and rhs in |literal|.
351 : BytecodeArrayBuilder& BinaryOperationSmiLiteral(Token::Value binop,
352 : Smi literal,
353 : int feedback_slot);
354 :
355 : // Unary and Count Operators (value stored in accumulator).
356 : // Type feedback will be recorded in the |feedback_slot|
357 : BytecodeArrayBuilder& UnaryOperation(Token::Value op, int feedback_slot);
358 :
359 : enum class ToBooleanMode {
360 : kConvertToBoolean, // Perform ToBoolean conversion on accumulator.
361 : kAlreadyBoolean, // Accumulator is already a Boolean.
362 : };
363 :
364 : // Unary Operators.
365 : BytecodeArrayBuilder& LogicalNot(ToBooleanMode mode);
366 : BytecodeArrayBuilder& TypeOf();
367 :
368 : // Expects a heap object in the accumulator. Returns its super constructor in
369 : // the register |out| if it passes the IsConstructor test. Otherwise, it
370 : // throws a TypeError exception.
371 : BytecodeArrayBuilder& GetSuperConstructor(Register out);
372 :
373 : // Deletes property from an object. This expects that accumulator contains
374 : // the key to be deleted and the register contains a reference to the object.
375 : BytecodeArrayBuilder& Delete(Register object, LanguageMode language_mode);
376 :
377 : // JavaScript defines two kinds of 'nil'.
378 : enum NilValue { kNullValue, kUndefinedValue };
379 :
380 : // Tests.
381 : BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg,
382 : int feedback_slot);
383 : BytecodeArrayBuilder& CompareReference(Register reg);
384 : BytecodeArrayBuilder& CompareUndetectable();
385 : BytecodeArrayBuilder& CompareUndefined();
386 : BytecodeArrayBuilder& CompareNull();
387 : BytecodeArrayBuilder& CompareNil(Token::Value op, NilValue nil);
388 : BytecodeArrayBuilder& CompareTypeOf(
389 : TestTypeOfFlags::LiteralFlag literal_flag);
390 :
391 : // Converts accumulator and stores result in register |out|.
392 : BytecodeArrayBuilder& ToObject(Register out);
393 : BytecodeArrayBuilder& ToName(Register out);
394 : BytecodeArrayBuilder& ToString();
395 :
396 : // Converts accumulator and stores result back in accumulator.
397 : BytecodeArrayBuilder& ToNumber(int feedback_slot);
398 : BytecodeArrayBuilder& ToNumeric(int feedback_slot);
399 :
400 : // Exception handling.
401 : BytecodeArrayBuilder& MarkHandler(int handler_id,
402 : HandlerTable::CatchPrediction will_catch);
403 : BytecodeArrayBuilder& MarkTryBegin(int handler_id, Register context);
404 : BytecodeArrayBuilder& MarkTryEnd(int handler_id);
405 :
406 : // Flow Control.
407 : BytecodeArrayBuilder& Bind(BytecodeLabel* label);
408 : BytecodeArrayBuilder& Bind(BytecodeLoopHeader* label);
409 : BytecodeArrayBuilder& Bind(BytecodeJumpTable* jump_table, int case_value);
410 :
411 : BytecodeArrayBuilder& Jump(BytecodeLabel* label);
412 : BytecodeArrayBuilder& JumpLoop(BytecodeLoopHeader* loop_header,
413 : int loop_depth);
414 :
415 : BytecodeArrayBuilder& JumpIfTrue(ToBooleanMode mode, BytecodeLabel* label);
416 : BytecodeArrayBuilder& JumpIfFalse(ToBooleanMode mode, BytecodeLabel* label);
417 : BytecodeArrayBuilder& JumpIfJSReceiver(BytecodeLabel* label);
418 : BytecodeArrayBuilder& JumpIfNull(BytecodeLabel* label);
419 : BytecodeArrayBuilder& JumpIfNotNull(BytecodeLabel* label);
420 : BytecodeArrayBuilder& JumpIfUndefined(BytecodeLabel* label);
421 : BytecodeArrayBuilder& JumpIfNotUndefined(BytecodeLabel* label);
422 : BytecodeArrayBuilder& JumpIfNil(BytecodeLabel* label, Token::Value op,
423 : NilValue nil);
424 : BytecodeArrayBuilder& JumpIfNotNil(BytecodeLabel* label, Token::Value op,
425 : NilValue nil);
426 :
427 : BytecodeArrayBuilder& SwitchOnSmiNoFeedback(BytecodeJumpTable* jump_table);
428 :
429 : BytecodeArrayBuilder& StackCheck(int position);
430 :
431 : // Sets the pending message to the value in the accumulator, and returns the
432 : // previous pending message in the accumulator.
433 : BytecodeArrayBuilder& SetPendingMessage();
434 :
435 : BytecodeArrayBuilder& Throw();
436 : BytecodeArrayBuilder& ReThrow();
437 : BytecodeArrayBuilder& Abort(AbortReason reason);
438 : BytecodeArrayBuilder& Return();
439 : BytecodeArrayBuilder& ThrowReferenceErrorIfHole(const AstRawString* name);
440 : BytecodeArrayBuilder& ThrowSuperNotCalledIfHole();
441 : BytecodeArrayBuilder& ThrowSuperAlreadyCalledIfNotHole();
442 :
443 : // Debugger.
444 : BytecodeArrayBuilder& Debugger();
445 :
446 : // Increment the block counter at the given slot (block code coverage).
447 : BytecodeArrayBuilder& IncBlockCounter(int slot);
448 :
449 : // Complex flow control.
450 : BytecodeArrayBuilder& ForInEnumerate(Register receiver);
451 : BytecodeArrayBuilder& ForInPrepare(RegisterList cache_info_triple,
452 : int feedback_slot);
453 : BytecodeArrayBuilder& ForInContinue(Register index, Register cache_length);
454 : BytecodeArrayBuilder& ForInNext(Register receiver, Register index,
455 : RegisterList cache_type_array_pair,
456 : int feedback_slot);
457 : BytecodeArrayBuilder& ForInStep(Register index);
458 :
459 : // Generators.
460 : BytecodeArrayBuilder& SuspendGenerator(Register generator,
461 : RegisterList registers,
462 : int suspend_id);
463 : BytecodeArrayBuilder& SwitchOnGeneratorState(Register generator,
464 : BytecodeJumpTable* jump_table);
465 : BytecodeArrayBuilder& ResumeGenerator(Register generator,
466 : RegisterList registers);
467 :
468 : // Creates a new handler table entry and returns a {hander_id} identifying the
469 : // entry, so that it can be referenced by above exception handling support.
470 154535 : int NewHandlerEntry() { return handler_table_builder()->NewHandlerEntry(); }
471 :
472 : // Allocates a new jump table of given |size| and |case_value_base| in the
473 : // constant pool.
474 : BytecodeJumpTable* AllocateJumpTable(int size, int case_value_base);
475 :
476 : // Gets a constant pool entry.
477 : size_t GetConstantPoolEntry(const AstRawString* raw_string);
478 : size_t GetConstantPoolEntry(AstBigInt bigint);
479 : size_t GetConstantPoolEntry(const Scope* scope);
480 : size_t GetConstantPoolEntry(double number);
481 : #define ENTRY_GETTER(NAME, ...) size_t NAME##ConstantPoolEntry();
482 : SINGLETON_CONSTANT_ENTRY_TYPES(ENTRY_GETTER)
483 : #undef ENTRY_GETTER
484 :
485 : // Allocates a slot in the constant pool which can later be set.
486 : size_t AllocateDeferredConstantPoolEntry();
487 : // Sets the deferred value into an allocated constant pool entry.
488 : void SetDeferredConstantPoolEntry(size_t entry, Handle<Object> object);
489 :
490 : void InitializeReturnPosition(FunctionLiteral* literal);
491 :
492 : void SetStatementPosition(Statement* stmt) {
493 13120769 : if (stmt->position() == kNoSourcePosition) return;
494 : latest_source_info_.MakeStatementPosition(stmt->position());
495 : }
496 :
497 : void SetExpressionPosition(Expression* expr) {
498 : SetExpressionPosition(expr->position());
499 : }
500 :
501 : void SetExpressionPosition(int position) {
502 24605692 : if (position == kNoSourcePosition) return;
503 22409103 : if (!latest_source_info_.is_statement()) {
504 : // Ensure the current expression position is overwritten with the
505 : // latest value.
506 : latest_source_info_.MakeExpressionPosition(position);
507 : }
508 : }
509 :
510 : void SetExpressionAsStatementPosition(Expression* expr) {
511 313536 : if (expr->position() == kNoSourcePosition) return;
512 : latest_source_info_.MakeStatementPosition(expr->position());
513 : }
514 :
515 2462117 : void SetReturnPosition(int source_position, FunctionLiteral* literal) {
516 2462117 : if (source_position != kNoSourcePosition) {
517 : latest_source_info_.MakeStatementPosition(source_position);
518 1527041 : } else if (literal->return_position() != kNoSourcePosition) {
519 1527086 : latest_source_info_.MakeStatementPosition(literal->return_position());
520 : }
521 2462163 : }
522 :
523 : bool RemainderOfBlockIsDead() const {
524 : return bytecode_array_writer_.RemainderOfBlockIsDead();
525 : }
526 :
527 : // Returns the raw operand value for the given register or register list.
528 : uint32_t GetInputRegisterOperand(Register reg);
529 : uint32_t GetOutputRegisterOperand(Register reg);
530 : uint32_t GetInputRegisterListOperand(RegisterList reg_list);
531 : uint32_t GetOutputRegisterListOperand(RegisterList reg_list);
532 :
533 : // Outputs raw register transfer bytecodes without going through the register
534 : // optimizer.
535 : void OutputLdarRaw(Register reg);
536 : void OutputStarRaw(Register reg);
537 : void OutputMovRaw(Register src, Register dest);
538 :
539 : // Accessors
540 : BytecodeRegisterAllocator* register_allocator() {
541 21069533 : return ®ister_allocator_;
542 : }
543 : const BytecodeRegisterAllocator* register_allocator() const {
544 : return ®ister_allocator_;
545 : }
546 : Zone* zone() const { return zone_; }
547 :
548 : private:
549 : friend class BytecodeRegisterAllocator;
550 : template <Bytecode bytecode, AccumulatorUse accumulator_use,
551 : OperandType... operand_types>
552 : friend class BytecodeNodeBuilder;
553 :
554 : const FeedbackVectorSpec* feedback_vector_spec() const {
555 : return feedback_vector_spec_;
556 : }
557 :
558 : // Returns the current source position for the given |bytecode|.
559 : V8_INLINE BytecodeSourceInfo CurrentSourcePosition(Bytecode bytecode);
560 :
561 : #define DECLARE_BYTECODE_OUTPUT(Name, ...) \
562 : template <typename... Operands> \
563 : V8_INLINE BytecodeNode Create##Name##Node(Operands... operands); \
564 : template <typename... Operands> \
565 : V8_INLINE void Output##Name(Operands... operands); \
566 : template <typename... Operands> \
567 : V8_INLINE void Output##Name(BytecodeLabel* label, Operands... operands);
568 : BYTECODE_LIST(DECLARE_BYTECODE_OUTPUT)
569 : #undef DECLARE_OPERAND_TYPE_INFO
570 :
571 : V8_INLINE void OutputJumpLoop(BytecodeLoopHeader* loop_header,
572 : int loop_depth);
573 : V8_INLINE void OutputSwitchOnSmiNoFeedback(BytecodeJumpTable* jump_table);
574 :
575 : bool RegisterIsValid(Register reg) const;
576 : bool RegisterListIsValid(RegisterList reg_list) const;
577 :
578 : // Sets a deferred source info which should be emitted before any future
579 : // source info (either attached to a following bytecode or as a nop).
580 : void SetDeferredSourceInfo(BytecodeSourceInfo source_info);
581 : // Either attach deferred source info to node, or emit it as a nop bytecode
582 : // if node already have valid source info.
583 : void AttachOrEmitDeferredSourceInfo(BytecodeNode* node);
584 :
585 : // Write bytecode to bytecode array.
586 : void Write(BytecodeNode* node);
587 : void WriteJump(BytecodeNode* node, BytecodeLabel* label);
588 : void WriteJumpLoop(BytecodeNode* node, BytecodeLoopHeader* loop_header);
589 : void WriteSwitch(BytecodeNode* node, BytecodeJumpTable* label);
590 :
591 : // Not implemented as the illegal bytecode is used inside internally
592 : // to indicate a bytecode field is not valid or an error has occurred
593 : // during bytecode generation.
594 : BytecodeArrayBuilder& Illegal();
595 :
596 : template <Bytecode bytecode, AccumulatorUse accumulator_use>
597 : void PrepareToOutputBytecode();
598 :
599 : BytecodeArrayWriter* bytecode_array_writer() {
600 : return &bytecode_array_writer_;
601 : }
602 : ConstantArrayBuilder* constant_array_builder() {
603 22609919 : return &constant_array_builder_;
604 : }
605 : const ConstantArrayBuilder* constant_array_builder() const {
606 : return &constant_array_builder_;
607 : }
608 : HandlerTableBuilder* handler_table_builder() {
609 2729622 : return &handler_table_builder_;
610 : }
611 :
612 : Zone* zone_;
613 : FeedbackVectorSpec* feedback_vector_spec_;
614 : bool bytecode_generated_;
615 : ConstantArrayBuilder constant_array_builder_;
616 : HandlerTableBuilder handler_table_builder_;
617 : int parameter_count_;
618 : int local_register_count_;
619 : BytecodeRegisterAllocator register_allocator_;
620 : BytecodeArrayWriter bytecode_array_writer_;
621 : BytecodeRegisterOptimizer* register_optimizer_;
622 : BytecodeSourceInfo latest_source_info_;
623 : BytecodeSourceInfo deferred_source_info_;
624 :
625 : DISALLOW_COPY_AND_ASSIGN(BytecodeArrayBuilder);
626 : };
627 :
628 : V8_EXPORT_PRIVATE std::ostream& operator<<(
629 : std::ostream& os, const BytecodeArrayBuilder::ToBooleanMode& mode);
630 :
631 : } // namespace interpreter
632 : } // namespace internal
633 : } // namespace v8
634 :
635 : #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_
|