From 2f91c612969c2533dba3eec9818f8a63c7af2fa4 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Wed, 4 Jan 2023 19:24:51 +0100 Subject: [PATCH] Port to LLVM 14. --- CMakeLists.txt | 2 +- README.md | 6 +++--- src/CMakeLists.txt | 4 ++-- src/asmgen.cpp | 7 ++++--- src/codegen.cpp | 20 +++++++++++--------- src/main.cpp | 2 ++ 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9efdf0..fa64936 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ # Use of this source code is governed by a GPLv3 license, see LICENSE.txt. cmake_minimum_required(VERSION 3.7) -project(Monicelli VERSION 2.0.0 LANGUAGES CXX) +project(Monicelli VERSION 2.1.0 LANGUAGES C CXX) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") diff --git a/README.md b/README.md index bb7d2b7..8e066ca 100644 --- a/README.md +++ b/README.md @@ -49,11 +49,11 @@ you will need to have installed. If this is not the case, the configuration script will warn you. Monicelli is developed with version 6.8, but any sufficiently recent release should do just fine. -You will also need to have LLVM development libraries installed, version 7, 8 or 9. -Other versions might or might not work. CMake looks for version 7 by default, you +You will also need to have LLVM development libraries installed, version 14. +Newer versions might or might not work. CMake looks for version 14 by default, you can override this by setting the `MONICELLI_LLVM_VERSION` variable: - $ cmake -DMONICELLI_LLVM_VERSION=9 + $ cmake -DMONICELLI_LLVM_VERSION=15 Finally, you will need CMake, version 3.7 or higher. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index efe4b6b..1061889 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,7 @@ # Use of this source code is governed by a GPLv3 license, see LICENSE.txt. set(MONICELLI_ARCH "x86" CACHE STRING "Target architecture for Monicelli.") -set(MONICELLI_LLVM_VERSION 7 CACHE STRING "LLVM version for Monicelli.") +set(MONICELLI_LLVM_VERSION 14 CACHE STRING "LLVM version for Monicelli.") set(MONICELLI_LINKER ON CACHE BOOL "Enable the Monicelli linker. Requires POSIX.") find_package(LLVM "${MONICELLI_LLVM_VERSION}" REQUIRED CONFIG) @@ -36,7 +36,7 @@ add_executable(mcc set_target_properties(mcc lexer PROPERTIES - CXX_STANDARD 11 + CXX_STANDARD 14 CXX_STANDARD_REQUIRED true ) diff --git a/src/asmgen.cpp b/src/asmgen.cpp index 638381b..b274ad5 100644 --- a/src/asmgen.cpp +++ b/src/asmgen.cpp @@ -6,8 +6,9 @@ #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" +#include "llvm/MC/TargetRegistry.h" +#include "llvm/Support/CodeGen.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" @@ -47,7 +48,7 @@ llvm::TargetMachine* getTargetMachine(const std::string& triple, const std::stri void writeAssembly(const std::string& to_filename, llvm::Module* module, llvm::TargetMachine* target_machine) { std::error_code error_code; - llvm::raw_fd_ostream output{to_filename, error_code, llvm::sys::fs::F_None}; + llvm::raw_fd_ostream output{to_filename, error_code, llvm::sys::fs::OF_None}; if (error_code) { std::cerr << "Could not open '" << to_filename << "' for output: " << error_code.message() @@ -56,7 +57,7 @@ void writeAssembly(const std::string& to_filename, llvm::Module* module, } llvm::legacy::PassManager asm_generator; - auto file_type = llvm::TargetMachine::CGFT_ObjectFile; + auto file_type = llvm::CGFT_ObjectFile; if (target_machine->addPassesToEmitFile(asm_generator, output, nullptr, file_type)) { std::cerr << "Cannot emit an object file of this type\n"; diff --git a/src/codegen.cpp b/src/codegen.cpp index 915a21d..be882cf 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -16,12 +16,14 @@ #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" +#include "llvm/Pass.h" #include "llvm/Support/raw_os_ostream.h" #include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/GVN.h" #include "llvm/Transforms/Utils.h" +#include #include using namespace monicelli; @@ -170,19 +172,19 @@ llvm::Value* NestedScopes::lookup(const std::string& name) { void IRGenerator::declareBuiltins() { llvm::FunctionType* abort_type = llvm::FunctionType::get(builder_.getVoidTy(), false); - auto no_return = llvm::AttributeList().addAttribute(context_, 1, llvm::Attribute::NoReturn); + auto no_return = llvm::AttributeList().addFnAttribute(context_, llvm::Attribute::NoReturn); module_->getOrInsertFunction("abort", abort_type, no_return); llvm::FunctionType* printf_type = llvm::FunctionType::get(builder_.getInt32Ty(), {builder_.getInt8PtrTy()}, true); - auto no_alias = llvm::AttributeList().addAttribute(context_, 1, llvm::Attribute::NoAlias); + auto no_alias = llvm::AttributeList().addParamAttribute(context_, 0, llvm::Attribute::NoAlias); module_->getOrInsertFunction("printf", printf_type, no_alias); module_->getOrInsertFunction("scanf", printf_type, no_alias); } llvm::Value* IRGenerator::visitModule(const Module* m) { - module_ = llvm::make_unique("antani", context_); + module_ = std::make_unique("antani", context_); declareBuiltins(); @@ -245,7 +247,7 @@ llvm::Value* IRGenerator::visitFunction(const Function* ast_f) { for (auto& arg : f->args()) { auto arg_ptr = builder_.CreateAlloca(arg.getType(), nullptr, arg.getName()); builder_.CreateStore(&arg, arg_ptr); - var_scopes_.define(arg.getName(), arg_ptr); + var_scopes_.define(arg.getName().str(), arg_ptr); } exit_block_ = llvm::BasicBlock::Create(context_, "exit"); @@ -260,7 +262,7 @@ llvm::Value* IRGenerator::visitFunction(const Function* ast_f) { builder_.SetInsertPoint(exit_block_); if (return_var_) { - builder_.CreateRet(builder_.CreateLoad(return_var_)); + builder_.CreateRet(builder_.CreateLoad(f->getReturnType(), return_var_)); } else { builder_.CreateRetVoid(); } @@ -518,7 +520,7 @@ llvm::Value* IRGenerator::visitInputStatement(const InputStatement* s) { callIOBuiltin(target_type, target); if (reading_bool) { - builder_.CreateStore(evalTruthiness(builder_.CreateLoad(target)), var); + builder_.CreateStore(evalTruthiness(builder_.CreateLoad(target_type, target)), var); } return nullptr; @@ -638,12 +640,12 @@ llvm::Value* IRGenerator::visitAtomicExpression(const AtomicExpression* e) { case AtomicExpression::FLOAT: return llvm::ConstantFP::get(builder_.getDoubleTy(), e->getFloatValue()); case AtomicExpression::IDENTIFIER: { - auto var = var_scopes_.lookup(e->getIdentifierValue().getName()); + auto var = + llvm::cast_or_null(var_scopes_.lookup(e->getIdentifierValue().getName())); if (!var) { error(&e->getIdentifierValue(), "undefined variable", e->getIdentifierValue().getName()); } - assert(llvm::isa(var)); - return builder_.CreateLoad(var); + return builder_.CreateLoad(var->getAllocatedType(), var); } default: UNREACHABLE("Unhandled AtomicExpression type"); diff --git a/src/main.cpp b/src/main.cpp index 12ff167..c9577ca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,8 @@ #include "options.h" #include "parser.h" +#include "llvm/Support/Host.h" + #include #include #include