Skip to content

Commit dfcadbe

Browse files
authored
Fix some bugs relating to machine integer literals (#1172)
I discovered 2 bugs relating to machine integer literals that are fixed by this PR: 1. If a MInt literal appeared in the initial configuration, its value would be changed by the code that creates the initial configuration term. 2. If an MInt literal appeared in a rule, the rule would match against the incorrect value of integer.
1 parent 1d701f5 commit dfcadbe

File tree

7 files changed

+2483
-12
lines changed

7 files changed

+2483
-12
lines changed

lib/codegen/CreateStaticTerm.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,15 @@ create_static_term::create_token(value_type sort, std::string contents) {
306306
assert(false && "not implemented yet: tokens");
307307
case sort_category::MInt: {
308308
size_t idx = contents.find_first_of("pP");
309-
assert(idx != std::string::npos);
310-
uint64_t bits = std::stoi(contents.substr(idx + 1));
309+
uint64_t bits{};
310+
if (idx == std::string::npos) {
311+
bits = sort.bits;
312+
} else {
313+
bits = std::stoi(contents.substr(idx + 1));
314+
contents = contents.substr(0, idx);
315+
}
311316
return llvm::ConstantInt::get(
312-
llvm::IntegerType::get(ctx_, bits), contents.substr(0, idx), 10);
317+
llvm::IntegerType::get(ctx_, bits), contents, 10);
313318
}
314319
case sort_category::Bool:
315320
return llvm::ConstantInt::get(

lib/codegen/EmitConfigParser.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,18 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
491491
= llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 0);
492492
for (auto const &entry : sorts) {
493493
std::string name = entry.first;
494-
if (!entry.second->get_object_sort_variables().empty()) {
495-
// TODO: MINT in initial configuration
496-
continue;
497-
}
498494
auto sort = kore_composite_sort::create(name);
499-
value_type cat = sort->get_category(definition);
500-
if (cat.cat == sort_category::Symbol
501-
|| cat.cat == sort_category::Variable) {
502-
continue;
495+
value_type cat{};
496+
if (entry.second->attributes().contains(attribute_set::key::Hook)
497+
&& entry.second->attributes().get_string(attribute_set::key::Hook)
498+
== "MINT.MInt") {
499+
cat.cat = sort_category::MInt;
500+
} else {
501+
cat = sort->get_category(definition);
502+
if (cat.cat == sort_category::Symbol
503+
|| cat.cat == sort_category::Variable) {
504+
continue;
505+
}
503506
}
504507
current_block->insertInto(func);
505508
current_block->setName("is_" + name);
@@ -527,10 +530,22 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
527530
case sort_category::List:
528531
case sort_category::Set:
529532
case sort_category::StringBuffer:
530-
case sort_category::MInt:
531533
// TODO: tokens
532534
add_abort(case_block, module);
533535
break;
536+
case sort_category::MInt: {
537+
auto const &len = func->arg_begin() + 1;
538+
auto const &contents = func->arg_begin() + 2;
539+
auto *get_mint_token = get_or_insert_function(
540+
module, "get_mint_token",
541+
llvm::FunctionType::get(
542+
ptr_ty, {llvm::Type::getInt64Ty(ctx), ptr_ty}, false));
543+
auto *mint_token = llvm::CallInst::Create(
544+
get_mint_token, {len, contents}, "", case_block);
545+
phi->addIncoming(mint_token, case_block);
546+
llvm::BranchInst::Create(merge_block, case_block);
547+
break;
548+
}
534549
case sort_category::Bool: {
535550
auto *str = llvm::ConstantDataArray::getString(ctx, "true", false);
536551
auto *global = module->getOrInsertGlobal("bool_true", str->getType());

matching/src/main/scala/org/kframework/backend/llvm/matching/Generator.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ object Generator {
174174
case "false" => "0"
175175
case _ => str
176176
}
177+
} else if (hookAtt.contains("MINT.MInt")) {
178+
str.substring(0, str.indexOf('p'))
177179
} else str,
178180
SortCategory(hookAtt.orElse(Some("STRING.String")), sort, symlib)
179181
)

runtime/util/ConfigurationParser.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ static thread_local Cache cache;
2020

2121
extern "C" {
2222

23+
size_t *hook_MINT_export(mpz_t in, uint64_t bits);
24+
25+
void *get_mint_token(size_t size, char const *c_str) {
26+
std::string str = std::string(c_str, size);
27+
size_t idx = str.find('p');
28+
assert(idx != std::string::npos);
29+
std::string precision_str = str.substr(idx + 1);
30+
long long precision = std::stoll(precision_str);
31+
long long precision_in_bytes = (precision + 7) / 8;
32+
char *token = (char *)malloc(precision_in_bytes);
33+
std::string val_str = str.substr(0, idx);
34+
mpz_t z;
35+
mpz_init_set_str(z, val_str.c_str(), 10);
36+
size_t *mint = hook_MINT_export(z, precision);
37+
memcpy(token, mint, precision_in_bytes);
38+
return token;
39+
}
40+
2341
uint32_t get_tag_for_symbol_name_internal(char const *);
2442

2543
void init_float(floating *result, char const *c_str) {

0 commit comments

Comments
 (0)