OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "src/ast.h" | 10 #include "src/ast.h" |
11 #include "src/identity-map.h" | 11 #include "src/identity-map.h" |
12 #include "src/interpreter/bytecodes.h" | 12 #include "src/interpreter/bytecodes.h" |
13 #include "src/zone.h" | 13 #include "src/zone.h" |
14 #include "src/zone-containers.h" | 14 #include "src/zone-containers.h" |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 class Isolate; | 19 class Isolate; |
20 | 20 |
21 namespace interpreter { | 21 namespace interpreter { |
22 | 22 |
23 class BytecodeLabel; | |
23 class Register; | 24 class Register; |
24 | 25 |
25 class BytecodeArrayBuilder { | 26 class BytecodeArrayBuilder { |
26 public: | 27 public: |
27 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); | 28 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); |
28 Handle<BytecodeArray> ToBytecodeArray(); | 29 Handle<BytecodeArray> ToBytecodeArray(); |
29 | 30 |
30 // Set number of parameters expected by function. | 31 // Set number of parameters expected by function. |
31 void set_parameter_count(int number_of_params); | 32 void set_parameter_count(int number_of_params); |
32 int parameter_count() const; | 33 int parameter_count() const; |
33 | 34 |
34 // Set number of locals required for bytecode array. | 35 // Set number of locals required for bytecode array. |
35 void set_locals_count(int number_of_locals); | 36 void set_locals_count(int number_of_locals); |
36 int locals_count() const; | 37 int locals_count() const; |
37 | 38 |
38 // Returns true if the bytecode has an explicit return at the end. | |
39 bool HasExplicitReturn(); | |
40 | |
41 Register Parameter(int parameter_index); | 39 Register Parameter(int parameter_index); |
42 | 40 |
43 // Constant loads to accumulator. | 41 // Constant loads to accumulator. |
44 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); | 42 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); |
45 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); | 43 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); |
46 BytecodeArrayBuilder& LoadUndefined(); | 44 BytecodeArrayBuilder& LoadUndefined(); |
47 BytecodeArrayBuilder& LoadNull(); | 45 BytecodeArrayBuilder& LoadNull(); |
48 BytecodeArrayBuilder& LoadTheHole(); | 46 BytecodeArrayBuilder& LoadTheHole(); |
49 BytecodeArrayBuilder& LoadTrue(); | 47 BytecodeArrayBuilder& LoadTrue(); |
50 BytecodeArrayBuilder& LoadFalse(); | 48 BytecodeArrayBuilder& LoadFalse(); |
(...skipping 16 matching lines...) Expand all Loading... | |
67 int feedback_slot, | 65 int feedback_slot, |
68 LanguageMode language_mode); | 66 LanguageMode language_mode); |
69 | 67 |
70 // Call a JS function. The JSFunction or Callable to be called should be in | 68 // Call a JS function. The JSFunction or Callable to be called should be in |
71 // |callable|, the receiver should be in |receiver| and all subsequent | 69 // |callable|, the receiver should be in |receiver| and all subsequent |
72 // arguments should be in registers <receiver + 1> to | 70 // arguments should be in registers <receiver + 1> to |
73 // <receiver + 1 + arg_count>. | 71 // <receiver + 1 + arg_count>. |
74 BytecodeArrayBuilder& Call(Register callable, Register receiver, | 72 BytecodeArrayBuilder& Call(Register callable, Register receiver, |
75 size_t arg_count); | 73 size_t arg_count); |
76 | 74 |
77 // Operators. | 75 // Operators (register == lhs, accumulator = rhs). |
78 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); | 76 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); |
79 | 77 |
78 // Tests. | |
79 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg, | |
80 LanguageMode language_mode); | |
81 | |
82 // Casts | |
83 BytecodeArrayBuilder& CastAccumulatorToBoolean(); | |
84 | |
80 // Flow Control. | 85 // Flow Control. |
86 BytecodeArrayBuilder& Bind(BytecodeLabel* label); | |
87 BytecodeArrayBuilder& Jump(BytecodeLabel* label); | |
88 BytecodeArrayBuilder& JumpIfTrue(BytecodeLabel* label); | |
89 BytecodeArrayBuilder& JumpIfFalse(BytecodeLabel* label); | |
81 BytecodeArrayBuilder& Return(); | 90 BytecodeArrayBuilder& Return(); |
82 | 91 |
92 BytecodeArrayBuilder& EnterBlock(); | |
rmcilroy
2015/09/24 11:44:36
nit - comment
| |
93 BytecodeArrayBuilder& LeaveBlock(); | |
94 | |
83 private: | 95 private: |
96 ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; } | |
97 const ZoneVector<uint8_t>* bytecodes() const { return &bytecodes_; } | |
98 Isolate* isolate() const { return isolate_; } | |
99 | |
84 static Bytecode BytecodeForBinaryOperation(Token::Value op); | 100 static Bytecode BytecodeForBinaryOperation(Token::Value op); |
85 static bool FitsInByteOperand(int value); | 101 static Bytecode BytecodeForCompareOperation(Token::Value op); |
86 static bool FitsInByteOperand(size_t value); | 102 static bool FitsInIdxOperand(int value); |
103 static bool FitsInIdxOperand(size_t value); | |
104 static bool FitsInImm8Operand(int value); | |
105 static bool IsJumpWithImm8Operand(Bytecode jump_bytecode); | |
106 static Bytecode GetJumpWithConstantOperand(Bytecode jump_with_smi8_operand); | |
87 | 107 |
88 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); | 108 template <size_t N> |
89 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); | 109 INLINE(void Output(uint8_t(&bytes)[N])); |
90 void Output(Bytecode bytecode, uint8_t r0); | 110 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1, |
111 uint8_t operand2); | |
112 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1); | |
113 void Output(Bytecode bytecode, uint8_t operand0); | |
91 void Output(Bytecode bytecode); | 114 void Output(Bytecode bytecode); |
115 void PatchJump(const ZoneVector<uint8_t>::iterator& jump_target, | |
116 ZoneVector<uint8_t>::iterator jump_location); | |
117 BytecodeArrayBuilder& OutputJump(Bytecode jump_bytecode, | |
118 BytecodeLabel* label); | |
119 | |
120 void EnsureReturn(); | |
92 | 121 |
93 bool OperandIsValid(Bytecode bytecode, int operand_index, | 122 bool OperandIsValid(Bytecode bytecode, int operand_index, |
94 uint8_t operand_value) const; | 123 uint8_t operand_value) const; |
124 bool LastBytecodeInSameBlock() const; | |
95 | 125 |
96 size_t GetConstantPoolEntry(Handle<Object> object); | 126 size_t GetConstantPoolEntry(Handle<Object> object); |
97 | 127 |
128 // Scope helpers used by TemporaryRegisterScope | |
98 int BorrowTemporaryRegister(); | 129 int BorrowTemporaryRegister(); |
99 void ReturnTemporaryRegister(int reg_index); | 130 void ReturnTemporaryRegister(int reg_index); |
100 | 131 |
101 Isolate* isolate_; | 132 Isolate* isolate_; |
102 ZoneVector<uint8_t> bytecodes_; | 133 ZoneVector<uint8_t> bytecodes_; |
103 bool bytecode_generated_; | 134 bool bytecode_generated_; |
135 size_t last_block_end_; | |
136 size_t last_bytecode_start_; | |
137 bool return_seen_in_block_; | |
104 | 138 |
105 IdentityMap<size_t> constants_map_; | 139 IdentityMap<size_t> constants_map_; |
106 ZoneVector<Handle<Object>> constants_; | 140 ZoneVector<Handle<Object>> constants_; |
107 | 141 |
108 int parameter_count_; | 142 int parameter_count_; |
109 int local_register_count_; | 143 int local_register_count_; |
110 int temporary_register_count_; | 144 int temporary_register_count_; |
111 int temporary_register_next_; | 145 int temporary_register_next_; |
112 | 146 |
113 friend class TemporaryRegisterScope; | 147 friend class TemporaryRegisterScope; |
114 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); | 148 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); |
115 }; | 149 }; |
116 | 150 |
151 | |
152 // A label representing a branch target in a bytecode array. When a | |
153 // label is bound, it represents a known position in the bytecode | |
154 // array. For labels that are forward references there can be at most | |
155 // one reference whilst it is unbound. | |
156 class BytecodeLabel final { | |
157 public: | |
158 BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {} | |
159 ~BytecodeLabel() { DCHECK(bound_ && offset_ != kInvalidOffset); } | |
160 | |
161 private: | |
162 static const size_t kInvalidOffset = static_cast<size_t>(-1); | |
163 | |
164 INLINE(void bind_to(size_t offset)) { | |
165 DCHECK(!bound_ && offset != kInvalidOffset); | |
166 offset_ = offset; | |
167 bound_ = true; | |
168 } | |
169 INLINE(void set_referrer(size_t offset)) { | |
170 DCHECK(!bound_ && offset != kInvalidOffset); | |
171 offset_ = offset; | |
172 } | |
173 INLINE(size_t offset() const) { return offset_; } | |
174 INLINE(bool is_bound() const) { return bound_; } | |
175 INLINE(bool is_forward_target() const) { | |
176 return offset() != kInvalidOffset && !is_bound(); | |
177 } | |
178 | |
179 // There are three states for a label: | |
180 // bound_ offset_ | |
181 // UNSET false kInvalidOffset | |
182 // FORWARD_TARGET false Offset of referring jump | |
183 // BACKWARD_TARGET true Offset of label in bytecode array when bound | |
184 bool bound_; | |
185 size_t offset_; | |
186 | |
187 friend class BytecodeArrayBuilder; | |
188 DISALLOW_COPY_AND_ASSIGN(BytecodeLabel); | |
189 }; | |
190 | |
191 | |
117 // A stack-allocated class than allows the instantiator to allocate | 192 // A stack-allocated class than allows the instantiator to allocate |
118 // temporary registers that are cleaned up when scope is closed. | 193 // temporary registers that are cleaned up when scope is closed. |
119 class TemporaryRegisterScope { | 194 class TemporaryRegisterScope { |
120 public: | 195 public: |
121 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); | 196 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
122 ~TemporaryRegisterScope(); | 197 ~TemporaryRegisterScope(); |
123 Register NewRegister(); | 198 Register NewRegister(); |
124 | 199 |
125 private: | 200 private: |
126 void* operator new(size_t size); | 201 void* operator new(size_t size); |
127 void operator delete(void* p); | 202 void operator delete(void* p); |
128 | 203 |
129 BytecodeArrayBuilder* builder_; | 204 BytecodeArrayBuilder* builder_; |
130 int count_; | 205 int count_; |
131 int last_register_index_; | 206 int last_register_index_; |
132 | 207 |
133 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); | 208 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
134 }; | 209 }; |
135 | 210 |
136 | 211 |
137 } // namespace interpreter | 212 } // namespace interpreter |
138 } // namespace internal | 213 } // namespace internal |
139 } // namespace v8 | 214 } // namespace v8 |
140 | 215 |
141 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 216 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
OLD | NEW |