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