Ejemplo n.º 1
0
  public Object visitExprArrayRange(ExprArrayRange exp) {
    RangeLen rl = exp.getSelection();
    if (!rl.hasLen()) {
      Expression newBase = doExpression(exp.getBase());
      Expression newIndex = doExpression(rl.start());
      return new ExprArrayRange(exp, newBase, newIndex);
    } else {
      TypeArray arrType = (TypeArray) getType(exp.getBase());
      Type baseType = arrType.getBase();
      Type type =
          new TypeArray(baseType, doExpression(rl.getLenExpression()), arrType.getMaxlength());

      Expression newBase = doExpression(exp.getBase());
      Expression newIndex = doExpression(rl.start());
      Expression newLen = doExpression(rl.getLenExpression());
      if (newIndex != rl.start() || newLen != rl.getLenExpression() || newBase != exp.getBase()) {
        exp = new ExprArrayRange(exp, newBase, new RangeLen(newIndex, newLen));
      }
      String newName = varGen.nextVar(lhsname);
      StmtVarDecl decl = new StmtVarDecl(exp, type, newName, null);
      this.addStatement(decl);
      Statement assign = new StmtAssign(new ExprVar(exp, newName), exp);
      this.addStatement(assign);
      return new ExprVar(exp, newName);
    }
  }
 void addTemp(Parameter p, Expression actual, List<Expression> newParams, VarReplacer vr) {
   String name = varGen.nextVar(p.getName());
   addStatement(new StmtVarDecl(actual, (Type) p.getType().accept(vr), name, actual));
   Expression ev = new ExprVar(actual, name);
   newParams.add(ev);
   after.add(new StmtAssign(actual, ev));
 }
Ejemplo n.º 3
0
  public Object visitStmtAssign(StmtAssign stmt) {
    if (stmt.getLHS() instanceof ExprVar) {
      lhsname = stmt.getLHS().toString();
    }

    Expression newRHS = doExpression(stmt.getRHS());

    lhsname = null;

    Expression newLHS;
    Statement postAssign = null;
    if (stmt.getLHS() instanceof ExprArrayRange) {
      ExprArrayRange arng = (ExprArrayRange) stmt.getLHS();

      RangeLen rl = arng.getSelection();
      if (rl.hasLen()) {
        TypeArray arrType = (TypeArray) getType(arng.getBase());
        Type baseType = arrType.getBase();
        Type type = new TypeArray(baseType, rl.getLenExpression(), arrType.getMaxlength());

        Expression newBase = doExpression(arng.getBase());
        Expression newIndex = doExpression(rl.start());
        Expression newLen = doExpression(rl.getLenExpression());
        if (newIndex != rl.start()
            || newLen != rl.getLenExpression()
            || newBase != arng.getBase()) {
          arng = new ExprArrayRange(arng, newBase, new RangeLen(newIndex, newLen));
        }
        String newName = varGen.nextVar();
        StmtVarDecl decl = new StmtVarDecl(arng, type, newName, null);
        addStatement(decl);
        Statement assign = new StmtAssign(new ExprVar(arng, newName), newRHS);
        addStatement(assign);
        return new StmtAssign(stmt, arng, new ExprVar(stmt, newName), stmt.getOp());
      } else {
        newLHS = doExpression(stmt.getLHS());
      }
    } else {
      newLHS = doExpression(stmt.getLHS());
    }

    if (newLHS == stmt.getLHS() && newRHS == stmt.getRHS()) return stmt;
    return new StmtAssign(stmt, newLHS, newRHS, stmt.getOp());
  }
Ejemplo n.º 4
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();
    }
  }