This patch implements the C/C++ requirement that falling off main returns zero. The easiest way to do this is to simply initialize the return-value alloca to zero at the start of the function.

This is a pretty obvious patch, but this is my first foray into codegen, so maybe I'm doing this horridly wrong.

John.
Index: lib/CodeGen/CGCall.cpp
===================================================================
--- lib/CodeGen/CGCall.cpp      (revision 77045)
+++ lib/CodeGen/CGCall.cpp      (working copy)
@@ -505,6 +505,13 @@ void CodeGenModule::ConstructAttributeLi
 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
                                          llvm::Function *Fn,
                                          const FunctionArgList &Args) {
+
+  // If this is main, initialize the return value to zero.
+  // Implements C++ [basic.start.main]p5 and C99 5.1.2.2.3.
+  if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(CurFuncDecl))
+    if (FD->isMain())
+      Builder.CreateStore(llvm::ConstantInt::get(LLVMIntTy, 0), ReturnValue);
+
   // FIXME: We no longer need the types from FunctionArgList; lift up and
   // simplify.
 
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to