1 /* Copyright 2007 Free Software Foundation, Inc.
3 This file is part of the GNU opcodes library.
5 This library 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 3, or (at your option)
10 It is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13 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,
18 MA 02110-1301, USA. */
25 #include "libiberty.h"
26 #include "safe-ctype.h"
31 #define _(String) gettext (String)
33 static const char *program_name
= NULL
;
36 /* File of i386 opcode and register tables. */
40 fail (const char *message
, ...)
44 va_start (args
, message
);
45 fprintf (stderr
, _("%s: Error: "), program_name
);
46 vfprintf (stderr
, message
, args
);
51 /* Remove leading white spaces. */
54 remove_leading_whitespaces (char *str
)
56 while (ISSPACE (*str
))
61 /* Remove trailing white spaces. */
64 remove_trailing_whitespaces (char *str
)
66 size_t last
= strlen (str
);
74 if (ISSPACE (str
[last
]))
82 /* Find next field separated by '.' and terminate it. Return a
83 pointer to the one after it. */
86 next_field (char *str
, char **next
)
90 p
= remove_leading_whitespaces (str
);
91 for (str
= p
; *str
!= ',' && *str
!= '\0'; str
++);
94 remove_trailing_whitespaces (p
);
102 process_i386_opcodes (void)
104 FILE *fp
= fopen ("i386-opc.tbl", "r");
107 char *str
, *p
, *last
;
108 char *name
, *operands
, *base_opcode
, *extension_opcode
;
109 char *cpu_flags
, *opcode_modifier
, *operand_types
[MAX_OPERANDS
];
112 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
115 fprintf (table
, "\n/* i386 opcode table. */\n\n");
116 fprintf (table
, "const template i386_optab[] =\n{\n");
120 if (fgets (buf
, sizeof (buf
), fp
) == NULL
)
123 p
= remove_leading_whitespaces (buf
);
126 str
= strstr (p
, "//");
130 /* Remove trailing white spaces. */
131 remove_trailing_whitespaces (p
);
136 fprintf (table
, "%s\n", p
);
144 last
= p
+ strlen (p
);
147 name
= next_field (p
, &str
);
152 /* Find number of operands. */
153 operands
= next_field (str
, &str
);
158 /* Find base_opcode. */
159 base_opcode
= next_field (str
, &str
);
164 /* Find extension_opcode. */
165 extension_opcode
= next_field (str
, &str
);
170 /* Find cpu_flags. */
171 cpu_flags
= next_field (str
, &str
);
176 /* Find opcode_modifier. */
177 opcode_modifier
= next_field (str
, &str
);
182 /* Remove the first {. */
183 str
= remove_leading_whitespaces (str
);
186 str
= remove_leading_whitespaces (str
+ 1);
190 /* There are at least "X}". */
194 /* Remove trailing white spaces and }. */
198 if (ISSPACE (str
[i
]) || str
[i
] == '}')
207 /* Find operand_types. */
208 for (i
= 0; i
< ARRAY_SIZE (operand_types
); i
++)
212 operand_types
[i
] = NULL
;
216 operand_types
[i
] = next_field (str
, &str
);
217 if (*operand_types
[i
] == '0')
220 operand_types
[i
] = NULL
;
225 fprintf (table
, " { \"%s\", %s, %s, %s, %s,\n",
226 name
, operands
, base_opcode
, extension_opcode
,
229 fprintf (table
, " %s,\n", opcode_modifier
);
231 fprintf (table
, " { ");
233 for (i
= 0; i
< ARRAY_SIZE (operand_types
); i
++)
235 if (operand_types
[i
] == NULL
236 || *operand_types
[i
] == '0')
239 fprintf (table
, "0");
244 fprintf (table
, ",\n ");
246 fprintf (table
, "%s", operand_types
[i
]);
248 fprintf (table
, " } },\n");
253 fprintf (table
, " { NULL, 0, 0, 0, 0, 0, { 0 } }\n");
254 fprintf (table
, "};\n");
258 process_i386_registers (void)
260 FILE *fp
= fopen ("i386-reg.tbl", "r");
262 char *str
, *p
, *last
;
263 char *reg_name
, *reg_type
, *reg_flags
, *reg_num
;
266 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
269 fprintf (table
, "\n/* i386 register table. */\n\n");
270 fprintf (table
, "const reg_entry i386_regtab[] =\n{\n");
274 if (fgets (buf
, sizeof (buf
), fp
) == NULL
)
277 p
= remove_leading_whitespaces (buf
);
280 str
= strstr (p
, "//");
284 /* Remove trailing white spaces. */
285 remove_trailing_whitespaces (p
);
290 fprintf (table
, "%s\n", p
);
298 last
= p
+ strlen (p
);
301 reg_name
= next_field (p
, &str
);
307 reg_type
= next_field (str
, &str
);
312 /* Find reg_flags. */
313 reg_flags
= next_field (str
, &str
);
319 reg_num
= next_field (str
, &str
);
321 fprintf (table
, " { \"%s\", %s, %s, %s },\n",
322 reg_name
, reg_type
, reg_flags
, reg_num
);
327 fprintf (table
, "};\n");
329 fprintf (table
, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
332 /* Program options. */
333 #define OPTION_SRCDIR 200
335 struct option long_options
[] =
337 {"srcdir", required_argument
, NULL
, OPTION_SRCDIR
},
338 {"debug", no_argument
, NULL
, 'd'},
339 {"version", no_argument
, NULL
, 'V'},
340 {"help", no_argument
, NULL
, 'h'},
341 {0, no_argument
, NULL
, 0}
347 printf ("%s: version 1.0\n", program_name
);
352 usage (FILE * stream
, int status
)
354 fprintf (stream
, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
360 main (int argc
, char **argv
)
362 extern int chdir (char *);
366 program_name
= *argv
;
367 xmalloc_set_program_name (program_name
);
369 while ((c
= getopt_long (argc
, argv
, "vVdh", long_options
, 0)) != EOF
)
394 if (chdir (srcdir
) != 0)
395 fail (_("unable to change directory to \"%s\", errno = %s\n"),
396 srcdir
, strerror (errno
));
398 table
= fopen ("i386-tbl.h", "w");
400 fail (_("can't create i386-tbl.h, errno = %s\n"), strerror (errno
));
402 fprintf (table
, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
403 /* Copyright 2007 Free Software Foundation, Inc.\n\
405 This file is part of the GNU opcodes library.\n\
407 This library is free software; you can redistribute it and/or modify\n\
408 it under the terms of the GNU General Public License as published by\n\
409 the Free Software Foundation; either version 3, or (at your option)\n\
410 any later version.\n\
412 It is distributed in the hope that it will be useful, but WITHOUT\n\
413 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
414 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
415 License for more details.\n\
417 You should have received a copy of the GNU General Public License\n\
418 along with this program; if not, write to the Free Software\n\
419 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
420 MA 02110-1301, USA. */\n");
422 process_i386_opcodes ();
423 process_i386_registers ();
This page took 0.038471 seconds and 5 git commands to generate.