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