/** * Inserts the second statement right before the first statement, which must appear in the block. * * @param originalStmt A statement that must be present in the block * @param insertedStmt A statement to be inserted right before originalStmt */ public void insertBefore(Stmt originalStmt, Stmt insertedStmt) { for (int i = 0; i < stmts.size(); i++) { if (getStmt(i) == originalStmt) { insertStmt(i, insertedStmt); return; } } throw new IllegalArgumentException("Should be unreachable."); }
/** * Inserts the second statement right after the first statement, which must appear in the block. * * @param originalStmt A statement that must be present in the block * @param insertedStmt A statement to be inserted right after originalStmt */ public void insertAfter(Stmt originalStmt, Stmt insertedStmt) { for (int i = 0; i < stmts.size(); i++) { if (getStmt(i) == originalStmt) { insertStmt(i + 1, insertedStmt); return; } } throw new IllegalArgumentException("Should be unreachable."); }
@Override public void inject(Stmt stmt) { if (nextStmt == null) { blockStmt.insertStmt(blockStmt.getNumStmts(), stmt); } else { blockStmt.insertBefore(nextStmt, stmt); } }
private void addComponentVariableIfNotPresent() { if (getAllVariableDeclInfosInBlock() .stream() .anyMatch(item -> item.getName().equals(getComponentName()))) { return; } block.insertStmt(0, new DeclarationStmt(new VariablesDeclaration( getComponentType(), new VariableDeclInfo(getComponentName(), null, null)))); }
private void addGlobalInitializers(FunctionDefinition mainFunction) { assert mainFunction.getPrototype().getName().equals("main"); for (int i = globalsToBeReInitialized.size() - 1; i >= 0; i--) { for (int j = globalsToBeReInitialized.get(i).getNumDecls() - 1; j >= 0; j--) { final VariableDeclInfo vdi = globalsToBeReInitialized.get(i).getDeclInfo(j); if (!(vdi.getInitializer() instanceof ScalarInitializer)) { throw new RuntimeException("Only know how to deal with scalar initializers at present."); } mainFunction.getBody().insertStmt(0, new ExprStmt(new BinaryExpr(new VariableIdentifierExpr(vdi.getName()), ((ScalarInitializer) vdi.getInitializer()).getExpr(), BinOp.ASSIGN))); vdi.setInitializer(null); } } }
@Override public void visitFunctionCallExpr(FunctionCallExpr functionCallExpr) { super.visitFunctionCallExpr(functionCallExpr); if (functionCallExpr != call) { return; } if (currentBlockStmt == null) { // Cannot inline e.g. at global scope return; } currentBlockStmt.insertStmt(currentIndex, new BlockStmt(inlinedStmts, true)); if (returnVariableName.isPresent()) { currentBlockStmt.insertStmt(currentIndex, new DeclarationStmt( new VariablesDeclaration( clonedFunctionDefinition.getPrototype().getReturnType() .getWithoutQualifiers(), new VariableDeclInfo(returnVariableName.get(), null, null)))); parentMap.getParent(functionCallExpr).replaceChild(functionCallExpr, new VariableIdentifierExpr(returnVariableName.get())); } else { assert clonedFunctionDefinition.getPrototype().getReturnType().getWithoutQualifiers() == VoidType.VOID; assert parentMap.getParent(functionCallExpr) instanceof ExprStmt; currentBlockStmt.removeStmt((Stmt) parentMap.getParent(functionCallExpr)); } } }.visit(tu);
public void apply() { if (blockAlreadyDeclaresVector()) { return; } for (ScopeEntry se : mergeSet.getIndidualScopeEntries()) { new VectorizerVisitor(se).visit(block); } block.insertStmt(0, new DeclarationStmt(new VariablesDeclaration( mergeSet.getMergedType(), new VariableDeclInfo(mergeSet.getMergedName(), null, null)))); }
((BlockStmt) loopStmt.getBody()).insertStmt(0, limitCheckAndIncrement.get(i));
private void addSpecialDeclarations() { if (fd.getPrototype().getReturnType().getWithoutQualifiers() != VoidType.VOID) { fd.getBody().insertStmt(0, new DeclarationStmt( new VariablesDeclaration(fd.getPrototype().getReturnType().getWithoutQualifiers(), new VariableDeclInfo(makeReturnValueName(), null, null)))); fd.getBody().addStmt(new ReturnStmt(makeReturnValue())); } fd.getBody().insertStmt(0, new DeclarationStmt( new VariablesDeclaration(BasicType.BOOL, new VariableDeclInfo(makeHasReturnedName(), null, new ScalarInitializer(BoolConstantExpr.FALSE))))); }