* config/tc-v850.c: New file.
[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 pd_reg
96 {
97 char *name;
98 int value;
99 };
100
101
102 /* an expressionS only has one register type, so we fake it */
103 /* by setting high bits to indicate type */
104 #define REGISTER_MASK 0xFF
105
106 /* The table is sorted. Suitable for searching by a binary search. */
107 static const struct pd_reg pre_defined_registers[] =
108 {
109 { "ep", 30 }, /* ep - element ptr */
110 { "gp", 4 }, /* gp - global ptr */
111 { "lp", 31 }, /* lp - link ptr */
112 { "r0", 0 },
113 { "r1", 1 },
114 { "r10", 10 },
115 { "r11", 11 },
116 { "r12", 12 },
117 { "r13", 13 },
118 { "r14", 14 },
119 { "r15", 15 },
120 { "r16", 16 },
121 { "r17", 17 },
122 { "r18", 18 },
123 { "r19", 19 },
124 { "r2", 2 },
125 { "r20", 20 },
126 { "r21", 21 },
127 { "r22", 22 },
128 { "r23", 23 },
129 { "r24", 24 },
130 { "r25", 25 },
131 { "r26", 26 },
132 { "r27", 27 },
133 { "r28", 28 },
134 { "r29", 29 },
135 { "r3", 3 },
136 { "r30", 30 },
137 { "r31", 31 },
138 { "r4", 4 },
139 { "r5", 5 },
140 { "r6", 6 },
141 { "r7", 7 },
142 { "r8", 8 },
143 { "r9", 9 },
144 { "sp", 3 }, /* sp - stack ptr */
145 { "tp", 5 }, /* tp - text ptr */
146 { "zero", 0 },
147 };
148 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct pd_reg))
149
150 /* reg_name_search does a binary search of the pre_defined_registers
151 array to see if "name" is a valid regiter name. Returns the register
152 number from the array on success, or -1 on failure. */
153
154 static int
155 reg_name_search (name)
156 char *name;
157 {
158 int middle, low, high;
159 int cmp;
160
161 low = 0;
162 high = REG_NAME_CNT - 1;
163
164 do
165 {
166 middle = (low + high) / 2;
167 cmp = strcasecmp (name, pre_defined_registers[middle].name);
168 if (cmp < 0)
169 high = middle - 1;
170 else if (cmp > 0)
171 low = middle + 1;
172 else
173 return pre_defined_registers[middle].value;
174 }
175 while (low <= high);
176 return -1;
177 }
178
179
180 /* Summary of register_name().
181 *
182 * in: Input_line_pointer points to 1st char of operand.
183 *
184 * out: A expressionS.
185 * The operand may have been a register: in this case, X_op == O_register,
186 * X_add_number is set to the register number, and truth is returned.
187 * Input_line_pointer->(next non-blank) char after operand, or is in
188 * its original state.
189 */
190 static boolean
191 register_name (expressionP)
192 expressionS *expressionP;
193 {
194 int reg_number;
195 char *name;
196 char *start;
197 char c;
198
199 /* Find the spelling of the operand */
200 start = name = input_line_pointer;
201
202 c = get_symbol_end ();
203 reg_number = reg_name_search (name);
204
205 /* look to see if it's in the register table */
206 if (reg_number >= 0)
207 {
208 expressionP->X_op = O_register;
209 expressionP->X_add_number = reg_number;
210
211 /* make the rest nice */
212 expressionP->X_add_symbol = NULL;
213 expressionP->X_op_symbol = NULL;
214 *input_line_pointer = c; /* put back the delimiting char */
215 return true;
216 }
217 else
218 {
219 /* reset the line as if we had not done anything */
220 *input_line_pointer = c; /* put back the delimiting char */
221 input_line_pointer = start; /* reset input_line pointer */
222 return false;
223 }
224 }
225
226 void
227 md_show_usage (stream)
228 FILE *stream;
229 {
230 fprintf(stream, "V850 options:\n\
231 none yet\n");
232 }
233
234 int
235 md_parse_option (c, arg)
236 int c;
237 char *arg;
238 {
239 return 0;
240 }
241
242 symbolS *
243 md_undefined_symbol (name)
244 char *name;
245 {
246 return 0;
247 }
248
249 char *
250 md_atof (type, litp, sizep)
251 int type;
252 char *litp;
253 int *sizep;
254 {
255 int prec;
256 LITTLENUM_TYPE words[4];
257 char *t;
258 int i;
259
260 switch (type)
261 {
262 case 'f':
263 prec = 2;
264 break;
265
266 case 'd':
267 prec = 4;
268 break;
269
270 default:
271 *sizep = 0;
272 return "bad call to md_atof";
273 }
274
275 t = atof_ieee (input_line_pointer, type, words);
276 if (t)
277 input_line_pointer = t;
278
279 *sizep = prec * 2;
280
281 for (i = prec - 1; i >= 0; i--)
282 {
283 md_number_to_chars (litp, (valueT) words[i], 2);
284 litp += 2;
285 }
286
287 return NULL;
288 }
289
290
291 void
292 md_convert_frag (abfd, sec, fragP)
293 bfd *abfd;
294 asection *sec;
295 fragS *fragP;
296 {
297 /* printf ("call to md_convert_frag \n"); */
298 abort ();
299 }
300
301 valueT
302 md_section_align (seg, addr)
303 asection *seg;
304 valueT addr;
305 {
306 int align = bfd_get_section_alignment (stdoutput, seg);
307 return ((addr + (1 << align) - 1) & (-1 << align));
308 }
309
310 void
311 md_begin ()
312 {
313 char *prev_name = "";
314 register const struct v850_opcode *op;
315 const struct v850_opcode *op_end;
316
317 v850_hash = hash_new();
318
319 /* Insert unique names into hash table. The V850 instruction set
320 has many identical opcode names that have different opcodes based
321 on the operands. This hash table then provides a quick index to
322 the first opcode with a particular name in the opcode table. */
323
324 op = v850_opcodes;
325 op_end = v850_opcodes + v850_num_opcodes;
326
327 for (; op < op_end; op++)
328 {
329 if (strcmp (prev_name, op->name))
330 {
331 prev_name = (char *) op->name;
332 hash_insert (v850_hash, op->name, (char *) op);
333 }
334 }
335 }
336
337
338 static bfd_reloc_code_real_type
339 get_reloc (op)
340 struct v850_operand *op;
341 {
342 abort ();
343 }
344
345
346 /* get_operands parses a string of operands and returns and array of
347 expressions. */
348 static int
349 get_operands (exp)
350 expressionS exp[];
351 {
352 char *p = input_line_pointer;
353 int numops = 0;
354
355 while (*p)
356 {
357 while (*p == ' ' || *p == '\t' || *p == ',')
358 p++;
359 if (*p==0 || *p=='\n' || *p=='\r')
360 break;
361
362 input_line_pointer = p;
363
364 if (!register_name(&exp[numops]))
365 expression(&exp[numops]);
366
367 numops++;
368 p = input_line_pointer;
369 }
370
371 exp[numops].X_op = 0;
372 return (numops);
373 }
374
375
376
377 void
378 md_assemble (str)
379 char *str;
380 {
381 char *s;
382 struct v850_opcode *opcode;
383 struct v850_opcode *next_opcode;
384 const unsigned char *opindex_ptr;
385 int next_opindex;
386 unsigned long insn;
387 char *f;
388 int i;
389 int numops;
390 int match;
391
392 int numopts;
393 expressionS myops[5];
394
395 /* Get the opcode. */
396 for (s = str; *s != '\0' && ! isspace (*s); s++)
397 ;
398 if (*s != '\0')
399 *s++ = '\0';
400
401 /* find the first opcode with the proper name */
402 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
403 if (opcode == NULL)
404 {
405 as_bad ("Unrecognized opcode: `%s'", str);
406 return;
407 }
408
409 str = s;
410 while (isspace (*str))
411 ++str;
412
413 input_line_pointer = str;
414
415 /* get all the operands and save them as expressions */
416 numops = get_operands (myops);
417
418 /* now search the opcode table for one with operands that matches
419 what we've got */
420 match = 0;
421 while (!match)
422 {
423 match = 1;
424 for (i = 0; opcode->operands[i]; i++)
425 {
426 int flags = v850_operands[opcode->operands[i]].flags;
427 int X_op = myops[i].X_op;
428 int num = myops[i].X_add_number;
429
430 if (X_op == 0)
431 {
432 match = 0;
433 break;
434 }
435
436 if (flags & OPERAND_REG)
437 {
438 if (X_op != O_register)
439 {
440 match = 0;
441 break;
442 }
443 }
444 }
445
446 /* we're only done if the operands matched so far AND there are
447 no more to check. */
448 if (match && myops[i].X_op == 0)
449 break;
450
451 next_opcode = opcode + 1;
452 if (next_opcode->opcode == 0)
453 break;
454 if (strcmp (next_opcode->name, opcode->name))
455 break;
456 opcode = next_opcode;
457 }
458
459 if (!match)
460 {
461 as_bad ("Unrecognized operands");
462 return;
463 }
464
465 insn = opcode->opcode;
466
467 #if 0
468 while (isspace (*str))
469 ++str;
470
471 if (*str != '\0')
472 as_bad ("junk at end of line: `%s'", str);
473 #endif
474
475 /* Write out the instruction. */
476 f = frag_more (2);
477 md_number_to_chars (f, insn, 2);
478 }
479
480
481 /* if while processing a fixup, a reloc really needs to be created */
482 /* then it is done here */
483
484 arelent *
485 tc_gen_reloc (seg, fixp)
486 asection *seg;
487 fixS *fixp;
488 {
489 arelent *reloc;
490 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
491 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
492 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
493 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
494 if (reloc->howto == (reloc_howto_type *) NULL)
495 {
496 as_bad_where (fixp->fx_file, fixp->fx_line,
497 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
498 return NULL;
499 }
500 reloc->addend = fixp->fx_addnumber;
501 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
502 return reloc;
503 }
504
505 int
506 md_estimate_size_before_relax (fragp, seg)
507 fragS *fragp;
508 asection *seg;
509 {
510 abort ();
511 return 0;
512 }
513
514 long
515 md_pcrel_from_section (fixp, sec)
516 fixS *fixp;
517 segT sec;
518 {
519 return 0;
520 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
521 }
522
523 int
524 md_apply_fix3 (fixp, valuep, seg)
525 fixS *fixp;
526 valueT *valuep;
527 segT seg;
528 {
529 abort();
530 #if 0
531 valueT value;
532 char *where;
533 unsigned long insn;
534 int op_type;
535
536 if (fixp->fx_addsy == (symbolS *) NULL)
537 {
538 value = *valuep;
539 fixp->fx_done = 1;
540 }
541 else if (fixp->fx_pcrel)
542 value = *valuep;
543 else
544 {
545 value = fixp->fx_offset;
546 if (fixp->fx_subsy != (symbolS *) NULL)
547 {
548 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
549 value -= S_GET_VALUE (fixp->fx_subsy);
550 else
551 {
552 /* We don't actually support subtracting a symbol. */
553 as_bad_where (fixp->fx_file, fixp->fx_line,
554 "expression too complex");
555 }
556 }
557 }
558
559 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
560
561 op_type = fixp->fx_r_type;
562 fixp->fx_r_type = get_reloc((struct v850_operand *)&v850_operands[op_type]);
563
564 /* printf("reloc=%d\n",fixp->fx_r_type); */
565
566 /* Fetch the instruction, insert the fully resolved operand
567 value, and stuff the instruction back again. */
568 where = fixp->fx_frag->fr_literal + fixp->fx_where;
569 insn = bfd_getb32 ((unsigned char *) where);
570 /* printf(" insn=%x value=%x\n",insn,value); */
571
572 insn = v850_insert_operand (insn, op_type, (offsetT) value);
573
574 /* printf(" new insn=%x\n",insn); */
575
576 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
577
578 if (fixp->fx_done)
579 return 1;
580
581 fixp->fx_addnumber = value;
582 return 1;
583 #endif
584 }
585
586 \f
587 /* Insert an operand value into an instruction. */
588
589 static unsigned long
590 v850_insert_operand (insn, operand, val, file, line)
591 unsigned long insn;
592 const struct v850_operand *operand;
593 offsetT val;
594 char *file;
595 unsigned int line;
596 {
597 if (operand->bits != 32)
598 {
599 long min, max;
600 offsetT test;
601
602 #if 0
603 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
604 {
605 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
606 && ppc_size == PPC_OPCODE_32)
607 max = (1 << operand->bits) - 1;
608 else
609 max = (1 << (operand->bits - 1)) - 1;
610 min = - (1 << (operand->bits - 1));
611 }
612 else
613 #endif
614 {
615 max = (1 << operand->bits) - 1;
616 min = 0;
617 }
618
619 #if 0
620 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
621 test = - val;
622 else
623 #endif
624 test = val;
625
626
627 if (test < (offsetT) min || test > (offsetT) max)
628 {
629 const char *err =
630 "operand out of range (%s not between %ld and %ld)";
631 char buf[100];
632
633 sprint_value (buf, test);
634 if (file == (char *) NULL)
635 as_warn (err, buf, min, max);
636 else
637 as_warn_where (file, line, err, buf, min, max);
638 }
639 }
640
641 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
642 return insn;
643 }