CodeGenFunction.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This coordinates the per-function state used while generating code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenFunction.h"
  14. #include "CodeGenModule.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "clang/AST/AST.h"
  17. #include "llvm/Constants.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/Function.h"
  20. #include "llvm/Analysis/Verifier.h"
  21. using namespace clang;
  22. using namespace CodeGen;
  23. CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
  24. : CGM(cgm), Target(CGM.getContext().Target) {}
  25. ASTContext &CodeGenFunction::getContext() const {
  26. return CGM.getContext();
  27. }
  28. llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
  29. llvm::BasicBlock *&BB = LabelMap[S];
  30. if (BB) return BB;
  31. // Create, but don't insert, the new block.
  32. return BB = new llvm::BasicBlock(S->getName());
  33. }
  34. const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
  35. return CGM.getTypes().ConvertType(T);
  36. }
  37. bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
  38. return !T->isRealType() && !T->isPointerType() && !T->isVoidType() &&
  39. !T->isVectorType();
  40. }
  41. void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
  42. LLVMIntTy = ConvertType(getContext().IntTy);
  43. LLVMPointerWidth = Target.getPointerWidth(SourceLocation());
  44. CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD));
  45. CurFuncDecl = FD;
  46. // TODO: Set up linkage and many other things.
  47. assert(CurFn->isDeclaration() && "Function already has body?");
  48. llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
  49. Builder.SetInsertPoint(EntryBB);
  50. // Create a marker to make it easy to insert allocas into the entryblock
  51. // later.
  52. llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
  53. AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
  54. // Emit allocs for param decls. Give the LLVM Argument nodes names.
  55. llvm::Function::arg_iterator AI = CurFn->arg_begin();
  56. // Name the struct return argument.
  57. if (hasAggregateLLVMType(FD->getResultType())) {
  58. AI->setName("agg.result");
  59. ++AI;
  60. }
  61. for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
  62. assert(AI != CurFn->arg_end() && "Argument mismatch!");
  63. EmitParmDecl(*FD->getParamDecl(i), AI);
  64. }
  65. // Emit the function body.
  66. EmitStmt(FD->getBody());
  67. // Emit a return for code that falls off the end.
  68. // FIXME: if this is C++ main, this should return 0.
  69. if (CurFn->getReturnType() == llvm::Type::VoidTy)
  70. Builder.CreateRetVoid();
  71. else
  72. Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
  73. // Verify that the function is well formed.
  74. assert(!verifyFunction(*CurFn));
  75. }