CodeGenModule.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
  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-module state used while generating code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenModule.h"
  14. #include "CodeGenFunction.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Decl.h"
  17. #include "clang/Basic/TargetInfo.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/Function.h"
  20. #include "llvm/GlobalVariable.h"
  21. #include "llvm/Intrinsics.h"
  22. using namespace clang;
  23. using namespace CodeGen;
  24. CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
  25. : Context(C), TheModule(M), Types(C.Target) {}
  26. llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) {
  27. // See if it is already in the map.
  28. llvm::Constant *&Entry = GlobalDeclMap[D];
  29. if (Entry) return Entry;
  30. QualType ASTTy = cast<ValueDecl>(D)->getType();
  31. const llvm::Type *Ty = getTypes().ConvertType(ASTTy);
  32. if (isa<FunctionDecl>(D)) {
  33. const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
  34. // FIXME: param attributes for sext/zext etc.
  35. return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage,
  36. D->getName(), &getModule());
  37. }
  38. assert(isa<FileVarDecl>(D) && "Unknown global decl!");
  39. return Entry = new llvm::GlobalVariable(Ty, false,
  40. llvm::GlobalValue::ExternalLinkage,
  41. 0, D->getName(), &getModule());
  42. }
  43. void CodeGenModule::EmitFunction(FunctionDecl *FD) {
  44. // If this is not a prototype, emit the body.
  45. if (FD->getBody())
  46. CodeGenFunction(*this).GenerateCode(FD);
  47. }
  48. llvm::Function *CodeGenModule::getMemCpyFn() {
  49. if (MemCpyFn) return MemCpyFn;
  50. llvm::Intrinsic::ID IID;
  51. switch (Context.Target.getPointerWidth(SourceLocation())) {
  52. default: assert(0 && "Unknown ptr width");
  53. case 32: IID = llvm::Intrinsic::memcpy_i32; break;
  54. case 64: IID = llvm::Intrinsic::memcpy_i64; break;
  55. }
  56. return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID);
  57. }