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 #include "libabckit/src/wrappers/pandasm_wrapper.h"
17 #include "libabckit/src/ir_impl.h"
18
19 #include "assembler/assembly-function.h"
20 #include "assembler/assembly-emitter.h"
21 #include "assembler/assembly-literals.h"
22 #include "assembler/mangling.h"
23
24 #include <generated/opc_to_string.h>
25
26 namespace libabckit {
27 // CC-OFFNXT(WordsTool.95) sensitive word conflict
28 // NOLINTNEXTLINE(google-build-using-namespace)
29 using namespace panda;
30
31 std::string InsWrapper::opcodeInvalid_ = "INVALID";
32
Fill()33 void FunctionWrapper::Fill()
34 {
35 auto *func = reinterpret_cast<pandasm::Function *>(impl);
36 name = func->name;
37 regsNum = func->regs_num;
38 valueOfFirstParam = func->value_of_first_param;
39 for (const auto &cb : func->catch_blocks) {
40 auto catchBlockWrap =
41 FunctionWrapper::CatchBlockWrapper(cb.whole_line, cb.exception_record, cb.try_begin_label, cb.try_end_label,
42 cb.catch_begin_label, cb.catch_end_label);
43 catchBlocks.push_back(catchBlockWrap);
44 }
45 }
46
Update()47 void FunctionWrapper::Update()
48 {
49 auto *func = reinterpret_cast<pandasm::Function *>(impl);
50 func->ins.clear();
51 func->catch_blocks.clear();
52 func->name = name;
53 func->regs_num = regsNum;
54 func->value_of_first_param = valueOfFirstParam;
55 func->slots_num = slotsNum;
56 for (auto insWrap : ins) {
57 auto insDebug = pandasm::debuginfo::Ins(insWrap.insDebug.lineNumber);
58 auto opcode = OpcFromName(insWrap.opcode);
59 auto regs = std::move(insWrap.regs);
60 auto ids = std::move(insWrap.ids);
61 auto imms = std::move(insWrap.imms);
62 auto label = std::move(insWrap.label);
63 auto setLabel = insWrap.setLabel;
64 pandasm::Ins *insn {nullptr};
65 if (setLabel) {
66 insn = new pandasm::LabelIns(label);
67 } else {
68 insn = pandasm::Ins::CreateIns(opcode, regs, imms, ids);
69 }
70 insn->ins_debug = insDebug;
71 func->ins.emplace_back(insn);
72 }
73 for (auto cbWrap : catchBlocks) {
74 auto catchBlock = pandasm::Function::CatchBlock();
75 catchBlock.whole_line = std::move(cbWrap.wholeLine);
76 catchBlock.exception_record = std::move(cbWrap.exceptionRecord);
77 catchBlock.try_begin_label = std::move(cbWrap.tryBeginLabel);
78 catchBlock.try_end_label = std::move(cbWrap.tryEndLabel);
79 catchBlock.catch_begin_label = std::move(cbWrap.catchBeginLabel);
80 catchBlock.catch_end_label = std::move(cbWrap.catchEndLabel);
81 func->catch_blocks.push_back(catchBlock);
82 }
83 }
84
CreateWrappedFunction(panda::panda_file::SourceLang lang)85 FunctionWrapper *PandasmWrapper::CreateWrappedFunction(panda::panda_file::SourceLang lang)
86 {
87 auto *newFunc = new panda::pandasm::Function("newCode", lang);
88 return GetWrappedFunction(newFunc);
89 }
90
GetUnwrappedLiteralArrayTable(void * program)91 std::map<std::string, void *> PandasmWrapper::GetUnwrappedLiteralArrayTable(void *program)
92 {
93 auto *prog = reinterpret_cast<pandasm::Program *>(program);
94 std::map<std::string, void *> res;
95 for (auto &[id, s] : prog->literalarray_table) {
96 res[id] = reinterpret_cast<void *>(&s);
97 }
98 return res;
99 }
100
101 } // namespace libabckit
102