5af236d6308dfaacf95a5e3d52e8657a23ce9411
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
;
110 /* Called in the reduction of a set. While both odats (eles and sets)
111 have identical label terminals, we are unable to give a single grammatical rule
112 for both due to how we allocate odats in the odat buf. Due to the
113 nature of bottom up parsing, the set label is recognized first, and then the
114 sets elements are recognized. This means that after we have processed the sets elemenets,
115 the curr_odat is going to be the last element and NOT the set that was first allocated.
116 To get around this, we create a global variable set_odatp that will store the pointer
117 to the odat when it is first allocated (in insert_set_label()) so that insert_set() can
118 have access to it. Curr set points the sets representation in the cdat, curr_set_odatp
119 points to the sets representation as an odat*/
128 struct set
* curr_setp
;
130 curr_setp
= curr_set();
131 curr_set_odatp
= alloc_odat();
133 memmove(curr_set_odatp
->name
, name
, 32);
134 memmove(curr_setp
->name
, name
, 32);
137 { curr_set_odatp
->ref_id
= ref_id
;
138 curr_setp
->ref_id
= ref_id
;
141 { curr_setp
->ref_id
= ss_ref_id
;
142 curr_set_odatp
->ref_id
= ss_ref_id
++;
147 /* Inserting a olink instead of a set. Set is really just a placeholder
148 for another set. Allocate the memory for the set so taht it can be populated*/
154 struct cdat
* curr_cdatp
;
155 struct link
* curr_linkp
;
156 struct odat
* curr_setp
;
158 curr_cdatp
= curr_cdat();
159 curr_setp
= alloc_odat();
160 curr_linkp
= alloc_link();
162 curr_setp
->cdat_idx
= curr_cdatp
->idx
;
163 curr_setp
->ref_id
= ref_id
; /* Will be resolved to offset
164 when link is processed */
165 curr_linkp
->type
= 1;
166 curr_linkp
->link_t
.olink
.ref_id
= ref_id
;
167 curr_linkp
->classp
= curr_cdatp
;
168 curr_linkp
->set_idx
= curr_cdatp
->num_sets
++;
169 curr_linkp
->ele_idx
= -1;
179 struct cdat
* curr_cdatp
;
180 struct link
* curr_linkp
;
182 curr_cdatp
= curr_cdat();
183 curr_linkp
= alloc_link();
185 /* Insert vlink into link_stack so that it gets processed at
187 curr_linkp
->type
= 2;
188 curr_linkp
->link_t
.vlink
.ref_id
= ref_id
;
189 curr_linkp
->classp
= curr_cdatp
;
190 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
191 curr_linkp
->ele_idx
= -1;
192 memmove(curr_linkp
->link_t
.vlink
.anim_name
, anim_name
, 32);
196 /* Svlinks dont have animation names */
202 struct cdat
* curr_cdatp
;
203 struct link
* curr_linkp
;
205 curr_cdatp
= curr_cdat();
206 curr_linkp
= alloc_link();
208 /* Insert svlink into link_stack so that it gets processed at
210 curr_linkp
->type
= 3;
211 curr_linkp
->classp
= curr_cdatp
;
212 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
213 curr_linkp
->ele_idx
= -1;
214 curr_linkp
->link_t
.svlink
.ref_id
= ref_id
;
218 /* At the point of reducing to a set, most of the
219 sets odat information has already been populated
220 during the reduction of its right hand side
221 non terminals (hitbox, root, quad_list). */
226 struct odat
* curr_odatp
;
227 struct cdat
* curr_cdatp
;
228 struct set
* curr_setp
;
229 struct ref
* prev_refp
;
230 struct ref
* curr_refp
;
231 struct vdat
* curr_vdatp
;
233 curr_odatp
= curr_set_odatp
; //allocated at insert_set_label, preserved in global space
234 curr_cdatp
= curr_cdat();
235 curr_setp
= curr_set();
236 prev_refp
= curr_ref();
237 curr_refp
= alloc_ref();
238 curr_vdatp
= curr_vdat();
240 curr_vdatp
->creator
= curr_set_odatp
;
242 curr_setp
->cdat_idx
= curr_cdatp
->idx
; //does a set need its class idx?
243 memmove(curr_setp
->name
, curr_odatp
->name
, 32);
244 curr_cdatp
->num_sets
++;
246 curr_odatp
->cdat_idx
= curr_cdatp
->idx
;
247 curr_odatp
->refp
= curr_refp
;
249 ref_id
= curr_setp
->ref_id
; // ref_id set by insert_set_label(name, ref_id)
251 curr_refp
->ref_id
= ref_id
;
252 curr_refp
->lastref
= prev_refp
;
253 curr_refp
->odatp
= curr_odatp
;
254 prev_refp
->nextref
= curr_refp
;
259 /* Created as a seperate function, instead of setting the ODATS vdat_id and
260 calling inc_vdat() inside of insert_set(), to account for the set reduction
261 where a vdat is not created (o/v/svlinks). */
266 struct vdat
* curr_vdatp
;
268 curr_vdatp
= curr_vdat();
270 curr_set_odatp
->vdat_id
= num_vdats
; //no vdat_id for odats that have vlinks/svlinks
271 curr_set_odatp
->vdatp
= curr_vdatp
;
272 curr_set_odatp
= NULL
; //This sets odat shouldnt be modified after populating odats vdat info
275 /* Populates the odat name and ref_id for odat, allocate the odat here for the rest of
276 the functions to use via curr_odat(). */
283 struct odat
* curr_odatp
;
285 curr_odatp
= alloc_odat();
287 memmove(curr_odatp
->name
, name
, 32);
290 curr_odatp
->ref_id
= ref_id
;
292 curr_odatp
->ref_id
= ss_ref_id
++;
296 /* We don't make an odat here, at output time we will resolve
297 the ref_id to the corresponding odat. */
303 struct cdat
* curr_cdatp
;
304 struct set
* curr_setp
;
305 struct ele
* curr_elep
;
306 struct link
* curr_linkp
;
308 curr_cdatp
= curr_cdat();
309 //curr_elep = curr_ele();
310 curr_linkp
= alloc_link();
312 //curr_elep->cdat_idx = curr_cdatp;
313 //curr_elep->ref_id = ref_id;
315 curr_linkp
->type
= 1;
316 curr_linkp
->link_t
.olink
.ref_id
= ref_id
;
317 curr_linkp
->classp
= curr_cdatp
;
318 curr_linkp
->set_idx
= curr_cdatp
->num_sets
++;
319 //curr_linkp->ele_idx = curr_setp->num_ele++;
329 struct cdat
* curr_cdatp
;
330 struct set
* curr_setp
;
331 struct link
* curr_linkp
;
333 curr_cdatp
= curr_cdat();
334 curr_setp
= curr_set();
335 curr_linkp
= alloc_link();
337 /* Insert vlink into link_stack so that it gets processed at
339 curr_linkp
->classp
= curr_cdatp
;
340 curr_linkp
->type
= 2;
341 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
342 //curr_linkp->ele_idx = curr_setp->num_ele;
343 curr_linkp
->link_t
.vlink
.ref_id
= ref_id
;
344 memmove(curr_linkp
->link_t
.vlink
.anim_name
, anim_name
, 32);
353 struct cdat
* curr_cdatp
;
354 struct set
* curr_setp
;
355 struct link
* curr_linkp
;
357 curr_cdatp
= curr_cdat();
358 curr_setp
= curr_set();
359 curr_linkp
= alloc_link();
361 curr_linkp
->classp
= curr_cdatp
;
362 curr_linkp
->type
= 3;
364 //curr_linkp->ele_idx = curr_setp->num_ele;
365 curr_linkp
->link_t
.svlink
.ref_id
= ref_id
;
370 //Insert element into odat_buf and cdatpages
375 struct cdat
* curr_cdatp
;
376 struct odat
* curr_odatp
;
377 struct vdat
* curr_vdatp
;
378 struct set
* curr_setp
;
379 struct ele
* curr_elep
;
380 struct ref
* curr_refp
;
381 struct ref
* prev_refp
;
384 curr_odatp
= curr_odat(); //malloced @ insert_ele_label
385 curr_vdatp
= curr_vdat();
386 curr_setp
= curr_set();
387 prev_refp
= curr_ref();
388 curr_refp
= alloc_ref();
390 curr_vdatp
->creator
= curr_odatp
;
392 /* Populate odat for ele */
393 curr_odatp
->cdat_idx
= curr_cdatp
->idx
;
394 curr_odatp
->refp
= curr_refp
;
396 ref_id
= curr_odatp
->ref_id
;
398 curr_refp
->ref_id
= ref_id
;
399 curr_refp
->lastref
= prev_refp
;
400 curr_refp
->odatp
= curr_odatp
;
401 prev_refp
->nextref
= curr_refp
;
408 { struct odat
* curr_odatp
;
409 curr_odatp
= curr_odat();
410 curr_odatp
->vdat_id
= num_vdats
;
418 struct odat
* curr_odatp
;
420 curr_odatp
->quad_filep
= quad_filep
;
423 /* Inserting the hitbox into the set
424 odat. Elements that don't have
425 a hitbox will use the sets root. */
430 { struct odat
* curr_odatp
;
432 curr_odatp
= curr_odat();
433 curr_odatp
->hitbox
= hitbox
;
436 /* Inserting the root into the set
437 odat. Elements that don't have
438 a root will use the sets root. */
445 { struct odat
* curr_odatp
;
447 curr_odatp
= curr_odat();
448 curr_odatp
->root
.x
= x
;
449 curr_odatp
->root
.y
= y
;
450 curr_odatp
->root
.z
= z
;
463 { struct vdat
* curr_vdatp
;
464 struct model
* curr_modelp
;
466 curr_vdatp
= curr_vdat();
467 curr_modelp
= curr_model();
469 curr_modelp
->spritesheet
[(int)direction
].height
= height
;
470 curr_modelp
->spritesheet
[(int)direction
].width
= width
;
471 curr_modelp
->spritesheet
[(int)direction
].num_frames
= num_frames
;
472 curr_vdatp
->num_models
++;
480 { struct model
* curr_modelp
;
482 curr_modelp
= curr_model();
484 curr_modelp
->spritesheet
[(int)direction
].frames
[curr_modelp
->spritesheet
[(int)direction
].num_frames
++] = frame
;