2 \brief IR Memory Implementation
3 \details Intermediary memory management
6 ----------------------------------------------------------------------------*/
9 #include <stdint.h> //uint64_t
10 #include <string.h> //memmove
11 #include <stdlib.h> //malloc
16 /* functions needed from irmem.c */
69 /* struct definitions needed from irmem.c */
71 extern struct cdat
** cdat_stackp
;
72 extern struct odat
* curr_set_odatp
;
73 extern uint64_t ss_ref_id
;
76 /* Dynamically allocate memory for a class data structure,
77 or cdat, after a class has been identified in a grammar.
78 We also create a stack of class pointers so that
79 we can access the cdat during processing of that
80 cdats sets and elements, a requirement because the
81 nature of recursive classes prevents us from accessing
82 the cdat based on the previous index into cdat_buf,
83 which is a list of all allocated cdats*/
89 struct cdat
* curr_cdatp
;
91 curr_cdatp
= alloc_cdat();
93 memmove(curr_cdatp
->name
, name
, 32);
94 curr_cdatp
->idx
= num_cdats
;
96 /* Set the cdat as a subclass of the previous cdat */
97 (*cdat_stackp
)->class_list
[(*cdat_stackp
)->num_classes
] = curr_cdatp
;
98 /* Push the cdat onto the cdat_stack */
99 *++cdat_stackp
= curr_cdatp
;
111 /* Called in the reduction of a set. While both odats (eles and sets)
112 have identical label terminals, we are unable to give a single grammatical rule
113 for both due to how we allocate odats in the odat buf. Due to the
114 nature of bottom up parsing, the set label is recognized first, and then the
115 sets elements are recognized. This means that after we have processed the sets elemenets,
116 the curr_odat is going to be the last element and NOT the set that was first allocated.
117 To get around this, we create a global variable set_odatp that will store the pointer
118 to the odat when it is first allocated (in insert_set_label()) so that insert_set() can
119 have access to it. */
127 struct set
* curr_setp
;
129 curr_setp
= curr_set();
130 curr_set_odatp
= alloc_odat();
132 memmove(curr_set_odatp
->name
, name
, 32);
133 memmove(curr_setp
->name
, name
, 32);
134 curr_setp
->ref_id
= ref_id
;
142 struct set
* curr_setp
;
143 struct cdat
* curr_cdatp
;
144 struct link
* curr_linkp
;
146 curr_setp
= curr_set();
147 curr_cdatp
= curr_cdat();
148 curr_linkp
= alloc_link();
150 curr_setp
->cdat_idx
= curr_cdatp
->idx
;
151 curr_setp
->ref_id
= ref_id
; /* Will be resolved to offset
152 when link is processed */
153 curr_linkp
->type
= 1;
154 curr_linkp
->link_t
.olink
.ref_id
= ref_id
;
155 curr_linkp
->cdat_idx
= curr_cdatp
->idx
;
156 curr_linkp
->set_idx
= curr_cdatp
->num_sets
++;
157 curr_linkp
->ele_idx
= -1;
167 struct cdat
* curr_cdatp
;
168 struct link
* curr_linkp
;
170 curr_cdatp
= curr_cdat();
171 curr_linkp
= alloc_link();
173 /* Insert vlink into link_stack so that it gets processed at
175 curr_linkp
->type
= 2;
176 curr_linkp
->link_t
.vlink
.ref_id
= ref_id
;
177 curr_linkp
->cdat_idx
= curr_cdatp
->idx
;
178 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
179 curr_linkp
->ele_idx
= -1;
180 memmove(curr_linkp
->link_t
.vlink
.anim_name
, anim_name
, 32);
184 /* Svlinks dont have animation names */
190 struct cdat
* curr_cdatp
;
191 struct link
* curr_linkp
;
193 curr_cdatp
= curr_cdat();
194 curr_linkp
= alloc_link();
196 /* Insert svlink into link_stack so that it gets processed at
198 curr_linkp
->type
= 3;
199 curr_linkp
->cdat_idx
= curr_cdatp
->idx
;
200 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
201 curr_linkp
->ele_idx
= -1;
202 curr_linkp
->link_t
.svlink
.ref_id
= ref_id
;
206 /* At the point of reducing to a set, most of the
207 sets odat information has already been populated
208 during the reduction of its right hand side
209 non terminals (hitbox, root, quad_list). */
214 struct odat
* curr_odatp
;
215 struct cdat
* curr_cdatp
;
216 struct set
* curr_setp
;
217 struct ref
* prev_refp
;
218 struct ref
* curr_refp
;
219 struct vdat
* curr_vdatp
;
221 curr_odatp
= curr_set_odatp
; //allocated at insert_set_label, preserved in global space
222 curr_cdatp
= curr_cdat();
223 curr_setp
= curr_set();
224 prev_refp
= prev_ref();
225 curr_refp
= alloc_ref();
226 curr_vdatp
= curr_vdat();
228 curr_vdatp
->creator
= curr_set_odatp
;
230 curr_setp
->cdat_idx
= curr_cdatp
->idx
; //does a set need its class idx?
231 memmove(curr_setp
->name
, curr_odatp
->name
, 32);
232 curr_cdatp
->num_sets
++;
234 curr_odatp
->cdat_idx
= curr_cdatp
->idx
;
235 curr_odatp
->refp
= curr_refp
;
237 ref_id
= curr_setp
->ref_id
; // ref_id set by insert_set_label(name, ref_id)
239 if(ref_id
== -1) /* user did not define a ref_id */
240 { ref_id
= ss_ref_id
;
244 curr_refp
->ref_id
= ref_id
;
245 curr_refp
->lastref
= prev_refp
;
246 curr_refp
->odatp
= curr_odatp
;
247 prev_refp
->nextref
= curr_refp
;
252 /* Created as a seperate function, instead of setting the ODATS vdat_id and
253 calling inc_vdat() inside of insert_set(), to account for the set reduction
254 where a vdat is not created (o/v/svlinks). */
259 curr_set_odatp
->vdat_id
= num_vdats
; //no vdat_id for odats that have vlinks/svlinks
260 curr_set_odatp
= NULL
; //So this sets odat cant be modified after (which would be a bug)
263 /* Populates both the odat name and ref_id
271 struct odat
* curr_odatp
;
273 curr_odatp
= alloc_odat();
275 memmove(curr_odatp
->name
, name
, 32);
276 curr_odatp
->ref_id
= ref_id
;
280 /* We don't make an odat here, at output time we will resolve
281 the ref_id to the corresponding odat. */
287 struct cdat
* curr_cdatp
;
288 struct set
* curr_setp
;
289 struct ele
* curr_elep
;
290 struct link
* curr_linkp
;
292 curr_cdatp
= curr_cdat();
293 //curr_elep = curr_ele();
294 curr_linkp
= alloc_link();
296 //curr_elep->cdat_idx = curr_cdatp->idx;
297 //curr_elep->ref_id = ref_id;
299 curr_linkp
->type
= 1;
300 curr_linkp
->link_t
.olink
.ref_id
= ref_id
;
301 curr_linkp
->cdat_idx
= curr_cdatp
->idx
;
302 curr_linkp
->set_idx
= curr_cdatp
->num_sets
++;
303 //curr_linkp->ele_idx = curr_setp->num_ele++;
313 struct cdat
* curr_cdatp
;
314 struct set
* curr_setp
;
315 struct link
* curr_linkp
;
317 curr_cdatp
= curr_cdat();
318 curr_setp
= curr_set();
319 curr_linkp
= alloc_link();
321 /* Insert vlink into link_stack so that it gets processed at
323 curr_linkp
->cdat_idx
= curr_cdatp
->idx
;
324 curr_linkp
->type
= 2;
325 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
326 //curr_linkp->ele_idx = curr_setp->num_ele;
327 curr_linkp
->link_t
.vlink
.ref_id
= ref_id
;
328 memmove(curr_linkp
->link_t
.vlink
.anim_name
, anim_name
, 32);
337 struct cdat
* curr_cdatp
;
338 struct set
* curr_setp
;
339 struct link
* curr_linkp
;
341 curr_cdatp
= curr_cdat();
342 curr_setp
= curr_set();
343 curr_linkp
= alloc_link();
345 curr_linkp
->cdat_idx
= curr_cdatp
->idx
;
346 curr_linkp
->type
= 3;
348 //curr_linkp->ele_idx = curr_setp->num_ele;
349 curr_linkp
->link_t
.svlink
.ref_id
= ref_id
;
354 //Insert element into odat_buf and cdatpages
359 struct cdat
* curr_cdatp
;
360 struct odat
* curr_odatp
;
361 struct vdat
* curr_vdatp
;
362 struct set
* curr_setp
;
363 struct ele
* curr_elep
;
364 struct ref
* curr_refp
;
365 struct ref
* prev_refp
;
368 curr_odatp
= curr_odat(); //malloced @ insert_ele_label
369 curr_vdatp
= curr_vdat();
370 curr_setp
= curr_set();
371 curr_refp
= alloc_ref();
372 prev_refp
= prev_ref();
374 curr_vdatp
->creator
= curr_odatp
;
376 /* Populate odat for ele */
377 curr_odatp
->cdat_idx
= curr_cdatp
->idx
;
378 curr_odatp
->refp
= curr_refp
;
380 ref_id
= curr_odatp
->ref_id
;
383 if(ref_id
== -1) /* user did not define a ref_id so */
384 { ref_id
= ss_ref_id
;
388 curr_refp
->ref_id
= ref_id
;
389 curr_refp
->lastref
= prev_refp
;
390 curr_refp
->odatp
= curr_odatp
;
391 prev_refp
->nextref
= curr_refp
;
398 { struct odat
* curr_odatp
;
399 curr_odatp
= curr_odat();
400 curr_odatp
->vdat_id
= num_vdats
;
409 /* struct quad* curr_quadp; */
410 /* struct odat* curr_odatp; */
412 /* curr_quadp = curr_quad(); */
413 /* curr_odatp = curr_odat(); */
415 /* curr_quadp->x = x; */
416 /* curr_quadp->y = y; */
417 /* curr_quadp->z = z; */
418 /* curr_quadp->ref_id = ref_id; */
419 /* curr_odatp->num_quads++; */
420 struct odat
* curr_odatp
;
422 curr_odatp
->quad_filep
= quad_filep
;
425 /* Inserting the hitbox into the set
426 odat. Elements that don't have
427 a hitbox will use the sets root. */
432 { struct odat
* curr_odatp
;
434 curr_odatp
= curr_odat();
435 curr_odatp
->hitbox
= hitbox
;
438 /* Inserting the root into the set
439 odat. Elements that don't have
440 a root will use the sets root. */
447 { struct odat
* curr_odatp
;
449 curr_odatp
= curr_odat();
450 curr_odatp
->root
.x
= x
;
451 curr_odatp
->root
.y
= y
;
452 curr_odatp
->root
.z
= z
;
465 { struct vdat
* curr_vdatp
;
466 struct model
* curr_modelp
;
468 curr_vdatp
= curr_vdat();
469 curr_modelp
= curr_model();
471 curr_modelp
->spritesheet
[(int)direction
].height
= height
;
472 curr_modelp
->spritesheet
[(int)direction
].width
= width
;
473 curr_modelp
->spritesheet
[(int)direction
].num_frames
= num_frames
;
474 curr_vdatp
->num_models
++;
482 { struct model
* curr_modelp
;
484 curr_modelp
= curr_model();
486 curr_modelp
->spritesheet
[(int)direction
].frames
[curr_modelp
->spritesheet
[(int)direction
].num_frames
++] = frame
;