1 /* Copyright 2007, 2008, 2009, 2010, 2011, 2012
2 Free Software Foundation, Inc.
4 This file is part of the GNU opcodes library.
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #include "libiberty.h"
27 #include "safe-ctype.h"
32 #define _(String) gettext (String)
34 static const char *program_name = NULL;
37 typedef struct initializer
43 static initializer cpu_flag_init[] =
45 { "CPU_UNKNOWN_FLAGS",
46 "~(CpuL1OM|CpuK1OM)" },
47 { "CPU_GENERIC32_FLAGS",
48 "Cpu186|Cpu286|Cpu386" },
49 { "CPU_GENERIC64_FLAGS",
50 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuLM" },
58 "Cpu186|Cpu286|Cpu386" },
60 "Cpu186|Cpu286|Cpu386|Cpu486" },
62 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu387" },
64 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687" },
65 { "CPU_PENTIUMPRO_FLAGS",
66 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop" },
68 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX" },
70 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE" },
72 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2" },
74 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuLM" },
76 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
78 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuLM" },
80 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuRdtscp|CpuLM" },
82 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX" },
84 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuNop|CpuMMX|Cpu3dnow" },
86 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA" },
88 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuLM" },
89 { "CPU_AMDFAM10_FLAGS",
90 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM" },
92 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA4|CpuXOP|CpuLWP" },
94 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C" },
102 "Cpu8087|Cpu287|Cpu387|Cpu687|CpuFISTTP" },
103 { "CPU_CLFLUSH_FLAGS",
107 { "CPU_SYSCALL_FLAGS",
114 "CpuMMX|CpuSSE|CpuSSE2" },
116 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
118 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3" },
119 { "CPU_SSE4_1_FLAGS",
120 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1" },
121 { "CPU_SSE4_2_FLAGS",
122 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2" },
123 { "CPU_ANY_SSE_FLAGS",
124 "CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuAVX|CpuAVX2" },
131 { "CPU_XSAVEOPT_FLAGS",
134 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAES" },
135 { "CPU_PCLMUL_FLAGS",
136 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuPCLMUL" },
138 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA" },
140 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA4" },
142 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuABM|CpuAVX|CpuFMA4|CpuXOP" },
151 { "CPU_RDTSCP_FLAGS",
155 { "CPU_FSGSBASE_FLAGS",
169 { "CPU_INVPCID_FLAGS",
171 { "CPU_VMFUNC_FLAGS",
175 { "CPU_3DNOWA_FLAGS",
176 "CpuMMX|Cpu3dnow|Cpu3dnowA" },
177 { "CPU_PADLOCK_FLAGS",
182 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a" },
186 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX" },
188 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2" },
189 { "CPU_ANY_AVX_FLAGS",
197 { "CPU_RDSEED_FLAGS",
199 { "CPU_PRFCHW_FLAGS",
203 static initializer operand_type_init[] =
205 { "OPERAND_TYPE_NONE",
207 { "OPERAND_TYPE_REG8",
209 { "OPERAND_TYPE_REG16",
211 { "OPERAND_TYPE_REG32",
213 { "OPERAND_TYPE_REG64",
215 { "OPERAND_TYPE_IMM1",
217 { "OPERAND_TYPE_IMM8",
219 { "OPERAND_TYPE_IMM8S",
221 { "OPERAND_TYPE_IMM16",
223 { "OPERAND_TYPE_IMM32",
225 { "OPERAND_TYPE_IMM32S",
227 { "OPERAND_TYPE_IMM64",
229 { "OPERAND_TYPE_BASEINDEX",
231 { "OPERAND_TYPE_DISP8",
233 { "OPERAND_TYPE_DISP16",
235 { "OPERAND_TYPE_DISP32",
237 { "OPERAND_TYPE_DISP32S",
239 { "OPERAND_TYPE_DISP64",
241 { "OPERAND_TYPE_INOUTPORTREG",
243 { "OPERAND_TYPE_SHIFTCOUNT",
245 { "OPERAND_TYPE_CONTROL",
247 { "OPERAND_TYPE_TEST",
249 { "OPERAND_TYPE_DEBUG",
251 { "OPERAND_TYPE_FLOATREG",
253 { "OPERAND_TYPE_FLOATACC",
255 { "OPERAND_TYPE_SREG2",
257 { "OPERAND_TYPE_SREG3",
259 { "OPERAND_TYPE_ACC",
261 { "OPERAND_TYPE_JUMPABSOLUTE",
263 { "OPERAND_TYPE_REGMMX",
265 { "OPERAND_TYPE_REGXMM",
267 { "OPERAND_TYPE_REGYMM",
269 { "OPERAND_TYPE_ESSEG",
271 { "OPERAND_TYPE_ACC32",
273 { "OPERAND_TYPE_ACC64",
275 { "OPERAND_TYPE_INOUTPORTREG",
277 { "OPERAND_TYPE_REG16_INOUTPORTREG",
278 "Reg16|InOutPortReg" },
279 { "OPERAND_TYPE_DISP16_32",
281 { "OPERAND_TYPE_ANYDISP",
282 "Disp8|Disp16|Disp32|Disp32S|Disp64" },
283 { "OPERAND_TYPE_IMM16_32",
285 { "OPERAND_TYPE_IMM16_32S",
287 { "OPERAND_TYPE_IMM16_32_32S",
288 "Imm16|Imm32|Imm32S" },
289 { "OPERAND_TYPE_IMM32_32S_DISP32",
290 "Imm32|Imm32S|Disp32" },
291 { "OPERAND_TYPE_IMM64_DISP64",
293 { "OPERAND_TYPE_IMM32_32S_64_DISP32",
294 "Imm32|Imm32S|Imm64|Disp32" },
295 { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
296 "Imm32|Imm32S|Imm64|Disp32|Disp64" },
297 { "OPERAND_TYPE_VEC_IMM4",
301 typedef struct bitfield
308 #define BITFIELD(n) { n, 0, #n }
310 static bitfield cpu_flags[] =
318 BITFIELD (CpuClflush),
320 BITFIELD (CpuSYSCALL),
325 BITFIELD (CpuFISTTP),
331 BITFIELD (CpuSSE4_1),
332 BITFIELD (CpuSSE4_2),
339 BITFIELD (Cpu3dnowA),
340 BITFIELD (CpuPadLock),
346 BITFIELD (CpuXsaveopt),
348 BITFIELD (CpuPCLMUL),
358 BITFIELD (CpuRdtscp),
359 BITFIELD (CpuFSGSBase),
366 BITFIELD (CpuINVPCID),
367 BITFIELD (CpuVMFUNC),
368 BITFIELD (CpuRDSEED),
370 BITFIELD (CpuPRFCHW),
374 BITFIELD (CpuUnused),
378 static bitfield opcode_modifiers[] =
384 BITFIELD (ShortForm),
386 BITFIELD (JumpDword),
388 BITFIELD (JumpInterSegment),
395 BITFIELD (CheckRegSize),
396 BITFIELD (IgnoreSize),
397 BITFIELD (DefaultSize),
406 BITFIELD (IsLockable),
407 BITFIELD (RegKludge),
408 BITFIELD (FirstXmm0),
409 BITFIELD (Implicit1stXmm0),
410 BITFIELD (RepPrefixOk),
411 BITFIELD (HLEPrefixOk),
414 BITFIELD (AddrPrefixOp0),
423 BITFIELD (VexOpcode),
424 BITFIELD (VexSources),
425 BITFIELD (VexImmExt),
430 BITFIELD (ATTMnemonic),
431 BITFIELD (ATTSyntax),
432 BITFIELD (IntelSyntax),
435 static bitfield operand_types[] =
452 BITFIELD (BaseIndex),
458 BITFIELD (InOutPortReg),
459 BITFIELD (ShiftCount),
467 BITFIELD (JumpAbsolute),
479 BITFIELD (Unspecified),
487 static const char *filename;
490 compare (const void *x, const void *y)
492 const bitfield *xp = (const bitfield *) x;
493 const bitfield *yp = (const bitfield *) y;
494 return xp->position - yp->position;
498 fail (const char *message, ...)
502 va_start (args, message);
503 fprintf (stderr, _("%s: Error: "), program_name);
504 vfprintf (stderr, message, args);
510 process_copyright (FILE *fp)
512 fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
513 /* Copyright 2007, 2008, 2009, 2010, 2011\n\
514 Free Software Foundation, Inc.\n\
516 This file is part of the GNU opcodes library.\n\
518 This library is free software; you can redistribute it and/or modify\n\
519 it under the terms of the GNU General Public License as published by\n\
520 the Free Software Foundation; either version 3, or (at your option)\n\
521 any later version.\n\
523 It is distributed in the hope that it will be useful, but WITHOUT\n\
524 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
525 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
526 License for more details.\n\
528 You should have received a copy of the GNU General Public License\n\
529 along with this program; if not, write to the Free Software\n\
530 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
531 MA 02110-1301, USA. */\n");
534 /* Remove leading white spaces. */
537 remove_leading_whitespaces (char *str)
539 while (ISSPACE (*str))
544 /* Remove trailing white spaces. */
547 remove_trailing_whitespaces (char *str)
549 size_t last = strlen (str);
557 if (ISSPACE (str [last]))
565 /* Find next field separated by SEP and terminate it. Return a
566 pointer to the one after it. */
569 next_field (char *str, char sep, char **next, char *last)
573 p = remove_leading_whitespaces (str);
574 for (str = p; *str != sep && *str != '\0'; str++);
577 remove_trailing_whitespaces (p);
588 set_bitfield (const char *f, bitfield *array, int value,
589 unsigned int size, int lineno)
593 if (strcmp (f, "CpuFP") == 0)
595 set_bitfield("Cpu387", array, value, size, lineno);
596 set_bitfield("Cpu287", array, value, size, lineno);
599 else if (strcmp (f, "Mmword") == 0)
601 else if (strcmp (f, "Oword") == 0)
604 for (i = 0; i < size; i++)
605 if (strcasecmp (array[i].name, f) == 0)
607 array[i].value = value;
613 const char *v = strchr (f, '=');
620 for (i = 0; i < size; i++)
621 if (strncasecmp (array[i].name, f, n) == 0)
623 value = strtol (v + 1, &end, 0);
626 array[i].value = value;
635 fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f);
637 fail (_("Unknown bitfield: %s\n"), f);
641 output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
642 int macro, const char *comma, const char *indent)
646 fprintf (table, "%s{ { ", indent);
648 for (i = 0; i < size - 1; i++)
650 fprintf (table, "%d, ", flags[i].value);
651 if (((i + 1) % 20) == 0)
653 /* We need \\ for macro. */
655 fprintf (table, " \\\n %s", indent);
657 fprintf (table, "\n %s", indent);
661 fprintf (table, "%d } }%s\n", flags[i].value, comma);
665 process_i386_cpu_flag (FILE *table, char *flag, int macro,
666 const char *comma, const char *indent,
669 char *str, *next, *last;
671 bitfield flags [ARRAY_SIZE (cpu_flags)];
673 /* Copy the default cpu flags. */
674 memcpy (flags, cpu_flags, sizeof (cpu_flags));
676 if (strcasecmp (flag, "unknown") == 0)
678 /* We turn on everything except for cpu64 in case of
679 CPU_UNKNOWN_FLAGS. */
680 for (i = 0; i < ARRAY_SIZE (flags); i++)
681 if (flags[i].position != Cpu64)
684 else if (flag[0] == '~')
686 last = flag + strlen (flag);
693 fail (_("%s: %d: Missing `)' in bitfield: %s\n"), filename,
700 /* First we turn on everything except for cpu64. */
701 for (i = 0; i < ARRAY_SIZE (flags); i++)
702 if (flags[i].position != Cpu64)
705 /* Turn off selective bits. */
706 for (; next && next < last; )
708 str = next_field (next, '|', &next, last);
710 set_bitfield (str, flags, 0, ARRAY_SIZE (flags), lineno);
713 else if (strcmp (flag, "0"))
715 /* Turn on selective bits. */
716 last = flag + strlen (flag);
717 for (next = flag; next && next < last; )
719 str = next_field (next, '|', &next, last);
721 set_bitfield (str, flags, 1, ARRAY_SIZE (flags), lineno);
725 output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
730 output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
734 fprintf (table, " { ");
736 for (i = 0; i < size - 1; i++)
738 fprintf (table, "%d, ", modifier[i].value);
739 if (((i + 1) % 20) == 0)
740 fprintf (table, "\n ");
743 fprintf (table, "%d },\n", modifier[i].value);
747 process_i386_opcode_modifier (FILE *table, char *mod, int lineno)
749 char *str, *next, *last;
750 bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
752 /* Copy the default opcode modifier. */
753 memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
755 if (strcmp (mod, "0"))
757 last = mod + strlen (mod);
758 for (next = mod; next && next < last; )
760 str = next_field (next, '|', &next, last);
762 set_bitfield (str, modifiers, 1, ARRAY_SIZE (modifiers),
766 output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
770 output_operand_type (FILE *table, bitfield *types, unsigned int size,
771 int macro, const char *indent)
775 fprintf (table, "{ { ");
777 for (i = 0; i < size - 1; i++)
779 fprintf (table, "%d, ", types[i].value);
780 if (((i + 1) % 20) == 0)
782 /* We need \\ for macro. */
784 fprintf (table, "\\\n%s", indent);
786 fprintf (table, "\n%s", indent);
790 fprintf (table, "%d } }", types[i].value);
794 process_i386_operand_type (FILE *table, char *op, int macro,
795 const char *indent, int lineno)
797 char *str, *next, *last;
798 bitfield types [ARRAY_SIZE (operand_types)];
800 /* Copy the default operand type. */
801 memcpy (types, operand_types, sizeof (types));
803 if (strcmp (op, "0"))
805 last = op + strlen (op);
806 for (next = op; next && next < last; )
808 str = next_field (next, '|', &next, last);
810 set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno);
813 output_operand_type (table, types, ARRAY_SIZE (types), macro,
818 output_i386_opcode (FILE *table, const char *name, char *str,
819 char *last, int lineno)
822 char *operands, *base_opcode, *extension_opcode, *opcode_length;
823 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
825 /* Find number of operands. */
826 operands = next_field (str, ',', &str, last);
828 /* Find base_opcode. */
829 base_opcode = next_field (str, ',', &str, last);
831 /* Find extension_opcode. */
832 extension_opcode = next_field (str, ',', &str, last);
834 /* Find opcode_length. */
835 opcode_length = next_field (str, ',', &str, last);
837 /* Find cpu_flags. */
838 cpu_flags = next_field (str, ',', &str, last);
840 /* Find opcode_modifier. */
841 opcode_modifier = next_field (str, ',', &str, last);
843 /* Remove the first {. */
844 str = remove_leading_whitespaces (str);
847 str = remove_leading_whitespaces (str + 1);
851 /* There are at least "X}". */
855 /* Remove trailing white spaces and }. */
859 if (ISSPACE (str[i]) || str[i] == '}')
868 /* Find operand_types. */
869 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
873 operand_types [i] = NULL;
877 operand_types [i] = next_field (str, ',', &str, last);
878 if (*operand_types[i] == '0')
881 operand_types[i] = NULL;
886 fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
887 name, operands, base_opcode, extension_opcode,
890 process_i386_cpu_flag (table, cpu_flags, 0, ",", " ", lineno);
892 process_i386_opcode_modifier (table, opcode_modifier, lineno);
894 fprintf (table, " { ");
896 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
898 if (operand_types[i] == NULL || *operand_types[i] == '0')
901 process_i386_operand_type (table, "0", 0, "\t ", lineno);
906 fprintf (table, ",\n ");
908 process_i386_operand_type (table, operand_types[i], 0,
911 fprintf (table, " } },\n");
914 struct opcode_hash_entry
916 struct opcode_hash_entry *next;
922 /* Calculate the hash value of an opcode hash entry P. */
925 opcode_hash_hash (const void *p)
927 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
928 return htab_hash_string (entry->name);
931 /* Compare a string Q against an opcode hash entry P. */
934 opcode_hash_eq (const void *p, const void *q)
936 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
937 const char *name = (const char *) q;
938 return strcmp (name, entry->name) == 0;
942 process_i386_opcodes (FILE *table)
947 char *str, *p, *last, *name;
948 struct opcode_hash_entry **hash_slot, **entry, *next;
949 htab_t opcode_hash_table;
950 struct opcode_hash_entry **opcode_array;
951 unsigned int opcode_array_size = 1024;
954 filename = "i386-opc.tbl";
955 fp = fopen (filename, "r");
958 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
962 opcode_array = (struct opcode_hash_entry **)
963 xmalloc (sizeof (*opcode_array) * opcode_array_size);
965 opcode_hash_table = htab_create_alloc (16, opcode_hash_hash,
966 opcode_hash_eq, NULL,
969 fprintf (table, "\n/* i386 opcode table. */\n\n");
970 fprintf (table, "const insn_template i386_optab[] =\n{\n");
972 /* Put everything on opcode array. */
975 if (fgets (buf, sizeof (buf), fp) == NULL)
980 p = remove_leading_whitespaces (buf);
983 str = strstr (p, "//");
987 /* Remove trailing white spaces. */
988 remove_trailing_whitespaces (p);
993 /* Ignore comments. */
1001 last = p + strlen (p);
1004 name = next_field (p, ',', &str, last);
1006 /* Get the slot in hash table. */
1007 hash_slot = (struct opcode_hash_entry **)
1008 htab_find_slot_with_hash (opcode_hash_table, name,
1009 htab_hash_string (name),
1012 if (*hash_slot == NULL)
1014 /* It is the new one. Put it on opcode array. */
1015 if (i >= opcode_array_size)
1017 /* Grow the opcode array when needed. */
1018 opcode_array_size += 1024;
1019 opcode_array = (struct opcode_hash_entry **)
1020 xrealloc (opcode_array,
1021 sizeof (*opcode_array) * opcode_array_size);
1024 opcode_array[i] = (struct opcode_hash_entry *)
1025 xmalloc (sizeof (struct opcode_hash_entry));
1026 opcode_array[i]->next = NULL;
1027 opcode_array[i]->name = xstrdup (name);
1028 opcode_array[i]->opcode = xstrdup (str);
1029 opcode_array[i]->lineno = lineno;
1030 *hash_slot = opcode_array[i];
1035 /* Append it to the existing one. */
1037 while ((*entry) != NULL)
1038 entry = &(*entry)->next;
1039 *entry = (struct opcode_hash_entry *)
1040 xmalloc (sizeof (struct opcode_hash_entry));
1041 (*entry)->next = NULL;
1042 (*entry)->name = (*hash_slot)->name;
1043 (*entry)->opcode = xstrdup (str);
1044 (*entry)->lineno = lineno;
1048 /* Process opcode array. */
1049 for (j = 0; j < i; j++)
1051 for (next = opcode_array[j]; next; next = next->next)
1055 lineno = next->lineno;
1056 last = str + strlen (str);
1057 output_i386_opcode (table, name, str, last, lineno);
1063 fprintf (table, " { NULL, 0, 0, 0, 0,\n");
1065 process_i386_cpu_flag (table, "0", 0, ",", " ", -1);
1067 process_i386_opcode_modifier (table, "0", -1);
1069 fprintf (table, " { ");
1070 process_i386_operand_type (table, "0", 0, "\t ", -1);
1071 fprintf (table, " } }\n");
1073 fprintf (table, "};\n");
1077 process_i386_registers (FILE *table)
1081 char *str, *p, *last;
1082 char *reg_name, *reg_type, *reg_flags, *reg_num;
1083 char *dw2_32_num, *dw2_64_num;
1086 filename = "i386-reg.tbl";
1087 fp = fopen (filename, "r");
1089 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
1092 fprintf (table, "\n/* i386 register table. */\n\n");
1093 fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
1097 if (fgets (buf, sizeof (buf), fp) == NULL)
1102 p = remove_leading_whitespaces (buf);
1104 /* Skip comments. */
1105 str = strstr (p, "//");
1109 /* Remove trailing white spaces. */
1110 remove_trailing_whitespaces (p);
1115 fprintf (table, "%s\n", p);
1123 last = p + strlen (p);
1125 /* Find reg_name. */
1126 reg_name = next_field (p, ',', &str, last);
1128 /* Find reg_type. */
1129 reg_type = next_field (str, ',', &str, last);
1131 /* Find reg_flags. */
1132 reg_flags = next_field (str, ',', &str, last);
1135 reg_num = next_field (str, ',', &str, last);
1137 fprintf (table, " { \"%s\",\n ", reg_name);
1139 process_i386_operand_type (table, reg_type, 0, "\t", lineno);
1141 /* Find 32-bit Dwarf2 register number. */
1142 dw2_32_num = next_field (str, ',', &str, last);
1144 /* Find 64-bit Dwarf2 register number. */
1145 dw2_64_num = next_field (str, ',', &str, last);
1147 fprintf (table, ",\n %s, %s, { %s, %s } },\n",
1148 reg_flags, reg_num, dw2_32_num, dw2_64_num);
1153 fprintf (table, "};\n");
1155 fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
1159 process_i386_initializers (void)
1162 FILE *fp = fopen ("i386-init.h", "w");
1166 fail (_("can't create i386-init.h, errno = %s\n"),
1169 process_copyright (fp);
1171 for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
1173 fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
1174 init = xstrdup (cpu_flag_init[i].init);
1175 process_i386_cpu_flag (fp, init, 1, "", " ", -1);
1179 for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
1181 fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
1182 init = xstrdup (operand_type_init[i].init);
1183 process_i386_operand_type (fp, init, 1, " ", -1);
1191 /* Program options. */
1192 #define OPTION_SRCDIR 200
1194 struct option long_options[] =
1196 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
1197 {"debug", no_argument, NULL, 'd'},
1198 {"version", no_argument, NULL, 'V'},
1199 {"help", no_argument, NULL, 'h'},
1200 {0, no_argument, NULL, 0}
1204 print_version (void)
1206 printf ("%s: version 1.0\n", program_name);
1211 usage (FILE * stream, int status)
1213 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
1219 main (int argc, char **argv)
1221 extern int chdir (char *);
1222 char *srcdir = NULL;
1226 program_name = *argv;
1227 xmalloc_set_program_name (program_name);
1229 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
1254 if (chdir (srcdir) != 0)
1255 fail (_("unable to change directory to \"%s\", errno = %s\n"),
1256 srcdir, xstrerror (errno));
1258 /* Check the unused bitfield in i386_cpu_flags. */
1260 c = CpuNumOfBits - CpuMax - 1;
1262 fail (_("%d unused bits in i386_cpu_flags.\n"), c);
1265 /* Check the unused bitfield in i386_operand_type. */
1267 c = OTNumOfBits - OTMax - 1;
1269 fail (_("%d unused bits in i386_operand_type.\n"), c);
1272 qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
1275 qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
1276 sizeof (opcode_modifiers [0]), compare);
1278 qsort (operand_types, ARRAY_SIZE (operand_types),
1279 sizeof (operand_types [0]), compare);
1281 table = fopen ("i386-tbl.h", "w");
1283 fail (_("can't create i386-tbl.h, errno = %s\n"),
1286 process_copyright (table);
1288 process_i386_opcodes (table);
1289 process_i386_registers (table);
1290 process_i386_initializers ();