Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
df2492b
Implementing `hook_MINT_powMint` codegen with support to MInt{256} an…
Robertorosmaninho Jul 4, 2025
64d46c5
Implementing `hook_MINT_MInt2Bytes` codegen with support to MInt{256}
Robertorosmaninho Jul 4, 2025
813f91d
Implementing `hook_MINT_Bytes2MInt` codegen with support to MInt{256}
Robertorosmaninho Jul 4, 2025
8229b1a
Implementing `hook_BYTES_lengthMInt` codegen with support to MInt{256…
Robertorosmaninho Jul 4, 2025
7797ded
Implementing `hook_BYTES_padRightMInt` codegen with support to MInt{2…
Robertorosmaninho Jul 4, 2025
0e0b832
Implementing `hook_BYTES_padLeftMInt` codegen with support to MInt{25…
Robertorosmaninho Jul 4, 2025
788febe
Implementing `hook_BYTES_replaceAtMInt` codegen with support to MInt{…
Robertorosmaninho Jul 4, 2025
3e5b1e5
Implementing `hook_BYTES_substrMInt` codegen with support to MInt{256…
Robertorosmaninho Jul 4, 2025
6535b5c
Implementing `hook_BYTES_getMInt` codegen with support to MInt{256} a…
Robertorosmaninho Jul 4, 2025
5178cce
Implementing `hook_BYTES_updateMInt` codegen with support to MInt{256…
Robertorosmaninho Jul 4, 2025
3669211
Implementing `hook_LIST_sizeMInt` codegen with support to MInt{256} a…
Robertorosmaninho Jul 4, 2025
5d7dd50
Implementing `hook_LIST_getMInt` codegen with support to MInt{64}
Robertorosmaninho Jul 4, 2025
d7efaf7
Implementing `hook_LIST_updateMInt` in bytes.cpp for supporting MInt{…
Robertorosmaninho Jul 4, 2025
d8749dc
Fixing typo
Robertorosmaninho Jul 11, 2025
8bc0a9b
Using `hook_BYTES_length64` instead of hardcoded `LENGTH_MASK`
Robertorosmaninho Jul 14, 2025
60c6c99
Reusing error functions to emit messages on Int hooks as well
Robertorosmaninho Jul 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/llvm_header.inc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ define void @take_search_step(ptr %subject) {
ret void
}

define i256 @hook_BYTES_length256(ptr %b) {
entry:
%hdr = load i64, ptr %b
%len = and i64 %hdr, 1099511627775 ; @LENGTH_MASK@
%len_256 = zext i64 %len to i256
ret i256 %len_256
}


define ptr @hook_MINT_MInt2Bytes(i256 %mint) {
entry:
%alloc = call ptr @kore_alloc(i64 40) ; 8 bytes for header, 32 bytes for MInt
Expand Down
32 changes: 32 additions & 0 deletions lib/codegen/CreateTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,38 @@ llvm::Value *create_term::create_hardcoded_hook(
current_block_ = merge_block;
return phi;
}
if (name == "BYTES.lengthMInt") {
llvm::Value *bytes = alloc_arg(pattern, 0, location_stack);
args.push_back(bytes);
auto *expected_sort = dynamic_cast<kore_composite_sort *>(
pattern->get_constructor()->get_sort().get());
unsigned expected_bits = expected_sort->get_category(definition_).bits;
llvm::CallInst *result = nullptr;
switch (expected_bits) {
case 64: {
result = llvm::CallInst::Create(
get_or_insert_function(
module_, "hook_BYTES_length64", llvm::Type::getInt64Ty(ctx_),
bytes->getType()),
{bytes}, "hook_BYTES_length64", current_block_);
break;
}
case 256: {
result = llvm::CallInst::Create(
get_or_insert_function(
module_, "hook_BYTES_length256",
llvm::IntegerType::get(ctx_, 256), bytes->getType()),
{bytes}, "hook_BYTES_length256", current_block_);
break;
}
default: {
throw std::invalid_argument(
fmt::format("BYTES.lengthMInt: unsupported size {}", expected_bits));
}
}
set_debug_loc(result);
return result;
}
if (name == "MINT.uvalue") {
llvm::Value *mint = alloc_arg(pattern, 0, location_stack);
args.push_back(mint);
Expand Down
4 changes: 4 additions & 0 deletions runtime/strings/bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ hook_BYTES_memset(SortBytes b, SortInt start, SortInt count, SortInt value) {
return b;
}

uint64_t hook_BYTES_length64(SortBytes a) {
return len(a);
}

SortInt hook_BYTES_length(SortBytes a) {
mpz_t result;
mpz_init_set_ui(result, len(a));
Expand Down
21 changes: 21 additions & 0 deletions test/defn/k-files/mints-hook/lengthbytes.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module LENGTHBYTES

imports INT
imports MINT
imports BYTES
imports BOOL
imports K-EQUAL

syntax MInt{64}
syntax MInt{256}

syntax Bytes ::= "bytesString" [macro]
rule bytesString =>
b"\x1d)0\xdd\xcc#\xeb\x14)Q\x8bAG\xcf\xd46\xa7\xdb\x8f\xc6&\xc1=N\xb6\xa4\x81%\xc2\xd2\xf4o"

syntax Bool ::= "testLengthBytes" [function]
rule testLengthBytes =>
lengthBytes(bytesString):Int ==Int MInt2Unsigned(lengthBytes(bytesString):MInt{64}) andBool
lengthBytes(bytesString):Int ==Int MInt2Unsigned(lengthBytes(bytesString):MInt{256})

endmodule
3,075 changes: 3,075 additions & 0 deletions test/defn/mints-hook/lengthbytes.kore

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/input/lengthbytes.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LblinitGeneratedTopCell{}(Lbl'Unds'Map'Unds'{}(Lbl'Stop'Map{}(),Lbl'UndsPipe'-'-GT-Unds'{}(inj{SortKConfigVar{}, SortKItem{}}(\dv{SortKConfigVar{}}("$PGM")),inj{SortBool{}, SortKItem{}}(LbltestLengthBytes'Unds'LENGTHBYTES'Unds'Bool{}()))))
1 change: 1 addition & 0 deletions test/output/lengthbytes.out.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortBool{}, SortKItem{}}(\dv{SortBool{}}("true")),dotk{}())),Lbl'-LT-'generatedCounter'-GT-'{}(\dv{SortInt{}}("0")))