-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solving the FP completeness issues in #1723 #3023
Draft
mattulbrich
wants to merge
15
commits into
main
Choose a base branch
from
mu/unbalancedFloats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5fcf646
this contributed to solving the FP completeness issues in #1723
mattulbrich f7924c8
Merge branch 'main' into mu/unbalancedFloats
wadoon bd9d054
First try in fixing cast assignments
samysweb 89cfded
Added Int/Long to Double casting rules
samysweb 085614e
this contributed to solving the FP completeness issues in #1723
mattulbrich 2683767
Merge branch 'main' into steuber/unbalancedFloats
samysweb f5970f1
Added type constraint to assignment and int assignment rules
samysweb 1595a2e
Might have fixed completeness bug w.r.t. casts
samysweb b6603d4
Rebuilt test oracle
samysweb 73da788
Merge branch 'mu/unbalancedFloats' of github.com:KeYProject/key into …
samysweb 56f918f
Merge branch 'main' into mu/unbalancedFloats
samysweb 1a6b938
Merge branch 'main' into steuber/unbalancedFloats
samysweb 59ba9ea
Merge branch 'mu/unbalancedFloats' into steuber/unbalancedFloats
samysweb d05f402
Fix spotless
samysweb 02be342
Floating Point rules for casts (#3062)
samysweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
key.core/src/main/java/de/uka/ilkd/key/rule/conditions/FloatingPointBalancedCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package de.uka.ilkd.key.rule.conditions; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import de.uka.ilkd.key.java.Expression; | ||
import de.uka.ilkd.key.java.JavaProgramElement; | ||
import de.uka.ilkd.key.java.ProgramElement; | ||
import de.uka.ilkd.key.java.Services; | ||
import de.uka.ilkd.key.java.abstraction.KeYJavaType; | ||
import de.uka.ilkd.key.java.expression.Operator; | ||
import de.uka.ilkd.key.java.expression.operator.BinaryOperator; | ||
import de.uka.ilkd.key.java.expression.operator.ComparativeOperator; | ||
import de.uka.ilkd.key.java.expression.operator.TypeCast; | ||
import de.uka.ilkd.key.java.reference.TypeRef; | ||
import de.uka.ilkd.key.java.visitor.ProgramElementReplacer; | ||
import de.uka.ilkd.key.logic.op.SVSubstitute; | ||
import de.uka.ilkd.key.logic.op.SchemaVariable; | ||
import de.uka.ilkd.key.logic.sort.Sort; | ||
import de.uka.ilkd.key.rule.MatchConditions; | ||
import de.uka.ilkd.key.rule.VariableCondition; | ||
import de.uka.ilkd.key.rule.inst.SVInstantiations; | ||
|
||
|
||
/** | ||
* This variable condition adds required numeric promotions to Java operations | ||
* with floating point arguments. | ||
* | ||
* For example: In the expression 1.0f + 1.0d, the first argument will be implicitly | ||
* cast to double like (double)1.0f + 1.0d. | ||
* | ||
* If such an unbalanced expression occurs in the program, according casts are | ||
* introduced by this varcond. | ||
* | ||
* The varcond is used like \floatingPointBalanced(#unbalanced, #balanced) | ||
* where the first argument is the one from the find expression of the rule | ||
* and the second one is the one that will be changed. | ||
* | ||
* @author Mattias Ulbrich | ||
* @see de.uka.ilkd.key.logic.sort.ProgramSVSort.FloatingPointBinaryExprSort | ||
*/ | ||
public final class FloatingPointBalancedCondition implements VariableCondition { | ||
/** | ||
* The first SV: It holds the unbalanced input expression | ||
*/ | ||
private final SchemaVariable unbalanced; | ||
/** | ||
* The 2nd SV: It holds the balanced computed expression | ||
*/ | ||
private final SchemaVariable balanced; | ||
|
||
public FloatingPointBalancedCondition(SchemaVariable unbalanced, SchemaVariable balanced) { | ||
this.unbalanced = unbalanced; | ||
this.balanced = balanced; | ||
} | ||
|
||
@Override | ||
public MatchConditions check(SchemaVariable var, SVSubstitute instCandidate, MatchConditions mc, | ||
Services services) { | ||
|
||
SVInstantiations svInst = mc.getInstantiations(); | ||
Object untypedInstantiation = svInst.getInstantiation(unbalanced); | ||
if (!(untypedInstantiation instanceof BinaryOperator | ||
|| untypedInstantiation instanceof ComparativeOperator)) { | ||
return null; | ||
} | ||
Operator inInst = (Operator) untypedInstantiation; | ||
JavaProgramElement outInst = (JavaProgramElement) svInst.getInstantiation(balanced); | ||
if (inInst == null) { | ||
return mc; | ||
} | ||
|
||
Operator properResultInst = balance(inInst, services); | ||
if (properResultInst == null) { | ||
return null; | ||
} else if (outInst == null) { | ||
svInst = svInst.add(balanced, properResultInst, services); | ||
return mc.setInstantiations(svInst); | ||
} else if (outInst.equals(properResultInst)) { | ||
return mc; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "\\floatingPointBalanced(" + unbalanced + ", " + balanced + ")"; | ||
} | ||
|
||
private static KeYJavaType getKeYJavaType(ProgramElement pe, Services services) { | ||
return services.getTypeConverter().getKeYJavaType((Expression) pe); | ||
} | ||
|
||
/** | ||
* Make sure the result is a binary operation with same types on lhs | ||
* and rhs. do this by adding cast if needed. | ||
* | ||
* If no cast is needed, return null. | ||
* | ||
* @param inInst the binary AST element to balance | ||
* @param services as usual ... to lookup everything | ||
* @return null if already same types. Otherwise a binary operator which | ||
* has an added cast compared to the input | ||
*/ | ||
private static @Nullable Operator balance(Operator inInst, Services services) { | ||
|
||
ProgramElement child0 = inInst.getChildAt(0); | ||
ProgramElement child1 = inInst.getChildAt(1); | ||
|
||
KeYJavaType type0 = getKeYJavaType(child0, services); | ||
KeYJavaType type1 = getKeYJavaType(child1, services); | ||
if (type0.getSort() == type1.getSort()) { | ||
// nothing to be done ... same type | ||
return null; | ||
} | ||
|
||
Sort doubleSort = services.getTypeConverter().getDoubleLDT().targetSort(); | ||
Sort floatSort = services.getTypeConverter().getFloatLDT().targetSort(); | ||
if (type0.getSort() == doubleSort) { | ||
return cast(inInst, 1, type0, services); | ||
} | ||
if (type1.getSort() == doubleSort) { | ||
return cast(inInst, 0, type1, services); | ||
} | ||
if (type0.getSort() == floatSort) { | ||
return cast(inInst, 1, type0, services); | ||
} | ||
if (type1.getSort() == floatSort) { | ||
return cast(inInst, 0, type1, services); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Add a cast to a binary operation. | ||
* | ||
* @param inInst the tree to modify | ||
* @param childNo the child to which a cast is to be added | ||
* @param kjt the type to which to cast | ||
* @param services as usual | ||
* @return a binary operation similar to the input, but with one | ||
* cast added to child childNo. | ||
*/ | ||
private static Operator cast(Operator inInst, int childNo, KeYJavaType kjt, | ||
Services services) { | ||
Expression child = (Expression) inInst.getChildAt(childNo); | ||
TypeCast cast = new TypeCast(child, new TypeRef(kjt)); | ||
ProgramElementReplacer per = new ProgramElementReplacer(inInst, services); | ||
ProgramElement result = per.replace(child, cast); | ||
return (Operator) result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / QDJVMC
Constant conditions & exceptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smells like a false alert?