3 #include <stdint.h> //uint64_t
4 #include <string.h> //memmove
5 #include <stdlib.h> //malloc
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 #define CURR_POST (post_buf[num_posts])
22 int curr_max_cdats
= PTRS_IN_PAGE
;
25 int curr_max_odats
= PTRS_IN_PAGE
;
29 int curr_max_vdats
= PTRS_IN_PAGE
;
32 int curr_max_refs
= PTRS_IN_PAGE
;
33 uint64_t ss_ref_id
= 0x00FFFFFF; /* system space for ref_ids */
36 int curr_max_links
= PTRS_IN_PAGE
;
39 int curr_max_posts
= PTRS_IN_PAGE
;
44 /* Init root cdat and stack */
45 char root
[4] = "root";
47 cdat_buf
[num_cdats
] = (struct cdat
*) malloc(sizeof(struct cdat
) );
48 cdat_buf
[num_cdats
]->idx
= 0;
49 memmove(cdat_buf
[num_cdats
]->name
, root
, 4);
51 cdat_stackp
= cdat_stack
;
52 *cdat_stackp
= cdat_buf
[num_cdats
++];
55 if( (CURR_ODAT
= (struct odat
*) malloc(sizeof(struct odat
))) == NULL
)
56 perror("malloc first odat failed");
59 if( (CURR_VDAT
= (struct vdat
*) malloc(sizeof(struct vdat
))) == NULL
)
60 perror("malloc first vdat failed");
63 if( (CURR_REF
= (struct ref
*) malloc(sizeof(struct ref
))) == NULL
)
64 perror("malloc first ref failed");
67 if( (CURR_LINK
= (struct link
*) malloc(sizeof(struct link
))) == NULL
)
68 perror("malloc first link failed");
71 if( (CURR_POST
= (struct ref
*) malloc(sizeof(struct ref
))) == NULL
)
72 perror("malloc first post failed");
80 if(curr_max_cdats
<= num_cdats
)
82 if( (realloc((void*) cdat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
83 perror("realloc cdat_buf failed");
84 curr_max_cdats
+= PTRS_IN_PAGE
;
85 if( (realloc( (void*) cdat_stack
, PTRS_IN_PAGE
* 4)) == NULL
) //increase cdat_stack also
86 perror("realloc cdat_stack failed");
88 if( (cdat_buf
[num_cdats
] = (struct cdat
*) malloc(sizeof (struct cdat
)) ) == NULL
)
89 perror("malloc cdat failed");
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*/
103 push_cdat(char* name
)
108 memmove(cdat_buf
[num_cdats
]->name
, name
, 32);
109 cdat_buf
[num_cdats
]->idx
= num_cdats
;
111 /* Set the cdat as a class of the previous cdat */
112 (*cdat_stackp
)->class_list
[(*cdat_stackp
)->num_classes
++] = cdat_buf
[num_cdats
];
114 /* Push the cdat onto the cdat_stack */
115 *++cdat_stackp
= cdat_buf
[num_cdats
++];
130 if(num_posts
>= curr_max_posts
)
132 if( (realloc((void*) post_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
133 perror("realloc post_buf failed");
134 curr_max_posts
+= PTRS_IN_PAGE
;
136 if( (CURR_POST
= (struct ref
*) malloc(sizeof (struct ref
))) == NULL
)
138 perror("malloc post failed");
146 if(num_odats
>= curr_max_odats
)
148 if( (realloc((void*) odat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
149 perror("realloc odat_buf failed");
150 curr_max_odats
+= PTRS_IN_PAGE
;
152 if( (CURR_ODAT
= (struct odat
*) malloc(sizeof (struct odat
))) == NULL
)
154 perror("malloc odat failed");
163 if(num_vdats
>= curr_max_vdats
)
165 if( (realloc((void*) vdat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
166 perror("realloc vdat_buf failed");
167 curr_max_vdats
+= PTRS_IN_PAGE
;
169 if((CURR_VDAT
= (struct vdat
*) malloc(sizeof (struct vdat
))) == NULL
)
171 perror("malloc vdat failed");
180 if(num_links
>= curr_max_links
)
182 if( (realloc((void*) link_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
183 perror("realloc vdat_buf failed");
184 curr_max_links
+= PTRS_IN_PAGE
;
186 if((CURR_LINK
= (struct link
*) malloc(sizeof (struct link
))) == NULL
)
188 perror("malloc link failed");
196 if(num_refs
% 16 == 0)
198 CURR_POST
= CURR_REF
;
203 if(num_refs
>= curr_max_refs
)
205 if( (realloc((void*) ref_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
206 perror("realloc ref_buf failed");
207 curr_max_refs
+= PTRS_IN_PAGE
;
209 if((CURR_REF
= (struct ref
*) malloc(sizeof (struct ref
))) == NULL
)
210 perror("malloc ref failed");
214 insert_set_label(char* name
, uint64_t ref_id
)
218 memmove(CURR_CDAT
->CURR_SET
.name
,name
,32);
219 memmove(&CURR_CDAT
->CURR_SET
.ref_id
,&ref_id
,64);
223 insert_set_olink(uint64_t ref_id
)
225 CURR_CDAT
->CURR_SET
.cdat_idx
= CURR_CDAT
->idx
;
226 CURR_CDAT
->CURR_SET
.ref_id
= ref_id
; /* Will be resolved to offset
227 when link is processed */
229 CURR_LINK
->link_t
.olink
.ref_id
= ref_id
;
230 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
231 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
++;
232 CURR_LINK
->ele_idx
= -1;
238 insert_set_vlink(uint64_t ref_id
, char* anim_name
)
240 /* Insert vlink into link_stack so that it gets processed at
242 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
243 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
245 CURR_LINK
->link_t
.vlink
.ref_id
= ref_id
;
246 memmove(CURR_LINK
->link_t
.vlink
.anim_name
, anim_name
, 32);
252 insert_set_svlink(uint64_t ref_id
)
255 /* Insert vlink into link_stack so that it gets processed at
257 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
258 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
260 CURR_LINK
->link_t
.svlink
.ref_id
= ref_id
;
264 /* At the point of reducing to a set, most of the
265 sets odat information has already been populated
266 during the reduction of its right hand side
267 non terminals (hitbox, root, quad_list). */
273 ref_id
= CURR_CDAT
->CURR_SET
.ref_id
;
275 CURR_CDAT
->CURR_SET
.cdat_idx
= CURR_CDAT
->idx
;
276 memmove(CURR_ODAT
->name
, CURR_CDAT
->CURR_SET
.name
, 32);
277 CURR_CDAT
->num_sets
++;
279 CURR_ODAT
->cdat_idx
= CURR_CDAT
->idx
;
280 CURR_ODAT
->refp
= CURR_REF
;
283 CURR_REF
->lastref
= PREV_REF
;
284 PREV_REF
->nextref
= CURR_REF
;
285 CURR_REF
->odatp
= CURR_ODAT
;
288 if(ref_id
== -1) /* user did not define a ref_id so */
289 { /* use a ref_id from system_space */
294 CURR_REF
->ref_id
= ref_id
;
305 PREV_ODAT
->vdat_id
= num_vdats
; //NULL for vlink, svlink
309 /* Populates both the odat name and ref_id
312 insert_ele_label(char* name
, uint64_t ref_id
)
314 memmove(CURR_CDAT
->CURR_SET
.CURR_ELE
.name
, name
, 32);
315 memmove(&CURR_CDAT
->CURR_SET
.ele_list
[CURR_CDAT
->CURR_SET
.ref_id
].ref_id
, &ref_id
, 64);
319 insert_ele_olink(uint64_t ref_id
)
321 CURR_CDAT
->CURR_SET
.CURR_ELE
.cdat_idx
= CURR_CDAT
->idx
;
322 CURR_CDAT
->CURR_SET
.CURR_ELE
.ref_id
= ref_id
; /* Will be resolved to offset
323 when link is processed */
325 CURR_LINK
->link_t
.olink
.ref_id
= ref_id
;
326 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
327 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
++;
328 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
++;
332 insert_ele_vlink(uint64_t ref_id
, char* anim_name
)
335 /* Insert vlink into link_stack so that it gets processed at
337 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
339 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
340 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
;
341 CURR_LINK
->link_t
.vlink
.ref_id
= ref_id
;
342 memmove(CURR_LINK
->link_t
.vlink
.anim_name
, anim_name
, 32);
347 insert_ele_svlink(uint64_t ref_id
)
350 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
352 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
353 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
;
354 CURR_LINK
->link_t
.svlink
.ref_id
= ref_id
;
358 //Insert element into odat_buf and cdatpages
365 ref_id
= CURR_CDAT
->CURR_SET
.CURR_ELE
.ref_id
;
367 CURR_CDAT
->CURR_SET
.CURR_ELE
.cdat_idx
= CURR_CDAT
->idx
;
368 memmove(CURR_ODAT
->name
,CURR_CDAT
->CURR_SET
.CURR_ELE
.name
, 32);
369 CURR_CDAT
->CURR_SET
.num_ele
++;
371 CURR_ODAT
->cdat_idx
= CURR_CDAT
->idx
;
372 CURR_ODAT
->refp
= CURR_REF
;
374 if(ref_id
== -1) /* user did not define a ref_id so */
375 { /* use a ref_id from system_space */
380 CURR_REF
->ref_id
= ref_id
;
388 insert_framesheet(char direction
, char* name
, uint64_t ref_id
, int height
, int width
, int num_frames
)
390 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].height
= height
;
391 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].width
= width
;
392 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].num_frames
= num_frames
;
393 CURR_VDAT
->num_models
++;
396 #define CURR_QUAD (CURR_ODAT->quad_list[CURR_ODAT->num_quads])
398 insert_quad(int x
, int y
, int z
, uint64_t ref_id
)
403 CURR_QUAD
.ref_id
= ref_id
;
404 CURR_ODAT
->num_quads
++;
407 /* Inserting the hitbox into the set
408 odat. Elements that don't have
409 a hitbox will use the sets root. */
411 insert_hitbox(int hitbox
)
413 CURR_ODAT
->hitbox
= hitbox
;
416 /* Inserting the root into the set
417 odat. Elements that don't have
418 a root will use the sets root. */
420 insert_root(int x
, int y
, int z
)
423 CURR_ODAT
->root
.x
= x
;
424 CURR_ODAT
->root
.y
= y
;
425 CURR_ODAT
->root
.z
= z
;
429 insert_frame_pointer(char direction
, void* frame
)
431 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].frames
[CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].num_frames
++] = frame
;