| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include "v8.h" |
| 29 |
| 30 #if defined(V8_TARGET_ARCH_A64) |
| 31 |
| 32 #include "codegen.h" |
| 33 #include "macro-assembler.h" |
| 34 #include "simulator-a64.h" |
| 35 |
| 36 namespace v8 { |
| 37 namespace internal { |
| 38 |
| 39 #define __ ACCESS_MASM(masm) |
| 40 |
| 41 UnaryMathFunction CreateTranscendentalFunction(TranscendentalCache::Type type) { |
| 42 switch (type) { |
| 43 case TranscendentalCache::SIN: return &sin; |
| 44 case TranscendentalCache::COS: return &cos; |
| 45 case TranscendentalCache::TAN: return &tan; |
| 46 case TranscendentalCache::LOG: return &log; |
| 47 default: UNIMPLEMENTED(); |
| 48 } |
| 49 return NULL; |
| 50 } |
| 51 |
| 52 |
| 53 #if defined(USE_SIMULATOR) |
| 54 byte* fast_exp_a64_machine_code = NULL; |
| 55 double fast_exp_simulator(double x) { |
| 56 Simulator * simulator = Simulator::current(Isolate::Current()); |
| 57 return simulator->CallDouble(fast_exp_a64_machine_code, |
| 58 Simulator::CallArgument(x), |
| 59 Simulator::CallArgument::End()); |
| 60 } |
| 61 #endif |
| 62 |
| 63 |
| 64 UnaryMathFunction CreateExpFunction() { |
| 65 if (!FLAG_fast_math) return &exp; |
| 66 |
| 67 // Use the Math.exp implemetation in MathExpGenerator::EmitMathExp() to create |
| 68 // an AAPCS64-compliant exp() function. This will be faster than the C |
| 69 // library's exp() function, but probably less accurate. |
| 70 size_t actual_size; |
| 71 byte* buffer = static_cast<byte*>(OS::Allocate(1 * KB, &actual_size, true)); |
| 72 if (buffer == NULL) return &exp; |
| 73 |
| 74 ExternalReference::InitializeMathExpData(); |
| 75 MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); |
| 76 masm.SetStackPointer(csp); |
| 77 |
| 78 // The argument will be in d0 on entry. |
| 79 DoubleRegister input = d0; |
| 80 // Use other caller-saved registers for all other values. |
| 81 DoubleRegister result = d1; |
| 82 DoubleRegister double_temp1 = d2; |
| 83 DoubleRegister double_temp2 = d3; |
| 84 Register temp1 = x10; |
| 85 Register temp2 = x11; |
| 86 Register temp3 = x12; |
| 87 |
| 88 MathExpGenerator::EmitMathExp(&masm, input, result, |
| 89 double_temp1, double_temp2, |
| 90 temp1, temp2, temp3); |
| 91 // Move the result to the return register. |
| 92 masm.Fmov(d0, result); |
| 93 masm.Ret(); |
| 94 |
| 95 CodeDesc desc; |
| 96 masm.GetCode(&desc); |
| 97 ASSERT(!RelocInfo::RequiresRelocation(desc)); |
| 98 |
| 99 CPU::FlushICache(buffer, actual_size); |
| 100 OS::ProtectCode(buffer, actual_size); |
| 101 |
| 102 #if !defined(USE_SIMULATOR) |
| 103 return FUNCTION_CAST<UnaryMathFunction>(buffer); |
| 104 #else |
| 105 fast_exp_a64_machine_code = buffer; |
| 106 return &fast_exp_simulator; |
| 107 #endif |
| 108 } |
| 109 |
| 110 |
| 111 UnaryMathFunction CreateSqrtFunction() { |
| 112 return &sqrt; |
| 113 } |
| 114 |
| 115 // ------------------------------------------------------------------------- |
| 116 // Platform-specific RuntimeCallHelper functions. |
| 117 |
| 118 void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const { |
| 119 masm->EnterFrame(StackFrame::INTERNAL); |
| 120 ASSERT(!masm->has_frame()); |
| 121 masm->set_has_frame(true); |
| 122 } |
| 123 |
| 124 |
| 125 void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { |
| 126 masm->LeaveFrame(StackFrame::INTERNAL); |
| 127 ASSERT(masm->has_frame()); |
| 128 masm->set_has_frame(false); |
| 129 } |
| 130 |
| 131 |
| 132 // ------------------------------------------------------------------------- |
| 133 // Code generators |
| 134 |
| 135 void ElementsTransitionGenerator::GenerateMapChangeElementsTransition( |
| 136 MacroAssembler* masm, AllocationSiteMode mode, |
| 137 Label* allocation_site_info_found) { |
| 138 // ----------- S t a t e ------------- |
| 139 // -- x2 : receiver |
| 140 // -- x3 : target map |
| 141 // ----------------------------------- |
| 142 Register receiver = x2; |
| 143 Register map = x3; |
| 144 |
| 145 if (mode == TRACK_ALLOCATION_SITE) { |
| 146 ASSERT(allocation_site_info_found != NULL); |
| 147 __ TestJSArrayForAllocationSiteInfo(receiver, x10, x11); |
| 148 __ B(eq, allocation_site_info_found); |
| 149 } |
| 150 |
| 151 // Set transitioned map. |
| 152 __ Str(map, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 153 __ RecordWriteField(receiver, |
| 154 HeapObject::kMapOffset, |
| 155 map, |
| 156 x10, |
| 157 kLRHasNotBeenSaved, |
| 158 kDontSaveFPRegs, |
| 159 EMIT_REMEMBERED_SET, |
| 160 OMIT_SMI_CHECK); |
| 161 } |
| 162 |
| 163 |
| 164 void ElementsTransitionGenerator::GenerateSmiToDouble( |
| 165 MacroAssembler* masm, AllocationSiteMode mode, Label* fail) { |
| 166 ASM_LOCATION("ElementsTransitionGenerator::GenerateSmiToDouble"); |
| 167 // ----------- S t a t e ------------- |
| 168 // -- lr : return address |
| 169 // -- x0 : value |
| 170 // -- x1 : key |
| 171 // -- x2 : receiver |
| 172 // -- x3 : target map, scratch for subsequent call |
| 173 // ----------------------------------- |
| 174 Register receiver = x2; |
| 175 Register target_map = x3; |
| 176 |
| 177 Label gc_required, only_change_map; |
| 178 |
| 179 if (mode == TRACK_ALLOCATION_SITE) { |
| 180 __ TestJSArrayForAllocationSiteInfo(receiver, x10, x11); |
| 181 __ B(eq, fail); |
| 182 } |
| 183 |
| 184 // Check for empty arrays, which only require a map transition and no changes |
| 185 // to the backing store. |
| 186 Register elements = x4; |
| 187 __ Ldr(elements, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
| 188 __ JumpIfRoot(elements, Heap::kEmptyFixedArrayRootIndex, &only_change_map); |
| 189 |
| 190 __ Push(lr); |
| 191 Register length = x5; |
| 192 __ Ldrsw(length, UntagSmiFieldMemOperand(elements, |
| 193 FixedArray::kLengthOffset)); |
| 194 |
| 195 // Allocate new FixedDoubleArray. |
| 196 Register array_size = x6; |
| 197 Register array = x7; |
| 198 __ Lsl(array_size, length, kDoubleSizeLog2); |
| 199 __ Add(array_size, array_size, FixedDoubleArray::kHeaderSize); |
| 200 __ Allocate(array_size, array, x10, x11, &gc_required, DOUBLE_ALIGNMENT); |
| 201 // Register array is non-tagged heap object. |
| 202 |
| 203 // Set the destination FixedDoubleArray's length and map. |
| 204 Register map_root = x6; |
| 205 __ LoadRoot(map_root, Heap::kFixedDoubleArrayMapRootIndex); |
| 206 __ SmiTag(x11, length); |
| 207 __ Str(x11, MemOperand(array, FixedDoubleArray::kLengthOffset)); |
| 208 __ Str(map_root, MemOperand(array, HeapObject::kMapOffset)); |
| 209 |
| 210 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 211 __ RecordWriteField(receiver, HeapObject::kMapOffset, target_map, x6, |
| 212 kLRHasBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET, |
| 213 OMIT_SMI_CHECK, EXPECT_PREGENERATED); |
| 214 |
| 215 // Replace receiver's backing store with newly created FixedDoubleArray. |
| 216 __ Add(x10, array, kHeapObjectTag); |
| 217 __ Str(x10, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
| 218 __ RecordWriteField(receiver, JSObject::kElementsOffset, x10, |
| 219 x6, kLRHasBeenSaved, kDontSaveFPRegs, |
| 220 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK, EXPECT_PREGENERATED); |
| 221 |
| 222 // Prepare for conversion loop. |
| 223 Register src_elements = x10; |
| 224 Register dst_elements = x11; |
| 225 Register dst_end = x12; |
| 226 __ Add(src_elements, elements, FixedArray::kHeaderSize - kHeapObjectTag); |
| 227 __ Add(dst_elements, array, FixedDoubleArray::kHeaderSize); |
| 228 __ Add(dst_end, dst_elements, Operand(length, LSL, kDoubleSizeLog2)); |
| 229 |
| 230 FPRegister nan_d = d1; |
| 231 __ Fmov(nan_d, rawbits_to_double(kHoleNanInt64)); |
| 232 |
| 233 Label entry, done; |
| 234 __ B(&entry); |
| 235 |
| 236 __ Bind(&only_change_map); |
| 237 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 238 __ RecordWriteField(receiver, HeapObject::kMapOffset, target_map, x6, |
| 239 kLRHasNotBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET, |
| 240 OMIT_SMI_CHECK, EXPECT_PREGENERATED); |
| 241 __ B(&done); |
| 242 |
| 243 // Call into runtime if GC is required. |
| 244 __ Bind(&gc_required); |
| 245 __ Pop(lr); |
| 246 __ B(fail); |
| 247 |
| 248 // Iterate over the array, copying and coverting smis to doubles. If an |
| 249 // element is non-smi, write a hole to the destination. |
| 250 { |
| 251 Label loop; |
| 252 __ Bind(&loop); |
| 253 __ Ldr(x13, MemOperand(src_elements, kPointerSize, PostIndex)); |
| 254 __ SmiUntagToDouble(d0, x13, kSpeculativeUntag); |
| 255 __ Tst(x13, kSmiTagMask); |
| 256 __ Fcsel(d0, d0, nan_d, eq); |
| 257 __ Str(d0, MemOperand(dst_elements, kDoubleSize, PostIndex)); |
| 258 |
| 259 __ Bind(&entry); |
| 260 __ Cmp(dst_elements, dst_end); |
| 261 __ B(lt, &loop); |
| 262 } |
| 263 |
| 264 __ Pop(lr); |
| 265 __ Bind(&done); |
| 266 } |
| 267 |
| 268 |
| 269 void ElementsTransitionGenerator::GenerateDoubleToObject( |
| 270 MacroAssembler* masm, AllocationSiteMode mode, Label* fail) { |
| 271 ASM_LOCATION("ElementsTransitionGenerator::GenerateDoubleToObject"); |
| 272 // ----------- S t a t e ------------- |
| 273 // -- x0 : value |
| 274 // -- x1 : key |
| 275 // -- x2 : receiver |
| 276 // -- lr : return address |
| 277 // -- x3 : target map, scratch for subsequent call |
| 278 // -- x4 : scratch (elements) |
| 279 // ----------------------------------- |
| 280 Register value = x0; |
| 281 Register key = x1; |
| 282 Register receiver = x2; |
| 283 Register target_map = x3; |
| 284 |
| 285 if (mode == TRACK_ALLOCATION_SITE) { |
| 286 __ TestJSArrayForAllocationSiteInfo(receiver, x10, x11); |
| 287 __ B(eq, fail); |
| 288 } |
| 289 |
| 290 // Check for empty arrays, which only require a map transition and no changes |
| 291 // to the backing store. |
| 292 Label only_change_map; |
| 293 Register elements = x4; |
| 294 __ Ldr(elements, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
| 295 __ JumpIfRoot(elements, Heap::kEmptyFixedArrayRootIndex, &only_change_map); |
| 296 |
| 297 __ Push(lr); |
| 298 // TODO(all): These registers may not need to be pushed. Examine |
| 299 // RecordWriteStub and check whether it's needed. |
| 300 __ Push(target_map, receiver, key, value); |
| 301 Register length = x5; |
| 302 __ Ldrsw(length, UntagSmiFieldMemOperand(elements, |
| 303 FixedArray::kLengthOffset)); |
| 304 |
| 305 // Allocate new FixedArray. |
| 306 Register array_size = x6; |
| 307 Register array = x7; |
| 308 Label gc_required; |
| 309 __ Mov(array_size, FixedDoubleArray::kHeaderSize); |
| 310 __ Add(array_size, array_size, Operand(length, LSL, kPointerSizeLog2)); |
| 311 __ Allocate(array_size, array, x10, x11, &gc_required, NO_ALLOCATION_FLAGS); |
| 312 |
| 313 // Set destination FixedDoubleArray's length and map. |
| 314 Register map_root = x6; |
| 315 __ LoadRoot(map_root, Heap::kFixedArrayMapRootIndex); |
| 316 __ SmiTag(x11, length); |
| 317 __ Str(x11, MemOperand(array, FixedDoubleArray::kLengthOffset)); |
| 318 __ Str(map_root, MemOperand(array, HeapObject::kMapOffset)); |
| 319 |
| 320 // Prepare for conversion loop. |
| 321 Register src_elements = x10; |
| 322 Register dst_elements = x11; |
| 323 Register dst_end = x12; |
| 324 __ Add(src_elements, elements, |
| 325 FixedDoubleArray::kHeaderSize - kHeapObjectTag); |
| 326 __ Add(dst_elements, array, FixedArray::kHeaderSize); |
| 327 __ Add(array, array, kHeapObjectTag); |
| 328 __ Add(dst_end, dst_elements, Operand(length, LSL, kPointerSizeLog2)); |
| 329 |
| 330 Register the_hole = x14; |
| 331 Register heap_num_map = x15; |
| 332 __ LoadRoot(the_hole, Heap::kTheHoleValueRootIndex); |
| 333 __ LoadRoot(heap_num_map, Heap::kHeapNumberMapRootIndex); |
| 334 |
| 335 Label entry; |
| 336 __ B(&entry); |
| 337 |
| 338 // Call into runtime if GC is required. |
| 339 __ Bind(&gc_required); |
| 340 __ Pop(value, key, receiver, target_map); |
| 341 __ Pop(lr); |
| 342 __ B(fail); |
| 343 |
| 344 { |
| 345 Label loop, convert_hole; |
| 346 __ Bind(&loop); |
| 347 __ Ldr(x13, MemOperand(src_elements, kPointerSize, PostIndex)); |
| 348 __ Cmp(x13, kHoleNanInt64); |
| 349 __ B(eq, &convert_hole); |
| 350 |
| 351 // Non-hole double, copy value into a heap number. |
| 352 Register heap_num = x5; |
| 353 __ AllocateHeapNumber(heap_num, &gc_required, x6, x4, heap_num_map); |
| 354 __ Str(x13, FieldMemOperand(heap_num, HeapNumber::kValueOffset)); |
| 355 __ Mov(x13, dst_elements); |
| 356 __ Str(heap_num, MemOperand(dst_elements, kPointerSize, PostIndex)); |
| 357 __ RecordWrite(array, x13, heap_num, kLRHasBeenSaved, kDontSaveFPRegs, |
| 358 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK, EXPECT_PREGENERATED); |
| 359 |
| 360 __ B(&entry); |
| 361 |
| 362 // Replace the-hole NaN with the-hole pointer. |
| 363 __ Bind(&convert_hole); |
| 364 __ Str(the_hole, MemOperand(dst_elements, kPointerSize, PostIndex)); |
| 365 |
| 366 __ Bind(&entry); |
| 367 __ Cmp(dst_elements, dst_end); |
| 368 __ B(lt, &loop); |
| 369 } |
| 370 |
| 371 __ Pop(value, key, receiver, target_map); |
| 372 // Replace receiver's backing store with newly created and filled FixedArray. |
| 373 __ Str(array, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
| 374 __ RecordWriteField(receiver, JSObject::kElementsOffset, array, x13, |
| 375 kLRHasBeenSaved, kDontSaveFPRegs, EMIT_REMEMBERED_SET, |
| 376 OMIT_SMI_CHECK, EXPECT_PREGENERATED); |
| 377 __ Pop(lr); |
| 378 |
| 379 __ Bind(&only_change_map); |
| 380 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 381 __ RecordWriteField(receiver, HeapObject::kMapOffset, target_map, x13, |
| 382 kLRHasNotBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET, |
| 383 OMIT_SMI_CHECK, EXPECT_PREGENERATED); |
| 384 } |
| 385 |
| 386 |
| 387 bool Code::IsYoungSequence(byte* sequence) { |
| 388 return MacroAssembler::IsYoungSequence(sequence); |
| 389 } |
| 390 |
| 391 |
| 392 void Code::GetCodeAgeAndParity(byte* sequence, Age* age, |
| 393 MarkingParity* parity) { |
| 394 if (IsYoungSequence(sequence)) { |
| 395 *age = kNoAge; |
| 396 *parity = NO_MARKING_PARITY; |
| 397 } else { |
| 398 byte* target = sequence + kCodeAgeStubEntryOffset; |
| 399 Code* stub = GetCodeFromTargetAddress(Memory::Address_at(target)); |
| 400 GetCodeAgeAndParity(stub, age, parity); |
| 401 } |
| 402 } |
| 403 |
| 404 |
| 405 void Code::PatchPlatformCodeAge(byte* sequence, |
| 406 Code::Age age, |
| 407 MarkingParity parity) { |
| 408 PatchingAssembler patcher(sequence, kCodeAgeSequenceSize / kInstructionSize); |
| 409 if (age == kNoAge) { |
| 410 MacroAssembler::EmitFrameSetupForCodeAgePatching(&patcher); |
| 411 } else { |
| 412 Code * stub = GetCodeAgeStub(age, parity); |
| 413 MacroAssembler::EmitCodeAgeSequence(&patcher, stub); |
| 414 } |
| 415 } |
| 416 |
| 417 |
| 418 void StringCharLoadGenerator::Generate(MacroAssembler* masm, |
| 419 Register string, |
| 420 Register index, |
| 421 Register result, |
| 422 Label* call_runtime) { |
| 423 // Fetch the instance type of the receiver into result register. |
| 424 __ Ldr(result, FieldMemOperand(string, HeapObject::kMapOffset)); |
| 425 __ Ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset)); |
| 426 |
| 427 // We need special handling for indirect strings. |
| 428 Label check_sequential; |
| 429 __ TestAndBranchIfAllClear(result, kIsIndirectStringMask, &check_sequential); |
| 430 |
| 431 // Dispatch on the indirect string shape: slice or cons. |
| 432 Label cons_string; |
| 433 __ TestAndBranchIfAllClear(result, kSlicedNotConsMask, &cons_string); |
| 434 |
| 435 // Handle slices. |
| 436 Label indirect_string_loaded; |
| 437 __ Ldrsw(result, |
| 438 UntagSmiFieldMemOperand(string, SlicedString::kOffsetOffset)); |
| 439 __ Ldr(string, FieldMemOperand(string, SlicedString::kParentOffset)); |
| 440 __ Add(index, index, result); |
| 441 __ B(&indirect_string_loaded); |
| 442 |
| 443 // Handle cons strings. |
| 444 // Check whether the right hand side is the empty string (i.e. if |
| 445 // this is really a flat string in a cons string). If that is not |
| 446 // the case we would rather go to the runtime system now to flatten |
| 447 // the string. |
| 448 __ Bind(&cons_string); |
| 449 __ Ldr(result, FieldMemOperand(string, ConsString::kSecondOffset)); |
| 450 __ JumpIfNotRoot(result, Heap::kempty_stringRootIndex, call_runtime); |
| 451 // Get the first of the two strings and load its instance type. |
| 452 __ Ldr(string, FieldMemOperand(string, ConsString::kFirstOffset)); |
| 453 |
| 454 __ Bind(&indirect_string_loaded); |
| 455 __ Ldr(result, FieldMemOperand(string, HeapObject::kMapOffset)); |
| 456 __ Ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset)); |
| 457 |
| 458 // Distinguish sequential and external strings. Only these two string |
| 459 // representations can reach here (slices and flat cons strings have been |
| 460 // reduced to the underlying sequential or external string). |
| 461 Label external_string, check_encoding; |
| 462 __ Bind(&check_sequential); |
| 463 STATIC_ASSERT(kSeqStringTag == 0); |
| 464 __ TestAndBranchIfAnySet(result, kStringRepresentationMask, &external_string); |
| 465 |
| 466 // Prepare sequential strings |
| 467 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize); |
| 468 __ Add(string, string, SeqTwoByteString::kHeaderSize - kHeapObjectTag); |
| 469 __ B(&check_encoding); |
| 470 |
| 471 // Handle external strings. |
| 472 __ Bind(&external_string); |
| 473 if (FLAG_debug_code) { |
| 474 // Assert that we do not have a cons or slice (indirect strings) here. |
| 475 // Sequential strings have already been ruled out. |
| 476 __ Tst(result, kIsIndirectStringMask); |
| 477 __ Assert(eq, "external string expected, but not found"); |
| 478 } |
| 479 // Rule out short external strings. |
| 480 STATIC_CHECK(kShortExternalStringTag != 0); |
| 481 __ TestAndBranchIfAnySet(result, kShortExternalStringMask, call_runtime); |
| 482 __ Ldr(string, FieldMemOperand(string, ExternalString::kResourceDataOffset)); |
| 483 |
| 484 Label ascii, done; |
| 485 __ Bind(&check_encoding); |
| 486 STATIC_ASSERT(kTwoByteStringTag == 0); |
| 487 __ TestAndBranchIfAnySet(result, kStringEncodingMask, &ascii); |
| 488 // Two-byte string. |
| 489 __ Ldrh(result, MemOperand(string, index, LSL, 1)); |
| 490 __ B(&done); |
| 491 __ Bind(&ascii); |
| 492 // Ascii string. |
| 493 __ Ldrb(result, MemOperand(string, index)); |
| 494 __ Bind(&done); |
| 495 } |
| 496 |
| 497 |
| 498 static MemOperand ExpConstant(Register base, int index) { |
| 499 return MemOperand(base, index * kDoubleSize); |
| 500 } |
| 501 |
| 502 |
| 503 void MathExpGenerator::EmitMathExp(MacroAssembler* masm, |
| 504 DoubleRegister input, |
| 505 DoubleRegister result, |
| 506 DoubleRegister double_temp1, |
| 507 DoubleRegister double_temp2, |
| 508 Register temp1, |
| 509 Register temp2, |
| 510 Register temp3) { |
| 511 // TODO(jbramley): There are several instances where fnmsub could be used |
| 512 // instead of fmul and fsub. Doing this changes the result, but since this is |
| 513 // an estimation anyway, does it matter? |
| 514 |
| 515 ASSERT(!AreAliased(input, result, |
| 516 double_temp1, double_temp2, |
| 517 temp1, temp2, temp3)); |
| 518 ASSERT(ExternalReference::math_exp_constants(0).address() != NULL); |
| 519 |
| 520 Label done; |
| 521 DoubleRegister double_temp3 = result; |
| 522 Register constants = temp3; |
| 523 |
| 524 // The algorithm used relies on some magic constants which are initialized in |
| 525 // ExternalReference::InitializeMathExpData(). |
| 526 |
| 527 // Load the address of the start of the array. |
| 528 __ Mov(constants, Operand(ExternalReference::math_exp_constants(0))); |
| 529 |
| 530 // We have to do a four-way split here: |
| 531 // - If input <= about -708.4, the output always rounds to zero. |
| 532 // - If input >= about 709.8, the output always rounds to +infinity. |
| 533 // - If the input is NaN, the output is NaN. |
| 534 // - Otherwise, the result needs to be calculated. |
| 535 Label result_is_finite_non_zero; |
| 536 // Assert that we can load offset 0 (the small input threshold) and offset 1 |
| 537 // (the large input threshold) with a single ldp. |
| 538 ASSERT(kDRegSizeInBytes == (ExpConstant(constants, 1).offset() - |
| 539 ExpConstant(constants, 0).offset())); |
| 540 __ Ldp(double_temp1, double_temp2, ExpConstant(constants, 0)); |
| 541 |
| 542 __ Fcmp(input, double_temp1); |
| 543 __ Fccmp(input, double_temp2, NoFlag, hi); |
| 544 // At this point, the condition flags can be in one of five states: |
| 545 // NZCV |
| 546 // 1000 -708.4 < input < 709.8 result = exp(input) |
| 547 // 0110 input == 709.8 result = +infinity |
| 548 // 0010 input > 709.8 result = +infinity |
| 549 // 0011 input is NaN result = input |
| 550 // 0000 input <= -708.4 result = +0.0 |
| 551 |
| 552 // Continue the common case first. 'mi' tests N == 1. |
| 553 __ B(&result_is_finite_non_zero, mi); |
| 554 |
| 555 // TODO(jbramley): Add (and use) a zero D register for A64. |
| 556 // TODO(jbramley): Consider adding a +infinity register for A64. |
| 557 __ Ldr(double_temp2, ExpConstant(constants, 2)); // Synthesize +infinity. |
| 558 __ Fsub(double_temp1, double_temp1, double_temp1); // Synthesize +0.0. |
| 559 |
| 560 // Select between +0.0 and +infinity. 'lo' tests C == 0. |
| 561 __ Fcsel(result, double_temp1, double_temp2, lo); |
| 562 // Select between {+0.0 or +infinity} and input. 'vc' tests V == 0. |
| 563 __ Fcsel(result, result, input, vc); |
| 564 __ B(&done); |
| 565 |
| 566 // The rest is magic, as described in InitializeMathExpData(). |
| 567 __ Bind(&result_is_finite_non_zero); |
| 568 |
| 569 // Assert that we can load offset 3 and offset 4 with a single ldp. |
| 570 ASSERT(kDRegSizeInBytes == (ExpConstant(constants, 4).offset() - |
| 571 ExpConstant(constants, 3).offset())); |
| 572 __ Ldp(double_temp1, double_temp3, ExpConstant(constants, 3)); |
| 573 __ Fmadd(double_temp1, double_temp1, input, double_temp3); |
| 574 __ Fmov(temp2.W(), double_temp1.S()); |
| 575 __ Fsub(double_temp1, double_temp1, double_temp3); |
| 576 |
| 577 // Assert that we can load offset 5 and offset 6 with a single ldp. |
| 578 ASSERT(kDRegSizeInBytes == (ExpConstant(constants, 6).offset() - |
| 579 ExpConstant(constants, 5).offset())); |
| 580 __ Ldp(double_temp2, double_temp3, ExpConstant(constants, 5)); |
| 581 // TODO(jbramley): Consider using Fnmsub here. |
| 582 __ Fmul(double_temp1, double_temp1, double_temp2); |
| 583 __ Fsub(double_temp1, double_temp1, input); |
| 584 |
| 585 __ Fmul(double_temp2, double_temp1, double_temp1); |
| 586 __ Fsub(double_temp3, double_temp3, double_temp1); |
| 587 __ Fmul(double_temp3, double_temp3, double_temp2); |
| 588 |
| 589 __ Mov(temp1.W(), Operand(temp2.W(), LSR, 11)); |
| 590 |
| 591 __ Ldr(double_temp2, ExpConstant(constants, 7)); |
| 592 // TODO(jbramley): Consider using Fnmsub here. |
| 593 __ Fmul(double_temp3, double_temp3, double_temp2); |
| 594 __ Fsub(double_temp3, double_temp3, double_temp1); |
| 595 |
| 596 // The 8th constant is 1.0, so use an immediate move rather than a load. |
| 597 // We can't generate a runtime assertion here as we would need to call Abort |
| 598 // in the runtime and we don't have an Isolate when we generate this code. |
| 599 __ Fmov(double_temp2, 1.0); |
| 600 __ Fadd(double_temp3, double_temp3, double_temp2); |
| 601 |
| 602 __ And(temp2, temp2, 0x7ff); |
| 603 __ Add(temp1, temp1, 0x3ff); |
| 604 |
| 605 // Do the final table lookup. |
| 606 __ Mov(temp3, Operand(ExternalReference::math_exp_log_table())); |
| 607 |
| 608 __ Add(temp3, temp3, Operand(temp2, LSL, kDRegSizeInBytesLog2)); |
| 609 __ Ldp(temp2.W(), temp3.W(), MemOperand(temp3)); |
| 610 __ Orr(temp1.W(), temp3.W(), Operand(temp1.W(), LSL, 20)); |
| 611 __ Bfi(temp2, temp1, 32, 32); |
| 612 __ Fmov(double_temp1, temp2); |
| 613 |
| 614 __ Fmul(result, double_temp3, double_temp1); |
| 615 |
| 616 __ Bind(&done); |
| 617 } |
| 618 |
| 619 #undef __ |
| 620 |
| 621 } } // namespace v8::internal |
| 622 |
| 623 #endif // V8_TARGET_ARCH_A64 |
| OLD | NEW |