1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation.
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
);
197 typedef struct z8k_op
199 /* 'b','w','r','q'. */
207 /* Any other register associated with the mode. */
210 /* Any expression. */
216 static expressionS
*da_operand
;
217 static expressionS
*imm_operand
;
230 if (isdigit (src
[1]))
232 *reg
= (src
[0] - '0') * 10 + src
[1] - '0';
237 *reg
= (src
[0] - '0');
248 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
256 /* Try to parse a reg name. Return a pointer to the first character
257 in SRC after the reg name. */
260 parse_reg (src
, mode
, reg
)
268 if (src
[0] == 's' && src
[1] == 'p')
272 *mode
= CLASS_REG_LONG
;
277 *mode
= CLASS_REG_WORD
;
286 *mode
= CLASS_REG_LONG
;
287 res
= whatreg (reg
, src
+ 2);
290 as_warn (_("register rr%d, out of range."), regno
);
292 else if (src
[1] == 'h')
294 *mode
= CLASS_REG_BYTE
;
295 res
= whatreg (reg
, src
+ 2);
298 as_warn (_("register rh%d, out of range."), regno
);
300 else if (src
[1] == 'l')
302 *mode
= CLASS_REG_BYTE
;
303 res
= whatreg (reg
, src
+ 2);
306 as_warn (_("register rl%d, out of range."), regno
);
309 else if (src
[1] == 'q')
311 *mode
= CLASS_REG_QUAD
;
312 res
= whatreg (reg
, src
+ 2);
315 as_warn (_("register rq%d, out of range."), regno
);
319 *mode
= CLASS_REG_WORD
;
320 res
= whatreg (reg
, src
+ 1);
323 as_warn (_("register r%d, out of range."), regno
);
334 char *save
= input_line_pointer
;
337 input_line_pointer
= s
;
339 if (op
->X_op
== O_absent
)
340 as_bad (_("missing operand"));
341 new = input_line_pointer
;
342 input_line_pointer
= save
;
346 /* The many forms of operand:
368 as_bad (_("expected %c"), what
);
373 /* Make sure the mode supplied is the size of a word. */
376 regword (mode
, string
)
385 as_bad (_("register is wrong size for a word %s"), string
);
389 /* Make sure the mode supplied is the size of an address. */
392 regaddr (mode
, string
)
398 ok
= segmented_mode
? CLASS_REG_LONG
: CLASS_REG_WORD
;
401 as_bad (_("register is wrong size for address %s"), string
);
411 struct ctrl_names ctrl_table
[] = {
424 get_ctrl_operand (ptr
, mode
, dst
)
436 mode
->mode
= CLASS_CTRL
;
437 for (i
= 0; ctrl_table
[i
].name
; i
++)
441 for (j
= 0; ctrl_table
[i
].name
[j
]; j
++)
443 if (ctrl_table
[i
].name
[j
] != src
[j
])
446 the_ctrl
= ctrl_table
[i
].value
;
463 struct flag_names flag_table
[] = {
474 get_flags_operand (ptr
, mode
, dst
)
487 mode
->mode
= CLASS_FLAGS
;
489 for (j
= 0; j
<= 9; j
++)
493 for (i
= 0; flag_table
[i
].name
; i
++)
495 if (flag_table
[i
].name
[0] == src
[j
])
497 the_flags
= the_flags
| flag_table
[i
].value
;
510 struct interrupt_names
517 struct interrupt_names intr_table
[] =
527 get_interrupt_operand (ptr
, mode
, dst
)
539 mode
->mode
= CLASS_IMM
;
540 for (i
= 0; intr_table
[i
].name
; i
++)
544 for (j
= 0; intr_table
[i
].name
[j
]; j
++)
546 if (intr_table
[i
].name
[j
] != src
[j
])
549 the_interrupt
= intr_table
[i
].value
;
566 struct cc_names table
[] = {
593 get_cc_operand (ptr
, mode
, dst
)
605 mode
->mode
= CLASS_CC
;
606 for (i
= 0; table
[i
].name
; i
++)
610 for (j
= 0; table
[i
].name
[j
]; j
++)
612 if (table
[i
].name
[j
] != src
[j
])
615 the_cc
= table
[i
].value
;
625 get_operand (ptr
, mode
, dst
)
642 mode
->mode
= CLASS_IMM
;
643 imm_operand
= &(mode
->exp
);
644 src
= parse_exp (src
+ 1, &(mode
->exp
));
646 else if (*src
== '@')
650 mode
->mode
= CLASS_IR
;
651 src
= parse_reg (src
+ 1, &d
, &mode
->reg
);
657 end
= parse_reg (src
, &mode
->mode
, ®n
);
667 end
= parse_reg (src
, &nw
, &nr
);
674 as_bad (_("Missing ) in ra(rb)"));
678 regaddr (mode
->mode
, "ra(rb) ra");
680 regword (mode
->mode
, "ra(rb) rb");
682 mode
->mode
= CLASS_BX
;
692 src
= parse_exp (src
, &(mode
->exp
));
693 src
= checkfor (src
, ')');
694 mode
->mode
= CLASS_BA
;
697 imm_operand
= &(mode
->exp
);
708 /* No initial reg. */
709 src
= parse_exp (src
, &(mode
->exp
));
713 end
= parse_reg (src
, &(mode
->mode
), ®n
);
714 regword (mode
->mode
, "addr(Ra) ra");
715 mode
->mode
= CLASS_X
;
718 da_operand
= &(mode
->exp
);
719 src
= checkfor (end
, ')');
723 /* Just an address. */
724 mode
->mode
= CLASS_DA
;
727 da_operand
= &(mode
->exp
);
735 get_operands (opcode
, op_end
, operand
)
736 opcode_entry_type
*opcode
;
743 switch (opcode
->noperands
)
752 if (opcode
->arg_info
[0] == CLASS_CC
)
754 get_cc_operand (&ptr
, operand
+ 0, 0);
756 else if (opcode
->arg_info
[0] == CLASS_FLAGS
)
758 get_flags_operand (&ptr
, operand
+ 0, 0);
760 else if (opcode
->arg_info
[0] == (CLASS_IMM
+ (ARG_IMM2
)))
762 get_interrupt_operand (&ptr
, operand
+ 0, 0);
766 get_operand (&ptr
, operand
+ 0, 0);
774 if (opcode
->arg_info
[0] == CLASS_CC
)
776 get_cc_operand (&ptr
, operand
+ 0, 0);
778 else if (opcode
->arg_info
[0] == CLASS_CTRL
)
780 get_ctrl_operand (&ptr
, operand
+ 0, 0);
784 get_operand (&ptr
, operand
+ 0, 0);
789 get_ctrl_operand (&ptr
, operand
+ 1, 1);
795 get_operand (&ptr
, operand
+ 0, 0);
801 get_operand (&ptr
, operand
+ 1, 1);
806 get_operand (&ptr
, operand
+ 0, 0);
809 get_operand (&ptr
, operand
+ 1, 1);
812 get_operand (&ptr
, operand
+ 2, 2);
817 get_operand (&ptr
, operand
+ 0, 0);
820 get_operand (&ptr
, operand
+ 1, 1);
823 get_operand (&ptr
, operand
+ 2, 2);
826 get_cc_operand (&ptr
, operand
+ 3, 3);
836 /* Passed a pointer to a list of opcodes which use different
837 addressing modes. Return the opcode which matches the opcodes
840 static opcode_entry_type
*
841 get_specific (opcode
, operands
)
842 opcode_entry_type
*opcode
;
846 opcode_entry_type
*this_try
= opcode
;
848 unsigned int noperands
= opcode
->noperands
;
850 unsigned int dispreg
;
851 unsigned int this_index
= opcode
->idx
;
853 while (this_index
== opcode
->idx
&& !found
)
858 for (i
= 0; i
< noperands
; i
++)
860 int mode
= operands
[i
].mode
;
862 if ((mode
& CLASS_MASK
) != (this_try
->arg_info
[i
] & CLASS_MASK
))
864 /* It could be an pc rel operand, if this is a da mode
865 and we like disps, then insert it. */
867 if (mode
== CLASS_DA
&& this_try
->arg_info
[i
] == CLASS_DISP
)
869 /* This is the case. */
870 operands
[i
].mode
= CLASS_DISP
;
872 else if (mode
== CLASS_BA
&& this_try
->arg_info
[i
])
874 /* Can't think of a way to turn what we've been
875 given into something that's OK. */
878 else if (this_try
->arg_info
[i
] & CLASS_PR
)
880 if (mode
== CLASS_REG_LONG
&& segmented_mode
)
884 else if (mode
== CLASS_REG_WORD
&& !segmented_mode
)
894 switch (mode
& CLASS_MASK
)
909 reg
[this_try
->arg_info
[i
] & ARG_MASK
] = operands
[i
].reg
;
925 check_operand (operand
, width
, string
)
926 struct z8k_op
*operand
;
930 if (operand
->exp
.X_add_symbol
== 0
931 && operand
->exp
.X_op_symbol
== 0)
934 /* No symbol involved, let's look at offset, it's dangerous if
935 any of the high bits are not 0 or ff's, find out by oring or
936 anding with the width and seeing if the answer is 0 or all
938 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
939 (operand
->exp
.X_add_number
| width
) != (~0))
941 as_warn (_("operand %s0x%x out of range."),
942 string
, operand
->exp
.X_add_number
);
948 static char buffer
[20];
951 newfix (ptr
, type
, operand
)
954 expressionS
*operand
;
956 if (operand
->X_add_symbol
957 || operand
->X_op_symbol
958 || operand
->X_add_number
)
960 fix_new_exp (frag_now
,
970 apply_fix (ptr
, type
, operand
, size
)
973 expressionS
*operand
;
976 int n
= operand
->X_add_number
;
978 operand
->X_add_number
= n
;
979 newfix ((ptr
- buffer
) / 2, type
, operand
);
983 case 8: /* 8 nibbles == 32 bits */
988 case 4: /* 4 niblles == 16 bits */
1002 /* Now we know what sort of opcodes it is. Let's build the bytes. */
1004 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
1006 build_bytes (this_try
, operand
)
1007 opcode_entry_type
*this_try
;
1008 struct z8k_op
*operand
;
1014 char *output_ptr
= buffer
;
1020 unsigned int *class_ptr
;
1022 frag_wane (frag_now
);
1025 memset (buffer
, 20, 0);
1026 class_ptr
= this_try
->byte_info
;
1029 for (nibble
= 0; c
= *class_ptr
++; nibble
++)
1032 switch (c
& CLASS_MASK
)
1038 /* Direct address, we don't cope with the SS mode right now. */
1041 da_operand
->X_add_number
|= 0x80000000;
1042 output_ptr
= apply_fix (output_ptr
, R_IMM32
, da_operand
, 8);
1046 output_ptr
= apply_fix (output_ptr
, R_IMM16
, da_operand
, 4);
1052 output_ptr
= apply_fix (output_ptr
, R_JR
, da_operand
, 2);
1059 output_ptr
= apply_fix (output_ptr
, R_DISP7
, da_operand
, 2);
1066 output_ptr
= apply_fix (output_ptr
, R_DISP7
, da_operand
, 2);
1067 output_ptr
[-2] = 0x8;
1071 case CLASS_BIT_1OR2
:
1072 *output_ptr
= c
& 0xf;
1075 if (imm_operand
->X_add_number
== 2)
1077 else if (imm_operand
->X_add_number
!= 1)
1078 as_bad (_("immediate must be 1 or 2"));
1081 as_bad (_("immediate 1 or 2 expected"));
1085 *output_ptr
++ = the_cc
;
1088 *output_ptr
++ = the_ctrl
;
1091 *output_ptr
++ = the_ctrl
| 0x8;
1094 *output_ptr
++ = (~the_interrupt
& 0x3);
1097 *output_ptr
++ = (~the_interrupt
& 0x3) | 0x4;
1100 *output_ptr
++ = the_flags
;
1103 *output_ptr
++ = c
& 0xf;
1106 if (reg
[c
& 0xf] == 0)
1107 as_bad (_("can't use R0 here"));
1110 case CLASS_REG_BYTE
:
1111 case CLASS_REG_WORD
:
1112 case CLASS_REG_LONG
:
1113 case CLASS_REG_QUAD
:
1114 /* Insert bit mattern of right reg. */
1115 *output_ptr
++ = reg
[c
& 0xf];
1118 output_ptr
= apply_fix (output_ptr
, R_IMM16
, da_operand
, 4);
1125 switch (c
& ARG_MASK
)
1128 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
1131 imm_operand
->X_add_number
--;
1132 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
1134 case ARG_IMMNMINUS1
:
1135 imm_operand
->X_add_number
--;
1136 output_ptr
= apply_fix (output_ptr
, R_IMM4L
, imm_operand
, 1);
1139 imm_operand
->X_add_number
= -imm_operand
->X_add_number
;
1141 output_ptr
= apply_fix (output_ptr
, R_IMM8
, imm_operand
, 2);
1144 output_ptr
= apply_fix (output_ptr
, R_IMM16
, imm_operand
, 4);
1148 output_ptr
= apply_fix (output_ptr
, R_IMM32
, imm_operand
, 8);
1158 /* Copy from the nibble buffer into the frag. */
1160 int length
= (output_ptr
- buffer
) / 2;
1162 char *fragp
= frag_more (length
);
1164 while (src
< output_ptr
)
1166 *fragp
= (src
[0] << 4) | src
[1];
1173 /* This is the guts of the machine-dependent assembler. STR points to a
1174 machine dependent instruction. This function is supposed to emit
1175 the frags/bytes it assembles to. */
1184 struct z8k_op operand
[3];
1185 opcode_entry_type
*opcode
;
1186 opcode_entry_type
*prev_opcode
;
1191 /* Drop leading whitespace. */
1195 /* Find the op code end. */
1196 for (op_start
= op_end
= str
;
1197 *op_end
!= 0 && *op_end
!= ' ';
1201 if (op_end
== op_start
)
1203 as_bad (_("can't find opcode "));
1209 opcode
= (opcode_entry_type
*) hash_find (opcode_hash_control
, op_start
);
1213 as_bad (_("unknown opcode"));
1217 if (opcode
->opcode
== 250)
1219 /* Was really a pseudo op. */
1224 char *old
= input_line_pointer
;
1227 input_line_pointer
= op_end
;
1231 while (*input_line_pointer
== ' ')
1232 input_line_pointer
++;
1233 p
= (pseudo_typeS
*) (opcode
->func
);
1235 (p
->poc_handler
) (p
->poc_val
);
1236 input_line_pointer
= old
;
1241 input_line_pointer
= get_operands (opcode
, op_end
, operand
);
1242 prev_opcode
= opcode
;
1244 opcode
= get_specific (opcode
, operand
);
1248 /* Couldn't find an opcode which matched the operands. */
1249 char *where
= frag_more (2);
1254 as_bad (_("Can't find opcode to match operands"));
1258 build_bytes (opcode
, operand
);
1263 tc_crawl_symbol_chain (headers
)
1264 object_headers
*headers
;
1266 printf (_("call to tc_crawl_symbol_chain \n"));
1270 md_undefined_symbol (name
)
1277 tc_headers_hook (headers
)
1278 object_headers
*headers
;
1280 printf (_("call to tc_headers_hook \n"));
1283 /* Various routines to kill one day. */
1284 /* Equal to MAX_PRECISION in atof-ieee.c. */
1285 #define MAX_LITTLENUMS 6
1287 /* Turn a string in input_line_pointer into a floating point constant
1288 of type TYPE, and store the appropriate bytes in *LITP. The number
1289 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1290 returned, or NULL on OK. */
1293 md_atof (type
, litP
, sizeP
)
1299 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1300 LITTLENUM_TYPE
*wordP
;
1332 return _("Bad call to MD_ATOF()");
1334 t
= atof_ieee (input_line_pointer
, type
, words
);
1336 input_line_pointer
= t
;
1338 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1339 for (wordP
= words
; prec
--;)
1341 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1342 litP
+= sizeof (LITTLENUM_TYPE
);
1347 CONST
char *md_shortopts
= "z:";
1349 struct option md_longopts
[] = {
1350 {NULL
, no_argument
, NULL
, 0}
1353 size_t md_longopts_size
= sizeof (md_longopts
);
1356 md_parse_option (c
, arg
)
1363 if (!strcmp (arg
, "8001"))
1365 else if (!strcmp (arg
, "8002"))
1369 as_bad (_("invalid architecture -z%s"), arg
);
1382 md_show_usage (stream
)
1385 fprintf (stream
, _("\
1387 -z8001 generate segmented code\n\
1388 -z8002 generate unsegmented code\n"));
1392 tc_aout_fix_to_chars ()
1394 printf (_("call to tc_aout_fix_to_chars \n"));
1399 md_convert_frag (headers
, seg
, fragP
)
1400 object_headers
*headers
;
1404 printf (_("call to md_convert_frag \n"));
1409 md_section_align (seg
, size
)
1413 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
1414 & (-1 << section_alignment
[(int) seg
]));
1419 md_apply_fix (fixP
, val
)
1423 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1425 switch (fixP
->fx_r_type
)
1428 buf
[0] = (buf
[0] & 0xf0) | ((buf
[0] + val
) & 0xf);
1453 *buf
++ = (val
>> 8);
1457 *buf
++ = (val
>> 24);
1458 *buf
++ = (val
>> 16);
1459 *buf
++ = (val
>> 8);
1464 *buf
++ = (val
>> 16);
1466 *buf
++ = (val
>> 8);
1472 md_number_to_chars (buf
, val
, fixP
->fx_size
);
1481 md_estimate_size_before_relax (fragP
, segment_type
)
1482 register fragS
*fragP
;
1483 register segT segment_type
;
1485 printf (_("call tomd_estimate_size_before_relax \n"));
1489 /* Put number into target byte order. */
1492 md_number_to_chars (ptr
, use
, nbytes
)
1497 number_to_chars_bigendian (ptr
, use
, nbytes
);
1501 md_pcrel_from (fixP
)
1508 tc_coff_symbol_emit_hook (s
)
1514 tc_reloc_mangle (fix_ptr
, intr
, base
)
1516 struct internal_reloc
*intr
;
1520 symbolS
*symbol_ptr
;
1522 if (fix_ptr
->fx_addsy
1523 && fix_ptr
->fx_subsy
)
1525 symbolS
*add
= fix_ptr
->fx_addsy
;
1526 symbolS
*sub
= fix_ptr
->fx_subsy
;
1528 if (S_GET_SEGMENT (add
) != S_GET_SEGMENT (sub
))
1529 as_bad (_("Can't subtract symbols in different sections %s %s"),
1530 S_GET_NAME (add
), S_GET_NAME (sub
));
1533 int diff
= S_GET_VALUE (add
) - S_GET_VALUE (sub
);
1535 fix_ptr
->fx_addsy
= 0;
1536 fix_ptr
->fx_subsy
= 0;
1537 fix_ptr
->fx_offset
+= diff
;
1540 symbol_ptr
= fix_ptr
->fx_addsy
;
1542 /* If this relocation is attached to a symbol then it's ok
1544 if (fix_ptr
->fx_r_type
== 0)
1546 /* cons likes to create reloc32's whatever the size of the reloc. */
1547 switch (fix_ptr
->fx_size
)
1550 intr
->r_type
= R_IMM16
;
1553 intr
->r_type
= R_IMM8
;
1556 intr
->r_type
= R_IMM32
;
1563 intr
->r_type
= fix_ptr
->fx_r_type
;
1565 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1566 intr
->r_offset
= fix_ptr
->fx_offset
;
1569 intr
->r_symndx
= symbol_ptr
->sy_number
;
1571 intr
->r_symndx
= -1;