• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef ECMASCRIPT_JIT_H
17 #define ECMASCRIPT_JIT_H
18 
19 #include "ecmascript/common.h"
20 #include "ecmascript/platform/mutex.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/mem/clock_scope.h"
23 #include "ecmascript/compiler/compiler_log.h"
24 
25 namespace panda::ecmascript {
26 class JitTask;
27 enum JitCompileMode {
28     SYNC = 0,
29     ASYNC
30 };
31 class Jit {
32 public:
Jit(EcmaVM * vm)33     Jit(EcmaVM *vm) : vm_(vm), initialized_(false), jitCompile_(nullptr), jitFinalize_(nullptr),
34         createJitCompiler_(nullptr), deleteJitCompile_(nullptr), libHandle_(nullptr) {};
35     ~Jit();
36     void Initialize();
37 
38     static void Compile(EcmaVM *vm, JSHandle<JSFunction> &jsFunction, JitCompileMode mode = SYNC);
39     bool JitCompile(void *compiler, JitTask *jitTask);
40     bool JitFinalize(void *compiler, JitTask *jitTask);
IsInitialized()41     bool IsInitialized() const
42     {
43         return initialized_;
44     }
45 
46     void DeleteJitCompile(void *compiler);
47 
48     void RequestInstallCode(JitTask *jitTask);
49     void InstallTasks();
50     void InstallTasksWithoutClearFlag();
51     bool IsCompiling(JSHandle<JSFunction> &jsFunction);
52     void AddCompilingTask(JitTask *jitTask);
53     void RemoveCompilingTask(JitTask *jitTask);
54 
55     JitTask *GetAsyncCompileTask();
56     void AddAsyncCompileTask(JitTask *jitTask);
57     void RemoveAsyncCompileTask(JitTask *jitTask);
58 
59     NO_COPY_SEMANTIC(Jit);
60     NO_MOVE_SEMANTIC(Jit);
61 
62     class Scope : public ClockScope {
63     public:
Scope(CString message)64         Scope(CString message) : message_(message) {}
65         ~Scope();
66     private:
67         CString message_;
68     };
69 
70 private:
71     bool SupportJIT(Method *method);
72     EcmaVM *vm_;
73     bool initialized_;
74     bool(*jitCompile_)(void*, JitTask*);
75     bool(*jitFinalize_)(void*, JitTask*);
76     void*(*createJitCompiler_)(EcmaVM*, JitTask*);
77     void(*deleteJitCompile_)(void*);
78     void *libHandle_;
79 
80     std::deque<JitTask*> compilingJitTasks_;
81     std::deque<JitTask*> installJitTasks_;
82     static std::deque<JitTask*> asyncCompileJitTasks_;
83     Mutex installJitTasksDequeMtx_;
84     static Mutex asyncCompileJitTasksMtx_;
85 };
86 }  // namespace panda::ecmascript
87 #endif  // ECMASCRIPT_JIT_H
88