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