• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef ECMASCRIPT_COMPILER_TS_INLINE_LOWERING_H
17 #define ECMASCRIPT_COMPILER_TS_INLINE_LOWERING_H
18 
19 #include "ecmascript/compiler/argument_accessor.h"
20 #include "ecmascript/compiler/builtins/builtins_call_signature.h"
21 #include "ecmascript/compiler/bytecode_circuit_builder.h"
22 #include "ecmascript/compiler/bytecode_info_collector.h"
23 #include "ecmascript/compiler/circuit_builder-inl.h"
24 #include "ecmascript/compiler/pass_manager.h"
25 #include "ecmascript/jspandafile/js_pandafile.h"
26 
27 namespace panda::ecmascript::kungfu {
28 class CircuitRootScope {
29 public:
CircuitRootScope(Circuit * circuit)30     explicit CircuitRootScope(Circuit *circuit)
31         : circuit_(circuit), root_(circuit->GetRoot())
32     {
33     }
34 
~CircuitRootScope()35     ~CircuitRootScope()
36     {
37         circuit_->SetRoot(root_);
38     }
39 
40 private:
41     Circuit *circuit_ {nullptr};
42     GateRef root_ { 0 };
43 };
44 
45 class TSInlineLowering {
46 public:
47     static constexpr size_t MAX_INLINE_BYTECODE_COUNT = 10;
48     static constexpr size_t MAX_INLINE_CALL_ALLOWED = 5;
TSInlineLowering(Circuit * circuit,PassInfo * info,bool enableLog,const std::string & name)49     TSInlineLowering(Circuit *circuit, PassInfo *info, bool enableLog, const std::string& name)
50         : circuit_(circuit), acc_(circuit), builder_(circuit, info->GetCompilerConfig()),
51           tsManager_(info->GetTSManager()), info_(info), enableLog_(enableLog), methodName_(name) {}
52 
53     ~TSInlineLowering() = default;
54 
55     void RunTSInlineLowering();
56 
57 private:
IsLogEnabled()58     bool IsLogEnabled() const
59     {
60         return enableLog_;
61     }
62 
GetMethodName()63     const std::string& GetMethodName() const
64     {
65         return methodName_;
66     }
67 
68     void TryInline(GateRef gate);
69     void TryInline(GateRef gate, bool isCallThis);
70     bool FilterInlinedMethod(MethodLiteral* method, std::vector<const uint8_t*> pcOffsets);
71     void InlineCall(MethodInfo &methodInfo, MethodPcInfo &methodPCInfo, MethodLiteral* method);
72     CString GetRecordName(const JSPandaFile *jsPandaFile,
73         panda_file::File::EntityId methodIndex);
74     void ReplaceCallInput(GateRef gate, bool isCallThis);
75 
76     void ReplaceEntryGate(GateRef callGate);
77     void ReplaceReturnGate(GateRef callGate);
78 
79     void ReplaceHirAndDeleteState(GateRef gate, GateRef state, GateRef depend, GateRef value);
80 
81     GateRef MergeAllReturn(const std::vector<GateRef> &returnVector,
82         GateRef &state, GateRef &depend, size_t numOfIns);
83     bool CheckParameter(GateRef gate, bool isCallThis, MethodLiteral* method);
84 
85     void LowerToInlineCall(GateRef gate, const std::vector<GateRef> &args);
86 
87     Circuit *circuit_ {nullptr};
88     GateAccessor acc_;
89     CircuitBuilder builder_;
90     TSManager *tsManager_ {nullptr};
91     PassInfo *info_ {nullptr};
92     bool enableLog_ {false};
93     std::string methodName_;
94     size_t inlinedCall_ { 0 };
95 };
96 }  // panda::ecmascript::kungfu
97 #endif  // ECMASCRIPT_COMPILER_TS_INLINE_LOWERING_H