| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | namespace System.Reflection.Metadata |
| | | 5 | | { |
| | | 6 | | public static partial class ILOpCodeExtensions |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Returns true of the specified op-code is a branch to a label. |
| | | 10 | | /// </summary> |
| | | 11 | | public static bool IsBranch(this ILOpCode opCode) |
| | 0 | 12 | | { |
| | 0 | 13 | | switch (opCode) |
| | | 14 | | { |
| | | 15 | | case ILOpCode.Br: |
| | | 16 | | case ILOpCode.Br_s: |
| | | 17 | | case ILOpCode.Brtrue: |
| | | 18 | | case ILOpCode.Brtrue_s: |
| | | 19 | | case ILOpCode.Brfalse: |
| | | 20 | | case ILOpCode.Brfalse_s: |
| | | 21 | | case ILOpCode.Beq: |
| | | 22 | | case ILOpCode.Beq_s: |
| | | 23 | | case ILOpCode.Bne_un: |
| | | 24 | | case ILOpCode.Bne_un_s: |
| | | 25 | | case ILOpCode.Bge: |
| | | 26 | | case ILOpCode.Bge_s: |
| | | 27 | | case ILOpCode.Bge_un: |
| | | 28 | | case ILOpCode.Bge_un_s: |
| | | 29 | | case ILOpCode.Bgt: |
| | | 30 | | case ILOpCode.Bgt_s: |
| | | 31 | | case ILOpCode.Bgt_un: |
| | | 32 | | case ILOpCode.Bgt_un_s: |
| | | 33 | | case ILOpCode.Ble: |
| | | 34 | | case ILOpCode.Ble_s: |
| | | 35 | | case ILOpCode.Ble_un: |
| | | 36 | | case ILOpCode.Ble_un_s: |
| | | 37 | | case ILOpCode.Blt: |
| | | 38 | | case ILOpCode.Blt_s: |
| | | 39 | | case ILOpCode.Blt_un: |
| | | 40 | | case ILOpCode.Blt_un_s: |
| | | 41 | | case ILOpCode.Leave: |
| | | 42 | | case ILOpCode.Leave_s: |
| | 0 | 43 | | return true; |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | return false; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Calculate the size of the specified branch instruction operand. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="opCode">Branch op-code.</param> |
| | | 53 | | /// <returns>1 if <paramref name="opCode"/> is a short branch or 4 if it is a long branch.</returns> |
| | | 54 | | /// <exception cref="ArgumentException">Specified <paramref name="opCode"/> is not a branch op-code.</exception> |
| | | 55 | | public static int GetBranchOperandSize(this ILOpCode opCode) |
| | 0 | 56 | | { |
| | 0 | 57 | | switch (opCode) |
| | | 58 | | { |
| | | 59 | | case ILOpCode.Br_s: |
| | | 60 | | case ILOpCode.Brfalse_s: |
| | | 61 | | case ILOpCode.Brtrue_s: |
| | | 62 | | case ILOpCode.Beq_s: |
| | | 63 | | case ILOpCode.Bge_s: |
| | | 64 | | case ILOpCode.Bgt_s: |
| | | 65 | | case ILOpCode.Ble_s: |
| | | 66 | | case ILOpCode.Blt_s: |
| | | 67 | | case ILOpCode.Bne_un_s: |
| | | 68 | | case ILOpCode.Bge_un_s: |
| | | 69 | | case ILOpCode.Bgt_un_s: |
| | | 70 | | case ILOpCode.Ble_un_s: |
| | | 71 | | case ILOpCode.Blt_un_s: |
| | | 72 | | case ILOpCode.Leave_s: |
| | 0 | 73 | | return 1; |
| | | 74 | | |
| | | 75 | | case ILOpCode.Br: |
| | | 76 | | case ILOpCode.Brfalse: |
| | | 77 | | case ILOpCode.Brtrue: |
| | | 78 | | case ILOpCode.Beq: |
| | | 79 | | case ILOpCode.Bge: |
| | | 80 | | case ILOpCode.Bgt: |
| | | 81 | | case ILOpCode.Ble: |
| | | 82 | | case ILOpCode.Blt: |
| | | 83 | | case ILOpCode.Bne_un: |
| | | 84 | | case ILOpCode.Bge_un: |
| | | 85 | | case ILOpCode.Bgt_un: |
| | | 86 | | case ILOpCode.Ble_un: |
| | | 87 | | case ILOpCode.Blt_un: |
| | | 88 | | case ILOpCode.Leave: |
| | 0 | 89 | | return 4; |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | throw new ArgumentException(SR.Format(SR.UnexpectedOpCode, opCode), nameof(opCode)); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Get a short form of the specified branch op-code. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="opCode">Branch op-code.</param> |
| | | 99 | | /// <returns>Short form of the branch op-code.</returns> |
| | | 100 | | /// <exception cref="ArgumentException">Specified <paramref name="opCode"/> is not a branch op-code.</exception> |
| | | 101 | | public static ILOpCode GetShortBranch(this ILOpCode opCode) |
| | 0 | 102 | | { |
| | 0 | 103 | | switch (opCode) |
| | | 104 | | { |
| | | 105 | | case ILOpCode.Br_s: |
| | | 106 | | case ILOpCode.Brfalse_s: |
| | | 107 | | case ILOpCode.Brtrue_s: |
| | | 108 | | case ILOpCode.Beq_s: |
| | | 109 | | case ILOpCode.Bge_s: |
| | | 110 | | case ILOpCode.Bgt_s: |
| | | 111 | | case ILOpCode.Ble_s: |
| | | 112 | | case ILOpCode.Blt_s: |
| | | 113 | | case ILOpCode.Bne_un_s: |
| | | 114 | | case ILOpCode.Bge_un_s: |
| | | 115 | | case ILOpCode.Bgt_un_s: |
| | | 116 | | case ILOpCode.Ble_un_s: |
| | | 117 | | case ILOpCode.Blt_un_s: |
| | | 118 | | case ILOpCode.Leave_s: |
| | 0 | 119 | | return opCode; |
| | | 120 | | |
| | | 121 | | case ILOpCode.Br: |
| | 0 | 122 | | return ILOpCode.Br_s; |
| | | 123 | | |
| | | 124 | | case ILOpCode.Brfalse: |
| | 0 | 125 | | return ILOpCode.Brfalse_s; |
| | | 126 | | |
| | | 127 | | case ILOpCode.Brtrue: |
| | 0 | 128 | | return ILOpCode.Brtrue_s; |
| | | 129 | | |
| | | 130 | | case ILOpCode.Beq: |
| | 0 | 131 | | return ILOpCode.Beq_s; |
| | | 132 | | |
| | | 133 | | case ILOpCode.Bge: |
| | 0 | 134 | | return ILOpCode.Bge_s; |
| | | 135 | | |
| | | 136 | | case ILOpCode.Bgt: |
| | 0 | 137 | | return ILOpCode.Bgt_s; |
| | | 138 | | |
| | | 139 | | case ILOpCode.Ble: |
| | 0 | 140 | | return ILOpCode.Ble_s; |
| | | 141 | | |
| | | 142 | | case ILOpCode.Blt: |
| | 0 | 143 | | return ILOpCode.Blt_s; |
| | | 144 | | |
| | | 145 | | case ILOpCode.Bne_un: |
| | 0 | 146 | | return ILOpCode.Bne_un_s; |
| | | 147 | | |
| | | 148 | | case ILOpCode.Bge_un: |
| | 0 | 149 | | return ILOpCode.Bge_un_s; |
| | | 150 | | |
| | | 151 | | case ILOpCode.Bgt_un: |
| | 0 | 152 | | return ILOpCode.Bgt_un_s; |
| | | 153 | | |
| | | 154 | | case ILOpCode.Ble_un: |
| | 0 | 155 | | return ILOpCode.Ble_un_s; |
| | | 156 | | |
| | | 157 | | case ILOpCode.Blt_un: |
| | 0 | 158 | | return ILOpCode.Blt_un_s; |
| | | 159 | | |
| | | 160 | | case ILOpCode.Leave: |
| | 0 | 161 | | return ILOpCode.Leave_s; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | throw new ArgumentException(SR.Format(SR.UnexpectedOpCode, opCode), nameof(opCode)); |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Get a long form of the specified branch op-code. |
| | | 169 | | /// </summary> |
| | | 170 | | /// <param name="opCode">Branch op-code.</param> |
| | | 171 | | /// <returns>Long form of the branch op-code.</returns> |
| | | 172 | | /// <exception cref="ArgumentException">Specified <paramref name="opCode"/> is not a branch op-code.</exception> |
| | | 173 | | public static ILOpCode GetLongBranch(this ILOpCode opCode) |
| | 0 | 174 | | { |
| | 0 | 175 | | switch (opCode) |
| | | 176 | | { |
| | | 177 | | case ILOpCode.Br: |
| | | 178 | | case ILOpCode.Brfalse: |
| | | 179 | | case ILOpCode.Brtrue: |
| | | 180 | | case ILOpCode.Beq: |
| | | 181 | | case ILOpCode.Bge: |
| | | 182 | | case ILOpCode.Bgt: |
| | | 183 | | case ILOpCode.Ble: |
| | | 184 | | case ILOpCode.Blt: |
| | | 185 | | case ILOpCode.Bne_un: |
| | | 186 | | case ILOpCode.Bge_un: |
| | | 187 | | case ILOpCode.Bgt_un: |
| | | 188 | | case ILOpCode.Ble_un: |
| | | 189 | | case ILOpCode.Blt_un: |
| | | 190 | | case ILOpCode.Leave: |
| | 0 | 191 | | return opCode; |
| | | 192 | | |
| | | 193 | | case ILOpCode.Br_s: |
| | 0 | 194 | | return ILOpCode.Br; |
| | | 195 | | |
| | | 196 | | case ILOpCode.Brfalse_s: |
| | 0 | 197 | | return ILOpCode.Brfalse; |
| | | 198 | | |
| | | 199 | | case ILOpCode.Brtrue_s: |
| | 0 | 200 | | return ILOpCode.Brtrue; |
| | | 201 | | |
| | | 202 | | case ILOpCode.Beq_s: |
| | 0 | 203 | | return ILOpCode.Beq; |
| | | 204 | | |
| | | 205 | | case ILOpCode.Bge_s: |
| | 0 | 206 | | return ILOpCode.Bge; |
| | | 207 | | |
| | | 208 | | case ILOpCode.Bgt_s: |
| | 0 | 209 | | return ILOpCode.Bgt; |
| | | 210 | | |
| | | 211 | | case ILOpCode.Ble_s: |
| | 0 | 212 | | return ILOpCode.Ble; |
| | | 213 | | |
| | | 214 | | case ILOpCode.Blt_s: |
| | 0 | 215 | | return ILOpCode.Blt; |
| | | 216 | | |
| | | 217 | | case ILOpCode.Bne_un_s: |
| | 0 | 218 | | return ILOpCode.Bne_un; |
| | | 219 | | |
| | | 220 | | case ILOpCode.Bge_un_s: |
| | 0 | 221 | | return ILOpCode.Bge_un; |
| | | 222 | | |
| | | 223 | | case ILOpCode.Bgt_un_s: |
| | 0 | 224 | | return ILOpCode.Bgt_un; |
| | | 225 | | |
| | | 226 | | case ILOpCode.Ble_un_s: |
| | 0 | 227 | | return ILOpCode.Ble_un; |
| | | 228 | | |
| | | 229 | | case ILOpCode.Blt_un_s: |
| | 0 | 230 | | return ILOpCode.Blt_un; |
| | | 231 | | |
| | | 232 | | case ILOpCode.Leave_s: |
| | 0 | 233 | | return ILOpCode.Leave; |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | throw new ArgumentException(SR.Format(SR.UnexpectedOpCode, opCode), nameof(opCode)); |
| | 0 | 237 | | } |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |