* config/tc-mn10200.c (md_relax_table): Define.
[binutils-gdb.git] / gas / config / tc-mn10200.c
1 /* tc-mn10200.c -- Assembler code for the Matsushita 10200
2
3 Copyright (C) 1996, 1997 Free Software Foundation.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include <stdio.h>
23 #include <ctype.h>
24 #include "as.h"
25 #include "subsegs.h"
26 #include "opcode/mn10200.h"
27 \f
28 /* Structure to hold information about predefined registers. */
29 struct reg_name
30 {
31 const char *name;
32 int value;
33 };
34
35 /* Generic assembler global variables which must be defined by all targets. */
36
37 /* Characters which always start a comment. */
38 const char comment_chars[] = "#";
39
40 /* Characters which start a comment at the beginning of a line. */
41 const char line_comment_chars[] = ";#";
42
43 /* Characters which may be used to separate multiple commands on a
44 single line. */
45 const char line_separator_chars[] = ";";
46
47 /* Characters which are used to indicate an exponent in a floating
48 point number. */
49 const char EXP_CHARS[] = "eE";
50
51 /* Characters which mean that a number is a floating point constant,
52 as in 0d1.0. */
53 const char FLT_CHARS[] = "dD";
54 \f
55
56 const relax_typeS md_relax_table[] = {
57 /* bCC relaxing */
58 {0x7f, -0x80, 2, 1},
59 {0x7fff, -0x8000, 5, 2},
60 {0x7fffff, -0x8000000, 7, 0},
61 /* bCCx relaxing */
62 {0x7f, -0x80, 3, 4},
63 {0x7fff, -0x8000, 6, 5},
64 {0x7fffff, -0x8000000, 8, 0},
65 /* jmp/jsr relaxing, could have a bra variant too! */
66 {0x7fff, -0x8000, 3, 7},
67 {0x7fffff, -0x8000000, 5, 0},
68
69 };
70 /* local functions */
71 static void mn10200_insert_operand PARAMS ((unsigned long *, unsigned long *,
72 const struct mn10200_operand *,
73 offsetT, char *, unsigned,
74 unsigned));
75 static unsigned long check_operand PARAMS ((unsigned long,
76 const struct mn10200_operand *,
77 offsetT));
78 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
79 static boolean data_register_name PARAMS ((expressionS *expressionP));
80 static boolean address_register_name PARAMS ((expressionS *expressionP));
81 static boolean other_register_name PARAMS ((expressionS *expressionP));
82
83
84 /* fixups */
85 #define MAX_INSN_FIXUPS (5)
86 struct mn10200_fixup
87 {
88 expressionS exp;
89 int opindex;
90 bfd_reloc_code_real_type reloc;
91 };
92 struct mn10200_fixup fixups[MAX_INSN_FIXUPS];
93 static int fc;
94 \f
95 const char *md_shortopts = "";
96 struct option md_longopts[] = {
97 {NULL, no_argument, NULL, 0}
98 };
99 size_t md_longopts_size = sizeof(md_longopts);
100
101 /* The target specific pseudo-ops which we support. */
102 const pseudo_typeS md_pseudo_table[] =
103 {
104 { NULL, NULL, 0 }
105 };
106
107 /* Opcode hash table. */
108 static struct hash_control *mn10200_hash;
109
110 /* This table is sorted. Suitable for searching by a binary search. */
111 static const struct reg_name data_registers[] =
112 {
113 { "d0", 0 },
114 { "d1", 1 },
115 { "d2", 2 },
116 { "d3", 3 },
117 };
118 #define DATA_REG_NAME_CNT (sizeof(data_registers) / sizeof(struct reg_name))
119
120 static const struct reg_name address_registers[] =
121 {
122 { "a0", 0 },
123 { "a1", 1 },
124 { "a2", 2 },
125 { "a3", 3 },
126 };
127 #define ADDRESS_REG_NAME_CNT (sizeof(address_registers) / sizeof(struct reg_name))
128
129 static const struct reg_name other_registers[] =
130 {
131 { "mdr", 0 },
132 { "psw", 0 },
133 };
134 #define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
135
136 /* reg_name_search does a binary search of the given register table
137 to see if "name" is a valid regiter name. Returns the register
138 number from the array on success, or -1 on failure. */
139
140 static int
141 reg_name_search (regs, regcount, name)
142 const struct reg_name *regs;
143 int regcount;
144 const char *name;
145 {
146 int middle, low, high;
147 int cmp;
148
149 low = 0;
150 high = regcount - 1;
151
152 do
153 {
154 middle = (low + high) / 2;
155 cmp = strcasecmp (name, regs[middle].name);
156 if (cmp < 0)
157 high = middle - 1;
158 else if (cmp > 0)
159 low = middle + 1;
160 else
161 return regs[middle].value;
162 }
163 while (low <= high);
164 return -1;
165 }
166
167
168 /* Summary of register_name().
169 *
170 * in: Input_line_pointer points to 1st char of operand.
171 *
172 * out: A expressionS.
173 * The operand may have been a register: in this case, X_op == O_register,
174 * X_add_number is set to the register number, and truth is returned.
175 * Input_line_pointer->(next non-blank) char after operand, or is in
176 * its original state.
177 */
178 static boolean
179 data_register_name (expressionP)
180 expressionS *expressionP;
181 {
182 int reg_number;
183 char *name;
184 char *start;
185 char c;
186
187 /* Find the spelling of the operand */
188 start = name = input_line_pointer;
189
190 c = get_symbol_end ();
191 reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
192
193 /* look to see if it's in the register table */
194 if (reg_number >= 0)
195 {
196 expressionP->X_op = O_register;
197 expressionP->X_add_number = reg_number;
198
199 /* make the rest nice */
200 expressionP->X_add_symbol = NULL;
201 expressionP->X_op_symbol = NULL;
202 *input_line_pointer = c; /* put back the delimiting char */
203 return true;
204 }
205 else
206 {
207 /* reset the line as if we had not done anything */
208 *input_line_pointer = c; /* put back the delimiting char */
209 input_line_pointer = start; /* reset input_line pointer */
210 return false;
211 }
212 }
213
214 /* Summary of register_name().
215 *
216 * in: Input_line_pointer points to 1st char of operand.
217 *
218 * out: A expressionS.
219 * The operand may have been a register: in this case, X_op == O_register,
220 * X_add_number is set to the register number, and truth is returned.
221 * Input_line_pointer->(next non-blank) char after operand, or is in
222 * its original state.
223 */
224 static boolean
225 address_register_name (expressionP)
226 expressionS *expressionP;
227 {
228 int reg_number;
229 char *name;
230 char *start;
231 char c;
232
233 /* Find the spelling of the operand */
234 start = name = input_line_pointer;
235
236 c = get_symbol_end ();
237 reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
238
239 /* look to see if it's in the register table */
240 if (reg_number >= 0)
241 {
242 expressionP->X_op = O_register;
243 expressionP->X_add_number = reg_number;
244
245 /* make the rest nice */
246 expressionP->X_add_symbol = NULL;
247 expressionP->X_op_symbol = NULL;
248 *input_line_pointer = c; /* put back the delimiting char */
249 return true;
250 }
251 else
252 {
253 /* reset the line as if we had not done anything */
254 *input_line_pointer = c; /* put back the delimiting char */
255 input_line_pointer = start; /* reset input_line pointer */
256 return false;
257 }
258 }
259
260 /* Summary of register_name().
261 *
262 * in: Input_line_pointer points to 1st char of operand.
263 *
264 * out: A expressionS.
265 * The operand may have been a register: in this case, X_op == O_register,
266 * X_add_number is set to the register number, and truth is returned.
267 * Input_line_pointer->(next non-blank) char after operand, or is in
268 * its original state.
269 */
270 static boolean
271 other_register_name (expressionP)
272 expressionS *expressionP;
273 {
274 int reg_number;
275 char *name;
276 char *start;
277 char c;
278
279 /* Find the spelling of the operand */
280 start = name = input_line_pointer;
281
282 c = get_symbol_end ();
283 reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
284
285 /* look to see if it's in the register table */
286 if (reg_number >= 0)
287 {
288 expressionP->X_op = O_register;
289 expressionP->X_add_number = reg_number;
290
291 /* make the rest nice */
292 expressionP->X_add_symbol = NULL;
293 expressionP->X_op_symbol = NULL;
294 *input_line_pointer = c; /* put back the delimiting char */
295 return true;
296 }
297 else
298 {
299 /* reset the line as if we had not done anything */
300 *input_line_pointer = c; /* put back the delimiting char */
301 input_line_pointer = start; /* reset input_line pointer */
302 return false;
303 }
304 }
305
306 void
307 md_show_usage (stream)
308 FILE *stream;
309 {
310 fprintf(stream, "MN10200 options:\n\
311 none yet\n");
312 }
313
314 int
315 md_parse_option (c, arg)
316 int c;
317 char *arg;
318 {
319 return 0;
320 }
321
322 symbolS *
323 md_undefined_symbol (name)
324 char *name;
325 {
326 return 0;
327 }
328
329 char *
330 md_atof (type, litp, sizep)
331 int type;
332 char *litp;
333 int *sizep;
334 {
335 int prec;
336 LITTLENUM_TYPE words[4];
337 char *t;
338 int i;
339
340 switch (type)
341 {
342 case 'f':
343 prec = 2;
344 break;
345
346 case 'd':
347 prec = 4;
348 break;
349
350 default:
351 *sizep = 0;
352 return "bad call to md_atof";
353 }
354
355 t = atof_ieee (input_line_pointer, type, words);
356 if (t)
357 input_line_pointer = t;
358
359 *sizep = prec * 2;
360
361 for (i = prec - 1; i >= 0; i--)
362 {
363 md_number_to_chars (litp, (valueT) words[i], 2);
364 litp += 2;
365 }
366
367 return NULL;
368 }
369
370
371 void
372 md_convert_frag (abfd, sec, fragP)
373 bfd *abfd;
374 asection *sec;
375 fragS *fragP;
376 {
377 subseg_change (sec, 0);
378 if (fragP->fr_subtype == 0)
379 {
380 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
381 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
382 fragP->fr_var = 0;
383 fragP->fr_fix += 2;
384 }
385 else if (fragP->fr_subtype == 1)
386 {
387 /* Reverse the condition of the first branch. */
388 int offset = fragP->fr_fix;
389 int opcode = fragP->fr_literal[offset] & 0xff;
390
391 switch (opcode)
392 {
393 case 0xe8:
394 opcode = 0xe9;
395 break;
396 case 0xe9:
397 opcode = 0xe8;
398 break;
399 case 0xe0:
400 opcode = 0xe2;
401 break;
402 case 0xe2:
403 opcode = 0xe0;
404 break;
405 case 0xe3:
406 opcode = 0xe1;
407 break;
408 case 0xe1:
409 opcode = 0xe3;
410 break;
411 case 0xe4:
412 opcode = 0xe6;
413 break;
414 case 0xe6:
415 opcode = 0xe4;
416 break;
417 case 0xe7:
418 opcode = 0xe5;
419 break;
420 case 0xe5:
421 opcode = 0xe7;
422 break;
423 default:
424 abort ();
425 }
426 fragP->fr_literal[offset] = opcode;
427
428 /* Set the displacement bits so that we branch around
429 the unconditional branch. */
430 fragP->fr_literal[offset + 1] = 0x5;
431
432 /* Now create the unconditional branch + fixup to the
433 final target. */
434 fragP->fr_literal[offset + 2] = 0xfc;
435 fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
436 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
437 fragP->fr_var = 0;
438 fragP->fr_fix += 5;
439 }
440 else if (fragP->fr_subtype == 2)
441 {
442 /* Reverse the condition of the first branch. */
443 int offset = fragP->fr_fix;
444 int opcode = fragP->fr_literal[offset] & 0xff;
445
446 switch (opcode)
447 {
448 case 0xe8:
449 opcode = 0xe9;
450 break;
451 case 0xe9:
452 opcode = 0xe8;
453 break;
454 case 0xe0:
455 opcode = 0xe2;
456 break;
457 case 0xe2:
458 opcode = 0xe0;
459 break;
460 case 0xe3:
461 opcode = 0xe1;
462 break;
463 case 0xe1:
464 opcode = 0xe3;
465 break;
466 case 0xe4:
467 opcode = 0xe6;
468 break;
469 case 0xe6:
470 opcode = 0xe4;
471 break;
472 case 0xe7:
473 opcode = 0xe5;
474 break;
475 case 0xe5:
476 opcode = 0xe7;
477 break;
478 default:
479 abort ();
480 }
481 fragP->fr_literal[offset] = opcode;
482
483 /* Set the displacement bits so that we branch around
484 the unconditional branch. */
485 fragP->fr_literal[offset + 1] = 0x7;
486
487 /* Now create the unconditional branch + fixup to the
488 final target. */
489 fragP->fr_literal[offset + 2] = 0xf4;
490 fragP->fr_literal[offset + 3] = 0xe0;
491 fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
492 fragP->fr_offset + 2, 1, BFD_RELOC_24_PCREL);
493 fragP->fr_var = 0;
494 fragP->fr_fix += 7;
495 }
496 else if (fragP->fr_subtype == 3)
497 {
498 fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
499 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
500 fragP->fr_var = 0;
501 fragP->fr_fix += 3;
502 }
503 else if (fragP->fr_subtype == 4)
504 {
505 /* Reverse the condition of the first branch. */
506 int offset = fragP->fr_fix;
507 int opcode = fragP->fr_literal[offset + 1] & 0xff;
508
509 switch (opcode)
510 {
511 case 0xfc:
512 opcode = 0xfd;
513 break;
514 case 0xfd:
515 opcode = 0xfc;
516 break;
517 case 0xfe:
518 opcode = 0xff;
519 break;
520 case 0xff:
521 opcode = 0xfe;
522 case 0xe8:
523 opcode = 0xe9;
524 break;
525 case 0xe9:
526 opcode = 0xe8;
527 break;
528 case 0xe0:
529 opcode = 0xe2;
530 break;
531 case 0xe2:
532 opcode = 0xe0;
533 break;
534 case 0xe3:
535 opcode = 0xe1;
536 break;
537 case 0xe1:
538 opcode = 0xe3;
539 break;
540 case 0xe4:
541 opcode = 0xe6;
542 break;
543 case 0xe6:
544 opcode = 0xe4;
545 break;
546 case 0xe7:
547 opcode = 0xe5;
548 break;
549 case 0xe5:
550 opcode = 0xe7;
551 break;
552 case 0xec:
553 opcode = 0xed;
554 break;
555 case 0xed:
556 opcode = 0xec;
557 break;
558 case 0xee:
559 opcode = 0xef;
560 break;
561 case 0xef:
562 opcode = 0xee;
563 break;
564 default:
565 abort ();
566 }
567 fragP->fr_literal[offset + 1] = opcode;
568
569 /* Set the displacement bits so that we branch around
570 the unconditional branch. */
571 fragP->fr_literal[offset + 2] = 0x6;
572
573 /* Now create the unconditional branch + fixup to the
574 final target. */
575 fragP->fr_literal[offset + 3] = 0xfc;
576 fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
577 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
578 fragP->fr_var = 0;
579 fragP->fr_fix += 6;
580 }
581 else if (fragP->fr_subtype == 5)
582 {
583 /* Reverse the condition of the first branch. */
584 int offset = fragP->fr_fix;
585 int opcode = fragP->fr_literal[offset + 1] & 0xff;
586
587 switch (opcode)
588 {
589 case 0xfc:
590 opcode = 0xfd;
591 break;
592 case 0xfd:
593 opcode = 0xfc;
594 break;
595 case 0xfe:
596 opcode = 0xff;
597 break;
598 case 0xff:
599 opcode = 0xfe;
600 case 0xe8:
601 opcode = 0xe9;
602 break;
603 case 0xe9:
604 opcode = 0xe8;
605 break;
606 case 0xe0:
607 opcode = 0xe2;
608 break;
609 case 0xe2:
610 opcode = 0xe0;
611 break;
612 case 0xe3:
613 opcode = 0xe1;
614 break;
615 case 0xe1:
616 opcode = 0xe3;
617 break;
618 case 0xe4:
619 opcode = 0xe6;
620 break;
621 case 0xe6:
622 opcode = 0xe4;
623 break;
624 case 0xe7:
625 opcode = 0xe5;
626 break;
627 case 0xe5:
628 opcode = 0xe7;
629 break;
630 case 0xec:
631 opcode = 0xed;
632 break;
633 case 0xed:
634 opcode = 0xec;
635 break;
636 case 0xee:
637 opcode = 0xef;
638 break;
639 case 0xef:
640 opcode = 0xee;
641 break;
642 default:
643 abort ();
644 }
645 fragP->fr_literal[offset + 1] = opcode;
646
647 /* Set the displacement bits so that we branch around
648 the unconditional branch. */
649 fragP->fr_literal[offset + 2] = 0x8;
650
651 /* Now create the unconditional branch + fixup to the
652 final target. */
653 fragP->fr_literal[offset + 3] = 0xf4;
654 fragP->fr_literal[offset + 4] = 0xe0;
655 fix_new (fragP, fragP->fr_fix + 5, 4, fragP->fr_symbol,
656 fragP->fr_offset + 2, 1, BFD_RELOC_24_PCREL);
657 fragP->fr_var = 0;
658 fragP->fr_fix += 8;
659 }
660 else if (fragP->fr_subtype == 6)
661 {
662 fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
663 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
664 fragP->fr_var = 0;
665 fragP->fr_fix += 3;
666 }
667 else if (fragP->fr_subtype == 7)
668 {
669 int offset = fragP->fr_fix;
670 int opcode = fragP->fr_literal[offset] & 0xff;
671
672 if (opcode == 0xfc)
673 {
674 fragP->fr_literal[offset] = 0xf4;
675 fragP->fr_literal[offset + 1] = 0xe0;
676 }
677 else if (opcode == 0xfd)
678 {
679 fragP->fr_literal[offset] = 0xf4;
680 fragP->fr_literal[offset + 1] = 0xe1;
681 }
682 else
683 abort ();
684
685 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
686 fragP->fr_offset + 2, 1, BFD_RELOC_24_PCREL);
687 fragP->fr_var = 0;
688 fragP->fr_fix += 5;
689 }
690 else
691 abort ();
692 }
693
694 valueT
695 md_section_align (seg, addr)
696 asection *seg;
697 valueT addr;
698 {
699 int align = bfd_get_section_alignment (stdoutput, seg);
700 return ((addr + (1 << align) - 1) & (-1 << align));
701 }
702
703 void
704 md_begin ()
705 {
706 char *prev_name = "";
707 register const struct mn10200_opcode *op;
708
709 mn10200_hash = hash_new();
710
711 /* Insert unique names into hash table. The MN10200 instruction set
712 has many identical opcode names that have different opcodes based
713 on the operands. This hash table then provides a quick index to
714 the first opcode with a particular name in the opcode table. */
715
716 op = mn10200_opcodes;
717 while (op->name)
718 {
719 if (strcmp (prev_name, op->name))
720 {
721 prev_name = (char *) op->name;
722 hash_insert (mn10200_hash, op->name, (char *) op);
723 }
724 op++;
725 }
726
727 /* This is both a simplification (we don't have to write md_apply_fix)
728 and support for future optimizations (branch shortening and similar
729 stuff in the linker. */
730 linkrelax = 1;
731 }
732
733 void
734 md_assemble (str)
735 char *str;
736 {
737 char *s;
738 struct mn10200_opcode *opcode;
739 struct mn10200_opcode *next_opcode;
740 const unsigned char *opindex_ptr;
741 int next_opindex, relaxable;
742 unsigned long insn, extension, size = 0;
743 char *f;
744 int i;
745 int match;
746
747 /* Get the opcode. */
748 for (s = str; *s != '\0' && ! isspace (*s); s++)
749 ;
750 if (*s != '\0')
751 *s++ = '\0';
752
753 /* find the first opcode with the proper name */
754 opcode = (struct mn10200_opcode *)hash_find (mn10200_hash, str);
755 if (opcode == NULL)
756 {
757 as_bad ("Unrecognized opcode: `%s'", str);
758 return;
759 }
760
761 str = s;
762 while (isspace (*str))
763 ++str;
764
765 input_line_pointer = str;
766
767 for(;;)
768 {
769 const char *errmsg = NULL;
770 int op_idx;
771 char *hold;
772 int extra_shift = 0;
773
774 relaxable = 0;
775 fc = 0;
776 match = 0;
777 next_opindex = 0;
778 insn = opcode->opcode;
779 extension = 0;
780 for (op_idx = 1, opindex_ptr = opcode->operands;
781 *opindex_ptr != 0;
782 opindex_ptr++, op_idx++)
783 {
784 const struct mn10200_operand *operand;
785 expressionS ex;
786
787 if (next_opindex == 0)
788 {
789 operand = &mn10200_operands[*opindex_ptr];
790 }
791 else
792 {
793 operand = &mn10200_operands[next_opindex];
794 next_opindex = 0;
795 }
796
797 errmsg = NULL;
798
799 while (*str == ' ' || *str == ',')
800 ++str;
801
802 if (operand->flags & MN10200_OPERAND_RELAX)
803 relaxable = 1;
804
805 /* Gather the operand. */
806 hold = input_line_pointer;
807 input_line_pointer = str;
808
809 if (operand->flags & MN10200_OPERAND_PAREN)
810 {
811 if (*input_line_pointer != ')' && *input_line_pointer != '(')
812 {
813 input_line_pointer = hold;
814 str = hold;
815 goto error;
816 }
817 input_line_pointer++;
818 goto keep_going;
819 }
820 /* See if we can match the operands. */
821 else if (operand->flags & MN10200_OPERAND_DREG)
822 {
823 if (!data_register_name (&ex))
824 {
825 input_line_pointer = hold;
826 str = hold;
827 goto error;
828 }
829 }
830 else if (operand->flags & MN10200_OPERAND_AREG)
831 {
832 if (!address_register_name (&ex))
833 {
834 input_line_pointer = hold;
835 str = hold;
836 goto error;
837 }
838 }
839 else if (operand->flags & MN10200_OPERAND_PSW)
840 {
841 char *start = input_line_pointer;
842 char c = get_symbol_end ();
843
844 if (strcmp (start, "psw") != 0)
845 {
846 *input_line_pointer = c;
847 input_line_pointer = hold;
848 str = hold;
849 goto error;
850 }
851 *input_line_pointer = c;
852 goto keep_going;
853 }
854 else if (operand->flags & MN10200_OPERAND_MDR)
855 {
856 char *start = input_line_pointer;
857 char c = get_symbol_end ();
858
859 if (strcmp (start, "mdr") != 0)
860 {
861 *input_line_pointer = c;
862 input_line_pointer = hold;
863 str = hold;
864 goto error;
865 }
866 *input_line_pointer = c;
867 goto keep_going;
868 }
869 else if (data_register_name (&ex))
870 {
871 input_line_pointer = hold;
872 str = hold;
873 goto error;
874 }
875 else if (address_register_name (&ex))
876 {
877 input_line_pointer = hold;
878 str = hold;
879 goto error;
880 }
881 else if (other_register_name (&ex))
882 {
883 input_line_pointer = hold;
884 str = hold;
885 goto error;
886 }
887 else if (*str == ')' || *str == '(')
888 {
889 input_line_pointer = hold;
890 str = hold;
891 goto error;
892 }
893 else
894 {
895 expression (&ex);
896 }
897
898 switch (ex.X_op)
899 {
900 case O_illegal:
901 errmsg = "illegal operand";
902 goto error;
903 case O_absent:
904 errmsg = "missing operand";
905 goto error;
906 case O_register:
907 if ((operand->flags
908 & (MN10200_OPERAND_DREG | MN10200_OPERAND_AREG)) == 0)
909 {
910 input_line_pointer = hold;
911 str = hold;
912 goto error;
913 }
914
915 if (opcode->format == FMT_2 || opcode->format == FMT_5)
916 extra_shift = 8;
917 else if (opcode->format == FMT_3 || opcode->format == FMT_6
918 || opcode->format == FMT_7)
919 extra_shift = 16;
920 else
921 extra_shift = 0;
922
923 mn10200_insert_operand (&insn, &extension, operand,
924 ex.X_add_number, (char *) NULL,
925 0, extra_shift);
926
927 break;
928
929 case O_constant:
930 /* If this operand can be promoted, and it doesn't
931 fit into the allocated bitfield for this insn,
932 then promote it (ie this opcode does not match). */
933 if (operand->flags & MN10200_OPERAND_PROMOTE
934 && ! check_operand (insn, operand, ex.X_add_number))
935 {
936 input_line_pointer = hold;
937 str = hold;
938 goto error;
939 }
940
941 mn10200_insert_operand (&insn, &extension, operand,
942 ex.X_add_number, (char *) NULL,
943 0, 0);
944 break;
945
946 default:
947 /* If this operand can be promoted, then this opcode didn't
948 match since we can't know if it needed promotion! */
949 if (operand->flags & MN10200_OPERAND_PROMOTE)
950 {
951 input_line_pointer = hold;
952 str = hold;
953 goto error;
954 }
955
956 /* We need to generate a fixup for this expression. */
957 if (fc >= MAX_INSN_FIXUPS)
958 as_fatal ("too many fixups");
959 fixups[fc].exp = ex;
960 fixups[fc].opindex = *opindex_ptr;
961 fixups[fc].reloc = BFD_RELOC_UNUSED;
962 ++fc;
963 break;
964 }
965
966 keep_going:
967 str = input_line_pointer;
968 input_line_pointer = hold;
969
970 while (*str == ' ' || *str == ',')
971 ++str;
972
973 }
974
975 /* Make sure we used all the operands! */
976 if (*str != ',')
977 match = 1;
978
979 error:
980 if (match == 0)
981 {
982 next_opcode = opcode + 1;
983 if (!strcmp(next_opcode->name, opcode->name))
984 {
985 opcode = next_opcode;
986 continue;
987 }
988
989 as_bad ("%s", errmsg);
990 return;
991 }
992 break;
993 }
994
995 while (isspace (*str))
996 ++str;
997
998 if (*str != '\0')
999 as_bad ("junk at end of line: `%s'", str);
1000
1001 input_line_pointer = str;
1002
1003 if (opcode->format == FMT_1)
1004 size = 1;
1005 else if (opcode->format == FMT_2 || opcode->format == FMT_4)
1006 size = 2;
1007 else if (opcode->format == FMT_3 || opcode->format == FMT_5)
1008 size = 3;
1009 else if (opcode->format == FMT_6)
1010 size = 4;
1011 else if (opcode->format == FMT_7)
1012 size = 5;
1013 else
1014 abort ();
1015
1016 /* Write out the instruction. */
1017
1018 if (relaxable && fc > 0)
1019 {
1020 int type;
1021
1022 if (size == 2)
1023 type = 0;
1024 else if (size == 3
1025 && opcode->opcode != 0xfd0000 && opcode->opcode != 0xfc0000)
1026 type = 3;
1027 else
1028 type = 6;
1029 f = frag_var (rs_machine_dependent, 8, 8 - size, type,
1030 fixups[0].exp.X_add_symbol,
1031 fixups[0].exp.X_add_number,
1032 (char *)fixups[0].opindex);
1033 number_to_chars_bigendian (f, insn, size);
1034 if (8 - size > 4)
1035 {
1036 number_to_chars_bigendian (f + size, 0, 4);
1037 number_to_chars_bigendian (f + size + 4, 0, 8 - size - 4);
1038 }
1039 else
1040 number_to_chars_bigendian (f + size, 0, 8 - size);
1041 }
1042
1043 else
1044 {
1045 f = frag_more (size);
1046
1047 /* Oh, what a mess. The instruction is in big endian format, but
1048 16 and 24bit immediates are little endian! */
1049 if (opcode->format == FMT_3)
1050 {
1051 number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
1052 number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
1053 }
1054 else if (opcode->format == FMT_6)
1055 {
1056 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1057 number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
1058 }
1059 else if (opcode->format == FMT_7)
1060 {
1061 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1062 number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
1063 number_to_chars_littleendian (f + 4, extension & 0xff, 1);
1064 }
1065 else
1066 {
1067 number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
1068 }
1069
1070 /* Create any fixups. */
1071 for (i = 0; i < fc; i++)
1072 {
1073 const struct mn10200_operand *operand;
1074
1075 operand = &mn10200_operands[fixups[i].opindex];
1076 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1077 {
1078 reloc_howto_type *reloc_howto;
1079 int size;
1080 int offset;
1081 fixS *fixP;
1082
1083 reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1084
1085 if (!reloc_howto)
1086 abort();
1087
1088 size = bfd_get_reloc_size (reloc_howto);
1089
1090 if (size < 1 || size > 4)
1091 abort();
1092
1093 offset = 4 - size;
1094 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1095 size,
1096 &fixups[i].exp,
1097 reloc_howto->pc_relative,
1098 fixups[i].reloc);
1099 }
1100 else
1101 {
1102 int reloc, pcrel, reloc_size, offset;
1103 fixS *fixP;
1104
1105 reloc = BFD_RELOC_NONE;
1106 /* How big is the reloc? Remember SPLIT relocs are
1107 implicitly 32bits. */
1108 reloc_size = operand->bits;
1109
1110 offset = size - reloc_size / 8;
1111
1112 /* Is the reloc pc-relative? */
1113 pcrel = (operand->flags & MN10200_OPERAND_PCREL) != 0;
1114
1115
1116 /* Choose a proper BFD relocation type. */
1117 if (pcrel)
1118 {
1119 if (reloc_size == 8)
1120 reloc = BFD_RELOC_8_PCREL;
1121 else if (reloc_size == 24)
1122 reloc = BFD_RELOC_24_PCREL;
1123 else
1124 abort ();
1125 }
1126 else
1127 {
1128 if (reloc_size == 32)
1129 reloc = BFD_RELOC_32;
1130 else if (reloc_size == 16)
1131 reloc = BFD_RELOC_16;
1132 else if (reloc_size == 8)
1133 reloc = BFD_RELOC_8;
1134 else if (reloc_size == 24)
1135 reloc = BFD_RELOC_24;
1136 else
1137 abort ();
1138 }
1139
1140 /* Convert the size of the reloc into what fix_new_exp wants. */
1141 reloc_size = reloc_size / 8;
1142 if (reloc_size == 8)
1143 reloc_size = 0;
1144 else if (reloc_size == 16)
1145 reloc_size = 1;
1146 else if (reloc_size == 32 || reloc_size == 24)
1147 reloc_size = 2;
1148
1149 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1150 reloc_size, &fixups[i].exp, pcrel,
1151 ((bfd_reloc_code_real_type) reloc));
1152 if (pcrel)
1153 fixP->fx_offset += offset;
1154 }
1155 }
1156 }
1157 }
1158
1159
1160 /* if while processing a fixup, a reloc really needs to be created */
1161 /* then it is done here */
1162
1163 arelent *
1164 tc_gen_reloc (seg, fixp)
1165 asection *seg;
1166 fixS *fixp;
1167 {
1168 arelent *reloc;
1169 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
1170
1171 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1172 if (reloc->howto == (reloc_howto_type *) NULL)
1173 {
1174 as_bad_where (fixp->fx_file, fixp->fx_line,
1175 "reloc %d not supported by object file format",
1176 (int)fixp->fx_r_type);
1177 return NULL;
1178 }
1179 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1180
1181 if (fixp->fx_addsy && fixp->fx_subsy)
1182 {
1183 reloc->sym_ptr_ptr = &bfd_abs_symbol;
1184 reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
1185 - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
1186 }
1187 else
1188 {
1189 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1190 reloc->addend = fixp->fx_offset;
1191 }
1192 return reloc;
1193 }
1194
1195 int
1196 md_estimate_size_before_relax (fragp, seg)
1197 fragS *fragp;
1198 asection *seg;
1199 {
1200 if (fragp->fr_subtype == 0)
1201 return 2;
1202 if (fragp->fr_subtype == 3)
1203 return 3;
1204 if (fragp->fr_subtype == 6)
1205 {
1206 if (!S_IS_DEFINED (fragp->fr_symbol))
1207 {
1208 fragp->fr_subtype = 7;
1209 return 5;
1210 }
1211 }
1212 return 3;
1213 }
1214
1215 long
1216 md_pcrel_from (fixp)
1217 fixS *fixp;
1218 {
1219 return fixp->fx_frag->fr_address;
1220 #if 0
1221 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1222 {
1223 /* The symbol is undefined. Let the linker figure it out. */
1224 return 0;
1225 }
1226 return fixp->fx_frag->fr_address + fixp->fx_where;
1227 #endif
1228 }
1229
1230 int
1231 md_apply_fix3 (fixp, valuep, seg)
1232 fixS *fixp;
1233 valueT *valuep;
1234 segT seg;
1235 {
1236 /* We shouldn't ever get here because linkrelax is nonzero. */
1237 abort ();
1238 fixp->fx_done = 1;
1239 return 0;
1240 }
1241
1242 /* Insert an operand value into an instruction. */
1243
1244 static void
1245 mn10200_insert_operand (insnp, extensionp, operand, val, file, line, shift)
1246 unsigned long *insnp;
1247 unsigned long *extensionp;
1248 const struct mn10200_operand *operand;
1249 offsetT val;
1250 char *file;
1251 unsigned int line;
1252 unsigned int shift;
1253 {
1254 /* No need to check 24 or 32bit operands for a bit. */
1255 if (operand->bits < 24
1256 && (operand->flags & MN10200_OPERAND_NOCHECK) == 0)
1257 {
1258 long min, max;
1259 offsetT test;
1260
1261 if ((operand->flags & MN10200_OPERAND_SIGNED) != 0)
1262 {
1263 max = (1 << (operand->bits - 1)) - 1;
1264 min = - (1 << (operand->bits - 1));
1265 }
1266 else
1267 {
1268 max = (1 << operand->bits) - 1;
1269 min = 0;
1270 }
1271
1272 test = val;
1273
1274
1275 if (test < (offsetT) min || test > (offsetT) max)
1276 {
1277 const char *err =
1278 "operand out of range (%s not between %ld and %ld)";
1279 char buf[100];
1280
1281 sprint_value (buf, test);
1282 if (file == (char *) NULL)
1283 as_warn (err, buf, min, max);
1284 else
1285 as_warn_where (file, line, err, buf, min, max);
1286 }
1287 }
1288
1289 if ((operand->flags & MN10200_OPERAND_EXTENDED) == 0)
1290 {
1291 *insnp |= (((long) val & ((1 << operand->bits) - 1))
1292 << (operand->shift + shift));
1293
1294 if ((operand->flags & MN10200_OPERAND_REPEATED) != 0)
1295 *insnp |= (((long) val & ((1 << operand->bits) - 1))
1296 << (operand->shift + shift + 2));
1297 }
1298 else
1299 {
1300 *extensionp |= (val >> 16) & 0xff;
1301 *insnp |= val & 0xffff;
1302 }
1303 }
1304
1305 static unsigned long
1306 check_operand (insn, operand, val)
1307 unsigned long insn;
1308 const struct mn10200_operand *operand;
1309 offsetT val;
1310 {
1311 /* No need to check 24bit or 32bit operands for a bit. */
1312 if (operand->bits < 24
1313 && (operand->flags & MN10200_OPERAND_NOCHECK) == 0)
1314 {
1315 long min, max;
1316 offsetT test;
1317
1318 if ((operand->flags & MN10200_OPERAND_SIGNED) != 0)
1319 {
1320 max = (1 << (operand->bits - 1)) - 1;
1321 min = - (1 << (operand->bits - 1));
1322 }
1323 else
1324 {
1325 max = (1 << operand->bits) - 1;
1326 min = 0;
1327 }
1328
1329 test = val;
1330
1331
1332 if (test < (offsetT) min || test > (offsetT) max)
1333 return 0;
1334 else
1335 return 1;
1336 }
1337 return 1;
1338 }