diff --git a/k-distribution/include/kframework/builtin/domains.md b/k-distribution/include/kframework/builtin/domains.md index 43c8fd60ab1..851e0865dca 100644 --- a/k-distribution/include/kframework/builtin/domains.md +++ b/k-distribution/include/kframework/builtin/domains.md @@ -3015,6 +3015,7 @@ than the input. ```k syntax {Width1, Width2} MInt{Width1} ::= roundMInt(MInt{Width2}) [function, total, hook(MINT.round)] + syntax {Width1, Width2} MInt{Width1} ::= signExtendMInt(MInt{Width2}) [function, total, hook(MINT.sext)] ``` ```k diff --git a/k-distribution/tests/regression-new/mint-llvm-3/Makefile b/k-distribution/tests/regression-new/mint-llvm-3/Makefile new file mode 100644 index 00000000000..d48aaade4fd --- /dev/null +++ b/k-distribution/tests/regression-new/mint-llvm-3/Makefile @@ -0,0 +1,7 @@ +DEF=test +EXT=test +TESTDIR=. +KOMPILE_BACKEND=llvm +KOMPILE_FLAGS=--syntax-module TEST + +include ../../../include/kframework/ktest.mak diff --git a/k-distribution/tests/regression-new/mint-llvm-3/test.k b/k-distribution/tests/regression-new/mint-llvm-3/test.k new file mode 100644 index 00000000000..f522b626f48 --- /dev/null +++ b/k-distribution/tests/regression-new/mint-llvm-3/test.k @@ -0,0 +1,19 @@ +module TEST + imports BOOL + imports MINT + + syntax MInt{64} + syntax MInt{32} + + syntax KItem ::= foo(MInt{64}) + + syntax MInt{64} ::= m64() [function] + rule m64() => 0p64 + syntax MInt{32} ::= m32() [function] + rule m32() => 0p32 + + rule foo(X) => .K + requires (X +MInt m64()) <=uMInt (roundMInt(m32()) < foo(0p64) +endmodule diff --git a/k-frontend/src/main/java/org/kframework/compile/AddSortInjections.java b/k-frontend/src/main/java/org/kframework/compile/AddSortInjections.java index 45002ce556a..94d7c86394d 100644 --- a/k-frontend/src/main/java/org/kframework/compile/AddSortInjections.java +++ b/k-frontend/src/main/java/org/kframework/compile/AddSortInjections.java @@ -501,7 +501,9 @@ private static Sort lub( s -> mod.subsorts().lessThanEq(s, Sorts.KBott()) || mod.subsorts().greaterThan(s, Sorts.K())); - if (expectedSort != null && !expectedSort.name().equals(SORTPARAM_NAME)) { + if (expectedSort != null + && expectedSort.head().params() == 0 + && !expectedSort.name().equals(SORTPARAM_NAME)) { bounds.removeIf(s -> !mod.subsorts().lessThanEq(s, expectedSort)); } diff --git a/k-frontend/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java b/k-frontend/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java index 08162c259f1..cbd9f6d3a1f 100644 --- a/k-frontend/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java +++ b/k-frontend/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java @@ -429,15 +429,6 @@ public static Tuple3 getCombinedGrammarImpl( stream(mod.allSorts()) .filter(s -> (!isParserSort(s) || s.equals(Sorts.KItem()) || s.equals(Sorts.K()))) .toList(); - for (SortHead sh : mutable(mod.definedInstantiations()).keySet()) { - for (Sort s : mutable(mod.definedInstantiations().apply(sh))) { - // syntax MInt{K} ::= MInt{6} - Production p1 = - Production( - Option.empty(), Seq(), Sort(s.name(), Sorts.K()), Seq(NonTerminal(s)), Att.empty()); - prods.add(p1); - } - } for (Production p : iterable(mod.productions())) { if (p.params().nonEmpty()) { if (p.params().contains(p.sort())) { // case 1 @@ -634,6 +625,17 @@ public static Tuple3 getCombinedGrammarImpl( } disambProds = new HashSet<>(parseProds); + + for (SortHead sh : mutable(mod.definedInstantiations()).keySet()) { + for (Sort s : mutable(mod.definedInstantiations().apply(sh))) { + // syntax MInt{K} ::= MInt{6} + Production p1 = + Production( + Option.empty(), Seq(), Sort(s.name(), Sorts.K()), Seq(NonTerminal(s)), Att.empty()); + parseProds.add(p1); + } + } + if (mod.importedModuleNames().contains(PROGRAM_LISTS)) { Set prods3 = new HashSet<>(); // if no start symbol has been defined in the configuration, then use K diff --git a/k-frontend/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferenceVisitor.java b/k-frontend/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferenceVisitor.java index ca99277fa09..7b261ed90e4 100644 --- a/k-frontend/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferenceVisitor.java +++ b/k-frontend/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferenceVisitor.java @@ -230,7 +230,7 @@ public Either, Term> apply(Term term) { return typeError(pr, expectedSort, inferred); } // well typed, so add a cast and return - return wrapTermWithCast((Constant) pr, inferred); + return wrapTermWithCast(pr, inferred); } // compute the instantiated production with its sort parameters Production substituted = pr.production().substitute(inferencer.getArgs(pr)); @@ -275,12 +275,26 @@ public Either, Term> apply(Term term) { j++; } } + if (pr.production().params().nonEmpty() && hasParametricSort(pr.production())) { + return wrapTermWithCast(tc, substituted.sort()); + } return Right.apply(tc); } return Right.apply(pr); } - private Either, Term> wrapTermWithCast(Constant c, Sort declared) { + private boolean hasParametricSort(Production prod) { + if (prod.sort().head().params() != 0) { + return true; + } + if (stream(prod.nonterminals()).anyMatch(nt -> nt.sort().head().params() != 0)) { + return true; + } + return false; + } + + private Either, Term> wrapTermWithCast( + ProductionReference pr, Sort declared) { if (castContext != CastContext.SEMANTIC) { // There isn't an existing :Sort, so add one Production cast = @@ -289,9 +303,10 @@ private Either, Term> wrapTermWithCast(Constant c, Sort declar .productionsFor() .apply(KLabel("#SemanticCastTo" + declared.toString())) .head(); - return Right.apply(TermCons.apply(ConsPStack.singleton(c), cast, c.location(), c.source())); + return Right.apply( + TermCons.apply(ConsPStack.singleton(pr), cast, pr.location(), pr.source())); } - return Right.apply(c); + return Right.apply(pr); } } }