• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 PANDA_BYTECODE_OPT_CODEGEN_H
17 #define PANDA_BYTECODE_OPT_CODEGEN_H
18 
19 #include "assembler/assembly-function.h"
20 #include "assembler/assembly-ins.h"
21 #include "ins_create_api.h"
22 #include "ir_interface.h"
23 #include "compiler/optimizer/pass.h"
24 #include "compiler/optimizer/ir/basicblock.h"
25 #include "compiler/optimizer/ir/graph.h"
26 #include "compiler/optimizer/ir/graph_visitor.h"
27 #include "utils/logger.h"
28 #include "common.h"
29 
30 namespace ark::bytecodeopt {
31 
32 using compiler::BasicBlock;
33 using compiler::Inst;
34 using compiler::Opcode;
35 
36 void DoLdaObj(compiler::Register reg, std::vector<pandasm::Ins> &result);
37 void DoLda(compiler::Register reg, std::vector<pandasm::Ins> &result);
38 void DoLda64(compiler::Register reg, std::vector<pandasm::Ins> &result);
39 void DoStaObj(compiler::Register reg, std::vector<pandasm::Ins> &result);
40 void DoSta(compiler::Register reg, std::vector<pandasm::Ins> &result);
41 void DoSta64(compiler::Register reg, std::vector<pandasm::Ins> &result);
42 void DoSta(compiler::DataType::Type type, compiler::Register reg, std::vector<pandasm::Ins> &result);
43 void DoLdaDyn(compiler::Register reg, std::vector<pandasm::Ins> &result);
44 void DoStaDyn(compiler::Register reg, std::vector<pandasm::Ins> &result);
45 
46 // NOLINTNEXTLINE(fuchsia-multiple-inheritance)
47 class BytecodeGen : public compiler::Optimization, public compiler::GraphVisitor {
48 public:
BytecodeGen(compiler::Graph * graph,pandasm::Function * function,const BytecodeOptIrInterface * iface)49     explicit BytecodeGen(compiler::Graph *graph, pandasm::Function *function, const BytecodeOptIrInterface *iface)
50         : compiler::Optimization(graph), function_(function), irInterface_(iface)
51     {
52     }
53     ~BytecodeGen() override = default;
54     NO_COPY_SEMANTIC(BytecodeGen);
55     NO_MOVE_SEMANTIC(BytecodeGen);
56 
57     bool RunImpl() override;
GetPassName()58     const char *GetPassName() const override
59     {
60         return "BytecodeGen";
61     }
GetEncodedInstructions()62     std::vector<pandasm::Ins> GetEncodedInstructions() const
63     {
64         return res_;
65     }
66 
67     void Reserve(size_t resSize = 0)
68     {
69         if (resSize > 0) {
70             result_.reserve(resSize);
71         }
72     }
73 
GetStatus()74     bool GetStatus() const
75     {
76         return success_;
77     }
78 
GetResult()79     const std::vector<pandasm::Ins> &GetResult() const
80     {
81         return result_;
82     }
83 
GetResult()84     std::vector<pandasm::Ins> &&GetResult()
85     {
86         return std::move(result_);
87     }
88 
LabelName(uint32_t id)89     static std::string LabelName(uint32_t id)
90     {
91         return "label_" + std::to_string(id);
92     }
93 
EmitLabel(const std::string & label)94     void EmitLabel(const std::string &label)
95     {
96         pandasm::Ins l;
97         l.label = label;
98         l.setLabel = true;
99         result_.emplace_back(l);
100     }
101 
102     void EmitJump(const BasicBlock *bb);
103 
104     void EncodeSpillFillData(const compiler::SpillFillData &sf);
105     void EncodeSta(compiler::Register reg, compiler::DataType::Type type);
106     void AddLineNumber(const Inst *inst, size_t idx);
107     void AddColumnNumber(const Inst *inst, uint32_t idx);
108     void AddLineAndColumnNumber(const Inst *inst, size_t idx);
109 
GetBlocksToVisit()110     const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override
111     {
112         return GetGraph()->GetBlocksRPO();
113     }
114     static void VisitSpillFill(GraphVisitor *v, Inst *inst);
115     static void VisitConstant(GraphVisitor *v, Inst *inst);
116     static void VisitCallStatic(GraphVisitor *visitor, Inst *inst);
117     static void VisitCallVirtual(GraphVisitor *visitor, Inst *inst);
118     static void VisitInitObject(GraphVisitor *visitor, Inst *inst);
119     static void VisitCatchPhi(GraphVisitor *visitor, Inst *inst);
120 
121     static void VisitIf(GraphVisitor *v, Inst *instBase);
122     static void VisitIfImm(GraphVisitor *v, Inst *instBase);
123     static void VisitCast(GraphVisitor *v, Inst *instBase);
124     static void IfImmZero(GraphVisitor *v, Inst *instBase);
125     static void IfImmNonZero(GraphVisitor *v, Inst *instBase);
126     static void IfImm64(GraphVisitor *v, Inst *instBase);
127     static void VisitIntrinsic(GraphVisitor *v, Inst *instBase);
128     static void CallHandler(GraphVisitor *visitor, Inst *inst, std::string methodId);
129     static void CallHandler(GraphVisitor *visitor, Inst *inst);
130     static void VisitStoreObject(GraphVisitor *v, Inst *instBase);
131     static void VisitStoreStatic(GraphVisitor *v, Inst *instBase);
132     static void VisitLoadObject(GraphVisitor *v, Inst *instBase);
133     static void VisitLoadStatic(GraphVisitor *v, Inst *instBase);
134     static void VisitLoadString(GraphVisitor *v, Inst *instBase);
135     static void VisitReturn(GraphVisitor *v, Inst *instBase);
136     static void VisitReturnAny(GraphVisitor *v, Inst *instBase);
137 
138     static void VisitCastValueToAnyType(GraphVisitor *v, Inst *instBase);
139 
140     static void VisitEcma(GraphVisitor *v, Inst *instBase);
141     static void IfEcma(GraphVisitor *v, compiler::IfInst *inst);
142 
143 #include "generated/codegen_visitors.inc"
144 
145 #include "generated/insn_selection.h"
146 
VisitDefault(Inst * inst)147     void VisitDefault(Inst *inst) override
148     {
149         LOG(ERROR, BYTECODE_OPTIMIZER) << "Opcode " << compiler::GetOpcodeString(inst->GetOpcode())
150                                        << " not yet implemented in codegen";
151         success_ = false;
152     }
153 
154 #include "compiler/optimizer/ir/visitor.inc"
155 
156 private:
157     void AppendCatchBlock(uint32_t typeId, const compiler::BasicBlock *tryBegin, const compiler::BasicBlock *tryEnd,
158                           const compiler::BasicBlock *catchBegin, const compiler::BasicBlock *catchEnd = nullptr);
159     void VisitTryBegin(const compiler::BasicBlock *bb);
160 
161 private:
162     pandasm::Function *function_;
163     const BytecodeOptIrInterface *irInterface_;
164 
165     std::vector<pandasm::Ins> res_;
166     std::vector<pandasm::Function::CatchBlock> catchBlocks_;
167 
168     bool success_ {true};
169     std::vector<pandasm::Ins> result_;
170 };
171 
172 }  // namespace ark::bytecodeopt
173 
174 #endif  // PANDA_BYTECODE_OPT_CODEGEN_H
175