int32_t Context::run(ModulePtr module) { typedef int32_t (*ReturnMainPoint)(); // JIT the function, returning a function pointer. module->build(); llvm::Module* backendModule = module->getMetadata()->getBackendModule(); llvm::Function* mainFunc = backendModule->getFunction("__start"); if (mainFunc) { void *pointFunc = m_metadata->getBackendEngine()->getPointerToFunction(mainFunc); ReturnMainPoint main = reinterpret_cast<ReturnMainPoint>(reinterpret_cast<intptr_t>(pointFunc)); return main(); } BOOST_THROW_EXCEPTION(Exception() << exception_format("Not found `main` function in module `%1%`", module->getName())); }
int main(int argc, char const *argv[]) { kiwi::ManagedStatic guard; int opt = 0; // Declare the supported options. po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("version", "dump version") ("optimization,O", po::value<int>(&opt)->default_value(0), "optimization level [0-3]") ("debug", "debug parser and scanner") ("ir-dump", "dump generated LLVM module") ("run", "run script, even if --ir-dump enabled") ("input-file", po::value< std::vector< std::string > >(), "input file") ; // Add position for files po::positional_options_description p; p.add("input-file", -1); // Load options from console po::variables_map vm; try { po::store(po::command_line_parser(argc, argv). options(desc).positional(p).run(), vm); po::notify(vm); } catch (boost::program_options::error & ex ) { std::cerr << "Error: " << ex.what() << "\n"; return 2; } /// Run application ContextPtr context = Context::create(); context->setOptimizationLevel(opt); context->setDebug(vm.count("debug")); if (vm.count("input-file")) { std::vector< std::string > files = vm["input-file"].as< std::vector< std::string > >(); try { // create script module ModulePtr module = Module::create("Kiwi::Script", context); for (std::vector< std::string >::iterator i = files.begin(); i != files.end(); ++i) { module->includeFile(*i); } // build module and dump or execute if (vm.count("ir-dump")) { module->build(); module->dump(); } if (!vm.count("ir-dump") || vm.count("run")) { return context->run(module); } } catch (Exception& ex) { std::cerr << ex << "\n"; return 1; } } else if (vm.count("help")) { std::cout << desc << "\n"; } else if (vm.count("version")) { printVersion(); return 0; } return 0; }