Skip to content

Commit 0a8f220

Browse files
author
Liu Yafei
committed
add argv parsing
1 parent 8b73d11 commit 0a8f220

File tree

2 files changed

+93
-38
lines changed

2 files changed

+93
-38
lines changed

CMakeLists.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(myccpiler)
2+
project(mycc)
33

44
set(CMAKE_CXX_STANDARD 14)
55
#set (CMAKE_C_COMPILER clang)
66
#set (CMAKE_CXX_COMPILER clang++)
77

88
find_package(LLVM REQUIRED CONFIG)
9+
find_package(Boost REQUIRED COMPONENTS program_options)
910

10-
add_executable(myccpiler
11+
add_executable(mycc
1112
main.cpp
1213
src/lex/lex.cpp
1314
src/parser/parser.cpp
1415
src/sema/ast.cpp src/sema/symbol_tables.cpp src/sema/types.cpp includes/sema/qualifiedType.h src/sema/qualifiedType.cpp src/sema/value.cpp)
15-
target_include_directories(myccpiler PUBLIC includes)
16+
target_include_directories(mycc PUBLIC includes)
1617

1718
llvm_map_components_to_libnames(llvm_libs support core irreader mc mcjit native interpreter)
18-
target_include_directories(myccpiler PUBLIC ${LLVM_INCLUDE_DIRS})
19-
target_compile_definitions(myccpiler PUBLIC ${LLVM_DEFINITIONS})
20-
target_link_libraries(myccpiler ${llvm_libs})
19+
target_include_directories(mycc PUBLIC ${LLVM_INCLUDE_DIRS})
20+
target_compile_definitions(mycc PUBLIC ${LLVM_DEFINITIONS})
21+
target_link_libraries(mycc ${llvm_libs})
22+
target_link_libraries(mycc Boost::program_options)
2123

2224
add_executable(first_sets src/tools/main.cpp src/tools/first_set_generator.cpp)
23-
target_include_directories(first_sets PUBLIC includes)
25+
target_include_directories(first_sets PUBLIC includes)

main.cpp

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,100 @@
1010
#include "llvm/Support/TargetSelect.h"
1111
#include "llvm/IR/Verifier.h"
1212
#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+
}
1369

14-
int main() {
1570
llvm::InitializeNativeTarget();
1671
llvm::InitializeNativeTargetAsmPrinter();
1772
llvm::InitializeNativeTargetAsmParser();
1873

19-
20-
// try {
2174
std::ifstream testFile;
22-
testFile.open("test.c");
75+
testFile.open(inputName.c_str());
2376
auto parser = Parser(testFile);
2477
auto tr = parser.parseTranslationUnit();
25-
// tr->print(0);
78+
if (printAST) {
79+
tr->print(0);
80+
}
2681
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+
}
3385

3486
if (llvm::verifyModule(*AST::getModule(), &llvm::errs())) {
3587
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;
56109
}

0 commit comments

Comments
 (0)