1b1994897Sopenharmony_ci/*
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#include "disassembler.h"
17b1994897Sopenharmony_ci
18b1994897Sopenharmony_ci#include "utils/logger.h"
19b1994897Sopenharmony_ci#include "utils/pandargs.h"
20b1994897Sopenharmony_ci#include "ark_version.h"
21b1994897Sopenharmony_ci#include "file_format_version.h"
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_civoid PrintHelp(panda::PandArgParser &pa_parser)
24b1994897Sopenharmony_ci{
25b1994897Sopenharmony_ci    std::cerr << "Usage:" << std::endl;
26b1994897Sopenharmony_ci    std::cerr << "ark_disasm [options] input_file output_file" << std::endl << std::endl;
27b1994897Sopenharmony_ci    std::cerr << "Supported options:" << std::endl << std::endl;
28b1994897Sopenharmony_ci    std::cerr << pa_parser.GetHelpString() << std::endl;
29b1994897Sopenharmony_ci}
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_civoid Disassemble(const std::string &input_file, const std::string &output_file, const bool verbose, const bool quiet,
32b1994897Sopenharmony_ci                 const bool skip_strings)
33b1994897Sopenharmony_ci{
34b1994897Sopenharmony_ci    LOG(DEBUG, DISASSEMBLER) << "[initializing disassembler]\nfile: " << input_file << "\n";
35b1994897Sopenharmony_ci
36b1994897Sopenharmony_ci    panda::disasm::Disassembler disasm {};
37b1994897Sopenharmony_ci    disasm.Disassemble(input_file, quiet, skip_strings);
38b1994897Sopenharmony_ci    if (verbose) {
39b1994897Sopenharmony_ci        disasm.CollectInfo();
40b1994897Sopenharmony_ci    }
41b1994897Sopenharmony_ci
42b1994897Sopenharmony_ci    LOG(DEBUG, DISASSEMBLER) << "[serializing results]\n";
43b1994897Sopenharmony_ci
44b1994897Sopenharmony_ci    std::ofstream res_pa;
45b1994897Sopenharmony_ci    res_pa.open(output_file, std::ios::trunc | std::ios::out);
46b1994897Sopenharmony_ci    disasm.Serialize(res_pa, true, verbose);
47b1994897Sopenharmony_ci    res_pa.close();
48b1994897Sopenharmony_ci}
49b1994897Sopenharmony_ci
50b1994897Sopenharmony_cibool ProcessArgs(panda::PandArgParser &pa_parser, const panda::PandArg<std::string> &input_file,
51b1994897Sopenharmony_ci                 const panda::PandArg<std::string> &output_file, panda::PandArg<bool> &debug,
52b1994897Sopenharmony_ci                 const panda::PandArg<std::string> &debug_file, const panda::PandArg<bool> &help,
53b1994897Sopenharmony_ci                 const panda::PandArg<bool> &version, int argc, const char **argv)
54b1994897Sopenharmony_ci{
55b1994897Sopenharmony_ci    if (!pa_parser.Parse(argc, argv)) {
56b1994897Sopenharmony_ci        PrintHelp(pa_parser);
57b1994897Sopenharmony_ci        return false;
58b1994897Sopenharmony_ci    }
59b1994897Sopenharmony_ci
60b1994897Sopenharmony_ci    if (version.GetValue()) {
61b1994897Sopenharmony_ci        panda::PrintPandaVersion();
62b1994897Sopenharmony_ci        panda::panda_file::PrintBytecodeVersion();
63b1994897Sopenharmony_ci        return false;
64b1994897Sopenharmony_ci    }
65b1994897Sopenharmony_ci
66b1994897Sopenharmony_ci    if (input_file.GetValue().empty() || output_file.GetValue().empty() || help.GetValue()) {
67b1994897Sopenharmony_ci        PrintHelp(pa_parser);
68b1994897Sopenharmony_ci        return false;
69b1994897Sopenharmony_ci    }
70b1994897Sopenharmony_ci
71b1994897Sopenharmony_ci    panda::Logger::ComponentMask log_mask = panda::Logger::ComponentMask().set(panda::Logger::Component::DISASSEMBLER)
72b1994897Sopenharmony_ci                                                                          .set(panda::Logger::Component::PANDAFILE);
73b1994897Sopenharmony_ci    if (debug.GetValue()) {
74b1994897Sopenharmony_ci        if (debug_file.GetValue().empty()) {
75b1994897Sopenharmony_ci            panda::Logger::InitializeStdLogging(
76b1994897Sopenharmony_ci                panda::Logger::Level::DEBUG, log_mask);
77b1994897Sopenharmony_ci        } else {
78b1994897Sopenharmony_ci            panda::Logger::InitializeFileLogging(
79b1994897Sopenharmony_ci                debug_file.GetValue(), panda::Logger::Level::DEBUG, log_mask);
80b1994897Sopenharmony_ci        }
81b1994897Sopenharmony_ci    } else {
82b1994897Sopenharmony_ci        panda::Logger::InitializeStdLogging(panda::Logger::Level::ERROR, log_mask);
83b1994897Sopenharmony_ci    }
84b1994897Sopenharmony_ci
85b1994897Sopenharmony_ci    return true;
86b1994897Sopenharmony_ci}
87b1994897Sopenharmony_ci
88b1994897Sopenharmony_ciint main(int argc, const char **argv)
89b1994897Sopenharmony_ci{
90b1994897Sopenharmony_ci    panda::PandArg<bool> help("help", false, "Print this message and exit");
91b1994897Sopenharmony_ci    panda::PandArg<bool> verbose("verbose", false, "enable informative code output");
92b1994897Sopenharmony_ci    panda::PandArg<bool> quiet("quiet", false, "enables all of the --skip-* flags");
93b1994897Sopenharmony_ci    panda::PandArg<bool> skip_strings(
94b1994897Sopenharmony_ci        "skip-string-literals", false,
95b1994897Sopenharmony_ci        "replaces string literals with their respectie id's, thus shortening emitted code size");
96b1994897Sopenharmony_ci    panda::PandArg<bool> debug(
97b1994897Sopenharmony_ci        "debug", false, "enable debug messages (will be printed to standard output if no --debug-file was specified) ");
98b1994897Sopenharmony_ci    panda::PandArg<std::string> debug_file("debug-file", "",
99b1994897Sopenharmony_ci                                           "(--debug-file FILENAME) set debug file name. default is std::cout");
100b1994897Sopenharmony_ci    panda::PandArg<std::string> input_file("input_file", "", "Path to the source binary code");
101b1994897Sopenharmony_ci    panda::PandArg<std::string> output_file("output_file", "", "Path to the generated assembly code");
102b1994897Sopenharmony_ci    panda::PandArg<bool> version {"version", false,
103b1994897Sopenharmony_ci                                  "Ark version, file format version and minimum supported file format version"};
104b1994897Sopenharmony_ci
105b1994897Sopenharmony_ci    panda::PandArgParser pa_parser;
106b1994897Sopenharmony_ci
107b1994897Sopenharmony_ci    pa_parser.Add(&help);
108b1994897Sopenharmony_ci    pa_parser.Add(&verbose);
109b1994897Sopenharmony_ci    pa_parser.Add(&quiet);
110b1994897Sopenharmony_ci    pa_parser.Add(&skip_strings);
111b1994897Sopenharmony_ci    pa_parser.Add(&debug);
112b1994897Sopenharmony_ci    pa_parser.Add(&debug_file);
113b1994897Sopenharmony_ci    pa_parser.Add(&version);
114b1994897Sopenharmony_ci    pa_parser.PushBackTail(&input_file);
115b1994897Sopenharmony_ci    pa_parser.PushBackTail(&output_file);
116b1994897Sopenharmony_ci    pa_parser.EnableTail();
117b1994897Sopenharmony_ci
118b1994897Sopenharmony_ci    if (!ProcessArgs(pa_parser, input_file, output_file, debug, debug_file, help, version, argc, argv)) {
119b1994897Sopenharmony_ci        return 1;
120b1994897Sopenharmony_ci    }
121b1994897Sopenharmony_ci
122b1994897Sopenharmony_ci    Disassemble(input_file.GetValue(), output_file.GetValue(), verbose.GetValue(), quiet.GetValue(),
123b1994897Sopenharmony_ci                skip_strings.GetValue());
124b1994897Sopenharmony_ci
125b1994897Sopenharmony_ci    pa_parser.DisableTail();
126b1994897Sopenharmony_ci
127b1994897Sopenharmony_ci    return 0;
128b1994897Sopenharmony_ci}
129