• 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 #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, insWrap.insDebug.wholeLine,
58                                                 insWrap.insDebug.boundLeft, insWrap.insDebug.boundRight);
59         auto insn = pandasm::Ins();
60         insn.opcode = OpcFromName(insWrap.opcode);
61         insn.regs = std::move(insWrap.regs);
62         insn.ids = std::move(insWrap.ids);
63         insn.imms = std::move(insWrap.imms);
64         insn.label = std::move(insWrap.label);
65         insn.set_label = insWrap.setLabel;
66         insn.ins_debug = insDebug;
67         func->ins.push_back(insn);
68     }
69     for (auto cbWrap : catchBlocks) {
70         auto catchBlock = pandasm::Function::CatchBlock();
71         catchBlock.whole_line = std::move(cbWrap.wholeLine);
72         catchBlock.exception_record = std::move(cbWrap.exceptionRecord);
73         catchBlock.try_begin_label = std::move(cbWrap.tryBeginLabel);
74         catchBlock.try_end_label = std::move(cbWrap.tryEndLabel);
75         catchBlock.catch_begin_label = std::move(cbWrap.catchBeginLabel);
76         catchBlock.catch_end_label = std::move(cbWrap.catchEndLabel);
77         func->catch_blocks.push_back(catchBlock);
78     }
79 }
80 
CreateWrappedFunction(panda::panda_file::SourceLang lang)81 FunctionWrapper *PandasmWrapper::CreateWrappedFunction(panda::panda_file::SourceLang lang)
82 {
83     auto *newFunc = new panda::pandasm::Function("newCode", lang);
84     return GetWrappedFunction(newFunc);
85 }
86 
GetUnwrappedLiteralArrayTable(void * program)87 std::map<std::string, void *> PandasmWrapper::GetUnwrappedLiteralArrayTable(void *program)
88 {
89     auto *prog = reinterpret_cast<pandasm::Program *>(program);
90     std::map<std::string, void *> res;
91     for (auto &[id, s] : prog->literalarray_table) {
92         res[id] = reinterpret_cast<void *>(&s);
93     }
94     return res;
95 }
96 
97 }  // namespace libabckit
98