OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 Handle<Code> code = isolate->factory()->NewCode( | 305 Handle<Code> code = isolate->factory()->NewCode( |
306 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); | 306 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
307 CHECK(code->IsCode()); | 307 CHECK(code->IsCode()); |
308 | 308 |
309 F0 f = FUNCTION_CAST<F0>(code->entry()); | 309 F0 f = FUNCTION_CAST<F0>(code->entry()); |
310 int res = f(); | 310 int res = f(); |
311 CHECK_EQ(42, res); | 311 CHECK_EQ(42, res); |
312 } | 312 } |
313 | 313 |
314 | 314 |
| 315 TEST(AssemblerIa32JumpTables1) { |
| 316 // Test jump tables with forward jumps. |
| 317 CcTest::InitializeVM(); |
| 318 Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate()); |
| 319 HandleScope scope(isolate); |
| 320 Assembler assm(isolate, nullptr, 0); |
| 321 |
| 322 const int kNumCases = 512; |
| 323 int values[kNumCases]; |
| 324 isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
| 325 Label labels[kNumCases]; |
| 326 |
| 327 Label done, table; |
| 328 __ mov(eax, Operand(esp, 4)); |
| 329 __ jmp(Operand::JumpTable(eax, times_4, &table)); |
| 330 __ ud2(); |
| 331 __ bind(&table); |
| 332 for (int i = 0; i < kNumCases; ++i) { |
| 333 __ dd(&labels[i]); |
| 334 } |
| 335 |
| 336 for (int i = 0; i < kNumCases; ++i) { |
| 337 __ bind(&labels[i]); |
| 338 __ mov(eax, Immediate(values[i])); |
| 339 __ jmp(&done); |
| 340 } |
| 341 |
| 342 __ bind(&done); |
| 343 __ ret(0); |
| 344 |
| 345 CodeDesc desc; |
| 346 assm.GetCode(&desc); |
| 347 Handle<Code> code = isolate->factory()->NewCode( |
| 348 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 349 #ifdef OBJECT_PRINT |
| 350 OFStream os(stdout); |
| 351 code->Print(os); |
| 352 #endif |
| 353 F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 354 for (int i = 0; i < kNumCases; ++i) { |
| 355 int res = f(i); |
| 356 ::printf("f(%d) = %d\n", i, res); |
| 357 CHECK_EQ(values[i], res); |
| 358 } |
| 359 } |
| 360 |
| 361 |
| 362 TEST(AssemblerIa32JumpTables2) { |
| 363 // Test jump tables with backward jumps. |
| 364 CcTest::InitializeVM(); |
| 365 Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate()); |
| 366 HandleScope scope(isolate); |
| 367 Assembler assm(isolate, nullptr, 0); |
| 368 |
| 369 const int kNumCases = 512; |
| 370 int values[kNumCases]; |
| 371 isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
| 372 Label labels[kNumCases]; |
| 373 |
| 374 Label done, table; |
| 375 __ mov(eax, Operand(esp, 4)); |
| 376 __ jmp(Operand::JumpTable(eax, times_4, &table)); |
| 377 __ ud2(); |
| 378 |
| 379 for (int i = 0; i < kNumCases; ++i) { |
| 380 __ bind(&labels[i]); |
| 381 __ mov(eax, Immediate(values[i])); |
| 382 __ jmp(&done); |
| 383 } |
| 384 |
| 385 __ bind(&table); |
| 386 for (int i = 0; i < kNumCases; ++i) { |
| 387 __ dd(&labels[i]); |
| 388 } |
| 389 |
| 390 __ bind(&done); |
| 391 __ ret(0); |
| 392 |
| 393 CodeDesc desc; |
| 394 assm.GetCode(&desc); |
| 395 Handle<Code> code = isolate->factory()->NewCode( |
| 396 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 397 #ifdef OBJECT_PRINT |
| 398 OFStream os(stdout); |
| 399 code->Print(os); |
| 400 #endif |
| 401 F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 402 for (int i = 0; i < kNumCases; ++i) { |
| 403 int res = f(i); |
| 404 ::printf("f(%d) = %d\n", i, res); |
| 405 CHECK_EQ(values[i], res); |
| 406 } |
| 407 } |
| 408 |
315 #undef __ | 409 #undef __ |
OLD | NEW |