Пример #1
0
 public void EmitReturnDefaultValue(Type type, GroboIL.Label valueIsNullLabel, GroboIL.Label valueIsNotNullLabel)
 {
     Il.Br(valueIsNotNullLabel);
     MarkLabelAndSurroundWithSP(valueIsNullLabel);
     Il.Pop();
     EmitLoadDefaultValue(type);
     MarkLabelAndSurroundWithSP(valueIsNotNullLabel);
 }
        public void SkipValue()
        {
            LoadIndexByRef(); // stack: [ref index]
            LoadIndex();      // stack: [ref index, index]

            // todo: сделать switch
            Il.Ldloc(TypeCode);                         // stack: [ref index, index, TypeCode]
            Il.Ldc_I4((int)GroBufTypeCode.DateTimeOld); // stack: [ref index, index, TypeCode, GroBufTypeCode.DateTimeOld]
            var notDateTimeLabel = Il.DefineLabel("notDateTime");

            Il.Bne_Un(notDateTimeLabel); // if(TypeCode != GroBufTypeCode.DateTimeOld) goto notDateTime; stack: [ref index, index]
            Il.Ldc_I4(8);                // stack: [ref index, index, 8]
            AssertLength();
            GoToCurrentLocation();       // stack: [ref index, index, &data[index]]
            Il.Ldc_I4(4);
            Il.Add();
            Il.Ldind(typeof(int)); // stack: [ref index, index, (int)(&data[index])]
            Il.Ldc_I4(31);         // stack: [ref index, index, (int)&data[index], 31]
            Il.Shr(true);          // stack: [ref index, index, (int)&data[index] >> 31]
            Il.Ldc_I4(8);          // stack: [ref index, index, (int)&data[index] >> 31, 8]
            Il.Add();              // stack: [ref index, index, (int)&data[index] >> 31 + 8]
            var increaseLabel = Il.DefineLabel("increase");

            Il.Br(increaseLabel);

            Il.MarkLabel(notDateTimeLabel);
            LoadField(Context.Lengths);

            Il.Ldloc(TypeCode);       // stack: [ref index, index, lengths, typeCode]
            Il.Ldelem(typeof(int));   // stack: [ref index, index, lengths[typeCode]]
            Il.Dup();                 // stack: [ref index, index, lengths[typeCode], lengths[typeCode]]
            Il.Ldc_I4(-1);            // stack: [ref index, index, lengths[typeCode], lengths[typeCode], -1]
            Il.Bne_Un(increaseLabel); // if(lengths[typeCode] != -1) goto increase;

            Il.Ldc_I4(4);
            AssertLength();
            Il.Pop();               // stack: [ref index, index]
            Il.Dup();               // stack: [ref index, index, index]
            LoadData();             // stack: [ref index, index, index, pinnedData]
            Il.Add();               // stack: [ref index, index, index + pinnedData]
            Il.Ldind(typeof(uint)); // stack: [ref index, index, *(uint*)(pinnedData + index)]
            Il.Ldc_I4(4);           // stack: [ref index, index, *(uint*)(pinnedData + index), 4]
            Il.Add();               // stack: [ref index, *(uint*)(pinnedData + index) + 4]

            Il.MarkLabel(increaseLabel);
            Il.Dup();              // stack: [ref index, length, length]
            AssertLength();        // stack: [ref index, length]
            Il.Add();              // stack: [ref index, index + length]
            Il.Stind(typeof(int)); // index = index + length
        }
Пример #3
0
 public void EmitConvert(Type from, Type to, bool check = false)
 {
     if (from == to)
     {
         return;
     }
     if (!from.IsValueType)
     {
         if (!to.IsValueType)
         {
             Il.Castclass(to);
         }
         else
         {
             if (from != typeof(object) && !(from == typeof(Enum) && to.IsEnum))
             {
                 throw new InvalidCastException("Cannot cast an object of type '" + from + "' to type '" + to + "'");
             }
             Il.Unbox_Any(to);
         }
     }
     else
     {
         if (!to.IsValueType)
         {
             if (to != typeof(object) && !(to == typeof(Enum) && from.IsEnum))
             {
                 throw new InvalidCastException("Cannot cast an object of type '" + from + "' to type '" + to + "'");
             }
             Il.Box(from);
         }
         else
         {
             if (to.IsNullable())
             {
                 var toArgument = to.GetGenericArguments()[0];
                 if (from.IsNullable())
                 {
                     var fromArgument = from.GetGenericArguments()[0];
                     using (var temp = DeclareLocal(from))
                     {
                         Il.Stloc(temp);
                         Il.Ldloca(temp);
                         EmitHasValueAccess(from);
                         var valueIsNullLabel = Il.DefineLabel("valueIsNull");
                         Il.Brfalse(valueIsNullLabel);
                         Il.Ldloca(temp);
                         EmitValueAccess(from);
                         if (toArgument != fromArgument)
                         {
                             EmitConvert(fromArgument, toArgument, check);
                         }
                         Il.Newobj(to.GetConstructor(new[] { toArgument }));
                         var doneLabel = Il.DefineLabel("done");
                         Il.Br(doneLabel);
                         MarkLabelAndSurroundWithSP(valueIsNullLabel);
                         EmitLoadDefaultValue(to);
                         MarkLabelAndSurroundWithSP(doneLabel);
                     }
                 }
                 else
                 {
                     if (toArgument != from)
                     {
                         EmitConvert(from, toArgument, check);
                     }
                     Il.Newobj(to.GetConstructor(new[] { toArgument }));
                 }
             }
             else if (from.IsNullable())
             {
                 var fromArgument = from.GetGenericArguments()[0];
                 using (var temp = DeclareLocal(from))
                 {
                     Il.Stloc(temp);
                     Il.Ldloca(temp);
                     var valueIsNullLabel = Il.DefineLabel("valueIsNull");
                     Il.Brfalse(valueIsNullLabel);
                     Il.Ldloca(temp);
                     EmitValueAccess(from);
                     if (to != fromArgument)
                     {
                         EmitConvert(fromArgument, to, check);
                     }
                     var doneLabel = Il.DefineLabel("done");
                     Il.Br(doneLabel);
                     MarkLabelAndSurroundWithSP(valueIsNullLabel);
                     EmitLoadDefaultValue(to);
                     MarkLabelAndSurroundWithSP(doneLabel);
                 }
             }
             else if (to.IsEnum || to == typeof(Enum))
             {
                 EmitConvert(from, typeof(int), check);
             }
             else if (from.IsEnum || from == typeof(Enum))
             {
                 EmitConvert(typeof(int), to, check);
             }
             else
             {
                 if (!check)
                 {
                     EmitConvertValue(Il, from, to);
                 }
                 else
                 {
                     EmitConvertValueChecked(Il, from, to);
                 }
             }
         }
     }
 }
Пример #4
0
        public void EmitArithmeticOperation(ExpressionType nodeType, Type resultType, Type leftType, Type rightType, MethodInfo method)
        {
            if (!leftType.IsNullable() && !rightType.IsNullable())
            {
                if (method != null)
                {
                    Il.Call(method);
                }
                else
                {
                    if (leftType.IsStruct())
                    {
                        throw new InvalidOperationException("Unable to perfrom operation '" + nodeType + "' to a struct of type '" + leftType + "'");
                    }
                    if (rightType.IsStruct())
                    {
                        throw new InvalidOperationException("Unable to perfrom operation '" + nodeType + "' to a struct of type '" + rightType + "'");
                    }
                    EmitOp(Il, nodeType, resultType);
                }
            }
            else
            {
                using (var localLeft = DeclareLocal(leftType))
                    using (var localRight = DeclareLocal(rightType))
                    {
                        Il.Stloc(localRight);
                        Il.Stloc(localLeft);
                        var returnNullLabel = Il.DefineLabel("returnNull");
                        if (leftType.IsNullable())
                        {
                            Il.Ldloca(localLeft);
                            EmitHasValueAccess(leftType);
                            Il.Brfalse(returnNullLabel);
                        }
                        if (rightType.IsNullable())
                        {
                            Il.Ldloca(localRight);
                            EmitHasValueAccess(rightType);
                            Il.Brfalse(returnNullLabel);
                        }
                        if (!leftType.IsNullable())
                        {
                            Il.Ldloc(localLeft);
                        }
                        else
                        {
                            Il.Ldloca(localLeft);
                            EmitValueAccess(leftType);
                        }
                        if (!rightType.IsNullable())
                        {
                            Il.Ldloc(localRight);
                        }
                        else
                        {
                            Il.Ldloca(localRight);
                            EmitValueAccess(rightType);
                        }
                        Type argumentType = resultType.GetGenericArguments()[0];
                        if (method != null)
                        {
                            Il.Call(method);
                        }
                        else
                        {
                            EmitOp(Il, nodeType, argumentType);
                        }
                        Il.Newobj(resultType.GetConstructor(new[] { argumentType }));

                        var doneLabel = Il.DefineLabel("done");
                        Il.Br(doneLabel);
                        MarkLabelAndSurroundWithSP(returnNullLabel);
                        EmitLoadDefaultValue(resultType);
                        MarkLabelAndSurroundWithSP(doneLabel);
                    }
            }
        }