1 // Copyright 2015 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 5 #include "src/interface-descriptors.h" 6 #include "src/isolate.h" 7 #include "test/cctest/compiler/function-tester.h" 8 9 namespace v8 { 10 namespace internal { 11 namespace compiler { 12 13 14 class CodeStubAssemblerTester : public CodeStubAssembler { 15 public: 16 CodeStubAssemblerTester(Isolate* isolate, 17 const CallInterfaceDescriptor& descriptor) 18 : CodeStubAssembler(isolate, isolate->runtime_zone(), descriptor, 19 Code::STUB, "test"), 20 scope_(isolate) {} 21 22 private: 23 HandleScope scope_; 24 LocalContext context_; 25 }; 26 27 28 TEST(SimpleSmiReturn) { 29 Isolate* isolate(CcTest::InitIsolateOnce()); 30 VoidDescriptor descriptor(isolate); 31 CodeStubAssemblerTester m(isolate, descriptor); 32 m.Return(m.SmiTag(m.Int32Constant(37))); 33 Handle<Code> code = m.GenerateCode(); 34 FunctionTester ft(descriptor, code); 35 MaybeHandle<Object> result = ft.Call(); 36 CHECK_EQ(37, Handle<Smi>::cast(result.ToHandleChecked())->value()); 37 } 38 39 40 TEST(SimpleIntPtrReturn) { 41 Isolate* isolate(CcTest::InitIsolateOnce()); 42 VoidDescriptor descriptor(isolate); 43 CodeStubAssemblerTester m(isolate, descriptor); 44 int test; 45 m.Return(m.IntPtrConstant(reinterpret_cast<intptr_t>(&test))); 46 Handle<Code> code = m.GenerateCode(); 47 FunctionTester ft(descriptor, code); 48 MaybeHandle<Object> result = ft.Call(); 49 CHECK_EQ(reinterpret_cast<intptr_t>(&test), 50 reinterpret_cast<intptr_t>(*result.ToHandleChecked())); 51 } 52 53 54 TEST(SimpleDoubleReturn) { 55 Isolate* isolate(CcTest::InitIsolateOnce()); 56 VoidDescriptor descriptor(isolate); 57 CodeStubAssemblerTester m(isolate, descriptor); 58 m.Return(m.NumberConstant(0.5)); 59 Handle<Code> code = m.GenerateCode(); 60 FunctionTester ft(descriptor, code); 61 MaybeHandle<Object> result = ft.Call(); 62 CHECK_EQ(0.5, Handle<HeapNumber>::cast(result.ToHandleChecked())->value()); 63 } 64 65 66 TEST(SimpleCallRuntime1Arg) { 67 Isolate* isolate(CcTest::InitIsolateOnce()); 68 VoidDescriptor descriptor(isolate); 69 CodeStubAssemblerTester m(isolate, descriptor); 70 Node* context = m.HeapConstant(Handle<Context>(isolate->native_context())); 71 Node* b = m.SmiTag(m.Int32Constant(256)); 72 m.Return(m.CallRuntime(Runtime::kMathSqrt, context, b)); 73 Handle<Code> code = m.GenerateCode(); 74 FunctionTester ft(descriptor, code); 75 MaybeHandle<Object> result = ft.Call(); 76 CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value()); 77 } 78 79 80 TEST(SimpleTailCallRuntime1Arg) { 81 Isolate* isolate(CcTest::InitIsolateOnce()); 82 VoidDescriptor descriptor(isolate); 83 CodeStubAssemblerTester m(isolate, descriptor); 84 Node* context = m.HeapConstant(Handle<Context>(isolate->native_context())); 85 Node* b = m.SmiTag(m.Int32Constant(256)); 86 m.TailCallRuntime(Runtime::kMathSqrt, context, b); 87 Handle<Code> code = m.GenerateCode(); 88 FunctionTester ft(descriptor, code); 89 MaybeHandle<Object> result = ft.Call(); 90 CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value()); 91 } 92 93 94 TEST(SimpleCallRuntime2Arg) { 95 Isolate* isolate(CcTest::InitIsolateOnce()); 96 VoidDescriptor descriptor(isolate); 97 CodeStubAssemblerTester m(isolate, descriptor); 98 Node* context = m.HeapConstant(Handle<Context>(isolate->native_context())); 99 Node* a = m.SmiTag(m.Int32Constant(2)); 100 Node* b = m.SmiTag(m.Int32Constant(4)); 101 m.Return(m.CallRuntime(Runtime::kMathPow, context, a, b)); 102 Handle<Code> code = m.GenerateCode(); 103 FunctionTester ft(descriptor, code); 104 MaybeHandle<Object> result = ft.Call(); 105 CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value()); 106 } 107 108 109 TEST(SimpleTailCallRuntime2Arg) { 110 Isolate* isolate(CcTest::InitIsolateOnce()); 111 VoidDescriptor descriptor(isolate); 112 CodeStubAssemblerTester m(isolate, descriptor); 113 Node* context = m.HeapConstant(Handle<Context>(isolate->native_context())); 114 Node* a = m.SmiTag(m.Int32Constant(2)); 115 Node* b = m.SmiTag(m.Int32Constant(4)); 116 m.TailCallRuntime(Runtime::kMathPow, context, a, b); 117 Handle<Code> code = m.GenerateCode(); 118 FunctionTester ft(descriptor, code); 119 MaybeHandle<Object> result = ft.Call(); 120 CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value()); 121 } 122 123 } // namespace compiler 124 } // namespace internal 125 } // namespace v8 126