Skip to content

Commit

Permalink
Add ability to use const expressions inside array initializers (#693)
Browse files Browse the repository at this point in the history
* Add ability to use const expressions inside array initializers
  • Loading branch information
kant2002 authored Nov 7, 2024
1 parent 9b8679e commit 5b91822
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ int main() {
return 0;
}
return 1;
}");

[Fact]
public Task ArrayInitializerConstExpression() => DoTest(@"
int main() {
int a[] = { -2, 99, 0, -743, 1+1, 2, 4 };
if (a[1] != 99) {
return 0;
}
return 1;
}");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
System.Int32 <Module>::main()
Locals:
System.Int32* V_0
IL_0000: ldc.i4.s 28
IL_0002: conv.u
IL_0003: localloc
IL_0005: stloc.0
IL_0006: ldsflda <ConstantPool>/<ConstantPoolItemType28> <ConstantPool>::ConstDataBuffer0
IL_000b: ldloc V_0
IL_000f: ldc.i4.s 28
IL_0011: conv.u
IL_0012: call System.Void Cesium.Runtime.RuntimeHelpers::InitializeCompound(System.Void*,System.Void*,System.UInt32)
IL_0017: ldloc.0
IL_0018: ldc.i4.1
IL_0019: conv.i
IL_001a: ldc.i4 4
IL_001f: mul
IL_0020: add
IL_0021: ldind.i4
IL_0022: ldc.i4.s 99
IL_0024: ceq
IL_0026: ldc.i4.0
IL_0027: ceq
IL_0029: brfalse IL_0030
IL_002e: ldc.i4.0
IL_002f: ret
IL_0030: nop
IL_0031: ldc.i4.1
IL_0032: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::main()
IL_0005: stloc.s V_0
IL_0007: ldloc.s V_0
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
IL_000e: ldloc.s V_0
IL_0010: ret
27 changes: 26 additions & 1 deletion Cesium.CodeGen/Ir/Expressions/ArrayInitializerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ public IType GetExpressionType(IDeclarationScope scope)

public IExpression Lower(IDeclarationScope scope)
{
throw new NotImplementedException();
return InlineConstantExpressions(scope);
}

public ArrayInitializerExpression InlineConstantExpressions(IDeclarationScope scope)
{
List<IExpression?> expressions = new();
foreach (var initializer in Initializers)
{
if (initializer is null)
{
expressions.Add(initializer);
continue;
}

var (errorMessage, constant) = ConstantEvaluator.TryGetConstantValue(initializer);
if (constant != null)
{
expressions.Add(new ConstantLiteralExpression(constant));
}
else
{
expressions.Add(initializer);
}
}

return new ArrayInitializerExpression(expressions.ToImmutableArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ void WriteValue(long value)

public IType GetExpressionType(IDeclarationScope scope) => _type;

public IExpression Lower(IDeclarationScope scope) => new CompoundInitializationExpression(scope.ResolveType(_type), _arrayInitializer);
public IExpression Lower(IDeclarationScope scope) => new CompoundInitializationExpression(scope.ResolveType(_type), _arrayInitializer.InlineConstantExpressions(scope));
}

0 comments on commit 5b91822

Please sign in to comment.