debugging support
[henge/apc.git] / src / ir.c
index b68142e..db97097 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,22 @@ 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\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
 extern //apc.c\r
-long sys_pagesize;\r
+long     sys_pagesize;\r
 static\r
 struct pagelist_t datapages, namepages;\r
 static\r
@@ -152,9 +133,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 +182,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
@@ -216,8 +202,12 @@ struct ir_class_t* ir_class_addchild
 )\r
 { struct ir_class_t* iter;\r
   if (class->nextchild == NULL)\r
-    return class->nextchild = struct_alloc(ir_class_t);\r
+    goto alloc;\r
   iter = class->nextchild;\r
+  if (iter->name == NULL)\r
+    eprintf("Null name pointer in class %p\n", iter);\r
+  if (name == NULL)\r
+    eprintf("Null child added to class %s\n", iter->name);\r
  check:\r
   if (bytes_identical(iter->name, name))\r
     return iter;\r
@@ -225,6 +215,7 @@ struct ir_class_t* ir_class_addchild
     { iter = iter->nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_class_t);\r
   iter->nextsib = class->nextchild;\r
   iter->name = name_alloc(name);\r
@@ -241,8 +232,12 @@ struct ir_set_t* ir_class_addset
 )\r
 { struct ir_set_t* iter;\r
   if (class->root_set == NULL)\r
-    return class->root_set = struct_alloc(ir_set_t);\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 %U\n", iter->name);\r
  check:\r
   if (bytes_identical(iter->name, name))\r
     return iter;\r
@@ -250,6 +245,7 @@ struct ir_set_t* ir_class_addset
     { iter = iter->nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_set_t);\r
   iter->nextsib = class->root_set;\r
   iter->name = name_alloc(name);\r
@@ -266,8 +262,12 @@ struct ir_set_t* ir_set_addchild
 )\r
 { struct ir_set_t* iter;\r
   if (set->nextchild == NULL)\r
-    return set->nextchild = struct_alloc(ir_set_t);\r
+    goto alloc;\r
   iter = set->nextchild;\r
+  if (iter->name == NULL)\r
+    eprintf("Null name pointer in set %p\n", iter);\r
+  if (name == NULL)\r
+    eprintf("Null child added to set %s\n", iter->name);\r
  check:\r
   if (bytes_identical(iter->name, name))\r
     return iter;\r
@@ -275,6 +275,7 @@ struct ir_set_t* ir_set_addchild
     { iter = iter->nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_set_t);\r
   iter->nextsib = set->nextchild;\r
   iter->name = name_alloc(name);\r
@@ -292,7 +293,7 @@ struct ir_framebox_t* ir_set_add_framebox
 )\r
 { struct ir_framebox_t* iter;\r
   if (set->frameboxes == NULL)\r
-    return set->frameboxes = struct_alloc(ir_framebox_t);\r
+    goto alloc;\r
   iter = set->frameboxes;\r
  check:\r
   if (bytes_identical(iter->header.data_name, name))\r
@@ -301,6 +302,7 @@ struct ir_framebox_t* ir_set_add_framebox
     { iter = (struct ir_framebox_t*) iter->header.nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_framebox_t);\r
   iter->header.nextsib = (union ir_setdata_t*) set->frameboxes;\r
   iter->header.data_name = name_alloc(name);\r
@@ -390,7 +392,9 @@ void ir_data_assign_path
 ( union ir_setdata_t* setdata,\r
   const uint8_t*      path\r
 )\r
-{ if (setdata->header.src_filename != NULL)\r
+{ if (path == NULL)\r
+    eprintf("Null path in data %s\n", setdata->header.data_name);\r
+  if (setdata->header.src_filename != NULL)\r
     wprintf("Path override: %s -> %s for setdata %s\n",\r
            setdata->header.src_filename, path, setdata->header.data_name);\r
   setdata->header.src_filename = name_alloc(path);\r
@@ -421,6 +425,8 @@ union ir_setdata_t* ir_framedata
   int            height\r
 )\r
 { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t);\r
+  if (name == NULL)\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
@@ -432,6 +438,8 @@ union ir_setdata_t* ir_framedata
 union ir_setdata_t* ir_audio\r
 ( const uint8_t* name )\r
 { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t);\r
+  if (name == NULL)\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
@@ -442,6 +450,8 @@ union ir_setdata_t* ir_audio
 struct ir_classld_t* ir_classld_from_class\r
 ( struct ir_class_t* class )\r
 { struct ir_classld_t* classld;\r
+  if (class == NULL)\r
+    eprintf("Null class in classld\n");\r
   classld = struct_alloc(ir_classld_t);\r
   classld->root_class = class;\r
   return classld;\r
@@ -509,7 +519,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
@@ -531,7 +541,7 @@ 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