1 /* A Bison parser, made by GNU Bison 2.5. */
3 /* Bison implementation for Yacc-like parsers in C
5 Copyright (C) 1984, 1989-1990, 2000-2011 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.5"
50 #define YYSKELETON_NAME "yacc.c"
61 /* Using locations. */
62 #define YYLSP_NEEDED 0
64 /* Substitute the variable and function names. */
65 #define yyparse i386_parse
66 #define yylex i386_lex
67 #define yyerror i386_error
68 #define yylval i386_lval
69 #define yychar i386_char
70 #define yydebug i386_debug
71 #define yynerrs i386_nerrs
74 /* Copy the first part of user declarations. */
76 /* Line 268 of yacc.c */
77 #line 1 "i386_parse.y"
79 /* Parser for i386 CPU description.
80 Copyright (C) 2004, 2005, 2007, 2008, 2009 Red Hat, Inc.
81 Written by Ulrich Drepper <drepper@redhat.com>, 2004.
83 Red Hat elfutils is free software; you can redistribute it and/or modify
84 it under the terms of the GNU General Public License as published by the
85 Free Software Foundation; version 2 of the License.
87 Red Hat elfutils is distributed in the hope that it will be useful, but
88 WITHOUT ANY WARRANTY; without even the implied warranty of
89 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
90 General Public License for more details.
92 You should have received a copy of the GNU General Public License along
93 with Red Hat elfutils; if not, write to the Free Software Foundation,
94 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
96 Red Hat elfutils is an included package of the Open Invention Network.
97 An included package of the Open Invention Network is a package for which
98 Open Invention Network licensees cross-license their patents. No patent
99 license is granted, either expressly or impliedly, by designation as an
100 included package. Should you wish to participate in the Open Invention
101 Network licensing program, please visit www.openinventionnetwork.com
102 <http://www.openinventionnetwork.com>. */
112 #include <inttypes.h>
121 #include <sys/param.h>
125 #define obstack_chunk_alloc xmalloc
126 #define obstack_chunk_free free
128 /* The error handler. */
129 static void yyerror (const char *s);
131 extern int yylex (void);
132 extern int i386_lineno;
133 extern char *infname;
136 struct known_bitfield
139 unsigned long int bits;
146 enum bittype { zeroone, field, failure } type;
150 struct known_bitfield *field;
152 struct bitvalue *next;
158 enum nametype { string, nfield } type;
162 struct known_bitfield *field;
164 struct argname *next;
170 struct argname *name;
171 struct argument *next;
177 /* The byte encoding. */
178 struct bitvalue *bytes;
180 /* Prefix possible. */
188 enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
189 suffix_w1, suffix_W1, suffix_D } suffix;
191 /* Flag set if modr/m is used. */
204 struct instruction *next;
230 static struct known_bitfield ax_reg =
232 .name = "ax", .bits = 0, .tmp = 0
235 static struct known_bitfield dx_reg =
237 .name = "dx", .bits = 0, .tmp = 0
240 static struct known_bitfield di_reg =
242 .name = "es_di", .bits = 0, .tmp = 0
245 static struct known_bitfield si_reg =
247 .name = "ds_si", .bits = 0, .tmp = 0
250 static struct known_bitfield bx_reg =
252 .name = "ds_bx", .bits = 0, .tmp = 0
256 static int bitfield_compare (const void *p1, const void *p2);
257 static void new_bitfield (char *name, unsigned long int num);
258 static void check_bits (struct bitvalue *value);
259 static int check_duplicates (struct bitvalue *val);
260 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
261 static int check_bitsused (struct bitvalue *bitval,
262 struct known_bitfield *suffix,
263 struct argument *args);
264 static struct argname *combine (struct argname *name);
265 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
266 struct instruction *instr, int n);
267 static void find_numbers (void);
268 static int compare_syn (const void *p1, const void *p2);
269 static int compare_suf (const void *p1, const void *p2);
270 static void instrtable_out (void);
272 static void create_mnemonic_table (void);
275 static void *bitfields;
276 static struct instruction *instructions;
277 static size_t ninstructions;
278 static void *synonyms;
279 static void *suffixes;
280 static int nsuffixes;
281 static void *mnemonics;
283 extern FILE *outfile;
285 /* Number of bits used mnemonics. */
287 static size_t best_mnemonic_bits;
291 /* Line 268 of yacc.c */
292 #line 293 "i386_parse.c"
294 /* Enabling traces. */
299 /* Enabling verbose error messages. */
300 #ifdef YYERROR_VERBOSE
301 # undef YYERROR_VERBOSE
302 # define YYERROR_VERBOSE 1
304 # define YYERROR_VERBOSE 0
307 /* Enabling the token table. */
308 #ifndef YYTOKEN_TABLE
309 # define YYTOKEN_TABLE 0
316 /* Put the tokens into the symbol table, so that GDB and other debuggers
338 #define kPERCPERC 264
339 #define kBITFIELD 265
346 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
347 typedef union YYSTYPE
350 /* Line 293 of yacc.c */
351 #line 214 "i386_parse.y"
353 unsigned long int num;
356 struct known_bitfield *field;
357 struct bitvalue *bit;
358 struct argname *name;
359 struct argument *arg;
363 /* Line 293 of yacc.c */
364 #line 365 "i386_parse.c"
366 # define YYSTYPE_IS_TRIVIAL 1
367 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
368 # define YYSTYPE_IS_DECLARED 1
372 /* Copy the second part of user declarations. */
375 /* Line 343 of yacc.c */
376 #line 377 "i386_parse.c"
383 typedef YYTYPE_UINT8 yytype_uint8;
385 typedef unsigned char yytype_uint8;
389 typedef YYTYPE_INT8 yytype_int8;
390 #elif (defined __STDC__ || defined __C99__FUNC__ \
391 || defined __cplusplus || defined _MSC_VER)
392 typedef signed char yytype_int8;
394 typedef short int yytype_int8;
398 typedef YYTYPE_UINT16 yytype_uint16;
400 typedef unsigned short int yytype_uint16;
404 typedef YYTYPE_INT16 yytype_int16;
406 typedef short int yytype_int16;
410 # ifdef __SIZE_TYPE__
411 # define YYSIZE_T __SIZE_TYPE__
412 # elif defined size_t
413 # define YYSIZE_T size_t
414 # elif ! defined YYSIZE_T && (defined __STDC__ || defined __C99__FUNC__ \
415 || defined __cplusplus || defined _MSC_VER)
416 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
417 # define YYSIZE_T size_t
419 # define YYSIZE_T unsigned int
423 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
426 # if defined YYENABLE_NLS && YYENABLE_NLS
428 # include <libintl.h> /* INFRINGES ON USER NAME SPACE */
429 # define YY_(msgid) dgettext ("bison-runtime", msgid)
433 # define YY_(msgid) msgid
437 /* Suppress unused-variable warnings by "using" E. */
438 #if ! defined lint || defined __GNUC__
439 # define YYUSE(e) ((void) (e))
441 # define YYUSE(e) /* empty */
444 /* Identity function, used to suppress warnings about constant conditions. */
448 #if (defined __STDC__ || defined __C99__FUNC__ \
449 || defined __cplusplus || defined _MSC_VER)
462 #if ! defined yyoverflow || YYERROR_VERBOSE
464 /* The parser invokes alloca or malloc; define the necessary symbols. */
466 # ifdef YYSTACK_USE_ALLOCA
467 # if YYSTACK_USE_ALLOCA
469 # define YYSTACK_ALLOC __builtin_alloca
470 # elif defined __BUILTIN_VA_ARG_INCR
471 # include <alloca.h> /* INFRINGES ON USER NAME SPACE */
473 # define YYSTACK_ALLOC __alloca
474 # elif defined _MSC_VER
475 # include <malloc.h> /* INFRINGES ON USER NAME SPACE */
476 # define alloca _alloca
478 # define YYSTACK_ALLOC alloca
479 # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
480 || defined __cplusplus || defined _MSC_VER)
481 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
482 # ifndef EXIT_SUCCESS
483 # define EXIT_SUCCESS 0
490 # ifdef YYSTACK_ALLOC
491 /* Pacify GCC's `empty if-body' warning. */
492 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
493 # ifndef YYSTACK_ALLOC_MAXIMUM
494 /* The OS might guarantee only one guard page at the bottom of the stack,
495 and a page size can be as small as 4096 bytes. So we cannot safely
496 invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
497 to allow for a few compiler-allocated temporary stack slots. */
498 # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
501 # define YYSTACK_ALLOC YYMALLOC
502 # define YYSTACK_FREE YYFREE
503 # ifndef YYSTACK_ALLOC_MAXIMUM
504 # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
506 # if (defined __cplusplus && ! defined EXIT_SUCCESS \
507 && ! ((defined YYMALLOC || defined malloc) \
508 && (defined YYFREE || defined free)))
509 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
510 # ifndef EXIT_SUCCESS
511 # define EXIT_SUCCESS 0
515 # define YYMALLOC malloc
516 # if ! defined malloc && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
517 || defined __cplusplus || defined _MSC_VER)
518 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
523 # if ! defined free && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
524 || defined __cplusplus || defined _MSC_VER)
525 void free (void *); /* INFRINGES ON USER NAME SPACE */
529 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
532 #if (! defined yyoverflow \
533 && (! defined __cplusplus \
534 || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
536 /* A type that is properly aligned for any stack member. */
539 yytype_int16 yyss_alloc;
543 /* The size of the maximum gap between one aligned stack and the next. */
544 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
546 /* The size of an array large to enough to hold all stacks, each with
548 # define YYSTACK_BYTES(N) \
549 ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
550 + YYSTACK_GAP_MAXIMUM)
552 # define YYCOPY_NEEDED 1
554 /* Relocate STACK from its old location to the new one. The
555 local variables YYSIZE and YYSTACKSIZE give the old and new number of
556 elements in the stack, and YYPTR gives the new location of the
557 stack. Advance YYPTR to a properly aligned location for the next
559 # define YYSTACK_RELOCATE(Stack_alloc, Stack) \
562 YYSIZE_T yynewbytes; \
563 YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \
564 Stack = &yyptr->Stack_alloc; \
565 yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
566 yyptr += yynewbytes / sizeof (*yyptr); \
572 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
573 /* Copy COUNT objects from FROM to TO. The source and destination do
576 # if defined __GNUC__ && 1 < __GNUC__
577 # define YYCOPY(To, From, Count) \
578 __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
580 # define YYCOPY(To, From, Count) \
584 for (yyi = 0; yyi < (Count); yyi++) \
585 (To)[yyi] = (From)[yyi]; \
590 #endif /* !YYCOPY_NEEDED */
592 /* YYFINAL -- State number of the termination state. */
594 /* YYLAST -- Last index in YYTABLE. */
597 /* YYNTOKENS -- Number of terminals. */
599 /* YYNNTS -- Number of nonterminals. */
601 /* YYNRULES -- Number of rules. */
603 /* YYNRULES -- Number of states. */
606 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
608 #define YYMAXUTOK 267
610 #define YYTRANSLATE(YYX) \
611 ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
613 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */
614 static const yytype_uint8 yytranslate[] =
616 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
617 13, 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, 15, 2, 2, 2, 16, 17,
621 2, 2, 2, 2, 2, 2, 2, 2, 14, 2,
622 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
623 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
624 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
625 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
626 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
627 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
628 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
629 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
630 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
631 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
632 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
633 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
634 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
635 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
636 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
637 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
638 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
639 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
640 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
641 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
642 5, 6, 7, 8, 9, 10, 11, 12
646 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
648 static const yytype_uint8 yyprhs[] =
650 0, 0, 3, 8, 12, 14, 18, 21, 24, 28,
651 29, 33, 35, 42, 43, 45, 46, 50, 52, 55,
652 57, 59, 61, 63, 66, 67, 71, 73, 76, 78,
656 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
657 static const yytype_int8 yyrhs[] =
659 19, 0, -1, 20, 9, 13, 22, -1, 20, 13,
660 21, -1, 21, -1, 3, 10, 8, -1, 4, 10,
661 -1, 5, 10, -1, 6, 10, 10, -1, -1, 22,
662 13, 23, -1, 23, -1, 25, 14, 24, 7, 24,
663 28, -1, -1, 10, -1, -1, 25, 15, 26, -1,
664 26, -1, 26, 27, -1, 27, -1, 16, -1, 17,
665 -1, 10, -1, 12, 29, -1, -1, 29, 15, 30,
666 -1, 30, -1, 30, 31, -1, 31, -1, 10, -1,
667 11, -1, 7, -1, 14, -1
670 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
671 static const yytype_uint16 yyrline[] =
673 0, 244, 244, 254, 255, 258, 260, 262, 264, 276,
674 279, 280, 283, 366, 369, 385, 388, 398, 405, 413,
675 417, 424, 431, 453, 456, 459, 469, 477, 485, 488,
680 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
681 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
682 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
683 static const char *const yytname[] =
685 "$end", "error", "$undefined", "kMASK", "kPREFIX", "kSUFFIX",
686 "kSYNONYM", "kID", "kNUMBER", "kPERCPERC", "kBITFIELD", "kCHAR",
687 "kSPACE", "'\\n'", "':'", "','", "'0'", "'1'", "$accept", "spec",
688 "masks", "mask", "instrs", "instr", "bitfieldopt", "bytes", "byte",
689 "bit", "optargs", "args", "arg", "argcomp", 0
694 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
696 static const yytype_uint16 yytoknum[] =
698 0, 256, 257, 258, 259, 260, 261, 262, 263, 264,
699 265, 266, 267, 10, 58, 44, 48, 49
703 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
704 static const yytype_uint8 yyr1[] =
706 0, 18, 19, 20, 20, 21, 21, 21, 21, 21,
707 22, 22, 23, 23, 24, 24, 25, 25, 26, 26,
708 27, 27, 27, 28, 28, 29, 29, 30, 30, 31,
712 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
713 static const yytype_uint8 yyr2[] =
715 0, 2, 4, 3, 1, 3, 2, 2, 3, 0,
716 3, 1, 6, 0, 1, 0, 3, 1, 2, 1,
717 1, 1, 1, 2, 0, 3, 1, 2, 1, 1,
721 /* YYDEFACT[STATE-NAME] -- Default reduction number in state STATE-NUM.
722 Performed when YYTABLE doesn't specify something else to do. Zero
723 means the default is an error. */
724 static const yytype_uint8 yydefact[] =
726 9, 0, 0, 0, 0, 0, 0, 4, 0, 6,
727 7, 0, 1, 0, 9, 5, 8, 13, 3, 22,
728 20, 21, 2, 11, 0, 17, 19, 13, 15, 0,
729 18, 10, 14, 0, 16, 15, 24, 0, 12, 31,
730 29, 30, 32, 23, 26, 28, 0, 27, 25
733 /* YYDEFGOTO[NTERM-NUM]. */
734 static const yytype_int8 yydefgoto[] =
736 -1, 5, 6, 7, 22, 23, 33, 24, 25, 26,
740 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
742 #define YYPACT_NINF -35
743 static const yytype_int8 yypact[] =
745 12, 9, 10, 11, 13, 22, -2, -35, 16, -35,
746 -35, 15, -35, 14, 12, -35, -35, -4, -35, -35,
747 -35, -35, 17, -35, -12, -4, -35, -4, 18, -4,
748 -35, -35, -35, 19, -4, 18, 20, -6, -35, -35,
749 -35, -35, -35, 21, -6, -35, -6, -35, -6
752 /* YYPGOTO[NTERM-NUM]. */
753 static const yytype_int8 yypgoto[] =
755 -35, -35, -35, 23, -35, 2, -1, -35, 4, -25,
759 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
760 positive, shift that token. If negative, reduce the rule which
761 number is the opposite. If YYTABLE_NINF, syntax error. */
762 #define YYTABLE_NINF -1
763 static const yytype_uint8 yytable[] =
765 30, 39, 28, 29, 40, 41, 19, 13, 42, 30,
766 47, 14, 20, 21, 47, 1, 2, 3, 4, 8,
767 9, 10, 12, 11, 15, 16, 35, 17, 32, 31,
768 27, 48, 37, 34, 36, 0, 46, 18
771 #define yypact_value_is_default(yystate) \
774 #define yytable_value_is_error(yytable_value) \
777 static const yytype_int8 yycheck[] =
779 25, 7, 14, 15, 10, 11, 10, 9, 14, 34,
780 44, 13, 16, 17, 48, 3, 4, 5, 6, 10,
781 10, 10, 0, 10, 8, 10, 7, 13, 10, 27,
782 13, 46, 12, 29, 35, -1, 15, 14
785 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
786 symbol of state STATE-NUM. */
787 static const yytype_uint8 yystos[] =
789 0, 3, 4, 5, 6, 19, 20, 21, 10, 10,
790 10, 10, 0, 9, 13, 8, 10, 13, 21, 10,
791 16, 17, 22, 23, 25, 26, 27, 13, 14, 15,
792 27, 23, 10, 24, 26, 7, 24, 12, 28, 7,
793 10, 11, 14, 29, 30, 31, 15, 31, 30
796 #define yyerrok (yyerrstatus = 0)
797 #define yyclearin (yychar = YYEMPTY)
801 #define YYACCEPT goto yyacceptlab
802 #define YYABORT goto yyabortlab
803 #define YYERROR goto yyerrorlab
806 /* Like YYERROR except do call yyerror. This remains here temporarily
807 to ease the transition to the new meaning of YYERROR, for GCC.
808 Once GCC version 2 has supplanted version 1, this can go. However,
809 YYFAIL appears to be in use. Nevertheless, it is formally deprecated
810 in Bison 2.4.2's NEWS entry, where a plan to phase it out is
813 #define YYFAIL goto yyerrlab
815 /* This is here to suppress warnings from the GCC cpp's
816 -Wunused-macros. Normally we don't worry about that warning, but
817 some users do, and we want to make it easy for users to remove
818 YYFAIL uses, which will produce warnings from Bison 2.5. */
821 #define YYRECOVERING() (!!yyerrstatus)
823 #define YYBACKUP(Token, Value) \
825 if (yychar == YYEMPTY && yylen == 1) \
834 yyerror (YY_("syntax error: cannot back up")); \
841 #define YYERRCODE 256
844 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
845 If N is 0, then set CURRENT to the empty location which ends
846 the previous symbol: RHS[0] (always defined). */
848 #define YYRHSLOC(Rhs, K) ((Rhs)[K])
849 #ifndef YYLLOC_DEFAULT
850 # define YYLLOC_DEFAULT(Current, Rhs, N) \
854 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
855 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
856 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
857 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
861 (Current).first_line = (Current).last_line = \
862 YYRHSLOC (Rhs, 0).last_line; \
863 (Current).first_column = (Current).last_column = \
864 YYRHSLOC (Rhs, 0).last_column; \
870 /* This macro is provided for backward compatibility. */
872 #ifndef YY_LOCATION_PRINT
873 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
877 /* YYLEX -- calling `yylex' with the right arguments. */
880 # define YYLEX yylex (YYLEX_PARAM)
882 # define YYLEX yylex ()
885 /* Enable debugging if requested. */
889 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
890 # define YYFPRINTF fprintf
893 # define YYDPRINTF(Args) \
899 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
903 YYFPRINTF (stderr, "%s ", Title); \
904 yy_symbol_print (stderr, \
906 YYFPRINTF (stderr, "\n"); \
911 /*--------------------------------.
912 | Print this symbol on YYOUTPUT. |
913 `--------------------------------*/
916 #if (defined __STDC__ || defined __C99__FUNC__ \
917 || defined __cplusplus || defined _MSC_VER)
919 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
922 yy_symbol_value_print (yyoutput, yytype, yyvaluep)
925 YYSTYPE const * const yyvaluep;
931 if (yytype < YYNTOKENS)
932 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
944 /*--------------------------------.
945 | Print this symbol on YYOUTPUT. |
946 `--------------------------------*/
948 #if (defined __STDC__ || defined __C99__FUNC__ \
949 || defined __cplusplus || defined _MSC_VER)
951 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
954 yy_symbol_print (yyoutput, yytype, yyvaluep)
957 YYSTYPE const * const yyvaluep;
960 if (yytype < YYNTOKENS)
961 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
963 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
965 yy_symbol_value_print (yyoutput, yytype, yyvaluep);
966 YYFPRINTF (yyoutput, ")");
969 /*------------------------------------------------------------------.
970 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
972 `------------------------------------------------------------------*/
974 #if (defined __STDC__ || defined __C99__FUNC__ \
975 || defined __cplusplus || defined _MSC_VER)
977 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
980 yy_stack_print (yybottom, yytop)
981 yytype_int16 *yybottom;
985 YYFPRINTF (stderr, "Stack now");
986 for (; yybottom <= yytop; yybottom++)
988 int yybot = *yybottom;
989 YYFPRINTF (stderr, " %d", yybot);
991 YYFPRINTF (stderr, "\n");
994 # define YY_STACK_PRINT(Bottom, Top) \
997 yy_stack_print ((Bottom), (Top)); \
1001 /*------------------------------------------------.
1002 | Report that the YYRULE is going to be reduced. |
1003 `------------------------------------------------*/
1005 #if (defined __STDC__ || defined __C99__FUNC__ \
1006 || defined __cplusplus || defined _MSC_VER)
1008 yy_reduce_print (YYSTYPE *yyvsp, int yyrule)
1011 yy_reduce_print (yyvsp, yyrule)
1016 int yynrhs = yyr2[yyrule];
1018 unsigned long int yylno = yyrline[yyrule];
1019 YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
1021 /* The symbols being reduced. */
1022 for (yyi = 0; yyi < yynrhs; yyi++)
1024 YYFPRINTF (stderr, " $%d = ", yyi + 1);
1025 yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
1026 &(yyvsp[(yyi + 1) - (yynrhs)])
1028 YYFPRINTF (stderr, "\n");
1032 # define YY_REDUCE_PRINT(Rule) \
1035 yy_reduce_print (yyvsp, Rule); \
1038 /* Nonzero means print parse trace. It is left uninitialized so that
1039 multiple parsers can coexist. */
1041 #else /* !YYDEBUG */
1042 # define YYDPRINTF(Args)
1043 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
1044 # define YY_STACK_PRINT(Bottom, Top)
1045 # define YY_REDUCE_PRINT(Rule)
1046 #endif /* !YYDEBUG */
1049 /* YYINITDEPTH -- initial size of the parser's stacks. */
1051 # define YYINITDEPTH 200
1054 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1055 if the built-in stack extension method is used).
1057 Do not make this value too large; the results are undefined if
1058 YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1059 evaluated with infinite-precision integer arithmetic. */
1062 # define YYMAXDEPTH 10000
1069 # if defined __GLIBC__ && defined _STRING_H
1070 # define yystrlen strlen
1072 /* Return the length of YYSTR. */
1073 #if (defined __STDC__ || defined __C99__FUNC__ \
1074 || defined __cplusplus || defined _MSC_VER)
1076 yystrlen (const char *yystr)
1084 for (yylen = 0; yystr[yylen]; yylen++)
1092 # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1093 # define yystpcpy stpcpy
1095 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1097 #if (defined __STDC__ || defined __C99__FUNC__ \
1098 || defined __cplusplus || defined _MSC_VER)
1100 yystpcpy (char *yydest, const char *yysrc)
1103 yystpcpy (yydest, yysrc)
1109 const char *yys = yysrc;
1111 while ((*yyd++ = *yys++) != '\0')
1120 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1121 quotes and backslashes, so that it's suitable for yyerror. The
1122 heuristic is that double-quoting is unnecessary unless the string
1123 contains an apostrophe, a comma, or backslash (other than
1124 backslash-backslash). YYSTR is taken from yytname. If YYRES is
1125 null, do not copy; instead, return the length of what the result
1128 yytnamerr (char *yyres, const char *yystr)
1133 char const *yyp = yystr;
1140 goto do_not_strip_quotes;
1144 goto do_not_strip_quotes;
1157 do_not_strip_quotes: ;
1161 return yystrlen (yystr);
1163 return yystpcpy (yyres, yystr) - yyres;
1167 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1168 about the unexpected token YYTOKEN for the state stack whose top is
1171 Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is
1172 not large enough to hold the message. In that case, also set
1173 *YYMSG_ALLOC to the required number of bytes. Return 2 if the
1174 required number of bytes is too large to store. */
1176 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1177 yytype_int16 *yyssp, int yytoken)
1179 YYSIZE_T yysize0 = yytnamerr (0, yytname[yytoken]);
1180 YYSIZE_T yysize = yysize0;
1182 enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1183 /* Internationalized format string. */
1184 const char *yyformat = 0;
1185 /* Arguments of yyformat. */
1186 char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1187 /* Number of reported tokens (one for the "unexpected", one per
1191 /* There are many possibilities here to consider:
1192 - Assume YYFAIL is not used. It's too flawed to consider. See
1193 <http://lists.gnu.org/archive/html/bison-patches/2009-12/msg00024.html>
1194 for details. YYERROR is fine as it does not invoke this
1196 - If this state is a consistent state with a default action, then
1197 the only way this function was invoked is if the default action
1198 is an error action. In that case, don't check for expected
1199 tokens because there are none.
1200 - The only way there can be no lookahead present (in yychar) is if
1201 this state is a consistent state with a default action. Thus,
1202 detecting the absence of a lookahead is sufficient to determine
1203 that there is no unexpected or expected token to report. In that
1204 case, just report a simple "syntax error".
1205 - Don't assume there isn't a lookahead just because this state is a
1206 consistent state with a default action. There might have been a
1207 previous inconsistent state, consistent state with a non-default
1208 action, or user semantic action that manipulated yychar.
1209 - Of course, the expected token list depends on states to have
1210 correct lookahead information, and it depends on the parser not
1211 to perform extra reductions after fetching a lookahead from the
1212 scanner and before detecting a syntax error. Thus, state merging
1213 (from LALR or IELR) and default reductions corrupt the expected
1214 token list. However, the list is correct for canonical LR with
1215 one exception: it will still contain any token that will not be
1216 accepted due to an error action in a later state.
1218 if (yytoken != YYEMPTY)
1220 int yyn = yypact[*yyssp];
1221 yyarg[yycount++] = yytname[yytoken];
1222 if (!yypact_value_is_default (yyn))
1224 /* Start YYX at -YYN if negative to avoid negative indexes in
1225 YYCHECK. In other words, skip the first -YYN actions for
1226 this state because they are default actions. */
1227 int yyxbegin = yyn < 0 ? -yyn : 0;
1228 /* Stay within bounds of both yycheck and yytname. */
1229 int yychecklim = YYLAST - yyn + 1;
1230 int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1233 for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1234 if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1235 && !yytable_value_is_error (yytable[yyx + yyn]))
1237 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1243 yyarg[yycount++] = yytname[yyx];
1244 yysize1 = yysize + yytnamerr (0, yytname[yyx]);
1245 if (! (yysize <= yysize1
1246 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1255 # define YYCASE_(N, S) \
1259 YYCASE_(0, YY_("syntax error"));
1260 YYCASE_(1, YY_("syntax error, unexpected %s"));
1261 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1262 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1263 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1264 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1268 yysize1 = yysize + yystrlen (yyformat);
1269 if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1273 if (*yymsg_alloc < yysize)
1275 *yymsg_alloc = 2 * yysize;
1276 if (! (yysize <= *yymsg_alloc
1277 && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1278 *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1282 /* Avoid sprintf, as that infringes on the user's name space.
1283 Don't have undefined behavior even if the translation
1284 produced a string with the wrong number of "%s"s. */
1288 while ((*yyp = *yyformat) != '\0')
1289 if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1291 yyp += yytnamerr (yyp, yyarg[yyi++]);
1302 #endif /* YYERROR_VERBOSE */
1304 /*-----------------------------------------------.
1305 | Release the memory associated to this symbol. |
1306 `-----------------------------------------------*/
1309 #if (defined __STDC__ || defined __C99__FUNC__ \
1310 || defined __cplusplus || defined _MSC_VER)
1312 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1315 yydestruct (yymsg, yytype, yyvaluep)
1325 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1336 /* Prevent warnings from -Wmissing-prototypes. */
1337 #ifdef YYPARSE_PARAM
1338 #if defined __STDC__ || defined __cplusplus
1339 int yyparse (void *YYPARSE_PARAM);
1343 #else /* ! YYPARSE_PARAM */
1344 #if defined __STDC__ || defined __cplusplus
1349 #endif /* ! YYPARSE_PARAM */
1352 /* The lookahead symbol. */
1355 /* The semantic value of the lookahead symbol. */
1358 /* Number of syntax errors so far. */
1366 #ifdef YYPARSE_PARAM
1367 #if (defined __STDC__ || defined __C99__FUNC__ \
1368 || defined __cplusplus || defined _MSC_VER)
1370 yyparse (void *YYPARSE_PARAM)
1373 yyparse (YYPARSE_PARAM)
1374 void *YYPARSE_PARAM;
1376 #else /* ! YYPARSE_PARAM */
1377 #if (defined __STDC__ || defined __C99__FUNC__ \
1378 || defined __cplusplus || defined _MSC_VER)
1389 /* Number of tokens to shift before error messages enabled. */
1392 /* The stacks and their tools:
1393 `yyss': related to states.
1394 `yyvs': related to semantic values.
1396 Refer to the stacks thru separate pointers, to allow yyoverflow
1397 to reallocate them elsewhere. */
1399 /* The state stack. */
1400 yytype_int16 yyssa[YYINITDEPTH];
1402 yytype_int16 *yyssp;
1404 /* The semantic value stack. */
1405 YYSTYPE yyvsa[YYINITDEPTH];
1409 YYSIZE_T yystacksize;
1413 /* Lookahead token as an internal (translated) token number. */
1415 /* The variables used to return semantic value and location from the
1420 /* Buffer for error messages, and its allocated size. */
1422 char *yymsg = yymsgbuf;
1423 YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1426 #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
1428 /* The number of symbols on the RHS of the reduced rule.
1429 Keep to zero when no symbol should be popped. */
1435 yystacksize = YYINITDEPTH;
1437 YYDPRINTF ((stderr, "Starting parse\n"));
1442 yychar = YYEMPTY; /* Cause a token to be read. */
1444 /* Initialize stack pointers.
1445 Waste one element of value and location stack
1446 so that they stay on the same level as the state stack.
1447 The wasted elements are never initialized. */
1453 /*------------------------------------------------------------.
1454 | yynewstate -- Push a new state, which is found in yystate. |
1455 `------------------------------------------------------------*/
1457 /* In all cases, when you get here, the value and location stacks
1458 have just been pushed. So pushing a state here evens the stacks. */
1464 if (yyss + yystacksize - 1 <= yyssp)
1466 /* Get the current used size of the three stacks, in elements. */
1467 YYSIZE_T yysize = yyssp - yyss + 1;
1471 /* Give user a chance to reallocate the stack. Use copies of
1472 these so that the &'s don't force the real ones into
1474 YYSTYPE *yyvs1 = yyvs;
1475 yytype_int16 *yyss1 = yyss;
1477 /* Each stack pointer address is followed by the size of the
1478 data in use in that stack, in bytes. This used to be a
1479 conditional around just the two extra args, but that might
1480 be undefined if yyoverflow is a macro. */
1481 yyoverflow (YY_("memory exhausted"),
1482 &yyss1, yysize * sizeof (*yyssp),
1483 &yyvs1, yysize * sizeof (*yyvsp),
1489 #else /* no yyoverflow */
1490 # ifndef YYSTACK_RELOCATE
1491 goto yyexhaustedlab;
1493 /* Extend the stack our own way. */
1494 if (YYMAXDEPTH <= yystacksize)
1495 goto yyexhaustedlab;
1497 if (YYMAXDEPTH < yystacksize)
1498 yystacksize = YYMAXDEPTH;
1501 yytype_int16 *yyss1 = yyss;
1502 union yyalloc *yyptr =
1503 (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1505 goto yyexhaustedlab;
1506 YYSTACK_RELOCATE (yyss_alloc, yyss);
1507 YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1508 # undef YYSTACK_RELOCATE
1510 YYSTACK_FREE (yyss1);
1513 #endif /* no yyoverflow */
1515 yyssp = yyss + yysize - 1;
1516 yyvsp = yyvs + yysize - 1;
1518 YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1519 (unsigned long int) yystacksize));
1521 if (yyss + yystacksize - 1 <= yyssp)
1525 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1527 if (yystate == YYFINAL)
1537 /* Do appropriate processing given the current state. Read a
1538 lookahead token if we need one and don't already have one. */
1540 /* First try to decide what to do without reference to lookahead token. */
1541 yyn = yypact[yystate];
1542 if (yypact_value_is_default (yyn))
1545 /* Not known => get a lookahead token if don't already have one. */
1547 /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */
1548 if (yychar == YYEMPTY)
1550 YYDPRINTF ((stderr, "Reading a token: "));
1554 if (yychar <= YYEOF)
1556 yychar = yytoken = YYEOF;
1557 YYDPRINTF ((stderr, "Now at end of input.\n"));
1561 yytoken = YYTRANSLATE (yychar);
1562 YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1565 /* If the proper action on seeing token YYTOKEN is to reduce or to
1566 detect an error, take that action. */
1568 if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1573 if (yytable_value_is_error (yyn))
1579 /* Count tokens shifted since error; after three, turn off error
1584 /* Shift the lookahead token. */
1585 YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1587 /* Discard the shifted token. */
1596 /*-----------------------------------------------------------.
1597 | yydefault -- do the default action for the current state. |
1598 `-----------------------------------------------------------*/
1600 yyn = yydefact[yystate];
1606 /*-----------------------------.
1607 | yyreduce -- Do a reduction. |
1608 `-----------------------------*/
1610 /* yyn is the number of a rule to reduce with. */
1613 /* If YYLEN is nonzero, implement the default value of the action:
1616 Otherwise, the following line sets YYVAL to garbage.
1617 This behavior is undocumented and Bison
1618 users should not rely upon it. Assigning to YYVAL
1619 unconditionally makes the parser a bit smaller, and it avoids a
1620 GCC warning that YYVAL may be used uninitialized. */
1621 yyval = yyvsp[1-yylen];
1624 YY_REDUCE_PRINT (yyn);
1629 /* Line 1806 of yacc.c */
1630 #line 245 "i386_parse.y"
1632 if (error_message_count != 0)
1633 error (EXIT_FAILURE, 0,
1634 "terminated due to previous error");
1642 /* Line 1806 of yacc.c */
1643 #line 259 "i386_parse.y"
1644 { new_bitfield ((yyvsp[(2) - (3)].str), (yyvsp[(3) - (3)].num)); }
1649 /* Line 1806 of yacc.c */
1650 #line 261 "i386_parse.y"
1651 { new_bitfield ((yyvsp[(2) - (2)].str), -1); }
1656 /* Line 1806 of yacc.c */
1657 #line 263 "i386_parse.y"
1658 { new_bitfield ((yyvsp[(2) - (2)].str), -2); }
1663 /* Line 1806 of yacc.c */
1664 #line 265 "i386_parse.y"
1666 struct synonym *newp = xmalloc (sizeof (*newp));
1667 newp->from = (yyvsp[(2) - (3)].str);
1668 newp->to = (yyvsp[(3) - (3)].str);
1669 if (tfind (newp, &synonyms, compare_syn) != NULL)
1671 "%d: duplicate definition for synonym '%s'",
1672 i386_lineno, (yyvsp[(2) - (3)].str));
1673 else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
1674 error (EXIT_FAILURE, 0, "tsearch");
1680 /* Line 1806 of yacc.c */
1681 #line 284 "i386_parse.y"
1683 if ((yyvsp[(3) - (6)].field) != NULL && strcmp ((yyvsp[(3) - (6)].field)->name, "RE") != 0
1684 && strcmp ((yyvsp[(3) - (6)].field)->name, "R") != 0)
1686 error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
1689 if (check_duplicates ((yyvsp[(1) - (6)].bit)) == 0
1690 && check_argsdef ((yyvsp[(1) - (6)].bit), (yyvsp[(6) - (6)].arg)) == 0
1691 && check_bitsused ((yyvsp[(1) - (6)].bit), (yyvsp[(5) - (6)].field), (yyvsp[(6) - (6)].arg)) == 0)
1693 struct instruction *newp = xcalloc (sizeof (*newp),
1695 if ((yyvsp[(3) - (6)].field) != NULL)
1697 if (strcmp ((yyvsp[(3) - (6)].field)->name, "RE") == 0)
1699 else if (strcmp ((yyvsp[(3) - (6)].field)->name, "R") == 0)
1703 newp->bytes = (yyvsp[(1) - (6)].bit);
1704 newp->mnemonic = (yyvsp[(4) - (6)].str);
1705 if (newp->mnemonic != (void *) -1l
1706 && tfind ((yyvsp[(4) - (6)].str), &mnemonics,
1707 (comparison_fn_t) strcmp) == NULL)
1709 if (tsearch ((yyvsp[(4) - (6)].str), &mnemonics,
1710 (comparison_fn_t) strcmp) == NULL)
1711 error (EXIT_FAILURE, errno, "tsearch");
1715 if ((yyvsp[(5) - (6)].field) != NULL)
1717 if (strcmp ((yyvsp[(5) - (6)].field)->name, "w") == 0)
1718 newp->suffix = suffix_w;
1719 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w0") == 0)
1720 newp->suffix = suffix_w0;
1721 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "tttn") == 0)
1722 newp->suffix = suffix_tttn;
1723 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w1") == 0)
1724 newp->suffix = suffix_w1;
1725 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W") == 0)
1726 newp->suffix = suffix_W;
1727 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W1") == 0)
1728 newp->suffix = suffix_W1;
1729 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "D") == 0)
1730 newp->suffix = suffix_D;
1732 error (EXIT_FAILURE, 0,
1733 "%s: %d: unknown suffix '%s'",
1734 infname, i386_lineno - 1, (yyvsp[(5) - (6)].field)->name);
1736 struct suffix search = { .name = (yyvsp[(5) - (6)].field)->name };
1737 if (tfind (&search, &suffixes, compare_suf)
1740 struct suffix *ns = xmalloc (sizeof (*ns));
1741 ns->name = (yyvsp[(5) - (6)].field)->name;
1742 ns->idx = ++nsuffixes;
1743 if (tsearch (ns, &suffixes, compare_suf)
1745 error (EXIT_FAILURE, errno, "tsearch");
1749 struct argument *args = (yyvsp[(6) - (6)].arg);
1751 while (args != NULL)
1753 fillin_arg ((yyvsp[(1) - (6)].bit), args->name, newp, n);
1759 newp->next = instructions;
1760 instructions = newp;
1768 /* Line 1806 of yacc.c */
1769 #line 370 "i386_parse.y"
1771 struct known_bitfield search;
1772 search.name = (yyvsp[(1) - (1)].str);
1773 struct known_bitfield **res;
1774 res = tfind (&search, &bitfields, bitfield_compare);
1777 error (0, 0, "%d: unknown bitfield '%s'",
1778 i386_lineno, search.name);
1779 (yyval.field) = NULL;
1782 (yyval.field) = *res;
1788 /* Line 1806 of yacc.c */
1789 #line 385 "i386_parse.y"
1790 { (yyval.field) = NULL; }
1795 /* Line 1806 of yacc.c */
1796 #line 389 "i386_parse.y"
1798 check_bits ((yyvsp[(3) - (3)].bit));
1800 struct bitvalue *runp = (yyvsp[(1) - (3)].bit);
1801 while (runp->next != NULL)
1803 runp->next = (yyvsp[(3) - (3)].bit);
1804 (yyval.bit) = (yyvsp[(1) - (3)].bit);
1810 /* Line 1806 of yacc.c */
1811 #line 399 "i386_parse.y"
1813 check_bits ((yyvsp[(1) - (1)].bit));
1814 (yyval.bit) = (yyvsp[(1) - (1)].bit);
1820 /* Line 1806 of yacc.c */
1821 #line 406 "i386_parse.y"
1823 struct bitvalue *runp = (yyvsp[(1) - (2)].bit);
1824 while (runp->next != NULL)
1826 runp->next = (yyvsp[(2) - (2)].bit);
1827 (yyval.bit) = (yyvsp[(1) - (2)].bit);
1833 /* Line 1806 of yacc.c */
1834 #line 414 "i386_parse.y"
1835 { (yyval.bit) = (yyvsp[(1) - (1)].bit); }
1840 /* Line 1806 of yacc.c */
1841 #line 418 "i386_parse.y"
1843 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1844 (yyval.bit)->type = zeroone;
1845 (yyval.bit)->value = 0;
1846 (yyval.bit)->next = NULL;
1852 /* Line 1806 of yacc.c */
1853 #line 425 "i386_parse.y"
1855 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1856 (yyval.bit)->type = zeroone;
1857 (yyval.bit)->value = 1;
1858 (yyval.bit)->next = NULL;
1864 /* Line 1806 of yacc.c */
1865 #line 432 "i386_parse.y"
1867 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1868 struct known_bitfield search;
1869 search.name = (yyvsp[(1) - (1)].str);
1870 struct known_bitfield **res;
1871 res = tfind (&search, &bitfields, bitfield_compare);
1874 error (0, 0, "%d: unknown bitfield '%s'",
1875 i386_lineno, search.name);
1876 (yyval.bit)->type = failure;
1880 (yyval.bit)->type = field;
1881 (yyval.bit)->field = *res;
1883 (yyval.bit)->next = NULL;
1889 /* Line 1806 of yacc.c */
1890 #line 454 "i386_parse.y"
1891 { (yyval.arg) = (yyvsp[(2) - (2)].arg); }
1896 /* Line 1806 of yacc.c */
1897 #line 456 "i386_parse.y"
1898 { (yyval.arg) = NULL; }
1903 /* Line 1806 of yacc.c */
1904 #line 460 "i386_parse.y"
1906 struct argument *runp = (yyvsp[(1) - (3)].arg);
1907 while (runp->next != NULL)
1909 runp->next = xmalloc (sizeof (struct argument));
1910 runp->next->name = combine ((yyvsp[(3) - (3)].name));
1911 runp->next->next = NULL;
1912 (yyval.arg) = (yyvsp[(1) - (3)].arg);
1918 /* Line 1806 of yacc.c */
1919 #line 470 "i386_parse.y"
1921 (yyval.arg) = xmalloc (sizeof (struct argument));
1922 (yyval.arg)->name = combine ((yyvsp[(1) - (1)].name));
1923 (yyval.arg)->next = NULL;
1929 /* Line 1806 of yacc.c */
1930 #line 478 "i386_parse.y"
1932 struct argname *runp = (yyvsp[(1) - (2)].name);
1933 while (runp->next != NULL)
1935 runp->next = (yyvsp[(2) - (2)].name);
1936 (yyval.name) = (yyvsp[(1) - (2)].name);
1942 /* Line 1806 of yacc.c */
1943 #line 486 "i386_parse.y"
1944 { (yyval.name) = (yyvsp[(1) - (1)].name); }
1949 /* Line 1806 of yacc.c */
1950 #line 489 "i386_parse.y"
1952 (yyval.name) = xmalloc (sizeof (struct argname));
1953 (yyval.name)->type = nfield;
1954 (yyval.name)->next = NULL;
1956 struct known_bitfield search;
1957 search.name = (yyvsp[(1) - (1)].str);
1958 struct known_bitfield **res;
1959 res = tfind (&search, &bitfields, bitfield_compare);
1962 if (strcmp ((yyvsp[(1) - (1)].str), "ax") == 0)
1963 (yyval.name)->field = &ax_reg;
1964 else if (strcmp ((yyvsp[(1) - (1)].str), "dx") == 0)
1965 (yyval.name)->field = &dx_reg;
1966 else if (strcmp ((yyvsp[(1) - (1)].str), "es_di") == 0)
1967 (yyval.name)->field = &di_reg;
1968 else if (strcmp ((yyvsp[(1) - (1)].str), "ds_si") == 0)
1969 (yyval.name)->field = &si_reg;
1970 else if (strcmp ((yyvsp[(1) - (1)].str), "ds_bx") == 0)
1971 (yyval.name)->field = &bx_reg;
1974 error (0, 0, "%d: unknown bitfield '%s'",
1975 i386_lineno, search.name);
1976 (yyval.name)->field = NULL;
1980 (yyval.name)->field = *res;
1986 /* Line 1806 of yacc.c */
1987 #line 521 "i386_parse.y"
1989 (yyval.name) = xmalloc (sizeof (struct argname));
1990 (yyval.name)->type = string;
1991 (yyval.name)->next = NULL;
1992 (yyval.name)->str = xmalloc (2);
1993 (yyval.name)->str[0] = (yyvsp[(1) - (1)].ch);
1994 (yyval.name)->str[1] = '\0';
2000 /* Line 1806 of yacc.c */
2001 #line 530 "i386_parse.y"
2003 (yyval.name) = xmalloc (sizeof (struct argname));
2004 (yyval.name)->type = string;
2005 (yyval.name)->next = NULL;
2006 (yyval.name)->str = (yyvsp[(1) - (1)].str);
2012 /* Line 1806 of yacc.c */
2013 #line 537 "i386_parse.y"
2015 (yyval.name) = xmalloc (sizeof (struct argname));
2016 (yyval.name)->type = string;
2017 (yyval.name)->next = NULL;
2018 (yyval.name)->str = xmalloc (2);
2019 (yyval.name)->str[0] = ':';
2020 (yyval.name)->str[1] = '\0';
2026 /* Line 1806 of yacc.c */
2027 #line 2028 "i386_parse.c"
2030 /* User semantic actions sometimes alter yychar, and that requires
2031 that yytoken be updated with the new translation. We take the
2032 approach of translating immediately before every use of yytoken.
2033 One alternative is translating here after every semantic action,
2034 but that translation would be missed if the semantic action invokes
2035 YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
2036 if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an
2037 incorrect destructor might then be invoked immediately. In the
2038 case of YYERROR or YYBACKUP, subsequent parser actions might lead
2039 to an incorrect destructor call or verbose syntax error message
2040 before the lookahead is translated. */
2041 YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
2045 YY_STACK_PRINT (yyss, yyssp);
2049 /* Now `shift' the result of the reduction. Determine what state
2050 that goes to, based on the state we popped back to and the rule
2051 number reduced by. */
2055 yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
2056 if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
2057 yystate = yytable[yystate];
2059 yystate = yydefgoto[yyn - YYNTOKENS];
2064 /*------------------------------------.
2065 | yyerrlab -- here on detecting error |
2066 `------------------------------------*/
2068 /* Make sure we have latest lookahead translation. See comments at
2069 user semantic actions for why this is necessary. */
2070 yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
2072 /* If not already recovering from an error, report this error. */
2076 #if ! YYERROR_VERBOSE
2077 yyerror (YY_("syntax error"));
2079 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
2082 char const *yymsgp = YY_("syntax error");
2083 int yysyntax_error_status;
2084 yysyntax_error_status = YYSYNTAX_ERROR;
2085 if (yysyntax_error_status == 0)
2087 else if (yysyntax_error_status == 1)
2089 if (yymsg != yymsgbuf)
2090 YYSTACK_FREE (yymsg);
2091 yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
2095 yymsg_alloc = sizeof yymsgbuf;
2096 yysyntax_error_status = 2;
2100 yysyntax_error_status = YYSYNTAX_ERROR;
2105 if (yysyntax_error_status == 2)
2106 goto yyexhaustedlab;
2108 # undef YYSYNTAX_ERROR
2114 if (yyerrstatus == 3)
2116 /* If just tried and failed to reuse lookahead token after an
2117 error, discard it. */
2119 if (yychar <= YYEOF)
2121 /* Return failure if at end of input. */
2122 if (yychar == YYEOF)
2127 yydestruct ("Error: discarding",
2133 /* Else will try to reuse lookahead token after shifting the error
2138 /*---------------------------------------------------.
2139 | yyerrorlab -- error raised explicitly by YYERROR. |
2140 `---------------------------------------------------*/
2143 /* Pacify compilers like GCC when the user code never invokes
2144 YYERROR and the label yyerrorlab therefore never appears in user
2146 if (/*CONSTCOND*/ 0)
2149 /* Do not reclaim the symbols of the rule which action triggered
2153 YY_STACK_PRINT (yyss, yyssp);
2158 /*-------------------------------------------------------------.
2159 | yyerrlab1 -- common code for both syntax error and YYERROR. |
2160 `-------------------------------------------------------------*/
2162 yyerrstatus = 3; /* Each real token shifted decrements this. */
2166 yyn = yypact[yystate];
2167 if (!yypact_value_is_default (yyn))
2170 if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
2178 /* Pop the current state because it cannot handle the error token. */
2183 yydestruct ("Error: popping",
2184 yystos[yystate], yyvsp);
2187 YY_STACK_PRINT (yyss, yyssp);
2193 /* Shift the error token. */
2194 YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
2200 /*-------------------------------------.
2201 | yyacceptlab -- YYACCEPT comes here. |
2202 `-------------------------------------*/
2207 /*-----------------------------------.
2208 | yyabortlab -- YYABORT comes here. |
2209 `-----------------------------------*/
2214 #if !defined(yyoverflow) || YYERROR_VERBOSE
2215 /*-------------------------------------------------.
2216 | yyexhaustedlab -- memory exhaustion comes here. |
2217 `-------------------------------------------------*/
2219 yyerror (YY_("memory exhausted"));
2225 if (yychar != YYEMPTY)
2227 /* Make sure we have latest lookahead translation. See comments at
2228 user semantic actions for why this is necessary. */
2229 yytoken = YYTRANSLATE (yychar);
2230 yydestruct ("Cleanup: discarding lookahead",
2233 /* Do not reclaim the symbols of the rule which action triggered
2234 this YYABORT or YYACCEPT. */
2236 YY_STACK_PRINT (yyss, yyssp);
2237 while (yyssp != yyss)
2239 yydestruct ("Cleanup: popping",
2240 yystos[*yyssp], yyvsp);
2245 YYSTACK_FREE (yyss);
2248 if (yymsg != yymsgbuf)
2249 YYSTACK_FREE (yymsg);
2251 /* Make sure YYID is used. */
2252 return YYID (yyresult);
2257 /* Line 2067 of yacc.c */
2258 #line 547 "i386_parse.y"
2262 yyerror (const char *s)
2264 error (0, 0, gettext ("while reading i386 CPU description: %s at line %d"),
2265 gettext (s), i386_lineno);
2270 bitfield_compare (const void *p1, const void *p2)
2272 struct known_bitfield *f1 = (struct known_bitfield *) p1;
2273 struct known_bitfield *f2 = (struct known_bitfield *) p2;
2275 return strcmp (f1->name, f2->name);
2280 new_bitfield (char *name, unsigned long int num)
2282 struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
2287 if (tfind (newp, &bitfields, bitfield_compare) != NULL)
2289 error (0, 0, "%d: duplicated definition of bitfield '%s'",
2295 if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
2296 error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
2301 /* Check that the number of bits is a multiple of 8. */
2303 check_bits (struct bitvalue *val)
2305 struct bitvalue *runp = val;
2306 unsigned int total = 0;
2308 while (runp != NULL)
2310 if (runp->type == zeroone)
2312 else if (runp->field == NULL)
2313 /* No sense doing anything, the field is not known. */
2316 total += runp->field->bits;
2328 if (val->type == zeroone)
2329 obstack_printf (&os, "%u", val->value);
2331 obstack_printf (&os, "{%s}", val->field->name);
2334 obstack_1grow (&os, '\0');
2336 error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
2337 i386_lineno, (char *) obstack_finish (&os));
2339 obstack_free (&os, NULL);
2345 check_duplicates (struct bitvalue *val)
2353 if (val->type == field && val->field != NULL)
2355 if (val->field->tmp == testcnt)
2357 error (0, 0, "%d: bitfield '%s' used more than once",
2358 i386_lineno - 1, val->field->name);
2361 val->field->tmp = testcnt;
2372 check_argsdef (struct bitvalue *bitval, struct argument *args)
2376 while (args != NULL)
2378 for (struct argname *name = args->name; name != NULL; name = name->next)
2379 if (name->type == nfield && name->field != NULL
2380 && name->field != &ax_reg && name->field != &dx_reg
2381 && name->field != &di_reg && name->field != &si_reg
2382 && name->field != &bx_reg)
2384 struct bitvalue *runp = bitval;
2386 while (runp != NULL)
2387 if (runp->type == field && runp->field == name->field)
2394 error (0, 0, "%d: unknown bitfield '%s' used in output format",
2395 i386_lineno - 1, name->field->name);
2408 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
2409 struct argument *args)
2413 while (bitval != NULL)
2415 if (bitval->type == field && bitval->field != NULL
2416 && bitval->field != suffix
2417 /* {w} is handled special. */
2418 && strcmp (bitval->field->name, "w") != 0)
2420 struct argument *runp;
2421 for (runp = args; runp != NULL; runp = runp->next)
2423 struct argname *name = runp->name;
2425 while (name != NULL)
2426 if (name->type == nfield && name->field == bitval->field)
2438 error (0, 0, "%d: bitfield '%s' not used",
2439 i386_lineno - 1, bitval->field->name);
2445 bitval = bitval->next;
2452 static struct argname *
2453 combine (struct argname *name)
2455 struct argname *last_str = NULL;
2456 for (struct argname *runp = name; runp != NULL; runp = runp->next)
2458 if (runp->type == string)
2460 if (last_str == NULL)
2464 last_str->str = xrealloc (last_str->str,
2465 strlen (last_str->str)
2466 + strlen (runp->str) + 1);
2467 strcat (last_str->str, runp->str);
2468 last_str->next = runp->next;
2478 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
2482 fillin_arg (struct bitvalue *bytes, struct argname *name,
2483 struct instruction *instr, int n)
2485 static struct obstack ob;
2486 static int initialized;
2493 struct argname *runp = name;
2495 while (runp != NULL)
2497 /* We ignore strings in the function name. */
2498 if (runp->type == string)
2500 if (instr->operands[n].str != NULL)
2501 error (EXIT_FAILURE, 0,
2502 "%d: cannot have more than one string parameter",
2505 instr->operands[n].str = runp->str;
2509 assert (runp->type == nfield);
2511 /* Construct the function name. */
2513 obstack_1grow (&ob, '$');
2515 if (runp->field == NULL)
2516 /* Add some string which contains invalid characters. */
2517 obstack_grow_str (&ob, "!!!INVALID!!!");
2520 char *fieldname = runp->field->name;
2522 struct synonym search = { .from = fieldname };
2524 struct synonym **res = tfind (&search, &synonyms, compare_syn);
2526 fieldname = (*res)->to;
2528 obstack_grow_str (&ob, fieldname);
2531 /* Now compute the bit offset of the field. */
2532 struct bitvalue *b = bytes;
2534 if (runp->field != NULL)
2537 if (b->type == field && b->field != NULL)
2539 if (strcmp (b->field->name, runp->field->name) == 0)
2541 bitoff += b->field->bits;
2548 if (instr->operands[n].off1 == 0)
2549 instr->operands[n].off1 = bitoff;
2550 else if (instr->operands[n].off2 == 0)
2551 instr->operands[n].off2 = bitoff;
2552 else if (instr->operands[n].off3 == 0)
2553 instr->operands[n].off3 = bitoff;
2555 error (EXIT_FAILURE, 0,
2556 "%d: cannot have more than three fields in parameter",
2559 if (runp->field != NULL
2560 && strncasecmp (runp->field->name, "mod", 3) == 0)
2566 if (obstack_object_size (&ob) == 0)
2567 obstack_grow_str (&ob, "string");
2568 obstack_1grow (&ob, '\0');
2569 char *fct = obstack_finish (&ob);
2571 instr->operands[n].fct = fct;
2577 nameout (const void *nodep, VISIT value, int level)
2579 if (value == leaf || value == postorder)
2580 printf (" %s\n", *(const char **) nodep);
2586 compare_argstring (const void *p1, const void *p2)
2588 const struct argstring *a1 = (const struct argstring *) p1;
2589 const struct argstring *a2 = (const struct argstring *) p2;
2591 return strcmp (a1->str, a2->str);
2595 static int maxoff[3][3];
2596 static int minoff[3][3] = { { 1000, 1000, 1000 },
2597 { 1000, 1000, 1000 },
2598 { 1000, 1000, 1000 } };
2599 static int nbitoff[3][3];
2600 static void *fct_names[3];
2601 static int nbitfct[3];
2603 static void *strs[3];
2604 static int nbitstr[3];
2605 static int total_bits = 2; // Already counted the rep/repe bits.
2610 int nfct_names[3] = { 0, 0, 0 };
2611 int nstrs[3] = { 0, 0, 0 };
2613 /* We reverse the order of the instruction list while processing it.
2614 Later phases need it in the order in which the input file has
2616 struct instruction *reversed = NULL;
2618 struct instruction *runp = instructions;
2619 while (runp != NULL)
2621 for (int i = 0; i < 3; ++i)
2622 if (runp->operands[i].fct != NULL)
2624 struct argstring search = { .str = runp->operands[i].fct };
2625 if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
2627 struct argstring *newp = xmalloc (sizeof (*newp));
2628 newp->str = runp->operands[i].fct;
2630 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
2631 error (EXIT_FAILURE, errno, "tsearch");
2635 if (runp->operands[i].str != NULL)
2637 search.str = runp->operands[i].str;
2638 if (tfind (&search, &strs[i], compare_argstring) == NULL)
2640 struct argstring *newp = xmalloc (sizeof (*newp));
2641 newp->str = runp->operands[i].str;
2643 if (tsearch (newp, &strs[i], compare_argstring) == NULL)
2644 error (EXIT_FAILURE, errno, "tsearch");
2649 maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
2650 maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
2651 maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
2653 if (runp->operands[i].off1 > 0)
2654 minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
2655 if (runp->operands[i].off2 > 0)
2656 minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
2657 if (runp->operands[i].off3 > 0)
2658 minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
2661 struct instruction *old = runp;
2664 old->next = reversed;
2667 instructions = reversed;
2671 for (int i = 0; i < 3; ++i)
2673 // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
2674 // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
2676 if (minoff[i][0] == 1000)
2681 d = maxoff[i][0] - minoff[i][0];
2688 total_bits += nbitoff[i][0];
2691 if (minoff[i][1] == 1000)
2696 d = maxoff[i][1] - minoff[i][1];
2703 total_bits += nbitoff[i][1];
2706 if (minoff[i][2] == 1000)
2711 d = maxoff[i][2] - minoff[i][2];
2718 total_bits += nbitoff[i][2];
2720 // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
2730 total_bits += nbitfct[i];
2731 // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
2743 total_bits += nbitstr[i];
2746 // twalk (fct_names[i], nameout);
2757 total_bits += nbitsuf;
2758 // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
2763 compare_syn (const void *p1, const void *p2)
2765 const struct synonym *s1 = (const struct synonym *) p1;
2766 const struct synonym *s2 = (const struct synonym *) p2;
2768 return strcmp (s1->from, s2->from);
2773 compare_suf (const void *p1, const void *p2)
2775 const struct suffix *s1 = (const struct suffix *) p1;
2776 const struct suffix *s2 = (const struct suffix *) p2;
2778 return strcmp (s1->name, s2->name);
2782 static int count_op_str;
2783 static int off_op_str;
2785 print_op_str (const void *nodep, VISIT value,
2786 int level __attribute__ ((unused)))
2788 if (value == leaf || value == postorder)
2790 const char *str = (*(struct argstring **) nodep)->str;
2791 fprintf (outfile, "%s\n \"%s",
2792 count_op_str == 0 ? "" : "\\0\"", str);
2793 (*(struct argstring **) nodep)->idx = ++count_op_str;
2794 (*(struct argstring **) nodep)->off = off_op_str;
2795 off_op_str += strlen (str) + 1;
2801 print_op_str_idx (const void *nodep, VISIT value,
2802 int level __attribute__ ((unused)))
2804 if (value == leaf || value == postorder)
2805 printf (" %d,\n", (*(struct argstring **) nodep)->off);
2810 print_op_fct (const void *nodep, VISIT value,
2811 int level __attribute__ ((unused)))
2813 if (value == leaf || value == postorder)
2815 fprintf (outfile, " FCT_%s,\n", (*(struct argstring **) nodep)->str);
2816 (*(struct argstring **) nodep)->idx = ++count_op_str;
2822 # error "bogus NMNES value"
2826 instrtable_out (void)
2831 create_mnemonic_table ();
2833 fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
2835 fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
2836 lrint (ceil (log2 (NMNES))));
2838 fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
2839 for (int i = 0; i < 3; ++i)
2841 fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
2842 if (nbitstr[i] != 0)
2843 fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
2844 fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
2845 fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
2846 if (nbitoff[i][1] != 0)
2848 fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
2849 fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
2851 if (nbitoff[i][2] != 0)
2853 fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
2854 fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
2858 fputs ("\n#include <i386_data.h>\n\n", outfile);
2861 #define APPEND(a, b) APPEND_ (a, b)
2862 #define APPEND_(a, b) a##b
2863 #define EMIT_SUFFIX(suf) \
2864 fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
2874 fputc_unlocked ('\n', outfile);
2876 for (int i = 0; i < 3; ++i)
2880 fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n NULL,\n",
2882 twalk (fct_names[i], print_op_fct);
2883 fputs ("};\n", outfile);
2885 /* The operand strings. */
2886 if (nbitstr[i] != 0)
2890 fprintf (outfile, "static const char op%d_str[] =", i + 1);
2891 twalk (strs[i], print_op_str);
2892 fputs ("\";\n", outfile);
2894 fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
2896 twalk (strs[i], print_op_str_idx);
2897 fputs ("};\n", outfile);
2902 fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
2903 struct instruction *instr;
2904 for (instr = instructions; instr != NULL; instr = instr->next)
2906 fputs (" {", outfile);
2907 if (instr->mnemonic == (void *) -1l)
2908 fputs (" .mnemonic = MNE_INVALID,", outfile);
2910 fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
2911 fprintf (outfile, " .rep = %d,", instr->rep);
2912 fprintf (outfile, " .repe = %d,", instr->repe);
2913 fprintf (outfile, " .suffix = %d,", instr->suffix);
2914 fprintf (outfile, " .modrm = %d,", instr->modrm);
2916 for (int i = 0; i < 3; ++i)
2919 if (instr->operands[i].fct != NULL)
2921 struct argstring search = { .str = instr->operands[i].fct };
2922 struct argstring **res = tfind (&search, &fct_names[i],
2924 assert (res != NULL);
2927 fprintf (outfile, " .fct%d = %d,", i + 1, idx);
2930 if (instr->operands[i].str != NULL)
2932 struct argstring search = { .str = instr->operands[i].str };
2933 struct argstring **res = tfind (&search, &strs[i],
2935 assert (res != NULL);
2938 if (nbitstr[i] != 0)
2939 fprintf (outfile, " .str%d = %d,", i + 1, idx);
2941 fprintf (outfile, " .off%d_1 = %d,", i + 1,
2942 MAX (0, instr->operands[i].off1 - minoff[i][0]));
2944 if (nbitoff[i][1] != 0)
2945 fprintf (outfile, " .off%d_2 = %d,", i + 1,
2946 MAX (0, instr->operands[i].off2 - minoff[i][1]));
2948 if (nbitoff[i][2] != 0)
2949 fprintf (outfile, " .off%d_3 = %d,", i + 1,
2950 MAX (0, instr->operands[i].off3 - minoff[i][2]));
2953 fputs (" },\n", outfile);
2955 fputs ("};\n", outfile);
2957 fputs ("static const uint8_t match_data[] =\n{\n", outfile);
2959 for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
2961 /* First count the number of bytes. */
2962 size_t totalbits = 0;
2963 size_t zerobits = 0;
2964 bool leading_p = true;
2965 size_t leadingbits = 0;
2966 struct bitvalue *b = instr->bytes;
2969 if (b->type == zeroone)
2978 totalbits += b->field->bits;
2979 /* We must always count the mod/rm byte. */
2980 if (strncasecmp (b->field->name, "mod", 3) == 0)
2983 zerobits += b->field->bits;
2988 size_t nbytes = (totalbits - zerobits + 7) / 8;
2989 assert (nbytes > 0);
2990 size_t leadingbytes = leadingbits / 8;
2992 fprintf (outfile, " %#zx,", nbytes | (leadingbytes << 4));
2994 /* Now create the mask and byte values. */
3001 if (b->type == zeroone)
3003 byte = (byte << 1) | b->value;
3004 mask = (mask << 1) | 1;
3007 if (leadingbytes > 0)
3009 assert (mask == 0xff);
3010 fprintf (outfile, " %#" PRIx8 ",", byte);
3014 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
3016 byte = mask = nbits = 0;
3023 assert (leadingbytes == 0);
3025 unsigned long int remaining = b->field->bits;
3026 while (nbits + remaining > 8)
3028 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
3029 mask << (8 - nbits), byte << (8 - nbits));
3030 remaining = nbits + remaining - 8;
3031 byte = mask = nbits = 0;
3040 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
3041 byte = mask = nbits = 0;
3049 fputc_unlocked ('\n', outfile);
3051 fputs ("};\n", outfile);
3056 static size_t mnemonic_maxlen;
3057 static size_t mnemonic_minlen;
3059 which_chars (const char *str[], size_t nstr)
3061 char used_char[256];
3062 memset (used_char, '\0', sizeof (used_char));
3063 mnemonic_maxlen = 0;
3064 mnemonic_minlen = 10000;
3065 for (size_t cnt = 0; cnt < nstr; ++cnt)
3067 const unsigned char *cp = (const unsigned char *) str[cnt];
3068 mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
3069 mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
3071 used_char[*cp++] = 1;
3072 while (*cp != '\0');
3074 size_t nused_char = 0;
3075 for (size_t cnt = 0; cnt < 256; ++cnt)
3076 if (used_char[cnt] != 0)
3082 static const char **mnemonic_strs;
3083 static size_t nmnemonic_strs;
3085 add_mnemonics (const void *nodep, VISIT value,
3086 int level __attribute__ ((unused)))
3088 if (value == leaf || value == postorder)
3089 mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
3098 static struct charfreq pfxfreq[256];
3099 static struct charfreq sfxfreq[256];
3103 compare_freq (const void *p1, const void *p2)
3105 const struct charfreq *c1 = (const struct charfreq *) p1;
3106 const struct charfreq *c2 = (const struct charfreq *) p2;
3108 if (c1->freq > c2->freq)
3110 if (c1->freq < c2->freq)
3117 compute_pfxfreq (const char *str[], size_t nstr)
3119 memset (pfxfreq, '\0', sizeof (pfxfreq));
3121 for (size_t i = 0; i < nstr; ++i)
3124 for (size_t i = 0; i < nstr; ++i)
3125 ++pfxfreq[*((const unsigned char *) str[i])].freq;
3127 qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
3130 while (n < 256 && pfxfreq[n].freq != 0)
3143 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
3145 memset (sfxfreq, '\0', sizeof (sfxfreq));
3147 for (size_t i = 0; i < nstr; ++i)
3150 for (size_t i = 0; i < nstr; ++i)
3151 ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
3153 qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
3156 while (n < 256 && sfxfreq[n].freq != 0)
3163 create_mnemonic_table (void)
3165 mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
3167 twalk (mnemonics, add_mnemonics);
3169 (void) which_chars (mnemonic_strs, nmnemonic_strs);
3171 size_t best_so_far = 100000000;
3172 char *best_prefix = NULL;
3173 char *best_suffix = NULL;
3174 char *best_table = NULL;
3175 size_t best_table_size = 0;
3176 size_t best_table_bits = 0;
3177 size_t best_prefix_bits = 0;
3179 /* We can precompute the prefix characters. */
3180 size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
3182 /* Compute best size for string representation including explicit NUL. */
3183 for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
3185 char prefix[1 << pfxbits];
3187 for (i = 0; i < (1u << pfxbits) - 1; ++i)
3188 prefix[i] = pfxfreq[i].ch;
3191 struct strsnlen strsnlen[nmnemonic_strs];
3193 for (i = 0; i < nmnemonic_strs; ++i)
3195 if (strchr (prefix, *mnemonic_strs[i]) != NULL)
3196 strsnlen[i].str = mnemonic_strs[i] + 1;
3198 strsnlen[i].str = mnemonic_strs[i];
3199 strsnlen[i].len = strlen (strsnlen[i].str);
3202 /* With the prefixes gone, try to combine strings. */
3203 size_t nstrsnlen = 1;
3204 for (i = 1; i < nmnemonic_strs; ++i)
3207 for (j = 0; j < nstrsnlen; ++j)
3208 if (strsnlen[i].len > strsnlen[j].len
3209 && strcmp (strsnlen[j].str,
3210 strsnlen[i].str + (strsnlen[i].len
3211 - strsnlen[j].len)) == 0)
3213 strsnlen[j] = strsnlen[i];
3216 else if (strsnlen[i].len < strsnlen[j].len
3217 && strcmp (strsnlen[i].str,
3218 strsnlen[j].str + (strsnlen[j].len
3219 - strsnlen[i].len)) == 0)
3223 strsnlen[nstrsnlen++] = strsnlen[i];
3226 size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
3228 for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
3230 char suffix[1 << sfxbits];
3232 for (i = 0; i < (1u << sfxbits) - 1; ++i)
3233 suffix[i] = sfxfreq[i].ch;
3236 size_t newlen[nstrsnlen];
3238 for (i = 0; i < nstrsnlen; ++i)
3239 if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
3240 newlen[i] = strsnlen[i].len - 1;
3242 newlen[i] = strsnlen[i].len;
3245 memset (charused, '\0', sizeof (charused));
3246 size_t ncharused = 0;
3248 const char *tablestr[nstrsnlen];
3249 size_t ntablestr = 1;
3250 tablestr[0] = strsnlen[0].str;
3251 size_t table = newlen[0] + 1;
3252 for (i = 1; i < nstrsnlen; ++i)
3255 for (j = 0; j < ntablestr; ++j)
3256 if (newlen[i] > newlen[j]
3257 && memcmp (tablestr[j],
3258 strsnlen[i].str + (newlen[i] - newlen[j]),
3261 table += newlen[i] - newlen[j];
3262 tablestr[j] = strsnlen[i].str;
3263 newlen[j] = newlen[i];
3266 else if (newlen[i] < newlen[j]
3267 && memcmp (strsnlen[i].str,
3268 tablestr[j] + (newlen[j] - newlen[i]),
3274 table += newlen[i] + 1;
3275 tablestr[ntablestr] = strsnlen[i].str;
3276 newlen[ntablestr] = newlen[i];
3281 for (size_t x = 0; x < newlen[j]; ++x)
3282 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
3286 size_t ncharused_bits = 0;
3288 while (i < ncharused)
3294 size_t table_bits = 0;
3302 size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
3303 size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
3304 + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
3305 + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
3306 + (((total_bits + mnemonic_bits + 7) / 8)
3309 if (new_total < best_so_far)
3311 best_so_far = new_total;
3312 best_mnemonic_bits = mnemonic_bits;
3315 best_suffix = xstrdup (suffix);
3318 best_prefix = xstrdup (prefix);
3319 best_prefix_bits = pfxbits;
3321 best_table_size = table;
3322 best_table_bits = table_bits;
3323 char *cp = best_table = xrealloc (best_table, table);
3324 for (i = 0; i < ntablestr; ++i)
3326 assert (cp + newlen[i] + 1 <= best_table + table);
3327 cp = mempcpy (cp, tablestr[i], newlen[i]);
3330 assert (cp == best_table + table);
3335 fputs ("static const char mnemonic_table[] =\n\"", outfile);
3336 for (size_t i = 0; i < best_table_size; ++i)
3338 if (((i + 1) % 60) == 0)
3339 fputs ("\"\n\"", outfile);
3340 if (!isascii (best_table[i]) || !isprint (best_table[i]))
3341 fprintf (outfile, "\\%03o", best_table[i]);
3343 fputc (best_table[i], outfile);
3345 fputs ("\";\n", outfile);
3347 if (best_prefix[0] != '\0')
3349 "static const char prefix[%zu] = \"%s\";\n"
3350 "#define PREFIXCHAR_BITS %zu\n",
3351 strlen (best_prefix), best_prefix, best_prefix_bits);
3353 fputs ("#define NO_PREFIX\n", outfile);
3355 if (best_suffix[0] != '\0')
3356 fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
3357 strlen (best_suffix), best_suffix);
3359 fputs ("#define NO_SUFFIX\n", outfile);
3361 for (size_t i = 0; i < nmnemonic_strs; ++i)
3363 const char *mne = mnemonic_strs[i];
3366 char *cp = strchr (best_prefix, *mne);
3369 pfxval = 1 + (cp - best_prefix);
3373 size_t l = strlen (mne);
3376 cp = strchr (best_suffix, mne[l - 1]);
3379 sfxval = 1 + (cp - best_suffix);
3383 char *off = memmem (best_table, best_table_size, mne, l);
3384 while (off[l] != '\0')
3386 off = memmem (off + 1, best_table_size, mne, l);
3387 assert (off != NULL);
3390 fprintf (outfile, "#define MNE_%s %#zx\n",
3393 + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));