From 4f03e487ed9341271025eacbef1c196454cf1f76 Mon Sep 17 00:00:00 2001 From: abrahamFerga Date: Tue, 10 Oct 2023 00:54:48 -0600 Subject: [PATCH 1/2] #422 Added support for array initialization without size --- .../Ir/Declarations/LocalDeclarationInfo.cs | 19 +++++++++++++++---- .../Ir/Declarations/ScopedDeclarationInfo.cs | 2 +- .../array/array_initialization.c | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs b/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs index ec3f6050..6500e4ea 100644 --- a/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs +++ b/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs @@ -16,7 +16,7 @@ internal record LocalDeclarationInfo( string? Identifier, string? CliImportMemberName) { - public static LocalDeclarationInfo Of(IReadOnlyList specifiers, Declarator? declarator) + public static LocalDeclarationInfo Of(IReadOnlyList specifiers, Declarator? declarator, Initializer? initializer = null) { var (type, cliImportMemberName) = ProcessSpecifiers(specifiers); if (declarator == null) @@ -36,7 +36,7 @@ public static LocalDeclarationInfo Of(IReadOnlyList speci 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); } @@ -168,7 +168,7 @@ private static IType ProcessPointer(Pointer? pointer, IType type) 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; @@ -214,7 +214,18 @@ private static (IType, string? Identifier) ProcessDirectDeclarator(IDirectDeclar // 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 || diff --git a/Cesium.CodeGen/Ir/Declarations/ScopedDeclarationInfo.cs b/Cesium.CodeGen/Ir/Declarations/ScopedDeclarationInfo.cs index a9cc88d5..6a577e80 100644 --- a/Cesium.CodeGen/Ir/Declarations/ScopedDeclarationInfo.cs +++ b/Cesium.CodeGen/Ir/Declarations/ScopedDeclarationInfo.cs @@ -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); diff --git a/Cesium.IntegrationTests/array/array_initialization.c b/Cesium.IntegrationTests/array/array_initialization.c index c9d64698..9b856e9f 100644 --- a/Cesium.IntegrationTests/array/array_initialization.c +++ b/Cesium.IntegrationTests/array/array_initialization.c @@ -52,6 +52,16 @@ int testGlobalIntArray() { return 1; } +//int testNoSizeSpecified() { +// char a[] = { 99, 0, 22, 17, 2, 0, }; +// +// if (sizeof(a) != 6) { +// return 0; +// } +// +// return 1; +//} + int main(int argc, char *argv[]) { if (!testIntArray()) { @@ -70,5 +80,9 @@ int main(int argc, char *argv[]) return -4; } + //if (!testNoSizeSpecified()) { + // return -5; + //} + return 42; } From ad64f69151b1accacb5ec1e3b5e98a3019039da6 Mon Sep 17 00:00:00 2001 From: abrahamFerga Date: Sat, 14 Oct 2023 21:17:48 -0600 Subject: [PATCH 2/2] Added tests for array initialization without size --- Cesium.CodeGen.Tests/CodeGenArrayTests.cs | 20 ++++++++ ...rrayInitializationWithoutSize.verified.txt | 46 +++++++++++++++++++ ...ayTests.ArrayParameterPassing.verified.txt | 8 ++++ ...rrayInitializationWithoutSize.verified.txt | 46 +++++++++++++++++++ .../array/array_initialization.c | 14 ------ 5 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayInitializationWithoutSize.verified.txt create mode 100644 Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayParameterPassing.verified.txt create mode 100644 Cesium.CodeGen.Tests/verified/CodeGenArrayTests.GlobalArrayInitializationWithoutSize.verified.txt diff --git a/Cesium.CodeGen.Tests/CodeGenArrayTests.cs b/Cesium.CodeGen.Tests/CodeGenArrayTests.cs index 9da9cf43..958a20a1 100644 --- a/Cesium.CodeGen.Tests/CodeGenArrayTests.cs +++ b/Cesium.CodeGen.Tests/CodeGenArrayTests.cs @@ -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]; }"); } diff --git a/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayInitializationWithoutSize.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayInitializationWithoutSize.verified.txt new file mode 100644 index 00000000..c61932bd --- /dev/null +++ b/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayInitializationWithoutSize.verified.txt @@ -0,0 +1,46 @@ +System.Int32 ::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 / ::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 / ::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 ::() + Locals: + System.Int32 V_0 + IL_0000: call System.Int32 ::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 diff --git a/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayParameterPassing.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayParameterPassing.verified.txt new file mode 100644 index 00000000..2f941d7d --- /dev/null +++ b/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.ArrayParameterPassing.verified.txt @@ -0,0 +1,8 @@ +System.Int32 ::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 diff --git a/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.GlobalArrayInitializationWithoutSize.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.GlobalArrayInitializationWithoutSize.verified.txt new file mode 100644 index 00000000..0c548001 --- /dev/null +++ b/Cesium.CodeGen.Tests/verified/CodeGenArrayTests.GlobalArrayInitializationWithoutSize.verified.txt @@ -0,0 +1,46 @@ +System.Void ::.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* ::ints1 + IL_000d: ldsflda / ::ConstDataBuffer0 + IL_0012: ldsflda System.Int32* ::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* ::ints2 + IL_002c: ldsflda / ::ConstDataBuffer1 + IL_0031: ldsflda System.Int32* ::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 ::main() + IL_0000: ldsflda System.Int32* ::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* ::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 ::() + Locals: + System.Int32 V_0 + IL_0000: call System.Int32 ::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 diff --git a/Cesium.IntegrationTests/array/array_initialization.c b/Cesium.IntegrationTests/array/array_initialization.c index 9b856e9f..c9d64698 100644 --- a/Cesium.IntegrationTests/array/array_initialization.c +++ b/Cesium.IntegrationTests/array/array_initialization.c @@ -52,16 +52,6 @@ int testGlobalIntArray() { return 1; } -//int testNoSizeSpecified() { -// char a[] = { 99, 0, 22, 17, 2, 0, }; -// -// if (sizeof(a) != 6) { -// return 0; -// } -// -// return 1; -//} - int main(int argc, char *argv[]) { if (!testIntArray()) { @@ -80,9 +70,5 @@ int main(int argc, char *argv[]) return -4; } - //if (!testNoSizeSpecified()) { - // return -5; - //} - return 42; }