10
10
#include " llvm/Support/TargetSelect.h"
11
11
#include " llvm/IR/Verifier.h"
12
12
#include " llvm/Support/Error.h"
13
+ #include < boost/program_options.hpp>
14
+
15
+ namespace po = boost::program_options;
16
+
17
+ int main (int argc, char *argv[]) {
18
+ std::string inputName;
19
+ bool printAST = false ;
20
+ bool printIR = false ;
21
+ bool execute = false ;
22
+ unsigned int ac = 0 ;
23
+ std::string av{};
24
+
25
+ po::options_description desc (" Allowed options" );
26
+ desc.add_options ()
27
+ (" help" , " produce help message" )
28
+ (" print-ast" , " print the abstract syntax tree" )
29
+ (" print-ir" , " print the generated IR code" )
30
+ (" execute" , " execute main function with parameters" )
31
+ (" input-file" , po::value<std::vector<std::string> >(), " input file" );
32
+ po::positional_options_description p;
33
+ p.add (" input-file" , -1 );
34
+
35
+ po::variables_map vm;
36
+ po::store (po::command_line_parser (argc, argv).options (desc).positional (p).run (), vm);
37
+ po::notify (vm);
38
+
39
+ if (vm.empty ()) {
40
+ std::cout << " no input files" << std::endl;
41
+ return 0 ;
42
+ }
43
+
44
+ if (vm.count (" help" )) {
45
+ std::cout << " Usage: mycc [options] file..." << std::endl;
46
+ std::cout << desc << std::endl;
47
+ return 0 ;
48
+ }
49
+
50
+ if (vm.count (" input-file" )) {
51
+ auto v = vm[" input-file" ].as <std::vector<std::string>>();
52
+ if (v.size () > 1 ) {
53
+ std::cout << " multiple input files detected, currently do not support" ;
54
+ }
55
+ inputName = v[0 ];
56
+ }
57
+
58
+ if (vm.count (" print-ast" )) {
59
+ printAST = true ;
60
+ }
61
+
62
+ if (vm.count (" print-ir" )) {
63
+ printIR = true ;
64
+ }
65
+
66
+ if (vm.count (" execute" )) {
67
+ execute = true ;
68
+ }
13
69
14
- int main () {
15
70
llvm::InitializeNativeTarget ();
16
71
llvm::InitializeNativeTargetAsmPrinter ();
17
72
llvm::InitializeNativeTargetAsmParser ();
18
73
19
-
20
- // try {
21
74
std::ifstream testFile;
22
- testFile.open (" test.c " );
75
+ testFile.open (inputName. c_str () );
23
76
auto parser = Parser (testFile);
24
77
auto tr = parser.parseTranslationUnit ();
25
- // tr->print(0);
78
+ if (printAST) {
79
+ tr->print (0 );
80
+ }
26
81
tr->codegen ();
27
- AST::getModule ()->print (llvm::outs (), nullptr );
28
- // } catch (const SemaException &e) {
29
- // std::cerr << e.what() << std::endl;
30
- // } catch (const ParserException &e) {
31
- // std::cerr << e.what() << std::endl;
32
- // }
82
+ if (printIR) {
83
+ AST::getModule ()->print (llvm::outs (), nullptr );
84
+ }
33
85
34
86
if (llvm::verifyModule (*AST::getModule (), &llvm::errs ())) {
35
87
return 0 ;
36
- } else {
37
- std::cout << " Verified success\n " ;
38
- }
39
-
40
- std::string error;
41
- auto engine = llvm::EngineBuilder (AST::takeModule ())
42
- .setErrorStr (&error)
43
- .setOptLevel (llvm::CodeGenOpt::Aggressive)
44
- .setEngineKind (llvm::EngineKind::JIT)
45
- .create ();
46
- if (!engine) {
47
- std::cerr << " EE Error: " << error << ' \n ' ;
48
- return 1 ;
49
- }
50
-
51
- engine->finalizeObject ();
52
- typedef void (*Function)();
53
- Function f = reinterpret_cast <Function>(
54
- engine->getPointerToNamedFunction (" main" ));
55
- f ();
88
+ }
89
+
90
+ if (execute) {
91
+ std::string error;
92
+ auto engine = llvm::EngineBuilder (AST::takeModule ())
93
+ .setErrorStr (&error)
94
+ .setOptLevel (llvm::CodeGenOpt::Aggressive)
95
+ .setEngineKind (llvm::EngineKind::JIT)
96
+ .create ();
97
+ if (!engine) {
98
+ std::cerr << " EE Error: " << error << ' \n ' ;
99
+ return 1 ;
100
+ }
101
+
102
+ engine->finalizeObject ();
103
+ typedef int (*Function)();
104
+ Function f = reinterpret_cast <Function>(
105
+ engine->getPointerToNamedFunction (" main" ));
106
+ f ();
107
+ }
108
+ return 0 ;
56
109
}
0 commit comments