• 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 "assemblyProgramProto.h"
17 
18 namespace panda::proto {
Serialize(const panda::pandasm::Program & program,protoPanda::Program & protoProgram)19 void Program::Serialize(const panda::pandasm::Program &program, protoPanda::Program &protoProgram)
20 {
21     protoProgram.set_lang(static_cast<uint32_t>(program.lang));
22 
23     for (const auto &[name, record] : program.record_table) {
24         auto *recordMap = protoProgram.add_recordtable();
25         recordMap->set_key(name);
26         auto *protoRecord = recordMap->mutable_value();
27         Record::Serialize(record, *protoRecord);
28     }
29 
30     for (const auto &[name, func] : program.function_table) {
31         auto *functionMap = protoProgram.add_functiontable();
32         functionMap->set_key(name);
33         auto *protoFunc = functionMap->mutable_value();
34         Function::Serialize(func, *protoFunc);
35     }
36 
37     for (const auto &[name, array] : program.literalarray_table) {
38         auto *literalarrayMap = protoProgram.add_literalarraytable();
39         literalarrayMap->set_key(name);
40         auto *protoArray = literalarrayMap->mutable_value();
41         LiteralArray::Serialize(array, *protoArray);
42     }
43     for (const auto &str : program.strings) {
44         protoProgram.add_strings(str);
45     }
46     for (const auto &type : program.array_types) {
47         auto *protoType = protoProgram.add_arraytypes();
48         Type::Serialize(type, *protoType);
49     }
50 }
51 
Deserialize(const protoPanda::Program & protoProgram,panda::pandasm::Program & program,panda::ArenaAllocator * allocator)52 void Program::Deserialize(const protoPanda::Program &protoProgram, panda::pandasm::Program &program,
53                           panda::ArenaAllocator *allocator)
54 {
55     program.lang = static_cast<panda::panda_file::SourceLang>(protoProgram.lang());
56 
57     for (const auto &recordUnit : protoProgram.recordtable()) {
58         auto &name = recordUnit.key();
59         auto &protoRecord = recordUnit.value();
60         auto record = panda::pandasm::Record(protoRecord.name(),
61                                              static_cast<panda::panda_file::SourceLang>(protoRecord.language()));
62         Record::Deserialize(protoRecord, record, allocator);
63         program.record_table.insert({name, std::move(record)});
64     }
65 
66     for (const auto &functionUnit : protoProgram.functiontable()) {
67         auto &name = functionUnit.key();
68         auto &protoFunction = functionUnit.value();
69         auto *function = allocator->New<panda::pandasm::Function>(protoFunction.name(),
70             static_cast<panda::panda_file::SourceLang>(protoFunction.language()));
71         CHECK_NOT_NULL(function);
72         Function::Deserialize(protoFunction, *function, allocator);
73         program.function_table.insert({name, std::move(*function)});
74     }
75 
76     for (const auto &literalUnit : protoProgram.literalarraytable()) {
77         auto &name = literalUnit.key();
78         auto &protoLiteralArray = literalUnit.value();
79         panda::pandasm::LiteralArray literalArray;
80         LiteralArray::Deserialize(protoLiteralArray, literalArray);
81         program.literalarray_table.insert({name, std::move(literalArray)});
82     }
83 
84     for (const auto &protoString : protoProgram.strings()) {
85         program.strings.insert(protoString);
86     }
87 
88     for (const auto &protoArrayType : protoProgram.arraytypes()) {
89         auto &arrayType = Type::Deserialize(protoArrayType, allocator);
90         program.array_types.insert(std::move(arrayType));
91     }
92 }
93 } // panda::proto
94