44dff2b5ee0407e8d336a7627e465bb93707c837
[deliverable/binutils-gdb.git] / gas / config / tc-v850.c
1 /* tc-v850.c -- Assembler code for the NEC V850
2
3 Copyright (C) 1996 Free Software Foundation.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include <stdio.h>
23 #include <ctype.h>
24 #include "as.h"
25 #include "subsegs.h"
26 #include "opcode/v850.h"
27
28 /* Temporarily holds the reloc in a cons expression. */
29 static bfd_reloc_code_real_type hold_cons_reloc;
30 \f
31 /* Structure to hold information about predefined registers. */
32 struct reg_name
33 {
34 const char *name;
35 int value;
36 };
37
38 /* Generic assembler global variables which must be defined by all targets. */
39
40 /* Characters which always start a comment. */
41 const char comment_chars[] = "#";
42
43 /* Characters which start a comment at the beginning of a line. */
44 const char line_comment_chars[] = ";#";
45
46 /* Characters which may be used to separate multiple commands on a
47 single line. */
48 const char line_separator_chars[] = ";";
49
50 /* Characters which are used to indicate an exponent in a floating
51 point number. */
52 const char EXP_CHARS[] = "eE";
53
54 /* Characters which mean that a number is a floating point constant,
55 as in 0d1.0. */
56 const char FLT_CHARS[] = "dD";
57 \f
58
59 /* local functions */
60 static unsigned long v850_insert_operand
61 PARAMS ((unsigned long insn, const struct v850_operand *operand,
62 offsetT val, char *file, unsigned int line));
63 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
64 static boolean register_name PARAMS ((expressionS *expressionP));
65 static boolean system_register_name PARAMS ((expressionS *expressionP));
66 static boolean cc_name PARAMS ((expressionS *expressionP));
67 static bfd_reloc_code_real_type v850_reloc_prefix PARAMS ((void));
68
69
70 /* fixups */
71 #define MAX_INSN_FIXUPS (5)
72 struct v850_fixup
73 {
74 expressionS exp;
75 int opindex;
76 bfd_reloc_code_real_type reloc;
77 };
78 struct v850_fixup fixups[MAX_INSN_FIXUPS];
79 static int fc;
80 \f
81 const char *md_shortopts = "";
82 struct option md_longopts[] = {
83 {NULL, no_argument, NULL, 0}
84 };
85 size_t md_longopts_size = sizeof(md_longopts);
86
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 { "eipc", 0 },
145 { "eipsw", 1 },
146 { "fepc", 2 },
147 { "fepsw", 3 },
148 { "ecr", 4 },
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 void
349 md_show_usage (stream)
350 FILE *stream;
351 {
352 fprintf(stream, "V850 options:\n\
353 none yet\n");
354 }
355
356 int
357 md_parse_option (c, arg)
358 int c;
359 char *arg;
360 {
361 return 0;
362 }
363
364 symbolS *
365 md_undefined_symbol (name)
366 char *name;
367 {
368 return 0;
369 }
370
371 char *
372 md_atof (type, litp, sizep)
373 int type;
374 char *litp;
375 int *sizep;
376 {
377 int prec;
378 LITTLENUM_TYPE words[4];
379 char *t;
380 int i;
381
382 switch (type)
383 {
384 case 'f':
385 prec = 2;
386 break;
387
388 case 'd':
389 prec = 4;
390 break;
391
392 default:
393 *sizep = 0;
394 return "bad call to md_atof";
395 }
396
397 t = atof_ieee (input_line_pointer, type, words);
398 if (t)
399 input_line_pointer = t;
400
401 *sizep = prec * 2;
402
403 for (i = prec - 1; i >= 0; i--)
404 {
405 md_number_to_chars (litp, (valueT) words[i], 2);
406 litp += 2;
407 }
408
409 return NULL;
410 }
411
412
413 void
414 md_convert_frag (abfd, sec, fragP)
415 bfd *abfd;
416 asection *sec;
417 fragS *fragP;
418 {
419 /* printf ("call to md_convert_frag \n"); */
420 abort ();
421 }
422
423 valueT
424 md_section_align (seg, addr)
425 asection *seg;
426 valueT addr;
427 {
428 int align = bfd_get_section_alignment (stdoutput, seg);
429 return ((addr + (1 << align) - 1) & (-1 << align));
430 }
431
432 void
433 md_begin ()
434 {
435 char *prev_name = "";
436 register const struct v850_opcode *op;
437
438 v850_hash = hash_new();
439
440 /* Insert unique names into hash table. The V850 instruction set
441 has many identical opcode names that have different opcodes based
442 on the operands. This hash table then provides a quick index to
443 the first opcode with a particular name in the opcode table. */
444
445 op = v850_opcodes;
446 while (op->name)
447 {
448 if (strcmp (prev_name, op->name))
449 {
450 prev_name = (char *) op->name;
451 hash_insert (v850_hash, op->name, (char *) op);
452 }
453 op++;
454 }
455 }
456
457 static bfd_reloc_code_real_type
458 v850_reloc_prefix()
459 {
460 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
461 {
462 input_line_pointer += 3;
463 return BFD_RELOC_HI16;
464 }
465 if (strncmp(input_line_pointer, "hi(", 3) == 0)
466 {
467 input_line_pointer += 2;
468 return BFD_RELOC_HI16_S;
469 }
470 if (strncmp (input_line_pointer, "lo(", 3) == 0)
471 {
472 input_line_pointer += 2;
473 return BFD_RELOC_LO16;
474 }
475
476 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
477 {
478 input_line_pointer += 6;
479 return BFD_RELOC_V850_SDA_OFFSET;
480 }
481
482 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
483 {
484 input_line_pointer += 6;
485 return BFD_RELOC_V850_ZDA_OFFSET;
486 }
487
488 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
489 {
490 input_line_pointer += 6;
491 return BFD_RELOC_V850_TDA_OFFSET;
492 }
493
494 /* Disgusting */
495 if (strncmp(input_line_pointer, "(hi0(", 5) == 0)
496 {
497 input_line_pointer += 4;
498 return BFD_RELOC_HI16;
499 }
500 if (strncmp(input_line_pointer, "(hi(", 4) == 0)
501 {
502 input_line_pointer += 3;
503 return BFD_RELOC_HI16_S;
504 }
505 if (strncmp (input_line_pointer, "(lo(", 4) == 0)
506 {
507 input_line_pointer += 3;
508 return BFD_RELOC_LO16;
509 }
510
511 if (strncmp (input_line_pointer, "(sdaoff(", 8) == 0)
512 {
513 input_line_pointer += 7;
514 return BFD_RELOC_V850_SDA_OFFSET;
515 }
516
517 if (strncmp (input_line_pointer, "(zdaoff(", 8) == 0)
518 {
519 input_line_pointer += 7;
520 return BFD_RELOC_V850_ZDA_OFFSET;
521 }
522
523 if (strncmp (input_line_pointer, "(tdaoff(", 8) == 0)
524 {
525 input_line_pointer += 7;
526 return BFD_RELOC_V850_TDA_OFFSET;
527 }
528
529 return BFD_RELOC_UNUSED;
530 }
531
532 void
533 md_assemble (str)
534 char *str;
535 {
536 char *s;
537 struct v850_opcode *opcode;
538 struct v850_opcode *next_opcode;
539 const unsigned char *opindex_ptr;
540 int next_opindex;
541 unsigned long insn, insn_size;
542 char *f;
543 int i;
544 int match;
545 bfd_reloc_code_real_type reloc;
546
547 /* Get the opcode. */
548 for (s = str; *s != '\0' && ! isspace (*s); s++)
549 ;
550 if (*s != '\0')
551 *s++ = '\0';
552
553 /* find the first opcode with the proper name */
554 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
555 if (opcode == NULL)
556 {
557 as_bad ("Unrecognized opcode: `%s'", str);
558 return;
559 }
560
561 str = s;
562 while (isspace (*str))
563 ++str;
564
565 input_line_pointer = str;
566
567 for(;;)
568 {
569 const char *errmsg = NULL;
570
571 fc = 0;
572 match = 0;
573 next_opindex = 0;
574 insn = opcode->opcode;
575 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
576 {
577 const struct v850_operand *operand;
578 char *hold;
579 expressionS ex;
580
581 if (next_opindex == 0)
582 {
583 operand = &v850_operands[*opindex_ptr];
584 }
585 else
586 {
587 operand = &v850_operands[next_opindex];
588 next_opindex = 0;
589 }
590
591 errmsg = NULL;
592
593 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
594 ++str;
595
596 /* Gather the operand. */
597 hold = input_line_pointer;
598 input_line_pointer = str;
599
600
601 /* lo(), hi(), hi0(), etc... */
602 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
603 {
604 expression(&ex);
605
606 if (ex.X_op == O_constant)
607 {
608 switch (reloc)
609 {
610 case BFD_RELOC_LO16:
611 {
612 /* Truncate, then sign extend the value. */
613 int temp = ex.X_add_number & 0xffff;
614
615 /* XXX Assumes 32bit ints! */
616 temp = (temp << 16) >> 16;
617 ex.X_add_number = temp;
618 break;
619 }
620
621 case BFD_RELOC_HI16:
622 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff);
623 break;
624
625 case BFD_RELOC_HI16_S:
626 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
627 + ((ex.X_add_number >> 15) & 1);
628 break;
629
630 default:
631 break;
632 }
633
634 insn = v850_insert_operand (insn, operand, ex.X_add_number,
635 (char *) NULL, 0);
636 }
637 else
638 {
639 if (fc > MAX_INSN_FIXUPS)
640 as_fatal ("too many fixups");
641
642 fixups[fc].exp = ex;
643 fixups[fc].opindex = *opindex_ptr;
644 fixups[fc].reloc = reloc;
645 fc++;
646 }
647 }
648 else
649 {
650 if ((operand->flags & V850_OPERAND_REG) != 0)
651 {
652 if (!register_name(&ex))
653 {
654 errmsg = "invalid register name";
655 goto error;
656 }
657 }
658 else if ((operand->flags & V850_OPERAND_SRG) != 0)
659 {
660 if (!system_register_name(&ex))
661 {
662 errmsg = "invalid system register name";
663 goto error;
664 }
665 }
666 else if ((operand->flags & V850_OPERAND_EP) != 0)
667 {
668 char *start = input_line_pointer;
669 char c = get_symbol_end ();
670 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
671 {
672 /* Put things back the way we found them. */
673 *input_line_pointer = c;
674 input_line_pointer = start;
675 errmsg = "expected EP register";
676 goto error;
677 }
678 *input_line_pointer = c;
679 str = input_line_pointer;
680 input_line_pointer = hold;
681
682 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
683 ++str;
684 continue;
685 }
686 else if ((operand->flags & V850_OPERAND_CC) != 0)
687 {
688 if (!cc_name(&ex))
689 {
690 errmsg = "invalid condition code name";
691 goto error;
692 }
693 }
694 else if (register_name (&ex)
695 && (operand->flags & V850_OPERAND_REG) == 0)
696 {
697 errmsg = "syntax error: register not expected";
698 goto error;
699 }
700 else if (system_register_name (&ex)
701 && (operand->flags & V850_OPERAND_SRG) == 0)
702 {
703 errmsg = "syntax error: system register not expected";
704 goto error;
705 }
706 else if (cc_name (&ex)
707 && (operand->flags & V850_OPERAND_CC) == 0)
708 {
709 errmsg = "syntax error: condition code not expected";
710 goto error;
711 }
712 else
713 {
714 expression(&ex);
715 }
716
717 switch (ex.X_op)
718 {
719 case O_illegal:
720 errmsg = "illegal operand";
721 goto error;
722 case O_absent:
723 errmsg = "missing operand";
724 goto error;
725 case O_register:
726 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
727 {
728 errmsg = "invalid operand";
729 goto error;
730 }
731
732 insn = v850_insert_operand (insn, operand, ex.X_add_number,
733 (char *) NULL, 0);
734 break;
735
736 case O_constant:
737 insn = v850_insert_operand (insn, operand, ex.X_add_number,
738 (char *) NULL, 0);
739 break;
740
741 default:
742 /* We need to generate a fixup for this expression. */
743 if (fc >= MAX_INSN_FIXUPS)
744 as_fatal ("too many fixups");
745 fixups[fc].exp = ex;
746 fixups[fc].opindex = *opindex_ptr;
747 fixups[fc].reloc = BFD_RELOC_UNUSED;
748 ++fc;
749 break;
750 }
751
752 }
753
754 str = input_line_pointer;
755 input_line_pointer = hold;
756
757 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
758 || *str == ')')
759 ++str;
760 }
761 match = 1;
762
763 error:
764 if (match == 0)
765 {
766 next_opcode = opcode + 1;
767 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
768 {
769 opcode = next_opcode;
770 continue;
771 }
772
773 as_bad ("%s", errmsg);
774 return;
775 }
776 break;
777 }
778
779 while (isspace (*str))
780 ++str;
781
782 if (*str != '\0')
783 as_bad ("junk at end of line: `%s'", str);
784
785 input_line_pointer = str;
786
787 /* Write out the instruction.
788
789 Four byte insns have an opcode with the two high bits on. */
790 if ((insn & 0x0600) == 0x0600)
791 insn_size = 4;
792 else
793 insn_size = 2;
794 f = frag_more (insn_size);
795 md_number_to_chars (f, insn, insn_size);
796
797 /* Create any fixups. At this point we do not use a
798 bfd_reloc_code_real_type, but instead just use the
799 BFD_RELOC_UNUSED plus the operand index. This lets us easily
800 handle fixups for any operand type, although that is admittedly
801 not a very exciting feature. We pick a BFD reloc type in
802 md_apply_fix. */
803 for (i = 0; i < fc; i++)
804 {
805 const struct v850_operand *operand;
806
807 operand = &v850_operands[fixups[i].opindex];
808 if (fixups[i].reloc != BFD_RELOC_UNUSED)
809 {
810 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
811 int size;
812 int offset;
813 fixS *fixP;
814
815 if (!reloc_howto)
816 abort();
817
818 size = bfd_get_reloc_size (reloc_howto);
819
820 /* The "size" of a TDA_OFFSET reloc varies depending
821 on what kind of instruction it's used in! */
822 if (reloc_howto->type == 11 && insn_size > 2)
823 size = 2;
824
825 if (size < 1 || size > 4)
826 abort();
827
828 offset = 4 - size;
829 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
830 &fixups[i].exp,
831 reloc_howto->pc_relative,
832 fixups[i].reloc);
833 }
834 else
835 {
836 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
837 &fixups[i].exp,
838 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
839 ((bfd_reloc_code_real_type)
840 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
841 }
842 }
843 }
844
845
846 /* if while processing a fixup, a reloc really needs to be created */
847 /* then it is done here */
848
849 arelent *
850 tc_gen_reloc (seg, fixp)
851 asection *seg;
852 fixS *fixp;
853 {
854 arelent *reloc;
855 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
856 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
857 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
858 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
859 if (reloc->howto == (reloc_howto_type *) NULL)
860 {
861 as_bad_where (fixp->fx_file, fixp->fx_line,
862 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
863 return NULL;
864 }
865 reloc->addend = fixp->fx_addnumber;
866 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
867 return reloc;
868 }
869
870 int
871 md_estimate_size_before_relax (fragp, seg)
872 fragS *fragp;
873 asection *seg;
874 {
875 return 0;
876 }
877
878 long
879 md_pcrel_from (fixp)
880 fixS *fixp;
881 {
882 /* If the symbol is undefined, or in a section other than our own,
883 then let the linker figure it out. */
884 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
885 {
886 /* The symbol is undefined. Let the linker figure it out. */
887 return 0;
888 }
889 return fixp->fx_frag->fr_address + fixp->fx_where;
890 }
891
892 int
893 md_apply_fix3 (fixp, valuep, seg)
894 fixS *fixp;
895 valueT *valuep;
896 segT seg;
897 {
898 valueT value;
899 char *where;
900
901 if (fixp->fx_addsy == (symbolS *) NULL)
902 {
903 value = *valuep;
904 fixp->fx_done = 1;
905 }
906 else if (fixp->fx_pcrel)
907 value = *valuep;
908 else
909 {
910 value = fixp->fx_offset;
911 if (fixp->fx_subsy != (symbolS *) NULL)
912 {
913 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
914 value -= S_GET_VALUE (fixp->fx_subsy);
915 else
916 {
917 /* We don't actually support subtracting a symbol. */
918 as_bad_where (fixp->fx_file, fixp->fx_line,
919 "expression too complex");
920 }
921 }
922 }
923
924 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
925
926 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
927 {
928 int opindex;
929 const struct v850_operand *operand;
930 char *where;
931 unsigned long insn;
932
933 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
934 operand = &v850_operands[opindex];
935
936 /* Fetch the instruction, insert the fully resolved operand
937 value, and stuff the instruction back again.
938
939 Note the instruction has been stored in little endian
940 format! */
941 where = fixp->fx_frag->fr_literal + fixp->fx_where;
942
943 insn = bfd_getl32((unsigned char *) where);
944 insn = v850_insert_operand (insn, operand, (offsetT) value,
945 fixp->fx_file, fixp->fx_line);
946 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
947
948 if (fixp->fx_done)
949 {
950 /* Nothing else to do here. */
951 return 1;
952 }
953
954 /* Determine a BFD reloc value based on the operand information.
955 We are only prepared to turn a few of the operands into relocs. */
956
957 if (operand->bits == 22)
958 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
959 else if (operand->bits == 9)
960 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
961 else
962 {
963 as_bad_where(fixp->fx_file, fixp->fx_line,
964 "unresolved expression that must be resolved");
965 fixp->fx_done = 1;
966 return 1;
967 }
968 }
969 else if (fixp->fx_done)
970 {
971 /* We still have to insert the value into memory! */
972 where = fixp->fx_frag->fr_literal + fixp->fx_where;
973 if (fixp->fx_size == 1)
974 *where = value & 0xff;
975 if (fixp->fx_size == 2)
976 bfd_putl16(value & 0xffff, (unsigned char *) where);
977 if (fixp->fx_size == 4)
978 bfd_putl32(value, (unsigned char *) where);
979 }
980
981 fixp->fx_addnumber = value;
982 return 1;
983 }
984
985 \f
986 /* Insert an operand value into an instruction. */
987
988 static unsigned long
989 v850_insert_operand (insn, operand, val, file, line)
990 unsigned long insn;
991 const struct v850_operand *operand;
992 offsetT val;
993 char *file;
994 unsigned int line;
995 {
996 if (operand->bits != 16)
997 {
998 long min, max;
999 offsetT test;
1000
1001 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1002 {
1003 max = (1 << (operand->bits - 1)) - 1;
1004 min = - (1 << (operand->bits - 1));
1005 }
1006 else
1007 {
1008 max = (1 << operand->bits) - 1;
1009 min = 0;
1010 }
1011
1012 test = val;
1013
1014
1015 if (test < (offsetT) min || test > (offsetT) max)
1016 {
1017 const char *err =
1018 "operand out of range (%s not between %ld and %ld)";
1019 char buf[100];
1020
1021 sprint_value (buf, test);
1022 if (file == (char *) NULL)
1023 as_warn (err, buf, min, max);
1024 else
1025 as_warn_where (file, line, err, buf, min, max);
1026 }
1027 }
1028
1029 if (operand->insert)
1030 {
1031 const char *message = NULL;
1032 insn = (*operand->insert) (insn, val, &message);
1033 if (message != NULL)
1034 {
1035 if (file == (char *) NULL)
1036 as_warn (message);
1037 else
1038 as_warn_where (file, line, message);
1039 }
1040 }
1041 else
1042 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1043 return insn;
1044 }
1045
1046 /* Parse a cons expression. We have to handle hi(), lo(), etc
1047 on the v850. */
1048 void
1049 parse_cons_expression_v850 (exp)
1050 expressionS *exp;
1051 {
1052 /* See if there's a reloc prefix like hi() we have to handle. */
1053 hold_cons_reloc = v850_reloc_prefix ();
1054
1055 /* Do normal expression parsing. */
1056 expression (exp);
1057 }
1058
1059 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1060 found a reloc prefix, then we use that reloc, else we choose an
1061 appropriate one based on the size of the expression. */
1062 void
1063 cons_fix_new_v850 (frag, where, size, exp)
1064 fragS *frag;
1065 int where;
1066 int size;
1067 expressionS *exp;
1068 {
1069 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1070 {
1071 if (size == 4)
1072 hold_cons_reloc = BFD_RELOC_32;
1073 if (size == 2)
1074 hold_cons_reloc = BFD_RELOC_16;
1075 if (size == 1)
1076 hold_cons_reloc = BFD_RELOC_8;
1077 }
1078
1079 if (exp != NULL)
1080 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1081 else
1082 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
1083 }
This page took 0.054412 seconds and 4 git commands to generate.