Index: test/cctest/test-assembler-x87.cc |
diff --git a/test/cctest/test-assembler-x87.cc b/test/cctest/test-assembler-x87.cc |
index 17ec2f2794e7bd11a4c22ef2d4b54a52a3686e33..c07be845b9ce3138df1326c6ca5c94f3c929a16e 100644 |
--- a/test/cctest/test-assembler-x87.cc |
+++ b/test/cctest/test-assembler-x87.cc |
@@ -312,4 +312,98 @@ TEST(AssemblerMultiByteNop) { |
} |
+TEST(AssemblerIa32JumpTables1) { |
+ // Test jump tables with forward jumps. |
+ CcTest::InitializeVM(); |
+ Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate()); |
+ HandleScope scope(isolate); |
+ Assembler assm(isolate, nullptr, 0); |
+ |
+ const int kNumCases = 512; |
+ int values[kNumCases]; |
+ isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
+ Label labels[kNumCases]; |
+ |
+ Label done, table; |
+ __ mov(eax, Operand(esp, 4)); |
+ __ jmp(Operand::JumpTable(eax, times_4, &table)); |
+ __ ud2(); |
+ __ bind(&table); |
+ for (int i = 0; i < kNumCases; ++i) { |
+ __ dd(&labels[i]); |
+ } |
+ |
+ for (int i = 0; i < kNumCases; ++i) { |
+ __ bind(&labels[i]); |
+ __ mov(eax, Immediate(values[i])); |
+ __ jmp(&done); |
+ } |
+ |
+ __ bind(&done); |
+ __ ret(0); |
+ |
+ CodeDesc desc; |
+ assm.GetCode(&desc); |
+ Handle<Code> code = isolate->factory()->NewCode( |
+ desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
+#ifdef OBJECT_PRINT |
+ OFStream os(stdout); |
+ code->Print(os); |
+#endif |
+ F1 f = FUNCTION_CAST<F1>(code->entry()); |
+ for (int i = 0; i < kNumCases; ++i) { |
+ int res = f(i); |
+ ::printf("f(%d) = %d\n", i, res); |
+ CHECK_EQ(values[i], res); |
+ } |
+} |
+ |
+ |
+TEST(AssemblerIa32JumpTables2) { |
+ // Test jump tables with backward jumps. |
+ CcTest::InitializeVM(); |
+ Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate()); |
+ HandleScope scope(isolate); |
+ Assembler assm(isolate, nullptr, 0); |
+ |
+ const int kNumCases = 512; |
+ int values[kNumCases]; |
+ isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
+ Label labels[kNumCases]; |
+ |
+ Label done, table; |
+ __ mov(eax, Operand(esp, 4)); |
+ __ jmp(Operand::JumpTable(eax, times_4, &table)); |
+ __ ud2(); |
+ |
+ for (int i = 0; i < kNumCases; ++i) { |
+ __ bind(&labels[i]); |
+ __ mov(eax, Immediate(values[i])); |
+ __ jmp(&done); |
+ } |
+ |
+ __ bind(&table); |
+ for (int i = 0; i < kNumCases; ++i) { |
+ __ dd(&labels[i]); |
+ } |
+ |
+ __ bind(&done); |
+ __ ret(0); |
+ |
+ CodeDesc desc; |
+ assm.GetCode(&desc); |
+ Handle<Code> code = isolate->factory()->NewCode( |
+ desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
+#ifdef OBJECT_PRINT |
+ OFStream os(stdout); |
+ code->Print(os); |
+#endif |
+ F1 f = FUNCTION_CAST<F1>(code->entry()); |
+ for (int i = 0; i < kNumCases; ++i) { |
+ int res = f(i); |
+ ::printf("f(%d) = %d\n", i, res); |
+ CHECK_EQ(values[i], res); |
+ } |
+} |
+ |
#undef __ |