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