Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 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/base/smart-pointers.h" |
| 10 | #include "src/builtins.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 11 | #include "src/code-stub-assembler.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12 | #include "src/frames.h" |
| 13 | #include "src/interpreter/bytecodes.h" |
| 14 | #include "src/runtime/runtime.h" |
| 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | namespace interpreter { |
| 19 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 20 | class InterpreterAssembler : public CodeStubAssembler { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 21 | public: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 22 | InterpreterAssembler(Isolate* isolate, Zone* zone, Bytecode bytecode, |
| 23 | OperandScale operand_scale); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 24 | virtual ~InterpreterAssembler(); |
| 25 | |
| 26 | // Returns the count immediate for bytecode operand |operand_index| in the |
| 27 | // current bytecode. |
| 28 | compiler::Node* BytecodeOperandCount(int operand_index); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 29 | // Returns the 8-bit flag for bytecode operand |operand_index| in the |
| 30 | // current bytecode. |
| 31 | compiler::Node* BytecodeOperandFlag(int operand_index); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 32 | // Returns the index immediate for bytecode operand |operand_index| in the |
| 33 | // current bytecode. |
| 34 | compiler::Node* BytecodeOperandIdx(int operand_index); |
| 35 | // Returns the Imm8 immediate for bytecode operand |operand_index| in the |
| 36 | // current bytecode. |
| 37 | compiler::Node* BytecodeOperandImm(int operand_index); |
| 38 | // Returns the register index for bytecode operand |operand_index| in the |
| 39 | // current bytecode. |
| 40 | compiler::Node* BytecodeOperandReg(int operand_index); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 41 | // Returns the runtime id immediate for bytecode operand |
| 42 | // |operand_index| in the current bytecode. |
| 43 | compiler::Node* BytecodeOperandRuntimeId(int operand_index); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 44 | |
| 45 | // Accumulator. |
| 46 | compiler::Node* GetAccumulator(); |
| 47 | void SetAccumulator(compiler::Node* value); |
| 48 | |
| 49 | // Context. |
| 50 | compiler::Node* GetContext(); |
| 51 | void SetContext(compiler::Node* value); |
| 52 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 53 | // Number of registers. |
| 54 | compiler::Node* RegisterCount(); |
| 55 | |
| 56 | // Backup/restore register file to/from a fixed array of the correct length. |
| 57 | compiler::Node* ExportRegisterFile(compiler::Node* array); |
| 58 | compiler::Node* ImportRegisterFile(compiler::Node* array); |
| 59 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 60 | // Loads from and stores to the interpreter register file. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 61 | compiler::Node* LoadRegister(Register reg); |
| 62 | compiler::Node* LoadRegister(compiler::Node* reg_index); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 63 | compiler::Node* StoreRegister(compiler::Node* value, Register reg); |
| 64 | compiler::Node* StoreRegister(compiler::Node* value, |
| 65 | compiler::Node* reg_index); |
| 66 | |
| 67 | // Returns the next consecutive register. |
| 68 | compiler::Node* NextRegister(compiler::Node* reg_index); |
| 69 | |
| 70 | // Returns the location in memory of the register |reg_index| in the |
| 71 | // interpreter register file. |
| 72 | compiler::Node* RegisterLocation(compiler::Node* reg_index); |
| 73 | |
| 74 | // Load constant at |index| in the constant pool. |
| 75 | compiler::Node* LoadConstantPoolEntry(compiler::Node* index); |
| 76 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 77 | // Load |slot_index| from |context|. |
| 78 | compiler::Node* LoadContextSlot(compiler::Node* context, int slot_index); |
| 79 | compiler::Node* LoadContextSlot(compiler::Node* context, |
| 80 | compiler::Node* slot_index); |
| 81 | // Stores |value| into |slot_index| of |context|. |
| 82 | compiler::Node* StoreContextSlot(compiler::Node* context, |
| 83 | compiler::Node* slot_index, |
| 84 | compiler::Node* value); |
| 85 | |
| 86 | // Load the TypeFeedbackVector for the current function. |
| 87 | compiler::Node* LoadTypeFeedbackVector(); |
| 88 | |
| 89 | // Call JSFunction or Callable |function| with |arg_count| |
| 90 | // arguments (not including receiver) and the first argument |
| 91 | // located at |first_arg|. |
| 92 | compiler::Node* CallJS(compiler::Node* function, compiler::Node* context, |
| 93 | compiler::Node* first_arg, compiler::Node* arg_count, |
| 94 | TailCallMode tail_call_mode); |
| 95 | |
| 96 | // Call constructor |constructor| with |arg_count| arguments (not |
| 97 | // including receiver) and the first argument located at |
| 98 | // |first_arg|. The |new_target| is the same as the |
| 99 | // |constructor| for the new keyword, but differs for the super |
| 100 | // keyword. |
| 101 | compiler::Node* CallConstruct(compiler::Node* constructor, |
| 102 | compiler::Node* context, |
| 103 | compiler::Node* new_target, |
| 104 | compiler::Node* first_arg, |
| 105 | compiler::Node* arg_count); |
| 106 | |
| 107 | // Call runtime function with |arg_count| arguments and the first argument |
| 108 | // located at |first_arg|. |
| 109 | compiler::Node* CallRuntimeN(compiler::Node* function_id, |
| 110 | compiler::Node* context, |
| 111 | compiler::Node* first_arg, |
| 112 | compiler::Node* arg_count, int return_size = 1); |
| 113 | |
| 114 | // Jump relative to the current bytecode by |jump_offset|. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 115 | compiler::Node* Jump(compiler::Node* jump_offset); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 116 | |
| 117 | // Jump relative to the current bytecode by |jump_offset| if the |
| 118 | // word values |lhs| and |rhs| are equal. |
| 119 | void JumpIfWordEqual(compiler::Node* lhs, compiler::Node* rhs, |
| 120 | compiler::Node* jump_offset); |
| 121 | |
| 122 | // Jump relative to the current bytecode by |jump_offset| if the |
| 123 | // word values |lhs| and |rhs| are not equal. |
| 124 | void JumpIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs, |
| 125 | compiler::Node* jump_offset); |
| 126 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 127 | // Returns true if the stack guard check triggers an interrupt. |
| 128 | compiler::Node* StackCheckTriggeredInterrupt(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 129 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 130 | // Updates the profiler interrupt budget for a return. |
| 131 | void UpdateInterruptBudgetOnReturn(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 132 | |
| 133 | // Dispatch to the bytecode. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 134 | compiler::Node* Dispatch(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 135 | |
| 136 | // Dispatch to bytecode handler. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 137 | compiler::Node* DispatchToBytecodeHandler(compiler::Node* handler) { |
| 138 | return DispatchToBytecodeHandler(handler, BytecodeOffset()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 141 | // Dispatch bytecode as wide operand variant. |
| 142 | void DispatchWide(OperandScale operand_scale); |
| 143 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 144 | // Abort with the given bailout reason. |
| 145 | void Abort(BailoutReason bailout_reason); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 146 | void AbortIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs, |
| 147 | BailoutReason bailout_reason); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 148 | |
| 149 | protected: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 150 | Bytecode bytecode() const { return bytecode_; } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 151 | static bool TargetSupportsUnalignedAccess(); |
| 152 | |
| 153 | private: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 154 | // Returns a tagged pointer to the current function's BytecodeArray object. |
| 155 | compiler::Node* BytecodeArrayTaggedPointer(); |
| 156 | // Returns the offset from the BytecodeArrayPointer of the current bytecode. |
| 157 | compiler::Node* BytecodeOffset(); |
| 158 | // Returns a raw pointer to first entry in the interpreter dispatch table. |
| 159 | compiler::Node* DispatchTableRawPointer(); |
| 160 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 161 | // Returns the accumulator value without checking whether bytecode |
| 162 | // uses it. This is intended to be used only in dispatch and in |
| 163 | // tracing as these need to bypass accumulator use validity checks. |
| 164 | compiler::Node* GetAccumulatorUnchecked(); |
| 165 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 166 | // Saves and restores interpreter bytecode offset to the interpreter stack |
| 167 | // frame when performing a call. |
| 168 | void CallPrologue() override; |
| 169 | void CallEpilogue() override; |
| 170 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 171 | // Increment the dispatch counter for the (current, next) bytecode pair. |
| 172 | void TraceBytecodeDispatch(compiler::Node* target_index); |
| 173 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 174 | // Traces the current bytecode by calling |function_id|. |
| 175 | void TraceBytecode(Runtime::FunctionId function_id); |
| 176 | |
| 177 | // Updates the bytecode array's interrupt budget by |weight| and calls |
| 178 | // Runtime::kInterrupt if counter reaches zero. |
| 179 | void UpdateInterruptBudget(compiler::Node* weight); |
| 180 | |
| 181 | // Returns the offset of register |index| relative to RegisterFilePointer(). |
| 182 | compiler::Node* RegisterFrameOffset(compiler::Node* index); |
| 183 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 184 | // Returns the offset of an operand relative to the current bytecode offset. |
| 185 | compiler::Node* OperandOffset(int operand_index); |
| 186 | |
| 187 | // Returns a value built from an sequence of bytes in the bytecode |
| 188 | // array starting at |relative_offset| from the current bytecode. |
| 189 | // The |result_type| determines the size and signedness. of the |
| 190 | // value read. This method should only be used on architectures that |
| 191 | // do not support unaligned memory accesses. |
| 192 | compiler::Node* BytecodeOperandReadUnaligned(int relative_offset, |
| 193 | MachineType result_type); |
| 194 | |
| 195 | compiler::Node* BytecodeOperandUnsignedByte(int operand_index); |
| 196 | compiler::Node* BytecodeOperandSignedByte(int operand_index); |
| 197 | compiler::Node* BytecodeOperandUnsignedShort(int operand_index); |
| 198 | compiler::Node* BytecodeOperandSignedShort(int operand_index); |
| 199 | compiler::Node* BytecodeOperandUnsignedQuad(int operand_index); |
| 200 | compiler::Node* BytecodeOperandSignedQuad(int operand_index); |
| 201 | |
| 202 | compiler::Node* BytecodeSignedOperand(int operand_index, |
| 203 | OperandSize operand_size); |
| 204 | compiler::Node* BytecodeUnsignedOperand(int operand_index, |
| 205 | OperandSize operand_size); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 206 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 207 | // Jump relative to the current bytecode by |jump_offset| if the |
| 208 | // |condition| is true. Helper function for JumpIfWordEqual and |
| 209 | // JumpIfWordNotEqual. |
| 210 | void JumpConditional(compiler::Node* condition, compiler::Node* jump_offset); |
| 211 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 212 | // Returns BytecodeOffset() advanced by delta bytecodes. Note: this does not |
| 213 | // update BytecodeOffset() itself. |
| 214 | compiler::Node* Advance(int delta); |
| 215 | compiler::Node* Advance(compiler::Node* delta); |
| 216 | |
| 217 | // Starts next instruction dispatch at |new_bytecode_offset|. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 218 | compiler::Node* DispatchTo(compiler::Node* new_bytecode_offset); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 219 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 220 | // Dispatch to the bytecode handler with code offset |handler|. |
| 221 | compiler::Node* DispatchToBytecodeHandler(compiler::Node* handler, |
| 222 | compiler::Node* bytecode_offset); |
| 223 | |
| 224 | // Dispatch to the bytecode handler with code entry point |handler_entry|. |
| 225 | compiler::Node* DispatchToBytecodeHandlerEntry( |
| 226 | compiler::Node* handler_entry, compiler::Node* bytecode_offset); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 227 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 228 | OperandScale operand_scale() const { return operand_scale_; } |
| 229 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 230 | Bytecode bytecode_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 231 | OperandScale operand_scale_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 232 | CodeStubAssembler::Variable accumulator_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 233 | AccumulatorUse accumulator_use_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 234 | bool made_call_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 235 | |
| 236 | bool disable_stack_check_across_call_; |
| 237 | compiler::Node* stack_pointer_before_call_; |
| 238 | |
| 239 | DISALLOW_COPY_AND_ASSIGN(InterpreterAssembler); |
| 240 | }; |
| 241 | |
| 242 | } // namespace interpreter |
| 243 | } // namespace internal |
| 244 | } // namespace v8 |
| 245 | |
| 246 | #endif // V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_ |