Skip to content

Commit

Permalink
(#132) Compiler: add a command line arg and an architecture set enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Nov 29, 2022
1 parent b6032a5 commit cffd690
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<Module>");
return VerifyMethods(moduleType);
}

[Theory]
Expand Down
9 changes: 7 additions & 2 deletions Cesium.CodeGen.Tests/CodeGenTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -38,10 +38,15 @@ protected static void DoesNotCompile<T>(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,
Expand Down
1 change: 1 addition & 0 deletions Cesium.CodeGen/CompilationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Cesium.CodeGen;

public record CompilationOptions(
TargetRuntimeDescriptor TargetRuntime,
TargetArchitectureSet TargetArchitectureSet,
ModuleKind ModuleKind,
string CorelibAssembly,
string CesiumRuntime,
Expand Down
20 changes: 20 additions & 0 deletions Cesium.CodeGen/TargetArchitectureSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Cesium.CodeGen;

/// <summary>Describes the set of system architectures targeted by the assembly.</summary>
public enum TargetArchitectureSet
{
/// <summary>
/// <para>Dynamic architecture.</para>
/// <para>
/// 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.
/// </para>
/// </summary>
Dynamic,

/// <summary>An architecture with 32-bit pointers. Targets ARM32 and x86 CPUs.</summary>
Bit32,

/// <summary>An architecture with 64-bit pointers. Targets ARM64 and x86-64 CPUs.</summary>
Bit64
}
4 changes: 4 additions & 0 deletions Cesium.Compiler/Arguments.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Cesium.CodeGen;
using CommandLine;
using Mono.Cecil;

Expand All @@ -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!;

Expand Down
11 changes: 10 additions & 1 deletion Cesium.Compiler/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
return 2;
}

var targetArchitectureSet = args.TargetArchitectureSet;
var targetRuntime = args.Framework switch
{
TargetFrameworkKind.NetFramework => TargetRuntimeDescriptor.Net48,
Expand All @@ -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);
},
_ =>
Expand Down

0 comments on commit cffd690

Please sign in to comment.