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