1 /*
2 * Copyright (c) 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 "ecmascript/jit/jit_resources.h"
17 #include "ecmascript/jit/jit_task.h"
18 #include "ecmascript/ohos/jit_tools.h"
19
20 namespace panda::ecmascript {
21
Destroy()22 void JitResources::Destroy()
23 {
24 JitTaskpool::GetCurrentTaskpool()->Destroy();
25 if (libHandle_ != nullptr) {
26 CloseLib(libHandle_);
27 libHandle_ = nullptr;
28 initJitCompiler_ = nullptr;
29 jitCompile_ = nullptr;
30 jitFinalize_ = nullptr;
31 createJitCompilerTask_ = nullptr;
32 deleteJitCompilerTask_ = nullptr;
33 }
34 }
35
InitJitEnv(const JSRuntimeOptions & options)36 bool JitResources::InitJitEnv(const JSRuntimeOptions &options)
37 {
38 jsRuntimeOptions_ = options;
39 InitCompiler();
40 InitJitTaskpool();
41 return true;
42 }
43
ResolveLib()44 bool JitResources::ResolveLib()
45 {
46 #if defined(OHOS_UNIT_TEST)
47 #else
48 static const std::string CREATEJITCOMPILETASK = "CreateJitCompilerTask";
49 static const std::string JITCOMPILEINIT = "InitJitCompiler";
50 static const std::string JITCOMPILE = "JitCompile";
51 static const std::string JITFINALIZE = "JitFinalize";
52 static const std::string DELETEJITCOMPILERTASK = "DeleteJitCompilerTask";
53 static const std::string LIBARK_JSOPTIMIZER = "libark_jsoptimizer.so";
54
55 libHandle_ = LoadLib(LIBARK_JSOPTIMIZER);
56 if (libHandle_ == nullptr) {
57 char *error = LoadLibError();
58 LOG_JIT(ERROR) << "jit dlopen libark_jsoptimizer.so failed, as:" <<
59 ((error == nullptr) ? "unknown error" : error);
60 return false;
61 }
62
63 initJitCompiler_ = reinterpret_cast<InitJitCompilerFuncType>(FindSymbol(libHandle_, JITCOMPILEINIT.c_str()));
64 if (initJitCompiler_ == nullptr) {
65 LOG_JIT(ERROR) << "jit can't find symbol initJitCompiler";
66 return false;
67 }
68 jitCompile_ = reinterpret_cast<JitCompileFuncType>(FindSymbol(libHandle_, JITCOMPILE.c_str()));
69 if (jitCompile_ == nullptr) {
70 LOG_JIT(ERROR) << "jit can't find symbol jitCompile";
71 return false;
72 }
73
74 jitFinalize_ = reinterpret_cast<JitFinalizeFuncType>(FindSymbol(libHandle_, JITFINALIZE.c_str()));
75 if (jitFinalize_ == nullptr) {
76 LOG_JIT(ERROR) << "jit can't find symbol jitFinalize";
77 return false;
78 }
79
80 createJitCompilerTask_ = reinterpret_cast<CreateJitCompilerTaskFuncType>(FindSymbol(libHandle_,
81 CREATEJITCOMPILETASK.c_str()));
82 if (createJitCompilerTask_ == nullptr) {
83 LOG_JIT(ERROR) << "jit can't find symbol createJitCompilertask";
84 return false;
85 }
86
87 deleteJitCompilerTask_ = reinterpret_cast<DeleteJitCompilerTaskFuncType>(FindSymbol(libHandle_,
88 DELETEJITCOMPILERTASK.c_str()));
89 if (deleteJitCompilerTask_ == nullptr) {
90 LOG_JIT(ERROR) << "jit can't find symbol deleteJitCompile";
91 return false;
92 }
93 #endif
94 libResolved_ = true;
95 return true;
96 }
97
InitCompiler()98 void JitResources::InitCompiler()
99 {
100 if (initJitCompiler_ == nullptr) {
101 return;
102 }
103 initJitCompiler_(jsRuntimeOptions_);
104 }
105
InitJitTaskpool()106 void JitResources::InitJitTaskpool()
107 {
108 bool enableCodeSign = !ohos::JitTools::GetCodeSignDisable(jsRuntimeOptions_.GetDisableCodeSign());
109 JitTaskpool::GetCurrentTaskpool()->Initialize(enableCodeSign);
110 }
111 } // namespace panda::ecmascript
112