Move fatal(), warning() and aprintf() into the new data module (because they're

really useful).
This commit is contained in:
David Given 2016-09-30 19:10:30 +02:00
parent b32883b013
commit 3a973a19f3
10 changed files with 211 additions and 90 deletions

View file

@ -1,38 +1,5 @@
#include "mcg.h" #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) bool tracing(char k)
{ {
switch (k) switch (k)

View file

@ -18,6 +18,8 @@
#include "em_ptyp.h" #include "em_ptyp.h"
#include "array.h" #include "array.h"
#include "map.h" #include "map.h"
#include "diagnostics.h"
#include "astring.h"
#include "ir.h" #include "ir.h"
#include "mcgg.h" #include "mcgg.h"
#include "hop.h" #include "hop.h"
@ -88,7 +90,6 @@ struct basicblock
bool is_terminated : 1; bool is_terminated : 1;
}; };
extern void fatal(const char* s, ...);
extern const char* aprintf(const char* fmt, ...); extern const char* aprintf(const char* fmt, ...);
extern void tracef(char k, const char* fmt, ...); extern void tracef(char k, const char* fmt, ...);
extern bool tracing(char k); extern bool tracing(char k);

View file

@ -1,28 +1,28 @@
REGISTERS REGISTERS
r0 reg4 int ret0; r0 reg4 i ret0;
r1 reg4 int ret1; r1 reg4 i;
r2 reg4 int; r2 reg4 i;
r3 reg4 int; r3 reg4 i;
r4 reg4 int; r4 reg4 i;
r5 reg4 int; r5 reg4 i;
r6 reg4 int; r6 reg4 i;
r7 reg4 int; r7 reg4 i;
r8 reg4 int; r8 reg4 i;
r9 reg4 int; r9 reg4 i;
r10 reg4 int; r10 reg4 i;
r11 reg4 int; r11 reg4 i;
s0 reg4 float; s0 reg4 f;
s1 reg4 float; s1 reg4 f;
s2 reg4 float; s2 reg4 f;
s3 reg4 float; s3 reg4 f;
s4 reg4 float; s4 reg4 f;
s5 reg4 float; s5 reg4 f;
s6 reg4 float; s6 reg4 f;
s7 reg4 float; s7 reg4 f;
s8 reg4 float; s8 reg4 f;
s9 reg4 float; s9 reg4 f;
cc conditioncode; cc conditioncode;

View file

@ -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 : */

View file

@ -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 : */

View file

@ -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 : */

View file

@ -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 : */

58
modules/src/data/imap.c Normal file
View file

@ -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);
}

30
modules/src/data/imap.h Normal file
View file

@ -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

View file

@ -11,6 +11,7 @@
#include <errno.h> #include <errno.h>
#include "iburg.h" #include "iburg.h"
#include "ircodes.h" #include "ircodes.h"
#include "astring.h"
static char rcsid[] = "$Id$"; static char rcsid[] = "$Id$";
@ -31,7 +32,7 @@ static void print(char* fmt, ...);
static void ckreach(Nonterm p); static void ckreach(Nonterm p);
static void registerterminals(void); static void registerterminals(void);
static void emitclosure(Nonterm nts); 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 emitcostcalc(Rule r);
static void emitdefs(Nonterm nts, int ntnumber); static void emitdefs(Nonterm nts, int ntnumber);
static void emitfuncs(void); static void emitfuncs(void);
@ -50,7 +51,7 @@ static void emitstate(Term terms, Nonterm start, int ntnumber);
static void emitstring(Rule rules); static void emitstring(Rule rules);
static void emitstruct(Nonterm nts, int ntnumber); static void emitstruct(Nonterm nts, int ntnumber);
static void emitterms(Term terms); 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; extern int yy_flex_debug;
@ -154,31 +155,9 @@ int main(int argc, char* argv[])
return errcnt > 0; 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) 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); int esn = ir_to_esn(iropcode, size);
term(s, esn); term(s, esn);
@ -650,16 +629,16 @@ static void emitclosure(Nonterm nts)
} }
/* emitcost - emit cost computation for tree t */ /* 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; Nonterm p = t->op;
if (p->kind == TERM) if (p->kind == TERM)
{ {
if (t->left) if (t->left)
emitcost(t->left, stringf("%s->left", v)); emitcost(t->left, aprintf("%s->left", v));
if (t->right) if (t->right)
emitcost(t->right, stringf("%s->right", v)); emitcost(t->right, aprintf("%s->right", v));
} }
else else
print("%s->cost[%P%S_NT] + ", v, p); print("%s->cost[%P%S_NT] + ", v, p);
@ -704,7 +683,7 @@ static void emitheader(void)
} }
/* computekids - compute paths to kids in tree t */ /* 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; Term p = t->op;
@ -715,9 +694,9 @@ static char* computekids(Tree t, char* v, char* bp, int* ip)
} }
else if (p->arity > 0) 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) 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; return bp;
} }
@ -1168,7 +1147,7 @@ static void emitterms(Term terms)
} }
/* emittest - emit clause for testing a match */ /* 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; 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, print("%3%s->op == %d%s/* %S */\n", v, p->esn,
t->nterms > 1 ? " && " : suffix, p); t->nterms > 1 ? " && " : suffix, p);
if (t->left) if (t->left)
emittest(t->left, stringf("%s->left", v), emittest(t->left, aprintf("%s->left", v),
t->right && t->right->nterms ? " && " : suffix); t->right && t->right->nterms ? " && " : suffix);
if (t->right) if (t->right)
emittest(t->right, stringf("%s->right", v), suffix); emittest(t->right, aprintf("%s->right", v), suffix);
} }
} }