public static HLInstructionBlock Create(HLMethod pMethod, HLLabel pStartLabel)
 {
     HLInstructionBlock block = new HLInstructionBlock();
     block.mMethod = pMethod;
     block.mStartLabel = pStartLabel;
     block.EmitLabel(pStartLabel);
     return block;
 }
Example #2
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));
            }
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
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);
        }
Example #6
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);
 }
Example #7
0
        private void ProcessExpressionStatement(IExpressionStatement pStatement)
        {
            if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());

            ProcessExpression(pStatement.Expression);
        }
Example #8
0
 private void ProcessEmptyStatement(IEmptyStatement pStatement)
 {
     if (mCurrentBlock.Terminated) mCurrentBlock = CreateBlock(CreateLabel());
 }
Example #9
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);
            }
        }
Example #10
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;
        }
Example #11
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());
        }