2 \brief Intermediate Representation (IR) between Directory Structure and Engine Grammar
3 \details The IR serves as a storage structure that is populated during the
4 parsing of the input directory structure. After parsing is complete,
5 the IR will be condensed (removed of excess allocated space) and then
6 output as the Engine Grammar. In this file we describe the semantic actions
7 that are called at each step, and the memory buffers that they populate.
8 See parser.y for the description on how the input grammar is constructed,
9 and where/when semantic actions are called.
10 TODO: or just write it here.
11 \author Jordan Lavatai
13 ----------------------------------------------------------------------------*/
17 //#include <apc/mem.h>TODO:
23 #define MAX_MODELS 256
25 #define MAX_CLASS_DEPTH 256
26 #define MAX_CLASSES 256
27 #define MAX_FRAMES 256
28 /* All bufs are of pointers to their respective structs. When a buf is full */
29 /* (number of data structs pointers >= max number of data struct pointers), */
30 /* we need to allocate a more pointers for that buf. Allocate these */
31 /* pointers a page at a time (1024 = Page bytes (4096)/bytes per pointer(4)) */
32 /* TODO: Account for different page sizes in different system */
33 #define PTRS_IN_PAGE 1024
35 /* General: All information from the directory structure is stored in */
36 /* five buffers that comprise the IR: cdat_buf, odat_buf, vdat_buf, ref_buf */
37 /* and link_buf. Each buf corresponds to the data structure that it stores. */
38 /* The storage techique for all bufs (except cdat) is the same. Each bufs member first */
39 /* populates its struct and then allocates the space for the next member */
40 /* and increments the buf index. This means that we have to allocate the */
41 /* very first member of each buf at ir_init(), so that we don't segfault */
42 /* as the first member attempts to access memory that its previous member */
43 /* didn't allocate (because it doesnt exist). We access the buf members */
44 /* through standard array indexing but conceal the tediousness of array */
45 /* indexing with macros. E.g. without macros, acessing an elements name */
46 /* member would look like (split up to not go over line char limit): */
47 /* (*cdat_stackp)->set_list[(*cdat_stackp)->num_sets] */
48 /* .ele_list[(*cdat_stackp)->set_list[(*cdat_stackp->num_sets)].num_ele].name */
50 /* For cdats in cdat_buf, we allocate the memory for a cdat once a cdat
51 is recognized in the grammar. Cdat_buf is different from the other bufs
52 because cdats have a root cdat that all cdats are a subclass of. This root
53 cdat can have a set_list like other cdats. */
58 /* Elements: Ele stands for element and has two representations in the IR. */
59 /* In the cdat_buf eles store their name, cdat_idx (their classes index in */
60 /* the cdat_buf) and the ref_id (refer to ref ). In the odat_buf, eles store */
61 /* their object data (odat). At output time, the ref_id is dereferenced to */
62 /* determine the elements odat which is the data that the engine expects */
63 /* from an element. */
71 /* Sets: The set is similar to the ele, but it contains a list of its */
72 /* elements. The set is populated at parse time AFTER the elements are */
73 /* populated, due to the nature of bottom up parsing. */
80 struct ele ele_list
[MAX_ELES
];
83 /* Cdats: A cdat is a class data structure. Cdats serve as the central */
84 /* data types of the IR. At output, the cdat_buf is iterated through and */
85 /* each is written to the output file. For each cdat, sets and element */
86 /* ref_ids must be dereferenced to determine the odat information. Cdats */
87 /* contain pointers to their subclasses so that the relationship between */
88 /* classes can be determined, but the subclasses are not represented inside */
89 /* of the cdat itself but rather in the subsequent cdats in cdat_buf. We */
90 /* can determine the number of subclasses (the last index into cdat_buf */
91 /* that represents a subclass of some arbitrary cdat) each cdat has by */
92 /* incrementing num_classes during parse time. */
93 /* TODO: Should classes point to their parent class? */
100 struct cdat
* class_list
[MAX_CLASSES
];
101 struct set set_list
[MAX_SETS
];
104 /* There are an unknown amount of cdats at compile time, so we maintain */
105 /* a cdat_buf of cdat pointers that can be expanded as needed. */
106 struct cdat
* cdat_buf
[PTRS_IN_PAGE
];
108 /* The cdat_stack is a stack pointers to cdat pointers, the top of which is
109 the cdat that is currently being parsed. Whenever a new cdat is recognized
110 by the grammar (CLOPEN), a cdat is pushed onto the cdat_stack, and we refer
111 to this cdat through the macro CURR_CDAT. By keeping a cdat_stack, we have
112 access to the current cdat so that the elements and sets can populate themselves
113 in the cdat accordingly. */
115 struct cdat
* cdat_stack
[PTRS_IN_PAGE
];
116 struct cdat
** cdat_stackp
;
118 /* Refs: Each set/ele has a reference to its object data (odat) through a ref_id.
119 Ref_ids are unsigned 64 byte integers that map to the hex values RGBA. During
120 the construction of the directory structure, users can choose a RGBA value for
121 each object that any other object can refer to via links (see link). If a user
122 does not choose an RGBA value, then the object is given one from the system space.
123 We maintain a doubly linked list of refs in the ref_buf at parse time so that
124 links can be resolved after the parsing of the directory structure is complete.
125 For every 16th ref, we create a post so that we can reduce on the search time for
133 uint64_t ref_id
; //0xFFFFFF->digit
136 /* Like the cdat_buf, ref_buf stores pointers to refs and can
138 struct ref
* ref_buf
[PTRS_IN_PAGE
];
140 /* posts for ref_buf */
141 struct ref
* post_buf
[PTRS_IN_PAGE
];
143 /* Links: At parse time, a set/ele can include a link in their
144 grammar representation instead of the actual data and this signifies
145 to the APC that that set/ele wishes to use the data of another
146 set/ele, either its video data (vdat) or object data (odat). The link
147 itself contains the type of link it is, the ref_id OR name, and
148 which set/ele created the link. During parse time, links can be made
149 to o/vdats that have yet to be parsed. In order to accomodate for this,
150 we resolve all links AFTER parse time by iterating through the link_buf,
151 finding the ref_id that was stored for some object (if the ref_id exists),
152 and creating a relative pointer from the original object to the data that
155 /* Svlinks stand for short vlink, which is a link to a vdat
156 TODO: diff btwn vlink*/
162 /* A vlink is what it sounds like, a link to a vdat
169 /* Olinks are links to odats */
177 struct svlink svlink
;
181 int type
; //1 = olink, 2 = vlink, 3 = svlink
187 /* link_buf contains all the links that
188 we encountered during parse time that need
189 to be resolved to an offset at output time.
190 This does not include quad refs, because
191 those are already known to need to be resolved */
192 struct link
* link_buf
[PTRS_IN_PAGE
];
195 /* Odats: Odats consist of the object data necessary for
196 each object. Odats are sometimes referred to as archetypes
197 at compile-time, in order to distinguish the difference from
198 a runtime object and a compile-time object.
199 TODO: Need more info about objects at runtime, to described
200 the reasoning behind odat structure at compile-time*/
202 /* Each set has a quad_list or a list of quads. The quad_list
206 uint64_t ref_id
; //rgba
219 struct ref
* refp
; /* pointer to it's ref on ref_list */
221 struct quad quad_list
[MAX_QUADS
];
224 /* Populated and allocated same way as other bufs */
225 struct odat
* odat_buf
[PTRS_IN_PAGE
];
227 /* A framesheet is a grouping of animation frames in
228 a single direction (N,W,S,E) */
233 void* frames
[MAX_FRAMES
];
236 /* A model is a collection of framesheets for every
237 direction (N,W,S,E,NW,NE,SW,SE)*/
238 /* NAMED spritesheet */
241 struct framesheet spritesheet
[8]; //one for each
244 /* Vdat: Vdats are the video data of each object. They can not be
245 created as a stand alone object (because they consist solely
246 of animation information and not the skeleton on which the
247 animation manipulates). Vdats have a list of models for every
248 animation that the vdats odat can do for that vdat*/
250 struct odat
* creator
; //pointer to odat that made this vdat
252 struct model model_list
[MAX_MODELS
];
256 struct vdat
* vdat_buf
[PTRS_IN_PAGE
];
258 /* The initalization function of the IR. Mallocs the
259 first c/v/odat and the first links and refs and
260 inits the cdat_stack */
264 /* mallocs memory for a new cdat. If the cdat_buf
265 is full, mallocs another 1024 cdat pointers. */
269 /* Called after the cdat open operator has been recognized in grammar. Allocates
270 the space for a cdat on the cdat_buf, pushes that pointer onto
275 /* Called after a cdat end operator has been recognized in grammar. Sets
276 top stack cdat ** to null and decrements stack pointer */
280 /* Called after an odat has been populated. Allocates memory for
285 /* Called after an vdat has been populated. Allocates memory for
296 /* Called in the reduction of a set. While both odats (eles and sets)
297 have identical label terminals, we are unable to give a single grammatical rule
298 for both due to how we allocate odats in the odat buf. Due to the
299 nature of bottom up parsing, all the elements will be inserted into the
300 odat_buf first, and then the set that contains these element is inserted. Since
301 the sets label comes before the element list in the grammar, we would be giving an element
302 a set label in its respective odat, which would then be replaced by the
303 elements label. Instead, we store the label in the sets representation inside
304 CURR_CDAT and after we are done parsing the element_list and know that the CURR_ODAT
305 is the set, we populate the sets label members in CURR_ODAT with the values we stored
306 previously in CURR_CDAT. */
308 insert_set_label(char*, uint64_t);
310 /* Populate the sets representation in CURR_CDAT with a ref_id and insert a link
311 into the link_buf that will resolve the ref_id to an actual odat after parse time. */
313 insert_set_olink(uint64_t);
315 /* Put the vlink in the link_buf to be processed after parsetime */
317 insert_set_vlink(uint64_t, char*);
319 /* Put svlink in the link_buf to be processed after parsetime */
321 insert_set_svlink(uint64_t);
323 /* Called for every set reduction except for sets with olinks. Populates the
324 set data structures in the CDAT and in the ODAT. Uses the name and ref_id
325 from insert_set_label. Also inserts a ref into the ref_buf with the CURR_ODAT
326 pointer so that we can also resolve the odat from its ref_id. */
330 /* Insertion of eles is practically equivalent to how sets are inserted because both
331 share the same data type (ODAT). Like sets, eles have links, labels
332 and odats. Eles have the added notion of a parent set, and so must be inserted
333 into said parent set, but this is the only place they truly differ from sets. */
336 insert_ele_label(char*, uint64_t);
339 insert_ele_olink(uint64_t);
342 insert_ele_vlink(uint64_t, char*);
345 insert_ele_svlink(uint64_t);
350 /* Created as a seperate function, instead of setting the ODATS vdat_id and
351 calling inc_vdat() inside of insert_set(), to account for the set reduction
352 where a vdat is not created (o/v/svlinks). Because insert_set/ele is always
353 called before insert_vdat, and thus increments the CURR_ODAT to be the next
354 ODAT to be populated, insert_vdat() targets the last ODAT that was populated,
359 /* Inserts the hitbox into the CURR_ODAT */
363 /* Inserts the root into the CURR_ODAT */
365 insert_root(int, int, int);
367 /* Inserts a quad into the CURR_ODAT */
369 insert_quad(int, int, int, uint64_t);
375 insert_framesheet(char, char*, uint64_t, int, int, int);
378 insert_frame_pointer(char, void*);