Frontend: Pull CodeGenAction out more, and eliminate CreateBackendConsumer.

This is the way I would like to move the frontend function towards -- distinct
pieces of functionality should be exposed only via FrontendAction
implementations which have clean and relatively-stable APIs.

This also isolates the surface area in clang which depends on LLVM CodeGen.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97110 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-02-25 04:37:45 +00:00
Родитель 5d64f8a05a
Коммит 4ee34616c6
6 изменённых файлов: 105 добавлений и 115 удалений

Просмотреть файл

@ -69,26 +69,6 @@ ASTConsumer *CreateObjCRewriter(const std::string &InFile,
const LangOptions &LOpts,
bool SilenceRewriteMacroWarning);
// LLVM code generator: uses the code generation backend to generate LLVM
// assembly. This runs optimizations depending on the CodeGenOptions
// parameter. The output depends on the Action parameter.
enum BackendAction {
Backend_EmitAssembly, // Emit native assembly files
Backend_EmitBC, // Emit LLVM bitcode files
Backend_EmitLL, // Emit human-readable LLVM assembly
Backend_EmitNothing, // Don't emit anything (benchmarking mode)
Backend_EmitObj // Emit native object files
};
ASTConsumer *CreateBackendConsumer(BackendAction Action,
Diagnostic &Diags,
const LangOptions &Features,
const CodeGenOptions &CodeGenOpts,
const TargetOptions &TargetOpts,
bool TimePasses,
const std::string &ModuleID,
llvm::raw_ostream *OS,
llvm::LLVMContext& C);
/// CreateHTMLPrinter - Create an AST consumer which rewrites source code to
/// HTML with syntax highlighting suitable for viewing in a web-browser.
ASTConsumer *CreateHTMLPrinter(llvm::raw_ostream *OS, Preprocessor &PP,

Просмотреть файл

@ -0,0 +1,50 @@
//===--- CodeGenAction.h - LLVM Code Generation Frontend Action -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/FrontendAction.h"
namespace clang {
class CodeGenAction : public ASTFrontendAction {
private:
unsigned Act;
protected:
CodeGenAction(unsigned _Act);
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef InFile);
};
class EmitAssemblyAction : public CodeGenAction {
public:
EmitAssemblyAction();
};
class EmitBCAction : public CodeGenAction {
public:
EmitBCAction();
};
class EmitLLVMAction : public CodeGenAction {
public:
EmitLLVMAction();
};
class EmitLLVMOnlyAction : public CodeGenAction {
public:
EmitLLVMOnlyAction();
};
class EmitObjAction : public CodeGenAction {
public:
EmitObjAction();
};
}

Просмотреть файл

@ -158,46 +158,6 @@ public:
virtual bool hasCodeCompletionSupport() const;
};
//===----------------------------------------------------------------------===//
// Code Gen AST Actions
//===----------------------------------------------------------------------===//
class CodeGenAction : public ASTFrontendAction {
private:
unsigned Act;
protected:
CodeGenAction(unsigned _Act);
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef InFile);
};
class EmitAssemblyAction : public CodeGenAction {
public:
EmitAssemblyAction();
};
class EmitBCAction : public CodeGenAction {
public:
EmitBCAction();
};
class EmitLLVMAction : public CodeGenAction {
public:
EmitLLVMAction();
};
class EmitLLVMOnlyAction : public CodeGenAction {
public:
EmitLLVMOnlyAction();
};
class EmitObjAction : public CodeGenAction {
public:
EmitObjAction();
};
//===----------------------------------------------------------------------===//
// Preprocessor Actions
//===----------------------------------------------------------------------===//

Просмотреть файл

@ -1,4 +1,4 @@
//===--- Backend.cpp - Interface to LLVM backend technologies -------------===//
//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
//
// The LLVM Compiler Infrastructure
//
@ -7,7 +7,7 @@
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/CodeGenAction.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclGroup.h"
@ -15,6 +15,8 @@
#include "clang/Basic/TargetOptions.h"
#include "clang/CodeGen/CodeGenOptions.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
@ -37,6 +39,14 @@ using namespace clang;
using namespace llvm;
namespace {
enum BackendAction {
Backend_EmitAssembly, ///< Emit native assembly files
Backend_EmitBC, ///< Emit LLVM bitcode files
Backend_EmitLL, ///< Emit human-readable LLVM assembly
Backend_EmitNothing, ///< Don't emit anything (benchmarking mode)
Backend_EmitObj ///< Emit native object files
};
class BackendConsumer : public ASTConsumer {
Diagnostic &Diags;
BackendAction Action;
@ -419,15 +429,46 @@ void BackendConsumer::EmitAssembly() {
}
}
ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
Diagnostic &Diags,
const LangOptions &LangOpts,
const CodeGenOptions &CodeGenOpts,
const TargetOptions &TargetOpts,
bool TimePasses,
const std::string& InFile,
llvm::raw_ostream* OS,
LLVMContext& C) {
return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
TargetOpts, TimePasses, InFile, OS, C);
//
CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {}
ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef InFile) {
BackendAction BA = static_cast<BackendAction>(Act);
llvm::OwningPtr<llvm::raw_ostream> OS;
switch (BA) {
case Backend_EmitAssembly:
OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
break;
case Backend_EmitLL:
OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
break;
case Backend_EmitBC:
OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
break;
case Backend_EmitNothing:
break;
case Backend_EmitObj:
OS.reset(CI.createDefaultOutputFile(true, InFile, "o"));
break;
}
if (BA != Backend_EmitNothing && !OS)
return 0;
return new BackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
CI.getCodeGenOpts(), CI.getTargetOpts(),
CI.getFrontendOpts().ShowTimers, InFile, OS.take(),
CI.getLLVMContext());
}
EmitAssemblyAction::EmitAssemblyAction()
: CodeGenAction(Backend_EmitAssembly) {}
EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {}
EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {}
EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {}
EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {}

Просмотреть файл

@ -159,48 +159,6 @@ ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
return new ASTConsumer();
}
CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {}
ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef InFile) {
BackendAction BA = static_cast<BackendAction>(Act);
llvm::OwningPtr<llvm::raw_ostream> OS;
switch (BA) {
case Backend_EmitAssembly:
OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
break;
case Backend_EmitLL:
OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
break;
case Backend_EmitBC:
OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
break;
case Backend_EmitNothing:
break;
case Backend_EmitObj:
OS.reset(CI.createDefaultOutputFile(true, InFile, "o"));
break;
}
if (BA != Backend_EmitNothing && !OS)
return 0;
return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
CI.getCodeGenOpts(), CI.getTargetOpts(),
CI.getFrontendOpts().ShowTimers, InFile,
OS.take(), CI.getLLVMContext());
}
EmitAssemblyAction::EmitAssemblyAction()
: CodeGenAction(Backend_EmitAssembly) {}
EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {}
EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {}
EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {}
EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {}
//===----------------------------------------------------------------------===//
// Preprocessor Actions
//===----------------------------------------------------------------------===//

Просмотреть файл

@ -19,6 +19,7 @@
#include "clang/Driver/CC1Options.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/OptTable.h"
#include "clang/Frontend/CodeGenAction.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendActions.h"