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

#422 Added support for array initialization without size #448

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,24 @@ int main() {
a[1] = 2;
return a[1];
}");

[Fact]
public Task GlobalArrayInitializationWithoutSize() => DoTest(@"
int ints1[3] = { 1, 2, 3 };
int ints2[] = { 1, 2, 1 };

int main() {
return ints1[0] + ints2[2];
}");

[Fact]
public Task ArrayInitializationWithoutSize() => DoTest(@"int main() {
int ints1[3] = { 1, 2, 3 };
int ints2[] = { 1, 2, 1 };
return ints1[0] + ints2[2];
}");

[Fact]
public Task ArrayParameterPassing() => DoTest(@"
int foo(int ints[]) { return ints[0]; }");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
System.Int32 <Module>::main()
Locals:
System.Int32* V_0
System.Int32* V_1
IL_0000: ldc.i4.s 12
IL_0002: conv.u
IL_0003: localloc
IL_0005: stloc.0
IL_0006: ldsflda <ConstantPool>/<ConstantPoolItemType12> <ConstantPool>::ConstDataBuffer0
IL_000b: ldloc V_0
IL_000f: ldc.i4.s 12
IL_0011: conv.u
IL_0012: call System.Void Cesium.Runtime.RuntimeHelpers::InitializeCompound(System.Void*,System.Void*,System.UInt32)
IL_0017: ldc.i4.s 12
IL_0019: conv.u
IL_001a: localloc
IL_001c: stloc.1
IL_001d: ldsflda <ConstantPool>/<ConstantPoolItemType12> <ConstantPool>::ConstDataBuffer1
IL_0022: ldloc V_1
IL_0026: ldc.i4.s 12
IL_0028: conv.u
IL_0029: call System.Void Cesium.Runtime.RuntimeHelpers::InitializeCompound(System.Void*,System.Void*,System.UInt32)
IL_002e: ldloc.0
IL_002f: ldc.i4.4
IL_0030: ldc.i4.0
IL_0031: mul
IL_0032: add
IL_0033: ldind.i4
IL_0034: ldloc.1
IL_0035: ldc.i4.4
IL_0036: ldc.i4.2
IL_0037: mul
IL_0038: add
IL_0039: ldind.i4
IL_003a: add
IL_003b: 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
System.Int32 <Module>::foo(System.Int32* ints)
IL_0000: ldarg.0
IL_0001: ldc.i4.4
IL_0002: ldc.i4.0
IL_0003: mul
IL_0004: add
IL_0005: ldind.i4
IL_0006: ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
System.Void <Module>::.cctor()
IL_0000: ldc.i4.s 12
IL_0002: conv.u
IL_0003: call System.Void* Cesium.Runtime.RuntimeHelpers::AllocateGlobalField(System.UInt32)
IL_0008: stsfld System.Int32* <Module>::ints1
IL_000d: ldsflda <ConstantPool>/<ConstantPoolItemType12> <ConstantPool>::ConstDataBuffer0
IL_0012: ldsflda System.Int32* <Module>::ints1
IL_0017: ldc.i4.s 12
IL_0019: conv.u
IL_001a: call System.Void Cesium.Runtime.RuntimeHelpers::InitializeCompound(System.Void*,System.Void*,System.UInt32)
IL_001f: ldc.i4.s 12
IL_0021: conv.u
IL_0022: call System.Void* Cesium.Runtime.RuntimeHelpers::AllocateGlobalField(System.UInt32)
IL_0027: stsfld System.Int32* <Module>::ints2
IL_002c: ldsflda <ConstantPool>/<ConstantPoolItemType12> <ConstantPool>::ConstDataBuffer1
IL_0031: ldsflda System.Int32* <Module>::ints2
IL_0036: ldc.i4.s 12
IL_0038: conv.u
IL_0039: call System.Void Cesium.Runtime.RuntimeHelpers::InitializeCompound(System.Void*,System.Void*,System.UInt32)
IL_003e: ret

System.Int32 <Module>::main()
IL_0000: ldsflda System.Int32* <Module>::ints1
IL_0005: ldc.i4.4
IL_0006: ldc.i4.0
IL_0007: mul
IL_0008: add
IL_0009: ldind.i4
IL_000a: ldsflda System.Int32* <Module>::ints2
IL_000f: ldc.i4.4
IL_0010: ldc.i4.2
IL_0011: mul
IL_0012: add
IL_0013: ldind.i4
IL_0014: add
IL_0015: 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
19 changes: 15 additions & 4 deletions Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
string? Identifier,
string? CliImportMemberName)
{
public static LocalDeclarationInfo Of(IReadOnlyList<IDeclarationSpecifier> specifiers, Declarator? declarator)
public static LocalDeclarationInfo Of(IReadOnlyList<IDeclarationSpecifier> specifiers, Declarator? declarator, Initializer? initializer = null)
{
var (type, cliImportMemberName) = ProcessSpecifiers(specifiers);
if (declarator == null)
Expand All @@ -36,7 +36,7 @@

var (pointer, directDeclarator) = declarator;
type = ProcessPointer(pointer, type);
(type, var identifier) = ProcessDirectDeclarator(directDeclarator, type);
(type, var identifier) = ProcessDirectDeclarator(directDeclarator, type, initializer);

return new LocalDeclarationInfo(type, identifier, cliImportMemberName);
}
Expand Down Expand Up @@ -168,7 +168,7 @@
return type;
}

private static (IType, string? Identifier) ProcessDirectDeclarator(IDirectDeclarator directDeclarator, IType type)
private static (IType, string? Identifier) ProcessDirectDeclarator(IDirectDeclarator directDeclarator, IType type, Initializer? initializer = null)
{
string? identifier = null;

Expand Down Expand Up @@ -214,7 +214,18 @@

// TODO[#126]: should check that size required in scoped declaration and not needed in parameter declaration
if (sizeExpr == null)
type = new PointerType(type);
{
if (initializer != null && initializer is ArrayInitializer arrayInitializer &&
arrayInitializer.Initializers.Length > 0)
{
var size = arrayInitializer.Initializers.Length;
type = CreateArrayType(type, size);
}
else
{
type = new PointerType(type);
}
}
else
{
if (sizeExpr is not ConstantLiteralExpression constantExpression ||
Expand Down Expand Up @@ -288,7 +299,7 @@
$"Direct abstract declarator is not supported, yet: {current}.");
}

current = current.Base;

Check warning on line 302 in Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs

View workflow job for this annotation

GitHub Actions / main (macos-latest)

Unreachable code detected

Check warning on line 302 in Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs

View workflow job for this annotation

GitHub Actions / main (macos-latest)

Unreachable code detected

Check warning on line 302 in Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest)

Unreachable code detected

Check warning on line 302 in Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest)

Unreachable code detected

Check warning on line 302 in Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs

View workflow job for this annotation

GitHub Actions / main (windows-latest)

Unreachable code detected

Check warning on line 302 in Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs

View workflow job for this annotation

GitHub Actions / main (windows-latest)

Unreachable code detected
}

return type;
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Declarations/ScopedDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static InitializableDeclarationInfo IdentifierOf(
InitDeclarator initDeclarator)
{
var (declarator, initializer) = initDeclarator;
var declarationInfo = LocalDeclarationInfo.Of(specifiers, declarator);
var declarationInfo = LocalDeclarationInfo.Of(specifiers, declarator, initializer);
var (type, _, _) = declarationInfo;
var expression = ConvertInitializer(type, initializer);
return new InitializableDeclarationInfo(declarationInfo, expression);
Expand Down
Loading