X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fir.c;h=0bdba39315462fbbbd12e49e40b249775b3c6407;hp=3f90e1875a58e7730c089904e8711068f02e709a;hb=e92cf135e461e07d723762aa4efb4bf7a0a17589;hpb=db01318a8e246249ceea23255a6512ea8a5c00c2 diff --git a/src/ir.c b/src/ir.c index 3f90e18..0bdba39 100644 --- a/src/ir.c +++ b/src/ir.c @@ -17,12 +17,11 @@ #include //u32_cpy #include //ulc_fprintf /* Local */ +#define eprintf_callback(...) exit(EXIT_FAILURE) #include "print.h" #include "apc.h" #include "ir.h" #include "pagenode.h" -#undef do_error -#define do_error(...) exit(-1) #define XXH_PRIVATE_API #include "../xxHash/xxhash.h" /* Public */ @@ -44,16 +43,15 @@ struct ir_classld_t }; struct ir_setld_t { struct ir_classld_t* classld; - long long ref; + uint32_t ref; struct ir_namelist_t* namelist, * namelist_head; }; struct ir_setdata_header_t { enum dtype type; uint8_t* src_filename, * data_name; union ir_setdata_t* nextsib; + long filepos; }; -struct ir_frameinfo_t -{ int facing, w, h; }; struct ir_framedata_t { struct ir_setdata_header_t header; struct ir_frameinfo_t frameinfo; @@ -66,8 +64,9 @@ struct ir_framebox_t struct ir_simplex_t { struct ir_setdata_header_t header; }; struct ir_link_t { struct ir_setdata_header_t header; - struct ir_classld_t* classld; struct ir_setld_t* setld; + struct ir_set_t* trg_set; + uint8_t* dlink; enum ltype type; }; union ir_setdata_t @@ -82,6 +81,7 @@ struct ir_class_t { struct ir_class_t* nextchild, * nextsib; struct ir_set_t* root_set; uint8_t* name; + long filepos; }; struct ir_set_t { struct ir_set_t* nextchild, * nextsib; @@ -90,7 +90,7 @@ struct ir_set_t struct ir_framebox_t* frameboxes; struct ir_simplex_t* audio; struct ir_link_t* links; - long filepos; + long filepos; }; /* Functions */ static inline @@ -98,6 +98,8 @@ struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,uint8_t*); static inline union ir_setdata_t* ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int); static inline +void ir_linkdata_resolve_set(union ir_setdata_t*); +static inline int bytes_identical(const uint8_t*,const uint8_t*); static inline int classnames_identical(const uint8_t*,const uint8_t*); @@ -110,10 +112,12 @@ uint8_t* classname_alloc(const uint8_t*); #define struct_alloc(_T) ((struct _T*) stack_alloc(&datapages, sizeof(struct _T))) extern //apc.c long sys_pagesize; +extern //apc.c +char* apc_package_name; static struct pagelist_t datapages, namepages, refhashpages; static -struct ir_class_t root_class = { .name = (uint8_t*)"." }; +struct ir_class_t root_class; /* Init */ int ir_init @@ -121,7 +125,7 @@ int ir_init { pagelist_init(datapages, (size_t)SYS_PAGESIZE); pagelist_init(namepages, (size_t)NAME_PAGESIZE); pagelist_init(refhashpages, (size_t)SYS_PAGESIZE); - + root_class.name = (uint8_t*) apc_package_name; return 0; } @@ -222,6 +226,11 @@ struct ir_set_t* ir_class_addset return iter->nextsib; } +/* Get the root set of the class */ +struct ir_set_t* ir_class_rootset +( struct ir_class_t* class ) +{ return class->root_set; } + struct ir_set_t* ir_set_from_ref ( uint32_t ref ) { uint16_t hash; @@ -330,6 +339,55 @@ int classnames_identical return (ca == cb); } +/* Return the name of the set */ +uint8_t* ir_set_name +( struct ir_set_t* set) +{ return set->name; } + +/* Return the next sib of the class */ +struct ir_class_t* ir_class_nextsib +( struct ir_class_t* class ) +{ return class->nextsib; } + +/* Return the next sib of the class */ +struct ir_class_t* ir_class_nextchild +( struct ir_class_t* class ) +{ return class->nextchild; } + +/* Get the file position of the class */ +long ir_class_fpos +( struct ir_class_t* class ) +{ return class->filepos; } + +/* Set the file position of the class */ +void ir_class_assign_fpos +( struct ir_class_t* class, + long newpos +) +{ class->filepos = newpos; } + +/* Get the next sibling of the provided set */ +struct ir_set_t* ir_set_nextsib +( struct ir_set_t* set ) +{ return set->nextsib; } + +/* Get the next child of the provided set */ +struct ir_set_t* ir_set_nextchild +( struct ir_set_t* set ) +{ return set->nextchild; } + +/* Get the file position of the class */ +long ir_set_fpos +( struct ir_set_t* set ) +{ return set->filepos; } + +/* Set the file position of the class */ +void ir_set_assign_fpos +( struct ir_set_t* set, + long newpos +) +{ set->filepos = newpos; } + /* Assign Setdata to Set */ void ir_set_assign_data ( struct ir_set_t* set, @@ -538,15 +596,272 @@ union ir_setdata_t* ir_link link = struct_alloc(ir_link_t); struct_clear(link); link->header.type = LDAT; - link->type = link_type; - link->classld = setld->classld; + link->type = link_type; link->setld = setld; if (link_type != OLINK && name != NULL) link->header.data_name = name_alloc(name); return (union ir_setdata_t*) link; } +/* Return a set's root framebox */ +union ir_setdata_t* ir_set_framebox +( struct ir_set_t* set ) +{ return (union ir_setdata_t*) set->frameboxes; } + +/* Return a set's root audio data */ +union ir_setdata_t* ir_set_audio +( struct ir_set_t* set ) +{ return (union ir_setdata_t*) set->audio; } + +/* Return a set's root link data */ +union ir_setdata_t* ir_set_link +( struct ir_set_t* set ) +{ return (union ir_setdata_t*) set->links; } + +#define assert_link(linkdata) if (DEBUG) { \ + if (linkdata->header.type != LDAT) \ + eprintf("Data %s is not a link\n", linkdata->header.data_name); \ + } + +/* Return the link type */ +enum ltype ir_linkdata_type +( union ir_setdata_t* linkdata ) +{ assert_link(linkdata); + return linkdata->link.type; +} + +/* Return the link type */ +uint32_t ir_linkdata_ref +( union ir_setdata_t* linkdata ) +{ assert_link(linkdata); + return linkdata->link.setld->ref; +} + +/* Return the current target set, resolving it first if not present */ +struct ir_set_t* ir_linkdata_set +( union ir_setdata_t* linkdata ) +{ assert_link(linkdata); + if (linkdata->link.trg_set == NULL) + ir_linkdata_resolve_set(linkdata); + return linkdata->link.trg_set; +} + +/* Resolve and assign the link's target set */ +static inline +void ir_linkdata_resolve_set +( union ir_setdata_t* linkdata ) +{ struct ir_class_t* class_iter; + struct ir_namelist_t* namelist_iter,* namelist_iter_last; + struct ir_setld_t* setld; + struct ir_classld_t* classld; + struct ir_set_t* set; + set = NULL; + class_iter = NULL; + assert_link(linkdata); + setld = linkdata->link.setld; + if (linkdata->link.setld == NULL) + eprintf("Link data is invalid\n"); + classld = setld->classld; + if (classld != NULL) + { namelist_iter = classld->namelist; + if (classld->root_class == NULL) + eprintf("No root class for classld\n"); + class_iter = classld->root_class->nextchild; + namelist_iter_last = NULL; + while (class_iter != NULL) + { if (classnames_identical(class_iter->name, namelist_iter->name)) + { if (namelist_iter == classld->namelist_head) + break; + class_iter = class_iter->nextchild; + namelist_iter_last = namelist_iter; + namelist_iter = namelist_iter->nextsib; + } + else + class_iter = class_iter->nextsib; + } + if (class_iter == NULL) + { if (namelist_iter_last) + eprintf("No such subclass \"%s\" of class \"%s\"\n", + namelist_iter->name, + namelist_iter_last->name); + else + { wprintf("No such class \"%s\"\n", namelist_iter->name); + return; + } + } + set = class_iter->root_set; + } + else + set = ir_set_from_ref(setld->ref); + if (set == NULL) + eprintf("Initial set resolution failed\n"); + namelist_iter = setld->namelist; + namelist_iter_last = NULL; + if (setld->namelist != NULL) + { while (set != NULL) + { if (bytes_identical(set->name, namelist_iter->name)) + { if (namelist_iter == setld->namelist_head) + break; + set = set->nextchild; + namelist_iter_last = namelist_iter; + namelist_iter = namelist_iter->nextsib; + } + else + set = set->nextsib; + } + if (set == NULL) + { if (namelist_iter_last) + eprintf("No such subset \"%s\" of set \"%s\"\n", + namelist_iter->name, + namelist_iter_last->name); + else + eprintf("No such set \"%s\" in class \"%s\"\n", + namelist_iter->name, + class_iter->name); + } + } + linkdata->link.trg_set = set; +} + +/* Assign a linkdatas trg_set */ +void ir_linkdata_assign_set +( union ir_setdata_t* link, struct ir_set_t* set ) +{ assert_link(link); + link->link.trg_set = set; +} + +/* Assign a linkdatas type */ +void ir_linkdata_assign_type +( union ir_setdata_t* link, enum ltype type ) +{ assert_link(link); + link->link.type = type; +} + +/* Get, or generate, the fully qualified name of the link's target set */ +uint8_t* +ir_linkdata_dlink_name +( union ir_setdata_t* link ) +{ struct ir_namelist_t* namelist_iter; + struct ir_setld_t* setld; + struct ir_classld_t* classld; + uint8_t* bytep; + size_t bytes; + char setpass; + uint8_t delimiter; + assert_link(link); + if (link->link.dlink != NULL) + return link->link.dlink; + bytes = 0; + setld = link->link.setld; + if (setld == NULL) + eprintf("No setld in dlink\n"); + classld = setld->classld; + if (classld == NULL) + eprintf("No classld in dlink\n"); + if (classld->root_class != NULL) + eprintf("Cannot dlink local class \"%s\"\n", classld->root_class->name); + namelist_iter = classld->namelist; + setpass = 0; + count_bytes_in_namelist: + while (namelist_iter != NULL) + { bytep = namelist_iter->name; + while (*bytep++); + bytes += (bytep - namelist_iter->name); + namelist_iter = namelist_iter->nextsib; + } + if (setpass == 0) + { setpass = 1; + namelist_iter = setld->namelist; + goto count_bytes_in_namelist; + } + bytes += 2; //trailing '\0' and preceding '.' + link->link.dlink = stack_alloc(&namepages, bytes); + bytes = 0; + link->link.dlink[bytes++] = '.'; //dlinks start with '.' + namelist_iter = classld->namelist; + setpass = 0; + delimiter = '-'; //class delimiter + copy_bytes_in_namelist: + while (namelist_iter != NULL) + { bytep = namelist_iter->name; + while (*bytep) + link->link.dlink[bytes++] = *bytep++; + link->link.dlink[bytes++] = delimiter; + namelist_iter = namelist_iter->nextsib; + } + if (setpass == 0) + { setpass = 1; + namelist_iter = setld->namelist; + delimiter = '_'; //set delimiter + link->link.dlink[bytes - 1] = delimiter; //overwrite last delimiter + goto copy_bytes_in_namelist; + } + link->link.dlink[bytes] = '\0'; //tailing '\0' null termination + return link->link.dlink; +} + +/* Get a setdata's next sibling */ +union ir_setdata_t* ir_setdata_nextsib +( union ir_setdata_t* setdata ) +{ return setdata->header.nextsib; } + +/* Get a setdata's name */ +uint8_t* ir_setdata_name +( union ir_setdata_t* setdata ) +{ return setdata->header.data_name; } + +/* Get a setdata's filename */ +uint8_t* ir_setdata_filename +( union ir_setdata_t* setdata ) +{ return setdata->header.src_filename; } + +/* Get a setdata's file position */ +long ir_setdata_fpos +( union ir_setdata_t* setdata ) +{ return setdata->header.filepos; } + +/* Set a setdata's file position */ +void ir_setdata_assign_fpos +( union ir_setdata_t* setdata, + long newpos +) +{ setdata->header.filepos = newpos; } + +/* Assign a setdatas name */ +void ir_setdata_assign_name +( union ir_setdata_t* setdata, uint8_t* name ) +{ setdata->header.data_name = name;} + +/* Return a framebox's specified framesheet */ +union ir_setdata_t* ir_framebox_framesheet +( union ir_setdata_t* fbox, + apc_facing facing +) +{ if (fbox->header.type != FBDAT) + eprintf("Data %s is not a framebox\n", fbox->header.data_name); + return (union ir_setdata_t*) &fbox->framebox.framesheets[facing]; +} + +/* Return a framebox's specified mapsheet */ +union ir_setdata_t* ir_framebox_mapsheet +( union ir_setdata_t* fbox, + apc_facing facing +) +{ if (fbox->header.type != FBDAT) + eprintf("Data %s is not a framebox\n", fbox->header.data_name); + return (union ir_setdata_t*) &fbox->framebox.mapsheets[facing]; +} + +/* Return a framedata's frame info */ +struct ir_frameinfo_t* ir_framedata_frameinfo +( union ir_setdata_t* framedata ) +{ if (framedata->header.type != MSDAT && framedata->header.type != FSDAT) + eprintf("Data %s is not a framedata\n", framedata->header.data_name); + return &framedata->mapsheet.frameinfo; +} + +/** Allocators **/ static uint8_t* name_alloc ( const uint8_t* name_src ) @@ -588,13 +903,15 @@ uint8_t* classname_alloc static void crawl_class(struct ir_class_t*); static void crawl_set(struct ir_set_t*,int); +extern +int binout_init(ir_class); void ir_test(void) { uprintf("IR From Directory: %s\n",getcwd(NULL,255)); crawl_class(&root_class); if (root_class.root_set != NULL) crawl_set(root_class.root_set, 0); uprintf("starting binaryout \n"); - ir_binout_init(&root_class); + binout_init(&root_class); }