OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 | 2 |
3 // Test constant pool array code. | 3 // Test constant pool array code. |
4 | 4 |
5 #include "v8.h" | 5 #include "v8.h" |
6 | 6 |
7 #include "factory.h" | 7 #include "factory.h" |
8 #include "objects.h" | 8 #include "objects.h" |
9 #include "cctest.h" | 9 #include "cctest.h" |
10 | 10 |
11 using namespace v8::internal; | 11 using namespace v8::internal; |
12 | 12 |
| 13 static ConstantPoolArray::Type kTypes[] = { ConstantPoolArray::INT64, |
| 14 ConstantPoolArray::CODE_PTR, |
| 15 ConstantPoolArray::HEAP_PTR, |
| 16 ConstantPoolArray::INT32 }; |
| 17 static ConstantPoolArray::LayoutSection kSmall = |
| 18 ConstantPoolArray::SMALL_SECTION; |
| 19 static ConstantPoolArray::LayoutSection kExtended = |
| 20 ConstantPoolArray::EXTENDED_SECTION; |
13 | 21 |
14 Code* DummyCode(LocalContext* context) { | 22 Code* DummyCode(LocalContext* context) { |
15 CompileRun("function foo() {};"); | 23 CompileRun("function foo() {};"); |
16 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( | 24 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( |
17 *v8::Local<v8::Function>::Cast( | 25 *v8::Local<v8::Function>::Cast( |
18 (*context)->Global()->Get(v8_str("foo")))); | 26 (*context)->Global()->Get(v8_str("foo")))); |
19 return fun->code(); | 27 return fun->code(); |
20 } | 28 } |
21 | 29 |
22 | 30 |
23 TEST(ConstantPool) { | 31 TEST(ConstantPoolSmall) { |
24 LocalContext context; | 32 LocalContext context; |
25 Isolate* isolate = CcTest::i_isolate(); | 33 Isolate* isolate = CcTest::i_isolate(); |
26 Heap* heap = isolate->heap(); | 34 Heap* heap = isolate->heap(); |
27 Factory* factory = isolate->factory(); | 35 Factory* factory = isolate->factory(); |
28 v8::HandleScope scope(context->GetIsolate()); | 36 v8::HandleScope scope(context->GetIsolate()); |
29 | 37 |
30 // Check construction. | 38 // Check construction. |
31 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(3, 1, 2, 1); | 39 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(3, 1, 2, 1); |
32 CHECK_EQ(array->count_of_int64_entries(), 3); | 40 |
33 CHECK_EQ(array->count_of_code_ptr_entries(), 1); | 41 int expected_counts[] = { 3, 1, 2, 1 }; |
34 CHECK_EQ(array->count_of_heap_ptr_entries(), 2); | 42 int expected_first_idx[] = { 0, 3, 4, 6 }; |
35 CHECK_EQ(array->count_of_int32_entries(), 1); | 43 int expected_last_idx[] = { 2, 3, 5, 6 }; |
36 CHECK_EQ(array->length(), 7); | 44 for (int i = 0; i < 4; i++) { |
37 CHECK_EQ(array->first_int64_index(), 0); | 45 CHECK_EQ(expected_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
38 CHECK_EQ(array->first_code_ptr_index(), 3); | 46 CHECK_EQ(expected_first_idx[i], array->first_index(kTypes[i], kSmall)); |
39 CHECK_EQ(array->first_heap_ptr_index(), 4); | 47 CHECK_EQ(expected_last_idx[i], array->last_index(kTypes[i], kSmall)); |
40 CHECK_EQ(array->first_int32_index(), 6); | 48 } |
| 49 CHECK(!array->is_extended_layout()); |
41 | 50 |
42 // Check getters and setters. | 51 // Check getters and setters. |
43 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); | 52 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
44 Handle<Object> object = factory->NewHeapNumber(4.0); | 53 Handle<Object> object = factory->NewHeapNumber(4.0); |
45 Code* code = DummyCode(&context); | 54 Code* code = DummyCode(&context); |
46 array->set(0, big_number); | 55 array->set(0, big_number); |
47 array->set(1, 0.5); | 56 array->set(1, 0.5); |
48 array->set(2, 3e-24); | 57 array->set(2, 3e-24); |
49 array->set(3, code->entry()); | 58 array->set(3, code->entry()); |
50 array->set(4, code); | 59 array->set(4, code); |
51 array->set(5, *object); | 60 array->set(5, *object); |
52 array->set(6, 50); | 61 array->set(6, 50); |
53 CHECK_EQ(array->get_int64_entry(0), big_number); | 62 CHECK_EQ(big_number, array->get_int64_entry(0)); |
54 CHECK_EQ(array->get_int64_entry_as_double(1), 0.5); | 63 CHECK_EQ(0.5, array->get_int64_entry_as_double(1)); |
55 CHECK_EQ(array->get_int64_entry_as_double(2), 3e-24); | 64 CHECK_EQ(3e-24, array->get_int64_entry_as_double(2)); |
56 CHECK_EQ(array->get_code_ptr_entry(3), code->entry()); | 65 CHECK_EQ(code->entry(), array->get_code_ptr_entry(3)); |
57 CHECK_EQ(array->get_heap_ptr_entry(4), code); | 66 CHECK_EQ(code, array->get_heap_ptr_entry(4)); |
58 CHECK_EQ(array->get_heap_ptr_entry(5), *object); | 67 CHECK_EQ(*object, array->get_heap_ptr_entry(5)); |
59 CHECK_EQ(array->get_int32_entry(6), 50); | 68 CHECK_EQ(50, array->get_int32_entry(6)); |
60 | 69 |
61 // Check pointers are updated on GC. | 70 // Check pointers are updated on GC. |
62 Object* old_ptr = array->get_heap_ptr_entry(5); | 71 Object* old_ptr = array->get_heap_ptr_entry(5); |
63 CHECK_EQ(*object, old_ptr); | 72 CHECK_EQ(*object, old_ptr); |
64 heap->CollectGarbage(NEW_SPACE); | 73 heap->CollectGarbage(NEW_SPACE); |
65 Object* new_ptr = array->get_heap_ptr_entry(5); | 74 Object* new_ptr = array->get_heap_ptr_entry(5); |
66 CHECK_NE(*object, old_ptr); | 75 CHECK_NE(*object, old_ptr); |
67 CHECK_EQ(*object, new_ptr); | 76 CHECK_EQ(*object, new_ptr); |
68 } | 77 } |
| 78 |
| 79 |
| 80 TEST(ConstantPoolExtended) { |
| 81 LocalContext context; |
| 82 Isolate* isolate = CcTest::i_isolate(); |
| 83 Heap* heap = isolate->heap(); |
| 84 Factory* factory = isolate->factory(); |
| 85 v8::HandleScope scope(context->GetIsolate()); |
| 86 |
| 87 // Check construction. |
| 88 Handle<ConstantPoolArray> array = |
| 89 factory->NewExtendedConstantPoolArray(1, 2, 3, 4, 5, 6, 7, 8); |
| 90 |
| 91 // Check small section. |
| 92 int small_counts[] = { 1, 2, 3, 4 }; |
| 93 int small_first_idx[] = { 0, 1, 3, 6 }; |
| 94 int small_last_idx[] = { 0, 2, 5, 9 }; |
| 95 for (int i = 0; i < 4; i++) { |
| 96 CHECK_EQ(small_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
| 97 CHECK_EQ(small_first_idx[i], array->first_index(kTypes[i], kSmall)); |
| 98 CHECK_EQ(small_last_idx[i], array->last_index(kTypes[i], kSmall)); |
| 99 } |
| 100 |
| 101 // Check extended layout. |
| 102 CHECK(array->is_extended_layout()); |
| 103 int extended_counts[] = { 5, 6, 7, 8 }; |
| 104 int extended_first_idx[] = { 10, 15, 21, 28 }; |
| 105 int extended_last_idx[] = { 14, 20, 27, 35 }; |
| 106 for (int i = 0; i < 4; i++) { |
| 107 CHECK_EQ(extended_counts[i], |
| 108 array->number_of_entries(kTypes[i], kExtended)); |
| 109 CHECK_EQ(extended_first_idx[i], array->first_index(kTypes[i], kExtended)); |
| 110 CHECK_EQ(extended_last_idx[i], array->last_index(kTypes[i], kExtended)); |
| 111 } |
| 112 |
| 113 // Check small and large section's don't overlap. |
| 114 int64_t small_section_int64 = V8_2PART_UINT64_C(0x56781234, DEF09ABC); |
| 115 Code* small_section_code_ptr = DummyCode(&context); |
| 116 Handle<Object> small_section_heap_ptr = factory->NewHeapNumber(4.0); |
| 117 int32_t small_section_int32 = 0xab12cd45; |
| 118 |
| 119 int64_t extended_section_int64 = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
| 120 Code* extended_section_code_ptr = DummyCode(&context); |
| 121 Handle<Object> extended_section_heap_ptr = factory->NewHeapNumber(4.0); |
| 122 int32_t extended_section_int32 = 0xef67ab89; |
| 123 |
| 124 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
| 125 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
| 126 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
| 127 array->set(i, small_section_int64); |
| 128 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
| 129 array->set(i, small_section_code_ptr->entry()); |
| 130 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
| 131 array->set(i, *small_section_heap_ptr); |
| 132 } else { |
| 133 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kSmall)); |
| 134 array->set(i, small_section_int32); |
| 135 } |
| 136 } |
| 137 for (int i = array->first_index(ConstantPoolArray::INT64, kExtended); |
| 138 i <= array->last_index(ConstantPoolArray::INT32, kExtended); i++) { |
| 139 if (i <= array->last_index(ConstantPoolArray::INT64, kExtended)) { |
| 140 array->set(i, extended_section_int64); |
| 141 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
| 142 array->set(i, extended_section_code_ptr->entry()); |
| 143 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
| 144 array->set(i, *extended_section_heap_ptr); |
| 145 } else { |
| 146 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
| 147 array->set(i, extended_section_int32); |
| 148 } |
| 149 } |
| 150 |
| 151 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
| 152 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
| 153 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
| 154 CHECK_EQ(small_section_int64, array->get_int64_entry(i)); |
| 155 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
| 156 CHECK_EQ(small_section_code_ptr->entry(), array->get_code_ptr_entry(i)); |
| 157 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
| 158 CHECK_EQ(*small_section_heap_ptr, array->get_heap_ptr_entry(i)); |
| 159 } else { |
| 160 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kSmall)); |
| 161 CHECK_EQ(small_section_int32, array->get_int32_entry(i)); |
| 162 } |
| 163 } |
| 164 for (int i = array->first_index(ConstantPoolArray::INT64, kExtended); |
| 165 i <= array->last_index(ConstantPoolArray::INT32, kExtended); i++) { |
| 166 if (i <= array->last_index(ConstantPoolArray::INT64, kExtended)) { |
| 167 CHECK_EQ(extended_section_int64, array->get_int64_entry(i)); |
| 168 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
| 169 CHECK_EQ(extended_section_code_ptr->entry(), |
| 170 array->get_code_ptr_entry(i)); |
| 171 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
| 172 CHECK_EQ(*extended_section_heap_ptr, array->get_heap_ptr_entry(i)); |
| 173 } else { |
| 174 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
| 175 CHECK_EQ(extended_section_int32, array->get_int32_entry(i)); |
| 176 } |
| 177 } |
| 178 // Check pointers are updated on GC in extended section. |
| 179 int index = array->first_index(ConstantPoolArray::HEAP_PTR, kExtended); |
| 180 Object* old_ptr = array->get_heap_ptr_entry(index); |
| 181 CHECK_EQ(*extended_section_heap_ptr, old_ptr); |
| 182 heap->CollectGarbage(NEW_SPACE); |
| 183 Object* new_ptr = array->get_heap_ptr_entry(index); |
| 184 CHECK_NE(*extended_section_heap_ptr, old_ptr); |
| 185 CHECK_EQ(*extended_section_heap_ptr, new_ptr); |
| 186 } |
| 187 |
| 188 |
| 189 static void CheckIterator(Handle<ConstantPoolArray> array, |
| 190 ConstantPoolArray::Type type, |
| 191 int expected_indexes[], |
| 192 int count) { |
| 193 int i = 0; |
| 194 ConstantPoolArray::Iterator iter(*array, type); |
| 195 while (!iter.is_finished()) { |
| 196 CHECK_EQ(expected_indexes[i++], iter.next_index()); |
| 197 } |
| 198 CHECK_EQ(count, i); |
| 199 } |
| 200 |
| 201 |
| 202 TEST(ConstantPoolIterator) { |
| 203 LocalContext context; |
| 204 Isolate* isolate = CcTest::i_isolate(); |
| 205 Factory* factory = isolate->factory(); |
| 206 v8::HandleScope scope(context->GetIsolate()); |
| 207 |
| 208 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(1, 5, 2, 0); |
| 209 |
| 210 int expected_int64_indexs[] = { 0 }; |
| 211 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 1); |
| 212 int expected_code_indexs[] = { 1, 2, 3, 4, 5 }; |
| 213 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 5); |
| 214 int expected_heap_indexs[] = { 6, 7 }; |
| 215 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 2); |
| 216 int expected_int32_indexs[1]; |
| 217 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 0); |
| 218 } |
| 219 |
| 220 |
| 221 TEST(ConstantPoolIteratorExtended) { |
| 222 LocalContext context; |
| 223 Isolate* isolate = CcTest::i_isolate(); |
| 224 Factory* factory = isolate->factory(); |
| 225 v8::HandleScope scope(context->GetIsolate()); |
| 226 |
| 227 Handle<ConstantPoolArray> array = |
| 228 factory->NewExtendedConstantPoolArray(1, 0, 0, 4, 5, 0, 3, 0); |
| 229 |
| 230 int expected_int64_indexs[] = { 0, 5, 6, 7, 8, 9 }; |
| 231 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 6); |
| 232 int expected_code_indexs[1]; |
| 233 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 0); |
| 234 int expected_heap_indexs[] = { 10, 11, 12 }; |
| 235 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 3); |
| 236 int expected_int32_indexs[] = { 1, 2, 3, 4 }; |
| 237 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 4); |
| 238 } |
OLD | NEW |