Patch looks good, do you have a SVN  account?<br><br><div><br><div class="gmail_quote">On Wed, Mar 17, 2010 at 2:22 PM, Thierry Lafage <span dir="ltr">&lt;<a href="mailto:thierry.lafage@inria.fr">thierry.lafage@inria.fr</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi all,<br>
<br>
Here is a small patch for ilasm which makes it replace ldc.i4 with short forms when possible.<br>
Note that I&#39;m a C# and monodevelop newbie (but I tried to respect the coding guidelines).<br>
I suggest to add the &quot;-opt:ldc&quot; because I would like to add the same kind of optimization for ldloc/stloc (use short forms when possible). For this latter one, the command line option could be &quot;-opt:loc&quot; (and we could add &quot;-opt:all&quot; to enable both at once).<br>

I think the impact of such a patch is quite limited, but it&#39;s important for us (the gcc4cli team) since we rely on ilasm to assemble the CIL code produced by our compiler. And these &quot;optimizations&quot; tasks are better suited to the assembler than to the compiler itself.<br>

<br>
Regards,<br><font color="#888888">
Thierry Lafage.<br>
<br>
</font><br>Index: ilasm/Driver.cs<br>
===================================================================<br>
--- ilasm/Driver.cs     (revision 153715)<br>
+++ ilasm/Driver.cs     (working copy)<br>
@@ -20,6 +20,8 @@<br>
<br>
         public class Driver {<br>
<br>
+                               internal static bool opt_ldc = false;<br>
+<br>
                 enum Target {<br>
                         Dll,<br>
                         Exe<br>
@@ -270,6 +272,10 @@<br>
                                                else<br>
                                                        keyname = command_arg;<br>
                                                break;<br>
+                                        case &quot;opt&quot;:<br>
+                                               if (&quot;ldc&quot; == command_arg)<br>
+                                                       Driver.opt_ldc = true;<br>
+                                               break;<br>
                                         case &quot;scan_only&quot;:<br>
                                                 scan_only = true;<br>
                                                 break;<br>
@@ -344,6 +350,7 @@<br>
                                         &quot;   /exe               Compile to executable.\n&quot; +<br>
                                         &quot;   /dll               Compile to library.\n&quot; +<br>
                                         &quot;   /debug             Include debug information.\n&quot; +<br>
+                                        &quot;   /opt:ldc           Optimize ldc instructions (use shorter forms when possible).\n&quot; +<br>
                                        &quot;   /key:keyfile       Strongname using the specified key file\n&quot; +<br>
                                        &quot;   /key:@container    Strongname using the specified key container\n&quot; +<br>
                                         &quot;Options can be of the form -option or /option\n&quot;);<br>
Index: ilasm/codegen/IntInstr.cs<br>
===================================================================<br>
--- ilasm/codegen/IntInstr.cs   (revision 153715)<br>
+++ ilasm/codegen/IntInstr.cs   (working copy)<br>
@@ -27,9 +27,35 @@<br>
                 public override void Emit (CodeGen code_gen, MethodDef meth,<br>
                                           PEAPI.CILInstructions cil)<br>
                 {<br>
-                        cil.IntInst (op, operand);<br>
-                }<br>
+                       if (Driver.opt_ldc &amp;&amp; op == PEAPI.IntOp.ldc_i4) {<br>
+                               if (operand &gt;= -1 &amp;&amp; operand &lt;= 8)<br>
+                                       cil.Inst (getOptLdcOp (operand));<br>
+                               else if (operand &gt;= -128 &amp;&amp; operand &lt;= 127)<br>
+                                       cil.IntInst (PEAPI.IntOp.ldc_i4_s,<br>
+                                               operand);<br>
+                               else<br>
+                                       cil.IntInst (op, operand);<br>
+                       } else<br>
+                               cil.IntInst (op, operand);<br>
+               }<br>
<br>
+               private PEAPI.Op getOptLdcOp (int operand)<br>
+               {<br>
+                       switch (operand) {<br>
+                       case -1: return PEAPI.Op.ldc_i4_m1;<br>
+                       case 0:  return PEAPI.Op.ldc_i4_0;<br>
+                       case 1:  return PEAPI.Op.ldc_i4_1;<br>
+                       case 2:  return PEAPI.Op.ldc_i4_2;<br>
+                       case 3:  return PEAPI.Op.ldc_i4_3;<br>
+                       case 4:  return PEAPI.Op.ldc_i4_4;<br>
+                       case 5:  return PEAPI.Op.ldc_i4_5;<br>
+                       case 6:  return PEAPI.Op.ldc_i4_6;<br>
+                       case 7:  return PEAPI.Op.ldc_i4_7;<br>
+                       case 8:  return PEAPI.Op.ldc_i4_8;<br>
+                       default:<br>
+                               throw new ArgumentOutOfRangeException (&quot;operand&quot;);<br>
+                       }<br>
+               }<br>
         }<br>
<br>
 }<br>
<br>_______________________________________________<br>
Mono-devel-list mailing list<br>
<a href="mailto:Mono-devel-list@lists.ximian.com">Mono-devel-list@lists.ximian.com</a><br>
<a href="http://lists.ximian.com/mailman/listinfo/mono-devel-list" target="_blank">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a><br>
<br></blockquote></div><br></div>