2008-01-04 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / opcodes / i386-gen.c
blob5da2bb42d479894a0f65ab3006698fc86c44fb69
1 /* Copyright 2007, 2008 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)
8 any later version.
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. */
20 #include "sysdep.h"
21 #include <stdio.h>
22 #include <errno.h>
23 #include "getopt.h"
24 #include "libiberty.h"
25 #include "safe-ctype.h"
27 #include "i386-opc.h"
29 #include <libintl.h>
30 #define _(String) gettext (String)
32 static const char *program_name = NULL;
33 static int debug = 0;
35 typedef struct initializer
37 const char *name;
38 const char *init;
39 } initializer;
41 static initializer cpu_flag_init [] =
43 { "CPU_UNKNOWN_FLAGS",
44 "unknown" },
45 { "CPU_GENERIC32_FLAGS",
46 "Cpu186|Cpu286|Cpu386" },
47 { "CPU_GENERIC64_FLAGS",
48 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2" },
49 { "CPU_NONE_FLAGS",
50 "0" },
51 { "CPU_I186_FLAGS",
52 "Cpu186" },
53 { "CPU_I286_FLAGS",
54 "Cpu186|Cpu286" },
55 { "CPU_I386_FLAGS",
56 "Cpu186|Cpu286|Cpu386" },
57 { "CPU_I486_FLAGS",
58 "Cpu186|Cpu286|Cpu386|Cpu486" },
59 { "CPU_I586_FLAGS",
60 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586" },
61 { "CPU_I686_FLAGS",
62 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686" },
63 { "CPU_P2_FLAGS",
64 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX" },
65 { "CPU_P3_FLAGS",
66 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuMMX2|CpuSSE" },
67 { "CPU_P4_FLAGS",
68 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2" },
69 { "CPU_NOCONA_FLAGS",
70 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuLM" },
71 { "CPU_CORE_FLAGS",
72 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3" },
73 { "CPU_CORE2_FLAGS",
74 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuLM" },
75 { "CPU_K6_FLAGS",
76 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX" },
77 { "CPU_K6_2_FLAGS",
78 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX|Cpu3dnow" },
79 { "CPU_ATHLON_FLAGS",
80 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA" },
81 { "CPU_K8_FLAGS",
82 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuK8|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuLM" },
83 { "CPU_AMDFAM10_FLAGS",
84 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuK8|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM" },
85 { "CPU_MMX_FLAGS",
86 "CpuMMX" },
87 { "CPU_SSE_FLAGS",
88 "CpuMMX|CpuMMX2|CpuSSE" },
89 { "CPU_SSE2_FLAGS",
90 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2" },
91 { "CPU_SSE3_FLAGS",
92 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3" },
93 { "CPU_SSSE3_FLAGS",
94 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3" },
95 { "CPU_SSE4_1_FLAGS",
96 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1" },
97 { "CPU_SSE4_2_FLAGS",
98 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2" },
99 { "CPU_3DNOW_FLAGS",
100 "CpuMMX|Cpu3dnow" },
101 { "CPU_3DNOWA_FLAGS",
102 "CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA" },
103 { "CPU_PADLOCK_FLAGS",
104 "CpuPadLock" },
105 { "CPU_SVME_FLAGS",
106 "CpuSVME" },
107 { "CPU_SSE4A_FLAGS",
108 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a" },
109 { "CPU_ABM_FLAGS",
110 "CpuABM" },
111 { "CPU_SSE5_FLAGS",
112 "CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuSSE5"},
115 static initializer operand_type_init [] =
117 { "OPERAND_TYPE_NONE",
118 "0" },
119 { "OPERAND_TYPE_REG8",
120 "Reg8" },
121 { "OPERAND_TYPE_REG16",
122 "Reg16" },
123 { "OPERAND_TYPE_REG32",
124 "Reg32" },
125 { "OPERAND_TYPE_REG64",
126 "Reg64" },
127 { "OPERAND_TYPE_IMM1",
128 "Imm1" },
129 { "OPERAND_TYPE_IMM8",
130 "Imm8" },
131 { "OPERAND_TYPE_IMM8S",
132 "Imm8S" },
133 { "OPERAND_TYPE_IMM16",
134 "Imm16" },
135 { "OPERAND_TYPE_IMM32",
136 "Imm32" },
137 { "OPERAND_TYPE_IMM32S",
138 "Imm32S" },
139 { "OPERAND_TYPE_IMM64",
140 "Imm64" },
141 { "OPERAND_TYPE_BASEINDEX",
142 "BaseIndex" },
143 { "OPERAND_TYPE_DISP8",
144 "Disp8" },
145 { "OPERAND_TYPE_DISP16",
146 "Disp16" },
147 { "OPERAND_TYPE_DISP32",
148 "Disp32" },
149 { "OPERAND_TYPE_DISP32S",
150 "Disp32S" },
151 { "OPERAND_TYPE_DISP64",
152 "Disp64" },
153 { "OPERAND_TYPE_INOUTPORTREG",
154 "InOutPortReg" },
155 { "OPERAND_TYPE_SHIFTCOUNT",
156 "ShiftCount" },
157 { "OPERAND_TYPE_CONTROL",
158 "Control" },
159 { "OPERAND_TYPE_TEST",
160 "Test" },
161 { "OPERAND_TYPE_DEBUG",
162 "FloatReg" },
163 { "OPERAND_TYPE_FLOATREG",
164 "FloatReg" },
165 { "OPERAND_TYPE_FLOATACC",
166 "FloatAcc" },
167 { "OPERAND_TYPE_SREG2",
168 "SReg2" },
169 { "OPERAND_TYPE_SREG3",
170 "SReg3" },
171 { "OPERAND_TYPE_ACC",
172 "Acc" },
173 { "OPERAND_TYPE_JUMPABSOLUTE",
174 "JumpAbsolute" },
175 { "OPERAND_TYPE_REGMMX",
176 "RegMMX" },
177 { "OPERAND_TYPE_REGXMM",
178 "RegXMM" },
179 { "OPERAND_TYPE_ESSEG",
180 "EsSeg" },
181 { "OPERAND_TYPE_ACC32",
182 "Reg32|Acc" },
183 { "OPERAND_TYPE_ACC64",
184 "Reg64|Acc" },
185 { "OPERAND_TYPE_REG16_INOUTPORTREG",
186 "Reg16|InOutPortReg" },
187 { "OPERAND_TYPE_DISP16_32",
188 "Disp16|Disp32" },
189 { "OPERAND_TYPE_ANYDISP",
190 "Disp8|Disp16|Disp32|Disp32S|Disp64" },
191 { "OPERAND_TYPE_IMM16_32",
192 "Imm16|Imm32" },
193 { "OPERAND_TYPE_IMM16_32S",
194 "Imm16|Imm32S" },
195 { "OPERAND_TYPE_IMM16_32_32S",
196 "Imm16|Imm32|Imm32S" },
197 { "OPERAND_TYPE_IMM32_32S_DISP32",
198 "Imm32|Imm32S|Disp32" },
199 { "OPERAND_TYPE_IMM64_DISP64",
200 "Imm64|Disp64" },
201 { "OPERAND_TYPE_IMM32_32S_64_DISP32",
202 "Imm32|Imm32S|Imm64|Disp32" },
203 { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
204 "Imm32|Imm32S|Imm64|Disp32|Disp64" },
207 typedef struct bitfield
209 int position;
210 int value;
211 const char *name;
212 } bitfield;
214 #define BITFIELD(n) { n, 0, #n }
216 static bitfield cpu_flags[] =
218 BITFIELD (Cpu186),
219 BITFIELD (Cpu286),
220 BITFIELD (Cpu386),
221 BITFIELD (Cpu486),
222 BITFIELD (Cpu586),
223 BITFIELD (Cpu686),
224 BITFIELD (CpuP4),
225 BITFIELD (CpuK6),
226 BITFIELD (CpuK8),
227 BITFIELD (CpuMMX),
228 BITFIELD (CpuMMX2),
229 BITFIELD (CpuSSE),
230 BITFIELD (CpuSSE2),
231 BITFIELD (CpuSSE3),
232 BITFIELD (CpuSSSE3),
233 BITFIELD (CpuSSE4_1),
234 BITFIELD (CpuSSE4_2),
235 BITFIELD (CpuSSE4a),
236 BITFIELD (CpuSSE5),
237 BITFIELD (Cpu3dnow),
238 BITFIELD (Cpu3dnowA),
239 BITFIELD (CpuPadLock),
240 BITFIELD (CpuSVME),
241 BITFIELD (CpuVMX),
242 BITFIELD (CpuSMX),
243 BITFIELD (CpuABM),
244 BITFIELD (CpuLM),
245 BITFIELD (Cpu64),
246 BITFIELD (CpuNo64),
247 #ifdef CpuUnused
248 BITFIELD (CpuUnused),
249 #endif
252 static bitfield opcode_modifiers[] =
254 BITFIELD (D),
255 BITFIELD (W),
256 BITFIELD (Modrm),
257 BITFIELD (ShortForm),
258 BITFIELD (Jump),
259 BITFIELD (JumpDword),
260 BITFIELD (JumpByte),
261 BITFIELD (JumpInterSegment),
262 BITFIELD (FloatMF),
263 BITFIELD (FloatR),
264 BITFIELD (FloatD),
265 BITFIELD (Size16),
266 BITFIELD (Size32),
267 BITFIELD (Size64),
268 BITFIELD (IgnoreSize),
269 BITFIELD (DefaultSize),
270 BITFIELD (No_bSuf),
271 BITFIELD (No_wSuf),
272 BITFIELD (No_lSuf),
273 BITFIELD (No_sSuf),
274 BITFIELD (No_qSuf),
275 BITFIELD (No_ldSuf),
276 BITFIELD (CheckSize),
277 BITFIELD (Byte),
278 BITFIELD (Word),
279 BITFIELD (Dword),
280 BITFIELD (Qword),
281 BITFIELD (Xmmword),
282 BITFIELD (FWait),
283 BITFIELD (IsString),
284 BITFIELD (RegKludge),
285 BITFIELD (FirstXmm0),
286 BITFIELD (ByteOkIntel),
287 BITFIELD (ToDword),
288 BITFIELD (ToQword),
289 BITFIELD (AddrPrefixOp0),
290 BITFIELD (IsPrefix),
291 BITFIELD (ImmExt),
292 BITFIELD (NoRex64),
293 BITFIELD (Rex64),
294 BITFIELD (Ugh),
295 BITFIELD (Drex),
296 BITFIELD (Drexv),
297 BITFIELD (Drexc),
298 BITFIELD (OldGcc),
299 BITFIELD (ATTMnemonic),
300 BITFIELD (IntelMnemonic),
303 static bitfield operand_types[] =
305 BITFIELD (Reg8),
306 BITFIELD (Reg16),
307 BITFIELD (Reg32),
308 BITFIELD (Reg64),
309 BITFIELD (FloatReg),
310 BITFIELD (RegMMX),
311 BITFIELD (RegXMM),
312 BITFIELD (Imm8),
313 BITFIELD (Imm8S),
314 BITFIELD (Imm16),
315 BITFIELD (Imm32),
316 BITFIELD (Imm32S),
317 BITFIELD (Imm64),
318 BITFIELD (Imm1),
319 BITFIELD (BaseIndex),
320 BITFIELD (Disp8),
321 BITFIELD (Disp16),
322 BITFIELD (Disp32),
323 BITFIELD (Disp32S),
324 BITFIELD (Disp64),
325 BITFIELD (InOutPortReg),
326 BITFIELD (ShiftCount),
327 BITFIELD (Control),
328 BITFIELD (Debug),
329 BITFIELD (Test),
330 BITFIELD (SReg2),
331 BITFIELD (SReg3),
332 BITFIELD (Acc),
333 BITFIELD (FloatAcc),
334 BITFIELD (JumpAbsolute),
335 BITFIELD (EsSeg),
336 BITFIELD (RegMem),
337 #ifdef OTUnused
338 BITFIELD (OTUnused),
339 #endif
342 static int
343 compare (const void *x, const void *y)
345 const bitfield *xp = (const bitfield *) x;
346 const bitfield *yp = (const bitfield *) y;
347 return xp->position - yp->position;
350 static void
351 fail (const char *message, ...)
353 va_list args;
355 va_start (args, message);
356 fprintf (stderr, _("%s: Error: "), program_name);
357 vfprintf (stderr, message, args);
358 va_end (args);
359 xexit (1);
362 static void
363 process_copyright (FILE *fp)
365 fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
366 /* Copyright 2007, 2008 Free Software Foundation, Inc.\n\
368 This file is part of the GNU opcodes library.\n\
370 This library is free software; you can redistribute it and/or modify\n\
371 it under the terms of the GNU General Public License as published by\n\
372 the Free Software Foundation; either version 3, or (at your option)\n\
373 any later version.\n\
375 It is distributed in the hope that it will be useful, but WITHOUT\n\
376 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
377 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
378 License for more details.\n\
380 You should have received a copy of the GNU General Public License\n\
381 along with this program; if not, write to the Free Software\n\
382 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
383 MA 02110-1301, USA. */\n");
386 /* Remove leading white spaces. */
388 static char *
389 remove_leading_whitespaces (char *str)
391 while (ISSPACE (*str))
392 str++;
393 return str;
396 /* Remove trailing white spaces. */
398 static void
399 remove_trailing_whitespaces (char *str)
401 size_t last = strlen (str);
403 if (last == 0)
404 return;
408 last--;
409 if (ISSPACE (str [last]))
410 str[last] = '\0';
411 else
412 break;
414 while (last != 0);
417 /* Find next field separated by SEP and terminate it. Return a
418 pointer to the one after it. */
420 static char *
421 next_field (char *str, char sep, char **next)
423 char *p;
425 p = remove_leading_whitespaces (str);
426 for (str = p; *str != sep && *str != '\0'; str++);
428 *str = '\0';
429 remove_trailing_whitespaces (p);
431 *next = str + 1;
433 return p;
436 static void
437 set_bitfield (const char *f, bitfield *array, unsigned int size)
439 unsigned int i;
441 if (strcmp (f, "CpuSledgehammer") == 0)
442 f= "CpuK8";
444 for (i = 0; i < size; i++)
445 if (strcasecmp (array[i].name, f) == 0)
447 array[i].value = 1;
448 return;
451 printf ("Unknown bitfield: %s\n", f);
452 abort ();
455 static void
456 output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
457 int macro, const char *comma, const char *indent)
459 unsigned int i;
461 fprintf (table, "%s{ { ", indent);
463 for (i = 0; i < size - 1; i++)
465 fprintf (table, "%d, ", flags[i].value);
466 if (((i + 1) % 20) == 0)
468 /* We need \\ for macro. */
469 if (macro)
470 fprintf (table, " \\\n %s", indent);
471 else
472 fprintf (table, "\n %s", indent);
476 fprintf (table, "%d } }%s\n", flags[i].value, comma);
479 static void
480 process_i386_cpu_flag (FILE *table, char *flag, int macro,
481 const char *comma, const char *indent)
483 char *str, *next, *last;
484 bitfield flags [ARRAY_SIZE (cpu_flags)];
486 /* Copy the default cpu flags. */
487 memcpy (flags, cpu_flags, sizeof (cpu_flags));
489 if (strcasecmp (flag, "unknown") == 0)
491 unsigned int i;
493 /* We turn on everything except for cpu64 in case of
494 CPU_UNKNOWN_FLAGS. */
495 for (i = 0; i < ARRAY_SIZE (flags); i++)
496 if (flags[i].position != Cpu64)
497 flags[i].value = 1;
499 else if (strcmp (flag, "0"))
501 last = flag + strlen (flag);
502 for (next = flag; next && next < last; )
504 str = next_field (next, '|', &next);
505 if (str)
506 set_bitfield (str, flags, ARRAY_SIZE (flags));
510 output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
511 comma, indent);
514 static void
515 output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
517 unsigned int i;
519 fprintf (table, " { ");
521 for (i = 0; i < size - 1; i++)
523 fprintf (table, "%d, ", modifier[i].value);
524 if (((i + 1) % 20) == 0)
525 fprintf (table, "\n ");
528 fprintf (table, "%d },\n", modifier[i].value);
531 static void
532 process_i386_opcode_modifier (FILE *table, char *mod)
534 char *str, *next, *last;
535 bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
537 /* Copy the default opcode modifier. */
538 memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
540 if (strcmp (mod, "0"))
542 last = mod + strlen (mod);
543 for (next = mod; next && next < last; )
545 str = next_field (next, '|', &next);
546 if (str)
547 set_bitfield (str, modifiers, ARRAY_SIZE (modifiers));
550 output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
553 static void
554 output_operand_type (FILE *table, bitfield *types, unsigned int size,
555 int macro, const char *indent)
557 unsigned int i;
559 fprintf (table, "{ { ");
561 for (i = 0; i < size - 1; i++)
563 fprintf (table, "%d, ", types[i].value);
564 if (((i + 1) % 20) == 0)
566 /* We need \\ for macro. */
567 if (macro)
568 fprintf (table, "\\\n%s", indent);
569 else
570 fprintf (table, "\n%s", indent);
574 fprintf (table, "%d } }", types[i].value);
577 static void
578 process_i386_operand_type (FILE *table, char *op, int macro,
579 const char *indent)
581 char *str, *next, *last;
582 bitfield types [ARRAY_SIZE (operand_types)];
584 /* Copy the default operand type. */
585 memcpy (types, operand_types, sizeof (types));
587 if (strcmp (op, "0"))
589 last = op + strlen (op);
590 for (next = op; next && next < last; )
592 str = next_field (next, '|', &next);
593 if (str)
594 set_bitfield (str, types, ARRAY_SIZE (types));
597 output_operand_type (table, types, ARRAY_SIZE (types), macro,
598 indent);
601 static void
602 process_i386_opcodes (FILE *table)
604 FILE *fp = fopen ("i386-opc.tbl", "r");
605 char buf[2048];
606 unsigned int i;
607 char *str, *p, *last;
608 char *name, *operands, *base_opcode, *extension_opcode;
609 char *opcode_length;
610 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
612 if (fp == NULL)
613 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
614 xstrerror (errno));
616 fprintf (table, "\n/* i386 opcode table. */\n\n");
617 fprintf (table, "const template i386_optab[] =\n{\n");
619 while (!feof (fp))
621 if (fgets (buf, sizeof (buf), fp) == NULL)
622 break;
624 p = remove_leading_whitespaces (buf);
626 /* Skip comments. */
627 str = strstr (p, "//");
628 if (str != NULL)
629 str[0] = '\0';
631 /* Remove trailing white spaces. */
632 remove_trailing_whitespaces (p);
634 switch (p[0])
636 case '#':
637 fprintf (table, "%s\n", p);
638 case '\0':
639 continue;
640 break;
641 default:
642 break;
645 last = p + strlen (p);
647 /* Find name. */
648 name = next_field (p, ',', &str);
650 if (str >= last)
651 abort ();
653 /* Find number of operands. */
654 operands = next_field (str, ',', &str);
656 if (str >= last)
657 abort ();
659 /* Find base_opcode. */
660 base_opcode = next_field (str, ',', &str);
662 if (str >= last)
663 abort ();
665 /* Find extension_opcode. */
666 extension_opcode = next_field (str, ',', &str);
668 if (str >= last)
669 abort ();
671 /* Find opcode_length. */
672 opcode_length = next_field (str, ',', &str);
674 if (str >= last)
675 abort ();
677 /* Find cpu_flags. */
678 cpu_flags = next_field (str, ',', &str);
680 if (str >= last)
681 abort ();
683 /* Find opcode_modifier. */
684 opcode_modifier = next_field (str, ',', &str);
686 if (str >= last)
687 abort ();
689 /* Remove the first {. */
690 str = remove_leading_whitespaces (str);
691 if (*str != '{')
692 abort ();
693 str = remove_leading_whitespaces (str + 1);
695 i = strlen (str);
697 /* There are at least "X}". */
698 if (i < 2)
699 abort ();
701 /* Remove trailing white spaces and }. */
704 i--;
705 if (ISSPACE (str[i]) || str[i] == '}')
706 str[i] = '\0';
707 else
708 break;
710 while (i != 0);
712 last = str + i;
714 /* Find operand_types. */
715 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
717 if (str >= last)
719 operand_types [i] = NULL;
720 break;
723 operand_types [i] = next_field (str, ',', &str);
724 if (*operand_types[i] == '0')
726 if (i != 0)
727 operand_types[i] = NULL;
728 break;
732 fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
733 name, operands, base_opcode, extension_opcode,
734 opcode_length);
736 process_i386_cpu_flag (table, cpu_flags, 0, ",", " ");
738 process_i386_opcode_modifier (table, opcode_modifier);
740 fprintf (table, " { ");
742 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
744 if (operand_types[i] == NULL
745 || *operand_types[i] == '0')
747 if (i == 0)
748 process_i386_operand_type (table, "0", 0, "\t ");
749 break;
752 if (i != 0)
753 fprintf (table, ",\n ");
755 process_i386_operand_type (table, operand_types[i], 0,
756 "\t ");
758 fprintf (table, " } },\n");
761 fclose (fp);
763 fprintf (table, " { NULL, 0, 0, 0, 0,\n");
765 process_i386_cpu_flag (table, "0", 0, ",", " ");
767 process_i386_opcode_modifier (table, "0");
769 fprintf (table, " { ");
770 process_i386_operand_type (table, "0", 0, "\t ");
771 fprintf (table, " } }\n");
773 fprintf (table, "};\n");
776 static void
777 process_i386_registers (FILE *table)
779 FILE *fp = fopen ("i386-reg.tbl", "r");
780 char buf[2048];
781 char *str, *p, *last;
782 char *reg_name, *reg_type, *reg_flags, *reg_num;
784 if (fp == NULL)
785 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
786 xstrerror (errno));
788 fprintf (table, "\n/* i386 register table. */\n\n");
789 fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
791 while (!feof (fp))
793 if (fgets (buf, sizeof (buf), fp) == NULL)
794 break;
796 p = remove_leading_whitespaces (buf);
798 /* Skip comments. */
799 str = strstr (p, "//");
800 if (str != NULL)
801 str[0] = '\0';
803 /* Remove trailing white spaces. */
804 remove_trailing_whitespaces (p);
806 switch (p[0])
808 case '#':
809 fprintf (table, "%s\n", p);
810 case '\0':
811 continue;
812 break;
813 default:
814 break;
817 last = p + strlen (p);
819 /* Find reg_name. */
820 reg_name = next_field (p, ',', &str);
822 if (str >= last)
823 abort ();
825 /* Find reg_type. */
826 reg_type = next_field (str, ',', &str);
828 if (str >= last)
829 abort ();
831 /* Find reg_flags. */
832 reg_flags = next_field (str, ',', &str);
834 if (str >= last)
835 abort ();
837 /* Find reg_num. */
838 reg_num = next_field (str, ',', &str);
840 fprintf (table, " { \"%s\",\n ", reg_name);
842 process_i386_operand_type (table, reg_type, 0, "\t");
844 fprintf (table, ",\n %s, %s },\n", reg_flags, reg_num);
847 fclose (fp);
849 fprintf (table, "};\n");
851 fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
854 static void
855 process_i386_initializers (void)
857 unsigned int i;
858 FILE *fp = fopen ("i386-init.h", "w");
859 char *init;
861 if (fp == NULL)
862 fail (_("can't create i386-init.h, errno = %s\n"),
863 xstrerror (errno));
865 process_copyright (fp);
867 for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
869 fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
870 init = xstrdup (cpu_flag_init[i].init);
871 process_i386_cpu_flag (fp, init, 1, "", " ");
872 free (init);
875 for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
877 fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
878 init = xstrdup (operand_type_init[i].init);
879 process_i386_operand_type (fp, init, 1, " ");
880 free (init);
882 fprintf (fp, "\n");
884 fclose (fp);
887 /* Program options. */
888 #define OPTION_SRCDIR 200
890 struct option long_options[] =
892 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
893 {"debug", no_argument, NULL, 'd'},
894 {"version", no_argument, NULL, 'V'},
895 {"help", no_argument, NULL, 'h'},
896 {0, no_argument, NULL, 0}
899 static void
900 print_version (void)
902 printf ("%s: version 1.0\n", program_name);
903 xexit (0);
906 static void
907 usage (FILE * stream, int status)
909 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
910 program_name);
911 xexit (status);
915 main (int argc, char **argv)
917 extern int chdir (char *);
918 char *srcdir = NULL;
919 int c;
920 FILE *table;
922 program_name = *argv;
923 xmalloc_set_program_name (program_name);
925 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
926 switch (c)
928 case OPTION_SRCDIR:
929 srcdir = optarg;
930 break;
931 case 'V':
932 case 'v':
933 print_version ();
934 break;
935 case 'd':
936 debug = 1;
937 break;
938 case 'h':
939 case '?':
940 usage (stderr, 0);
941 default:
942 case 0:
943 break;
946 if (optind != argc)
947 usage (stdout, 1);
949 if (srcdir != NULL)
950 if (chdir (srcdir) != 0)
951 fail (_("unable to change directory to \"%s\", errno = %s\n"),
952 srcdir, xstrerror (errno));
954 /* Check the unused bitfield in i386_cpu_flags. */
955 #ifndef CpuUnused
956 c = CpuNumOfBits - CpuMax - 1;
957 if (c)
958 fail (_("%d unused bits in i386_cpu_flags.\n"), c);
959 #endif
961 /* Check the unused bitfield in i386_operand_type. */
962 #ifndef OTUnused
963 c = OTNumOfBits - OTMax - 1;
964 if (c)
965 fail (_("%d unused bits in i386_operand_type.\n"), c);
966 #endif
968 qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
969 compare);
971 qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
972 sizeof (opcode_modifiers [0]), compare);
974 qsort (operand_types, ARRAY_SIZE (operand_types),
975 sizeof (operand_types [0]), compare);
977 table = fopen ("i386-tbl.h", "w");
978 if (table == NULL)
979 fail (_("can't create i386-tbl.h, errno = %s\n"),
980 xstrerror (errno));
982 process_copyright (table);
984 process_i386_opcodes (table);
985 process_i386_registers (table);
986 process_i386_initializers ();
988 fclose (table);
990 exit (0);