Move options parsing of UhdmAstFrontend to UhdmCommonFrontend

Signed-off-by: Ryszard Różak <rrozak@antmicro.com>
diff --git a/uhdm-plugin/Makefile b/uhdm-plugin/Makefile
index 9f43dc0..223a126 100644
--- a/uhdm-plugin/Makefile
+++ b/uhdm-plugin/Makefile
@@ -9,6 +9,7 @@
 NAME = uhdm
 SOURCES = UhdmAst.cc \
 	  uhdmastfrontend.cc \
+	  uhdmcommonfrontend.cc \
 	  uhdmsurelogastfrontend.cc \
 	  uhdmastreport.cc
 
diff --git a/uhdm-plugin/uhdmastfrontend.cc b/uhdm-plugin/uhdmastfrontend.cc
index c857076..41aa93c 100644
--- a/uhdm-plugin/uhdmastfrontend.cc
+++ b/uhdm-plugin/uhdmastfrontend.cc
@@ -19,9 +19,7 @@
  *
  */
 
-#include "UhdmAst.h"
-#include "frontends/ast/ast.h"
-#include "kernel/yosys.h"
+#include "uhdmcommonfrontend.h"
 
 namespace UHDM
 {
@@ -31,85 +29,25 @@
 
 YOSYS_NAMESPACE_BEGIN
 
-/* Stub for AST::process */
-static void set_line_num(int) {}
-
-/* Stub for AST::process */
-static int get_line_num(void) { return 1; }
-
-struct UhdmAstFrontend : public Frontend {
-    UhdmAstFrontend() : Frontend("uhdm", "read UHDM file") {}
-    void help()
+struct UhdmAstFrontend : public UhdmCommonFrontend {
+    UhdmAstFrontend() : UhdmCommonFrontend("uhdm", "read UHDM file") {}
+    AST::AstNode *parse(std::string filename)
     {
-        //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
-        log("\n");
-        log("    read_uhdm [options] [filename]\n");
-        log("\n");
-        log("Load design from a UHDM file into the current design\n");
-        log("\n");
-        log("    -noassert\n");
-        log("        ignore assert() statements");
-        log("\n");
-        log("    -debug\n");
-        log("        print debug info to stdout");
-        log("\n");
-        log("    -report [directory]\n");
-        log("        write a coverage report for the UHDM file\n");
-        log("\n");
-        log("    -defer\n");
-        log("        only read the abstract syntax tree and defer actual compilation\n");
-        log("        to a later 'hierarchy' command. Useful in cases where the default\n");
-        log("        parameters of modules yield invalid or not synthesizable code.\n");
-        log("\n");
-    }
-    void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
-    {
-        log_header(design, "Executing UHDM frontend.\n");
-
-        UhdmAstShared shared;
-        UhdmAst uhdm_ast(shared);
-        bool defer = false;
-
-        std::string report_directory;
-        for (size_t i = 1; i < args.size(); i++) {
-            if (args[i] == "-debug") {
-                shared.debug_flag = true;
-            } else if (args[i] == "-report" && ++i < args.size()) {
-                report_directory = args[i];
-                shared.stop_on_error = false;
-            } else if (args[i] == "-noassert") {
-                shared.no_assert = true;
-            } else if (args[i] == "-defer") {
-                defer = true;
-            }
-        }
-        extra_args(f, filename, args, args.size() - 1);
-
-        AST::current_filename = filename;
-        AST::set_line_num = &set_line_num;
-        AST::get_line_num = &get_line_num;
-        struct AST::AstNode *current_ast;
-
         UHDM::Serializer serializer;
 
         std::vector<vpiHandle> restoredDesigns = serializer.Restore(filename);
         for (auto design : restoredDesigns) {
             std::stringstream strstr;
-            UHDM::visit_object(design, 1, "", &shared.report.unhandled, shared.debug_flag ? std::cout : strstr);
+            UHDM::visit_object(design, 1, "", &this->shared.report.unhandled, this->shared.debug_flag ? std::cout : strstr);
         }
-        current_ast = uhdm_ast.visit_designs(restoredDesigns);
-        if (!report_directory.empty()) {
-            shared.report.write(report_directory);
+        UhdmAst uhdm_ast(this->shared);
+        AST::AstNode *current_ast = uhdm_ast.visit_designs(restoredDesigns);
+        if (!this->report_directory.empty()) {
+            this->shared.report.write(this->report_directory);
         }
         for (auto design : restoredDesigns)
             vpi_release_handle(design);
-        bool dump_ast1 = shared.debug_flag;
-        bool dump_ast2 = shared.debug_flag;
-        bool dont_redefine = false;
-        bool default_nettype_wire = true;
-        AST::process(design, current_ast, dump_ast1, dump_ast2, false, false, false, false, false, false, false, false, false, false, false, false,
-                     false, false, dont_redefine, false, defer, default_nettype_wire);
-        delete current_ast;
+        return current_ast;
     }
 } UhdmAstFrontend;
 
diff --git a/uhdm-plugin/uhdmcommonfrontend.cc b/uhdm-plugin/uhdmcommonfrontend.cc
new file mode 100644
index 0000000..16a3007
--- /dev/null
+++ b/uhdm-plugin/uhdmcommonfrontend.cc
@@ -0,0 +1,91 @@
+/*
+ *  yosys -- Yosys Open SYnthesis Suite
+ *
+ *  Copyright (C) 2020 Antmicro
+
+ *  Based on frontends/json/jsonparse.cc
+ *
+ *  Permission to use, copy, modify, and/or distribute this software for any
+ *  purpose with or without fee is hereby granted, provided that the above
+ *  copyright notice and this permission notice appear in all copies.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include "uhdmcommonfrontend.h"
+
+YOSYS_NAMESPACE_BEGIN
+
+/* Stub for AST::process */
+static void set_line_num(int) {}
+
+/* Stub for AST::process */
+static int get_line_num(void) { return 1; }
+
+void UhdmCommonFrontend::help()
+{
+    //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
+    log("\n");
+    log("    read_uhdm [options] [filename]\n");
+    log("\n");
+    log("Load design from a UHDM file into the current design\n");
+    log("\n");
+    log("    -noassert\n");
+    log("        ignore assert() statements");
+    log("\n");
+    log("    -debug\n");
+    log("        print debug info to stdout");
+    log("\n");
+    log("    -report [directory]\n");
+    log("        write a coverage report for the UHDM file\n");
+    log("\n");
+    log("    -defer\n");
+    log("        only read the abstract syntax tree and defer actual compilation\n");
+    log("        to a later 'hierarchy' command. Useful in cases where the default\n");
+    log("        parameters of modules yield invalid or not synthesizable code.\n");
+    log("\n");
+}
+
+void UhdmCommonFrontend::execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
+{
+    bool defer = false;
+
+    std::string report_directory;
+    for (size_t i = 1; i < args.size(); i++) {
+        if (args[i] == "-debug") {
+            this->shared.debug_flag = true;
+        } else if (args[i] == "-report" && ++i < args.size()) {
+            this->report_directory = args[i];
+            this->shared.stop_on_error = false;
+        } else if (args[i] == "-noassert") {
+            this->shared.no_assert = true;
+        } else if (args[i] == "-defer") {
+            defer = true;
+        }
+    }
+    extra_args(f, filename, args, args.size() - 1);
+
+    AST::current_filename = filename;
+    AST::set_line_num = &set_line_num;
+    AST::get_line_num = &get_line_num;
+
+    bool dump_ast1 = this->shared.debug_flag;
+    bool dump_ast2 = this->shared.debug_flag;
+    bool dont_redefine = false;
+    bool default_nettype_wire = true;
+
+    AST::AstNode *current_ast = parse(filename);
+
+    AST::process(design, current_ast, dump_ast1, dump_ast2, false, false, false, false, false, false, false, false, false, false, false, false, false,
+                 false, dont_redefine, false, defer, default_nettype_wire);
+    delete current_ast;
+}
+
+YOSYS_NAMESPACE_END
diff --git a/uhdm-plugin/uhdmcommonfrontend.h b/uhdm-plugin/uhdmcommonfrontend.h
new file mode 100644
index 0000000..9123687
--- /dev/null
+++ b/uhdm-plugin/uhdmcommonfrontend.h
@@ -0,0 +1,39 @@
+/*
+ *  yosys -- Yosys Open SYnthesis Suite
+ *
+ *  Copyright (C) 2020 Antmicro
+
+ *  Based on frontends/json/jsonparse.cc
+ *
+ *  Permission to use, copy, modify, and/or distribute this software for any
+ *  purpose with or without fee is hereby granted, provided that the above
+ *  copyright notice and this permission notice appear in all copies.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include "UhdmAst.h"
+#include "frontends/ast/ast.h"
+#include "kernel/yosys.h"
+#include <string>
+#include <vector>
+
+YOSYS_NAMESPACE_BEGIN
+
+struct UhdmCommonFrontend : public Frontend {
+    UhdmAstShared shared;
+    std::string report_directory;
+    UhdmCommonFrontend(std::string name, std::string short_help) : Frontend(name, short_help) {}
+    void help();
+    virtual AST::AstNode *parse(std::string filename) = 0;
+    void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design);
+};
+
+YOSYS_NAMESPACE_END