Example #1
0
        private void CompilePrimitive(InterType type)
        {
            if (Program.BoxType == BoxingType.Java)
            {
                return;
            }

            if (type.PrimitiveType == PrimitiveType.Void)
            {
                return; //Skip compiling void type. It's never will boxed
            }
            JavaPrimitiveType javaType = JavaHelpers.InterTypeToJavaPrimitive(type);

            currentJavaClass            = new Java.Class();
            currentJavaClass.AccessFlag = Java.ClassAccessFlag.Public | Java.ClassAccessFlag.Super | ClassAccessFlag.Final;
            currentJavaClass.ThisClass  = TypeNameToJava(type.CILBoxType);
            currentJavaClass.SuperClass = TypeNameToJava(ClassNames.JavaObject);

            Field valueField = new Field();

            valueField.AccessFlags = FieldAccessFlags.Private;
            valueField.Name        = "value";
            valueField.Descriptor  = GetFieldDescriptor(type);
            currentJavaClass.Fields.Add(valueField);
            FieldRef valueFieldRef = new FieldRef(currentJavaClass.ThisClass, valueField.Name, valueField.Descriptor);

            Method ctorMethod = new Method();

            ctorMethod.AccessFlags = MethodAccessFlags.Public;
            ctorMethod.Name        = ClassNames.JavaConstructorMethodName;
            ctorMethod.Descriptor  = "()V";
            ctorMethod.Attributes.Add(new JavaBytecodeWriter()
                                      .Add(OpCodes.aload_0)
                                      .Add(OpCodes.invokespecial, ClassNames.JavaObjectCtorMethodRef)

                                      .Add(OpCodes.aload_0)
                                      .AddDefaultValue(javaType)
                                      .Add(OpCodes.putfield, valueFieldRef)

                                      .Add(OpCodes._return)

                                      .End(currentJavaClass.ConstantPool));
            currentJavaClass.Methods.Add(ctorMethod);

            Method ctorValueMethod = new Method();

            ctorValueMethod.AccessFlags = MethodAccessFlags.Public;
            ctorValueMethod.Name        = ClassNames.JavaConstructorMethodName;
            ctorValueMethod.Descriptor  = "(" + valueField.Descriptor + ")V";
            ctorValueMethod.Attributes.Add(new JavaBytecodeWriter()
                                           .Add(OpCodes.aload_0)
                                           .Add(OpCodes.invokespecial, ClassNames.JavaObjectCtorMethodRef)

                                           .Add(OpCodes.aload_0)
                                           .AddLoad(javaType, 1)
                                           .Add(OpCodes.putfield, valueFieldRef)

                                           .Add(OpCodes._return)

                                           .End(currentJavaClass.ConstantPool));
            currentJavaClass.Methods.Add(ctorValueMethod);
            MethodRef ctorValueMethodRef = new MethodRef(currentJavaClass.ThisClass, ctorValueMethod.Name, ctorValueMethod.Descriptor);

            Method valueOfMethod = new Method();

            valueOfMethod.AccessFlags = MethodAccessFlags.Public | MethodAccessFlags.Static;
            valueOfMethod.Name        = "valueOf";
            valueOfMethod.Descriptor  = "(" + valueField.Descriptor + ")L" + TypeNameToJava(type.CILBoxType) + ";";
            valueOfMethod.Attributes.Add(new JavaBytecodeWriter()
                                         .Add(OpCodes._new, new Java.Constants.Class(TypeNameToJava(type.CILBoxType)))
                                         .Add(OpCodes.dup)
                                         .AddLoad(javaType, 0)
                                         .Add(OpCodes.invokespecial, ctorValueMethodRef)
                                         .Add(OpCodes.areturn)

                                         .End(currentJavaClass.ConstantPool));
            currentJavaClass.Methods.Add(valueOfMethod);

            Method getValueMethod = new Method();

            getValueMethod.AccessFlags = MethodAccessFlags.Public;
            getValueMethod.Name        = Utils.GetJavaTypeName(type.PrimitiveType) + "Value";
            getValueMethod.Descriptor  = "()" + GetFieldDescriptor(type);
            getValueMethod.Attributes.Add(new JavaBytecodeWriter()
                                          .Add(OpCodes.aload_0)
                                          .Add(OpCodes.getfield, valueFieldRef)
                                          .AddReturn(JavaHelpers.InterTypeToJavaPrimitive(type))

                                          .End(currentJavaClass.ConstantPool));
            currentJavaClass.Methods.Add(getValueMethod);

            currentJavaInnerClasses = new Java.Attributes.InnerClasses();

            foreach (InterField field in type.Fields)
            {
                currentJavaClass.Fields.Add(CompileField(field));
            }

            foreach (InterMethod method in type.Methods)
            {
                currentJavaClass.Methods.Add(CompileMethod(method));
            }

            if (currentJavaInnerClasses.Classes.Count > 0)
            {
                currentJavaClass.Attributes.Add(currentJavaInnerClasses);
            }

            WriteClass(currentJavaClass);
        }
Example #2
0
        private void GenerateDelegateRunner(InterType type)
        {
            Java.Class runner = new Java.Class();
            runner.AccessFlag = ClassAccessFlag.Final;
            runner.ThisClass  = currentJavaClass.ThisClass + "$" + ClassNames.DelegateRunnerClassName;
            runner.SuperClass = TypeNameToJava(ClassNames.CIL2JavaDelegateRunner.ClassName);

            Java.Attributes.InnerClasses.InnerClass inner = new Java.Attributes.InnerClasses.InnerClass()
            {
                AccessFlags    = Java.Attributes.InnerClasses.InnerClassAccessFlags.Final | Java.Attributes.InnerClasses.InnerClassAccessFlags.Private,
                InnerClassInfo = runner.ThisClass,
                InnerName      = ClassNames.DelegateRunnerClassName,
                OuterClassInfo = currentJavaClass.ThisClass
            };

            Java.Attributes.InnerClasses innerAttr = new Java.Attributes.InnerClasses();
            innerAttr.Classes.Add(inner);
            runner.Attributes.Add(innerAttr);
            currentJavaInnerClasses.Classes.Add(inner);

            InterMethod invokeMethod = type.Methods.Where(M => M.Name == ClassNames.DelegateInvokeMethodName).FirstOrDefault();

            Field selfField = new Field();

            selfField.AccessFlags = FieldAccessFlags.Private | FieldAccessFlags.Final;
            selfField.Name        = ClassNames.DelegateRunnerSelfFieldName;
            selfField.Descriptor  = GetFieldDescriptor(type);
            runner.Fields.Add(selfField);
            FieldRef selfFieldRef = new FieldRef(runner.ThisClass, selfField.Name, selfField.Descriptor);

            JavaBytecodeWriter ctorCodeWriter = new JavaBytecodeWriter();
            JavaBytecodeWriter runCodeWriter  = new JavaBytecodeWriter();

            ctorCodeWriter
            //super()
            .Add(OpCodes.aload_0)
            .Add(OpCodes.invokespecial, ClassNames.CIL2JavaDelegateRunner.CtorMethodRef)

            //this.self = self;
            .Add(OpCodes.aload_0)
            .Add(OpCodes.aload_1)
            .Add(OpCodes.putfield, selfFieldRef);

            if (invokeMethod.ReturnParameter.Type.PrimitiveType != PrimitiveType.Void)
            {
                runCodeWriter.Add(OpCodes.aload_0);
            }

            runCodeWriter
            .Add(OpCodes.aload_0)
            .Add(OpCodes.getfield, selfFieldRef);

            for (int i = 0; i < invokeMethod.Parameters.Count; i++)
            {
                InterType         paramType = invokeMethod.Parameters[i].Type;
                JavaPrimitiveType jp        = JavaHelpers.InterTypeToJavaPrimitive(paramType);

                Field paramField = new Field();
                paramField.AccessFlags = FieldAccessFlags.Final | FieldAccessFlags.Private;
                paramField.Name        = ClassNames.DelegateRunnerParamFieldNamePrefix + i.ToString();;
                paramField.Descriptor  = GetFieldDescriptor(paramType);
                runner.Fields.Add(paramField);
                FieldRef paramFieldRef = new FieldRef(runner.ThisClass, paramField.Name, paramField.Descriptor);

                ctorCodeWriter
                .Add(OpCodes.aload_0)
                .AddLoad(jp, i + 2)
                .Add(OpCodes.putfield, paramFieldRef);

                runCodeWriter
                .Add(OpCodes.aload_0)
                .Add(OpCodes.getfield, paramFieldRef);
            }

            if (invokeMethod.ReturnParameter.Type.PrimitiveType != PrimitiveType.Void)
            {
                Field resultField = new Field();
                resultField.AccessFlags = FieldAccessFlags.Private;
                resultField.Name        = ClassNames.DelegateRunnerResultFieldName;
                resultField.Descriptor  = GetFieldDescriptor(invokeMethod.ReturnParameter.Type);
                runner.Fields.Add(resultField);
                runCodeWriter.Add(OpCodes.putfield, new FieldRef(runner.ThisClass, resultField.Name, resultField.Descriptor));
            }

            runCodeWriter
            .Add(OpCodes.invokevirtual, new MethodRef(TypeNameToJava(type.Fullname), ClassNames.DelegateInvokeMethodName,
                                                      GetMethodDescriptor(invokeMethod)))
            .Add(OpCodes.aload_0)
            .Add(OpCodes.getfield, ClassNames.CIL2JavaDelegateRunner.OnEndedFieldRef)
            .Add(OpCodes.dup)
            .Add(OpCodes.ifnull, "noOnEnd")
            .Add(OpCodes.aload_0)
            .Add(OpCodes.getfield, ClassNames.CIL2JavaDelegateRunner.AsyncResultFieldResult)
            .Add(OpCodes.invokevirtual, ClassNames.SystemAsyncCallback.InvokeMethodRef)
            .Add(OpCodes._goto, "exit")
            .Label("noOnEnd")
            .Add(OpCodes.pop)
            .Label("exit");

            ctorCodeWriter.Add(OpCodes._return);
            runCodeWriter.Add(OpCodes._return);

            string paramsDescriptor = GetMethodDescriptor(invokeMethod);

            paramsDescriptor = paramsDescriptor.Substring(1, paramsDescriptor.LastIndexOf(')') - 1);

            Method ctorMethod = new Method();

            ctorMethod.AccessFlags = MethodAccessFlags.Public;
            ctorMethod.Name        = ClassNames.JavaConstructorMethodName;
            ctorMethod.Descriptor  = "(" + GetFieldDescriptor(type) + paramsDescriptor + ")V";
            ctorMethod.Attributes.Add(ctorCodeWriter.End(runner.ConstantPool));
            runner.Methods.Add(ctorMethod);

            Method runMethod = new Method();

            runMethod.AccessFlags = MethodAccessFlags.Public;
            runMethod.Name        = ClassNames.CIL2JavaDelegateRunner.RunMethodName;
            runMethod.Descriptor  = "()V";
            runMethod.Attributes.Add(runCodeWriter.End(runner.ConstantPool));
            runner.Methods.Add(runMethod);

            WriteClass(runner);
        }
Example #3
0
        private Java.Class ComplileType(InterType type)
        {
            sourceFileNameCounter = new Counter <string>();

            Messages.Verbose("  Compiling type {0}...", type.ToString());
            currentType      = type;
            currentJavaClass = new Java.Class();

            currentJavaClass.AccessFlag = (Java.ClassAccessFlag)GetClassAccessFlags(type, false);

            currentJavaClass.ThisClass = TypeNameToJava(type.Fullname);
            if (type.BaseType == null)
            {
                currentJavaClass.SuperClass = TypeNameToJava(ClassNames.JavaObject);
            }
            else
            {
                currentJavaClass.SuperClass = TypeNameToJava(type.BaseType.Fullname);
            }

            foreach (InterType i in type.Interfaces)
            {
                currentJavaClass.Interfaces.Add(TypeNameToJava(i.Fullname));
            }

            if (type.IsValueType)
            {
                CompileValueType(type);
            }
            if (type.IsEnum)
            {
                CompileEnum(type);
            }

            currentJavaInnerClasses = new Java.Attributes.InnerClasses();

            if (type.DeclaringType != null)
            {
                Java.Attributes.InnerClasses.InnerClass innerClass = new Java.Attributes.InnerClasses.InnerClass();
                innerClass.AccessFlags    = (Java.Attributes.InnerClasses.InnerClassAccessFlags)GetClassAccessFlags(type, true);
                innerClass.InnerClassInfo = TypeNameToJava(type.Fullname);
                innerClass.InnerName      = TypeNameToJava(type.Name);
                innerClass.OuterClassInfo = TypeNameToJava(type.DeclaringType.Fullname);
                currentJavaInnerClasses.Classes.Add(innerClass);
            }

            foreach (InterType nestedClass in type.NestedClasses)
            {
                Java.Attributes.InnerClasses.InnerClass innerClass = new Java.Attributes.InnerClasses.InnerClass();
                innerClass.AccessFlags    = (Java.Attributes.InnerClasses.InnerClassAccessFlags)GetClassAccessFlags(nestedClass, true);
                innerClass.InnerClassInfo = TypeNameToJava(nestedClass.Fullname);
                innerClass.InnerName      = TypeNameToJava(nestedClass.Name);
                innerClass.OuterClassInfo = TypeNameToJava(type.Fullname);
                currentJavaInnerClasses.Classes.Add(innerClass);
            }

            if (type.IsDelegate)
            {
                CompileDelegate(type);
            }
            else
            {
                foreach (InterField field in type.Fields)
                {
                    currentJavaClass.Fields.Add(CompileField(field));
                }

                foreach (InterMethod method in type.Methods)
                {
                    currentJavaClass.Methods.Add(CompileMethod(method));
                }

                if (type.BaseType != null)
                {
                    CompileOverridedMethods(type, type.BaseType);
                }
                foreach (InterType iface in type.Interfaces)
                {
                    CompileOverridedMethods(type, iface);
                }

                GenerateFieldAccessors(type);
            }

            bool hasStaticCtor  = type.Methods.Where(M => ((M.IsConstructor) && (M.IsStatic))).Count() > 0;
            bool needStaticCtor = type.Fields.Where(F => ((F.FieldType.IsValueType) && (F.IsStatic))).Count() > 0;

            needStaticCtor |= type.Fields.Where(F => F.IsStatic && F.IsThreadLocal).Count() > 0;

            if ((needStaticCtor) && (!hasStaticCtor))
            {
                GenerateStaticCtor(type);
            }

            if (currentJavaInnerClasses.Classes.Count > 0)
            {
                currentJavaClass.Attributes.Add(currentJavaInnerClasses);
            }

            if ((Program.Debug) && (sourceFileNameCounter != null) && (sourceFileNameCounter.IsStarted))
            {
                string fileName = sourceFileNameCounter.MostUsed;
                if (!Program.FullPathInSourceFileName)
                {
                    fileName = System.IO.Path.GetFileName(fileName);
                }

                currentJavaClass.Attributes.Add(new Java.Attributes.SourceFile(fileName));
            }
            return(currentJavaClass);
        }
Example #4
0
        public static Attribute ReadAttribute(BinaryReader Reader, ConstantPool Pool)
        {
            string Name   = ((Constants.Utf8)Pool[Reader.ReadUInt16BE()]).Value;
            uint   Length = Reader.ReadUInt32BE();
            long   Next   = (long)Reader.BaseStream.Position - (long)(Length - 6);

            Attribute Result = null;

            if (Name == "AnnotationDefault")
            {
                Result = new Attributes.AnnotationDefault();
            }
            else if (Name == "Code")
            {
                Result = new Attributes.Code();
            }
            else if (Name == "ConstantValue")
            {
                Result = new Attributes.ConstantValue();
            }
            else if (Name == "Deprecated")
            {
                Result = new Attributes.Deprecated();
            }
            else if (Name == "Exceptions")
            {
                Result = new Attributes.Exceptions();
            }
            else if (Name == "InnerClasses")
            {
                Result = new Attributes.InnerClasses();
            }
            else if (Name == "LineNumberTable")
            {
                Result = new Attributes.LineNumberTable();
            }
            else if (Name == "LocalVariableTable")
            {
                Result = new Attributes.LocalVariableTable();
            }
            else if (Name == "RuntimeVisibleAnnotations")
            {
                Result = new Attributes.RuntimeVisibleAnnotations();
            }
            else if (Name == "RuntimeInvisibleAnnotations")
            {
                Result = new Attributes.RuntimeInvisibleAnnotations();
            }
            else if (Name == "RuntimeVisibleParameterAnnotations")
            {
                Result = new Attributes.RuntimeVisibleParameterAnnotations(Reader.ReadByte());
            }
            else if (Name == "RuntimeInvisibleParameterAnnotations")
            {
                Result = new Attributes.RuntimeInvisibleParameterAnnotations(Reader.ReadByte());
            }
            else if (Name == "Signature")
            {
                Result = new Attributes.Signature();
            }
            else if (Name == "SourceFile")
            {
                Result = new Attributes.SourceFile();
            }
            else if (Name == "Synthetic")
            {
                Result = new Attributes.Synthetic();
            }
            else
            {
                Result      = new Attributes.RawAttribute();
                Result.Name = Name;
            }

            Result.Read(Length, Reader, Pool);
            return(Result);
        }