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

Side by Side Diff: test/cctest/interpreter/test-interpreter.cc

Issue 1392913002: [Interpreter] Adds shift operators to interpreter (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: fixed a typo in comment Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/execution.h" 7 #include "src/execution.h"
8 #include "src/handles.h" 8 #include "src/handles.h"
9 #include "src/interpreter/bytecode-array-builder.h" 9 #include "src/interpreter/bytecode-array-builder.h"
10 #include "src/interpreter/interpreter.h" 10 #include "src/interpreter/interpreter.h"
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 345
346 InterpreterTester tester(handles.main_isolate(), bytecode_array); 346 InterpreterTester tester(handles.main_isolate(), bytecode_array);
347 auto callable = tester.GetCallable<>(); 347 auto callable = tester.GetCallable<>();
348 Handle<Object> return_val = callable().ToHandleChecked(); 348 Handle<Object> return_val = callable().ToHandleChecked();
349 CHECK(return_val.is_identical_to(true_value)); 349 CHECK(return_val.is_identical_to(true_value));
350 } 350 }
351 } 351 }
352 352
353 353
354 static const Token::Value kArithmeticOperators[] = { 354 static const Token::Value kArithmeticOperators[] = {
355 Token::Value::ADD, Token::Value::SUB, Token::Value::MUL, Token::Value::DIV, 355 Token::Value::SHL, Token::Value::SAR, Token::Value::SHR, Token::Value::ADD,
356 Token::Value::MOD}; 356 Token::Value::SUB, Token::Value::MUL, Token::Value::DIV, Token::Value::MOD};
357
358
359 static const Token::Value kShiftOperators[] = {
360 Token::Value::SHL, Token::Value::SAR, Token::Value::SHR};
357 361
358 362
359 static double BinaryOpC(Token::Value op, double lhs, double rhs) { 363 static double BinaryOpC(Token::Value op, double lhs, double rhs) {
360 switch (op) { 364 switch (op) {
361 case Token::Value::ADD: 365 case Token::Value::ADD:
362 return lhs + rhs; 366 return lhs + rhs;
363 case Token::Value::SUB: 367 case Token::Value::SUB:
364 return lhs - rhs; 368 return lhs - rhs;
365 case Token::Value::MUL: 369 case Token::Value::MUL:
366 return lhs * rhs; 370 return lhs * rhs;
367 case Token::Value::DIV: 371 case Token::Value::DIV:
368 return lhs / rhs; 372 return lhs / rhs;
369 case Token::Value::MOD: 373 case Token::Value::MOD:
370 return std::fmod(lhs, rhs); 374 return std::fmod(lhs, rhs);
375 case Token::Value::SHL: {
376 int32_t val = v8::internal::DoubleToInt32(lhs);
377 uint32_t count = v8::internal::DoubleToUint32(rhs) & 0x1F;
378 return val << count;
379 }
380 case Token::Value::SAR: {
381 int32_t val = v8::internal::DoubleToInt32(lhs);
382 uint32_t count = v8::internal::DoubleToUint32(rhs) & 0x1F;
383 return val >> count;
384 }
385 case Token::Value::SHR: {
386 uint32_t val = v8::internal::DoubleToUint32(lhs);
387 uint32_t count = v8::internal::DoubleToUint32(rhs) & 0x1F;
388 return val >> count;
389 }
371 default: 390 default:
372 UNREACHABLE(); 391 UNREACHABLE();
373 return std::numeric_limits<double>::min(); 392 return std::numeric_limits<double>::min();
374 } 393 }
375 } 394 }
376 395
377 396
397 TEST(InterpreterShiftOpsSmi) {
398 int lhs_inputs[] = {0, -17, -182, 2147483647, -1};
399 int rhs_inputs[] = {5, 2, 1, -1, -2, 0, 31, 32, -32, 64, 37};
400 for (size_t l = 0; l < arraysize(lhs_inputs); l++) {
401 for (size_t r = 0; r < arraysize(rhs_inputs); r++) {
402 for (size_t o = 0; o < arraysize(kShiftOperators); o++) {
403 HandleAndZoneScope handles;
404 i::Factory* factory = handles.main_isolate()->factory();
405 BytecodeArrayBuilder builder(handles.main_isolate(),
406 handles.main_zone());
407 builder.set_locals_count(1);
408 builder.set_parameter_count(1);
409 Register reg(0);
410 int lhs = lhs_inputs[l];
411 int rhs = rhs_inputs[l];
412 builder.LoadLiteral(Smi::FromInt(lhs))
413 .StoreAccumulatorInRegister(reg)
414 .LoadLiteral(Smi::FromInt(rhs))
415 .BinaryOperation(kArithmeticOperators[o], reg, Strength::WEAK)
416 .Return();
417 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray();
418
419 InterpreterTester tester(handles.main_isolate(), bytecode_array);
420 auto callable = tester.GetCallable<>();
421 Handle<Object> return_value = callable().ToHandleChecked();
422 Handle<Object> expected_value =
423 factory->NewNumber(BinaryOpC(kShiftOperators[o], lhs, rhs));
424 CHECK(return_value->SameValue(*expected_value));
425 }
426 }
427 }
428 }
429
430
378 TEST(InterpreterBinaryOpsSmi) { 431 TEST(InterpreterBinaryOpsSmi) {
379 int lhs_inputs[] = {3266, 1024, 0, -17, -18000}; 432 int lhs_inputs[] = {3266, 1024, 0, -17, -18000};
380 int rhs_inputs[] = {3266, 5, 4, 3, 2, 1, -1, -2}; 433 int rhs_inputs[] = {3266, 5, 4, 3, 2, 1, -1, -2};
381 for (size_t l = 0; l < arraysize(lhs_inputs); l++) { 434 for (size_t l = 0; l < arraysize(lhs_inputs); l++) {
382 for (size_t r = 0; r < arraysize(rhs_inputs); r++) { 435 for (size_t r = 0; r < arraysize(rhs_inputs); r++) {
383 for (size_t o = 0; o < arraysize(kArithmeticOperators); o++) { 436 for (size_t o = 0; o < arraysize(kArithmeticOperators); o++) {
384 HandleAndZoneScope handles; 437 HandleAndZoneScope handles;
385 i::Factory* factory = handles.main_isolate()->factory(); 438 i::Factory* factory = handles.main_isolate()->factory();
386 BytecodeArrayBuilder builder(handles.main_isolate(), 439 BytecodeArrayBuilder builder(handles.main_isolate(),
387 handles.main_zone()); 440 handles.main_zone());
(...skipping 1076 matching lines...) Expand 10 before | Expand all | Expand 10 after
1464 .CallRuntime(Runtime::kAdd, Register(0), 2) 1517 .CallRuntime(Runtime::kAdd, Register(0), 2)
1465 .Return(); 1518 .Return();
1466 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); 1519 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray();
1467 1520
1468 InterpreterTester tester(handles.main_isolate(), bytecode_array); 1521 InterpreterTester tester(handles.main_isolate(), bytecode_array);
1469 auto callable = tester.GetCallable<>(); 1522 auto callable = tester.GetCallable<>();
1470 1523
1471 Handle<Object> return_val = callable().ToHandleChecked(); 1524 Handle<Object> return_val = callable().ToHandleChecked();
1472 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(55)); 1525 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(55));
1473 } 1526 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698