From c44128315d6d9d1b2fffe082f5356bb8db762086 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 4 Nov 2019 18:25:54 -0800 Subject: [PATCH] Implement function and type creation, add text to blog in compiler series --- 08/CMakeLists.txt | 1 + 08/llvm_context.cpp | 90 ++++++++++++++++++++++++++++++++++++++++----- 08/llvm_context.hpp | 1 + 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/08/CMakeLists.txt b/08/CMakeLists.txt index bce62c8..f31664b 100644 --- a/08/CMakeLists.txt +++ b/08/CMakeLists.txt @@ -22,6 +22,7 @@ llvm_map_components_to_libnames(LLVM_LIBS core x86asmparser x86codegen) # Create compiler executable add_executable(compiler ast.cpp ast.hpp definition.cpp + llvm_context.cpp llvm_context.hpp type_env.cpp type_env.hpp env.cpp env.hpp type.cpp type.hpp diff --git a/08/llvm_context.cpp b/08/llvm_context.cpp index a019db5..582b45c 100644 --- a/08/llvm_context.cpp +++ b/08/llvm_context.cpp @@ -1,18 +1,88 @@ #include "llvm_context.hpp" #include +using namespace llvm; + 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")); + stack_type = StructType::create(ctx, "stack"); + stack_ptr_type = PointerType::getUnqual(stack_type); + tag_type = IntegerType::getInt8Ty(ctx); + struct_types["node_base"] = StructType::create(ctx, "node_base"); + struct_types["node_app"] = StructType::create(ctx, "node_app"); + struct_types["node_num"] = StructType::create(ctx, "node_num"); + struct_types["node_global"] = StructType::create(ctx, "node_global"); + struct_types["node_ind"] = StructType::create(ctx, "node_ind"); + struct_types["node_data"] = StructType::create(ctx, "node_data"); + node_ptr_type = PointerType::getUnqual(struct_types.at("node_base")); } void llvm_state::create_functions() { - + auto void_type = Type::getVoidTy(ctx); + auto sizet_type = IntegerType::getInt64Ty(ctx); + functions["stack_init"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_init", + &module + ); + functions["stack_free"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_free", + &module + ); + functions["stack_push"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type, node_ptr_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_pop"] = Function::Create( + FunctionType::get(node_ptr_type, { stack_ptr_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_peek"] = Function::Create( + FunctionType::get(node_ptr_type, { stack_ptr_type, sizet_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_popn"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type, sizet_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_slide"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type, sizet_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_update"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type, sizet_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_alloc"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type, sizet_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_pack"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type, sizet_type, tag_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); + functions["stack_split"] = Function::Create( + FunctionType::get(node_ptr_type, { stack_ptr_type, sizet_type }, false), + Function::LinkageTypes::ExternalLinkage, + "stack_push", + &module + ); } diff --git a/08/llvm_context.hpp b/08/llvm_context.hpp index 3e6ab90..5863a85 100644 --- a/08/llvm_context.hpp +++ b/08/llvm_context.hpp @@ -15,6 +15,7 @@ struct llvm_state { std::map struct_types; llvm::StructType* stack_type; + llvm::PointerType* stack_ptr_type; llvm::PointerType* node_ptr_type; llvm::IntegerType* tag_type;