1 /* 2 * Copyright (c) 2021 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_CONSTANT_FOLDING_H 17 #define ECMASCRIPT_COMPILER_CONSTANT_FOLDING_H 18 19 #include "ecmascript/compiler/circuit.h" 20 #include "ecmascript/compiler/circuit_builder.h" 21 #include "ecmascript/compiler/gate.h" 22 #include "ecmascript/compiler/share_gate_meta_data.h" 23 #include "ecmascript/compiler/combined_pass_visitor.h" 24 #include "ecmascript/compiler/number_gate_info.h" 25 26 27 namespace panda::ecmascript::kungfu { 28 29 class ConstantFolding : public PassVisitor { 30 public: ConstantFolding(Circuit * circuit,RPOVisitor * visitor,CompilationConfig * cmpCfg,bool enableLog,const std::string & name,Chunk * chunk)31 ConstantFolding(Circuit *circuit, RPOVisitor* visitor, CompilationConfig *cmpCfg, bool enableLog, 32 const std::string& name, Chunk* chunk) 33 : PassVisitor(circuit, chunk, visitor), circuit_(circuit), acc_(circuit), builder_(circuit, cmpCfg), 34 enableLog_(enableLog), methodName_(name) {} 35 ~ConstantFolding() = default; 36 void Print() const; 37 GateRef VisitGate(GateRef gate); 38 private: 39 GateRef VisitADD(GateRef gate); 40 GateRef VisitMUL(GateRef gate); 41 GateRef VisitZEXT(GateRef gate); 42 GateRef VisitSUB(GateRef gate); 43 GateRef VisitSMOD(GateRef gate); 44 GateRef VisitUMOD(GateRef gate); 45 GateRef Int32Constant(int32_t val); 46 GateRef Int64Constant(size_t val); 47 RangeInfo GetRange(GateRef gate) const; 48 bool IsInt32Type(GateRef gate) const; 49 IsConstant(GateRef gate)50 bool IsConstant(GateRef gate) const 51 { 52 return acc_.GetOpCode(gate) == OpCode::CONSTANT; 53 } 54 IsLogEnabled()55 bool IsLogEnabled() const 56 { 57 return enableLog_; 58 } GetMethodName()59 std::string GetMethodName() const 60 { 61 return methodName_; 62 } AddFoldingCount()63 void AddFoldingCount() 64 { 65 ++foldingCount_; 66 } GetFoldingCount()67 int32_t GetFoldingCount() const 68 { 69 return foldingCount_; 70 } 71 Circuit* circuit_; 72 GateAccessor acc_; 73 CircuitBuilder builder_; 74 bool enableLog_{false}; 75 std::string methodName_; 76 int32_t foldingCount_{0}; 77 }; 78 79 } // namespace panda::ecmascript::kungfu 80 #endif //ECMASCRIPT_COMPILER_CONSTANT_FOLDING_H