• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 BYTECODE_OPTIMIZER_CODEGEN_H
17 #define BYTECODE_OPTIMIZER_CODEGEN_H
18 
19 #include "assembler/annotation.h"
20 #include "assembler/assembly-function.h"
21 #include "assembler/assembly-ins.h"
22 #include "ins_create_api.h"
23 #include "ir_interface.h"
24 #include "compiler/optimizer/pass.h"
25 #include "compiler/optimizer/ir/basicblock.h"
26 #include "compiler/optimizer/ir/graph.h"
27 #include "compiler/optimizer/ir/graph_visitor.h"
28 #include "utils/logger.h"
29 
30 namespace panda::bytecodeopt {
31 
32 using compiler::BasicBlock;
33 using compiler::Inst;
34 using compiler::Opcode;
35 
36 void DoLda(compiler::Register reg, std::vector<pandasm::Ins> &result);
37 void DoSta(compiler::Register reg, std::vector<pandasm::Ins> &result);
38 
39 class BytecodeGen : public compiler::Optimization, public compiler::GraphVisitor {
40 public:
BytecodeGen(compiler::Graph * graph,pandasm::Function * function,const BytecodeOptIrInterface * iface,pandasm::Program * prog)41     explicit BytecodeGen(compiler::Graph *graph, pandasm::Function *function,
42         const BytecodeOptIrInterface *iface, pandasm::Program *prog)
43         : compiler::Optimization(graph), function_(function), ir_interface_(iface), program_(prog)
44     {
45     }
46     ~BytecodeGen() override = default;
47     bool RunImpl() override;
GetPassName()48     const char *GetPassName() const override
49     {
50         return "BytecodeGen";
51     }
GetEncodedInstructions()52     std::vector<pandasm::Ins> GetEncodedInstructions() const
53     {
54         return res_;
55     }
56 
57     void Reserve(size_t res_size = 0)
58     {
59         if (res_size > 0) {
60             result_.reserve(res_size);
61         }
62     }
63 
GetStatus()64     bool GetStatus() const
65     {
66         return success_;
67     }
68 
GetResult()69     const std::vector<pandasm::Ins> &GetResult() const
70     {
71         return result_;
72     }
73 
GetResult()74     std::vector<pandasm::Ins> &&GetResult()
75     {
76         return std::move(result_);
77     }
78 
LabelName(uint32_t id)79     static std::string LabelName(uint32_t id)
80     {
81         return "label_" + std::to_string(id);
82     }
83 
EmitLabel(const std::string label)84     void EmitLabel(const std::string label)
85     {
86         pandasm::Ins l;
87         l.label = label;
88         l.set_label = true;
89         result_.emplace_back(l);
90     }
91 
92     void EmitJump(const BasicBlock *bb);
93 
94     void EncodeSpillFillData(const compiler::SpillFillData &sf);
95     void EncodeSta(compiler::Register reg, compiler::DataType::Type type);
96     void AddLineNumber(const Inst *inst, const size_t idx);
97     void AddColumnNumber(const Inst *inst, const uint32_t idx);
98 
GetBlocksToVisit()99     const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override
100     {
101         return GetGraph()->GetBlocksRPO();
102     }
103     static void VisitSpillFill(GraphVisitor *visitor, Inst *inst);
104     static void VisitConstant(GraphVisitor *visitor, Inst *inst);
105     static void VisitCatchPhi(GraphVisitor *visitor, Inst *inst);
106 
107     static void VisitIf(GraphVisitor *v, Inst *inst_base);
108     static void VisitIfImm(GraphVisitor *v, Inst *inst_base);
109     static void IfImmZero(GraphVisitor *v, Inst *inst_base);
110     static void VisitIntrinsic(GraphVisitor *visitor, Inst *inst_base);
111     static void VisitLoadString(GraphVisitor *v, Inst *inst_base);
112     static void VisitReturn(GraphVisitor *v, Inst *inst_base);
113 
114     static void VisitCastValueToAnyType(GraphVisitor *v, Inst *inst_base);
115 
116     static void VisitEcma(GraphVisitor *v, Inst *inst_base);
117     static void IfEcma(GraphVisitor *v, compiler::IfInst *inst);
118 
119 #include "generated/codegen_visitors.inc"
120 
121 #include "generated/insn_selection.h"
122 
VisitDefault(Inst * inst)123     void VisitDefault(Inst *inst) override
124     {
125         LOG(ERROR, BYTECODE_OPTIMIZER) << "Opcode " << compiler::GetOpcodeString(inst->GetOpcode())
126                                        << " not yet implemented in codegen";
127         success_ = false;
128     }
129 
130 #include "compiler/optimizer/ir/visitor.inc"
131 
132 private:
133     void AppendCatchBlock(uint32_t type_id, const compiler::BasicBlock *try_begin, const compiler::BasicBlock *try_end,
134                           const compiler::BasicBlock *catch_begin, const compiler::BasicBlock *catch_end = nullptr);
135     void VisitTryBegin(const compiler::BasicBlock *bb);
136 
137 private:
138     pandasm::Function *function_;
139     const BytecodeOptIrInterface *ir_interface_;
140     pandasm::Program *program_;
141 
142     std::vector<pandasm::Ins> res_;
143     std::vector<pandasm::Function::CatchBlock> catch_blocks_;
144 
145     bool success_ {true};
146     std::vector<pandasm::Ins> result_;
147 };
148 
149 }  // namespace panda::bytecodeopt
150 
151 #endif  // BYTECODE_OPTIMIZER_CODEGEN_H
152