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"
39 char line_separator_chars
[]=
42 const char comment_chars
[] = { '!',0 };
43 const char line_separator_chars
[] = { ';' ,0};
44 const char line_comment_chars
[] = "";
48 extern int coff_flags
;
52 /* This table describes all the machine specific pseudo-ops the assembler
53 has to support. The fields are:
54 pseudo-op name without dot
55 function to call to execute this pseudo-op
56 Integer arg to pass to the function
66 machine
= bfd_mach_z8001
;
74 machine
= bfd_mach_z8002
;
77 const pseudo_typeS md_pseudo_table
[]=
83 {"form", listing_psize
, 0},
84 {"heading", listing_title
, 0},
85 {"import", s_ignore
, 0},
86 {"page", listing_eject
, 0},
87 {"program", s_ignore
, 0},
89 {"z8002", s_unseg
, 0},
94 const char EXP_CHARS
[]= "eE";
96 /* Chars that mean this number is a floating point constant */
100 char FLT_CHARS
[]= "rRsSfFdDxXpP";
102 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
106 const relax_typeS md_relax_table
[1];
109 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
116 opcode_entry_type
*opcode
;
117 char *prev_name
= "";
120 opcode_hash_control
= hash_new ();
123 for (opcode
= z8k_table
; opcode
->name
; opcode
++)
125 /* Only enter unique codes into the table */
126 char *src
= opcode
->name
;
128 if (strcmp (opcode
->name
, prev_name
))
130 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
134 prev_name
= opcode
->name
;
137 /* default to z8002 */
148 typedef struct z8k_op
150 char regsize
; /* 'b','w','r','q' */
151 unsigned int reg
; /* 0..15 */
155 unsigned int x_reg
; /* any other register associated with the mode */
156 expressionS exp
; /* any expression */
161 static expressionS
*da_operand
;
162 static expressionS
*imm_operand
;
169 DEFUN (whatreg
, (reg
, src
),
173 if (isdigit (src
[1]))
175 *reg
= (src
[0] - '0') * 10 + src
[1] - '0';
180 *reg
= (src
[0] - '0');
192 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
202 /* try and parse a reg name, returns number of chars consumed */
204 DEFUN (parse_reg
, (src
, mode
, reg
),
215 *mode
= CLASS_REG_LONG
;
216 res
= whatreg (reg
, src
+ 2);
218 else if (src
[1] == 'h')
220 *mode
= CLASS_REG_BYTE
;
221 res
= whatreg (reg
, src
+ 2);
223 else if (src
[1] == 'l')
225 *mode
= CLASS_REG_BYTE
;
226 res
= whatreg (reg
, src
+ 2) ;
229 else if (src
[1] == 'q')
231 *mode
= CLASS_REG_QUAD
;
232 res
= whatreg (reg
, src
+ 2);
236 *mode
= CLASS_REG_WORD
;
237 res
= whatreg (reg
, src
+ 1);
246 DEFUN (parse_exp
, (s
, op
),
250 char *save
= input_line_pointer
;
254 input_line_pointer
= s
;
256 new = input_line_pointer
;
257 input_line_pointer
= save
;
258 if (SEG_NORMAL (seg
))
269 as_bad ("Missing operand");
272 as_bad ("Don't understand operand of type %s", segment_name (seg
));
277 /* The many forms of operand:
296 DEFUN (checkfor
, (ptr
, what
),
304 as_bad ("expected %c", what
);
309 /* Make sure the mode supplied is the size of a word */
311 DEFUN (regword
, (mode
, string
),
320 as_bad ("register is wrong size for a word %s", string
);
324 /* Make sure the mode supplied is the size of an address */
326 DEFUN (regaddr
, (mode
, string
),
332 ok
= segmented_mode
? CLASS_REG_LONG
: CLASS_REG_WORD
;
335 as_bad ("register is wrong size for address %s", string
);
347 struct cc_names table
[]=
375 DEFUN (get_cc_operand
, (ptr
, mode
, dst
),
377 struct z8k_op
*mode AND
387 mode
->mode
= CLASS_CC
;
388 for (i
= 0; table
[i
].name
; i
++)
392 for (j
= 0; table
[i
].name
[j
]; j
++)
394 if (table
[i
].name
[j
] != src
[j
])
397 the_cc
= table
[i
].value
;
407 DEFUN (get_operand
, (ptr
, mode
, dst
),
409 struct z8k_op
*mode AND
425 mode
->mode
= CLASS_IMM
;
426 imm_operand
= &(mode
->exp
);
427 src
= parse_exp (src
+ 1, &(mode
->exp
));
429 else if (*src
== '@')
433 mode
->mode
= CLASS_IR
;
434 src
= parse_reg (src
+ 1, &d
, &mode
->reg
);
440 end
= parse_reg (src
, &mode
->mode
, ®n
);
450 end
= parse_reg (src
, &nw
, &nr
);
458 as_bad ("Missing ) in ra(rb)");
465 regaddr (mode
->mode
, "ra(rb) ra");
466 regword (mode
->mode
, "ra(rb) rb");
467 mode
->mode
= CLASS_BX
;
477 src
= parse_exp (src
, &(mode
->exp
));
478 src
= checkfor (src
, ')');
479 mode
->mode
= CLASS_BA
;
482 imm_operand
= &(mode
->exp
);
494 src
= parse_exp (src
, &(mode
->exp
));
498 end
= parse_reg (src
, &(mode
->mode
), ®n
);
499 regword (mode
->mode
, "addr(Ra) ra");
500 mode
->mode
= CLASS_X
;
503 da_operand
= &(mode
->exp
);
504 src
= checkfor (end
, ')');
508 /* Just an address */
509 mode
->mode
= CLASS_DA
;
512 da_operand
= &(mode
->exp
);
521 DEFUN (get_operands
, (opcode
, op_end
, operand
),
522 opcode_entry_type
* opcode AND
528 switch (opcode
->noperands
)
537 if (opcode
->arg_info
[0] == CLASS_CC
)
539 get_cc_operand (&ptr
, operand
+ 0, 0);
544 get_operand (&ptr
, operand
+ 0, 0);
551 if (opcode
->arg_info
[0] == CLASS_CC
)
553 get_cc_operand (&ptr
, operand
+ 0, 0);
558 get_operand (&ptr
, operand
+ 0, 0);
562 get_operand (&ptr
, operand
+ 1, 1);
567 get_operand (&ptr
, operand
+ 0, 0);
570 get_operand (&ptr
, operand
+ 1, 1);
573 get_operand (&ptr
, operand
+ 2, 2);
578 get_operand (&ptr
, operand
+ 0, 0);
581 get_operand (&ptr
, operand
+ 1, 1);
584 get_operand (&ptr
, operand
+ 2, 2);
587 get_cc_operand (&ptr
, operand
+ 3, 3);
597 /* Passed a pointer to a list of opcodes which use different
598 addressing modes, return the opcode which matches the opcodes
607 DEFUN (get_specific
, (opcode
, operands
),
608 opcode_entry_type
* opcode AND
612 opcode_entry_type
*this_try
= opcode
;
614 unsigned int noperands
= opcode
->noperands
;
616 unsigned int dispreg
;
617 unsigned int this_index
= opcode
->idx
;
619 while (this_index
== opcode
->idx
&& !found
)
624 for (i
= 0; i
< noperands
; i
++)
626 int mode
= operands
[i
].mode
;
628 if ((mode
& CLASS_MASK
) != (this_try
->arg_info
[i
] & CLASS_MASK
))
630 /* it could be an pc rel operand, if this is a da mode and
631 we like disps, then insert it */
633 if (mode
== CLASS_DA
&& this_try
->arg_info
[i
] == CLASS_DISP
)
635 /* This is the case */
636 operands
[i
].mode
= CLASS_DISP
;
638 else if (mode
== CLASS_BA
&& this_try
->arg_info
[i
])
640 /* Can't think of a way to turn what we've been given into
641 something that's ok */
646 switch (mode
& CLASS_MASK
)
661 reg
[this_try
->arg_info
[i
] & ARG_MASK
] = operands
[i
].reg
;
676 DEFUN (check_operand
, (operand
, width
, string
),
677 struct z8k_op
*operand AND
678 unsigned int width AND
681 if (operand
->exp
.X_add_symbol
== 0
682 && operand
->exp
.X_subtract_symbol
== 0)
685 /* No symbol involved, let's look at offset, it's dangerous if any of
686 the high bits are not 0 or ff's, find out by oring or anding with
687 the width and seeing if the answer is 0 or all fs*/
688 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
689 (operand
->exp
.X_add_number
| width
) != (~0))
691 as_warn ("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
697 static char buffer
[20];
700 DEFUN (newfix
, (ptr
, type
, operand
),
703 expressionS
* operand
)
705 if (operand
->X_add_symbol
706 || operand
->X_subtract_symbol
707 || operand
->X_add_number
)
712 operand
->X_add_symbol
,
713 operand
->X_subtract_symbol
,
714 operand
->X_add_number
,
721 DEFUN (apply_fix
,(ptr
, type
, operand
, size
),
724 expressionS
*operand AND
727 int n
= operand
->X_add_number
;
728 operand
->X_add_number
= n
;
729 newfix((ptr
- buffer
)/2, type
, operand
);
732 case 8: /* 8 nibbles == 32 bits */
737 case 4: /* 4 niblles == 16 bits */
752 /* Now we know what sort of opcodes it is, lets build the bytes -
754 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
756 DEFUN (build_bytes
, (this_try
, operand
),
757 opcode_entry_type
* this_try AND
758 struct z8k_op
*operand
)
764 char *output_ptr
= buffer
;
770 unsigned short *class_ptr
;
771 frag_wane (frag_now
);
774 memset (buffer
, 20, 0);
775 class_ptr
= this_try
->byte_info
;
778 for (nibble
= 0; c
= *class_ptr
++; nibble
++)
781 switch (c
& CLASS_MASK
)
787 /* Direct address, we don't cope with the SS mode right now */
790 output_ptr
= apply_fix (output_ptr
, R_DA
| R_SEG
, da_operand
, 8);
794 output_ptr
= apply_fix(output_ptr
, R_DA
, da_operand
, 4);
800 output_ptr
= apply_fix (output_ptr
, R_JR
, da_operand
, 2);
806 *output_ptr
++ = the_cc
;
809 *output_ptr
++ = c
& 0xf;
812 if (reg
[c
& 0xf] == 0)
814 as_bad ("can't use R0 here");
821 /* Insert bit mattern of
823 *output_ptr
++ = reg
[c
& 0xf];
826 output_ptr
= apply_fix (output_ptr
, R_DA
, da_operand
, 4);
832 switch (c
& ARG_MASK
)
835 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
838 imm_operand
->X_add_number
--;
839 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
842 imm_operand
->X_add_number
--;
843 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
,1);
846 imm_operand
->X_add_number
= -imm_operand
->X_add_number
;
848 output_ptr
= apply_fix (output_ptr
, R_IMM8
, imm_operand
, 2);
853 output_ptr
= apply_fix(output_ptr
, R_DA
, imm_operand
, 4);
857 output_ptr
= apply_fix (output_ptr
, R_IMM32
, imm_operand
, 8);
867 /* Copy from the nibble buffer into the frag */
870 int length
= (output_ptr
- buffer
) / 2;
872 char *fragp
= frag_more (length
);
874 while (src
< output_ptr
)
876 *fragp
= (src
[0] << 4) | src
[1];
886 /* This is the guts of the machine-dependent assembler. STR points to a
887 machine dependent instruction. This funciton is supposed to emit
888 the frags/bytes it assembles to.
894 DEFUN (md_assemble
, (str
),
900 struct z8k_op operand
[3];
901 opcode_entry_type
*opcode
;
902 opcode_entry_type
*prev_opcode
;
907 /* Drop leading whitespace */
911 /* find the op code end */
912 for (op_start
= op_end
= str
;
913 *op_end
!= 0 && *op_end
!= ' ';
920 if (op_end
== op_start
)
922 as_bad ("can't find opcode ");
928 opcode
= (opcode_entry_type
*) hash_find (opcode_hash_control
,
933 as_bad ("unknown opcode");
938 input_line_pointer
= get_operands (opcode
, op_end
,
941 prev_opcode
= opcode
;
943 opcode
= get_specific (opcode
, operand
);
947 /* Couldn't find an opcode which matched the operands */
948 char *where
= frag_more (2);
953 as_bad ("Can't find opcode to match operands");
957 build_bytes (opcode
, operand
);
961 DEFUN (tc_crawl_symbol_chain
, (headers
),
962 object_headers
* headers
)
964 printf ("call to tc_crawl_symbol_chain \n");
968 DEFUN (md_undefined_symbol
, (name
),
975 DEFUN (tc_headers_hook
, (headers
),
976 object_headers
* headers
)
978 printf ("call to tc_headers_hook \n");
986 /* Various routines to kill one day */
987 /* Equal to MAX_PRECISION in atof-ieee.c */
988 #define MAX_LITTLENUMS 6
990 /* Turn a string in input_line_pointer into a floating point constant of type
991 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
992 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
995 md_atof (type
, litP
, sizeP
)
1001 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1002 LITTLENUM_TYPE
*wordP
;
1034 return "Bad call to MD_ATOF()";
1036 t
= atof_ieee (input_line_pointer
, type
, words
);
1038 input_line_pointer
= t
;
1040 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1041 for (wordP
= words
; prec
--;)
1043 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1044 litP
+= sizeof (LITTLENUM_TYPE
);
1046 return ""; /* Someone should teach Dean about null pointers */
1050 md_parse_option (argP
, cntP
, vecP
)
1060 int md_short_jump_size
;
1063 tc_aout_fix_to_chars ()
1065 printf ("call to tc_aout_fix_to_chars \n");
1070 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1077 as_fatal ("failed sanity check.");
1081 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1083 long from_addr
, to_addr
;
1087 as_fatal ("failed sanity check.");
1091 md_convert_frag (headers
, fragP
)
1092 object_headers
*headers
;
1096 printf ("call to md_convert_frag \n");
1101 DEFUN (md_section_align
, (seg
, size
),
1105 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1110 md_apply_fix (fixP
, val
)
1114 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1116 switch (fixP
->fx_r_type
)
1119 buf
[0] = (buf
[0] & 0xf0) | ((buf
[0] + val
) & 0xf);
1125 /* if (val != 0) abort();*/
1134 *buf
++ = (val
>> 8);
1138 *buf
++ = (val
>> 24);
1139 *buf
++ = (val
>> 16);
1140 *buf
++ = (val
>> 8);
1144 *buf
++ = (val
>> 16);
1146 *buf
++ = (val
>> 8);
1156 DEFUN (md_operand
, (expressionP
), expressionS
* expressionP
)
1160 int md_long_jump_size
;
1162 md_estimate_size_before_relax (fragP
, segment_type
)
1163 register fragS
*fragP
;
1164 register segT segment_type
;
1166 printf ("call tomd_estimate_size_before_relax \n");
1170 /* Put number into target byte order */
1173 DEFUN (md_number_to_chars
, (ptr
, use
, nbytes
),
1180 case 4:*ptr
++ = (use
>> 24) & 0xff;
1182 *ptr
++ = (use
>> 16) & 0xff;
1184 *ptr
++ = (use
>> 8) & 0xff;
1186 *ptr
++ = (use
>> 0) & 0xff;
1193 md_pcrel_from (fixP
)
1200 tc_coff_symbol_emit_hook ()
1206 tc_reloc_mangle (fix_ptr
, intr
, base
)
1208 struct internal_reloc
*intr
;
1212 symbolS
*symbol_ptr
;
1214 symbol_ptr
= fix_ptr
->fx_addsy
;
1216 /* If this relocation is attached to a symbol then it's ok
1218 if (fix_ptr
->fx_r_type
== 0)
1220 /* cons likes to create reloc32's whatever the size of the reloc..
1222 switch (fix_ptr
->fx_size
)
1226 intr
->r_type
= R_DA
;
1229 intr
->r_type
= R_IMM8
;
1239 intr
->r_type
= fix_ptr
->fx_r_type
;
1242 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1243 intr
->r_offset
= fix_ptr
->fx_offset
;
1246 intr
->r_symndx
= symbol_ptr
->sy_number
;
1248 intr
->r_symndx
= -1;