daily update
[deliverable/binutils-gdb.git] / gas / config / tc-z8k.c
CommitLineData
252b5132 1/* tc-z8k.c -- Assemble code for the Zilog Z800n
f87a1e0c
CG
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003,
3 2005 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
252b5132 21
e0c6ed95
AM
22/* Written By Steve Chamberlain <sac@cygnus.com>. */
23
252b5132
RH
24#define DEFINE_TABLE
25#include <stdio.h>
26
252b5132
RH
27#include "as.h"
28#include "bfd.h"
3882b010 29#include "safe-ctype.h"
a5d2034a 30#include "opcodes/z8k-opc.h"
252b5132 31
63a0b638
AM
32const char comment_chars[] = "!";
33const char line_comment_chars[] = "#";
34const char line_separator_chars[] = ";";
252b5132
RH
35
36extern int machine;
37extern int coff_flags;
38int segmented_mode;
252b5132 39
7f31df7c
CG
40/* This is non-zero if target was set from the command line. */
41static int z8k_target_from_cmdline;
42
78a33af2 43static void
464800ca 44s_segm (int segm)
252b5132 45{
7f31df7c
CG
46 if (segm)
47 {
d5bf5799
CG
48 segmented_mode = 1;
49 machine = bfd_mach_z8001;
50 coff_flags = F_Z8001;
7f31df7c
CG
51 }
52 else
53 {
d5bf5799
CG
54 segmented_mode = 0;
55 machine = bfd_mach_z8002;
56 coff_flags = F_Z8002;
7f31df7c 57 }
252b5132
RH
58}
59
e0c6ed95 60static void
464800ca 61even (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
62{
63 frag_align (1, 0, 0);
64 record_alignment (now_seg, 1);
65}
66
78a33af2 67static int
464800ca 68tohex (int c)
252b5132 69{
3882b010 70 if (ISDIGIT (c))
252b5132 71 return c - '0';
3882b010 72 if (ISLOWER (c))
252b5132
RH
73 return c - 'a' + 10;
74 return c - 'A' + 10;
75}
76
78a33af2 77static void
464800ca 78sval (int ignore ATTRIBUTE_UNUSED)
252b5132 79{
252b5132
RH
80 SKIP_WHITESPACE ();
81 if (*input_line_pointer == '\'')
82 {
83 int c;
84 input_line_pointer++;
85 c = *input_line_pointer++;
86 while (c != '\'')
87 {
88 if (c == '%')
89 {
90 c = (tohex (input_line_pointer[0]) << 4)
91 | tohex (input_line_pointer[1]);
92 input_line_pointer += 2;
93 }
94 FRAG_APPEND_1_CHAR (c);
95 c = *input_line_pointer++;
96 }
97 demand_empty_rest_of_line ();
98 }
252b5132 99}
e0c6ed95
AM
100
101/* This table describes all the machine specific pseudo-ops the assembler
102 has to support. The fields are:
103 pseudo-op name without dot
104 function to call to execute this pseudo-op
105 Integer arg to pass to the function
106 */
107
108const pseudo_typeS md_pseudo_table[] = {
109 {"int" , cons , 2},
110 {"data.b" , cons , 1},
111 {"data.w" , cons , 2},
112 {"data.l" , cons , 4},
113 {"form" , listing_psize , 0},
114 {"heading", listing_title , 0},
115 {"import" , s_ignore , 0},
116 {"page" , listing_eject , 0},
117 {"program", s_ignore , 0},
7f31df7c
CG
118 {"z8001" , s_segm , 1},
119 {"z8002" , s_segm , 0},
e0c6ed95 120
7f31df7c
CG
121 {"segm" , s_segm , 1},
122 {"unsegm" , s_segm , 0},
123 {"unseg" , s_segm , 0},
e0c6ed95
AM
124 {"name" , s_app_file , 0},
125 {"global" , s_globl , 0},
126 {"wval" , cons , 2},
127 {"lval" , cons , 4},
128 {"bval" , cons , 1},
129 {"sval" , sval , 0},
130 {"rsect" , obj_coff_section, 0},
131 {"sect" , obj_coff_section, 0},
132 {"block" , s_space , 0},
133 {"even" , even , 0},
134 {0 , 0 , 0}
252b5132
RH
135};
136
137const char EXP_CHARS[] = "eE";
138
e0c6ed95
AM
139/* Chars that mean this number is a floating point constant.
140 As in 0f12.456
141 or 0d1.2345e12 */
252b5132
RH
142const char FLT_CHARS[] = "rRsSfFdDxXpP";
143
e0c6ed95
AM
144/* Opcode mnemonics. */
145static struct hash_control *opcode_hash_control;
252b5132
RH
146
147void
464800ca 148md_begin (void)
252b5132 149{
78a33af2
AM
150 const opcode_entry_type *opcode;
151 int idx = -1;
252b5132
RH
152
153 opcode_hash_control = hash_new ();
154
155 for (opcode = z8k_table; opcode->name; opcode++)
156 {
e0c6ed95 157 /* Only enter unique codes into the table. */
78a33af2
AM
158 if (idx != opcode->idx)
159 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
160 idx = opcode->idx;
252b5132
RH
161 }
162
e0c6ed95 163 /* Default to z8002. */
7f31df7c
CG
164 if (! z8k_target_from_cmdline)
165 s_segm (0);
252b5132 166
e0c6ed95 167 /* Insert the pseudo ops, too. */
252b5132
RH
168 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
169 {
170 opcode_entry_type *fake_opcode;
171 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
879db8be
NC
172 fake_opcode->name = md_pseudo_table[idx].poc_name;
173 fake_opcode->func = (void *) (md_pseudo_table + idx);
252b5132
RH
174 fake_opcode->opcode = 250;
175 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
176 }
252b5132
RH
177}
178
19d63e5d 179typedef struct z8k_op {
f590b86e
CG
180 /* CLASS_REG_xxx. */
181 int regsize;
e0c6ed95
AM
182
183 /* 0 .. 15. */
184 unsigned int reg;
252b5132
RH
185
186 int mode;
187
e0c6ed95
AM
188 /* Any other register associated with the mode. */
189 unsigned int x_reg;
190
191 /* Any expression. */
192 expressionS exp;
19d63e5d 193} op_type;
252b5132
RH
194
195static expressionS *da_operand;
196static expressionS *imm_operand;
197
464800ca
CG
198static int reg[16];
199static int the_cc;
200static int the_ctrl;
201static int the_flags;
202static int the_interrupt;
78a33af2
AM
203
204static char *
2132e3a3 205whatreg (unsigned int *reg, char *src)
252b5132 206{
3882b010 207 if (ISDIGIT (src[1]))
252b5132
RH
208 {
209 *reg = (src[0] - '0') * 10 + src[1] - '0';
210 return src + 2;
211 }
212 else
213 {
214 *reg = (src[0] - '0');
215 return src + 1;
216 }
217}
218
e0c6ed95 219/* Parse operands
252b5132 220
e0c6ed95
AM
221 rh0-rh7, rl0-rl7
222 r0-r15
223 rr0-rr14
224 rq0--rq12
225 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
226 r0l,r0h,..r7l,r7h
227 @WREG
228 @WREG+
229 @-WREG
230 #const
231*/
252b5132 232
bc0d738a
NC
233/* Try to parse a reg name. Return a pointer to the first character
234 in SRC after the reg name. */
235
78a33af2 236static char *
464800ca 237parse_reg (char *src, int *mode, unsigned int *reg)
252b5132
RH
238{
239 char *res = 0;
240 char regno;
241
f69532ae
CG
242 /* Check for stack pointer "sp" alias. */
243 if ((src[0] == 's' || src[0] == 'S')
244 && (src[1] == 'p' || src[1] == 'P')
245 && (src[2] == 0 || src[2] == ','))
252b5132
RH
246 {
247 if (segmented_mode)
e0c6ed95
AM
248 {
249 *mode = CLASS_REG_LONG;
250 *reg = 14;
251 }
252b5132 252 else
e0c6ed95
AM
253 {
254 *mode = CLASS_REG_WORD;
255 *reg = 15;
256 }
252b5132
RH
257 return src + 2;
258 }
f69532ae
CG
259
260 if (src[0] == 'r' || src[0] == 'R')
252b5132 261 {
f69532ae 262 if (src[1] == 'r' || src[1] == 'R')
e0c6ed95 263 {
28bab82b
NC
264 if (src[2] < '0' || src[2] > '9')
265 return res; /* Assume no register name but a label starting with 'rr'. */
e0c6ed95
AM
266 *mode = CLASS_REG_LONG;
267 res = whatreg (reg, src + 2);
252b5132
RH
268 regno = *reg;
269 if (regno > 14)
f590b86e
CG
270 as_bad (_("register rr%d out of range"), regno);
271 if (regno & 1)
272 as_bad (_("register rr%d does not exist"), regno);
e0c6ed95 273 }
f69532ae 274 else if (src[1] == 'h' || src[1] == 'H')
e0c6ed95 275 {
28bab82b
NC
276 if (src[2] < '0' || src[2] > '9')
277 return res; /* Assume no register name but a label starting with 'rh'. */
e0c6ed95
AM
278 *mode = CLASS_REG_BYTE;
279 res = whatreg (reg, src + 2);
252b5132
RH
280 regno = *reg;
281 if (regno > 7)
f590b86e 282 as_bad (_("register rh%d out of range"), regno);
e0c6ed95 283 }
f69532ae 284 else if (src[1] == 'l' || src[1] == 'L')
e0c6ed95 285 {
28bab82b
NC
286 if (src[2] < '0' || src[2] > '9')
287 return res; /* Assume no register name but a label starting with 'rl'. */
e0c6ed95
AM
288 *mode = CLASS_REG_BYTE;
289 res = whatreg (reg, src + 2);
252b5132
RH
290 regno = *reg;
291 if (regno > 7)
f590b86e 292 as_bad (_("register rl%d out of range"), regno);
e0c6ed95
AM
293 *reg += 8;
294 }
f69532ae 295 else if (src[1] == 'q' || src[1] == 'Q')
e0c6ed95 296 {
28bab82b
NC
297 if (src[2] < '0' || src[2] > '9')
298 return res; /* Assume no register name but a label starting with 'rq'. */
e0c6ed95
AM
299 *mode = CLASS_REG_QUAD;
300 res = whatreg (reg, src + 2);
252b5132
RH
301 regno = *reg;
302 if (regno > 12)
f590b86e
CG
303 as_bad (_("register rq%d out of range"), regno);
304 if (regno & 3)
305 as_bad (_("register rq%d does not exist"), regno);
e0c6ed95 306 }
252b5132 307 else
e0c6ed95 308 {
28bab82b
NC
309 if (src[1] < '0' || src[1] > '9')
310 return res; /* Assume no register name but a label starting with 'r'. */
e0c6ed95
AM
311 *mode = CLASS_REG_WORD;
312 res = whatreg (reg, src + 1);
252b5132
RH
313 regno = *reg;
314 if (regno > 15)
f590b86e 315 as_bad (_("register r%d out of range"), regno);
e0c6ed95 316 }
252b5132
RH
317 }
318 return res;
252b5132
RH
319}
320
78a33af2 321static char *
464800ca 322parse_exp (char *s, expressionS *op)
252b5132
RH
323{
324 char *save = input_line_pointer;
325 char *new;
326
327 input_line_pointer = s;
328 expression (op);
329 if (op->X_op == O_absent)
330 as_bad (_("missing operand"));
331 new = input_line_pointer;
332 input_line_pointer = save;
333 return new;
334}
335
336/* The many forms of operand:
337
338 <rb>
339 <r>
340 <rr>
341 <rq>
342 @r
343 #exp
344 exp
345 exp(r)
346 r(#exp)
347 r(r)
252b5132
RH
348 */
349
e0c6ed95 350static char *
464800ca 351checkfor (char *ptr, char what)
252b5132
RH
352{
353 if (*ptr == what)
354 ptr++;
355 else
e0c6ed95
AM
356 as_bad (_("expected %c"), what);
357
252b5132
RH
358 return ptr;
359}
360
e0c6ed95
AM
361/* Make sure the mode supplied is the size of a word. */
362
252b5132 363static void
464800ca 364regword (int mode, char *string)
252b5132
RH
365{
366 int ok;
367
368 ok = CLASS_REG_WORD;
369 if (ok != mode)
370 {
371 as_bad (_("register is wrong size for a word %s"), string);
372 }
373}
374
e0c6ed95
AM
375/* Make sure the mode supplied is the size of an address. */
376
252b5132 377static void
464800ca 378regaddr (int mode, char *string)
252b5132
RH
379{
380 int ok;
381
382 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
383 if (ok != mode)
384 {
385 as_bad (_("register is wrong size for address %s"), string);
386 }
387}
388
19d63e5d 389struct ctrl_names {
e0c6ed95
AM
390 int value;
391 char *name;
252b5132
RH
392};
393
464800ca 394static struct ctrl_names ctrl_table[] = {
bb5737a7
CG
395 { 0x1, "flags" }, /* ldctlb only. */
396 { 0x2, "fcw" }, /* ldctl only. Applies to all remaining control registers. */
879db8be
NC
397 { 0x3, "refresh" },
398 { 0x4, "psapseg" },
399 { 0x5, "psapoff" },
400 { 0x5, "psap" },
401 { 0x6, "nspseg" },
402 { 0x7, "nspoff" },
403 { 0x7, "nsp" },
404 { 0 , 0 }
252b5132 405};
e0c6ed95 406
252b5132 407static void
464800ca 408get_ctrl_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
252b5132
RH
409{
410 char *src = *ptr;
f69532ae 411 int i, l;
252b5132
RH
412
413 while (*src == ' ')
414 src++;
415
416 mode->mode = CLASS_CTRL;
417 for (i = 0; ctrl_table[i].name; i++)
418 {
f69532ae
CG
419 l = strlen (ctrl_table[i].name);
420 if (! strncasecmp (ctrl_table[i].name, src, l))
421 {
422 the_ctrl = ctrl_table[i].value;
423 if (*(src + l) && *(src + l) != ',')
424 break;
425 *ptr = src + l; /* Valid control name found: "consume" it. */
426 return;
427 }
252b5132
RH
428 }
429 the_ctrl = 0;
252b5132
RH
430}
431
19d63e5d 432struct flag_names {
252b5132
RH
433 int value;
434 char *name;
252b5132
RH
435};
436
464800ca 437static struct flag_names flag_table[] = {
bb5737a7
CG
438 { 0x1, "P" },
439 { 0x1, "V" },
440 { 0x2, "S" },
441 { 0x4, "Z" },
442 { 0x8, "C" },
879db8be 443 { 0x0, "+" },
bb5737a7 444 { 0x0, "," },
879db8be 445 { 0, 0 }
252b5132
RH
446};
447
448static void
464800ca 449get_flags_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
252b5132
RH
450{
451 char *src = *ptr;
bb5737a7 452 char c;
252b5132
RH
453 int i;
454 int j;
455
456 while (*src == ' ')
457 src++;
458
459 mode->mode = CLASS_FLAGS;
460 the_flags = 0;
461 for (j = 0; j <= 9; j++)
462 {
e0c6ed95 463 if (!src[j])
252b5132 464 goto done;
bb5737a7 465 c = TOUPPER(src[j]);
e0c6ed95
AM
466 for (i = 0; flag_table[i].name; i++)
467 {
bb5737a7 468 if (flag_table[i].name[0] == c)
e0c6ed95
AM
469 {
470 the_flags = the_flags | flag_table[i].value;
471 goto match;
472 }
473 }
252b5132
RH
474 goto done;
475 match:
e0c6ed95 476 ;
252b5132 477 }
e0c6ed95 478 done:
252b5132 479 *ptr = src + j;
252b5132
RH
480}
481
19d63e5d 482struct interrupt_names {
252b5132
RH
483 int value;
484 char *name;
252b5132
RH
485};
486
464800ca 487static struct interrupt_names intr_table[] = {
879db8be
NC
488 { 0x1, "nvi" },
489 { 0x2, "vi" },
490 { 0x3, "both" },
491 { 0x3, "all" },
492 { 0, 0 }
252b5132
RH
493};
494
495static void
464800ca 496get_interrupt_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
252b5132
RH
497{
498 char *src = *ptr;
bb5737a7 499 int i, l;
252b5132
RH
500
501 while (*src == ' ')
502 src++;
503
504 mode->mode = CLASS_IMM;
bb5737a7 505 the_interrupt = 0;
252b5132 506
bb5737a7
CG
507 while (*src)
508 {
509 for (i = 0; intr_table[i].name; i++)
510 {
511 l = strlen (intr_table[i].name);
512 if (! strncasecmp (intr_table[i].name, src, l))
513 {
514 the_interrupt |= intr_table[i].value;
515 if (*(src + l) && *(src + l) != ',')
516 {
517 *ptr = src + l;
518 invalid:
519 as_bad (_("unknown interrupt %s"), src);
520 while (**ptr && ! is_end_of_line[(unsigned char) **ptr])
521 (*ptr)++; /* Consume rest of line. */
522 return;
523 }
524 src += l;
525 if (! *src)
526 {
527 *ptr = src;
528 return;
529 }
530 }
531 }
532 if (*src == ',')
533 src++;
534 else
e0c6ed95 535 {
bb5737a7
CG
536 *ptr = src;
537 goto invalid;
e0c6ed95 538 }
252b5132 539 }
bb5737a7 540
7f31df7c 541 /* No interrupt type specified, opcode won't do anything. */
f590b86e 542 as_warn (_("opcode has no effect"));
252b5132 543 the_interrupt = 0x0;
252b5132
RH
544}
545
19d63e5d 546struct cc_names {
252b5132
RH
547 int value;
548 char *name;
252b5132
RH
549};
550
464800ca 551static struct cc_names table[] = {
879db8be
NC
552 { 0x0, "f" },
553 { 0x1, "lt" },
554 { 0x2, "le" },
555 { 0x3, "ule" },
d5bf5799 556 { 0x4, "ov/pe" },
879db8be 557 { 0x4, "ov" },
d5bf5799 558 { 0x4, "pe/ov" },
879db8be
NC
559 { 0x4, "pe" },
560 { 0x5, "mi" },
561 { 0x6, "eq" },
562 { 0x6, "z" },
d5bf5799 563 { 0x7, "c/ult" },
879db8be 564 { 0x7, "c" },
d5bf5799 565 { 0x7, "ult/c" },
879db8be
NC
566 { 0x7, "ult" },
567 { 0x8, "t" },
568 { 0x9, "ge" },
569 { 0xa, "gt" },
570 { 0xb, "ugt" },
d5bf5799 571 { 0xc, "nov/po" },
879db8be 572 { 0xc, "nov" },
d5bf5799 573 { 0xc, "po/nov" },
879db8be
NC
574 { 0xc, "po" },
575 { 0xd, "pl" },
576 { 0xe, "ne" },
577 { 0xe, "nz" },
d5bf5799 578 { 0xf, "nc/uge" },
879db8be 579 { 0xf, "nc" },
d5bf5799 580 { 0xf, "uge/nc" },
879db8be
NC
581 { 0xf, "uge" },
582 { 0 , 0 }
252b5132
RH
583};
584
585static void
464800ca 586get_cc_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
252b5132
RH
587{
588 char *src = *ptr;
d5bf5799 589 int i, l;
252b5132
RH
590
591 while (*src == ' ')
592 src++;
593
594 mode->mode = CLASS_CC;
595 for (i = 0; table[i].name; i++)
596 {
d5bf5799
CG
597 l = strlen (table[i].name);
598 if (! strncasecmp (table[i].name, src, l))
599 {
600 the_cc = table[i].value;
601 if (*(src + l) && *(src + l) != ',')
602 break;
603 *ptr = src + l; /* Valid cc found: "consume" it. */
604 return;
605 }
252b5132 606 }
d5bf5799 607 the_cc = 0x8; /* Not recognizing the cc defaults to t. (Assuming no cc present.) */
252b5132
RH
608}
609
610static void
464800ca 611get_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
252b5132
RH
612{
613 char *src = *ptr;
614 char *end;
252b5132
RH
615
616 mode->mode = 0;
617
618 while (*src == ' ')
619 src++;
620 if (*src == '#')
621 {
622 mode->mode = CLASS_IMM;
623 imm_operand = &(mode->exp);
624 src = parse_exp (src + 1, &(mode->exp));
625 }
626 else if (*src == '@')
627 {
252b5132 628 mode->mode = CLASS_IR;
f590b86e 629 src = parse_reg (src + 1, &mode->regsize, &mode->reg);
252b5132
RH
630 }
631 else
632 {
2132e3a3 633 unsigned int regn;
252b5132
RH
634
635 end = parse_reg (src, &mode->mode, &regn);
636
637 if (end)
638 {
2132e3a3
AM
639 int nw;
640 unsigned int nr;
252b5132
RH
641
642 src = end;
643 if (*src == '(')
644 {
645 src++;
646 end = parse_reg (src, &nw, &nr);
647 if (end)
648 {
e0c6ed95 649 /* Got Ra(Rb). */
252b5132
RH
650 src = end;
651
652 if (*src != ')')
e0c6ed95 653 as_bad (_("Missing ) in ra(rb)"));
252b5132 654 else
e0c6ed95 655 src++;
252b5132
RH
656
657 regaddr (mode->mode, "ra(rb) ra");
252b5132
RH
658 mode->mode = CLASS_BX;
659 mode->reg = regn;
660 mode->x_reg = nr;
661 reg[ARG_RX] = nr;
662 }
663 else
664 {
e0c6ed95 665 /* Got Ra(disp). */
252b5132
RH
666 if (*src == '#')
667 src++;
668 src = parse_exp (src, &(mode->exp));
669 src = checkfor (src, ')');
670 mode->mode = CLASS_BA;
671 mode->reg = regn;
672 mode->x_reg = 0;
673 imm_operand = &(mode->exp);
674 }
675 }
676 else
677 {
678 mode->reg = regn;
679 mode->x_reg = 0;
680 }
681 }
682 else
683 {
e0c6ed95 684 /* No initial reg. */
252b5132
RH
685 src = parse_exp (src, &(mode->exp));
686 if (*src == '(')
687 {
688 src++;
689 end = parse_reg (src, &(mode->mode), &regn);
690 regword (mode->mode, "addr(Ra) ra");
691 mode->mode = CLASS_X;
692 mode->reg = regn;
693 mode->x_reg = 0;
694 da_operand = &(mode->exp);
695 src = checkfor (end, ')');
696 }
697 else
698 {
e0c6ed95 699 /* Just an address. */
252b5132
RH
700 mode->mode = CLASS_DA;
701 mode->reg = 0;
702 mode->x_reg = 0;
703 da_operand = &(mode->exp);
704 }
705 }
706 }
707 *ptr = src;
708}
709
e0c6ed95 710static char *
464800ca 711get_operands (const opcode_entry_type *opcode, char *op_end, op_type *operand)
252b5132
RH
712{
713 char *ptr = op_end;
e0c6ed95
AM
714 char *savptr;
715
252b5132
RH
716 switch (opcode->noperands)
717 {
718 case 0:
719 operand[0].mode = 0;
720 operand[1].mode = 0;
d5bf5799
CG
721 while (*ptr == ' ')
722 ptr++;
252b5132
RH
723 break;
724
725 case 1:
252b5132 726 if (opcode->arg_info[0] == CLASS_CC)
d5bf5799
CG
727 {
728 get_cc_operand (&ptr, operand + 0, 0);
729 while (*ptr == ' ')
730 ptr++;
731 if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
732 {
733 as_bad (_("invalid condition code '%s'"), ptr);
734 while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
735 ptr++; /* Consume rest of line. */
736 }
737 }
252b5132 738 else if (opcode->arg_info[0] == CLASS_FLAGS)
bb5737a7
CG
739 {
740 get_flags_operand (&ptr, operand + 0, 0);
741 while (*ptr == ' ')
742 ptr++;
743 if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
744 {
745 as_bad (_("invalid flag '%s'"), ptr);
746 while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
747 ptr++; /* Consume rest of line. */
748 }
749 }
e0c6ed95 750 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
14899840 751 get_interrupt_operand (&ptr, operand + 0, 0);
252b5132 752 else
14899840
NC
753 get_operand (&ptr, operand + 0, 0);
754
252b5132
RH
755 operand[1].mode = 0;
756 break;
757
758 case 2:
252b5132
RH
759 savptr = ptr;
760 if (opcode->arg_info[0] == CLASS_CC)
d5bf5799
CG
761 {
762 get_cc_operand (&ptr, operand + 0, 0);
763 while (*ptr == ' ')
764 ptr++;
765 if (*ptr != ',' && strchr (ptr + 1, ','))
766 {
767 savptr = ptr;
768 while (*ptr != ',')
769 ptr++;
770 *ptr = 0;
771 ptr++;
772 as_bad (_("invalid condition code '%s'"), savptr);
773 }
774 }
252b5132 775 else if (opcode->arg_info[0] == CLASS_CTRL)
e0c6ed95
AM
776 {
777 get_ctrl_operand (&ptr, operand + 0, 0);
14899840 778
e0c6ed95
AM
779 if (the_ctrl == 0)
780 {
781 ptr = savptr;
782 get_operand (&ptr, operand + 0, 0);
14899840 783
e0c6ed95 784 if (ptr == 0)
879db8be 785 return NULL;
e0c6ed95
AM
786 if (*ptr == ',')
787 ptr++;
788 get_ctrl_operand (&ptr, operand + 1, 1);
bb5737a7
CG
789 if (the_ctrl == 0)
790 return NULL;
e0c6ed95
AM
791 return ptr;
792 }
793 }
252b5132 794 else
14899840
NC
795 get_operand (&ptr, operand + 0, 0);
796
252b5132 797 if (ptr == 0)
879db8be 798 return NULL;
252b5132 799 if (*ptr == ',')
e0c6ed95 800 ptr++;
252b5132
RH
801 get_operand (&ptr, operand + 1, 1);
802 break;
803
804 case 3:
252b5132
RH
805 get_operand (&ptr, operand + 0, 0);
806 if (*ptr == ',')
807 ptr++;
808 get_operand (&ptr, operand + 1, 1);
809 if (*ptr == ',')
810 ptr++;
811 get_operand (&ptr, operand + 2, 2);
812 break;
813
814 case 4:
252b5132
RH
815 get_operand (&ptr, operand + 0, 0);
816 if (*ptr == ',')
817 ptr++;
818 get_operand (&ptr, operand + 1, 1);
819 if (*ptr == ',')
820 ptr++;
821 get_operand (&ptr, operand + 2, 2);
822 if (*ptr == ',')
823 ptr++;
824 get_cc_operand (&ptr, operand + 3, 3);
825 break;
e0c6ed95 826
252b5132
RH
827 default:
828 abort ();
829 }
830
831 return ptr;
832}
833
834/* Passed a pointer to a list of opcodes which use different
e0c6ed95
AM
835 addressing modes. Return the opcode which matches the opcodes
836 provided. */
252b5132 837
e0c6ed95 838static opcode_entry_type *
464800ca 839get_specific (opcode_entry_type *opcode, op_type *operands)
252b5132
RH
840{
841 opcode_entry_type *this_try = opcode;
842 int found = 0;
843 unsigned int noperands = opcode->noperands;
844
879db8be 845 int this_index = opcode->idx;
252b5132
RH
846
847 while (this_index == opcode->idx && !found)
848 {
849 unsigned int i;
850
851 this_try = opcode++;
852 for (i = 0; i < noperands; i++)
853 {
879db8be 854 unsigned int mode = operands[i].mode;
252b5132 855
f590b86e
CG
856 if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO))
857 {
858 mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO;
859 }
860
252b5132
RH
861 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
862 {
c03099e6 863 /* It could be a pc rel operand, if this is a da mode
e0c6ed95 864 and we like disps, then insert it. */
252b5132
RH
865
866 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
867 {
e0c6ed95 868 /* This is the case. */
252b5132
RH
869 operands[i].mode = CLASS_DISP;
870 }
871 else if (mode == CLASS_BA && this_try->arg_info[i])
872 {
e0c6ed95
AM
873 /* Can't think of a way to turn what we've been
874 given into something that's OK. */
252b5132
RH
875 goto fail;
876 }
877 else if (this_try->arg_info[i] & CLASS_PR)
878 {
879 if (mode == CLASS_REG_LONG && segmented_mode)
880 {
e0c6ed95 881 /* OK. */
252b5132
RH
882 }
883 else if (mode == CLASS_REG_WORD && !segmented_mode)
884 {
e0c6ed95 885 /* OK. */
252b5132
RH
886 }
887 else
888 goto fail;
889 }
890 else
891 goto fail;
892 }
893 switch (mode & CLASS_MASK)
894 {
895 default:
896 break;
f590b86e
CG
897 case CLASS_IRO:
898 if (operands[i].regsize != CLASS_REG_WORD)
899 as_bad (_("invalid indirect register size"));
900 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
901 break;
252b5132 902 case CLASS_IR:
f590b86e
CG
903 if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG)
904 || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD))
905 as_bad (_("invalid indirect register size"));
906 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
907 break;
908 case CLASS_X:
252b5132
RH
909 case CLASS_BA:
910 case CLASS_BX:
911 case CLASS_DISP:
912 case CLASS_REG:
913 case CLASS_REG_WORD:
914 case CLASS_REG_BYTE:
915 case CLASS_REG_QUAD:
916 case CLASS_REG_LONG:
917 case CLASS_REGN0:
918 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
919 break;
bb5737a7
CG
920 case CLASS_CTRL:
921 if (this_try->opcode == OPC_ldctlb && the_ctrl != 1)
922 as_bad (_("invalid control register name"));
923 break;
252b5132
RH
924 }
925 }
926
927 found = 1;
e0c6ed95
AM
928 fail:
929 ;
252b5132
RH
930 }
931 if (found)
932 return this_try;
933 else
934 return 0;
935}
936
252b5132
RH
937static char buffer[20];
938
939static void
464800ca 940newfix (int ptr, int type, int size, expressionS *operand)
252b5132 941{
a72d6b4e
CG
942 int is_pcrel = 0;
943
464800ca
CG
944 /* size is in nibbles. */
945
252b5132
RH
946 if (operand->X_add_symbol
947 || operand->X_op_symbol
948 || operand->X_add_number)
949 {
a72d6b4e
CG
950 switch(type)
951 {
952 case R_JR:
953 case R_DISP7:
954 case R_CALLR:
955 is_pcrel = 1;
956 }
252b5132
RH
957 fix_new_exp (frag_now,
958 ptr,
7f31df7c 959 size / 2,
252b5132 960 operand,
a72d6b4e 961 is_pcrel,
252b5132
RH
962 type);
963 }
964}
965
966static char *
464800ca 967apply_fix (char *ptr, int type, expressionS *operand, int size)
252b5132 968{
7f31df7c 969 long n = operand->X_add_number;
252b5132 970
464800ca
CG
971 /* size is in nibbles. */
972
7f31df7c 973 newfix ((ptr - buffer) / 2, type, size + 1, operand);
252b5132
RH
974 switch (size)
975 {
879db8be 976 case 8: /* 8 nibbles == 32 bits. */
252b5132
RH
977 *ptr++ = n >> 28;
978 *ptr++ = n >> 24;
979 *ptr++ = n >> 20;
980 *ptr++ = n >> 16;
879db8be 981 case 4: /* 4 nibbles == 16 bits. */
252b5132
RH
982 *ptr++ = n >> 12;
983 *ptr++ = n >> 8;
984 case 2:
985 *ptr++ = n >> 4;
986 case 1:
987 *ptr++ = n >> 0;
988 break;
989 }
252b5132 990 return ptr;
252b5132
RH
991}
992
e0c6ed95
AM
993/* Now we know what sort of opcodes it is. Let's build the bytes. */
994
252b5132 995static void
464800ca 996build_bytes (opcode_entry_type *this_try, struct z8k_op *operand ATTRIBUTE_UNUSED)
252b5132 997{
252b5132 998 char *output_ptr = buffer;
252b5132 999 int c;
252b5132
RH
1000 int nibble;
1001 unsigned int *class_ptr;
1002
1003 frag_wane (frag_now);
1004 frag_new (0);
1005
25d3fb58 1006 memset (buffer, 0, sizeof (buffer));
252b5132 1007 class_ptr = this_try->byte_info;
252b5132 1008
879db8be 1009 for (nibble = 0; (c = *class_ptr++); nibble++)
252b5132
RH
1010 {
1011
1012 switch (c & CLASS_MASK)
1013 {
1014 default:
252b5132 1015 abort ();
e0c6ed95 1016
252b5132 1017 case CLASS_ADDRESS:
e0c6ed95 1018 /* Direct address, we don't cope with the SS mode right now. */
252b5132
RH
1019 if (segmented_mode)
1020 {
879db8be 1021 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
252b5132
RH
1022 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1023 }
1024 else
1025 {
1026 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1027 }
1028 da_operand = 0;
1029 break;
1030 case CLASS_DISP8:
e0c6ed95 1031 /* pc rel 8 bit */
252b5132
RH
1032 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1033 da_operand = 0;
1034 break;
1035
1036 case CLASS_0DISP7:
e0c6ed95 1037 /* pc rel 7 bit */
252b5132
RH
1038 *output_ptr = 0;
1039 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1040 da_operand = 0;
1041 break;
1042
1043 case CLASS_1DISP7:
e0c6ed95 1044 /* pc rel 7 bit */
252b5132
RH
1045 *output_ptr = 0x80;
1046 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
e0c6ed95 1047 output_ptr[-2] = 0x8;
252b5132
RH
1048 da_operand = 0;
1049 break;
1050
1051 case CLASS_BIT_1OR2:
1052 *output_ptr = c & 0xf;
1053 if (imm_operand)
1054 {
1055 if (imm_operand->X_add_number == 2)
e0c6ed95 1056 *output_ptr |= 2;
252b5132 1057 else if (imm_operand->X_add_number != 1)
e0c6ed95 1058 as_bad (_("immediate must be 1 or 2"));
252b5132
RH
1059 }
1060 else
e0c6ed95 1061 as_bad (_("immediate 1 or 2 expected"));
252b5132
RH
1062 output_ptr++;
1063 break;
1064 case CLASS_CC:
1065 *output_ptr++ = the_cc;
1066 break;
e0c6ed95 1067 case CLASS_0CCC:
bb5737a7
CG
1068 if (the_ctrl < 2 || the_ctrl > 7)
1069 as_bad (_("invalid control register name"));
e0c6ed95
AM
1070 *output_ptr++ = the_ctrl;
1071 break;
1072 case CLASS_1CCC:
bb5737a7
CG
1073 if (the_ctrl < 2 || the_ctrl > 7)
1074 as_bad (_("invalid control register name"));
e0c6ed95
AM
1075 *output_ptr++ = the_ctrl | 0x8;
1076 break;
1077 case CLASS_00II:
1078 *output_ptr++ = (~the_interrupt & 0x3);
1079 break;
1080 case CLASS_01II:
1081 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1082 break;
1083 case CLASS_FLAGS:
1084 *output_ptr++ = the_flags;
1085 break;
3c25c5f6 1086 case CLASS_IGNORE:
252b5132
RH
1087 case CLASS_BIT:
1088 *output_ptr++ = c & 0xf;
1089 break;
1090 case CLASS_REGN0:
1091 if (reg[c & 0xf] == 0)
e0c6ed95
AM
1092 as_bad (_("can't use R0 here"));
1093 /* Fall through. */
252b5132
RH
1094 case CLASS_REG:
1095 case CLASS_REG_BYTE:
1096 case CLASS_REG_WORD:
1097 case CLASS_REG_LONG:
1098 case CLASS_REG_QUAD:
e0c6ed95 1099 /* Insert bit mattern of right reg. */
252b5132
RH
1100 *output_ptr++ = reg[c & 0xf];
1101 break;
1102 case CLASS_DISP:
6840198f
NC
1103 switch (c & ARG_MASK)
1104 {
1105 case ARG_DISP12:
1106 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1107 break;
1108 case ARG_DISP16:
879db8be
NC
1109 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1110 break;
1111 default:
1112 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1113 }
252b5132
RH
1114 da_operand = 0;
1115 break;
1116
1117 case CLASS_IMM:
1118 {
252b5132
RH
1119 switch (c & ARG_MASK)
1120 {
3c25c5f6 1121 case ARG_NIM4:
7f31df7c
CG
1122 if (imm_operand->X_add_number > 15)
1123 {
1124 as_bad (_("immediate value out of range"));
1125 }
3c25c5f6 1126 imm_operand->X_add_number = -imm_operand->X_add_number;
252b5132
RH
1127 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1128 break;
7f31df7c 1129 /*case ARG_IMMNMINUS1: not used. */
252b5132
RH
1130 case ARG_IMM4M1:
1131 imm_operand->X_add_number--;
7f31df7c
CG
1132 /* Drop through. */
1133 case ARG_IMM4:
1134 if (imm_operand->X_add_number > 15)
1135 {
1136 as_bad (_("immediate value out of range"));
1137 }
252b5132
RH
1138 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1139 break;
1140 case ARG_NIM8:
1141 imm_operand->X_add_number = -imm_operand->X_add_number;
7f31df7c 1142 /* Drop through. */
252b5132
RH
1143 case ARG_IMM8:
1144 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1145 break;
1146 case ARG_IMM16:
1147 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1148 break;
252b5132
RH
1149 case ARG_IMM32:
1150 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1151 break;
252b5132
RH
1152 default:
1153 abort ();
1154 }
1155 }
1156 }
1157 }
1158
e0c6ed95 1159 /* Copy from the nibble buffer into the frag. */
252b5132
RH
1160 {
1161 int length = (output_ptr - buffer) / 2;
1162 char *src = buffer;
1163 char *fragp = frag_more (length);
1164
1165 while (src < output_ptr)
1166 {
1167 *fragp = (src[0] << 4) | src[1];
1168 src += 2;
1169 fragp++;
1170 }
252b5132 1171 }
252b5132
RH
1172}
1173
1174/* This is the guts of the machine-dependent assembler. STR points to a
1994a7c7
NC
1175 machine dependent instruction. This function is supposed to emit
1176 the frags/bytes it assembles to. */
252b5132
RH
1177
1178void
464800ca 1179md_assemble (char *str)
252b5132 1180{
879db8be 1181 char c;
252b5132
RH
1182 char *op_start;
1183 char *op_end;
d8cbebfd 1184 struct z8k_op operand[4];
252b5132 1185 opcode_entry_type *opcode;
252b5132 1186
e0c6ed95 1187 /* Drop leading whitespace. */
252b5132
RH
1188 while (*str == ' ')
1189 str++;
1190
e0c6ed95 1191 /* Find the op code end. */
252b5132 1192 for (op_start = op_end = str;
d5bf5799 1193 *op_end != 0 && *op_end != ' ' && ! is_end_of_line[(unsigned char) *op_end];
252b5132 1194 op_end++)
e0c6ed95 1195 ;
252b5132
RH
1196
1197 if (op_end == op_start)
1198 {
1199 as_bad (_("can't find opcode "));
1200 }
1201 c = *op_end;
1202
d5bf5799 1203 *op_end = 0; /* Zero-terminate op code string for hash_find() call. */
252b5132 1204
e0c6ed95 1205 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
252b5132
RH
1206
1207 if (opcode == NULL)
1208 {
1209 as_bad (_("unknown opcode"));
1210 return;
1211 }
1212
d5bf5799
CG
1213 *op_end = c; /* Restore original string. */
1214
252b5132
RH
1215 if (opcode->opcode == 250)
1216 {
252b5132
RH
1217 pseudo_typeS *p;
1218 char oc;
252b5132 1219 char *old = input_line_pointer;
252b5132 1220
3c25c5f6
NC
1221 /* Was really a pseudo op. */
1222
252b5132
RH
1223 input_line_pointer = op_end;
1224
1225 oc = *old;
1226 *old = '\n';
1227 while (*input_line_pointer == ' ')
1228 input_line_pointer++;
1229 p = (pseudo_typeS *) (opcode->func);
1230
1231 (p->poc_handler) (p->poc_val);
1232 input_line_pointer = old;
1233 *old = oc;
1234 }
1235 else
1236 {
3c25c5f6
NC
1237 char *new_input_line_pointer;
1238
1239 new_input_line_pointer = get_operands (opcode, op_end, operand);
1240 if (new_input_line_pointer)
7af0dfc7
CG
1241 {
1242 input_line_pointer = new_input_line_pointer;
1243 opcode = get_specific (opcode, operand);
1244 }
252b5132 1245
7af0dfc7 1246 if (new_input_line_pointer == NULL || opcode == NULL)
252b5132 1247 {
e0c6ed95 1248 /* Couldn't find an opcode which matched the operands. */
252b5132
RH
1249 char *where = frag_more (2);
1250
1251 where[0] = 0x0;
1252 where[1] = 0x0;
1253
1254 as_bad (_("Can't find opcode to match operands"));
1255 return;
1256 }
1257
1258 build_bytes (opcode, operand);
1259 }
1260}
1261
7f31df7c
CG
1262/* We have no need to default values of symbols. */
1263
252b5132 1264symbolS *
464800ca 1265md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
252b5132
RH
1266{
1267 return 0;
1268}
1269
e0c6ed95
AM
1270/* Various routines to kill one day. */
1271/* Equal to MAX_PRECISION in atof-ieee.c. */
252b5132
RH
1272#define MAX_LITTLENUMS 6
1273
e0c6ed95
AM
1274/* Turn a string in input_line_pointer into a floating point constant
1275 of type TYPE, and store the appropriate bytes in *LITP. The number
1276 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1277 returned, or NULL on OK. */
1278
252b5132 1279char *
464800ca 1280md_atof (int type, char *litP, int *sizeP)
252b5132
RH
1281{
1282 int prec;
1283 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1284 LITTLENUM_TYPE *wordP;
1285 char *t;
252b5132
RH
1286
1287 switch (type)
1288 {
1289 case 'f':
1290 case 'F':
1291 case 's':
1292 case 'S':
1293 prec = 2;
1294 break;
1295
1296 case 'd':
1297 case 'D':
1298 case 'r':
1299 case 'R':
1300 prec = 4;
1301 break;
1302
1303 case 'x':
1304 case 'X':
1305 prec = 6;
1306 break;
1307
1308 case 'p':
1309 case 'P':
1310 prec = 6;
1311 break;
1312
1313 default:
1314 *sizeP = 0;
1315 return _("Bad call to MD_ATOF()");
1316 }
1317 t = atof_ieee (input_line_pointer, type, words);
1318 if (t)
1319 input_line_pointer = t;
1320
1321 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1322 for (wordP = words; prec--;)
1323 {
1324 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1325 litP += sizeof (LITTLENUM_TYPE);
1326 }
1327 return 0;
1328}
1329\f
5a38dc70 1330const char *md_shortopts = "z:";
e0c6ed95 1331
3c25c5f6
NC
1332struct option md_longopts[] =
1333 {
7f31df7c
CG
1334#define OPTION_RELAX (OPTION_MD_BASE)
1335 {"linkrelax", no_argument, NULL, OPTION_RELAX},
3c25c5f6
NC
1336 {NULL, no_argument, NULL, 0}
1337 };
e0c6ed95
AM
1338
1339size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
1340
1341int
464800ca 1342md_parse_option (int c, char *arg)
252b5132
RH
1343{
1344 switch (c)
1345 {
1346 case 'z':
1347 if (!strcmp (arg, "8001"))
7f31df7c 1348 s_segm (1);
252b5132 1349 else if (!strcmp (arg, "8002"))
7f31df7c 1350 s_segm (0);
252b5132
RH
1351 else
1352 {
1353 as_bad (_("invalid architecture -z%s"), arg);
1354 return 0;
1355 }
7f31df7c
CG
1356 z8k_target_from_cmdline = 1;
1357 break;
1358
1359 case OPTION_RELAX:
1360 linkrelax = 1;
252b5132
RH
1361 break;
1362
1363 default:
1364 return 0;
1365 }
1366
1367 return 1;
1368}
1369
1370void
464800ca 1371md_show_usage (FILE *stream)
252b5132 1372{
e0c6ed95 1373 fprintf (stream, _("\
7f31df7c
CG
1374 Z8K options:\n\
1375 -z8001 generate segmented code\n\
1376 -z8002 generate unsegmented code\n\
1377 -linkrelax create linker relaxable code\n"));
252b5132
RH
1378}
1379\f
252b5132 1380void
464800ca
CG
1381md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
1382 segT seg ATTRIBUTE_UNUSED,
1383 fragS *fragP ATTRIBUTE_UNUSED)
252b5132 1384{
7f31df7c 1385 printf (_("call to md_convert_frag\n"));
252b5132
RH
1386 abort ();
1387}
1388
1389valueT
464800ca 1390md_section_align (segT seg, valueT size)
252b5132 1391{
e0c6ed95
AM
1392 return ((size + (1 << section_alignment[(int) seg]) - 1)
1393 & (-1 << section_alignment[(int) seg]));
252b5132
RH
1394}
1395
a72d6b4e
CG
1396/* Attempt to simplify or eliminate a fixup. To indicate that a fixup
1397 has been eliminated, set fix->fx_done. If fix->fx_addsy is non-NULL,
1398 we will have to generate a reloc entry. */
252b5132 1399void
55cf6793 1400md_apply_fix (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED)
252b5132 1401{
94f592af 1402 long val = * (long *) valP;
252b5132
RH
1403 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1404
1405 switch (fixP->fx_r_type)
1406 {
1407 case R_IMM4L:
7f31df7c 1408 buf[0] = (buf[0] & 0xf0) | (val & 0xf);
252b5132
RH
1409 break;
1410
1411 case R_JR:
a72d6b4e
CG
1412 if (fixP->fx_addsy)
1413 {
1414 fixP->fx_no_overflow = 1;
1415 fixP->fx_done = 0;
1416 }
1417 else
1418 {
f87a1e0c
CG
1419 if (val & 1)
1420 as_bad_where (fixP->fx_file, fixP->fx_line,
1421 _("cannot branch to odd address"));
1422 val /= 2;
1423 if (val > 127 || val < -128)
befd69ef
CG
1424 as_bad_where (fixP->fx_file, fixP->fx_line,
1425 _("relative jump out of range"));
f87a1e0c
CG
1426 *buf++ = val;
1427 fixP->fx_no_overflow = 1;
a72d6b4e
CG
1428 fixP->fx_done = 1;
1429 }
252b5132
RH
1430 break;
1431
1432 case R_DISP7:
a72d6b4e
CG
1433 if (fixP->fx_addsy)
1434 {
1435 fixP->fx_no_overflow = 1;
1436 fixP->fx_done = 0;
1437 }
1438 else
1439 {
d5bf5799 1440 if (val & 1)
f87a1e0c
CG
1441 as_bad_where (fixP->fx_file, fixP->fx_line,
1442 _("cannot branch to odd address"));
d5bf5799 1443 val /= 2;
a72d6b4e 1444 if (val > 0 || val < -127)
f87a1e0c
CG
1445 as_bad_where (fixP->fx_file, fixP->fx_line,
1446 _("relative jump out of range"));
a72d6b4e 1447 *buf = (*buf & 0x80) | (-val & 0x7f);
d5bf5799 1448 fixP->fx_no_overflow = 1;
a72d6b4e
CG
1449 fixP->fx_done = 1;
1450 }
7f31df7c 1451 break;
252b5132 1452
7f31df7c 1453 case R_CALLR:
a72d6b4e
CG
1454 if (fixP->fx_addsy)
1455 {
1456 fixP->fx_no_overflow = 1;
1457 fixP->fx_done = 0;
1458 }
1459 else
1460 {
1461 if (val & 1)
f87a1e0c
CG
1462 as_bad_where (fixP->fx_file, fixP->fx_line,
1463 _("cannot branch to odd address"));
a72d6b4e 1464 if (val > 4096 || val < -4095)
f87a1e0c
CG
1465 as_bad_where (fixP->fx_file, fixP->fx_line,
1466 _("relative call out of range"));
a72d6b4e 1467 val = -val / 2;
d5bf5799
CG
1468 *buf = (*buf & 0xf0) | ((val >> 8) & 0xf);
1469 buf++;
1470 *buf++ = val & 0xff;
a72d6b4e
CG
1471 fixP->fx_no_overflow = 1;
1472 fixP->fx_done = 1;
1473 }
252b5132
RH
1474 break;
1475
1476 case R_IMM8:
7f31df7c 1477 *buf++ = val;
252b5132 1478 break;
7f31df7c 1479
252b5132
RH
1480 case R_IMM16:
1481 *buf++ = (val >> 8);
1482 *buf++ = val;
1483 break;
7f31df7c 1484
252b5132
RH
1485 case R_IMM32:
1486 *buf++ = (val >> 24);
1487 *buf++ = (val >> 16);
1488 *buf++ = (val >> 8);
1489 *buf++ = val;
1490 break;
7f31df7c
CG
1491
1492 case R_REL16:
1493 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1494 if (val > 32767 || val < -32768)
f87a1e0c
CG
1495 as_bad_where (fixP->fx_file, fixP->fx_line,
1496 _("relative address out of range"));
7f31df7c
CG
1497 *buf++ = (val >> 8);
1498 *buf++ = val;
1499 fixP->fx_no_overflow = 1;
1500 break;
1501
252b5132
RH
1502 case 0:
1503 md_number_to_chars (buf, val, fixP->fx_size);
1504 break;
1505
1506 default:
55cf6793 1507 printf(_("md_apply_fix: unknown r_type 0x%x\n"), fixP->fx_r_type);
252b5132 1508 abort ();
252b5132 1509 }
94f592af
NC
1510
1511 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1512 fixP->fx_done = 1;
252b5132
RH
1513}
1514
1515int
464800ca
CG
1516md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
1517 segT segment_type ATTRIBUTE_UNUSED)
252b5132 1518{
7f31df7c 1519 printf (_("call to md_estimate_size_before_relax\n"));
252b5132
RH
1520 abort ();
1521}
1522
e0c6ed95 1523/* Put number into target byte order. */
252b5132
RH
1524
1525void
464800ca 1526md_number_to_chars (char *ptr, valueT use, int nbytes)
252b5132
RH
1527{
1528 number_to_chars_bigendian (ptr, use, nbytes);
1529}
e0c6ed95 1530
a72d6b4e
CG
1531/* On the Z8000, a PC-relative offset is relative to the address of the
1532 instruction plus its size. */
252b5132 1533long
464800ca 1534md_pcrel_from (fixS *fixP)
252b5132 1535{
a72d6b4e 1536 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
252b5132
RH
1537}
1538
1539void
464800ca 1540tc_coff_symbol_emit_hook (symbolS *s ATTRIBUTE_UNUSED)
252b5132
RH
1541{
1542}
This page took 0.39045 seconds and 4 git commands to generate.