1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
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)
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.
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
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
27 #include "opcodes/z8k-opc.h"
33 const char comment_chars
[] = "!";
34 const char line_comment_chars
[] = "#";
35 const char line_separator_chars
[] = ";";
38 extern int coff_flags
;
40 const int md_reloc_size
;
48 machine
= bfd_mach_z8001
;
56 machine
= bfd_mach_z8002
;
64 record_alignment (now_seg
, 1);
67 void obj_coff_section ();
84 if (*input_line_pointer
== '\'')
88 c
= *input_line_pointer
++;
93 c
= (tohex (input_line_pointer
[0]) << 4)
94 | tohex (input_line_pointer
[1]);
95 input_line_pointer
+= 2;
97 FRAG_APPEND_1_CHAR (c
);
98 c
= *input_line_pointer
++;
100 demand_empty_rest_of_line ();
104 /* This table describes all the machine specific pseudo-ops the assembler
105 has to support. The fields are:
106 pseudo-op name without dot
107 function to call to execute this pseudo-op
108 Integer arg to pass to the function
111 const pseudo_typeS md_pseudo_table
[] = {
113 {"data.b" , cons
, 1},
114 {"data.w" , cons
, 2},
115 {"data.l" , cons
, 4},
116 {"form" , listing_psize
, 0},
117 {"heading", listing_title
, 0},
118 {"import" , s_ignore
, 0},
119 {"page" , listing_eject
, 0},
120 {"program", s_ignore
, 0},
121 {"z8001" , s_segm
, 0},
122 {"z8002" , s_unseg
, 0},
124 {"segm" , s_segm
, 0},
125 {"unsegm" , s_unseg
, 0},
126 {"unseg" , s_unseg
, 0},
127 {"name" , s_app_file
, 0},
128 {"global" , s_globl
, 0},
133 {"rsect" , obj_coff_section
, 0},
134 {"sect" , obj_coff_section
, 0},
135 {"block" , s_space
, 0},
140 const char EXP_CHARS
[] = "eE";
142 /* Chars that mean this number is a floating point constant.
145 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
147 /* Opcode mnemonics. */
148 static struct hash_control
*opcode_hash_control
;
153 opcode_entry_type
*opcode
;
154 char *prev_name
= "";
157 opcode_hash_control
= hash_new ();
159 for (opcode
= z8k_table
; opcode
->name
; opcode
++)
161 /* Only enter unique codes into the table. */
162 char *src
= opcode
->name
;
164 if (strcmp (opcode
->name
, prev_name
))
166 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
170 prev_name
= opcode
->name
;
173 /* Default to z8002. */
176 /* Insert the pseudo ops, too. */
177 for (idx
= 0; md_pseudo_table
[idx
].poc_name
; idx
++)
179 opcode_entry_type
*fake_opcode
;
180 fake_opcode
= (opcode_entry_type
*) malloc (sizeof (opcode_entry_type
));
181 fake_opcode
->name
= md_pseudo_table
[idx
].poc_name
,
182 fake_opcode
->func
= (void *) (md_pseudo_table
+ idx
);
183 fake_opcode
->opcode
= 250;
184 hash_insert (opcode_hash_control
, fake_opcode
->name
, fake_opcode
);
196 typedef struct z8k_op
{
197 /* 'b','w','r','q'. */
205 /* Any other register associated with the mode. */
208 /* Any expression. */
212 static expressionS
*da_operand
;
213 static expressionS
*imm_operand
;
226 if (isdigit (src
[1]))
228 *reg
= (src
[0] - '0') * 10 + src
[1] - '0';
233 *reg
= (src
[0] - '0');
244 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
252 /* Try to parse a reg name. Return a pointer to the first character
253 in SRC after the reg name. */
256 parse_reg (src
, mode
, reg
)
264 if (src
[0] == 's' && src
[1] == 'p')
268 *mode
= CLASS_REG_LONG
;
273 *mode
= CLASS_REG_WORD
;
282 *mode
= CLASS_REG_LONG
;
283 res
= whatreg (reg
, src
+ 2);
286 as_warn (_("register rr%d, out of range."), regno
);
288 else if (src
[1] == 'h')
290 *mode
= CLASS_REG_BYTE
;
291 res
= whatreg (reg
, src
+ 2);
294 as_warn (_("register rh%d, out of range."), regno
);
296 else if (src
[1] == 'l')
298 *mode
= CLASS_REG_BYTE
;
299 res
= whatreg (reg
, src
+ 2);
302 as_warn (_("register rl%d, out of range."), regno
);
305 else if (src
[1] == 'q')
307 *mode
= CLASS_REG_QUAD
;
308 res
= whatreg (reg
, src
+ 2);
311 as_warn (_("register rq%d, out of range."), regno
);
315 *mode
= CLASS_REG_WORD
;
316 res
= whatreg (reg
, src
+ 1);
319 as_warn (_("register r%d, out of range."), regno
);
330 char *save
= input_line_pointer
;
333 input_line_pointer
= s
;
335 if (op
->X_op
== O_absent
)
336 as_bad (_("missing operand"));
337 new = input_line_pointer
;
338 input_line_pointer
= save
;
342 /* The many forms of operand:
364 as_bad (_("expected %c"), what
);
369 /* Make sure the mode supplied is the size of a word. */
372 regword (mode
, string
)
381 as_bad (_("register is wrong size for a word %s"), string
);
385 /* Make sure the mode supplied is the size of an address. */
388 regaddr (mode
, string
)
394 ok
= segmented_mode
? CLASS_REG_LONG
: CLASS_REG_WORD
;
397 as_bad (_("register is wrong size for address %s"), string
);
406 struct ctrl_names ctrl_table
[] = {
419 get_ctrl_operand (ptr
, mode
, dst
)
431 mode
->mode
= CLASS_CTRL
;
432 for (i
= 0; ctrl_table
[i
].name
; i
++)
436 for (j
= 0; ctrl_table
[i
].name
[j
]; j
++)
438 if (ctrl_table
[i
].name
[j
] != src
[j
])
441 the_ctrl
= ctrl_table
[i
].value
;
457 struct flag_names flag_table
[] = {
468 get_flags_operand (ptr
, mode
, dst
)
481 mode
->mode
= CLASS_FLAGS
;
483 for (j
= 0; j
<= 9; j
++)
487 for (i
= 0; flag_table
[i
].name
; i
++)
489 if (flag_table
[i
].name
[0] == src
[j
])
491 the_flags
= the_flags
| flag_table
[i
].value
;
504 struct interrupt_names
{
510 struct interrupt_names intr_table
[] = {
519 get_interrupt_operand (ptr
, mode
, dst
)
531 mode
->mode
= CLASS_IMM
;
532 for (i
= 0; intr_table
[i
].name
; i
++)
536 for (j
= 0; intr_table
[i
].name
[j
]; j
++)
538 if (intr_table
[i
].name
[j
] != src
[j
])
541 the_interrupt
= intr_table
[i
].value
;
557 struct cc_names table
[] = {
584 get_cc_operand (ptr
, mode
, dst
)
596 mode
->mode
= CLASS_CC
;
597 for (i
= 0; table
[i
].name
; i
++)
601 for (j
= 0; table
[i
].name
[j
]; j
++)
603 if (table
[i
].name
[j
] != src
[j
])
606 the_cc
= table
[i
].value
;
616 get_operand (ptr
, mode
, dst
)
633 mode
->mode
= CLASS_IMM
;
634 imm_operand
= &(mode
->exp
);
635 src
= parse_exp (src
+ 1, &(mode
->exp
));
637 else if (*src
== '@')
641 mode
->mode
= CLASS_IR
;
642 src
= parse_reg (src
+ 1, &d
, &mode
->reg
);
648 end
= parse_reg (src
, &mode
->mode
, ®n
);
658 end
= parse_reg (src
, &nw
, &nr
);
665 as_bad (_("Missing ) in ra(rb)"));
669 regaddr (mode
->mode
, "ra(rb) ra");
671 regword (mode
->mode
, "ra(rb) rb");
673 mode
->mode
= CLASS_BX
;
683 src
= parse_exp (src
, &(mode
->exp
));
684 src
= checkfor (src
, ')');
685 mode
->mode
= CLASS_BA
;
688 imm_operand
= &(mode
->exp
);
699 /* No initial reg. */
700 src
= parse_exp (src
, &(mode
->exp
));
704 end
= parse_reg (src
, &(mode
->mode
), ®n
);
705 regword (mode
->mode
, "addr(Ra) ra");
706 mode
->mode
= CLASS_X
;
709 da_operand
= &(mode
->exp
);
710 src
= checkfor (end
, ')');
714 /* Just an address. */
715 mode
->mode
= CLASS_DA
;
718 da_operand
= &(mode
->exp
);
726 get_operands (opcode
, op_end
, operand
)
727 opcode_entry_type
*opcode
;
734 switch (opcode
->noperands
)
743 if (opcode
->arg_info
[0] == CLASS_CC
)
745 get_cc_operand (&ptr
, operand
+ 0, 0);
747 else if (opcode
->arg_info
[0] == CLASS_FLAGS
)
749 get_flags_operand (&ptr
, operand
+ 0, 0);
751 else if (opcode
->arg_info
[0] == (CLASS_IMM
+ (ARG_IMM2
)))
753 get_interrupt_operand (&ptr
, operand
+ 0, 0);
757 get_operand (&ptr
, operand
+ 0, 0);
765 if (opcode
->arg_info
[0] == CLASS_CC
)
767 get_cc_operand (&ptr
, operand
+ 0, 0);
769 else if (opcode
->arg_info
[0] == CLASS_CTRL
)
771 get_ctrl_operand (&ptr
, operand
+ 0, 0);
775 get_operand (&ptr
, operand
+ 0, 0);
780 get_ctrl_operand (&ptr
, operand
+ 1, 1);
786 get_operand (&ptr
, operand
+ 0, 0);
792 get_operand (&ptr
, operand
+ 1, 1);
797 get_operand (&ptr
, operand
+ 0, 0);
800 get_operand (&ptr
, operand
+ 1, 1);
803 get_operand (&ptr
, operand
+ 2, 2);
808 get_operand (&ptr
, operand
+ 0, 0);
811 get_operand (&ptr
, operand
+ 1, 1);
814 get_operand (&ptr
, operand
+ 2, 2);
817 get_cc_operand (&ptr
, operand
+ 3, 3);
827 /* Passed a pointer to a list of opcodes which use different
828 addressing modes. Return the opcode which matches the opcodes
831 static opcode_entry_type
*
832 get_specific (opcode
, operands
)
833 opcode_entry_type
*opcode
;
837 opcode_entry_type
*this_try
= opcode
;
839 unsigned int noperands
= opcode
->noperands
;
841 unsigned int dispreg
;
842 unsigned int this_index
= opcode
->idx
;
844 while (this_index
== opcode
->idx
&& !found
)
849 for (i
= 0; i
< noperands
; i
++)
851 int mode
= operands
[i
].mode
;
853 if ((mode
& CLASS_MASK
) != (this_try
->arg_info
[i
] & CLASS_MASK
))
855 /* It could be an pc rel operand, if this is a da mode
856 and we like disps, then insert it. */
858 if (mode
== CLASS_DA
&& this_try
->arg_info
[i
] == CLASS_DISP
)
860 /* This is the case. */
861 operands
[i
].mode
= CLASS_DISP
;
863 else if (mode
== CLASS_BA
&& this_try
->arg_info
[i
])
865 /* Can't think of a way to turn what we've been
866 given into something that's OK. */
869 else if (this_try
->arg_info
[i
] & CLASS_PR
)
871 if (mode
== CLASS_REG_LONG
&& segmented_mode
)
875 else if (mode
== CLASS_REG_WORD
&& !segmented_mode
)
885 switch (mode
& CLASS_MASK
)
900 reg
[this_try
->arg_info
[i
] & ARG_MASK
] = operands
[i
].reg
;
916 check_operand (operand
, width
, string
)
917 struct z8k_op
*operand
;
921 if (operand
->exp
.X_add_symbol
== 0
922 && operand
->exp
.X_op_symbol
== 0)
925 /* No symbol involved, let's look at offset, it's dangerous if
926 any of the high bits are not 0 or ff's, find out by oring or
927 anding with the width and seeing if the answer is 0 or all
929 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
930 (operand
->exp
.X_add_number
| width
) != (~0))
932 as_warn (_("operand %s0x%x out of range."),
933 string
, operand
->exp
.X_add_number
);
939 static char buffer
[20];
942 newfix (ptr
, type
, operand
)
945 expressionS
*operand
;
947 if (operand
->X_add_symbol
948 || operand
->X_op_symbol
949 || operand
->X_add_number
)
951 fix_new_exp (frag_now
,
961 apply_fix (ptr
, type
, operand
, size
)
964 expressionS
*operand
;
967 int n
= operand
->X_add_number
;
969 operand
->X_add_number
= n
;
970 newfix ((ptr
- buffer
) / 2, type
, operand
);
974 case 8: /* 8 nibbles == 32 bits */
979 case 4: /* 4 niblles == 16 bits */
993 /* Now we know what sort of opcodes it is. Let's build the bytes. */
995 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
998 build_bytes (this_try
, operand
)
999 opcode_entry_type
*this_try
;
1000 struct z8k_op
*operand
;
1006 char *output_ptr
= buffer
;
1012 unsigned int *class_ptr
;
1014 frag_wane (frag_now
);
1017 memset (buffer
, 20, 0);
1018 class_ptr
= this_try
->byte_info
;
1021 for (nibble
= 0; c
= *class_ptr
++; nibble
++)
1024 switch (c
& CLASS_MASK
)
1030 /* Direct address, we don't cope with the SS mode right now. */
1033 da_operand
->X_add_number
|= 0x80000000;
1034 output_ptr
= apply_fix (output_ptr
, R_IMM32
, da_operand
, 8);
1038 output_ptr
= apply_fix (output_ptr
, R_IMM16
, da_operand
, 4);
1044 output_ptr
= apply_fix (output_ptr
, R_JR
, da_operand
, 2);
1051 output_ptr
= apply_fix (output_ptr
, R_DISP7
, da_operand
, 2);
1058 output_ptr
= apply_fix (output_ptr
, R_DISP7
, da_operand
, 2);
1059 output_ptr
[-2] = 0x8;
1063 case CLASS_BIT_1OR2
:
1064 *output_ptr
= c
& 0xf;
1067 if (imm_operand
->X_add_number
== 2)
1069 else if (imm_operand
->X_add_number
!= 1)
1070 as_bad (_("immediate must be 1 or 2"));
1073 as_bad (_("immediate 1 or 2 expected"));
1077 *output_ptr
++ = the_cc
;
1080 *output_ptr
++ = the_ctrl
;
1083 *output_ptr
++ = the_ctrl
| 0x8;
1086 *output_ptr
++ = (~the_interrupt
& 0x3);
1089 *output_ptr
++ = (~the_interrupt
& 0x3) | 0x4;
1092 *output_ptr
++ = the_flags
;
1095 *output_ptr
++ = c
& 0xf;
1098 if (reg
[c
& 0xf] == 0)
1099 as_bad (_("can't use R0 here"));
1102 case CLASS_REG_BYTE
:
1103 case CLASS_REG_WORD
:
1104 case CLASS_REG_LONG
:
1105 case CLASS_REG_QUAD
:
1106 /* Insert bit mattern of right reg. */
1107 *output_ptr
++ = reg
[c
& 0xf];
1110 output_ptr
= apply_fix (output_ptr
, R_IMM16
, da_operand
, 4);
1117 switch (c
& ARG_MASK
)
1120 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
1123 imm_operand
->X_add_number
--;
1124 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
1126 case ARG_IMMNMINUS1
:
1127 imm_operand
->X_add_number
--;
1128 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
1131 imm_operand
->X_add_number
= -imm_operand
->X_add_number
;
1133 output_ptr
= apply_fix (output_ptr
, R_IMM8
, imm_operand
, 2);
1136 output_ptr
= apply_fix (output_ptr
, R_IMM16
, imm_operand
, 4);
1140 output_ptr
= apply_fix (output_ptr
, R_IMM32
, imm_operand
, 8);
1150 /* Copy from the nibble buffer into the frag. */
1152 int length
= (output_ptr
- buffer
) / 2;
1154 char *fragp
= frag_more (length
);
1156 while (src
< output_ptr
)
1158 *fragp
= (src
[0] << 4) | src
[1];
1165 /* This is the guts of the machine-dependent assembler. STR points to a
1166 machine dependent instruction. This function is supposed to emit
1167 the frags/bytes it assembles to. */
1176 struct z8k_op operand
[3];
1177 opcode_entry_type
*opcode
;
1178 opcode_entry_type
*prev_opcode
;
1183 /* Drop leading whitespace. */
1187 /* Find the op code end. */
1188 for (op_start
= op_end
= str
;
1189 *op_end
!= 0 && *op_end
!= ' ';
1193 if (op_end
== op_start
)
1195 as_bad (_("can't find opcode "));
1201 opcode
= (opcode_entry_type
*) hash_find (opcode_hash_control
, op_start
);
1205 as_bad (_("unknown opcode"));
1209 if (opcode
->opcode
== 250)
1211 /* Was really a pseudo op. */
1216 char *old
= input_line_pointer
;
1219 input_line_pointer
= op_end
;
1223 while (*input_line_pointer
== ' ')
1224 input_line_pointer
++;
1225 p
= (pseudo_typeS
*) (opcode
->func
);
1227 (p
->poc_handler
) (p
->poc_val
);
1228 input_line_pointer
= old
;
1233 input_line_pointer
= get_operands (opcode
, op_end
, operand
);
1234 prev_opcode
= opcode
;
1236 opcode
= get_specific (opcode
, operand
);
1240 /* Couldn't find an opcode which matched the operands. */
1241 char *where
= frag_more (2);
1246 as_bad (_("Can't find opcode to match operands"));
1250 build_bytes (opcode
, operand
);
1255 tc_crawl_symbol_chain (headers
)
1256 object_headers
*headers
;
1258 printf (_("call to tc_crawl_symbol_chain \n"));
1262 md_undefined_symbol (name
)
1269 tc_headers_hook (headers
)
1270 object_headers
*headers
;
1272 printf (_("call to tc_headers_hook \n"));
1275 /* Various routines to kill one day. */
1276 /* Equal to MAX_PRECISION in atof-ieee.c. */
1277 #define MAX_LITTLENUMS 6
1279 /* Turn a string in input_line_pointer into a floating point constant
1280 of type TYPE, and store the appropriate bytes in *LITP. The number
1281 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1282 returned, or NULL on OK. */
1285 md_atof (type
, litP
, sizeP
)
1291 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1292 LITTLENUM_TYPE
*wordP
;
1324 return _("Bad call to MD_ATOF()");
1326 t
= atof_ieee (input_line_pointer
, type
, words
);
1328 input_line_pointer
= t
;
1330 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1331 for (wordP
= words
; prec
--;)
1333 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1334 litP
+= sizeof (LITTLENUM_TYPE
);
1339 CONST
char *md_shortopts
= "z:";
1341 struct option md_longopts
[] = {
1342 {NULL
, no_argument
, NULL
, 0}
1345 size_t md_longopts_size
= sizeof (md_longopts
);
1348 md_parse_option (c
, arg
)
1355 if (!strcmp (arg
, "8001"))
1357 else if (!strcmp (arg
, "8002"))
1361 as_bad (_("invalid architecture -z%s"), arg
);
1374 md_show_usage (stream
)
1377 fprintf (stream
, _("\
1379 -z8001 generate segmented code\n\
1380 -z8002 generate unsegmented code\n"));
1384 tc_aout_fix_to_chars ()
1386 printf (_("call to tc_aout_fix_to_chars \n"));
1391 md_convert_frag (headers
, seg
, fragP
)
1392 object_headers
*headers
;
1396 printf (_("call to md_convert_frag \n"));
1401 md_section_align (seg
, size
)
1405 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
1406 & (-1 << section_alignment
[(int) seg
]));
1411 md_apply_fix (fixP
, val
)
1415 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1417 switch (fixP
->fx_r_type
)
1420 buf
[0] = (buf
[0] & 0xf0) | ((buf
[0] + val
) & 0xf);
1445 *buf
++ = (val
>> 8);
1449 *buf
++ = (val
>> 24);
1450 *buf
++ = (val
>> 16);
1451 *buf
++ = (val
>> 8);
1456 *buf
++ = (val
>> 16);
1458 *buf
++ = (val
>> 8);
1464 md_number_to_chars (buf
, val
, fixP
->fx_size
);
1473 md_estimate_size_before_relax (fragP
, segment_type
)
1474 register fragS
*fragP
;
1475 register segT segment_type
;
1477 printf (_("call tomd_estimate_size_before_relax \n"));
1481 /* Put number into target byte order. */
1484 md_number_to_chars (ptr
, use
, nbytes
)
1489 number_to_chars_bigendian (ptr
, use
, nbytes
);
1493 md_pcrel_from (fixP
)
1500 tc_coff_symbol_emit_hook (s
)
1506 tc_reloc_mangle (fix_ptr
, intr
, base
)
1508 struct internal_reloc
*intr
;
1512 symbolS
*symbol_ptr
;
1514 if (fix_ptr
->fx_addsy
1515 && fix_ptr
->fx_subsy
)
1517 symbolS
*add
= fix_ptr
->fx_addsy
;
1518 symbolS
*sub
= fix_ptr
->fx_subsy
;
1520 if (S_GET_SEGMENT (add
) != S_GET_SEGMENT (sub
))
1521 as_bad (_("Can't subtract symbols in different sections %s %s"),
1522 S_GET_NAME (add
), S_GET_NAME (sub
));
1525 int diff
= S_GET_VALUE (add
) - S_GET_VALUE (sub
);
1527 fix_ptr
->fx_addsy
= 0;
1528 fix_ptr
->fx_subsy
= 0;
1529 fix_ptr
->fx_offset
+= diff
;
1532 symbol_ptr
= fix_ptr
->fx_addsy
;
1534 /* If this relocation is attached to a symbol then it's ok
1536 if (fix_ptr
->fx_r_type
== 0)
1538 /* cons likes to create reloc32's whatever the size of the reloc. */
1539 switch (fix_ptr
->fx_size
)
1542 intr
->r_type
= R_IMM16
;
1545 intr
->r_type
= R_IMM8
;
1548 intr
->r_type
= R_IMM32
;
1555 intr
->r_type
= fix_ptr
->fx_r_type
;
1557 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1558 intr
->r_offset
= fix_ptr
->fx_offset
;
1561 intr
->r_symndx
= symbol_ptr
->sy_number
;
1563 intr
->r_symndx
= -1;