debug print formatting
[henge/apc.git] / src / ir.c
index f39f85b..a175ef3 100644 (file)
--- a/src/ir.c
+++ b/src/ir.c
 #include <unistd.h>   //u8_* functions\r
 #include <unitypes.h> //uint8_t as a char\r
 #include <unistr.h>   //u32_cpy\r
+#include <unistdio.h> //ulc_fprintf\r
 /* Local */\r
+#include "print.h"\r
 #include "apc.h"\r
 #include "ir.h"\r
+#undef  do_error\r
+#define do_error(...) exit(-1)\r
 /* Public */\r
 int      ir_init(void);\r
 void     ir_quit(void);\r
 int      ir_linker(void);\r
 int      ir_condenser(void);\r
 /* Memory allocation structures */\r
-enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT };\r
 struct pagenode_t;\r
 struct pagenode_header_t {\r
   struct pagenode_t* next;\r
@@ -37,6 +40,13 @@ struct pagelist_t {
   struct pagenode_t* root, * head;\r
   size_t             pagesize;\r
 };\r
+#define DATA_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
 struct ir_namelist_t\r
 { struct ir_namelist_t* nextsib;\r
@@ -51,7 +61,6 @@ struct ir_setld_t
   long long             ref;\r
   struct ir_namelist_t* namelist, * namelist_head;\r
 };\r
-/* Set data mem */\r
 struct ir_setdata_header_t\r
 { enum dtype          type;\r
   uint8_t*            src_filename, * data_name;\r
@@ -99,50 +108,26 @@ struct ir_set_t
 };\r
 /* Functions */\r
 static inline\r
-int                   init_pagelist(struct pagelist_t*,size_t);\r
-static inline\r
 struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,const uint8_t*);\r
+static inline\r
+union ir_setdata_t*   ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int);\r
+static inline\r
+int      init_pagelist(struct pagelist_t*,size_t);\r
 static\r
-void                  ir_free_pagenodes(struct pagenode_t*);\r
+void     ir_free_pagenodes(struct pagenode_t*);\r
 static inline\r
-int                   bytes_identical(const uint8_t*,const uint8_t*);\r
+int      bytes_identical(const uint8_t*,const uint8_t*);\r
+static inline\r
+int      classnames_identical(const uint8_t*,const uint8_t*);\r
 static\r
-void*                 stack_alloc(size_t);\r
+void*    stack_alloc(size_t);\r
+#define  struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T)))\r
 static\r
-uint8_t*              name_alloc(const uint8_t*);\r
-static inline\r
-union ir_setdata_t*   ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int);\r
-/* Function-Like Macros */\r
-#define do_warn() do {                         \\r
-  } while (0)\r
-#define wprint(str) do {                       \\r
-    fprintf(stderr, str);                      \\r
-    do_warn();                                 \\r
-  } while (0)\r
-#define wprintf(fmt,...) do {                  \\r
-    fprintf(stderr, fmt, __VA_ARGS__);         \\r
-    do_warn();                                 \\r
-  } while (0)\r
-#define do_error() do {                                \\r
-    exit(-1);                                  \\r
-  } while (0)\r
-#define eprint(str) do {                       \\r
-    fprintf(stderr, str);                      \\r
-    do_error();                                        \\r
-  } while (0)\r
-#define eprintf(fmt,...) do {                  \\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
-#define DATA_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
-/* Memory */\r
+uint8_t* name_alloc(const uint8_t*);\r
+static\r
+uint8_t* classname_alloc(const uint8_t*);\r
 extern //apc.c\r
-long sys_pagesize;\r
+long     sys_pagesize;\r
 static\r
 struct pagelist_t datapages, namepages;\r
 static\r
@@ -152,9 +137,9 @@ struct ir_class_t root_class = { .name = (uint8_t*)"." };
 int ir_init\r
 ( void )\r
 { if (init_pagelist(&datapages, (size_t)DATA_PAGESIZE))\r
-    eprint("Memory allocation error\n");\r
+    eprintf("Memory allocation error\n");\r
   if (init_pagelist(&namepages, (size_t)NAME_PAGESIZE))\r
-    eprint("Memory allocation error\n");\r
+    eprintf("Memory allocation error\n");\r
   return 0;\r
 }\r
 \r
@@ -201,6 +186,11 @@ int ir_condenser
 ( void )\r
 { return 0; }\r
 \r
+/* Return the class's name string */\r
+uint8_t* ir_class_name\r
+( struct ir_class_t* class )\r
+{ return class->name; }\r
+\r
 /* Return a pointer to the root class */\r
 struct ir_class_t* ir_class_root\r
 ( void )\r
@@ -215,7 +205,6 @@ struct ir_class_t* ir_class_addchild
   const uint8_t*     name\r
 )\r
 { struct ir_class_t* iter;\r
-  printf("Class %s, addchild %s\n", class->name, name);\r
   if (class->nextchild == NULL)\r
     goto alloc;\r
   iter = class->nextchild;\r
@@ -224,7 +213,7 @@ struct ir_class_t* ir_class_addchild
   if (name == NULL)\r
     eprintf("Null child added to class %s\n", iter->name);\r
  check:\r
-  if (bytes_identical(iter->name, name))\r
+  if (classnames_identical(iter->name, name))\r
     return iter;\r
   if (iter->nextsib != NULL)\r
     { iter = iter->nextsib;\r
@@ -233,7 +222,7 @@ struct ir_class_t* ir_class_addchild
  alloc:\r
   iter = struct_alloc(ir_class_t);\r
   iter->nextsib = class->nextchild;\r
-  iter->name = name_alloc(name);\r
+  iter->name = classname_alloc(name);\r
   return class->nextchild = iter;\r
 }\r
 \r
@@ -246,14 +235,13 @@ struct ir_set_t* ir_class_addset
   const uint8_t*     name\r
 )\r
 { struct ir_set_t* iter;\r
-  printf("Class %s, addset %s\n", class->name, name);\r
   if (class->root_set == NULL)\r
     goto alloc;\r
   iter = class->root_set;\r
   if (iter->name == NULL)\r
     eprintf("Null name pointer in class %p\n", iter);\r
   if (name == NULL)\r
-    eprintf("Null set added to class %s\n", iter->name);\r
+    eprintf("Null set added to class %U\n", iter->name);\r
  check:\r
   if (bytes_identical(iter->name, name))\r
     return iter;\r
@@ -277,7 +265,6 @@ struct ir_set_t* ir_set_addchild
   const uint8_t*   name\r
 )\r
 { struct ir_set_t* iter;\r
-  printf("Set %s, addchild %s\n", set->name, name);\r
   if (set->nextchild == NULL)\r
     goto alloc;\r
   iter = set->nextchild;\r
@@ -342,6 +329,19 @@ int bytes_identical
   return (ca == cb);\r
 }\r
 \r
+static inline\r
+int classnames_identical\r
+( const uint8_t* stra,\r
+  const uint8_t* strb\r
+)\r
+{ int ca, cb;\r
+  do {\r
+    ca = *stra++;\r
+    cb = *strb++;\r
+  } while (ca && ca == cb);\r
+  return (ca == cb);\r
+}\r
+\r
 /* Assign Setdata to Set\r
 \r
  */\r
@@ -443,7 +443,7 @@ union ir_setdata_t* ir_framedata
 )\r
 { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t);\r
   if (name == NULL)\r
-    eprint("Null name in set allocation\n");\r
+    eprintf("Null name in set allocation\n");\r
   framedata->header.type = type;\r
   framedata->header.data_name = name_alloc(name);\r
   framedata->frameinfo.facing = d;\r
@@ -456,7 +456,7 @@ union ir_setdata_t* ir_audio
 ( const uint8_t* name )\r
 { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t);\r
   if (name == NULL)\r
-    eprint("Null audio\n");\r
+    eprintf("Null audio\n");\r
   audio->header.type = ADAT;\r
   audio->header.data_name = name_alloc(name);\r
   return (union ir_setdata_t*) audio;\r
@@ -468,7 +468,7 @@ struct ir_classld_t* ir_classld_from_class
 ( struct ir_class_t* class )\r
 { struct ir_classld_t* classld;\r
   if (class == NULL)\r
-    eprint("Null class in classld\n");\r
+    eprintf("Null class in classld\n");\r
   classld = struct_alloc(ir_classld_t);\r
   classld->root_class = class;\r
   return classld;\r
@@ -536,7 +536,7 @@ void* stack_alloc
   if (PL_HEADMEM(datapages) < bytes) \r
     { datapages.head->header.next = (struct pagenode_t*) calloc(datapages.pagesize,1);\r
       if (datapages.head->header.next == NULL)\r
-       eprint("Memory allocation error \n");\r
+       eprintf("Memory allocation error \n");\r
       datapages.head = datapages.head->header.next; \r
       datapages.head->header.head = datapages.head->root;\r
     }\r
@@ -558,7 +558,30 @@ uint8_t* name_alloc
   if (head_mem == 0) //not enough room\r
     { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1);\r
       if (namepages.head->header.next == NULL)\r
-       eprint("Memory allocation error\n");\r
+       eprintf("Memory allocation error\n");\r
+      namepages.head = namepages.head->header.next;\r
+      namepages.head->header.head = namepages.head->root;\r
+      goto copy;\r
+    }\r
+  *(namepages.head->header.head)++ = '\0';\r
+  return name;\r
+}\r
+\r
+static\r
+uint8_t* classname_alloc\r
+( const uint8_t* name_src )\r
+{ const uint8_t* iter;\r
+  uint8_t*       name;\r
+  int            head_mem;\r
+ copy:\r
+  name = (uint8_t*)namepages.head->header.head;\r
+  iter = name_src;\r
+  for (head_mem = PL_HEADMEM(namepages); *iter && head_mem; head_mem--)\r
+    *(namepages.head->header.head)++ = *iter++;\r
+  if (head_mem == 0) //not enough room\r
+    { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1);\r
+      if (namepages.head->header.next == NULL)\r
+       eprintf("Memory allocation error\n");\r
       namepages.head = namepages.head->header.next;\r
       namepages.head->header.head = namepages.head->root;\r
       goto copy;\r