• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WRAPPERS_PANDASM_WRAPPER_H
17 #define LIBABCKIT_SRC_WRAPPERS_PANDASM_WRAPPER_H
18 
19 #include <unordered_set>
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include <variant>
24 
25 namespace panda::panda_file {
26 enum class SourceLang : uint8_t;
27 }  // namespace panda::panda_file
28 
29 namespace libabckit {
30 
31 struct LiteralArrayWrapper;
32 struct InsWrapper;
33 
34 // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
35 struct LiteralArrayWrapper {
36     enum class LiteralTagWrapper : uint8_t {
37         TAGVALUE = 0x00,
38         BOOL = 0x01,
39         INTEGER = 0x02,
40         FLOAT = 0x03,
41         DOUBLE = 0x04,
42         STRING = 0x05,
43         METHOD = 0x06,
44         GENERATORMETHOD = 0x07,
45         ACCESSOR = 0x08,
46         METHODAFFILIATE = 0x09,
47         ARRAY_U1 = 0x0a,
48         ARRAY_U8 = 0x0b,
49         ARRAY_I8 = 0x0c,
50         ARRAY_U16 = 0x0d,
51         ARRAY_I16 = 0x0e,
52         ARRAY_U32 = 0x0f,
53         ARRAY_I32 = 0x10,
54         ARRAY_U64 = 0x11,
55         ARRAY_I64 = 0x12,
56         ARRAY_F32 = 0x13,
57         ARRAY_F64 = 0x14,
58         ARRAY_STRING = 0x15,
59         ASYNCGENERATORMETHOD = 0x16,
60         LITERALBUFFERINDEX = 0x17,
61         LITERALARRAY = 0x18,
62         BUILTINTYPEINDEX = 0x19,
63         GETTER = 0x1a,
64         SETTER = 0x1b,
65         NULLVALUE = 0xff
66     };
67 
68     struct LiteralWrapper {
69         LiteralTagWrapper tag;
70         std::variant<bool, uint8_t, uint16_t, uint32_t, uint64_t, float, double, std::string> value;
71     };
72 
73     std::vector<LiteralArrayWrapper::LiteralWrapper> literals;
74 
LiteralArrayWrapperLiteralArrayWrapper75     explicit LiteralArrayWrapper(std::vector<LiteralArrayWrapper::LiteralWrapper> lits) : literals(std::move(lits)) {}
76     explicit LiteralArrayWrapper() = default;
77 };
78 
79 struct FunctionWrapper {
80     struct CatchBlockWrapper {
81         std::string wholeLine;
82         std::string exceptionRecord;
83         std::string tryBeginLabel;
84         std::string tryEndLabel;
85         std::string catchBeginLabel;
86         std::string catchEndLabel;
87 
88         CatchBlockWrapper() = default;
CatchBlockWrapperFunctionWrapper::CatchBlockWrapper89         CatchBlockWrapper(std::string ln, std::string excr, std::string tbl, std::string tel, std::string cbl,
90                           std::string cel)
91             : wholeLine(std::move(ln)),
92               exceptionRecord(std::move(excr)),
93               tryBeginLabel(std::move(tbl)),
94               tryEndLabel(std::move(tel)),
95               catchBeginLabel(std::move(cbl)),
96               catchEndLabel(std::move(cel))
97         {
98         }
99     };
FunctionWrapperFunctionWrapper100     explicit FunctionWrapper(void *func) : impl(func) {}
101 
102     void Fill();
103     void Update();
104 
105     void *impl = nullptr;
106     std::string name;
107     int64_t valueOfFirstParam = -1;
108     size_t regsNum = 0;
109     size_t slotsNum = 0;
110     std::vector<InsWrapper> ins = {};
111     std::vector<CatchBlockWrapper> catchBlocks = {};
112 };
113 
114 struct InsWrapper {
115     static std::string opcodeInvalid_;
116 
117     struct DebugIns {
118         size_t lineNumber = 0;
119         uint32_t columnNumber = 0;
120         std::string wholeLine;  // NOTE(mbolshov): redundant given file and line_number
121         size_t boundLeft = 0;
122         size_t boundRight = 0;
123 
SetLineNumberInsWrapper::DebugIns124         void SetLineNumber(size_t ln)
125         {
126             lineNumber = ln;
127         }
128 
SetColumnNumberInsWrapper::DebugIns129         void SetColumnNumber(size_t cn)
130         {
131             columnNumber = cn;
132         }
133 
134         DebugIns() = default;
DebugInsInsWrapper::DebugIns135         DebugIns(size_t ln, std::string &fc, size_t bl, size_t br)
136             : lineNumber(ln), wholeLine(fc), boundLeft(bl), boundRight(br)
137         {
138         }
139     };
140     using IType = std::variant<int64_t, double>;
141 
142     constexpr static uint16_t ACCUMULATOR = -1;
143     constexpr static size_t MAX_CALL_SHORT_ARGS = 2;
144     constexpr static size_t MAX_CALL_ARGS = 4;
145     constexpr static uint16_t MAX_NON_RANGE_CALL_REG = 15;
146     constexpr static uint16_t MAX_RANGE_CALL_START_REG = (1U << 12U) * 2 - 1U;
147 
148     std::string opcode = opcodeInvalid_;
149     std::vector<uint16_t> regs;
150     std::vector<std::string> ids;
151     std::vector<IType> imms;
152     std::string label;
153     bool setLabel = false;
154     DebugIns insDebug;
155 
156     InsWrapper() = default;
InsWrapperInsWrapper157     InsWrapper(std::string opcodeArg, std::vector<uint16_t> regsArg, std::vector<std::string> idsArg,
158                std::vector<IType> immsArg, std::string labelArg, bool setLabelArg, DebugIns insDebugArg)
159         : opcode(std::move(opcodeArg)),
160           regs(std::move(regsArg)),
161           ids(std::move(idsArg)),
162           imms(std::move(immsArg)),
163           label(std::move(labelArg)),
164           setLabel(setLabelArg),
165           insDebug(std::move(insDebugArg))
166     {
167     }
168 // NOLINTNEXTLINE(readability-duplicate-include)
169 #include "arkcompiler/runtime_core/libabckit/src/wrappers/generated/ins_create_wrapper_api.inc"
170 };
171 // NOLINTEND(misc-non-private-member-variables-in-classes)
172 
173 class PandasmWrapper {
174 public:
175     using ArgTypes = std::variant<uint8_t, int64_t, double, uint64_t, uint32_t, std::string>;
176 
177     static FunctionWrapper *CreateWrappedFunction(panda::panda_file::SourceLang lang);
178 
GetWrappedFunction(void * function)179     static FunctionWrapper *GetWrappedFunction(void *function)
180     {
181         auto wrFunc = new FunctionWrapper(function);
182         wrFunc->Fill();
183         return wrFunc;
184     }
185 
186     static std::map<std::string, void *> GetUnwrappedLiteralArrayTable(void *prog);
GetPandasmFunction(FunctionWrapper * wrFunc)187     static void *GetPandasmFunction(FunctionWrapper *wrFunc)
188     {
189         wrFunc->Update();
190         auto function = wrFunc->impl;
191         delete wrFunc;
192         return function;
193     }
194 // NOLINTNEXTLINE(readability-duplicate-include)
195 #include "arkcompiler/runtime_core/libabckit/src/wrappers/generated/ins_create_wrapper_api.inc"
196 };
197 
198 // std::string DeMangleName(const std::string &name);
199 
200 }  // namespace libabckit
201 
202 #endif  // LIBABCKIT_SRC_WRAPPERS_PANDASM_WRAPPER_H
203