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