• 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 #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         return nullptr;
60     }
61 
62     protoPanda::ProgramCache protoCache;
63     if (!protoCache.ParseFromIstream(&input)) {
64         std::cerr << "Failed to parse cache file: " << cacheFilePath << std::endl;
65         return nullptr;
66     }
67 
68     auto *program = allocator->New<panda::pandasm::Program>();
69     Program::Deserialize(protoCache.program(), *program, allocator);
70     uint32_t hashCode = protoCache.hashcode();
71     auto *programCache = allocator->New<panda::es2panda::util::ProgramCache>(hashCode, std::move(*program));
72 
73     return programCache;
74 }
75 
UpdateCacheFile(const panda::es2panda::util::ProgramCache * programCache,const std::string & cacheFilePath)76 void ProtobufSnapshotGenerator::UpdateCacheFile(const panda::es2panda::util::ProgramCache *programCache,
77     const std::string &cacheFilePath)
78 {
79     protoPanda::ProgramCache protoCache;
80     protoCache.set_hashcode(programCache->hashCode);
81     auto *protoProgram = protoCache.mutable_program();
82     Program::Serialize(programCache->program, *protoProgram);
83 
84     std::fstream output(panda::os::file::File::GetExtendedFilePath(cacheFilePath),
85         std::ios::out | std::ios::trunc | std::ios::binary);
86     if (!output) {
87         std::cerr << "Failed to create cache file: " << cacheFilePath << std::endl;
88         return;
89     }
90     protoCache.SerializeToOstream(&output);
91     output.close();
92 }
93 
94 } // panda::proto
95