Kaleidoscope-Orc: Extract IRGen work into a utility function.
[oota-llvm.git] / examples / Kaleidoscope / Orc / initial / toy.cpp
index 6daa8d5e92400dc7290c62b946edc3c31a1a5847..1b772259a19ef8f384e644e0fa81f6d761b68d4c 100644 (file)
@@ -1180,16 +1180,24 @@ private:
   CompileLayerT CompileLayer;
 };
 
-static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
-  if (auto F = ParseDefinition()) {
-    IRGenContext C(S);
-    if (auto LF = F->IRGen(C)) {
+static std::unique_ptr<llvm::Module>
+IRGen(KaleidoscopeJIT &J, SessionContext &S, const FunctionAST &F) {
+  IRGenContext C(S);
+  auto LF = F.IRGen(C);
+  if (!LF)
+    return nullptr;
 #ifndef MINIMAL_STDERR_OUTPUT
-      std::cerr << "Read function definition:\n";
-      LF->dump();
+  fprintf(stderr, "Read function definition:");
+  LF->dump();
 #endif
-      J.addModule(C.takeM());
+  return C.takeM();
+}
+
+static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
+  if (auto F = ParseDefinition()) {
+    if (auto M = IRGen(J, S, *F)) {
       S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
+      J.addModule(std::move(M));
     }
   } else {
     // Skip token for error recovery.