Kaleidoscope-Orc: Reuse the IRGen utility function in later chapters, and remove...
[oota-llvm.git] / examples / Kaleidoscope / Orc / lazy_codegen / toy.cpp
index 41abd27d349fa9d7982318bc156be547ed82beda..1a2043f5933cd57e86263da22d76dac1388aa09f 100644 (file)
@@ -1182,16 +1182,24 @@ private:
   LazyEmitLayerT LazyEmitLayer;
 };
 
-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(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(S, *F)) {
       S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
+      J.addModule(std::move(M));
     }
   } else {
     // Skip token for error recovery.