From d4de9ec16cb5432d8a358d002c020d05749bd35f Mon Sep 17 00:00:00 2001 From: Jianling Zhong Date: Fri, 6 Dec 2024 15:38:48 -0800 Subject: [PATCH] Support ratio IR generation --- compiler+runtime/bin/format | 6 +-- compiler+runtime/include/cpp/jank/c_api.h | 1 + .../cpp/jank/codegen/llvm_processor.hpp | 1 + compiler+runtime/src/cpp/jank/c_api.cpp | 6 +++ .../src/cpp/jank/codegen/llvm_processor.cpp | 42 ++++++++++++++++++- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/compiler+runtime/bin/format b/compiler+runtime/bin/format index fef24ab2..13886269 100755 --- a/compiler+runtime/bin/format +++ b/compiler+runtime/bin/format @@ -4,7 +4,7 @@ set -euo pipefail git_root=$(git rev-parse --show-toplevel) -for i in $(git status | grep -E "modified:" | sed 's/modified:\s*//'); do - "$git_root"/compiler+runtime/build/llvm-install/usr/local/bin/clang-format -i "$i" - echo "formatted" "$i" +for i in $(git status | grep -E "modified:.*[hc]pp" | sed 's/modified:\s*//'); do + "${git_root}"/compiler+runtime/build/llvm-install/usr/local/bin/clang-format -i "${i}" + echo "formatted" "${i}" done diff --git a/compiler+runtime/include/cpp/jank/c_api.h b/compiler+runtime/include/cpp/jank/c_api.h index 5e068005..42f8f5f0 100644 --- a/compiler+runtime/include/cpp/jank/c_api.h +++ b/compiler+runtime/include/cpp/jank/c_api.h @@ -110,6 +110,7 @@ extern "C" jank_object_ptr jank_false(); jank_object_ptr jank_integer_create(jank_native_integer i); jank_object_ptr jank_real_create(jank_native_real r); + jank_object_ptr jank_ratio_create(jank_native_integer numerator, jank_native_integer denominator); jank_object_ptr jank_string_create(char const *s); jank_object_ptr jank_symbol_create(jank_object_ptr ns, jank_object_ptr name); jank_object_ptr jank_character_create(char const *s); diff --git a/compiler+runtime/include/cpp/jank/codegen/llvm_processor.hpp b/compiler+runtime/include/cpp/jank/codegen/llvm_processor.hpp index 6d015ee7..31af9eb4 100644 --- a/compiler+runtime/include/cpp/jank/codegen/llvm_processor.hpp +++ b/compiler+runtime/include/cpp/jank/codegen/llvm_processor.hpp @@ -123,6 +123,7 @@ namespace jank::codegen llvm::Value *gen_global(obj::boolean_ptr b) const; llvm::Value *gen_global(obj::integer_ptr i) const; llvm::Value *gen_global(obj::real_ptr r) const; + llvm::Value *gen_global(obj::ratio_ptr r) const; llvm::Value *gen_global(obj::persistent_string_ptr s) const; llvm::Value *gen_global(obj::symbol_ptr s); llvm::Value *gen_global(obj::keyword_ptr k) const; diff --git a/compiler+runtime/src/cpp/jank/c_api.cpp b/compiler+runtime/src/cpp/jank/c_api.cpp index 2a21fd5a..7650d7fd 100644 --- a/compiler+runtime/src/cpp/jank/c_api.cpp +++ b/compiler+runtime/src/cpp/jank/c_api.cpp @@ -359,6 +359,12 @@ extern "C" return erase(make_box(r)); } + jank_object_ptr + jank_ratio_create(jank_native_integer const numerator, jank_native_integer const denominator) + { + return erase(make_box(runtime::obj::ratio_data(numerator, denominator))); + } + jank_object_ptr jank_string_create(char const *s) { assert(s); diff --git a/compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp b/compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp index e4453f48..96f88628 100644 --- a/compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp +++ b/compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp @@ -361,7 +361,8 @@ namespace jank::codegen || std::same_as || std::same_as || std::same_as || std::same_as - || std::same_as) + || std::same_as + || std::same_as) { return gen_global(typed_o); } @@ -1039,6 +1040,45 @@ namespace jank::codegen return ctx->builder->CreateLoad(ctx->builder->getPtrTy(), global); } + llvm::Value *llvm_processor::gen_global(obj::ratio_ptr const r) const + { + if(auto const found(ctx->literal_globals.find(r)); found != ctx->literal_globals.end()) + { + return ctx->builder->CreateLoad(ctx->builder->getPtrTy(), found->second); + } + + auto &global(ctx->literal_globals[r]); + auto const name(fmt::format("ratio_{}", r->to_hash())); + auto const var(create_global_var(name)); + ctx->module->insertGlobalVariable(var); + global = var; + + auto const prev_block(ctx->builder->GetInsertBlock()); + { + llvm::IRBuilder<>::InsertPointGuard const guard{ *ctx->builder }; + ctx->builder->SetInsertPoint(ctx->global_ctor_block); + + auto const create_fn_type( + llvm::FunctionType::get(ctx->builder->getPtrTy(), + { ctx->builder->getInt64Ty(), ctx->builder->getInt64Ty() }, + false)); + auto const create_fn(ctx->module->getOrInsertFunction("jank_ratio_create", create_fn_type)); + auto const num_arg( + llvm::ConstantInt::getSigned(ctx->builder->getInt64Ty(), r->data.numerator)); + auto const denom_arg( + llvm::ConstantInt::getSigned(ctx->builder->getInt64Ty(), r->data.denominator)); + auto const call(ctx->builder->CreateCall(create_fn, { num_arg, denom_arg })); + ctx->builder->CreateStore(call, global); + + if(prev_block == ctx->global_ctor_block) + { + return call; + } + } + + return ctx->builder->CreateLoad(ctx->builder->getPtrTy(), global); + } + llvm::Value *llvm_processor::gen_global(obj::persistent_string_ptr const s) const { auto const found(ctx->literal_globals.find(s));