From d9b8616c27e60719c297ce625f09d9b67a3c802c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 25 Oct 2007 04:30:35 +0000 Subject: [PATCH] Run the verifier on generated code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43327 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/tutorial/LangImpl3.html | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/docs/tutorial/LangImpl3.html b/docs/tutorial/LangImpl3.html index 7e3ed037737..49e55d35a98 100644 --- a/docs/tutorial/LangImpl3.html +++ b/docs/tutorial/LangImpl3.html @@ -400,10 +400,6 @@ last function we compiled.

Builder.SetInsertPoint(BB); if (Value *RetVal = Body->Codegen()) { - // Finish off the function. - Builder.CreateRet(RetVal); - return TheFunction; - } @@ -417,12 +413,29 @@ href="http://en.wikipedia.org/wiki/Control_flow_graph">Control Flow Graph. Since we don't have any control flow, our functions will only contain one block so far. We'll fix this in a future installment :).

+
+
+  if (Value *RetVal = Body->Codegen()) {
+    // Finish off the function.
+    Builder.CreateRet(RetVal);
+    
+    // Validate the generated code, checking for consistency.
+    verifyFunction(*TheFunction);
+    return TheFunction;
+  }
+
+
+

Once the insertion point is set up, we call the CodeGen() method for the root expression of the function. If no error happens, this emits code to compute the expression into the entry block and returns the value that was computed. Assuming no error, we then create an LLVM ret instruction. This completes the function, -which is then returned.

+href="../LangRef.html#i_ret">ret instruction, which completes the function. +Once the function is built, we call the verifyFunction function, which +is provided by LLVM. This function does a variety of consistency checks on the +generated code, to determine if our compiler is doing everything right. Using +this is important: it can catch a lot of bugs. Once the function is finished +and validated, we return it.

@@ -625,6 +638,7 @@ our makefile/command line about which options to use:

#include "llvm/DerivedTypes.h" #include "llvm/Module.h" +#include "llvm/Analysis/Verifier.h" #include "llvm/Support/LLVMBuilder.h" #include <cstdio> #include <string> @@ -1071,6 +1085,9 @@ Function *FunctionAST::Codegen() { if (Value *RetVal = Body->Codegen()) { // Finish off the function. Builder.CreateRet(RetVal); + + // Validate the generated code, checking for consistency. + verifyFunction(*TheFunction); return TheFunction; } -- 2.34.1