├── .gitignore ├── LICENSE ├── README.md ├── RTCLI.AOTCompiler.Core ├── CodeTextStorage.cs ├── CodeTextWriter.cs ├── Common │ └── Utilities.cs ├── ILConverter │ ├── CXX │ │ ├── CallConvertersCXX.cs │ │ ├── CompareConvertersCXX.cs │ │ ├── FlowControlConvertersCXX.cs │ │ ├── ILConverterCXX.cs │ │ ├── ILConvertersCXX.cs │ │ ├── LdargConvertersCXX.cs │ │ ├── LdcConverters.cs │ │ ├── LdelemConvertersCXX.cs │ │ ├── LdindConvertersCXX.cs │ │ ├── LdlocConvertersCXX.cs │ │ ├── StargConvertersCXX.cs │ │ ├── StelemConvertersCXX.cs │ │ ├── StindConvertersCXX.cs │ │ └── StlocConvertersCXX.cs │ └── ILConverter.cs ├── Metadata │ ├── AssemblyInformation.cs │ ├── AssemblyResolver.cs │ ├── FieldInformation.cs │ ├── InstructionInformation.cs │ ├── MemberInformation.cs │ ├── MetadataContext.cs │ ├── MetadataInformationBase.cs │ ├── MethodInformation.cs │ ├── ModuleInformation.cs │ ├── Operand.cs │ ├── ParameterInformation.cs │ ├── PropertyInformation.cs │ ├── TypeInformation.cs │ └── VariableInformation.cs ├── Options.cs ├── RTCLI.AOTCompiler.Core.csproj ├── TranslateContext.cs └── Translators │ ├── CXX │ ├── CXXMethodTranslateContext.cs │ ├── CXXTranslator.cs │ └── InformationToCXX.cs │ ├── MetadataSerializer.cs │ └── MethodTranslateContext.cs ├── RTCLI.AOTCompiler.sln ├── RTCLI.AOTCompiler ├── Dispatcher.cs ├── Program.cs ├── Properties │ └── launchSettings.json └── RTCLI.AOTCompiler.csproj ├── RTCLI.AOTCompiler3.Core ├── CXX │ ├── FieldInformationCXX.cs │ ├── MethodInformationCXX.cs │ ├── MethodTranslateContextCXX.cs │ ├── ParamInformationCXX.cs │ ├── TypeInformationCXX.cs │ └── VarInformationCXX.cs ├── CodeTextStorage.cs ├── CodeTextWriter.cs ├── Constants.cs ├── ILConverter │ ├── CXX │ │ ├── CallConvertersCXX.cs │ │ ├── CompareConvertersCXX.cs │ │ ├── FlowControlConvertersCXX.cs │ │ ├── ILConverterCXX.cs │ │ ├── ILConvertersCXX.cs │ │ ├── LdargConvertersCXX.cs │ │ ├── LdcConverters.cs │ │ ├── LdelemConvertersCXX.cs │ │ ├── LdindConvertersCXX.cs │ │ ├── LdlocConvertersCXX.cs │ │ ├── StargConvertersCXX.cs │ │ ├── StelemConvertersCXX.cs │ │ ├── StindConvertersCXX.cs │ │ └── StlocConvertersCXX.cs │ └── ILConverter.cs ├── Meta │ ├── AssemblyRepo.cs │ └── RTCLIAssembly.cs ├── Options.cs ├── RTCLI.AOTCompiler3.Core.csproj ├── Theory │ ├── CXXXX.cs │ ├── HXXXX.cs │ └── SXXXX.cs ├── Translators │ ├── CXX │ │ ├── CXXHeaderRules.cs │ │ ├── CXXHeaderTranslator.cs │ │ ├── CXXPathHelper.cs │ │ ├── CXXScopeDisposer.cs │ │ ├── CXXSourceRules.cs │ │ └── CXXSourceTranslator.cs │ └── IRTCLITranslator.cs └── Utilities.cs ├── RTCLI.AOTCompiler3 ├── Dispatcher.cs ├── Program.cs ├── Properties │ └── launchSettings.json └── RTCLI.AOTCompiler3.csproj ├── RTCLI.CXXTest ├── .gitignore ├── .vscode │ └── settings.json ├── CMakeLists.txt ├── gen_vs2017.bat ├── gen_vs2019.bat └── gen_xcode.sh ├── RTCLI.Standard ├── mscorlib.dll ├── netstandard.dll └── netstandard_2_1 ├── RTCLI.TestCase.Reference ├── AsssemblyReference.cs └── TestCase.Reference.csproj ├── RTCLI.TestCase ├── TestArray.cs ├── TestCase - Backup.csproj ├── TestCase.csproj ├── TestCrossFile.cs ├── TestDelegate.cs ├── TestEnum.cs ├── TestGeneric.cs ├── TestInnerClass.cs ├── TestInterface.cs ├── TestReference.cs ├── TestString.cs ├── TestStruct.cs ├── TestSystem.cs ├── TestThrow.cs └── netstandard.dll └── TestProgram ├── Program.cs └── TestProgram.csproj /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Team-RTCLI 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/CodeTextStorage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace RTCLI.AOTCompiler 8 | { 9 | public class CodeTextStorage 10 | { 11 | private readonly TextWriter logw; 12 | public readonly string BasePath; 13 | private readonly Queue scopeNames = new Queue(); 14 | 15 | public CodeTextStorage(TextWriter logw, string basePath, string indent) 16 | { 17 | this.logw = logw; 18 | this.BasePath = basePath; 19 | } 20 | 21 | private string getScopePath() 22 | { 23 | string fullDirName = BasePath; 24 | foreach (var scope in scopeNames) 25 | { 26 | fullDirName = Path.Combine(fullDirName, scope); 27 | } 28 | return fullDirName; 29 | } 30 | 31 | public IDisposable EnterScope(string scopeName, bool splitScope = true) 32 | { 33 | scopeNames.Enqueue(splitScope ? Utilities.GetCXXLanguageScopedPath(scopeName) : scopeName); 34 | if (!Directory.Exists(getScopePath())) 35 | { 36 | Directory.CreateDirectory(getScopePath()); 37 | } 38 | return new ScopeDisposer(this); 39 | } 40 | 41 | public CodeTextWriter Wirter(string FileName) 42 | { 43 | return new CodeTextWriter(Path.Combine(getScopePath(), FileName)); 44 | } 45 | 46 | private sealed class ScopeDisposer : IDisposable 47 | { 48 | private CodeTextStorage parent; 49 | public ScopeDisposer(CodeTextStorage parent) 50 | { 51 | this.parent = parent; 52 | } 53 | 54 | public void Dispose() 55 | { 56 | if (parent != null) 57 | { 58 | parent.scopeNames.Dequeue(); 59 | parent = null; 60 | } 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/CodeTextWriter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Text; 5 | 6 | namespace RTCLI.AOTCompiler 7 | { 8 | public class CodeTextWriter 9 | { 10 | public CodeTextWriter(string relatedPath) 11 | { 12 | sw = new StreamWriter(relatedPath, false, Encoding.UTF8); 13 | } 14 | 15 | public CodeTextWriter WriteLine(string toWrite) 16 | { 17 | sw.WriteLine(indentString + toWrite); 18 | return this; 19 | } 20 | 21 | public CodeTextWriter indent() 22 | { 23 | indentString += "\t"; 24 | return this; 25 | } 26 | public CodeTextWriter unindent() 27 | { 28 | if(indentString.Length > 0) 29 | indentString = indentString.Remove(indentString.Length - 1); 30 | return this; 31 | } 32 | 33 | public void Flush() 34 | { 35 | sw.Flush(); 36 | } 37 | 38 | private String indentString = ""; 39 | private StreamWriter sw = null; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Common/Utilities.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace RTCLI.AOTCompiler 7 | { 8 | public static class Utilities 9 | { 10 | public static string GetCXXLanguageScopedPath(IEnumerable scopeNames) => 11 | string.Join("/", scopeNames.SelectMany(sn => sn.Split('.'))); 12 | 13 | public static string GetCXXLanguageScopedPath(params string[] scopeNames) => 14 | GetCXXLanguageScopedPath((IEnumerable)scopeNames); 15 | 16 | public static string GetCXXValidTokenString(string raw_token) => 17 | raw_token.Replace('<', '_').Replace('>', '_'); 18 | 19 | public static string GetLabel(this Instruction instruction) 20 | { 21 | return instruction.ToString().Split(":")[0]; 22 | } 23 | 24 | public static string HoldEscape(this string source) 25 | { 26 | return source.Replace("\\", "\\\\").Replace("\n", "\\n") 27 | .Replace("\'", "\\'").Replace("\"", "\\\"") 28 | .Replace("\0", "\\0").Replace("\a", "\\a") 29 | .Replace("\b", "\\b").Replace("\f", "\\f") 30 | .Replace("\r", "\\r").Replace("\t", "\\t") 31 | .Replace("\v", "\\v"); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/CallConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | using System.Linq; 9 | 10 | namespace RTCLI.AOTCompiler.ILConverters 11 | { 12 | public class MethodCallConvert 13 | { 14 | public static string GetMethodOwner(MethodReference mtd, MethodTranslateContext methodContext) 15 | { 16 | var type = methodContext.MetadataContext.GetTypeInformation(mtd.DeclaringType); 17 | return type.CXXTypeName; 18 | } 19 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, bool Virt) 20 | { 21 | var mtd = (instruction.Operand as MethodReference); 22 | string args = ""; 23 | List argList = new List(); 24 | for (int i = 1; i <= mtd.Parameters.Count; i++) 25 | argList.Add((methodContext as CXXMethodTranslateContext).CmptStackPopObject); 26 | argList.Reverse(); 27 | args = string.Join(',', argList); 28 | if(mtd.FullName.StartsWith("!!0")) 29 | { 30 | var garg = (mtd as GenericInstanceMethod).GenericArguments[0]; 31 | var type = methodContext.MetadataContext.GetTypeInformation(garg); 32 | return $"{type.CXXTypeName}& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = \n\t\tRTCLI::new_object<{type.CXXTypeName}>({args});"; 33 | } 34 | 35 | var methodInformation = mtd.GetMetaInformation(methodContext.MetadataContext); 36 | string genericArgs = ""; 37 | if (mtd is GenericInstanceMethod gmtd) 38 | genericArgs = $"<{string.Join(',', gmtd.GenericArguments.Select(a => methodContext.MetadataContext.GetTypeInformation(a).CXXTypeName))}>"; 39 | if (!methodInformation.IsStatic) 40 | { 41 | string caller = $"(({GetMethodOwner(mtd, methodContext)}&)" // Caster: ((DeclaringType&) 42 | + $"{(methodContext as CXXMethodTranslateContext).CmptStackPopObject})."; // Caller: caller) 43 | string callBody = 44 | $"{caller}" + 45 | (Virt ? "" : $"{GetMethodOwner(mtd, methodContext)}::") + 46 | $"{methodInformation.CXXMethodNameShort + genericArgs}({args});"; // Method Call body. 47 | return (mtd.ReturnType.FullName != "System.Void" ? $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = " : "") 48 | + callBody; 49 | } 50 | if(methodInformation.IsStatic) 51 | { 52 | var type = methodContext.MetadataContext.GetTypeInformation(mtd.DeclaringType); 53 | string callBody = $"{methodInformation.CXXMethodCallName(type) + genericArgs}({args});"; // Method Call body. 54 | return type.CallStaticConstructor(methodContext) + 55 | (mtd.ReturnType.FullName != "System.Void" ? $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = " : "") 56 | + callBody; 57 | } 58 | return "ERROR_METHOD_NAME"; 59 | } 60 | } 61 | 62 | public class CallConverterCXX : ICXXILConverter 63 | { 64 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 65 | => MethodCallConvert.Convert(instruction, methodContext, false); 66 | public OpCode TargetOpCode() => OpCodes.Call; 67 | } 68 | public class CallVirtConverterCXX : ICXXILConverter 69 | { 70 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 71 | => MethodCallConvert.Convert(instruction, methodContext, true); 72 | public OpCode TargetOpCode() => OpCodes.Callvirt; 73 | } 74 | public class TailcallConverterCXX : ICXXILConverter 75 | { 76 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 77 | => ""; 78 | public OpCode TargetOpCode() => OpCodes.Tail; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/CompareConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class CgtConverterCXX : ICXXILConverter 12 | { 13 | public OpCode TargetOpCode() => OpCodes.Cgt; 14 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 15 | { 16 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 17 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 18 | return $"RTCLI::i32 {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 19 | $" = RTCLI::Cgt({op1}, {op2});"; 20 | } 21 | } 22 | 23 | public class Cgt_UnConverterCXX : ICXXILConverter 24 | { 25 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 26 | { 27 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 28 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 29 | return $"RTCLI::i32 {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 30 | $" = RTCLI::Cgt_Un({op1}, {op2});"; 31 | } 32 | 33 | public OpCode TargetOpCode() => OpCodes.Cgt_Un; 34 | } 35 | 36 | public class CeqConverterCXX : ICXXILConverter 37 | { 38 | public OpCode TargetOpCode() => OpCodes.Ceq; 39 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 40 | { 41 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 42 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 43 | return $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 44 | $" = RTCLI::Ceq({op1}, {op2});"; 45 | } 46 | } 47 | 48 | public class CltConverterCXX : ICXXILConverter 49 | { 50 | public OpCode TargetOpCode() => OpCodes.Clt; 51 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 52 | { 53 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 54 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 55 | return $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 56 | $" = RTCLI::Clt({op1}, {op2});"; 57 | } 58 | } 59 | 60 | public class Clt_UnConverterCXX : ICXXILConverter 61 | { 62 | public OpCode TargetOpCode() => OpCodes.Clt_Un; 63 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 64 | { 65 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 66 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 67 | return $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 68 | $" = RTCLI::Clt_Un({op1}, {op2});"; 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/ILConverterCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System.Dynamic; 3 | using RTCLI.AOTCompiler.Translators; 4 | 5 | namespace RTCLI.AOTCompiler.ILConverters 6 | { 7 | public interface ICXXILConverter : IILConverter 8 | { 9 | string Note(Instruction instruction, MethodTranslateContext methodContext) => "// " + instruction.ToString().HoldEscape(); 10 | } 11 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/LdargConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class LdargConvert 12 | { 13 | public static string Convert(MethodTranslateContext methodContext, int index) 14 | { 15 | if (!methodContext.MethodInfo.IsStatic) 16 | index -= 1; 17 | if(index < 0) 18 | return $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = *this;"; 19 | else 20 | return $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = {methodContext.MethodInfo.Parameters[index].Name};"; 21 | } 22 | } 23 | 24 | public class LdnullConverterCXX : ICXXILConverter 25 | { 26 | public OpCode TargetOpCode() => OpCodes.Ldnull; 27 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 28 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = RTCLI::null;"; 29 | } 30 | public class Ldarg_0ConverterCXX : ICXXILConverter 31 | { 32 | public OpCode TargetOpCode() => OpCodes.Ldarg_0; 33 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 34 | => LdargConvert.Convert(methodContext, 0); 35 | } 36 | public class Ldarg_1ConverterCXX : ICXXILConverter 37 | { 38 | public OpCode TargetOpCode() => OpCodes.Ldarg_1; 39 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 40 | => LdargConvert.Convert(methodContext, 1); 41 | } 42 | public class Ldarg_2ConverterCXX : ICXXILConverter 43 | { 44 | public OpCode TargetOpCode() => OpCodes.Ldarg_2; 45 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 46 | => LdargConvert.Convert(methodContext, 2); 47 | } 48 | public class Ldarg_3ConverterCXX : ICXXILConverter 49 | { 50 | public OpCode TargetOpCode() => OpCodes.Ldarg_3; 51 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 52 | => LdargConvert.Convert(methodContext, 3); 53 | } 54 | public class LdargConverterCXX : ICXXILConverter 55 | { 56 | public OpCode TargetOpCode() => OpCodes.Ldarg; 57 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 58 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = {(instruction.Operand as ParameterDefinition).Name};"; 59 | } 60 | public class Ldarg_SConverterCXX : ICXXILConverter 61 | { 62 | public OpCode TargetOpCode() => OpCodes.Ldarg_S; 63 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 64 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = {(instruction.Operand as ParameterDefinition).Name};"; 65 | } 66 | public class LdargaConverterCXX : ICXXILConverter 67 | { 68 | public OpCode TargetOpCode() => OpCodes.Ldarga; 69 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 70 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = RTCLI_ADDRESSOF({(instruction.Operand as ParameterDefinition).Name});"; 71 | } 72 | public class Ldarga_SConverterCXX : ICXXILConverter 73 | { 74 | public OpCode TargetOpCode() => OpCodes.Ldarga_S; 75 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 76 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = RTCLI_ADDRESSOF({(instruction.Operand as ParameterDefinition).Name});"; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/LdcConverters.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class Ldc_Convert 12 | { 13 | public static string I4Convert(Instruction instruction, MethodTranslateContext methodContext, Int32 val) 14 | { 15 | CXXMethodTranslateContext ctx = methodContext as CXXMethodTranslateContext; 16 | return $"RTCLI::System::Int32 {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Int32"}>({val});"; 17 | } 18 | public static string I8Convert(Instruction instruction, MethodTranslateContext methodContext, Int64 val) 19 | { 20 | CXXMethodTranslateContext ctx = methodContext as CXXMethodTranslateContext; 21 | return $"RTCLI::System::Int64 {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Int64"}>({val});"; 22 | } 23 | public static string R4Convert(Instruction instruction, MethodTranslateContext methodContext, Single val) 24 | { 25 | CXXMethodTranslateContext ctx = methodContext as CXXMethodTranslateContext; 26 | return $"RTCLI::System::Single {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Single"}>({val});"; 27 | } 28 | public static string R8Convert(Instruction instruction, MethodTranslateContext methodContext, Double val) 29 | { 30 | CXXMethodTranslateContext ctx = methodContext as CXXMethodTranslateContext; 31 | return $"RTCLI::System::Double {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Double"}>({val});"; 32 | } 33 | } 34 | 35 | public class Ldc_I4ConverterCXX : ICXXILConverter 36 | { 37 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 38 | => Ldc_Convert.I4Convert(instruction, methodContext, Int32.Parse(instruction.Operand.ToString())); 39 | 40 | public OpCode TargetOpCode() => OpCodes.Ldc_I4; 41 | } 42 | public class Ldc_I4_0ConverterCXX : ICXXILConverter 43 | { 44 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 45 | => Ldc_Convert.I4Convert(instruction, methodContext, 0); 46 | 47 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_0; 48 | } 49 | public class Ldc_I4_SConverterCXX : ICXXILConverter 50 | { 51 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 52 | => Ldc_Convert.I4Convert(instruction, methodContext, Int32.Parse(instruction.Operand.ToString())); 53 | 54 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_S; 55 | } 56 | public class Ldc_I4_1ConverterCXX : ICXXILConverter 57 | { 58 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 59 | => Ldc_Convert.I4Convert(instruction, methodContext, 1); 60 | 61 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_1; 62 | } 63 | public class Ldc_I4_2ConverterCXX : ICXXILConverter 64 | { 65 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 66 | => Ldc_Convert.I4Convert(instruction, methodContext, 2); 67 | 68 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_2; 69 | } 70 | public class Ldc_I4_3ConverterCXX : ICXXILConverter 71 | { 72 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 73 | => Ldc_Convert.I4Convert(instruction, methodContext, 3); 74 | 75 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_3; 76 | } 77 | public class Ldc_I4_4ConverterCXX : ICXXILConverter 78 | { 79 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 80 | => Ldc_Convert.I4Convert(instruction, methodContext, 4); 81 | 82 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_4; 83 | } 84 | public class Ldc_I4_5ConverterCXX : ICXXILConverter 85 | { 86 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 87 | => Ldc_Convert.I4Convert(instruction, methodContext, 5); 88 | 89 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_5; 90 | } 91 | public class Ldc_I4_6ConverterCXX : ICXXILConverter 92 | { 93 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 94 | => Ldc_Convert.I4Convert(instruction, methodContext, 6); 95 | 96 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_6; 97 | } 98 | public class Ldc_I4_7ConverterCXX : ICXXILConverter 99 | { 100 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 101 | => Ldc_Convert.I4Convert(instruction, methodContext, 7); 102 | 103 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_7; 104 | } 105 | public class Ldc_I4_8ConverterCXX : ICXXILConverter 106 | { 107 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 108 | => Ldc_Convert.I4Convert(instruction, methodContext, 8); 109 | 110 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_8; 111 | } 112 | public class Ldc_I4_M1ConverterCXX : ICXXILConverter 113 | { 114 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 115 | => Ldc_Convert.I4Convert(instruction, methodContext, -1); 116 | 117 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_M1; 118 | } 119 | 120 | public class Ldc_I8ConverterCXX : ICXXILConverter 121 | { 122 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 123 | => Ldc_Convert.I8Convert(instruction, methodContext, Int64.Parse(instruction.Operand.ToString())); 124 | 125 | public OpCode TargetOpCode() => OpCodes.Ldc_I8; 126 | } 127 | public class Ldc_R4ConverterCXX : ICXXILConverter 128 | { 129 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 130 | => Ldc_Convert.R4Convert(instruction, methodContext, Single.Parse(instruction.Operand.ToString())); 131 | 132 | public OpCode TargetOpCode() => OpCodes.Ldc_R4; 133 | } 134 | public class Ldc_R8ConverterCXX : ICXXILConverter 135 | { 136 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 137 | => Ldc_Convert.R8Convert(instruction, methodContext, Double.Parse(instruction.Operand.ToString())); 138 | 139 | public OpCode TargetOpCode() => OpCodes.Ldc_R8; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/LdelemConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class LdelemConvert 12 | { 13 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, string type) 14 | { 15 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 16 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 17 | return $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 18 | $" = RTCLI::ArrayGet<{type}>({op1}, {op2});"; 19 | } 20 | } 21 | 22 | public class LdelemConverterCXX : ICXXILConverter 23 | { 24 | public OpCode TargetOpCode() => OpCodes.Ldelem_Any; 25 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 26 | { 27 | var typeReference = instruction.Operand as TypeReference; 28 | TypeInformation typeInformation = methodContext.TranslateContext.MetadataContext.GetTypeInformation(typeReference); 29 | return LdelemConvert.Convert(instruction, methodContext, typeInformation.CXXTypeName); 30 | } 31 | } 32 | public class Ldelem_RefConverterCXX : ICXXILConverter 33 | { 34 | public OpCode TargetOpCode() => OpCodes.Ldelem_Ref; 35 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 36 | => LdelemConvert.Convert(instruction, methodContext, ""); 37 | } 38 | 39 | public class Ldelem_IConverterCXX : ICXXILConverter 40 | { 41 | public OpCode TargetOpCode() => OpCodes.Ldelem_I; 42 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 43 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::NativeInt"); 44 | } 45 | public class Ldelem_I1ConverterCXX : ICXXILConverter 46 | { 47 | public OpCode TargetOpCode() => OpCodes.Ldelem_I1; 48 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 49 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int8"); 50 | } 51 | public class Ldelem_I2ConverterCXX : ICXXILConverter 52 | { 53 | public OpCode TargetOpCode() => OpCodes.Ldelem_I2; 54 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 55 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int16"); 56 | } 57 | public class Ldelem_I4ConverterCXX : ICXXILConverter 58 | { 59 | public OpCode TargetOpCode() => OpCodes.Ldelem_I4; 60 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 61 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int32"); 62 | } 63 | public class Ldelem_I8ConverterCXX : ICXXILConverter 64 | { 65 | public OpCode TargetOpCode() => OpCodes.Ldelem_I8; 66 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 67 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int64"); 68 | } 69 | 70 | public class Ldelem_R4ConverterCXX : ICXXILConverter 71 | { 72 | public OpCode TargetOpCode() => OpCodes.Ldelem_R4; 73 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 74 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Single"); 75 | } 76 | public class Ldelem_R8ConverterCXX : ICXXILConverter 77 | { 78 | public OpCode TargetOpCode() => OpCodes.Ldelem_R8; 79 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 80 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Double"); 81 | } 82 | 83 | public class Ldelem_U1ConverterCXX : ICXXILConverter 84 | { 85 | public OpCode TargetOpCode() => OpCodes.Ldelem_U1; 86 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 87 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::UInt8"); 88 | } 89 | public class Ldelem_U2ConverterCXX : ICXXILConverter 90 | { 91 | public OpCode TargetOpCode() => OpCodes.Ldelem_U2; 92 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 93 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::UInt16"); 94 | } 95 | public class Ldelem_U4ConverterCXX : ICXXILConverter 96 | { 97 | public OpCode TargetOpCode() => OpCodes.Ldelem_U4; 98 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 99 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::UInt32"); 100 | } 101 | 102 | 103 | public class LdelemaConverterCXX : ICXXILConverter 104 | { 105 | public OpCode TargetOpCode() => OpCodes.Ldelema; 106 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 107 | { 108 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 109 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 110 | return $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject}" + 111 | $" = RTCLI::ArrayGet_Addr({op1}, {op2});"; 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/LdindConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class LdindConvert 12 | { 13 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, string type) 14 | { 15 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 16 | return $"auto {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = RTCLI::Deref<{type}>({op1});"; 17 | } 18 | } 19 | 20 | public class Ldind_RefConverterCXX : ICXXILConverter 21 | { 22 | public OpCode TargetOpCode() => OpCodes.Ldind_Ref; 23 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 24 | => LdindConvert.Convert(instruction, methodContext, ""); 25 | } 26 | 27 | public class Ldind_IConverterCXX : ICXXILConverter 28 | { 29 | public OpCode TargetOpCode() => OpCodes.Ldind_I; 30 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 31 | => LdindConvert.Convert(instruction, methodContext, "int"); 32 | } 33 | public class Ldind_I1ConverterCXX : ICXXILConverter 34 | { 35 | public OpCode TargetOpCode() => OpCodes.Ldind_I1; 36 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 37 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i8"); 38 | } 39 | public class Ldind_I2ConverterCXX : ICXXILConverter 40 | { 41 | public OpCode TargetOpCode() => OpCodes.Ldind_I2; 42 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 43 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i16"); 44 | } 45 | public class Ldind_I4ConverterCXX : ICXXILConverter 46 | { 47 | public OpCode TargetOpCode() => OpCodes.Ldind_I4; 48 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 49 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i32"); 50 | } 51 | public class Ldind_I8ConverterCXX : ICXXILConverter 52 | { 53 | public OpCode TargetOpCode() => OpCodes.Ldind_I8; 54 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 55 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i64"); 56 | } 57 | 58 | public class Ldind_R4ConverterCXX : ICXXILConverter 59 | { 60 | public OpCode TargetOpCode() => OpCodes.Ldind_R4; 61 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 62 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::f32"); 63 | } 64 | public class Ldind_R8ConverterCXX : ICXXILConverter 65 | { 66 | public OpCode TargetOpCode() => OpCodes.Ldind_R8; 67 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 68 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::f64"); 69 | } 70 | 71 | public class Ldind_U1ConverterCXX : ICXXILConverter 72 | { 73 | public OpCode TargetOpCode() => OpCodes.Ldind_U1; 74 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 75 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::u8"); 76 | } 77 | public class Ldind_U2ConverterCXX : ICXXILConverter 78 | { 79 | public OpCode TargetOpCode() => OpCodes.Ldind_U2; 80 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 81 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::u16"); 82 | } 83 | public class Ldind_U4ConverterCXX : ICXXILConverter 84 | { 85 | public OpCode TargetOpCode() => OpCodes.Ldind_U4; 86 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 87 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::u32"); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/LdlocConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | using System.Reflection; 9 | 10 | namespace RTCLI.AOTCompiler.ILConverters 11 | { 12 | public class LdlocOnvert 13 | { 14 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, int index) 15 | { 16 | var type = methodContext.MethodInfo.LocalVariables[index].Type; 17 | return $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = v{index}{(type.IsValueType ? "" : ".Get()")};"; 18 | } 19 | } 20 | 21 | public class Ldloc_0ConverterCXX : ICXXILConverter 22 | { 23 | public OpCode TargetOpCode() => OpCodes.Ldloc_0; 24 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 25 | => LdlocOnvert.Convert(instruction, methodContext, 0); 26 | } 27 | 28 | public class Ldloc_1ConverterCXX : ICXXILConverter 29 | { 30 | public OpCode TargetOpCode() => OpCodes.Ldloc_1; 31 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 32 | => LdlocOnvert.Convert(instruction, methodContext, 1); 33 | } 34 | public class Ldloc_2ConverterCXX : ICXXILConverter 35 | { 36 | public OpCode TargetOpCode() => OpCodes.Ldloc_2; 37 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 38 | => LdlocOnvert.Convert(instruction, methodContext, 2); 39 | } 40 | public class Ldloc_3ConverterCXX : ICXXILConverter 41 | { 42 | public OpCode TargetOpCode() => OpCodes.Ldloc_3; 43 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 44 | => LdlocOnvert.Convert(instruction, methodContext, 3); 45 | } 46 | public class LdlocConverterCXX : ICXXILConverter 47 | { 48 | public OpCode TargetOpCode() => OpCodes.Ldloc; 49 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 50 | => LdlocOnvert.Convert(instruction, methodContext, (instruction.Operand as VariableDefinition).Index); 51 | } 52 | public class Ldloc_SConverterCXX : ICXXILConverter 53 | { 54 | public OpCode TargetOpCode() => OpCodes.Ldloc_S; 55 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 56 | => LdlocOnvert.Convert(instruction, methodContext, (instruction.Operand as VariableDefinition).Index); 57 | } 58 | public class LdlocaConverterCXX : ICXXILConverter 59 | { 60 | public OpCode TargetOpCode() => OpCodes.Ldloca; 61 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 62 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = RTCLI_ADDRESSOF(v{(instruction.Operand as VariableDefinition).Index});"; 63 | } 64 | public class Ldloca_SConverterCXX : ICXXILConverter 65 | { 66 | public OpCode TargetOpCode() => OpCodes.Ldloca_S; 67 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 68 | => $"auto& {(methodContext as CXXMethodTranslateContext).CmptStackPushObject} = RTCLI_ADDRESSOF(v{(instruction.Operand as VariableDefinition).Index});"; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/StargConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class Starg_ConverterCXX : ICXXILConverter 12 | { 13 | public OpCode TargetOpCode() => OpCodes.Starg; 14 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 15 | => $"{methodContext.MethodInfo.Parameters[(instruction.Operand as ParameterDefinition).Index].Name} = {(methodContext as CXXMethodTranslateContext).CmptStackPopObject};"; 16 | } 17 | public class Starg_SConverterCXX : ICXXILConverter 18 | { 19 | public OpCode TargetOpCode() => OpCodes.Starg_S; 20 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 21 | => $"{methodContext.MethodInfo.Parameters[(instruction.Operand as ParameterDefinition).Index].Name} = {(methodContext as CXXMethodTranslateContext).CmptStackPopObject};"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/StelemConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class StelemConvert 12 | { 13 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, string type) 14 | { 15 | var op3 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 16 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 17 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 18 | return $"RTCLI::ArraySet<{type}>({op1}, {op2}, {op3});"; 19 | } 20 | } 21 | 22 | public class StelemConverterCXX : ICXXILConverter 23 | { 24 | public OpCode TargetOpCode() => OpCodes.Stelem_Any; 25 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 26 | { 27 | var typeReference = instruction.Operand as TypeReference; 28 | TypeInformation typeInformation = methodContext.TranslateContext.MetadataContext.GetTypeInformation(typeReference); 29 | return StelemConvert.Convert(instruction, methodContext, typeInformation.CXXTypeName); 30 | } 31 | } 32 | public class Stelem_RefConverterCXX : ICXXILConverter 33 | { 34 | public OpCode TargetOpCode() => OpCodes.Stelem_Ref; 35 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 36 | => StelemConvert.Convert(instruction, methodContext, ""); 37 | } 38 | 39 | public class Stelem_IConverterCXX : ICXXILConverter 40 | { 41 | public OpCode TargetOpCode() => OpCodes.Stelem_I; 42 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 43 | => StelemConvert.Convert(instruction, methodContext, "int"); 44 | } 45 | public class Stelem_I1ConverterCXX : ICXXILConverter 46 | { 47 | public OpCode TargetOpCode() => OpCodes.Stelem_I1; 48 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 49 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int8"); 50 | } 51 | public class Stelem_I2ConverterCXX : ICXXILConverter 52 | { 53 | public OpCode TargetOpCode() => OpCodes.Stelem_I2; 54 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 55 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int16"); 56 | } 57 | public class Stelem_I4ConverterCXX : ICXXILConverter 58 | { 59 | public OpCode TargetOpCode() => OpCodes.Stelem_I4; 60 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 61 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int32"); 62 | } 63 | public class Stelem_I8ConverterCXX : ICXXILConverter 64 | { 65 | public OpCode TargetOpCode() => OpCodes.Stelem_I8; 66 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 67 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int64"); 68 | } 69 | 70 | public class Stelem_R4ConverterCXX : ICXXILConverter 71 | { 72 | public OpCode TargetOpCode() => OpCodes.Stelem_R4; 73 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 74 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Single"); 75 | } 76 | public class Stelem_R8ConverterCXX : ICXXILConverter 77 | { 78 | public OpCode TargetOpCode() => OpCodes.Stelem_R8; 79 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 80 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Double"); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/StindConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | 9 | namespace RTCLI.AOTCompiler.ILConverters 10 | { 11 | public class StindConvert 12 | { 13 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, string type) 14 | { 15 | var op1 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 16 | var op2 = (methodContext as CXXMethodTranslateContext).CmptStackPopObject; 17 | return $"RTCLI::Deref<{type}>({op1}) = {op2};"; 18 | } 19 | } 20 | 21 | public class Stind_RefConverterCXX : ICXXILConverter 22 | { 23 | public OpCode TargetOpCode() => OpCodes.Stind_Ref; 24 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 25 | => StindConvert.Convert(instruction, methodContext, ""); 26 | } 27 | 28 | public class Stind_IConverterCXX : ICXXILConverter 29 | { 30 | public OpCode TargetOpCode() => OpCodes.Stind_I; 31 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 32 | => StindConvert.Convert(instruction, methodContext, "int"); 33 | } 34 | public class Stind_I1ConverterCXX : ICXXILConverter 35 | { 36 | public OpCode TargetOpCode() => OpCodes.Stind_I1; 37 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 38 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i8"); 39 | } 40 | public class Stind_I2ConverterCXX : ICXXILConverter 41 | { 42 | public OpCode TargetOpCode() => OpCodes.Stind_I2; 43 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 44 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i16"); 45 | } 46 | public class Stind_I4ConverterCXX : ICXXILConverter 47 | { 48 | public OpCode TargetOpCode() => OpCodes.Stind_I4; 49 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 50 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i32"); 51 | } 52 | public class Stind_I8ConverterCXX : ICXXILConverter 53 | { 54 | public OpCode TargetOpCode() => OpCodes.Stind_I8; 55 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 56 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i64"); 57 | } 58 | 59 | public class Stind_R4ConverterCXX : ICXXILConverter 60 | { 61 | public OpCode TargetOpCode() => OpCodes.Stind_R4; 62 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 63 | => StindConvert.Convert(instruction, methodContext, "RTCLI::f32"); 64 | } 65 | public class Stind_R8ConverterCXX : ICXXILConverter 66 | { 67 | public OpCode TargetOpCode() => OpCodes.Stind_R8; 68 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 69 | => StindConvert.Convert(instruction, methodContext, "RTCLI::f64"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/CXX/StlocConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using RTCLI.AOTCompiler.Translators; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler.Metadata; 8 | using System.Reflection; 9 | 10 | namespace RTCLI.AOTCompiler.ILConverters 11 | { 12 | public class StlocConvert 13 | { 14 | public static string Convert(Instruction instruction, MethodTranslateContext methodContext, int index) 15 | { 16 | return $"v{index} = {(methodContext as CXXMethodTranslateContext).CmptStackPopObject};"; 17 | } 18 | } 19 | public class Stloc_0ConverterCXX : ICXXILConverter 20 | { 21 | public OpCode TargetOpCode() => OpCodes.Stloc_0; 22 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 23 | => StlocConvert.Convert(instruction, methodContext, 0); 24 | } 25 | public class Stloc_1ConverterCXX : ICXXILConverter 26 | { 27 | public OpCode TargetOpCode() => OpCodes.Stloc_1; 28 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 29 | => StlocConvert.Convert(instruction, methodContext, 1); 30 | } 31 | public class Stloc_2ConverterCXX : ICXXILConverter 32 | { 33 | public OpCode TargetOpCode() => OpCodes.Stloc_2; 34 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 35 | => StlocConvert.Convert(instruction, methodContext, 2); 36 | } 37 | public class Stloc_3ConverterCXX : ICXXILConverter 38 | { 39 | public OpCode TargetOpCode() => OpCodes.Stloc_3; 40 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 41 | => StlocConvert.Convert(instruction, methodContext, 3); 42 | } 43 | public class Stloc_SConverterCXX : ICXXILConverter 44 | { 45 | public OpCode TargetOpCode() => OpCodes.Stloc_S; 46 | public string Convert(Instruction instruction, MethodTranslateContext methodContext) 47 | => StlocConvert.Convert(instruction, methodContext, (instruction.Operand as VariableDefinition).Index); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/ILConverter/ILConverter.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System.Dynamic; 3 | using RTCLI.AOTCompiler.Translators; 4 | 5 | namespace RTCLI.AOTCompiler.ILConverters 6 | { 7 | public interface IILConverter 8 | { 9 | OpCode TargetOpCode(); 10 | string Convert(Instruction instruction, MethodTranslateContext methodContext); 11 | } 12 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/AssemblyInformation.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | 5 | using Mono.Cecil; 6 | using Newtonsoft.Json; 7 | using System; 8 | 9 | namespace RTCLI.AOTCompiler.Metadata 10 | { 11 | public class AssemblyInformation : IMetadataInformation 12 | { 13 | public string IdentName => definition.Name.Name.Replace('.', '_') 14 | /*+ ".v" + definition.Name.Version.ToString().Replace('.', '_')*/; 15 | public string FullName => definition.FullName; 16 | public string Name => definition.Name.Name; 17 | 18 | public readonly Dictionary Modules = new Dictionary(); 19 | public AssemblyInformation(AssemblyDefinition def, MetadataContext metadataContext) 20 | { 21 | this.definition = def; 22 | this.MetadataContext = metadataContext; 23 | 24 | foreach (var module in def.Modules) 25 | { 26 | if(module.HasTypes) 27 | Modules.Add(module, new ModuleInformation(module, metadataContext)); 28 | } 29 | } 30 | 31 | [JsonIgnore] private readonly AssemblyDefinition definition = null; 32 | public IMetadataTokenProvider Definition => definition; 33 | public MetadataContext MetadataContext { get; } 34 | } 35 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/AssemblyResolver.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil; 2 | 3 | namespace RTCLI.AOTCompiler.Metadata 4 | { 5 | internal sealed class BasePathAssemblyResolver : IAssemblyResolver 6 | { 7 | private readonly DefaultAssemblyResolver resolver = new DefaultAssemblyResolver(); 8 | 9 | public BasePathAssemblyResolver(string basePath) 10 | { 11 | resolver.AddSearchDirectory(basePath); 12 | } 13 | 14 | public void Dispose() 15 | { 16 | resolver.Dispose(); 17 | } 18 | 19 | public AssemblyDefinition Resolve(AssemblyNameReference name) 20 | { 21 | return resolver.Resolve(name); 22 | } 23 | 24 | public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) 25 | { 26 | return resolver.Resolve(name, parameters); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/FieldInformation.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Mono.Cecil; 4 | using Newtonsoft.Json; 5 | 6 | namespace RTCLI.AOTCompiler.Metadata 7 | { 8 | public partial class FieldInformation : IMemberInformation 9 | { 10 | public readonly string FullName = "None"; 11 | public string Name => definition.Name; 12 | public readonly string FieldType = null; 13 | 14 | public FieldInformation(FieldDefinition def, MetadataContext metadataContext) 15 | { 16 | this.definition = def; 17 | this.MetadataContext = metadataContext; 18 | 19 | FieldType = def.FieldType.FullName; 20 | FullName = def.FullName; 21 | } 22 | 23 | public bool IsPrivate => definition.IsPrivate; 24 | public bool IsStatic => definition.IsStatic; 25 | public bool IsPublic => definition.IsPublic; 26 | public bool IsFamily => definition.IsFamily; 27 | 28 | [JsonIgnore] private readonly FieldDefinition definition = null; 29 | public IMetadataTokenProvider Definition => definition; 30 | public MetadataContext MetadataContext { get; } 31 | } 32 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/InstructionInformation.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil; 2 | using Mono.Cecil.Cil; 3 | using Newtonsoft.Json; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Text; 7 | 8 | namespace RTCLI.AOTCompiler.Metadata 9 | { 10 | public struct InstructionInformation 11 | { 12 | public InstructionInformation(Instruction inst) 13 | { 14 | instruction = inst; 15 | } 16 | 17 | [JsonIgnore] Instruction instruction; 18 | public string Body => instruction.ToString(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/MemberInformation.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using Mono.Cecil; 3 | using Mono.Cecil.Cil; 4 | 5 | namespace RTCLI.AOTCompiler.Metadata 6 | { 7 | public interface IMemberInformation : IMetadataInformation 8 | { 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/MetadataContext.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | 7 | using Mono.Cecil; 8 | using Newtonsoft.Json; 9 | using System.Diagnostics; 10 | 11 | namespace RTCLI.AOTCompiler.Metadata 12 | { 13 | public sealed class MetadataContext 14 | { 15 | public static MetadataContext NetStandardMetadataContext = null; 16 | public Dictionary Assemblies = new Dictionary(); 17 | public Dictionary Types = new Dictionary(); 18 | private List assemblyFindDir = new List(); 19 | 20 | static MetadataContext() 21 | { 22 | // Initialize NetStandard. 23 | NetStandardMetadataContext = new MetadataContext("netstandard.dll", false); 24 | } 25 | 26 | public MetadataContext(string assemblyPath, bool readSymbols) 27 | { 28 | // Initialize Assembly Resolver. 29 | var resolver = new BasePathAssemblyResolver(Path.GetDirectoryName(assemblyPath)); 30 | var parameter = new ReaderParameters 31 | { 32 | AssemblyResolver = resolver, 33 | ReadSymbols = readSymbols 34 | }; 35 | // Read Assembly 36 | string AssemblyBase = Path.GetDirectoryName(assemblyPath); 37 | assemblyFindDir.Add(Path.GetDirectoryName(assemblyPath)); 38 | assemblyFindDir.Add(""); 39 | assemblyFindDir.Add(Directory.GetCurrentDirectory()); 40 | 41 | var FocusedAssemblyLoaded = ReadAssemblyRecursively(assemblyPath, parameter); 42 | 43 | foreach (var assembly in Assemblies.Values) 44 | foreach (var module in assembly.Modules.Values) 45 | foreach (var type in module.Types.Values) 46 | BuildTypeMapRecursively(type); 47 | foreach (var type in Types.Values) 48 | type.SetupBaseAndInterface(this); 49 | FocusedAssembly = FocusedAssemblyLoaded.Name.Name; 50 | } 51 | 52 | private AssemblyDefinition ReadAssemblyFromEnv(string assemblyName, ReaderParameters parameter) 53 | { 54 | AssemblyDefinition loaded = null; 55 | if (File.Exists(assemblyName)) 56 | { 57 | System.Console.WriteLine($"Reading {assemblyName}..."); 58 | loaded = AssemblyDefinition.ReadAssembly(assemblyName, parameter); 59 | } 60 | else 61 | { 62 | foreach (var dir in assemblyFindDir) 63 | { 64 | if (File.Exists(Path.Combine(dir, assemblyName))) 65 | { 66 | System.Console.WriteLine($"Reading {assemblyName}..."); 67 | loaded = AssemblyDefinition.ReadAssembly(Path.Combine(dir, assemblyName), parameter); 68 | } 69 | } 70 | } 71 | 72 | if (Assemblies.ContainsKey(loaded.Name.Name))//Already Exists this Assembly 73 | { 74 | if (Assemblies[loaded.Name.Name].FullName != loaded.FullName)//Version Diff 75 | Trace.Assert(false, "Assembly: " + loaded.Name.Name + " already exists!"); 76 | return null; 77 | } 78 | else 79 | Assemblies.Add(loaded.Name.Name, new AssemblyInformation(loaded, this)); 80 | 81 | return loaded; 82 | } 83 | 84 | private AssemblyDefinition ReadAssemblyRecursively(string assemblyName, ReaderParameters parameter) 85 | { 86 | AssemblyDefinition AssemblyLoadedRecursively = null; 87 | AssemblyLoadedRecursively = ReadAssemblyFromEnv(assemblyName, parameter); 88 | if(AssemblyLoadedRecursively != null) 89 | { 90 | var AssemblyLoadedRecursivelyName = AssemblyLoadedRecursively.Name.Name; 91 | ReaderParameters sys_parameter = new ReaderParameters 92 | { 93 | AssemblyResolver = parameter.AssemblyResolver, 94 | ReadSymbols = false 95 | }; 96 | 97 | foreach (var module in AssemblyLoadedRecursively.Modules) 98 | { 99 | var references = module.AssemblyReferences; 100 | foreach (var reference in references) 101 | { 102 | if(NetStandardMetadataContext is null) 103 | { 104 | ReadAssemblyRecursively(reference.Name + ".dll", sys_parameter); 105 | } 106 | else if (reference.Name != "netstandard" && reference.Name != "mscorlib") 107 | { 108 | ReadAssemblyRecursively(reference.Name + ".dll", parameter); 109 | } 110 | else 111 | { 112 | Assemblies = Assemblies.Concat(NetStandardMetadataContext.Assemblies).ToDictionary(k => k.Key, v => v.Value); 113 | } 114 | } 115 | } 116 | } 117 | return AssemblyLoadedRecursively; 118 | } 119 | 120 | public void BuildTypeMapRecursively(TypeInformation type) 121 | { 122 | Types.Add(type.FullName, type); 123 | foreach (var nested in type.Nested) 124 | BuildTypeMapRecursively(nested); 125 | } 126 | 127 | public TypeInformation GetTypeInformation(string inType) 128 | { 129 | // CTS 130 | if (Types.ContainsKey(inType)) 131 | return Types[inType]; 132 | return null; 133 | } 134 | public TypeInformation GetTypeInformation(TypeReference inType) 135 | { 136 | if (inType.IsArray) 137 | return new TypeInformation(inType as ArrayType, this); 138 | if(inType.IsGenericInstance) 139 | return new TypeInformation(inType as GenericInstanceType, this); 140 | if(inType.IsGenericParameter) 141 | return new TypeInformation(inType as GenericParameter, this); 142 | if(inType.IsByReference) 143 | return new TypeInformation(inType, this); 144 | 145 | if(inType.IsPinned) 146 | return new TypeInformation(inType, this); 147 | if(inType.IsPointer) 148 | return new TypeInformation(inType as PointerType, this); 149 | if(inType.IsRequiredModifier) 150 | return new TypeInformation(inType, this); 151 | 152 | return GetTypeInformation(inType.FullName); 153 | } 154 | 155 | [JsonIgnore] public string FocusedAssembly; 156 | } 157 | 158 | public static class MetaExtension 159 | { 160 | public static MethodInformation GetMetaInformation( 161 | this MethodReference methodReference, MetadataContext context) 162 | { 163 | var type = context.GetTypeInformation(methodReference.DeclaringType); 164 | foreach (var mtdInfo in type.Methods) 165 | { 166 | if (mtdInfo.Definition is MethodReference mtdRef) 167 | if (mtdRef.Name == methodReference.Name) 168 | return mtdInfo; 169 | } 170 | return null; 171 | } 172 | } 173 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/MetadataInformationBase.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using Mono.Cecil; 3 | using Newtonsoft.Json; 4 | 5 | namespace RTCLI.AOTCompiler.Metadata 6 | { 7 | public interface IMetadataInformation 8 | { 9 | [JsonIgnore] IMetadataTokenProvider Definition { get; } 10 | [JsonIgnore] MetadataContext MetadataContext { get; } 11 | } 12 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/MethodInformation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using Mono.Cecil; 5 | using Mono.Cecil.Cil; 6 | using Newtonsoft.Json; 7 | using System.Linq; 8 | 9 | namespace RTCLI.AOTCompiler.Metadata 10 | { 11 | public partial class MethodInformation : IMemberInformation 12 | { 13 | public MethodInformation(MethodDefinition def, MetadataContext metadataContext) 14 | { 15 | this.definition = def; 16 | this.MetadataContext = metadataContext; 17 | 18 | if (def.HasBody) 19 | { 20 | foreach (var Inst in def.Body.Instructions) 21 | { 22 | Instructions.Add(new InstructionInformation(Inst)); 23 | } 24 | if(def.Body.HasVariables) 25 | { 26 | foreach (var localVar in def.Body.Variables) 27 | { 28 | LocalVariables.Add(new VariableInformation(localVar, metadataContext)); 29 | } 30 | } 31 | } 32 | if (def.HasParameters) 33 | { 34 | foreach (ParameterDefinition param in def.Parameters) 35 | { 36 | Parameters.Add(new ParameterInformation(param, metadataContext)); 37 | } 38 | } 39 | if (def.HasGenericParameters) 40 | genericParameterTypes = def.GenericParameters.Select(a => MetadataContext.GetTypeInformation(a)).ToArray(); 41 | } 42 | 43 | [JsonIgnore] public MethodBody Body => definition.Body; 44 | [JsonIgnore] private readonly MethodDefinition definition = null; 45 | 46 | public bool InitLocals => definition.Body!=null ? definition.Body.InitLocals : false; 47 | 48 | public bool IsPrivate => definition.IsPrivate; 49 | public bool IsStatic => definition.IsStatic; 50 | public bool IsConstructor => definition.IsConstructor; 51 | public bool IsPublic => definition.IsPublic; 52 | public bool IsFamily => definition.IsFamily; 53 | public bool IsStaticConstructor => definition.IsStatic && definition.IsConstructor; 54 | public bool HasGenericParameters => definition.HasGenericParameters; 55 | public bool IsNewSlot => definition.IsNewSlot; 56 | 57 | 58 | public readonly List Instructions = new List(); 59 | public readonly List LocalVariables = new List(); 60 | public readonly List Parameters = new List(); 61 | public readonly TypeInformation[] genericParameterTypes = null; 62 | public IMetadataTokenProvider Definition => definition; 63 | public MetadataContext MetadataContext { get; } 64 | } 65 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/ModuleInformation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Mono.Cecil; 5 | using Newtonsoft.Json; 6 | 7 | namespace RTCLI.AOTCompiler.Metadata 8 | { 9 | public class ModuleInformation : IMemberInformation 10 | { 11 | public readonly Dictionary Types = new Dictionary(); 12 | public ModuleInformation(ModuleDefinition def, MetadataContext metadataContext) 13 | { 14 | this.definition = def; 15 | this.MetadataContext = metadataContext; 16 | 17 | foreach(var type in definition.Types) 18 | { 19 | if(type.FullName != "") 20 | { 21 | if(Types.ContainsKey(type.FullName)) 22 | { 23 | Console.WriteLine(type.Module.Name 24 | + "!=" 25 | + (Types[type.FullName].Definition as TypeDefinition).Module.Name 26 | ); 27 | } 28 | else 29 | Types.Add(type.FullName, new TypeInformation(type, metadataContext)); 30 | } 31 | } 32 | } 33 | 34 | public string CXXHeaderName => definition.Name.Replace(".dll", "").Replace(".", "_"); 35 | [JsonIgnore] private readonly ModuleDefinition definition = null; 36 | public IMetadataTokenProvider Definition => definition; 37 | public MetadataContext MetadataContext { get; } 38 | } 39 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/Operand.cs: -------------------------------------------------------------------------------- 1 | namespace RTCLI.AOTCompiler.Metadata 2 | { 3 | public class Operand 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/ParameterInformation.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil; 2 | using Newtonsoft.Json; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace RTCLI.AOTCompiler.Metadata 8 | { 9 | public class ParameterInformation 10 | { 11 | public ParameterInformation(ParameterDefinition defination, MetadataContext context) 12 | { 13 | this.MetadataContext = context; 14 | this.Definition = defination; 15 | } 16 | public string CXXParamDecorated 17 | { 18 | get 19 | { 20 | if (Definition.ParameterType.IsGenericParameter) 21 | return $"RTCLI::TVar<{CXXTypeName}>"; 22 | if (!IsValueType || (Definition.IsOut || Definition.IsIn)) 23 | return CXXTypeName + "&"; 24 | 25 | return CXXTypeName; 26 | } 27 | } 28 | public string CXXTypeName 29 | => MetadataContext.GetTypeInformation(Definition.ParameterType).CXXTypeName; 30 | 31 | public bool IsByReference => Definition.ParameterType.IsByReference; 32 | 33 | public string Name => Definition.Name; 34 | public bool IsValueType => Definition.ParameterType.IsValueType; 35 | [JsonIgnore] public TypeInformation Type => MetadataContext.GetTypeInformation(Definition.ParameterType); 36 | [JsonIgnore] public readonly ParameterDefinition Definition = null; 37 | [JsonIgnore] public MetadataContext MetadataContext { get; } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/PropertyInformation.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Mono.Cecil; 4 | using Newtonsoft.Json; 5 | 6 | namespace RTCLI.AOTCompiler.Metadata 7 | { 8 | public class PropertyInformation : IMemberInformation 9 | { 10 | public PropertyInformation(PropertyDefinition def, MetadataContext metadataContext) 11 | { 12 | this.definition = def; 13 | this.MetadataContext = metadataContext; 14 | } 15 | 16 | [JsonIgnore] private readonly PropertyDefinition definition = null; 17 | public IMetadataTokenProvider Definition => definition; 18 | public MetadataContext MetadataContext { get; } 19 | } 20 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/TypeInformation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text.RegularExpressions; 5 | using Mono.Cecil; 6 | using System.Linq; 7 | using Newtonsoft.Json; 8 | 9 | namespace RTCLI.AOTCompiler.Metadata 10 | { 11 | public partial class TypeInformation : IMemberInformation 12 | { 13 | public string FullName => IsReference ? reference.FullName : definition.FullName; 14 | public string Namespace => IsReference ? reference.FullName : definition.Namespace; 15 | public string TypeName => IsReference ? reference.FullName : definition.Name; 16 | public readonly string[] NamespaceChain = null; 17 | public readonly string[] TypeAttributes = null; 18 | 19 | public bool IsValueType => definition == null ? false : definition.IsValueType; 20 | public bool IsReference => reference != null; 21 | public bool IsGenericParameter => reference == null ? false : reference.IsGenericParameter; 22 | public bool IsPointer => reference == null ? false : reference.IsPointer; 23 | public bool IsArray => reference == null ? false : reference.IsArray; 24 | public bool IsStruct => definition!=null ? definition.IsValueType : false; 25 | public bool IsByReference => reference == null ? false : reference.IsByReference; 26 | public bool IsGenericInstance => reference == null ? false : reference.IsGenericInstance; 27 | public bool HasGenericParameters => definition == null ? false : definition.HasGenericParameters; 28 | 29 | public readonly List Methods = new List(); 30 | public readonly List Fields = new List(); 31 | public readonly List Properties = new List(); 32 | public readonly List Nested = new List(); 33 | public readonly List Interfaces = new List(); 34 | public TypeInformation BaseType = null; 35 | public readonly MethodInformation StaticConstructor = null; 36 | public TypeInformation GetElementType() => elementType; 37 | 38 | public void SetupBaseAndInterface(MetadataContext metadataContext) 39 | { 40 | if (definition == null || IsGenericInstance) 41 | return; 42 | foreach (var Interface in definition.Interfaces) 43 | Interfaces.Add(MetadataContext.GetTypeInformation(Interface.InterfaceType)); 44 | if(definition.BaseType!=null) 45 | BaseType = MetadataContext.GetTypeInformation(definition.BaseType); 46 | } 47 | 48 | public TypeInformation(TypeReference def, MetadataContext metadataContext) 49 | { 50 | this.reference = def; 51 | this.MetadataContext = metadataContext; 52 | } 53 | 54 | public TypeInformation(TypeDefinition def, MetadataContext metadataContext) 55 | { 56 | this.definition = def; 57 | this.MetadataContext = metadataContext; 58 | NamespaceChain = def.Namespace.Split('.'); 59 | 60 | foreach (var method in def.Methods) 61 | { 62 | var mtd = new MethodInformation(method, metadataContext); 63 | if (mtd.IsStaticConstructor) 64 | StaticConstructor = mtd; 65 | Methods.Add(mtd); 66 | } 67 | foreach(var prop in def.Properties) 68 | Properties.Add(new PropertyInformation(prop, metadataContext)); 69 | foreach(var field in def.Fields) 70 | Fields.Add(new FieldInformation(field, metadataContext)); 71 | foreach(var nested in def.NestedTypes) 72 | Nested.Add(new TypeInformation(nested, metadataContext)); 73 | 74 | 75 | this.genericParameterTypes = def.GenericParameters.Select(a => MetadataContext.GetTypeInformation(a)).ToArray(); 76 | TypeAttributes = def.Attributes.ToString().Split(sep, StringSplitOptions.RemoveEmptyEntries); 77 | } 78 | 79 | public TypeInformation(ArrayType def, MetadataContext metadataContext) 80 | { 81 | reference = def; 82 | this.definitionArray = def; 83 | this.MetadataContext = metadataContext; 84 | var dd = definitionArray.ElementType; 85 | 86 | this.elementType = IsArray ? MetadataContext.GetTypeInformation(dd) : null; 87 | 88 | } 89 | 90 | public TypeInformation(PointerType def, MetadataContext metadataContext) 91 | { 92 | reference = def; 93 | this.definitionPointer = def; 94 | this.MetadataContext = metadataContext; 95 | 96 | //var dd = definitionArray.ElementType; 97 | //this.elementType = IsArray ? MetadataContext.GetTypeInformation(dd) : null; 98 | } 99 | 100 | public TypeInformation(GenericParameter def, MetadataContext metadataContext) 101 | { 102 | reference = def; 103 | this.definitionGP = def; 104 | this.MetadataContext = metadataContext; 105 | } 106 | 107 | public TypeInformation(GenericInstanceType def, MetadataContext metadataContext) 108 | { 109 | reference = def; 110 | this.definitionGI = def; 111 | this.MetadataContext = metadataContext; 112 | var ElementType = metadataContext.GetTypeInformation(def.ElementType); 113 | this.genericElementType = ElementType; 114 | this.genericArgumentTypes = def.GenericArguments.Select(a => MetadataContext.GetTypeInformation(a)).ToArray(); 115 | this.definition = ElementType.definition; 116 | Methods = ElementType.Methods; 117 | StaticConstructor = ElementType.StaticConstructor; 118 | Properties = ElementType.Properties; 119 | Fields = ElementType.Fields; 120 | Nested = ElementType.Nested; 121 | TypeAttributes = ElementType.TypeAttributes; 122 | } 123 | 124 | private char[] sep = {',', ' '}; 125 | 126 | [JsonIgnore] private readonly TypeReference reference = null; 127 | 128 | [JsonIgnore] private readonly ArrayType definitionArray = null; 129 | [JsonIgnore] private readonly PointerType definitionPointer = null; 130 | [JsonIgnore] private readonly TypeInformation elementType = null; 131 | [JsonIgnore] private readonly GenericInstanceType definitionGI = null; 132 | [JsonIgnore] private readonly GenericParameter definitionGP = null; 133 | 134 | [JsonIgnore] private readonly TypeInformation genericElementType = null; 135 | [JsonIgnore] private readonly TypeInformation[] genericArgumentTypes = null; 136 | [JsonIgnore] private readonly TypeInformation[] genericParameterTypes = null; 137 | 138 | [JsonIgnore] private readonly TypeDefinition definition = null; 139 | [JsonIgnore] public IMetadataTokenProvider Definition => definition; 140 | public MetadataContext MetadataContext { get; } 141 | } 142 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Metadata/VariableInformation.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil; 2 | using Mono.Cecil.Cil; 3 | using Newtonsoft.Json; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Text; 7 | 8 | namespace RTCLI.AOTCompiler.Metadata 9 | { 10 | public partial class VariableInformation 11 | { 12 | public VariableInformation(VariableDefinition def, MetadataContext metadataContext) 13 | { 14 | Definition = def; 15 | MetadataContext = metadataContext; 16 | } 17 | 18 | public override string ToString() => Definition.ToString(); 19 | public int Index => Definition.Index; 20 | public bool IsPinned => Definition.IsPinned; 21 | 22 | [JsonIgnore] public TypeInformation Type => MetadataContext.GetTypeInformation(Definition.VariableType); 23 | [JsonIgnore] public readonly VariableDefinition Definition = null; 24 | [JsonIgnore] public MetadataContext MetadataContext { get; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Options.cs: -------------------------------------------------------------------------------- 1 | namespace RTCLI.AOTCompiler 2 | { 3 | public enum DebugInformationOptions 4 | { 5 | None, 6 | CommentOnly, 7 | Full 8 | } 9 | 10 | public enum TargetPlatforms 11 | { 12 | NativeCpp, 13 | InterpreterCode, 14 | Generic = NativeCpp 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/RTCLI.AOTCompiler.Core.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/TranslateContext.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Collections.Generic; 3 | using Mono.Cecil; 4 | using RTCLI.AOTCompiler.Metadata; 5 | 6 | namespace RTCLI.AOTCompiler 7 | { 8 | public sealed class TranslateContext 9 | { 10 | #region Constructors 11 | public TranslateContext(Assembly assembly, bool readSymbols, MetadataContext metadataContext) 12 | : this(assembly.Location, readSymbols, metadataContext) 13 | { 14 | } 15 | 16 | public TranslateContext(string assemblyPath, bool readSymbols, MetadataContext metadataContext) 17 | { 18 | this.FocusedAssembly = metadataContext.FocusedAssembly; 19 | this.MetadataContext = metadataContext; 20 | } 21 | #endregion 22 | public MetadataContext MetadataContext { get; } 23 | 24 | public string FocusedAssembly; 25 | public AssemblyInformation FocusedAssemblyInformation => MetadataContext.Assemblies[FocusedAssembly]; 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Translators/CXX/CXXMethodTranslateContext.cs: -------------------------------------------------------------------------------- 1 | using RTCLI.AOTCompiler.Metadata; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace RTCLI.AOTCompiler.Translators 7 | { 8 | public class CXXMethodTranslateContext : MethodTranslateContext 9 | { 10 | public CXXMethodTranslateContext(TranslateContext translateContext, MethodInformation methodInformation) 11 | : base(translateContext, methodInformation) 12 | { 13 | 14 | } 15 | 16 | private int CmptStackObjectIndex = -1; 17 | public string CmptStackObjectName => CmptStackValidate() ? $"s{CmptStack.Peek()}" : "ERROR_CMPT_STACK_EMPTY"; 18 | public string CmptStackPushObject 19 | { 20 | get 21 | { 22 | CmptStackObjectIndex++; 23 | CmptStack.Push(CmptStackObjectIndex); 24 | return $"s{CmptStack.Peek()}"; 25 | } 26 | } 27 | public string CmptStackPopObject 28 | { 29 | get 30 | { 31 | if (CmptStackValidate()) 32 | return $"s{CmptStack.Pop()}"; 33 | return "ERROR_CMPT_STACK_EMPTY"; 34 | } 35 | } 36 | public string CmptStackPopAll 37 | { 38 | get 39 | { 40 | string result = ""; 41 | while (CmptStackValidate()) 42 | result += $"s{CmptStack.Pop()}"; 43 | return result; 44 | } 45 | } 46 | public string CmptStackPeek 47 | { 48 | get 49 | { 50 | if (CmptStackValidate()) 51 | return $"s{CmptStack.Peek()}"; 52 | return "ERROR_CMPT_STACK_EMPTY"; 53 | } 54 | } 55 | private bool CmptStackValidate() 56 | { 57 | return CmptStack.Count > 0; 58 | } 59 | 60 | public int ArgumentsCount => ArgumentsStack.Count; 61 | 62 | Stack CmptStack = new Stack(); 63 | List ArgumentsStack = new List(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Translators/CXX/InformationToCXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | using System.Text.RegularExpressions; 6 | using Mono.Cecil; 7 | using Newtonsoft.Json; 8 | 9 | namespace RTCLI.AOTCompiler.Metadata 10 | { 11 | public partial class TypeInformation : IMemberInformation 12 | { 13 | public string CXXNamespace => "RTCLI::" + string.Join("::", Namespace.Split('.')); 14 | public string CXXTypeName 15 | { 16 | get 17 | { 18 | if (IsArray) 19 | return $"RTCLI::System::ElementArray<{elementType.CXXTypeName}>"; 20 | if (IsGenericInstance) 21 | return $"{genericElementType.CXXTypeName}<{string.Join(',', genericArgumentTypes.Select(a => a.CXXTypeName))}>"; 22 | if (IsGenericParameter) 23 | return definitionGP.FullName; 24 | else return "RTCLI::" + string.Join("::", FullName.Split('.', '/')).Replace("<>", "__").Replace('`', '_').Replace("<", "_").Replace(">", "_"); 25 | } 26 | } 27 | public string CXXTemplateParam => 28 | genericParameterTypes != null ? string.Join(',', genericParameterTypes.Select(a => $"class {a.CXXTypeName}")) : ""; 29 | public string CXXTemplateArg => 30 | genericParameterTypes != null ? string.Join(',', genericParameterTypes.Select(a => a.CXXTypeName)) : ""; 31 | public string CXXTypeNameShort 32 | { 33 | get 34 | { 35 | if (IsArray) 36 | return $"RTCLI::System::ElementArray<{elementType.CXXTypeName}>"; 37 | if (IsGenericInstance) 38 | return $"{genericElementType.CXXTypeName}<{string.Join(',', genericArgumentTypes.Select(a => a.CXXTypeName))}>"; 39 | if (IsGenericParameter) 40 | return definitionGP.FullName; 41 | if (IsPointer) 42 | return definitionPointer.FullName; 43 | else return definition.Name.Replace("<>", "__").Replace('`', '_').Replace("<", "_").Replace(">", "_"); 44 | } 45 | } 46 | 47 | 48 | 49 | public string CallStaticConstructor(Translators.MethodTranslateContext methodContext) 50 | { 51 | if (methodContext.MethodInfo == StaticConstructor) 52 | return ""; 53 | if (!methodContext.StaticReference.Contains(this)) 54 | { 55 | methodContext.StaticReference.Add(this); 56 | if (StaticConstructor != null) 57 | return $"{StaticConstructor.CXXMethodCallName(this)}();"; 58 | } 59 | return ""; 60 | } 61 | } 62 | 63 | public partial class MethodInformation : IMemberInformation 64 | { 65 | private string CXXParamsSequence(bool WithConstant) 66 | { 67 | string sequence = ""; 68 | //Since LdArg.0 -> this, start argument index from 1 69 | uint i = 1; 70 | foreach (var param in Parameters) 71 | { 72 | sequence += param.CXXParamDecorated + " " + param.Name; 73 | if (param.Definition.HasConstant && WithConstant) 74 | { 75 | if (param.Definition.Constant != null) 76 | sequence += " = " + param.Definition.Constant; 77 | else 78 | sequence += " = " + (param.IsValueType ? $"{param.CXXTypeName}()" : $"RTCLI::null"); 79 | } 80 | if (i++ != Parameters.Count) 81 | sequence = sequence + ", " + ((i % 3 == 1) ? "\n\t" : ""); 82 | } 83 | return sequence; 84 | } 85 | 86 | public string CXXMethodDeclareName 87 | { 88 | get 89 | { 90 | var type = MetadataContext.GetTypeInformation(definition.DeclaringType); 91 | return type.CXXTypeName + (type.HasGenericParameters ? $"<{type.CXXTemplateArg}>" : "") + "::" + CXXMethodNameShort; 92 | } 93 | } 94 | public string CXXMethodCallName(TypeInformation type) 95 | { 96 | return $"{type.CXXTypeName}::{CXXMethodNameShort}"; 97 | } 98 | public string CXXMethodNameShort 99 | => (definition.IsConstructor ? (definition.IsStatic ? "StaticConstructor" : "Constructor") : definition?.Name.Replace('<', '_').Replace('>', '_')); 100 | 101 | public string CXXMethodSignature(bool WithConstant) => (IsStatic ? "static " : "") + CXXRetType + " " + CXXMethodNameShort + CXXParamSequence(WithConstant); 102 | public string CXXParamSequence(bool WithConstant) => $"({CXXParamsSequence(WithConstant)})"; //Param Sequence 103 | public string CXXArgSequence => $"({string.Join(',', Parameters.Select(a => a.Name))})"; 104 | 105 | public string CXXTemplateParam => 106 | genericParameterTypes != null ? string.Join(',', genericParameterTypes.Select(a => $"class {a.CXXTypeName}")) : ""; 107 | public string CXXTemplateArg => 108 | genericParameterTypes != null ? string.Join(',', genericParameterTypes.Select(a => a.CXXTypeName)) : ""; 109 | public string CXXRetType 110 | { 111 | get 112 | { 113 | var type = MetadataContext.GetTypeInformation(definition.ReturnType); 114 | //if (type == null) 115 | // return "RTCLI::System::Void"; 116 | if(definition.ReturnType.IsByReference) 117 | return type.CXXTypeName + "&"; 118 | if (definition.ReturnType.IsGenericParameter) 119 | return $"RTCLI::TRet<{type.CXXTypeName}>"; 120 | if (type.IsValueType) 121 | return type.CXXTypeName; 122 | else 123 | return type.CXXTypeName + "&"; 124 | } 125 | } 126 | public string CXXStackName => $"{string.Join("_", CXXMethodDeclareName.Split(new string[] { "::", "<", ">" }, StringSplitOptions.None))}__Stack"; 127 | } 128 | 129 | public partial class FieldInformation : IMemberInformation 130 | { 131 | public string CXXTypeName => MetadataContext.GetTypeInformation(definition.FieldType).CXXTypeName; 132 | public string CXXTypeNameShort => MetadataContext.GetTypeInformation(definition.FieldType).CXXTypeNameShort; 133 | public string CXXFieldDeclaration => (IsStatic ? "static " : "") + 134 | (definition.FieldType.IsGenericParameter ? $"RTCLI::TField<{CXXTypeName}> " : definition.FieldType.IsValueType ? $"{CXXTypeName} " : $"RTCLI::Managed<{CXXTypeName}> ") + 135 | $"{Utilities.GetCXXValidTokenString(Name)};"; 136 | } 137 | 138 | public partial class VariableInformation 139 | { 140 | public string CXXTypeName => MetadataContext.GetTypeInformation(Definition.VariableType).CXXTypeName; 141 | 142 | public string CXXVarDeclaration => Type.IsGenericParameter ? $"RTCLI::TVar<{CXXTypeName}>" : Type.IsValueType ? CXXTypeName : $"RTCLI::TRef<{CXXTypeName}>"; 143 | public string CXXVarInitVal => this.Definition.VariableType.IsValueType ? $"{CXXVarDeclaration}()" : $"RTCLI::null"; 144 | } 145 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Translators/MetadataSerializer.cs: -------------------------------------------------------------------------------- 1 | 2 | 3 | using Newtonsoft.Json; 4 | using System.IO; 5 | 6 | namespace RTCLI.AOTCompiler.Translators 7 | { 8 | public class MetadataSerializer 9 | { 10 | public MetadataSerializer(TranslateContext translateContext) 11 | { 12 | this.translateContext = translateContext; 13 | } 14 | 15 | public void WriteResult(CodeTextStorage storage) 16 | { 17 | var assemblyInformation 18 | = translateContext.MetadataContext.Assemblies[translateContext.FocusedAssembly]; 19 | CodeTextWriter writer = storage.Wirter( 20 | assemblyInformation.IdentName + ".json" 21 | ); 22 | writer.WriteLine( 23 | JsonConvert.SerializeObject( 24 | translateContext.MetadataContext.Assemblies[translateContext.FocusedAssembly], 25 | Formatting.Indented) 26 | ); 27 | writer.Flush(); 28 | } 29 | 30 | private readonly TranslateContext translateContext = null; 31 | } 32 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.Core/Translators/MethodTranslateContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.Design; 4 | using System.Reflection; 5 | using Mono.Cecil.Cil; 6 | using RTCLI.AOTCompiler.Metadata; 7 | 8 | namespace RTCLI.AOTCompiler.Translators 9 | { 10 | public class MethodTranslateContext 11 | { 12 | public MethodTranslateContext(TranslateContext translateContext, MethodInformation methodInfo) 13 | { 14 | this.TranslateContext = translateContext; 15 | this.MetadataContext = translateContext.MetadataContext; 16 | this.MethodInfo = methodInfo; 17 | } 18 | public MethodInformation MethodInfo { get; } 19 | public TranslateContext TranslateContext { get; } 20 | public MetadataContext MetadataContext { get; } 21 | 22 | public List StaticReference = new List(); 23 | } 24 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30523.141 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RTCLI.AOTCompiler", "RTCLI.AOTCompiler\RTCLI.AOTCompiler.csproj", "{577E49C0-872D-4B5C-BD41-9C537A8D9EBE}" 7 | ProjectSection(ProjectDependencies) = postProject 8 | {0989EF22-4170-4F10-89C2-777906C32681} = {0989EF22-4170-4F10-89C2-777906C32681} 9 | {5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE} = {5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE} 10 | {3EE3D983-FB9A-4346-9411-CFFD9EA95005} = {3EE3D983-FB9A-4346-9411-CFFD9EA95005} 11 | EndProjectSection 12 | EndProject 13 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RTCLI.AOTCompiler.Core", "RTCLI.AOTCompiler.Core\RTCLI.AOTCompiler.Core.csproj", "{0989EF22-4170-4F10-89C2-777906C32681}" 14 | EndProject 15 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestCase", "RTCLI.TestCase\TestCase.csproj", "{3EE3D983-FB9A-4346-9411-CFFD9EA95005}" 16 | EndProject 17 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestCase.Reference", "RTCLI.TestCase.Reference\TestCase.Reference.csproj", "{5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE}" 18 | EndProject 19 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RTCLI.AOTCompiler3", "RTCLI.AOTCompiler3\RTCLI.AOTCompiler3.csproj", "{2B30EC00-6D8C-426A-A2E0-3D858F956AC6}" 20 | EndProject 21 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RTCLI.AOTCompiler3.Core", "RTCLI.AOTCompiler3.Core\RTCLI.AOTCompiler3.Core.csproj", "{C4192F78-A66A-4D6B-9967-64B7B1D3AA55}" 22 | EndProject 23 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProgram", "TestProgram\TestProgram.csproj", "{2630E1A8-EA3E-43A3-A540-61097B4A2280}" 24 | EndProject 25 | Global 26 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 27 | Debug|Any CPU = Debug|Any CPU 28 | Release|Any CPU = Release|Any CPU 29 | EndGlobalSection 30 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 31 | {577E49C0-872D-4B5C-BD41-9C537A8D9EBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {577E49C0-872D-4B5C-BD41-9C537A8D9EBE}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {577E49C0-872D-4B5C-BD41-9C537A8D9EBE}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {577E49C0-872D-4B5C-BD41-9C537A8D9EBE}.Release|Any CPU.Build.0 = Release|Any CPU 35 | {0989EF22-4170-4F10-89C2-777906C32681}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {0989EF22-4170-4F10-89C2-777906C32681}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {0989EF22-4170-4F10-89C2-777906C32681}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {0989EF22-4170-4F10-89C2-777906C32681}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {3EE3D983-FB9A-4346-9411-CFFD9EA95005}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {3EE3D983-FB9A-4346-9411-CFFD9EA95005}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {3EE3D983-FB9A-4346-9411-CFFD9EA95005}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {3EE3D983-FB9A-4346-9411-CFFD9EA95005}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE}.Release|Any CPU.ActiveCfg = Release|Any CPU 46 | {5EC2476B-FCE0-4931-8B80-8F7EA60DF0DE}.Release|Any CPU.Build.0 = Release|Any CPU 47 | {2B30EC00-6D8C-426A-A2E0-3D858F956AC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 48 | {2B30EC00-6D8C-426A-A2E0-3D858F956AC6}.Debug|Any CPU.Build.0 = Debug|Any CPU 49 | {2B30EC00-6D8C-426A-A2E0-3D858F956AC6}.Release|Any CPU.ActiveCfg = Release|Any CPU 50 | {2B30EC00-6D8C-426A-A2E0-3D858F956AC6}.Release|Any CPU.Build.0 = Release|Any CPU 51 | {C4192F78-A66A-4D6B-9967-64B7B1D3AA55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 | {C4192F78-A66A-4D6B-9967-64B7B1D3AA55}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 | {C4192F78-A66A-4D6B-9967-64B7B1D3AA55}.Release|Any CPU.ActiveCfg = Release|Any CPU 54 | {C4192F78-A66A-4D6B-9967-64B7B1D3AA55}.Release|Any CPU.Build.0 = Release|Any CPU 55 | {2630E1A8-EA3E-43A3-A540-61097B4A2280}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 56 | {2630E1A8-EA3E-43A3-A540-61097B4A2280}.Debug|Any CPU.Build.0 = Debug|Any CPU 57 | {2630E1A8-EA3E-43A3-A540-61097B4A2280}.Release|Any CPU.ActiveCfg = Release|Any CPU 58 | {2630E1A8-EA3E-43A3-A540-61097B4A2280}.Release|Any CPU.Build.0 = Release|Any CPU 59 | EndGlobalSection 60 | GlobalSection(SolutionProperties) = preSolution 61 | HideSolutionNode = FALSE 62 | EndGlobalSection 63 | GlobalSection(ExtensibilityGlobals) = postSolution 64 | SolutionGuid = {55581159-E044-4F80-9313-AE1B5AAB9DDA} 65 | EndGlobalSection 66 | EndGlobal 67 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler/Dispatcher.cs: -------------------------------------------------------------------------------- 1 | using RTCLI.AOTCompiler.Translators; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using RTCLI.AOTCompiler.Metadata; 5 | using System.Threading.Tasks; 6 | 7 | namespace RTCLI.AOTCompiler 8 | { 9 | public static class Dispatcher 10 | { 11 | public static void Translate( 12 | CodeTextStorage storage, 13 | DispatchArgs dispatchArgs, 14 | string assemblyPath) 15 | { 16 | System.Console.WriteLine("AOTCompiler: Preparing assembly: \"{0}\" ...", Path.GetFullPath(assemblyPath)); 17 | 18 | var metaContext = new MetadataContext(assemblyPath, dispatchArgs.readSymbols); 19 | var translateContext = new TranslateContext(assemblyPath, dispatchArgs.readSymbols, metaContext); 20 | CXXTranslateOptions cxxOptions = new CXXTranslateOptions(); 21 | cxxOptions.StaticAssertOnUnimplementatedILs = dispatchArgs.cxxStaticAssertOnUnimplementatedILs; 22 | var cxxTranslator = new CXXTranslator(translateContext, cxxOptions); 23 | 24 | //using (var _ = storage.EnterScope("meta")) 25 | //{ 26 | // MetadataSerializer metaSerializer = new MetadataSerializer(translateContext); 27 | // metaSerializer.WriteResult(storage); 28 | //} 29 | 30 | using (var _ = storage.EnterScope("include")) 31 | { 32 | cxxTranslator.WriteHeader(storage); 33 | } 34 | using (var _ = storage.EnterScope("src")) 35 | { 36 | cxxTranslator.WriteSource(storage); 37 | } 38 | 39 | System.Console.WriteLine(" done."); 40 | } 41 | 42 | public static void TranslateAll( 43 | TextWriter logw, 44 | string outputPath, 45 | DispatchArgs dispatchArgs, 46 | IEnumerable assemblyPaths) 47 | { 48 | Parallel.ForEach(assemblyPaths, aseemblyPath => { 49 | var storage = new CodeTextStorage( 50 | logw, 51 | outputPath, 52 | " "); 53 | 54 | Translate( 55 | storage, 56 | dispatchArgs, 57 | aseemblyPath); 58 | }); 59 | } 60 | 61 | public static void TranslateAll( 62 | TextWriter logw, 63 | string outputPath, 64 | DispatchArgs dispatchArgs, 65 | params string[] assemblyPaths) 66 | { 67 | TranslateAll( 68 | logw, 69 | outputPath, 70 | dispatchArgs, 71 | (IEnumerable)assemblyPaths); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Runtime.InteropServices; 4 | using Mono.Options; 5 | 6 | namespace RTCLI.AOTCompiler 7 | { 8 | public class DispatchArgs 9 | { 10 | public DebugInformationOptions debugInformationOptions = DebugInformationOptions.None; 11 | public bool readSymbols = true; 12 | public bool enableBundler = false; 13 | public TargetPlatforms targetPlatform = TargetPlatforms.Generic; 14 | public bool cxxStaticAssertOnUnimplementatedILs = false; 15 | } 16 | 17 | public class Program 18 | { 19 | public static int Main(string[] args) 20 | { 21 | try 22 | { 23 | DispatchArgs dispatchArgs = new DispatchArgs(); 24 | var help = false; 25 | 26 | var options = new OptionSet() 27 | { 28 | { "g1|debug", "Emit debug informations (contains only comments)", v => dispatchArgs.debugInformationOptions = DebugInformationOptions.CommentOnly }, 29 | { "g|g2|debug-full", "Emit debug informations (contains line numbers)", v => dispatchArgs.debugInformationOptions = DebugInformationOptions.Full }, 30 | { "no-read-symbols", "NO read symbol files", _ => dispatchArgs.readSymbols = false }, 31 | { "bundler", "Produce bundler source file", _ => dispatchArgs.enableBundler = true }, 32 | { "target=", "Target platform [NativeCpp|InterpreterCode]", v => dispatchArgs.targetPlatform = Enum.TryParse(v, true, out var t) ? t : TargetPlatforms.Generic }, 33 | { "h|help", "Print this help", _ => help = true }, 34 | { "assert-with-il-unimpl", "Gen CXX Static Assert On Unimplementated ILs", _ => dispatchArgs.cxxStaticAssertOnUnimplementatedILs = true }, 35 | }; 36 | 37 | var extra = options.Parse(args); 38 | if (help || (extra.Count < 2)) 39 | { 40 | Console.Out.WriteLine("usage: RTCLI.AOTCompiler.exe [options]"); 41 | options.WriteOptionDescriptions(Console.Out); 42 | } 43 | else 44 | { 45 | var outputPath = extra[0]; 46 | var assemblyPaths = extra.Skip(1); 47 | dispatchArgs.readSymbols = false; 48 | 49 | Dispatcher.TranslateAll( 50 | Console.Out, 51 | outputPath, 52 | dispatchArgs, 53 | assemblyPaths 54 | ); 55 | } 56 | 57 | return 0; 58 | } 59 | catch (OptionException ex) 60 | { 61 | Console.Error.WriteLine(ex.Message); 62 | return Marshal.GetHRForException(ex); 63 | } 64 | catch (Exception ex) 65 | { 66 | Console.Error.WriteLine(ex); 67 | return Marshal.GetHRForException(ex); 68 | } 69 | } 70 | 71 | public TranslateContext translateContext = null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "RTCLI.AOTCompiler": { 4 | "commandName": "Project", 5 | "commandLineArgs": "-g1 \"$(SolutionDir)RTCLI.CXXTest/RTCLI.Generated\" \"$(SolutionDir)RTCLI.TestCase/bin/Debug/netstandard2.1/TestCase.dll\" \"$(SolutionDir)RTCLI.TestCase.Reference/bin/Debug/netstandard2.1/TestCase.Reference.dll\"" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler/RTCLI.AOTCompiler.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CXX/FieldInformationCXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | using System.Linq; 6 | 7 | namespace RTCLI.AOTCompiler3.Meta 8 | { 9 | public static class FieldInformationCXX 10 | { 11 | public static string CXXTypeName(this FieldDefinition field) 12 | { 13 | return field.FieldType.CXXTypeName(); 14 | } 15 | public static string CXXConstant(this FieldDefinition field) 16 | { 17 | if (field.Constant != null) 18 | return field.Constant.ToString(); 19 | else 20 | return (field.FieldType.IsValueType ? $"{field.FieldType.CXXTypeName()}()" : $"RTCLI::null"); 21 | } 22 | public static string CXXFieldDeclaration(this FieldDefinition field) 23 | { 24 | if (field.HasConstant && !field.IsStatic) 25 | { 26 | } 27 | string res = 28 | (field.IsStatic ? "static " : "") + 29 | (field.FieldType.IsGenericParameter ? $"RTCLI::TField<{field.CXXTypeName()}> " : 30 | field.FieldType.IsValueType ? $"{field.CXXTypeName()} " : $"RTCLI::TRef<{field.CXXTypeName()}> ") + 31 | $"{Utilities.GetCXXValidTokenString(field.Name)};"; 32 | return res; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CXX/MethodInformationCXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | using System.Linq; 6 | 7 | namespace RTCLI.AOTCompiler3.Meta 8 | { 9 | public static class MethodInformationCXX 10 | { 11 | public static string CXXTemplateParam(this MethodDefinition method) 12 | { 13 | var gTs = method.GenericParameters; 14 | return gTs != null ? string.Join(',', gTs.Select(a => $"class {a.CXXTypeName()}")) : ""; 15 | } 16 | 17 | public static string CondStr(bool Cond, string Str) 18 | { 19 | return Cond ? Str : ""; 20 | } 21 | [H2001()] 22 | public static string CXXMethodSignature(this MethodDefinition method) 23 | { 24 | return (method.IsStatic ? "static " : "") + 25 | method.CXXRetType() + " " + 26 | method.CXXShortMethodName() + method.CXXParamSequence(true); 27 | } 28 | 29 | [H2001()] 30 | public static string CXXMethodImplSignature(this MethodDefinition method, bool ValueType, TypeDefinition Type = null) 31 | { 32 | return method.CXXRetType() + " " + 33 | method.CXXMethodDeclareName(ValueType, Type) + method.CXXParamSequence(false); 34 | } 35 | 36 | public static string CXXMethodDeclarePrefix(this TypeDefinition Type, bool ValueType) 37 | { 38 | return $"{Type.CXXTypeName()}{CondStr(!ValueType && Type.IsValueType, "_V")}{CondStr(Type.HasGenericParameters, $"<{Type.CXXTemplateArg()}>")}"; 39 | } 40 | 41 | public static string CXXMethodDeclareName(this MethodDefinition Method, bool ValueType, TypeDefinition Type = null) 42 | { 43 | if(Type is null) 44 | Type = Method.DeclaringType; 45 | return $"{Type.CXXMethodDeclarePrefix(ValueType)}::{Method.CXXShortMethodName()}"; 46 | } 47 | 48 | public static string CXXArgSequence(this MethodDefinition method) 49 | { 50 | return $"({string.Join(',', method.Parameters.Select(a => a.Name))})"; 51 | } 52 | 53 | public static string CXXParamSequence(this MethodDefinition method, bool WithConstant) 54 | { 55 | string sequence = ""; 56 | //Since LdArg.0 -> this, start argument index from 1 57 | uint i = 1; 58 | foreach (var param in method.Parameters) 59 | { 60 | sequence += param.CXXParamDecorated() + " " + param.Name; 61 | if (param.HasConstant && WithConstant) 62 | { 63 | if (param.Constant != null) 64 | sequence += " = " + param.Constant; 65 | else 66 | sequence += " = " + 67 | (param.ParameterType.IsValueType ? 68 | $"{param.ParameterType.CXXTypeName()}()" : 69 | $"RTCLI::null"); 70 | } 71 | if (i++ != method.Parameters.Count) 72 | sequence = sequence + ", " + ((i % 3 == 1) ? "\n\t" : ""); 73 | } 74 | return $"({sequence})"; 75 | } 76 | 77 | public static string CXXRetType(this MethodDefinition method) 78 | { 79 | var type = method.ReturnType; 80 | if (type.IsVoid()) 81 | return "RTCLI::System::Void"; 82 | if (type.IsOptionalModifier) 83 | return type.GetElementType().CXXVarDeclaration(); 84 | 85 | bool IsByRef = type.IsByReference; 86 | if (IsByRef) 87 | { 88 | if (type.IsGenericParameter) 89 | return $"RTCLI::TRet<{type.CXXTypeName()}&>"; 90 | else if (type.GetElementType().IsValueType) 91 | return type.CXXTypeName() + "&"; 92 | else 93 | return $"RTCLI::TRef<{type.CXXTypeName()}>&"; 94 | } 95 | else 96 | { 97 | if (type.IsGenericParameter) 98 | return $"RTCLI::TRet<{type.CXXTypeName()}>"; 99 | else if (type.IsValueType) 100 | return type.CXXTypeName(); 101 | else 102 | return $"{type.CXXTypeName()}&"; 103 | } 104 | } 105 | 106 | public static string CXXMethodCallName(this MethodDefinition method, TypeReference type) 107 | { 108 | return $"{type.CXXTypeName()}::{method.CXXShortMethodName()}"; 109 | } 110 | public static string CXXRowName(this MethodDefinition method) 111 | => method?.Name.Replace('<', '_').Replace('>', '_'); 112 | public static string CXXShortMethodName(this MethodDefinition method) 113 | { 114 | if(method.IsConstructor) 115 | { 116 | if(method.IsStatic) 117 | { 118 | return Constants.CXXStaticCtorName; 119 | } 120 | return Constants.CXXCtorName; 121 | } 122 | #if ENABLE_EXPLICT_OVERRIDE 123 | if(method.IsVirtual) 124 | return method?.DeclaringType.CXXTypeName().Replace("::", "_") + "_" + method?.Name.Replace('<', '_').Replace('>', '_'); ; 125 | #endif 126 | return method.CXXRowName(); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CXX/MethodTranslateContextCXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public class MethodTranslateContextCXX 9 | { 10 | public MethodTranslateContextCXX(MethodDefinition method) 11 | { 12 | Method = method; 13 | } 14 | 15 | private int CmptStackObjectIndex = -1; 16 | public string CmptStackObjectName => CmptStackValidate() ? $"s{CmptStack.Peek()}" : "ERROR_CMPT_STACK_EMPTY"; 17 | public string CmptStackPushObject 18 | { 19 | get 20 | { 21 | CmptStackObjectIndex++; 22 | CmptStack.Push(CmptStackObjectIndex); 23 | return $"s{CmptStack.Peek()}"; 24 | } 25 | } 26 | public string CmptStackPopObject 27 | { 28 | get 29 | { 30 | if (CmptStackValidate()) 31 | return $"s{CmptStack.Pop()}"; 32 | return "ERROR_CMPT_STACK_EMPTY"; 33 | } 34 | } 35 | public string CmptStackPopAll 36 | { 37 | get 38 | { 39 | string result = ""; 40 | while (CmptStackValidate()) 41 | result += $"s{CmptStack.Pop()}"; 42 | return result; 43 | } 44 | } 45 | public string CmptStackPeek 46 | { 47 | get 48 | { 49 | if (CmptStackValidate()) 50 | return $"s{CmptStack.Peek()}"; 51 | return "ERROR_CMPT_STACK_EMPTY"; 52 | } 53 | } 54 | private bool CmptStackValidate() 55 | { 56 | return CmptStack.Count > 0; 57 | } 58 | 59 | 60 | public void AddStaticReference(TypeDefinition typeDefinition) 61 | { 62 | if (!StaticReference.Contains(typeDefinition)) 63 | StaticReference.Add(typeDefinition); 64 | } 65 | 66 | public int ArgumentsCount => ArgumentsStack.Count; 67 | Stack CmptStack = new Stack(); 68 | List ArgumentsStack = new List(); 69 | public List StaticReference = new List(); 70 | public List LableReference = new List(); 71 | public List TypeReference = new List(); 72 | 73 | public MethodDefinition Method { get; } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CXX/ParamInformationCXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | using System.Linq; 6 | 7 | namespace RTCLI.AOTCompiler3.Meta 8 | { 9 | public static class ParamInformationCXX 10 | { 11 | public static string CXXParamDecorated(this ParameterDefinition pram) 12 | { 13 | return pram.ParameterType.CXXVarDeclaration(); 14 | } 15 | public static string CXXTypeName(this ParameterDefinition pram) 16 | { 17 | return pram.ParameterType.CXXTypeName(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CXX/VarInformationCXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | using Mono.Cecil.Cil; 6 | using System.Linq; 7 | 8 | namespace RTCLI.AOTCompiler3.Meta 9 | { 10 | public static class VarInformationCXX 11 | { 12 | public static string CXXVarDeclaration(this TypeReference type) 13 | { 14 | if (type.IsVoid()) 15 | return "RTCLI::System::Void"; 16 | bool IsByRef = type.IsByReference; 17 | if (IsByRef) 18 | { 19 | if (type.IsGenericParameter) 20 | return $"RTCLI::TLocal<{type.CXXTypeName()}&>"; 21 | else if (type.GetElementType().IsValueType) 22 | return $"RTCLI::TRef<{type.CXXTypeName()}>"; 23 | else 24 | return $"RTCLI::TRef>"; 25 | } 26 | else 27 | { 28 | if (type.IsGenericParameter) 29 | return $"RTCLI::TLocal<{type.CXXTypeName()}>"; 30 | else if (type.IsValueType) 31 | return type.CXXTypeName(); 32 | else 33 | return $"RTCLI::TRef<{type.CXXTypeName()}>"; 34 | } 35 | } 36 | 37 | public static string CXXVarDeclaration(this VariableDefinition Var) 38 | { 39 | return Var.VariableType.CXXVarDeclaration(); 40 | } 41 | 42 | public static string CXXVarInitVal(this VariableDefinition Var) 43 | { 44 | var Type = Var.VariableType; 45 | return Type.IsValueType ? $"{Var.CXXVarDeclaration()}()" : $"RTCLI::null"; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CodeTextStorage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace RTCLI.AOTCompiler3 8 | { 9 | public class CodeTextStorage 10 | { 11 | private readonly TextWriter logw; 12 | public readonly string BasePath; 13 | private readonly Queue scopeNames = new Queue(); 14 | 15 | public CodeTextStorage(TextWriter logw, string basePath, string indent) 16 | { 17 | this.logw = logw; 18 | this.BasePath = basePath; 19 | } 20 | 21 | private string getScopePath() 22 | { 23 | string fullDirName = BasePath; 24 | foreach (var scope in scopeNames) 25 | { 26 | fullDirName = Path.Combine(fullDirName, scope); 27 | } 28 | return fullDirName; 29 | } 30 | 31 | public IDisposable EnterScope(string scopeName, bool splitScope = true) 32 | { 33 | scopeNames.Enqueue(splitScope ? Utilities.GetCXXLanguageScopedPath(scopeName) : scopeName); 34 | if (!Directory.Exists(getScopePath())) 35 | { 36 | Directory.CreateDirectory(getScopePath()); 37 | } 38 | return new ScopeDisposer(this); 39 | } 40 | 41 | public CodeTextWriter Wirter(string FileName) 42 | { 43 | var fullDirName = Path.GetDirectoryName(Path.Combine(getScopePath(), FileName)); 44 | if (!Directory.Exists(fullDirName)) 45 | { 46 | Directory.CreateDirectory(fullDirName); 47 | } 48 | return new CodeTextWriter(Path.Combine(getScopePath(), FileName)); 49 | } 50 | 51 | private sealed class ScopeDisposer : IDisposable 52 | { 53 | private CodeTextStorage parent; 54 | public ScopeDisposer(CodeTextStorage parent) 55 | { 56 | this.parent = parent; 57 | } 58 | 59 | public void Dispose() 60 | { 61 | if (parent != null) 62 | { 63 | parent.scopeNames.Dequeue(); 64 | parent = null; 65 | } 66 | } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/CodeTextWriter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Text; 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public class CodeTextWriter 9 | { 10 | public CodeTextWriter(string relatedPath) 11 | { 12 | sw = new StreamWriter(relatedPath, false, Encoding.UTF8); 13 | } 14 | 15 | public CodeTextWriter WriteLine(string toWrite = "") 16 | { 17 | sw.WriteLine(indentString + toWrite); 18 | return this; 19 | } 20 | 21 | public CodeTextWriter WriteLineRaw(string toWrite = "") 22 | { 23 | sw.WriteLine(toWrite); 24 | return this; 25 | } 26 | 27 | public CodeTextWriter indent() 28 | { 29 | indentString += "\t"; 30 | return this; 31 | } 32 | public CodeTextWriter unindent() 33 | { 34 | if(indentString.Length > 0) 35 | indentString = indentString.Remove(indentString.Length - 1); 36 | return this; 37 | } 38 | 39 | public void Flush() 40 | { 41 | sw.Flush(); 42 | } 43 | 44 | private String indentString = ""; 45 | private StreamWriter sw = null; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Constants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Mono.Cecil.Cil; 4 | using System.Reflection; 5 | using RTCLI.AOTCompiler3.ILConverters; 6 | 7 | namespace RTCLI.AOTCompiler3 8 | { 9 | public static class Constants 10 | { 11 | public static string CopyRight => "// COPYRIGHT STRING"; 12 | public static string CXXStaticCtorName => "StaticConstructor"; 13 | public static string CXXCtorName => "Constructor"; 14 | public static string CXXUberHeaderName => "_UberHeader_.h"; 15 | 16 | public static bool CXXUseUberHeader = false; 17 | 18 | static Constants() 19 | { 20 | InitAllCXXILConverters(); 21 | } 22 | public static Dictionary CXXILConverters = new Dictionary(); 23 | 24 | 25 | 26 | private static void InitAllCXXILConverters() 27 | { 28 | System.Console.WriteLine("Preparing ConvertersCXX..."); 29 | Assembly assembly = Assembly.GetAssembly(typeof(ILConverters.ICXXILConverter)); 30 | TypeFilter typeNameFilter = new TypeFilter(TypeNameFilter); 31 | int currentLineCursor = Console.CursorTop; 32 | int index = 0; 33 | foreach (var type in assembly.GetTypes()) 34 | { 35 | if (type.IsInterface) 36 | continue; 37 | Type[] typeInterfaces = type.FindInterfaces(typeNameFilter, typeof(ILConverters.ICXXILConverter)); 38 | if (typeInterfaces.Length > 0) 39 | { 40 | var newConv = System.Activator.CreateInstance(type) as ILConverters.ICXXILConverter; 41 | CXXILConverters.Add(newConv.TargetOpCode(), newConv); 42 | 43 | System.Console.SetCursorPosition(0, currentLineCursor); 44 | Console.Write(new string(' ', Console.WindowWidth)); 45 | System.Console.SetCursorPosition(0, currentLineCursor); 46 | System.Console.WriteLine($"Registered: {newConv.TargetOpCode()}, {++index} / {Enum.GetValues(typeof(Code)).Length}"); 47 | } 48 | } 49 | System.Console.WriteLine($"Total Converters: {CXXILConverters.Count} / {Enum.GetValues(typeof(Code)).Length}"); 50 | } 51 | private static bool TypeNameFilter(Type typeObj, Object criteriaObj) 52 | { 53 | if (typeObj.ToString() == criteriaObj.ToString()) 54 | return true; 55 | else 56 | return false; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/CallConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class MethodCallConvert 11 | { 12 | public static string GetMethodOwner(MethodReference mtd, MethodTranslateContextCXX methodContext) 13 | { 14 | return mtd.DeclaringType.CXXTypeName(); 15 | } 16 | public static void Visit(Instruction instruction, MethodTranslateContextCXX methodContext) 17 | { 18 | var mtd = instruction.Operand as MethodReference; 19 | var mtdDef = mtd.Resolve(); 20 | if (mtdDef.IsStatic) 21 | { 22 | var type = mtd.DeclaringType.Resolve(); 23 | methodContext.AddStaticReference(type); 24 | } 25 | } 26 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, bool Virt) 27 | { 28 | var mtd = instruction.Operand as MethodReference; 29 | var mtdDef = mtd.Resolve(); 30 | if(mtdDef is null) return "ERROR_METHOD_NAME"; 31 | 32 | string args = ""; 33 | List argList = new List(); 34 | for (int i = 1; i <= mtd.Parameters.Count; i++) 35 | argList.Add(methodContext.CmptStackPopObject); 36 | argList.Reverse(); 37 | args = string.Join(',', argList); 38 | /*if (mtd.FullName.StartsWith("!!0")) 39 | { 40 | var gargT = (mtd as GenericInstanceMethod).GenericArguments[0]; 41 | 42 | return $"{gargT.CXXTypeName()}& {methodContext.CmptStackPushObject} = " + 43 | $"\\n\t\tRTCLI::new_object<{gargT.CXXTypeName()}>({args});"; 44 | }*/ 45 | 46 | string genericArgs = ""; 47 | if (mtd is GenericInstanceMethod gmtd) 48 | genericArgs = $"<{string.Join(',', gmtd.GenericArguments.Select(a => a.CXXTypeName()))}>"; 49 | if (!mtdDef.IsStatic) 50 | { 51 | string caller = methodContext.CmptStackPopObject; 52 | string callBody; 53 | if (Virt) 54 | { 55 | var type = mtd.DeclaringType.Resolve(); 56 | callBody = 57 | $"{caller}.{mtdDef.CXXShortMethodName() + genericArgs}({args});"; // Method Call body. 58 | } 59 | else 60 | { 61 | var type = mtd.DeclaringType.Resolve(); 62 | callBody = 63 | $"{caller}.{mtdDef.CXXMethodCallName(type) + genericArgs}({args});"; // Method Call body. 64 | } 65 | return (mtd.ReturnType.FullName != "System.Void" ? $"auto {methodContext.CmptStackPushObject} = " : "") 66 | + callBody; 67 | } 68 | if (mtdDef.IsStatic) 69 | { 70 | var type = mtd.DeclaringType.Resolve(); 71 | string callBody = $"{mtdDef.CXXMethodCallName(type) + genericArgs}({args});"; // Method Call body. 72 | return (mtd.ReturnType.FullName != "System.Void" ? $"auto {methodContext.CmptStackPushObject} = " : "") 73 | + callBody; 74 | } 75 | return "ERROR_METHOD_NAME"; 76 | } 77 | } 78 | 79 | public class CallConverterCXX : ICXXILConverter 80 | { 81 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 82 | => MethodCallConvert.Convert(instruction, methodContext, false); 83 | public void Visit(Instruction instruction, MethodTranslateContextCXX methodContext) 84 | => MethodCallConvert.Visit(instruction, methodContext); 85 | public OpCode TargetOpCode() => OpCodes.Call; 86 | } 87 | public class CallVirtConverterCXX : ICXXILConverter 88 | { 89 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 90 | => MethodCallConvert.Convert(instruction, methodContext, true); 91 | public void Visit(Instruction instruction, MethodTranslateContextCXX methodContext) 92 | => MethodCallConvert.Visit(instruction, methodContext); 93 | public OpCode TargetOpCode() => OpCodes.Callvirt; 94 | } 95 | public class TailcallConverterCXX : ICXXILConverter 96 | { 97 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 98 | => ""; 99 | public OpCode TargetOpCode() => OpCodes.Tail; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/CompareConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class CgtConverterCXX : ICXXILConverter 11 | { 12 | public OpCode TargetOpCode() => OpCodes.Cgt; 13 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 14 | { 15 | var op2 = methodContext.CmptStackPopObject; 16 | var op1 = methodContext.CmptStackPopObject; 17 | return $"RTCLI::i32 {methodContext.CmptStackPushObject}" + 18 | $" = RTCLI::Cgt({op1}, {op2});"; 19 | } 20 | } 21 | 22 | public class Cgt_UnConverterCXX : ICXXILConverter 23 | { 24 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 25 | { 26 | var op2 = methodContext.CmptStackPopObject; 27 | var op1 = methodContext.CmptStackPopObject; 28 | return $"RTCLI::i32 {methodContext.CmptStackPushObject}" + 29 | $" = RTCLI::Cgt_Un({op1}, {op2});"; 30 | } 31 | 32 | public OpCode TargetOpCode() => OpCodes.Cgt_Un; 33 | } 34 | 35 | public class CeqConverterCXX : ICXXILConverter 36 | { 37 | public OpCode TargetOpCode() => OpCodes.Ceq; 38 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 39 | { 40 | var op2 = methodContext.CmptStackPopObject; 41 | var op1 = methodContext.CmptStackPopObject; 42 | return $"auto {methodContext.CmptStackPushObject}" + 43 | $" = RTCLI::Ceq({op1}, {op2});"; 44 | } 45 | } 46 | 47 | public class CltConverterCXX : ICXXILConverter 48 | { 49 | public OpCode TargetOpCode() => OpCodes.Clt; 50 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 51 | { 52 | var op2 = methodContext.CmptStackPopObject; 53 | var op1 = methodContext.CmptStackPopObject; 54 | return $"auto {methodContext.CmptStackPushObject}" + 55 | $" = RTCLI::Clt({op1}, {op2});"; 56 | } 57 | } 58 | 59 | public class Clt_UnConverterCXX : ICXXILConverter 60 | { 61 | public OpCode TargetOpCode() => OpCodes.Clt_Un; 62 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 63 | { 64 | var op2 = methodContext.CmptStackPopObject; 65 | var op1 = methodContext.CmptStackPopObject; 66 | return $"auto {methodContext.CmptStackPushObject}" + 67 | $" = RTCLI::Clt_Un({op1}, {op2});"; 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/ILConverterCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System.Dynamic; 3 | using RTCLI.AOTCompiler3.Translators; 4 | using RTCLI.AOTCompiler3.Meta; 5 | using System.Collections.Generic; 6 | using System.Reflection; 7 | using System; 8 | 9 | namespace RTCLI.AOTCompiler3.ILConverters 10 | { 11 | public interface ICXXILConverter : IILConverter 12 | { 13 | string Note(Instruction instruction, MethodTranslateContextCXX methodContext) 14 | => "// " + instruction.ToString().HoldEscape(); 15 | 16 | string Convert(Instruction instruction, MethodTranslateContextCXX methodContext); 17 | void Visit(Instruction instruction, MethodTranslateContextCXX methodContext) { } 18 | } 19 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/LdargConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class LdargConvert 11 | { 12 | public static string Convert(MethodTranslateContextCXX methodContext, int index) 13 | { 14 | if (!methodContext.Method.IsStatic) 15 | index -= 1; 16 | if(index < 0) 17 | return $"auto& {methodContext.CmptStackPushObject} = *this;"; 18 | else 19 | { 20 | var param = methodContext.Method.Parameters[index]; 21 | return $"auto& {methodContext.CmptStackPushObject} = {param.Name}{(param.ParameterType.IsValueType?"" : ".Get()")};"; 22 | } 23 | } 24 | } 25 | 26 | public class LdnullConverterCXX : ICXXILConverter 27 | { 28 | public OpCode TargetOpCode() => OpCodes.Ldnull; 29 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 30 | => $"auto& {methodContext.CmptStackPushObject} = RTCLI::null;"; 31 | } 32 | public class Ldarg_0ConverterCXX : ICXXILConverter 33 | { 34 | public OpCode TargetOpCode() => OpCodes.Ldarg_0; 35 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 36 | => LdargConvert.Convert(methodContext, 0); 37 | } 38 | public class Ldarg_1ConverterCXX : ICXXILConverter 39 | { 40 | public OpCode TargetOpCode() => OpCodes.Ldarg_1; 41 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 42 | => LdargConvert.Convert(methodContext, 1); 43 | } 44 | public class Ldarg_2ConverterCXX : ICXXILConverter 45 | { 46 | public OpCode TargetOpCode() => OpCodes.Ldarg_2; 47 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 48 | => LdargConvert.Convert(methodContext, 2); 49 | } 50 | public class Ldarg_3ConverterCXX : ICXXILConverter 51 | { 52 | public OpCode TargetOpCode() => OpCodes.Ldarg_3; 53 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 54 | => LdargConvert.Convert(methodContext, 3); 55 | } 56 | public class LdargConverterCXX : ICXXILConverter 57 | { 58 | public OpCode TargetOpCode() => OpCodes.Ldarg; 59 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 60 | => $"auto& {methodContext.CmptStackPushObject} = {(instruction.Operand as ParameterDefinition).Name};"; 61 | } 62 | public class Ldarg_SConverterCXX : ICXXILConverter 63 | { 64 | public OpCode TargetOpCode() => OpCodes.Ldarg_S; 65 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 66 | => $"auto& {methodContext.CmptStackPushObject} = {(instruction.Operand as ParameterDefinition).Name};"; 67 | } 68 | public class LdargaConverterCXX : ICXXILConverter 69 | { 70 | public OpCode TargetOpCode() => OpCodes.Ldarga; 71 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 72 | => $"auto& {methodContext.CmptStackPushObject} = RTCLI_ADDRESSOF({(instruction.Operand as ParameterDefinition).Name});"; 73 | } 74 | public class Ldarga_SConverterCXX : ICXXILConverter 75 | { 76 | public OpCode TargetOpCode() => OpCodes.Ldarga_S; 77 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 78 | => $"auto& {methodContext.CmptStackPushObject} = RTCLI_ADDRESSOF({(instruction.Operand as ParameterDefinition).Name});"; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/LdcConverters.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class Ldc_Convert 11 | { 12 | public static string I4Convert(Instruction instruction, MethodTranslateContextCXX ctx, Int32 val) 13 | { 14 | return $"RTCLI::System::Int32 {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Int32"}>({val});"; 15 | } 16 | public static string I8Convert(Instruction instruction, MethodTranslateContextCXX ctx, Int64 val) 17 | { 18 | return $"RTCLI::System::Int64 {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Int64"}>({val});"; 19 | } 20 | public static string R4Convert(Instruction instruction, MethodTranslateContextCXX ctx, Single val) 21 | { 22 | return $"RTCLI::System::Single {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Single"}>({val});"; 23 | } 24 | public static string R8Convert(Instruction instruction, MethodTranslateContextCXX ctx, Double val) 25 | { 26 | return $"RTCLI::System::Double {ctx.CmptStackPushObject} = RTCLI::StaticCast<{/*ctx.MetadataContext.UInt32Type.CXXTypeName*/"RTCLI::System::Double"}>({val});"; 27 | } 28 | } 29 | 30 | public class Ldc_I4ConverterCXX : ICXXILConverter 31 | { 32 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 33 | => Ldc_Convert.I4Convert(instruction, methodContext, Int32.Parse(instruction.Operand.ToString())); 34 | 35 | public OpCode TargetOpCode() => OpCodes.Ldc_I4; 36 | } 37 | public class Ldc_I4_0ConverterCXX : ICXXILConverter 38 | { 39 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 40 | => Ldc_Convert.I4Convert(instruction, methodContext, 0); 41 | 42 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_0; 43 | } 44 | public class Ldc_I4_SConverterCXX : ICXXILConverter 45 | { 46 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 47 | => Ldc_Convert.I4Convert(instruction, methodContext, Int32.Parse(instruction.Operand.ToString())); 48 | 49 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_S; 50 | } 51 | public class Ldc_I4_1ConverterCXX : ICXXILConverter 52 | { 53 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 54 | => Ldc_Convert.I4Convert(instruction, methodContext, 1); 55 | 56 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_1; 57 | } 58 | public class Ldc_I4_2ConverterCXX : ICXXILConverter 59 | { 60 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 61 | => Ldc_Convert.I4Convert(instruction, methodContext, 2); 62 | 63 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_2; 64 | } 65 | public class Ldc_I4_3ConverterCXX : ICXXILConverter 66 | { 67 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 68 | => Ldc_Convert.I4Convert(instruction, methodContext, 3); 69 | 70 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_3; 71 | } 72 | public class Ldc_I4_4ConverterCXX : ICXXILConverter 73 | { 74 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 75 | => Ldc_Convert.I4Convert(instruction, methodContext, 4); 76 | 77 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_4; 78 | } 79 | public class Ldc_I4_5ConverterCXX : ICXXILConverter 80 | { 81 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 82 | => Ldc_Convert.I4Convert(instruction, methodContext, 5); 83 | 84 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_5; 85 | } 86 | public class Ldc_I4_6ConverterCXX : ICXXILConverter 87 | { 88 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 89 | => Ldc_Convert.I4Convert(instruction, methodContext, 6); 90 | 91 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_6; 92 | } 93 | public class Ldc_I4_7ConverterCXX : ICXXILConverter 94 | { 95 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 96 | => Ldc_Convert.I4Convert(instruction, methodContext, 7); 97 | 98 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_7; 99 | } 100 | public class Ldc_I4_8ConverterCXX : ICXXILConverter 101 | { 102 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 103 | => Ldc_Convert.I4Convert(instruction, methodContext, 8); 104 | 105 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_8; 106 | } 107 | public class Ldc_I4_M1ConverterCXX : ICXXILConverter 108 | { 109 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 110 | => Ldc_Convert.I4Convert(instruction, methodContext, -1); 111 | 112 | public OpCode TargetOpCode() => OpCodes.Ldc_I4_M1; 113 | } 114 | 115 | public class Ldc_I8ConverterCXX : ICXXILConverter 116 | { 117 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 118 | => Ldc_Convert.I8Convert(instruction, methodContext, Int64.Parse(instruction.Operand.ToString())); 119 | 120 | public OpCode TargetOpCode() => OpCodes.Ldc_I8; 121 | } 122 | public class Ldc_R4ConverterCXX : ICXXILConverter 123 | { 124 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 125 | => Ldc_Convert.R4Convert(instruction, methodContext, Single.Parse(instruction.Operand.ToString())); 126 | 127 | public OpCode TargetOpCode() => OpCodes.Ldc_R4; 128 | } 129 | public class Ldc_R8ConverterCXX : ICXXILConverter 130 | { 131 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 132 | => Ldc_Convert.R8Convert(instruction, methodContext, Double.Parse(instruction.Operand.ToString())); 133 | 134 | public OpCode TargetOpCode() => OpCodes.Ldc_R8; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/LdelemConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class LdelemConvert 11 | { 12 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, string type) 13 | { 14 | var op2 = methodContext.CmptStackPopObject; 15 | var op1 = methodContext.CmptStackPopObject; 16 | return $"auto {methodContext.CmptStackPushObject}" + 17 | $" = RTCLI::ArrayGet<{type}>({op1}, {op2});"; 18 | } 19 | } 20 | 21 | public class LdelemConverterCXX : ICXXILConverter 22 | { 23 | public OpCode TargetOpCode() => OpCodes.Ldelem_Any; 24 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 25 | { 26 | var typeReference = instruction.Operand as TypeReference; 27 | return LdelemConvert.Convert(instruction, methodContext, typeReference.CXXTypeName()); 28 | } 29 | } 30 | public class Ldelem_RefConverterCXX : ICXXILConverter 31 | { 32 | public OpCode TargetOpCode() => OpCodes.Ldelem_Ref; 33 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 34 | => LdelemConvert.Convert(instruction, methodContext, ""); 35 | } 36 | 37 | public class Ldelem_IConverterCXX : ICXXILConverter 38 | { 39 | public OpCode TargetOpCode() => OpCodes.Ldelem_I; 40 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 41 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::NativeInt"); 42 | } 43 | public class Ldelem_I1ConverterCXX : ICXXILConverter 44 | { 45 | public OpCode TargetOpCode() => OpCodes.Ldelem_I1; 46 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 47 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int8"); 48 | } 49 | public class Ldelem_I2ConverterCXX : ICXXILConverter 50 | { 51 | public OpCode TargetOpCode() => OpCodes.Ldelem_I2; 52 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 53 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int16"); 54 | } 55 | public class Ldelem_I4ConverterCXX : ICXXILConverter 56 | { 57 | public OpCode TargetOpCode() => OpCodes.Ldelem_I4; 58 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 59 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int32"); 60 | } 61 | public class Ldelem_I8ConverterCXX : ICXXILConverter 62 | { 63 | public OpCode TargetOpCode() => OpCodes.Ldelem_I8; 64 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 65 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int64"); 66 | } 67 | 68 | public class Ldelem_R4ConverterCXX : ICXXILConverter 69 | { 70 | public OpCode TargetOpCode() => OpCodes.Ldelem_R4; 71 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 72 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Single"); 73 | } 74 | public class Ldelem_R8ConverterCXX : ICXXILConverter 75 | { 76 | public OpCode TargetOpCode() => OpCodes.Ldelem_R8; 77 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 78 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::Double"); 79 | } 80 | 81 | public class Ldelem_U1ConverterCXX : ICXXILConverter 82 | { 83 | public OpCode TargetOpCode() => OpCodes.Ldelem_U1; 84 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 85 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::UInt8"); 86 | } 87 | public class Ldelem_U2ConverterCXX : ICXXILConverter 88 | { 89 | public OpCode TargetOpCode() => OpCodes.Ldelem_U2; 90 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 91 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::UInt16"); 92 | } 93 | public class Ldelem_U4ConverterCXX : ICXXILConverter 94 | { 95 | public OpCode TargetOpCode() => OpCodes.Ldelem_U4; 96 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 97 | => LdelemConvert.Convert(instruction, methodContext, "RTCLI::System::UInt32"); 98 | } 99 | 100 | 101 | public class LdelemaConverterCXX : ICXXILConverter 102 | { 103 | public OpCode TargetOpCode() => OpCodes.Ldelema; 104 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 105 | { 106 | var op2 = methodContext.CmptStackPopObject; 107 | var op1 = methodContext.CmptStackPopObject; 108 | return $"auto {methodContext.CmptStackPushObject}" + 109 | $" = RTCLI::ArrayGet_Addr({op1}, {op2});"; 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/LdindConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class LdindConvert 11 | { 12 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, string type) 13 | { 14 | var op1 = methodContext.CmptStackPopObject; 15 | return $"auto {methodContext.CmptStackPushObject} = RTCLI::Deref<{type}>({op1});"; 16 | } 17 | } 18 | 19 | public class Ldind_RefConverterCXX : ICXXILConverter 20 | { 21 | public OpCode TargetOpCode() => OpCodes.Ldind_Ref; 22 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 23 | => LdindConvert.Convert(instruction, methodContext, ""); 24 | } 25 | 26 | public class Ldind_IConverterCXX : ICXXILConverter 27 | { 28 | public OpCode TargetOpCode() => OpCodes.Ldind_I; 29 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 30 | => LdindConvert.Convert(instruction, methodContext, "int"); 31 | } 32 | public class Ldind_I1ConverterCXX : ICXXILConverter 33 | { 34 | public OpCode TargetOpCode() => OpCodes.Ldind_I1; 35 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 36 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i8"); 37 | } 38 | public class Ldind_I2ConverterCXX : ICXXILConverter 39 | { 40 | public OpCode TargetOpCode() => OpCodes.Ldind_I2; 41 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 42 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i16"); 43 | } 44 | public class Ldind_I4ConverterCXX : ICXXILConverter 45 | { 46 | public OpCode TargetOpCode() => OpCodes.Ldind_I4; 47 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 48 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i32"); 49 | } 50 | public class Ldind_I8ConverterCXX : ICXXILConverter 51 | { 52 | public OpCode TargetOpCode() => OpCodes.Ldind_I8; 53 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 54 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::i64"); 55 | } 56 | 57 | public class Ldind_R4ConverterCXX : ICXXILConverter 58 | { 59 | public OpCode TargetOpCode() => OpCodes.Ldind_R4; 60 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 61 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::f32"); 62 | } 63 | public class Ldind_R8ConverterCXX : ICXXILConverter 64 | { 65 | public OpCode TargetOpCode() => OpCodes.Ldind_R8; 66 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 67 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::f64"); 68 | } 69 | 70 | public class Ldind_U1ConverterCXX : ICXXILConverter 71 | { 72 | public OpCode TargetOpCode() => OpCodes.Ldind_U1; 73 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 74 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::u8"); 75 | } 76 | public class Ldind_U2ConverterCXX : ICXXILConverter 77 | { 78 | public OpCode TargetOpCode() => OpCodes.Ldind_U2; 79 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 80 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::u16"); 81 | } 82 | public class Ldind_U4ConverterCXX : ICXXILConverter 83 | { 84 | public OpCode TargetOpCode() => OpCodes.Ldind_U4; 85 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 86 | => LdindConvert.Convert(instruction, methodContext, "RTCLI::u32"); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/LdlocConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class LdlocOnvert 11 | { 12 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, int index) 13 | { 14 | var type = methodContext.Method.Body.Variables[index].VariableType; 15 | return $"auto& {methodContext.CmptStackPushObject} = v{index}{(type.IsValueType ? "" : ".Get()")};"; 16 | } 17 | } 18 | 19 | public class Ldloc_0ConverterCXX : ICXXILConverter 20 | { 21 | public OpCode TargetOpCode() => OpCodes.Ldloc_0; 22 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 23 | => LdlocOnvert.Convert(instruction, methodContext, 0); 24 | } 25 | 26 | public class Ldloc_1ConverterCXX : ICXXILConverter 27 | { 28 | public OpCode TargetOpCode() => OpCodes.Ldloc_1; 29 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 30 | => LdlocOnvert.Convert(instruction, methodContext, 1); 31 | } 32 | public class Ldloc_2ConverterCXX : ICXXILConverter 33 | { 34 | public OpCode TargetOpCode() => OpCodes.Ldloc_2; 35 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 36 | => LdlocOnvert.Convert(instruction, methodContext, 2); 37 | } 38 | public class Ldloc_3ConverterCXX : ICXXILConverter 39 | { 40 | public OpCode TargetOpCode() => OpCodes.Ldloc_3; 41 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 42 | => LdlocOnvert.Convert(instruction, methodContext, 3); 43 | } 44 | public class LdlocConverterCXX : ICXXILConverter 45 | { 46 | public OpCode TargetOpCode() => OpCodes.Ldloc; 47 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 48 | => LdlocOnvert.Convert(instruction, methodContext, (instruction.Operand as VariableDefinition).Index); 49 | } 50 | public class Ldloc_SConverterCXX : ICXXILConverter 51 | { 52 | public OpCode TargetOpCode() => OpCodes.Ldloc_S; 53 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 54 | => LdlocOnvert.Convert(instruction, methodContext, (instruction.Operand as VariableDefinition).Index); 55 | } 56 | public class LdlocaConverterCXX : ICXXILConverter 57 | { 58 | public OpCode TargetOpCode() => OpCodes.Ldloca; 59 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 60 | => $"auto& {methodContext.CmptStackPushObject} = RTCLI_ADDRESSOF(v{(instruction.Operand as VariableDefinition).Index});"; 61 | } 62 | public class Ldloca_SConverterCXX : ICXXILConverter 63 | { 64 | public OpCode TargetOpCode() => OpCodes.Ldloca_S; 65 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 66 | => $"auto& {methodContext.CmptStackPushObject} = RTCLI_ADDRESSOF(v{(instruction.Operand as VariableDefinition).Index});"; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/StargConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class Starg_ConverterCXX : ICXXILConverter 11 | { 12 | public OpCode TargetOpCode() => OpCodes.Starg; 13 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 14 | => $"{methodContext.Method.Parameters[(instruction.Operand as ParameterDefinition).Index].Name} = {methodContext.CmptStackPopObject};"; 15 | } 16 | public class Starg_SConverterCXX : ICXXILConverter 17 | { 18 | public OpCode TargetOpCode() => OpCodes.Starg_S; 19 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 20 | => $"{methodContext.Method.Parameters[(instruction.Operand as ParameterDefinition).Index].Name} = {methodContext.CmptStackPopObject};"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/StelemConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class StelemConvert 11 | { 12 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, string type) 13 | { 14 | var op3 = methodContext.CmptStackPopObject; 15 | var op2 = methodContext.CmptStackPopObject; 16 | var op1 = methodContext.CmptStackPopObject; 17 | return $"RTCLI::ArraySet<{type}>({op1}, {op2}, {op3});"; 18 | } 19 | } 20 | 21 | public class StelemConverterCXX : ICXXILConverter 22 | { 23 | public OpCode TargetOpCode() => OpCodes.Stelem_Any; 24 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 25 | { 26 | var typeReference = instruction.Operand as TypeReference; 27 | return StelemConvert.Convert(instruction, methodContext, typeReference.CXXTypeName()); 28 | } 29 | } 30 | public class Stelem_RefConverterCXX : ICXXILConverter 31 | { 32 | public OpCode TargetOpCode() => OpCodes.Stelem_Ref; 33 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 34 | => StelemConvert.Convert(instruction, methodContext, ""); 35 | } 36 | 37 | public class Stelem_IConverterCXX : ICXXILConverter 38 | { 39 | public OpCode TargetOpCode() => OpCodes.Stelem_I; 40 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 41 | => StelemConvert.Convert(instruction, methodContext, "int"); 42 | } 43 | public class Stelem_I1ConverterCXX : ICXXILConverter 44 | { 45 | public OpCode TargetOpCode() => OpCodes.Stelem_I1; 46 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 47 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int8"); 48 | } 49 | public class Stelem_I2ConverterCXX : ICXXILConverter 50 | { 51 | public OpCode TargetOpCode() => OpCodes.Stelem_I2; 52 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 53 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int16"); 54 | } 55 | public class Stelem_I4ConverterCXX : ICXXILConverter 56 | { 57 | public OpCode TargetOpCode() => OpCodes.Stelem_I4; 58 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 59 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int32"); 60 | } 61 | public class Stelem_I8ConverterCXX : ICXXILConverter 62 | { 63 | public OpCode TargetOpCode() => OpCodes.Stelem_I8; 64 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 65 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Int64"); 66 | } 67 | 68 | public class Stelem_R4ConverterCXX : ICXXILConverter 69 | { 70 | public OpCode TargetOpCode() => OpCodes.Stelem_R4; 71 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 72 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Single"); 73 | } 74 | public class Stelem_R8ConverterCXX : ICXXILConverter 75 | { 76 | public OpCode TargetOpCode() => OpCodes.Stelem_R8; 77 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 78 | => StelemConvert.Convert(instruction, methodContext, "RTCLI::System::Double"); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/StindConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class StindConvert 11 | { 12 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, string type) 13 | { 14 | var op1 = methodContext.CmptStackPopObject; 15 | var op2 = methodContext.CmptStackPopObject; 16 | return $"RTCLI::Deref<{type}>({op2}) = {op1};"; 17 | } 18 | } 19 | 20 | public class Stind_RefConverterCXX : ICXXILConverter 21 | { 22 | public OpCode TargetOpCode() => OpCodes.Stind_Ref; 23 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 24 | => StindConvert.Convert(instruction, methodContext, ""); 25 | } 26 | 27 | public class Stind_IConverterCXX : ICXXILConverter 28 | { 29 | public OpCode TargetOpCode() => OpCodes.Stind_I; 30 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 31 | => StindConvert.Convert(instruction, methodContext, "int"); 32 | } 33 | public class Stind_I1ConverterCXX : ICXXILConverter 34 | { 35 | public OpCode TargetOpCode() => OpCodes.Stind_I1; 36 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 37 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i8"); 38 | } 39 | public class Stind_I2ConverterCXX : ICXXILConverter 40 | { 41 | public OpCode TargetOpCode() => OpCodes.Stind_I2; 42 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 43 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i16"); 44 | } 45 | public class Stind_I4ConverterCXX : ICXXILConverter 46 | { 47 | public OpCode TargetOpCode() => OpCodes.Stind_I4; 48 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 49 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i32"); 50 | } 51 | public class Stind_I8ConverterCXX : ICXXILConverter 52 | { 53 | public OpCode TargetOpCode() => OpCodes.Stind_I8; 54 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 55 | => StindConvert.Convert(instruction, methodContext, "RTCLI::i64"); 56 | } 57 | 58 | public class Stind_R4ConverterCXX : ICXXILConverter 59 | { 60 | public OpCode TargetOpCode() => OpCodes.Stind_R4; 61 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 62 | => StindConvert.Convert(instruction, methodContext, "RTCLI::f32"); 63 | } 64 | public class Stind_R8ConverterCXX : ICXXILConverter 65 | { 66 | public OpCode TargetOpCode() => OpCodes.Stind_R8; 67 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 68 | => StindConvert.Convert(instruction, methodContext, "RTCLI::f64"); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/CXX/StlocConvertersCXX.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using Mono.Cecil; 3 | using System; 4 | using System.Linq; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using System.Collections.Generic; 7 | 8 | namespace RTCLI.AOTCompiler3.ILConverters 9 | { 10 | public class StlocConvert 11 | { 12 | public static string Convert(Instruction instruction, MethodTranslateContextCXX methodContext, int index) 13 | { 14 | return $"v{index} = {methodContext.CmptStackPopObject};"; 15 | } 16 | } 17 | public class Stloc_0ConverterCXX : ICXXILConverter 18 | { 19 | public OpCode TargetOpCode() => OpCodes.Stloc_0; 20 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 21 | => StlocConvert.Convert(instruction, methodContext, 0); 22 | } 23 | public class Stloc_1ConverterCXX : ICXXILConverter 24 | { 25 | public OpCode TargetOpCode() => OpCodes.Stloc_1; 26 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 27 | => StlocConvert.Convert(instruction, methodContext, 1); 28 | } 29 | public class Stloc_2ConverterCXX : ICXXILConverter 30 | { 31 | public OpCode TargetOpCode() => OpCodes.Stloc_2; 32 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 33 | => StlocConvert.Convert(instruction, methodContext, 2); 34 | } 35 | public class Stloc_3ConverterCXX : ICXXILConverter 36 | { 37 | public OpCode TargetOpCode() => OpCodes.Stloc_3; 38 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 39 | => StlocConvert.Convert(instruction, methodContext, 3); 40 | } 41 | public class Stloc_SConverterCXX : ICXXILConverter 42 | { 43 | public OpCode TargetOpCode() => OpCodes.Stloc_S; 44 | public string Convert(Instruction instruction, MethodTranslateContextCXX methodContext) 45 | => StlocConvert.Convert(instruction, methodContext, (instruction.Operand as VariableDefinition).Index); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/ILConverter/ILConverter.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System.Dynamic; 3 | using RTCLI.AOTCompiler3.Translators; 4 | 5 | namespace RTCLI.AOTCompiler3.ILConverters 6 | { 7 | public interface IILConverter 8 | { 9 | OpCode TargetOpCode(); 10 | } 11 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Meta/AssemblyRepo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using Mono.Cecil; 7 | using RTCLI.AOTCompiler3.Meta; 8 | 9 | namespace RTCLI.AOTCompiler3 10 | { 11 | public static class AssemblyRepo 12 | { 13 | public static AssemblyDefinition Store(string assemblyName, ReaderParameters parameter) 14 | { 15 | return ReadAssemblyRecursively(assemblyName, parameter); 16 | } 17 | 18 | static AssemblyRepo() 19 | { 20 | assemblyFindDir.Add(""); 21 | assemblyFindDir.Add(Directory.GetCurrentDirectory()); 22 | } 23 | 24 | private static AssemblyDefinition ReadAssemblyRecursively( 25 | string assemblyName, ReaderParameters parameter) 26 | { 27 | var pth = Path.GetDirectoryName(assemblyName); 28 | if(Directory.Exists(pth)) 29 | { 30 | System.Console.WriteLine("Add " + pth + " to Assembly Find Dir."); 31 | assemblyFindDir.Add(pth); 32 | } 33 | AssemblyDefinition AssemblyLoadedRecursively = null; 34 | AssemblyLoadedRecursively = ReadAssemblyFromDisk(assemblyName, parameter); 35 | if (AssemblyLoadedRecursively != null) 36 | { 37 | var AssemblyLoadedRecursivelyName = AssemblyLoadedRecursively.RTCLIShortName(); 38 | 39 | if (GlobalAssemblies.ContainsKey(AssemblyLoadedRecursivelyName)) 40 | { 41 | // Skip... 42 | } 43 | else 44 | { 45 | System.Console.WriteLine(AssemblyLoadedRecursivelyName + " Loaded!"); 46 | GlobalAssemblies.Add(AssemblyLoadedRecursivelyName, AssemblyLoadedRecursively); 47 | 48 | ReaderParameters sys_parameter = new ReaderParameters 49 | { 50 | AssemblyResolver = parameter.AssemblyResolver, 51 | ReadSymbols = false 52 | }; 53 | 54 | foreach (var module in AssemblyLoadedRecursively.Modules) 55 | { 56 | var references = module.AssemblyReferences; 57 | foreach (var reference in references) 58 | { 59 | ReadAssemblyRecursively(reference.Name + ".dll", parameter); 60 | } 61 | } 62 | } 63 | } 64 | return AssemblyLoadedRecursively; 65 | } 66 | 67 | private static AssemblyDefinition ReadAssemblyFromDisk(string assemblyName, ReaderParameters parameter) 68 | { 69 | AssemblyDefinition loaded = null; 70 | foreach (var dir in assemblyFindDir) 71 | { 72 | if (File.Exists(Path.Combine(dir, assemblyName))) 73 | { 74 | loaded = AssemblyDefinition.ReadAssembly(Path.Combine(dir, assemblyName), parameter); 75 | if (loaded != null) 76 | return loaded; 77 | } 78 | } 79 | return loaded; 80 | } 81 | 82 | private static List assemblyFindDir = new List(); 83 | public static readonly Dictionary GlobalAssemblies 84 | = new Dictionary(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Meta/RTCLIAssembly.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | 6 | namespace RTCLI.AOTCompiler3.Meta 7 | { 8 | public static class RTCLIAssembly 9 | { 10 | public static string RTCLIShortName(this AssemblyDefinition assembly) 11 | { 12 | return assembly.Name.Name.Replace('.', '_'); 13 | } 14 | 15 | public static string RTCLIShortName(this AssemblyNameReference assembly) 16 | { 17 | return assembly.Name.Replace('.', '_'); 18 | } 19 | 20 | public static string RTCLIFullName(this AssemblyDefinition assembly) 21 | { 22 | return assembly.FullName; 23 | } 24 | 25 | } 26 | 27 | public sealed class BasePathAssemblyResolver : IAssemblyResolver 28 | { 29 | private readonly DefaultAssemblyResolver resolver = new DefaultAssemblyResolver(); 30 | 31 | public BasePathAssemblyResolver(string basePath) 32 | { 33 | resolver.AddSearchDirectory(basePath); 34 | } 35 | 36 | public void Dispose() 37 | { 38 | resolver.Dispose(); 39 | } 40 | 41 | public AssemblyDefinition Resolve(AssemblyNameReference name) 42 | { 43 | return resolver.Resolve(name); 44 | } 45 | 46 | public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) 47 | { 48 | return resolver.Resolve(name, parameters); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Options.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace RTCLI.AOTCompiler3 4 | { 5 | public enum DebugInformationOptions 6 | { 7 | None, 8 | CommentOnly, 9 | Full 10 | } 11 | 12 | public enum TargetPlatforms 13 | { 14 | NativeCpp, 15 | InterpreterCode, 16 | Generic = NativeCpp 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/RTCLI.AOTCompiler3.Core.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Theory/CXXXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace RTCLI.AOTCompiler3 6 | { 7 | public class CAttribute : System.Attribute 8 | { 9 | public CAttribute() 10 | { 11 | 12 | } 13 | } 14 | 15 | // doc: https://www.yuque.com/oy5oo6/su8qgw/mr3l89 16 | public class C0001 : CAttribute 17 | { 18 | public C0001() 19 | { 20 | 21 | } 22 | static string description => "value type"; 23 | } 24 | 25 | // doc: https://www.yuque.com/oy5oo6/su8qgw/teuw4w 26 | public class C0002 : CAttribute 27 | { 28 | public C0002() 29 | { 30 | 31 | } 32 | static string description => "interface"; 33 | } 34 | 35 | // doc: https://www.yuque.com/oy5oo6/su8qgw/hx1ev5 36 | public class C0003 : CAttribute 37 | { 38 | public C0003() 39 | { 40 | 41 | } 42 | static string description => "class"; 43 | } 44 | 45 | // doc: https://www.yuque.com/oy5oo6/su8qgw/lh5pw3 46 | public class C0004 : CAttribute 47 | { 48 | public C0004() 49 | { 50 | 51 | } 52 | static string description => "generic"; 53 | } 54 | 55 | // doc: https://www.yuque.com/oy5oo6/su8qgw/cb16dg 56 | public class C1001 : CAttribute 57 | { 58 | public C1001() { } 59 | static string description => "Strong Reference"; 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Theory/HXXXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public class HAttribute : System.Attribute 9 | { 10 | public HAttribute() {} 11 | } 12 | 13 | // doc: https://www.yuque.com/oy5oo6/su8qgw/hn6b42 14 | public class H0000 : HAttribute 15 | { 16 | public H0000() {} 17 | static string description => "Include Protect"; 18 | } 19 | 20 | // doc: https://www.yuque.com/oy5oo6/su8qgw/repik3 21 | public class H0001 : HAttribute 22 | { 23 | public H0001() {} 24 | static string description => "Forward Declaration"; 25 | } 26 | 27 | // doc: https://www.yuque.com/oy5oo6/su8qgw/xf2tgm 28 | public class H1000 : HAttribute 29 | { 30 | public H1000() {} 31 | static string description => "Uber Header"; 32 | } 33 | 34 | // doc: https://www.yuque.com/oy5oo6/su8qgw/nflly8 35 | public class H1001 : HAttribute 36 | { 37 | public H1001() {} 38 | static string description => "Include Strong References"; 39 | } 40 | 41 | 42 | 43 | // doc: https://www.yuque.com/oy5oo6/su8qgw/mzg1ps 44 | public class H2000 : HAttribute 45 | { 46 | public H2000() {} 47 | static string description => "Type Scope"; 48 | } 49 | 50 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ahm9xi 51 | public class H2001 : HAttribute 52 | { 53 | public H2001() {} 54 | static string description => "Method Signatures"; 55 | } 56 | 57 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ykpc5k 58 | public class H2003 : HAttribute 59 | { 60 | public H2003() {} 61 | static string description => "namespace"; 62 | } 63 | 64 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ui239g 65 | public class H2002 : HAttribute 66 | { 67 | public H2002() {} 68 | static string description => "Inner Types"; 69 | } 70 | 71 | // doc: https://www.yuque.com/oy5oo6/su8qgw/bg2gtq 72 | public class H2004 : HAttribute 73 | { 74 | public H2004() {} 75 | static string description => "Boxed ValueType"; 76 | } 77 | 78 | // doc: https://www.yuque.com/oy5oo6/su8qgw/su3nw1 79 | public class H2005 : HAttribute 80 | { 81 | public H2005() {} 82 | static string description => "Field Declaration"; 83 | } 84 | 85 | public class H9999 : HAttribute 86 | { 87 | public H9999() {} 88 | static string description => "Copyright"; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Theory/SXXXX.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public class SAttribute : System.Attribute 9 | { 10 | public SAttribute() 11 | { 12 | 13 | } 14 | } 15 | 16 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ggxmal 17 | public class S0001 : SAttribute 18 | { 19 | public S0001() 20 | { 21 | 22 | } 23 | static string description => "Close Unused-Label Warnings"; 24 | } 25 | 26 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ggxmal 27 | public class S1000 : SAttribute 28 | { 29 | public S1000() 30 | { 31 | 32 | } 33 | static string description => "Include Uber Headers"; 34 | } 35 | 36 | // doc: https://www.yuque.com/oy5oo6/su8qgw/vg0364 37 | public class S1001 : SAttribute 38 | { 39 | public S1001() 40 | { 41 | 42 | } 43 | static string description => "Include Weak References"; 44 | } 45 | 46 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ggxmal 47 | public class S2000 : SAttribute 48 | { 49 | public S2000() 50 | { 51 | 52 | } 53 | static string description => "Method Body"; 54 | } 55 | 56 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ggxmal 57 | public class S2001 : SAttribute 58 | { 59 | public S2001() 60 | { 61 | 62 | } 63 | static string description => "Static Field Implementation"; 64 | } 65 | 66 | // doc: https://www.yuque.com/oy5oo6/su8qgw/ggxmal 67 | public class S9999 : SAttribute 68 | { 69 | public S9999() 70 | { 71 | 72 | } 73 | static string description => "Copyright"; 74 | } 75 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Translators/CXX/CXXHeaderTranslator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using Mono.Cecil; 4 | using RTCLI.AOTCompiler3.Meta; 5 | using System.Linq; 6 | 7 | namespace RTCLI.AOTCompiler3.Translators 8 | { 9 | public class CXXHeaderTranslator : IRTCLITranslator 10 | { 11 | // ${OutputPath}/${Assembly}/include 12 | public void Run(CodeTextStorage Storage, AssemblyDefinition FocusedAssembly) 13 | { 14 | // [H1000] UberHeader 15 | CXXHeaderRules.GenerateUberHeader(Storage, FocusedAssembly); 16 | 17 | foreach (var Module in FocusedAssembly.Modules) 18 | { 19 | foreach(var Type in Module.Types) 20 | { 21 | var Writer = Storage.Wirter(Type.CXXHeaderPath()); 22 | // [H9999] Copyright 23 | CXXHeaderRules.CopyWrite(Writer); 24 | // [H0000] Include Protect 25 | CXXHeaderRules.WriteIncludeProtect(Writer); 26 | if (Type.IsPrimitive) 27 | continue; 28 | 29 | Writer.WriteLine(EnvIncludes); 30 | // [H1001] Strong Reference Type Headers 31 | CXXHeaderRules.IncludeStrongReferences(Writer, Type); 32 | 33 | Writer.WriteLine(); 34 | 35 | // [H0001] Forward Declaration 36 | CXXHeaderRules.WriteForwardDeclaration(Writer, Type); 37 | Writer.WriteLine(); 38 | 39 | // [H2003] namespace 40 | using (var ___ = new CXXNamespaceScope(Writer, Type.CXXNamespace())) 41 | { 42 | WriteTypeRecursively(Writer, Type); 43 | } 44 | Writer.Flush(); 45 | } 46 | } 47 | } 48 | 49 | [H2002()] 50 | private void WriteTypeRecursively(CodeTextWriter codeWriter, TypeDefinition type) 51 | { 52 | if(type.IsEnum) 53 | { 54 | CXXHeaderRules.WriteEnumType(codeWriter, type); 55 | return; 56 | } 57 | 58 | // [C0004] generic 59 | if (type.HasGenericParameters) 60 | codeWriter.WriteLine(CXXHeaderRules.GenericDeclaration(type)); 61 | 62 | // [H2000] Type Scope 63 | using (var typeScope = new CXXTypeScope(codeWriter, type)) 64 | { 65 | codeWriter.unindent().WriteLine("public:").indent(); 66 | foreach (var nested in type.NestedTypes) 67 | { 68 | // [H2002] Inner Types 69 | codeWriter.WriteLine($"// [H2002] Inner Types {nested.CXXShortTypeName()}"); 70 | WriteTypeRecursively(codeWriter, nested); 71 | } 72 | // [H2001] Method Signatures 73 | CXXHeaderRules.WriteMethodSignatures(codeWriter, type, type.IsValueType); 74 | 75 | // [H2005] Field Declaration 76 | CXXHeaderRules.WriteFieldDeclaration(codeWriter, type); 77 | } 78 | 79 | // [H2004] Boxed ValueType 80 | if (!type.IsValueType) 81 | return; 82 | CXXHeaderRules.WriteBoxedValueType(codeWriter, type); 83 | } 84 | 85 | private string EnvIncludes => "#include "; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Translators/CXX/CXXPathHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using Mono.Cecil; 5 | using RTCLI.AOTCompiler3.Meta; 6 | 7 | namespace RTCLI.AOTCompiler3.Translators 8 | { 9 | public static class TypeCXXPathHelper 10 | { 11 | public static string CXXHeaderPath(this TypeReference typeReference) 12 | { 13 | var T = typeReference; 14 | if(typeReference.IsGenericInstance) 15 | { 16 | T = typeReference.GetElementType(); 17 | } 18 | return Path.Combine(T.CXXNamespaceToPath(), T.CXXShortTypeName() + ".h").Replace("\\", "/"); 19 | } 20 | 21 | public static string CXXUberHeaderPath(this TypeReference typeReference) 22 | { 23 | return typeReference.Module.Assembly.CXXUberHeaderPath(); 24 | } 25 | } 26 | 27 | public static class AssemblyCXXPathHelper 28 | { 29 | public static string CXXUberHeaderPath(this AssemblyDefinition assembly) 30 | { 31 | return Path.Combine(assembly.RTCLIShortName(), $"include/{Constants.CXXUberHeaderName}").Replace("\\", "/"); 32 | } 33 | 34 | public static string CXXUberHeaderPath(this AssemblyNameReference assembly) 35 | { 36 | return Path.Combine(assembly.RTCLIShortName(), $"include/{Constants.CXXUberHeaderName}").Replace("\\", "/"); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Translators/CXX/CXXScopeDisposer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace RTCLI.AOTCompiler3.Translators 6 | { 7 | public class CXXScopeDisposer : IDisposable 8 | { 9 | private CodeTextWriter parent; 10 | private bool EndWithSemicolon = false; 11 | private string onExit = null; 12 | public CXXScopeDisposer(CodeTextWriter parent, string Scope, bool bEndWithSemicolon = false, string onEnter = null, string onExit = null) 13 | { 14 | this.parent = parent; 15 | this.EndWithSemicolon = bEndWithSemicolon; 16 | this.onExit = onExit; 17 | if (onEnter != null) parent.WriteLine(onEnter); 18 | parent.WriteLine(Scope); 19 | parent.WriteLine("{"); 20 | parent.indent(); 21 | } 22 | 23 | public void Dispose() 24 | { 25 | if (parent != null) 26 | { 27 | parent.unindent(); 28 | parent.WriteLine("}" + (EndWithSemicolon ? ";" : "")); 29 | if (onExit != null) parent.WriteLine(onExit); 30 | parent.WriteLine(); 31 | parent = null; 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Translators/CXX/CXXSourceTranslator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.IO; 5 | using Mono.Cecil; 6 | using RTCLI.AOTCompiler3.Meta; 7 | 8 | namespace RTCLI.AOTCompiler3.Translators 9 | { 10 | public class CXXSourceTranslator : IRTCLITranslator 11 | { 12 | // ${OutputPath}/${Assembly}/src 13 | public void Run(CodeTextStorage Storage, AssemblyDefinition FocusedAssembly) 14 | { 15 | foreach (var Module in FocusedAssembly.Modules) 16 | { 17 | foreach (var Type in Module.Types) 18 | { 19 | var Writer = Storage.Wirter(Path.Combine(Type.CXXNamespaceToPath(), Type.CXXShortTypeName() + ".cpp")); 20 | // [S9999] Copyright 21 | CXXSourceRules.Copyright(Writer); 22 | Writer.WriteLine($"#include "); 23 | if (Type.IsPrimitive) 24 | continue; 25 | 26 | if(Constants.CXXUseUberHeader) 27 | { 28 | // [S1000] Include Uber Headers. 29 | CXXSourceRules.IncludeUberHeaders(Writer, Type); 30 | } 31 | else 32 | { 33 | CXXSourceRules.IncludeWeakReferences(Writer, Type); 34 | } 35 | 36 | // [S0001] Close Unused-Label Warning 37 | using (var no_unused_lables = new ScopeNoUnusedWarning(Writer)) 38 | { 39 | // [S2001] Static Field Implementation 40 | CXXSourceRules.WriteStaticFieldImplementation(Writer, Type); 41 | 42 | Writer.WriteLine(""); 43 | WriteMethodRecursive(Writer, Type, Type.IsValueType); 44 | if(Type.IsValueType) 45 | WriteMethodRecursive(Writer, Type, false); 46 | Writer.WriteLine(""); 47 | 48 | 49 | Writer.WriteLine(""); 50 | Writer.WriteLine("// [S0000] Generate Test Point."); 51 | Writer.WriteLine($"#ifdef RTCLI_TEST_POINT"); 52 | Writer.WriteLine("int main(void){"); 53 | Writer.WriteLine($"\t{Type.CXXTypeName()}::Test();"); 54 | Writer.WriteLine("\treturn 0;"); 55 | Writer.WriteLine("}"); 56 | Writer.WriteLine($"#endif"); 57 | } 58 | 59 | Writer.Flush(); 60 | } 61 | } 62 | } 63 | 64 | public void WriteMethodRecursive(CodeTextWriter Writer, TypeDefinition Type, bool ValueType) 65 | { 66 | foreach (var Nested in Type.NestedTypes) 67 | { 68 | WriteMethodRecursive(Writer, Nested, ValueType); 69 | } 70 | foreach (var Method in Type.Methods) 71 | { 72 | if (Method.Body == null) 73 | continue; 74 | // [S2000] Method Body 75 | List overrided = new List(); 76 | if (!ValueType && Method.HasOverrides && Method.IsVirtual) 77 | { 78 | foreach (var od in Method.Overrides) 79 | { 80 | var odd = od.Resolve(); 81 | overrided.Add(odd); 82 | if (Type.HasGenericParameters) 83 | Writer.WriteLine($"template<{Type.CXXTemplateParam()}>"); 84 | Writer.WriteLine(odd.CXXMethodImplSignature(ValueType, Type)); 85 | CXXSourceRules.WriteMethodBody(Writer, Method, ValueType); 86 | } 87 | } 88 | else if(!ValueType && Method.IsVirtual) 89 | { 90 | if (Type.HasGenericParameters) 91 | Writer.WriteLine($"template<{Type.CXXTemplateParam()}>"); 92 | #if ENABLE_EXPLICT_OVERRIDE 93 | Writer.WriteLine($"{Method.CXXRetType()} {Type.CXXMethodDeclarePrefix(ValueType)}::{Method.CXXRowName()}_Impl{Method.CXXParamSequence(false)}"); 94 | CXXSourceRules.WriteMethodBody(Writer, Method, ValueType); 95 | if (Method.IsNewSlot) 96 | { 97 | if (Type.HasGenericParameters) 98 | Writer.WriteLine($"template<{Type.CXXTemplateParam()}>"); 99 | Writer.WriteLine(Method.CXXMethodImplSignature(ValueType)); 100 | Writer.WriteLine($"{{ return {Method.CXXRowName()}_Impl{Method.CXXArgSequence()}; }}"); 101 | } 102 | if (!Type.IsInterface) 103 | { 104 | foreach (var i in Type.Interfaces) 105 | { 106 | var itype = i.InterfaceType.Resolve(); 107 | foreach (var mtdd in itype.Methods) 108 | { 109 | if (mtdd.Name == Method.Name && !overrided.Contains(mtdd)) 110 | { 111 | if (Type.HasGenericParameters) 112 | Writer.WriteLine($"template<{Type.CXXTemplateParam()}>"); 113 | Writer.WriteLine(mtdd.CXXMethodImplSignature(ValueType, Type)); 114 | Writer.WriteLine($"{{ return {Method.CXXRowName()}_Impl{Method.CXXArgSequence()}; }}"); 115 | } 116 | } 117 | } 118 | } 119 | #else 120 | Writer.WriteLine($"{Method.CXXRetType()} {Type.CXXMethodDeclarePrefix(ValueType)}::{Method.CXXRowName()}{Method.CXXParamSequence(false)}"); 121 | CXXSourceRules.WriteMethodBody(Writer, Method, ValueType); 122 | #endif 123 | } 124 | else 125 | { 126 | if (Type.HasGenericParameters) 127 | Writer.WriteLine($"template<{Type.CXXTemplateParam()}>"); 128 | if (Method.HasGenericParameters) 129 | Writer.WriteLine($"template<{Method.CXXTemplateParam()}>"); 130 | 131 | Writer.WriteLine(Method.CXXMethodImplSignature(ValueType)); 132 | 133 | CXXSourceRules.WriteMethodBody(Writer, Method, ValueType); 134 | } 135 | 136 | } 137 | } 138 | 139 | private string EnvIncludes => "#include "; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Translators/IRTCLITranslator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Cecil; 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public interface IRTCLITranslator 9 | { 10 | public void Run(CodeTextStorage storage, AssemblyDefinition assembly); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3.Core/Utilities.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil.Cil; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public static class Utilities 9 | { 10 | public static string GetCXXLanguageScopedPath(IEnumerable scopeNames) => 11 | string.Join("/", scopeNames.SelectMany(sn => sn.Split('.'))); 12 | 13 | public static string GetCXXLanguageScopedPath(params string[] scopeNames) => 14 | GetCXXLanguageScopedPath((IEnumerable)scopeNames); 15 | 16 | public static string GetCXXValidTokenString(string raw_token) => 17 | raw_token.Replace('<', '_').Replace('>', '_'); 18 | 19 | public static string GetLabel(this Instruction instruction) 20 | { 21 | return instruction.ToString().Split(":")[0]; 22 | } 23 | 24 | public static string HoldEscape(this string source) 25 | { 26 | return source.Replace("\\", "\\\\").Replace("\n", "\\n") 27 | .Replace("\'", "\\'").Replace("\"", "\\\"") 28 | .Replace("\0", "\\0").Replace("\a", "\\a") 29 | .Replace("\b", "\\b").Replace("\f", "\\f") 30 | .Replace("\r", "\\r").Replace("\t", "\\t") 31 | .Replace("\v", "\\v"); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3/Dispatcher.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using System.Threading.Tasks; 4 | using Mono.Cecil; 5 | using RTCLI.AOTCompiler3.Meta; 6 | using RTCLI.AOTCompiler3.Translators; 7 | 8 | namespace RTCLI.AOTCompiler3 9 | { 10 | // ${OutputPath}/${Assembly}/[include/src]/${namespaces}.../${classname}.h/cpp 11 | public static class Dispatcher 12 | { 13 | public static void Translate( 14 | CodeTextStorage storage, 15 | DispatchArgs dispatchArgs, 16 | AssemblyDefinition assembly) 17 | { 18 | // ${OutputPath}/${Assembly} 19 | using (var _ = storage.EnterScope(assembly.RTCLIShortName())) 20 | { 21 | // ${OutputPath}/${Assembly}/include 22 | using (var _h = storage.EnterScope("include")) 23 | { 24 | CXXHeaderTranslator translator = new CXXHeaderTranslator(); 25 | translator.Run(storage, assembly); 26 | } 27 | } 28 | using (var _ = storage.EnterScope(assembly.RTCLIShortName())) 29 | { 30 | // ${OutputPath}/${Assembly}/src 31 | using (var _cpp = storage.EnterScope("src")) 32 | { 33 | CXXSourceTranslator translator = new CXXSourceTranslator(); 34 | translator.Run(storage, assembly); 35 | } 36 | } 37 | System.Console.WriteLine($"Processing {assembly.RTCLIShortName()} done."); 38 | } 39 | 40 | public static void TranslateAll( 41 | TextWriter logw, 42 | string outputPath, 43 | DispatchArgs dispatchArgs, 44 | IEnumerable assemblyPaths) 45 | { 46 | Dictionary assemblyPathMapping 47 | = new Dictionary(); 48 | // Collect all assemblies 49 | foreach (var path in assemblyPaths) 50 | { 51 | var resolver = new BasePathAssemblyResolver(Path.GetDirectoryName(path)); 52 | var parameter = new ReaderParameters 53 | { 54 | AssemblyResolver = resolver, 55 | ReadSymbols = dispatchArgs.readSymbols 56 | }; 57 | if(!assemblyPathMapping.ContainsKey(path)) 58 | { 59 | assemblyPathMapping[path] = AssemblyRepo.Store(path, parameter); 60 | } 61 | } 62 | 63 | if(dispatchArgs.recursivelyCompileAll) 64 | { 65 | Parallel.ForEach(AssemblyRepo.GlobalAssemblies, assembly => 66 | { 67 | var storage = new CodeTextStorage( 68 | logw, 69 | outputPath, 70 | " "); 71 | { 72 | Translate( 73 | storage, 74 | dispatchArgs, 75 | assembly.Value); 76 | } 77 | });; 78 | } 79 | else 80 | { 81 | // Translate 82 | Parallel.ForEach(assemblyPaths, aseemblyPath => { 83 | if (!assemblyPathMapping.ContainsKey(aseemblyPath)) 84 | { 85 | 86 | } 87 | else 88 | { 89 | var assembly = assemblyPathMapping[aseemblyPath]; 90 | var storage = new CodeTextStorage( 91 | logw, 92 | outputPath, 93 | " "); 94 | { 95 | Translate( 96 | storage, 97 | dispatchArgs, 98 | assembly); 99 | } 100 | } 101 | }); 102 | } 103 | } 104 | 105 | public static void TranslateAll( 106 | TextWriter logw, 107 | string outputPath, 108 | DispatchArgs dispatchArgs, 109 | params string[] assemblyPaths) 110 | { 111 | TranslateAll( 112 | logw, 113 | outputPath, 114 | dispatchArgs, 115 | (IEnumerable)assemblyPaths 116 | ); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Runtime.InteropServices; 4 | using Mono.Options; 5 | 6 | namespace RTCLI.AOTCompiler3 7 | { 8 | public class DispatchArgs 9 | { 10 | public DebugInformationOptions debugInformationOptions = DebugInformationOptions.None; 11 | public bool readSymbols = true; 12 | public bool enableBundler = false; 13 | public TargetPlatforms targetPlatform = TargetPlatforms.Generic; 14 | public bool cxxStaticAssertOnUnimplementatedILs = false; 15 | public bool recursivelyCompileAll = false; 16 | } 17 | 18 | class Program 19 | { 20 | public static int Main(string[] args) 21 | { 22 | try 23 | { 24 | DispatchArgs dispatchArgs = new DispatchArgs(); 25 | var help = false; 26 | 27 | var options = new OptionSet() 28 | { 29 | { "g1|debug", "Emit debug informations (contains only comments)", v => dispatchArgs.debugInformationOptions = DebugInformationOptions.CommentOnly }, 30 | { "g|g2|debug-full", "Emit debug informations (contains line numbers)", v => dispatchArgs.debugInformationOptions = DebugInformationOptions.Full }, 31 | { "no-read-symbols", "NO read symbol files", _ => dispatchArgs.readSymbols = false }, 32 | { "bundler", "Produce bundler source file", _ => dispatchArgs.enableBundler = true }, 33 | { "target=", "Target platform [NativeCpp|InterpreterCode]", v => dispatchArgs.targetPlatform = Enum.TryParse(v, true, out var t) ? t : TargetPlatforms.Generic }, 34 | { "h|help", "Print this help", _ => help = true }, 35 | { "r|recursive", "Recursively Compile All Depended Assemblies", _ => dispatchArgs.recursivelyCompileAll = true }, 36 | { "assert-with-il-unimpl", "Gen CXX Static Assert On Unimplementated ILs", _ => dispatchArgs.cxxStaticAssertOnUnimplementatedILs = true }, 37 | }; 38 | 39 | var extra = options.Parse(args); 40 | if (help || (extra.Count < 2)) 41 | { 42 | Console.Out.WriteLine("usage: RTCLI.AOTCompiler.exe [options]"); 43 | options.WriteOptionDescriptions(Console.Out); 44 | } 45 | else 46 | { 47 | var outputPath = extra[0]; 48 | var assemblyPaths = extra.Skip(1); 49 | dispatchArgs.readSymbols = false; 50 | 51 | Dispatcher.TranslateAll( 52 | Console.Out, 53 | outputPath, 54 | dispatchArgs, 55 | assemblyPaths 56 | ); 57 | } 58 | 59 | return 0; 60 | } 61 | catch (OptionException ex) 62 | { 63 | Console.Error.WriteLine(ex.Message); 64 | return Marshal.GetHRForException(ex); 65 | } 66 | catch (Exception ex) 67 | { 68 | Console.Error.WriteLine(ex); 69 | return Marshal.GetHRForException(ex); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "RTCLI.AOTCompiler3": { 4 | "commandName": "Project", 5 | "commandLineArgs": "-g1 \"$(SolutionDir)RTCLI.CXXTest/RTCLI.Generated\" \"$(SolutionDir)RTCLI.TestCase/bin/Debug/netstandard2.1/TestCase.dll\" \"$(SolutionDir)RTCLI.TestCase.Reference/bin/Debug/netstandard2.1/TestCase.Reference.dll\"" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /RTCLI.AOTCompiler3/RTCLI.AOTCompiler3.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /RTCLI.CXXTest/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "compare": "cpp", 4 | "concepts": "cpp", 5 | "exception": "cpp", 6 | "limits": "cpp", 7 | "new": "cpp", 8 | "tuple": "cpp", 9 | "type_traits": "cpp", 10 | "utility": "cpp", 11 | "xmemory": "cpp", 12 | "xstddef": "cpp", 13 | "xstring": "cpp", 14 | "xtr1common": "cpp", 15 | "xutility": "cpp" 16 | } 17 | } -------------------------------------------------------------------------------- /RTCLI.CXXTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.19.0) 2 | 3 | set(CMAKE_C_STANDARD 11) 4 | set(CMAKE_CXX_STANDARD 17) 5 | if(WIN32) 6 | set(CMAKE_PREFIX_PATH "C:/Program Files (x86)") 7 | endif() 8 | 9 | project(RTCLI_CXXTest) 10 | 11 | set(GEN_TEST TRUE) 12 | 13 | find_package(RTCLIDefault 0.0.1 REQUIRED) 14 | link_libraries(RTCLI::RTCLIDefault) 15 | include_directories(RTCLI.Generated) 16 | 17 | FILE(GLOB modules 18 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/* 19 | ) 20 | 21 | # build all assemblies 22 | FOREACH(module ${modules}) 23 | message(STATUS ${module}) 24 | file(GLOB_RECURSE 25 | inc_list 26 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.h 27 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.hpp 28 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.hxx 29 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.inl 30 | ) 31 | file(GLOB_RECURSE 32 | source_list 33 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.c 34 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.cpp 35 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/*.cxx 36 | ) 37 | add_library(${module} ${inc_list} ${source_list}) 38 | target_include_directories(${module} 39 | PUBLIC 40 | ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/include 41 | ) 42 | 43 | if(GEN_TEST) 44 | FOREACH(test ${source_list}) 45 | get_filename_component(TARGET_NAME ${test} NAME_WLE) 46 | set(TARGET_NAME ${module}_${TARGET_NAME}) 47 | message(STATUS ${TARGET_NAME}) 48 | 49 | add_executable(${TARGET_NAME} ${test}) 50 | target_compile_definitions(${TARGET_NAME} PRIVATE "-DRTCLI_TEST_POINT") 51 | target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/RTCLI.Generated/${module}/include) 52 | ENDFOREACH() 53 | endif(GEN_TEST) 54 | ENDFOREACH() 55 | 56 | #add_definitions("-DRTCLI_TEST_POINT") 57 | -------------------------------------------------------------------------------- /RTCLI.CXXTest/gen_vs2017.bat: -------------------------------------------------------------------------------- 1 | cmake -G "Visual Studio 15 2017 Win64" -S ./ -B ./build/ 2 | pause -------------------------------------------------------------------------------- /RTCLI.CXXTest/gen_vs2019.bat: -------------------------------------------------------------------------------- 1 | cmake -G "Visual Studio 16 2019" -S ./ -B ./build/ 2 | pause -------------------------------------------------------------------------------- /RTCLI.CXXTest/gen_xcode.sh: -------------------------------------------------------------------------------- 1 | cmake -G "Xcode" -S ./ -B ./build/ 2 | read -n 1 -p "Press any key to continue..." -------------------------------------------------------------------------------- /RTCLI.Standard/mscorlib.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Team-RTCLI/RTCLI.AOTCompiler/9af1bcf3e4b595edfdc0c672541bd760a1491de1/RTCLI.Standard/mscorlib.dll -------------------------------------------------------------------------------- /RTCLI.Standard/netstandard.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Team-RTCLI/RTCLI.AOTCompiler/9af1bcf3e4b595edfdc0c672541bd760a1491de1/RTCLI.Standard/netstandard.dll -------------------------------------------------------------------------------- /RTCLI.Standard/netstandard_2_1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Team-RTCLI/RTCLI.AOTCompiler/9af1bcf3e4b595edfdc0c672541bd760a1491de1/RTCLI.Standard/netstandard_2_1 -------------------------------------------------------------------------------- /RTCLI.TestCase.Reference/AsssemblyReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class RawC 4 | { 5 | 6 | } 7 | 8 | namespace TestCase.Reference 9 | { 10 | public interface BaseInterface 11 | { 12 | string Note() => "aa"; 13 | string Name { get; } 14 | } 15 | 16 | public interface IGetName : BaseInterface 17 | { 18 | new string Note() => "aaa"; 19 | 20 | public class InAnInterface 21 | { 22 | int val; 23 | } 24 | } 25 | 26 | public class RefClass : IGetName 27 | { 28 | string Note() => "aaaa"; 29 | public RefClass(string name) 30 | { 31 | this.Name = name; 32 | } 33 | public void CallTest(int u) 34 | { 35 | System.Console.WriteLine("Call Test"); 36 | return; 37 | } 38 | public void CallTestF(float u) 39 | { 40 | System.Console.WriteLine("Call Test F"); 41 | return; 42 | } 43 | public string Name { get; } 44 | } 45 | 46 | public struct RefStruct 47 | { 48 | public string Name; 49 | private string InternalName; 50 | } 51 | } 52 | 53 | namespace TestCase.Reference2 54 | { 55 | public class RefClass2 56 | { 57 | public int value; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /RTCLI.TestCase.Reference/TestCase.Reference.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestArray.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestArray 8 | { 9 | public static void Test() 10 | { 11 | int[] list = new int[12]; 12 | list[0] = 1111111; 13 | list[11] = 111111; 14 | list[0] = list[11]; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestCase - Backup.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netstandard2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestCase.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Library 5 | netstandard2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestCrossFile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | public class TestCrossFile 8 | { 9 | public static void Test() 10 | { 11 | System.Console.WriteLine(TestSystem.val); 12 | System.Console.WriteLine(TestSystem.valP); 13 | System.Console.WriteLine(system_test.valval); 14 | TestSystem.Test(); 15 | } 16 | 17 | static TestSystem system_test = new TestSystem(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestDelegate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestDelegate 8 | { 9 | public delegate void DelegateA(float arg); 10 | event DelegateA e; 11 | public static float StaticFunc(int a) { return 1; } 12 | public static void Test(TestDelegate obj) 13 | { 14 | DelegateA arg = null; 15 | arg(1.0f); 16 | float upvalue = 0; 17 | arg = arg2 => { upvalue = arg2; }; 18 | arg(2.0f); 19 | obj.e += arg => { }; 20 | Func asd = arg2 => arg2; 21 | Func aaa = StaticFunc; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestEnum 8 | { 9 | enum MyEnum 10 | { 11 | A,B,C 12 | } 13 | enum FlagEnum 14 | { 15 | A = 0x1, B = 0x2, C = 0x4 16 | } 17 | enum LongEnum : ulong 18 | { 19 | A, B, C 20 | } 21 | void Test() 22 | { 23 | MyEnum a = MyEnum.A; 24 | MyEnum b = MyEnum.B; 25 | int c; 26 | LongEnum d = LongEnum.C; 27 | c = (int)a; 28 | a = b; 29 | b = (MyEnum)c; 30 | c = (int)d; 31 | FlagEnum E = FlagEnum.A; 32 | FlagEnum F = FlagEnum.B; 33 | E = E | F; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestGeneric 8 | { 9 | public class GenericClass where T : new() 10 | { 11 | T fieldInGenericClass; 12 | public void MethodInGenericClass(T arg) 13 | { 14 | arg = new T(); 15 | } 16 | public static void StaticMethodInGenericClass(T arg) 17 | { 18 | arg = new T(); 19 | } 20 | public void GenericMethodInGenericClass(Y arg) where Y : new() 21 | { 22 | arg = new Y(); 23 | } 24 | } 25 | public static void GenericMethod() where Y : new() 26 | { 27 | var a = new GenericClass(); 28 | a.MethodInGenericClass(new Y()); 29 | GenericClass.StaticMethodInGenericClass(new Y()); 30 | } 31 | public void MethodUseGenericClass() 32 | { 33 | GenericMethod(); 34 | } 35 | 36 | public delegate TResult GenericDelegate(TArg arg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestInnerClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | public class TestInnerClass 8 | { 9 | public class Inner 10 | { 11 | public class InnerInner 12 | { 13 | public InnerInner(int id) 14 | { 15 | 16 | } 17 | public int ID; 18 | } 19 | public Inner(int id) 20 | { 21 | this.ID = id; 22 | } 23 | public void CallTest(int u) 24 | { 25 | System.Console.WriteLine("Call Test"); 26 | return; 27 | } 28 | public void CallTestF(float u) 29 | { 30 | System.Console.WriteLine("Call Test F"); 31 | return; 32 | } 33 | public int ID { get; } 34 | public int ID2 { get; } 35 | }; 36 | 37 | public static void Test() 38 | { 39 | TestInnerClass test = null; 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestInterface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestInterface 8 | { 9 | public struct TestStruct : IEquatable 10 | { 11 | float a; 12 | 13 | public bool Equals(TestStruct other) 14 | { 15 | return a == other.a; 16 | } 17 | } 18 | 19 | public interface Interface1 20 | { 21 | public float a(); 22 | } 23 | 24 | public interface Interface2 : Interface1 25 | { 26 | public new float a(); 27 | } 28 | 29 | public interface Interface3 : Interface2 30 | { 31 | public new float a() => 3; 32 | } 33 | 34 | public interface Interface4 : Interface3 35 | { 36 | public float a() => 4; 37 | } 38 | 39 | public interface Interface5 : Interface4 40 | { 41 | 42 | } 43 | 44 | public class A : Interface4 45 | { 46 | float Interface1.a() => 1; 47 | float Interface2.a() => 2; 48 | } 49 | 50 | bool TestFunc(IEquatable arg1, T arg2) 51 | { 52 | return arg1.Equals(arg2); 53 | } 54 | 55 | bool TestFunc2(T arg1, T arg2) where T : IEquatable 56 | { 57 | return arg1.Equals(arg2); 58 | } 59 | 60 | bool TestGeneric() 61 | { 62 | TestStruct a = new TestStruct(); 63 | TestStruct b = new TestStruct(); 64 | TestFunc2(a, b); 65 | return TestFunc(a, b); 66 | } 67 | 68 | void Test() 69 | { 70 | var Test = new TestInterface.A(); 71 | var I1 = (TestInterface.Interface1)Test; 72 | var I2 = (TestInterface.Interface2)Test; 73 | var I3 = (TestInterface.Interface3)Test; 74 | var I4 = (TestInterface.Interface4)Test; 75 | Console.WriteLine(I1.a()); 76 | Console.WriteLine(I2.a()); 77 | Console.WriteLine(I3.a()); 78 | Console.WriteLine(I4.a()); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestReference 8 | { 9 | public struct Struct { } 10 | public class Class { public TestReference field; } 11 | public ref Struct RefStruct(ref Struct a) 12 | { 13 | a = new Struct(); 14 | return ref a; 15 | } 16 | public ref Class RefClass(ref Class a) 17 | { 18 | a = new Class(); 19 | return ref a; 20 | } 21 | public ref TestReference RefField() 22 | { 23 | var a = new Class(); 24 | return ref a.field; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestString.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestString 8 | { 9 | public static void Test() 10 | { 11 | String nullStr = null; 12 | String str = "Static String"; 13 | 14 | System.Console.WriteLine(str); 15 | System.Console.WriteLine(str.Length); 16 | String lowerStr = str.ToLower(); 17 | System.Console.Write("ToLower: "); 18 | System.Console.WriteLine(lowerStr); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestStruct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using TestCase.Reference; 5 | 6 | namespace TestCase 7 | { 8 | class TestStruct 9 | { 10 | public struct PureStruct 11 | { 12 | public String name; 13 | public float val; 14 | } 15 | public static void Test(PureStruct arg = new PureStruct(), TestStruct arg2 = null) 16 | { 17 | PureStruct local = new PureStruct(); 18 | local.name = "asd"; 19 | local = new PureStruct(); 20 | local = new PureStruct { val = 10f }; 21 | local = arg; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TestCase 4 | { 5 | public struct TestSystem 6 | { 7 | public static void Test() 8 | { 9 | System.Console.Write("Write Tested!\n"); 10 | System.Console.WriteLine("WriteLine Tested!"); 11 | } 12 | public static int valP => 1; 13 | public static int val = 1; 14 | public int valval; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /RTCLI.TestCase/TestThrow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestCase 6 | { 7 | class TestThrow 8 | { 9 | public static void Test() 10 | { 11 | object value = null; 12 | try 13 | { 14 | if (value != null) 15 | throw new Exception(); 16 | } 17 | catch 18 | { 19 | 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /RTCLI.TestCase/netstandard.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Team-RTCLI/RTCLI.AOTCompiler/9af1bcf3e4b595edfdc0c672541bd760a1491de1/RTCLI.TestCase/netstandard.dll -------------------------------------------------------------------------------- /TestProgram/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TestProgram 4 | { 5 | public struct TestStaticVal 6 | { 7 | public static int val = 1; 8 | } 9 | 10 | public interface Interface0 11 | { 12 | void Slot() 13 | { 14 | Console.WriteLine("0"); 15 | } 16 | } 17 | public interface Interface1 : Interface0 18 | { 19 | new void Slot() 20 | { 21 | Console.WriteLine("1"); 22 | } 23 | } 24 | public class Class : Interface1 25 | { 26 | void Interface1.Slot() 27 | { 28 | Console.WriteLine("Override Interface1"); 29 | } 30 | } 31 | class Program 32 | { 33 | static void Main(string[] args) 34 | { 35 | Class Fuck = new Class(); 36 | (Fuck as Interface0).Slot(); 37 | (Fuck as Interface1).Slot(); 38 | 39 | Console.WriteLine(TestStaticVal.val); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /TestProgram/TestProgram.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | --------------------------------------------------------------------------------