@Override public IntConstantExpr clone() { return new IntConstantExpr(value); }
private static boolean isIntValue(Expr expr, List<String> values) { if (!(expr instanceof IntConstantExpr)) { return false; } return values.contains(((IntConstantExpr) expr).getValue()); }
private boolean isZero(Expr expr) { return expr instanceof IntConstantExpr && ((IntConstantExpr) expr).getText().equals("0"); }
private Expr makeZeroInt() { return new IntConstantExpr("0"); }
@Override public void visitIntConstantExpr(IntConstantExpr intConstantExpr) { out.append(intConstantExpr.getValue()); }
private IntConstantExpr randomIntLiteral(IRandom generator) { final int maxValue = 200000; return new IntConstantExpr(String.valueOf(generator.nextInt(maxValue) - (maxValue / 2))); }
private static Optional<Integer> maybeGetIntegerValue(Expr expr) { if (!(expr instanceof IntConstantExpr)) { return Optional.empty(); } return Optional.of(Integer.parseInt(((IntConstantExpr) expr).getValue())); }
private List<Stmt> generateUnreachableSwitchContent(int numCases, List<Integer> usedLabels, Fuzzer stmtFuzzer, IRandom generator) { List<Stmt> stmts = new ArrayList<>(); for (int i = 0; i < numCases; i++) { Integer caseLabel = getCaseLabel(usedLabels, generator); final Stmt fuzzedStmt = stmtFuzzer.fuzzStmt(); stmts.add(new ExprCaseLabel( new IntConstantExpr(caseLabel.toString()))); stmts.add(fuzzedStmt); } return stmts; }
private static boolean staticallyInBounds(Expr index, Type type) { if (!(index instanceof IntConstantExpr)) { return false; } Integer indexValue = Integer.parseInt(((IntConstantExpr) index).getValue()); return indexValue >= 0 && indexValue < getSize(type); }
public static Expr getBasicTypeLiteralExpr(BasicType baseType, List<Number> args) { List<Expr> argExprs; if (baseType.getElementType() == BasicType.FLOAT) { argExprs = args.stream().map(item -> new FloatConstantExpr(item.toString())) .collect(Collectors.toList()); } else if (baseType.getElementType() == BasicType.UINT) { argExprs = args.stream().map(item -> new UIntConstantExpr(item.toString() + "u")) .collect(Collectors.toList()); } else { argExprs = args.stream().map(item -> new IntConstantExpr(item.toString())) .collect(Collectors.toList()); } if (argExprs.size() == 1) { return argExprs.get(0); } return new TypeConstructorExpr(baseType.toString(), argExprs); }
private boolean isZeroSwitchCase(Stmt stmt) { return stmt instanceof ExprCaseLabel && ((IntConstantExpr) ((ExprCaseLabel) stmt).getExpr()).getValue().equals("0"); }
private List<Stmt> generateReachableSwitchCases(BlockStmt block, int numCases, List<Integer> usedLabels, IRandom generator) { // Index i of this list stores the case labels to appear before statement i List<List<Integer>> stmtIndexToCaseLabels = new ArrayList<>(); // Initially make the label sets empty for (int i = 0; i < block.getNumStmts(); i++) { stmtIndexToCaseLabels.add(new ArrayList<>()); } // Label 0, which is what we should actually jump to, is the first label. stmtIndexToCaseLabels.get(0).add(0); for (int i = 0; i < numCases; i++) { stmtIndexToCaseLabels.get(generator.nextInt(block.getNumStmts())) .add(getCaseLabel(usedLabels, generator)); } List<Stmt> result = new ArrayList<>(); for (int i = 0; i < block.getNumStmts(); i++) { result.addAll(stmtIndexToCaseLabels.get(i) .stream().map(item -> new ExprCaseLabel(new IntConstantExpr(item.toString()))) .collect(Collectors.toList())); result.add(block.getStmt(i)); } result.add(BreakStmt.INSTANCE); return result; }
public void notifySwitchCase(ExprCaseLabel caseLabel) { assert !switchStmts.isEmpty(); Optional<SwitchCaseStatus> status = switchStmts.peekLast(); if (!status.isPresent()) { return; } if (status.get() == SwitchCaseStatus.NO_LABEL_YET) { status = resetSwitchCaseStatus(SwitchCaseStatus.BEFORE_ORIGINAL_CODE); } if (((IntConstantExpr) caseLabel.getExpr()).getValue().equals("0")) { assert status.get() == SwitchCaseStatus.BEFORE_ORIGINAL_CODE; resetSwitchCaseStatus(SwitchCaseStatus.IN_ORIGINAL_CODE); } }
@Override public void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr) { if (!literalToUniformIndex.containsKey(floatConstantExpr.getValue())) { literalToUniformIndex.put(floatConstantExpr.getValue(), uniformIndex); uniformIndex++; } parentMap.getParent(floatConstantExpr).replaceChild(floatConstantExpr, new ArrayIndexExpr(new VariableIdentifierExpr(Constants.FLOAT_CONST), new IntConstantExpr(Integer.toString( literalToUniformIndex.get(floatConstantExpr.getValue()))))); }
private Optional<Integer> getAsConstant(Expr expr) { if (expr instanceof ParenExpr) { return getAsConstant(((ParenExpr)expr).getExpr()); } if (expr instanceof IntConstantExpr) { return Optional.of(Integer.valueOf(((IntConstantExpr) expr).getValue())); } if (expr instanceof UnaryExpr) { final UnaryExpr unaryExpr = (UnaryExpr) expr; switch (unaryExpr.getOp()) { case MINUS: return getAsConstant(unaryExpr.getExpr()).map(item -> -item); case PLUS: return getAsConstant(unaryExpr.getExpr()); default: return Optional.empty(); } } return Optional.empty(); }
private void adjustInitializer(ForStmt loop, int numIterationsToSplitAfter, LoopSplitInfo loopSplitInfo) { final Integer newStart = new Integer(loopSplitInfo.getStartValue() + (loopSplitInfo.getIncreasing() ? 1 : -1) * numIterationsToSplitAfter); VariablesDeclaration varDecl = ((DeclarationStmt) loop.getInit()).getVariablesDeclaration(); varDecl.getDeclInfo(0).setInitializer(new ScalarInitializer(new IntConstantExpr( newStart.toString()))); }
private ArrayInfo getArrayInfo(Array_specifierContext arraySpecifierContext) { if (arraySpecifierContext.array_specifier() != null) { throw new RuntimeException("Not yet supporting multi-dimmensional arrays"); } if (arraySpecifierContext.constant_expression() == null) { // An array with unspecified length. return new ArrayInfo(); } final Expr expr = (Expr) visit(arraySpecifierContext.constant_expression()); if (expr instanceof IntConstantExpr) { return new ArrayInfo(Integer.parseInt(((IntConstantExpr) expr).getValue())); } throw new RuntimeException("Unable to construct array info for array with size " + expr.getText()); }
private void adjustBound(ForStmt loop, int numIterationsToSplitAfter, LoopSplitInfo loopSplitInfo, String newLoopCounter) { final Integer newBound = new Integer(loopSplitInfo.getStartValue() + (loopSplitInfo.getIncreasing() ? 1 : -1) * numIterationsToSplitAfter); final BinOp newOp = loopSplitInfo.getIncreasing() ? BinOp.LT : BinOp.GT; loop.setCondition( new BinaryExpr( new VariableIdentifierExpr(newLoopCounter), new IntConstantExpr(newBound.toString()), newOp)); }
private boolean canMergeLoops(Stmt first, Stmt second) { if (!(first instanceof ForStmt && second instanceof ForStmt)) { return false; } ForStmt firstLoop = (ForStmt) first; ForStmt secondLoop = (ForStmt) second; Optional<String> commonLoopCounter = checkForCommonLoopCounter(firstLoop, secondLoop); if (!commonLoopCounter.isPresent()) { return false; } if (!commonLoopCounter.get().startsWith(Constants.SPLIT_LOOP_COUNTER_PREFIX)) { return false; } if (!hasRegularLoopGuard(firstLoop, commonLoopCounter.get())) { return false; } if (!hasRegularLoopGuard(secondLoop, commonLoopCounter.get())) { return false; } final Integer firstLoopEnd = new Integer(((IntConstantExpr) ((BinaryExpr) firstLoop.getCondition()).getRhs()).getValue()); final BinOp firstLoopOp = ((BinaryExpr) firstLoop.getCondition()).getOp(); final Integer secondLoopStart = new Integer(((IntConstantExpr) ((ScalarInitializer) ((DeclarationStmt) secondLoop.getInit()).getVariablesDeclaration().getDeclInfo(0) .getInitializer()).getExpr()).getValue()); assert firstLoopOp == BinOp.LT || firstLoopOp == BinOp.GT : "Unexpected operator in split loops."; return firstLoopEnd.equals(secondLoopStart); }
@Override public Expr generateExpr(IRandom generator, Expr... args) { assert args.length == getNumArguments(); int index; if (BasicType.allVectorTypes().contains(argType)) { index = generator.nextInt(argType.getNumElements()); } else { assert BasicType.allMatrixTypes().contains(argType); index = generator.nextInt(BasicType.numColumns(argType)); } return new ArrayIndexExpr(args[0], new IntConstantExpr(String.valueOf(index))); }