blob: 183d4ddaefff1e11306170eea5be41dee460e64a [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 Murdoch61f157c2016-09-16 13:49:30 +010044 // Returns the intrinsic id immediate for bytecode operand
45 // |operand_index| in the current bytecode.
46 compiler::Node* BytecodeOperandIntrinsicId(int operand_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +010047
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 Murdochc5610432016-08-08 18:44:38 +010056 // 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 Murdoch097c5b22016-05-18 11:27:45 +010063 // Loads from and stores to the interpreter register file.
Ben Murdoch097c5b22016-05-18 11:27:45 +010064 compiler::Node* LoadRegister(Register reg);
65 compiler::Node* LoadRegister(compiler::Node* reg_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +010066 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 Murdoch097c5b22016-05-18 11:27:45 +010080 // 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 Murdochc5610432016-08-08 18:44:38 +0100118 compiler::Node* Jump(compiler::Node* jump_offset);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100119
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 Murdochc5610432016-08-08 18:44:38 +0100130 // Returns true if the stack guard check triggers an interrupt.
131 compiler::Node* StackCheckTriggeredInterrupt();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100132
Ben Murdochc5610432016-08-08 18:44:38 +0100133 // Updates the profiler interrupt budget for a return.
134 void UpdateInterruptBudgetOnReturn();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100135
136 // Dispatch to the bytecode.
Ben Murdochc5610432016-08-08 18:44:38 +0100137 compiler::Node* Dispatch();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100138
139 // Dispatch to bytecode handler.
Ben Murdochc5610432016-08-08 18:44:38 +0100140 compiler::Node* DispatchToBytecodeHandler(compiler::Node* handler) {
141 return DispatchToBytecodeHandler(handler, BytecodeOffset());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100142 }
143
Ben Murdochda12d292016-06-02 14:46:10 +0100144 // Dispatch bytecode as wide operand variant.
145 void DispatchWide(OperandScale operand_scale);
146
Ben Murdoch097c5b22016-05-18 11:27:45 +0100147 // Abort with the given bailout reason.
148 void Abort(BailoutReason bailout_reason);
Ben Murdochc5610432016-08-08 18:44:38 +0100149 void AbortIfWordNotEqual(compiler::Node* lhs, compiler::Node* rhs,
150 BailoutReason bailout_reason);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100151
Ben Murdoch61f157c2016-09-16 13:49:30 +0100152 // Returns the offset from the BytecodeArrayPointer of the current bytecode.
153 compiler::Node* BytecodeOffset();
154
Ben Murdoch097c5b22016-05-18 11:27:45 +0100155 protected:
Ben Murdochda12d292016-06-02 14:46:10 +0100156 Bytecode bytecode() const { return bytecode_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100157 static bool TargetSupportsUnalignedAccess();
158
159 private:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100160 // Returns a tagged pointer to the current function's BytecodeArray object.
161 compiler::Node* BytecodeArrayTaggedPointer();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100162
Ben Murdoch097c5b22016-05-18 11:27:45 +0100163 // 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 Murdoch61f157c2016-09-16 13:49:30 +0100171 // Returns the frame pointer for the interpreted frame of the function being
172 // interpreted.
173 compiler::Node* GetInterpretedFramePointer();
174
Ben Murdoch097c5b22016-05-18 11:27:45 +0100175 // 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 Murdochc5610432016-08-08 18:44:38 +0100180 // Increment the dispatch counter for the (current, next) bytecode pair.
181 void TraceBytecodeDispatch(compiler::Node* target_index);
182
Ben Murdoch097c5b22016-05-18 11:27:45 +0100183 // 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 Murdochda12d292016-06-02 14:46:10 +0100193 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100215
Ben Murdochc5610432016-08-08 18:44:38 +0100216 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100221 // 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 Murdochc5610432016-08-08 18:44:38 +0100227 compiler::Node* DispatchTo(compiler::Node* new_bytecode_offset);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100228
Ben Murdochc5610432016-08-08 18:44:38 +0100229 // 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 Murdoch097c5b22016-05-18 11:27:45 +0100236
Ben Murdochda12d292016-06-02 14:46:10 +0100237 OperandScale operand_scale() const { return operand_scale_; }
238
Ben Murdoch097c5b22016-05-18 11:27:45 +0100239 Bytecode bytecode_;
Ben Murdochda12d292016-06-02 14:46:10 +0100240 OperandScale operand_scale_;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100241 CodeStubAssembler::Variable interpreted_frame_pointer_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100242 CodeStubAssembler::Variable accumulator_;
Ben Murdochda12d292016-06-02 14:46:10 +0100243 AccumulatorUse accumulator_use_;
Ben Murdochc5610432016-08-08 18:44:38 +0100244 bool made_call_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100245
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_