• 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 "protobufSnapshotGenerator.h"
17 
18 namespace panda::proto {
GenerateSnapshot(const panda::pandasm::Program & program,const std::string & outputName)19 void ProtobufSnapshotGenerator::GenerateSnapshot(const panda::pandasm::Program &program, const std::string &outputName)
20 {
21     protoPanda::Program protoProgram;
22 
23     Program::Serialize(program, protoProgram);
24     ProtobufSnapshotGenerator::SerializeToProtoPath<protoPanda::Program>(protoProgram, outputName);
25 }
26 
GenerateProgram(const std::string & inputName,panda::pandasm::Program & prog,panda::ArenaAllocator * allocator)27 void ProtobufSnapshotGenerator::GenerateProgram(const std::string &inputName, panda::pandasm::Program &prog,
28                                                 panda::ArenaAllocator *allocator)
29 {
30     std::fstream input = panda::es2panda::util::Helpers::FileStream<std::fstream>(
31         panda::os::file::File::GetExtendedFilePath(inputName),
32         std::ios::in | std::ios::binary);
33     if (!input) {
34         std::cerr << "Failed to open: " << inputName << std::endl;
35         return;
36     }
37     protoPanda::Program proto_program;
38     if (!proto_program.ParseFromIstream(&input)) {
39         std::cerr << "Failed to parse: " << inputName << std::endl;
40         return;
41     }
42     Program::Deserialize(proto_program, prog, allocator);
43 }
44 
GetCacheContext(const std::string & cacheFilePath,panda::ArenaAllocator * allocator)45 panda::es2panda::util::ProgramCache *ProtobufSnapshotGenerator::GetCacheContext(const std::string &cacheFilePath,
46     panda::ArenaAllocator *allocator)
47 {
48     std::fstream input = panda::es2panda::util::Helpers::FileStream<std::fstream>(
49         panda::os::file::File::GetExtendedFilePath(cacheFilePath),
50         std::ios::in | std::ios::binary);
51     if (!input) {
52         return nullptr;
53     }
54 
55     protoPanda::ProgramCache protoCache;
56     if (!protoCache.ParseFromIstream(&input)) {
57         std::cerr << "Failed to parse cache file: " << cacheFilePath << std::endl;
58         return nullptr;
59     }
60 
61     auto *program = allocator->New<panda::pandasm::Program>();
62     CHECK_NOT_NULL(program);
63     Program::Deserialize(protoCache.program(), *program, allocator);
64     uint32_t hashCode = protoCache.hashcode();
65     auto *programCache = allocator->New<panda::es2panda::util::ProgramCache>(hashCode, std::move(*program));
66 
67     return programCache;
68 }
69 
UpdateCacheFile(const panda::es2panda::util::ProgramCache * programCache,const std::string & cacheFilePath)70 void ProtobufSnapshotGenerator::UpdateCacheFile(const panda::es2panda::util::ProgramCache *programCache,
71     const std::string &cacheFilePath)
72 {
73     protoPanda::ProgramCache protoCache;
74     protoCache.set_hashcode(programCache->hashCode);
75     auto *protoProgram = protoCache.mutable_program();
76     Program::Serialize(programCache->program, *protoProgram);
77     ProtobufSnapshotGenerator::SerializeToProtoPath<protoPanda::ProgramCache>(protoCache, cacheFilePath);
78 }
79 
GetAbcInputCacheContext(const std::string & cacheFilePath,panda::ArenaAllocator * allocator)80 panda::es2panda::util::AbcProgramsCache *ProtobufSnapshotGenerator::GetAbcInputCacheContext(
81     const std::string &cacheFilePath, panda::ArenaAllocator *allocator)
82 {
83     std::fstream input = panda::es2panda::util::Helpers::FileStream<std::fstream>(
84         panda::os::file::File::GetExtendedFilePath(cacheFilePath),
85         std::ios::in | std::ios::binary);
86     if (!input) {
87         return nullptr;
88     }
89 
90     protoPanda::AbcProtoProgramsCache abcProtoCache;
91     if (!abcProtoCache.ParseFromIstream(&input)) {
92         std::cerr << "Failed to parse cache file: " << cacheFilePath << std::endl;
93         return nullptr;
94     }
95 
96     std::map<std::string, panda::es2panda::util::ProgramCache *> abcProgsInfo {};
97     for (const auto &item : abcProtoCache.abcprotoprogramsmap()) {
98         auto *program = new panda::pandasm::Program();
99         auto &protoName = item.name();
100         auto &protoProgram = item.program();
101         Program::Deserialize(protoProgram, *program, allocator);
102         auto *programCache = allocator->New<panda::es2panda::util::ProgramCache>(std::move(*program));
103         CHECK_NOT_NULL(programCache);
104         abcProgsInfo.emplace(protoName, programCache);
105         delete program;
106         program = nullptr;
107     }
108 
109     uint32_t hashCode = abcProtoCache.hashcode();
110     auto *abcProgarmsCache = allocator->New<panda::es2panda::util::AbcProgramsCache>(hashCode, abcProgsInfo);
111     CHECK_NOT_NULL(abcProgarmsCache);
112 
113     return abcProgarmsCache;
114 }
115 
UpdateAbcCacheFile(const panda::es2panda::util::AbcProgramsCache * abcProgramsCache,const std::string & cacheFilePath)116 void ProtobufSnapshotGenerator::UpdateAbcCacheFile(const panda::es2panda::util::AbcProgramsCache *abcProgramsCache,
117     const std::string &cacheFilePath)
118 {
119     protoPanda::AbcProtoProgramsCache abcProtoCache;
120     abcProtoCache.set_hashcode(abcProgramsCache->hashCode);
121 
122     for (const auto &pair : abcProgramsCache->programsCache) {
123         auto *abcProtoProgramItem = abcProtoCache.add_abcprotoprogramsmap();
124         abcProtoProgramItem->set_name(pair.first);
125         auto *protoProgram = abcProtoProgramItem->mutable_program();
126         Program::Serialize(pair.second->program, *protoProgram);
127     }
128     ProtobufSnapshotGenerator::SerializeToProtoPath<protoPanda::AbcProtoProgramsCache>(abcProtoCache, cacheFilePath);
129 }
130 
131 } // panda::proto
132