]> git.localhorst.tv Git - l2e.git/blob - src/loader/ObjectFile.h
new object file format in compiler
[l2e.git] / src / loader / ObjectFile.h
1 #ifndef LOADER_OBJECTFILE_H_
2 #define LOADER_OBJECTFILE_H_
3
4 #include <SDL.h>
5
6 namespace loader {
7
8 const unsigned int FORMAT_ID = 2;
9
10 struct Export {
11         /// Offset of the identifier in the file.
12         unsigned int nameOffset;
13         /// Type ID of referenced object.
14         unsigned int typeId;
15         /// File-offset of the object's actual data.
16         unsigned int dataOffset;
17 };
18
19 struct External {
20         /// File-relative offset of the referenced object's
21         /// identifier.
22         unsigned int nameOffset;
23         /// Target position for linking/inlining.
24         unsigned int referenceOffset;
25         /// Nonzero if the object should be copied rather that
26         /// just writing a reference.
27         unsigned int inlined;
28 };
29
30 struct Object {
31         unsigned int typeId;
32         unsigned int size;
33         char *RawObject();
34         Object *Next();
35 };
36
37 struct Array {
38         unsigned int size;
39         bool ref;
40         char *Data();
41         Array *Next();
42 };
43
44 struct ObjectFileHeader {
45         /// Has to be "L2E\n"
46         char ident[4];
47
48         /// Version ID of the object file format
49         /// For now it must match FORMAT_ID for the loader to be
50         /// able to read it.
51         /// Backwards compatibility might be implemented at some
52         /// point in the future, but don't bet on that ever
53         /// happening.
54         unsigned int versionId;
55
56         /// File-relative offsets of the export section's begin
57         /// and end respectively.
58         /// Exports are named and typed addresses within the
59         /// file. This is essentially an array of Export structs.
60         unsigned int exportsBegin;
61         unsigned int exportsEnd;
62
63         /// File-relative offsets of the externals section's
64         /// begin and end respectively.
65         /// Each external names an entity which must be linked in
66         /// for this object file to function properly. This is
67         /// essentially an array of External structs.
68         unsigned int externalsBegin;
69         unsigned int externalsEnd;
70
71         /// File-relative offsets of the objet section's begin
72         /// and end respectively.
73         /// Each object begins with its type ID followed by its
74         /// size and finally the raw object data.
75         /// All referecte type fields should contain either a
76         /// file-relative offset or zero to indicate a null
77         /// reference.
78         /// This can be sen as a linked list where the next
79         /// object for a node can be obtained by adding the size
80         /// of to int and the object to the current node's
81         /// address.
82         unsigned int objectsBegin;
83         unsigned int objectsEnd;
84
85         /// File-relative offsets of the array section's begin
86         /// and end respectively.
87         /// Each array consists of an unsigned integer indicating
88         /// its size followed by a boolean flag which is true if
89         /// the arrays consists of pointers followed by the data.
90         unsigned int arraysBegin;
91         unsigned int arraysEnd;
92
93         ObjectFileHeader();
94
95         /// Check if there are any problems with the file header.
96         /// Throws a std::runtime_error on failure.
97         void IntegrityCheck(unsigned int fileSize) const;
98         Export *ExportsBegin();
99         Export *ExportsEnd();
100         External *ExternalsBegin();
101         External *ExternalsEnd();
102         Object *ObjectsBegin();
103         Object *ObjectsEnd();
104         Array *ArraysBegin();
105         Array *ArraysEnd();
106 };
107
108 struct LoadedExport {
109         char *location;
110         int typeId;
111 };
112
113 }
114
115 #endif