blob: f8d4b7ce60e937073ac366396dcf1f135459436f [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001// 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 Murdochc5610432016-08-08 18:44:38 +010011#include "src/code-stub-assembler.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010012#include "src/frames.h"
13#include "src/interpreter/bytecodes.h"
14#include "src/runtime/runtime.h"
15
16namespace v8 {
17namespace internal {
18namespace interpreter {
19
Ben Murdochc5610432016-08-08 18:44:38 +010020class InterpreterAssembler : public CodeStubAssembler {
Ben Murdoch097c5b22016-05-18 11:27:45 +010021 public:
Ben Murdochda12d292016-06-02 14:46:10 +010022 InterpreterAssembler(Isolate* isolate, Zone* zone, Bytecode bytecode,
23 OperandScale operand_scale);
Ben Murdoch097c5b22016-05-18 11:27:45 +010024 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 Murdochda12d292016-06-02 14:46:10 +010029 // Returns the 8-bit flag for bytecode operand |operand_index| in the
30 // current bytecode.
31 compiler::Node* BytecodeOperandFlag(int operand_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +010032 // 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 Murdochda12d292016-06-02 14:46:10 +010041 // Returns the runtime id immediate for bytecode operand
42 // |operand_index| in the current bytecode.
43 compiler::Node* BytecodeOperandRuntimeId(int operand_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +010044
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 Murdochc5610432016-08-08 18:44:38 +010053 // 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 Murdoch097c5b22016-05-18 11:27:45 +010060 // Loads from and stores to the interpreter register file.
Ben Murdoch097c5b22016-05-18 11:27:45 +010061 compiler::Node* LoadRegister(Register reg);
62 compiler::Node* LoadRegister(compiler::Node* reg_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +010063 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 Murdoch097c5b22016-05-18 11:27:45 +010077 // 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 Murdochc5610432016-08-08 18:44:38 +0100115 compiler::Node* Jump(compiler::Node* jump_offset);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100116
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 Murdochc5610432016-08-08 18:44:38 +0100127 // Returns true if the stack guard check triggers an interrupt.
128 compiler::Node* StackCheckTriggeredInterrupt();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100129
Ben Murdochc5610432016-08-08 18:44:38 +0100130 // Updates the profiler interrupt budget for a return.
131 void UpdateInterruptBudgetOnReturn();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100132
133 // Dispatch to the bytecode.
Ben Murdochc5610432016-08-08 18:44:38 +0100134 compiler::Node* Dispatch();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100135
136 // Dispatch to bytecode handler.
Ben Murdochc5610432016-08-08 18:44:38 +0100137 compiler::Node* DispatchToBytecodeHandler(compiler::Node* handler) {
138 return DispatchToBytecodeHandler(handler, BytecodeOffset());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100139 }
140
Ben Murdochda12d292016-06-02 14:46:10 +0100141 // Dispatch bytecode as wide operand variant.
142 void DispatchWide(OperandScale operand_scale);
143
Ben Murdoch097c5b22016-05-18 11:27:45 +0100144 // Abort with the given bailout reason.
145 void Abort(BailoutReason bailout_reason);
Ben Murdochc5610432016-08-08 18:44:38 +0100146 void AbortIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs,
147 BailoutReason bailout_reason);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100148
149 protected:
Ben Murdochda12d292016-06-02 14:46:10 +0100150 Bytecode bytecode() const { return bytecode_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100151 static bool TargetSupportsUnalignedAccess();
152
153 private:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100154 // 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 Murdochda12d292016-06-02 14:46:10 +0100161 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100166 // 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 Murdochc5610432016-08-08 18:44:38 +0100171 // Increment the dispatch counter for the (current, next) bytecode pair.
172 void TraceBytecodeDispatch(compiler::Node* target_index);
173
Ben Murdoch097c5b22016-05-18 11:27:45 +0100174 // 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 Murdochda12d292016-06-02 14:46:10 +0100184 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100206
Ben Murdochc5610432016-08-08 18:44:38 +0100207 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100212 // 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 Murdochc5610432016-08-08 18:44:38 +0100218 compiler::Node* DispatchTo(compiler::Node* new_bytecode_offset);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100219
Ben Murdochc5610432016-08-08 18:44:38 +0100220 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100227
Ben Murdochda12d292016-06-02 14:46:10 +0100228 OperandScale operand_scale() const { return operand_scale_; }
229
Ben Murdoch097c5b22016-05-18 11:27:45 +0100230 Bytecode bytecode_;
Ben Murdochda12d292016-06-02 14:46:10 +0100231 OperandScale operand_scale_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100232 CodeStubAssembler::Variable accumulator_;
Ben Murdochda12d292016-06-02 14:46:10 +0100233 AccumulatorUse accumulator_use_;
Ben Murdochc5610432016-08-08 18:44:38 +0100234 bool made_call_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100235
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_