1 /* Copyright (C) 2007-2014 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. */
24 #include "libiberty.h"
26 #include "safe-ctype.h"
31 #define _(String) gettext (String)
33 static const char *program_name = NULL;
36 typedef struct initializer
42 static initializer cpu_flag_init[] =
44 { "CPU_UNKNOWN_FLAGS",
45 "~(CpuL1OM|CpuK1OM)" },
46 { "CPU_GENERIC32_FLAGS",
47 "Cpu186|Cpu286|Cpu386" },
48 { "CPU_GENERIC64_FLAGS",
49 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuLM" },
57 "Cpu186|Cpu286|Cpu386" },
59 "Cpu186|Cpu286|Cpu386|Cpu486" },
61 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu387" },
63 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687" },
64 { "CPU_PENTIUMPRO_FLAGS",
65 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop" },
67 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX" },
69 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE" },
71 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2" },
73 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuLM|CpuCX16" },
75 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuCX16" },
77 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuLM|CpuCX16" },
79 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuRdtscp|CpuLM|CpuCX16" },
81 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX" },
83 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX|Cpu3dnow" },
85 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA" },
87 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuLM" },
88 { "CPU_AMDFAM10_FLAGS",
89 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|C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uABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW|CpuXsave|CpuXsaveopt|CpuFSGSBase|CpuAVX2|CpuMovbe|CpuBMI2|CpuRdRnd" },
99 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4a|CpuABM|CpuLM|CpuPRFCHW|CpuCX16|CpuClflush|CpuFISTTP|CpuSVME|CpuLZCNT" },
100 { "CPU_BTVER2_FLAGS",
101 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4a|CpuSSE4_1|CpuSSE4_2|CpuABM|CpuLM|CpuBMI|CpuF16C|CpuAES|CpuPCLMUL|CpuAVX|CpuMovbe|CpuXsave|CpuXsaveopt|CpuPRFCHW|CpuCX16|CpuClflush|CpuFISTTP|CpuSVME|CpuLZCNT" },
109 "Cpu8087|Cpu287|Cpu387|Cpu687|CpuFISTTP" },
110 { "CPU_CLFLUSH_FLAGS",
114 { "CPU_SYSCALL_FLAGS",
121 "CpuMMX|CpuSSE|CpuSSE2" },
123 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
125 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3" },
126 { "CPU_SSE4_1_FLAGS",
127 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1" },
128 { "CPU_SSE4_2_FLAGS",
129 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|Cpu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SSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F" },
200 { "CPU_AVX512CD_FLAGS",
201 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD" },
202 { "CPU_AVX512ER_FLAGS",
203 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512ER" },
204 { "CPU_AVX512PF_FLAGS",
205 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512PF" },
206 { "CPU_ANY_AVX_FLAGS",
207 "CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD|CpuAVX512ER|CpuAVX512PF" },
214 { "CPU_RDSEED_FLAGS",
216 { "CPU_PRFCHW_FLAGS",
224 { "CPU_CLFLUSHOPT_FLAGS",
226 { "CPU_XSAVES_FLAGS",
228 { "CPU_XSAVEC_FLAGS",
230 { "CPU_PREFETCHWT1_FLAGS",
234 { "CPU_AVX512DQ_FLAGS",
235 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512DQ" },
236 { "CPU_AVX512BW_FLAGS",
237 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512BW" },
238 { "CPU_AVX512VL_FLAGS",
239 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512VL" },
244 static initializer operand_type_init[] =
246 { "OPERAND_TYPE_NONE",
248 { "OPERAND_TYPE_REG8",
250 { "OPERAND_TYPE_REG16",
252 { "OPERAND_TYPE_REG32",
254 { "OPERAND_TYPE_REG64",
256 { "OPERAND_TYPE_IMM1",
258 { "OPERAND_TYPE_IMM8",
260 { "OPERAND_TYPE_IMM8S",
262 { "OPERAND_TYPE_IMM16",
264 { "OPERAND_TYPE_IMM32",
266 { "OPERAND_TYPE_IMM32S",
268 { "OPERAND_TYPE_IMM64",
270 { "OPERAND_TYPE_BASEINDEX",
272 { "OPERAND_TYPE_DISP8",
274 { "OPERAND_TYPE_DISP16",
276 { "OPERAND_TYPE_DISP32",
278 { "OPERAND_TYPE_DISP32S",
280 { "OPERAND_TYPE_DISP64",
282 { "OPERAND_TYPE_INOUTPORTREG",
284 { "OPERAND_TYPE_SHIFTCOUNT",
286 { "OPERAND_TYPE_CONTROL",
288 { "OPERAND_TYPE_TEST",
290 { "OPERAND_TYPE_DEBUG",
292 { "OPERAND_TYPE_FLOATREG",
294 { "OPERAND_TYPE_FLOATACC",
296 { "OPERAND_TYPE_SREG2",
298 { "OPERAND_TYPE_SREG3",
300 { "OPERAND_TYPE_ACC",
302 { "OPERAND_TYPE_JUMPABSOLUTE",
304 { "OPERAND_TYPE_REGMMX",
306 { "OPERAND_TYPE_REGXMM",
308 { "OPERAND_TYPE_REGYMM",
310 { "OPERAND_TYPE_REGZMM",
312 { "OPERAND_TYPE_REGMASK",
314 { "OPERAND_TYPE_ESSEG",
316 { "OPERAND_TYPE_ACC32",
318 { "OPERAND_TYPE_ACC64",
320 { "OPERAND_TYPE_INOUTPORTREG",
322 { "OPERAND_TYPE_REG16_INOUTPORTREG",
323 "Reg16|InOutPortReg" },
324 { "OPERAND_TYPE_DISP16_32",
326 { "OPERAND_TYPE_ANYDISP",
327 "Disp8|Disp16|Disp32|Disp32S|Disp64" },
328 { "OPERAND_TYPE_IMM16_32",
330 { "OPERAND_TYPE_IMM16_32S",
332 { "OPERAND_TYPE_IMM16_32_32S",
333 "Imm16|Imm32|Imm32S" },
334 { "OPERAND_TYPE_IMM32_64",
336 { "OPERAND_TYPE_IMM32_32S_DISP32",
337 "Imm32|Imm32S|Disp32" },
338 { "OPERAND_TYPE_IMM64_DISP64",
340 { "OPERAND_TYPE_IMM32_32S_64_DISP32",
341 "Imm32|Imm32S|Imm64|Disp32" },
342 { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
343 "Imm32|Imm32S|Imm64|Disp32|Disp64" },
344 { "OPERAND_TYPE_VEC_IMM4",
346 { "OPERAND_TYPE_REGBND",
348 { "OPERAND_TYPE_VEC_DISP8",
352 typedef struct bitfield
359 #define BITFIELD(n) { n, 0, #n }
361 static bitfield cpu_flags[] =
369 BITFIELD (CpuClflush),
371 BITFIELD (CpuSYSCALL),
376 BITFIELD (CpuFISTTP),
382 BITFIELD (CpuSSE4_1),
383 BITFIELD (CpuSSE4_2),
386 BITFIELD (CpuAVX512F),
387 BITFIELD (CpuAVX512CD),
388 BITFIELD (CpuAVX512ER),
389 BITFIELD (CpuAVX512PF),
390 BITFIELD (CpuAVX512VL),
391 BITFIELD (CpuAVX512DQ),
392 BITFIELD (CpuAVX512BW),
397 BITFIELD (Cpu3dnowA),
398 BITFIELD (CpuPadLock),
404 BITFIELD (CpuXsaveopt),
406 BITFIELD (CpuPCLMUL),
417 BITFIELD (CpuRdtscp),
418 BITFIELD (CpuFSGSBase),
425 BITFIELD (CpuINVPCID),
426 BITFIELD (CpuVMFUNC),
427 BITFIELD (CpuRDSEED),
429 BITFIELD (CpuPRFCHW),
433 BITFIELD (CpuClflushOpt),
434 BITFIELD (CpuXSAVES),
435 BITFIELD (CpuXSAVEC),
436 BITFIELD (CpuPREFETCHWT1),
443 BITFIELD (CpuUnused),
447 static bitfield opcode_modifiers[] =
453 BITFIELD (ShortForm),
455 BITFIELD (JumpDword),
457 BITFIELD (JumpInterSegment),
464 BITFIELD (CheckRegSize),
465 BITFIELD (IgnoreSize),
466 BITFIELD (DefaultSize),
475 BITFIELD (BNDPrefixOk),
476 BITFIELD (IsLockable),
477 BITFIELD (RegKludge),
478 BITFIELD (FirstXmm0),
479 BITFIELD (Implicit1stXmm0),
480 BITFIELD (RepPrefixOk),
481 BITFIELD (HLEPrefixOk),
484 BITFIELD (AddrPrefixOp0),
493 BITFIELD (VexOpcode),
494 BITFIELD (VexSources),
495 BITFIELD (VexImmExt),
502 BITFIELD (Broadcast),
503 BITFIELD (StaticRounding),
505 BITFIELD (Disp8MemShift),
506 BITFIELD (NoDefMask),
508 BITFIELD (ATTMnemonic),
509 BITFIELD (ATTSyntax),
510 BITFIELD (IntelSyntax),
513 static bitfield operand_types[] =
532 BITFIELD (BaseIndex),
538 BITFIELD (InOutPortReg),
539 BITFIELD (ShiftCount),
547 BITFIELD (JumpAbsolute),
560 BITFIELD (Unspecified),
564 BITFIELD (Vec_Disp8),
570 static const char *filename;
573 compare (const void *x, const void *y)
575 const bitfield *xp = (const bitfield *) x;
576 const bitfield *yp = (const bitfield *) y;
577 return xp->position - yp->position;
581 fail (const char *message, ...)
585 va_start (args, message);
586 fprintf (stderr, _("%s: Error: "), program_name);
587 vfprintf (stderr, message, args);
593 process_copyright (FILE *fp)
595 fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
596 /* Copyright (C) 2007-2014 Free Software Foundation, Inc.\n\
598 This file is part of the GNU opcodes library.\n\
600 This library is free software; you can redistribute it and/or modify\n\
601 it under the terms of the GNU General Public License as published by\n\
602 the Free Software Foundation; either version 3, or (at your option)\n\
603 any later version.\n\
605 It is distributed in the hope that it will be useful, but WITHOUT\n\
606 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
607 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
608 License for more details.\n\
610 You should have received a copy of the GNU General Public License\n\
611 along with this program; if not, write to the Free Software\n\
612 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
613 MA 02110-1301, USA. */\n");
616 /* Remove leading white spaces. */
619 remove_leading_whitespaces (char *str)
621 while (ISSPACE (*str))
626 /* Remove trailing white spaces. */
629 remove_trailing_whitespaces (char *str)
631 size_t last = strlen (str);
639 if (ISSPACE (str [last]))
647 /* Find next field separated by SEP and terminate it. Return a
648 pointer to the one after it. */
651 next_field (char *str, char sep, char **next, char *last)
655 p = remove_leading_whitespaces (str);
656 for (str = p; *str != sep && *str != '\0'; str++);
659 remove_trailing_whitespaces (p);
670 set_bitfield (const char *f, bitfield *array, int value,
671 unsigned int size, int lineno)
675 if (strcmp (f, "CpuFP") == 0)
677 set_bitfield("Cpu387", array, value, size, lineno);
678 set_bitfield("Cpu287", array, value, size, lineno);
681 else if (strcmp (f, "Mmword") == 0)
683 else if (strcmp (f, "Oword") == 0)
686 for (i = 0; i < size; i++)
687 if (strcasecmp (array[i].name, f) == 0)
689 array[i].value = value;
695 const char *v = strchr (f, '=');
702 for (i = 0; i < size; i++)
703 if (strncasecmp (array[i].name, f, n) == 0)
705 value = strtol (v + 1, &end, 0);
708 array[i].value = value;
717 fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f);
719 fail (_("Unknown bitfield: %s\n"), f);
723 output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
724 int macro, const char *comma, const char *indent)
728 fprintf (table, "%s{ { ", indent);
730 for (i = 0; i < size - 1; i++)
732 if (((i + 1) % 20) != 0)
733 fprintf (table, "%d, ", flags[i].value);
735 fprintf (table, "%d,", flags[i].value);
736 if (((i + 1) % 20) == 0)
738 /* We need \\ for macro. */
740 fprintf (table, " \\\n %s", indent);
742 fprintf (table, "\n %s", indent);
746 fprintf (table, "%d } }%s\n", flags[i].value, comma);
750 process_i386_cpu_flag (FILE *table, char *flag, int macro,
751 const char *comma, const char *indent,
754 char *str, *next, *last;
756 bitfield flags [ARRAY_SIZE (cpu_flags)];
758 /* Copy the default cpu flags. */
759 memcpy (flags, cpu_flags, sizeof (cpu_flags));
761 if (strcasecmp (flag, "unknown") == 0)
763 /* We turn on everything except for cpu64 in case of
764 CPU_UNKNOWN_FLAGS. */
765 for (i = 0; i < ARRAY_SIZE (flags); i++)
766 if (flags[i].position != Cpu64)
769 else if (flag[0] == '~')
771 last = flag + strlen (flag);
778 fail (_("%s: %d: Missing `)' in bitfield: %s\n"), filename,
785 /* First we turn on everything except for cpu64. */
786 for (i = 0; i < ARRAY_SIZE (flags); i++)
787 if (flags[i].position != Cpu64)
790 /* Turn off selective bits. */
791 for (; next && next < last; )
793 str = next_field (next, '|', &next, last);
795 set_bitfield (str, flags, 0, ARRAY_SIZE (flags), lineno);
798 else if (strcmp (flag, "0"))
800 /* Turn on selective bits. */
801 last = flag + strlen (flag);
802 for (next = flag; next && next < last; )
804 str = next_field (next, '|', &next, last);
806 set_bitfield (str, flags, 1, ARRAY_SIZE (flags), lineno);
810 output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
815 output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
819 fprintf (table, " { ");
821 for (i = 0; i < size - 1; i++)
823 if (((i + 1) % 20) != 0)
824 fprintf (table, "%d, ", modifier[i].value);
826 fprintf (table, "%d,", modifier[i].value);
827 if (((i + 1) % 20) == 0)
828 fprintf (table, "\n ");
831 fprintf (table, "%d },\n", modifier[i].value);
835 process_i386_opcode_modifier (FILE *table, char *mod, int lineno)
837 char *str, *next, *last;
838 bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
840 /* Copy the default opcode modifier. */
841 memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
843 if (strcmp (mod, "0"))
845 last = mod + strlen (mod);
846 for (next = mod; next && next < last; )
848 str = next_field (next, '|', &next, last);
850 set_bitfield (str, modifiers, 1, ARRAY_SIZE (modifiers),
854 output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
858 output_operand_type (FILE *table, bitfield *types, unsigned int size,
859 int macro, const char *indent)
863 fprintf (table, "{ { ");
865 for (i = 0; i < size - 1; i++)
867 if (((i + 1) % 20) != 0)
868 fprintf (table, "%d, ", types[i].value);
870 fprintf (table, "%d,", types[i].value);
871 if (((i + 1) % 20) == 0)
873 /* We need \\ for macro. */
875 fprintf (table, " \\\n%s", indent);
877 fprintf (table, "\n%s", indent);
881 fprintf (table, "%d } }", types[i].value);
885 process_i386_operand_type (FILE *table, char *op, int macro,
886 const char *indent, int lineno)
888 char *str, *next, *last;
889 bitfield types [ARRAY_SIZE (operand_types)];
891 /* Copy the default operand type. */
892 memcpy (types, operand_types, sizeof (types));
894 if (strcmp (op, "0"))
896 last = op + strlen (op);
897 for (next = op; next && next < last; )
899 str = next_field (next, '|', &next, last);
901 set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno);
904 output_operand_type (table, types, ARRAY_SIZE (types), macro,
909 output_i386_opcode (FILE *table, const char *name, char *str,
910 char *last, int lineno)
913 char *operands, *base_opcode, *extension_opcode, *opcode_length;
914 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
916 /* Find number of operands. */
917 operands = next_field (str, ',', &str, last);
919 /* Find base_opcode. */
920 base_opcode = next_field (str, ',', &str, last);
922 /* Find extension_opcode. */
923 extension_opcode = next_field (str, ',', &str, last);
925 /* Find opcode_length. */
926 opcode_length = next_field (str, ',', &str, last);
928 /* Find cpu_flags. */
929 cpu_flags = next_field (str, ',', &str, last);
931 /* Find opcode_modifier. */
932 opcode_modifier = next_field (str, ',', &str, last);
934 /* Remove the first {. */
935 str = remove_leading_whitespaces (str);
938 str = remove_leading_whitespaces (str + 1);
942 /* There are at least "X}". */
946 /* Remove trailing white spaces and }. */
950 if (ISSPACE (str[i]) || str[i] == '}')
959 /* Find operand_types. */
960 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
964 operand_types [i] = NULL;
968 operand_types [i] = next_field (str, ',', &str, last);
969 if (*operand_types[i] == '0')
972 operand_types[i] = NULL;
977 fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
978 name, operands, base_opcode, extension_opcode,
981 process_i386_cpu_flag (table, cpu_flags, 0, ",", " ", lineno);
983 process_i386_opcode_modifier (table, opcode_modifier, lineno);
985 fprintf (table, " { ");
987 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
989 if (operand_types[i] == NULL || *operand_types[i] == '0')
992 process_i386_operand_type (table, "0", 0, "\t ", lineno);
997 fprintf (table, ",\n ");
999 process_i386_operand_type (table, operand_types[i], 0,
1002 fprintf (table, " } },\n");
1005 struct opcode_hash_entry
1007 struct opcode_hash_entry *next;
1013 /* Calculate the hash value of an opcode hash entry P. */
1016 opcode_hash_hash (const void *p)
1018 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
1019 return htab_hash_string (entry->name);
1022 /* Compare a string Q against an opcode hash entry P. */
1025 opcode_hash_eq (const void *p, const void *q)
1027 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
1028 const char *name = (const char *) q;
1029 return strcmp (name, entry->name) == 0;
1033 process_i386_opcodes (FILE *table)
1038 char *str, *p, *last, *name;
1039 struct opcode_hash_entry **hash_slot, **entry, *next;
1040 htab_t opcode_hash_table;
1041 struct opcode_hash_entry **opcode_array;
1042 unsigned int opcode_array_size = 1024;
1045 filename = "i386-opc.tbl";
1046 fp = fopen (filename, "r");
1049 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
1053 opcode_array = (struct opcode_hash_entry **)
1054 xmalloc (sizeof (*opcode_array) * opcode_array_size);
1056 opcode_hash_table = htab_create_alloc (16, opcode_hash_hash,
1057 opcode_hash_eq, NULL,
1060 fprintf (table, "\n/* i386 opcode table. */\n\n");
1061 fprintf (table, "const insn_template i386_optab[] =\n{\n");
1063 /* Put everything on opcode array. */
1066 if (fgets (buf, sizeof (buf), fp) == NULL)
1071 p = remove_leading_whitespaces (buf);
1073 /* Skip comments. */
1074 str = strstr (p, "//");
1078 /* Remove trailing white spaces. */
1079 remove_trailing_whitespaces (p);
1084 /* Ignore comments. */
1092 last = p + strlen (p);
1095 name = next_field (p, ',', &str, last);
1097 /* Get the slot in hash table. */
1098 hash_slot = (struct opcode_hash_entry **)
1099 htab_find_slot_with_hash (opcode_hash_table, name,
1100 htab_hash_string (name),
1103 if (*hash_slot == NULL)
1105 /* It is the new one. Put it on opcode array. */
1106 if (i >= opcode_array_size)
1108 /* Grow the opcode array when needed. */
1109 opcode_array_size += 1024;
1110 opcode_array = (struct opcode_hash_entry **)
1111 xrealloc (opcode_array,
1112 sizeof (*opcode_array) * opcode_array_size);
1115 opcode_array[i] = (struct opcode_hash_entry *)
1116 xmalloc (sizeof (struct opcode_hash_entry));
1117 opcode_array[i]->next = NULL;
1118 opcode_array[i]->name = xstrdup (name);
1119 opcode_array[i]->opcode = xstrdup (str);
1120 opcode_array[i]->lineno = lineno;
1121 *hash_slot = opcode_array[i];
1126 /* Append it to the existing one. */
1128 while ((*entry) != NULL)
1129 entry = &(*entry)->next;
1130 *entry = (struct opcode_hash_entry *)
1131 xmalloc (sizeof (struct opcode_hash_entry));
1132 (*entry)->next = NULL;
1133 (*entry)->name = (*hash_slot)->name;
1134 (*entry)->opcode = xstrdup (str);
1135 (*entry)->lineno = lineno;
1139 /* Process opcode array. */
1140 for (j = 0; j < i; j++)
1142 for (next = opcode_array[j]; next; next = next->next)
1146 lineno = next->lineno;
1147 last = str + strlen (str);
1148 output_i386_opcode (table, name, str, last, lineno);
1154 fprintf (table, " { NULL, 0, 0, 0, 0,\n");
1156 process_i386_cpu_flag (table, "0", 0, ",", " ", -1);
1158 process_i386_opcode_modifier (table, "0", -1);
1160 fprintf (table, " { ");
1161 process_i386_operand_type (table, "0", 0, "\t ", -1);
1162 fprintf (table, " } }\n");
1164 fprintf (table, "};\n");
1168 process_i386_registers (FILE *table)
1172 char *str, *p, *last;
1173 char *reg_name, *reg_type, *reg_flags, *reg_num;
1174 char *dw2_32_num, *dw2_64_num;
1177 filename = "i386-reg.tbl";
1178 fp = fopen (filename, "r");
1180 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
1183 fprintf (table, "\n/* i386 register table. */\n\n");
1184 fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
1188 if (fgets (buf, sizeof (buf), fp) == NULL)
1193 p = remove_leading_whitespaces (buf);
1195 /* Skip comments. */
1196 str = strstr (p, "//");
1200 /* Remove trailing white spaces. */
1201 remove_trailing_whitespaces (p);
1206 fprintf (table, "%s\n", p);
1214 last = p + strlen (p);
1216 /* Find reg_name. */
1217 reg_name = next_field (p, ',', &str, last);
1219 /* Find reg_type. */
1220 reg_type = next_field (str, ',', &str, last);
1222 /* Find reg_flags. */
1223 reg_flags = next_field (str, ',', &str, last);
1226 reg_num = next_field (str, ',', &str, last);
1228 fprintf (table, " { \"%s\",\n ", reg_name);
1230 process_i386_operand_type (table, reg_type, 0, "\t", lineno);
1232 /* Find 32-bit Dwarf2 register number. */
1233 dw2_32_num = next_field (str, ',', &str, last);
1235 /* Find 64-bit Dwarf2 register number. */
1236 dw2_64_num = next_field (str, ',', &str, last);
1238 fprintf (table, ",\n %s, %s, { %s, %s } },\n",
1239 reg_flags, reg_num, dw2_32_num, dw2_64_num);
1244 fprintf (table, "};\n");
1246 fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
1250 process_i386_initializers (void)
1253 FILE *fp = fopen ("i386-init.h", "w");
1257 fail (_("can't create i386-init.h, errno = %s\n"),
1260 process_copyright (fp);
1262 for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
1264 fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
1265 init = xstrdup (cpu_flag_init[i].init);
1266 process_i386_cpu_flag (fp, init, 1, "", " ", -1);
1270 for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
1272 fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
1273 init = xstrdup (operand_type_init[i].init);
1274 process_i386_operand_type (fp, init, 1, " ", -1);
1282 /* Program options. */
1283 #define OPTION_SRCDIR 200
1285 struct option long_options[] =
1287 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
1288 {"debug", no_argument, NULL, 'd'},
1289 {"version", no_argument, NULL, 'V'},
1290 {"help", no_argument, NULL, 'h'},
1291 {0, no_argument, NULL, 0}
1295 print_version (void)
1297 printf ("%s: version 1.0\n", program_name);
1302 usage (FILE * stream, int status)
1304 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
1310 main (int argc, char **argv)
1312 extern int chdir (char *);
1313 char *srcdir = NULL;
1317 program_name = *argv;
1318 xmalloc_set_program_name (program_name);
1320 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
1345 if (chdir (srcdir) != 0)
1346 fail (_("unable to change directory to \"%s\", errno = %s\n"),
1347 srcdir, xstrerror (errno));
1349 /* Check the unused bitfield in i386_cpu_flags. */
1351 c = CpuNumOfBits - CpuMax - 1;
1353 fail (_("%d unused bits in i386_cpu_flags.\n"), c);
1356 /* Check the unused bitfield in i386_operand_type. */
1358 c = OTNumOfBits - OTMax - 1;
1360 fail (_("%d unused bits in i386_operand_type.\n"), c);
1363 qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
1366 qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
1367 sizeof (opcode_modifiers [0]), compare);
1369 qsort (operand_types, ARRAY_SIZE (operand_types),
1370 sizeof (operand_types [0]), compare);
1372 table = fopen ("i386-tbl.h", "w");
1374 fail (_("can't create i386-tbl.h, errno = %s\n"),
1377 process_copyright (table);
1379 process_i386_opcodes (table);
1380 process_i386_registers (table);
1381 process_i386_initializers ();