ld-insn.c revision 1.1.1.3 1 1.1 christos /* This file is part of the program psim.
2 1.1 christos
3 1.1 christos Copyright 1994, 1995, 1996, 2003 Andrew Cagney
4 1.1 christos
5 1.1 christos This program is free software; you can redistribute it and/or modify
6 1.1 christos it under the terms of the GNU General Public License as published by
7 1.1 christos the Free Software Foundation; either version 3 of the License, or
8 1.1 christos (at your option) any later version.
9 1.1 christos
10 1.1 christos This program is distributed in the hope that it will be useful,
11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 christos GNU General Public License for more details.
14 1.1 christos
15 1.1 christos You should have received a copy of the GNU General Public License
16 1.1 christos along with this program; if not, see <http://www.gnu.org/licenses/>.
17 1.1 christos
18 1.1 christos */
19 1.1 christos
20 1.1 christos
21 1.1 christos #include "misc.h"
22 1.1 christos #include "lf.h"
23 1.1 christos #include "table.h"
24 1.1 christos #include "filter.h"
25 1.1 christos #include "ld-decode.h"
26 1.1 christos #include "ld-cache.h"
27 1.1 christos #include "ld-insn.h"
28 1.1 christos
29 1.1 christos #include "igen.h"
30 1.1 christos
31 1.1.1.2 christos static model *last_model;
32 1.1.1.2 christos
33 1.1.1.2 christos static insn *last_model_macro;
34 1.1.1.2 christos static insn *last_model_function;
35 1.1.1.2 christos static insn *last_model_internal;
36 1.1.1.2 christos static insn *last_model_static;
37 1.1.1.2 christos static insn *last_model_data;
38 1.1.1.2 christos
39 1.1.1.2 christos model *models;
40 1.1.1.2 christos
41 1.1.1.2 christos insn *model_macros;
42 1.1.1.2 christos insn *model_functions;
43 1.1.1.2 christos insn *model_internal;
44 1.1.1.2 christos insn *model_static;
45 1.1.1.2 christos insn *model_data;
46 1.1.1.2 christos
47 1.1.1.2 christos int max_model_fields_len;
48 1.1.1.2 christos
49 1.1 christos static void
50 1.1 christos update_depth(insn_table *entry,
51 1.1 christos lf *file,
52 1.1 christos void *data,
53 1.1 christos insn *instruction,
54 1.1 christos int depth)
55 1.1 christos {
56 1.1 christos int *max_depth = (int*)data;
57 1.1 christos if (*max_depth < depth)
58 1.1 christos *max_depth = depth;
59 1.1 christos }
60 1.1 christos
61 1.1 christos
62 1.1 christos int
63 1.1 christos insn_table_depth(insn_table *table)
64 1.1 christos {
65 1.1 christos int depth = 0;
66 1.1 christos insn_table_traverse_tree(table,
67 1.1 christos NULL,
68 1.1 christos &depth,
69 1.1 christos 1,
70 1.1 christos NULL, /*start*/
71 1.1 christos update_depth,
72 1.1 christos NULL, /*end*/
73 1.1 christos NULL); /*padding*/
74 1.1 christos return depth;
75 1.1 christos }
76 1.1 christos
77 1.1 christos
78 1.1 christos static insn_fields *
79 1.1 christos parse_insn_format(table_entry *entry,
80 1.1.1.3 christos const char *format)
81 1.1 christos {
82 1.1.1.3 christos const char *chp;
83 1.1 christos insn_fields *fields = ZALLOC(insn_fields);
84 1.1 christos
85 1.1 christos /* create a leading sentinal */
86 1.1 christos fields->first = ZALLOC(insn_field);
87 1.1 christos fields->first->first = -1;
88 1.1 christos fields->first->last = -1;
89 1.1 christos fields->first->width = 0;
90 1.1 christos
91 1.1 christos /* and a trailing sentinal */
92 1.1 christos fields->last = ZALLOC(insn_field);
93 1.1 christos fields->last->first = insn_bit_size;
94 1.1 christos fields->last->last = insn_bit_size;
95 1.1 christos fields->last->width = 0;
96 1.1 christos
97 1.1 christos /* link them together */
98 1.1 christos fields->first->next = fields->last;
99 1.1 christos fields->last->prev = fields->first;
100 1.1 christos
101 1.1 christos /* now work through the formats */
102 1.1 christos chp = format;
103 1.1 christos
104 1.1 christos while (*chp != '\0') {
105 1.1.1.3 christos const char *start_pos;
106 1.1.1.3 christos const char *start_val;
107 1.1 christos int strlen_val;
108 1.1 christos int strlen_pos;
109 1.1 christos insn_field *new_field;
110 1.1 christos
111 1.1 christos /* sanity check */
112 1.1 christos if (!isdigit(*chp)) {
113 1.1 christos error("%s:%d: missing position field at `%s'\n",
114 1.1 christos entry->file_name, entry->line_nr, chp);
115 1.1 christos }
116 1.1 christos
117 1.1 christos /* break out the bit position */
118 1.1 christos start_pos = chp;
119 1.1 christos while (isdigit(*chp))
120 1.1 christos chp++;
121 1.1 christos strlen_pos = chp - start_pos;
122 1.1 christos if (*chp == '.' && strlen_pos > 0)
123 1.1 christos chp++;
124 1.1 christos else {
125 1.1 christos error("%s:%d: missing field value at %s\n",
126 1.1 christos entry->file_name, entry->line_nr, chp);
127 1.1 christos break;
128 1.1 christos }
129 1.1 christos
130 1.1 christos /* break out the value */
131 1.1 christos start_val = chp;
132 1.1 christos while ((*start_val == '/' && *chp == '/')
133 1.1 christos || (isdigit(*start_val) && isdigit(*chp))
134 1.1 christos || (isalpha(*start_val) && (isalnum(*chp) || *chp == '_')))
135 1.1 christos chp++;
136 1.1 christos strlen_val = chp - start_val;
137 1.1 christos if (*chp == ',')
138 1.1 christos chp++;
139 1.1 christos else if (*chp != '\0' || strlen_val == 0) {
140 1.1 christos error("%s:%d: missing field terminator at %s\n",
141 1.1 christos entry->file_name, entry->line_nr, chp);
142 1.1 christos break;
143 1.1 christos }
144 1.1 christos
145 1.1 christos /* create a new field and insert it */
146 1.1 christos new_field = ZALLOC(insn_field);
147 1.1 christos new_field->next = fields->last;
148 1.1 christos new_field->prev = fields->last->prev;
149 1.1 christos new_field->next->prev = new_field;
150 1.1 christos new_field->prev->next = new_field;
151 1.1 christos
152 1.1 christos /* the value */
153 1.1 christos new_field->val_string = (char*)zalloc(strlen_val+1);
154 1.1 christos strncpy(new_field->val_string, start_val, strlen_val);
155 1.1 christos if (isdigit(*new_field->val_string)) {
156 1.1 christos new_field->val_int = a2i(new_field->val_string);
157 1.1 christos new_field->is_int = 1;
158 1.1 christos }
159 1.1 christos else if (new_field->val_string[0] == '/') {
160 1.1 christos new_field->is_slash = 1;
161 1.1 christos }
162 1.1 christos else {
163 1.1 christos new_field->is_string = 1;
164 1.1 christos }
165 1.1 christos
166 1.1 christos /* the pos */
167 1.1 christos new_field->pos_string = (char*)zalloc(strlen_pos+1);
168 1.1 christos strncpy(new_field->pos_string, start_pos, strlen_pos);
169 1.1 christos new_field->first = target_a2i(hi_bit_nr, new_field->pos_string);
170 1.1 christos new_field->last = new_field->next->first - 1; /* guess */
171 1.1 christos new_field->width = new_field->last - new_field->first + 1; /* guess */
172 1.1 christos new_field->prev->last = new_field->first-1; /*fix*/
173 1.1 christos new_field->prev->width = new_field->first - new_field->prev->first; /*fix*/
174 1.1 christos }
175 1.1 christos
176 1.1 christos /* fiddle first/last so that the sentinals `disapear' */
177 1.1 christos ASSERT(fields->first->last < 0);
178 1.1 christos ASSERT(fields->last->first >= insn_bit_size);
179 1.1 christos fields->first = fields->first->next;
180 1.1 christos fields->last = fields->last->prev;
181 1.1 christos
182 1.1 christos /* now go over this again, pointing each bit position at a field
183 1.1 christos record */
184 1.1 christos {
185 1.1 christos int i;
186 1.1 christos insn_field *field;
187 1.1 christos field = fields->first;
188 1.1 christos for (i = 0; i < insn_bit_size; i++) {
189 1.1 christos while (field->last < i)
190 1.1 christos field = field->next;
191 1.1 christos fields->bits[i] = field;
192 1.1 christos }
193 1.1 christos }
194 1.1 christos
195 1.1 christos /* go over each of the fields, and compute a `value' for the insn */
196 1.1 christos {
197 1.1 christos insn_field *field;
198 1.1 christos fields->value = 0;
199 1.1 christos for (field = fields->first;
200 1.1 christos field->last < insn_bit_size;
201 1.1 christos field = field->next) {
202 1.1 christos fields->value <<= field->width;
203 1.1 christos if (field->is_int)
204 1.1 christos fields->value |= field->val_int;
205 1.1 christos }
206 1.1 christos }
207 1.1 christos return fields;
208 1.1 christos }
209 1.1 christos
210 1.1 christos
211 1.1.1.3 christos static void
212 1.1 christos parse_include_entry (table *file,
213 1.1 christos table_entry *file_entry,
214 1.1 christos filter *filters,
215 1.1 christos table_include *includes)
216 1.1 christos {
217 1.1 christos /* parse the include file_entry */
218 1.1 christos if (file_entry->nr_fields < 4)
219 1.1 christos error ("Incorrect nr fields for include record\n");
220 1.1 christos /* process it */
221 1.1 christos if (!is_filtered_out(file_entry->fields[include_flags], filters))
222 1.1 christos {
223 1.1 christos table_push (file, includes,
224 1.1 christos file_entry->fields[include_path],
225 1.1 christos file_entry->nr_fields, file_entry->nr_fields);
226 1.1 christos }
227 1.1 christos }
228 1.1 christos
229 1.1 christos static void
230 1.1 christos model_table_insert(insn_table *table,
231 1.1 christos table_entry *file_entry)
232 1.1 christos {
233 1.1 christos int len;
234 1.1 christos
235 1.1 christos /* create a new model */
236 1.1 christos model *new_model = ZALLOC(model);
237 1.1 christos
238 1.1 christos new_model->name = file_entry->fields[model_identifer];
239 1.1 christos new_model->printable_name = file_entry->fields[model_name];
240 1.1 christos new_model->insn_default = file_entry->fields[model_default];
241 1.1 christos
242 1.1 christos while (*new_model->insn_default && isspace(*new_model->insn_default))
243 1.1 christos new_model->insn_default++;
244 1.1 christos
245 1.1 christos len = strlen(new_model->insn_default);
246 1.1 christos if (max_model_fields_len < len)
247 1.1 christos max_model_fields_len = len;
248 1.1 christos
249 1.1 christos /* append it to the end of the model list */
250 1.1 christos if (last_model)
251 1.1 christos last_model->next = new_model;
252 1.1 christos else
253 1.1 christos models = new_model;
254 1.1 christos last_model = new_model;
255 1.1 christos }
256 1.1 christos
257 1.1 christos static void
258 1.1 christos model_table_insert_specific(insn_table *table,
259 1.1 christos table_entry *file_entry,
260 1.1 christos insn **start_ptr,
261 1.1 christos insn **end_ptr)
262 1.1 christos {
263 1.1 christos insn *ptr = ZALLOC(insn);
264 1.1 christos ptr->file_entry = file_entry;
265 1.1 christos if (*end_ptr)
266 1.1 christos (*end_ptr)->next = ptr;
267 1.1 christos else
268 1.1 christos (*start_ptr) = ptr;
269 1.1 christos (*end_ptr) = ptr;
270 1.1 christos }
271 1.1 christos
272 1.1 christos
273 1.1 christos static void
274 1.1 christos insn_table_insert_function(insn_table *table,
275 1.1 christos table_entry *file_entry)
276 1.1 christos {
277 1.1 christos /* create a new function */
278 1.1 christos insn *new_function = ZALLOC(insn);
279 1.1 christos new_function->file_entry = file_entry;
280 1.1 christos
281 1.1 christos /* append it to the end of the function list */
282 1.1 christos if (table->last_function)
283 1.1 christos table->last_function->next = new_function;
284 1.1 christos else
285 1.1 christos table->functions = new_function;
286 1.1 christos table->last_function = new_function;
287 1.1 christos }
288 1.1 christos
289 1.1 christos extern void
290 1.1 christos insn_table_insert_insn(insn_table *table,
291 1.1 christos table_entry *file_entry,
292 1.1 christos insn_fields *fields)
293 1.1 christos {
294 1.1 christos insn **ptr_to_cur_insn = &table->insns;
295 1.1 christos insn *cur_insn = *ptr_to_cur_insn;
296 1.1 christos table_model_entry *insn_model_ptr;
297 1.1 christos model *model_ptr;
298 1.1 christos
299 1.1 christos /* create a new instruction */
300 1.1 christos insn *new_insn = ZALLOC(insn);
301 1.1 christos new_insn->file_entry = file_entry;
302 1.1 christos new_insn->fields = fields;
303 1.1 christos
304 1.1 christos /* Check out any model information returned to make sure the model
305 1.1 christos is correct. */
306 1.1 christos for(insn_model_ptr = file_entry->model_first; insn_model_ptr; insn_model_ptr = insn_model_ptr->next) {
307 1.1.1.3 christos const char *name = insn_model_ptr->fields[insn_model_name];
308 1.1 christos int len = strlen (insn_model_ptr->fields[insn_model_fields]);
309 1.1 christos
310 1.1 christos while (len > 0 && isspace(*insn_model_ptr->fields[insn_model_fields])) {
311 1.1 christos len--;
312 1.1 christos insn_model_ptr->fields[insn_model_fields]++;
313 1.1 christos }
314 1.1 christos
315 1.1 christos if (max_model_fields_len < len)
316 1.1 christos max_model_fields_len = len;
317 1.1 christos
318 1.1 christos for(model_ptr = models; model_ptr; model_ptr = model_ptr->next) {
319 1.1 christos if (strcmp(name, model_ptr->printable_name) == 0) {
320 1.1 christos
321 1.1 christos /* Replace the name field with that of the global model, so that when we
322 1.1 christos want to print it out, we can just compare pointers. */
323 1.1 christos insn_model_ptr->fields[insn_model_name] = model_ptr->printable_name;
324 1.1 christos break;
325 1.1 christos }
326 1.1 christos }
327 1.1 christos
328 1.1 christos if (!model_ptr)
329 1.1 christos error("%s:%d: machine model `%s' was not known about\n",
330 1.1 christos file_entry->file_name, file_entry->line_nr, name);
331 1.1 christos }
332 1.1 christos
333 1.1 christos /* insert it according to the order of the fields */
334 1.1 christos while (cur_insn != NULL
335 1.1 christos && new_insn->fields->value >= cur_insn->fields->value) {
336 1.1 christos ptr_to_cur_insn = &cur_insn->next;
337 1.1 christos cur_insn = *ptr_to_cur_insn;
338 1.1 christos }
339 1.1 christos
340 1.1 christos new_insn->next = cur_insn;
341 1.1 christos *ptr_to_cur_insn = new_insn;
342 1.1 christos
343 1.1 christos table->nr_insn++;
344 1.1 christos }
345 1.1 christos
346 1.1 christos
347 1.1 christos
348 1.1 christos insn_table *
349 1.1 christos load_insn_table(const char *file_name,
350 1.1 christos decode_table *decode_rules,
351 1.1 christos filter *filters,
352 1.1 christos table_include *includes,
353 1.1 christos cache_table **cache_rules)
354 1.1 christos {
355 1.1 christos table *file = table_open(file_name, nr_insn_table_fields, nr_insn_model_table_fields);
356 1.1 christos insn_table *table = ZALLOC(insn_table);
357 1.1 christos table_entry *file_entry;
358 1.1 christos table->opcode_rule = decode_rules;
359 1.1 christos
360 1.1 christos while ((file_entry = table_entry_read(file)) != NULL) {
361 1.1 christos if (it_is("function", file_entry->fields[insn_flags])
362 1.1 christos || it_is("internal", file_entry->fields[insn_flags])) {
363 1.1 christos insn_table_insert_function(table, file_entry);
364 1.1 christos }
365 1.1 christos else if ((it_is("function", file_entry->fields[insn_form])
366 1.1 christos || it_is("internal", file_entry->fields[insn_form]))
367 1.1 christos && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
368 1.1 christos /* Ok, this is evil. Need to convert a new style function into
369 1.1 christos an old style function. Construct an old style table and then
370 1.1 christos copy it back. */
371 1.1 christos char *fields[nr_insn_table_fields];
372 1.1 christos memset (fields, 0, sizeof fields);
373 1.1 christos fields[insn_flags] = file_entry->fields[insn_form];
374 1.1 christos fields[function_type] = file_entry->fields[insn_name];
375 1.1 christos fields[function_name] = file_entry->fields[insn_comment];
376 1.1 christos fields[function_param] = file_entry->fields[insn_field_6];
377 1.1 christos memcpy (file_entry->fields, fields,
378 1.1 christos sizeof (fields[0]) * file_entry->nr_fields);
379 1.1 christos insn_table_insert_function(table, file_entry);
380 1.1 christos #if 0
381 1.1 christos ":" "..."
382 1.1 christos ":" <filter-flags>
383 1.1 christos ":" <filter-models>
384 1.1 christos ":" <typedef>
385 1.1 christos ":" <name>
386 1.1 christos [ ":" <parameter-list> ]
387 1.1 christos <nl>
388 1.1 christos [ <function-model> ]
389 1.1 christos <code-block>
390 1.1 christos #endif
391 1.1 christos }
392 1.1 christos else if (it_is("model", file_entry->fields[insn_flags])) {
393 1.1 christos model_table_insert(table, file_entry);
394 1.1 christos }
395 1.1 christos else if (it_is("model-macro", file_entry->fields[insn_flags])) {
396 1.1 christos model_table_insert_specific(table, file_entry, &model_macros, &last_model_macro);
397 1.1 christos }
398 1.1 christos else if (it_is("model-function", file_entry->fields[insn_flags])) {
399 1.1 christos model_table_insert_specific(table, file_entry, &model_functions, &last_model_function);
400 1.1 christos }
401 1.1 christos else if (it_is("model-internal", file_entry->fields[insn_flags])) {
402 1.1 christos model_table_insert_specific(table, file_entry, &model_internal, &last_model_internal);
403 1.1 christos }
404 1.1 christos else if (it_is("model-static", file_entry->fields[insn_flags])) {
405 1.1 christos model_table_insert_specific(table, file_entry, &model_static, &last_model_static);
406 1.1 christos }
407 1.1 christos else if (it_is("model-data", file_entry->fields[insn_flags])) {
408 1.1 christos model_table_insert_specific(table, file_entry, &model_data, &last_model_data);
409 1.1 christos }
410 1.1 christos else if (it_is("include", file_entry->fields[insn_form])
411 1.1 christos && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
412 1.1 christos parse_include_entry (file, file_entry, filters, includes);
413 1.1 christos }
414 1.1 christos else if ((it_is("cache", file_entry->fields[insn_form])
415 1.1 christos || it_is("compute", file_entry->fields[insn_form])
416 1.1 christos || it_is("scratch", file_entry->fields[insn_form]))
417 1.1 christos && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
418 1.1 christos append_cache_rule (cache_rules,
419 1.1 christos file_entry->fields[insn_form], /* type */
420 1.1 christos file_entry->fields[cache_name],
421 1.1 christos file_entry->fields[cache_derived_name],
422 1.1 christos file_entry->fields[cache_type_def],
423 1.1 christos file_entry->fields[cache_expression],
424 1.1 christos file_entry);
425 1.1 christos }
426 1.1 christos else {
427 1.1 christos insn_fields *fields;
428 1.1 christos /* skip instructions that aren't relevant to the mode */
429 1.1 christos if (is_filtered_out(file_entry->fields[insn_flags], filters)) {
430 1.1 christos fprintf(stderr, "Dropping %s - %s\n",
431 1.1 christos file_entry->fields[insn_name],
432 1.1 christos file_entry->fields[insn_flags]);
433 1.1 christos }
434 1.1 christos else {
435 1.1 christos /* create/insert the new instruction */
436 1.1 christos fields = parse_insn_format(file_entry,
437 1.1 christos file_entry->fields[insn_format]);
438 1.1 christos insn_table_insert_insn(table, file_entry, fields);
439 1.1 christos }
440 1.1 christos }
441 1.1 christos }
442 1.1 christos return table;
443 1.1 christos }
444 1.1 christos
445 1.1 christos
446 1.1 christos extern void
447 1.1 christos insn_table_traverse_tree(insn_table *table,
448 1.1 christos lf *file,
449 1.1 christos void *data,
450 1.1 christos int depth,
451 1.1 christos leaf_handler *start,
452 1.1 christos insn_handler *leaf,
453 1.1 christos leaf_handler *end,
454 1.1 christos padding_handler *padding)
455 1.1 christos {
456 1.1 christos insn_table *entry;
457 1.1 christos int entry_nr;
458 1.1 christos
459 1.1 christos ASSERT(table != NULL
460 1.1 christos && table->opcode != NULL
461 1.1 christos && table->nr_entries > 0
462 1.1 christos && table->entries != 0);
463 1.1 christos
464 1.1 christos if (start != NULL && depth >= 0)
465 1.1 christos start(table, file, data, depth);
466 1.1 christos
467 1.1 christos for (entry_nr = 0, entry = table->entries;
468 1.1 christos entry_nr < (table->opcode->is_boolean
469 1.1 christos ? 2
470 1.1 christos : (1 << (table->opcode->last - table->opcode->first + 1)));
471 1.1 christos entry_nr ++) {
472 1.1 christos if (entry == NULL
473 1.1 christos || (!table->opcode->is_boolean
474 1.1 christos && entry_nr < entry->opcode_nr)) {
475 1.1 christos if (padding != NULL && depth >= 0)
476 1.1 christos padding(table, file, data, depth, entry_nr);
477 1.1 christos }
478 1.1 christos else {
479 1.1 christos ASSERT(entry != NULL && (entry->opcode_nr == entry_nr
480 1.1 christos || table->opcode->is_boolean));
481 1.1 christos if (entry->opcode != NULL && depth != 0) {
482 1.1 christos insn_table_traverse_tree(entry, file, data, depth+1,
483 1.1 christos start, leaf, end, padding);
484 1.1 christos }
485 1.1 christos else if (depth >= 0) {
486 1.1 christos if (leaf != NULL)
487 1.1 christos leaf(entry, file, data, entry->insns, depth);
488 1.1 christos }
489 1.1 christos entry = entry->sibling;
490 1.1 christos }
491 1.1 christos }
492 1.1 christos if (end != NULL && depth >= 0)
493 1.1 christos end(table, file, data, depth);
494 1.1 christos }
495 1.1 christos
496 1.1 christos
497 1.1 christos extern void
498 1.1 christos insn_table_traverse_function(insn_table *table,
499 1.1 christos lf *file,
500 1.1 christos void *data,
501 1.1 christos function_handler *leaf)
502 1.1 christos {
503 1.1 christos insn *function;
504 1.1 christos for (function = table->functions;
505 1.1 christos function != NULL;
506 1.1 christos function = function->next) {
507 1.1 christos leaf(table, file, data, function->file_entry);
508 1.1 christos }
509 1.1 christos }
510 1.1 christos
511 1.1 christos extern void
512 1.1 christos insn_table_traverse_insn(insn_table *table,
513 1.1 christos lf *file,
514 1.1 christos void *data,
515 1.1 christos insn_handler *handler)
516 1.1 christos {
517 1.1 christos insn *instruction;
518 1.1 christos for (instruction = table->insns;
519 1.1 christos instruction != NULL;
520 1.1 christos instruction = instruction->next) {
521 1.1 christos handler(table, file, data, instruction, 0);
522 1.1 christos }
523 1.1 christos }
524 1.1 christos
525 1.1 christos
526 1.1 christos /****************************************************************/
527 1.1 christos
528 1.1 christos typedef enum {
529 1.1 christos field_constant_int = 1,
530 1.1 christos field_constant_slash = 2,
531 1.1 christos field_constant_string = 3
532 1.1 christos } constant_field_types;
533 1.1 christos
534 1.1 christos
535 1.1 christos static int
536 1.1 christos insn_field_is_constant(insn_field *field,
537 1.1 christos decode_table *rule)
538 1.1 christos {
539 1.1 christos /* field is an integer */
540 1.1 christos if (field->is_int)
541 1.1 christos return field_constant_int;
542 1.1 christos /* field is `/' and treating that as a constant */
543 1.1 christos if (field->is_slash && rule->force_slash)
544 1.1 christos return field_constant_slash;
545 1.1 christos /* field, though variable is on the list */
546 1.1 christos if (field->is_string && rule->force_expansion != NULL) {
547 1.1.1.3 christos const char *forced_fields = rule->force_expansion;
548 1.1 christos while (*forced_fields != '\0') {
549 1.1 christos int field_len;
550 1.1.1.3 christos const char *end = strchr(forced_fields, ',');
551 1.1 christos if (end == NULL)
552 1.1 christos field_len = strlen(forced_fields);
553 1.1 christos else
554 1.1 christos field_len = end-forced_fields;
555 1.1 christos if (strncmp(forced_fields, field->val_string, field_len) == 0
556 1.1 christos && field->val_string[field_len] == '\0')
557 1.1 christos return field_constant_string;
558 1.1 christos forced_fields += field_len;
559 1.1 christos if (*forced_fields == ',')
560 1.1 christos forced_fields++;
561 1.1 christos }
562 1.1 christos }
563 1.1 christos return 0;
564 1.1 christos }
565 1.1 christos
566 1.1 christos
567 1.1 christos static opcode_field *
568 1.1 christos insn_table_find_opcode_field(insn *insns,
569 1.1 christos decode_table *rule,
570 1.1 christos int string_only)
571 1.1 christos {
572 1.1 christos opcode_field *curr_opcode = ZALLOC(opcode_field);
573 1.1 christos insn *entry;
574 1.1 christos ASSERT(rule);
575 1.1 christos
576 1.1 christos curr_opcode->first = insn_bit_size;
577 1.1 christos curr_opcode->last = -1;
578 1.1 christos for (entry = insns; entry != NULL; entry = entry->next) {
579 1.1 christos insn_fields *fields = entry->fields;
580 1.1 christos opcode_field new_opcode;
581 1.1 christos
582 1.1 christos /* find a start point for the opcode field */
583 1.1 christos new_opcode.first = rule->first;
584 1.1 christos while (new_opcode.first <= rule->last
585 1.1 christos && (!string_only
586 1.1 christos || insn_field_is_constant(fields->bits[new_opcode.first],
587 1.1 christos rule) != field_constant_string)
588 1.1 christos && (string_only
589 1.1 christos || !insn_field_is_constant(fields->bits[new_opcode.first],
590 1.1 christos rule)))
591 1.1 christos new_opcode.first = fields->bits[new_opcode.first]->last + 1;
592 1.1 christos ASSERT(new_opcode.first > rule->last
593 1.1 christos || (string_only
594 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.first],
595 1.1 christos rule) == field_constant_string)
596 1.1 christos || (!string_only
597 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.first],
598 1.1 christos rule)));
599 1.1 christos
600 1.1 christos /* find the end point for the opcode field */
601 1.1 christos new_opcode.last = rule->last;
602 1.1 christos while (new_opcode.last >= rule->first
603 1.1 christos && (!string_only
604 1.1 christos || insn_field_is_constant(fields->bits[new_opcode.last],
605 1.1 christos rule) != field_constant_string)
606 1.1 christos && (string_only
607 1.1 christos || !insn_field_is_constant(fields->bits[new_opcode.last],
608 1.1 christos rule)))
609 1.1 christos new_opcode.last = fields->bits[new_opcode.last]->first - 1;
610 1.1 christos ASSERT(new_opcode.last < rule->first
611 1.1 christos || (string_only
612 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.last],
613 1.1 christos rule) == field_constant_string)
614 1.1 christos || (!string_only
615 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.last],
616 1.1 christos rule)));
617 1.1 christos
618 1.1 christos /* now see if our current opcode needs expanding */
619 1.1 christos if (new_opcode.first <= rule->last
620 1.1 christos && curr_opcode->first > new_opcode.first)
621 1.1 christos curr_opcode->first = new_opcode.first;
622 1.1 christos if (new_opcode.last >= rule->first
623 1.1 christos && curr_opcode->last < new_opcode.last)
624 1.1 christos curr_opcode->last = new_opcode.last;
625 1.1 christos
626 1.1 christos }
627 1.1 christos
628 1.1 christos /* was any thing interesting found? */
629 1.1 christos if (curr_opcode->first > rule->last) {
630 1.1 christos ASSERT(curr_opcode->last < rule->first);
631 1.1 christos return NULL;
632 1.1 christos }
633 1.1 christos ASSERT(curr_opcode->last >= rule->first);
634 1.1 christos ASSERT(curr_opcode->first <= rule->last);
635 1.1 christos
636 1.1 christos /* if something was found, check it includes the forced field range */
637 1.1 christos if (!string_only
638 1.1 christos && curr_opcode->first > rule->force_first) {
639 1.1 christos curr_opcode->first = rule->force_first;
640 1.1 christos }
641 1.1 christos if (!string_only
642 1.1 christos && curr_opcode->last < rule->force_last) {
643 1.1 christos curr_opcode->last = rule->force_last;
644 1.1 christos }
645 1.1 christos /* handle special case elminating any need to do shift after mask */
646 1.1 christos if (string_only
647 1.1 christos && rule->force_last == insn_bit_size-1) {
648 1.1 christos curr_opcode->last = insn_bit_size-1;
649 1.1 christos }
650 1.1 christos
651 1.1 christos /* handle any special cases */
652 1.1 christos switch (rule->type) {
653 1.1 christos case normal_decode_rule:
654 1.1 christos /* let the above apply */
655 1.1 christos break;
656 1.1 christos case expand_forced_rule:
657 1.1 christos /* expand a limited nr of bits, ignoring the rest */
658 1.1 christos curr_opcode->first = rule->force_first;
659 1.1 christos curr_opcode->last = rule->force_last;
660 1.1 christos break;
661 1.1 christos case boolean_rule:
662 1.1 christos curr_opcode->is_boolean = 1;
663 1.1 christos curr_opcode->boolean_constant = rule->special_constant;
664 1.1 christos break;
665 1.1 christos default:
666 1.1 christos error("Something is going wrong\n");
667 1.1 christos }
668 1.1 christos
669 1.1 christos return curr_opcode;
670 1.1 christos }
671 1.1 christos
672 1.1 christos
673 1.1 christos static void
674 1.1 christos insn_table_insert_expanded(insn_table *table,
675 1.1 christos insn *old_insn,
676 1.1 christos int new_opcode_nr,
677 1.1 christos insn_bits *new_bits)
678 1.1 christos {
679 1.1 christos insn_table **ptr_to_cur_entry = &table->entries;
680 1.1 christos insn_table *cur_entry = *ptr_to_cur_entry;
681 1.1 christos
682 1.1 christos /* find the new table for this entry */
683 1.1 christos while (cur_entry != NULL
684 1.1 christos && cur_entry->opcode_nr < new_opcode_nr) {
685 1.1 christos ptr_to_cur_entry = &cur_entry->sibling;
686 1.1 christos cur_entry = *ptr_to_cur_entry;
687 1.1 christos }
688 1.1 christos
689 1.1 christos if (cur_entry == NULL || cur_entry->opcode_nr != new_opcode_nr) {
690 1.1 christos insn_table *new_entry = ZALLOC(insn_table);
691 1.1 christos new_entry->opcode_nr = new_opcode_nr;
692 1.1 christos new_entry->expanded_bits = new_bits;
693 1.1 christos new_entry->opcode_rule = table->opcode_rule->next;
694 1.1 christos new_entry->sibling = cur_entry;
695 1.1 christos new_entry->parent = table;
696 1.1 christos *ptr_to_cur_entry = new_entry;
697 1.1 christos cur_entry = new_entry;
698 1.1 christos table->nr_entries++;
699 1.1 christos }
700 1.1 christos /* ASSERT new_bits == cur_entry bits */
701 1.1 christos ASSERT(cur_entry != NULL && cur_entry->opcode_nr == new_opcode_nr);
702 1.1 christos insn_table_insert_insn(cur_entry,
703 1.1 christos old_insn->file_entry,
704 1.1 christos old_insn->fields);
705 1.1 christos }
706 1.1 christos
707 1.1 christos static void
708 1.1 christos insn_table_expand_opcode(insn_table *table,
709 1.1 christos insn *instruction,
710 1.1 christos int field_nr,
711 1.1 christos int opcode_nr,
712 1.1 christos insn_bits *bits)
713 1.1 christos {
714 1.1 christos
715 1.1 christos if (field_nr > table->opcode->last) {
716 1.1 christos insn_table_insert_expanded(table, instruction, opcode_nr, bits);
717 1.1 christos }
718 1.1 christos else {
719 1.1 christos insn_field *field = instruction->fields->bits[field_nr];
720 1.1 christos if (field->is_int || field->is_slash) {
721 1.1 christos ASSERT(field->first >= table->opcode->first
722 1.1 christos && field->last <= table->opcode->last);
723 1.1 christos insn_table_expand_opcode(table, instruction, field->last+1,
724 1.1 christos ((opcode_nr << field->width) + field->val_int),
725 1.1 christos bits);
726 1.1 christos }
727 1.1 christos else {
728 1.1 christos int val;
729 1.1 christos int last_pos = ((field->last < table->opcode->last)
730 1.1 christos ? field->last : table->opcode->last);
731 1.1 christos int first_pos = ((field->first > table->opcode->first)
732 1.1 christos ? field->first : table->opcode->first);
733 1.1 christos int width = last_pos - first_pos + 1;
734 1.1 christos int last_val = (table->opcode->is_boolean
735 1.1 christos ? 2 : (1 << width));
736 1.1 christos for (val = 0; val < last_val; val++) {
737 1.1 christos insn_bits *new_bits = ZALLOC(insn_bits);
738 1.1 christos new_bits->field = field;
739 1.1 christos new_bits->value = val;
740 1.1 christos new_bits->last = bits;
741 1.1 christos new_bits->opcode = table->opcode;
742 1.1 christos insn_table_expand_opcode(table, instruction, last_pos+1,
743 1.1 christos ((opcode_nr << width) | val),
744 1.1 christos new_bits);
745 1.1 christos }
746 1.1 christos }
747 1.1 christos }
748 1.1 christos }
749 1.1 christos
750 1.1 christos static void
751 1.1 christos insn_table_insert_expanding(insn_table *table,
752 1.1 christos insn *entry)
753 1.1 christos {
754 1.1 christos insn_table_expand_opcode(table,
755 1.1 christos entry,
756 1.1 christos table->opcode->first,
757 1.1 christos 0,
758 1.1 christos table->expanded_bits);
759 1.1 christos }
760 1.1 christos
761 1.1 christos
762 1.1 christos extern void
763 1.1 christos insn_table_expand_insns(insn_table *table)
764 1.1 christos {
765 1.1 christos
766 1.1 christos ASSERT(table->nr_insn >= 1);
767 1.1 christos
768 1.1 christos /* determine a valid opcode */
769 1.1 christos while (table->opcode_rule) {
770 1.1 christos /* specials only for single instructions */
771 1.1 christos if ((table->nr_insn > 1
772 1.1 christos && table->opcode_rule->special_mask == 0
773 1.1 christos && table->opcode_rule->type == normal_decode_rule)
774 1.1 christos || (table->nr_insn == 1
775 1.1 christos && table->opcode_rule->special_mask != 0
776 1.1 christos && ((table->insns->fields->value
777 1.1 christos & table->opcode_rule->special_mask)
778 1.1 christos == table->opcode_rule->special_value))
779 1.1 christos || (generate_expanded_instructions
780 1.1 christos && table->opcode_rule->special_mask == 0
781 1.1 christos && table->opcode_rule->type == normal_decode_rule))
782 1.1 christos table->opcode =
783 1.1 christos insn_table_find_opcode_field(table->insns,
784 1.1 christos table->opcode_rule,
785 1.1 christos table->nr_insn == 1/*string*/
786 1.1 christos );
787 1.1 christos if (table->opcode != NULL)
788 1.1 christos break;
789 1.1 christos table->opcode_rule = table->opcode_rule->next;
790 1.1 christos }
791 1.1 christos
792 1.1 christos /* did we find anything */
793 1.1 christos if (table->opcode == NULL) {
794 1.1 christos return;
795 1.1 christos }
796 1.1 christos ASSERT(table->opcode != NULL);
797 1.1 christos
798 1.1 christos /* back link what we found to its parent */
799 1.1 christos if (table->parent != NULL) {
800 1.1 christos ASSERT(table->parent->opcode != NULL);
801 1.1 christos table->opcode->parent = table->parent->opcode;
802 1.1 christos }
803 1.1 christos
804 1.1 christos /* expand the raw instructions according to the opcode */
805 1.1 christos {
806 1.1 christos insn *entry;
807 1.1 christos for (entry = table->insns; entry != NULL; entry = entry->next) {
808 1.1 christos insn_table_insert_expanding(table, entry);
809 1.1 christos }
810 1.1 christos }
811 1.1 christos
812 1.1 christos /* and do the same for the sub entries */
813 1.1 christos {
814 1.1 christos insn_table *entry;
815 1.1 christos for (entry = table->entries; entry != NULL; entry = entry->sibling) {
816 1.1 christos insn_table_expand_insns(entry);
817 1.1 christos }
818 1.1 christos }
819 1.1 christos }
820 1.1 christos
821 1.1 christos
822 1.1 christos
823 1.1 christos
824 1.1 christos #ifdef MAIN
825 1.1 christos
826 1.1 christos static void
827 1.1 christos dump_insn_field(insn_field *field,
828 1.1 christos int indent)
829 1.1 christos {
830 1.1.1.3 christos printf ("(insn_field*)%p\n", field);
831 1.1.1.3 christos dumpf (indent, "(first %d)\n", field->first);
832 1.1.1.3 christos dumpf (indent, "(last %d)\n", field->last);
833 1.1.1.3 christos dumpf (indent, "(width %d)\n", field->width);
834 1.1 christos if (field->is_int)
835 1.1.1.3 christos dumpf (indent, "(is_int %d)\n", field->val_int);
836 1.1 christos if (field->is_slash)
837 1.1.1.3 christos dumpf (indent, "(is_slash)\n");
838 1.1 christos if (field->is_string)
839 1.1.1.3 christos dumpf (indent, "(is_string `%s')\n", field->val_string);
840 1.1.1.3 christos dumpf (indent, "(next %p)\n", field->next);
841 1.1.1.3 christos dumpf (indent, "(prev %p)\n", field->prev);
842 1.1 christos }
843 1.1 christos
844 1.1 christos static void
845 1.1 christos dump_insn_fields(insn_fields *fields,
846 1.1 christos int indent)
847 1.1 christos {
848 1.1 christos int i;
849 1.1 christos
850 1.1 christos printf("(insn_fields*)%p\n", fields);
851 1.1 christos
852 1.1.1.3 christos dumpf(indent, "(first %p)\n", fields->first);
853 1.1.1.3 christos dumpf(indent, "(last %p)\n", fields->last);
854 1.1 christos
855 1.1 christos dumpf(indent, "(value 0x%x)\n", fields->value);
856 1.1 christos
857 1.1 christos for (i = 0; i < insn_bit_size; i++) {
858 1.1.1.3 christos dumpf(indent, "(bits[%d]", i);
859 1.1 christos dump_insn_field(fields->bits[i], indent+1);
860 1.1 christos dumpf(indent, " )\n");
861 1.1 christos }
862 1.1 christos
863 1.1 christos }
864 1.1 christos
865 1.1 christos
866 1.1 christos static void
867 1.1 christos dump_opcode_field(opcode_field *field, int indent, int levels)
868 1.1 christos {
869 1.1 christos printf("(opcode_field*)%p\n", field);
870 1.1 christos if (levels && field != NULL) {
871 1.1 christos dumpf(indent, "(first %d)\n", field->first);
872 1.1 christos dumpf(indent, "(last %d)\n", field->last);
873 1.1 christos dumpf(indent, "(is_boolean %d)\n", field->is_boolean);
874 1.1 christos dumpf(indent, "(parent ");
875 1.1 christos dump_opcode_field(field->parent, indent, levels-1);
876 1.1 christos }
877 1.1 christos }
878 1.1 christos
879 1.1 christos
880 1.1 christos static void
881 1.1 christos dump_insn_bits(insn_bits *bits, int indent, int levels)
882 1.1 christos {
883 1.1 christos printf("(insn_bits*)%p\n", bits);
884 1.1 christos
885 1.1 christos if (levels && bits != NULL) {
886 1.1 christos dumpf(indent, "(value %d)\n", bits->value);
887 1.1 christos dumpf(indent, "(opcode ");
888 1.1 christos dump_opcode_field(bits->opcode, indent+1, 0);
889 1.1 christos dumpf(indent, " )\n");
890 1.1 christos dumpf(indent, "(field ");
891 1.1 christos dump_insn_field(bits->field, indent+1);
892 1.1 christos dumpf(indent, " )\n");
893 1.1 christos dumpf(indent, "(last ");
894 1.1 christos dump_insn_bits(bits->last, indent+1, levels-1);
895 1.1 christos }
896 1.1 christos }
897 1.1 christos
898 1.1 christos
899 1.1 christos
900 1.1 christos static void
901 1.1 christos dump_insn(insn *entry, int indent, int levels)
902 1.1 christos {
903 1.1 christos printf("(insn*)%p\n", entry);
904 1.1 christos
905 1.1 christos if (levels && entry != NULL) {
906 1.1 christos
907 1.1 christos dumpf(indent, "(file_entry ");
908 1.1 christos dump_table_entry(entry->file_entry, indent+1);
909 1.1 christos dumpf(indent, " )\n");
910 1.1 christos
911 1.1 christos dumpf(indent, "(fields ");
912 1.1 christos dump_insn_fields(entry->fields, indent+1);
913 1.1 christos dumpf(indent, " )\n");
914 1.1 christos
915 1.1 christos dumpf(indent, "(next ");
916 1.1 christos dump_insn(entry->next, indent+1, levels-1);
917 1.1 christos dumpf(indent, " )\n");
918 1.1 christos
919 1.1 christos }
920 1.1 christos
921 1.1 christos }
922 1.1 christos
923 1.1 christos
924 1.1 christos static void
925 1.1 christos dump_insn_table(insn_table *table,
926 1.1 christos int indent, int levels)
927 1.1 christos {
928 1.1 christos
929 1.1 christos printf("(insn_table*)%p\n", table);
930 1.1 christos
931 1.1 christos if (levels && table != NULL) {
932 1.1 christos
933 1.1 christos dumpf(indent, "(opcode_nr %d)\n", table->opcode_nr);
934 1.1 christos
935 1.1 christos dumpf(indent, "(expanded_bits ");
936 1.1 christos dump_insn_bits(table->expanded_bits, indent+1, -1);
937 1.1 christos dumpf(indent, " )\n");
938 1.1 christos
939 1.1 christos dumpf(indent, "(int nr_insn %d)\n", table->nr_insn);
940 1.1 christos
941 1.1 christos dumpf(indent, "(insns ");
942 1.1 christos dump_insn(table->insns, indent+1, table->nr_insn);
943 1.1 christos dumpf(indent, " )\n");
944 1.1 christos
945 1.1 christos dumpf(indent, "(opcode_rule ");
946 1.1 christos dump_decode_rule(table->opcode_rule, indent+1);
947 1.1 christos dumpf(indent, " )\n");
948 1.1 christos
949 1.1 christos dumpf(indent, "(opcode ");
950 1.1 christos dump_opcode_field(table->opcode, indent+1, 1);
951 1.1 christos dumpf(indent, " )\n");
952 1.1 christos
953 1.1.1.3 christos dumpf(indent, "(nr_entries %d)\n", table->nr_entries);
954 1.1 christos dumpf(indent, "(entries ");
955 1.1 christos dump_insn_table(table->entries, indent+1, table->nr_entries);
956 1.1 christos dumpf(indent, " )\n");
957 1.1 christos
958 1.1.1.3 christos dumpf(indent, "(sibling ");
959 1.1 christos dump_insn_table(table->sibling, indent+1, levels-1);
960 1.1 christos dumpf(indent, " )\n");
961 1.1 christos
962 1.1.1.3 christos dumpf(indent, "(parent ");
963 1.1 christos dump_insn_table(table->parent, indent+1, 0);
964 1.1 christos dumpf(indent, " )\n");
965 1.1 christos
966 1.1 christos }
967 1.1 christos }
968 1.1 christos
969 1.1 christos int insn_bit_size = max_insn_bit_size;
970 1.1 christos int hi_bit_nr;
971 1.1 christos int generate_expanded_instructions;
972 1.1 christos
973 1.1 christos int
974 1.1 christos main(int argc, char **argv)
975 1.1 christos {
976 1.1 christos filter *filters = NULL;
977 1.1 christos decode_table *decode_rules = NULL;
978 1.1 christos insn_table *instructions = NULL;
979 1.1 christos cache_table *cache_rules = NULL;
980 1.1 christos
981 1.1 christos if (argc != 5)
982 1.1 christos error("Usage: insn <filter> <hi-bit-nr> <decode-table> <insn-table>\n");
983 1.1 christos
984 1.1 christos filters = new_filter(argv[1], filters);
985 1.1 christos hi_bit_nr = a2i(argv[2]);
986 1.1 christos ASSERT(hi_bit_nr < insn_bit_size);
987 1.1 christos decode_rules = load_decode_table(argv[3], hi_bit_nr);
988 1.1 christos instructions = load_insn_table(argv[4], decode_rules, filters, NULL,
989 1.1 christos &cache_rules);
990 1.1 christos insn_table_expand_insns(instructions);
991 1.1 christos
992 1.1 christos dump_insn_table(instructions, 0, -1);
993 1.1 christos return 0;
994 1.1 christos }
995 1.1 christos
996 1.1 christos #endif
997