gas/
[binutils.git] / opcodes / i386-gen.c
bloba54d633c5196a6c2785afdf5393d403932d619c2
1 /* Copyright 2007, 2008, 2009
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)
9 any later version.
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. */
21 #include "sysdep.h"
22 #include <stdio.h>
23 #include <errno.h>
24 #include "getopt.h"
25 #include "libiberty.h"
26 #include "hashtab.h"
27 #include "safe-ctype.h"
29 #include "i386-opc.h"
31 #include <libintl.h>
32 #define _(String) gettext (String)
34 static const char *program_name = NULL;
35 static int debug = 0;
37 typedef struct initializer
39 const char *name;
40 const char *init;
41 } initializer;
43 static initializer cpu_flag_init[] =
45 { "CPU_UNKNOWN_FLAGS",
46 "unknown" },
47 { "CPU_GENERIC32_FLAGS",
48 "Cpu186|Cpu286|Cpu386" },
49 { "CPU_GENERIC64_FLAGS",
50 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuMMX|CpuSSE|CpuSSE2|CpuLM" },
51 { "CPU_NONE_FLAGS",
52 "0" },
53 { "CPU_I186_FLAGS",
54 "Cpu186" },
55 { "CPU_I286_FLAGS",
56 "Cpu186|Cpu286" },
57 { "CPU_I386_FLAGS",
58 "Cpu186|Cpu286|Cpu386" },
59 { "CPU_I486_FLAGS",
60 "Cpu186|Cpu286|Cpu386|Cpu486" },
61 { "CPU_I586_FLAGS",
62 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu387" },
63 { "CPU_I686_FLAGS",
64 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687" },
65 { "CPU_P2_FLAGS",
66 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuMMX" },
67 { "CPU_P3_FLAGS",
68 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuMMX|CpuSSE" },
69 { "CPU_P4_FLAGS",
70 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuMMX|CpuSSE|CpuSSE2" },
71 { "CPU_NOCONA_FLAGS",
72 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuLM" },
73 { "CPU_CORE_FLAGS",
74 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
75 { "CPU_CORE2_FLAGS",
76 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuLM" },
77 { "CPU_COREI7_FLAGS",
78 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuRdtscp|CpuLM" },
79 { "CPU_K6_FLAGS",
80 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX" },
81 { "CPU_K6_2_FLAGS",
82 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX|Cpu3dnow" },
83 { "CPU_ATHLON_FLAGS",
84 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|Cpu387|Cpu687|CpuMMX|Cpu3dnow|Cpu3dnowA" },
85 { "CPU_K8_FLAGS",
86 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuLM" },
87 { "CPU_AMDFAM10_FLAGS",
88 "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM" },
89 { "CPU_8087_FLAGS",
90 "Cpu8087" },
91 { "CPU_287_FLAGS",
92 "Cpu287" },
93 { "CPU_387_FLAGS",
94 "Cpu387" },
95 { "CPU_ANY87_FLAGS",
96 "Cpu8087|Cpu287|Cpu387|Cpu687|CpuFISTTP" },
97 { "CPU_CLFLUSH_FLAGS",
98 "CpuClflush" },
99 { "CPU_SYSCALL_FLAGS",
100 "CpuSYSCALL" },
101 { "CPU_MMX_FLAGS",
102 "CpuMMX" },
103 { "CPU_SSE_FLAGS",
104 "CpuMMX|CpuSSE" },
105 { "CPU_SSE2_FLAGS",
106 "CpuMMX|CpuSSE|CpuSSE2" },
107 { "CPU_SSE3_FLAGS",
108 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
109 { "CPU_SSSE3_FLAGS",
110 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3" },
111 { "CPU_SSE4_1_FLAGS",
112 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1" },
113 { "CPU_SSE4_2_FLAGS",
114 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2" },
115 { "CPU_ANY_SSE_FLAGS",
116 "CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuAVX" },
117 { "CPU_VMX_FLAGS",
118 "CpuVMX" },
119 { "CPU_SMX_FLAGS",
120 "CpuSMX" },
121 { "CPU_XSAVE_FLAGS",
122 "CpuXsave" },
123 { "CPU_AES_FLAGS",
124 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAES" },
125 { "CPU_PCLMUL_FLAGS",
126 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuPCLMUL" },
127 { "CPU_FMA_FLAGS",
128 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA" },
129 { "CPU_FMA4_FLAGS",
130 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA4" },
131 { "CPU_MOVBE_FLAGS",
132 "CpuMovbe" },
133 { "CPU_RDTSCP_FLAGS",
134 "CpuRdtscp" },
135 { "CPU_EPT_FLAGS",
136 "CpuEPT" },
137 { "CPU_3DNOW_FLAGS",
138 "CpuMMX|Cpu3dnow" },
139 { "CPU_3DNOWA_FLAGS",
140 "CpuMMX|Cpu3dnow|Cpu3dnowA" },
141 { "CPU_PADLOCK_FLAGS",
142 "CpuPadLock" },
143 { "CPU_SVME_FLAGS",
144 "CpuSVME" },
145 { "CPU_SSE4A_FLAGS",
146 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a" },
147 { "CPU_ABM_FLAGS",
148 "CpuABM" },
149 { "CPU_AVX_FLAGS",
150 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX" },
151 { "CPU_ANY_AVX_FLAGS",
152 "CpuAVX" },
155 static initializer operand_type_init[] =
157 { "OPERAND_TYPE_NONE",
158 "0" },
159 { "OPERAND_TYPE_REG8",
160 "Reg8" },
161 { "OPERAND_TYPE_REG16",
162 "Reg16" },
163 { "OPERAND_TYPE_REG32",
164 "Reg32" },
165 { "OPERAND_TYPE_REG64",
166 "Reg64" },
167 { "OPERAND_TYPE_IMM1",
168 "Imm1" },
169 { "OPERAND_TYPE_IMM8",
170 "Imm8" },
171 { "OPERAND_TYPE_IMM8S",
172 "Imm8S" },
173 { "OPERAND_TYPE_IMM16",
174 "Imm16" },
175 { "OPERAND_TYPE_IMM32",
176 "Imm32" },
177 { "OPERAND_TYPE_IMM32S",
178 "Imm32S" },
179 { "OPERAND_TYPE_IMM64",
180 "Imm64" },
181 { "OPERAND_TYPE_BASEINDEX",
182 "BaseIndex" },
183 { "OPERAND_TYPE_DISP8",
184 "Disp8" },
185 { "OPERAND_TYPE_DISP16",
186 "Disp16" },
187 { "OPERAND_TYPE_DISP32",
188 "Disp32" },
189 { "OPERAND_TYPE_DISP32S",
190 "Disp32S" },
191 { "OPERAND_TYPE_DISP64",
192 "Disp64" },
193 { "OPERAND_TYPE_INOUTPORTREG",
194 "InOutPortReg" },
195 { "OPERAND_TYPE_SHIFTCOUNT",
196 "ShiftCount" },
197 { "OPERAND_TYPE_CONTROL",
198 "Control" },
199 { "OPERAND_TYPE_TEST",
200 "Test" },
201 { "OPERAND_TYPE_DEBUG",
202 "FloatReg" },
203 { "OPERAND_TYPE_FLOATREG",
204 "FloatReg" },
205 { "OPERAND_TYPE_FLOATACC",
206 "FloatAcc" },
207 { "OPERAND_TYPE_SREG2",
208 "SReg2" },
209 { "OPERAND_TYPE_SREG3",
210 "SReg3" },
211 { "OPERAND_TYPE_ACC",
212 "Acc" },
213 { "OPERAND_TYPE_JUMPABSOLUTE",
214 "JumpAbsolute" },
215 { "OPERAND_TYPE_REGMMX",
216 "RegMMX" },
217 { "OPERAND_TYPE_REGXMM",
218 "RegXMM" },
219 { "OPERAND_TYPE_REGYMM",
220 "RegYMM" },
221 { "OPERAND_TYPE_ESSEG",
222 "EsSeg" },
223 { "OPERAND_TYPE_ACC32",
224 "Reg32|Acc|Dword" },
225 { "OPERAND_TYPE_ACC64",
226 "Reg64|Acc|Qword" },
227 { "OPERAND_TYPE_INOUTPORTREG",
228 "InOutPortReg" },
229 { "OPERAND_TYPE_REG16_INOUTPORTREG",
230 "Reg16|InOutPortReg" },
231 { "OPERAND_TYPE_DISP16_32",
232 "Disp16|Disp32" },
233 { "OPERAND_TYPE_ANYDISP",
234 "Disp8|Disp16|Disp32|Disp32S|Disp64" },
235 { "OPERAND_TYPE_IMM16_32",
236 "Imm16|Imm32" },
237 { "OPERAND_TYPE_IMM16_32S",
238 "Imm16|Imm32S" },
239 { "OPERAND_TYPE_IMM16_32_32S",
240 "Imm16|Imm32|Imm32S" },
241 { "OPERAND_TYPE_IMM32_32S_DISP32",
242 "Imm32|Imm32S|Disp32" },
243 { "OPERAND_TYPE_IMM64_DISP64",
244 "Imm64|Disp64" },
245 { "OPERAND_TYPE_IMM32_32S_64_DISP32",
246 "Imm32|Imm32S|Imm64|Disp32" },
247 { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
248 "Imm32|Imm32S|Imm64|Disp32|Disp64" },
251 typedef struct bitfield
253 int position;
254 int value;
255 const char *name;
256 } bitfield;
258 #define BITFIELD(n) { n, 0, #n }
260 static bitfield cpu_flags[] =
262 BITFIELD (Cpu186),
263 BITFIELD (Cpu286),
264 BITFIELD (Cpu386),
265 BITFIELD (Cpu486),
266 BITFIELD (Cpu586),
267 BITFIELD (Cpu686),
268 BITFIELD (CpuClflush),
269 BITFIELD (CpuSYSCALL),
270 BITFIELD (Cpu8087),
271 BITFIELD (Cpu287),
272 BITFIELD (Cpu387),
273 BITFIELD (Cpu687),
274 BITFIELD (CpuFISTTP),
275 BITFIELD (CpuMMX),
276 BITFIELD (CpuSSE),
277 BITFIELD (CpuSSE2),
278 BITFIELD (CpuSSE3),
279 BITFIELD (CpuSSSE3),
280 BITFIELD (CpuSSE4_1),
281 BITFIELD (CpuSSE4_2),
282 BITFIELD (CpuAVX),
283 BITFIELD (CpuSSE4a),
284 BITFIELD (Cpu3dnow),
285 BITFIELD (Cpu3dnowA),
286 BITFIELD (CpuPadLock),
287 BITFIELD (CpuSVME),
288 BITFIELD (CpuVMX),
289 BITFIELD (CpuSMX),
290 BITFIELD (CpuABM),
291 BITFIELD (CpuXsave),
292 BITFIELD (CpuAES),
293 BITFIELD (CpuPCLMUL),
294 BITFIELD (CpuFMA),
295 BITFIELD (CpuFMA4),
296 BITFIELD (CpuLM),
297 BITFIELD (CpuMovbe),
298 BITFIELD (CpuEPT),
299 BITFIELD (CpuRdtscp),
300 BITFIELD (Cpu64),
301 BITFIELD (CpuNo64),
302 #ifdef CpuUnused
303 BITFIELD (CpuUnused),
304 #endif
307 static bitfield opcode_modifiers[] =
309 BITFIELD (D),
310 BITFIELD (W),
311 BITFIELD (S),
312 BITFIELD (Modrm),
313 BITFIELD (ShortForm),
314 BITFIELD (Jump),
315 BITFIELD (JumpDword),
316 BITFIELD (JumpByte),
317 BITFIELD (JumpInterSegment),
318 BITFIELD (FloatMF),
319 BITFIELD (FloatR),
320 BITFIELD (FloatD),
321 BITFIELD (Size16),
322 BITFIELD (Size32),
323 BITFIELD (Size64),
324 BITFIELD (IgnoreSize),
325 BITFIELD (DefaultSize),
326 BITFIELD (No_bSuf),
327 BITFIELD (No_wSuf),
328 BITFIELD (No_lSuf),
329 BITFIELD (No_sSuf),
330 BITFIELD (No_qSuf),
331 BITFIELD (No_ldSuf),
332 BITFIELD (FWait),
333 BITFIELD (IsString),
334 BITFIELD (RegKludge),
335 BITFIELD (FirstXmm0),
336 BITFIELD (Implicit1stXmm0),
337 BITFIELD (ByteOkIntel),
338 BITFIELD (ToDword),
339 BITFIELD (ToQword),
340 BITFIELD (AddrPrefixOp0),
341 BITFIELD (IsPrefix),
342 BITFIELD (ImmExt),
343 BITFIELD (NoRex64),
344 BITFIELD (Rex64),
345 BITFIELD (Ugh),
346 BITFIELD (Vex),
347 BITFIELD (Vex256),
348 BITFIELD (VexNDS),
349 BITFIELD (VexNDD),
350 BITFIELD (VexW0),
351 BITFIELD (VexW1),
352 BITFIELD (Vex0F),
353 BITFIELD (Vex0F38),
354 BITFIELD (Vex0F3A),
355 BITFIELD (Vex3Sources),
356 BITFIELD (VexImmExt),
357 BITFIELD (SSE2AVX),
358 BITFIELD (NoAVX),
359 BITFIELD (OldGcc),
360 BITFIELD (ATTMnemonic),
361 BITFIELD (ATTSyntax),
362 BITFIELD (IntelSyntax),
365 static bitfield operand_types[] =
367 BITFIELD (Reg8),
368 BITFIELD (Reg16),
369 BITFIELD (Reg32),
370 BITFIELD (Reg64),
371 BITFIELD (FloatReg),
372 BITFIELD (RegMMX),
373 BITFIELD (RegXMM),
374 BITFIELD (RegYMM),
375 BITFIELD (Imm8),
376 BITFIELD (Imm8S),
377 BITFIELD (Imm16),
378 BITFIELD (Imm32),
379 BITFIELD (Imm32S),
380 BITFIELD (Imm64),
381 BITFIELD (Imm1),
382 BITFIELD (BaseIndex),
383 BITFIELD (Disp8),
384 BITFIELD (Disp16),
385 BITFIELD (Disp32),
386 BITFIELD (Disp32S),
387 BITFIELD (Disp64),
388 BITFIELD (InOutPortReg),
389 BITFIELD (ShiftCount),
390 BITFIELD (Control),
391 BITFIELD (Debug),
392 BITFIELD (Test),
393 BITFIELD (SReg2),
394 BITFIELD (SReg3),
395 BITFIELD (Acc),
396 BITFIELD (FloatAcc),
397 BITFIELD (JumpAbsolute),
398 BITFIELD (EsSeg),
399 BITFIELD (RegMem),
400 BITFIELD (Mem),
401 BITFIELD (Byte),
402 BITFIELD (Word),
403 BITFIELD (Dword),
404 BITFIELD (Fword),
405 BITFIELD (Qword),
406 BITFIELD (Tbyte),
407 BITFIELD (Xmmword),
408 BITFIELD (Ymmword),
409 BITFIELD (Unspecified),
410 BITFIELD (Anysize),
411 #ifdef OTUnused
412 BITFIELD (OTUnused),
413 #endif
416 static const char *filename;
418 static int
419 compare (const void *x, const void *y)
421 const bitfield *xp = (const bitfield *) x;
422 const bitfield *yp = (const bitfield *) y;
423 return xp->position - yp->position;
426 static void
427 fail (const char *message, ...)
429 va_list args;
431 va_start (args, message);
432 fprintf (stderr, _("%s: Error: "), program_name);
433 vfprintf (stderr, message, args);
434 va_end (args);
435 xexit (1);
438 static void
439 process_copyright (FILE *fp)
441 fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
442 /* Copyright 2007, 2008, 2009\n\
443 Free Software Foundation, Inc.\n\
445 This file is part of the GNU opcodes library.\n\
447 This library is free software; you can redistribute it and/or modify\n\
448 it under the terms of the GNU General Public License as published by\n\
449 the Free Software Foundation; either version 3, or (at your option)\n\
450 any later version.\n\
452 It is distributed in the hope that it will be useful, but WITHOUT\n\
453 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
454 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
455 License for more details.\n\
457 You should have received a copy of the GNU General Public License\n\
458 along with this program; if not, write to the Free Software\n\
459 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
460 MA 02110-1301, USA. */\n");
463 /* Remove leading white spaces. */
465 static char *
466 remove_leading_whitespaces (char *str)
468 while (ISSPACE (*str))
469 str++;
470 return str;
473 /* Remove trailing white spaces. */
475 static void
476 remove_trailing_whitespaces (char *str)
478 size_t last = strlen (str);
480 if (last == 0)
481 return;
485 last--;
486 if (ISSPACE (str [last]))
487 str[last] = '\0';
488 else
489 break;
491 while (last != 0);
494 /* Find next field separated by SEP and terminate it. Return a
495 pointer to the one after it. */
497 static char *
498 next_field (char *str, char sep, char **next, char *last)
500 char *p;
502 p = remove_leading_whitespaces (str);
503 for (str = p; *str != sep && *str != '\0'; str++);
505 *str = '\0';
506 remove_trailing_whitespaces (p);
508 *next = str + 1;
510 if (p >= last)
511 abort ();
513 return p;
516 static void
517 set_bitfield (const char *f, bitfield *array, unsigned int size, int lineno)
519 unsigned int i;
521 if (strcmp (f, "CpuFP") == 0)
523 set_bitfield("Cpu387", array, size, lineno);
524 set_bitfield("Cpu287", array, size, lineno);
525 f = "Cpu8087";
527 else if (strcmp (f, "Mmword") == 0)
528 f= "Qword";
529 else if (strcmp (f, "Oword") == 0)
530 f= "Xmmword";
532 for (i = 0; i < size; i++)
533 if (strcasecmp (array[i].name, f) == 0)
535 array[i].value = 1;
536 return;
539 if (lineno != -1)
540 fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f);
541 else
542 fail (_("Unknown bitfield: %s\n"), f);
545 static void
546 output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
547 int macro, const char *comma, const char *indent)
549 unsigned int i;
551 fprintf (table, "%s{ { ", indent);
553 for (i = 0; i < size - 1; i++)
555 fprintf (table, "%d, ", flags[i].value);
556 if (((i + 1) % 20) == 0)
558 /* We need \\ for macro. */
559 if (macro)
560 fprintf (table, " \\\n %s", indent);
561 else
562 fprintf (table, "\n %s", indent);
566 fprintf (table, "%d } }%s\n", flags[i].value, comma);
569 static void
570 process_i386_cpu_flag (FILE *table, char *flag, int macro,
571 const char *comma, const char *indent,
572 int lineno)
574 char *str, *next, *last;
575 bitfield flags [ARRAY_SIZE (cpu_flags)];
577 /* Copy the default cpu flags. */
578 memcpy (flags, cpu_flags, sizeof (cpu_flags));
580 if (strcasecmp (flag, "unknown") == 0)
582 unsigned int i;
584 /* We turn on everything except for cpu64 in case of
585 CPU_UNKNOWN_FLAGS. */
586 for (i = 0; i < ARRAY_SIZE (flags); i++)
587 if (flags[i].position != Cpu64)
588 flags[i].value = 1;
590 else if (strcmp (flag, "0"))
592 last = flag + strlen (flag);
593 for (next = flag; next && next < last; )
595 str = next_field (next, '|', &next, last);
596 if (str)
597 set_bitfield (str, flags, ARRAY_SIZE (flags), lineno);
601 output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
602 comma, indent);
605 static void
606 output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
608 unsigned int i;
610 fprintf (table, " { ");
612 for (i = 0; i < size - 1; i++)
614 fprintf (table, "%d, ", modifier[i].value);
615 if (((i + 1) % 20) == 0)
616 fprintf (table, "\n ");
619 fprintf (table, "%d },\n", modifier[i].value);
622 static void
623 process_i386_opcode_modifier (FILE *table, char *mod, int lineno)
625 char *str, *next, *last;
626 bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
628 /* Copy the default opcode modifier. */
629 memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
631 if (strcmp (mod, "0"))
633 last = mod + strlen (mod);
634 for (next = mod; next && next < last; )
636 str = next_field (next, '|', &next, last);
637 if (str)
638 set_bitfield (str, modifiers, ARRAY_SIZE (modifiers), lineno);
641 output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
644 static void
645 output_operand_type (FILE *table, bitfield *types, unsigned int size,
646 int macro, const char *indent)
648 unsigned int i;
650 fprintf (table, "{ { ");
652 for (i = 0; i < size - 1; i++)
654 fprintf (table, "%d, ", types[i].value);
655 if (((i + 1) % 20) == 0)
657 /* We need \\ for macro. */
658 if (macro)
659 fprintf (table, "\\\n%s", indent);
660 else
661 fprintf (table, "\n%s", indent);
665 fprintf (table, "%d } }", types[i].value);
668 static void
669 process_i386_operand_type (FILE *table, char *op, int macro,
670 const char *indent, int lineno)
672 char *str, *next, *last;
673 bitfield types [ARRAY_SIZE (operand_types)];
675 /* Copy the default operand type. */
676 memcpy (types, operand_types, sizeof (types));
678 if (strcmp (op, "0"))
680 last = op + strlen (op);
681 for (next = op; next && next < last; )
683 str = next_field (next, '|', &next, last);
684 if (str)
685 set_bitfield (str, types, ARRAY_SIZE (types), lineno);
688 output_operand_type (table, types, ARRAY_SIZE (types), macro,
689 indent);
692 static void
693 output_i386_opcode (FILE *table, const char *name, char *str,
694 char *last, int lineno)
696 unsigned int i;
697 char *operands, *base_opcode, *extension_opcode, *opcode_length;
698 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
700 /* Find number of operands. */
701 operands = next_field (str, ',', &str, last);
703 /* Find base_opcode. */
704 base_opcode = next_field (str, ',', &str, last);
706 /* Find extension_opcode. */
707 extension_opcode = next_field (str, ',', &str, last);
709 /* Find opcode_length. */
710 opcode_length = next_field (str, ',', &str, last);
712 /* Find cpu_flags. */
713 cpu_flags = next_field (str, ',', &str, last);
715 /* Find opcode_modifier. */
716 opcode_modifier = next_field (str, ',', &str, last);
718 /* Remove the first {. */
719 str = remove_leading_whitespaces (str);
720 if (*str != '{')
721 abort ();
722 str = remove_leading_whitespaces (str + 1);
724 i = strlen (str);
726 /* There are at least "X}". */
727 if (i < 2)
728 abort ();
730 /* Remove trailing white spaces and }. */
733 i--;
734 if (ISSPACE (str[i]) || str[i] == '}')
735 str[i] = '\0';
736 else
737 break;
739 while (i != 0);
741 last = str + i;
743 /* Find operand_types. */
744 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
746 if (str >= last)
748 operand_types [i] = NULL;
749 break;
752 operand_types [i] = next_field (str, ',', &str, last);
753 if (*operand_types[i] == '0')
755 if (i != 0)
756 operand_types[i] = NULL;
757 break;
761 fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
762 name, operands, base_opcode, extension_opcode,
763 opcode_length);
765 process_i386_cpu_flag (table, cpu_flags, 0, ",", " ", lineno);
767 process_i386_opcode_modifier (table, opcode_modifier, lineno);
769 fprintf (table, " { ");
771 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
773 if (operand_types[i] == NULL || *operand_types[i] == '0')
775 if (i == 0)
776 process_i386_operand_type (table, "0", 0, "\t ", lineno);
777 break;
780 if (i != 0)
781 fprintf (table, ",\n ");
783 process_i386_operand_type (table, operand_types[i], 0,
784 "\t ", lineno);
786 fprintf (table, " } },\n");
789 struct opcode_hash_entry
791 struct opcode_hash_entry *next;
792 char *name;
793 char *opcode;
794 int lineno;
797 /* Calculate the hash value of an opcode hash entry P. */
799 static hashval_t
800 opcode_hash_hash (const void *p)
802 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
803 return htab_hash_string (entry->name);
806 /* Compare a string Q against an opcode hash entry P. */
808 static int
809 opcode_hash_eq (const void *p, const void *q)
811 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
812 const char *name = (const char *) q;
813 return strcmp (name, entry->name) == 0;
816 static void
817 process_i386_opcodes (FILE *table)
819 FILE *fp;
820 char buf[2048];
821 unsigned int i, j;
822 char *str, *p, *last, *name;
823 struct opcode_hash_entry **hash_slot, **entry, *next;
824 htab_t opcode_hash_table;
825 struct opcode_hash_entry **opcode_array;
826 unsigned int opcode_array_size = 1024;
827 int lineno = 0;
829 filename = "i386-opc.tbl";
830 fp = fopen (filename, "r");
832 if (fp == NULL)
833 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
834 xstrerror (errno));
836 i = 0;
837 opcode_array = (struct opcode_hash_entry **)
838 xmalloc (sizeof (*opcode_array) * opcode_array_size);
840 opcode_hash_table = htab_create_alloc (16, opcode_hash_hash,
841 opcode_hash_eq, NULL,
842 xcalloc, free);
844 fprintf (table, "\n/* i386 opcode table. */\n\n");
845 fprintf (table, "const template i386_optab[] =\n{\n");
847 /* Put everything on opcode array. */
848 while (!feof (fp))
850 if (fgets (buf, sizeof (buf), fp) == NULL)
851 break;
853 lineno++;
855 p = remove_leading_whitespaces (buf);
857 /* Skip comments. */
858 str = strstr (p, "//");
859 if (str != NULL)
860 str[0] = '\0';
862 /* Remove trailing white spaces. */
863 remove_trailing_whitespaces (p);
865 switch (p[0])
867 case '#':
868 /* Ignore comments. */
869 case '\0':
870 continue;
871 break;
872 default:
873 break;
876 last = p + strlen (p);
878 /* Find name. */
879 name = next_field (p, ',', &str, last);
881 /* Get the slot in hash table. */
882 hash_slot = (struct opcode_hash_entry **)
883 htab_find_slot_with_hash (opcode_hash_table, name,
884 htab_hash_string (name),
885 INSERT);
887 if (*hash_slot == NULL)
889 /* It is the new one. Put it on opcode array. */
890 if (i >= opcode_array_size)
892 /* Grow the opcode array when needed. */
893 opcode_array_size += 1024;
894 opcode_array = (struct opcode_hash_entry **)
895 xrealloc (opcode_array,
896 sizeof (*opcode_array) * opcode_array_size);
899 opcode_array[i] = (struct opcode_hash_entry *)
900 xmalloc (sizeof (struct opcode_hash_entry));
901 opcode_array[i]->next = NULL;
902 opcode_array[i]->name = xstrdup (name);
903 opcode_array[i]->opcode = xstrdup (str);
904 opcode_array[i]->lineno = lineno;
905 *hash_slot = opcode_array[i];
906 i++;
908 else
910 /* Append it to the existing one. */
911 entry = hash_slot;
912 while ((*entry) != NULL)
913 entry = &(*entry)->next;
914 *entry = (struct opcode_hash_entry *)
915 xmalloc (sizeof (struct opcode_hash_entry));
916 (*entry)->next = NULL;
917 (*entry)->name = (*hash_slot)->name;
918 (*entry)->opcode = xstrdup (str);
919 (*entry)->lineno = lineno;
923 /* Process opcode array. */
924 for (j = 0; j < i; j++)
926 for (next = opcode_array[j]; next; next = next->next)
928 name = next->name;
929 str = next->opcode;
930 lineno = next->lineno;
931 last = str + strlen (str);
932 output_i386_opcode (table, name, str, last, lineno);
936 fclose (fp);
938 fprintf (table, " { NULL, 0, 0, 0, 0,\n");
940 process_i386_cpu_flag (table, "0", 0, ",", " ", -1);
942 process_i386_opcode_modifier (table, "0", -1);
944 fprintf (table, " { ");
945 process_i386_operand_type (table, "0", 0, "\t ", -1);
946 fprintf (table, " } }\n");
948 fprintf (table, "};\n");
951 static void
952 process_i386_registers (FILE *table)
954 FILE *fp;
955 char buf[2048];
956 char *str, *p, *last;
957 char *reg_name, *reg_type, *reg_flags, *reg_num;
958 char *dw2_32_num, *dw2_64_num;
959 int lineno = 0;
961 filename = "i386-reg.tbl";
962 fp = fopen (filename, "r");
963 if (fp == NULL)
964 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
965 xstrerror (errno));
967 fprintf (table, "\n/* i386 register table. */\n\n");
968 fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
970 while (!feof (fp))
972 if (fgets (buf, sizeof (buf), fp) == NULL)
973 break;
975 lineno++;
977 p = remove_leading_whitespaces (buf);
979 /* Skip comments. */
980 str = strstr (p, "//");
981 if (str != NULL)
982 str[0] = '\0';
984 /* Remove trailing white spaces. */
985 remove_trailing_whitespaces (p);
987 switch (p[0])
989 case '#':
990 fprintf (table, "%s\n", p);
991 case '\0':
992 continue;
993 break;
994 default:
995 break;
998 last = p + strlen (p);
1000 /* Find reg_name. */
1001 reg_name = next_field (p, ',', &str, last);
1003 /* Find reg_type. */
1004 reg_type = next_field (str, ',', &str, last);
1006 /* Find reg_flags. */
1007 reg_flags = next_field (str, ',', &str, last);
1009 /* Find reg_num. */
1010 reg_num = next_field (str, ',', &str, last);
1012 fprintf (table, " { \"%s\",\n ", reg_name);
1014 process_i386_operand_type (table, reg_type, 0, "\t", lineno);
1016 /* Find 32-bit Dwarf2 register number. */
1017 dw2_32_num = next_field (str, ',', &str, last);
1019 /* Find 64-bit Dwarf2 register number. */
1020 dw2_64_num = next_field (str, ',', &str, last);
1022 fprintf (table, ",\n %s, %s, { %s, %s } },\n",
1023 reg_flags, reg_num, dw2_32_num, dw2_64_num);
1026 fclose (fp);
1028 fprintf (table, "};\n");
1030 fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
1033 static void
1034 process_i386_initializers (void)
1036 unsigned int i;
1037 FILE *fp = fopen ("i386-init.h", "w");
1038 char *init;
1040 if (fp == NULL)
1041 fail (_("can't create i386-init.h, errno = %s\n"),
1042 xstrerror (errno));
1044 process_copyright (fp);
1046 for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
1048 fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
1049 init = xstrdup (cpu_flag_init[i].init);
1050 process_i386_cpu_flag (fp, init, 1, "", " ", -1);
1051 free (init);
1054 for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
1056 fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
1057 init = xstrdup (operand_type_init[i].init);
1058 process_i386_operand_type (fp, init, 1, " ", -1);
1059 free (init);
1061 fprintf (fp, "\n");
1063 fclose (fp);
1066 /* Program options. */
1067 #define OPTION_SRCDIR 200
1069 struct option long_options[] =
1071 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
1072 {"debug", no_argument, NULL, 'd'},
1073 {"version", no_argument, NULL, 'V'},
1074 {"help", no_argument, NULL, 'h'},
1075 {0, no_argument, NULL, 0}
1078 static void
1079 print_version (void)
1081 printf ("%s: version 1.0\n", program_name);
1082 xexit (0);
1085 static void
1086 usage (FILE * stream, int status)
1088 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
1089 program_name);
1090 xexit (status);
1094 main (int argc, char **argv)
1096 extern int chdir (char *);
1097 char *srcdir = NULL;
1098 int c;
1099 FILE *table;
1101 program_name = *argv;
1102 xmalloc_set_program_name (program_name);
1104 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
1105 switch (c)
1107 case OPTION_SRCDIR:
1108 srcdir = optarg;
1109 break;
1110 case 'V':
1111 case 'v':
1112 print_version ();
1113 break;
1114 case 'd':
1115 debug = 1;
1116 break;
1117 case 'h':
1118 case '?':
1119 usage (stderr, 0);
1120 default:
1121 case 0:
1122 break;
1125 if (optind != argc)
1126 usage (stdout, 1);
1128 if (srcdir != NULL)
1129 if (chdir (srcdir) != 0)
1130 fail (_("unable to change directory to \"%s\", errno = %s\n"),
1131 srcdir, xstrerror (errno));
1133 /* Check the unused bitfield in i386_cpu_flags. */
1134 #ifndef CpuUnused
1135 c = CpuNumOfBits - CpuMax - 1;
1136 if (c)
1137 fail (_("%d unused bits in i386_cpu_flags.\n"), c);
1138 #endif
1140 /* Check the unused bitfield in i386_operand_type. */
1141 #ifndef OTUnused
1142 c = OTNumOfBits - OTMax - 1;
1143 if (c)
1144 fail (_("%d unused bits in i386_operand_type.\n"), c);
1145 #endif
1147 qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
1148 compare);
1150 qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
1151 sizeof (opcode_modifiers [0]), compare);
1153 qsort (operand_types, ARRAY_SIZE (operand_types),
1154 sizeof (operand_types [0]), compare);
1156 table = fopen ("i386-tbl.h", "w");
1157 if (table == NULL)
1158 fail (_("can't create i386-tbl.h, errno = %s\n"),
1159 xstrerror (errno));
1161 process_copyright (table);
1163 process_i386_opcodes (table);
1164 process_i386_registers (table);
1165 process_i386_initializers ();
1167 fclose (table);
1169 exit (0);