diff --git a/Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs b/Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs index bef8c18f..e6ad6b0c 100644 --- a/Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs +++ b/Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs @@ -9,7 +9,8 @@ private static Task DoTest(string source, string @namespace = "", string globalT { Assert.True(false, "TODO: Provide architecture for tests"); var assembly = GenerateAssembly(default, @namespace, globalTypeFqn, source); - return VerifyMethods(assembly); + var moduleType = assembly.Modules.Single().GetType(""); + return VerifyMethods(moduleType); } [Theory] diff --git a/Cesium.CodeGen.Tests/CodeGenTestBase.cs b/Cesium.CodeGen.Tests/CodeGenTestBase.cs index c34314cb..7e61fc08 100644 --- a/Cesium.CodeGen.Tests/CodeGenTestBase.cs +++ b/Cesium.CodeGen.Tests/CodeGenTestBase.cs @@ -22,7 +22,7 @@ protected static AssemblyDefinition GenerateAssembly(TargetRuntimeDescriptor? ru } protected static AssemblyDefinition GenerateAssembly(TargetRuntimeDescriptor? runtime, string @namespace = "", string globalTypeFqn = "", params string[] sources) { - var context = CreateAssembly(runtime, @namespace, globalTypeFqn); + var context = CreateAssembly(runtime, @namespace: @namespace, globalTypeFqn: globalTypeFqn); GenerateCode(context, sources); return EmitAssembly(context); } @@ -38,10 +38,15 @@ protected static void DoesNotCompile(string source, string expectedMessage) w Assert.Contains(expectedMessage, ex.Message); } - private static AssemblyContext CreateAssembly(TargetRuntimeDescriptor? targetRuntime, string @namespace = "", string globalTypeFqn = "") + private static AssemblyContext CreateAssembly( + TargetRuntimeDescriptor? targetRuntime, + TargetArchitectureSet targetArchitectureSet = TargetArchitectureSet.Dynamic, + string @namespace = "", + string globalTypeFqn = "") { CompilationOptions compilationOptions = new CompilationOptions( targetRuntime ?? TargetRuntimeDescriptor.Net60, + targetArchitectureSet, ModuleKind.Console, typeof(Math).Assembly.Location, typeof(Runtime.RuntimeHelpers).Assembly.Location, diff --git a/Cesium.CodeGen/CompilationOptions.cs b/Cesium.CodeGen/CompilationOptions.cs index db40110f..bbb28a39 100644 --- a/Cesium.CodeGen/CompilationOptions.cs +++ b/Cesium.CodeGen/CompilationOptions.cs @@ -4,6 +4,7 @@ namespace Cesium.CodeGen; public record CompilationOptions( TargetRuntimeDescriptor TargetRuntime, + TargetArchitectureSet TargetArchitectureSet, ModuleKind ModuleKind, string CorelibAssembly, string CesiumRuntime, diff --git a/Cesium.CodeGen/TargetArchitectureSet.cs b/Cesium.CodeGen/TargetArchitectureSet.cs new file mode 100644 index 00000000..a99550ae --- /dev/null +++ b/Cesium.CodeGen/TargetArchitectureSet.cs @@ -0,0 +1,20 @@ +namespace Cesium.CodeGen; + +/// Describes the set of system architectures targeted by the assembly. +public enum TargetArchitectureSet +{ + /// + /// Dynamic architecture. + /// + /// May not support every feature of the C programming language, but compiles to AnyCPU assembly. Performs some + /// calculations, such as pointer array size calculations, in runtime. + /// + /// + Dynamic, + + /// An architecture with 32-bit pointers. Targets ARM32 and x86 CPUs. + Bit32, + + /// An architecture with 64-bit pointers. Targets ARM64 and x86-64 CPUs. + Bit64 +} diff --git a/Cesium.Compiler/Arguments.cs b/Cesium.Compiler/Arguments.cs index 2d73873c..3ce71eb8 100644 --- a/Cesium.Compiler/Arguments.cs +++ b/Cesium.Compiler/Arguments.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Cesium.CodeGen; using CommandLine; using Mono.Cecil; @@ -24,6 +25,9 @@ public class Arguments [Option("framework", Default = TargetFrameworkKind.Net)] public TargetFrameworkKind Framework { get; init; } + [Option("arch", Default = TargetArchitectureSet.Dynamic)] + public TargetArchitectureSet TargetArchitectureSet { get; init; } + [Option("modulekind")] public ModuleKind? ModuleKind { get; init; } = null!; diff --git a/Cesium.Compiler/Main.cs b/Cesium.Compiler/Main.cs index f7a6d78b..7ee12bf1 100644 --- a/Cesium.Compiler/Main.cs +++ b/Cesium.Compiler/Main.cs @@ -21,6 +21,7 @@ return 2; } + var targetArchitectureSet = args.TargetArchitectureSet; var targetRuntime = args.Framework switch { TargetFrameworkKind.NetFramework => TargetRuntimeDescriptor.Net48, @@ -39,7 +40,15 @@ ".dll" => ModuleKind.Dll, var o => throw new CompilationException($"Unknown file extension: {o}. \"modulekind\" is not specified.") }; - var compilationOptions = new CompilationOptions(targetRuntime, moduleKind, corelibAssembly, cesiumRuntime, defaultImportsAssembly, args.Namespace, args.GlobalClass); + var compilationOptions = new CompilationOptions( + targetRuntime, + targetArchitectureSet, + moduleKind, + corelibAssembly, + cesiumRuntime, + defaultImportsAssembly, + args.Namespace, + args.GlobalClass); return await Compilation.Compile(args.InputFilePaths, args.OutputFilePath, compilationOptions); }, _ =>