2 * genmdesc: Generates the machine description
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/opcodes.h>
14 #if defined(__native_client__) || defined(__native_client_codegen__)
15 volatile int __nacl_thread_suspension_needed = 0;
16 void __nacl_suspend_thread_if_needed() {}
19 #define MINI_OP(a,b,dest,src1,src2) b,
20 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
21 /* keep in sync with the enum in mini.h */
22 static const char* const
30 * Duplicate this from helpers.c, so the opcode name array can be omitted when
35 if (op >= OP_LOAD && op <= OP_LAST)
36 return opnames [op - OP_LOAD];
38 return mono_opcode_name (op);
39 g_error ("unknown opcode name for %d", op);
48 char spec [MONO_INST_MAX];
52 static GHashTable *table;
53 static GHashTable *template_table;
55 #define eat_whitespace(s) while (*(s) && isspace (*(s))) s++;
58 load_file (const char *name) {
66 if (!(f = fopen (name, "r")))
67 g_error ("Cannot open file '%s'", name);
69 comment = g_string_new ("");
71 * The format of the lines are:
73 * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format]
74 * [cost:num] [res:format] [delay:num] [len:num] [nacl:num]
75 * format is a single letter that depends on the field
76 * NOTE: no space between the field name and the ':'
78 * len: maximum instruction length
81 while ((str = fgets (buf, sizeof (buf), f))) {
82 gboolean is_template = FALSE;
83 gboolean nacl_length_set = FALSE;
90 g_string_append (comment, str);
93 p = strchr (str, ':');
95 g_error ("Invalid format at line %d in %s\n", line, name);
98 if (strcmp (str, "template") == 0) {
100 desc = g_new0 (OpDesc, 1);
102 desc = g_hash_table_lookup (table, str);
104 g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
106 g_error ("Duplicated opcode %s at line %d in %s\n", str, line, name);
108 desc->desc = g_strdup (p);
109 desc->comment = g_strdup (comment->str);
110 g_string_truncate (comment, 0);
112 if (strncmp (p, "dest:", 5) == 0) {
113 desc->spec [MONO_INST_DEST] = p [5];
115 } else if (strncmp (p, "src1:", 5) == 0) {
116 desc->spec [MONO_INST_SRC1] = p [5];
118 } else if (strncmp (p, "src2:", 5) == 0) {
119 desc->spec [MONO_INST_SRC2] = p [5];
121 } else if (strncmp (p, "src3:", 5) == 0) {
122 desc->spec [MONO_INST_SRC3] = p [5];
124 } else if (strncmp (p, "clob:", 5) == 0) {
125 desc->spec [MONO_INST_CLOB] = p [5];
127 /* Currently unused fields
128 } else if (strncmp (p, "cost:", 5) == 0) {
129 desc->spec [MONO_INST_COST] = p [5];
131 } else if (strncmp (p, "res:", 4) == 0) {
132 desc->spec [MONO_INST_RES] = p [4];
134 } else if (strncmp (p, "flags:", 6) == 0) {
135 desc->spec [MONO_INST_FLAGS] = p [6];
137 } else if (strncmp (p, "delay:", 6) == 0) {
138 desc->spec [MONO_INST_DELAY] = p [6];
141 } else if (strncmp (p, "len:", 4) == 0) {
145 size = strtoul (p, &endptr, 10);
146 if (size == 0 && p == endptr)
147 g_error ("Invalid length '%s' at line %d in %s\n", p, line, name);
149 if (!nacl_length_set) {
150 desc->spec [MONO_INST_LEN] = size;
152 } else if (strncmp (p, "nacl:", 5) == 0) {
155 size = strtoul (p, &p, 10);
157 desc->spec [MONO_INST_LEN] = size;
158 nacl_length_set = TRUE;
160 } else if (strncmp (p, "template:", 9) == 0) {
166 while (*p && isalnum (*p)) ++p;
168 tdesc = g_hash_table_lookup (template_table, tname);
170 g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
171 for (i = 0; i < MONO_INST_MAX; ++i) {
173 g_error ("The template overrides any previous value set at line %d in %s\n", line, name);
175 memcpy (desc->spec, tdesc->spec, sizeof (desc->spec));
176 } else if (strncmp (p, "name:", 5) == 0) {
179 g_error ("name tag only valid in templates at '%s' at line %d in %s\n", p, line, name);
181 g_error ("Duplicated name tag in template %s at '%s' at line %d in %s\n", desc->name, p, line, name);
184 while (*p && isalnum (*p)) ++p;
186 if (g_hash_table_lookup (template_table, tname))
187 g_error ("Duplicated template %s at line %d in %s\n", tname, line, name);
188 desc->name = g_strdup (tname);
189 g_hash_table_insert (template_table, (void*)desc->name, desc);
191 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
195 if (is_template && !desc->name)
196 g_error ("Template without name at line %d in %s\n", line, name);
202 static OpDesc *opcodes = NULL;
209 template_table = g_hash_table_new (g_str_hash, g_str_equal);
210 table = g_hash_table_new (g_str_hash, g_str_equal);
212 opcodes = g_new0 (OpDesc, OP_LAST);
213 for (i = OP_LOAD; i < OP_LAST; ++i) {
216 desc->name = inst_name (i);
217 g_hash_table_insert (table, (char *)desc->name, desc);
222 output_char (FILE *f, char c) {
224 fprintf (f, "%c", c);
226 fprintf (f, "\\x%x\" \"", (guint8)c);
230 build_table (const char *fname, const char *name) {
234 GString *idx_array = g_string_new ("");
235 /* use this to remove duplicates */
236 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
238 if (!(f = fopen (fname, "w")))
239 g_error ("Cannot open file '%s'", fname);
240 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
241 fprintf (f, "const char mono_%s [] = {\n", name);
243 for (j = 0; j < MONO_INST_MAX; ++j)
245 fprintf (f, "\"\t/* null entry */\n");
247 g_string_append_printf (idx_array, "const guint16 mono_%s_idx [] = {\n", name);
249 for (i = OP_LOAD; i < OP_LAST; ++i) {
252 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
255 for (j = 0; j < MONO_INST_MAX; ++j)
256 output_char (f, desc->spec [j]);
257 fprintf (f, "\"\t/* %s */\n", desc->name);
258 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
262 fprintf (f, "};\n\n");
263 fprintf (f, "%s};\n\n", idx_array->str);
265 g_string_free (idx_array, TRUE);
266 g_hash_table_destroy (desc_ht);
274 for (i = 0; i < MONO_CEE_LAST; ++i) {
277 g_print ("%s", desc->comment);
279 g_print ("%s:\n", desc->name);
281 g_print ("%s: %s", desc->name, desc->desc);
282 if (!strchr (desc->desc, '\n'))
286 for (i = OP_LOAD; i < OP_LAST; ++i) {
289 g_print ("%s:\n", desc->name);
291 g_print ("%s: %s", desc->name, desc->desc);
292 if (!strchr (desc->desc, '\n'))
299 * TODO: output the table (possibly merged), in the input format
302 main (int argc, char* argv [])
306 /* useful to get a new file when some opcodes are added: looses the comments, though */
307 load_file (argv [1]);
309 } else if (argc < 4) {
310 g_print ("Usage: genmdesc arguments\n");
311 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
312 g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
313 " Write to output the description in a table named 'name',\n"
314 " use --nacl to generate Google NativeClient code\n");
318 if (strcmp (argv [1], "--nacl") == 0) {
323 for (; i < argc; ++i)
324 load_file (argv [i]);
326 build_table (argv [1 + nacl], argv [2 + nacl]);