2007-06-28 14:29:56 +00:00
|
|
|
/* Copyright 2007 Free Software Foundation, Inc.
|
|
|
|
|
2007-07-05 09:49:03 +00:00
|
|
|
This file is part of the GNU opcodes library.
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-07-05 09:49:03 +00:00
|
|
|
This library is free software; you can redistribute it and/or modify
|
2007-06-28 14:29:56 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-05 09:49:03 +00:00
|
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
any later version.
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-07-05 09:49:03 +00:00
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
License for more details.
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2007-07-05 09:49:03 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA. */
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "getopt.h"
|
|
|
|
#include "libiberty.h"
|
|
|
|
#include "safe-ctype.h"
|
|
|
|
|
|
|
|
#include "i386-opc.h"
|
|
|
|
|
|
|
|
#include <libintl.h>
|
|
|
|
#define _(String) gettext (String)
|
|
|
|
|
|
|
|
static const char *program_name = NULL;
|
|
|
|
static int debug = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
fail (const char *message, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, message);
|
|
|
|
fprintf (stderr, _("%s: Error: "), program_name);
|
|
|
|
vfprintf (stderr, message, args);
|
|
|
|
va_end (args);
|
|
|
|
xexit (1);
|
|
|
|
}
|
|
|
|
|
2007-09-06 22:08:08 +00:00
|
|
|
static void
|
|
|
|
process_copyright (FILE *fp)
|
|
|
|
{
|
|
|
|
fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
|
|
|
|
/* Copyright 2007 Free Software Foundation, Inc.\n\
|
|
|
|
\n\
|
|
|
|
This file is part of the GNU opcodes library.\n\
|
|
|
|
\n\
|
|
|
|
This library is free software; you can redistribute it and/or modify\n\
|
|
|
|
it under the terms of the GNU General Public License as published by\n\
|
|
|
|
the Free Software Foundation; either version 3, or (at your option)\n\
|
|
|
|
any later version.\n\
|
|
|
|
\n\
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT\n\
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
|
|
|
|
License for more details.\n\
|
|
|
|
\n\
|
|
|
|
You should have received a copy of the GNU General Public License\n\
|
|
|
|
along with this program; if not, write to the Free Software\n\
|
|
|
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
|
|
|
|
MA 02110-1301, USA. */\n");
|
|
|
|
}
|
|
|
|
|
2007-06-28 14:29:56 +00:00
|
|
|
/* Remove leading white spaces. */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
remove_leading_whitespaces (char *str)
|
|
|
|
{
|
|
|
|
while (ISSPACE (*str))
|
|
|
|
str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove trailing white spaces. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_trailing_whitespaces (char *str)
|
|
|
|
{
|
|
|
|
size_t last = strlen (str);
|
|
|
|
|
|
|
|
if (last == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
last--;
|
|
|
|
if (ISSPACE (str [last]))
|
|
|
|
str[last] = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (last != 0);
|
|
|
|
}
|
|
|
|
|
2007-09-06 22:55:04 +00:00
|
|
|
/* Find next field separated by SEP and terminate it. Return a
|
2007-06-28 14:29:56 +00:00
|
|
|
pointer to the one after it. */
|
|
|
|
|
|
|
|
static char *
|
2007-09-06 22:55:04 +00:00
|
|
|
next_field (char *str, char sep, char **next)
|
2007-06-28 14:29:56 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = remove_leading_whitespaces (str);
|
2007-09-06 22:55:04 +00:00
|
|
|
for (str = p; *str != sep && *str != '\0'; str++);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
*str = '\0';
|
|
|
|
remove_trailing_whitespaces (p);
|
|
|
|
|
|
|
|
*next = str + 1;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-06 22:08:08 +00:00
|
|
|
process_i386_opcodes (FILE *table)
|
2007-06-28 14:29:56 +00:00
|
|
|
{
|
|
|
|
FILE *fp = fopen ("i386-opc.tbl", "r");
|
|
|
|
char buf[2048];
|
|
|
|
unsigned int i;
|
|
|
|
char *str, *p, *last;
|
|
|
|
char *name, *operands, *base_opcode, *extension_opcode;
|
|
|
|
char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
|
|
|
|
|
|
|
|
if (fp == NULL)
|
2007-09-06 21:31:55 +00:00
|
|
|
fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
|
|
|
|
strerror (errno));
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "\n/* i386 opcode table. */\n\n");
|
|
|
|
fprintf (table, "const template i386_optab[] =\n{\n");
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
while (!feof (fp))
|
|
|
|
{
|
|
|
|
if (fgets (buf, sizeof (buf), fp) == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = remove_leading_whitespaces (buf);
|
|
|
|
|
|
|
|
/* Skip comments. */
|
|
|
|
str = strstr (p, "//");
|
|
|
|
if (str != NULL)
|
|
|
|
str[0] = '\0';
|
|
|
|
|
|
|
|
/* Remove trailing white spaces. */
|
|
|
|
remove_trailing_whitespaces (p);
|
|
|
|
|
|
|
|
switch (p[0])
|
|
|
|
{
|
|
|
|
case '#':
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "%s\n", p);
|
2007-06-28 14:29:56 +00:00
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
last = p + strlen (p);
|
|
|
|
|
|
|
|
/* Find name. */
|
2007-09-06 22:55:04 +00:00
|
|
|
name = next_field (p, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find number of operands. */
|
2007-09-06 22:55:04 +00:00
|
|
|
operands = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find base_opcode. */
|
2007-09-06 22:55:04 +00:00
|
|
|
base_opcode = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find extension_opcode. */
|
2007-09-06 22:55:04 +00:00
|
|
|
extension_opcode = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find cpu_flags. */
|
2007-09-06 22:55:04 +00:00
|
|
|
cpu_flags = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find opcode_modifier. */
|
2007-09-06 22:55:04 +00:00
|
|
|
opcode_modifier = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Remove the first {. */
|
|
|
|
str = remove_leading_whitespaces (str);
|
|
|
|
if (*str != '{')
|
|
|
|
abort ();
|
|
|
|
str = remove_leading_whitespaces (str + 1);
|
|
|
|
|
|
|
|
i = strlen (str);
|
|
|
|
|
|
|
|
/* There are at least "X}". */
|
|
|
|
if (i < 2)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Remove trailing white spaces and }. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
if (ISSPACE (str[i]) || str[i] == '}')
|
|
|
|
str[i] = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (i != 0);
|
|
|
|
|
|
|
|
last = str + i;
|
|
|
|
|
|
|
|
/* Find operand_types. */
|
|
|
|
for (i = 0; i < ARRAY_SIZE (operand_types); i++)
|
|
|
|
{
|
|
|
|
if (str >= last)
|
|
|
|
{
|
|
|
|
operand_types [i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-06 22:55:04 +00:00
|
|
|
operand_types [i] = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
if (*operand_types[i] == '0')
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
operand_types[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
|
|
|
|
name, operands, base_opcode, extension_opcode,
|
|
|
|
cpu_flags);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, " %s,\n", opcode_modifier);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, " { ");
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (operand_types); i++)
|
|
|
|
{
|
|
|
|
if (operand_types[i] == NULL
|
|
|
|
|| *operand_types[i] == '0')
|
|
|
|
{
|
|
|
|
if (i == 0)
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "0");
|
2007-06-28 14:29:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != 0)
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, ",\n ");
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "%s", operand_types[i]);
|
2007-06-28 14:29:56 +00:00
|
|
|
}
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, " } },\n");
|
2007-06-28 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
fprintf (table, " { NULL, 0, 0, 0, 0, 0, { 0 } }\n");
|
|
|
|
fprintf (table, "};\n");
|
2007-06-28 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-06 22:08:08 +00:00
|
|
|
process_i386_registers (FILE *table)
|
2007-06-28 14:29:56 +00:00
|
|
|
{
|
|
|
|
FILE *fp = fopen ("i386-reg.tbl", "r");
|
|
|
|
char buf[2048];
|
|
|
|
char *str, *p, *last;
|
|
|
|
char *reg_name, *reg_type, *reg_flags, *reg_num;
|
|
|
|
|
|
|
|
if (fp == NULL)
|
2007-09-06 21:31:55 +00:00
|
|
|
fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
|
|
|
|
strerror (errno));
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "\n/* i386 register table. */\n\n");
|
|
|
|
fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
while (!feof (fp))
|
|
|
|
{
|
|
|
|
if (fgets (buf, sizeof (buf), fp) == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = remove_leading_whitespaces (buf);
|
|
|
|
|
|
|
|
/* Skip comments. */
|
|
|
|
str = strstr (p, "//");
|
|
|
|
if (str != NULL)
|
|
|
|
str[0] = '\0';
|
|
|
|
|
|
|
|
/* Remove trailing white spaces. */
|
|
|
|
remove_trailing_whitespaces (p);
|
|
|
|
|
|
|
|
switch (p[0])
|
|
|
|
{
|
|
|
|
case '#':
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "%s\n", p);
|
2007-06-28 14:29:56 +00:00
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
last = p + strlen (p);
|
|
|
|
|
|
|
|
/* Find reg_name. */
|
2007-09-06 22:55:04 +00:00
|
|
|
reg_name = next_field (p, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find reg_type. */
|
2007-09-06 22:55:04 +00:00
|
|
|
reg_type = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find reg_flags. */
|
2007-09-06 22:55:04 +00:00
|
|
|
reg_flags = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
if (str >= last)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Find reg_num. */
|
2007-09-06 22:55:04 +00:00
|
|
|
reg_num = next_field (str, ',', &str);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, " { \"%s\", %s, %s, %s },\n",
|
|
|
|
reg_name, reg_type, reg_flags, reg_num);
|
2007-06-28 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
fprintf (table, "};\n");
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
|
2007-06-28 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Program options. */
|
|
|
|
#define OPTION_SRCDIR 200
|
|
|
|
|
|
|
|
struct option long_options[] =
|
|
|
|
{
|
|
|
|
{"srcdir", required_argument, NULL, OPTION_SRCDIR},
|
|
|
|
{"debug", no_argument, NULL, 'd'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{0, no_argument, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_version (void)
|
|
|
|
{
|
|
|
|
printf ("%s: version 1.0\n", program_name);
|
|
|
|
xexit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage (FILE * stream, int status)
|
|
|
|
{
|
|
|
|
fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
|
|
|
|
program_name);
|
|
|
|
xexit (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
extern int chdir (char *);
|
|
|
|
char *srcdir = NULL;
|
|
|
|
int c;
|
2007-09-06 22:08:08 +00:00
|
|
|
FILE *table;
|
2007-06-28 14:29:56 +00:00
|
|
|
|
|
|
|
program_name = *argv;
|
|
|
|
xmalloc_set_program_name (program_name);
|
|
|
|
|
|
|
|
while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case OPTION_SRCDIR:
|
|
|
|
srcdir = optarg;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
case 'v':
|
|
|
|
print_version ();
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debug = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage (stderr, 0);
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc)
|
|
|
|
usage (stdout, 1);
|
|
|
|
|
|
|
|
if (srcdir != NULL)
|
|
|
|
if (chdir (srcdir) != 0)
|
|
|
|
fail (_("unable to change directory to \"%s\", errno = %s\n"),
|
|
|
|
srcdir, strerror (errno));
|
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
table = fopen ("i386-tbl.h", "w");
|
|
|
|
if (table == NULL)
|
|
|
|
fail (_("can't create i386-tbl.h, errno = %s\n"), strerror (errno));
|
|
|
|
|
2007-09-06 22:08:08 +00:00
|
|
|
process_copyright (table);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 22:08:08 +00:00
|
|
|
process_i386_opcodes (table);
|
|
|
|
process_i386_registers (table);
|
2007-06-28 14:29:56 +00:00
|
|
|
|
2007-09-06 21:31:55 +00:00
|
|
|
fclose (table);
|
|
|
|
|
2007-06-28 14:29:56 +00:00
|
|
|
exit (0);
|
|
|
|
}
|