Add the 'core' static lib that will contain the std libs

This commit is contained in:
Sameer Rahmani 2022-06-11 18:11:00 +01:00
parent b922284a39
commit 05fdd9f1b2
8 changed files with 447 additions and 216 deletions

View File

@ -17,7 +17,7 @@ cmake_minimum_required(VERSION 3.19)
# Project name and a few useful settings. Other commands can pick up the results
project(Serene
VERSION 0.1.0
VERSION 1.0.0
DESCRIPTION "Serene language is a modern Lisp."
LANGUAGES CXX C)
@ -214,8 +214,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
llvm_map_components_to_libnames(llvm_libs support)
# Serene Setup ===================================
# We don't want the generated files from table gen
@ -233,11 +231,21 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
"${LLVM_COMMON_CMAKE_UTILS}/Modules"
)
# Hide all the symbols by default
if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET AND
NOT DEFINED CMAKE_VISIBILITY_INLINES_HIDDEN)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
endif()
include(tablegen-serene)
# Create the tools we use to compile Serene
add_subdirectory(serene-tblgen)
# The compiled library code is here
# The static library containing builtin special forms and functions
add_subdirectory(core)
# Binary tools of the compiler
add_subdirectory(serenec)
add_subdirectory(serene-repl)
# The compiled library code is here

83
core/CMakeLists.txt Normal file
View File

@ -0,0 +1,83 @@
# Serene Programming Language
#
# Copyright (c) 2019-2022 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/>.
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_subdirectory(lib)
get_target_property(LIBNAME Serene::core CMAKE_PKG_NAME)
# Install rules for libserene target
install(TARGETS core
EXPORT CoreExports
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
# Install rules for the public header files.
install(DIRECTORY ${INCLUDE_DIR}/serene
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN *.h
PATTERN *.td
PATTERN "CMake*" EXCLUDE)
# Install rule for the public generated header files
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN *.h
PATTERN *.td
PATTERN *.h.inc
PATTERN "CMake*" EXCLUDE)
include(CMakePackageConfigHelpers)
# Package config file let us use find_package with serene
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/${LIBNAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${LIBNAME}Config.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/serene-core-${PROJECT_VERSION}
)
write_basic_package_version_file(
"CoreConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${LIBNAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${LIBNAME}ConfigVersion.cmake"
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/serene-core-${PROJECT_VERSION}
)
# Install the package exports
install(EXPORT CoreExports
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/serene-core-${PROJECT_VERSION}
NAMESPACE serene::)
# Testing only available if this is the main app
# Emergency override SERENE_CMAKE_BUILD_TESTING provided as well
if(SERENE_BUILD_TESTING)
message("Build the test binary")
add_subdirectory(tests)
endif()

View File

@ -0,0 +1,21 @@
# Serene Programming Language
#
# Copyright (c) 2019-2022 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/>.
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Exports.cmake")
check_required_components("@PROJECT_NAME@")

View File

@ -0,0 +1,29 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2022 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_CORE_READER_H
#define SERENE_CORE_READER_H
#include "serene/export.h"
namespace serene {
extern "C" int SERENE_EXPORT reader() asm("serene.core/reader");
int example() { return 1 + 2; }
} // namespace serene
#endif

70
core/lib/CMakeLists.txt Normal file
View File

@ -0,0 +1,70 @@
# Serene Programming Language
#
# Copyright (c) 2019-2022 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/>.
#TODO: To support MacOS look into cmake's public headers
# https://cmake.org/cmake/help/latest/prop_tgt/PUBLIC_HEADER.html
# Prevent any future RPATH issue on Posix
if(NOT APPLE)
set(CMAKE_INSTALL_RPATH $ORIGIN)
endif()
add_library(core STATIC
reader.cpp)
# Create an ALIAS target. This way if we mess up the name
# there will be an cmake error inseat of a linker error which is harder
# to understand. So any binary that wants to use serene has to
# use `Serene::core` alias instead
add_library(Serene::core ALIAS core)
set_target_properties(core PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
CMAKE_PKG_NAME SereneCore
# Warn on unused libs
LINK_WHAT_YOU_USE TRUE
CXX_INCLUDE_WHAT_YOU_USE "include-what-you-use"
# LTO support
INTERPROCEDURAL_OPTIMIZATION TRUE)
if(SERENE_ENABLE_TIDY)
set_target_properties(core PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH})
endif()
if (CPP_20_SUPPORT)
target_compile_features(core PUBLIC cxx_std_20)
else()
target_compile_features(core PUBLIC cxx_std_17)
endif()
# We need this directory, and users of our library will need it too
target_include_directories(core PUBLIC "$<BUILD_INTERFACE:${INCLUDE_DIR}>")
target_include_directories(core PUBLIC "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>")
# Generate the export.h
include(GenerateExportHeader)
generate_export_header(core EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/include/serene/core/export.h)
if(SERENE_SHOW_LLVM_LIBS)
execute_process(COMMAND llvm-config --libs all
OUTPUT_VARIABLE SERENE_LLVM_LIBS)
message(STATUS "LLVM libs available:\n ${SERENE_LLVM_LIBS}")
endif()
target_link_libraries(core PRIVATE)

25
core/lib/reader.cpp Normal file
View File

@ -0,0 +1,25 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2022 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/>.
*/
#include "serene/core/reader.h"
namespace serene {
int reader() { return 0; };
} // namespace serene

View File

@ -17,13 +17,6 @@
#TODO: To support MacOS look into cmake's public headers
# https://cmake.org/cmake/help/latest/prop_tgt/PUBLIC_HEADER.html
# Hide all the symbols by default
if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET AND
NOT DEFINED CMAKE_VISIBILITY_INLINES_HIDDEN)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
endif()
# Prevent any future RPATH issue on Posix
if(NOT APPLE)
set(CMAKE_INSTALL_RPATH $ORIGIN)

View File

@ -16,98 +16,99 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "serene/jit/halley.h"
#include "serene/namespace.h"
#include "serene/reader/location.h"
#include "serene/reader/reader.h"
#include "serene/semantics.h"
#include "serene/serene.h"
#include "serene/slir/generatable.h"
#include "serene/slir/slir.h"
// #include "serene/jit/halley.h"
// #include "serene/namespace.h"
// #include "serene/reader/location.h"
// #include "serene/reader/reader.h"
// #include "serene/semantics.h"
// #include "serene/serene.h"
// #include "serene/slir/generatable.h"
// #include "serene/slir/slir.h"
#include <lld/Common/Driver.h>
// #include <lld/Common/Driver.h>
#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/ADT/ArrayRef.h>
#include <llvm/ADT/SmallString.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/IR/LegacyPassManager.h>
//#include <llvm/MC/TargetRegistry.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/FormatVariadic.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h>
// #include <clang/Driver/Compilation.h>
// #include <clang/Driver/Driver.h>
// #include <clang/Frontend/TextDiagnosticPrinter.h>
// #include <clang/Tooling/Tooling.h>
// #include <llvm/ADT/ArrayRef.h>
// #include <llvm/ADT/SmallString.h>
// #include <llvm/ADT/StringRef.h>
// #include <llvm/IR/LegacyPassManager.h>
// //#include <llvm/MC/TargetRegistry.h>
// #include <llvm/Support/CommandLine.h>
// #include <llvm/Support/FileSystem.h>
// #include <llvm/Support/FormatVariadic.h>
// #include <llvm/Support/Host.h>
// #include <llvm/Support/Path.h>
// #include <llvm/Support/raw_ostream.h>
// #include <llvm/Target/TargetMachine.h>
// #include <llvm/Target/TargetOptions.h>
#include <memory>
// #include <memory>
using namespace std;
using namespace serene;
// using namespace std;
// using namespace serene;
namespace cl = llvm::cl;
// namespace cl = llvm::cl;
namespace {
enum Action {
None,
DumpAST,
DumpSLIR,
DumpMLIR,
DumpSemantic,
DumpLIR,
DumpIR,
CompileToObject,
Compile,
// TODO: Remove this option and replace it by a subcommand
RunJIT,
};
} // namespace
// namespace {
// enum Action {
// None,
// DumpAST,
// DumpSLIR,
// DumpMLIR,
// DumpSemantic,
// DumpLIR,
// DumpIR,
// CompileToObject,
// Compile,
// // TODO: Remove this option and replace it by a subcommand
// RunJIT,
// };
// } // namespace
static std::string banner =
llvm::formatv("\n\nSerene Compiler Version {0}"
"\nCopyright (C) 2019-2022 "
"Sameer Rahmani <lxsameer@gnu.org>\n"
"Serene comes with ABSOLUTELY NO WARRANTY;\n"
"This is free software, and you are welcome\n"
"to redistribute it under certain conditions; \n"
"for details take a look at the LICENSE file.\n",
SERENE_VERSION);
// static std::string banner =
// llvm::formatv("\n\nSerene Compiler Version {0}"
// "\nCopyright (C) 2019-2022 "
// "Sameer Rahmani <lxsameer@gnu.org>\n"
// "Serene comes with ABSOLUTELY NO WARRANTY;\n"
// "This is free software, and you are welcome\n"
// "to redistribute it under certain conditions; \n"
// "for details take a look at the LICENSE file.\n",
// SERENE_VERSION);
static cl::opt<std::string> inputNS(cl::Positional, cl::desc("<namespace>"),
cl::Required);
// static cl::opt<std::string> inputNS(cl::Positional, cl::desc("<namespace>"),
// cl::Required);
static cl::opt<std::string> outputFile(
"o", cl::desc("The relative path to the output file from the build dir"),
cl::init("-"), cl::value_desc("filename"));
// static cl::opt<std::string> outputFile(
// "o", cl::desc("The relative path to the output file from the build dir"),
// cl::init("-"), cl::value_desc("filename"));
static cl::opt<std::string>
outputDir("b", cl::desc("The absolute path to the build directory"),
cl::value_desc("dir"), cl::Required);
// static cl::opt<std::string>
// outputDir("b", cl::desc("The absolute path to the build directory"),
// cl::value_desc("dir"), cl::Required);
static cl::opt<enum Action> emitAction(
"emit", cl::desc("Select what to dump."), cl::init(Compile),
cl::values(clEnumValN(DumpSemantic, "semantic",
"Output the AST after one level of analysis only")),
cl::values(clEnumValN(DumpIR, "ir", "Output the lowered IR only")),
cl::values(clEnumValN(DumpSLIR, "slir", "Output the SLIR only")),
cl::values(clEnumValN(DumpMLIR, "mlir",
"Output the MLIR only (Lowered SLIR)")),
cl::values(clEnumValN(DumpLIR, "lir",
"Output the LIR only (Lowerd to LLVM dialect)")),
cl::values(clEnumValN(DumpAST, "ast", "Output the AST only")),
cl::values(clEnumValN(CompileToObject, "object",
"Compile to object file.")),
cl::values(clEnumValN(Compile, "target",
"Compile to target code. (Default)")),
cl::values(clEnumValN(RunJIT, "jit",
"Run the give input file with the JIT."))
// static cl::opt<enum Action> emitAction(
// "emit", cl::desc("Select what to dump."), cl::init(Compile),
// cl::values(clEnumValN(DumpSemantic, "semantic",
// "Output the AST after one level of analysis
// only")),
// cl::values(clEnumValN(DumpIR, "ir", "Output the lowered IR only")),
// cl::values(clEnumValN(DumpSLIR, "slir", "Output the SLIR only")),
// cl::values(clEnumValN(DumpMLIR, "mlir",
// "Output the MLIR only (Lowered SLIR)")),
// cl::values(clEnumValN(DumpLIR, "lir",
// "Output the LIR only (Lowerd to LLVM dialect)")),
// cl::values(clEnumValN(DumpAST, "ast", "Output the AST only")),
// cl::values(clEnumValN(CompileToObject, "object",
// "Compile to object file.")),
// cl::values(clEnumValN(Compile, "target",
// "Compile to target code. (Default)")),
// cl::values(clEnumValN(RunJIT, "jit",
// "Run the give input file with the JIT."))
);
// );
// int dumpAsObject(Namespace &ns) {
// // TODO: Move the compilation process to the Namespace class
@ -255,142 +256,143 @@ static cl::opt<enum Action> emitAction(
// return 0;
// };
int main(int argc, char *argv[]) {
initCompiler();
registerSereneCLOptions();
int main() {
// initCompiler();
// registerSereneCLOptions();
cl::ParseCommandLineOptions(argc, argv, banner);
// cl::ParseCommandLineOptions(argc, argv, banner);
auto ctx = makeSereneContext();
// auto ctx = makeSereneContext();
applySereneCLOptions(*ctx);
// applySereneCLOptions(*ctx);
// TODO: handle the outputDir by not forcing it. it should be
// default to the current working dir
if (outputDir == "-") {
llvm::errs() << "Error: The build directory is not set. Did you forget to "
"use '-b'?\n";
return 1;
}
// // TODO: handle the outputDir by not forcing it. it should be
// // default to the current working dir
// if (outputDir == "-") {
// llvm::errs() << "Error: The build directory is not set. Did you forget to
// "
// "use '-b'?\n";
// return 1;
// }
switch (emitAction) {
case Action::RunJIT: {
// TODO: Replace it by a proper jit configuration
ctx->setOperationPhase(CompilationPhase::NoOptimization);
break;
};
// Just print out the raw AST
case Action::DumpAST: {
ctx->setOperationPhase(CompilationPhase::Parse);
break;
};
case Action::DumpSemantic: {
ctx->setOperationPhase(CompilationPhase::Analysis);
break;
};
case Action::DumpSLIR: {
ctx->setOperationPhase(CompilationPhase::SLIR);
break;
}
case Action::DumpMLIR: {
ctx->setOperationPhase(CompilationPhase::MLIR);
break;
}
case Action::DumpLIR: {
ctx->setOperationPhase(CompilationPhase::LIR);
break;
}
case Action::DumpIR: {
ctx->setOperationPhase(CompilationPhase::IR);
break;
}
case Action::CompileToObject: {
ctx->setOperationPhase(CompilationPhase::NoOptimization);
break;
}
case Action::Compile: {
ctx->setOperationPhase(CompilationPhase::NoOptimization);
break;
}
default: {
llvm::errs() << "No action specified. TODO: Print out help here\n";
return 1;
}
}
auto runLoc = reader::LocationRange::UnknownLocation(inputNS);
auto maybeNS = ctx->importNamespace(inputNS, runLoc);
if (!maybeNS) {
auto err = maybeNS.takeError();
throwErrors(*ctx, err);
return (int)std::errc::no_such_file_or_directory;
}
auto ns = *maybeNS;
switch (emitAction) {
case Action::DumpAST:
case Action::DumpSemantic: {
auto ast = ns->getTree();
llvm::outs() << exprs::astToString(&ast) << "\n";
return 0;
}
case Action::DumpSLIR:
case Action::DumpMLIR:
case Action::DumpLIR: {
ns->dump();
break;
};
case Action::DumpIR: {
auto maybeModule = ns->compileToLLVM();
if (!maybeModule) {
llvm::errs() << "Failed to generate the IR.\n";
return 1;
}
auto tsm = std::move(*maybeModule);
tsm.withModuleDo([](auto &m) { m.dump(); });
break;
};
// switch (emitAction) {
// case Action::RunJIT: {
// auto maybeJIT = JIT::make(*ns);
// if (!maybeJIT) {
// // TODO: panic in here: "Couldn't creat the JIT!"
// return -1;
// }
// auto jit = std::move(maybeJIT.getValue());
// if (jit->invoke("main")) {
// llvm::errs() << "Faild to invoke the 'main' function.\n";
// return 1;
// }
// llvm::outs() << "Done!";
// // TODO: Replace it by a proper jit configuration
// ctx->setOperationPhase(CompilationPhase::NoOptimization);
// break;
// };
// case Action::Compile:
// case Action::CompileToObject: {
// return dumpAsObject(*ns);
// // Just print out the raw AST
// case Action::DumpAST: {
// ctx->setOperationPhase(CompilationPhase::Parse);
// break;
// };
default: {
llvm::errs() << "Action is not supported yet!\n";
};
}
// case Action::DumpSemantic: {
// ctx->setOperationPhase(CompilationPhase::Analysis);
// break;
// };
// case Action::DumpSLIR: {
// ctx->setOperationPhase(CompilationPhase::SLIR);
// break;
// }
// case Action::DumpMLIR: {
// ctx->setOperationPhase(CompilationPhase::MLIR);
// break;
// }
// case Action::DumpLIR: {
// ctx->setOperationPhase(CompilationPhase::LIR);
// break;
// }
// case Action::DumpIR: {
// ctx->setOperationPhase(CompilationPhase::IR);
// break;
// }
// case Action::CompileToObject: {
// ctx->setOperationPhase(CompilationPhase::NoOptimization);
// break;
// }
// case Action::Compile: {
// ctx->setOperationPhase(CompilationPhase::NoOptimization);
// break;
// }
// default: {
// llvm::errs() << "No action specified. TODO: Print out help here\n";
// return 1;
// }
// }
// auto runLoc = reader::LocationRange::UnknownLocation(inputNS);
// auto maybeNS = ctx->importNamespace(inputNS, runLoc);
// if (!maybeNS) {
// auto err = maybeNS.takeError();
// throwErrors(*ctx, err);
// return (int)std::errc::no_such_file_or_directory;
// }
// auto ns = *maybeNS;
// switch (emitAction) {
// case Action::DumpAST:
// case Action::DumpSemantic: {
// auto ast = ns->getTree();
// llvm::outs() << exprs::astToString(&ast) << "\n";
// return 0;
// }
// case Action::DumpSLIR:
// case Action::DumpMLIR:
// case Action::DumpLIR: {
// ns->dump();
// break;
// };
// case Action::DumpIR: {
// auto maybeModule = ns->compileToLLVM();
// if (!maybeModule) {
// llvm::errs() << "Failed to generate the IR.\n";
// return 1;
// }
// auto tsm = std::move(*maybeModule);
// tsm.withModuleDo([](auto &m) { m.dump(); });
// break;
// };
// // case Action::RunJIT: {
// // auto maybeJIT = JIT::make(*ns);
// // if (!maybeJIT) {
// // // TODO: panic in here: "Couldn't creat the JIT!"
// // return -1;
// // }
// // auto jit = std::move(maybeJIT.getValue());
// // if (jit->invoke("main")) {
// // llvm::errs() << "Faild to invoke the 'main' function.\n";
// // return 1;
// // }
// // llvm::outs() << "Done!";
// // break;
// // };
// // case Action::Compile:
// // case Action::CompileToObject: {
// // return dumpAsObject(*ns);
// // };
// default: {
// llvm::errs() << "Action is not supported yet!\n";
// };
// }
return 0;
}