Beispiel #1
0
  private Function addAssumes(Function f) {
    Map<Expression, Expression> lenVars = new HashMap<Expression, Expression>();
    List<Statement> stmts = new ArrayList<Statement>();
    for (Parameter p : f.getParams()) {
      Type t = p.getType();
      if (t.isArray()) {
        TypeArray ta = (TypeArray) t;
        Type tb = ta.getBase();
        if (!tb.isArray()) {
          Expression len = ta.getLength();
          if (!len.isConstant()) {
            Expression v = lenVars.get(len);
            if (v == null) {
              String vname = varGen.nextVar("valen_" + p.getName());
              v = new ExprVar(p, vname);
              lenVars.put(len, v);
              stmts.add(new StmtVarDecl(p, TypePrimitive.inttype, vname, len));
            }
            Expression zero = tb.defaultValue();
            Expression arr = new ExprVar(p, p.getName());
            for (int j = 0; j < arrSize; ++j) {
              Expression jexp = constInts.get(j);
              Expression cond1 = new ExprBinary(p, ExprBinary.BINOP_GT, v, jexp);
              Expression cond2 =
                  new ExprBinary(p, ExprBinary.BINOP_EQ, zero, new ExprArrayRange(arr, jexp));
              stmts.add(
                  new StmtAssume(
                      p,
                      new ExprBinary(p, ExprBinary.BINOP_OR, cond1, cond2),
                      "trunc " + p.getName() + " " + j));
            }
          }
        }
      }
    }

    if (stmts.isEmpty()) {
      return f;
    } else {
      stmts.addAll(((StmtBlock) f.getBody()).getStmts());
      return f.creator().body(new StmtBlock(stmts)).create();
    }
  }