wip
authorJordan <jordanlavatai@gmail.com>
Wed, 11 Jan 2017 22:31:19 +0000 (14:31 -0800)
committerJordan <jordanlavatai@gmail.com>
Wed, 11 Jan 2017 22:31:19 +0000 (14:31 -0800)
src/ir.c
src/ir.h

index 49480ab..76c221d 100644 (file)
--- a/src/ir.c
+++ b/src/ir.c
 #include "ir.h"\r
 /* Public */\r
 int      ir_init(void);\r
 #include "ir.h"\r
 /* Public */\r
 int      ir_init(void);\r
+void     ir_quit(void);\r
 int      ir_linker(void);\r
 int      ir_condenser(void);\r
 int      ir_linker(void);\r
 int      ir_condenser(void);\r
-#if 0\r
-ir_class ir_class_findchild(ir_class, uint8_t*);\r
-ir_set   ir_class_rootset(ir_class);\r
-ir_class ir_class_nextsib(ir_class);\r
-ir_class ir_class_nextchild(ir_class);\r
-uint8_t* ir_class_name(ir_class);\r
-ir_set   ir_class_findset(ir_class,uint8_t*);\r
-ir_set   ir_set_findchild(ir_set, uint8_t*);\r
-ir_set   ir_set_nextsib(ir_set);\r
-ir_set   ir_set_nextchild(ir_set);\r
-ir_set   ir_set_findref(long long);\r
-uint8_t* ir_set_name(ir_set);\r
-#endif\r
 /* Private */\r
 /* Private */\r
+extern //apc.c\r
+long sys_pagesize;\r
+static\r
+\r
+static inline\r
+struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*, uint8_t*);\r
+static inline\r
+void                  ir_quit_r(struct pagenode_t*);\r
 static inline\r
 static inline\r
-uint8_t bytes_identical(uint8_t*,uint8_t*);\r
+int                   bytes_identical(uint8_t*,uint8_t*);\r
 \r
 /* Enumerated types */\r
 \r
 /* Enumerated types */\r
-enum dtype { FSDAT, MSDAT, ADAT, LDAT };\r
+enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT };\r
 enum ltype { OLINK, MLINK, VLINK, ALINK };\r
 /* Set data mem */\r
 enum ltype { OLINK, MLINK, VLINK, ALINK };\r
 /* Set data mem */\r
-struct sdat_header_t\r
-{ enum dtype type;\r
-  uint8_t* src_file_name;\r
-  uint8_t* data_name;\r
+struct ir_setdata_header_t\r
+{ enum dtype          type;\r
+  uint8_t*            src_filename, * data_name;\r
+  union ir_setdata_t* nextsib;\r
 };\r
 };\r
-struct frameinfo_t\r
-{ int facing, w, h;\r
+struct ir_frameinfo_t\r
+{ int facing, w, h; };\r
+struct ir_framedata_t\r
+{ struct ir_setdata_header_t header;\r
+  struct ir_frameinfo_t     frameinfo;\r
+}** framedatas;\r
+struct ir_framebox_t\r
+{ struct ir_setdata_header_t header;\r
+  struct ir_framedata_t      framesheets[FACING_MAX];\r
+  struct ir_framedata_t      mapsheets[FACING_MAX];\r
+}** frameboxes;\r
+struct ir_simplex_t\r
+{ struct ir_setdat_header_t header;\r
+}** simplexes;\r
+struct ir_link_t\r
+{ struct ir_setdat_header_t header;\r
+  struct ir_set_t*          src, * trg;\r
+  enum ltype                type;\r
+}** links;\r
+union ir_setdata_t\r
+{ struct ir_setdata_header_t  header;\r
+  struct ir_framebox_t        framebox;\r
+  struct ir_framedata_t       framesheet;\r
+  struct ir_framedata_t       mapsheet;\r
+  struct ir_simplex_t         audio;\r
+  struct ir_link_t            link;\r
 };\r
 };\r
-struct framedata_t\r
-{ struct sdat_header_t header;\r
-  struct frameinfo_t   frameinfo;\r
-} **framedatas;\r
-struct framebox_t\r
-{ struct framedata_t framesheets[FACE_MAX];\r
-  struct framedata_t mapsheets[FACE_MAX];\r
-  uint8_t            *data_name;\r
-} **frameboxes;\r
-struct simplex_t\r
-{ struct sdat_header_t header;\r
-} **simplexes;\r
-struct link_t\r
-{ struct sdat_header_t header;\r
-  struct set_t         *src, *trg;\r
-  enum ltype           type;\r
-} **links;\r
-union sdat_t\r
-{ struct sdat_header_t header;\r
-  struct framedata_t   framesheet;\r
-  struct framedata_t   mapsheet;\r
-  struct simplex_t     audio;\r
-  struct link_t        link;\r
-};\r
-struct ir_class\r
-{ struct class_t *parent, *nextchild, *nextsib;\r
-  struct set_t   *root_set;\r
-  uint8_t        *name;\r
-} **classes;\r
-struct ir_set\r
-{ struct set_t      *parent, *nextchild, *nextsib;\r
-  struct class_t    *class;\r
-  uint8_t           *name;\r
-  struct framebox_t **sprites;\r
-  struct framebox_t **maps;\r
-  struct simplex_t  **audio;\r
-  struct link_t     **links;\r
-} **sets;\r
+struct ir_class_t\r
+{ struct ir_class_t* nextchild, * nextsib;\r
+  struct ir_set_t*   root_set;\r
+  uint8_t*           name;\r
+}** classes;\r
+struct ir_set_t\r
+{ struct ir_set_t*    nextchild, * nextsib;\r
+  struct ir_class_t*  class;\r
+  long long           ref;\r
+  uint8_t*            name;\r
+  struct framebox_t*  frameboxes;\r
+  struct simplex_t*   audio;\r
+  struct link_t*      links;\r
+}** sets;\r
 /* Function-Like Macros */\r
 #define do_warn() do {                         \\r
   } while (0)\r
 /* Function-Like Macros */\r
 #define do_warn() do {                         \\r
   } while (0)\r
@@ -95,7 +91,7 @@ struct ir_set
     fprintf(stderr, str);                      \\r
     do_warn();                                 \\r
   } while (0)\r
     fprintf(stderr, str);                      \\r
     do_warn();                                 \\r
   } while (0)\r
-#define wprintf(fmt,...) do {                  \\r
+ #define wprintf(fmt,...) do {                 \\r
     fprintf(stderr, fmt, __VA_ARGS__);         \\r
     do_warn();                                 \\r
   } while (0)\r
     fprintf(stderr, fmt, __VA_ARGS__);         \\r
     do_warn();                                 \\r
   } while (0)\r
@@ -110,13 +106,48 @@ struct ir_set
     fprintf(stderr, fmt, __VA_ARGS__);         \\r
     do_error();                                        \\r
   } while (0)\r
     fprintf(stderr, fmt, __VA_ARGS__);         \\r
     do_error();                                        \\r
   } while (0)\r
+#define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T)))\r
+\r
 \r
 static\r
 \r
 static\r
-struct ir_class root_class = { .name = &"." };\r
+struct ir_class_t root_class = { .name = "." };\r
 \r
 /* Init */\r
 int ir_init\r
 ( void )\r
 \r
 /* Init */\r
 int ir_init\r
 ( void )\r
+{ pagenode_root = calloc(struct pagenode_t*) calloc(PN_ALLOCSIZE);\r
+  if (pagenode_root == NULL)\r
+    return -1;\r
+  pagenode_root->head = pagenode_root->root;\r
+  pagenode_head = pagenode_root;\r
+  return 0;\r
+} \r
+\r
+/* Quit/Cleanup\r
+   Recursively clean pagenode linked list\r
+*/\r
+void ir_quit\r
+( void )\r
+{ ir_quit_r(pagenode_root); }\r
+\r
+static inline\r
+void ir_quit_r\r
+( struct pagenode_t* pagenode )\r
+{ if (pagenode->next != NULL)\r
+    ir_quit_r(pagenode->next);\r
+  free(pagenode);\r
+}\r
+\r
+/* Link\r
+*/\r
+int ir_linker\r
+( void )\r
+{ return 0; }\r
+\r
+/* Condense\r
+*/\r
+int ir_condenser\r
+( void )\r
 { return 0; }\r
 \r
 /* Return a pointer to the root class */\r
 { return 0; }\r
 \r
 /* Return a pointer to the root class */\r
@@ -125,70 +156,99 @@ struct ir_class_t* ir_class_root
 { return &root_class; }\r
 \r
 /* Add a subclass to a class\r
 { return &root_class; }\r
 \r
 /* Add a subclass to a class\r
-   Attempts to create a new subclass in the provided class, exiting with an\r
-   error if the class already exists\r
+   Attempts to create a new subclass in the provided class, returning\r
+   the class if it already exists\r
 */\r
 */\r
-#define ERR_DUPECLASS "Subclass %s of class %s already exists!", name, *class.name\r
 struct ir_class_t* ir_class_addchild\r
 ( struct ir_class_t* class,\r
   uint8_t*           name\r
 )\r
 struct ir_class_t* ir_class_addchild\r
 ( struct ir_class_t* class,\r
   uint8_t*           name\r
 )\r
-{ struct ir_class_t* iter = *class.nextchild;\r
-  if (iter == NULL)\r
-    return *class.nextchild = class_alloc(name);\r
- iterate:\r
-  if (bytes_identical(*iter.name, name))\r
-    { fprintf(stderr, ERR_DUPECLASS);\r
-      exit(-1);\r
-    }\r
-  if (*iter.nextsib != NULL)\r
-    { iter = *iter.nextsib;\r
-      goto iterate;\r
+{ struct ir_class_t* iter;\r
+  if (class->nextchild == NULL)\r
+    return class->nextchild = struct_alloc(ir_class_t);\r
+  iter = class->nextchild;\r
+ check:\r
+  if (bytes_identical(iter->name, name))\r
+    return iter;\r
+  if (iter->nextsib != NULL)\r
+    { iter = iter->nextsib;\r
+      goto check;\r
     }\r
     }\r
-  return *iter.nextsib = class_alloc(name);\r
-\r
+  return iter->nextsib = struct_alloc(ir_class_t);\r
 }\r
 \r
 }\r
 \r
-/* Return a pointer to the parent of the provided class */\r
-struct ir_class_t* ir_class_parent\r
-( struct ir_class_t* class )\r
-{ return class.parent; }\r
-\r
 /* Add a set to a class\r
 /* Add a set to a class\r
-   Attempts to create a new root set in the specified class, exiting with an\r
-   error if the set already exists\r
+   Attempts to create a new root set in the specified class, returning\r
+   the set if it already exists\r
 */\r
 */\r
-#define ERR_DUPESET "Root set %s of class %s already exists!", name, *class.name\r
 struct ir_set_t* ir_class_addset\r
 ( struct ir_class_t* class,\r
   uint8_t*           name\r
 )\r
 struct ir_set_t* ir_class_addset\r
 ( struct ir_class_t* class,\r
   uint8_t*           name\r
 )\r
-{ struct ir_set_t* iter = *class.root_set;\r
-  if (iter == NULL)\r
-    return *class.root_set = set_alloc(name);\r
- iterate:\r
-  if (bytes_identical(*iter.name, name))\r
-    { fprintf(stderr, ERR_DUPSET);\r
-      exit(-1);\r
+{ struct ir_set_t* iter;\r
+  if (class->root_set == NULL)\r
+    return class->root_set = struct_alloc(ir_set_t);\r
+  iter = class->root_set;\r
+ check:\r
+  if (bytes_identical(iter->name, name))\r
+    return iter;\r
+  if (iter->nextsib != NULL)\r
+    { iter = iter->nextsib;\r
+      goto check;\r
     }\r
     }\r
-  if (*iter.nextsib != NULL)\r
-    { iter = *iter.nextsib;\r
-      goto iterate;\r
-    }\r
-  return *iter.nextsib = set_alloc(name);\r
+  return iter->nextsib = struct_alloc(ir_set_t);\r
 }\r
 \r
 }\r
 \r
+/* Add a set to a set\r
+   Attempts to create a new subset of the specified set, returning the\r
+   child if it already exists\r
+*/\r
 struct ir_set_t* ir_set_addchild\r
 ( struct ir_set_t* set,\r
   uint8_t*         name\r
 )\r
 struct ir_set_t* ir_set_addchild\r
 ( struct ir_set_t* set,\r
   uint8_t*         name\r
 )\r
-{ }\r
+{ struct ir_set_t* iter;\r
+  if (set->nextchild == NULL)\r
+    return set->nextchild = struct_alloc(ir_set_t);\r
+  iter = set->nextchild;\r
+ check:\r
+  if (bytes_identical(iter->name, name))\r
+    return iter;\r
+  if (iter->nextsib != NULL)\r
+    { iter = iter->nextsib;\r
+      goto check;\r
+    }\r
+  return iter->nextsib = struct_alloc(ir_set_t);\r
+}\r
+\r
+/* Add a framebox to a set\r
+   Attempts to create a new framebox of the specified set, returning\r
+   the framebox if it already exists\r
+*/\r
+static inline\r
+struct ir_framebox_t* ir_set_add_framebox\r
+( struct ir_set_t* set,\r
+  uint8_t*         name\r
+)\r
+{ struct ir_framebox_t* iter;\r
+  if (set->frameboxes == NULL)\r
+    return set->frameboxes = struct_alloc(ir_framebox_t);\r
+  iter = set->frameboxes;\r
+ check:\r
+  if (bytes_identical(iter->header.name, name))\r
+    return iter;\r
+  if (iter->header.nextsib != NULL)\r
+    { iter = iter->header.nextsib;\r
+      goto check;\r
+    }\r
+  return iter->header.nextsib = struct_alloc(ir_framebox_t);\r
+}\r
 \r
 /* Match two null-terminated bytestrings\r
    Return 1 if the two bytestrings are identical, else 0\r
 */\r
 static inline\r
 \r
 /* Match two null-terminated bytestrings\r
    Return 1 if the two bytestrings are identical, else 0\r
 */\r
 static inline\r
-uint8_t bytes_identical\r
+int bytes_identical\r
 ( uint8_t* stra,\r
   uint8_t* strb\r
 )\r
 ( uint8_t* stra,\r
   uint8_t* strb\r
 )\r
@@ -197,3 +257,151 @@ uint8_t bytes_identical
       return 0;\r
   return *stra == *strb;\r
 }\r
       return 0;\r
   return *stra == *strb;\r
 }\r
+\r
+/* Assign Setdata to Set\r
+\r
+ */\r
+void ir_set_assign_data\r
+( struct ir_set_t*      set,\r
+  union  ir_setdata_t*  setdata\r
+)\r
+{ struct ir_framebox_t* framebox;\r
+  struct ir_simplex_t*  simplex;\r
+  struct ir_link_t*     link;\r
+  switch (setdata->header.type)\r
+    { case FSDAT:\r
+       framebox = ir_set_add_framebox(set, setdata->header.name);\r
+       if (framebox->framesheets[setdata->framesheet.frameinfo.facing] != NULL)\r
+         wprintf("Duplicate framesheet [%i] %s\n",\r
+                 setdata->framesheet.frameinfo.facing, setdata->header.data_name);\r
+       framebox->framesheets[setdata->framesheet.frameinfo.facing] = setdata->framesheet;\r
+       break;\r
+      case MSDAT:\r
+       framebox = ir_set_add_framebox(set, setdata->header.name);\r
+       if (framebox->mapsheets[setdata->mapsheet.frameinfo.facing] != NULL)\r
+         wprintf("Duplicate mapsheet [%i] %s\n",\r
+                 setdata->mapsheet.frameinfo.facing, setdata->header.data_name);\r
+       framebox->mapsheets[setdata->mapsheet.frameinfo.facing] = setdata->mapsheet;\r
+       break;\r
+      case ADAT:\r
+       if (set->audio == NULL)\r
+         { set->audio = &setdata->audio;\r
+           return;\r
+         }\r
+       simplex = set->audio;\r
+       while (simplex->header.nextsib != NULL;)\r
+         if (bytes_identical(simplex->header.data_name, setdata->header.data_name))\r
+           { wprintf("Duplicate audio %s\n", setdata->header.data_name);\r
+             *simplex = setdata->audio;\r
+             return;\r
+           }\r
+         else\r
+           simplex = simplex->header.nextsib;\r
+       simplex->header.nextsib = &setdata->audio;\r
+       break;\r
+      case LDAT:\r
+       if (set->links == NULL)\r
+         { set->links = &setdata->link;\r
+           return;\r
+         }\r
+       link = set->links;\r
+       while (link->header.nextsib != NULL)\r
+         link = link->header.nextsib;\r
+       link->header.nextsib = &setdata->link;\r
+       break;\r
+      default:\r
+       fprintf(stderr, "Unknown setdata type %x\n", setdata->header.type);\r
+       exit(-1);\r
+    }\r
+}\r
+\r
+void ir_set_assign_ref\r
+( ir_set_t* set,\r
+  long long ref\r
+)\r
+{ if (set->ref != 0)\r
+    wprintf("Ref override: 0x%x -> 0x%x for set %s\n",\r
+           set->ref, ref, set->name);\r
+  set->ref = ref;\r
+  //TODO: reflist_add(set);\r
+}\r
+\r
+void ir_data_assign_path\r
+( union ir_setdata_t* setdata,\r
+  uint8_t*            path\r
+)\r
+{ if (set->header.src_filename)\r
+    wprintf("Path override: %s -> %s for set %s\n",\r
+           set->header.src_filename, path, set->name);\r
+  set->header.src_filename = path;\r
+  //TODO: internal strdup, not assign (= path;)\r
+}\r
+\r
+\r
+//TODO: Macro ir_framsheet and mapsheet?\r
+union ir_setdata_t* ir_framesheet\r
+( uint8_t* name,\r
+  apc_facing d,\r
+  int width,\r
+  int height\r
+)\r
+{ struct ir_framedata_t* framesheet = struct_alloc(ir_framedata_t);\r
+  framesheet->header->type = FSDAT;\r
+  framesheet->header.data_name = name;\r
+  framesheet->frameinfo.facing = d;\r
+  framesheet->frameinfo.w = width;\r
+  framesheet->frameinfo.h = height;\r
+  return (union ir_setdata_t*) framesheet; \r
+}\r
+\r
+union ir_setdata_t* ir_mapsheet\r
+( uint8_t* name,\r
+  apc_facing d,\r
+  int width,\r
+  int height,\r
+)\r
+{ struct ir_framedata_t* mapsheet = struct_alloc(ir_framedata_t);\r
+  mapsheet->header.type = MSDAT;\r
+  mapsheet->header.data_name = name;\r
+  mapsheet->frameinfo.facing = d;\r
+  mapsheet->frameinfo.w = width;\r
+  mapsheet->frameinfo.h = height;\r
+  return (union ir_setdata_t*) mapsheet;\r
+}\r
+\r
+union ir_setdata_t* ir_audio\r
+( uint8_t* name )\r
+{ struct ir_simplex_t* audio = struct_alloc(ir_simplex_t);\r
+  audio->header.type = ADAT;\r
+  audio->header.data_name = name;\r
+  return (union ir_setdata_t*) audio;\r
+}\r
+\r
+\r
+#define PN_ALLOCSIZE    (sys_pagesize)\r
+#define PN_HEADERSIZE() (sizeof(struct pagenode_t*) + sizeof(void*))\r
+#define PN_MEMSIZE()    (PN_ALLOCSIZE - PN_HEADERSIZE())\r
+#define PN_HEADSIZE()   (pagenode_head->head - pagenode_head->root)\r
+#define PN_HEADSPACE()  (PN_MEMSIZE() - PN_HEADSIZE())\r
+struct pagenode_t {\r
+  struct pagenode_t* next;\r
+  char*              head;\r
+  char               root[];\r
+}* pagenode_root, * pagenode_head;\r
+\r
+\r
+static\r
+void* stack_alloc(size_t bytes)\r
+{ if (!bytes)\r
+    { wprint("Attempting to allocate 0 bytes in stack_alloc");\r
+      return pagenode_head->head;\r
+    }\r
+  if (PN_HEADSPACE() < bytes) \r
+    { pagenode_head->next = (struct pagenode_t*) calloc(PN_ALLOCSIZE);\r
+      pagenode_head = pagenode_head->next;\r
+      pagenode_head->head = pagenode_head->root;\r
+    }\r
+  pagenode_head->head += bytes;\r
+  return (void*) pagenode_head->head - bytes;\r
+}\r
+\r
index 1a7d1a7..7c1f4b3 100644 (file)
--- a/src/ir.h
+++ b/src/ir.h
@@ -20,9 +20,9 @@
 #ifndef _IR_H_\r
 #define _IR_H_\r
 #include <unitypes.h>\r
 #ifndef _IR_H_\r
 #define _IR_H_\r
 #include <unitypes.h>\r
-enum frame_facing { SFACE, SWFACE, WFACE, NWFACE, NFACE, NEFACE, EFACE, SEFACE, FACING_MAX };\r
-typedef enum frame_facing    ir_facing;\r
-typedef union ir_set_data_t* ir_setdata;\r
+#include "apc.h"\r
+\r
+typedef union ir_setdata_t*  ir_setdata;\r
 typedef struct ir_set_t*     ir_set;\r
 typedef struct ir_class_t*   ir_class;\r
 typedef struct ir_setld_t*   ir_setld;\r
 typedef struct ir_set_t*     ir_set;\r
 typedef struct ir_class_t*   ir_class;\r
 typedef struct ir_setld_t*   ir_setld;\r
@@ -65,8 +65,8 @@ ir_set     ir_set_addchild(ir_set,uint8_t*);
 void       ir_set_assign_data(ir_set,ir_setdata);\r
 void       ir_set_assign_ref(ir_set,long long);\r
 void       ir_data_assign_path(ir_setdata,uint8_t*);\r
 void       ir_set_assign_data(ir_set,ir_setdata);\r
 void       ir_set_assign_ref(ir_set,long long);\r
 void       ir_data_assign_path(ir_setdata,uint8_t*);\r
-ir_setdata ir_framesheet(uint8_t*,ir_facing,int,int);\r
-ir_setdata ir_mapsheet(uint8_t*,ir_facing,int,int);\r
+ir_setdata ir_framesheet(uint8_t*, apc_facing, int,int);\r
+ir_setdata ir_mapsheet(uint8_t*, apc_facing, int,int);\r
 ir_setdata ir_audio(uint8_t*);\r
 ir_setdata ir_link_odat(ir_setld);\r
 ir_setdata ir_link_vdat(ir_setld,uint8_t*);\r
 ir_setdata ir_audio(uint8_t*);\r
 ir_setdata ir_link_odat(ir_setld);\r
 ir_setdata ir_link_vdat(ir_setld,uint8_t*);\r