2 * genmdesc: Generates the machine description
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/opcodes.h>
14 #define MINI_OP(a,b,dest,src1,src2) b,
15 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
16 /* keep in sync with the enum in mini.h */
17 static const char* const
25 * Duplicate this from helpers.c, so the opcode name array can be omitted when
30 if (op >= OP_LOAD && op <= OP_LAST)
31 return opnames [op - OP_LOAD];
33 return mono_opcode_name (op);
34 g_error ("unknown opcode name for %d", op);
43 char spec [MONO_INST_MAX];
47 static GHashTable *table;
48 static GHashTable *template_table;
50 #define eat_whitespace(s) while (*(s) && isspace (*(s))) s++;
53 load_file (const char *name) {
61 if (!(f = fopen (name, "r")))
62 g_error ("Cannot open file '%s'", name);
64 comment = g_string_new ("");
66 * The format of the lines are:
68 * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format]
69 * [cost:num] [res:format] [delay:num] [len:num] [nacl:num]
70 * format is a single letter that depends on the field
71 * NOTE: no space between the field name and the ':'
73 * len: maximum instruction length
76 while ((str = fgets (buf, sizeof (buf), f))) {
77 gboolean is_template = FALSE;
78 gboolean nacl_length_set = FALSE;
85 g_string_append (comment, str);
88 p = strchr (str, ':');
90 g_error ("Invalid format at line %d in %s\n", line, name);
93 if (strcmp (str, "template") == 0) {
95 desc = g_new0 (OpDesc, 1);
97 desc = g_hash_table_lookup (table, str);
99 g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
101 g_error ("Duplicated opcode %s at line %d in %s\n", str, line, name);
103 desc->desc = g_strdup (p);
104 desc->comment = g_strdup (comment->str);
105 g_string_truncate (comment, 0);
107 if (strncmp (p, "dest:", 5) == 0) {
108 desc->spec [MONO_INST_DEST] = p [5];
110 } else if (strncmp (p, "src1:", 5) == 0) {
111 desc->spec [MONO_INST_SRC1] = p [5];
113 } else if (strncmp (p, "src2:", 5) == 0) {
114 desc->spec [MONO_INST_SRC2] = p [5];
116 } else if (strncmp (p, "src3:", 5) == 0) {
117 desc->spec [MONO_INST_SRC3] = p [5];
119 } else if (strncmp (p, "clob:", 5) == 0) {
120 desc->spec [MONO_INST_CLOB] = p [5];
122 /* Currently unused fields
123 } else if (strncmp (p, "cost:", 5) == 0) {
124 desc->spec [MONO_INST_COST] = p [5];
126 } else if (strncmp (p, "res:", 4) == 0) {
127 desc->spec [MONO_INST_RES] = p [4];
129 } else if (strncmp (p, "flags:", 6) == 0) {
130 desc->spec [MONO_INST_FLAGS] = p [6];
132 } else if (strncmp (p, "delay:", 6) == 0) {
133 desc->spec [MONO_INST_DELAY] = p [6];
136 } else if (strncmp (p, "len:", 4) == 0) {
139 size = strtoul (p, &p, 10);
140 if (!nacl_length_set) {
141 desc->spec [MONO_INST_LEN] = size;
143 } else if (strncmp (p, "nacl:", 5) == 0) {
146 size = strtoul (p, &p, 10);
148 desc->spec [MONO_INST_LEN] = size;
149 nacl_length_set = TRUE;
151 } else if (strncmp (p, "template:", 9) == 0) {
157 while (*p && isalnum (*p)) ++p;
159 tdesc = g_hash_table_lookup (template_table, tname);
161 g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
162 for (i = 0; i < MONO_INST_MAX; ++i) {
164 g_error ("The template overrides any previous value set at line %d in %s\n", line, name);
166 memcpy (desc->spec, tdesc->spec, sizeof (desc->spec));
167 } else if (strncmp (p, "name:", 5) == 0) {
170 g_error ("name tag only valid in templates at '%s' at line %d in %s\n", p, line, name);
172 g_error ("Duplicated name tag in template %s at '%s' at line %d in %s\n", desc->name, p, line, name);
175 while (*p && isalnum (*p)) ++p;
177 if (g_hash_table_lookup (template_table, tname))
178 g_error ("Duplicated template %s at line %d in %s\n", tname, line, name);
179 desc->name = g_strdup (tname);
180 g_hash_table_insert (template_table, (void*)desc->name, desc);
182 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
186 if (is_template && !desc->name)
187 g_error ("Template without name at line %d in %s\n", line, name);
193 static OpDesc *opcodes = NULL;
200 template_table = g_hash_table_new (g_str_hash, g_str_equal);
201 table = g_hash_table_new (g_str_hash, g_str_equal);
203 opcodes = g_new0 (OpDesc, OP_LAST);
204 for (i = OP_LOAD; i < OP_LAST; ++i) {
207 desc->name = inst_name (i);
208 g_hash_table_insert (table, (char *)desc->name, desc);
213 output_char (FILE *f, char c) {
215 fprintf (f, "%c", c);
217 fprintf (f, "\\x%x\" \"", c);
221 build_table (const char *fname, const char *name) {
225 GString *idx_array = g_string_new ("");
226 /* use this to remove duplicates */
227 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
229 if (!(f = fopen (fname, "w")))
230 g_error ("Cannot open file '%s'", fname);
231 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
232 fprintf (f, "const char %s [] = {\n", name);
234 for (j = 0; j < MONO_INST_MAX; ++j)
236 fprintf (f, "\"\t/* null entry */\n");
238 g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
240 for (i = OP_LOAD; i < OP_LAST; ++i) {
243 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
246 for (j = 0; j < MONO_INST_MAX; ++j)
247 output_char (f, desc->spec [j]);
248 fprintf (f, "\"\t/* %s */\n", desc->name);
249 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
253 fprintf (f, "};\n\n");
254 fprintf (f, "%s};\n\n", idx_array->str);
256 g_string_free (idx_array, TRUE);
257 g_hash_table_destroy (desc_ht);
265 for (i = 0; i < MONO_CEE_LAST; ++i) {
268 g_print ("%s", desc->comment);
270 g_print ("%s:\n", desc->name);
272 g_print ("%s: %s", desc->name, desc->desc);
273 if (!strchr (desc->desc, '\n'))
277 for (i = OP_LOAD; i < OP_LAST; ++i) {
280 g_print ("%s:\n", desc->name);
282 g_print ("%s: %s", desc->name, desc->desc);
283 if (!strchr (desc->desc, '\n'))
290 * TODO: output the table (possibly merged), in the input format
293 main (int argc, char* argv [])
297 /* useful to get a new file when some opcodes are added: looses the comments, though */
298 load_file (argv [1]);
300 } else if (argc < 4) {
301 g_print ("Usage: genmdesc arguments\n");
302 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
303 g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
304 " Write to output the description in a table named 'name',\n"
305 " use --nacl to generate Google NativeClient code\n");
309 if (strcmp (argv [1], "--nacl") == 0) {
314 for (; i < argc; ++i)
315 load_file (argv [i]);
317 build_table (argv [1 + nacl], argv [2 + nacl]);