OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "vm/globals.h" |
| 6 #if defined(TARGET_ARCH_X64) |
| 7 |
| 8 #include "vm/isolate.h" |
| 9 #include "vm/dart_entry.h" |
| 10 #include "vm/native_entry.h" |
| 11 #include "vm/native_entry_test.h" |
| 12 #include "vm/object.h" |
| 13 #include "vm/runtime_entry.h" |
| 14 #include "vm/stub_code.h" |
| 15 #include "vm/unit_test.h" |
| 16 |
| 17 #define __ assembler-> |
| 18 |
| 19 namespace dart { |
| 20 |
| 21 DECLARE_RUNTIME_ENTRY(TestSmiSub); |
| 22 |
| 23 |
| 24 // Test calls to stub code which calls into the runtime. |
| 25 static void GenerateCallToCallRuntimeStub(Assembler* assembler, |
| 26 int value1, int value2) { |
| 27 const int argc = 2; |
| 28 const Smi& smi1 = Smi::ZoneHandle(Smi::New(value1)); |
| 29 const Smi& smi2 = Smi::ZoneHandle(Smi::New(value2)); |
| 30 const Object& result = Object::ZoneHandle(); |
| 31 const Context& context = Context::ZoneHandle(Context::New(0)); |
| 32 ASSERT(context.isolate() == Isolate::Current()); |
| 33 __ enter(Immediate(0)); |
| 34 __ LoadObject(CTX, context); |
| 35 __ PushObject(result); // Push Null object for return value. |
| 36 __ PushObject(smi1); // Push argument 1 smi1. |
| 37 __ PushObject(smi2); // Push argument 2 smi2. |
| 38 ASSERT(kTestSmiSubRuntimeEntry.argument_count() == argc); |
| 39 __ CallRuntimeFromStub(kTestSmiSubRuntimeEntry); // Call SmiSub runtime func. |
| 40 __ AddImmediate(RSP, Immediate(argc * kWordSize)); |
| 41 __ popq(RAX); // Pop return value from return slot. |
| 42 __ leave(); |
| 43 __ ret(); |
| 44 } |
| 45 |
| 46 |
| 47 TEST_CASE(CallRuntimeStubCode) { |
| 48 extern const Function& RegisterFakeFunction(const char* name, |
| 49 const Code& code); |
| 50 const int value1 = 10; |
| 51 const int value2 = 20; |
| 52 const char* kName = "Test_CallRuntimeStubCode"; |
| 53 Assembler _assembler_; |
| 54 GenerateCallToCallRuntimeStub(&_assembler_, value1, value2); |
| 55 const Code& code = Code::Handle( |
| 56 Code::FinalizeCode("Test_CallRuntimeStubCode", &_assembler_)); |
| 57 const Function& function = RegisterFakeFunction(kName, code); |
| 58 GrowableArray<const Object*> arguments; |
| 59 const Array& kNoArgumentNames = Array::Handle(); |
| 60 Smi& result = Smi::Handle(); |
| 61 result ^= DartEntry::InvokeStatic(function, arguments, kNoArgumentNames); |
| 62 EXPECT_EQ((value1 - value2), result.Value()); |
| 63 } |
| 64 |
| 65 |
| 66 // Test calls to stub code which calls a native C function. |
| 67 static void GenerateCallToCallNativeCFunctionStub(Assembler* assembler, |
| 68 int value1, int value2) { |
| 69 const int argc = 2; |
| 70 const Smi& smi1 = Smi::ZoneHandle(Smi::New(value1)); |
| 71 const Smi& smi2 = Smi::ZoneHandle(Smi::New(value2)); |
| 72 const Object& result = Object::ZoneHandle(); |
| 73 const String& native_name = String::ZoneHandle(String::New("TestSmiSub")); |
| 74 Dart_NativeFunction native_function = |
| 75 NativeTestEntry_Lookup(native_name, argc); |
| 76 const Context& context = Context::ZoneHandle(Context::New(0)); |
| 77 ASSERT(context.isolate() == Isolate::Current()); |
| 78 __ enter(Immediate(0)); |
| 79 __ LoadObject(CTX, context); |
| 80 __ PushObject(smi1); // Push argument 1 smi1. |
| 81 __ PushObject(smi2); // Push argument 2 smi2. |
| 82 __ PushObject(result); // Push Null object for return value. |
| 83 // Pass a pointer to the first argument in RAX. |
| 84 __ leaq(RAX, Address(RSP, 2 * kWordSize)); |
| 85 __ movq(RBX, Immediate(reinterpret_cast<uword>(native_function))); |
| 86 __ movq(R10, Immediate(argc)); |
| 87 __ call(&StubCode::CallNativeCFunctionLabel()); |
| 88 __ popq(RAX); // Pop return value from return slot. |
| 89 __ AddImmediate(RSP, Immediate(argc * kWordSize)); |
| 90 __ leave(); |
| 91 __ ret(); |
| 92 } |
| 93 |
| 94 |
| 95 TEST_CASE(CallNativeCFunctionStubCode) { |
| 96 extern const Function& RegisterFakeFunction(const char* name, |
| 97 const Code& code); |
| 98 const int value1 = 15; |
| 99 const int value2 = 20; |
| 100 const char* kName = "Test_CallNativeCFunctionStubCode"; |
| 101 Assembler _assembler_; |
| 102 GenerateCallToCallNativeCFunctionStub(&_assembler_, value1, value2); |
| 103 const Code& code = Code::Handle( |
| 104 Code::FinalizeCode(kName, &_assembler_)); |
| 105 const Function& function = RegisterFakeFunction(kName, code); |
| 106 GrowableArray<const Object*> arguments; |
| 107 const Array& kNoArgumentNames = Array::Handle(); |
| 108 Smi& result = Smi::Handle(); |
| 109 result ^= DartEntry::InvokeStatic(function, arguments, kNoArgumentNames); |
| 110 EXPECT_EQ((value1 - value2), result.Value()); |
| 111 } |
| 112 |
| 113 } // namespace dart |
| 114 |
| 115 #endif // defined TARGET_ARCH_X64 |
OLD | NEW |