blob: 99af838a33ca0658e5de554fcd7bcc6ca413f8ba [file] [log] [blame]
Daniel Dunbarcea0c702010-02-25 04:37:45 +00001//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
Daniel Dunbarc13935e2008-10-21 23:49:24 +00002//
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
Daniel Dunbarcea0c702010-02-25 04:37:45 +000010#include "clang/Frontend/CodeGenAction.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000011#include "clang/Basic/SourceManager.h"
12#include "clang/Basic/TargetInfo.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000013#include "clang/AST/ASTConsumer.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000014#include "clang/AST/ASTContext.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000015#include "clang/AST/DeclGroup.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000016#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbarcea0c702010-02-25 04:37:45 +000017#include "clang/Frontend/ASTConsumers.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000018#include "clang/Frontend/BackendUtil.h"
Daniel Dunbarcea0c702010-02-25 04:37:45 +000019#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbaracadc552009-12-03 09:12:54 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Chris Lattner6d672132010-04-06 17:52:14 +000021#include "llvm/LLVMContext.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000022#include "llvm/Module.h"
Daniel Dunbar3e111522010-06-07 23:21:04 +000023#include "llvm/Pass.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000024#include "llvm/ADT/OwningPtr.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SourceMgr.h"
Chris Lattner263d64c2009-02-18 01:37:30 +000027#include "llvm/Support/Timer.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000028using namespace clang;
29using namespace llvm;
30
31namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000032 class BackendConsumer : public ASTConsumer {
Daniel Dunbar20c13162009-12-04 08:17:40 +000033 Diagnostic &Diags;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000034 BackendAction Action;
Daniel Dunbarde182242009-11-30 08:39:32 +000035 const CodeGenOptions &CodeGenOpts;
Daniel Dunbarde182242009-11-30 08:39:32 +000036 const TargetOptions &TargetOpts;
Eli Friedman94cf21e2009-05-18 22:20:00 +000037 llvm::raw_ostream *AsmOutStream;
Chris Lattnereae6cb62009-03-05 08:00:35 +000038 ASTContext *Context;
Daniel Dunbarb3a36cf2008-10-29 08:50:02 +000039
Chris Lattner263d64c2009-02-18 01:37:30 +000040 Timer LLVMIRGeneration;
Mike Stump11289f42009-09-09 15:08:12 +000041
Daniel Dunbarc13935e2008-10-21 23:49:24 +000042 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump11289f42009-09-09 15:08:12 +000043
Daniel Dunbar400a6932010-02-25 04:37:50 +000044 llvm::OwningPtr<llvm::Module> TheModule;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000045
Mike Stump11289f42009-09-09 15:08:12 +000046 public:
Daniel Dunbaracadc552009-12-03 09:12:54 +000047 BackendConsumer(BackendAction action, Diagnostic &_Diags,
Daniel Dunbar6d5824f2010-06-07 23:19:17 +000048 const CodeGenOptions &compopts,
Daniel Dunbar8e705052009-11-30 08:39:52 +000049 const TargetOptions &targetopts, bool TimePasses,
50 const std::string &infile, llvm::raw_ostream *OS,
Chris Lattner6d672132010-04-06 17:52:14 +000051 LLVMContext &C) :
Daniel Dunbaracadc552009-12-03 09:12:54 +000052 Diags(_Diags),
Mike Stump11289f42009-09-09 15:08:12 +000053 Action(action),
Chandler Carruthbc55fe22009-11-12 17:24:48 +000054 CodeGenOpts(compopts),
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000055 TargetOpts(targetopts),
Chris Lattner15681772009-07-14 20:39:15 +000056 AsmOutStream(OS),
Chris Lattner263d64c2009-02-18 01:37:30 +000057 LLVMIRGeneration("LLVM IR Generation Time"),
Daniel Dunbar3e111522010-06-07 23:21:04 +000058 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)) {
Daniel Dunbar8e705052009-11-30 08:39:52 +000059 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattnerdeffa132009-02-18 01:23:44 +000060 }
Daniel Dunbarc13935e2008-10-21 23:49:24 +000061
Daniel Dunbar400a6932010-02-25 04:37:50 +000062 llvm::Module *takeModule() { return TheModule.take(); }
63
Chris Lattner5cf49fe2009-03-28 02:18:25 +000064 virtual void Initialize(ASTContext &Ctx) {
65 Context = &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000066
Daniel Dunbar8e705052009-11-30 08:39:52 +000067 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000068 LLVMIRGeneration.startTimer();
Mike Stump11289f42009-09-09 15:08:12 +000069
Chris Lattner5cf49fe2009-03-28 02:18:25 +000070 Gen->Initialize(Ctx);
Daniel Dunbarc13935e2008-10-21 23:49:24 +000071
Daniel Dunbar400a6932010-02-25 04:37:50 +000072 TheModule.reset(Gen->GetModule());
Mike Stump11289f42009-09-09 15:08:12 +000073
Daniel Dunbar8e705052009-11-30 08:39:52 +000074 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000075 LLVMIRGeneration.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000076 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner5bbb3c82009-03-29 16:50:03 +000078 virtual void HandleTopLevelDecl(DeclGroupRef D) {
79 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnereae6cb62009-03-05 08:00:35 +000080 Context->getSourceManager(),
81 "LLVM IR generation of declaration");
Mike Stump11289f42009-09-09 15:08:12 +000082
Daniel Dunbar8e705052009-11-30 08:39:52 +000083 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000084 LLVMIRGeneration.startTimer();
Chris Lattner5bbb3c82009-03-29 16:50:03 +000085
Daniel Dunbarc13935e2008-10-21 23:49:24 +000086 Gen->HandleTopLevelDecl(D);
Chris Lattner263d64c2009-02-18 01:37:30 +000087
Daniel Dunbar8e705052009-11-30 08:39:52 +000088 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000089 LLVMIRGeneration.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000090 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattnercf169832009-03-28 04:11:33 +000092 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattnereae6cb62009-03-05 08:00:35 +000093 {
Chris Lattnere46de752009-03-06 06:46:31 +000094 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbar8e705052009-11-30 08:39:52 +000095 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +000096 LLVMIRGeneration.startTimer();
Chris Lattner263d64c2009-02-18 01:37:30 +000097
Chris Lattnercf169832009-03-28 04:11:33 +000098 Gen->HandleTranslationUnit(C);
Daniel Dunbara94d8732008-11-11 06:35:39 +000099
Daniel Dunbar8e705052009-11-30 08:39:52 +0000100 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000101 LLVMIRGeneration.stopTimer();
102 }
Chris Lattner263d64c2009-02-18 01:37:30 +0000103
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000104 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar3e111522010-06-07 23:21:04 +0000105 if (!TheModule)
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000106 return;
Mike Stump11289f42009-09-09 15:08:12 +0000107
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000108 // Make sure IR generation is happy with the module. This is released by
109 // the module provider.
110 Module *M = Gen->ReleaseModule();
111 if (!M) {
112 // The module has been released by IR gen on failures, do not double
113 // free.
114 TheModule.take();
115 return;
116 }
117
118 assert(TheModule.get() == M &&
119 "Unexpected module change during IR generation");
120
121 // Install an inline asm handler so that diagnostics get printed through
122 // our diagnostics hooks.
123 LLVMContext &Ctx = TheModule->getContext();
124 void *OldHandler = Ctx.getInlineAsmDiagnosticHandler();
125 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
126 Ctx.setInlineAsmDiagnosticHandler((void*)(intptr_t)InlineAsmDiagHandler,
127 this);
128
129 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts,
Daniel Dunbar3e111522010-06-07 23:21:04 +0000130 TheModule.get(), Action, AsmOutStream);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000131
132 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000133 }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000135 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000136 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
137 Context->getSourceManager(),
138 "LLVM IR generation of declaration");
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000139 Gen->HandleTagDeclDefinition(D);
140 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000141
142 virtual void CompleteTentativeDefinition(VarDecl *D) {
143 Gen->CompleteTentativeDefinition(D);
144 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000145
Douglas Gregor88d292c2010-05-13 16:44:06 +0000146 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
147 Gen->HandleVTable(RD, DefinitionRequired);
148 }
149
Chris Lattner5ec32e72010-04-06 18:38:50 +0000150 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
151 unsigned LocCookie) {
152 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
153 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
154 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000155
Chris Lattner5ec32e72010-04-06 18:38:50 +0000156 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
157 SourceLocation LocCookie);
Mike Stump11289f42009-09-09 15:08:12 +0000158 };
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000159}
160
Chris Lattner79f67a72010-04-08 00:23:06 +0000161/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
162/// buffer to be a valid FullSourceLoc.
163static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
164 SourceManager &CSM) {
165 // Get both the clang and llvm source managers. The location is relative to
166 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000167 // a copy to the Clang source manager.
Chris Lattner79f67a72010-04-08 00:23:06 +0000168 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000169
Chris Lattner79f67a72010-04-08 00:23:06 +0000170 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
171 // already owns its one and clang::SourceManager wants to own its one.
172 const MemoryBuffer *LBuf =
173 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000174
Chris Lattner79f67a72010-04-08 00:23:06 +0000175 // Create the copy and transfer ownership to clang::SourceManager.
176 llvm::MemoryBuffer *CBuf =
177 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
178 LBuf->getBufferIdentifier());
179 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000180
Chris Lattner79f67a72010-04-08 00:23:06 +0000181 // Translate the offset into the file.
182 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000183 SourceLocation NewLoc =
Chris Lattner79f67a72010-04-08 00:23:06 +0000184 CSM.getLocForStartOfFile(FID).getFileLocWithOffset(Offset);
185 return FullSourceLoc(NewLoc, CSM);
186}
187
Chris Lattner6d672132010-04-06 17:52:14 +0000188
Chris Lattner5ec32e72010-04-06 18:38:50 +0000189/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
190/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000191/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000192void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
193 SourceLocation LocCookie) {
194 // There are a couple of different kinds of errors we could get here. First,
195 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000196
Chris Lattner5ec32e72010-04-06 18:38:50 +0000197 // Strip "error: " off the start of the message string.
198 llvm::StringRef Message = D.getMessage();
199 if (Message.startswith("error: "))
200 Message = Message.substr(7);
201
202 // There are two cases: the SMDiagnostic could have a inline asm source
203 // location or it might not. If it does, translate the location.
204 FullSourceLoc Loc;
Chris Lattner79f67a72010-04-08 00:23:06 +0000205 if (D.getLoc() != SMLoc())
206 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattner5ec32e72010-04-06 18:38:50 +0000207 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000208
Chris Lattner5ec32e72010-04-06 18:38:50 +0000209 // This could be a problem with no clang-level source location information.
210 // In this case, LocCookie is invalid. If there is source level information,
211 // print an "generated from" note.
212 if (LocCookie.isValid())
213 Diags.Report(FullSourceLoc(LocCookie, Context->getSourceManager()),
214 diag::note_fe_inline_asm_here);
215}
216
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000217//
218
219CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {}
220
Daniel Dunbare8ecf9a2010-02-25 20:37:44 +0000221CodeGenAction::~CodeGenAction() {}
222
Daniel Dunbar400a6932010-02-25 04:37:50 +0000223void CodeGenAction::EndSourceFileAction() {
224 // If the consumer creation failed, do nothing.
225 if (!getCompilerInstance().hasASTConsumer())
226 return;
227
228 // Steal the module from the consumer.
229 BackendConsumer *Consumer = static_cast<BackendConsumer*>(
230 &getCompilerInstance().getASTConsumer());
231
232 TheModule.reset(Consumer->takeModule());
233}
234
235llvm::Module *CodeGenAction::takeModule() {
236 return TheModule.take();
237}
238
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000239ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
240 llvm::StringRef InFile) {
241 BackendAction BA = static_cast<BackendAction>(Act);
242 llvm::OwningPtr<llvm::raw_ostream> OS;
243 switch (BA) {
244 case Backend_EmitAssembly:
245 OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
246 break;
247 case Backend_EmitLL:
248 OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
249 break;
250 case Backend_EmitBC:
251 OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
252 break;
253 case Backend_EmitNothing:
254 break;
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000255 case Backend_EmitMCNull:
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000256 case Backend_EmitObj:
257 OS.reset(CI.createDefaultOutputFile(true, InFile, "o"));
258 break;
259 }
260 if (BA != Backend_EmitNothing && !OS)
261 return 0;
262
Daniel Dunbar6d5824f2010-06-07 23:19:17 +0000263 return new BackendConsumer(BA, CI.getDiagnostics(),
John McCall731be662010-03-04 04:29:44 +0000264 CI.getCodeGenOpts(), CI.getTargetOpts(),
265 CI.getFrontendOpts().ShowTimers, InFile, OS.take(),
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000266 CI.getLLVMContext());
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000267}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000268
269EmitAssemblyAction::EmitAssemblyAction()
270 : CodeGenAction(Backend_EmitAssembly) {}
271
272EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {}
273
274EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {}
275
276EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {}
277
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000278EmitCodeGenOnlyAction::EmitCodeGenOnlyAction() : CodeGenAction(Backend_EmitMCNull) {}
279
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000280EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {}