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_INTERPRETER_ASSEMBLER_H_
6 : #define V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_
7 :
8 : #include "src/allocation.h"
9 : #include "src/builtins/builtins.h"
10 : #include "src/code-stub-assembler.h"
11 : #include "src/globals.h"
12 : #include "src/interpreter/bytecode-register.h"
13 : #include "src/interpreter/bytecodes.h"
14 : #include "src/runtime/runtime.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 : namespace interpreter {
19 :
20 : class V8_EXPORT_PRIVATE InterpreterAssembler : public CodeStubAssembler {
21 : public:
22 : InterpreterAssembler(compiler::CodeAssemblerState* state, Bytecode bytecode,
23 : OperandScale operand_scale);
24 : ~InterpreterAssembler();
25 :
26 : // Returns the 32-bit unsigned count immediate for bytecode operand
27 : // |operand_index| in the current bytecode.
28 : compiler::Node* BytecodeOperandCount(int operand_index);
29 : // Returns the 32-bit unsigned flag for bytecode operand |operand_index|
30 : // in the current bytecode.
31 : compiler::Node* BytecodeOperandFlag(int operand_index);
32 : // Returns the 32-bit zero-extended index immediate for bytecode operand
33 : // |operand_index| in the current bytecode.
34 : compiler::Node* BytecodeOperandIdxInt32(int operand_index);
35 : // Returns the word zero-extended index immediate for bytecode operand
36 : // |operand_index| in the current bytecode.
37 : compiler::Node* BytecodeOperandIdx(int operand_index);
38 : // Returns the smi index immediate for bytecode operand |operand_index|
39 : // in the current bytecode.
40 : compiler::Node* BytecodeOperandIdxSmi(int operand_index);
41 : // Returns the 32-bit unsigned immediate for bytecode operand |operand_index|
42 : // in the current bytecode.
43 : compiler::Node* BytecodeOperandUImm(int operand_index);
44 : // Returns the word-size unsigned immediate for bytecode operand
45 : // |operand_index| in the current bytecode.
46 : compiler::Node* BytecodeOperandUImmWord(int operand_index);
47 : // Returns the unsigned smi immediate for bytecode operand |operand_index| in
48 : // the current bytecode.
49 : compiler::Node* BytecodeOperandUImmSmi(int operand_index);
50 : // Returns the 32-bit signed immediate for bytecode operand |operand_index|
51 : // in the current bytecode.
52 : compiler::Node* BytecodeOperandImm(int operand_index);
53 : // Returns the word-size signed immediate for bytecode operand |operand_index|
54 : // in the current bytecode.
55 : compiler::Node* BytecodeOperandImmIntPtr(int operand_index);
56 : // Returns the smi immediate for bytecode operand |operand_index| in the
57 : // current bytecode.
58 : compiler::Node* BytecodeOperandImmSmi(int operand_index);
59 : // Returns the 32-bit unsigned runtime id immediate for bytecode operand
60 : // |operand_index| in the current bytecode.
61 : compiler::Node* BytecodeOperandRuntimeId(int operand_index);
62 : // Returns the 32-bit unsigned native context index immediate for bytecode
63 : // operand |operand_index| in the current bytecode.
64 : compiler::Node* BytecodeOperandNativeContextIndex(int operand_index);
65 : // Returns the 32-bit unsigned intrinsic id immediate for bytecode operand
66 : // |operand_index| in the current bytecode.
67 : compiler::Node* BytecodeOperandIntrinsicId(int operand_index);
68 :
69 : // Accumulator.
70 : compiler::Node* GetAccumulator();
71 : void SetAccumulator(compiler::Node* value);
72 :
73 : // Context.
74 : compiler::Node* GetContext();
75 : void SetContext(compiler::Node* value);
76 :
77 : // Context at |depth| in the context chain starting at |context|.
78 : compiler::Node* GetContextAtDepth(compiler::Node* context,
79 : compiler::Node* depth);
80 :
81 : // Goto the given |target| if the context chain starting at |context| has any
82 : // extensions up to the given |depth|.
83 : void GotoIfHasContextExtensionUpToDepth(compiler::Node* context,
84 : compiler::Node* depth, Label* target);
85 :
86 : // A RegListNodePair provides an abstraction over lists of registers.
87 : class RegListNodePair {
88 : public:
89 : RegListNodePair(Node* base_reg_location, Node* reg_count)
90 174 : : base_reg_location_(base_reg_location), reg_count_(reg_count) {}
91 :
92 : compiler::Node* reg_count() const { return reg_count_; }
93 : compiler::Node* base_reg_location() const { return base_reg_location_; }
94 :
95 : private:
96 : compiler::Node* base_reg_location_;
97 : compiler::Node* reg_count_;
98 : };
99 :
100 : // Backup/restore register file to/from a fixed array of the correct length.
101 : // There is an asymmetry between suspend/export and resume/import.
102 : // - Suspend copies arguments and registers to the generator.
103 : // - Resume copies only the registers from the generator, the arguments
104 : // are copied by the ResumeGenerator trampoline.
105 : compiler::Node* ExportParametersAndRegisterFile(
106 : TNode<FixedArray> array, const RegListNodePair& registers,
107 : TNode<Int32T> formal_parameter_count);
108 : compiler::Node* ImportRegisterFile(TNode<FixedArray> array,
109 : const RegListNodePair& registers,
110 : TNode<Int32T> formal_parameter_count);
111 :
112 : // Loads from and stores to the interpreter register file.
113 : compiler::Node* LoadRegister(Register reg);
114 : compiler::Node* LoadAndUntagRegister(Register reg);
115 : compiler::Node* LoadRegisterAtOperandIndex(int operand_index);
116 : std::pair<compiler::Node*, compiler::Node*> LoadRegisterPairAtOperandIndex(
117 : int operand_index);
118 : void StoreRegister(compiler::Node* value, Register reg);
119 : void StoreAndTagRegister(compiler::Node* value, Register reg);
120 : void StoreRegisterAtOperandIndex(compiler::Node* value, int operand_index);
121 : void StoreRegisterPairAtOperandIndex(compiler::Node* value1,
122 : compiler::Node* value2,
123 : int operand_index);
124 : void StoreRegisterTripleAtOperandIndex(compiler::Node* value1,
125 : compiler::Node* value2,
126 : compiler::Node* value3,
127 : int operand_index);
128 :
129 : RegListNodePair GetRegisterListAtOperandIndex(int operand_index);
130 : Node* LoadRegisterFromRegisterList(const RegListNodePair& reg_list,
131 : int index);
132 : Node* RegisterLocationInRegisterList(const RegListNodePair& reg_list,
133 : int index);
134 :
135 : // Load constant at the index specified in operand |operand_index| from the
136 : // constant pool.
137 : compiler::Node* LoadConstantPoolEntryAtOperandIndex(int operand_index);
138 : // Load and untag constant at the index specified in operand |operand_index|
139 : // from the constant pool.
140 : compiler::Node* LoadAndUntagConstantPoolEntryAtOperandIndex(
141 : int operand_index);
142 : // Load constant at |index| in the constant pool.
143 : compiler::Node* LoadConstantPoolEntry(compiler::Node* index);
144 : // Load and untag constant at |index| in the constant pool.
145 : compiler::Node* LoadAndUntagConstantPoolEntry(compiler::Node* index);
146 :
147 : // Load the FeedbackVector for the current function. The retuned node could be
148 : // undefined.
149 : compiler::TNode<HeapObject> LoadFeedbackVector();
150 :
151 : // Increment the call count for a CALL_IC or construct call.
152 : // The call count is located at feedback_vector[slot_id + 1].
153 : void IncrementCallCount(compiler::Node* feedback_vector,
154 : compiler::Node* slot_id);
155 :
156 : // Collect the callable |target| feedback for either a CALL_IC or
157 : // an INSTANCEOF_IC in the |feedback_vector| at |slot_id|.
158 : void CollectCallableFeedback(compiler::Node* target, compiler::Node* context,
159 : compiler::Node* feedback_vector,
160 : compiler::Node* slot_id);
161 :
162 : // Collect CALL_IC feedback for |target| function in the
163 : // |feedback_vector| at |slot_id|, and the call counts in
164 : // the |feedback_vector| at |slot_id+1|.
165 : void CollectCallFeedback(compiler::Node* target, compiler::Node* context,
166 : compiler::Node* maybe_feedback_vector,
167 : compiler::Node* slot_id);
168 :
169 : // Call JSFunction or Callable |function| with |args| arguments, possibly
170 : // including the receiver depending on |receiver_mode|. After the call returns
171 : // directly dispatches to the next bytecode.
172 : void CallJSAndDispatch(compiler::Node* function, compiler::Node* context,
173 : const RegListNodePair& args,
174 : ConvertReceiverMode receiver_mode);
175 :
176 : // Call JSFunction or Callable |function| with |arg_count| arguments (not
177 : // including receiver) passed as |args|, possibly including the receiver
178 : // depending on |receiver_mode|. After the call returns directly dispatches to
179 : // the next bytecode.
180 : template <class... TArgs>
181 : void CallJSAndDispatch(Node* function, Node* context, Node* arg_count,
182 : ConvertReceiverMode receiver_mode, TArgs... args);
183 :
184 : // Call JSFunction or Callable |function| with |args|
185 : // arguments (not including receiver), and the final argument being spread.
186 : // After the call returns directly dispatches to the next bytecode.
187 : void CallJSWithSpreadAndDispatch(compiler::Node* function,
188 : compiler::Node* context,
189 : const RegListNodePair& args,
190 : compiler::Node* slot_id,
191 : compiler::Node* feedback_vector);
192 :
193 : // Call constructor |target| with |args| arguments (not including receiver).
194 : // The |new_target| is the same as the |target| for the new keyword, but
195 : // differs for the super keyword.
196 : compiler::Node* Construct(compiler::Node* target, compiler::Node* context,
197 : compiler::Node* new_target,
198 : const RegListNodePair& args,
199 : compiler::Node* slot_id,
200 : compiler::Node* feedback_vector);
201 :
202 : // Call constructor |target| with |args| arguments (not including
203 : // receiver). The last argument is always a spread. The |new_target| is the
204 : // same as the |target| for the new keyword, but differs for the super
205 : // keyword.
206 : compiler::Node* ConstructWithSpread(compiler::Node* target,
207 : compiler::Node* context,
208 : compiler::Node* new_target,
209 : const RegListNodePair& args,
210 : compiler::Node* slot_id,
211 : compiler::Node* feedback_vector);
212 :
213 : // Call runtime function with |args| arguments which will return |return_size|
214 : // number of values.
215 : compiler::Node* CallRuntimeN(compiler::Node* function_id,
216 : compiler::Node* context,
217 : const RegListNodePair& args,
218 : int return_size = 1);
219 :
220 : // Jump forward relative to the current bytecode by the |jump_offset|.
221 : compiler::Node* Jump(compiler::Node* jump_offset);
222 :
223 : // Jump backward relative to the current bytecode by the |jump_offset|.
224 : compiler::Node* JumpBackward(compiler::Node* jump_offset);
225 :
226 : // Jump forward relative to the current bytecode by |jump_offset| if the
227 : // word values |lhs| and |rhs| are equal.
228 : void JumpIfWordEqual(compiler::Node* lhs, compiler::Node* rhs,
229 : compiler::Node* jump_offset);
230 :
231 : // Jump forward relative to the current bytecode by |jump_offset| if the
232 : // word values |lhs| and |rhs| are not equal.
233 : void JumpIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs,
234 : compiler::Node* jump_offset);
235 :
236 : // Updates the profiler interrupt budget for a return.
237 : void UpdateInterruptBudgetOnReturn();
238 :
239 : // Returns the OSR nesting level from the bytecode header.
240 : compiler::Node* LoadOSRNestingLevel();
241 :
242 : // Dispatch to the bytecode.
243 : compiler::Node* Dispatch();
244 :
245 : // Dispatch bytecode as wide operand variant.
246 : void DispatchWide(OperandScale operand_scale);
247 :
248 : // Dispatch to |target_bytecode| at |new_bytecode_offset|.
249 : // |target_bytecode| should be equivalent to loading from the offset.
250 : compiler::Node* DispatchToBytecode(compiler::Node* target_bytecode,
251 : compiler::Node* new_bytecode_offset);
252 :
253 : // Abort with the given abort reason.
254 : void Abort(AbortReason abort_reason);
255 : void AbortIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs,
256 : AbortReason abort_reason);
257 : // Abort if |register_count| is invalid for given register file array.
258 : void AbortIfRegisterCountInvalid(compiler::Node* parameters_and_registers,
259 : compiler::Node* formal_parameter_count,
260 : compiler::Node* register_count);
261 :
262 : // Dispatch to frame dropper trampoline if necessary.
263 : void MaybeDropFrames(compiler::Node* context);
264 :
265 : // Returns the offset from the BytecodeArrayPointer of the current bytecode.
266 : compiler::Node* BytecodeOffset();
267 :
268 : protected:
269 : Bytecode bytecode() const { return bytecode_; }
270 : static bool TargetSupportsUnalignedAccess();
271 :
272 : void ToNumberOrNumeric(Object::Conversion mode);
273 :
274 : private:
275 : // Returns a tagged pointer to the current function's BytecodeArray object.
276 : compiler::Node* BytecodeArrayTaggedPointer();
277 :
278 : // Returns a raw pointer to first entry in the interpreter dispatch table.
279 : compiler::Node* DispatchTableRawPointer();
280 :
281 : // Returns the accumulator value without checking whether bytecode
282 : // uses it. This is intended to be used only in dispatch and in
283 : // tracing as these need to bypass accumulator use validity checks.
284 : compiler::Node* GetAccumulatorUnchecked();
285 :
286 : // Returns the frame pointer for the interpreted frame of the function being
287 : // interpreted.
288 : compiler::Node* GetInterpretedFramePointer();
289 :
290 : // Operations on registers.
291 : compiler::Node* RegisterLocation(Register reg);
292 : compiler::Node* RegisterLocation(compiler::Node* reg_index);
293 : compiler::Node* NextRegister(compiler::Node* reg_index);
294 : compiler::Node* LoadRegister(Node* reg_index);
295 : void StoreRegister(compiler::Node* value, compiler::Node* reg_index);
296 :
297 : // Saves and restores interpreter bytecode offset to the interpreter stack
298 : // frame when performing a call.
299 : void CallPrologue();
300 : void CallEpilogue();
301 :
302 : // Increment the dispatch counter for the (current, next) bytecode pair.
303 : void TraceBytecodeDispatch(compiler::Node* target_index);
304 :
305 : // Traces the current bytecode by calling |function_id|.
306 : void TraceBytecode(Runtime::FunctionId function_id);
307 :
308 : // Updates the bytecode array's interrupt budget by a 32-bit unsigned |weight|
309 : // and calls Runtime::kInterrupt if counter reaches zero. If |backward|, then
310 : // the interrupt budget is decremented, otherwise it is incremented.
311 : void UpdateInterruptBudget(compiler::Node* weight, bool backward);
312 :
313 : // Returns the offset of register |index| relative to RegisterFilePointer().
314 : compiler::Node* RegisterFrameOffset(compiler::Node* index);
315 :
316 : // Returns the offset of an operand relative to the current bytecode offset.
317 : compiler::Node* OperandOffset(int operand_index);
318 :
319 : // Returns a value built from an sequence of bytes in the bytecode
320 : // array starting at |relative_offset| from the current bytecode.
321 : // The |result_type| determines the size and signedness. of the
322 : // value read. This method should only be used on architectures that
323 : // do not support unaligned memory accesses.
324 : compiler::Node* BytecodeOperandReadUnaligned(
325 : int relative_offset, MachineType result_type,
326 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
327 :
328 : // Returns zero- or sign-extended to word32 value of the operand.
329 : compiler::Node* BytecodeOperandUnsignedByte(
330 : int operand_index,
331 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
332 : compiler::Node* BytecodeOperandSignedByte(
333 : int operand_index,
334 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
335 : compiler::Node* BytecodeOperandUnsignedShort(
336 : int operand_index,
337 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
338 : compiler::Node* BytecodeOperandSignedShort(
339 : int operand_index,
340 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
341 : compiler::Node* BytecodeOperandUnsignedQuad(
342 : int operand_index,
343 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
344 : compiler::Node* BytecodeOperandSignedQuad(
345 : int operand_index,
346 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
347 :
348 : // Returns zero- or sign-extended to word32 value of the operand of
349 : // given size.
350 : compiler::Node* BytecodeSignedOperand(
351 : int operand_index, OperandSize operand_size,
352 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
353 : compiler::Node* BytecodeUnsignedOperand(
354 : int operand_index, OperandSize operand_size,
355 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
356 :
357 : // Returns the word-size sign-extended register index for bytecode operand
358 : // |operand_index| in the current bytecode. Value is not poisoned on
359 : // speculation since the value loaded from the register is poisoned instead.
360 : compiler::Node* BytecodeOperandReg(
361 : int operand_index,
362 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
363 :
364 : // Returns the word zero-extended index immediate for bytecode operand
365 : // |operand_index| in the current bytecode for use when loading a .
366 : compiler::Node* BytecodeOperandConstantPoolIdx(
367 : int operand_index,
368 : LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
369 :
370 : // Jump relative to the current bytecode by the |jump_offset|. If |backward|,
371 : // then jump backward (subtract the offset), otherwise jump forward (add the
372 : // offset). Helper function for Jump and JumpBackward.
373 : compiler::Node* Jump(compiler::Node* jump_offset, bool backward);
374 :
375 : // Jump forward relative to the current bytecode by |jump_offset| if the
376 : // |condition| is true. Helper function for JumpIfWordEqual and
377 : // JumpIfWordNotEqual.
378 : void JumpConditional(compiler::Node* condition, compiler::Node* jump_offset);
379 :
380 : // Save the bytecode offset to the interpreter frame.
381 : void SaveBytecodeOffset();
382 : // Reload the bytecode offset from the interpreter frame.
383 : Node* ReloadBytecodeOffset();
384 :
385 : // Updates and returns BytecodeOffset() advanced by the current bytecode's
386 : // size. Traces the exit of the current bytecode.
387 : compiler::Node* Advance();
388 :
389 : // Updates and returns BytecodeOffset() advanced by delta bytecodes.
390 : // Traces the exit of the current bytecode.
391 : compiler::Node* Advance(int delta);
392 : compiler::Node* Advance(compiler::Node* delta, bool backward = false);
393 :
394 : // Load the bytecode at |bytecode_offset|.
395 : compiler::Node* LoadBytecode(compiler::Node* bytecode_offset);
396 :
397 : // Look ahead for Star and inline it in a branch. Returns a new target
398 : // bytecode node for dispatch.
399 : compiler::Node* StarDispatchLookahead(compiler::Node* target_bytecode);
400 :
401 : // Build code for Star at the current BytecodeOffset() and Advance() to the
402 : // next dispatch offset.
403 : void InlineStar();
404 :
405 : // Dispatch to the bytecode handler with code offset |handler|.
406 : compiler::Node* DispatchToBytecodeHandler(compiler::Node* handler,
407 : compiler::Node* bytecode_offset,
408 : compiler::Node* target_bytecode);
409 :
410 : // Dispatch to the bytecode handler with code entry point |handler_entry|.
411 : compiler::Node* DispatchToBytecodeHandlerEntry(
412 : compiler::Node* handler_entry, compiler::Node* bytecode_offset,
413 : compiler::Node* target_bytecode);
414 :
415 : int CurrentBytecodeSize() const;
416 :
417 : OperandScale operand_scale() const { return operand_scale_; }
418 :
419 : Bytecode bytecode_;
420 : OperandScale operand_scale_;
421 : CodeStubAssembler::Variable interpreted_frame_pointer_;
422 : CodeStubAssembler::Variable bytecode_array_;
423 : CodeStubAssembler::Variable bytecode_offset_;
424 : CodeStubAssembler::Variable dispatch_table_;
425 : CodeStubAssembler::Variable accumulator_;
426 : AccumulatorUse accumulator_use_;
427 : bool made_call_;
428 : bool reloaded_frame_ptr_;
429 : bool bytecode_array_valid_;
430 : bool disable_stack_check_across_call_;
431 : compiler::Node* stack_pointer_before_call_;
432 :
433 : DISALLOW_COPY_AND_ASSIGN(InterpreterAssembler);
434 : };
435 :
436 : } // namespace interpreter
437 : } // namespace internal
438 : } // namespace v8
439 :
440 : #endif // V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_
|