1 /**
2 * Copyright (c) 2021-2022 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 panda::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_ = panda::MTManagedThread::GetCurrent();
38 thread_->ManagedCodeBegin();
39 }
40 Method *GetMethod(Class *klass, size_t num);
41 Class *GetClass();
42 void CompileMethods();
43
~OsrCodeTest()44 ~OsrCodeTest()
45 {
46 thread_->ManagedCodeEnd();
47 Runtime::Destroy();
48 }
49
50 protected:
51 panda::MTManagedThread *thread_;
52 };
53
GetMethod(Class * klass,size_t num)54 Method *OsrCodeTest::GetMethod(Class *klass, size_t num)
55 {
56 PandaStringStream ss;
57 ss << "f" << num;
58 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8(ss.str().c_str()));
59 return method;
60 }
61
GetClass()62 Class *OsrCodeTest::GetClass()
63 {
64 pandasm::Parser p;
65
66 PandaStringStream ss;
67
68 for (size_t i = 0; i < OsrCodeTest::METHOD_COUNT; i++) {
69 ss << ".function void f" << i << "() {" << std::endl;
70 ss << " return.void" << std::endl;
71 ss << "}" << std::endl;
72 }
73
74 auto source = ss.str();
75 auto res = p.Parse(source.c_str());
76 auto pf = pandasm::AsmEmitter::Emit(res.Value());
77
78 ClassLinker *class_linker = Runtime::GetCurrent()->GetClassLinker();
79 class_linker->AddPandaFile(std::move(pf));
80 auto *extension = class_linker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
81
82 PandaString descriptor;
83
84 return extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &descriptor));
85 }
86
CompileMethods()87 void OsrCodeTest::CompileMethods()
88 {
89 auto *klass = GetClass();
90 ASSERT_NE(klass, nullptr);
91
92 auto *compiler = static_cast<Compiler *>(PandaVM::GetCurrent()->GetCompiler());
93
94 for (size_t i = 0; i < OsrCodeTest::METHOD_COUNT; i++) {
95 Method *method = GetMethod(klass, i);
96 ASSERT_NE(method, nullptr);
97 compiler->SetOsrCode(method, static_cast<void *>(method));
98 }
99
100 for (size_t i = 0; i < OsrCodeTest::METHOD_COUNT; i++) {
101 Method *method = GetMethod(klass, i);
102 ASSERT_NE(method, nullptr);
103 ASSERT_EQ(compiler->GetOsrCode(method), static_cast<const void *>(method));
104 }
105 }
106
TEST_F(OsrCodeTest,GetOsrCode)107 TEST_F(OsrCodeTest, GetOsrCode)
108 {
109 CompileMethods();
110 }
111
112 } // namespace panda::test
113