| | | 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 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Encodes instructions. |
| | | 10 | | /// </summary> |
| | | 11 | | public readonly struct InstructionEncoder |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Underlying builder where encoded instructions are written to. |
| | | 15 | | /// </summary> |
| | 0 | 16 | | public BlobBuilder CodeBuilder { get; } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Builder tracking labels, branches and exception handlers. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// If null the encoder doesn't support construction of control flow. |
| | | 23 | | /// </remarks> |
| | 0 | 24 | | public ControlFlowBuilder? ControlFlowBuilder { get; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates an encoder backed by code and control-flow builders. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="codeBuilder">Builder to write encoded instructions to.</param> |
| | | 30 | | /// <param name="controlFlowBuilder"> |
| | | 31 | | /// Builder tracking labels, branches and exception handlers. |
| | | 32 | | /// Must be specified to be able to use some of the control-flow factory methods of <see cref="InstructionEncode |
| | | 33 | | /// such as <see cref="Branch(ILOpCode, LabelHandle)"/>, <see cref="DefineLabel"/>, <see cref="MarkLabel(LabelHa |
| | | 34 | | /// </param> |
| | | 35 | | public InstructionEncoder(BlobBuilder codeBuilder, ControlFlowBuilder? controlFlowBuilder = null) |
| | 0 | 36 | | { |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(codeBuilder); |
| | | 38 | | |
| | 0 | 39 | | CodeBuilder = codeBuilder; |
| | 0 | 40 | | ControlFlowBuilder = controlFlowBuilder; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Offset of the next encoded instruction. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public int Offset => CodeBuilder.Count; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Encodes specified op-code. |
| | | 50 | | /// </summary> |
| | | 51 | | public void OpCode(ILOpCode code) |
| | 0 | 52 | | { |
| | 0 | 53 | | ControlFlowBuilder?.ValidateNotInSwitch(); |
| | 0 | 54 | | if (unchecked((byte)code) == (ushort)code) |
| | 0 | 55 | | { |
| | 0 | 56 | | CodeBuilder.WriteByte((byte)code); |
| | 0 | 57 | | } |
| | | 58 | | else |
| | 0 | 59 | | { |
| | | 60 | | // IL opcodes that occupy two bytes are written to |
| | | 61 | | // the byte stream with the high-order byte first, |
| | | 62 | | // in contrast to the little-endian format of the |
| | | 63 | | // numeric arguments and tokens. |
| | 0 | 64 | | CodeBuilder.WriteUInt16BE((ushort)code); |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Encodes a token. |
| | | 70 | | /// </summary> |
| | | 71 | | public void Token(EntityHandle handle) |
| | 0 | 72 | | { |
| | 0 | 73 | | Token(MetadataTokens.GetToken(handle)); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Encodes a token. |
| | | 78 | | /// </summary> |
| | | 79 | | public void Token(int token) |
| | 0 | 80 | | { |
| | 0 | 81 | | ControlFlowBuilder?.ValidateNotInSwitch(); |
| | 0 | 82 | | CodeBuilder.WriteInt32(token); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Encodes <code>ldstr</code> instruction and its operand. |
| | | 87 | | /// </summary> |
| | | 88 | | public void LoadString(UserStringHandle handle) |
| | 0 | 89 | | { |
| | 0 | 90 | | OpCode(ILOpCode.Ldstr); |
| | 0 | 91 | | Token(MetadataTokens.GetToken(handle)); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Encodes <code>call</code> instruction and its operand. |
| | | 96 | | /// </summary> |
| | | 97 | | public void Call(EntityHandle methodHandle) |
| | 0 | 98 | | { |
| | 0 | 99 | | if (methodHandle.Kind != HandleKind.MethodDefinition && |
| | 0 | 100 | | methodHandle.Kind != HandleKind.MethodSpecification && |
| | 0 | 101 | | methodHandle.Kind != HandleKind.MemberReference) |
| | 0 | 102 | | { |
| | 0 | 103 | | Throw.InvalidArgument_Handle(nameof(methodHandle)); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | OpCode(ILOpCode.Call); |
| | 0 | 107 | | Token(methodHandle); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Encodes <code>call</code> instruction and its operand. |
| | | 112 | | /// </summary> |
| | | 113 | | public void Call(MethodDefinitionHandle methodHandle) |
| | 0 | 114 | | { |
| | 0 | 115 | | OpCode(ILOpCode.Call); |
| | 0 | 116 | | Token(methodHandle); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Encodes <code>call</code> instruction and its operand. |
| | | 121 | | /// </summary> |
| | | 122 | | public void Call(MethodSpecificationHandle methodHandle) |
| | 0 | 123 | | { |
| | 0 | 124 | | OpCode(ILOpCode.Call); |
| | 0 | 125 | | Token(methodHandle); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Encodes <code>call</code> instruction and its operand. |
| | | 130 | | /// </summary> |
| | | 131 | | public void Call(MemberReferenceHandle methodHandle) |
| | 0 | 132 | | { |
| | 0 | 133 | | OpCode(ILOpCode.Call); |
| | 0 | 134 | | Token(methodHandle); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Encodes <code>calli</code> instruction and its operand. |
| | | 139 | | /// </summary> |
| | | 140 | | public void CallIndirect(StandaloneSignatureHandle signature) |
| | 0 | 141 | | { |
| | 0 | 142 | | OpCode(ILOpCode.Calli); |
| | 0 | 143 | | Token(signature); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Encodes <see cref="int"/> constant load instruction. |
| | | 148 | | /// </summary> |
| | | 149 | | public void LoadConstantI4(int value) |
| | 0 | 150 | | { |
| | | 151 | | ILOpCode code; |
| | 0 | 152 | | switch (value) |
| | | 153 | | { |
| | 0 | 154 | | case -1: code = ILOpCode.Ldc_i4_m1; break; |
| | 0 | 155 | | case 0: code = ILOpCode.Ldc_i4_0; break; |
| | 0 | 156 | | case 1: code = ILOpCode.Ldc_i4_1; break; |
| | 0 | 157 | | case 2: code = ILOpCode.Ldc_i4_2; break; |
| | 0 | 158 | | case 3: code = ILOpCode.Ldc_i4_3; break; |
| | 0 | 159 | | case 4: code = ILOpCode.Ldc_i4_4; break; |
| | 0 | 160 | | case 5: code = ILOpCode.Ldc_i4_5; break; |
| | 0 | 161 | | case 6: code = ILOpCode.Ldc_i4_6; break; |
| | 0 | 162 | | case 7: code = ILOpCode.Ldc_i4_7; break; |
| | 0 | 163 | | case 8: code = ILOpCode.Ldc_i4_8; break; |
| | | 164 | | |
| | | 165 | | default: |
| | 0 | 166 | | if (unchecked((sbyte)value == value)) |
| | 0 | 167 | | { |
| | 0 | 168 | | OpCode(ILOpCode.Ldc_i4_s); |
| | 0 | 169 | | CodeBuilder.WriteSByte((sbyte)value); |
| | 0 | 170 | | } |
| | | 171 | | else |
| | 0 | 172 | | { |
| | 0 | 173 | | OpCode(ILOpCode.Ldc_i4); |
| | 0 | 174 | | CodeBuilder.WriteInt32(value); |
| | 0 | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | OpCode(code); |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Encodes <see cref="long"/> constant load instruction. |
| | | 185 | | /// </summary> |
| | | 186 | | public void LoadConstantI8(long value) |
| | 0 | 187 | | { |
| | 0 | 188 | | OpCode(ILOpCode.Ldc_i8); |
| | 0 | 189 | | CodeBuilder.WriteInt64(value); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Encodes <see cref="float"/> constant load instruction. |
| | | 194 | | /// </summary> |
| | | 195 | | public void LoadConstantR4(float value) |
| | 0 | 196 | | { |
| | 0 | 197 | | OpCode(ILOpCode.Ldc_r4); |
| | 0 | 198 | | CodeBuilder.WriteSingle(value); |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Encodes <see cref="double"/> constant load instruction. |
| | | 203 | | /// </summary> |
| | | 204 | | public void LoadConstantR8(double value) |
| | 0 | 205 | | { |
| | 0 | 206 | | OpCode(ILOpCode.Ldc_r8); |
| | 0 | 207 | | CodeBuilder.WriteDouble(value); |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Encodes local variable load instruction. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="slotIndex">Index of the local variable slot.</param> |
| | | 214 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="slotIndex"/> is negative or greater than <see |
| | | 215 | | public void LoadLocal(int slotIndex) |
| | 0 | 216 | | { |
| | 0 | 217 | | switch (slotIndex) |
| | | 218 | | { |
| | 0 | 219 | | case 0: OpCode(ILOpCode.Ldloc_0); break; |
| | 0 | 220 | | case 1: OpCode(ILOpCode.Ldloc_1); break; |
| | 0 | 221 | | case 2: OpCode(ILOpCode.Ldloc_2); break; |
| | 0 | 222 | | case 3: OpCode(ILOpCode.Ldloc_3); break; |
| | | 223 | | |
| | | 224 | | default: |
| | 0 | 225 | | if (unchecked((uint)slotIndex) <= byte.MaxValue) |
| | 0 | 226 | | { |
| | 0 | 227 | | OpCode(ILOpCode.Ldloc_s); |
| | 0 | 228 | | CodeBuilder.WriteByte((byte)slotIndex); |
| | 0 | 229 | | } |
| | 0 | 230 | | else if (unchecked((uint)slotIndex) <= ushort.MaxValue) |
| | 0 | 231 | | { |
| | 0 | 232 | | OpCode(ILOpCode.Ldloc); |
| | 0 | 233 | | CodeBuilder.WriteUInt16((ushort)slotIndex); |
| | 0 | 234 | | } |
| | | 235 | | else |
| | 0 | 236 | | { |
| | 0 | 237 | | Throw.ArgumentOutOfRange(nameof(slotIndex)); |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | break; |
| | | 241 | | } |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Encodes local variable store instruction. |
| | | 246 | | /// </summary> |
| | | 247 | | /// <param name="slotIndex">Index of the local variable slot.</param> |
| | | 248 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="slotIndex"/> is negative or greater than <see |
| | | 249 | | public void StoreLocal(int slotIndex) |
| | 0 | 250 | | { |
| | 0 | 251 | | switch (slotIndex) |
| | | 252 | | { |
| | 0 | 253 | | case 0: OpCode(ILOpCode.Stloc_0); break; |
| | 0 | 254 | | case 1: OpCode(ILOpCode.Stloc_1); break; |
| | 0 | 255 | | case 2: OpCode(ILOpCode.Stloc_2); break; |
| | 0 | 256 | | case 3: OpCode(ILOpCode.Stloc_3); break; |
| | | 257 | | |
| | | 258 | | default: |
| | 0 | 259 | | if (unchecked((uint)slotIndex) <= byte.MaxValue) |
| | 0 | 260 | | { |
| | 0 | 261 | | OpCode(ILOpCode.Stloc_s); |
| | 0 | 262 | | CodeBuilder.WriteByte((byte)slotIndex); |
| | 0 | 263 | | } |
| | 0 | 264 | | else if (unchecked((uint)slotIndex) <= ushort.MaxValue) |
| | 0 | 265 | | { |
| | 0 | 266 | | OpCode(ILOpCode.Stloc); |
| | 0 | 267 | | CodeBuilder.WriteUInt16((ushort)slotIndex); |
| | 0 | 268 | | } |
| | | 269 | | else |
| | 0 | 270 | | { |
| | 0 | 271 | | Throw.ArgumentOutOfRange(nameof(slotIndex)); |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | break; |
| | | 275 | | } |
| | 0 | 276 | | } |
| | | 277 | | |
| | | 278 | | /// <summary> |
| | | 279 | | /// Encodes local variable address load instruction. |
| | | 280 | | /// </summary> |
| | | 281 | | /// <param name="slotIndex">Index of the local variable slot.</param> |
| | | 282 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="slotIndex"/> is negative or greater than <see |
| | | 283 | | public void LoadLocalAddress(int slotIndex) |
| | 0 | 284 | | { |
| | 0 | 285 | | if (unchecked((uint)slotIndex) <= byte.MaxValue) |
| | 0 | 286 | | { |
| | 0 | 287 | | OpCode(ILOpCode.Ldloca_s); |
| | 0 | 288 | | CodeBuilder.WriteByte((byte)slotIndex); |
| | 0 | 289 | | } |
| | 0 | 290 | | else if (unchecked((uint)slotIndex) <= ushort.MaxValue) |
| | 0 | 291 | | { |
| | 0 | 292 | | OpCode(ILOpCode.Ldloca); |
| | 0 | 293 | | CodeBuilder.WriteUInt16((ushort)slotIndex); |
| | 0 | 294 | | } |
| | | 295 | | else |
| | 0 | 296 | | { |
| | 0 | 297 | | Throw.ArgumentOutOfRange(nameof(slotIndex)); |
| | | 298 | | } |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Encodes argument load instruction. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <param name="argumentIndex">Index of the argument.</param> |
| | | 305 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentIndex"/> is negative or greater than < |
| | | 306 | | public void LoadArgument(int argumentIndex) |
| | 0 | 307 | | { |
| | 0 | 308 | | switch (argumentIndex) |
| | | 309 | | { |
| | 0 | 310 | | case 0: OpCode(ILOpCode.Ldarg_0); break; |
| | 0 | 311 | | case 1: OpCode(ILOpCode.Ldarg_1); break; |
| | 0 | 312 | | case 2: OpCode(ILOpCode.Ldarg_2); break; |
| | 0 | 313 | | case 3: OpCode(ILOpCode.Ldarg_3); break; |
| | | 314 | | |
| | | 315 | | default: |
| | 0 | 316 | | if (unchecked((uint)argumentIndex) <= byte.MaxValue) |
| | 0 | 317 | | { |
| | 0 | 318 | | OpCode(ILOpCode.Ldarg_s); |
| | 0 | 319 | | CodeBuilder.WriteByte((byte)argumentIndex); |
| | 0 | 320 | | } |
| | 0 | 321 | | else if (unchecked((uint)argumentIndex) <= ushort.MaxValue) |
| | 0 | 322 | | { |
| | 0 | 323 | | OpCode(ILOpCode.Ldarg); |
| | 0 | 324 | | CodeBuilder.WriteUInt16((ushort)argumentIndex); |
| | 0 | 325 | | } |
| | | 326 | | else |
| | 0 | 327 | | { |
| | 0 | 328 | | Throw.ArgumentOutOfRange(nameof(argumentIndex)); |
| | | 329 | | } |
| | | 330 | | |
| | 0 | 331 | | break; |
| | | 332 | | } |
| | 0 | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Encodes argument address load instruction. |
| | | 337 | | /// </summary> |
| | | 338 | | /// <param name="argumentIndex">Index of the argument.</param> |
| | | 339 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentIndex"/> is negative or greater than < |
| | | 340 | | public void LoadArgumentAddress(int argumentIndex) |
| | 0 | 341 | | { |
| | 0 | 342 | | if (unchecked((uint)argumentIndex) <= byte.MaxValue) |
| | 0 | 343 | | { |
| | 0 | 344 | | OpCode(ILOpCode.Ldarga_s); |
| | 0 | 345 | | CodeBuilder.WriteByte((byte)argumentIndex); |
| | 0 | 346 | | } |
| | 0 | 347 | | else if (unchecked((uint)argumentIndex) <= ushort.MaxValue) |
| | 0 | 348 | | { |
| | 0 | 349 | | OpCode(ILOpCode.Ldarga); |
| | 0 | 350 | | CodeBuilder.WriteUInt16((ushort)argumentIndex); |
| | 0 | 351 | | } |
| | | 352 | | else |
| | 0 | 353 | | { |
| | 0 | 354 | | Throw.ArgumentOutOfRange(nameof(argumentIndex)); |
| | | 355 | | } |
| | 0 | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// Encodes argument store instruction. |
| | | 360 | | /// </summary> |
| | | 361 | | /// <param name="argumentIndex">Index of the argument.</param> |
| | | 362 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentIndex"/> is negative or greater than < |
| | | 363 | | public void StoreArgument(int argumentIndex) |
| | 0 | 364 | | { |
| | 0 | 365 | | if (unchecked((uint)argumentIndex) <= byte.MaxValue) |
| | 0 | 366 | | { |
| | 0 | 367 | | OpCode(ILOpCode.Starg_s); |
| | 0 | 368 | | CodeBuilder.WriteByte((byte)argumentIndex); |
| | 0 | 369 | | } |
| | 0 | 370 | | else if (unchecked((uint)argumentIndex) <= ushort.MaxValue) |
| | 0 | 371 | | { |
| | 0 | 372 | | OpCode(ILOpCode.Starg); |
| | 0 | 373 | | CodeBuilder.WriteUInt16((ushort)argumentIndex); |
| | 0 | 374 | | } |
| | | 375 | | else |
| | 0 | 376 | | { |
| | 0 | 377 | | Throw.ArgumentOutOfRange(nameof(argumentIndex)); |
| | | 378 | | } |
| | 0 | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Defines a label that can later be used to mark and refer to a location in the instruction stream. |
| | | 383 | | /// </summary> |
| | | 384 | | /// <returns>Label handle.</returns> |
| | | 385 | | /// <exception cref="InvalidOperationException"><see cref="ControlFlowBuilder"/> is null.</exception> |
| | | 386 | | public LabelHandle DefineLabel() |
| | 0 | 387 | | { |
| | 0 | 388 | | return GetBranchBuilder().AddLabel(); |
| | 0 | 389 | | } |
| | | 390 | | |
| | | 391 | | internal void LabelOperand(ILOpCode code, LabelHandle label, int instructionEndDisplacement, int ilOffset) |
| | 0 | 392 | | { |
| | 0 | 393 | | GetBranchBuilder().AddBranch(Offset, label, instructionEndDisplacement, ilOffset, code); |
| | | 394 | | |
| | | 395 | | // -1 points in the middle of the branch instruction and is thus invalid. |
| | | 396 | | // We want to produce invalid IL so that if the caller doesn't patch the branches |
| | | 397 | | // the branch instructions will be invalid in an obvious way. |
| | 0 | 398 | | if (instructionEndDisplacement == 1) |
| | 0 | 399 | | { |
| | 0 | 400 | | CodeBuilder.WriteSByte(-1); |
| | 0 | 401 | | } |
| | | 402 | | else |
| | 0 | 403 | | { |
| | 0 | 404 | | CodeBuilder.WriteInt32(-1); |
| | 0 | 405 | | } |
| | 0 | 406 | | } |
| | | 407 | | |
| | | 408 | | /// <summary> |
| | | 409 | | /// Encodes a branch instruction. |
| | | 410 | | /// </summary> |
| | | 411 | | /// <param name="code">Branch instruction to encode.</param> |
| | | 412 | | /// <param name="label">Label of the target location in instruction stream.</param> |
| | | 413 | | /// <exception cref="ArgumentException"><paramref name="code"/> is not a branch instruction.</exception> |
| | | 414 | | /// <exception cref="ArgumentException"><paramref name="label"/> was not defined by this encoder.</exception> |
| | | 415 | | /// <exception cref="InvalidOperationException"><see cref="ControlFlowBuilder"/> is null.</exception> |
| | | 416 | | /// <exception cref="ArgumentNullException"><paramref name="label"/> has default value.</exception> |
| | | 417 | | public void Branch(ILOpCode code, LabelHandle label) |
| | 0 | 418 | | { |
| | | 419 | | // throws if code is not a branch: |
| | 0 | 420 | | int operandSize = code.GetBranchOperandSize(); |
| | | 421 | | // We want the offset before we add the opcode. |
| | 0 | 422 | | int ilOffset = Offset; |
| | | 423 | | |
| | 0 | 424 | | OpCode(code); |
| | 0 | 425 | | LabelOperand(code, label, operandSize, ilOffset); |
| | 0 | 426 | | } |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Starts encoding a switch instruction. |
| | | 430 | | /// </summary> |
| | | 431 | | /// <param name="branchCount">The number of branches the instruction will have.</param> |
| | | 432 | | /// <returns>A <see cref="SwitchInstructionEncoder"/> that will |
| | | 433 | | /// be used to emit the labels for the branches.</returns> |
| | | 434 | | /// <remarks> |
| | | 435 | | /// Before using this <see cref="InstructionEncoder"/> in any other way, |
| | | 436 | | /// the method <see cref="SwitchInstructionEncoder.Branch(LabelHandle)"/> |
| | | 437 | | /// must be called on the returned value exactly <paramref name="branchCount"/> |
| | | 438 | | /// times. Failure to do so will throw <see cref="InvalidOperationException"/>. |
| | | 439 | | /// </remarks> |
| | | 440 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="branchCount"/> |
| | | 441 | | /// less than or equal to zero.</exception> |
| | | 442 | | public SwitchInstructionEncoder Switch(int branchCount) |
| | 0 | 443 | | { |
| | 0 | 444 | | if (branchCount <= 0) |
| | 0 | 445 | | { |
| | 0 | 446 | | Throw.ArgumentOutOfRange(nameof(branchCount)); |
| | | 447 | | } |
| | 0 | 448 | | ControlFlowBuilder branchBuilder = GetBranchBuilder(); |
| | | 449 | | |
| | | 450 | | // We want the offset before we add the opcode. |
| | 0 | 451 | | int ilOffset = Offset; |
| | | 452 | | |
| | 0 | 453 | | OpCode(ILOpCode.Switch); |
| | 0 | 454 | | branchBuilder.RemainingSwitchBranches = branchCount; |
| | 0 | 455 | | CodeBuilder.WriteUInt32((uint)branchCount); |
| | | 456 | | |
| | | 457 | | // We calculate the offset where the instruction will end. |
| | | 458 | | // The Offset property now accounts for the opcode byte and |
| | | 459 | | // the four bits of the count, and we also add four bytes for |
| | | 460 | | // each branch we are expecting from the user to emit. |
| | 0 | 461 | | int instructionEnd = Offset + 4 * branchCount; |
| | 0 | 462 | | return new SwitchInstructionEncoder(this, ilOffset, instructionEnd); |
| | 0 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Associates specified label with the current IL offset. |
| | | 467 | | /// </summary> |
| | | 468 | | /// <param name="label">Label to mark.</param> |
| | | 469 | | /// <remarks> |
| | | 470 | | /// A single label may be marked multiple times, the last offset wins. |
| | | 471 | | /// </remarks> |
| | | 472 | | /// <exception cref="InvalidOperationException"><see cref="ControlFlowBuilder"/> is null.</exception> |
| | | 473 | | /// <exception cref="ArgumentException"><paramref name="label"/> was not defined by this encoder.</exception> |
| | | 474 | | /// <exception cref="ArgumentNullException"><paramref name="label"/> has default value.</exception> |
| | | 475 | | public void MarkLabel(LabelHandle label) |
| | 0 | 476 | | { |
| | 0 | 477 | | GetBranchBuilder().MarkLabel(Offset, label); |
| | 0 | 478 | | } |
| | | 479 | | |
| | | 480 | | private ControlFlowBuilder GetBranchBuilder() |
| | 0 | 481 | | { |
| | 0 | 482 | | if (ControlFlowBuilder == null) |
| | 0 | 483 | | { |
| | 0 | 484 | | Throw.ControlFlowBuilderNotAvailable(); |
| | | 485 | | } |
| | | 486 | | |
| | 0 | 487 | | return ControlFlowBuilder; |
| | 0 | 488 | | } |
| | | 489 | | } |
| | | 490 | | } |
| | | 491 | | |