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