Exemple #1
0
static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
  // Evaluate a top-level expression into an anonymous function.
  if (auto F = ParseTopLevelExpr()) {
    IRGenContext C(S);
    if (auto ExprFunc = F->IRGen(C)) {
#ifndef MINIMAL_STDERR_OUTPUT
      std::cerr << "Expression function:\n";
      ExprFunc->dump();
#endif
      // Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove
      // this module as soon as we've executed Function ExprFunc.
      auto H = J.addModule(C.takeM());

      // Get the address of the JIT'd function in memory.
      auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");

      // Cast it to the right type (takes no arguments, returns a double) so we
      // can call it as a native function.
      double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
#ifdef MINIMAL_STDERR_OUTPUT
      FP();
#else
      std::cerr << "Evaluated to " << FP() << "\n";
#endif

      // Remove the function.
      J.removeModule(H);
    }
  } else {
    // Skip token for error recovery.
    getNextToken();
  }
}
Exemple #2
0
static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
  if (auto F = ParseDefinition()) {
    S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
    J.addFunctionAST(std::move(F));
  } else {
    // Skip token for error recovery.
    getNextToken();
  }
}