test-bytecode-generator.cc 207 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/v8.h"

#include "src/compiler.h"
8
#include "src/interpreter/bytecode-array-iterator.h"
9 10 11
#include "src/interpreter/bytecode-generator.h"
#include "src/interpreter/interpreter.h"
#include "test/cctest/cctest.h"
12
#include "test/cctest/test-feedback-vector.h"
13 14 15 16 17 18 19

namespace v8 {
namespace internal {
namespace interpreter {

class BytecodeGeneratorHelper {
 public:
20
  const char* kFunctionName = "f";
21

22
  static const int kLastParamIndex =
23 24
      -InterpreterFrameConstants::kLastParamFromRegisterPointer / kPointerSize;

25 26
  BytecodeGeneratorHelper() {
    i::FLAG_ignition = true;
27
    i::FLAG_ignition_fake_try_catch = true;
28 29
    i::FLAG_ignition_filter = StrDup(kFunctionName);
    i::FLAG_always_opt = false;
30
    i::FLAG_allow_natives_syntax = true;
31 32 33
    CcTest::i_isolate()->interpreter()->Initialize();
  }

34
  Isolate* isolate() { return CcTest::i_isolate(); }
35 36
  Factory* factory() { return CcTest::i_isolate()->factory(); }

37 38 39 40 41 42 43 44 45
  Handle<BytecodeArray> MakeTopLevelBytecode(const char* source) {
    const char* old_ignition_filter = i::FLAG_ignition_filter;
    i::FLAG_ignition_filter = "*";
    Local<v8::Script> script = v8_compile(source);
    i::FLAG_ignition_filter = old_ignition_filter;
    i::Handle<i::JSFunction> js_function = v8::Utils::OpenHandle(*script);
    return handle(js_function->shared()->bytecode_array(), CcTest::i_isolate());
  }

46 47 48
  Handle<BytecodeArray> MakeBytecode(const char* script,
                                     const char* function_name) {
    CompileRun(script);
49 50 51 52
    v8::Local<v8::Context> context =
        v8::Isolate::GetCurrent()->GetCurrentContext();
    Local<Function> function = Local<Function>::Cast(
        CcTest::global()->Get(context, v8_str(function_name)).ToLocalChecked());
53 54
    i::Handle<i::JSFunction> js_function =
        i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function));
55 56 57
    return handle(js_function->shared()->bytecode_array(), CcTest::i_isolate());
  }

58 59 60 61 62 63 64 65 66
  Handle<BytecodeArray> MakeBytecode(const char* script, const char* filter,
                                     const char* function_name) {
    const char* old_ignition_filter = i::FLAG_ignition_filter;
    i::FLAG_ignition_filter = filter;
    Handle<BytecodeArray> return_val = MakeBytecode(script, function_name);
    i::FLAG_ignition_filter = old_ignition_filter;
    return return_val;
  }

67
  Handle<BytecodeArray> MakeBytecodeForFunctionBody(const char* body) {
68
    ScopedVector<char> program(3072);
69 70 71 72
    SNPrintF(program, "function %s() { %s }\n%s();", kFunctionName, body,
             kFunctionName);
    return MakeBytecode(program.start(), kFunctionName);
  }
73 74

  Handle<BytecodeArray> MakeBytecodeForFunction(const char* function) {
75
    ScopedVector<char> program(3072);
76 77 78
    SNPrintF(program, "%s\n%s();", function, kFunctionName);
    return MakeBytecode(program.start(), kFunctionName);
  }
79 80

  Handle<BytecodeArray> MakeBytecodeForFunctionNoFilter(const char* function) {
81
    ScopedVector<char> program(3072);
82
    SNPrintF(program, "%s\n%s();", function, kFunctionName);
83
    return MakeBytecode(program.start(), "*", kFunctionName);
84
  }
85 86 87
};


88 89 90 91
// Helper macros for handcrafting bytecode sequences.
#define B(x) static_cast<uint8_t>(Bytecode::k##x)
#define U8(x) static_cast<uint8_t>((x) & 0xff)
#define R(x) static_cast<uint8_t>(-(x) & 0xff)
92 93
#define A(x, n) R(helper.kLastParamIndex - (n) + 1 + (x))
#define THIS(n) A(0, n)
94 95 96 97 98 99 100 101 102
#if defined(V8_TARGET_LITTLE_ENDIAN)
#define U16(x) static_cast<uint8_t>((x) & 0xff),                    \
               static_cast<uint8_t>(((x) >> kBitsPerByte) & 0xff)
#elif defined(V8_TARGET_BIG_ENDIAN)
#define U16(x) static_cast<uint8_t>(((x) >> kBitsPerByte) & 0xff),   \
               static_cast<uint8_t>((x) & 0xff)
#else
#error Unknown byte ordering
#endif
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#define COMMA() ,
#define SPACE()

#define REPEAT_2(SEP, ...)  \
  __VA_ARGS__ SEP() __VA_ARGS__
#define REPEAT_4(SEP, ...)  \
  REPEAT_2(SEP, __VA_ARGS__) SEP() REPEAT_2(SEP, __VA_ARGS__)
#define REPEAT_8(SEP, ...)  \
  REPEAT_4(SEP, __VA_ARGS__) SEP() REPEAT_4(SEP, __VA_ARGS__)
#define REPEAT_16(SEP, ...)  \
  REPEAT_8(SEP, __VA_ARGS__) SEP() REPEAT_8(SEP, __VA_ARGS__)
#define REPEAT_32(SEP, ...)  \
  REPEAT_16(SEP, __VA_ARGS__) SEP() REPEAT_16(SEP, __VA_ARGS__)
#define REPEAT_64(SEP, ...)  \
  REPEAT_32(SEP, __VA_ARGS__) SEP() REPEAT_32(SEP, __VA_ARGS__)
#define REPEAT_128(SEP, ...)  \
  REPEAT_64(SEP, __VA_ARGS__) SEP() REPEAT_64(SEP, __VA_ARGS__)
#define REPEAT_256(SEP, ...)  \
  REPEAT_128(SEP, __VA_ARGS__) SEP() REPEAT_128(SEP, __VA_ARGS__)

#define REPEAT_127(SEP, ...)                                           \
  REPEAT_64(SEP, __VA_ARGS__) SEP() REPEAT_32(SEP, __VA_ARGS__) SEP()  \
  REPEAT_16(SEP, __VA_ARGS__) SEP() REPEAT_8(SEP, __VA_ARGS__) SEP()   \
  REPEAT_4(SEP, __VA_ARGS__) SEP() REPEAT_2(SEP, __VA_ARGS__) SEP()    \
  __VA_ARGS__
129

130
// Structure for containing expected bytecode snippets.
131
template<typename T, int C = 6>
132
struct ExpectedSnippet {
133
  const char* code_snippet;
134
  int frame_size;
135
  int parameter_count;
136
  int bytecode_length;
137
  const uint8_t bytecode[2048];
138
  int constant_count;
139
  T constants[C];
140 141 142
};


143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static void CheckConstant(int expected, Object* actual) {
  CHECK_EQ(expected, Smi::cast(actual)->value());
}


static void CheckConstant(double expected, Object* actual) {
  CHECK_EQ(expected, HeapNumber::cast(actual)->value());
}


static void CheckConstant(const char* expected, Object* actual) {
  Handle<String> expected_string =
      CcTest::i_isolate()->factory()->NewStringFromAsciiChecked(expected);
  CHECK(String::cast(actual)->Equals(*expected_string));
}


160 161 162 163 164
static void CheckConstant(Handle<Object> expected, Object* actual) {
  CHECK(actual == *expected || expected->StrictEquals(actual));
}


165 166 167 168 169
static void CheckConstant(InstanceType expected, Object* actual) {
  CHECK_EQ(expected, HeapObject::cast(actual)->map()->instance_type());
}


170 171
template <typename T, int C>
static void CheckBytecodeArrayEqual(const ExpectedSnippet<T, C>& expected,
172
                                    Handle<BytecodeArray> actual) {
173 174 175
  CHECK_EQ(expected.frame_size, actual->frame_size());
  CHECK_EQ(expected.parameter_count, actual->parameter_count());
  CHECK_EQ(expected.bytecode_length, actual->length());
176
  if (expected.constant_count == 0) {
177
    CHECK_EQ(CcTest::heap()->empty_fixed_array(), actual->constant_pool());
178
  } else {
179
    CHECK_EQ(expected.constant_count, actual->constant_pool()->length());
180 181
    for (int i = 0; i < expected.constant_count; i++) {
      CheckConstant(expected.constants[i], actual->constant_pool()->get(i));
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }
  }

  BytecodeArrayIterator iterator(actual);
  int i = 0;
  while (!iterator.done()) {
    int bytecode_index = i++;
    Bytecode bytecode = iterator.current_bytecode();
    if (Bytecodes::ToByte(bytecode) != expected.bytecode[bytecode_index]) {
      std::ostringstream stream;
      stream << "Check failed: expected bytecode [" << bytecode_index
             << "] to be " << Bytecodes::ToString(static_cast<Bytecode>(
                                  expected.bytecode[bytecode_index]))
             << " but got " << Bytecodes::ToString(bytecode);
      FATAL(stream.str().c_str());
    }
198 199 200 201 202 203 204 205 206
    for (int j = 0; j < Bytecodes::NumberOfOperands(bytecode); ++j) {
      OperandType operand_type = Bytecodes::GetOperandType(bytecode, j);
      int operand_index = i;
      i += static_cast<int>(Bytecodes::SizeOfOperand(operand_type));
      uint32_t raw_operand = iterator.GetRawOperand(j, operand_type);
      uint32_t expected_operand;
      switch (Bytecodes::SizeOfOperand(operand_type)) {
        case OperandSize::kNone:
          UNREACHABLE();
207
          return;
208 209 210 211 212
        case OperandSize::kByte:
          expected_operand =
              static_cast<uint32_t>(expected.bytecode[operand_index]);
          break;
        case OperandSize::kShort:
213 214
          expected_operand =
              ReadUnalignedUInt16(&expected.bytecode[operand_index]);
215
          break;
216 217 218
        default:
          UNREACHABLE();
          return;
219 220
      }
      if (raw_operand != expected_operand) {
221 222 223
        std::ostringstream stream;
        stream << "Check failed: expected operand [" << j << "] for bytecode ["
               << bytecode_index << "] to be "
224
               << static_cast<unsigned int>(expected_operand) << " but got "
225 226 227 228 229 230 231
               << static_cast<unsigned int>(raw_operand);
        FATAL(stream.str().c_str());
      }
    }
    iterator.Advance();
  }
}
232 233 234 235 236 237


TEST(PrimitiveReturnStatements) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

238
  ExpectedSnippet<int> snippets[] = {
239
      {"", 0, 1, 2, {B(LdaUndefined), B(Return)}, 0},
240 241 242 243 244 245 246 247 248
      {"return;", 0, 1, 2, {B(LdaUndefined), B(Return)}, 0},
      {"return null;", 0, 1, 2, {B(LdaNull), B(Return)}, 0},
      {"return true;", 0, 1, 2, {B(LdaTrue), B(Return)}, 0},
      {"return false;", 0, 1, 2, {B(LdaFalse), B(Return)}, 0},
      {"return 0;", 0, 1, 2, {B(LdaZero), B(Return)}, 0},
      {"return +1;", 0, 1, 3, {B(LdaSmi8), U8(1), B(Return)}, 0},
      {"return -1;", 0, 1, 3, {B(LdaSmi8), U8(-1), B(Return)}, 0},
      {"return +127;", 0, 1, 3, {B(LdaSmi8), U8(127), B(Return)}, 0},
      {"return -128;", 0, 1, 3, {B(LdaSmi8), U8(-128), B(Return)}, 0},
249 250
  };

251
  for (size_t i = 0; i < arraysize(snippets); i++) {
252
    Handle<BytecodeArray> bytecode_array =
253
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
254
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
255 256 257 258 259 260 261 262
  }
}


TEST(PrimitiveExpressions) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

263
  ExpectedSnippet<int> snippets[] = {
264 265
      {"var x = 0; return x;",
       kPointerSize,
266
       1,
267
       4,
268 269 270
       {B(LdaZero),     //
        B(Star), R(0),  //
        B(Return)},
271
       0},
272
      {"var x = 0; return x + 3;",
273
       kPointerSize,
274
       1,
275
       8,
276 277 278
       {B(LdaZero),         //
        B(Star), R(0),      //
        B(LdaSmi8), U8(3),  //
279
        B(Add), R(0),       //
280
        B(Return)},
281
       0},
282
      {"var x = 0; return x - 3;",
283
       kPointerSize,
284
       1,
285
       8,
286 287 288
       {B(LdaZero),         //
        B(Star), R(0),      //
        B(LdaSmi8), U8(3),  //
289
        B(Sub), R(0),       //
290
        B(Return)},
291 292
       0},
      {"var x = 4; return x * 3;",
293
       kPointerSize,
294
       1,
295
       9,
296 297 298
       {B(LdaSmi8), U8(4),  //
        B(Star), R(0),      //
        B(LdaSmi8), U8(3),  //
299
        B(Mul), R(0),       //
300
        B(Return)},
301 302
       0},
      {"var x = 4; return x / 3;",
303
       kPointerSize,
304
       1,
305
       9,
306 307 308
       {B(LdaSmi8), U8(4),  //
        B(Star), R(0),      //
        B(LdaSmi8), U8(3),  //
309
        B(Div), R(0),       //
310
        B(Return)},
311 312
       0},
      {"var x = 4; return x % 3;",
313
       kPointerSize,
314
       1,
315
       9,
316 317 318
       {B(LdaSmi8), U8(4),  //
        B(Star), R(0),      //
        B(LdaSmi8), U8(3),  //
319
        B(Mod), R(0),       //
320
        B(Return)},
321 322
       0},
      {"var x = 1; return x | 2;",
323
       kPointerSize,
324
       1,
325
       9,
326 327 328
       {B(LdaSmi8), U8(1),   //
        B(Star), R(0),       //
        B(LdaSmi8), U8(2),   //
329
        B(BitwiseOr), R(0),  //
330
        B(Return)},
331 332
       0},
      {"var x = 1; return x ^ 2;",
333
       kPointerSize,
334
       1,
335
       9,
336 337 338
       {B(LdaSmi8), U8(1),    //
        B(Star), R(0),        //
        B(LdaSmi8), U8(2),    //
339
        B(BitwiseXor), R(0),  //
340
        B(Return)},
341 342
       0},
      {"var x = 1; return x & 2;",
343
       kPointerSize,
344
       1,
345
       9,
346 347 348
       {B(LdaSmi8), U8(1),    //
        B(Star), R(0),        //
        B(LdaSmi8), U8(2),    //
349
        B(BitwiseAnd), R(0),  //
350
        B(Return)},
351
       0},
352
      {"var x = 10; return x << 3;",
353
       kPointerSize,
354
       1,
355
       9,
356 357 358
       {B(LdaSmi8), U8(10),  //
        B(Star), R(0),       //
        B(LdaSmi8), U8(3),   //
359
        B(ShiftLeft), R(0),  //
360
        B(Return)},
361 362
       0},
      {"var x = 10; return x >> 3;",
363
       kPointerSize,
364
       1,
365
       9,
366 367 368
       {B(LdaSmi8), U8(10),   //
        B(Star), R(0),        //
        B(LdaSmi8), U8(3),    //
369
        B(ShiftRight), R(0),  //
370
        B(Return)},
371 372
       0},
      {"var x = 10; return x >>> 3;",
373
       kPointerSize,
374
       1,
375
       9,
376 377 378
       {B(LdaSmi8), U8(10),          //
        B(Star), R(0),               //
        B(LdaSmi8), U8(3),           //
379
        B(ShiftRightLogical), R(0),  //
380 381 382
        B(Return)},
       0},
      {"var x = 0; return (x, 3);",
383
       kPointerSize,
384
       1,
385
       6,
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
       {B(LdaZero),         //
        B(Star), R(0),      //
        B(LdaSmi8), U8(3),  //
        B(Return)},
       0}};

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(LogicalExpressions) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
      {"var x = 0; return x || 3;",
       1 * kPointerSize,
       1,
408
       8,
409 410 411 412 413 414
       {B(LdaZero),                     //
        B(Star), R(0),                  //
        B(JumpIfToBooleanTrue), U8(4),  //
        B(LdaSmi8), U8(3),              //
        B(Return)},
       0},
415 416 417 418 419 420 421 422 423 424 425 426
      {"var x = 0; return (x == 1) || 3;",
       1 * kPointerSize,
       1,
       12,
       {B(LdaZero),            //
        B(Star), R(0),         //
        B(LdaSmi8), U8(1),     //
        B(TestEqual), R(0),    //
        B(JumpIfTrue), U8(4),  //
        B(LdaSmi8), U8(3),     //
        B(Return)},
       0},
427 428 429
      {"var x = 0; return x && 3;",
       1 * kPointerSize,
       1,
430
       8,
431 432 433 434 435 436
       {B(LdaZero),                      //
        B(Star), R(0),                   //
        B(JumpIfToBooleanFalse), U8(4),  //
        B(LdaSmi8), U8(3),               //
        B(Return)},
       0},
437 438 439 440 441 442 443 444 445 446 447 448
      {"var x = 0; return (x == 0) && 3;",
       1 * kPointerSize,
       1,
       11,
       {B(LdaZero),             //
        B(Star), R(0),          //
        B(LdaZero),             //
        B(TestEqual), R(0),     //
        B(JumpIfFalse), U8(4),  //
        B(LdaSmi8), U8(3),      //
        B(Return)},
       0},
449 450 451
      {"var x = 0; return x || (1, 2, 3);",
       1 * kPointerSize,
       1,
452
       8,
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
       {B(LdaZero),                     //
        B(Star), R(0),                  //
        B(JumpIfToBooleanTrue), U8(4),  //
        B(LdaSmi8), U8(3),              //
        B(Return)},
       0},
      {"var a = 2, b = 3, c = 4; return a || (a, b, a, b, c = 5, 3);",
       3 * kPointerSize,
       1,
       23,
       {B(LdaSmi8), U8(2),              //
        B(Star), R(0),                  //
        B(LdaSmi8), U8(3),              //
        B(Star), R(1),                  //
        B(LdaSmi8), U8(4),              //
        B(Star), R(2),                  //
        B(Ldar), R(0),                  //
        B(JumpIfToBooleanTrue), U8(8),  //
        B(LdaSmi8), U8(5),              //
        B(Star), R(2),                  //
        B(LdaSmi8), U8(3),              //
        B(Return)},
       0},
476
      {"var x = 1; var a = 2, b = 3; return x || ("
477
       REPEAT_32(SPACE, "a = 1, b = 2, ")
478 479 480
       "3);",
       3 * kPointerSize,
       1,
481
       275,
482 483 484 485 486 487 488 489
       {B(LdaSmi8), U8(1),                      //
        B(Star), R(0),                          //
        B(LdaSmi8), U8(2),                      //
        B(Star), R(1),                          //
        B(LdaSmi8), U8(3),                      //
        B(Star), R(2),                          //
        B(Ldar), R(0),                          //
        B(JumpIfToBooleanTrueConstant), U8(0),  //
490 491 492 493 494
        REPEAT_32(COMMA,                        //
          B(LdaSmi8), U8(1),                    //
          B(Star), R(1),                        //
          B(LdaSmi8), U8(2),                    //
          B(Star), R(2)),                       //
495 496 497
        B(LdaSmi8), U8(3),                      //
        B(Return)},
       1,
498
       {260, 0, 0, 0}},
499
      {"var x = 0; var a = 2, b = 3; return x && ("
500
       REPEAT_32(SPACE, "a = 1, b = 2, ")
501 502 503
       "3);",
       3 * kPointerSize,
       1,
504
       274,
505 506 507 508 509 510 511 512
       {B(LdaZero),                              //
        B(Star), R(0),                           //
        B(LdaSmi8), U8(2),                       //
        B(Star), R(1),                           //
        B(LdaSmi8), U8(3),                       //
        B(Star), R(2),                           //
        B(Ldar), R(0),                           //
        B(JumpIfToBooleanFalseConstant), U8(0),  //
513 514 515 516 517 518 519
        REPEAT_32(COMMA,                         //
          B(LdaSmi8), U8(1),                     //
          B(Star), R(1),                         //
          B(LdaSmi8), U8(2),                     //
          B(Star), R(2)),                        //
        B(LdaSmi8), U8(3),                       //
        B(Return)},                              //
520
       1,
521
       {260, 0, 0, 0}},
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
      {"var x = 1; var a = 2, b = 3; return (x > 3) || ("
        REPEAT_32(SPACE, "a = 1, b = 2, ")
       "3);",
       3 * kPointerSize,
       1,
       277,
       {B(LdaSmi8), U8(1),             //
        B(Star), R(0),                 //
        B(LdaSmi8), U8(2),             //
        B(Star), R(1),                 //
        B(LdaSmi8), U8(3),             //
        B(Star), R(2),                 //
        B(LdaSmi8), U8(3),             //
        B(TestGreaterThan), R(0),      //
        B(JumpIfTrueConstant), U8(0),  //
        REPEAT_32(COMMA,               //
          B(LdaSmi8), U8(1),           //
          B(Star), R(1),               //
          B(LdaSmi8), U8(2),           //
          B(Star), R(2)),              //
        B(LdaSmi8), U8(3),             //
        B(Return)},
       1,
       {260, 0, 0, 0}},
      {"var x = 0; var a = 2, b = 3; return (x < 5) && ("
        REPEAT_32(SPACE, "a = 1, b = 2, ")
       "3);",
       3 * kPointerSize,
       1,
       276,
       {B(LdaZero),                     //
        B(Star), R(0),                  //
        B(LdaSmi8), U8(2),              //
        B(Star), R(1),                  //
        B(LdaSmi8), U8(3),              //
        B(Star), R(2),                  //
        B(LdaSmi8), U8(5),              //
        B(TestLessThan), R(0),          //
        B(JumpIfFalseConstant), U8(0),  //
        REPEAT_32(COMMA,                //
          B(LdaSmi8), U8(1),            //
          B(Star), R(1),                //
          B(LdaSmi8), U8(2),            //
          B(Star), R(2)),               //
        B(LdaSmi8), U8(3),              //
        B(Return)},
       1,
       {260, 0, 0, 0}},
570 571 572 573
      {"return 0 && 3;",
       0 * kPointerSize,
       1,
       2,
574
       {B(LdaZero),  //
575 576 577 578 579 580
        B(Return)},
       0},
      {"return 1 || 3;",
       0 * kPointerSize,
       1,
       3,
581
       {B(LdaSmi8), U8(1),  //
582 583 584 585 586
        B(Return)},
       0},
      {"var x = 1; return x && 3 || 0, 1;",
       1 * kPointerSize,
       1,
587
       14,
588 589 590 591 592 593 594 595
       {B(LdaSmi8), U8(1),               //
        B(Star), R(0),                   //
        B(JumpIfToBooleanFalse), U8(4),  //
        B(LdaSmi8), U8(3),               //
        B(JumpIfToBooleanTrue), U8(3),   //
        B(LdaZero),                      //
        B(LdaSmi8), U8(1),               //
        B(Return)},
596
       0}};
597

598
  for (size_t i = 0; i < arraysize(snippets); i++) {
599
    Handle<BytecodeArray> bytecode_array =
600
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
601
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
602 603 604 605 606 607 608 609
  }
}


TEST(Parameters) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

610
  ExpectedSnippet<int> snippets[] = {
611
      {"function f() { return this; }",
612 613 614 615 616
       0,
       1,
       3,
       {B(Ldar), THIS(1), B(Return)},
       0},
617
      {"function f(arg1) { return arg1; }",
618 619 620 621 622
       0,
       2,
       3,
       {B(Ldar), A(1, 2), B(Return)},
       0},
623
      {"function f(arg1) { return this; }",
624 625 626 627 628
       0,
       2,
       3,
       {B(Ldar), THIS(2), B(Return)},
       0},
629
      {"function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return arg4; }",
630 631 632 633 634
       0,
       8,
       3,
       {B(Ldar), A(4, 8), B(Return)},
       0},
635
      {"function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return this; }",
636 637 638 639 640
       0,
       8,
       3,
       {B(Ldar), THIS(8), B(Return)},
       0},
641
      {"function f(arg1) { arg1 = 1; }",
642 643 644 645 646 647
       0,
       2,
       6,
       {B(LdaSmi8), U8(1),  //
        B(Star), A(1, 2),   //
        B(LdaUndefined),    //
648 649 650
        B(Return)},
       0},
      {"function f(arg1, arg2, arg3, arg4) { arg2 = 1; }",
651 652 653 654 655 656
       0,
       5,
       6,
       {B(LdaSmi8), U8(1),  //
        B(Star), A(2, 5),   //
        B(LdaUndefined),    //
657 658
        B(Return)},
       0},
659 660
  };

661
  for (size_t i = 0; i < arraysize(snippets); i++) {
662
    Handle<BytecodeArray> bytecode_array =
663
        helper.MakeBytecodeForFunction(snippets[i].code_snippet);
664
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
665 666 667
  }
}

668

669
TEST(IntegerConstants) {
670 671 672
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  ExpectedSnippet<int> snippets[] = {
    {"return 12345678;",
     0,
     1,
     3,
     {
       B(LdaConstant), U8(0),  //
       B(Return)               //
     },
     1,
     {12345678}},
    {"var a = 1234; return 5678;",
     1 * kPointerSize,
     1,
     7,
     {
       B(LdaConstant), U8(0),  //
       B(Star), R(0),          //
       B(LdaConstant), U8(1),  //
       B(Return)               //
     },
     2,
     {1234, 5678}},
    {"var a = 1234; return 1234;",
     1 * kPointerSize,
     1,
     7,
     {
       B(LdaConstant), U8(0),  //
       B(Star), R(0),          //
       B(LdaConstant), U8(0),  //
       B(Return)               //
     },
     1,
     {1234}}};
708

709 710 711
  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
712
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
713
  }
714
}
715 716


717 718 719 720
TEST(HeapNumberConstants) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

721 722 723
  int wide_idx = 0;

  ExpectedSnippet<double, 257> snippets[] = {
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    {"return 1.2;",
     0,
     1,
     3,
     {
       B(LdaConstant), U8(0),  //
       B(Return)               //
     },
     1,
     {1.2}},
    {"var a = 1.2; return 2.6;",
     1 * kPointerSize,
     1,
     7,
     {
       B(LdaConstant), U8(0),  //
       B(Star), R(0),          //
       B(LdaConstant), U8(1),  //
       B(Return)               //
     },
     2,
     {1.2, 2.6}},
    {"var a = 3.14; return 3.14;",
     1 * kPointerSize,
     1,
     7,
     {
       B(LdaConstant), U8(0),  //
       B(Star), R(0),          //
       B(LdaConstant), U8(1),  //
       B(Return)               //
     },
     2,
757 758 759 760 761 762 763 764 765 766 767
     {3.14, 3.14}},
    {"var a;"
     REPEAT_256(SPACE, " a = 1.414;")
     " a = 3.14;",
     1 * kPointerSize,
     1,
     1031,
     {
         REPEAT_256(COMMA,                     //
           B(LdaConstant), U8(wide_idx++),     //
           B(Star), R(0)),                     //
768
         B(LdaConstantWide), U16(wide_idx),    //
769 770 771 772 773 774 775 776
         B(Star), R(0),                        //
         B(LdaUndefined),                      //
         B(Return),                            //
     },
     257,
     {REPEAT_256(COMMA, 1.414),
      3.14}}
  };
777 778 779 780
  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
781
  }
782
}
783 784


785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
TEST(StringConstants) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<const char*> snippets[] = {
      {"return \"This is a string\";",
       0,
       1,
       3,
       {
           B(LdaConstant), U8(0),  //
           B(Return)               //
       },
       1,
       {"This is a string"}},
      {"var a = \"First string\"; return \"Second string\";",
       1 * kPointerSize,
       1,
       7,
       {
           B(LdaConstant), U8(0),  //
           B(Star), R(0),          //
           B(LdaConstant), U8(1),  //
           B(Return)               //
       },
       2,
       {"First string", "Second string"}},
      {"var a = \"Same string\"; return \"Same string\";",
       1 * kPointerSize,
       1,
       7,
       {
           B(LdaConstant), U8(0),  //
           B(Star), R(0),          //
           B(LdaConstant), U8(0),  //
           B(Return)               //
       },
       1,
       {"Same string"}}};

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
829 830 831 832
  }
}


833 834 835
TEST(PropertyLoads) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
836 837 838 839 840
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
841 842

  Handle<i::TypeFeedbackVector> vector =
843
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
844

845 846 847 848 849 850
  // These are a hack used by the LoadICXXXWide tests below.
  int wide_idx_1 = vector->GetIndex(slot1) - 2;
  int wide_idx_2 = vector->GetIndex(slot1) - 2;
  int wide_idx_3 = vector->GetIndex(slot1) - 2;
  int wide_idx_4 = vector->GetIndex(slot1) - 2;

851 852
  ExpectedSnippet<const char*> snippets[] = {
      {"function f(a) { return a.name; }\nf({name : \"test\"})",
853
       0,
854
       2,
855
       5,
856
       {
857 858
           B(LoadICSloppy), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Return),                                                     //
859
       },
860 861
       1,
       {"name"}},
862
      {"function f(a) { return a[\"key\"]; }\nf({key : \"test\"})",
863
       0,
864
       2,
865
       5,
866
       {
867 868
           B(LoadICSloppy), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Return)                                                      //
869
       },
870 871
       1,
       {"key"}},
872
      {"function f(a) { return a[100]; }\nf({100 : \"test\"})",
873
       0,
874
       2,
875
       6,
876
       {
877 878 879
           B(LdaSmi8), U8(100),                                         //
           B(KeyedLoadICSloppy), A(1, 2), U8(vector->GetIndex(slot1)),  //
           B(Return)                                                    //
880 881
       },
       0},
882
      {"function f(a, b) { return a[b]; }\nf({arg : \"test\"}, \"arg\")",
883
       0,
884
       3,
885
       6,
886
       {
887 888 889
           B(Ldar), A(1, 2),                                            //
           B(KeyedLoadICSloppy), A(1, 3), U8(vector->GetIndex(slot1)),  //
           B(Return)                                                    //
890 891
       },
       0},
892 893
      {"function f(a) { var b = a.name; return a[-124]; }\n"
       "f({\"-124\" : \"test\", name : 123 })",
894
       kPointerSize,
895
       2,
896
       12,
897
       {
898 899 900 901 902
           B(LoadICSloppy), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Star), R(0),                                                 //
           B(LdaSmi8), U8(-124),                                          //
           B(KeyedLoadICSloppy), A(1, 2), U8(vector->GetIndex(slot2)),    //
           B(Return),                                                     //
903
       },
904
       1,
905 906
       {"name"}},
      {"function f(a) { \"use strict\"; return a.name; }\nf({name : \"test\"})",
907
       0,
908
       2,
909
       5,
910
       {
911 912
           B(LoadICStrict), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Return),                                                     //
913
       },
914 915
       1,
       {"name"}},
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
      {"function f(a, b) { \"use strict\"; return a[b]; }\n"
       "f({arg : \"test\"}, \"arg\")",
       0,
       3,
       6,
       {
           B(Ldar), A(2, 3),                                            //
           B(KeyedLoadICStrict), A(1, 3), U8(vector->GetIndex(slot1)),  //
           B(Return),                                                   //
       },
       0},
      {"function f(a) {\n"
       " var b;\n" REPEAT_127(SPACE, " b = a.name; ") " return a.name; }\n"
                                                      "f({name : \"test\"})\n",
       1 * kPointerSize,
       2,
       767,
       {
           REPEAT_127(COMMA,  //
                      B(LoadICSloppy), A(1, 2), U8(0),
                      U8((wide_idx_1 += 2)),  //
                      B(Star), R(0)),         //
           B(LoadICSloppy),
           A(1, 2), U8(0), U8(wide_idx_1 + 2),  //
           B(Return),                           //
       },
       1,
       {"name"}},
      {"function f(a) {\n"
       " 'use strict'; var b;\n" REPEAT_127(
           SPACE, " b = a.name; ") " return a.name; }\n"
                                   "f({name : \"test\"})\n",
       1 * kPointerSize,
       2,
       767,
       {
           REPEAT_127(COMMA,  //
                      B(LoadICStrict), A(1, 2), U8(0),
                      U8((wide_idx_2 += 2)),  //
                      B(Star), R(0)),         //
           B(LoadICStrict),
           A(1, 2), U8(0), U8(wide_idx_2 + 2),  //
           B(Return),                           //
       },
       1,
       {"name"}},
      {"function f(a, b) {\n"
       " var c;\n" REPEAT_127(SPACE,
                              " c = a[b]; ") " return a[b]; }\n"
                                             "f({name : \"test\"}, \"name\")\n",
       1 * kPointerSize,
       3,
       895,
       {
           REPEAT_127(COMMA,                                                 //
                      B(Ldar), A(2, 3),                                      //
                      B(KeyedLoadICSloppy), A(1, 3), U8((wide_idx_3 += 2)),  //
                      B(Star), R(0)),                                        //
           B(Ldar),
           A(2, 3),                                            //
           B(KeyedLoadICSloppy), A(1, 3), U8(wide_idx_3 + 2),  //
           B(Return),                                          //
       }},
      {"function f(a, b) {\n"
       " 'use strict'; var c;\n" REPEAT_127(
           SPACE, " c = a[b]; ") " return a[b]; }\n"
                                 "f({name : \"test\"}, \"name\")\n",
       1 * kPointerSize,
       3,
       895,
       {
           REPEAT_127(COMMA,                                                 //
                      B(Ldar), A(2, 3),                                      //
                      B(KeyedLoadICStrict), A(1, 3), U8((wide_idx_4 += 2)),  //
                      B(Star), R(0)),                                        //
           B(Ldar),
           A(2, 3),                                            //
           B(KeyedLoadICStrict), A(1, 3), U8(wide_idx_4 + 2),  //
           B(Return),                                          //
       }},
  };
997
  for (size_t i = 0; i < arraysize(snippets); i++) {
998
    Handle<BytecodeArray> bytecode_array =
999
        helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
1000
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1001 1002 1003
  }
}

1004 1005 1006 1007

TEST(PropertyStores) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
1008 1009 1010 1011 1012
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddStoreICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();
1013 1014

  Handle<i::TypeFeedbackVector> vector =
1015
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
1016

1017 1018 1019 1020 1021 1022
  // These are a hack used by the StoreICXXXWide tests below.
  int wide_idx_1 = vector->GetIndex(slot1) - 2;
  int wide_idx_2 = vector->GetIndex(slot1) - 2;
  int wide_idx_3 = vector->GetIndex(slot1) - 2;
  int wide_idx_4 = vector->GetIndex(slot1) - 2;

1023 1024
  ExpectedSnippet<const char*> snippets[] = {
      {"function f(a) { a.name = \"val\"; }\nf({name : \"test\"})",
1025
       0,
1026
       2,
1027
       8,
1028
       {
1029 1030 1031 1032
           B(LdaConstant), U8(1),                                          //
           B(StoreICSloppy), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(LdaUndefined),                                                //
           B(Return),                                                      //
1033
       },
1034 1035
       2,
       {"name", "val"}},
1036
      {"function f(a) { a[\"key\"] = \"val\"; }\nf({key : \"test\"})",
1037
       0,
1038
       2,
1039
       8,
1040
       {
1041 1042 1043 1044
           B(LdaConstant), U8(1),                                          //
           B(StoreICSloppy), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(LdaUndefined),                                                //
           B(Return),                                                      //
1045
       },
1046 1047
       2,
       {"key", "val"}},
1048
      {"function f(a) { a[100] = \"val\"; }\nf({100 : \"test\"})",
1049
       kPointerSize,
1050
       2,
1051
       12,
1052
       {
1053 1054 1055 1056 1057 1058 1059
           B(LdaSmi8), U8(100),                   //
           B(Star), R(0),                         //
           B(LdaConstant), U8(0),                 //
           B(KeyedStoreICSloppy), A(1, 2), R(0),  //
           U8(vector->GetIndex(slot1)),           //
           B(LdaUndefined),                       //
           B(Return),                             //
1060
       },
1061 1062
       1,
       {"val"}},
1063
      {"function f(a, b) { a[b] = \"val\"; }\nf({arg : \"test\"}, \"arg\")",
1064
       0,
1065
       3,
1066
       8,
1067
       {
1068 1069 1070 1071 1072
           B(LdaConstant), U8(0),                    //
           B(KeyedStoreICSloppy), A(1, 3), A(2, 3),  //
           U8(vector->GetIndex(slot1)),              //
           B(LdaUndefined),                          //
           B(Return),                                //
1073
       },
1074 1075
       1,
       {"val"}},
1076 1077
      {"function f(a) { a.name = a[-124]; }\n"
       "f({\"-124\" : \"test\", name : 123 })",
1078
       0,
1079
       2,
1080
       11,
1081
       {
1082 1083 1084 1085 1086
           B(LdaSmi8), U8(-124),                                           //
           B(KeyedLoadICSloppy), A(1, 2), U8(vector->GetIndex(slot1)),     //
           B(StoreICSloppy), A(1, 2), U8(0), U8(vector->GetIndex(slot2)),  //
           B(LdaUndefined),                                                //
           B(Return),                                                      //
1087
       },
1088
       1,
1089 1090 1091
       {"name"}},
      {"function f(a) { \"use strict\"; a.name = \"val\"; }\n"
       "f({name : \"test\"})",
1092
       0,
1093
       2,
1094
       8,
1095
       {
1096 1097 1098 1099
           B(LdaConstant), U8(1),                                          //
           B(StoreICStrict), A(1, 2), U8(0), U8(vector->GetIndex(slot1)),  //
           B(LdaUndefined),                                                //
           B(Return),                                                      //
1100
       },
1101 1102
       2,
       {"name", "val"}},
1103 1104
      {"function f(a, b) { \"use strict\"; a[b] = \"val\"; }\n"
       "f({arg : \"test\"}, \"arg\")",
1105
       0,
1106
       3,
1107
       8,
1108
       {
1109 1110 1111 1112 1113
           B(LdaConstant), U8(0),                    //
           B(KeyedStoreICStrict), A(1, 3), A(2, 3),  //
           U8(vector->GetIndex(slot1)),              //
           B(LdaUndefined),                          //
           B(Return),                                //
1114
       },
1115
       1,
1116
       {"val"}},
1117 1118 1119
      {"function f(a) {\n" REPEAT_127(SPACE,
                                      " a.name = 1; ") " a.name = 2; }\n"
                                                       "f({name : \"test\"})\n",
1120 1121
       0,
       2,
1122
       770,
1123
       {
1124 1125 1126 1127 1128 1129 1130 1131 1132
           REPEAT_127(COMMA,              //
                      B(LdaSmi8), U8(1),  //
                      B(StoreICSloppy), A(1, 2), U8(0),
                      U8((wide_idx_1 += 2))),  //
           B(LdaSmi8),
           U8(2),                                                 //
           B(StoreICSloppy), A(1, 2), U8(0), U8(wide_idx_1 + 2),  //
           B(LdaUndefined),                                       //
           B(Return),                                             //
1133 1134 1135 1136
       },
       1,
       {"name"}},
      {"function f(a) {\n"
1137 1138 1139
       "'use strict';\n" REPEAT_127(SPACE,
                                    " a.name = 1; ") " a.name = 2; }\n"
                                                     "f({name : \"test\"})\n",
1140 1141
       0,
       2,
1142
       770,
1143
       {
1144 1145 1146 1147 1148 1149 1150 1151 1152
           REPEAT_127(COMMA,              //
                      B(LdaSmi8), U8(1),  //
                      B(StoreICStrict), A(1, 2), U8(0),
                      U8((wide_idx_2 += 2))),  //
           B(LdaSmi8),
           U8(2),                                                 //
           B(StoreICStrict), A(1, 2), U8(0), U8(wide_idx_2 + 2),  //
           B(LdaUndefined),                                       //
           B(Return),                                             //
1153 1154 1155
       },
       1,
       {"name"}},
1156 1157 1158
      {"function f(a, b) {\n" REPEAT_127(
           SPACE, " a[b] = 1; ") " a[b] = 2; }\n"
                                 "f({name : \"test\"})\n",
1159 1160
       0,
       3,
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
       770,
       {
           REPEAT_127(COMMA,                                    //
                      B(LdaSmi8), U8(1),                        //
                      B(KeyedStoreICSloppy), A(1, 3), A(2, 3),  //
                      U8((wide_idx_3 += 2))),                   //
           B(LdaSmi8),
           U8(2),                                    //
           B(KeyedStoreICSloppy), A(1, 3), A(2, 3),  //
           U8(wide_idx_3 + 2),                       //
           B(LdaUndefined),                          //
           B(Return),                                //
1173 1174
       }},
      {"function f(a, b) {\n"
1175 1176 1177
       "'use strict';\n" REPEAT_127(SPACE,
                                    " a[b] = 1; ") " a[b] = 2; }\n"
                                                   "f({name : \"test\"})\n",
1178 1179
       0,
       3,
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
       770,
       {
           REPEAT_127(COMMA,                                    //
                      B(LdaSmi8), U8(1),                        //
                      B(KeyedStoreICStrict), A(1, 3), A(2, 3),  //
                      U8((wide_idx_4 += 2))),                   //
           B(LdaSmi8),
           U8(2),                                    //
           B(KeyedStoreICStrict), A(1, 3), A(2, 3),  //
           U8(wide_idx_4 + 2),                       //
           B(LdaUndefined),                          //
           B(Return),                                //
1192
       }}};
1193
  for (size_t i = 0; i < arraysize(snippets); i++) {
1194
    Handle<BytecodeArray> bytecode_array =
1195
        helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
1196
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1197 1198 1199
  }
}

1200 1201 1202 1203 1204 1205

#define FUNC_ARG "new (function Obj() { this.func = function() { return; }})()"


TEST(PropertyCall) {
  InitializedHandleScope handle_scope;
1206
  BytecodeGeneratorHelper helper;
1207 1208 1209
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
1210
  FeedbackVectorSlot slot1 = feedback_spec.AddCallICSlot();
1211 1212
  FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
  USE(slot1);
1213 1214

  Handle<i::TypeFeedbackVector> vector =
1215
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
1216

1217 1218 1219
  // These are a hack used by the CallWide test below.
  int wide_idx = vector->GetIndex(slot1) - 2;

1220 1221
  ExpectedSnippet<const char*> snippets[] = {
      {"function f(a) { return a.func(); }\nf(" FUNC_ARG ")",
1222 1223
       2 * kPointerSize,
       2,
1224
       16,
1225
       {
1226 1227 1228 1229
           B(Ldar), A(1, 2),                                           //
           B(Star), R(1),                                              //
           B(LoadICSloppy), R(1), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Star), R(0),                                              //
1230
           B(Call), R(0), R(1), U8(0), U8(vector->GetIndex(slot1)),    //
1231
           B(Return),                                                  //
1232
       },
1233 1234
       1,
       {"func"}},
1235
      {"function f(a, b, c) { return a.func(b, c); }\nf(" FUNC_ARG ", 1, 2)",
1236 1237
       4 * kPointerSize,
       4,
1238
       24,
1239
       {
1240 1241 1242 1243 1244 1245 1246 1247
           B(Ldar), A(1, 4),                                           //
           B(Star), R(1),                                              //
           B(LoadICSloppy), R(1), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Star), R(0),                                              //
           B(Ldar), A(2, 4),                                           //
           B(Star), R(2),                                              //
           B(Ldar), A(3, 4),                                           //
           B(Star), R(3),                                              //
1248
           B(Call), R(0), R(1), U8(2), U8(vector->GetIndex(slot1)),    //
1249
           B(Return)                                                   //
1250
       },
1251 1252 1253 1254 1255
       1,
       {"func"}},
      {"function f(a, b) { return a.func(b + b, b); }\nf(" FUNC_ARG ", 1)",
       4 * kPointerSize,
       3,
1256
       26,
1257
       {
1258 1259 1260 1261 1262 1263 1264 1265 1266
           B(Ldar), A(1, 3),                                           //
           B(Star), R(1),                                              //
           B(LoadICSloppy), R(1), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Star), R(0),                                              //
           B(Ldar), A(2, 3),                                           //
           B(Add), A(2, 3),                                            //
           B(Star), R(2),                                              //
           B(Ldar), A(2, 3),                                           //
           B(Star), R(3),                                              //
1267
           B(Call), R(0), R(1), U8(2), U8(vector->GetIndex(slot1)),    //
1268
           B(Return),                                                  //
1269
       },
1270
       1,
1271
       {"func"}},
1272 1273
      {"function f(a) {\n" REPEAT_127(
           SPACE, " a.func;\n") " return a.func(); }\nf(" FUNC_ARG ")",
1274 1275
       2 * kPointerSize,
       2,
1276
       526,
1277
       {
1278 1279 1280 1281 1282 1283 1284 1285 1286
           REPEAT_127(COMMA,                                                  //
                      B(LoadICSloppy), A(1, 2), U8(0), U8((wide_idx += 2))),  //
           B(Ldar),
           A(1, 2),                                               //
           B(Star), R(1),                                         //
           B(LoadICSloppyWide), R(1), U16(0), U16(wide_idx + 4),  //
           B(Star), R(0),                                         //
           B(Call), R(0), R(1), U8(0), U8(wide_idx + 2),          //
           B(Return),                                             //
1287 1288 1289 1290
       },
       1,
       {"func"}},
  };
1291
  for (size_t i = 0; i < arraysize(snippets); i++) {
1292
    Handle<BytecodeArray> bytecode_array =
1293
        helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
1294 1295 1296 1297 1298 1299 1300 1301
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(LoadGlobal) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
1302
  Zone zone;
1303

1304
  FeedbackVectorSpec feedback_spec(&zone);
1305
  FeedbackVectorSlot slot = feedback_spec.AddLoadICSlot();
1306 1307 1308 1309

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

1310 1311 1312 1313
  // These are a hack used by the LdaGlobalXXXWide tests below.
  int wide_idx_1 = vector->GetIndex(slot) - 2;
  int wide_idx_2 = vector->GetIndex(slot) - 2;

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  ExpectedSnippet<const char*> snippets[] = {
      {"var a = 1;\nfunction f() { return a; }\nf()",
       0,
       1,
       4,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
           B(Return)                                               //
       },
       1,
       {"a"}},
      {"function t() { }\nfunction f() { return t; }\nf()",
       0,
       1,
       4,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
           B(Return)                                               //
       },
       1,
       {"t"}},
      {"'use strict'; var a = 1;\nfunction f() { return a; }\nf()",
       0,
       1,
       4,
       {
           B(LdaGlobalStrict), U8(0), U8(vector->GetIndex(slot)),  //
           B(Return)                                               //
       },
       1,
       {"a"}},
      {"a = 1;\nfunction f() { return a; }\nf()",
       0,
       1,
       4,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
           B(Return)                                               //
       },
       1,
       {"a"}},
1355 1356
      {"a = 1; function f(b) {\n" REPEAT_127(
           SPACE, "b.name; ") " return a; }\nf({name: 1});",
1357 1358
       0,
       2,
1359
       512,
1360
       {
1361 1362 1363 1364 1365
           REPEAT_127(COMMA,                                                  //
                      B(LoadICSloppy), A(1, 2), U8(0), U8(wide_idx_1 += 2)),  //
           B(LdaGlobalSloppy),
           U8(1), U8(wide_idx_1 + 2),  //
           B(Return),                  //
1366 1367 1368 1369
       },
       2,
       {"name", "a"}},
      {"a = 1; function f(b) {\n"
1370 1371
       " 'use strict';\n" REPEAT_127(SPACE,
                                     "b.name; ") " return a; }\nf({name: 1});",
1372 1373
       0,
       2,
1374
       512,
1375
       {
1376 1377 1378 1379 1380
           REPEAT_127(COMMA,                                                  //
                      B(LoadICStrict), A(1, 2), U8(0), U8(wide_idx_2 += 2)),  //
           B(LdaGlobalStrict),
           U8(1), U8(wide_idx_2 + 2),  //
           B(Return),                  //
1381 1382 1383
       },
       2,
       {"name", "a"}},
1384 1385
  };

1386
  for (size_t i = 0; i < arraysize(snippets); i++) {
1387 1388
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
1389
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1390 1391 1392 1393
  }
}


1394 1395 1396
TEST(StoreGlobal) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
1397 1398 1399
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
1400
  FeedbackVectorSlot slot = feedback_spec.AddStoreICSlot();
1401 1402 1403 1404

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

1405 1406 1407 1408
  // These are a hack used by the StaGlobalXXXWide tests below.
  int wide_idx_1 = vector->GetIndex(slot) - 2;
  int wide_idx_2 = vector->GetIndex(slot) - 2;

1409
  ExpectedSnippet<const char*> snippets[] = {
1410 1411
      {"var a = 1;\nfunction f() { a = 2; }\nf()",
       0,
1412
       1,
1413 1414 1415 1416 1417 1418 1419
       7,
       {
           B(LdaSmi8), U8(2),                                      //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
           B(LdaUndefined),                                        //
           B(Return)                                               //
       },
1420 1421
       1,
       {"a"}},
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
      {"var a = \"test\"; function f(b) { a = b; }\nf(\"global\")",
       0,
       2,
       7,
       {
           B(Ldar), R(helper.kLastParamIndex),                     //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
           B(LdaUndefined),                                        //
           B(Return)                                               //
       },
1432
       1,
1433 1434 1435
       {"a"}},
      {"'use strict'; var a = 1;\nfunction f() { a = 2; }\nf()",
       0,
1436
       1,
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
       7,
       {
           B(LdaSmi8), U8(2),                                      //
           B(StaGlobalStrict), U8(0), U8(vector->GetIndex(slot)),  //
           B(LdaUndefined),                                        //
           B(Return)                                               //
       },
       1,
       {"a"}},
      {"a = 1;\nfunction f() { a = 2; }\nf()",
       0,
       1,
       7,
       {
           B(LdaSmi8), U8(2),                                      //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
           B(LdaUndefined),                                        //
           B(Return)                                               //
       },
       1,
       {"a"}},
1458 1459
      {"a = 1; function f(b) {\n" REPEAT_127(
           SPACE, "b.name; ") " a = 2; }\nf({name: 1});",
1460 1461
       0,
       2,
1462
       515,
1463
       {
1464 1465 1466 1467 1468 1469 1470
           REPEAT_127(COMMA,                                                  //
                      B(LoadICSloppy), A(1, 2), U8(0), U8(wide_idx_1 += 2)),  //
           B(LdaSmi8),
           U8(2),                                          //
           B(StaGlobalSloppy), U8(1), U8(wide_idx_1 + 2),  //
           B(LdaUndefined),                                //
           B(Return),                                      //
1471 1472 1473 1474
       },
       2,
       {"name", "a"}},
      {"a = 1; function f(b) {\n"
1475 1476
       " 'use strict';\n" REPEAT_127(SPACE,
                                     "b.name; ") " a = 2; }\nf({name: 1});",
1477 1478
       0,
       2,
1479
       515,
1480
       {
1481 1482 1483 1484 1485 1486 1487
           REPEAT_127(COMMA,                                                  //
                      B(LoadICStrict), A(1, 2), U8(0), U8(wide_idx_2 += 2)),  //
           B(LdaSmi8),
           U8(2),                                          //
           B(StaGlobalStrict), U8(1), U8(wide_idx_2 + 2),  //
           B(LdaUndefined),                                //
           B(Return),                                      //
1488 1489 1490
       },
       2,
       {"name", "a"}},
1491 1492 1493 1494 1495
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
1496
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1497 1498 1499 1500
  }
}


1501
TEST(CallGlobal) {
1502 1503 1504 1505 1506
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
1507
  FeedbackVectorSlot slot1 = feedback_spec.AddCallICSlot();
1508 1509
  FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
  USE(slot1);
1510 1511 1512 1513 1514

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<const char*> snippets[] = {
1515 1516
      {"function t() { }\nfunction f() { return t(); }\nf()",
       2 * kPointerSize,
1517
       1,
1518
       14,
1519
       {
1520 1521 1522 1523 1524 1525
           B(LdaUndefined),                                          //
           B(Star), R(1),                                            //
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),   //
           B(Star), R(0),                                            //
           B(Call), R(0), R(1), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Return)                                                 //
1526
       },
1527
       1,
1528 1529 1530
       {"t"}},
      {"function t(a, b, c) { }\nfunction f() { return t(1, 2, 3); }\nf()",
       5 * kPointerSize,
1531
       1,
1532
       26,
1533
       {
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
           B(LdaUndefined),                                          //
           B(Star), R(1),                                            //
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),   //
           B(Star), R(0),                                            //
           B(LdaSmi8), U8(1),                                        //
           B(Star), R(2),                                            //
           B(LdaSmi8), U8(2),                                        //
           B(Star), R(3),                                            //
           B(LdaSmi8), U8(3),                                        //
           B(Star), R(4),                                            //
           B(Call), R(0), R(1), U8(3), U8(vector->GetIndex(slot1)),  //
           B(Return)                                                 //
1546
       },
1547 1548 1549 1550
       1,
       {"t"}},
  };

1551 1552
  size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]);
  for (size_t i = 0; i < num_snippets; i++) {
1553 1554
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
1555
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1556 1557 1558 1559
  }
}


1560 1561 1562 1563
TEST(CallRuntime) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

1564
  ExpectedSnippet<InstanceType> snippets[] = {
1565 1566
      {
          "function f() { %TheHole() }\nf()",
1567
          0,
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
          1,
          7,
          {
              B(CallRuntime), U16(Runtime::kTheHole), R(0), U8(0),  //
              B(LdaUndefined),                                      //
              B(Return)                                             //
          },
      },
      {
          "function f(a) { return %IsArray(a) }\nf(undefined)",
          1 * kPointerSize,
          2,
          10,
          {
1582
              B(Ldar), A(1, 2),                                     //
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
              B(Star), R(0),                                        //
              B(CallRuntime), U16(Runtime::kIsArray), R(0), U8(1),  //
              B(Return)                                             //
          },
      },
      {
          "function f() { return %Add(1, 2) }\nf()",
          2 * kPointerSize,
          1,
          14,
          {
              B(LdaSmi8), U8(1),                                //
              B(Star), R(0),                                    //
              B(LdaSmi8), U8(2),                                //
              B(Star), R(1),                                    //
              B(CallRuntime), U16(Runtime::kAdd), R(0), U8(2),  //
              B(Return)                                         //
          },
1601
      },
1602 1603 1604 1605
      {
          "function f() { return %spread_iterable([1]) }\nf()",
          2 * kPointerSize,
          1,
1606
          15,
1607 1608 1609
          {
              B(LdaUndefined),                                              //
              B(Star), R(0),                                                //
1610
              B(CreateArrayLiteral), U8(0), U8(0), U8(3),                   //
1611 1612 1613 1614 1615 1616 1617 1618
              B(Star), R(1),                                                //
              B(CallJSRuntime), U16(Context::SPREAD_ITERABLE_INDEX), R(0),  //
              U8(1),                                                        //
              B(Return),                                                    //
          },
          1,
          {InstanceType::FIXED_ARRAY_TYPE},
      },
1619 1620
  };

1621
  for (size_t i = 0; i < arraysize(snippets); i++) {
1622 1623
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
1624
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1625 1626 1627
  }
}

1628 1629 1630 1631 1632 1633 1634 1635

TEST(IfConditions) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  Handle<Object> unused = helper.factory()->undefined_value();

  ExpectedSnippet<Handle<Object>> snippets[] = {
1636
      {"function f() { if (0) { return 1; } else { return -1; } } f()",
1637 1638
       0,
       1,
1639 1640 1641 1642 1643
       3,
       {
           B(LdaSmi8), U8(-1),  //
           B(Return),           //
       },
1644
       0,
1645
       {unused, unused, unused, unused, unused, unused}},
1646
      {"function f() { if ('lucky') { return 1; } else { return -1; } } f();",
1647 1648
       0,
       1,
1649 1650 1651 1652 1653
       3,
       {
           B(LdaSmi8), U8(1),  //
           B(Return),          //
       },
1654 1655
       0,
       {unused, unused, unused, unused, unused, unused}},
1656
      {"function f() { if (false) { return 1; } else { return -1; } } f();",
1657 1658
       0,
       1,
1659 1660 1661 1662 1663
       3,
       {
           B(LdaSmi8), U8(-1),  //
           B(Return),           //
       },
1664 1665 1666 1667 1668 1669
       0,
       {unused, unused, unused, unused, unused, unused}},
      {"function f() { if (false) { return 1; } } f();",
       0,
       1,
       2,
1670 1671 1672 1673 1674 1675 1676 1677 1678
       {
           B(LdaUndefined),  //
           B(Return),        //
       },
       0,
       {unused, unused, unused, unused, unused, unused}},
      {"function f() { var a = 1; if (a) { a += 1; } else { return 2; } } f();",
       1 * kPointerSize,
       1,
1679
       19,
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
       {
           B(LdaSmi8), U8(1),                //
           B(Star), R(0),                    //
           B(JumpIfToBooleanFalse), U8(10),  //
           B(LdaSmi8), U8(1),                //
           B(Add), R(0),                     //
           B(Star), R(0),                    //
           B(Jump), U8(5),                   //
           B(LdaSmi8), U8(2),                //
           B(Return),                        //
           B(LdaUndefined),                  //
           B(Return),                        //
       },
1693
       0,
1694
       {unused, unused, unused, unused, unused, unused}},
1695 1696
      {"function f(a) { if (a <= 0) { return 200; } else { return -200; } }"
       "f(99);",
1697
       0,
1698
       2,
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
       13,
       {
           B(LdaZero),                       //
           B(TestLessThanOrEqual), A(1, 2),  //
           B(JumpIfFalse), U8(5),            //
           B(LdaConstant), U8(0),            //
           B(Return),                        //
           B(LdaConstant), U8(1),            //
           B(Return),                        //
           B(LdaUndefined),                  //
           B(Return),                        //
       },
1711 1712
       2,
       {helper.factory()->NewNumberFromInt(200),
1713 1714
        helper.factory()->NewNumberFromInt(-200), unused, unused, unused,
        unused}},
1715 1716
      {"function f(a, b) { if (a in b) { return 200; } }"
       "f('prop', { prop: 'yes'});",
1717
       0,
1718
       3,
1719
       11,
1720 1721 1722 1723 1724 1725 1726 1727 1728
       {
           B(Ldar), A(2, 3),       //
           B(TestIn), A(1, 3),     //
           B(JumpIfFalse), U8(5),  //
           B(LdaConstant), U8(0),  //
           B(Return),              //
           B(LdaUndefined),        //
           B(Return),              //
       },
1729
       1,
1730 1731
       {helper.factory()->NewNumberFromInt(200), unused, unused, unused, unused,
        unused}},
1732
      {"function f(z) { var a = 0; var b = 0; if (a === 0.01) { "
1733
       REPEAT_64(SPACE, "b = a; a = b; ")
1734
       " return 200; } else { return -200; } } f(0.001)",
1735
       2 * kPointerSize,
1736
       2,
1737
       278,
1738 1739 1740 1741 1742 1743 1744 1745
       {
           B(LdaZero),                     //
           B(Star), R(0),                  //
           B(LdaZero),                     //
           B(Star), R(1),                  //
           B(LdaConstant), U8(0),          //
           B(TestEqualStrict), R(0),       //
           B(JumpIfFalseConstant), U8(2),  //
1746 1747
           B(Ldar), R(0),                  //
           REPEAT_64(COMMA,                //
1748 1749
             B(Star), R(1),                //
             B(Star), R(0)),               //
1750 1751 1752 1753 1754 1755
           B(LdaConstant), U8(1),          //
           B(Return),                      //
           B(LdaConstant), U8(3),          //
           B(Return),                      //
           B(LdaUndefined),                //
           B(Return)},                     //
1756 1757 1758
       4,
       {helper.factory()->NewHeapNumber(0.01),
        helper.factory()->NewNumberFromInt(200),
1759
        helper.factory()->NewNumberFromInt(263),
1760
        helper.factory()->NewNumberFromInt(-200), unused, unused}},
1761
      {"function f() { var a = 0; var b = 0; if (a) { "
1762
       REPEAT_64(SPACE, "b = a; a = b; ")
1763 1764 1765
       " return 200; } else { return -200; } } f()",
       2 * kPointerSize,
       1,
1766
       276,
1767 1768 1769 1770 1771 1772 1773
       {
           B(LdaZero),                              //
           B(Star), R(0),                           //
           B(LdaZero),                              //
           B(Star), R(1),                           //
           B(Ldar), R(0),                           //
           B(JumpIfToBooleanFalseConstant), U8(1),  //
1774 1775
           B(Ldar), R(0),                           //
           REPEAT_64(COMMA,                         //
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
             B(Star), R(1),                         //
             B(Star), R(0)),                        //
           B(LdaConstant), U8(0),                   //
           B(Return),                               //
           B(LdaConstant), U8(2),                   //
           B(Return),                               //
           B(LdaUndefined),                         //
           B(Return)},                              //
       3,
       {helper.factory()->NewNumberFromInt(200),
1786
        helper.factory()->NewNumberFromInt(263),
1787 1788
        helper.factory()->NewNumberFromInt(-200), unused, unused, unused}},

1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
      {"function f(a, b) {\n"
       "  if (a == b) { return 1; }\n"
       "  if (a === b) { return 1; }\n"
       "  if (a < b) { return 1; }\n"
       "  if (a > b) { return 1; }\n"
       "  if (a <= b) { return 1; }\n"
       "  if (a >= b) { return 1; }\n"
       "  if (a in b) { return 1; }\n"
       "  if (a instanceof b) { return 1; }\n"
       "  return 0;\n"
       "} f(1, 1);",
1800
       0,
1801
       3,
1802
       74,
1803
       {
1804
#define IF_CONDITION_RETURN(condition) \
1805
         B(Ldar), A(2, 3),             \
1806 1807 1808 1809
         B(condition), A(1, 3),        \
         B(JumpIfFalse), U8(5),        \
         B(LdaSmi8), U8(1),            \
         B(Return),
1810 1811 1812 1813 1814 1815 1816 1817
           IF_CONDITION_RETURN(TestEqual)               //
           IF_CONDITION_RETURN(TestEqualStrict)         //
           IF_CONDITION_RETURN(TestLessThan)            //
           IF_CONDITION_RETURN(TestGreaterThan)         //
           IF_CONDITION_RETURN(TestLessThanOrEqual)     //
           IF_CONDITION_RETURN(TestGreaterThanOrEqual)  //
           IF_CONDITION_RETURN(TestIn)                  //
           IF_CONDITION_RETURN(TestInstanceOf)          //
1818 1819
           B(LdaZero),                                  //
           B(Return)},                                  //
1820 1821
#undef IF_CONDITION_RETURN
       0,
1822
       {unused, unused, unused, unused, unused, unused}},
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
      {"function f() {"
       " var a = 0;"
       " if (a) {"
       "  return 20;"
       "} else {"
       "  return -20;}"
       "};"
       "f();",
       1 * kPointerSize,
       1,
1833
       13,
1834 1835 1836
       {
           B(LdaZero),                      //
           B(Star), R(0),                   //
1837
           B(JumpIfToBooleanFalse), U8(5),  //
1838 1839 1840 1841 1842 1843 1844 1845
           B(LdaSmi8), U8(20),              //
           B(Return),                       //
           B(LdaSmi8), U8(-20),             //
           B(Return),                       //
           B(LdaUndefined),                 //
           B(Return)
       },
       0,
1846
       {unused, unused, unused, unused, unused, unused}}};
1847 1848 1849

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
1850
        helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
1851 1852 1853 1854 1855
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


1856 1857 1858
TEST(DeclareGlobals) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
1859 1860 1861
  Zone zone;

  // Create different feedback vector specs to be precise on slot numbering.
1862 1863 1864 1865
  FeedbackVectorSpec feedback_spec_stores(&zone);
  FeedbackVectorSlot store_slot_1 = feedback_spec_stores.AddStoreICSlot();
  FeedbackVectorSlot store_slot_2 = feedback_spec_stores.AddStoreICSlot();
  USE(store_slot_1);
1866

1867 1868
  Handle<i::TypeFeedbackVector> store_vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec_stores);
1869

1870 1871
  FeedbackVectorSpec feedback_spec_loads(&zone);
  FeedbackVectorSlot load_slot_1 = feedback_spec_loads.AddLoadICSlot();
1872
  FeedbackVectorSlot call_slot_1 = feedback_spec_loads.AddCallICSlot();
1873

1874 1875
  Handle<i::TypeFeedbackVector> load_vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec_loads);
1876 1877 1878 1879 1880 1881 1882

  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a = 1;",
       4 * kPointerSize,
       1,
       30,
       {
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
           B(LdaConstant), U8(0),                                            //
           B(Star), R(1),                                                    //
           B(LdaZero),                                                       //
           B(Star), R(2),                                                    //
           B(CallRuntime), U16(Runtime::kDeclareGlobals), R(1), U8(2),       //
           B(LdaConstant), U8(1),                                            //
           B(Star), R(1),                                                    //
           B(LdaZero),                                                       //
           B(Star), R(2),                                                    //
           B(LdaSmi8), U8(1),                                                //
           B(Star), R(3),                                                    //
           B(CallRuntime), U16(Runtime::kInitializeVarGlobal), R(1), U8(3),  //
           B(LdaUndefined),                                                  //
           B(Return)                                                         //
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"function f() {}",
       2 * kPointerSize,
       1,
       14,
       {
           B(LdaConstant), U8(0),                                       //
           B(Star), R(0),                                               //
           B(LdaZero),                                                  //
           B(Star), R(1),                                               //
           B(CallRuntime), U16(Runtime::kDeclareGlobals), R(0), U8(2),  //
           B(LdaUndefined),                                             //
           B(Return)                                                    //
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 1;\na=2;",
       4 * kPointerSize,
       1,
1919
       36,
1920
       {
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
           B(LdaConstant), U8(0),                                            //
           B(Star), R(1),                                                    //
           B(LdaZero),                                                       //
           B(Star), R(2),                                                    //
           B(CallRuntime), U16(Runtime::kDeclareGlobals), R(1), U8(2),       //
           B(LdaConstant), U8(1),                                            //
           B(Star), R(1),                                                    //
           B(LdaZero),                                                       //
           B(Star), R(2),                                                    //
           B(LdaSmi8), U8(1),                                                //
           B(Star), R(3),                                                    //
           B(CallRuntime), U16(Runtime::kInitializeVarGlobal), R(1), U8(3),  //
           B(LdaSmi8), U8(2),                                                //
           B(StaGlobalSloppy), U8(1),                                        //
                               U8(store_vector->GetIndex(store_slot_2)),     //
           B(Star), R(0),                                                    //
           B(Return)                                                         //
1938 1939 1940 1941 1942
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"function f() {}\nf();",
1943
       3 * kPointerSize,
1944
       1,
1945
       28,
1946
       {
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
           B(LdaConstant), U8(0),                                        //
           B(Star), R(1),                                                //
           B(LdaZero),                                                   //
           B(Star), R(2),                                                //
           B(CallRuntime), U16(Runtime::kDeclareGlobals), R(1), U8(2),   //
           B(LdaUndefined),                                              //
           B(Star), R(2),                                                //
           B(LdaGlobalSloppy), U8(1),                                    //
                               U8(load_vector->GetIndex(load_slot_1)),   //
           B(Star), R(1),                                                //
           B(Call), R(1), R(2), U8(0),                                   //
                                U8(load_vector->GetIndex(call_slot_1)),  //
           B(Star), R(0),                                                //
           B(Return)                                                     //
1961 1962 1963 1964 1965 1966 1967 1968 1969
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeTopLevelBytecode(snippets[i].code_snippet);
1970
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
1971 1972 1973 1974
  }
}


1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
TEST(BreakableBlocks) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
      {"var x = 0;\n"
       "label: {\n"
       "  x = x + 1;\n"
       "  break label;\n"
       "  x = x + 1;\n"
       "}\n"
       "return x;",
       1 * kPointerSize,
       1,
       14,
       {
           B(LdaZero),         //
           B(Star), R(0),      //
           B(LdaSmi8), U8(1),  //
           B(Add), R(0),       //
           B(Star), R(0),      //
           B(Jump), U8(2),     //
           B(Ldar), R(0),      //
           B(Return)           //
       }},
      {"var sum = 0;\n"
       "outer: {\n"
       "  for (var x = 0; x < 10; ++x) {\n"
       "    for (var y = 0; y < 3; ++y) {\n"
       "      ++sum;\n"
       "      if (x + y == 12) { break outer; }\n"
       "    }\n"
       "  }\n"
       "}\n"
       "return sum;",
       4 * kPointerSize,
       1,
       60,
       {
           B(LdaZero),              //
           B(Star), R(0),           //
           B(LdaZero),              //
           B(Star), R(1),           //
           B(LdaSmi8), U8(10),      //
           B(TestLessThan), R(1),   //
           B(JumpIfFalse), U8(47),  //
           B(LdaZero),              //
           B(Star), R(2),           //
           B(LdaSmi8), U8(3),       //
           B(TestLessThan), R(2),   //
           B(JumpIfFalse), U8(30),  //
           B(Ldar), R(0),           //
           B(ToNumber),             //
           B(Inc),                  //
           B(Star), R(0),           //
           B(Ldar), R(2),           //
           B(Add), R(1),            //
           B(Star), R(3),           //
           B(LdaSmi8), U8(12),      //
           B(TestEqual), R(3),      //
           B(JumpIfFalse), U8(4),   //
           B(Jump), U8(18),         //
           B(Ldar), R(2),           //
           B(ToNumber),             //
           B(Inc),                  //
           B(Star), R(2),           //
           B(Jump), U8(-32),        //
           B(Ldar), R(1),           //
           B(ToNumber),             //
           B(Inc),                  //
           B(Star), R(1),           //
           B(Jump), U8(-49),        //
           B(Ldar), R(0),           //
           B(Return),               //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


2060 2061 2062 2063 2064
TEST(BasicLoops) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
      {"var x = 0;\n"
       "while (false) { x = 99; break; continue; }\n"
       "return x;",
       1 * kPointerSize,
       1,
       4,
       {
           B(LdaZero),     //
           B(Star), R(0),  //
           B(Return)       //
       }},
      {"var x = 0;"
       "while (false) {"
       "  x = x + 1;"
       "};"
       "return x;",
       1 * kPointerSize,
       1,
       4,
       {
           B(LdaZero),     //
           B(Star), R(0),  //
           B(Return),      //
       },
       0},
2090 2091 2092
      {"var x = 0;"
       "var y = 1;"
       "while (x < 10) {"
2093
       "  y = y * 12;"
2094
       "  x = x + 1;"
2095 2096
       "  if (x == 3) continue;"
       "  if (x == 4) break;"
2097 2098
       "}"
       "return y;",
2099
       2 * kPointerSize,
2100
       1,
2101
       46,
2102 2103 2104 2105 2106
       {
           B(LdaZero),              //
           B(Star), R(0),           //
           B(LdaSmi8), U8(1),       //
           B(Star), R(1),           //
2107 2108 2109
           B(LdaSmi8), U8(10),      //
           B(TestLessThan), R(0),   //
           B(JumpIfFalse), U8(32),  //
2110 2111
           B(LdaSmi8), U8(12),      //
           B(Mul), R(1),            //
2112 2113
           B(Star), R(1),           //
           B(LdaSmi8), U8(1),       //
2114
           B(Add), R(0),            //
2115
           B(Star), R(0),           //
2116 2117 2118 2119 2120 2121 2122 2123 2124
           B(LdaSmi8), U8(3),       //
           B(TestEqual), R(0),      //
           B(JumpIfFalse), U8(4),   //
           B(Jump), U8(-24),        //
           B(LdaSmi8), U8(4),       //
           B(TestEqual), R(0),      //
           B(JumpIfFalse), U8(4),   //
           B(Jump), U8(4),          //
           B(Jump), U8(-34),        //
2125 2126 2127 2128 2129
           B(Ldar), R(1),           //
           B(Return),               //
       },
       0},
      {"var i = 0;"
2130
       "while (true) {"
2131 2132 2133 2134 2135 2136 2137 2138
       "  if (i < 0) continue;"
       "  if (i == 3) break;"
       "  if (i == 4) break;"
       "  if (i == 10) continue;"
       "  if (i == 5) break;"
       "  i = i + 1;"
       "}"
       "return i;",
2139
       1 * kPointerSize,
2140
       1,
2141
       53,
2142
       {
2143 2144 2145 2146 2147
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaZero),             //
           B(TestLessThan), R(0),  //
           B(JumpIfFalse), U8(4),  //
2148
           B(Jump), U8(-5),        //
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
           B(LdaSmi8), U8(3),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(34),        //
           B(LdaSmi8), U8(4),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(26),        //
           B(LdaSmi8), U8(10),     //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
2160
           B(Jump), U8(-29),       //
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
           B(LdaSmi8), U8(5),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(10),        //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(Jump), U8(-45),       //
           B(Ldar), R(0),          //
           B(Return),              //
2171 2172 2173
       },
       0},
      {"var i = 0;"
2174
       "while (true) {"
2175 2176 2177 2178 2179 2180 2181 2182
       "  while (i < 3) {"
       "    if (i == 2) break;"
       "    i = i + 1;"
       "  }"
       "  i = i + 1;"
       "  break;"
       "}"
       "return i;",
2183
       1 * kPointerSize,
2184
       1,
2185
       38,
2186 2187 2188
       {
           B(LdaZero),              //
           B(Star), R(0),           //
2189 2190 2191
           B(LdaSmi8), U8(3),       //
           B(TestLessThan), R(0),   //
           B(JumpIfFalse), U8(18),  //
2192
           B(LdaSmi8), U8(2),       //
2193
           B(TestEqual), R(0),      //
2194
           B(JumpIfFalse), U8(4),   //
2195
           B(Jump), U8(10),         //
2196
           B(LdaSmi8), U8(1),       //
2197
           B(Add), R(0),            //
2198
           B(Star), R(0),           //
2199
           B(Jump), U8(-20),        //
2200
           B(LdaSmi8), U8(1),       //
2201
           B(Add), R(0),            //
2202
           B(Star), R(0),           //
2203 2204
           B(Jump), U8(4),          //
           B(Jump), U8(-30),        //
2205 2206 2207 2208
           B(Ldar), R(0),           //
           B(Return),               //
       },
       0},
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
      {"var x = 10;"
       "var y = 1;"
       "while (x) {"
       "  y = y * 12;"
       "  x = x - 1;"
       "}"
       "return y;",
       2 * kPointerSize,
       1,
       29,
       {
           B(LdaSmi8), U8(10),               //
           B(Star), R(0),                    //
           B(LdaSmi8), U8(1),                //
           B(Star), R(1),                    //
2224 2225
           B(Ldar), R(0),                    //
           B(JumpIfToBooleanFalse), U8(16),  //
2226 2227 2228 2229 2230 2231
           B(LdaSmi8), U8(12),               //
           B(Mul), R(1),                     //
           B(Star), R(1),                    //
           B(LdaSmi8), U8(1),                //
           B(Sub), R(0),                     //
           B(Star), R(0),                    //
2232
           B(Jump), U8(-16),                 //
2233 2234
           B(Ldar), R(1),                    //
           B(Return),                        //
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
       },
       0},
      {"var x = 0; var y = 1;"
       "do {"
       "  y = y * 10;"
       "  if (x == 5) break;"
       "  if (x == 6) continue;"
       "  x = x + 1;"
       "} while (x < 10);"
       "return y;",
       2 * kPointerSize,
       1,
       44,
       {
           B(LdaZero),              //
           B(Star), R(0),           //
           B(LdaSmi8), U8(1),       //
           B(Star), R(1),           //
           B(LdaSmi8), U8(10),      //
           B(Mul), R(1),            //
           B(Star), R(1),           //
           B(LdaSmi8), U8(5),       //
           B(TestEqual), R(0),      //
           B(JumpIfFalse), U8(4),   //
           B(Jump), U8(22),         //
           B(LdaSmi8), U8(6),       //
           B(TestEqual), R(0),      //
           B(JumpIfFalse), U8(4),   //
           B(Jump), U8(8),          //
           B(LdaSmi8), U8(1),       //
           B(Add), R(0),            //
           B(Star), R(0),           //
           B(LdaSmi8), U8(10),      //
           B(TestLessThan), R(0),   //
           B(JumpIfTrue), U8(-32),  //
           B(Ldar), R(1),           //
           B(Return),               //
       },
2273 2274
       0},
      {"var x = 10;"
2275 2276 2277 2278
       "var y = 1;"
       "do {"
       "  y = y * 12;"
       "  x = x - 1;"
2279
       "} while (x);"
2280 2281 2282 2283 2284
       "return y;",
       2 * kPointerSize,
       1,
       27,
       {
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
           B(LdaSmi8), U8(10),               //
           B(Star), R(0),                    //
           B(LdaSmi8), U8(1),                //
           B(Star), R(1),                    //
           B(LdaSmi8), U8(12),               //
           B(Mul), R(1),                     //
           B(Star), R(1),                    //
           B(LdaSmi8), U8(1),                //
           B(Sub), R(0),                     //
           B(Star), R(0),                    //
           B(Ldar), R(0),                    //
           B(JumpIfToBooleanTrue), U8(-14),  //
           B(Ldar), R(1),                    //
           B(Return),                        //
2299
       },
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
       0},
      {"var x = 0; var y = 1;"
       "do {"
       "  y = y * 10;"
       "  if (x == 5) break;"
       "  x = x + 1;"
       "  if (x == 6) continue;"
       "} while (false);"
       "return y;",
       2 * kPointerSize,
       1,
       38,
       {
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaSmi8), U8(1),      //
           B(Star), R(1),          //
           B(LdaSmi8), U8(10),     //
           B(Mul), R(1),           //
           B(Star), R(1),          //
           B(LdaSmi8), U8(5),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(16),        //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(LdaSmi8), U8(6),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(2),         //
           B(Ldar), R(1),          //
           B(Return),              //
       },
       0},
      {"var x = 0; var y = 1;"
       "do {"
       "  y = y * 10;"
       "  if (x == 5) break;"
       "  x = x + 1;"
       "  if (x == 6) continue;"
       "} while (true);"
       "return y;",
       2 * kPointerSize,
       1,
       40,
       {
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaSmi8), U8(1),      //
           B(Star), R(1),          //
           B(LdaSmi8), U8(10),     //
           B(Mul), R(1),           //
           B(Star), R(1),          //
           B(LdaSmi8), U8(5),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(18),        //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(LdaSmi8), U8(6),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
2364
           B(Jump), U8(-26),       //
2365 2366 2367 2368 2369
           B(Jump), U8(-28),       //
           B(Ldar), R(1),          //
           B(Return),              //
       },
       0},
2370 2371 2372 2373
      {"var x = 0; "
       "for (;;) {"
       "  if (x == 1) break;"
       "  if (x == 2) continue;"
2374
       "  x = x + 1;"
2375
       "}",
2376 2377
       1 * kPointerSize,
       1,
2378
       29,
2379
       {
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaSmi8), U8(1),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(18),        //
           B(LdaSmi8), U8(2),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(-14),       //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(Jump), U8(-22),       //
           B(LdaUndefined),        //
           B(Return),              //
       },
       0},
      {"for (var x = 0;;) {"
       "  if (x == 1) break;"
       "  if (x == 2) continue;"
       "  x = x + 1;"
       "}",
       1 * kPointerSize,
       1,
       29,
       {
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaSmi8), U8(1),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(18),        //
           B(LdaSmi8), U8(2),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(-14),       //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(Jump), U8(-22),       //
           B(LdaUndefined),        //
           B(Return),              //
       },
       0},
      {"var x = 0; "
       "for (;; x = x + 1) {"
       "  if (x == 1) break;"
       "  if (x == 2) continue;"
       "}",
       1 * kPointerSize,
       1,
       29,
       {
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaSmi8), U8(1),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(18),        //
           B(LdaSmi8), U8(2),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(2),         //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(Jump), U8(-22),       //
           B(LdaUndefined),        //
           B(Return),              //
       },
       0},
      {"for (var x = 0;; x = x + 1) {"
       "  if (x == 1) break;"
       "  if (x == 2) continue;"
       "}",
       1 * kPointerSize,
       1,
       29,
       {
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaSmi8), U8(1),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(18),        //
           B(LdaSmi8), U8(2),      //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(2),         //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(Jump), U8(-22),       //
           B(LdaUndefined),        //
           B(Return),              //
       },
       0},
      {"var u = 0;"
       "for (var i = 0; i < 100; i = i + 1) {"
       "  u = u + 1;"
       "  continue;"
       "}",
       2 * kPointerSize,
       1,
       30,
       {
           B(LdaZero),              //
           B(Star), R(0),           //
           B(LdaZero),              //
           B(Star), R(1),           //
           B(LdaSmi8), U8(100),     //
           B(TestLessThan), R(1),   //
           B(JumpIfFalse), U8(18),  //
           B(LdaSmi8), U8(1),       //
           B(Add), R(0),            //
           B(Star), R(0),           //
           B(Jump), U8(2),          //
           B(LdaSmi8), U8(1),       //
           B(Add), R(1),            //
           B(Star), R(1),           //
           B(Jump), U8(-20),        //
           B(LdaUndefined),         //
           B(Return),               //
       },
       0},
      {"var y = 1;"
       "for (var x = 10; x; --x) {"
       "  y = y * 12;"
       "}"
       "return y;",
       2 * kPointerSize,
       1,
       29,
       {
           B(LdaSmi8), U8(1),                //
           B(Star), R(0),                    //
           B(LdaSmi8), U8(10),               //
           B(Star), R(1),                    //
           B(Ldar), R(1),                    //
           B(JumpIfToBooleanFalse), U8(16),  //
           B(LdaSmi8), U8(12),               //
           B(Mul), R(0),                     //
           B(Star), R(0),                    //
           B(Ldar), R(1),                    //
           B(ToNumber),                      //
           B(Dec),                           //
           B(Star), R(1),                    //
           B(Jump), U8(-16),                 //
           B(Ldar), R(0),                    //
           B(Return),                        //
2531 2532 2533
       },
       0},
      {"var x = 0;"
2534
       "for (var i = 0; false; i++) {"
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
       "  x = x + 1;"
       "};"
       "return x;",
       2 * kPointerSize,
       1,
       9,
       {
           B(LdaZero),     //
           B(Star), R(0),  //
           B(LdaZero),     //
           B(Star), R(1),  //
           B(Ldar), R(0),  //
           B(Return),      //
       },
       0},
      {"var x = 0;"
2551
       "for (var i = 0; true; ++i) {"
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
       "  x = x + 1;"
       "  if (x == 20) break;"
       "};"
       "return x;",
       2 * kPointerSize,
       1,
       31,
       {
           B(LdaZero),             //
           B(Star), R(0),          //
           B(LdaZero),             //
           B(Star), R(1),          //
           B(LdaSmi8), U8(1),      //
           B(Add), R(0),           //
           B(Star), R(0),          //
           B(LdaSmi8), U8(20),     //
           B(TestEqual), R(0),     //
           B(JumpIfFalse), U8(4),  //
           B(Jump), U8(10),        //
           B(Ldar), R(1),          //
           B(ToNumber),            //
           B(Inc),                 //
           B(Star), R(1),          //
           B(Jump), U8(-20),       //
           B(Ldar), R(0),          //
           B(Return),              //
       },
       0},
  };
2581 2582 2583 2584 2585 2586 2587 2588

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599

TEST(UnaryOperators) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
      {"var x = 0;"
       "while (x != 10) {"
       "  x = x + 10;"
       "}"
       "return x;",
2600
       kPointerSize,
2601
       1,
2602
       21,
2603 2604 2605 2606
       {
           B(LdaZero),              //
           B(Star), R(0),           //
           B(LdaSmi8), U8(10),      //
2607
           B(TestEqual), R(0),      //
2608
           B(LogicalNot),           //
2609 2610 2611 2612 2613
           B(JumpIfFalse), U8(10),  //
           B(LdaSmi8), U8(10),      //
           B(Add), R(0),            //
           B(Star), R(0),           //
           B(Jump), U8(-13),        //
2614 2615 2616 2617 2618 2619 2620 2621 2622
           B(Ldar), R(0),           //
           B(Return),               //
       },
       0},
      {"var x = false;"
       "do {"
       "  x = !x;"
       "} while(x == false);"
       "return x;",
2623
       kPointerSize,
2624
       1,
2625
       16,
2626
       {
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
           B(LdaFalse),            //
           B(Star), R(0),          //
           B(Ldar), R(0),          //
           B(LogicalNot),          //
           B(Star), R(0),          //
           B(LdaFalse),            //
           B(TestEqual), R(0),     //
           B(JumpIfTrue), U8(-8),  //
           B(Ldar), R(0),          //
           B(Return),              //
2637 2638 2639 2640
       },
       0},
      {"var x = 101;"
       "return void(x * 3);",
2641
       kPointerSize,
2642
       1,
2643
       10,
2644 2645 2646 2647
       {
           B(LdaSmi8), U8(101),  //
           B(Star), R(0),        //
           B(LdaSmi8), U8(3),    //
2648
           B(Mul), R(0),         //
2649 2650 2651 2652 2653 2654 2655
           B(LdaUndefined),      //
           B(Return),            //
       },
       0},
      {"var x = 1234;"
       "var y = void (x * x - 1);"
       "return y;",
2656
       3 * kPointerSize,
2657
       1,
2658
       16,
2659 2660 2661
       {
           B(LdaConstant), U8(0),  //
           B(Star), R(0),          //
2662
           B(Mul), R(0),           //
2663 2664 2665 2666 2667 2668 2669 2670 2671
           B(Star), R(2),          //
           B(LdaSmi8), U8(1),      //
           B(Sub), R(2),           //
           B(LdaUndefined),        //
           B(Star), R(1),          //
           B(Return),              //
       },
       1,
       {1234}},
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
      {"var x = 13;"
       "return ~x;",
       1 * kPointerSize,
       1,
       9,
       {
           B(LdaSmi8), U8(13),   //
           B(Star), R(0),        //
           B(LdaSmi8), U8(-1),   //
           B(BitwiseXor), R(0),  //
           B(Return),            //
       },
       0},
      {"var x = 13;"
       "return +x;",
       1 * kPointerSize,
       1,
       9,
       {
           B(LdaSmi8), U8(13),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(1),   //
           B(Mul), R(0),        //
           B(Return),           //
       },
       0},
      {"var x = 13;"
       "return -x;",
       1 * kPointerSize,
       1,
       9,
       {
           B(LdaSmi8), U8(13),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(-1),  //
           B(Mul), R(0),        //
           B(Return),           //
       },
       0}};

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
TEST(Typeof) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot = feedback_spec.AddLoadICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<const char*> snippets[] = {
      {"function f() {\n"
       " var x = 13;\n"
       " return typeof(x);\n"
       "}; f();",
       kPointerSize,
       1,
2738
       6,
2739 2740
       {
           B(LdaSmi8), U8(13),  //
2741
           B(Star), R(0),       //
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
           B(TypeOf),           //
           B(Return),           //
       }},
      {"var x = 13;\n"
       "function f() {\n"
       " return typeof(x);\n"
       "}; f();",
       0,
       1,
       5,
       {
           B(LdaGlobalInsideTypeofSloppy), U8(0),                       //
                                           U8(vector->GetIndex(slot)),  //
           B(TypeOf),                                                   //
           B(Return),                                                   //
       },
       1,
       {"x"}},
      {"var x = 13;\n"
       "function f() {\n"
       " 'use strict';\n"
       " return typeof(x);\n"
       "}; f();",
       0,
       1,
       5,
       {
           B(LdaGlobalInsideTypeofStrict), U8(0),                       //
                                           U8(vector->GetIndex(slot)),  //
           B(TypeOf),                                                   //
           B(Return),                                                   //
       },
       1,
       {"x"}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunction(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
TEST(Delete) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  int deep_elements_flags =
      ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;
  int closure = Register::function_closure().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;

  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a = {x:13, y:14}; return delete a.x;",
       1 * kPointerSize,
       1,
2799
       11,
2800
       {
2801 2802 2803 2804
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(0),                                                  //
           B(LdaConstant), U8(1),                                          //
           B(DeletePropertySloppy), R(0),                                  //
2805 2806 2807 2808 2809 2810 2811 2812
           B(Return)
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"'use strict'; var a = {x:13, y:14}; return delete a.x;",
       1 * kPointerSize,
       1,
2813
       11,
2814
       {
2815 2816 2817 2818
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(0),                                                  //
           B(LdaConstant), U8(1),                                          //
           B(DeletePropertyStrict), R(0),                                  //
2819 2820 2821 2822 2823 2824 2825 2826
           B(Return)
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var a = {1:13, 2:14}; return delete a[2];",
       1 * kPointerSize,
       1,
2827
       11,
2828
       {
2829 2830 2831 2832
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(0),                                                  //
           B(LdaSmi8), U8(2),                                              //
           B(DeletePropertySloppy), R(0),                                  //
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
           B(Return)
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 10; return delete a;",
       1 * kPointerSize,
       1,
       6,
       {
           B(LdaSmi8), U8(10),  //
           B(Star), R(0),       //
           B(LdaFalse),         //
           B(Return)
        },
       0},
      {"'use strict';"
       "var a = {1:10};"
       "(function f1() {return a;});"
       "return delete a[1];",
       2 * kPointerSize,
       1,
2854
       27,
2855
       {
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
           B(CallRuntime), U16(Runtime::kNewFunctionContext),              //
                            R(closure), U8(1),                             //
           B(PushContext), R(0),                                           //
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(StaContextSlot), R(0), U8(first_context_slot),                //
           B(CreateClosure), U8(1), U8(0),                                 //
           B(LdaContextSlot), R(0), U8(first_context_slot),                //
           B(Star), R(1),                                                  //
           B(LdaSmi8), U8(1),                                              //
           B(DeletePropertyStrict), R(1),                                  //
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
           B(Return)
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"return delete 'test';",
       0 * kPointerSize,
       1,
       2,
       {
           B(LdaTrue),  //
           B(Return)
       },
       0},
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


2890 2891 2892 2893 2894 2895
TEST(GlobalDelete) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int context = Register::function_context().index();
2896 2897
  int native_context_index = Context::NATIVE_CONTEXT_INDEX;
  int global_context_index = Context::EXTENSION_INDEX;
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot = feedback_spec.AddLoadICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a = {x:13, y:14};\n function f() { return delete a.x; };\n f();",
       1 * kPointerSize,
       1,
       10,
2909 2910 2911 2912 2913
       {B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot)),  //
        B(Star), R(0),                                          //
        B(LdaConstant), U8(1),                                  //
        B(DeletePropertySloppy), R(0),                          //
        B(Return)},
2914 2915 2916 2917 2918 2919 2920 2921
       2,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"a = {1:13, 2:14};\n"
       "function f() {'use strict'; return delete a[1];};\n f();",
       1 * kPointerSize,
       1,
       10,
2922 2923 2924 2925 2926
       {B(LdaGlobalStrict), U8(0), U8(vector->GetIndex(slot)),  //
        B(Star), R(0),                                          //
        B(LdaSmi8), U8(1),                                      //
        B(DeletePropertyStrict), R(0),                          //
        B(Return)},
2927 2928 2929
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var a = {x:13, y:14};\n function f() { return delete a; };\n f();",
2930
       2 * kPointerSize,
2931
       1,
2932 2933 2934 2935 2936 2937 2938 2939
       15,
       {B(LdaContextSlot), R(context), U8(native_context_index),  //
        B(Star), R(0),                                            //
        B(LdaContextSlot), R(0), U8(global_context_index),        //
        B(Star), R(1),                                            //
        B(LdaConstant), U8(0),                                    //
        B(DeletePropertySloppy), R(1),                            //
        B(Return)},
2940 2941 2942
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"b = 30;\n function f() { return delete b; };\n f();",
2943
       2 * kPointerSize,
2944
       1,
2945 2946 2947 2948 2949 2950 2951 2952
       15,
       {B(LdaContextSlot), R(context), U8(native_context_index),  //
        B(Star), R(0),                                            //
        B(LdaContextSlot), R(0), U8(global_context_index),        //
        B(Star), R(1),                                            //
        B(LdaConstant), U8(0),                                    //
        B(DeletePropertySloppy), R(1),                            //
        B(Return)},
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}}};

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


2964 2965 2966
TEST(FunctionLiterals) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
2967 2968 2969 2970 2971 2972 2973
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot = feedback_spec.AddCallICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
2974 2975 2976 2977 2978

  ExpectedSnippet<InstanceType> snippets[] = {
      {"return function(){ }",
       0,
       1,
2979
       4,
2980
       {
2981 2982
           B(CreateClosure), U8(0), U8(0),  //
           B(Return)                        //
2983 2984 2985 2986 2987 2988
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"return (function(){ })()",
       2 * kPointerSize,
       1,
2989
       14,
2990
       {
2991 2992
           B(LdaUndefined),                                         //
           B(Star), R(1),                                           //
2993
           B(CreateClosure), U8(0), U8(0),                          //
2994 2995 2996
           B(Star), R(0),                                           //
           B(Call), R(0), R(1), U8(0), U8(vector->GetIndex(slot)),  //
           B(Return)                                                //
2997 2998 2999 3000 3001 3002
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"return (function(x){ return x; })(1)",
       3 * kPointerSize,
       1,
3003
       18,
3004
       {
3005 3006
           B(LdaUndefined),                                         //
           B(Star), R(1),                                           //
3007
           B(CreateClosure), U8(0), U8(0),                          //
3008 3009 3010 3011 3012
           B(Star), R(0),                                           //
           B(LdaSmi8), U8(1),                                       //
           B(Star), R(2),                                           //
           B(Call), R(0), R(1), U8(1), U8(vector->GetIndex(slot)),  //
           B(Return)                                                //
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

3025

3026 3027 3028 3029 3030 3031
TEST(RegExpLiterals) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
3032
  FeedbackVectorSlot slot1 = feedback_spec.AddCallICSlot();
3033
  FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
3034
  uint8_t i_flags = JSRegExp::kIgnoreCase;
3035 3036 3037 3038 3039 3040

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<const char*> snippets[] = {
      {"return /ab+d/;",
3041
       0 * kPointerSize,
3042
       1,
3043
       5,
3044
       {
3045 3046
           B(CreateRegExpLiteral), U8(0), U8(0), U8(0),  //
           B(Return),                                    //
3047
       },
3048 3049
       1,
       {"ab+d"}},
3050
      {"return /(\\w+)\\s(\\w+)/i;",
3051
       0 * kPointerSize,
3052
       1,
3053
       5,
3054
       {
3055 3056
           B(CreateRegExpLiteral), U8(0), U8(0), U8(i_flags),  //
           B(Return),                                          //
3057
       },
3058 3059
       1,
       {"(\\w+)\\s(\\w+)"}},
3060 3061 3062
      {"return /ab+d/.exec('abdd');",
       3 * kPointerSize,
       1,
3063
       22,
3064
       {
3065
           B(CreateRegExpLiteral), U8(0), U8(0), U8(0),                //
3066
           B(Star), R(1),                                              //
3067
           B(LoadICSloppy), R(1), U8(1), U8(vector->GetIndex(slot2)),  //
3068
           B(Star), R(0),                                              //
3069
           B(LdaConstant), U8(2),                                      //
3070
           B(Star), R(2),                                              //
3071
           B(Call), R(0), R(1), U8(1), U8(vector->GetIndex(slot1)),    //
3072
           B(Return),                                                  //
3073
       },
3074 3075
       3,
       {"ab+d", "exec", "abdd"}},
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117
TEST(RegExpLiteralsWide) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int wide_idx = 0;

  ExpectedSnippet<InstanceType, 257> snippets[] = {
      {"var a;" REPEAT_256(SPACE, "a = 1.23;") "return /ab+d/;",
       1 * kPointerSize,
       1,
       1031,
       {
           REPEAT_256(COMMA,                                     //
             B(LdaConstant), U8(wide_idx++),                     //
             B(Star), R(0)),                                     //
           B(CreateRegExpLiteralWide), U16(256), U16(0), U8(0),  //
           B(Return)                                             //
       },
       257,
       {REPEAT_256(COMMA, InstanceType::HEAP_NUMBER_TYPE),
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3118 3119 3120
TEST(ArrayLiterals) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
3121 3122 3123 3124 3125 3126 3127 3128 3129
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddKeyedStoreICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddKeyedStoreICSlot();
  FeedbackVectorSlot slot3 = feedback_spec.AddKeyedStoreICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
3130 3131 3132 3133 3134 3135 3136 3137

  int simple_flags =
      ArrayLiteral::kDisableMementos | ArrayLiteral::kShallowElements;
  int deep_elements_flags = ArrayLiteral::kDisableMementos;
  ExpectedSnippet<InstanceType> snippets[] = {
      {"return [ 1, 2 ];",
       0,
       1,
3138
       5,
3139
       {
3140 3141
           B(CreateArrayLiteral), U8(0), U8(0), U8(simple_flags),  //
           B(Return)                                               //
3142 3143 3144 3145
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 1; return [ a, a + 1 ];",
3146
       3 * kPointerSize,
3147
       1,
3148
       34,
3149
       {
3150 3151
           B(LdaSmi8), U8(1),                                               //
           B(Star), R(0),                                                   //
3152
           B(CreateArrayLiteral), U8(0), U8(0), U8(3),                      //
3153 3154 3155 3156 3157 3158 3159 3160
           B(Star), R(2),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(1),                                                   //
           B(Ldar), R(0),                                                   //
           B(KeyedStoreICSloppy), R(2), R(1), U8(vector->GetIndex(slot1)),  //
           B(LdaSmi8), U8(1),                                               //
           B(Star), R(1),                                                   //
           B(LdaSmi8), U8(1),                                               //
3161
           B(Add), R(0),                                                    //
3162 3163
           B(KeyedStoreICSloppy), R(2), R(1), U8(vector->GetIndex(slot1)),  //
           B(Ldar), R(2),                                                   //
3164
           B(Return),                                                       //
3165 3166 3167 3168 3169 3170
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"return [ [ 1, 2 ], [ 3 ] ];",
       0,
       1,
3171
       5,
3172
       {
3173 3174
           B(CreateArrayLiteral), U8(0), U8(2), U8(deep_elements_flags),  //
           B(Return)                                                      //
3175 3176 3177 3178
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 1; return [ [ a, 2 ], [ a + 2 ] ];",
3179
       5 * kPointerSize,
3180
       1,
3181
       64,
3182
       {
3183 3184
           B(LdaSmi8), U8(1),                                               //
           B(Star), R(0),                                                   //
3185
           B(CreateArrayLiteral), U8(0), U8(2), U8(deep_elements_flags),    //
3186 3187 3188
           B(Star), R(2),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(1),                                                   //
3189
           B(CreateArrayLiteral), U8(1), U8(0), U8(simple_flags),           //
3190 3191 3192 3193 3194 3195 3196 3197 3198
           B(Star), R(4),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(3),                                                   //
           B(Ldar), R(0),                                                   //
           B(KeyedStoreICSloppy), R(4), R(3), U8(vector->GetIndex(slot1)),  //
           B(Ldar), R(4),                                                   //
           B(KeyedStoreICSloppy), R(2), R(1), U8(vector->GetIndex(slot3)),  //
           B(LdaSmi8), U8(1),                                               //
           B(Star), R(1),                                                   //
3199
           B(CreateArrayLiteral), U8(2), U8(1), U8(simple_flags),           //
3200 3201 3202 3203
           B(Star), R(4),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(3),                                                   //
           B(LdaSmi8), U8(2),                                               //
3204
           B(Add), R(0),                                                    //
3205 3206 3207 3208 3209
           B(KeyedStoreICSloppy), R(4), R(3), U8(vector->GetIndex(slot2)),  //
           B(Ldar), R(4),                                                   //
           B(KeyedStoreICSloppy), R(2), R(1), U8(vector->GetIndex(slot3)),  //
           B(Ldar), R(2),                                                   //
           B(Return),                                                       //
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
       },
       3,
       {InstanceType::FIXED_ARRAY_TYPE, InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::FIXED_ARRAY_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

3223

3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
TEST(ArrayLiteralsWide) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int wide_idx = 0;
  int simple_flags =
      ArrayLiteral::kDisableMementos | ArrayLiteral::kShallowElements;

  ExpectedSnippet<InstanceType, 257> snippets[] = {
      {"var a;" REPEAT_256(SPACE, "a = 1.23;") "return [ 1 , 2 ];",
       1 * kPointerSize,
       1,
       1031,
       {
           REPEAT_256(COMMA,                                               //
             B(LdaConstant), U8(wide_idx++),                               //
             B(Star), R(0)),                                               //
           B(CreateArrayLiteralWide), U16(256), U16(0), U8(simple_flags),  //
           B(Return)                                                       //
       },
       257,
       {REPEAT_256(COMMA, InstanceType::HEAP_NUMBER_TYPE),
        InstanceType::FIXED_ARRAY_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3258 3259 3260
TEST(ObjectLiterals) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
3261 3262 3263 3264 3265 3266 3267
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddStoreICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277

  int simple_flags = ObjectLiteral::kFastElements |
                     ObjectLiteral::kShallowProperties |
                     ObjectLiteral::kDisableMementos;
  int deep_elements_flags =
      ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;
  ExpectedSnippet<InstanceType> snippets[] = {
      {"return { };",
       0,
       1,
3278
       5,
3279
       {
3280 3281
           B(CreateObjectLiteral), U8(0), U8(0), U8(simple_flags),  //
           B(Return)                                                //
3282 3283 3284 3285 3286 3287
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"return { name: 'string', val: 9.2 };",
       0,
       1,
3288
       5,
3289
       {
3290 3291
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Return)                                                       //
3292 3293 3294 3295
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 1; return { name: 'string', val: a };",
3296
       2 * kPointerSize,
3297
       1,
3298
       19,
3299
       {
3300 3301 3302 3303 3304 3305 3306 3307
           B(LdaSmi8), U8(1),                                              //
           B(Star), R(0),                                                  //
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(1),                                                  //
           B(Ldar), R(0),                                                  //
           B(StoreICSloppy), R(1), U8(1), U8(vector->GetIndex(slot1)),     //
           B(Ldar), R(1),                                                  //
           B(Return),                                                      //
3308 3309 3310 3311 3312
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var a = 1; return { val: a, val: a + 1 };",
3313
       2 * kPointerSize,
3314
       1,
3315
       21,
3316
       {
3317 3318 3319 3320 3321 3322 3323 3324 3325
           B(LdaSmi8), U8(1),                                              //
           B(Star), R(0),                                                  //
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(1),                                                  //
           B(LdaSmi8), U8(1),                                              //
           B(Add), R(0),                                                   //
           B(StoreICSloppy), R(1), U8(1), U8(vector->GetIndex(slot1)),     //
           B(Ldar), R(1),                                                  //
           B(Return),                                                      //
3326 3327 3328 3329 3330
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"return { func: function() { } };",
3331
       1 * kPointerSize,
3332
       1,
3333
       16,
3334
       {
3335 3336 3337 3338 3339 3340
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(0),                                                  //
           B(CreateClosure), U8(2), U8(0),                                 //
           B(StoreICSloppy), R(0), U8(1), U8(vector->GetIndex(slot1)),     //
           B(Ldar), R(0),                                                  //
           B(Return),                                                      //
3341 3342 3343 3344 3345 3346
       },
       3,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"return { func(a) { return a; } };",
3347
       1 * kPointerSize,
3348
       1,
3349
       16,
3350
       {
3351 3352 3353 3354 3355 3356
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(0),                                                  //
           B(CreateClosure), U8(2), U8(0),                                 //
           B(StoreICSloppy), R(0), U8(1), U8(vector->GetIndex(slot1)),     //
           B(Ldar), R(0),                                                  //
           B(Return),                                                      //
3357 3358 3359 3360 3361 3362 3363 3364
       },
       3,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"return { get a() { return 2; } };",
       5 * kPointerSize,
       1,
3365
       29,
3366
       {
3367
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),   //
3368 3369 3370
           B(Star), R(0),                                                   //
           B(LdaConstant), U8(1),                                           //
           B(Star), R(1),                                                   //
3371
           B(CreateClosure), U8(2), U8(0),                                  //
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388
           B(Star), R(2),                                                   //
           B(LdaNull),                                                      //
           B(Star), R(3),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(4),                                                   //
           B(CallRuntime), U16(Runtime::kDefineAccessorPropertyUnchecked),  //
                           R(0), U8(5),                                     //
           B(Ldar), R(0),                                                   //
           B(Return),                                                       //
       },
       3,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"return { get a() { return this.x; }, set a(val) { this.x = val } };",
       5 * kPointerSize,
       1,
3389
       31,
3390
       {
3391
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),   //
3392 3393 3394
           B(Star), R(0),                                                   //
           B(LdaConstant), U8(1),                                           //
           B(Star), R(1),                                                   //
3395
           B(CreateClosure), U8(2), U8(0),                                  //
3396
           B(Star), R(2),                                                   //
3397
           B(CreateClosure), U8(3), U8(0),                                  //
3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
           B(Star), R(3),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(4),                                                   //
           B(CallRuntime), U16(Runtime::kDefineAccessorPropertyUnchecked),  //
                           R(0), U8(5),                                     //
           B(Ldar), R(0),                                                   //
           B(Return),                                                       //
       },
       4,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
3411
      {"return { set b(val) { this.y = val } };",
3412 3413
       5 * kPointerSize,
       1,
3414
       29,
3415
       {
3416
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),   //
3417 3418 3419 3420 3421
           B(Star), R(0),                                                   //
           B(LdaConstant), U8(1),                                           //
           B(Star), R(1),                                                   //
           B(LdaNull),                                                      //
           B(Star), R(2),                                                   //
3422
           B(CreateClosure), U8(2), U8(0),                                  //
3423 3424 3425 3426 3427 3428 3429 3430
           B(Star), R(3),                                                   //
           B(LdaZero),                                                      //
           B(Star), R(4),                                                   //
           B(CallRuntime), U16(Runtime::kDefineAccessorPropertyUnchecked),  //
                           R(0), U8(5),                                     //
           B(Ldar), R(0),                                                   //
           B(Return),                                                       //
       },
3431
       3,
3432 3433 3434 3435 3436 3437
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"var a = 1; return { 1: a };",
       5 * kPointerSize,
       1,
3438
       29,
3439
       {
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
           B(LdaSmi8), U8(1),                                              //
           B(Star), R(0),                                                  //
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),  //
           B(Star), R(1),                                                  //
           B(LdaSmi8), U8(1),                                              //
           B(Star), R(2),                                                  //
           B(Ldar), R(0),                                                  //
           B(Star), R(3),                                                  //
           B(LdaZero),                                                     //
           B(Star), R(4),                                                  //
           B(CallRuntime), U16(Runtime::kSetProperty), R(1), U8(4),        //
           B(Ldar), R(1),                                                  //
           B(Return),                                                      //
3453 3454 3455 3456 3457 3458
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"return { __proto__: null }",
       2 * kPointerSize,
       1,
3459
       17,
3460
       {
3461
           B(CreateObjectLiteral), U8(0), U8(0), U8(simple_flags),            //
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
           B(Star), R(0),                                                     //
           B(LdaNull), B(Star), R(1),                                         //
           B(CallRuntime), U16(Runtime::kInternalSetPrototype), R(0), U8(2),  //
           B(Ldar), R(0),                                                     //
           B(Return),                                                         //
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 'test'; return { [a]: 1 }",
       5 * kPointerSize,
       1,
3473
       30,
3474 3475 3476
       {
           B(LdaConstant), U8(0),                                             //
           B(Star), R(0),                                                     //
3477
           B(CreateObjectLiteral), U8(1), U8(0), U8(simple_flags),            //
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496
           B(Star), R(1),                                                     //
           B(Ldar), R(0),                                                     //
           B(ToName),                                                         //
           B(Star), R(2),                                                     //
           B(LdaSmi8), U8(1),                                                 //
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(CallRuntime), U16(Runtime::kDefineDataPropertyUnchecked), R(1),  //
                           U8(4),                                             //
           B(Ldar), R(1),                                                     //
           B(Return),                                                         //
       },
       2,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 'test'; return { val: a, [a]: 1 }",
       5 * kPointerSize,
       1,
3497
       36,
3498 3499 3500
       {
           B(LdaConstant), U8(0),                                             //
           B(Star), R(0),                                                     //
3501
           B(CreateObjectLiteral), U8(1), U8(0), U8(deep_elements_flags),     //
3502 3503
           B(Star), R(1),                                                     //
           B(Ldar), R(0),                                                     //
3504
           B(StoreICSloppy), R(1), U8(2), U8(vector->GetIndex(slot1)),        //
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
           B(Ldar), R(0),                                                     //
           B(ToName),                                                         //
           B(Star), R(2),                                                     //
           B(LdaSmi8), U8(1),                                                 //
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(CallRuntime), U16(Runtime::kDefineDataPropertyUnchecked), R(1),  //
                           U8(4),                                             //
           B(Ldar), R(1),                                                     //
           B(Return),                                                         //
       },
       3,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var a = 'test'; return { [a]: 1, __proto__: {} }",
       5 * kPointerSize,
       1,
3524
       41,
3525 3526 3527
       {
           B(LdaConstant), U8(0),                                             //
           B(Star), R(0),                                                     //
3528
           B(CreateObjectLiteral), U8(1), U8(1), U8(simple_flags),            //
3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
           B(Star), R(1),                                                     //
           B(Ldar), R(0),                                                     //
           B(ToName),                                                         //
           B(Star), R(2),                                                     //
           B(LdaSmi8), U8(1),                                                 //
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(CallRuntime), U16(Runtime::kDefineDataPropertyUnchecked), R(1),  //
                           U8(4),                                             //
3539
           B(CreateObjectLiteral), U8(1), U8(0), U8(13),                      //
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
           B(Star), R(2),                                                     //
           B(CallRuntime), U16(Runtime::kInternalSetPrototype), R(1), U8(2),  //
           B(Ldar), R(1),                                                     //
           B(Return),                                                         //
       },
       2,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE}},
      {"var n = 'name'; return { [n]: 'val', get a() { }, set a(b) {} };",
       5 * kPointerSize,
       1,
3551
       64,
3552 3553 3554
       {
           B(LdaConstant), U8(0),                                             //
           B(Star), R(0),                                                     //
3555
           B(CreateObjectLiteral), U8(1), U8(0), U8(simple_flags),            //
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
           B(Star), R(1),                                                     //
           B(Ldar), R(0),                                                     //
           B(ToName),                                                         //
           B(Star), R(2),                                                     //
           B(LdaConstant), U8(2),                                             //
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(CallRuntime), U16(Runtime::kDefineDataPropertyUnchecked), R(1),  //
                           U8(4),                                             //
           B(LdaConstant), U8(3),                                             //
           B(Star), R(2),                                                     //
3568
           B(CreateClosure), U8(4), U8(0),                                    //
3569 3570 3571 3572
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(CallRuntime), U16(Runtime::kDefineGetterPropertyUnchecked),      //
3573
                           R(1), U8(4),                                       //
3574 3575
           B(LdaConstant), U8(3),                                             //
           B(Star), R(2),                                                     //
3576
           B(CreateClosure), U8(5), U8(0),                                    //
3577 3578 3579 3580
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(CallRuntime), U16(Runtime::kDefineSetterPropertyUnchecked),      //
3581
                           R(1), U8(4),                                       //
3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601
           B(Ldar), R(1),                                                     //
           B(Return),                                                         //
       },
       6,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637
TEST(ObjectLiteralsWide) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int deep_elements_flags =
      ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;
  int wide_idx = 0;

  ExpectedSnippet<InstanceType, 257> snippets[] = {
      {"var a;" REPEAT_256(SPACE,
                           "a = 1.23;") "return { name: 'string', val: 9.2 };",
       1 * kPointerSize,
       1,
       1031,
       {
           REPEAT_256(COMMA,                                     //
             B(LdaConstant), U8(wide_idx++),                     //
             B(Star), R(0)),                                     //
           B(CreateObjectLiteralWide), U16(256), U16(0),         //
                                       U8(deep_elements_flags),  //
           B(Return)                                             //
       },
       257,
       {REPEAT_256(COMMA, InstanceType::HEAP_NUMBER_TYPE),
        InstanceType::FIXED_ARRAY_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3638 3639 3640 3641
TEST(TopLevelObjectLiterals) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

3642 3643 3644 3645 3646
  int has_function_flags = ObjectLiteral::kFastElements |
                           ObjectLiteral::kHasFunction |
                           ObjectLiteral::kDisableMementos;
  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a = { func: function() { } };",
3647
       5 * kPointerSize,
3648
       1,
3649
       48,
3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
       {
           B(LdaConstant), U8(0),                                            //
           B(Star), R(1),                                                    //
           B(LdaZero),                                                       //
           B(Star), R(2),                                                    //
           B(CallRuntime), U16(Runtime::kDeclareGlobals), R(1), U8(2),       //
           B(LdaConstant), U8(1),                                            //
           B(Star), R(1),                                                    //
           B(LdaZero),                                                       //
           B(Star), R(2),                                                    //
3660
           B(CreateObjectLiteral), U8(2), U8(0), U8(has_function_flags),     //
3661
           B(Star), R(4),                                                    //
3662
           B(CreateClosure), U8(4), U8(1),                                   //
3663
           B(StoreICSloppy), R(4), U8(3), U8(3),                             //
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
           B(CallRuntime), U16(Runtime::kToFastProperties), R(4), U8(1),     //
           B(Ldar), R(4),                                                    //
           B(Star), R(3),                                                    //
           B(CallRuntime), U16(Runtime::kInitializeVarGlobal), R(1), U8(3),  //
           B(LdaUndefined),                                                  //
           B(Return),                                                        //
       },
       5,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeTopLevelBytecode(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3687 3688 3689 3690 3691 3692 3693
TEST(TryCatch) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  // TODO(rmcilroy): modify tests when we have real try catch support.
  ExpectedSnippet<int> snippets[] = {
      {"try { return 1; } catch(e) { return 2; }",
3694
       kPointerSize,
3695
       1,
3696
       3,
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718
       {
           B(LdaSmi8), U8(1),  //
           B(Return),          //
       },
       0},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(TryFinally) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  // TODO(rmcilroy): modify tests when we have real try finally support.
  ExpectedSnippet<int> snippets[] = {
      {"var a = 1; try { a = 2; } finally { a = 3; }",
3719
       kPointerSize,
3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
       1,
       14,
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(LdaSmi8), U8(2),  //
           B(Star), R(0),      //
           B(LdaSmi8), U8(3),  //
           B(Star), R(0),      //
           B(LdaUndefined),    //
           B(Return),          //
       },
       0},
      {"var a = 1; try { a = 2; } catch(e) { a = 20 } finally { a = 3; }",
3734
       2 * kPointerSize,
3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756
       1,
       14,
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(LdaSmi8), U8(2),  //
           B(Star), R(0),      //
           B(LdaSmi8), U8(3),  //
           B(Star), R(0),      //
           B(LdaUndefined),    //
           B(Return),          //
       },
       0},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

3757

3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
TEST(Throw) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  // TODO(rmcilroy): modify tests when we have real try catch support.
  ExpectedSnippet<const char*> snippets[] = {
      {"throw 1;",
       0,
       1,
       3,
       {
           B(LdaSmi8), U8(1),  //
           B(Throw),           //
       },
       0},
      {"throw 'Error';",
       0,
       1,
       3,
       {
           B(LdaConstant), U8(0),  //
           B(Throw),               //
       },
       1,
       {"Error"}},
3783 3784
      {"var a = 1; if (a) { throw 'Error'; };",
       1 * kPointerSize,
3785
       1,
3786
       11,
3787
       {
3788 3789 3790 3791 3792 3793 3794
           B(LdaSmi8), U8(1),               //
           B(Star), R(0),                   //
           B(JumpIfToBooleanFalse), U8(5),  //
           B(LdaConstant), U8(0),           //
           B(Throw),                        //
           B(LdaUndefined),                 //
           B(Return),                       //
3795
       },
3796 3797
       1,
       {"Error"}},
3798 3799 3800 3801 3802 3803 3804 3805 3806 3807
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


3808 3809 3810
TEST(CallNew) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
3811 3812 3813
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
3814
  FeedbackVectorSlot slot1 = feedback_spec.AddGeneralSlot();
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824
  FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
  USE(slot1);

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<InstanceType> snippets[] = {
      {"function bar() { this.value = 0; }\n"
       "function f() { return new bar(); }\n"
       "f()",
3825
       1 * kPointerSize,
3826
       1,
3827
       10,
3828
       {
3829 3830 3831 3832
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Star), R(0),                                           //
           B(New), R(0), R(0), U8(0),                               //
           B(Return),                                               //
3833 3834 3835 3836 3837 3838 3839 3840
       },
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"function bar(x) { this.value = 18; this.x = x;}\n"
       "function f() { return new bar(3); }\n"
       "f()",
       2 * kPointerSize,
       1,
3841
       14,
3842
       {
3843 3844 3845 3846 3847 3848
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Star), R(0),                                           //
           B(LdaSmi8), U8(3),                                       //
           B(Star), R(1),                                           //
           B(New), R(0), R(1), U8(1),                               //
           B(Return),                                               //
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
       },
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"function bar(w, x, y, z) {\n"
       "  this.value = 18;\n"
       "  this.x = x;\n"
       "  this.y = y;\n"
       "  this.z = z;\n"
       "}\n"
       "function f() { return new bar(3, 4, 5); }\n"
       "f()",
       4 * kPointerSize,
       1,
3862
       22,
3863
       {
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Star), R(0),                                           //
           B(LdaSmi8), U8(3),                                       //
           B(Star), R(1),                                           //
           B(LdaSmi8), U8(4),                                       //
           B(Star), R(2),                                           //
           B(LdaSmi8), U8(5),                                       //
           B(Star), R(3),                                           //
           B(New), R(0), R(1), U8(3),                               //
           B(Return),                                               //
3874 3875 3876 3877 3878 3879 3880 3881
       },
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
3882
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
3883 3884 3885 3886
  }
}


3887 3888 3889
TEST(ContextVariables) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
3890 3891 3892 3893 3894 3895 3896
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot = feedback_spec.AddCallICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
3897 3898 3899 3900 3901 3902 3903

  int closure = Register::function_closure().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;
  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a; return function() { a = 1; };",
       1 * kPointerSize,
       1,
3904
       11,
3905 3906 3907 3908
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),  //
                           R(closure), U8(1),                  //
           B(PushContext), R(0),                               //
3909
           B(CreateClosure), U8(0), U8(0),                     //
3910 3911 3912 3913 3914 3915 3916
           B(Return),                                          //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"var a = 1; return function() { a = 2; };",
       1 * kPointerSize,
       1,
3917
       16,
3918 3919 3920 3921 3922 3923
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),  //
                           R(closure), U8(1),                  //
           B(PushContext), R(0),                               //
           B(LdaSmi8), U8(1),                                  //
           B(StaContextSlot), R(0), U8(first_context_slot),    //
3924
           B(CreateClosure), U8(0), U8(0),                     //
3925 3926 3927 3928 3929 3930 3931
           B(Return),                                          //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"var a = 1; var b = 2; return function() { a = 2; b = 3 };",
       1 * kPointerSize,
       1,
3932
       21,
3933 3934 3935 3936 3937 3938 3939 3940
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),    //
                           R(closure), U8(1),                    //
           B(PushContext), R(0),                                 //
           B(LdaSmi8), U8(1),                                    //
           B(StaContextSlot), R(0), U8(first_context_slot),      //
           B(LdaSmi8), U8(2),                                    //
           B(StaContextSlot), R(0), U8(first_context_slot + 1),  //
3941
           B(CreateClosure), U8(0), U8(0),                       //
3942 3943 3944 3945 3946 3947 3948
           B(Return),                                            //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"var a; (function() { a = 2; })(); return a;",
       3 * kPointerSize,
       1,
3949
       24,
3950
       {
3951 3952 3953 3954 3955
           B(CallRuntime), U16(Runtime::kNewFunctionContext),       //
                           R(closure), U8(1),                       //
           B(PushContext), R(0),                                    //
           B(LdaUndefined),                                         //
           B(Star), R(2),                                           //
3956
           B(CreateClosure), U8(0), U8(0),                          //
3957 3958 3959 3960
           B(Star), R(1),                                           //
           B(Call), R(1), R(2), U8(0), U8(vector->GetIndex(slot)),  //
           B(LdaContextSlot), R(0), U8(first_context_slot),         //
           B(Return),                                               //
3961 3962 3963 3964 3965 3966
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"'use strict'; let a = 1; { let b = 2; return function() { a + b; }; }",
       4 * kPointerSize,
       1,
3967
       44,
3968 3969 3970 3971 3972 3973 3974 3975
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),             //
                           R(closure), U8(1),                             //
           B(PushContext), R(0),                                          //
           B(LdaTheHole),                                                 //
           B(StaContextSlot), R(0), U8(first_context_slot),               //
           B(LdaSmi8), U8(1),                                             //
           B(StaContextSlot), R(0), U8(first_context_slot),               //
3976
           B(LdaConstant), U8(0),                                         //
3977 3978 3979 3980 3981 3982 3983 3984 3985
           B(Star), R(2),                                                 //
           B(Ldar), R(closure),                                           //
           B(Star), R(3),                                                 //
           B(CallRuntime), U16(Runtime::kPushBlockContext), R(2), U8(2),  //
           B(PushContext), R(1),                                          //
           B(LdaTheHole),                                                 //
           B(StaContextSlot), R(1), U8(first_context_slot),               //
           B(LdaSmi8), U8(2),                                             //
           B(StaContextSlot), R(1), U8(first_context_slot),               //
3986
           B(CreateClosure), U8(1), U8(0),                                //
3987 3988
           B(Return),                                                     //
       },
3989 3990
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007
        InstanceType::SHARED_FUNCTION_INFO_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(ContextParameters) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  int closure = Register::function_closure().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;
4008

4009 4010 4011 4012
  ExpectedSnippet<InstanceType> snippets[] = {
      {"function f(arg1) { return function() { arg1 = 2; }; }",
       1 * kPointerSize,
       2,
4013
       16,
4014 4015 4016 4017 4018 4019
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),  //
                           R(closure), U8(1),                  //
           B(PushContext), R(0),                               //
           B(Ldar), R(helper.kLastParamIndex),                 //
           B(StaContextSlot), R(0), U8(first_context_slot),    //
4020
           B(CreateClosure), U8(0), U8(0),                     //
4021 4022 4023 4024 4025 4026 4027
           B(Return),                                          //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"function f(arg1) { var a = function() { arg1 = 2; }; return arg1; }",
       2 * kPointerSize,
       2,
4028
       21,
4029 4030 4031 4032 4033 4034
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),  //
                           R(closure), U8(1),                  //
           B(PushContext), R(1),                               //
           B(Ldar), R(helper.kLastParamIndex),                 //
           B(StaContextSlot), R(1), U8(first_context_slot),    //
4035
           B(CreateClosure), U8(0), U8(0),                     //
4036 4037 4038 4039 4040 4041 4042 4043 4044
           B(Star), R(0),                                      //
           B(LdaContextSlot), R(1), U8(first_context_slot),    //
           B(Return),                                          //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"function f(a1, a2, a3, a4) { return function() { a1 = a3; }; }",
       1 * kPointerSize,
       5,
4045
       21,
4046 4047 4048 4049 4050 4051 4052 4053
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),    //
                           R(closure), U8(1),                    //
           B(PushContext), R(0),                                 //
           B(Ldar), R(helper.kLastParamIndex - 3),               //
           B(StaContextSlot), R(0), U8(first_context_slot + 1),  //
           B(Ldar), R(helper.kLastParamIndex -1),                //
           B(StaContextSlot), R(0), U8(first_context_slot),      //
4054
           B(CreateClosure), U8(0), U8(0),                       //
4055 4056 4057 4058 4059 4060 4061
           B(Return),                                            //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"function f() { var self = this; return function() { self = 2; }; }",
       1 * kPointerSize,
       1,
4062
       16,
4063 4064 4065 4066 4067 4068
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext),  //
                           R(closure), U8(1),                  //
           B(PushContext), R(0),                               //
           B(Ldar), R(helper.kLastParamIndex),                 //
           B(StaContextSlot), R(0), U8(first_context_slot),    //
4069
           B(CreateClosure), U8(0), U8(0),                     //
4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082
           B(Return),                                          //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunction(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

4083

4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146
TEST(OuterContextVariables) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  int context = Register::function_context().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;

  ExpectedSnippet<InstanceType> snippets[] = {
      {"function Outer() {"
       "  var outerVar = 1;"
       "  function Inner(innerArg) {"
       "    this.innerFunc = function() { return outerVar * innerArg; }"
       "  }"
       "  this.getInnerFunc = function() { return new Inner(1).innerFunc; }"
       "}"
       "var f = new Outer().getInnerFunc();",
       2 * kPointerSize,
       1,
       20,
       {
           B(Ldar), R(context),                                    //
           B(Star), R(0),                                          //
           B(LdaContextSlot), R(0), U8(Context::PREVIOUS_INDEX),   //
           B(Star), R(0),                                          //
           B(LdaContextSlot), R(0), U8(first_context_slot),        //
           B(Star), R(1),                                          //
           B(LdaContextSlot), R(context), U8(first_context_slot),  //
           B(Mul), R(1),                                           //
           B(Return),                                              //
       }},
      {"function Outer() {"
       "  var outerVar = 1;"
       "  function Inner(innerArg) {"
       "    this.innerFunc = function() { outerVar = innerArg; }"
       "  }"
       "  this.getInnerFunc = function() { return new Inner(1).innerFunc; }"
       "}"
       "var f = new Outer().getInnerFunc();",
       2 * kPointerSize,
       1,
       21,
       {
           B(LdaContextSlot), R(context), U8(first_context_slot),  //
           B(Star), R(0),                                          //
           B(Ldar), R(context),                                    //
           B(Star), R(1),                                          //
           B(LdaContextSlot), R(1), U8(Context::PREVIOUS_INDEX),   //
           B(Star), R(1),                                          //
           B(Ldar), R(0),                                          //
           B(StaContextSlot), R(1), U8(first_context_slot),        //
           B(LdaUndefined),                                        //
           B(Return),                                              //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionNoFilter(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
TEST(CountOperators) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();
  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

4158 4159 4160 4161 4162
  FeedbackVectorSpec store_feedback_spec(&zone);
  FeedbackVectorSlot store_slot = store_feedback_spec.AddStoreICSlot();
  Handle<i::TypeFeedbackVector> store_vector =
      i::NewTypeFeedbackVector(helper.isolate(), &store_feedback_spec);

4163 4164 4165 4166 4167
  int closure = Register::function_closure().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;

  int object_literal_flags =
      ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;
4168 4169
  int array_literal_flags =
      ArrayLiteral::kDisableMementos | ArrayLiteral::kShallowElements;
4170 4171 4172 4173 4174

  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a = 1; return ++a;",
       1 * kPointerSize,
       1,
4175
       9,
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(ToNumber),        //
           B(Inc),             //
           B(Star), R(0),      //
           B(Return),          //
       }},
      {"var a = 1; return a++;",
       2 * kPointerSize,
       1,
4187
       13,
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(ToNumber),        //
           B(Star), R(1),      //
           B(Inc),             //
           B(Star), R(0),      //
           B(Ldar), R(1),      //
           B(Return),          //
       }},
      {"var a = 1; return --a;",
       1 * kPointerSize,
       1,
4201
       9,
4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(ToNumber),        //
           B(Dec),             //
           B(Star), R(0),      //
           B(Return),          //
       }},
      {"var a = 1; return a--;",
       2 * kPointerSize,
       1,
4213
       13,
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(ToNumber),        //
           B(Star), R(1),      //
           B(Dec),             //
           B(Star), R(0),      //
           B(Ldar), R(1),      //
           B(Return),          //
       }},
      {"var a = { val: 1 }; return a.val++;",
       2 * kPointerSize,
       1,
4227
       21,
4228
       {
4229 4230 4231 4232 4233 4234 4235 4236 4237
           B(CreateObjectLiteral), U8(0), U8(0), U8(object_literal_flags),  //
           B(Star), R(0),                                                   //
           B(LoadICSloppy), R(0), U8(1), U8(vector->GetIndex(slot1)),       //
           B(ToNumber),                                                     //
           B(Star), R(1),                                                   //
           B(Inc),                                                          //
           B(StoreICSloppy), R(0), U8(1), U8(vector->GetIndex(slot2)),      //
           B(Ldar), R(1),                                                   //
           B(Return),                                                       //
4238 4239 4240 4241 4242 4243 4244
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var a = { val: 1 }; return --a.val;",
       1 * kPointerSize,
       1,
4245
       17,
4246
       {
4247 4248 4249 4250 4251 4252 4253
           B(CreateObjectLiteral), U8(0), U8(0), U8(object_literal_flags),  //
           B(Star), R(0),                                                   //
           B(LoadICSloppy), R(0), U8(1), U8(vector->GetIndex(slot1)),       //
           B(ToNumber),                                                     //
           B(Dec),                                                          //
           B(StoreICSloppy), R(0), U8(1), U8(vector->GetIndex(slot2)),      //
           B(Return),                                                       //
4254 4255 4256 4257 4258 4259 4260
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var name = 'var'; var a = { val: 1 }; return a[name]--;",
       4 * kPointerSize,
       1,
4261
       28,
4262 4263 4264
       {
           B(LdaConstant), U8(0),                                           //
           B(Star), R(0),                                                   //
4265
           B(CreateObjectLiteral), U8(1), U8(0), U8(object_literal_flags),  //
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282
           B(Star), R(1),                                                   //
           B(Ldar), R(0),                                                   //
           B(Star), R(2),                                                   //
           B(KeyedLoadICSloppy), R(1), U8(vector->GetIndex(slot1)),         //
           B(ToNumber),                                                     //
           B(Star), R(3),                                                   //
           B(Dec),                                                          //
           B(KeyedStoreICSloppy), R(1), R(2), U8(vector->GetIndex(slot2)),  //
           B(Ldar), R(3),                                                   //
           B(Return),                                                       //
       },
       2,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE}},
      {"var name = 'var'; var a = { val: 1 }; return ++a[name];",
       3 * kPointerSize,
       1,
4283
       24,
4284 4285 4286
       {
           B(LdaConstant), U8(0),                                           //
           B(Star), R(0),                                                   //
4287
           B(CreateObjectLiteral), U8(1), U8(0), U8(object_literal_flags),  //
4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
           B(Star), R(1),                                                   //
           B(Ldar), R(0),                                                   //
           B(Star), R(2),                                                   //
           B(KeyedLoadICSloppy), R(1), U8(vector->GetIndex(slot1)),         //
           B(ToNumber),                                                     //
           B(Inc),                                                          //
           B(KeyedStoreICSloppy), R(1), R(2), U8(vector->GetIndex(slot2)),  //
           B(Return),                                                       //
       },
       2,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
        InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 1; var b = function() { return a }; return ++a;",
       2 * kPointerSize,
       1,
4303
       26,
4304 4305 4306 4307 4308 4309
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext), R(closure),  //
                           U8(1),                                          //
           B(PushContext), R(1),                                           //
           B(LdaSmi8), U8(1),                                              //
           B(StaContextSlot), R(1), U8(first_context_slot),                //
4310
           B(CreateClosure), U8(0), U8(0),                                 //
4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322
           B(Star), R(0),                                                  //
           B(LdaContextSlot), R(1), U8(first_context_slot),                //
           B(ToNumber),                                                    //
           B(Inc),                                                         //
           B(StaContextSlot), R(1), U8(first_context_slot),                //
           B(Return),                                                      //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
      {"var a = 1; var b = function() { return a }; return a--;",
       3 * kPointerSize,
       1,
4323
       30,
4324 4325 4326 4327 4328 4329
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext), R(closure),  //
                           U8(1),                                          //
           B(PushContext), R(1),                                           //
           B(LdaSmi8), U8(1),                                              //
           B(StaContextSlot), R(1), U8(first_context_slot),                //
4330
           B(CreateClosure), U8(0), U8(0),                                 //
4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341
           B(Star), R(0),                                                  //
           B(LdaContextSlot), R(1), U8(first_context_slot),                //
           B(ToNumber),                                                    //
           B(Star), R(2),                                                  //
           B(Dec),                                                         //
           B(StaContextSlot), R(1), U8(first_context_slot),                //
           B(Ldar), R(2),                                                  //
           B(Return),                                                      //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
4342 4343 4344
      {"var idx = 1; var a = [1, 2]; return a[idx++] = 2;",
       3 * kPointerSize,
       1,
4345
       25,
4346 4347 4348
       {
           B(LdaSmi8), U8(1),                                              //
           B(Star), R(0),                                                  //
4349
           B(CreateArrayLiteral), U8(0), U8(0), U8(array_literal_flags),   //
4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362
           B(Star), R(1),                                                  //
           B(Ldar), R(0),                                                  //
           B(ToNumber),                                                    //
           B(Star), R(2),                                                  //
           B(Inc),                                                         //
           B(Star), R(0),                                                  //
           B(LdaSmi8), U8(2),                                              //
           B(KeyedStoreICSloppy), R(1), R(2),                              //
                                  U8(store_vector->GetIndex(store_slot)),  //
           B(Return),                                                      //
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(GlobalCountOperators) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<const char*> snippets[] = {
      {"var global = 1;\nfunction f() { return ++global; }\nf()",
       0,
       1,
       9,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot1)),  //
           B(ToNumber),                                             //
           B(Inc),                                                  //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Return),                                               //
       },
       1,
       {"global"}},
      {"var global = 1;\nfunction f() { return global--; }\nf()",
       1 * kPointerSize,
       1,
       13,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot1)),  //
           B(ToNumber),                                             //
           B(Star), R(0),                                           //
           B(Dec),                                                  //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Ldar), R(0),                                           //
           B(Return),
       },
       1,
       {"global"}},
      {"unallocated = 1;\nfunction f() { 'use strict'; return --unallocated; }"
       "f()",
       0,
       1,
       9,
       {
           B(LdaGlobalStrict), U8(0), U8(vector->GetIndex(slot1)),  //
           B(ToNumber),                                             //
           B(Dec),                                                  //
           B(StaGlobalStrict), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Return),                                               //
       },
       1,
       {"unallocated"}},
      {"unallocated = 1;\nfunction f() { return unallocated++; }\nf()",
       1 * kPointerSize,
       1,
       13,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot1)),  //
           B(ToNumber),                                             //
           B(Star), R(0),                                           //
           B(Inc),                                                  //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Ldar), R(0),                                           //
           B(Return),
       },
       1,
       {"unallocated"}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(CompoundExpressions) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int closure = Register::function_closure().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  int object_literal_flags =
      ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;
  ExpectedSnippet<InstanceType> snippets[] = {
      {"var a = 1; a += 2;",
       1 * kPointerSize,
       1,
       12,
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(LdaSmi8), U8(2),  //
           B(Add), R(0),       //
           B(Star), R(0),      //
           B(LdaUndefined),    //
           B(Return),          //
       }},
      {"var a = 1; a /= 2;",
       1 * kPointerSize,
       1,
       12,
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(LdaSmi8), U8(2),  //
           B(Div), R(0),       //
           B(Star), R(0),      //
           B(LdaUndefined),    //
           B(Return),          //
       }},
      {"var a = { val: 2 }; a.name *= 2;",
       2 * kPointerSize,
       1,
4500
       22,
4501
       {
4502 4503 4504 4505 4506 4507 4508 4509 4510
           B(CreateObjectLiteral), U8(0), U8(0), U8(object_literal_flags),  //
           B(Star), R(0),                                                   //
           B(LoadICSloppy), R(0), U8(1), U8(vector->GetIndex(slot1)),       //
           B(Star), R(1),                                                   //
           B(LdaSmi8), U8(2),                                               //
           B(Mul), R(1),                                                    //
           B(StoreICSloppy), R(0), U8(1), U8(vector->GetIndex(slot2)),      //
           B(LdaUndefined),                                                 //
           B(Return),                                                       //
4511 4512 4513 4514 4515 4516 4517
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var a = { 1: 2 }; a[1] ^= 2;",
       3 * kPointerSize,
       1,
4518
       25,
4519
       {
4520
           B(CreateObjectLiteral), U8(0), U8(0), U8(object_literal_flags),  //
4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536
           B(Star), R(0),                                                   //
           B(LdaSmi8), U8(1),                                               //
           B(Star), R(1),                                                   //
           B(KeyedLoadICSloppy), R(0), U8(vector->GetIndex(slot1)),         //
           B(Star), R(2),                                                   //
           B(LdaSmi8), U8(2),                                               //
           B(BitwiseXor), R(2),                                             //
           B(KeyedStoreICSloppy), R(0), R(1), U8(vector->GetIndex(slot2)),  //
           B(LdaUndefined),                                                 //
           B(Return),                                                       //
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var a = 1; (function f() { return a; }); a |= 24;",
       2 * kPointerSize,
       1,
4537
       29,
4538 4539 4540 4541 4542 4543
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext), R(closure),  //
                           U8(1),                                          //
           B(PushContext), R(0),                                           //
           B(LdaSmi8), U8(1),                                              //
           B(StaContextSlot), R(0), U8(first_context_slot),                //
4544
           B(CreateClosure), U8(0), U8(0),                                 //
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
           B(LdaContextSlot), R(0), U8(first_context_slot),                //
           B(Star), R(1),                                                  //
           B(LdaSmi8), U8(24),                                             //
           B(BitwiseOr), R(1),                                             //
           B(StaContextSlot), R(0), U8(first_context_slot),                //
           B(LdaUndefined),                                                //
           B(Return),                                                      //
       },
       1,
       {InstanceType::SHARED_FUNCTION_INFO_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(GlobalCompoundExpressions) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<const char*> snippets[] = {
      {"var global = 1;\nfunction f() { return global &= 1; }\nf()",
       1 * kPointerSize,
       1,
       13,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Star), R(0),                                           //
           B(LdaSmi8), U8(1),                                       //
           B(BitwiseAnd), R(0),                                     //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Return),                                               //
       },
       1,
       {"global"}},
      {"unallocated = 1;\nfunction f() { return unallocated += 1; }\nf()",
       1 * kPointerSize,
       1,
       13,
       {
           B(LdaGlobalSloppy), U8(0), U8(vector->GetIndex(slot1)),  //
           B(Star), R(0),                                           //
           B(LdaSmi8), U8(1),                                       //
           B(Add), R(0),                                            //
           B(StaGlobalSloppy), U8(0), U8(vector->GetIndex(slot2)),  //
           B(Return),                                               //
       },
       1,
       {"unallocated"}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(snippets[i].code_snippet, "f");
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
4612 4613 4614
  }
}

4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633

TEST(CreateArguments) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int closure = Register::function_closure().index();
  int first_context_slot = Context::MIN_CONTEXT_SLOTS;

  FeedbackVectorSpec feedback_spec(&zone);
  FeedbackVectorSlot slot = feedback_spec.AddKeyedLoadICSlot();

  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<const char*> snippets[] = {
      {"function f() { return arguments; }",
       1 * kPointerSize,
       1,
4634
       4,
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653
       {
           B(CreateMappedArguments),  //
           B(Star), R(0),             //
           B(Return),                 //
       }},
      {"function f() { return arguments[0]; }",
       1 * kPointerSize,
       1,
       8,
       {
           B(CreateMappedArguments),                                //
           B(Star), R(0),                                           //
           B(LdaZero),                                              //
           B(KeyedLoadICSloppy), R(0), U8(vector->GetIndex(slot)),  //
           B(Return),                                               //
       }},
      {"function f() { 'use strict'; return arguments; }",
       1 * kPointerSize,
       1,
4654
       4,
4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678
       {
           B(CreateUnmappedArguments),  //
           B(Star), R(0),               //
           B(Return),                   //
       }},
      {"function f(a) { return arguments[0]; }",
       2 * kPointerSize,
       2,
       20,
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext), R(closure),  //
                           U8(1),                                          //
           B(PushContext), R(1),                                           //
           B(Ldar), R(BytecodeGeneratorHelper::kLastParamIndex),           //
           B(StaContextSlot), R(1), U8(first_context_slot),                //
           B(CreateMappedArguments),                                       //
           B(Star), R(0),                                                  //
           B(LdaZero),                                                     //
           B(KeyedLoadICSloppy), R(0), U8(vector->GetIndex(slot)),         //
           B(Return),                                                      //
       }},
      {"function f(a, b, c) { return arguments; }",
       2 * kPointerSize,
       4,
4679
       26,
4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696
       {
           B(CallRuntime), U16(Runtime::kNewFunctionContext), R(closure),  //
                           U8(1),                                          //
           B(PushContext), R(1),                                           //
           B(Ldar), R(BytecodeGeneratorHelper::kLastParamIndex - 2),       //
           B(StaContextSlot), R(1), U8(first_context_slot + 2),            //
           B(Ldar), R(BytecodeGeneratorHelper::kLastParamIndex - 1),       //
           B(StaContextSlot), R(1), U8(first_context_slot + 1),            //
           B(Ldar), R(BytecodeGeneratorHelper::kLastParamIndex),           //
           B(StaContextSlot), R(1), U8(first_context_slot),                //
           B(CreateMappedArguments),                                       //
           B(Star), R(0),                                                  //
           B(Return),                                                      //
       }},
      {"function f(a, b, c) { 'use strict'; return arguments; }",
       1 * kPointerSize,
       4,
4697
       4,
4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716
       {
           B(CreateUnmappedArguments),  //
           B(Star), R(0),               //
           B(Return),                   //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunction(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(IllegalRedeclaration) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

4717 4718 4719 4720
  CHECK_GE(MessageTemplate::kVarRedeclaration, 128);
  // Must adapt bytecode if this changes.

  ExpectedSnippet<Handle<Object>, 2> snippets[] = {
4721 4722 4723 4724 4725 4726
      {"const a = 1; { var a = 2; }",
       3 * kPointerSize,
       1,
       14,
       {
           B(LdaConstant), U8(0),                                       //
4727 4728
           B(Star), R(1),                                               //
           B(LdaConstant), U8(1),                                       //
4729 4730 4731 4732
           B(Star), R(2),                                               //
           B(CallRuntime), U16(Runtime::kNewSyntaxError), R(1), U8(2),  //
           B(Throw),                                                    //
       },
4733 4734 4735
       2,
       {helper.factory()->NewNumberFromInt(MessageTemplate::kVarRedeclaration),
        helper.factory()->NewStringFromAsciiChecked("a")}},
4736 4737 4738 4739 4740 4741 4742 4743 4744
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

4745

4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780
TEST(ForIn) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;
  Zone zone;

  int simple_flags =
      ArrayLiteral::kDisableMementos | ArrayLiteral::kShallowElements;
  int deep_elements_flags =
      ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;

  FeedbackVectorSpec feedback_spec(&zone);
  feedback_spec.AddStoreICSlot();
  FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();
  FeedbackVectorSlot slot3 = feedback_spec.AddStoreICSlot();
  FeedbackVectorSlot slot4 = feedback_spec.AddStoreICSlot();
  Handle<i::TypeFeedbackVector> vector =
      i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);

  ExpectedSnippet<InstanceType> snippets[] = {
      {"for (var p in null) {}",
       2 * kPointerSize,
       1,
       2,
       {B(LdaUndefined), B(Return)},
       0},
      {"for (var p in undefined) {}",
       2 * kPointerSize,
       1,
       2,
       {B(LdaUndefined), B(Return)},
       0},
      {"var x = 'potatoes';\n"
       "for (var p in x) { return p; }",
       5 * kPointerSize,
       1,
4781
       46,
4782 4783 4784
       {
           B(LdaConstant), U8(0),                                             //
           B(Star), R(1),                                                     //
4785 4786
           B(JumpIfUndefined), U8(40),                                        //
           B(JumpIfNull), U8(38),                                             //
4787 4788 4789 4790
           B(ToObject),                                                       //
           B(Star), R(3),                                                     //
           B(CallRuntime), U16(Runtime::kGetPropertyNamesFast), R(3), U8(1),  //
           B(ForInPrepare), R(3),                                             //
4791
           B(JumpIfUndefined), U8(26),                                        //
4792 4793 4794 4795
           B(Star), R(4),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(3),                                                     //
           B(ForInDone), R(4),                                                //
4796
           B(JumpIfTrue), U8(17),                                             //
4797
           B(ForInNext), R(4), R(3),                                          //
4798
           B(JumpIfUndefined), U8(7),                                         //
4799 4800 4801 4802 4803
           B(Star), R(0),                                                     //
           B(Star), R(2),                                                     //
           B(Return),                                                         //
           B(Ldar), R(3),                                                     //
           B(Inc),                                                            //
4804
           B(Jump), U8(-19),                                                  //
4805 4806 4807 4808 4809 4810 4811 4812 4813
           B(LdaUndefined),                                                   //
           B(Return),                                                         //
       },
       1,
       {InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var x = 0;\n"
       "for (var p in [1,2,3]) { x += p; }",
       5 * kPointerSize,
       1,
4814
       52,
4815 4816 4817
       {
           B(LdaZero),                                                        //
           B(Star), R(1),                                                     //
4818
           B(CreateArrayLiteral), U8(0), U8(0), U8(simple_flags),             //
4819 4820
           B(JumpIfUndefined), U8(43),                                        //
           B(JumpIfNull), U8(41),                                             //
4821 4822 4823 4824
           B(ToObject),                                                       //
           B(Star), R(3),                                                     //
           B(CallRuntime), U16(Runtime::kGetPropertyNamesFast), R(3), U8(1),  //
           B(ForInPrepare), R(3),                                             //
4825
           B(JumpIfUndefined), U8(29),                                        //
4826 4827 4828 4829
           B(Star), R(4),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(3),                                                     //
           B(ForInDone), R(4),                                                //
4830
           B(JumpIfTrue), U8(20),                                             //
4831
           B(ForInNext), R(4), R(3),                                          //
4832
           B(JumpIfUndefined), U8(10),                                        //
4833 4834 4835 4836 4837 4838
           B(Star), R(0),                                                     //
           B(Star), R(2),                                                     //
           B(Add), R(1),                                                      //
           B(Star), R(1),                                                     //
           B(Ldar), R(3),                                                     //
           B(Inc),                                                            //
4839
           B(Jump), U8(-22),                                                  //
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851
           B(LdaUndefined),                                                   //
           B(Return),                                                         //
       },
       1,
       {InstanceType::FIXED_ARRAY_TYPE}},
      {"var x = { 'a': 1, 'b': 2 };\n"
       "for (x['a'] in [10, 20, 30]) {\n"
       "  if (x['a'] == 10) continue;\n"
       "  if (x['a'] == 20) break;\n"
       "}",
       4 * kPointerSize,
       1,
4852
       81,
4853
       {
4854
           B(CreateObjectLiteral), U8(0), U8(0), U8(deep_elements_flags),     //
4855
           B(Star), R(0),                                                     //
4856
           B(CreateArrayLiteral), U8(1), U8(1), U8(simple_flags),             //
4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897
           B(JumpIfUndefined), U8(69),                                        //
           B(JumpIfNull), U8(67),                                             //
           B(ToObject),                                                       //
           B(Star), R(1),                                                     //
           B(CallRuntime), U16(Runtime::kGetPropertyNamesFast), R(1), U8(1),  //
           B(ForInPrepare), R(1),                                             //
           B(JumpIfUndefined), U8(55),                                        //
           B(Star), R(2),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(1),                                                     //
           B(ForInDone), R(2),                                                //
           B(JumpIfTrue), U8(46),                                             //
           B(ForInNext), R(2), R(1),                                          //
           B(JumpIfUndefined), U8(36),                                        //
           B(Star), R(3),                                                     //
           B(StoreICSloppy), R(0), U8(2), U8(vector->GetIndex(slot4)),        //
           B(LoadICSloppy), R(0), U8(2), U8(vector->GetIndex(slot2)),         //
           B(Star), R(3),                                                     //
           B(LdaSmi8), U8(10),                                                //
           B(TestEqual), R(3),                                                //
           B(JumpIfFalse), U8(4),                                             //
           B(Jump), U8(16),                                                   //
           B(LoadICSloppy), R(0), U8(2), U8(vector->GetIndex(slot3)),         //
           B(Star), R(3),                                                     //
           B(LdaSmi8), U8(20),                                                //
           B(TestEqual), R(3),                                                //
           B(JumpIfFalse), U8(4),                                             //
           B(Jump), U8(7),                                                    //
           B(Ldar), R(1),                                                     //
           B(Inc),                                                            //
           B(Jump), U8(-48),                                                  //
           B(LdaUndefined),                                                   //
           B(Return),                                                         //
       },
       3,
       {InstanceType::FIXED_ARRAY_TYPE, InstanceType::FIXED_ARRAY_TYPE,
        InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
      {"var x = [ 10, 11, 12 ] ;\n"
       "for (x[0] in [1,2,3]) { return x[3]; }",
       5 * kPointerSize,
       1,
4898
       64,
4899
       {
4900
           B(CreateArrayLiteral), U8(0), U8(0), U8(simple_flags),             //
4901
           B(Star), R(0),                                                     //
4902
           B(CreateArrayLiteral), U8(1), U8(1), U8(simple_flags),             //
4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942
           B(JumpIfUndefined), U8(52),                                        //
           B(JumpIfNull), U8(50),                                             //
           B(ToObject),                                                       //
           B(Star), R(1),                                                     //
           B(CallRuntime), U16(Runtime::kGetPropertyNamesFast), R(1), U8(1),  //
           B(ForInPrepare), R(1),                                             //
           B(JumpIfUndefined), U8(38),                                        //
           B(Star), R(2),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(1),                                                     //
           B(ForInDone), R(2),                                                //
           B(JumpIfTrue), U8(29),                                             //
           B(ForInNext), R(2), R(1),                                          //
           B(JumpIfUndefined), U8(19),                                        //
           B(Star), R(3),                                                     //
           B(LdaZero),                                                        //
           B(Star), R(4),                                                     //
           B(Ldar), R(3),                                                     //
           B(KeyedStoreICSloppy), R(0), R(4), U8(vector->GetIndex(slot3)),    //
           B(LdaSmi8), U8(3),                                                 //
           B(KeyedLoadICSloppy), R(0), U8(vector->GetIndex(slot2)),           //
           B(Return),                                                         //
           B(Ldar), R(1),                                                     //
           B(Inc),                                                            //
           B(Jump), U8(-31),                                                  //
           B(LdaUndefined),                                                   //
           B(Return),                                                         //
       },
       2,
       {InstanceType::FIXED_ARRAY_TYPE, InstanceType::FIXED_ARRAY_TYPE}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


4943 4944 4945 4946 4947 4948 4949 4950
TEST(Conditional) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
      {"return 1 ? 2 : 3;",
       0,
       1,
4951
       11,
4952
       {
4953 4954 4955 4956 4957 4958
           B(LdaSmi8), U8(1),               //
           B(JumpIfToBooleanFalse), U8(6),  //
           B(LdaSmi8), U8(2),               //
           B(Jump), U8(4),                  //
           B(LdaSmi8), U8(3),               //
           B(Return),                       //
4959 4960 4961 4962
       }},
      {"return 1 ? 2 ? 3 : 4 : 5;",
       0,
       1,
4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974
       19,
       {
           B(LdaSmi8), U8(1),                //
           B(JumpIfToBooleanFalse), U8(14),  //
           B(LdaSmi8), U8(2),                //
           B(JumpIfToBooleanFalse), U8(6),   //
           B(LdaSmi8), U8(3),                //
           B(Jump), U8(4),                   //
           B(LdaSmi8), U8(4),                //
           B(Jump), U8(4),                   //
           B(LdaSmi8), U8(5),                //
           B(Return),                        //
4975 4976 4977 4978 4979 4980 4981 4982 4983 4984
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997

TEST(Switch) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
      {"var a = 1;\n"
       "switch(a) {\n"
       " case 1: return 2;\n"
       " case 2: return 3;\n"
       "}\n",
       2 * kPointerSize,
       1,
4998
       28,
4999 5000 5001
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             // The tag variable is allocated as a
5002 5003
           B(Star), R(0),             // local by the parser, hence the store
           B(LdaSmi8), U8(1),         // to another local register.
5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(7),      //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(2),         //
           B(Return),                 //
           B(LdaSmi8), U8(3),         //
           B(Return),                 //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
      {"var a = 1;\n"
       "switch(a) {\n"
       " case 1: a = 2; break;\n"
       " case 2: a = 3; break;\n"
       "}\n",
       2 * kPointerSize,
       1,
5024
       34,
5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Star), R(0),             //
           B(LdaSmi8), U8(1),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(Jump), U8(14),           //
           B(LdaSmi8), U8(2),         //
           B(Star), R(1),             //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(3),         //
           B(Star), R(1),             //
           B(Jump), U8(2),            //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
      {"var a = 1;\n"
       "switch(a) {\n"
       " case 1: a = 2; // fall-through\n"
       " case 2: a = 3; break;\n"
       "}\n",
       2 * kPointerSize,
       1,
5052
       32,
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Star), R(0),             //
           B(LdaSmi8), U8(1),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(8),      //
           B(Jump), U8(12),           //
           B(LdaSmi8), U8(2),         //
           B(Star), R(1),             //
           B(LdaSmi8), U8(3),         //
           B(Star), R(1),             //
           B(Jump), U8(2),            //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
      {"var a = 1;\n"
       "switch(a) {\n"
       " case 2: break;\n"
       " case 3: break;\n"
       " default: a = 1; break;\n"
       "}\n",
       2 * kPointerSize,
       1,
5080
       32,
5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Star), R(0),             //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(LdaSmi8), U8(3),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(6),      //
           B(Jump), U8(6),            //
           B(Jump), U8(10),           //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Jump), U8(2),            //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
      {"var a = 1;\n"
       "switch(typeof(a)) {\n"
       " case 2: a = 1; break;\n"
       " case 3: a = 2; break;\n"
       " default: a = 3; break;\n"
       "}\n",
       2 * kPointerSize,
       1,
5108
       41,
5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(TypeOf),                 //
           B(Star), R(0),             //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(LdaSmi8), U8(3),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(Jump), U8(14),           //
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Jump), U8(14),           //
           B(LdaSmi8), U8(2),         //
           B(Star), R(1),             //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(3),         //
           B(Star), R(1),             //
           B(Jump), U8(2),            //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
      {"var a = 1;\n"
       "switch(a) {\n"
       " case typeof(a): a = 1; break;\n"
       " default: a = 2; break;\n"
       "}\n",
       2 * kPointerSize,
       1,
5140
       29,
5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Star), R(0),             //
           B(Ldar), R(1),             //
           B(TypeOf),                 //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(4),      //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(1),         //
           B(Star), R(1),             //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(2),         //
           B(Star), R(1),             //
           B(Jump), U8(2),            //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
      {"var a = 1;\n"
       "switch(a) {\n"
       " case 1:\n" REPEAT_64(SPACE, "  a = 2;")
       "break;\n"
       " case 2: a = 3; break;"
       "}\n",
       2 * kPointerSize,
       1,
5167
       286,
5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201
       {
           B(LdaSmi8), U8(1),             //
           B(Star), R(1),                 //
           B(Star), R(0),                 //
           B(LdaSmi8), U8(1),             //
           B(TestEqualStrict), R(0),      //
           B(JumpIfTrue), U8(10),         //
           B(LdaSmi8), U8(2),             //
           B(TestEqualStrict), R(0),      //
           B(JumpIfTrueConstant), U8(0),  //
           B(JumpConstant), U8(1),        //
           REPEAT_64(COMMA,               //
              B(LdaSmi8), U8(2),          //
              B(Star), R(1)),             //
           B(Jump), U8(8),                //
           B(LdaSmi8), U8(3),             //
           B(Star), R(1),                 //
           B(Jump), U8(2),                //
           B(LdaUndefined),               //
           B(Return),                     //
       },
       2,
       {262, 266}},
      {"var a = 1;\n"
       "switch(a) {\n"
       " case 1: \n"
       "   switch(a + 1) {\n"
       "      case 2 : a = 1; break;\n"
       "      default : a = 2; break;\n"
       "   }  // fall-through\n"
       " case 2: a = 3;\n"
       "}\n",
       3 * kPointerSize,
       1,
5202
       52,
5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240
       {
           B(LdaSmi8), U8(1),         //
           B(Star), R(2),             //
           B(Star), R(0),             //
           B(LdaSmi8), U8(1),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(10),     //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(0),  //
           B(JumpIfTrue), U8(30),     //
           B(Jump), U8(32),           //
           B(LdaSmi8), U8(1),         //
           B(Add), R(2),              //
           B(Star), R(1),             //
           B(LdaSmi8), U8(2),         //
           B(TestEqualStrict), R(1),  //
           B(JumpIfTrue), U8(4),      //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(1),         //
           B(Star), R(2),             //
           B(Jump), U8(8),            //
           B(LdaSmi8), U8(2),         //
           B(Star), R(2),             //
           B(Jump), U8(2),            //
           B(LdaSmi8), U8(3),         //
           B(Star), R(2),             //
           B(LdaUndefined),           //
           B(Return),                 //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251

TEST(BasicBlockToBoolean) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  // Check that we don't omit ToBoolean calls if they are at the start of basic
  // blocks.
  ExpectedSnippet<int> snippets[] = {
      {"var a = 1; if (a || a < 0) { return 1; }",
       1 * kPointerSize,
       1,
5252
       16,
5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
       {
           B(LdaSmi8), U8(1),               //
           B(Star), R(0),                   //
           B(JumpIfToBooleanTrue), U8(5),   //
           B(LdaZero),                      //
           B(TestLessThan), R(0),           //
           B(JumpIfToBooleanFalse), U8(5),  //
           B(LdaSmi8), U8(1),               //
           B(Return),                       //
           B(LdaUndefined),                 //
           B(Return),                       //
       }},
      {"var a = 1; if (a && a < 0) { return 1; }",
       1 * kPointerSize,
       1,
5268
       16,
5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283
       {
           B(LdaSmi8), U8(1),               //
           B(Star), R(0),                   //
           B(JumpIfToBooleanFalse), U8(5),  //
           B(LdaZero),                      //
           B(TestLessThan), R(0),           //
           B(JumpIfToBooleanFalse), U8(5),  //
           B(LdaSmi8), U8(1),               //
           B(Return),                       //
           B(LdaUndefined),                 //
           B(Return),                       //
       }},
      {"var a = 1; a = (a || a < 0) ? 2 : 3;",
       1 * kPointerSize,
       1,
5284
       21,
5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342
       {
           B(LdaSmi8), U8(1),               //
           B(Star), R(0),                   //
           B(JumpIfToBooleanTrue), U8(5),   //
           B(LdaZero),                      //
           B(TestLessThan), R(0),           //
           B(JumpIfToBooleanFalse), U8(6),  //
           B(LdaSmi8), U8(2),               //
           B(Jump), U8(4),                  //
           B(LdaSmi8), U8(3),               //
           B(Star), R(0),                   //
           B(LdaUndefined),                 //
           B(Return),                       //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(DeadCodeRemoval) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
      {"return; var a = 1; a();",
       1 * kPointerSize,
       1,
       2,
       {
           B(LdaUndefined),  //
           B(Return),        //
       }},
      {"if (false) { return; }; var a = 1;",
       1 * kPointerSize,
       1,
       6,
       {
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(LdaUndefined),    //
           B(Return),          //
       }},
      {"if (true) { return 1; } else { return 2; };",
       0,
       1,
       3,
       {
           B(LdaSmi8), U8(1),  //
           B(Return),          //
       }},
      {"var a = 1; if (a) { return 1; }; return 2;",
       1 * kPointerSize,
       1,
5343
       12,
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361
       {
           B(LdaSmi8), U8(1),               //
           B(Star), R(0),                   //
           B(JumpIfToBooleanFalse), U8(5),  //
           B(LdaSmi8), U8(1),               //
           B(Return),                       //
           B(LdaSmi8), U8(2),               //
           B(Return),                       //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384

TEST(ThisFunction) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  int closure = Register::function_closure().index();

  ExpectedSnippet<int> snippets[] = {
      {"var f;\n f = function f() { }",
       1 * kPointerSize,
       1,
       9,
       {
           B(LdaTheHole),        //
           B(Star), R(0),        //
           B(Ldar), R(closure),  //
           B(Star), R(0),        //
           B(LdaUndefined),      //
           B(Return),            //
       }},
      {"var f;\n f = function f() { return f; }",
       1 * kPointerSize,
       1,
5385
       8,
5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401
       {
           B(LdaTheHole),        //
           B(Star), R(0),        //
           B(Ldar), R(closure),  //
           B(Star), R(0),        //
           B(Return),            //
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunction(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

5402 5403 5404 5405 5406

TEST(NewTarget) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

5407 5408
  int new_target = Register::new_target().index();

5409 5410 5411 5412
  ExpectedSnippet<int> snippets[] = {
      {"return new.target;",
       1 * kPointerSize,
       1,
5413 5414 5415 5416 5417 5418 5419 5420 5421 5422
       5,
       {
           B(Ldar), R(new_target),  //
           B(Star), R(0),           //
           B(Return),               //
       }},
      {"new.target;",
       1 * kPointerSize,
       1,
       6,
5423
       {
5424 5425 5426 5427 5428
           B(Ldar), R(new_target),  //
           B(Star), R(0),           //
           B(LdaUndefined),         //
           B(Return),               //
       }},
5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}


TEST(RemoveRedundantLdar) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<int> snippets[] = {
5444 5445 5446
      {"var ld_a = 1;\n"          // This test is to check Ldar does not
       "while(true) {\n"          // get removed if the preceding Star is
       "  ld_a = ld_a + ld_a;\n"  // in a different basicblock.
5447 5448 5449 5450 5451 5452
       "  if (ld_a > 10) break;\n"
       "}\n"
       "return ld_a;",
       1 * kPointerSize,
       1,
       23,
5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464
       {B(LdaSmi8), U8(1),         //
        B(Star), R(0),             //
        B(Ldar), R(0),             //  This load should not be removed as it
        B(Add), R(0),              //  is the target of the branch.
        B(Star), R(0),             //
        B(LdaSmi8), U8(10),        //
        B(TestGreaterThan), R(0),  //
        B(JumpIfFalse), U8(4),     //
        B(Jump), U8(4),            //
        B(Jump), U8(-14),          //
        B(Ldar), R(0),             //
        B(Return)}},
5465 5466 5467 5468 5469 5470 5471 5472
      {"var ld_a = 1;\n"
       "do {\n"
       "  ld_a = ld_a + ld_a;\n"
       "  if (ld_a > 10) continue;\n"
       "} while(false);\n"
       "return ld_a;",
       1 * kPointerSize,
       1,
5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490
       21,
       {B(LdaSmi8), U8(1),         //
        B(Star), R(0),             //
        B(Ldar), R(0),             //
        B(Add), R(0),              //
        B(Star), R(0),             //
        B(LdaSmi8), U8(10),        //
        B(TestGreaterThan), R(0),  //
        B(JumpIfFalse), U8(4),     //
        B(Jump), U8(2),            //
        B(Ldar), R(0),             //
        B(Return)}},
      {"var ld_a = 1;\n"
       "  ld_a = ld_a + ld_a;\n"
       "  return ld_a;",
       1 * kPointerSize,
       1,
       9,
5491
       {
5492 5493 5494 5495 5496
           B(LdaSmi8), U8(1),  //
           B(Star), R(0),      //
           B(Add), R(0),       //
           B(Star), R(0),      //
           B(Return)           //
5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
       }},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725

TEST(AssignmentsInBinaryExpression) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  ExpectedSnippet<const char*> snippets[] = {
      {"var x = 0, y = 1;\n"
       "return (x = 2, y = 3, x = 4, y = 5)",
       2 * kPointerSize,
       1,
       24,
       {
           B(LdaZero), B(Star), R(0),  //
           B(LdaSmi8), U8(1),          //
           B(Star), R(1),              //
           B(LdaSmi8), U8(2),          //
           B(Star), R(0),              //
           B(LdaSmi8), U8(3),          //
           B(Star), R(1),              //
           B(LdaSmi8), U8(4),          //
           B(Star), R(0),              //
           B(LdaSmi8), U8(5),          //
           B(Star), R(1),              //
           B(Return),                  //
       },
       0},
      {"var x = 55;\n"
       "var y = (x = 100);\n"
       "return y",
       2 * kPointerSize,
       1,
       11,
       {
           B(LdaSmi8), U8(55),   //
           B(Star), R(0),        //
           B(LdaSmi8), U8(100),  //
           B(Star), R(0),        //
           B(Star), R(1),        //
           B(Return),            //
       },
       0},
      {"var x = 55;\n"
       "x = x + (x = 100) + (x = 101);\n"
       "return x;",
       4 * kPointerSize,
       1,
       24,
       {
           B(LdaSmi8), U8(55),   //
           B(Star), R(0),        //
           B(LdaSmi8), U8(100),  //
           B(Star), R(1),        //
           B(Add), R(0),         //
           B(Star), R(2),        //
           B(LdaSmi8), U8(101),  //
           B(Star), R(3),        //
           B(Add), R(2),         //
           B(Mov), R(3), R(0),   //
           B(Star), R(0),        //
           B(Return),            //
       },
       0},
      {"var x = 55;\n"
       "x = (x = 56) - x + (x = 57);\n"
       "x++;\n"
       "return x;",
       3 * kPointerSize,
       1,
       34,
       {
           B(LdaSmi8), U8(55),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(56),  //
           B(Star), R(0),       //
           B(Star), R(1),       //
           B(Ldar), R(0),       //
           B(Sub), R(1),        //
           B(Star), R(2),       //
           B(LdaSmi8), U8(57),  //
           B(Star), R(1),       //
           B(Add), R(2),        //
           B(Mov), R(1), R(0),  //
           B(Star), R(0),       //
           B(ToNumber),         //
           B(Star), R(1),       //
           B(Inc),              //
           B(Star), R(0),       //
           B(Return),           //
       },
       0},
      {"var x = 55;\n"
       "var y = x + (x = 1) + (x = 2) + (x = 3);\n"
       "return y;",
       6 * kPointerSize,
       1,
       32,
       {
           B(LdaSmi8), U8(55),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(1),   //
           B(Star), R(2),       //
           B(Add), R(0),        //
           B(Star), R(3),       //
           B(LdaSmi8), U8(2),   //
           B(Star), R(4),       //
           B(Add), R(3),        //
           B(Star), R(5),       //
           B(LdaSmi8), U8(3),   //
           B(Star), R(3),       //
           B(Add), R(5),        //
           B(Mov), R(3), R(0),  //
           B(Star), R(1),       //
           B(Return),           //
       },
       0},
      {"var x = 55;\n"
       "var x = x + (x = 1) + (x = 2) + (x = 3);\n"
       "return x;",
       5 * kPointerSize,
       1,
       32,
       {
           B(LdaSmi8), U8(55),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(1),   //
           B(Star), R(1),       //
           B(Add), R(0),        //
           B(Star), R(2),       //
           B(LdaSmi8), U8(2),   //
           B(Star), R(3),       //
           B(Add), R(2),        //
           B(Star), R(4),       //
           B(LdaSmi8), U8(3),   //
           B(Star), R(2),       //
           B(Add), R(4),        //
           B(Mov), R(2), R(0),  //
           B(Star), R(0),       //
           B(Return),           //
       },
       0},
      {"var x = 10, y = 20;\n"
       "return x + (x = 1) + (x + 1) * (y = 2) + (y = 3) + (x = 4) + (y = 5) + "
       "y;\n",
       6 * kPointerSize,
       1,
       64,
       {
           B(LdaSmi8), U8(10),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(20),  //
           B(Star), R(1),       //
           B(LdaSmi8), U8(1),   //
           B(Star), R(2),       //
           B(Add), R(0),        //
           B(Star), R(3),       //
           B(LdaSmi8), U8(1),   //
           B(Add), R(2),        //
           B(Star), R(4),       //
           B(LdaSmi8), U8(2),   //
           B(Star), R(1),       //
           B(Mul), R(4),        //
           B(Add), R(3),        //
           B(Star), R(4),       //
           B(LdaSmi8), U8(3),   //
           B(Star), R(1),       //
           B(Add), R(4),        //
           B(Star), R(3),       //
           B(LdaSmi8), U8(4),   //
           B(Star), R(4),       //
           B(Add), R(3),        //
           B(Star), R(5),       //
           B(LdaSmi8), U8(5),   //
           B(Star), R(1),       //
           B(Add), R(5),        //
           B(Star), R(3),       //
           B(Ldar), R(1),       //
           B(Add), R(3),        //
           B(Mov), R(4), R(0),  //
           B(Return),           //
       },
       0},
      {"var x = 17;\n"
       "return 1 + x + (x++) + (++x);\n",
       5 * kPointerSize,
       1,
       40,
       {
           B(LdaSmi8), U8(17),  //
           B(Star), R(0),       //
           B(LdaSmi8), U8(1),   //
           B(Star), R(1),       //
           B(Ldar), R(0),       //
           B(Add), R(1),        //
           B(Star), R(2),       //
           B(Ldar), R(0),       //
           B(ToNumber),         //
           B(Star), R(1),       //
           B(Inc),              //
           B(Star), R(3),       //
           B(Ldar), R(1),       //
           B(Add), R(2),        //
           B(Star), R(4),       //
           B(Ldar), R(3),       //
           B(ToNumber),         //
           B(Inc),              //
           B(Star), R(1),       //
           B(Add), R(4),        //
           B(Mov), R(1), R(0),  //
           B(Return),           //
       },
       0}};

  for (size_t i = 0; i < arraysize(snippets); i++) {
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798

TEST(LookupSlotInEval) {
  InitializedHandleScope handle_scope;
  BytecodeGeneratorHelper helper;

  const char* function_prologue = "var f;"
                                  "var x = 1;"
                                  "function f1() {"
                                  "  eval(\"function t() {";
  const char* function_epilogue = "        }; f = t; f();\");"
                                  "}"
                                  "f1();";

  ExpectedSnippet<const char*> snippets[] = {
      {"return x;",
       0 * kPointerSize,
       1,
       3,
       {
           B(LdaLookupSlot), U8(0),  //
           B(Return)                 //
       },
       1,
       {"x"}},
      {"x = 10;",
       0 * kPointerSize,
       1,
       6,
       {
           B(LdaSmi8), U8(10),             //
           B(StaLookupSlotSloppy), U8(0),  //
           B(LdaUndefined),                //
           B(Return),                      //
       },
       1,
       {"x"}},
      {"'use strict'; x = 10;",
       0 * kPointerSize,
       1,
       6,
       {
           B(LdaSmi8), U8(10),             //
           B(StaLookupSlotStrict), U8(0),  //
           B(LdaUndefined),                //
           B(Return),                      //
       },
       1,
       {"x"}},
      {"return typeof x;",
       0 * kPointerSize,
       1,
       4,
       {
           B(LdaLookupSlotInsideTypeof), U8(0),  //
           B(TypeOf),                            //
           B(Return),                            //
       },
       1,
       {"x"}},
  };

  for (size_t i = 0; i < arraysize(snippets); i++) {
    std::string script = std::string(function_prologue) +
                         std::string(snippets[i].code_snippet) +
                         std::string(function_epilogue);
    // TODO(mythria): use * as filter when function declarations are supported
    // inside eval.
    Handle<BytecodeArray> bytecode_array =
        helper.MakeBytecode(script.c_str(), "t", "f");
    CheckBytecodeArrayEqual(snippets[i], bytecode_array);
  }
}

5799 5800
}  // namespace interpreter
}  // namespace internal
5801
}  // namespace v8