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 This file is free software; you can redistribute it and/or modify
84 it under the terms of either
86 * the GNU Lesser General Public License as published by the Free
87 Software Foundation; either version 3 of the License, or (at
88 your option) any later version
92 * the GNU General Public License as published by the Free
93 Software Foundation; either version 2 of the License, or (at
94 your option) any later version
96 or both in parallel, as here.
98 elfutils is distributed in the hope that it will be useful, but
99 WITHOUT ANY WARRANTY; without even the implied warranty of
100 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
101 General Public License for more details.
103 You should have received copies of the GNU General Public License and
104 the GNU Lesser General Public License along with this program. If
105 not, see <http://www.gnu.org/licenses/>. */
115 #include <inttypes.h>
124 #include <sys/param.h>
128 #define obstack_chunk_alloc xmalloc
129 #define obstack_chunk_free free
131 /* The error handler. */
132 static void yyerror (const char *s);
134 extern int yylex (void);
135 extern int i386_lineno;
136 extern char *infname;
139 struct known_bitfield
142 unsigned long int bits;
149 enum bittype { zeroone, field, failure } type;
153 struct known_bitfield *field;
155 struct bitvalue *next;
161 enum nametype { string, nfield } type;
165 struct known_bitfield *field;
167 struct argname *next;
173 struct argname *name;
174 struct argument *next;
180 /* The byte encoding. */
181 struct bitvalue *bytes;
183 /* Prefix possible. */
191 enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
192 suffix_w1, suffix_W1, suffix_D } suffix;
194 /* Flag set if modr/m is used. */
207 struct instruction *next;
233 static struct known_bitfield ax_reg =
235 .name = "ax", .bits = 0, .tmp = 0
238 static struct known_bitfield dx_reg =
240 .name = "dx", .bits = 0, .tmp = 0
243 static struct known_bitfield di_reg =
245 .name = "es_di", .bits = 0, .tmp = 0
248 static struct known_bitfield si_reg =
250 .name = "ds_si", .bits = 0, .tmp = 0
253 static struct known_bitfield bx_reg =
255 .name = "ds_bx", .bits = 0, .tmp = 0
259 static int bitfield_compare (const void *p1, const void *p2);
260 static void new_bitfield (char *name, unsigned long int num);
261 static void check_bits (struct bitvalue *value);
262 static int check_duplicates (struct bitvalue *val);
263 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
264 static int check_bitsused (struct bitvalue *bitval,
265 struct known_bitfield *suffix,
266 struct argument *args);
267 static struct argname *combine (struct argname *name);
268 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
269 struct instruction *instr, int n);
270 static void find_numbers (void);
271 static int compare_syn (const void *p1, const void *p2);
272 static int compare_suf (const void *p1, const void *p2);
273 static void instrtable_out (void);
275 static void create_mnemonic_table (void);
278 static void *bitfields;
279 static struct instruction *instructions;
280 static size_t ninstructions;
281 static void *synonyms;
282 static void *suffixes;
283 static int nsuffixes;
284 static void *mnemonics;
286 extern FILE *outfile;
288 /* Number of bits used mnemonics. */
290 static size_t best_mnemonic_bits;
294 /* Line 268 of yacc.c */
295 #line 296 "i386_parse.c"
297 /* Enabling traces. */
302 /* Enabling verbose error messages. */
303 #ifdef YYERROR_VERBOSE
304 # undef YYERROR_VERBOSE
305 # define YYERROR_VERBOSE 1
307 # define YYERROR_VERBOSE 0
310 /* Enabling the token table. */
311 #ifndef YYTOKEN_TABLE
312 # define YYTOKEN_TABLE 0
319 /* Put the tokens into the symbol table, so that GDB and other debuggers
341 #define kPERCPERC 264
342 #define kBITFIELD 265
349 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
350 typedef union YYSTYPE
353 /* Line 293 of yacc.c */
354 #line 217 "i386_parse.y"
356 unsigned long int num;
359 struct known_bitfield *field;
360 struct bitvalue *bit;
361 struct argname *name;
362 struct argument *arg;
366 /* Line 293 of yacc.c */
367 #line 368 "i386_parse.c"
369 # define YYSTYPE_IS_TRIVIAL 1
370 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
371 # define YYSTYPE_IS_DECLARED 1
375 /* Copy the second part of user declarations. */
378 /* Line 343 of yacc.c */
379 #line 380 "i386_parse.c"
386 typedef YYTYPE_UINT8 yytype_uint8;
388 typedef unsigned char yytype_uint8;
392 typedef YYTYPE_INT8 yytype_int8;
393 #elif (defined __STDC__ || defined __C99__FUNC__ \
394 || defined __cplusplus || defined _MSC_VER)
395 typedef signed char yytype_int8;
397 typedef short int yytype_int8;
401 typedef YYTYPE_UINT16 yytype_uint16;
403 typedef unsigned short int yytype_uint16;
407 typedef YYTYPE_INT16 yytype_int16;
409 typedef short int yytype_int16;
413 # ifdef __SIZE_TYPE__
414 # define YYSIZE_T __SIZE_TYPE__
415 # elif defined size_t
416 # define YYSIZE_T size_t
417 # elif ! defined YYSIZE_T && (defined __STDC__ || defined __C99__FUNC__ \
418 || defined __cplusplus || defined _MSC_VER)
419 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
420 # define YYSIZE_T size_t
422 # define YYSIZE_T unsigned int
426 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
429 # if defined YYENABLE_NLS && YYENABLE_NLS
431 # include <libintl.h> /* INFRINGES ON USER NAME SPACE */
432 # define YY_(msgid) dgettext ("bison-runtime", msgid)
436 # define YY_(msgid) msgid
440 /* Suppress unused-variable warnings by "using" E. */
441 #if ! defined lint || defined __GNUC__
442 # define YYUSE(e) ((void) (e))
444 # define YYUSE(e) /* empty */
447 /* Identity function, used to suppress warnings about constant conditions. */
451 #if (defined __STDC__ || defined __C99__FUNC__ \
452 || defined __cplusplus || defined _MSC_VER)
465 #if ! defined yyoverflow || YYERROR_VERBOSE
467 /* The parser invokes alloca or malloc; define the necessary symbols. */
469 # ifdef YYSTACK_USE_ALLOCA
470 # if YYSTACK_USE_ALLOCA
472 # define YYSTACK_ALLOC __builtin_alloca
473 # elif defined __BUILTIN_VA_ARG_INCR
474 # include <alloca.h> /* INFRINGES ON USER NAME SPACE */
476 # define YYSTACK_ALLOC __alloca
477 # elif defined _MSC_VER
478 # include <malloc.h> /* INFRINGES ON USER NAME SPACE */
479 # define alloca _alloca
481 # define YYSTACK_ALLOC alloca
482 # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
483 || defined __cplusplus || defined _MSC_VER)
484 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
485 # ifndef EXIT_SUCCESS
486 # define EXIT_SUCCESS 0
493 # ifdef YYSTACK_ALLOC
494 /* Pacify GCC's `empty if-body' warning. */
495 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
496 # ifndef YYSTACK_ALLOC_MAXIMUM
497 /* The OS might guarantee only one guard page at the bottom of the stack,
498 and a page size can be as small as 4096 bytes. So we cannot safely
499 invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
500 to allow for a few compiler-allocated temporary stack slots. */
501 # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
504 # define YYSTACK_ALLOC YYMALLOC
505 # define YYSTACK_FREE YYFREE
506 # ifndef YYSTACK_ALLOC_MAXIMUM
507 # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
509 # if (defined __cplusplus && ! defined EXIT_SUCCESS \
510 && ! ((defined YYMALLOC || defined malloc) \
511 && (defined YYFREE || defined free)))
512 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
513 # ifndef EXIT_SUCCESS
514 # define EXIT_SUCCESS 0
518 # define YYMALLOC malloc
519 # if ! defined malloc && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
520 || defined __cplusplus || defined _MSC_VER)
521 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
526 # if ! defined free && ! defined EXIT_SUCCESS && (defined __STDC__ || defined __C99__FUNC__ \
527 || defined __cplusplus || defined _MSC_VER)
528 void free (void *); /* INFRINGES ON USER NAME SPACE */
532 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
535 #if (! defined yyoverflow \
536 && (! defined __cplusplus \
537 || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
539 /* A type that is properly aligned for any stack member. */
542 yytype_int16 yyss_alloc;
546 /* The size of the maximum gap between one aligned stack and the next. */
547 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
549 /* The size of an array large to enough to hold all stacks, each with
551 # define YYSTACK_BYTES(N) \
552 ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
553 + YYSTACK_GAP_MAXIMUM)
555 # define YYCOPY_NEEDED 1
557 /* Relocate STACK from its old location to the new one. The
558 local variables YYSIZE and YYSTACKSIZE give the old and new number of
559 elements in the stack, and YYPTR gives the new location of the
560 stack. Advance YYPTR to a properly aligned location for the next
562 # define YYSTACK_RELOCATE(Stack_alloc, Stack) \
565 YYSIZE_T yynewbytes; \
566 YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \
567 Stack = &yyptr->Stack_alloc; \
568 yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
569 yyptr += yynewbytes / sizeof (*yyptr); \
575 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
576 /* Copy COUNT objects from FROM to TO. The source and destination do
579 # if defined __GNUC__ && 1 < __GNUC__
580 # define YYCOPY(To, From, Count) \
581 __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
583 # define YYCOPY(To, From, Count) \
587 for (yyi = 0; yyi < (Count); yyi++) \
588 (To)[yyi] = (From)[yyi]; \
593 #endif /* !YYCOPY_NEEDED */
595 /* YYFINAL -- State number of the termination state. */
597 /* YYLAST -- Last index in YYTABLE. */
600 /* YYNTOKENS -- Number of terminals. */
602 /* YYNNTS -- Number of nonterminals. */
604 /* YYNRULES -- Number of rules. */
606 /* YYNRULES -- Number of states. */
609 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
611 #define YYMAXUTOK 267
613 #define YYTRANSLATE(YYX) \
614 ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
616 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */
617 static const yytype_uint8 yytranslate[] =
619 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
620 13, 2, 2, 2, 2, 2, 2, 2, 2, 2,
621 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
622 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
623 2, 2, 2, 2, 15, 2, 2, 2, 16, 17,
624 2, 2, 2, 2, 2, 2, 2, 2, 14, 2,
625 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
626 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
627 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
628 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
629 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
630 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
631 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
632 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
633 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
634 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
635 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
636 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
637 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
638 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
639 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
640 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
641 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
642 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
643 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
644 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
645 5, 6, 7, 8, 9, 10, 11, 12
649 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
651 static const yytype_uint8 yyprhs[] =
653 0, 0, 3, 8, 12, 14, 18, 21, 24, 28,
654 29, 33, 35, 42, 43, 45, 46, 50, 52, 55,
655 57, 59, 61, 63, 66, 67, 71, 73, 76, 78,
659 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
660 static const yytype_int8 yyrhs[] =
662 19, 0, -1, 20, 9, 13, 22, -1, 20, 13,
663 21, -1, 21, -1, 3, 10, 8, -1, 4, 10,
664 -1, 5, 10, -1, 6, 10, 10, -1, -1, 22,
665 13, 23, -1, 23, -1, 25, 14, 24, 7, 24,
666 28, -1, -1, 10, -1, -1, 25, 15, 26, -1,
667 26, -1, 26, 27, -1, 27, -1, 16, -1, 17,
668 -1, 10, -1, 12, 29, -1, -1, 29, 15, 30,
669 -1, 30, -1, 30, 31, -1, 31, -1, 10, -1,
670 11, -1, 7, -1, 14, -1
673 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
674 static const yytype_uint16 yyrline[] =
676 0, 247, 247, 257, 258, 261, 263, 265, 267, 279,
677 282, 283, 286, 369, 372, 388, 391, 401, 408, 416,
678 420, 427, 434, 456, 459, 462, 472, 480, 488, 491,
683 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
684 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
685 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
686 static const char *const yytname[] =
688 "$end", "error", "$undefined", "kMASK", "kPREFIX", "kSUFFIX",
689 "kSYNONYM", "kID", "kNUMBER", "kPERCPERC", "kBITFIELD", "kCHAR",
690 "kSPACE", "'\\n'", "':'", "','", "'0'", "'1'", "$accept", "spec",
691 "masks", "mask", "instrs", "instr", "bitfieldopt", "bytes", "byte",
692 "bit", "optargs", "args", "arg", "argcomp", 0
697 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
699 static const yytype_uint16 yytoknum[] =
701 0, 256, 257, 258, 259, 260, 261, 262, 263, 264,
702 265, 266, 267, 10, 58, 44, 48, 49
706 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
707 static const yytype_uint8 yyr1[] =
709 0, 18, 19, 20, 20, 21, 21, 21, 21, 21,
710 22, 22, 23, 23, 24, 24, 25, 25, 26, 26,
711 27, 27, 27, 28, 28, 29, 29, 30, 30, 31,
715 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
716 static const yytype_uint8 yyr2[] =
718 0, 2, 4, 3, 1, 3, 2, 2, 3, 0,
719 3, 1, 6, 0, 1, 0, 3, 1, 2, 1,
720 1, 1, 1, 2, 0, 3, 1, 2, 1, 1,
724 /* YYDEFACT[STATE-NAME] -- Default reduction number in state STATE-NUM.
725 Performed when YYTABLE doesn't specify something else to do. Zero
726 means the default is an error. */
727 static const yytype_uint8 yydefact[] =
729 9, 0, 0, 0, 0, 0, 0, 4, 0, 6,
730 7, 0, 1, 0, 9, 5, 8, 13, 3, 22,
731 20, 21, 2, 11, 0, 17, 19, 13, 15, 0,
732 18, 10, 14, 0, 16, 15, 24, 0, 12, 31,
733 29, 30, 32, 23, 26, 28, 0, 27, 25
736 /* YYDEFGOTO[NTERM-NUM]. */
737 static const yytype_int8 yydefgoto[] =
739 -1, 5, 6, 7, 22, 23, 33, 24, 25, 26,
743 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
745 #define YYPACT_NINF -35
746 static const yytype_int8 yypact[] =
748 12, 9, 10, 11, 13, 22, -2, -35, 16, -35,
749 -35, 15, -35, 14, 12, -35, -35, -4, -35, -35,
750 -35, -35, 17, -35, -12, -4, -35, -4, 18, -4,
751 -35, -35, -35, 19, -4, 18, 20, -6, -35, -35,
752 -35, -35, -35, 21, -6, -35, -6, -35, -6
755 /* YYPGOTO[NTERM-NUM]. */
756 static const yytype_int8 yypgoto[] =
758 -35, -35, -35, 23, -35, 2, -1, -35, 4, -25,
762 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
763 positive, shift that token. If negative, reduce the rule which
764 number is the opposite. If YYTABLE_NINF, syntax error. */
765 #define YYTABLE_NINF -1
766 static const yytype_uint8 yytable[] =
768 30, 39, 28, 29, 40, 41, 19, 13, 42, 30,
769 47, 14, 20, 21, 47, 1, 2, 3, 4, 8,
770 9, 10, 12, 11, 15, 16, 35, 17, 32, 31,
771 27, 48, 37, 34, 36, 0, 46, 18
774 #define yypact_value_is_default(yystate) \
777 #define yytable_value_is_error(yytable_value) \
780 static const yytype_int8 yycheck[] =
782 25, 7, 14, 15, 10, 11, 10, 9, 14, 34,
783 44, 13, 16, 17, 48, 3, 4, 5, 6, 10,
784 10, 10, 0, 10, 8, 10, 7, 13, 10, 27,
785 13, 46, 12, 29, 35, -1, 15, 14
788 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
789 symbol of state STATE-NUM. */
790 static const yytype_uint8 yystos[] =
792 0, 3, 4, 5, 6, 19, 20, 21, 10, 10,
793 10, 10, 0, 9, 13, 8, 10, 13, 21, 10,
794 16, 17, 22, 23, 25, 26, 27, 13, 14, 15,
795 27, 23, 10, 24, 26, 7, 24, 12, 28, 7,
796 10, 11, 14, 29, 30, 31, 15, 31, 30
799 #define yyerrok (yyerrstatus = 0)
800 #define yyclearin (yychar = YYEMPTY)
804 #define YYACCEPT goto yyacceptlab
805 #define YYABORT goto yyabortlab
806 #define YYERROR goto yyerrorlab
809 /* Like YYERROR except do call yyerror. This remains here temporarily
810 to ease the transition to the new meaning of YYERROR, for GCC.
811 Once GCC version 2 has supplanted version 1, this can go. However,
812 YYFAIL appears to be in use. Nevertheless, it is formally deprecated
813 in Bison 2.4.2's NEWS entry, where a plan to phase it out is
816 #define YYFAIL goto yyerrlab
818 /* This is here to suppress warnings from the GCC cpp's
819 -Wunused-macros. Normally we don't worry about that warning, but
820 some users do, and we want to make it easy for users to remove
821 YYFAIL uses, which will produce warnings from Bison 2.5. */
824 #define YYRECOVERING() (!!yyerrstatus)
826 #define YYBACKUP(Token, Value) \
828 if (yychar == YYEMPTY && yylen == 1) \
837 yyerror (YY_("syntax error: cannot back up")); \
844 #define YYERRCODE 256
847 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
848 If N is 0, then set CURRENT to the empty location which ends
849 the previous symbol: RHS[0] (always defined). */
851 #define YYRHSLOC(Rhs, K) ((Rhs)[K])
852 #ifndef YYLLOC_DEFAULT
853 # define YYLLOC_DEFAULT(Current, Rhs, N) \
857 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
858 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
859 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
860 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
864 (Current).first_line = (Current).last_line = \
865 YYRHSLOC (Rhs, 0).last_line; \
866 (Current).first_column = (Current).last_column = \
867 YYRHSLOC (Rhs, 0).last_column; \
873 /* This macro is provided for backward compatibility. */
875 #ifndef YY_LOCATION_PRINT
876 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
880 /* YYLEX -- calling `yylex' with the right arguments. */
883 # define YYLEX yylex (YYLEX_PARAM)
885 # define YYLEX yylex ()
888 /* Enable debugging if requested. */
892 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
893 # define YYFPRINTF fprintf
896 # define YYDPRINTF(Args) \
902 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
906 YYFPRINTF (stderr, "%s ", Title); \
907 yy_symbol_print (stderr, \
909 YYFPRINTF (stderr, "\n"); \
914 /*--------------------------------.
915 | Print this symbol on YYOUTPUT. |
916 `--------------------------------*/
919 #if (defined __STDC__ || defined __C99__FUNC__ \
920 || defined __cplusplus || defined _MSC_VER)
922 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
925 yy_symbol_value_print (yyoutput, yytype, yyvaluep)
928 YYSTYPE const * const yyvaluep;
934 if (yytype < YYNTOKENS)
935 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
947 /*--------------------------------.
948 | Print this symbol on YYOUTPUT. |
949 `--------------------------------*/
951 #if (defined __STDC__ || defined __C99__FUNC__ \
952 || defined __cplusplus || defined _MSC_VER)
954 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
957 yy_symbol_print (yyoutput, yytype, yyvaluep)
960 YYSTYPE const * const yyvaluep;
963 if (yytype < YYNTOKENS)
964 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
966 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
968 yy_symbol_value_print (yyoutput, yytype, yyvaluep);
969 YYFPRINTF (yyoutput, ")");
972 /*------------------------------------------------------------------.
973 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
975 `------------------------------------------------------------------*/
977 #if (defined __STDC__ || defined __C99__FUNC__ \
978 || defined __cplusplus || defined _MSC_VER)
980 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
983 yy_stack_print (yybottom, yytop)
984 yytype_int16 *yybottom;
988 YYFPRINTF (stderr, "Stack now");
989 for (; yybottom <= yytop; yybottom++)
991 int yybot = *yybottom;
992 YYFPRINTF (stderr, " %d", yybot);
994 YYFPRINTF (stderr, "\n");
997 # define YY_STACK_PRINT(Bottom, Top) \
1000 yy_stack_print ((Bottom), (Top)); \
1004 /*------------------------------------------------.
1005 | Report that the YYRULE is going to be reduced. |
1006 `------------------------------------------------*/
1008 #if (defined __STDC__ || defined __C99__FUNC__ \
1009 || defined __cplusplus || defined _MSC_VER)
1011 yy_reduce_print (YYSTYPE *yyvsp, int yyrule)
1014 yy_reduce_print (yyvsp, yyrule)
1019 int yynrhs = yyr2[yyrule];
1021 unsigned long int yylno = yyrline[yyrule];
1022 YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
1024 /* The symbols being reduced. */
1025 for (yyi = 0; yyi < yynrhs; yyi++)
1027 YYFPRINTF (stderr, " $%d = ", yyi + 1);
1028 yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
1029 &(yyvsp[(yyi + 1) - (yynrhs)])
1031 YYFPRINTF (stderr, "\n");
1035 # define YY_REDUCE_PRINT(Rule) \
1038 yy_reduce_print (yyvsp, Rule); \
1041 /* Nonzero means print parse trace. It is left uninitialized so that
1042 multiple parsers can coexist. */
1044 #else /* !YYDEBUG */
1045 # define YYDPRINTF(Args)
1046 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
1047 # define YY_STACK_PRINT(Bottom, Top)
1048 # define YY_REDUCE_PRINT(Rule)
1049 #endif /* !YYDEBUG */
1052 /* YYINITDEPTH -- initial size of the parser's stacks. */
1054 # define YYINITDEPTH 200
1057 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1058 if the built-in stack extension method is used).
1060 Do not make this value too large; the results are undefined if
1061 YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1062 evaluated with infinite-precision integer arithmetic. */
1065 # define YYMAXDEPTH 10000
1072 # if defined __GLIBC__ && defined _STRING_H
1073 # define yystrlen strlen
1075 /* Return the length of YYSTR. */
1076 #if (defined __STDC__ || defined __C99__FUNC__ \
1077 || defined __cplusplus || defined _MSC_VER)
1079 yystrlen (const char *yystr)
1087 for (yylen = 0; yystr[yylen]; yylen++)
1095 # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1096 # define yystpcpy stpcpy
1098 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1100 #if (defined __STDC__ || defined __C99__FUNC__ \
1101 || defined __cplusplus || defined _MSC_VER)
1103 yystpcpy (char *yydest, const char *yysrc)
1106 yystpcpy (yydest, yysrc)
1112 const char *yys = yysrc;
1114 while ((*yyd++ = *yys++) != '\0')
1123 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1124 quotes and backslashes, so that it's suitable for yyerror. The
1125 heuristic is that double-quoting is unnecessary unless the string
1126 contains an apostrophe, a comma, or backslash (other than
1127 backslash-backslash). YYSTR is taken from yytname. If YYRES is
1128 null, do not copy; instead, return the length of what the result
1131 yytnamerr (char *yyres, const char *yystr)
1136 char const *yyp = yystr;
1143 goto do_not_strip_quotes;
1147 goto do_not_strip_quotes;
1160 do_not_strip_quotes: ;
1164 return yystrlen (yystr);
1166 return yystpcpy (yyres, yystr) - yyres;
1170 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1171 about the unexpected token YYTOKEN for the state stack whose top is
1174 Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is
1175 not large enough to hold the message. In that case, also set
1176 *YYMSG_ALLOC to the required number of bytes. Return 2 if the
1177 required number of bytes is too large to store. */
1179 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1180 yytype_int16 *yyssp, int yytoken)
1182 YYSIZE_T yysize0 = yytnamerr (0, yytname[yytoken]);
1183 YYSIZE_T yysize = yysize0;
1185 enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1186 /* Internationalized format string. */
1187 const char *yyformat = 0;
1188 /* Arguments of yyformat. */
1189 char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1190 /* Number of reported tokens (one for the "unexpected", one per
1194 /* There are many possibilities here to consider:
1195 - Assume YYFAIL is not used. It's too flawed to consider. See
1196 <http://lists.gnu.org/archive/html/bison-patches/2009-12/msg00024.html>
1197 for details. YYERROR is fine as it does not invoke this
1199 - If this state is a consistent state with a default action, then
1200 the only way this function was invoked is if the default action
1201 is an error action. In that case, don't check for expected
1202 tokens because there are none.
1203 - The only way there can be no lookahead present (in yychar) is if
1204 this state is a consistent state with a default action. Thus,
1205 detecting the absence of a lookahead is sufficient to determine
1206 that there is no unexpected or expected token to report. In that
1207 case, just report a simple "syntax error".
1208 - Don't assume there isn't a lookahead just because this state is a
1209 consistent state with a default action. There might have been a
1210 previous inconsistent state, consistent state with a non-default
1211 action, or user semantic action that manipulated yychar.
1212 - Of course, the expected token list depends on states to have
1213 correct lookahead information, and it depends on the parser not
1214 to perform extra reductions after fetching a lookahead from the
1215 scanner and before detecting a syntax error. Thus, state merging
1216 (from LALR or IELR) and default reductions corrupt the expected
1217 token list. However, the list is correct for canonical LR with
1218 one exception: it will still contain any token that will not be
1219 accepted due to an error action in a later state.
1221 if (yytoken != YYEMPTY)
1223 int yyn = yypact[*yyssp];
1224 yyarg[yycount++] = yytname[yytoken];
1225 if (!yypact_value_is_default (yyn))
1227 /* Start YYX at -YYN if negative to avoid negative indexes in
1228 YYCHECK. In other words, skip the first -YYN actions for
1229 this state because they are default actions. */
1230 int yyxbegin = yyn < 0 ? -yyn : 0;
1231 /* Stay within bounds of both yycheck and yytname. */
1232 int yychecklim = YYLAST - yyn + 1;
1233 int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1236 for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1237 if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1238 && !yytable_value_is_error (yytable[yyx + yyn]))
1240 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1246 yyarg[yycount++] = yytname[yyx];
1247 yysize1 = yysize + yytnamerr (0, yytname[yyx]);
1248 if (! (yysize <= yysize1
1249 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1258 # define YYCASE_(N, S) \
1262 YYCASE_(0, YY_("syntax error"));
1263 YYCASE_(1, YY_("syntax error, unexpected %s"));
1264 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1265 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1266 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1267 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1271 yysize1 = yysize + yystrlen (yyformat);
1272 if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1276 if (*yymsg_alloc < yysize)
1278 *yymsg_alloc = 2 * yysize;
1279 if (! (yysize <= *yymsg_alloc
1280 && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1281 *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1285 /* Avoid sprintf, as that infringes on the user's name space.
1286 Don't have undefined behavior even if the translation
1287 produced a string with the wrong number of "%s"s. */
1291 while ((*yyp = *yyformat) != '\0')
1292 if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1294 yyp += yytnamerr (yyp, yyarg[yyi++]);
1305 #endif /* YYERROR_VERBOSE */
1307 /*-----------------------------------------------.
1308 | Release the memory associated to this symbol. |
1309 `-----------------------------------------------*/
1312 #if (defined __STDC__ || defined __C99__FUNC__ \
1313 || defined __cplusplus || defined _MSC_VER)
1315 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1318 yydestruct (yymsg, yytype, yyvaluep)
1328 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1339 /* Prevent warnings from -Wmissing-prototypes. */
1340 #ifdef YYPARSE_PARAM
1341 #if defined __STDC__ || defined __cplusplus
1342 int yyparse (void *YYPARSE_PARAM);
1346 #else /* ! YYPARSE_PARAM */
1347 #if defined __STDC__ || defined __cplusplus
1352 #endif /* ! YYPARSE_PARAM */
1355 /* The lookahead symbol. */
1358 /* The semantic value of the lookahead symbol. */
1361 /* Number of syntax errors so far. */
1369 #ifdef YYPARSE_PARAM
1370 #if (defined __STDC__ || defined __C99__FUNC__ \
1371 || defined __cplusplus || defined _MSC_VER)
1373 yyparse (void *YYPARSE_PARAM)
1376 yyparse (YYPARSE_PARAM)
1377 void *YYPARSE_PARAM;
1379 #else /* ! YYPARSE_PARAM */
1380 #if (defined __STDC__ || defined __C99__FUNC__ \
1381 || defined __cplusplus || defined _MSC_VER)
1392 /* Number of tokens to shift before error messages enabled. */
1395 /* The stacks and their tools:
1396 `yyss': related to states.
1397 `yyvs': related to semantic values.
1399 Refer to the stacks thru separate pointers, to allow yyoverflow
1400 to reallocate them elsewhere. */
1402 /* The state stack. */
1403 yytype_int16 yyssa[YYINITDEPTH];
1405 yytype_int16 *yyssp;
1407 /* The semantic value stack. */
1408 YYSTYPE yyvsa[YYINITDEPTH];
1412 YYSIZE_T yystacksize;
1416 /* Lookahead token as an internal (translated) token number. */
1418 /* The variables used to return semantic value and location from the
1423 /* Buffer for error messages, and its allocated size. */
1425 char *yymsg = yymsgbuf;
1426 YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1429 #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
1431 /* The number of symbols on the RHS of the reduced rule.
1432 Keep to zero when no symbol should be popped. */
1438 yystacksize = YYINITDEPTH;
1440 YYDPRINTF ((stderr, "Starting parse\n"));
1445 yychar = YYEMPTY; /* Cause a token to be read. */
1447 /* Initialize stack pointers.
1448 Waste one element of value and location stack
1449 so that they stay on the same level as the state stack.
1450 The wasted elements are never initialized. */
1456 /*------------------------------------------------------------.
1457 | yynewstate -- Push a new state, which is found in yystate. |
1458 `------------------------------------------------------------*/
1460 /* In all cases, when you get here, the value and location stacks
1461 have just been pushed. So pushing a state here evens the stacks. */
1467 if (yyss + yystacksize - 1 <= yyssp)
1469 /* Get the current used size of the three stacks, in elements. */
1470 YYSIZE_T yysize = yyssp - yyss + 1;
1474 /* Give user a chance to reallocate the stack. Use copies of
1475 these so that the &'s don't force the real ones into
1477 YYSTYPE *yyvs1 = yyvs;
1478 yytype_int16 *yyss1 = yyss;
1480 /* Each stack pointer address is followed by the size of the
1481 data in use in that stack, in bytes. This used to be a
1482 conditional around just the two extra args, but that might
1483 be undefined if yyoverflow is a macro. */
1484 yyoverflow (YY_("memory exhausted"),
1485 &yyss1, yysize * sizeof (*yyssp),
1486 &yyvs1, yysize * sizeof (*yyvsp),
1492 #else /* no yyoverflow */
1493 # ifndef YYSTACK_RELOCATE
1494 goto yyexhaustedlab;
1496 /* Extend the stack our own way. */
1497 if (YYMAXDEPTH <= yystacksize)
1498 goto yyexhaustedlab;
1500 if (YYMAXDEPTH < yystacksize)
1501 yystacksize = YYMAXDEPTH;
1504 yytype_int16 *yyss1 = yyss;
1505 union yyalloc *yyptr =
1506 (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1508 goto yyexhaustedlab;
1509 YYSTACK_RELOCATE (yyss_alloc, yyss);
1510 YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1511 # undef YYSTACK_RELOCATE
1513 YYSTACK_FREE (yyss1);
1516 #endif /* no yyoverflow */
1518 yyssp = yyss + yysize - 1;
1519 yyvsp = yyvs + yysize - 1;
1521 YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1522 (unsigned long int) yystacksize));
1524 if (yyss + yystacksize - 1 <= yyssp)
1528 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1530 if (yystate == YYFINAL)
1540 /* Do appropriate processing given the current state. Read a
1541 lookahead token if we need one and don't already have one. */
1543 /* First try to decide what to do without reference to lookahead token. */
1544 yyn = yypact[yystate];
1545 if (yypact_value_is_default (yyn))
1548 /* Not known => get a lookahead token if don't already have one. */
1550 /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */
1551 if (yychar == YYEMPTY)
1553 YYDPRINTF ((stderr, "Reading a token: "));
1557 if (yychar <= YYEOF)
1559 yychar = yytoken = YYEOF;
1560 YYDPRINTF ((stderr, "Now at end of input.\n"));
1564 yytoken = YYTRANSLATE (yychar);
1565 YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1568 /* If the proper action on seeing token YYTOKEN is to reduce or to
1569 detect an error, take that action. */
1571 if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1576 if (yytable_value_is_error (yyn))
1582 /* Count tokens shifted since error; after three, turn off error
1587 /* Shift the lookahead token. */
1588 YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1590 /* Discard the shifted token. */
1599 /*-----------------------------------------------------------.
1600 | yydefault -- do the default action for the current state. |
1601 `-----------------------------------------------------------*/
1603 yyn = yydefact[yystate];
1609 /*-----------------------------.
1610 | yyreduce -- Do a reduction. |
1611 `-----------------------------*/
1613 /* yyn is the number of a rule to reduce with. */
1616 /* If YYLEN is nonzero, implement the default value of the action:
1619 Otherwise, the following line sets YYVAL to garbage.
1620 This behavior is undocumented and Bison
1621 users should not rely upon it. Assigning to YYVAL
1622 unconditionally makes the parser a bit smaller, and it avoids a
1623 GCC warning that YYVAL may be used uninitialized. */
1624 yyval = yyvsp[1-yylen];
1627 YY_REDUCE_PRINT (yyn);
1632 /* Line 1806 of yacc.c */
1633 #line 248 "i386_parse.y"
1635 if (error_message_count != 0)
1636 error (EXIT_FAILURE, 0,
1637 "terminated due to previous error");
1645 /* Line 1806 of yacc.c */
1646 #line 262 "i386_parse.y"
1647 { new_bitfield ((yyvsp[(2) - (3)].str), (yyvsp[(3) - (3)].num)); }
1652 /* Line 1806 of yacc.c */
1653 #line 264 "i386_parse.y"
1654 { new_bitfield ((yyvsp[(2) - (2)].str), -1); }
1659 /* Line 1806 of yacc.c */
1660 #line 266 "i386_parse.y"
1661 { new_bitfield ((yyvsp[(2) - (2)].str), -2); }
1666 /* Line 1806 of yacc.c */
1667 #line 268 "i386_parse.y"
1669 struct synonym *newp = xmalloc (sizeof (*newp));
1670 newp->from = (yyvsp[(2) - (3)].str);
1671 newp->to = (yyvsp[(3) - (3)].str);
1672 if (tfind (newp, &synonyms, compare_syn) != NULL)
1674 "%d: duplicate definition for synonym '%s'",
1675 i386_lineno, (yyvsp[(2) - (3)].str));
1676 else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
1677 error (EXIT_FAILURE, 0, "tsearch");
1683 /* Line 1806 of yacc.c */
1684 #line 287 "i386_parse.y"
1686 if ((yyvsp[(3) - (6)].field) != NULL && strcmp ((yyvsp[(3) - (6)].field)->name, "RE") != 0
1687 && strcmp ((yyvsp[(3) - (6)].field)->name, "R") != 0)
1689 error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
1692 if (check_duplicates ((yyvsp[(1) - (6)].bit)) == 0
1693 && check_argsdef ((yyvsp[(1) - (6)].bit), (yyvsp[(6) - (6)].arg)) == 0
1694 && check_bitsused ((yyvsp[(1) - (6)].bit), (yyvsp[(5) - (6)].field), (yyvsp[(6) - (6)].arg)) == 0)
1696 struct instruction *newp = xcalloc (sizeof (*newp),
1698 if ((yyvsp[(3) - (6)].field) != NULL)
1700 if (strcmp ((yyvsp[(3) - (6)].field)->name, "RE") == 0)
1702 else if (strcmp ((yyvsp[(3) - (6)].field)->name, "R") == 0)
1706 newp->bytes = (yyvsp[(1) - (6)].bit);
1707 newp->mnemonic = (yyvsp[(4) - (6)].str);
1708 if (newp->mnemonic != (void *) -1l
1709 && tfind ((yyvsp[(4) - (6)].str), &mnemonics,
1710 (comparison_fn_t) strcmp) == NULL)
1712 if (tsearch ((yyvsp[(4) - (6)].str), &mnemonics,
1713 (comparison_fn_t) strcmp) == NULL)
1714 error (EXIT_FAILURE, errno, "tsearch");
1718 if ((yyvsp[(5) - (6)].field) != NULL)
1720 if (strcmp ((yyvsp[(5) - (6)].field)->name, "w") == 0)
1721 newp->suffix = suffix_w;
1722 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w0") == 0)
1723 newp->suffix = suffix_w0;
1724 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "tttn") == 0)
1725 newp->suffix = suffix_tttn;
1726 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "w1") == 0)
1727 newp->suffix = suffix_w1;
1728 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W") == 0)
1729 newp->suffix = suffix_W;
1730 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "W1") == 0)
1731 newp->suffix = suffix_W1;
1732 else if (strcmp ((yyvsp[(5) - (6)].field)->name, "D") == 0)
1733 newp->suffix = suffix_D;
1735 error (EXIT_FAILURE, 0,
1736 "%s: %d: unknown suffix '%s'",
1737 infname, i386_lineno - 1, (yyvsp[(5) - (6)].field)->name);
1739 struct suffix search = { .name = (yyvsp[(5) - (6)].field)->name };
1740 if (tfind (&search, &suffixes, compare_suf)
1743 struct suffix *ns = xmalloc (sizeof (*ns));
1744 ns->name = (yyvsp[(5) - (6)].field)->name;
1745 ns->idx = ++nsuffixes;
1746 if (tsearch (ns, &suffixes, compare_suf)
1748 error (EXIT_FAILURE, errno, "tsearch");
1752 struct argument *args = (yyvsp[(6) - (6)].arg);
1754 while (args != NULL)
1756 fillin_arg ((yyvsp[(1) - (6)].bit), args->name, newp, n);
1762 newp->next = instructions;
1763 instructions = newp;
1771 /* Line 1806 of yacc.c */
1772 #line 373 "i386_parse.y"
1774 struct known_bitfield search;
1775 search.name = (yyvsp[(1) - (1)].str);
1776 struct known_bitfield **res;
1777 res = tfind (&search, &bitfields, bitfield_compare);
1780 error (0, 0, "%d: unknown bitfield '%s'",
1781 i386_lineno, search.name);
1782 (yyval.field) = NULL;
1785 (yyval.field) = *res;
1791 /* Line 1806 of yacc.c */
1792 #line 388 "i386_parse.y"
1793 { (yyval.field) = NULL; }
1798 /* Line 1806 of yacc.c */
1799 #line 392 "i386_parse.y"
1801 check_bits ((yyvsp[(3) - (3)].bit));
1803 struct bitvalue *runp = (yyvsp[(1) - (3)].bit);
1804 while (runp->next != NULL)
1806 runp->next = (yyvsp[(3) - (3)].bit);
1807 (yyval.bit) = (yyvsp[(1) - (3)].bit);
1813 /* Line 1806 of yacc.c */
1814 #line 402 "i386_parse.y"
1816 check_bits ((yyvsp[(1) - (1)].bit));
1817 (yyval.bit) = (yyvsp[(1) - (1)].bit);
1823 /* Line 1806 of yacc.c */
1824 #line 409 "i386_parse.y"
1826 struct bitvalue *runp = (yyvsp[(1) - (2)].bit);
1827 while (runp->next != NULL)
1829 runp->next = (yyvsp[(2) - (2)].bit);
1830 (yyval.bit) = (yyvsp[(1) - (2)].bit);
1836 /* Line 1806 of yacc.c */
1837 #line 417 "i386_parse.y"
1838 { (yyval.bit) = (yyvsp[(1) - (1)].bit); }
1843 /* Line 1806 of yacc.c */
1844 #line 421 "i386_parse.y"
1846 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1847 (yyval.bit)->type = zeroone;
1848 (yyval.bit)->value = 0;
1849 (yyval.bit)->next = NULL;
1855 /* Line 1806 of yacc.c */
1856 #line 428 "i386_parse.y"
1858 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1859 (yyval.bit)->type = zeroone;
1860 (yyval.bit)->value = 1;
1861 (yyval.bit)->next = NULL;
1867 /* Line 1806 of yacc.c */
1868 #line 435 "i386_parse.y"
1870 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1871 struct known_bitfield search;
1872 search.name = (yyvsp[(1) - (1)].str);
1873 struct known_bitfield **res;
1874 res = tfind (&search, &bitfields, bitfield_compare);
1877 error (0, 0, "%d: unknown bitfield '%s'",
1878 i386_lineno, search.name);
1879 (yyval.bit)->type = failure;
1883 (yyval.bit)->type = field;
1884 (yyval.bit)->field = *res;
1886 (yyval.bit)->next = NULL;
1892 /* Line 1806 of yacc.c */
1893 #line 457 "i386_parse.y"
1894 { (yyval.arg) = (yyvsp[(2) - (2)].arg); }
1899 /* Line 1806 of yacc.c */
1900 #line 459 "i386_parse.y"
1901 { (yyval.arg) = NULL; }
1906 /* Line 1806 of yacc.c */
1907 #line 463 "i386_parse.y"
1909 struct argument *runp = (yyvsp[(1) - (3)].arg);
1910 while (runp->next != NULL)
1912 runp->next = xmalloc (sizeof (struct argument));
1913 runp->next->name = combine ((yyvsp[(3) - (3)].name));
1914 runp->next->next = NULL;
1915 (yyval.arg) = (yyvsp[(1) - (3)].arg);
1921 /* Line 1806 of yacc.c */
1922 #line 473 "i386_parse.y"
1924 (yyval.arg) = xmalloc (sizeof (struct argument));
1925 (yyval.arg)->name = combine ((yyvsp[(1) - (1)].name));
1926 (yyval.arg)->next = NULL;
1932 /* Line 1806 of yacc.c */
1933 #line 481 "i386_parse.y"
1935 struct argname *runp = (yyvsp[(1) - (2)].name);
1936 while (runp->next != NULL)
1938 runp->next = (yyvsp[(2) - (2)].name);
1939 (yyval.name) = (yyvsp[(1) - (2)].name);
1945 /* Line 1806 of yacc.c */
1946 #line 489 "i386_parse.y"
1947 { (yyval.name) = (yyvsp[(1) - (1)].name); }
1952 /* Line 1806 of yacc.c */
1953 #line 492 "i386_parse.y"
1955 (yyval.name) = xmalloc (sizeof (struct argname));
1956 (yyval.name)->type = nfield;
1957 (yyval.name)->next = NULL;
1959 struct known_bitfield search;
1960 search.name = (yyvsp[(1) - (1)].str);
1961 struct known_bitfield **res;
1962 res = tfind (&search, &bitfields, bitfield_compare);
1965 if (strcmp ((yyvsp[(1) - (1)].str), "ax") == 0)
1966 (yyval.name)->field = &ax_reg;
1967 else if (strcmp ((yyvsp[(1) - (1)].str), "dx") == 0)
1968 (yyval.name)->field = &dx_reg;
1969 else if (strcmp ((yyvsp[(1) - (1)].str), "es_di") == 0)
1970 (yyval.name)->field = &di_reg;
1971 else if (strcmp ((yyvsp[(1) - (1)].str), "ds_si") == 0)
1972 (yyval.name)->field = &si_reg;
1973 else if (strcmp ((yyvsp[(1) - (1)].str), "ds_bx") == 0)
1974 (yyval.name)->field = &bx_reg;
1977 error (0, 0, "%d: unknown bitfield '%s'",
1978 i386_lineno, search.name);
1979 (yyval.name)->field = NULL;
1983 (yyval.name)->field = *res;
1989 /* Line 1806 of yacc.c */
1990 #line 524 "i386_parse.y"
1992 (yyval.name) = xmalloc (sizeof (struct argname));
1993 (yyval.name)->type = string;
1994 (yyval.name)->next = NULL;
1995 (yyval.name)->str = xmalloc (2);
1996 (yyval.name)->str[0] = (yyvsp[(1) - (1)].ch);
1997 (yyval.name)->str[1] = '\0';
2003 /* Line 1806 of yacc.c */
2004 #line 533 "i386_parse.y"
2006 (yyval.name) = xmalloc (sizeof (struct argname));
2007 (yyval.name)->type = string;
2008 (yyval.name)->next = NULL;
2009 (yyval.name)->str = (yyvsp[(1) - (1)].str);
2015 /* Line 1806 of yacc.c */
2016 #line 540 "i386_parse.y"
2018 (yyval.name) = xmalloc (sizeof (struct argname));
2019 (yyval.name)->type = string;
2020 (yyval.name)->next = NULL;
2021 (yyval.name)->str = xmalloc (2);
2022 (yyval.name)->str[0] = ':';
2023 (yyval.name)->str[1] = '\0';
2029 /* Line 1806 of yacc.c */
2030 #line 2031 "i386_parse.c"
2033 /* User semantic actions sometimes alter yychar, and that requires
2034 that yytoken be updated with the new translation. We take the
2035 approach of translating immediately before every use of yytoken.
2036 One alternative is translating here after every semantic action,
2037 but that translation would be missed if the semantic action invokes
2038 YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
2039 if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an
2040 incorrect destructor might then be invoked immediately. In the
2041 case of YYERROR or YYBACKUP, subsequent parser actions might lead
2042 to an incorrect destructor call or verbose syntax error message
2043 before the lookahead is translated. */
2044 YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
2048 YY_STACK_PRINT (yyss, yyssp);
2052 /* Now `shift' the result of the reduction. Determine what state
2053 that goes to, based on the state we popped back to and the rule
2054 number reduced by. */
2058 yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
2059 if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
2060 yystate = yytable[yystate];
2062 yystate = yydefgoto[yyn - YYNTOKENS];
2067 /*------------------------------------.
2068 | yyerrlab -- here on detecting error |
2069 `------------------------------------*/
2071 /* Make sure we have latest lookahead translation. See comments at
2072 user semantic actions for why this is necessary. */
2073 yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
2075 /* If not already recovering from an error, report this error. */
2079 #if ! YYERROR_VERBOSE
2080 yyerror (YY_("syntax error"));
2082 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
2085 char const *yymsgp = YY_("syntax error");
2086 int yysyntax_error_status;
2087 yysyntax_error_status = YYSYNTAX_ERROR;
2088 if (yysyntax_error_status == 0)
2090 else if (yysyntax_error_status == 1)
2092 if (yymsg != yymsgbuf)
2093 YYSTACK_FREE (yymsg);
2094 yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
2098 yymsg_alloc = sizeof yymsgbuf;
2099 yysyntax_error_status = 2;
2103 yysyntax_error_status = YYSYNTAX_ERROR;
2108 if (yysyntax_error_status == 2)
2109 goto yyexhaustedlab;
2111 # undef YYSYNTAX_ERROR
2117 if (yyerrstatus == 3)
2119 /* If just tried and failed to reuse lookahead token after an
2120 error, discard it. */
2122 if (yychar <= YYEOF)
2124 /* Return failure if at end of input. */
2125 if (yychar == YYEOF)
2130 yydestruct ("Error: discarding",
2136 /* Else will try to reuse lookahead token after shifting the error
2141 /*---------------------------------------------------.
2142 | yyerrorlab -- error raised explicitly by YYERROR. |
2143 `---------------------------------------------------*/
2146 /* Pacify compilers like GCC when the user code never invokes
2147 YYERROR and the label yyerrorlab therefore never appears in user
2149 if (/*CONSTCOND*/ 0)
2152 /* Do not reclaim the symbols of the rule which action triggered
2156 YY_STACK_PRINT (yyss, yyssp);
2161 /*-------------------------------------------------------------.
2162 | yyerrlab1 -- common code for both syntax error and YYERROR. |
2163 `-------------------------------------------------------------*/
2165 yyerrstatus = 3; /* Each real token shifted decrements this. */
2169 yyn = yypact[yystate];
2170 if (!yypact_value_is_default (yyn))
2173 if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
2181 /* Pop the current state because it cannot handle the error token. */
2186 yydestruct ("Error: popping",
2187 yystos[yystate], yyvsp);
2190 YY_STACK_PRINT (yyss, yyssp);
2196 /* Shift the error token. */
2197 YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
2203 /*-------------------------------------.
2204 | yyacceptlab -- YYACCEPT comes here. |
2205 `-------------------------------------*/
2210 /*-----------------------------------.
2211 | yyabortlab -- YYABORT comes here. |
2212 `-----------------------------------*/
2217 #if !defined(yyoverflow) || YYERROR_VERBOSE
2218 /*-------------------------------------------------.
2219 | yyexhaustedlab -- memory exhaustion comes here. |
2220 `-------------------------------------------------*/
2222 yyerror (YY_("memory exhausted"));
2228 if (yychar != YYEMPTY)
2230 /* Make sure we have latest lookahead translation. See comments at
2231 user semantic actions for why this is necessary. */
2232 yytoken = YYTRANSLATE (yychar);
2233 yydestruct ("Cleanup: discarding lookahead",
2236 /* Do not reclaim the symbols of the rule which action triggered
2237 this YYABORT or YYACCEPT. */
2239 YY_STACK_PRINT (yyss, yyssp);
2240 while (yyssp != yyss)
2242 yydestruct ("Cleanup: popping",
2243 yystos[*yyssp], yyvsp);
2248 YYSTACK_FREE (yyss);
2251 if (yymsg != yymsgbuf)
2252 YYSTACK_FREE (yymsg);
2254 /* Make sure YYID is used. */
2255 return YYID (yyresult);
2260 /* Line 2067 of yacc.c */
2261 #line 550 "i386_parse.y"
2265 yyerror (const char *s)
2267 error (0, 0, gettext ("while reading i386 CPU description: %s at line %d"),
2268 gettext (s), i386_lineno);
2273 bitfield_compare (const void *p1, const void *p2)
2275 struct known_bitfield *f1 = (struct known_bitfield *) p1;
2276 struct known_bitfield *f2 = (struct known_bitfield *) p2;
2278 return strcmp (f1->name, f2->name);
2283 new_bitfield (char *name, unsigned long int num)
2285 struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
2290 if (tfind (newp, &bitfields, bitfield_compare) != NULL)
2292 error (0, 0, "%d: duplicated definition of bitfield '%s'",
2298 if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
2299 error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
2304 /* Check that the number of bits is a multiple of 8. */
2306 check_bits (struct bitvalue *val)
2308 struct bitvalue *runp = val;
2309 unsigned int total = 0;
2311 while (runp != NULL)
2313 if (runp->type == zeroone)
2315 else if (runp->field == NULL)
2316 /* No sense doing anything, the field is not known. */
2319 total += runp->field->bits;
2331 if (val->type == zeroone)
2332 obstack_printf (&os, "%u", val->value);
2334 obstack_printf (&os, "{%s}", val->field->name);
2337 obstack_1grow (&os, '\0');
2339 error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
2340 i386_lineno, (char *) obstack_finish (&os));
2342 obstack_free (&os, NULL);
2348 check_duplicates (struct bitvalue *val)
2356 if (val->type == field && val->field != NULL)
2358 if (val->field->tmp == testcnt)
2360 error (0, 0, "%d: bitfield '%s' used more than once",
2361 i386_lineno - 1, val->field->name);
2364 val->field->tmp = testcnt;
2375 check_argsdef (struct bitvalue *bitval, struct argument *args)
2379 while (args != NULL)
2381 for (struct argname *name = args->name; name != NULL; name = name->next)
2382 if (name->type == nfield && name->field != NULL
2383 && name->field != &ax_reg && name->field != &dx_reg
2384 && name->field != &di_reg && name->field != &si_reg
2385 && name->field != &bx_reg)
2387 struct bitvalue *runp = bitval;
2389 while (runp != NULL)
2390 if (runp->type == field && runp->field == name->field)
2397 error (0, 0, "%d: unknown bitfield '%s' used in output format",
2398 i386_lineno - 1, name->field->name);
2411 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
2412 struct argument *args)
2416 while (bitval != NULL)
2418 if (bitval->type == field && bitval->field != NULL
2419 && bitval->field != suffix
2420 /* {w} is handled special. */
2421 && strcmp (bitval->field->name, "w") != 0)
2423 struct argument *runp;
2424 for (runp = args; runp != NULL; runp = runp->next)
2426 struct argname *name = runp->name;
2428 while (name != NULL)
2429 if (name->type == nfield && name->field == bitval->field)
2441 error (0, 0, "%d: bitfield '%s' not used",
2442 i386_lineno - 1, bitval->field->name);
2448 bitval = bitval->next;
2455 static struct argname *
2456 combine (struct argname *name)
2458 struct argname *last_str = NULL;
2459 for (struct argname *runp = name; runp != NULL; runp = runp->next)
2461 if (runp->type == string)
2463 if (last_str == NULL)
2467 last_str->str = xrealloc (last_str->str,
2468 strlen (last_str->str)
2469 + strlen (runp->str) + 1);
2470 strcat (last_str->str, runp->str);
2471 last_str->next = runp->next;
2481 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
2485 fillin_arg (struct bitvalue *bytes, struct argname *name,
2486 struct instruction *instr, int n)
2488 static struct obstack ob;
2489 static int initialized;
2496 struct argname *runp = name;
2498 while (runp != NULL)
2500 /* We ignore strings in the function name. */
2501 if (runp->type == string)
2503 if (instr->operands[n].str != NULL)
2504 error (EXIT_FAILURE, 0,
2505 "%d: cannot have more than one string parameter",
2508 instr->operands[n].str = runp->str;
2512 assert (runp->type == nfield);
2514 /* Construct the function name. */
2516 obstack_1grow (&ob, '$');
2518 if (runp->field == NULL)
2519 /* Add some string which contains invalid characters. */
2520 obstack_grow_str (&ob, "!!!INVALID!!!");
2523 char *fieldname = runp->field->name;
2525 struct synonym search = { .from = fieldname };
2527 struct synonym **res = tfind (&search, &synonyms, compare_syn);
2529 fieldname = (*res)->to;
2531 obstack_grow_str (&ob, fieldname);
2534 /* Now compute the bit offset of the field. */
2535 struct bitvalue *b = bytes;
2537 if (runp->field != NULL)
2540 if (b->type == field && b->field != NULL)
2542 if (strcmp (b->field->name, runp->field->name) == 0)
2544 bitoff += b->field->bits;
2551 if (instr->operands[n].off1 == 0)
2552 instr->operands[n].off1 = bitoff;
2553 else if (instr->operands[n].off2 == 0)
2554 instr->operands[n].off2 = bitoff;
2555 else if (instr->operands[n].off3 == 0)
2556 instr->operands[n].off3 = bitoff;
2558 error (EXIT_FAILURE, 0,
2559 "%d: cannot have more than three fields in parameter",
2562 if (runp->field != NULL
2563 && strncasecmp (runp->field->name, "mod", 3) == 0)
2569 if (obstack_object_size (&ob) == 0)
2570 obstack_grow_str (&ob, "string");
2571 obstack_1grow (&ob, '\0');
2572 char *fct = obstack_finish (&ob);
2574 instr->operands[n].fct = fct;
2580 nameout (const void *nodep, VISIT value, int level)
2582 if (value == leaf || value == postorder)
2583 printf (" %s\n", *(const char **) nodep);
2589 compare_argstring (const void *p1, const void *p2)
2591 const struct argstring *a1 = (const struct argstring *) p1;
2592 const struct argstring *a2 = (const struct argstring *) p2;
2594 return strcmp (a1->str, a2->str);
2598 static int maxoff[3][3];
2599 static int minoff[3][3] = { { 1000, 1000, 1000 },
2600 { 1000, 1000, 1000 },
2601 { 1000, 1000, 1000 } };
2602 static int nbitoff[3][3];
2603 static void *fct_names[3];
2604 static int nbitfct[3];
2606 static void *strs[3];
2607 static int nbitstr[3];
2608 static int total_bits = 2; // Already counted the rep/repe bits.
2613 int nfct_names[3] = { 0, 0, 0 };
2614 int nstrs[3] = { 0, 0, 0 };
2616 /* We reverse the order of the instruction list while processing it.
2617 Later phases need it in the order in which the input file has
2619 struct instruction *reversed = NULL;
2621 struct instruction *runp = instructions;
2622 while (runp != NULL)
2624 for (int i = 0; i < 3; ++i)
2625 if (runp->operands[i].fct != NULL)
2627 struct argstring search = { .str = runp->operands[i].fct };
2628 if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
2630 struct argstring *newp = xmalloc (sizeof (*newp));
2631 newp->str = runp->operands[i].fct;
2633 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
2634 error (EXIT_FAILURE, errno, "tsearch");
2638 if (runp->operands[i].str != NULL)
2640 search.str = runp->operands[i].str;
2641 if (tfind (&search, &strs[i], compare_argstring) == NULL)
2643 struct argstring *newp = xmalloc (sizeof (*newp));
2644 newp->str = runp->operands[i].str;
2646 if (tsearch (newp, &strs[i], compare_argstring) == NULL)
2647 error (EXIT_FAILURE, errno, "tsearch");
2652 maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
2653 maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
2654 maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
2656 if (runp->operands[i].off1 > 0)
2657 minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
2658 if (runp->operands[i].off2 > 0)
2659 minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
2660 if (runp->operands[i].off3 > 0)
2661 minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
2664 struct instruction *old = runp;
2667 old->next = reversed;
2670 instructions = reversed;
2674 for (int i = 0; i < 3; ++i)
2676 // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
2677 // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
2679 if (minoff[i][0] == 1000)
2684 d = maxoff[i][0] - minoff[i][0];
2691 total_bits += nbitoff[i][0];
2694 if (minoff[i][1] == 1000)
2699 d = maxoff[i][1] - minoff[i][1];
2706 total_bits += nbitoff[i][1];
2709 if (minoff[i][2] == 1000)
2714 d = maxoff[i][2] - minoff[i][2];
2721 total_bits += nbitoff[i][2];
2723 // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
2733 total_bits += nbitfct[i];
2734 // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
2746 total_bits += nbitstr[i];
2749 // twalk (fct_names[i], nameout);
2760 total_bits += nbitsuf;
2761 // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
2766 compare_syn (const void *p1, const void *p2)
2768 const struct synonym *s1 = (const struct synonym *) p1;
2769 const struct synonym *s2 = (const struct synonym *) p2;
2771 return strcmp (s1->from, s2->from);
2776 compare_suf (const void *p1, const void *p2)
2778 const struct suffix *s1 = (const struct suffix *) p1;
2779 const struct suffix *s2 = (const struct suffix *) p2;
2781 return strcmp (s1->name, s2->name);
2785 static int count_op_str;
2786 static int off_op_str;
2788 print_op_str (const void *nodep, VISIT value,
2789 int level __attribute__ ((unused)))
2791 if (value == leaf || value == postorder)
2793 const char *str = (*(struct argstring **) nodep)->str;
2794 fprintf (outfile, "%s\n \"%s",
2795 count_op_str == 0 ? "" : "\\0\"", str);
2796 (*(struct argstring **) nodep)->idx = ++count_op_str;
2797 (*(struct argstring **) nodep)->off = off_op_str;
2798 off_op_str += strlen (str) + 1;
2804 print_op_str_idx (const void *nodep, VISIT value,
2805 int level __attribute__ ((unused)))
2807 if (value == leaf || value == postorder)
2808 printf (" %d,\n", (*(struct argstring **) nodep)->off);
2813 print_op_fct (const void *nodep, VISIT value,
2814 int level __attribute__ ((unused)))
2816 if (value == leaf || value == postorder)
2818 fprintf (outfile, " FCT_%s,\n", (*(struct argstring **) nodep)->str);
2819 (*(struct argstring **) nodep)->idx = ++count_op_str;
2825 # error "bogus NMNES value"
2829 instrtable_out (void)
2834 create_mnemonic_table ();
2836 fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
2838 fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
2839 lrint (ceil (log2 (NMNES))));
2841 fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
2842 for (int i = 0; i < 3; ++i)
2844 fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
2845 if (nbitstr[i] != 0)
2846 fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
2847 fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
2848 fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
2849 if (nbitoff[i][1] != 0)
2851 fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
2852 fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
2854 if (nbitoff[i][2] != 0)
2856 fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
2857 fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
2861 fputs ("\n#include <i386_data.h>\n\n", outfile);
2864 #define APPEND(a, b) APPEND_ (a, b)
2865 #define APPEND_(a, b) a##b
2866 #define EMIT_SUFFIX(suf) \
2867 fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
2877 fputc_unlocked ('\n', outfile);
2879 for (int i = 0; i < 3; ++i)
2883 fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n NULL,\n",
2885 twalk (fct_names[i], print_op_fct);
2886 fputs ("};\n", outfile);
2888 /* The operand strings. */
2889 if (nbitstr[i] != 0)
2893 fprintf (outfile, "static const char op%d_str[] =", i + 1);
2894 twalk (strs[i], print_op_str);
2895 fputs ("\";\n", outfile);
2897 fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
2899 twalk (strs[i], print_op_str_idx);
2900 fputs ("};\n", outfile);
2905 fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
2906 struct instruction *instr;
2907 for (instr = instructions; instr != NULL; instr = instr->next)
2909 fputs (" {", outfile);
2910 if (instr->mnemonic == (void *) -1l)
2911 fputs (" .mnemonic = MNE_INVALID,", outfile);
2913 fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
2914 fprintf (outfile, " .rep = %d,", instr->rep);
2915 fprintf (outfile, " .repe = %d,", instr->repe);
2916 fprintf (outfile, " .suffix = %d,", instr->suffix);
2917 fprintf (outfile, " .modrm = %d,", instr->modrm);
2919 for (int i = 0; i < 3; ++i)
2922 if (instr->operands[i].fct != NULL)
2924 struct argstring search = { .str = instr->operands[i].fct };
2925 struct argstring **res = tfind (&search, &fct_names[i],
2927 assert (res != NULL);
2930 fprintf (outfile, " .fct%d = %d,", i + 1, idx);
2933 if (instr->operands[i].str != NULL)
2935 struct argstring search = { .str = instr->operands[i].str };
2936 struct argstring **res = tfind (&search, &strs[i],
2938 assert (res != NULL);
2941 if (nbitstr[i] != 0)
2942 fprintf (outfile, " .str%d = %d,", i + 1, idx);
2944 fprintf (outfile, " .off%d_1 = %d,", i + 1,
2945 MAX (0, instr->operands[i].off1 - minoff[i][0]));
2947 if (nbitoff[i][1] != 0)
2948 fprintf (outfile, " .off%d_2 = %d,", i + 1,
2949 MAX (0, instr->operands[i].off2 - minoff[i][1]));
2951 if (nbitoff[i][2] != 0)
2952 fprintf (outfile, " .off%d_3 = %d,", i + 1,
2953 MAX (0, instr->operands[i].off3 - minoff[i][2]));
2956 fputs (" },\n", outfile);
2958 fputs ("};\n", outfile);
2960 fputs ("static const uint8_t match_data[] =\n{\n", outfile);
2962 for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
2964 /* First count the number of bytes. */
2965 size_t totalbits = 0;
2966 size_t zerobits = 0;
2967 bool leading_p = true;
2968 size_t leadingbits = 0;
2969 struct bitvalue *b = instr->bytes;
2972 if (b->type == zeroone)
2981 totalbits += b->field->bits;
2982 /* We must always count the mod/rm byte. */
2983 if (strncasecmp (b->field->name, "mod", 3) == 0)
2986 zerobits += b->field->bits;
2991 size_t nbytes = (totalbits - zerobits + 7) / 8;
2992 assert (nbytes > 0);
2993 size_t leadingbytes = leadingbits / 8;
2995 fprintf (outfile, " %#zx,", nbytes | (leadingbytes << 4));
2997 /* Now create the mask and byte values. */
3004 if (b->type == zeroone)
3006 byte = (byte << 1) | b->value;
3007 mask = (mask << 1) | 1;
3010 if (leadingbytes > 0)
3012 assert (mask == 0xff);
3013 fprintf (outfile, " %#" PRIx8 ",", byte);
3017 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
3019 byte = mask = nbits = 0;
3026 assert (leadingbytes == 0);
3028 unsigned long int remaining = b->field->bits;
3029 while (nbits + remaining > 8)
3031 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
3032 mask << (8 - nbits), byte << (8 - nbits));
3033 remaining = nbits + remaining - 8;
3034 byte = mask = nbits = 0;
3043 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
3044 byte = mask = nbits = 0;
3052 fputc_unlocked ('\n', outfile);
3054 fputs ("};\n", outfile);
3059 static size_t mnemonic_maxlen;
3060 static size_t mnemonic_minlen;
3062 which_chars (const char *str[], size_t nstr)
3064 char used_char[256];
3065 memset (used_char, '\0', sizeof (used_char));
3066 mnemonic_maxlen = 0;
3067 mnemonic_minlen = 10000;
3068 for (size_t cnt = 0; cnt < nstr; ++cnt)
3070 const unsigned char *cp = (const unsigned char *) str[cnt];
3071 mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
3072 mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
3074 used_char[*cp++] = 1;
3075 while (*cp != '\0');
3077 size_t nused_char = 0;
3078 for (size_t cnt = 0; cnt < 256; ++cnt)
3079 if (used_char[cnt] != 0)
3085 static const char **mnemonic_strs;
3086 static size_t nmnemonic_strs;
3088 add_mnemonics (const void *nodep, VISIT value,
3089 int level __attribute__ ((unused)))
3091 if (value == leaf || value == postorder)
3092 mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
3101 static struct charfreq pfxfreq[256];
3102 static struct charfreq sfxfreq[256];
3106 compare_freq (const void *p1, const void *p2)
3108 const struct charfreq *c1 = (const struct charfreq *) p1;
3109 const struct charfreq *c2 = (const struct charfreq *) p2;
3111 if (c1->freq > c2->freq)
3113 if (c1->freq < c2->freq)
3120 compute_pfxfreq (const char *str[], size_t nstr)
3122 memset (pfxfreq, '\0', sizeof (pfxfreq));
3124 for (size_t i = 0; i < nstr; ++i)
3127 for (size_t i = 0; i < nstr; ++i)
3128 ++pfxfreq[*((const unsigned char *) str[i])].freq;
3130 qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
3133 while (n < 256 && pfxfreq[n].freq != 0)
3146 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
3148 memset (sfxfreq, '\0', sizeof (sfxfreq));
3150 for (size_t i = 0; i < nstr; ++i)
3153 for (size_t i = 0; i < nstr; ++i)
3154 ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
3156 qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
3159 while (n < 256 && sfxfreq[n].freq != 0)
3166 create_mnemonic_table (void)
3168 mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
3170 twalk (mnemonics, add_mnemonics);
3172 (void) which_chars (mnemonic_strs, nmnemonic_strs);
3174 size_t best_so_far = 100000000;
3175 char *best_prefix = NULL;
3176 char *best_suffix = NULL;
3177 char *best_table = NULL;
3178 size_t best_table_size = 0;
3179 size_t best_table_bits = 0;
3180 size_t best_prefix_bits = 0;
3182 /* We can precompute the prefix characters. */
3183 size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
3185 /* Compute best size for string representation including explicit NUL. */
3186 for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
3188 char prefix[1 << pfxbits];
3190 for (i = 0; i < (1u << pfxbits) - 1; ++i)
3191 prefix[i] = pfxfreq[i].ch;
3194 struct strsnlen strsnlen[nmnemonic_strs];
3196 for (i = 0; i < nmnemonic_strs; ++i)
3198 if (strchr (prefix, *mnemonic_strs[i]) != NULL)
3199 strsnlen[i].str = mnemonic_strs[i] + 1;
3201 strsnlen[i].str = mnemonic_strs[i];
3202 strsnlen[i].len = strlen (strsnlen[i].str);
3205 /* With the prefixes gone, try to combine strings. */
3206 size_t nstrsnlen = 1;
3207 for (i = 1; i < nmnemonic_strs; ++i)
3210 for (j = 0; j < nstrsnlen; ++j)
3211 if (strsnlen[i].len > strsnlen[j].len
3212 && strcmp (strsnlen[j].str,
3213 strsnlen[i].str + (strsnlen[i].len
3214 - strsnlen[j].len)) == 0)
3216 strsnlen[j] = strsnlen[i];
3219 else if (strsnlen[i].len < strsnlen[j].len
3220 && strcmp (strsnlen[i].str,
3221 strsnlen[j].str + (strsnlen[j].len
3222 - strsnlen[i].len)) == 0)
3226 strsnlen[nstrsnlen++] = strsnlen[i];
3229 size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
3231 for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
3233 char suffix[1 << sfxbits];
3235 for (i = 0; i < (1u << sfxbits) - 1; ++i)
3236 suffix[i] = sfxfreq[i].ch;
3239 size_t newlen[nstrsnlen];
3241 for (i = 0; i < nstrsnlen; ++i)
3242 if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
3243 newlen[i] = strsnlen[i].len - 1;
3245 newlen[i] = strsnlen[i].len;
3248 memset (charused, '\0', sizeof (charused));
3249 size_t ncharused = 0;
3251 const char *tablestr[nstrsnlen];
3252 size_t ntablestr = 1;
3253 tablestr[0] = strsnlen[0].str;
3254 size_t table = newlen[0] + 1;
3255 for (i = 1; i < nstrsnlen; ++i)
3258 for (j = 0; j < ntablestr; ++j)
3259 if (newlen[i] > newlen[j]
3260 && memcmp (tablestr[j],
3261 strsnlen[i].str + (newlen[i] - newlen[j]),
3264 table += newlen[i] - newlen[j];
3265 tablestr[j] = strsnlen[i].str;
3266 newlen[j] = newlen[i];
3269 else if (newlen[i] < newlen[j]
3270 && memcmp (strsnlen[i].str,
3271 tablestr[j] + (newlen[j] - newlen[i]),
3277 table += newlen[i] + 1;
3278 tablestr[ntablestr] = strsnlen[i].str;
3279 newlen[ntablestr] = newlen[i];
3284 for (size_t x = 0; x < newlen[j]; ++x)
3285 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
3289 size_t ncharused_bits = 0;
3291 while (i < ncharused)
3297 size_t table_bits = 0;
3305 size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
3306 size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
3307 + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
3308 + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
3309 + (((total_bits + mnemonic_bits + 7) / 8)
3312 if (new_total < best_so_far)
3314 best_so_far = new_total;
3315 best_mnemonic_bits = mnemonic_bits;
3318 best_suffix = xstrdup (suffix);
3321 best_prefix = xstrdup (prefix);
3322 best_prefix_bits = pfxbits;
3324 best_table_size = table;
3325 best_table_bits = table_bits;
3326 char *cp = best_table = xrealloc (best_table, table);
3327 for (i = 0; i < ntablestr; ++i)
3329 assert (cp + newlen[i] + 1 <= best_table + table);
3330 cp = mempcpy (cp, tablestr[i], newlen[i]);
3333 assert (cp == best_table + table);
3338 fputs ("static const char mnemonic_table[] =\n\"", outfile);
3339 for (size_t i = 0; i < best_table_size; ++i)
3341 if (((i + 1) % 60) == 0)
3342 fputs ("\"\n\"", outfile);
3343 if (!isascii (best_table[i]) || !isprint (best_table[i]))
3344 fprintf (outfile, "\\%03o", best_table[i]);
3346 fputc (best_table[i], outfile);
3348 fputs ("\";\n", outfile);
3350 if (best_prefix[0] != '\0')
3352 "static const char prefix[%zu] = \"%s\";\n"
3353 "#define PREFIXCHAR_BITS %zu\n",
3354 strlen (best_prefix), best_prefix, best_prefix_bits);
3356 fputs ("#define NO_PREFIX\n", outfile);
3358 if (best_suffix[0] != '\0')
3359 fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
3360 strlen (best_suffix), best_suffix);
3362 fputs ("#define NO_SUFFIX\n", outfile);
3364 for (size_t i = 0; i < nmnemonic_strs; ++i)
3366 const char *mne = mnemonic_strs[i];
3369 char *cp = strchr (best_prefix, *mne);
3372 pfxval = 1 + (cp - best_prefix);
3376 size_t l = strlen (mne);
3379 cp = strchr (best_suffix, mne[l - 1]);
3382 sfxval = 1 + (cp - best_suffix);
3386 char *off = memmem (best_table, best_table_size, mne, l);
3387 while (off[l] != '\0')
3389 off = memmem (off + 1, best_table_size, mne, l);
3390 assert (off != NULL);
3393 fprintf (outfile, "#define MNE_%s %#zx\n",
3396 + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));