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 "assemblyFunctionProto.h"
17
18 namespace panda::proto {
Serialize(const panda::pandasm::Function::CatchBlock & block,protoPanda::CatchBlock & protoBlock)19 void CatchBlock::Serialize(const panda::pandasm::Function::CatchBlock &block, protoPanda::CatchBlock &protoBlock)
20 {
21 protoBlock.set_wholeline(block.whole_line);
22 protoBlock.set_exceptionrecord(block.exception_record);
23 protoBlock.set_trybeginlabel(block.try_begin_label);
24 protoBlock.set_tryendlabel(block.try_end_label);
25 protoBlock.set_catchbeginlabel(block.catch_begin_label);
26 protoBlock.set_catchendlabel(block.catch_end_label);
27 }
28
Deserialize(const protoPanda::CatchBlock & protoBlock,panda::pandasm::Function::CatchBlock & block)29 void CatchBlock::Deserialize(const protoPanda::CatchBlock &protoBlock, panda::pandasm::Function::CatchBlock &block)
30 {
31 block.whole_line = protoBlock.wholeline();
32 block.exception_record = protoBlock.exceptionrecord();
33 block.try_begin_label = protoBlock.trybeginlabel();
34 block.try_end_label = protoBlock.tryendlabel();
35 block.catch_begin_label = protoBlock.catchbeginlabel();
36 block.catch_end_label = protoBlock.catchendlabel();
37 }
38
Serialize(const panda::pandasm::Function::Parameter & param,protoPanda::Parameter & protoParam)39 void Parameter::Serialize(const panda::pandasm::Function::Parameter ¶m, protoPanda::Parameter &protoParam)
40 {
41 auto *type = protoParam.mutable_type();
42 Type::Serialize(param.type, *type);
43 }
44
Serialize(const panda::pandasm::Function & function,protoPanda::Function & protoFunction)45 void Function::Serialize(const panda::pandasm::Function &function, protoPanda::Function &protoFunction)
46 {
47 protoFunction.set_name(function.name);
48 protoFunction.set_language(static_cast<uint32_t>(function.language));
49
50 auto *protoFuncMeta = protoFunction.mutable_metadata();
51 FunctionMetadata::Serialize(*function.metadata, *protoFuncMeta);
52
53 for (const auto &[name, label] : function.label_table) {
54 auto *labelMap = protoFunction.add_labeltable();
55 labelMap->set_key(name);
56 auto *protoLabel = labelMap->mutable_value();
57 Label::Serialize(label, *protoLabel);
58 }
59
60 for (const auto &insn : function.ins) {
61 auto *protoIns = protoFunction.add_ins();
62 Ins::Serialize(insn, *protoIns);
63 }
64
65 for (const auto &debug : function.local_variable_debug) {
66 auto *protoDebug = protoFunction.add_localvariabledebug();
67 LocalVariable::Serialize(debug, *protoDebug);
68 }
69
70 protoFunction.set_sourcefile(function.source_file);
71 protoFunction.set_sourcecode(function.source_code);
72
73 for (const auto &block : function.catch_blocks) {
74 auto *protoBlock = protoFunction.add_catchblocks();
75 CatchBlock::Serialize(block, *protoBlock);
76 }
77
78 protoFunction.set_valueoffirstparam(function.value_of_first_param);
79 protoFunction.set_regsnum(function.regs_num);
80
81 for (const auto ¶m : function.params) {
82 auto *protoParam = protoFunction.add_params();
83 Parameter::Serialize(param, *protoParam);
84 }
85
86 protoFunction.set_bodypresence(function.body_presence);
87
88 auto *protoReturnType = protoFunction.mutable_returntype();
89 Type::Serialize(function.return_type, *protoReturnType);
90
91 auto *protoBodyLocation = protoFunction.mutable_bodylocation();
92 SourceLocation::Serialize(function.body_location, *protoBodyLocation);
93
94 const auto &fileLocation = function.file_location;
95 if (fileLocation.has_value()) {
96 auto *protoFileLocation = protoFunction.mutable_filelocation();
97 FileLocation::Serialize(fileLocation.value(), *protoFileLocation);
98 }
99 protoFunction.set_function_kind(static_cast<uint8_t>(function.function_kind));
100 protoFunction.set_slotsnum(function.slots_num);
101
102 for (const auto &moduleRequestId : function.concurrent_module_requests) {
103 protoFunction.add_concurrent_module_requests(moduleRequestId);
104 }
105 }
106
DeserializeLabels(const protoPanda::Function & protoFunction,panda::pandasm::Function & function,panda::ArenaAllocator * allocator)107 void Function::DeserializeLabels(const protoPanda::Function &protoFunction, panda::pandasm::Function &function,
108 panda::ArenaAllocator *allocator)
109 {
110 function.label_table.reserve(protoFunction.labeltable_size());
111 for (const auto &labelUnit : protoFunction.labeltable()) {
112 auto &name = labelUnit.key();
113 auto &protoLabel = labelUnit.value();
114 panda::pandasm::Label label(name);
115 Label::Deserialize(protoLabel, label);
116 function.label_table.insert({name, label});
117 }
118 }
119
DeserializeProtoIns(const protoPanda::Function & protoFunction,panda::pandasm::Function & function,panda::ArenaAllocator * allocator)120 void Function::DeserializeProtoIns(const protoPanda::Function &protoFunction, panda::pandasm::Function &function,
121 panda::ArenaAllocator *allocator)
122 {
123 function.ins.reserve(protoFunction.ins_size());
124 for (const auto &protoIns : protoFunction.ins()) {
125 panda::pandasm::Ins *ins {nullptr};
126 Ins::Deserialize(protoIns, ins);
127 function.ins.emplace_back(ins);
128 }
129 }
130
DeserializeProtoLocalVariable(const protoPanda::Function & protoFunction,panda::pandasm::Function & function,panda::ArenaAllocator * allocator)131 void Function::DeserializeProtoLocalVariable(const protoPanda::Function &protoFunction,
132 panda::pandasm::Function &function,
133 panda::ArenaAllocator *allocator)
134 {
135 function.local_variable_debug.reserve(protoFunction.localvariabledebug_size());
136 for (const auto &protoLocalVariable : protoFunction.localvariabledebug()) {
137 panda::pandasm::debuginfo::LocalVariable localVariable;
138 LocalVariable::Deserialize(protoLocalVariable, localVariable);
139 function.local_variable_debug.emplace_back(std::move(localVariable));
140 }
141 }
142
Deserialize(const protoPanda::Function & protoFunction,panda::pandasm::Function & function,panda::ArenaAllocator * allocator)143 void Function::Deserialize(const protoPanda::Function &protoFunction, panda::pandasm::Function &function,
144 panda::ArenaAllocator *allocator)
145 {
146 FunctionMetadata::Deserialize(protoFunction.metadata(), function.metadata, allocator);
147 DeserializeLabels(protoFunction, function, allocator);
148 DeserializeProtoIns(protoFunction, function, allocator);
149 DeserializeProtoLocalVariable(protoFunction, function, allocator);
150
151 function.source_file = protoFunction.sourcefile();
152 function.source_code = protoFunction.sourcecode();
153
154 function.catch_blocks.reserve(protoFunction.catchblocks_size());
155 for (const auto &protoCatchBlock : protoFunction.catchblocks()) {
156 auto *catchBlock = allocator->New<panda::pandasm::Function::CatchBlock>();
157 CHECK_NOT_NULL(catchBlock);
158 CatchBlock::Deserialize(protoCatchBlock, *catchBlock);
159 function.catch_blocks.emplace_back(std::move(*catchBlock));
160 }
161
162 function.value_of_first_param = protoFunction.valueoffirstparam();
163 function.regs_num = protoFunction.regsnum();
164
165 function.params.reserve(protoFunction.params_size());
166 for (const auto &protoParam : protoFunction.params()) {
167 auto ¶mType = Type::Deserialize(protoParam.type(), allocator);
168 panda::pandasm::Function::Parameter param(paramType, function.language);
169 function.params.emplace_back(std::move(param));
170 }
171
172 function.body_presence = protoFunction.bodypresence();
173 function.return_type = Type::Deserialize(protoFunction.returntype(), allocator);
174 SourceLocation::Deserialize(protoFunction.bodylocation(), function.body_location);
175
176 if (protoFunction.has_filelocation()) {
177 FileLocation::Deserialize(protoFunction.filelocation(), function.file_location);
178 }
179 function.SetFunctionKind(static_cast<panda::panda_file::FunctionKind>(protoFunction.function_kind()));
180 function.SetSlotsNum(protoFunction.slotsnum());
181
182 for (const auto &moduleRequestId : protoFunction.concurrent_module_requests()) {
183 function.concurrent_module_requests.emplace_back(moduleRequestId);
184 }
185 }
186 } // panda::proto
187