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 #include "assembly-program.h"
18 #include "protobufSnapshotGenerator.h"
19
20 namespace panda::proto {
GenerateSnapshot(const panda::pandasm::Program & program,const std::string & outputName)21 void ProtobufSnapshotGenerator::GenerateSnapshot(const panda::pandasm::Program &program, const std::string &outputName)
22 {
23 protoPanda::Program protoProgram;
24
25 Program::Serialize(program, protoProgram);
26
27 std::fstream output(panda::os::file::File::GetExtendedFilePath(outputName),
28 std::ios::out | std::ios::trunc | std::ios::binary);
29 if (!output) {
30 std::cerr << "Failed to create: " << outputName << std::endl;
31 return;
32 }
33 protoProgram.SerializeToOstream(&output);
34 output.close();
35 }
36
GenerateProgram(const std::string & inputName,panda::pandasm::Program & prog,panda::ArenaAllocator * allocator)37 void ProtobufSnapshotGenerator::GenerateProgram(const std::string &inputName, panda::pandasm::Program &prog,
38 panda::ArenaAllocator *allocator)
39 {
40 std::fstream input(panda::os::file::File::GetExtendedFilePath(inputName), std::ios::in | std::ios::binary);
41 if (!input) {
42 std::cerr << "Failed to open: " << inputName << std::endl;
43 return;
44 }
45 protoPanda::Program proto_program;
46 if (!proto_program.ParseFromIstream(&input)) {
47 std::cerr << "Failed to parse: " << inputName << std::endl;
48 return;
49 }
50 Program::Deserialize(proto_program, prog, allocator);
51 }
52
GetCacheContext(const std::string & cacheFilePath,panda::ArenaAllocator * allocator)53 panda::es2panda::util::ProgramCache *ProtobufSnapshotGenerator::GetCacheContext(const std::string &cacheFilePath,
54 panda::ArenaAllocator *allocator)
55 {
56 std::fstream input(panda::os::file::File::GetExtendedFilePath(cacheFilePath),
57 std::ios::in | std::ios::binary);
58 if (!input) {
59 std::cout << "Cache file: " << cacheFilePath << " doesn't exist" << std::endl;
60 return nullptr;
61 }
62
63 protoPanda::ProgramCache protoCache;
64 if (!protoCache.ParseFromIstream(&input)) {
65 std::cerr << "Failed to parse cache file: " << cacheFilePath << std::endl;
66 return nullptr;
67 }
68
69 auto *program = allocator->New<panda::pandasm::Program>();
70 Program::Deserialize(protoCache.program(), *program, allocator);
71 uint32_t hashCode = protoCache.hashcode();
72 auto *programCache = allocator->New<panda::es2panda::util::ProgramCache>(hashCode, std::move(*program));
73
74 return programCache;
75 }
76
UpdateCacheFile(const panda::es2panda::util::ProgramCache * programCache,const std::string & cacheFilePath)77 void ProtobufSnapshotGenerator::UpdateCacheFile(const panda::es2panda::util::ProgramCache *programCache,
78 const std::string &cacheFilePath)
79 {
80 protoPanda::ProgramCache protoCache;
81 protoCache.set_hashcode(programCache->hashCode);
82 auto *protoProgram = protoCache.mutable_program();
83 Program::Serialize(programCache->program, *protoProgram);
84
85 std::fstream output(panda::os::file::File::GetExtendedFilePath(cacheFilePath),
86 std::ios::out | std::ios::trunc | std::ios::binary);
87 if (!output) {
88 std::cerr << "Failed to create cache file: " << cacheFilePath << std::endl;
89 return;
90 }
91 protoCache.SerializeToOstream(&output);
92 output.close();
93 }
94
95 } // panda::proto
96