2012-11-09 Nick Clifton <nickc@redhat.com>
[deliverable/binutils-gdb.git] / gas / config / tc-h8300.c
CommitLineData
c2dcd04e 1/* tc-h8300.c -- Assemble code for the Renesas H8/300
cc8a6dd0 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
35a35807 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012
aa820537 4 Free Software Foundation, Inc.
252b5132
RH
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
ec2655a6 10 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
252b5132 22
bc0d738a 23/* Written By Steve Chamberlain <sac@cygnus.com>. */
252b5132 24
252b5132
RH
25#include "as.h"
26#include "subsegs.h"
2c8714f2 27#include "dwarf2dbg.h"
2c8714f2 28
252b5132
RH
29#define DEFINE_TABLE
30#define h8_opcodes ops
31#include "opcode/h8300.h"
3882b010 32#include "safe-ctype.h"
252b5132 33
7e0de7bf
JL
34#ifdef OBJ_ELF
35#include "elf/h8.h"
7e0de7bf
JL
36#endif
37
63a0b638 38const char comment_chars[] = ";";
252b5132 39const char line_comment_chars[] = "#";
63a0b638 40const char line_separator_chars[] = "";
252b5132 41
600e9c99
KH
42static void sbranch (int);
43static void h8300hmode (int);
44static void h8300smode (int);
45static void h8300hnmode (int);
46static void h8300snmode (int);
47static void h8300sxmode (int);
48static void h8300sxnmode (int);
b54a3392 49static void pint (int);
252b5132 50
66faad26
KH
51int Hmode;
52int Smode;
53int Nmode;
54int SXmode;
3048287a 55
d4e2de6b 56#define PSIZE (Hmode && !Nmode ? L_32 : L_16)
3048287a 57
600e9c99 58static int bsize = L_8; /* Default branch displacement. */
252b5132 59
a720f7bc
KD
60struct h8_instruction
61{
62 int length;
63 int noperands;
64 int idx;
65 int size;
66 const struct h8_opcode *opcode;
67};
68
600e9c99 69static struct h8_instruction *h8_instructions;
a720f7bc 70
600e9c99 71static void
b54a3392 72h8300hmode (int arg ATTRIBUTE_UNUSED)
252b5132
RH
73{
74 Hmode = 1;
75 Smode = 0;
83e20b45
JL
76 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300h))
77 as_warn (_("could not set architecture and machine"));
252b5132
RH
78}
79
600e9c99 80static void
b54a3392 81h8300smode (int arg ATTRIBUTE_UNUSED)
252b5132
RH
82{
83 Smode = 1;
84 Hmode = 1;
83e20b45
JL
85 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300s))
86 as_warn (_("could not set architecture and machine"));
252b5132 87}
70d6ecf3 88
600e9c99 89static void
b54a3392 90h8300hnmode (int arg ATTRIBUTE_UNUSED)
8d9cd6b1
NC
91{
92 Hmode = 1;
93 Smode = 0;
94 Nmode = 1;
8d9cd6b1
NC
95 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300hn))
96 as_warn (_("could not set architecture and machine"));
8d9cd6b1
NC
97}
98
600e9c99 99static void
b54a3392 100h8300snmode (int arg ATTRIBUTE_UNUSED)
8d9cd6b1
NC
101{
102 Smode = 1;
103 Hmode = 1;
104 Nmode = 1;
8d9cd6b1
NC
105 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sn))
106 as_warn (_("could not set architecture and machine"));
8d9cd6b1
NC
107}
108
600e9c99 109static void
b54a3392 110h8300sxmode (int arg ATTRIBUTE_UNUSED)
7ee7b84d
MS
111{
112 Smode = 1;
113 Hmode = 1;
114 SXmode = 1;
7ee7b84d
MS
115 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sx))
116 as_warn (_("could not set architecture and machine"));
7ee7b84d
MS
117}
118
600e9c99 119static void
b54a3392 120h8300sxnmode (int arg ATTRIBUTE_UNUSED)
f4984206
RS
121{
122 Smode = 1;
123 Hmode = 1;
124 SXmode = 1;
125 Nmode = 1;
f4984206
RS
126 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sxn))
127 as_warn (_("could not set architecture and machine"));
f4984206
RS
128}
129
600e9c99 130static void
b54a3392 131sbranch (int size)
252b5132
RH
132{
133 bsize = size;
134}
135
70d6ecf3 136static void
b54a3392 137pint (int arg ATTRIBUTE_UNUSED)
252b5132
RH
138{
139 cons (Hmode ? 4 : 2);
140}
141
52b010e4
NC
142/* Like obj_elf_section, but issues a warning for new
143 sections which do not have an attribute specification. */
144
145static void
146h8300_elf_section (int push)
147{
148 static const char * known_data_sections [] = { ".rodata", ".tdata", ".tbss" };
9c8b3bfe 149 static const char * known_data_prefixes [] = { ".debug", ".zdebug", ".gnu.warning" };
52b010e4
NC
150 char * saved_ilp = input_line_pointer;
151 char * name;
152
153 name = obj_elf_section_name ();
154 if (name == NULL)
155 return;
156
157 if (* input_line_pointer != ','
158 && bfd_get_section_by_name (stdoutput, name) == NULL)
159 {
160 signed int i;
161
162 /* Ignore this warning for well known data sections. */
163 for (i = ARRAY_SIZE (known_data_sections); i--;)
164 if (strcmp (name, known_data_sections[i]) == 0)
165 break;
166
167 if (i < 0)
168 for (i = ARRAY_SIZE (known_data_prefixes); i--;)
169 if (strncmp (name, known_data_prefixes[i],
170 strlen (known_data_prefixes[i])) == 0)
171 break;
172
173 if (i < 0)
174 as_warn (_("new section '%s' defined without attributes - this might cause problems"), name);
175 }
176
177 /* FIXME: We ought to free the memory allocated by obj_elf_section_name()
178 for 'name', but we do not know if it was taken from the obstack, via
179 demand_copy_C_string(), or xmalloc()ed. */
180 input_line_pointer = saved_ilp;
181 obj_elf_section (push);
182}
183
3048287a
NC
184/* This table describes all the machine specific pseudo-ops the assembler
185 has to support. The fields are:
186 pseudo-op name without dot
187 function to call to execute this pseudo-op
188 Integer arg to pass to the function. */
189
252b5132
RH
190const pseudo_typeS md_pseudo_table[] =
191{
7ee7b84d 192 {"h8300h", h8300hmode, 0},
8d9cd6b1 193 {"h8300hn", h8300hnmode, 0},
7ee7b84d 194 {"h8300s", h8300smode, 0},
8d9cd6b1 195 {"h8300sn", h8300snmode, 0},
7ee7b84d 196 {"h8300sx", h8300sxmode, 0},
f4984206 197 {"h8300sxn", h8300sxnmode, 0},
252b5132
RH
198 {"sbranch", sbranch, L_8},
199 {"lbranch", sbranch, L_16},
200
201 {"int", pint, 0},
202 {"data.b", cons, 1},
203 {"data.w", cons, 2},
204 {"data.l", cons, 4},
205 {"form", listing_psize, 0},
206 {"heading", listing_title, 0},
7ee7b84d
MS
207 {"import", s_ignore, 0},
208 {"page", listing_eject, 0},
252b5132 209 {"program", s_ignore, 0},
52b010e4
NC
210
211#ifdef OBJ_ELF
212 {"section", h8300_elf_section, 0},
213 {"section.s", h8300_elf_section, 0},
214 {"sect", h8300_elf_section, 0},
215 {"sect.s", h8300_elf_section, 0},
216#endif
217
252b5132
RH
218 {0, 0, 0}
219};
220
252b5132
RH
221const char EXP_CHARS[] = "eE";
222
3048287a
NC
223/* Chars that mean this number is a floating point constant
224 As in 0f12.456
225 or 0d1.2345e12. */
252b5132
RH
226const char FLT_CHARS[] = "rRsSfFdDxXpP";
227
3048287a 228static struct hash_control *opcode_hash_control; /* Opcode mnemonics. */
252b5132 229
70d6ecf3
AM
230/* This function is called once, at assembler startup time. This
231 should set up all the tables, etc. that the MD part of the assembler
232 needs. */
3048287a 233
252b5132 234void
b54a3392 235md_begin (void)
252b5132 236{
a720f7bc 237 unsigned int nopcodes;
7ee7b84d 238 struct h8_opcode *p, *p1;
a720f7bc 239 struct h8_instruction *pi;
252b5132
RH
240 char prev_buffer[100];
241 int idx = 0;
242
83e20b45
JL
243 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300))
244 as_warn (_("could not set architecture and machine"));
83e20b45 245
252b5132
RH
246 opcode_hash_control = hash_new ();
247 prev_buffer[0] = 0;
248
a720f7bc
KD
249 nopcodes = sizeof (h8_opcodes) / sizeof (struct h8_opcode);
250
251 h8_instructions = (struct h8_instruction *)
252 xmalloc (nopcodes * sizeof (struct h8_instruction));
253
7ee7b84d
MS
254 pi = h8_instructions;
255 p1 = h8_opcodes;
256 /* We do a minimum amount of sorting on the opcode table; this is to
257 make it easy to describe the mova instructions without unnecessary
258 code duplication.
259 Sorting only takes place inside blocks of instructions of the form
260 X/Y, so for example mova/b, mova/w and mova/l can be intermixed. */
261 while (p1)
252b5132 262 {
7ee7b84d
MS
263 struct h8_opcode *first_skipped = 0;
264 int len, cmplen = 0;
265 char *src = p1->name;
266 char *dst, *buffer;
252b5132 267
7ee7b84d
MS
268 if (p1->name == 0)
269 break;
270 /* Strip off any . part when inserting the opcode and only enter
271 unique codes into the hash table. */
272 dst = buffer = malloc (strlen (src) + 1);
252b5132
RH
273 while (*src)
274 {
275 if (*src == '.')
276 {
277 src++;
252b5132
RH
278 break;
279 }
7ee7b84d
MS
280 if (*src == '/')
281 cmplen = src - p1->name + 1;
252b5132
RH
282 *dst++ = *src++;
283 }
7ee7b84d
MS
284 *dst = 0;
285 len = dst - buffer;
286 if (cmplen == 0)
287 cmplen = len;
288 hash_insert (opcode_hash_control, buffer, (char *) pi);
289 strcpy (prev_buffer, buffer);
290 idx++;
291
292 for (p = p1; p->name; p++)
252b5132 293 {
7ee7b84d
MS
294 /* A negative TIME is used to indicate that we've added this opcode
295 already. */
296 if (p->time == -1)
297 continue;
298 if (strncmp (p->name, buffer, cmplen) != 0
299 || (p->name[cmplen] != '\0' && p->name[cmplen] != '.'
300 && p->name[cmplen - 1] != '/'))
301 {
302 if (first_skipped == 0)
303 first_skipped = p;
304 break;
305 }
306 if (strncmp (p->name, buffer, len) != 0)
307 {
308 if (first_skipped == 0)
309 first_skipped = p;
310 continue;
311 }
312
313 p->time = -1;
314 pi->size = p->name[len] == '.' ? p->name[len + 1] : 0;
315 pi->idx = idx;
252b5132 316
7ee7b84d
MS
317 /* Find the number of operands. */
318 pi->noperands = 0;
319 while (pi->noperands < 3 && p->args.nib[pi->noperands] != (op_type) E)
320 pi->noperands++;
70d6ecf3 321
7ee7b84d
MS
322 /* Find the length of the opcode in bytes. */
323 pi->length = 0;
324 while (p->data.nib[pi->length * 2] != (op_type) E)
325 pi->length++;
a720f7bc 326
7ee7b84d
MS
327 pi->opcode = p;
328 pi++;
329 }
330 p1 = first_skipped;
252b5132
RH
331 }
332
a720f7bc
KD
333 /* Add entry for the NULL vector terminator. */
334 pi->length = 0;
335 pi->noperands = 0;
336 pi->idx = 0;
337 pi->size = 0;
7ee7b84d 338 pi->opcode = 0;
a720f7bc 339
252b5132
RH
340 linkrelax = 1;
341}
342
252b5132
RH
343struct h8_op
344{
345 op_type mode;
346 unsigned reg;
347 expressionS exp;
348};
349
b54a3392
KH
350static void clever_message (const struct h8_instruction *, struct h8_op *);
351static void fix_operand_size (struct h8_op *, int);
352static void build_bytes (const struct h8_instruction *, struct h8_op *);
bcb012d3 353static void do_a_fix_imm (int, int, struct h8_op *, int, const struct h8_instruction *);
b54a3392
KH
354static void check_operand (struct h8_op *, unsigned int, char *);
355static const struct h8_instruction * get_specific (const struct h8_instruction *, struct h8_op *, int) ;
356static char *get_operands (unsigned, char *, struct h8_op *);
357static void get_operand (char **, struct h8_op *, int);
358static int parse_reg (char *, op_type *, unsigned *, int);
359static char *skip_colonthing (char *, int *);
360static char *parse_exp (char *, struct h8_op *);
361
b54a3392 362static int constant_fits_size_p (struct h8_op *, int, int);
7ee7b84d 363
252b5132
RH
364/*
365 parse operands
366 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
367 r0l,r0h,..r7l,r7h
368 @WREG
369 @WREG+
370 @-WREG
371 #const
372 ccr
373*/
374
bc0d738a
NC
375/* Try to parse a reg name. Return the number of chars consumed. */
376
40f09f82 377static int
b54a3392 378parse_reg (char *src, op_type *mode, unsigned int *reg, int direction)
252b5132
RH
379{
380 char *end;
381 int len;
382
70d6ecf3 383 /* Cribbed from get_symbol_end. */
252b5132
RH
384 if (!is_name_beginner (*src) || *src == '\001')
385 return 0;
70d6ecf3 386 end = src + 1;
7ee7b84d 387 while ((is_part_of_name (*end) && *end != '.') || *end == '\001')
252b5132
RH
388 end++;
389 len = end - src;
390
7ee7b84d 391 if (len == 2 && TOLOWER (src[0]) == 's' && TOLOWER (src[1]) == 'p')
252b5132
RH
392 {
393 *mode = PSIZE | REG | direction;
394 *reg = 7;
395 return len;
396 }
7ee7b84d
MS
397 if (len == 3 &&
398 TOLOWER (src[0]) == 'c' &&
399 TOLOWER (src[1]) == 'c' &&
400 TOLOWER (src[2]) == 'r')
252b5132
RH
401 {
402 *mode = CCR;
403 *reg = 0;
404 return len;
405 }
7ee7b84d
MS
406 if (len == 3 &&
407 TOLOWER (src[0]) == 'e' &&
408 TOLOWER (src[1]) == 'x' &&
409 TOLOWER (src[2]) == 'r')
252b5132
RH
410 {
411 *mode = EXR;
7ee7b84d
MS
412 *reg = 1;
413 return len;
414 }
415 if (len == 3 &&
416 TOLOWER (src[0]) == 'v' &&
417 TOLOWER (src[1]) == 'b' &&
418 TOLOWER (src[2]) == 'r')
419 {
420 *mode = VBR;
421 *reg = 6;
422 return len;
423 }
424 if (len == 3 &&
425 TOLOWER (src[0]) == 's' &&
426 TOLOWER (src[1]) == 'b' &&
427 TOLOWER (src[2]) == 'r')
428 {
429 *mode = SBR;
430 *reg = 7;
252b5132
RH
431 return len;
432 }
7ee7b84d 433 if (len == 2 && TOLOWER (src[0]) == 'f' && TOLOWER (src[1]) == 'p')
252b5132
RH
434 {
435 *mode = PSIZE | REG | direction;
436 *reg = 6;
437 return len;
438 }
7ee7b84d
MS
439 if (len == 3 && TOLOWER (src[0]) == 'e' && TOLOWER (src[1]) == 'r' &&
440 src[2] >= '0' && src[2] <= '7')
252b5132
RH
441 {
442 *mode = L_32 | REG | direction;
443 *reg = src[2] - '0';
444 if (!Hmode)
445 as_warn (_("Reg not valid for H8/300"));
446 return len;
447 }
7ee7b84d 448 if (len == 2 && TOLOWER (src[0]) == 'e' && src[1] >= '0' && src[1] <= '7')
252b5132
RH
449 {
450 *mode = L_16 | REG | direction;
451 *reg = src[1] - '0' + 8;
452 if (!Hmode)
453 as_warn (_("Reg not valid for H8/300"));
454 return len;
455 }
456
7ee7b84d 457 if (TOLOWER (src[0]) == 'r')
252b5132
RH
458 {
459 if (src[1] >= '0' && src[1] <= '7')
460 {
7ee7b84d 461 if (len == 3 && TOLOWER (src[2]) == 'l')
252b5132
RH
462 {
463 *mode = L_8 | REG | direction;
464 *reg = (src[1] - '0') + 8;
465 return len;
466 }
7ee7b84d 467 if (len == 3 && TOLOWER (src[2]) == 'h')
252b5132
RH
468 {
469 *mode = L_8 | REG | direction;
470 *reg = (src[1] - '0');
471 return len;
472 }
473 if (len == 2)
474 {
475 *mode = L_16 | REG | direction;
476 *reg = (src[1] - '0');
477 return len;
478 }
479 }
480 }
481
482 return 0;
483}
484
1b680e4f
RS
485
486/* Parse an immediate or address-related constant and store it in OP.
487 If the user also specifies the operand's size, store that size
488 in OP->MODE, otherwise leave it for later code to decide. */
489
40f09f82 490static char *
b54a3392 491parse_exp (char *src, struct h8_op *op)
252b5132 492{
1b680e4f 493 char *save;
252b5132 494
1b680e4f
RS
495 save = input_line_pointer;
496 input_line_pointer = src;
497 expression (&op->exp);
498 if (op->exp.X_op == O_absent)
252b5132 499 as_bad (_("missing operand"));
1b680e4f 500 src = input_line_pointer;
252b5132 501 input_line_pointer = save;
1b680e4f
RS
502
503 return skip_colonthing (src, &op->mode);
252b5132
RH
504}
505
1b680e4f
RS
506
507/* If SRC starts with an explicit operand size, skip it and store the size
508 in *MODE. Leave *MODE unchanged otherwise. */
509
252b5132 510static char *
b54a3392 511skip_colonthing (char *src, int *mode)
252b5132 512{
1b680e4f 513 if (*src == ':')
252b5132 514 {
1b680e4f 515 src++;
252b5132 516 *mode &= ~SIZE;
1b680e4f 517 if (src[0] == '8' && !ISDIGIT (src[1]))
7ee7b84d 518 *mode |= L_8;
1b680e4f 519 else if (src[0] == '2' && !ISDIGIT (src[1]))
7ee7b84d 520 *mode |= L_2;
1b680e4f 521 else if (src[0] == '3' && !ISDIGIT (src[1]))
7ee7b84d 522 *mode |= L_3;
1b680e4f 523 else if (src[0] == '4' && !ISDIGIT (src[1]))
7ee7b84d 524 *mode |= L_4;
1b680e4f 525 else if (src[0] == '5' && !ISDIGIT (src[1]))
7ee7b84d 526 *mode |= L_5;
1b680e4f 527 else if (src[0] == '2' && src[1] == '4' && !ISDIGIT (src[2]))
7ee7b84d 528 *mode |= L_24;
1b680e4f 529 else if (src[0] == '3' && src[1] == '2' && !ISDIGIT (src[2]))
7ee7b84d 530 *mode |= L_32;
1b680e4f 531 else if (src[0] == '1' && src[1] == '6' && !ISDIGIT (src[2]))
7ee7b84d 532 *mode |= L_16;
252b5132 533 else
7ee7b84d
MS
534 as_bad (_("invalid operand size requested"));
535
1b680e4f
RS
536 while (ISDIGIT (*src))
537 src++;
252b5132 538 }
1b680e4f 539 return src;
252b5132
RH
540}
541
542/* The many forms of operand:
543
544 Rn Register direct
545 @Rn Register indirect
546 @(exp[:16], Rn) Register indirect with displacement
547 @Rn+
548 @-Rn
70d6ecf3
AM
549 @aa:8 absolute 8 bit
550 @aa:16 absolute 16 bit
252b5132
RH
551 @aa absolute 16 bit
552
553 #xx[:size] immediate data
70d6ecf3 554 @(exp:[8], pc) pc rel
3048287a 555 @@aa[:8] memory indirect. */
252b5132 556
7ee7b84d 557static int
df7b86aa 558constant_fits_width_p (struct h8_op *operand, offsetT width)
7ee7b84d 559{
35a35807
AM
560 offsetT num;
561
562 num = ((operand->exp.X_add_number & 0xffffffff) ^ 0x80000000) - 0x80000000;
563 return (num & ~width) == 0 || (num | width) == ~0;
7ee7b84d
MS
564}
565
566static int
b54a3392 567constant_fits_size_p (struct h8_op *operand, int size, int no_symbols)
7ee7b84d 568{
35a35807
AM
569 offsetT num;
570
7ee7b84d
MS
571 if (no_symbols
572 && (operand->exp.X_add_symbol != 0 || operand->exp.X_op_symbol != 0))
573 return 0;
35a35807 574 num = operand->exp.X_add_number & 0xffffffff;
7ee7b84d
MS
575 switch (size)
576 {
577 case L_2:
578 return (num & ~3) == 0;
579 case L_3:
580 return (num & ~7) == 0;
581 case L_3NZ:
582 return num >= 1 && num < 8;
583 case L_4:
584 return (num & ~15) == 0;
585 case L_5:
586 return num >= 1 && num < 32;
587 case L_8:
35a35807
AM
588 num = (num ^ 0x80000000) - 0x80000000;
589 return (num & ~0xFF) == 0 || (num | 0x7F) == ~0;
7ee7b84d
MS
590 case L_8U:
591 return (num & ~0xFF) == 0;
592 case L_16:
35a35807
AM
593 num = (num ^ 0x80000000) - 0x80000000;
594 return (num & ~0xFFFF) == 0 || (num | 0x7FFF) == ~0;
7ee7b84d
MS
595 case L_16U:
596 return (num & ~0xFFFF) == 0;
597 case L_32:
598 return 1;
599 default:
600 abort ();
601 }
602}
603
252b5132 604static void
b54a3392 605get_operand (char **ptr, struct h8_op *op, int direction)
252b5132
RH
606{
607 char *src = *ptr;
608 op_type mode;
609 unsigned int num;
610 unsigned int len;
611
7ee7b84d 612 op->mode = 0;
252b5132 613
3048287a
NC
614 /* Check for '(' and ')' for instructions ldm and stm. */
615 if (src[0] == '(' && src[8] == ')')
616 ++ src;
617
252b5132
RH
618 /* Gross. Gross. ldm and stm have a format not easily handled
619 by get_operand. We deal with it explicitly here. */
7ee7b84d
MS
620 if (TOLOWER (src[0]) == 'e' && TOLOWER (src[1]) == 'r' &&
621 ISDIGIT (src[2]) && src[3] == '-' &&
622 TOLOWER (src[4]) == 'e' && TOLOWER (src[5]) == 'r' && ISDIGIT (src[6]))
252b5132
RH
623 {
624 int low, high;
625
626 low = src[2] - '0';
627 high = src[6] - '0';
628
4892e510
NC
629 /* Check register pair's validity as per tech note TN-H8*-193A/E
630 from Renesas for H8S and H8SX hardware manual. */
631 if ( !(low == 0 && (high == 1 || high == 2 || high == 3))
632 && !(low == 1 && (high == 2 || high == 3 || high == 4) && SXmode)
633 && !(low == 2 && (high == 3 || ((high == 4 || high == 5) && SXmode)))
634 && !(low == 3 && (high == 4 || high == 5 || high == 6) && SXmode)
635 && !(low == 4 && (high == 5 || high == 6))
b4f16abb 636 && !(low == 4 && high == 7 && SXmode)
4892e510
NC
637 && !(low == 5 && (high == 6 || high == 7) && SXmode)
638 && !(low == 6 && high == 7 && SXmode))
252b5132
RH
639 as_bad (_("Invalid register list for ldm/stm\n"));
640
252b5132
RH
641 /* Even sicker. We encode two registers into op->reg. One
642 for the low register to save, the other for the high
643 register to save; we also set the high bit in op->reg
644 so we know this is "very special". */
645 op->reg = 0x80000000 | (high << 8) | low;
646 op->mode = REG;
3048287a
NC
647 if (src[7] == ')')
648 *ptr = src + 8;
649 else
650 *ptr = src + 7;
252b5132
RH
651 return;
652 }
653
654 len = parse_reg (src, &op->mode, &op->reg, direction);
655 if (len)
656 {
7ee7b84d
MS
657 src += len;
658 if (*src == '.')
659 {
660 int size = op->mode & SIZE;
661 switch (src[1])
662 {
663 case 'l': case 'L':
664 if (size != L_32)
665 as_warn (_("mismatch between register and suffix"));
666 op->mode = (op->mode & ~MODE) | LOWREG;
667 break;
668 case 'w': case 'W':
669 if (size != L_32 && size != L_16)
670 as_warn (_("mismatch between register and suffix"));
671 op->mode = (op->mode & ~MODE) | LOWREG;
672 op->mode = (op->mode & ~SIZE) | L_16;
673 break;
674 case 'b': case 'B':
675 op->mode = (op->mode & ~MODE) | LOWREG;
676 if (size != L_32 && size != L_8)
677 as_warn (_("mismatch between register and suffix"));
678 op->mode = (op->mode & ~MODE) | LOWREG;
679 op->mode = (op->mode & ~SIZE) | L_8;
680 break;
681 default:
20203fb9 682 as_warn (_("invalid suffix after register."));
7ee7b84d
MS
683 break;
684 }
685 src += 2;
686 }
687 *ptr = src;
252b5132
RH
688 return;
689 }
690
691 if (*src == '@')
692 {
693 src++;
694 if (*src == '@')
695 {
1b680e4f 696 *ptr = parse_exp (src + 1, op);
7ee7b84d
MS
697 if (op->exp.X_add_number >= 0x100)
698 {
d4e2de6b 699 int divisor = 1;
7ee7b84d
MS
700
701 op->mode = VECIND;
702 /* FIXME : 2? or 4? */
703 if (op->exp.X_add_number >= 0x400)
704 as_bad (_("address too high for vector table jmp/jsr"));
705 else if (op->exp.X_add_number >= 0x200)
706 divisor = 4;
707 else
708 divisor = 2;
709
710 op->exp.X_add_number = op->exp.X_add_number / divisor - 0x80;
711 }
712 else
713 op->mode = MEMIND;
252b5132 714 return;
252b5132
RH
715 }
716
7ee7b84d 717 if (*src == '-' || *src == '+')
252b5132 718 {
1b680e4f 719 len = parse_reg (src + 1, &mode, &num, direction);
252b5132
RH
720 if (len == 0)
721 {
70d6ecf3 722 /* Oops, not a reg after all, must be ordinary exp. */
1b680e4f
RS
723 op->mode = ABS | direction;
724 *ptr = parse_exp (src, op);
252b5132 725 return;
252b5132
RH
726 }
727
d4e2de6b
NC
728 if (((mode & SIZE) != PSIZE)
729 /* For Normal mode accept 16 bit and 32 bit pointer registers. */
730 && (!Nmode || ((mode & SIZE) != L_32)))
252b5132 731 as_bad (_("Wrong size pointer register for architecture."));
1b680e4f
RS
732
733 op->mode = src[0] == '-' ? RDPREDEC : RDPREINC;
252b5132 734 op->reg = num;
1b680e4f 735 *ptr = src + 1 + len;
252b5132
RH
736 return;
737 }
738 if (*src == '(')
739 {
252b5132
RH
740 src++;
741
7ee7b84d
MS
742 /* See if this is @(ERn.x, PC). */
743 len = parse_reg (src, &mode, &op->reg, direction);
744 if (len != 0 && (mode & MODE) == REG && src[len] == '.')
745 {
746 switch (TOLOWER (src[len + 1]))
747 {
748 case 'b':
749 mode = PCIDXB | direction;
750 break;
751 case 'w':
752 mode = PCIDXW | direction;
753 break;
754 case 'l':
755 mode = PCIDXL | direction;
756 break;
757 default:
758 mode = 0;
759 break;
760 }
761 if (mode
762 && src[len + 2] == ','
763 && TOLOWER (src[len + 3]) != 'p'
764 && TOLOWER (src[len + 4]) != 'c'
765 && src[len + 5] != ')')
766 {
767 *ptr = src + len + 6;
768 op->mode |= mode;
769 return;
770 }
771 /* Fall through into disp case - the grammar is somewhat
772 ambiguous, so we should try whether it's a DISP operand
773 after all ("ER3.L" might be a poorly named label...). */
774 }
775
776 /* Disp. */
777
70d6ecf3 778 /* Start off assuming a 16 bit offset. */
252b5132 779
1b680e4f 780 src = parse_exp (src, op);
252b5132
RH
781 if (*src == ')')
782 {
252b5132 783 op->mode |= ABS | direction;
1b680e4f 784 *ptr = src + 1;
252b5132
RH
785 return;
786 }
787
788 if (*src != ',')
789 {
790 as_bad (_("expected @(exp, reg16)"));
791 return;
252b5132
RH
792 }
793 src++;
794
795 len = parse_reg (src, &mode, &op->reg, direction);
7ee7b84d 796 if (len == 0 || (mode & MODE) != REG)
252b5132
RH
797 {
798 as_bad (_("expected @(exp, reg16)"));
799 return;
800 }
252b5132 801 src += len;
7ee7b84d
MS
802 if (src[0] == '.')
803 {
804 switch (TOLOWER (src[1]))
805 {
806 case 'b':
807 op->mode |= INDEXB | direction;
808 break;
809 case 'w':
810 op->mode |= INDEXW | direction;
811 break;
812 case 'l':
813 op->mode |= INDEXL | direction;
814 break;
815 default:
816 as_bad (_("expected .L, .W or .B for register in indexed addressing mode"));
817 }
818 src += 2;
819 op->reg &= 7;
820 }
821 else
822 op->mode |= DISP | direction;
1b680e4f 823 src = skip_colonthing (src, &op->mode);
252b5132
RH
824
825 if (*src != ')' && '(')
826 {
827 as_bad (_("expected @(exp, reg16)"));
828 return;
829 }
830 *ptr = src + 1;
252b5132
RH
831 return;
832 }
833 len = parse_reg (src, &mode, &num, direction);
834
835 if (len)
836 {
837 src += len;
7ee7b84d 838 if (*src == '+' || *src == '-')
252b5132 839 {
d4e2de6b
NC
840 if (((mode & SIZE) != PSIZE)
841 /* For Normal mode accept 16 bit and 32 bit pointer registers. */
842 && (!Nmode || ((mode & SIZE) != L_32)))
252b5132 843 as_bad (_("Wrong size pointer register for architecture."));
7ee7b84d 844 op->mode = *src == '+' ? RSPOSTINC : RSPOSTDEC;
252b5132 845 op->reg = num;
7ee7b84d 846 src++;
252b5132
RH
847 *ptr = src;
848 return;
849 }
d4e2de6b
NC
850 if (((mode & SIZE) != PSIZE)
851 /* For Normal mode accept 16 bit and 32 bit pointer registers. */
852 && (!Nmode || ((mode & SIZE) != L_32)))
252b5132
RH
853 as_bad (_("Wrong size pointer register for architecture."));
854
855 op->mode = direction | IND | PSIZE;
856 op->reg = num;
857 *ptr = src;
858
859 return;
860 }
861 else
862 {
863 /* must be a symbol */
864
865 op->mode = ABS | direction;
1b680e4f 866 *ptr = parse_exp (src, op);
252b5132
RH
867 return;
868 }
869 }
870
252b5132
RH
871 if (*src == '#')
872 {
252b5132 873 op->mode = IMM;
1b680e4f 874 *ptr = parse_exp (src + 1, op);
252b5132
RH
875 return;
876 }
7ee7b84d
MS
877 else if (strncmp (src, "mach", 4) == 0 ||
878 strncmp (src, "macl", 4) == 0 ||
879 strncmp (src, "MACH", 4) == 0 ||
880 strncmp (src, "MACL", 4) == 0)
252b5132 881 {
7ee7b84d 882 op->reg = TOLOWER (src[3]) == 'l';
252b5132
RH
883 op->mode = MACREG;
884 *ptr = src + 4;
885 return;
886 }
887 else
888 {
1b680e4f
RS
889 op->mode = PCREL;
890 *ptr = parse_exp (src, op);
252b5132
RH
891 }
892}
893
70d6ecf3 894static char *
b54a3392 895get_operands (unsigned int noperands, char *op_end, struct h8_op *operand)
252b5132
RH
896{
897 char *ptr = op_end;
898
899 switch (noperands)
900 {
901 case 0:
252b5132
RH
902 break;
903
904 case 1:
905 ptr++;
7ee7b84d 906 get_operand (&ptr, operand + 0, SRC);
252b5132
RH
907 if (*ptr == ',')
908 {
909 ptr++;
7ee7b84d 910 get_operand (&ptr, operand + 1, DST);
252b5132 911 }
252b5132 912 break;
70d6ecf3 913
252b5132
RH
914 case 2:
915 ptr++;
7ee7b84d 916 get_operand (&ptr, operand + 0, SRC);
252b5132
RH
917 if (*ptr == ',')
918 ptr++;
7ee7b84d
MS
919 get_operand (&ptr, operand + 1, DST);
920 break;
921
922 case 3:
923 ptr++;
924 get_operand (&ptr, operand + 0, SRC);
925 if (*ptr == ',')
926 ptr++;
927 get_operand (&ptr, operand + 1, DST);
928 if (*ptr == ',')
929 ptr++;
930 get_operand (&ptr, operand + 2, OP3);
252b5132
RH
931 break;
932
933 default:
934 abort ();
935 }
936
252b5132
RH
937 return ptr;
938}
939
7ee7b84d
MS
940/* MOVA has special requirements. Rather than adding twice the amount of
941 addressing modes, we simply special case it a bit. */
942static void
943get_mova_operands (char *op_end, struct h8_op *operand)
944{
945 char *ptr = op_end;
946
947 if (ptr[1] != '@' || ptr[2] != '(')
948 goto error;
949 ptr += 3;
950 operand[0].mode = 0;
1b680e4f 951 ptr = parse_exp (ptr, &operand[0]);
7ee7b84d
MS
952
953 if (*ptr !=',')
954 goto error;
955 ptr++;
956 get_operand (&ptr, operand + 1, DST);
957
958 if (*ptr =='.')
959 {
960 ptr++;
961 switch (*ptr++)
962 {
963 case 'b': case 'B':
964 operand[0].mode = (operand[0].mode & ~MODE) | INDEXB;
965 break;
966 case 'w': case 'W':
967 operand[0].mode = (operand[0].mode & ~MODE) | INDEXW;
968 break;
969 case 'l': case 'L':
970 operand[0].mode = (operand[0].mode & ~MODE) | INDEXL;
971 break;
972 default:
973 goto error;
974 }
975 }
976 else if ((operand[1].mode & MODE) == LOWREG)
977 {
978 switch (operand[1].mode & SIZE)
979 {
980 case L_8:
981 operand[0].mode = (operand[0].mode & ~MODE) | INDEXB;
982 break;
983 case L_16:
984 operand[0].mode = (operand[0].mode & ~MODE) | INDEXW;
985 break;
986 case L_32:
987 operand[0].mode = (operand[0].mode & ~MODE) | INDEXL;
988 break;
989 default:
990 goto error;
991 }
992 }
993 else
994 goto error;
995
996 if (*ptr++ != ')' || *ptr++ != ',')
997 goto error;
998 get_operand (&ptr, operand + 2, OP3);
999 /* See if we can use the short form of MOVA. */
1000 if (((operand[1].mode & MODE) == REG || (operand[1].mode & MODE) == LOWREG)
1001 && (operand[2].mode & MODE) == REG
1002 && (operand[1].reg & 7) == (operand[2].reg & 7))
1003 {
1004 operand[1].mode = operand[2].mode = 0;
1005 operand[0].reg = operand[2].reg & 7;
1006 }
1007 return;
1008
1009 error:
1010 as_bad (_("expected valid addressing mode for mova: \"@(disp, ea.sz),ERn\""));
7ee7b84d
MS
1011}
1012
1013static void
1014get_rtsl_operands (char *ptr, struct h8_op *operand)
1015{
2132e3a3
AM
1016 int mode, len, type = 0;
1017 unsigned int num, num2;
7ee7b84d
MS
1018
1019 ptr++;
1020 if (*ptr == '(')
1021 {
1022 ptr++;
1023 type = 1;
1024 }
1025 len = parse_reg (ptr, &mode, &num, SRC);
1026 if (len == 0 || (mode & MODE) != REG)
1027 {
1028 as_bad (_("expected register"));
1029 return;
1030 }
0613284f
RS
1031 ptr += len;
1032 if (*ptr == '-')
7ee7b84d 1033 {
0613284f 1034 len = parse_reg (++ptr, &mode, &num2, SRC);
7ee7b84d
MS
1035 if (len == 0 || (mode & MODE) != REG)
1036 {
1037 as_bad (_("expected register"));
1038 return;
1039 }
1040 ptr += len;
7ee7b84d
MS
1041 /* CONST_xxx are used as placeholders in the opcode table. */
1042 num = num2 - num;
2132e3a3 1043 if (num > 3)
7ee7b84d
MS
1044 {
1045 as_bad (_("invalid register list"));
1046 return;
1047 }
1048 }
1049 else
1050 num2 = num, num = 0;
0613284f
RS
1051 if (type == 1 && *ptr++ != ')')
1052 {
1053 as_bad (_("expected closing paren"));
1054 return;
1055 }
7ee7b84d
MS
1056 operand[0].mode = RS32;
1057 operand[1].mode = RD32;
1058 operand[0].reg = num;
1059 operand[1].reg = num2;
1060}
1061
252b5132
RH
1062/* Passed a pointer to a list of opcodes which use different
1063 addressing modes, return the opcode which matches the opcodes
70d6ecf3 1064 provided. */
3048287a 1065
a720f7bc 1066static const struct h8_instruction *
b54a3392
KH
1067get_specific (const struct h8_instruction *instruction,
1068 struct h8_op *operands, int size)
252b5132 1069{
a720f7bc 1070 const struct h8_instruction *this_try = instruction;
7ee7b84d 1071 const struct h8_instruction *found_other = 0, *found_mismatched = 0;
252b5132 1072 int found = 0;
a720f7bc 1073 int this_index = instruction->idx;
7ee7b84d 1074 int noperands = 0;
252b5132
RH
1075
1076 /* There's only one ldm/stm and it's easier to just
1077 get out quick for them. */
7ee7b84d
MS
1078 if (OP_KIND (instruction->opcode->how) == O_LDM
1079 || OP_KIND (instruction->opcode->how) == O_STM)
252b5132
RH
1080 return this_try;
1081
7ee7b84d
MS
1082 while (noperands < 3 && operands[noperands].mode != 0)
1083 noperands++;
1084
a720f7bc 1085 while (this_index == instruction->idx && !found)
252b5132 1086 {
7ee7b84d 1087 int this_size;
252b5132 1088
7ee7b84d 1089 found = 1;
a720f7bc 1090 this_try = instruction++;
7ee7b84d 1091 this_size = this_try->opcode->how & SN;
252b5132 1092
7ee7b84d
MS
1093 if (this_try->noperands != noperands)
1094 found = 0;
1095 else if (this_try->noperands > 0)
252b5132 1096 {
3048287a 1097 int i;
252b5132
RH
1098
1099 for (i = 0; i < this_try->noperands && found; i++)
1100 {
a720f7bc 1101 op_type op = this_try->opcode->args.nib[i];
7ee7b84d
MS
1102 int op_mode = op & MODE;
1103 int op_size = op & SIZE;
252b5132 1104 int x = operands[i].mode;
7ee7b84d
MS
1105 int x_mode = x & MODE;
1106 int x_size = x & SIZE;
252b5132 1107
7ee7b84d 1108 if (op_mode == LOWREG && (x_mode == REG || x_mode == LOWREG))
252b5132 1109 {
7ee7b84d
MS
1110 if ((x_size == L_8 && (operands[i].reg & 8) == 0)
1111 || (x_size == L_16 && (operands[i].reg & 8) == 8))
1112 as_warn (_("can't use high part of register in operand %d"), i);
1113
1114 if (x_size != op_size)
1115 found = 0;
252b5132 1116 }
7ee7b84d 1117 else if (op_mode == REG)
252b5132 1118 {
7ee7b84d
MS
1119 if (x_mode == LOWREG)
1120 x_mode = REG;
1121 if (x_mode != REG)
252b5132
RH
1122 found = 0;
1123
7ee7b84d
MS
1124 if (x_size == L_P)
1125 x_size = (Hmode ? L_32 : L_16);
1126 if (op_size == L_P)
1127 op_size = (Hmode ? L_32 : L_16);
252b5132 1128
70d6ecf3 1129 /* The size of the reg is v important. */
7ee7b84d 1130 if (op_size != x_size)
252b5132
RH
1131 found = 0;
1132 }
7ee7b84d 1133 else if (op_mode & CTRL) /* control register */
252b5132 1134 {
7ee7b84d
MS
1135 if (!(x_mode & CTRL))
1136 found = 0;
1137
1138 switch (x_mode)
1139 {
1140 case CCR:
1141 if (op_mode != CCR &&
1142 op_mode != CCR_EXR &&
1143 op_mode != CC_EX_VB_SB)
1144 found = 0;
1145 break;
1146 case EXR:
1147 if (op_mode != EXR &&
1148 op_mode != CCR_EXR &&
1149 op_mode != CC_EX_VB_SB)
1150 found = 0;
1151 break;
1152 case MACH:
1153 if (op_mode != MACH &&
1154 op_mode != MACREG)
1155 found = 0;
1156 break;
1157 case MACL:
1158 if (op_mode != MACL &&
1159 op_mode != MACREG)
1160 found = 0;
1161 break;
1162 case VBR:
1163 if (op_mode != VBR &&
1164 op_mode != VBR_SBR &&
1165 op_mode != CC_EX_VB_SB)
1166 found = 0;
1167 break;
1168 case SBR:
1169 if (op_mode != SBR &&
1170 op_mode != VBR_SBR &&
1171 op_mode != CC_EX_VB_SB)
1172 found = 0;
1173 break;
1174 }
1175 }
1176 else if ((op & ABSJMP) && (x_mode == ABS || x_mode == PCREL))
1177 {
1178 operands[i].mode &= ~MODE;
252b5132 1179 operands[i].mode |= ABSJMP;
70d6ecf3 1180 /* But it may not be 24 bits long. */
7ee7b84d 1181 if (x_mode == ABS && !Hmode)
252b5132
RH
1182 {
1183 operands[i].mode &= ~SIZE;
1184 operands[i].mode |= L_16;
1185 }
7ee7b84d
MS
1186 if ((operands[i].mode & SIZE) == L_32
1187 && (op_mode & SIZE) != L_32)
1188 found = 0;
252b5132 1189 }
7ee7b84d 1190 else if (x_mode == IMM && op_mode != IMM)
252b5132 1191 {
35a35807 1192 offsetT num = operands[i].exp.X_add_number & 0xffffffff;
7ee7b84d
MS
1193 if (op_mode == KBIT || op_mode == DBIT)
1194 /* This is ok if the immediate value is sensible. */;
1195 else if (op_mode == CONST_2)
1196 found = num == 2;
1197 else if (op_mode == CONST_4)
1198 found = num == 4;
1199 else if (op_mode == CONST_8)
1200 found = num == 8;
1201 else if (op_mode == CONST_16)
1202 found = num == 16;
1203 else
1204 found = 0;
252b5132 1205 }
7ee7b84d 1206 else if (op_mode == PCREL && op_mode == x_mode)
252b5132 1207 {
ba18dd6f 1208 /* movsd, bsr/bc and bsr/bs only come in PCREL16 flavour:
7ee7b84d 1209 If x_size is L_8, promote it. */
ba18dd6f
AO
1210 if (OP_KIND (this_try->opcode->how) == O_MOVSD
1211 || OP_KIND (this_try->opcode->how) == O_BSRBC
1212 || OP_KIND (this_try->opcode->how) == O_BSRBS)
7ee7b84d
MS
1213 if (x_size == L_8)
1214 x_size = L_16;
1215
70d6ecf3 1216 /* The size of the displacement is important. */
7ee7b84d 1217 if (op_size != x_size)
252b5132
RH
1218 found = 0;
1219 }
7ee7b84d
MS
1220 else if ((op_mode == DISP || op_mode == IMM || op_mode == ABS
1221 || op_mode == INDEXB || op_mode == INDEXW
1222 || op_mode == INDEXL)
1223 && op_mode == x_mode)
252b5132
RH
1224 {
1225 /* Promote a L_24 to L_32 if it makes us match. */
7ee7b84d 1226 if (x_size == L_24 && op_size == L_32)
252b5132 1227 {
7ee7b84d
MS
1228 x &= ~SIZE;
1229 x |= x_size = L_32;
252b5132 1230 }
7ee7b84d 1231
7ee7b84d 1232 if (((x_size == L_16 && op_size == L_16U)
2d0d09ca 1233 || (x_size == L_8 && op_size == L_8U)
7ee7b84d
MS
1234 || (x_size == L_3 && op_size == L_3NZ))
1235 /* We're deliberately more permissive for ABS modes. */
1236 && (op_mode == ABS
1237 || constant_fits_size_p (operands + i, op_size,
1238 op & NO_SYMBOLS)))
1239 x_size = op_size;
1240
1241 if (x_size != 0 && op_size != x_size)
1242 found = 0;
1243 else if (x_size == 0
1244 && ! constant_fits_size_p (operands + i, op_size,
1245 op & NO_SYMBOLS))
252b5132
RH
1246 found = 0;
1247 }
7ee7b84d 1248 else if (op_mode != x_mode)
252b5132
RH
1249 {
1250 found = 0;
70d6ecf3 1251 }
252b5132
RH
1252 }
1253 }
7ee7b84d
MS
1254 if (found)
1255 {
1256 if ((this_try->opcode->available == AV_H8SX && ! SXmode)
d4ea8842 1257 || (this_try->opcode->available == AV_H8S && ! Smode)
7ee7b84d
MS
1258 || (this_try->opcode->available == AV_H8H && ! Hmode))
1259 found = 0, found_other = this_try;
1260 else if (this_size != size && (this_size != SN && size != SN))
1261 found_mismatched = this_try, found = 0;
1262
1263 }
252b5132
RH
1264 }
1265 if (found)
1266 return this_try;
7ee7b84d
MS
1267 if (found_other)
1268 {
1269 as_warn (_("Opcode `%s' with these operand types not available in %s mode"),
1270 found_other->opcode->name,
1271 (! Hmode && ! Smode ? "H8/300"
1272 : SXmode ? "H8sx"
1273 : Smode ? "H8/300S"
1274 : "H8/300H"));
1275 }
1276 else if (found_mismatched)
1277 {
1278 as_warn (_("mismatch between opcode size and operand size"));
1279 return found_mismatched;
1280 }
1281 return 0;
252b5132
RH
1282}
1283
1284static void
b54a3392 1285check_operand (struct h8_op *operand, unsigned int width, char *string)
252b5132
RH
1286{
1287 if (operand->exp.X_add_symbol == 0
1288 && operand->exp.X_op_symbol == 0)
1289 {
70d6ecf3
AM
1290 /* No symbol involved, let's look at offset, it's dangerous if
1291 any of the high bits are not 0 or ff's, find out by oring or
1292 anding with the width and seeing if the answer is 0 or all
1293 fs. */
252b5132 1294
7ee7b84d 1295 if (! constant_fits_width_p (operand, width))
252b5132 1296 {
70d6ecf3 1297 if (width == 255
252b5132
RH
1298 && (operand->exp.X_add_number & 0xff00) == 0xff00)
1299 {
1300 /* Just ignore this one - which happens when trying to
1301 fit a 16 bit address truncated into an 8 bit address
1302 of something like bset. */
1303 }
166e23f9
KH
1304 else if (strcmp (string, "@") == 0
1305 && width == 0xffff
1306 && (operand->exp.X_add_number & 0xff8000) == 0xff8000)
1307 {
1308 /* Just ignore this one - which happens when trying to
1309 fit a 24 bit address truncated into a 16 bit address
1310 of something like mov.w. */
1311 }
70d6ecf3 1312 else
252b5132
RH
1313 {
1314 as_warn (_("operand %s0x%lx out of range."), string,
1315 (unsigned long) operand->exp.X_add_number);
1316 }
1317 }
1318 }
252b5132
RH
1319}
1320
1321/* RELAXMODE has one of 3 values:
1322
1323 0 Output a "normal" reloc, no relaxing possible for this insn/reloc
1324
1325 1 Output a relaxable 24bit absolute mov.w address relocation
1326 (may relax into a 16bit absolute address).
1327
1328 2 Output a relaxable 16/24 absolute mov.b address relocation
1329 (may relax into an 8bit absolute address). */
1330
1331static void
bcb012d3 1332do_a_fix_imm (int offset, int nibble, struct h8_op *operand, int relaxmode, const struct h8_instruction *this_try)
252b5132
RH
1333{
1334 int idx;
1335 int size;
1336 int where;
7ee7b84d 1337 char *bytes = frag_now->fr_literal + offset;
252b5132 1338
7ee7b84d 1339 char *t = ((operand->mode & MODE) == IMM) ? "#" : "@";
252b5132
RH
1340
1341 if (operand->exp.X_add_symbol == 0)
1342 {
252b5132
RH
1343 switch (operand->mode & SIZE)
1344 {
1345 case L_2:
1346 check_operand (operand, 0x3, t);
7ee7b84d 1347 bytes[0] |= (operand->exp.X_add_number & 3) << (nibble ? 0 : 4);
252b5132
RH
1348 break;
1349 case L_3:
7ee7b84d 1350 case L_3NZ:
252b5132 1351 check_operand (operand, 0x7, t);
7ee7b84d
MS
1352 bytes[0] |= (operand->exp.X_add_number & 7) << (nibble ? 0 : 4);
1353 break;
1354 case L_4:
1355 check_operand (operand, 0xF, t);
1356 bytes[0] |= (operand->exp.X_add_number & 15) << (nibble ? 0 : 4);
1357 break;
1358 case L_5:
1359 check_operand (operand, 0x1F, t);
1360 bytes[0] |= operand->exp.X_add_number & 31;
252b5132
RH
1361 break;
1362 case L_8:
7ee7b84d 1363 case L_8U:
252b5132 1364 check_operand (operand, 0xff, t);
7ee7b84d 1365 bytes[0] |= operand->exp.X_add_number;
252b5132
RH
1366 break;
1367 case L_16:
7ee7b84d 1368 case L_16U:
252b5132 1369 check_operand (operand, 0xffff, t);
7ee7b84d
MS
1370 bytes[0] |= operand->exp.X_add_number >> 8;
1371 bytes[1] |= operand->exp.X_add_number >> 0;
bcb012d3
DD
1372#ifdef OBJ_ELF
1373 /* MOVA needs both relocs to relax the second operand properly. */
1374 if (relaxmode != 0
1375 && (OP_KIND(this_try->opcode->how) == O_MOVAB
1376 || OP_KIND(this_try->opcode->how) == O_MOVAW
1377 || OP_KIND(this_try->opcode->how) == O_MOVAL))
1378 {
1379 idx = BFD_RELOC_16;
1380 fix_new_exp (frag_now, offset, 2, &operand->exp, 0, idx);
1381 }
1382#endif
252b5132
RH
1383 break;
1384 case L_24:
1385 check_operand (operand, 0xffffff, t);
7ee7b84d
MS
1386 bytes[0] |= operand->exp.X_add_number >> 16;
1387 bytes[1] |= operand->exp.X_add_number >> 8;
1388 bytes[2] |= operand->exp.X_add_number >> 0;
252b5132
RH
1389 break;
1390
1391 case L_32:
70d6ecf3 1392 /* This should be done with bfd. */
7ee7b84d
MS
1393 bytes[0] |= operand->exp.X_add_number >> 24;
1394 bytes[1] |= operand->exp.X_add_number >> 16;
1395 bytes[2] |= operand->exp.X_add_number >> 8;
1396 bytes[3] |= operand->exp.X_add_number >> 0;
4132022d
AM
1397 if (relaxmode != 0)
1398 {
1399 idx = (relaxmode == 2) ? R_MOV24B1 : R_MOVL1;
1400 fix_new_exp (frag_now, offset, 4, &operand->exp, 0, idx);
1401 }
252b5132
RH
1402 break;
1403 }
252b5132
RH
1404 }
1405 else
1406 {
1407 switch (operand->mode & SIZE)
1408 {
252b5132
RH
1409 case L_24:
1410 case L_32:
1411 size = 4;
1412 where = (operand->mode & SIZE) == L_24 ? -1 : 0;
1413 if (relaxmode == 2)
1414 idx = R_MOV24B1;
1415 else if (relaxmode == 1)
1416 idx = R_MOVL1;
1417 else
1418 idx = R_RELLONG;
1419 break;
1420 default:
70d6ecf3 1421 as_bad (_("Can't work out size of operand.\n"));
252b5132 1422 case L_16:
7ee7b84d 1423 case L_16U:
252b5132
RH
1424 size = 2;
1425 where = 0;
1426 if (relaxmode == 2)
1427 idx = R_MOV16B1;
1428 else
1429 idx = R_RELWORD;
4132022d
AM
1430 operand->exp.X_add_number =
1431 ((operand->exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
7ee7b84d 1432 operand->exp.X_add_number |= (bytes[0] << 8) | bytes[1];
252b5132
RH
1433 break;
1434 case L_8:
1435 size = 1;
1436 where = 0;
1437 idx = R_RELBYTE;
4132022d
AM
1438 operand->exp.X_add_number =
1439 ((operand->exp.X_add_number & 0xff) ^ 0x80) - 0x80;
7ee7b84d 1440 operand->exp.X_add_number |= bytes[0];
252b5132
RH
1441 }
1442
1443 fix_new_exp (frag_now,
1444 offset + where,
1445 size,
1446 &operand->exp,
1447 0,
1448 idx);
1449 }
252b5132
RH
1450}
1451
70d6ecf3 1452/* Now we know what sort of opcodes it is, let's build the bytes. */
3048287a 1453
252b5132 1454static void
b54a3392 1455build_bytes (const struct h8_instruction *this_try, struct h8_op *operand)
252b5132 1456{
3048287a 1457 int i;
252b5132 1458 char *output = frag_more (this_try->length);
d1e50f8a 1459 const op_type *nibble_ptr = this_try->opcode->data.nib;
252b5132
RH
1460 op_type c;
1461 unsigned int nibble_count = 0;
7ee7b84d 1462 int op_at[3];
3048287a 1463 int nib = 0;
252b5132 1464 int movb = 0;
7ee7b84d 1465 char asnibbles[100];
252b5132 1466 char *p = asnibbles;
7ee7b84d 1467 int high, low;
252b5132 1468
d4ea8842 1469 if (!Hmode && this_try->opcode->available != AV_H8)
252b5132 1470 as_warn (_("Opcode `%s' with these operand types not available in H8/300 mode"),
a720f7bc 1471 this_try->opcode->name);
d4ea8842
MS
1472 else if (!Smode
1473 && this_try->opcode->available != AV_H8
1474 && this_try->opcode->available != AV_H8H)
1475 as_warn (_("Opcode `%s' with these operand types not available in H8/300H mode"),
1476 this_try->opcode->name);
1477 else if (!SXmode
1478 && this_try->opcode->available != AV_H8
1479 && this_try->opcode->available != AV_H8H
1480 && this_try->opcode->available != AV_H8S)
1481 as_warn (_("Opcode `%s' with these operand types not available in H8/300S mode"),
1482 this_try->opcode->name);
252b5132 1483
7ee7b84d 1484 while (*nibble_ptr != (op_type) E)
252b5132
RH
1485 {
1486 int d;
7ee7b84d
MS
1487
1488 nib = 0;
252b5132
RH
1489 c = *nibble_ptr++;
1490
7ee7b84d 1491 d = (c & OP3) == OP3 ? 2 : (c & DST) == DST ? 1 : 0;
252b5132
RH
1492
1493 if (c < 16)
3048287a 1494 nib = c;
252b5132
RH
1495 else
1496 {
7ee7b84d
MS
1497 int c2 = c & MODE;
1498
1499 if (c2 == REG || c2 == LOWREG
1500 || c2 == IND || c2 == PREINC || c2 == PREDEC
1501 || c2 == POSTINC || c2 == POSTDEC)
1502 {
1503 nib = operand[d].reg;
1504 if (c2 == LOWREG)
1505 nib &= 7;
1506 }
1507
1508 else if (c & CTRL) /* Control reg operand. */
3048287a
NC
1509 nib = operand[d].reg;
1510
252b5132 1511 else if ((c & DISPREG) == (DISPREG))
7ee7b84d
MS
1512 {
1513 nib = operand[d].reg;
1514 }
1515 else if (c2 == ABS)
252b5132
RH
1516 {
1517 operand[d].mode = c;
7ee7b84d 1518 op_at[d] = nibble_count;
252b5132
RH
1519 nib = 0;
1520 }
7ee7b84d
MS
1521 else if (c2 == IMM || c2 == PCREL || c2 == ABS
1522 || (c & ABSJMP) || c2 == DISP)
252b5132
RH
1523 {
1524 operand[d].mode = c;
7ee7b84d 1525 op_at[d] = nibble_count;
252b5132
RH
1526 nib = 0;
1527 }
7ee7b84d 1528 else if ((c & IGNORE) || (c & DATA))
3048287a
NC
1529 nib = 0;
1530
7ee7b84d 1531 else if (c2 == DBIT)
252b5132
RH
1532 {
1533 switch (operand[0].exp.X_add_number)
1534 {
1535 case 1:
1536 nib = c;
1537 break;
1538 case 2:
1539 nib = 0x8 | c;
1540 break;
1541 default:
1542 as_bad (_("Need #1 or #2 here"));
1543 }
1544 }
7ee7b84d 1545 else if (c2 == KBIT)
252b5132
RH
1546 {
1547 switch (operand[0].exp.X_add_number)
1548 {
1549 case 1:
1550 nib = 0;
1551 break;
1552 case 2:
1553 nib = 8;
1554 break;
1555 case 4:
1556 if (!Hmode)
1557 as_warn (_("#4 not valid on H8/300."));
1558 nib = 9;
1559 break;
1560
1561 default:
1562 as_bad (_("Need #1 or #2 here"));
1563 break;
1564 }
70d6ecf3 1565 /* Stop it making a fix. */
252b5132
RH
1566 operand[0].mode = 0;
1567 }
1568
1569 if (c & MEMRELAX)
3048287a 1570 operand[d].mode |= MEMRELAX;
252b5132
RH
1571
1572 if (c & B31)
3048287a 1573 nib |= 0x8;
252b5132 1574
7ee7b84d
MS
1575 if (c & B21)
1576 nib |= 0x4;
1577
1578 if (c & B11)
1579 nib |= 0x2;
1580
1581 if (c & B01)
1582 nib |= 0x1;
1583
1584 if (c2 == MACREG)
252b5132 1585 {
f0c56b90
NC
1586 if (operand[0].mode == MACREG)
1587 /* stmac has mac[hl] as the first operand. */
1588 nib = 2 + operand[0].reg;
1589 else
1590 /* ldmac has mac[hl] as the second operand. */
1591 nib = 2 + operand[1].reg;
252b5132
RH
1592 }
1593 }
1594 nibble_count++;
1595
1596 *p++ = nib;
1597 }
1598
1599 /* Disgusting. Why, oh why didn't someone ask us for advice
1600 on the assembler format. */
7ee7b84d 1601 if (OP_KIND (this_try->opcode->how) == O_LDM)
252b5132 1602 {
7ee7b84d
MS
1603 high = (operand[1].reg >> 8) & 0xf;
1604 low = (operand[1].reg) & 0xf;
252b5132 1605 asnibbles[2] = high - low;
7ee7b84d
MS
1606 asnibbles[7] = high;
1607 }
1608 else if (OP_KIND (this_try->opcode->how) == O_STM)
1609 {
1610 high = (operand[0].reg >> 8) & 0xf;
1611 low = (operand[0].reg) & 0xf;
1612 asnibbles[2] = high - low;
1613 asnibbles[7] = low;
252b5132
RH
1614 }
1615
1616 for (i = 0; i < this_try->length; i++)
3048287a 1617 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
252b5132 1618
ca9a79a1
NC
1619 /* Note if this is a movb or a bit manipulation instruction
1620 there is a special relaxation which only applies. */
1621 if ( this_try->opcode->how == O (O_MOV, SB)
1622 || this_try->opcode->how == O (O_BCLR, SB)
1623 || this_try->opcode->how == O (O_BAND, SB)
1624 || this_try->opcode->how == O (O_BIAND, SB)
1625 || this_try->opcode->how == O (O_BILD, SB)
1626 || this_try->opcode->how == O (O_BIOR, SB)
1627 || this_try->opcode->how == O (O_BIST, SB)
1628 || this_try->opcode->how == O (O_BIXOR, SB)
1629 || this_try->opcode->how == O (O_BLD, SB)
1630 || this_try->opcode->how == O (O_BNOT, SB)
1631 || this_try->opcode->how == O (O_BOR, SB)
1632 || this_try->opcode->how == O (O_BSET, SB)
1633 || this_try->opcode->how == O (O_BST, SB)
1634 || this_try->opcode->how == O (O_BTST, SB)
1635 || this_try->opcode->how == O (O_BXOR, SB))
252b5132
RH
1636 movb = 1;
1637
70d6ecf3 1638 /* Output any fixes. */
7ee7b84d 1639 for (i = 0; i < this_try->noperands; i++)
252b5132
RH
1640 {
1641 int x = operand[i].mode;
7ee7b84d 1642 int x_mode = x & MODE;
252b5132 1643
7ee7b84d
MS
1644 if (x_mode == IMM || x_mode == DISP)
1645 do_a_fix_imm (output - frag_now->fr_literal + op_at[i] / 2,
bcb012d3
DD
1646 op_at[i] & 1, operand + i, (x & MEMRELAX) != 0,
1647 this_try);
3048287a 1648
7ee7b84d
MS
1649 else if (x_mode == ABS)
1650 do_a_fix_imm (output - frag_now->fr_literal + op_at[i] / 2,
1651 op_at[i] & 1, operand + i,
bcb012d3
DD
1652 (x & MEMRELAX) ? movb + 1 : 0,
1653 this_try);
3048287a 1654
7ee7b84d 1655 else if (x_mode == PCREL)
252b5132 1656 {
7ee7b84d 1657 int size16 = (x & SIZE) == L_16;
252b5132
RH
1658 int size = size16 ? 2 : 1;
1659 int type = size16 ? R_PCRWORD : R_PCRBYTE;
36ed2fff 1660 fixS *fixP;
252b5132
RH
1661
1662 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1663
1664 if (operand[i].exp.X_add_number & 1)
3048287a
NC
1665 as_warn (_("branch operand has odd offset (%lx)\n"),
1666 (unsigned long) operand->exp.X_add_number);
36ed2fff
JL
1667#ifndef OBJ_ELF
1668 /* The COFF port has always been off by one, changing it
1669 now would be an incompatible change, so we leave it as-is.
1670
1671 We don't want to do this for ELF as we want to be
1672 compatible with the proposed ELF format from Hitachi. */
252b5132 1673 operand[i].exp.X_add_number -= 1;
36ed2fff 1674#endif
7ee7b84d
MS
1675 if (size16)
1676 {
1677 operand[i].exp.X_add_number =
1678 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1679 }
1680 else
1681 {
1682 operand[i].exp.X_add_number =
1683 ((operand[i].exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1684 }
1685
1686 /* For BRA/S. */
1687 if (! size16)
1688 operand[i].exp.X_add_number |= output[op_at[i] / 2];
252b5132 1689
36ed2fff 1690 fixP = fix_new_exp (frag_now,
7ee7b84d 1691 output - frag_now->fr_literal + op_at[i] / 2,
36ed2fff
JL
1692 size,
1693 &operand[i].exp,
1694 1,
1695 type);
1696 fixP->fx_signed = 1;
252b5132 1697 }
7ee7b84d 1698 else if (x_mode == MEMIND)
252b5132 1699 {
252b5132
RH
1700 check_operand (operand + i, 0xff, "@@");
1701 fix_new_exp (frag_now,
1702 output - frag_now->fr_literal + 1,
1703 1,
1704 &operand[i].exp,
1705 0,
1706 R_MEM_INDIRECT);
1707 }
7ee7b84d
MS
1708 else if (x_mode == VECIND)
1709 {
1710 check_operand (operand + i, 0x7f, "@@");
1711 /* FIXME: approximating the effect of "B31" here...
1712 This is very hackish, and ought to be done a better way. */
1713 operand[i].exp.X_add_number |= 0x80;
1714 fix_new_exp (frag_now,
1715 output - frag_now->fr_literal + 1,
1716 1,
1717 &operand[i].exp,
1718 0,
1719 R_MEM_INDIRECT);
1720 }
252b5132
RH
1721 else if (x & ABSJMP)
1722 {
3c1ba8a3 1723 int where = 0;
7ee7b84d 1724 bfd_reloc_code_real_type reloc_type = R_JMPL1;
3c1ba8a3
JL
1725
1726#ifdef OBJ_ELF
1727 /* To be compatible with the proposed H8 ELF format, we
1728 want the relocation's offset to point to the first byte
1729 that will be modified, not to the start of the instruction. */
7ee7b84d
MS
1730
1731 if ((operand->mode & SIZE) == L_32)
1732 {
1733 where = 2;
1734 reloc_type = R_RELLONG;
1735 }
1736 else
1737 where = 1;
3c1ba8a3 1738#endif
de342d07 1739
70d6ecf3 1740 /* This jmp may be a jump or a branch. */
252b5132 1741
7ee7b84d
MS
1742 check_operand (operand + i,
1743 SXmode ? 0xffffffff : Hmode ? 0xffffff : 0xffff,
1744 "@");
3048287a 1745
252b5132 1746 if (operand[i].exp.X_add_number & 1)
3048287a
NC
1747 as_warn (_("branch operand has odd offset (%lx)\n"),
1748 (unsigned long) operand->exp.X_add_number);
1749
252b5132 1750 if (!Hmode)
70d6ecf3 1751 operand[i].exp.X_add_number =
4132022d 1752 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
252b5132 1753 fix_new_exp (frag_now,
3c1ba8a3 1754 output - frag_now->fr_literal + where,
252b5132
RH
1755 4,
1756 &operand[i].exp,
1757 0,
7ee7b84d 1758 reloc_type);
252b5132
RH
1759 }
1760 }
252b5132
RH
1761}
1762
70d6ecf3
AM
1763/* Try to give an intelligent error message for common and simple to
1764 detect errors. */
3048287a 1765
252b5132 1766static void
b54a3392
KH
1767clever_message (const struct h8_instruction *instruction,
1768 struct h8_op *operand)
252b5132 1769{
70d6ecf3 1770 /* Find out if there was more than one possible opcode. */
252b5132 1771
a720f7bc 1772 if ((instruction + 1)->idx != instruction->idx)
252b5132 1773 {
3048287a 1774 int argn;
252b5132 1775
70d6ecf3
AM
1776 /* Only one opcode of this flavour, try to guess which operand
1777 didn't match. */
a720f7bc 1778 for (argn = 0; argn < instruction->noperands; argn++)
252b5132 1779 {
a720f7bc 1780 switch (instruction->opcode->args.nib[argn])
252b5132
RH
1781 {
1782 case RD16:
1783 if (operand[argn].mode != RD16)
1784 {
1785 as_bad (_("destination operand must be 16 bit register"));
1786 return;
1787
1788 }
1789 break;
1790
1791 case RS8:
252b5132
RH
1792 if (operand[argn].mode != RS8)
1793 {
1794 as_bad (_("source operand must be 8 bit register"));
1795 return;
1796 }
1797 break;
1798
1799 case ABS16DST:
1800 if (operand[argn].mode != ABS16DST)
1801 {
1802 as_bad (_("destination operand must be 16bit absolute address"));
1803 return;
1804 }
1805 break;
1806 case RD8:
1807 if (operand[argn].mode != RD8)
1808 {
1809 as_bad (_("destination operand must be 8 bit register"));
1810 return;
1811 }
1812 break;
1813
252b5132
RH
1814 case ABS16SRC:
1815 if (operand[argn].mode != ABS16SRC)
1816 {
1817 as_bad (_("source operand must be 16bit absolute address"));
1818 return;
1819 }
1820 break;
1821
1822 }
1823 }
1824 }
1825 as_bad (_("invalid operands"));
1826}
1827
d4ea8842 1828
1b680e4f
RS
1829/* If OPERAND is part of an address, adjust its size and value given
1830 that it addresses SIZE bytes.
d4ea8842 1831
1b680e4f
RS
1832 This function decides how big non-immediate constants are when no
1833 size was explicitly given. It also scales down the assembly-level
d4ea8842
MS
1834 displacement in an @(d:2,ERn) operand. */
1835
1836static void
b54a3392 1837fix_operand_size (struct h8_op *operand, int size)
d4ea8842 1838{
1b680e4f 1839 if (SXmode && (operand->mode & MODE) == DISP)
d4ea8842
MS
1840 {
1841 /* If the user didn't specify an operand width, see if we
1842 can use @(d:2,ERn). */
1b680e4f
RS
1843 if ((operand->mode & SIZE) == 0
1844 && operand->exp.X_add_symbol == 0
1845 && operand->exp.X_op_symbol == 0
d4ea8842
MS
1846 && (operand->exp.X_add_number == size
1847 || operand->exp.X_add_number == size * 2
1848 || operand->exp.X_add_number == size * 3))
1849 operand->mode |= L_2;
1850
1851 /* Scale down the displacement in an @(d:2,ERn) operand.
1852 X_add_number then contains the desired field value. */
1853 if ((operand->mode & SIZE) == L_2)
1854 {
1855 if (operand->exp.X_add_number % size != 0)
1856 as_warn (_("operand/size mis-match"));
1857 operand->exp.X_add_number /= size;
1858 }
1859 }
1860
d4ea8842
MS
1861 if ((operand->mode & SIZE) == 0)
1862 switch (operand->mode & MODE)
1863 {
1864 case DISP:
1865 case INDEXB:
1866 case INDEXW:
1867 case INDEXL:
1868 case ABS:
1b680e4f
RS
1869 /* Pick a 24-bit address unless we know that a 16-bit address
1870 is safe. get_specific() will relax L_24 into L_32 where
1871 necessary. */
1872 if (Hmode
d4e2de6b 1873 && !Nmode
35a35807
AM
1874 && ((((addressT) operand->exp.X_add_number + 0x8000)
1875 & 0xffffffff) > 0xffff
1b680e4f
RS
1876 || operand->exp.X_add_symbol != 0
1877 || operand->exp.X_op_symbol != 0))
1878 operand->mode |= L_24;
1879 else
1880 operand->mode |= L_16;
1881 break;
1882
1883 case PCREL:
35a35807
AM
1884 if ((((addressT) operand->exp.X_add_number + 0x80)
1885 & 0xffffffff) <= 0xff)
cc189afc
DD
1886 {
1887 if (operand->exp.X_add_symbol != NULL)
1888 operand->mode |= bsize;
1889 else
1890 operand->mode |= L_8;
1891 }
1b680e4f
RS
1892 else
1893 operand->mode |= L_16;
d4ea8842
MS
1894 break;
1895 }
1896}
1897
1898
70d6ecf3
AM
1899/* This is the guts of the machine-dependent assembler. STR points to
1900 a machine dependent instruction. This function is supposed to emit
1901 the frags/bytes it assembles. */
3048287a 1902
252b5132 1903void
b54a3392 1904md_assemble (char *str)
252b5132
RH
1905{
1906 char *op_start;
1907 char *op_end;
7ee7b84d 1908 struct h8_op operand[3];
a720f7bc
KD
1909 const struct h8_instruction *instruction;
1910 const struct h8_instruction *prev_instruction;
252b5132
RH
1911
1912 char *dot = 0;
0c0b9be0 1913 char *slash = 0;
252b5132 1914 char c;
7ee7b84d 1915 int size, i;
252b5132 1916
70d6ecf3 1917 /* Drop leading whitespace. */
252b5132
RH
1918 while (*str == ' ')
1919 str++;
1920
70d6ecf3 1921 /* Find the op code end. */
252b5132
RH
1922 for (op_start = op_end = str;
1923 *op_end != 0 && *op_end != ' ';
1924 op_end++)
1925 {
1926 if (*op_end == '.')
1927 {
1928 dot = op_end + 1;
1929 *op_end = 0;
1930 op_end += 2;
1931 break;
1932 }
0c0b9be0
AO
1933 else if (*op_end == '/' && ! slash)
1934 slash = op_end;
252b5132
RH
1935 }
1936
252b5132
RH
1937 if (op_end == op_start)
1938 {
1939 as_bad (_("can't find opcode "));
1940 }
1941 c = *op_end;
1942
1943 *op_end = 0;
1944
0c0b9be0
AO
1945 /* The assembler stops scanning the opcode at slashes, so it fails
1946 to make characters following them lower case. Fix them. */
1947 if (slash)
1948 while (*++slash)
1949 *slash = TOLOWER (*slash);
1950
a720f7bc
KD
1951 instruction = (const struct h8_instruction *)
1952 hash_find (opcode_hash_control, op_start);
252b5132 1953
a720f7bc 1954 if (instruction == NULL)
252b5132
RH
1955 {
1956 as_bad (_("unknown opcode"));
1957 return;
1958 }
1959
70d6ecf3 1960 /* We used to set input_line_pointer to the result of get_operands,
252b5132
RH
1961 but that is wrong. Our caller assumes we don't change it. */
1962
7ee7b84d
MS
1963 operand[0].mode = 0;
1964 operand[1].mode = 0;
1965 operand[2].mode = 0;
1966
1967 if (OP_KIND (instruction->opcode->how) == O_MOVAB
1968 || OP_KIND (instruction->opcode->how) == O_MOVAW
1969 || OP_KIND (instruction->opcode->how) == O_MOVAL)
1970 get_mova_operands (op_end, operand);
1971 else if (OP_KIND (instruction->opcode->how) == O_RTEL
1972 || OP_KIND (instruction->opcode->how) == O_RTSL)
1973 get_rtsl_operands (op_end, operand);
1974 else
1975 get_operands (instruction->noperands, op_end, operand);
1976
252b5132 1977 *op_end = c;
a720f7bc 1978 prev_instruction = instruction;
252b5132 1979
4892e510
NC
1980 /* Now we have operands from instruction.
1981 Let's check them out for ldm and stm. */
1982 if (OP_KIND (instruction->opcode->how) == O_LDM)
1983 {
1984 /* The first operand must be @er7+, and the
1985 second operand must be a register pair. */
1986 if ((operand[0].mode != RSINC)
1987 || (operand[0].reg != 7)
1988 || ((operand[1].reg & 0x80000000) == 0))
1989 as_bad (_("invalid operand in ldm"));
1990 }
1991 else if (OP_KIND (instruction->opcode->how) == O_STM)
1992 {
1993 /* The first operand must be a register pair,
1994 and the second operand must be @-er7. */
1995 if (((operand[0].reg & 0x80000000) == 0)
1996 || (operand[1].mode != RDDEC)
1997 || (operand[1].reg != 7))
1998 as_bad (_("invalid operand in stm"));
1999 }
2000
252b5132
RH
2001 size = SN;
2002 if (dot)
2003 {
0c0b9be0 2004 switch (TOLOWER (*dot))
252b5132
RH
2005 {
2006 case 'b':
2007 size = SB;
2008 break;
2009
2010 case 'w':
2011 size = SW;
2012 break;
2013
2014 case 'l':
2015 size = SL;
2016 break;
2017 }
2018 }
7ee7b84d
MS
2019 if (OP_KIND (instruction->opcode->how) == O_MOVAB ||
2020 OP_KIND (instruction->opcode->how) == O_MOVAW ||
2021 OP_KIND (instruction->opcode->how) == O_MOVAL)
252b5132 2022 {
d4ea8842
MS
2023 switch (operand[0].mode & MODE)
2024 {
7ee7b84d
MS
2025 case INDEXB:
2026 default:
d4ea8842 2027 fix_operand_size (&operand[1], 1);
7ee7b84d
MS
2028 break;
2029 case INDEXW:
d4ea8842 2030 fix_operand_size (&operand[1], 2);
7ee7b84d
MS
2031 break;
2032 case INDEXL:
d4ea8842 2033 fix_operand_size (&operand[1], 4);
7ee7b84d 2034 break;
252b5132
RH
2035 }
2036 }
7ee7b84d
MS
2037 else
2038 {
d4ea8842
MS
2039 for (i = 0; i < 3 && operand[i].mode != 0; i++)
2040 switch (size)
2041 {
7ee7b84d
MS
2042 case SN:
2043 case SB:
2044 default:
d4ea8842 2045 fix_operand_size (&operand[i], 1);
7ee7b84d
MS
2046 break;
2047 case SW:
d4ea8842 2048 fix_operand_size (&operand[i], 2);
7ee7b84d
MS
2049 break;
2050 case SL:
d4ea8842 2051 fix_operand_size (&operand[i], 4);
7ee7b84d
MS
2052 break;
2053 }
2054 }
252b5132 2055
d4ea8842
MS
2056 instruction = get_specific (instruction, operand, size);
2057
2058 if (instruction == 0)
2059 {
2060 /* Couldn't find an opcode which matched the operands. */
2061 char *where = frag_more (2);
2062
2063 where[0] = 0x0;
2064 where[1] = 0x0;
2065 clever_message (prev_instruction, operand);
2066
2067 return;
2068 }
2069
a720f7bc 2070 build_bytes (instruction, operand);
2c8714f2 2071
2c8714f2 2072 dwarf2_emit_insn (instruction->length);
252b5132
RH
2073}
2074
2075symbolS *
b54a3392 2076md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
252b5132
RH
2077{
2078 return 0;
2079}
2080
499ac353 2081/* Various routines to kill one day. */
bc0d738a 2082
252b5132 2083char *
b54a3392 2084md_atof (int type, char *litP, int *sizeP)
252b5132 2085{
499ac353 2086 return ieee_md_atof (type, litP, sizeP, TRUE);
252b5132
RH
2087}
2088\f
6fd4f6cc
DD
2089#define OPTION_H_TICK_HEX (OPTION_MD_BASE)
2090
5a38dc70 2091const char *md_shortopts = "";
252b5132 2092struct option md_longopts[] = {
6fd4f6cc 2093 { "h-tick-hex", no_argument, NULL, OPTION_H_TICK_HEX },
252b5132
RH
2094 {NULL, no_argument, NULL, 0}
2095};
70d6ecf3
AM
2096
2097size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
2098
2099int
b54a3392 2100md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
252b5132 2101{
6fd4f6cc
DD
2102 switch (c)
2103 {
2104 case OPTION_H_TICK_HEX:
2105 enable_h_tick_hex = 1;
2106 break;
2107
2108 default:
2109 return 0;
2110 }
2111 return 1;
252b5132
RH
2112}
2113
2114void
b54a3392 2115md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
252b5132
RH
2116{
2117}
2118\f
b54a3392 2119void tc_aout_fix_to_chars (void);
3048287a 2120
252b5132 2121void
b54a3392 2122tc_aout_fix_to_chars (void)
252b5132
RH
2123{
2124 printf (_("call to tc_aout_fix_to_chars \n"));
2125 abort ();
2126}
2127
2128void
7be1c489 2129md_convert_frag (bfd *headers ATTRIBUTE_UNUSED,
b54a3392
KH
2130 segT seg ATTRIBUTE_UNUSED,
2131 fragS *fragP ATTRIBUTE_UNUSED)
252b5132
RH
2132{
2133 printf (_("call to md_convert_frag \n"));
2134 abort ();
2135}
2136
3c1ba8a3 2137valueT
b54a3392 2138md_section_align (segT segment, valueT size)
3c1ba8a3
JL
2139{
2140 int align = bfd_get_section_alignment (stdoutput, segment);
2141 return ((size + (1 << align) - 1) & (-1 << align));
2142}
252b5132
RH
2143
2144void
55cf6793 2145md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132
RH
2146{
2147 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
a161fe53 2148 long val = *valP;
252b5132
RH
2149
2150 switch (fixP->fx_size)
2151 {
2152 case 1:
2153 *buf++ = val;
2154 break;
2155 case 2:
2156 *buf++ = (val >> 8);
2157 *buf++ = val;
2158 break;
2159 case 4:
2160 *buf++ = (val >> 24);
2161 *buf++ = (val >> 16);
2162 *buf++ = (val >> 8);
2163 *buf++ = val;
2164 break;
550c1888
NC
2165 case 8:
2166 /* This can arise when the .quad or .8byte pseudo-ops are used.
2167 Returning here (without setting fx_done) will cause the code
2168 to attempt to generate a reloc which will then fail with the
2169 slightly more helpful error message: "Cannot represent
2170 relocation type BFD_RELOC_64". */
2171 return;
252b5132
RH
2172 default:
2173 abort ();
2174 }
94f592af
NC
2175
2176 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
2177 fixP->fx_done = 1;
252b5132
RH
2178}
2179
2180int
c85dd50d
NC
2181md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
2182 segT segment_type ATTRIBUTE_UNUSED)
252b5132 2183{
c85dd50d 2184 printf (_("call to md_estimate_size_before_relax \n"));
252b5132
RH
2185 abort ();
2186}
2187
70d6ecf3 2188/* Put number into target byte order. */
252b5132 2189void
b54a3392 2190md_number_to_chars (char *ptr, valueT use, int nbytes)
252b5132
RH
2191{
2192 number_to_chars_bigendian (ptr, use, nbytes);
2193}
70d6ecf3 2194
252b5132 2195long
52b010e4 2196md_pcrel_from (fixS *fixp)
252b5132 2197{
52b010e4
NC
2198 as_bad_where (fixp->fx_file, fixp->fx_line,
2199 _("Unexpected reference to a symbol in a non-code section"));
2200 return 0;
252b5132
RH
2201}
2202
f333765f 2203arelent *
b54a3392 2204tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
f333765f
JL
2205{
2206 arelent *rel;
2207 bfd_reloc_code_real_type r_type;
2208
de342d07
JL
2209 if (fixp->fx_addsy && fixp->fx_subsy)
2210 {
2211 if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
2212 || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
2213 {
2214 as_bad_where (fixp->fx_file, fixp->fx_line,
c85dd50d 2215 _("Difference of symbols in different sections is not supported"));
de342d07
JL
2216 return NULL;
2217 }
2218 }
2219
c85dd50d
NC
2220 rel = xmalloc (sizeof (arelent));
2221 rel->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
f333765f
JL
2222 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2223 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2224 rel->addend = fixp->fx_offset;
2225
2226 r_type = fixp->fx_r_type;
2227
2228#define DEBUG 0
2229#if DEBUG
2230 fprintf (stderr, "%s\n", bfd_get_reloc_code_name (r_type));
c85dd50d 2231 fflush (stderr);
f333765f
JL
2232#endif
2233 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
2234 if (rel->howto == NULL)
2235 {
2236 as_bad_where (fixp->fx_file, fixp->fx_line,
2237 _("Cannot represent relocation type %s"),
2238 bfd_get_reloc_code_name (r_type));
2239 return NULL;
2240 }
2241
2242 return rel;
2243}
This page took 0.688728 seconds and 4 git commands to generate.