title: Error with short form opcodes in Reflection.Emit
body: <p>I'm making a small language that is very similar to <code>hlsl</code> but supports only pixel shaders. This language uses <code>reflection.emit</code> to build .NET assemblies that implement the same functionality. I'm currently testing my implementation of the branch instruction <code>if</code> and in one of my unit tests (a large <code>if</code> with inner <code>if/else</code>s) failed with the following error message:</p>
  <blockquote>
  <p>System.NotSupportedException : Illegal one-byte branch at position: 32. Requested branch was: 132.</p>
  </blockquote>
  <p>I've traced the problem to the use of short form instructions in my case <code>OpCodes.Br_S</code>. The solution was simple, I've replaced <code>OpCodes.Br_S</code> with <code>OpCodes.Br</code> however I have a couple of questions about this solution:</p>
  <p>Does this solution have an impact on the performance of the generated code?</p>
  <p>If I want to correctly generate <code>Br_S</code> for single byte and <code>Br</code> for the other cases how can I do that? The problem here is that I'm using a visitor pattern and for a branch instruction like the <code>if</code> I have to output <code>Br</code> or <code>Br_s</code> first and at that point, I have no way of knowing if the remaining code will require more than a single byte to jump to the label. To better illustrate my question this is the code that I generate for the following statements:</p>
  <p>My Language:</p>
  <pre><code>int a = -1; if (1&gt;1) { a=1; } else if (2&gt;2) { a=2; }
  </code></pre>
  <p>IL:</p>
  <pre class="lang-il prettyprint-override"><code>.method public virtual final instance int32 Main() cil managed
  {
  
      .maxstack 4
      .locals init (
          [0] int32 num)
      L_0000: ldc.i4.m1 
      L_0001: stloc.0 
      L_0002: ldc.i4.1 
      L_0003: ldc.i4.1 
      L_0004: ble.s L_000a
      L_0006: ldc.i4.1 
      L_0007: stloc.0 
      L_0008: br.s L_0010
      L_000a: ldc.i4.2 
      L_000b: ldc.i4.2 
      L_000c: ble.s L_0010
      L_000e: ldc.i4.2 
      L_000f: stloc.0 
      L_0010: ldloc.0 
      L_0011: ret 
  }
  </code></pre>
  <p>In this case, I'm using two short form instructions <code>ble.s</code> and <code>br.s</code> to implement the <code>if</code>s just like what the .NET compiler does. However .NET compiler is able to choose <code>br.s</code> or <code>br</code> according to the cases, my problem is how can I do something similar?</p>
  <p>Tnks</p>