Ejemplo n.º 1
0
        private void ProcessConditionalStatement(IConditionalStatement pStatement)
        {
            if (mCurrentBlock.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
            }

            HLLocation         locationCondition = ProcessExpression(pStatement.Condition);
            HLInstructionBlock blockParent       = mCurrentBlock;

            HLInstructionBlock blockTrueStart = CreateBlock(CreateLabel());

            mCurrentBlock = blockTrueStart;
            ProcessStatement(pStatement.TrueBranch);
            HLInstructionBlock blockTrueEnd = mCurrentBlock;

            HLInstructionBlock blockFalseStart = CreateBlock(CreateLabel());

            mCurrentBlock = blockFalseStart;
            ProcessStatement(pStatement.FalseBranch);
            HLInstructionBlock blockFalseEnd = mCurrentBlock;

            blockParent.EmitBranch(locationCondition, blockTrueStart.StartLabel, blockFalseStart.StartLabel);

            if (!blockTrueEnd.Terminated || !blockFalseEnd.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
                blockTrueEnd.Terminate(mCurrentBlock.StartLabel);
                blockFalseEnd.Terminate(mCurrentBlock.StartLabel);
            }
        }
Ejemplo n.º 2
0
 private void ProcessEmptyStatement(IEmptyStatement pStatement)
 {
     if (mCurrentBlock.Terminated)
     {
         mCurrentBlock = CreateBlock(CreateLabel());
     }
 }
Ejemplo n.º 3
0
        private HLLocation ProcessConditionalExpression(IConditional pExpression)
        {
            HLLocation         locationCondition = ProcessExpression(pExpression.Condition);
            HLInstructionBlock blockParent       = mCurrentBlock;

            HLLocation locationResult = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            HLInstructionBlock blockTrueStart = CreateBlock(CreateLabel());

            mCurrentBlock = blockTrueStart;
            HLLocation locationTrue = ProcessExpression(pExpression.ResultIfTrue);

            mCurrentBlock.EmitAssignment(locationResult, locationTrue);
            HLInstructionBlock blockTrueEnd = mCurrentBlock;

            HLInstructionBlock blockFalseStart = CreateBlock(CreateLabel());

            mCurrentBlock = blockFalseStart;
            HLLocation locationFalse = ProcessExpression(pExpression.ResultIfFalse);

            mCurrentBlock.EmitAssignment(locationResult, locationFalse);
            HLInstructionBlock blockFalseEnd = mCurrentBlock;

            blockParent.EmitBranch(locationCondition, blockTrueStart.StartLabel, blockFalseStart.StartLabel);

            mCurrentBlock = CreateBlock(CreateLabel());
            blockTrueEnd.Terminate(mCurrentBlock.StartLabel);
            blockFalseEnd.Terminate(mCurrentBlock.StartLabel);

            return(locationResult);
        }
Ejemplo n.º 4
0
        public HLInstructionBlock CreateBlock(HLLabel pStartLabel)
        {
            HLInstructionBlock block = HLInstructionBlock.Create(this, pStartLabel);

            mBlocks.Add(block);
            return(block);
        }
Ejemplo n.º 5
0
        internal void Process()
        {
            if (Definition.IsExternal)
            {
                return;
            }
            ISourceMethodBody sourceMethodBody = null;

            if (Definition.Body is ISourceMethodBody)
            {
                sourceMethodBody = (ISourceMethodBody)Definition.Body;
            }
            else
            {
                sourceMethodBody = Decompiler.GetCodeModelFromMetadataModel(HLDomain.Host, Definition.Body, null);
            }

            mCurrentBlock = CreateBlock(CreateLabel());
            ProcessStatement(sourceMethodBody.Block);
            mCurrentBlock.Terminate(null);

            List <string> buf = new List <string>();

            foreach (HLInstructionBlock block in mBlocks)
            {
                foreach (HLInstruction instruction in block.Instructions)
                {
                    buf.Add(instruction.ToString());
                }
            }
        }
Ejemplo n.º 6
0
 public static HLInstructionBlock Create(HLMethod pMethod, HLLabel pStartLabel)
 {
     HLInstructionBlock block = new HLInstructionBlock();
     block.mMethod = pMethod;
     block.mStartLabel = pStartLabel;
     block.EmitLabel(pStartLabel);
     return block;
 }
Ejemplo n.º 7
0
        public static HLInstructionBlock Create(HLMethod pMethod, HLLabel pStartLabel)
        {
            HLInstructionBlock block = new HLInstructionBlock();

            block.mMethod     = pMethod;
            block.mStartLabel = pStartLabel;
            block.EmitLabel(pStartLabel);
            return(block);
        }
Ejemplo n.º 8
0
        private void ProcessExpressionStatement(IExpressionStatement pStatement)
        {
            if (mCurrentBlock.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
            }

            ProcessExpression(pStatement.Expression);
        }
Ejemplo n.º 9
0
        private void ProcessLabeledStatement(ILabeledStatement pStatement)
        {
            HLLabel labelTarget = null;

            if (!mRemappedLabels.TryGetValue(pStatement.Label.Value, out labelTarget))
            {
                labelTarget = CreateLabel();
                mRemappedLabels.Add(pStatement.Label.Value, labelTarget);
            }
            mCurrentBlock.Terminate(labelTarget);
            mCurrentBlock = CreateBlock(labelTarget);
            ProcessStatement(pStatement.Statement);
        }
Ejemplo n.º 10
0
        private void ProcessReturnStatement(IReturnStatement pStatement)
        {
            if (mCurrentBlock.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
            }

            HLLocation locationExpression = null;

            if (pStatement.Expression != null)
            {
                locationExpression = ProcessExpression(pStatement.Expression);
            }
            mCurrentBlock.EmitReturn(locationExpression);
        }
Ejemplo n.º 11
0
        private void ProcessSwitchStatement(ISwitchStatement pStatement)
        {
            //if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            HLLocation         locationCondition = ProcessExpression(pStatement.Expression);
            HLInstructionBlock blockParent       = mCurrentBlock;

            List <HLInstructionBlock> blocksStarts           = new List <HLInstructionBlock>();
            List <HLInstructionBlock> blocksEnds             = new List <HLInstructionBlock>();
            HLInstructionBlock        blockDefaultCase       = null;
            List <Tuple <HLLiteralLocation, HLLabel> > cases = new List <Tuple <HLLiteralLocation, HLLabel> >();

            foreach (ISwitchCase switchCase in pStatement.Cases)
            {
                HLInstructionBlock blockCase = CreateBlock(CreateLabel());
                mCurrentBlock = blockCase;
                blocksStarts.Add(blockCase);
                if (switchCase.IsDefault)
                {
                    blockDefaultCase = blockCase;
                }
                else
                {
                    HLLiteralLocation locationCase = (HLLiteralLocation)ProcessCompileTimeConstantExpression(switchCase.Expression);
                    cases.Add(new Tuple <HLLiteralLocation, HLLabel>(locationCase, blockCase.StartLabel));
                }
                foreach (IStatement statementCase in switchCase.Body)
                {
                    ProcessStatement(statementCase);
                }
                blocksEnds.Add(mCurrentBlock);
            }
            if (blockDefaultCase == null)
            {
                blockDefaultCase = CreateBlock(CreateLabel());
                mCurrentBlock    = blockDefaultCase;
                blocksStarts.Add(blockDefaultCase);
                blocksEnds.Add(blockDefaultCase);
            }

            blockParent.EmitSwitch(locationCondition, blockDefaultCase.StartLabel, cases);

            if (!blocksEnds.TrueForAll(b => b.Terminated))
            {
                mCurrentBlock = CreateBlock(CreateLabel());
                blocksEnds.ForEach(b => b.Terminate(mCurrentBlock.StartLabel));
            }
        }
Ejemplo n.º 12
0
        private void ProcessLocalDeclarationStatement(ILocalDeclarationStatement pStatement)
        {
            if (mCurrentBlock.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
            }

            if (pStatement.InitialValue == null)
            {
                return;
            }
            HLLocation locationLocalVariable = HLLocalLocation.Create(HLDomain.GetLocal(pStatement.LocalVariable));
            HLLocation locationInitialValue  = ProcessExpression(pStatement.InitialValue);

            mCurrentBlock.EmitAssignment(locationLocalVariable, locationInitialValue);
        }
Ejemplo n.º 13
0
        private void ProcessGotoStatement(IGotoStatement pStatement)
        {
            if (mCurrentBlock.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
            }

            HLLabel labelTarget = null;

            if (!mRemappedLabels.TryGetValue(pStatement.TargetStatement.Label.Value, out labelTarget))
            {
                labelTarget = CreateLabel();
                mRemappedLabels.Add(pStatement.TargetStatement.Label.Value, labelTarget);
            }
            mCurrentBlock.EmitGoto(labelTarget);
        }
Ejemplo n.º 14
0
        internal void Process()
        {
            if (Definition.IsExternal) return;
            ISourceMethodBody sourceMethodBody = null;
            if (Definition.Body is ISourceMethodBody) sourceMethodBody = (ISourceMethodBody)Definition.Body;
            else sourceMethodBody = Decompiler.GetCodeModelFromMetadataModel(HLDomain.Host, Definition.Body, null);

            mCurrentBlock = CreateBlock(CreateLabel());
            ProcessStatement(sourceMethodBody.Block);
            mCurrentBlock.Terminate(null);

            List<string> buf = new List<string>();
            foreach (HLInstructionBlock block in mBlocks)
                foreach (HLInstruction instruction in block.Instructions)
                    buf.Add(instruction.ToString());
        }
Ejemplo n.º 15
0
        private void ProcessConditionalStatement(IConditionalStatement pStatement)
        {
            if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            HLLocation locationCondition = ProcessExpression(pStatement.Condition);
            HLInstructionBlock blockParent = mCurrentBlock;

            HLInstructionBlock blockTrueStart = CreateBlock(CreateLabel());
            mCurrentBlock = blockTrueStart;
            ProcessStatement(pStatement.TrueBranch);
            HLInstructionBlock blockTrueEnd = mCurrentBlock;

            HLInstructionBlock blockFalseStart = CreateBlock(CreateLabel());
            mCurrentBlock = blockFalseStart;
            ProcessStatement(pStatement.FalseBranch);
            HLInstructionBlock blockFalseEnd = mCurrentBlock;

            blockParent.EmitBranch(locationCondition, blockTrueStart.StartLabel, blockFalseStart.StartLabel);

            if (!blockTrueEnd.Terminated || !blockFalseEnd.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
                blockTrueEnd.Terminate(mCurrentBlock.StartLabel);
                blockFalseEnd.Terminate(mCurrentBlock.StartLabel);
            }
        }
Ejemplo n.º 16
0
 private void ProcessEmptyStatement(IEmptyStatement pStatement)
 {
     if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());
 }
Ejemplo n.º 17
0
        private void ProcessGotoStatement(IGotoStatement pStatement)
        {
            if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            HLLabel labelTarget = null;
            if (!mRemappedLabels.TryGetValue(pStatement.TargetStatement.Label.Value, out labelTarget))
            {
                labelTarget = CreateLabel();
                mRemappedLabels.Add(pStatement.TargetStatement.Label.Value, labelTarget);
            }
            mCurrentBlock.EmitGoto(labelTarget);
        }
Ejemplo n.º 18
0
        private void ProcessExpressionStatement(IExpressionStatement pStatement)
        {
            if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            ProcessExpression(pStatement.Expression);
        }
Ejemplo n.º 19
0
        private HLLocation ProcessConditionalExpression(IConditional pExpression)
        {
            HLLocation locationCondition = ProcessExpression(pExpression.Condition);
            HLInstructionBlock blockParent = mCurrentBlock;

            HLLocation locationResult = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            HLInstructionBlock blockTrueStart = CreateBlock(CreateLabel());
            mCurrentBlock = blockTrueStart;
            HLLocation locationTrue = ProcessExpression(pExpression.ResultIfTrue);
            mCurrentBlock.EmitAssignment(locationResult, locationTrue);
            HLInstructionBlock blockTrueEnd = mCurrentBlock;

            HLInstructionBlock blockFalseStart = CreateBlock(CreateLabel());
            mCurrentBlock = blockFalseStart;
            HLLocation locationFalse = ProcessExpression(pExpression.ResultIfFalse);
            mCurrentBlock.EmitAssignment(locationResult, locationFalse);
            HLInstructionBlock blockFalseEnd = mCurrentBlock;

            blockParent.EmitBranch(locationCondition, blockTrueStart.StartLabel, blockFalseStart.StartLabel);

            mCurrentBlock = CreateBlock(CreateLabel());
            blockTrueEnd.Terminate(mCurrentBlock.StartLabel);
            blockFalseEnd.Terminate(mCurrentBlock.StartLabel);

            return locationResult;
        }
Ejemplo n.º 20
0
 private void ProcessLabeledStatement(ILabeledStatement pStatement)
 {
     HLLabel labelTarget = null;
     if (!mRemappedLabels.TryGetValue(pStatement.Label.Value, out labelTarget))
     {
         labelTarget = CreateLabel();
         mRemappedLabels.Add(pStatement.Label.Value, labelTarget);
     }
     mCurrentBlock.Terminate(labelTarget);
     mCurrentBlock = CreateBlock(labelTarget);
     ProcessStatement(pStatement.Statement);
 }
Ejemplo n.º 21
0
        private void ProcessSwitchStatement(ISwitchStatement pStatement)
        {
            //if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            HLLocation locationCondition = ProcessExpression(pStatement.Expression);
            HLInstructionBlock blockParent = mCurrentBlock;

            List<HLInstructionBlock> blocksStarts = new List<HLInstructionBlock>();
            List<HLInstructionBlock> blocksEnds = new List<HLInstructionBlock>();
            HLInstructionBlock blockDefaultCase = null;
            List<Tuple<HLLiteralLocation, HLLabel>> cases = new List<Tuple<HLLiteralLocation, HLLabel>>();
            foreach (ISwitchCase switchCase in pStatement.Cases)
            {
                HLInstructionBlock blockCase = CreateBlock(CreateLabel());
                mCurrentBlock = blockCase;
                blocksStarts.Add(blockCase);
                if (switchCase.IsDefault) blockDefaultCase = blockCase;
                else
                {
                    HLLiteralLocation locationCase = (HLLiteralLocation)ProcessCompileTimeConstantExpression(switchCase.Expression);
                    cases.Add(new Tuple<HLLiteralLocation, HLLabel>(locationCase, blockCase.StartLabel));
                }
                foreach (IStatement statementCase in switchCase.Body)
                    ProcessStatement(statementCase);
                blocksEnds.Add(mCurrentBlock);
            }
            if (blockDefaultCase == null)
            {
                blockDefaultCase = CreateBlock(CreateLabel());
                mCurrentBlock = blockDefaultCase;
                blocksStarts.Add(blockDefaultCase);
                blocksEnds.Add(blockDefaultCase);
            }

            blockParent.EmitSwitch(locationCondition, blockDefaultCase.StartLabel, cases);

            if (!blocksEnds.TrueForAll(b => b.Terminated))
            {
                mCurrentBlock = CreateBlock(CreateLabel());
                blocksEnds.ForEach(b => b.Terminate(mCurrentBlock.StartLabel));
            }
        }
Ejemplo n.º 22
0
        private void ProcessReturnStatement(IReturnStatement pStatement)
        {
            if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            HLLocation locationExpression = null;
            if (pStatement.Expression != null) locationExpression = ProcessExpression(pStatement.Expression);
            mCurrentBlock.EmitReturn(locationExpression);
        }
Ejemplo n.º 23
0
        private void ProcessLocalDeclarationStatement(ILocalDeclarationStatement pStatement)
        {
            if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            if (pStatement.InitialValue == null) return;
            HLLocation locationLocalVariable = HLLocalLocation.Create(HLDomain.GetLocal(pStatement.LocalVariable));
            HLLocation locationInitialValue = ProcessExpression(pStatement.InitialValue);
            mCurrentBlock.EmitAssignment(locationLocalVariable, locationInitialValue);
        }