1 /* Copyright 2007 Free Software Foundation, Inc.
3 This file is part of GAS, the GNU Assembler, and GDB, the GNU Debugger.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "safe-ctype.h"
30 #define _(String) gettext (String)
32 static const char *program_name = NULL;
36 fail (const char *message, ...)
40 va_start (args, message);
41 fprintf (stderr, _("%s: Error: "), program_name);
42 vfprintf (stderr, message, args);
47 /* Remove leading white spaces. */
50 remove_leading_whitespaces (char *str)
52 while (ISSPACE (*str))
57 /* Remove trailing white spaces. */
60 remove_trailing_whitespaces (char *str)
62 size_t last = strlen (str);
70 if (ISSPACE (str [last]))
78 /* Find next field separated by '.' and terminate it. Return a
79 pointer to the one after it. */
82 next_field (char *str, char **next)
86 p = remove_leading_whitespaces (str);
87 for (str = p; *str != ',' && *str != '\0'; str++);
90 remove_trailing_whitespaces (p);
98 process_i386_opcodes (void)
100 FILE *fp = fopen ("i386-opc.tbl", "r");
103 char *str, *p, *last;
104 char *name, *operands, *base_opcode, *extension_opcode;
105 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
108 fail (_("can't find i386-opc.tbl for reading\n"));
110 printf ("\n/* i386 opcode table. */\n\n");
111 printf ("const template i386_optab[] =\n{\n");
115 if (fgets (buf, sizeof (buf), fp) == NULL)
118 p = remove_leading_whitespaces (buf);
121 str = strstr (p, "//");
125 /* Remove trailing white spaces. */
126 remove_trailing_whitespaces (p);
139 last = p + strlen (p);
142 name = next_field (p, &str);
147 /* Find number of operands. */
148 operands = next_field (str, &str);
153 /* Find base_opcode. */
154 base_opcode = next_field (str, &str);
159 /* Find extension_opcode. */
160 extension_opcode = next_field (str, &str);
165 /* Find cpu_flags. */
166 cpu_flags = next_field (str, &str);
171 /* Find opcode_modifier. */
172 opcode_modifier = next_field (str, &str);
177 /* Remove the first {. */
178 str = remove_leading_whitespaces (str);
181 str = remove_leading_whitespaces (str + 1);
185 /* There are at least "X}". */
189 /* Remove trailing white spaces and }. */
193 if (ISSPACE (str[i]) || str[i] == '}')
202 /* Find operand_types. */
203 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
207 operand_types [i] = NULL;
211 operand_types [i] = next_field (str, &str);
212 if (*operand_types[i] == '0')
215 operand_types[i] = NULL;
220 printf (" { \"%s\", %s, %s, %s, %s,\n",
221 name, operands, base_opcode, extension_opcode,
224 printf (" %s,\n", opcode_modifier);
228 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
230 if (operand_types[i] == NULL
231 || *operand_types[i] == '0')
241 printf ("%s", operand_types[i]);
246 printf (" { NULL, 0, 0, 0, 0, 0, { 0 } }\n");
251 process_i386_registers (void)
253 FILE *fp = fopen ("i386-reg.tbl", "r");
255 char *str, *p, *last;
256 char *reg_name, *reg_type, *reg_flags, *reg_num;
259 fail (_("can't find i386-reg.tbl for reading\n"));
261 printf ("\n/* i386 register table. */\n\n");
262 printf ("const reg_entry i386_regtab[] =\n{\n");
266 if (fgets (buf, sizeof (buf), fp) == NULL)
269 p = remove_leading_whitespaces (buf);
272 str = strstr (p, "//");
276 /* Remove trailing white spaces. */
277 remove_trailing_whitespaces (p);
290 last = p + strlen (p);
293 reg_name = next_field (p, &str);
299 reg_type = next_field (str, &str);
304 /* Find reg_flags. */
305 reg_flags = next_field (str, &str);
311 reg_num = next_field (str, &str);
313 printf (" { \"%s\", %s, %s, %s },\n",
314 reg_name, reg_type, reg_flags, reg_num);
319 printf ("\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
322 /* Program options. */
323 #define OPTION_SRCDIR 200
325 struct option long_options[] =
327 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
328 {"debug", no_argument, NULL, 'd'},
329 {"version", no_argument, NULL, 'V'},
330 {"help", no_argument, NULL, 'h'},
331 {0, no_argument, NULL, 0}
337 printf ("%s: version 1.0\n", program_name);
342 usage (FILE * stream, int status)
344 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
350 main (int argc, char **argv)
352 extern int chdir (char *);
356 program_name = *argv;
357 xmalloc_set_program_name (program_name);
359 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
384 if (chdir (srcdir) != 0)
385 fail (_("unable to change directory to \"%s\", errno = %s\n"),
386 srcdir, strerror (errno));
388 printf ("/* This file is automatically generated by i386-gen. Do not edit! */\n");
390 process_i386_opcodes ();
391 process_i386_registers ();