Skip to content

Commit 3669211

Browse files
Implementing hook_LIST_sizeMInt codegen with support to MInt{256} and MInt{64}
Implementing `hook_LIST_size256` in LLVM IR Implementing `hook_LIST_size64` in bytes.cpp Adding `list-size` unit test
1 parent 5178cce commit 3669211

File tree

7 files changed

+3142
-0
lines changed

7 files changed

+3142
-0
lines changed

config/llvm_header.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ define void @take_search_step(ptr %subject) {
281281
ret void
282282
}
283283
284+
; MInt Functions
285+
286+
define i256 @hook_LIST_size256(ptr %l) {
287+
entry:
288+
%len_ptr = getelementptr %list, ptr %l, i64 0, i32 0
289+
%len = load i64, ptr %len_ptr
290+
%len_256 = zext i64 %len to i256
291+
ret i256 %len_256
292+
}
293+
284294
define i256 @hook_BYTES_get256(ptr %b, i256 %off) {
285295
entry:
286296
; Check for buffer overflow

lib/codegen/CreateTerm.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,38 @@ llvm::Value *create_term::create_hardcoded_hook(
446446
current_block_ = merge_block;
447447
return phi;
448448
}
449+
if (name == "LIST.sizeMInt") {
450+
llvm::Value *list = alloc_arg(pattern, 0, location_stack);
451+
args.push_back(list);
452+
auto *expected_sort = dynamic_cast<kore_composite_sort *>(
453+
pattern->get_constructor()->get_sort().get());
454+
unsigned expected_bits = expected_sort->get_category(definition_).bits;
455+
llvm::CallInst *result = nullptr;
456+
switch (expected_bits) {
457+
case 64: {
458+
result = llvm::CallInst::Create(
459+
get_or_insert_function(
460+
module_, "hook_LIST_size64", llvm::Type::getInt64Ty(ctx_),
461+
list->getType()),
462+
{list}, "hook_LIST_size64", current_block_);
463+
break;
464+
}
465+
case 256: {
466+
result = llvm::CallInst::Create(
467+
get_or_insert_function(
468+
module_, "hook_LIST_size256", llvm::IntegerType::get(ctx_, 256),
469+
list->getType()),
470+
{list}, "hook_LIST_size256", current_block_);
471+
break;
472+
}
473+
default: {
474+
throw std::invalid_argument(
475+
fmt::format("LIST.sizeMInt: unsupported size {}", expected_bits));
476+
}
477+
}
478+
set_debug_loc(result);
479+
return result;
480+
}
449481
if (name == "BYTES.updateMInt") {
450482
assert(pattern->get_arguments().size() == 3);
451483
llvm::Value *bytes = alloc_arg(pattern, 0, location_stack);

runtime/collections/lists.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ size_t hook_LIST_size_long(SortList list) {
111111
return list->size();
112112
}
113113

114+
uint64_t hook_LIST_size64(SortList list) {
115+
return static_cast<uint64_t>(list->size());
116+
}
117+
114118
SortInt hook_LIST_size(SortList list) {
115119
mpz_t size;
116120
mpz_init_set_ui(size, list->size());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module LIST-SIZE
2+
3+
imports INT
4+
imports MINT
5+
imports BOOL
6+
imports LIST
7+
8+
syntax MInt{64}
9+
syntax MInt{256}
10+
11+
syntax List ::= "listOfMInt" [macro]
12+
rule listOfMInt => ListItem(2p64) (ListItem(3p64) (ListItem(4p64) .List))
13+
14+
syntax Bool ::= "testListSize" [function]
15+
rule testListSize =>
16+
size(listOfMInt) ==Int MInt2Unsigned(size(listOfMInt)::MInt{64}) andBool
17+
size(listOfMInt) ==Int MInt2Unsigned(size(listOfMInt)::MInt{256})
18+
19+
endmodule

test/defn/mints-hook/list-size.kore

Lines changed: 3075 additions & 0 deletions
Large diffs are not rendered by default.

test/input/list-size.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LblinitGeneratedTopCell{}(Lbl'Unds'Map'Unds'{}(Lbl'Stop'Map{}(),Lbl'UndsPipe'-'-GT-Unds'{}(inj{SortKConfigVar{}, SortKItem{}}(\dv{SortKConfigVar{}}("$PGM")),inj{SortBool{}, SortKItem{}}(LbltestListSize'Unds'LIST-SIZE'Unds'Bool{}()))))

test/output/list-size.out.diff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortBool{}, SortKItem{}}(\dv{SortBool{}}("true")),dotk{}())),Lbl'-LT-'generatedCounter'-GT-'{}(\dv{SortInt{}}("0")))

0 commit comments

Comments
 (0)