From 6737f57a3d9e35cbc2e0610b29b6849e7068337b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 2 Nov 2019 17:53:15 -0700 Subject: [PATCH] Start work on chapter 8 code for compilers --- 08/CMakeLists.txt | 13 +++++++++++++ 08/llvm_context.cpp | 18 ++++++++++++++++++ 08/llvm_context.hpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 08/llvm_context.cpp create mode 100644 08/llvm_context.hpp diff --git a/08/CMakeLists.txt b/08/CMakeLists.txt index 5e5dab4..bce62c8 100644 --- a/08/CMakeLists.txt +++ b/08/CMakeLists.txt @@ -1,8 +1,12 @@ cmake_minimum_required(VERSION 3.1) project(compiler) +# Find all the required packages find_package(BISON) find_package(FLEX) +find_package(LLVM REQUIRED CONFIG) + +# Set up the flex and bison targets bison_target(parser ${CMAKE_CURRENT_SOURCE_DIR}/parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp @@ -12,6 +16,10 @@ flex_target(scanner ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp) add_flex_bison_dependency(scanner parser) +# Find all the relevant LLVM components +llvm_map_components_to_libnames(LLVM_LIBS core x86asmparser x86codegen) + +# Create compiler executable add_executable(compiler ast.cpp ast.hpp definition.cpp type_env.cpp type_env.hpp @@ -24,5 +32,10 @@ add_executable(compiler ${FLEX_scanner_OUTPUTS} main.cpp ) + +# Configure compiler executable target_include_directories(compiler PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(compiler PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(compiler PUBLIC ${LLVM_DEFINITIONS}) +target_compile_definitions(compiler PUBLIC ${LLVM_DEFINITIONS}) +target_link_libraries(compiler ${LLVM_LIBS}) diff --git a/08/llvm_context.cpp b/08/llvm_context.cpp new file mode 100644 index 0000000..a019db5 --- /dev/null +++ b/08/llvm_context.cpp @@ -0,0 +1,18 @@ +#include "llvm_context.hpp" +#include + +void llvm_state::create_types() { + stack_type = llvm::StructType::create(ctx, "stack"); + tag_type = llvm::IntegerType::getInt8Ty(ctx); + struct_types["node_base"] = llvm::StructType::create(ctx, "node_base"); + struct_types["node_app"] = llvm::StructType::create(ctx, "node_app"); + struct_types["node_num"] = llvm::StructType::create(ctx, "node_num"); + struct_types["node_global"] = llvm::StructType::create(ctx, "node_global"); + struct_types["node_ind"] = llvm::StructType::create(ctx, "node_ind"); + struct_types["node_data"] = llvm::StructType::create(ctx, "node_data"); + node_ptr_type = llvm::PointerType::getUnqual(struct_types.at("node_base")); +} + +void llvm_state::create_functions() { + +} diff --git a/08/llvm_context.hpp b/08/llvm_context.hpp new file mode 100644 index 0000000..3e6ab90 --- /dev/null +++ b/08/llvm_context.hpp @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +struct llvm_state { + llvm::LLVMContext ctx; + llvm::IRBuilder<> builder; + llvm::Module module; + + std::map functions; + std::map struct_types; + + llvm::StructType* stack_type; + llvm::PointerType* node_ptr_type; + llvm::IntegerType* tag_type; + + llvm_state() + : builder(ctx), module("bloglang", ctx) { + create_types(); + create_functions(); + } + + void create_types(); + void create_functions(); +};