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 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 Red Hat elfutils is an included package of the Open Invention Network.
20 An included package of the Open Invention Network is a package for which
21 Open Invention Network licensees cross-license their patents. No patent
22 license is granted, either expressly or impliedly, by designation as an
23 included package. Should you wish to participate in the Open Invention
24 Network licensing program, please visit www.openinventionnetwork.com
25 <http://www.openinventionnetwork.com>. */
44 #include <sys/param.h>
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
51 /* The error handler. */
52 static void yyerror (const char *s);
54 extern int yylex (void);
55 extern int i386_lineno;
62 unsigned long int bits;
69 enum bittype { zeroone, field, failure } type;
73 struct known_bitfield *field;
75 struct bitvalue *next;
81 enum nametype { string, nfield } type;
85 struct known_bitfield *field;
94 struct argument *next;
100 /* The byte encoding. */
101 struct bitvalue *bytes;
103 /* Prefix possible. */
111 enum { suffix_none = 0, suffix_w, suffix_w0, suffix_W, suffix_tttn,
112 suffix_w1, suffix_W1, suffix_D } suffix;
114 /* Flag set if modr/m is used. */
127 struct instruction *next;
153 static struct known_bitfield ax_reg =
155 .name = "ax", .bits = 0, .tmp = 0
158 static struct known_bitfield dx_reg =
160 .name = "dx", .bits = 0, .tmp = 0
163 static struct known_bitfield di_reg =
165 .name = "es_di", .bits = 0, .tmp = 0
168 static struct known_bitfield si_reg =
170 .name = "ds_si", .bits = 0, .tmp = 0
173 static struct known_bitfield bx_reg =
175 .name = "ds_bx", .bits = 0, .tmp = 0
179 static int bitfield_compare (const void *p1, const void *p2);
180 static void new_bitfield (char *name, unsigned long int num);
181 static void check_bits (struct bitvalue *value);
182 static int check_duplicates (struct bitvalue *val);
183 static int check_argsdef (struct bitvalue *bitval, struct argument *args);
184 static int check_bitsused (struct bitvalue *bitval,
185 struct known_bitfield *suffix,
186 struct argument *args);
187 static struct argname *combine (struct argname *name);
188 static void fillin_arg (struct bitvalue *bytes, struct argname *name,
189 struct instruction *instr, int n);
190 static void find_numbers (void);
191 static int compare_syn (const void *p1, const void *p2);
192 static int compare_suf (const void *p1, const void *p2);
193 static void instrtable_out (void);
195 static void create_mnemonic_table (void);
198 static void *bitfields;
199 static struct instruction *instructions;
200 static size_t ninstructions;
201 static void *synonyms;
202 static void *suffixes;
203 static int nsuffixes;
204 static void *mnemonics;
206 extern FILE *outfile;
208 /* Number of bits used mnemonics. */
210 static size_t best_mnemonic_bits;
215 unsigned long int num;
218 struct known_bitfield *field;
219 struct bitvalue *bit;
220 struct argname *name;
221 struct argument *arg;
231 %token <str> kBITFIELD
235 %type <bit> bit byte bytes
236 %type <field> bitfieldopt
237 %type <name> argcomp arg
238 %type <arg> args optargs
244 spec: masks kPERCPERC '\n' instrs
246 if (error_message_count != 0)
247 error (EXIT_FAILURE, 0,
248 "terminated due to previous error");
254 masks: masks '\n' mask
258 mask: kMASK kBITFIELD kNUMBER
259 { new_bitfield ($2, $3); }
261 { new_bitfield ($2, -1); }
263 { new_bitfield ($2, -2); }
264 | kSYNONYM kBITFIELD kBITFIELD
266 struct synonym *newp = xmalloc (sizeof (*newp));
269 if (tfind (newp, &synonyms, compare_syn) != NULL)
271 "%d: duplicate definition for synonym '%s'",
273 else if (tsearch ( newp, &synonyms, compare_syn) == NULL)
274 error (EXIT_FAILURE, 0, "tsearch");
279 instrs: instrs '\n' instr
283 instr: bytes ':' bitfieldopt kID bitfieldopt optargs
285 if ($3 != NULL && strcmp ($3->name, "RE") != 0
286 && strcmp ($3->name, "R") != 0)
288 error (0, 0, "%d: only 'R' and 'RE' prefix allowed",
291 if (check_duplicates ($1) == 0
292 && check_argsdef ($1, $6) == 0
293 && check_bitsused ($1, $5, $6) == 0)
295 struct instruction *newp = xcalloc (sizeof (*newp),
299 if (strcmp ($3->name, "RE") == 0)
301 else if (strcmp ($3->name, "R") == 0)
307 if (newp->mnemonic != (void *) -1l
308 && tfind ($4, &mnemonics,
309 (comparison_fn_t) strcmp) == NULL)
311 if (tsearch ($4, &mnemonics,
312 (comparison_fn_t) strcmp) == NULL)
313 error (EXIT_FAILURE, errno, "tsearch");
319 if (strcmp ($5->name, "w") == 0)
320 newp->suffix = suffix_w;
321 else if (strcmp ($5->name, "w0") == 0)
322 newp->suffix = suffix_w0;
323 else if (strcmp ($5->name, "tttn") == 0)
324 newp->suffix = suffix_tttn;
325 else if (strcmp ($5->name, "w1") == 0)
326 newp->suffix = suffix_w1;
327 else if (strcmp ($5->name, "W") == 0)
328 newp->suffix = suffix_W;
329 else if (strcmp ($5->name, "W1") == 0)
330 newp->suffix = suffix_W1;
331 else if (strcmp ($5->name, "D") == 0)
332 newp->suffix = suffix_D;
334 error (EXIT_FAILURE, 0,
335 "%s: %d: unknown suffix '%s'",
336 infname, i386_lineno - 1, $5->name);
338 struct suffix search = { .name = $5->name };
339 if (tfind (&search, &suffixes, compare_suf)
342 struct suffix *ns = xmalloc (sizeof (*ns));
344 ns->idx = ++nsuffixes;
345 if (tsearch (ns, &suffixes, compare_suf)
347 error (EXIT_FAILURE, errno, "tsearch");
351 struct argument *args = $6;
355 fillin_arg ($1, args->name, newp, n);
361 newp->next = instructions;
369 bitfieldopt: kBITFIELD
371 struct known_bitfield search;
373 struct known_bitfield **res;
374 res = tfind (&search, &bitfields, bitfield_compare);
377 error (0, 0, "%d: unknown bitfield '%s'",
378 i386_lineno, search.name);
388 bytes: bytes ',' byte
392 struct bitvalue *runp = $1;
393 while (runp->next != NULL)
407 struct bitvalue *runp = $1;
408 while (runp->next != NULL)
419 $$ = xmalloc (sizeof (struct bitvalue));
426 $$ = xmalloc (sizeof (struct bitvalue));
433 $$ = xmalloc (sizeof (struct bitvalue));
434 struct known_bitfield search;
436 struct known_bitfield **res;
437 res = tfind (&search, &bitfields, bitfield_compare);
440 error (0, 0, "%d: unknown bitfield '%s'",
441 i386_lineno, search.name);
461 struct argument *runp = $1;
462 while (runp->next != NULL)
464 runp->next = xmalloc (sizeof (struct argument));
465 runp->next->name = combine ($3);
466 runp->next->next = NULL;
471 $$ = xmalloc (sizeof (struct argument));
472 $$->name = combine ($1);
479 struct argname *runp = $1;
480 while (runp->next != NULL)
490 $$ = xmalloc (sizeof (struct argname));
494 struct known_bitfield search;
496 struct known_bitfield **res;
497 res = tfind (&search, &bitfields, bitfield_compare);
500 if (strcmp ($1, "ax") == 0)
502 else if (strcmp ($1, "dx") == 0)
504 else if (strcmp ($1, "es_di") == 0)
506 else if (strcmp ($1, "ds_si") == 0)
508 else if (strcmp ($1, "ds_bx") == 0)
512 error (0, 0, "%d: unknown bitfield '%s'",
513 i386_lineno, search.name);
522 $$ = xmalloc (sizeof (struct argname));
525 $$->str = xmalloc (2);
531 $$ = xmalloc (sizeof (struct argname));
538 $$ = xmalloc (sizeof (struct argname));
541 $$->str = xmalloc (2);
550 yyerror (const char *s)
552 error (0, 0, gettext ("while reading i386 CPU description: %s at line %d"),
553 gettext (s), i386_lineno);
558 bitfield_compare (const void *p1, const void *p2)
560 struct known_bitfield *f1 = (struct known_bitfield *) p1;
561 struct known_bitfield *f2 = (struct known_bitfield *) p2;
563 return strcmp (f1->name, f2->name);
568 new_bitfield (char *name, unsigned long int num)
570 struct known_bitfield *newp = xmalloc (sizeof (struct known_bitfield));
575 if (tfind (newp, &bitfields, bitfield_compare) != NULL)
577 error (0, 0, "%d: duplicated definition of bitfield '%s'",
583 if (tsearch (newp, &bitfields, bitfield_compare) == NULL)
584 error (EXIT_FAILURE, errno, "%d: cannot insert new bitfield '%s'",
589 /* Check that the number of bits is a multiple of 8. */
591 check_bits (struct bitvalue *val)
593 struct bitvalue *runp = val;
594 unsigned int total = 0;
598 if (runp->type == zeroone)
600 else if (runp->field == NULL)
601 /* No sense doing anything, the field is not known. */
604 total += runp->field->bits;
616 if (val->type == zeroone)
617 obstack_printf (&os, "%u", val->value);
619 obstack_printf (&os, "{%s}", val->field->name);
622 obstack_1grow (&os, '\0');
624 error (0, 0, "%d: field '%s' not a multiple of 8 bits in size",
625 i386_lineno, (char *) obstack_finish (&os));
627 obstack_free (&os, NULL);
633 check_duplicates (struct bitvalue *val)
641 if (val->type == field && val->field != NULL)
643 if (val->field->tmp == testcnt)
645 error (0, 0, "%d: bitfield '%s' used more than once",
646 i386_lineno - 1, val->field->name);
649 val->field->tmp = testcnt;
660 check_argsdef (struct bitvalue *bitval, struct argument *args)
666 for (struct argname *name = args->name; name != NULL; name = name->next)
667 if (name->type == nfield && name->field != NULL
668 && name->field != &ax_reg && name->field != &dx_reg
669 && name->field != &di_reg && name->field != &si_reg
670 && name->field != &bx_reg)
672 struct bitvalue *runp = bitval;
675 if (runp->type == field && runp->field == name->field)
682 error (0, 0, "%d: unknown bitfield '%s' used in output format",
683 i386_lineno - 1, name->field->name);
696 check_bitsused (struct bitvalue *bitval, struct known_bitfield *suffix,
697 struct argument *args)
701 while (bitval != NULL)
703 if (bitval->type == field && bitval->field != NULL
704 && bitval->field != suffix
705 /* {w} is handled special. */
706 && strcmp (bitval->field->name, "w") != 0)
708 struct argument *runp;
709 for (runp = args; runp != NULL; runp = runp->next)
711 struct argname *name = runp->name;
714 if (name->type == nfield && name->field == bitval->field)
726 error (0, 0, "%d: bitfield '%s' not used",
727 i386_lineno - 1, bitval->field->name);
733 bitval = bitval->next;
740 static struct argname *
741 combine (struct argname *name)
743 struct argname *last_str = NULL;
744 for (struct argname *runp = name; runp != NULL; runp = runp->next)
746 if (runp->type == string)
748 if (last_str == NULL)
752 last_str->str = xrealloc (last_str->str,
753 strlen (last_str->str)
754 + strlen (runp->str) + 1);
755 strcat (last_str->str, runp->str);
756 last_str->next = runp->next;
766 #define obstack_grow_str(ob, str) obstack_grow (ob, str, strlen (str))
770 fillin_arg (struct bitvalue *bytes, struct argname *name,
771 struct instruction *instr, int n)
773 static struct obstack ob;
774 static int initialized;
781 struct argname *runp = name;
785 /* We ignore strings in the function name. */
786 if (runp->type == string)
788 if (instr->operands[n].str != NULL)
789 error (EXIT_FAILURE, 0,
790 "%d: cannot have more than one string parameter",
793 instr->operands[n].str = runp->str;
797 assert (runp->type == nfield);
799 /* Construct the function name. */
801 obstack_1grow (&ob, '$');
803 if (runp->field == NULL)
804 /* Add some string which contains invalid characters. */
805 obstack_grow_str (&ob, "!!!INVALID!!!");
808 char *fieldname = runp->field->name;
810 struct synonym search = { .from = fieldname };
812 struct synonym **res = tfind (&search, &synonyms, compare_syn);
814 fieldname = (*res)->to;
816 obstack_grow_str (&ob, fieldname);
819 /* Now compute the bit offset of the field. */
820 struct bitvalue *b = bytes;
822 if (runp->field != NULL)
825 if (b->type == field && b->field != NULL)
827 if (strcmp (b->field->name, runp->field->name) == 0)
829 bitoff += b->field->bits;
836 if (instr->operands[n].off1 == 0)
837 instr->operands[n].off1 = bitoff;
838 else if (instr->operands[n].off2 == 0)
839 instr->operands[n].off2 = bitoff;
840 else if (instr->operands[n].off3 == 0)
841 instr->operands[n].off3 = bitoff;
843 error (EXIT_FAILURE, 0,
844 "%d: cannot have more than three fields in parameter",
847 if (runp->field != NULL
848 && strncasecmp (runp->field->name, "mod", 3) == 0)
854 if (obstack_object_size (&ob) == 0)
855 obstack_grow_str (&ob, "string");
856 obstack_1grow (&ob, '\0');
857 char *fct = obstack_finish (&ob);
859 instr->operands[n].fct = fct;
865 nameout (const void *nodep, VISIT value, int level)
867 if (value == leaf || value == postorder)
868 printf (" %s\n", *(const char **) nodep);
874 compare_argstring (const void *p1, const void *p2)
876 const struct argstring *a1 = (const struct argstring *) p1;
877 const struct argstring *a2 = (const struct argstring *) p2;
879 return strcmp (a1->str, a2->str);
883 static int maxoff[3][3];
884 static int minoff[3][3] = { { 1000, 1000, 1000 },
885 { 1000, 1000, 1000 },
886 { 1000, 1000, 1000 } };
887 static int nbitoff[3][3];
888 static void *fct_names[3];
889 static int nbitfct[3];
891 static void *strs[3];
892 static int nbitstr[3];
893 static int total_bits = 2; // Already counted the rep/repe bits.
898 int nfct_names[3] = { 0, 0, 0 };
899 int nstrs[3] = { 0, 0, 0 };
901 /* We reverse the order of the instruction list while processing it.
902 Later phases need it in the order in which the input file has
904 struct instruction *reversed = NULL;
906 struct instruction *runp = instructions;
909 for (int i = 0; i < 3; ++i)
910 if (runp->operands[i].fct != NULL)
912 struct argstring search = { .str = runp->operands[i].fct };
913 if (tfind (&search, &fct_names[i], compare_argstring) == NULL)
915 struct argstring *newp = xmalloc (sizeof (*newp));
916 newp->str = runp->operands[i].fct;
918 if (tsearch (newp, &fct_names[i], compare_argstring) == NULL)
919 error (EXIT_FAILURE, errno, "tsearch");
923 if (runp->operands[i].str != NULL)
925 search.str = runp->operands[i].str;
926 if (tfind (&search, &strs[i], compare_argstring) == NULL)
928 struct argstring *newp = xmalloc (sizeof (*newp));
929 newp->str = runp->operands[i].str;
931 if (tsearch (newp, &strs[i], compare_argstring) == NULL)
932 error (EXIT_FAILURE, errno, "tsearch");
937 maxoff[i][0] = MAX (maxoff[i][0], runp->operands[i].off1);
938 maxoff[i][1] = MAX (maxoff[i][1], runp->operands[i].off2);
939 maxoff[i][2] = MAX (maxoff[i][2], runp->operands[i].off3);
941 if (runp->operands[i].off1 > 0)
942 minoff[i][0] = MIN (minoff[i][0], runp->operands[i].off1);
943 if (runp->operands[i].off2 > 0)
944 minoff[i][1] = MIN (minoff[i][1], runp->operands[i].off2);
945 if (runp->operands[i].off3 > 0)
946 minoff[i][2] = MIN (minoff[i][2], runp->operands[i].off3);
949 struct instruction *old = runp;
952 old->next = reversed;
955 instructions = reversed;
959 for (int i = 0; i < 3; ++i)
961 // printf ("min1 = %d, min2 = %d, min3 = %d\n", minoff[i][0], minoff[i][1], minoff[i][2]);
962 // printf ("max1 = %d, max2 = %d, max3 = %d\n", maxoff[i][0], maxoff[i][1], maxoff[i][2]);
964 if (minoff[i][0] == 1000)
969 d = maxoff[i][0] - minoff[i][0];
976 total_bits += nbitoff[i][0];
979 if (minoff[i][1] == 1000)
984 d = maxoff[i][1] - minoff[i][1];
991 total_bits += nbitoff[i][1];
994 if (minoff[i][2] == 1000)
999 d = maxoff[i][2] - minoff[i][2];
1006 total_bits += nbitoff[i][2];
1008 // printf ("off1 = %d, off2 = %d, off3 = %d\n", nbitoff[i][0], nbitoff[i][1], nbitoff[i][2]);
1018 total_bits += nbitfct[i];
1019 // printf ("%d fct[%d], %d bits\n", nfct_names[i], i, nbitfct[i]);
1031 total_bits += nbitstr[i];
1034 // twalk (fct_names[i], nameout);
1045 total_bits += nbitsuf;
1046 // printf ("%d suffixes, %d bits\n", nsuffixes, nbitsuf);
1051 compare_syn (const void *p1, const void *p2)
1053 const struct synonym *s1 = (const struct synonym *) p1;
1054 const struct synonym *s2 = (const struct synonym *) p2;
1056 return strcmp (s1->from, s2->from);
1061 compare_suf (const void *p1, const void *p2)
1063 const struct suffix *s1 = (const struct suffix *) p1;
1064 const struct suffix *s2 = (const struct suffix *) p2;
1066 return strcmp (s1->name, s2->name);
1070 static int count_op_str;
1071 static int off_op_str;
1073 print_op_str (const void *nodep, VISIT value,
1074 int level __attribute__ ((unused)))
1076 if (value == leaf || value == postorder)
1078 const char *str = (*(struct argstring **) nodep)->str;
1079 fprintf (outfile, "%s\n \"%s",
1080 count_op_str == 0 ? "" : "\\0\"", str);
1081 (*(struct argstring **) nodep)->idx = ++count_op_str;
1082 (*(struct argstring **) nodep)->off = off_op_str;
1083 off_op_str += strlen (str) + 1;
1089 print_op_str_idx (const void *nodep, VISIT value,
1090 int level __attribute__ ((unused)))
1092 if (value == leaf || value == postorder)
1093 printf (" %d,\n", (*(struct argstring **) nodep)->off);
1098 print_op_fct (const void *nodep, VISIT value,
1099 int level __attribute__ ((unused)))
1101 if (value == leaf || value == postorder)
1103 fprintf (outfile, " FCT_%s,\n", (*(struct argstring **) nodep)->str);
1104 (*(struct argstring **) nodep)->idx = ++count_op_str;
1110 # error "bogus NMNES value"
1114 instrtable_out (void)
1119 create_mnemonic_table ();
1121 fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
1123 fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
1124 lrint (ceil (log2 (NMNES))));
1126 fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
1127 for (int i = 0; i < 3; ++i)
1129 fprintf (outfile, "#define FCT%d_BITS %d\n", i + 1, nbitfct[i]);
1130 if (nbitstr[i] != 0)
1131 fprintf (outfile, "#define STR%d_BITS %d\n", i + 1, nbitstr[i]);
1132 fprintf (outfile, "#define OFF%d_1_BITS %d\n", i + 1, nbitoff[i][0]);
1133 fprintf (outfile, "#define OFF%d_1_BIAS %d\n", i + 1, minoff[i][0]);
1134 if (nbitoff[i][1] != 0)
1136 fprintf (outfile, "#define OFF%d_2_BITS %d\n", i + 1, nbitoff[i][1]);
1137 fprintf (outfile, "#define OFF%d_2_BIAS %d\n", i + 1, minoff[i][1]);
1139 if (nbitoff[i][2] != 0)
1141 fprintf (outfile, "#define OFF%d_3_BITS %d\n", i + 1, nbitoff[i][2]);
1142 fprintf (outfile, "#define OFF%d_3_BIAS %d\n", i + 1, minoff[i][2]);
1146 fputs ("\n#include <i386_data.h>\n\n", outfile);
1149 #define APPEND(a, b) APPEND_ (a, b)
1150 #define APPEND_(a, b) a##b
1151 #define EMIT_SUFFIX(suf) \
1152 fprintf (outfile, "#define suffix_%s %d\n", #suf, APPEND (suffix_, suf))
1162 fputc_unlocked ('\n', outfile);
1164 for (int i = 0; i < 3; ++i)
1168 fprintf (outfile, "static const opfct_t op%d_fct[] =\n{\n NULL,\n",
1170 twalk (fct_names[i], print_op_fct);
1171 fputs ("};\n", outfile);
1173 /* The operand strings. */
1174 if (nbitstr[i] != 0)
1178 fprintf (outfile, "static const char op%d_str[] =", i + 1);
1179 twalk (strs[i], print_op_str);
1180 fputs ("\";\n", outfile);
1182 fprintf (outfile, "static const uint8_t op%d_str_idx[] = {\n",
1184 twalk (strs[i], print_op_str_idx);
1185 fputs ("};\n", outfile);
1190 fputs ("static const struct instr_enc instrtab[] =\n{\n", outfile);
1191 struct instruction *instr;
1192 for (instr = instructions; instr != NULL; instr = instr->next)
1194 fputs (" {", outfile);
1195 if (instr->mnemonic == (void *) -1l)
1196 fputs (" .mnemonic = MNE_INVALID,", outfile);
1198 fprintf (outfile, " .mnemonic = MNE_%s,", instr->mnemonic);
1199 fprintf (outfile, " .rep = %d,", instr->rep);
1200 fprintf (outfile, " .repe = %d,", instr->repe);
1201 fprintf (outfile, " .suffix = %d,", instr->suffix);
1202 fprintf (outfile, " .modrm = %d,", instr->modrm);
1204 for (int i = 0; i < 3; ++i)
1207 if (instr->operands[i].fct != NULL)
1209 struct argstring search = { .str = instr->operands[i].fct };
1210 struct argstring **res = tfind (&search, &fct_names[i],
1212 assert (res != NULL);
1215 fprintf (outfile, " .fct%d = %d,", i + 1, idx);
1218 if (instr->operands[i].str != NULL)
1220 struct argstring search = { .str = instr->operands[i].str };
1221 struct argstring **res = tfind (&search, &strs[i],
1223 assert (res != NULL);
1226 if (nbitstr[i] != 0)
1227 fprintf (outfile, " .str%d = %d,", i + 1, idx);
1229 fprintf (outfile, " .off%d_1 = %d,", i + 1,
1230 MAX (0, instr->operands[i].off1 - minoff[i][0]));
1232 if (nbitoff[i][1] != 0)
1233 fprintf (outfile, " .off%d_2 = %d,", i + 1,
1234 MAX (0, instr->operands[i].off2 - minoff[i][1]));
1236 if (nbitoff[i][2] != 0)
1237 fprintf (outfile, " .off%d_3 = %d,", i + 1,
1238 MAX (0, instr->operands[i].off3 - minoff[i][2]));
1241 fputs (" },\n", outfile);
1243 fputs ("};\n", outfile);
1245 fputs ("static const uint8_t match_data[] =\n{\n", outfile);
1247 for (instr = instructions; instr != NULL; instr = instr->next, ++cnt)
1249 /* First count the number of bytes. */
1250 size_t totalbits = 0;
1251 size_t zerobits = 0;
1252 bool leading_p = true;
1253 size_t leadingbits = 0;
1254 struct bitvalue *b = instr->bytes;
1257 if (b->type == zeroone)
1266 totalbits += b->field->bits;
1267 /* We must always count the mod/rm byte. */
1268 if (strncasecmp (b->field->name, "mod", 3) == 0)
1271 zerobits += b->field->bits;
1276 size_t nbytes = (totalbits - zerobits + 7) / 8;
1277 assert (nbytes > 0);
1278 size_t leadingbytes = leadingbits / 8;
1280 fprintf (outfile, " %#zx,", nbytes | (leadingbytes << 4));
1282 /* Now create the mask and byte values. */
1289 if (b->type == zeroone)
1291 byte = (byte << 1) | b->value;
1292 mask = (mask << 1) | 1;
1295 if (leadingbytes > 0)
1297 assert (mask == 0xff);
1298 fprintf (outfile, " %#" PRIx8 ",", byte);
1302 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
1304 byte = mask = nbits = 0;
1311 assert (leadingbytes == 0);
1313 unsigned long int remaining = b->field->bits;
1314 while (nbits + remaining > 8)
1316 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",",
1317 mask << (8 - nbits), byte << (8 - nbits));
1318 remaining = nbits + remaining - 8;
1319 byte = mask = nbits = 0;
1328 fprintf (outfile, " %#" PRIx8 ", %#" PRIx8 ",", mask, byte);
1329 byte = mask = nbits = 0;
1337 fputc_unlocked ('\n', outfile);
1339 fputs ("};\n", outfile);
1344 static size_t mnemonic_maxlen;
1345 static size_t mnemonic_minlen;
1347 which_chars (const char *str[], size_t nstr)
1349 char used_char[256];
1350 memset (used_char, '\0', sizeof (used_char));
1351 mnemonic_maxlen = 0;
1352 mnemonic_minlen = 10000;
1353 for (size_t cnt = 0; cnt < nstr; ++cnt)
1355 const unsigned char *cp = (const unsigned char *) str[cnt];
1356 mnemonic_maxlen = MAX (mnemonic_maxlen, strlen ((char *) cp));
1357 mnemonic_minlen = MIN (mnemonic_minlen, strlen ((char *) cp));
1359 used_char[*cp++] = 1;
1360 while (*cp != '\0');
1362 size_t nused_char = 0;
1363 for (size_t cnt = 0; cnt < 256; ++cnt)
1364 if (used_char[cnt] != 0)
1370 static const char **mnemonic_strs;
1371 static size_t nmnemonic_strs;
1373 add_mnemonics (const void *nodep, VISIT value,
1374 int level __attribute__ ((unused)))
1376 if (value == leaf || value == postorder)
1377 mnemonic_strs[nmnemonic_strs++] = *(const char **) nodep;
1386 static struct charfreq pfxfreq[256];
1387 static struct charfreq sfxfreq[256];
1391 compare_freq (const void *p1, const void *p2)
1393 const struct charfreq *c1 = (const struct charfreq *) p1;
1394 const struct charfreq *c2 = (const struct charfreq *) p2;
1396 if (c1->freq > c2->freq)
1398 if (c1->freq < c2->freq)
1405 compute_pfxfreq (const char *str[], size_t nstr)
1407 memset (pfxfreq, '\0', sizeof (pfxfreq));
1409 for (size_t i = 0; i < nstr; ++i)
1412 for (size_t i = 0; i < nstr; ++i)
1413 ++pfxfreq[*((const unsigned char *) str[i])].freq;
1415 qsort (pfxfreq, 256, sizeof (struct charfreq), compare_freq);
1418 while (n < 256 && pfxfreq[n].freq != 0)
1431 compute_sfxfreq (size_t nstr, struct strsnlen *strsnlen)
1433 memset (sfxfreq, '\0', sizeof (sfxfreq));
1435 for (size_t i = 0; i < nstr; ++i)
1438 for (size_t i = 0; i < nstr; ++i)
1439 ++sfxfreq[((const unsigned char *) strchrnul (strsnlen[i].str, '\0'))[-1]].freq;
1441 qsort (sfxfreq, 256, sizeof (struct charfreq), compare_freq);
1444 while (n < 256 && sfxfreq[n].freq != 0)
1451 create_mnemonic_table (void)
1453 mnemonic_strs = xmalloc (nmnemonics * sizeof (char *));
1455 twalk (mnemonics, add_mnemonics);
1457 (void) which_chars (mnemonic_strs, nmnemonic_strs);
1459 size_t best_so_far = 100000000;
1460 char *best_prefix = NULL;
1461 char *best_suffix = NULL;
1462 char *best_table = NULL;
1463 size_t best_table_size = 0;
1464 size_t best_table_bits = 0;
1465 size_t best_prefix_bits = 0;
1467 /* We can precompute the prefix characters. */
1468 size_t npfx_char = compute_pfxfreq (mnemonic_strs, nmnemonic_strs);
1470 /* Compute best size for string representation including explicit NUL. */
1471 for (size_t pfxbits = 0; (1u << pfxbits) < 2 * npfx_char; ++pfxbits)
1473 char prefix[1 << pfxbits];
1475 for (i = 0; i < (1u << pfxbits) - 1; ++i)
1476 prefix[i] = pfxfreq[i].ch;
1479 struct strsnlen strsnlen[nmnemonic_strs];
1481 for (i = 0; i < nmnemonic_strs; ++i)
1483 if (strchr (prefix, *mnemonic_strs[i]) != NULL)
1484 strsnlen[i].str = mnemonic_strs[i] + 1;
1486 strsnlen[i].str = mnemonic_strs[i];
1487 strsnlen[i].len = strlen (strsnlen[i].str);
1490 /* With the prefixes gone, try to combine strings. */
1491 size_t nstrsnlen = 1;
1492 for (i = 1; i < nmnemonic_strs; ++i)
1495 for (j = 0; j < nstrsnlen; ++j)
1496 if (strsnlen[i].len > strsnlen[j].len
1497 && strcmp (strsnlen[j].str,
1498 strsnlen[i].str + (strsnlen[i].len
1499 - strsnlen[j].len)) == 0)
1501 strsnlen[j] = strsnlen[i];
1504 else if (strsnlen[i].len < strsnlen[j].len
1505 && strcmp (strsnlen[i].str,
1506 strsnlen[j].str + (strsnlen[j].len
1507 - strsnlen[i].len)) == 0)
1511 strsnlen[nstrsnlen++] = strsnlen[i];
1514 size_t nsfx_char = compute_sfxfreq (nstrsnlen, strsnlen);
1516 for (size_t sfxbits = 0; (1u << sfxbits) < 2 * nsfx_char; ++sfxbits)
1518 char suffix[1 << sfxbits];
1520 for (i = 0; i < (1u << sfxbits) - 1; ++i)
1521 suffix[i] = sfxfreq[i].ch;
1524 size_t newlen[nstrsnlen];
1526 for (i = 0; i < nstrsnlen; ++i)
1527 if (strchr (suffix, strsnlen[i].str[strsnlen[i].len - 1]) != NULL)
1528 newlen[i] = strsnlen[i].len - 1;
1530 newlen[i] = strsnlen[i].len;
1533 memset (charused, '\0', sizeof (charused));
1534 size_t ncharused = 0;
1536 const char *tablestr[nstrsnlen];
1537 size_t ntablestr = 1;
1538 tablestr[0] = strsnlen[0].str;
1539 size_t table = newlen[0] + 1;
1540 for (i = 1; i < nstrsnlen; ++i)
1543 for (j = 0; j < ntablestr; ++j)
1544 if (newlen[i] > newlen[j]
1545 && memcmp (tablestr[j],
1546 strsnlen[i].str + (newlen[i] - newlen[j]),
1549 table += newlen[i] - newlen[j];
1550 tablestr[j] = strsnlen[i].str;
1551 newlen[j] = newlen[i];
1554 else if (newlen[i] < newlen[j]
1555 && memcmp (strsnlen[i].str,
1556 tablestr[j] + (newlen[j] - newlen[i]),
1562 table += newlen[i] + 1;
1563 tablestr[ntablestr] = strsnlen[i].str;
1564 newlen[ntablestr] = newlen[i];
1569 for (size_t x = 0; x < newlen[j]; ++x)
1570 if (charused[((const unsigned char *) tablestr[j])[x]]++ == 0)
1574 size_t ncharused_bits = 0;
1576 while (i < ncharused)
1582 size_t table_bits = 0;
1590 size_t mnemonic_bits = table_bits + pfxbits + sfxbits;
1591 size_t new_total = (((table + 7) / 8) * ncharused_bits + ncharused
1592 + (pfxbits == 0 ? 0 : (1 << pfxbits) - 1)
1593 + (sfxbits == 0 ? 0 : (1 << sfxbits) - 1)
1594 + (((total_bits + mnemonic_bits + 7) / 8)
1597 if (new_total < best_so_far)
1599 best_so_far = new_total;
1600 best_mnemonic_bits = mnemonic_bits;
1603 best_suffix = xstrdup (suffix);
1606 best_prefix = xstrdup (prefix);
1607 best_prefix_bits = pfxbits;
1609 best_table_size = table;
1610 best_table_bits = table_bits;
1611 char *cp = best_table = xrealloc (best_table, table);
1612 for (i = 0; i < ntablestr; ++i)
1614 assert (cp + newlen[i] + 1 <= best_table + table);
1615 cp = mempcpy (cp, tablestr[i], newlen[i]);
1618 assert (cp == best_table + table);
1623 fputs ("static const char mnemonic_table[] =\n\"", outfile);
1624 for (size_t i = 0; i < best_table_size; ++i)
1626 if (((i + 1) % 60) == 0)
1627 fputs ("\"\n\"", outfile);
1628 if (!isascii (best_table[i]) || !isprint (best_table[i]))
1629 fprintf (outfile, "\\%03o", best_table[i]);
1631 fputc (best_table[i], outfile);
1633 fputs ("\";\n", outfile);
1635 if (best_prefix[0] != '\0')
1637 "static const char prefix[%zu] = \"%s\";\n"
1638 "#define PREFIXCHAR_BITS %zu\n",
1639 strlen (best_prefix), best_prefix, best_prefix_bits);
1641 fputs ("#define NO_PREFIX\n", outfile);
1643 if (best_suffix[0] != '\0')
1644 fprintf (outfile, "static const char suffix[%zu] = \"%s\";\n",
1645 strlen (best_suffix), best_suffix);
1647 fputs ("#define NO_SUFFIX\n", outfile);
1649 for (size_t i = 0; i < nmnemonic_strs; ++i)
1651 const char *mne = mnemonic_strs[i];
1654 char *cp = strchr (best_prefix, *mne);
1657 pfxval = 1 + (cp - best_prefix);
1661 size_t l = strlen (mne);
1664 cp = strchr (best_suffix, mne[l - 1]);
1667 sfxval = 1 + (cp - best_suffix);
1671 char *off = memmem (best_table, best_table_size, mne, l);
1672 while (off[l] != '\0')
1674 off = memmem (off + 1, best_table_size, mne, l);
1675 assert (off != NULL);
1678 fprintf (outfile, "#define MNE_%s %#zx\n",
1681 + ((pfxval + (sfxval << best_prefix_bits)) << best_table_bits));