gas/
[deliverable/binutils-gdb.git] / gas / config / tc-v850.c
CommitLineData
252b5132 1/* tc-v850.c -- Assembler code for the NEC V850
ae6063d4 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
507c8a1a 3 Free Software Foundation, Inc.
252b5132
RH
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>
252b5132 23#include "as.h"
3882b010 24#include "safe-ctype.h"
28e4f854 25#include "subsegs.h"
252b5132 26#include "opcode/v850.h"
a8761a19 27#include "dwarf2dbg.h"
252b5132 28
28e4f854
KH
29/* Sign-extend a 16-bit number. */
30#define SEXT16(x) ((((x) & 0xffff) ^ (~0x7fff)) + 0x8000)
252b5132
RH
31
32/* Temporarily holds the reloc in a cons expression. */
a8761a19 33static bfd_reloc_code_real_type hold_cons_reloc = BFD_RELOC_UNUSED;
252b5132
RH
34
35/* Set to TRUE if we want to be pedantic about signed overflows. */
b34976b6
AM
36static bfd_boolean warn_signed_overflows = FALSE;
37static bfd_boolean warn_unsigned_overflows = FALSE;
252b5132
RH
38
39/* Indicates the target BFD machine number. */
28e4f854 40static int machine = -1;
252b5132
RH
41
42/* Indicates the target processor(s) for the assemble. */
28e4f854 43static int processor_mask = -1;
252b5132
RH
44\f
45/* Structure to hold information about predefined registers. */
19d63e5d 46struct reg_name {
28e4f854
KH
47 const char *name;
48 int value;
252b5132
RH
49};
50
28e4f854
KH
51/* Generic assembler global variables which must be defined by all
52 targets. */
252b5132 53
28e4f854 54/* Characters which always start a comment. */
252b5132
RH
55const char comment_chars[] = "#";
56
57/* Characters which start a comment at the beginning of a line. */
58const char line_comment_chars[] = ";#";
59
28e4f854 60/* Characters which may be used to separate multiple commands on a
252b5132
RH
61 single line. */
62const char line_separator_chars[] = ";";
63
28e4f854 64/* Characters which are used to indicate an exponent in a floating
252b5132
RH
65 point number. */
66const char EXP_CHARS[] = "eE";
67
28e4f854 68/* Characters which mean that a number is a floating point constant,
252b5132
RH
69 as in 0d1.0. */
70const char FLT_CHARS[] = "dD";
71\f
19d63e5d 72const relax_typeS md_relax_table[] = {
252b5132
RH
73 /* Conditional branches. */
74 {0xff, -0x100, 2, 1},
75 {0x1fffff, -0x200000, 6, 0},
76 /* Unconditional branches. */
77 {0xff, -0x100, 2, 3},
78 {0x1fffff, -0x200000, 4, 0},
79};
80
86aba9db
NC
81static int v850_relax = 0;
82
28e4f854 83/* Fixups. */
252b5132 84#define MAX_INSN_FIXUPS (5)
19d63e5d 85struct v850_fixup {
28e4f854
KH
86 expressionS exp;
87 int opindex;
252b5132
RH
88 bfd_reloc_code_real_type reloc;
89};
90
28e4f854 91struct v850_fixup fixups[MAX_INSN_FIXUPS];
252b5132 92static int fc;
252b5132 93
9e0665bc 94struct v850_seg_entry
252b5132 95{
9e0665bc
AM
96 segT s;
97 const char *name;
98 flagword flags;
99};
252b5132 100
9e0665bc 101struct v850_seg_entry v850_seg_table[] =
252b5132 102{
9e0665bc
AM
103 { NULL, ".sdata",
104 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
105 | SEC_SMALL_DATA },
106 { NULL, ".tdata",
107 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
108 { NULL, ".zdata",
109 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
110 { NULL, ".sbss",
111 SEC_ALLOC | SEC_SMALL_DATA },
112 { NULL, ".tbss",
113 SEC_ALLOC },
114 { NULL, ".zbss",
115 SEC_ALLOC},
116 { NULL, ".rosdata",
117 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_DATA
118 | SEC_HAS_CONTENTS | SEC_SMALL_DATA },
119 { NULL, ".rozdata",
120 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_DATA
121 | SEC_HAS_CONTENTS },
122 { NULL, ".scommon",
123 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
124 | SEC_SMALL_DATA | SEC_IS_COMMON },
125 { NULL, ".tcommon",
126 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
127 | SEC_IS_COMMON },
128 { NULL, ".zcommon",
129 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
130 | SEC_IS_COMMON },
131 { NULL, ".call_table_data",
132 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
133 { NULL, ".call_table_text",
134 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_CODE
135 | SEC_HAS_CONTENTS},
136 { NULL, ".bss",
137 SEC_ALLOC }
138};
28e4f854 139
9e0665bc
AM
140#define SDATA_SECTION 0
141#define TDATA_SECTION 1
142#define ZDATA_SECTION 2
143#define SBSS_SECTION 3
144#define TBSS_SECTION 4
145#define ZBSS_SECTION 5
146#define ROSDATA_SECTION 6
147#define ROZDATA_SECTION 7
148#define SCOMMON_SECTION 8
149#define TCOMMON_SECTION 9
150#define ZCOMMON_SECTION 10
151#define CALL_TABLE_DATA_SECTION 11
152#define CALL_TABLE_TEXT_SECTION 12
153#define BSS_SECTION 13
154
155static void do_v850_seg PARAMS ((int, subsegT));
252b5132 156
9e0665bc
AM
157static void
158do_v850_seg (i, sub)
159 int i;
160 subsegT sub;
252b5132 161{
9e0665bc 162 struct v850_seg_entry *seg = v850_seg_table + i;
28e4f854 163
28e4f854 164 obj_elf_section_change_hook ();
9e0665bc
AM
165 if (seg->s != NULL)
166 {
167 subseg_set (seg->s, sub);
168 }
169 else
170 {
171 seg->s = subseg_new (seg->name, sub);
172 bfd_set_section_flags (stdoutput, seg->s, seg->flags);
173 if ((seg->flags & SEC_LOAD) == 0)
174 seg_info (seg->s)->bss = 1;
175 }
252b5132
RH
176}
177
9e0665bc 178static void v850_seg PARAMS ((int i));
28e4f854 179
9e0665bc
AM
180static void
181v850_seg (i)
182 int i;
252b5132 183{
9e0665bc 184 subsegT sub = get_absolute_expression ();
28e4f854 185
9e0665bc 186 do_v850_seg (i, sub);
252b5132
RH
187 demand_empty_rest_of_line ();
188}
189
9e0665bc 190static void v850_offset PARAMS ((int));
28e4f854 191
9e0665bc
AM
192static void
193v850_offset (ignore)
194 int ignore ATTRIBUTE_UNUSED;
252b5132 195{
825487fa 196 char *pfrag;
252b5132 197 int temp = get_absolute_expression ();
825487fa
JW
198
199 pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, (symbolS *)0,
200 (offsetT) temp, (char *) 0);
201 *pfrag = 0;
28e4f854 202
252b5132
RH
203 demand_empty_rest_of_line ();
204}
205
28e4f854
KH
206/* Copied from obj_elf_common() in gas/config/obj-elf.c. */
207
9e0665bc
AM
208static void v850_comm PARAMS ((int));
209
252b5132
RH
210static void
211v850_comm (area)
212 int area;
213{
28e4f854
KH
214 char *name;
215 char c;
216 char *p;
217 int temp;
218 unsigned int size;
219 symbolS *symbolP;
220 int have_align;
252b5132
RH
221
222 name = input_line_pointer;
223 c = get_symbol_end ();
28e4f854
KH
224
225 /* Just after name is now '\0'. */
252b5132
RH
226 p = input_line_pointer;
227 *p = c;
28e4f854 228
252b5132 229 SKIP_WHITESPACE ();
28e4f854 230
252b5132
RH
231 if (*input_line_pointer != ',')
232 {
233 as_bad (_("Expected comma after symbol-name"));
234 ignore_rest_of_line ();
235 return;
236 }
28e4f854
KH
237
238 /* Skip ','. */
239 input_line_pointer++;
240
252b5132
RH
241 if ((temp = get_absolute_expression ()) < 0)
242 {
28e4f854 243 /* xgettext:c-format */
252b5132
RH
244 as_bad (_(".COMMon length (%d.) < 0! Ignored."), temp);
245 ignore_rest_of_line ();
246 return;
247 }
28e4f854 248
252b5132
RH
249 size = temp;
250 *p = 0;
251 symbolP = symbol_find_or_make (name);
252 *p = c;
28e4f854 253
252b5132
RH
254 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
255 {
256 as_bad (_("Ignoring attempt to re-define symbol"));
257 ignore_rest_of_line ();
258 return;
259 }
28e4f854 260
252b5132
RH
261 if (S_GET_VALUE (symbolP) != 0)
262 {
263 if (S_GET_VALUE (symbolP) != size)
264 {
28e4f854 265 /* xgettext:c-format */
252b5132
RH
266 as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %d."),
267 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
268 }
269 }
28e4f854
KH
270
271 know (symbol_get_frag (symbolP) == &zero_address_frag);
272
252b5132
RH
273 if (*input_line_pointer != ',')
274 have_align = 0;
275 else
276 {
277 have_align = 1;
278 input_line_pointer++;
279 SKIP_WHITESPACE ();
280 }
28e4f854 281
252b5132
RH
282 if (! have_align || *input_line_pointer != '"')
283 {
284 if (! have_align)
285 temp = 0;
286 else
287 {
288 temp = get_absolute_expression ();
28e4f854 289
252b5132
RH
290 if (temp < 0)
291 {
292 temp = 0;
293 as_warn (_("Common alignment negative; 0 assumed"));
294 }
295 }
28e4f854 296
cac58fa6 297 if (symbol_get_obj (symbolP)->local)
252b5132 298 {
28e4f854
KH
299 segT old_sec;
300 int old_subsec;
301 char *pfrag;
302 int align;
303 flagword applicable;
252b5132
RH
304
305 old_sec = now_seg;
306 old_subsec = now_subseg;
28e4f854 307
252b5132 308 applicable = bfd_applicable_section_flags (stdoutput);
28e4f854 309
252b5132 310 applicable &= SEC_ALLOC;
28e4f854 311
252b5132
RH
312 switch (area)
313 {
9e0665bc
AM
314 case SCOMMON_SECTION:
315 do_v850_seg (SBSS_SECTION, 0);
252b5132 316 break;
28e4f854 317
9e0665bc
AM
318 case ZCOMMON_SECTION:
319 do_v850_seg (ZBSS_SECTION, 0);
252b5132 320 break;
28e4f854 321
9e0665bc
AM
322 case TCOMMON_SECTION:
323 do_v850_seg (TBSS_SECTION, 0);
252b5132
RH
324 break;
325 }
326
327 if (temp)
328 {
28e4f854 329 /* Convert to a power of 2 alignment. */
252b5132
RH
330 for (align = 0; (temp & 1) == 0; temp >>= 1, ++align)
331 ;
28e4f854 332
252b5132
RH
333 if (temp != 1)
334 {
335 as_bad (_("Common alignment not a power of 2"));
336 ignore_rest_of_line ();
337 return;
338 }
339 }
340 else
341 align = 0;
28e4f854 342
9e0665bc 343 record_alignment (now_seg, align);
28e4f854 344
252b5132
RH
345 if (align)
346 frag_align (align, 0, 0);
347
348 switch (area)
349 {
9e0665bc
AM
350 case SCOMMON_SECTION:
351 if (S_GET_SEGMENT (symbolP) == v850_seg_table[SBSS_SECTION].s)
7dcc9865 352 symbol_get_frag (symbolP)->fr_symbol = 0;
252b5132
RH
353 break;
354
9e0665bc
AM
355 case ZCOMMON_SECTION:
356 if (S_GET_SEGMENT (symbolP) == v850_seg_table[ZBSS_SECTION].s)
7dcc9865 357 symbol_get_frag (symbolP)->fr_symbol = 0;
252b5132
RH
358 break;
359
9e0665bc
AM
360 case TCOMMON_SECTION:
361 if (S_GET_SEGMENT (symbolP) == v850_seg_table[TBSS_SECTION].s)
7dcc9865 362 symbol_get_frag (symbolP)->fr_symbol = 0;
252b5132
RH
363 break;
364
365 default:
cac58fa6 366 abort ();
252b5132 367 }
28e4f854 368
7dcc9865 369 symbol_set_frag (symbolP, frag_now);
252b5132
RH
370 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
371 (offsetT) size, (char *) 0);
372 *pfrag = 0;
373 S_SET_SIZE (symbolP, size);
28e4f854 374
252b5132
RH
375 switch (area)
376 {
9e0665bc
AM
377 case SCOMMON_SECTION:
378 S_SET_SEGMENT (symbolP, v850_seg_table[SBSS_SECTION].s);
252b5132 379 break;
28e4f854 380
9e0665bc
AM
381 case ZCOMMON_SECTION:
382 S_SET_SEGMENT (symbolP, v850_seg_table[ZBSS_SECTION].s);
252b5132 383 break;
28e4f854 384
9e0665bc
AM
385 case TCOMMON_SECTION:
386 S_SET_SEGMENT (symbolP, v850_seg_table[TBSS_SECTION].s);
252b5132 387 break;
28e4f854 388
252b5132 389 default:
28e4f854 390 abort ();
252b5132 391 }
28e4f854 392
252b5132 393 S_CLEAR_EXTERNAL (symbolP);
28e4f854 394 obj_elf_section_change_hook ();
252b5132
RH
395 subseg_set (old_sec, old_subsec);
396 }
397 else
398 {
86aba9db
NC
399 segT old_sec;
400 int old_subsec;
401
252b5132 402 allocate_common:
86aba9db
NC
403 old_sec = now_seg;
404 old_subsec = now_subseg;
405
252b5132
RH
406 S_SET_VALUE (symbolP, (valueT) size);
407 S_SET_ALIGN (symbolP, temp);
408 S_SET_EXTERNAL (symbolP);
28e4f854 409
252b5132
RH
410 switch (area)
411 {
9e0665bc
AM
412 case SCOMMON_SECTION:
413 case ZCOMMON_SECTION:
414 case TCOMMON_SECTION:
415 do_v850_seg (area, 0);
416 S_SET_SEGMENT (symbolP, v850_seg_table[area].s);
252b5132 417 break;
28e4f854 418
252b5132 419 default:
28e4f854 420 abort ();
252b5132 421 }
86aba9db
NC
422
423 obj_elf_section_change_hook ();
424 subseg_set (old_sec, old_subsec);
252b5132
RH
425 }
426 }
427 else
428 {
429 input_line_pointer++;
28e4f854 430
252b5132
RH
431 /* @@ Some use the dot, some don't. Can we get some consistency?? */
432 if (*input_line_pointer == '.')
433 input_line_pointer++;
28e4f854 434
252b5132
RH
435 /* @@ Some say data, some say bss. */
436 if (strncmp (input_line_pointer, "bss\"", 4)
437 && strncmp (input_line_pointer, "data\"", 5))
438 {
439 while (*--input_line_pointer != '"')
440 ;
441 input_line_pointer--;
442 goto bad_common_segment;
443 }
444 while (*input_line_pointer++ != '"')
445 ;
446 goto allocate_common;
447 }
448
fed9b18a 449 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
252b5132
RH
450
451 demand_empty_rest_of_line ();
452 return;
453
454 {
455 bad_common_segment:
456 p = input_line_pointer;
457 while (*p && *p != '\n')
458 p++;
459 c = *p;
460 *p = '\0';
461 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
462 *p = c;
463 input_line_pointer = p;
464 ignore_rest_of_line ();
465 return;
466 }
467}
468
9e0665bc
AM
469static void set_machine PARAMS ((int));
470
471static void
472set_machine (number)
473 int number;
252b5132
RH
474{
475 machine = number;
476 bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
477
478 switch (machine)
479 {
28e4f854
KH
480 case 0: processor_mask = PROCESSOR_V850; break;
481 case bfd_mach_v850e: processor_mask = PROCESSOR_V850E; break;
252b5132
RH
482 }
483}
484
86aba9db
NC
485static void v850_longcode PARAMS ((int));
486
487static void
488v850_longcode (type)
489 int type;
490{
491 expressionS ex;
492
493 if (! v850_relax)
494 {
495 if (type == 1)
496 as_warn (".longcall pseudo-op seen when not relaxing");
497 else
498 as_warn (".longjump pseudo-op seen when not relaxing");
499 }
500
501 expression (&ex);
502
503 if (ex.X_op != O_symbol || ex.X_add_number != 0)
504 {
505 as_bad ("bad .longcall format");
506 ignore_rest_of_line ();
507
508 return;
509 }
510
511 if (type == 1)
512 fix_new_exp (frag_now, frag_now_fix (), 4, & ex, 1,
513 BFD_RELOC_V850_LONGCALL);
514 else
515 fix_new_exp (frag_now, frag_now_fix (), 4, & ex, 1,
516 BFD_RELOC_V850_LONGJUMP);
517
518 demand_empty_rest_of_line ();
519}
520
252b5132 521/* The target specific pseudo-ops which we support. */
77c6dd37
NC
522const pseudo_typeS md_pseudo_table[] =
523{
9e0665bc
AM
524 { "sdata", v850_seg, SDATA_SECTION },
525 { "tdata", v850_seg, TDATA_SECTION },
526 { "zdata", v850_seg, ZDATA_SECTION },
527 { "sbss", v850_seg, SBSS_SECTION },
528 { "tbss", v850_seg, TBSS_SECTION },
529 { "zbss", v850_seg, ZBSS_SECTION },
530 { "rosdata", v850_seg, ROSDATA_SECTION },
531 { "rozdata", v850_seg, ROZDATA_SECTION },
532 { "bss", v850_seg, BSS_SECTION },
533 { "offset", v850_offset, 0 },
534 { "word", cons, 4 },
535 { "zcomm", v850_comm, ZCOMMON_SECTION },
536 { "scomm", v850_comm, SCOMMON_SECTION },
537 { "tcomm", v850_comm, TCOMMON_SECTION },
538 { "v850", set_machine, 0 },
539 { "call_table_data", v850_seg, CALL_TABLE_DATA_SECTION },
540 { "call_table_text", v850_seg, CALL_TABLE_TEXT_SECTION },
541 { "v850e", set_machine, bfd_mach_v850e },
9f1838ed 542 { "file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0 },
9e0665bc 543 { "loc", dwarf2_directive_loc, 0 },
86aba9db
NC
544 { "longcall", v850_longcode, 1 },
545 { "longjump", v850_longcode, 2 },
9e0665bc 546 { NULL, NULL, 0 }
252b5132
RH
547};
548
549/* Opcode hash table. */
550static struct hash_control *v850_hash;
551
28e4f854 552/* This table is sorted. Suitable for searching by a binary search. */
77c6dd37
NC
553static const struct reg_name pre_defined_registers[] =
554{
252b5132 555 { "ep", 30 }, /* ep - element ptr */
28e4f854
KH
556 { "gp", 4 }, /* gp - global ptr */
557 { "hp", 2 }, /* hp - handler stack ptr */
558 { "lp", 31 }, /* lp - link ptr */
252b5132
RH
559 { "r0", 0 },
560 { "r1", 1 },
561 { "r10", 10 },
562 { "r11", 11 },
563 { "r12", 12 },
564 { "r13", 13 },
565 { "r14", 14 },
566 { "r15", 15 },
567 { "r16", 16 },
568 { "r17", 17 },
569 { "r18", 18 },
570 { "r19", 19 },
571 { "r2", 2 },
572 { "r20", 20 },
573 { "r21", 21 },
574 { "r22", 22 },
575 { "r23", 23 },
576 { "r24", 24 },
577 { "r25", 25 },
578 { "r26", 26 },
579 { "r27", 27 },
580 { "r28", 28 },
581 { "r29", 29 },
582 { "r3", 3 },
583 { "r30", 30 },
584 { "r31", 31 },
585 { "r4", 4 },
586 { "r5", 5 },
587 { "r6", 6 },
588 { "r7", 7 },
589 { "r8", 8 },
590 { "r9", 9 },
28e4f854
KH
591 { "sp", 3 }, /* sp - stack ptr */
592 { "tp", 5 }, /* tp - text ptr */
252b5132
RH
593 { "zero", 0 },
594};
252b5132 595
28e4f854
KH
596#define REG_NAME_CNT \
597 (sizeof (pre_defined_registers) / sizeof (struct reg_name))
252b5132 598
77c6dd37
NC
599static const struct reg_name system_registers[] =
600{
601 { "asid", 23 },
602 { "bpc", 22 },
603 { "bpav", 24 },
604 { "bpam", 25 },
605 { "bpdv", 26 },
606 { "bpdm", 27 },
252b5132
RH
607 { "ctbp", 20 },
608 { "ctpc", 16 },
609 { "ctpsw", 17 },
610 { "dbpc", 18 },
611 { "dbpsw", 19 },
77c6dd37 612 { "dir", 21 },
252b5132
RH
613 { "ecr", 4 },
614 { "eipc", 0 },
615 { "eipsw", 1 },
616 { "fepc", 2 },
617 { "fepsw", 3 },
618 { "psw", 5 },
619};
28e4f854
KH
620
621#define SYSREG_NAME_CNT \
622 (sizeof (system_registers) / sizeof (struct reg_name))
252b5132 623
77c6dd37
NC
624static const struct reg_name system_list_registers[] =
625{
252b5132
RH
626 {"PS", 5 },
627 {"SR", 0 + 1}
628};
28e4f854
KH
629
630#define SYSREGLIST_NAME_CNT \
631 (sizeof (system_list_registers) / sizeof (struct reg_name))
252b5132 632
77c6dd37
NC
633static const struct reg_name cc_names[] =
634{
252b5132
RH
635 { "c", 0x1 },
636 { "e", 0x2 },
637 { "ge", 0xe },
638 { "gt", 0xf },
639 { "h", 0xb },
640 { "l", 0x1 },
641 { "le", 0x7 },
642 { "lt", 0x6 },
643 { "n", 0x4 },
644 { "nc", 0x9 },
645 { "ne", 0xa },
646 { "nh", 0x3 },
647 { "nl", 0x9 },
648 { "ns", 0xc },
649 { "nv", 0x8 },
650 { "nz", 0xa },
651 { "p", 0xc },
652 { "s", 0x4 },
653 { "sa", 0xd },
654 { "t", 0x5 },
655 { "v", 0x0 },
656 { "z", 0x2 },
657};
252b5132 658
28e4f854
KH
659#define CC_NAME_CNT \
660 (sizeof (cc_names) / sizeof (struct reg_name))
661
662/* Do a binary search of the given register table to see if NAME is a
663 valid regiter name. Return the register number from the array on
664 success, or -1 on failure. */
252b5132 665
9e0665bc 666static int reg_name_search
b34976b6 667 PARAMS ((const struct reg_name *, int, const char *, bfd_boolean));
9e0665bc 668
252b5132
RH
669static int
670reg_name_search (regs, regcount, name, accept_numbers)
28e4f854
KH
671 const struct reg_name *regs;
672 int regcount;
673 const char *name;
b34976b6 674 bfd_boolean accept_numbers;
252b5132
RH
675{
676 int middle, low, high;
677 int cmp;
28e4f854 678 symbolS *symbolP;
252b5132
RH
679
680 /* If the register name is a symbol, then evaluate it. */
681 if ((symbolP = symbol_find (name)) != NULL)
682 {
683 /* If the symbol is an alias for another name then use that.
684 If the symbol is an alias for a number, then return the number. */
a77f5182 685 if (symbol_equated_p (symbolP))
252b5132 686 {
28e4f854
KH
687 name
688 = S_GET_NAME (symbol_get_value_expression (symbolP)->X_add_symbol);
252b5132
RH
689 }
690 else if (accept_numbers)
691 {
692 int reg = S_GET_VALUE (symbolP);
28e4f854 693
252b5132
RH
694 if (reg >= 0 && reg <= 31)
695 return reg;
696 }
697
698 /* Otherwise drop through and try parsing name normally. */
699 }
28e4f854 700
252b5132
RH
701 low = 0;
702 high = regcount - 1;
703
704 do
705 {
706 middle = (low + high) / 2;
707 cmp = strcasecmp (name, regs[middle].name);
708 if (cmp < 0)
709 high = middle - 1;
710 else if (cmp > 0)
711 low = middle + 1;
712 else
713 return regs[middle].value;
714 }
715 while (low <= high);
716 return -1;
717}
718
252b5132 719/* Summary of register_name().
77c6dd37
NC
720
721 in: Input_line_pointer points to 1st char of operand.
722
723 out: An expressionS.
724 The operand may have been a register: in this case, X_op == O_register,
725 X_add_number is set to the register number, and truth is returned.
726 Input_line_pointer->(next non-blank) char after operand, or is in
727 its original state. */
28e4f854 728
b34976b6 729static bfd_boolean register_name PARAMS ((expressionS *));
9e0665bc 730
b34976b6 731static bfd_boolean
252b5132 732register_name (expressionP)
28e4f854 733 expressionS *expressionP;
252b5132 734{
28e4f854
KH
735 int reg_number;
736 char *name;
737 char *start;
738 char c;
252b5132 739
28e4f854 740 /* Find the spelling of the operand. */
252b5132
RH
741 start = name = input_line_pointer;
742
743 c = get_symbol_end ();
744
745 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT,
746 name, FALSE);
747
28e4f854
KH
748 /* Put back the delimiting char. */
749 *input_line_pointer = c;
750
751 /* Look to see if it's in the register table. */
752 if (reg_number >= 0)
252b5132
RH
753 {
754 expressionP->X_op = O_register;
755 expressionP->X_add_number = reg_number;
756
28e4f854 757 /* Make the rest nice. */
252b5132
RH
758 expressionP->X_add_symbol = NULL;
759 expressionP->X_op_symbol = NULL;
28e4f854 760
b34976b6 761 return TRUE;
252b5132
RH
762 }
763 else
764 {
28e4f854 765 /* Reset the line as if we had not done anything. */
252b5132 766 input_line_pointer = start;
28e4f854 767
b34976b6 768 return FALSE;
252b5132
RH
769 }
770}
771
772/* Summary of system_register_name().
77c6dd37
NC
773
774 in: INPUT_LINE_POINTER points to 1st char of operand.
775 EXPRESSIONP points to an expression structure to be filled in.
776 ACCEPT_NUMBERS is true iff numerical register names may be used.
777 ACCEPT_LIST_NAMES is true iff the special names PS and SR may be
778 accepted.
779
780 out: An expressionS structure in expressionP.
781 The operand may have been a register: in this case, X_op == O_register,
782 X_add_number is set to the register number, and truth is returned.
783 Input_line_pointer->(next non-blank) char after operand, or is in
784 its original state. */
28e4f854 785
b34976b6
AM
786static bfd_boolean system_register_name
787 PARAMS ((expressionS *, bfd_boolean, bfd_boolean));
9e0665bc 788
b34976b6 789static bfd_boolean
252b5132 790system_register_name (expressionP, accept_numbers, accept_list_names)
28e4f854 791 expressionS *expressionP;
b34976b6
AM
792 bfd_boolean accept_numbers;
793 bfd_boolean accept_list_names;
252b5132 794{
28e4f854
KH
795 int reg_number;
796 char *name;
797 char *start;
798 char c;
252b5132 799
28e4f854 800 /* Find the spelling of the operand. */
252b5132
RH
801 start = name = input_line_pointer;
802
803 c = get_symbol_end ();
804 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name,
805 accept_numbers);
806
28e4f854
KH
807 /* Put back the delimiting char. */
808 *input_line_pointer = c;
809
252b5132
RH
810 if (reg_number < 0
811 && accept_numbers)
812 {
28e4f854
KH
813 /* Reset input_line pointer. */
814 input_line_pointer = start;
252b5132 815
3882b010 816 if (ISDIGIT (*input_line_pointer))
252b5132 817 {
28e4f854 818 reg_number = strtol (input_line_pointer, &input_line_pointer, 10);
252b5132 819
28e4f854
KH
820 /* Make sure that the register number is allowable. */
821 if (reg_number < 0
5480ccf3 822 || (reg_number > 5 && reg_number < 16)
77c6dd37 823 || reg_number > 27)
252b5132
RH
824 {
825 reg_number = -1;
826 }
827 }
828 else if (accept_list_names)
829 {
830 c = get_symbol_end ();
831 reg_number = reg_name_search (system_list_registers,
832 SYSREGLIST_NAME_CNT, name, FALSE);
833
28e4f854
KH
834 /* Put back the delimiting char. */
835 *input_line_pointer = c;
252b5132
RH
836 }
837 }
28e4f854
KH
838
839 /* Look to see if it's in the register table. */
840 if (reg_number >= 0)
252b5132
RH
841 {
842 expressionP->X_op = O_register;
843 expressionP->X_add_number = reg_number;
844
28e4f854 845 /* Make the rest nice. */
252b5132
RH
846 expressionP->X_add_symbol = NULL;
847 expressionP->X_op_symbol = NULL;
848
b34976b6 849 return TRUE;
252b5132
RH
850 }
851 else
852 {
28e4f854 853 /* Reset the line as if we had not done anything. */
252b5132 854 input_line_pointer = start;
28e4f854 855
b34976b6 856 return FALSE;
252b5132
RH
857 }
858}
859
860/* Summary of cc_name().
77c6dd37
NC
861
862 in: INPUT_LINE_POINTER points to 1st char of operand.
863
864 out: An expressionS.
865 The operand may have been a register: in this case, X_op == O_register,
866 X_add_number is set to the register number, and truth is returned.
867 Input_line_pointer->(next non-blank) char after operand, or is in
868 its original state. */
28e4f854 869
b34976b6 870static bfd_boolean cc_name PARAMS ((expressionS *));
9e0665bc 871
b34976b6 872static bfd_boolean
252b5132 873cc_name (expressionP)
28e4f854 874 expressionS *expressionP;
252b5132 875{
28e4f854
KH
876 int reg_number;
877 char *name;
878 char *start;
879 char c;
252b5132 880
28e4f854 881 /* Find the spelling of the operand. */
252b5132
RH
882 start = name = input_line_pointer;
883
884 c = get_symbol_end ();
885 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name, FALSE);
886
28e4f854
KH
887 /* Put back the delimiting char. */
888 *input_line_pointer = c;
889
890 /* Look to see if it's in the register table. */
891 if (reg_number >= 0)
252b5132
RH
892 {
893 expressionP->X_op = O_constant;
894 expressionP->X_add_number = reg_number;
895
28e4f854 896 /* Make the rest nice. */
252b5132
RH
897 expressionP->X_add_symbol = NULL;
898 expressionP->X_op_symbol = NULL;
899
b34976b6 900 return TRUE;
252b5132
RH
901 }
902 else
903 {
28e4f854 904 /* Reset the line as if we had not done anything. */
252b5132 905 input_line_pointer = start;
28e4f854 906
b34976b6 907 return FALSE;
252b5132
RH
908 }
909}
910
9e0665bc
AM
911static void skip_white_space PARAMS ((void));
912
252b5132 913static void
9e0665bc 914skip_white_space ()
252b5132 915{
28e4f854
KH
916 while (*input_line_pointer == ' '
917 || *input_line_pointer == '\t')
918 ++input_line_pointer;
252b5132
RH
919}
920
921/* Summary of parse_register_list ().
77c6dd37
NC
922
923 in: INPUT_LINE_POINTER points to 1st char of a list of registers.
924 INSN is the partially constructed instruction.
925 OPERAND is the operand being inserted.
926
927 out: NULL if the parse completed successfully, otherwise a
928 pointer to an error message is returned. If the parse
929 completes the correct bit fields in the instruction
930 will be filled in.
931
932 Parses register lists with the syntax:
933
934 { rX }
935 { rX, rY }
936 { rX - rY }
937 { rX - rY, rZ }
938 etc
939
940 and also parses constant epxressions whoes bits indicate the
941 registers in the lists. The LSB in the expression refers to
942 the lowest numbered permissable register in the register list,
943 and so on upwards. System registers are considered to be very
944 high numbers. */
28e4f854 945
9e0665bc
AM
946static char *parse_register_list
947 PARAMS ((unsigned long *, const struct v850_operand *));
948
252b5132 949static char *
28e4f854
KH
950parse_register_list (insn, operand)
951 unsigned long *insn;
952 const struct v850_operand *operand;
252b5132 953{
28e4f854
KH
954 static int type1_regs[32] = {
955 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
956 0, 0, 0, 0, 0, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24
957 };
958 static int type2_regs[32] = {
959 19, 18, 17, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
960 0, 0, 0, 0, 30, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24
961 };
962 static int type3_regs[32] = {
963 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
964 0, 0, 0, 0, 14, 15, 13, 12, 7, 6, 5, 4, 11, 10, 9, 8
965 };
966 int *regs;
252b5132
RH
967 expressionS exp;
968
28e4f854 969 /* Select a register array to parse. */
252b5132
RH
970 switch (operand->shift)
971 {
972 case 0xffe00001: regs = type1_regs; break;
973 case 0xfff8000f: regs = type2_regs; break;
974 case 0xfff8001f: regs = type3_regs; break;
975 default:
976 as_bad (_("unknown operand shift: %x\n"), operand->shift);
977 return _("internal failure in parse_register_list");
978 }
979
980 skip_white_space ();
981
982 /* If the expression starts with a curly brace it is a register list.
983 Otherwise it is a constant expression, whoes bits indicate which
984 registers are to be included in the list. */
28e4f854 985 if (*input_line_pointer != '{')
252b5132 986 {
252b5132
RH
987 int reg;
988 int i;
28e4f854
KH
989
990 expression (&exp);
991
252b5132
RH
992 if (exp.X_op != O_constant)
993 return _("constant expression or register list expected");
994
995 if (regs == type1_regs)
996 {
997 if (exp.X_add_number & 0xFFFFF000)
998 return _("high bits set in register list expression");
28e4f854
KH
999
1000 for (reg = 20; reg < 32; reg++)
252b5132
RH
1001 if (exp.X_add_number & (1 << (reg - 20)))
1002 {
1003 for (i = 0; i < 32; i++)
1004 if (regs[i] == reg)
28e4f854 1005 *insn |= (1 << i);
252b5132
RH
1006 }
1007 }
1008 else if (regs == type2_regs)
1009 {
1010 if (exp.X_add_number & 0xFFFE0000)
1011 return _("high bits set in register list expression");
28e4f854
KH
1012
1013 for (reg = 1; reg < 16; reg++)
252b5132
RH
1014 if (exp.X_add_number & (1 << (reg - 1)))
1015 {
1016 for (i = 0; i < 32; i++)
1017 if (regs[i] == reg)
28e4f854 1018 *insn |= (1 << i);
252b5132
RH
1019 }
1020
1021 if (exp.X_add_number & (1 << 15))
28e4f854
KH
1022 *insn |= (1 << 3);
1023
252b5132 1024 if (exp.X_add_number & (1 << 16))
28e4f854 1025 *insn |= (1 << 19);
252b5132 1026 }
28e4f854 1027 else /* regs == type3_regs */
252b5132
RH
1028 {
1029 if (exp.X_add_number & 0xFFFE0000)
1030 return _("high bits set in register list expression");
28e4f854
KH
1031
1032 for (reg = 16; reg < 32; reg++)
252b5132
RH
1033 if (exp.X_add_number & (1 << (reg - 16)))
1034 {
1035 for (i = 0; i < 32; i++)
1036 if (regs[i] == reg)
28e4f854 1037 *insn |= (1 << i);
252b5132
RH
1038 }
1039
1040 if (exp.X_add_number & (1 << 16))
28e4f854 1041 *insn |= (1 << 19);
252b5132
RH
1042 }
1043
1044 return NULL;
1045 }
1046
28e4f854 1047 input_line_pointer++;
252b5132
RH
1048
1049 /* Parse the register list until a terminator (closing curly brace or
1050 new-line) is found. */
1051 for (;;)
1052 {
28e4f854 1053 if (register_name (&exp))
252b5132 1054 {
28e4f854
KH
1055 int i;
1056
252b5132
RH
1057 /* Locate the given register in the list, and if it is there,
1058 insert the corresponding bit into the instruction. */
1059 for (i = 0; i < 32; i++)
1060 {
28e4f854 1061 if (regs[i] == exp.X_add_number)
252b5132 1062 {
28e4f854 1063 *insn |= (1 << i);
252b5132
RH
1064 break;
1065 }
1066 }
1067
1068 if (i == 32)
77c6dd37 1069 return _("illegal register included in list");
252b5132 1070 }
b34976b6 1071 else if (system_register_name (&exp, TRUE, TRUE))
252b5132
RH
1072 {
1073 if (regs == type1_regs)
1074 {
1075 return _("system registers cannot be included in list");
1076 }
1077 else if (exp.X_add_number == 5)
1078 {
1079 if (regs == type2_regs)
1080 return _("PSW cannot be included in list");
1081 else
28e4f854 1082 *insn |= 0x8;
252b5132
RH
1083 }
1084 else if (exp.X_add_number < 4)
28e4f854 1085 *insn |= 0x80000;
252b5132
RH
1086 else
1087 return _("High value system registers cannot be included in list");
1088 }
28e4f854 1089 else if (*input_line_pointer == '}')
252b5132 1090 {
28e4f854 1091 input_line_pointer++;
252b5132
RH
1092 break;
1093 }
28e4f854 1094 else if (*input_line_pointer == ',')
252b5132 1095 {
28e4f854 1096 input_line_pointer++;
252b5132
RH
1097 continue;
1098 }
28e4f854 1099 else if (*input_line_pointer == '-')
252b5132 1100 {
28e4f854
KH
1101 /* We have encountered a range of registers: rX - rY. */
1102 int j;
252b5132
RH
1103 expressionS exp2;
1104
1105 /* Skip the dash. */
28e4f854 1106 ++input_line_pointer;
252b5132
RH
1107
1108 /* Get the second register in the range. */
28e4f854 1109 if (! register_name (&exp2))
252b5132
RH
1110 {
1111 return _("second register should follow dash in register list");
1112 exp2.X_add_number = exp.X_add_number;
1113 }
1114
1115 /* Add the rest of the registers in the range. */
1116 for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
1117 {
28e4f854
KH
1118 int i;
1119
252b5132
RH
1120 /* Locate the given register in the list, and if it is there,
1121 insert the corresponding bit into the instruction. */
1122 for (i = 0; i < 32; i++)
1123 {
28e4f854 1124 if (regs[i] == j)
252b5132 1125 {
28e4f854 1126 *insn |= (1 << i);
252b5132
RH
1127 break;
1128 }
1129 }
1130
1131 if (i == 32)
1132 return _("illegal register included in list");
1133 }
1134 }
1135 else
77c6dd37 1136 break;
252b5132
RH
1137
1138 skip_white_space ();
1139 }
1140
1141 return NULL;
1142}
1143
5a38dc70 1144const char *md_shortopts = "m:";
252b5132 1145
19d63e5d 1146struct option md_longopts[] = {
252b5132
RH
1147 {NULL, no_argument, NULL, 0}
1148};
252b5132 1149
28e4f854 1150size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
1151
1152void
1153md_show_usage (stream)
28e4f854 1154 FILE *stream;
252b5132
RH
1155{
1156 fprintf (stream, _(" V850 options:\n"));
1157 fprintf (stream, _(" -mwarn-signed-overflow Warn if signed immediate values overflow\n"));
1158 fprintf (stream, _(" -mwarn-unsigned-overflow Warn if unsigned immediate values overflow\n"));
1159 fprintf (stream, _(" -mv850 The code is targeted at the v850\n"));
1160 fprintf (stream, _(" -mv850e The code is targeted at the v850e\n"));
252b5132 1161 fprintf (stream, _(" -mv850any The code is generic, despite any processor specific instructions\n"));
86aba9db
NC
1162 fprintf (stream, _(" -mrelax Enable relaxation\n"));
1163
252b5132
RH
1164}
1165
1166int
1167md_parse_option (c, arg)
28e4f854
KH
1168 int c;
1169 char *arg;
252b5132
RH
1170{
1171 if (c != 'm')
1172 {
5480ccf3 1173 if (c != 'a')
28e4f854 1174 /* xgettext:c-format */
5480ccf3 1175 fprintf (stderr, _("unknown command line option: -%c%s\n"), c, arg);
252b5132
RH
1176 return 0;
1177 }
1178
1179 if (strcmp (arg, "warn-signed-overflow") == 0)
1180 {
1181 warn_signed_overflows = TRUE;
1182 }
1183 else if (strcmp (arg, "warn-unsigned-overflow") == 0)
1184 {
1185 warn_unsigned_overflows = TRUE;
1186 }
1187 else if (strcmp (arg, "v850") == 0)
1188 {
1189 machine = 0;
1190 processor_mask = PROCESSOR_V850;
1191 }
1192 else if (strcmp (arg, "v850e") == 0)
1193 {
1194 machine = bfd_mach_v850e;
1195 processor_mask = PROCESSOR_V850E;
1196 }
252b5132
RH
1197 else if (strcmp (arg, "v850any") == 0)
1198 {
28e4f854
KH
1199 /* Tell the world that this is for any v850 chip. */
1200 machine = 0;
1201
1202 /* But support instructions for the extended versions. */
86aba9db 1203 processor_mask = PROCESSOR_V850E;
252b5132 1204 }
86aba9db
NC
1205 else if (strcmp (arg, "relax") == 0)
1206 v850_relax = 1;
252b5132
RH
1207 else
1208 {
28e4f854 1209 /* xgettext:c-format */
252b5132
RH
1210 fprintf (stderr, _("unknown command line option: -%c%s\n"), c, arg);
1211 return 0;
1212 }
28e4f854 1213
252b5132
RH
1214 return 1;
1215}
1216
1217symbolS *
1218md_undefined_symbol (name)
28e4f854 1219 char *name ATTRIBUTE_UNUSED;
252b5132
RH
1220{
1221 return 0;
1222}
1223
1224char *
1225md_atof (type, litp, sizep)
28e4f854
KH
1226 int type;
1227 char *litp;
1228 int *sizep;
252b5132 1229{
28e4f854 1230 int prec;
252b5132 1231 LITTLENUM_TYPE words[4];
28e4f854
KH
1232 char *t;
1233 int i;
252b5132
RH
1234
1235 switch (type)
1236 {
1237 case 'f':
1238 prec = 2;
1239 break;
1240
1241 case 'd':
1242 prec = 4;
1243 break;
1244
1245 default:
1246 *sizep = 0;
1247 return _("bad call to md_atof");
1248 }
28e4f854 1249
252b5132
RH
1250 t = atof_ieee (input_line_pointer, type, words);
1251 if (t)
1252 input_line_pointer = t;
1253
1254 *sizep = prec * 2;
1255
1256 for (i = prec - 1; i >= 0; i--)
1257 {
1258 md_number_to_chars (litp, (valueT) words[i], 2);
1259 litp += 2;
1260 }
1261
1262 return NULL;
1263}
1264
252b5132 1265/* Very gross. */
28e4f854 1266
252b5132
RH
1267void
1268md_convert_frag (abfd, sec, fragP)
19d63e5d
KH
1269 bfd *abfd ATTRIBUTE_UNUSED;
1270 asection *sec;
1271 fragS *fragP;
252b5132
RH
1272{
1273 subseg_change (sec, 0);
28e4f854 1274
252b5132
RH
1275 /* In range conditional or unconditional branch. */
1276 if (fragP->fr_subtype == 0 || fragP->fr_subtype == 2)
1277 {
1278 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
1279 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
252b5132
RH
1280 fragP->fr_fix += 2;
1281 }
1282 /* Out of range conditional branch. Emit a branch around a jump. */
1283 else if (fragP->fr_subtype == 1)
1284 {
28e4f854 1285 unsigned char *buffer =
252b5132
RH
1286 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1287
1288 /* Reverse the condition of the first branch. */
1289 buffer[0] ^= 0x08;
1290 /* Mask off all the displacement bits. */
1291 buffer[0] &= 0x8f;
1292 buffer[1] &= 0x07;
1293 /* Now set the displacement bits so that we branch
1294 around the unconditional branch. */
1295 buffer[0] |= 0x30;
1296
1297 /* Now create the unconditional branch + fixup to the final
1298 target. */
1299 md_number_to_chars (buffer + 2, 0x00000780, 4);
1300 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
1301 fragP->fr_offset, 1, BFD_RELOC_UNUSED +
1302 (int) fragP->fr_opcode + 1);
252b5132
RH
1303 fragP->fr_fix += 6;
1304 }
1305 /* Out of range unconditional branch. Emit a jump. */
1306 else if (fragP->fr_subtype == 3)
1307 {
1308 md_number_to_chars (fragP->fr_fix + fragP->fr_literal, 0x00000780, 4);
1309 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
1310 fragP->fr_offset, 1, BFD_RELOC_UNUSED +
1311 (int) fragP->fr_opcode + 1);
252b5132
RH
1312 fragP->fr_fix += 4;
1313 }
1314 else
1315 abort ();
1316}
1317
1318valueT
1319md_section_align (seg, addr)
28e4f854
KH
1320 asection *seg;
1321 valueT addr;
252b5132
RH
1322{
1323 int align = bfd_get_section_alignment (stdoutput, seg);
1324 return ((addr + (1 << align) - 1) & (-1 << align));
1325}
1326
1327void
1328md_begin ()
1329{
28e4f854 1330 char *prev_name = "";
86aba9db 1331 const struct v850_opcode *op;
28e4f854 1332
86aba9db 1333 if (strncmp (TARGET_CPU, "v850e", 5) == 0)
252b5132
RH
1334 {
1335 if (machine == -1)
28e4f854
KH
1336 machine = bfd_mach_v850e;
1337
252b5132
RH
1338 if (processor_mask == -1)
1339 processor_mask = PROCESSOR_V850E;
1340 }
28e4f854 1341 else if (strncmp (TARGET_CPU, "v850", 4) == 0)
252b5132
RH
1342 {
1343 if (machine == -1)
28e4f854
KH
1344 machine = 0;
1345
252b5132
RH
1346 if (processor_mask == -1)
1347 processor_mask = PROCESSOR_V850;
1348 }
1349 else
28e4f854
KH
1350 /* xgettext:c-format */
1351 as_bad (_("Unable to determine default target processor from string: %s"),
252b5132
RH
1352 TARGET_CPU);
1353
19d63e5d 1354 v850_hash = hash_new ();
252b5132
RH
1355
1356 /* Insert unique names into hash table. The V850 instruction set
1357 has many identical opcode names that have different opcodes based
1358 on the operands. This hash table then provides a quick index to
1359 the first opcode with a particular name in the opcode table. */
252b5132
RH
1360 op = v850_opcodes;
1361 while (op->name)
1362 {
28e4f854 1363 if (strcmp (prev_name, op->name))
252b5132
RH
1364 {
1365 prev_name = (char *) op->name;
1366 hash_insert (v850_hash, op->name, (char *) op);
1367 }
1368 op++;
1369 }
1370
9e0665bc 1371 v850_seg_table[BSS_SECTION].s = bss_section;
252b5132 1372 bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
252b5132
RH
1373}
1374
9e0665bc
AM
1375static bfd_reloc_code_real_type handle_ctoff
1376 PARAMS ((const struct v850_operand *));
1377
252b5132 1378static bfd_reloc_code_real_type
9e0665bc
AM
1379handle_ctoff (operand)
1380 const struct v850_operand *operand;
252b5132
RH
1381{
1382 if (operand == NULL)
1383 return BFD_RELOC_V850_CALLT_16_16_OFFSET;
1384
28e4f854 1385 if (operand->bits != 6
252b5132
RH
1386 || operand->shift != 0)
1387 {
1388 as_bad (_("ctoff() relocation used on an instruction which does not support it"));
1389 return BFD_RELOC_64; /* Used to indicate an error condition. */
1390 }
28e4f854 1391
252b5132
RH
1392 return BFD_RELOC_V850_CALLT_6_7_OFFSET;
1393}
1394
9e0665bc
AM
1395static bfd_reloc_code_real_type handle_sdaoff
1396 PARAMS ((const struct v850_operand *));
1397
252b5132 1398static bfd_reloc_code_real_type
9e0665bc
AM
1399handle_sdaoff (operand)
1400 const struct v850_operand *operand;
252b5132 1401{
28e4f854
KH
1402 if (operand == NULL)
1403 return BFD_RELOC_V850_SDA_16_16_OFFSET;
1404
1405 if (operand->bits == 15 && operand->shift == 17)
1406 return BFD_RELOC_V850_SDA_15_16_OFFSET;
1407
1408 if (operand->bits == -1)
1409 return BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET;
1410
1411 if (operand->bits != 16
252b5132
RH
1412 || operand->shift != 16)
1413 {
1414 as_bad (_("sdaoff() relocation used on an instruction which does not support it"));
1415 return BFD_RELOC_64; /* Used to indicate an error condition. */
1416 }
28e4f854 1417
252b5132
RH
1418 return BFD_RELOC_V850_SDA_16_16_OFFSET;
1419}
1420
9e0665bc
AM
1421static bfd_reloc_code_real_type handle_zdaoff
1422 PARAMS ((const struct v850_operand *));
1423
252b5132 1424static bfd_reloc_code_real_type
9e0665bc
AM
1425handle_zdaoff (operand)
1426 const struct v850_operand *operand;
252b5132 1427{
28e4f854
KH
1428 if (operand == NULL)
1429 return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1430
1431 if (operand->bits == 15 && operand->shift == 17)
1432 return BFD_RELOC_V850_ZDA_15_16_OFFSET;
252b5132 1433
28e4f854
KH
1434 if (operand->bits == -1)
1435 return BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET;
1436
1437 if (operand->bits != 16
252b5132
RH
1438 || operand->shift != 16)
1439 {
1440 as_bad (_("zdaoff() relocation used on an instruction which does not support it"));
28e4f854
KH
1441 /* Used to indicate an error condition. */
1442 return BFD_RELOC_64;
252b5132 1443 }
28e4f854 1444
252b5132
RH
1445 return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1446}
1447
9e0665bc
AM
1448static bfd_reloc_code_real_type handle_tdaoff
1449 PARAMS ((const struct v850_operand *));
1450
252b5132 1451static bfd_reloc_code_real_type
9e0665bc
AM
1452handle_tdaoff (operand)
1453 const struct v850_operand *operand;
252b5132 1454{
28e4f854
KH
1455 if (operand == NULL)
1456 /* Data item, not an instruction. */
1457 return BFD_RELOC_V850_TDA_7_7_OFFSET;
1458
1459 if (operand->bits == 6 && operand->shift == 1)
1460 /* sld.w/sst.w, operand: D8_6 */
1461 return BFD_RELOC_V850_TDA_6_8_OFFSET;
1462
1463 if (operand->bits == 4 && operand->insert != NULL)
1464 /* sld.hu, operand: D5-4 */
1465 return BFD_RELOC_V850_TDA_4_5_OFFSET;
1466
1467 if (operand->bits == 4 && operand->insert == NULL)
1468 /* sld.bu, operand: D4 */
1469 return BFD_RELOC_V850_TDA_4_4_OFFSET;
1470
1471 if (operand->bits == 16 && operand->shift == 16)
1472 /* set1 & chums, operands: D16 */
1473 return BFD_RELOC_V850_TDA_16_16_OFFSET;
1474
252b5132
RH
1475 if (operand->bits != 7)
1476 {
1477 as_bad (_("tdaoff() relocation used on an instruction which does not support it"));
28e4f854
KH
1478 /* Used to indicate an error condition. */
1479 return BFD_RELOC_64;
252b5132 1480 }
28e4f854 1481
252b5132 1482 return operand->insert != NULL
28e4f854
KH
1483 ? BFD_RELOC_V850_TDA_7_8_OFFSET /* sld.h/sst.h, operand: D8_7 */
1484 : BFD_RELOC_V850_TDA_7_7_OFFSET; /* sld.b/sst.b, opreand: D7 */
252b5132
RH
1485}
1486
1487/* Warning: The code in this function relies upon the definitions
1488 in the v850_operands[] array (defined in opcodes/v850-opc.c)
1489 matching the hard coded values contained herein. */
1490
9e0665bc
AM
1491static bfd_reloc_code_real_type v850_reloc_prefix
1492 PARAMS ((const struct v850_operand *));
1493
252b5132 1494static bfd_reloc_code_real_type
9e0665bc
AM
1495v850_reloc_prefix (operand)
1496 const struct v850_operand *operand;
252b5132 1497{
b34976b6 1498 bfd_boolean paren_skipped = FALSE;
252b5132 1499
252b5132 1500 /* Skip leading opening parenthesis. */
28e4f854 1501 if (*input_line_pointer == '(')
252b5132 1502 {
28e4f854 1503 ++input_line_pointer;
b34976b6 1504 paren_skipped = TRUE;
252b5132
RH
1505 }
1506
1507#define CHECK_(name, reloc) \
ec266e19 1508 if (strncmp (input_line_pointer, name "(", strlen (name) + 1) == 0) \
252b5132
RH
1509 { \
1510 input_line_pointer += strlen (name); \
1511 return reloc; \
1512 }
28e4f854
KH
1513
1514 CHECK_ ("hi0", BFD_RELOC_HI16 );
1515 CHECK_ ("hi", BFD_RELOC_HI16_S );
1516 CHECK_ ("lo", BFD_RELOC_LO16 );
252b5132
RH
1517 CHECK_ ("sdaoff", handle_sdaoff (operand));
1518 CHECK_ ("zdaoff", handle_zdaoff (operand));
1519 CHECK_ ("tdaoff", handle_tdaoff (operand));
28e4f854
KH
1520 CHECK_ ("hilo", BFD_RELOC_32 );
1521 CHECK_ ("ctoff", handle_ctoff (operand) );
1522
252b5132
RH
1523 /* Restore skipped parenthesis. */
1524 if (paren_skipped)
28e4f854
KH
1525 --input_line_pointer;
1526
252b5132
RH
1527 return BFD_RELOC_UNUSED;
1528}
1529
1530/* Insert an operand value into an instruction. */
1531
9e0665bc
AM
1532static unsigned long v850_insert_operand
1533 PARAMS ((unsigned long, const struct v850_operand *, offsetT, char *,
1534 unsigned int, char *));
1535
252b5132
RH
1536static unsigned long
1537v850_insert_operand (insn, operand, val, file, line, str)
28e4f854
KH
1538 unsigned long insn;
1539 const struct v850_operand *operand;
1540 offsetT val;
1541 char *file;
1542 unsigned int line;
1543 char *str;
252b5132
RH
1544{
1545 if (operand->insert)
1546 {
28e4f854
KH
1547 const char *message = NULL;
1548
1549 insn = operand->insert (insn, val, &message);
252b5132
RH
1550 if (message != NULL)
1551 {
1552 if ((operand->flags & V850_OPERAND_SIGNED)
1553 && ! warn_signed_overflows
1554 && strstr (message, "out of range") != NULL)
1555 {
28e4f854 1556 /* Skip warning... */
252b5132
RH
1557 }
1558 else if ((operand->flags & V850_OPERAND_SIGNED) == 0
1559 && ! warn_unsigned_overflows
1560 && strstr (message, "out of range") != NULL)
1561 {
28e4f854 1562 /* Skip warning... */
252b5132
RH
1563 }
1564 else if (str)
1565 {
1566 if (file == (char *) NULL)
1567 as_warn ("%s: %s", str, message);
1568 else
1569 as_warn_where (file, line, "%s: %s", str, message);
1570 }
1571 else
1572 {
1573 if (file == (char *) NULL)
1574 as_warn (message);
1575 else
1576 as_warn_where (file, line, message);
1577 }
1578 }
1579 }
1580 else
1581 {
1582 if (operand->bits != 32)
1583 {
28e4f854 1584 long min, max;
252b5132
RH
1585
1586 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1587 {
1588 if (! warn_signed_overflows)
1589 max = (1 << operand->bits) - 1;
1590 else
1591 max = (1 << (operand->bits - 1)) - 1;
28e4f854
KH
1592
1593 min = -(1 << (operand->bits - 1));
252b5132
RH
1594 }
1595 else
1596 {
1597 max = (1 << operand->bits) - 1;
28e4f854 1598
252b5132 1599 if (! warn_unsigned_overflows)
28e4f854 1600 min = -(1 << (operand->bits - 1));
252b5132
RH
1601 else
1602 min = 0;
1603 }
28e4f854 1604
252b5132
RH
1605 if (val < (offsetT) min || val > (offsetT) max)
1606 {
28e4f854
KH
1607 /* xgettext:c-format */
1608 const char *err =
1609 _("operand out of range (%s not between %ld and %ld)");
1610 char buf[100];
1611
252b5132
RH
1612 /* Restore min and mix to expected values for decimal ranges. */
1613 if ((operand->flags & V850_OPERAND_SIGNED)
1614 && ! warn_signed_overflows)
1615 max = (1 << (operand->bits - 1)) - 1;
1616
1617 if (! (operand->flags & V850_OPERAND_SIGNED)
1618 && ! warn_unsigned_overflows)
1619 min = 0;
1620
1621 if (str)
1622 {
1623 sprintf (buf, "%s: ", str);
28e4f854 1624
252b5132
RH
1625 sprint_value (buf + strlen (buf), val);
1626 }
1627 else
1628 sprint_value (buf, val);
28e4f854 1629
252b5132
RH
1630 if (file == (char *) NULL)
1631 as_warn (err, buf, min, max);
1632 else
1633 as_warn_where (file, line, err, buf, min, max);
1634 }
1635 }
1636
1637 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1638 }
28e4f854 1639
252b5132
RH
1640 return insn;
1641}
252b5132 1642\f
28e4f854 1643static char copy_of_instruction[128];
252b5132
RH
1644
1645void
28e4f854
KH
1646md_assemble (str)
1647 char *str;
252b5132 1648{
28e4f854
KH
1649 char *s;
1650 char *start_of_operands;
1651 struct v850_opcode *opcode;
1652 struct v850_opcode *next_opcode;
1653 const unsigned char *opindex_ptr;
1654 int next_opindex;
1655 int relaxable = 0;
1656 unsigned long insn;
1657 unsigned long insn_size;
1658 char *f;
1659 int i;
1660 int match;
b34976b6 1661 bfd_boolean extra_data_after_insn = FALSE;
28e4f854
KH
1662 unsigned extra_data_len = 0;
1663 unsigned long extra_data = 0;
1664 char *saved_input_line_pointer;
1665
252b5132 1666 strncpy (copy_of_instruction, str, sizeof (copy_of_instruction) - 1);
28e4f854 1667
252b5132 1668 /* Get the opcode. */
3882b010 1669 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
252b5132 1670 continue;
28e4f854 1671
252b5132
RH
1672 if (*s != '\0')
1673 *s++ = '\0';
1674
28e4f854 1675 /* Find the first opcode with the proper name. */
252b5132
RH
1676 opcode = (struct v850_opcode *) hash_find (v850_hash, str);
1677 if (opcode == NULL)
1678 {
28e4f854 1679 /* xgettext:c-format */
252b5132
RH
1680 as_bad (_("Unrecognized opcode: `%s'"), str);
1681 ignore_rest_of_line ();
1682 return;
1683 }
1684
1685 str = s;
3882b010 1686 while (ISSPACE (*str))
28e4f854 1687 ++str;
252b5132
RH
1688
1689 start_of_operands = str;
1690
1691 saved_input_line_pointer = input_line_pointer;
28e4f854 1692
252b5132
RH
1693 for (;;)
1694 {
28e4f854 1695 const char *errmsg = NULL;
252b5132
RH
1696
1697 match = 0;
28e4f854 1698
252b5132
RH
1699 if ((opcode->processors & processor_mask) == 0)
1700 {
1701 errmsg = _("Target processor does not support this instruction.");
1702 goto error;
1703 }
28e4f854 1704
252b5132
RH
1705 relaxable = 0;
1706 fc = 0;
1707 next_opindex = 0;
1708 insn = opcode->opcode;
b34976b6 1709 extra_data_after_insn = FALSE;
252b5132
RH
1710
1711 input_line_pointer = str = start_of_operands;
1712
28e4f854 1713 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
252b5132 1714 {
28e4f854
KH
1715 const struct v850_operand *operand;
1716 char *hold;
1717 expressionS ex;
1718 bfd_reloc_code_real_type reloc;
252b5132
RH
1719
1720 if (next_opindex == 0)
1721 {
28e4f854 1722 operand = &v850_operands[*opindex_ptr];
252b5132
RH
1723 }
1724 else
1725 {
28e4f854 1726 operand = &v850_operands[next_opindex];
252b5132
RH
1727 next_opindex = 0;
1728 }
1729
1730 errmsg = NULL;
1731
1732 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
28e4f854 1733 ++str;
252b5132
RH
1734
1735 if (operand->flags & V850_OPERAND_RELAX)
1736 relaxable = 1;
1737
28e4f854 1738 /* Gather the operand. */
252b5132
RH
1739 hold = input_line_pointer;
1740 input_line_pointer = str;
28e4f854
KH
1741
1742 /* lo(), hi(), hi0(), etc... */
252b5132
RH
1743 if ((reloc = v850_reloc_prefix (operand)) != BFD_RELOC_UNUSED)
1744 {
1745 /* This is a fake reloc, used to indicate an error condition. */
1746 if (reloc == BFD_RELOC_64)
1747 {
1748 match = 1;
1749 goto error;
1750 }
28e4f854
KH
1751
1752 expression (&ex);
252b5132
RH
1753
1754 if (ex.X_op == O_constant)
1755 {
1756 switch (reloc)
1757 {
1758 case BFD_RELOC_V850_ZDA_16_16_OFFSET:
1759 /* To cope with "not1 7, zdaoff(0xfffff006)[r0]"
1760 and the like. */
1761 /* Fall through. */
28e4f854 1762
252b5132
RH
1763 case BFD_RELOC_LO16:
1764 {
1765 /* Truncate, then sign extend the value. */
1766 ex.X_add_number = SEXT16 (ex.X_add_number);
1767 break;
1768 }
1769
1770 case BFD_RELOC_HI16:
1771 {
1772 /* Truncate, then sign extend the value. */
1773 ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
1774 break;
1775 }
1776
1777 case BFD_RELOC_HI16_S:
1778 {
1779 /* Truncate, then sign extend the value. */
28e4f854 1780 int temp = (ex.X_add_number >> 16) & 0xffff;
252b5132
RH
1781
1782 temp += (ex.X_add_number >> 15) & 1;
1783
1784 ex.X_add_number = SEXT16 (temp);
1785 break;
1786 }
28e4f854 1787
252b5132
RH
1788 case BFD_RELOC_32:
1789 if ((operand->flags & V850E_IMMEDIATE32) == 0)
1790 {
1791 errmsg = _("immediate operand is too large");
1792 goto error;
1793 }
28e4f854 1794
b34976b6 1795 extra_data_after_insn = TRUE;
252b5132 1796 extra_data_len = 4;
86aba9db 1797 extra_data = 0;
252b5132 1798 break;
28e4f854 1799
252b5132
RH
1800 default:
1801 fprintf (stderr, "reloc: %d\n", reloc);
1802 as_bad (_("AAARG -> unhandled constant reloc"));
1803 break;
1804 }
1805
1806 if (fc > MAX_INSN_FIXUPS)
1807 as_fatal (_("too many fixups"));
28e4f854
KH
1808
1809 fixups[fc].exp = ex;
1810 fixups[fc].opindex = *opindex_ptr;
1811 fixups[fc].reloc = reloc;
252b5132
RH
1812 fc++;
1813 }
1814 else
1815 {
1816 if (reloc == BFD_RELOC_32)
1817 {
1818 if ((operand->flags & V850E_IMMEDIATE32) == 0)
1819 {
1820 errmsg = _("immediate operand is too large");
1821 goto error;
1822 }
28e4f854 1823
b34976b6 1824 extra_data_after_insn = TRUE;
252b5132
RH
1825 extra_data_len = 4;
1826 extra_data = ex.X_add_number;
1827 }
28e4f854 1828
252b5132
RH
1829 if (fc > MAX_INSN_FIXUPS)
1830 as_fatal (_("too many fixups"));
1831
28e4f854
KH
1832 fixups[fc].exp = ex;
1833 fixups[fc].opindex = *opindex_ptr;
1834 fixups[fc].reloc = reloc;
252b5132
RH
1835 fc++;
1836 }
1837 }
1838 else
1839 {
1840 errmsg = NULL;
28e4f854
KH
1841
1842 if ((operand->flags & V850_OPERAND_REG) != 0)
252b5132 1843 {
28e4f854 1844 if (!register_name (&ex))
252b5132
RH
1845 {
1846 errmsg = _("invalid register name");
1847 }
1848 else if ((operand->flags & V850_NOT_R0)
28e4f854 1849 && ex.X_add_number == 0)
252b5132
RH
1850 {
1851 errmsg = _("register r0 cannot be used here");
28e4f854 1852
252b5132
RH
1853 /* Force an error message to be generated by
1854 skipping over any following potential matches
1855 for this opcode. */
1856 opcode += 3;
1857 }
1858 }
28e4f854 1859 else if ((operand->flags & V850_OPERAND_SRG) != 0)
252b5132 1860 {
b34976b6 1861 if (!system_register_name (&ex, TRUE, FALSE))
252b5132
RH
1862 {
1863 errmsg = _("invalid system register name");
1864 }
1865 }
1866 else if ((operand->flags & V850_OPERAND_EP) != 0)
1867 {
28e4f854
KH
1868 char *start = input_line_pointer;
1869 char c = get_symbol_end ();
1870
252b5132
RH
1871 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
1872 {
1873 /* Put things back the way we found them. */
1874 *input_line_pointer = c;
1875 input_line_pointer = start;
1876 errmsg = _("expected EP register");
1877 goto error;
1878 }
28e4f854 1879
252b5132
RH
1880 *input_line_pointer = c;
1881 str = input_line_pointer;
1882 input_line_pointer = hold;
28e4f854
KH
1883
1884 while (*str == ' ' || *str == ','
1885 || *str == '[' || *str == ']')
1886 ++str;
252b5132
RH
1887 continue;
1888 }
28e4f854 1889 else if ((operand->flags & V850_OPERAND_CC) != 0)
252b5132 1890 {
28e4f854 1891 if (!cc_name (&ex))
252b5132
RH
1892 {
1893 errmsg = _("invalid condition code name");
1894 }
1895 }
28e4f854 1896 else if (operand->flags & V850E_PUSH_POP)
252b5132 1897 {
28e4f854
KH
1898 errmsg = parse_register_list (&insn, operand);
1899
252b5132
RH
1900 /* The parse_register_list() function has already done
1901 everything, so fake a dummy expression. */
1902 ex.X_op = O_constant;
1903 ex.X_add_number = 0;
1904 }
28e4f854 1905 else if (operand->flags & V850E_IMMEDIATE16)
252b5132 1906 {
28e4f854 1907 expression (&ex);
252b5132
RH
1908
1909 if (ex.X_op != O_constant)
1910 errmsg = _("constant expression expected");
1911 else if (ex.X_add_number & 0xffff0000)
1912 {
1913 if (ex.X_add_number & 0xffff)
1914 errmsg = _("constant too big to fit into instruction");
1915 else if ((insn & 0x001fffc0) == 0x00130780)
1916 ex.X_add_number >>= 16;
1917 else
1918 errmsg = _("constant too big to fit into instruction");
1919 }
28e4f854 1920
b34976b6 1921 extra_data_after_insn = TRUE;
252b5132
RH
1922 extra_data_len = 2;
1923 extra_data = ex.X_add_number;
1924 ex.X_add_number = 0;
1925 }
28e4f854 1926 else if (operand->flags & V850E_IMMEDIATE32)
252b5132 1927 {
28e4f854
KH
1928 expression (&ex);
1929
252b5132
RH
1930 if (ex.X_op != O_constant)
1931 errmsg = _("constant expression expected");
28e4f854 1932
b34976b6 1933 extra_data_after_insn = TRUE;
252b5132
RH
1934 extra_data_len = 4;
1935 extra_data = ex.X_add_number;
1936 ex.X_add_number = 0;
1937 }
28e4f854 1938 else if (register_name (&ex)
252b5132
RH
1939 && (operand->flags & V850_OPERAND_REG) == 0)
1940 {
1941 char c;
28e4f854
KH
1942 int exists = 0;
1943
252b5132
RH
1944 /* It is possible that an alias has been defined that
1945 matches a register name. For example the code may
1946 include a ".set ZERO, 0" directive, which matches
1947 the register name "zero". Attempt to reparse the
1948 field as an expression, and only complain if we
1949 cannot generate a constant. */
1950
1951 input_line_pointer = str;
1952
1953 c = get_symbol_end ();
28e4f854 1954
252b5132
RH
1955 if (symbol_find (str) != NULL)
1956 exists = 1;
28e4f854
KH
1957
1958 *input_line_pointer = c;
252b5132 1959 input_line_pointer = str;
28e4f854
KH
1960
1961 expression (&ex);
252b5132
RH
1962
1963 if (ex.X_op != O_constant)
1964 {
1965 /* If this register is actually occuring too early on
1966 the parsing of the instruction, (because another
1967 field is missing) then report this. */
1968 if (opindex_ptr[1] != 0
28e4f854
KH
1969 && (v850_operands[opindex_ptr[1]].flags
1970 & V850_OPERAND_REG))
252b5132
RH
1971 errmsg = _("syntax error: value is missing before the register name");
1972 else
1973 errmsg = _("syntax error: register not expected");
1974
28e4f854
KH
1975 /* If we created a symbol in the process of this
1976 test then delete it now, so that it will not
1977 be output with the real symbols... */
252b5132
RH
1978 if (exists == 0
1979 && ex.X_op == O_symbol)
1980 symbol_remove (ex.X_add_symbol,
28e4f854 1981 &symbol_rootP, &symbol_lastP);
252b5132
RH
1982 }
1983 }
b34976b6 1984 else if (system_register_name (&ex, FALSE, FALSE)
252b5132
RH
1985 && (operand->flags & V850_OPERAND_SRG) == 0)
1986 {
1987 errmsg = _("syntax error: system register not expected");
1988 }
1989 else if (cc_name (&ex)
1990 && (operand->flags & V850_OPERAND_CC) == 0)
1991 {
1992 errmsg = _("syntax error: condition code not expected");
1993 }
1994 else
1995 {
28e4f854 1996 expression (&ex);
252b5132 1997 /* Special case:
50b15da2
NC
1998 If we are assembling a MOV instruction and the immediate
1999 value does not fit into the bits available then create a
2000 fake error so that the next MOV instruction will be
2001 selected. This one has a 32 bit immediate field. */
252b5132
RH
2002
2003 if (((insn & 0x07e0) == 0x0200)
50b15da2 2004 && operand->bits == 5 /* Do not match the CALLT instruction. */
252b5132 2005 && ex.X_op == O_constant
28e4f854 2006 && (ex.X_add_number < (-(1 << (operand->bits - 1)))
03223580 2007 || ex.X_add_number > ((1 << (operand->bits - 1)) - 1)))
252b5132
RH
2008 errmsg = _("immediate operand is too large");
2009 }
2010
2011 if (errmsg)
2012 goto error;
252b5132 2013
28e4f854
KH
2014#if 0
2015 fprintf (stderr,
2016 " insn: %x, operand %d, op: %d, add_number: %d\n",
2017 insn, opindex_ptr - opcode->operands,
2018 ex.X_op, ex.X_add_number);
2019#endif
2020
2021 switch (ex.X_op)
252b5132
RH
2022 {
2023 case O_illegal:
2024 errmsg = _("illegal operand");
2025 goto error;
2026 case O_absent:
2027 errmsg = _("missing operand");
2028 goto error;
2029 case O_register:
28e4f854
KH
2030 if ((operand->flags
2031 & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
252b5132
RH
2032 {
2033 errmsg = _("invalid operand");
2034 goto error;
2035 }
2036 insn = v850_insert_operand (insn, operand, ex.X_add_number,
2037 (char *) NULL, 0,
2038 copy_of_instruction);
2039 break;
2040
2041 case O_constant:
2042 insn = v850_insert_operand (insn, operand, ex.X_add_number,
2043 (char *) NULL, 0,
2044 copy_of_instruction);
2045 break;
2046
2047 default:
2048 /* We need to generate a fixup for this expression. */
2049 if (fc >= MAX_INSN_FIXUPS)
2050 as_fatal (_("too many fixups"));
2051
28e4f854
KH
2052 fixups[fc].exp = ex;
2053 fixups[fc].opindex = *opindex_ptr;
2054 fixups[fc].reloc = BFD_RELOC_UNUSED;
252b5132
RH
2055 ++fc;
2056 break;
2057 }
2058 }
2059
2060 str = input_line_pointer;
2061 input_line_pointer = hold;
2062
2063 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
2064 || *str == ')')
2065 ++str;
2066 }
2067 match = 1;
2068
2069 error:
2070 if (match == 0)
28e4f854 2071 {
252b5132
RH
2072 next_opcode = opcode + 1;
2073 if (next_opcode->name != NULL
2074 && strcmp (next_opcode->name, opcode->name) == 0)
2075 {
2076 opcode = next_opcode;
2077
2078 /* Skip versions that are not supported by the target
2079 processor. */
2080 if ((opcode->processors & processor_mask) == 0)
2081 goto error;
28e4f854 2082
252b5132
RH
2083 continue;
2084 }
28e4f854 2085
252b5132 2086 as_bad ("%s: %s", copy_of_instruction, errmsg);
28e4f854
KH
2087
2088 if (*input_line_pointer == ']')
2089 ++input_line_pointer;
2090
252b5132
RH
2091 ignore_rest_of_line ();
2092 input_line_pointer = saved_input_line_pointer;
2093 return;
28e4f854 2094 }
252b5132
RH
2095 break;
2096 }
28e4f854 2097
3882b010 2098 while (ISSPACE (*str))
252b5132
RH
2099 ++str;
2100
2101 if (*str != '\0')
28e4f854 2102 /* xgettext:c-format */
252b5132
RH
2103 as_bad (_("junk at end of line: `%s'"), str);
2104
2105 input_line_pointer = str;
2106
9fcc94b6
AM
2107 /* Tie dwarf2 debug info to the address at the start of the insn.
2108 We can't do this after the insn has been output as the current
2109 frag may have been closed off. eg. by frag_var. */
2110 dwarf2_emit_insn (0);
2111
28e4f854
KH
2112 /* Write out the instruction. */
2113
252b5132
RH
2114 if (relaxable && fc > 0)
2115 {
2116 insn_size = 2;
2117 fc = 0;
2118
2119 if (!strcmp (opcode->name, "br"))
2120 {
2121 f = frag_var (rs_machine_dependent, 4, 2, 2,
2122 fixups[0].exp.X_add_symbol,
2123 fixups[0].exp.X_add_number,
28e4f854 2124 (char *) fixups[0].opindex);
252b5132
RH
2125 md_number_to_chars (f, insn, insn_size);
2126 md_number_to_chars (f + 2, 0, 2);
2127 }
2128 else
2129 {
2130 f = frag_var (rs_machine_dependent, 6, 4, 0,
2131 fixups[0].exp.X_add_symbol,
2132 fixups[0].exp.X_add_number,
28e4f854 2133 (char *) fixups[0].opindex);
252b5132
RH
2134 md_number_to_chars (f, insn, insn_size);
2135 md_number_to_chars (f + 2, 0, 4);
2136 }
2137 }
28e4f854 2138 else
252b5132
RH
2139 {
2140 /* Four byte insns have an opcode with the two high bits on. */
2141 if ((insn & 0x0600) == 0x0600)
2142 insn_size = 4;
2143 else
2144 insn_size = 2;
2145
28e4f854 2146 /* Special case: 32 bit MOV. */
252b5132
RH
2147 if ((insn & 0xffe0) == 0x0620)
2148 insn_size = 2;
28e4f854 2149
252b5132 2150 f = frag_more (insn_size);
252b5132
RH
2151 md_number_to_chars (f, insn, insn_size);
2152
2153 if (extra_data_after_insn)
2154 {
2155 f = frag_more (extra_data_len);
252b5132
RH
2156 md_number_to_chars (f, extra_data, extra_data_len);
2157
b34976b6 2158 extra_data_after_insn = FALSE;
252b5132
RH
2159 }
2160 }
2161
2162 /* Create any fixups. At this point we do not use a
2163 bfd_reloc_code_real_type, but instead just use the
2164 BFD_RELOC_UNUSED plus the operand index. This lets us easily
2165 handle fixups for any operand type, although that is admittedly
2166 not a very exciting feature. We pick a BFD reloc type in
94f592af 2167 md_apply_fix3. */
252b5132
RH
2168 for (i = 0; i < fc; i++)
2169 {
28e4f854
KH
2170 const struct v850_operand *operand;
2171 bfd_reloc_code_real_type reloc;
2172
2173 operand = &v850_operands[fixups[i].opindex];
252b5132
RH
2174
2175 reloc = fixups[i].reloc;
28e4f854 2176
252b5132
RH
2177 if (reloc != BFD_RELOC_UNUSED)
2178 {
28e4f854
KH
2179 reloc_howto_type *reloc_howto =
2180 bfd_reloc_type_lookup (stdoutput, reloc);
2181 int size;
2182 int address;
2183 fixS *fixP;
252b5132
RH
2184
2185 if (!reloc_howto)
28e4f854
KH
2186 abort ();
2187
252b5132
RH
2188 size = bfd_get_reloc_size (reloc_howto);
2189
2190 /* XXX This will abort on an R_V850_8 reloc -
28e4f854
KH
2191 is this reloc actually used? */
2192 if (size != 2 && size != 4)
252b5132
RH
2193 abort ();
2194
2195 address = (f - frag_now->fr_literal) + insn_size - size;
2196
2197 if (reloc == BFD_RELOC_32)
28e4f854
KH
2198 address += 2;
2199
252b5132 2200 fixP = fix_new_exp (frag_now, address, size,
28e4f854 2201 &fixups[i].exp,
252b5132
RH
2202 reloc_howto->pc_relative,
2203 reloc);
2204
2205 switch (reloc)
2206 {
2207 case BFD_RELOC_LO16:
2208 case BFD_RELOC_HI16:
2209 case BFD_RELOC_HI16_S:
2210 fixP->fx_no_overflow = 1;
2211 break;
5480ccf3
NC
2212 default:
2213 break;
252b5132
RH
2214 }
2215 }
2216 else
2217 {
28e4f854 2218 fix_new_exp (frag_now,
252b5132
RH
2219 f - frag_now->fr_literal, 4,
2220 & fixups[i].exp,
28e4f854 2221 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
252b5132 2222 (bfd_reloc_code_real_type) (fixups[i].opindex
28e4f854 2223 + (int) BFD_RELOC_UNUSED));
252b5132
RH
2224 }
2225 }
2226
2227 input_line_pointer = saved_input_line_pointer;
2228}
2229
28e4f854
KH
2230/* If while processing a fixup, a reloc really needs to be created
2231 then it is done here. */
252b5132 2232
252b5132
RH
2233arelent *
2234tc_gen_reloc (seg, fixp)
28e4f854
KH
2235 asection *seg ATTRIBUTE_UNUSED;
2236 fixS *fixp;
252b5132 2237{
28e4f854
KH
2238 arelent *reloc;
2239
2240 reloc = (arelent *) xmalloc (sizeof (arelent));
2241 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2242 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2243 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2244 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
252b5132
RH
2245
2246 if (reloc->howto == (reloc_howto_type *) NULL)
2247 {
2248 as_bad_where (fixp->fx_file, fixp->fx_line,
28e4f854
KH
2249 /* xgettext:c-format */
2250 _("reloc %d not supported by object file format"),
252b5132
RH
2251 (int) fixp->fx_r_type);
2252
2253 xfree (reloc);
28e4f854 2254
252b5132
RH
2255 return NULL;
2256 }
28e4f854
KH
2257
2258 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
252b5132
RH
2259 || fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
2260 reloc->addend = fixp->fx_offset;
86aba9db
NC
2261 else if ( fixp->fx_r_type == BFD_RELOC_V850_LONGCALL
2262 || fixp->fx_r_type == BFD_RELOC_V850_LONGJUMP
2263 || fixp->fx_r_type == BFD_RELOC_V850_ALIGN)
2264 reloc->addend = fixp->fx_offset;
252b5132
RH
2265 else
2266 reloc->addend = fixp->fx_addnumber;
28e4f854 2267
252b5132
RH
2268 return reloc;
2269}
2270
86aba9db
NC
2271void
2272v850_handle_align (frag)
2273 fragS * frag;
2274{
2275 if (v850_relax
2276 && frag->fr_type == rs_align
2277 && frag->fr_address + frag->fr_fix > 0
2278 && frag->fr_offset > 1
2279 && now_seg != bss_section
2280 && now_seg != v850_seg_table[SBSS_SECTION].s
2281 && now_seg != v850_seg_table[TBSS_SECTION].s
2282 && now_seg != v850_seg_table[ZBSS_SECTION].s)
2283 fix_new (frag, frag->fr_fix, 2, & abs_symbol, frag->fr_offset, 0,
2284 BFD_RELOC_V850_ALIGN);
2285}
2286
606ab118 2287/* Return current size of variable part of frag. */
28e4f854 2288
252b5132
RH
2289int
2290md_estimate_size_before_relax (fragp, seg)
28e4f854
KH
2291 fragS *fragp;
2292 asection *seg ATTRIBUTE_UNUSED;
252b5132 2293{
606ab118 2294 if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
252b5132 2295 abort ();
606ab118
AM
2296
2297 return md_relax_table[fragp->fr_subtype].rlx_length;
28e4f854 2298}
252b5132
RH
2299
2300long
2301v850_pcrel_from_section (fixp, section)
28e4f854
KH
2302 fixS *fixp;
2303 segT section;
252b5132
RH
2304{
2305 /* If the symbol is undefined, or in a section other than our own,
d6c497c7 2306 or it is weak (in which case it may well be in another section,
252b5132
RH
2307 then let the linker figure it out. */
2308 if (fixp->fx_addsy != (symbolS *) NULL
2309 && (! S_IS_DEFINED (fixp->fx_addsy)
d6c497c7 2310 || S_IS_WEAK (fixp->fx_addsy)
252b5132 2311 || (S_GET_SEGMENT (fixp->fx_addsy) != section)))
d6c497c7 2312 return 0;
28e4f854 2313
252b5132
RH
2314 return fixp->fx_frag->fr_address + fixp->fx_where;
2315}
2316
94f592af
NC
2317void
2318md_apply_fix3 (fixP, valueP, seg)
2319 fixS *fixP;
2320 valueT *valueP;
28e4f854 2321 segT seg ATTRIBUTE_UNUSED;
252b5132 2322{
94f592af 2323 valueT value = * valueP;
28e4f854 2324 char *where;
252b5132 2325
94f592af 2326 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
86aba9db
NC
2327 || fixP->fx_r_type == BFD_RELOC_V850_LONGCALL
2328 || fixP->fx_r_type == BFD_RELOC_V850_LONGJUMP
94f592af 2329 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132 2330 {
94f592af
NC
2331 fixP->fx_done = 0;
2332 return;
252b5132
RH
2333 }
2334
94f592af 2335 if (fixP->fx_addsy == (symbolS *) NULL)
86aba9db 2336 fixP->fx_addnumber = value,
94f592af
NC
2337 fixP->fx_done = 1;
2338
2339 else if (fixP->fx_pcrel)
86aba9db 2340 fixP->fx_addnumber = fixP->fx_offset;
94f592af 2341
252b5132
RH
2342 else
2343 {
94f592af
NC
2344 value = fixP->fx_offset;
2345 if (fixP->fx_subsy != (symbolS *) NULL)
252b5132 2346 {
94f592af
NC
2347 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
2348 value -= S_GET_VALUE (fixP->fx_subsy);
252b5132
RH
2349 else
2350 {
2351 /* We don't actually support subtracting a symbol. */
94f592af 2352 as_bad_where (fixP->fx_file, fixP->fx_line,
252b5132
RH
2353 _("expression too complex"));
2354 }
2355 }
86aba9db 2356 fixP->fx_addnumber = value;
252b5132
RH
2357 }
2358
94f592af 2359 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
252b5132 2360 {
28e4f854
KH
2361 int opindex;
2362 const struct v850_operand *operand;
2363 unsigned long insn;
252b5132 2364
94f592af 2365 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
28e4f854 2366 operand = &v850_operands[opindex];
252b5132
RH
2367
2368 /* Fetch the instruction, insert the fully resolved operand
2369 value, and stuff the instruction back again.
2370
2371 Note the instruction has been stored in little endian
2372 format! */
94f592af 2373 where = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
2374
2375 insn = bfd_getl32 ((unsigned char *) where);
2376 insn = v850_insert_operand (insn, operand, (offsetT) value,
94f592af 2377 fixP->fx_file, fixP->fx_line, NULL);
252b5132
RH
2378 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
2379
94f592af
NC
2380 if (fixP->fx_done)
2381 /* Nothing else to do here. */
2382 return;
252b5132 2383
28e4f854
KH
2384 /* Determine a BFD reloc value based on the operand information.
2385 We are only prepared to turn a few of the operands into relocs. */
252b5132
RH
2386
2387 if (operand->bits == 22)
94f592af 2388 fixP->fx_r_type = BFD_RELOC_V850_22_PCREL;
252b5132 2389 else if (operand->bits == 9)
94f592af 2390 fixP->fx_r_type = BFD_RELOC_V850_9_PCREL;
252b5132
RH
2391 else
2392 {
28e4f854
KH
2393#if 0
2394 fprintf (stderr, "bits: %d, insn: %x\n", operand->bits, insn);
2395#endif
2396
94f592af 2397 as_bad_where (fixP->fx_file, fixP->fx_line,
28e4f854 2398 _("unresolved expression that must be resolved"));
94f592af
NC
2399 fixP->fx_done = 1;
2400 return;
252b5132
RH
2401 }
2402 }
94f592af 2403 else if (fixP->fx_done)
252b5132
RH
2404 {
2405 /* We still have to insert the value into memory! */
94f592af 2406 where = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 2407
94f592af 2408 if (fixP->fx_size == 1)
28e4f854 2409 *where = value & 0xff;
94f592af 2410 else if (fixP->fx_size == 2)
252b5132 2411 bfd_putl16 (value & 0xffff, (unsigned char *) where);
94f592af 2412 else if (fixP->fx_size == 4)
252b5132
RH
2413 bfd_putl32 (value, (unsigned char *) where);
2414 }
252b5132 2415}
252b5132
RH
2416\f
2417/* Parse a cons expression. We have to handle hi(), lo(), etc
2418 on the v850. */
28e4f854 2419
252b5132
RH
2420void
2421parse_cons_expression_v850 (exp)
28e4f854 2422 expressionS *exp;
252b5132
RH
2423{
2424 /* See if there's a reloc prefix like hi() we have to handle. */
2425 hold_cons_reloc = v850_reloc_prefix (NULL);
2426
2427 /* Do normal expression parsing. */
2428 expression (exp);
2429}
2430
2431/* Create a fixup for a cons expression. If parse_cons_expression_v850
2432 found a reloc prefix, then we use that reloc, else we choose an
2433 appropriate one based on the size of the expression. */
28e4f854 2434
252b5132
RH
2435void
2436cons_fix_new_v850 (frag, where, size, exp)
28e4f854 2437 fragS *frag;
252b5132
RH
2438 int where;
2439 int size;
2440 expressionS *exp;
2441{
2442 if (hold_cons_reloc == BFD_RELOC_UNUSED)
2443 {
2444 if (size == 4)
2445 hold_cons_reloc = BFD_RELOC_32;
2446 if (size == 2)
2447 hold_cons_reloc = BFD_RELOC_16;
2448 if (size == 1)
2449 hold_cons_reloc = BFD_RELOC_8;
2450 }
2451
2452 if (exp != NULL)
2453 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
2454 else
2455 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
a8761a19
DD
2456
2457 hold_cons_reloc = BFD_RELOC_UNUSED;
252b5132 2458}
d6c497c7 2459
b34976b6 2460bfd_boolean
252b5132 2461v850_fix_adjustable (fixP)
28e4f854 2462 fixS *fixP;
252b5132 2463{
252b5132
RH
2464 if (fixP->fx_addsy == NULL)
2465 return 1;
28e4f854 2466
28e4f854 2467 /* Don't adjust function names. */
252b5132
RH
2468 if (S_IS_FUNCTION (fixP->fx_addsy))
2469 return 0;
2470
28e4f854
KH
2471 /* We need the symbol name for the VTABLE entries. */
2472 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
252b5132
RH
2473 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2474 return 0;
28e4f854 2475
252b5132
RH
2476 return 1;
2477}
28e4f854 2478
252b5132 2479int
d6c497c7 2480v850_force_relocation (fixP)
28e4f854 2481 struct fix *fixP;
252b5132 2482{
a161fe53 2483 if (fixP->fx_r_type == BFD_RELOC_V850_LONGCALL
86aba9db
NC
2484 || fixP->fx_r_type == BFD_RELOC_V850_LONGJUMP)
2485 return 1;
2486
2487 if (v850_relax
2488 && (fixP->fx_pcrel
a161fe53
AM
2489 || fixP->fx_r_type == BFD_RELOC_V850_ALIGN
2490 || fixP->fx_r_type == BFD_RELOC_V850_22_PCREL
2491 || fixP->fx_r_type == BFD_RELOC_V850_9_PCREL
2492 || fixP->fx_r_type >= BFD_RELOC_UNUSED))
86aba9db
NC
2493 return 1;
2494
ae6063d4 2495 return generic_force_reloc (fixP);
252b5132 2496}
This page took 0.346456 seconds and 4 git commands to generate.