1 /* A Bison parser, made by GNU Bison 3.0.4. */
3 /* Bison implementation for Yacc-like parsers in C
5 Copyright (C) 1984, 1989-1990, 2000-2015 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 "3.0.4"
50 #define YYSKELETON_NAME "yacc.c"
62 /* Substitute the variable and function names. */
63 #define yyparse i386_parse
64 #define yylex i386_lex
65 #define yyerror i386_error
66 #define yydebug i386_debug
67 #define yynerrs i386_nerrs
69 #define yylval i386_lval
70 #define yychar i386_char
72 /* Copy the first part of user declarations. */
73 #line 1 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:339 */
75 /* Parser for i386 CPU description.
76 Copyright (C) 2004, 2005, 2007, 2008, 2009 Red Hat, Inc.
77 Written by Ulrich Drepper <drepper@redhat.com>, 2004.
79 This file is free software; you can redistribute it and/or modify
80 it under the terms of either
82 * the GNU Lesser General Public License as published by the Free
83 Software Foundation; either version 3 of the License, or (at
84 your option) any later version
88 * the GNU General Public License as published by the Free
89 Software Foundation; either version 2 of the License, or (at
90 your option) any later version
92 or both in parallel, as here.
94 elfutils is distributed in the hope that it will be useful, but
95 WITHOUT ANY WARRANTY; without even the implied warranty of
96 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
97 General Public License for more details.
99 You should have received copies of the GNU General Public License and
100 the GNU Lesser General Public License along with this program. If
101 not, see <http://www.gnu.org/licenses/>. */
111 #include <inttypes.h>
120 #include <sys/param.h>
124 #define obstack_chunk_alloc xmalloc
125 #define obstack_chunk_free free
127 /* The error handler. */
128 static void yyerror (const char *s);
130 extern int yylex (void);
131 extern int i386_lineno;
132 extern char *infname;
135 struct known_bitfield
138 unsigned long int bits;
145 enum bittype { zeroone, field, failure } type;
149 struct known_bitfield *field;
151 struct bitvalue *next;
157 enum nametype { string, nfield } type;
161 struct known_bitfield *field;
163 struct argname *next;
169 struct argname *name;
170 struct argument *next;
176 /* The byte encoding. */
177 struct bitvalue *bytes;
179 /* Prefix possible. */
187 enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
188 suffix_w1, suffix_W1, suffix_D } suffix;
190 /* Flag set if modr/m is used. */
203 struct instruction *next;
229 static struct known_bitfield ax_reg =
231 .name = "ax", .bits = 0, .tmp = 0
234 static struct known_bitfield dx_reg =
236 .name = "dx", .bits = 0, .tmp = 0
239 static struct known_bitfield di_reg =
241 .name = "es_di", .bits = 0, .tmp = 0
244 static struct known_bitfield si_reg =
246 .name = "ds_si", .bits = 0, .tmp = 0
249 static struct known_bitfield bx_reg =
251 .name = "ds_bx", .bits = 0, .tmp = 0
255 static int bitfield_compare (const void *p1, const void *p2);
256 static void new_bitfield (char *name, unsigned long int num);
257 static void check_bits (struct bitvalue *value);
258 static int check_duplicates (struct bitvalue *val);
259 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
260 static int check_bitsused (struct bitvalue *bitval,
261 struct known_bitfield *suffix,
262 struct argument *args);
263 static struct argname *combine (struct argname *name);
264 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
265 struct instruction *instr, int n);
266 static void find_numbers (void);
267 static int compare_syn (const void *p1, const void *p2);
268 static int compare_suf (const void *p1, const void *p2);
269 static void instrtable_out (void);
271 static void create_mnemonic_table (void);
274 static void *bitfields;
275 static struct instruction *instructions;
276 static size_t ninstructions;
277 static void *synonyms;
278 static void *suffixes;
279 static int nsuffixes;
280 static void *mnemonics;
282 extern FILE *outfile;
284 /* Number of bits used mnemonics. */
286 static size_t best_mnemonic_bits;
289 #line 290 "i386_parse.c" /* yacc.c:339 */
292 # if defined __cplusplus && 201103L <= __cplusplus
293 # define YY_NULLPTR nullptr
295 # define YY_NULLPTR 0
299 /* Enabling verbose error messages. */
300 #ifdef YYERROR_VERBOSE
301 # undef YYERROR_VERBOSE
302 # define YYERROR_VERBOSE 1
304 # define YYERROR_VERBOSE 0
307 /* In a future release of Bison, this section will be replaced
308 by #include "y.tab.h". */
309 #ifndef YY_I386_I_PARSE_H_INCLUDED
310 # define YY_I386_I_PARSE_H_INCLUDED
316 extern int i386_debug;
343 #define kPERCPERC 264
344 #define kBITFIELD 265
349 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
353 #line 217 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:355 */
355 unsigned long int num;
358 struct known_bitfield *field;
359 struct bitvalue *bit;
360 struct argname *name;
361 struct argument *arg;
363 #line 364 "i386_parse.c" /* yacc.c:355 */
366 typedef union YYSTYPE YYSTYPE;
367 # define YYSTYPE_IS_TRIVIAL 1
368 # define YYSTYPE_IS_DECLARED 1
372 extern YYSTYPE i386_lval;
374 int i386_parse (void);
376 #endif /* !YY_I386_I_PARSE_H_INCLUDED */
378 /* Copy the second part of user declarations. */
380 #line 381 "i386_parse.c" /* yacc.c:358 */
387 typedef YYTYPE_UINT8 yytype_uint8;
389 typedef unsigned char yytype_uint8;
393 typedef YYTYPE_INT8 yytype_int8;
395 typedef signed char yytype_int8;
399 typedef YYTYPE_UINT16 yytype_uint16;
401 typedef unsigned short int yytype_uint16;
405 typedef YYTYPE_INT16 yytype_int16;
407 typedef short int yytype_int16;
411 # ifdef __SIZE_TYPE__
412 # define YYSIZE_T __SIZE_TYPE__
413 # elif defined size_t
414 # define YYSIZE_T size_t
415 # elif ! defined YYSIZE_T
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
438 # if (defined __GNUC__ \
439 && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \
440 || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C
441 # define YY_ATTRIBUTE(Spec) __attribute__(Spec)
443 # define YY_ATTRIBUTE(Spec) /* empty */
447 #ifndef YY_ATTRIBUTE_PURE
448 # define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__))
451 #ifndef YY_ATTRIBUTE_UNUSED
452 # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__))
455 #if !defined _Noreturn \
456 && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112)
457 # if defined _MSC_VER && 1200 <= _MSC_VER
458 # define _Noreturn __declspec (noreturn)
460 # define _Noreturn YY_ATTRIBUTE ((__noreturn__))
464 /* Suppress unused-variable warnings by "using" E. */
465 #if ! defined lint || defined __GNUC__
466 # define YYUSE(E) ((void) (E))
468 # define YYUSE(E) /* empty */
471 #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
472 /* Suppress an incorrect diagnostic about yylval being uninitialized. */
473 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
474 _Pragma ("GCC diagnostic push") \
475 _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\
476 _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
477 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
478 _Pragma ("GCC diagnostic pop")
480 # define YY_INITIAL_VALUE(Value) Value
482 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
483 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
484 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
486 #ifndef YY_INITIAL_VALUE
487 # define YY_INITIAL_VALUE(Value) /* Nothing. */
491 #if ! defined yyoverflow || YYERROR_VERBOSE
493 /* The parser invokes alloca or malloc; define the necessary symbols. */
495 # ifdef YYSTACK_USE_ALLOCA
496 # if YYSTACK_USE_ALLOCA
498 # define YYSTACK_ALLOC __builtin_alloca
499 # elif defined __BUILTIN_VA_ARG_INCR
500 # include <alloca.h> /* INFRINGES ON USER NAME SPACE */
502 # define YYSTACK_ALLOC __alloca
503 # elif defined _MSC_VER
504 # include <malloc.h> /* INFRINGES ON USER NAME SPACE */
505 # define alloca _alloca
507 # define YYSTACK_ALLOC alloca
508 # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS
509 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
510 /* Use EXIT_SUCCESS as a witness for stdlib.h. */
511 # ifndef EXIT_SUCCESS
512 # define EXIT_SUCCESS 0
519 # ifdef YYSTACK_ALLOC
520 /* Pacify GCC's 'empty if-body' warning. */
521 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
522 # ifndef YYSTACK_ALLOC_MAXIMUM
523 /* The OS might guarantee only one guard page at the bottom of the stack,
524 and a page size can be as small as 4096 bytes. So we cannot safely
525 invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
526 to allow for a few compiler-allocated temporary stack slots. */
527 # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
530 # define YYSTACK_ALLOC YYMALLOC
531 # define YYSTACK_FREE YYFREE
532 # ifndef YYSTACK_ALLOC_MAXIMUM
533 # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
535 # if (defined __cplusplus && ! defined EXIT_SUCCESS \
536 && ! ((defined YYMALLOC || defined malloc) \
537 && (defined YYFREE || defined free)))
538 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
539 # ifndef EXIT_SUCCESS
540 # define EXIT_SUCCESS 0
544 # define YYMALLOC malloc
545 # if ! defined malloc && ! defined EXIT_SUCCESS
546 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
551 # if ! defined free && ! defined EXIT_SUCCESS
552 void free (void *); /* INFRINGES ON USER NAME SPACE */
556 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
559 #if (! defined yyoverflow \
560 && (! defined __cplusplus \
561 || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
563 /* A type that is properly aligned for any stack member. */
566 yytype_int16 yyss_alloc;
570 /* The size of the maximum gap between one aligned stack and the next. */
571 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
573 /* The size of an array large to enough to hold all stacks, each with
575 # define YYSTACK_BYTES(N) \
576 ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
577 + YYSTACK_GAP_MAXIMUM)
579 # define YYCOPY_NEEDED 1
581 /* Relocate STACK from its old location to the new one. The
582 local variables YYSIZE and YYSTACKSIZE give the old and new number of
583 elements in the stack, and YYPTR gives the new location of the
584 stack. Advance YYPTR to a properly aligned location for the next
586 # define YYSTACK_RELOCATE(Stack_alloc, Stack) \
589 YYSIZE_T yynewbytes; \
590 YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \
591 Stack = &yyptr->Stack_alloc; \
592 yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
593 yyptr += yynewbytes / sizeof (*yyptr); \
599 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
600 /* Copy COUNT objects from SRC to DST. The source and destination do
603 # if defined __GNUC__ && 1 < __GNUC__
604 # define YYCOPY(Dst, Src, Count) \
605 __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src)))
607 # define YYCOPY(Dst, Src, Count) \
611 for (yyi = 0; yyi < (Count); yyi++) \
612 (Dst)[yyi] = (Src)[yyi]; \
617 #endif /* !YYCOPY_NEEDED */
619 /* YYFINAL -- State number of the termination state. */
621 /* YYLAST -- Last index in YYTABLE. */
624 /* YYNTOKENS -- Number of terminals. */
626 /* YYNNTS -- Number of nonterminals. */
628 /* YYNRULES -- Number of rules. */
630 /* YYNSTATES -- Number of states. */
633 /* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned
634 by yylex, with out-of-bounds checking. */
636 #define YYMAXUTOK 267
638 #define YYTRANSLATE(YYX) \
639 ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
641 /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
642 as returned by yylex, without out-of-bounds checking. */
643 static const yytype_uint8 yytranslate[] =
645 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
646 13, 2, 2, 2, 2, 2, 2, 2, 2, 2,
647 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
648 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
649 2, 2, 2, 2, 15, 2, 2, 2, 16, 17,
650 2, 2, 2, 2, 2, 2, 2, 2, 14, 2,
651 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
652 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
653 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
654 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
655 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
656 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
657 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
658 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
659 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
660 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
661 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
662 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
663 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
664 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
665 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
666 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
667 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
668 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
669 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
670 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
671 5, 6, 7, 8, 9, 10, 11, 12
675 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
676 static const yytype_uint16 yyrline[] =
678 0, 247, 247, 257, 258, 261, 263, 265, 267, 279,
679 282, 283, 286, 369, 372, 388, 391, 401, 408, 416,
680 420, 427, 434, 456, 459, 462, 472, 480, 488, 491,
685 #if YYDEBUG || YYERROR_VERBOSE || 0
686 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
687 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
688 static const char *const yytname[] =
690 "$end", "error", "$undefined", "kMASK", "kPREFIX", "kSUFFIX",
691 "kSYNONYM", "kID", "kNUMBER", "kPERCPERC", "kBITFIELD", "kCHAR",
692 "kSPACE", "'\\n'", "':'", "','", "'0'", "'1'", "$accept", "spec",
693 "masks", "mask", "instrs", "instr", "bitfieldopt", "bytes", "byte",
694 "bit", "optargs", "args", "arg", "argcomp", YY_NULLPTR
699 /* YYTOKNUM[NUM] -- (External) token number corresponding to the
700 (internal) symbol number NUM (which must be that of a token). */
701 static const yytype_uint16 yytoknum[] =
703 0, 256, 257, 258, 259, 260, 261, 262, 263, 264,
704 265, 266, 267, 10, 58, 44, 48, 49
708 #define YYPACT_NINF -35
710 #define yypact_value_is_default(Yystate) \
711 (!!((Yystate) == (-35)))
713 #define YYTABLE_NINF -1
715 #define yytable_value_is_error(Yytable_value) \
718 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
720 static const yytype_int8 yypact[] =
722 12, 9, 10, 11, 13, 22, -2, -35, 16, -35,
723 -35, 15, -35, 14, 12, -35, -35, -4, -35, -35,
724 -35, -35, 17, -35, -12, -4, -35, -4, 18, -4,
725 -35, -35, -35, 19, -4, 18, 20, -6, -35, -35,
726 -35, -35, -35, 21, -6, -35, -6, -35, -6
729 /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
730 Performed when YYTABLE does not specify something else to do. Zero
731 means the default is an error. */
732 static const yytype_uint8 yydefact[] =
734 9, 0, 0, 0, 0, 0, 0, 4, 0, 6,
735 7, 0, 1, 0, 9, 5, 8, 13, 3, 22,
736 20, 21, 2, 11, 0, 17, 19, 13, 15, 0,
737 18, 10, 14, 0, 16, 15, 24, 0, 12, 31,
738 29, 30, 32, 23, 26, 28, 0, 27, 25
741 /* YYPGOTO[NTERM-NUM]. */
742 static const yytype_int8 yypgoto[] =
744 -35, -35, -35, 23, -35, 2, -1, -35, 4, -25,
748 /* YYDEFGOTO[NTERM-NUM]. */
749 static const yytype_int8 yydefgoto[] =
751 -1, 5, 6, 7, 22, 23, 33, 24, 25, 26,
755 /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
756 positive, shift that token. If negative, reduce the rule whose
757 number is the opposite. If YYTABLE_NINF, syntax error. */
758 static const yytype_uint8 yytable[] =
760 30, 39, 28, 29, 40, 41, 19, 13, 42, 30,
761 47, 14, 20, 21, 47, 1, 2, 3, 4, 8,
762 9, 10, 12, 11, 15, 16, 35, 17, 32, 31,
763 27, 48, 37, 34, 36, 0, 46, 18
766 static const yytype_int8 yycheck[] =
768 25, 7, 14, 15, 10, 11, 10, 9, 14, 34,
769 44, 13, 16, 17, 48, 3, 4, 5, 6, 10,
770 10, 10, 0, 10, 8, 10, 7, 13, 10, 27,
771 13, 46, 12, 29, 35, -1, 15, 14
774 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
775 symbol of state STATE-NUM. */
776 static const yytype_uint8 yystos[] =
778 0, 3, 4, 5, 6, 19, 20, 21, 10, 10,
779 10, 10, 0, 9, 13, 8, 10, 13, 21, 10,
780 16, 17, 22, 23, 25, 26, 27, 13, 14, 15,
781 27, 23, 10, 24, 26, 7, 24, 12, 28, 7,
782 10, 11, 14, 29, 30, 31, 15, 31, 30
785 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
786 static const yytype_uint8 yyr1[] =
788 0, 18, 19, 20, 20, 21, 21, 21, 21, 21,
789 22, 22, 23, 23, 24, 24, 25, 25, 26, 26,
790 27, 27, 27, 28, 28, 29, 29, 30, 30, 31,
794 /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */
795 static const yytype_uint8 yyr2[] =
797 0, 2, 4, 3, 1, 3, 2, 2, 3, 0,
798 3, 1, 6, 0, 1, 0, 3, 1, 2, 1,
799 1, 1, 1, 2, 0, 3, 1, 2, 1, 1,
804 #define yyerrok (yyerrstatus = 0)
805 #define yyclearin (yychar = YYEMPTY)
809 #define YYACCEPT goto yyacceptlab
810 #define YYABORT goto yyabortlab
811 #define YYERROR goto yyerrorlab
814 #define YYRECOVERING() (!!yyerrstatus)
816 #define YYBACKUP(Token, Value) \
818 if (yychar == YYEMPTY) \
822 YYPOPSTACK (yylen); \
828 yyerror (YY_("syntax error: cannot back up")); \
833 /* Error token number */
835 #define YYERRCODE 256
839 /* Enable debugging if requested. */
843 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
844 # define YYFPRINTF fprintf
847 # define YYDPRINTF(Args) \
853 /* This macro is provided for backward compatibility. */
854 #ifndef YY_LOCATION_PRINT
855 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
859 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
863 YYFPRINTF (stderr, "%s ", Title); \
864 yy_symbol_print (stderr, \
866 YYFPRINTF (stderr, "\n"); \
871 /*----------------------------------------.
872 | Print this symbol's value on YYOUTPUT. |
873 `----------------------------------------*/
876 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
878 FILE *yyo = yyoutput;
883 if (yytype < YYNTOKENS)
884 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
890 /*--------------------------------.
891 | Print this symbol on YYOUTPUT. |
892 `--------------------------------*/
895 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
897 YYFPRINTF (yyoutput, "%s %s (",
898 yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]);
900 yy_symbol_value_print (yyoutput, yytype, yyvaluep);
901 YYFPRINTF (yyoutput, ")");
904 /*------------------------------------------------------------------.
905 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
907 `------------------------------------------------------------------*/
910 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
912 YYFPRINTF (stderr, "Stack now");
913 for (; yybottom <= yytop; yybottom++)
915 int yybot = *yybottom;
916 YYFPRINTF (stderr, " %d", yybot);
918 YYFPRINTF (stderr, "\n");
921 # define YY_STACK_PRINT(Bottom, Top) \
924 yy_stack_print ((Bottom), (Top)); \
928 /*------------------------------------------------.
929 | Report that the YYRULE is going to be reduced. |
930 `------------------------------------------------*/
933 yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule)
935 unsigned long int yylno = yyrline[yyrule];
936 int yynrhs = yyr2[yyrule];
938 YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
940 /* The symbols being reduced. */
941 for (yyi = 0; yyi < yynrhs; yyi++)
943 YYFPRINTF (stderr, " $%d = ", yyi + 1);
944 yy_symbol_print (stderr,
945 yystos[yyssp[yyi + 1 - yynrhs]],
946 &(yyvsp[(yyi + 1) - (yynrhs)])
948 YYFPRINTF (stderr, "\n");
952 # define YY_REDUCE_PRINT(Rule) \
955 yy_reduce_print (yyssp, yyvsp, Rule); \
958 /* Nonzero means print parse trace. It is left uninitialized so that
959 multiple parsers can coexist. */
962 # define YYDPRINTF(Args)
963 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
964 # define YY_STACK_PRINT(Bottom, Top)
965 # define YY_REDUCE_PRINT(Rule)
966 #endif /* !YYDEBUG */
969 /* YYINITDEPTH -- initial size of the parser's stacks. */
971 # define YYINITDEPTH 200
974 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
975 if the built-in stack extension method is used).
977 Do not make this value too large; the results are undefined if
978 YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
979 evaluated with infinite-precision integer arithmetic. */
982 # define YYMAXDEPTH 10000
989 # if defined __GLIBC__ && defined _STRING_H
990 # define yystrlen strlen
992 /* Return the length of YYSTR. */
994 yystrlen (const char *yystr)
997 for (yylen = 0; yystr[yylen]; yylen++)
1005 # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1006 # define yystpcpy stpcpy
1008 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1011 yystpcpy (char *yydest, const char *yysrc)
1014 const char *yys = yysrc;
1016 while ((*yyd++ = *yys++) != '\0')
1025 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1026 quotes and backslashes, so that it's suitable for yyerror. The
1027 heuristic is that double-quoting is unnecessary unless the string
1028 contains an apostrophe, a comma, or backslash (other than
1029 backslash-backslash). YYSTR is taken from yytname. If YYRES is
1030 null, do not copy; instead, return the length of what the result
1033 yytnamerr (char *yyres, const char *yystr)
1038 char const *yyp = yystr;
1045 goto do_not_strip_quotes;
1049 goto do_not_strip_quotes;
1062 do_not_strip_quotes: ;
1066 return yystrlen (yystr);
1068 return yystpcpy (yyres, yystr) - yyres;
1072 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1073 about the unexpected token YYTOKEN for the state stack whose top is
1076 Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is
1077 not large enough to hold the message. In that case, also set
1078 *YYMSG_ALLOC to the required number of bytes. Return 2 if the
1079 required number of bytes is too large to store. */
1081 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1082 yytype_int16 *yyssp, int yytoken)
1084 YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]);
1085 YYSIZE_T yysize = yysize0;
1086 enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1087 /* Internationalized format string. */
1088 const char *yyformat = YY_NULLPTR;
1089 /* Arguments of yyformat. */
1090 char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1091 /* Number of reported tokens (one for the "unexpected", one per
1095 /* There are many possibilities here to consider:
1096 - If this state is a consistent state with a default action, then
1097 the only way this function was invoked is if the default action
1098 is an error action. In that case, don't check for expected
1099 tokens because there are none.
1100 - The only way there can be no lookahead present (in yychar) is if
1101 this state is a consistent state with a default action. Thus,
1102 detecting the absence of a lookahead is sufficient to determine
1103 that there is no unexpected or expected token to report. In that
1104 case, just report a simple "syntax error".
1105 - Don't assume there isn't a lookahead just because this state is a
1106 consistent state with a default action. There might have been a
1107 previous inconsistent state, consistent state with a non-default
1108 action, or user semantic action that manipulated yychar.
1109 - Of course, the expected token list depends on states to have
1110 correct lookahead information, and it depends on the parser not
1111 to perform extra reductions after fetching a lookahead from the
1112 scanner and before detecting a syntax error. Thus, state merging
1113 (from LALR or IELR) and default reductions corrupt the expected
1114 token list. However, the list is correct for canonical LR with
1115 one exception: it will still contain any token that will not be
1116 accepted due to an error action in a later state.
1118 if (yytoken != YYEMPTY)
1120 int yyn = yypact[*yyssp];
1121 yyarg[yycount++] = yytname[yytoken];
1122 if (!yypact_value_is_default (yyn))
1124 /* Start YYX at -YYN if negative to avoid negative indexes in
1125 YYCHECK. In other words, skip the first -YYN actions for
1126 this state because they are default actions. */
1127 int yyxbegin = yyn < 0 ? -yyn : 0;
1128 /* Stay within bounds of both yycheck and yytname. */
1129 int yychecklim = YYLAST - yyn + 1;
1130 int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1133 for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1134 if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1135 && !yytable_value_is_error (yytable[yyx + yyn]))
1137 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1143 yyarg[yycount++] = yytname[yyx];
1145 YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]);
1146 if (! (yysize <= yysize1
1147 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1157 # define YYCASE_(N, S) \
1161 YYCASE_(0, YY_("syntax error"));
1162 YYCASE_(1, YY_("syntax error, unexpected %s"));
1163 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1164 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1165 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1166 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1171 YYSIZE_T yysize1 = yysize + yystrlen (yyformat);
1172 if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1177 if (*yymsg_alloc < yysize)
1179 *yymsg_alloc = 2 * yysize;
1180 if (! (yysize <= *yymsg_alloc
1181 && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1182 *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1186 /* Avoid sprintf, as that infringes on the user's name space.
1187 Don't have undefined behavior even if the translation
1188 produced a string with the wrong number of "%s"s. */
1192 while ((*yyp = *yyformat) != '\0')
1193 if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1195 yyp += yytnamerr (yyp, yyarg[yyi++]);
1206 #endif /* YYERROR_VERBOSE */
1208 /*-----------------------------------------------.
1209 | Release the memory associated to this symbol. |
1210 `-----------------------------------------------*/
1213 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1218 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1220 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1222 YY_IGNORE_MAYBE_UNINITIALIZED_END
1228 /* The lookahead symbol. */
1231 /* The semantic value of the lookahead symbol. */
1233 /* Number of syntax errors so far. */
1245 /* Number of tokens to shift before error messages enabled. */
1248 /* The stacks and their tools:
1249 'yyss': related to states.
1250 'yyvs': related to semantic values.
1252 Refer to the stacks through separate pointers, to allow yyoverflow
1253 to reallocate them elsewhere. */
1255 /* The state stack. */
1256 yytype_int16 yyssa[YYINITDEPTH];
1258 yytype_int16 *yyssp;
1260 /* The semantic value stack. */
1261 YYSTYPE yyvsa[YYINITDEPTH];
1265 YYSIZE_T yystacksize;
1269 /* Lookahead token as an internal (translated) token number. */
1271 /* The variables used to return semantic value and location from the
1276 /* Buffer for error messages, and its allocated size. */
1278 char *yymsg = yymsgbuf;
1279 YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1282 #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
1284 /* The number of symbols on the RHS of the reduced rule.
1285 Keep to zero when no symbol should be popped. */
1288 yyssp = yyss = yyssa;
1289 yyvsp = yyvs = yyvsa;
1290 yystacksize = YYINITDEPTH;
1292 YYDPRINTF ((stderr, "Starting parse\n"));
1297 yychar = YYEMPTY; /* Cause a token to be read. */
1300 /*------------------------------------------------------------.
1301 | yynewstate -- Push a new state, which is found in yystate. |
1302 `------------------------------------------------------------*/
1304 /* In all cases, when you get here, the value and location stacks
1305 have just been pushed. So pushing a state here evens the stacks. */
1311 if (yyss + yystacksize - 1 <= yyssp)
1313 /* Get the current used size of the three stacks, in elements. */
1314 YYSIZE_T yysize = yyssp - yyss + 1;
1318 /* Give user a chance to reallocate the stack. Use copies of
1319 these so that the &'s don't force the real ones into
1321 YYSTYPE *yyvs1 = yyvs;
1322 yytype_int16 *yyss1 = yyss;
1324 /* Each stack pointer address is followed by the size of the
1325 data in use in that stack, in bytes. This used to be a
1326 conditional around just the two extra args, but that might
1327 be undefined if yyoverflow is a macro. */
1328 yyoverflow (YY_("memory exhausted"),
1329 &yyss1, yysize * sizeof (*yyssp),
1330 &yyvs1, yysize * sizeof (*yyvsp),
1336 #else /* no yyoverflow */
1337 # ifndef YYSTACK_RELOCATE
1338 goto yyexhaustedlab;
1340 /* Extend the stack our own way. */
1341 if (YYMAXDEPTH <= yystacksize)
1342 goto yyexhaustedlab;
1344 if (YYMAXDEPTH < yystacksize)
1345 yystacksize = YYMAXDEPTH;
1348 yytype_int16 *yyss1 = yyss;
1349 union yyalloc *yyptr =
1350 (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1352 goto yyexhaustedlab;
1353 YYSTACK_RELOCATE (yyss_alloc, yyss);
1354 YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1355 # undef YYSTACK_RELOCATE
1357 YYSTACK_FREE (yyss1);
1360 #endif /* no yyoverflow */
1362 yyssp = yyss + yysize - 1;
1363 yyvsp = yyvs + yysize - 1;
1365 YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1366 (unsigned long int) yystacksize));
1368 if (yyss + yystacksize - 1 <= yyssp)
1372 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1374 if (yystate == YYFINAL)
1384 /* Do appropriate processing given the current state. Read a
1385 lookahead token if we need one and don't already have one. */
1387 /* First try to decide what to do without reference to lookahead token. */
1388 yyn = yypact[yystate];
1389 if (yypact_value_is_default (yyn))
1392 /* Not known => get a lookahead token if don't already have one. */
1394 /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */
1395 if (yychar == YYEMPTY)
1397 YYDPRINTF ((stderr, "Reading a token: "));
1401 if (yychar <= YYEOF)
1403 yychar = yytoken = YYEOF;
1404 YYDPRINTF ((stderr, "Now at end of input.\n"));
1408 yytoken = YYTRANSLATE (yychar);
1409 YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1412 /* If the proper action on seeing token YYTOKEN is to reduce or to
1413 detect an error, take that action. */
1415 if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1420 if (yytable_value_is_error (yyn))
1426 /* Count tokens shifted since error; after three, turn off error
1431 /* Shift the lookahead token. */
1432 YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1434 /* Discard the shifted token. */
1438 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1440 YY_IGNORE_MAYBE_UNINITIALIZED_END
1445 /*-----------------------------------------------------------.
1446 | yydefault -- do the default action for the current state. |
1447 `-----------------------------------------------------------*/
1449 yyn = yydefact[yystate];
1455 /*-----------------------------.
1456 | yyreduce -- Do a reduction. |
1457 `-----------------------------*/
1459 /* yyn is the number of a rule to reduce with. */
1462 /* If YYLEN is nonzero, implement the default value of the action:
1465 Otherwise, the following line sets YYVAL to garbage.
1466 This behavior is undocumented and Bison
1467 users should not rely upon it. Assigning to YYVAL
1468 unconditionally makes the parser a bit smaller, and it avoids a
1469 GCC warning that YYVAL may be used uninitialized. */
1470 yyval = yyvsp[1-yylen];
1473 YY_REDUCE_PRINT (yyn);
1477 #line 248 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1479 if (error_message_count != 0)
1480 error (EXIT_FAILURE, 0,
1481 "terminated due to previous error");
1485 #line 1486 "i386_parse.c" /* yacc.c:1646 */
1489 #line 262 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1490 { new_bitfield ((yyvsp[-1].str), (yyvsp[0].num)); }
1491 #line 1492 "i386_parse.c" /* yacc.c:1646 */
1495 #line 264 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1496 { new_bitfield ((yyvsp[0].str), -1); }
1497 #line 1498 "i386_parse.c" /* yacc.c:1646 */
1501 #line 266 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1502 { new_bitfield ((yyvsp[0].str), -2); }
1503 #line 1504 "i386_parse.c" /* yacc.c:1646 */
1507 #line 268 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1509 struct synonym *newp = xmalloc (sizeof (*newp));
1510 newp->from = (yyvsp[-1].str);
1511 newp->to = (yyvsp[0].str);
1512 if (tfind (newp, &synonyms, compare_syn) != NULL)
1514 "%d: duplicate definition for synonym '%s'",
1515 i386_lineno, (yyvsp[-1].str));
1516 else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
1517 error (EXIT_FAILURE, 0, "tsearch");
1519 #line 1520 "i386_parse.c" /* yacc.c:1646 */
1523 #line 287 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1525 if ((yyvsp[-3].field) != NULL && strcmp ((yyvsp[-3].field)->name, "RE") != 0
1526 && strcmp ((yyvsp[-3].field)->name, "R") != 0)
1528 error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
1531 if (check_duplicates ((yyvsp[-5].bit)) == 0
1532 && check_argsdef ((yyvsp[-5].bit), (yyvsp[0].arg)) == 0
1533 && check_bitsused ((yyvsp[-5].bit), (yyvsp[-1].field), (yyvsp[0].arg)) == 0)
1535 struct instruction *newp = xcalloc (sizeof (*newp),
1537 if ((yyvsp[-3].field) != NULL)
1539 if (strcmp ((yyvsp[-3].field)->name, "RE") == 0)
1541 else if (strcmp ((yyvsp[-3].field)->name, "R") == 0)
1545 newp->bytes = (yyvsp[-5].bit);
1546 newp->mnemonic = (yyvsp[-2].str);
1547 if (newp->mnemonic != (void *) -1l
1548 && tfind ((yyvsp[-2].str), &mnemonics,
1549 (comparison_fn_t) strcmp) == NULL)
1551 if (tsearch ((yyvsp[-2].str), &mnemonics,
1552 (comparison_fn_t) strcmp) == NULL)
1553 error (EXIT_FAILURE, errno, "tsearch");
1557 if ((yyvsp[-1].field) != NULL)
1559 if (strcmp ((yyvsp[-1].field)->name, "w") == 0)
1560 newp->suffix = suffix_w;
1561 else if (strcmp ((yyvsp[-1].field)->name, "w0") == 0)
1562 newp->suffix = suffix_w0;
1563 else if (strcmp ((yyvsp[-1].field)->name, "tttn") == 0)
1564 newp->suffix = suffix_tttn;
1565 else if (strcmp ((yyvsp[-1].field)->name, "w1") == 0)
1566 newp->suffix = suffix_w1;
1567 else if (strcmp ((yyvsp[-1].field)->name, "W") == 0)
1568 newp->suffix = suffix_W;
1569 else if (strcmp ((yyvsp[-1].field)->name, "W1") == 0)
1570 newp->suffix = suffix_W1;
1571 else if (strcmp ((yyvsp[-1].field)->name, "D") == 0)
1572 newp->suffix = suffix_D;
1574 error (EXIT_FAILURE, 0,
1575 "%s: %d: unknown suffix '%s'",
1576 infname, i386_lineno - 1, (yyvsp[-1].field)->name);
1578 struct suffix search = { .name = (yyvsp[-1].field)->name };
1579 if (tfind (&search, &suffixes, compare_suf)
1582 struct suffix *ns = xmalloc (sizeof (*ns));
1583 ns->name = (yyvsp[-1].field)->name;
1584 ns->idx = ++nsuffixes;
1585 if (tsearch (ns, &suffixes, compare_suf)
1587 error (EXIT_FAILURE, errno, "tsearch");
1591 struct argument *args = (yyvsp[0].arg);
1593 while (args != NULL)
1595 fillin_arg ((yyvsp[-5].bit), args->name, newp, n);
1601 newp->next = instructions;
1602 instructions = newp;
1606 #line 1607 "i386_parse.c" /* yacc.c:1646 */
1610 #line 373 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1612 struct known_bitfield search;
1613 search.name = (yyvsp[0].str);
1614 struct known_bitfield **res;
1615 res = tfind (&search, &bitfields, bitfield_compare);
1618 error (0, 0, "%d: unknown bitfield '%s'",
1619 i386_lineno, search.name);
1620 (yyval.field) = NULL;
1623 (yyval.field) = *res;
1625 #line 1626 "i386_parse.c" /* yacc.c:1646 */
1629 #line 388 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1630 { (yyval.field) = NULL; }
1631 #line 1632 "i386_parse.c" /* yacc.c:1646 */
1635 #line 392 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1637 check_bits ((yyvsp[0].bit));
1639 struct bitvalue *runp = (yyvsp[-2].bit);
1640 while (runp->next != NULL)
1642 runp->next = (yyvsp[0].bit);
1643 (yyval.bit) = (yyvsp[-2].bit);
1645 #line 1646 "i386_parse.c" /* yacc.c:1646 */
1649 #line 402 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1651 check_bits ((yyvsp[0].bit));
1652 (yyval.bit) = (yyvsp[0].bit);
1654 #line 1655 "i386_parse.c" /* yacc.c:1646 */
1658 #line 409 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1660 struct bitvalue *runp = (yyvsp[-1].bit);
1661 while (runp->next != NULL)
1663 runp->next = (yyvsp[0].bit);
1664 (yyval.bit) = (yyvsp[-1].bit);
1666 #line 1667 "i386_parse.c" /* yacc.c:1646 */
1670 #line 417 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1671 { (yyval.bit) = (yyvsp[0].bit); }
1672 #line 1673 "i386_parse.c" /* yacc.c:1646 */
1676 #line 421 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1678 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1679 (yyval.bit)->type = zeroone;
1680 (yyval.bit)->value = 0;
1681 (yyval.bit)->next = NULL;
1683 #line 1684 "i386_parse.c" /* yacc.c:1646 */
1687 #line 428 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1689 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1690 (yyval.bit)->type = zeroone;
1691 (yyval.bit)->value = 1;
1692 (yyval.bit)->next = NULL;
1694 #line 1695 "i386_parse.c" /* yacc.c:1646 */
1698 #line 435 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1700 (yyval.bit) = xmalloc (sizeof (struct bitvalue));
1701 struct known_bitfield search;
1702 search.name = (yyvsp[0].str);
1703 struct known_bitfield **res;
1704 res = tfind (&search, &bitfields, bitfield_compare);
1707 error (0, 0, "%d: unknown bitfield '%s'",
1708 i386_lineno, search.name);
1709 (yyval.bit)->type = failure;
1713 (yyval.bit)->type = field;
1714 (yyval.bit)->field = *res;
1716 (yyval.bit)->next = NULL;
1718 #line 1719 "i386_parse.c" /* yacc.c:1646 */
1722 #line 457 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1723 { (yyval.arg) = (yyvsp[0].arg); }
1724 #line 1725 "i386_parse.c" /* yacc.c:1646 */
1728 #line 459 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1729 { (yyval.arg) = NULL; }
1730 #line 1731 "i386_parse.c" /* yacc.c:1646 */
1734 #line 463 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1736 struct argument *runp = (yyvsp[-2].arg);
1737 while (runp->next != NULL)
1739 runp->next = xmalloc (sizeof (struct argument));
1740 runp->next->name = combine ((yyvsp[0].name));
1741 runp->next->next = NULL;
1742 (yyval.arg) = (yyvsp[-2].arg);
1744 #line 1745 "i386_parse.c" /* yacc.c:1646 */
1748 #line 473 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1750 (yyval.arg) = xmalloc (sizeof (struct argument));
1751 (yyval.arg)->name = combine ((yyvsp[0].name));
1752 (yyval.arg)->next = NULL;
1754 #line 1755 "i386_parse.c" /* yacc.c:1646 */
1758 #line 481 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1760 struct argname *runp = (yyvsp[-1].name);
1761 while (runp->next != NULL)
1763 runp->next = (yyvsp[0].name);
1764 (yyval.name) = (yyvsp[-1].name);
1766 #line 1767 "i386_parse.c" /* yacc.c:1646 */
1770 #line 489 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1771 { (yyval.name) = (yyvsp[0].name); }
1772 #line 1773 "i386_parse.c" /* yacc.c:1646 */
1776 #line 492 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1778 (yyval.name) = xmalloc (sizeof (struct argname));
1779 (yyval.name)->type = nfield;
1780 (yyval.name)->next = NULL;
1782 struct known_bitfield search;
1783 search.name = (yyvsp[0].str);
1784 struct known_bitfield **res;
1785 res = tfind (&search, &bitfields, bitfield_compare);
1788 if (strcmp ((yyvsp[0].str), "ax") == 0)
1789 (yyval.name)->field = &ax_reg;
1790 else if (strcmp ((yyvsp[0].str), "dx") == 0)
1791 (yyval.name)->field = &dx_reg;
1792 else if (strcmp ((yyvsp[0].str), "es_di") == 0)
1793 (yyval.name)->field = &di_reg;
1794 else if (strcmp ((yyvsp[0].str), "ds_si") == 0)
1795 (yyval.name)->field = &si_reg;
1796 else if (strcmp ((yyvsp[0].str), "ds_bx") == 0)
1797 (yyval.name)->field = &bx_reg;
1800 error (0, 0, "%d: unknown bitfield '%s'",
1801 i386_lineno, search.name);
1802 (yyval.name)->field = NULL;
1806 (yyval.name)->field = *res;
1808 #line 1809 "i386_parse.c" /* yacc.c:1646 */
1812 #line 524 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1814 (yyval.name) = xmalloc (sizeof (struct argname));
1815 (yyval.name)->type = string;
1816 (yyval.name)->next = NULL;
1817 (yyval.name)->str = xmalloc (2);
1818 (yyval.name)->str[0] = (yyvsp[0].ch);
1819 (yyval.name)->str[1] = '\0';
1821 #line 1822 "i386_parse.c" /* yacc.c:1646 */
1825 #line 533 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1827 (yyval.name) = xmalloc (sizeof (struct argname));
1828 (yyval.name)->type = string;
1829 (yyval.name)->next = NULL;
1830 (yyval.name)->str = (yyvsp[0].str);
1832 #line 1833 "i386_parse.c" /* yacc.c:1646 */
1836 #line 540 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1646 */
1838 (yyval.name) = xmalloc (sizeof (struct argname));
1839 (yyval.name)->type = string;
1840 (yyval.name)->next = NULL;
1841 (yyval.name)->str = xmalloc (2);
1842 (yyval.name)->str[0] = ':';
1843 (yyval.name)->str[1] = '\0';
1845 #line 1846 "i386_parse.c" /* yacc.c:1646 */
1849 #line 1850 "i386_parse.c" /* yacc.c:1646 */
1852 /* User semantic actions sometimes alter yychar, and that requires
1853 that yytoken be updated with the new translation. We take the
1854 approach of translating immediately before every use of yytoken.
1855 One alternative is translating here after every semantic action,
1856 but that translation would be missed if the semantic action invokes
1857 YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
1858 if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an
1859 incorrect destructor might then be invoked immediately. In the
1860 case of YYERROR or YYBACKUP, subsequent parser actions might lead
1861 to an incorrect destructor call or verbose syntax error message
1862 before the lookahead is translated. */
1863 YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
1867 YY_STACK_PRINT (yyss, yyssp);
1871 /* Now 'shift' the result of the reduction. Determine what state
1872 that goes to, based on the state we popped back to and the rule
1873 number reduced by. */
1877 yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
1878 if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
1879 yystate = yytable[yystate];
1881 yystate = yydefgoto[yyn - YYNTOKENS];
1886 /*--------------------------------------.
1887 | yyerrlab -- here on detecting error. |
1888 `--------------------------------------*/
1890 /* Make sure we have latest lookahead translation. See comments at
1891 user semantic actions for why this is necessary. */
1892 yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
1894 /* If not already recovering from an error, report this error. */
1898 #if ! YYERROR_VERBOSE
1899 yyerror (YY_("syntax error"));
1901 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
1904 char const *yymsgp = YY_("syntax error");
1905 int yysyntax_error_status;
1906 yysyntax_error_status = YYSYNTAX_ERROR;
1907 if (yysyntax_error_status == 0)
1909 else if (yysyntax_error_status == 1)
1911 if (yymsg != yymsgbuf)
1912 YYSTACK_FREE (yymsg);
1913 yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
1917 yymsg_alloc = sizeof yymsgbuf;
1918 yysyntax_error_status = 2;
1922 yysyntax_error_status = YYSYNTAX_ERROR;
1927 if (yysyntax_error_status == 2)
1928 goto yyexhaustedlab;
1930 # undef YYSYNTAX_ERROR
1936 if (yyerrstatus == 3)
1938 /* If just tried and failed to reuse lookahead token after an
1939 error, discard it. */
1941 if (yychar <= YYEOF)
1943 /* Return failure if at end of input. */
1944 if (yychar == YYEOF)
1949 yydestruct ("Error: discarding",
1955 /* Else will try to reuse lookahead token after shifting the error
1960 /*---------------------------------------------------.
1961 | yyerrorlab -- error raised explicitly by YYERROR. |
1962 `---------------------------------------------------*/
1965 /* Pacify compilers like GCC when the user code never invokes
1966 YYERROR and the label yyerrorlab therefore never appears in user
1968 if (/*CONSTCOND*/ 0)
1971 /* Do not reclaim the symbols of the rule whose action triggered
1975 YY_STACK_PRINT (yyss, yyssp);
1980 /*-------------------------------------------------------------.
1981 | yyerrlab1 -- common code for both syntax error and YYERROR. |
1982 `-------------------------------------------------------------*/
1984 yyerrstatus = 3; /* Each real token shifted decrements this. */
1988 yyn = yypact[yystate];
1989 if (!yypact_value_is_default (yyn))
1992 if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
2000 /* Pop the current state because it cannot handle the error token. */
2005 yydestruct ("Error: popping",
2006 yystos[yystate], yyvsp);
2009 YY_STACK_PRINT (yyss, yyssp);
2012 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
2014 YY_IGNORE_MAYBE_UNINITIALIZED_END
2017 /* Shift the error token. */
2018 YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
2024 /*-------------------------------------.
2025 | yyacceptlab -- YYACCEPT comes here. |
2026 `-------------------------------------*/
2031 /*-----------------------------------.
2032 | yyabortlab -- YYABORT comes here. |
2033 `-----------------------------------*/
2038 #if !defined yyoverflow || YYERROR_VERBOSE
2039 /*-------------------------------------------------.
2040 | yyexhaustedlab -- memory exhaustion comes here. |
2041 `-------------------------------------------------*/
2043 yyerror (YY_("memory exhausted"));
2049 if (yychar != YYEMPTY)
2051 /* Make sure we have latest lookahead translation. See comments at
2052 user semantic actions for why this is necessary. */
2053 yytoken = YYTRANSLATE (yychar);
2054 yydestruct ("Cleanup: discarding lookahead",
2057 /* Do not reclaim the symbols of the rule whose action triggered
2058 this YYABORT or YYACCEPT. */
2060 YY_STACK_PRINT (yyss, yyssp);
2061 while (yyssp != yyss)
2063 yydestruct ("Cleanup: popping",
2064 yystos[*yyssp], yyvsp);
2069 YYSTACK_FREE (yyss);
2072 if (yymsg != yymsgbuf)
2073 YYSTACK_FREE (yymsg);
2077 #line 550 "/home/mark/src/elfutils/libcpu/i386_parse.y" /* yacc.c:1906 */
2081 yyerror (const char *s)
2083 error (0, 0, gettext ("while reading i386 CPU description: %s at line %d"),
2084 gettext (s), i386_lineno);
2089 bitfield_compare (const void *p1, const void *p2)
2091 struct known_bitfield *f1 = (struct known_bitfield *) p1;
2092 struct known_bitfield *f2 = (struct known_bitfield *) p2;
2094 return strcmp (f1->name, f2->name);
2099 new_bitfield (char *name, unsigned long int num)
2101 struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
2106 if (tfind (newp, &bitfields, bitfield_compare) != NULL)
2108 error (0, 0, "%d: duplicated definition of bitfield '%s'",
2114 if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
2115 error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
2120 /* Check that the number of bits is a multiple of 8. */
2122 check_bits (struct bitvalue *val)
2124 struct bitvalue *runp = val;
2125 unsigned int total = 0;
2127 while (runp != NULL)
2129 if (runp->type == zeroone)
2131 else if (runp->field == NULL)
2132 /* No sense doing anything, the field is not known. */
2135 total += runp->field->bits;
2147 if (val->type == zeroone)
2148 obstack_printf (&os, "%u", val->value);
2150 obstack_printf (&os, "{%s}", val->field->name);
2153 obstack_1grow (&os, '\0');
2155 error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
2156 i386_lineno, (char *) obstack_finish (&os));
2158 obstack_free (&os, NULL);
2164 check_duplicates (struct bitvalue *val)
2172 if (val->type == field && val->field != NULL)
2174 if (val->field->tmp == testcnt)
2176 error (0, 0, "%d: bitfield '%s' used more than once",
2177 i386_lineno - 1, val->field->name);
2180 val->field->tmp = testcnt;
2191 check_argsdef (struct bitvalue *bitval, struct argument *args)
2195 while (args != NULL)
2197 for (struct argname *name = args->name; name != NULL; name = name->next)
2198 if (name->type == nfield && name->field != NULL
2199 && name->field != &ax_reg && name->field != &dx_reg
2200 && name->field != &di_reg && name->field != &si_reg
2201 && name->field != &bx_reg)
2203 struct bitvalue *runp = bitval;
2205 while (runp != NULL)
2206 if (runp->type == field && runp->field == name->field)
2213 error (0, 0, "%d: unknown bitfield '%s' used in output format",
2214 i386_lineno - 1, name->field->name);
2227 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
2228 struct argument *args)
2232 while (bitval != NULL)
2234 if (bitval->type == field && bitval->field != NULL
2235 && bitval->field != suffix
2236 /* {w} is handled special. */
2237 && strcmp (bitval->field->name, "w") != 0)
2239 struct argument *runp;
2240 for (runp = args; runp != NULL; runp = runp->next)
2242 struct argname *name = runp->name;
2244 while (name != NULL)
2245 if (name->type == nfield && name->field == bitval->field)
2257 error (0, 0, "%d: bitfield '%s' not used",
2258 i386_lineno - 1, bitval->field->name);
2264 bitval = bitval->next;
2271 static struct argname *
2272 combine (struct argname *name)
2274 struct argname *last_str = NULL;
2275 for (struct argname *runp = name; runp != NULL; runp = runp->next)
2277 if (runp->type == string)
2279 if (last_str == NULL)
2283 last_str->str = xrealloc (last_str->str,
2284 strlen (last_str->str)
2285 + strlen (runp->str) + 1);
2286 strcat (last_str->str, runp->str);
2287 last_str->next = runp->next;
2297 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
2301 fillin_arg (struct bitvalue *bytes, struct argname *name,
2302 struct instruction *instr, int n)
2304 static struct obstack ob;
2305 static int initialized;
2312 struct argname *runp = name;
2314 while (runp != NULL)
2316 /* We ignore strings in the function name. */
2317 if (runp->type == string)
2319 if (instr->operands[n].str != NULL)
2320 error (EXIT_FAILURE, 0,
2321 "%d: cannot have more than one string parameter",
2324 instr->operands[n].str = runp->str;
2328 assert (runp->type == nfield);
2330 /* Construct the function name. */
2332 obstack_1grow (&ob, '$');
2334 if (runp->field == NULL)
2335 /* Add some string which contains invalid characters. */
2336 obstack_grow_str (&ob, "!!!INVALID!!!");
2339 char *fieldname = runp->field->name;
2341 struct synonym search = { .from = fieldname };
2343 struct synonym **res = tfind (&search, &synonyms, compare_syn);
2345 fieldname = (*res)->to;
2347 obstack_grow_str (&ob, fieldname);
2350 /* Now compute the bit offset of the field. */
2351 struct bitvalue *b = bytes;
2353 if (runp->field != NULL)
2356 if (b->type == field && b->field != NULL)
2358 if (strcmp (b->field->name, runp->field->name) == 0)
2360 bitoff += b->field->bits;
2367 if (instr->operands[n].off1 == 0)
2368 instr->operands[n].off1 = bitoff;
2369 else if (instr->operands[n].off2 == 0)
2370 instr->operands[n].off2 = bitoff;
2371 else if (instr->operands[n].off3 == 0)
2372 instr->operands[n].off3 = bitoff;
2374 error (EXIT_FAILURE, 0,
2375 "%d: cannot have more than three fields in parameter",
2378 if (runp->field != NULL
2379 && strncasecmp (runp->field->name, "mod", 3) == 0)
2385 if (obstack_object_size (&ob) == 0)
2386 obstack_grow_str (&ob, "string");
2387 obstack_1grow (&ob, '\0');
2388 char *fct = obstack_finish (&ob);
2390 instr->operands[n].fct = fct;
2396 nameout (const void *nodep, VISIT value, int level)
2398 if (value == leaf || value == postorder)
2399 printf (" %s\n", *(const char **) nodep);
2405 compare_argstring (const void *p1, const void *p2)
2407 const struct argstring *a1 = (const struct argstring *) p1;
2408 const struct argstring *a2 = (const struct argstring *) p2;
2410 return strcmp (a1->str, a2->str);
2414 static int maxoff[3][3];
2415 static int minoff[3][3] = { { 1000, 1000, 1000 },
2416 { 1000, 1000, 1000 },
2417 { 1000, 1000, 1000 } };
2418 static int nbitoff[3][3];
2419 static void *fct_names[3];
2420 static int nbitfct[3];
2422 static void *strs[3];
2423 static int nbitstr[3];
2424 static int total_bits = 2; // Already counted the rep/repe bits.
2429 int nfct_names[3] = { 0, 0, 0 };
2430 int nstrs[3] = { 0, 0, 0 };
2432 /* We reverse the order of the instruction list while processing it.
2433 Later phases need it in the order in which the input file has
2435 struct instruction *reversed = NULL;
2437 struct instruction *runp = instructions;
2438 while (runp != NULL)
2440 for (int i = 0; i < 3; ++i)
2441 if (runp->operands[i].fct != NULL)
2443 struct argstring search = { .str = runp->operands[i].fct };
2444 if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
2446 struct argstring *newp = xmalloc (sizeof (*newp));
2447 newp->str = runp->operands[i].fct;
2449 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
2450 error (EXIT_FAILURE, errno, "tsearch");
2454 if (runp->operands[i].str != NULL)
2456 search.str = runp->operands[i].str;
2457 if (tfind (&search, &strs[i], compare_argstring) == NULL)
2459 struct argstring *newp = xmalloc (sizeof (*newp));
2460 newp->str = runp->operands[i].str;
2462 if (tsearch (newp, &strs[i], compare_argstring) == NULL)
2463 error (EXIT_FAILURE, errno, "tsearch");
2468 maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
2469 maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
2470 maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
2472 if (runp->operands[i].off1 > 0)
2473 minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
2474 if (runp->operands[i].off2 > 0)
2475 minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
2476 if (runp->operands[i].off3 > 0)
2477 minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
2480 struct instruction *old = runp;
2483 old->next = reversed;
2486 instructions = reversed;
2490 for (int i = 0; i < 3; ++i)
2492 // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
2493 // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
2495 if (minoff[i][0] == 1000)
2500 d = maxoff[i][0] - minoff[i][0];
2507 total_bits += nbitoff[i][0];
2510 if (minoff[i][1] == 1000)
2515 d = maxoff[i][1] - minoff[i][1];
2522 total_bits += nbitoff[i][1];
2525 if (minoff[i][2] == 1000)
2530 d = maxoff[i][2] - minoff[i][2];
2537 total_bits += nbitoff[i][2];
2539 // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
2549 total_bits += nbitfct[i];
2550 // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
2562 total_bits += nbitstr[i];
2565 // twalk (fct_names[i], nameout);
2576 total_bits += nbitsuf;
2577 // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
2582 compare_syn (const void *p1, const void *p2)
2584 const struct synonym *s1 = (const struct synonym *) p1;
2585 const struct synonym *s2 = (const struct synonym *) p2;
2587 return strcmp (s1->from, s2->from);
2592 compare_suf (const void *p1, const void *p2)
2594 const struct suffix *s1 = (const struct suffix *) p1;
2595 const struct suffix *s2 = (const struct suffix *) p2;
2597 return strcmp (s1->name, s2->name);
2601 static int count_op_str;
2602 static int off_op_str;
2604 print_op_str (const void *nodep, VISIT value,
2605 int level __attribute__ ((unused)))
2607 if (value == leaf || value == postorder)
2609 const char *str = (*(struct argstring **) nodep)->str;
2610 fprintf (outfile, "%s\n \"%s",
2611 count_op_str == 0 ? "" : "\\0\"", str);
2612 (*(struct argstring **) nodep)->idx = ++count_op_str;
2613 (*(struct argstring **) nodep)->off = off_op_str;
2614 off_op_str += strlen (str) + 1;
2620 print_op_str_idx (const void *nodep, VISIT value,
2621 int level __attribute__ ((unused)))
2623 if (value == leaf || value == postorder)
2624 printf (" %d,\n", (*(struct argstring **) nodep)->off);
2629 print_op_fct (const void *nodep, VISIT value,
2630 int level __attribute__ ((unused)))
2632 if (value == leaf || value == postorder)
2634 fprintf (outfile, " FCT_%s,\n", (*(struct argstring **) nodep)->str);
2635 (*(struct argstring **) nodep)->idx = ++count_op_str;
2641 # error "bogus NMNES value"
2645 instrtable_out (void)
2650 create_mnemonic_table ();
2652 fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
2654 fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
2655 lrint (ceil (log2 (NMNES))));
2657 fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
2658 for (int i = 0; i < 3; ++i)
2660 fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
2661 if (nbitstr[i] != 0)
2662 fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
2663 fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
2664 fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
2665 if (nbitoff[i][1] != 0)
2667 fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
2668 fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
2670 if (nbitoff[i][2] != 0)
2672 fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
2673 fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
2677 fputs ("\n#include <i386_data.h>\n\n", outfile);
2680 #define APPEND(a, b) APPEND_ (a, b)
2681 #define APPEND_(a, b) a##b
2682 #define EMIT_SUFFIX(suf) \
2683 fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
2693 fputc_unlocked ('\n', outfile);
2695 for (int i = 0; i < 3; ++i)
2699 fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n NULL,\n",
2701 twalk (fct_names[i], print_op_fct);
2702 fputs ("};\n", outfile);
2704 /* The operand strings. */
2705 if (nbitstr[i] != 0)
2709 fprintf (outfile, "static const char op%d_str[] =", i + 1);
2710 twalk (strs[i], print_op_str);
2711 fputs ("\";\n", outfile);
2713 fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
2715 twalk (strs[i], print_op_str_idx);
2716 fputs ("};\n", outfile);
2721 fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
2722 struct instruction *instr;
2723 for (instr = instructions; instr != NULL; instr = instr->next)
2725 fputs (" {", outfile);
2726 if (instr->mnemonic == (void *) -1l)
2727 fputs (" .mnemonic = MNE_INVALID,", outfile);
2729 fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
2730 fprintf (outfile, " .rep = %d,", instr->rep);
2731 fprintf (outfile, " .repe = %d,", instr->repe);
2732 fprintf (outfile, " .suffix = %d,", instr->suffix);
2733 fprintf (outfile, " .modrm = %d,", instr->modrm);
2735 for (int i = 0; i < 3; ++i)
2738 if (instr->operands[i].fct != NULL)
2740 struct argstring search = { .str = instr->operands[i].fct };
2741 struct argstring **res = tfind (&search, &fct_names[i],
2743 assert (res != NULL);
2746 fprintf (outfile, " .fct%d = %d,", i + 1, idx);
2749 if (instr->operands[i].str != NULL)
2751 struct argstring search = { .str = instr->operands[i].str };
2752 struct argstring **res = tfind (&search, &strs[i],
2754 assert (res != NULL);
2757 if (nbitstr[i] != 0)
2758 fprintf (outfile, " .str%d = %d,", i + 1, idx);
2760 fprintf (outfile, " .off%d_1 = %d,", i + 1,
2761 MAX (0, instr->operands[i].off1 - minoff[i][0]));
2763 if (nbitoff[i][1] != 0)
2764 fprintf (outfile, " .off%d_2 = %d,", i + 1,
2765 MAX (0, instr->operands[i].off2 - minoff[i][1]));
2767 if (nbitoff[i][2] != 0)
2768 fprintf (outfile, " .off%d_3 = %d,", i + 1,
2769 MAX (0, instr->operands[i].off3 - minoff[i][2]));
2772 fputs (" },\n", outfile);
2774 fputs ("};\n", outfile);
2776 fputs ("static const uint8_t match_data[] =\n{\n", outfile);
2778 for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
2780 /* First count the number of bytes. */
2781 size_t totalbits = 0;
2782 size_t zerobits = 0;
2783 bool leading_p = true;
2784 size_t leadingbits = 0;
2785 struct bitvalue *b = instr->bytes;
2788 if (b->type == zeroone)
2797 totalbits += b->field->bits;
2798 /* We must always count the mod/rm byte. */
2799 if (strncasecmp (b->field->name, "mod", 3) == 0)
2802 zerobits += b->field->bits;
2807 size_t nbytes = (totalbits - zerobits + 7) / 8;
2808 assert (nbytes > 0);
2809 size_t leadingbytes = leadingbits / 8;
2811 fprintf (outfile, " %#zx,", nbytes | (leadingbytes << 4));
2813 /* Now create the mask and byte values. */
2820 if (b->type == zeroone)
2822 byte = (byte << 1) | b->value;
2823 mask = (mask << 1) | 1;
2826 if (leadingbytes > 0)
2828 assert (mask == 0xff);
2829 fprintf (outfile, " %#" PRIx8 ",", byte);
2833 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
2835 byte = mask = nbits = 0;
2842 assert (leadingbytes == 0);
2844 unsigned long int remaining = b->field->bits;
2845 while (nbits + remaining > 8)
2847 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
2848 mask << (8 - nbits), byte << (8 - nbits));
2849 remaining = nbits + remaining - 8;
2850 byte = mask = nbits = 0;
2859 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
2860 byte = mask = nbits = 0;
2868 fputc_unlocked ('\n', outfile);
2870 fputs ("};\n", outfile);
2875 static size_t mnemonic_maxlen;
2876 static size_t mnemonic_minlen;
2878 which_chars (const char *str[], size_t nstr)
2880 char used_char[256];
2881 memset (used_char, '\0', sizeof (used_char));
2882 mnemonic_maxlen = 0;
2883 mnemonic_minlen = 10000;
2884 for (size_t cnt = 0; cnt < nstr; ++cnt)
2886 const unsigned char *cp = (const unsigned char *) str[cnt];
2887 mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
2888 mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
2890 used_char[*cp++] = 1;
2891 while (*cp != '\0');
2893 size_t nused_char = 0;
2894 for (size_t cnt = 0; cnt < 256; ++cnt)
2895 if (used_char[cnt] != 0)
2901 static const char **mnemonic_strs;
2902 static size_t nmnemonic_strs;
2904 add_mnemonics (const void *nodep, VISIT value,
2905 int level __attribute__ ((unused)))
2907 if (value == leaf || value == postorder)
2908 mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
2917 static struct charfreq pfxfreq[256];
2918 static struct charfreq sfxfreq[256];
2922 compare_freq (const void *p1, const void *p2)
2924 const struct charfreq *c1 = (const struct charfreq *) p1;
2925 const struct charfreq *c2 = (const struct charfreq *) p2;
2927 if (c1->freq > c2->freq)
2929 if (c1->freq < c2->freq)
2936 compute_pfxfreq (const char *str[], size_t nstr)
2938 memset (pfxfreq, '\0', sizeof (pfxfreq));
2940 for (size_t i = 0; i < nstr; ++i)
2943 for (size_t i = 0; i < nstr; ++i)
2944 ++pfxfreq[*((const unsigned char *) str[i])].freq;
2946 qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
2949 while (n < 256 && pfxfreq[n].freq != 0)
2962 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
2964 memset (sfxfreq, '\0', sizeof (sfxfreq));
2966 for (size_t i = 0; i < nstr; ++i)
2969 for (size_t i = 0; i < nstr; ++i)
2970 ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
2972 qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
2975 while (n < 256 && sfxfreq[n].freq != 0)
2982 create_mnemonic_table (void)
2984 mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
2986 twalk (mnemonics, add_mnemonics);
2988 (void) which_chars (mnemonic_strs, nmnemonic_strs);
2990 size_t best_so_far = 100000000;
2991 char *best_prefix = NULL;
2992 char *best_suffix = NULL;
2993 char *best_table = NULL;
2994 size_t best_table_size = 0;
2995 size_t best_table_bits = 0;
2996 size_t best_prefix_bits = 0;
2998 /* We can precompute the prefix characters. */
2999 size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
3001 /* Compute best size for string representation including explicit NUL. */
3002 for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
3004 char prefix[1 << pfxbits];
3006 for (i = 0; i < (1u << pfxbits) - 1; ++i)
3007 prefix[i] = pfxfreq[i].ch;
3010 struct strsnlen strsnlen[nmnemonic_strs];
3012 for (i = 0; i < nmnemonic_strs; ++i)
3014 if (strchr (prefix, *mnemonic_strs[i]) != NULL)
3015 strsnlen[i].str = mnemonic_strs[i] + 1;
3017 strsnlen[i].str = mnemonic_strs[i];
3018 strsnlen[i].len = strlen (strsnlen[i].str);
3021 /* With the prefixes gone, try to combine strings. */
3022 size_t nstrsnlen = 1;
3023 for (i = 1; i < nmnemonic_strs; ++i)
3026 for (j = 0; j < nstrsnlen; ++j)
3027 if (strsnlen[i].len > strsnlen[j].len
3028 && strcmp (strsnlen[j].str,
3029 strsnlen[i].str + (strsnlen[i].len
3030 - strsnlen[j].len)) == 0)
3032 strsnlen[j] = strsnlen[i];
3035 else if (strsnlen[i].len < strsnlen[j].len
3036 && strcmp (strsnlen[i].str,
3037 strsnlen[j].str + (strsnlen[j].len
3038 - strsnlen[i].len)) == 0)
3042 strsnlen[nstrsnlen++] = strsnlen[i];
3045 size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
3047 for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
3049 char suffix[1 << sfxbits];
3051 for (i = 0; i < (1u << sfxbits) - 1; ++i)
3052 suffix[i] = sfxfreq[i].ch;
3055 size_t newlen[nstrsnlen];
3057 for (i = 0; i < nstrsnlen; ++i)
3058 if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
3059 newlen[i] = strsnlen[i].len - 1;
3061 newlen[i] = strsnlen[i].len;
3064 memset (charused, '\0', sizeof (charused));
3065 size_t ncharused = 0;
3067 const char *tablestr[nstrsnlen];
3068 size_t ntablestr = 1;
3069 tablestr[0] = strsnlen[0].str;
3070 size_t table = newlen[0] + 1;
3071 for (i = 1; i < nstrsnlen; ++i)
3074 for (j = 0; j < ntablestr; ++j)
3075 if (newlen[i] > newlen[j]
3076 && memcmp (tablestr[j],
3077 strsnlen[i].str + (newlen[i] - newlen[j]),
3080 table += newlen[i] - newlen[j];
3081 tablestr[j] = strsnlen[i].str;
3082 newlen[j] = newlen[i];
3085 else if (newlen[i] < newlen[j]
3086 && memcmp (strsnlen[i].str,
3087 tablestr[j] + (newlen[j] - newlen[i]),
3093 table += newlen[i] + 1;
3094 tablestr[ntablestr] = strsnlen[i].str;
3095 newlen[ntablestr] = newlen[i];
3100 for (size_t x = 0; x < newlen[j]; ++x)
3101 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
3105 size_t ncharused_bits = 0;
3107 while (i < ncharused)
3113 size_t table_bits = 0;
3121 size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
3122 size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
3123 + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
3124 + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
3125 + (((total_bits + mnemonic_bits + 7) / 8)
3128 if (new_total < best_so_far)
3130 best_so_far = new_total;
3131 best_mnemonic_bits = mnemonic_bits;
3134 best_suffix = xstrdup (suffix);
3137 best_prefix = xstrdup (prefix);
3138 best_prefix_bits = pfxbits;
3140 best_table_size = table;
3141 best_table_bits = table_bits;
3142 char *cp = best_table = xrealloc (best_table, table);
3143 for (i = 0; i < ntablestr; ++i)
3145 assert (cp + newlen[i] + 1 <= best_table + table);
3146 cp = mempcpy (cp, tablestr[i], newlen[i]);
3149 assert (cp == best_table + table);
3154 fputs ("static const char mnemonic_table[] =\n\"", outfile);
3155 for (size_t i = 0; i < best_table_size; ++i)
3157 if (((i + 1) % 60) == 0)
3158 fputs ("\"\n\"", outfile);
3159 if (!isascii (best_table[i]) || !isprint (best_table[i]))
3160 fprintf (outfile, "\\%03o", best_table[i]);
3162 fputc (best_table[i], outfile);
3164 fputs ("\";\n", outfile);
3166 if (best_prefix[0] != '\0')
3168 "static const char prefix[%zu] = \"%s\";\n"
3169 "#define PREFIXCHAR_BITS %zu\n",
3170 strlen (best_prefix), best_prefix, best_prefix_bits);
3172 fputs ("#define NO_PREFIX\n", outfile);
3174 if (best_suffix[0] != '\0')
3175 fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
3176 strlen (best_suffix), best_suffix);
3178 fputs ("#define NO_SUFFIX\n", outfile);
3180 for (size_t i = 0; i < nmnemonic_strs; ++i)
3182 const char *mne = mnemonic_strs[i];
3185 char *cp = strchr (best_prefix, *mne);
3188 pfxval = 1 + (cp - best_prefix);
3192 size_t l = strlen (mne);
3195 cp = strchr (best_suffix, mne[l - 1]);
3198 sfxval = 1 + (cp - best_suffix);
3202 char *off = memmem (best_table, best_table_size, mne, l);
3203 while (off[l] != '\0')
3205 off = memmem (off + 1, best_table_size, mne, l);
3206 assert (off != NULL);
3209 fprintf (outfile, "#define MNE_%s %#zx\n",
3212 + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));