Example #1
0
 private static void logCode(SequenceCodeInstruction sequenceCodeInstruction, int highlightAddress)
 {
     foreach (CodeInstruction codeInstruction in sequenceCodeInstruction.CodeSequence.Instructions)
     {
         logCode(codeInstruction, highlightAddress);
     }
 }
Example #2
0
        private void splitCodeSequences(CompilerContext context, int methodMaxInstructions)
        {
            IList <CodeSequence> codeSequences = new List <CodeSequence>();

            generateCodeSequences(codeSequences, methodMaxInstructions);
            codeSequences.Sort();

            int currentMethodInstructions           = codeInstructions.Count;
            IList <CodeSequence> sequencesToBeSplit = new List <CodeSequence>();

            foreach (CodeSequence codeSequence in codeSequences)
            {
                sequencesToBeSplit.Add(codeSequence);
                //if (log.DebugEnabled)
                {
                    Console.WriteLine("Sequence to be split: " + codeSequence.ToString());
                }
                currentMethodInstructions -= codeSequence.Length;
                if (currentMethodInstructions <= methodMaxInstructions)
                {
                    break;
                }
            }

            CodeSequence currentCodeSequence = null;

//JAVA TO C# CONVERTER WARNING: Unlike Java's ListIterator, enumerators in .NET do not allow altering the collection:
            for (IEnumerator <CodeInstruction> lit = codeInstructions.GetEnumerator(); lit.MoveNext();)
            {
                CodeInstruction codeInstruction = lit.Current;
                CodeSequence    codeSequence    = findCodeSequence(codeInstruction, sequencesToBeSplit, currentCodeSequence);
                if (codeSequence != null)
                {
//JAVA TO C# CONVERTER TODO TASK: .NET enumerators are read-only:
                    lit.remove();
                    if (codeSequence.Instructions.Count == 0)
                    {
                        codeSequence.addInstruction(codeInstruction);
                        SequenceCodeInstruction sequenceCodeInstruction = new SequenceCodeInstruction(codeSequence);
                        lit.add(sequenceCodeInstruction);
                        sequenceCodeInstructions.AddLast(sequenceCodeInstruction);
                    }
                    else
                    {
                        codeSequence.addInstruction(codeInstruction);
                    }
                    currentCodeSequence = codeSequence;
                }
            }
        }
Example #3
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);
        }