• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     LEXENV,
29     ACTUAL_ARGC,
30     FUNC,
31     NEW_TARGET,
32     THIS_OBJECT,
33     NUM_OF_ARGS,
34 };
35 
36 class ArgumentAccessor {
37 public:
38     explicit ArgumentAccessor(
39         Circuit *circuit, const MethodLiteral *methodLiteral = nullptr)
circuit_(circuit)40         : circuit_(circuit),
41           method_(methodLiteral),
42           argRoot_(circuit->GetArgRoot()),
43           args_(0)
44     {
45         CollectArgs();
46     }
47     ~ArgumentAccessor() = default;
48 
49     void NewCommonArg(const CommonArgIdx argIndex, MachineType machineType, GateType gateType);
50     void NewArg(const size_t argIndex);
51     // method must be set
52     size_t GetActualNumArgs() const;
53     // method must be set
54     GateRef GetArgGate(const size_t currentVreg) const;
55     GateRef GetCommonArgGate(const CommonArgIdx arg) const;
ArgsAt(const size_t index)56     GateRef ArgsAt(const size_t index) const
57     {
58         return args_.at(index);
59     }
ArgsCount()60     size_t ArgsCount() const
61     {
62         return args_.size();
63     }
64     void FillArgsGateType(const TypeRecorder *typeRecorder);
65     void CollectArgs();
GetFixArgsNum()66     static size_t GetFixArgsNum()
67     {
68         return static_cast<size_t>(CommonArgIdx::NUM_OF_ARGS) - static_cast<size_t>(CommonArgIdx::FUNC);
69     }
70 
71 private:
72     size_t GetFunctionArgIndex(const size_t currentVreg, const bool haveFunc,
73                                const bool haveNewTarget, const bool haveThis) const;
74     GateRef GetTypedArgGate(const size_t argIndex) const;
75 
76     Circuit *circuit_ {nullptr};
77     const MethodLiteral *method_ {nullptr};
78     GateRef argRoot_;
79     std::vector<GateRef> args_;
80 };
81 }
82 #endif  // ECMASCRIPT_COMPILER_ARGUMENT_ACCESSOR_H
83