1 | // Copyright (c) 2013, 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_ARM) |
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 | __ EnterDartFrame(0); |
43 | __ PushObject(Object::null_object()); // Push Null object 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(SP, argc * kWordSize); |
49 | __ Pop(R0); // Pop return value from return slot. |
50 | __ LeaveDartFrameAndReturn(); |
51 | } |
52 | |
53 | ISOLATE_UNIT_TEST_CASE(CallRuntimeStubCode) { |
54 | extern const Function& RegisterFakeFunction(const char* name, |
55 | const Code& code); |
56 | const int length = 10; |
57 | const char* kName = "Test_CallRuntimeStubCode" ; |
58 | compiler::ObjectPoolBuilder object_pool_builder; |
59 | compiler::Assembler assembler(&object_pool_builder); |
60 | GenerateCallToCallRuntimeStub(&assembler, length); |
61 | const Code& code = Code::Handle(Code::FinalizeCodeAndNotify( |
62 | *CreateFunction("Test_CallRuntimeStubCode" ), nullptr, &assembler, |
63 | Code::PoolAttachment::kAttachPool)); |
64 | const Function& function = RegisterFakeFunction(kName, code); |
65 | Array& result = Array::Handle(); |
66 | result ^= DartEntry::InvokeFunction(function, Object::empty_array()); |
67 | EXPECT_EQ(length, result.Length()); |
68 | } |
69 | |
70 | // Test calls to stub code which calls into a leaf runtime entry. |
71 | static void GenerateCallToCallLeafRuntimeStub(compiler::Assembler* assembler, |
72 | const char* str_value, |
73 | intptr_t lhs_index_value, |
74 | intptr_t rhs_index_value, |
75 | intptr_t length_value) { |
76 | const String& str = String::ZoneHandle(String::New(str_value, Heap::kOld)); |
77 | const Smi& lhs_index = Smi::ZoneHandle(Smi::New(lhs_index_value)); |
78 | const Smi& rhs_index = Smi::ZoneHandle(Smi::New(rhs_index_value)); |
79 | const Smi& length = Smi::ZoneHandle(Smi::New(length_value)); |
80 | __ EnterDartFrame(0); |
81 | __ ReserveAlignedFrameSpace(0); |
82 | __ LoadObject(R0, str); |
83 | __ LoadObject(R1, lhs_index); |
84 | __ LoadObject(R2, rhs_index); |
85 | __ LoadObject(R3, length); |
86 | __ CallRuntime(kCaseInsensitiveCompareUCS2RuntimeEntry, 4); |
87 | __ LeaveDartFrameAndReturn(); // Return value is in R0. |
88 | } |
89 | |
90 | ISOLATE_UNIT_TEST_CASE(CallLeafRuntimeStubCode) { |
91 | extern const Function& RegisterFakeFunction(const char* name, |
92 | const Code& code); |
93 | const char* str_value = "abAB" ; |
94 | intptr_t lhs_index_value = 0; |
95 | intptr_t rhs_index_value = 2; |
96 | intptr_t length_value = 2; |
97 | const char* kName = "Test_CallLeafRuntimeStubCode" ; |
98 | compiler::ObjectPoolBuilder object_pool_builder; |
99 | compiler::Assembler assembler(&object_pool_builder); |
100 | GenerateCallToCallLeafRuntimeStub(&assembler, str_value, lhs_index_value, |
101 | rhs_index_value, length_value); |
102 | const Code& code = Code::Handle(Code::FinalizeCodeAndNotify( |
103 | *CreateFunction("Test_CallLeafRuntimeStubCode" ), nullptr, &assembler, |
104 | Code::PoolAttachment::kAttachPool)); |
105 | const Function& function = RegisterFakeFunction(kName, code); |
106 | Instance& result = Instance::Handle(); |
107 | result ^= DartEntry::InvokeFunction(function, Object::empty_array()); |
108 | EXPECT_EQ(Bool::True().raw(), result.raw()); |
109 | } |
110 | |
111 | } // namespace dart |
112 | |
113 | #endif // defined TARGET_ARCH_ARM |
114 | |