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" |
| 11 | #include "src/compiler/code-stub-assembler.h" |
| 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 | |
| 20 | class InterpreterAssembler : public compiler::CodeStubAssembler { |
| 21 | public: |
| 22 | InterpreterAssembler(Isolate* isolate, Zone* zone, Bytecode bytecode); |
| 23 | virtual ~InterpreterAssembler(); |
| 24 | |
| 25 | // Returns the count immediate for bytecode operand |operand_index| in the |
| 26 | // current bytecode. |
| 27 | compiler::Node* BytecodeOperandCount(int operand_index); |
| 28 | // Returns the index immediate for bytecode operand |operand_index| in the |
| 29 | // current bytecode. |
| 30 | compiler::Node* BytecodeOperandIdx(int operand_index); |
| 31 | // Returns the Imm8 immediate for bytecode operand |operand_index| in the |
| 32 | // current bytecode. |
| 33 | compiler::Node* BytecodeOperandImm(int operand_index); |
| 34 | // Returns the register index for bytecode operand |operand_index| in the |
| 35 | // current bytecode. |
| 36 | compiler::Node* BytecodeOperandReg(int operand_index); |
| 37 | |
| 38 | // Accumulator. |
| 39 | compiler::Node* GetAccumulator(); |
| 40 | void SetAccumulator(compiler::Node* value); |
| 41 | |
| 42 | // Context. |
| 43 | compiler::Node* GetContext(); |
| 44 | void SetContext(compiler::Node* value); |
| 45 | |
| 46 | // Loads from and stores to the interpreter register file. |
| 47 | compiler::Node* LoadRegister(int offset); |
| 48 | compiler::Node* LoadRegister(Register reg); |
| 49 | compiler::Node* LoadRegister(compiler::Node* reg_index); |
| 50 | compiler::Node* StoreRegister(compiler::Node* value, int offset); |
| 51 | compiler::Node* StoreRegister(compiler::Node* value, Register reg); |
| 52 | compiler::Node* StoreRegister(compiler::Node* value, |
| 53 | compiler::Node* reg_index); |
| 54 | |
| 55 | // Returns the next consecutive register. |
| 56 | compiler::Node* NextRegister(compiler::Node* reg_index); |
| 57 | |
| 58 | // Returns the location in memory of the register |reg_index| in the |
| 59 | // interpreter register file. |
| 60 | compiler::Node* RegisterLocation(compiler::Node* reg_index); |
| 61 | |
| 62 | // Load constant at |index| in the constant pool. |
| 63 | compiler::Node* LoadConstantPoolEntry(compiler::Node* index); |
| 64 | |
| 65 | // Load an element from a fixed array on the heap. |
| 66 | compiler::Node* LoadFixedArrayElement(compiler::Node* fixed_array, int index); |
| 67 | |
| 68 | // Load a field from an object on the heap. |
| 69 | compiler::Node* LoadObjectField(compiler::Node* object, int offset); |
| 70 | |
| 71 | // Load |slot_index| from |context|. |
| 72 | compiler::Node* LoadContextSlot(compiler::Node* context, int slot_index); |
| 73 | compiler::Node* LoadContextSlot(compiler::Node* context, |
| 74 | compiler::Node* slot_index); |
| 75 | // Stores |value| into |slot_index| of |context|. |
| 76 | compiler::Node* StoreContextSlot(compiler::Node* context, |
| 77 | compiler::Node* slot_index, |
| 78 | compiler::Node* value); |
| 79 | |
| 80 | // Load the TypeFeedbackVector for the current function. |
| 81 | compiler::Node* LoadTypeFeedbackVector(); |
| 82 | |
| 83 | // Call JSFunction or Callable |function| with |arg_count| |
| 84 | // arguments (not including receiver) and the first argument |
| 85 | // located at |first_arg|. |
| 86 | compiler::Node* CallJS(compiler::Node* function, compiler::Node* context, |
| 87 | compiler::Node* first_arg, compiler::Node* arg_count, |
| 88 | TailCallMode tail_call_mode); |
| 89 | |
| 90 | // Call constructor |constructor| with |arg_count| arguments (not |
| 91 | // including receiver) and the first argument located at |
| 92 | // |first_arg|. The |new_target| is the same as the |
| 93 | // |constructor| for the new keyword, but differs for the super |
| 94 | // keyword. |
| 95 | compiler::Node* CallConstruct(compiler::Node* constructor, |
| 96 | compiler::Node* context, |
| 97 | compiler::Node* new_target, |
| 98 | compiler::Node* first_arg, |
| 99 | compiler::Node* arg_count); |
| 100 | |
| 101 | // Call runtime function with |arg_count| arguments and the first argument |
| 102 | // located at |first_arg|. |
| 103 | compiler::Node* CallRuntimeN(compiler::Node* function_id, |
| 104 | compiler::Node* context, |
| 105 | compiler::Node* first_arg, |
| 106 | compiler::Node* arg_count, int return_size = 1); |
| 107 | |
| 108 | // Jump relative to the current bytecode by |jump_offset|. |
| 109 | void Jump(compiler::Node* jump_offset); |
| 110 | |
| 111 | // Jump relative to the current bytecode by |jump_offset| if the |
| 112 | // |condition| is true. Helper function for JumpIfWordEqual and |
| 113 | // JumpIfWordNotEqual. |
| 114 | void JumpConditional(compiler::Node* condition, compiler::Node* jump_offset); |
| 115 | |
| 116 | // Jump relative to the current bytecode by |jump_offset| if the |
| 117 | // word values |lhs| and |rhs| are equal. |
| 118 | void JumpIfWordEqual(compiler::Node* lhs, compiler::Node* rhs, |
| 119 | compiler::Node* jump_offset); |
| 120 | |
| 121 | // Jump relative to the current bytecode by |jump_offset| if the |
| 122 | // word values |lhs| and |rhs| are not equal. |
| 123 | void JumpIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs, |
| 124 | compiler::Node* jump_offset); |
| 125 | |
| 126 | // Perform a stack guard check. |
| 127 | void StackCheck(); |
| 128 | |
| 129 | // Returns from the function. |
| 130 | void InterpreterReturn(); |
| 131 | |
| 132 | // Dispatch to the bytecode. |
| 133 | void Dispatch(); |
| 134 | |
| 135 | // Dispatch to bytecode handler. |
| 136 | void DispatchToBytecodeHandler(compiler::Node* handler, |
| 137 | compiler::Node* bytecode_offset); |
| 138 | void DispatchToBytecodeHandler(compiler::Node* handler) { |
| 139 | DispatchToBytecodeHandler(handler, BytecodeOffset()); |
| 140 | } |
| 141 | |
| 142 | // Abort with the given bailout reason. |
| 143 | void Abort(BailoutReason bailout_reason); |
| 144 | |
| 145 | protected: |
| 146 | static bool TargetSupportsUnalignedAccess(); |
| 147 | |
| 148 | private: |
| 149 | // Returns a raw pointer to start of the register file on the stack. |
| 150 | compiler::Node* RegisterFileRawPointer(); |
| 151 | // Returns a tagged pointer to the current function's BytecodeArray object. |
| 152 | compiler::Node* BytecodeArrayTaggedPointer(); |
| 153 | // Returns the offset from the BytecodeArrayPointer of the current bytecode. |
| 154 | compiler::Node* BytecodeOffset(); |
| 155 | // Returns a raw pointer to first entry in the interpreter dispatch table. |
| 156 | compiler::Node* DispatchTableRawPointer(); |
| 157 | |
| 158 | // Saves and restores interpreter bytecode offset to the interpreter stack |
| 159 | // frame when performing a call. |
| 160 | void CallPrologue() override; |
| 161 | void CallEpilogue() override; |
| 162 | |
| 163 | // Traces the current bytecode by calling |function_id|. |
| 164 | void TraceBytecode(Runtime::FunctionId function_id); |
| 165 | |
| 166 | // Updates the bytecode array's interrupt budget by |weight| and calls |
| 167 | // Runtime::kInterrupt if counter reaches zero. |
| 168 | void UpdateInterruptBudget(compiler::Node* weight); |
| 169 | |
| 170 | // Returns the offset of register |index| relative to RegisterFilePointer(). |
| 171 | compiler::Node* RegisterFrameOffset(compiler::Node* index); |
| 172 | |
| 173 | compiler::Node* BytecodeOperand(int operand_index); |
| 174 | compiler::Node* BytecodeOperandSignExtended(int operand_index); |
| 175 | compiler::Node* BytecodeOperandShort(int operand_index); |
| 176 | compiler::Node* BytecodeOperandShortSignExtended(int operand_index); |
| 177 | |
| 178 | // Returns BytecodeOffset() advanced by delta bytecodes. Note: this does not |
| 179 | // update BytecodeOffset() itself. |
| 180 | compiler::Node* Advance(int delta); |
| 181 | compiler::Node* Advance(compiler::Node* delta); |
| 182 | |
| 183 | // Starts next instruction dispatch at |new_bytecode_offset|. |
| 184 | void DispatchTo(compiler::Node* new_bytecode_offset); |
| 185 | |
| 186 | // Abort operations for debug code. |
| 187 | void AbortIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs, |
| 188 | BailoutReason bailout_reason); |
| 189 | |
| 190 | Bytecode bytecode_; |
| 191 | CodeStubAssembler::Variable accumulator_; |
| 192 | CodeStubAssembler::Variable context_; |
| 193 | CodeStubAssembler::Variable bytecode_array_; |
| 194 | |
| 195 | bool disable_stack_check_across_call_; |
| 196 | compiler::Node* stack_pointer_before_call_; |
| 197 | |
| 198 | DISALLOW_COPY_AND_ASSIGN(InterpreterAssembler); |
| 199 | }; |
| 200 | |
| 201 | } // namespace interpreter |
| 202 | } // namespace internal |
| 203 | } // namespace v8 |
| 204 | |
| 205 | #endif // V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_ |