Exemple #1
0
 private static bool String_toCharArray(EmitIntrinsicContext eic)
 {
     if (eic.MatchRange(-1, 2) &&
         eic.Match(-1, NormalizedByteCode.__ldc_nothrow))
     {
         string str = eic.GetStringLiteral(-1);
         // arbitrary length for "big" strings
         if (str.Length > 128)
         {
             eic.Emitter.Emit(OpCodes.Pop);
             EmitLoadCharArrayLiteral(eic.Emitter, str, eic.Caller.DeclaringType);
             return(true);
         }
     }
     return(false);
 }
Exemple #2
0
 // this intrinsifies the unsafe.objectFieldOffset(XXX.class.getDeclaredField("xxx")) pattern
 // to avoid initializing the full reflection machinery at this point
 private static bool Class_getDeclaredField(EmitIntrinsicContext eic)
 {
     // validate that we're inside the XXX class and that xxx is an instance field of that class
     if (eic.MatchRange(-2, 4) &&
         eic.Match(-2, NormalizedByteCode.__ldc) && eic.GetClassLiteral(-2) == eic.Caller.DeclaringType &&
         eic.Match(-1, NormalizedByteCode.__ldc_nothrow) &&
         eic.Match(1, NormalizedByteCode.__invokevirtual))
     {
         FieldWrapper field     = null;
         string       fieldName = eic.GetStringLiteral(-1);
         foreach (FieldWrapper fw in eic.Caller.DeclaringType.GetFields())
         {
             if (fw.Name == fieldName)
             {
                 if (field != null)
                 {
                     return(false);
                 }
                 field = fw;
             }
         }
         if (field == null || field.IsStatic)
         {
             return(false);
         }
         ClassFile.ConstantPoolItemMI cpi = eic.GetMethodref(1);
         if (cpi.Class == "sun.misc.Unsafe" && cpi.Name == "objectFieldOffset" && cpi.Signature == "(Ljava.lang.reflect.Field;)J")
         {
             MethodWrapper mw = ClassLoaderWrapper.LoadClassCritical("sun.misc.Unsafe")
                                .GetMethodWrapper("objectFieldOffset", "(Ljava.lang.Class;Ljava.lang.String;)J", false);
             mw.Link();
             mw.EmitCallvirt(eic.Emitter);
             eic.PatchOpCode(1, NormalizedByteCode.__nop);
             return(true);
         }
     }
     return(false);
 }
Exemple #3
0
        // this intrinsifies the following two patterns:
        //   unsafe.objectFieldOffset(XXX.class.getDeclaredField("xxx"));
        // and
        //   Class k = XXX.class;
        //   unsafe.objectFieldOffset(k.getDeclaredField("xxx"));
        // to avoid initializing the full reflection machinery at this point
        private static bool Class_getDeclaredField(EmitIntrinsicContext eic)
        {
            if (eic.Caller.DeclaringType.GetClassLoader() != CoreClasses.java.lang.Object.Wrapper.GetClassLoader())
            {
                // we can only do this optimization when compiling the trusted core classes
                return(false);
            }
            TypeWrapper fieldClass;

            if (eic.MatchRange(-2, 4) &&
                eic.Match(-2, NormalizedByteCode.__ldc) &&
                eic.Match(-1, NormalizedByteCode.__ldc_nothrow) &&
                eic.Match(1, NormalizedByteCode.__invokevirtual))
            {
                // unsafe.objectFieldOffset(XXX.class.getDeclaredField("xxx"));
                fieldClass = eic.GetClassLiteral(-2);
            }
            else if (eic.MatchRange(-5, 7) &&
                     eic.Match(-5, NormalizedByteCode.__ldc) &&
                     eic.Match(-4, NormalizedByteCode.__astore) &&
                     eic.Match(-3, NormalizedByteCode.__getstatic) &&
                     eic.Match(-2, NormalizedByteCode.__aload, eic.Code[eic.OpcodeIndex - 4].NormalizedArg1) &&
                     eic.Match(-1, NormalizedByteCode.__ldc_nothrow) &&
                     eic.Match(1, NormalizedByteCode.__invokevirtual))
            {
                // Class k = XXX.class;
                // unsafe.objectFieldOffset(k.getDeclaredField("xxx"));
                fieldClass = eic.GetClassLiteral(-5);
            }
            else
            {
                return(false);
            }
            FieldWrapper field     = null;
            string       fieldName = eic.GetStringLiteral(-1);

            foreach (FieldWrapper fw in fieldClass.GetFields())
            {
                if (fw.Name == fieldName)
                {
                    if (field != null)
                    {
                        return(false);
                    }
                    field = fw;
                }
            }
            if (field == null || field.IsStatic)
            {
                return(false);
            }
            ClassFile.ConstantPoolItemMI cpi = eic.GetMethodref(1);
            if (cpi.Class == "sun.misc.Unsafe" && cpi.Name == "objectFieldOffset" && cpi.Signature == "(Ljava.lang.reflect.Field;)J")
            {
                MethodWrapper mw = ClassLoaderWrapper.LoadClassCritical("sun.misc.Unsafe")
                                   .GetMethodWrapper("objectFieldOffset", "(Ljava.lang.Class;Ljava.lang.String;)J", false);
                mw.Link();
                mw.EmitCallvirt(eic.Emitter);
                eic.PatchOpCode(1, NormalizedByteCode.__nop);
                return(true);
            }
            return(false);
        }