-
Notifications
You must be signed in to change notification settings - Fork 57
Cell swap (#2) #1461
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
Cell swap (#2) #1461
Conversation
Add the new feature to support mutable swap operation on Cell
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.
Looks good 👍 ! Just minor details :)
There is also a missing case for CellSwap
in AntiAliasing#doNormalize
:
case swp @ CellSwap(cell1, cell2) =>
val (ctxCell1, normCell1) = normalizeForTarget(cell1)
val (ctxCell2, normCell2) = normalizeForTarget(cell2)
(ctxCell1 compose ctxCell2, CellSwap(normCell1, normCell2).copiedFrom(swp))
val cellClassDef = symbols.lookup.get[ClassDef]("stainless.lang.Cell").get | ||
val vFieldId = cellClassDef.fields.head.id | ||
|
||
val temp = ValDef.fresh("temp", base).setPos(cellSwap) | ||
val targets1 = getDirectTargetsDealiased(cell1, ClassFieldAccessor(vFieldId), env) | ||
.getOrElse(throw MalformedStainlessCode(cellSwap, "Unsupported cellSwap (first cell)")) | ||
val targets2 = getDirectTargetsDealiased(cell2, ClassFieldAccessor(vFieldId), env) | ||
.getOrElse(throw MalformedStainlessCode(cellSwap, "Unsupported cellSwap (second cell)")) | ||
|
||
val updates1 = updatedTargetsAndAliases(targets2, ClassSelector(cell1, vFieldId).setPos(cellSwap), env, cellSwap.getPos) | ||
val updates2 = updatedTargetsAndAliases(targets1, temp.toVariable, env, cellSwap.getPos) | ||
val updates = updates1 ++ updates2 | ||
if (updates.isEmpty) UnitLiteral().setPos(cellSwap) | ||
else | ||
Let(temp, transform(ClassSelector(cell2, vFieldId).setPos(cellSwap), env), | ||
Block(updates.init, updates.last).setPos(cellSwap) | ||
).setPos(cellSwap) |
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.
recCell1
and recCell2
should be used here
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.
whoops, fixed! Thanks :)
val tmpId = rec(FreshIdentifier("tmp")) | ||
val tmpVd = CIR.ValDef(tmpId, rec(cellBaseType), false) | ||
val tmp = CIR.Binding(tmpVd) | ||
throw new Exception(f"CellSwap to IR phase: cellBase type = $cellBaseType; vField Id = $vFieldId") |
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.
leftover?
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.
indeed 🙈
Closes #1356 |
Whoops, I added it! |
@mario-bucev I applied the fixes according to your comments! |
Awesome! |
Add a new
Cell
feature with aswap
to support more mutability, notably swapping 2 variables.This adds a new
Cell
class in the library, and aswap
function exchanging their values:These can be used as follows: