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