Add support for curly brace register list syntax.
[deliverable/binutils-gdb.git] / gas / config / tc-v850.c
1 /* tc-v850.c -- Assembler code for the NEC V850
2 Copyright (C) 1996, 1997 Free Software Foundation.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "as.h"
24 #include "subsegs.h"
25 #include "opcode/v850.h"
26
27 /* sign-extend a 16-bit number */
28 #define SEXT16(x) ((((x) & 0xffff) ^ (~ 0x7fff)) + 0x8000)
29
30 /* Temporarily holds the reloc in a cons expression. */
31 static bfd_reloc_code_real_type hold_cons_reloc;
32 \f
33 /* Structure to hold information about predefined registers. */
34 struct reg_name
35 {
36 const char *name;
37 int value;
38 };
39
40 /* Generic assembler global variables which must be defined by all targets. */
41
42 /* Characters which always start a comment. */
43 const char comment_chars[] = "#";
44
45 /* Characters which start a comment at the beginning of a line. */
46 const char line_comment_chars[] = ";#";
47
48 /* Characters which may be used to separate multiple commands on a
49 single line. */
50 const char line_separator_chars[] = ";";
51
52 /* Characters which are used to indicate an exponent in a floating
53 point number. */
54 const char EXP_CHARS[] = "eE";
55
56 /* Characters which mean that a number is a floating point constant,
57 as in 0d1.0. */
58 const char FLT_CHARS[] = "dD";
59 \f
60
61 const relax_typeS md_relax_table[] = {
62 {0xff, -0x100, 2, 1},
63 {0x1fffff, -0x200000, 6, 0},
64 };
65
66
67 /* local functions */
68 static unsigned long v850_insert_operand
69 PARAMS ((unsigned long insn, const struct v850_operand *operand,
70 offsetT val, char *file, unsigned int line));
71
72
73 /* fixups */
74 #define MAX_INSN_FIXUPS (5)
75 struct v850_fixup
76 {
77 expressionS exp;
78 int opindex;
79 bfd_reloc_code_real_type reloc;
80 };
81 struct v850_fixup fixups[MAX_INSN_FIXUPS];
82 static int fc;
83 \f
84 static void
85 v850_section (int arg)
86 {
87 char saved_c;
88 char * ptr;
89
90 for (ptr = input_line_pointer; * ptr != '\n' && * ptr != 0; ptr ++)
91 if (* ptr == ',' && ptr[1] == '.')
92 break;
93
94 saved_c = * ptr;
95 * ptr = ';';
96
97 obj_elf_section (arg);
98
99 * ptr = saved_c;
100 }
101
102 void
103 v850_bss (int ignore)
104 {
105 register int temp = get_absolute_expression ();
106
107 subseg_set (bss_section, (subsegT) temp);
108
109 demand_empty_rest_of_line ();
110 }
111
112 void
113 v850_offset (int ignore)
114 {
115 register int temp = get_absolute_expression ();
116
117 subseg_set (now_seg, (subsegT) temp);
118
119 demand_empty_rest_of_line ();
120 }
121
122 /* The target specific pseudo-ops which we support. */
123 const pseudo_typeS md_pseudo_table[] =
124 {
125 {"bss", v850_bss, 0},
126 {"offset", v850_offset, 0},
127 {"section", v850_section, 0},
128 {"word", cons, 4},
129 { NULL, NULL, 0}
130 };
131
132 /* Opcode hash table. */
133 static struct hash_control *v850_hash;
134
135 /* This table is sorted. Suitable for searching by a binary search. */
136 static const struct reg_name pre_defined_registers[] =
137 {
138 { "ep", 30 }, /* ep - element ptr */
139 { "gp", 4 }, /* gp - global ptr */
140 { "lp", 31 }, /* lp - link ptr */
141 { "r0", 0 },
142 { "r1", 1 },
143 { "r10", 10 },
144 { "r11", 11 },
145 { "r12", 12 },
146 { "r13", 13 },
147 { "r14", 14 },
148 { "r15", 15 },
149 { "r16", 16 },
150 { "r17", 17 },
151 { "r18", 18 },
152 { "r19", 19 },
153 { "r2", 2 },
154 { "r20", 20 },
155 { "r21", 21 },
156 { "r22", 22 },
157 { "r23", 23 },
158 { "r24", 24 },
159 { "r25", 25 },
160 { "r26", 26 },
161 { "r27", 27 },
162 { "r28", 28 },
163 { "r29", 29 },
164 { "r3", 3 },
165 { "r30", 30 },
166 { "r31", 31 },
167 { "r4", 4 },
168 { "r5", 5 },
169 { "r6", 6 },
170 { "r7", 7 },
171 { "r8", 8 },
172 { "r9", 9 },
173 { "sp", 3 }, /* sp - stack ptr */
174 { "tp", 5 }, /* tp - text ptr */
175 { "zero", 0 },
176 };
177 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct reg_name))
178
179
180 static const struct reg_name system_registers[] =
181 {
182 /* start-sanitize-v850e */
183 { "ctbp", 20 },
184 { "ctpc", 16 },
185 { "ctpsw", 17 },
186 { "dbpc", 18 },
187 { "dbpsw", 19 },
188 /* end-sanitize-v850e */
189 { "ecr", 4 },
190 { "eipc", 0 },
191 { "eipsw", 1 },
192 { "fepc", 2 },
193 { "fepsw", 3 },
194 { "psw", 5 },
195 };
196 #define SYSREG_NAME_CNT (sizeof (system_registers) / sizeof (struct reg_name))
197
198 static const struct reg_name cc_names[] =
199 {
200 { "c", 0x1 },
201 { "e", 0x2 },
202 { "ge", 0xe },
203 { "gt", 0xf },
204 { "h", 0xb },
205 { "l", 0x1 },
206 { "le", 0x7 },
207 { "lt", 0x6 },
208 { "n", 0x4 },
209 { "nc", 0x9 },
210 { "ne", 0xa },
211 { "nh", 0x3 },
212 { "nl", 0x9 },
213 { "ns", 0xc },
214 { "nv", 0x8 },
215 { "nz", 0xa },
216 { "p", 0xc },
217 { "s", 0x4 },
218 { "sa", 0xd },
219 { "t", 0x5 },
220 { "v", 0x0 },
221 { "z", 0x2 },
222 };
223 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
224
225 /* reg_name_search does a binary search of the given register table
226 to see if "name" is a valid regiter name. Returns the register
227 number from the array on success, or -1 on failure. */
228
229 static int
230 reg_name_search (regs, regcount, name)
231 const struct reg_name *regs;
232 int regcount;
233 const char *name;
234 {
235 int middle, low, high;
236 int cmp;
237
238 low = 0;
239 high = regcount - 1;
240
241 do
242 {
243 middle = (low + high) / 2;
244 cmp = strcasecmp (name, regs[middle].name);
245 if (cmp < 0)
246 high = middle - 1;
247 else if (cmp > 0)
248 low = middle + 1;
249 else
250 return regs[middle].value;
251 }
252 while (low <= high);
253 return -1;
254 }
255
256
257 /* Summary of register_name().
258 *
259 * in: Input_line_pointer points to 1st char of operand.
260 *
261 * out: A expressionS.
262 * The operand may have been a register: in this case, X_op == O_register,
263 * X_add_number is set to the register number, and truth is returned.
264 * Input_line_pointer->(next non-blank) char after operand, or is in
265 * its original state.
266 */
267 static boolean
268 register_name (expressionP)
269 expressionS *expressionP;
270 {
271 int reg_number;
272 char *name;
273 char *start;
274 char c;
275
276 /* Find the spelling of the operand */
277 start = name = input_line_pointer;
278
279 c = get_symbol_end ();
280
281 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
282
283 * input_line_pointer = c; /* put back the delimiting char */
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
295 return true;
296 }
297 else
298 {
299 /* reset the line as if we had not done anything */
300 input_line_pointer = start;
301
302 return false;
303 }
304 }
305
306 /* Summary of system_register_name().
307 *
308 * in: Input_line_pointer points to 1st char of operand.
309 *
310 * out: A expressionS.
311 * The operand may have been a register: in this case, X_op == O_register,
312 * X_add_number is set to the register number, and truth is returned.
313 * Input_line_pointer->(next non-blank) char after operand, or is in
314 * its original state.
315 */
316 static boolean
317 system_register_name (expressionP, accept_numbers)
318 expressionS * expressionP;
319 boolean accept_numbers;
320 {
321 int reg_number;
322 char *name;
323 char *start;
324 char c;
325
326 /* Find the spelling of the operand */
327 start = name = input_line_pointer;
328
329 c = get_symbol_end ();
330 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
331
332 * input_line_pointer = c; /* put back the delimiting char */
333
334 if (reg_number < 0
335 && accept_numbers)
336 {
337 input_line_pointer = start; /* reset input_line pointer */
338
339 if (isdigit (* input_line_pointer))
340 reg_number = strtol (input_line_pointer, & input_line_pointer, 10);
341
342 /* Make sure that the register number is allowable. */
343 if ( reg_number < 0
344 || reg_number > 5
345 /* start-sanitize-v850e */
346 && reg_number < 16
347 || reg_number > 20
348 /* end-sanitize-v850e */
349 )
350 {
351 reg_number = -1;
352 }
353 }
354
355 /* look to see if it's in the register table */
356 if (reg_number >= 0)
357 {
358 expressionP->X_op = O_register;
359 expressionP->X_add_number = reg_number;
360
361 /* make the rest nice */
362 expressionP->X_add_symbol = NULL;
363 expressionP->X_op_symbol = NULL;
364
365 return true;
366 }
367 else
368 {
369 /* reset the line as if we had not done anything */
370 input_line_pointer = start;
371
372 return false;
373 }
374 }
375
376 /* Summary of cc_name().
377 *
378 * in: Input_line_pointer points to 1st char of operand.
379 *
380 * out: A expressionS.
381 * The operand may have been a register: in this case, X_op == O_register,
382 * X_add_number is set to the register number, and truth is returned.
383 * Input_line_pointer->(next non-blank) char after operand, or is in
384 * its original state.
385 */
386 static boolean
387 cc_name (expressionP)
388 expressionS *expressionP;
389 {
390 int reg_number;
391 char *name;
392 char *start;
393 char c;
394
395 /* Find the spelling of the operand */
396 start = name = input_line_pointer;
397
398 c = get_symbol_end ();
399 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
400
401 * input_line_pointer = c; /* put back the delimiting char */
402
403 /* look to see if it's in the register table */
404 if (reg_number >= 0)
405 {
406 expressionP->X_op = O_constant;
407 expressionP->X_add_number = reg_number;
408
409 /* make the rest nice */
410 expressionP->X_add_symbol = NULL;
411 expressionP->X_op_symbol = NULL;
412
413 return true;
414 }
415 else
416 {
417 /* reset the line as if we had not done anything */
418 input_line_pointer = start;
419
420 return false;
421 }
422 }
423
424 static void
425 skip_white_space (void)
426 {
427 while ( * input_line_pointer == ' '
428 || * input_line_pointer == '\t')
429 ++ input_line_pointer;
430 }
431
432 /* start-sanitize-v850e */
433 /* Summary of parse_register_list ().
434 *
435 * in: Input_line_pointer points to 1st char of a list of registers.
436 * insn is the partially constructed instruction.
437 * operand is the operand being inserted.
438 *
439 * out: True if the parse completed successfully, False otherwise.
440 * If the parse completes the correct bit fields in the
441 * instruction will be filled in.
442 */
443 static boolean
444 parse_register_list
445 (
446 unsigned long * insn,
447 const struct v850_operand * operand
448 )
449 {
450 static int type1_regs[ 32 ] = { 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24 };
451 /* start-sanitize-v850eq */
452 static int type2_regs[ 32 ] = { 19, 18, 17, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24 };
453 static int type3_regs[ 32 ] = { 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 15, 13, 12, 7, 6, 5, 4, 11, 10, 9, 8 };
454 /* end-sanitize-v850eq */
455 int * regs;
456
457 /* Select a register array to parse. */
458 switch (operand->shift)
459 {
460 case 0xffe00001: regs = type1_regs; break;
461 /* start-sanitize-v850eq */
462 case 0xfff8000f: regs = type2_regs; break;
463 case 0xfff8001f: regs = type3_regs; break;
464 /* end-sanitize-v850eq */
465 default:
466 as_bad ("unknown operand shift: %x\n", operand->shift );
467 return false;
468 }
469
470 skip_white_space();
471
472 if (* input_line_pointer != '{')
473 {
474 as_bad ("no opening curly brace at start of register list\n");
475 return false;
476 }
477
478 input_line_pointer ++;
479
480 /* Parse the register list until a terminator (closing curly brace or new-line) is found. */
481 for (;;)
482 {
483 expressionS exp;
484
485 if (register_name (& exp))
486 {
487 int i;
488
489 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
490 for (i = 0; i < 32; i++)
491 {
492 if (regs[ i ] == exp.X_add_number)
493 {
494 * insn |= (1 << i);
495 break;
496 }
497 }
498
499 if (i == 32)
500 {
501 as_bad( "it is illegal to include register r%d in list\n", exp.X_add_number );
502 return false;
503 }
504 }
505 else if (system_register_name (& exp, true))
506 {
507 if (regs == type1_regs)
508 {
509 as_bad ("system registers cannot be included in this register list" );
510 return false;
511 }
512 else if (exp.X_add_number == 5)
513 {
514 if (regs == type2_regs)
515 as_bad ("PSW cannot be included in this register list" );
516 else
517 * insn |= 0x8;
518 }
519 else
520 * insn |= 0x80000;
521 }
522 else if (* input_line_pointer == '}')
523 {
524 input_line_pointer ++;
525 break;
526 }
527 else if (* input_line_pointer == ',')
528 {
529 input_line_pointer ++;
530 continue;
531 }
532 else if (* input_line_pointer == '-')
533 {
534 /* We have encountered a range of registers: rX - rY */
535 int j;
536 expressionS exp2;
537
538 /* Skip the dash. */
539 ++ input_line_pointer;
540
541 /* Get the second register in the range. */
542 if (! register_name (& exp2))
543 {
544 as_bad ("second register should follow dash in register list\n");
545 exp2.X_add_number = exp.X_add_number;
546 }
547
548 /* Add the rest of the registers in the range. */
549 for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
550 {
551 int i;
552
553 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
554 for (i = 0; i < 32; i++)
555 {
556 if (regs[ i ] == j)
557 {
558 * insn |= (1 << i);
559 break;
560 }
561 }
562
563 if (i == 32)
564 {
565 as_bad( "it is illegal to include register r%d in list\n", j );
566 return false;
567 }
568 }
569 }
570 else
571 {
572 break;
573 }
574
575 skip_white_space();
576 }
577
578 return true;
579 }
580 /* end-sanitize-v850e */
581
582 CONST char * md_shortopts = "m:";
583
584 struct option md_longopts[] =
585 {
586 {NULL, no_argument, NULL, 0}
587 };
588 size_t md_longopts_size = sizeof md_longopts;
589
590
591 void
592 md_show_usage (stream)
593 FILE *stream;
594 {
595 fprintf (stream, "V850 options:\n");
596 fprintf (stream, "\tnone at present\n");
597 }
598
599 int
600 md_parse_option (c, arg)
601 int c;
602 char * arg;
603 {
604 return 0;
605 }
606
607 symbolS *
608 md_undefined_symbol (name)
609 char *name;
610 {
611 return 0;
612 }
613
614 char *
615 md_atof (type, litp, sizep)
616 int type;
617 char *litp;
618 int *sizep;
619 {
620 int prec;
621 LITTLENUM_TYPE words[4];
622 char *t;
623 int i;
624
625 switch (type)
626 {
627 case 'f':
628 prec = 2;
629 break;
630
631 case 'd':
632 prec = 4;
633 break;
634
635 default:
636 *sizep = 0;
637 return "bad call to md_atof";
638 }
639
640 t = atof_ieee (input_line_pointer, type, words);
641 if (t)
642 input_line_pointer = t;
643
644 *sizep = prec * 2;
645
646 for (i = prec - 1; i >= 0; i--)
647 {
648 md_number_to_chars (litp, (valueT) words[i], 2);
649 litp += 2;
650 }
651
652 return NULL;
653 }
654
655
656 /* Very gross. */
657 void
658 md_convert_frag (abfd, sec, fragP)
659 bfd *abfd;
660 asection *sec;
661 fragS *fragP;
662 {
663 subseg_change (sec, 0);
664 if (fragP->fr_subtype == 0)
665 {
666 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
667 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
668 fragP->fr_var = 0;
669 fragP->fr_fix += 2;
670 }
671 else if (fragP->fr_subtype == 1)
672 {
673 /* Reverse the condition of the first branch. */
674 fragP->fr_literal[0] &= 0xf7;
675 /* Mask off all the displacement bits. */
676 fragP->fr_literal[0] &= 0x8f;
677 fragP->fr_literal[1] &= 0x07;
678 /* Now set the displacement bits so that we branch
679 around the unconditional branch. */
680 fragP->fr_literal[0] |= 0x30;
681
682 /* Now create the unconditional branch + fixup to the final
683 target. */
684 md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
685 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
686 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
687 fragP->fr_var = 0;
688 fragP->fr_fix += 6;
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 v850_opcode *op;
708
709 v850_hash = hash_new();
710
711 /* Insert unique names into hash table. The V850 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 = v850_opcodes;
717 while (op->name)
718 {
719 if (strcmp (prev_name, op->name))
720 {
721 prev_name = (char *) op->name;
722 hash_insert (v850_hash, op->name, (char *) op);
723 }
724 op++;
725 }
726 }
727
728 static bfd_reloc_code_real_type
729 v850_reloc_prefix ()
730 {
731 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
732 {
733 input_line_pointer += 3;
734 return BFD_RELOC_HI16;
735 }
736 if (strncmp(input_line_pointer, "hi(", 3) == 0)
737 {
738 input_line_pointer += 2;
739 return BFD_RELOC_HI16_S;
740 }
741 if (strncmp (input_line_pointer, "lo(", 3) == 0)
742 {
743 input_line_pointer += 2;
744 return BFD_RELOC_LO16;
745 }
746
747 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
748 {
749 input_line_pointer += 6;
750 return BFD_RELOC_V850_SDA_OFFSET;
751 }
752
753 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
754 {
755 input_line_pointer += 6;
756 return BFD_RELOC_V850_ZDA_OFFSET;
757 }
758
759 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
760 {
761 input_line_pointer += 6;
762 return BFD_RELOC_V850_TDA_OFFSET;
763 }
764
765 /* Disgusting */
766 if (strncmp(input_line_pointer, "(hi0(", 5) == 0)
767 {
768 input_line_pointer += 4;
769 return BFD_RELOC_HI16;
770 }
771 if (strncmp(input_line_pointer, "(hi(", 4) == 0)
772 {
773 input_line_pointer += 3;
774 return BFD_RELOC_HI16_S;
775 }
776 if (strncmp (input_line_pointer, "(lo(", 4) == 0)
777 {
778 input_line_pointer += 3;
779 return BFD_RELOC_LO16;
780 }
781
782 if (strncmp (input_line_pointer, "(sdaoff(", 8) == 0)
783 {
784 input_line_pointer += 7;
785 return BFD_RELOC_V850_SDA_OFFSET;
786 }
787
788 if (strncmp (input_line_pointer, "(zdaoff(", 8) == 0)
789 {
790 input_line_pointer += 7;
791 return BFD_RELOC_V850_ZDA_OFFSET;
792 }
793
794 if (strncmp (input_line_pointer, "(tdaoff(", 8) == 0)
795 {
796 input_line_pointer += 7;
797 return BFD_RELOC_V850_TDA_OFFSET;
798 }
799
800 return BFD_RELOC_UNUSED;
801 }
802
803 void
804 md_assemble (str)
805 char * str;
806 {
807 char * s;
808 char * start_of_operands;
809 struct v850_opcode * opcode;
810 struct v850_opcode * next_opcode;
811 const unsigned char * opindex_ptr;
812 int next_opindex;
813 int relaxable;
814 unsigned long insn;
815 unsigned long insn_size;
816 char * f;
817 int i;
818 int match;
819 bfd_reloc_code_real_type reloc;
820 boolean extra_data_after_insn = false;
821 unsigned extra_data_len;
822 unsigned long extra_data;
823
824
825 /* Get the opcode. */
826 for (s = str; *s != '\0' && ! isspace (*s); s++)
827 continue;
828
829 if (*s != '\0')
830 *s++ = '\0';
831
832 /* find the first opcode with the proper name */
833 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
834 if (opcode == NULL)
835 {
836 as_bad ("Unrecognized opcode: `%s'", str);
837 ignore_rest_of_line ();
838 return;
839 }
840
841 str = s;
842 while (isspace (*str))
843 ++str;
844
845 start_of_operands = str;
846
847 for (;;)
848 {
849 const char * errmsg = NULL;
850
851 relaxable = 0;
852 fc = 0;
853 match = 0;
854 next_opindex = 0;
855 insn = opcode->opcode;
856 extra_data_after_insn = false;
857
858 input_line_pointer = str = start_of_operands;
859
860 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
861 {
862 const struct v850_operand * operand;
863 char * hold;
864 expressionS ex;
865
866 if (next_opindex == 0)
867 {
868 operand = & v850_operands[ * opindex_ptr ];
869 }
870 else
871 {
872 operand = & v850_operands[ next_opindex ];
873 next_opindex = 0;
874 }
875
876 errmsg = NULL;
877
878 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
879 ++str;
880
881 if (operand->flags & V850_OPERAND_RELAX)
882 relaxable = 1;
883
884 /* Gather the operand. */
885 hold = input_line_pointer;
886 input_line_pointer = str;
887
888 //fprintf (stderr, "operand: %s index = %d, opcode = %s\n", input_line_pointer, opindex_ptr - opcode->operands, opcode->name );
889
890 /* lo(), hi(), hi0(), etc... */
891 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
892 {
893 expression (& ex);
894
895 if (ex.X_op == O_constant)
896 {
897 switch (reloc)
898 {
899 case BFD_RELOC_LO16:
900 {
901 /* Truncate, then sign extend the value. */
902 ex.X_add_number = SEXT16 (ex.X_add_number);
903 break;
904 }
905
906 case BFD_RELOC_HI16:
907 {
908 /* Truncate, then sign extend the value. */
909 ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
910 break;
911 }
912
913 case BFD_RELOC_HI16_S:
914 {
915 /* Truncate, then sign extend the value. */
916 int temp = (ex.X_add_number >> 16) & 0xffff;
917
918 temp += (ex.X_add_number >> 15) & 1;
919
920 ex.X_add_number = SEXT16 (temp);
921 break;
922 }
923
924 default:
925 break;
926 }
927
928 insn = v850_insert_operand (insn, operand, ex.X_add_number,
929 (char *) NULL, 0);
930 }
931 else
932 {
933 if (fc > MAX_INSN_FIXUPS)
934 as_fatal ("too many fixups");
935
936 /* Adjust any offsets for sst.{h,w}/sld.{h,w} instructions */
937 if (operand->flags & V850_OPERAND_ADJUST_SHORT_MEMORY)
938 ex.X_add_number >>= 1;
939
940 fixups[fc].exp = ex;
941 fixups[fc].opindex = *opindex_ptr;
942 fixups[fc].reloc = reloc;
943 fc++;
944 }
945 }
946 else
947 {
948 errmsg = NULL;
949
950 if ((operand->flags & V850_OPERAND_REG) != 0)
951 {
952 if (!register_name (& ex))
953 {
954 errmsg = "invalid register name";
955 }
956
957 if ((operand->flags & V850_NOT_R0)
958 && ex.X_add_number == 0)
959 {
960 errmsg = "register r0 cannot be used here";
961 }
962 }
963 else if ((operand->flags & V850_OPERAND_SRG) != 0)
964 {
965 if (!system_register_name (& ex, true))
966 {
967 errmsg = "UGG invalid system register name";
968 }
969 }
970 else if ((operand->flags & V850_OPERAND_EP) != 0)
971 {
972 char * start = input_line_pointer;
973 char c = get_symbol_end ();
974
975 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
976 {
977 /* Put things back the way we found them. */
978 *input_line_pointer = c;
979 input_line_pointer = start;
980 errmsg = "expected EP register";
981 goto error;
982 }
983
984 *input_line_pointer = c;
985 str = input_line_pointer;
986 input_line_pointer = hold;
987
988 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
989 ++str;
990 continue;
991 }
992 else if ((operand->flags & V850_OPERAND_CC) != 0)
993 {
994 if (!cc_name (& ex))
995 {
996 errmsg = "invalid condition code name";
997 }
998 }
999 else if (operand->flags & V850E_PUSH_POP)
1000 {
1001 if (! parse_register_list (& insn, operand))
1002 {
1003 errmsg = "invalid register list";
1004 }
1005
1006 /* The parse_register_list() function has already done everything, so fake a dummy expression. */
1007 ex.X_op = O_constant;
1008 ex.X_add_number = 0;
1009 }
1010 else if (operand->flags & V850E_IMMEDIATE16)
1011 {
1012 expression (& ex);
1013
1014 if (ex.X_op != O_constant)
1015 errmsg = "constant expression expected";
1016 else if (ex.X_add_number & 0xffff0000)
1017 {
1018 if (ex.X_add_number & 0xffff)
1019 errmsg = "constant too big to fit into instruction";
1020 else if ((insn & 0x001fffc0) == 0x00130780)
1021 ex.X_add_number >>= 16;
1022 else
1023 errmsg = "constant too big to fit into instruction";
1024 }
1025
1026 extra_data_after_insn = true;
1027 extra_data_len = 2;
1028 extra_data = ex.X_add_number;
1029 ex.X_add_number = 0;
1030 }
1031 else if (operand->flags & V850E_IMMEDIATE32)
1032 {
1033 expression (& ex);
1034
1035 if (ex.X_op != O_constant)
1036 errmsg = "constant expression expected";
1037
1038 extra_data_after_insn = true;
1039 extra_data_len = 4;
1040 extra_data = ex.X_add_number;
1041 ex.X_add_number = 0;
1042 }
1043 else if (register_name (&ex)
1044 && (operand->flags & V850_OPERAND_REG) == 0)
1045 {
1046 errmsg = "syntax error: register not expected";
1047 }
1048 else if (system_register_name (& ex, false)
1049 && (operand->flags & V850_OPERAND_SRG) == 0)
1050 {
1051 errmsg = "syntax error: system register not expected";
1052 }
1053 else if (cc_name (&ex)
1054 && (operand->flags & V850_OPERAND_CC) == 0)
1055 {
1056 errmsg = "syntax error: condition code not expected";
1057 }
1058 else
1059 {
1060 expression (& ex);
1061 /* start-sanitize-v850e */
1062 /* Special case:
1063 If we are assembling a MOV instruction (or a CALLT.... :-)
1064 and the immediate value does not fit into the bits available
1065 then create a fake error so that the next MOV instruction
1066 will be selected. This one has a 32 bit immediate field. */
1067
1068 if (((insn & 0x07e0) == 0x0200)
1069 && ex.X_op == O_constant
1070 && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1071 errmsg = "use bigger instruction";
1072 /* end-sanitize-v850e */
1073 }
1074
1075 if (errmsg)
1076 goto error;
1077
1078 //fprintf (stderr, "insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number );
1079
1080 switch (ex.X_op)
1081 {
1082 case O_illegal:
1083 errmsg = "illegal operand";
1084 goto error;
1085 case O_absent:
1086 errmsg = "missing operand";
1087 goto error;
1088 case O_register:
1089 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1090 {
1091 errmsg = "invalid operand";
1092 goto error;
1093 }
1094 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1095 (char *) NULL, 0);
1096 break;
1097
1098 case O_constant:
1099 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1100 (char *) NULL, 0);
1101 break;
1102
1103 default:
1104 /* We need to generate a fixup for this expression. */
1105 if (fc >= MAX_INSN_FIXUPS)
1106 as_fatal ("too many fixups");
1107 fixups[fc].exp = ex;
1108 fixups[fc].opindex = *opindex_ptr;
1109 fixups[fc].reloc = BFD_RELOC_UNUSED;
1110 ++fc;
1111 break;
1112 }
1113 }
1114
1115 str = input_line_pointer;
1116 input_line_pointer = hold;
1117
1118 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1119 || *str == ')')
1120 ++str;
1121 }
1122 match = 1;
1123
1124 error:
1125 if (match == 0)
1126 {
1127 next_opcode = opcode + 1;
1128 if (next_opcode->opcode != 0 && !strcmp (next_opcode->name, opcode->name))
1129 {
1130 opcode = next_opcode;
1131 continue;
1132 }
1133
1134 as_bad ("%s", errmsg);
1135 ignore_rest_of_line ();
1136 return;
1137 }
1138 break;
1139 }
1140
1141 while (isspace (*str))
1142 ++str;
1143
1144 if (*str != '\0')
1145 as_bad ("junk at end of line: `%s'", str);
1146
1147 input_line_pointer = str;
1148
1149 /* Write out the instruction.
1150
1151 Four byte insns have an opcode with the two high bits on. */
1152 if (relaxable && fc > 0)
1153 {
1154 f = frag_var (rs_machine_dependent, 6, 4, 0,
1155 fixups[0].exp.X_add_symbol,
1156 fixups[0].exp.X_add_number,
1157 (char *)fixups[0].opindex);
1158 insn_size = 2;
1159 md_number_to_chars (f, insn, insn_size);
1160 md_number_to_chars (f + 2, 0, 4);
1161 fc = 0;
1162 }
1163 else
1164 {
1165 if ((insn & 0x0600) == 0x0600)
1166 insn_size = 4;
1167 else
1168 insn_size = 2;
1169
1170 /* start-sanitize-v850e */
1171 /* Special case: 32 bit MOV */
1172 if ((insn & 0xffe0) == 0x0620)
1173 insn_size = 2;
1174 /* end_sanitize-v850e */
1175
1176 f = frag_more (insn_size);
1177
1178 md_number_to_chars (f, insn, insn_size);
1179
1180 if (extra_data_after_insn)
1181 {
1182 f = frag_more (extra_data_len);
1183 md_number_to_chars (f, extra_data, extra_data_len);
1184
1185 extra_data_after_insn = false;
1186 }
1187 }
1188
1189 /* Create any fixups. At this point we do not use a
1190 bfd_reloc_code_real_type, but instead just use the
1191 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1192 handle fixups for any operand type, although that is admittedly
1193 not a very exciting feature. We pick a BFD reloc type in
1194 md_apply_fix. */
1195 for (i = 0; i < fc; i++)
1196 {
1197 const struct v850_operand *operand;
1198
1199 operand = &v850_operands[fixups[i].opindex];
1200 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1201 {
1202 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1203 int size;
1204 int offset;
1205 fixS *fixP;
1206
1207 if (!reloc_howto)
1208 abort();
1209
1210 size = bfd_get_reloc_size (reloc_howto);
1211
1212 /* The "size" of a TDA_OFFSET reloc varies depending
1213 on what kind of instruction it's used in! */
1214 if (reloc_howto->type == 11 && insn_size > 2)
1215 size = 2;
1216
1217 if (size < 1 || size > 4)
1218 abort();
1219
1220 offset = 4 - size;
1221 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
1222 &fixups[i].exp,
1223 reloc_howto->pc_relative,
1224 fixups[i].reloc);
1225
1226 switch (fixups[i].reloc)
1227 {
1228 case BFD_RELOC_LO16:
1229 case BFD_RELOC_HI16:
1230 case BFD_RELOC_HI16_S:
1231 fixP->fx_no_overflow = 1;
1232 break;
1233 }
1234 }
1235 else
1236 {
1237 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1238 &fixups[i].exp,
1239 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
1240 ((bfd_reloc_code_real_type)
1241 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1242 }
1243 }
1244 }
1245
1246
1247 /* if while processing a fixup, a reloc really needs to be created */
1248 /* then it is done here */
1249
1250 arelent *
1251 tc_gen_reloc (seg, fixp)
1252 asection *seg;
1253 fixS *fixp;
1254 {
1255 arelent *reloc;
1256 reloc = (arelent *) xmalloc (sizeof (arelent));
1257 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1258 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1259 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1260 if (reloc->howto == (reloc_howto_type *) NULL)
1261 {
1262 as_bad_where (fixp->fx_file, fixp->fx_line,
1263 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1264 return NULL;
1265 }
1266 reloc->addend = fixp->fx_addnumber;
1267 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
1268 return reloc;
1269 }
1270
1271 /* Assume everything will fit in two bytes, then expand as necessary. */
1272 int
1273 md_estimate_size_before_relax (fragp, seg)
1274 fragS *fragp;
1275 asection *seg;
1276 {
1277 fragp->fr_var = 4;
1278 return 2;
1279 }
1280
1281
1282 long
1283 md_pcrel_from (fixp)
1284 fixS *fixp;
1285 {
1286 /* If the symbol is undefined, or in a section other than our own,
1287 then let the linker figure it out. */
1288 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1289 {
1290 /* The symbol is undefined. Let the linker figure it out. */
1291 return 0;
1292 }
1293 return fixp->fx_frag->fr_address + fixp->fx_where;
1294 }
1295
1296 int
1297 md_apply_fix3 (fixp, valuep, seg)
1298 fixS *fixp;
1299 valueT *valuep;
1300 segT seg;
1301 {
1302 valueT value;
1303 char *where;
1304
1305 if (fixp->fx_addsy == (symbolS *) NULL)
1306 {
1307 value = *valuep;
1308 fixp->fx_done = 1;
1309 }
1310 else if (fixp->fx_pcrel)
1311 value = *valuep;
1312 else
1313 {
1314 value = fixp->fx_offset;
1315 if (fixp->fx_subsy != (symbolS *) NULL)
1316 {
1317 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1318 value -= S_GET_VALUE (fixp->fx_subsy);
1319 else
1320 {
1321 /* We don't actually support subtracting a symbol. */
1322 as_bad_where (fixp->fx_file, fixp->fx_line,
1323 "expression too complex");
1324 }
1325 }
1326 }
1327
1328 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
1329
1330 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
1331 {
1332 int opindex;
1333 const struct v850_operand *operand;
1334 char *where;
1335 unsigned long insn;
1336
1337 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
1338 operand = &v850_operands[opindex];
1339
1340 /* Fetch the instruction, insert the fully resolved operand
1341 value, and stuff the instruction back again.
1342
1343 Note the instruction has been stored in little endian
1344 format! */
1345 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1346
1347 insn = bfd_getl32((unsigned char *) where);
1348 insn = v850_insert_operand (insn, operand, (offsetT) value,
1349 fixp->fx_file, fixp->fx_line);
1350 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
1351
1352 if (fixp->fx_done)
1353 {
1354 /* Nothing else to do here. */
1355 return 1;
1356 }
1357
1358 /* Determine a BFD reloc value based on the operand information.
1359 We are only prepared to turn a few of the operands into relocs. */
1360
1361 if (operand->bits == 22)
1362 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1363 else if (operand->bits == 9)
1364 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1365 else if (operand->bits == 16)
1366 fixp->fx_r_type = BFD_RELOC_V850_16_PCREL;
1367 else
1368 {
1369 as_bad_where(fixp->fx_file, fixp->fx_line,
1370 "unresolved expression that must be resolved");
1371 fixp->fx_done = 1;
1372 return 1;
1373 }
1374 }
1375 else if (fixp->fx_done)
1376 {
1377 /* We still have to insert the value into memory! */
1378 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1379 if (fixp->fx_size == 1)
1380 *where = value & 0xff;
1381 if (fixp->fx_size == 2)
1382 bfd_putl16(value & 0xffff, (unsigned char *) where);
1383 if (fixp->fx_size == 4)
1384 bfd_putl32(value, (unsigned char *) where);
1385 }
1386
1387 fixp->fx_addnumber = value;
1388 return 1;
1389 }
1390
1391 \f
1392 /* Insert an operand value into an instruction. */
1393
1394 static unsigned long
1395 v850_insert_operand (insn, operand, val, file, line)
1396 unsigned long insn;
1397 const struct v850_operand * operand;
1398 offsetT val;
1399 char *file;
1400 unsigned int line;
1401 {
1402 if (operand->bits != 32)
1403 {
1404 long min, max;
1405 offsetT test;
1406
1407 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1408 {
1409 max = (1 << (operand->bits - 1)) - 1;
1410 min = - (1 << (operand->bits - 1));
1411 }
1412 else
1413 {
1414 max = (1 << operand->bits) - 1;
1415 min = 0;
1416 }
1417
1418 test = val;
1419
1420 if (test < (offsetT) min || test > (offsetT) max)
1421 {
1422 const char * err =
1423 "operand out of range (%s not between %ld and %ld)";
1424 char buf[100];
1425
1426 sprint_value (buf, test);
1427 if (file == (char *) NULL)
1428 as_warn (err, buf, min, max);
1429 else
1430 as_warn_where (file, line, err, buf, min, max);
1431 }
1432 }
1433
1434 if (operand->insert)
1435 {
1436 const char * message = NULL;
1437
1438 insn = (*operand->insert) (insn, val, & message);
1439 if (message != NULL)
1440 {
1441 if (file == (char *) NULL)
1442 as_warn (message);
1443 else
1444 as_warn_where (file, line, message);
1445 }
1446 }
1447 else
1448 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1449
1450 return insn;
1451 }
1452
1453 /* Parse a cons expression. We have to handle hi(), lo(), etc
1454 on the v850. */
1455 void
1456 parse_cons_expression_v850 (exp)
1457 expressionS *exp;
1458 {
1459 /* See if there's a reloc prefix like hi() we have to handle. */
1460 hold_cons_reloc = v850_reloc_prefix ();
1461
1462 /* Do normal expression parsing. */
1463 expression (exp);
1464 }
1465
1466 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1467 found a reloc prefix, then we use that reloc, else we choose an
1468 appropriate one based on the size of the expression. */
1469 void
1470 cons_fix_new_v850 (frag, where, size, exp)
1471 fragS *frag;
1472 int where;
1473 int size;
1474 expressionS *exp;
1475 {
1476 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1477 {
1478 if (size == 4)
1479 hold_cons_reloc = BFD_RELOC_32;
1480 if (size == 2)
1481 hold_cons_reloc = BFD_RELOC_16;
1482 if (size == 1)
1483 hold_cons_reloc = BFD_RELOC_8;
1484 }
1485
1486 if (exp != NULL)
1487 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1488 else
1489 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
1490 }
This page took 0.080656 seconds and 5 git commands to generate.