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