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