1 /* A Bison parser, made by GNU Bison 2.7. */
3 /* Bison implementation for Yacc-like parsers in C
5 Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* As a special exception, you may create a larger work that contains
21 part or all of the Bison parser skeleton and distribute that work
22 under terms of your choice, so long as that work isn't itself a
23 parser generator using the skeleton or a modified version thereof
24 as a parser skeleton. Alternatively, if you modify or redistribute
25 the parser skeleton itself, you may (at your option) remove this
26 special exception, which will cause the skeleton and the resulting
27 Bison output files to be licensed under the GNU General Public
28 License without this special exception.
30 This special exception was added by the Free Software Foundation in
31 version 2.2 of Bison. */
33 /* C LALR(1) parser skeleton written by Richard Stallman, by
34 simplifying the original so-called "semantic" parser. */
36 /* All symbols defined below should begin with yy or YY, to avoid
37 infringing on user name space. This should be done even for local
38 variables, as they might otherwise be expanded by user macros.
39 There are some unavoidable exceptions within include files to
40 define necessary library symbols; they are noted "INFRINGES ON
41 USER NAME SPACE" below. */
43 /* Identify Bison output. */
47 #define YYBISON_VERSION "2.7"
50 #define YYSKELETON_NAME "yacc.c"
62 /* Substitute the variable and function names. */
63 #define yyparse i386_parse
64 #define yylex i386_lex
65 #define yyerror i386_error
66 #define yylval i386_lval
67 #define yychar i386_char
68 #define yydebug i386_debug
69 #define yynerrs i386_nerrs
71 /* Copy the first part of user declarations. */
72 /* Line 371 of yacc.c */
73 #line 1 "i386_parse.y"
75 /* Parser for i386 CPU description.
76 Copyright (C) 2004, 2005, 2007, 2008, 2009 Red Hat, Inc.
77 Written by Ulrich Drepper <drepper@redhat.com>, 2004.
79 This file is free software; you can redistribute it and/or modify
80 it under the terms of either
82 * the GNU Lesser General Public License as published by the Free
83 Software Foundation; either version 3 of the License, or (at
84 your option) any later version
88 * the GNU General Public License as published by the Free
89 Software Foundation; either version 2 of the License, or (at
90 your option) any later version
92 or both in parallel, as here.
94 elfutils is distributed in the hope that it will be useful, but
95 WITHOUT ANY WARRANTY; without even the implied warranty of
96 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
97 General Public License for more details.
99 You should have received copies of the GNU General Public License and
100 the GNU Lesser General Public License along with this program. If
101 not, see <http://www.gnu.org/licenses/>. */
111 #include <inttypes.h>
120 #include <sys/param.h>
124 #define obstack_chunk_alloc xmalloc
125 #define obstack_chunk_free free
127 /* The error handler. */
128 static void yyerror (const char *s);
130 extern int yylex (void);
131 extern int i386_lineno;
132 extern char *infname;
135 struct known_bitfield
138 unsigned long int bits;
145 enum bittype { zeroone, field, failure } type;
149 struct known_bitfield *field;
151 struct bitvalue *next;
157 enum nametype { string, nfield } type;
161 struct known_bitfield *field;
163 struct argname *next;
169 struct argname *name;
170 struct argument *next;
176 /* The byte encoding. */
177 struct bitvalue *bytes;
179 /* Prefix possible. */
187 enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
188 suffix_w1, suffix_W1, suffix_D } suffix;
190 /* Flag set if modr/m is used. */
203 struct instruction *next;
229 static struct known_bitfield ax_reg =
231 .name = "ax", .bits = 0, .tmp = 0
234 static struct known_bitfield dx_reg =
236 .name = "dx", .bits = 0, .tmp = 0
239 static struct known_bitfield di_reg =
241 .name = "es_di", .bits = 0, .tmp = 0
244 static struct known_bitfield si_reg =
246 .name = "ds_si", .bits = 0, .tmp = 0
249 static struct known_bitfield bx_reg =
251 .name = "ds_bx", .bits = 0, .tmp = 0
255 static int bitfield_compare (const void *p1, const void *p2);
256 static void new_bitfield (char *name, unsigned long int num);
257 static void check_bits (struct bitvalue *value);
258 static int check_duplicates (struct bitvalue *val);
259 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
260 static int check_bitsused (struct bitvalue *bitval,
261 struct known_bitfield *suffix,
262 struct argument *args);
263 static struct argname *combine (struct argname *name);
264 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
265 struct instruction *instr, int n);
266 static void find_numbers (void);
267 static int compare_syn (const void *p1, const void *p2);
268 static int compare_suf (const void *p1, const void *p2);
269 static void instrtable_out (void);
271 static void create_mnemonic_table (void);
274 static void *bitfields;
275 static struct instruction *instructions;
276 static size_t ninstructions;
277 static void *synonyms;
278 static void *suffixes;
279 static int nsuffixes;
280 static void *mnemonics;
282 extern FILE *outfile;
284 /* Number of bits used mnemonics. */
286 static size_t best_mnemonic_bits;
289 /* Line 371 of yacc.c */
290 #line 291 "i386_parse.c"
293 # if defined __cplusplus && 201103L <= __cplusplus
294 # define YY_NULL nullptr
300 /* Enabling verbose error messages. */
301 #ifdef YYERROR_VERBOSE
302 # undef YYERROR_VERBOSE
303 # define YYERROR_VERBOSE 1
305 # define YYERROR_VERBOSE 0
308 /* In a future release of Bison, this section will be replaced
309 by #include "y.tab.h". */
310 #ifndef YY_I386_I_PARSE_H_INCLUDED
311 # define YY_I386_I_PARSE_H_INCLUDED
312 /* Enabling traces. */
317 extern int i386_debug;
323 /* Put the tokens into the symbol table, so that GDB and other debuggers
345 #define kPERCPERC 264
346 #define kBITFIELD 265
352 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
353 typedef union YYSTYPE
355 /* Line 387 of yacc.c */
356 #line 217 "i386_parse.y"
358 unsigned long int num;
361 struct known_bitfield *field;
362 struct bitvalue *bit;
363 struct argname *name;
364 struct argument *arg;
367 /* Line 387 of yacc.c */
368 #line 369 "i386_parse.c"
370 # define YYSTYPE_IS_TRIVIAL 1
371 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
372 # define YYSTYPE_IS_DECLARED 1
375 extern YYSTYPE i386_lval;
378 #if defined __STDC__ || defined __cplusplus
379 int i386_parse (void *YYPARSE_PARAM);
383 #else /* ! YYPARSE_PARAM */
384 #if defined __STDC__ || defined __cplusplus
385 int i386_parse (void);
389 #endif /* ! YYPARSE_PARAM */
391 #endif /* !YY_I386_I_PARSE_H_INCLUDED */
393 /* Copy the second part of user declarations. */
395 /* Line 390 of yacc.c */
396 #line 397 "i386_parse.c"
403 typedef YYTYPE_UINT8 yytype_uint8;
405 typedef unsigned char yytype_uint8;
409 typedef YYTYPE_INT8 yytype_int8;
410 #elif (defined __STDC__ || defined __C99__FUNC__ \
411 || defined __cplusplus || defined _MSC_VER)
412 typedef signed char yytype_int8;
414 typedef short int yytype_int8;
418 typedef YYTYPE_UINT16 yytype_uint16;
420 typedef unsigned short int yytype_uint16;
424 typedef YYTYPE_INT16 yytype_int16;
426 typedef short int yytype_int16;
430 # ifdef __SIZE_TYPE__
431 # define YYSIZE_T __SIZE_TYPE__
432 # elif defined size_t
433 # define YYSIZE_T size_t
434 # elif ! defined YYSIZE_T && (defined __STDC__ || defined __C99__FUNC__ \
435 || defined __cplusplus || defined _MSC_VER)
436 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
437 # define YYSIZE_T size_t
439 # define YYSIZE_T unsigned int
443 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
446 # if defined YYENABLE_NLS && YYENABLE_NLS
448 # include <libintl.h> /* INFRINGES ON USER NAME SPACE */
449 # define YY_(Msgid) dgettext ("bison-runtime", Msgid)
453 # define YY_(Msgid) Msgid
457 /* Suppress unused-variable warnings by "using" E. */
458 #if ! defined lint || defined __GNUC__
459 # define YYUSE(E) ((void) (E))
461 # define YYUSE(E) /* empty */
464 /* Identity function, used to suppress warnings about constant conditions. */
468 #if (defined __STDC__ || defined __C99__FUNC__ \
469 || defined __cplusplus || defined _MSC_VER)
482 #if ! defined yyoverflow || YYERROR_VERBOSE
484 /* The parser invokes alloca or malloc; define the necessary symbols. */
486 # ifdef YYSTACK_USE_ALLOCA
487 # if YYSTACK_USE_ALLOCA
489 # define YYSTACK_ALLOC __builtin_alloca
490 # elif defined __BUILTIN_VA_ARG_INCR
491 # include <alloca.h> /* INFRINGES ON USER NAME SPACE */
493 # define YYSTACK_ALLOC __alloca
494 # elif defined _MSC_VER
495 # include <malloc.h> /* INFRINGES ON USER NAME SPACE */
496 # define alloca _alloca
498 # define YYSTACK_ALLOC alloca
499 # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
500 || defined __cplusplus || defined _MSC_VER)
501 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
502 /* Use EXIT_SUCCESS as a witness for stdlib.h. */
503 # ifndef EXIT_SUCCESS
504 # define EXIT_SUCCESS 0
511 # ifdef YYSTACK_ALLOC
512 /* Pacify GCC's `empty if-body' warning. */
513 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
514 # ifndef YYSTACK_ALLOC_MAXIMUM
515 /* The OS might guarantee only one guard page at the bottom of the stack,
516 and a page size can be as small as 4096 bytes. So we cannot safely
517 invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
518 to allow for a few compiler-allocated temporary stack slots. */
519 # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
522 # define YYSTACK_ALLOC YYMALLOC
523 # define YYSTACK_FREE YYFREE
524 # ifndef YYSTACK_ALLOC_MAXIMUM
525 # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
527 # if (defined __cplusplus && ! defined EXIT_SUCCESS \
528 && ! ((defined YYMALLOC || defined malloc) \
529 && (defined YYFREE || defined free)))
530 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
531 # ifndef EXIT_SUCCESS
532 # define EXIT_SUCCESS 0
536 # define YYMALLOC malloc
537 # if ! defined malloc && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
538 || defined __cplusplus || defined _MSC_VER)
539 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
544 # if ! defined free && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
545 || defined __cplusplus || defined _MSC_VER)
546 void free (void *); /* INFRINGES ON USER NAME SPACE */
550 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
553 #if (! defined yyoverflow \
554 && (! defined __cplusplus \
555 || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
557 /* A type that is properly aligned for any stack member. */
560 yytype_int16 yyss_alloc;
564 /* The size of the maximum gap between one aligned stack and the next. */
565 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
567 /* The size of an array large to enough to hold all stacks, each with
569 # define YYSTACK_BYTES(N) \
570 ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
571 + YYSTACK_GAP_MAXIMUM)
573 # define YYCOPY_NEEDED 1
575 /* Relocate STACK from its old location to the new one. The
576 local variables YYSIZE and YYSTACKSIZE give the old and new number of
577 elements in the stack, and YYPTR gives the new location of the
578 stack. Advance YYPTR to a properly aligned location for the next
580 # define YYSTACK_RELOCATE(Stack_alloc, Stack) \
583 YYSIZE_T yynewbytes; \
584 YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \
585 Stack = &yyptr->Stack_alloc; \
586 yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
587 yyptr += yynewbytes / sizeof (*yyptr); \
593 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
594 /* Copy COUNT objects from SRC to DST. The source and destination do
597 # if defined __GNUC__ && 1 < __GNUC__
598 # define YYCOPY(Dst, Src, Count) \
599 __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src)))
601 # define YYCOPY(Dst, Src, Count) \
605 for (yyi = 0; yyi < (Count); yyi++) \
606 (Dst)[yyi] = (Src)[yyi]; \
611 #endif /* !YYCOPY_NEEDED */
613 /* YYFINAL -- State number of the termination state. */
615 /* YYLAST -- Last index in YYTABLE. */
618 /* YYNTOKENS -- Number of terminals. */
620 /* YYNNTS -- Number of nonterminals. */
622 /* YYNRULES -- Number of rules. */
624 /* YYNRULES -- Number of states. */
627 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
629 #define YYMAXUTOK 267
631 #define YYTRANSLATE(YYX) \
632 ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
634 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */
635 static const yytype_uint8 yytranslate[] =
637 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
638 13, 2, 2, 2, 2, 2, 2, 2, 2, 2,
639 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
640 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
641 2, 2, 2, 2, 15, 2, 2, 2, 16, 17,
642 2, 2, 2, 2, 2, 2, 2, 2, 14, 2,
643 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
644 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
645 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
646 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
647 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
648 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
649 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
650 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
651 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
652 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
653 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
654 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
655 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
656 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
657 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
658 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
659 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
660 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
661 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
662 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
663 5, 6, 7, 8, 9, 10, 11, 12
667 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
669 static const yytype_uint8 yyprhs[] =
671 0, 0, 3, 8, 12, 14, 18, 21, 24, 28,
672 29, 33, 35, 42, 43, 45, 46, 50, 52, 55,
673 57, 59, 61, 63, 66, 67, 71, 73, 76, 78,
677 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
678 static const yytype_int8 yyrhs[] =
680 19, 0, -1, 20, 9, 13, 22, -1, 20, 13,
681 21, -1, 21, -1, 3, 10, 8, -1, 4, 10,
682 -1, 5, 10, -1, 6, 10, 10, -1, -1, 22,
683 13, 23, -1, 23, -1, 25, 14, 24, 7, 24,
684 28, -1, -1, 10, -1, -1, 25, 15, 26, -1,
685 26, -1, 26, 27, -1, 27, -1, 16, -1, 17,
686 -1, 10, -1, 12, 29, -1, -1, 29, 15, 30,
687 -1, 30, -1, 30, 31, -1, 31, -1, 10, -1,
688 11, -1, 7, -1, 14, -1
691 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
692 static const yytype_uint16 yyrline[] =
694 0, 247, 247, 257, 258, 261, 263, 265, 267, 279,
695 282, 283, 286, 369, 372, 388, 391, 401, 408, 416,
696 420, 427, 434, 456, 459, 462, 472, 480, 488, 491,
701 #if YYDEBUG || YYERROR_VERBOSE || 0
702 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
703 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
704 static const char *const yytname[] =
706 "$end", "error", "$undefined", "kMASK", "kPREFIX", "kSUFFIX",
707 "kSYNONYM", "kID", "kNUMBER", "kPERCPERC", "kBITFIELD", "kCHAR",
708 "kSPACE", "'\\n'", "':'", "','", "'0'", "'1'", "$accept", "spec",
709 "masks", "mask", "instrs", "instr", "bitfieldopt", "bytes", "byte",
710 "bit", "optargs", "args", "arg", "argcomp", YY_NULL
715 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
717 static const yytype_uint16 yytoknum[] =
719 0, 256, 257, 258, 259, 260, 261, 262, 263, 264,
720 265, 266, 267, 10, 58, 44, 48, 49
724 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
725 static const yytype_uint8 yyr1[] =
727 0, 18, 19, 20, 20, 21, 21, 21, 21, 21,
728 22, 22, 23, 23, 24, 24, 25, 25, 26, 26,
729 27, 27, 27, 28, 28, 29, 29, 30, 30, 31,
733 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
734 static const yytype_uint8 yyr2[] =
736 0, 2, 4, 3, 1, 3, 2, 2, 3, 0,
737 3, 1, 6, 0, 1, 0, 3, 1, 2, 1,
738 1, 1, 1, 2, 0, 3, 1, 2, 1, 1,
742 /* YYDEFACT[STATE-NAME] -- Default reduction number in state STATE-NUM.
743 Performed when YYTABLE doesn't specify something else to do. Zero
744 means the default is an error. */
745 static const yytype_uint8 yydefact[] =
747 9, 0, 0, 0, 0, 0, 0, 4, 0, 6,
748 7, 0, 1, 0, 9, 5, 8, 13, 3, 22,
749 20, 21, 2, 11, 0, 17, 19, 13, 15, 0,
750 18, 10, 14, 0, 16, 15, 24, 0, 12, 31,
751 29, 30, 32, 23, 26, 28, 0, 27, 25
754 /* YYDEFGOTO[NTERM-NUM]. */
755 static const yytype_int8 yydefgoto[] =
757 -1, 5, 6, 7, 22, 23, 33, 24, 25, 26,
761 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
763 #define YYPACT_NINF -35
764 static const yytype_int8 yypact[] =
766 12, 9, 10, 11, 13, 22, -2, -35, 16, -35,
767 -35, 15, -35, 14, 12, -35, -35, -4, -35, -35,
768 -35, -35, 17, -35, -12, -4, -35, -4, 18, -4,
769 -35, -35, -35, 19, -4, 18, 20, -6, -35, -35,
770 -35, -35, -35, 21, -6, -35, -6, -35, -6
773 /* YYPGOTO[NTERM-NUM]. */
774 static const yytype_int8 yypgoto[] =
776 -35, -35, -35, 23, -35, 2, -1, -35, 4, -25,
780 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
781 positive, shift that token. If negative, reduce the rule which
782 number is the opposite. If YYTABLE_NINF, syntax error. */
783 #define YYTABLE_NINF -1
784 static const yytype_uint8 yytable[] =
786 30, 39, 28, 29, 40, 41, 19, 13, 42, 30,
787 47, 14, 20, 21, 47, 1, 2, 3, 4, 8,
788 9, 10, 12, 11, 15, 16, 35, 17, 32, 31,
789 27, 48, 37, 34, 36, 0, 46, 18
792 #define yypact_value_is_default(Yystate) \
793 (!!((Yystate) == (-35)))
795 #define yytable_value_is_error(Yytable_value) \
798 static const yytype_int8 yycheck[] =
800 25, 7, 14, 15, 10, 11, 10, 9, 14, 34,
801 44, 13, 16, 17, 48, 3, 4, 5, 6, 10,
802 10, 10, 0, 10, 8, 10, 7, 13, 10, 27,
803 13, 46, 12, 29, 35, -1, 15, 14
806 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
807 symbol of state STATE-NUM. */
808 static const yytype_uint8 yystos[] =
810 0, 3, 4, 5, 6, 19, 20, 21, 10, 10,
811 10, 10, 0, 9, 13, 8, 10, 13, 21, 10,
812 16, 17, 22, 23, 25, 26, 27, 13, 14, 15,
813 27, 23, 10, 24, 26, 7, 24, 12, 28, 7,
814 10, 11, 14, 29, 30, 31, 15, 31, 30
817 #define yyerrok (yyerrstatus = 0)
818 #define yyclearin (yychar = YYEMPTY)
822 #define YYACCEPT goto yyacceptlab
823 #define YYABORT goto yyabortlab
824 #define YYERROR goto yyerrorlab
827 /* Like YYERROR except do call yyerror. This remains here temporarily
828 to ease the transition to the new meaning of YYERROR, for GCC.
829 Once GCC version 2 has supplanted version 1, this can go. However,
830 YYFAIL appears to be in use. Nevertheless, it is formally deprecated
831 in Bison 2.4.2's NEWS entry, where a plan to phase it out is
834 #define YYFAIL goto yyerrlab
836 /* This is here to suppress warnings from the GCC cpp's
837 -Wunused-macros. Normally we don't worry about that warning, but
838 some users do, and we want to make it easy for users to remove
839 YYFAIL uses, which will produce warnings from Bison 2.5. */
842 #define YYRECOVERING() (!!yyerrstatus)
844 #define YYBACKUP(Token, Value) \
846 if (yychar == YYEMPTY) \
850 YYPOPSTACK (yylen); \
856 yyerror (YY_("syntax error: cannot back up")); \
861 /* Error token number */
863 #define YYERRCODE 256
866 /* This macro is provided for backward compatibility. */
867 #ifndef YY_LOCATION_PRINT
868 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
872 /* YYLEX -- calling `yylex' with the right arguments. */
874 # define YYLEX yylex (YYLEX_PARAM)
876 # define YYLEX yylex ()
879 /* Enable debugging if requested. */
883 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
884 # define YYFPRINTF fprintf
887 # define YYDPRINTF(Args) \
893 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
897 YYFPRINTF (stderr, "%s ", Title); \
898 yy_symbol_print (stderr, \
900 YYFPRINTF (stderr, "\n"); \
905 /*--------------------------------.
906 | Print this symbol on YYOUTPUT. |
907 `--------------------------------*/
910 #if (defined __STDC__ || defined __C99__FUNC__ \
911 || defined __cplusplus || defined _MSC_VER)
913 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
916 yy_symbol_value_print (yyoutput, yytype, yyvaluep)
919 YYSTYPE const * const yyvaluep;
922 FILE *yyo = yyoutput;
927 if (yytype < YYNTOKENS)
928 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
940 /*--------------------------------.
941 | Print this symbol on YYOUTPUT. |
942 `--------------------------------*/
944 #if (defined __STDC__ || defined __C99__FUNC__ \
945 || defined __cplusplus || defined _MSC_VER)
947 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
950 yy_symbol_print (yyoutput, yytype, yyvaluep)
953 YYSTYPE const * const yyvaluep;
956 if (yytype < YYNTOKENS)
957 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
959 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
961 yy_symbol_value_print (yyoutput, yytype, yyvaluep);
962 YYFPRINTF (yyoutput, ")");
965 /*------------------------------------------------------------------.
966 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
968 `------------------------------------------------------------------*/
970 #if (defined __STDC__ || defined __C99__FUNC__ \
971 || defined __cplusplus || defined _MSC_VER)
973 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
976 yy_stack_print (yybottom, yytop)
977 yytype_int16 *yybottom;
981 YYFPRINTF (stderr, "Stack now");
982 for (; yybottom <= yytop; yybottom++)
984 int yybot = *yybottom;
985 YYFPRINTF (stderr, " %d", yybot);
987 YYFPRINTF (stderr, "\n");
990 # define YY_STACK_PRINT(Bottom, Top) \
993 yy_stack_print ((Bottom), (Top)); \
997 /*------------------------------------------------.
998 | Report that the YYRULE is going to be reduced. |
999 `------------------------------------------------*/
1001 #if (defined __STDC__ || defined __C99__FUNC__ \
1002 || defined __cplusplus || defined _MSC_VER)
1004 yy_reduce_print (YYSTYPE *yyvsp, int yyrule)
1007 yy_reduce_print (yyvsp, yyrule)
1012 int yynrhs = yyr2[yyrule];
1014 unsigned long int yylno = yyrline[yyrule];
1015 YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
1017 /* The symbols being reduced. */
1018 for (yyi = 0; yyi < yynrhs; yyi++)
1020 YYFPRINTF (stderr, " $%d = ", yyi + 1);
1021 yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
1022 &(yyvsp[(yyi + 1) - (yynrhs)])
1024 YYFPRINTF (stderr, "\n");
1028 # define YY_REDUCE_PRINT(Rule) \
1031 yy_reduce_print (yyvsp, Rule); \
1034 /* Nonzero means print parse trace. It is left uninitialized so that
1035 multiple parsers can coexist. */
1037 #else /* !YYDEBUG */
1038 # define YYDPRINTF(Args)
1039 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
1040 # define YY_STACK_PRINT(Bottom, Top)
1041 # define YY_REDUCE_PRINT(Rule)
1042 #endif /* !YYDEBUG */
1045 /* YYINITDEPTH -- initial size of the parser's stacks. */
1047 # define YYINITDEPTH 200
1050 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1051 if the built-in stack extension method is used).
1053 Do not make this value too large; the results are undefined if
1054 YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1055 evaluated with infinite-precision integer arithmetic. */
1058 # define YYMAXDEPTH 10000
1065 # if defined __GLIBC__ && defined _STRING_H
1066 # define yystrlen strlen
1068 /* Return the length of YYSTR. */
1069 #if (defined __STDC__ || defined __C99__FUNC__ \
1070 || defined __cplusplus || defined _MSC_VER)
1072 yystrlen (const char *yystr)
1080 for (yylen = 0; yystr[yylen]; yylen++)
1088 # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1089 # define yystpcpy stpcpy
1091 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1093 #if (defined __STDC__ || defined __C99__FUNC__ \
1094 || defined __cplusplus || defined _MSC_VER)
1096 yystpcpy (char *yydest, const char *yysrc)
1099 yystpcpy (yydest, yysrc)
1105 const char *yys = yysrc;
1107 while ((*yyd++ = *yys++) != '\0')
1116 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1117 quotes and backslashes, so that it's suitable for yyerror. The
1118 heuristic is that double-quoting is unnecessary unless the string
1119 contains an apostrophe, a comma, or backslash (other than
1120 backslash-backslash). YYSTR is taken from yytname. If YYRES is
1121 null, do not copy; instead, return the length of what the result
1124 yytnamerr (char *yyres, const char *yystr)
1129 char const *yyp = yystr;
1136 goto do_not_strip_quotes;
1140 goto do_not_strip_quotes;
1153 do_not_strip_quotes: ;
1157 return yystrlen (yystr);
1159 return yystpcpy (yyres, yystr) - yyres;
1163 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1164 about the unexpected token YYTOKEN for the state stack whose top is
1167 Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is
1168 not large enough to hold the message. In that case, also set
1169 *YYMSG_ALLOC to the required number of bytes. Return 2 if the
1170 required number of bytes is too large to store. */
1172 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1173 yytype_int16 *yyssp, int yytoken)
1175 YYSIZE_T yysize0 = yytnamerr (YY_NULL, yytname[yytoken]);
1176 YYSIZE_T yysize = yysize0;
1177 enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1178 /* Internationalized format string. */
1179 const char *yyformat = YY_NULL;
1180 /* Arguments of yyformat. */
1181 char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1182 /* Number of reported tokens (one for the "unexpected", one per
1186 /* There are many possibilities here to consider:
1187 - Assume YYFAIL is not used. It's too flawed to consider. See
1188 <http://lists.gnu.org/archive/html/bison-patches/2009-12/msg00024.html>
1189 for details. YYERROR is fine as it does not invoke this
1191 - If this state is a consistent state with a default action, then
1192 the only way this function was invoked is if the default action
1193 is an error action. In that case, don't check for expected
1194 tokens because there are none.
1195 - The only way there can be no lookahead present (in yychar) is if
1196 this state is a consistent state with a default action. Thus,
1197 detecting the absence of a lookahead is sufficient to determine
1198 that there is no unexpected or expected token to report. In that
1199 case, just report a simple "syntax error".
1200 - Don't assume there isn't a lookahead just because this state is a
1201 consistent state with a default action. There might have been a
1202 previous inconsistent state, consistent state with a non-default
1203 action, or user semantic action that manipulated yychar.
1204 - Of course, the expected token list depends on states to have
1205 correct lookahead information, and it depends on the parser not
1206 to perform extra reductions after fetching a lookahead from the
1207 scanner and before detecting a syntax error. Thus, state merging
1208 (from LALR or IELR) and default reductions corrupt the expected
1209 token list. However, the list is correct for canonical LR with
1210 one exception: it will still contain any token that will not be
1211 accepted due to an error action in a later state.
1213 if (yytoken != YYEMPTY)
1215 int yyn = yypact[*yyssp];
1216 yyarg[yycount++] = yytname[yytoken];
1217 if (!yypact_value_is_default (yyn))
1219 /* Start YYX at -YYN if negative to avoid negative indexes in
1220 YYCHECK. In other words, skip the first -YYN actions for
1221 this state because they are default actions. */
1222 int yyxbegin = yyn < 0 ? -yyn : 0;
1223 /* Stay within bounds of both yycheck and yytname. */
1224 int yychecklim = YYLAST - yyn + 1;
1225 int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1228 for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1229 if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1230 && !yytable_value_is_error (yytable[yyx + yyn]))
1232 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1238 yyarg[yycount++] = yytname[yyx];
1240 YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULL, yytname[yyx]);
1241 if (! (yysize <= yysize1
1242 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1252 # define YYCASE_(N, S) \
1256 YYCASE_(0, YY_("syntax error"));
1257 YYCASE_(1, YY_("syntax error, unexpected %s"));
1258 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1259 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1260 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1261 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1266 YYSIZE_T yysize1 = yysize + yystrlen (yyformat);
1267 if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1272 if (*yymsg_alloc < yysize)
1274 *yymsg_alloc = 2 * yysize;
1275 if (! (yysize <= *yymsg_alloc
1276 && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1277 *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1281 /* Avoid sprintf, as that infringes on the user's name space.
1282 Don't have undefined behavior even if the translation
1283 produced a string with the wrong number of "%s"s. */
1287 while ((*yyp = *yyformat) != '\0')
1288 if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1290 yyp += yytnamerr (yyp, yyarg[yyi++]);
1301 #endif /* YYERROR_VERBOSE */
1303 /*-----------------------------------------------.
1304 | Release the memory associated to this symbol. |
1305 `-----------------------------------------------*/
1308 #if (defined __STDC__ || defined __C99__FUNC__ \
1309 || defined __cplusplus || defined _MSC_VER)
1311 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1314 yydestruct (yymsg, yytype, yyvaluep)
1324 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1337 /* The lookahead symbol. */
1341 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1342 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1343 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
1345 #ifndef YY_INITIAL_VALUE
1346 # define YY_INITIAL_VALUE(Value) /* Nothing. */
1349 /* The semantic value of the lookahead symbol. */
1350 YYSTYPE yylval YY_INITIAL_VALUE(yyval_default);
1352 /* Number of syntax errors so far. */
1360 #ifdef YYPARSE_PARAM
1361 #if (defined __STDC__ || defined __C99__FUNC__ \
1362 || defined __cplusplus || defined _MSC_VER)
1364 yyparse (void *YYPARSE_PARAM)
1367 yyparse (YYPARSE_PARAM)
1368 void *YYPARSE_PARAM;
1370 #else /* ! YYPARSE_PARAM */
1371 #if (defined __STDC__ || defined __C99__FUNC__ \
1372 || defined __cplusplus || defined _MSC_VER)
1383 /* Number of tokens to shift before error messages enabled. */
1386 /* The stacks and their tools:
1387 `yyss': related to states.
1388 `yyvs': related to semantic values.
1390 Refer to the stacks through separate pointers, to allow yyoverflow
1391 to reallocate them elsewhere. */
1393 /* The state stack. */
1394 yytype_int16 yyssa[YYINITDEPTH];
1396 yytype_int16 *yyssp;
1398 /* The semantic value stack. */
1399 YYSTYPE yyvsa[YYINITDEPTH];
1403 YYSIZE_T yystacksize;
1407 /* Lookahead token as an internal (translated) token number. */
1409 /* The variables used to return semantic value and location from the
1414 /* Buffer for error messages, and its allocated size. */
1416 char *yymsg = yymsgbuf;
1417 YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1420 #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
1422 /* The number of symbols on the RHS of the reduced rule.
1423 Keep to zero when no symbol should be popped. */
1426 yyssp = yyss = yyssa;
1427 yyvsp = yyvs = yyvsa;
1428 yystacksize = YYINITDEPTH;
1430 YYDPRINTF ((stderr, "Starting parse\n"));
1435 yychar = YYEMPTY; /* Cause a token to be read. */
1438 /*------------------------------------------------------------.
1439 | yynewstate -- Push a new state, which is found in yystate. |
1440 `------------------------------------------------------------*/
1442 /* In all cases, when you get here, the value and location stacks
1443 have just been pushed. So pushing a state here evens the stacks. */
1449 if (yyss + yystacksize - 1 <= yyssp)
1451 /* Get the current used size of the three stacks, in elements. */
1452 YYSIZE_T yysize = yyssp - yyss + 1;
1456 /* Give user a chance to reallocate the stack. Use copies of
1457 these so that the &'s don't force the real ones into
1459 YYSTYPE *yyvs1 = yyvs;
1460 yytype_int16 *yyss1 = yyss;
1462 /* Each stack pointer address is followed by the size of the
1463 data in use in that stack, in bytes. This used to be a
1464 conditional around just the two extra args, but that might
1465 be undefined if yyoverflow is a macro. */
1466 yyoverflow (YY_("memory exhausted"),
1467 &yyss1, yysize * sizeof (*yyssp),
1468 &yyvs1, yysize * sizeof (*yyvsp),
1474 #else /* no yyoverflow */
1475 # ifndef YYSTACK_RELOCATE
1476 goto yyexhaustedlab;
1478 /* Extend the stack our own way. */
1479 if (YYMAXDEPTH <= yystacksize)
1480 goto yyexhaustedlab;
1482 if (YYMAXDEPTH < yystacksize)
1483 yystacksize = YYMAXDEPTH;
1486 yytype_int16 *yyss1 = yyss;
1487 union yyalloc *yyptr =
1488 (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1490 goto yyexhaustedlab;
1491 YYSTACK_RELOCATE (yyss_alloc, yyss);
1492 YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1493 # undef YYSTACK_RELOCATE
1495 YYSTACK_FREE (yyss1);
1498 #endif /* no yyoverflow */
1500 yyssp = yyss + yysize - 1;
1501 yyvsp = yyvs + yysize - 1;
1503 YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1504 (unsigned long int) yystacksize));
1506 if (yyss + yystacksize - 1 <= yyssp)
1510 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1512 if (yystate == YYFINAL)
1522 /* Do appropriate processing given the current state. Read a
1523 lookahead token if we need one and don't already have one. */
1525 /* First try to decide what to do without reference to lookahead token. */
1526 yyn = yypact[yystate];
1527 if (yypact_value_is_default (yyn))
1530 /* Not known => get a lookahead token if don't already have one. */
1532 /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */
1533 if (yychar == YYEMPTY)
1535 YYDPRINTF ((stderr, "Reading a token: "));
1539 if (yychar <= YYEOF)
1541 yychar = yytoken = YYEOF;
1542 YYDPRINTF ((stderr, "Now at end of input.\n"));
1546 yytoken = YYTRANSLATE (yychar);
1547 YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1550 /* If the proper action on seeing token YYTOKEN is to reduce or to
1551 detect an error, take that action. */
1553 if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1558 if (yytable_value_is_error (yyn))
1564 /* Count tokens shifted since error; after three, turn off error
1569 /* Shift the lookahead token. */
1570 YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1572 /* Discard the shifted token. */
1576 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1578 YY_IGNORE_MAYBE_UNINITIALIZED_END
1583 /*-----------------------------------------------------------.
1584 | yydefault -- do the default action for the current state. |
1585 `-----------------------------------------------------------*/
1587 yyn = yydefact[yystate];
1593 /*-----------------------------.
1594 | yyreduce -- Do a reduction. |
1595 `-----------------------------*/
1597 /* yyn is the number of a rule to reduce with. */
1600 /* If YYLEN is nonzero, implement the default value of the action:
1603 Otherwise, the following line sets YYVAL to garbage.
1604 This behavior is undocumented and Bison
1605 users should not rely upon it. Assigning to YYVAL
1606 unconditionally makes the parser a bit smaller, and it avoids a
1607 GCC warning that YYVAL may be used uninitialized. */
1608 yyval = yyvsp[1-yylen];
1611 YY_REDUCE_PRINT (yyn);
1615 /* Line 1792 of yacc.c */
1616 #line 248 "i386_parse.y"
1618 if (error_message_count != 0)
1619 error (EXIT_FAILURE, 0,
1620 "terminated due to previous error");
1627 /* Line 1792 of yacc.c */
1628 #line 262 "i386_parse.y"
1629 { new_bitfield ((yyvsp[(2) - (3)].str), (yyvsp[(3) - (3)].num)); }
1633 /* Line 1792 of yacc.c */
1634 #line 264 "i386_parse.y"
1635 { new_bitfield ((yyvsp[(2) - (2)].str), -1); }
1639 /* Line 1792 of yacc.c */
1640 #line 266 "i386_parse.y"
1641 { new_bitfield ((yyvsp[(2) - (2)].str), -2); }
1645 /* Line 1792 of yacc.c */
1646 #line 268 "i386_parse.y"
1648 struct synonym *newp = xmalloc (sizeof (*newp));
1649 newp->from = (yyvsp[(2) - (3)].str);
1650 newp->to = (yyvsp[(3) - (3)].str);
1651 if (tfind (newp, &synonyms, compare_syn) != NULL)
1653 "%d: duplicate definition for synonym '%s'",
1654 i386_lineno, (yyvsp[(2) - (3)].str));
1655 else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
1656 error (EXIT_FAILURE, 0, "tsearch");
1661 /* Line 1792 of yacc.c */
1662 #line 287 "i386_parse.y"
1664 if ((yyvsp[(3) - (6)].field) != NULL && strcmp ((yyvsp[(3) - (6)].field)->name, "RE") != 0
1665 && strcmp ((yyvsp[(3) - (6)].field)->name, "R") != 0)
1667 error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
1670 if (check_duplicates ((yyvsp[(1) - (6)].bit)) == 0
1671 && check_argsdef ((yyvsp[(1) - (6)].bit), (yyvsp[(6) - (6)].arg)) == 0
1672 && check_bitsused ((yyvsp[(1) - (6)].bit), (yyvsp[(5) - (6)].field), (yyvsp[(6) - (6)].arg)) == 0)
1674 struct instruction *newp = xcalloc (sizeof (*newp),
1676 if ((yyvsp[(3) - (6)].field) != NULL)
1678 if (strcmp ((yyvsp[(3) - (6)].field)->name, "RE") == 0)
1680 else if (strcmp ((yyvsp[(3) - (6)].field)->name, "R") == 0)
1684 newp->bytes = (yyvsp[(1) - (6)].bit);
1685 newp->mnemonic = (yyvsp[(4) - (6)].str);
1686 if (newp->mnemonic != (void *) -1l
1687 && tfind ((yyvsp[(4) - (6)].str), &mnemonics,
1688 (comparison_fn_t) strcmp) == NULL)
1690 if (tsearch ((yyvsp[(4) - (6)].str), &mnemonics,
1691 (comparison_fn_t) strcmp) == NULL)
1692 error (EXIT_FAILURE, errno, "tsearch");
1696 if ((yyvsp[(5) - (6)].field) != NULL)
1698 if (strcmp ((yyvsp[(5) - (6)].field)->name, "w") == 0)
1699 newp->suffix = suffix_w;
1700 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w0") == 0)
1701 newp->suffix = suffix_w0;
1702 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "tttn") == 0)
1703 newp->suffix = suffix_tttn;
1704 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w1") == 0)
1705 newp->suffix = suffix_w1;
1706 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W") == 0)
1707 newp->suffix = suffix_W;
1708 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W1") == 0)
1709 newp->suffix = suffix_W1;
1710 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "D") == 0)
1711 newp->suffix = suffix_D;
1713 error (EXIT_FAILURE, 0,
1714 "%s: %d: unknown suffix '%s'",
1715 infname, i386_lineno - 1, (yyvsp[(5) - (6)].field)->name);
1717 struct suffix search = { .name = (yyvsp[(5) - (6)].field)->name };
1718 if (tfind (&search, &suffixes, compare_suf)
1721 struct suffix *ns = xmalloc (sizeof (*ns));
1722 ns->name = (yyvsp[(5) - (6)].field)->name;
1723 ns->idx = ++nsuffixes;
1724 if (tsearch (ns, &suffixes, compare_suf)
1726 error (EXIT_FAILURE, errno, "tsearch");
1730 struct argument *args = (yyvsp[(6) - (6)].arg);
1732 while (args != NULL)
1734 fillin_arg ((yyvsp[(1) - (6)].bit), args->name, newp, n);
1740 newp->next = instructions;
1741 instructions = newp;
1748 /* Line 1792 of yacc.c */
1749 #line 373 "i386_parse.y"
1751 struct known_bitfield search;
1752 search.name = (yyvsp[(1) - (1)].str);
1753 struct known_bitfield **res;
1754 res = tfind (&search, &bitfields, bitfield_compare);
1757 error (0, 0, "%d: unknown bitfield '%s'",
1758 i386_lineno, search.name);
1759 (yyval.field) = NULL;
1762 (yyval.field) = *res;
1767 /* Line 1792 of yacc.c */
1768 #line 388 "i386_parse.y"
1769 { (yyval.field) = NULL; }
1773 /* Line 1792 of yacc.c */
1774 #line 392 "i386_parse.y"
1776 check_bits ((yyvsp[(3) - (3)].bit));
1778 struct bitvalue *runp = (yyvsp[(1) - (3)].bit);
1779 while (runp->next != NULL)
1781 runp->next = (yyvsp[(3) - (3)].bit);
1782 (yyval.bit) = (yyvsp[(1) - (3)].bit);
1787 /* Line 1792 of yacc.c */
1788 #line 402 "i386_parse.y"
1790 check_bits ((yyvsp[(1) - (1)].bit));
1791 (yyval.bit) = (yyvsp[(1) - (1)].bit);
1796 /* Line 1792 of yacc.c */
1797 #line 409 "i386_parse.y"
1799 struct bitvalue *runp = (yyvsp[(1) - (2)].bit);
1800 while (runp->next != NULL)
1802 runp->next = (yyvsp[(2) - (2)].bit);
1803 (yyval.bit) = (yyvsp[(1) - (2)].bit);
1808 /* Line 1792 of yacc.c */
1809 #line 417 "i386_parse.y"
1810 { (yyval.bit) = (yyvsp[(1) - (1)].bit); }
1814 /* Line 1792 of yacc.c */
1815 #line 421 "i386_parse.y"
1817 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1818 (yyval.bit)->type = zeroone;
1819 (yyval.bit)->value = 0;
1820 (yyval.bit)->next = NULL;
1825 /* Line 1792 of yacc.c */
1826 #line 428 "i386_parse.y"
1828 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1829 (yyval.bit)->type = zeroone;
1830 (yyval.bit)->value = 1;
1831 (yyval.bit)->next = NULL;
1836 /* Line 1792 of yacc.c */
1837 #line 435 "i386_parse.y"
1839 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1840 struct known_bitfield search;
1841 search.name = (yyvsp[(1) - (1)].str);
1842 struct known_bitfield **res;
1843 res = tfind (&search, &bitfields, bitfield_compare);
1846 error (0, 0, "%d: unknown bitfield '%s'",
1847 i386_lineno, search.name);
1848 (yyval.bit)->type = failure;
1852 (yyval.bit)->type = field;
1853 (yyval.bit)->field = *res;
1855 (yyval.bit)->next = NULL;
1860 /* Line 1792 of yacc.c */
1861 #line 457 "i386_parse.y"
1862 { (yyval.arg) = (yyvsp[(2) - (2)].arg); }
1866 /* Line 1792 of yacc.c */
1867 #line 459 "i386_parse.y"
1868 { (yyval.arg) = NULL; }
1872 /* Line 1792 of yacc.c */
1873 #line 463 "i386_parse.y"
1875 struct argument *runp = (yyvsp[(1) - (3)].arg);
1876 while (runp->next != NULL)
1878 runp->next = xmalloc (sizeof (struct argument));
1879 runp->next->name = combine ((yyvsp[(3) - (3)].name));
1880 runp->next->next = NULL;
1881 (yyval.arg) = (yyvsp[(1) - (3)].arg);
1886 /* Line 1792 of yacc.c */
1887 #line 473 "i386_parse.y"
1889 (yyval.arg) = xmalloc (sizeof (struct argument));
1890 (yyval.arg)->name = combine ((yyvsp[(1) - (1)].name));
1891 (yyval.arg)->next = NULL;
1896 /* Line 1792 of yacc.c */
1897 #line 481 "i386_parse.y"
1899 struct argname *runp = (yyvsp[(1) - (2)].name);
1900 while (runp->next != NULL)
1902 runp->next = (yyvsp[(2) - (2)].name);
1903 (yyval.name) = (yyvsp[(1) - (2)].name);
1908 /* Line 1792 of yacc.c */
1909 #line 489 "i386_parse.y"
1910 { (yyval.name) = (yyvsp[(1) - (1)].name); }
1914 /* Line 1792 of yacc.c */
1915 #line 492 "i386_parse.y"
1917 (yyval.name) = xmalloc (sizeof (struct argname));
1918 (yyval.name)->type = nfield;
1919 (yyval.name)->next = NULL;
1921 struct known_bitfield search;
1922 search.name = (yyvsp[(1) - (1)].str);
1923 struct known_bitfield **res;
1924 res = tfind (&search, &bitfields, bitfield_compare);
1927 if (strcmp ((yyvsp[(1) - (1)].str), "ax") == 0)
1928 (yyval.name)->field = &ax_reg;
1929 else if (strcmp ((yyvsp[(1) - (1)].str), "dx") == 0)
1930 (yyval.name)->field = &dx_reg;
1931 else if (strcmp ((yyvsp[(1) - (1)].str), "es_di") == 0)
1932 (yyval.name)->field = &di_reg;
1933 else if (strcmp ((yyvsp[(1) - (1)].str), "ds_si") == 0)
1934 (yyval.name)->field = &si_reg;
1935 else if (strcmp ((yyvsp[(1) - (1)].str), "ds_bx") == 0)
1936 (yyval.name)->field = &bx_reg;
1939 error (0, 0, "%d: unknown bitfield '%s'",
1940 i386_lineno, search.name);
1941 (yyval.name)->field = NULL;
1945 (yyval.name)->field = *res;
1950 /* Line 1792 of yacc.c */
1951 #line 524 "i386_parse.y"
1953 (yyval.name) = xmalloc (sizeof (struct argname));
1954 (yyval.name)->type = string;
1955 (yyval.name)->next = NULL;
1956 (yyval.name)->str = xmalloc (2);
1957 (yyval.name)->str[0] = (yyvsp[(1) - (1)].ch);
1958 (yyval.name)->str[1] = '\0';
1963 /* Line 1792 of yacc.c */
1964 #line 533 "i386_parse.y"
1966 (yyval.name) = xmalloc (sizeof (struct argname));
1967 (yyval.name)->type = string;
1968 (yyval.name)->next = NULL;
1969 (yyval.name)->str = (yyvsp[(1) - (1)].str);
1974 /* Line 1792 of yacc.c */
1975 #line 540 "i386_parse.y"
1977 (yyval.name) = xmalloc (sizeof (struct argname));
1978 (yyval.name)->type = string;
1979 (yyval.name)->next = NULL;
1980 (yyval.name)->str = xmalloc (2);
1981 (yyval.name)->str[0] = ':';
1982 (yyval.name)->str[1] = '\0';
1987 /* Line 1792 of yacc.c */
1988 #line 1989 "i386_parse.c"
1991 /* User semantic actions sometimes alter yychar, and that requires
1992 that yytoken be updated with the new translation. We take the
1993 approach of translating immediately before every use of yytoken.
1994 One alternative is translating here after every semantic action,
1995 but that translation would be missed if the semantic action invokes
1996 YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
1997 if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an
1998 incorrect destructor might then be invoked immediately. In the
1999 case of YYERROR or YYBACKUP, subsequent parser actions might lead
2000 to an incorrect destructor call or verbose syntax error message
2001 before the lookahead is translated. */
2002 YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
2006 YY_STACK_PRINT (yyss, yyssp);
2010 /* Now `shift' the result of the reduction. Determine what state
2011 that goes to, based on the state we popped back to and the rule
2012 number reduced by. */
2016 yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
2017 if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
2018 yystate = yytable[yystate];
2020 yystate = yydefgoto[yyn - YYNTOKENS];
2025 /*------------------------------------.
2026 | yyerrlab -- here on detecting error |
2027 `------------------------------------*/
2029 /* Make sure we have latest lookahead translation. See comments at
2030 user semantic actions for why this is necessary. */
2031 yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
2033 /* If not already recovering from an error, report this error. */
2037 #if ! YYERROR_VERBOSE
2038 yyerror (YY_("syntax error"));
2040 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
2043 char const *yymsgp = YY_("syntax error");
2044 int yysyntax_error_status;
2045 yysyntax_error_status = YYSYNTAX_ERROR;
2046 if (yysyntax_error_status == 0)
2048 else if (yysyntax_error_status == 1)
2050 if (yymsg != yymsgbuf)
2051 YYSTACK_FREE (yymsg);
2052 yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
2056 yymsg_alloc = sizeof yymsgbuf;
2057 yysyntax_error_status = 2;
2061 yysyntax_error_status = YYSYNTAX_ERROR;
2066 if (yysyntax_error_status == 2)
2067 goto yyexhaustedlab;
2069 # undef YYSYNTAX_ERROR
2075 if (yyerrstatus == 3)
2077 /* If just tried and failed to reuse lookahead token after an
2078 error, discard it. */
2080 if (yychar <= YYEOF)
2082 /* Return failure if at end of input. */
2083 if (yychar == YYEOF)
2088 yydestruct ("Error: discarding",
2094 /* Else will try to reuse lookahead token after shifting the error
2099 /*---------------------------------------------------.
2100 | yyerrorlab -- error raised explicitly by YYERROR. |
2101 `---------------------------------------------------*/
2104 /* Pacify compilers like GCC when the user code never invokes
2105 YYERROR and the label yyerrorlab therefore never appears in user
2107 if (/*CONSTCOND*/ 0)
2110 /* Do not reclaim the symbols of the rule which action triggered
2114 YY_STACK_PRINT (yyss, yyssp);
2119 /*-------------------------------------------------------------.
2120 | yyerrlab1 -- common code for both syntax error and YYERROR. |
2121 `-------------------------------------------------------------*/
2123 yyerrstatus = 3; /* Each real token shifted decrements this. */
2127 yyn = yypact[yystate];
2128 if (!yypact_value_is_default (yyn))
2131 if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
2139 /* Pop the current state because it cannot handle the error token. */
2144 yydestruct ("Error: popping",
2145 yystos[yystate], yyvsp);
2148 YY_STACK_PRINT (yyss, yyssp);
2151 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
2153 YY_IGNORE_MAYBE_UNINITIALIZED_END
2156 /* Shift the error token. */
2157 YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
2163 /*-------------------------------------.
2164 | yyacceptlab -- YYACCEPT comes here. |
2165 `-------------------------------------*/
2170 /*-----------------------------------.
2171 | yyabortlab -- YYABORT comes here. |
2172 `-----------------------------------*/
2177 #if !defined yyoverflow || YYERROR_VERBOSE
2178 /*-------------------------------------------------.
2179 | yyexhaustedlab -- memory exhaustion comes here. |
2180 `-------------------------------------------------*/
2182 yyerror (YY_("memory exhausted"));
2188 if (yychar != YYEMPTY)
2190 /* Make sure we have latest lookahead translation. See comments at
2191 user semantic actions for why this is necessary. */
2192 yytoken = YYTRANSLATE (yychar);
2193 yydestruct ("Cleanup: discarding lookahead",
2196 /* Do not reclaim the symbols of the rule which action triggered
2197 this YYABORT or YYACCEPT. */
2199 YY_STACK_PRINT (yyss, yyssp);
2200 while (yyssp != yyss)
2202 yydestruct ("Cleanup: popping",
2203 yystos[*yyssp], yyvsp);
2208 YYSTACK_FREE (yyss);
2211 if (yymsg != yymsgbuf)
2212 YYSTACK_FREE (yymsg);
2214 /* Make sure YYID is used. */
2215 return YYID (yyresult);
2219 /* Line 2055 of yacc.c */
2220 #line 550 "i386_parse.y"
2224 yyerror (const char *s)
2226 error (0, 0, gettext ("while reading i386 CPU description: %s at line %d"),
2227 gettext (s), i386_lineno);
2232 bitfield_compare (const void *p1, const void *p2)
2234 struct known_bitfield *f1 = (struct known_bitfield *) p1;
2235 struct known_bitfield *f2 = (struct known_bitfield *) p2;
2237 return strcmp (f1->name, f2->name);
2242 new_bitfield (char *name, unsigned long int num)
2244 struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
2249 if (tfind (newp, &bitfields, bitfield_compare) != NULL)
2251 error (0, 0, "%d: duplicated definition of bitfield '%s'",
2257 if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
2258 error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
2263 /* Check that the number of bits is a multiple of 8. */
2265 check_bits (struct bitvalue *val)
2267 struct bitvalue *runp = val;
2268 unsigned int total = 0;
2270 while (runp != NULL)
2272 if (runp->type == zeroone)
2274 else if (runp->field == NULL)
2275 /* No sense doing anything, the field is not known. */
2278 total += runp->field->bits;
2290 if (val->type == zeroone)
2291 obstack_printf (&os, "%u", val->value);
2293 obstack_printf (&os, "{%s}", val->field->name);
2296 obstack_1grow (&os, '\0');
2298 error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
2299 i386_lineno, (char *) obstack_finish (&os));
2301 obstack_free (&os, NULL);
2307 check_duplicates (struct bitvalue *val)
2315 if (val->type == field && val->field != NULL)
2317 if (val->field->tmp == testcnt)
2319 error (0, 0, "%d: bitfield '%s' used more than once",
2320 i386_lineno - 1, val->field->name);
2323 val->field->tmp = testcnt;
2334 check_argsdef (struct bitvalue *bitval, struct argument *args)
2338 while (args != NULL)
2340 for (struct argname *name = args->name; name != NULL; name = name->next)
2341 if (name->type == nfield && name->field != NULL
2342 && name->field != &ax_reg && name->field != &dx_reg
2343 && name->field != &di_reg && name->field != &si_reg
2344 && name->field != &bx_reg)
2346 struct bitvalue *runp = bitval;
2348 while (runp != NULL)
2349 if (runp->type == field && runp->field == name->field)
2356 error (0, 0, "%d: unknown bitfield '%s' used in output format",
2357 i386_lineno - 1, name->field->name);
2370 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
2371 struct argument *args)
2375 while (bitval != NULL)
2377 if (bitval->type == field && bitval->field != NULL
2378 && bitval->field != suffix
2379 /* {w} is handled special. */
2380 && strcmp (bitval->field->name, "w") != 0)
2382 struct argument *runp;
2383 for (runp = args; runp != NULL; runp = runp->next)
2385 struct argname *name = runp->name;
2387 while (name != NULL)
2388 if (name->type == nfield && name->field == bitval->field)
2400 error (0, 0, "%d: bitfield '%s' not used",
2401 i386_lineno - 1, bitval->field->name);
2407 bitval = bitval->next;
2414 static struct argname *
2415 combine (struct argname *name)
2417 struct argname *last_str = NULL;
2418 for (struct argname *runp = name; runp != NULL; runp = runp->next)
2420 if (runp->type == string)
2422 if (last_str == NULL)
2426 last_str->str = xrealloc (last_str->str,
2427 strlen (last_str->str)
2428 + strlen (runp->str) + 1);
2429 strcat (last_str->str, runp->str);
2430 last_str->next = runp->next;
2440 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
2444 fillin_arg (struct bitvalue *bytes, struct argname *name,
2445 struct instruction *instr, int n)
2447 static struct obstack ob;
2448 static int initialized;
2455 struct argname *runp = name;
2457 while (runp != NULL)
2459 /* We ignore strings in the function name. */
2460 if (runp->type == string)
2462 if (instr->operands[n].str != NULL)
2463 error (EXIT_FAILURE, 0,
2464 "%d: cannot have more than one string parameter",
2467 instr->operands[n].str = runp->str;
2471 assert (runp->type == nfield);
2473 /* Construct the function name. */
2475 obstack_1grow (&ob, '$');
2477 if (runp->field == NULL)
2478 /* Add some string which contains invalid characters. */
2479 obstack_grow_str (&ob, "!!!INVALID!!!");
2482 char *fieldname = runp->field->name;
2484 struct synonym search = { .from = fieldname };
2486 struct synonym **res = tfind (&search, &synonyms, compare_syn);
2488 fieldname = (*res)->to;
2490 obstack_grow_str (&ob, fieldname);
2493 /* Now compute the bit offset of the field. */
2494 struct bitvalue *b = bytes;
2496 if (runp->field != NULL)
2499 if (b->type == field && b->field != NULL)
2501 if (strcmp (b->field->name, runp->field->name) == 0)
2503 bitoff += b->field->bits;
2510 if (instr->operands[n].off1 == 0)
2511 instr->operands[n].off1 = bitoff;
2512 else if (instr->operands[n].off2 == 0)
2513 instr->operands[n].off2 = bitoff;
2514 else if (instr->operands[n].off3 == 0)
2515 instr->operands[n].off3 = bitoff;
2517 error (EXIT_FAILURE, 0,
2518 "%d: cannot have more than three fields in parameter",
2521 if (runp->field != NULL
2522 && strncasecmp (runp->field->name, "mod", 3) == 0)
2528 if (obstack_object_size (&ob) == 0)
2529 obstack_grow_str (&ob, "string");
2530 obstack_1grow (&ob, '\0');
2531 char *fct = obstack_finish (&ob);
2533 instr->operands[n].fct = fct;
2539 nameout (const void *nodep, VISIT value, int level)
2541 if (value == leaf || value == postorder)
2542 printf (" %s\n", *(const char **) nodep);
2548 compare_argstring (const void *p1, const void *p2)
2550 const struct argstring *a1 = (const struct argstring *) p1;
2551 const struct argstring *a2 = (const struct argstring *) p2;
2553 return strcmp (a1->str, a2->str);
2557 static int maxoff[3][3];
2558 static int minoff[3][3] = { { 1000, 1000, 1000 },
2559 { 1000, 1000, 1000 },
2560 { 1000, 1000, 1000 } };
2561 static int nbitoff[3][3];
2562 static void *fct_names[3];
2563 static int nbitfct[3];
2565 static void *strs[3];
2566 static int nbitstr[3];
2567 static int total_bits = 2; // Already counted the rep/repe bits.
2572 int nfct_names[3] = { 0, 0, 0 };
2573 int nstrs[3] = { 0, 0, 0 };
2575 /* We reverse the order of the instruction list while processing it.
2576 Later phases need it in the order in which the input file has
2578 struct instruction *reversed = NULL;
2580 struct instruction *runp = instructions;
2581 while (runp != NULL)
2583 for (int i = 0; i < 3; ++i)
2584 if (runp->operands[i].fct != NULL)
2586 struct argstring search = { .str = runp->operands[i].fct };
2587 if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
2589 struct argstring *newp = xmalloc (sizeof (*newp));
2590 newp->str = runp->operands[i].fct;
2592 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
2593 error (EXIT_FAILURE, errno, "tsearch");
2597 if (runp->operands[i].str != NULL)
2599 search.str = runp->operands[i].str;
2600 if (tfind (&search, &strs[i], compare_argstring) == NULL)
2602 struct argstring *newp = xmalloc (sizeof (*newp));
2603 newp->str = runp->operands[i].str;
2605 if (tsearch (newp, &strs[i], compare_argstring) == NULL)
2606 error (EXIT_FAILURE, errno, "tsearch");
2611 maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
2612 maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
2613 maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
2615 if (runp->operands[i].off1 > 0)
2616 minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
2617 if (runp->operands[i].off2 > 0)
2618 minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
2619 if (runp->operands[i].off3 > 0)
2620 minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
2623 struct instruction *old = runp;
2626 old->next = reversed;
2629 instructions = reversed;
2633 for (int i = 0; i < 3; ++i)
2635 // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
2636 // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
2638 if (minoff[i][0] == 1000)
2643 d = maxoff[i][0] - minoff[i][0];
2650 total_bits += nbitoff[i][0];
2653 if (minoff[i][1] == 1000)
2658 d = maxoff[i][1] - minoff[i][1];
2665 total_bits += nbitoff[i][1];
2668 if (minoff[i][2] == 1000)
2673 d = maxoff[i][2] - minoff[i][2];
2680 total_bits += nbitoff[i][2];
2682 // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
2692 total_bits += nbitfct[i];
2693 // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
2705 total_bits += nbitstr[i];
2708 // twalk (fct_names[i], nameout);
2719 total_bits += nbitsuf;
2720 // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
2725 compare_syn (const void *p1, const void *p2)
2727 const struct synonym *s1 = (const struct synonym *) p1;
2728 const struct synonym *s2 = (const struct synonym *) p2;
2730 return strcmp (s1->from, s2->from);
2735 compare_suf (const void *p1, const void *p2)
2737 const struct suffix *s1 = (const struct suffix *) p1;
2738 const struct suffix *s2 = (const struct suffix *) p2;
2740 return strcmp (s1->name, s2->name);
2744 static int count_op_str;
2745 static int off_op_str;
2747 print_op_str (const void *nodep, VISIT value,
2748 int level __attribute__ ((unused)))
2750 if (value == leaf || value == postorder)
2752 const char *str = (*(struct argstring **) nodep)->str;
2753 fprintf (outfile, "%s\n \"%s",
2754 count_op_str == 0 ? "" : "\\0\"", str);
2755 (*(struct argstring **) nodep)->idx = ++count_op_str;
2756 (*(struct argstring **) nodep)->off = off_op_str;
2757 off_op_str += strlen (str) + 1;
2763 print_op_str_idx (const void *nodep, VISIT value,
2764 int level __attribute__ ((unused)))
2766 if (value == leaf || value == postorder)
2767 printf (" %d,\n", (*(struct argstring **) nodep)->off);
2772 print_op_fct (const void *nodep, VISIT value,
2773 int level __attribute__ ((unused)))
2775 if (value == leaf || value == postorder)
2777 fprintf (outfile, " FCT_%s,\n", (*(struct argstring **) nodep)->str);
2778 (*(struct argstring **) nodep)->idx = ++count_op_str;
2784 # error "bogus NMNES value"
2788 instrtable_out (void)
2793 create_mnemonic_table ();
2795 fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
2797 fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
2798 lrint (ceil (log2 (NMNES))));
2800 fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
2801 for (int i = 0; i < 3; ++i)
2803 fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
2804 if (nbitstr[i] != 0)
2805 fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
2806 fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
2807 fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
2808 if (nbitoff[i][1] != 0)
2810 fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
2811 fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
2813 if (nbitoff[i][2] != 0)
2815 fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
2816 fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
2820 fputs ("\n#include <i386_data.h>\n\n", outfile);
2823 #define APPEND(a, b) APPEND_ (a, b)
2824 #define APPEND_(a, b) a##b
2825 #define EMIT_SUFFIX(suf) \
2826 fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
2836 fputc_unlocked ('\n', outfile);
2838 for (int i = 0; i < 3; ++i)
2842 fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n NULL,\n",
2844 twalk (fct_names[i], print_op_fct);
2845 fputs ("};\n", outfile);
2847 /* The operand strings. */
2848 if (nbitstr[i] != 0)
2852 fprintf (outfile, "static const char op%d_str[] =", i + 1);
2853 twalk (strs[i], print_op_str);
2854 fputs ("\";\n", outfile);
2856 fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
2858 twalk (strs[i], print_op_str_idx);
2859 fputs ("};\n", outfile);
2864 fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
2865 struct instruction *instr;
2866 for (instr = instructions; instr != NULL; instr = instr->next)
2868 fputs (" {", outfile);
2869 if (instr->mnemonic == (void *) -1l)
2870 fputs (" .mnemonic = MNE_INVALID,", outfile);
2872 fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
2873 fprintf (outfile, " .rep = %d,", instr->rep);
2874 fprintf (outfile, " .repe = %d,", instr->repe);
2875 fprintf (outfile, " .suffix = %d,", instr->suffix);
2876 fprintf (outfile, " .modrm = %d,", instr->modrm);
2878 for (int i = 0; i < 3; ++i)
2881 if (instr->operands[i].fct != NULL)
2883 struct argstring search = { .str = instr->operands[i].fct };
2884 struct argstring **res = tfind (&search, &fct_names[i],
2886 assert (res != NULL);
2889 fprintf (outfile, " .fct%d = %d,", i + 1, idx);
2892 if (instr->operands[i].str != NULL)
2894 struct argstring search = { .str = instr->operands[i].str };
2895 struct argstring **res = tfind (&search, &strs[i],
2897 assert (res != NULL);
2900 if (nbitstr[i] != 0)
2901 fprintf (outfile, " .str%d = %d,", i + 1, idx);
2903 fprintf (outfile, " .off%d_1 = %d,", i + 1,
2904 MAX (0, instr->operands[i].off1 - minoff[i][0]));
2906 if (nbitoff[i][1] != 0)
2907 fprintf (outfile, " .off%d_2 = %d,", i + 1,
2908 MAX (0, instr->operands[i].off2 - minoff[i][1]));
2910 if (nbitoff[i][2] != 0)
2911 fprintf (outfile, " .off%d_3 = %d,", i + 1,
2912 MAX (0, instr->operands[i].off3 - minoff[i][2]));
2915 fputs (" },\n", outfile);
2917 fputs ("};\n", outfile);
2919 fputs ("static const uint8_t match_data[] =\n{\n", outfile);
2921 for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
2923 /* First count the number of bytes. */
2924 size_t totalbits = 0;
2925 size_t zerobits = 0;
2926 bool leading_p = true;
2927 size_t leadingbits = 0;
2928 struct bitvalue *b = instr->bytes;
2931 if (b->type == zeroone)
2940 totalbits += b->field->bits;
2941 /* We must always count the mod/rm byte. */
2942 if (strncasecmp (b->field->name, "mod", 3) == 0)
2945 zerobits += b->field->bits;
2950 size_t nbytes = (totalbits - zerobits + 7) / 8;
2951 assert (nbytes > 0);
2952 size_t leadingbytes = leadingbits / 8;
2954 fprintf (outfile, " %#zx,", nbytes | (leadingbytes << 4));
2956 /* Now create the mask and byte values. */
2963 if (b->type == zeroone)
2965 byte = (byte << 1) | b->value;
2966 mask = (mask << 1) | 1;
2969 if (leadingbytes > 0)
2971 assert (mask == 0xff);
2972 fprintf (outfile, " %#" PRIx8 ",", byte);
2976 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
2978 byte = mask = nbits = 0;
2985 assert (leadingbytes == 0);
2987 unsigned long int remaining = b->field->bits;
2988 while (nbits + remaining > 8)
2990 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
2991 mask << (8 - nbits), byte << (8 - nbits));
2992 remaining = nbits + remaining - 8;
2993 byte = mask = nbits = 0;
3002 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
3003 byte = mask = nbits = 0;
3011 fputc_unlocked ('\n', outfile);
3013 fputs ("};\n", outfile);
3018 static size_t mnemonic_maxlen;
3019 static size_t mnemonic_minlen;
3021 which_chars (const char *str[], size_t nstr)
3023 char used_char[256];
3024 memset (used_char, '\0', sizeof (used_char));
3025 mnemonic_maxlen = 0;
3026 mnemonic_minlen = 10000;
3027 for (size_t cnt = 0; cnt < nstr; ++cnt)
3029 const unsigned char *cp = (const unsigned char *) str[cnt];
3030 mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
3031 mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
3033 used_char[*cp++] = 1;
3034 while (*cp != '\0');
3036 size_t nused_char = 0;
3037 for (size_t cnt = 0; cnt < 256; ++cnt)
3038 if (used_char[cnt] != 0)
3044 static const char **mnemonic_strs;
3045 static size_t nmnemonic_strs;
3047 add_mnemonics (const void *nodep, VISIT value,
3048 int level __attribute__ ((unused)))
3050 if (value == leaf || value == postorder)
3051 mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
3060 static struct charfreq pfxfreq[256];
3061 static struct charfreq sfxfreq[256];
3065 compare_freq (const void *p1, const void *p2)
3067 const struct charfreq *c1 = (const struct charfreq *) p1;
3068 const struct charfreq *c2 = (const struct charfreq *) p2;
3070 if (c1->freq > c2->freq)
3072 if (c1->freq < c2->freq)
3079 compute_pfxfreq (const char *str[], size_t nstr)
3081 memset (pfxfreq, '\0', sizeof (pfxfreq));
3083 for (size_t i = 0; i < nstr; ++i)
3086 for (size_t i = 0; i < nstr; ++i)
3087 ++pfxfreq[*((const unsigned char *) str[i])].freq;
3089 qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
3092 while (n < 256 && pfxfreq[n].freq != 0)
3105 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
3107 memset (sfxfreq, '\0', sizeof (sfxfreq));
3109 for (size_t i = 0; i < nstr; ++i)
3112 for (size_t i = 0; i < nstr; ++i)
3113 ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
3115 qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
3118 while (n < 256 && sfxfreq[n].freq != 0)
3125 create_mnemonic_table (void)
3127 mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
3129 twalk (mnemonics, add_mnemonics);
3131 (void) which_chars (mnemonic_strs, nmnemonic_strs);
3133 size_t best_so_far = 100000000;
3134 char *best_prefix = NULL;
3135 char *best_suffix = NULL;
3136 char *best_table = NULL;
3137 size_t best_table_size = 0;
3138 size_t best_table_bits = 0;
3139 size_t best_prefix_bits = 0;
3141 /* We can precompute the prefix characters. */
3142 size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
3144 /* Compute best size for string representation including explicit NUL. */
3145 for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
3147 char prefix[1 << pfxbits];
3149 for (i = 0; i < (1u << pfxbits) - 1; ++i)
3150 prefix[i] = pfxfreq[i].ch;
3153 struct strsnlen strsnlen[nmnemonic_strs];
3155 for (i = 0; i < nmnemonic_strs; ++i)
3157 if (strchr (prefix, *mnemonic_strs[i]) != NULL)
3158 strsnlen[i].str = mnemonic_strs[i] + 1;
3160 strsnlen[i].str = mnemonic_strs[i];
3161 strsnlen[i].len = strlen (strsnlen[i].str);
3164 /* With the prefixes gone, try to combine strings. */
3165 size_t nstrsnlen = 1;
3166 for (i = 1; i < nmnemonic_strs; ++i)
3169 for (j = 0; j < nstrsnlen; ++j)
3170 if (strsnlen[i].len > strsnlen[j].len
3171 && strcmp (strsnlen[j].str,
3172 strsnlen[i].str + (strsnlen[i].len
3173 - strsnlen[j].len)) == 0)
3175 strsnlen[j] = strsnlen[i];
3178 else if (strsnlen[i].len < strsnlen[j].len
3179 && strcmp (strsnlen[i].str,
3180 strsnlen[j].str + (strsnlen[j].len
3181 - strsnlen[i].len)) == 0)
3185 strsnlen[nstrsnlen++] = strsnlen[i];
3188 size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
3190 for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
3192 char suffix[1 << sfxbits];
3194 for (i = 0; i < (1u << sfxbits) - 1; ++i)
3195 suffix[i] = sfxfreq[i].ch;
3198 size_t newlen[nstrsnlen];
3200 for (i = 0; i < nstrsnlen; ++i)
3201 if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
3202 newlen[i] = strsnlen[i].len - 1;
3204 newlen[i] = strsnlen[i].len;
3207 memset (charused, '\0', sizeof (charused));
3208 size_t ncharused = 0;
3210 const char *tablestr[nstrsnlen];
3211 size_t ntablestr = 1;
3212 tablestr[0] = strsnlen[0].str;
3213 size_t table = newlen[0] + 1;
3214 for (i = 1; i < nstrsnlen; ++i)
3217 for (j = 0; j < ntablestr; ++j)
3218 if (newlen[i] > newlen[j]
3219 && memcmp (tablestr[j],
3220 strsnlen[i].str + (newlen[i] - newlen[j]),
3223 table += newlen[i] - newlen[j];
3224 tablestr[j] = strsnlen[i].str;
3225 newlen[j] = newlen[i];
3228 else if (newlen[i] < newlen[j]
3229 && memcmp (strsnlen[i].str,
3230 tablestr[j] + (newlen[j] - newlen[i]),
3236 table += newlen[i] + 1;
3237 tablestr[ntablestr] = strsnlen[i].str;
3238 newlen[ntablestr] = newlen[i];
3243 for (size_t x = 0; x < newlen[j]; ++x)
3244 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
3248 size_t ncharused_bits = 0;
3250 while (i < ncharused)
3256 size_t table_bits = 0;
3264 size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
3265 size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
3266 + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
3267 + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
3268 + (((total_bits + mnemonic_bits + 7) / 8)
3271 if (new_total < best_so_far)
3273 best_so_far = new_total;
3274 best_mnemonic_bits = mnemonic_bits;
3277 best_suffix = xstrdup (suffix);
3280 best_prefix = xstrdup (prefix);
3281 best_prefix_bits = pfxbits;
3283 best_table_size = table;
3284 best_table_bits = table_bits;
3285 char *cp = best_table = xrealloc (best_table, table);
3286 for (i = 0; i < ntablestr; ++i)
3288 assert (cp + newlen[i] + 1 <= best_table + table);
3289 cp = mempcpy (cp, tablestr[i], newlen[i]);
3292 assert (cp == best_table + table);
3297 fputs ("static const char mnemonic_table[] =\n\"", outfile);
3298 for (size_t i = 0; i < best_table_size; ++i)
3300 if (((i + 1) % 60) == 0)
3301 fputs ("\"\n\"", outfile);
3302 if (!isascii (best_table[i]) || !isprint (best_table[i]))
3303 fprintf (outfile, "\\%03o", best_table[i]);
3305 fputc (best_table[i], outfile);
3307 fputs ("\";\n", outfile);
3309 if (best_prefix[0] != '\0')
3311 "static const char prefix[%zu] = \"%s\";\n"
3312 "#define PREFIXCHAR_BITS %zu\n",
3313 strlen (best_prefix), best_prefix, best_prefix_bits);
3315 fputs ("#define NO_PREFIX\n", outfile);
3317 if (best_suffix[0] != '\0')
3318 fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
3319 strlen (best_suffix), best_suffix);
3321 fputs ("#define NO_SUFFIX\n", outfile);
3323 for (size_t i = 0; i < nmnemonic_strs; ++i)
3325 const char *mne = mnemonic_strs[i];
3328 char *cp = strchr (best_prefix, *mne);
3331 pfxval = 1 + (cp - best_prefix);
3335 size_t l = strlen (mne);
3338 cp = strchr (best_suffix, mne[l - 1]);
3341 sfxval = 1 + (cp - best_suffix);
3345 char *off = memmem (best_table, best_table_size, mne, l);
3346 while (off[l] != '\0')
3348 off = memmem (off + 1, best_table_size, mne, l);
3349 assert (off != NULL);
3352 fprintf (outfile, "#define MNE_%s %#zx\n",
3355 + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));