* config/tc-v850.c (md_pseudo_table): Add .word; allocates
[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 \f
28 /* Structure to hold information about predefined registers. */
29 struct reg_name
30 {
31 const char *name;
32 int value;
33 };
34
35 /* Generic assembler global variables which must be defined by all targets. */
36
37 /* Characters which always start a comment. */
38 const char comment_chars[] = "#";
39
40 /* Characters which start a comment at the beginning of a line. */
41 const char line_comment_chars[] = ";#";
42
43 /* Characters which may be used to separate multiple commands on a
44 single line. */
45 const char line_separator_chars[] = ";";
46
47 /* Characters which are used to indicate an exponent in a floating
48 point number. */
49 const char EXP_CHARS[] = "eE";
50
51 /* Characters which mean that a number is a floating point constant,
52 as in 0d1.0. */
53 const char FLT_CHARS[] = "dD";
54 \f
55
56 /* local functions */
57 static unsigned long v850_insert_operand
58 PARAMS ((unsigned long insn, const struct v850_operand *operand,
59 offsetT val, char *file, unsigned int line));
60 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
61 static boolean register_name PARAMS ((expressionS *expressionP));
62 static boolean system_register_name PARAMS ((expressionS *expressionP));
63 static boolean cc_name PARAMS ((expressionS *expressionP));
64 static bfd_reloc_code_real_type v850_reloc_prefix PARAMS ((void));
65
66
67 /* fixups */
68 #define MAX_INSN_FIXUPS (5)
69 struct v850_fixup
70 {
71 expressionS exp;
72 int opindex;
73 bfd_reloc_code_real_type reloc;
74 };
75 struct v850_fixup fixups[MAX_INSN_FIXUPS];
76 static int fc;
77 \f
78 const char *md_shortopts = "";
79 struct option md_longopts[] = {
80 {NULL, no_argument, NULL, 0}
81 };
82 size_t md_longopts_size = sizeof(md_longopts);
83
84 /* The target specific pseudo-ops which we support. */
85 const pseudo_typeS md_pseudo_table[] =
86 {
87 {"word", cons, 4},
88 { NULL, NULL, 0 }
89 };
90
91 /* Opcode hash table. */
92 static struct hash_control *v850_hash;
93
94 /* This table is sorted. Suitable for searching by a binary search. */
95 static const struct reg_name pre_defined_registers[] =
96 {
97 { "ep", 30 }, /* ep - element ptr */
98 { "gp", 4 }, /* gp - global ptr */
99 { "lp", 31 }, /* lp - link ptr */
100 { "r0", 0 },
101 { "r1", 1 },
102 { "r10", 10 },
103 { "r11", 11 },
104 { "r12", 12 },
105 { "r13", 13 },
106 { "r14", 14 },
107 { "r15", 15 },
108 { "r16", 16 },
109 { "r17", 17 },
110 { "r18", 18 },
111 { "r19", 19 },
112 { "r2", 2 },
113 { "r20", 20 },
114 { "r21", 21 },
115 { "r22", 22 },
116 { "r23", 23 },
117 { "r24", 24 },
118 { "r25", 25 },
119 { "r26", 26 },
120 { "r27", 27 },
121 { "r28", 28 },
122 { "r29", 29 },
123 { "r3", 3 },
124 { "r30", 30 },
125 { "r31", 31 },
126 { "r4", 4 },
127 { "r5", 5 },
128 { "r6", 6 },
129 { "r7", 7 },
130 { "r8", 8 },
131 { "r9", 9 },
132 { "sp", 3 }, /* sp - stack ptr */
133 { "tp", 5 }, /* tp - text ptr */
134 { "zero", 0 },
135 };
136 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
137
138
139 static const struct reg_name system_registers[] =
140 {
141 { "eipc", 0 },
142 { "eipsw", 1 },
143 { "fepc", 2 },
144 { "fepsw", 3 },
145 { "ecr", 4 },
146 { "psw", 5 },
147 };
148 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
149
150 static const struct reg_name cc_names[] =
151 {
152 { "c", 0x1 },
153 { "ge", 0xe },
154 { "gt", 0xf },
155 { "h", 0xb },
156 { "l", 0x1 },
157 { "le", 0x7 },
158 { "lt", 0x6 },
159 { "n", 0x4 },
160 { "nc", 0x9 },
161 { "nh", 0x3 },
162 { "nl", 0x9 },
163 { "ns", 0xc },
164 { "nv", 0x8 },
165 { "nz", 0xa },
166 { "p", 0xc },
167 { "s", 0x4 },
168 { "sa", 0xd },
169 { "t", 0x5 },
170 { "v", 0x0 },
171 { "z", 0x2 },
172 };
173 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
174
175 /* reg_name_search does a binary search of the given register table
176 to see if "name" is a valid regiter name. Returns the register
177 number from the array on success, or -1 on failure. */
178
179 static int
180 reg_name_search (regs, regcount, name)
181 const struct reg_name *regs;
182 int regcount;
183 const char *name;
184 {
185 int middle, low, high;
186 int cmp;
187
188 low = 0;
189 high = regcount - 1;
190
191 do
192 {
193 middle = (low + high) / 2;
194 cmp = strcasecmp (name, regs[middle].name);
195 if (cmp < 0)
196 high = middle - 1;
197 else if (cmp > 0)
198 low = middle + 1;
199 else
200 return regs[middle].value;
201 }
202 while (low <= high);
203 return -1;
204 }
205
206
207 /* Summary of register_name().
208 *
209 * in: Input_line_pointer points to 1st char of operand.
210 *
211 * out: A expressionS.
212 * The operand may have been a register: in this case, X_op == O_register,
213 * X_add_number is set to the register number, and truth is returned.
214 * Input_line_pointer->(next non-blank) char after operand, or is in
215 * its original state.
216 */
217 static boolean
218 register_name (expressionP)
219 expressionS *expressionP;
220 {
221 int reg_number;
222 char *name;
223 char *start;
224 char c;
225
226 /* Find the spelling of the operand */
227 start = name = input_line_pointer;
228
229 c = get_symbol_end ();
230 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
231
232 /* look to see if it's in the register table */
233 if (reg_number >= 0)
234 {
235 expressionP->X_op = O_register;
236 expressionP->X_add_number = reg_number;
237
238 /* make the rest nice */
239 expressionP->X_add_symbol = NULL;
240 expressionP->X_op_symbol = NULL;
241 *input_line_pointer = c; /* put back the delimiting char */
242 return true;
243 }
244 else
245 {
246 /* reset the line as if we had not done anything */
247 *input_line_pointer = c; /* put back the delimiting char */
248 input_line_pointer = start; /* reset input_line pointer */
249 return false;
250 }
251 }
252
253 /* Summary of system_register_name().
254 *
255 * in: Input_line_pointer points to 1st char of operand.
256 *
257 * out: A expressionS.
258 * The operand may have been a register: in this case, X_op == O_register,
259 * X_add_number is set to the register number, and truth is returned.
260 * Input_line_pointer->(next non-blank) char after operand, or is in
261 * its original state.
262 */
263 static boolean
264 system_register_name (expressionP)
265 expressionS *expressionP;
266 {
267 int reg_number;
268 char *name;
269 char *start;
270 char c;
271
272 /* Find the spelling of the operand */
273 start = name = input_line_pointer;
274
275 c = get_symbol_end ();
276 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
277
278 /* look to see if it's in the register table */
279 if (reg_number >= 0)
280 {
281 expressionP->X_op = O_register;
282 expressionP->X_add_number = reg_number;
283
284 /* make the rest nice */
285 expressionP->X_add_symbol = NULL;
286 expressionP->X_op_symbol = NULL;
287 *input_line_pointer = c; /* put back the delimiting char */
288 return true;
289 }
290 else
291 {
292 /* reset the line as if we had not done anything */
293 *input_line_pointer = c; /* put back the delimiting char */
294 input_line_pointer = start; /* reset input_line pointer */
295 return false;
296 }
297 }
298
299 /* Summary of cc_name().
300 *
301 * in: Input_line_pointer points to 1st char of operand.
302 *
303 * out: A expressionS.
304 * The operand may have been a register: in this case, X_op == O_register,
305 * X_add_number is set to the register number, and truth is returned.
306 * Input_line_pointer->(next non-blank) char after operand, or is in
307 * its original state.
308 */
309 static boolean
310 cc_name (expressionP)
311 expressionS *expressionP;
312 {
313 int reg_number;
314 char *name;
315 char *start;
316 char c;
317
318 /* Find the spelling of the operand */
319 start = name = input_line_pointer;
320
321 c = get_symbol_end ();
322 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
323
324 /* look to see if it's in the register table */
325 if (reg_number >= 0)
326 {
327 expressionP->X_op = O_constant;
328 expressionP->X_add_number = reg_number;
329
330 /* make the rest nice */
331 expressionP->X_add_symbol = NULL;
332 expressionP->X_op_symbol = NULL;
333 *input_line_pointer = c; /* put back the delimiting char */
334 return true;
335 }
336 else
337 {
338 /* reset the line as if we had not done anything */
339 *input_line_pointer = c; /* put back the delimiting char */
340 input_line_pointer = start; /* reset input_line pointer */
341 return false;
342 }
343 }
344
345 void
346 md_show_usage (stream)
347 FILE *stream;
348 {
349 fprintf(stream, "V850 options:\n\
350 none yet\n");
351 }
352
353 int
354 md_parse_option (c, arg)
355 int c;
356 char *arg;
357 {
358 return 0;
359 }
360
361 symbolS *
362 md_undefined_symbol (name)
363 char *name;
364 {
365 return 0;
366 }
367
368 char *
369 md_atof (type, litp, sizep)
370 int type;
371 char *litp;
372 int *sizep;
373 {
374 int prec;
375 LITTLENUM_TYPE words[4];
376 char *t;
377 int i;
378
379 switch (type)
380 {
381 case 'f':
382 prec = 2;
383 break;
384
385 case 'd':
386 prec = 4;
387 break;
388
389 default:
390 *sizep = 0;
391 return "bad call to md_atof";
392 }
393
394 t = atof_ieee (input_line_pointer, type, words);
395 if (t)
396 input_line_pointer = t;
397
398 *sizep = prec * 2;
399
400 for (i = prec - 1; i >= 0; i--)
401 {
402 md_number_to_chars (litp, (valueT) words[i], 2);
403 litp += 2;
404 }
405
406 return NULL;
407 }
408
409
410 void
411 md_convert_frag (abfd, sec, fragP)
412 bfd *abfd;
413 asection *sec;
414 fragS *fragP;
415 {
416 /* printf ("call to md_convert_frag \n"); */
417 abort ();
418 }
419
420 valueT
421 md_section_align (seg, addr)
422 asection *seg;
423 valueT addr;
424 {
425 int align = bfd_get_section_alignment (stdoutput, seg);
426 return ((addr + (1 << align) - 1) & (-1 << align));
427 }
428
429 void
430 md_begin ()
431 {
432 char *prev_name = "";
433 register const struct v850_opcode *op;
434
435 v850_hash = hash_new();
436
437 /* Insert unique names into hash table. The V850 instruction set
438 has many identical opcode names that have different opcodes based
439 on the operands. This hash table then provides a quick index to
440 the first opcode with a particular name in the opcode table. */
441
442 op = v850_opcodes;
443 while (op->name)
444 {
445 if (strcmp (prev_name, op->name))
446 {
447 prev_name = (char *) op->name;
448 hash_insert (v850_hash, op->name, (char *) op);
449 }
450 op++;
451 }
452 }
453
454 static bfd_reloc_code_real_type
455 v850_reloc_prefix()
456 {
457 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
458 {
459 input_line_pointer += 4;
460 return BFD_RELOC_HI16;
461 }
462 if (strncmp(input_line_pointer, "hi(", 3) == 0)
463 {
464 input_line_pointer += 3;
465 return BFD_RELOC_HI16_S;
466 }
467 if (strncmp (input_line_pointer, "lo(", 3) == 0)
468 {
469 input_line_pointer += 3;
470 return BFD_RELOC_LO16;
471 }
472
473 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
474 {
475 input_line_pointer += 7;
476 return BFD_RELOC_V850_SDA_OFFSET;
477 }
478
479 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
480 {
481 input_line_pointer += 7;
482 return BFD_RELOC_V850_ZDA_OFFSET;
483 }
484
485 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
486 {
487 input_line_pointer += 7;
488 return BFD_RELOC_V850_TDA_OFFSET;
489 }
490
491 /* FIXME: implement sda, tda, zda here */
492
493 return BFD_RELOC_UNUSED;
494 }
495
496 void
497 md_assemble (str)
498 char *str;
499 {
500 char *s;
501 struct v850_opcode *opcode;
502 struct v850_opcode *next_opcode;
503 const unsigned char *opindex_ptr;
504 int next_opindex;
505 unsigned long insn, insn_size;
506 char *f;
507 int i;
508 int match;
509 bfd_reloc_code_real_type reloc;
510
511 /* Get the opcode. */
512 for (s = str; *s != '\0' && ! isspace (*s); s++)
513 ;
514 if (*s != '\0')
515 *s++ = '\0';
516
517 /* find the first opcode with the proper name */
518 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
519 if (opcode == NULL)
520 {
521 as_bad ("Unrecognized opcode: `%s'", str);
522 return;
523 }
524
525 str = s;
526 while (isspace (*str))
527 ++str;
528
529 input_line_pointer = str;
530
531 for(;;)
532 {
533 const char *errmsg = NULL;
534
535 fc = 0;
536 match = 0;
537 next_opindex = 0;
538 insn = opcode->opcode;
539 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
540 {
541 const struct v850_operand *operand;
542 char *hold;
543 expressionS ex;
544
545 if (next_opindex == 0)
546 {
547 operand = &v850_operands[*opindex_ptr];
548 }
549 else
550 {
551 operand = &v850_operands[next_opindex];
552 next_opindex = 0;
553 }
554
555 errmsg = NULL;
556
557 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
558 ++str;
559
560 /* Gather the operand. */
561 hold = input_line_pointer;
562 input_line_pointer = str;
563
564
565 /* lo(), hi(), hi0(), etc... */
566 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
567 {
568 expression(&ex);
569
570 if (*input_line_pointer++ != ')')
571 {
572 errmsg = "syntax error: expected `)'";
573 goto error;
574 }
575
576 if (ex.X_op == O_constant)
577 {
578 switch (reloc)
579 {
580 case BFD_RELOC_LO16:
581 ex.X_add_number &= 0xffff;
582 break;
583
584 case BFD_RELOC_HI16:
585 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff);
586 break;
587
588 case BFD_RELOC_HI16_S:
589 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
590 + ((ex.X_add_number >> 15) & 1);
591 break;
592
593 default:
594 break;
595 }
596
597 insn = v850_insert_operand (insn, operand, ex.X_add_number,
598 (char *) NULL, 0);
599 }
600 else
601 {
602 if (fc > MAX_INSN_FIXUPS)
603 as_fatal ("too many fixups");
604
605 fixups[fc].exp = ex;
606 fixups[fc].opindex = *opindex_ptr;
607 fixups[fc].reloc = reloc;
608 fc++;
609 }
610 }
611 else
612 {
613 if ((operand->flags & V850_OPERAND_REG) != 0)
614 {
615 if (!register_name(&ex))
616 {
617 errmsg = "invalid register name";
618 goto error;
619 }
620 }
621 else if ((operand->flags & V850_OPERAND_SRG) != 0)
622 {
623 if (!system_register_name(&ex))
624 {
625 errmsg = "invalid system register name";
626 goto error;
627 }
628 }
629 else if ((operand->flags & V850_OPERAND_EP) != 0)
630 {
631 char *start = input_line_pointer;
632 char c = get_symbol_end ();
633 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
634 {
635 /* Put things back the way we found them. */
636 *input_line_pointer = c;
637 input_line_pointer = start;
638 errmsg = "expected EP register";
639 goto error;
640 }
641 *input_line_pointer = c;
642 str = input_line_pointer;
643 input_line_pointer = hold;
644
645 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
646 ++str;
647 continue;
648 }
649 else if ((operand->flags & V850_OPERAND_CC) != 0)
650 {
651 if (!cc_name(&ex))
652 {
653 errmsg = "invalid condition code name";
654 goto error;
655 }
656 }
657 else if (register_name (&ex)
658 && (operand->flags & V850_OPERAND_REG) == 0)
659 {
660 errmsg = "syntax error: register not expected";
661 goto error;
662 }
663 else if (system_register_name (&ex)
664 && (operand->flags & V850_OPERAND_SRG) == 0)
665 {
666 errmsg = "syntax error: system register not expected";
667 goto error;
668 }
669 else if (cc_name (&ex)
670 && (operand->flags & V850_OPERAND_CC) == 0)
671 {
672 errmsg = "syntax error: condition code not expected";
673 goto error;
674 }
675 else
676 {
677 expression(&ex);
678 }
679
680 switch (ex.X_op)
681 {
682 case O_illegal:
683 errmsg = "illegal operand";
684 goto error;
685 case O_absent:
686 errmsg = "missing operand";
687 goto error;
688 case O_register:
689 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
690 {
691 errmsg = "invalid operand";
692 goto error;
693 }
694
695 insn = v850_insert_operand (insn, operand, ex.X_add_number,
696 (char *) NULL, 0);
697 break;
698
699 case O_constant:
700 insn = v850_insert_operand (insn, operand, ex.X_add_number,
701 (char *) NULL, 0);
702 break;
703
704 default:
705 /* We need to generate a fixup for this expression. */
706 if (fc >= MAX_INSN_FIXUPS)
707 as_fatal ("too many fixups");
708 fixups[fc].exp = ex;
709 fixups[fc].opindex = *opindex_ptr;
710 fixups[fc].reloc = BFD_RELOC_UNUSED;
711 ++fc;
712 break;
713 }
714
715 }
716
717 str = input_line_pointer;
718 input_line_pointer = hold;
719
720 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
721 ++str;
722 }
723 match = 1;
724
725 error:
726 if (match == 0)
727 {
728 next_opcode = opcode + 1;
729 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
730 {
731 opcode = next_opcode;
732 continue;
733 }
734
735 as_bad ("%s", errmsg);
736 return;
737 }
738 break;
739 }
740
741 while (isspace (*str))
742 ++str;
743
744 if (*str != '\0')
745 as_bad ("junk at end of line: `%s'", str);
746
747 input_line_pointer = str;
748
749 /* Write out the instruction.
750
751 Four byte insns have an opcode with the two high bits on. */
752 if ((insn & 0x0600) == 0x0600)
753 insn_size = 4;
754 else
755 insn_size = 2;
756 f = frag_more (insn_size);
757 md_number_to_chars (f, insn, insn_size);
758
759 /* Create any fixups. At this point we do not use a
760 bfd_reloc_code_real_type, but instead just use the
761 BFD_RELOC_UNUSED plus the operand index. This lets us easily
762 handle fixups for any operand type, although that is admittedly
763 not a very exciting feature. We pick a BFD reloc type in
764 md_apply_fix. */
765 for (i = 0; i < fc; i++)
766 {
767 const struct v850_operand *operand;
768
769 operand = &v850_operands[fixups[i].opindex];
770 if (fixups[i].reloc != BFD_RELOC_UNUSED)
771 {
772 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
773 int size;
774 int offset;
775 fixS *fixP;
776
777 if (!reloc_howto)
778 abort();
779
780 size = bfd_get_reloc_size (reloc_howto);
781
782 /* The "size" of a TDA_OFFSET reloc varies depending
783 on what kind of instruction it's used in! */
784 if (reloc_howto->type == 11 && insn_size > 2)
785 size = 2;
786
787 if (size < 1 || size > 4)
788 abort();
789
790 offset = 4 - size;
791 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
792 &fixups[i].exp,
793 reloc_howto->pc_relative,
794 fixups[i].reloc);
795 }
796 else
797 {
798 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
799 &fixups[i].exp,
800 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
801 ((bfd_reloc_code_real_type)
802 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
803 }
804 }
805 }
806
807
808 /* if while processing a fixup, a reloc really needs to be created */
809 /* then it is done here */
810
811 arelent *
812 tc_gen_reloc (seg, fixp)
813 asection *seg;
814 fixS *fixp;
815 {
816 arelent *reloc;
817 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
818 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
819 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
820 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
821 if (reloc->howto == (reloc_howto_type *) NULL)
822 {
823 as_bad_where (fixp->fx_file, fixp->fx_line,
824 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
825 return NULL;
826 }
827 reloc->addend = fixp->fx_addnumber;
828 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
829 return reloc;
830 }
831
832 int
833 md_estimate_size_before_relax (fragp, seg)
834 fragS *fragp;
835 asection *seg;
836 {
837 return 0;
838 }
839
840 long
841 md_pcrel_from (fixp)
842 fixS *fixp;
843 {
844 /* If the symbol is undefined, or in a section other than our own,
845 then let the linker figure it out. */
846 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
847 {
848 /* The symbol is undefined. Let the linker figure it out. */
849 return 0;
850 }
851 return fixp->fx_frag->fr_address + fixp->fx_where;
852 }
853
854 int
855 md_apply_fix3 (fixp, valuep, seg)
856 fixS *fixp;
857 valueT *valuep;
858 segT seg;
859 {
860 valueT value;
861 char *where;
862
863 if (fixp->fx_addsy == (symbolS *) NULL)
864 {
865 value = *valuep;
866 fixp->fx_done = 1;
867 }
868 else if (fixp->fx_pcrel)
869 value = *valuep;
870 else
871 {
872 value = fixp->fx_offset;
873 if (fixp->fx_subsy != (symbolS *) NULL)
874 {
875 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
876 value -= S_GET_VALUE (fixp->fx_subsy);
877 else
878 {
879 /* We don't actually support subtracting a symbol. */
880 as_bad_where (fixp->fx_file, fixp->fx_line,
881 "expression too complex");
882 }
883 }
884 }
885
886 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
887
888 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
889 {
890 int opindex;
891 const struct v850_operand *operand;
892 char *where;
893 unsigned long insn;
894
895 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
896 operand = &v850_operands[opindex];
897
898 /* Fetch the instruction, insert the fully resolved operand
899 value, and stuff the instruction back again.
900
901 Note the instruction has been stored in little endian
902 format! */
903 where = fixp->fx_frag->fr_literal + fixp->fx_where;
904
905 insn = bfd_getl32((unsigned char *) where);
906 insn = v850_insert_operand (insn, operand, (offsetT) value,
907 fixp->fx_file, fixp->fx_line);
908 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
909
910 if (fixp->fx_done)
911 {
912 /* Nothing else to do here. */
913 return 1;
914 }
915
916 /* Determine a BFD reloc value based on the operand information.
917 We are only prepared to turn a few of the operands into relocs. */
918
919 if (operand->bits == 22)
920 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
921 else if (operand->bits == 9)
922 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
923 else
924 {
925 as_bad_where(fixp->fx_file, fixp->fx_line,
926 "unresolved expression that must be resolved");
927 fixp->fx_done = 1;
928 return 1;
929 }
930 }
931 else if (fixp->fx_done)
932 {
933 /* We still have to insert the value into memory! */
934 where = fixp->fx_frag->fr_literal + fixp->fx_where;
935 if (fixp->fx_size == 1)
936 *where = value & 0xff;
937 if (fixp->fx_size == 2)
938 bfd_putl16(value & 0xffff, (unsigned char *) where);
939 if (fixp->fx_size == 4)
940 bfd_putl32(value, (unsigned char *) where);
941 }
942
943 fixp->fx_addnumber = value;
944 return 1;
945 }
946
947 \f
948 /* Insert an operand value into an instruction. */
949
950 static unsigned long
951 v850_insert_operand (insn, operand, val, file, line)
952 unsigned long insn;
953 const struct v850_operand *operand;
954 offsetT val;
955 char *file;
956 unsigned int line;
957 {
958 if (operand->bits != 16)
959 {
960 long min, max;
961 offsetT test;
962
963 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
964 {
965 max = (1 << (operand->bits - 1)) - 1;
966 min = - (1 << (operand->bits - 1));
967 }
968 else
969 {
970 max = (1 << operand->bits) - 1;
971 min = 0;
972 }
973
974 test = val;
975
976
977 if (test < (offsetT) min || test > (offsetT) max)
978 {
979 const char *err =
980 "operand out of range (%s not between %ld and %ld)";
981 char buf[100];
982
983 sprint_value (buf, test);
984 if (file == (char *) NULL)
985 as_warn (err, buf, min, max);
986 else
987 as_warn_where (file, line, err, buf, min, max);
988 }
989 }
990
991 if (operand->insert)
992 {
993 const char *message = NULL;
994 insn = (*operand->insert) (insn, val, &message);
995 if (message != NULL)
996 {
997 if (file == (char *) NULL)
998 as_warn (message);
999 else
1000 as_warn_where (file, line, message);
1001 }
1002 }
1003 else
1004 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1005 return insn;
1006 }