diff --git a/CMakeLists.txt b/CMakeLists.txt index 0de7e07..a486fb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -209,6 +209,21 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) include_directories(SYSTEM ${PROJECT_BINARY_DIR}/include) include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/) + # Make sure that our source directory is on the current cmake module path so + # that we can include cmake files from this directory. + list(INSERT CMAKE_MODULE_PATH 0 + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" + "${LLVM_COMMON_CMAKE_UTILS}/Modules" + ) + + include(tablegen-serene) + + # Create the tools we use to compile Serene + add_subdirectory(tools) + + # Make sure that serene_tablegen can use the tbl-srn result + set(SERENE_TABLEGEN_EXE "$") + # The compiled library code is here add_subdirectory(src) # The executable code is here diff --git a/cmake/#tablegen-serene.cmake# b/cmake/#tablegen-serene.cmake# new file mode 100644 index 0000000..d34e661 --- /dev/null +++ b/cmake/#tablegen-serene.cmake# @@ -0,0 +1,89 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2021 Sameer Rahmani +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +macro(add_ target project) + set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS}) + set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen) + + # CMake doesn't let compilation units depend on their dependent libraries on some generators. + if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND NOT XCODE) + # FIXME: It leaks to user, callee of add_tablegen. + set(LLVM_ENABLE_OBJLIB ON) + endif() + + + set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS}) + + set(${project}_TABLEGEN "${target}" CACHE + STRING "Native TableGen executable. Saves building one when cross-compiling.") + + # Effective tblgen executable to be used: + set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE) + set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE) + + if(LLVM_USE_HOST_TOOLS) + if( ${${project}_TABLEGEN} STREQUAL "${target}" ) + # The NATIVE tablegen executable *must* depend on the current target one + # otherwise the native one won't get rebuilt when the tablgen sources + # change, and we end up with incorrect builds. + build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target}) + set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE) + + add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE}) + set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE) + + # Create an artificial dependency between tablegen projects, because they + # compile the same dependencies, thus using the same build folders. + # FIXME: A proper fix requires sequentially chaining tablegens. + if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND + TARGET LLVM-tablegen-host) + add_dependencies(${project}-tablegen-host LLVM-tablegen-host) + endif() + + # If we're using the host tablegen, and utils were not requested, we have no + # need to build this tablegen. + if ( NOT LLVM_BUILD_UTILS ) + set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON) + endif() + endif() + endif() + + if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS) + set(export_to_llvmexports) + if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR + NOT LLVM_DISTRIBUTION_COMPONENTS) + set(export_to_llvmexports EXPORT LLVMExports) + endif() + + install(TARGETS ${target} + ${export_to_llvmexports} + COMPONENT ${target} + RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR}) + if(NOT LLVM_ENABLE_IDE) + add_llvm_install_targets("install-${target}" + DEPENDS ${target} + COMPONENT ${target}) + endif() + endif() + set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target}) +endmacro() + +function(serene_tablegen ofn) + tablegen(SERENE ${ARGV}) + set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} + PARENT_SCOPE) + include_directories(${CMAKE_CURRENT_BINARY_DIR}) +endfunction() diff --git a/cmake/.#tablegen-serene.cmake b/cmake/.#tablegen-serene.cmake new file mode 120000 index 0000000..b293748 --- /dev/null +++ b/cmake/.#tablegen-serene.cmake @@ -0,0 +1 @@ +lxsameer@underworld.2515108:1638986649 \ No newline at end of file diff --git a/cmake/tablegen-serene.cmake b/cmake/tablegen-serene.cmake new file mode 100644 index 0000000..c628899 --- /dev/null +++ b/cmake/tablegen-serene.cmake @@ -0,0 +1,22 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2021 Sameer Rahmani +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +function(serene_tablegen ofn) + tablegen(SERENE ${ARGV}) + set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} + PARENT_SCOPE) + include_directories(${CMAKE_CURRENT_BINARY_DIR}) +endfunction() diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 0c406cd..1542268 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory("serene/slir/") +add_subdirectory("serene/errors/") diff --git a/include/serene/errors/CMakeLists.txt b/include/serene/errors/CMakeLists.txt new file mode 100644 index 0000000..6d52b63 --- /dev/null +++ b/include/serene/errors/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_TARGET_DEFINITIONS errors.td) + +# serene_tablegen(ops.h.inc -gen-op-decls) +# serene_tablegen(ops.cpp.inc -gen-op-defs) +# serene_tablegen(types.h.inc -gen-typedef-decls) +# serene_tablegen(types.cpp.inc -gen-typedef-defs) +# serene_tablegen(dialect.h.inc -gen-dialect-decls) +# serene_tablegen(dialect.cpp.inc -gen-dialect-defs) + +#add_public_tablegen_target(SereneErrorGen) diff --git a/include/serene/errors/errors.td b/include/serene/errors/errors.td new file mode 100644 index 0000000..bfd4a27 --- /dev/null +++ b/include/serene/errors/errors.td @@ -0,0 +1 @@ +def Err; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 046eabd..a971296 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,7 @@ add_subdirectory(libserene) add_subdirectory(serenec) add_subdirectory(serene-repl) + # Testing only available if this is the main app # Emergency override SERENE_CMAKE_BUILD_TESTING provided as well if(SERENE_BUILD_TESTING) diff --git a/src/libserene/CMakeLists.txt b/src/libserene/CMakeLists.txt index f12c984..6fcbc01 100644 --- a/src/libserene/CMakeLists.txt +++ b/src/libserene/CMakeLists.txt @@ -110,7 +110,7 @@ else() endif() # Generate the tablegen ODS files before this target -add_dependencies(serene SereneDialectGen) +add_dependencies(serene tbl-srn SereneDialectGen ) #SereneErrorGen # We need this directory, and users of our library will need it too target_include_directories(serene PUBLIC "$") diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 0000000..2862157 --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,17 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2021 Sameer Rahmani +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +add_subdirectory(tbl-srn) diff --git a/tools/tbl-srn/CMakeLists.txt b/tools/tbl-srn/CMakeLists.txt new file mode 100644 index 0000000..97bbdd8 --- /dev/null +++ b/tools/tbl-srn/CMakeLists.txt @@ -0,0 +1,32 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2021 Sameer Rahmani +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set(LLVM_LINK_COMPONENTS Support TableGen) + +add_executable(tbl-srn + errors-backend.cpp + main.cpp +) + +set(SERENE_TABLEGEN "tbl-srn" CACHE + STRING "Native TableGen executable. Saves building one when cross-compiling.") + +# Effective tblgen executable to be used: +set(SERENE_TABLEGEN_EXE ${SERENE_TABLEGEN} PARENT_SCOPE) +set(SERENE_TABLEGEN_TARGET ${SERENE_TABLEGEN} PARENT_SCOPE) + +target_link_libraries(tbl-srn PRIVATE LLVMTableGenGlobalISel) +set_target_properties(tbl-srn PROPERTIES FOLDER "tbl-srn") diff --git a/tools/tbl-srn/errors-backend.cpp b/tools/tbl-srn/errors-backend.cpp new file mode 100644 index 0000000..3e0d206 --- /dev/null +++ b/tools/tbl-srn/errors-backend.cpp @@ -0,0 +1,50 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2021 Sameer Rahmani + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include + +#define DEBUG_TYPE "errors-backend" + +namespace serene { + +// Any helper data structures can be defined here. Some backends use +// structs to collect information from the records. + +class ErrorsBackend { +private: + llvm::RecordKeeper &records; + +public: + ErrorsBackend(llvm::RecordKeeper &rk) : records(rk) {} + + void run(llvm::raw_ostream &os); +}; // emitter class + +void ErrorsBackend::run(llvm::raw_ostream &os) { + llvm::emitSourceFileHeader("Serene's Errors collection", os); + + (void)records; +} + +void emitErrors(llvm::RecordKeeper &rk, llvm::raw_ostream &os) { + ErrorsBackend(rk).run(os); +} + +} // namespace serene diff --git a/tools/tbl-srn/errors-backend.h b/tools/tbl-srn/errors-backend.h new file mode 100644 index 0000000..ceec67d --- /dev/null +++ b/tools/tbl-srn/errors-backend.h @@ -0,0 +1,28 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2021 Sameer Rahmani + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#define DEBUG_TYPE "errors-backend" + +namespace serene { + +void emitErrors(llvm::RecordKeeper &rk, llvm::raw_ostream &os); + +} // namespace serene diff --git a/tools/tbl-srn/main.cpp b/tools/tbl-srn/main.cpp new file mode 100644 index 0000000..4f2e411 --- /dev/null +++ b/tools/tbl-srn/main.cpp @@ -0,0 +1,74 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2021 Sameer Rahmani + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * Commentary: + * This is the main file for Serene's tablegen instance. All the backends + * are local to this instance and we use this instance alongside the official + * instances. + */ + +#include + +#include +#include +#include +#include +#include + +namespace cl = llvm::cl; + +namespace serene { +enum ActionType { ErrorsBackend }; + +cl::opt + action(cl::desc("Action to perform:"), + cl::values(clEnumValN( + ErrorsBackend, "errors-backend", + "Generate a C++ file containing errors in the input file"))); + +bool llvmTableGenMain(llvm::raw_ostream &os, llvm::RecordKeeper &records) { + switch (action) { + case ErrorsBackend: + emitErrors(records, os); + break; + } + return false; +} +} // namespace serene + +int main(int argc, char **argv) { + llvm::InitLLVM x(argc, argv); + cl::ParseCommandLineOptions(argc, argv); + + return llvm::TableGenMain(argv[0], &serene::llvmTableGenMain); +} + +#ifndef __has_feature +#define __has_feature(x) 0 +#endif + +#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) || \ + __has_feature(leak_sanitizer) + +#include +// Disable LeakSanitizer for this binary as it has too many leaks that are not +// very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h . +LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; } + +#endif