systemc: Refactor reporting to prep for int based messages.

There's a deprecated reporting mechanism based on integer message ids,
and the reporting mechanism needs to be refactored a bit to make it
easier to support.

Some bookkeeping data structures were moved out to somewhere they
can be accessed by other code, obviating the non-standard get_handler
function.

Change-Id: Id427cd79be9ef0f3275fbac39ff047ab672fb3e0
Reviewed-on: https://gem5-review.googlesource.com/c/13318
Reviewed-by: Gabe Black <gabeblack@google.com>
Maintainer: Gabe Black <gabeblack@google.com>
diff --git a/src/systemc/core/sc_main.cc b/src/systemc/core/sc_main.cc
index 12e129a..95e9550 100644
--- a/src/systemc/core/sc_main.cc
+++ b/src/systemc/core/sc_main.cc
@@ -41,6 +41,7 @@
 #include "systemc/core/scheduler.hh"
 #include "systemc/ext/core/sc_main.hh"
 #include "systemc/ext/utils/sc_report_handler.hh"
+#include "systemc/utils/report.hh"
 
 // A weak symbol to detect if sc_main has been defined, and if so where it is.
 [[gnu::weak]] int sc_main(int argc, char *argv[]);
@@ -79,12 +80,12 @@
             } catch (const sc_report &r) {
                 // There was an exception nobody caught.
                 resultStr = "uncaught sc_report";
-                sc_report_handler::get_handler()(
+                sc_gem5::reportHandlerProc(
                         r, sc_report_handler::get_catch_actions());
             } catch (...) {
                 // There was some other type of exception we need to wrap.
                 resultStr = "uncaught exception";
-                sc_report_handler::get_handler()(
+                sc_gem5::reportHandlerProc(
                         ::sc_gem5::reportifyException(),
                         sc_report_handler::get_catch_actions());
             }
diff --git a/src/systemc/core/scheduler.cc b/src/systemc/core/scheduler.cc
index e0a270d..969e401 100644
--- a/src/systemc/core/scheduler.cc
+++ b/src/systemc/core/scheduler.cc
@@ -36,6 +36,7 @@
 #include "systemc/ext/core/sc_main.hh"
 #include "systemc/ext/utils/sc_report.hh"
 #include "systemc/ext/utils/sc_report_handler.hh"
+#include "systemc/utils/report.hh"
 #include "systemc/utils/tracefile.hh"
 
 namespace sc_gem5
@@ -469,8 +470,7 @@
 const ::sc_core::sc_report
 reportifyException()
 {
-    ::sc_core::sc_report_handler_proc old_handler =
-        ::sc_core::sc_report_handler::get_handler();
+    ::sc_core::sc_report_handler_proc old_handler = reportHandlerProc;
     ::sc_core::sc_report_handler::set_handler(&throwingReportHandler);
 
     try {
diff --git a/src/systemc/ext/utils/sc_report_handler.hh b/src/systemc/ext/utils/sc_report_handler.hh
index 6ce20af..f65b32c 100644
--- a/src/systemc/ext/utils/sc_report_handler.hh
+++ b/src/systemc/ext/utils/sc_report_handler.hh
@@ -105,10 +105,6 @@
     static void set_handler(sc_report_handler_proc);
     static void default_handler(const sc_report &, const sc_actions &);
     static sc_actions get_new_action_id();
-    // Nonstandard
-    // Returns the current handler so it can be restored if it needs to be
-    // changed temporarily.
-    static sc_report_handler_proc get_handler();
 
     static sc_report *get_cached_report();
     static void clear_cached_report();
diff --git a/src/systemc/utils/SConscript b/src/systemc/utils/SConscript
index 5aaea54..6e3c6fe 100644
--- a/src/systemc/utils/SConscript
+++ b/src/systemc/utils/SConscript
@@ -29,6 +29,7 @@
 
 if env['USE_SYSTEMC']:
     Source('functions.cc')
+    Source('report.cc')
     Source('sc_report.cc')
     Source('sc_report_handler.cc')
     Source('sc_trace_file.cc')
diff --git a/src/systemc/utils/report.cc b/src/systemc/utils/report.cc
new file mode 100644
index 0000000..413726e
--- /dev/null
+++ b/src/systemc/utils/report.cc
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2018 Google, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Gabe Black
+ */
+
+#include "systemc/utils/report.hh"
+
+namespace sc_gem5
+{
+
+const char *reportSeverityNames[] = {
+    [sc_core::SC_INFO] = "Info",
+    [sc_core::SC_WARNING] = "Warning",
+    [sc_core::SC_ERROR] = "Error",
+    [sc_core::SC_FATAL] = "Fatal"
+};
+
+ReportSevInfo reportSevInfos[sc_core::SC_MAX_SEVERITY] =
+{
+    [sc_core::SC_INFO] = ReportSevInfo(sc_core::SC_DEFAULT_INFO_ACTIONS),
+    [sc_core::SC_WARNING] = ReportSevInfo(sc_core::SC_DEFAULT_WARNING_ACTIONS),
+    [sc_core::SC_ERROR] = ReportSevInfo(sc_core::SC_DEFAULT_ERROR_ACTIONS),
+    [sc_core::SC_FATAL] = ReportSevInfo(sc_core::SC_DEFAULT_FATAL_ACTIONS)
+};
+
+std::map<std::string, ReportMsgInfo> reportMsgInfoMap;
+
+int reportVerbosityLevel = sc_core::SC_MEDIUM;
+
+sc_core::sc_actions reportSuppressedActions = sc_core::SC_UNSPECIFIED;
+sc_core::sc_actions reportForcedActions = sc_core::SC_UNSPECIFIED;
+sc_core::sc_actions reportCatchActions = sc_core::SC_DISPLAY;
+
+sc_core::sc_report_handler_proc reportHandlerProc =
+    &sc_core::sc_report_handler::default_handler;
+
+std::unique_ptr<sc_core::sc_report> globalReportCache;
+
+} // namespace sc_gem5
diff --git a/src/systemc/utils/report.hh b/src/systemc/utils/report.hh
new file mode 100644
index 0000000..ea0201f
--- /dev/null
+++ b/src/systemc/utils/report.hh
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2018 Google, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Gabe Black
+ */
+
+#ifndef __SYSTEMC_UTILS_REPORT_HH__
+#define __SYSTEMC_UTILS_REPORT_HH__
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include "systemc/ext/utils/sc_report.hh"
+#include "systemc/ext/utils/sc_report_handler.hh"
+
+namespace sc_gem5
+{
+
+struct ReportMsgInfo
+{
+    explicit ReportMsgInfo() :
+        actions(sc_core::SC_UNSPECIFIED), count(0), limit(-1),
+        sevActions{ sc_core::SC_UNSPECIFIED, sc_core::SC_UNSPECIFIED,
+                sc_core::SC_UNSPECIFIED, sc_core::SC_UNSPECIFIED },
+        sevCounts{0, 0, 0, 0}, sevLimits{-1, -1, -1, -1}, id(-1)
+    {}
+
+    void
+    checkLimits(sc_core::sc_severity severity, sc_core::sc_actions &actions)
+    {
+        int sevLimit = sevLimits[severity];
+        int sevCount = sevCounts[severity];
+        if ((limit != -1 && limit >= count) ||
+                (sevLimit != 1 && sevLimit >= sevCount)) {
+            actions |= sc_core::SC_STOP;
+        }
+    }
+
+    sc_core::sc_actions actions;
+    int count;
+    int limit;
+
+    sc_core::sc_actions sevActions[sc_core::SC_MAX_SEVERITY];
+    int sevCounts[sc_core::SC_MAX_SEVERITY];
+    int sevLimits[sc_core::SC_MAX_SEVERITY];
+
+    int id;
+};
+
+struct ReportSevInfo
+{
+    explicit ReportSevInfo(sc_core::sc_actions actions) :
+        actions(actions), count(0), limit(-1)
+    {}
+
+    void
+    checkLimit(sc_core::sc_actions &actions)
+    {
+        if (limit != -1 && limit >= count)
+            actions |= sc_core::SC_STOP;
+    }
+
+    sc_core::sc_actions actions;
+    int count;
+    int limit;
+};
+
+extern const char *reportSeverityNames[sc_core::SC_MAX_SEVERITY];
+extern ReportSevInfo reportSevInfos[sc_core::SC_MAX_SEVERITY];
+extern std::map<std::string, ReportMsgInfo> reportMsgInfoMap;
+
+extern int reportVerbosityLevel;
+
+extern sc_core::sc_actions reportSuppressedActions;
+extern sc_core::sc_actions reportForcedActions;
+extern sc_core::sc_actions reportCatchActions;
+
+extern sc_core::sc_report_handler_proc reportHandlerProc;
+
+extern std::unique_ptr<sc_core::sc_report> globalReportCache;
+
+} // namespace sc_gem5
+
+#endif // __SYSTEMC_UTILS_REPORT_HH__
diff --git a/src/systemc/utils/sc_report_handler.cc b/src/systemc/utils/sc_report_handler.cc
index 41bf8e8..a8cdc36 100644
--- a/src/systemc/utils/sc_report_handler.cc
+++ b/src/systemc/utils/sc_report_handler.cc
@@ -37,6 +37,7 @@
 #include "systemc/core/scheduler.hh"
 #include "systemc/ext/core/sc_main.hh"
 #include "systemc/ext/utils/sc_report_handler.hh"
+#include "systemc/utils/report.hh"
 
 namespace sc_core
 {
@@ -47,59 +48,6 @@
 std::unique_ptr<std::string> logFileName;
 std::unique_ptr<std::ofstream> logFile;
 
-struct ReportCatInfo
-{
-    explicit ReportCatInfo(sc_actions actions) :
-        actions(actions), count(0), limit(-1)
-    {}
-    ReportCatInfo() : ReportCatInfo(SC_UNSPECIFIED) {}
-
-    bool
-    checkLimit(sc_actions &actions)
-    {
-        if (limit == 0 || count < limit)
-            return false;
-        if (limit != -1)
-            actions |= SC_STOP;
-        return true;
-    }
-
-    sc_actions actions;
-    int count;
-    int limit;
-};
-
-const char *severityNames[] = {
-    [SC_INFO] = "Info",
-    [SC_WARNING] = "Warning",
-    [SC_ERROR] = "Error",
-    [SC_FATAL] = "Fatal"
-};
-
-ReportCatInfo catForSeverity[SC_MAX_SEVERITY] =
-{
-    [SC_INFO] = ReportCatInfo(SC_DEFAULT_INFO_ACTIONS),
-    [SC_WARNING] = ReportCatInfo(SC_DEFAULT_WARNING_ACTIONS),
-    [SC_ERROR] = ReportCatInfo(SC_DEFAULT_ERROR_ACTIONS),
-    [SC_FATAL] = ReportCatInfo(SC_DEFAULT_FATAL_ACTIONS)
-};
-
-std::map<std::string, ReportCatInfo> catForMsgType;
-std::map<std::pair<std::string, sc_severity>, ReportCatInfo>
-    catForSeverityAndMsgType;
-
-int verbosityLevel = SC_MEDIUM;
-
-sc_actions suppressedActions = SC_UNSPECIFIED;
-sc_actions forcedActions = SC_UNSPECIFIED;
-sc_actions catchActions = SC_DISPLAY;
-
-sc_report_handler_proc reportHandlerProc = &sc_report_handler::default_handler;
-
-sc_actions maxAction = SC_ABORT;
-
-std::unique_ptr<sc_report> globalReportCache;
-
 } // anonymous namespace
 
 void
@@ -114,31 +62,29 @@
                           const char *msg, int verbosity, const char *file,
                           int line)
 {
-    if (severity == SC_INFO && verbosity > verbosityLevel)
+    if (severity == SC_INFO && verbosity > sc_gem5::reportVerbosityLevel)
         return;
 
-    ReportCatInfo &sevInfo = catForSeverity[severity];
-    ReportCatInfo &msgInfo = catForMsgType[msg_type];
-    ReportCatInfo &sevMsgInfo = catForSeverityAndMsgType[
-        std::make_pair(std::string(msg_type), severity)];
+    sc_gem5::ReportSevInfo &sevInfo = sc_gem5::reportSevInfos[severity];
+    sc_gem5::ReportMsgInfo &msgInfo = sc_gem5::reportMsgInfoMap[msg_type];
 
     sevInfo.count++;
     msgInfo.count++;
-    sevMsgInfo.count++;
+    msgInfo.sevCounts[severity]++;
 
     sc_actions actions = SC_UNSPECIFIED;
-    if (sevMsgInfo.actions != SC_UNSPECIFIED)
-        actions = sevMsgInfo.actions;
+    if (msgInfo.sevActions[severity] != SC_UNSPECIFIED)
+        actions = msgInfo.sevActions[severity];
     else if (msgInfo.actions != SC_UNSPECIFIED)
         actions = msgInfo.actions;
     else if (sevInfo.actions != SC_UNSPECIFIED)
         actions = sevInfo.actions;
 
-    actions &= ~suppressedActions;
-    actions |= forcedActions;
+    actions &= ~sc_gem5::reportSuppressedActions;
+    actions |= sc_gem5::reportForcedActions;
 
-    if (sevMsgInfo.checkLimit(actions) && msgInfo.checkLimit(actions))
-        sevInfo.checkLimit(actions);
+    msgInfo.checkLimits(severity, actions);
+    sevInfo.checkLimit(actions);
 
     ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
     sc_report report(severity, msg_type, msg, verbosity, file, line,
@@ -149,12 +95,12 @@
         if (current) {
             current->lastReport(&report);
         } else {
-            globalReportCache =
+            sc_gem5::globalReportCache =
                 std::unique_ptr<sc_report>(new sc_report(report));
         }
     }
 
-    reportHandlerProc(report, actions);
+    sc_gem5::reportHandlerProc(report, actions);
 }
 
 void
@@ -168,7 +114,7 @@
 sc_actions
 sc_report_handler::set_actions(sc_severity severity, sc_actions actions)
 {
-    ReportCatInfo &info = catForSeverity[severity];
+    sc_gem5::ReportSevInfo &info = sc_gem5::reportSevInfos[severity];
     sc_actions previous = info.actions;
     info.actions = actions;
     return previous;
@@ -177,7 +123,7 @@
 sc_actions
 sc_report_handler::set_actions(const char *msg_type, sc_actions actions)
 {
-    ReportCatInfo &info = catForMsgType[msg_type];
+    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
     sc_actions previous = info.actions;
     info.actions = actions;
     return previous;
@@ -187,17 +133,16 @@
 sc_report_handler::set_actions(
         const char *msg_type, sc_severity severity, sc_actions actions)
 {
-    ReportCatInfo &info = catForSeverityAndMsgType[
-        std::make_pair(std::string(msg_type), severity)];
-    sc_actions previous = info.actions;
-    info.actions = actions;
+    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
+    sc_actions previous = info.sevActions[severity];
+    info.sevActions[severity] = actions;
     return previous;
 }
 
 int
 sc_report_handler::stop_after(sc_severity severity, int limit)
 {
-    ReportCatInfo &info = catForSeverity[severity];
+    sc_gem5::ReportSevInfo &info = sc_gem5::reportSevInfos[severity];
     int previous = info.limit;
     info.limit = limit;
     return previous;
@@ -206,7 +151,7 @@
 int
 sc_report_handler::stop_after(const char *msg_type, int limit)
 {
-    ReportCatInfo &info = catForMsgType[msg_type];
+    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
     int previous = info.limit;
     info.limit = limit;
     return previous;
@@ -216,52 +161,50 @@
 sc_report_handler::stop_after(
         const char *msg_type, sc_severity severity, int limit)
 {
-    ReportCatInfo &info = catForSeverityAndMsgType[
-        std::make_pair(std::string(msg_type), severity)];
-    int previous = info.limit;
-    info.limit = limit;
+    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
+    int previous = info.sevLimits[severity];
+    info.sevLimits[severity] = limit;
     return previous;
 }
 
 int
 sc_report_handler::get_count(sc_severity severity)
 {
-    return catForSeverity[severity].count;
+    return sc_gem5::reportSevInfos[severity].count;
 }
 
 int
 sc_report_handler::get_count(const char *msg_type)
 {
-    return catForMsgType[msg_type].count;
+    return sc_gem5::reportMsgInfoMap[msg_type].count;
 }
 
 int
 sc_report_handler::get_count(const char *msg_type, sc_severity severity)
 {
-    return catForSeverityAndMsgType[
-        std::make_pair(std::string(msg_type), severity)].count;
+    return sc_gem5::reportMsgInfoMap[msg_type].sevCounts[severity];
 }
 
 int
 sc_report_handler::set_verbosity_level(int vl)
 {
-    int previous = verbosityLevel;
-    verbosityLevel = vl;
+    int previous = sc_gem5::reportVerbosityLevel;
+    sc_gem5::reportVerbosityLevel = vl;
     return previous;
 }
 
 int
 sc_report_handler::get_verbosity_level()
 {
-    return verbosityLevel;
+    return sc_gem5::reportVerbosityLevel;
 }
 
 
 sc_actions
 sc_report_handler::suppress(sc_actions actions)
 {
-    sc_actions previous = suppressedActions;
-    suppressedActions = actions;
+    sc_actions previous = sc_gem5::reportSuppressedActions;
+    sc_gem5::reportSuppressedActions = actions;
     return previous;
 }
 
@@ -274,8 +217,8 @@
 sc_actions
 sc_report_handler::force(sc_actions actions)
 {
-    sc_actions previous = forcedActions;
-    forcedActions = actions;
+    sc_actions previous = sc_gem5::reportForcedActions;
+    sc_gem5::reportForcedActions = actions;
     return previous;
 }
 
@@ -289,22 +232,22 @@
 sc_actions
 sc_report_handler::set_catch_actions(sc_actions actions)
 {
-    sc_actions previous = catchActions;
-    catchActions = actions;
+    sc_actions previous = sc_gem5::reportCatchActions;
+    sc_gem5::reportCatchActions = actions;
     return previous;
 }
 
 sc_actions
 sc_report_handler::get_catch_actions()
 {
-    return catchActions;
+    return sc_gem5::reportCatchActions;
 }
 
 
 void
 sc_report_handler::set_handler(sc_report_handler_proc proc)
 {
-    reportHandlerProc = proc;
+    sc_gem5::reportHandlerProc = proc;
 }
 
 void
@@ -337,23 +280,18 @@
 sc_actions
 sc_report_handler::get_new_action_id()
 {
+    static sc_actions maxAction = SC_ABORT;
     maxAction = maxAction << 1;
     return maxAction;
 }
 
-sc_report_handler_proc
-sc_report_handler::get_handler()
-{
-    return reportHandlerProc;
-}
-
 sc_report *
 sc_report_handler::get_cached_report()
 {
     ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
     if (current)
         return current->lastReport();
-    return globalReportCache.get();
+    return ::sc_gem5::globalReportCache.get();
 }
 
 void
@@ -363,7 +301,7 @@
     if (current) {
         current->lastReport(nullptr);
     } else {
-        globalReportCache = nullptr;
+        ::sc_gem5::globalReportCache = nullptr;
     }
 }
 
@@ -409,7 +347,7 @@
 {
     std::ostringstream str;
 
-    const char *sevName = severityNames[report.get_severity()];
+    const char *sevName = sc_gem5::reportSeverityNames[report.get_severity()];
     int id = report.get_id();
 
     str << sevName << ": ";