summaryrefslogtreecommitdiff
path: root/sim
diff options
context:
space:
mode:
authorSteve Reinhardt <stever@eecs.umich.edu>2003-10-28 12:55:12 -0800
committerSteve Reinhardt <stever@eecs.umich.edu>2003-10-28 12:55:12 -0800
commitb90f810575679442d78c0e9cd0c98a057ba4871b (patch)
tree8882e14d0f8cbee6f7c630ecb2408b62974a32d2 /sim
parentcec7f73abf841a65bdce38d6eb67a643e4879335 (diff)
downloadgem5-b90f810575679442d78c0e9cd0c98a057ba4871b.tar.xz
Revamp serialization to make it easier.
--HG-- extra : convert_revision : c57a538d7cf606dbdf5fa244f92da46bd830e335
Diffstat (limited to 'sim')
-rw-r--r--sim/eventq.cc8
-rw-r--r--sim/eventq.hh2
-rw-r--r--sim/serialize.cc138
-rw-r--r--sim/serialize.hh80
4 files changed, 160 insertions, 68 deletions
diff --git a/sim/eventq.cc b/sim/eventq.cc
index 6ffce4c0e..da9e85eeb 100644
--- a/sim/eventq.cc
+++ b/sim/eventq.cc
@@ -134,7 +134,7 @@ EventQueue::nameChildren()
}
void
-EventQueue::serialize()
+EventQueue::serialize(ostream &os)
{
string objects = "";
@@ -142,12 +142,12 @@ EventQueue::serialize()
while (event) {
objects += event->name();
objects += " ";
- event->serialize();
+ event->serialize(os);
event = event->next;
}
- nameOut("Serialized");
- paramOut("objects",objects);
+ nameOut(os, "Serialized");
+ SERIALIZE_MEMBER(objects);
}
void
diff --git a/sim/eventq.hh b/sim/eventq.hh
index df8e00bd8..cd86512e4 100644
--- a/sim/eventq.hh
+++ b/sim/eventq.hh
@@ -246,7 +246,7 @@ class EventQueue : public Serializeable
Tick nextEventTime() { return empty() ? curTick : head->when(); }
virtual void nameChildren();
- virtual void serialize();
+ virtual void serialize(std::ostream &os);
};
diff --git a/sim/serialize.cc b/sim/serialize.cc
index c90f1694e..936812795 100644
--- a/sim/serialize.cc
+++ b/sim/serialize.cc
@@ -34,6 +34,7 @@
#include <vector>
#include "base/misc.hh"
+#include "base/str.hh"
#include "sim/eventq.hh"
#include "sim/param.hh"
@@ -48,7 +49,7 @@ using namespace std;
Serializer *Serializeable::serializer = NULL;
Serializeable::Serializeable(const string &n)
- : proxy(this), objName(n), serialized(false)
+ : objName(n), serialized(false)
{ }
Serializeable::~Serializeable()
@@ -63,30 +64,131 @@ Serializeable::mark()
serialized = true;
}
-ostream &
-Serializeable::out() const
+void
+Serializeable::nameOut(ostream &os)
+{
+ os << "\n[" << name() << "]\n";
+}
+
+void
+Serializeable::nameOut(ostream &os, const string &_name)
+{
+ os << "\n[" << _name << "]\n";
+}
+
+template <class T> bool parseParam(const std::string &str, T &data);
+template <class T> void showParam(const std::ostream &os, T &data);
+
+template <class T>
+void
+paramOut(ostream &os, const std::string &name, const T& param)
{
- return serializer->out();
+ os << name << "=";
+ showParam(os, param);
+ os << "\n";
}
+
+template <class T>
void
-Serializeable::nameOut()
+paramIn(IniFile &db, const std::string &section,
+ const std::string &name, T& param)
{
- out() << "\n[" << name() << "]\n";
+ std::string str;
+ if (!db.find(section, name, str) || !parseParam(str, param)) {
+ fatal("Can't unserialize '%s:%s'\n", section, name);
+ }
}
+
+template <class T>
void
-Serializeable::nameOut(const string &_name)
+arrayParamOut(ostream &os, const std::string &name,
+ const T *param, int size)
{
- out() << "\n[" << _name << "]\n";
+ os << name << "=";
+ if (size > 0)
+ showParam(os, param[0]);
+ for (int i = 1; i < size; ++i) {
+ os << " ";
+ showParam(os, param[i]);
+ }
+ os << "\n";
}
-template<> void
-Serializeable::paramOut(const string &name, const uint64_t& param)
+
+template <class T>
+void
+arrayParamIn(IniFile &db, const std::string &section,
+ const std::string &name, T *param, int size)
{
- out() << name << "=0x" << hex << param << dec << "\n";
+ std::string str;
+ if (!db.find(section, name, str)) {
+ fatal("Can't unserialize '%s:%s'\n", section, name);
+ }
+
+ // code below stolen from VectorParam<T>::parse().
+ // it would be nice to unify these somehow...
+
+ vector<string> tokens;
+
+ tokenize(tokens, str, ' ');
+
+ // Need this if we were doing a vector
+ // value.resize(tokens.size());
+
+ if (tokens.size() != size) {
+ fatal("Array size mismatch on %s:%s'\n", section, name);
+ }
+
+ for (int i = 0; i < tokens.size(); i++) {
+ // need to parse into local variable to handle vector<bool>,
+ // for which operator[] returns a special reference class
+ // that's not the same as 'bool&', (since it's a packed
+ // vector)
+ T scalar_value;
+ if (!parseParam(tokens[i], scalar_value)) {
+ string err("could not parse \"");
+
+ err += str;
+ err += "\"";
+
+ fatal(err);
+ }
+
+ // assign parsed value to vector
+ param[i] = scalar_value;
+ }
}
+
+#define INSTANTIATE_PARAM_TEMPLATES(type) \
+template void \
+paramOut(ostream &os, const std::string &name, const type &param); \
+template void \
+paramIn(IniFile &db, const std::string &section, \
+ const std::string &name, type & param); \
+template void \
+arrayParamOut(ostream &os, const std::string &name, \
+ const type *param, int size); \
+template void \
+arrayParamIn(IniFile &db, const std::string &section, \
+ const std::string &name, type *param, int size);
+
+
+INSTANTIATE_PARAM_TEMPLATES(int8_t)
+INSTANTIATE_PARAM_TEMPLATES(uint8_t)
+INSTANTIATE_PARAM_TEMPLATES(int16_t)
+INSTANTIATE_PARAM_TEMPLATES(uint16_t)
+INSTANTIATE_PARAM_TEMPLATES(int32_t)
+INSTANTIATE_PARAM_TEMPLATES(uint32_t)
+INSTANTIATE_PARAM_TEMPLATES(int64_t)
+INSTANTIATE_PARAM_TEMPLATES(uint64_t)
+INSTANTIATE_PARAM_TEMPLATES(string)
+
+
+#if 0
+// unneeded?
void
Serializeable::childOut(const string &name, Serializeable *child)
{
@@ -96,6 +198,7 @@ Serializeable::childOut(const string &name, Serializeable *child)
out() << name << "=" << child->name() << "\n";
}
+#endif
void
Serializeable::setName(const string &name)
@@ -174,11 +277,12 @@ Serializer::serialize(const string &f)
add_objects();
while (!objects.empty()) {
- Serializeable *serial = objects.front();
- DPRINTF(Serialize, "Serializing %s\n", serial->name());
- serial->serialize();
+ Serializeable *obj = objects.front();
+ DPRINTF(Serialize, "Serializing %s\n", obj->name());
+ obj->nameOut(out());
+ obj->serialize(out());
objects.pop_front();
- list.push_back(serial);
+ list.push_back(obj);
}
while (!list.empty()) {
@@ -203,7 +307,7 @@ class SerializeEvent : public Event
~SerializeEvent();
virtual void process();
- virtual void serialize();
+ virtual void serialize(std::ostream &os);
};
SerializeEvent::SerializeEvent(EventQueue *q, Tick when, const string &f)
@@ -226,7 +330,7 @@ SerializeEvent::process()
}
void
-SerializeEvent::serialize()
+SerializeEvent::serialize(ostream &os)
{
panic("Cannot serialize the SerializeEvent");
}
diff --git a/sim/serialize.hh b/sim/serialize.hh
index ffcbbcdc2..5ebbfaba5 100644
--- a/sim/serialize.hh
+++ b/sim/serialize.hh
@@ -36,44 +36,52 @@
#include <list>
#include <iostream>
+#include <map>
#include "sim/host.hh"
#include "sim/configfile.hh"
class IniFile;
+template <class T>
+void paramOut(std::ostream &os, const std::string &name, const T& param);
+
+template <class T>
+void paramIn(IniFile &db, const std::string &section,
+ const std::string &name, T& param);
+
+template <class T>
+void arrayParamOut(std::ostream &os, const std::string &name,
+ const T *param, int size);
+
+template <class T>
+void arrayParamIn(IniFile &db, const std::string &section,
+ const std::string &name, T *param, int size);
+
+//
+// These macros are streamlined to use in serialize/unserialize
+// functions. It's assumed that serialize() has a parameter 'os' for
+// the ostream, and unserialize() has parameters 'db' and 'section'.
+#define SERIALIZE_MEMBER(member) paramOut(os, #member, member)
+
+#define UNSERIALIZE_MEMBER(member) paramIn(db, section, #member, member)
+
+#define SERIALIZE_ARRAY(member, size) \
+ arrayParamOut(os, #member, member, size)
+
+#define UNSERIALIZE_ARRAY(member, size) \
+ arrayParamIn(db, section, #member, member, size)
+
/*
* Basic support for object serialization.
*/
class Serializeable
{
public:
- // To allow other classes to do some of the serialization work.
- class Proxy {
- private:
- Serializeable *obj;
-
- // Make it so only Serializables can construct one of these.
- Proxy(Serializeable *o) : obj(o) {};
-
- friend class Serializeable;
-
- public:
- template <class T>
- void paramOut(const std::string &name, const T& param) const {
- obj->paramOut(name, param);
- };
- };
friend class Serializer;
- friend class Proxy;
-
- private:
- Proxy proxy;
protected:
- const Proxy &getProxy() { return(proxy); };
-
// object name: should be unique
std::string objName;
@@ -81,13 +89,8 @@ class Serializeable
static Serializer *serializer;
void mark();
- void nameOut();
- void nameOut(const std::string &_name);
- void childOut(const std::string &name, Serializeable *child);
- template <class T>
- void paramOut(const std::string &name, const T& param);
-
- std::ostream &out() const;
+ void nameOut(std::ostream& os);
+ void nameOut(std::ostream& os, const std::string &_name);
public:
Serializeable(const std::string &n);
@@ -99,12 +102,8 @@ class Serializeable
const std::string &name() const { return objName; }
virtual void nameChildren() {}
- virtual void serialize() {}
- virtual void unserialize(IniFile &db, const std::string &category,
- ConfigNode *node = NULL)
- {
- std::cout << name() << " is being unserialized" << std::endl;
- }
+ virtual void serialize(std::ostream& os) {}
+ virtual void unserialize(IniFile &db, const std::string &section) {}
};
class Serializer
@@ -131,17 +130,6 @@ class Serializer
const std::string &filename() const { return file; }
};
-template <class T>
-inline void
-Serializeable::paramOut(const std::string &name, const T& param)
-{
- out() << name << "=" << param << "\n";
-}
-
-template <> void
-Serializeable::paramOut(const std::string &name, const uint64_t& param);
-
-
//
// A SerializeableBuilder serves as an evaluation context for a set of
// parameters that describe a specific instance of a Serializeable. This