From 7703dbc061dea8fe554b3e397db34e7732159280 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Wed, 1 Feb 2023 14:00:05 +0100 Subject: [PATCH 1/6] Support targeting iOS platforms with ILC --- src/coreclr/tools/Common/CommandLineHelpers.cs | 4 ++++ .../Target_X64/TargetRegisterMap.cs | 1 + .../Internal/Runtime/EETypeBuilderHelpers.cs | 2 +- .../tools/Common/JitInterface/CorInfoImpl.cs | 2 +- .../Common/TypeSystem/Common/TargetDetails.cs | 11 ++++++++--- .../TypeSystem/Interop/IL/MarshalHelpers.cs | 4 ++-- .../DependencyAnalysis/ObjectWriter.cs | 18 ++++++++++++++---- .../Compiler/ExportsFileWriter.cs | 2 +- .../ILCompiler.Diagnostics/PerfMapWriter.cs | 1 + .../ILCompiler/ConfigurablePInvokePolicy.cs | 2 +- 10 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/coreclr/tools/Common/CommandLineHelpers.cs b/src/coreclr/tools/Common/CommandLineHelpers.cs index d1282b416f779..e1a1a6442db80 100644 --- a/src/coreclr/tools/Common/CommandLineHelpers.cs +++ b/src/coreclr/tools/Common/CommandLineHelpers.cs @@ -78,6 +78,10 @@ public static TargetOS GetTargetOS(string token) return TargetOS.OSX; else if (token.Equals("freebsd", StringComparison.OrdinalIgnoreCase)) return TargetOS.FreeBSD; + else if (token.Equals("ios", StringComparison.OrdinalIgnoreCase)) + return TargetOS.iOS; + else if (token.Equals("iossimulator", StringComparison.OrdinalIgnoreCase)) + return TargetOS.iOSSimulator; throw new CommandLineException($"Target OS '{token}' is not supported"); } diff --git a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs index f377441b0d486..e31b0844a4ad6 100644 --- a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs +++ b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs @@ -32,6 +32,7 @@ public TargetRegisterMap(TargetOS os) case TargetOS.Linux: case TargetOS.OSX: + // TODO: case TargetOS.iOSSimulator: case TargetOS.FreeBSD: case TargetOS.SunOS: case TargetOS.NetBSD: diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index 34aa52ae43c81..ca426c113d28c 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -113,7 +113,7 @@ mdType.Name is "WeakReference" or "WeakReference`1" && flagsEx |= (ushort)EETypeFlagsEx.HasCriticalFinalizerFlag; } - if (type.Context.Target.IsOSX && IsTrackedReferenceWithFinalizer(type)) + if (type.Context.Target.IsOSXLike && IsTrackedReferenceWithFinalizer(type)) { flagsEx |= (ushort)EETypeFlagsEx.IsTrackedReferenceWithFinalizerFlag; } diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index df7d878490116..c092df8c478c1 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -3073,7 +3073,7 @@ private void ThrowExceptionForHelper(ref CORINFO_HELPER_DESC throwHelper) public static CORINFO_OS TargetToOs(TargetDetails target) { return target.IsWindows ? CORINFO_OS.CORINFO_WINNT : - target.IsOSX ? CORINFO_OS.CORINFO_MACOS : CORINFO_OS.CORINFO_UNIX; + target.IsOSXLike ? CORINFO_OS.CORINFO_MACOS : CORINFO_OS.CORINFO_UNIX; } private void getEEInfo(ref CORINFO_EE_INFO pEEInfoOut) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs index ef77bfa5d10df..39a3c20f45a20 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs @@ -19,6 +19,8 @@ public enum TargetOS NetBSD, SunOS, WebAssembly, + iOS, + iOSSimulator } public enum TargetAbi @@ -303,13 +305,16 @@ public bool IsWindows } /// - /// Returns True if compiling for OSX + /// Returns True if compiling for OSX family of operating systems. + /// Currently including OSX, iOS and iOSSimulator /// - public bool IsOSX + public bool IsOSXLike { get { - return OperatingSystem == TargetOS.OSX; + return OperatingSystem == TargetOS.OSX || + OperatingSystem == TargetOS.iOS || + OperatingSystem == TargetOS.iOSSimulator; } } diff --git a/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs b/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs index 6402d5e535a2a..cb80143d94250 100644 --- a/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs +++ b/src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalHelpers.cs @@ -927,7 +927,7 @@ internal static MarshallerKind GetDisabledMarshallerKind( internal static bool ShouldCheckForPendingException(TargetDetails target, PInvokeMetadata metadata) { - if (!target.IsOSX) + if (!target.IsOSXLike) return false; const string ObjectiveCMsgSend = "objc_msgSend"; @@ -944,7 +944,7 @@ internal static bool ShouldCheckForPendingException(TargetDetails target, PInvok internal static int? GetObjectiveCMessageSendFunction(TargetDetails target, string pinvokeModule, string pinvokeFunction) { - if (!target.IsOSX || pinvokeModule != ObjectiveCLibrary) + if (!target.IsOSXLike || pinvokeModule != ObjectiveCLibrary) return null; #pragma warning disable CA1416 diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs index 6b728ccb3405a..120e9a81692ca 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs @@ -718,7 +718,7 @@ public void EmitCFICodes(int offset) Debug.Assert(false); } - if (_targetPlatform.OperatingSystem == TargetOS.OSX) + if (_targetPlatform.IsOSXLike) { // Emit a symbol for beginning of the frame. This is workaround for ld64 // linker bug which would produce DWARF with incorrect pcStart offsets for @@ -792,9 +792,9 @@ public void BuildSymbolDefinitionMap(ObjectNode node, ISymbolDefinitionNode[] de private void AppendExternCPrefix(Utf8StringBuilder sb) { - if (_targetPlatform.OperatingSystem == TargetOS.OSX) + if (_targetPlatform.IsOSXLike) { - // On OSX, we need to prefix an extra underscore to account for correct linkage of + // On OSX-like systems, we need to prefix an extra underscore to account for correct linkage of // extern "C" functions. sb.Append('_'); } @@ -908,7 +908,7 @@ private bool ShouldShareSymbol(ObjectNode node) if (_isSingleFileCompilation) return false; - if (_targetPlatform.OperatingSystem == TargetOS.OSX) + if (_targetPlatform.IsOSXLike) return false; if (!(node is ISymbolNode)) @@ -1250,6 +1250,16 @@ private static string GetLLVMTripleFromTarget(TargetDetails target) sys = "darwin16"; abi = "macho"; break; + case TargetOS.iOS: + vendor = "apple"; + sys = "ios11.0"; + abi = "macho"; + break; + case TargetOS.iOSSimulator: + vendor = "apple"; + sys = "ios11.0"; + abi = "simulator"; + break; case TargetOS.WebAssembly: vendor = "unknown"; sys = "unknown"; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs index 4a0929729b47f..9937ecacc986c 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs @@ -37,7 +37,7 @@ public void EmitExportedMethods() foreach (var method in _methods) streamWriter.WriteLine($" {method.GetUnmanagedCallersOnlyExportName()}"); } - else if(_context.Target.IsOSX) + else if(_context.Target.IsOSXLike) { foreach (var method in _methods) streamWriter.WriteLine($"_{method.GetUnmanagedCallersOnlyExportName()}"); diff --git a/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs b/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs index 6f1d3113c3150..2f751322b24a1 100644 --- a/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs @@ -108,6 +108,7 @@ internal record struct PerfmapTokensForTarget(PerfMapOSToken OperatingSystem, Pe private static PerfmapTokensForTarget TranslateTargetDetailsToPerfmapConstants(TargetDetails details) { + // TODO: What about ios and iossimulator PerfMapOSToken osToken = details.OperatingSystem switch { TargetOS.Unknown => PerfMapOSToken.Unknown, diff --git a/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs b/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs index f5f634c06f12d..78f218a61be47 100644 --- a/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs +++ b/src/coreclr/tools/aot/ILCompiler/ConfigurablePInvokePolicy.cs @@ -91,7 +91,7 @@ private IEnumerable ModuleNameVariations(string name) } else { - string suffix = _target.IsOSX ? ".dylib" : ".so"; + string suffix = _target.IsOSXLike ? ".dylib" : ".so"; if (name.EndsWith(suffix, StringComparison.Ordinal)) yield return name.Substring(0, name.Length - suffix.Length); From 0953262df78b92cea9aaa1d14d75f44af1b08880 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Wed, 1 Feb 2023 18:27:52 +0100 Subject: [PATCH 2/6] Simplify TargetRegisterMap on X64 architectures by special casing the Windows calling convention --- .../Target_X64/TargetRegisterMap.cs | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs index e31b0844a4ad6..e420bac44d271 100644 --- a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs +++ b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs @@ -20,31 +20,11 @@ public struct TargetRegisterMap public TargetRegisterMap(TargetOS os) { - switch (os) - { - case TargetOS.Windows: - Arg0 = Register.RCX; - Arg1 = Register.RDX; - Arg2 = Register.R8; - Arg3 = Register.R9; - Result = Register.RAX; - break; - - case TargetOS.Linux: - case TargetOS.OSX: - // TODO: case TargetOS.iOSSimulator: - case TargetOS.FreeBSD: - case TargetOS.SunOS: - case TargetOS.NetBSD: - Arg0 = Register.RDI; - Arg1 = Register.RSI; - Arg2 = Register.RDX; - Arg3 = Register.RCX; - Result = Register.RAX; - break; - default: - throw new NotImplementedException(); - } + Arg0 = os == TargetOS.Windows ? Register.RCX : Register.RDI; + Arg1 = os == TargetOS.Windows ? Register.RDX : Register.RSI; + Arg2 = os == TargetOS.Windows ? Register.R8 : Register.RDX; + Arg3 = os == TargetOS.Windows ? Register.R9 : Register.RCX; + Result = Register.RAX; } } } From d789775205a32a99f5f79d49d05d2be34fa9a825 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Thu, 2 Feb 2023 10:04:26 +0100 Subject: [PATCH 3/6] Formatting: group osx-like systems together --- src/coreclr/tools/Common/CommandLineHelpers.cs | 4 ++-- src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/tools/Common/CommandLineHelpers.cs b/src/coreclr/tools/Common/CommandLineHelpers.cs index e1a1a6442db80..a0771b736c618 100644 --- a/src/coreclr/tools/Common/CommandLineHelpers.cs +++ b/src/coreclr/tools/Common/CommandLineHelpers.cs @@ -74,10 +74,10 @@ public static TargetOS GetTargetOS(string token) return TargetOS.Windows; else if (token.Equals("linux", StringComparison.OrdinalIgnoreCase)) return TargetOS.Linux; - else if (token.Equals("osx", StringComparison.OrdinalIgnoreCase)) - return TargetOS.OSX; else if (token.Equals("freebsd", StringComparison.OrdinalIgnoreCase)) return TargetOS.FreeBSD; + else if (token.Equals("osx", StringComparison.OrdinalIgnoreCase)) + return TargetOS.OSX; else if (token.Equals("ios", StringComparison.OrdinalIgnoreCase)) return TargetOS.iOS; else if (token.Equals("iossimulator", StringComparison.OrdinalIgnoreCase)) diff --git a/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs b/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs index bfbb1fed9c509..235b7443e5548 100644 --- a/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs +++ b/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs @@ -306,7 +306,7 @@ public static IEnumerable> GetExtendedHelp(HelpContext _) "considered to be input files. If no input files begin with '--' then this option is not necessary.\n"); string[] ValidArchitectures = new string[] { "arm", "arm64", "x86", "x64" }; - string[] ValidOS = new string[] { "windows", "linux", "osx", "freebsd" }; + string[] ValidOS = new string[] { "windows", "linux", "freebsd", "osx", "ios", "iossimulator" }; Console.WriteLine("Valid switches for {0} are: '{1}'. The default value is '{2}'\n", "--targetos", string.Join("', '", ValidOS), Helpers.GetTargetOS(null).ToString().ToLowerInvariant()); From b5e7d1ae304f2383cc7d7692f2f91565833e0266 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Thu, 2 Feb 2023 10:08:42 +0100 Subject: [PATCH 4/6] Formatting: use switch for TargetRegisterMap instead --- .../Target_X64/TargetRegisterMap.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs index e420bac44d271..3d87424ae9863 100644 --- a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs +++ b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_X64/TargetRegisterMap.cs @@ -20,11 +20,23 @@ public struct TargetRegisterMap public TargetRegisterMap(TargetOS os) { - Arg0 = os == TargetOS.Windows ? Register.RCX : Register.RDI; - Arg1 = os == TargetOS.Windows ? Register.RDX : Register.RSI; - Arg2 = os == TargetOS.Windows ? Register.R8 : Register.RDX; - Arg3 = os == TargetOS.Windows ? Register.R9 : Register.RCX; - Result = Register.RAX; + switch (os) + { + case TargetOS.Windows: + Arg0 = Register.RCX; + Arg1 = Register.RDX; + Arg2 = Register.R8; + Arg3 = Register.R9; + Result = Register.RAX; + break; + default: + Arg0 = Register.RDI; + Arg1 = Register.RSI; + Arg2 = Register.RDX; + Arg3 = Register.RCX; + Result = Register.RAX; + break; + } } } } From d4b793005740976c3c16d3cbc04afd84c939d542 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Thu, 2 Feb 2023 10:11:38 +0100 Subject: [PATCH 5/6] Removing the comment as it is unlikely we will support targeting iOS with RyuJIT --- src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs b/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs index 2f751322b24a1..6f1d3113c3150 100644 --- a/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Diagnostics/PerfMapWriter.cs @@ -108,7 +108,6 @@ internal record struct PerfmapTokensForTarget(PerfMapOSToken OperatingSystem, Pe private static PerfmapTokensForTarget TranslateTargetDetailsToPerfmapConstants(TargetDetails details) { - // TODO: What about ios and iossimulator PerfMapOSToken osToken = details.OperatingSystem switch { TargetOS.Unknown => PerfMapOSToken.Unknown, From 98e9cdd6d48ae0cca5edd0ad16ce68c26621956e Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Thu, 2 Feb 2023 17:09:31 +0100 Subject: [PATCH 6/6] Formatting: introduce switch expression instead of if..else if clauses --- .../tools/Common/CommandLineHelpers.cs | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/coreclr/tools/Common/CommandLineHelpers.cs b/src/coreclr/tools/Common/CommandLineHelpers.cs index a0771b736c618..39161e2c753b0 100644 --- a/src/coreclr/tools/Common/CommandLineHelpers.cs +++ b/src/coreclr/tools/Common/CommandLineHelpers.cs @@ -69,21 +69,19 @@ public static TargetOS GetTargetOS(string token) throw new NotImplementedException(); } - - if (token.Equals("windows", StringComparison.OrdinalIgnoreCase)) - return TargetOS.Windows; - else if (token.Equals("linux", StringComparison.OrdinalIgnoreCase)) - return TargetOS.Linux; - else if (token.Equals("freebsd", StringComparison.OrdinalIgnoreCase)) - return TargetOS.FreeBSD; - else if (token.Equals("osx", StringComparison.OrdinalIgnoreCase)) - return TargetOS.OSX; - else if (token.Equals("ios", StringComparison.OrdinalIgnoreCase)) - return TargetOS.iOS; - else if (token.Equals("iossimulator", StringComparison.OrdinalIgnoreCase)) - return TargetOS.iOSSimulator; - - throw new CommandLineException($"Target OS '{token}' is not supported"); + else + { + return token.ToLowerInvariant() switch + { + "windows" => TargetOS.Windows, + "linux" => TargetOS.Linux, + "freebsd" => TargetOS.FreeBSD, + "osx" => TargetOS.OSX, + "ios" => TargetOS.iOS, + "iossimulator" => TargetOS.iOSSimulator, + _ => throw new CommandLineException($"Target OS '{token}' is not supported") + }; + } } public static TargetArchitecture GetTargetArchitecture(string token) @@ -100,19 +98,18 @@ public static TargetArchitecture GetTargetArchitecture(string token) _ => throw new NotImplementedException() }; } - - if (token.Equals("x86", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.X86; - else if (token.Equals("x64", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.X64; - else if (token.Equals("arm", StringComparison.OrdinalIgnoreCase) || token.Equals("armel", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.ARM; - else if (token.Equals("arm64", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.ARM64; - else if (token.Equals("loongarch64", StringComparison.OrdinalIgnoreCase)) - return TargetArchitecture.LoongArch64; - - throw new CommandLineException($"Target architecture '{token}' is not supported"); + else + { + return token.ToLowerInvariant() switch + { + "x86" => TargetArchitecture.X86, + "x64" => TargetArchitecture.X64, + "arm" or "armel" => TargetArchitecture.ARM, + "arm64" => TargetArchitecture.ARM64, + "loongarch64" => TargetArchitecture.LoongArch64, + _ => throw new CommandLineException($"Target architecture '{token}' is not supported") + }; + } } public static void MakeReproPackage(string makeReproPath, string outputFilePath, string[] args, ParseResult res, IEnumerable inputOptions, IEnumerable outputOptions = null)