| 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_IA32) |
| 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::kMinSource)); |
| 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 | __ enter(compiler::Immediate(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(ESP, compiler::Immediate(argc * kWordSize)); |
| 49 | __ popl(EAX); // Pop return value from return slot. |
| 50 | __ leave(); |
| 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::Assembler assembler(nullptr); |
| 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 | __ enter(compiler::Immediate(0)); |
| 81 | __ ReserveAlignedFrameSpace(4 * kWordSize); |
| 82 | __ LoadObject(EAX, str); |
| 83 | __ movl(compiler::Address(ESP, 0), EAX); // Push argument 1. |
| 84 | __ LoadObject(EAX, lhs_index); |
| 85 | __ movl(compiler::Address(ESP, kWordSize), EAX); // Push argument 2. |
| 86 | __ LoadObject(EAX, rhs_index); |
| 87 | __ movl(compiler::Address(ESP, 2 * kWordSize), EAX); // Push argument 3. |
| 88 | __ LoadObject(EAX, length); |
| 89 | __ movl(compiler::Address(ESP, 3 * kWordSize), EAX); // Push argument 4. |
| 90 | __ CallRuntime(kCaseInsensitiveCompareUCS2RuntimeEntry, 4); |
| 91 | __ leave(); |
| 92 | __ ret(); // Return value is in EAX. |
| 93 | } |
| 94 | |
| 95 | ISOLATE_UNIT_TEST_CASE(CallLeafRuntimeStubCode) { |
| 96 | extern const Function& RegisterFakeFunction(const char* name, |
| 97 | const Code& code); |
| 98 | const char* str_value = "abAB" ; |
| 99 | intptr_t lhs_index_value = 0; |
| 100 | intptr_t rhs_index_value = 2; |
| 101 | intptr_t length_value = 2; |
| 102 | const char* kName = "Test_CallLeafRuntimeStubCode" ; |
| 103 | compiler::Assembler assembler(nullptr); |
| 104 | GenerateCallToCallLeafRuntimeStub(&assembler, str_value, lhs_index_value, |
| 105 | rhs_index_value, length_value); |
| 106 | const Code& code = Code::Handle(Code::FinalizeCodeAndNotify( |
| 107 | *CreateFunction("Test_CallLeafRuntimeStubCode" ), nullptr, &assembler, |
| 108 | Code::PoolAttachment::kAttachPool)); |
| 109 | const Function& function = RegisterFakeFunction(kName, code); |
| 110 | Instance& result = Instance::Handle(); |
| 111 | result ^= DartEntry::InvokeFunction(function, Object::empty_array()); |
| 112 | EXPECT_EQ(Bool::True().raw(), result.raw()); |
| 113 | } |
| 114 | |
| 115 | } // namespace dart |
| 116 | |
| 117 | #endif // defined TARGET_ARCH_IA32 |
| 118 | |