Flutter Engine
The Flutter Engine
stub_code_riscv_test.cc
Go to the documentation of this file.
1// Copyright (c) 2021, 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_RISCV32) || defined(TARGET_ARCH_RISCV64)
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::kNoSource));
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 __ EnterDartFrame(0);
44 __ PushObject(Object::null_object()); // Push Null obj 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 __ addi(SP, SP, argc * kWordSize);
50 __ PopRegister(A0); // Pop return value from return slot.
51 __ LeaveDartFrame();
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::ObjectPoolBuilder object_pool_builder;
61 compiler::Assembler assembler(&object_pool_builder);
62 GenerateCallToCallRuntimeStub(&assembler, length);
63 SafepointWriteRwLocker ml(thread, thread->isolate_group()->program_lock());
65 *CreateFunction("Test_CallRuntimeStubCode"), nullptr, &assembler,
67 const Function& function = RegisterFakeFunction(kName, code);
68 Array& result = Array::Handle();
69 result ^= DartEntry::InvokeFunction(function, Object::empty_array());
70 EXPECT_EQ(length, result.Length());
71}
72
73// Test calls to stub code which calls into a leaf runtime entry.
74static void GenerateCallToCallLeafRuntimeStub(compiler::Assembler* assembler,
75 const char* str_value,
76 intptr_t lhs_index_value,
77 intptr_t rhs_index_value,
78 intptr_t length_value) {
79 const String& str = String::ZoneHandle(String::New(str_value, Heap::kOld));
80 const Smi& lhs_index = Smi::ZoneHandle(Smi::New(lhs_index_value));
81 const Smi& rhs_index = Smi::ZoneHandle(Smi::New(rhs_index_value));
82 const Smi& length = Smi::ZoneHandle(Smi::New(length_value));
83 __ EnterDartFrame(0);
84 {
85 compiler::LeafRuntimeScope rt(assembler,
86 /*frame_size=*/0,
87 /*preserve_registers=*/false);
88 __ LoadObject(A0, str);
89 __ LoadObject(A1, lhs_index);
90 __ LoadObject(A2, rhs_index);
91 __ LoadObject(A3, length);
92 rt.Call(kCaseInsensitiveCompareUCS2RuntimeEntry, 4);
93 }
94 __ LeaveDartFrame();
95 __ ret(); // Return value is in A0.
96}
97
98ISOLATE_UNIT_TEST_CASE(CallLeafRuntimeStubCode) {
99 extern const Function& RegisterFakeFunction(const char* name,
100 const Code& code);
101 const char* str_value = "abAB";
102 intptr_t lhs_index_value = 0;
103 intptr_t rhs_index_value = 2;
104 intptr_t length_value = 2;
105 const char* kName = "Test_CallLeafRuntimeStubCode";
106 compiler::ObjectPoolBuilder object_pool_builder;
107 compiler::Assembler assembler(&object_pool_builder);
108 GenerateCallToCallLeafRuntimeStub(&assembler, str_value, lhs_index_value,
109 rhs_index_value, length_value);
110 SafepointWriteRwLocker ml(thread, thread->isolate_group()->program_lock());
112 *CreateFunction("Test_CallLeafRuntimeStubCode"), nullptr, &assembler,
114 if (FLAG_disassemble) {
115 OS::PrintErr("Disassemble:\n");
116 code.Disassemble();
117 }
118 const Function& function = RegisterFakeFunction(kName, code);
119 Instance& result = Instance::Handle();
120 result ^= DartEntry::InvokeFunction(function, Object::empty_array());
121 EXPECT_EQ(Bool::True().ptr(), result.ptr());
122}
123
124} // namespace dart
125
126#endif // defined TARGET_ARCH_RISCV
#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
#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