-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
587 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
cmake_minimum_required(VERSION 3.22) | ||
|
||
if(NOT DEFINED VERSION_MAJOR) | ||
set(VERSION_MAJOR 1) | ||
endif() | ||
if(NOT DEFINED VERSION_MINOR) | ||
set(VERSION_MINOR 0) | ||
endif() | ||
if(NOT DEFINED VERSION_SUFFIX) | ||
find_package(Git QUIET) | ||
if(GIT_FOUND) | ||
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short=7 HEAD | ||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} | ||
RESULT_VARIABLE REVPARSE_RESULT | ||
OUTPUT_VARIABLE REVPARSE_OUTPUT | ||
ERROR_QUIET | ||
) | ||
if(REVPARSE_RESULT EQUAL 0) | ||
string(STRIP "${REVPARSE_OUTPUT}" REVPARSE_OUTPUT) | ||
set(VERSION_SUFFIX "-${REVPARSE_OUTPUT}") | ||
endif() | ||
else() | ||
set(VERSION_SUFFIX "") | ||
message(WARNING "Unable to detect version suffix from Git") | ||
endif() | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include "compiler/ast/syntax_tree.hpp" | ||
#include "compiler/frontend/lexer/token.hpp" | ||
#include "compiler/optree/program.hpp" | ||
#include "compiler/utils/source_files.hpp" | ||
|
||
#include "compiler/cli/options.hpp" | ||
|
||
namespace cli { | ||
|
||
class Compiler { | ||
const Options &opt; | ||
std::unordered_map<std::string_view, long long> measuredTimes; | ||
|
||
utils::SourceFile source; | ||
lexer::TokenList tokens; | ||
ast::SyntaxTree tree; | ||
optree::Program program; | ||
|
||
int readFiles(); | ||
int runPreprocessor(); | ||
int runLexer(); | ||
int runParser(); | ||
int runConverter(); | ||
|
||
int runAstSemantizer(); | ||
int runAstOptimizer(); | ||
#ifdef ENABLE_CODEGEN_AST_TO_LLVMIR | ||
int runAstLLVMIRGenerator(); | ||
#endif | ||
|
||
int runOptreeOptimizer(); | ||
#ifdef ENABLE_CODEGEN_OPTREE_TO_LLVMIR | ||
int runOptreeLLVMIRGenerator(); | ||
#endif | ||
|
||
public: | ||
Compiler() = delete; | ||
Compiler(const Options &options); | ||
Compiler(const Compiler &) = delete; | ||
Compiler(Compiler &&) = delete; | ||
~Compiler() = delete; | ||
~Compiler() = default; | ||
|
||
static int exec(int argc, char *argv[]); | ||
const auto &measurements() const { | ||
return measuredTimes; | ||
} | ||
|
||
int run(); | ||
}; | ||
|
||
} // namespace cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace cli { | ||
|
||
class TemporaryDirectory { | ||
std::filesystem::path dir; | ||
|
||
public: | ||
TemporaryDirectory(); | ||
TemporaryDirectory(const TemporaryDirectory &) = delete; | ||
TemporaryDirectory(TemporaryDirectory &&) = delete; | ||
~TemporaryDirectory(); | ||
|
||
const std::filesystem::path &path() const; | ||
}; | ||
|
||
std::string wrapQuotes(const std::string &str); | ||
|
||
std::string makeCommand(const std::vector<std::string> &args); | ||
|
||
} // namespace cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#pragma once | ||
|
||
#include <exception> | ||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
#if defined(ENABLE_CODEGEN_AST_TO_LLVMIR) || defined(ENABLE_CODEGEN_OPTREE_TO_LLVMIR) | ||
#define LLVMIR_CODEGEN_ENABLED | ||
#endif | ||
|
||
namespace cli { | ||
|
||
namespace arg { | ||
|
||
constexpr std::string_view help = "--help"; | ||
constexpr std::string_view debug = "--debug"; | ||
constexpr std::string_view optimize = "--optimize"; | ||
constexpr std::string_view time = "--time"; | ||
constexpr std::string_view stopAfter = "--stop-after"; | ||
constexpr std::string_view path = "--path"; | ||
constexpr std::string_view files = "FILES"; | ||
|
||
#ifdef LLVMIR_CODEGEN_ENABLED | ||
constexpr std::string_view compile = "--compile"; | ||
constexpr std::string_view clang = "--clang"; | ||
constexpr std::string_view llc = "--llc"; | ||
constexpr std::string_view output = "--output"; | ||
#endif | ||
|
||
} // namespace arg | ||
|
||
namespace stage { | ||
|
||
constexpr std::string_view preprocessor = "preprocessor"; | ||
constexpr std::string_view lexer = "lexer"; | ||
constexpr std::string_view parser = "parser"; | ||
constexpr std::string_view converter = "converter"; | ||
constexpr std::string_view semantizer = "semantizer"; | ||
constexpr std::string_view optimizer = "optimizer"; | ||
|
||
#ifdef LLVMIR_CODEGEN_ENABLED | ||
constexpr std::string_view codegen = "codegen"; | ||
#endif | ||
|
||
} // namespace stage | ||
|
||
namespace compilation_path { | ||
|
||
constexpr std::string_view ast = "ast"; | ||
constexpr std::string_view optree = "optree"; | ||
|
||
} // namespace compilation_path | ||
|
||
struct Options { | ||
bool help; | ||
bool debug; | ||
std::string path; | ||
bool time; | ||
std::optional<std::string> stopAfter; | ||
std::optional<std::string> optimize; | ||
#ifdef LLVMIR_CODEGEN_ENABLED | ||
bool compile; | ||
std::string clang; | ||
std::string llc; | ||
std::string output; | ||
#endif | ||
std::vector<std::string> files; | ||
std::string helpMessage; | ||
}; | ||
|
||
class OptionsError : public std::exception { | ||
std::string message; | ||
|
||
public: | ||
OptionsError(const std::string &message) : message(message){}; | ||
~OptionsError() = default; | ||
|
||
OptionsError &operator=(const OptionsError &) noexcept = default; | ||
|
||
const char *what() const noexcept override { | ||
return message.c_str(); | ||
} | ||
}; | ||
|
||
Options parseArguments(int argc, const char *const argv[]); | ||
|
||
} // namespace cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
namespace cli { | ||
|
||
constexpr const char *version = "@VERSION_MAJOR@.@VERSION_MINOR@@VERSION_SUFFIX@"; | ||
constexpr const char *versionMajor = "@VERSION_MAJOR@"; | ||
constexpr const char *versionMinor = "@VERSION_MINOR@"; | ||
constexpr const char *versionSuffix = "@VERSION_SUFFIX@"; | ||
|
||
} // namespace cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.