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 #include "src/interpreter/bytecode-array-builder.h" | 5 #include "src/interpreter/bytecode-array-builder.h" |
6 | 6 |
7 namespace v8 { | 7 namespace v8 { |
8 namespace internal { | 8 namespace internal { |
9 namespace interpreter { | 9 namespace interpreter { |
10 | 10 |
11 BytecodeArrayBuilder::BytecodeArrayBuilder(Isolate* isolate, Zone* zone) | 11 BytecodeArrayBuilder::BytecodeArrayBuilder(Isolate* isolate, Zone* zone) |
12 : isolate_(isolate), | 12 : isolate_(isolate), |
13 bytecodes_(zone), | 13 bytecodes_(zone), |
14 bytecode_generated_(false), | 14 bytecode_generated_(false), |
15 last_block_end_(0), | |
16 last_bytecode_start_(~0), | |
15 constants_map_(isolate->heap(), zone), | 17 constants_map_(isolate->heap(), zone), |
16 constants_(zone), | 18 constants_(zone), |
17 parameter_count_(-1), | 19 parameter_count_(-1), |
18 local_register_count_(-1), | 20 local_register_count_(-1), |
19 temporary_register_count_(0), | 21 temporary_register_count_(0), |
20 temporary_register_next_(0) {} | 22 temporary_register_next_(0) {} |
21 | 23 |
22 | 24 |
23 void BytecodeArrayBuilder::set_locals_count(int number_of_locals) { | 25 void BytecodeArrayBuilder::set_locals_count(int number_of_locals) { |
24 local_register_count_ = number_of_locals; | 26 local_register_count_ = number_of_locals; |
25 temporary_register_next_ = local_register_count_; | 27 temporary_register_next_ = local_register_count_; |
26 } | 28 } |
27 | 29 |
28 | 30 |
29 int BytecodeArrayBuilder::locals_count() const { return local_register_count_; } | 31 int BytecodeArrayBuilder::locals_count() const { return local_register_count_; } |
30 | 32 |
31 | 33 |
32 void BytecodeArrayBuilder::set_parameter_count(int number_of_parameters) { | 34 void BytecodeArrayBuilder::set_parameter_count(int number_of_parameters) { |
33 parameter_count_ = number_of_parameters; | 35 parameter_count_ = number_of_parameters; |
34 } | 36 } |
35 | 37 |
36 | 38 |
37 int BytecodeArrayBuilder::parameter_count() const { return parameter_count_; } | 39 int BytecodeArrayBuilder::parameter_count() const { return parameter_count_; } |
38 | 40 |
39 | 41 |
40 bool BytecodeArrayBuilder::HasExplicitReturn() { | |
41 // TODO(rmcilroy): When we have control flow we should return false here if | |
42 // there is an outstanding jump target, even if the last bytecode is kReturn. | |
43 return !bytecodes_.empty() && | |
44 bytecodes_.back() == Bytecodes::ToByte(Bytecode::kReturn); | |
45 } | |
46 | |
47 | |
48 Register BytecodeArrayBuilder::Parameter(int parameter_index) { | 42 Register BytecodeArrayBuilder::Parameter(int parameter_index) { |
49 DCHECK_GE(parameter_index, 0); | 43 DCHECK_GE(parameter_index, 0); |
50 DCHECK_LT(parameter_index, parameter_count_); | 44 DCHECK_LT(parameter_index, parameter_count_); |
51 return Register::FromParameterIndex(parameter_index, parameter_count_); | 45 return Register::FromParameterIndex(parameter_index, parameter_count_); |
52 } | 46 } |
53 | 47 |
54 | 48 |
55 Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() { | 49 Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() { |
56 DCHECK_EQ(bytecode_generated_, false); | 50 DCHECK_EQ(bytecode_generated_, false); |
57 DCHECK_GE(parameter_count_, 0); | 51 DCHECK_GE(parameter_count_, 0); |
58 DCHECK_GE(local_register_count_, 0); | 52 DCHECK_GE(local_register_count_, 0); |
53 EnsureReturn(); | |
rmcilroy
2015/09/23 14:00:28
nit - newlines around this.
oth
2015/09/24 11:15:27
Done.
| |
59 int bytecode_size = static_cast<int>(bytecodes_.size()); | 54 int bytecode_size = static_cast<int>(bytecodes_.size()); |
60 int register_count = local_register_count_ + temporary_register_count_; | 55 int register_count = local_register_count_ + temporary_register_count_; |
61 int frame_size = register_count * kPointerSize; | 56 int frame_size = register_count * kPointerSize; |
62 | 57 |
63 Factory* factory = isolate_->factory(); | 58 Factory* factory = isolate_->factory(); |
64 int constants_count = static_cast<int>(constants_.size()); | 59 int constants_count = static_cast<int>(constants_.size()); |
65 Handle<FixedArray> constant_pool = | 60 Handle<FixedArray> constant_pool = |
66 factory->NewFixedArray(constants_count, TENURED); | 61 factory->NewFixedArray(constants_count, TENURED); |
67 for (int i = 0; i < constants_count; i++) { | 62 for (int i = 0; i < constants_count; i++) { |
68 constant_pool->set(i, *constants_[i]); | 63 constant_pool->set(i, *constants_[i]); |
69 } | 64 } |
70 | 65 |
71 Handle<BytecodeArray> output = | 66 Handle<BytecodeArray> output = |
72 factory->NewBytecodeArray(bytecode_size, &bytecodes_.front(), frame_size, | 67 factory->NewBytecodeArray(bytecode_size, &bytecodes_.front(), frame_size, |
73 parameter_count_, constant_pool); | 68 parameter_count_, constant_pool); |
74 bytecode_generated_ = true; | 69 bytecode_generated_ = true; |
75 return output; | 70 return output; |
76 } | 71 } |
77 | 72 |
78 | 73 |
79 BytecodeArrayBuilder& BytecodeArrayBuilder::BinaryOperation(Token::Value binop, | 74 template <size_t N> |
75 void BytecodeArrayBuilder::Output(uint8_t(&bytes)[N]) { | |
76 DCHECK_EQ(Bytecodes::NumberOfOperands(Bytecodes::FromByte(bytes[0])), N - 1); | |
77 last_bytecode_start_ = bytecodes()->size(); | |
78 for (int i = 1; i < static_cast<int>(N); i++) { | |
79 DCHECK(OperandIsValid(Bytecodes::FromByte(bytes[0]), i - 1, bytes[i])); | |
80 } | |
81 bytecodes()->insert(bytecodes()->end(), bytes, bytes + N); | |
82 } | |
83 | |
84 | |
85 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, | |
86 uint8_t operand1, uint8_t operand2) { | |
87 uint8_t bytes[] = {Bytecodes::ToByte(bytecode), operand0, operand1, operand2}; | |
88 Output(bytes); | |
rmcilroy
2015/09/23 14:00:28
personally I prefer the old-style returns (each of
oth
2015/09/24 11:15:27
The point is well made. This pattern occurs elsewh
rmcilroy
2015/09/24 11:44:36
Ok, you've convinced me :).
| |
89 } | |
90 | |
91 | |
92 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, | |
93 uint8_t operand1) { | |
94 uint8_t bytes[] = {Bytecodes::ToByte(bytecode), operand0, operand1}; | |
95 Output(bytes); | |
96 } | |
97 | |
98 | |
99 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0) { | |
100 uint8_t bytes[] = {Bytecodes::ToByte(bytecode), operand0}; | |
101 Output(bytes); | |
102 } | |
103 | |
104 | |
105 void BytecodeArrayBuilder::Output(Bytecode bytecode) { | |
106 uint8_t bytes[] = {Bytecodes::ToByte(bytecode)}; | |
107 Output(bytes); | |
108 } | |
109 | |
110 | |
111 BytecodeArrayBuilder& BytecodeArrayBuilder::BinaryOperation(Token::Value op, | |
80 Register reg) { | 112 Register reg) { |
81 Output(BytecodeForBinaryOperation(binop), reg.ToOperand()); | 113 Output(BytecodeForBinaryOperation(op), reg.ToOperand()); |
82 return *this; | 114 return *this; |
83 } | 115 } |
84 | 116 |
117 | |
118 BytecodeArrayBuilder& BytecodeArrayBuilder::CompareOperation( | |
119 Token::Value op, Register reg, LanguageMode language_mode) { | |
120 if (!is_sloppy(language_mode)) { | |
121 UNIMPLEMENTED(); | |
122 } | |
123 | |
124 Output(BytecodeForCompareOperation(op), reg.ToOperand()); | |
125 return *this; | |
126 } | |
127 | |
85 | 128 |
86 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral( | 129 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral( |
87 v8::internal::Smi* smi) { | 130 v8::internal::Smi* smi) { |
88 int32_t raw_smi = smi->value(); | 131 int32_t raw_smi = smi->value(); |
89 if (raw_smi == 0) { | 132 if (raw_smi == 0) { |
90 Output(Bytecode::kLdaZero); | 133 Output(Bytecode::kLdaZero); |
91 } else if (raw_smi >= -128 && raw_smi <= 127) { | 134 } else if (raw_smi >= -128 && raw_smi <= 127) { |
92 Output(Bytecode::kLdaSmi8, static_cast<uint8_t>(raw_smi)); | 135 Output(Bytecode::kLdaSmi8, static_cast<uint8_t>(raw_smi)); |
93 } else { | 136 } else { |
94 LoadLiteral(Handle<Object>(smi, isolate_)); | 137 LoadLiteral(Handle<Object>(smi, isolate_)); |
95 } | 138 } |
96 return *this; | 139 return *this; |
97 } | 140 } |
98 | 141 |
99 | 142 |
100 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral(Handle<Object> object) { | 143 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral(Handle<Object> object) { |
101 size_t entry = GetConstantPoolEntry(object); | 144 size_t entry = GetConstantPoolEntry(object); |
102 if (FitsInByteOperand(entry)) { | 145 if (FitsInIdxOperand(entry)) { |
103 Output(Bytecode::kLdaConstant, static_cast<uint8_t>(entry)); | 146 Output(Bytecode::kLdaConstant, static_cast<uint8_t>(entry)); |
104 } else { | 147 } else { |
105 UNIMPLEMENTED(); | 148 UNIMPLEMENTED(); |
106 } | 149 } |
107 return *this; | 150 return *this; |
108 } | 151 } |
109 | 152 |
110 | 153 |
111 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadUndefined() { | 154 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadUndefined() { |
112 Output(Bytecode::kLdaUndefined); | 155 Output(Bytecode::kLdaUndefined); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 return *this; | 194 return *this; |
152 } | 195 } |
153 | 196 |
154 | 197 |
155 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadNamedProperty( | 198 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadNamedProperty( |
156 Register object, int feedback_slot, LanguageMode language_mode) { | 199 Register object, int feedback_slot, LanguageMode language_mode) { |
157 if (!is_sloppy(language_mode)) { | 200 if (!is_sloppy(language_mode)) { |
158 UNIMPLEMENTED(); | 201 UNIMPLEMENTED(); |
159 } | 202 } |
160 | 203 |
161 if (FitsInByteOperand(feedback_slot)) { | 204 if (FitsInIdxOperand(feedback_slot)) { |
162 Output(Bytecode::kLoadIC, object.ToOperand(), | 205 Output(Bytecode::kLoadIC, object.ToOperand(), |
163 static_cast<uint8_t>(feedback_slot)); | 206 static_cast<uint8_t>(feedback_slot)); |
164 } else { | 207 } else { |
165 UNIMPLEMENTED(); | 208 UNIMPLEMENTED(); |
166 } | 209 } |
167 return *this; | 210 return *this; |
168 } | 211 } |
169 | 212 |
170 | 213 |
171 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadKeyedProperty( | 214 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadKeyedProperty( |
172 Register object, int feedback_slot, LanguageMode language_mode) { | 215 Register object, int feedback_slot, LanguageMode language_mode) { |
173 if (!is_sloppy(language_mode)) { | 216 if (!is_sloppy(language_mode)) { |
174 UNIMPLEMENTED(); | 217 UNIMPLEMENTED(); |
175 } | 218 } |
176 | 219 |
177 if (FitsInByteOperand(feedback_slot)) { | 220 if (FitsInIdxOperand(feedback_slot)) { |
178 Output(Bytecode::kKeyedLoadIC, object.ToOperand(), | 221 Output(Bytecode::kKeyedLoadIC, object.ToOperand(), |
179 static_cast<uint8_t>(feedback_slot)); | 222 static_cast<uint8_t>(feedback_slot)); |
180 } else { | 223 } else { |
181 UNIMPLEMENTED(); | 224 UNIMPLEMENTED(); |
182 } | 225 } |
183 return *this; | 226 return *this; |
184 } | 227 } |
185 | 228 |
186 | 229 |
187 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreNamedProperty( | 230 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreNamedProperty( |
188 Register object, Register name, int feedback_slot, | 231 Register object, Register name, int feedback_slot, |
189 LanguageMode language_mode) { | 232 LanguageMode language_mode) { |
190 if (!is_sloppy(language_mode)) { | 233 if (!is_sloppy(language_mode)) { |
191 UNIMPLEMENTED(); | 234 UNIMPLEMENTED(); |
192 } | 235 } |
193 | 236 |
194 if (FitsInByteOperand(feedback_slot)) { | 237 if (FitsInIdxOperand(feedback_slot)) { |
195 Output(Bytecode::kStoreIC, object.ToOperand(), name.ToOperand(), | 238 Output(Bytecode::kStoreIC, object.ToOperand(), name.ToOperand(), |
196 static_cast<uint8_t>(feedback_slot)); | 239 static_cast<uint8_t>(feedback_slot)); |
197 } else { | 240 } else { |
198 UNIMPLEMENTED(); | 241 UNIMPLEMENTED(); |
199 } | 242 } |
200 return *this; | 243 return *this; |
201 } | 244 } |
202 | 245 |
203 | 246 |
204 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreKeyedProperty( | 247 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreKeyedProperty( |
205 Register object, Register key, int feedback_slot, | 248 Register object, Register key, int feedback_slot, |
206 LanguageMode language_mode) { | 249 LanguageMode language_mode) { |
207 if (!is_sloppy(language_mode)) { | 250 if (!is_sloppy(language_mode)) { |
208 UNIMPLEMENTED(); | 251 UNIMPLEMENTED(); |
209 } | 252 } |
210 | 253 |
211 if (FitsInByteOperand(feedback_slot)) { | 254 if (FitsInIdxOperand(feedback_slot)) { |
212 Output(Bytecode::kKeyedStoreIC, object.ToOperand(), key.ToOperand(), | 255 Output(Bytecode::kKeyedStoreIC, object.ToOperand(), key.ToOperand(), |
213 static_cast<uint8_t>(feedback_slot)); | 256 static_cast<uint8_t>(feedback_slot)); |
214 } else { | 257 } else { |
215 UNIMPLEMENTED(); | 258 UNIMPLEMENTED(); |
216 } | 259 } |
217 return *this; | 260 return *this; |
218 } | 261 } |
219 | 262 |
220 | 263 |
264 BytecodeArrayBuilder& BytecodeArrayBuilder::CastAccumulatorToBoolean() { | |
265 if (LastBytecodeInSameBlock()) { | |
266 // If the previous bytecode puts a boolean in the accumulator | |
267 // there is no need to emit an instruction. | |
268 switch (Bytecodes::FromByte(bytecodes()->at(last_bytecode_start_))) { | |
269 case Bytecode::kCastToBoolean: | |
270 DCHECK(false); | |
rmcilroy
2015/09/23 14:00:28
nit - just UNREACHABLE()
oth
2015/09/24 11:15:27
Done.
| |
271 case Bytecode::kLdaTrue: | |
272 case Bytecode::kLdaFalse: | |
273 case Bytecode::kTestEqual: | |
274 case Bytecode::kTestNotEqual: | |
275 case Bytecode::kTestEqualStrict: | |
276 case Bytecode::kTestNotEqualStrict: | |
277 case Bytecode::kTestLessThan: | |
278 case Bytecode::kTestLessThanEqual: | |
279 case Bytecode::kTestGreaterThan: | |
280 case Bytecode::kTestGreaterThanEqual: | |
281 case Bytecode::kTestInstanceOf: | |
282 case Bytecode::kTestIn: | |
283 break; | |
284 default: | |
285 Output(Bytecode::kCastToBoolean); | |
286 } | |
287 } | |
288 return *this; | |
289 } | |
290 | |
291 | |
292 BytecodeArrayBuilder& BytecodeArrayBuilder::Bind(BytecodeLabel* label) { | |
293 if (label->is_forward_target()) { | |
294 // An earlier jump instruction refers to this label. Update it's location. | |
295 PatchJump(bytecodes()->end(), bytecodes()->begin() + label->offset()); | |
296 // Now treat as if the label will only be back referred to. | |
297 } | |
298 label->bind_to(bytecodes()->size()); | |
299 return *this; | |
300 } | |
301 | |
302 | |
303 // static | |
304 bool BytecodeArrayBuilder::IsJumpWithSmi8Operand(Bytecode jump_bytecode) { | |
rmcilroy
2015/09/23 14:00:28
nit /s/Smi8/Imm8
oth
2015/09/24 11:15:27
Done.
| |
305 return jump_bytecode == Bytecode::kJump || | |
306 jump_bytecode == Bytecode::kJumpIfTrue || | |
307 jump_bytecode == Bytecode::kJumpIfFalse; | |
308 } | |
309 | |
310 | |
311 // static | |
312 Bytecode BytecodeArrayBuilder::GetJumpWithConstantOperand( | |
313 Bytecode jump_bytecode) { | |
314 switch (jump_bytecode) { | |
315 case Bytecode::kJump: | |
316 return Bytecode::kJumpConstant; | |
317 case Bytecode::kJumpIfTrue: | |
318 return Bytecode::kJumpIfTrueConstant; | |
319 case Bytecode::kJumpIfFalse: | |
320 return Bytecode::kJumpIfFalseConstant; | |
321 default: | |
322 UNREACHABLE(); | |
323 return Bytecode::kJumpConstant; | |
324 } | |
325 } | |
326 | |
327 | |
328 void BytecodeArrayBuilder::PatchJump( | |
329 const ZoneVector<uint8_t>::iterator& jump_target, | |
330 ZoneVector<uint8_t>::iterator jump_location) { | |
331 Bytecode jump_bytecode = Bytecodes::FromByte(*jump_location); | |
332 DCHECK(IsJumpWithSmi8Operand(jump_bytecode)); | |
333 DCHECK_EQ(Bytecodes::Size(jump_bytecode), 2); | |
334 int delta = static_cast<int>(jump_target - jump_location); | |
335 DCHECK_GE(delta, 0); | |
336 if (FitsInImm8Operand(delta)) { | |
337 // Just update the operand | |
338 jump_location++; | |
339 *jump_location = static_cast<uint8_t>(delta); | |
340 } else { | |
341 // Update the jump type and operand | |
342 size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); | |
rmcilroy
2015/09/23 14:00:28
We will need to reserve the constant pool entry ah
oth
2015/09/24 11:15:27
Done.
| |
343 if (FitsInIdxOperand(entry)) { | |
344 *jump_location++ = | |
345 Bytecodes::ToByte(GetJumpWithConstantOperand(jump_bytecode)); | |
346 *jump_location = static_cast<uint8_t>(entry); | |
347 } else { | |
348 UNIMPLEMENTED(); | |
349 } | |
350 } | |
351 } | |
352 | |
353 | |
354 void BytecodeArrayBuilder::OutputJump( | |
355 Bytecode jump_bytecode, const ZoneVector<uint8_t>::iterator& jump_target) { | |
356 DCHECK(IsJumpWithSmi8Operand(jump_bytecode)); | |
357 | |
358 int delta = static_cast<int>(jump_target - bytecodes()->end()); | |
359 if (FitsInImm8Operand(delta)) { | |
360 Output(jump_bytecode, static_cast<uint8_t>(delta)); | |
361 } else { | |
362 size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); | |
363 if (FitsInIdxOperand(entry)) { | |
364 Output(GetJumpWithConstantOperand(jump_bytecode), | |
365 static_cast<uint8_t>(entry)); | |
366 } else { | |
367 UNIMPLEMENTED(); | |
368 } | |
369 } | |
370 } | |
371 | |
372 | |
373 BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(Bytecode jump_bytecode, | |
374 BytecodeLabel* label) { | |
375 if (label->is_bound()) { | |
376 // Label has been bound already so this is a backwards jump. | |
377 OutputJump(jump_bytecode, bytecodes()->begin() + label->offset()); | |
378 } else { | |
379 // Label has not yet been bound so this is a forward reference | |
380 // that will be patched when the label is bound. | |
381 label->set_referrer(bytecodes()->size()); | |
382 OutputJump(jump_bytecode, bytecodes()->end()); | |
383 } | |
rmcilroy
2015/09/23 14:00:28
nit - could we fold OutputJump into this function
oth
2015/09/24 11:15:27
Done.
| |
384 return *this; | |
385 } | |
386 | |
387 | |
388 BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(BytecodeLabel* label) { | |
389 return Jump(Bytecode::kJump, label); | |
390 } | |
391 | |
392 | |
393 BytecodeArrayBuilder& BytecodeArrayBuilder::JumpIfTrue(BytecodeLabel* label) { | |
394 return Jump(Bytecode::kJumpIfTrue, label); | |
395 } | |
396 | |
397 | |
398 BytecodeArrayBuilder& BytecodeArrayBuilder::JumpIfFalse(BytecodeLabel* label) { | |
399 return Jump(Bytecode::kJumpIfFalse, label); | |
400 } | |
401 | |
402 | |
221 BytecodeArrayBuilder& BytecodeArrayBuilder::Return() { | 403 BytecodeArrayBuilder& BytecodeArrayBuilder::Return() { |
222 Output(Bytecode::kReturn); | 404 Output(Bytecode::kReturn); |
223 return *this; | 405 return *this; |
224 } | 406 } |
225 | 407 |
226 | 408 |
409 BytecodeArrayBuilder& BytecodeArrayBuilder::EnterBlock() { return *this; } | |
410 | |
411 | |
412 BytecodeArrayBuilder& BytecodeArrayBuilder::LeaveBlock() { | |
413 last_block_end_ = bytecodes()->size(); | |
414 return *this; | |
415 } | |
416 | |
417 | |
418 void BytecodeArrayBuilder::EnsureReturn() { | |
419 size_t offset = last_block_end_; | |
420 while (offset < bytecodes()->size()) { | |
rmcilroy
2015/09/23 14:00:28
As discussed offline, please add a TODO to avoid t
oth
2015/09/24 11:15:27
Switched to O(1) implementation.
| |
421 Bytecode bytecode = Bytecodes::FromByte(bytecodes()->at(offset)); | |
422 if (bytecode == Bytecode::kReturn) { | |
423 return; | |
424 } | |
425 offset += Bytecodes::Size(bytecode); | |
426 } | |
427 LoadUndefined(); | |
428 Return(); | |
429 } | |
430 | |
227 BytecodeArrayBuilder& BytecodeArrayBuilder::Call(Register callable, | 431 BytecodeArrayBuilder& BytecodeArrayBuilder::Call(Register callable, |
228 Register receiver, | 432 Register receiver, |
229 size_t arg_count) { | 433 size_t arg_count) { |
230 if (FitsInByteOperand(arg_count)) { | 434 if (FitsInIdxOperand(arg_count)) { |
231 Output(Bytecode::kCall, callable.ToOperand(), receiver.ToOperand(), | 435 Output(Bytecode::kCall, callable.ToOperand(), receiver.ToOperand(), |
232 static_cast<uint8_t>(arg_count)); | 436 static_cast<uint8_t>(arg_count)); |
233 } else { | 437 } else { |
234 UNIMPLEMENTED(); | 438 UNIMPLEMENTED(); |
235 } | 439 } |
236 return *this; | 440 return *this; |
237 } | 441 } |
238 | 442 |
239 | 443 |
240 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) { | 444 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 return parameter_index >= 0 && parameter_index < parameter_count_; | 495 return parameter_index >= 0 && parameter_index < parameter_count_; |
292 } else { | 496 } else { |
293 return (reg.index() >= 0 && reg.index() < temporary_register_next_); | 497 return (reg.index() >= 0 && reg.index() < temporary_register_next_); |
294 } | 498 } |
295 } | 499 } |
296 } | 500 } |
297 UNREACHABLE(); | 501 UNREACHABLE(); |
298 return false; | 502 return false; |
299 } | 503 } |
300 | 504 |
301 | 505 bool BytecodeArrayBuilder::LastBytecodeInSameBlock() const { |
302 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, | 506 return last_bytecode_start_ < bytecodes()->size() && |
303 uint8_t operand1, uint8_t operand2) { | 507 last_bytecode_start_ >= last_block_end_; |
304 DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 3); | |
305 DCHECK(OperandIsValid(bytecode, 0, operand0) && | |
306 OperandIsValid(bytecode, 1, operand1) && | |
307 OperandIsValid(bytecode, 2, operand2)); | |
308 bytecodes_.push_back(Bytecodes::ToByte(bytecode)); | |
309 bytecodes_.push_back(operand0); | |
310 bytecodes_.push_back(operand1); | |
311 bytecodes_.push_back(operand2); | |
312 } | 508 } |
313 | 509 |
314 | 510 |
315 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, | |
316 uint8_t operand1) { | |
317 DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 2); | |
318 DCHECK(OperandIsValid(bytecode, 0, operand0) && | |
319 OperandIsValid(bytecode, 1, operand1)); | |
320 bytecodes_.push_back(Bytecodes::ToByte(bytecode)); | |
321 bytecodes_.push_back(operand0); | |
322 bytecodes_.push_back(operand1); | |
323 } | |
324 | |
325 | |
326 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0) { | |
327 DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 1); | |
328 DCHECK(OperandIsValid(bytecode, 0, operand0)); | |
329 bytecodes_.push_back(Bytecodes::ToByte(bytecode)); | |
330 bytecodes_.push_back(operand0); | |
331 } | |
332 | |
333 | |
334 void BytecodeArrayBuilder::Output(Bytecode bytecode) { | |
335 DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 0); | |
336 bytecodes_.push_back(Bytecodes::ToByte(bytecode)); | |
337 } | |
338 | |
339 | |
340 // static | 511 // static |
341 Bytecode BytecodeArrayBuilder::BytecodeForBinaryOperation(Token::Value op) { | 512 Bytecode BytecodeArrayBuilder::BytecodeForBinaryOperation(Token::Value op) { |
342 switch (op) { | 513 switch (op) { |
343 case Token::Value::ADD: | 514 case Token::Value::ADD: |
344 return Bytecode::kAdd; | 515 return Bytecode::kAdd; |
345 case Token::Value::SUB: | 516 case Token::Value::SUB: |
346 return Bytecode::kSub; | 517 return Bytecode::kSub; |
347 case Token::Value::MUL: | 518 case Token::Value::MUL: |
348 return Bytecode::kMul; | 519 return Bytecode::kMul; |
349 case Token::Value::DIV: | 520 case Token::Value::DIV: |
350 return Bytecode::kDiv; | 521 return Bytecode::kDiv; |
351 case Token::Value::MOD: | 522 case Token::Value::MOD: |
352 return Bytecode::kMod; | 523 return Bytecode::kMod; |
353 default: | 524 default: |
354 UNIMPLEMENTED(); | 525 UNREACHABLE(); |
355 return static_cast<Bytecode>(-1); | 526 return static_cast<Bytecode>(-1); |
356 } | 527 } |
357 } | 528 } |
358 | 529 |
359 | 530 |
360 // static | 531 // static |
361 bool BytecodeArrayBuilder::FitsInByteOperand(int value) { | 532 Bytecode BytecodeArrayBuilder::BytecodeForCompareOperation(Token::Value op) { |
533 switch (op) { | |
534 case Token::Value::EQ: | |
535 return Bytecode::kTestEqual; | |
536 case Token::Value::NE: | |
537 return Bytecode::kTestNotEqual; | |
538 case Token::Value::EQ_STRICT: | |
539 return Bytecode::kTestEqualStrict; | |
540 case Token::Value::NE_STRICT: | |
541 return Bytecode::kTestNotEqualStrict; | |
542 case Token::Value::LT: | |
543 return Bytecode::kTestLessThan; | |
544 case Token::Value::GT: | |
545 return Bytecode::kTestGreaterThan; | |
546 case Token::Value::LTE: | |
547 return Bytecode::kTestLessThanEqual; | |
548 case Token::Value::GTE: | |
549 return Bytecode::kTestGreaterThanEqual; | |
550 case Token::Value::INSTANCEOF: | |
551 return Bytecode::kTestInstanceOf; | |
552 case Token::Value::IN: | |
553 return Bytecode::kTestIn; | |
554 default: | |
555 UNREACHABLE(); | |
556 return static_cast<Bytecode>(-1); | |
557 } | |
558 } | |
559 | |
560 | |
561 // static | |
562 bool BytecodeArrayBuilder::FitsInIdxOperand(int value) { | |
362 return 0 <= value && value <= 255; | 563 return 0 <= value && value <= 255; |
363 } | 564 } |
364 | 565 |
365 | 566 |
366 // static | 567 // static |
367 bool BytecodeArrayBuilder::FitsInByteOperand(size_t value) { | 568 bool BytecodeArrayBuilder::FitsInIdxOperand(size_t value) { |
368 return value <= 255; | 569 return value <= 255; |
369 } | 570 } |
370 | 571 |
371 | 572 |
573 // static | |
574 bool BytecodeArrayBuilder::FitsInImm8Operand(int value) { | |
575 return -128 <= value && value < 128; | |
576 } | |
577 | |
578 | |
372 TemporaryRegisterScope::TemporaryRegisterScope(BytecodeArrayBuilder* builder) | 579 TemporaryRegisterScope::TemporaryRegisterScope(BytecodeArrayBuilder* builder) |
373 : builder_(builder), count_(0), last_register_index_(-1) {} | 580 : builder_(builder), count_(0), last_register_index_(-1) {} |
374 | 581 |
375 | 582 |
376 TemporaryRegisterScope::~TemporaryRegisterScope() { | 583 TemporaryRegisterScope::~TemporaryRegisterScope() { |
377 while (count_-- != 0) { | 584 while (count_-- != 0) { |
378 builder_->ReturnTemporaryRegister(last_register_index_--); | 585 builder_->ReturnTemporaryRegister(last_register_index_--); |
379 } | 586 } |
380 } | 587 } |
381 | 588 |
382 | 589 |
383 Register TemporaryRegisterScope::NewRegister() { | 590 Register TemporaryRegisterScope::NewRegister() { |
384 count_++; | 591 count_++; |
385 last_register_index_ = builder_->BorrowTemporaryRegister(); | 592 last_register_index_ = builder_->BorrowTemporaryRegister(); |
386 return Register(last_register_index_); | 593 return Register(last_register_index_); |
387 } | 594 } |
388 | 595 |
389 } // namespace interpreter | 596 } // namespace interpreter |
390 } // namespace internal | 597 } // namespace internal |
391 } // namespace v8 | 598 } // namespace v8 |
OLD | NEW |