* config/tc-v850.c (md_assemble): Don't forget to initialize
[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 insn = opcode->opcode;
470 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
471 {
472 const struct v850_operand *operand;
473 char *hold;
474 expressionS ex;
475 char endc;
476
477 if (next_opindex == 0)
478 {
479 operand = &v850_operands[*opindex_ptr];
480 }
481 else
482 {
483 operand = &v850_operands[next_opindex];
484 next_opindex = 0;
485 }
486
487 errmsg = NULL;
488
489 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
490 ++str;
491
492 /* Gather the operand. */
493 hold = input_line_pointer;
494 input_line_pointer = str;
495
496 if ((operand->flags & V850_OPERAND_REG) != 0)
497 {
498 if (!register_name(&ex))
499 {
500 errmsg = "invalid register name";
501 goto error;
502 }
503 }
504 else if ((operand->flags & V850_OPERAND_SRG) != 0)
505 {
506 if (!system_register_name(&ex))
507 {
508 errmsg = "invalid system register name";
509 goto error;
510 }
511 }
512 else if (strncmp(input_line_pointer, "lo(", 3) == 0)
513 {
514 input_line_pointer += 3;
515 expression(&ex);
516
517 if (*input_line_pointer++ != ')')
518 {
519 errmsg = "syntax error: expected `)'";
520 goto error;
521 }
522
523 if (ex.X_op == O_constant)
524 ex.X_add_number &= 0xffff;
525 else
526 {
527 if (fc > MAX_INSN_FIXUPS)
528 as_fatal ("too many fixups");
529
530 fixups[fc].exp = ex;
531 fixups[fc].opindex = *opindex_ptr;
532 fixups[fc].reloc = BFD_RELOC_LO16;
533 fc++;
534 }
535 }
536 else if (strncmp(input_line_pointer, "hi(", 3) == 0)
537 {
538 input_line_pointer += 3;
539 expression(&ex);
540
541 if (*input_line_pointer++ != ')')
542 {
543 errmsg = "syntax error: expected `)'";
544 goto error;
545 }
546
547 if (ex.X_op == O_constant)
548 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
549 else
550 {
551 if (fc > MAX_INSN_FIXUPS)
552 as_fatal ("too many fixups");
553
554 fixups[fc].exp = ex;
555 fixups[fc].opindex = *opindex_ptr;
556 fixups[fc].reloc = BFD_RELOC_HI16;
557 fc++;
558 }
559 }
560 else if (register_name (&ex)
561 && (operand->flags & V850_OPERAND_REG) == 0)
562 {
563 errmsg = "syntax error: register not expected";
564 goto error;
565 }
566 else if (system_register_name (&ex)
567 && (operand->flags & V850_OPERAND_SRG) == 0)
568 {
569 errmsg = "syntax error: system register not expected";
570 goto error;
571 }
572 else
573 {
574 expression(&ex);
575 }
576
577 str = input_line_pointer;
578 input_line_pointer = hold;
579
580 switch (ex.X_op)
581 {
582 case O_illegal:
583 errmsg = "illegal operand";
584 goto error;
585 case O_absent:
586 errmsg = "missing operand";
587 goto error;
588 case O_register:
589 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
590 {
591 errmsg = "invalid operand";
592 goto error;
593 }
594
595 insn = v850_insert_operand (insn, operand, ex.X_add_number,
596 (char *) NULL, 0);
597 break;
598
599 case O_constant:
600 insn = v850_insert_operand (insn, operand, ex.X_add_number,
601 (char *) NULL, 0);
602 break;
603
604 default:
605 /* We need to generate a fixup for this expression. */
606 if (fc >= MAX_INSN_FIXUPS)
607 as_fatal ("too many fixups");
608 fixups[fc].exp = ex;
609 fixups[fc].opindex = *opindex_ptr;
610 fixups[fc].reloc = BFD_RELOC_UNUSED;
611 ++fc;
612 break;
613 }
614
615 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
616 ++str;
617 }
618 match = 1;
619
620 error:
621 if (match == 0)
622 {
623 next_opcode = opcode + 1;
624 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
625 {
626 opcode = next_opcode;
627 continue;
628 }
629
630 as_bad ("%s", errmsg);
631 return;
632 }
633 break;
634 }
635
636 while (isspace (*str))
637 ++str;
638
639 if (*str != '\0')
640 as_bad ("junk at end of line: `%s'", str);
641
642 input_line_pointer = str;
643
644 f = frag_more (opcode->size);
645 md_number_to_chars (f, insn, opcode->size);
646 }
647
648
649 /* if while processing a fixup, a reloc really needs to be created */
650 /* then it is done here */
651
652 arelent *
653 tc_gen_reloc (seg, fixp)
654 asection *seg;
655 fixS *fixp;
656 {
657 arelent *reloc;
658 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
659 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
660 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
661 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
662 if (reloc->howto == (reloc_howto_type *) NULL)
663 {
664 as_bad_where (fixp->fx_file, fixp->fx_line,
665 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
666 return NULL;
667 }
668 reloc->addend = fixp->fx_addnumber;
669 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
670 return reloc;
671 }
672
673 int
674 md_estimate_size_before_relax (fragp, seg)
675 fragS *fragp;
676 asection *seg;
677 {
678 abort ();
679 return 0;
680 }
681
682 long
683 md_pcrel_from_section (fixp, sec)
684 fixS *fixp;
685 segT sec;
686 {
687 return 0;
688 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
689 }
690
691 int
692 md_apply_fix3 (fixp, valuep, seg)
693 fixS *fixp;
694 valueT *valuep;
695 segT seg;
696 {
697 abort();
698 #if 0
699 valueT value;
700 char *where;
701 unsigned long insn;
702 int op_type;
703
704 if (fixp->fx_addsy == (symbolS *) NULL)
705 {
706 value = *valuep;
707 fixp->fx_done = 1;
708 }
709 else if (fixp->fx_pcrel)
710 value = *valuep;
711 else
712 {
713 value = fixp->fx_offset;
714 if (fixp->fx_subsy != (symbolS *) NULL)
715 {
716 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
717 value -= S_GET_VALUE (fixp->fx_subsy);
718 else
719 {
720 /* We don't actually support subtracting a symbol. */
721 as_bad_where (fixp->fx_file, fixp->fx_line,
722 "expression too complex");
723 }
724 }
725 }
726
727 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
728
729 op_type = fixp->fx_r_type;
730 fixp->fx_r_type = get_reloc((struct v850_operand *)&v850_operands[op_type]);
731
732 /* printf("reloc=%d\n",fixp->fx_r_type); */
733
734 /* Fetch the instruction, insert the fully resolved operand
735 value, and stuff the instruction back again. */
736 where = fixp->fx_frag->fr_literal + fixp->fx_where;
737 insn = bfd_getb32 ((unsigned char *) where);
738 /* printf(" insn=%x value=%x\n",insn,value); */
739
740 insn = v850_insert_operand (insn, op_type, (offsetT) value);
741
742 /* printf(" new insn=%x\n",insn); */
743
744 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
745
746 if (fixp->fx_done)
747 return 1;
748
749 fixp->fx_addnumber = value;
750 return 1;
751 #endif
752 }
753
754 \f
755 /* Insert an operand value into an instruction. */
756
757 static unsigned long
758 v850_insert_operand (insn, operand, val, file, line)
759 unsigned long insn;
760 const struct v850_operand *operand;
761 offsetT val;
762 char *file;
763 unsigned int line;
764 {
765 if (operand->bits != 32)
766 {
767 long min, max;
768 offsetT test;
769
770 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
771 {
772 #if 0
773 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
774 && ppc_size == PPC_OPCODE_32)
775 max = (1 << operand->bits) - 1;
776 else
777 #endif
778 max = (1 << (operand->bits - 1)) - 1;
779 min = - (1 << (operand->bits - 1));
780 }
781 else
782 {
783 max = (1 << operand->bits) - 1;
784 min = 0;
785 }
786
787 #if 0
788 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
789 test = - val;
790 else
791 #endif
792 test = val;
793
794
795 if (test < (offsetT) min || test > (offsetT) max)
796 {
797 const char *err =
798 "operand out of range (%s not between %ld and %ld)";
799 char buf[100];
800
801 sprint_value (buf, test);
802 if (file == (char *) NULL)
803 as_warn (err, buf, min, max);
804 else
805 as_warn_where (file, line, err, buf, min, max);
806 }
807 }
808
809 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
810 return insn;
811 }