add assert option to scratchpad command
authorN. Engelhardt <nak@symbioticeda.com>
Mon, 16 Dec 2019 12:09:31 +0000 (13:09 +0100)
committerN. Engelhardt <nak@symbioticeda.com>
Mon, 16 Dec 2019 13:00:21 +0000 (14:00 +0100)
passes/cmds/scratchpad.cc
tests/various/scratchpad.sh [deleted file]
tests/various/scratchpad.ys [new file with mode: 0644]

index 6bf14a6bd8008c902d6344818eb68a81b5cec389..805badc7ea3f8f551c8885f2e8afc78b0f626ba8 100644 (file)
@@ -34,15 +34,29 @@ struct ScratchpadPass : public Pass {
                log("    scratchpad [options]\n");
                log("\n");
                log("This pass allows to read and modify values from the scratchpad of the current\n");
-               log("design. Options:\n\n");
+               log("design. Options:\n");
+               log("\n");
                log("    -get <identifier>\n");
-               log("        print the value saved in the scratchpad under the given identifier.\n\n");
+               log("        print the value saved in the scratchpad under the given identifier.\n");
+               log("\n");
                log("    -set <identifier> <value>\n");
-               log("        save the given value in the scratchpad under the given identifier.\n\n");
+               log("        save the given value in the scratchpad under the given identifier.\n");
+               log("\n");
                log("    -unset <identifier>\n");
-               log("        remove the entry for the given identifier from the scratchpad.\n\n");
+               log("        remove the entry for the given identifier from the scratchpad.\n");
+               log("\n");
                log("    -copy <identifier_from> <identifier_to>\n");
-               log("        copy the value of the first identifier to the second identifier.\n\n");
+               log("        copy the value of the first identifier to the second identifier.\n");
+               log("\n");
+               log("    -assert <identifier> <value>\n");
+               log("        assert that the entry for the given identifier is set to the given value.\n");
+               log("\n");
+               log("    -assert-set <identifier>\n");
+               log("        assert that the entry for the given identifier exists.\n");
+               log("\n");
+               log("    -assert-unset <identifier>\n");
+               log("        assert that the entry for the given identifier does not exist.\n");
+               log("\n");
                log("The identifier may not contain whitespace. By convention, it is usually prefixed\n");
                log("by the name of the pass that uses it, e.g. 'opt.did_something'. If the value\n");
                log("contains whitespace, it must be enclosed in double quotes.\n");
@@ -83,6 +97,31 @@ struct ScratchpadPass : public Pass {
                                design->scratchpad_set_string(identifier_to, value);
                                continue;
                        }
+                       if (args[argidx] == "-assert" && argidx+2 < args.size()) {
+                               string identifier = args[++argidx];
+                               string expected = args[++argidx];
+                               if (expected.front() == '\"' && expected.back() == '\"') expected = expected.substr(1, expected.size() - 2);
+                               if (design->scratchpad.count(identifier) == 0)
+                                       log_error("Assertion failed: scratchpad entry '%s' is not defined\n", identifier.c_str());
+                               string value = design->scratchpad_get_string(identifier);
+                               if (value != expected) {
+                                       log_error("Assertion failed: scratchpad entry '%s' is set to '%s' instead of the asserted '%s'\n",
+                                                  identifier.c_str(), value.c_str(), expected.c_str());
+                               }
+                               continue;
+                       }
+                       if (args[argidx] == "-assert-set" && argidx+1 < args.size()) {
+                               string identifier = args[++argidx];
+                               if (design->scratchpad.count(identifier) == 0)
+                                       log_error("Assertion failed: scratchpad entry '%s' is not defined\n", identifier.c_str());
+                               continue;
+                       }
+                       if (args[argidx] == "-assert-unset" && argidx+1 < args.size()) {
+                               string identifier = args[++argidx];
+                               if (design->scratchpad.count(identifier) > 0)
+                                       log_error("Assertion failed: scratchpad entry '%s' is defined\n", identifier.c_str());
+                               continue;
+                       }
                        log("Unrecognized argument: %s\n", args[argidx].c_str());
                        break;
                }
diff --git a/tests/various/scratchpad.sh b/tests/various/scratchpad.sh
deleted file mode 100755 (executable)
index 4e92473..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/bash
-
-trap 'echo "ERROR in scratchpad.sh" >&2; exit 1' ERR
-
-../../yosys -qp "scratchpad -set foo \"bar baz\"; \
-scratchpad -copy foo oof; scratchpad -unset foo; \
-tee -o scratchpad1.log scratchpad -get oof; \
-tee -o scratchpad2.log scratchpad -get foo"
-
-test "$(cat scratchpad1.log)" = "bar baz"
-test "$(cat scratchpad2.log)" = "\"foo\" not set"
-
-rm scratchpad1.log
-rm scratchpad2.log
diff --git a/tests/various/scratchpad.ys b/tests/various/scratchpad.ys
new file mode 100644 (file)
index 0000000..dc94081
--- /dev/null
@@ -0,0 +1,5 @@
+scratchpad -set foo "bar baz"
+scratchpad -copy foo oof
+scratchpad -unset foo
+scratchpad -assert oof "bar baz"
+scratchpad -assert-unset foo