Пример #1
0
int run(unique_ptr<Module> module, Chars_const fnName, const vector<string> args) {
  
  check(module, "no module");
  
  Function* entryFn = module->getFunction(fnName);
  check(entryFn, "Source file does not contain a function of the given name");
  
  // Use an EngineBuilder to configure and construct an MCJIT ExecutionEngine.
#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
  EngineBuilder builder(module.release());
  builder.setUseMCJIT(true);
#else
  const EngineBuilder builder(move(module));
  const SectionMemoryManager mm;
  builder.setMCJITMemoryManager(&mm);
#endif
  string errorStr;
  builder.setErrorStr(&errorStr);
  builder.setEngineKind(EngineKind::JIT);
  builder.setOptLevel(CodeGenOpt::None);
  
  ExecutionEngine* engine = builder.create();
  
  // Call 'finalizeObject' to notify the JIT that we're ready to execute the jitted code,
  // then run the static constructors.
  engine->finalizeObject();
  engine->runStaticConstructorsDestructors(false);
  
  // Pass the args to the jitted function, and capture the result.
  const int result = engine->runFunctionAsMain(entryFn, args, nullptr);
  engine->runStaticConstructorsDestructors(true); // Run the static destructors.
  return result;
}
Пример #2
0
/* Executes the AST by running the main function */
void CodeGenContext::runCode() {
    std::cout << "Running code...\n";
    assert(module != nullptr);
    InitializeNativeTarget();
	ExecutionEngine *ee = ExecutionEngine::create(module, nullptr);
    assert(ee != nullptr);
	vector<string> noargs;
    const char *const * noenv = nullptr;
    assert(mainFunction != nullptr);
    int v = ee->runFunctionAsMain(mainFunction, noargs, noenv);
    
    std::cout << "Code was run.\n";
	return;
}