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. */
21 Written By Steve Chamberlain
27 #include "../opcodes/z8k-opc.h"
35 const char comment_chars
[] =
37 const char line_separator_chars
[] =
39 const char line_comment_chars
[] = { '#', 0};
42 extern int coff_flags
;
46 /* This table describes all the machine specific pseudo-ops the assembler
47 has to support. The fields are:
48 pseudo-op name without dot
49 function to call to execute this pseudo-op
50 Integer arg to pass to the function
59 machine
= bfd_mach_z8001
;
67 machine
= bfd_mach_z8002
;
75 record_alignment(now_seg
,1);
77 void obj_coff_section();
82 if (isdigit(c
)) return c
- '0';
83 if (islower(c
)) return c
- 'a' + 10;
90 if (*input_line_pointer
== '\'') {
93 c
= *input_line_pointer
++;
96 c
= (tohex(input_line_pointer
[0]) << 4)
97 | tohex(input_line_pointer
[1]);
98 input_line_pointer
+=2;
100 FRAG_APPEND_1_CHAR(c
);
101 c
= *input_line_pointer
++;
103 demand_empty_rest_of_line();
107 const pseudo_typeS md_pseudo_table
[] =
113 {"form", listing_psize
, 0},
114 {"heading", listing_title
, 0},
115 {"import", s_ignore
, 0},
116 {"page", listing_eject
, 0},
117 {"program", s_ignore
, 0},
118 {"z8001", s_segm
, 0},
119 {"z8002", s_unseg
, 0},
123 {"unsegm", s_unseg
, 0},
124 {"name", s_app_file
, 0},
125 {"global",s_globl
,0},
130 {"rsect",obj_coff_section
,0},
131 {"sect",obj_coff_section
,0},
137 const char EXP_CHARS
[] = "eE";
139 /* Chars that mean this number is a floating point constant */
142 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
144 const relax_typeS md_relax_table
[1];
146 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
151 opcode_entry_type
*opcode
;
152 char *prev_name
= "";
155 opcode_hash_control
= hash_new ();
157 for (opcode
= z8k_table
; opcode
->name
; opcode
++)
159 /* Only enter unique codes into the table */
160 char *src
= opcode
->name
;
162 if (strcmp (opcode
->name
, prev_name
))
164 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
168 prev_name
= opcode
->name
;
171 /* default to z8002 */
174 /* insert the pseudo ops too */
175 for (idx
= 0; md_pseudo_table
[idx
].poc_name
; idx
++)
177 opcode_entry_type
*fake_opcode
;
178 fake_opcode
= (opcode_entry_type
*)malloc(sizeof(opcode_entry_type
));
179 fake_opcode
->name
= md_pseudo_table
[idx
].poc_name
,
180 fake_opcode
->func
= (void *)(md_pseudo_table
+idx
);
181 fake_opcode
->opcode
= 250;
183 hash_insert(opcode_hash_control
,fake_opcode
->name
,fake_opcode
);
194 typedef struct z8k_op
196 char regsize
; /* 'b','w','r','q' */
197 unsigned int reg
; /* 0..15 */
201 unsigned int x_reg
; /* any other register associated with the mode */
202 expressionS exp
; /* any expression */
207 static expressionS
*da_operand
;
208 static expressionS
*imm_operand
;
214 DEFUN (whatreg
, (reg
, src
),
218 if (isdigit (src
[1]))
220 *reg
= (src
[0] - '0') * 10 + src
[1] - '0';
225 *reg
= (src
[0] - '0');
237 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
246 /* try and parse a reg name, returns number of chars consumed */
248 DEFUN (parse_reg
, (src
, mode
, reg
),
255 if (src
[0] == 's' && src
[1]=='p') {
256 if (segmented_mode
) {
257 *mode
= CLASS_REG_LONG
;
262 *mode
= CLASS_REG_WORD
;
271 *mode
= CLASS_REG_LONG
;
272 res
= whatreg (reg
, src
+ 2);
274 else if (src
[1] == 'h' )
276 *mode
= CLASS_REG_BYTE
;
277 res
= whatreg (reg
, src
+ 2) ;
279 else if (src
[1] == 'l' )
281 *mode
= CLASS_REG_BYTE
;
282 res
= whatreg (reg
, src
+ 2);
285 else if (src
[1] == 'q')
287 *mode
= CLASS_REG_QUAD
;
288 res
= whatreg (reg
, src
+ 2);
292 *mode
= CLASS_REG_WORD
;
293 res
= whatreg (reg
, src
+ 1);
301 DEFUN (parse_exp
, (s
, op
),
305 char *save
= input_line_pointer
;
309 input_line_pointer
= s
;
311 new = input_line_pointer
;
312 input_line_pointer
= save
;
313 if (SEG_NORMAL (seg
))
324 as_bad ("Missing operand");
327 as_bad ("Don't understand operand of type %s", segment_name (seg
));
332 /* The many forms of operand:
351 DEFUN (checkfor
, (ptr
, what
),
359 as_bad ("expected %c", what
);
364 /* Make sure the mode supplied is the size of a word */
366 DEFUN (regword
, (mode
, string
),
375 as_bad ("register is wrong size for a word %s", string
);
379 /* Make sure the mode supplied is the size of an address */
381 DEFUN (regaddr
, (mode
, string
),
387 ok
= segmented_mode
? CLASS_REG_LONG
: CLASS_REG_WORD
;
390 as_bad ("register is wrong size for address %s", string
);
401 struct cc_names table
[] =
429 DEFUN (get_cc_operand
, (ptr
, mode
, dst
),
431 struct z8k_op
*mode AND
441 mode
->mode
= CLASS_CC
;
442 for (i
= 0; table
[i
].name
; i
++)
446 for (j
= 0; table
[i
].name
[j
]; j
++)
448 if (table
[i
].name
[j
] != src
[j
])
451 the_cc
= table
[i
].value
;
461 DEFUN (get_operand
, (ptr
, mode
, dst
),
463 struct z8k_op
*mode AND
478 mode
->mode
= CLASS_IMM
;
479 imm_operand
= &(mode
->exp
);
480 src
= parse_exp (src
+ 1, &(mode
->exp
));
482 else if (*src
== '@')
486 mode
->mode
= CLASS_IR
;
487 src
= parse_reg (src
+ 1, &d
, &mode
->reg
);
493 end
= parse_reg (src
, &mode
->mode
, ®n
);
503 end
= parse_reg (src
, &nw
, &nr
);
511 as_bad ("Missing ) in ra(rb)");
518 regaddr (mode
->mode
, "ra(rb) ra");
519 regword (mode
->mode
, "ra(rb) rb");
520 mode
->mode
= CLASS_BX
;
530 src
= parse_exp (src
, &(mode
->exp
));
531 src
= checkfor (src
, ')');
532 mode
->mode
= CLASS_BA
;
535 imm_operand
= &(mode
->exp
);
547 src
= parse_exp (src
, &(mode
->exp
));
551 end
= parse_reg (src
, &(mode
->mode
), ®n
);
552 regword (mode
->mode
, "addr(Ra) ra");
553 mode
->mode
= CLASS_X
;
556 da_operand
= &(mode
->exp
);
557 src
= checkfor (end
, ')');
561 /* Just an address */
562 mode
->mode
= CLASS_DA
;
565 da_operand
= &(mode
->exp
);
574 DEFUN (get_operands
, (opcode
, op_end
, operand
),
575 opcode_entry_type
* opcode AND
581 switch (opcode
->noperands
)
590 if (opcode
->arg_info
[0] == CLASS_CC
)
592 get_cc_operand (&ptr
, operand
+ 0, 0);
597 get_operand (&ptr
, operand
+ 0, 0);
604 if (opcode
->arg_info
[0] == CLASS_CC
)
606 get_cc_operand (&ptr
, operand
+ 0, 0);
611 get_operand (&ptr
, operand
+ 0, 0);
617 get_operand (&ptr
, operand
+ 1, 1);
622 get_operand (&ptr
, operand
+ 0, 0);
625 get_operand (&ptr
, operand
+ 1, 1);
628 get_operand (&ptr
, operand
+ 2, 2);
633 get_operand (&ptr
, operand
+ 0, 0);
636 get_operand (&ptr
, operand
+ 1, 1);
639 get_operand (&ptr
, operand
+ 2, 2);
642 get_cc_operand (&ptr
, operand
+ 3, 3);
651 /* Passed a pointer to a list of opcodes which use different
652 addressing modes, return the opcode which matches the opcodes
658 DEFUN (get_specific
, (opcode
, operands
),
659 opcode_entry_type
* opcode AND
663 opcode_entry_type
*this_try
= opcode
;
665 unsigned int noperands
= opcode
->noperands
;
667 unsigned int dispreg
;
668 unsigned int this_index
= opcode
->idx
;
670 while (this_index
== opcode
->idx
&& !found
)
675 for (i
= 0; i
< noperands
; i
++)
677 int mode
= operands
[i
].mode
;
679 if ((mode
& CLASS_MASK
) != (this_try
->arg_info
[i
] & CLASS_MASK
))
681 /* it could be an pc rel operand, if this is a da mode and
682 we like disps, then insert it */
684 if (mode
== CLASS_DA
&& this_try
->arg_info
[i
] == CLASS_DISP
)
686 /* This is the case */
687 operands
[i
].mode
= CLASS_DISP
;
689 else if (mode
== CLASS_BA
&& this_try
->arg_info
[i
])
691 /* Can't think of a way to turn what we've been given into
692 something that's ok */
698 switch (mode
& CLASS_MASK
)
713 reg
[this_try
->arg_info
[i
] & ARG_MASK
] = operands
[i
].reg
;
728 DEFUN (check_operand
, (operand
, width
, string
),
729 struct z8k_op
*operand AND
730 unsigned int width AND
733 if (operand
->exp
.X_add_symbol
== 0
734 && operand
->exp
.X_subtract_symbol
== 0)
737 /* No symbol involved, let's look at offset, it's dangerous if any of
738 the high bits are not 0 or ff's, find out by oring or anding with
739 the width and seeing if the answer is 0 or all fs*/
740 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
741 (operand
->exp
.X_add_number
| width
) != (~0))
743 as_warn ("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
749 static char buffer
[20];
752 DEFUN (newfix
, (ptr
, type
, operand
),
755 expressionS
* operand
)
757 if (operand
->X_add_symbol
758 || operand
->X_subtract_symbol
759 || operand
->X_add_number
)
764 operand
->X_add_symbol
,
765 operand
->X_subtract_symbol
,
766 operand
->X_add_number
,
773 DEFUN (apply_fix
, (ptr
, type
, operand
, size
),
776 expressionS
* operand AND
779 int n
= operand
->X_add_number
;
781 operand
->X_add_number
= n
;
782 newfix ((ptr
- buffer
) / 2, type
, operand
);
786 case 8: /* 8 nibbles == 32 bits */
791 case 4: /* 4 niblles == 16 bits */
805 /* Now we know what sort of opcodes it is, lets build the bytes -
807 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
809 DEFUN (build_bytes
, (this_try
, operand
),
810 opcode_entry_type
* this_try AND
811 struct z8k_op
*operand
)
817 char *output_ptr
= buffer
;
823 unsigned short *class_ptr
;
825 frag_wane (frag_now
);
828 memset (buffer
, 20, 0);
829 class_ptr
= this_try
->byte_info
;
832 for (nibble
= 0; c
= *class_ptr
++; nibble
++)
835 switch (c
& CLASS_MASK
)
841 /* Direct address, we don't cope with the SS mode right now */
844 da_operand
->X_add_number
|= 0x80000000;
845 output_ptr
= apply_fix (output_ptr
, R_IMM32
, da_operand
, 8);
849 output_ptr
= apply_fix (output_ptr
, R_IMM16
, da_operand
, 4);
855 output_ptr
= apply_fix (output_ptr
, R_JR
, da_operand
, 2);
860 *output_ptr
= c
& 0xf;
863 if (imm_operand
->X_add_number
==2)
867 else if (imm_operand
->X_add_number
!= 1)
869 as_bad("immediate must be 1 or 2");
874 as_bad("immediate 1 or 2 expected");
879 *output_ptr
++ = the_cc
;
882 *output_ptr
++ = c
& 0xf;
885 if (reg
[c
& 0xf] == 0)
887 as_bad ("can't use R0 here");
894 /* Insert bit mattern of
896 *output_ptr
++ = reg
[c
& 0xf];
899 output_ptr
= apply_fix (output_ptr
, R_IMM16
, da_operand
, 4);
906 switch (c
& ARG_MASK
)
909 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
912 imm_operand
->X_add_number
--;
913 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
916 imm_operand
->X_add_number
--;
917 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
920 imm_operand
->X_add_number
= -imm_operand
->X_add_number
;
922 output_ptr
= apply_fix (output_ptr
, R_IMM8
, imm_operand
, 2);
925 output_ptr
= apply_fix (output_ptr
, R_IMM16
, imm_operand
, 4);
929 output_ptr
= apply_fix (output_ptr
, R_IMM32
, imm_operand
, 8);
939 /* Copy from the nibble buffer into the frag */
942 int length
= (output_ptr
- buffer
) / 2;
944 char *fragp
= frag_more (length
);
946 while (src
< output_ptr
)
948 *fragp
= (src
[0] << 4) | src
[1];
957 /* This is the guts of the machine-dependent assembler. STR points to a
958 machine dependent instruction. This funciton is supposed to emit
959 the frags/bytes it assembles to.
963 DEFUN (md_assemble
, (str
),
969 struct z8k_op operand
[3];
970 opcode_entry_type
*opcode
;
971 opcode_entry_type
*prev_opcode
;
976 /* Drop leading whitespace */
980 /* find the op code end */
981 for (op_start
= op_end
= str
;
982 *op_end
!= 0 && *op_end
!= ' ';
989 if (op_end
== op_start
)
991 as_bad ("can't find opcode ");
997 opcode
= (opcode_entry_type
*) hash_find (opcode_hash_control
,
1003 as_bad ("unknown opcode");
1007 if (opcode
->opcode
== 250)
1009 /* was really a pseudo op */
1014 char *old
= input_line_pointer
;
1018 input_line_pointer
= op_end
;
1022 while (*input_line_pointer
== ' ')
1023 input_line_pointer
++;
1024 p
= (pseudo_typeS
*)(opcode
->func
);
1026 (p
->poc_handler
)(p
->poc_val
);
1027 input_line_pointer
= old
;
1031 input_line_pointer
= get_operands (opcode
, op_end
,
1034 prev_opcode
= opcode
;
1036 opcode
= get_specific (opcode
, operand
);
1040 /* Couldn't find an opcode which matched the operands */
1041 char *where
= frag_more (2);
1046 as_bad ("Can't find opcode to match operands");
1050 build_bytes (opcode
, operand
);
1055 DEFUN (tc_crawl_symbol_chain
, (headers
),
1056 object_headers
* headers
)
1058 printf ("call to tc_crawl_symbol_chain \n");
1062 DEFUN (md_undefined_symbol
, (name
),
1069 DEFUN (tc_headers_hook
, (headers
),
1070 object_headers
* headers
)
1072 printf ("call to tc_headers_hook \n");
1080 /* Various routines to kill one day */
1081 /* Equal to MAX_PRECISION in atof-ieee.c */
1082 #define MAX_LITTLENUMS 6
1084 /* Turn a string in input_line_pointer into a floating point constant of type
1085 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1086 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1089 md_atof (type
, litP
, sizeP
)
1095 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1096 LITTLENUM_TYPE
*wordP
;
1128 return "Bad call to MD_ATOF()";
1130 t
= atof_ieee (input_line_pointer
, type
, words
);
1132 input_line_pointer
= t
;
1134 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1135 for (wordP
= words
; prec
--;)
1137 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1138 litP
+= sizeof (LITTLENUM_TYPE
);
1140 return ""; /* Someone should teach Dean about null pointers */
1144 md_parse_option (argP
, cntP
, vecP
)
1150 if (!strcmp(*argP
,"z8001")) {
1153 else if (!strcmp(*argP
,"z8002")) {
1161 int md_short_jump_size
;
1164 tc_aout_fix_to_chars ()
1166 printf ("call to tc_aout_fix_to_chars \n");
1171 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1178 as_fatal ("failed sanity check.");
1182 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1184 long from_addr
, to_addr
;
1188 as_fatal ("failed sanity check.");
1192 md_convert_frag (headers
, fragP
)
1193 object_headers
*headers
;
1197 printf ("call to md_convert_frag \n");
1202 DEFUN (md_section_align
, (seg
, size
),
1206 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1211 md_apply_fix (fixP
, val
)
1215 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1217 switch (fixP
->fx_r_type
)
1220 buf
[0] = (buf
[0] & 0xf0) | ((buf
[0] + val
) & 0xf);
1226 /* if (val != 0) abort();*/
1233 *buf
++ = (val
>> 8);
1237 *buf
++ = (val
>> 24);
1238 *buf
++ = (val
>> 16);
1239 *buf
++ = (val
>> 8);
1244 *buf
++ = (val
>> 16);
1246 *buf
++ = (val
>> 8);
1258 DEFUN (md_operand
, (expressionP
), expressionS
* expressionP
)
1262 int md_long_jump_size
;
1264 md_estimate_size_before_relax (fragP
, segment_type
)
1265 register fragS
*fragP
;
1266 register segT segment_type
;
1268 printf ("call tomd_estimate_size_before_relax \n");
1272 /* Put number into target byte order */
1275 DEFUN (md_number_to_chars
, (ptr
, use
, nbytes
),
1283 *ptr
++ = (use
>> 24) & 0xff;
1285 *ptr
++ = (use
>> 16) & 0xff;
1287 *ptr
++ = (use
>> 8) & 0xff;
1289 *ptr
++ = (use
>> 0) & 0xff;
1296 md_pcrel_from (fixP
)
1303 tc_coff_symbol_emit_hook ()
1308 tc_reloc_mangle (fix_ptr
, intr
, base
)
1310 struct internal_reloc
*intr
;
1314 symbolS
*symbol_ptr
;
1316 symbol_ptr
= fix_ptr
->fx_addsy
;
1318 /* If this relocation is attached to a symbol then it's ok
1320 if (fix_ptr
->fx_r_type
== 0)
1322 /* cons likes to create reloc32's whatever the size of the reloc..
1324 switch (fix_ptr
->fx_size
)
1328 intr
->r_type
= R_IMM16
;
1331 intr
->r_type
= R_IMM8
;
1334 intr
->r_type
= R_IMM32
;
1344 intr
->r_type
= fix_ptr
->fx_r_type
;
1347 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1348 intr
->r_offset
= fix_ptr
->fx_offset
;
1351 intr
->r_symndx
= symbol_ptr
->sy_number
;
1353 intr
->r_symndx
= -1;