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++;
57 // Per spec isalnum() expects input in the range 0-255
58 // and can misbehave if you pass in a signed char.
62 return isalnum ((unsigned char)c);
66 load_file (const char *name) {
74 if (!(f = fopen (name, "r")))
75 g_error ("Cannot open file '%s'", name);
77 comment = g_string_new ("");
79 * The format of the lines are:
81 * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format]
82 * [cost:num] [res:format] [delay:num] [len:num] [nacl:num]
83 * format is a single letter that depends on the field
84 * NOTE: no space between the field name and the ':'
86 * len: maximum instruction length
89 while ((str = fgets (buf, sizeof (buf), f))) {
90 gboolean is_template = FALSE;
91 gboolean nacl_length_set = FALSE;
98 g_string_append (comment, str);
101 p = strchr (str, ':');
103 g_error ("Invalid format at line %d in %s\n", line, name);
106 if (strcmp (str, "template") == 0) {
108 desc = g_new0 (OpDesc, 1);
110 desc = (OpDesc *)g_hash_table_lookup (table, str);
112 g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
114 g_error ("Duplicated opcode %s at line %d in %s\n", str, line, name);
116 desc->desc = g_strdup (p);
117 desc->comment = g_strdup (comment->str);
118 g_string_truncate (comment, 0);
120 if (strncmp (p, "dest:", 5) == 0) {
121 desc->spec [MONO_INST_DEST] = p [5];
123 } else if (strncmp (p, "src1:", 5) == 0) {
124 desc->spec [MONO_INST_SRC1] = p [5];
126 } else if (strncmp (p, "src2:", 5) == 0) {
127 desc->spec [MONO_INST_SRC2] = p [5];
129 } else if (strncmp (p, "src3:", 5) == 0) {
130 desc->spec [MONO_INST_SRC3] = p [5];
132 } else if (strncmp (p, "clob:", 5) == 0) {
133 desc->spec [MONO_INST_CLOB] = p [5];
135 /* Currently unused fields
136 } else if (strncmp (p, "cost:", 5) == 0) {
137 desc->spec [MONO_INST_COST] = p [5];
139 } else if (strncmp (p, "res:", 4) == 0) {
140 desc->spec [MONO_INST_RES] = p [4];
142 } else if (strncmp (p, "flags:", 6) == 0) {
143 desc->spec [MONO_INST_FLAGS] = p [6];
145 } else if (strncmp (p, "delay:", 6) == 0) {
146 desc->spec [MONO_INST_DELAY] = p [6];
149 } else if (strncmp (p, "len:", 4) == 0) {
153 size = strtoul (p, &endptr, 10);
154 if (size == 0 && p == endptr)
155 g_error ("Invalid length '%s' at line %d in %s\n", p, line, name);
157 if (!nacl_length_set) {
158 desc->spec [MONO_INST_LEN] = size;
160 } else if (strncmp (p, "nacl:", 5) == 0) {
163 size = strtoul (p, &p, 10);
165 desc->spec [MONO_INST_LEN] = size;
166 nacl_length_set = TRUE;
168 } else if (strncmp (p, "template:", 9) == 0) {
174 while (*p && isalnum_char (*p)) ++p;
176 tdesc = (OpDesc *)g_hash_table_lookup (template_table, tname);
178 g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
179 for (i = 0; i < MONO_INST_MAX; ++i) {
181 g_error ("The template overrides any previous value set at line %d in %s\n", line, name);
183 memcpy (desc->spec, tdesc->spec, sizeof (desc->spec));
184 } else if (strncmp (p, "name:", 5) == 0) {
187 g_error ("name tag only valid in templates at '%s' at line %d in %s\n", p, line, name);
189 g_error ("Duplicated name tag in template %s at '%s' at line %d in %s\n", desc->name, p, line, name);
192 while (*p && isalnum_char (*p)) ++p;
194 if (g_hash_table_lookup (template_table, tname))
195 g_error ("Duplicated template %s at line %d in %s\n", tname, line, name);
196 desc->name = g_strdup (tname);
197 g_hash_table_insert (template_table, (void*)desc->name, desc);
199 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
203 if (is_template && !desc->name)
204 g_error ("Template without name at line %d in %s\n", line, name);
206 g_string_free (comment,TRUE);
211 static OpDesc *opcodes = NULL;
218 template_table = g_hash_table_new (g_str_hash, g_str_equal);
219 table = g_hash_table_new (g_str_hash, g_str_equal);
221 opcodes = g_new0 (OpDesc, OP_LAST);
222 for (i = OP_LOAD; i < OP_LAST; ++i) {
225 desc->name = inst_name (i);
226 g_hash_table_insert (table, (char *)desc->name, desc);
231 output_char (FILE *f, char c) {
232 if (isalnum_char (c))
233 fprintf (f, "%c", c);
235 fprintf (f, "\\x%x\" \"", (guint8)c);
239 build_table (const char *fname, const char *name) {
243 GString *idx_array = g_string_new ("");
244 /* use this to remove duplicates */
245 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
247 if (!(f = fopen (fname, "w")))
248 g_error ("Cannot open file '%s'", fname);
249 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
250 fprintf (f, "const char mono_%s [] = {\n", name);
252 for (j = 0; j < MONO_INST_MAX; ++j)
254 fprintf (f, "\"\t/* null entry */\n");
256 g_string_append_printf (idx_array, "const guint16 mono_%s_idx [] = {\n", name);
258 for (i = OP_LOAD; i < OP_LAST; ++i) {
261 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
264 for (j = 0; j < MONO_INST_MAX; ++j)
265 output_char (f, desc->spec [j]);
266 fprintf (f, "\"\t/* %s */\n", desc->name);
267 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
271 fprintf (f, "};\n\n");
272 fprintf (f, "%s};\n\n", idx_array->str);
274 g_string_free (idx_array, TRUE);
275 g_hash_table_destroy (desc_ht);
283 for (i = 0; i < MONO_CEE_LAST; ++i) {
286 g_print ("%s", desc->comment);
288 g_print ("%s:\n", desc->name);
290 g_print ("%s: %s", desc->name, desc->desc);
291 if (!strchr (desc->desc, '\n'))
295 for (i = OP_LOAD; i < OP_LAST; ++i) {
298 g_print ("%s:\n", desc->name);
300 g_print ("%s: %s", desc->name, desc->desc);
301 if (!strchr (desc->desc, '\n'))
308 * TODO: output the table (possibly merged), in the input format
311 main (int argc, char* argv [])
315 /* useful to get a new file when some opcodes are added: looses the comments, though */
316 load_file (argv [1]);
318 } else if (argc < 4) {
319 g_print ("Usage: genmdesc arguments\n");
320 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
321 g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
322 " Write to output the description in a table named 'name',\n"
323 " use --nacl to generate Google NativeClient code\n");
327 if (strcmp (argv [1], "--nacl") == 0) {
332 for (; i < argc; ++i)
333 load_file (argv [i]);
335 build_table (argv [1 + nacl], argv [2 + nacl]);