Ejemplo n.º 1
0
 internal static Rethrow Rethrow(UnaryExpression expression)
 {
     return(new Rethrow()
     {
         Operand = Serialize(expression.Operand),
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
         Type = expression.Type.Null(t => TypeRef.Serialize(t)),
     }.If(n => n.Type == null, n => n.TypeHint = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 2
0
 internal static MultiplyChecked MultiplyChecked(BinaryExpression expression)
 {
     return(new MultiplyChecked()
     {
         Left = Serialize(expression.Left),
         Right = Serialize(expression.Right),
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 3
0
        public void ShouldSetProperties()
        {
            var actual = MethodRef.CreateLocalReference(_nA, _mA, _dA);

            Assert.AreSame(_nA, actual.Name);
            Assert.AreSame(_dA, actual.Declaration);
            Assert.AreSame(_mA, actual.Method);
            Assert.IsFalse(actual.IsAssemblyReference);
        }
Ejemplo n.º 4
0
        public void ShouldSetProperties2()
        {
            var actual = MethodRef.CreateAssemblyReference(_nA, _mA);

            Assert.AreSame(_nA, actual.Name);
            Assert.IsNull(actual.Declaration);
            Assert.AreSame(_mA, actual.Method);
            Assert.IsTrue(actual.IsAssemblyReference);
        }
Ejemplo n.º 5
0
 internal static Divide Divide(BinaryExpression expression)
 {
     return(new Divide()
     {
         Left = Serialize(expression.Left),
         Right = Serialize(expression.Right),
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 6
0
 internal static ConvertChecked ConvertChecked(UnaryExpression expression)
 {
     return(new ConvertChecked()
     {
         Operand = Serialize(expression.Operand),
         Type = TypeRef.Serialize(expression.Type),
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 7
0
 internal static Convert Convert(UnaryExpression expression)
 {
     return(new Convert()
     {
         Operand = Serialize(expression.Operand),
         Type = TypeRef.Serialize(expression.Type),
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
     });
 }
Ejemplo n.º 8
0
        public static Class forName0(Type clazz, string classname, bool initialize, ClassLoader loader, Class caller)
        {
            Class found;

            Console.WriteLine(" searching for {0}", classname);

            if (loader != null)
            {
                found = _loadClass[loader](classname, initialize);
            }
            else
            {
                if (classname == null)
                {
                    throw new NullReferenceException(nameof(classname));
                }

                if (classname.StartsWith("["))
                {
                    classname  = classname.TrimStart('[');
                    initialize = false;
                }

                if (classname.StartsWith("L") && classname.EndsWith(";"))
                {
                    classname = classname.Substring(1, classname.Length - 2);
                }

                Type t = null;

                foreach (var type in typeof(Class).Assembly.GetTypes())
                {
                    if (type.FullName == classname)
                    {
                        t = type;
                    }
                    if (type.GetCustomAttribute <JavaNameAttribute>()?.Name.Replace('/', '.') == classname)
                    {
                        t = type;
                    }
                }

                if (t == null)
                {
                    throw new ClassNotFoundException(classname);
                }

                if (initialize)
                {
                    RuntimeHelpers.RunClassConstructor(t.TypeHandle);
                }

                found = (Class)ReflectionBridge.GetClass(t);
            }

            return(found);
        }
Ejemplo n.º 9
0
 internal static LessThanOrEqual LessThanOrEqual(BinaryExpression expression)
 {
     return(new LessThanOrEqual()
     {
         Left = Serialize(expression.Left),
         Right = Serialize(expression.Right),
         LiftToNull = expression.IsLiftedToNull,
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 10
0
 internal static OrAssign OrAssign(BinaryExpression expression)
 {
     return(new OrAssign()
     {
         Left = Serialize(expression.Left),
         Right = Serialize(expression.Right),
         Method = expression.Method.Null(m => MethodRef.Serialize(m)),
         Conversion = expression.Conversion.Null(e => Lambda(e)),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 11
0
 internal static Call Call(MethodCallExpression expression)
 {
     return(new Call()
     {
         Method = MethodRef.Serialize(expression.Method),
         Object = expression.Object.Null(e => Serialize(e)),
         Arguments = expression.Arguments.Select(Serialize).ToArray(),
         IsExtension = expression.Method.IsExtensionMethod(),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 12
0
        public override void Build()
        {
            ns("MyNameSpace");

            declClass(TypePublic, "MyClasss");
            declMethod(MemPublic, "System.Void", "MyMethod");

            MethodRef testMethod = new MethodRef(my(), "TestMethod");

            enter(ex(testMethod));
        }
Ejemplo n.º 13
0
        private void TranslateType(InterType type, ExpectType expected, object tag)
        {
            if ((Program.Unsigned) && (type.IsPrimitive) && (type.PrimitiveType.IsUnsigned()))
            {
                if ((type.PrimitiveType == PrimitiveType.Byte) || (type.PrimitiveType == PrimitiveType.UInt16))
                {
                    if (type.PrimitiveType == PrimitiveType.Byte)
                    {
                        codeGenerator.AddIntConst(0xff, tag);
                    }
                    else
                    {
                        codeGenerator.AddIntConst(0xffff, tag);
                    }
                    codeGenerator.Add(Java.OpCodes.iand, null, tag);
                }
            }

            if (expected == ExpectType.Any)
            {
                return;
            }

            if (expected == ExpectType.None)
            {
                codeGenerator.AddPop(JavaHelpers.InterTypeToJavaPrimitive(type), tag);
            }

            if ((type.IsByRef) && (expected != ExpectType.ByRef) && (!type.ElementType.IsValueType))
            {
                JavaPrimitiveType    javaType      = JavaHelpers.InterTypeToJavaPrimitive(type.ElementType);
                MethodRef            getMethodRef  = byRefController.GetByRefGetValueMethodRef(javaType);
                Java.Constants.Class loadedTypeRef = new Java.Constants.Class(namesController.TypeNameToJava(type.ElementType));

                codeGenerator.Add(OpCodes.invokevirtual, getMethodRef, tag);

                if (javaType == JavaPrimitiveType.Ref)
                {
                    codeGenerator.Add(OpCodes.checkcast, loadedTypeRef, tag);
                }

                type = type.ElementType;
            }

            if (((expected == ExpectType.Boxed) || (expected == ExpectType.Reference)) &&
                ((type.IsPrimitive) || (type.IsEnum) || (type.IsValueType)))
            {
                if ((!type.IsNullable) || (expected != ExpectType.Reference))
                {
                    BoxType(type, tag);
                }
            }
        }
Ejemplo n.º 14
0
        private void CompileLdelem(ILExpression e, ExpectType expect)
        {
            TypeReference typeRef = e.Operand as TypeReference ?? e.InferredType;
            InterType     operand = null;

            if (typeRef != null)
            {
                operand = resolver.Resolve(typeRef, thisMethod.FullGenericArguments);
            }
            else
            {
                switch (e.Code)
                {
                case ILCode.Ldelem_I: operand = InterType.PrimitiveTypes[(int)(Program.AsX64 ? PrimitiveType.Int64 : PrimitiveType.Int32)]; break;

                case ILCode.Ldelem_I1: operand = InterType.PrimitiveTypes[(int)PrimitiveType.SByte]; break;

                case ILCode.Ldelem_I2: operand = InterType.PrimitiveTypes[(int)PrimitiveType.Int16]; break;

                case ILCode.Ldelem_I4: operand = InterType.PrimitiveTypes[(int)PrimitiveType.Int32]; break;

                case ILCode.Ldelem_I8: operand = InterType.PrimitiveTypes[(int)PrimitiveType.Int64]; break;

                case ILCode.Ldelem_R4: operand = InterType.PrimitiveTypes[(int)PrimitiveType.Single]; break;

                case ILCode.Ldelem_R8: operand = InterType.PrimitiveTypes[(int)PrimitiveType.Double]; break;

                case ILCode.Ldelem_U1: operand = InterType.PrimitiveTypes[(int)PrimitiveType.Byte]; break;

                case ILCode.Ldelem_U2: operand = InterType.PrimitiveTypes[(int)PrimitiveType.UInt16]; break;

                case ILCode.Ldelem_U4: operand = InterType.PrimitiveTypes[(int)PrimitiveType.UInt32]; break;
                }
            }
            JavaArrayType arrType = operand != null?JavaHelpers.InterTypeToJavaArrayType(operand) : JavaArrayType.Ref;

            CompileExpression(e.Arguments[0], ExpectType.Reference);    //array
            CompileExpression(e.Arguments[1], ExpectType.Primitive);    //index

            codeGenerator.AddArrayLoad(arrType, e);

            if ((operand != null) && (operand.IsValueType))
            {
                MethodRef getCopyRef = new MethodRef(namesController.TypeNameToJava(operand),
                                                     ClassNames.ValueTypeGetCopy, "()" + namesController.GetFieldDescriptor(operand));
                codeGenerator.Add(Java.OpCodes.invokevirtual, getCopyRef, e);
            }

            if (operand != null)
            {
                TranslateType(operand, expect, e);
            }
        }
Ejemplo n.º 15
0
        private void CompileUnbox_Any(ILExpression e, ExpectType expect)
        {
            InterType operand      = resolver.Resolve((TypeReference)e.Operand, thisMethod.FullGenericArguments);
            InterType nullableType = null;

            if (operand.IsNullable)
            {
                nullableType = operand;
                operand      = operand.GenericArguments[0].Type;

                codeGenerator
                .Add(OpCodes._new, new Java.Constants.Class(namesController.TypeNameToJava(nullableType)), e)
                .Add(OpCodes.dup, null, e);
            }

            CompileExpression(e.Arguments[0], ExpectType.Boxed);

            string boxType = GetBoxType(operand);

            Java.Constants.Class operandRef = new Java.Constants.Class(namesController.TypeNameToJava(boxType));

            Java.Constants.MethodRef valueRef;

            if (operand.IsValueType)
            {
                valueRef = new Java.Constants.MethodRef(operandRef.Value, ClassNames.ValueTypeGetCopy, "()" +
                                                        namesController.GetFieldDescriptor(operand));
            }
            else if (operand.IsEnum)
            {
                valueRef = new MethodRef(operandRef.Value, ClassNames.EnumGetUnboxedMethodName,
                                         "()" + namesController.GetFieldDescriptor(operand.ElementType));
            }
            else
            {
                valueRef = new Java.Constants.MethodRef(operandRef.Value,
                                                        Utils.GetJavaTypeName(operand.PrimitiveType) + "Value",
                                                        "()" + namesController.GetFieldDescriptor(operand));
            }

            codeGenerator
            .Add(Java.OpCodes.checkcast, operandRef, e)
            .Add(Java.OpCodes.invokevirtual, valueRef, e);

            if (nullableType != null)
            {
                codeGenerator.Add(OpCodes.invokespecial, new MethodRef(
                                      namesController.TypeNameToJava(nullableType),
                                      ClassNames.JavaConstructorMethodName,
                                      "(" + namesController.GetFieldDescriptor(operand) + ")V"));
            }
        }
Ejemplo n.º 16
0
        public override void Build()
        {
            ns("MyNameSpace");

            declClass(TypePublic, "MyClasss");
            declMethod(MemPublic, "System.Void", "MyMethod");

            MethodRef ConsoleWrite = new MethodRef("Console", "Write");

            //ex = CodeMethodInvokeExpression

            enter(ex(ConsoleWrite, new Literal("Example String")));
        }
Ejemplo n.º 17
0
        public override void Build()
        {
            ns("MyNameSpace");
            uses("System");

            MethodRef ConsoleWrite = new MethodRef("System.Console", "WriteLine");
            Literal   HelloWorld   = "Hello World from Mr. ZADCO";


            declClass(TypePublic, "Class1");
            declEntry();
            enter(ex(ConsoleWrite, HelloWorld));
        }
Ejemplo n.º 18
0
 internal static New New(NewExpression expression)
 {
     return(new New()
     {
         Constructor = MethodRef.Serialize(expression.Constructor),
         Arguments = expression.Arguments.Select(Serialize).ToArray(),
         Members = expression.Members.If(
             ms => ms != null && ms.Any(),
             ms => ms.Select(MemberRef.Serialize).ToArray(),
             _ => null
             ),
     }.Apply(n => n.Type = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 19
0
        public override void Build()
        {
            ns("MyNameSpace");

            declClass(TypePublic, "MyClasss", "ParentClass");
            declMethod(MemPublic, "System.Void", "MyMethod");

            MethodRef selfDispose = new MethodRef(mySuper(), "Dispose");

            enter(ex(selfDispose));

            enter(ex(new MethodRef(mySuper(), "SomeMethod")));
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Determine the type of the parameter for the specified argument index.
 /// </summary>
 public override TypeRefBase GetParameterType(int argumentIndex)
 {
     if (_indexerRef is IndexerRef)
     {
         TypeRefBase parameterTypeRef = MethodRef.GetParameterType(_indexerRef.Reference, argumentIndex, _expression);
         if (parameterTypeRef != null)
         {
             return(parameterTypeRef);
         }
     }
     // By default, assume we're indexing an array type
     return(TypeRef.IntRef);
 }
        public override void Build()
        {
            ns("MyNameSpace");

            declClass(TypePublic, "MyClasss");
            declMethod(MemPublic, "System.Void", "MyMethod");

            MethodRef selfDispose = new MethodRef(my(), "Dispose");

            enter(ex(selfDispose, new Literal(true)));

            enter(ex(new MethodRef(my(), "SomeMethod"), new Literal(false)));
        }
Ejemplo n.º 22
0
 public void Add <T>(string name, T value)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentNullException(name);
     }
     if (MethodRef <T> .Method == null)
     {
         throw new Exception("Unsupported property type: " + typeof(T));
     }
     globalPropertyNames.Add(name);
     MethodRef <T> .Method(CmdBuf, name, value);
 }
Ejemplo n.º 23
0
 internal static Switch Switch(SwitchExpression expression)
 {
     return(new Switch()
     {
         Type = expression.Type != expression.Cases[0].Body.Type
             ? TypeRef.Serialize(expression.Type)
             : null,
         SwitchValue = Serialize(expression.SwitchValue),
         Cases = expression.Cases.Select(SwitchCase.Serialize).ToArray(),
         DefaultBody = expression.DefaultBody.Null(e => Serialize(e)),
         Comparison = expression.Comparison.Null(m => MethodRef.Serialize(m)),
     }.If(n => n.Type == null, n => n.TypeHint = TypeRef.Serialize(expression.Type)));
 }
Ejemplo n.º 24
0
        //internal static ClassRef GetClass(string strClassName)
        //{
        //    if (string.IsNullOrEmpty(strClassName))
        //        strClassName = "Myexce";
        //    string str1 = Process.GetCurrentProcess().MainModule.FileName;
        //    //Console.WriteLine(str1);
        //    PEFile file = PEFile.ReadPEFile(str1);
        //    string Assemblyname = System.IO.Path.GetFileNameWithoutExtension(str1); ;
        //    AssemblyRef newAssemblyRef = file.MakeExternAssembly(Assemblyname);
        //    ClassRef newMethodLoggerRef = TryGetMethodLoggerFromAssembly(newAssemblyRef, "MethodExecLogger");
        //    return newMethodLoggerRef;
        //}

        internal static bool GEtMethodsFromClass(ClassRef methodClass, string strMethodName, out Method TagetMethod)
        {
            TagetMethod = null;
            MethodRef tmpTagetMethod = methodClass.GetMethod(strMethodName);

            if (tmpTagetMethod != null)
            {
                TagetMethod = tmpTagetMethod;
                return(true);
            }


            return(false);
        }
Ejemplo n.º 25
0
        private void CompileDelegateCtor(InterType type)
        {
            Method result = new Method();

            result.AccessFlags = MethodAccessFlags.Public;
            result.Name        = ClassNames.JavaConstructorMethodName;

            string methodPointerType;

            if (Program.MethodPointersType == MethodPointerImplementation.Fast)
            {
                methodPointerType = ((INamesController)this).GetMethodPointerInterface(
                    type.Methods.Where(M => M.Name == ClassNames.DelegateInvokeMethodName).FirstOrDefault());

                methodPointerType = "L" + TypeNameToJava(methodPointerType) + ";";
            }
            else
            {
                methodPointerType = Program.AsX64 ? "J" : "I";
            }

            result.Descriptor = "(L" + TypeNameToJava(ClassNames.JavaObject) + ";" + methodPointerType + ")V";

            MethodRef baseInitRef = new MethodRef(currentJavaClass.SuperClass, ClassNames.JavaConstructorMethodName,
                                                  "(L" + TypeNameToJava(ClassNames.JavaObject) + ";L" + TypeNameToJava(ClassNames.JavaObject) + ";)V");

            JavaBytecodeWriter codeWriter = new JavaBytecodeWriter();

            codeWriter
            .Add(OpCodes.aload_0)
            .Add(OpCodes.aload_1);

            if (Program.MethodPointersType == MethodPointerImplementation.Fast)
            {
                codeWriter.Add(OpCodes.aload_2);
            }
            else
            {
                codeWriter
                .Add(Program.AsX64 ? OpCodes.lload_2 : OpCodes.iload_2)
                .Add(OpCodes.invokestatic, Program.AsX64 ? ClassNames.JavaLongBox : ClassNames.JavaIntegerBox);
            }

            result.Attributes.Add(codeWriter
                                  .Add(OpCodes.invokespecial, baseInitRef)
                                  .Add(OpCodes._return)
                                  .End(currentJavaClass.ConstantPool));

            currentJavaClass.Methods.Add(result);
        }
        public override void Build()
        {
            ns("MyNameSpace");
            uses("System");

            FieldRef TestParam = new FieldRef(my(), "TestParameter");

            MethodRef ThisMethod = new MethodRef(my(), "ThisMethod");

            declClass(TypePublic, "Class1");
            declMethod(MemPublic, "System.Void", "ClassMethod");

            enter(ex(ThisMethod, refp(TestParam)));
        }
Ejemplo n.º 27
0
        private MethodRef Scramble(MethodRef methodRef)
        {
            if (methodRef.IsEmpty)
            {
                return(default(MethodRef));
            }

            return(new MethodRef(
                       this.Scramble(methodRef.DeclaringType),
                       this.GetScrambledMetadataToken(methodRef.MetadataToken),
                       methodRef.Name,
                       methodRef.ParameterTypes.Select(this.Scramble).ToImmutableArray(),
                       methodRef.GenericMethodArguments.Select(this.Scramble).ToImmutableArray()));
        }
 protected override ImageReference GetIcon(IDotNetImageService dnImgMgr)
 {
     if (imageReference is null)
     {
         if (MethodRef.ResolveMethodDef() is MethodDef md)
         {
             imageReference = dnImgMgr.GetImageReference(md);
         }
         else
         {
             imageReference = DsImages.MethodPublic;
         }
     }
     return(imageReference.Value);
 }
Ejemplo n.º 29
0
        public override void Build()
        {
            ns("MyNameSpace");

            declClass(TypePublic, "Type1");
            declMethod(MemPublic, "System.Void", "MonMethod");

            jumpTo("JumpToLabel");

            MethodRef w = new MethodRef("System.Console", "WriteLine");

            enter(ex(w, new Literal("Test Output")));

            declLabel("JumpToLabel", ex(w, new Literal("Output from Labeled statement")));
        }
Ejemplo n.º 30
0
 public AssemblyDef(Global global, IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, AssemblyName name, IImSeq<AssemblyName> references, IImSeq<TypeDef> types, MethodRef entryPoint)
 {
     Annotations = annotations ?? Constants.EmptyAnnotations;
     CustomAttributes = customAttributes ?? new Seq<CustomAttribute>();
     Name = name;
     References = references ?? Constants.EmptyStrongAssemblyNames;
     Types = types ?? Constants.EmptyTypeDefs;
     EntryPoint = entryPoint;
     nameToTypeDefCache = new Map<TypeName, TypeDef>();
     if (types != null)
     {
         foreach (var t in types)
             nameToTypeDefCache.Add(t.EffectiveName(global), t);
     }
 }
Ejemplo n.º 31
0
        private void CompileArrayGet(ILExpression e, ExpectType expect)
        {
            InterMethod operand = resolver.Resolve((MethodReference)e.Operand, thisMethod.FullGenericArguments);

            JavaArrayType arrType = JavaHelpers.InterTypeToJavaArrayType(operand.DeclaringType.ElementType);

            codeGenerator.AddArrayLoad(arrType, e);

            if (operand.DeclaringType.ElementType.IsValueType)
            {
                MethodRef getCopyRef = new MethodRef(namesController.TypeNameToJava(operand.DeclaringType.ElementType),
                                                     ClassNames.ValueTypeGetCopy, "()" + namesController.GetFieldDescriptor(operand.DeclaringType.ElementType));
                codeGenerator.Add(Java.OpCodes.invokevirtual, getCopyRef, e);
            }
        }
Ejemplo n.º 32
0
 public DuplicateEntryPointMessage(MethodRef existingEntryPoint, MethodRef newEntryPoint) :
     base(null, Severity.Warning, "1029")
 {
     ExistingEntryPoint = existingEntryPoint;
     NewEntryPoint = newEntryPoint;
 }
Ejemplo n.º 33
0
 public IgnoringEntryPointMessage(MethodRef method, string fileName)
     : base(null, Severity.Warning, "1018")
 {
     Method = method;
     FileName = fileName;
 }
Ejemplo n.º 34
0
        private MethodRef MakeMethodRef(Cci.IMethodReference methodRef, Cci.ISpecializedMethodReference specializedRef, bool isConstructor)
        {
            Type declaringType = ResolveType(methodRef.GetContainingType(_context));

            MethodBase unspecializedDefinition = null;
            if (specializedRef != null)
            {
                // resolvemethod
                unspecializedDefinition = isConstructor ? ResolveConstructor(specializedRef.UnspecializedVersion) : ResolveMethod(specializedRef.UnspecializedVersion);
            }

            var m = new MethodRef(declaringType, methodRef.Name, GetManagedCallingConvention(methodRef.CallingConvention), unspecializedDefinition, GetExtraParameterTypes(methodRef));

            Type[] genericParameters;
            ParameterInfo[] parameters;
            ParameterInfo returnParameter;

            if (unspecializedDefinition != null)
            {
                if (isConstructor)
                {
                    returnParameter = new SimpleParameterInfo(unspecializedDefinition, 0, typeof(void));
                }
                else
                {
                    returnParameter = ((MethodInfo)unspecializedDefinition).ReturnParameter;
                }

                // TODO(tomat): change containing method?
                genericParameters = unspecializedDefinition.GetGenericArguments();
                parameters = unspecializedDefinition.GetParameters();
            }
            else
            {
                MakeMethodParameters(m, methodRef, out genericParameters, out parameters, out returnParameter);
            }

            m.InitializeParameters(genericParameters, parameters, returnParameter);

            return m;
        }
Ejemplo n.º 35
0
 internal static void ReadMember(PEReader buff, TableRow[] members)
 {
     Contract.Requires(buff != null);
     Contract.Requires(members != null);
     for (int i = 0; i < members.Length; i++)
     {
         uint parenIx = buff.GetCodedIndex(CIx.MemberRefParent);
         string memName = buff.GetString();
         uint sigIx = buff.GetBlobIx();
         if (buff.FirstBlobByte(sigIx) == Field.FieldTag) // got a field
             members[i] = new FieldRef(parenIx, memName, sigIx);
         else
             members[i] = new MethodRef(parenIx, memName, sigIx);
     }
 }