Skip to content

Commit b990ee8

Browse files
committed
Scaffold compiler
1 parent 35c3392 commit b990ee8

30 files changed

Lines changed: 1807 additions & 38 deletions

bin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_subdirectory(astdump)
55
add_subdirectory(fmt)
66
add_subdirectory(tidy)
77
add_subdirectory(lsp)
8+
add_subdirectory(compiler)
89

910
###
1011

bin/compiler/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(vanadiumc
2+
src/Vanadiumc.cpp
3+
)
4+
5+
target_include_directories(vanadiumc PRIVATE
6+
include
7+
)
8+
9+
target_link_libraries(vanadiumc PRIVATE
10+
vanadium_bin_boostrap
11+
vanadium_core
12+
vanadium_compiler
13+
argparse::argparse
14+
)

bin/compiler/src/Vanadiumc.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <fstream>
2+
3+
#include <argparse/argparse.hpp>
4+
5+
#include <vanadium/bin/Bootstrap.h>
6+
#include <vanadium/compiler/Compiler.h>
7+
#include <vanadium/core/Program.h>
8+
#include <vanadium/version.h>
9+
10+
namespace {
11+
int main(int argc, char* argv[]) {
12+
argparse::ArgumentParser ap("vanadiumc", vanadium::bin::kVersion);
13+
ap.add_description("TTCN-3 Compiler");
14+
//
15+
std::string filepath;
16+
ap.add_argument("path").store_into(filepath).help("file path");
17+
18+
//
19+
PARSE_CLI_ARGS_OR_EXIT(ap, argc, argv, 1);
20+
//
21+
22+
vanadium::core::Program program;
23+
program.Commit([&](auto& modify) {
24+
const auto read_file = [](const std::string& path, std::string& buf) {
25+
if (auto f = std::ifstream(path)) {
26+
std::stringstream ss;
27+
ss << f.rdbuf();
28+
buf = ss.str();
29+
} else {
30+
assert(false);
31+
}
32+
};
33+
modify.update(filepath, read_file);
34+
});
35+
36+
const auto* sf = program.GetFile(filepath);
37+
assert(sf != nullptr);
38+
assert(sf->ast.errors.empty());
39+
assert(sf->semantic_errors.empty());
40+
assert(sf->type_errors.empty());
41+
assert(sf->module.has_value());
42+
43+
vanadium::compiler::CompileIR(*sf);
44+
45+
return 0;
46+
}
47+
} // namespace
48+
49+
DEFINE_VANADIUM_ENTRYPOINT(main);

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ add_subdirectory(tooling)
77
add_subdirectory(format)
88
add_subdirectory(lint)
99
add_subdirectory(ls)
10+
11+
add_subdirectory(compiler)

src/ast/src/Parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,10 +1807,11 @@ nodes::SelectStmt* Parser::ParseSelect() {
18071807
}
18081808

18091809
nodes::CaseClause* Parser::ParseCaseClause() {
1810+
// TODO: multiple "case else" should be a syntax error
18101811
return NewNode<nodes::CaseClause>([&](auto& c) {
18111812
Expect(TokenKind::CASE);
18121813
if (tok_ == TokenKind::ELSE) {
1813-
Consume();
1814+
Consume(); // TODO: add a special mark?
18141815
} else {
18151816
Expect(TokenKind::LPAREN);
18161817
c.cond = ParseExprList();

src/compiler/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(backend)
2+
add_subdirectory(runtime)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
find_package(LLVM REQUIRED CONFIG)
2+
3+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
4+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
5+
6+
add_library(vanadium_compiler STATIC
7+
src/Compiler.cpp
8+
src/RuntimeBindings.cpp
9+
src/ModuleRegistrar.cpp
10+
)
11+
12+
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
13+
target_compile_definitions(vanadium_compiler PRIVATE
14+
${LLVM_DEFINITIONS_LIST}
15+
)
16+
17+
target_include_directories(vanadium_compiler PUBLIC
18+
interface
19+
)
20+
target_include_directories(vanadium_compiler PRIVATE
21+
include
22+
${LLVM_INCLUDE_DIRS}
23+
)
24+
25+
llvm_map_components_to_libnames(llvm_libs
26+
support
27+
core
28+
irreader
29+
TransformUtils
30+
)
31+
target_link_libraries(vanadium_compiler PRIVATE
32+
vanadium_lib_common
33+
vanadium_core
34+
35+
${llvm_libs}
36+
magic_enum
37+
)
38+
39+
add_gtest_executable(vanadium_compiler)
40+
41+
42+
target_compile_options(vanadium_compiler PRIVATE "-fsanitize=${name}")
43+
target_link_options(vanadium_compiler PRIVATE "-fsanitize=${name}")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <llvm/IR/IRBuilder.h>
4+
#include <llvm/IR/LLVMContext.h>
5+
#include <llvm/IR/Module.h>
6+
7+
namespace vanadium {
8+
9+
namespace core {
10+
struct SourceFile;
11+
}
12+
13+
namespace compiler {
14+
void GenerateModuleRegistrationCode(const core::SourceFile&, llvm::LLVMContext&, llvm::IRBuilder<>&, llvm::Module&);
15+
}
16+
17+
} // namespace vanadium
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <string_view>
5+
6+
#include <llvm-19/llvm/IR/Value.h>
7+
#include <llvm/IR/DerivedTypes.h>
8+
#include <llvm/IR/Function.h>
9+
#include <llvm/IR/LLVMContext.h>
10+
#include <llvm/IR/Module.h>
11+
12+
namespace vanadium::compiler {
13+
class RuntimeBindings {
14+
public:
15+
RuntimeBindings(llvm::LLVMContext&, llvm::Module&);
16+
//
17+
18+
llvm::Function* panic;
19+
20+
llvm::FunctionType* generic_getter_fn_ty;
21+
22+
llvm::StructType* typeinfo_ty;
23+
//
24+
llvm::FunctionType* type_ctor_fn_ty;
25+
llvm::FunctionType* type_dtor_fn_ty;
26+
27+
llvm::Function* type_alloc_f;
28+
llvm::Function* type_free_f;
29+
30+
llvm::StructType* generic_val_ty;
31+
32+
llvm::Function* log_f;
33+
34+
llvm::StructType* optional_ty;
35+
//
36+
llvm::Function* optional_get_f;
37+
llvm::Function* optional_set_f;
38+
llvm::Function* optional_ispresent_f;
39+
llvm::Function* optional_dtor_f;
40+
41+
llvm::StructType* int_ty;
42+
//
43+
llvm::Value* int_undef;
44+
[[nodiscard]] llvm::Value* GetInt(std::int64_t) const;
45+
//
46+
llvm::Function* int_eq_f;
47+
llvm::Function* int_ne_f;
48+
//
49+
llvm::Function* int_lt_f;
50+
llvm::Function* int_le_f;
51+
llvm::Function* int_gt_f;
52+
llvm::Function* int_ge_f;
53+
//
54+
llvm::Function* int_add_f;
55+
llvm::Function* int_sub_f;
56+
llvm::Function* int_mul_f;
57+
llvm::Function* int_div_f;
58+
59+
llvm::StructType* charstring_ty;
60+
//
61+
llvm::Value* charstring_undef;
62+
[[nodiscard]] llvm::Value* GetCharstring(std::string_view) const;
63+
64+
private:
65+
llvm::LLVMContext& ctx_;
66+
llvm::Module& mod_;
67+
};
68+
} // namespace vanadium::compiler
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <vanadium/core/Program.h>
4+
5+
namespace vanadium::compiler {
6+
7+
void CompileIR(const core::SourceFile&);
8+
9+
}

0 commit comments

Comments
 (0)