| 23 | namespace po = boost::program_options; |
| 24 | |
| 25 | int interpret(const std::vector<std::string>& opts, const char* argv0) { |
| 26 | po::options_description interpret_ops("interpret options"); |
| 27 | interpret_ops.add_options()( |
| 28 | "input-file,i", |
| 29 | po::value<std::vector<std::string>>()->default_value(std::vector<std::string>({"-"}), "-"), |
| 30 | "Input file, - for stdin")("optimization,O", po::value<int>()->default_value(2), |
| 31 | "Optimization value, either 0, 1, 2, or 3")( |
| 32 | "function,f", po::value<std::string>()->default_value("main"), "The function to run")( |
| 33 | "subargs", po::value<std::vector<std::string>>(), "arguments for command"); |
| 34 | |
| 35 | po::positional_options_description pos; |
| 36 | pos.add("subargs", -1); |
| 37 | |
| 38 | po::variables_map vm; |
| 39 | |
| 40 | auto parsed_options = po::command_line_parser(opts) |
| 41 | .options(interpret_ops) |
| 42 | .positional(pos) |
| 43 | .allow_unregistered() |
| 44 | .run(); |
| 45 | po::store(parsed_options, vm); |
| 46 | po::notify(vm); |
| 47 | |
| 48 | std::vector<std::string> command_opts = |
| 49 | po::collect_unrecognized(parsed_options.options, po::include_positional); |
| 50 | // add the name into it |
| 51 | command_opts.insert(command_opts.begin(), argv0); |
| 52 | |
| 53 | auto infiles = vm["input-file"].as<std::vector<std::string>>(); |
| 54 | |
| 55 | // get opt value |
| 56 | llvm::CodeGenOpt::Level optLevel; |
| 57 | switch (vm["optimization"].as<int>()) { |
| 58 | case 0: optLevel = llvm::CodeGenOpt::None; break; |
| 59 | case 1: optLevel = llvm::CodeGenOpt::Less; break; |
| 60 | case 2: optLevel = llvm::CodeGenOpt::Default; break; |
| 61 | case 3: optLevel = llvm::CodeGenOpt::Aggressive; break; |
| 62 | default: |
| 63 | std::cerr << "Unrecognized optimization level: " << vm["optimization"].as<int>() |
| 64 | << std::endl; |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | chi::Context ctx; |
| 69 | |
| 70 | // load the modules |
| 71 | std::deque<std::unique_ptr<llvm::Module>> mods; |
| 72 | for (const auto& file : infiles) { |
| 73 | llvm::SMDiagnostic err; |
| 74 | |
| 75 | // this recognizes stdin correctly |
| 76 | auto mod = |
| 77 | #if LLVM_VERSION_LESS_EQUAL(3, 5) |
| 78 | std::unique_ptr<llvm::Module>(llvm::ParseIRFile |
| 79 | #else |
| 80 | (llvm::parseIRFile |
| 81 | #endif |
| 82 | (file, err, ctx.llvmContext())); |
no test coverage detected