• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PANDA_ASSEMBLER_ASSEMBLY_FUNCTION_H_
17 #define PANDA_ASSEMBLER_ASSEMBLY_FUNCTION_H_
18 
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <string_view>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "assembly-ins.h"
27 #include "assembly-label.h"
28 #include "assembly-type.h"
29 #include "assembly-debug.h"
30 #include "assembly-file-location.h"
31 #include "bytecode_emitter.h"
32 #include "extensions/extensions.h"
33 #include "file_items.h"
34 #include "file_item_container.h"
35 #include "ide_helpers.h"
36 #include "meta.h"
37 
38 namespace panda::pandasm {
39 
40 struct Function {
41     struct CatchBlock {
42         std::string whole_line;
43         std::string exception_record;
44         std::string try_begin_label;
45         std::string try_end_label;
46         std::string catch_begin_label;
47         std::string catch_end_label;
48     };
49 
50     struct TryCatchInfo {
51         std::unordered_map<std::string_view, size_t> try_catch_labels;
52         std::unordered_map<std::string, std::vector<const CatchBlock *>> try_catch_map;
53         std::vector<std::string> try_catch_order;
TryCatchInfoFunction::TryCatchInfo54         TryCatchInfo(std::unordered_map<std::string_view, size_t> &labels,
55                      std::unordered_map<std::string, std::vector<const CatchBlock *>> &map,
56                      std::vector<std::string> &param_try_catch_order)
57             : try_catch_labels(labels), try_catch_map(map), try_catch_order(param_try_catch_order)
58         {
59         }
60     };
61 
62     struct Parameter {
63         Type type;
64         std::unique_ptr<ParamMetadata> metadata;
65 
ParameterFunction::Parameter66         Parameter(Type t, extensions::Language lang)
67             : type(std::move(t)), metadata(extensions::MetadataExtension::CreateParamMetadata(lang))
68         {
69         }
70     };
71 
72     std::string name = "";
73     extensions::Language language;
74     std::unique_ptr<FunctionMetadata> metadata;
75 
76     std::unordered_map<std::string, panda::pandasm::Label> label_table;
77     std::vector<panda::pandasm::Ins> ins; /* function instruction list */
78     std::vector<panda::pandasm::debuginfo::LocalVariable> local_variable_debug;
79     std::string source_file; /* The file in which the function is defined or empty */
80     std::string source_code;
81     std::vector<CatchBlock> catch_blocks;
82     int64_t value_of_first_param = -1;
83     size_t regs_num = 0;
84     std::vector<Parameter> params;
85     bool body_presence = false;
86     Type return_type;
87     SourceLocation body_location;
88     std::optional<FileLocation> file_location;
89 
SetInsDebugFunction90     void SetInsDebug(const std::vector<debuginfo::Ins> &ins_debug)
91     {
92         ASSERT(ins_debug.size() == ins.size());
93         for (std::size_t i = 0; i < ins.size(); i++) {
94             ins[i].ins_debug = ins_debug[i];
95         }
96     }
97 
AddInstructionFunction98     void AddInstruction(const panda::pandasm::Ins &instruction)
99     {
100         ins.emplace_back(instruction);
101     }
102 
FunctionFunction103     Function(std::string s, extensions::Language lang, size_t b_l, size_t b_r, std::string f_c, bool d, size_t l_n)
104         : name(std::move(s)),
105           language(lang),
106           metadata(extensions::MetadataExtension::CreateFunctionMetadata(lang)),
107           file_location({f_c, b_l, b_r, l_n, d})
108     {
109     }
110 
FunctionFunction111     Function(std::string s, extensions::Language lang)
112         : name(std::move(s)), language(lang), metadata(extensions::MetadataExtension::CreateFunctionMetadata(lang))
113     {
114     }
115 
GetParamsNumFunction116     std::size_t GetParamsNum() const
117     {
118         return params.size();
119     }
120 
IsStaticFunction121     bool IsStatic() const
122     {
123         return (metadata->GetAccessFlags() & ACC_STATIC) != 0;
124     }
125 
126     bool Emit(BytecodeEmitter &emitter, panda_file::MethodItem *method,
127               const std::unordered_map<std::string, panda_file::BaseMethodItem *> &methods,
128               const std::unordered_map<std::string, panda_file::BaseFieldItem *> &fields,
129               const std::unordered_map<std::string, panda_file::BaseClassItem *> &classes,
130               const std::unordered_map<std::string_view, panda_file::StringItem *> &strings,
131               const std::unordered_map<std::string, panda_file::LiteralArrayItem *> &literalarrays) const;
132 
133     size_t GetLineNumber(size_t i) const;
134 
135     size_t GetColumnNumber(size_t i) const;
136 
137     void EmitLocalVariable(panda_file::LineNumberProgramItem *program, panda_file::ItemContainer *container,
138                            std::vector<uint8_t> *constant_pool, uint32_t &pc_inc, size_t instruction_number) const;
139     void EmitNumber(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool, uint32_t pc_inc,
140                     int32_t line_inc) const;
141     void EmitLineNumber(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool,
142                         int32_t &prev_line_number, uint32_t &pc_inc, size_t instruction_number) const;
143     // column number is only for javascript for now
144     void EmitColumnNumber(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool,
145                           int32_t &prev_column_number, uint32_t &pc_inc, size_t instruction_number) const;
146 
147     void BuildLineNumberProgram(panda_file::DebugInfoItem *debug_item, const std::vector<uint8_t> &bytecode,
148                                 panda_file::ItemContainer *container, std::vector<uint8_t> *constant_pool,
149                                 bool emit_debug_info) const;
150 
151     Function::TryCatchInfo MakeOrderAndOffsets(const std::vector<uint8_t> &bytecode) const;
152 
153     std::vector<panda_file::CodeItem::TryBlock> BuildTryBlocks(
154         panda_file::MethodItem *method, const std::unordered_map<std::string, panda_file::BaseClassItem *> &class_items,
155         const std::vector<uint8_t> &bytecode) const;
156 
HasImplementationFunction157     bool HasImplementation() const
158     {
159         return !metadata->IsForeign() && metadata->HasImplementation();
160     }
161 
IsParameterFunction162     bool IsParameter(uint32_t reg_number) const
163     {
164         return reg_number >= regs_num;
165     }
166 
CanThrowFunction167     bool CanThrow() const
168     {
169         return std::any_of(ins.cbegin(), ins.cend(), [](const Ins &insn) { return insn.CanThrow(); });
170     }
171 
HasDebugInfoFunction172     bool HasDebugInfo() const
173     {
174         return std::any_of(ins.cbegin(), ins.cend(), [](const Ins &insn) { return insn.HasDebugInfo(); });
175     }
176 
177     void DebugDump() const;
178 };
179 
180 }  // namespace panda::pandasm
181 
182 #endif  // PANDA_ASSEMBLER_ASSEMBLY_FUNCTION_H_
183