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#ifndef ECMASCRIPT_COMPILER_ARGUMENT_ACCESSOR_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_ARGUMENT_ACCESSOR_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/circuit.h"
204514f5e3Sopenharmony_ci#include "ecmascript/compiler/gate.h"
214514f5e3Sopenharmony_ci#include "ecmascript/compiler/gate_accessor.h"
224514f5e3Sopenharmony_ci#include "ecmascript/method.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
254514f5e3Sopenharmony_cienum class CommonArgIdx : uint8_t {
264514f5e3Sopenharmony_ci    GLUE = 0,
274514f5e3Sopenharmony_ci    ACTUAL_ARGC,
284514f5e3Sopenharmony_ci    ACTUAL_ARGV,
294514f5e3Sopenharmony_ci    FUNC,
304514f5e3Sopenharmony_ci    NEW_TARGET,
314514f5e3Sopenharmony_ci    THIS_OBJECT,
324514f5e3Sopenharmony_ci    NUM_OF_ARGS,
334514f5e3Sopenharmony_ci};
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_cienum class FastCallArgIdx : uint8_t {
364514f5e3Sopenharmony_ci    GLUE = 0,
374514f5e3Sopenharmony_ci    FUNC,
384514f5e3Sopenharmony_ci    THIS_OBJECT,
394514f5e3Sopenharmony_ci    NUM_OF_ARGS,
404514f5e3Sopenharmony_ci};
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_cienum class FrameArgIdx : uint8_t {
434514f5e3Sopenharmony_ci    FUNC = 0,
444514f5e3Sopenharmony_ci    NEW_TARGET,
454514f5e3Sopenharmony_ci    THIS_OBJECT,
464514f5e3Sopenharmony_ci    ACTUAL_ARGC,
474514f5e3Sopenharmony_ci    ACTUAL_ARGV,
484514f5e3Sopenharmony_ci    SHARED_CONST_POOL,
494514f5e3Sopenharmony_ci    UNSHARED_CONST_POOL,
504514f5e3Sopenharmony_ci    NUM_OF_ARGS,
514514f5e3Sopenharmony_ci};
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ciclass ArgumentAccessor {
544514f5e3Sopenharmony_cipublic:
554514f5e3Sopenharmony_ci    explicit ArgumentAccessor(
564514f5e3Sopenharmony_ci        Circuit *circuit, const MethodLiteral *methodLiteral = nullptr)
574514f5e3Sopenharmony_ci        : circuit_(circuit),
584514f5e3Sopenharmony_ci          method_(methodLiteral),
594514f5e3Sopenharmony_ci          argRoot_(circuit->GetArgRoot()),
604514f5e3Sopenharmony_ci          args_(0),
614514f5e3Sopenharmony_ci          frameArgs_{Circuit::NullGate()}
624514f5e3Sopenharmony_ci    {
634514f5e3Sopenharmony_ci        CollectArgs();
644514f5e3Sopenharmony_ci    }
654514f5e3Sopenharmony_ci    ~ArgumentAccessor() = default;
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    void NewCommonArg(const CommonArgIdx argIndex, MachineType machineType, GateType gateType);
684514f5e3Sopenharmony_ci    void NewArg(const size_t argIndex);
694514f5e3Sopenharmony_ci    // method must be set
704514f5e3Sopenharmony_ci    size_t GetActualNumArgs() const;
714514f5e3Sopenharmony_ci    // method must be set
724514f5e3Sopenharmony_ci    GateRef GetArgGate(const size_t currentVreg) const;
734514f5e3Sopenharmony_ci    bool ArgGateNotExisted(const size_t currentVreg);
744514f5e3Sopenharmony_ci    GateRef ArgsAt(const size_t index) const
754514f5e3Sopenharmony_ci    {
764514f5e3Sopenharmony_ci        return args_.at(index);
774514f5e3Sopenharmony_ci    }
784514f5e3Sopenharmony_ci    size_t ArgsCount() const
794514f5e3Sopenharmony_ci    {
804514f5e3Sopenharmony_ci        return args_.size();
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci    void CollectArgs();
834514f5e3Sopenharmony_ci    static size_t GetFixArgsNum()
844514f5e3Sopenharmony_ci    {
854514f5e3Sopenharmony_ci        return static_cast<size_t>(CommonArgIdx::NUM_OF_ARGS) - static_cast<size_t>(CommonArgIdx::FUNC);
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    static size_t GetExtraArgsNum()
894514f5e3Sopenharmony_ci    {
904514f5e3Sopenharmony_ci        return static_cast<size_t>(CommonArgIdx::ACTUAL_ARGV) - static_cast<size_t>(CommonArgIdx::GLUE);
914514f5e3Sopenharmony_ci    }
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    GateRef GetFrameArgs() const
944514f5e3Sopenharmony_ci    {
954514f5e3Sopenharmony_ci        return frameArgs_;
964514f5e3Sopenharmony_ci    }
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci    void SetFrameArgs(GateRef frameArgs)
994514f5e3Sopenharmony_ci    {
1004514f5e3Sopenharmony_ci        frameArgs_ = frameArgs;
1014514f5e3Sopenharmony_ci    }
1024514f5e3Sopenharmony_ci    GateRef GetFrameArgsIn(GateRef gate, FrameArgIdx idx);
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ciprivate:
1054514f5e3Sopenharmony_ci    // Disables using this interface during lowering, only allows it to be used during building graph.
1064514f5e3Sopenharmony_ci    GateRef GetCommonArgGate(const CommonArgIdx arg) const;
1074514f5e3Sopenharmony_ci    size_t GetFunctionArgIndex(const size_t currentVreg, const bool haveFunc,
1084514f5e3Sopenharmony_ci                               const bool haveNewTarget, const bool haveThis) const;
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    Circuit *circuit_ {nullptr};
1114514f5e3Sopenharmony_ci    const MethodLiteral *method_ {nullptr};
1124514f5e3Sopenharmony_ci    GateRef argRoot_;
1134514f5e3Sopenharmony_ci    std::vector<GateRef> args_;
1144514f5e3Sopenharmony_ci    GateRef frameArgs_;
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci    friend class BytecodeCircuitBuilder;
1174514f5e3Sopenharmony_ci    friend class AsyncFunctionLowering;
1184514f5e3Sopenharmony_ci    friend class InitializationAnalysis;
1194514f5e3Sopenharmony_ci};
1204514f5e3Sopenharmony_ci}
1214514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_ARGUMENT_ACCESSOR_H
122