269eaf06b832376285d579e43db6d7f352ab41ba
[henge/webcc.git] / src / apc / ir.h
1 /*!@file
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
12 \date Aug 2016
13 ----------------------------------------------------------------------------*/
14
15
16 #include <stdint.h>
17 #include <unitypes.h>
18 #include <limits.h>
19
20 #define BUF_SIZE 256
21 #define MAX_SETS 256
22 #define MAX_ELES 256
23 #define MAX_QUADS 256
24 #define MAX_MODELS 256
25 #define MAX_VARIANTS 8
26 #define MAX_POSTS 256
27 #define MAX_CLASS_DEPTH 256
28 #define MAX_CLASSES 256
29 #define MAX_FRAMES 256
30 #define PTRS_IN_PAGE 1024
31 #define MAX_CHUNKS 256
32 #define PAGES_PER_CHUNK 16
33
34 /* Sets: elements. The set is populated at parse time AFTER the
35 elements are populated, due to the nature of bottom up parsing. */
36
37 struct set {
38 uint8_t name[32];
39 int ref_id;
40 int cdat_idx;
41 };
42
43 /* Cdats: A cdat is a class data structure. Cdats serve as the central */
44 /* data types of the IR. For each cdat, sets and element */
45 /* ref_ids must be dereferenced to determine the odat information. Cdats */
46 /* contain pointers to their subclasses so that the relationship between */
47 /* classes can be determined, but the subclasses are not represented inside */
48 /* of the cdat itself but rather in subsequent cdats in cdat_buf. We */
49 /* can determine the number of subclasses (the last index into cdat_buf */
50 /* that represents a subclass of some arbitrary cdat) each cdat has by */
51 /* incrementing num_classes during parse time. */
52 /* TODO: Should classes point to their parent class? */
53
54 struct cdat {
55 uint8_t name[32];
56 int idx;
57 int num_classes;
58 int num_sets;
59 struct cdat* class_list[MAX_CLASSES];
60 struct set set_list[MAX_SETS];
61 };
62
63 /* The cdat_stack is a stack pointers to cdat pointers, the top of which is
64 the cdat that is currently being parsed. Whenever a new cdat is recognized
65 by the grammar (CLOPEN), a cdat is pushed onto the cdat_stack, and we refer
66 to this cdat through the macro CURR_CDAT. By keeping a cdat_stack, we have
67 access to the current cdat so that the elements and sets can populate themselves
68 in the cdat accordingly. */
69
70
71 /* Refs: Each set/ele has a reference to its object data (odat) through a ref_id.
72 Ref_ids are unsigned 64 byte integers that map to the hex values RGBA. During
73 the construction of the directory structure, users can choose a RGBA value for
74 each object that any other object can refer to via links (see link). If a user
75 does not choose an RGBA value, then the object is given one from the system space.
76 We maintain a doubly linked list of refs in the ref_buf at parse time so that
77 links can be resolved after the parsing of the directory structure is complete.
78 For every 16th ref, we create a post so that we can reduce on the search time for
79 a random access. */
80
81 struct ref {
82 int type;
83 struct ref* nextref;
84 struct ref* lastref;
85 struct odat* odatp;
86 int ref_id; //0xFFFFFF->digit
87 };
88
89
90 /* Links: At parse time, a set/ele can include a link in their
91 grammar representation instead of the actual data and this signifies
92 to the APC that that set/ele wishes to use the data of another
93 set/ele, either its video data (vdat) or object data (odat). The link
94 itself contains the type of link it is, the ref_id OR name, and
95 which set/ele created the link. During parse time, links can be made
96 to o/vdats that have yet to be parsed. In order to accomodate for this,
97 we resolve all links AFTER parse time by iterating through the link_buf,
98 finding the ref_id that was stored for some object (if the ref_id exists),
99 and creating a relative pointer from the original object to the data that
100 was linked */
101
102 /* Svlinks stand for short vlink, which is a link to a vdat. svlinks
103 differ from vlinks because they do not have a name */
104
105 struct svlink {
106 int ref_id;
107 };
108
109 /* A vlink is what it sounds like, a link to a vdat */
110 struct vlink {
111 int ref_id;
112 uint8_t anim_name[32];
113 };
114
115 union link_t {
116 struct vlink vlink;
117 struct svlink svlink;
118 };
119
120 /* From: src odat ()To: dest odat (ref_id)*/
121 struct link {
122 int type; //1 = olink, 2 = vlink, 3 = svlink
123 union link_t link_t;
124 struct cdat* classp;
125 struct odat* odatp;
126 int set_idx;
127 int ele_idx;
128 };
129
130 struct root {
131 int x, y, z;
132 };
133
134 struct quad {
135 int x;
136 int y;
137 int z;
138 int ref_id;
139 };
140
141 /* variants: variants store the different map data for each archetype. */
142 struct variant {
143 uint8_t filename[NAME_MAX/sizeof(ucs4_t)];
144 uint8_t filepath[PATH_MAX/sizeof(ucs4_t)];
145 int height;
146 int width;
147 // int num_quads;
148 //struct quad quad_list[MAX_QUADS];
149 };
150
151 /* Odats: Odats consist of the object data necessary for
152 each object. Odats are sometimes referred to as archetypes
153 at compile-time, in order to distinguish the difference from
154 a runtime object and a compile-time object.
155 TODO: Need more info about objects at runtime, to described
156 the reasoning behind odat structure at compile-time*/
157 struct odat {
158 uint8_t name[32];
159 struct vdat* vdatp;
160 int vdat_id; //
161 int cdat_idx;
162 int hitbox;
163 int ref_id;
164 struct odat* parent_odatp; // odat == set ? null : set ref_id
165 struct root root;
166 struct ref* refp; /* pointer to it's ref on ref_list */
167 struct variant* variant_list[MAX_VARIANTS];
168 int vli; //variant list index
169 };
170
171 struct odat* curr_set_odatp; //when a set has elements, insert_set() can no longer
172 //refer to its odat via curr_odat, so save the set odat.
173
174 /* A framesheet is a grouping of animation frames in
175 a single direction (N,W,S,E) */
176 struct framesheet {
177 int width;
178 int height;
179 int num_frames;
180 void* frames[MAX_FRAMES];
181 };
182
183 /* A model is a collection of framesheets for every
184 direction (N,W,S,E,NW,NE,SW,SE)*/
185 /* NAMED spritesheet */
186 struct model {
187 uint8_t name[32];
188 struct framesheet spritesheet[8]; //one for each
189 };
190
191 /* Vdat: Vdats are the video data of each object. They can not be
192 created as a stand alone object (because they consist solely
193 of animation information and not the skeleton which the
194 animation manipulates). Vdats have a list of models for every
195 animation that the vdats odat can do for that vdat*/
196 struct vdat {
197 struct odat* creator; //pointer to odat that made this vdat
198 int num_models;
199 uint8_t filename[NAME_MAX/sizeof(ucs4_t)];
200 int height;
201 int width;
202 uint8_t filepath[PATH_MAX/sizeof(ucs4_t)];
203 //struct model model_list[MAX_MODELS];
204 };
205
206 /* Called after the cdat open operator has been recognized in grammar. Allocates
207 the space for a cdat on the cdat_buf, pushes that pointer onto
208 the cdat_stack */
209 void
210 push_cdat(uint8_t*);
211
212 /* Called after a cdat end operator has been recognized in grammar. Sets
213 top stack cdat ** to null and decrements stack pointer */
214 void
215 pop_cdat(void);
216
217 /* Called after an odat has been populated. Allocates memory for
218 the next odat. */
219
220 void
221 insert_set_label(uint8_t*, int);
222
223 /* Populate the sets representation in CURR_CDAT with a ref_id and insert a link
224 into the link_buf that will resolve the ref_id to an actual odat after parse time. */
225 void
226 insert_set_olink(int);
227
228 /* Put the vlink in the link_buf to be processed after parsetime */
229 void
230 insert_set_vlink(int, uint8_t*);
231
232 /* Put svlink in the link_buf to be processed after parsetime */
233 void
234 insert_set_svlink(int);
235
236 /* Called for every set reduction except for sets with olinks. Populates the
237 set data structures in the CDAT and in the ODAT. Uses the name and ref_id
238 from insert_set_label. Also inserts a ref into the ref_buf with the CURR_ODAT
239 pointer so that we can also resolve the odat from its ref_id. */
240 void
241 insert_set(void);
242
243 /* Insertion of eles is practically equivalent to how sets are inserted because both
244 share the same data type (ODAT). Like sets, eles have links, labels
245 and odats. Eles have the added notion of a parent set, and so must be inserted
246 into said parent set, but this is the only place they truly differ from sets. */
247
248 void
249 insert_set_vdatid(void);
250
251 void
252 insert_ele_label(uint8_t*, int);
253
254 /* Insert an ele olink into the CURR_ODAT */
255 void
256 insert_ele_olink(int);
257
258 /* Insert a ele vlink into CURR_ODAT*/
259 void
260 insert_ele_vlink(int, uint8_t*);
261
262 /* Inserts an ele short vlink into CURR_ODAT*/
263 void
264 insert_ele_svlink(int);
265
266 /* inserts ele into CURR_CLASS and CURR_ODAT */
267 void
268 insert_ele(void);
269
270 void
271 insert_ele_vdatid(void);
272
273 void
274 insert_vdat(uint8_t*, int, int, uint8_t*);
275 /* Inserts the hitbox into the CURR_ODAT */
276 void
277 insert_hitbox(int);
278
279 /* Inserts the root into the CURR_ODAT */
280 void
281 insert_root(int, int, int);
282
283 /* Inserts a quad into the CURR_ODAT */
284 void
285 insert_quad(int, int, int, int);
286
287 void
288 insert_variant(uint8_t*, int, int, uint8_t*);
289
290 void
291 insert_model(void);
292
293 void
294 insert_framesheet(uint8_t, uint8_t*, int, int, int, int);
295
296 void
297 insert_frame_pointer(uint8_t, void*);
298
299