Exemple #1
0
        static byte TestBool(JavaCode code, JavaType stackTop)
        {
            if (stackTop.IsReference)
            {
                return(0xC7); // ifnonnull
            }

            if (stackTop.PrimitiveType == TypeCode.Int64 ||
                stackTop.PrimitiveType == TypeCode.UInt64 ||
                stackTop.PrimitiveType == TypeCode.Int32 ||
                stackTop.PrimitiveType == TypeCode.UInt32 ||
                stackTop.PrimitiveType == TypeCode.Int16 ||
                stackTop.PrimitiveType == TypeCode.UInt16 ||
                stackTop.PrimitiveType == TypeCode.SByte ||
                stackTop.PrimitiveType == TypeCode.Byte ||
                stackTop.PrimitiveType == TypeCode.Char ||
                stackTop.PrimitiveType == TypeCode.Boolean)
            {
                if (stackTop.PrimitiveType == TypeCode.Int64 ||
                    stackTop.PrimitiveType == TypeCode.UInt64)
                {
                    CilMain.MakeRoomForCategory2ValueOnStack(code, JavaType.LongType);
                    code.NewInstruction(0x09 /* lconst_0 (long) */, null, null);
                    code.NewInstruction(0x94 /* lcmp (long) */, null, null);
                }

                return(0x9A); // ifne != zero
            }

            throw new InvalidProgramException();
        }
Exemple #2
0
        static int ConvertToLong(JavaCode code, TypeCode oldType, TypeCode newType)
        {
            if (oldType == TypeCode.Int64 || oldType == TypeCode.UInt64)
            {
                return(0x00); // nop
            }
            if (oldType == TypeCode.Double)
            {
                return(0x8F); // d2l
            }
            if (oldType == TypeCode.Single)
            {
                return(0x8C); // f2l
            }
            if (newType == TypeCode.UInt64)
            {
                code.NewInstruction(0x85 /* i2l */, null, null);
                code.StackMap.PushStack(JavaType.LongType);
                long maskValue = (oldType == TypeCode.Byte)   ? 0xFF
                               : (oldType == TypeCode.UInt16) ? 0xFFFF
                                                              : 0xFFFFFFFF;
                code.NewInstruction(0x12 /* ldc */, null, (long)maskValue);
                code.StackMap.PushStack(JavaType.LongType);
                code.StackMap.PopStack(CilMain.Where);
                code.StackMap.PopStack(CilMain.Where);
                return(0x7F); // land
            }

            CilMain.MakeRoomForCategory2ValueOnStack(code);
            return(0x85); // i2l
        }
Exemple #3
0
        static int ConvertToFloat(JavaCode code, TypeCode oldType, TypeCode newType, bool unsigned)
        {
            if (newType == TypeCode.Single)
            {
                //
                // convert to float
                //

                if (oldType == TypeCode.Single)
                {
                    return(0x00); // nop
                }
                if (oldType == TypeCode.Double)
                {
                    return(0x90); // d2f
                }
                if (oldType == TypeCode.Int64)
                {
                    return(0x89); // l2f
                }
                if (oldType != TypeCode.UInt64)
                {
                    return(0x86); // i2f
                }
                CallUnsignedLongToDouble(code);
                code.NewInstruction(0x8D /* d2f */, null, null);
                return(-1); // no output
            }

            else if (!unsigned)
            {
                //
                // convert to double
                //

                if (oldType == TypeCode.Double)
                {
                    return(0x00); // nop
                }
                if (oldType == TypeCode.Single)
                {
                    CilMain.MakeRoomForCategory2ValueOnStack(code);
                    return(0x8D); // f2d
                }

                if (oldType == TypeCode.Int64)
                {
                    return(0x8A); // l2d
                }
                if (oldType != TypeCode.UInt64)
                {
                    return(0x87); // i2d
                }
                CallUnsignedLongToDouble(code);
                return(-1); // no output
            }

            else
            {
                //
                // convert integer, interpreted as unsigned, to a double
                //

                if (oldType == TypeCode.Single || oldType == TypeCode.Double)
                {
                    throw new InvalidProgramException();
                }

                bool fromInt32 = (oldType != TypeCode.Int64 && oldType != TypeCode.UInt64);
                if (fromInt32)
                {
                    CilMain.MakeRoomForCategory2ValueOnStack(code);

                    code.NewInstruction(0x85 /* i2l */, null, null);
                    code.StackMap.PushStack(JavaType.LongType);

                    if (oldType == TypeCode.UInt32)
                    {
                        code.NewInstruction(0x12 /* ldc */, null, (long)0xFFFFFFFF);
                        code.StackMap.PushStack(JavaType.LongType);
                        code.NewInstruction(0x7F /* land */, null, null);
                        code.StackMap.PopStack(CilMain.Where);
                    }
                }

                CallUnsignedLongToDouble(code);

                if (fromInt32)
                {
                    code.StackMap.PopStack(CilMain.Where);
                }

                return(-1); // no output
            }

            void CallUnsignedLongToDouble(JavaCode code)
            {
                code.NewInstruction(0xB8 /* invokestatic */,
                                    CilType.From(JavaType.DoubleType).AsWritableClass,
                                    new JavaMethodRef("UnsignedLongToDouble",
                                                      JavaType.DoubleType, JavaType.LongType));
            }
        }