From becafb4cd505f7a27923c98bfec753b0a5cf2be0 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Sat, 7 Mar 2015 19:43:53 +0100 Subject: [PATCH] Plug new bitcode generator in main. --- CMakeLists.txt | 2 +- main.cpp | 48 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 481ea2c..cfdc53a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ elseif(BISON_VERSION VERSION_LESS 3.0) endif() ## 3. External components -find_package(Boost 1.54 REQUIRED) +find_package(Boost 1.48 REQUIRED regex) find_package(LLVM REQUIRED CONFIG) add_definitions( diff --git a/main.cpp b/main.cpp index c30546c..4e86ca4 100644 --- a/main.cpp +++ b/main.cpp @@ -20,26 +20,52 @@ #include "Scanner.hpp" #include "Parser.hpp" #include "CppEmitter.hpp" +#include "BitcodeEmitter.hpp" + +#include +#include +#include + +#include #include #include +#include +#include using namespace monicelli; -int main() { - Program program; - Scanner scanner(&std::cin); - Parser parser(scanner, program); -#if YYDEBUG - parser.set_debug_level(1); -#endif +int main(int argc, char **argv) { + for (int i = 1; i < argc; ++i) { + std::string inputname(argv[i]); + std::ifstream instream(inputname); - parser.parse(); - CppEmitter emitter(&std::cout); + Program program; + Scanner scanner(&instream); + Parser parser(scanner, program); + BitcodeEmitter emitter; - program.emit(&emitter); +# if YYDEBUG + parser.set_debug_level(1); +# endif - return 0; + parser.parse(); + + boost::regex namere("^(.+)\\.mc$"); + std::string outputname; + + if (boost::regex_match(inputname, namere)) { + outputname = boost::regex_replace(inputname, namere, "$1.bc"); + } else { + outputname = inputname + ".bc"; + } + + if (!program.emit(&emitter)) return 1; + + std::ofstream outstream(outputname); + llvm::raw_os_ostream stream(outstream); + llvm::WriteBitcodeToFile(&emitter.getModule(), stream); + } }