2 * genmdesc: Generates the machine description
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/opcodes.h>
14 void __nacl_suspend_thread_if_needed() {}
16 #define MINI_OP(a,b,dest,src1,src2) b,
17 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
18 /* keep in sync with the enum in mini.h */
19 static const char* const
27 * Duplicate this from helpers.c, so the opcode name array can be omitted when
32 if (op >= OP_LOAD && op <= OP_LAST)
33 return opnames [op - OP_LOAD];
35 return mono_opcode_name (op);
36 g_error ("unknown opcode name for %d", op);
45 char spec [MONO_INST_MAX];
49 static GHashTable *table;
50 static GHashTable *template_table;
52 #define eat_whitespace(s) while (*(s) && isspace (*(s))) s++;
55 load_file (const char *name) {
63 if (!(f = fopen (name, "r")))
64 g_error ("Cannot open file '%s'", name);
66 comment = g_string_new ("");
68 * The format of the lines are:
70 * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format]
71 * [cost:num] [res:format] [delay:num] [len:num] [nacl:num]
72 * format is a single letter that depends on the field
73 * NOTE: no space between the field name and the ':'
75 * len: maximum instruction length
78 while ((str = fgets (buf, sizeof (buf), f))) {
79 gboolean is_template = FALSE;
80 gboolean nacl_length_set = FALSE;
87 g_string_append (comment, str);
90 p = strchr (str, ':');
92 g_error ("Invalid format at line %d in %s\n", line, name);
95 if (strcmp (str, "template") == 0) {
97 desc = g_new0 (OpDesc, 1);
99 desc = g_hash_table_lookup (table, str);
101 g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
103 g_error ("Duplicated opcode %s at line %d in %s\n", str, line, name);
105 desc->desc = g_strdup (p);
106 desc->comment = g_strdup (comment->str);
107 g_string_truncate (comment, 0);
109 if (strncmp (p, "dest:", 5) == 0) {
110 desc->spec [MONO_INST_DEST] = p [5];
112 } else if (strncmp (p, "src1:", 5) == 0) {
113 desc->spec [MONO_INST_SRC1] = p [5];
115 } else if (strncmp (p, "src2:", 5) == 0) {
116 desc->spec [MONO_INST_SRC2] = p [5];
118 } else if (strncmp (p, "src3:", 5) == 0) {
119 desc->spec [MONO_INST_SRC3] = p [5];
121 } else if (strncmp (p, "clob:", 5) == 0) {
122 desc->spec [MONO_INST_CLOB] = p [5];
124 /* Currently unused fields
125 } else if (strncmp (p, "cost:", 5) == 0) {
126 desc->spec [MONO_INST_COST] = p [5];
128 } else if (strncmp (p, "res:", 4) == 0) {
129 desc->spec [MONO_INST_RES] = p [4];
131 } else if (strncmp (p, "flags:", 6) == 0) {
132 desc->spec [MONO_INST_FLAGS] = p [6];
134 } else if (strncmp (p, "delay:", 6) == 0) {
135 desc->spec [MONO_INST_DELAY] = p [6];
138 } else if (strncmp (p, "len:", 4) == 0) {
141 size = strtoul (p, &p, 10);
142 if (!nacl_length_set) {
143 desc->spec [MONO_INST_LEN] = size;
145 } else if (strncmp (p, "nacl:", 5) == 0) {
148 size = strtoul (p, &p, 10);
150 desc->spec [MONO_INST_LEN] = size;
151 nacl_length_set = TRUE;
153 } else if (strncmp (p, "template:", 9) == 0) {
159 while (*p && isalnum (*p)) ++p;
161 tdesc = g_hash_table_lookup (template_table, tname);
163 g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
164 for (i = 0; i < MONO_INST_MAX; ++i) {
166 g_error ("The template overrides any previous value set at line %d in %s\n", line, name);
168 memcpy (desc->spec, tdesc->spec, sizeof (desc->spec));
169 } else if (strncmp (p, "name:", 5) == 0) {
172 g_error ("name tag only valid in templates at '%s' at line %d in %s\n", p, line, name);
174 g_error ("Duplicated name tag in template %s at '%s' at line %d in %s\n", desc->name, p, line, name);
177 while (*p && isalnum (*p)) ++p;
179 if (g_hash_table_lookup (template_table, tname))
180 g_error ("Duplicated template %s at line %d in %s\n", tname, line, name);
181 desc->name = g_strdup (tname);
182 g_hash_table_insert (template_table, (void*)desc->name, desc);
184 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
188 if (is_template && !desc->name)
189 g_error ("Template without name at line %d in %s\n", line, name);
195 static OpDesc *opcodes = NULL;
202 template_table = g_hash_table_new (g_str_hash, g_str_equal);
203 table = g_hash_table_new (g_str_hash, g_str_equal);
205 opcodes = g_new0 (OpDesc, OP_LAST);
206 for (i = OP_LOAD; i < OP_LAST; ++i) {
209 desc->name = inst_name (i);
210 g_hash_table_insert (table, (char *)desc->name, desc);
215 output_char (FILE *f, char c) {
217 fprintf (f, "%c", c);
219 fprintf (f, "\\x%x\" \"", c);
223 build_table (const char *fname, const char *name) {
227 GString *idx_array = g_string_new ("");
228 /* use this to remove duplicates */
229 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
231 if (!(f = fopen (fname, "w")))
232 g_error ("Cannot open file '%s'", fname);
233 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
234 fprintf (f, "const char %s [] = {\n", name);
236 for (j = 0; j < MONO_INST_MAX; ++j)
238 fprintf (f, "\"\t/* null entry */\n");
240 g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
242 for (i = OP_LOAD; i < OP_LAST; ++i) {
245 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
248 for (j = 0; j < MONO_INST_MAX; ++j)
249 output_char (f, desc->spec [j]);
250 fprintf (f, "\"\t/* %s */\n", desc->name);
251 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
255 fprintf (f, "};\n\n");
256 fprintf (f, "%s};\n\n", idx_array->str);
258 g_string_free (idx_array, TRUE);
259 g_hash_table_destroy (desc_ht);
267 for (i = 0; i < MONO_CEE_LAST; ++i) {
270 g_print ("%s", desc->comment);
272 g_print ("%s:\n", desc->name);
274 g_print ("%s: %s", desc->name, desc->desc);
275 if (!strchr (desc->desc, '\n'))
279 for (i = OP_LOAD; i < OP_LAST; ++i) {
282 g_print ("%s:\n", desc->name);
284 g_print ("%s: %s", desc->name, desc->desc);
285 if (!strchr (desc->desc, '\n'))
292 * TODO: output the table (possibly merged), in the input format
295 main (int argc, char* argv [])
299 /* useful to get a new file when some opcodes are added: looses the comments, though */
300 load_file (argv [1]);
302 } else if (argc < 4) {
303 g_print ("Usage: genmdesc arguments\n");
304 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
305 g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
306 " Write to output the description in a table named 'name',\n"
307 " use --nacl to generate Google NativeClient code\n");
311 if (strcmp (argv [1], "--nacl") == 0) {
316 for (; i < argc; ++i)
317 load_file (argv [i]);
319 build_table (argv [1 + nacl], argv [2 + nacl]);