--- /dev/null
+#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 : */
+
--- /dev/null
+#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);
+}
+
#include <errno.h>
#include "iburg.h"
#include "ircodes.h"
+#include "astring.h"
static char rcsid[] = "$Id$";
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);
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;
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);
}
/* 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);
}
/* 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;
}
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;
}
}
/* 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;
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);
}
}