Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: src/interpreter/bytecode-array-builder.cc

Issue 1343363002: [Interpreter] Basic flow control. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Incorporate comments in https://codereview.chromium.org/1343363002/#msg10 Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 return *this; 197 return *this;
152 } 198 }
153 199
154 200
155 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadNamedProperty( 201 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadNamedProperty(
156 Register object, int feedback_slot, LanguageMode language_mode) { 202 Register object, int feedback_slot, LanguageMode language_mode) {
157 if (!is_sloppy(language_mode)) { 203 if (!is_sloppy(language_mode)) {
158 UNIMPLEMENTED(); 204 UNIMPLEMENTED();
159 } 205 }
160 206
161 if (FitsInByteOperand(feedback_slot)) { 207 if (FitsInIdxOperand(feedback_slot)) {
162 Output(Bytecode::kLoadIC, object.ToOperand(), 208 Output(Bytecode::kLoadIC, object.ToOperand(),
163 static_cast<uint8_t>(feedback_slot)); 209 static_cast<uint8_t>(feedback_slot));
164 } else { 210 } else {
165 UNIMPLEMENTED(); 211 UNIMPLEMENTED();
166 } 212 }
167 return *this; 213 return *this;
168 } 214 }
169 215
170 216
171 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadKeyedProperty( 217 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadKeyedProperty(
172 Register object, int feedback_slot, LanguageMode language_mode) { 218 Register object, int feedback_slot, LanguageMode language_mode) {
173 if (!is_sloppy(language_mode)) { 219 if (!is_sloppy(language_mode)) {
174 UNIMPLEMENTED(); 220 UNIMPLEMENTED();
175 } 221 }
176 222
177 if (FitsInByteOperand(feedback_slot)) { 223 if (FitsInIdxOperand(feedback_slot)) {
178 Output(Bytecode::kKeyedLoadIC, object.ToOperand(), 224 Output(Bytecode::kKeyedLoadIC, object.ToOperand(),
179 static_cast<uint8_t>(feedback_slot)); 225 static_cast<uint8_t>(feedback_slot));
180 } else { 226 } else {
181 UNIMPLEMENTED(); 227 UNIMPLEMENTED();
182 } 228 }
183 return *this; 229 return *this;
184 } 230 }
185 231
186 232
187 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreNamedProperty( 233 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreNamedProperty(
188 Register object, Register name, int feedback_slot, 234 Register object, Register name, int feedback_slot,
189 LanguageMode language_mode) { 235 LanguageMode language_mode) {
190 if (!is_sloppy(language_mode)) { 236 if (!is_sloppy(language_mode)) {
191 UNIMPLEMENTED(); 237 UNIMPLEMENTED();
192 } 238 }
193 239
194 if (FitsInByteOperand(feedback_slot)) { 240 if (FitsInIdxOperand(feedback_slot)) {
195 Output(Bytecode::kStoreIC, object.ToOperand(), name.ToOperand(), 241 Output(Bytecode::kStoreIC, object.ToOperand(), name.ToOperand(),
196 static_cast<uint8_t>(feedback_slot)); 242 static_cast<uint8_t>(feedback_slot));
197 } else { 243 } else {
198 UNIMPLEMENTED(); 244 UNIMPLEMENTED();
199 } 245 }
200 return *this; 246 return *this;
201 } 247 }
202 248
203 249
204 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreKeyedProperty( 250 BytecodeArrayBuilder& BytecodeArrayBuilder::StoreKeyedProperty(
205 Register object, Register key, int feedback_slot, 251 Register object, Register key, int feedback_slot,
206 LanguageMode language_mode) { 252 LanguageMode language_mode) {
207 if (!is_sloppy(language_mode)) { 253 if (!is_sloppy(language_mode)) {
208 UNIMPLEMENTED(); 254 UNIMPLEMENTED();
209 } 255 }
210 256
211 if (FitsInByteOperand(feedback_slot)) { 257 if (FitsInIdxOperand(feedback_slot)) {
212 Output(Bytecode::kKeyedStoreIC, object.ToOperand(), key.ToOperand(), 258 Output(Bytecode::kKeyedStoreIC, object.ToOperand(), key.ToOperand(),
213 static_cast<uint8_t>(feedback_slot)); 259 static_cast<uint8_t>(feedback_slot));
214 } else { 260 } else {
215 UNIMPLEMENTED(); 261 UNIMPLEMENTED();
216 } 262 }
217 return *this; 263 return *this;
218 } 264 }
219 265
220 266
267 BytecodeArrayBuilder& BytecodeArrayBuilder::CastAccumulatorToBoolean() {
268 if (LastBytecodeInSameBlock()) {
269 // If the previous bytecode puts a boolean in the accumulator
270 // there is no need to emit an instruction.
271 switch (Bytecodes::FromByte(bytecodes()->at(last_bytecode_start_))) {
272 case Bytecode::kToBoolean:
273 UNREACHABLE();
274 case Bytecode::kLdaTrue:
275 case Bytecode::kLdaFalse:
276 case Bytecode::kTestEqual:
277 case Bytecode::kTestNotEqual:
278 case Bytecode::kTestEqualStrict:
279 case Bytecode::kTestNotEqualStrict:
280 case Bytecode::kTestLessThan:
281 case Bytecode::kTestLessThanEqual:
282 case Bytecode::kTestGreaterThan:
283 case Bytecode::kTestGreaterThanEqual:
284 case Bytecode::kTestInstanceOf:
285 case Bytecode::kTestIn:
286 break;
287 default:
288 Output(Bytecode::kToBoolean);
289 }
290 }
291 return *this;
292 }
293
294
295 BytecodeArrayBuilder& BytecodeArrayBuilder::Bind(BytecodeLabel* label) {
296 if (label->is_forward_target()) {
297 // An earlier jump instruction refers to this label. Update it's location.
298 PatchJump(bytecodes()->end(), bytecodes()->begin() + label->offset());
299 // Now treat as if the label will only be back referred to.
300 }
301 label->bind_to(bytecodes()->size());
302 return *this;
303 }
304
305
306 // static
307 bool BytecodeArrayBuilder::IsJumpWithImm8Operand(Bytecode jump_bytecode) {
308 return jump_bytecode == Bytecode::kJump ||
309 jump_bytecode == Bytecode::kJumpIfTrue ||
310 jump_bytecode == Bytecode::kJumpIfFalse;
311 }
312
313
314 // static
315 Bytecode BytecodeArrayBuilder::GetJumpWithConstantOperand(
316 Bytecode jump_bytecode) {
317 switch (jump_bytecode) {
318 case Bytecode::kJump:
319 return Bytecode::kJumpConstant;
320 case Bytecode::kJumpIfTrue:
321 return Bytecode::kJumpIfTrueConstant;
322 case Bytecode::kJumpIfFalse:
323 return Bytecode::kJumpIfFalseConstant;
324 default:
325 UNREACHABLE();
326 return Bytecode::kJumpConstant;
327 }
328 }
329
330
331 void BytecodeArrayBuilder::PatchJump(
332 const ZoneVector<uint8_t>::iterator& jump_target,
333 ZoneVector<uint8_t>::iterator jump_location) {
334 Bytecode jump_bytecode = Bytecodes::FromByte(*jump_location);
335 int delta = static_cast<int>(jump_target - jump_location);
336
337 DCHECK(IsJumpWithImm8Operand(jump_bytecode));
338 DCHECK_EQ(Bytecodes::Size(jump_bytecode), 2);
339 DCHECK_GE(delta, 0);
340
341 if (FitsInImm8Operand(delta)) {
342 // Just update the operand
343 jump_location++;
344 *jump_location = static_cast<uint8_t>(delta);
345 } else {
346 // Update the jump type and operand
347 size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate()));
348 if (FitsInIdxOperand(entry)) {
349 *jump_location++ =
350 Bytecodes::ToByte(GetJumpWithConstantOperand(jump_bytecode));
351 *jump_location = static_cast<uint8_t>(entry);
352 } else {
353 // TODO(oth): OutputJump should reserve a constant pool entry
354 // when jump is written. The reservation should be used here if
355 // needed, or cancelled if not. This is due to the patch needing
356 // to match the size of the code it's replacing. In future,
357 // there will probably be a jump with 32-bit operand for cases
358 // when constant pool is full, but that needs to be emitted in
359 // OutputJump too.
360 UNIMPLEMENTED();
361 }
362 }
363 }
364
365
366 BytecodeArrayBuilder& BytecodeArrayBuilder::OutputJump(Bytecode jump_bytecode,
367 BytecodeLabel* label) {
368 int delta;
369 if (label->is_bound()) {
370 // Label has been bound already so this is a backwards jump.
371 CHECK_GE(bytecodes()->size(), label->offset());
372 CHECK_LE(bytecodes()->size(), static_cast<size_t>(kMaxInt));
373 size_t abs_delta = bytecodes()->size() - label->offset();
374 delta = -static_cast<int>(abs_delta);
375 } else {
376 // Label has not yet been bound so this is a forward reference
377 // that will be patched when the label is bound.
378 label->set_referrer(bytecodes()->size());
379 delta = 0;
380 }
381
382 if (FitsInImm8Operand(delta)) {
383 Output(jump_bytecode, static_cast<uint8_t>(delta));
384 } else {
385 size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate()));
386 if (FitsInIdxOperand(entry)) {
387 Output(GetJumpWithConstantOperand(jump_bytecode),
388 static_cast<uint8_t>(entry));
389 } else {
390 UNIMPLEMENTED();
391 }
392 }
393 return *this;
394 }
395
396
397 BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(BytecodeLabel* label) {
398 return OutputJump(Bytecode::kJump, label);
399 }
400
401
402 BytecodeArrayBuilder& BytecodeArrayBuilder::JumpIfTrue(BytecodeLabel* label) {
403 return OutputJump(Bytecode::kJumpIfTrue, label);
404 }
405
406
407 BytecodeArrayBuilder& BytecodeArrayBuilder::JumpIfFalse(BytecodeLabel* label) {
408 return OutputJump(Bytecode::kJumpIfFalse, label);
409 }
410
411
221 BytecodeArrayBuilder& BytecodeArrayBuilder::Return() { 412 BytecodeArrayBuilder& BytecodeArrayBuilder::Return() {
222 Output(Bytecode::kReturn); 413 Output(Bytecode::kReturn);
414 return_seen_in_block_ = true;
223 return *this; 415 return *this;
224 } 416 }
225 417
226 418
419 BytecodeArrayBuilder& BytecodeArrayBuilder::EnterBlock() { return *this; }
420
421
422 BytecodeArrayBuilder& BytecodeArrayBuilder::LeaveBlock() {
423 last_block_end_ = bytecodes()->size();
424 return_seen_in_block_ = false;
425 return *this;
426 }
427
428
429 void BytecodeArrayBuilder::EnsureReturn() {
430 if (!return_seen_in_block_) {
431 LoadUndefined();
432 Return();
433 }
434 }
435
227 BytecodeArrayBuilder& BytecodeArrayBuilder::Call(Register callable, 436 BytecodeArrayBuilder& BytecodeArrayBuilder::Call(Register callable,
228 Register receiver, 437 Register receiver,
229 size_t arg_count) { 438 size_t arg_count) {
230 if (FitsInByteOperand(arg_count)) { 439 if (FitsInIdxOperand(arg_count)) {
231 Output(Bytecode::kCall, callable.ToOperand(), receiver.ToOperand(), 440 Output(Bytecode::kCall, callable.ToOperand(), receiver.ToOperand(),
232 static_cast<uint8_t>(arg_count)); 441 static_cast<uint8_t>(arg_count));
233 } else { 442 } else {
234 UNIMPLEMENTED(); 443 UNIMPLEMENTED();
235 } 444 }
236 return *this; 445 return *this;
237 } 446 }
238 447
239 448
240 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) { 449 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 return parameter_index >= 0 && parameter_index < parameter_count_; 500 return parameter_index >= 0 && parameter_index < parameter_count_;
292 } else { 501 } else {
293 return (reg.index() >= 0 && reg.index() < temporary_register_next_); 502 return (reg.index() >= 0 && reg.index() < temporary_register_next_);
294 } 503 }
295 } 504 }
296 } 505 }
297 UNREACHABLE(); 506 UNREACHABLE();
298 return false; 507 return false;
299 } 508 }
300 509
301 510 bool BytecodeArrayBuilder::LastBytecodeInSameBlock() const {
302 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, 511 return last_bytecode_start_ < bytecodes()->size() &&
303 uint8_t operand1, uint8_t operand2) { 512 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 } 513 }
313 514
314 515
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 516 // static
341 Bytecode BytecodeArrayBuilder::BytecodeForBinaryOperation(Token::Value op) { 517 Bytecode BytecodeArrayBuilder::BytecodeForBinaryOperation(Token::Value op) {
342 switch (op) { 518 switch (op) {
343 case Token::Value::ADD: 519 case Token::Value::ADD:
344 return Bytecode::kAdd; 520 return Bytecode::kAdd;
345 case Token::Value::SUB: 521 case Token::Value::SUB:
346 return Bytecode::kSub; 522 return Bytecode::kSub;
347 case Token::Value::MUL: 523 case Token::Value::MUL:
348 return Bytecode::kMul; 524 return Bytecode::kMul;
349 case Token::Value::DIV: 525 case Token::Value::DIV:
350 return Bytecode::kDiv; 526 return Bytecode::kDiv;
351 case Token::Value::MOD: 527 case Token::Value::MOD:
352 return Bytecode::kMod; 528 return Bytecode::kMod;
353 default: 529 default:
354 UNIMPLEMENTED(); 530 UNREACHABLE();
355 return static_cast<Bytecode>(-1); 531 return static_cast<Bytecode>(-1);
356 } 532 }
357 } 533 }
358 534
359 535
360 // static 536 // static
361 bool BytecodeArrayBuilder::FitsInByteOperand(int value) { 537 Bytecode BytecodeArrayBuilder::BytecodeForCompareOperation(Token::Value op) {
362 return 0 <= value && value <= 255; 538 switch (op) {
539 case Token::Value::EQ:
540 return Bytecode::kTestEqual;
541 case Token::Value::NE:
542 return Bytecode::kTestNotEqual;
543 case Token::Value::EQ_STRICT:
544 return Bytecode::kTestEqualStrict;
545 case Token::Value::NE_STRICT:
546 return Bytecode::kTestNotEqualStrict;
547 case Token::Value::LT:
548 return Bytecode::kTestLessThan;
549 case Token::Value::GT:
550 return Bytecode::kTestGreaterThan;
551 case Token::Value::LTE:
552 return Bytecode::kTestLessThanEqual;
553 case Token::Value::GTE:
554 return Bytecode::kTestGreaterThanEqual;
555 case Token::Value::INSTANCEOF:
556 return Bytecode::kTestInstanceOf;
557 case Token::Value::IN:
558 return Bytecode::kTestIn;
559 default:
560 UNREACHABLE();
561 return static_cast<Bytecode>(-1);
562 }
363 } 563 }
364 564
365 565
366 // static 566 // static
367 bool BytecodeArrayBuilder::FitsInByteOperand(size_t value) { 567 bool BytecodeArrayBuilder::FitsInIdxOperand(int value) {
368 return value <= 255; 568 return kMinUInt8 <= value && value <= kMaxUInt8;
369 } 569 }
370 570
371 571
572 // static
573 bool BytecodeArrayBuilder::FitsInIdxOperand(size_t value) {
574 return value <= static_cast<size_t>(kMaxUInt8);
575 }
576
577
578 // static
579 bool BytecodeArrayBuilder::FitsInImm8Operand(int value) {
580 return kMinInt8 <= value && value < kMaxInt8;
581 }
582
583
372 TemporaryRegisterScope::TemporaryRegisterScope(BytecodeArrayBuilder* builder) 584 TemporaryRegisterScope::TemporaryRegisterScope(BytecodeArrayBuilder* builder)
373 : builder_(builder), count_(0), last_register_index_(-1) {} 585 : builder_(builder), count_(0), last_register_index_(-1) {}
374 586
375 587
376 TemporaryRegisterScope::~TemporaryRegisterScope() { 588 TemporaryRegisterScope::~TemporaryRegisterScope() {
377 while (count_-- != 0) { 589 while (count_-- != 0) {
378 builder_->ReturnTemporaryRegister(last_register_index_--); 590 builder_->ReturnTemporaryRegister(last_register_index_--);
379 } 591 }
380 } 592 }
381 593
382 594
383 Register TemporaryRegisterScope::NewRegister() { 595 Register TemporaryRegisterScope::NewRegister() {
384 count_++; 596 count_++;
385 last_register_index_ = builder_->BorrowTemporaryRegister(); 597 last_register_index_ = builder_->BorrowTemporaryRegister();
386 return Register(last_register_index_); 598 return Register(last_register_index_);
387 } 599 }
388 600
389 } // namespace interpreter 601 } // namespace interpreter
390 } // namespace internal 602 } // namespace internal
391 } // namespace v8 603 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698