• 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_TS_TYPE_LOWERING_H
17 #define ECMASCRIPT_COMPILER_TS_TYPE_LOWERING_H
18 
19 #include "ecmascript/compiler/argument_accessor.h"
20 #include "ecmascript/compiler/builtins/builtins_call_signature.h"
21 #include "ecmascript/compiler/bytecode_circuit_builder.h"
22 #include "ecmascript/compiler/circuit_builder-inl.h"
23 #include "ecmascript/compiler/pass_manager.h"
24 
25 namespace panda::ecmascript::kungfu {
26 class TSTypeLowering {
27 public:
TSTypeLowering(Circuit * circuit,PassInfo * info,bool enableLog,const std::string & name)28     TSTypeLowering(Circuit *circuit, PassInfo *info,
29                    bool enableLog, const std::string& name)
30         : circuit_(circuit), acc_(circuit), builder_(circuit, info->GetCompilerConfig()),
31           dependEntry_(circuit->GetDependRoot()),
32           tsManager_(info->GetTSManager()),
33           enableLog_(enableLog),
34           profiling_(info->GetCompilerConfig()->IsProfiling()),
35           methodName_(name), glue_(acc_.GetGlueFromArgList()) {}
36 
37     ~TSTypeLowering() = default;
38 
39     void RunTSTypeLowering();
40 
41 private:
IsLogEnabled()42     bool IsLogEnabled() const
43     {
44         return enableLog_;
45     }
46 
IsProfiling()47     bool IsProfiling() const
48     {
49         return profiling_;
50     }
51 
GetMethodName()52     const std::string& GetMethodName() const
53     {
54         return methodName_;
55     }
56 
57     void Lower(GateRef gate);
58     void VerifyGuard() const;
59     void DeleteGates(GateRef hir, std::vector<GateRef> &unusedGate);
60     void ReplaceHIRGate(GateRef hir, GateRef outir, GateRef state, GateRef depend,
61                         std::vector<GateRef> &unuseGate);
62     void LowerTypedAdd(GateRef gate);
63     void LowerTypedSub(GateRef gate);
64     void LowerTypedMul(GateRef gate);
65     void LowerTypedMod(GateRef gate);
66     void LowerTypedLess(GateRef gate);
67     void LowerTypedLessEq(GateRef gate);
68     void LowerTypedGreater(GateRef gate);
69     void LowerTypedGreaterEq(GateRef gate);
70     void LowerTypedDiv(GateRef gate);
71     void LowerTypedEq(GateRef gate);
72     void LowerTypedNotEq(GateRef gate);
73     void LowerTypedShl(GateRef gate);
74     void LowerTypedShr(GateRef gate);
75     void LowerTypedAshr(GateRef gate);
76     void LowerTypedAnd(GateRef gate);
77     void LowerTypedOr(GateRef gate);
78     void LowerTypedXor(GateRef gate);
79     void LowerTypedInc(GateRef gate);
80     void LowerTypedDec(GateRef gate);
81     void LowerTypeToNumeric(GateRef gate);
82     void LowerPrimitiveTypeToNumber(GateRef gate);
83     void LowerConditionJump(GateRef gate);
84     void LowerTypedNeg(GateRef gate);
85     void LowerTypedNot(GateRef gate);
86     void LowerTypedLdObjByName(GateRef gate);
87     void LowerTypedLdArrayLength(GateRef gate);
88     void LowerTypedStObjByName(GateRef gate, bool isThis);
89     void LowerTypedLdObjByIndex(GateRef gate);
90     void LowerTypedStObjByIndex(GateRef gate);
91     void LowerTypedLdObjByValue(GateRef gate, bool isThis);
92     void LowerTypedIsTrueOrFalse(GateRef gate, bool flag);
93     void LowerTypedNewObjRange(GateRef gate);
94     void LowerTypedSuperCall(GateRef gate, GateRef ctor, GateRef newTarget);
95 
96     void LowerCallThis1Imm8V8V8(GateRef gate);
97     bool CheckParam(GateRef gate, bool isCallThis, MethodLiteral* method);
98 
99     // TypeTrusted means the type of gate is already PrimitiveTypeCheck-passed, or the gate is constant and no need to check.
100     bool IsTrustedType(GateRef gate) const;
101     bool NeedInt32OverflowCheck(TypedUnOp op) const;
102 
103     template<TypedBinOp Op>
104     void SpeculateNumbers(GateRef gate);
105     template<TypedUnOp Op>
106     void SpeculateNumber(GateRef gate);
107     void SpeculateConditionJump(GateRef gate);
108     void SpeculateCallBuiltin(GateRef gate, BuiltinsStubCSigns::ID Op);
109     BuiltinsStubCSigns::ID GetBuiltinId(GateRef func, GateRef receiver);
110 
111     void AddProfiling(GateRef gate);
112     Circuit *circuit_ {nullptr};
113     GateAccessor acc_;
114     CircuitBuilder builder_;
115     GateRef dependEntry_ {Gate::InvalidGateRef};
116     TSManager *tsManager_ {nullptr};
117     bool enableLog_ {false};
118     bool profiling_ {false};
119     std::string methodName_;
120     GateRef glue_ {Circuit::NullGate()};
121 };
122 }  // panda::ecmascript::kungfu
123 #endif  // ECMASCRIPT_COMPILER_TS_TYPE_LOWERING_H
124