Skip to content

Commit

Permalink
feat: basic ir generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlekcahdp4 committed Dec 12, 2024
1 parent f872ccf commit 1c44ab5
Show file tree
Hide file tree
Showing 12 changed files with 624 additions and 27 deletions.
55 changes: 48 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
cmake_minimum_required(VERSION 3.14)
project(paracl)

include(cmake/functions.cmake)

find_package(LLVM REQUIRED)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")

function(add_llvm_based_lib target_name)
set(options_args SHARED)
set(multi_value_args LLVM_COMPONENTS SOURCES)
cmake_parse_arguments(ARGUMENTS "${options_args}" "" "${multi_value_args}"
${ARGN})
if(${ARGUMENTS_SHARED})
add_library(${target_name} SHARED ${ARGUMENTS_SOURCES})
else()
add_library(${target_name} STATIC ${ARGUMENTS_SOURCES})
endif()
enable_warnings(${target_name})
llvm_config(${target_name} ${ARGUMENTS_LLVM_COMPONENTS})
# warnings for this project's targets.
target_include_directories(${target_name} SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_compile_features(${target_name} PUBLIC cxx_std_23)
endfunction()

function(add_llvm_based_tool target_name)
set(options_args SHARED)
set(multi_value_args LLVM_COMPONENTS SOURCES)
cmake_parse_arguments(ARGUMENTS "${options_args}" "" "${multi_value_args}"
${ARGN})
add_executable(${target_name} ${ARGUMENTS_SOURCES})
enable_warnings(${target_name})
llvm_config(${target_name} ${ARGUMENTS_LLVM_COMPONENTS})
# warnings for this project's targets.
target_include_directories(${target_name} SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_compile_features(${target_name} PUBLIC cxx_std_23)
endfunction()

option(INSOURCEBUILD OFF)

if((${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR}) AND NOT
Expand Down Expand Up @@ -57,13 +92,11 @@ FetchContent_Declare(
GIT_REPOSITORY "https://github.com/ajlekcahdp4/graphs.git"
GIT_TAG origin/main)

FetchContent_Declare(
ezvis
GIT_REPOSITORY "https://github.com/serjzimmerman/ezvis.git"
GIT_TAG origin/main)

FetchContent_MakeAvailable(graphs)

set(PARACL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

# Tools
find_package(FLEX REQUIRED)
find_package(BISON 3.8 REQUIRED)
Expand All @@ -85,7 +118,7 @@ target_link_libraries(bytecode_vm PUBLIC fmt::fmt)
# target_enable_linter(pcldis)

add_executable(pclvm src/pclvm.cc)
target_link_libraries(pclvm Boost::program_options bytecode_vm)
target_link_libraries(pclvm PRIVATE Boost::program_options bytecode_vm)
target_enable_linter(pclvm)

bison_target(
Expand All @@ -111,9 +144,17 @@ target_include_directories(paracl_compiler PUBLIC include
target_link_libraries(paracl_compiler PUBLIC ezvis graphs fmt::fmt)
target_enable_linter(paracl_compiler)

add_executable(pclc src/pclc.cc)
target_link_libraries(pclc PRIVATE ezvis Boost::program_options paracl_compiler bytecode_vm)
add_llvm_based_tool(pclc
LLVM_COMPONENTS
executionengine
interpreter
SOURCES
src/pclc.cc
)
target_enable_linter(pclc)
target_link_libraries(pclc PRIVATE ezvis Boost::program_options paracl_compiler bytecode_vm paracl-llvm)

add_subdirectory(src/llvm_codegen)

set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/scripts)
find_program(BASH_PROGRAM bash)
Expand Down
69 changes: 69 additions & 0 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function(suppress_warnings TARGET_NAME)
get_target_property(target_options ${TARGET_NAME} COMPILE_OPTIONS)

if(target_options MATCHES "target_options-NOTFOUND")
set(target_options "")
endif()

if(NOT MSVC)
# [TODO]: Come up with a better way
else()
list(APPEND target_options -w)
endif()

set_property(TARGET ${TARGET_NAME} PROPERTY COMPILE_OPTIONS ${target_options})
endfunction()

function(enable_warnings TARGET_NAME)
if(MSVC)
target_compile_options(${TARGET_NAME} /W4)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(
${TARGET_NAME}
PRIVATE -Wall
-Wextra
-Wshadow
-Wcast-align
-Wunused
-Wpedantic
-Wmisleading-indentation
-Wnull-dereference
-Wformat=2
-Wimplicit-fallthrough
-Wno-missing-field-initializers
-Wno-nullability-extension)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
target_compile_options(
${TARGET_NAME}
PRIVATE -Wall
-Wextra
-pedantic
-Wshadow
-Wcast-align
-Wunused
-Wmisleading-indentation
-Wduplicated-cond
-Wduplicated-branches
-Wlogical-op
-Wnull-dereference
-Wformat=2
-Wimplicit-fallthrough
-Wno-missing-field-initializers)
endif()

endfunction(enable_warnings)

option(LINT OFF)
if(${LINT})
find_program(CLANG_TIDY_EXECUTABLE "clang-tidy")
set(CLANG_TIDY_COMMAND "${CLANG_TIDY_EXECUTABLE}")

function(target_enable_linter TARGET_NAME)
set_target_properties(${TARGET_NAME} PROPERTIES CXX_CLANG_TIDY
"${CLANG_TIDY_COMMAND}")
endfunction()
else()
function(target_enable_linter TARGET_NAME)
# Do nothing
endfunction()
endif()
5 changes: 4 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ stdenv.mkDerivation {
flex
fmt
];
buildInputs = with pkgs; [ boost ];
buildInputs = with pkgs; [
boost
llvm_18
];
}
14 changes: 4 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,23 @@
"x86_64-linux"
"aarch64-linux"
];
flake.overlays.default = _final: prev: {
llvmPackages_19.libllvm = prev.llvmPackages_19.libllvm.overrideAttrs {
patches = prev.patches ++ [ ./overlays/llvm-install-target-headers.patch ];
};
};
perSystem =
{ pkgs, ... }:
let
llvmPkgs = pkgs.llvmPackages_19;
in
rec {
imports = [ ./nix/treefmt.nix ];
packages = rec {
paracl = pkgs.callPackage ./. { inherit (llvmPkgs) stdenv; };
paracl = pkgs.callPackage ./. { stdenv = pkgs.gcc14Stdenv; };
};
devShells.default = pkgs.mkShell {
devShells.default = (pkgs.mkShell.override { stdenv = pkgs.gcc14Stdenv; }) {
nativeBuildInputs =
packages.paracl.nativeBuildInputs
++ (with pkgs; [
clang-tools
filecheck
act
gdb
lldb
libffi
valgrind
just
]);
Expand Down
4 changes: 3 additions & 1 deletion include/frontend/ast/ast_nodes/function_call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class function_call : public i_expression, private std::vector<i_expression *> {
auto end() const { return vector::end(); }

void append_parameter(i_expression *ptr) { vector::push_back(ptr); }

auto *get_callee() const { return m_def; }
};

} // namespace paracl::frontend::ast
} // namespace paracl::frontend::ast
3 changes: 2 additions & 1 deletion include/frontend/frontend_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class frontend_driver {
m_parsing_driver{std::make_unique<parser_driver>(m_source.filename())} {
m_parsing_driver->switch_input_stream(m_iss.get());
}
std::string_view get_filename() const { return m_source.get_filename(); }

const ast::ast_container &ast() const & { return m_parsing_driver->ast(); }
const functions_analytics &functions() const & { return m_functions; }
Expand Down Expand Up @@ -121,4 +122,4 @@ class frontend_driver {
}
};

} // namespace paracl::frontend
} // namespace paracl::frontend
3 changes: 2 additions & 1 deletion include/frontend/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class source_input {
// Can't make this const qualified, because bison location requires it be a modifiable pointer for whatever reason.
// Note that the string is allocated on the heap to avoid issues with default constructors.
std::string *filename() & { return m_filename.get(); }
std::string_view get_filename() const { return *m_filename.get(); }
std::istringstream iss() const & { return std::istringstream{m_file_source}; }
};

} // namespace paracl::frontend
} // namespace paracl::frontend
6 changes: 5 additions & 1 deletion include/frontend/types/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class type_builtin final : public i_type {
static_cast<const type_builtin &>(rhs).m_builtin_type_tag == m_builtin_type_tag;
}

auto get_builtin_type_class() const { return m_builtin_type_tag; }

std::string to_string() const override { return builtin_type_to_string(m_builtin_type_tag); }
unique_type clone() const override { return std::make_unique<type_builtin>(*this); }
};
Expand Down Expand Up @@ -195,12 +197,14 @@ class type_composite_function : public i_type, private std::vector<generic_type>
generic_type &return_type() & { return m_return_type; }
const generic_type &return_type() const & { return m_return_type; }

using vector::begin;
using vector::cbegin;
using vector::cend;
using vector::crbegin;
using vector::crend;
using vector::empty;
using vector::end;
using vector::size;
};

} // namespace paracl::frontend::types
} // namespace paracl::frontend::types
29 changes: 29 additions & 0 deletions include/llvm_codegen/codegen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long
* as you retain this notice you can do whatever you want with this stuff. If we
* meet some day, and you think this stuff is worth it, you can buy us a beer in
* return.
* ----------------------------------------------------------------------------
*/

#pragma once

#include "frontend/frontend_driver.hpp"

#include <llvm/IR/Module.h>

#include <memory>

namespace paracl::llvm_codegen {

namespace intrinsics {
void print(int32_t val);

int32_t read();
}

auto emit_llvm(const frontend::frontend_driver &drv, llvm::LLVMContext &ctx) -> std::unique_ptr<llvm::Module>;

}
13 changes: 13 additions & 0 deletions src/llvm_codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add_llvm_based_lib(paracl-llvm
SHARED
LLVM_COMPONENTS
target
core
executionengine
interpreter
support
SOURCES
codegen.cpp
)
target_include_directories(paracl-llvm PUBLIC ${PARACL_INCLUDE_DIR})
target_link_libraries(paracl-llvm PUBLIC paracl_compiler)
Loading

0 comments on commit 1c44ab5

Please sign in to comment.