1 /* tc-z8k.c -- Assemble code for the Zilog Z800N
2 Copyright (C) 1992 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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. */
22 Written By Steve Chamberlain
28 #include "../opcodes/z8k-opc.h"
36 const char comment_chars
[] = { '!',0 };
37 const char line_separator_chars
[] = { ';' ,0};
38 const char line_comment_chars
[] = "";
41 extern int coff_flags
;
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
58 machine
= bfd_mach_z8001
;
65 machine
= bfd_mach_z8002
;
68 const pseudo_typeS md_pseudo_table
[] =
71 { "data.b", cons
, 1 },
72 { "data.w", cons
, 2 },
73 { "data.l", cons
, 4 },
74 { "form", listing_psize
, 0 },
75 { "heading", listing_title
, 0},
76 { "import", s_ignore
, 0},
77 { "page", listing_eject
, 0},
78 { "program", s_ignore
, 0},
79 { "z8001", s_segm
, 0},
80 { "z8002", s_unseg
, 0},
85 const char EXP_CHARS
[] = "eE";
87 /* Chars that mean this number is a floating point constant */
90 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
93 const relax_typeS md_relax_table
[1];
96 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
102 opcode_entry_type
*opcode
;
106 opcode_hash_control
= hash_new();
109 for (opcode
= z8k_table
; opcode
->name
; opcode
++)
111 /* Only enter unique codes into the table */
112 char *src
= opcode
->name
;
114 if (strcmp(opcode
->name
, prev_name
))
116 hash_insert(opcode_hash_control
, opcode
->name
, (char *)opcode
);
120 prev_name
= opcode
->name
;
131 typedef struct z8k_op
133 char regsize
; /* 'b','w','r','q' */
134 unsigned int reg
; /* 0..15 */
138 unsigned int x_reg
;/* any other register associated with the mode */
139 expressionS exp
; /* any expression */
144 static expressionS
*da_operand
;
145 static expressionS
*imm_operand
;
152 DEFUN(whatreg
,(reg
, src
),
158 *reg
= (src
[0] - '0') * 10 +src
[1] - '0';
163 *reg
= (src
[0] - '0');
176 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
186 /* try and parse a reg name, returns number of chars consumed */
188 DEFUN(parse_reg
,(src
, mode
, reg
),
198 *mode
= CLASS_REG_LONG
;
199 res
= whatreg(reg
, src
+2);
201 else if (src
[1] == 'h')
203 *mode
= CLASS_REG_BYTE
;
204 res
= whatreg(reg
, src
+2);
206 else if (src
[1] == 'l')
208 *mode
= CLASS_REG_BYTE
;
209 res
= whatreg(reg
, src
+2);
211 else if (src
[1] == 'q')
213 * mode
= CLASS_REG_QUAD
;
214 res
= whatreg(reg
, src
+2);
218 *mode
= CLASS_REG_WORD
;
219 res
= whatreg(reg
, src
+1);
228 DEFUN(parse_exp
,(s
, op
),
232 char *save
= input_line_pointer
;
235 input_line_pointer
= s
;
237 new = input_line_pointer
;
238 input_line_pointer
= save
;
249 as_bad("Missing operand");
252 as_bad("Don't understand operand of type %s", segment_name (seg
));
258 /* The many forms of operand:
277 DEFUN(checkfor
,(ptr
, what
),
281 if (*ptr
== what
) ptr
++;
283 as_bad("expected %c", what
);
288 /* Make sure the mode supplied is the size of a word */
290 DEFUN(regword
,(mode
, string
),
298 as_bad("register is wrong size for a word %s", string
);
302 /* Make sure the mode supplied is the size of an address */
304 DEFUN(regaddr
,(mode
, string
),
309 ok
= segmented_mode
? CLASS_REG_LONG
: CLASS_REG_WORD
;
312 as_bad("register is wrong size for address %s", string
);
323 struct cc_names table
[] =
351 DEFUN(get_cc_operand
,(ptr
, mode
, dst
),
353 struct z8k_op
*mode AND
362 mode
->mode
= CLASS_CC
;
363 for (i
= 0; table
[i
].name
; i
++)
366 for (j
= 0; table
[i
].name
[j
]; j
++)
368 if (table
[i
].name
[j
] != src
[j
])
371 the_cc
= table
[i
].value
;
381 DEFUN(get_operand
,(ptr
, mode
, dst
),
383 struct z8k_op
*mode AND
398 mode
->mode
= CLASS_IMM
;
399 imm_operand
= &(mode
->exp
);
400 src
= parse_exp(src
+1, &(mode
->exp
));
402 else if (*src
== '@') {
404 mode
->mode
= CLASS_IR
;
405 src
= parse_reg(src
+1, &d
, &mode
->reg
);
410 end
= parse_reg(src
, &mode
->mode
, ®n
);
419 end
= parse_reg(src
, &nw
, &nr
);
427 as_bad("Missing ) in ra(rb)");
434 regaddr(mode
->mode
,"ra(rb) ra");
435 regword(mode
->mode
,"ra(rb) rb");
436 mode
->mode
= CLASS_BX
;
446 src
= parse_exp(src
, &(mode
->exp
));
447 src
= checkfor(src
, ')');
448 mode
->mode
= CLASS_BA
;
451 da_operand
= &(mode
->exp
);
463 src
= parse_exp(src
, &(mode
->exp
));
467 end
= parse_reg(src
, &(mode
->mode
), ®n
);
468 regword(mode
->mode
,"addr(Ra) ra");
469 mode
->mode
= CLASS_X
;
472 da_operand
= &(mode
->exp
);
473 src
= checkfor(end
, ')');
477 /* Just an address */
478 mode
->mode
= CLASS_DA
;
481 da_operand
= &(mode
->exp
);
490 DEFUN(get_operands
,(opcode
, op_end
, operand
),
491 opcode_entry_type
*opcode AND
496 switch (opcode
->noperands
)
505 if (opcode
->arg_info
[0] == CLASS_CC
)
507 get_cc_operand(&ptr
, operand
+0,0);
512 get_operand(& ptr
, operand
+0,0);
519 if (opcode
->arg_info
[0] == CLASS_CC
)
521 get_cc_operand(&ptr
, operand
+0,0);
526 get_operand(& ptr
, operand
+0,0);
528 if (*ptr
== ',') ptr
++;
529 get_operand(& ptr
, operand
+1, 1);
534 get_operand(& ptr
, operand
+0,0);
535 if (*ptr
== ',') ptr
++;
536 get_operand(& ptr
, operand
+1, 1);
537 if (*ptr
== ',') ptr
++;
538 get_operand(& ptr
, operand
+2, 2);
549 /* Passed a pointer to a list of opcodes which use different
550 addressing modes, return the opcode which matches the opcodes
559 DEFUN(get_specific
,(opcode
, operands
),
560 opcode_entry_type
*opcode AND
564 opcode_entry_type
*this_try
= opcode
;
566 unsigned int noperands
= opcode
->noperands
;
568 unsigned int dispreg
;
569 unsigned int this_index
= opcode
->idx
;
571 while (this_index
== opcode
->idx
&& !found
)
575 this_try
= opcode
++;
576 for (i
= 0; i
< noperands
; i
++)
578 int mode
= operands
[i
].mode
;
580 if ((mode
&CLASS_MASK
) != (this_try
->arg_info
[i
] & CLASS_MASK
))
582 /* it could be an pc rel operand, if this is a da mode and
583 we like disps, then insert it */
585 if (mode
== CLASS_DA
&& this_try
->arg_info
[i
] == CLASS_DISP
)
587 /* This is the case */
588 operands
[i
].mode
= CLASS_DISP
;
591 /* Can't think of a way to turn what we've been given into
592 something that's ok */
596 switch (mode
& CLASS_MASK
) {
605 reg
[this_try
->arg_info
[i
] & ARG_MASK
] = operands
[i
].reg
;
620 DEFUN(check_operand
,(operand
, width
, string
),
621 struct z8k_op
*operand AND
622 unsigned int width AND
625 if (operand
->exp
.X_add_symbol
== 0
626 && operand
->exp
.X_subtract_symbol
== 0)
629 /* No symbol involved, let's look at offset, it's dangerous if any of
630 the high bits are not 0 or ff's, find out by oring or anding with
631 the width and seeing if the answer is 0 or all fs*/
632 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
633 (operand
->exp
.X_add_number
| width
)!= (~0))
635 as_warn("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
642 DEFUN(newfix
,(ptr
, type
, operand
),
645 expressionS
*operand
)
647 if (operand
->X_add_symbol
648 || operand
->X_subtract_symbol
649 || operand
->X_add_number
) {
653 operand
->X_add_symbol
,
654 operand
->X_subtract_symbol
,
655 operand
->X_add_number
,
662 /* Now we know what sort of opcodes it is, lets build the bytes -
665 DEFUN (build_bytes
,(this_try
, operand
),
666 opcode_entry_type
*this_try AND
667 struct z8k_op
*operand
)
673 char *output_ptr
= buffer
;
679 unsigned short *class_ptr
;
680 memset(buffer
, 20, 0);
681 class_ptr
= this_try
->byte_info
;
684 for (nibble
= 0; c
= *class_ptr
++; nibble
++)
687 switch (c
& CLASS_MASK
)
693 /* Direct address, we don't cope with the SS mode right now */
694 if (segmented_mode
) {
695 newfix((output_ptr
-buffer
)/2, R_DA
| R_SEG
, da_operand
);
706 newfix((output_ptr
-buffer
)/2, R_DA
, da_operand
);
716 newfix((output_ptr
-buffer
)/2, R_JR
, da_operand
);
723 *output_ptr
++ = the_cc
;
726 *output_ptr
++ = c
& 0xf;
731 as_bad("can't use R0 here");
738 /* Insert bit mattern of
740 *output_ptr
++ = reg
[c
& 0xf];
743 newfix((output_ptr
-buffer
)/2, R_DA
, da_operand
);
750 switch (c
& ARG_MASK
)
753 *output_ptr
++ = imm_operand
->X_add_number
;
754 imm_operand
->X_add_number
= 0;
755 newfix((output_ptr
-buffer
)/2, R_IMM4L
, imm_operand
);
758 imm_operand
->X_add_number
--;
759 newfix((output_ptr
-buffer
)/2, R_IMM4L
, imm_operand
);
763 newfix((output_ptr
-buffer
)/2, R_IMM8
, imm_operand
);
768 imm_operand
->X_add_number
= - imm_operand
->X_add_number
;
769 newfix((output_ptr
-buffer
)/2, R_DA
, imm_operand
);
778 int n
= imm_operand
->X_add_number
;
779 imm_operand
->X_add_number
= 0;
780 newfix((output_ptr
-buffer
)/2, R_DA
, imm_operand
);
781 *output_ptr
++ = n
>>24;
782 *output_ptr
++ = n
>>16;
783 *output_ptr
++ = n
>>8;
790 newfix((output_ptr
-buffer
)/2, R_IMM32
, imm_operand
);
812 /* Copy from the nibble buffer into the frag */
815 int length
= (output_ptr
- buffer
) / 2 ;
817 char *fragp
= frag_more(length
);
820 while (src
< output_ptr
)
822 *fragp
= (src
[0] << 4) | src
[1];
832 /* This is the guts of the machine-dependent assembler. STR points to a
833 machine dependent instruction. This funciton is supposed to emit
834 the frags/bytes it assembles to.
840 DEFUN(md_assemble
,(str
),
846 struct z8k_op operand
[3];
847 opcode_entry_type
*opcode
;
848 opcode_entry_type
* prev_opcode
;
852 /* Drop leading whitespace */
856 /* find the op code end */
857 for (op_start
= op_end
= str
;
858 *op_end
!= 0 && *op_end
!= ' ';
865 if (op_end
== op_start
)
867 as_bad("can't find opcode ");
873 opcode
= (opcode_entry_type
*) hash_find(opcode_hash_control
,
878 as_bad("unknown opcode");
883 input_line_pointer
= get_operands(opcode
, op_end
,
886 prev_opcode
= opcode
;
888 opcode
= get_specific(opcode
, operand
);
892 /* Couldn't find an opcode which matched the operands */
893 char *where
=frag_more(2);
897 as_bad("Can't find opcode to match operands");
901 build_bytes(opcode
, operand
);
906 DEFUN(tc_crawl_symbol_chain
, (headers
),
907 object_headers
*headers
)
909 printf("call to tc_crawl_symbol_chain \n");
912 symbolS
*DEFUN(md_undefined_symbol
,(name
),
919 DEFUN(tc_headers_hook
,(headers
),
920 object_headers
*headers
)
922 printf("call to tc_headers_hook \n");
929 /* Various routines to kill one day */
930 /* Equal to MAX_PRECISION in atof-ieee.c */
931 #define MAX_LITTLENUMS 6
933 /* Turn a string in input_line_pointer into a floating point constant of type
934 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
935 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
938 md_atof(type
,litP
,sizeP
)
944 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
945 LITTLENUM_TYPE
*wordP
;
976 return "Bad call to MD_ATOF()";
978 t
=atof_ieee(input_line_pointer
,type
,words
);
980 input_line_pointer
=t
;
982 *sizeP
=prec
* sizeof(LITTLENUM_TYPE
);
983 for(wordP
=words
;prec
--;) {
984 md_number_to_chars(litP
,(long)(*wordP
++),sizeof(LITTLENUM_TYPE
));
985 litP
+=sizeof(LITTLENUM_TYPE
);
987 return ""; /* Someone should teach Dean about null pointers */
991 md_parse_option(argP
, cntP
, vecP
)
1001 int md_short_jump_size
;
1003 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
1005 void md_create_short_jump(ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1012 as_fatal("failed sanity check.");
1016 md_create_long_jump(ptr
,from_addr
,to_addr
,frag
,to_symbol
)
1018 long from_addr
, to_addr
;
1022 as_fatal("failed sanity check.");
1026 md_convert_frag(headers
, fragP
)
1027 object_headers
*headers
;
1030 { printf("call to md_convert_frag \n"); abort(); }
1033 DEFUN(md_section_align
,(seg
, size
),
1037 return((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1042 md_apply_fix(fixP
, val
)
1046 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1048 switch(fixP
->fx_r_type
) {
1050 buf
[0] = (buf
[0] & 0xf0) | ((buf
[0] + val
) & 0xf);
1056 /* if (val != 0) abort();*/
1086 void DEFUN(md_operand
, (expressionP
),expressionS
*expressionP
)
1089 int md_long_jump_size
;
1091 md_estimate_size_before_relax(fragP
, segment_type
)
1092 register fragS
*fragP
;
1093 register segT segment_type
;
1095 printf("call tomd_estimate_size_before_relax \n"); abort(); }
1096 /* Put number into target byte order */
1098 void DEFUN(md_number_to_chars
,(ptr
, use
, nbytes
),
1104 case 4: *ptr
++ = (use
>> 24) & 0xff;
1105 case 3: *ptr
++ = (use
>> 16) & 0xff;
1106 case 2: *ptr
++ = (use
>> 8) & 0xff;
1107 case 1: *ptr
++ = (use
>> 0) & 0xff;
1113 long md_pcrel_from(fixP
)
1114 fixS
*fixP
; { abort(); }
1116 void tc_coff_symbol_emit_hook() { }
1119 void tc_reloc_mangle(fix_ptr
, intr
, base
)
1121 struct internal_reloc
*intr
;
1125 symbolS
*symbol_ptr
;
1127 symbol_ptr
= fix_ptr
->fx_addsy
;
1129 /* If this relocation is attached to a symbol then it's ok
1131 if (fix_ptr
->fx_r_type
== RELOC_32
) {
1132 /* cons likes to create reloc32's whatever the size of the reloc..
1134 switch (fix_ptr
->fx_size
)
1138 intr
->r_type
= R_DA
;
1141 intr
->r_type
= R_IMM8
;
1150 intr
->r_type
= fix_ptr
->fx_r_type
;
1153 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+base
;
1154 intr
->r_offset
= fix_ptr
->fx_offset
;
1157 intr
->r_symndx
= symbol_ptr
->sy_number
;
1159 intr
->r_symndx
= -1;