• 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_TASK_H
17 #define ECMASCRIPT_JIT_TASK_H
18 
19 #include "ecmascript/common.h"
20 #include "ecmascript/platform/mutex.h"
21 #include "ecmascript/ecma_vm.h"
22 
23 #include "ecmascript/jit/jit.h"
24 
25 namespace panda::ecmascript {
26 enum CompileState : uint8_t {
27     SUCCESS = 0,
28     FAIL,
29 };
30 
31 class JitTask {
32 public:
JitTask(EcmaVM * vm,Jit * jit,JSHandle<JSFunction> & jsFunction)33     JitTask(EcmaVM *vm, Jit *jit, JSHandle<JSFunction> &jsFunction) : vm_(vm), jit_(jit),
34         jsFunction_(jsFunction), compiler_(nullptr), state_(CompileState::SUCCESS) {
35             PersistentHandle();
36         }
37     ~JitTask();
38     void Optimize();
39     void Finalize();
40 
41     void InstallCode();
GetMachineCodeDesc()42     MachineCodeDesc *GetMachineCodeDesc()
43     {
44         return &codeDesc_;
45     }
46 
GetJsFunction()47     JSHandle<JSFunction> GetJsFunction() const
48     {
49         return jsFunction_;
50     }
51 
SetCompiler(void * compiler)52     void SetCompiler(void *compiler)
53     {
54         compiler_ = compiler;
55     }
56 
RequestInstallCode()57     void RequestInstallCode()
58     {
59         jit_->RequestInstallCode(this);
60     }
61 
IsCompileSuccess()62     bool IsCompileSuccess() const
63     {
64         return state_ == CompileState::SUCCESS;
65     }
66 
SetCompileFailed()67     void SetCompileFailed()
68     {
69         state_ = CompileState::FAIL;
70     }
71 
GetJit()72     Jit *GetJit()
73     {
74         return jit_;
75     }
76 
GetVM()77     EcmaVM *GetVM()
78     {
79         return vm_;
80     }
81 
GetMethodInfo()82     CString GetMethodInfo() const
83     {
84         return methodInfo_;
85     }
86 
SetMethodInfo(CString methodInfo)87     void SetMethodInfo(CString methodInfo)
88     {
89         methodInfo_ = methodInfo;
90     }
91 
92     class AsyncTask : public Task {
93     public:
AsyncTask(JitTask * jitTask,int32_t id)94         explicit AsyncTask(JitTask *jitTask, int32_t id) : Task(id), jitTask_(jitTask) { }
95         virtual ~AsyncTask() override = default;
96 
97         bool Run(uint32_t threadIndex) override;
98     private:
99         JitTask *jitTask_;
100     };
101 private:
102     void PersistentHandle();
103     void ReleasePersistentHandle();
SetJsFunction(JSHandle<JSFunction> & jsFunction)104     void SetJsFunction(JSHandle<JSFunction> &jsFunction)
105     {
106         jsFunction_ = jsFunction;
107     }
108 
109     EcmaVM *vm_;
110     Jit *jit_;
111     JSHandle<JSFunction> jsFunction_;
112     void *compiler_;
113     MachineCodeDesc codeDesc_;
114     CompileState state_;
115     CString methodInfo_;
116 };
117 }  // namespace panda::ecmascript
118 #endif  // ECMASCRIPT_JIT_TASK_H
119