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/dart_entry.h" |
9 | #include "vm/isolate.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/symbols.h" |
16 | #include "vm/unit_test.h" |
17 | |
18 | #define __ assembler-> |
19 | |
20 | namespace dart { |
21 | |
22 | static Function* CreateFunction(const char* name) { |
23 | const String& class_name = |
24 | String::Handle(Symbols::New(Thread::Current(), "ownerClass" )); |
25 | const Script& script = Script::Handle(); |
26 | const Library& lib = Library::Handle(Library::New(class_name)); |
27 | const Class& owner_class = Class::Handle( |
28 | Class::New(lib, class_name, script, TokenPosition::kNoSource)); |
29 | const String& function_name = |
30 | String::ZoneHandle(Symbols::New(Thread::Current(), name)); |
31 | Function& function = Function::ZoneHandle(Function::New( |
32 | function_name, FunctionLayout::kRegularFunction, true, false, false, |
33 | false, false, owner_class, TokenPosition::kNoSource)); |
34 | return &function; |
35 | } |
36 | |
37 | // Test calls to stub code which calls into the runtime. |
38 | static void GenerateCallToCallRuntimeStub(compiler::Assembler* assembler, |
39 | int length) { |
40 | const int argc = 2; |
41 | const Smi& smi_length = Smi::ZoneHandle(Smi::New(length)); |
42 | __ EnterStubFrame(); |
43 | __ PushObject(Object::null_object()); // Push Null obj for return value. |
44 | __ PushObject(smi_length); // Push argument 1: length. |
45 | __ PushObject(Object::null_object()); // Push argument 2: type arguments. |
46 | ASSERT(kAllocateArrayRuntimeEntry.argument_count() == argc); |
47 | __ CallRuntime(kAllocateArrayRuntimeEntry, argc); |
48 | __ AddImmediate(RSP, compiler::Immediate(argc * kWordSize)); |
49 | __ popq(RAX); // Pop return value from return slot. |
50 | __ LeaveStubFrame(); |
51 | __ ret(); |
52 | } |
53 | |
54 | ISOLATE_UNIT_TEST_CASE(CallRuntimeStubCode) { |
55 | extern const Function& RegisterFakeFunction(const char* name, |
56 | const Code& code); |
57 | const int length = 10; |
58 | const char* kName = "Test_CallRuntimeStubCode" ; |
59 | compiler::ObjectPoolBuilder object_pool_builder; |
60 | compiler::Assembler assembler(&object_pool_builder); |
61 | GenerateCallToCallRuntimeStub(&assembler, length); |
62 | const Code& code = Code::Handle(Code::FinalizeCodeAndNotify( |
63 | *CreateFunction("Test_CallRuntimeStubCode" ), nullptr, &assembler, |
64 | Code::PoolAttachment::kAttachPool)); |
65 | const Function& function = RegisterFakeFunction(kName, code); |
66 | Array& result = Array::Handle(); |
67 | result ^= DartEntry::InvokeFunction(function, Object::empty_array()); |
68 | EXPECT_EQ(length, result.Length()); |
69 | } |
70 | |
71 | // Test calls to stub code which calls into a leaf runtime entry. |
72 | static void GenerateCallToCallLeafRuntimeStub(compiler::Assembler* assembler, |
73 | const char* str_value, |
74 | intptr_t lhs_index_value, |
75 | intptr_t rhs_index_value, |
76 | intptr_t length_value) { |
77 | const String& str = String::ZoneHandle(String::New(str_value, Heap::kOld)); |
78 | const Smi& lhs_index = Smi::ZoneHandle(Smi::New(lhs_index_value)); |
79 | const Smi& rhs_index = Smi::ZoneHandle(Smi::New(rhs_index_value)); |
80 | const Smi& length = Smi::ZoneHandle(Smi::New(length_value)); |
81 | __ EnterStubFrame(); |
82 | __ ReserveAlignedFrameSpace(0); |
83 | __ LoadObject(CallingConventions::kArg1Reg, str); |
84 | __ LoadObject(CallingConventions::kArg2Reg, lhs_index); |
85 | __ LoadObject(CallingConventions::kArg3Reg, rhs_index); |
86 | __ LoadObject(CallingConventions::kArg4Reg, length); |
87 | __ CallRuntime(kCaseInsensitiveCompareUCS2RuntimeEntry, 4); |
88 | __ LeaveStubFrame(); |
89 | __ ret(); // Return value is in RAX. |
90 | } |
91 | |
92 | ISOLATE_UNIT_TEST_CASE(CallLeafRuntimeStubCode) { |
93 | extern const Function& RegisterFakeFunction(const char* name, |
94 | const Code& code); |
95 | const char* str_value = "abAB" ; |
96 | intptr_t lhs_index_value = 0; |
97 | intptr_t rhs_index_value = 2; |
98 | intptr_t length_value = 2; |
99 | const char* kName = "Test_CallLeafRuntimeStubCode" ; |
100 | compiler::ObjectPoolBuilder object_pool_builder; |
101 | compiler::Assembler assembler(&object_pool_builder); |
102 | GenerateCallToCallLeafRuntimeStub(&assembler, str_value, lhs_index_value, |
103 | rhs_index_value, length_value); |
104 | const Code& code = Code::Handle(Code::FinalizeCodeAndNotify( |
105 | *CreateFunction("Test_CallLeafRuntimeStubCode" ), nullptr, &assembler, |
106 | Code::PoolAttachment::kAttachPool)); |
107 | const Function& function = RegisterFakeFunction(kName, code); |
108 | Instance& result = Instance::Handle(); |
109 | result ^= DartEntry::InvokeFunction(function, Object::empty_array()); |
110 | EXPECT_EQ(Bool::True().raw(), result.raw()); |
111 | } |
112 | |
113 | } // namespace dart |
114 | |
115 | #endif // defined TARGET_ARCH_X64 |
116 | |