blob: 86ecea54dd08517a1cd87890d6dd7f0f010bd46f [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"
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
16namespace v8 {
17namespace internal {
18namespace interpreter {
19
20class InterpreterAssembler : public compiler::CodeStubAssembler {
21 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
53 // Loads from and stores to the interpreter register file.
54 compiler::Node* LoadRegister(int offset);
55 compiler::Node* LoadRegister(Register reg);
56 compiler::Node* LoadRegister(compiler::Node* reg_index);
57 compiler::Node* StoreRegister(compiler::Node* value, int offset);
58 compiler::Node* StoreRegister(compiler::Node* value, Register reg);
59 compiler::Node* StoreRegister(compiler::Node* value,
60 compiler::Node* reg_index);
61
62 // Returns the next consecutive register.
63 compiler::Node* NextRegister(compiler::Node* reg_index);
64
65 // Returns the location in memory of the register |reg_index| in the
66 // interpreter register file.
67 compiler::Node* RegisterLocation(compiler::Node* reg_index);
68
69 // Load constant at |index| in the constant pool.
70 compiler::Node* LoadConstantPoolEntry(compiler::Node* index);
71
Ben Murdoch097c5b22016-05-18 11:27:45 +010072 // Load a field from an object on the heap.
73 compiler::Node* LoadObjectField(compiler::Node* object, int offset);
74
75 // Load |slot_index| from |context|.
76 compiler::Node* LoadContextSlot(compiler::Node* context, int slot_index);
77 compiler::Node* LoadContextSlot(compiler::Node* context,
78 compiler::Node* slot_index);
79 // Stores |value| into |slot_index| of |context|.
80 compiler::Node* StoreContextSlot(compiler::Node* context,
81 compiler::Node* slot_index,
82 compiler::Node* value);
83
84 // Load the TypeFeedbackVector for the current function.
85 compiler::Node* LoadTypeFeedbackVector();
86
87 // Call JSFunction or Callable |function| with |arg_count|
88 // arguments (not including receiver) and the first argument
89 // located at |first_arg|.
90 compiler::Node* CallJS(compiler::Node* function, compiler::Node* context,
91 compiler::Node* first_arg, compiler::Node* arg_count,
92 TailCallMode tail_call_mode);
93
94 // Call constructor |constructor| with |arg_count| arguments (not
95 // including receiver) and the first argument located at
96 // |first_arg|. The |new_target| is the same as the
97 // |constructor| for the new keyword, but differs for the super
98 // keyword.
99 compiler::Node* CallConstruct(compiler::Node* constructor,
100 compiler::Node* context,
101 compiler::Node* new_target,
102 compiler::Node* first_arg,
103 compiler::Node* arg_count);
104
105 // Call runtime function with |arg_count| arguments and the first argument
106 // located at |first_arg|.
107 compiler::Node* CallRuntimeN(compiler::Node* function_id,
108 compiler::Node* context,
109 compiler::Node* first_arg,
110 compiler::Node* arg_count, int return_size = 1);
111
112 // Jump relative to the current bytecode by |jump_offset|.
113 void Jump(compiler::Node* jump_offset);
114
115 // Jump relative to the current bytecode by |jump_offset| if the
116 // |condition| is true. Helper function for JumpIfWordEqual and
117 // JumpIfWordNotEqual.
118 void JumpConditional(compiler::Node* condition, compiler::Node* jump_offset);
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
130 // Perform a stack guard check.
131 void StackCheck();
132
133 // Returns from the function.
134 void InterpreterReturn();
135
136 // Dispatch to the bytecode.
137 void Dispatch();
138
139 // Dispatch to bytecode handler.
140 void DispatchToBytecodeHandler(compiler::Node* handler,
141 compiler::Node* bytecode_offset);
142 void DispatchToBytecodeHandler(compiler::Node* handler) {
143 DispatchToBytecodeHandler(handler, BytecodeOffset());
144 }
145
Ben Murdochda12d292016-06-02 14:46:10 +0100146 // Dispatch bytecode as wide operand variant.
147 void DispatchWide(OperandScale operand_scale);
148
Ben Murdoch097c5b22016-05-18 11:27:45 +0100149 // Abort with the given bailout reason.
150 void Abort(BailoutReason bailout_reason);
151
152 protected:
Ben Murdochda12d292016-06-02 14:46:10 +0100153 Bytecode bytecode() const { return bytecode_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100154 static bool TargetSupportsUnalignedAccess();
155
156 private:
157 // Returns a raw pointer to start of the register file on the stack.
158 compiler::Node* RegisterFileRawPointer();
159 // Returns a tagged pointer to the current function's BytecodeArray object.
160 compiler::Node* BytecodeArrayTaggedPointer();
161 // Returns the offset from the BytecodeArrayPointer of the current bytecode.
162 compiler::Node* BytecodeOffset();
163 // Returns a raw pointer to first entry in the interpreter dispatch table.
164 compiler::Node* DispatchTableRawPointer();
165
Ben Murdochda12d292016-06-02 14:46:10 +0100166 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100171 // Saves and restores interpreter bytecode offset to the interpreter stack
172 // frame when performing a call.
173 void CallPrologue() override;
174 void CallEpilogue() override;
175
176 // Traces the current bytecode by calling |function_id|.
177 void TraceBytecode(Runtime::FunctionId function_id);
178
179 // Updates the bytecode array's interrupt budget by |weight| and calls
180 // Runtime::kInterrupt if counter reaches zero.
181 void UpdateInterruptBudget(compiler::Node* weight);
182
183 // Returns the offset of register |index| relative to RegisterFilePointer().
184 compiler::Node* RegisterFrameOffset(compiler::Node* index);
185
Ben Murdochda12d292016-06-02 14:46:10 +0100186 // Returns the offset of an operand relative to the current bytecode offset.
187 compiler::Node* OperandOffset(int operand_index);
188
189 // Returns a value built from an sequence of bytes in the bytecode
190 // array starting at |relative_offset| from the current bytecode.
191 // The |result_type| determines the size and signedness. of the
192 // value read. This method should only be used on architectures that
193 // do not support unaligned memory accesses.
194 compiler::Node* BytecodeOperandReadUnaligned(int relative_offset,
195 MachineType result_type);
196
197 compiler::Node* BytecodeOperandUnsignedByte(int operand_index);
198 compiler::Node* BytecodeOperandSignedByte(int operand_index);
199 compiler::Node* BytecodeOperandUnsignedShort(int operand_index);
200 compiler::Node* BytecodeOperandSignedShort(int operand_index);
201 compiler::Node* BytecodeOperandUnsignedQuad(int operand_index);
202 compiler::Node* BytecodeOperandSignedQuad(int operand_index);
203
204 compiler::Node* BytecodeSignedOperand(int operand_index,
205 OperandSize operand_size);
206 compiler::Node* BytecodeUnsignedOperand(int operand_index,
207 OperandSize operand_size);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100208
209 // Returns BytecodeOffset() advanced by delta bytecodes. Note: this does not
210 // update BytecodeOffset() itself.
211 compiler::Node* Advance(int delta);
212 compiler::Node* Advance(compiler::Node* delta);
213
214 // Starts next instruction dispatch at |new_bytecode_offset|.
215 void DispatchTo(compiler::Node* new_bytecode_offset);
216
217 // Abort operations for debug code.
218 void AbortIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs,
219 BailoutReason bailout_reason);
220
Ben Murdochda12d292016-06-02 14:46:10 +0100221 OperandScale operand_scale() const { return operand_scale_; }
222
Ben Murdoch097c5b22016-05-18 11:27:45 +0100223 Bytecode bytecode_;
Ben Murdochda12d292016-06-02 14:46:10 +0100224 OperandScale operand_scale_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100225 CodeStubAssembler::Variable accumulator_;
Ben Murdochda12d292016-06-02 14:46:10 +0100226 AccumulatorUse accumulator_use_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100227 CodeStubAssembler::Variable context_;
228 CodeStubAssembler::Variable bytecode_array_;
229
230 bool disable_stack_check_across_call_;
231 compiler::Node* stack_pointer_before_call_;
232
233 DISALLOW_COPY_AND_ASSIGN(InterpreterAssembler);
234};
235
236} // namespace interpreter
237} // namespace internal
238} // namespace v8
239
240#endif // V8_INTERPRETER_INTERPRETER_ASSEMBLER_H_