67ab9cc2a9da4444105ad6aa4f5a6f66be847652
[platform/upstream/v8.git] / src / compiler / interpreter-assembler.h
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_COMPILER_INTERPRETER_ASSEMBLER_H_
6 #define V8_COMPILER_INTERPRETER_ASSEMBLER_H_
7
8 // Clients of this interface shouldn't depend on lots of compiler internals.
9 // Do not include anything from src/compiler here!
10 #include "src/allocation.h"
11 #include "src/base/smart-pointers.h"
12 #include "src/builtins.h"
13 #include "src/frames.h"
14 #include "src/interpreter/bytecodes.h"
15 #include "src/runtime/runtime.h"
16 #include "src/zone-containers.h"
17
18 namespace v8 {
19 namespace internal {
20
21 class CallInterfaceDescriptor;
22 class Isolate;
23 class Zone;
24
25 namespace compiler {
26
27 class CallDescriptor;
28 class Graph;
29 class Node;
30 class Operator;
31 class RawMachineAssembler;
32 class Schedule;
33
34 class InterpreterAssembler {
35  public:
36   InterpreterAssembler(Isolate* isolate, Zone* zone,
37                        interpreter::Bytecode bytecode);
38   virtual ~InterpreterAssembler();
39
40   Handle<Code> GenerateCode();
41
42   // Returns the count immediate for bytecode operand |operand_index| in the
43   // current bytecode.
44   Node* BytecodeOperandCount(int operand_index);
45   // Returns the index immediate for bytecode operand |operand_index| in the
46   // current bytecode.
47   Node* BytecodeOperandIdx(int operand_index);
48   // Returns the Imm8 immediate for bytecode operand |operand_index| in the
49   // current bytecode.
50   Node* BytecodeOperandImm8(int operand_index);
51   // Returns the register index for bytecode operand |operand_index| in the
52   // current bytecode.
53   Node* BytecodeOperandReg(int operand_index);
54
55   // Accumulator.
56   Node* GetAccumulator();
57   void SetAccumulator(Node* value);
58
59   // Loads from and stores to the interpreter register file.
60   Node* LoadRegister(Node* reg_index);
61   Node* StoreRegister(Node* value, Node* reg_index);
62
63   // Returns the location in memory of the register |reg_index| in the
64   // interpreter register file.
65   Node* RegisterLocation(Node* reg_index);
66
67   // Constants.
68   Node* Int32Constant(int value);
69   Node* IntPtrConstant(intptr_t value);
70   Node* NumberConstant(double value);
71   Node* HeapConstant(Handle<HeapObject> object);
72   Node* BooleanConstant(bool value);
73
74   // Tag and untag Smi values.
75   Node* SmiTag(Node* value);
76   Node* SmiUntag(Node* value);
77
78   // Basic arithmetic operations.
79   Node* IntPtrAdd(Node* a, Node* b);
80   Node* IntPtrSub(Node* a, Node* b);
81   Node* WordShl(Node* value, int shift);
82
83   // Load constant at |index| in the constant pool.
84   Node* LoadConstantPoolEntry(Node* index);
85
86   // Load a field from an object on the heap.
87   Node* LoadObjectField(Node* object, int offset);
88
89   // Load |slot_index| from a context.
90   Node* LoadContextSlot(Node* context, int slot_index);
91
92   // Load |slot_index| from the current context.
93   Node* LoadContextSlot(int slot_index);
94
95   // Load the TypeFeedbackVector for the current function.
96   Node* LoadTypeFeedbackVector();
97
98   // Call JSFunction or Callable |function| with |arg_count| (not including
99   // receiver) and the first argument located at |first_arg|.
100   Node* CallJS(Node* function, Node* first_arg, Node* arg_count);
101
102   // Call an IC code stub.
103   Node* CallIC(CallInterfaceDescriptor descriptor, Node* target, Node* arg1,
104                Node* arg2, Node* arg3, Node* arg4);
105   Node* CallIC(CallInterfaceDescriptor descriptor, Node* target, Node* arg1,
106                Node* arg2, Node* arg3, Node* arg4, Node* arg5);
107
108   // Call runtime function.
109   Node* CallRuntime(Runtime::FunctionId function_id, Node* arg1);
110   Node* CallRuntime(Runtime::FunctionId function_id, Node* arg1, Node* arg2);
111
112   // Jump relative to the current bytecode by |jump_offset|.
113   void Jump(Node* jump_offset);
114
115   // Jump relative to the current bytecode by |jump_offset| if the
116   // word values |lhs| and |rhs| are equal.
117   void JumpIfWordEqual(Node* lhs, Node* rhs, Node* jump_offset);
118
119   // Returns from the function.
120   void Return();
121
122   // Dispatch to the bytecode.
123   void Dispatch();
124
125  protected:
126   // Close the graph.
127   void End();
128
129   // Protected helpers (for testing) which delegate to RawMachineAssembler.
130   CallDescriptor* call_descriptor() const;
131   Graph* graph();
132
133  private:
134   // Returns a raw pointer to start of the register file on the stack.
135   Node* RegisterFileRawPointer();
136   // Returns a tagged pointer to the current function's BytecodeArray object.
137   Node* BytecodeArrayTaggedPointer();
138   // Returns the offset from the BytecodeArrayPointer of the current bytecode.
139   Node* BytecodeOffset();
140   // Returns a raw pointer to first entry in the interpreter dispatch table.
141   Node* DispatchTableRawPointer();
142   // Returns a tagged pointer to the current context.
143   Node* ContextTaggedPointer();
144
145   // Returns the offset of register |index| relative to RegisterFilePointer().
146   Node* RegisterFrameOffset(Node* index);
147
148   Node* SmiShiftBitsConstant();
149   Node* BytecodeOperand(int operand_index);
150   Node* BytecodeOperandSignExtended(int operand_index);
151
152   Node* CallIC(CallInterfaceDescriptor descriptor, Node* target, Node** args);
153   Node* CallJSBuiltin(int context_index, Node* receiver, Node** js_args,
154                       int js_arg_count);
155
156   // Returns BytecodeOffset() advanced by delta bytecodes. Note: this does not
157   // update BytecodeOffset() itself.
158   Node* Advance(int delta);
159   Node* Advance(Node* delta);
160
161   // Starts next instruction dispatch at |new_bytecode_offset|.
162   void DispatchTo(Node* new_bytecode_offset);
163
164   // Adds an end node of the graph.
165   void AddEndInput(Node* input);
166
167   // Private helpers which delegate to RawMachineAssembler.
168   Isolate* isolate();
169   Schedule* schedule();
170   Zone* zone();
171
172   interpreter::Bytecode bytecode_;
173   base::SmartPointer<RawMachineAssembler> raw_assembler_;
174   ZoneVector<Node*> end_nodes_;
175   Node* accumulator_;
176   bool code_generated_;
177
178   DISALLOW_COPY_AND_ASSIGN(InterpreterAssembler);
179 };
180
181 }  // namespace interpreter
182 }  // namespace internal
183 }  // namespace v8
184
185 #endif  // V8_COMPILER_INTERPRETER_ASSEMBLER_H_