Initial scaffolding for an -emit-llvm mode. This requires the LLVM VMCore

library to be built for the driver to link.

llvm-svn: 39495
This commit is contained in:
Chris Lattner 2007-05-24 06:29:05 +00:00
parent 6d4024ae6b
commit f97fe38cb5
10 changed files with 258 additions and 2 deletions

40
clang/CodeGen/Builder.h Normal file
View File

@ -0,0 +1,40 @@
//===--- Builder.h - Internal interface for LLVM Builder ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is the internal per-translation-unit state used for llvm translation.
//
//===----------------------------------------------------------------------===//
#ifndef CODEGEN_BUILDER_H
#define CODEGEN_BUILDER_H
namespace llvm {
class Module;
namespace clang {
class ASTContext;
class FunctionDecl;
namespace CodeGen {
class Builder {
ASTContext &Context;
Module &TheModule;
public:
Builder(ASTContext &C, Module &M) : Context(C), TheModule(M) {}
void CodeGenFunction(FunctionDecl *FD) {}
void PrintStats() {}
};
} // end namespace CodeGen
} // end namespace clang
} // end namespace llvm
#endif

23
clang/CodeGen/Makefile Normal file
View File

@ -0,0 +1,23 @@
##===- clang/CodeGen/Makefile ------------------------------*- Makefile -*-===##
#
# The LLVM Compiler Infrastructure
#
# This file was developed by Chris Lattner and is distributed under
# the University of Illinois Open Source License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
#
# This implements the AST -> LLVM code generation library for the
# C-Language front-end.
#
##===----------------------------------------------------------------------===##
LEVEL = ../../..
LIBRARYNAME := clangCodeGen
BUILD_ARCHIVE = 1
CXXFLAGS = -fno-rtti
CPPFLAGS += -I$(PROJ_SRC_DIR)/../include
include $(LEVEL)/Makefile.common

View File

@ -0,0 +1,40 @@
//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This builds an AST and converts it to LLVM Code.
//
//===----------------------------------------------------------------------===//
#include "clang/CodeGen/ModuleBuilder.h"
#include "Builder.h"
using namespace llvm;
using namespace clang;
/// Init - Create an ModuleBuilder with the specified ASTContext.
llvm::clang::CodeGen::BuilderTy *
llvm::clang::CodeGen::Init(ASTContext &Context, Module &M) {
return new Builder(Context, M);
}
void llvm::clang::CodeGen::Terminate(BuilderTy *B) {
delete static_cast<Builder*>(B);
}
/// CodeGenFunction - Convert the AST node for a FunctionDecl into LLVM.
///
void llvm::clang::CodeGen::CodeGenFunction(BuilderTy *B, FunctionDecl *D) {
static_cast<Builder*>(B)->CodeGenFunction(D);
}
/// PrintStats - Emit statistic information to stderr.
///
void llvm::clang::CodeGen::PrintStats(BuilderTy *B) {
static_cast<Builder*>(B)->PrintStats();
}

View File

@ -0,0 +1,65 @@
//===--- LLVMCodegen.cpp - Emit LLVM Code from ASTs -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This builds an AST and converts it to LLVM Code.
//
//===----------------------------------------------------------------------===//
#include "clang.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Sema/ASTStreamer.h"
#include "clang/AST/AST.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/Module.h"
#include <iostream>
using namespace llvm;
using namespace clang;
//===----------------------------------------------------------------------===//
// LLVM Emission
//===----------------------------------------------------------------------===//
void llvm::clang::EmitLLVMFromASTs(Preprocessor &PP, unsigned MainFileID,
bool PrintStats) {
Diagnostic &Diags = PP.getDiagnostics();
// Create the streamer to read the file.
ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
// Create the module to codegen into.
Module M("foo");
CodeGen::BuilderTy *Builder = CodeGen::Init(Context, M);
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
// FIXME: if (Diags.error ever occurred) continue;
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
CodeGen::CodeGenFunction(Builder, FD);
} else if (isa<TypedefDecl>(D)) {
std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
} else {
std::cerr << "Read top-level variable decl: '" << D->getName() << "'\n";
}
}
if (PrintStats) {
std::cerr << "\nSTATISTICS:\n";
CodeGen::PrintStats(Builder);
ASTStreamer_PrintStats(Streamer);
Context.PrintStats();
}
CodeGen::Terminate(Builder);
ASTStreamer_Terminate(Streamer);
// Print the generated code.
M.print(std::cout);
}

View File

@ -3,6 +3,6 @@ CPPFLAGS += -I$(PROJ_SRC_DIR)/../include
CXXFLAGS = -fno-rtti
TOOLNAME = clang
USEDLIBS = clangSEMA.a clangAST.a clangParse.a clangLex.a clangBasic.a LLVMSupport.a LLVMSystem.a
USEDLIBS = LLVMCore.a clangCodeGen.a clangSEMA.a clangAST.a clangParse.a clangLex.a clangBasic.a LLVMSupport.a LLVMSystem.a
include $(LEVEL)/Makefile.common

View File

@ -52,6 +52,7 @@ static cl::opt<bool>
Stats("stats", cl::desc("Print performance metrics and statistics"));
enum ProgActions {
EmitLLVM, // Emit a .ll file.
ParseASTPrint, // Parse ASTs and print them.
ParseAST, // Parse ASTs.
ParsePrintCallbacks, // Parse and print each callback.
@ -82,6 +83,8 @@ ProgAction(cl::desc("Choose output type:"), cl::ZeroOrMore,
"Run parser and build ASTs"),
clEnumValN(ParseASTPrint, "parse-ast-print",
"Run parser, build ASTs, then print ASTs"),
clEnumValN(EmitLLVM, "emit-llvm",
"Build ASTs then convert to LLVM, emit .ll file"),
clEnumValEnd));
//===----------------------------------------------------------------------===//
@ -1141,6 +1144,9 @@ static void ProcessInputFile(const std::string &InFile,
case ParseASTPrint:
PrintASTs(PP, MainFileID);
break;
case EmitLLVM:
EmitLLVMFromASTs(PP, MainFileID, Stats);
break;
}
if (Stats) {

View File

@ -34,6 +34,10 @@ MinimalAction *CreatePrintParserActionsAction();
/// the -arch command line option.
TargetInfo *CreateTargetInfo(Diagnostic &Diags);
void EmitLLVMFromASTs(Preprocessor &PP, unsigned MainFileID,
bool PrintStats);
} // end namespace clang
} // end namespace llvm

View File

@ -1,5 +1,5 @@
LEVEL = ../..
DIRS := Basic Lex Parse AST Sema Driver
DIRS := Basic Lex Parse AST Sema CodeGen Driver
include $(LEVEL)/Makefile.common

View File

@ -55,6 +55,10 @@
DE67E7280C02109800F66BC5 /* ASTStreamer.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE67E7270C02109800F66BC5 /* ASTStreamer.h */; };
DE75ED290B044DC90020CF81 /* ASTContext.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE75ED280B044DC90020CF81 /* ASTContext.h */; };
DE75EDF10B06880E0020CF81 /* Type.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE75EDF00B06880E0020CF81 /* Type.cpp */; };
DE927FFD0C055DE900231DA4 /* LLVMCodegen.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE927FFC0C055DE900231DA4 /* LLVMCodegen.cpp */; };
DE928B110C05658A00231DA4 /* Builder.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE928B100C05658A00231DA4 /* Builder.h */; };
DE928B130C05659200231DA4 /* ModuleBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE928B120C05659200231DA4 /* ModuleBuilder.cpp */; };
DE928B200C0565B000231DA4 /* ModuleBuilder.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE928B1F0C0565B000231DA4 /* ModuleBuilder.h */; };
DEAEE98B0A5A2B970045101B /* MultipleIncludeOpt.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEAEE98A0A5A2B970045101B /* MultipleIncludeOpt.h */; };
DEAEED4B0A5AF89A0045101B /* NOTES.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEAEED4A0A5AF89A0045101B /* NOTES.txt */; };
DEC8D9910A9433CD00353FCA /* Decl.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEC8D9900A9433CD00353FCA /* Decl.h */; };
@ -147,6 +151,8 @@
1A869A700BA2164C008DA07A /* LiteralSupport.h in CopyFiles */,
DE67E7150C020EDF00F66BC5 /* Sema.h in CopyFiles */,
DE67E7280C02109800F66BC5 /* ASTStreamer.h in CopyFiles */,
DE928B110C05658A00231DA4 /* Builder.h in CopyFiles */,
DE928B200C0565B000231DA4 /* ModuleBuilder.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 1;
};
@ -202,6 +208,10 @@
DE67E7270C02109800F66BC5 /* ASTStreamer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ASTStreamer.h; path = clang/Sema/ASTStreamer.h; sourceTree = "<group>"; };
DE75ED280B044DC90020CF81 /* ASTContext.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ASTContext.h; path = clang/AST/ASTContext.h; sourceTree = "<group>"; };
DE75EDF00B06880E0020CF81 /* Type.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Type.cpp; path = AST/Type.cpp; sourceTree = "<group>"; };
DE927FFC0C055DE900231DA4 /* LLVMCodegen.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = LLVMCodegen.cpp; path = Driver/LLVMCodegen.cpp; sourceTree = "<group>"; };
DE928B100C05658A00231DA4 /* Builder.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Builder.h; path = CodeGen/Builder.h; sourceTree = "<group>"; };
DE928B120C05659200231DA4 /* ModuleBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ModuleBuilder.cpp; path = CodeGen/ModuleBuilder.cpp; sourceTree = "<group>"; };
DE928B1F0C0565B000231DA4 /* ModuleBuilder.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ModuleBuilder.h; path = clang/CodeGen/ModuleBuilder.h; sourceTree = "<group>"; };
DEAEE98A0A5A2B970045101B /* MultipleIncludeOpt.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = MultipleIncludeOpt.h; sourceTree = "<group>"; };
DEAEED4A0A5AF89A0045101B /* NOTES.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = NOTES.txt; sourceTree = "<group>"; };
DEC8D9900A9433CD00353FCA /* Decl.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Decl.h; path = clang/AST/Decl.h; sourceTree = "<group>"; };
@ -274,6 +284,7 @@
DE1F22600A7D8C9B00FBF588 /* Parse */,
DEC8D9920A9433F400353FCA /* AST */,
DE67E7070C020EAB00F66BC5 /* Sema */,
DE927FCC0C0557CD00231DA4 /* CodeGen */,
);
name = Source;
sourceTree = "<group>";
@ -345,6 +356,23 @@
name = Sema;
sourceTree = "<group>";
};
DE927FCC0C0557CD00231DA4 /* CodeGen */ = {
isa = PBXGroup;
children = (
DE928B100C05658A00231DA4 /* Builder.h */,
DE928B120C05659200231DA4 /* ModuleBuilder.cpp */,
);
name = CodeGen;
sourceTree = "<group>";
};
DE928B140C05659A00231DA4 /* CodeGen */ = {
isa = PBXGroup;
children = (
DE928B1F0C0565B000231DA4 /* ModuleBuilder.h */,
);
name = CodeGen;
sourceTree = "<group>";
};
DEAEECAE0A5AF0FA0045101B /* Driver */ = {
isa = PBXGroup;
children = (
@ -353,6 +381,7 @@
DED627020AE0C51D001E80A4 /* Targets.cpp */,
DED67AEF0B6DB92F00AAD4A3 /* PPCBuiltins.def */,
DED67AED0B6DB92A00AAD4A3 /* X86Builtins.def */,
DE927FFC0C055DE900231DA4 /* LLVMCodegen.cpp */,
DE5932CF0AD60FF400BC794C /* PrintParserCallbacks.cpp */,
DE5932D00AD60FF400BC794C /* PrintPreprocessedOutput.cpp */,
);
@ -400,6 +429,7 @@
DE1F21F20A7D84E800FBF588 /* Parse */,
DEC8D98B0A9433BC00353FCA /* AST */,
DE67E7260C02108300F66BC5 /* Sema */,
DE928B140C05659A00231DA4 /* CodeGen */,
);
path = include;
sourceTree = "<group>";
@ -556,6 +586,8 @@
DE67E7170C020EE400F66BC5 /* Sema.cpp in Sources */,
DE67E71A0C020F4F00F66BC5 /* ASTStreamer.cpp in Sources */,
DE06756C0C051CFE00EBBFD8 /* ParseExprCXX.cpp in Sources */,
DE927FFD0C055DE900231DA4 /* LLVMCodegen.cpp in Sources */,
DE928B130C05659200231DA4 /* ModuleBuilder.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,46 @@
//===--- CodeGen/ModuleBuilder.h - Build LLVM from ASTs ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the ModuleBuilder interface.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_CODEGEN_MODULEBUILDER_H
#define LLVM_CLANG_CODEGEN_MODULEBUILDER_H
namespace llvm {
class Module;
namespace clang {
class ASTContext;
class FunctionDecl;
namespace CodeGen {
/// BuilderTy - This is an opaque type used to reference ModuleBuilder
/// objects.
typedef void BuilderTy;
/// Init - Create an ModuleBuilder with the specified ASTContext.
BuilderTy *Init(ASTContext &Context, Module &M);
/// CodeGenFunction - Convert the AST node for a FunctionDecl into LLVM.
///
void CodeGenFunction(BuilderTy *Builder, FunctionDecl *D);
/// PrintStats - Emit statistic information to stderr.
///
void PrintStats(BuilderTy *Builder);
/// Terminate - Gracefully shut down the builder.
///
void Terminate(BuilderTy *Builder);
} // end namespace CodeGen
} // end namespace clang
} // end namespace llvm
#endif