|
| 1 | +using ILSourceParser.Syntax.Instructions; |
| 2 | +using ILSourceParser.Syntax.Instructions.OpCodes; |
| 3 | + |
| 4 | +namespace ILSourceParser.Utilities; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Utilities for <see cref="InstructionSyntax"/>. |
| 8 | +/// </summary> |
| 9 | +public static class InstructionUtilities |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Returns the name of the instruction based on the syntax node. For example, |
| 13 | + /// if the syntax node is an instance of <see cref="ParameterlessOpCodeSyntax"/>, |
| 14 | + /// the name of the parameterless instruction is returned, or, for example, if |
| 15 | + /// the syntax node is an instance of <see cref="UnboxAnyOpCodeSyntax"/>, "unbox.any" |
| 16 | + /// is returned. |
| 17 | + /// </summary> |
| 18 | + /// <param name="instruction">The input instruction to get name of.</param> |
| 19 | + /// <returns>The name of the instruction as a string.</returns> |
| 20 | + /// <exception cref="ArgumentException">Thrown when the input instruction is not valid.</exception> |
| 21 | + public static string GetInstructionName(InstructionSyntax instruction) |
| 22 | + { |
| 23 | + return instruction switch |
| 24 | + { |
| 25 | + ParameterlessOpCodeSyntax parameterlessOpCode => parameterlessOpCode.Name, |
| 26 | + ArgumentLoadOpCodeSyntax argumentLoad => argumentLoad.Name, |
| 27 | + ArgumentStoreOpCodeSyntax argumentStore => argumentStore.Name, |
| 28 | + BranchOpCodeSyntax branchOpCode => branchOpCode.Name, |
| 29 | + ComparisonOpCodeSyntax comparisonOpCode => comparisonOpCode.Name, |
| 30 | + ConversionOpCodeSyntax conversionOpCode => conversionOpCode.Name, |
| 31 | + LoadElementOpCodeSyntax loadElement => loadElement.Name, |
| 32 | + LoadIndirectOpCodeSyntax loadIndirect => loadIndirect.Name, |
| 33 | + LoadLocalOpCodeSyntax loadLocal => loadLocal.Name, |
| 34 | + PushNumberToStackOpCodeSyntax pushNumberToStack => pushNumberToStack.Name, |
| 35 | + StoreElementOpCodeSyntax storeElement => storeElement.Name, |
| 36 | + StoreIndirectOpCodeSyntax storeIndirect => storeIndirect.Name, |
| 37 | + StoreLocalOpCodeSyntax storeLocal => storeLocal.Name, |
| 38 | + BoxOpCodeSyntax => "box", |
| 39 | + CalliOpCodeSyntax => "calli", |
| 40 | + CallOpCodeSyntax => "call", |
| 41 | + CallvirtOpCodeSyntax => "callvirt", |
| 42 | + CastclassOpCodeSyntax => "castclass", |
| 43 | + CpobjOpCodeSyntax => "cpobj", |
| 44 | + InitobjOpCodeSyntax => "initobj", |
| 45 | + IsinstOpCodeSyntax => "isinst", |
| 46 | + JmpOpCodeSyntax => "jmp", |
| 47 | + LdfldaOpCodeSyntax => "ldflda", |
| 48 | + LdfldOpCodeSyntax => "ldfld", |
| 49 | + LdftnOpCodeSyntax => "ldftn", |
| 50 | + LdsfldaOpCodeSyntax => "ldsflda", |
| 51 | + LdsfldOpCodeSyntax => "ldsfld", |
| 52 | + LdstrOpCodeSyntax => "ldstr", |
| 53 | + LdvirtftnOpCodeSyntax => "ldvirtftn", |
| 54 | + LeaveOpCodeSyntax => "leave", |
| 55 | + MkrefanyOpCodeSyntax => "mkrefany", |
| 56 | + NewarrOpCodeSyntax => "newarr", |
| 57 | + NewobjOpCodeSyntax => "newobj", |
| 58 | + RefanyvalOpCodeSyntax => "refanyval", |
| 59 | + SizeofOpCodeSyntax => "sizeof", |
| 60 | + SwitchOpCodeSyntax => "switch", |
| 61 | + UnboxAnyOpCodeSyntax => "unbox.any", |
| 62 | + UnboxOpCodeSyntax => "unbox", |
| 63 | + _ => throw new ArgumentException("Invalid opcode syntax node", nameof(instruction)) |
| 64 | + }; |
| 65 | + } |
| 66 | +} |
0 commit comments