Flutter Engine
The Flutter Engine
stub_code_ia32_test.cc
Go to the documentation of this file.
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"
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
20namespace dart {
21
22static Function* CreateFunction(const char* name) {
23 const String& class_name =
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 =
31 const FunctionType& signature = FunctionType::ZoneHandle(FunctionType::New());
33 signature, function_name, UntaggedFunction::kRegularFunction, true, false,
34 false, false, false, owner_class, TokenPosition::kMinSource));
35 return &function;
36}
37
38// Test calls to stub code which calls into the runtime.
39static void GenerateCallToCallRuntimeStub(compiler::Assembler* assembler,
40 int length) {
41 const int argc = 2;
42 const Smi& smi_length = Smi::ZoneHandle(Smi::New(length));
43 __ enter(compiler::Immediate(0));
44 __ PushObject(Object::null_object()); // Push Null object for return value.
45 __ PushObject(smi_length); // Push argument 1: length.
46 __ PushObject(Object::null_object()); // Push argument 2: type arguments.
47 ASSERT(kAllocateArrayRuntimeEntry.argument_count() == argc);
48 __ CallRuntime(kAllocateArrayRuntimeEntry, argc);
49 __ AddImmediate(ESP, compiler::Immediate(argc * kWordSize));
50 __ popl(EAX); // Pop return value from return slot.
51 __ leave();
52 __ ret();
53}
54
55ISOLATE_UNIT_TEST_CASE(CallRuntimeStubCode) {
56 extern const Function& RegisterFakeFunction(const char* name,
57 const Code& code);
58 const int length = 10;
59 const char* kName = "Test_CallRuntimeStubCode";
60 compiler::Assembler assembler(nullptr);
61 GenerateCallToCallRuntimeStub(&assembler, length);
62 SafepointWriteRwLocker ml(thread, thread->isolate_group()->program_lock());
64 *CreateFunction("Test_CallRuntimeStubCode"), nullptr, &assembler,
66 const Function& function = RegisterFakeFunction(kName, code);
67 Array& result = Array::Handle();
68 result ^= DartEntry::InvokeFunction(function, Object::empty_array());
69 EXPECT_EQ(length, result.Length());
70}
71
72// Test calls to stub code which calls into a leaf runtime entry.
73static void GenerateCallToCallLeafRuntimeStub(compiler::Assembler* assembler,
74 const char* str_value,
75 intptr_t lhs_index_value,
76 intptr_t rhs_index_value,
77 intptr_t length_value) {
78 const String& str = String::ZoneHandle(String::New(str_value, Heap::kOld));
79 const Smi& lhs_index = Smi::ZoneHandle(Smi::New(lhs_index_value));
80 const Smi& rhs_index = Smi::ZoneHandle(Smi::New(rhs_index_value));
81 const Smi& length = Smi::ZoneHandle(Smi::New(length_value));
82 __ enter(compiler::Immediate(0));
83 {
84 compiler::LeafRuntimeScope rt(assembler,
85 /*frame_size=*/4 * kWordSize,
86 /*preserve_registers=*/false);
87 __ LoadObject(EAX, str);
88 __ movl(compiler::Address(ESP, 0), EAX); // Push argument 1.
89 __ LoadObject(EAX, lhs_index);
90 __ movl(compiler::Address(ESP, kWordSize), EAX); // Push argument 2.
91 __ LoadObject(EAX, rhs_index);
92 __ movl(compiler::Address(ESP, 2 * kWordSize), EAX); // Push argument 3.
93 __ LoadObject(EAX, length);
94 __ movl(compiler::Address(ESP, 3 * kWordSize), EAX); // Push argument 4.
95 rt.Call(kCaseInsensitiveCompareUCS2RuntimeEntry, 4);
96 }
97 __ leave();
98 __ ret(); // Return value is in EAX.
99}
100
101ISOLATE_UNIT_TEST_CASE(CallLeafRuntimeStubCode) {
102 extern const Function& RegisterFakeFunction(const char* name,
103 const Code& code);
104 const char* str_value = "abAB";
105 intptr_t lhs_index_value = 0;
106 intptr_t rhs_index_value = 2;
107 intptr_t length_value = 2;
108 const char* kName = "Test_CallLeafRuntimeStubCode";
109 compiler::Assembler assembler(nullptr);
110 GenerateCallToCallLeafRuntimeStub(&assembler, str_value, lhs_index_value,
111 rhs_index_value, length_value);
112 SafepointWriteRwLocker ml(thread, thread->isolate_group()->program_lock());
114 *CreateFunction("Test_CallLeafRuntimeStubCode"), nullptr, &assembler,
116 if (FLAG_disassemble) {
117 OS::PrintErr("Disassemble:\n");
118 code.Disassemble();
119 }
120 const Function& function = RegisterFakeFunction(kName, code);
121 Instance& result = Instance::Handle();
122 result ^= DartEntry::InvokeFunction(function, Object::empty_array());
123 EXPECT_EQ(Bool::True().ptr(), result.ptr());
124}
125
126} // namespace dart
127
128#endif // defined TARGET_ARCH_IA32
#define __
static const Bool & True()
Definition: object.h:10797
static ClassPtr New(IsolateGroup *isolate_group, bool register_class=true)
Definition: object.cc:3053
static CodePtr FinalizeCodeAndNotify(const Function &function, FlowGraphCompiler *compiler, compiler::Assembler *assembler, PoolAttachment pool_attachment, bool optimized=false, CodeStatistics *stats=nullptr)
Definition: object.cc:17983
static ObjectPtr InvokeFunction(const Function &function, const Array &arguments)
Definition: dart_entry.cc:31
static FunctionTypePtr New(intptr_t num_parent_type_arguments=0, Nullability nullability=Nullability::kNonNullable, Heap::Space space=Heap::kOld)
Definition: object.cc:11631
static FunctionPtr New(const FunctionType &signature, const String &name, UntaggedFunction::Kind kind, bool is_static, bool is_const, bool is_abstract, bool is_external, bool is_native, const Object &owner, TokenPosition token_pos, Heap::Space space=Heap::kOld)
Definition: object.cc:10243
@ kOld
Definition: heap.h:39
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static Object & Handle()
Definition: object.h:407
static Object & ZoneHandle()
Definition: object.h:419
static SmiPtr New(intptr_t value)
Definition: object.h:10006
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition: object.cc:23698
static StringPtr New(Thread *thread, const char *cstr)
Definition: symbols.h:723
static Thread * Current()
Definition: thread.h:362
static const TokenPosition kMinSource
#define ASSERT(E)
GAsyncResult * result
Dart_NativeFunction function
Definition: fuchsia.cc:51
size_t length
Definition: dart_vm.cc:33
const char *const name
const char *const class_name
const Function & RegisterFakeFunction(const char *name, const Code &code)
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)
constexpr intptr_t kWordSize
Definition: globals.h:509
static FunctionPtr CreateFunction(const char *name)
const char *const function_name