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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 __ lw(result, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); | 81 __ lw(result, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
82 __ lw(result, | 82 __ lw(result, |
83 FieldMemOperand(result, GlobalObject::kGlobalContextOffset)); | 83 FieldMemOperand(result, GlobalObject::kGlobalContextOffset)); |
84 // Load the Array function from the global context. | 84 // Load the Array function from the global context. |
85 __ lw(result, | 85 __ lw(result, |
86 MemOperand(result, | 86 MemOperand(result, |
87 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX))); | 87 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX))); |
88 } | 88 } |
89 | 89 |
90 | 90 |
91 // This constant has the same value as JSArray::kPreallocatedArrayElements and | |
92 // if JSArray::kPreallocatedArrayElements is changed handling of loop unfolding | |
93 // below should be reconsidered. | |
94 static const int kLoopUnfoldLimit = 4; | |
95 | |
96 | |
97 // Allocate an empty JSArray. The allocated array is put into the result | 91 // Allocate an empty JSArray. The allocated array is put into the result |
98 // register. An elements backing store is allocated with size initial_capacity | 92 // register. An elements backing store is allocated with size initial_capacity |
99 // and filled with the hole values. | 93 // and filled with the hole values. |
100 static void AllocateEmptyJSArray(MacroAssembler* masm, | 94 static void AllocateEmptyJSArray(MacroAssembler* masm, |
101 Register array_function, | 95 Register array_function, |
102 Register result, | 96 Register result, |
103 Register scratch1, | 97 Register scratch1, |
104 Register scratch2, | 98 Register scratch2, |
105 Register scratch3, | 99 Register scratch3, |
106 int initial_capacity, | |
107 Label* gc_required) { | 100 Label* gc_required) { |
108 ASSERT(initial_capacity > 0); | 101 const int initial_capacity = JSArray::kPreallocatedArrayElements; |
| 102 STATIC_ASSERT(initial_capacity >= 0); |
109 // Load the initial map from the array function. | 103 // Load the initial map from the array function. |
110 __ lw(scratch1, FieldMemOperand(array_function, | 104 __ lw(scratch1, FieldMemOperand(array_function, |
111 JSFunction::kPrototypeOrInitialMapOffset)); | 105 JSFunction::kPrototypeOrInitialMapOffset)); |
112 | 106 |
113 // Allocate the JSArray object together with space for a fixed array with the | 107 // Allocate the JSArray object together with space for a fixed array with the |
114 // requested elements. | 108 // requested elements. |
115 int size = JSArray::kSize + FixedArray::SizeFor(initial_capacity); | 109 int size = JSArray::kSize + FixedArray::SizeFor(initial_capacity); |
116 __ AllocateInNewSpace(size, | 110 __ AllocateInNewSpace(size, |
117 result, | 111 result, |
118 scratch2, | 112 scratch2, |
(...skipping 29 matching lines...) Expand all Loading... |
148 // scratch2: start of next object | 142 // scratch2: start of next object |
149 __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex); | 143 __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex); |
150 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset); | 144 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset); |
151 __ sw(scratch3, MemOperand(scratch1)); | 145 __ sw(scratch3, MemOperand(scratch1)); |
152 __ Addu(scratch1, scratch1, kPointerSize); | 146 __ Addu(scratch1, scratch1, kPointerSize); |
153 __ li(scratch3, Operand(Smi::FromInt(initial_capacity))); | 147 __ li(scratch3, Operand(Smi::FromInt(initial_capacity))); |
154 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset); | 148 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset); |
155 __ sw(scratch3, MemOperand(scratch1)); | 149 __ sw(scratch3, MemOperand(scratch1)); |
156 __ Addu(scratch1, scratch1, kPointerSize); | 150 __ Addu(scratch1, scratch1, kPointerSize); |
157 | 151 |
158 // Fill the FixedArray with the hole value. | 152 // Fill the FixedArray with the hole value. Inline the code if short. |
| 153 if (initial_capacity == 0) return; |
159 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize); | 154 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize); |
160 ASSERT(initial_capacity <= kLoopUnfoldLimit); | |
161 __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex); | 155 __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex); |
162 for (int i = 0; i < initial_capacity; i++) { | 156 static const int kLoopUnfoldLimit = 4; |
| 157 if (initial_capacity <= kLoopUnfoldLimit) { |
| 158 for (int i = 0; i < initial_capacity; i++) { |
| 159 __ sw(scratch3, MemOperand(scratch1, i * kPointerSize)); |
| 160 } |
| 161 } else { |
| 162 Label loop, entry; |
| 163 __ Addu(scratch2, scratch1, Operand(initial_capacity * kPointerSize)); |
| 164 __ Branch(&entry); |
| 165 __ bind(&loop); |
163 __ sw(scratch3, MemOperand(scratch1)); | 166 __ sw(scratch3, MemOperand(scratch1)); |
164 __ Addu(scratch1, scratch1, kPointerSize); | 167 __ Addu(scratch1, scratch1, kPointerSize); |
| 168 __ bind(&entry); |
| 169 __ Branch(&loop, lt, scratch1, Operand(scratch2)); |
165 } | 170 } |
166 } | 171 } |
167 | 172 |
168 | 173 |
169 // Allocate a JSArray with the number of elements stored in a register. The | 174 // Allocate a JSArray with the number of elements stored in a register. The |
170 // register array_function holds the built-in Array function and the register | 175 // register array_function holds the built-in Array function and the register |
171 // array_size holds the size of the array as a smi. The allocated array is put | 176 // array_size holds the size of the array as a smi. The allocated array is put |
172 // into the result register and beginning and end of the FixedArray elements | 177 // into the result register and beginning and end of the FixedArray elements |
173 // storage is put into registers elements_array_storage and elements_array_end | 178 // storage is put into registers elements_array_storage and elements_array_end |
174 // (see below for when that is not the case). If the parameter fill_with_holes | 179 // (see below for when that is not the case). If the parameter fill_with_holes |
175 // is true the allocated elements backing store is filled with the hole values | 180 // is true the allocated elements backing store is filled with the hole values |
176 // otherwise it is left uninitialized. When the backing store is filled the | 181 // otherwise it is left uninitialized. When the backing store is filled the |
177 // register elements_array_storage is scratched. | 182 // register elements_array_storage is scratched. |
178 static void AllocateJSArray(MacroAssembler* masm, | 183 static void AllocateJSArray(MacroAssembler* masm, |
179 Register array_function, // Array function. | 184 Register array_function, // Array function. |
180 Register array_size, // As a smi. | 185 Register array_size, // As a smi, cannot be 0. |
181 Register result, | 186 Register result, |
182 Register elements_array_storage, | 187 Register elements_array_storage, |
183 Register elements_array_end, | 188 Register elements_array_end, |
184 Register scratch1, | 189 Register scratch1, |
185 Register scratch2, | 190 Register scratch2, |
186 bool fill_with_hole, | 191 bool fill_with_hole, |
187 Label* gc_required) { | 192 Label* gc_required) { |
188 Label not_empty, allocated; | |
189 | |
190 // Load the initial map from the array function. | 193 // Load the initial map from the array function. |
191 __ lw(elements_array_storage, | 194 __ lw(elements_array_storage, |
192 FieldMemOperand(array_function, | 195 FieldMemOperand(array_function, |
193 JSFunction::kPrototypeOrInitialMapOffset)); | 196 JSFunction::kPrototypeOrInitialMapOffset)); |
194 | 197 |
195 // Check whether an empty sized array is requested. | 198 if (FLAG_debug_code) { // Assert that array size is not zero. |
196 __ Branch(¬_empty, ne, array_size, Operand(zero_reg)); | 199 __ Assert( |
197 | 200 ne, "array size is unexpectedly 0", array_size, Operand(zero_reg)); |
198 // If an empty array is requested allocate a small elements array anyway. This | 201 } |
199 // keeps the code below free of special casing for the empty array. | |
200 int size = JSArray::kSize + | |
201 FixedArray::SizeFor(JSArray::kPreallocatedArrayElements); | |
202 __ AllocateInNewSpace(size, | |
203 result, | |
204 elements_array_end, | |
205 scratch1, | |
206 gc_required, | |
207 TAG_OBJECT); | |
208 __ Branch(&allocated); | |
209 | 202 |
210 // Allocate the JSArray object together with space for a FixedArray with the | 203 // Allocate the JSArray object together with space for a FixedArray with the |
211 // requested number of elements. | 204 // requested number of elements. |
212 __ bind(¬_empty); | |
213 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0); | 205 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0); |
214 __ li(elements_array_end, | 206 __ li(elements_array_end, |
215 (JSArray::kSize + FixedArray::kHeaderSize) / kPointerSize); | 207 (JSArray::kSize + FixedArray::kHeaderSize) / kPointerSize); |
216 __ sra(scratch1, array_size, kSmiTagSize); | 208 __ sra(scratch1, array_size, kSmiTagSize); |
217 __ Addu(elements_array_end, elements_array_end, scratch1); | 209 __ Addu(elements_array_end, elements_array_end, scratch1); |
218 __ AllocateInNewSpace( | 210 __ AllocateInNewSpace( |
219 elements_array_end, | 211 elements_array_end, |
220 result, | 212 result, |
221 scratch1, | 213 scratch1, |
222 scratch2, | 214 scratch2, |
223 gc_required, | 215 gc_required, |
224 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS)); | 216 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS)); |
225 | 217 |
226 // Allocated the JSArray. Now initialize the fields except for the elements | 218 // Allocated the JSArray. Now initialize the fields except for the elements |
227 // array. | 219 // array. |
228 // result: JSObject | 220 // result: JSObject |
229 // elements_array_storage: initial map | 221 // elements_array_storage: initial map |
230 // array_size: size of array (smi) | 222 // array_size: size of array (smi) |
231 __ bind(&allocated); | |
232 __ sw(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset)); | 223 __ sw(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset)); |
233 __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex); | 224 __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex); |
234 __ sw(elements_array_storage, | 225 __ sw(elements_array_storage, |
235 FieldMemOperand(result, JSArray::kPropertiesOffset)); | 226 FieldMemOperand(result, JSArray::kPropertiesOffset)); |
236 // Field JSArray::kElementsOffset is initialized later. | 227 // Field JSArray::kElementsOffset is initialized later. |
237 __ sw(array_size, FieldMemOperand(result, JSArray::kLengthOffset)); | 228 __ sw(array_size, FieldMemOperand(result, JSArray::kLengthOffset)); |
238 | 229 |
239 // Calculate the location of the elements array and set elements array member | 230 // Calculate the location of the elements array and set elements array member |
240 // of the JSArray. | 231 // of the JSArray. |
241 // result: JSObject | 232 // result: JSObject |
(...skipping 13 matching lines...) Expand all Loading... |
255 // array_size: size of array (smi) | 246 // array_size: size of array (smi) |
256 __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex); | 247 __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex); |
257 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset); | 248 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset); |
258 __ sw(scratch1, MemOperand(elements_array_storage)); | 249 __ sw(scratch1, MemOperand(elements_array_storage)); |
259 __ Addu(elements_array_storage, elements_array_storage, kPointerSize); | 250 __ Addu(elements_array_storage, elements_array_storage, kPointerSize); |
260 | 251 |
261 // Length of the FixedArray is the number of pre-allocated elements if | 252 // Length of the FixedArray is the number of pre-allocated elements if |
262 // the actual JSArray has length 0 and the size of the JSArray for non-empty | 253 // the actual JSArray has length 0 and the size of the JSArray for non-empty |
263 // JSArrays. The length of a FixedArray is stored as a smi. | 254 // JSArrays. The length of a FixedArray is stored as a smi. |
264 STATIC_ASSERT(kSmiTag == 0); | 255 STATIC_ASSERT(kSmiTag == 0); |
265 __ li(at, Operand(Smi::FromInt(JSArray::kPreallocatedArrayElements))); | |
266 __ movz(array_size, at, array_size); | |
267 | 256 |
268 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset); | 257 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset); |
269 __ sw(array_size, MemOperand(elements_array_storage)); | 258 __ sw(array_size, MemOperand(elements_array_storage)); |
270 __ Addu(elements_array_storage, elements_array_storage, kPointerSize); | 259 __ Addu(elements_array_storage, elements_array_storage, kPointerSize); |
271 | 260 |
272 // Calculate elements array and elements array end. | 261 // Calculate elements array and elements array end. |
273 // result: JSObject | 262 // result: JSObject |
274 // elements_array_storage: elements array element storage | 263 // elements_array_storage: elements array element storage |
275 // array_size: smi-tagged size of elements array | 264 // array_size: smi-tagged size of elements array |
276 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2); | 265 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2); |
(...skipping 28 matching lines...) Expand all Loading... |
305 // sp[0]: last argument | 294 // sp[0]: last argument |
306 // This function is used for both construct and normal calls of Array. The only | 295 // This function is used for both construct and normal calls of Array. The only |
307 // difference between handling a construct call and a normal call is that for a | 296 // difference between handling a construct call and a normal call is that for a |
308 // construct call the constructor function in a1 needs to be preserved for | 297 // construct call the constructor function in a1 needs to be preserved for |
309 // entering the generic code. In both cases argc in a0 needs to be preserved. | 298 // entering the generic code. In both cases argc in a0 needs to be preserved. |
310 // Both registers are preserved by this code so no need to differentiate between | 299 // Both registers are preserved by this code so no need to differentiate between |
311 // construct call and normal call. | 300 // construct call and normal call. |
312 static void ArrayNativeCode(MacroAssembler* masm, | 301 static void ArrayNativeCode(MacroAssembler* masm, |
313 Label* call_generic_code) { | 302 Label* call_generic_code) { |
314 Counters* counters = masm->isolate()->counters(); | 303 Counters* counters = masm->isolate()->counters(); |
315 Label argc_one_or_more, argc_two_or_more; | 304 Label argc_one_or_more, argc_two_or_more, not_empty_array, empty_array; |
316 | 305 |
317 // Check for array construction with zero arguments or one. | 306 // Check for array construction with zero arguments or one. |
318 __ Branch(&argc_one_or_more, ne, a0, Operand(zero_reg)); | 307 __ Branch(&argc_one_or_more, ne, a0, Operand(zero_reg)); |
319 // Handle construction of an empty array. | 308 // Handle construction of an empty array. |
| 309 __ bind(&empty_array); |
320 AllocateEmptyJSArray(masm, | 310 AllocateEmptyJSArray(masm, |
321 a1, | 311 a1, |
322 a2, | 312 a2, |
323 a3, | 313 a3, |
324 t0, | 314 t0, |
325 t1, | 315 t1, |
326 JSArray::kPreallocatedArrayElements, | |
327 call_generic_code); | 316 call_generic_code); |
328 __ IncrementCounter(counters->array_function_native(), 1, a3, t0); | 317 __ IncrementCounter(counters->array_function_native(), 1, a3, t0); |
329 // Setup return value, remove receiver from stack and return. | 318 // Setup return value, remove receiver from stack and return. |
330 __ mov(v0, a2); | 319 __ mov(v0, a2); |
331 __ Addu(sp, sp, Operand(kPointerSize)); | 320 __ Addu(sp, sp, Operand(kPointerSize)); |
332 __ Ret(); | 321 __ Ret(); |
333 | 322 |
334 // Check for one argument. Bail out if argument is not smi or if it is | 323 // Check for one argument. Bail out if argument is not smi or if it is |
335 // negative. | 324 // negative. |
336 __ bind(&argc_one_or_more); | 325 __ bind(&argc_one_or_more); |
337 __ Branch(&argc_two_or_more, ne, a0, Operand(1)); | 326 __ Branch(&argc_two_or_more, ne, a0, Operand(1)); |
338 | 327 |
339 STATIC_ASSERT(kSmiTag == 0); | 328 STATIC_ASSERT(kSmiTag == 0); |
340 __ lw(a2, MemOperand(sp)); // Get the argument from the stack. | 329 __ lw(a2, MemOperand(sp)); // Get the argument from the stack. |
| 330 __ Branch(¬_empty_array, ne, a2, Operand(zero_reg)); |
| 331 __ Drop(1); // Adjust stack. |
| 332 __ mov(a0, zero_reg); // Treat this as a call with argc of zero. |
| 333 __ Branch(&empty_array); |
| 334 |
| 335 __ bind(¬_empty_array); |
341 __ And(a3, a2, Operand(kIntptrSignBit | kSmiTagMask)); | 336 __ And(a3, a2, Operand(kIntptrSignBit | kSmiTagMask)); |
342 __ Branch(call_generic_code, eq, a3, Operand(zero_reg)); | 337 __ Branch(call_generic_code, eq, a3, Operand(zero_reg)); |
343 | 338 |
344 // Handle construction of an empty array of a certain size. Bail out if size | 339 // Handle construction of an empty array of a certain size. Bail out if size |
345 // is too large to actually allocate an elements array. | 340 // is too large to actually allocate an elements array. |
346 STATIC_ASSERT(kSmiTag == 0); | 341 STATIC_ASSERT(kSmiTag == 0); |
347 __ Branch(call_generic_code, Ugreater_equal, a2, | 342 __ Branch(call_generic_code, Ugreater_equal, a2, |
348 Operand(JSObject::kInitialMaxFastElementArray << kSmiTagSize)); | 343 Operand(JSObject::kInitialMaxFastElementArray << kSmiTagSize)); |
349 | 344 |
350 // a0: argc | 345 // a0: argc |
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 __ mov(cp, zero_reg); | 1041 __ mov(cp, zero_reg); |
1047 | 1042 |
1048 // Enter an internal frame. | 1043 // Enter an internal frame. |
1049 { | 1044 { |
1050 FrameScope scope(masm, StackFrame::INTERNAL); | 1045 FrameScope scope(masm, StackFrame::INTERNAL); |
1051 | 1046 |
1052 // Set up the context from the function argument. | 1047 // Set up the context from the function argument. |
1053 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset)); | 1048 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset)); |
1054 | 1049 |
1055 // Set up the roots register. | 1050 // Set up the roots register. |
1056 ExternalReference roots_address = | 1051 ExternalReference roots_array_start = |
1057 ExternalReference::roots_address(masm->isolate()); | 1052 ExternalReference::roots_array_start(masm->isolate()); |
1058 __ li(s6, Operand(roots_address)); | 1053 __ li(s6, Operand(roots_array_start)); |
1059 | 1054 |
1060 // Push the function and the receiver onto the stack. | 1055 // Push the function and the receiver onto the stack. |
1061 __ Push(a1, a2); | 1056 __ Push(a1, a2); |
1062 | 1057 |
1063 // Copy arguments to the stack in a loop. | 1058 // Copy arguments to the stack in a loop. |
1064 // a3: argc | 1059 // a3: argc |
1065 // s0: argv, ie points to first arg | 1060 // s0: argv, ie points to first arg |
1066 Label loop, entry; | 1061 Label loop, entry; |
1067 __ sll(t0, a3, kPointerSizeLog2); | 1062 __ sll(t0, a3, kPointerSizeLog2); |
1068 __ addu(t2, s0, t0); | 1063 __ addu(t2, s0, t0); |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1687 __ bind(&dont_adapt_arguments); | 1682 __ bind(&dont_adapt_arguments); |
1688 __ Jump(a3); | 1683 __ Jump(a3); |
1689 } | 1684 } |
1690 | 1685 |
1691 | 1686 |
1692 #undef __ | 1687 #undef __ |
1693 | 1688 |
1694 } } // namespace v8::internal | 1689 } } // namespace v8::internal |
1695 | 1690 |
1696 #endif // V8_TARGET_ARCH_MIPS | 1691 #endif // V8_TARGET_ARCH_MIPS |
OLD | NEW |