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