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
21namespace panda::proto {
22void 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
39void 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
57panda::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    CHECK_NOT_NULL(program);
75    Program::Deserialize(protoCache.program(), *program, allocator);
76    uint32_t hashCode = protoCache.hashcode();
77    auto *programCache = allocator->New<panda::es2panda::util::ProgramCache>(hashCode, std::move(*program));
78
79    return programCache;
80}
81
82void ProtobufSnapshotGenerator::UpdateCacheFile(const panda::es2panda::util::ProgramCache *programCache,
83    const std::string &cacheFilePath)
84{
85    protoPanda::ProgramCache protoCache;
86    protoCache.set_hashcode(programCache->hashCode);
87    auto *protoProgram = protoCache.mutable_program();
88    Program::Serialize(programCache->program, *protoProgram);
89
90    std::fstream output = panda::es2panda::util::Helpers::FileStream<std::fstream>(
91        panda::os::file::File::GetExtendedFilePath(cacheFilePath),
92        std::ios::out | std::ios::trunc | std::ios::binary);
93    if (!output) {
94        std::cerr << "Failed to create cache file: " << cacheFilePath << std::endl;
95        return;
96    }
97    protoCache.SerializeToOstream(&output);
98    output.close();
99}
100
101} // panda::proto
102