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