is there such thing as a 'while' loop opcode.
what are the opcodes i use to emit this loop:
int i = 0;
while(true)
{
if(i == 3)
{
break;
}
i++;
}
looking for something performance specific,
thanks in advance,
Jer,
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
J APosted Dec 2, 2008, 12:08 AM
I have attempted to write this using emit.
Thanks in advance for any help.
-Jer
int i = 0;
while(true)
{
i++;
if(i >= 100)
{
break;
}
}
I get:
Unhandled Exception: System.InvalidProgramException: JIT Compiler encountered an
internal limitation.
at Hello()
at test.Main(String[] d)
test.cs
//----------------------------------------
using System;
using System.Reflection;
using System.Reflection.Emit;
public class test {
private delegate int HelloDelegate();
public static void Main (string [] d)
{
DynamicMethod hello = new DynamicMethod("Hello", typeof(int), null, typeof(string).Module);
Type[] writeStringArgs = {typeof(string)};
MethodInfo writeString = typeof(Console).GetMethod("WriteLine",
writeStringArgs);
ILGenerator il = hello.GetILGenerator(256);
Label label1 = il.DefineLabel();
Label label2 = il.DefineLabel();
il.Emit(OpCodes.Ldc_I4_0); // 1 on stack
il.MarkLabel(label2);
il.Emit(OpCodes.Ldstr,"test"); // 2 on stack
il.EmitCall(OpCodes.Call, writeString,null); // pop
il.Emit(OpCodes.Ldc_I4_1); // 2 on stack
il.Emit(OpCodes.Add); // 1 on stack
il.Emit(OpCodes.Dup); // 2 on stack
il.Emit(OpCodes.Ldc_I4,100); // 3 on stack
il.Emit(OpCodes.Bge, label1); // if true, no stack
il.Emit(OpCodes.Pop); // 1 popped
il.Emit(OpCodes.Pop); // 1 popped
il.Emit(OpCodes.Br_S,label2); // loop
il.MarkLabel(label1);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Ret);
HelloDelegate hi =
(HelloDelegate) hello.CreateDelegate(typeof(HelloDelegate));
hi();
}
}
AlanPosted Dec 1, 2008, 4:44 PM
There isn't a specific opcode for a while loop. It's coded in much the same as it would be in assembler.
If you put that code in a method and then examine the output with a disassembler such as ildasm or Redgate Reflector you'll see which opcodes are actually generated in the MSIL.
J APosted Dec 1, 2008, 2:04 PM
using Reflection.Emit, dotnet api, you can code performance critical operations using opcodes from csharp.
I am trying to learn how to use opcodes, and I would like to see how do take this standard csharp code, and code it using Emit opcodes.
I have been scouring the net trying to find if there is a specific opcode for a 'while' expression.
I hope this gives you enough info, so you can help.
thanks in advance,
Jer
Bechir BejaouiPosted Dec 1, 2008, 6:45 AM