Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
-/* This program generates z8k-opc.h. Compile with -fwritable-strings. */
+/* This program generates z8k-opc.h. */
#include <stdio.h>
#include "sysdep.h"
char type;
char *bits;
char *name;
- char *flavor;
+ /* Unique number for stable sorting. */
+ int id;
};
#define iswhite(x) ((x) == ' ' || (x) == '\t')
{"------", 7, 32, "1000 1100 dddd 0001", "ldctlb rbd,ctrl", 0},
{"CZSVDH", 7, 32, "1000 1100 ssss 1001", "ldctlb ctrl,rbs", 0},
- {"*", 4, 8, "1000 1000 ssss dddd", "xorb rbd,rbs", 0},
{"*", 0, 0, 0, 0, 0}
};
}
static int
-func (struct op *a, struct op *b)
+func (const void *p1, const void *p2)
{
- return strcmp ((a)->name, (b)->name);
+ const struct op *a = p1;
+ const struct op *b = p2;
+ int ret = strcmp (a->name, b->name);
+ if (ret != 0)
+ return ret;
+ return a->id > b->id ? 1 : -1;
}
return nargs;
}
-static void
+static char *
sub (char *x, char c)
{
+ /* Create copy. */
+ char *ret = xstrdup (x);
+ x = ret;
while (*x)
{
if (x[0] == c && x[1] == c &&
}
x++;
}
+ return ret;
}
internal (void)
{
int c = count ();
- struct op *new = xmalloc (sizeof (struct op) * c);
+ int id;
+ struct op *new_op = xmalloc (sizeof (struct op) * (c + 1));
struct op *p = opt;
- memcpy (new, p, c * sizeof (struct op));
+ memcpy (new_op, p, (c + 1) * sizeof (struct op));
+
+ /* Assign unique id. */
+ for (id = 0; id < c; id++)
+ new_op[id].id = id;
/* Sort all names in table alphabetically. */
- qsort (new, c, sizeof (struct op), (int (*)(const void *, const void *))func);
+ qsort (new_op, c, sizeof (struct op), func);
- p = new;
+ p = new_op;
while (p->flags && p->flags[0] != '*')
{
/* If there are any @rs, sub the ssss into a ssn0, (rs), (ssn0). */
/* Skip the r and sub the string. */
s++;
c = s[1];
- sub (p->bits, c);
+ p->bits = sub (p->bits, c);
}
if (s[0] == '(' && s[3] == ')')
{
- sub (p->bits, s[2]);
+ p->bits = sub (p->bits, s[2]);
}
if (s[0] == '(')
{
- sub (p->bits, s[-1]);
+ p->bits = sub (p->bits, s[-1]);
}
s++;
gas (void)
{
int c = count ();
+ int id;
struct op *p = opt;
int idx = -1;
char *oldname = "";
- struct op *new = xmalloc (sizeof (struct op) * c);
+ struct op *new_op = xmalloc (sizeof (struct op) * (c + 1));
+
+ memcpy (new_op, p, (c + 1) * sizeof (struct op));
- memcpy (new, p, c * sizeof (struct op));
+ /* Assign unique id. */
+ for (id = 0; id < c; id++)
+ new_op[id].id = id;
/* Sort all names in table alphabetically. */
- qsort (new, c, sizeof (struct op), (int (*)(const void *, const void *)) func);
+ qsort (new_op, c, sizeof (struct op), func);
printf ("/* DO NOT EDIT! -*- buffer-read-only: t -*-\n");
printf (" This file is automatically generated by z8kgen. */\n\n");
printf ("#ifdef DEFINE_TABLE\n");
printf ("const opcode_entry_type z8k_table[] = {\n");
- while (new->flags && new->flags[0])
+ while (new_op->flags && new_op->flags[0] != '*')
{
int nargs;
int length;
- printf ("\n/* %s *** %s */\n", new->bits, new->name);
+ printf ("\n/* %s *** %s */\n", new_op->bits, new_op->name);
printf ("{\n");
printf ("#ifdef NICENAMES\n");
- printf ("\"%s\",%d,%d,", new->name, new->type, new->cycles);
+ printf ("\"%s\",%d,%d,", new_op->name, new_op->type, new_op->cycles);
{
int answer = 0;
- char *p = new->flags;
+ char *p = new_op->flags;
while (*p)
{
printf ("#endif\n");
- nargs = chewname (&new->name);
+ nargs = chewname (&new_op->name);
printf ("\n\t");
- chewbits (new->bits, &length);
+ chewbits (new_op->bits, &length);
length /= 2;
if (length & 1)
abort();
- if (strcmp (oldname, new->name) != 0)
+ if (strcmp (oldname, new_op->name) != 0)
idx++;
printf (",%d,%d,%d", nargs, length, idx);
- oldname = new->name;
+ oldname = new_op->name;
printf ("},\n");
- new++;
+ new_op++;
}
printf ("\n/* end marker */\n");
printf ("{\n#ifdef NICENAMES\nNULL,0,0,\n0,\n#endif\n");