2 /* Parser for i386 CPU description.
3 Copyright (C) 2004, 2005, 2007, 2008, 2009 Red Hat, Inc.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2004.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
49 #define obstack_chunk_alloc xmalloc
50 #define obstack_chunk_free free
52 /* The error handler. */
53 static void yyerror (const char *s);
55 extern int yylex (void);
56 extern int i386_lineno;
63 unsigned long int bits;
70 enum bittype { zeroone, field, failure } type;
74 struct known_bitfield *field;
76 struct bitvalue *next;
82 enum nametype { string, nfield } type;
86 struct known_bitfield *field;
95 struct argument *next;
101 /* The byte encoding. */
102 struct bitvalue *bytes;
104 /* Prefix possible. */
112 enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
113 suffix_w1, suffix_W1, suffix_D } suffix;
115 /* Flag set if modr/m is used. */
128 struct instruction *next;
154 static struct known_bitfield ax_reg =
156 .name = "ax", .bits = 0, .tmp = 0
159 static struct known_bitfield dx_reg =
161 .name = "dx", .bits = 0, .tmp = 0
164 static struct known_bitfield di_reg =
166 .name = "es_di", .bits = 0, .tmp = 0
169 static struct known_bitfield si_reg =
171 .name = "ds_si", .bits = 0, .tmp = 0
174 static struct known_bitfield bx_reg =
176 .name = "ds_bx", .bits = 0, .tmp = 0
180 static int bitfield_compare (const void *p1, const void *p2);
181 static void new_bitfield (char *name, unsigned long int num);
182 static void check_bits (struct bitvalue *value);
183 static int check_duplicates (struct bitvalue *val);
184 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
185 static int check_bitsused (struct bitvalue *bitval,
186 struct known_bitfield *suffix,
187 struct argument *args);
188 static struct argname *combine (struct argname *name);
189 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
190 struct instruction *instr, int n);
191 static void find_numbers (void);
192 static int compare_syn (const void *p1, const void *p2);
193 static int compare_suf (const void *p1, const void *p2);
194 static void instrtable_out (void);
196 static void create_mnemonic_table (void);
199 static void *bitfields;
200 static struct instruction *instructions;
201 static size_t ninstructions;
202 static void *synonyms;
203 static void *suffixes;
204 static int nsuffixes;
205 static void *mnemonics;
207 extern FILE *outfile;
209 /* Number of bits used mnemonics. */
211 static size_t best_mnemonic_bits;
216 unsigned long int num;
219 struct known_bitfield *field;
220 struct bitvalue *bit;
221 struct argname *name;
222 struct argument *arg;
232 %token <str> kBITFIELD
236 %type <bit> bit byte bytes
237 %type <field> bitfieldopt
238 %type <name> argcomp arg
239 %type <arg> args optargs
245 spec: masks kPERCPERC '\n' instrs
247 if (error_message_count != 0)
248 error (EXIT_FAILURE, 0,
249 "terminated due to previous error");
255 masks: masks '\n' mask
259 mask: kMASK kBITFIELD kNUMBER
260 { new_bitfield ($2, $3); }
262 { new_bitfield ($2, -1); }
264 { new_bitfield ($2, -2); }
265 | kSYNONYM kBITFIELD kBITFIELD
267 struct synonym *newp = xmalloc (sizeof (*newp));
270 if (tfind (newp, &synonyms, compare_syn) != NULL)
272 "%d: duplicate definition for synonym '%s'",
274 else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
275 error (EXIT_FAILURE, 0, "tsearch");
280 instrs: instrs '\n' instr
284 instr: bytes ':' bitfieldopt kID bitfieldopt optargs
286 if ($3 != NULL && strcmp ($3->name, "RE") != 0
287 && strcmp ($3->name, "R") != 0)
289 error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
292 if (check_duplicates ($1) == 0
293 && check_argsdef ($1, $6) == 0
294 && check_bitsused ($1, $5, $6) == 0)
296 struct instruction *newp = xcalloc (sizeof (*newp),
300 if (strcmp ($3->name, "RE") == 0)
302 else if (strcmp ($3->name, "R") == 0)
308 if (newp->mnemonic != (void *) -1l
309 && tfind ($4, &mnemonics,
310 (int (*)(const void *, const void *)) strcmp) == NULL)
312 if (tsearch ($4, &mnemonics,
313 (int (*)(const void *, const void *)) strcmp) == NULL)
314 error (EXIT_FAILURE, errno, "tsearch");
320 if (strcmp ($5->name, "w") == 0)
321 newp->suffix = suffix_w;
322 else if (strcmp ($5->name, "w0") == 0)
323 newp->suffix = suffix_w0;
324 else if (strcmp ($5->name, "tttn") == 0)
325 newp->suffix = suffix_tttn;
326 else if (strcmp ($5->name, "w1") == 0)
327 newp->suffix = suffix_w1;
328 else if (strcmp ($5->name, "W") == 0)
329 newp->suffix = suffix_W;
330 else if (strcmp ($5->name, "W1") == 0)
331 newp->suffix = suffix_W1;
332 else if (strcmp ($5->name, "D") == 0)
333 newp->suffix = suffix_D;
335 error (EXIT_FAILURE, 0,
336 "%s: %d: unknown suffix '%s'",
337 infname, i386_lineno - 1, $5->name);
339 struct suffix search = { .name = $5->name };
340 if (tfind (&search, &suffixes, compare_suf)
343 struct suffix *ns = xmalloc (sizeof (*ns));
345 ns->idx = ++nsuffixes;
346 if (tsearch (ns, &suffixes, compare_suf)
348 error (EXIT_FAILURE, errno, "tsearch");
352 struct argument *args = $6;
356 fillin_arg ($1, args->name, newp, n);
362 newp->next = instructions;
370 bitfieldopt: kBITFIELD
372 struct known_bitfield search;
374 struct known_bitfield **res;
375 res = tfind (&search, &bitfields, bitfield_compare);
378 error (0, 0, "%d: unknown bitfield '%s'",
379 i386_lineno, search.name);
389 bytes: bytes ',' byte
393 struct bitvalue *runp = $1;
394 while (runp->next != NULL)
408 struct bitvalue *runp = $1;
409 while (runp->next != NULL)
420 $$ = xmalloc (sizeof (struct bitvalue));
427 $$ = xmalloc (sizeof (struct bitvalue));
434 $$ = xmalloc (sizeof (struct bitvalue));
435 struct known_bitfield search;
437 struct known_bitfield **res;
438 res = tfind (&search, &bitfields, bitfield_compare);
441 error (0, 0, "%d: unknown bitfield '%s'",
442 i386_lineno, search.name);
462 struct argument *runp = $1;
463 while (runp->next != NULL)
465 runp->next = xmalloc (sizeof (struct argument));
466 runp->next->name = combine ($3);
467 runp->next->next = NULL;
472 $$ = xmalloc (sizeof (struct argument));
473 $$->name = combine ($1);
480 struct argname *runp = $1;
481 while (runp->next != NULL)
491 $$ = xmalloc (sizeof (struct argname));
495 struct known_bitfield search;
497 struct known_bitfield **res;
498 res = tfind (&search, &bitfields, bitfield_compare);
501 if (strcmp ($1, "ax") == 0)
503 else if (strcmp ($1, "dx") == 0)
505 else if (strcmp ($1, "es_di") == 0)
507 else if (strcmp ($1, "ds_si") == 0)
509 else if (strcmp ($1, "ds_bx") == 0)
513 error (0, 0, "%d: unknown bitfield '%s'",
514 i386_lineno, search.name);
523 $$ = xmalloc (sizeof (struct argname));
526 $$->str = xmalloc (2);
532 $$ = xmalloc (sizeof (struct argname));
539 $$ = xmalloc (sizeof (struct argname));
542 $$->str = xmalloc (2);
551 yyerror (const char *s)
553 error (0, 0, _("while reading i386 CPU description: %s at line %d"),
559 bitfield_compare (const void *p1, const void *p2)
561 struct known_bitfield *f1 = (struct known_bitfield *) p1;
562 struct known_bitfield *f2 = (struct known_bitfield *) p2;
564 return strcmp (f1->name, f2->name);
569 new_bitfield (char *name, unsigned long int num)
571 struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
576 if (tfind (newp, &bitfields, bitfield_compare) != NULL)
578 error (0, 0, "%d: duplicated definition of bitfield '%s'",
585 if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
586 error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
591 /* Check that the number of bits is a multiple of 8. */
593 check_bits (struct bitvalue *val)
595 struct bitvalue *runp = val;
596 unsigned int total = 0;
600 if (runp->type == zeroone)
602 else if (runp->field == NULL)
603 /* No sense doing anything, the field is not known. */
606 total += runp->field->bits;
618 if (val->type == zeroone)
619 obstack_printf (&os, "%u", val->value);
621 obstack_printf (&os, "{%s}", val->field->name);
624 obstack_1grow (&os, '\0');
626 error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
627 i386_lineno, (char *) obstack_finish (&os));
629 obstack_free (&os, NULL);
635 check_duplicates (struct bitvalue *val)
643 if (val->type == field && val->field != NULL)
645 if (val->field->tmp == testcnt)
647 error (0, 0, "%d: bitfield '%s' used more than once",
648 i386_lineno - 1, val->field->name);
651 val->field->tmp = testcnt;
662 check_argsdef (struct bitvalue *bitval, struct argument *args)
668 for (struct argname *name = args->name; name != NULL; name = name->next)
669 if (name->type == nfield && name->field != NULL
670 && name->field != &ax_reg && name->field != &dx_reg
671 && name->field != &di_reg && name->field != &si_reg
672 && name->field != &bx_reg)
674 struct bitvalue *runp = bitval;
677 if (runp->type == field && runp->field == name->field)
684 error (0, 0, "%d: unknown bitfield '%s' used in output format",
685 i386_lineno - 1, name->field->name);
698 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
699 struct argument *args)
703 while (bitval != NULL)
705 if (bitval->type == field && bitval->field != NULL
706 && bitval->field != suffix
707 /* {w} is handled special. */
708 && strcmp (bitval->field->name, "w") != 0)
710 struct argument *runp;
711 for (runp = args; runp != NULL; runp = runp->next)
713 struct argname *name = runp->name;
716 if (name->type == nfield && name->field == bitval->field)
728 error (0, 0, "%d: bitfield '%s' not used",
729 i386_lineno - 1, bitval->field->name);
735 bitval = bitval->next;
742 static struct argname *
743 combine (struct argname *name)
745 struct argname *last_str = NULL;
746 for (struct argname *runp = name; runp != NULL; runp = runp->next)
748 if (runp->type == string)
750 if (last_str == NULL)
754 last_str->str = xrealloc (last_str->str,
755 strlen (last_str->str)
756 + strlen (runp->str) + 1);
757 strcat (last_str->str, runp->str);
758 last_str->next = runp->next;
768 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
772 fillin_arg (struct bitvalue *bytes, struct argname *name,
773 struct instruction *instr, int n)
775 static struct obstack ob;
776 static int initialized;
783 struct argname *runp = name;
787 /* We ignore strings in the function name. */
788 if (runp->type == string)
790 if (instr->operands[n].str != NULL)
791 error (EXIT_FAILURE, 0,
792 "%d: cannot have more than one string parameter",
795 instr->operands[n].str = runp->str;
799 assert (runp->type == nfield);
801 /* Construct the function name. */
803 obstack_1grow (&ob, '$');
805 if (runp->field == NULL)
806 /* Add some string which contains invalid characters. */
807 obstack_grow_str (&ob, "!!!INVALID!!!");
810 char *fieldname = runp->field->name;
812 struct synonym search = { .from = fieldname };
814 struct synonym **res = tfind (&search, &synonyms, compare_syn);
816 fieldname = (*res)->to;
818 obstack_grow_str (&ob, fieldname);
821 /* Now compute the bit offset of the field. */
822 struct bitvalue *b = bytes;
824 if (runp->field != NULL)
827 if (b->type == field && b->field != NULL)
829 if (strcmp (b->field->name, runp->field->name) == 0)
831 bitoff += b->field->bits;
838 if (instr->operands[n].off1 == 0)
839 instr->operands[n].off1 = bitoff;
840 else if (instr->operands[n].off2 == 0)
841 instr->operands[n].off2 = bitoff;
842 else if (instr->operands[n].off3 == 0)
843 instr->operands[n].off3 = bitoff;
845 error (EXIT_FAILURE, 0,
846 "%d: cannot have more than three fields in parameter",
849 if (runp->field != NULL
850 && strncasecmp (runp->field->name, "mod", 3) == 0)
856 if (obstack_object_size (&ob) == 0)
857 obstack_grow_str (&ob, "string");
858 obstack_1grow (&ob, '\0');
859 char *fct = obstack_finish (&ob);
861 instr->operands[n].fct = fct;
867 nameout (const void *nodep, VISIT value, int level)
869 if (value == leaf || value == postorder)
870 printf (" %s\n", *(const char **) nodep);
876 compare_argstring (const void *p1, const void *p2)
878 const struct argstring *a1 = (const struct argstring *) p1;
879 const struct argstring *a2 = (const struct argstring *) p2;
881 return strcmp (a1->str, a2->str);
885 static int maxoff[3][3];
886 static int minoff[3][3] = { { 1000, 1000, 1000 },
887 { 1000, 1000, 1000 },
888 { 1000, 1000, 1000 } };
889 static int nbitoff[3][3];
890 static void *fct_names[3];
891 static int nbitfct[3];
893 static void *strs[3];
894 static int nbitstr[3];
895 static int total_bits = 2; // Already counted the rep/repe bits.
900 int nfct_names[3] = { 0, 0, 0 };
901 int nstrs[3] = { 0, 0, 0 };
903 /* We reverse the order of the instruction list while processing it.
904 Later phases need it in the order in which the input file has
906 struct instruction *reversed = NULL;
908 struct instruction *runp = instructions;
911 for (int i = 0; i < 3; ++i)
912 if (runp->operands[i].fct != NULL)
914 struct argstring search = { .str = runp->operands[i].fct };
915 if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
917 struct argstring *newp = xmalloc (sizeof (*newp));
918 newp->str = runp->operands[i].fct;
920 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
921 error (EXIT_FAILURE, errno, "tsearch");
925 if (runp->operands[i].str != NULL)
927 search.str = runp->operands[i].str;
928 if (tfind (&search, &strs[i], compare_argstring) == NULL)
930 struct argstring *newp = xmalloc (sizeof (*newp));
931 newp->str = runp->operands[i].str;
933 if (tsearch (newp, &strs[i], compare_argstring) == NULL)
934 error (EXIT_FAILURE, errno, "tsearch");
939 maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
940 maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
941 maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
943 if (runp->operands[i].off1 > 0)
944 minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
945 if (runp->operands[i].off2 > 0)
946 minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
947 if (runp->operands[i].off3 > 0)
948 minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
951 struct instruction *old = runp;
954 old->next = reversed;
957 instructions = reversed;
961 for (int i = 0; i < 3; ++i)
963 // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
964 // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
966 if (minoff[i][0] == 1000)
971 d = maxoff[i][0] - minoff[i][0];
978 total_bits += nbitoff[i][0];
981 if (minoff[i][1] == 1000)
986 d = maxoff[i][1] - minoff[i][1];
993 total_bits += nbitoff[i][1];
996 if (minoff[i][2] == 1000)
1001 d = maxoff[i][2] - minoff[i][2];
1008 total_bits += nbitoff[i][2];
1010 // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
1020 total_bits += nbitfct[i];
1021 // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
1033 total_bits += nbitstr[i];
1036 // twalk (fct_names[i], nameout);
1047 total_bits += nbitsuf;
1048 // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
1053 compare_syn (const void *p1, const void *p2)
1055 const struct synonym *s1 = (const struct synonym *) p1;
1056 const struct synonym *s2 = (const struct synonym *) p2;
1058 return strcmp (s1->from, s2->from);
1063 compare_suf (const void *p1, const void *p2)
1065 const struct suffix *s1 = (const struct suffix *) p1;
1066 const struct suffix *s2 = (const struct suffix *) p2;
1068 return strcmp (s1->name, s2->name);
1072 static int count_op_str;
1073 static int off_op_str;
1075 print_op_str (const void *nodep, VISIT value,
1076 int level __attribute__ ((unused)))
1078 if (value == leaf || value == postorder)
1080 const char *str = (*(struct argstring **) nodep)->str;
1081 fprintf (outfile, "%s\n \"%s",
1082 count_op_str == 0 ? "" : "\\0\"", str);
1083 (*(struct argstring **) nodep)->idx = ++count_op_str;
1084 (*(struct argstring **) nodep)->off = off_op_str;
1085 off_op_str += strlen (str) + 1;
1091 print_op_str_idx (const void *nodep, VISIT value,
1092 int level __attribute__ ((unused)))
1094 if (value == leaf || value == postorder)
1095 printf (" %d,\n", (*(struct argstring **) nodep)->off);
1100 print_op_fct (const void *nodep, VISIT value,
1101 int level __attribute__ ((unused)))
1103 if (value == leaf || value == postorder)
1105 fprintf (outfile, " FCT_%s,\n", (*(struct argstring **) nodep)->str);
1106 (*(struct argstring **) nodep)->idx = ++count_op_str;
1112 # error "bogus NMNES value"
1116 instrtable_out (void)
1121 create_mnemonic_table ();
1123 fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
1125 fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
1126 lrint (ceil (log2 (NMNES))));
1128 fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
1129 for (int i = 0; i < 3; ++i)
1131 fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
1132 if (nbitstr[i] != 0)
1133 fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
1134 fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
1135 fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
1136 if (nbitoff[i][1] != 0)
1138 fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
1139 fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
1141 if (nbitoff[i][2] != 0)
1143 fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
1144 fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
1148 fputs ("\n#include <i386_data.h>\n\n", outfile);
1151 #define APPEND(a, b) APPEND_ (a, b)
1152 #define APPEND_(a, b) a##b
1153 #define EMIT_SUFFIX(suf) \
1154 fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
1164 fputc_unlocked ('\n', outfile);
1166 for (int i = 0; i < 3; ++i)
1170 fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n NULL,\n",
1172 twalk (fct_names[i], print_op_fct);
1173 fputs ("};\n", outfile);
1175 /* The operand strings. */
1176 if (nbitstr[i] != 0)
1180 fprintf (outfile, "static const char op%d_str[] =", i + 1);
1181 twalk (strs[i], print_op_str);
1182 fputs ("\";\n", outfile);
1184 fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
1186 twalk (strs[i], print_op_str_idx);
1187 fputs ("};\n", outfile);
1192 fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
1193 struct instruction *instr;
1194 for (instr = instructions; instr != NULL; instr = instr->next)
1196 fputs (" {", outfile);
1197 if (instr->mnemonic == (void *) -1l)
1198 fputs (" .mnemonic = MNE_INVALID,", outfile);
1200 fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
1201 fprintf (outfile, " .rep = %d,", instr->rep);
1202 fprintf (outfile, " .repe = %d,", instr->repe);
1203 fprintf (outfile, " .suffix = %d,", instr->suffix);
1204 fprintf (outfile, " .modrm = %d,", instr->modrm);
1206 for (int i = 0; i < 3; ++i)
1209 if (instr->operands[i].fct != NULL)
1211 struct argstring search = { .str = instr->operands[i].fct };
1212 struct argstring **res = tfind (&search, &fct_names[i],
1214 assert (res != NULL);
1217 fprintf (outfile, " .fct%d = %d,", i + 1, idx);
1220 if (instr->operands[i].str != NULL)
1222 struct argstring search = { .str = instr->operands[i].str };
1223 struct argstring **res = tfind (&search, &strs[i],
1225 assert (res != NULL);
1228 if (nbitstr[i] != 0)
1229 fprintf (outfile, " .str%d = %d,", i + 1, idx);
1231 fprintf (outfile, " .off%d_1 = %d,", i + 1,
1232 MAX (0, instr->operands[i].off1 - minoff[i][0]));
1234 if (nbitoff[i][1] != 0)
1235 fprintf (outfile, " .off%d_2 = %d,", i + 1,
1236 MAX (0, instr->operands[i].off2 - minoff[i][1]));
1238 if (nbitoff[i][2] != 0)
1239 fprintf (outfile, " .off%d_3 = %d,", i + 1,
1240 MAX (0, instr->operands[i].off3 - minoff[i][2]));
1243 fputs (" },\n", outfile);
1245 fputs ("};\n", outfile);
1247 fputs ("static const uint8_t match_data[] =\n{\n", outfile);
1249 for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
1251 /* First count the number of bytes. */
1252 size_t totalbits = 0;
1253 size_t zerobits = 0;
1254 bool leading_p = true;
1255 size_t leadingbits = 0;
1256 struct bitvalue *b = instr->bytes;
1259 if (b->type == zeroone)
1268 totalbits += b->field->bits;
1269 /* We must always count the mod/rm byte. */
1270 if (strncasecmp (b->field->name, "mod", 3) == 0)
1273 zerobits += b->field->bits;
1278 size_t nbytes = (totalbits - zerobits + 7) / 8;
1279 assert (nbytes > 0);
1280 size_t leadingbytes = leadingbits / 8;
1282 fprintf (outfile, " %#zx,", nbytes | (leadingbytes << 4));
1284 /* Now create the mask and byte values. */
1291 if (b->type == zeroone)
1293 byte = (byte << 1) | b->value;
1294 mask = (mask << 1) | 1;
1297 if (leadingbytes > 0)
1299 assert (mask == 0xff);
1300 fprintf (outfile, " %#" PRIx8 ",", byte);
1304 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
1306 byte = mask = nbits = 0;
1313 assert (leadingbytes == 0);
1315 unsigned long int remaining = b->field->bits;
1316 while (nbits + remaining > 8)
1318 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
1319 mask << (8 - nbits), byte << (8 - nbits));
1320 remaining = nbits + remaining - 8;
1321 byte = mask = nbits = 0;
1330 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
1331 byte = mask = nbits = 0;
1339 fputc_unlocked ('\n', outfile);
1341 fputs ("};\n", outfile);
1346 static size_t mnemonic_maxlen;
1347 static size_t mnemonic_minlen;
1349 which_chars (const char *str[], size_t nstr)
1351 char used_char[256];
1352 memset (used_char, '\0', sizeof (used_char));
1353 mnemonic_maxlen = 0;
1354 mnemonic_minlen = 10000;
1355 for (size_t cnt = 0; cnt < nstr; ++cnt)
1357 const unsigned char *cp = (const unsigned char *) str[cnt];
1358 mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
1359 mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
1361 used_char[*cp++] = 1;
1362 while (*cp != '\0');
1364 size_t nused_char = 0;
1365 for (size_t cnt = 0; cnt < 256; ++cnt)
1366 if (used_char[cnt] != 0)
1372 static const char **mnemonic_strs;
1373 static size_t nmnemonic_strs;
1375 add_mnemonics (const void *nodep, VISIT value,
1376 int level __attribute__ ((unused)))
1378 if (value == leaf || value == postorder)
1379 mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
1388 static struct charfreq pfxfreq[256];
1389 static struct charfreq sfxfreq[256];
1393 compare_freq (const void *p1, const void *p2)
1395 const struct charfreq *c1 = (const struct charfreq *) p1;
1396 const struct charfreq *c2 = (const struct charfreq *) p2;
1398 if (c1->freq > c2->freq)
1400 if (c1->freq < c2->freq)
1407 compute_pfxfreq (const char *str[], size_t nstr)
1409 memset (pfxfreq, '\0', sizeof (pfxfreq));
1411 for (size_t i = 0; i < nstr; ++i)
1414 for (size_t i = 0; i < nstr; ++i)
1415 ++pfxfreq[*((const unsigned char *) str[i])].freq;
1417 qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
1420 while (n < 256 && pfxfreq[n].freq != 0)
1433 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
1435 memset (sfxfreq, '\0', sizeof (sfxfreq));
1437 for (size_t i = 0; i < nstr; ++i)
1440 for (size_t i = 0; i < nstr; ++i)
1441 ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
1443 qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
1446 while (n < 256 && sfxfreq[n].freq != 0)
1453 create_mnemonic_table (void)
1455 mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
1457 twalk (mnemonics, add_mnemonics);
1459 (void) which_chars (mnemonic_strs, nmnemonic_strs);
1461 size_t best_so_far = 100000000;
1462 char *best_prefix = NULL;
1463 char *best_suffix = NULL;
1464 char *best_table = NULL;
1465 size_t best_table_size = 0;
1466 size_t best_table_bits = 0;
1467 size_t best_prefix_bits = 0;
1469 /* We can precompute the prefix characters. */
1470 size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
1472 /* Compute best size for string representation including explicit NUL. */
1473 for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
1475 char prefix[1 << pfxbits];
1477 for (i = 0; i < (1u << pfxbits) - 1; ++i)
1478 prefix[i] = pfxfreq[i].ch;
1481 struct strsnlen strsnlen[nmnemonic_strs];
1483 for (i = 0; i < nmnemonic_strs; ++i)
1485 if (strchr (prefix, *mnemonic_strs[i]) != NULL)
1486 strsnlen[i].str = mnemonic_strs[i] + 1;
1488 strsnlen[i].str = mnemonic_strs[i];
1489 strsnlen[i].len = strlen (strsnlen[i].str);
1492 /* With the prefixes gone, try to combine strings. */
1493 size_t nstrsnlen = 1;
1494 for (i = 1; i < nmnemonic_strs; ++i)
1497 for (j = 0; j < nstrsnlen; ++j)
1498 if (strsnlen[i].len > strsnlen[j].len
1499 && strcmp (strsnlen[j].str,
1500 strsnlen[i].str + (strsnlen[i].len
1501 - strsnlen[j].len)) == 0)
1503 strsnlen[j] = strsnlen[i];
1506 else if (strsnlen[i].len < strsnlen[j].len
1507 && strcmp (strsnlen[i].str,
1508 strsnlen[j].str + (strsnlen[j].len
1509 - strsnlen[i].len)) == 0)
1513 strsnlen[nstrsnlen++] = strsnlen[i];
1516 size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
1518 for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
1520 char suffix[1 << sfxbits];
1522 for (i = 0; i < (1u << sfxbits) - 1; ++i)
1523 suffix[i] = sfxfreq[i].ch;
1526 size_t newlen[nstrsnlen];
1528 for (i = 0; i < nstrsnlen; ++i)
1529 if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
1530 newlen[i] = strsnlen[i].len - 1;
1532 newlen[i] = strsnlen[i].len;
1535 memset (charused, '\0', sizeof (charused));
1536 size_t ncharused = 0;
1538 const char *tablestr[nstrsnlen];
1539 size_t ntablestr = 1;
1540 tablestr[0] = strsnlen[0].str;
1541 size_t table = newlen[0] + 1;
1542 for (i = 1; i < nstrsnlen; ++i)
1545 for (j = 0; j < ntablestr; ++j)
1546 if (newlen[i] > newlen[j]
1547 && memcmp (tablestr[j],
1548 strsnlen[i].str + (newlen[i] - newlen[j]),
1551 table += newlen[i] - newlen[j];
1552 tablestr[j] = strsnlen[i].str;
1553 newlen[j] = newlen[i];
1556 else if (newlen[i] < newlen[j]
1557 && memcmp (strsnlen[i].str,
1558 tablestr[j] + (newlen[j] - newlen[i]),
1564 table += newlen[i] + 1;
1565 tablestr[ntablestr] = strsnlen[i].str;
1566 newlen[ntablestr] = newlen[i];
1571 for (size_t x = 0; x < newlen[j]; ++x)
1572 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
1576 size_t ncharused_bits = 0;
1578 while (i < ncharused)
1584 size_t table_bits = 0;
1592 size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
1593 size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
1594 + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
1595 + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
1596 + (((total_bits + mnemonic_bits + 7) / 8)
1599 if (new_total < best_so_far)
1601 best_so_far = new_total;
1602 best_mnemonic_bits = mnemonic_bits;
1605 best_suffix = xstrdup (suffix);
1608 best_prefix = xstrdup (prefix);
1609 best_prefix_bits = pfxbits;
1611 best_table_size = table;
1612 best_table_bits = table_bits;
1613 char *cp = best_table = xrealloc (best_table, table);
1614 for (i = 0; i < ntablestr; ++i)
1616 assert (cp + newlen[i] + 1 <= best_table + table);
1617 cp = mempcpy (cp, tablestr[i], newlen[i]);
1620 assert (cp == best_table + table);
1625 fputs ("static const char mnemonic_table[] =\n\"", outfile);
1626 for (size_t i = 0; i < best_table_size; ++i)
1628 if (((i + 1) % 60) == 0)
1629 fputs ("\"\n\"", outfile);
1630 if (!isascii (best_table[i]) || !isprint (best_table[i]))
1631 fprintf (outfile, "\\%03o", best_table[i]);
1633 fputc (best_table[i], outfile);
1635 fputs ("\";\n", outfile);
1637 if (best_prefix[0] != '\0')
1639 "static const char prefix[%zu] = \"%s\";\n"
1640 "#define PREFIXCHAR_BITS %zu\n",
1641 strlen (best_prefix), best_prefix, best_prefix_bits);
1643 fputs ("#define NO_PREFIX\n", outfile);
1645 if (best_suffix[0] != '\0')
1646 fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
1647 strlen (best_suffix), best_suffix);
1649 fputs ("#define NO_SUFFIX\n", outfile);
1651 for (size_t i = 0; i < nmnemonic_strs; ++i)
1653 const char *mne = mnemonic_strs[i];
1656 char *cp = strchr (best_prefix, *mne);
1659 pfxval = 1 + (cp - best_prefix);
1663 size_t l = strlen (mne);
1666 cp = strchr (best_suffix, mne[l - 1]);
1669 sfxval = 1 + (cp - best_suffix);
1673 char *off = memmem (best_table, best_table_size, mne, l);
1674 while (off[l] != '\0')
1676 off = memmem (off + 1, best_table_size, mne, l);
1677 assert (off != NULL);
1680 fprintf (outfile, "#define MNE_%s %#zx\n",
1683 + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));