diff options
author | Zack Rusin <zack@kde.org> | 2010-11-11 01:44:37 -0500 |
---|---|---|
committer | Zack Rusin <zack@kde.org> | 2010-11-11 01:44:37 -0500 |
commit | 75937295ca1bc8ba296d4d57d2d37076577cac84 (patch) | |
tree | de51a5c9c10b117df88a72ea3ae0749bd0467642 | |
parent | d502708bbfdbcf89139c49539cfac58c9e2c56ce (diff) |
Get it all a little closer to being reasonable.
-rw-r--r-- | cmake/modules/FindClang.cmake | 1 | ||||
-rw-r--r-- | cmake/modules/FindGallium.cmake | 16 | ||||
-rw-r--r-- | src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/compiler/compiler.cpp | 289 | ||||
-rw-r--r-- | src/compiler/compiler.h | 33 | ||||
-rw-r--r-- | src/tools/CMakeLists.txt | 17 | ||||
-rw-r--r-- | src/tools/clcompiler.cpp | 2 |
7 files changed, 98 insertions, 264 deletions
diff --git a/cmake/modules/FindClang.cmake b/cmake/modules/FindClang.cmake index b5c1953..6d6beb6 100644 --- a/cmake/modules/FindClang.cmake +++ b/cmake/modules/FindClang.cmake @@ -24,6 +24,7 @@ FIND_AND_ADD_CLANG_LIB(clangAST) FIND_AND_ADD_CLANG_LIB(clangParse) FIND_AND_ADD_CLANG_LIB(clangLex) FIND_AND_ADD_CLANG_LIB(clangBasic) +FIND_AND_ADD_CLANG_LIB(clangSerialization) MESSAGE(STATUS "Clang libs: " ${CLANG_LIBS}) diff --git a/cmake/modules/FindGallium.cmake b/cmake/modules/FindGallium.cmake index 2d59d99..96f2cdb 100644 --- a/cmake/modules/FindGallium.cmake +++ b/cmake/modules/FindGallium.cmake @@ -14,17 +14,17 @@ set(GALLIUM_INCLUDE_DIRS ${GALLIUM_INCLUDE_DIRS} ${GALLIUM_DIR}/src/gallium/incl set(GALLIUM_INCLUDE_DIRS ${GALLIUM_INCLUDE_DIRS} ${GALLIUM_DIR}/src/gallium/auxiliary) set(GALLIUM_INCLUDE_DIRS ${GALLIUM_INCLUDE_DIRS} ${GALLIUM_DIR}/src/gallium/drivers) -FIND_AND_ADD_GALLIUM_LIB(pipebuffer) -FIND_AND_ADD_GALLIUM_LIB(sct) -FIND_AND_ADD_GALLIUM_LIB(draw) -FIND_AND_ADD_GALLIUM_LIB(rtasm) -FIND_AND_ADD_GALLIUM_LIB(translate) -FIND_AND_ADD_GALLIUM_LIB(cso_cache) -FIND_AND_ADD_GALLIUM_LIB(tgsi) +#FIND_AND_ADD_GALLIUM_LIB(pipebuffer) +#FIND_AND_ADD_GALLIUM_LIB(sct) +#FIND_AND_ADD_GALLIUM_LIB(draw) +#FIND_AND_ADD_GALLIUM_LIB(rtasm) +#FIND_AND_ADD_GALLIUM_LIB(translate) +#FIND_AND_ADD_GALLIUM_LIB(cso_cache) +#FIND_AND_ADD_GALLIUM_LIB(tgsi) #FIXME: always some other libutil is found #FIND_AND_ADD_GALLIUM_LIB(util) -set(GALLIUM_LIBS ${GALLIUM_LIBS} ${GALLIUM_DIR}//src/gallium/auxiliary/util/libutil.a) +#set(GALLIUM_LIBS ${GALLIUM_LIBS} ${GALLIUM_DIR}//src/gallium/auxiliary/util/libutil.a) MESSAGE(STATUS "Gallium libs: " ${GALLIUM_LIBS}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e75abf6..8a7e43e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,7 +27,7 @@ set(COAL_SRC_FILES add_library(OpenCL SHARED ${COAL_SRC_FILES}) -SET(LIBRARY_OUTPUT_PATH ${COAL_BINARY_DIR}/lib) +SET(LIBRARY_OUTPUT_PATH ${Coal_BINARY_DIR}/lib) SET_TARGET_PROPERTIES(OpenCL PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} @@ -43,7 +43,7 @@ set_target_properties(OpenCL TARGET_LINK_LIBRARIES(OpenCL ${CLANG_LIBS} ${LLVM_LIBS_CORE} - ${GALLIUM_LIBS} +# ${GALLIUM_LIBS} ) add_subdirectory(tools) diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index f055102..38e552d 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -1,144 +1,31 @@ #include "compiler.h" -#include <clang/Lex/Preprocessor.h> -#include <clang/Frontend/ASTConsumers.h> +#include <clang/CodeGen/CodeGenAction.h> +#include <clang/Driver/Compilation.h> +#include <clang/Driver/Driver.h> +#include <clang/Driver/Tool.h> +#include <clang/Frontend/CompilerInvocation.h> +#include <clang/Frontend/CompilerInstance.h> +#include <clang/Frontend/DiagnosticOptions.h> #include <clang/Frontend/FrontendDiagnostic.h> #include <clang/Frontend/TextDiagnosticPrinter.h> -#include <clang/Frontend/InitPreprocessor.h> -#include <clang/Frontend/InitHeaderSearch.h> -#include <clang/Frontend/Utils.h> -#include <clang/Lex/HeaderSearch.h> -#include <clang/Sema/ParseAST.h> -#include <clang/AST/ASTConsumer.h> -#include <clang/AST/ASTContext.h> -#include <clang/Basic/FileManager.h> -#include <clang/Basic/Diagnostic.h> -#include <clang/Basic/SourceManager.h> -#include <clang/Basic/TargetInfo.h> -#include <clang/Basic/SourceLocation.h> +#include <llvm/LLVMContext.h> #include <llvm/Module.h> +#include <llvm/Config/config.h> #include <llvm/ADT/OwningPtr.h> +#include <llvm/ADT/SmallString.h> +#include <llvm/Config/config.h> +#include <llvm/ExecutionEngine/ExecutionEngine.h> +#include <llvm/Support/ManagedStatic.h> #include <llvm/Support/raw_ostream.h> #include <llvm/System/Host.h> -#include <llvm/LLVMContext.h> -#include <llvm/Support/MemoryBuffer.h> +#include <llvm/System/Path.h> +#include <llvm/Target/TargetSelect.h> +using namespace Coal; using namespace clang; -static void llvmErrorHandler(void *userData, const std::string &message) -{ - clang::Diagnostic &diags = *static_cast<clang::Diagnostic*>(userData); - - diags.Report(clang::FullSourceLoc(), clang::diag::err_fe_error_backend) << message; - - // We cannot recover from llvm errors. - exit(1); -} - -static void -initializePreprocessorInitOptions(clang::PreprocessorInitOptions &InitOpts) -{ - //add macros and paths -} - -/* almost verbatim copy of the class from clang-cc.cpp */ -namespace { -class DriverPreprocessorFactory : public PreprocessorFactory { - clang::Diagnostic &Diags; - const clang::LangOptions &LangInfo; - clang::TargetInfo &Target; - clang::SourceManager &SourceMgr; - clang::HeaderSearch &HeaderInfo; - -public: - DriverPreprocessorFactory(clang::Diagnostic &diags, const clang::LangOptions &opts, - clang::TargetInfo &target, clang::SourceManager &SM, - clang::HeaderSearch &Headers) - : Diags(diags), LangInfo(opts), Target(target), - SourceMgr(SM), HeaderInfo(Headers) - {} - - virtual ~DriverPreprocessorFactory() - {} - - virtual Preprocessor * CreatePreprocessor() { - llvm::OwningPtr<clang::PTHManager> PTHMgr; - - if (Diags.hasErrorOccurred()) - exit(1); - - // Create the Preprocessor. - llvm::OwningPtr<clang::Preprocessor> PP( - new clang::Preprocessor(Diags, LangInfo, Target, - SourceMgr, HeaderInfo, - PTHMgr.get())); - - // Note that this is different then passing PTHMgr to Preprocessor's ctor. - // That argument is used as the IdentifierInfoLookup argument to - // IdentifierTable's ctor. - if (PTHMgr) { - PTHMgr->setPreprocessor(PP.get()); - PP->setPTHManager(PTHMgr.take()); - } - - clang::PreprocessorInitOptions InitOpts; - initializePreprocessorInitOptions(InitOpts); - if (clang::InitializePreprocessor(*PP, InitOpts)) - return 0; - - return PP.take(); - } -}; -} - - -static bool -initializeSourceManager(Preprocessor &pp, - const std::string &inFile, - const std::string &sourceStr) -{ - // Figure out where to get and map in the main file. - SourceManager &sourceMgr = pp.getSourceManager(); - FileManager &fileMgr = pp.getFileManager(); - - if (inFile != "-") { - const FileEntry *file = fileMgr.getFile(inFile); - if (file) sourceMgr.createMainFileID(file, SourceLocation()); - if (sourceMgr.getMainFileID().isInvalid()) { - pp.getDiagnostics().Report(FullSourceLoc(), diag::err_fe_error_reading) - << inFile.c_str(); - return true; - } - } else { - const char *source = sourceStr.c_str(); - llvm::MemoryBuffer *sb = - llvm::MemoryBuffer::getMemBuffer(source, - &source[sourceStr.size()], - "OpenCL Source"); - - sourceMgr.createMainFileIDForMemBuffer(sb); - if (sourceMgr.getMainFileID().isInvalid()) { - pp.getDiagnostics().Report(FullSourceLoc(), - diag::err_fe_error_reading_stdin); - return true; - } - } - - return false; -} - -static void initializeIncludePaths(clang::HeaderSearch &headers, - clang::FileManager &fm, - const clang::LangOptions &lang) -{ - clang::InitHeaderSearch init(headers, true, "/"); - - init.AddDefaultEnvVarPaths(lang); - init.AddDefaultSystemIncludePaths(lang); - - init.Realize(); -} Compiler::Compiler() { @@ -152,114 +39,50 @@ Compiler::~Compiler() void Compiler::init() { - m_langOptions.OpenCL = 1; - m_langOptions.AltiVec = 1; - m_langOptions.CXXOperatorNames = 1; - m_langOptions.LaxVectorConversions = 1; - m_langOptions.C99 = 1; - m_langOptions.HexFloats = 1; - m_langOptions.BCPLComment = 1; - m_langOptions.Digraphs = 1; - m_langOptions.Bool = 1; +#if 0 + // Initialize a compiler invocation object from the clang (-cc1) arguments. + const driver::ArgStringList &CCArgs = Cmd->getArguments(); + llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); + CompilerInvocation::CreateFromArgs(*CI, + const_cast<const char **>(CCArgs.data()), + const_cast<const char **>(CCArgs.data()) + + CCArgs.size(), + Diags); + + // Show the invocation, with -v. + if (CI->getHeaderSearchOpts().Verbose) { + llvm::errs() << "clang invocation:\n"; + C->PrintJob(llvm::errs(), C->getJobs(), "\n", true); + llvm::errs() << "\n"; + } + + // FIXME: This is copied from cc1_main.cpp; simplify and eliminate. + + // Create a compiler instance to handle the actual work. + CompilerInstance Clang; + Clang.setLLVMContext(new llvm::LLVMContext); + Clang.setInvocation(CI.take()); + + // Create the compilers actual diagnostics engine. + Clang.createDiagnostics(int(CCArgs.size()),const_cast<char**>( + CCArgs.data())); + if (!Clang.hasDiagnostics()) + return 1; + + // Infer the builtin include path if unspecified. + if (Clang.getHeaderSearchOpts().UseBuiltinIncludes && + Clang.getHeaderSearchOpts().ResourceDir.empty()) + Clang.getHeaderSearchOpts().ResourceDir = + CompilerInvocation::GetResourcesPath(argv[0], MainAddr); +#endif } llvm::Module * Compiler::compile(const std::string &text) { - llvm::OwningPtr<ASTConsumer> consumer; - std::string moduleId; - std::string llvmIr; - llvm::raw_string_ostream output(llvmIr); - llvm::OwningPtr<clang::DiagnosticClient> diagClient; - std::vector<std::string> optWarnings; - std::string targetTriple = llvm::sys::getHostTriple(); - llvm::OwningPtr<clang::SourceManager> sourceMgr; - - diagClient.reset(new clang::TextDiagnosticPrinter(llvm::errs())); - clang::Diagnostic diags(diagClient.get()); - - if (clang::ProcessWarningOptions(diags, - optWarnings, - true /*pedantic warnings*/, - false /*OptPedanticErrors */, - false /*OptNoWarnings*/)) + // Create and execute the frontend to generate an LLVM bitcode module. + llvm::OwningPtr<CodeGenAction> act(new EmitLLVMOnlyAction()); + if (!m_clang.ExecuteAction(*act)) return 0; - // Set an error handler, so that any LLVM backend diagnostics go through our - // error handler. - llvm::llvm_install_error_handler(llvmErrorHandler, - static_cast<void*>(&diags)); - - llvm::OwningPtr<clang::TargetInfo> target( - clang::TargetInfo::CreateTargetInfo(targetTriple)); - - if (target == 0) { - diags.Report(clang::FullSourceLoc(), clang::diag::err_fe_unknown_triple) - << targetTriple.c_str(); - return 0; - } - - llvm::StringMap<bool> features; - target->getDefaultFeatures(m_targetCpu, features); - - if (!sourceMgr) - sourceMgr.reset(new clang::SourceManager()); - else - sourceMgr->clearIDTables(); - - diagClient->setLangOptions(&m_langOptions); - - // Allow the target to set the default the langauge options as it sees fit. - target->getDefaultLangOptions(m_langOptions); - - // Pass the map of target features to the target for validation and - // processing. - target->HandleTargetFeatures(features); - - clang::FileManager fileMgr; - clang::HeaderSearch headerInfo(fileMgr); - initializeIncludePaths(headerInfo, fileMgr, m_langOptions); - - // Set up the preprocessor with these options. - DriverPreprocessorFactory ppFactory(diags, m_langOptions, *target, - *sourceMgr.get(), headerInfo); - - llvm::OwningPtr<Preprocessor> pp(ppFactory.CreatePreprocessor()); - - if (!pp) - return 0; - - if (initializeSourceManager(*pp.get(), std::string(), text)) - return 0; - - pp->getBuiltinInfo().InitializeBuiltins(pp->getIdentifierTable(), - pp->getLangOptions().NoBuiltin); - - consumer.reset(clang::CreateBackendConsumer( - clang::Backend_EmitLL, - pp->getDiagnostics(), - pp->getLangOptions(), - m_compileOptions, - moduleId, - &output, - llvm::getGlobalContext())); - - llvm::OwningPtr<ASTContext> contextOwner; - if (consumer) - contextOwner.reset(new ASTContext(pp->getLangOptions(), - pp->getSourceManager(), - pp->getTargetInfo(), - pp->getIdentifierTable(), - pp->getSelectorTable(), - pp->getBuiltinInfo(), - /* FreeMemory = */ true, - /* size_reserve = */0)); - - if (consumer) - ParseAST(*pp.get(), consumer.get(), *contextOwner.get(), - false, true); - - consumer.reset(); - contextOwner.reset(); - - return 0; + return act->takeModule(); } diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index 987a5a9..ed74afa 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -1,28 +1,27 @@ -#ifndef COMPILER_H -#define COMPILER_H +#ifndef COAL_COMPILER_H +#define COAL_COMPILER_H -#include <clang/Basic/LangOptions.h> -#include <clang/Frontend/CompileOptions.h> +#include <clang/Frontend/CompilerInstance.h> namespace llvm { class Module; } -class Compiler -{ -public: - Compiler(); - ~Compiler(); +namespace Coal { + class Compiler + { + public: + Compiler(); + ~Compiler(); - llvm::Module *compile(const std::string &text); + llvm::Module *compile(const std::string &text); -private: - void init(); + private: + void init(); -private: - clang::LangOptions m_langOptions; - clang::CompileOptions m_compileOptions; - std::string m_targetCpu; -}; + private: + clang::CompilerInstance m_clang; + }; +} #endif diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 39b7481..ca88e2e 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -1,9 +1,20 @@ include_directories (${Coal_SOURCE_DIR}/include ${Coal_SOURCE_DIR}/src/tools - ${Coal_SOURCE_DIR}/src/compiler) + ${Coal_SOURCE_DIR}/src/compiler + ${LLVM_INCLUDE_DIR} + ${CLANG_INCLUDE_DIRS}) + +set(COMPILER_SRC_FILES + clcompiler.cpp) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") link_directories(${Coal_BINARY_DIR}/src) -add_executable(clcompiler clcompiler.cpp ${Coal_SOURCE_DIR}/src/compiler) +set_source_files_properties( ${COMPILER_SRC_FILES} + PROPERTIES COMPILE_FLAGS ${LLVM_COMPILE_FLAGS}) + +add_executable(clcompiler ${COMPILER_SRC_FILES} ${Coal_SOURCE_DIR}/src/compiler) -target_link_libraries(clcompiler OpenCL) +target_link_libraries(clcompiler OpenCL ${CLANG_LIBS} + ${LLVM_LIBS_CORE}) diff --git a/src/tools/clcompiler.cpp b/src/tools/clcompiler.cpp index 5d24132..9770944 100644 --- a/src/tools/clcompiler.cpp +++ b/src/tools/clcompiler.cpp @@ -10,7 +10,7 @@ void usage(const char *progName) int main(int argc, char **argv) { - Compiler compiler; + Coal::Compiler compiler; if (argc < 2) { usage(argv[0]); |