e59011e5ecfdc247f5d71c47624e4b1ebfbbef6a
[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 /* local functions */
56 static unsigned long v850_insert_operand
57 PARAMS ((unsigned long insn, const struct v850_operand *operand,
58 offsetT val, char *file, unsigned int line));
59 static int reg_name_search PARAMS ((char *name, const struct reg_name *, int));
60 static boolean register_name PARAMS ((expressionS *expressionP));
61 static boolean system_register_name PARAMS ((expressionS *expressionP));
62 static int postfix PARAMS ((char *p));
63 static bfd_reloc_code_real_type get_reloc PARAMS ((struct v850_operand *op));
64 static unsigned long build_insn PARAMS ((struct v850_opcode *opcode, expressionS *opers));
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 /*
88 { "byte", ppc_byte, 0 },
89 { "long", ppc_elf_cons, 4 },
90 { "word", ppc_elf_cons, 2 },
91 { "short", ppc_elf_cons, 2 },
92 { "rdata", ppc_elf_rdata, 0 },
93 { "rodata", ppc_elf_rdata, 0 },
94 { "lcomm", ppc_elf_lcomm, 0 },
95 */
96 { NULL, NULL, 0 }
97 };
98
99 /* Opcode hash table. */
100 static struct hash_control *v850_hash;
101
102 /* This table is sorted. Suitable for searching by a binary search. */
103 static const struct reg_name pre_defined_registers[] =
104 {
105 { "ep", 30 }, /* ep - element ptr */
106 { "gp", 4 }, /* gp - global ptr */
107 { "lp", 31 }, /* lp - link ptr */
108 { "r0", 0 },
109 { "r1", 1 },
110 { "r10", 10 },
111 { "r11", 11 },
112 { "r12", 12 },
113 { "r13", 13 },
114 { "r14", 14 },
115 { "r15", 15 },
116 { "r16", 16 },
117 { "r17", 17 },
118 { "r18", 18 },
119 { "r19", 19 },
120 { "r2", 2 },
121 { "r20", 20 },
122 { "r21", 21 },
123 { "r22", 22 },
124 { "r23", 23 },
125 { "r24", 24 },
126 { "r25", 25 },
127 { "r26", 26 },
128 { "r27", 27 },
129 { "r28", 28 },
130 { "r29", 29 },
131 { "r3", 3 },
132 { "r30", 30 },
133 { "r31", 31 },
134 { "r4", 4 },
135 { "r5", 5 },
136 { "r6", 6 },
137 { "r7", 7 },
138 { "r8", 8 },
139 { "r9", 9 },
140 { "sp", 3 }, /* sp - stack ptr */
141 { "tp", 5 }, /* tp - text ptr */
142 { "zero", 0 },
143 };
144 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
145
146
147 static const struct reg_name system_registers[] =
148 {
149 { "eipc", 0 },
150 { "eipsw", 1 },
151 { "fepc", 2 },
152 { "fepsw", 3 },
153 { "ecr", 4 },
154 { "psw", 5 },
155 };
156 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
157
158 static const struct reg_name cc_names[] =
159 {
160 { "c", 0x1 },
161 { "ge", 0xe },
162 { "gt", 0xf },
163 { "h", 0xb },
164 { "l", 0x1 },
165 { "le", 0x7 },
166 { "lt", 0x6 },
167 { "n", 0x4 },
168 { "nc", 0x9 },
169 { "nh", 0x3 },
170 { "nl", 0x9 },
171 { "ns", 0xc },
172 { "nv", 0x8 },
173 { "nz", 0xa },
174 { "p", 0xc },
175 { "s", 0x4 },
176 { "sa", 0xd },
177 { "t", 0x5 },
178 { "v", 0x0 },
179 { "z", 0x2 },
180 };
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 (name, table, high)
188 char *name;
189 const struct reg_name *table;
190 int high;
191 {
192 int middle, low;
193 int cmp;
194
195 low = 0;
196 do
197 {
198 middle = (low + high) / 2;
199 cmp = strcasecmp (name, table[middle].name);
200 if (cmp < 0)
201 high = middle - 1;
202 else if (cmp > 0)
203 low = middle + 1;
204 else
205 return table[middle].value;
206 }
207 while (low <= high);
208 return -1;
209 }
210
211
212 /* Summary of register_name().
213 *
214 * in: Input_line_pointer points to 1st char of operand.
215 *
216 * out: A expressionS.
217 * The operand may have been a register: in this case, X_op == O_register,
218 * X_add_number is set to the register number, and truth is returned.
219 * Input_line_pointer->(next non-blank) char after operand, or is in
220 * its original state.
221 */
222 static boolean
223 register_name (expressionP)
224 expressionS *expressionP;
225 {
226 int reg_number;
227 char *name;
228 char *start;
229 char c;
230
231 /* Find the spelling of the operand */
232 start = name = input_line_pointer;
233
234 c = get_symbol_end ();
235 reg_number = reg_name_search (name, pre_defined_registers, REG_NAME_CNT - 1);
236
237 /* look to see if it's in the register table */
238 if (reg_number >= 0)
239 {
240 expressionP->X_op = O_register;
241 expressionP->X_add_number = reg_number;
242
243 /* make the rest nice */
244 expressionP->X_add_symbol = NULL;
245 expressionP->X_op_symbol = NULL;
246 *input_line_pointer = c; /* put back the delimiting char */
247 return true;
248 }
249 else
250 {
251 /* reset the line as if we had not done anything */
252 *input_line_pointer = c; /* put back the delimiting char */
253 input_line_pointer = start; /* reset input_line pointer */
254 return false;
255 }
256 }
257
258 /* Summary of system_register_name().
259 *
260 * in: Input_line_pointer points to 1st char of operand.
261 *
262 * out: A expressionS.
263 * The operand may have been a register: in this case, X_op == O_register,
264 * X_add_number is set to the register number, and truth is returned.
265 * Input_line_pointer->(next non-blank) char after operand, or is in
266 * its original state.
267 */
268 static boolean
269 system_register_name (expressionP)
270 expressionS *expressionP;
271 {
272 int reg_number;
273 char *name;
274 char *start;
275 char c;
276
277 /* Find the spelling of the operand */
278 start = name = input_line_pointer;
279
280 c = get_symbol_end ();
281 reg_number = reg_name_search (name, system_registers, SYSREG_NAME_CNT - 1);
282
283 /* look to see if it's in the register table */
284 if (reg_number >= 0)
285 {
286 expressionP->X_op = O_register;
287 expressionP->X_add_number = reg_number;
288
289 /* make the rest nice */
290 expressionP->X_add_symbol = NULL;
291 expressionP->X_op_symbol = NULL;
292 *input_line_pointer = c; /* put back the delimiting char */
293 return true;
294 }
295 else
296 {
297 /* reset the line as if we had not done anything */
298 *input_line_pointer = c; /* put back the delimiting char */
299 input_line_pointer = start; /* reset input_line pointer */
300 return false;
301 }
302 }
303
304 void
305 md_show_usage (stream)
306 FILE *stream;
307 {
308 fprintf(stream, "V850 options:\n\
309 none yet\n");
310 }
311
312 int
313 md_parse_option (c, arg)
314 int c;
315 char *arg;
316 {
317 return 0;
318 }
319
320 symbolS *
321 md_undefined_symbol (name)
322 char *name;
323 {
324 return 0;
325 }
326
327 char *
328 md_atof (type, litp, sizep)
329 int type;
330 char *litp;
331 int *sizep;
332 {
333 int prec;
334 LITTLENUM_TYPE words[4];
335 char *t;
336 int i;
337
338 switch (type)
339 {
340 case 'f':
341 prec = 2;
342 break;
343
344 case 'd':
345 prec = 4;
346 break;
347
348 default:
349 *sizep = 0;
350 return "bad call to md_atof";
351 }
352
353 t = atof_ieee (input_line_pointer, type, words);
354 if (t)
355 input_line_pointer = t;
356
357 *sizep = prec * 2;
358
359 for (i = prec - 1; i >= 0; i--)
360 {
361 md_number_to_chars (litp, (valueT) words[i], 2);
362 litp += 2;
363 }
364
365 return NULL;
366 }
367
368
369 void
370 md_convert_frag (abfd, sec, fragP)
371 bfd *abfd;
372 asection *sec;
373 fragS *fragP;
374 {
375 /* printf ("call to md_convert_frag \n"); */
376 abort ();
377 }
378
379 valueT
380 md_section_align (seg, addr)
381 asection *seg;
382 valueT addr;
383 {
384 int align = bfd_get_section_alignment (stdoutput, seg);
385 return ((addr + (1 << align) - 1) & (-1 << align));
386 }
387
388 void
389 md_begin ()
390 {
391 char *prev_name = "";
392 register const struct v850_opcode *op;
393 const struct v850_opcode *op_end;
394
395 v850_hash = hash_new();
396
397 /* Insert unique names into hash table. The V850 instruction set
398 has many identical opcode names that have different opcodes based
399 on the operands. This hash table then provides a quick index to
400 the first opcode with a particular name in the opcode table. */
401
402 op = v850_opcodes;
403 op_end = v850_opcodes + v850_num_opcodes;
404
405 for (; op < op_end; op++)
406 {
407 if (strcmp (prev_name, op->name))
408 {
409 prev_name = (char *) op->name;
410 hash_insert (v850_hash, op->name, (char *) op);
411 }
412 }
413 }
414
415
416 static bfd_reloc_code_real_type
417 get_reloc (op)
418 struct v850_operand *op;
419 {
420 abort ();
421 }
422
423
424 void
425 md_assemble (str)
426 char *str;
427 {
428 char *s;
429 struct v850_opcode *opcode;
430 struct v850_opcode *next_opcode;
431 const unsigned char *opindex_ptr;
432 int next_opindex;
433 unsigned long insn;
434 char *f;
435 int i;
436 int numops;
437 int match;
438
439 int numopts;
440 expressionS myops[5];
441
442 /* Get the opcode. */
443 for (s = str; *s != '\0' && ! isspace (*s); s++)
444 ;
445 if (*s != '\0')
446 *s++ = '\0';
447
448 /* find the first opcode with the proper name */
449 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
450 if (opcode == NULL)
451 {
452 as_bad ("Unrecognized opcode: `%s'", str);
453 return;
454 }
455
456 str = s;
457 while (isspace (*str))
458 ++str;
459
460 input_line_pointer = str;
461
462 for(;;)
463 {
464 const char *errmsg = NULL;
465
466 fc = 0;
467 match = 0;
468 next_opindex = 0;
469 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
470 {
471 const struct v850_operand *operand;
472 char *hold;
473 expressionS ex;
474 char endc;
475
476 if (next_opindex == 0)
477 {
478 operand = &v850_operands[*opindex_ptr];
479 }
480 else
481 {
482 operand = &v850_operands[next_opindex];
483 next_opindex = 0;
484 }
485
486 errmsg = NULL;
487
488 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
489 ++str;
490
491 /* Gather the operand. */
492 hold = input_line_pointer;
493 input_line_pointer = str;
494
495 if ((operand->flags & V850_OPERAND_REG) != 0)
496 {
497 if (!register_name(&ex))
498 {
499 errmsg = "invalid register name";
500 goto error;
501 }
502 }
503 else if ((operand->flags & V850_OPERAND_SRG) != 0)
504 {
505 if (!system_register_name(&ex))
506 {
507 errmsg = "invalid system register name";
508 goto error;
509 }
510 }
511 else if (strncmp(input_line_pointer, "lo(", 3) == 0)
512 {
513 input_line_pointer += 3;
514 expression(&ex);
515
516 if (*input_line_pointer++ != ')')
517 {
518 errmsg = "syntax error: expected `)'";
519 goto error;
520 }
521
522 if (ex.X_op == O_constant)
523 ex.X_add_number &= 0xffff;
524 else
525 {
526 if (fc > MAX_INSN_FIXUPS)
527 as_fatal ("too many fixups");
528
529 fixups[fc].exp = ex;
530 fixups[fc].opindex = *opindex_ptr;
531 fixups[fc].reloc = BFD_RELOC_LO16;
532 fc++;
533 }
534 }
535 else if (strncmp(input_line_pointer, "hi(", 3) == 0)
536 {
537 input_line_pointer += 3;
538 expression(&ex);
539
540 if (*input_line_pointer++ != ')')
541 {
542 errmsg = "syntax error: expected `)'";
543 goto error;
544 }
545
546 if (ex.X_op == O_constant)
547 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
548 else
549 {
550 if (fc > MAX_INSN_FIXUPS)
551 as_fatal ("too many fixups");
552
553 fixups[fc].exp = ex;
554 fixups[fc].opindex = *opindex_ptr;
555 fixups[fc].reloc = BFD_RELOC_HI16;
556 fc++;
557 }
558 }
559 else if (register_name (&ex)
560 && (operand->flags & V850_OPERAND_REG) == 0)
561 {
562 errmsg = "syntax error: register not expected";
563 goto error;
564 }
565 else if (system_register_name (&ex)
566 && (operand->flags & V850_OPERAND_SRG) == 0)
567 {
568 errmsg = "syntax error: system register not expected";
569 goto error;
570 }
571 else
572 {
573 expression(&ex);
574 }
575
576 str = input_line_pointer;
577 input_line_pointer = hold;
578
579 switch (ex.X_op)
580 {
581 case O_illegal:
582 errmsg = "illegal operand";
583 goto error;
584 case O_absent:
585 errmsg = "missing operand";
586 goto error;
587 case O_register:
588 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
589 {
590 errmsg = "invalid operand";
591 goto error;
592 }
593
594 insn = v850_insert_operand (insn, operand, ex.X_add_number,
595 (char *) NULL, 0);
596 break;
597
598 case O_constant:
599 insn = v850_insert_operand (insn, operand, ex.X_add_number,
600 (char *) NULL, 0);
601 break;
602
603 default:
604 /* We need to generate a fixup for this expression. */
605 if (fc >= MAX_INSN_FIXUPS)
606 as_fatal ("too many fixups");
607 fixups[fc].exp = ex;
608 fixups[fc].opindex = *opindex_ptr;
609 fixups[fc].reloc = BFD_RELOC_UNUSED;
610 ++fc;
611 break;
612 }
613
614 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
615 ++str;
616 }
617 match = 1;
618
619 error:
620 if (match == 0)
621 {
622 next_opcode = opcode + 1;
623 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
624 {
625 opcode = next_opcode;
626 continue;
627 }
628
629 as_bad ("%s", errmsg);
630 return;
631 }
632 break;
633 }
634
635 while (isspace (*str))
636 ++str;
637
638 if (*str != '\0')
639 as_bad ("junk at end of line: `%s'", str);
640
641 input_line_pointer = str;
642
643 f = frag_more (opcode->size);
644 md_number_to_chars (f, insn, opcode->size);
645 }
646
647
648 /* if while processing a fixup, a reloc really needs to be created */
649 /* then it is done here */
650
651 arelent *
652 tc_gen_reloc (seg, fixp)
653 asection *seg;
654 fixS *fixp;
655 {
656 arelent *reloc;
657 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
658 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
659 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
660 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
661 if (reloc->howto == (reloc_howto_type *) NULL)
662 {
663 as_bad_where (fixp->fx_file, fixp->fx_line,
664 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
665 return NULL;
666 }
667 reloc->addend = fixp->fx_addnumber;
668 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
669 return reloc;
670 }
671
672 int
673 md_estimate_size_before_relax (fragp, seg)
674 fragS *fragp;
675 asection *seg;
676 {
677 abort ();
678 return 0;
679 }
680
681 long
682 md_pcrel_from_section (fixp, sec)
683 fixS *fixp;
684 segT sec;
685 {
686 return 0;
687 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
688 }
689
690 int
691 md_apply_fix3 (fixp, valuep, seg)
692 fixS *fixp;
693 valueT *valuep;
694 segT seg;
695 {
696 abort();
697 #if 0
698 valueT value;
699 char *where;
700 unsigned long insn;
701 int op_type;
702
703 if (fixp->fx_addsy == (symbolS *) NULL)
704 {
705 value = *valuep;
706 fixp->fx_done = 1;
707 }
708 else if (fixp->fx_pcrel)
709 value = *valuep;
710 else
711 {
712 value = fixp->fx_offset;
713 if (fixp->fx_subsy != (symbolS *) NULL)
714 {
715 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
716 value -= S_GET_VALUE (fixp->fx_subsy);
717 else
718 {
719 /* We don't actually support subtracting a symbol. */
720 as_bad_where (fixp->fx_file, fixp->fx_line,
721 "expression too complex");
722 }
723 }
724 }
725
726 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
727
728 op_type = fixp->fx_r_type;
729 fixp->fx_r_type = get_reloc((struct v850_operand *)&v850_operands[op_type]);
730
731 /* printf("reloc=%d\n",fixp->fx_r_type); */
732
733 /* Fetch the instruction, insert the fully resolved operand
734 value, and stuff the instruction back again. */
735 where = fixp->fx_frag->fr_literal + fixp->fx_where;
736 insn = bfd_getb32 ((unsigned char *) where);
737 /* printf(" insn=%x value=%x\n",insn,value); */
738
739 insn = v850_insert_operand (insn, op_type, (offsetT) value);
740
741 /* printf(" new insn=%x\n",insn); */
742
743 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
744
745 if (fixp->fx_done)
746 return 1;
747
748 fixp->fx_addnumber = value;
749 return 1;
750 #endif
751 }
752
753 \f
754 /* Insert an operand value into an instruction. */
755
756 static unsigned long
757 v850_insert_operand (insn, operand, val, file, line)
758 unsigned long insn;
759 const struct v850_operand *operand;
760 offsetT val;
761 char *file;
762 unsigned int line;
763 {
764 if (operand->bits != 32)
765 {
766 long min, max;
767 offsetT test;
768
769 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
770 {
771 #if 0
772 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
773 && ppc_size == PPC_OPCODE_32)
774 max = (1 << operand->bits) - 1;
775 else
776 #endif
777 max = (1 << (operand->bits - 1)) - 1;
778 min = - (1 << (operand->bits - 1));
779 }
780 else
781 {
782 max = (1 << operand->bits) - 1;
783 min = 0;
784 }
785
786 #if 0
787 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
788 test = - val;
789 else
790 #endif
791 test = val;
792
793
794 if (test < (offsetT) min || test > (offsetT) max)
795 {
796 const char *err =
797 "operand out of range (%s not between %ld and %ld)";
798 char buf[100];
799
800 sprint_value (buf, test);
801 if (file == (char *) NULL)
802 as_warn (err, buf, min, max);
803 else
804 as_warn_where (file, line, err, buf, min, max);
805 }
806 }
807
808 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
809 return insn;
810 }