2 * genmdesc: Generates the machine description
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/opcodes.h>
19 char spec [MONO_INST_MAX];
22 static GHashTable *table;
24 #define eat_whitespace(s) while (*(s) && isspace (*(s))) s++;
27 load_file (const char *name) {
35 if (!(f = fopen (name, "r")))
36 g_error ("Cannot open file '%s'", name);
38 comment = g_string_new ("");
40 * The format of the lines are:
42 * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format]
43 * [cost:num] [res:format] [delay:num] [len:num]
44 * format is a single letter that depends on the field
45 * NOTE: no space between the field name and the ':'
47 * len: maximum instruction length
50 while ((str = fgets (buf, sizeof (buf), f))) {
56 g_string_append (comment, str);
59 p = strchr (str, ':');
61 g_error ("Invalid format at line %d in %s\n", line, name);
64 desc = g_hash_table_lookup (table, str);
66 g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
68 g_error ("Duplicated opcode '%s' at line %d in %s\n", str, line, name);
69 desc->desc = g_strdup (p);
70 desc->comment = g_strdup (comment->str);
71 g_string_truncate (comment, 0);
73 if (strncmp (p, "dest:", 5) == 0) {
74 desc->spec [MONO_INST_DEST] = p [5];
76 } else if (strncmp (p, "src1:", 5) == 0) {
77 desc->spec [MONO_INST_SRC1] = p [5];
79 } else if (strncmp (p, "src2:", 5) == 0) {
80 desc->spec [MONO_INST_SRC2] = p [5];
82 } else if (strncmp (p, "clob:", 5) == 0) {
83 desc->spec [MONO_INST_CLOB] = p [5];
85 /* Currently unused fields
86 } else if (strncmp (p, "cost:", 5) == 0) {
87 desc->spec [MONO_INST_COST] = p [5];
89 } else if (strncmp (p, "res:", 4) == 0) {
90 desc->spec [MONO_INST_RES] = p [4];
92 } else if (strncmp (p, "flags:", 6) == 0) {
93 desc->spec [MONO_INST_FLAGS] = p [6];
95 } else if (strncmp (p, "delay:", 6) == 0) {
96 desc->spec [MONO_INST_DELAY] = p [6];
99 } else if (strncmp (p, "len:", 4) == 0) {
101 desc->spec [MONO_INST_LEN] = strtoul (p, &p, 10);
103 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
112 static OpDesc *opcodes = NULL;
119 table = g_hash_table_new (g_str_hash, g_str_equal);
121 opcodes = g_new0 (OpDesc, OP_LAST);
122 #ifndef MONO_ARCH_ENABLE_NORMALIZE_OPCODES
123 for (i = 0; i < MONO_CEE_LAST; ++i) {
126 desc->name = mono_inst_name (i);
127 g_hash_table_insert (table, (char *)desc->name, desc);
130 for (i = OP_LOAD; i < OP_LAST; ++i) {
133 desc->name = mono_inst_name (i);
134 g_hash_table_insert (table, (char *)desc->name, desc);
139 output_char (FILE *f, char c) {
141 fprintf (f, "%c", c);
143 fprintf (f, "\\x%x\" \"", c);
147 build_table (const char *fname, const char *name) {
151 GString *idx_array = g_string_new ("");
152 /* use this to remove duplicates */
153 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
155 if (!(f = fopen (fname, "w")))
156 g_error ("Cannot open file '%s'", fname);
157 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
158 fprintf (f, "const char %s [] = {\n", name);
160 for (j = 0; j < MONO_INST_MAX; ++j)
162 fprintf (f, "\"\t/* null entry */\n");
164 g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
166 for (i = 0; i < OP_LAST; ++i) {
169 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
172 for (j = 0; j < MONO_INST_MAX; ++j)
173 output_char (f, desc->spec [j]);
174 fprintf (f, "\"\t/* %s */\n", desc->name);
175 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
179 fprintf (f, "};\n\n");
180 fprintf (f, "%s};\n\n", idx_array->str);
182 g_string_free (idx_array, TRUE);
183 g_hash_table_destroy (desc_ht);
191 for (i = 0; i < MONO_CEE_LAST; ++i) {
194 g_print ("%s", desc->comment);
196 g_print ("%s:\n", desc->name);
198 g_print ("%s: %s", desc->name, desc->desc);
199 if (!strchr (desc->desc, '\n'))
203 for (i = OP_LOAD; i < OP_LAST; ++i) {
206 g_print ("%s:\n", desc->name);
208 g_print ("%s: %s", desc->name, desc->desc);
209 if (!strchr (desc->desc, '\n'))
216 * TODO: output the table (possibly merged), in the input format
219 main (int argc, char* argv [])
224 /* useful to get a new file when some opcodes are added: looses the comments, though */
225 load_file (argv [1]);
229 load_file (argv [1]);
230 build_table (argv [2], argv [3]);
233 g_print ("Usage: genmdesc arguments\n");
234 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
235 g_print ("\tgenmdesc desc output name Write to output the description in a table named 'name'.\n");