1 /** 2 * Copyright (c) 2024 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 LIBABCKIT_SRC_CODEGEN_CODEGEN_DYNAMIC_H 17 #define LIBABCKIT_SRC_CODEGEN_CODEGEN_DYNAMIC_H 18 19 #include "static_core/compiler/optimizer/pass.h" 20 #include "static_core/compiler/optimizer/ir/basicblock.h" 21 #include "static_core/compiler/optimizer/ir/graph.h" 22 #include "static_core/compiler/optimizer/ir/graph_visitor.h" 23 #include "static_core/compiler/optimizer/ir/constants.h" 24 #include "static_core/compiler/optimizer/ir/inst.h" 25 #include "libabckit/src/codegen/common.h" 26 #include "libabckit/src/wrappers/pandasm_wrapper.h" 27 #include "libabckit/src/ir_impl.h" 28 29 namespace libabckit { 30 31 using ark::compiler::BasicBlock; 32 using ark::compiler::Inst; 33 using ark::compiler::Opcode; 34 35 void DoLda(ark::compiler::Register reg, std::vector<InsWrapper> &result); 36 void DoSta(ark::compiler::Register reg, std::vector<InsWrapper> &result); 37 38 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 39 class CodeGenDynamic : public ark::compiler::Optimization, public ark::compiler::GraphVisitor { 40 public: CodeGenDynamic(ark::compiler::Graph * graph,FunctionWrapper * function,const AbckitIrInterface * irInterface)41 explicit CodeGenDynamic(ark::compiler::Graph *graph, FunctionWrapper *function, 42 const AbckitIrInterface *irInterface) 43 : ark::compiler::Optimization(graph), function_(function), irInterface_(irInterface) 44 { 45 } 46 47 constexpr static ark::compiler::Register RESERVED_REG = 0U; 48 bool RunImpl() override; GetPassName()49 const char *GetPassName() const override 50 { 51 return "CodeGenDynamic"; 52 } GetEncodedInstructions()53 std::vector<InsWrapper> GetEncodedInstructions() const 54 { 55 return res_; 56 } 57 58 void Reserve(size_t resSize = 0) 59 { 60 if (resSize > 0) { 61 result_.reserve(resSize); 62 } 63 } 64 GetStatus()65 bool GetStatus() const 66 { 67 return success_; 68 } 69 GetResult()70 const std::vector<InsWrapper> &GetResult() const 71 { 72 return result_; 73 } 74 GetResult()75 std::vector<InsWrapper> &&GetResult() 76 { 77 return std::move(result_); 78 } 79 LabelName(uint32_t id)80 static std::string LabelName(uint32_t id) 81 { 82 return "label_" + std::to_string(id); 83 } 84 EmitLabel(const std::string & label)85 void EmitLabel(const std::string &label) 86 { 87 InsWrapper l; 88 l.label = label; 89 l.setLabel = true; 90 result_.emplace_back(l); 91 } 92 93 void EmitJump(const BasicBlock *bb); 94 95 void EncodeSpillFillData(const ark::compiler::SpillFillData &sf); 96 void EncodeSta(ark::compiler::Register reg, ark::compiler::DataType::Type type); 97 void AddLineNumber(const Inst *inst, const size_t idx); 98 void AddColumnNumber(const Inst *inst, const uint32_t idx); 99 GetBlocksToVisit()100 const ark::ArenaVector<BasicBlock *> &GetBlocksToVisit() const override 101 { 102 return GetGraph()->GetBlocksRPO(); 103 } 104 static void VisitSpillFill(GraphVisitor *visitor, Inst *inst); 105 static void VisitConstant(GraphVisitor *visitor, Inst *inst); 106 static void VisitCatchPhi(GraphVisitor *visitor, Inst *inst); 107 108 static void VisitIf(GraphVisitor *v, Inst *instBase); 109 static void VisitIfImm(GraphVisitor *v, Inst *instBase); 110 static void IfImmZero(GraphVisitor *v, Inst *instBase); 111 static void VisitIntrinsic(GraphVisitor *visitor, Inst *instBase); 112 static void VisitLoadString(GraphVisitor *v, Inst *instBase); 113 static void VisitLoadStringIntrinsic(GraphVisitor *v, Inst *instBase); 114 static void VisitReturn(GraphVisitor *v, Inst *instBase); 115 116 static void VisitEcma(GraphVisitor *v, Inst *instBase); 117 118 #include "generated/codegen_visitors_dyn.inc" 119 120 #include "generated/insn_selection_dynamic.h" 121 VisitDefault(Inst * inst)122 void VisitDefault(Inst *inst) override 123 { 124 std::cerr << "Opcode " << ark::compiler::GetOpcodeString(inst->GetOpcode()) 125 << " not yet implemented in codegen\n"; 126 success_ = false; 127 } 128 129 #include "compiler/optimizer/ir/visitor.inc" 130 131 private: 132 void AppendCatchBlock(uint32_t typeId, const ark::compiler::BasicBlock *tryBegin, 133 const ark::compiler::BasicBlock *tryEnd, const ark::compiler::BasicBlock *catchBegin, 134 const ark::compiler::BasicBlock *catchEnd = nullptr); 135 void VisitTryBegin(const ark::compiler::BasicBlock *bb); 136 137 private: 138 FunctionWrapper *function_; 139 const AbckitIrInterface *irInterface_; 140 141 std::vector<InsWrapper> res_; 142 std::vector<FunctionWrapper::CatchBlockWrapper> catchBlocks_; 143 144 bool success_ {true}; 145 std::vector<InsWrapper> result_; 146 }; 147 148 } // namespace libabckit 149 150 #endif // LIBABCKIT_SRC_CODEGEN_CODEGEN_DYNAMIC_H 151