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) {
144 size = strtoul (p, &p, 10);
145 if (!nacl_length_set) {
146 desc->spec [MONO_INST_LEN] = size;
148 } else if (strncmp (p, "nacl:", 5) == 0) {
151 size = strtoul (p, &p, 10);
153 desc->spec [MONO_INST_LEN] = size;
154 nacl_length_set = TRUE;
156 } else if (strncmp (p, "template:", 9) == 0) {
162 while (*p && isalnum (*p)) ++p;
164 tdesc = g_hash_table_lookup (template_table, tname);
166 g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
167 for (i = 0; i < MONO_INST_MAX; ++i) {
169 g_error ("The template overrides any previous value set at line %d in %s\n", line, name);
171 memcpy (desc->spec, tdesc->spec, sizeof (desc->spec));
172 } else if (strncmp (p, "name:", 5) == 0) {
175 g_error ("name tag only valid in templates at '%s' at line %d in %s\n", p, line, name);
177 g_error ("Duplicated name tag in template %s at '%s' at line %d in %s\n", desc->name, p, line, name);
180 while (*p && isalnum (*p)) ++p;
182 if (g_hash_table_lookup (template_table, tname))
183 g_error ("Duplicated template %s at line %d in %s\n", tname, line, name);
184 desc->name = g_strdup (tname);
185 g_hash_table_insert (template_table, (void*)desc->name, desc);
187 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
191 if (is_template && !desc->name)
192 g_error ("Template without name at line %d in %s\n", line, name);
198 static OpDesc *opcodes = NULL;
205 template_table = g_hash_table_new (g_str_hash, g_str_equal);
206 table = g_hash_table_new (g_str_hash, g_str_equal);
208 opcodes = g_new0 (OpDesc, OP_LAST);
209 for (i = OP_LOAD; i < OP_LAST; ++i) {
212 desc->name = inst_name (i);
213 g_hash_table_insert (table, (char *)desc->name, desc);
218 output_char (FILE *f, char c) {
220 fprintf (f, "%c", c);
222 fprintf (f, "\\x%x\" \"", (guint8)c);
226 build_table (const char *fname, const char *name) {
230 GString *idx_array = g_string_new ("");
231 /* use this to remove duplicates */
232 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
234 if (!(f = fopen (fname, "w")))
235 g_error ("Cannot open file '%s'", fname);
236 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
237 fprintf (f, "const char %s [] = {\n", name);
239 for (j = 0; j < MONO_INST_MAX; ++j)
241 fprintf (f, "\"\t/* null entry */\n");
243 g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
245 for (i = OP_LOAD; i < OP_LAST; ++i) {
248 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
251 for (j = 0; j < MONO_INST_MAX; ++j)
252 output_char (f, desc->spec [j]);
253 fprintf (f, "\"\t/* %s */\n", desc->name);
254 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
258 fprintf (f, "};\n\n");
259 fprintf (f, "%s};\n\n", idx_array->str);
261 g_string_free (idx_array, TRUE);
262 g_hash_table_destroy (desc_ht);
270 for (i = 0; i < MONO_CEE_LAST; ++i) {
273 g_print ("%s", desc->comment);
275 g_print ("%s:\n", desc->name);
277 g_print ("%s: %s", desc->name, desc->desc);
278 if (!strchr (desc->desc, '\n'))
282 for (i = OP_LOAD; i < OP_LAST; ++i) {
285 g_print ("%s:\n", desc->name);
287 g_print ("%s: %s", desc->name, desc->desc);
288 if (!strchr (desc->desc, '\n'))
295 * TODO: output the table (possibly merged), in the input format
298 main (int argc, char* argv [])
302 /* useful to get a new file when some opcodes are added: looses the comments, though */
303 load_file (argv [1]);
305 } else if (argc < 4) {
306 g_print ("Usage: genmdesc arguments\n");
307 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
308 g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
309 " Write to output the description in a table named 'name',\n"
310 " use --nacl to generate Google NativeClient code\n");
314 if (strcmp (argv [1], "--nacl") == 0) {
319 for (; i < argc; ++i)
320 load_file (argv [i]);
322 build_table (argv [1 + nacl], argv [2 + nacl]);