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