parse [reg], lo(exp), and hi(exp)
[deliverable/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 /* skip trailing parens */
363 if (*p == ')' || *p == ']')
364 {
365 p++;
366 continue;
367 }
368
369 if (*p == '[')
370 {
371 p++;
372 input_line_pointer = p;
373 register_name(&exp[numops]);
374 }
375 else if (strncmp(p, "lo(", 3) == 0)
376 {
377 p += 3;
378 input_line_pointer = p;
379 expression(&exp[numops]);
380 }
381 else if (strncmp(p, "hi(", 3) == 0)
382 {
383 p += 3;
384 input_line_pointer = p;
385 expression(&exp[numops]);
386 }
387 else
388 {
389 input_line_pointer = p;
390 if (!register_name(&exp[numops]))
391 expression(&exp[numops]);
392 }
393
394 p = input_line_pointer;
395 numops++;
396 }
397
398 input_line_pointer = p;
399
400 exp[numops].X_op = 0;
401 return (numops);
402 }
403
404
405
406 void
407 md_assemble (str)
408 char *str;
409 {
410 char *s;
411 struct v850_opcode *opcode;
412 struct v850_opcode *next_opcode;
413 const unsigned char *opindex_ptr;
414 int next_opindex;
415 unsigned long insn;
416 char *f;
417 int i;
418 int numops;
419 int match;
420
421 int numopts;
422 expressionS myops[5];
423
424 /* Get the opcode. */
425 for (s = str; *s != '\0' && ! isspace (*s); s++)
426 ;
427 if (*s != '\0')
428 *s++ = '\0';
429
430 /* find the first opcode with the proper name */
431 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
432 if (opcode == NULL)
433 {
434 as_bad ("Unrecognized opcode: `%s'", str);
435 return;
436 }
437
438 str = s;
439 while (isspace (*str))
440 ++str;
441
442 input_line_pointer = str;
443
444 /* get all the operands and save them as expressions */
445 numops = get_operands (myops);
446
447 /* now search the opcode table for one with operands that matches
448 what we've got */
449 match = 0;
450 while (!match)
451 {
452 match = 1;
453 for (i = 0; opcode->operands[i]; i++)
454 {
455 int flags = v850_operands[opcode->operands[i]].flags;
456 int X_op = myops[i].X_op;
457 int num = myops[i].X_add_number;
458
459 if (X_op == 0)
460 {
461 match = 0;
462 break;
463 }
464
465 if (flags & OPERAND_REG)
466 {
467 if (X_op != O_register)
468 {
469 match = 0;
470 break;
471 }
472 }
473 }
474
475 /* we're only done if the operands matched so far AND there are
476 no more to check. */
477 if (match && myops[i].X_op == 0)
478 break;
479
480 next_opcode = opcode + 1;
481 if (next_opcode->opcode == 0)
482 break;
483 if (strcmp (next_opcode->name, opcode->name))
484 break;
485 opcode = next_opcode;
486 }
487
488 if (!match)
489 {
490 as_bad ("Unrecognized operands");
491 return;
492 }
493
494 insn = opcode->opcode;
495
496 #if 0
497 while (isspace (*str))
498 ++str;
499
500 if (*str != '\0')
501 as_bad ("junk at end of line: `%s'", str);
502 #endif
503
504 /* Write out the instruction. */
505 if ((insn & 0x0600) == 0x0600)
506 {
507 f = frag_more (4);
508 md_number_to_chars (f, insn, 4);
509 }
510 else
511 {
512 f = frag_more (2);
513 md_number_to_chars (f, insn, 2);
514 }
515 }
516
517
518 /* if while processing a fixup, a reloc really needs to be created */
519 /* then it is done here */
520
521 arelent *
522 tc_gen_reloc (seg, fixp)
523 asection *seg;
524 fixS *fixp;
525 {
526 arelent *reloc;
527 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
528 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
529 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
530 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
531 if (reloc->howto == (reloc_howto_type *) NULL)
532 {
533 as_bad_where (fixp->fx_file, fixp->fx_line,
534 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
535 return NULL;
536 }
537 reloc->addend = fixp->fx_addnumber;
538 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
539 return reloc;
540 }
541
542 int
543 md_estimate_size_before_relax (fragp, seg)
544 fragS *fragp;
545 asection *seg;
546 {
547 abort ();
548 return 0;
549 }
550
551 long
552 md_pcrel_from_section (fixp, sec)
553 fixS *fixp;
554 segT sec;
555 {
556 return 0;
557 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
558 }
559
560 int
561 md_apply_fix3 (fixp, valuep, seg)
562 fixS *fixp;
563 valueT *valuep;
564 segT seg;
565 {
566 abort();
567 #if 0
568 valueT value;
569 char *where;
570 unsigned long insn;
571 int op_type;
572
573 if (fixp->fx_addsy == (symbolS *) NULL)
574 {
575 value = *valuep;
576 fixp->fx_done = 1;
577 }
578 else if (fixp->fx_pcrel)
579 value = *valuep;
580 else
581 {
582 value = fixp->fx_offset;
583 if (fixp->fx_subsy != (symbolS *) NULL)
584 {
585 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
586 value -= S_GET_VALUE (fixp->fx_subsy);
587 else
588 {
589 /* We don't actually support subtracting a symbol. */
590 as_bad_where (fixp->fx_file, fixp->fx_line,
591 "expression too complex");
592 }
593 }
594 }
595
596 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
597
598 op_type = fixp->fx_r_type;
599 fixp->fx_r_type = get_reloc((struct v850_operand *)&v850_operands[op_type]);
600
601 /* printf("reloc=%d\n",fixp->fx_r_type); */
602
603 /* Fetch the instruction, insert the fully resolved operand
604 value, and stuff the instruction back again. */
605 where = fixp->fx_frag->fr_literal + fixp->fx_where;
606 insn = bfd_getb32 ((unsigned char *) where);
607 /* printf(" insn=%x value=%x\n",insn,value); */
608
609 insn = v850_insert_operand (insn, op_type, (offsetT) value);
610
611 /* printf(" new insn=%x\n",insn); */
612
613 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
614
615 if (fixp->fx_done)
616 return 1;
617
618 fixp->fx_addnumber = value;
619 return 1;
620 #endif
621 }
622
623 \f
624 /* Insert an operand value into an instruction. */
625
626 static unsigned long
627 v850_insert_operand (insn, operand, val, file, line)
628 unsigned long insn;
629 const struct v850_operand *operand;
630 offsetT val;
631 char *file;
632 unsigned int line;
633 {
634 if (operand->bits != 32)
635 {
636 long min, max;
637 offsetT test;
638
639 #if 0
640 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
641 {
642 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
643 && ppc_size == PPC_OPCODE_32)
644 max = (1 << operand->bits) - 1;
645 else
646 max = (1 << (operand->bits - 1)) - 1;
647 min = - (1 << (operand->bits - 1));
648 }
649 else
650 #endif
651 {
652 max = (1 << operand->bits) - 1;
653 min = 0;
654 }
655
656 #if 0
657 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
658 test = - val;
659 else
660 #endif
661 test = val;
662
663
664 if (test < (offsetT) min || test > (offsetT) max)
665 {
666 const char *err =
667 "operand out of range (%s not between %ld and %ld)";
668 char buf[100];
669
670 sprint_value (buf, test);
671 if (file == (char *) NULL)
672 as_warn (err, buf, min, max);
673 else
674 as_warn_where (file, line, err, buf, min, max);
675 }
676 }
677
678 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
679 return insn;
680 }
This page took 0.042678 seconds and 4 git commands to generate.