Skip to content

Commit

Permalink
Introducing KREFLECTION hook module
Browse files Browse the repository at this point in the history
Introducing `hook_KREFLECTION_freeze` function
  • Loading branch information
Robertorosmaninho committed Sep 3, 2024
1 parent 688c966 commit ba69af5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/runtime/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ size_t hook_SET_size_long(set *);

mpz_ptr hook_MINT_import(size_t *i, uint64_t bits, bool is_signed);

block *hook_KREFLECTION_freeze(block *term);

block *debruijnize(block *);
block *increment_debruijn(block *);
block *alpha_rename(block *);
Expand Down
1 change: 1 addition & 0 deletions runtime/meta/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(meta STATIC
substitution.cpp
ffi.cpp
kreflection.cpp
)

target_link_libraries(meta
Expand Down
92 changes: 92 additions & 0 deletions runtime/meta/kreflection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "runtime/alloc.h"
#include "runtime/arena.h"
#include "runtime/header.h"

extern "C" {
block *copy_block_to_eternal_lifetime(block *term) {
if (is_leaf_block(term)) {
return term;
}

// Get the layout data for the block and allocate a new block with the same size as the original block
auto layout = get_layout(term);

// If the block is string term we can just return it as we do for leaf blocks
if (layout == 0) {
return term;
}

auto *layout_data = get_layout_data(layout);

auto *new_block = (block *)kore_alloc_forever(size_hdr(term->h.hdr));

// Copy the block header and set the it as not young object, so gc does not collect it
new_block->h = term->h;
new_block->h.hdr |= NOT_YOUNG_OBJECT_BIT;

for (unsigned i = 0; i < layout_data->nargs; i++) {

// Get the layout item for the argument and the argument itself
auto *arg_data = layout_data->args + i;
auto *arg = ((char *)term) + arg_data->offset;

// Copy the argument based on the category of the argument
switch (arg_data->cat) {
case MAP_LAYOUT: {
auto *new_arg = copy_block_to_eternal_lifetime(*(block **)arg);
*(block **)(((char *)new_block) + arg_data->offset) = new_arg;
break;
}
case RANGEMAP_LAYOUT: {
auto *new_arg = copy_block_to_eternal_lifetime(*(block **)arg);
*(block **)(((char *)new_block) + arg_data->offset) = new_arg;
break;
}
case LIST_LAYOUT: {
auto *new_arg = copy_block_to_eternal_lifetime(*(block **)arg);
*(block **)(((char *)new_block) + arg_data->offset) = new_arg;
break;
}
case SET_LAYOUT: {
auto *new_arg = copy_block_to_eternal_lifetime(*(block **)arg);
*(block **)(((char *)new_block) + arg_data->offset) = new_arg;
break;
}
case VARIABLE_LAYOUT: {
auto *new_arg = copy_block_to_eternal_lifetime(*(block **)arg);
*(block **)(((char *)new_block) + arg_data->offset) = new_arg;
break;
}
case SYMBOL_LAYOUT: {
auto *new_arg = copy_block_to_eternal_lifetime(*(block **)arg);
*(block **)(((char *)new_block) + arg_data->offset) = new_arg;
break;
}
case STRINGBUFFER_LAYOUT: {
*(void **)(((char *)new_block) + arg_data->offset) = *(void **)arg;
break;
}
case INT_LAYOUT: {
*(void **)(((char *)new_block) + arg_data->offset) = *(void **)arg;
break;
}
case FLOAT_LAYOUT: {
*(void **)(((char *)new_block) + arg_data->offset) = *(void **)arg;
break;
}
case BOOL_LAYOUT: {
*(void **)(((char *)new_block) + arg_data->offset) = *(void **)arg;
break;
}
default: // mint
*(void **)(((char *)new_block) + arg_data->offset) = *(void **)arg;
break;
}
}
return new_block;
}

block *hook_KREFLECTION_freeze(block *term) {
return copy_block_to_eternal_lifetime(term);
}
}

0 comments on commit ba69af5

Please sign in to comment.