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_ARGUMENT_ACCESSOR_H 17 #define ECMASCRIPT_COMPILER_ARGUMENT_ACCESSOR_H 18 19 #include "ecmascript/compiler/circuit.h" 20 #include "ecmascript/compiler/gate.h" 21 #include "ecmascript/compiler/gate_accessor.h" 22 #include "ecmascript/compiler/type_recorder.h" 23 #include "ecmascript/method.h" 24 25 namespace panda::ecmascript::kungfu { 26 enum class CommonArgIdx : uint8_t { 27 GLUE = 0, 28 ACTUAL_ARGC, 29 FUNC, 30 NEW_TARGET, 31 THIS_OBJECT, 32 NUM_OF_ARGS, 33 }; 34 35 enum class FastCallArgIdx : uint8_t { 36 GLUE = 0, 37 FUNC, 38 THIS_OBJECT, 39 NUM_OF_ARGS, 40 }; 41 42 enum class FrameArgIdx : uint8_t { 43 FUNC = 0, 44 NEW_TARGET, 45 THIS_OBJECT, 46 ACTUAL_ARGC, 47 NUM_OF_ARGS, 48 }; 49 50 class ArgumentAccessor { 51 public: 52 explicit ArgumentAccessor( 53 Circuit *circuit, const MethodLiteral *methodLiteral = nullptr) circuit_(circuit)54 : circuit_(circuit), 55 method_(methodLiteral), 56 argRoot_(circuit->GetArgRoot()), 57 args_(0), 58 frameArgs_{Circuit::NullGate()} 59 { 60 CollectArgs(); 61 } 62 ~ArgumentAccessor() = default; 63 64 void NewCommonArg(const CommonArgIdx argIndex, MachineType machineType, GateType gateType); 65 void NewArg(const size_t argIndex); 66 // method must be set 67 size_t GetActualNumArgs() const; 68 // method must be set 69 GateRef GetArgGate(const size_t currentVreg) const; 70 bool ArgGateNotExisted(const size_t currentVreg); ArgsAt(const size_t index)71 GateRef ArgsAt(const size_t index) const 72 { 73 return args_.at(index); 74 } ArgsCount()75 size_t ArgsCount() const 76 { 77 return args_.size(); 78 } 79 void FillArgsGateType(const TypeRecorder *typeRecorder); 80 void CollectArgs(); GetFixArgsNum()81 static size_t GetFixArgsNum() 82 { 83 return static_cast<size_t>(CommonArgIdx::NUM_OF_ARGS) - static_cast<size_t>(CommonArgIdx::FUNC); 84 } 85 GetExtraArgsNum()86 static size_t GetExtraArgsNum() 87 { 88 return static_cast<size_t>(CommonArgIdx::ACTUAL_ARGC) - static_cast<size_t>(CommonArgIdx::GLUE); 89 } 90 GetFrameArgs()91 GateRef GetFrameArgs() const 92 { 93 return frameArgs_; 94 } 95 SetFrameArgs(GateRef frameArgs)96 void SetFrameArgs(GateRef frameArgs) 97 { 98 frameArgs_ = frameArgs; 99 } 100 GateRef GetFrameArgsIn(GateRef gate, FrameArgIdx idx); 101 102 private: 103 // Disables using this interface during lowering, only allows it to be used during building graph. 104 GateRef GetCommonArgGate(const CommonArgIdx arg) const; 105 size_t GetFunctionArgIndex(const size_t currentVreg, const bool haveFunc, 106 const bool haveNewTarget, const bool haveThis) const; 107 GateRef GetTypedArgGate(const size_t argIndex) const; 108 109 Circuit *circuit_ {nullptr}; 110 const MethodLiteral *method_ {nullptr}; 111 GateRef argRoot_; 112 std::vector<GateRef> args_; 113 GateRef frameArgs_; 114 115 friend class BytecodeCircuitBuilder; 116 friend class AsyncFunctionLowering; 117 friend class InitializationAnalysis; 118 }; 119 } 120 #endif // ECMASCRIPT_COMPILER_ARGUMENT_ACCESSOR_H 121