1 /**
2 * Copyright (c) 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 #include "assemblyInsProto.h"
17
18 namespace panda::proto {
Serialize(const panda::pandasm::InsPtr & insn,protoPanda::Ins & protoInsn)19 void Ins::Serialize(const panda::pandasm::InsPtr &insn, protoPanda::Ins &protoInsn)
20 {
21 protoInsn.set_opcode(static_cast<uint32_t>(insn->opcode));
22 for (const auto ® : insn->Regs()) {
23 protoInsn.add_regs(static_cast<uint32_t>(reg));
24 }
25 for (const auto &str : insn->Ids()) {
26 protoInsn.add_ids(str);
27 }
28 for (const auto &imm : insn->Imms()) {
29 auto *protoImm = protoInsn.add_imms();
30 switch (static_cast<protoPanda::Ins_IType::TypeCase>(imm.index() + 1)) { // 1: enum TypeCase start from 1
31 case protoPanda::Ins_IType::kValueInt:
32 protoImm->set_valueint(std::get<int64_t>(imm));
33 break;
34 case protoPanda::Ins_IType::kValueDouble:
35 protoImm->set_valuedouble(std::get<double>(imm));
36 break;
37 default:
38 UNREACHABLE();
39 }
40 }
41 protoInsn.set_label(insn->IsLabel() ? insn->Label() : "");
42 protoInsn.set_setlabelval(insn->IsLabel());
43 auto *protoDebug = protoInsn.mutable_insdebug();
44 DebuginfoIns::Serialize(insn->ins_debug, *protoDebug);
45 }
46
Deserialize(const protoPanda::Ins & protoInsn,panda::pandasm::Ins * & insn)47 void Ins::Deserialize(const protoPanda::Ins &protoInsn, panda::pandasm::Ins *&insn)
48 {
49 auto opcode = static_cast<panda::pandasm::Opcode>(protoInsn.opcode());
50 std::vector<uint16_t> regs;
51 regs.reserve(protoInsn.regs_size());
52 for (const auto &protoReg : protoInsn.regs()) {
53 regs.push_back(static_cast<uint16_t>(protoReg));
54 }
55 std::vector<std::string> ids;
56 ids.reserve(protoInsn.ids_size());
57 for (const auto &protoId : protoInsn.ids()) {
58 ids.push_back(protoId);
59 }
60 std::vector<panda::pandasm::IType> imms;
61 imms.reserve(protoInsn.imms_size());
62 for (const auto &protoImm : protoInsn.imms()) {
63 switch (protoImm.type_case()) {
64 case protoPanda::Ins_IType::kValueInt: {
65 imms.push_back(protoImm.valueint());
66 break;
67 }
68 case protoPanda::Ins_IType::kValueDouble: {
69 imms.push_back(protoImm.valuedouble());
70 break;
71 }
72 default:
73 UNREACHABLE();
74 }
75 }
76
77 auto set_label = protoInsn.setlabelval();
78 if (set_label) {
79 auto label = protoInsn.label();
80 insn = new pandasm::LabelIns(label);
81 } else {
82 insn = pandasm::Ins::CreateIns(opcode, regs, imms, ids);
83 }
84
85 const protoPanda::DebuginfoIns &protoDebugInfoIns = protoInsn.insdebug();
86 DebuginfoIns::Deserialize(protoDebugInfoIns, insn->ins_debug);
87 }
88 } // panda::proto
89