14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/compiler/argument_accessor.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu { 194514f5e3Sopenharmony_civoid ArgumentAccessor::NewCommonArg(const CommonArgIdx argIndex, MachineType machineType, GateType gateType) 204514f5e3Sopenharmony_ci{ 214514f5e3Sopenharmony_ci circuit_->NewArg(machineType, static_cast<size_t>(argIndex), gateType, argRoot_); 224514f5e3Sopenharmony_ci} 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_civoid ArgumentAccessor::NewArg(const size_t argIndex) 254514f5e3Sopenharmony_ci{ 264514f5e3Sopenharmony_ci circuit_->NewArg(MachineType::I64, argIndex, GateType::TaggedValue(), argRoot_); 274514f5e3Sopenharmony_ci} 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ci// method must be set 304514f5e3Sopenharmony_cisize_t ArgumentAccessor::GetActualNumArgs() const 314514f5e3Sopenharmony_ci{ 324514f5e3Sopenharmony_ci ASSERT(method_ != nullptr); 334514f5e3Sopenharmony_ci auto numArgs = method_->GetNumArgsWithCallField(); 344514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::NUM_OF_ARGS) + numArgs; 354514f5e3Sopenharmony_ci} 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ci// method must be set 384514f5e3Sopenharmony_ciGateRef ArgumentAccessor::GetArgGate(const size_t currentVreg) const 394514f5e3Sopenharmony_ci{ 404514f5e3Sopenharmony_ci ASSERT(method_ != nullptr); 414514f5e3Sopenharmony_ci const size_t offsetArgs = method_->GetNumVregsWithCallField(); 424514f5e3Sopenharmony_ci ASSERT(currentVreg >= offsetArgs && currentVreg < offsetArgs + method_->GetNumArgs()); 434514f5e3Sopenharmony_ci auto reg = currentVreg - offsetArgs; 444514f5e3Sopenharmony_ci auto haveFunc = method_->HaveFuncWithCallField(); 454514f5e3Sopenharmony_ci auto haveNewTarget = method_->HaveNewTargetWithCallField(); 464514f5e3Sopenharmony_ci auto haveThis = method_->HaveThisWithCallField(); 474514f5e3Sopenharmony_ci auto index = GetFunctionArgIndex(reg, haveFunc, haveNewTarget, haveThis); 484514f5e3Sopenharmony_ci return args_.at(index); 494514f5e3Sopenharmony_ci} 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_cibool ArgumentAccessor::ArgGateNotExisted(const size_t currentVreg) 524514f5e3Sopenharmony_ci{ 534514f5e3Sopenharmony_ci const size_t offsetArgs = method_->GetNumVregsWithCallField(); 544514f5e3Sopenharmony_ci if (currentVreg < offsetArgs || currentVreg >= offsetArgs + method_->GetNumArgs()) { 554514f5e3Sopenharmony_ci return true; 564514f5e3Sopenharmony_ci } 574514f5e3Sopenharmony_ci return false; 584514f5e3Sopenharmony_ci} 594514f5e3Sopenharmony_ci 604514f5e3Sopenharmony_ciGateRef ArgumentAccessor::GetCommonArgGate(const CommonArgIdx arg) const 614514f5e3Sopenharmony_ci{ 624514f5e3Sopenharmony_ci return args_.at(static_cast<size_t>(arg)); 634514f5e3Sopenharmony_ci} 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_cisize_t ArgumentAccessor::GetFunctionArgIndex(const size_t currentVreg, const bool haveFunc, 664514f5e3Sopenharmony_ci const bool haveNewTarget, const bool haveThis) const 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci size_t numCommonArgs = haveFunc + haveNewTarget + haveThis; 694514f5e3Sopenharmony_ci // 2: number of common args 704514f5e3Sopenharmony_ci if (numCommonArgs == 2) { 714514f5e3Sopenharmony_ci if (!haveFunc && currentVreg == 0) { 724514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::NEW_TARGET); 734514f5e3Sopenharmony_ci } 744514f5e3Sopenharmony_ci if (!haveFunc && currentVreg == 1) { 754514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::THIS_OBJECT); 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci if (!haveNewTarget && currentVreg == 0) { 784514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::FUNC); 794514f5e3Sopenharmony_ci } 804514f5e3Sopenharmony_ci if (!haveNewTarget && currentVreg == 1) { 814514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::THIS_OBJECT); 824514f5e3Sopenharmony_ci } 834514f5e3Sopenharmony_ci if (!haveThis && currentVreg == 0) { 844514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::FUNC); 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci if (!haveThis && currentVreg == 1) { 874514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::NEW_TARGET); 884514f5e3Sopenharmony_ci } 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci // 1: number of common args, 0: the index of currentVreg 914514f5e3Sopenharmony_ci if (numCommonArgs == 1 && currentVreg == 0) { 924514f5e3Sopenharmony_ci if (haveFunc) { 934514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::FUNC); 944514f5e3Sopenharmony_ci } 954514f5e3Sopenharmony_ci if (haveNewTarget) { 964514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::NEW_TARGET); 974514f5e3Sopenharmony_ci } 984514f5e3Sopenharmony_ci if (haveThis) { 994514f5e3Sopenharmony_ci return static_cast<size_t>(CommonArgIdx::THIS_OBJECT); 1004514f5e3Sopenharmony_ci } 1014514f5e3Sopenharmony_ci } 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_ci size_t numOfArgs = static_cast<size_t>(CommonArgIdx::NUM_OF_ARGS); 1044514f5e3Sopenharmony_ci ASSERT(currentVreg + numOfArgs >= numCommonArgs); 1054514f5e3Sopenharmony_ci return currentVreg + numOfArgs - numCommonArgs; 1064514f5e3Sopenharmony_ci} 1074514f5e3Sopenharmony_ci 1084514f5e3Sopenharmony_civoid ArgumentAccessor::CollectArgs() 1094514f5e3Sopenharmony_ci{ 1104514f5e3Sopenharmony_ci if (args_.size() == 0) { 1114514f5e3Sopenharmony_ci GateAccessor(circuit_).GetArgsOuts(args_); 1124514f5e3Sopenharmony_ci std::reverse(args_.begin(), args_.end()); 1134514f5e3Sopenharmony_ci if (method_ == nullptr) { 1144514f5e3Sopenharmony_ci return; 1154514f5e3Sopenharmony_ci } 1164514f5e3Sopenharmony_ci if (method_->IsFastCall() && args_.size() > 2) { // 2: mean have func and glue 1174514f5e3Sopenharmony_ci GateRef actualArgcGate = circuit_->GetConstantGate(MachineType::I64, 0, GateType::NJSValue()); 1184514f5e3Sopenharmony_ci GateRef actualArgvGate = circuit_->GetConstantGate(MachineType::ARCH, 0, GateType::NJSValue()); 1194514f5e3Sopenharmony_ci GateRef newTargetGate = circuit_->GetConstantGate(MachineType::I64, JSTaggedValue::VALUE_UNDEFINED, 1204514f5e3Sopenharmony_ci GateType::UndefinedType()); 1214514f5e3Sopenharmony_ci if (method_->GetFunctionKind() == FunctionKind::CLASS_CONSTRUCTOR) { 1224514f5e3Sopenharmony_ci newTargetGate = args_[1]; // 1: mean func index 1234514f5e3Sopenharmony_ci } 1244514f5e3Sopenharmony_ci args_.insert(args_.begin() + static_cast<size_t>(CommonArgIdx::ACTUAL_ARGC), actualArgcGate); 1254514f5e3Sopenharmony_ci args_.insert(args_.begin() + static_cast<size_t>(CommonArgIdx::ACTUAL_ARGV), actualArgvGate); 1264514f5e3Sopenharmony_ci args_.insert(args_.begin() + static_cast<size_t>(CommonArgIdx::NEW_TARGET), newTargetGate); 1274514f5e3Sopenharmony_ci } 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci} 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_ciGateRef ArgumentAccessor::GetFrameArgsIn(GateRef gate, FrameArgIdx idx) 1324514f5e3Sopenharmony_ci{ 1334514f5e3Sopenharmony_ci GateAccessor gateAcc(circuit_); 1344514f5e3Sopenharmony_ci OpCode opCode = gateAcc.GetOpCode(gate); 1354514f5e3Sopenharmony_ci ASSERT(opCode == OpCode::JS_BYTECODE || opCode == OpCode::FRAME_STATE); 1364514f5e3Sopenharmony_ci GateRef frameArgs = Circuit::NullGate(); 1374514f5e3Sopenharmony_ci if (opCode == OpCode::JS_BYTECODE) { 1384514f5e3Sopenharmony_ci GateRef frameState = gateAcc.GetFrameState(gate); 1394514f5e3Sopenharmony_ci OpCode op = gateAcc.GetOpCode(frameState); 1404514f5e3Sopenharmony_ci if (op == OpCode::FRAME_STATE) { 1414514f5e3Sopenharmony_ci frameArgs = gateAcc.GetValueIn(frameState, 0); // 0: frame args 1424514f5e3Sopenharmony_ci } else { 1434514f5e3Sopenharmony_ci ASSERT(op == OpCode::FRAME_ARGS); 1444514f5e3Sopenharmony_ci frameArgs = frameState; 1454514f5e3Sopenharmony_ci } 1464514f5e3Sopenharmony_ci } else { 1474514f5e3Sopenharmony_ci frameArgs = gateAcc.GetValueIn(gate, 0); // 0: frame args 1484514f5e3Sopenharmony_ci } 1494514f5e3Sopenharmony_ci return gateAcc.GetValueIn(frameArgs, static_cast<size_t>(idx)); 1504514f5e3Sopenharmony_ci} 1514514f5e3Sopenharmony_ci} // namespace panda::ecmascript::kungfu 152