* config/tc-v850.c (md_assemble): If we find a register, but the
[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 /* Generic assembler global variables which must be defined by all targets. */
29
30 /* Characters which always start a comment. */
31 const char comment_chars[] = "#";
32
33 /* Characters which start a comment at the beginning of a line. */
34 const char line_comment_chars[] = ";#";
35
36 /* Characters which may be used to separate multiple commands on a
37 single line. */
38 const char line_separator_chars[] = ";";
39
40 /* Characters which are used to indicate an exponent in a floating
41 point number. */
42 const char EXP_CHARS[] = "eE";
43
44 /* Characters which mean that a number is a floating point constant,
45 as in 0d1.0. */
46 const char FLT_CHARS[] = "dD";
47 \f
48 /* local functions */
49 static unsigned long v850_insert_operand
50 PARAMS ((unsigned long insn, const struct v850_operand *operand,
51 offsetT val, char *file, unsigned int line));
52 static int reg_name_search PARAMS ((char *name));
53 static boolean register_name PARAMS ((expressionS *expressionP));
54 static int postfix PARAMS ((char *p));
55 static bfd_reloc_code_real_type get_reloc PARAMS ((struct v850_operand *op));
56 static unsigned long build_insn PARAMS ((struct v850_opcode *opcode, expressionS *opers));
57
58
59 /* fixups */
60 #define MAX_INSN_FIXUPS (5)
61 struct v850_fixup
62 {
63 expressionS exp;
64 int opindex;
65 bfd_reloc_code_real_type reloc;
66 };
67 struct v850_fixup fixups[MAX_INSN_FIXUPS];
68 static int fc;
69 \f
70 const char *md_shortopts = "";
71 struct option md_longopts[] = {
72 {NULL, no_argument, NULL, 0}
73 };
74 size_t md_longopts_size = sizeof(md_longopts);
75
76 /* The target specific pseudo-ops which we support. */
77 const pseudo_typeS md_pseudo_table[] =
78 {
79 /*
80 { "byte", ppc_byte, 0 },
81 { "long", ppc_elf_cons, 4 },
82 { "word", ppc_elf_cons, 2 },
83 { "short", ppc_elf_cons, 2 },
84 { "rdata", ppc_elf_rdata, 0 },
85 { "rodata", ppc_elf_rdata, 0 },
86 { "lcomm", ppc_elf_lcomm, 0 },
87 */
88 { NULL, NULL, 0 }
89 };
90
91 /* Opcode hash table. */
92 static struct hash_control *v850_hash;
93
94 /* Structure to hold information about predefined registers. */
95 struct reg_name
96 {
97 const char *name;
98 int value;
99 };
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 { "eipc", 0 },
149 { "eipsw", 1 },
150 { "fepc", 2 },
151 { "fepsw", 3 },
152 { "ecr", 4 },
153 { "psw", 5 },
154 };
155
156 static const struct reg_name cc_names[] =
157 {
158 { "c", 0x1 },
159 { "ge", 0xe },
160 { "gt", 0xf },
161 { "h", 0xb },
162 { "l", 0x1 },
163 { "le", 0x7 },
164 { "lt", 0x6 },
165 { "n", 0x4 },
166 { "nc", 0x9 },
167 { "nh", 0x3 },
168 { "nl", 0x9 },
169 { "ns", 0xc },
170 { "nv", 0x8 },
171 { "nz", 0xa },
172 { "p", 0xc },
173 { "s", 0x4 },
174 { "sa", 0xd },
175 { "t", 0x5 },
176 { "v", 0x0 },
177 { "z", 0x2 },
178 };
179
180 /* reg_name_search does a binary search of the pre_defined_registers
181 array to see if "name" is a valid regiter name. Returns the register
182 number from the array on success, or -1 on failure. */
183
184 static int
185 reg_name_search (name)
186 char *name;
187 {
188 int middle, low, high;
189 int cmp;
190
191 low = 0;
192 high = REG_NAME_CNT - 1;
193
194 do
195 {
196 middle = (low + high) / 2;
197 cmp = strcasecmp (name, pre_defined_registers[middle].name);
198 if (cmp < 0)
199 high = middle - 1;
200 else if (cmp > 0)
201 low = middle + 1;
202 else
203 return pre_defined_registers[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 (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 void
257 md_show_usage (stream)
258 FILE *stream;
259 {
260 fprintf(stream, "V850 options:\n\
261 none yet\n");
262 }
263
264 int
265 md_parse_option (c, arg)
266 int c;
267 char *arg;
268 {
269 return 0;
270 }
271
272 symbolS *
273 md_undefined_symbol (name)
274 char *name;
275 {
276 return 0;
277 }
278
279 char *
280 md_atof (type, litp, sizep)
281 int type;
282 char *litp;
283 int *sizep;
284 {
285 int prec;
286 LITTLENUM_TYPE words[4];
287 char *t;
288 int i;
289
290 switch (type)
291 {
292 case 'f':
293 prec = 2;
294 break;
295
296 case 'd':
297 prec = 4;
298 break;
299
300 default:
301 *sizep = 0;
302 return "bad call to md_atof";
303 }
304
305 t = atof_ieee (input_line_pointer, type, words);
306 if (t)
307 input_line_pointer = t;
308
309 *sizep = prec * 2;
310
311 for (i = prec - 1; i >= 0; i--)
312 {
313 md_number_to_chars (litp, (valueT) words[i], 2);
314 litp += 2;
315 }
316
317 return NULL;
318 }
319
320
321 void
322 md_convert_frag (abfd, sec, fragP)
323 bfd *abfd;
324 asection *sec;
325 fragS *fragP;
326 {
327 /* printf ("call to md_convert_frag \n"); */
328 abort ();
329 }
330
331 valueT
332 md_section_align (seg, addr)
333 asection *seg;
334 valueT addr;
335 {
336 int align = bfd_get_section_alignment (stdoutput, seg);
337 return ((addr + (1 << align) - 1) & (-1 << align));
338 }
339
340 void
341 md_begin ()
342 {
343 char *prev_name = "";
344 register const struct v850_opcode *op;
345 const struct v850_opcode *op_end;
346
347 v850_hash = hash_new();
348
349 /* Insert unique names into hash table. The V850 instruction set
350 has many identical opcode names that have different opcodes based
351 on the operands. This hash table then provides a quick index to
352 the first opcode with a particular name in the opcode table. */
353
354 op = v850_opcodes;
355 op_end = v850_opcodes + v850_num_opcodes;
356
357 for (; op < op_end; op++)
358 {
359 if (strcmp (prev_name, op->name))
360 {
361 prev_name = (char *) op->name;
362 hash_insert (v850_hash, op->name, (char *) op);
363 }
364 }
365 }
366
367
368 static bfd_reloc_code_real_type
369 get_reloc (op)
370 struct v850_operand *op;
371 {
372 abort ();
373 }
374
375
376 void
377 md_assemble (str)
378 char *str;
379 {
380 char *s;
381 struct v850_opcode *opcode;
382 struct v850_opcode *next_opcode;
383 const unsigned char *opindex_ptr;
384 int next_opindex;
385 unsigned long insn;
386 char *f;
387 int i;
388 int numops;
389 int match;
390
391 int numopts;
392 expressionS myops[5];
393
394 /* Get the opcode. */
395 for (s = str; *s != '\0' && ! isspace (*s); s++)
396 ;
397 if (*s != '\0')
398 *s++ = '\0';
399
400 /* find the first opcode with the proper name */
401 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
402 if (opcode == NULL)
403 {
404 as_bad ("Unrecognized opcode: `%s'", str);
405 return;
406 }
407
408 str = s;
409 while (isspace (*str))
410 ++str;
411
412 input_line_pointer = str;
413
414 for(;;)
415 {
416 const char *errmsg = NULL;
417
418 fc = 0;
419 match = 0;
420 next_opindex = 0;
421 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
422 {
423 const struct v850_operand *operand;
424 char *hold;
425 expressionS ex;
426 char endc;
427
428 if (next_opindex == 0)
429 {
430 operand = &v850_operands[*opindex_ptr];
431 }
432 else
433 {
434 operand = &v850_operands[next_opindex];
435 next_opindex = 0;
436 }
437
438 errmsg = NULL;
439
440 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
441 ++str;
442
443 /* Gather the operand. */
444 hold = input_line_pointer;
445 input_line_pointer = str;
446
447 if ((operand->flags & V850_OPERAND_REG) != 0)
448 {
449 if (!register_name(&ex))
450 {
451 errmsg = "invalid register name";
452 goto error;
453 }
454 }
455 else if (strncmp(input_line_pointer, "lo(", 3) == 0)
456 {
457 input_line_pointer += 3;
458 expression(&ex);
459
460 if (*input_line_pointer++ != ')')
461 {
462 errmsg = "syntax error: expected `)'";
463 goto error;
464 }
465
466 if (ex.X_op == O_constant)
467 ex.X_add_number &= 0xffff;
468 else
469 {
470 if (fc > MAX_INSN_FIXUPS)
471 as_fatal ("too many fixups");
472
473 fixups[fc].exp = ex;
474 fixups[fc].opindex = *opindex_ptr;
475 fixups[fc].reloc = BFD_RELOC_LO16;
476 fc++;
477 }
478 }
479 else if (strncmp(input_line_pointer, "hi(", 3) == 0)
480 {
481 input_line_pointer += 3;
482 expression(&ex);
483
484 if (*input_line_pointer++ != ')')
485 {
486 errmsg = "syntax error: expected `)'";
487 goto error;
488 }
489
490 if (ex.X_op == O_constant)
491 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
492 else
493 {
494 if (fc > MAX_INSN_FIXUPS)
495 as_fatal ("too many fixups");
496
497 fixups[fc].exp = ex;
498 fixups[fc].opindex = *opindex_ptr;
499 fixups[fc].reloc = BFD_RELOC_HI16;
500 fc++;
501 }
502 }
503 else if (register_name (&ex)
504 && (operand->flags & V850_OPERAND_REG) == 0)
505 {
506 errmsg = "syntax error: register not expected";
507 goto error;
508 }
509 else
510 {
511 expression(&ex);
512 }
513
514 str = input_line_pointer;
515 input_line_pointer = hold;
516
517 switch (ex.X_op)
518 {
519 case O_illegal:
520 errmsg = "illegal operand";
521 goto error;
522 case O_absent:
523 errmsg = "missing operand";
524 goto error;
525 case O_register:
526 if ((operand->flags & V850_OPERAND_REG) == 0)
527 {
528 errmsg = "invalid operand";
529 goto error;
530 }
531
532 insn = v850_insert_operand (insn, operand, ex.X_add_number,
533 (char *) NULL, 0);
534 break;
535
536 case O_constant:
537 insn = v850_insert_operand (insn, operand, ex.X_add_number,
538 (char *) NULL, 0);
539 break;
540
541 default:
542 /* We need to generate a fixup for this expression. */
543 if (fc >= MAX_INSN_FIXUPS)
544 as_fatal ("too many fixups");
545 fixups[fc].exp = ex;
546 fixups[fc].opindex = *opindex_ptr;
547 fixups[fc].reloc = BFD_RELOC_UNUSED;
548 ++fc;
549 break;
550 }
551
552 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
553 ++str;
554 }
555 match = 1;
556
557 error:
558 if (match == 0)
559 {
560 next_opcode = opcode + 1;
561 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
562 {
563 opcode = next_opcode;
564 continue;
565 }
566
567 as_bad ("%s", errmsg);
568 return;
569 }
570 break;
571 }
572
573 while (isspace (*str))
574 ++str;
575
576 if (*str != '\0')
577 as_bad ("junk at end of line: `%s'", str);
578
579 input_line_pointer = str;
580
581 f = frag_more (opcode->size);
582 md_number_to_chars (f, insn, opcode->size);
583 }
584
585
586 /* if while processing a fixup, a reloc really needs to be created */
587 /* then it is done here */
588
589 arelent *
590 tc_gen_reloc (seg, fixp)
591 asection *seg;
592 fixS *fixp;
593 {
594 arelent *reloc;
595 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
596 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
597 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
598 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
599 if (reloc->howto == (reloc_howto_type *) NULL)
600 {
601 as_bad_where (fixp->fx_file, fixp->fx_line,
602 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
603 return NULL;
604 }
605 reloc->addend = fixp->fx_addnumber;
606 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
607 return reloc;
608 }
609
610 int
611 md_estimate_size_before_relax (fragp, seg)
612 fragS *fragp;
613 asection *seg;
614 {
615 abort ();
616 return 0;
617 }
618
619 long
620 md_pcrel_from_section (fixp, sec)
621 fixS *fixp;
622 segT sec;
623 {
624 return 0;
625 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
626 }
627
628 int
629 md_apply_fix3 (fixp, valuep, seg)
630 fixS *fixp;
631 valueT *valuep;
632 segT seg;
633 {
634 abort();
635 #if 0
636 valueT value;
637 char *where;
638 unsigned long insn;
639 int op_type;
640
641 if (fixp->fx_addsy == (symbolS *) NULL)
642 {
643 value = *valuep;
644 fixp->fx_done = 1;
645 }
646 else if (fixp->fx_pcrel)
647 value = *valuep;
648 else
649 {
650 value = fixp->fx_offset;
651 if (fixp->fx_subsy != (symbolS *) NULL)
652 {
653 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
654 value -= S_GET_VALUE (fixp->fx_subsy);
655 else
656 {
657 /* We don't actually support subtracting a symbol. */
658 as_bad_where (fixp->fx_file, fixp->fx_line,
659 "expression too complex");
660 }
661 }
662 }
663
664 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
665
666 op_type = fixp->fx_r_type;
667 fixp->fx_r_type = get_reloc((struct v850_operand *)&v850_operands[op_type]);
668
669 /* printf("reloc=%d\n",fixp->fx_r_type); */
670
671 /* Fetch the instruction, insert the fully resolved operand
672 value, and stuff the instruction back again. */
673 where = fixp->fx_frag->fr_literal + fixp->fx_where;
674 insn = bfd_getb32 ((unsigned char *) where);
675 /* printf(" insn=%x value=%x\n",insn,value); */
676
677 insn = v850_insert_operand (insn, op_type, (offsetT) value);
678
679 /* printf(" new insn=%x\n",insn); */
680
681 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
682
683 if (fixp->fx_done)
684 return 1;
685
686 fixp->fx_addnumber = value;
687 return 1;
688 #endif
689 }
690
691 \f
692 /* Insert an operand value into an instruction. */
693
694 static unsigned long
695 v850_insert_operand (insn, operand, val, file, line)
696 unsigned long insn;
697 const struct v850_operand *operand;
698 offsetT val;
699 char *file;
700 unsigned int line;
701 {
702 if (operand->bits != 32)
703 {
704 long min, max;
705 offsetT test;
706
707 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
708 {
709 #if 0
710 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
711 && ppc_size == PPC_OPCODE_32)
712 max = (1 << operand->bits) - 1;
713 else
714 #endif
715 max = (1 << (operand->bits - 1)) - 1;
716 min = - (1 << (operand->bits - 1));
717 }
718 else
719 {
720 max = (1 << operand->bits) - 1;
721 min = 0;
722 }
723
724 #if 0
725 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
726 test = - val;
727 else
728 #endif
729 test = val;
730
731
732 if (test < (offsetT) min || test > (offsetT) max)
733 {
734 const char *err =
735 "operand out of range (%s not between %ld and %ld)";
736 char buf[100];
737
738 sprint_value (buf, test);
739 if (file == (char *) NULL)
740 as_warn (err, buf, min, max);
741 else
742 as_warn_where (file, line, err, buf, min, max);
743 }
744 }
745
746 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
747 return insn;
748 }