]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Interpreter.cpp
new object file format in compiler
[l2e.git] / src / loader / Interpreter.cpp
index 8b54fc177913de18a1266f885cc8e959b6eb4eef..fc428cd8365e998ea37128ef23adf2ede9dbb716 100644 (file)
@@ -68,20 +68,64 @@ const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &id
        }
 }
 
+const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
+       std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(identifier));
+       if (i != parsedDefinitions.end()) {
+               return i->second;
+       } else {
+               throw Error("access to undefined object " + identifier);
+       }
+}
+
 
-void *Interpreter::GetObject(int typeId, const std::string &name) {
-       std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+void *Interpreter::GetObject(
+               int typeId,
+               const std::string &name) {
+       std::map<string, ParsedDefinition>::const_iterator
+                       i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               const TypeDescription &requested(TypeDescription::Get(typeId));
-               const TypeDescription &actual(TypeDescription::Get(i->second.type));
+               const TypeDescription &requested =
+                               TypeDescription::Get(typeId);
+               const TypeDescription &actual =
+                               TypeDescription::Get(i->second.type);
                if (requested.TypeId() == actual.TypeId()) {
                        return values[actual.TypeId()][i->second.id];
                } else if (actual.IsSubtypeOf(requested)) {
-                       char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
-                       std::ptrdiff_t offset(actual.SupertypeOffset(requested));
+                       char *sub = reinterpret_cast<char *>(
+                                       values[actual.TypeId()][i->second.id]);
+                       std::ptrdiff_t offset =
+                                       actual.SupertypeOffset(requested);
+                       return sub - offset;
+               } else {
+                       throw Error("cannot cast " + actual.TypeName()
+                                       + " to " + requested.TypeName());
+               }
+       } else {
+               throw Error("access to undefined object " + name);
+       }
+}
+
+const void *Interpreter::GetObject(
+               int typeId,
+               const std::string &name) const {
+       std::map<string, ParsedDefinition>::const_iterator
+                       i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               const TypeDescription &requested =
+                               TypeDescription::Get(typeId);
+               const TypeDescription &actual =
+                               TypeDescription::Get(i->second.type);
+               if (requested.TypeId() == actual.TypeId()) {
+                       return values.at(actual.TypeId()).at(i->second.id);
+               } else if (actual.IsSubtypeOf(requested)) {
+                       char *sub = reinterpret_cast<char *>(
+                                       values.at(actual.TypeId()).at(i->second.id));
+                       std::ptrdiff_t offset =
+                                       actual.SupertypeOffset(requested);
                        return sub - offset;
                } else {
-                       throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
+                       throw Error("cannot cast " + actual.TypeName()
+                                       + " to " + requested.TypeName());
                }
        } else {
                throw Error("access to undefined object " + name);
@@ -287,11 +331,16 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis
                        if (fd.IsAggregate()) {
                                int arraySize(i->second->GetLiteral().ArraySize());
                                size_t memberSize = fd.IsReferenced() ? sizeof(char *) : fieldType.Size();
-                               char *aggregate = alloc.Alloc(arraySize * memberSize);
-                               char *iter = aggregate;
+                               Array array;
+                               array.size = arraySize * memberSize;
+                               array.data = alloc.Alloc(array.size);
+                               array.ref = fd.IsReferenced();
+                               arrays.push_back(array);
+                               char *iter = reinterpret_cast<char *>(array.data);
                                if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
                                        const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
-                                       for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end());
+                                       for (vector<PropertyList *>::const_iterator
+                                                       j(list.begin()), end(list.end());
                                                        j != end; ++j, iter += memberSize) {
                                                char *member;
                                                if (fd.IsReferenced()) {
@@ -329,11 +378,12 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis
                                                        *reinterpret_cast<void **>(iter)
                                                                        = GetObject(fd.TypeId(), *j);
                                                } else {
-                                                       Postpone(iter, *j, fd.TypeId(), false);
+                                                       Postpone(reinterpret_cast<char *>(array.data),
+                                                                       iter, *j, fd.TypeId(), false);
                                                }
                                        }
                                }
-                               std::memcpy(dest, &aggregate, sizeof(char *));
+                               std::memcpy(dest, &array.data, sizeof(char *));
                                dest += sizeof(char *);
                                std::memcpy(dest, &arraySize, sizeof(int));
                        } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
@@ -355,7 +405,7 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis
                                }
                        }
                } else {
-                       Postpone(object, i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate());
+                       Postpone(object, object + fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate());
                }
        }
        td.Load(object);
@@ -861,16 +911,14 @@ bool Interpreter::CanLink(const Value &v) const {
 }
 
 void Interpreter::Postpone(
+               char *object,
                char *dest,
                const std::string &identifier,
                int type,
                bool inlined,
                bool aggregate) {
-       char *str(alloc.Alloc(identifier.size() + 1));
-       std::memcpy(str, identifier.c_str(), identifier.size());
-       str[identifier.size()] = '\0';
        postponedDefinitions.push_back(
-                       PostponedDefinition(dest, str, type, inlined, aggregate));
+                       PostponedDefinition(object, dest, identifier, type, inlined, aggregate));
 }