-prev_ref(void);
-
-#define CURR_CDAT (*cdat_stackp)
-#define CURR_SET set_list[CURR_CDAT->num_sets]
-#define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
-#define PREV_REF (ref_buf[num_refs-1])
-#define CURR_REF (ref_buf[num_refs])
-#define PREV_ODAT (odat_buf[num_odats-1])
-#define CURR_ODAT (odat_buf[num_odats])
-#define CURR_VDAT (vdat_buf[num_vdats])
-#define PREV_VDAT (vdat_buf[num_vdats-1])
-#define CURR_MODEL model_list[CURR_VDAT->num_models]
-#define CURR_LINK (link_buf[num_links])
-#define CURR_POST (post_buf[num_posts])
-
-/* General: All information from the directory structure is stored in */
-/* five buffers that comprise the IR: cdat_buf, odat_buf, vdat_buf, ref_buf */
-/* and link_buf. Each buf corresponds to the data structure that it stores. */
-/* The storage techique for all bufs (except cdat) is the same. Each bufs member first */
-/* populates its struct and then allocates the space for the next member */
-/* and increments the buf index. This means that we have to allocate the */
-/* very first member of each buf at ir_init(), so that we don't segfault */
-/* as the first member attempts to access memory that its previous member */
-/* didn't allocate (because it doesnt exist). We access the buf members */
-/* through standard array indexing but conceal the tediousness of array */
-/* indexing with macros. E.g. without macros, acessing an elements name */
-/* member would look like (split up to not go over line char limit): */
-/* (*cdat_stackp)->set_list[(*cdat_stackp)->num_sets] */
-/* .ele_list[(*cdat_stackp)->set_list[(*cdat_stackp->num_sets)].num_ele].name */
-
-/* For cdats in cdat_buf, we allocate the memory for a cdat once a cdat
- is recognized in the grammar. Cdat_buf is different from the other bufs
- because cdats have a root cdat that all cdats are a subclass of. This root
- cdat can have a set_list like other cdats. */
-
-/* Elements: Ele stands for element and has two representations in the IR. */
-/* In the cdat_buf eles store their name, cdat_idx (their classes index in */
-/* the cdat_buf) and the ref_id (refer to ref ). In the odat_buf, eles store */
-/* their object data (odat). At output time, the ref_id is dereferenced to */
-/* determine the elements odat which is the data that the engine expects */
-/* from an element. */
-
-
-/* All bufs are of pointers to their respective structs. When a buf is full */
-/* (number of data structs pointers >= max number of data struct pointers), */
-/* we need to allocate a more pointers for that buf. Allocate these */
-/* pointers a page at a time (1024 = Page bytes (4096)/bytes per pointer(4)) */
-
-struct ele {
- char name[32];
- uint64_t ref_id;
- int cdat_idx;
-};
-
-/* Sets: The set is similar to the ele, but it contains a list of its */
-/* elements. The set is populated at parse time AFTER the elements are */
-/* populated, due to the nature of bottom up parsing. */
-
-struct set {
- char name[32];
- uint64_t ref_id;
- int cdat_idx;
- int num_ele;
- struct ele ele_list[MAX_ELES];
-};
-
-/* Cdats: A cdat is a class data structure. Cdats serve as the central */
-/* data types of the IR. At output, the cdat_buf is iterated through and */
-/* each is written to the output file. For each cdat, sets and element */
-/* ref_ids must be dereferenced to determine the odat information. Cdats */
-/* contain pointers to their subclasses so that the relationship between */
-/* classes can be determined, but the subclasses are not represented inside */
-/* of the cdat itself but rather in the subsequent cdats in cdat_buf. We */
-/* can determine the number of subclasses (the last index into cdat_buf */
-/* that represents a subclass of some arbitrary cdat) each cdat has by */
-/* incrementing num_classes during parse time. */
-/* TODO: Should classes point to their parent class? */
-
-struct cdat {
- char name[32];
- int idx;
- int num_classes;
- int num_sets;
- struct cdat* class_list[MAX_CLASSES];
- struct set set_list[MAX_SETS];
-};
-
-/* There are an unknown amount of cdats at compile time, so we maintain */
-/* a cdat_buf of cdat pointers that can be expanded as needed. */
-struct cdat* cdat_buf[PTRS_IN_PAGE];
-
-/* The cdat_stack is a stack pointers to cdat pointers, the top of which is
- the cdat that is currently being parsed. Whenever a new cdat is recognized
- by the grammar (CLOPEN), a cdat is pushed onto the cdat_stack, and we refer
- to this cdat through the macro CURR_CDAT. By keeping a cdat_stack, we have
- access to the current cdat so that the elements and sets can populate themselves
- in the cdat accordingly. */
-
-struct cdat* cdat_stack[PTRS_IN_PAGE];
-struct cdat** cdat_stackp;
+curr_ref(void);
+struct model*
+curr_model(void);
+void
+inc_posts(void);
+
+#define PAGES_PER_CHUNK 16
+
+//"type free" chunk stacking
+struct chunk_stack
+{ void* chunks[MAX_CHUNKS];
+ void* *csp; //chunk stack pointer
+ void* dsp[MAX_CHUNKS]; //dat stack pointer (per chunk)
+ int chunk_size; //size of a chunk (including its forfeited page)
+ int max_dats; //number of dats per chunk for this stack
+} ocs, vcs, ccs, rcs, lcs, pcs, mcs; //odat, vdat, cdat,map, ref, link, post stacks
+
+//type safety handled by macro expansion (do not call these directly from code, make dependent macros for access to these)
+#define CHUNKS_LEN(STACK) ((STACK).csp - (STACK).chunks)
+#define CURRENT_CHUNK(STACK) ((STACK).chunks[CHUNKS_LEN(STACK) - 1])
+#define CHUNKS_FULL(STACK) ( (STACK).csp >= \
+ (STACK).chunks + MAX_CHUNKS * (STACK).chunk_size)
+#define CURRENT_DSP(STACK,TYPE) ((TYPE*) ((STACK).dsp[CHUNKS_LEN(STACK) - 1]))
+#define DATA_FULL(STACK,TYPE) ((void*) CURRENT_DSP(STACK,TYPE) >= \
+ (CURRENT_CHUNK(STACK) + (STACK).chunk_size))
+#define CSP_PUSH(STACK) (*(++(STACK).csp) = malloc((STACK).chunk_size))
+#define CURRENT_DATP(STACK,TYPE) (((TYPE**)(STACK).dsp)[CHUNKS_LEN(STACK) - 1])
+#define PREVIOUS_DATP(STACK,TYPE) (((TYPE**)(STACK).dsp)[CHUNKS_LEN(STACK) - 2])
+#define ALLOC_DAT(STACK,TYPE) (++CURRENT_DATP(STACK,TYPE))
+#define INIT_STACK(STACK,TYPE) \
+ { int i; \
+ (STACK).chunk_size = PAGES_PER_CHUNK * pagesize; \
+ (STACK).max_dats = (STACK).chunk_size / sizeof (TYPE); \
+ CSP_PUSH(STACK); \
+ for( i = 0; i < MAX_CHUNKS; i++){ \
+ (STACK).dsp[i] += pagesize; \
+ } \
+ }
+//Stack-specific macros (called directly from code (safety enforcement)
+#define INIT_ODAT() (INIT_STACK(ocs, struct odat))
+#define CURRENT_ODAT() (CURRENT_DATP(ocs,struct odat))
+#define ODAT_FULL() (DATA_FULL(ocs,struct odat))
+#define ODAT_ALLOC() (ALLOC_DAT(ocs,struct odat))
+#define OCS_FULL() (CHUNKS_FULL(ocs))
+#define INIT_VDAT() (INIT_STACK(vcs, struct vdat))
+#define CURRENT_VDAT() (CURRENT_DATP(vcs,struct vdat))
+#define VDAT_FULL() (DATA_FULL(vcs,struct vdat))
+#define VDAT_ALLOC() (ALLOC_DAT(vcs,struct vdat))
+#define VCS_FULL() (CHUNKS_FULL(vcs))
+#define INIT_CDAT() (INIT_STACK(ccs, struct cdat))
+#define CURRENT_CDAT() (CURRENT_DATP(ccs,struct cdat))
+#define CDAT_FULL() (DATA_FULL(ccs, struct cdat))
+#define CDAT_ALLOC() (ALLOC_DAT(ccs, struct cdat))
+#define CCS_FULL() (CHUNKS_FULL(ccs))
+#define INIT_MAP() (INIT_STACK(mcs, struct map))
+#define CURRENT_MAP() (CURRENT_DATP(mcs, struct map))
+#define MAP_FULL() (DATA_FULL(mcs, struct map))
+#define MAP_ALLOC() (ALLOC_DAT(mcs, struct map))
+#define MCS_FULL() (CHUNKS_FULL(mcs))
+#define INIT_LINK() (INIT_STACK(lcs, struct link))
+#define CURRENT_LINK() (CURRENT_DATP(lcs,struct link))
+#define LDAT_FULL() (DATA_FULL(lcs, struct link))
+#define LDAT_ALLOC() (ALLOC_DAT(lcs, struct link))
+#define LCS_FULL() (CHUNKS_FULL(lcs))
+#define INIT_POST() (INIT_STACK(rcs, struct ref))
+#define CURRENT_POST() (CURRENT_DATP(pcs,struct ref))
+#define POST_FULL() (DATA_FULL(pcs,struct ref))
+#define POST_ALLOC() (ALLOC_DAT(pcs,struct ref))
+#define PCS_FULL() (CHUNKS_FULL(pcs))
+#define INIT_REF() (INIT_STACK(rcs, struct ref))
+#define CURRENT_REF() (CURRENT_DATP(rcs,struct ref))
+#define PREVIOUS_REF() (PREVIOUS_DATP(rcs, struct ref))
+#define REF_FULL() (DATA_FULL(rcs,struct ref))
+#define REF_ALLOC() (ALLOC_DAT(rcs,struct ref))
+#define RCS_FULL() (CHUNKS_FULL(rcs))
+//Metadata
+#define CURRENT_SET() (CURRENT_CDAT()->set_list[CURRENT_CDAT()->num_sets])
+//#define CURRENT_QUAD() (CURRENT_MAP()->quad_list[CURRENT_MAP()->num_quads])
+//#define CURRENT_MODEL() (CURRENT_VDAT()->model_list[CURRENT_VDAT()->num_models])
+
+
+
+long pagesize;
+