1 /*
2  * Copyright (c) 2024 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 "abc2program_driver.h"
17 #include "abc2program_options.h"
18 #include "abc_class_processor.h"
19 #include "program_dump.h"
20 
21 namespace panda::abc2program {
22 
Run(int argc, const char **argv)23 int Abc2ProgramDriver::Run(int argc, const char **argv)
24 {
25     Abc2ProgramOptions options;
26     if (!options.Parse(argc, argv)) {
27         return 1;
28     }
29     if (Run(options.GetInputFilePath(), options.GetOutputFilePath())) {
30         return 0;
31     }
32     return 1;
33 }
34 
Run(const std::string &input_file_path, const std::string &output_file_path)35 bool Abc2ProgramDriver::Run(const std::string &input_file_path, const std::string &output_file_path)
36 {
37     return (Compile(input_file_path) && Dump(output_file_path));
38 }
39 
Compile(const std::string &input_file_path)40 bool Abc2ProgramDriver::Compile(const std::string &input_file_path)
41 {
42     input_file_path_ = input_file_path;
43     if (!compiler_.OpenAbcFile(input_file_path)) {
44         return false;
45     }
46     program_ = std::move(*compiler_.CompileAbcFile());
47     return true;
48 }
49 
Dump(const std::string &output_file_path)50 bool Abc2ProgramDriver::Dump(const std::string &output_file_path)
51 {
52     // program dump logic
53     std::ofstream ofs;
54     ofs.open(output_file_path, std::ios::trunc | std::ios::out);
55     PandasmProgramDumper dumper;
56     dumper.SetAbcFilePath(input_file_path_);
57     dumper.Dump(ofs, program_);
58     ofs.close();
59     return true;
60 }
61 
GetProgram() const62 const pandasm::Program &Abc2ProgramDriver::GetProgram() const
63 {
64     return program_;
65 }
66 
67 }  // namespace panda::abc2program