Search in sources :

Example 21 with ParsingException

use of com.dat3m.dartagnan.exception.ParsingException in project Dat3M by hernanponcedeleon.

the class VisitorBoogie method visitAssign_cmd.

@Override
public Object visitAssign_cmd(Assign_cmdContext ctx) {
    ExprsContext exprs = ctx.def_body().exprs();
    if (ctx.Ident().size() != 1 && exprs.expr().size() != ctx.Ident().size()) {
        throw new ParsingException("There should be one expression per variable\nor only one expression for all in " + ctx.getText());
    }
    for (int i = 0; i < ctx.Ident().size(); i++) {
        ExprInterface value = (ExprInterface) exprs.expr(i).accept(this);
        if (value == null) {
            continue;
        }
        String name = ctx.Ident(i).getText();
        if (smackDummyVariables.contains(name)) {
            continue;
        }
        if (constantsTypeMap.containsKey(name)) {
            throw new ParsingException("Constants cannot be assigned: " + ctx.getText());
        }
        if (initMode) {
            programBuilder.initLocEqConst(name, ((IExpr) value).reduce());
            continue;
        }
        Register register = programBuilder.getRegister(threadCount, currentScope.getID() + ":" + name);
        if (register != null) {
            if (ctx.getText().contains("$load.") || value instanceof MemoryObject) {
                try {
                    // This names are global so we don't use currentScope.getID(), but per thread.
                    Register reg = programBuilder.getOrCreateRegister(threadCount, ctx.Ident(0).getText(), ARCH_PRECISION);
                    String tmp = ctx.def_body().exprs().expr(0).getText();
                    tmp = tmp.substring(tmp.indexOf(",") + 1, tmp.indexOf(")"));
                    // This names are global so we don't use currentScope.getID(), but per thread.
                    Register ptr = programBuilder.getOrCreateRegister(threadCount, tmp, ARCH_PRECISION);
                    pool.addRegPtr(reg, ptr);
                } catch (Exception e) {
                // Nothing to be done
                }
                if (!allocationRegs.contains(value)) {
                    programBuilder.addChild(threadCount, EventFactory.newLoad(register, (IExpr) value, null)).setCLine(currentLine).setSourceCodeFile(sourceCodeFile);
                } else {
                    programBuilder.addChild(threadCount, EventFactory.newLoad(register, (IExpr) value, null));
                }
                continue;
            }
            value = value.visit(exprSimplifier);
            programBuilder.addChild(threadCount, EventFactory.newLocal(register, value)).setCLine(currentLine).setSourceCodeFile(sourceCodeFile);
            continue;
        }
        MemoryObject object = programBuilder.getObject(name);
        if (object != null) {
            programBuilder.addChild(threadCount, EventFactory.newStore(object, value, null)).setCLine(currentLine).setSourceCodeFile(sourceCodeFile);
            continue;
        }
        if (currentReturnName.equals(name)) {
            if (!returnRegister.isEmpty()) {
                Register ret = returnRegister.remove(returnRegister.size() - 1);
                programBuilder.addChild(threadCount, EventFactory.newLocal(ret, value)).setCLine(currentLine).setSourceCodeFile(sourceCodeFile);
            }
            continue;
        }
        throw new ParsingException("Variable " + name + " is not defined");
    }
    return null;
}
Also used : Register(com.dat3m.dartagnan.program.Register) ParsingException(com.dat3m.dartagnan.exception.ParsingException) MemoryObject(com.dat3m.dartagnan.program.memory.MemoryObject) ParsingException(com.dat3m.dartagnan.exception.ParsingException)

Example 22 with ParsingException

use of com.dat3m.dartagnan.exception.ParsingException in project Dat3M by hernanponcedeleon.

the class SVCOMPSanitizer method run.

public File run(int bound) throws ParsingException {
    File tmp = new File(System.getenv("DAT3M_HOME") + "/output/" + file.getName().substring(0, file.getName().lastIndexOf('.')) + "_tmp.c");
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath())));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
        StringBuilder contents = new StringBuilder();
        while (reader.ready()) {
            contents.append(reader.readLine());
        }
        reader.close();
        String stringContents = contents.toString();
        if (stringContents.matches(".*main\\(.*\\).*for.*\\{.*pthread_create(.*).*\\}")) {
            reader.close();
            writer.close();
            tmp.delete();
            throw new ParsingException("pthread_create cannot be inside a for loop");
        }
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath())));
        for (String line; (line = reader.readLine()) != null; ) {
            // SMACK does not create procedures for inline functions
            if (!line.contains("__inline")) {
                line = line.replace("inline ", "");
            }
            line = line.replace("void reach_error(){}", "__attribute__((optnone))void reach_error(){}");
            if (line.contains("while(1) { pthread_create(&t, 0, thr1, 0); }") || line.contains("while(1) pthread_create(&t, 0, thr1, 0);") || line.contains("while(__VERIFIER_nondet_int()) pthread_create(&t, 0, thr1, 0);")) {
                // While with empty body to force the creation of boundEvents
                line = line.replace("while(1) { pthread_create(&t, 0, thr1, 0); }", "for (int i = 1; i < " + bound + 1 + "; ++i) {}");
                line = line.replace("while(1) pthread_create(&t, 0, thr1, 0);", "for (int i = 1; i < " + bound + 1 + "; ++i) {}");
                line = line.replace("while(__VERIFIER_nondet_int()) pthread_create(&t, 0, thr1, 0);", "for (int i = 1; i < " + bound + 1 + "; ++i) {}");
                int i = 0;
                while (i < bound) {
                    writer.print("pthread_t t" + i + ";");
                    writer.print("pthread_create(&t" + i + ", 0, thr1, 0); ");
                    i++;
                }
            }
            if (line.contains("while(1) { pthread_create(&t, 0, thr2, 0); }")) {
                // While with empty body to force the creation of boundEvents
                line = line.replace("while(1) { pthread_create(&t, 0, thr2, 0); }", "while(1) {}");
                int i = 0;
                while (i < bound) {
                    writer.print("pthread_create(&t, 0, thr2, 0); ");
                    i++;
                }
            }
            if (line.contains("while") && line.contains("pthread_create")) {
                reader.close();
                writer.close();
                tmp.delete();
                throw new ParsingException(line + " cannot be parsed");
            }
            writer.println(line);
        }
        reader.close();
        writer.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.exit(0);
    }
    return tmp;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileOutputStream(java.io.FileOutputStream) ParsingException(com.dat3m.dartagnan.exception.ParsingException) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Aggregations

ParsingException (com.dat3m.dartagnan.exception.ParsingException)22 Register (com.dat3m.dartagnan.program.Register)10 MemoryObject (com.dat3m.dartagnan.program.memory.MemoryObject)7 Label (com.dat3m.dartagnan.program.event.core.Label)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 RecursiveRelation (com.dat3m.dartagnan.wmm.relation.RecursiveRelation)3 Relation (com.dat3m.dartagnan.wmm.relation.Relation)3 BigInteger (java.math.BigInteger)3 Atom (com.dat3m.dartagnan.expression.Atom)2 Scope (com.dat3m.dartagnan.parsers.program.boogie.Scope)2 Cmp (com.dat3m.dartagnan.program.event.core.Cmp)2 Event (com.dat3m.dartagnan.program.event.core.Event)2 FunCall (com.dat3m.dartagnan.program.event.core.annotations.FunCall)2 GlobalSettings (com.dat3m.dartagnan.GlobalSettings)1 ARCH_PRECISION (com.dat3m.dartagnan.GlobalSettings.ARCH_PRECISION)1 com.dat3m.dartagnan.expression (com.dat3m.dartagnan.expression)1 NOT (com.dat3m.dartagnan.expression.op.BOpUn.NOT)1 COpBin (com.dat3m.dartagnan.expression.op.COpBin)1 EQ (com.dat3m.dartagnan.expression.op.COpBin.EQ)1 IOpUn (com.dat3m.dartagnan.expression.op.IOpUn)1