2007-11-28 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / genmdesc.c
1 /*
2  * genmdesc: Generates the machine description
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * (C) 2003 Ximian, Inc.
8  */
9 #include "mini.h"
10 #include <ctype.h>
11 #include <string.h>
12 #include <mono/metadata/opcodes.h>
13
14 typedef struct {
15         int num;
16         const char *name;
17         char *desc;
18         char *comment;
19         char spec [MONO_INST_MAX];
20 } OpDesc;
21
22 static GHashTable *table;
23
24 #define eat_whitespace(s) while (*(s) && isspace (*(s))) s++;
25
26 static int
27 load_file (const char *name) {
28         FILE *f;
29         char buf [256];
30         char *str, *p;
31         int line;
32         OpDesc *desc;
33         GString *comment;
34
35         if (!(f = fopen (name, "r")))
36                 g_error ("Cannot open file '%s'", name);
37
38         comment = g_string_new ("");
39         /*
40          * The format of the lines are:
41          * # comment
42          * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format] 
43          *      [cost:num] [res:format] [delay:num] [len:num]
44          * format is a single letter that depends on the field
45          * NOTE: no space between the field name and the ':'
46          *
47          * len: maximum instruction length
48          */
49         line = 0;
50         while ((str = fgets (buf, sizeof (buf), f))) {
51                 ++line;
52                 eat_whitespace (str);
53                 if (!str [0])
54                         continue;
55                 if (str [0] == '#') {
56                         g_string_append (comment, str);
57                         continue;
58                 }
59                 p = strchr (str, ':');
60                 if (!p)
61                         g_error ("Invalid format at line %d in %s\n", line, name);
62                 *p++ = 0;
63                 eat_whitespace (p);
64                 desc = g_hash_table_lookup (table, str);
65                 if (!desc)
66                         g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
67                 if (desc->desc)
68                         g_error ("Duplicated opcode '%s' at line %d in %s\n", str, line, name);
69                 desc->desc = g_strdup (p);
70                 desc->comment = g_strdup (comment->str);
71                 g_string_truncate (comment, 0);
72                 while (*p) {
73                         if (strncmp (p, "dest:", 5) == 0) {
74                                 desc->spec [MONO_INST_DEST] = p [5];
75                                 p += 6;
76                         } else if (strncmp (p, "src1:", 5) == 0) {
77                                 desc->spec [MONO_INST_SRC1] = p [5];
78                                 p += 6;
79                         } else if (strncmp (p, "src2:", 5) == 0) {
80                                 desc->spec [MONO_INST_SRC2] = p [5];
81                                 p += 6;
82                         } else if (strncmp (p, "clob:", 5) == 0) {
83                                 desc->spec [MONO_INST_CLOB] = p [5];
84                                 p += 6;
85                                 /* Currently unused fields
86                         } else if (strncmp (p, "cost:", 5) == 0) {
87                                 desc->spec [MONO_INST_COST] = p [5];
88                                 p += 6;
89                         } else if (strncmp (p, "res:", 4) == 0) {
90                                 desc->spec [MONO_INST_RES] = p [4];
91                                 p += 5;
92                         } else if (strncmp (p, "flags:", 6) == 0) {
93                                 desc->spec [MONO_INST_FLAGS] = p [6];
94                                 p += 7;
95                         } else if (strncmp (p, "delay:", 6) == 0) {
96                                 desc->spec [MONO_INST_DELAY] = p [6];
97                                 p += 7;
98                                 */
99                         } else if (strncmp (p, "len:", 4) == 0) {
100                                 p += 4;
101                                 desc->spec [MONO_INST_LEN] = strtoul (p, &p, 10);
102                         } else {
103                                 g_error ("Parse error at '%s' at line %d in %s\n", p, line, name);
104                         }
105                         eat_whitespace (p);
106                 }
107         }
108         fclose (f);
109         return 0;
110 }
111
112 static OpDesc *opcodes = NULL;
113
114 static void
115 init_table (void) {
116         int i;
117         OpDesc *desc;
118
119         table = g_hash_table_new (g_str_hash, g_str_equal);
120
121         opcodes = g_new0 (OpDesc, OP_LAST);
122         for (i = 0; i < MONO_CEE_LAST; ++i) {
123                 desc = opcodes + i;
124                 desc->num = i;
125                 desc->name = mono_inst_name (i);
126                 g_hash_table_insert (table, (char *)desc->name, desc);
127         }
128         for (i = OP_LOAD; i < OP_LAST; ++i) {
129                 desc = opcodes + i;
130                 desc->num = i;
131                 desc->name = mono_inst_name (i);
132                 g_hash_table_insert (table, (char *)desc->name, desc);
133         }
134 }
135
136 static void
137 output_char (FILE *f, char c) {
138         if (isalnum (c))
139                 fprintf (f, "%c", c);
140         else
141                 fprintf (f, "\\x%x\" \"", c);
142 }
143
144 static void
145 build_table (const char *fname, const char *name) {
146         FILE *f;
147         int i, j, idx;
148         OpDesc *desc;
149         GString *idx_array =  g_string_new ("");
150         /* use this to remove duplicates */
151         GHashTable *desc_ht = g_hash_table_new (g_str_hash, g_str_equal);
152
153         if (!(f = fopen (fname, "w")))
154                 g_error ("Cannot open file '%s'", fname);
155         fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
156         fprintf (f, "const char %s [] = {\n", name);
157         fprintf (f, "\t\"");
158         for (j = 0; j < MONO_INST_MAX; ++j)
159                 fprintf (f, "\\x0");
160         fprintf (f, "\"\t/* null entry */\n");
161         idx = 1;
162         g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
163
164         for (i = 0; i < OP_LAST; ++i) {
165                 desc = opcodes + i;
166                 if (!desc->desc)
167                         g_string_append_printf (idx_array, "\t0,\t/* %s */\n", desc->name);
168                 else {
169                         fprintf (f, "\t\"");
170                         for (j = 0; j < MONO_INST_MAX; ++j)
171                                 output_char (f, desc->spec [j]);
172                         fprintf (f, "\"\t/* %s */\n", desc->name);
173                         g_string_append_printf (idx_array, "\t%d,\t/* %s */\n", idx * MONO_INST_MAX, desc->name);
174                         ++idx;
175                 }
176         }
177         fprintf (f, "};\n\n");
178         fprintf (f, "%s};\n\n", idx_array->str);
179         fclose (f);
180         g_string_free (idx_array, TRUE);
181         g_hash_table_destroy (desc_ht);
182 }
183
184 static void
185 dump (void) {
186         int i;
187         OpDesc *desc;
188         
189         for (i = 0; i < MONO_CEE_LAST; ++i) {
190                 desc = opcodes + i;
191                 if (desc->comment)
192                         g_print ("%s", desc->comment);
193                 if (!desc->desc)
194                         g_print ("%s:\n", desc->name);
195                 else {
196                         g_print ("%s: %s", desc->name, desc->desc);
197                         if (!strchr (desc->desc, '\n'))
198                                 g_print ("\n");
199                 }
200         }
201         for (i = OP_LOAD; i < OP_LAST; ++i) {
202                 desc = opcodes + i;
203                 if (!desc->desc)
204                         g_print ("%s:\n", desc->name);
205                 else {
206                         g_print ("%s: %s", desc->name, desc->desc);
207                         if (!strchr (desc->desc, '\n'))
208                                 g_print ("\n");
209                 }
210         }
211 }
212
213 /*
214  * TODO: output the table (possibly merged), in the input format 
215  */
216 int 
217 main (int argc, char* argv [])
218 {
219         init_table ();
220         switch (argc) {
221         case 2:
222                 /* useful to get a new file when some opcodes are added: looses the comments, though */
223                 load_file (argv [1]);
224                 dump ();
225                 break;
226         case 4:
227                 load_file (argv [1]);
228                 build_table (argv [2], argv [3]);
229                 break;
230         default:
231                 g_print ("Usage: genmdesc arguments\n");
232                 g_print ("\tgenmdesc desc             Output to stdout the description file.\n");
233                 g_print ("\tgenmdesc desc output name Write to output the description in a table named 'name'.\n");
234                 return 1;
235         }
236         return 0;
237 }
238