Set machine architecture and type.
[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 else if (operand->flags & V850E_PUSH_POP)
1092 {
1093 errmsg = parse_register_list (& insn, operand);
1094
1095 /* The parse_register_list() function has already done everything, so fake a dummy expression. */
1096 ex.X_op = O_constant;
1097 ex.X_add_number = 0;
1098 }
1099 else if (operand->flags & V850E_IMMEDIATE16)
1100 {
1101 expression (& ex);
1102
1103 if (ex.X_op != O_constant)
1104 errmsg = "constant expression expected";
1105 else if (ex.X_add_number & 0xffff0000)
1106 {
1107 if (ex.X_add_number & 0xffff)
1108 errmsg = "constant too big to fit into instruction";
1109 else if ((insn & 0x001fffc0) == 0x00130780)
1110 ex.X_add_number >>= 16;
1111 else
1112 errmsg = "constant too big to fit into instruction";
1113 }
1114
1115 extra_data_after_insn = true;
1116 extra_data_len = 2;
1117 extra_data = ex.X_add_number;
1118 ex.X_add_number = 0;
1119 }
1120 else if (operand->flags & V850E_IMMEDIATE32)
1121 {
1122 expression (& ex);
1123
1124 if (ex.X_op != O_constant)
1125 errmsg = "constant expression expected";
1126
1127 extra_data_after_insn = true;
1128 extra_data_len = 4;
1129 extra_data = ex.X_add_number;
1130 ex.X_add_number = 0;
1131 }
1132 else if (register_name (&ex)
1133 && (operand->flags & V850_OPERAND_REG) == 0)
1134 {
1135 errmsg = "syntax error: register not expected";
1136 }
1137 else if (system_register_name (& ex, false)
1138 && (operand->flags & V850_OPERAND_SRG) == 0)
1139 {
1140 errmsg = "syntax error: system register not expected";
1141 }
1142 else if (cc_name (&ex)
1143 && (operand->flags & V850_OPERAND_CC) == 0)
1144 {
1145 errmsg = "syntax error: condition code not expected";
1146 }
1147 else
1148 {
1149 expression (& ex);
1150 /* start-sanitize-v850e */
1151 /* Special case:
1152 If we are assembling a MOV instruction (or a CALLT.... :-)
1153 and the immediate value does not fit into the bits available
1154 then create a fake error so that the next MOV instruction
1155 will be selected. This one has a 32 bit immediate field. */
1156
1157 if (((insn & 0x07e0) == 0x0200)
1158 && ex.X_op == O_constant
1159 && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1160 errmsg = "use bigger instruction";
1161 /* end-sanitize-v850e */
1162 }
1163
1164 if (errmsg)
1165 goto error;
1166
1167 /*fprintf (stderr, "insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number );*/
1168
1169 switch (ex.X_op)
1170 {
1171 case O_illegal:
1172 errmsg = "illegal operand";
1173 goto error;
1174 case O_absent:
1175 errmsg = "missing operand";
1176 goto error;
1177 case O_register:
1178 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1179 {
1180 errmsg = "invalid operand";
1181 goto error;
1182 }
1183 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1184 (char *) NULL, 0);
1185 break;
1186
1187 case O_constant:
1188 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1189 (char *) NULL, 0);
1190 break;
1191
1192 default:
1193 /* We need to generate a fixup for this expression. */
1194 if (fc >= MAX_INSN_FIXUPS)
1195 as_fatal ("too many fixups");
1196 fixups[fc].exp = ex;
1197 fixups[fc].opindex = *opindex_ptr;
1198 fixups[fc].reloc = BFD_RELOC_UNUSED;
1199 ++fc;
1200 break;
1201 }
1202 }
1203
1204 str = input_line_pointer;
1205 input_line_pointer = hold;
1206
1207 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1208 || *str == ')')
1209 ++str;
1210 }
1211 match = 1;
1212
1213 error:
1214 if (match == 0)
1215 {
1216 next_opcode = opcode + 1;
1217 if (next_opcode->opcode != 0 && !strcmp (next_opcode->name, opcode->name))
1218 {
1219 opcode = next_opcode;
1220 continue;
1221 }
1222
1223 as_bad ("%s", errmsg);
1224 ignore_rest_of_line ();
1225 input_line_pointer = saved_input_line_pointer;
1226 return;
1227 }
1228 break;
1229 }
1230
1231 while (isspace (*str))
1232 ++str;
1233
1234 if (*str != '\0')
1235 as_bad ("junk at end of line: `%s'", str);
1236
1237 input_line_pointer = str;
1238
1239 /* Write out the instruction.
1240
1241 Four byte insns have an opcode with the two high bits on. */
1242 if (relaxable && fc > 0)
1243 {
1244 f = frag_var (rs_machine_dependent, 6, 4, 0,
1245 fixups[0].exp.X_add_symbol,
1246 fixups[0].exp.X_add_number,
1247 (char *)fixups[0].opindex);
1248 insn_size = 2;
1249 md_number_to_chars (f, insn, insn_size);
1250 md_number_to_chars (f + 2, 0, 4);
1251 fc = 0;
1252 }
1253 else
1254 {
1255 if ((insn & 0x0600) == 0x0600)
1256 insn_size = 4;
1257 else
1258 insn_size = 2;
1259
1260 /* start-sanitize-v850e */
1261 /* Special case: 32 bit MOV */
1262 if ((insn & 0xffe0) == 0x0620)
1263 insn_size = 2;
1264 /* end_sanitize-v850e */
1265
1266 f = frag_more (insn_size);
1267
1268 md_number_to_chars (f, insn, insn_size);
1269
1270 if (extra_data_after_insn)
1271 {
1272 f = frag_more (extra_data_len);
1273 md_number_to_chars (f, extra_data, extra_data_len);
1274
1275 extra_data_after_insn = false;
1276 }
1277 }
1278
1279 /* Create any fixups. At this point we do not use a
1280 bfd_reloc_code_real_type, but instead just use the
1281 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1282 handle fixups for any operand type, although that is admittedly
1283 not a very exciting feature. We pick a BFD reloc type in
1284 md_apply_fix. */
1285 for (i = 0; i < fc; i++)
1286 {
1287 const struct v850_operand *operand;
1288
1289 operand = &v850_operands[fixups[i].opindex];
1290 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1291 {
1292 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1293 int size;
1294 int offset;
1295 fixS *fixP;
1296
1297 if (!reloc_howto)
1298 abort();
1299
1300 size = bfd_get_reloc_size (reloc_howto);
1301
1302 /* The "size" of a TDA_OFFSET reloc varies depending
1303 on what kind of instruction it's used in! */
1304 if (reloc_howto->type == 11 && insn_size > 2)
1305 size = 2;
1306
1307 if (size < 1 || size > 4)
1308 abort();
1309
1310 offset = 4 - size;
1311 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
1312 &fixups[i].exp,
1313 reloc_howto->pc_relative,
1314 fixups[i].reloc);
1315
1316 switch (fixups[i].reloc)
1317 {
1318 case BFD_RELOC_LO16:
1319 case BFD_RELOC_HI16:
1320 case BFD_RELOC_HI16_S:
1321 fixP->fx_no_overflow = 1;
1322 break;
1323 }
1324 }
1325 else
1326 {
1327 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1328 &fixups[i].exp,
1329 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
1330 ((bfd_reloc_code_real_type)
1331 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1332 }
1333 }
1334
1335 input_line_pointer = saved_input_line_pointer;
1336 }
1337
1338
1339 /* if while processing a fixup, a reloc really needs to be created */
1340 /* then it is done here */
1341
1342 arelent *
1343 tc_gen_reloc (seg, fixp)
1344 asection *seg;
1345 fixS *fixp;
1346 {
1347 arelent *reloc;
1348 reloc = (arelent *) xmalloc (sizeof (arelent));
1349 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1350 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1351 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1352 if (reloc->howto == (reloc_howto_type *) NULL)
1353 {
1354 as_bad_where (fixp->fx_file, fixp->fx_line,
1355 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1356 return NULL;
1357 }
1358 reloc->addend = fixp->fx_addnumber;
1359 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
1360 return reloc;
1361 }
1362
1363 /* Assume everything will fit in two bytes, then expand as necessary. */
1364 int
1365 md_estimate_size_before_relax (fragp, seg)
1366 fragS *fragp;
1367 asection *seg;
1368 {
1369 fragp->fr_var = 4;
1370 return 2;
1371 }
1372
1373
1374 long
1375 md_pcrel_from (fixp)
1376 fixS *fixp;
1377 {
1378 /* If the symbol is undefined, or in a section other than our own,
1379 then let the linker figure it out. */
1380 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1381 {
1382 /* The symbol is undefined. Let the linker figure it out. */
1383 return 0;
1384 }
1385 return fixp->fx_frag->fr_address + fixp->fx_where;
1386 }
1387
1388 int
1389 md_apply_fix3 (fixp, valuep, seg)
1390 fixS *fixp;
1391 valueT *valuep;
1392 segT seg;
1393 {
1394 valueT value;
1395 char *where;
1396
1397 if (fixp->fx_addsy == (symbolS *) NULL)
1398 {
1399 value = *valuep;
1400 fixp->fx_done = 1;
1401 }
1402 else if (fixp->fx_pcrel)
1403 value = *valuep;
1404 else
1405 {
1406 value = fixp->fx_offset;
1407 if (fixp->fx_subsy != (symbolS *) NULL)
1408 {
1409 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1410 value -= S_GET_VALUE (fixp->fx_subsy);
1411 else
1412 {
1413 /* We don't actually support subtracting a symbol. */
1414 as_bad_where (fixp->fx_file, fixp->fx_line,
1415 "expression too complex");
1416 }
1417 }
1418 }
1419
1420 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
1421
1422 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
1423 {
1424 int opindex;
1425 const struct v850_operand *operand;
1426 char *where;
1427 unsigned long insn;
1428
1429 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
1430 operand = &v850_operands[opindex];
1431
1432 /* Fetch the instruction, insert the fully resolved operand
1433 value, and stuff the instruction back again.
1434
1435 Note the instruction has been stored in little endian
1436 format! */
1437 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1438
1439 insn = bfd_getl32((unsigned char *) where);
1440 insn = v850_insert_operand (insn, operand, (offsetT) value,
1441 fixp->fx_file, fixp->fx_line);
1442 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
1443
1444 if (fixp->fx_done)
1445 {
1446 /* Nothing else to do here. */
1447 return 1;
1448 }
1449
1450 /* Determine a BFD reloc value based on the operand information.
1451 We are only prepared to turn a few of the operands into relocs. */
1452
1453 if (operand->bits == 22)
1454 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1455 else if (operand->bits == 9)
1456 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1457 else if (operand->bits == 16)
1458 fixp->fx_r_type = BFD_RELOC_V850_16_PCREL;
1459 else
1460 {
1461 as_bad_where(fixp->fx_file, fixp->fx_line,
1462 "unresolved expression that must be resolved");
1463 fixp->fx_done = 1;
1464 return 1;
1465 }
1466 }
1467 else if (fixp->fx_done)
1468 {
1469 /* We still have to insert the value into memory! */
1470 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1471 if (fixp->fx_size == 1)
1472 *where = value & 0xff;
1473 if (fixp->fx_size == 2)
1474 bfd_putl16(value & 0xffff, (unsigned char *) where);
1475 if (fixp->fx_size == 4)
1476 bfd_putl32(value, (unsigned char *) where);
1477 }
1478
1479 fixp->fx_addnumber = value;
1480 return 1;
1481 }
1482
1483 \f
1484 /* Insert an operand value into an instruction. */
1485
1486 static unsigned long
1487 v850_insert_operand (insn, operand, val, file, line)
1488 unsigned long insn;
1489 const struct v850_operand * operand;
1490 offsetT val;
1491 char *file;
1492 unsigned int line;
1493 {
1494 if (operand->bits != 32)
1495 {
1496 long min, max;
1497 offsetT test;
1498
1499 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1500 {
1501 max = (1 << (operand->bits - 1)) - 1;
1502 min = - (1 << (operand->bits - 1));
1503 }
1504 else
1505 {
1506 max = (1 << operand->bits) - 1;
1507 min = 0;
1508 }
1509
1510 test = val;
1511
1512 if (test < (offsetT) min || test > (offsetT) max)
1513 {
1514 const char * err =
1515 "operand out of range (%s not between %ld and %ld)";
1516 char buf[100];
1517
1518 sprint_value (buf, test);
1519 if (file == (char *) NULL)
1520 as_warn (err, buf, min, max);
1521 else
1522 as_warn_where (file, line, err, buf, min, max);
1523 }
1524 }
1525
1526 if (operand->insert)
1527 {
1528 const char * message = NULL;
1529
1530 insn = (*operand->insert) (insn, val, & message);
1531 if (message != NULL)
1532 {
1533 if (file == (char *) NULL)
1534 as_warn (message);
1535 else
1536 as_warn_where (file, line, message);
1537 }
1538 }
1539 else
1540 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1541
1542 return insn;
1543 }
1544
1545 /* Parse a cons expression. We have to handle hi(), lo(), etc
1546 on the v850. */
1547 void
1548 parse_cons_expression_v850 (exp)
1549 expressionS *exp;
1550 {
1551 /* See if there's a reloc prefix like hi() we have to handle. */
1552 hold_cons_reloc = v850_reloc_prefix ();
1553
1554 /* Do normal expression parsing. */
1555 expression (exp);
1556 }
1557
1558 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1559 found a reloc prefix, then we use that reloc, else we choose an
1560 appropriate one based on the size of the expression. */
1561 void
1562 cons_fix_new_v850 (frag, where, size, exp)
1563 fragS *frag;
1564 int where;
1565 int size;
1566 expressionS *exp;
1567 {
1568 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1569 {
1570 if (size == 4)
1571 hold_cons_reloc = BFD_RELOC_32;
1572 if (size == 2)
1573 hold_cons_reloc = BFD_RELOC_16;
1574 if (size == 1)
1575 hold_cons_reloc = BFD_RELOC_8;
1576 }
1577
1578 if (exp != NULL)
1579 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1580 else
1581 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
1582 }