blob: 990dd4101fe04ba51a07af6af6732b367c42157b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/arm64/codegen-arm64.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006
7#if V8_TARGET_ARCH_ARM64
8
9#include "src/arm64/simulator-arm64.h"
10#include "src/codegen.h"
11#include "src/macro-assembler.h"
12
13namespace v8 {
14namespace internal {
15
16#define __ ACCESS_MASM(masm)
17
18#if defined(USE_SIMULATOR)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019byte* fast_exp_arm64_machine_code = nullptr;
20double fast_exp_simulator(double x, Isolate* isolate) {
21 Simulator * simulator = Simulator::current(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 Simulator::CallArgument args[] = {
23 Simulator::CallArgument(x),
24 Simulator::CallArgument::End()
25 };
26 return simulator->CallDouble(fast_exp_arm64_machine_code, args);
27}
28#endif
29
30
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 // Use the Math.exp implemetation in MathExpGenerator::EmitMathExp() to create
33 // an AAPCS64-compliant exp() function. This will be faster than the C
34 // library's exp() function, but probably less accurate.
35 size_t actual_size;
36 byte* buffer =
37 static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 if (buffer == nullptr) return nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039
40 ExternalReference::InitializeMathExpData();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size),
42 CodeObjectRequired::kNo);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 masm.SetStackPointer(csp);
44
45 // The argument will be in d0 on entry.
46 DoubleRegister input = d0;
47 // Use other caller-saved registers for all other values.
48 DoubleRegister result = d1;
49 DoubleRegister double_temp1 = d2;
50 DoubleRegister double_temp2 = d3;
51 Register temp1 = x10;
52 Register temp2 = x11;
53 Register temp3 = x12;
54
55 MathExpGenerator::EmitMathExp(&masm, input, result,
56 double_temp1, double_temp2,
57 temp1, temp2, temp3);
58 // Move the result to the return register.
59 masm.Fmov(d0, result);
60 masm.Ret();
61
62 CodeDesc desc;
63 masm.GetCode(&desc);
64 DCHECK(!RelocInfo::RequiresRelocation(desc));
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 Assembler::FlushICache(isolate, buffer, actual_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 base::OS::ProtectCode(buffer, actual_size);
68
69#if !defined(USE_SIMULATOR)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071#else
72 fast_exp_arm64_machine_code = buffer;
73 return &fast_exp_simulator;
74#endif
75}
76
77
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078UnaryMathFunctionWithIsolate CreateSqrtFunction(Isolate* isolate) {
79 return nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080}
81
82
83// -------------------------------------------------------------------------
84// Platform-specific RuntimeCallHelper functions.
85
86void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
87 masm->EnterFrame(StackFrame::INTERNAL);
88 DCHECK(!masm->has_frame());
89 masm->set_has_frame(true);
90}
91
92
93void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
94 masm->LeaveFrame(StackFrame::INTERNAL);
95 DCHECK(masm->has_frame());
96 masm->set_has_frame(false);
97}
98
99
100// -------------------------------------------------------------------------
101// Code generators
102
103void ElementsTransitionGenerator::GenerateMapChangeElementsTransition(
104 MacroAssembler* masm,
105 Register receiver,
106 Register key,
107 Register value,
108 Register target_map,
109 AllocationSiteMode mode,
110 Label* allocation_memento_found) {
111 ASM_LOCATION(
112 "ElementsTransitionGenerator::GenerateMapChangeElementsTransition");
113 DCHECK(!AreAliased(receiver, key, value, target_map));
114
115 if (mode == TRACK_ALLOCATION_SITE) {
116 DCHECK(allocation_memento_found != NULL);
117 __ JumpIfJSArrayHasAllocationMemento(receiver, x10, x11,
118 allocation_memento_found);
119 }
120
121 // Set transitioned map.
122 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
123 __ RecordWriteField(receiver,
124 HeapObject::kMapOffset,
125 target_map,
126 x10,
127 kLRHasNotBeenSaved,
128 kDontSaveFPRegs,
129 EMIT_REMEMBERED_SET,
130 OMIT_SMI_CHECK);
131}
132
133
134void ElementsTransitionGenerator::GenerateSmiToDouble(
135 MacroAssembler* masm,
136 Register receiver,
137 Register key,
138 Register value,
139 Register target_map,
140 AllocationSiteMode mode,
141 Label* fail) {
142 ASM_LOCATION("ElementsTransitionGenerator::GenerateSmiToDouble");
143 Label gc_required, only_change_map;
144 Register elements = x4;
145 Register length = x5;
146 Register array_size = x6;
147 Register array = x7;
148
149 Register scratch = x6;
150
151 // Verify input registers don't conflict with locals.
152 DCHECK(!AreAliased(receiver, key, value, target_map,
153 elements, length, array_size, array));
154
155 if (mode == TRACK_ALLOCATION_SITE) {
156 __ JumpIfJSArrayHasAllocationMemento(receiver, x10, x11, fail);
157 }
158
159 // Check for empty arrays, which only require a map transition and no changes
160 // to the backing store.
161 __ Ldr(elements, FieldMemOperand(receiver, JSObject::kElementsOffset));
162 __ JumpIfRoot(elements, Heap::kEmptyFixedArrayRootIndex, &only_change_map);
163
164 __ Push(lr);
165 __ Ldrsw(length, UntagSmiFieldMemOperand(elements,
166 FixedArray::kLengthOffset));
167
168 // Allocate new FixedDoubleArray.
169 __ Lsl(array_size, length, kDoubleSizeLog2);
170 __ Add(array_size, array_size, FixedDoubleArray::kHeaderSize);
171 __ Allocate(array_size, array, x10, x11, &gc_required, DOUBLE_ALIGNMENT);
172 // Register array is non-tagged heap object.
173
174 // Set the destination FixedDoubleArray's length and map.
175 Register map_root = array_size;
176 __ LoadRoot(map_root, Heap::kFixedDoubleArrayMapRootIndex);
177 __ SmiTag(x11, length);
Ben Murdochc5610432016-08-08 18:44:38 +0100178 __ Str(x11, FieldMemOperand(array, FixedDoubleArray::kLengthOffset));
179 __ Str(map_root, FieldMemOperand(array, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180
181 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
182 __ RecordWriteField(receiver, HeapObject::kMapOffset, target_map, scratch,
183 kLRHasBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET,
184 OMIT_SMI_CHECK);
185
186 // Replace receiver's backing store with newly created FixedDoubleArray.
Ben Murdochc5610432016-08-08 18:44:38 +0100187 __ Move(x10, array);
188 __ Str(array, FieldMemOperand(receiver, JSObject::kElementsOffset));
189 __ RecordWriteField(receiver, JSObject::kElementsOffset, x10, scratch,
190 kLRHasBeenSaved, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
191 OMIT_SMI_CHECK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192
193 // Prepare for conversion loop.
194 Register src_elements = x10;
195 Register dst_elements = x11;
196 Register dst_end = x12;
197 __ Add(src_elements, elements, FixedArray::kHeaderSize - kHeapObjectTag);
Ben Murdochc5610432016-08-08 18:44:38 +0100198 __ Add(dst_elements, array, FixedDoubleArray::kHeaderSize - kHeapObjectTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 __ Add(dst_end, dst_elements, Operand(length, LSL, kDoubleSizeLog2));
200
201 FPRegister nan_d = d1;
202 __ Fmov(nan_d, rawbits_to_double(kHoleNanInt64));
203
204 Label entry, done;
205 __ B(&entry);
206
207 __ Bind(&only_change_map);
208 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
209 __ RecordWriteField(receiver, HeapObject::kMapOffset, target_map, scratch,
210 kLRHasNotBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET,
211 OMIT_SMI_CHECK);
212 __ B(&done);
213
214 // Call into runtime if GC is required.
215 __ Bind(&gc_required);
216 __ Pop(lr);
217 __ B(fail);
218
219 // Iterate over the array, copying and coverting smis to doubles. If an
220 // element is non-smi, write a hole to the destination.
221 {
222 Label loop;
223 __ Bind(&loop);
224 __ Ldr(x13, MemOperand(src_elements, kPointerSize, PostIndex));
225 __ SmiUntagToDouble(d0, x13, kSpeculativeUntag);
226 __ Tst(x13, kSmiTagMask);
227 __ Fcsel(d0, d0, nan_d, eq);
228 __ Str(d0, MemOperand(dst_elements, kDoubleSize, PostIndex));
229
230 __ Bind(&entry);
231 __ Cmp(dst_elements, dst_end);
232 __ B(lt, &loop);
233 }
234
235 __ Pop(lr);
236 __ Bind(&done);
237}
238
239
240void ElementsTransitionGenerator::GenerateDoubleToObject(
241 MacroAssembler* masm,
242 Register receiver,
243 Register key,
244 Register value,
245 Register target_map,
246 AllocationSiteMode mode,
247 Label* fail) {
248 ASM_LOCATION("ElementsTransitionGenerator::GenerateDoubleToObject");
249 Register elements = x4;
250 Register array_size = x6;
251 Register array = x7;
252 Register length = x5;
253
254 // Verify input registers don't conflict with locals.
255 DCHECK(!AreAliased(receiver, key, value, target_map,
256 elements, array_size, array, length));
257
258 if (mode == TRACK_ALLOCATION_SITE) {
259 __ JumpIfJSArrayHasAllocationMemento(receiver, x10, x11, fail);
260 }
261
262 // Check for empty arrays, which only require a map transition and no changes
263 // to the backing store.
264 Label only_change_map;
265
266 __ Ldr(elements, FieldMemOperand(receiver, JSObject::kElementsOffset));
267 __ JumpIfRoot(elements, Heap::kEmptyFixedArrayRootIndex, &only_change_map);
268
269 __ Push(lr);
270 // TODO(all): These registers may not need to be pushed. Examine
271 // RecordWriteStub and check whether it's needed.
272 __ Push(target_map, receiver, key, value);
273 __ Ldrsw(length, UntagSmiFieldMemOperand(elements,
274 FixedArray::kLengthOffset));
275 // Allocate new FixedArray.
276 Label gc_required;
277 __ Mov(array_size, FixedDoubleArray::kHeaderSize);
278 __ Add(array_size, array_size, Operand(length, LSL, kPointerSizeLog2));
279 __ Allocate(array_size, array, x10, x11, &gc_required, NO_ALLOCATION_FLAGS);
280
281 // Set destination FixedDoubleArray's length and map.
282 Register map_root = array_size;
283 __ LoadRoot(map_root, Heap::kFixedArrayMapRootIndex);
284 __ SmiTag(x11, length);
Ben Murdochc5610432016-08-08 18:44:38 +0100285 __ Str(x11, FieldMemOperand(array, FixedDoubleArray::kLengthOffset));
286 __ Str(map_root, FieldMemOperand(array, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287
288 // Prepare for conversion loop.
289 Register src_elements = x10;
290 Register dst_elements = x11;
291 Register dst_end = x12;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400292 Register the_hole = x14;
293 __ LoadRoot(the_hole, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 __ Add(src_elements, elements,
295 FixedDoubleArray::kHeaderSize - kHeapObjectTag);
Ben Murdochc5610432016-08-08 18:44:38 +0100296 __ Add(dst_elements, array, FixedArray::kHeaderSize - kHeapObjectTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297 __ Add(dst_end, dst_elements, Operand(length, LSL, kPointerSizeLog2));
298
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400299 // Allocating heap numbers in the loop below can fail and cause a jump to
300 // gc_required. We can't leave a partly initialized FixedArray behind,
301 // so pessimistically fill it with holes now.
302 Label initialization_loop, initialization_loop_entry;
303 __ B(&initialization_loop_entry);
304 __ bind(&initialization_loop);
305 __ Str(the_hole, MemOperand(dst_elements, kPointerSize, PostIndex));
306 __ bind(&initialization_loop_entry);
307 __ Cmp(dst_elements, dst_end);
308 __ B(lt, &initialization_loop);
309
Ben Murdochc5610432016-08-08 18:44:38 +0100310 __ Add(dst_elements, array, FixedArray::kHeaderSize - kHeapObjectTag);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400311
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 Register heap_num_map = x15;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 __ LoadRoot(heap_num_map, Heap::kHeapNumberMapRootIndex);
314
315 Label entry;
316 __ B(&entry);
317
318 // Call into runtime if GC is required.
319 __ Bind(&gc_required);
320 __ Pop(value, key, receiver, target_map);
321 __ Pop(lr);
322 __ B(fail);
323
324 {
325 Label loop, convert_hole;
326 __ Bind(&loop);
327 __ Ldr(x13, MemOperand(src_elements, kPointerSize, PostIndex));
328 __ Cmp(x13, kHoleNanInt64);
329 __ B(eq, &convert_hole);
330
331 // Non-hole double, copy value into a heap number.
332 Register heap_num = length;
333 Register scratch = array_size;
334 Register scratch2 = elements;
335 __ AllocateHeapNumber(heap_num, &gc_required, scratch, scratch2,
336 x13, heap_num_map);
337 __ Mov(x13, dst_elements);
338 __ Str(heap_num, MemOperand(dst_elements, kPointerSize, PostIndex));
339 __ RecordWrite(array, x13, heap_num, kLRHasBeenSaved, kDontSaveFPRegs,
340 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
341
342 __ B(&entry);
343
344 // Replace the-hole NaN with the-hole pointer.
345 __ Bind(&convert_hole);
346 __ Str(the_hole, MemOperand(dst_elements, kPointerSize, PostIndex));
347
348 __ Bind(&entry);
349 __ Cmp(dst_elements, dst_end);
350 __ B(lt, &loop);
351 }
352
353 __ Pop(value, key, receiver, target_map);
354 // Replace receiver's backing store with newly created and filled FixedArray.
355 __ Str(array, FieldMemOperand(receiver, JSObject::kElementsOffset));
356 __ RecordWriteField(receiver, JSObject::kElementsOffset, array, x13,
357 kLRHasBeenSaved, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
358 OMIT_SMI_CHECK);
359 __ Pop(lr);
360
361 __ Bind(&only_change_map);
362 __ Str(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
363 __ RecordWriteField(receiver, HeapObject::kMapOffset, target_map, x13,
364 kLRHasNotBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET,
365 OMIT_SMI_CHECK);
366}
367
368
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369CodeAgingHelper::CodeAgingHelper(Isolate* isolate) {
370 USE(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 DCHECK(young_sequence_.length() == kNoCodeAgeSequenceLength);
372 // The sequence of instructions that is patched out for aging code is the
373 // following boilerplate stack-building prologue that is found both in
374 // FUNCTION and OPTIMIZED_FUNCTION code:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000375 PatchingAssembler patcher(isolate, young_sequence_.start(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 young_sequence_.length() / kInstructionSize);
377 // The young sequence is the frame setup code for FUNCTION code types. It is
378 // generated by FullCodeGenerator::Generate.
379 MacroAssembler::EmitFrameSetupForCodeAgePatching(&patcher);
380
381#ifdef DEBUG
382 const int length = kCodeAgeStubEntryOffset / kInstructionSize;
383 DCHECK(old_sequence_.length() >= kCodeAgeStubEntryOffset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000384 PatchingAssembler patcher_old(isolate, old_sequence_.start(), length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000385 MacroAssembler::EmitCodeAgeSequence(&patcher_old, NULL);
386#endif
387}
388
389
390#ifdef DEBUG
391bool CodeAgingHelper::IsOld(byte* candidate) const {
392 return memcmp(candidate, old_sequence_.start(), kCodeAgeStubEntryOffset) == 0;
393}
394#endif
395
396
397bool Code::IsYoungSequence(Isolate* isolate, byte* sequence) {
398 return MacroAssembler::IsYoungSequence(isolate, sequence);
399}
400
401
402void Code::GetCodeAgeAndParity(Isolate* isolate, byte* sequence, Age* age,
403 MarkingParity* parity) {
404 if (IsYoungSequence(isolate, sequence)) {
405 *age = kNoAgeCodeAge;
406 *parity = NO_MARKING_PARITY;
407 } else {
408 byte* target = sequence + kCodeAgeStubEntryOffset;
409 Code* stub = GetCodeFromTargetAddress(Memory::Address_at(target));
410 GetCodeAgeAndParity(stub, age, parity);
411 }
412}
413
414
415void Code::PatchPlatformCodeAge(Isolate* isolate,
416 byte* sequence,
417 Code::Age age,
418 MarkingParity parity) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 PatchingAssembler patcher(isolate, sequence,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 kNoCodeAgeSequenceLength / kInstructionSize);
421 if (age == kNoAgeCodeAge) {
422 MacroAssembler::EmitFrameSetupForCodeAgePatching(&patcher);
423 } else {
424 Code * stub = GetCodeAgeStub(isolate, age, parity);
425 MacroAssembler::EmitCodeAgeSequence(&patcher, stub);
426 }
427}
428
429
430void StringCharLoadGenerator::Generate(MacroAssembler* masm,
431 Register string,
432 Register index,
433 Register result,
434 Label* call_runtime) {
435 DCHECK(string.Is64Bits() && index.Is32Bits() && result.Is64Bits());
436 // Fetch the instance type of the receiver into result register.
437 __ Ldr(result, FieldMemOperand(string, HeapObject::kMapOffset));
438 __ Ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
439
440 // We need special handling for indirect strings.
441 Label check_sequential;
442 __ TestAndBranchIfAllClear(result, kIsIndirectStringMask, &check_sequential);
443
444 // Dispatch on the indirect string shape: slice or cons.
445 Label cons_string;
446 __ TestAndBranchIfAllClear(result, kSlicedNotConsMask, &cons_string);
447
448 // Handle slices.
449 Label indirect_string_loaded;
450 __ Ldr(result.W(),
451 UntagSmiFieldMemOperand(string, SlicedString::kOffsetOffset));
452 __ Ldr(string, FieldMemOperand(string, SlicedString::kParentOffset));
453 __ Add(index, index, result.W());
454 __ B(&indirect_string_loaded);
455
456 // Handle cons strings.
457 // Check whether the right hand side is the empty string (i.e. if
458 // this is really a flat string in a cons string). If that is not
459 // the case we would rather go to the runtime system now to flatten
460 // the string.
461 __ Bind(&cons_string);
462 __ Ldr(result, FieldMemOperand(string, ConsString::kSecondOffset));
463 __ JumpIfNotRoot(result, Heap::kempty_stringRootIndex, call_runtime);
464 // Get the first of the two strings and load its instance type.
465 __ Ldr(string, FieldMemOperand(string, ConsString::kFirstOffset));
466
467 __ Bind(&indirect_string_loaded);
468 __ Ldr(result, FieldMemOperand(string, HeapObject::kMapOffset));
469 __ Ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
470
471 // Distinguish sequential and external strings. Only these two string
472 // representations can reach here (slices and flat cons strings have been
473 // reduced to the underlying sequential or external string).
474 Label external_string, check_encoding;
475 __ Bind(&check_sequential);
476 STATIC_ASSERT(kSeqStringTag == 0);
477 __ TestAndBranchIfAnySet(result, kStringRepresentationMask, &external_string);
478
479 // Prepare sequential strings
480 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
481 __ Add(string, string, SeqTwoByteString::kHeaderSize - kHeapObjectTag);
482 __ B(&check_encoding);
483
484 // Handle external strings.
485 __ Bind(&external_string);
486 if (FLAG_debug_code) {
487 // Assert that we do not have a cons or slice (indirect strings) here.
488 // Sequential strings have already been ruled out.
489 __ Tst(result, kIsIndirectStringMask);
490 __ Assert(eq, kExternalStringExpectedButNotFound);
491 }
492 // Rule out short external strings.
493 STATIC_ASSERT(kShortExternalStringTag != 0);
494 // TestAndBranchIfAnySet can emit Tbnz. Do not use it because call_runtime
495 // can be bound far away in deferred code.
496 __ Tst(result, kShortExternalStringMask);
497 __ B(ne, call_runtime);
498 __ Ldr(string, FieldMemOperand(string, ExternalString::kResourceDataOffset));
499
500 Label one_byte, done;
501 __ Bind(&check_encoding);
502 STATIC_ASSERT(kTwoByteStringTag == 0);
503 __ TestAndBranchIfAnySet(result, kStringEncodingMask, &one_byte);
504 // Two-byte string.
505 __ Ldrh(result, MemOperand(string, index, SXTW, 1));
506 __ B(&done);
507 __ Bind(&one_byte);
508 // One-byte string.
509 __ Ldrb(result, MemOperand(string, index, SXTW));
510 __ Bind(&done);
511}
512
513
514static MemOperand ExpConstant(Register base, int index) {
515 return MemOperand(base, index * kDoubleSize);
516}
517
518
519void MathExpGenerator::EmitMathExp(MacroAssembler* masm,
520 DoubleRegister input,
521 DoubleRegister result,
522 DoubleRegister double_temp1,
523 DoubleRegister double_temp2,
524 Register temp1,
525 Register temp2,
526 Register temp3) {
527 // TODO(jbramley): There are several instances where fnmsub could be used
528 // instead of fmul and fsub. Doing this changes the result, but since this is
529 // an estimation anyway, does it matter?
530
531 DCHECK(!AreAliased(input, result,
532 double_temp1, double_temp2,
533 temp1, temp2, temp3));
534 DCHECK(ExternalReference::math_exp_constants(0).address() != NULL);
535 DCHECK(!masm->serializer_enabled()); // External references not serializable.
536
537 Label done;
538 DoubleRegister double_temp3 = result;
539 Register constants = temp3;
540
541 // The algorithm used relies on some magic constants which are initialized in
542 // ExternalReference::InitializeMathExpData().
543
544 // Load the address of the start of the array.
545 __ Mov(constants, ExternalReference::math_exp_constants(0));
546
547 // We have to do a four-way split here:
548 // - If input <= about -708.4, the output always rounds to zero.
549 // - If input >= about 709.8, the output always rounds to +infinity.
550 // - If the input is NaN, the output is NaN.
551 // - Otherwise, the result needs to be calculated.
552 Label result_is_finite_non_zero;
553 // Assert that we can load offset 0 (the small input threshold) and offset 1
554 // (the large input threshold) with a single ldp.
555 DCHECK(kDRegSize == (ExpConstant(constants, 1).offset() -
556 ExpConstant(constants, 0).offset()));
557 __ Ldp(double_temp1, double_temp2, ExpConstant(constants, 0));
558
559 __ Fcmp(input, double_temp1);
560 __ Fccmp(input, double_temp2, NoFlag, hi);
561 // At this point, the condition flags can be in one of five states:
562 // NZCV
563 // 1000 -708.4 < input < 709.8 result = exp(input)
564 // 0110 input == 709.8 result = +infinity
565 // 0010 input > 709.8 result = +infinity
566 // 0011 input is NaN result = input
567 // 0000 input <= -708.4 result = +0.0
568
569 // Continue the common case first. 'mi' tests N == 1.
570 __ B(&result_is_finite_non_zero, mi);
571
572 // TODO(jbramley): Consider adding a +infinity register for ARM64.
573 __ Ldr(double_temp2, ExpConstant(constants, 2)); // Synthesize +infinity.
574
575 // Select between +0.0 and +infinity. 'lo' tests C == 0.
576 __ Fcsel(result, fp_zero, double_temp2, lo);
577 // Select between {+0.0 or +infinity} and input. 'vc' tests V == 0.
578 __ Fcsel(result, result, input, vc);
579 __ B(&done);
580
581 // The rest is magic, as described in InitializeMathExpData().
582 __ Bind(&result_is_finite_non_zero);
583
584 // Assert that we can load offset 3 and offset 4 with a single ldp.
585 DCHECK(kDRegSize == (ExpConstant(constants, 4).offset() -
586 ExpConstant(constants, 3).offset()));
587 __ Ldp(double_temp1, double_temp3, ExpConstant(constants, 3));
588 __ Fmadd(double_temp1, double_temp1, input, double_temp3);
589 __ Fmov(temp2.W(), double_temp1.S());
590 __ Fsub(double_temp1, double_temp1, double_temp3);
591
592 // Assert that we can load offset 5 and offset 6 with a single ldp.
593 DCHECK(kDRegSize == (ExpConstant(constants, 6).offset() -
594 ExpConstant(constants, 5).offset()));
595 __ Ldp(double_temp2, double_temp3, ExpConstant(constants, 5));
596 // TODO(jbramley): Consider using Fnmsub here.
597 __ Fmul(double_temp1, double_temp1, double_temp2);
598 __ Fsub(double_temp1, double_temp1, input);
599
600 __ Fmul(double_temp2, double_temp1, double_temp1);
601 __ Fsub(double_temp3, double_temp3, double_temp1);
602 __ Fmul(double_temp3, double_temp3, double_temp2);
603
604 __ Mov(temp1.W(), Operand(temp2.W(), LSR, 11));
605
606 __ Ldr(double_temp2, ExpConstant(constants, 7));
607 // TODO(jbramley): Consider using Fnmsub here.
608 __ Fmul(double_temp3, double_temp3, double_temp2);
609 __ Fsub(double_temp3, double_temp3, double_temp1);
610
611 // The 8th constant is 1.0, so use an immediate move rather than a load.
612 // We can't generate a runtime assertion here as we would need to call Abort
613 // in the runtime and we don't have an Isolate when we generate this code.
614 __ Fmov(double_temp2, 1.0);
615 __ Fadd(double_temp3, double_temp3, double_temp2);
616
617 __ And(temp2, temp2, 0x7ff);
618 __ Add(temp1, temp1, 0x3ff);
619
620 // Do the final table lookup.
621 __ Mov(temp3, ExternalReference::math_exp_log_table());
622
623 __ Add(temp3, temp3, Operand(temp2, LSL, kDRegSizeLog2));
624 __ Ldp(temp2.W(), temp3.W(), MemOperand(temp3));
625 __ Orr(temp1.W(), temp3.W(), Operand(temp1.W(), LSL, 20));
626 __ Bfi(temp2, temp1, 32, 32);
627 __ Fmov(double_temp1, temp2);
628
629 __ Fmul(result, double_temp3, double_temp1);
630
631 __ Bind(&done);
632}
633
634#undef __
635
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636} // namespace internal
637} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638
639#endif // V8_TARGET_ARCH_ARM64