예제 #1
0
        private static void EmitOp(GroboIL il, ExpressionType nodeType, Type type)
        {
            switch (nodeType)
            {
            case ExpressionType.Add:
                il.Add();
                break;

            case ExpressionType.AddChecked:
                il.Add_Ovf(type.Unsigned());
                break;

            case ExpressionType.Subtract:
                il.Sub();
                break;

            case ExpressionType.SubtractChecked:
                il.Sub_Ovf(type.Unsigned());
                break;

            case ExpressionType.Multiply:
                il.Mul();
                break;

            case ExpressionType.MultiplyChecked:
                il.Mul_Ovf(type.Unsigned());
                break;

            case ExpressionType.Divide:
                il.Div(type.Unsigned());
                break;

            case ExpressionType.Modulo:
                il.Rem(type.Unsigned());
                break;

            case ExpressionType.LeftShift:
                il.Shl();
                break;

            case ExpressionType.RightShift:
                il.Shr(type.Unsigned());
                break;

            case ExpressionType.And:
                il.And();
                break;

            case ExpressionType.Or:
                il.Or();
                break;

            case ExpressionType.ExclusiveOr:
                il.Xor();
                break;

            default:
                throw new NotSupportedException("Node type '" + nodeType + "' is not supported");
            }
        }
예제 #2
0
        private void TestSuccess(Type type1, Type type2)
        {
            var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] { type1, type2, }.Where(type => type != null).ToArray(), typeof(string), true);

            using (var il = new GroboIL(method))
            {
                var index = 0;
                if (type1 != null)
                {
                    il.Ldarg(index++);
                }
                else
                {
                    il.Ldnull();
                }
                if (type2 != null)
                {
                    il.Ldarg(index++);
                }
                else
                {
                    il.Ldnull();
                }
                il.Mul();
                il.Pop();
                il.Ret();
                Console.WriteLine(il.GetILCode());
            }
        }
예제 #3
0
파일: Test.cs 프로젝트: qinfengzhu/gremit
        public void TestFarsh()
        {
            var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(int), new[] { typeof(int) }, typeof(Test));

            using (var il = new GroboIL(method))
            {
                var temp = il.DeclareLocal(typeof(int));
                il.Ldarg(0);   // stack: [x]
                var label0 = il.DefineLabel("L");
                il.Br(label0); // goto L_0; stack: [x]

                il.Ldstr("zzz");
                il.Ldobj(typeof(DateTime));
                il.Mul();
                il.Initobj(typeof(int));

                var label1 = il.DefineLabel("L");
                il.MarkLabel(label1);  // stack: [x, 2]
                il.Stloc(temp);        // temp = 2; stack: [x]
                var label2 = il.DefineLabel("L");
                il.MarkLabel(label2);  // stack: [cur]
                il.Ldarg(0);           // stack: [cur, x]
                il.Mul();              // stack: [cur * x = cur]
                il.Ldloc(temp);        // stack: [cur, temp]
                il.Ldc_I4(1);          // stack: [cur, temp, 1]
                il.Sub();              // stack: [cur, temp - 1]
                il.Stloc(temp);        // temp = temp - 1; stack: [cur]
                il.Ldloc(temp);        // stack: [cur, temp]
                il.Ldc_I4(0);          // stack: [cur, temp, 0]
                il.Bgt(label2, false); // if(temp > 0) goto L_2; stack: [cur]
                var label3 = il.DefineLabel("L");
                il.Br(label3);         // goto L_3; stack: [cur]
                il.MarkLabel(label0);  // stack: [x]
                il.Ldc_I4(2);          // stack: [x, 2]
                il.Br(label1);         // goto L_1; stack: [x, 2]
                il.MarkLabel(label3);  // stack: [cur]
                il.Ret();              // return cur; stack: []
                Console.Write(il.GetILCode());
            }
        }
예제 #4
0
        public void TestZzz()
        {
            var asm = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("foo"), AssemblyBuilderAccess.RunAndSave);

            var mod  = asm.DefineDynamicModule("mymod", "tmp.dll", true);
            var type = mod.DefineType("baz", TypeAttributes.Public | TypeAttributes.Class);
            var meth = type.DefineMethod("go", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new[] { typeof(int) });

            var document = mod.DefineDocument("TestDebug2.txt", Guid.Empty, Guid.Empty, Guid.Empty); //Expression.SymbolDocument("TestDebug2.txt");

            meth.DefineParameter(1, ParameterAttributes.In, "$x");

            //var di = Expression.DebugInfo(sdi, 2, 2, 2, 13);

            //var exp = Expression.Divide(Expression.Constant(2), Expression.Subtract(Expression.Constant(4), Expression.Constant(4)));
            //var block = Expression.Block(di, exp);

            using (var il = new GroboIL(meth))
            {
//                var tst = Expression.Block(Expression.DebugInfo(sdi, 6, 20, 6, 27), Expression.Equal(variable, zero));
//                var iftrue = Expression.Block(Expression.DebugInfo(sdi, 10, 20, 10, 26), Expression.Add(variable, two));
//                var iffalse = Expression.Block(Expression.DebugInfo(sdi, 14, 20, 14, 26), Expression.Divide(variable, two));
//                var exp = Expression.Condition(tst, iftrue, iffalse);
//                var block = Expression.Block(Expression.DebugInfo(sdi, 4, 16, 15, 10), exp);

//                        nop                      // []
//        nop                      // []
//        ldarg.0                  // [Int32]
//        ldc.i4.0                 // [Int32, Int32]
//        ceq                      // [Int32]
//        brfalse ifFalse_5        // []
//        nop                      // []
//        ldarg.0                  // [Int32]
//        ldc.i4.2                 // [Int32, Int32]
//        add                      // [Int32]
//        br done_8                // [Int32]
//ifFalse_5:                       // []
//        nop                      // []
//        ldarg.0                  // [Int32]
//        ldc.i4.2                 // [Int32, Int32]
//        div                      // [Int32]
//done_8:                          // [Int32]
//        ret                      // []

                il.MarkSequencePoint(document, 3, 9, 3, 15);
                il.Nop();
                il.MarkSequencePoint(document, 4, 13, 4, 19);
                il.Nop();
                il.Ldarg(0);
                il.Ldc_I4(0);
                il.Ceq();
                var label = il.DefineLabel("ifFalse");
                il.Brfalse(label);
                il.MarkSequencePoint(document, 7, 13, 7, 19);
                il.Nop();
                il.Ldarg(0);
                il.Ldc_I4(2);
                il.Add();
                var doneLabel = il.DefineLabel("done");
                il.Br(doneLabel);
                il.MarkLabel(label);
                il.MarkSequencePoint(document, 10, 13, 10, 19);
                il.Nop();
                il.Ldarg(0);
                il.Ldc_I4(2);
                il.Mul();
                il.MarkLabel(doneLabel);
                il.MarkSequencePoint(document, 12, 5, 12, 6);
                il.Nop();
                il.Ret();
            }

            var newtype = type.CreateType();

            asm.Save("tmp.dll");
            newtype.GetMethod("go").Invoke(null, new object[] { 0 });
            //meth.Invoke(null, new object[0]);
            //lambda.DynamicInvoke(new object[0]);
            Console.WriteLine(" ");
        }
예제 #5
0
        internal static void Resize(GroboIL il, GroboIL.Local buffer, GroboIL.Local offset /*, GroboIL.Local len*/)
        {
            var totalLen = il.DeclareLocal(typeof(int));

            //offset add typesize = minSize
            il.Ldloc(offset);
            il.Add();
            il.Stloc(totalLen);


            var exit = il.DefineLabel("exit");

            //Compare if minSize < buffer.len
            il.Ldloc(totalLen);
            il.Ldloc(buffer);
            il.Ldlen();
            il.Clt(false);
            il.Brtrue(exit);


            var avar = il.DeclareLocal(typeof(byte));

            il.Ldc_I4(2);
            il.Stloc(avar);

            var point = il.DefineLabel("for_label");

            il.MarkLabel(point);

            //body

            il.Ldloc(avar);
            il.Ldc_I4(2);
            il.Mul();
            il.Stloc(avar);

            //end body


            il.Ldloc(buffer);
            il.Ldlen();
            il.Ldloc(avar);
            il.Mul();
            //OutputValue(il,typeof(int));

            il.Ldloc(totalLen);
            //OutputValue(il, typeof(int));

            il.Clt(false);
            //OutputValue(il, typeof(bool));
            il.Brtrue(point);

            il.Ldloca(buffer);

            il.Ldloc(avar);
            il.Ldloc(buffer);
            il.Ldlen();
            il.Mul();
            //OutputValue(il, typeof(int));

            il.Call(typeof(Array).GetMethod("Resize", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(typeof(byte)));


            il.MarkLabel(exit);
        }