/// <summary>
        /// Runs the specified method compiler.
        /// </summary>
        void IMethodCompilerStage.Run()
        {
            if (methodCompiler.PlugSystem != null)
                if (methodCompiler.PlugSystem.GetPlugMethod(methodCompiler.Method) != null)
                    return;

            traversed = new BitArray(basicBlocks.Count);
            analyzed = new BitArray(basicBlocks.Count);

            RegisterBitmap start = new RegisterBitmap();

            // Main Code
            start.Set(architecture.StackFrameRegister);
            start.Set(architecture.StackPointerRegister);
            start.Set(architecture.CallingConvention.CalleeSavedRegisters);

            ProcesBlockChain(basicBlocks.PrologueBlock, start);

            // Handler Code
            foreach (ExceptionHandlingClause clause in methodCompiler.ExceptionClauseHeader.Clauses)
            {
                RegisterBitmap exceptionStart = new RegisterBitmap();
                start.Set(architecture.StackFrameRegister);
                start.Set(architecture.StackPointerRegister);

                // TODO: Set the exception register

                ProcesBlockChain(basicBlocks.GetByLabel(clause.HandlerOffset), exceptionStart);
            }
        }
        /// <summary>
        /// Performs stage specific processing on the compiler context.
        /// </summary>
        void IMethodCompilerStage.Run()
        {
            foreach (var block in this.basicBlocks)
            {
                for (var ctx = new Context(instructionSet, block); !ctx.EndOfInstruction; ctx.GotoNext())
                {
                    if (ctx.IsEmpty)
                        continue;

                    RegisterBitmap inputRegisters = new RegisterBitmap();
                    RegisterBitmap outputRegisters = new RegisterBitmap();

                    GetRegisterUsage(ctx, ref inputRegisters, ref outputRegisters);

                    Debug.WriteLine(String.Format("L_{0:X4}: {1}", ctx.Label, ctx.Instruction.ToString(ctx)));

                    if (outputRegisters.HasValue)
                    {
                        Debug.Write("\t OUTPUT: ");
                        Debug.Write(GetRegisterNames(outputRegisters));
                    }

                    if (inputRegisters.HasValue)
                    {
                        Debug.Write("\t INPUT: ");
                        Debug.Write(GetRegisterNames(inputRegisters));
                    }

                    if (inputRegisters.HasValue | outputRegisters.HasValue)
                    {
                        Debug.WriteLine("");
                    }
                }
            }
        }
Example #3
0
        protected void CheckAndResize()
        {
            if (instructionSet.Size <= map.Length)
                return;

            RegisterBitmap[] newmap = new RegisterBitmap[instructionSet.Size];
            map.CopyTo(newmap, 0);
            map = newmap;
        }
        public List<Register> GetRegisters(RegisterBitmap registers)
        {
            List<Register> list = new List<Register>();

            foreach (int index in registers)
            {
                list.Add(architecture.RegisterSet[index]);
            }

            return list;
        }
        protected string GetRegisterNames(RegisterBitmap registers)
        {
            StringBuilder list = new StringBuilder();

            foreach (Register register in GetRegisters(registers))
            {
                if (list.Length != 0)
                    list.Append(",");

                list.Append(register.ToString());
            }

            return list.ToString();
        }
        // Copied from RegisterUsageAnaluzerStage.cs
        private void GetRegisterUsage(Context context, ref RegisterBitmap inputRegisters, ref RegisterBitmap outputRegisters)
        {
            BasePlatformInstruction instruction = context.Instruction as BasePlatformInstruction;

            if (instruction == null)
                return;

            IRegisterUsage usage = instruction as IRegisterUsage;

            if (usage == null)
                return;

            outputRegisters = usage.GetOutputRegisters(context);
            inputRegisters = usage.GetInputRegisters(context);
        }
        private RegisterBitmap ProcesBlock(BasicBlock block, RegisterBitmap start)
        {
            analyzed.Set(block.Sequence, true);

            RegisterBitmap current = start;

            for (Context ctx = CreateContext(block); !ctx.EndOfInstruction; ctx.GotoNext())
            {
                ;

                // at the end of the loop

                RegisterBitmap inputRegisters = new RegisterBitmap();
                RegisterBitmap outputRegisters = new RegisterBitmap();

                GetRegisterUsage(ctx, ref inputRegisters, ref outputRegisters);

            }

            return current;
        }
        protected void AnalyzeBlock(BasicBlock block)
        {
            if (analyzed.Get(block.Sequence))
                return;

            //Debug.WriteLine(String.Format("Analyzing Block #{0} - Label L_{1:X4}", block.Index, block.Label));

            traversed.Set(block.Sequence, true);

            // Analysis all child blocks first
            foreach (BasicBlock nextBlock in block.NextBlocks)
            {
                if (!traversed.Get(nextBlock.Sequence))
                    AnalyzeBlock(nextBlock);
            }

            //Debug.WriteLine(String.Format("Working Block #{0} - Label L_{1:X4}", block.Index, block.Label));

            RegisterBitmap used = new RegisterBitmap();

            if (block.NextBlocks.Count == 0)
            {
                if (block.Label == Int32.MaxValue)
                {
                    used.Set(architecture.StackFrameRegister);
                    used.Set(architecture.StackPointerRegister);

                    used.Set(architecture.CallingConvention.GetReturnRegisters(methodCompiler.Method.Signature.ReturnType.Type));
                    used.Set(architecture.CallingConvention.CalleeSavedRegisters);
                }
            }
            else
            {
                foreach (BasicBlock nextBlock in block.NextBlocks)
                    used.Or(top[nextBlock.Sequence]);
            }

            bottom[block.Sequence] = used;

            var ctx = new Context(instructionSet, block);
            ctx.GotoLast();

            for (; ; ctx.GotoPrevious())
            {
                if (ctx.Ignore || ctx.Instruction == null)
                    if (ctx.IsFirstInstruction)
                        break;
                    else
                        continue;

                RegisterBitmap inputRegisters = new RegisterBitmap();
                RegisterBitmap outputRegisters = new RegisterBitmap();

                GetRegisterUsage(ctx, ref inputRegisters, ref outputRegisters);

                RegisterBitmap assignment = inputRegisters;
                assignment.Xor(outputRegisters);
                assignment.Not();

                used.And(assignment);

                used.Or(inputRegisters);

                usage[ctx.Index] = used;

                if (ctx.IsFirstInstruction)
                    break;
            }

            top[block.Sequence] = used;
            analyzed.Set(block.Sequence, true);
        }
Example #9
0
        /// <summary>
        /// Gets the output registers.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public virtual RegisterBitmap GetOutputRegisters(Context context)
        {
            RegisterBitmap registers = new RegisterBitmap();

            if (context.Result.IsRegister)
                registers.Set(context.Result.Register);

            registers.Or(AdditionalOutputRegisters);

            return registers;
        }
Example #10
0
        /// <summary>
        /// Gets the input registers.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public virtual RegisterBitmap GetInputRegisters(Context context)
        {
            RegisterBitmap registers = new RegisterBitmap();

            registers.Set(GetRegister(context.Operand1, true));
            registers.Set(GetRegister(context.Operand2, true));
            registers.Set(GetRegister(context.Operand3, true));
            registers.Set(GetRegister(context.Result, ResultIsInput));

            registers.Or(AdditionalInputRegisters);

            return registers;
        }
        /// <summary>
        /// Gets the output registers.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public virtual RegisterBitmap GetOutputRegisters(Context context)
        {
            RegisterBitmap registers = new RegisterBitmap();

            RegisterOperand regOperand = context.Result as RegisterOperand;

            if (regOperand != null)
                registers.Set(regOperand.Register);

            registers.Or(AdditionalOutputRegisters);

            return registers;
        }
        private void ProcesBlockChain(BasicBlock block, RegisterBitmap start)
        {
            if (analyzed.Get(block.Sequence))
                return;

            RegisterBitmap end = ProcesBlock(block, start);

            foreach (BasicBlock nextBlock in block.NextBlocks)
            {
                ProcesBlockChain(nextBlock, end);
            }
        }