示例#1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private Class<IExecutable> compile(CompilerContext context) throws ClassFormatError
        private Type <IExecutable> compile(CompilerContext context)
        {
            Type <IExecutable> compiledClass = null;

            context.CodeBlock = this;
            string className = InternalClassName;

            //if (log.DebugEnabled)
            {
                string functionName = Utilities.getFunctionNameByAddress(StartAddress);

                if (!string.ReferenceEquals(functionName, null))
                {
                    Console.WriteLine(string.Format("Compiling {0} ({1})", className, functionName));
                }
                else
                {
                    Console.WriteLine(string.Format("Compiling {0}", className));
                }
            }

            prepare(context, context.MethodMaxInstructions);

            currentSequence = null;
            int computeFlag = ClassWriter.COMPUTE_FRAMES;

            if (context.AutomaticMaxLocals || context.AutomaticMaxStack)
            {
                computeFlag |= ClassWriter.COMPUTE_MAXS;
            }
            ClassWriter  cw = new ClassWriter(computeFlag);
            ClassVisitor cv = cw;
            //if (log.DebugEnabled)
            {
                cv = new CheckClassAdapter(cv);
            }
            StringWriter debugOutput = null;

            if (log.TraceEnabled)
            {
                debugOutput = new StringWriter();
                PrintWriter debugPrintWriter = new PrintWriter(debugOutput);
                cv = new TraceClassVisitor(cv, debugPrintWriter);
            }
            cv.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, className, null, objectInternalName, interfacesForExecutable);
            context.startClass(cv);

            addConstructor(cv);
            addNonStaticMethods(context, cv);

            MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, context.StaticExecMethodName, context.StaticExecMethodDesc, null, exceptions);

            mv.visitCode();
            context.MethodVisitor = mv;
            context.startMethod();

            // Jump to the block start if other instructions have been inserted in front
            if (codeInstructions.Count > 0 && codeInstructions.First.Value.Address != StartAddress)
            {
                mv.visitJumpInsn(Opcodes.GOTO, getCodeInstruction(StartAddress).Label);
            }

            compile(context, mv, codeInstructions);
            mv.visitMaxs(context.MaxStack, context.MaxLocals);
            mv.visitEnd();

            foreach (SequenceCodeInstruction sequenceCodeInstruction in sequenceCodeInstructions)
            {
                //if (log.DebugEnabled)
                {
                    Console.WriteLine("Compiling Sequence " + sequenceCodeInstruction.getMethodName(context));
                }
                currentSequence = sequenceCodeInstruction;
                mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, sequenceCodeInstruction.getMethodName(context), "()V", null, exceptions);
                mv.visitCode();
                context.MethodVisitor = mv;
                context.startSequenceMethod();

                compile(context, mv, sequenceCodeInstruction.CodeSequence.Instructions);

                context.endSequenceMethod();
                mv.visitMaxs(context.MaxStack, context.MaxLocals);
                mv.visitEnd();
            }
            currentSequence = null;

            cv.visitEnd();

            if (debugOutput != null)
            {
                log.trace(debugOutput.ToString());
            }

            try
            {
                compiledClass = loadExecutable(context, className, cw.toByteArray());
            }
            catch (System.NullReferenceException e)
            {
                Console.WriteLine("Error while compiling " + className + ": " + e);
            }

            return(compiledClass);
        }
示例#2
0
        public virtual Type specialize(string name, Type c, Dictionary <string, object> variables)
        {
            ClassWriter  cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor cv = cw;

            StringWriter debugOutput = null;

            if (log.TraceEnabled)
            {
                // Dump the class to be specialized (only once)
                if (!tracedClasses.Contains(c))
                {
                    StringWriter classTrace   = new StringWriter();
                    ClassVisitor classTraceCv = new TraceClassVisitor(new PrintWriter(classTrace));
                    try
                    {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                        ClassReader cr = new ClassReader(c.FullName.Replace('.', '/'));
                        cr.accept(classTraceCv, 0);
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                        log.trace(string.Format("Dump of class to be specialized: {0}", c.FullName));
                        log.trace(classTrace);
                    }
                    catch (IOException)
                    {
                        // Ignore Exception
                    }
                    tracedClasses.Add(c);
                }

                log.trace(string.Format("Specializing class {0}", name));
                string[] variableNames = variables.Keys.toArray(new string[variables.Count]);
                Array.Sort(variableNames);
                foreach (string variableName in variableNames)
                {
                    log.trace(string.Format("Variable {0}={1}", variableName, variables[variableName]));
                }

                debugOutput = new StringWriter();
                PrintWriter debugPrintWriter = new PrintWriter(debugOutput);
                cv = new TraceClassVisitor(cv, debugPrintWriter);
                //cv = new TraceClassVisitor(debugPrintWriter);
            }

            try
            {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                ClassReader cr = new ClassReader(c.FullName.Replace('.', '/'));
                ClassNode   cn = new SpecializedClassVisitor(name, variables);
                cr.accept(cn, 0);
                cn.accept(cv);
            }
            catch (IOException e)
            {
                Console.WriteLine("Cannot read class", e);
            }

            if (debugOutput != null)
            {
                log.trace(debugOutput.ToString());
            }

            Type specializedClass = null;

            try
            {
                specializedClass = classLoader.defineClass(name, cw.toByteArray());
            }
            catch (ClassFormatError e)
            {
                Console.WriteLine("Error while defining specialized class", e);
            }

            return(specializedClass);
        }
示例#3
0
        private Type <IExecutable> interpret(CompilerContext context)
        {
            Type <IExecutable> compiledClass = null;

            context.CodeBlock = this;
            string className = InternalClassName;

            if (log.InfoEnabled)
            {
                Console.WriteLine("Compiling for Interpreter " + className);
            }

            int computeFlag = ClassWriter.COMPUTE_FRAMES;

            if (context.AutomaticMaxLocals || context.AutomaticMaxStack)
            {
                computeFlag |= ClassWriter.COMPUTE_MAXS;
            }
            ClassWriter  cw = new ClassWriter(computeFlag);
            ClassVisitor cv = cw;
            //if (log.DebugEnabled)
            {
                cv = new CheckClassAdapter(cv);
            }

            StringWriter debugOutput = null;

            //if (log.DebugEnabled)
            {
                debugOutput = new StringWriter();
                PrintWriter debugPrintWriter = new PrintWriter(debugOutput);
                cv = new TraceClassVisitor(cv, debugPrintWriter);
            }
            cv.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, className, null, objectInternalName, interfacesForExecutable);
            context.startClass(cv);

            addConstructor(cv);
            addNonStaticMethods(context, cv);

            MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, context.StaticExecMethodName, context.StaticExecMethodDesc, null, exceptions);

            mv.visitCode();
            context.MethodVisitor = mv;
            context.startMethod();

            context.compileExecuteInterpreter(StartAddress);

            mv.visitMaxs(context.MaxStack, context.MaxLocals);
            mv.visitEnd();

            cv.visitEnd();

            if (debugOutput != null)
            {
                Console.WriteLine(debugOutput.ToString());
            }

            compiledClass = loadExecutable(context, className, cw.toByteArray());

            return(compiledClass);
        }