From 23522ea491a5d4ecd91222da2672ff0f3bad5d56 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 11 Jul 2023 18:54:06 +0100 Subject: [PATCH] Add the userene binary --- CMakeLists.txt | 2 +- userene/CMakeLists.txt | 135 +++++++++++++++++++++++++++ userene/include/CMakeLists.txt | 17 ++++ userene/include/serene/commands.h | 27 ++++++ userene/include/serene/config.h.in | 22 +++++ userene/include/serene/types/types.h | 71 ++++++++++++++ userene/src/CMakeLists.txt | 21 +++++ userene/src/commands/commands.cpp | 30 ++++++ userene/src/userene.cpp | 70 ++++++++++++++ 9 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 userene/CMakeLists.txt create mode 100644 userene/include/CMakeLists.txt create mode 100644 userene/include/serene/commands.h create mode 100644 userene/include/serene/config.h.in create mode 100644 userene/include/serene/types/types.h create mode 100644 userene/src/CMakeLists.txt create mode 100644 userene/src/commands/commands.cpp create mode 100644 userene/src/userene.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b70a1d8..2861271 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,7 +184,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) # /LLVM setup ================================================================= - add_subdirectory(serene) + add_subdirectory(userene) # include(tablegen-serene) # Create the tools we use to compile Serene #add_subdirectory(serene-tblgen) diff --git a/userene/CMakeLists.txt b/userene/CMakeLists.txt new file mode 100644 index 0000000..295ad6d --- /dev/null +++ b/userene/CMakeLists.txt @@ -0,0 +1,135 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2023 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 . + +find_package(BDWgc 8.2.0 REQUIRED) + +# Main Binary ================================================================= +add_executable(userene) + +set_target_properties(userene PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + C_INCLUDE_WHAT_YOU_USE ${iwyu} + CXX_INCLUDE_WHAT_YOU_USE ${iwyu} + # Warn on unused libs + LINK_WHAT_YOU_USE TRUE +) + +if (CPP_20_SUPPORT) + target_compile_features(userene PRIVATE cxx_std_20) +else() + target_compile_features(userene PRIVATE cxx_std_17) +endif() + +# Setup header directory and auto generated headers +target_include_directories(userene + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +target_include_directories(userene SYSTEM PUBLIC + # We don't want the generated files from table gen + # to be treated as local since the contain warnings + ${PROJECT_BINARY_DIR}/serene/include) + +target_link_libraries(userene PRIVATE + LLVMSupport +) + +# Autogenerate the `config.h` file +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/serene/config.h.in include/serene/config.h) + + +target_compile_options(userene + PRIVATE + $<$>:-stdlib=libc++> + + # LLVM has it's own RTTI + -fno-rtti + -fno-builtin-strlen + + # Dedicate a section to each function, so the linker + # can do a better job on dead code elimination + -ffunction-sections + -fdata-sections + + $<$:-fsanitize=address> + $<$:-static-libsan> + $<$:-g3> + $<$:-O0> + $<$:-ggdb> + # For the sake of debugging + $<$:-fno-inline> + # To make the local ccache happy + $<$:-fdebug-prefix-map=${PROJECT_SOURCE_DIR}=.> + + # No tail call elimination on Debug to let asan provide + # better stacktrackes + $<$:-fno-optimize-sibling-calls> + + $<$:-fno-omit-frame-pointer> + $<$:-fomit-frame-pointer> + $<$:-O3> + $<$:-fmerge-all-constants> +) + +target_link_options(userene PRIVATE + $<$>:-stdlib=libc++> + $<$>:-lc++abi> + $<$>:--rtlib=compiler-rt> + + -Wl,--gc-sections + $<$:-s> + + $<$:-fsanitize=address> + $<$:-static-libsan> + # Do not link against shared libraries + --static +) + + +if(SERENE_ENABLE_BUILDID) + target_link_options(userene -Wl,--build-id) +endif() + +if(SERENE_ENABLE_TIDY) + set_target_properties(userene PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH}) +endif() + +if(SERENE_ENABLE_THINLTO) + include(CheckIPOSupported) + # Optional IPO. Do not use IPO if it's not supported by compiler. + check_ipo_supported(RESULT result OUTPUT output) + if(result) + message(STATUS "IPO is supported and is turned on") + set_property(TARGET userene PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + else() + message(WARNING "IPO is not supported: ${output}") + message(WARNING "Make sure to use lld") + endif() +endif() + + +include(GNUInstallDirs) + +install(TARGETS userene EXPORT SereneTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + +add_subdirectory(src) +add_subdirectory(include) diff --git a/userene/include/CMakeLists.txt b/userene/include/CMakeLists.txt new file mode 100644 index 0000000..6064235 --- /dev/null +++ b/userene/include/CMakeLists.txt @@ -0,0 +1,17 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2023 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("serene/slir/") diff --git a/userene/include/serene/commands.h b/userene/include/serene/commands.h new file mode 100644 index 0000000..57cacf0 --- /dev/null +++ b/userene/include/serene/commands.h @@ -0,0 +1,27 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2023 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 . + */ + +#ifndef SERENE_COMMANDS_H +#define SERENE_COMMANDS_H + +namespace serene::commands { +int cc(int argc, char **argv); +int run(); +} // namespace serene::commands + +#endif diff --git a/userene/include/serene/config.h.in b/userene/include/serene/config.h.in new file mode 100644 index 0000000..c151f63 --- /dev/null +++ b/userene/include/serene/config.h.in @@ -0,0 +1,22 @@ +#ifndef CONFIG_H +#define CONFIG_H + +// the configured options and settings + +#define SERENE_VERSION "@PROJECT_VERSION@" + +// Why so obvious? to make the linter shutup :)) +#define I8_SIZE 8 +#define I32_SIZE 32 +#define I64_SIZE 64 + +#define MAX_PATH_SLOTS 256 + +#define COMMON_ARGS_COUNT 6 + +#define PACKED_FUNCTION_NAME_PREFIX "__serene_" + +// Should we build the support for MLIR CL OPTIONS? +#cmakedefine SERENE_WITH_MLIR_CL_OPTION + +#endif diff --git a/userene/include/serene/types/types.h b/userene/include/serene/types/types.h new file mode 100644 index 0000000..4038a7e --- /dev/null +++ b/userene/include/serene/types/types.h @@ -0,0 +1,71 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2023 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 . + */ + +#ifndef SERENE_TYPES_TYPE_H +#define SERENE_TYPES_TYPE_H + +#include +namespace serene::types { + +// ============================================================================ +// Expression +// ============================================================================ +struct Expression { + const unsigned char *data; + explicit Expression(const unsigned char *data) : data(data){}; +}; + +// ============================================================================ +// Internal String +// ============================================================================ + +/// Internal string represts a smaller type of string with limited set of +/// functionalities that we use only for internal usage. for exmaple if you need +/// to bring something 2 +struct InternalString { + // We store the actual string in a "string" data section + const char *data; + size_t len; + + InternalString(const char *data, const unsigned int len) + : data(data), len(len){}; +}; + +// ============================================================================ +// Symbol +// ============================================================================ +struct Symbol { + const InternalString *ns; + const InternalString *name; + + Symbol(const InternalString *ns, const InternalString *name) + : ns(ns), name(name){}; +}; + +// ============================================================================ +// Namespace +// ============================================================================ +struct Namespace { + const InternalString *name; + + explicit Namespace(const InternalString *name) : name(name){}; +}; + +}; // namespace serene::types + +#endif diff --git a/userene/src/CMakeLists.txt b/userene/src/CMakeLists.txt new file mode 100644 index 0000000..24e8b08 --- /dev/null +++ b/userene/src/CMakeLists.txt @@ -0,0 +1,21 @@ +# Serene Programming Language +# +# Copyright (c) 2019-2023 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 . + +target_sources(userene PRIVATE + userene.cpp + + commands/commands.cpp +) diff --git a/userene/src/commands/commands.cpp b/userene/src/commands/commands.cpp new file mode 100644 index 0000000..714eb98 --- /dev/null +++ b/userene/src/commands/commands.cpp @@ -0,0 +1,30 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2023 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 "serene/commands.h" + +#include + +namespace serene::commands { +int cc(int argc, char **argv) { + printf(">> %d, %s\n", argc, argv[0]); + return 0; +} + +int run() { return 0; } +} // namespace serene::commands diff --git a/userene/src/userene.cpp b/userene/src/userene.cpp new file mode 100644 index 0000000..1fa50fb --- /dev/null +++ b/userene/src/userene.cpp @@ -0,0 +1,70 @@ +/* -*- C++ -*- + * Serene Programming Language + * + * Copyright (c) 2019-2023 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 "serene/commands.h" +#include "serene/config.h" + +#include +#include + +#include +#include + +namespace cl = llvm::cl; + +static std::string banner = + llvm::formatv("\n\nSerene Compiler Version {0}" + "\nCopyright (C) 2019-2023 " + "Sameer Rahmani \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); + +namespace serene::opts { +// Global options =========================================================== +static cl::opt verbose("v", cl::desc("Use verbose output"), + cl::cat(cl::getGeneralCategory()), + cl::sub(cl::SubCommand::getAll())); + +// Subcommands ============================================================== +// We don't use this subcommand directly but we need it for the CLI interface +static cl::SubCommand CC("cc", "Serene's C compiler interface"); + +static cl::SubCommand Run("run", "Run a Serene file"); +} // namespace serene::opts + +int main(int argc, char **argv) { + + // We don't use llvm::cl here cause we want to let + // the clang take care of the argument parsing. + if ((argc >= 2) && (strcmp(argv[1], "cc") == 0)) { + return serene::commands::cc(--argc, ++argv); + } + + // We start using llvm::cl from here onward which + // enforces our rules even for the subcommands. + cl::ParseCommandLineOptions(argc, argv, banner); + + if (serene::opts::Run) { + serene::commands::run(); + } + + return 0; +}