beginnings of binaryout
[henge/apc.git] / src / ir.c
index 6355654..3f90e18 100644 (file)
--- a/src/ir.c
+++ b/src/ir.c
@@ -10,6 +10,7 @@
 #include <stdarg.h> //va_args\r
 #include <stdint.h> //uint64_t\r
 #include <string.h> //memset, str*\r
+#include <errno.h>\r
 /* Unicode */\r
 #include <unistd.h>   //u8_* functions\r
 #include <unitypes.h> //uint8_t as a char\r
@@ -19,6 +20,7 @@
 #include "print.h"\r
 #include "apc.h"\r
 #include "ir.h"\r
+#include "pagenode.h"\r
 #undef  do_error\r
 #define do_error(...) exit(-1)\r
 #define XXH_PRIVATE_API\r
@@ -29,25 +31,6 @@ void     ir_quit(void);
 void     ir_test(void);\r
 int      ir_linker(void);\r
 int      ir_condenser(void);\r
-/* Memory allocation structures */\r
-struct pagenode_t;\r
-struct pagenode_header_t {\r
-  struct pagenode_t* next;\r
-  char*              head;\r
-};\r
-struct pagenode_t {\r
-  struct pagenode_header_t header;\r
-  char                     root[];\r
-};\r
-struct pagelist_t {\r
-  struct pagenode_t* root, * head;\r
-  size_t             pagesize;\r
-};\r
-#define SYS_PAGESIZE     (sys_pagesize)\r
-#define NAME_PAGESIZE    (APC_NAME_MAX * 1024)\r
-#define PL_HEADERSIZE    (sizeof(struct pagenode_header_t))\r
-#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root)\r
-#define PL_HEADMEM(_PL)  (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL))\r
 /* Set data mem */\r
 enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT };\r
 struct ir_namelist_t;\r
@@ -107,6 +90,7 @@ struct ir_set_t
   struct ir_framebox_t* frameboxes;\r
   struct ir_simplex_t*  audio;\r
   struct ir_link_t*     links;\r
+  long filepos;\r
 };\r
 /* Functions */\r
 static inline\r
@@ -118,37 +102,12 @@ int      bytes_identical(const uint8_t*,const uint8_t*);
 static inline\r
 int      classnames_identical(const uint8_t*,const uint8_t*);\r
 static\r
-void*    stack_alloc(size_t);\r
-#define  struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T)))\r
-#define  struct_clear(_S) (memset((_S), 0, sizeof(*(_S))))\r
-static\r
 uint8_t* name_alloc(const uint8_t*);\r
 static\r
 uint8_t* classname_alloc(const uint8_t*);\r
-static inline\r
-void*    pagelist_pop(struct pagelist_t*,size_t);\r
-#define $($)#$\r
-#define  pagelist_alloc(pagelist) do {                                 \\r
-    pagelist.head->header.next = (struct pagenode_t*) malloc(pagelist.pagesize); \\r
-    if (pagelist.head->header.next == NULL)                            \\r
-      eprintf("Memory allocation error\n");                            \\r
-    struct_clear(pagelist.head->header.next);                          \\r
-    pagelist.head = pagelist.head->header.next;                                \\r
-    pagelist.head->header.head = pagelist.head->root;                  \\r
-  } while (0)\r
-#define  pagelist_init(pagelist,size) do {                     \\r
-    pagelist.pagesize = size;                                  \\r
-    pagelist.root = (struct pagenode_t*) malloc(size);         \\r
-    if (pagelist.root == NULL)                                 \\r
-      eprintf("Memory allocation error\n");                    \\r
-    struct_clear(pagelist.root);                               \\r
-    pagelist.head = pagelist.root;                             \\r
-    pagelist.head->header.head = pagelist.head->root;          \\r
-  } while (0)\r
-static\r
-void     pagenode_free(struct pagenode_t*);\r
+#define  struct_clear(_S) (memset((_S), 0, sizeof(*(_S))))\r
 #define  REFHASH(ref) (XXH32(&ref, sizeof(uint32_t), 0xCEED) & 0xCFF)\r
-\r
+#define  struct_alloc(_T) ((struct _T*) stack_alloc(&datapages, sizeof(struct _T)))\r
 extern //apc.c\r
 long     sys_pagesize;\r
 static\r
@@ -162,6 +121,7 @@ int ir_init
 { pagelist_init(datapages, (size_t)SYS_PAGESIZE);\r
   pagelist_init(namepages, (size_t)NAME_PAGESIZE);\r
   pagelist_init(refhashpages, (size_t)SYS_PAGESIZE);\r
+\r
   return 0;\r
 }\r
 \r
@@ -171,15 +131,7 @@ void ir_quit
 { pagenode_free(datapages.root);\r
   pagenode_free(namepages.root);\r
   pagenode_free(refhashpages.root);\r
-}\r
 \r
-/* Recursively clean pagenode linked list, freeing last first */\r
-static\r
-void pagenode_free\r
-( struct pagenode_t* pagenode )\r
-{ if (pagenode->header.next != NULL)\r
-    pagenode_free(pagenode->header.next);\r
-  free(pagenode);\r
 }\r
 \r
 /* Link */\r
@@ -594,34 +546,6 @@ union ir_setdata_t* ir_link
   return (union ir_setdata_t*) link;\r
 }\r
 \r
-static inline\r
-void* pagelist_pop\r
-( struct pagelist_t* pagelist,\r
-  size_t             size\r
-)\r
-{ size_t headsize = PL_HEADSIZE((*pagelist));\r
-  if (!headsize)\r
-    { free(pagelist->head);\r
-      pagelist->head = pagelist->root;\r
-      while (pagelist->head->header.next != NULL)\r
-       pagelist->head = pagelist->head->header.next;\r
-    }\r
-  if (headsize < size)\r
-    eprintf("Attempted to pop unaligned value from pagelist\n");\r
-  pagelist->head->header.head -= size;\r
-  return pagelist->head->header.head;\r
-}\r
-\r
-static\r
-void* stack_alloc\r
-( size_t bytes )\r
-{ void* p;\r
-  if (PL_HEADMEM(datapages) < bytes) \r
-    pagelist_alloc(datapages);\r
-  p = datapages.head->header.head;\r
-  datapages.head->header.head += bytes;\r
-  return p;\r
-}\r
 \r
 static\r
 uint8_t* name_alloc\r
@@ -663,19 +587,23 @@ uint8_t* classname_alloc
 \r
 static void crawl_class(struct ir_class_t*);\r
 static void crawl_set(struct ir_set_t*,int);\r
+\r
 void     ir_test(void)\r
 { uprintf("IR From Directory: %s\n",getcwd(NULL,255));\r
   crawl_class(&root_class);\r
   if (root_class.root_set != NULL)\r
     crawl_set(root_class.root_set, 0);\r
+  uprintf("starting binaryout \n");\r
+  ir_binout_init(&root_class);\r
 }\r
 \r
+\r
 static\r
 void crawl_class\r
 ( struct ir_class_t* class )\r
 { struct ir_class_t* iter;\r
   for (iter = class->nextchild; iter != NULL; iter = iter->nextsib)\r
-    { wprintf("%U/\n", iter->name);\r
+    { wprintf("Crawling class %U/\n", iter->name);\r
       if(chdir((char*)iter->name))\r
        eprintf("CHDIR %U from %s\n",iter->name,getcwd(NULL,255));\r
       crawl_class(iter);\r
@@ -683,11 +611,12 @@ void crawl_class
         crawl_set(iter->root_set, 0);\r
       uprintf("%U\\\n",iter->name);\r
       if (chdir(".."))\r
-       eprintf("CHDIR ..\n");\r
+       eprintf("CHDIR ..\n");\r
+      wprintf("Finished crawling class %U/\n", iter->name);\r
     }\r
 }\r
 \r
-#define push_setp(setp) (*(struct ir_set_t**)stack_alloc(sizeof(struct ir_set_t*)) = setp)\r
+#define push_setp(setp) (*(struct ir_set_t**)stack_alloc(&datapages, sizeof(struct ir_set_t*)) = setp)\r
 #define pop_setp()      (*(struct ir_set_t**)pagelist_pop(&datapages, sizeof(struct ir_set_t*)))\r
 static\r
 void crawl_set\r
@@ -700,11 +629,13 @@ void crawl_set
   while (i--)\r
     putchar('.');\r
   i = depth;\r
+\r
   for(iter = set; iter != NULL; iter = iter->nextchild)\r
     { uprintf("[%10U]", iter->name);\r
       push_setp(iter);\r
       i++;\r
     }\r
+\r
   putchar('\n');\r
   while (--i >= depth)\r
     if (((iter = pop_setp())->nextsib) != NULL)\r