pseudoinst: get rid of mainEventQueue references.
[gem5.git] / src / sim / serialize.cc
index a01e053b93a8acb3f8d8ca6ffb249633d28d39b8..aa343d0e9c2ed7569574ad866f0fbef44d414754 100644 (file)
@@ -93,6 +93,14 @@ showParam(ostream &os, const char &value)
 }
 
 
+template <>
+void
+showParam(ostream &os, const signed char &value)
+{
+    os << (int)value;
+}
+
+
 template <>
 void
 showParam(ostream &os, const unsigned char &value)
@@ -171,35 +179,60 @@ Serializable::nameOut(ostream &os, const string &_name)
 
 template <class T>
 void
-paramOut(ostream &os, const std::string &name, const T &param)
+paramOut(ostream &os, const string &name, const T &param)
 {
     os << name << "=";
     showParam(os, param);
     os << "\n";
 }
 
+template <class T>
+void
+arrayParamOut(ostream &os, const string &name, const vector<T> &param)
+{
+    typename vector<T>::size_type size = param.size();
+    os << name << "=";
+    if (size > 0)
+        showParam(os, param[0]);
+    for (typename vector<T>::size_type i = 1; i < size; ++i) {
+        os << " ";
+        showParam(os, param[i]);
+    }
+    os << "\n";
+}
+
 
 template <class T>
 void
-paramIn(Checkpoint *cp, const std::string &section,
-        const std::string &name, T &param)
+paramIn(Checkpoint *cp, const string &section, const string &name, T &param)
 {
-    std::string str;
+    string str;
     if (!cp->find(section, name, str) || !parseParam(str, param)) {
         fatal("Can't unserialize '%s:%s'\n", section, name);
     }
 }
 
+template <class T>
+bool
+optParamIn(Checkpoint *cp, const string &section, const string &name, T &param)
+{
+    string str;
+    if (!cp->find(section, name, str) || !parseParam(str, param)) {
+        warn("optional parameter %s:%s not present\n", section, name);
+        return false;
+    } else {
+        return true;
+    }
+}
 
 template <class T>
 void
-arrayParamOut(ostream &os, const std::string &name,
-              const T *param, int size)
+arrayParamOut(ostream &os, const string &name, const T *param, unsigned size)
 {
     os << name << "=";
     if (size > 0)
         showParam(os, param[0]);
-    for (int i = 1; i < size; ++i) {
+    for (unsigned i = 1; i < size; ++i) {
         os << " ";
         showParam(os, param[i]);
     }
@@ -209,10 +242,10 @@ arrayParamOut(ostream &os, const std::string &name,
 
 template <class T>
 void
-arrayParamIn(Checkpoint *cp, const std::string &section,
-             const std::string &name, T *param, int size)
+arrayParamIn(Checkpoint *cp, const string &section, const string &name,
+             T *param, unsigned size)
 {
-    std::string str;
+    string str;
     if (!cp->find(section, name, str)) {
         fatal("Can't unserialize '%s:%s'\n", section, name);
     }
@@ -231,12 +264,12 @@ arrayParamIn(Checkpoint *cp, const std::string &section,
         fatal("Array size mismatch on %s:%s'\n", section, name);
     }
 
-    for (int i = 0; i < tokens.size(); i++) {
+    for (vector<string>::size_type 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;
+        T scalar_value = 0;
         if (!parseParam(tokens[i], scalar_value)) {
             string err("could not parse \"");
 
@@ -251,10 +284,51 @@ arrayParamIn(Checkpoint *cp, const std::string &section,
     }
 }
 
+template <class T>
+void
+arrayParamIn(Checkpoint *cp, const string &section,
+             const string &name, vector<T> &param)
+{
+    string str;
+    if (!cp->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());
+
+    param.resize(tokens.size());
+
+    for (vector<string>::size_type 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 = 0;
+        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;
+    }
+}
 
 void
-objParamIn(Checkpoint *cp, const std::string &section,
-           const std::string &name, SimObject * &param)
+objParamIn(Checkpoint *cp, const string &section,
+           const string &name, SimObject * &param)
 {
     if (!cp->findObj(section, name, param)) {
         fatal("Can't unserialize '%s:%s'\n", section, name);
@@ -262,19 +336,29 @@ objParamIn(Checkpoint *cp, const std::string &section,
 }
 
 
-#define INSTANTIATE_PARAM_TEMPLATES(type)                              \
-template void                                                          \
-paramOut(ostream &os, const std::string &name, type const &param);     \
-template void                                                          \
-paramIn(Checkpoint *cp, const std::string &section,                    \
-        const std::string &name, type & param);                                \
-template void                                                          \
-arrayParamOut(ostream &os, const std::string &name,                    \
-              type const *param, int size);                            \
-template void                                                          \
-arrayParamIn(Checkpoint *cp, const std::string &section,               \
-             const std::string &name, type *param, int size);
-
+#define INSTANTIATE_PARAM_TEMPLATES(type)                               \
+template void                                                           \
+paramOut(ostream &os, const string &name, type const &param);           \
+template void                                                           \
+paramIn(Checkpoint *cp, const string &section,                          \
+        const string &name, type & param);                              \
+template bool                                                           \
+optParamIn(Checkpoint *cp, const string &section,                       \
+        const string &name, type & param);                              \
+template void                                                           \
+arrayParamOut(ostream &os, const string &name,                          \
+              type const *param, unsigned size);                        \
+template void                                                           \
+arrayParamIn(Checkpoint *cp, const string &section,                     \
+             const string &name, type *param, unsigned size);           \
+template void                                                           \
+arrayParamOut(ostream &os, const string &name,                          \
+              const vector<type> &param);                               \
+template void                                                           \
+arrayParamIn(Checkpoint *cp, const string &section,                     \
+             const string &name, vector<type> &param);
+
+INSTANTIATE_PARAM_TEMPLATES(char)
 INSTANTIATE_PARAM_TEMPLATES(signed char)
 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
 INSTANTIATE_PARAM_TEMPLATES(signed short)
@@ -286,6 +370,8 @@ INSTANTIATE_PARAM_TEMPLATES(unsigned long)
 INSTANTIATE_PARAM_TEMPLATES(signed long long)
 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
 INSTANTIATE_PARAM_TEMPLATES(bool)
+INSTANTIATE_PARAM_TEMPLATES(float)
+INSTANTIATE_PARAM_TEMPLATES(double)
 INSTANTIATE_PARAM_TEMPLATES(string)
 
 
@@ -329,68 +415,50 @@ Globals::unserialize(Checkpoint *cp)
     mainEventQueue.unserialize(cp, "MainEventQueue");
 }
 
-void
-Serializable::serializeAll(const std::string &cpt_dir)
+Serializable::Serializable()
 {
-    setCheckpointDir(cpt_dir);
-    string dir = Checkpoint::dir();
-    if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
-            fatal("couldn't mkdir %s\n", dir);
-
-    string cpt_file = dir + Checkpoint::baseFilename;
-    ofstream outstream(cpt_file.c_str());
-    time_t t = time(NULL);
-    outstream << "// checkpoint generated: " << ctime(&t);
+}
 
-    globals.serialize(outstream);
-    SimObject::serializeAll(outstream);
+Serializable::~Serializable()
+{
 }
 
 void
-Serializable::unserializeAll(const std::string &cpt_dir)
+Serializable::serialize(ostream &os)
 {
-    setCheckpointDir(cpt_dir);
-    string dir = Checkpoint::dir();
-    string cpt_file = dir + Checkpoint::baseFilename;
-    string section = "";
-
-    DPRINTFR(Config, "Loading checkpoint dir '%s'\n",
-             dir);
-    Checkpoint *cp = new Checkpoint(dir, section);
-    unserializeGlobals(cp);
-
-    SimObject::unserializeAll(cp);
 }
 
 void
-Serializable::unserializeGlobals(Checkpoint *cp)
+Serializable::unserialize(Checkpoint *cp, const string &section)
 {
-    globals.unserialize(cp);
 }
 
-const char *Checkpoint::baseFilename = "m5.cpt";
-
-static string checkpointDirBase;
-
 void
-setCheckpointDir(const std::string &name)
+Serializable::serializeAll(const string &cpt_dir)
 {
-    checkpointDirBase = name;
-    if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
-        checkpointDirBase += "/";
+    string dir = Checkpoint::setDir(cpt_dir);
+    if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
+            fatal("couldn't mkdir %s\n", dir);
+
+    string cpt_file = dir + Checkpoint::baseFilename;
+    ofstream outstream(cpt_file.c_str());
+    time_t t = time(NULL);
+    if (!outstream.is_open())
+        fatal("Unable to open file %s for writing\n", cpt_file.c_str());
+    outstream << "## checkpoint generated: " << ctime(&t);
+
+    globals.serialize(outstream);
+    SimObject::serializeAll(outstream);
 }
 
-string
-Checkpoint::dir()
+void
+Serializable::unserializeGlobals(Checkpoint *cp)
 {
-    // use csprintf to insert curTick into directory name if it
-    // appears to have a format placeholder in it.
-    return (checkpointDirBase.find("%") != string::npos) ?
-        csprintf(checkpointDirBase, curTick) : checkpointDirBase;
+    globals.unserialize(cp);
 }
 
 void
-debug_serialize(const std::string &cpt_dir)
+debug_serialize(const string &cpt_dir)
 {
     Serializable::serializeAll(cpt_dir);
 }
@@ -406,32 +474,26 @@ debug_serialize(const std::string &cpt_dir)
 // Need to make this a pointer so we can force initialization on the
 // first reference; otherwise, some SerializableClass constructors
 // may be invoked before the classMap constructor.
-map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
+map<string, SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
 
 // SerializableClass constructor: add mapping to classMap
 SerializableClass::SerializableClass(const string &className,
-                                       CreateFunc createFunc)
+                                     CreateFunc createFunc)
 {
     if (classMap == NULL)
-        classMap = new map<string,SerializableClass::CreateFunc>();
+        classMap = new map<string, SerializableClass::CreateFunc>();
 
     if ((*classMap)[className])
-    {
-        cerr << "Error: simulation object class " << className << " redefined"
-             << endl;
-        fatal("");
-    }
+        fatal("Error: simulation object class %s redefined\n", className);
 
     // add className --> createFunc to class map
     (*classMap)[className] = createFunc;
 }
 
-
 //
 //
 Serializable *
-SerializableClass::createObject(Checkpoint *cp,
-                                 const std::string &section)
+SerializableClass::createObject(Checkpoint *cp, const string &section)
 {
     string className;
 
@@ -456,7 +518,7 @@ SerializableClass::createObject(Checkpoint *cp,
 
 
 Serializable *
-Serializable::create(Checkpoint *cp, const std::string &section)
+Serializable::create(Checkpoint *cp, const string &section)
 {
     Serializable *object = SerializableClass::createObject(cp, section);
     object->unserialize(cp, section);
@@ -464,10 +526,33 @@ Serializable::create(Checkpoint *cp, const std::string &section)
 }
 
 
-Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
-    : db(new IniFile), basePath(path), cptDir(cpt_dir)
+const char *Checkpoint::baseFilename = "m5.cpt";
+
+string Checkpoint::currentDirectory;
+
+string
+Checkpoint::setDir(const string &name)
+{
+    // use csprintf to insert curTick into directory name if it
+    // appears to have a format placeholder in it.
+    currentDirectory = (name.find("%") != string::npos) ?
+        csprintf(name, curTick) : name;
+    if (currentDirectory[currentDirectory.size() - 1] != '/')
+        currentDirectory += "/";
+    return currentDirectory;
+}
+
+string
+Checkpoint::dir()
+{
+    return currentDirectory;
+}
+
+
+Checkpoint::Checkpoint(const string &cpt_dir)
+    : db(new IniFile), cptDir(setDir(cpt_dir))
 {
-    string filename = cpt_dir + "/" + Checkpoint::baseFilename;
+    string filename = cptDir + "/" + Checkpoint::baseFilename;
     if (!db->load(filename)) {
         fatal("Can't load checkpoint file '%s'\n", filename);
     }
@@ -475,15 +560,14 @@ Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
 
 
 bool
-Checkpoint::find(const std::string &section, const std::string &entry,
-                 std::string &value)
+Checkpoint::find(const string &section, const string &entry, string &value)
 {
     return db->find(section, entry, value);
 }
 
 
 bool
-Checkpoint::findObj(const std::string &section, const std::string &entry,
+Checkpoint::findObj(const string &section, const string &entry,
                     SimObject *&value)
 {
     string path;
@@ -497,7 +581,7 @@ Checkpoint::findObj(const std::string &section, const std::string &entry,
 
 
 bool
-Checkpoint::sectionExists(const std::string &section)
+Checkpoint::sectionExists(const string &section)
 {
     return db->sectionExists(section);
 }