Last active
June 2, 2024 09:13
-
-
Save kekyo/bc8e411b846c98d2f2ae7b839ef77fea to your computer and use it in GitHub Desktop.
Extract CIL opcodes and makes relation chibias-cil opcode naming both Reflection.Emit and Cecil.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| namespace ConsoleApp1; | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var emitOpCodes = | |
| typeof(System.Reflection.Emit.OpCodes).GetFields().Where(field => | |
| field.IsPublic && field.IsStatic && field.IsInitOnly && | |
| field.FieldType.FullName == "System.Reflection.Emit.OpCode"). | |
| Select(field => (fieldName: field.Name, opCode: (System.Reflection.Emit.OpCode)field.GetValue(null)!)). | |
| Where(entry => entry.opCode.OpCodeType != System.Reflection.Emit.OpCodeType.Nternal). | |
| OrderBy(entry => entry.opCode.Value). | |
| Select(entry => (entry.fieldName, name: entry.opCode.Name!.Replace('_', '.').TrimEnd('.').ToLowerInvariant())). | |
| ToArray(); | |
| Console.WriteLine("namespace chibicc.toolchain;"); | |
| Console.WriteLine(); | |
| Console.WriteLine("public static class ReflectionOpCodeDefinition"); | |
| Console.WriteLine("{"); | |
| Console.WriteLine(" public static readonly System.Collections.Generic.Dictionary<string. System.Reflection.Emit.OpCode> GetOpCodes() =>"); | |
| Console.WriteLine(" new(System.StringComparer.OrdinalIgnoreCase)"); | |
| Console.WriteLine(" {"); | |
| foreach (var entry in emitOpCodes) | |
| { | |
| Console.WriteLine( | |
| $" {{ \"{entry.name}\", System.Reflection.Emit.OpCodes.{entry.fieldName} }},"); | |
| } | |
| Console.WriteLine(" };"); | |
| Console.WriteLine("}"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment