Move fatal(), warning() and aprintf() into the new data module (because they're
authorDavid Given <dg@cowlark.com>
Fri, 30 Sep 2016 17:10:30 +0000 (19:10 +0200)
committerDavid Given <dg@cowlark.com>
Fri, 30 Sep 2016 17:10:30 +0000 (19:10 +0200)
really useful).

mach/proto/mcg/main.c
mach/proto/mcg/mcg.h
mach/proto/mcg/table
modules/src/data/astring.c [new file with mode: 0644]
modules/src/data/astring.h [new file with mode: 0644]
modules/src/data/diagnostics.c [new file with mode: 0644]
modules/src/data/diagnostics.h [new file with mode: 0644]
modules/src/data/imap.c [new file with mode: 0644]
modules/src/data/imap.h [new file with mode: 0644]
util/mcgg/iburg.c

index c17b60b..5d450bd 100644 (file)
@@ -1,38 +1,5 @@
 #include "mcg.h"
 
-void fatal(const char* msg, ...)
-{
-       va_list ap;
-       va_start(ap, msg);
-
-       vfprintf(stderr, msg, ap);
-       fprintf(stderr, "\n");
-
-       va_end(ap);
-       abort();
-}
-
-const char* aprintf(const char* fmt, ...)
-{
-    int n;
-    char* p;
-    va_list ap;
-
-    va_start(ap, fmt);
-    n = vsnprintf(NULL, 0, fmt, ap) + 1;
-    va_end(ap);
-
-    p = malloc(n);
-    if (!p)
-        return NULL;
-
-    va_start(ap, fmt);
-    vsnprintf(p, n, fmt, ap);
-    va_end(ap);
-
-    return p;
-}
-
 bool tracing(char k)
 {
     switch (k)
index a836ecc..176efb5 100644 (file)
@@ -18,6 +18,8 @@
 #include "em_ptyp.h"
 #include "array.h"
 #include "map.h"
+#include "diagnostics.h"
+#include "astring.h"
 #include "ir.h"
 #include "mcgg.h"
 #include "hop.h"
@@ -88,7 +90,6 @@ struct basicblock
     bool is_terminated : 1;
 };
 
-extern void fatal(const char* s, ...);
 extern const char* aprintf(const char* fmt, ...);
 extern void tracef(char k, const char* fmt, ...);
 extern bool tracing(char k);
index 4126b2f..eac421d 100644 (file)
@@ -1,28 +1,28 @@
 REGISTERS
 
-       r0  reg4 int ret0;
-       r1  reg4 int ret1;
-       r2  reg4 int;
-       r3  reg4 int;
-       r4  reg4 int;
-       r5  reg4 int;
-       r6  reg4 int;
-       r7  reg4 int;
-       r8  reg4 int;
-       r9  reg4 int;
-       r10 reg4 int;
-       r11 reg4 int;
-
-       s0  reg4 float;
-       s1  reg4 float;
-       s2  reg4 float;
-       s3  reg4 float;
-       s4  reg4 float;
-       s5  reg4 float;
-       s6  reg4 float;
-       s7  reg4 float;
-       s8  reg4 float;
-       s9  reg4 float;
+       r0  reg4 i ret0;
+       r1  reg4 i;
+       r2  reg4 i;
+       r3  reg4 i;
+       r4  reg4 i;
+       r5  reg4 i;
+       r6  reg4 i;
+       r7  reg4 i;
+       r8  reg4 i;
+       r9  reg4 i;
+       r10 reg4 i;
+       r11 reg4 i;
+
+       s0  reg4 f;
+       s1  reg4 f;
+       s2  reg4 f;
+       s3  reg4 f;
+       s4  reg4 f;
+       s5  reg4 f;
+       s6  reg4 f;
+       s7  reg4 f;
+       s8  reg4 f;
+       s9  reg4 f;
 
        cc  conditioncode;
 
diff --git a/modules/src/data/astring.c b/modules/src/data/astring.c
new file mode 100644 (file)
index 0000000..7da06e9
--- /dev/null
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+const char* aprintf(const char* fmt, ...)
+{
+    int n;
+    char* p;
+    va_list ap;
+
+    va_start(ap, fmt);
+    n = vsnprintf(NULL, 0, fmt, ap) + 1;
+    va_end(ap);
+
+    p = malloc(n);
+    if (!p)
+        return NULL;
+
+    va_start(ap, fmt);
+    vsnprintf(p, n, fmt, ap);
+    va_end(ap);
+
+    return p;
+}
+
+/* vim: set sw=4 ts=4 expandtab : */
+
diff --git a/modules/src/data/astring.h b/modules/src/data/astring.h
new file mode 100644 (file)
index 0000000..483c028
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef ASTRING_H
+#define ASTRING_H
+
+extern const char* aprintf(const char* fmt, ...);
+
+#endif
+
+/* vim: set sw=4 ts=4 expandtab : */
+
diff --git a/modules/src/data/diagnostics.c b/modules/src/data/diagnostics.c
new file mode 100644 (file)
index 0000000..32ac6a0
--- /dev/null
@@ -0,0 +1,38 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include "diagnostics.h"
+
+const char* program_name = NULL;
+
+void warning(const char* msg, ...)
+{
+       va_list ap;
+       va_start(ap, msg);
+
+       if (program_name)
+               fprintf(stderr, "%s: ", program_name);
+       fprintf(stderr, "warning: ");
+       vfprintf(stderr, msg, ap);
+       fprintf(stderr, "\n");
+
+       va_end(ap);
+}
+
+void fatal(const char* msg, ...)
+{
+       va_list ap;
+       va_start(ap, msg);
+
+       if (program_name)
+               fprintf(stderr, "%s: ", program_name);
+       fprintf(stderr, "fatal: ");
+       vfprintf(stderr, msg, ap);
+       fprintf(stderr, "\n");
+
+       va_end(ap);
+       abort();
+}
+
+/* vim: set sw=4 ts=4 expandtab : */
+
diff --git a/modules/src/data/diagnostics.h b/modules/src/data/diagnostics.h
new file mode 100644 (file)
index 0000000..7c2908f
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef DIAGNOSTICS_H
+#define DIAGNOSTICS_H
+
+extern const char* program_name;
+
+extern void warning(const char* fmt, ...);
+extern void fatal(const char* fmt, ...);
+
+#endif
+
+/* vim: set sw=4 ts=4 expandtab : */
+
diff --git a/modules/src/data/imap.c b/modules/src/data/imap.c
new file mode 100644 (file)
index 0000000..15c66e7
--- /dev/null
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include "imap.h"
+
+static void append(void* mapp, int left, void* right)
+{
+    struct imap* map = mapp;
+    struct imap_node* node;
+
+       if (map->count == map->max)
+       {
+               int newmax = (map->max == 0) ? 8 : (map->max * 2);
+               struct imap_node* newarray = realloc(map->item, newmax * sizeof(*newarray));
+
+               map->max = newmax;
+               map->item = newarray;
+       }
+
+    node = &map->item[map->count];
+    map->count++;
+
+    node->left = left;
+    node->right = right;
+}
+
+void imap_put(void* mapp, int left, void* right)
+{
+    struct imap* map = mapp;
+    int i;
+
+    for (i=0; i<map->count; i++)
+    {
+        struct imap_node* node = &map->item[i];
+        if (node->left == left)
+        {
+            node->right = right;
+            return;
+        }
+    }
+
+    append(map, left, right);
+}
+
+void imap_add(void* mapp, int left, void* right)
+{
+    struct imap* map = mapp;
+    int i;
+
+    for (i=0; i<map->count; i++)
+    {
+        struct imap_node* node = &map->item[i];
+        if ((node->left == left) && (node->right == right))
+            return;
+    }
+
+    append(map, left, right);
+}
+
diff --git a/modules/src/data/imap.h b/modules/src/data/imap.h
new file mode 100644 (file)
index 0000000..022368e
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef IMAP_H
+#define IMAP_H
+
+struct imap_node
+{
+       int left;
+       void* right;
+};
+
+/* Danger, Will Robinson! The type and the macro must be compatible. */
+
+struct imap
+{
+       struct imap_node* item;
+       int count;
+       int max;
+};
+
+#define IMAPOF(RIGHT) \
+       struct { \
+               struct { int left; RIGHT* right; }* item; \
+               int count; \
+               int max; \
+       }
+
+extern void imap_put(void* map, int left, void* right);
+extern void imap_add(void* map, int left, void* right);
+
+#endif
+
index fa3d7d0..aeb68ae 100644 (file)
@@ -11,6 +11,7 @@
 #include <errno.h>
 #include "iburg.h"
 #include "ircodes.h"
+#include "astring.h"
 
 static char rcsid[] = "$Id$";
 
@@ -31,7 +32,7 @@ static void print(char* fmt, ...);
 static void ckreach(Nonterm p);
 static void registerterminals(void);
 static void emitclosure(Nonterm nts);
-static void emitcost(Tree t, char* v);
+static void emitcost(Tree t, const char* v);
 static void emitcostcalc(Rule r);
 static void emitdefs(Nonterm nts, int ntnumber);
 static void emitfuncs(void);
@@ -50,7 +51,7 @@ static void emitstate(Term terms, Nonterm start, int ntnumber);
 static void emitstring(Rule rules);
 static void emitstruct(Nonterm nts, int ntnumber);
 static void emitterms(Term terms);
-static void emittest(Tree t, char* v, char* suffix);
+static void emittest(Tree t, const char* v, const char* suffix);
 
 extern int yy_flex_debug;
 
@@ -154,31 +155,9 @@ int main(int argc, char* argv[])
        return errcnt > 0;
 }
 
-/* stringf - format and save a string */
-char* stringf(char* fmt, ...)
-{
-    int n;
-    char* p;
-    va_list ap;
-
-    va_start(ap, fmt);
-    n = vsnprintf(NULL, 0, fmt, ap) + 1;
-    va_end(ap);
-
-    p = malloc(n);
-    if (!p)
-        return NULL;
-
-    va_start(ap, fmt);
-    vsnprintf(p, n, fmt, ap);
-    va_end(ap);
-
-    return p;
-}
-
 static void registerterminal(const struct ir_data* data, int iropcode, int size)
 {
-       const char* s = (size == 0) ? data->name : stringf("%s%d", data->name, size);
+       const char* s = (size == 0) ? data->name : aprintf("%s%d", data->name, size);
        int esn = ir_to_esn(iropcode, size);
 
        term(s, esn);
@@ -650,16 +629,16 @@ static void emitclosure(Nonterm nts)
 }
 
 /* emitcost - emit cost computation for tree t */
-static void emitcost(Tree t, char* v)
+static void emitcost(Tree t, const char* v)
 {
        Nonterm p = t->op;
 
        if (p->kind == TERM)
        {
                if (t->left)
-                       emitcost(t->left, stringf("%s->left", v));
+                       emitcost(t->left, aprintf("%s->left", v));
                if (t->right)
-                       emitcost(t->right, stringf("%s->right", v));
+                       emitcost(t->right, aprintf("%s->right", v));
        }
        else
                print("%s->cost[%P%S_NT] + ", v, p);
@@ -704,7 +683,7 @@ static void emitheader(void)
 }
 
 /* computekids - compute paths to kids in tree t */
-static char* computekids(Tree t, char* v, char* bp, int* ip)
+static char* computekids(Tree t, const char* v, char* bp, int* ip)
 {
        Term p = t->op;
 
@@ -715,9 +694,9 @@ static char* computekids(Tree t, char* v, char* bp, int* ip)
        }
        else if (p->arity > 0)
        {
-               bp = computekids(t->left, stringf("LEFT_CHILD(%s)", v), bp, ip);
+               bp = computekids(t->left, aprintf("LEFT_CHILD(%s)", v), bp, ip);
                if (p->arity == 2)
-                       bp = computekids(t->right, stringf("RIGHT_CHILD(%s)", v), bp, ip);
+                       bp = computekids(t->right, aprintf("RIGHT_CHILD(%s)", v), bp, ip);
        }
        return bp;
 }
@@ -1168,7 +1147,7 @@ static void emitterms(Term terms)
 }
 
 /* emittest - emit clause for testing a match */
-static void emittest(Tree t, char* v, char* suffix)
+static void emittest(Tree t, const char* v, const char* suffix)
 {
        Term p = t->op;
 
@@ -1177,9 +1156,9 @@ static void emittest(Tree t, char* v, char* suffix)
                print("%3%s->op == %d%s/* %S */\n", v, p->esn,
                    t->nterms > 1 ? " && " : suffix, p);
                if (t->left)
-                       emittest(t->left, stringf("%s->left", v),
+                       emittest(t->left, aprintf("%s->left", v),
                            t->right && t->right->nterms ? " && " : suffix);
                if (t->right)
-                       emittest(t->right, stringf("%s->right", v), suffix);
+                       emittest(t->right, aprintf("%s->right", v), suffix);
        }
 }