Commit 6f655449 authored by Akim Demaille's avatar Akim Demaille
Browse files

style: do not generate tabulation in the output

* src/emit.c (output): Convert tabs to two spaces.
* tests/sample.brg, tests/sample.c.ref: Untabify.
Remove trailing spaces.
parent e32a800d
......@@ -37,9 +37,21 @@ void output (char *fmt, ...)
{
va_list ap;
/* Replace all the tabulations by two spaces. */
char *cp, *cp2;
va_start(ap, fmt);
vfprintf (outputfd, fmt, ap);
cp = g_strdup_vprintf(fmt, ap);
va_end (ap);
for (cp2 = cp; *cp2; ++cp2)
if (*cp2 == '\t')
{
putc (' ', outputfd);
putc (' ', outputfd);
}
else
putc (*cp2, outputfd);
g_free (cp);
}
/** Emit includes of external header files. */
......
/*
* This header (everything before the first "%%") is copied
/*
* This header (everything before the first "%%") is copied
* directly to the output.
*/
......@@ -11,9 +11,9 @@
typedef struct _MBTree MBTree;
struct _MBTree {
guint16 op;
MBTree *left, *right;
gpointer state;
guint16 op;
MBTree *left, *right;
gpointer state;
};
%% these are the monoburg definition
......@@ -28,7 +28,7 @@ struct _MBTree {
#
# we must first define the terminals
# with or without numbers
#
#
%term Assign Constant Fetch=3 Four=8 Mul=5 Plus=6 AltFetch=7
#
......@@ -36,38 +36,38 @@ struct _MBTree {
#
%start reg
con: Constant 0
con: Constant 0
con: Four 0
con: Four 0
addr: con 0
addr: con 0
addr: Plus(con,reg)
addr: Plus(con,reg)
{
int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
printf ("%s\n", mono_burg_rule_string [ern]);
} cost
{
int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
printf ("%s\n", mono_burg_rule_string [ern]);
} cost
{
return 1;
}
addr: Plus(con,Mul(Four,reg)) 2
addr: Plus(con,Mul(Four,reg)) 2
{
int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
printf ("%s\n", mono_burg_rule_string [ern]);
}
int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
printf ("%s\n", mono_burg_rule_string [ern]);
}
reg: AltFetch(addr),
reg: Fetch(addr) 1
reg: Fetch(addr) 1
{
int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
printf ("%s\n", mono_burg_rule_string [ern]);
int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
printf ("%s\n", mono_burg_rule_string [ern]);
}
reg: Assign(addr,reg) 1
reg: Assign(addr,reg) 1
{
int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
printf ("%s\n", mono_burg_rule_string [ern]);
int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
printf ("%s\n", mono_burg_rule_string [ern]);
}
%% the rest is also copied directly to the output
......@@ -78,72 +78,72 @@ reg: Assign(addr,reg) 1
static MBTree *
create_tree (int op, MBTree *left, MBTree *right)
{
MBTree *t = g_new0 (MBTree, 1);
t->op = op;
t->left = left;
t->right = right;
MBTree *t = g_new0 (MBTree, 1);
t->op = op;
t->left = left;
t->right = right;
return t;
return t;
}
static void
reduce (MBTree *tree, int goal)
reduce (MBTree *tree, int goal)
{
MBTree *kids[10];
int ern = mono_burg_rule (tree->state, goal);
const guint16 *nts = mono_burg_nts [ern];
int i, n;
mono_burg_kids (tree, ern, kids);
// printf ("TEST %d %d %s %d\n", goal, ern, mono_burg_rule_string [ern], nts [0]);
for (i = 0; nts [i]; i++)
reduce (kids [i], nts [i]);
n = (tree->left != NULL) + (tree->right != NULL);
if (n) { /* not a terminal */
// printf ("XXTE %s %d\n", mono_burg_rule_string [ern], n);
if (mono_burg_func [ern])
mono_burg_func [ern] (tree, NULL);
else
g_warning ("no code for rule %s\n",
mono_burg_rule_string [ern]);
} else {
if (mono_burg_func [ern])
g_warning ("unused code in rule %s\n",
mono_burg_rule_string [ern]);
}
MBTree *kids[10];
int ern = mono_burg_rule (tree->state, goal);
const guint16 *nts = mono_burg_nts [ern];
int i, n;
mono_burg_kids (tree, ern, kids);
// printf ("TEST %d %d %s %d\n", goal, ern, mono_burg_rule_string [ern], nts [0]);
for (i = 0; nts [i]; i++)
reduce (kids [i], nts [i]);
n = (tree->left != NULL) + (tree->right != NULL);
if (n) { /* not a terminal */
// printf ("XXTE %s %d\n", mono_burg_rule_string [ern], n);
if (mono_burg_func [ern])
mono_burg_func [ern] (tree, NULL);
else
g_warning ("no code for rule %s\n",
mono_burg_rule_string [ern]);
} else {
if (mono_burg_func [ern])
g_warning ("unused code in rule %s\n",
mono_burg_rule_string [ern]);
}
}
int
main ()
{
MBTree *t, *l, *r;
MBState *s;
MBTree *t, *l, *r;
MBState *s;
l = create_tree (MB_TERM_Constant, NULL, NULL);
l = create_tree (MB_TERM_Constant, NULL, NULL);
r = create_tree (MB_TERM_Fetch, l, NULL);
l = create_tree (MB_TERM_Constant, NULL, NULL);
r = create_tree (MB_TERM_Fetch, l, NULL);
l = create_tree (MB_TERM_Constant, NULL, NULL);
r = create_tree (MB_TERM_Assign, l, r);
l = create_tree (MB_TERM_Four, NULL, NULL);
r = create_tree (MB_TERM_Assign, l, r);
l = create_tree (MB_TERM_Four, NULL, NULL);
r = create_tree (MB_TERM_Mul, l, r);
l = create_tree (MB_TERM_Constant, NULL, NULL);
r = create_tree (MB_TERM_Mul, l, r);
l = create_tree (MB_TERM_Constant, NULL, NULL);
l = create_tree (MB_TERM_Plus, l, r);
l = create_tree (MB_TERM_Plus, l, r);
t = create_tree (MB_TERM_Fetch, l, NULL);
t = create_tree (MB_TERM_Fetch, l, NULL);
s = mono_burg_label (t, NULL);
s = mono_burg_label (t, NULL);
g_assert (s);
g_assert (s);
reduce (t, MB_NTERM_reg);
reduce (t, MB_NTERM_reg);
return 0;
return 0;
}
/*
* This header (everything before the first "%%") is copied
/*
* This header (everything before the first "%%") is copied
* directly to the output.
*/
......@@ -11,9 +11,9 @@
typedef struct _MBTree MBTree;
struct _MBTree {
guint16 op;
MBTree *left, *right;
gpointer state;
guint16 op;
MBTree *left, *right;
gpointer state;
};
#include <glib.h>
......@@ -48,30 +48,30 @@ struct _MBTree {
#define MBCOND(x) if (!(x)) return MBMAXCOST;
typedef enum {
MB_TERM_Assign = 1,
MB_TERM_Constant = 2,
MB_TERM_Fetch = 3,
MB_TERM_Mul = 5,
MB_TERM_Plus = 6,
MB_TERM_AltFetch = 7,
MB_TERM_Four = 8
MB_TERM_Assign = 1,
MB_TERM_Constant = 2,
MB_TERM_Fetch = 3,
MB_TERM_Mul = 5,
MB_TERM_Plus = 6,
MB_TERM_AltFetch = 7,
MB_TERM_Four = 8
} MBTerms;
typedef enum {
MB_NTERM_reg = 1,
MB_NTERM_con = 2,
MB_NTERM_addr = 3
MB_NTERM_reg = 1,
MB_NTERM_con = 2,
MB_NTERM_addr = 3
} MBNTerms;
#define MB_MAX_NTERMS 3
#define MB_MAX_NTERMS 3
struct _MBState {
int op;
struct _MBState *left, *right;
guint16 cost[4];
unsigned int rule_reg:2;
unsigned int rule_con:2;
unsigned int rule_addr:2;
int op;
struct _MBState *left, *right;
guint16 cost[4];
unsigned int rule_reg:2;
unsigned int rule_con:2;
unsigned int rule_addr:2;
};
typedef struct _MBState MBState;
......@@ -86,38 +86,38 @@ int mono_burg_rule (MBState *state, int goal);
MBTREE_TYPE **mono_burg_kids (MBTREE_TYPE *tree, int rulenr, MBTREE_TYPE *kids []);
const guint8 mono_burg_arity [] = {
0,
2, /* Assign */
0, /* Constant */
1, /* Fetch */
0,
2, /* Mul */
2, /* Plus */
1, /* AltFetch */
0, /* Four */
0,
2, /* Assign */
0, /* Constant */
1, /* Fetch */
0,
2, /* Mul */
2, /* Plus */
1, /* AltFetch */
0, /* Four */
};
const char *const mono_burg_term_string [] = {
NULL,
"Assign",
"Constant",
"Fetch",
"Mul",
"Plus",
"AltFetch",
"Four",
NULL,
"Assign",
"Constant",
"Fetch",
"Mul",
"Plus",
"AltFetch",
"Four",
};
const char * const mono_burg_rule_string [] = {
NULL,
"con: Constant",
"con: Four",
"addr: con",
"addr: Plus(con, reg)",
"addr: Plus(con, Mul(Four, reg))",
"reg: AltFetch(addr)",
"reg: Fetch(addr)",
"reg: Assign(addr, reg)",
NULL,
"con: Constant",
"con: Four",
"addr: con",
"addr: Plus(con, reg)",
"addr: Plus(con, Mul(Four, reg))",
"reg: AltFetch(addr)",
"reg: Fetch(addr)",
"reg: Assign(addr, reg)",
};
static const guint16 mono_burg_nts_0 [] = { 0 };
......@@ -127,41 +127,41 @@ static const guint16 mono_burg_nts_3 [] = { MB_NTERM_addr, 0 };
static const guint16 mono_burg_nts_4 [] = { MB_NTERM_addr, MB_NTERM_reg, 0 };
const guint16 *const mono_burg_nts [] = {
0,
mono_burg_nts_0, /* con: Constant */
mono_burg_nts_0, /* con: Four */
mono_burg_nts_1, /* addr: con */
mono_burg_nts_2, /* addr: Plus(con, reg) */
mono_burg_nts_2, /* addr: Plus(con, Mul(Four, reg)) */
mono_burg_nts_3, /* reg: AltFetch(addr) */
mono_burg_nts_3, /* reg: Fetch(addr) */
mono_burg_nts_4, /* reg: Assign(addr, reg) */
0,
mono_burg_nts_0, /* con: Constant */
mono_burg_nts_0, /* con: Four */
mono_burg_nts_1, /* addr: con */
mono_burg_nts_2, /* addr: Plus(con, reg) */
mono_burg_nts_2, /* addr: Plus(con, Mul(Four, reg)) */
mono_burg_nts_3, /* reg: AltFetch(addr) */
mono_burg_nts_3, /* reg: Fetch(addr) */
mono_burg_nts_4, /* reg: Assign(addr, reg) */
};
inline static guint16
/* addr: Plus(con, reg) */
mono_burg_cost_4 (MBTREE_TYPE *tree, MBCOST_DATA *data)
{
(void) data; (void) tree;
{
(void) data; (void) tree;
{
return 1;
}
}
}
static void /* addr: Plus(con, reg) */
mono_burg_emit_3 (MBTREE_TYPE *tree, MBCGEN_TYPE *s)
{
(void) tree; (void) s;
{
{
(void) tree; (void) s;
{
{
int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
printf ("%s\n", mono_burg_rule_string [ern]);
int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
printf ("%s\n", mono_burg_rule_string [ern]);
}
}
}
}
}
/* addr: Plus(con, Mul(Four, reg)) */
......@@ -170,15 +170,15 @@ mono_burg_emit_3 (MBTREE_TYPE *tree, MBCGEN_TYPE *s)
static void /* reg: AltFetch(addr) */
mono_burg_emit_5 (MBTREE_TYPE *tree, MBCGEN_TYPE *s)
{
(void) tree; (void) s;
{
{
(void) tree; (void) s;
{
{
int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
printf ("%s\n", mono_burg_rule_string [ern]);
int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
printf ("%s\n", mono_burg_rule_string [ern]);
}
}
}
}
}
/* reg: Fetch(addr) */
......@@ -188,35 +188,35 @@ mono_burg_emit_5 (MBTREE_TYPE *tree, MBCGEN_TYPE *s)
#define mono_burg_emit_7 mono_burg_emit_5
MBEmitFunc const mono_burg_func [] = {
NULL,
NULL,
NULL,
NULL,
mono_burg_emit_3,
mono_burg_emit_4,
mono_burg_emit_5,
mono_burg_emit_6,
mono_burg_emit_7,
NULL,
NULL,
NULL,
NULL,
mono_burg_emit_3,
mono_burg_emit_4,
mono_burg_emit_5,
mono_burg_emit_6,
mono_burg_emit_7,
};
const int mono_burg_decode_reg[] = {
0,
6,
7,
8,
0,
6,
7,
8,
};
const int mono_burg_decode_con[] = {
0,
1,
2,
0,
1,
2,
};
const int mono_burg_decode_addr[] = {
0,
3,
4,
5,
0,
3,
4,
5,
};
static void closure_con (MBState *p, int c);
......@@ -224,188 +224,188 @@ static void closure_con (MBState *p, int c);
static void
closure_con (MBState *p, int c)
{
/* addr: con */
if (c + 0 < p->cost[MB_NTERM_addr]) {
p->cost[MB_NTERM_addr] = c + 0;
p->rule_addr = 1;
}
/* addr: con */
if (c + 0 < p->cost[MB_NTERM_addr]) {
p->cost[MB_NTERM_addr] = c + 0;
p->rule_addr = 1;
}
}
static MBState *
mono_burg_label_priv (MBTREE_TYPE *tree, MBCOST_DATA *data) {
int arity;
int c;
MBState *p;
MBState *left = NULL, *right = NULL;
switch (mono_burg_arity [MBTREE_OP(tree)]) {
case 0:
break;
case 1:
left = mono_burg_label_priv (MBTREE_LEFT(tree), data);
right = NULL;
break;
case 2:
left = mono_burg_label_priv (MBTREE_LEFT(tree), data);
right = mono_burg_label_priv (MBTREE_RIGHT(tree), data);
}
arity = (left != NULL) + (right != NULL);
g_assert (arity == mono_burg_arity [MBTREE_OP(tree)]);
p = MBALLOC_STATE;
memset (p, 0, sizeof (MBState));
p->op = MBTREE_OP(tree);
p->left = left;
p->right = right;
p->cost [1] = 32767;
p->cost [2] = 32767;
p->cost [3] = 32767;
switch (MBTREE_OP(tree)) {
case 1: /* Assign */
/* reg: Assign(addr, reg) */
{
c = left->cost[MB_NTERM_addr] + right->cost[MB_NTERM_reg] + 1;
if (c < p->cost[MB_NTERM_reg]) {
p->cost[MB_NTERM_reg] = c;
p->rule_reg = 3;
}
}
break;
case 2: /* Constant */
/* con: Constant */
{
c = 0;
if (c < p->cost[MB_NTERM_con]) {
p->cost[MB_NTERM_con] = c;
p->rule_con = 1;
closure_con (p, c);
}
}
break;
case 3: /* Fetch */
/* reg: Fetch(addr) */
{
c = left->cost[MB_NTERM_addr] + 1;
if (c < p->cost[MB_NTERM_reg]) {
p->cost[MB_NTERM_reg] = c;
p->rule_reg = 2;
}
}
break;
case 5: /* Mul */
break;
case 6: /* Plus */
/* addr: Plus(con, reg) */
{
c = left->cost[MB_NTERM_con] + right->cost[MB_NTERM_reg] + mono_burg_cost_4 (tree, data);
if (c < p->cost[MB_NTERM_addr]) {
p->cost[MB_NTERM_addr] = c;
p->rule_addr = 2;
}
}
/* addr: Plus(con, Mul(Four, reg)) */
if (
p->right->op == 5 /* Mul */ &&
p->right->left->op == 8 /* Four */
)
{
c = left->cost[MB_NTERM_con] + right->right->cost[MB_NTERM_reg] + 2;
if (c < p->cost[MB_NTERM_addr]) {
p->cost[MB_NTERM_addr] = c;
p->rule_addr = 3;
}
}
break;
case 7: /* AltFetch */
/* reg: AltFetch(addr) */
{
c = left->cost[MB_NTERM_addr] + 1;
if (c < p->cost[MB_NTERM_reg]) {
p->cost[MB_NTERM_reg] = c;
p->rule_reg = 1;
}
}
break;
case 8: /* Four */
/* con: Four */
{
c = 0;
if (c < p->cost[MB_NTERM_con]) {
p->cost[MB_NTERM_con] = c;
p->rule_con = 2;
closure_con (p, c);
}
}
break;
default:
int arity;
int c;
MBState *p;
MBState *left = NULL, *right = NULL;
switch (mono_burg_arity [MBTREE_OP(tree)]) {
case 0:
break;
case 1:
left = mono_burg_label_priv (MBTREE_LEFT(tree), data);
right = NULL;
break;
case 2:
left = mono_burg_label_priv (MBTREE_LEFT(tree), data);
right = mono_burg_label_priv (MBTREE_RIGHT(tree), data);
}
arity = (left != NULL) + (right != NULL);
g_assert (arity == mono_burg_arity [MBTREE_OP(tree)]);
p = MBALLOC_STATE;
memset (p, 0, sizeof (MBState));
p->op = MBTREE_OP(tree);
p->left = left;
p->right = right;
p->cost [1] = 32767;
p->cost [2] = 32767;
p->cost [3] = 32767;
switch (MBTREE_OP(tree)) {
case 1: /* Assign */