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 e582e448..bca7c12b 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 cfbd0d4c..aaf350ab 100644 --- a/compiler+runtime/src/cpp/jank/c_api.cpp +++ b/compiler+runtime/src/cpp/jank/c_api.cpp @@ -358,6 +358,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 9cf69805..62899c34 100644 --- a/compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp +++ b/compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp @@ -358,7 +358,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); } @@ -967,6 +968,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));