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