]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-z8k.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gas / config / tc-z8k.c
CommitLineData
163107a1
SC
1/* tc-z8k.c -- Assemble code for the Zilog Z800N
2 Copyright (C) 1992 Free Software Foundation.
8ab22021 3
163107a1 4 This file is part of GAS, the GNU Assembler.
8ab22021 5
163107a1
SC
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
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
8ab22021 10
163107a1
SC
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.
8ab22021 15
163107a1
SC
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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
8ab22021 20/*
163107a1
SC
21 Written By Steve Chamberlain
22 sac@cygnus.com
23 */
163107a1 24#define DEFINE_TABLE
2b5936ff
SC
25#include <stdio.h>
26
cfb48ce5 27#include "../opcodes/z8k-opc.h"
163107a1
SC
28
29#include "as.h"
30#include "bfd.h"
31#include <ctype.h>
163107a1 32
7e8892c3 33const char comment_chars[] =
8ab22021 34{'!', 0};
7e8892c3 35const char line_separator_chars[] =
8ab22021 36{';', 0};
2b5936ff
SC
37const char line_comment_chars[] =
38{'#', 0};
163107a1 39
7e8892c3 40extern int machine;
163107a1
SC
41extern int coff_flags;
42int segmented_mode;
025b0302 43const int md_reloc_size;
163107a1
SC
44
45/* This table describes all the machine specific pseudo-ops the assembler
46 has to support. The fields are:
47 pseudo-op name without dot
48 function to call to execute this pseudo-op
49 Integer arg to pass to the function
50 */
51
8ab22021 52void cons ();
163107a1 53
7e8892c3 54void
8ab22021 55s_segm ()
163107a1
SC
56{
57 segmented_mode = 1;
58 machine = bfd_mach_z8001;
59 coff_flags = F_Z8001;
60}
61
7e8892c3 62void
8ab22021 63s_unseg ()
163107a1
SC
64{
65 segmented_mode = 0;
66 machine = bfd_mach_z8002;
67 coff_flags = F_Z8002;
68}
7e8892c3 69
2b5936ff
SC
70static
71void
72even ()
163107a1 73{
7e8892c3 74 frag_align (1, 0);
2b5936ff 75 record_alignment (now_seg, 1);
7e8892c3 76}
7e8892c3 77
2b5936ff
SC
78void obj_coff_section ();
79
80int
81tohex (c)
82 int c;
7e8892c3 83{
2b5936ff
SC
84 if (isdigit (c))
85 return c - '0';
86 if (islower (c))
87 return c - 'a' + 10;
88 return c - 'A' + 10;
7e8892c3 89}
2b5936ff
SC
90
91void
92sval ()
7e8892c3
SC
93{
94
2b5936ff
SC
95 SKIP_WHITESPACE ();
96 if (*input_line_pointer == '\'')
97 {
98 int c;
99 input_line_pointer++;
7e8892c3 100 c = *input_line_pointer++;
2b5936ff
SC
101 while (c != '\'')
102 {
103 if (c == '%')
104 {
105 c = (tohex (input_line_pointer[0]) << 4)
106 | tohex (input_line_pointer[1]);
107 input_line_pointer += 2;
108 }
109 FRAG_APPEND_1_CHAR (c);
110 c = *input_line_pointer++;
111 }
112 demand_empty_rest_of_line ();
7e8892c3 113 }
163107a1 114
7e8892c3
SC
115}
116const pseudo_typeS md_pseudo_table[] =
117{
2b5936ff
SC
118 {"int", cons, 2},
119 {"data.b", cons, 1},
120 {"data.w", cons, 2},
121 {"data.l", cons, 4},
122 {"form", listing_psize, 0},
123 {"heading", listing_title, 0},
124 {"import", s_ignore, 0},
125 {"page", listing_eject, 0},
126 {"program", s_ignore, 0},
127 {"z8001", s_segm, 0},
128 {"z8002", s_unseg, 0},
129
130
131 {"segm", s_segm, 0},
132 {"unsegm", s_unseg, 0},
133 {"unseg", s_unseg, 0},
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}
7e8892c3 145};
163107a1 146
7e8892c3 147const char EXP_CHARS[] = "eE";
163107a1
SC
148
149/* Chars that mean this number is a floating point constant */
150/* As in 0f12.456 */
151/* or 0d1.2345e12 */
587c4264 152const char FLT_CHARS[] = "rRsSfFdDxXpP";
163107a1
SC
153
154const relax_typeS md_relax_table[1];
155
163107a1
SC
156static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
157
7e8892c3 158void
8ab22021 159md_begin ()
163107a1
SC
160{
161 opcode_entry_type *opcode;
8ab22021 162 char *prev_name = "";
163107a1 163 int idx = 0;
163107a1 164
8ab22021 165 opcode_hash_control = hash_new ();
163107a1 166
8ab22021 167 for (opcode = z8k_table; opcode->name; opcode++)
163107a1 168 {
8ab22021
SC
169 /* Only enter unique codes into the table */
170 char *src = opcode->name;
171
172 if (strcmp (opcode->name, prev_name))
173 {
174 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
175 idx++;
176 }
177 opcode->idx = idx;
178 prev_name = opcode->name;
163107a1 179 }
8ab22021 180
7e8892c3
SC
181 /* default to z8002 */
182 s_unseg ();
163107a1 183
7e8892c3 184 /* insert the pseudo ops too */
2b5936ff
SC
185 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
186 {
187 opcode_entry_type *fake_opcode;
188 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
189 fake_opcode->name = md_pseudo_table[idx].poc_name,
190 fake_opcode->func = (void *) (md_pseudo_table + idx);
191 fake_opcode->opcode = 250;
192
193 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
194
195 }
7e8892c3 196}
163107a1 197
8ab22021
SC
198struct z8k_exp
199{
200 char *e_beg;
201 char *e_end;
202 expressionS e_exp;
163107a1 203};
8ab22021 204typedef struct z8k_op
163107a1 205{
8ab22021
SC
206 char regsize; /* 'b','w','r','q' */
207 unsigned int reg; /* 0..15 */
163107a1
SC
208
209 int mode;
210
8ab22021
SC
211 unsigned int x_reg; /* any other register associated with the mode */
212 expressionS exp; /* any expression */
7e8892c3 213}
163107a1 214
7e8892c3 215op_type;
163107a1 216
587c4264
ILT
217static expressionS *da_operand;
218static expressionS *imm_operand;
163107a1 219
587c4264 220int reg[16];
163107a1
SC
221int the_cc;
222
8ab22021
SC
223char *
224DEFUN (whatreg, (reg, src),
225 int *reg AND
226 char *src)
163107a1 227{
8ab22021
SC
228 if (isdigit (src[1]))
229 {
230 *reg = (src[0] - '0') * 10 + src[1] - '0';
231 return src + 2;
232 }
163107a1 233 else
8ab22021
SC
234 {
235 *reg = (src[0] - '0');
236 return src + 1;
237 }
163107a1
SC
238}
239
240/*
8ab22021 241 parse operands
163107a1
SC
242
243 rh0-rh7, rl0-rl7
244 r0-r15
245 rr0-rr14
246 rq0--rq12
247 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
248 r0l,r0h,..r7l,r7h
249 @WREG
250 @WREG+
251 @-WREG
252 #const
8ab22021 253
163107a1
SC
254 */
255
163107a1 256/* try and parse a reg name, returns number of chars consumed */
8ab22021
SC
257char *
258DEFUN (parse_reg, (src, mode, reg),
259 char *src AND
260 int *mode AND
261 unsigned int *reg)
163107a1
SC
262{
263 char *res = 0;
8ab22021 264
2b5936ff 265 if (src[0] == 's' && src[1] == 'p')
7e8892c3 266 {
2b5936ff
SC
267 if (segmented_mode)
268 {
269 *mode = CLASS_REG_LONG;
270 *reg = 14;
271 }
272 else
273 {
274 *mode = CLASS_REG_WORD;
275 *reg = 15;
276 }
277 return src + 2;
7e8892c3 278 }
8ab22021 279 if (src[0] == 'r')
7e8892c3 280 {
2b5936ff
SC
281 if (src[1] == 'r')
282 {
283 *mode = CLASS_REG_LONG;
284 res = whatreg (reg, src + 2);
285 }
286 else if (src[1] == 'h')
287 {
288 *mode = CLASS_REG_BYTE;
289 res = whatreg (reg, src + 2);
290 }
291 else if (src[1] == 'l')
292 {
293 *mode = CLASS_REG_BYTE;
294 res = whatreg (reg, src + 2);
295 *reg += 8;
296 }
297 else if (src[1] == 'q')
298 {
299 *mode = CLASS_REG_QUAD;
300 res = whatreg (reg, src + 2);
301 }
302 else
303 {
304 *mode = CLASS_REG_WORD;
305 res = whatreg (reg, src + 1);
306 }
7e8892c3 307 }
163107a1
SC
308 return res;
309
163107a1
SC
310}
311
312char *
8ab22021
SC
313DEFUN (parse_exp, (s, op),
314 char *s AND
315 expressionS * op)
163107a1 316{
8ab22021
SC
317 char *save = input_line_pointer;
318 char *new;
8ab22021
SC
319
320 input_line_pointer = s;
2b5936ff
SC
321 expression (op);
322 if (op->X_op == O_absent)
323 as_bad ("missing operand");
8ab22021
SC
324 new = input_line_pointer;
325 input_line_pointer = save;
2b5936ff 326 return new;
163107a1
SC
327}
328
163107a1 329/* The many forms of operand:
8ab22021 330
163107a1
SC
331 <rb>
332 <r>
333 <rr>
334 <rq>
335 @r
8ab22021 336 #exp
163107a1
SC
337 exp
338 exp(r)
339 r(#exp)
340 r(r)
341
342
8ab22021 343
163107a1
SC
344 */
345
346static
347char *
8ab22021
SC
348DEFUN (checkfor, (ptr, what),
349 char *ptr AND
350 char what)
163107a1 351{
8ab22021
SC
352 if (*ptr == what)
353 ptr++;
354 else
355 {
356 as_bad ("expected %c", what);
357 }
358 return ptr;
163107a1
SC
359}
360
361/* Make sure the mode supplied is the size of a word */
8ab22021
SC
362static void
363DEFUN (regword, (mode, string),
364 int mode AND
365 char *string)
163107a1
SC
366{
367 int ok;
8ab22021 368
163107a1 369 ok = CLASS_REG_WORD;
8ab22021
SC
370 if (ok != mode)
371 {
372 as_bad ("register is wrong size for a word %s", string);
373 }
163107a1
SC
374}
375
376/* Make sure the mode supplied is the size of an address */
8ab22021
SC
377static void
378DEFUN (regaddr, (mode, string),
379 int mode AND
380 char *string)
163107a1
SC
381{
382 int ok;
8ab22021 383
163107a1 384 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
8ab22021
SC
385 if (ok != mode)
386 {
387 as_bad ("register is wrong size for address %s", string);
388 }
163107a1
SC
389}
390
8ab22021
SC
391struct cc_names
392{
393 int value;
394 char *name;
163107a1 395
163107a1
SC
396};
397
7e8892c3 398struct cc_names table[] =
163107a1 399{
8ab22021
SC
400 0x0, "f",
401 0x1, "lt",
402 0x2, "le",
403 0x3, "ule",
404 0x4, "ov",
405 0x4, "pe",
406 0x5, "mi",
407 0x6, "eq",
408 0x6, "z",
409 0x7, "c",
410 0x7, "ult",
411 0x8, "t",
412 0x9, "ge",
413 0xa, "gt",
414 0xb, "ugt",
415 0xc, "nov",
416 0xc, "po",
417 0xd, "pl",
418 0xe, "ne",
419 0xe, "nz",
420 0xf, "nc",
421 0xf, "uge",
422 0, 0
423};
163107a1
SC
424
425static void
8ab22021
SC
426DEFUN (get_cc_operand, (ptr, mode, dst),
427 char **ptr AND
428 struct z8k_op *mode AND
429 unsigned int dst)
163107a1
SC
430{
431 char *src = *ptr;
432 int r;
433 int i;
8ab22021
SC
434
435 while (*src == ' ')
436 src++;
587c4264 437
163107a1
SC
438 mode->mode = CLASS_CC;
439 for (i = 0; table[i].name; i++)
163107a1 440 {
8ab22021
SC
441 int j;
442
443 for (j = 0; table[i].name[j]; j++)
444 {
445 if (table[i].name[j] != src[j])
446 goto fail;
447 }
448 the_cc = table[i].value;
449 *ptr = src + j;
450 return;
451 fail:;
163107a1 452 }
587c4264 453 the_cc = 0x8;
8ab22021 454 return;
163107a1
SC
455}
456
8ab22021 457static void
2b5936ff
SC
458get_operand (ptr, mode, dst)
459 char **ptr;
460 struct z8k_op *mode;
461 unsigned int dst;
163107a1
SC
462{
463 char *src = *ptr;
464 char *end;
8ab22021
SC
465 unsigned int num;
466 unsigned int len;
163107a1 467 unsigned int size;
8ab22021 468
163107a1
SC
469 mode->mode = 0;
470
587c4264 471 while (*src == ' ')
8ab22021
SC
472 src++;
473 if (*src == '#')
474 {
475 mode->mode = CLASS_IMM;
476 imm_operand = &(mode->exp);
477 src = parse_exp (src + 1, &(mode->exp));
478 }
479 else if (*src == '@')
480 {
481 int d;
163107a1 482
8ab22021
SC
483 mode->mode = CLASS_IR;
484 src = parse_reg (src + 1, &d, &mode->reg);
485 }
486 else
163107a1 487 {
8ab22021
SC
488 int regn;
489
490 end = parse_reg (src, &mode->mode, &regn);
491
492 if (end)
163107a1 493 {
8ab22021 494 int nw, nr;
163107a1 495
8ab22021
SC
496 src = end;
497 if (*src == '(')
498 {
499 src++;
500 end = parse_reg (src, &nw, &nr);
501 if (end)
502 {
503 /* Got Ra(Rb) */
504 src = end;
505
506 if (*src != ')')
507 {
508 as_bad ("Missing ) in ra(rb)");
509 }
510 else
511 {
512 src++;
513 }
514
515 regaddr (mode->mode, "ra(rb) ra");
2b5936ff 516/* regword (mode->mode, "ra(rb) rb");*/
8ab22021
SC
517 mode->mode = CLASS_BX;
518 mode->reg = regn;
519 mode->x_reg = nr;
520 reg[ARG_RX] = nr;
521 }
522 else
523 {
524 /* Got Ra(disp) */
525 if (*src == '#')
526 src++;
527 src = parse_exp (src, &(mode->exp));
528 src = checkfor (src, ')');
529 mode->mode = CLASS_BA;
530 mode->reg = regn;
531 mode->x_reg = 0;
532 imm_operand = &(mode->exp);
533 }
534 }
163107a1 535 else
8ab22021
SC
536 {
537 mode->reg = regn;
538 mode->x_reg = 0;
539 }
163107a1 540 }
8ab22021 541 else
163107a1 542 {
8ab22021
SC
543 /* No initial reg */
544 src = parse_exp (src, &(mode->exp));
545 if (*src == '(')
546 {
547 src++;
548 end = parse_reg (src, &(mode->mode), &regn);
549 regword (mode->mode, "addr(Ra) ra");
550 mode->mode = CLASS_X;
551 mode->reg = regn;
552 mode->x_reg = 0;
553 da_operand = &(mode->exp);
554 src = checkfor (end, ')');
555 }
556 else
557 {
558 /* Just an address */
559 mode->mode = CLASS_DA;
560 mode->reg = 0;
561 mode->x_reg = 0;
562 da_operand = &(mode->exp);
563 }
163107a1 564 }
163107a1 565 }
163107a1
SC
566 *ptr = src;
567}
568
569static
570char *
2b5936ff
SC
571get_operands (opcode, op_end, operand)
572 opcode_entry_type *opcode;
573 char *op_end;
574 op_type *operand;
163107a1
SC
575{
576 char *ptr = op_end;
8ab22021
SC
577
578 switch (opcode->noperands)
587c4264 579 {
7e8892c3
SC
580 case 0:
581 operand[0].mode = 0;
582 operand[1].mode = 0;
583 break;
587c4264 584
7e8892c3
SC
585 case 1:
586 ptr++;
587 if (opcode->arg_info[0] == CLASS_CC)
588 {
589 get_cc_operand (&ptr, operand + 0, 0);
590 }
591 else
592 {
8ab22021 593
7e8892c3
SC
594 get_operand (&ptr, operand + 0, 0);
595 }
596 operand[1].mode = 0;
597 break;
587c4264 598
7e8892c3
SC
599 case 2:
600 ptr++;
601 if (opcode->arg_info[0] == CLASS_CC)
602 {
603 get_cc_operand (&ptr, operand + 0, 0);
604 }
605 else
606 {
587c4264 607
7e8892c3
SC
608 get_operand (&ptr, operand + 0, 0);
609 }
2b5936ff
SC
610 if (ptr == 0)
611 return;
7e8892c3
SC
612 if (*ptr == ',')
613 ptr++;
614 get_operand (&ptr, operand + 1, 1);
615 break;
616
617 case 3:
618 ptr++;
619 get_operand (&ptr, operand + 0, 0);
620 if (*ptr == ',')
621 ptr++;
622 get_operand (&ptr, operand + 1, 1);
623 if (*ptr == ',')
624 ptr++;
625 get_operand (&ptr, operand + 2, 2);
626 break;
8ab22021 627
7e8892c3
SC
628 case 4:
629 ptr++;
630 get_operand (&ptr, operand + 0, 0);
631 if (*ptr == ',')
632 ptr++;
633 get_operand (&ptr, operand + 1, 1);
634 if (*ptr == ',')
635 ptr++;
636 get_operand (&ptr, operand + 2, 2);
637 if (*ptr == ',')
638 ptr++;
639 get_cc_operand (&ptr, operand + 3, 3);
640 break;
641 default:
642 abort ();
643 }
8ab22021
SC
644
645 return ptr;
163107a1
SC
646}
647
648/* Passed a pointer to a list of opcodes which use different
649 addressing modes, return the opcode which matches the opcodes
650 provided
651 */
652
163107a1
SC
653static
654opcode_entry_type *
8ab22021
SC
655DEFUN (get_specific, (opcode, operands),
656 opcode_entry_type * opcode AND
657 op_type * operands)
163107a1
SC
658
659{
8ab22021 660 opcode_entry_type *this_try = opcode;
163107a1
SC
661 int found = 0;
662 unsigned int noperands = opcode->noperands;
8ab22021 663
163107a1
SC
664 unsigned int dispreg;
665 unsigned int this_index = opcode->idx;
666
8ab22021 667 while (this_index == opcode->idx && !found)
163107a1 668 {
7e8892c3 669 unsigned int i;
163107a1 670
7e8892c3
SC
671 this_try = opcode++;
672 for (i = 0; i < noperands; i++)
673 {
674 int mode = operands[i].mode;
675
676 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
677 {
678 /* it could be an pc rel operand, if this is a da mode and
587c4264 679 we like disps, then insert it */
8ab22021 680
7e8892c3
SC
681 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
682 {
683 /* This is the case */
684 operands[i].mode = CLASS_DISP;
685 }
686 else if (mode == CLASS_BA && this_try->arg_info[i])
687 {
688 /* Can't think of a way to turn what we've been given into
587c4264 689 something that's ok */
7e8892c3
SC
690 goto fail;
691 }
2b5936ff
SC
692 else if (this_try->arg_info[i] & CLASS_PR)
693 {
694 if (mode == CLASS_REG_LONG && segmented_mode)
695 {
696 /* ok */
697 }
698 else if (mode == CLASS_REG_WORD && !segmented_mode)
699 {
700 /* ok */
701 }
702 else
703 goto fail;
704 }
7e8892c3
SC
705 else
706 goto fail;
707 }
708 switch (mode & CLASS_MASK)
709 {
710 default:
711 break;
712 case CLASS_X:
713 case CLASS_IR:
714 case CLASS_BA:
715 case CLASS_BX:
716 case CLASS_DISP:
717 case CLASS_REG:
718 case CLASS_REG_WORD:
719 case CLASS_REG_BYTE:
720 case CLASS_REG_QUAD:
721 case CLASS_REG_LONG:
722 case CLASS_REGN0:
723 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
724 break;
725 }
587c4264 726 }
7e8892c3
SC
727
728 found = 1;
729 fail:;
163107a1 730 }
8ab22021 731 if (found)
7e8892c3 732 return this_try;
8ab22021 733 else
7e8892c3 734 return 0;
163107a1 735}
7e8892c3 736
163107a1 737static void
8ab22021 738DEFUN (check_operand, (operand, width, string),
7e8892c3
SC
739 struct z8k_op *operand AND
740 unsigned int width AND
741 char *string)
163107a1 742{
8ab22021 743 if (operand->exp.X_add_symbol == 0
2b5936ff 744 && operand->exp.X_op_symbol == 0)
7e8892c3
SC
745 {
746
747 /* No symbol involved, let's look at offset, it's dangerous if any of
163107a1
SC
748 the high bits are not 0 or ff's, find out by oring or anding with
749 the width and seeing if the answer is 0 or all fs*/
7e8892c3
SC
750 if ((operand->exp.X_add_number & ~width) != 0 &&
751 (operand->exp.X_add_number | width) != (~0))
752 {
753 as_warn ("operand %s0x%x out of range.", string, operand->exp.X_add_number);
754 }
163107a1 755 }
8ab22021 756
163107a1
SC
757}
758
8ab22021
SC
759static char buffer[20];
760
761static void
762DEFUN (newfix, (ptr, type, operand),
763 int ptr AND
764 int type AND
765 expressionS * operand)
163107a1 766{
587c4264 767 if (operand->X_add_symbol
2b5936ff 768 || operand->X_op_symbol
8ab22021
SC
769 || operand->X_add_number)
770 {
2b5936ff
SC
771 fix_new_exp (frag_now,
772 ptr,
773 1,
774 operand,
775 0,
776 type);
8ab22021
SC
777 }
778}
779
780static char *
7e8892c3
SC
781DEFUN (apply_fix, (ptr, type, operand, size),
782 char *ptr AND
8ab22021 783 int type AND
7e8892c3 784 expressionS * operand AND
8ab22021
SC
785 int size)
786{
787 int n = operand->X_add_number;
7e8892c3 788
8ab22021 789 operand->X_add_number = n;
7e8892c3 790 newfix ((ptr - buffer) / 2, type, operand);
8ab22021 791#if 1
7e8892c3
SC
792 switch (size)
793 {
794 case 8: /* 8 nibbles == 32 bits */
795 *ptr++ = n >> 28;
796 *ptr++ = n >> 24;
797 *ptr++ = n >> 20;
798 *ptr++ = n >> 16;
799 case 4: /* 4 niblles == 16 bits */
800 *ptr++ = n >> 12;
801 *ptr++ = n >> 8;
802 case 2:
803 *ptr++ = n >> 4;
804 case 1:
805 *ptr++ = n >> 0;
806 break;
807 }
8ab22021 808#endif
7e8892c3 809 return ptr;
8ab22021 810
163107a1
SC
811}
812
163107a1
SC
813/* Now we know what sort of opcodes it is, lets build the bytes -
814 */
8ab22021
SC
815#define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
816static void
2b5936ff
SC
817build_bytes (this_try, operand)
818 opcode_entry_type * this_try;
819 struct z8k_op *operand;
163107a1
SC
820{
821 unsigned int i;
8ab22021 822
163107a1
SC
823 int length;
824 char *output;
825 char *output_ptr = buffer;
826 char part;
827 int c;
828 char high;
829 int nib;
830 int nibble;
2b5936ff 831 unsigned int *class_ptr;
7e8892c3
SC
832
833 frag_wane (frag_now);
834 frag_new (0);
8ab22021
SC
835
836 memset (buffer, 20, 0);
163107a1 837 class_ptr = this_try->byte_info;
7e8892c3 838top:;
163107a1
SC
839
840 for (nibble = 0; c = *class_ptr++; nibble++)
163107a1 841 {
587c4264 842
7e8892c3
SC
843 switch (c & CLASS_MASK)
844 {
845 default:
8ab22021 846
7e8892c3
SC
847 abort ();
848 case CLASS_ADDRESS:
849 /* Direct address, we don't cope with the SS mode right now */
850 if (segmented_mode)
851 {
852 da_operand->X_add_number |= 0x80000000;
853 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
854 }
855 else
856 {
857 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
858 }
859 da_operand = 0;
860 break;
861 case CLASS_DISP8:
862 /* pc rel 8 bit */
863 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
864 da_operand = 0;
2b5936ff 865 break;
587c4264 866
2b5936ff
SC
867 case CLASS_0DISP7:
868 /* pc rel 7 bit */
869 *output_ptr = 0;
870 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
871 da_operand = 0;
7e8892c3 872 break;
2b5936ff
SC
873
874 case CLASS_1DISP7:
875 /* pc rel 7 bit */
876 *output_ptr = 0x80;
877 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
878 da_operand = 0;
879 break;
880
7e8892c3
SC
881 case CLASS_BIT_1OR2:
882 *output_ptr = c & 0xf;
2b5936ff 883 if (imm_operand)
7e8892c3 884 {
2b5936ff
SC
885 if (imm_operand->X_add_number == 2)
886 {
887 *output_ptr |= 2;
888 }
889 else if (imm_operand->X_add_number != 1)
890 {
891 as_bad ("immediate must be 1 or 2");
892 }
7e8892c3 893 }
2b5936ff 894 else
7e8892c3 895 {
2b5936ff 896 as_bad ("immediate 1 or 2 expected");
7e8892c3 897 }
7e8892c3
SC
898 output_ptr++;
899 break;
900 case CLASS_CC:
901 *output_ptr++ = the_cc;
902 break;
903 case CLASS_BIT:
904 *output_ptr++ = c & 0xf;
905 break;
906 case CLASS_REGN0:
907 if (reg[c & 0xf] == 0)
908 {
909 as_bad ("can't use R0 here");
910 }
911 case CLASS_REG:
912 case CLASS_REG_BYTE:
913 case CLASS_REG_WORD:
914 case CLASS_REG_LONG:
915 case CLASS_REG_QUAD:
916 /* Insert bit mattern of
163107a1 917 right reg */
7e8892c3
SC
918 *output_ptr++ = reg[c & 0xf];
919 break;
920 case CLASS_DISP:
921 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
922 da_operand = 0;
923 break;
2b5936ff 924
7e8892c3
SC
925 case CLASS_IMM:
926 {
927 nib = 0;
928 switch (c & ARG_MASK)
929 {
930 case ARG_IMM4:
931 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
932 break;
933 case ARG_IMM4M1:
934 imm_operand->X_add_number--;
935 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
936 break;
937 case ARG_IMMNMINUS1:
938 imm_operand->X_add_number--;
939 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
940 break;
941 case ARG_NIM8:
942 imm_operand->X_add_number = -imm_operand->X_add_number;
943 case ARG_IMM8:
944 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
945 break;
946 case ARG_IMM16:
947 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
948 break;
949
950 case ARG_IMM32:
951 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
952 break;
953
954 default:
955 abort ();
956 }
957 }
958 }
163107a1 959 }
163107a1
SC
960
961 /* Copy from the nibble buffer into the frag */
962
7e8892c3
SC
963 {
964 int length = (output_ptr - buffer) / 2;
965 char *src = buffer;
966 char *fragp = frag_more (length);
163107a1 967
7e8892c3
SC
968 while (src < output_ptr)
969 {
970 *fragp = (src[0] << 4) | src[1];
971 src += 2;
972 fragp++;
973 }
8ab22021 974
7e8892c3 975 }
163107a1
SC
976
977}
978
979/* This is the guts of the machine-dependent assembler. STR points to a
980 machine dependent instruction. This funciton is supposed to emit
981 the frags/bytes it assembles to.
982 */
983
8ab22021
SC
984void
985DEFUN (md_assemble, (str),
986 char *str)
163107a1
SC
987{
988 char *op_start;
989 char *op_end;
990 unsigned int i;
8ab22021 991 struct z8k_op operand[3];
163107a1 992 opcode_entry_type *opcode;
8ab22021
SC
993 opcode_entry_type *prev_opcode;
994
163107a1 995 char *dot = 0;
8ab22021
SC
996 char c;
997
163107a1
SC
998 /* Drop leading whitespace */
999 while (*str == ' ')
2b5936ff 1000 str++;
8ab22021 1001
163107a1
SC
1002 /* find the op code end */
1003 for (op_start = op_end = str;
1004 *op_end != 0 && *op_end != ' ';
8ab22021 1005 op_end++)
2b5936ff
SC
1006 {
1007 }
8ab22021 1008
163107a1 1009 ;
8ab22021
SC
1010
1011 if (op_end == op_start)
2b5936ff
SC
1012 {
1013 as_bad ("can't find opcode ");
1014 }
163107a1 1015 c = *op_end;
8ab22021 1016
163107a1 1017 *op_end = 0;
163107a1 1018
8ab22021
SC
1019 opcode = (opcode_entry_type *) hash_find (opcode_hash_control,
1020 op_start);
1021
7e8892c3 1022
8ab22021 1023 if (opcode == NULL)
2b5936ff
SC
1024 {
1025 as_bad ("unknown opcode");
1026 return;
1027 }
7e8892c3 1028
2b5936ff
SC
1029 if (opcode->opcode == 250)
1030 {
1031 /* was really a pseudo op */
7e8892c3 1032
2b5936ff
SC
1033 pseudo_typeS *p;
1034 char oc;
8ab22021 1035
2b5936ff
SC
1036 char *old = input_line_pointer;
1037 *op_end = c;
8ab22021 1038
163107a1 1039
2b5936ff 1040 input_line_pointer = op_end;
7e8892c3 1041
2b5936ff
SC
1042 oc = *old;
1043 *old = '\n';
1044 while (*input_line_pointer == ' ')
1045 input_line_pointer++;
1046 p = (pseudo_typeS *) (opcode->func);
163107a1 1047
2b5936ff
SC
1048 (p->poc_handler) (p->poc_val);
1049 input_line_pointer = old;
1050 *old = oc;
1051 }
1052 else
8ab22021 1053 {
2b5936ff
SC
1054 input_line_pointer = get_operands (opcode, op_end,
1055 operand);
1056 prev_opcode = opcode;
163107a1 1057
2b5936ff 1058 opcode = get_specific (opcode, operand);
8ab22021 1059
2b5936ff
SC
1060 if (opcode == 0)
1061 {
1062 /* Couldn't find an opcode which matched the operands */
1063 char *where = frag_more (2);
8ab22021 1064
2b5936ff
SC
1065 where[0] = 0x0;
1066 where[1] = 0x0;
1067
1068 as_bad ("Can't find opcode to match operands");
1069 return;
1070 }
1071
1072 build_bytes (opcode, operand);
1073 }
8ab22021
SC
1074}
1075
1076void
1077DEFUN (tc_crawl_symbol_chain, (headers),
1078 object_headers * headers)
163107a1 1079{
8ab22021 1080 printf ("call to tc_crawl_symbol_chain \n");
163107a1
SC
1081}
1082
8ab22021
SC
1083symbolS *
1084DEFUN (md_undefined_symbol, (name),
1085 char *name)
163107a1 1086{
8ab22021 1087 return 0;
163107a1
SC
1088}
1089
8ab22021
SC
1090void
1091DEFUN (tc_headers_hook, (headers),
1092 object_headers * headers)
163107a1 1093{
8ab22021 1094 printf ("call to tc_headers_hook \n");
163107a1 1095}
8ab22021 1096
163107a1 1097void
8ab22021 1098DEFUN_VOID (md_end)
163107a1
SC
1099{
1100}
1101
1102/* Various routines to kill one day */
1103/* Equal to MAX_PRECISION in atof-ieee.c */
1104#define MAX_LITTLENUMS 6
1105
1106/* Turn a string in input_line_pointer into a floating point constant of type
1107 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1108 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1109 */
1110char *
8ab22021
SC
1111md_atof (type, litP, sizeP)
1112 char type;
1113 char *litP;
1114 int *sizeP;
163107a1 1115{
8ab22021
SC
1116 int prec;
1117 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1118 LITTLENUM_TYPE *wordP;
1119 char *t;
1120 char *atof_ieee ();
1121
1122 switch (type)
1123 {
1124 case 'f':
1125 case 'F':
1126 case 's':
1127 case 'S':
1128 prec = 2;
1129 break;
1130
1131 case 'd':
1132 case 'D':
1133 case 'r':
1134 case 'R':
1135 prec = 4;
1136 break;
1137
1138 case 'x':
1139 case 'X':
1140 prec = 6;
1141 break;
1142
1143 case 'p':
1144 case 'P':
1145 prec = 6;
1146 break;
1147
1148 default:
1149 *sizeP = 0;
1150 return "Bad call to MD_ATOF()";
1151 }
1152 t = atof_ieee (input_line_pointer, type, words);
1153 if (t)
1154 input_line_pointer = t;
1155
1156 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1157 for (wordP = words; prec--;)
1158 {
1159 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1160 litP += sizeof (LITTLENUM_TYPE);
1161 }
2b5936ff 1162 return 0;
163107a1
SC
1163}
1164
1165int
8ab22021
SC
1166md_parse_option (argP, cntP, vecP)
1167 char **argP;
1168 int *cntP;
1169 char ***vecP;
163107a1
SC
1170
1171{
2b5936ff
SC
1172 if (!strcmp (*argP, "z8001"))
1173 {
1174 s_segm ();
1175 }
1176 else if (!strcmp (*argP, "z8002"))
1177 {
1178 s_unseg ();
1179 }
1180 else
1181 return 0;
7e8892c3
SC
1182 **argP = 0;
1183 return 1;
163107a1
SC
1184}
1185
1186int md_short_jump_size;
1187
7e8892c3 1188void
8ab22021 1189tc_aout_fix_to_chars ()
163107a1 1190{
8ab22021
SC
1191 printf ("call to tc_aout_fix_to_chars \n");
1192 abort ();
1193}
1194
7e8892c3 1195void
8ab22021
SC
1196md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1197 char *ptr;
025b0302
ME
1198 addressT from_addr;
1199 addressT to_addr;
8ab22021
SC
1200 fragS *frag;
1201 symbolS *to_symbol;
1202{
1203 as_fatal ("failed sanity check.");
163107a1
SC
1204}
1205
1206void
8ab22021
SC
1207md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1208 char *ptr;
025b0302 1209 addressT from_addr, to_addr;
8ab22021
SC
1210 fragS *frag;
1211 symbolS *to_symbol;
163107a1 1212{
8ab22021 1213 as_fatal ("failed sanity check.");
163107a1
SC
1214}
1215
1216void
8ab22021
SC
1217md_convert_frag (headers, fragP)
1218 object_headers *headers;
1219 fragS *fragP;
163107a1 1220
8ab22021
SC
1221{
1222 printf ("call to md_convert_frag \n");
1223 abort ();
1224}
163107a1 1225
025b0302 1226valueT
8ab22021
SC
1227DEFUN (md_section_align, (seg, size),
1228 segT seg AND
025b0302 1229 valueT size)
163107a1 1230{
8ab22021
SC
1231 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1232
163107a1
SC
1233}
1234
1235void
8ab22021
SC
1236md_apply_fix (fixP, val)
1237 fixS *fixP;
1238 long val;
163107a1
SC
1239{
1240 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
cfb48ce5 1241
8ab22021
SC
1242 switch (fixP->fx_r_type)
1243 {
1244 case R_IMM4L:
1245 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1246 break;
587c4264 1247
8ab22021
SC
1248 case R_JR:
1249
1250 *buf++ = val;
7e8892c3 1251 /* if (val != 0) abort();*/
8ab22021 1252 break;
587c4264 1253
2b5936ff
SC
1254 case R_DISP7:
1255
1256 *buf++ += val;
1257 /* if (val != 0) abort();*/
1258 break;
1259
8ab22021
SC
1260 case R_IMM8:
1261 buf[0] += val;
1262 break;
2b5936ff 1263 case R_IMM16:
8ab22021
SC
1264 *buf++ = (val >> 8);
1265 *buf++ = val;
1266 break;
1267 case R_IMM32:
1268 *buf++ = (val >> 24);
1269 *buf++ = (val >> 16);
1270 *buf++ = (val >> 8);
1271 *buf++ = val;
1272 break;
7e8892c3 1273#if 0
8ab22021
SC
1274 case R_DA | R_SEG:
1275 *buf++ = (val >> 16);
1276 *buf++ = 0x00;
1277 *buf++ = (val >> 8);
1278 *buf++ = val;
1279 break;
7e8892c3
SC
1280#endif
1281
8ab22021
SC
1282 default:
1283 abort ();
1284
1285 }
163107a1
SC
1286}
1287
7e8892c3 1288void
8ab22021
SC
1289DEFUN (md_operand, (expressionP), expressionS * expressionP)
1290{
1291}
163107a1 1292
8ab22021 1293int md_long_jump_size;
163107a1 1294int
8ab22021
SC
1295md_estimate_size_before_relax (fragP, segment_type)
1296 register fragS *fragP;
1297 register segT segment_type;
1298{
1299 printf ("call tomd_estimate_size_before_relax \n");
1300 abort ();
1301}
1302
163107a1
SC
1303/* Put number into target byte order */
1304
7e8892c3 1305void
8ab22021
SC
1306DEFUN (md_number_to_chars, (ptr, use, nbytes),
1307 char *ptr AND
025b0302 1308 valueT use AND
8ab22021 1309 int nbytes)
163107a1 1310{
8ab22021
SC
1311 switch (nbytes)
1312 {
7e8892c3
SC
1313 case 4:
1314 *ptr++ = (use >> 24) & 0xff;
8ab22021
SC
1315 case 3:
1316 *ptr++ = (use >> 16) & 0xff;
1317 case 2:
1318 *ptr++ = (use >> 8) & 0xff;
1319 case 1:
1320 *ptr++ = (use >> 0) & 0xff;
1321 break;
1322 default:
1323 abort ();
1324 }
1325}
7e8892c3 1326long
8ab22021
SC
1327md_pcrel_from (fixP)
1328 fixS *fixP;
1329{
1330 abort ();
163107a1 1331}
163107a1 1332
7e8892c3 1333void
8ab22021
SC
1334tc_coff_symbol_emit_hook ()
1335{
1336}
163107a1 1337
7e8892c3 1338void
8ab22021
SC
1339tc_reloc_mangle (fix_ptr, intr, base)
1340 fixS *fix_ptr;
1341 struct internal_reloc *intr;
1342 bfd_vma base;
163107a1
SC
1343
1344{
1345 symbolS *symbol_ptr;
8ab22021 1346
163107a1 1347 symbol_ptr = fix_ptr->fx_addsy;
8ab22021 1348
163107a1
SC
1349 /* If this relocation is attached to a symbol then it's ok
1350 to output it */
8ab22021 1351 if (fix_ptr->fx_r_type == 0)
163107a1 1352 {
2b5936ff
SC
1353 /* cons likes to create reloc32's whatever the size of the reloc..
1354 */
1355 switch (fix_ptr->fx_size)
1356 {
8ab22021 1357
2b5936ff
SC
1358 case 2:
1359 intr->r_type = R_IMM16;
1360 break;
1361 case 1:
1362 intr->r_type = R_IMM8;
1363 break;
1364 case 4:
1365 intr->r_type = R_IMM32;
1366 break;
1367 default:
1368 abort ();
8ab22021 1369
2b5936ff 1370 }
7e8892c3 1371
2b5936ff 1372 }
8ab22021 1373 else
2b5936ff
SC
1374 {
1375 intr->r_type = fix_ptr->fx_r_type;
1376 }
8ab22021
SC
1377
1378 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
163107a1 1379 intr->r_offset = fix_ptr->fx_offset;
8ab22021 1380
163107a1 1381 if (symbol_ptr)
2b5936ff 1382 intr->r_symndx = symbol_ptr->sy_number;
163107a1 1383 else
2b5936ff 1384 intr->r_symndx = -1;
163107a1 1385
8ab22021 1386}