2009-11-05 Sebastian Pop <sebastian.pop@amd.com>
[deliverable/binutils-gdb.git] / opcodes / i386-gen.c
1 /* Copyright 2007, 2008, 2009
2 Free Software Foundation, Inc.
3
4 This file is part of the GNU opcodes library.
5
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.
10
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.
15
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. */
20
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"
28
29 #include "i386-opc.h"
30
31 #include <libintl.h>
32 #define _(String) gettext (String)
33
34 static const char *program_name = NULL;
35 static int debug = 0;
36
37 typedef struct initializer
38 {
39 const char *name;
40 const char *init;
41 } initializer;
42
43 static initializer cpu_flag_init[] =
44 {
45 { "CPU_UNKNOWN_FLAGS",
46 "~CpuL1OM" },
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_LWP_FLAGS",
132 "CpuLWP" },
133 { "CPU_MOVBE_FLAGS",
134 "CpuMovbe" },
135 { "CPU_RDTSCP_FLAGS",
136 "CpuRdtscp" },
137 { "CPU_EPT_FLAGS",
138 "CpuEPT" },
139 { "CPU_3DNOW_FLAGS",
140 "CpuMMX|Cpu3dnow" },
141 { "CPU_3DNOWA_FLAGS",
142 "CpuMMX|Cpu3dnow|Cpu3dnowA" },
143 { "CPU_PADLOCK_FLAGS",
144 "CpuPadLock" },
145 { "CPU_SVME_FLAGS",
146 "CpuSVME" },
147 { "CPU_SSE4A_FLAGS",
148 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a" },
149 { "CPU_ABM_FLAGS",
150 "CpuABM" },
151 { "CPU_AVX_FLAGS",
152 "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX" },
153 { "CPU_ANY_AVX_FLAGS",
154 "CpuAVX" },
155 { "CPU_L1OM_FLAGS",
156 "unknown" },
157 };
158
159 static initializer operand_type_init[] =
160 {
161 { "OPERAND_TYPE_NONE",
162 "0" },
163 { "OPERAND_TYPE_REG8",
164 "Reg8" },
165 { "OPERAND_TYPE_REG16",
166 "Reg16" },
167 { "OPERAND_TYPE_REG32",
168 "Reg32" },
169 { "OPERAND_TYPE_REG64",
170 "Reg64" },
171 { "OPERAND_TYPE_IMM1",
172 "Imm1" },
173 { "OPERAND_TYPE_IMM8",
174 "Imm8" },
175 { "OPERAND_TYPE_IMM8S",
176 "Imm8S" },
177 { "OPERAND_TYPE_IMM16",
178 "Imm16" },
179 { "OPERAND_TYPE_IMM32",
180 "Imm32" },
181 { "OPERAND_TYPE_IMM32S",
182 "Imm32S" },
183 { "OPERAND_TYPE_IMM64",
184 "Imm64" },
185 { "OPERAND_TYPE_BASEINDEX",
186 "BaseIndex" },
187 { "OPERAND_TYPE_DISP8",
188 "Disp8" },
189 { "OPERAND_TYPE_DISP16",
190 "Disp16" },
191 { "OPERAND_TYPE_DISP32",
192 "Disp32" },
193 { "OPERAND_TYPE_DISP32S",
194 "Disp32S" },
195 { "OPERAND_TYPE_DISP64",
196 "Disp64" },
197 { "OPERAND_TYPE_INOUTPORTREG",
198 "InOutPortReg" },
199 { "OPERAND_TYPE_SHIFTCOUNT",
200 "ShiftCount" },
201 { "OPERAND_TYPE_CONTROL",
202 "Control" },
203 { "OPERAND_TYPE_TEST",
204 "Test" },
205 { "OPERAND_TYPE_DEBUG",
206 "FloatReg" },
207 { "OPERAND_TYPE_FLOATREG",
208 "FloatReg" },
209 { "OPERAND_TYPE_FLOATACC",
210 "FloatAcc" },
211 { "OPERAND_TYPE_SREG2",
212 "SReg2" },
213 { "OPERAND_TYPE_SREG3",
214 "SReg3" },
215 { "OPERAND_TYPE_ACC",
216 "Acc" },
217 { "OPERAND_TYPE_JUMPABSOLUTE",
218 "JumpAbsolute" },
219 { "OPERAND_TYPE_REGMMX",
220 "RegMMX" },
221 { "OPERAND_TYPE_REGXMM",
222 "RegXMM" },
223 { "OPERAND_TYPE_REGYMM",
224 "RegYMM" },
225 { "OPERAND_TYPE_ESSEG",
226 "EsSeg" },
227 { "OPERAND_TYPE_ACC32",
228 "Reg32|Acc|Dword" },
229 { "OPERAND_TYPE_ACC64",
230 "Reg64|Acc|Qword" },
231 { "OPERAND_TYPE_INOUTPORTREG",
232 "InOutPortReg" },
233 { "OPERAND_TYPE_REG16_INOUTPORTREG",
234 "Reg16|InOutPortReg" },
235 { "OPERAND_TYPE_DISP16_32",
236 "Disp16|Disp32" },
237 { "OPERAND_TYPE_ANYDISP",
238 "Disp8|Disp16|Disp32|Disp32S|Disp64" },
239 { "OPERAND_TYPE_IMM16_32",
240 "Imm16|Imm32" },
241 { "OPERAND_TYPE_IMM16_32S",
242 "Imm16|Imm32S" },
243 { "OPERAND_TYPE_IMM16_32_32S",
244 "Imm16|Imm32|Imm32S" },
245 { "OPERAND_TYPE_IMM32_32S_DISP32",
246 "Imm32|Imm32S|Disp32" },
247 { "OPERAND_TYPE_IMM64_DISP64",
248 "Imm64|Disp64" },
249 { "OPERAND_TYPE_IMM32_32S_64_DISP32",
250 "Imm32|Imm32S|Imm64|Disp32" },
251 { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
252 "Imm32|Imm32S|Imm64|Disp32|Disp64" },
253 };
254
255 typedef struct bitfield
256 {
257 int position;
258 int value;
259 const char *name;
260 } bitfield;
261
262 #define BITFIELD(n) { n, 0, #n }
263
264 static bitfield cpu_flags[] =
265 {
266 BITFIELD (Cpu186),
267 BITFIELD (Cpu286),
268 BITFIELD (Cpu386),
269 BITFIELD (Cpu486),
270 BITFIELD (Cpu586),
271 BITFIELD (Cpu686),
272 BITFIELD (CpuClflush),
273 BITFIELD (CpuSYSCALL),
274 BITFIELD (Cpu8087),
275 BITFIELD (Cpu287),
276 BITFIELD (Cpu387),
277 BITFIELD (Cpu687),
278 BITFIELD (CpuFISTTP),
279 BITFIELD (CpuMMX),
280 BITFIELD (CpuSSE),
281 BITFIELD (CpuSSE2),
282 BITFIELD (CpuSSE3),
283 BITFIELD (CpuSSSE3),
284 BITFIELD (CpuSSE4_1),
285 BITFIELD (CpuSSE4_2),
286 BITFIELD (CpuAVX),
287 BITFIELD (CpuL1OM),
288 BITFIELD (CpuSSE4a),
289 BITFIELD (Cpu3dnow),
290 BITFIELD (Cpu3dnowA),
291 BITFIELD (CpuPadLock),
292 BITFIELD (CpuSVME),
293 BITFIELD (CpuVMX),
294 BITFIELD (CpuSMX),
295 BITFIELD (CpuABM),
296 BITFIELD (CpuXsave),
297 BITFIELD (CpuAES),
298 BITFIELD (CpuPCLMUL),
299 BITFIELD (CpuFMA),
300 BITFIELD (CpuFMA4),
301 BITFIELD (CpuLWP),
302 BITFIELD (CpuLM),
303 BITFIELD (CpuMovbe),
304 BITFIELD (CpuEPT),
305 BITFIELD (CpuRdtscp),
306 BITFIELD (Cpu64),
307 BITFIELD (CpuNo64),
308 #ifdef CpuUnused
309 BITFIELD (CpuUnused),
310 #endif
311 };
312
313 static bitfield opcode_modifiers[] =
314 {
315 BITFIELD (D),
316 BITFIELD (W),
317 BITFIELD (S),
318 BITFIELD (Modrm),
319 BITFIELD (ShortForm),
320 BITFIELD (Jump),
321 BITFIELD (JumpDword),
322 BITFIELD (JumpByte),
323 BITFIELD (JumpInterSegment),
324 BITFIELD (FloatMF),
325 BITFIELD (FloatR),
326 BITFIELD (FloatD),
327 BITFIELD (Size16),
328 BITFIELD (Size32),
329 BITFIELD (Size64),
330 BITFIELD (IgnoreSize),
331 BITFIELD (DefaultSize),
332 BITFIELD (No_bSuf),
333 BITFIELD (No_wSuf),
334 BITFIELD (No_lSuf),
335 BITFIELD (No_sSuf),
336 BITFIELD (No_qSuf),
337 BITFIELD (No_ldSuf),
338 BITFIELD (FWait),
339 BITFIELD (IsString),
340 BITFIELD (RegKludge),
341 BITFIELD (FirstXmm0),
342 BITFIELD (Implicit1stXmm0),
343 BITFIELD (ByteOkIntel),
344 BITFIELD (ToDword),
345 BITFIELD (ToQword),
346 BITFIELD (AddrPrefixOp0),
347 BITFIELD (IsPrefix),
348 BITFIELD (ImmExt),
349 BITFIELD (NoRex64),
350 BITFIELD (Rex64),
351 BITFIELD (Ugh),
352 BITFIELD (Vex),
353 BITFIELD (VexNDS),
354 BITFIELD (VexNDD),
355 BITFIELD (VexLWP),
356 BITFIELD (VexW0),
357 BITFIELD (VexW1),
358 BITFIELD (Vex0F),
359 BITFIELD (Vex0F38),
360 BITFIELD (Vex0F3A),
361 BITFIELD (XOP09),
362 BITFIELD (XOP0A),
363 BITFIELD (Vex3Sources),
364 BITFIELD (VexImmExt),
365 BITFIELD (SSE2AVX),
366 BITFIELD (NoAVX),
367 BITFIELD (OldGcc),
368 BITFIELD (ATTMnemonic),
369 BITFIELD (ATTSyntax),
370 BITFIELD (IntelSyntax),
371 };
372
373 static bitfield operand_types[] =
374 {
375 BITFIELD (Reg8),
376 BITFIELD (Reg16),
377 BITFIELD (Reg32),
378 BITFIELD (Reg64),
379 BITFIELD (FloatReg),
380 BITFIELD (RegMMX),
381 BITFIELD (RegXMM),
382 BITFIELD (RegYMM),
383 BITFIELD (Imm8),
384 BITFIELD (Imm8S),
385 BITFIELD (Imm16),
386 BITFIELD (Imm32),
387 BITFIELD (Imm32S),
388 BITFIELD (Imm64),
389 BITFIELD (Imm1),
390 BITFIELD (BaseIndex),
391 BITFIELD (Disp8),
392 BITFIELD (Disp16),
393 BITFIELD (Disp32),
394 BITFIELD (Disp32S),
395 BITFIELD (Disp64),
396 BITFIELD (InOutPortReg),
397 BITFIELD (ShiftCount),
398 BITFIELD (Control),
399 BITFIELD (Debug),
400 BITFIELD (Test),
401 BITFIELD (SReg2),
402 BITFIELD (SReg3),
403 BITFIELD (Acc),
404 BITFIELD (FloatAcc),
405 BITFIELD (JumpAbsolute),
406 BITFIELD (EsSeg),
407 BITFIELD (RegMem),
408 BITFIELD (Mem),
409 BITFIELD (Byte),
410 BITFIELD (Word),
411 BITFIELD (Dword),
412 BITFIELD (Fword),
413 BITFIELD (Qword),
414 BITFIELD (Tbyte),
415 BITFIELD (Xmmword),
416 BITFIELD (Ymmword),
417 BITFIELD (Unspecified),
418 BITFIELD (Anysize),
419 #ifdef OTUnused
420 BITFIELD (OTUnused),
421 #endif
422 };
423
424 static const char *filename;
425
426 static int
427 compare (const void *x, const void *y)
428 {
429 const bitfield *xp = (const bitfield *) x;
430 const bitfield *yp = (const bitfield *) y;
431 return xp->position - yp->position;
432 }
433
434 static void
435 fail (const char *message, ...)
436 {
437 va_list args;
438
439 va_start (args, message);
440 fprintf (stderr, _("%s: Error: "), program_name);
441 vfprintf (stderr, message, args);
442 va_end (args);
443 xexit (1);
444 }
445
446 static void
447 process_copyright (FILE *fp)
448 {
449 fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
450 /* Copyright 2007, 2008, 2009\n\
451 Free Software Foundation, Inc.\n\
452 \n\
453 This file is part of the GNU opcodes library.\n\
454 \n\
455 This library is free software; you can redistribute it and/or modify\n\
456 it under the terms of the GNU General Public License as published by\n\
457 the Free Software Foundation; either version 3, or (at your option)\n\
458 any later version.\n\
459 \n\
460 It is distributed in the hope that it will be useful, but WITHOUT\n\
461 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
462 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
463 License for more details.\n\
464 \n\
465 You should have received a copy of the GNU General Public License\n\
466 along with this program; if not, write to the Free Software\n\
467 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
468 MA 02110-1301, USA. */\n");
469 }
470
471 /* Remove leading white spaces. */
472
473 static char *
474 remove_leading_whitespaces (char *str)
475 {
476 while (ISSPACE (*str))
477 str++;
478 return str;
479 }
480
481 /* Remove trailing white spaces. */
482
483 static void
484 remove_trailing_whitespaces (char *str)
485 {
486 size_t last = strlen (str);
487
488 if (last == 0)
489 return;
490
491 do
492 {
493 last--;
494 if (ISSPACE (str [last]))
495 str[last] = '\0';
496 else
497 break;
498 }
499 while (last != 0);
500 }
501
502 /* Find next field separated by SEP and terminate it. Return a
503 pointer to the one after it. */
504
505 static char *
506 next_field (char *str, char sep, char **next, char *last)
507 {
508 char *p;
509
510 p = remove_leading_whitespaces (str);
511 for (str = p; *str != sep && *str != '\0'; str++);
512
513 *str = '\0';
514 remove_trailing_whitespaces (p);
515
516 *next = str + 1;
517
518 if (p >= last)
519 abort ();
520
521 return p;
522 }
523
524 static void
525 set_bitfield (const char *f, bitfield *array, int value,
526 unsigned int size, int lineno)
527 {
528 unsigned int i;
529
530 if (strcmp (f, "CpuFP") == 0)
531 {
532 set_bitfield("Cpu387", array, value, size, lineno);
533 set_bitfield("Cpu287", array, value, size, lineno);
534 f = "Cpu8087";
535 }
536 else if (strcmp (f, "Mmword") == 0)
537 f= "Qword";
538 else if (strcmp (f, "Oword") == 0)
539 f= "Xmmword";
540
541 for (i = 0; i < size; i++)
542 if (strcasecmp (array[i].name, f) == 0)
543 {
544 array[i].value = value;
545 return;
546 }
547
548 if (value)
549 {
550 const char *v = strchr (f, '=');
551
552 if (v)
553 {
554 size_t n = v - f;
555 char *end;
556
557 for (i = 0; i < size; i++)
558 if (strncasecmp (array[i].name, f, n) == 0)
559 {
560 value = strtol (v + 1, &end, 0);
561 if (*end == '\0')
562 {
563 array[i].value = value;
564 return;
565 }
566 break;
567 }
568 }
569 }
570
571 if (lineno != -1)
572 fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f);
573 else
574 fail (_("Unknown bitfield: %s\n"), f);
575 }
576
577 static void
578 output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
579 int macro, const char *comma, const char *indent)
580 {
581 unsigned int i;
582
583 fprintf (table, "%s{ { ", indent);
584
585 for (i = 0; i < size - 1; i++)
586 {
587 fprintf (table, "%d, ", flags[i].value);
588 if (((i + 1) % 20) == 0)
589 {
590 /* We need \\ for macro. */
591 if (macro)
592 fprintf (table, " \\\n %s", indent);
593 else
594 fprintf (table, "\n %s", indent);
595 }
596 }
597
598 fprintf (table, "%d } }%s\n", flags[i].value, comma);
599 }
600
601 static void
602 process_i386_cpu_flag (FILE *table, char *flag, int macro,
603 const char *comma, const char *indent,
604 int lineno)
605 {
606 char *str, *next, *last;
607 unsigned int i;
608 bitfield flags [ARRAY_SIZE (cpu_flags)];
609
610 /* Copy the default cpu flags. */
611 memcpy (flags, cpu_flags, sizeof (cpu_flags));
612
613 if (strcasecmp (flag, "unknown") == 0)
614 {
615 /* We turn on everything except for cpu64 in case of
616 CPU_UNKNOWN_FLAGS. */
617 for (i = 0; i < ARRAY_SIZE (flags); i++)
618 if (flags[i].position != Cpu64)
619 flags[i].value = 1;
620 }
621 else if (flag[0] == '~')
622 {
623 last = flag + strlen (flag);
624
625 if (flag[1] == '(')
626 {
627 last -= 1;
628 next = flag + 2;
629 if (*last != ')')
630 fail (_("%s: %d: Missing `)' in bitfield: %s\n"), filename,
631 lineno, flag);
632 *last = '\0';
633 }
634 else
635 next = flag + 1;
636
637 /* First we turn on everything except for cpu64. */
638 for (i = 0; i < ARRAY_SIZE (flags); i++)
639 if (flags[i].position != Cpu64)
640 flags[i].value = 1;
641
642 /* Turn off selective bits. */
643 for (; next && next < last; )
644 {
645 str = next_field (next, '|', &next, last);
646 if (str)
647 set_bitfield (str, flags, 0, ARRAY_SIZE (flags), lineno);
648 }
649 }
650 else if (strcmp (flag, "0"))
651 {
652 /* Turn on selective bits. */
653 last = flag + strlen (flag);
654 for (next = flag; next && next < last; )
655 {
656 str = next_field (next, '|', &next, last);
657 if (str)
658 set_bitfield (str, flags, 1, ARRAY_SIZE (flags), lineno);
659 }
660 }
661
662 output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
663 comma, indent);
664 }
665
666 static void
667 output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
668 {
669 unsigned int i;
670
671 fprintf (table, " { ");
672
673 for (i = 0; i < size - 1; i++)
674 {
675 fprintf (table, "%d, ", modifier[i].value);
676 if (((i + 1) % 20) == 0)
677 fprintf (table, "\n ");
678 }
679
680 fprintf (table, "%d },\n", modifier[i].value);
681 }
682
683 static void
684 process_i386_opcode_modifier (FILE *table, char *mod, int lineno)
685 {
686 char *str, *next, *last;
687 bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
688
689 /* Copy the default opcode modifier. */
690 memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
691
692 if (strcmp (mod, "0"))
693 {
694 last = mod + strlen (mod);
695 for (next = mod; next && next < last; )
696 {
697 str = next_field (next, '|', &next, last);
698 if (str)
699 set_bitfield (str, modifiers, 1, ARRAY_SIZE (modifiers),
700 lineno);
701 }
702 }
703 output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
704 }
705
706 static void
707 output_operand_type (FILE *table, bitfield *types, unsigned int size,
708 int macro, const char *indent)
709 {
710 unsigned int i;
711
712 fprintf (table, "{ { ");
713
714 for (i = 0; i < size - 1; i++)
715 {
716 fprintf (table, "%d, ", types[i].value);
717 if (((i + 1) % 20) == 0)
718 {
719 /* We need \\ for macro. */
720 if (macro)
721 fprintf (table, "\\\n%s", indent);
722 else
723 fprintf (table, "\n%s", indent);
724 }
725 }
726
727 fprintf (table, "%d } }", types[i].value);
728 }
729
730 static void
731 process_i386_operand_type (FILE *table, char *op, int macro,
732 const char *indent, int lineno)
733 {
734 char *str, *next, *last;
735 bitfield types [ARRAY_SIZE (operand_types)];
736
737 /* Copy the default operand type. */
738 memcpy (types, operand_types, sizeof (types));
739
740 if (strcmp (op, "0"))
741 {
742 last = op + strlen (op);
743 for (next = op; next && next < last; )
744 {
745 str = next_field (next, '|', &next, last);
746 if (str)
747 set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno);
748 }
749 }
750 output_operand_type (table, types, ARRAY_SIZE (types), macro,
751 indent);
752 }
753
754 static void
755 output_i386_opcode (FILE *table, const char *name, char *str,
756 char *last, int lineno)
757 {
758 unsigned int i;
759 char *operands, *base_opcode, *extension_opcode, *opcode_length;
760 char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
761
762 /* Find number of operands. */
763 operands = next_field (str, ',', &str, last);
764
765 /* Find base_opcode. */
766 base_opcode = next_field (str, ',', &str, last);
767
768 /* Find extension_opcode. */
769 extension_opcode = next_field (str, ',', &str, last);
770
771 /* Find opcode_length. */
772 opcode_length = next_field (str, ',', &str, last);
773
774 /* Find cpu_flags. */
775 cpu_flags = next_field (str, ',', &str, last);
776
777 /* Find opcode_modifier. */
778 opcode_modifier = next_field (str, ',', &str, last);
779
780 /* Remove the first {. */
781 str = remove_leading_whitespaces (str);
782 if (*str != '{')
783 abort ();
784 str = remove_leading_whitespaces (str + 1);
785
786 i = strlen (str);
787
788 /* There are at least "X}". */
789 if (i < 2)
790 abort ();
791
792 /* Remove trailing white spaces and }. */
793 do
794 {
795 i--;
796 if (ISSPACE (str[i]) || str[i] == '}')
797 str[i] = '\0';
798 else
799 break;
800 }
801 while (i != 0);
802
803 last = str + i;
804
805 /* Find operand_types. */
806 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
807 {
808 if (str >= last)
809 {
810 operand_types [i] = NULL;
811 break;
812 }
813
814 operand_types [i] = next_field (str, ',', &str, last);
815 if (*operand_types[i] == '0')
816 {
817 if (i != 0)
818 operand_types[i] = NULL;
819 break;
820 }
821 }
822
823 fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
824 name, operands, base_opcode, extension_opcode,
825 opcode_length);
826
827 process_i386_cpu_flag (table, cpu_flags, 0, ",", " ", lineno);
828
829 process_i386_opcode_modifier (table, opcode_modifier, lineno);
830
831 fprintf (table, " { ");
832
833 for (i = 0; i < ARRAY_SIZE (operand_types); i++)
834 {
835 if (operand_types[i] == NULL || *operand_types[i] == '0')
836 {
837 if (i == 0)
838 process_i386_operand_type (table, "0", 0, "\t ", lineno);
839 break;
840 }
841
842 if (i != 0)
843 fprintf (table, ",\n ");
844
845 process_i386_operand_type (table, operand_types[i], 0,
846 "\t ", lineno);
847 }
848 fprintf (table, " } },\n");
849 }
850
851 struct opcode_hash_entry
852 {
853 struct opcode_hash_entry *next;
854 char *name;
855 char *opcode;
856 int lineno;
857 };
858
859 /* Calculate the hash value of an opcode hash entry P. */
860
861 static hashval_t
862 opcode_hash_hash (const void *p)
863 {
864 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
865 return htab_hash_string (entry->name);
866 }
867
868 /* Compare a string Q against an opcode hash entry P. */
869
870 static int
871 opcode_hash_eq (const void *p, const void *q)
872 {
873 struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
874 const char *name = (const char *) q;
875 return strcmp (name, entry->name) == 0;
876 }
877
878 static void
879 process_i386_opcodes (FILE *table)
880 {
881 FILE *fp;
882 char buf[2048];
883 unsigned int i, j;
884 char *str, *p, *last, *name;
885 struct opcode_hash_entry **hash_slot, **entry, *next;
886 htab_t opcode_hash_table;
887 struct opcode_hash_entry **opcode_array;
888 unsigned int opcode_array_size = 1024;
889 int lineno = 0;
890
891 filename = "i386-opc.tbl";
892 fp = fopen (filename, "r");
893
894 if (fp == NULL)
895 fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
896 xstrerror (errno));
897
898 i = 0;
899 opcode_array = (struct opcode_hash_entry **)
900 xmalloc (sizeof (*opcode_array) * opcode_array_size);
901
902 opcode_hash_table = htab_create_alloc (16, opcode_hash_hash,
903 opcode_hash_eq, NULL,
904 xcalloc, free);
905
906 fprintf (table, "\n/* i386 opcode table. */\n\n");
907 fprintf (table, "const insn_template i386_optab[] =\n{\n");
908
909 /* Put everything on opcode array. */
910 while (!feof (fp))
911 {
912 if (fgets (buf, sizeof (buf), fp) == NULL)
913 break;
914
915 lineno++;
916
917 p = remove_leading_whitespaces (buf);
918
919 /* Skip comments. */
920 str = strstr (p, "//");
921 if (str != NULL)
922 str[0] = '\0';
923
924 /* Remove trailing white spaces. */
925 remove_trailing_whitespaces (p);
926
927 switch (p[0])
928 {
929 case '#':
930 /* Ignore comments. */
931 case '\0':
932 continue;
933 break;
934 default:
935 break;
936 }
937
938 last = p + strlen (p);
939
940 /* Find name. */
941 name = next_field (p, ',', &str, last);
942
943 /* Get the slot in hash table. */
944 hash_slot = (struct opcode_hash_entry **)
945 htab_find_slot_with_hash (opcode_hash_table, name,
946 htab_hash_string (name),
947 INSERT);
948
949 if (*hash_slot == NULL)
950 {
951 /* It is the new one. Put it on opcode array. */
952 if (i >= opcode_array_size)
953 {
954 /* Grow the opcode array when needed. */
955 opcode_array_size += 1024;
956 opcode_array = (struct opcode_hash_entry **)
957 xrealloc (opcode_array,
958 sizeof (*opcode_array) * opcode_array_size);
959 }
960
961 opcode_array[i] = (struct opcode_hash_entry *)
962 xmalloc (sizeof (struct opcode_hash_entry));
963 opcode_array[i]->next = NULL;
964 opcode_array[i]->name = xstrdup (name);
965 opcode_array[i]->opcode = xstrdup (str);
966 opcode_array[i]->lineno = lineno;
967 *hash_slot = opcode_array[i];
968 i++;
969 }
970 else
971 {
972 /* Append it to the existing one. */
973 entry = hash_slot;
974 while ((*entry) != NULL)
975 entry = &(*entry)->next;
976 *entry = (struct opcode_hash_entry *)
977 xmalloc (sizeof (struct opcode_hash_entry));
978 (*entry)->next = NULL;
979 (*entry)->name = (*hash_slot)->name;
980 (*entry)->opcode = xstrdup (str);
981 (*entry)->lineno = lineno;
982 }
983 }
984
985 /* Process opcode array. */
986 for (j = 0; j < i; j++)
987 {
988 for (next = opcode_array[j]; next; next = next->next)
989 {
990 name = next->name;
991 str = next->opcode;
992 lineno = next->lineno;
993 last = str + strlen (str);
994 output_i386_opcode (table, name, str, last, lineno);
995 }
996 }
997
998 fclose (fp);
999
1000 fprintf (table, " { NULL, 0, 0, 0, 0,\n");
1001
1002 process_i386_cpu_flag (table, "0", 0, ",", " ", -1);
1003
1004 process_i386_opcode_modifier (table, "0", -1);
1005
1006 fprintf (table, " { ");
1007 process_i386_operand_type (table, "0", 0, "\t ", -1);
1008 fprintf (table, " } }\n");
1009
1010 fprintf (table, "};\n");
1011 }
1012
1013 static void
1014 process_i386_registers (FILE *table)
1015 {
1016 FILE *fp;
1017 char buf[2048];
1018 char *str, *p, *last;
1019 char *reg_name, *reg_type, *reg_flags, *reg_num;
1020 char *dw2_32_num, *dw2_64_num;
1021 int lineno = 0;
1022
1023 filename = "i386-reg.tbl";
1024 fp = fopen (filename, "r");
1025 if (fp == NULL)
1026 fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
1027 xstrerror (errno));
1028
1029 fprintf (table, "\n/* i386 register table. */\n\n");
1030 fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
1031
1032 while (!feof (fp))
1033 {
1034 if (fgets (buf, sizeof (buf), fp) == NULL)
1035 break;
1036
1037 lineno++;
1038
1039 p = remove_leading_whitespaces (buf);
1040
1041 /* Skip comments. */
1042 str = strstr (p, "//");
1043 if (str != NULL)
1044 str[0] = '\0';
1045
1046 /* Remove trailing white spaces. */
1047 remove_trailing_whitespaces (p);
1048
1049 switch (p[0])
1050 {
1051 case '#':
1052 fprintf (table, "%s\n", p);
1053 case '\0':
1054 continue;
1055 break;
1056 default:
1057 break;
1058 }
1059
1060 last = p + strlen (p);
1061
1062 /* Find reg_name. */
1063 reg_name = next_field (p, ',', &str, last);
1064
1065 /* Find reg_type. */
1066 reg_type = next_field (str, ',', &str, last);
1067
1068 /* Find reg_flags. */
1069 reg_flags = next_field (str, ',', &str, last);
1070
1071 /* Find reg_num. */
1072 reg_num = next_field (str, ',', &str, last);
1073
1074 fprintf (table, " { \"%s\",\n ", reg_name);
1075
1076 process_i386_operand_type (table, reg_type, 0, "\t", lineno);
1077
1078 /* Find 32-bit Dwarf2 register number. */
1079 dw2_32_num = next_field (str, ',', &str, last);
1080
1081 /* Find 64-bit Dwarf2 register number. */
1082 dw2_64_num = next_field (str, ',', &str, last);
1083
1084 fprintf (table, ",\n %s, %s, { %s, %s } },\n",
1085 reg_flags, reg_num, dw2_32_num, dw2_64_num);
1086 }
1087
1088 fclose (fp);
1089
1090 fprintf (table, "};\n");
1091
1092 fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
1093 }
1094
1095 static void
1096 process_i386_initializers (void)
1097 {
1098 unsigned int i;
1099 FILE *fp = fopen ("i386-init.h", "w");
1100 char *init;
1101
1102 if (fp == NULL)
1103 fail (_("can't create i386-init.h, errno = %s\n"),
1104 xstrerror (errno));
1105
1106 process_copyright (fp);
1107
1108 for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
1109 {
1110 fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
1111 init = xstrdup (cpu_flag_init[i].init);
1112 process_i386_cpu_flag (fp, init, 1, "", " ", -1);
1113 free (init);
1114 }
1115
1116 for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
1117 {
1118 fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
1119 init = xstrdup (operand_type_init[i].init);
1120 process_i386_operand_type (fp, init, 1, " ", -1);
1121 free (init);
1122 }
1123 fprintf (fp, "\n");
1124
1125 fclose (fp);
1126 }
1127
1128 /* Program options. */
1129 #define OPTION_SRCDIR 200
1130
1131 struct option long_options[] =
1132 {
1133 {"srcdir", required_argument, NULL, OPTION_SRCDIR},
1134 {"debug", no_argument, NULL, 'd'},
1135 {"version", no_argument, NULL, 'V'},
1136 {"help", no_argument, NULL, 'h'},
1137 {0, no_argument, NULL, 0}
1138 };
1139
1140 static void
1141 print_version (void)
1142 {
1143 printf ("%s: version 1.0\n", program_name);
1144 xexit (0);
1145 }
1146
1147 static void
1148 usage (FILE * stream, int status)
1149 {
1150 fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
1151 program_name);
1152 xexit (status);
1153 }
1154
1155 int
1156 main (int argc, char **argv)
1157 {
1158 extern int chdir (char *);
1159 char *srcdir = NULL;
1160 int c;
1161 FILE *table;
1162
1163 program_name = *argv;
1164 xmalloc_set_program_name (program_name);
1165
1166 while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
1167 switch (c)
1168 {
1169 case OPTION_SRCDIR:
1170 srcdir = optarg;
1171 break;
1172 case 'V':
1173 case 'v':
1174 print_version ();
1175 break;
1176 case 'd':
1177 debug = 1;
1178 break;
1179 case 'h':
1180 case '?':
1181 usage (stderr, 0);
1182 default:
1183 case 0:
1184 break;
1185 }
1186
1187 if (optind != argc)
1188 usage (stdout, 1);
1189
1190 if (srcdir != NULL)
1191 if (chdir (srcdir) != 0)
1192 fail (_("unable to change directory to \"%s\", errno = %s\n"),
1193 srcdir, xstrerror (errno));
1194
1195 /* Check the unused bitfield in i386_cpu_flags. */
1196 #ifndef CpuUnused
1197 c = CpuNumOfBits - CpuMax - 1;
1198 if (c)
1199 fail (_("%d unused bits in i386_cpu_flags.\n"), c);
1200 #endif
1201
1202 /* Check the unused bitfield in i386_operand_type. */
1203 #ifndef OTUnused
1204 c = OTNumOfBits - OTMax - 1;
1205 if (c)
1206 fail (_("%d unused bits in i386_operand_type.\n"), c);
1207 #endif
1208
1209 qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
1210 compare);
1211
1212 qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
1213 sizeof (opcode_modifiers [0]), compare);
1214
1215 qsort (operand_types, ARRAY_SIZE (operand_types),
1216 sizeof (operand_types [0]), compare);
1217
1218 table = fopen ("i386-tbl.h", "w");
1219 if (table == NULL)
1220 fail (_("can't create i386-tbl.h, errno = %s\n"),
1221 xstrerror (errno));
1222
1223 process_copyright (table);
1224
1225 process_i386_opcodes (table);
1226 process_i386_registers (table);
1227 process_i386_initializers ();
1228
1229 fclose (table);
1230
1231 exit (0);
1232 }
This page took 0.055592 seconds and 5 git commands to generate.