Skip to content

Commit

Permalink
Support ratio IR generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jianlingzhong committed Dec 9, 2024
1 parent 1152bfa commit d4de9ec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
6 changes: 3 additions & 3 deletions compiler+runtime/bin/format
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions compiler+runtime/src/cpp/jank/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 41 additions & 1 deletion compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ namespace jank::codegen
|| std::same_as<T, runtime::obj::real> || std::same_as<T, runtime::obj::symbol>
|| std::same_as<T, runtime::obj::character>
|| std::same_as<T, runtime::obj::keyword>
|| std::same_as<T, runtime::obj::persistent_string>)
|| std::same_as<T, runtime::obj::persistent_string>
|| std::same_as<T, runtime::obj::ratio>)
{
return gen_global(typed_o);
}
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit d4de9ec

Please sign in to comment.