OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include <stdlib.h> | 28 #include <stdlib.h> |
29 | 29 |
30 #include "src/v8.h" | 30 #include "src/v8.h" |
31 | 31 |
32 #include "src/base/platform/platform.h" | 32 #include "src/base/platform/platform.h" |
33 #include "src/factory.h" | 33 #include "src/factory.h" |
34 #include "src/macro-assembler.h" | 34 #include "src/macro-assembler.h" |
35 #include "test/cctest/cctest.h" | 35 #include "test/cctest/cctest.h" |
| 36 #include "test/cctest/heap/heap-utils.h" |
36 | 37 |
37 namespace i = v8::internal; | 38 namespace i = v8::internal; |
38 using i::Address; | 39 using i::Address; |
39 using i::Assembler; | 40 using i::Assembler; |
40 using i::CodeDesc; | 41 using i::CodeDesc; |
41 using i::Condition; | 42 using i::Condition; |
42 using i::FUNCTION_CAST; | 43 using i::FUNCTION_CAST; |
43 using i::HandleScope; | 44 using i::HandleScope; |
44 using i::Immediate; | 45 using i::Immediate; |
45 using i::Isolate; | 46 using i::Isolate; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // Test the x64 assembler by compiling some simple functions into | 85 // Test the x64 assembler by compiling some simple functions into |
85 // a buffer and executing them. These tests do not initialize the | 86 // a buffer and executing them. These tests do not initialize the |
86 // V8 library, create a context, or use any V8 objects. | 87 // V8 library, create a context, or use any V8 objects. |
87 // The AMD64 calling convention is used, with the first five arguments | 88 // The AMD64 calling convention is used, with the first five arguments |
88 // in RSI, RDI, RDX, RCX, R8, and R9, and floating point arguments in | 89 // in RSI, RDI, RDX, RCX, R8, and R9, and floating point arguments in |
89 // the XMM registers. The return value is in RAX. | 90 // the XMM registers. The return value is in RAX. |
90 // This calling convention is used on Linux, with GCC, and on Mac OS, | 91 // This calling convention is used on Linux, with GCC, and on Mac OS, |
91 // with GCC. A different convention is used on 64-bit windows. | 92 // with GCC. A different convention is used on 64-bit windows. |
92 | 93 |
93 typedef int (*F0)(); | 94 typedef int (*F0)(); |
| 95 typedef intptr_t (*F1)(); |
94 | 96 |
95 #define __ masm-> | 97 #define __ masm-> |
96 | 98 |
97 | 99 |
98 static void EntryCode(MacroAssembler* masm) { | 100 static void EntryCode(MacroAssembler* masm) { |
99 // Smi constant register is callee save. | 101 // Smi constant register is callee save. |
100 __ pushq(i::kRootRegister); | 102 __ pushq(i::kRootRegister); |
101 __ InitializeRootRegister(); | 103 __ InitializeRootRegister(); |
102 } | 104 } |
103 | 105 |
(...skipping 2617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2721 ExitCode(masm); | 2723 ExitCode(masm); |
2722 __ ret(0); | 2724 __ ret(0); |
2723 | 2725 |
2724 CodeDesc desc; | 2726 CodeDesc desc; |
2725 masm->GetCode(&desc); | 2727 masm->GetCode(&desc); |
2726 // Call the function from C++. | 2728 // Call the function from C++. |
2727 int result = FUNCTION_CAST<F0>(buffer)(); | 2729 int result = FUNCTION_CAST<F0>(buffer)(); |
2728 CHECK_EQ(0, result); | 2730 CHECK_EQ(0, result); |
2729 } | 2731 } |
2730 | 2732 |
| 2733 // Helper for the tests below. |
| 2734 template <typename Fun> |
| 2735 void AssembleFunction(Isolate* isolate, size_t actual_size, byte* buffer, |
| 2736 Fun f) { |
| 2737 MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size), |
| 2738 v8::internal::CodeObjectRequired::kYes); |
| 2739 MacroAssembler* masm = &assembler; |
| 2740 Label exit; |
| 2741 EntryCode(masm); |
| 2742 f(masm); |
| 2743 ExitCode(masm); |
| 2744 __ ret(0); |
| 2745 |
| 2746 CodeDesc desc; |
| 2747 masm->GetCode(&desc); |
| 2748 } |
| 2749 |
| 2750 TEST(AllocateMacrosNoGCRequired) { |
| 2751 // Allocate an executable page of memory. |
| 2752 size_t actual_size; |
| 2753 byte* buffer = static_cast<byte*>(v8::base::OS::Allocate( |
| 2754 Assembler::kMinimalBufferSize, &actual_size, true)); |
| 2755 CHECK(buffer); |
| 2756 Isolate* isolate = CcTest::i_isolate(); |
| 2757 HandleScope handles(isolate); |
| 2758 |
| 2759 AllocationFlags const kDoubleAligned = |
| 2760 static_cast<AllocationFlags>(DOUBLE_ALIGNMENT); |
| 2761 AllocationFlags const kNoAllocationFlags = |
| 2762 static_cast<AllocationFlags>(NO_ALLOCATION_FLAGS); |
| 2763 |
| 2764 #define CHECK_TAGGED(result) CHECK_EQ(result& i::kHeapObjectTag, 1); |
| 2765 #define CHECK_DOUBLE_ALIGNED(result) \ |
| 2766 do { \ |
| 2767 CHECK_TAGGED((result)); \ |
| 2768 CHECK_EQ((result)&i::kDoubleAlignmentMaskTagged, 0); \ |
| 2769 } while (false) |
| 2770 |
| 2771 i::heap::GcAndSweep(isolate->heap(), i::AllocationSpace::NEW_SPACE); |
| 2772 { |
| 2773 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2774 Label gc_required, success; |
| 2775 __ Allocate(kPointerSize, rax, rcx, rdx, &gc_required, |
| 2776 kNoAllocationFlags); |
| 2777 __ jmp(&success); |
| 2778 __ bind(&gc_required); |
| 2779 __ Abort(i::kNoReason); |
| 2780 __ bind(&success); |
| 2781 }); |
| 2782 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2783 CHECK_TAGGED(test_result); |
| 2784 } |
| 2785 |
| 2786 { |
| 2787 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2788 Label gc_required, success; |
| 2789 __ Allocate(i::kDoubleSize, rax, rcx, rdx, &gc_required, kDoubleAligned); |
| 2790 __ jmp(&success); |
| 2791 __ bind(&gc_required); |
| 2792 __ Abort(i::kNoReason); |
| 2793 __ bind(&success); |
| 2794 }); |
| 2795 i::heap::MakeSureNewSpaceTopIsNotDoubleAligned(isolate->heap()); |
| 2796 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2797 CHECK_DOUBLE_ALIGNED(test_result); |
| 2798 } |
| 2799 { |
| 2800 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2801 Label gc_required, success; |
| 2802 __ movl(rbx, Immediate(kPointerSize)); |
| 2803 __ Allocate(kPointerSize, rax, rcx, rdx, &gc_required, |
| 2804 kNoAllocationFlags); |
| 2805 __ jmp(&success); |
| 2806 __ bind(&gc_required); |
| 2807 __ Abort(i::kNoReason); |
| 2808 __ bind(&success); |
| 2809 }); |
| 2810 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2811 CHECK_TAGGED(test_result); |
| 2812 } |
| 2813 |
| 2814 { |
| 2815 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2816 Label gc_required, success; |
| 2817 __ movl(rbx, Immediate(i::kDoubleSize)); |
| 2818 __ Allocate(rbx, rax, rcx, rdx, &gc_required, kDoubleAligned); |
| 2819 __ jmp(&success); |
| 2820 __ bind(&gc_required); |
| 2821 __ Abort(i::kNoReason); |
| 2822 __ bind(&success); |
| 2823 }); |
| 2824 i::heap::MakeSureNewSpaceTopIsNotDoubleAligned(isolate->heap()); |
| 2825 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2826 CHECK_DOUBLE_ALIGNED(test_result); |
| 2827 } |
| 2828 |
| 2829 { |
| 2830 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2831 Label gc_required, success; |
| 2832 __ movl(rbx, Immediate(2)); |
| 2833 __ Allocate(0, i::times_4, rbx, rax, rcx, rdx, &gc_required, |
| 2834 kNoAllocationFlags); |
| 2835 __ jmp(&success); |
| 2836 __ bind(&gc_required); |
| 2837 __ Abort(i::kNoReason); |
| 2838 __ bind(&success); |
| 2839 }); |
| 2840 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2841 CHECK_TAGGED(test_result); |
| 2842 } |
| 2843 |
| 2844 { |
| 2845 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2846 Label gc_required, success; |
| 2847 __ movl(rbx, Immediate(i::kDoubleSize)); |
| 2848 __ Allocate(i::FixedDoubleArray::kHeaderSize, i::times_8, rbx, rax, rcx, |
| 2849 rdx, &gc_required, kDoubleAligned); |
| 2850 __ jmp(&success); |
| 2851 __ bind(&gc_required); |
| 2852 __ Abort(i::kNoReason); |
| 2853 __ bind(&success); |
| 2854 }); |
| 2855 i::heap::MakeSureNewSpaceTopIsNotDoubleAligned(isolate->heap()); |
| 2856 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2857 CHECK_DOUBLE_ALIGNED(test_result); |
| 2858 } |
| 2859 #undef CHECK_TAGGED |
| 2860 #undef CHECK_DOUBLE_ALIGNED |
| 2861 } |
| 2862 |
| 2863 TEST(AllocateMacrosGCRequired) { |
| 2864 // Allocate an executable page of memory. |
| 2865 size_t actual_size; |
| 2866 byte* buffer = static_cast<byte*>(v8::base::OS::Allocate( |
| 2867 Assembler::kMinimalBufferSize, &actual_size, true)); |
| 2868 CHECK(buffer); |
| 2869 Isolate* isolate = CcTest::i_isolate(); |
| 2870 HandleScope handles(isolate); |
| 2871 |
| 2872 AllocationFlags const kDoubleAligned = |
| 2873 static_cast<AllocationFlags>(DOUBLE_ALIGNMENT); |
| 2874 |
| 2875 // Test that the macro jumps to the gc_required label if there's not enough |
| 2876 // space to allocate with double alignment. |
| 2877 { |
| 2878 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2879 Label gc_required, end; |
| 2880 __ Allocate(kPointerSize, rax, rcx, rdx, &gc_required, kDoubleAligned); |
| 2881 __ xorl(rax, rax); |
| 2882 __ jmp(&end); |
| 2883 __ bind(&gc_required); |
| 2884 __ movl(rax, Immediate(1)); |
| 2885 __ bind(&end); |
| 2886 }); |
| 2887 i::heap::AllocateAllButNBytes(isolate->heap()->new_space(), 4); |
| 2888 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2889 CHECK_EQ(test_result, 1); |
| 2890 } |
| 2891 |
| 2892 { |
| 2893 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2894 Label gc_required, end; |
| 2895 __ movl(rbx, Immediate(kPointerSize)); |
| 2896 __ Allocate(rbx, rax, rcx, rdx, &gc_required, kDoubleAligned); |
| 2897 __ xorl(rax, rax); |
| 2898 __ jmp(&end); |
| 2899 __ bind(&gc_required); |
| 2900 __ movl(rax, Immediate(1)); |
| 2901 __ bind(&end); |
| 2902 }); |
| 2903 i::heap::AllocateAllButNBytes(isolate->heap()->new_space(), 4); |
| 2904 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2905 CHECK_EQ(test_result, 1); |
| 2906 } |
| 2907 |
| 2908 { |
| 2909 AssembleFunction(isolate, actual_size, buffer, [](MacroAssembler* masm) { |
| 2910 Label gc_required, end; |
| 2911 __ movl(rbx, Immediate(kPointerSize)); |
| 2912 __ Allocate(0, i::times_8, rbx, rax, rcx, rdx, &gc_required, |
| 2913 kDoubleAligned); |
| 2914 __ xorl(rax, rax); |
| 2915 __ jmp(&end); |
| 2916 __ bind(&gc_required); |
| 2917 __ movl(rax, Immediate(1)); |
| 2918 __ bind(&end); |
| 2919 }); |
| 2920 i::heap::AllocateAllButNBytes(isolate->heap()->new_space(), 4); |
| 2921 intptr_t test_result = FUNCTION_CAST<F1>(buffer)(); |
| 2922 CHECK_EQ(test_result, 1); |
| 2923 } |
| 2924 } |
2731 | 2925 |
2732 #undef __ | 2926 #undef __ |
OLD | NEW |