[cfe-commits] r39547 - in /cfe/cfe/trunk/CodeGen: CGStmt.cpp CodeGenFunction.cpp CodeGenFunction.h

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:45:28 PDT 2007


Author: clattner
Date: Wed Jul 11 11:45:28 2007
New Revision: 39547

URL: http://llvm.org/viewvc/llvm-project?rev=39547&view=rev
Log:
implement codegen support for return of void and simple scalars.

Modified:
    cfe/cfe/trunk/CodeGen/CGStmt.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.h

Modified: cfe/cfe/trunk/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CGStmt.cpp?rev=39547&r1=39546&r2=39547&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGStmt.cpp Wed Jul 11 11:45:28 2007
@@ -43,6 +43,7 @@
   case Stmt::LabelStmtClass:    EmitLabelStmt(cast<LabelStmt>(*S));       break;
   case Stmt::GotoStmtClass:     EmitGotoStmt(cast<GotoStmt>(*S));         break;
   case Stmt::IfStmtClass:       EmitIfStmt(cast<IfStmt>(*S));             break;
+  case Stmt::ReturnStmtClass:   EmitReturnStmt(cast<ReturnStmt>(*S));     break;
   }
 }
 
@@ -178,3 +179,36 @@
   EmitBlock(ContBlock);
 }
 
+/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
+/// if the function returns void, or may be missing one if the function returns
+/// non-void.  Fun stuff :).
+void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
+  ExprResult RetVal;
+  
+  // Emit the result value, even if unused, to evalute the side effects.
+  const Expr *RV = S.getRetValue();
+  if (RV)
+    RetVal = EmitExpr(RV);
+  
+  if (CurFuncDecl->getType()->isVoidType()) {
+    // If the function returns void, emit ret void, and ignore the retval.
+    Builder.CreateRetVoid();
+  } else if (RV == 0) {
+    // "return;" in a function that returns a value.
+    const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
+    if (RetTy == llvm::Type::VoidTy)
+      Builder.CreateRetVoid();   // struct return etc.
+    else
+      Builder.CreateRet(llvm::UndefValue::get(RetTy));
+  } else if (RetVal.isScalar()) {
+    // FIXME: return should coerce its operand to the return type!
+    Builder.CreateRet(RetVal.getVal());
+  } else {
+    assert(0 && "FIXME: aggregate return unimp");
+  }
+  
+  // Emit a block after the branch so that dead code after a goto has some place
+  // to go.
+  Builder.SetInsertPoint(new BasicBlock("", CurFn));
+}
+

Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp?rev=39547&r1=39546&r2=39547&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp Wed Jul 11 11:45:28 2007
@@ -116,6 +116,7 @@
   const llvm::FunctionType *Ty = 
     cast<llvm::FunctionType>(ConvertType(FD->getType(), FD->getLocation()));
   
+  CurFuncDecl = FD;
   CurFn = new Function(Ty, Function::ExternalLinkage,
                        FD->getName(), &CGM.getModule());
   

Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.h?rev=39547&r1=39546&r2=39547&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:45:28 2007
@@ -30,6 +30,7 @@
   class LabelStmt;
   class GotoStmt;
   class IfStmt;
+  class ReturnStmt;
   
   class Expr;
   class IntegerLiteral;
@@ -80,6 +81,8 @@
   CodeGenModule &CGM;  // Per-module state.
   TargetInfo &Target;
   LLVMBuilder Builder;
+  
+  const FunctionDecl *CurFuncDecl;
   llvm::Function *CurFn;
 
   /// LabelMap - This keeps track of the LLVM basic block for each C label.
@@ -108,6 +111,7 @@
   void EmitLabelStmt(const LabelStmt &S);
   void EmitGotoStmt(const GotoStmt &S);
   void EmitIfStmt(const IfStmt &S);
+  void EmitReturnStmt(const ReturnStmt &S);
   
   //===--------------------------------------------------------------------===//
   //                             Expression Emission





More information about the cfe-commits mailing list