Add tbl-srn generator to the cmake pipeline

This commit is contained in:
Sameer Rahmani 2022-01-27 11:29:58 +00:00
parent 7638c631ed
commit cff5c28c95
8 changed files with 84 additions and 30 deletions

View File

@ -25,7 +25,7 @@ project(Serene
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
# Policies ==========================
cmake_policy(SET CMP0116 NEW)
cmake_policy(SET CMP0116 OLD)
# User Options ======================
option(CPP_20_SUPPORT "C++20 Support" OFF)
@ -45,6 +45,7 @@ option(SERENE_SHOW_LLVM_LIBS "Print all the llvm libs available" OFF)
option(SERENE_WITH_MLIR_CL_OPTION "Add support for controlling MLIR via command line options" OFF)
# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
## Settings =======================
@ -212,8 +213,8 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# 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"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/"
"${LLVM_COMMON_CMAKE_UTILS}/Modules"
)
include(tablegen-serene)
@ -221,9 +222,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# 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 "$<TARGET_FILE:tbl-srn>")
# The compiled library code is here
add_subdirectory(src)
# The executable code is here

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function(serene_tablegen ofn)
tablegen(SERENE ${ARGV})
set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}

View File

@ -1,10 +1,5 @@
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)
serene_tablegen(errs.h.inc -errors-backend)
#add_public_tablegen_target(SereneErrorGen)
add_public_tablegen_target(SereneErrorGen)

View File

@ -0,0 +1,61 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef SERENE_ERRORS_BASE_H
#define SERENE_ERRORS_BASE_H
#include "serene/reader/location.h"
#include <system_error>
#include <llvm/Support/Error.h>
namespace serene::errors {
template <typename T>
class SereneError : public llvm::ErrorInfo<T> {
reader::LocationRange location;
std::string msg;
public:
static const int ID = -1;
void log(llvm::raw_ostream &os) const { os << msg; };
std::string message() const override { return msg; };
std::string &getTitle() const override { return T::title; };
std::string &getDesc() const override { return T::description; };
std::error_code convertToErrorCode() const { return std::error_code(); };
SereneError(reader::LocationRange &loc, std::string &msg)
: location(loc), msg(msg){};
reader::LocationRange &where() { return location; };
static const void *classID() { return &T::ID; }
bool isA(const void *const id) const override {
// the check with -1 is a shortcut for us to figure
// out whether we're dealing with an Serene error or
// LLVM error
return *(const int *)id == -1 || id == classID() ||
llvm::ErrorInfoBase::isA(id);
}
};
}; // namespace serene::errors
#endif

View File

@ -110,7 +110,7 @@ else()
endif()
# Generate the tablegen ODS files before this target
add_dependencies(serene tbl-srn SereneDialectGen ) #SereneErrorGen
add_dependencies(serene SereneTablegen SereneDialectGen SereneErrorGen)
# We need this directory, and users of our library will need it too
target_include_directories(serene PUBLIC "$<BUILD_INTERFACE:${INCLUDE_DIR}>")

View File

@ -14,10 +14,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(LLVM_LINK_COMPONENTS Support TableGen)
#TODO: Make sure that we don't include the project wide header files
# path here
include_directories(${PROJECT_SOURCE_DIR}/tools/tbl-srn)
add_executable(tbl-srn
@ -25,12 +21,15 @@ add_executable(tbl-srn
main.cpp
)
set(SERENE_TABLEGEN "tbl-srn" CACHE
add_executable(SereneTablegen ALIAS tbl-srn)
set(SERENE_TABLEGEN $<TARGET_FILE: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)
set(SERENE_TABLEGEN_EXE SereneTablegen
CACHE STRING "Where to find the tbl-srn binary")
set(SERENE_TABLEGEN_TARGET SereneTablegen CACHE
STRING "Target name for the tbl-srn")
target_link_libraries(tbl-srn PRIVATE LLVMTableGenGlobalISel)
target_link_libraries(tbl-srn PRIVATE LLVMTableGenGlobalISel ${llvm_libs})
set_target_properties(tbl-srn PROPERTIES FOLDER "tbl-srn")

View File

@ -27,6 +27,7 @@
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/InitLLVM.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/TableGen/Main.h>
#include <llvm/TableGen/Record.h>
#include <llvm/TableGen/SetTheory.h>

View File

@ -41,8 +41,7 @@ public:
ErrorsBackend(llvm::RecordKeeper &rk) : records(rk) {}
void createNSBody(llvm::raw_ostream &os);
void createErrorClass(const int id, llvm::Record &defRec,
llvm::raw_ostream &os);
void createErrorClass(int id, llvm::Record &defRec, llvm::raw_ostream &os);
void run(llvm::raw_ostream &os);
}; // emitter class
@ -59,9 +58,9 @@ void ErrorsBackend::createErrorClass(const int id, llvm::Record &defRec,
const auto recName = defRec.getName();
os << "class " << recName << " : public llvm::ErrorInfo<" << recName
<< "> {\n";
os << " static int ID = " << id << ";\n";
os << "class " << recName << " : public SereneError<" << recName << "> {\n"
<< "public:\n"
<< " static int ID = " << id << ";\n";
for (const auto &val : defRec.getValues()) {
auto valName = val.getName();
@ -129,7 +128,9 @@ void ErrorsBackend::run(llvm::raw_ostream &os) {
(void)records;
llvm::emitSourceFileHeader("Serene's Errors collection", os);
os << "#include <llvm/Support/Error.h>\n\n";
os << "#inlude \"serene/errors/base.h\"\n\n#include "
"<llvm/Support/Error.h>\n\n";
inNamespace("serene::errors", os,
[&](llvm::raw_ostream &os) { createNSBody(os); });
}