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;
83 g_string_append (comment, str);
86 p = strchr (str, ':');
88 g_error ("Invalid format at line %d in %s\n", line, name);
91 if (strcmp (str, "template") == 0) {
93 desc = g_new0 (OpDesc, 1);
95 desc = g_hash_table_lookup (table, str);
97 g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
99 g_error ("Duplicated opcode %s at line %d in %s\n", str, line, name);
101 desc->desc = g_strdup (p);
102 desc->comment = g_strdup (comment->str);
103 g_string_truncate (comment, 0);
105 if (strncmp (p, "dest:", 5) == 0) {
106 desc->spec [MONO_INST_DEST] = p [5];
108 } else if (strncmp (p, "src1:", 5) == 0) {
109 desc->spec [MONO_INST_SRC1] = p [5];
111 } else if (strncmp (p, "src2:", 5) == 0) {
112 desc->spec [MONO_INST_SRC2] = p [5];
114 } else if (strncmp (p, "src3:", 5) == 0) {
115 desc->spec [MONO_INST_SRC3] = p [5];
117 } else if (strncmp (p, "clob:", 5) == 0) {
118 desc->spec [MONO_INST_CLOB] = p [5];
120 /* Currently unused fields
121 } else if (strncmp (p, "cost:", 5) == 0) {
122 desc->spec [MONO_INST_COST] = p [5];
124 } else if (strncmp (p, "res:", 4) == 0) {
125 desc->spec [MONO_INST_RES] = p [4];
127 } else if (strncmp (p, "flags:", 6) == 0) {
128 desc->spec [MONO_INST_FLAGS] = p [6];
130 } else if (strncmp (p, "delay:", 6) == 0) {
131 desc->spec [MONO_INST_DELAY] = p [6];
134 } else if (strncmp (p, "len:", 4) == 0) {
136 desc->spec [MONO_INST_LEN] += strtoul (p, &p, 10);
137 } else if (strncmp (p, "nacl:", 5) == 0){
140 size = strtoul (p, &p, 10);
142 desc->spec [MONO_INST_LEN] += size;
143 } else if (strncmp (p, "template:", 9) == 0) {
149 while (*p && isalnum (*p)) ++p;
151 tdesc = g_hash_table_lookup (template_table, tname);
153 g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
154 for (i = 0; i < MONO_INST_MAX; ++i) {
156 g_error ("The template overrides any previous value set at line %d in %s\n", line, name);
158 memcpy (desc->spec, tdesc->spec, sizeof (desc->spec));
159 } else if (strncmp (p, "name:", 5) == 0) {
162 g_error ("name tag only valid in templates at '%s' at line %d in %s\n", p, line, name);
164 g_error ("Duplicated name tag in template %s at '%s' at line %d in %s\n", desc->name, p, line, name);
167 while (*p && isalnum (*p)) ++p;
169 if (g_hash_table_lookup (template_table, tname))
170 g_error ("Duplicated template %s at line %d in %s\n", tname, line, name);
171 desc->name = g_strdup (tname);
172 g_hash_table_insert (template_table, (void*)desc->name, desc);
174 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
178 if (is_template && !desc->name)
179 g_error ("Template without name at line %d in %s\n", line, name);
185 static OpDesc *opcodes = NULL;
192 template_table = g_hash_table_new (g_str_hash, g_str_equal);
193 table = g_hash_table_new (g_str_hash, g_str_equal);
195 opcodes = g_new0 (OpDesc, OP_LAST);
196 for (i = OP_LOAD; i < OP_LAST; ++i) {
199 desc->name = inst_name (i);
200 g_hash_table_insert (table, (char *)desc->name, desc);
205 output_char (FILE *f, char c) {
207 fprintf (f, "%c", c);
209 fprintf (f, "\\x%x\" \"", c);
213 build_table (const char *fname, const char *name) {
217 GString *idx_array = g_string_new ("");
218 /* use this to remove duplicates */
219 GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
221 if (!(f = fopen (fname, "w")))
222 g_error ("Cannot open file '%s'", fname);
223 fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
224 fprintf (f, "const char %s [] = {\n", name);
226 for (j = 0; j < MONO_INST_MAX; ++j)
228 fprintf (f, "\"\t/* null entry */\n");
230 g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
232 for (i = OP_LOAD; i < OP_LAST; ++i) {
235 g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name ? desc->name : "");
238 for (j = 0; j < MONO_INST_MAX; ++j)
239 output_char (f, desc->spec [j]);
240 fprintf (f, "\"\t/* %s */\n", desc->name);
241 g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
245 fprintf (f, "};\n\n");
246 fprintf (f, "%s};\n\n", idx_array->str);
248 g_string_free (idx_array, TRUE);
249 g_hash_table_destroy (desc_ht);
257 for (i = 0; i < MONO_CEE_LAST; ++i) {
260 g_print ("%s", desc->comment);
262 g_print ("%s:\n", desc->name);
264 g_print ("%s: %s", desc->name, desc->desc);
265 if (!strchr (desc->desc, '\n'))
269 for (i = OP_LOAD; i < OP_LAST; ++i) {
272 g_print ("%s:\n", desc->name);
274 g_print ("%s: %s", desc->name, desc->desc);
275 if (!strchr (desc->desc, '\n'))
282 * TODO: output the table (possibly merged), in the input format
285 main (int argc, char* argv [])
289 /* useful to get a new file when some opcodes are added: looses the comments, though */
290 load_file (argv [1]);
292 } else if (argc < 4) {
293 g_print ("Usage: genmdesc arguments\n");
294 g_print ("\tgenmdesc desc Output to stdout the description file.\n");
295 g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
296 " Write to output the description in a table named 'name',\n"
297 " use --nacl to generate Google NativeClient code\n");
301 if (strcmp (argv [1], "--nacl") == 0){
306 for (; i < argc; ++i)
307 load_file (argv [i]);
309 build_table (argv [1 + nacl], argv [2 + nacl]);