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_BYTECODE_LOWERING_H 17 #define ECMASCRIPT_COMPILER_NTYPE_BYTECODE_LOWERING_H 18 19 #include "ecmascript/compiler/argument_accessor.h" 20 #include "ecmascript/compiler/pass_manager.h" 21 22 namespace panda::ecmascript::kungfu { 23 class NTypeBytecodeLowering { 24 public: NTypeBytecodeLowering(Circuit * circuit,PassContext * ctx,bool enableLog,bool enableLazyDeopt,const std::string & name,const CString & recordName)25 NTypeBytecodeLowering(Circuit *circuit, PassContext *ctx, 26 bool enableLog, bool enableLazyDeopt, const std::string& name, const CString& recordName) 27 : circuit_(circuit), 28 acc_(circuit), 29 builder_(circuit, ctx->GetCompilerConfig()), 30 ptManager_(ctx->GetPTManager()), 31 jsPandaFile_(ctx->GetJSPandaFile()), 32 enableLog_(enableLog), 33 enableLazyDeopt_(enableLazyDeopt), 34 profiling_(ctx->GetCompilerConfig()->IsProfiling()), 35 traceBc_(ctx->GetCompilerConfig()->IsTraceBC()), 36 methodName_(name), 37 recordName_(recordName), 38 glue_(acc_.GetGlueFromArgList()), 39 argAcc_(circuit->GetArgumentAccessor()), 40 compilationEnv_(ctx->GetCompilationEnv()) {} 41 42 ~NTypeBytecodeLowering() = default; 43 44 void RunNTypeBytecodeLowering(); 45 private: 46 void Lower(GateRef gate); 47 void LowerNTypedCreateEmptyArray(GateRef gate); 48 void LowerNTypedCreateArrayWithBuffer(GateRef gate); 49 void LowerNTypedCopyRestArgs(GateRef gate); 50 void LowerNTypedGetUnmappedArgs(GateRef gate); 51 void LowerNTypedStownByIndex(GateRef gate); 52 void LowerLdLexVar(GateRef gate); 53 void LowerStLexVar(GateRef gate); 54 void LowerThrowUndefinedIfHoleWithName(GateRef gate); 55 void LowerLdLocalMoudleVar(GateRef gate); 56 void LowerLdExternalMoudleVar(GateRef gate); 57 void LowerStModuleVar(GateRef gate); 58 void LowerNTypedStOwnByName(GateRef gate); 59 void LowerThrowIfSuperNotCorrectCall(GateRef gate); 60 void LowerThrowIfNotObject(GateRef gate); 61 void ReplaceGateWithPendingException(GateRef gate, GateRef state, GateRef depend, GateRef value); 62 IsLogEnabled()63 bool IsLogEnabled() const 64 { 65 return enableLog_; 66 } 67 IsProfiling()68 bool IsProfiling() const 69 { 70 return profiling_; 71 } 72 IsTraceBC()73 bool IsTraceBC() const 74 { 75 return traceBc_; 76 } 77 GetMethodName()78 const std::string& GetMethodName() const 79 { 80 return methodName_; 81 } 82 GetConstantpoolValue(uint32_t cpId)83 JSTaggedValue GetConstantpoolValue(uint32_t cpId) 84 { 85 return compilationEnv_->FindConstpool(jsPandaFile_, cpId); 86 } 87 GetArrayLiteralValue(uint32_t cpId,uint32_t cpIdx)88 JSTaggedValue GetArrayLiteralValue(uint32_t cpId, uint32_t cpIdx) 89 { 90 JSTaggedValue cp = GetConstantpoolValue(cpId); 91 JSTaggedValue unsharedCp = compilationEnv_->FindOrCreateUnsharedConstpool(cp); 92 return compilationEnv_->GetArrayLiteralFromCache(unsharedCp, cpIdx, recordName_); 93 } 94 95 enum SuperCorrectCallCheck : uint8_t { 96 CALL_SUPER_BEFORE_THIS_CHECK = 0, 97 FORBIDDEN_SUPER_REBIND_THIS_CHECK, 98 }; 99 100 void AddProfiling(GateRef gate); 101 Circuit *circuit_ {nullptr}; 102 GateAccessor acc_; 103 CircuitBuilder builder_; 104 PGOTypeManager *ptManager_ {nullptr}; 105 const JSPandaFile *jsPandaFile_ {nullptr}; 106 bool enableLog_ {false}; 107 bool enableLazyDeopt_; 108 bool profiling_ {false}; 109 bool traceBc_ {false}; 110 std::string methodName_; 111 const CString recordName_; 112 GateRef glue_ {Circuit::NullGate()}; 113 ArgumentAccessor *argAcc_; 114 const CompilationEnv *compilationEnv_ {nullptr}; 115 }; 116 } // panda::ecmascript::kungfu 117 #endif // ECMASCRIPT_COMPILER_NTYPE_BYTECODE_LOWERING_H 118