• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::Ins & insn,protoPanda::Ins & protoInsn)19 void Ins::Serialize(const panda::pandasm::Ins &insn, protoPanda::Ins &protoInsn)
20 {
21     protoInsn.set_opcode(static_cast<uint32_t>(insn.opcode));
22     for (const auto &reg : 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.label);
42     protoInsn.set_setlabelval(insn.set_label);
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     insn.opcode = static_cast<panda::pandasm::Opcode>(protoInsn.opcode());
50     insn.regs.reserve(protoInsn.regs_size());
51     for (const auto &protoReg : protoInsn.regs()) {
52         insn.regs.push_back(static_cast<uint16_t>(protoReg));
53     }
54     insn.ids.reserve(protoInsn.ids_size());
55     for (const auto &protoId : protoInsn.ids()) {
56         insn.ids.push_back(protoId);
57     }
58     insn.imms.reserve(protoInsn.imms_size());
59     for (const auto &protoImm : protoInsn.imms()) {
60         switch (protoImm.type_case()) {
61             case protoPanda::Ins_IType::kValueInt: {
62                 insn.imms.push_back(protoImm.valueint());
63                 break;
64             }
65             case protoPanda::Ins_IType::kValueDouble: {
66                 insn.imms.push_back(protoImm.valuedouble());
67                 break;
68             }
69             default:
70                 UNREACHABLE();
71         }
72     }
73     insn.label = protoInsn.label();
74     insn.set_label = protoInsn.setlabelval();
75     const protoPanda::DebuginfoIns &protoDebugInfoIns = protoInsn.insdebug();
76     DebuginfoIns::Deserialize(protoDebugInfoIns, insn.ins_debug);
77 }
78 } // panda::proto
79