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
42 changes: 42 additions & 0 deletions config/llvm_header.inc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,48 @@ entry:
ret i256 %len_256
}

define ptr @hook_BYTES_padRight256(ptr %b, i256 %length, i256 %v) {
entry:
%hdr = load i64, ptr %b
%len = and i64 %hdr, 1099511627775 ; @LENGTH_MASK@
%len_256 = zext i64 %len to i256
%length_gt_len = icmp ugt i256 %length, %len_256
br i1 %length_gt_len, label %pad, label %return_b

return_b:
ret ptr %b

pad:
%v_gt_255 = icmp ugt i256 %v, 255
br i1 %v_gt_255, label %error, label %allocate

error:
%v_trunc = trunc i256 %v to i64
call void @interger_overflow(i64 %v_trunc)
unreachable

allocate:
%bytes_len = mul i256 %length, 8
%alloc_size = add i256 %bytes_len, 40 ; 8 bytes for header, rest for data
%alloc_size_i64 = trunc i256 %alloc_size to i64
%alloc = call ptr @kore_alloc(i64 %alloc_size_i64)

%length_i64 = trunc i256 %length to i64
store i64 %length_i64, ptr %alloc

%data_ptr = getelementptr i8, ptr %alloc, i64 8
%b_data_ptr = getelementptr i8, ptr %b, i64 8
call void @memcpy(ptr %data_ptr, ptr %b_data_ptr, i64 %len)

%remaining_bytes = sub i64 %length_i64, %len
%fill_ptr = getelementptr i8, ptr %data_ptr, i64 %len
%v_i8 = trunc i256 %v to i8
call void @llvm.memset.p0.i64(ptr %fill_ptr, i8 %v_i8, i64 %remaining_bytes, i1 false)

%result = bitcast ptr %alloc to ptr
ret ptr %result
}


define ptr @hook_MINT_MInt2Bytes(i256 %mint) {
entry:
Expand Down
41 changes: 41 additions & 0 deletions lib/codegen/CreateTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,47 @@ llvm::Value *create_term::create_hardcoded_hook(
current_block_ = merge_block;
return phi;
}
if (name == "BYTES.padRightMInt") {
assert(pattern->get_arguments().size() == 3);
llvm::Value *bytes = alloc_arg(pattern, 0, location_stack);
args.push_back(bytes);
llvm::Value *length = alloc_arg(pattern, 1, location_stack);
args.push_back(length);
llvm::Value *value = alloc_arg(pattern, 2, location_stack);
args.push_back(value);
auto *length_type = llvm::dyn_cast<llvm::IntegerType>(length->getType());
auto *value_type = llvm::dyn_cast<llvm::IntegerType>(value->getType());
if (!length_type || !value_type) {
throw std::invalid_argument("BYTES.padRightMInt: length and/or value "
"argument is not a machine integer type");
}
unsigned value_bits = value_type->getBitWidth();
llvm::CallInst *result = nullptr;
switch (value_bits) {
case 64: {
result = llvm::CallInst::Create(
get_or_insert_function(
module_, "hook_BYTES_padRight64", ptr_ty, bytes->getType(),
length_type, value_type),
{bytes, length, value}, "hook_BYTES_padRight64", current_block_);
break;
}
case 256: {
result = llvm::CallInst::Create(
get_or_insert_function(
module_, "hook_BYTES_padRight256", ptr_ty, bytes->getType(),
length_type, value_type),
{bytes, length, value}, "hook_BYTES_padRight256", current_block_);
break;
}
default: {
throw std::invalid_argument(
fmt::format("BYTES.padRightMInt: unsupported size {}", value_bits));
}
}
set_debug_loc(result);
return result;
}
if (name == "BYTES.lengthMInt") {
llvm::Value *bytes = alloc_arg(pattern, 0, location_stack);
args.push_back(bytes);
Expand Down
15 changes: 15 additions & 0 deletions runtime/strings/bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ SortInt hook_BYTES_length(SortBytes a) {
return move_int(result);
}

SortBytes hook_BYTES_padRight64(SortBytes b, uint64_t length, uint64_t v) {
if (length <= len(b)) {
return b;
}
if (v > 255) {
KLLVM_HOOK_INVALID_ARGUMENT("Integer overflow on value: {}", v);
}
auto *result
= static_cast<string *>(kore_alloc_token(sizeof(string) + length));
init_with_len(result, length);
memcpy(result->data, b->data, len(b));
memset(result->data + len(b), v, length - len(b));
return result;
}

SortBytes hook_BYTES_padRight(SortBytes b, SortInt length, SortInt v) {
unsigned long ulen = GET_UI(length);
if (ulen <= len(b)) {
Expand Down
22 changes: 22 additions & 0 deletions test/defn/k-files/mints-hook/padrightbytes.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module PADRIGHTBYTES

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 ::= "testPadRightBytes" [function]
rule testPadRightBytes =>
padRightBytes(bytesString, 50, 15) ==K padRightBytes(bytesString, 50p256, 15p256) andBool
padRightBytes(bytesString, 50, 15) ==K padRightBytes(bytesString, 50p64, 15p64)

endmodule
Loading