Example #1
0
    public virtual List<Block/*!*/>/*!*/ DoInlineBlocks(List<Block/*!*/>/*!*/ blocks, ref bool inlinedSomething) {
      Contract.Requires(cce.NonNullElements(blocks));
      Contract.Ensures(cce.NonNullElements(Contract.Result<List<Block>>()));
      List<Block/*!*/>/*!*/ newBlocks = new List<Block/*!*/>();
      
      foreach (Block block in blocks) {
        TransferCmd/*!*/ transferCmd = cce.NonNull(block.TransferCmd);
        List<Cmd> cmds = block.Cmds;
        List<Cmd> newCmds = new List<Cmd>();
        int lblCount = 0;

        for (int i = 0; i < cmds.Count; ++i)
        {
            Cmd cmd = cmds[i];

            if (cmd is CallCmd)
            {
                CallCmd callCmd = (CallCmd)cmd;
                Implementation impl = FindProcImpl(program, callCmd.Proc);
                if (impl == null)
                {
                    newCmds.Add(codeCopier.CopyCmd(callCmd));
                    continue;
                }
                int inline = inlineDepth >= 0 ? inlineDepth : GetInlineCount(callCmd, impl);
                if (inline > 0)
                {
                    inlinedSomething = true;
                    lblCount = InlineCallCmd(block, callCmd, impl, newCmds, newBlocks, lblCount);
                    newCmds = new List<Cmd>();
                }
                else if (inline == 0)
                {
                    inlinedSomething = true;
                    if (CommandLineOptions.Clo.ProcedureInlining == CommandLineOptions.Inlining.Assert)
                    {
                        // add assert
                        newCmds.Add(new AssertCmd(callCmd.tok, Expr.False));
                    }
                    else if (CommandLineOptions.Clo.ProcedureInlining == CommandLineOptions.Inlining.Assume)
                    {
                        // add assume
                        newCmds.Add(new AssumeCmd(callCmd.tok, Expr.False));
                    }
                    else
                    {
                        // add call
                        newCmds.Add(codeCopier.CopyCmd(callCmd));
                    }
                }
                else
                {
                    newCmds.Add(codeCopier.CopyCmd(callCmd));
                }
            }
            else if (cmd is PredicateCmd)
            {
                PredicateCmd predCmd = (PredicateCmd)cmd;
                this.inlinedSomething = false;
                Expr newExpr = this.VisitExpr(predCmd.Expr);
                if (this.inlinedSomething)
                {
                    inlinedSomething = true;
                    PredicateCmd newPredCmd = (PredicateCmd)codeCopier.CopyCmd(predCmd);
                    newPredCmd.Expr = newExpr;
                    newCmds.Add(newPredCmd);
                }
                else
                {
                    newCmds.Add(codeCopier.CopyCmd(predCmd));
                }
            }
            else if (cmd is AssignCmd)
            {
                AssignCmd assignCmd = (AssignCmd)cmd;
                this.inlinedSomething = false;
                List<Expr> newRhss = new List<Expr>();
                foreach (Expr rhsExpr in assignCmd.Rhss)
                {
                    newRhss.Add(this.VisitExpr(rhsExpr));
                }
                if (this.inlinedSomething)
                {
                    inlinedSomething = true;
                    AssignCmd newAssignCmd = (AssignCmd)codeCopier.CopyCmd(assignCmd);
                    newAssignCmd.Rhss = newRhss;
                    newCmds.Add(newAssignCmd);
                }
                else
                {
                    newCmds.Add(codeCopier.CopyCmd(assignCmd));
                }
            }
            else
            {
                newCmds.Add(codeCopier.CopyCmd(cmd));
            }
        }

        Block newBlock = new Block(block.tok, ((lblCount == 0) ? (block.Label) : (block.Label + "$" + lblCount)), newCmds, codeCopier.CopyTransferCmd(transferCmd));
        newBlocks.Add(newBlock);
      }

      return newBlocks;
    }