Gabe Black has submitted this change and it was merged. ( https://gem5-review.googlesource.com/c/public/gem5/+/13191 )

Change subject: systemc: Add a non-standard sc_time constructor and from_string.
......................................................................

systemc: Add a non-standard sc_time constructor and from_string.

The sc_time constructor was being called, but because of implicit type
conversions, a const char * was being treated as a bool and totally
unrelated constructor was being called.

This change adds and implements the missing but non-standard
constructor. It also implements the from_string function which uses
that constructor.

Change-Id: I21e7e40fd1a8d1c579b1abdc2036d016501f510c
Reviewed-on: https://gem5-review.googlesource.com/c/13191
Reviewed-by: Gabe Black <[email protected]>
Maintainer: Gabe Black <[email protected]>
---
M src/systemc/core/sc_time.cc
M src/systemc/core/time.cc
M src/systemc/core/time.hh
M src/systemc/ext/core/sc_time.hh
4 files changed, 45 insertions(+), 2 deletions(-)

Approvals:
  Gabe Black: Looks good to me, approved; Looks good to me, approved



diff --git a/src/systemc/core/sc_time.cc b/src/systemc/core/sc_time.cc
index acc3c08..88058a7 100644
--- a/src/systemc/core/sc_time.cc
+++ b/src/systemc/core/sc_time.cc
@@ -153,6 +153,25 @@
     val = t.val;
 }

+sc_time::sc_time(double d, const char *unit)
+{
+    sc_time_unit tu;
+    for (tu = SC_FS; tu <= SC_SEC; tu = (sc_time_unit)(tu + 1)) {
+        if (strcmp(unit, sc_gem5::TimeUnitNames[tu]) == 0 ||
+            strcmp(unit, sc_gem5::TimeUnitConstantNames[tu]) == 0) {
+            break;
+        }
+    }
+
+    if (tu > SC_SEC) {
+        SC_REPORT_ERROR("(E567) sc_time conversion failed",
+                "invalid unit given");
+        val = 0;
+        return;
+    }
+    set(this, d, tu);
+}
+
 sc_time::sc_time(double d, bool scale)
 {
     double scaler = scale ? defaultUnit : SimClock::Float::Hz;
@@ -288,8 +307,19 @@
 sc_time
 sc_time::from_string(const char *str)
 {
-    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
-    return sc_time();
+    char *end = nullptr;
+
+    double d = str ? std::strtod(str, &end) : 0.0;
+    if (str == end || d < 0.0) {
+        SC_REPORT_ERROR("(E567) sc_time conversion failed",
+                "invalid value given");
+        return SC_ZERO_TIME;
+    }
+
+    while (*end && std::isspace(*end))
+        end++;
+
+    return sc_time(d, end);
 }

 const sc_time
diff --git a/src/systemc/core/time.cc b/src/systemc/core/time.cc
index 4c90864..587d8cb 100644
--- a/src/systemc/core/time.cc
+++ b/src/systemc/core/time.cc
@@ -43,6 +43,15 @@
     [::sc_core::SC_SEC] = "s"
 };

+const char *TimeUnitConstantNames[] = {
+    [::sc_core::SC_FS] = "SC_FS",
+    [::sc_core::SC_PS] = "SC_PS",
+    [::sc_core::SC_NS] = "SC_NS",
+    [::sc_core::SC_US] = "SC_US",
+    [::sc_core::SC_MS] = "SC_MS",
+    [::sc_core::SC_SEC] = "SC_SEC"
+};
+
 double TimeUnitScale[] = {
     [::sc_core::SC_FS] = 1.0e-15,
     [::sc_core::SC_PS] = 1.0e-12,
diff --git a/src/systemc/core/time.hh b/src/systemc/core/time.hh
index 4357fbd..dca965e 100644
--- a/src/systemc/core/time.hh
+++ b/src/systemc/core/time.hh
@@ -36,6 +36,7 @@
 {

 extern const char *TimeUnitNames[];
+extern const char *TimeUnitConstantNames[];
 extern double TimeUnitScale[];
 extern Tick TimeUnitFrequency[];

diff --git a/src/systemc/ext/core/sc_time.hh b/src/systemc/ext/core/sc_time.hh
index 0a31a65..d2b1e98 100644
--- a/src/systemc/ext/core/sc_time.hh
+++ b/src/systemc/ext/core/sc_time.hh
@@ -55,6 +55,9 @@
     sc_time(double, sc_time_unit);
     sc_time(const sc_time &);

+    // Nonstandard
+    sc_time(double, const char *);
+
     // Deprecated
     sc_time(double, bool);
     sc_time(sc_dt::uint64, bool);

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/13191
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: I21e7e40fd1a8d1c579b1abdc2036d016501f510c
Gerrit-Change-Number: 13191
Gerrit-PatchSet: 5
Gerrit-Owner: Gabe Black <[email protected]>
Gerrit-Reviewer: Andreas Sandberg <[email protected]>
Gerrit-Reviewer: Gabe Black <[email protected]>
Gerrit-Reviewer: Giacomo Travaglini <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Matthias Jung <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to