CodeGenModuleContainer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===--- CodeGenModuleContainer.cpp - Emit .pcm files ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/CodeGen/CodeGenModuleContainer.h"
  10. #include "CodeGenModule.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/AST/DeclObjC.h"
  13. #include "clang/AST/Expr.h"
  14. #include "clang/AST/RecursiveASTVisitor.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "clang/CodeGen/BackendUtil.h"
  18. #include "clang/Frontend/CodeGenOptions.h"
  19. #include "clang/Serialization/ASTWriter.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/LLVMContext.h"
  24. #include "llvm/IR/Module.h"
  25. #include "llvm/Support/TargetRegistry.h"
  26. #include <memory>
  27. using namespace clang;
  28. namespace {
  29. class ModuleContainerGenerator : public CodeGenerator {
  30. DiagnosticsEngine &Diags;
  31. std::unique_ptr<const llvm::DataLayout> TD;
  32. ASTContext *Ctx;
  33. const CodeGenOptions CodeGenOpts;
  34. const TargetOptions TargetOpts;
  35. const LangOptions LangOpts;
  36. llvm::LLVMContext VMContext;
  37. std::unique_ptr<llvm::Module> M;
  38. std::unique_ptr<CodeGen::CodeGenModule> Builder;
  39. raw_ostream *OS;
  40. SmallVectorImpl<char> *SerializedASTBuffer;
  41. public:
  42. ModuleContainerGenerator(DiagnosticsEngine &diags,
  43. const std::string &ModuleName,
  44. const CodeGenOptions &CGO, const TargetOptions &TO,
  45. const LangOptions &LO, raw_ostream *OS,
  46. PCHGenerator *PCHGen)
  47. : Diags(diags), CodeGenOpts(CGO), TargetOpts(TO), LangOpts(LO),
  48. M(new llvm::Module(ModuleName, VMContext)), OS(OS) {
  49. PCHGen->RegisterSerializationFinishedCallback(
  50. [&](SmallVectorImpl<char> *Buf){
  51. SerializedASTBuffer = Buf;
  52. });
  53. }
  54. virtual ~ModuleContainerGenerator() {}
  55. llvm::Module *GetModule() override { return M.get(); }
  56. llvm::Module *ReleaseModule() override { return M.release(); }
  57. /// Lifted from ModuleBuilder.
  58. const Decl *GetDeclForMangledName(StringRef MangledName) override {
  59. GlobalDecl Result;
  60. if (!Builder->lookupRepresentativeDecl(MangledName, Result))
  61. return nullptr;
  62. const Decl *D = Result.getCanonicalDecl().getDecl();
  63. if (auto FD = dyn_cast<FunctionDecl>(D)) {
  64. if (FD->hasBody(FD))
  65. return FD;
  66. } else if (auto TD = dyn_cast<TagDecl>(D)) {
  67. if (auto Def = TD->getDefinition())
  68. return Def;
  69. }
  70. return D;
  71. }
  72. void Initialize(ASTContext &Context) override {
  73. Ctx = &Context;
  74. M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
  75. M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
  76. TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
  77. Builder.reset(
  78. new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD, Diags));
  79. }
  80. /// Emit a container holding the serialized AST.
  81. void HandleTranslationUnit(ASTContext &Ctx) override {
  82. if (Diags.hasErrorOccurred()) {
  83. if (Builder)
  84. Builder->clear();
  85. M.reset();
  86. return;
  87. }
  88. // Finalize the Builder.
  89. if (Builder)
  90. Builder->Release();
  91. // Initialize the backend if we haven't done so already.
  92. LLVMInitializeAllTargetInfos();
  93. LLVMInitializeAllTargets();
  94. LLVMInitializeAllAsmPrinters();
  95. LLVMInitializeAllTargetMCs();
  96. // Ensure the target exists.
  97. std::string Error;
  98. auto Triple = Ctx.getTargetInfo().getTriple();
  99. if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
  100. llvm::report_fatal_error(Error);
  101. // Emit the serialized Clang AST into its own section.
  102. auto Size = SerializedASTBuffer->size();
  103. auto Int8Ty = llvm::Type::getInt8Ty(VMContext);
  104. auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
  105. auto *Data = llvm::ConstantDataArray::getString(VMContext,
  106. StringRef(SerializedASTBuffer->data(), Size), /*AddNull=*/false);
  107. auto *ASTSym = new llvm::GlobalVariable(*M, Ty, /*constant*/ true,
  108. llvm::GlobalVariable::InternalLinkage, Data, "__clang_ast");
  109. ASTSym->setAlignment(8);
  110. if (Triple.isOSBinFormatMachO())
  111. // Include Mach-O segment name.
  112. ASTSym->setSection("__CLANG,__clangast");
  113. else if (Triple.isOSBinFormatCOFF())
  114. // Adhere to COFF eight-character limit.
  115. ASTSym->setSection("clangast");
  116. else
  117. ASTSym->setSection("__clangast");
  118. // Use the LLVM backend to emit the pcm.
  119. clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
  120. Ctx.getTargetInfo().getTargetDescription(), M.get(),
  121. BackendAction::Backend_EmitObj, OS);
  122. // Make sure the module container hits disk now.
  123. OS->flush();
  124. // Free up some memory, in case the process is kept alive.
  125. SerializedASTBuffer->clear();
  126. }
  127. };
  128. }
  129. CodeGenerator *clang::CreateModuleContainerGenerator(
  130. DiagnosticsEngine &Diags, const std::string &ModuleName,
  131. const CodeGenOptions &CGO, const TargetOptions &TO, const LangOptions &LO,
  132. llvm::raw_ostream *OS, PCHGenerator *PCHGen) {
  133. return
  134. new ModuleContainerGenerator(Diags, ModuleName, CGO, TO, LO, OS, PCHGen);
  135. }