• 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_COMPILER_NTYPE_HCR_LOWERING_H
17 #define ECMASCRIPT_COMPILER_NTYPE_HCR_LOWERING_H
18 
19 #include "ecmascript/compiler/combined_pass_visitor.h"
20 #include "ecmascript/compiler/pass_manager.h"
21 #include "ecmascript/jspandafile/program_object.h"
22 
23 namespace panda::ecmascript::kungfu {
24 class NTypeHCRLowering : public PassVisitor {
25 public:
NTypeHCRLowering(Circuit * circuit,RPOVisitor * visitor,PassContext * ctx,const CString & recordName,const MethodLiteral * methodLiteral,bool enablePgoSpace,Chunk * chunk)26     NTypeHCRLowering(Circuit *circuit, RPOVisitor *visitor, PassContext *ctx, const CString &recordName,
27                      const MethodLiteral *methodLiteral, bool enablePgoSpace, Chunk* chunk)
28         : PassVisitor(circuit, chunk, visitor),
29           circuit_(circuit),
30           acc_(circuit),
31           compilationEnv_(ctx->GetCompilationEnv()),
32           builder_(circuit, ctx->GetCompilerConfig()),
33           dependEntry_(circuit->GetDependRoot()),
34           jsPandaFile_(ctx->GetJSPandaFile()),
35           recordName_(recordName),
36           methodLiteral_(methodLiteral),
37           profiling_(ctx->GetCompilerConfig()->IsProfiling()),
38           enablePgoSpace_(enablePgoSpace),
39           traceBc_(ctx->GetCompilerConfig()->IsTraceBC()),
40           glue_(acc_.GetGlueFromArgList()) {}
41 
42     ~NTypeHCRLowering() = default;
43     GateRef VisitGate(GateRef gate) override;
44 private:
45     static constexpr int MAX_TAGGED_ARRAY_LENGTH = 50;
46     void Lower(GateRef gate);
47     void LowerCreateArray(GateRef gate, GateRef glue);
48     void LowerCreateArrayWithBuffer(GateRef gate, GateRef glue);
49     void LowerCreateArguments(GateRef gate, GateRef glue);
50     void LowerCreateEmptyArray(GateRef gate, GateRef glue);
51     void LowerCreateArrayWithOwn(GateRef gate, GateRef glue);
52     void LowerStoreModuleVar(GateRef gate, GateRef glue);
53     void LowerLdLocalModuleVar(GateRef gate, GateRef glue);
54     void LowerLdExternalModuleVar(GateRef gate);
55 
56     GateRef LoadFromConstPool(GateRef glue, GateRef unsharedConstPool, size_t index, size_t valVecType);
57     GateRef NewActualArgv(GateRef gate, GateRef glue);
58     GateRef NewJSArrayLiteral(
59         GateRef glue, GateRef gate, GateRef elements, GateRef length, uint32_t hintLength = 0);
60     GateRef NewTaggedArray(size_t length, GateRef glue);
61     GateRef CreateElementsWithLength(GateRef gate, GateRef glue, size_t arrayLength);
62     GateRef LowerCallRuntime(GateRef glue, GateRef hirGate, int index, const std::vector<GateRef> &args,
63                              bool useLabel = false);
64     void ReplaceGateWithPendingException(GateRef gate, GateRef state, GateRef depend, GateRef value);
65 
GetFrameState(GateRef gate)66     GateRef GetFrameState(GateRef gate) const
67     {
68         return acc_.GetFrameState(gate);
69     }
70 
GetConstantpoolValue(uint32_t cpId)71     JSTaggedValue GetConstantpoolValue(uint32_t cpId)
72     {
73         return compilationEnv_->FindConstpool(jsPandaFile_, cpId);
74     }
75 
GetArrayLiteralValue(uint32_t cpId,uint32_t cpIdx)76     JSTaggedValue GetArrayLiteralValue(uint32_t cpId, uint32_t cpIdx)
77     {
78         JSTaggedValue cp = GetConstantpoolValue(cpId);
79         JSTaggedValue unsharedCp = compilationEnv_->FindOrCreateUnsharedConstpool(cp);
80         return compilationEnv_->GetArrayLiteralFromCache(unsharedCp, cpIdx, recordName_);
81     }
82 
83     Circuit *circuit_ {nullptr};
84     GateAccessor acc_;
85     CompilationEnv *compilationEnv_ {nullptr};
86     CircuitBuilder builder_;
87     GateRef dependEntry_;
88     const JSPandaFile *jsPandaFile_ {nullptr};
89     const CString &recordName_;
90     const MethodLiteral *methodLiteral_ {nullptr};
91     panda_file::File::EntityId methodId_ {0};
92     bool profiling_ {false};
93     bool enablePgoSpace_ {false};
94     bool traceBc_ {false};
95     GateRef glue_ {Circuit::NullGate()};
96 };
97 }  // panda::ecmascript::kungfu
98 #endif  // ECMASCRIPT_COMPILER_NTYPE_HCR_LOWERING_H
99