Example #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)));
 }
Example #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)));
 }
Example #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);
        }
Example #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);
        }
Example #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)));
 }
Example #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)));
 }
Example #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)),
     });
 }
Example #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);
        }
Example #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)));
 }
Example #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)));
 }
Example #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)));
 }
Example #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));
        }
        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);
                }
            }
        }
Example #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);
            }
        }
Example #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"));
            }
        }
Example #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")));
        }
Example #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));
        }
Example #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)));
 }
Example #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")));
        }
Example #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)));
        }
Example #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);
 }
Example #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)));
 }
Example #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);
        }
Example #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)));
        }
Example #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);
 }
        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")));
        }
Example #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);
     }
 }
Example #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);
            }
        }
Example #32
0
 public DuplicateEntryPointMessage(MethodRef existingEntryPoint, MethodRef newEntryPoint) :
     base(null, Severity.Warning, "1029")
 {
     ExistingEntryPoint = existingEntryPoint;
     NewEntryPoint = newEntryPoint;
 }
Example #33
0
 public IgnoringEntryPointMessage(MethodRef method, string fileName)
     : base(null, Severity.Warning, "1018")
 {
     Method = method;
     FileName = fileName;
 }
Example #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;
        }
Example #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);
     }
 }