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 "program_dump.h"
18 #include "abc2program_options.h"
19
20 namespace ark::abc2program {
21
Run(int argc,const char ** argv)22 int Abc2ProgramDriver::Run(int argc, const char **argv)
23 {
24 Abc2ProgramOptions options;
25 if (!options.Parse(argc, argv)) {
26 return 1;
27 }
28 if (Run(options.GetInputFilePath(), options.GetOutputFilePath())) {
29 return 0;
30 }
31 return 1;
32 }
33
Run(const std::string & inputFilePath,const std::string & outputFilePath)34 bool Abc2ProgramDriver::Run(const std::string &inputFilePath, const std::string &outputFilePath)
35 {
36 return (Compile(inputFilePath) && Dump(outputFilePath));
37 }
38
Compile(const std::string & inputFilePath)39 bool Abc2ProgramDriver::Compile(const std::string &inputFilePath)
40 {
41 return Compile(inputFilePath, program_);
42 }
43
Compile(const std::string & inputFilePath,pandasm::Program & program)44 bool Abc2ProgramDriver::Compile(const std::string &inputFilePath, pandasm::Program &program)
45 {
46 // abc file compile logic
47 if (!compiler_.OpenAbcFile(inputFilePath)) {
48 return false;
49 }
50 return compiler_.FillProgramData(program);
51 }
52
Dump(const std::string & outputFilePath)53 bool Abc2ProgramDriver::Dump(const std::string &outputFilePath)
54 {
55 // program dump logic
56 std::ofstream ofs;
57 ofs.open(outputFilePath, std::ios::trunc | std::ios::out);
58 PandasmProgramDumper dumper(compiler_.GetAbcFile(), compiler_.GetAbcStringTable());
59 dumper.Dump(ofs, program_);
60 ofs.close();
61 return true;
62 }
63
GetProgram() const64 const pandasm::Program &Abc2ProgramDriver::GetProgram() const
65 {
66 return program_;
67 }
68
GetProgram()69 pandasm::Program &Abc2ProgramDriver::GetProgram()
70 {
71 return program_;
72 }
73
74 } // namespace ark::abc2program