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