interpreter-assembler.h 18.6 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 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.

#ifndef V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_
#define V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_

8
#include "src/builtins/builtins.h"
9
#include "src/codegen/code-stub-assembler.h"
10
#include "src/common/globals.h"
11
#include "src/interpreter/bytecode-register.h"
12 13
#include "src/interpreter/bytecodes.h"
#include "src/runtime/runtime.h"
14
#include "src/utils/allocation.h"
15 16 17 18 19

namespace v8 {
namespace internal {
namespace interpreter {

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 27
  // Returns the 32-bit unsigned count immediate for bytecode operand
  // |operand_index| in the current bytecode.
28
  TNode<Uint32T> BytecodeOperandCount(int operand_index);
29 30
  // Returns the 32-bit unsigned flag for bytecode operand |operand_index|
  // in the current bytecode.
31
  TNode<Uint32T> BytecodeOperandFlag(int operand_index);
32 33
  // Returns the 32-bit zero-extended index immediate for bytecode operand
  // |operand_index| in the current bytecode.
34
  TNode<Uint32T> BytecodeOperandIdxInt32(int operand_index);
35 36
  // Returns the word zero-extended index immediate for bytecode operand
  // |operand_index| in the current bytecode.
37
  TNode<UintPtrT> BytecodeOperandIdx(int operand_index);
38 39
  // Returns the smi index immediate for bytecode operand |operand_index|
  // in the current bytecode.
40
  TNode<Smi> BytecodeOperandIdxSmi(int operand_index);
41 42 43
  // Returns the TaggedIndex immediate for bytecode operand |operand_index|
  // in the current bytecode.
  TNode<TaggedIndex> BytecodeOperandIdxTaggedIndex(int operand_index);
44 45
  // Returns the 32-bit unsigned immediate for bytecode operand |operand_index|
  // in the current bytecode.
46
  TNode<Uint32T> BytecodeOperandUImm(int operand_index);
47 48
  // Returns the word-size unsigned immediate for bytecode operand
  // |operand_index| in the current bytecode.
49
  TNode<UintPtrT> BytecodeOperandUImmWord(int operand_index);
50 51
  // Returns the unsigned smi immediate for bytecode operand |operand_index| in
  // the current bytecode.
52
  TNode<Smi> BytecodeOperandUImmSmi(int operand_index);
53 54
  // Returns the 32-bit signed immediate for bytecode operand |operand_index|
  // in the current bytecode.
55
  TNode<Int32T> BytecodeOperandImm(int operand_index);
56 57
  // Returns the word-size signed immediate for bytecode operand |operand_index|
  // in the current bytecode.
58
  TNode<IntPtrT> BytecodeOperandImmIntPtr(int operand_index);
59
  // Returns the smi immediate for bytecode operand |operand_index| in the
60
  // current bytecode.
61
  TNode<Smi> BytecodeOperandImmSmi(int operand_index);
62
  // Returns the 32-bit unsigned runtime id immediate for bytecode operand
63
  // |operand_index| in the current bytecode.
64
  TNode<Uint32T> BytecodeOperandRuntimeId(int operand_index);
65
  // Returns the word zero-extended native context index immediate for bytecode
66
  // operand |operand_index| in the current bytecode.
67
  TNode<UintPtrT> BytecodeOperandNativeContextIndex(int operand_index);
68
  // Returns the 32-bit unsigned intrinsic id immediate for bytecode operand
69
  // |operand_index| in the current bytecode.
70
  TNode<Uint32T> BytecodeOperandIntrinsicId(int operand_index);
71
  // Accumulator.
72
  TNode<Object> GetAccumulator();
73
  void SetAccumulator(TNode<Object> value);
74 75

  // Context.
76 77
  TNode<Context> GetContext();
  void SetContext(TNode<Context> value);
78

79
  // Context at |depth| in the context chain starting at |context|.
80 81
  TNode<Context> GetContextAtDepth(TNode<Context> context,
                                   TNode<Uint32T> depth);
82

83 84
  // Goto the given |target| if the context chain starting at |context| has any
  // extensions up to the given |depth|.
85 86
  void GotoIfHasContextExtensionUpToDepth(TNode<Context> context,
                                          TNode<Uint32T> depth, Label* target);
87

88 89 90
  // A RegListNodePair provides an abstraction over lists of registers.
  class RegListNodePair {
   public:
91
    RegListNodePair(TNode<IntPtrT> base_reg_location, TNode<Word32T> reg_count)
92 93
        : base_reg_location_(base_reg_location), reg_count_(reg_count) {}

94 95
    TNode<Word32T> reg_count() const { return reg_count_; }
    TNode<IntPtrT> base_reg_location() const { return base_reg_location_; }
96 97

   private:
98 99
    TNode<IntPtrT> base_reg_location_;
    TNode<Word32T> reg_count_;
100 101
  };

102
  // Backup/restore register file to/from a fixed array of the correct length.
103 104 105 106
  // There is an asymmetry between suspend/export and resume/import.
  // - Suspend copies arguments and registers to the generator.
  // - Resume copies only the registers from the generator, the arguments
  //   are copied by the ResumeGenerator trampoline.
107
  TNode<FixedArray> ExportParametersAndRegisterFile(
108 109
      TNode<FixedArray> array, const RegListNodePair& registers,
      TNode<Int32T> formal_parameter_count);
110 111 112
  TNode<FixedArray> ImportRegisterFile(TNode<FixedArray> array,
                                       const RegListNodePair& registers,
                                       TNode<Int32T> formal_parameter_count);
113

114
  // Loads from and stores to the interpreter register file.
115 116 117 118 119 120 121 122 123 124 125 126
  TNode<Object> LoadRegister(Register reg);
  TNode<IntPtrT> LoadAndUntagRegister(Register reg);
  TNode<Object> LoadRegisterAtOperandIndex(int operand_index);
  std::pair<TNode<Object>, TNode<Object>> LoadRegisterPairAtOperandIndex(
      int operand_index);
  void StoreRegister(TNode<Object> value, Register reg);
  void StoreRegisterAtOperandIndex(TNode<Object> value, int operand_index);
  void StoreRegisterPairAtOperandIndex(TNode<Object> value1,
                                       TNode<Object> value2, int operand_index);
  void StoreRegisterTripleAtOperandIndex(TNode<Object> value1,
                                         TNode<Object> value2,
                                         TNode<Object> value3,
127 128 129
                                         int operand_index);

  RegListNodePair GetRegisterListAtOperandIndex(int operand_index);
130 131
  TNode<Object> LoadRegisterFromRegisterList(const RegListNodePair& reg_list,
                                             int index);
132 133
  TNode<IntPtrT> RegisterLocationInRegisterList(const RegListNodePair& reg_list,
                                                int index);
134 135 136

  // Load constant at the index specified in operand |operand_index| from the
  // constant pool.
137
  TNode<Object> LoadConstantPoolEntryAtOperandIndex(int operand_index);
138 139
  // Load and untag constant at the index specified in operand |operand_index|
  // from the constant pool.
140
  TNode<IntPtrT> LoadAndUntagConstantPoolEntryAtOperandIndex(int operand_index);
141
  // Load constant at |index| in the constant pool.
142
  TNode<Object> LoadConstantPoolEntry(TNode<WordT> index);
143
  // Load and untag constant at |index| in the constant pool.
144
  TNode<IntPtrT> LoadAndUntagConstantPoolEntry(TNode<WordT> index);
145

146 147
  // Load the FeedbackVector for the current function. The retuned node could be
  // undefined.
148
  TNode<HeapObject> LoadFeedbackVector();
149

150
  // Call JSFunction or Callable |function| with |args| arguments, possibly
151 152
  // including the receiver depending on |receiver_mode|. After the call returns
  // directly dispatches to the next bytecode.
153
  void CallJSAndDispatch(TNode<Object> function, TNode<Context> context,
154
                         const RegListNodePair& args,
155
                         ConvertReceiverMode receiver_mode);
156

157 158 159 160 161
  // Call JSFunction or Callable |function| with |arg_count| arguments (not
  // including receiver) passed as |args|, possibly including the receiver
  // depending on |receiver_mode|. After the call returns directly dispatches to
  // the next bytecode.
  template <class... TArgs>
162 163
  void CallJSAndDispatch(TNode<Object> function, TNode<Context> context,
                         TNode<Word32T> arg_count,
164 165
                         ConvertReceiverMode receiver_mode, TArgs... args);

166 167 168
  // Call JSFunction or Callable |function| with |args|
  // arguments (not including receiver), and the final argument being spread.
  // After the call returns directly dispatches to the next bytecode.
169 170
  void CallJSWithSpreadAndDispatch(TNode<Object> function,
                                   TNode<Context> context,
171
                                   const RegListNodePair& args,
172
                                   TNode<UintPtrT> slot_id,
173
                                   TNode<HeapObject> maybe_feedback_vector);
174

175 176 177
  // Call constructor |target| with |args| arguments (not including receiver).
  // The |new_target| is the same as the |target| for the new keyword, but
  // differs for the super keyword.
178 179 180 181
  TNode<Object> Construct(TNode<Object> target, TNode<Context> context,
                          TNode<Object> new_target, const RegListNodePair& args,
                          TNode<UintPtrT> slot_id,
                          TNode<HeapObject> maybe_feedback_vector);
182

183 184 185 186
  // Call constructor |target| with |args| arguments (not including
  // receiver). The last argument is always a spread. The |new_target| is the
  // same as the |target| for the new keyword, but differs for the super
  // keyword.
187 188 189 190 191 192
  TNode<Object> ConstructWithSpread(TNode<Object> target,
                                    TNode<Context> context,
                                    TNode<Object> new_target,
                                    const RegListNodePair& args,
                                    TNode<UintPtrT> slot_id,
                                    TNode<HeapObject> maybe_feedback_vector);
193

194 195
  // Call runtime function with |args| arguments which will return |return_size|
  // number of values.
196 197
  compiler::Node* CallRuntimeN(TNode<Uint32T> function_id,
                               TNode<Context> context,
198 199
                               const RegListNodePair& args,
                               int return_size = 1);
200

201
  // Jump forward relative to the current bytecode by the |jump_offset|.
202
  void Jump(TNode<IntPtrT> jump_offset);
203

204
  // Jump backward relative to the current bytecode by the |jump_offset|.
205
  void JumpBackward(TNode<IntPtrT> jump_offset);
206 207

  // Jump forward relative to the current bytecode by |jump_offset| if the
208
  // word values |lhs| and |rhs| are equal.
209 210
  void JumpIfTaggedEqual(TNode<Object> lhs, TNode<Object> rhs,
                         TNode<IntPtrT> jump_offset);
211

212
  // Jump forward relative to the current bytecode by |jump_offset| if the
213
  // word values |lhs| and |rhs| are not equal.
214 215
  void JumpIfTaggedNotEqual(TNode<Object> lhs, TNode<Object> rhs,
                            TNode<IntPtrT> jump_offset);
216

217 218
  // Updates the profiler interrupt budget for a return.
  void UpdateInterruptBudgetOnReturn();
219

220
  // Returns the OSR nesting level from the bytecode header.
221
  TNode<Int8T> LoadOsrNestingLevel();
222

223
  // Dispatch to the bytecode.
224
  void Dispatch();
225

226 227 228
  // Dispatch bytecode as wide operand variant.
  void DispatchWide(OperandScale operand_scale);

229 230
  // Dispatch to |target_bytecode| at |new_bytecode_offset|.
  // |target_bytecode| should be equivalent to loading from the offset.
231 232
  void DispatchToBytecode(TNode<WordT> target_bytecode,
                          TNode<IntPtrT> new_bytecode_offset);
233

234 235
  // Abort with the given abort reason.
  void Abort(AbortReason abort_reason);
236
  void AbortIfWordNotEqual(TNode<WordT> lhs, TNode<WordT> rhs,
237
                           AbortReason abort_reason);
238
  // Abort if |register_count| is invalid for given register file array.
239
  void AbortIfRegisterCountInvalid(
240 241
      TNode<FixedArrayBase> parameters_and_registers,
      TNode<IntPtrT> formal_parameter_count, TNode<UintPtrT> register_count);
242

243
  // Dispatch to frame dropper trampoline if necessary.
244
  void MaybeDropFrames(TNode<Context> context);
245

246
  // Returns the offset from the BytecodeArrayPointer of the current bytecode.
247
  TNode<IntPtrT> BytecodeOffset();
248

249
 protected:
250
  Bytecode bytecode() const { return bytecode_; }
251 252
  static bool TargetSupportsUnalignedAccess();

253 254
  void ToNumberOrNumeric(Object::Conversion mode);

255
 private:
256 257
  // Returns a pointer to the current function's BytecodeArray object.
  TNode<BytecodeArray> BytecodeArrayTaggedPointer();
258

259 260
  // Returns a pointer to first entry in the interpreter dispatch table.
  TNode<ExternalReference> DispatchTablePointer();
261

262 263 264
  // Returns the accumulator value without checking whether bytecode
  // uses it. This is intended to be used only in dispatch and in
  // tracing as these need to bypass accumulator use validity checks.
265
  TNode<Object> GetAccumulatorUnchecked();
266

267 268
  // Returns the frame pointer for the interpreted frame of the function being
  // interpreted.
269
  TNode<RawPtrT> GetInterpretedFramePointer();
270

271
  // Operations on registers.
272 273 274 275 276
  TNode<IntPtrT> RegisterLocation(Register reg);
  TNode<IntPtrT> RegisterLocation(TNode<IntPtrT> reg_index);
  TNode<IntPtrT> NextRegister(TNode<IntPtrT> reg_index);
  TNode<Object> LoadRegister(TNode<IntPtrT> reg_index);
  void StoreRegister(TNode<Object> value, TNode<IntPtrT> reg_index);
277

278 279
  // Saves and restores interpreter bytecode offset to the interpreter stack
  // frame when performing a call.
280 281
  void CallPrologue();
  void CallEpilogue();
282

283
  // Increment the dispatch counter for the (current, next) bytecode pair.
284
  void TraceBytecodeDispatch(TNode<WordT> target_bytecode);
285

286 287 288
  // Traces the current bytecode by calling |function_id|.
  void TraceBytecode(Runtime::FunctionId function_id);

289 290 291
  // Updates the bytecode array's interrupt budget by a 32-bit unsigned |weight|
  // and calls Runtime::kInterrupt if counter reaches zero. If |backward|, then
  // the interrupt budget is decremented, otherwise it is incremented.
292
  void UpdateInterruptBudget(TNode<Int32T> weight, bool backward);
293

294
  // Returns the offset of register |index| relative to RegisterFilePointer().
295
  TNode<IntPtrT> RegisterFrameOffset(TNode<IntPtrT> index);
296

297
  // Returns the offset of an operand relative to the current bytecode offset.
298
  TNode<IntPtrT> OperandOffset(int operand_index);
299 300 301 302 303 304

  // Returns a value built from an sequence of bytes in the bytecode
  // array starting at |relative_offset| from the current bytecode.
  // The |result_type| determines the size and signedness.  of the
  // value read. This method should only be used on architectures that
  // do not support unaligned memory accesses.
305
  TNode<Word32T> BytecodeOperandReadUnaligned(
306
      int relative_offset, MachineType result_type,
307
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
308 309

  // Returns zero- or sign-extended to word32 value of the operand.
310
  TNode<Uint8T> BytecodeOperandUnsignedByte(
311
      int operand_index,
312
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
313
  TNode<Int8T> BytecodeOperandSignedByte(
314
      int operand_index,
315
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
316
  TNode<Uint16T> BytecodeOperandUnsignedShort(
317
      int operand_index,
318
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
319
  TNode<Int16T> BytecodeOperandSignedShort(
320
      int operand_index,
321
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
322
  TNode<Uint32T> BytecodeOperandUnsignedQuad(
323
      int operand_index,
324
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
325
  TNode<Int32T> BytecodeOperandSignedQuad(
326
      int operand_index,
327
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
328

329 330
  // Returns zero- or sign-extended to word32 value of the operand of
  // given size.
331
  TNode<Int32T> BytecodeSignedOperand(
332
      int operand_index, OperandSize operand_size,
333
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
334
  TNode<Uint32T> BytecodeUnsignedOperand(
335
      int operand_index, OperandSize operand_size,
336
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
337

338 339 340
  // Returns the word-size sign-extended register index for bytecode operand
  // |operand_index| in the current bytecode. Value is not poisoned on
  // speculation since the value loaded from the register is poisoned instead.
341
  TNode<IntPtrT> BytecodeOperandReg(
342
      int operand_index,
343
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
344 345 346

  // Returns the word zero-extended index immediate for bytecode operand
  // |operand_index| in the current bytecode for use when loading a .
347
  TNode<UintPtrT> BytecodeOperandConstantPoolIdx(
348
      int operand_index,
349
      LoadSensitivity needs_poisoning = LoadSensitivity::kCritical);
350

351 352 353
  // Jump relative to the current bytecode by the |jump_offset|. If |backward|,
  // then jump backward (subtract the offset), otherwise jump forward (add the
  // offset). Helper function for Jump and JumpBackward.
354
  void Jump(TNode<IntPtrT> jump_offset, bool backward);
355 356

  // Jump forward relative to the current bytecode by |jump_offset| if the
357 358
  // |condition| is true. Helper function for JumpIfTaggedEqual and
  // JumpIfTaggedNotEqual.
359
  void JumpConditional(TNode<BoolT> condition, TNode<IntPtrT> jump_offset);
360

361 362
  // Save the bytecode offset to the interpreter frame.
  void SaveBytecodeOffset();
363
  // Reload the bytecode offset from the interpreter frame.
364
  TNode<IntPtrT> ReloadBytecodeOffset();
365

366 367
  // Updates and returns BytecodeOffset() advanced by the current bytecode's
  // size. Traces the exit of the current bytecode.
368
  TNode<IntPtrT> Advance();
369 370 371

  // Updates and returns BytecodeOffset() advanced by delta bytecodes.
  // Traces the exit of the current bytecode.
372
  TNode<IntPtrT> Advance(int delta);
373
  TNode<IntPtrT> Advance(TNode<IntPtrT> delta, bool backward = false);
374

375
  // Load the bytecode at |bytecode_offset|.
376
  TNode<WordT> LoadBytecode(TNode<IntPtrT> bytecode_offset);
377 378 379

  // Look ahead for Star and inline it in a branch. Returns a new target
  // bytecode node for dispatch.
380
  TNode<WordT> StarDispatchLookahead(TNode<WordT> target_bytecode);
381 382 383 384 385

  // Build code for Star at the current BytecodeOffset() and Advance() to the
  // next dispatch offset.
  void InlineStar();

386
  // Dispatch to the bytecode handler with code entry point |handler_entry|.
387 388
  void DispatchToBytecodeHandlerEntry(TNode<RawPtrT> handler_entry,
                                      TNode<IntPtrT> bytecode_offset);
389

390 391
  int CurrentBytecodeSize() const;

392 393
  OperandScale operand_scale() const { return operand_scale_; }

394
  Bytecode bytecode_;
395
  OperandScale operand_scale_;
396 397 398 399 400
  CodeStubAssembler::TVariable<RawPtrT> interpreted_frame_pointer_;
  CodeStubAssembler::TVariable<BytecodeArray> bytecode_array_;
  CodeStubAssembler::TVariable<IntPtrT> bytecode_offset_;
  CodeStubAssembler::TVariable<ExternalReference> dispatch_table_;
  CodeStubAssembler::TVariable<Object> accumulator_;
401
  AccumulatorUse accumulator_use_;
402
  bool made_call_;
403
  bool reloaded_frame_ptr_;
404
  bool bytecode_array_valid_;
405 406 407 408 409 410 411 412 413

  DISALLOW_COPY_AND_ASSIGN(InterpreterAssembler);
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_