From 3a973a19f3ce8b5446d4a50b63cbed06318d875d Mon Sep 17 00:00:00 2001 From: David Given Date: Fri, 30 Sep 2016 19:10:30 +0200 Subject: [PATCH] Move fatal(), warning() and aprintf() into the new data module (because they're really useful). --- mach/proto/mcg/main.c | 33 ------------------- mach/proto/mcg/mcg.h | 3 +- mach/proto/mcg/table | 44 +++++++++++++------------- modules/src/data/astring.c | 27 ++++++++++++++++ modules/src/data/astring.h | 9 ++++++ modules/src/data/diagnostics.c | 38 ++++++++++++++++++++++ modules/src/data/diagnostics.h | 12 +++++++ modules/src/data/imap.c | 58 ++++++++++++++++++++++++++++++++++ modules/src/data/imap.h | 30 ++++++++++++++++++ util/mcgg/iburg.c | 47 ++++++++------------------- 10 files changed, 211 insertions(+), 90 deletions(-) create mode 100644 modules/src/data/astring.c create mode 100644 modules/src/data/astring.h create mode 100644 modules/src/data/diagnostics.c create mode 100644 modules/src/data/diagnostics.h create mode 100644 modules/src/data/imap.c create mode 100644 modules/src/data/imap.h diff --git a/mach/proto/mcg/main.c b/mach/proto/mcg/main.c index c17b60bb0..5d450bdfc 100644 --- a/mach/proto/mcg/main.c +++ b/mach/proto/mcg/main.c @@ -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) diff --git a/mach/proto/mcg/mcg.h b/mach/proto/mcg/mcg.h index a836ecc2f..176efb535 100644 --- a/mach/proto/mcg/mcg.h +++ b/mach/proto/mcg/mcg.h @@ -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); diff --git a/mach/proto/mcg/table b/mach/proto/mcg/table index 4126b2f01..eac421ddf 100644 --- a/mach/proto/mcg/table +++ b/mach/proto/mcg/table @@ -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; + 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 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; + 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 index 000000000..7da06e9fa --- /dev/null +++ b/modules/src/data/astring.c @@ -0,0 +1,27 @@ +#include +#include +#include + +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 index 000000000..483c0287d --- /dev/null +++ b/modules/src/data/astring.h @@ -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 index 000000000..32ac6a0ee --- /dev/null +++ b/modules/src/data/diagnostics.c @@ -0,0 +1,38 @@ +#include +#include +#include +#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 index 000000000..7c2908fa0 --- /dev/null +++ b/modules/src/data/diagnostics.h @@ -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 index 000000000..15c66e769 --- /dev/null +++ b/modules/src/data/imap.c @@ -0,0 +1,58 @@ +#include +#include +#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; icount; 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; icount; 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 index 000000000..022368ec8 --- /dev/null +++ b/modules/src/data/imap.h @@ -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 + diff --git a/util/mcgg/iburg.c b/util/mcgg/iburg.c index fa3d7d0b1..aeb68ae31 100644 --- a/util/mcgg/iburg.c +++ b/util/mcgg/iburg.c @@ -11,6 +11,7 @@ #include #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); } }