Skip to content

Commit

Permalink
Hook in global ctor to object file loader
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Nov 27, 2024
1 parent 3455a84 commit 3086cc6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ add_custom_command(
DEPENDS ${CMAKE_BINARY_DIR}/jank ${CMAKE_SOURCE_DIR}/src/jank/clojure/core.jank
OUTPUT ${jank_core_libraries_flag}
COMMAND ${CMAKE_BINARY_DIR}/jank compile clojure.core
COMMAND mkdir -p ${CMAKE_BINARY_DIR}/classes; touch ${jank_core_libraries_flag}
COMMAND touch ${jank_core_libraries_flag}
)
add_custom_target(
jank_core_libraries
Expand Down
11 changes: 10 additions & 1 deletion compiler+runtime/src/cpp/jank/codegen/llvm_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ namespace jank::codegen
auto const entry(llvm::BasicBlock::Create(*ctx->llvm_ctx, "entry", fn));
ctx->builder->SetInsertPoint(entry);

/* TODO: If we're compiling a module, and we're in the load fn, call the global ctor. */
/* JIT loaded object files don't support global ctors, so we need to call our manually.
* Fortunately, we have our load function which we can hook into. So, if we're compiling
* a module and we've just created the load function fo that module, the first thing
* we want to do is call our global ctor. */
if(target == compilation_target::module
&& root_fn.unique_name == module::module_to_load_function(ctx->module_name))
{
auto const global_ctor_fn(ctx->global_ctor_block->getParent());
ctx->builder->CreateCall(global_ctor_fn, {});
}

for(size_t i{}; i < arity.params.size(); ++i)
{
Expand Down
7 changes: 2 additions & 5 deletions compiler+runtime/src/cpp/jank/runtime/module/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,14 @@ namespace jank::runtime::module
profile::timer timer{ fmt::format("load object {}", module) };
if(entry.archive_path.is_some())
{
/* TODO: */
//visit_jar_entry(entry, [&](auto const &str) { rt_ctx.jit_prc.load_bitcode(module, str); });
/* TODO: Load object code from string. */
//visit_jar_entry(entry, [&](auto const &str) { rt_ctx.jit_prc.load_object(module, str); });
}
else
{
rt_ctx.jit_prc.load_object(entry.path);
}

auto const init{ rt_ctx.jit_prc.find_symbol<void (*)()>("jank_global_init_2393") };
init();

auto const load{ rt_ctx.jit_prc.find_symbol<object *(*)()>(module_to_load_function(module)) };
load();

Expand Down

0 comments on commit 3086cc6

Please sign in to comment.