1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include <vector>
19
20 #include "assembly-parser.h"
21 #include "runtime/include/method.h"
22 #include "runtime/include/runtime.h"
23
24 #include "runtime/compiler.h"
25
26 namespace ark::test {
27
28 class OsrCodeTest : public testing::Test {
29 public:
30 static const size_t METHOD_COUNT = 32;
OsrCodeTest()31 OsrCodeTest()
32 {
33 RuntimeOptions options;
34 options.SetShouldLoadBootPandaFiles(false);
35 options.SetShouldInitializeIntrinsics(false);
36 Runtime::Create(options);
37 thread_ = ark::MTManagedThread::GetCurrent();
38 thread_->ManagedCodeBegin();
39 }
40 Method *GetMethod(Class *klass, size_t num);
41 Class *GetClass();
42 void CompileMethods();
43
~OsrCodeTest()44 ~OsrCodeTest() override
45 {
46 thread_->ManagedCodeEnd();
47 Runtime::Destroy();
48 }
49
50 NO_COPY_SEMANTIC(OsrCodeTest);
51 NO_MOVE_SEMANTIC(OsrCodeTest);
52
53 private:
54 ark::MTManagedThread *thread_;
55 };
56
GetMethod(Class * klass,size_t num)57 Method *OsrCodeTest::GetMethod(Class *klass, size_t num)
58 {
59 PandaStringStream ss;
60 ss << "f" << num;
61 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8(ss.str().c_str()));
62 return method;
63 }
64
GetClass()65 Class *OsrCodeTest::GetClass()
66 {
67 pandasm::Parser p;
68
69 PandaStringStream ss;
70
71 for (size_t i = 0; i < OsrCodeTest::METHOD_COUNT; i++) {
72 ss << ".function void f" << i << "() {" << std::endl;
73 ss << " return.void" << std::endl;
74 ss << "}" << std::endl;
75 }
76
77 auto source = ss.str();
78 // NOLINTNEXTLINE(readability-redundant-string-cstr)
79 auto res = p.Parse(source.c_str());
80 auto pf = pandasm::AsmEmitter::Emit(res.Value());
81
82 ClassLinker *classLinker = Runtime::GetCurrent()->GetClassLinker();
83 classLinker->AddPandaFile(std::move(pf));
84 auto *extension = classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
85
86 PandaString descriptor;
87
88 return extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &descriptor));
89 }
90
CompileMethods()91 void OsrCodeTest::CompileMethods()
92 {
93 auto *klass = GetClass();
94 ASSERT_NE(klass, nullptr);
95
96 auto *compiler = static_cast<Compiler *>(PandaVM::GetCurrent()->GetCompiler());
97
98 for (size_t i = 0; i < OsrCodeTest::METHOD_COUNT; i++) {
99 Method *method = GetMethod(klass, i);
100 ASSERT_NE(method, nullptr);
101 compiler->SetOsrCode(method, static_cast<void *>(method));
102 }
103
104 for (size_t i = 0; i < OsrCodeTest::METHOD_COUNT; i++) {
105 Method *method = GetMethod(klass, i);
106 ASSERT_NE(method, nullptr);
107 ASSERT_EQ(compiler->GetOsrCode(method), static_cast<const void *>(method));
108 }
109 }
110
TEST_F(OsrCodeTest,GetOsrCode)111 TEST_F(OsrCodeTest, GetOsrCode)
112 {
113 CompileMethods();
114 }
115
116 } // namespace ark::test
117