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