Skip to content
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

No flatten for most backends #5528

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
97d81e2
Add patterns/codeSize test
keyboardDrummer May 29, 2024
e77441a
Update codeSize test
keyboardDrummer May 29, 2024
547317f
Split off statement part from SPCG
keyboardDrummer May 29, 2024
55b4cde
Draft of nested match compiler
keyboardDrummer May 30, 2024
b4bc5cf
Save
keyboardDrummer May 30, 2024
f10aa78
Generated code looks good upon visual inspection
keyboardDrummer May 30, 2024
ee9ae6f
Generated code compiles and runs
keyboardDrummer May 30, 2024
069dddb
Move NestedMatch generation to just C#
keyboardDrummer May 30, 2024
da513a0
Attempt at implementation for expressions
keyboardDrummer May 30, 2024
88536c8
Ran formatter
keyboardDrummer May 30, 2024
f13be6e
Fixes
keyboardDrummer May 30, 2024
a61d607
Update C# code generator zo it also uses custom generation for nested…
keyboardDrummer May 30, 2024
846ad62
Regen GeneratedFromDafny code
keyboardDrummer May 30, 2024
85c1b20
Revert "Regen GeneratedFromDafny code"
keyboardDrummer May 30, 2024
3660a37
Fix gen
keyboardDrummer May 30, 2024
26000a5
Fixes for generics
keyboardDrummer May 30, 2024
2805584
Update generated files
keyboardDrummer May 30, 2024
09f39f8
Merge branch 'master' into noFlattenForCSharp
keyboardDrummer May 30, 2024
82a5d71
Fix bug related to disjoint patterns
keyboardDrummer May 31, 2024
c32be6f
Merge branch 'noFlattenForCSharp' of github.com:keyboardDrummer/dafny…
keyboardDrummer May 31, 2024
861cee7
Merge remote-tracking branch 'origin/master' into noFlattenForCSharp
keyboardDrummer May 31, 2024
1e25d7b
Fix literal pattern bug
keyboardDrummer May 31, 2024
6791e43
Remove DatatypeWrapperEraser.IsErasableDatatypeWrapper check
keyboardDrummer May 31, 2024
eedcf47
Ran formatter
keyboardDrummer May 31, 2024
6f981ab
Fix bug in disjunctive assignments
keyboardDrummer May 31, 2024
0bd8c0d
Merge branch 'master' into noFlattenForCSharp
keyboardDrummer May 31, 2024
903f2bf
Trigger CI
keyboardDrummer May 31, 2024
5d9dc07
Merge branch 'noFlattenForCSharp' of github.com:keyboardDrummer/dafny…
keyboardDrummer May 31, 2024
9477c6f
Fixes
keyboardDrummer Jun 1, 2024
b919fa8
Refactoring
keyboardDrummer Jun 1, 2024
c6de95b
Add force integration tests code
keyboardDrummer Jun 3, 2024
0672492
Merge branch 'master' into noFlattenForCSharp
keyboardDrummer Jun 4, 2024
3c86dd7
Convert if else chains to switch
keyboardDrummer Jun 4, 2024
e45420a
Merge commit 'a748799eef1d7~1' into noFlattenForCSharp
keyboardDrummer Jun 5, 2024
b8de307
Merge commit 'a748799eef1d7' into noFlattenForCSharp
keyboardDrummer Jun 5, 2024
f97bf09
Merge remote-tracking branch 'origin/master' into noFlattenForCSharp
keyboardDrummer Jun 5, 2024
75bb47e
Ran formatter
keyboardDrummer Jun 5, 2024
e1bb707
Refactoring
keyboardDrummer Jun 5, 2024
dbcb4a4
Further refactoring
keyboardDrummer Jun 5, 2024
420a13b
Further refactoring
keyboardDrummer Jun 5, 2024
5f860c9
Attempt at using nested matches for all backends
keyboardDrummer Jun 5, 2024
808d090
Use flattened matches for DafnyCodeGenerator
keyboardDrummer Jun 5, 2024
126344a
Reintroduce flattening for Java
keyboardDrummer Jun 5, 2024
a1011f7
Fixes
keyboardDrummer Jun 5, 2024
387b35f
Remove codeSize test
keyboardDrummer Jun 5, 2024
49e0c55
Update expect files
keyboardDrummer Jun 5, 2024
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
Prev Previous commit
Next Next commit
Fix literal pattern bug
  • Loading branch information
keyboardDrummer committed May 31, 2024
commit 1e25d7b57948446120d0636822c34b5ec8401c9e
15 changes: 7 additions & 8 deletions Source/DafnyCore/Backends/CSharp/CsharpCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3655,15 +3655,14 @@ private ConcreteSyntaxTree EmitNestedMatchCaseConditions(string sourceName,

var litExpression = MatchFlattener.GetLiteralExpressionFromPattern(pattern);
if (litExpression != null) {

var thenWriter = EmitIf(out var guardWriter, false, writer);
guardWriter.Write(sourceName);
// CompileBinOp(BinaryExpr.ResolvedOpcode.EqCommon, sourceType, sourceType, pattern.Tok, Type.Bool,
// out var opString, out var _, out var _, out var _, out var _,
// out var _, out var _, out var _, out var _,
// writer);
guardWriter.Write(" == ");
EmitExpr(litExpression, false, guardWriter, writer);
CompileBinOp(BinaryExpr.ResolvedOpcode.EqCommon, sourceType, litExpression.Type, pattern.Tok, Type.Bool,
out var opString, out var preOpString, out var postOpString, out var callString, out var staticCallString,
out _, out _, out _, out _,
writer);
var right = new ConcreteSyntaxTree();
EmitExpr(litExpression, false, right, writer);
EmitBinaryExprUsingConcreteSyntax(guardWriter, Type.Bool, preOpString, opString, new LineSegment(sourceName), right, callString, staticCallString, postOpString);
writer = thenWriter;
} else if (pattern is IdPattern idPattern) {
if (idPattern.Ctor == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,70 +272,8 @@ void EmitExpr(Expression e2, ConcreteSyntaxTree wr2, bool inLetExpr, ConcreteSyn
} else if (expr is TypeTestExpr typeTestExpr) {
CompileTypeTest(typeTestExpr, inLetExprBody, wr, ref wStmts);

} else if (expr is BinaryExpr) {
var e = (BinaryExpr)expr;

if (IsComparisonToZero(e, out var arg, out var sign, out var negated) &&
CompareZeroUsingSign(arg.Type)) {
// Transform e.g. x < BigInteger.Zero into x.Sign == -1
var w = EmitSign(arg.Type, wr);
TrParenExpr(arg, w, inLetExprBody, wStmts);
wr.Write(negated ? " != " : " == ");
wr.Write(sign.ToString());
} else {
CompileBinOp(e.ResolvedOp, e.E0.Type, e.E1.Type, e.tok, expr.Type.GetRuntimeType(),
out var opString,
out var preOpString,
out var postOpString,
out var callString,
out var staticCallString,
out var reverseArguments,
out var truncateResult,
out var convertE1_to_int,
out var coerceE1,
wr);

if (truncateResult && e.Type.NormalizeToAncestorType().AsBitVectorType is { } bitvectorType) {
wr = EmitBitvectorTruncation(bitvectorType, e.Type.AsNativeType(), true, wr);
}

var e0 = reverseArguments ? e.E1 : e.E0;
var e1 = reverseArguments ? e.E0 : e.E1;

var left = Expr(e0, inLetExprBody, wStmts);
ConcreteSyntaxTree right;
if (convertE1_to_int) {
right = ExprAsNativeInt(e1, inLetExprBody, wStmts);
} else {
right = Expr(e1, inLetExprBody, wStmts);
if (coerceE1) {
right = CoercionIfNecessary(e1.Type, TypeForCoercion(e1.Type), e1.tok, right);
}
}

wr.Write(preOpString);
if (opString != null) {
var nativeType = AsNativeType(e.Type);
string nativeName = null;
bool needsCast = false;
if (nativeType != null) {
GetNativeInfo(nativeType.Sel, out nativeName, out _, out needsCast);
}

var opResult = ConcreteSyntaxTree.Create($"{left.InParens()} {opString} {right.InParens()}");
if (needsCast) {
opResult = Cast(new LineSegment(nativeName), opResult);
}

wr.Append(opResult);
} else if (callString != null) {
wr.Format($"{left.InParens()}.{callString}({right})");
} else if (staticCallString != null) {
wr.Format($"{staticCallString}({left}, {right})");
}

wr.Write(postOpString);
}
} else if (expr is BinaryExpr binary) {
EmitBinaryExpr(inLetExprBody, wr, wStmts, binary);
} else if (expr is TernaryExpr) {
Contract.Assume(false); // currently, none of the ternary expressions is compilable

Expand Down Expand Up @@ -616,6 +554,79 @@ ConcreteSyntaxTree EmitGuardFragment(List<(Expression conj, ISet<IVariable> free
}
}

private void EmitBinaryExpr(bool inLetExprBody, ConcreteSyntaxTree wr,
ConcreteSyntaxTree wStmts, BinaryExpr binary)
{
if (IsComparisonToZero(binary, out var arg, out var sign, out var negated) &&
CompareZeroUsingSign(arg.Type)) {
// Transform e.g. x < BigInteger.Zero into x.Sign == -1
var w = EmitSign(arg.Type, wr);
TrParenExpr(arg, w, inLetExprBody, wStmts);
wr.Write(negated ? " != " : " == ");
wr.Write(sign.ToString());
} else {
CompileBinOp(binary.ResolvedOp, binary.E0.Type, binary.E1.Type, binary.tok, binary.Type.GetRuntimeType(),
out var opString,
out var preOpString,
out var postOpString,
out var callString,
out var staticCallString,
out var reverseArguments,
out var truncateResult,
out var convertE1_to_int,
out var coerceE1,
wr);

if (truncateResult && binary.Type.NormalizeToAncestorType().AsBitVectorType is { } bitvectorType) {
wr = EmitBitvectorTruncation(bitvectorType, binary.Type.AsNativeType(), true, wr);
}

var e0 = reverseArguments ? binary.E1 : binary.E0;
var e1 = reverseArguments ? binary.E0 : binary.E1;

var left = Expr(e0, inLetExprBody, wStmts);
ConcreteSyntaxTree right;
if (convertE1_to_int) {
right = ExprAsNativeInt(e1, inLetExprBody, wStmts);
} else {
right = Expr(e1, inLetExprBody, wStmts);
if (coerceE1) {
right = CoercionIfNecessary(e1.Type, TypeForCoercion(e1.Type), e1.tok, right);
}
}

EmitBinaryExprUsingConcreteSyntax(wr, binary.Type, preOpString, opString, left, right, callString, staticCallString, postOpString);
}
}

protected void EmitBinaryExprUsingConcreteSyntax(ConcreteSyntaxTree wr, Type resultType, string preOpString,
string opString, ICanRender left, ICanRender right, string callString, string staticCallString,
string postOpString)
{
wr.Write(preOpString);
if (opString != null) {
var nativeType = AsNativeType(resultType);
string nativeName = null;
bool needsCast = false;
if (nativeType != null) {
GetNativeInfo(nativeType.Sel, out nativeName, out _, out needsCast);
}

var opResult = ConcreteSyntaxTree.Create($"{left.InParens()} {opString} {right.InParens()}");
if (needsCast) {
opResult = Cast(new LineSegment(nativeName), opResult);
}

wr.Append(opResult);
} else if (callString != null) {
wr.Format($"{left.InParens()}.{callString}({right})");
} else if (staticCallString != null) {
wr.Format($"{staticCallString}({left}, {right})");
}

wr.Write(postOpString);
}

private void EmitMatchExpr(MatchExpr e, bool inLetExprBody, ConcreteSyntaxTree wr, ConcreteSyntaxTree wStmts) {
// ((System.Func<SourceType, TargetType>)((SourceType _source) => {
// if (source.is_Ctor0) {
Expand Down
10 changes: 6 additions & 4 deletions Source/DafnyCore/ConcreteSyntax/ConcreteSyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public enum BlockStyle {
NewlineBrace
}

public static class CanRenderExtensions {
public static ConcreteSyntaxTree InParens(this ICanRender canRender) {
return ConcreteSyntaxTree.Create($"({canRender})");
}
}

public class ConcreteSyntaxTree : ICanRender {
public ConcreteSyntaxTree(int relativeIndent = 0) {
RelativeIndentLevel = relativeIndent;
Expand Down Expand Up @@ -152,10 +158,6 @@ public ConcreteSyntaxTree Write(char value) {
return this;
}

public ConcreteSyntaxTree InParens() {
return Create($"({this})");
}

// ----- Nested blocks ------------------------------

public virtual ConcreteSyntaxTree ForkInParens() {
Expand Down
8 changes: 4 additions & 4 deletions Source/DafnyCore/GeneratedFromDafny/DCOMP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,7 @@ public void GenExprLiteral(DAST._IExpression e, Std.Wrappers._IOption<Dafny.ISeq
DAST._ILiteral _h150 = _source61.dtor_Literal_i_a0;
if (_h150.is_BoolLiteral) {
bool _h200 = _h150.dtor_BoolLiteral_i_a0;
if (_h200 == false) {
if ((_h200) == (false)) {
unmatched61 = false;
{
RAST._IExpr _out140;
Expand All @@ -2101,7 +2101,7 @@ public void GenExprLiteral(DAST._IExpression e, Std.Wrappers._IOption<Dafny.ISeq
DAST._ILiteral _h151 = _source61.dtor_Literal_i_a0;
if (_h151.is_BoolLiteral) {
bool _h201 = _h151.dtor_BoolLiteral_i_a0;
if (_h201 == true) {
if ((_h201) == (true)) {
unmatched61 = false;
{
RAST._IExpr _out142;
Expand Down Expand Up @@ -2339,9 +2339,9 @@ public void GenExprBinary(DAST._IExpression e, Std.Wrappers._IOption<Dafny.ISequ
if (unmatched66) {
if (_source66.is_Eq) {
bool referential0 = _source66.dtor_referential;
if (referential0 == true) {
if ((referential0) == (true)) {
bool nullable0 = _source66.dtor_nullable;
if (nullable0 == false) {
if ((nullable0) == (false)) {
unmatched66 = false;
return true;
}
Expand Down
Loading
Loading