4e23d161ed9edb26a11c287975cf06b7763bba02
2 \brief IR Memory Implementation
3 \details Intermediary memory management
6 ----------------------------------------------------------------------------*/
9 #include <unitypes.h> //uint8_t as a char
10 #include <unistr.h> //u32_cpy
11 #include <stdint.h> //uint64_t
12 #include <stdlib.h> //malloc
16 //scanner_scanpixels(int*, int);
18 /* functions needed from irmem.c */
87 /* struct definitions needed from irmem.c */
89 extern struct cdat
** cdat_stackp
;
90 extern struct odat
* curr_set_odatp
;
94 /* Dynamically allocate memory for a class data structure,
95 or cdat, after a class has been identified in a grammar.
96 We also create a stack of class pointers so that
97 we can access the cdat during processing of that
98 cdats sets and elements, a requirement because the
99 nature of recursive classes prevents us from accessing
100 the cdat based on the previous index into cdat_buf,
101 which is a list of all allocated cdats*/
107 struct cdat
* curr_cdatp
;
109 curr_cdatp
= alloc_cdat();
111 u8_cpy(curr_cdatp
->name
, name
, 32);
112 curr_cdatp
->idx
= num_cdats
;
114 /* Set the cdat as a subclass of the previous cdat */
115 (*cdat_stackp
)->class_list
[(*cdat_stackp
)->num_classes
] = curr_cdatp
;
116 /* Push the cdat onto the cdat_stack */
117 *++cdat_stackp
= curr_cdatp
;
128 /* Called in the reduction of a set. While both odats (eles and sets)
129 have identical label terminals, we are unable to give a single grammatical rule
130 for both due to how we allocate odats in the odat buf. Due to the
131 nature of bottom up parsing, the set label is recognized first, and then the
132 sets elements are recognized. This means that after we have processed the sets elemenets,
133 the curr_odat is going to be the last element and NOT the set that was first allocated.
134 To get around this, we create a global variable set_odatp that will store the pointer
135 to the odat when it is first allocated (in insert_set_label()) so that insert_set() can
136 have access to it. Curr set points the sets representation in the cdat, curr_set_odatp
137 points to the sets representation as an odat*/
146 struct set
* curr_setp
;
148 curr_setp
= curr_set();
149 curr_set_odatp
= alloc_odat();
151 u8_cpy(curr_set_odatp
->name
, name
, 32);
152 u8_cpy(curr_setp
->name
, name
, 32);
153 curr_set_odatp
->parent_odatp
= NULL
;
156 { curr_set_odatp
->ref_id
= ref_id
;
157 curr_setp
->ref_id
= ref_id
;
160 { curr_setp
->ref_id
= ss_ref_id
;
161 curr_set_odatp
->ref_id
= ss_ref_id
++;
166 /* Inserting a olink instead of a set. Set is really just a placeholder
167 for another set. Allocate the memory for the set so taht it can be populated*/
173 struct set
* curr_setp
;
175 curr_setp
= curr_set();
177 curr_setp
->ref_id
= ref_id
;
187 struct cdat
* curr_cdatp
;
188 struct odat
* curr_odatp
;
189 struct link
* curr_linkp
;
192 curr_cdatp
= curr_cdat();
193 curr_odatp
= curr_odat();
194 curr_linkp
= alloc_link();
196 /* Insert vlink into link_stack so that it gets processed at
198 curr_linkp
->type
= 2;
199 /* Store the target odat information*/
200 curr_linkp
->link_t
.vlink
.ref_id
= ref_id
;
201 u8_cpy(curr_linkp
->link_t
.vlink
.anim_name
, anim_name
, 32);
202 /* Store the linking odat/cdat information */
203 curr_linkp
->classp
= curr_cdatp
;
204 curr_linkp
->odatp
= curr_odatp
;
205 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
206 curr_linkp
->ele_idx
= -1;
210 /* Svlinks dont have animation names */
216 struct cdat
* curr_cdatp
;
217 struct link
* curr_linkp
;
219 curr_cdatp
= curr_cdat();
220 curr_linkp
= alloc_link();
222 /* Insert svlink into link_stack so that it gets processed at
224 curr_linkp
->type
= 3;
225 curr_linkp
->classp
= curr_cdatp
;
226 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
227 curr_linkp
->ele_idx
= -1;
228 curr_linkp
->link_t
.svlink
.ref_id
= ref_id
;
232 /* At the point of reducing to a set, most of the
233 sets odat information has already been populated
234 during the reduction of its right hand side
235 non terminals (hitbox, root, quad_list). */
240 struct odat
* curr_odatp
;
241 struct cdat
* curr_cdatp
;
242 struct set
* curr_setp
;
243 struct ref
* prev_refp
;
244 struct ref
* curr_refp
;
245 struct vdat
* curr_vdatp
;
247 curr_odatp
= curr_set_odatp
; //allocated at insert_set_label, preserved in global space
248 curr_cdatp
= curr_cdat();
249 curr_setp
= curr_set();
250 prev_refp
= curr_ref();
251 curr_refp
= alloc_ref();
252 curr_vdatp
= curr_vdat();
254 curr_vdatp
->creator
= curr_set_odatp
;
256 curr_setp
->cdat_idx
= curr_cdatp
->idx
; //does a set need its class idx?
257 u8_cpy(curr_setp
->name
, curr_odatp
->name
, 32);
258 curr_cdatp
->num_sets
++;
260 curr_odatp
->cdat_idx
= curr_cdatp
->idx
;
261 curr_odatp
->refp
= curr_refp
;
263 ref_id
= curr_setp
->ref_id
; // ref_id set by insert_set_label(name, ref_id)
265 curr_refp
->ref_id
= ref_id
;
266 curr_refp
->lastref
= prev_refp
;
267 curr_refp
->odatp
= curr_odatp
;
268 prev_refp
->nextref
= curr_refp
;
273 /* Created as a seperate function, instead of setting the ODATS vdat_id and
274 calling inc_vdat() inside of insert_set(), to account for the set reduction
275 where a vdat is not created (o/v/svlinks). */
280 struct vdat
* curr_vdatp
;
282 curr_vdatp
= curr_vdat();
284 curr_set_odatp
->vdat_id
= num_vdats
; //no vdat_id for odats that have vlinks/svlinks
285 curr_set_odatp
->vdatp
= curr_vdatp
;
286 curr_set_odatp
= NULL
; //This sets odat shouldnt be modified after populating odats vdat info
289 /* Populates the odat name and ref_id for odat, allocate the odat here for the rest of
290 the functions to use via curr_odat(). */
297 struct odat
* curr_odatp
;
299 curr_odatp
= alloc_odat();
301 u8_cpy(curr_odatp
->name
, name
, 32);
304 curr_odatp
->ref_id
= ref_id
;
306 curr_odatp
->ref_id
= ss_ref_id
++;
310 /* We don't make an odat here, at output time we will resolve
311 the ref_id to the corresponding odat. */
317 /* Do nothing because we already know the ref_id that
318 the odat needs for this element (in the quad_file) */
327 struct cdat
* curr_cdatp
;
328 struct set
* curr_setp
;
329 struct link
* curr_linkp
;
331 curr_cdatp
= curr_cdat();
332 curr_setp
= curr_set();
333 curr_linkp
= alloc_link();
335 /* Insert vlink into link_stack so that it gets processed at
337 curr_linkp
->classp
= curr_cdatp
;
338 curr_linkp
->type
= 2;
339 curr_linkp
->set_idx
= curr_cdatp
->num_sets
;
340 //curr_linkp->ele_idx = curr_setp->num_ele;
341 curr_linkp
->link_t
.vlink
.ref_id
= ref_id
;
342 u8_cpy(curr_linkp
->link_t
.vlink
.anim_name
, anim_name
, 32);
351 struct cdat
* curr_cdatp
;
352 struct set
* curr_setp
;
353 struct link
* curr_linkp
;
355 curr_cdatp
= curr_cdat();
356 curr_setp
= curr_set();
357 curr_linkp
= alloc_link();
359 curr_linkp
->classp
= curr_cdatp
;
360 curr_linkp
->type
= 3;
362 //curr_linkp->ele_idx = curr_setp->num_ele;
363 curr_linkp
->link_t
.svlink
.ref_id
= ref_id
;
368 //Insert element into odat_buf and cdatpages
373 struct cdat
* curr_cdatp
;
374 struct odat
* curr_odatp
;
375 struct vdat
* curr_vdatp
;
376 struct set
* curr_setp
;
377 struct ele
* curr_elep
;
378 struct ref
* curr_refp
;
379 struct ref
* prev_refp
;
382 curr_odatp
= curr_odat(); //malloced @ insert_ele_label
383 curr_vdatp
= curr_vdat();
384 curr_setp
= curr_set();
385 prev_refp
= curr_ref();
386 curr_refp
= alloc_ref();
388 curr_vdatp
->creator
= curr_odatp
;
390 /* Populate odat for ele */
391 curr_odatp
->cdat_idx
= curr_cdatp
->idx
;
392 curr_odatp
->refp
= curr_refp
;
393 curr_odatp
->parent_odatp
= curr_set_odatp
;
395 ref_id
= curr_odatp
->ref_id
;
397 curr_refp
->ref_id
= ref_id
;
398 curr_refp
->lastref
= prev_refp
;
399 curr_refp
->odatp
= curr_odatp
;
400 prev_refp
->nextref
= curr_refp
;
407 { struct odat
* curr_odatp
;
408 curr_odatp
= curr_odat();
409 curr_odatp
->vdat_id
= num_vdats
;
414 (uint8_t* filename
, int height
, int width
, uint8_t* filepath
)
416 struct vdat
* curr_vdatp
;
417 int filename_len
, filepath_len
;
419 curr_vdatp
= alloc_vdat();
421 u8_strcpy(curr_vdatp
->filename
, filename
);
422 u8_strcpy(curr_vdatp
->filepath
, filepath
);
423 curr_vdatp
->height
= height
;
424 curr_vdatp
->width
= width
;
425 curr_vdatp
->creator
= curr_odat();
432 (uint8_t* filename
, int height
, int width
, uint8_t* filepath
)
433 { struct variant
* curr_variantp
;
434 int x
, y
, pixel
, num_frames
, n
;
437 /* Allocate the mdat */
438 curr_variantp
= alloc_variant();
440 u8_strcpy(curr_variantp
->filename
, filename
);
441 u8_strcpy(curr_variantp
->filepath
, filepath
);
442 curr_variantp
->height
= height
;
443 curr_variantp
->width
= width
;
445 //curr_mdatp->quad_list[num_quads].x/.y/.z/ref_id */
448 curr_set_odatp
->variant_list
[curr_set_odatp
->vli
++] = curr_variantp
;
454 /* ( int x, int y, int z, int ref_id */
457 /* struct quad* curr_quadp; */
459 /* curr_quadp = curr_quad(); */
461 /* curr_quadp->x = x; */
462 /* curr_quadp->y = y; */
463 /* curr_quadp->z = z; */
464 /* curr_quadp->ref_id = ref_id; */
470 /* /\* serting the hitbox into the set */
471 /* odat. Elements that don't have */
472 /* a hitbox will use the sets root. *\/ */
477 /* { struct odat* curr_odatp; */
479 /* curr_odatp = curr_odat(); */
480 /* curr_odatp->hitbox = hitbox; */
483 /* /\* Inserting the root into the set */
484 /* odat. Elements that don't have */
485 /* a root will use the sets root. *\/ */
492 /* { struct odat* curr_odatp; */
494 /* curr_odatp = curr_odat(); */
495 /* curr_odatp->root.x = x; */
496 /* curr_odatp->root.y = y; */
497 /* curr_odatp->root.z = z; */
503 /* insert_framesheet */
504 /* ( uint8_t direction, */
511 /* { struct vdat* curr_vdatp; */
512 /* struct model* curr_modelp; */
514 /* curr_vdatp = curr_vdat(); */
515 /* curr_modelp = curr_model(); */
517 /* curr_modelp->spritesheet[(int)direction].height = height; */
518 /* curr_modelp->spritesheet[(int)direction].width = width; */
519 /* curr_modelp->spritesheet[(int)direction].num_frames = num_frames; */
520 /* curr_vdatp->num_models++; */
524 /* insert_frame_pointer */
525 /* ( uint8_t direction, */
528 /* { struct model* curr_modelp; */
530 /* curr_modelp = curr_model(); */
532 /* curr_modelp->spritesheet[(int)direction].frames[curr_modelp->spritesheet[(int)direction].num_frames++] = frame; */