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