d6bcf8d707ad78cd6196062568882e958aa93323
[platform/upstream/elfutils.git] / libcpu / i386_parse.c
1 /* A Bison parser, made by GNU Bison 2.7.  */
2
3 /* Bison implementation for Yacc-like parsers in C
4    
5       Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.
6    
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* As a special exception, you may create a larger work that contains
21    part or all of the Bison parser skeleton and distribute that work
22    under terms of your choice, so long as that work isn't itself a
23    parser generator using the skeleton or a modified version thereof
24    as a parser skeleton.  Alternatively, if you modify or redistribute
25    the parser skeleton itself, you may (at your option) remove this
26    special exception, which will cause the skeleton and the resulting
27    Bison output files to be licensed under the GNU General Public
28    License without this special exception.
29    
30    This special exception was added by the Free Software Foundation in
31    version 2.2 of Bison.  */
32
33 /* C LALR(1) parser skeleton written by Richard Stallman, by
34    simplifying the original so-called "semantic" parser.  */
35
36 /* All symbols defined below should begin with yy or YY, to avoid
37    infringing on user name space.  This should be done even for local
38    variables, as they might otherwise be expanded by user macros.
39    There are some unavoidable exceptions within include files to
40    define necessary library symbols; they are noted "INFRINGES ON
41    USER NAME SPACE" below.  */
42
43 /* Identify Bison output.  */
44 #define YYBISON 1
45
46 /* Bison version.  */
47 #define YYBISON_VERSION "2.7"
48
49 /* Skeleton name.  */
50 #define YYSKELETON_NAME "yacc.c"
51
52 /* Pure parsers.  */
53 #define YYPURE 0
54
55 /* Push parsers.  */
56 #define YYPUSH 0
57
58 /* Pull parsers.  */
59 #define YYPULL 1
60
61
62 /* Substitute the variable and function names.  */
63 #define yyparse         i386_parse
64 #define yylex           i386_lex
65 #define yyerror         i386_error
66 #define yylval          i386_lval
67 #define yychar          i386_char
68 #define yydebug         i386_debug
69 #define yynerrs         i386_nerrs
70
71 /* Copy the first part of user declarations.  */
72 /* Line 371 of yacc.c  */
73 #line 1 "i386_parse.y"
74
75 /* Parser for i386 CPU description.
76    Copyright (C) 2004, 2005, 2007, 2008, 2009 Red Hat, Inc.
77    Written by Ulrich Drepper <drepper@redhat.com>, 2004.
78
79    This file is free software; you can redistribute it and/or modify
80    it under the terms of either
81
82      * the GNU Lesser General Public License as published by the Free
83        Software Foundation; either version 3 of the License, or (at
84        your option) any later version
85
86    or
87
88      * the GNU General Public License as published by the Free
89        Software Foundation; either version 2 of the License, or (at
90        your option) any later version
91
92    or both in parallel, as here.
93
94    elfutils is distributed in the hope that it will be useful, but
95    WITHOUT ANY WARRANTY; without even the implied warranty of
96    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
97    General Public License for more details.
98
99    You should have received copies of the GNU General Public License and
100    the GNU Lesser General Public License along with this program.  If
101    not, see <http://www.gnu.org/licenses/>.  */
102
103 #ifdef HAVE_CONFIG_H
104 # include <config.h>
105 #endif
106
107 #include <assert.h>
108 #include <ctype.h>
109 #include <errno.h>
110 #include <error.h>
111 #include <inttypes.h>
112 #include <libintl.h>
113 #include <math.h>
114 #include <obstack.h>
115 #include <search.h>
116 #include <stdbool.h>
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120 #include <sys/param.h>
121
122 #include <system.h>
123
124 #define obstack_chunk_alloc xmalloc
125 #define obstack_chunk_free free
126
127 /* The error handler.  */
128 static void yyerror (const char *s);
129
130 extern int yylex (void);
131 extern int i386_lineno;
132 extern char *infname;
133
134
135 struct known_bitfield
136 {
137   char *name;
138   unsigned long int bits;
139   int tmp;
140 };
141
142
143 struct bitvalue
144 {
145   enum bittype { zeroone, field, failure } type;
146   union
147   {
148     unsigned int value;
149     struct known_bitfield *field;
150   };
151   struct bitvalue *next;
152 };
153
154
155 struct argname
156 {
157   enum nametype { string, nfield } type;
158   union
159   {
160     char *str;
161     struct known_bitfield *field;
162   };
163   struct argname *next;
164 };
165
166
167 struct argument
168 {
169   struct argname *name;
170   struct argument *next;
171 };
172
173
174 struct instruction
175 {
176   /* The byte encoding.  */
177   struct bitvalue *bytes;
178
179   /* Prefix possible.  */
180   int repe;
181   int rep;
182
183   /* Mnemonic.  */
184   char *mnemonic;
185
186   /* Suffix.  */
187   enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
188          suffix_w1, suffix_W1, suffix_D } suffix;
189
190   /* Flag set if modr/m is used.  */
191   int modrm;
192
193   /* Operands.  */
194   struct operand
195   {
196     char *fct;
197     char *str;
198     int off1;
199     int off2;
200     int off3;
201   } operands[3];
202
203   struct instruction *next;
204 };
205
206
207 struct synonym
208 {
209   char *from;
210   char *to;
211 };
212
213
214 struct suffix
215 {
216   char *name;
217   int idx;
218 };
219
220
221 struct argstring
222 {
223   char *str;
224   int idx;
225   int off;
226 };
227
228
229 static struct known_bitfield ax_reg =
230   {
231     .name = "ax", .bits = 0, .tmp = 0
232   };
233
234 static struct known_bitfield dx_reg =
235   {
236     .name = "dx", .bits = 0, .tmp = 0
237   };
238
239 static struct known_bitfield di_reg =
240   {
241     .name = "es_di", .bits = 0, .tmp = 0
242   };
243
244 static struct known_bitfield si_reg =
245   {
246     .name = "ds_si", .bits = 0, .tmp = 0
247   };
248
249 static struct known_bitfield bx_reg =
250   {
251     .name = "ds_bx", .bits = 0, .tmp = 0
252   };
253
254
255 static int bitfield_compare (const void *p1, const void *p2);
256 static void new_bitfield (char *name, unsigned long int num);
257 static void check_bits (struct bitvalue *value);
258 static int check_duplicates (struct bitvalue *val);
259 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
260 static int check_bitsused (struct bitvalue *bitval,
261                            struct known_bitfield *suffix,
262                            struct argument *args);
263 static struct argname *combine (struct argname *name);
264 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
265                         struct instruction *instr, int n);
266 static void find_numbers (void);
267 static int compare_syn (const void *p1, const void *p2);
268 static int compare_suf (const void *p1, const void *p2);
269 static void instrtable_out (void);
270 #if 0
271 static void create_mnemonic_table (void);
272 #endif
273
274 static void *bitfields;
275 static struct instruction *instructions;
276 static size_t ninstructions;
277 static void *synonyms;
278 static void *suffixes;
279 static int nsuffixes;
280 static void *mnemonics;
281 size_t nmnemonics;
282 extern FILE *outfile;
283
284 /* Number of bits used mnemonics.  */
285 #if 0
286 static size_t best_mnemonic_bits;
287 #endif
288
289 /* Line 371 of yacc.c  */
290 #line 291 "i386_parse.c"
291
292 # ifndef YY_NULL
293 #  if defined __cplusplus && 201103L <= __cplusplus
294 #   define YY_NULL nullptr
295 #  else
296 #   define YY_NULL 0
297 #  endif
298 # endif
299
300 /* Enabling verbose error messages.  */
301 #ifdef YYERROR_VERBOSE
302 # undef YYERROR_VERBOSE
303 # define YYERROR_VERBOSE 1
304 #else
305 # define YYERROR_VERBOSE 0
306 #endif
307
308 /* In a future release of Bison, this section will be replaced
309    by #include "y.tab.h".  */
310 #ifndef YY_I386_I_PARSE_H_INCLUDED
311 # define YY_I386_I_PARSE_H_INCLUDED
312 /* Enabling traces.  */
313 #ifndef YYDEBUG
314 # define YYDEBUG 0
315 #endif
316 #if YYDEBUG
317 extern int i386_debug;
318 #endif
319
320 /* Tokens.  */
321 #ifndef YYTOKENTYPE
322 # define YYTOKENTYPE
323    /* Put the tokens into the symbol table, so that GDB and other debuggers
324       know about them.  */
325    enum yytokentype {
326      kMASK = 258,
327      kPREFIX = 259,
328      kSUFFIX = 260,
329      kSYNONYM = 261,
330      kID = 262,
331      kNUMBER = 263,
332      kPERCPERC = 264,
333      kBITFIELD = 265,
334      kCHAR = 266,
335      kSPACE = 267
336    };
337 #endif
338 /* Tokens.  */
339 #define kMASK 258
340 #define kPREFIX 259
341 #define kSUFFIX 260
342 #define kSYNONYM 261
343 #define kID 262
344 #define kNUMBER 263
345 #define kPERCPERC 264
346 #define kBITFIELD 265
347 #define kCHAR 266
348 #define kSPACE 267
349
350
351
352 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
353 typedef union YYSTYPE
354 {
355 /* Line 387 of yacc.c  */
356 #line 217 "i386_parse.y"
357
358   unsigned long int num;
359   char *str;
360   char ch;
361   struct known_bitfield *field;
362   struct bitvalue *bit;
363   struct argname *name;
364   struct argument *arg;
365
366
367 /* Line 387 of yacc.c  */
368 #line 369 "i386_parse.c"
369 } YYSTYPE;
370 # define YYSTYPE_IS_TRIVIAL 1
371 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
372 # define YYSTYPE_IS_DECLARED 1
373 #endif
374
375 extern YYSTYPE i386_lval;
376
377 #ifdef YYPARSE_PARAM
378 #if defined __STDC__ || defined __cplusplus
379 int i386_parse (void *YYPARSE_PARAM);
380 #else
381 int i386_parse ();
382 #endif
383 #else /* ! YYPARSE_PARAM */
384 #if defined __STDC__ || defined __cplusplus
385 int i386_parse (void);
386 #else
387 int i386_parse ();
388 #endif
389 #endif /* ! YYPARSE_PARAM */
390
391 #endif /* !YY_I386_I_PARSE_H_INCLUDED  */
392
393 /* Copy the second part of user declarations.  */
394
395 /* Line 390 of yacc.c  */
396 #line 397 "i386_parse.c"
397
398 #ifdef short
399 # undef short
400 #endif
401
402 #ifdef YYTYPE_UINT8
403 typedef YYTYPE_UINT8 yytype_uint8;
404 #else
405 typedef unsigned char yytype_uint8;
406 #endif
407
408 #ifdef YYTYPE_INT8
409 typedef YYTYPE_INT8 yytype_int8;
410 #elif (defined __STDC__ || defined __C99__FUNC__ \
411      || defined __cplusplus || defined _MSC_VER)
412 typedef signed char yytype_int8;
413 #else
414 typedef short int yytype_int8;
415 #endif
416
417 #ifdef YYTYPE_UINT16
418 typedef YYTYPE_UINT16 yytype_uint16;
419 #else
420 typedef unsigned short int yytype_uint16;
421 #endif
422
423 #ifdef YYTYPE_INT16
424 typedef YYTYPE_INT16 yytype_int16;
425 #else
426 typedef short int yytype_int16;
427 #endif
428
429 #ifndef YYSIZE_T
430 # ifdef __SIZE_TYPE__
431 #  define YYSIZE_T __SIZE_TYPE__
432 # elif defined size_t
433 #  define YYSIZE_T size_t
434 # elif ! defined YYSIZE_T && (defined __STDC__ || defined __C99__FUNC__ \
435      || defined __cplusplus || defined _MSC_VER)
436 #  include <stddef.h> /* INFRINGES ON USER NAME SPACE */
437 #  define YYSIZE_T size_t
438 # else
439 #  define YYSIZE_T unsigned int
440 # endif
441 #endif
442
443 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
444
445 #ifndef YY_
446 # if defined YYENABLE_NLS && YYENABLE_NLS
447 #  if ENABLE_NLS
448 #   include <libintl.h> /* INFRINGES ON USER NAME SPACE */
449 #   define YY_(Msgid) dgettext ("bison-runtime", Msgid)
450 #  endif
451 # endif
452 # ifndef YY_
453 #  define YY_(Msgid) Msgid
454 # endif
455 #endif
456
457 /* Suppress unused-variable warnings by "using" E.  */
458 #if ! defined lint || defined __GNUC__
459 # define YYUSE(E) ((void) (E))
460 #else
461 # define YYUSE(E) /* empty */
462 #endif
463
464 /* Identity function, used to suppress warnings about constant conditions.  */
465 #ifndef lint
466 # define YYID(N) (N)
467 #else
468 #if (defined __STDC__ || defined __C99__FUNC__ \
469      || defined __cplusplus || defined _MSC_VER)
470 static int
471 YYID (int yyi)
472 #else
473 static int
474 YYID (yyi)
475     int yyi;
476 #endif
477 {
478   return yyi;
479 }
480 #endif
481
482 #if ! defined yyoverflow || YYERROR_VERBOSE
483
484 /* The parser invokes alloca or malloc; define the necessary symbols.  */
485
486 # ifdef YYSTACK_USE_ALLOCA
487 #  if YYSTACK_USE_ALLOCA
488 #   ifdef __GNUC__
489 #    define YYSTACK_ALLOC __builtin_alloca
490 #   elif defined __BUILTIN_VA_ARG_INCR
491 #    include <alloca.h> /* INFRINGES ON USER NAME SPACE */
492 #   elif defined _AIX
493 #    define YYSTACK_ALLOC __alloca
494 #   elif defined _MSC_VER
495 #    include <malloc.h> /* INFRINGES ON USER NAME SPACE */
496 #    define alloca _alloca
497 #   else
498 #    define YYSTACK_ALLOC alloca
499 #    if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
500      || defined __cplusplus || defined _MSC_VER)
501 #     include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
502       /* Use EXIT_SUCCESS as a witness for stdlib.h.  */
503 #     ifndef EXIT_SUCCESS
504 #      define EXIT_SUCCESS 0
505 #     endif
506 #    endif
507 #   endif
508 #  endif
509 # endif
510
511 # ifdef YYSTACK_ALLOC
512    /* Pacify GCC's `empty if-body' warning.  */
513 #  define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
514 #  ifndef YYSTACK_ALLOC_MAXIMUM
515     /* The OS might guarantee only one guard page at the bottom of the stack,
516        and a page size can be as small as 4096 bytes.  So we cannot safely
517        invoke alloca (N) if N exceeds 4096.  Use a slightly smaller number
518        to allow for a few compiler-allocated temporary stack slots.  */
519 #   define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
520 #  endif
521 # else
522 #  define YYSTACK_ALLOC YYMALLOC
523 #  define YYSTACK_FREE YYFREE
524 #  ifndef YYSTACK_ALLOC_MAXIMUM
525 #   define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
526 #  endif
527 #  if (defined __cplusplus && ! defined EXIT_SUCCESS \
528        && ! ((defined YYMALLOC || defined malloc) \
529              && (defined YYFREE || defined free)))
530 #   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
531 #   ifndef EXIT_SUCCESS
532 #    define EXIT_SUCCESS 0
533 #   endif
534 #  endif
535 #  ifndef YYMALLOC
536 #   define YYMALLOC malloc
537 #   if ! defined malloc && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
538      || defined __cplusplus || defined _MSC_VER)
539 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
540 #   endif
541 #  endif
542 #  ifndef YYFREE
543 #   define YYFREE free
544 #   if ! defined free && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
545      || defined __cplusplus || defined _MSC_VER)
546 void free (void *); /* INFRINGES ON USER NAME SPACE */
547 #   endif
548 #  endif
549 # endif
550 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
551
552
553 #if (! defined yyoverflow \
554      && (! defined __cplusplus \
555          || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
556
557 /* A type that is properly aligned for any stack member.  */
558 union yyalloc
559 {
560   yytype_int16 yyss_alloc;
561   YYSTYPE yyvs_alloc;
562 };
563
564 /* The size of the maximum gap between one aligned stack and the next.  */
565 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
566
567 /* The size of an array large to enough to hold all stacks, each with
568    N elements.  */
569 # define YYSTACK_BYTES(N) \
570      ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
571       + YYSTACK_GAP_MAXIMUM)
572
573 # define YYCOPY_NEEDED 1
574
575 /* Relocate STACK from its old location to the new one.  The
576    local variables YYSIZE and YYSTACKSIZE give the old and new number of
577    elements in the stack, and YYPTR gives the new location of the
578    stack.  Advance YYPTR to a properly aligned location for the next
579    stack.  */
580 # define YYSTACK_RELOCATE(Stack_alloc, Stack)                           \
581     do                                                                  \
582       {                                                                 \
583         YYSIZE_T yynewbytes;                                            \
584         YYCOPY (&yyptr->Stack_alloc, Stack, yysize);                    \
585         Stack = &yyptr->Stack_alloc;                                    \
586         yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
587         yyptr += yynewbytes / sizeof (*yyptr);                          \
588       }                                                                 \
589     while (YYID (0))
590
591 #endif
592
593 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
594 /* Copy COUNT objects from SRC to DST.  The source and destination do
595    not overlap.  */
596 # ifndef YYCOPY
597 #  if defined __GNUC__ && 1 < __GNUC__
598 #   define YYCOPY(Dst, Src, Count) \
599       __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src)))
600 #  else
601 #   define YYCOPY(Dst, Src, Count)              \
602       do                                        \
603         {                                       \
604           YYSIZE_T yyi;                         \
605           for (yyi = 0; yyi < (Count); yyi++)   \
606             (Dst)[yyi] = (Src)[yyi];            \
607         }                                       \
608       while (YYID (0))
609 #  endif
610 # endif
611 #endif /* !YYCOPY_NEEDED */
612
613 /* YYFINAL -- State number of the termination state.  */
614 #define YYFINAL  12
615 /* YYLAST -- Last index in YYTABLE.  */
616 #define YYLAST   37
617
618 /* YYNTOKENS -- Number of terminals.  */
619 #define YYNTOKENS  18
620 /* YYNNTS -- Number of nonterminals.  */
621 #define YYNNTS  14
622 /* YYNRULES -- Number of rules.  */
623 #define YYNRULES  32
624 /* YYNRULES -- Number of states.  */
625 #define YYNSTATES  49
626
627 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX.  */
628 #define YYUNDEFTOK  2
629 #define YYMAXUTOK   267
630
631 #define YYTRANSLATE(YYX)                                                \
632   ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
633
634 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX.  */
635 static const yytype_uint8 yytranslate[] =
636 {
637        0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
638       13,     2,     2,     2,     2,     2,     2,     2,     2,     2,
639        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
640        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
641        2,     2,     2,     2,    15,     2,     2,     2,    16,    17,
642        2,     2,     2,     2,     2,     2,     2,     2,    14,     2,
643        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
644        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
645        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
646        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
647        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
648        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
649        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
650        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
651        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
652        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
653        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
654        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
655        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
656        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
657        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
658        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
659        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
660        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
661        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
662        2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
663        5,     6,     7,     8,     9,    10,    11,    12
664 };
665
666 #if YYDEBUG
667 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
668    YYRHS.  */
669 static const yytype_uint8 yyprhs[] =
670 {
671        0,     0,     3,     8,    12,    14,    18,    21,    24,    28,
672       29,    33,    35,    42,    43,    45,    46,    50,    52,    55,
673       57,    59,    61,    63,    66,    67,    71,    73,    76,    78,
674       80,    82,    84
675 };
676
677 /* YYRHS -- A `-1'-separated list of the rules' RHS.  */
678 static const yytype_int8 yyrhs[] =
679 {
680       19,     0,    -1,    20,     9,    13,    22,    -1,    20,    13,
681       21,    -1,    21,    -1,     3,    10,     8,    -1,     4,    10,
682       -1,     5,    10,    -1,     6,    10,    10,    -1,    -1,    22,
683       13,    23,    -1,    23,    -1,    25,    14,    24,     7,    24,
684       28,    -1,    -1,    10,    -1,    -1,    25,    15,    26,    -1,
685       26,    -1,    26,    27,    -1,    27,    -1,    16,    -1,    17,
686       -1,    10,    -1,    12,    29,    -1,    -1,    29,    15,    30,
687       -1,    30,    -1,    30,    31,    -1,    31,    -1,    10,    -1,
688       11,    -1,     7,    -1,    14,    -1
689 };
690
691 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
692 static const yytype_uint16 yyrline[] =
693 {
694        0,   247,   247,   257,   258,   261,   263,   265,   267,   279,
695      282,   283,   286,   369,   372,   388,   391,   401,   408,   416,
696      420,   427,   434,   456,   459,   462,   472,   480,   488,   491,
697      523,   532,   539
698 };
699 #endif
700
701 #if YYDEBUG || YYERROR_VERBOSE || 0
702 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
703    First, the terminals, then, starting at YYNTOKENS, nonterminals.  */
704 static const char *const yytname[] =
705 {
706   "$end", "error", "$undefined", "kMASK", "kPREFIX", "kSUFFIX",
707   "kSYNONYM", "kID", "kNUMBER", "kPERCPERC", "kBITFIELD", "kCHAR",
708   "kSPACE", "'\\n'", "':'", "','", "'0'", "'1'", "$accept", "spec",
709   "masks", "mask", "instrs", "instr", "bitfieldopt", "bytes", "byte",
710   "bit", "optargs", "args", "arg", "argcomp", YY_NULL
711 };
712 #endif
713
714 # ifdef YYPRINT
715 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
716    token YYLEX-NUM.  */
717 static const yytype_uint16 yytoknum[] =
718 {
719        0,   256,   257,   258,   259,   260,   261,   262,   263,   264,
720      265,   266,   267,    10,    58,    44,    48,    49
721 };
722 # endif
723
724 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
725 static const yytype_uint8 yyr1[] =
726 {
727        0,    18,    19,    20,    20,    21,    21,    21,    21,    21,
728       22,    22,    23,    23,    24,    24,    25,    25,    26,    26,
729       27,    27,    27,    28,    28,    29,    29,    30,    30,    31,
730       31,    31,    31
731 };
732
733 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.  */
734 static const yytype_uint8 yyr2[] =
735 {
736        0,     2,     4,     3,     1,     3,     2,     2,     3,     0,
737        3,     1,     6,     0,     1,     0,     3,     1,     2,     1,
738        1,     1,     1,     2,     0,     3,     1,     2,     1,     1,
739        1,     1,     1
740 };
741
742 /* YYDEFACT[STATE-NAME] -- Default reduction number in state STATE-NUM.
743    Performed when YYTABLE doesn't specify something else to do.  Zero
744    means the default is an error.  */
745 static const yytype_uint8 yydefact[] =
746 {
747        9,     0,     0,     0,     0,     0,     0,     4,     0,     6,
748        7,     0,     1,     0,     9,     5,     8,    13,     3,    22,
749       20,    21,     2,    11,     0,    17,    19,    13,    15,     0,
750       18,    10,    14,     0,    16,    15,    24,     0,    12,    31,
751       29,    30,    32,    23,    26,    28,     0,    27,    25
752 };
753
754 /* YYDEFGOTO[NTERM-NUM].  */
755 static const yytype_int8 yydefgoto[] =
756 {
757       -1,     5,     6,     7,    22,    23,    33,    24,    25,    26,
758       38,    43,    44,    45
759 };
760
761 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
762    STATE-NUM.  */
763 #define YYPACT_NINF -35
764 static const yytype_int8 yypact[] =
765 {
766       12,     9,    10,    11,    13,    22,    -2,   -35,    16,   -35,
767      -35,    15,   -35,    14,    12,   -35,   -35,    -4,   -35,   -35,
768      -35,   -35,    17,   -35,   -12,    -4,   -35,    -4,    18,    -4,
769      -35,   -35,   -35,    19,    -4,    18,    20,    -6,   -35,   -35,
770      -35,   -35,   -35,    21,    -6,   -35,    -6,   -35,    -6
771 };
772
773 /* YYPGOTO[NTERM-NUM].  */
774 static const yytype_int8 yypgoto[] =
775 {
776      -35,   -35,   -35,    23,   -35,     2,    -1,   -35,     4,   -25,
777      -35,   -35,   -15,   -34
778 };
779
780 /* YYTABLE[YYPACT[STATE-NUM]].  What to do in state STATE-NUM.  If
781    positive, shift that token.  If negative, reduce the rule which
782    number is the opposite.  If YYTABLE_NINF, syntax error.  */
783 #define YYTABLE_NINF -1
784 static const yytype_uint8 yytable[] =
785 {
786       30,    39,    28,    29,    40,    41,    19,    13,    42,    30,
787       47,    14,    20,    21,    47,     1,     2,     3,     4,     8,
788        9,    10,    12,    11,    15,    16,    35,    17,    32,    31,
789       27,    48,    37,    34,    36,     0,    46,    18
790 };
791
792 #define yypact_value_is_default(Yystate) \
793   (!!((Yystate) == (-35)))
794
795 #define yytable_value_is_error(Yytable_value) \
796   YYID (0)
797
798 static const yytype_int8 yycheck[] =
799 {
800       25,     7,    14,    15,    10,    11,    10,     9,    14,    34,
801       44,    13,    16,    17,    48,     3,     4,     5,     6,    10,
802       10,    10,     0,    10,     8,    10,     7,    13,    10,    27,
803       13,    46,    12,    29,    35,    -1,    15,    14
804 };
805
806 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
807    symbol of state STATE-NUM.  */
808 static const yytype_uint8 yystos[] =
809 {
810        0,     3,     4,     5,     6,    19,    20,    21,    10,    10,
811       10,    10,     0,     9,    13,     8,    10,    13,    21,    10,
812       16,    17,    22,    23,    25,    26,    27,    13,    14,    15,
813       27,    23,    10,    24,    26,     7,    24,    12,    28,     7,
814       10,    11,    14,    29,    30,    31,    15,    31,    30
815 };
816
817 #define yyerrok         (yyerrstatus = 0)
818 #define yyclearin       (yychar = YYEMPTY)
819 #define YYEMPTY         (-2)
820 #define YYEOF           0
821
822 #define YYACCEPT        goto yyacceptlab
823 #define YYABORT         goto yyabortlab
824 #define YYERROR         goto yyerrorlab
825
826
827 /* Like YYERROR except do call yyerror.  This remains here temporarily
828    to ease the transition to the new meaning of YYERROR, for GCC.
829    Once GCC version 2 has supplanted version 1, this can go.  However,
830    YYFAIL appears to be in use.  Nevertheless, it is formally deprecated
831    in Bison 2.4.2's NEWS entry, where a plan to phase it out is
832    discussed.  */
833
834 #define YYFAIL          goto yyerrlab
835 #if defined YYFAIL
836   /* This is here to suppress warnings from the GCC cpp's
837      -Wunused-macros.  Normally we don't worry about that warning, but
838      some users do, and we want to make it easy for users to remove
839      YYFAIL uses, which will produce warnings from Bison 2.5.  */
840 #endif
841
842 #define YYRECOVERING()  (!!yyerrstatus)
843
844 #define YYBACKUP(Token, Value)                                  \
845 do                                                              \
846   if (yychar == YYEMPTY)                                        \
847     {                                                           \
848       yychar = (Token);                                         \
849       yylval = (Value);                                         \
850       YYPOPSTACK (yylen);                                       \
851       yystate = *yyssp;                                         \
852       goto yybackup;                                            \
853     }                                                           \
854   else                                                          \
855     {                                                           \
856       yyerror (YY_("syntax error: cannot back up")); \
857       YYERROR;                                                  \
858     }                                                           \
859 while (YYID (0))
860
861 /* Error token number */
862 #define YYTERROR        1
863 #define YYERRCODE       256
864
865
866 /* This macro is provided for backward compatibility. */
867 #ifndef YY_LOCATION_PRINT
868 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
869 #endif
870
871
872 /* YYLEX -- calling `yylex' with the right arguments.  */
873 #ifdef YYLEX_PARAM
874 # define YYLEX yylex (YYLEX_PARAM)
875 #else
876 # define YYLEX yylex ()
877 #endif
878
879 /* Enable debugging if requested.  */
880 #if YYDEBUG
881
882 # ifndef YYFPRINTF
883 #  include <stdio.h> /* INFRINGES ON USER NAME SPACE */
884 #  define YYFPRINTF fprintf
885 # endif
886
887 # define YYDPRINTF(Args)                        \
888 do {                                            \
889   if (yydebug)                                  \
890     YYFPRINTF Args;                             \
891 } while (YYID (0))
892
893 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
894 do {                                                                      \
895   if (yydebug)                                                            \
896     {                                                                     \
897       YYFPRINTF (stderr, "%s ", Title);                                   \
898       yy_symbol_print (stderr,                                            \
899                   Type, Value); \
900       YYFPRINTF (stderr, "\n");                                           \
901     }                                                                     \
902 } while (YYID (0))
903
904
905 /*--------------------------------.
906 | Print this symbol on YYOUTPUT.  |
907 `--------------------------------*/
908
909 /*ARGSUSED*/
910 #if (defined __STDC__ || defined __C99__FUNC__ \
911      || defined __cplusplus || defined _MSC_VER)
912 static void
913 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
914 #else
915 static void
916 yy_symbol_value_print (yyoutput, yytype, yyvaluep)
917     FILE *yyoutput;
918     int yytype;
919     YYSTYPE const * const yyvaluep;
920 #endif
921 {
922   FILE *yyo = yyoutput;
923   YYUSE (yyo);
924   if (!yyvaluep)
925     return;
926 # ifdef YYPRINT
927   if (yytype < YYNTOKENS)
928     YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
929 # else
930   YYUSE (yyoutput);
931 # endif
932   switch (yytype)
933     {
934       default:
935         break;
936     }
937 }
938
939
940 /*--------------------------------.
941 | Print this symbol on YYOUTPUT.  |
942 `--------------------------------*/
943
944 #if (defined __STDC__ || defined __C99__FUNC__ \
945      || defined __cplusplus || defined _MSC_VER)
946 static void
947 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
948 #else
949 static void
950 yy_symbol_print (yyoutput, yytype, yyvaluep)
951     FILE *yyoutput;
952     int yytype;
953     YYSTYPE const * const yyvaluep;
954 #endif
955 {
956   if (yytype < YYNTOKENS)
957     YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
958   else
959     YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
960
961   yy_symbol_value_print (yyoutput, yytype, yyvaluep);
962   YYFPRINTF (yyoutput, ")");
963 }
964
965 /*------------------------------------------------------------------.
966 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
967 | TOP (included).                                                   |
968 `------------------------------------------------------------------*/
969
970 #if (defined __STDC__ || defined __C99__FUNC__ \
971      || defined __cplusplus || defined _MSC_VER)
972 static void
973 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
974 #else
975 static void
976 yy_stack_print (yybottom, yytop)
977     yytype_int16 *yybottom;
978     yytype_int16 *yytop;
979 #endif
980 {
981   YYFPRINTF (stderr, "Stack now");
982   for (; yybottom <= yytop; yybottom++)
983     {
984       int yybot = *yybottom;
985       YYFPRINTF (stderr, " %d", yybot);
986     }
987   YYFPRINTF (stderr, "\n");
988 }
989
990 # define YY_STACK_PRINT(Bottom, Top)                            \
991 do {                                                            \
992   if (yydebug)                                                  \
993     yy_stack_print ((Bottom), (Top));                           \
994 } while (YYID (0))
995
996
997 /*------------------------------------------------.
998 | Report that the YYRULE is going to be reduced.  |
999 `------------------------------------------------*/
1000
1001 #if (defined __STDC__ || defined __C99__FUNC__ \
1002      || defined __cplusplus || defined _MSC_VER)
1003 static void
1004 yy_reduce_print (YYSTYPE *yyvsp, int yyrule)
1005 #else
1006 static void
1007 yy_reduce_print (yyvsp, yyrule)
1008     YYSTYPE *yyvsp;
1009     int yyrule;
1010 #endif
1011 {
1012   int yynrhs = yyr2[yyrule];
1013   int yyi;
1014   unsigned long int yylno = yyrline[yyrule];
1015   YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
1016              yyrule - 1, yylno);
1017   /* The symbols being reduced.  */
1018   for (yyi = 0; yyi < yynrhs; yyi++)
1019     {
1020       YYFPRINTF (stderr, "   $%d = ", yyi + 1);
1021       yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
1022                        &(yyvsp[(yyi + 1) - (yynrhs)])
1023                                        );
1024       YYFPRINTF (stderr, "\n");
1025     }
1026 }
1027
1028 # define YY_REDUCE_PRINT(Rule)          \
1029 do {                                    \
1030   if (yydebug)                          \
1031     yy_reduce_print (yyvsp, Rule); \
1032 } while (YYID (0))
1033
1034 /* Nonzero means print parse trace.  It is left uninitialized so that
1035    multiple parsers can coexist.  */
1036 int yydebug;
1037 #else /* !YYDEBUG */
1038 # define YYDPRINTF(Args)
1039 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
1040 # define YY_STACK_PRINT(Bottom, Top)
1041 # define YY_REDUCE_PRINT(Rule)
1042 #endif /* !YYDEBUG */
1043
1044
1045 /* YYINITDEPTH -- initial size of the parser's stacks.  */
1046 #ifndef YYINITDEPTH
1047 # define YYINITDEPTH 200
1048 #endif
1049
1050 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1051    if the built-in stack extension method is used).
1052
1053    Do not make this value too large; the results are undefined if
1054    YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1055    evaluated with infinite-precision integer arithmetic.  */
1056
1057 #ifndef YYMAXDEPTH
1058 # define YYMAXDEPTH 10000
1059 #endif
1060
1061
1062 #if YYERROR_VERBOSE
1063
1064 # ifndef yystrlen
1065 #  if defined __GLIBC__ && defined _STRING_H
1066 #   define yystrlen strlen
1067 #  else
1068 /* Return the length of YYSTR.  */
1069 #if (defined __STDC__ || defined __C99__FUNC__ \
1070      || defined __cplusplus || defined _MSC_VER)
1071 static YYSIZE_T
1072 yystrlen (const char *yystr)
1073 #else
1074 static YYSIZE_T
1075 yystrlen (yystr)
1076     const char *yystr;
1077 #endif
1078 {
1079   YYSIZE_T yylen;
1080   for (yylen = 0; yystr[yylen]; yylen++)
1081     continue;
1082   return yylen;
1083 }
1084 #  endif
1085 # endif
1086
1087 # ifndef yystpcpy
1088 #  if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1089 #   define yystpcpy stpcpy
1090 #  else
1091 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1092    YYDEST.  */
1093 #if (defined __STDC__ || defined __C99__FUNC__ \
1094      || defined __cplusplus || defined _MSC_VER)
1095 static char *
1096 yystpcpy (char *yydest, const char *yysrc)
1097 #else
1098 static char *
1099 yystpcpy (yydest, yysrc)
1100     char *yydest;
1101     const char *yysrc;
1102 #endif
1103 {
1104   char *yyd = yydest;
1105   const char *yys = yysrc;
1106
1107   while ((*yyd++ = *yys++) != '\0')
1108     continue;
1109
1110   return yyd - 1;
1111 }
1112 #  endif
1113 # endif
1114
1115 # ifndef yytnamerr
1116 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1117    quotes and backslashes, so that it's suitable for yyerror.  The
1118    heuristic is that double-quoting is unnecessary unless the string
1119    contains an apostrophe, a comma, or backslash (other than
1120    backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
1121    null, do not copy; instead, return the length of what the result
1122    would have been.  */
1123 static YYSIZE_T
1124 yytnamerr (char *yyres, const char *yystr)
1125 {
1126   if (*yystr == '"')
1127     {
1128       YYSIZE_T yyn = 0;
1129       char const *yyp = yystr;
1130
1131       for (;;)
1132         switch (*++yyp)
1133           {
1134           case '\'':
1135           case ',':
1136             goto do_not_strip_quotes;
1137
1138           case '\\':
1139             if (*++yyp != '\\')
1140               goto do_not_strip_quotes;
1141             /* Fall through.  */
1142           default:
1143             if (yyres)
1144               yyres[yyn] = *yyp;
1145             yyn++;
1146             break;
1147
1148           case '"':
1149             if (yyres)
1150               yyres[yyn] = '\0';
1151             return yyn;
1152           }
1153     do_not_strip_quotes: ;
1154     }
1155
1156   if (! yyres)
1157     return yystrlen (yystr);
1158
1159   return yystpcpy (yyres, yystr) - yyres;
1160 }
1161 # endif
1162
1163 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1164    about the unexpected token YYTOKEN for the state stack whose top is
1165    YYSSP.
1166
1167    Return 0 if *YYMSG was successfully written.  Return 1 if *YYMSG is
1168    not large enough to hold the message.  In that case, also set
1169    *YYMSG_ALLOC to the required number of bytes.  Return 2 if the
1170    required number of bytes is too large to store.  */
1171 static int
1172 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1173                 yytype_int16 *yyssp, int yytoken)
1174 {
1175   YYSIZE_T yysize0 = yytnamerr (YY_NULL, yytname[yytoken]);
1176   YYSIZE_T yysize = yysize0;
1177   enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1178   /* Internationalized format string. */
1179   const char *yyformat = YY_NULL;
1180   /* Arguments of yyformat. */
1181   char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1182   /* Number of reported tokens (one for the "unexpected", one per
1183      "expected"). */
1184   int yycount = 0;
1185
1186   /* There are many possibilities here to consider:
1187      - Assume YYFAIL is not used.  It's too flawed to consider.  See
1188        <http://lists.gnu.org/archive/html/bison-patches/2009-12/msg00024.html>
1189        for details.  YYERROR is fine as it does not invoke this
1190        function.
1191      - If this state is a consistent state with a default action, then
1192        the only way this function was invoked is if the default action
1193        is an error action.  In that case, don't check for expected
1194        tokens because there are none.
1195      - The only way there can be no lookahead present (in yychar) is if
1196        this state is a consistent state with a default action.  Thus,
1197        detecting the absence of a lookahead is sufficient to determine
1198        that there is no unexpected or expected token to report.  In that
1199        case, just report a simple "syntax error".
1200      - Don't assume there isn't a lookahead just because this state is a
1201        consistent state with a default action.  There might have been a
1202        previous inconsistent state, consistent state with a non-default
1203        action, or user semantic action that manipulated yychar.
1204      - Of course, the expected token list depends on states to have
1205        correct lookahead information, and it depends on the parser not
1206        to perform extra reductions after fetching a lookahead from the
1207        scanner and before detecting a syntax error.  Thus, state merging
1208        (from LALR or IELR) and default reductions corrupt the expected
1209        token list.  However, the list is correct for canonical LR with
1210        one exception: it will still contain any token that will not be
1211        accepted due to an error action in a later state.
1212   */
1213   if (yytoken != YYEMPTY)
1214     {
1215       int yyn = yypact[*yyssp];
1216       yyarg[yycount++] = yytname[yytoken];
1217       if (!yypact_value_is_default (yyn))
1218         {
1219           /* Start YYX at -YYN if negative to avoid negative indexes in
1220              YYCHECK.  In other words, skip the first -YYN actions for
1221              this state because they are default actions.  */
1222           int yyxbegin = yyn < 0 ? -yyn : 0;
1223           /* Stay within bounds of both yycheck and yytname.  */
1224           int yychecklim = YYLAST - yyn + 1;
1225           int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1226           int yyx;
1227
1228           for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1229             if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1230                 && !yytable_value_is_error (yytable[yyx + yyn]))
1231               {
1232                 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1233                   {
1234                     yycount = 1;
1235                     yysize = yysize0;
1236                     break;
1237                   }
1238                 yyarg[yycount++] = yytname[yyx];
1239                 {
1240                   YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULL, yytname[yyx]);
1241                   if (! (yysize <= yysize1
1242                          && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1243                     return 2;
1244                   yysize = yysize1;
1245                 }
1246               }
1247         }
1248     }
1249
1250   switch (yycount)
1251     {
1252 # define YYCASE_(N, S)                      \
1253       case N:                               \
1254         yyformat = S;                       \
1255       break
1256       YYCASE_(0, YY_("syntax error"));
1257       YYCASE_(1, YY_("syntax error, unexpected %s"));
1258       YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1259       YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1260       YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1261       YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1262 # undef YYCASE_
1263     }
1264
1265   {
1266     YYSIZE_T yysize1 = yysize + yystrlen (yyformat);
1267     if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1268       return 2;
1269     yysize = yysize1;
1270   }
1271
1272   if (*yymsg_alloc < yysize)
1273     {
1274       *yymsg_alloc = 2 * yysize;
1275       if (! (yysize <= *yymsg_alloc
1276              && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1277         *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1278       return 1;
1279     }
1280
1281   /* Avoid sprintf, as that infringes on the user's name space.
1282      Don't have undefined behavior even if the translation
1283      produced a string with the wrong number of "%s"s.  */
1284   {
1285     char *yyp = *yymsg;
1286     int yyi = 0;
1287     while ((*yyp = *yyformat) != '\0')
1288       if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1289         {
1290           yyp += yytnamerr (yyp, yyarg[yyi++]);
1291           yyformat += 2;
1292         }
1293       else
1294         {
1295           yyp++;
1296           yyformat++;
1297         }
1298   }
1299   return 0;
1300 }
1301 #endif /* YYERROR_VERBOSE */
1302
1303 /*-----------------------------------------------.
1304 | Release the memory associated to this symbol.  |
1305 `-----------------------------------------------*/
1306
1307 /*ARGSUSED*/
1308 #if (defined __STDC__ || defined __C99__FUNC__ \
1309      || defined __cplusplus || defined _MSC_VER)
1310 static void
1311 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1312 #else
1313 static void
1314 yydestruct (yymsg, yytype, yyvaluep)
1315     const char *yymsg;
1316     int yytype;
1317     YYSTYPE *yyvaluep;
1318 #endif
1319 {
1320   YYUSE (yyvaluep);
1321
1322   if (!yymsg)
1323     yymsg = "Deleting";
1324   YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1325
1326   switch (yytype)
1327     {
1328
1329       default:
1330         break;
1331     }
1332 }
1333
1334
1335
1336
1337 /* The lookahead symbol.  */
1338 int yychar;
1339
1340
1341 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1342 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1343 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
1344 #endif
1345 #ifndef YY_INITIAL_VALUE
1346 # define YY_INITIAL_VALUE(Value) /* Nothing. */
1347 #endif
1348
1349 /* The semantic value of the lookahead symbol.  */
1350 YYSTYPE yylval YY_INITIAL_VALUE(yyval_default);
1351
1352 /* Number of syntax errors so far.  */
1353 int yynerrs;
1354
1355
1356 /*----------.
1357 | yyparse.  |
1358 `----------*/
1359
1360 #ifdef YYPARSE_PARAM
1361 #if (defined __STDC__ || defined __C99__FUNC__ \
1362      || defined __cplusplus || defined _MSC_VER)
1363 int
1364 yyparse (void *YYPARSE_PARAM)
1365 #else
1366 int
1367 yyparse (YYPARSE_PARAM)
1368     void *YYPARSE_PARAM;
1369 #endif
1370 #else /* ! YYPARSE_PARAM */
1371 #if (defined __STDC__ || defined __C99__FUNC__ \
1372      || defined __cplusplus || defined _MSC_VER)
1373 int
1374 yyparse (void)
1375 #else
1376 int
1377 yyparse ()
1378
1379 #endif
1380 #endif
1381 {
1382     int yystate;
1383     /* Number of tokens to shift before error messages enabled.  */
1384     int yyerrstatus;
1385
1386     /* The stacks and their tools:
1387        `yyss': related to states.
1388        `yyvs': related to semantic values.
1389
1390        Refer to the stacks through separate pointers, to allow yyoverflow
1391        to reallocate them elsewhere.  */
1392
1393     /* The state stack.  */
1394     yytype_int16 yyssa[YYINITDEPTH];
1395     yytype_int16 *yyss;
1396     yytype_int16 *yyssp;
1397
1398     /* The semantic value stack.  */
1399     YYSTYPE yyvsa[YYINITDEPTH];
1400     YYSTYPE *yyvs;
1401     YYSTYPE *yyvsp;
1402
1403     YYSIZE_T yystacksize;
1404
1405   int yyn;
1406   int yyresult;
1407   /* Lookahead token as an internal (translated) token number.  */
1408   int yytoken = 0;
1409   /* The variables used to return semantic value and location from the
1410      action routines.  */
1411   YYSTYPE yyval;
1412
1413 #if YYERROR_VERBOSE
1414   /* Buffer for error messages, and its allocated size.  */
1415   char yymsgbuf[128];
1416   char *yymsg = yymsgbuf;
1417   YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1418 #endif
1419
1420 #define YYPOPSTACK(N)   (yyvsp -= (N), yyssp -= (N))
1421
1422   /* The number of symbols on the RHS of the reduced rule.
1423      Keep to zero when no symbol should be popped.  */
1424   int yylen = 0;
1425
1426   yyssp = yyss = yyssa;
1427   yyvsp = yyvs = yyvsa;
1428   yystacksize = YYINITDEPTH;
1429
1430   YYDPRINTF ((stderr, "Starting parse\n"));
1431
1432   yystate = 0;
1433   yyerrstatus = 0;
1434   yynerrs = 0;
1435   yychar = YYEMPTY; /* Cause a token to be read.  */
1436   goto yysetstate;
1437
1438 /*------------------------------------------------------------.
1439 | yynewstate -- Push a new state, which is found in yystate.  |
1440 `------------------------------------------------------------*/
1441  yynewstate:
1442   /* In all cases, when you get here, the value and location stacks
1443      have just been pushed.  So pushing a state here evens the stacks.  */
1444   yyssp++;
1445
1446  yysetstate:
1447   *yyssp = yystate;
1448
1449   if (yyss + yystacksize - 1 <= yyssp)
1450     {
1451       /* Get the current used size of the three stacks, in elements.  */
1452       YYSIZE_T yysize = yyssp - yyss + 1;
1453
1454 #ifdef yyoverflow
1455       {
1456         /* Give user a chance to reallocate the stack.  Use copies of
1457            these so that the &'s don't force the real ones into
1458            memory.  */
1459         YYSTYPE *yyvs1 = yyvs;
1460         yytype_int16 *yyss1 = yyss;
1461
1462         /* Each stack pointer address is followed by the size of the
1463            data in use in that stack, in bytes.  This used to be a
1464            conditional around just the two extra args, but that might
1465            be undefined if yyoverflow is a macro.  */
1466         yyoverflow (YY_("memory exhausted"),
1467                     &yyss1, yysize * sizeof (*yyssp),
1468                     &yyvs1, yysize * sizeof (*yyvsp),
1469                     &yystacksize);
1470
1471         yyss = yyss1;
1472         yyvs = yyvs1;
1473       }
1474 #else /* no yyoverflow */
1475 # ifndef YYSTACK_RELOCATE
1476       goto yyexhaustedlab;
1477 # else
1478       /* Extend the stack our own way.  */
1479       if (YYMAXDEPTH <= yystacksize)
1480         goto yyexhaustedlab;
1481       yystacksize *= 2;
1482       if (YYMAXDEPTH < yystacksize)
1483         yystacksize = YYMAXDEPTH;
1484
1485       {
1486         yytype_int16 *yyss1 = yyss;
1487         union yyalloc *yyptr =
1488           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1489         if (! yyptr)
1490           goto yyexhaustedlab;
1491         YYSTACK_RELOCATE (yyss_alloc, yyss);
1492         YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1493 #  undef YYSTACK_RELOCATE
1494         if (yyss1 != yyssa)
1495           YYSTACK_FREE (yyss1);
1496       }
1497 # endif
1498 #endif /* no yyoverflow */
1499
1500       yyssp = yyss + yysize - 1;
1501       yyvsp = yyvs + yysize - 1;
1502
1503       YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1504                   (unsigned long int) yystacksize));
1505
1506       if (yyss + yystacksize - 1 <= yyssp)
1507         YYABORT;
1508     }
1509
1510   YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1511
1512   if (yystate == YYFINAL)
1513     YYACCEPT;
1514
1515   goto yybackup;
1516
1517 /*-----------.
1518 | yybackup.  |
1519 `-----------*/
1520 yybackup:
1521
1522   /* Do appropriate processing given the current state.  Read a
1523      lookahead token if we need one and don't already have one.  */
1524
1525   /* First try to decide what to do without reference to lookahead token.  */
1526   yyn = yypact[yystate];
1527   if (yypact_value_is_default (yyn))
1528     goto yydefault;
1529
1530   /* Not known => get a lookahead token if don't already have one.  */
1531
1532   /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
1533   if (yychar == YYEMPTY)
1534     {
1535       YYDPRINTF ((stderr, "Reading a token: "));
1536       yychar = YYLEX;
1537     }
1538
1539   if (yychar <= YYEOF)
1540     {
1541       yychar = yytoken = YYEOF;
1542       YYDPRINTF ((stderr, "Now at end of input.\n"));
1543     }
1544   else
1545     {
1546       yytoken = YYTRANSLATE (yychar);
1547       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1548     }
1549
1550   /* If the proper action on seeing token YYTOKEN is to reduce or to
1551      detect an error, take that action.  */
1552   yyn += yytoken;
1553   if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1554     goto yydefault;
1555   yyn = yytable[yyn];
1556   if (yyn <= 0)
1557     {
1558       if (yytable_value_is_error (yyn))
1559         goto yyerrlab;
1560       yyn = -yyn;
1561       goto yyreduce;
1562     }
1563
1564   /* Count tokens shifted since error; after three, turn off error
1565      status.  */
1566   if (yyerrstatus)
1567     yyerrstatus--;
1568
1569   /* Shift the lookahead token.  */
1570   YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1571
1572   /* Discard the shifted token.  */
1573   yychar = YYEMPTY;
1574
1575   yystate = yyn;
1576   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1577   *++yyvsp = yylval;
1578   YY_IGNORE_MAYBE_UNINITIALIZED_END
1579
1580   goto yynewstate;
1581
1582
1583 /*-----------------------------------------------------------.
1584 | yydefault -- do the default action for the current state.  |
1585 `-----------------------------------------------------------*/
1586 yydefault:
1587   yyn = yydefact[yystate];
1588   if (yyn == 0)
1589     goto yyerrlab;
1590   goto yyreduce;
1591
1592
1593 /*-----------------------------.
1594 | yyreduce -- Do a reduction.  |
1595 `-----------------------------*/
1596 yyreduce:
1597   /* yyn is the number of a rule to reduce with.  */
1598   yylen = yyr2[yyn];
1599
1600   /* If YYLEN is nonzero, implement the default value of the action:
1601      `$$ = $1'.
1602
1603      Otherwise, the following line sets YYVAL to garbage.
1604      This behavior is undocumented and Bison
1605      users should not rely upon it.  Assigning to YYVAL
1606      unconditionally makes the parser a bit smaller, and it avoids a
1607      GCC warning that YYVAL may be used uninitialized.  */
1608   yyval = yyvsp[1-yylen];
1609
1610
1611   YY_REDUCE_PRINT (yyn);
1612   switch (yyn)
1613     {
1614         case 2:
1615 /* Line 1792 of yacc.c  */
1616 #line 248 "i386_parse.y"
1617     {
1618                       if (error_message_count != 0)
1619                         error (EXIT_FAILURE, 0,
1620                                "terminated due to previous error");
1621
1622                       instrtable_out ();
1623                     }
1624     break;
1625
1626   case 5:
1627 /* Line 1792 of yacc.c  */
1628 #line 262 "i386_parse.y"
1629     { new_bitfield ((yyvsp[(2) - (3)].str), (yyvsp[(3) - (3)].num)); }
1630     break;
1631
1632   case 6:
1633 /* Line 1792 of yacc.c  */
1634 #line 264 "i386_parse.y"
1635     { new_bitfield ((yyvsp[(2) - (2)].str), -1); }
1636     break;
1637
1638   case 7:
1639 /* Line 1792 of yacc.c  */
1640 #line 266 "i386_parse.y"
1641     { new_bitfield ((yyvsp[(2) - (2)].str), -2); }
1642     break;
1643
1644   case 8:
1645 /* Line 1792 of yacc.c  */
1646 #line 268 "i386_parse.y"
1647     {
1648                       struct synonym *newp = xmalloc (sizeof (*newp));
1649                       newp->from = (yyvsp[(2) - (3)].str);
1650                       newp->to = (yyvsp[(3) - (3)].str);
1651                       if (tfind (newp, &synonyms, compare_syn) != NULL)
1652                         error (0, 0,
1653                                "%d: duplicate definition for synonym '%s'",
1654                                i386_lineno, (yyvsp[(2) - (3)].str));
1655                       else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
1656                         error (EXIT_FAILURE, 0, "tsearch");
1657                     }
1658     break;
1659
1660   case 12:
1661 /* Line 1792 of yacc.c  */
1662 #line 287 "i386_parse.y"
1663     {
1664                       if ((yyvsp[(3) - (6)].field) != NULL && strcmp ((yyvsp[(3) - (6)].field)->name, "RE") != 0
1665                           && strcmp ((yyvsp[(3) - (6)].field)->name, "R") != 0)
1666                         {
1667                           error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
1668                                  i386_lineno - 1);
1669                         }
1670                       if (check_duplicates ((yyvsp[(1) - (6)].bit)) == 0
1671                           && check_argsdef ((yyvsp[(1) - (6)].bit), (yyvsp[(6) - (6)].arg)) == 0
1672                           && check_bitsused ((yyvsp[(1) - (6)].bit), (yyvsp[(5) - (6)].field), (yyvsp[(6) - (6)].arg)) == 0)
1673                         {
1674                           struct instruction *newp = xcalloc (sizeof (*newp),
1675                                                               1);
1676                           if ((yyvsp[(3) - (6)].field) != NULL)
1677                             {
1678                               if (strcmp ((yyvsp[(3) - (6)].field)->name, "RE") == 0)
1679                                 newp->repe = 1;
1680                               else if (strcmp ((yyvsp[(3) - (6)].field)->name, "R") == 0)
1681                                 newp->rep = 1;
1682                             }
1683
1684                           newp->bytes = (yyvsp[(1) - (6)].bit);
1685                           newp->mnemonic = (yyvsp[(4) - (6)].str);
1686                           if (newp->mnemonic != (void *) -1l
1687                               && tfind ((yyvsp[(4) - (6)].str), &mnemonics,
1688                                         (comparison_fn_t) strcmp) == NULL)
1689                             {
1690                               if (tsearch ((yyvsp[(4) - (6)].str), &mnemonics,
1691                                            (comparison_fn_t) strcmp) == NULL)
1692                                 error (EXIT_FAILURE, errno, "tsearch");
1693                               ++nmnemonics;
1694                             }
1695
1696                           if ((yyvsp[(5) - (6)].field) != NULL)
1697                             {
1698                               if (strcmp ((yyvsp[(5) - (6)].field)->name, "w") == 0)
1699                                 newp->suffix = suffix_w;
1700                               else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w0") == 0)
1701                                 newp->suffix = suffix_w0;
1702                               else if (strcmp ((yyvsp[(5) - (6)].field)->name, "tttn") == 0)
1703                                 newp->suffix = suffix_tttn;
1704                               else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w1") == 0)
1705                                 newp->suffix = suffix_w1;
1706                               else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W") == 0)
1707                                 newp->suffix = suffix_W;
1708                               else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W1") == 0)
1709                                 newp->suffix = suffix_W1;
1710                               else if (strcmp ((yyvsp[(5) - (6)].field)->name, "D") == 0)
1711                                 newp->suffix = suffix_D;
1712                               else
1713                                 error (EXIT_FAILURE, 0,
1714                                        "%s: %d: unknown suffix '%s'",
1715                                        infname, i386_lineno - 1, (yyvsp[(5) - (6)].field)->name);
1716
1717                               struct suffix search = { .name = (yyvsp[(5) - (6)].field)->name };
1718                               if (tfind (&search, &suffixes, compare_suf)
1719                                   == NULL)
1720                                 {
1721                                   struct suffix *ns = xmalloc (sizeof (*ns));
1722                                   ns->name = (yyvsp[(5) - (6)].field)->name;
1723                                   ns->idx = ++nsuffixes;
1724                                   if (tsearch (ns, &suffixes, compare_suf)
1725                                       == NULL)
1726                                     error (EXIT_FAILURE, errno, "tsearch");
1727                                 }
1728                             }
1729
1730                           struct argument *args = (yyvsp[(6) - (6)].arg);
1731                           int n = 0;
1732                           while (args != NULL)
1733                             {
1734                               fillin_arg ((yyvsp[(1) - (6)].bit), args->name, newp, n);
1735
1736                               args = args->next;
1737                               ++n;
1738                             }
1739
1740                           newp->next = instructions;
1741                           instructions = newp;
1742                           ++ninstructions;
1743                         }
1744                     }
1745     break;
1746
1747   case 14:
1748 /* Line 1792 of yacc.c  */
1749 #line 373 "i386_parse.y"
1750     {
1751                       struct known_bitfield search;
1752                       search.name = (yyvsp[(1) - (1)].str);
1753                       struct known_bitfield **res;
1754                       res = tfind (&search, &bitfields, bitfield_compare);
1755                       if (res == NULL)
1756                         {
1757                           error (0, 0, "%d: unknown bitfield '%s'",
1758                                  i386_lineno, search.name);
1759                           (yyval.field) = NULL;
1760                         }
1761                       else
1762                         (yyval.field) = *res;
1763                     }
1764     break;
1765
1766   case 15:
1767 /* Line 1792 of yacc.c  */
1768 #line 388 "i386_parse.y"
1769     { (yyval.field) = NULL; }
1770     break;
1771
1772   case 16:
1773 /* Line 1792 of yacc.c  */
1774 #line 392 "i386_parse.y"
1775     {
1776                       check_bits ((yyvsp[(3) - (3)].bit));
1777
1778                       struct bitvalue *runp = (yyvsp[(1) - (3)].bit);
1779                       while (runp->next != NULL)
1780                         runp = runp->next;
1781                       runp->next = (yyvsp[(3) - (3)].bit);
1782                       (yyval.bit) = (yyvsp[(1) - (3)].bit);
1783                     }
1784     break;
1785
1786   case 17:
1787 /* Line 1792 of yacc.c  */
1788 #line 402 "i386_parse.y"
1789     {
1790                       check_bits ((yyvsp[(1) - (1)].bit));
1791                       (yyval.bit) = (yyvsp[(1) - (1)].bit);
1792                     }
1793     break;
1794
1795   case 18:
1796 /* Line 1792 of yacc.c  */
1797 #line 409 "i386_parse.y"
1798     {
1799                       struct bitvalue *runp = (yyvsp[(1) - (2)].bit);
1800                       while (runp->next != NULL)
1801                         runp = runp->next;
1802                       runp->next = (yyvsp[(2) - (2)].bit);
1803                       (yyval.bit) = (yyvsp[(1) - (2)].bit);
1804                     }
1805     break;
1806
1807   case 19:
1808 /* Line 1792 of yacc.c  */
1809 #line 417 "i386_parse.y"
1810     { (yyval.bit) = (yyvsp[(1) - (1)].bit); }
1811     break;
1812
1813   case 20:
1814 /* Line 1792 of yacc.c  */
1815 #line 421 "i386_parse.y"
1816     {
1817                       (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1818                       (yyval.bit)->type = zeroone;
1819                       (yyval.bit)->value = 0;
1820                       (yyval.bit)->next = NULL;
1821                     }
1822     break;
1823
1824   case 21:
1825 /* Line 1792 of yacc.c  */
1826 #line 428 "i386_parse.y"
1827     {
1828                       (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1829                       (yyval.bit)->type = zeroone;
1830                       (yyval.bit)->value = 1;
1831                       (yyval.bit)->next = NULL;
1832                     }
1833     break;
1834
1835   case 22:
1836 /* Line 1792 of yacc.c  */
1837 #line 435 "i386_parse.y"
1838     {
1839                       (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1840                       struct known_bitfield search;
1841                       search.name = (yyvsp[(1) - (1)].str);
1842                       struct known_bitfield **res;
1843                       res = tfind (&search, &bitfields, bitfield_compare);
1844                       if (res == NULL)
1845                         {
1846                           error (0, 0, "%d: unknown bitfield '%s'",
1847                                  i386_lineno, search.name);
1848                           (yyval.bit)->type = failure;
1849                         }
1850                       else
1851                         {
1852                           (yyval.bit)->type = field;
1853                           (yyval.bit)->field = *res;
1854                         }
1855                       (yyval.bit)->next = NULL;
1856                     }
1857     break;
1858
1859   case 23:
1860 /* Line 1792 of yacc.c  */
1861 #line 457 "i386_parse.y"
1862     { (yyval.arg) = (yyvsp[(2) - (2)].arg); }
1863     break;
1864
1865   case 24:
1866 /* Line 1792 of yacc.c  */
1867 #line 459 "i386_parse.y"
1868     { (yyval.arg) = NULL; }
1869     break;
1870
1871   case 25:
1872 /* Line 1792 of yacc.c  */
1873 #line 463 "i386_parse.y"
1874     {
1875                       struct argument *runp = (yyvsp[(1) - (3)].arg);
1876                       while (runp->next != NULL)
1877                         runp = runp->next;
1878                       runp->next = xmalloc (sizeof (struct argument));
1879                       runp->next->name = combine ((yyvsp[(3) - (3)].name));
1880                       runp->next->next = NULL;
1881                       (yyval.arg) = (yyvsp[(1) - (3)].arg);
1882                     }
1883     break;
1884
1885   case 26:
1886 /* Line 1792 of yacc.c  */
1887 #line 473 "i386_parse.y"
1888     {
1889                       (yyval.arg) = xmalloc (sizeof (struct argument));
1890                       (yyval.arg)->name = combine ((yyvsp[(1) - (1)].name));
1891                       (yyval.arg)->next = NULL;
1892                     }
1893     break;
1894
1895   case 27:
1896 /* Line 1792 of yacc.c  */
1897 #line 481 "i386_parse.y"
1898     {
1899                       struct argname *runp = (yyvsp[(1) - (2)].name);
1900                       while (runp->next != NULL)
1901                         runp = runp->next;
1902                       runp->next = (yyvsp[(2) - (2)].name);
1903                       (yyval.name) = (yyvsp[(1) - (2)].name);
1904                     }
1905     break;
1906
1907   case 28:
1908 /* Line 1792 of yacc.c  */
1909 #line 489 "i386_parse.y"
1910     { (yyval.name) = (yyvsp[(1) - (1)].name); }
1911     break;
1912
1913   case 29:
1914 /* Line 1792 of yacc.c  */
1915 #line 492 "i386_parse.y"
1916     {
1917                       (yyval.name) = xmalloc (sizeof (struct argname));
1918                       (yyval.name)->type = nfield;
1919                       (yyval.name)->next = NULL;
1920
1921                       struct known_bitfield search;
1922                       search.name = (yyvsp[(1) - (1)].str);
1923                       struct known_bitfield **res;
1924                       res = tfind (&search, &bitfields, bitfield_compare);
1925                       if (res == NULL)
1926                         {
1927                           if (strcmp ((yyvsp[(1) - (1)].str), "ax") == 0)
1928                             (yyval.name)->field = &ax_reg;
1929                           else if (strcmp ((yyvsp[(1) - (1)].str), "dx") == 0)
1930                             (yyval.name)->field = &dx_reg;
1931                           else if (strcmp ((yyvsp[(1) - (1)].str), "es_di") == 0)
1932                             (yyval.name)->field = &di_reg;
1933                           else if (strcmp ((yyvsp[(1) - (1)].str), "ds_si") == 0)
1934                             (yyval.name)->field = &si_reg;
1935                           else if (strcmp ((yyvsp[(1) - (1)].str), "ds_bx") == 0)
1936                             (yyval.name)->field = &bx_reg;
1937                           else
1938                             {
1939                               error (0, 0, "%d: unknown bitfield '%s'",
1940                                      i386_lineno, search.name);
1941                               (yyval.name)->field = NULL;
1942                             }
1943                         }
1944                       else
1945                         (yyval.name)->field = *res;
1946                     }
1947     break;
1948
1949   case 30:
1950 /* Line 1792 of yacc.c  */
1951 #line 524 "i386_parse.y"
1952     {
1953                       (yyval.name) = xmalloc (sizeof (struct argname));
1954                       (yyval.name)->type = string;
1955                       (yyval.name)->next = NULL;
1956                       (yyval.name)->str = xmalloc (2);
1957                       (yyval.name)->str[0] = (yyvsp[(1) - (1)].ch);
1958                       (yyval.name)->str[1] = '\0';
1959                     }
1960     break;
1961
1962   case 31:
1963 /* Line 1792 of yacc.c  */
1964 #line 533 "i386_parse.y"
1965     {
1966                       (yyval.name) = xmalloc (sizeof (struct argname));
1967                       (yyval.name)->type = string;
1968                       (yyval.name)->next = NULL;
1969                       (yyval.name)->str = (yyvsp[(1) - (1)].str);
1970                     }
1971     break;
1972
1973   case 32:
1974 /* Line 1792 of yacc.c  */
1975 #line 540 "i386_parse.y"
1976     {
1977                       (yyval.name) = xmalloc (sizeof (struct argname));
1978                       (yyval.name)->type = string;
1979                       (yyval.name)->next = NULL;
1980                       (yyval.name)->str = xmalloc (2);
1981                       (yyval.name)->str[0] = ':';
1982                       (yyval.name)->str[1] = '\0';
1983                     }
1984     break;
1985
1986
1987 /* Line 1792 of yacc.c  */
1988 #line 1989 "i386_parse.c"
1989       default: break;
1990     }
1991   /* User semantic actions sometimes alter yychar, and that requires
1992      that yytoken be updated with the new translation.  We take the
1993      approach of translating immediately before every use of yytoken.
1994      One alternative is translating here after every semantic action,
1995      but that translation would be missed if the semantic action invokes
1996      YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
1997      if it invokes YYBACKUP.  In the case of YYABORT or YYACCEPT, an
1998      incorrect destructor might then be invoked immediately.  In the
1999      case of YYERROR or YYBACKUP, subsequent parser actions might lead
2000      to an incorrect destructor call or verbose syntax error message
2001      before the lookahead is translated.  */
2002   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
2003
2004   YYPOPSTACK (yylen);
2005   yylen = 0;
2006   YY_STACK_PRINT (yyss, yyssp);
2007
2008   *++yyvsp = yyval;
2009
2010   /* Now `shift' the result of the reduction.  Determine what state
2011      that goes to, based on the state we popped back to and the rule
2012      number reduced by.  */
2013
2014   yyn = yyr1[yyn];
2015
2016   yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
2017   if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
2018     yystate = yytable[yystate];
2019   else
2020     yystate = yydefgoto[yyn - YYNTOKENS];
2021
2022   goto yynewstate;
2023
2024
2025 /*------------------------------------.
2026 | yyerrlab -- here on detecting error |
2027 `------------------------------------*/
2028 yyerrlab:
2029   /* Make sure we have latest lookahead translation.  See comments at
2030      user semantic actions for why this is necessary.  */
2031   yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
2032
2033   /* If not already recovering from an error, report this error.  */
2034   if (!yyerrstatus)
2035     {
2036       ++yynerrs;
2037 #if ! YYERROR_VERBOSE
2038       yyerror (YY_("syntax error"));
2039 #else
2040 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
2041                                         yyssp, yytoken)
2042       {
2043         char const *yymsgp = YY_("syntax error");
2044         int yysyntax_error_status;
2045         yysyntax_error_status = YYSYNTAX_ERROR;
2046         if (yysyntax_error_status == 0)
2047           yymsgp = yymsg;
2048         else if (yysyntax_error_status == 1)
2049           {
2050             if (yymsg != yymsgbuf)
2051               YYSTACK_FREE (yymsg);
2052             yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
2053             if (!yymsg)
2054               {
2055                 yymsg = yymsgbuf;
2056                 yymsg_alloc = sizeof yymsgbuf;
2057                 yysyntax_error_status = 2;
2058               }
2059             else
2060               {
2061                 yysyntax_error_status = YYSYNTAX_ERROR;
2062                 yymsgp = yymsg;
2063               }
2064           }
2065         yyerror (yymsgp);
2066         if (yysyntax_error_status == 2)
2067           goto yyexhaustedlab;
2068       }
2069 # undef YYSYNTAX_ERROR
2070 #endif
2071     }
2072
2073
2074
2075   if (yyerrstatus == 3)
2076     {
2077       /* If just tried and failed to reuse lookahead token after an
2078          error, discard it.  */
2079
2080       if (yychar <= YYEOF)
2081         {
2082           /* Return failure if at end of input.  */
2083           if (yychar == YYEOF)
2084             YYABORT;
2085         }
2086       else
2087         {
2088           yydestruct ("Error: discarding",
2089                       yytoken, &yylval);
2090           yychar = YYEMPTY;
2091         }
2092     }
2093
2094   /* Else will try to reuse lookahead token after shifting the error
2095      token.  */
2096   goto yyerrlab1;
2097
2098
2099 /*---------------------------------------------------.
2100 | yyerrorlab -- error raised explicitly by YYERROR.  |
2101 `---------------------------------------------------*/
2102 yyerrorlab:
2103
2104   /* Pacify compilers like GCC when the user code never invokes
2105      YYERROR and the label yyerrorlab therefore never appears in user
2106      code.  */
2107   if (/*CONSTCOND*/ 0)
2108      goto yyerrorlab;
2109
2110   /* Do not reclaim the symbols of the rule which action triggered
2111      this YYERROR.  */
2112   YYPOPSTACK (yylen);
2113   yylen = 0;
2114   YY_STACK_PRINT (yyss, yyssp);
2115   yystate = *yyssp;
2116   goto yyerrlab1;
2117
2118
2119 /*-------------------------------------------------------------.
2120 | yyerrlab1 -- common code for both syntax error and YYERROR.  |
2121 `-------------------------------------------------------------*/
2122 yyerrlab1:
2123   yyerrstatus = 3;      /* Each real token shifted decrements this.  */
2124
2125   for (;;)
2126     {
2127       yyn = yypact[yystate];
2128       if (!yypact_value_is_default (yyn))
2129         {
2130           yyn += YYTERROR;
2131           if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
2132             {
2133               yyn = yytable[yyn];
2134               if (0 < yyn)
2135                 break;
2136             }
2137         }
2138
2139       /* Pop the current state because it cannot handle the error token.  */
2140       if (yyssp == yyss)
2141         YYABORT;
2142
2143
2144       yydestruct ("Error: popping",
2145                   yystos[yystate], yyvsp);
2146       YYPOPSTACK (1);
2147       yystate = *yyssp;
2148       YY_STACK_PRINT (yyss, yyssp);
2149     }
2150
2151   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
2152   *++yyvsp = yylval;
2153   YY_IGNORE_MAYBE_UNINITIALIZED_END
2154
2155
2156   /* Shift the error token.  */
2157   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
2158
2159   yystate = yyn;
2160   goto yynewstate;
2161
2162
2163 /*-------------------------------------.
2164 | yyacceptlab -- YYACCEPT comes here.  |
2165 `-------------------------------------*/
2166 yyacceptlab:
2167   yyresult = 0;
2168   goto yyreturn;
2169
2170 /*-----------------------------------.
2171 | yyabortlab -- YYABORT comes here.  |
2172 `-----------------------------------*/
2173 yyabortlab:
2174   yyresult = 1;
2175   goto yyreturn;
2176
2177 #if !defined yyoverflow || YYERROR_VERBOSE
2178 /*-------------------------------------------------.
2179 | yyexhaustedlab -- memory exhaustion comes here.  |
2180 `-------------------------------------------------*/
2181 yyexhaustedlab:
2182   yyerror (YY_("memory exhausted"));
2183   yyresult = 2;
2184   /* Fall through.  */
2185 #endif
2186
2187 yyreturn:
2188   if (yychar != YYEMPTY)
2189     {
2190       /* Make sure we have latest lookahead translation.  See comments at
2191          user semantic actions for why this is necessary.  */
2192       yytoken = YYTRANSLATE (yychar);
2193       yydestruct ("Cleanup: discarding lookahead",
2194                   yytoken, &yylval);
2195     }
2196   /* Do not reclaim the symbols of the rule which action triggered
2197      this YYABORT or YYACCEPT.  */
2198   YYPOPSTACK (yylen);
2199   YY_STACK_PRINT (yyss, yyssp);
2200   while (yyssp != yyss)
2201     {
2202       yydestruct ("Cleanup: popping",
2203                   yystos[*yyssp], yyvsp);
2204       YYPOPSTACK (1);
2205     }
2206 #ifndef yyoverflow
2207   if (yyss != yyssa)
2208     YYSTACK_FREE (yyss);
2209 #endif
2210 #if YYERROR_VERBOSE
2211   if (yymsg != yymsgbuf)
2212     YYSTACK_FREE (yymsg);
2213 #endif
2214   /* Make sure YYID is used.  */
2215   return YYID (yyresult);
2216 }
2217
2218
2219 /* Line 2055 of yacc.c  */
2220 #line 550 "i386_parse.y"
2221
2222
2223 static void
2224 yyerror (const char *s)
2225 {
2226   error (0, 0, gettext ("while reading i386 CPU description: %s at line %d"),
2227          gettext (s), i386_lineno);
2228 }
2229
2230
2231 static int
2232 bitfield_compare (const void *p1, const void *p2)
2233 {
2234   struct known_bitfield *f1 = (struct known_bitfield *) p1;
2235   struct known_bitfield *f2 = (struct known_bitfield *) p2;
2236
2237   return strcmp (f1->name, f2->name);
2238 }
2239
2240
2241 static void
2242 new_bitfield (char *name, unsigned long int num)
2243 {
2244   struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
2245   newp->name = name;
2246   newp->bits = num;
2247   newp->tmp = 0;
2248
2249   if (tfind (newp, &bitfields, bitfield_compare) != NULL)
2250     {
2251       error (0, 0, "%d: duplicated definition of bitfield '%s'",
2252              i386_lineno, name);
2253       free (name);
2254       return;
2255     }
2256
2257   if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
2258     error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
2259            i386_lineno, name);
2260 }
2261
2262
2263 /* Check that the number of bits is a multiple of 8.  */
2264 static void
2265 check_bits (struct bitvalue *val)
2266 {
2267   struct bitvalue *runp = val;
2268   unsigned int total = 0;
2269
2270   while (runp != NULL)
2271     {
2272       if (runp->type == zeroone)
2273         ++total;
2274       else if (runp->field == NULL)
2275         /* No sense doing anything, the field is not known.  */
2276         return;
2277       else
2278         total += runp->field->bits;
2279
2280       runp = runp->next;
2281     }
2282
2283   if (total % 8 != 0)
2284     {
2285       struct obstack os;
2286       obstack_init (&os);
2287
2288       while (val != NULL)
2289         {
2290           if (val->type == zeroone)
2291             obstack_printf (&os, "%u", val->value);
2292           else
2293             obstack_printf (&os, "{%s}", val->field->name);
2294           val = val->next;
2295         }
2296       obstack_1grow (&os, '\0');
2297
2298       error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
2299              i386_lineno, (char *) obstack_finish (&os));
2300
2301       obstack_free (&os, NULL);
2302     }
2303 }
2304
2305
2306 static int
2307 check_duplicates (struct bitvalue *val)
2308 {
2309   static int testcnt;
2310   ++testcnt;
2311
2312   int result = 0;
2313   while (val != NULL)
2314     {
2315       if (val->type == field && val->field != NULL)
2316         {
2317           if (val->field->tmp == testcnt)
2318             {
2319               error (0, 0, "%d: bitfield '%s' used more than once",
2320                      i386_lineno - 1, val->field->name);
2321               result = 1;
2322             }
2323           val->field->tmp = testcnt;
2324         }
2325
2326       val = val->next;
2327     }
2328
2329   return result;
2330 }
2331
2332
2333 static int
2334 check_argsdef (struct bitvalue *bitval, struct argument *args)
2335 {
2336   int result = 0;
2337
2338   while (args != NULL)
2339     {
2340       for (struct argname *name = args->name; name != NULL; name = name->next)
2341         if (name->type == nfield && name->field != NULL
2342             && name->field != &ax_reg && name->field != &dx_reg
2343             && name->field != &di_reg && name->field != &si_reg
2344             && name->field != &bx_reg)
2345           {
2346             struct bitvalue *runp = bitval;
2347
2348             while (runp != NULL)
2349               if (runp->type == field && runp->field == name->field)
2350                 break;
2351               else
2352                 runp = runp->next;
2353
2354             if (runp == NULL)
2355               {
2356                 error (0, 0, "%d: unknown bitfield '%s' used in output format",
2357                        i386_lineno - 1, name->field->name);
2358                 result = 1;
2359               }
2360           }
2361
2362       args = args->next;
2363     }
2364
2365   return result;
2366 }
2367
2368
2369 static int
2370 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
2371                 struct argument *args)
2372 {
2373   int result = 0;
2374
2375   while (bitval != NULL)
2376     {
2377       if (bitval->type == field && bitval->field != NULL
2378           && bitval->field != suffix
2379           /* {w} is handled special.  */
2380           && strcmp (bitval->field->name, "w") != 0)
2381         {
2382           struct argument *runp;
2383           for (runp = args; runp != NULL; runp = runp->next)
2384             {
2385               struct argname *name = runp->name;
2386
2387               while (name != NULL)
2388                 if (name->type == nfield && name->field == bitval->field)
2389                   break;
2390                 else
2391                   name = name->next;
2392
2393               if (name != NULL)
2394                 break;
2395             }
2396
2397 #if 0
2398           if (runp == NULL)
2399             {
2400               error (0, 0, "%d: bitfield '%s' not used",
2401                      i386_lineno - 1, bitval->field->name);
2402               result = 1;
2403             }
2404 #endif
2405         }
2406
2407       bitval = bitval->next;
2408     }
2409
2410   return result;
2411 }
2412
2413
2414 static struct argname *
2415 combine (struct argname *name)
2416 {
2417   struct argname *last_str = NULL;
2418   for (struct argname *runp = name; runp != NULL; runp = runp->next)
2419     {
2420       if (runp->type == string)
2421         {
2422           if (last_str == NULL)
2423             last_str = runp;
2424           else
2425             {
2426               last_str->str = xrealloc (last_str->str,
2427                                         strlen (last_str->str)
2428                                         + strlen (runp->str) + 1);
2429               strcat (last_str->str, runp->str);
2430               last_str->next = runp->next;
2431             }
2432         }
2433       else
2434         last_str = NULL;
2435     }
2436   return name;
2437 }
2438
2439
2440 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
2441
2442
2443 static void
2444 fillin_arg (struct bitvalue *bytes, struct argname *name,
2445             struct instruction *instr, int n)
2446 {
2447   static struct obstack ob;
2448   static int initialized;
2449   if (! initialized)
2450     {
2451       initialized = 1;
2452       obstack_init (&ob);
2453     }
2454
2455   struct argname *runp = name;
2456   int cnt = 0;
2457   while (runp != NULL)
2458     {
2459       /* We ignore strings in the function name.  */
2460       if (runp->type == string)
2461         {
2462           if (instr->operands[n].str != NULL)
2463             error (EXIT_FAILURE, 0,
2464                    "%d: cannot have more than one string parameter",
2465                    i386_lineno - 1);
2466
2467           instr->operands[n].str = runp->str;
2468         }
2469       else
2470         {
2471           assert (runp->type == nfield);
2472
2473           /* Construct the function name.  */
2474           if (cnt++ > 0)
2475             obstack_1grow (&ob, '$');
2476
2477           if (runp->field == NULL)
2478             /* Add some string which contains invalid characters.  */
2479             obstack_grow_str (&ob, "!!!INVALID!!!");
2480           else
2481             {
2482               char *fieldname = runp->field->name;
2483
2484               struct synonym search = { .from = fieldname };
2485
2486               struct synonym **res = tfind (&search, &synonyms, compare_syn);
2487               if (res != NULL)
2488                 fieldname = (*res)->to;
2489
2490               obstack_grow_str (&ob, fieldname);
2491             }
2492
2493           /* Now compute the bit offset of the field.  */
2494           struct bitvalue *b = bytes;
2495           int bitoff = 0;
2496           if (runp->field != NULL)
2497             while (b != NULL)
2498               {
2499                 if (b->type == field && b->field != NULL)
2500                   {
2501                     if (strcmp (b->field->name, runp->field->name) == 0)
2502                       break;
2503                     bitoff += b->field->bits;
2504                   }
2505                 else
2506                   ++bitoff;
2507
2508                 b = b->next;
2509               }
2510           if (instr->operands[n].off1 == 0)
2511             instr->operands[n].off1 = bitoff;
2512           else if (instr->operands[n].off2 == 0)
2513             instr->operands[n].off2 = bitoff;
2514           else if (instr->operands[n].off3 == 0)
2515             instr->operands[n].off3 = bitoff;
2516           else
2517             error (EXIT_FAILURE, 0,
2518                    "%d: cannot have more than three fields in parameter",
2519                    i386_lineno - 1);
2520
2521           if  (runp->field != NULL
2522                && strncasecmp (runp->field->name, "mod", 3) == 0)
2523             instr->modrm = 1;
2524         }
2525
2526       runp = runp->next;
2527     }
2528   if (obstack_object_size (&ob) == 0)
2529     obstack_grow_str (&ob, "string");
2530   obstack_1grow (&ob, '\0');
2531   char *fct = obstack_finish (&ob);
2532
2533   instr->operands[n].fct = fct;
2534 }
2535
2536
2537 #if 0
2538 static void
2539 nameout (const void *nodep, VISIT value, int level)
2540 {
2541   if (value == leaf || value == postorder)
2542     printf ("  %s\n", *(const char **) nodep);
2543 }
2544 #endif
2545
2546
2547 static int
2548 compare_argstring (const void *p1, const void *p2)
2549 {
2550   const struct argstring *a1 = (const struct argstring *) p1;
2551   const struct argstring *a2 = (const struct argstring *) p2;
2552
2553   return strcmp (a1->str, a2->str);
2554 }
2555
2556
2557 static int maxoff[3][3];
2558 static int minoff[3][3] = { { 1000, 1000, 1000 },
2559                             { 1000, 1000, 1000 },
2560                             { 1000, 1000, 1000 } };
2561 static int nbitoff[3][3];
2562 static void *fct_names[3];
2563 static int nbitfct[3];
2564 static int nbitsuf;
2565 static void *strs[3];
2566 static int nbitstr[3];
2567 static int total_bits = 2;      // Already counted the rep/repe bits.
2568
2569 static void
2570 find_numbers (void)
2571 {
2572   int nfct_names[3] = { 0, 0, 0 };
2573   int nstrs[3] = { 0, 0, 0 };
2574
2575   /* We reverse the order of the instruction list while processing it.
2576      Later phases need it in the order in which the input file has
2577      them.  */
2578   struct instruction *reversed = NULL;
2579
2580   struct instruction *runp = instructions;
2581   while (runp != NULL)
2582     {
2583       for (int i = 0; i < 3; ++i)
2584         if (runp->operands[i].fct != NULL)
2585           {
2586             struct argstring search = { .str = runp->operands[i].fct };
2587             if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
2588               {
2589                 struct argstring *newp = xmalloc (sizeof (*newp));
2590                 newp->str = runp->operands[i].fct;
2591                 newp->idx = 0;
2592                 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
2593                   error (EXIT_FAILURE, errno, "tsearch");
2594                 ++nfct_names[i];
2595               }
2596
2597             if (runp->operands[i].str != NULL)
2598               {
2599                 search.str = runp->operands[i].str;
2600                 if (tfind (&search, &strs[i], compare_argstring) == NULL)
2601                   {
2602                     struct argstring *newp = xmalloc (sizeof (*newp));
2603                     newp->str = runp->operands[i].str;
2604                     newp->idx = 0;
2605                     if (tsearch (newp, &strs[i], compare_argstring) == NULL)
2606                       error (EXIT_FAILURE, errno, "tsearch");
2607                     ++nstrs[i];
2608                   }
2609               }
2610
2611             maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
2612             maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
2613             maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
2614
2615             if (runp->operands[i].off1 > 0)
2616               minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
2617             if (runp->operands[i].off2 > 0)
2618               minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
2619             if (runp->operands[i].off3 > 0)
2620               minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
2621           }
2622
2623       struct instruction *old = runp;
2624       runp = runp->next;
2625
2626       old->next = reversed;
2627       reversed = old;
2628     }
2629   instructions = reversed;
2630
2631   int d;
2632   int c;
2633   for (int i = 0; i < 3; ++i)
2634     {
2635       // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
2636       // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
2637
2638       if (minoff[i][0] == 1000)
2639         nbitoff[i][0] = 0;
2640       else
2641         {
2642           nbitoff[i][0] = 1;
2643           d = maxoff[i][0] - minoff[i][0];
2644           c = 1;
2645           while (c < d)
2646             {
2647               ++nbitoff[i][0];
2648               c *= 2;
2649             }
2650           total_bits += nbitoff[i][0];
2651         }
2652
2653       if (minoff[i][1] == 1000)
2654         nbitoff[i][1] = 0;
2655       else
2656         {
2657           nbitoff[i][1] = 1;
2658           d = maxoff[i][1] - minoff[i][1];
2659           c = 1;
2660           while (c < d)
2661             {
2662               ++nbitoff[i][1];
2663               c *= 2;
2664             }
2665           total_bits += nbitoff[i][1];
2666         }
2667
2668       if (minoff[i][2] == 1000)
2669         nbitoff[i][2] = 0;
2670       else
2671         {
2672           nbitoff[i][2] = 1;
2673           d = maxoff[i][2] - minoff[i][2];
2674           c = 1;
2675           while (c < d)
2676             {
2677               ++nbitoff[i][2];
2678               c *= 2;
2679             }
2680           total_bits += nbitoff[i][2];
2681         }
2682       // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
2683
2684       nbitfct[i] = 1;
2685       d = nfct_names[i];
2686       c = 1;
2687       while (c < d)
2688         {
2689           ++nbitfct[i];
2690           c *= 2;
2691         }
2692       total_bits += nbitfct[i];
2693       // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
2694
2695       if (nstrs[i] != 0)
2696         {
2697           nbitstr[i] = 1;
2698           d = nstrs[i];
2699           c = 1;
2700           while (c < d)
2701             {
2702               ++nbitstr[i];
2703               c *= 2;
2704             }
2705           total_bits += nbitstr[i];
2706         }
2707
2708       // twalk (fct_names[i], nameout);
2709     }
2710
2711   nbitsuf = 0;
2712   d = nsuffixes;
2713   c = 1;
2714   while (c < d)
2715     {
2716       ++nbitsuf;
2717       c *= 2;
2718     }
2719   total_bits += nbitsuf;
2720   // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
2721 }
2722
2723
2724 static int
2725 compare_syn (const void *p1, const void *p2)
2726 {
2727   const struct synonym *s1 = (const struct synonym *) p1;
2728   const struct synonym *s2 = (const struct synonym *) p2;
2729
2730   return strcmp (s1->from, s2->from);
2731 }
2732
2733
2734 static int
2735 compare_suf (const void *p1, const void *p2)
2736 {
2737   const struct suffix *s1 = (const struct suffix *) p1;
2738   const struct suffix *s2 = (const struct suffix *) p2;
2739
2740   return strcmp (s1->name, s2->name);
2741 }
2742
2743
2744 static int count_op_str;
2745 static int off_op_str;
2746 static void
2747 print_op_str (const void *nodep, VISIT value,
2748               int level __attribute__ ((unused)))
2749 {
2750   if (value == leaf || value == postorder)
2751     {
2752       const char *str = (*(struct argstring **) nodep)->str;
2753       fprintf (outfile, "%s\n  \"%s",
2754                count_op_str == 0 ? "" : "\\0\"", str);
2755       (*(struct argstring **) nodep)->idx = ++count_op_str;
2756       (*(struct argstring **) nodep)->off = off_op_str;
2757       off_op_str += strlen (str) + 1;
2758     }
2759 }
2760
2761
2762 static void
2763 print_op_str_idx (const void *nodep, VISIT value,
2764                   int level __attribute__ ((unused)))
2765 {
2766   if (value == leaf || value == postorder)
2767     printf ("  %d,\n", (*(struct argstring **) nodep)->off);
2768 }
2769
2770
2771 static void
2772 print_op_fct (const void *nodep, VISIT value,
2773               int level __attribute__ ((unused)))
2774 {
2775   if (value == leaf || value == postorder)
2776     {
2777       fprintf (outfile, "  FCT_%s,\n", (*(struct argstring **) nodep)->str);
2778       (*(struct argstring **) nodep)->idx = ++count_op_str;
2779     }
2780 }
2781
2782
2783 #if NMNES < 2
2784 # error "bogus NMNES value"
2785 #endif
2786
2787 static void
2788 instrtable_out (void)
2789 {
2790   find_numbers ();
2791
2792 #if 0
2793   create_mnemonic_table ();
2794
2795   fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
2796 #else
2797   fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
2798            lrint (ceil (log2 (NMNES))));
2799 #endif
2800   fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
2801   for (int i = 0; i < 3; ++i)
2802     {
2803       fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
2804       if (nbitstr[i] != 0)
2805         fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
2806       fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
2807       fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
2808       if (nbitoff[i][1] != 0)
2809         {
2810           fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
2811           fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
2812         }
2813       if (nbitoff[i][2] != 0)
2814         {
2815           fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
2816           fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
2817         }
2818     }
2819
2820   fputs ("\n#include <i386_data.h>\n\n", outfile);
2821
2822
2823 #define APPEND(a, b) APPEND_ (a, b)
2824 #define APPEND_(a, b) a##b
2825 #define EMIT_SUFFIX(suf) \
2826   fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
2827   EMIT_SUFFIX (none);
2828   EMIT_SUFFIX (w);
2829   EMIT_SUFFIX (w0);
2830   EMIT_SUFFIX (W);
2831   EMIT_SUFFIX (tttn);
2832   EMIT_SUFFIX (D);
2833   EMIT_SUFFIX (w1);
2834   EMIT_SUFFIX (W1);
2835
2836   fputc_unlocked ('\n', outfile);
2837
2838   for (int i = 0; i < 3; ++i)
2839     {
2840       /* Functions.  */
2841       count_op_str = 0;
2842       fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n  NULL,\n",
2843                i + 1);
2844       twalk (fct_names[i], print_op_fct);
2845       fputs ("};\n", outfile);
2846
2847       /* The operand strings.  */
2848       if (nbitstr[i] != 0)
2849         {
2850           count_op_str = 0;
2851           off_op_str = 0;
2852           fprintf (outfile, "static const char op%d_str[] =", i + 1);
2853           twalk (strs[i], print_op_str);
2854           fputs ("\";\n", outfile);
2855
2856           fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
2857                    i + 1);
2858           twalk (strs[i], print_op_str_idx);
2859           fputs ("};\n", outfile);
2860         }
2861     }
2862
2863
2864   fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
2865   struct instruction *instr;
2866   for (instr = instructions; instr != NULL; instr = instr->next)
2867     {
2868       fputs ("  {", outfile);
2869       if (instr->mnemonic == (void *) -1l)
2870         fputs (" .mnemonic = MNE_INVALID,", outfile);
2871       else
2872         fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
2873       fprintf (outfile, " .rep = %d,", instr->rep);
2874       fprintf (outfile, " .repe = %d,", instr->repe);
2875       fprintf (outfile, " .suffix = %d,", instr->suffix);
2876       fprintf (outfile, " .modrm = %d,", instr->modrm);
2877
2878       for (int i = 0; i < 3; ++i)
2879         {
2880           int idx = 0;
2881           if (instr->operands[i].fct != NULL)
2882             {
2883               struct argstring search = { .str = instr->operands[i].fct };
2884               struct argstring **res = tfind (&search, &fct_names[i],
2885                                               compare_argstring);
2886               assert (res != NULL);
2887               idx = (*res)->idx;
2888             }
2889           fprintf (outfile, " .fct%d = %d,", i + 1, idx);
2890
2891           idx = 0;
2892           if (instr->operands[i].str != NULL)
2893             {
2894               struct argstring search = { .str = instr->operands[i].str };
2895               struct argstring **res = tfind (&search, &strs[i],
2896                                               compare_argstring);
2897               assert (res != NULL);
2898               idx = (*res)->idx;
2899             }
2900           if (nbitstr[i] != 0)
2901             fprintf (outfile, " .str%d = %d,", i + 1, idx);
2902
2903           fprintf (outfile, " .off%d_1 = %d,", i + 1,
2904                    MAX (0, instr->operands[i].off1 - minoff[i][0]));
2905
2906           if (nbitoff[i][1] != 0)
2907             fprintf (outfile, " .off%d_2 = %d,", i + 1,
2908                      MAX (0, instr->operands[i].off2 - minoff[i][1]));
2909
2910           if (nbitoff[i][2] != 0)
2911             fprintf (outfile, " .off%d_3 = %d,", i + 1,
2912                      MAX (0, instr->operands[i].off3 - minoff[i][2]));
2913         }
2914
2915       fputs (" },\n", outfile);
2916     }
2917   fputs ("};\n", outfile);
2918
2919   fputs ("static const uint8_t match_data[] =\n{\n", outfile);
2920   size_t cnt = 0;
2921   for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
2922     {
2923       /* First count the number of bytes.  */
2924       size_t totalbits = 0;
2925       size_t zerobits = 0;
2926       bool leading_p = true;
2927       size_t leadingbits = 0;
2928       struct bitvalue *b = instr->bytes;
2929       while (b != NULL)
2930         {
2931           if (b->type == zeroone)
2932             {
2933               ++totalbits;
2934               zerobits = 0;
2935               if (leading_p)
2936                 ++leadingbits;
2937             }
2938           else
2939             {
2940               totalbits += b->field->bits;
2941               /* We must always count the mod/rm byte.  */
2942               if (strncasecmp (b->field->name, "mod", 3) == 0)
2943                 zerobits = 0;
2944               else
2945                 zerobits += b->field->bits;
2946               leading_p = false;
2947             }
2948           b = b->next;
2949         }
2950       size_t nbytes = (totalbits - zerobits + 7) / 8;
2951       assert (nbytes > 0);
2952       size_t leadingbytes = leadingbits / 8;
2953
2954       fprintf (outfile, "  %#zx,", nbytes | (leadingbytes << 4));
2955
2956       /* Now create the mask and byte values.  */
2957       uint8_t byte = 0;
2958       uint8_t mask = 0;
2959       int nbits = 0;
2960       b = instr->bytes;
2961       while (b != NULL)
2962         {
2963           if (b->type == zeroone)
2964             {
2965               byte = (byte << 1) | b->value;
2966               mask = (mask << 1) | 1;
2967               if (++nbits == 8)
2968                 {
2969                   if (leadingbytes > 0)
2970                     {
2971                       assert (mask == 0xff);
2972                       fprintf (outfile, " %#" PRIx8 ",", byte);
2973                       --leadingbytes;
2974                     }
2975                   else
2976                     fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
2977                              mask, byte);
2978                   byte = mask = nbits = 0;
2979                   if (--nbytes == 0)
2980                     break;
2981                 }
2982             }
2983           else
2984             {
2985               assert (leadingbytes == 0);
2986
2987               unsigned long int remaining = b->field->bits;
2988               while (nbits + remaining > 8)
2989                 {
2990                   fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
2991                            mask << (8 - nbits), byte << (8 - nbits));
2992                   remaining = nbits + remaining - 8;
2993                   byte = mask = nbits = 0;
2994                   if (--nbytes == 0)
2995                     break;
2996                 }
2997               byte <<= remaining;
2998               mask <<= remaining;
2999               nbits += remaining;
3000               if (nbits == 8)
3001                 {
3002                   fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
3003                   byte = mask = nbits = 0;
3004                   if (--nbytes == 0)
3005                     break;
3006                 }
3007             }
3008           b = b->next;
3009         }
3010
3011       fputc_unlocked ('\n', outfile);
3012     }
3013   fputs ("};\n", outfile);
3014 }
3015
3016
3017 #if 0
3018 static size_t mnemonic_maxlen;
3019 static size_t mnemonic_minlen;
3020 static size_t
3021 which_chars (const char *str[], size_t nstr)
3022 {
3023   char used_char[256];
3024   memset (used_char, '\0', sizeof (used_char));
3025   mnemonic_maxlen = 0;
3026   mnemonic_minlen = 10000;
3027   for (size_t cnt = 0; cnt < nstr; ++cnt)
3028     {
3029       const unsigned char *cp = (const unsigned char *) str[cnt];
3030       mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
3031       mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
3032       do
3033         used_char[*cp++] = 1;
3034       while (*cp != '\0');
3035     }
3036   size_t nused_char = 0;
3037   for (size_t cnt = 0; cnt < 256; ++cnt)
3038     if (used_char[cnt] != 0)
3039       ++nused_char;
3040   return nused_char;
3041 }
3042
3043
3044 static const char **mnemonic_strs;
3045 static size_t nmnemonic_strs;
3046 static void
3047 add_mnemonics (const void *nodep, VISIT value,
3048                int level __attribute__ ((unused)))
3049 {
3050   if (value == leaf || value == postorder)
3051     mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
3052 }
3053
3054
3055 struct charfreq
3056 {
3057   char ch;
3058   int freq;
3059 };
3060 static struct charfreq pfxfreq[256];
3061 static struct charfreq sfxfreq[256];
3062
3063
3064 static int
3065 compare_freq (const void *p1, const void *p2)
3066 {
3067   const struct charfreq *c1 = (const struct charfreq *) p1;
3068   const struct charfreq *c2 = (const struct charfreq *) p2;
3069
3070   if (c1->freq > c2->freq)
3071     return -1;
3072   if (c1->freq < c2->freq)
3073     return 1;
3074   return 0;
3075 }
3076
3077
3078 static size_t
3079 compute_pfxfreq (const char *str[], size_t nstr)
3080 {
3081   memset (pfxfreq, '\0', sizeof (pfxfreq));
3082
3083   for (size_t i = 0; i < nstr; ++i)
3084     pfxfreq[i].ch = i;
3085
3086   for (size_t i = 0; i < nstr; ++i)
3087     ++pfxfreq[*((const unsigned char *) str[i])].freq;
3088
3089   qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
3090
3091   size_t n = 0;
3092   while (n < 256 && pfxfreq[n].freq != 0)
3093     ++n;
3094   return n;
3095 }
3096
3097
3098 struct strsnlen
3099 {
3100   const char *str;
3101   size_t len;
3102 };
3103
3104 static size_t
3105 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
3106 {
3107   memset (sfxfreq, '\0', sizeof (sfxfreq));
3108
3109   for (size_t i = 0; i < nstr; ++i)
3110     sfxfreq[i].ch = i;
3111
3112   for (size_t i = 0; i < nstr; ++i)
3113     ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
3114
3115   qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
3116
3117   size_t n = 0;
3118   while (n < 256 && sfxfreq[n].freq != 0)
3119     ++n;
3120   return n;
3121 }
3122
3123
3124 static void
3125 create_mnemonic_table (void)
3126 {
3127   mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
3128
3129   twalk (mnemonics, add_mnemonics);
3130
3131   (void) which_chars (mnemonic_strs, nmnemonic_strs);
3132
3133   size_t best_so_far = 100000000;
3134   char *best_prefix = NULL;
3135   char *best_suffix = NULL;
3136   char *best_table = NULL;
3137   size_t best_table_size = 0;
3138   size_t best_table_bits = 0;
3139   size_t best_prefix_bits = 0;
3140
3141   /* We can precompute the prefix characters.  */
3142   size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
3143
3144   /* Compute best size for string representation including explicit NUL.  */
3145   for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
3146     {
3147       char prefix[1 << pfxbits];
3148       size_t i;
3149       for (i = 0; i < (1u << pfxbits) - 1; ++i)
3150         prefix[i] = pfxfreq[i].ch;
3151       prefix[i] = '\0';
3152
3153       struct strsnlen strsnlen[nmnemonic_strs];
3154
3155       for (i = 0; i < nmnemonic_strs; ++i)
3156         {
3157           if (strchr (prefix, *mnemonic_strs[i]) != NULL)
3158             strsnlen[i].str = mnemonic_strs[i] + 1;
3159           else
3160             strsnlen[i].str = mnemonic_strs[i];
3161           strsnlen[i].len = strlen (strsnlen[i].str);
3162         }
3163
3164       /* With the prefixes gone, try to combine strings.  */
3165       size_t nstrsnlen = 1;
3166       for (i = 1; i < nmnemonic_strs; ++i)
3167         {
3168           size_t j;
3169           for (j = 0; j < nstrsnlen; ++j)
3170             if (strsnlen[i].len > strsnlen[j].len
3171                 && strcmp (strsnlen[j].str,
3172                            strsnlen[i].str + (strsnlen[i].len
3173                                               - strsnlen[j].len)) == 0)
3174               {
3175                 strsnlen[j] = strsnlen[i];
3176                 break;
3177               }
3178             else if (strsnlen[i].len < strsnlen[j].len
3179                      && strcmp (strsnlen[i].str,
3180                                 strsnlen[j].str + (strsnlen[j].len
3181                                                    - strsnlen[i].len)) == 0)
3182               break;
3183 ;
3184           if (j == nstrsnlen)
3185               strsnlen[nstrsnlen++] = strsnlen[i];
3186         }
3187
3188       size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
3189
3190       for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
3191         {
3192           char suffix[1 << sfxbits];
3193
3194           for (i = 0; i < (1u << sfxbits) - 1; ++i)
3195             suffix[i] = sfxfreq[i].ch;
3196           suffix[i] = '\0';
3197
3198           size_t newlen[nstrsnlen];
3199
3200           for (i = 0; i < nstrsnlen; ++i)
3201             if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
3202               newlen[i] = strsnlen[i].len - 1;
3203             else
3204               newlen[i] = strsnlen[i].len;
3205
3206           char charused[256];
3207           memset (charused, '\0', sizeof (charused));
3208           size_t ncharused = 0;
3209
3210           const char *tablestr[nstrsnlen];
3211           size_t ntablestr = 1;
3212           tablestr[0] = strsnlen[0].str;
3213           size_t table = newlen[0] + 1;
3214           for (i = 1; i < nstrsnlen; ++i)
3215             {
3216               size_t j;
3217               for (j = 0; j < ntablestr; ++j)
3218                 if (newlen[i] > newlen[j]
3219                     && memcmp (tablestr[j],
3220                                strsnlen[i].str + (newlen[i] - newlen[j]),
3221                                newlen[j]) == 0)
3222                   {
3223                     table += newlen[i] - newlen[j];
3224                     tablestr[j] = strsnlen[i].str;
3225                     newlen[j] = newlen[i];
3226                     break;
3227                   }
3228                 else if (newlen[i] < newlen[j]
3229                      && memcmp (strsnlen[i].str,
3230                                 tablestr[j] + (newlen[j] - newlen[i]),
3231                                 newlen[i]) == 0)
3232                   break;
3233
3234               if (j == ntablestr)
3235                 {
3236                   table += newlen[i] + 1;
3237                   tablestr[ntablestr] = strsnlen[i].str;
3238                   newlen[ntablestr] = newlen[i];
3239
3240                   ++ntablestr;
3241                 }
3242
3243               for (size_t x = 0; x < newlen[j]; ++x)
3244                 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
3245                   ++ncharused;
3246             }
3247
3248           size_t ncharused_bits = 0;
3249           i = 1;
3250           while (i < ncharused)
3251             {
3252               i *= 2;
3253               ++ncharused_bits;
3254             }
3255
3256           size_t table_bits = 0;
3257           i = 1;
3258           while (i < table)
3259             {
3260               i *= 2;
3261               ++table_bits;
3262             }
3263
3264           size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
3265           size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
3266                               + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
3267                               + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
3268                               + (((total_bits + mnemonic_bits + 7) / 8)
3269                                  * ninstructions));
3270
3271           if (new_total < best_so_far)
3272             {
3273               best_so_far = new_total;
3274               best_mnemonic_bits = mnemonic_bits;
3275
3276               free (best_suffix);
3277               best_suffix = xstrdup (suffix);
3278
3279               free (best_prefix);
3280               best_prefix = xstrdup (prefix);
3281               best_prefix_bits = pfxbits;
3282
3283               best_table_size = table;
3284               best_table_bits = table_bits;
3285               char *cp = best_table = xrealloc (best_table, table);
3286               for (i = 0; i < ntablestr; ++i)
3287                 {
3288                   assert (cp + newlen[i] + 1 <= best_table + table);
3289                   cp = mempcpy (cp, tablestr[i], newlen[i]);
3290                   *cp++ = '\0';
3291                 }
3292               assert (cp == best_table + table);
3293             }
3294         }
3295     }
3296
3297   fputs ("static const char mnemonic_table[] =\n\"", outfile);
3298   for (size_t i = 0; i < best_table_size; ++i)
3299     {
3300       if (((i + 1) % 60) == 0)
3301         fputs ("\"\n\"", outfile);
3302       if (!isascii (best_table[i]) || !isprint (best_table[i]))
3303         fprintf (outfile, "\\%03o", best_table[i]);
3304       else
3305         fputc (best_table[i], outfile);
3306     }
3307   fputs ("\";\n", outfile);
3308
3309   if (best_prefix[0] != '\0')
3310     fprintf (outfile,
3311              "static const char prefix[%zu] = \"%s\";\n"
3312              "#define PREFIXCHAR_BITS %zu\n",
3313              strlen (best_prefix), best_prefix, best_prefix_bits);
3314   else
3315     fputs ("#define NO_PREFIX\n", outfile);
3316
3317   if (best_suffix[0] != '\0')
3318     fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
3319              strlen (best_suffix), best_suffix);
3320   else
3321     fputs ("#define NO_SUFFIX\n", outfile);
3322
3323   for (size_t i = 0; i < nmnemonic_strs; ++i)
3324     {
3325       const char *mne = mnemonic_strs[i];
3326
3327       size_t pfxval = 0;
3328       char *cp = strchr (best_prefix, *mne);
3329       if (cp != NULL)
3330         {
3331           pfxval = 1 + (cp - best_prefix);
3332           ++mne;
3333         }
3334
3335       size_t l = strlen (mne);
3336
3337       size_t sfxval = 0;
3338       cp = strchr (best_suffix, mne[l - 1]);
3339       if (cp != NULL)
3340         {
3341           sfxval = 1 + (cp - best_suffix);
3342           --l;
3343         }
3344
3345       char *off = memmem (best_table, best_table_size, mne, l);
3346       while (off[l] != '\0')
3347         {
3348           off = memmem (off + 1, best_table_size, mne, l);
3349           assert (off != NULL);
3350         }
3351
3352       fprintf (outfile, "#define MNE_%s %#zx\n",
3353                mnemonic_strs[i],
3354                (off - best_table)
3355                + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));
3356     }
3357 }
3358 #endif