%code required{}
[henge/webcc.git] / src / apc / ir.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdint.h> //uint64_t
4 #include <string.h> //memmove
5 #include <stdlib.h> //malloc
6 #include "ir.h"
7
8 #define CURR_CDAT (*cdat_stackp)
9 #define CURR_SET set_list[CURR_CDAT->num_sets]
10 #define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
11 #define PREV_REF (ref_buf[num_refs-1])
12 #define CURR_REF (ref_buf[num_refs])
13 #define PREV_ODAT (odat_buf[num_odats-1])
14 #define CURR_ODAT (odat_buf[num_odats])
15 #define CURR_VDAT (vdat_buf[num_vdats])
16 #define PREV_VDAT (vdat_buf[num_vdats-1])
17 #define CURR_MODEL model_list[CURR_VDAT->num_models]
18 #define CURR_LINK (link_buf[num_links])
19
20
21 void
22 ir_init()
23 {
24 /* Init root cdat and stack */
25 char root[4] = "root";
26
27 cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof(struct cdat) );
28 cdat_buf[num_cdats]->idx = 0;
29 memmove(cdat_buf[num_cdats]->name, root, 4);
30
31 cdat_stackp = cdat_stack;
32 *cdat_stackp = cdat_buf[num_cdats++];
33
34 /* Init first odat */
35 if( (CURR_ODAT = (struct odat*) malloc(sizeof(struct odat))) == NULL)
36 perror("malloc first odat failed");
37
38 /* init first vdat*/
39 if( (CURR_VDAT = (struct vdat*) malloc(sizeof(struct vdat))) == NULL)
40 perror("malloc first vdat failed");
41
42 /* Init first ref */
43 if( (CURR_REF = (struct ref*) malloc(sizeof(struct ref))) == NULL)
44 perror("malloc first ref failed");
45
46 /* Init first link */
47 if( (CURR_LINK = (struct link*) malloc(sizeof(struct link))) == NULL)
48 perror("malloc first link failed");
49 }
50
51 //TODO: FREE MEMORY!
52 void
53 malloc_cdat()
54 {
55
56 if(curr_max_cdats <= num_cdats)
57 {
58 if( (realloc((void*) cdat_buf, PTRS_IN_PAGE * 4)) == NULL)
59 perror("realloc cdat_buf failed");
60 curr_max_cdats += PTRS_IN_PAGE;
61 if( (realloc( (void*) cdat_stack, PTRS_IN_PAGE * 4)) == NULL) //increase cdat_stack also
62 perror("realloc cdat_stack failed");
63 }
64 if( (cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof (struct cdat)) ) == NULL )
65 perror("malloc cdat failed");
66
67
68 }
69
70 /* Dynamically allocate memory for a class data structure,
71 or cdat, after a class has been identified in a grammar.
72 We also create a stack of class pointers so that
73 we can access the cdat during processing of that
74 cdats sets and elements, a requirement because the
75 nature of recursive classes prevents us from accessing
76 the cdat based on the previous index into cdat_buf,
77 which is a list of all allocated cdats*/
78 void
79 push_cdat(char* name)
80 {
81
82 malloc_cdat();
83
84 memmove(cdat_buf[num_cdats]->name, name, 32);
85 cdat_buf[num_cdats]->idx = num_cdats;
86
87 /* Set the cdat as a class of the previous cdat */
88 (*cdat_stackp)->class_list[(*cdat_stackp)->num_classes++] = cdat_buf[num_cdats];
89
90 /* Push the cdat onto the cdat_stack */
91 *++cdat_stackp = cdat_buf[num_cdats++];
92
93 }
94
95 void
96 pop_cdat()
97 {
98 *cdat_stackp = NULL;
99 cdat_stackp--;
100 }
101
102 void
103 inc_odat()
104 {
105 num_odats++;
106 if(num_odats >= curr_max_odats)
107 {
108 if( (realloc((void*) odat_buf, PTRS_IN_PAGE * 4)) == NULL)
109 perror("realloc odat_buf failed");
110 curr_max_odats += PTRS_IN_PAGE;
111 }
112 if( (CURR_ODAT = (struct odat*) malloc(sizeof (struct odat))) == NULL)
113 {
114 perror("malloc odat failed");
115 }
116
117 }
118
119 void
120 inc_vdat()
121 {
122 num_vdats++;
123 if(num_vdats >= curr_max_vdats)
124 {
125 if( (realloc((void*) vdat_buf, PTRS_IN_PAGE * 4)) == NULL)
126 perror("realloc vdat_buf failed");
127 curr_max_vdats += PTRS_IN_PAGE;
128 }
129 if((CURR_VDAT = (struct vdat*) malloc(sizeof (struct vdat))) == NULL)
130 {
131 perror("malloc vdat failed");
132 }
133
134 }
135
136 void
137 inc_link()
138 {
139 num_links++;
140 if(num_links >= curr_max_links)
141 {
142 if( (realloc((void*) link_buf, PTRS_IN_PAGE * 4)) == NULL)
143 perror("realloc vdat_buf failed");
144 curr_max_links += PTRS_IN_PAGE;
145 }
146 if((CURR_LINK = (struct link*) malloc(sizeof (struct link))) == NULL)
147 {
148 perror("malloc link failed");
149 }
150 }
151
152 void
153 inc_ref()
154 {
155
156 if(num_refs % 16 == 0)
157 {
158 posts[num_posts++] = *CURR_REF;
159 }
160
161 num_refs++;
162 if(num_refs >= curr_max_refs)
163 {
164 if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
165 perror("realloc ref_buf failed");
166 curr_max_refs += PTRS_IN_PAGE;
167 }
168 if((CURR_REF = (struct ref*) malloc(sizeof (struct ref))) == NULL)
169 perror("malloc ref failed");
170 }
171
172 void
173 insert_set_label(char* name, uint64_t ref_id)
174 {
175
176
177 memmove(CURR_CDAT->CURR_SET.name,name,32);
178 memmove(&CURR_CDAT->CURR_SET.ref_id,&ref_id,64);
179
180 }
181 void
182 insert_set_olink(uint64_t ref_id)
183 {
184 CURR_CDAT->CURR_SET.cdat_idx = CURR_CDAT->idx;
185 CURR_CDAT->CURR_SET.ref_id = ref_id; /* Will be resolved to offset
186 when link is processed */
187 CURR_LINK->type = 1;
188 CURR_LINK->link_t.olink.ref_id = ref_id;
189 CURR_LINK->cdat_idx = CURR_CDAT->idx;
190 CURR_LINK->set_idx = CURR_CDAT->num_sets++;
191 CURR_LINK->ele_idx = -1;
192
193 inc_link();
194 }
195
196 void
197 insert_set_vlink(uint64_t ref_id, char* anim_name)
198 {
199 /* Insert vlink into link_stack so that it gets processed at
200 output time */
201 CURR_LINK->cdat_idx = CURR_CDAT->idx;
202 CURR_LINK->set_idx = CURR_CDAT->num_sets;
203 CURR_LINK->type = 2;
204 CURR_LINK->link_t.vlink.ref_id = ref_id;
205 memmove(CURR_LINK->link_t.vlink.anim_name, anim_name, 32);
206
207
208 }
209
210 void
211 insert_set_svlink(uint64_t ref_id)
212 {
213
214 /* Insert vlink into link_stack so that it gets processed at
215 output time */
216 CURR_LINK->cdat_idx = CURR_CDAT->idx;
217 CURR_LINK->set_idx = CURR_CDAT->num_sets;
218 CURR_LINK->type = 3;
219 CURR_LINK->link_t.svlink.ref_id = ref_id;
220
221 }
222
223 /* At the point of reducing to a set, most of the
224 sets odat information has already been populated
225 during the reduction of its right hand side
226 non terminals (hitbox, root, quad_list). */
227 void
228 insert_set()
229 {
230 uint64_t ref_id;
231
232 ref_id = CURR_CDAT->CURR_SET.ref_id;
233
234 CURR_CDAT->CURR_SET.cdat_idx = CURR_CDAT->idx;
235 memmove(CURR_ODAT->name, CURR_CDAT->CURR_SET.name, 32);
236 CURR_CDAT->num_sets++;
237
238 CURR_ODAT->cdat_idx = CURR_CDAT->idx;
239 CURR_ODAT->refp = CURR_REF;
240
241
242 CURR_REF->lastref = PREV_REF;
243 PREV_REF->nextref = CURR_REF;
244 CURR_REF->odatp = CURR_ODAT;
245
246
247 if(ref_id == -1) /* user did not define a ref_id so */
248 { /* use a ref_id from system_space */
249 ref_id = ss_ref_id;
250 ss_ref_id++;
251 }
252
253 CURR_REF->ref_id = ref_id;
254
255
256
257 inc_ref();
258 inc_odat();
259 }
260
261 void
262 insert_vdat()
263 {
264 PREV_ODAT->vdat_id = num_vdats; //NULL for vlink, svlink
265 inc_vdat();
266 }
267
268 /* Populates both the odat name and ref_id
269 for element. */
270 void
271 insert_ele_label(char* name, uint64_t ref_id)
272 {
273 memmove(CURR_CDAT->CURR_SET.CURR_ELE.name, name, 32);
274 memmove(&CURR_CDAT->CURR_SET.ele_list[CURR_CDAT->CURR_SET.ref_id].ref_id, &ref_id, 64);
275 }
276
277 void
278 insert_ele_olink(uint64_t ref_id)
279 {
280 CURR_CDAT->CURR_SET.CURR_ELE.cdat_idx = CURR_CDAT->idx;
281 CURR_CDAT->CURR_SET.CURR_ELE.ref_id = ref_id; /* Will be resolved to offset
282 when link is processed */
283 CURR_LINK->type = 1;
284 CURR_LINK->link_t.olink.ref_id = ref_id;
285 CURR_LINK->cdat_idx = CURR_CDAT->idx;
286 CURR_LINK->set_idx = CURR_CDAT->num_sets++;
287 CURR_LINK->ele_idx = CURR_CDAT->CURR_SET.num_ele++;
288
289 }
290 void
291 insert_ele_vlink(uint64_t ref_id, char* anim_name)
292 {
293
294 /* Insert vlink into link_stack so that it gets processed at
295 output time */
296 CURR_LINK->cdat_idx = CURR_CDAT->idx;
297 CURR_LINK->type = 2;
298 CURR_LINK->set_idx = CURR_CDAT->num_sets;
299 CURR_LINK->ele_idx = CURR_CDAT->CURR_SET.num_ele;
300 CURR_LINK->link_t.vlink.ref_id = ref_id;
301 memmove(CURR_LINK->link_t.vlink.anim_name, anim_name, 32);
302
303
304 }
305 void
306 insert_ele_svlink(uint64_t ref_id)
307 {
308
309 CURR_LINK->cdat_idx = CURR_CDAT->idx;
310 CURR_LINK->type = 3;
311 CURR_LINK->set_idx = CURR_CDAT->num_sets;
312 CURR_LINK->ele_idx = CURR_CDAT->CURR_SET.num_ele;
313 CURR_LINK->link_t.svlink.ref_id = ref_id;
314
315
316 }
317 //Insert element into odat_buf and cdatpages
318 void
319 insert_ele()
320 {
321
322 uint64_t ref_id;
323
324 ref_id = CURR_CDAT->CURR_SET.CURR_ELE.ref_id;
325
326 CURR_CDAT->CURR_SET.CURR_ELE.cdat_idx = CURR_CDAT->idx;
327 memmove(CURR_ODAT->name,CURR_CDAT->CURR_SET.CURR_ELE.name, 32);
328 CURR_CDAT->CURR_SET.num_ele++;
329
330 CURR_ODAT->cdat_idx = CURR_CDAT->idx;
331 CURR_ODAT->refp = CURR_REF;
332
333 if(ref_id == -1) /* user did not define a ref_id so */
334 { /* use a ref_id from system_space */
335 ref_id = ss_ref_id;
336 ss_ref_id++;
337 }
338
339 CURR_REF->ref_id = ref_id;
340
341 inc_odat();
342 inc_ref();
343
344 }
345
346 void
347 insert_framesheets(char direction, char* name, uint64_t ref_id, int height , int width, int num_frames)
348 {
349 CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].height = height;
350 CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].width = width;
351 CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].num_frames = num_frames;
352 CURR_VDAT->num_models++;
353 }
354
355 #define CURR_QUAD (CURR_ODAT->quad_list[CURR_ODAT->num_quads])
356 void
357 insert_quad(int x, int y, int z, uint64_t ref_id)
358 {
359 CURR_QUAD.x = x;
360 CURR_QUAD.y = y;
361 CURR_QUAD.z = z;
362 CURR_QUAD.ref_id = ref_id;
363 CURR_ODAT->num_quads++;
364 }
365
366 /* Inserting the hitbox into the set
367 odat. Elements that don't have
368 a hitbox will use the sets root. */
369 void
370 insert_hitbox(int hitbox)
371 {
372 CURR_ODAT->hitbox = hitbox;
373 }
374
375 /* Inserting the root into the set
376 odat. Elements that don't have
377 a root will use the sets root. */
378 void
379 insert_root(int x, int y, int z)
380 {
381
382 CURR_ODAT->root.x = x;
383 CURR_ODAT->root.y = y;
384 CURR_ODAT->root.z = z;
385 }
386
387 void
388 insert_frame_pointer(char direction, void* frame)
389 {
390 CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].frames[CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].num_frames++] = frame;
391 }
392