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