1 /* tc-zpu.c -- Assembler code for the Zylin ZPU
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Oyvind Harboe <oyvind.harboe@zylin.com>
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
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "safe-ctype.h"
25 #include "dwarf2dbg.h"
27 #include "opcode/zpu.h"
30 const char comment_chars
[] = ";!";
31 const char line_comment_chars
[] = "#*";
32 const char line_separator_chars
[] = "";
34 const char EXP_CHARS
[] = "eE";
35 const char FLT_CHARS
[] = "dD";
39 struct zpu_opcode_def
{
41 struct zpu_opcode
*opcode
;
44 static struct zpu_opcode_def
*zpu_opcode_defs
= 0;
45 static int zpu_nb_opcode_defs
= 0;
47 /* Local functions. */
48 static int cmp_opcode (struct zpu_opcode
*, struct zpu_opcode
*);
49 static char *print_opcode_format (struct zpu_opcode
*, int);
50 static char *skip_whites (char *);
51 static void print_opcode_list (void);
52 static void get_default_target (void);
54 static struct zpu_opcode
*find_opcode (struct zpu_opcode_def
*);
59 /* Dumps the list of instructions with syntax and then exit:
60 1 -> Only dumps the list (sorted by name)
61 2 -> Generate an example (or test) that can be compiled. */
62 static short flag_print_opcodes
= 0;
64 /* Opcode hash table. */
65 static struct hash_control
*zpu_hash
;
67 /* Default cpu determined by 'get_default_target'. */
68 static const char *default_cpu
;
70 /* Number of opcodes in the sorted table (filtered by current cpu). */
71 static int num_opcodes
;
73 /* The opcodes sorted by name and filtered by current cpu. */
74 static struct zpu_opcode
*zpu_sorted_opcodes
;
77 static void s_bss
PARAMS ((int));
79 /* This table describes all the machine specific pseudo-ops the assembler
80 has to support. The fields are:
81 pseudo-op name without dot
82 function to call to execute this pseudo-op
83 Integer arg to pass to the function. */
84 const pseudo_typeS md_pseudo_table
[] = {
90 /* Options and initialization. */
92 const char *md_shortopts
= "Sm:";
94 struct option md_longopts
[] = {
96 {NULL
, no_argument
, NULL
, 0}
98 size_t md_longopts_size
= sizeof (md_longopts
);
100 /* Get the target cpu for the assembler. This is based on the configure
101 options and on the If no option is specified,
102 we must get the default. */
104 zpu_arch_format (void)
106 get_default_target ();
110 enum bfd_architecture
113 get_default_target ();
123 /* Listing header selected according to cpu. */
125 zpu_listing_header (void)
131 md_show_usage (FILE *stream
)
133 get_default_target ();
134 fprintf (stream
, _("\
135 Zylin ZPU options:\n\
136 --print-insn-syntax print the syntax of instruction in case of error\n\
137 --print-opcodes print the list of instructions with syntax\n\
138 --generate-example generate an example of each instruction\n\
139 (used for testing)\n"));
142 /* Try to identify the default target based on the BFD library. */
144 get_default_target (void)
146 const bfd_target
*target
;
150 default_cpu
= "unknown";
151 target
= bfd_find_target (0, &abfd
);
155 md_parse_option (int c
, char *arg
)
163 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
168 /* Equal to MAX_PRECISION in atof-ieee.c. */
169 #define MAX_LITTLENUMS 6
171 /* Turn a string in input_line_pointer into a floating point constant
172 of type TYPE, and store the appropriate bytes in *LITP. The number
173 of LITTLENUMS emitted is stored in *SIZEP. An error message is
174 returned, or NULL on OK. */
176 md_atof (int type
, char *litP
, int *sizeP
)
179 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
180 LITTLENUM_TYPE
*wordP
;
211 return _("Bad call to MD_ATOF()");
213 t
= atof_ieee (input_line_pointer
, type
, words
);
215 input_line_pointer
= t
;
217 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
218 for (wordP
= words
; prec
--;)
220 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
221 litP
+= sizeof (LITTLENUM_TYPE
);
227 md_section_align (asection
*seg
, valueT addr
)
229 int align
= bfd_get_section_alignment (stdoutput
, seg
);
230 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
234 cmp_opcode (struct zpu_opcode
*op1
, struct zpu_opcode
*op2
)
236 return strcmp (op1
->name
, op2
->name
);
240 /* Initialize the assembler. Create the opcode hash table
241 (sorted on the names) with the M6811 opcode table
242 (from opcode library). */
246 char *prev_name
= "";
247 struct zpu_opcode
*opcodes
;
248 struct zpu_opcode_def
*opc
= 0;
251 get_default_target ();
253 zpu_hash
= hash_new ();
255 /* Get a writable copy of the opcode table and sort it on the names. */
256 opcodes
= (struct zpu_opcode
*) xmalloc (zpu_num_opcodes
*
259 zpu_sorted_opcodes
= opcodes
;
261 for (i
= 0; i
< zpu_num_opcodes
; i
++)
263 opcodes
[num_opcodes
] = zpu_opcodes
[i
];
266 qsort (opcodes
, num_opcodes
, sizeof (struct zpu_opcode
),
267 (int (*) (const void*, const void*)) cmp_opcode
);
269 opc
= (struct zpu_opcode_def
*)
270 xmalloc (num_opcodes
* sizeof (struct zpu_opcode_def
));
271 zpu_opcode_defs
= opc
--;
273 /* Insert unique names into hash table. The M6811 instruction set
274 has several identical opcode names that have different opcodes based
275 on the operands. This hash table then provides a quick index to
276 the first opcode with a particular name in the opcode table. */
277 for (i
= 0; i
< num_opcodes
; i
++, opcodes
++)
280 if (strcmp (prev_name
, opcodes
->name
))
282 prev_name
= (char *) opcodes
->name
;
285 opc
->opcode
= opcodes
;
287 hash_insert (zpu_hash
, opcodes
->name
, opc
);
292 zpu_nb_opcode_defs
= opc
- zpu_opcode_defs
;
294 if (flag_print_opcodes
)
296 print_opcode_list ();
303 zpu_init_after_args (void)
309 /* Return a string that represents the operand format for the instruction.
310 When example is true, this generates an example of operand. This is used
311 to give an example and also to generate a test. */
313 print_opcode_format (struct zpu_opcode
*opcode
, int example
)
317 static char buf
[128];
326 /* Prints the list of instructions with the possible operands. */
328 print_opcode_list (void)
331 char *prev_name
= "";
332 struct zpu_opcode
*opcodes
;
333 int example
= flag_print_opcodes
== 2;
336 opcodes
= zpu_sorted_opcodes
;
338 /* Walk the list sorted on names (by md_begin). We only report
339 one instruction per line, and we collect the different operand
341 for (i
= 0; i
< num_opcodes
; i
++, opcodes
++)
343 char *fmt
= print_opcode_format (opcodes
, example
);
347 printf ("L%d:\t", i
);
348 printf ("%s %s\n", opcodes
->name
, fmt
);
352 if (strcmp (prev_name
, opcodes
->name
))
357 printf ("%-5.5s ", opcodes
->name
);
358 prev_name
= (char *) opcodes
->name
;
361 printf (" [%s]", fmt
);
369 skip_whites (char *p
)
378 static int checkRange(int val
, int bits
)
381 prevSign
=(val
>>(bits
-1))&1;
384 return ((val
>>bits
)&0x7f)!=0x7f;
387 return ((val
>>bits
)&0x7f)!=0x00;
391 static void emit_insn_core(int insn
)
395 dwarf2_emit_insn (1);
396 number_to_chars_bigendian (f
, insn
, 1);
399 static int immediate
=0;
400 static int prevImmediate
=0;
402 static void emit_insn(int insn
)
404 emit_insn_core(insn
);
408 static int emit7Bits(int val
, int pos
)
410 return ((val
>>pos
)&0x7f)|0x80;
413 /* generate immediate sequence */
414 static int genImmediate(char *buffer
, int val
)
421 if (!first
||(i
==0)||checkRange(val
, i
*7))
423 buffer
[j
++]=emit7Bits(val
, i
*7);
430 static int flip(int val
)
443 /* build_insn simple instruction */
445 build_immediate_insn (expressionS
*oper
, int fixed14
)
447 if (oper
->X_op
== O_constant
)
454 char buffer
[10]; /* maximum length */
456 val
=oper
->X_add_number
;
458 a
=genImmediate(buffer
, val
);
459 b
=genImmediate(buffer
, flip(val
))+1;
462 length
=genImmediate(buffer
, flip(val
));
463 buffer
[length
++]=ZPU_flip
;
466 length
=genImmediate(buffer
, val
);
469 for (i
=0; i
<length
; i
++)
471 emit_insn(buffer
[i
]);
478 fix_new_exp (frag_now
, frag_now_fix (), 2, oper
, 0, BFD_RELOC_ZPU_IM_14_NONRELAX
);
483 fix_new_exp (frag_now
, frag_now_fix (), 5, oper
, 0, BFD_RELOC_ZPU_IM_32
);
491 static void build_loadstoresp(enum ZPU_OPCODE opcode
, expressionS
*oper
)
494 if (oper
->X_op
!= O_constant
)
496 as_bad (_("illegal loadsp operand"));
499 offset
=oper
->X_add_number
/4;
501 if ((offset
<0)||(offset
>31))
503 as_bad (_("illegal loadsp operand"));
506 /* we invert bit 4. This allows the same sp+offset circuitry to be used
507 * for addsp & loadsp/storesp */
509 emit_insn(opcode
+offset
);
512 static void build_addsp(enum ZPU_OPCODE opcode
, expressionS
*oper
)
515 if (oper
->X_op
!= O_constant
)
517 as_bad (_("illegal addsp operand"));
520 offset
=oper
->X_add_number
/4;
522 if ((offset
<0)||(offset
>15))
524 as_bad (_("illegal addsp operand"));
527 emit_insn(opcode
+offset
);
531 /* Opcode identification and operand analysis. */
534 /* Find the real opcode and its associated operands. We use a progressive
535 approach here. On entry, 'opc' points to the first opcode in the
536 table that matches the opcode name in the source line. We try to
537 isolate an operand, find a possible match in the opcode table.
538 We isolate another operand if no match were found. The table 'operands'
539 is filled while operands are recognized.
541 Returns the opcode pointer that matches the opcode name in the
542 source line and the associated operands. */
543 static struct zpu_opcode
*
544 find_opcode (struct zpu_opcode_def
*opc
)
549 /* Gas line assembler entry point. */
551 /* This is the main entry point for the machine-dependent assembler. str
552 points to a machine-dependent instruction. This function is supposed to
553 emit the frags/bytes it assembles to. */
555 md_assemble (char *str
)
557 struct zpu_opcode_def
*opc
;
558 struct zpu_opcode
*opcode
;
560 unsigned char *op_start
;
561 unsigned char *op_end
;
565 /* Drop leading whitespace. */
566 str
=skip_whites(str
);
568 /* Find the opcode end and get the opcode in 'name'. The opcode is forced
569 lower case (the opcode table only has lower case op-codes). */
570 for (op_start
= op_end
= (unsigned char *) (str
);
571 *op_end
&& nlen
< 20 && !is_end_of_line
[*op_end
] && !isspace(*op_end
);
574 name
[nlen
] = TOLOWER (op_start
[nlen
]);
581 as_bad (_("No instruction or missing opcode."));
585 /* Find the opcode definition given its name. */
586 opc
= (struct zpu_opcode_def
*) hash_find (zpu_hash
, name
);
589 as_bad (_("illegal instruction"));
593 input_line_pointer
= skip_whites(op_end
);
598 opcode
= find_opcode (opc
);
605 if (opcode
->amode
==ADDR_IMMEDIATE
)
610 switch (opcode
->code
)
613 build_addsp(opcode
->code
, &ex
);
618 build_loadstoresp(opcode
->code
, &ex
);
624 build_immediate_insn (&ex
, (opcode
->code
==ZPU_im14
));
628 } else if (opcode
->amode
==ADDR_PCREL
)
635 fix_new_exp (frag_now
, frag_now_fix (), 5, &ex
, 1, BFD_RELOC_ZPU_IM_32_PCREL
);
640 emit_insn(opcode
->code
);
642 if (immediate
&&prevImmediate
)
644 as_bad (_("Two consequtive IM instructions is illegal"));
646 prevImmediate
=immediate
;
649 /* If while processing a fixup, a reloc really needs to be created
650 then it is done here. */
652 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
656 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
657 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
658 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
659 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
660 assert(fixp
->fx_r_type
!= 0);
661 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
663 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
665 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
666 _("Relocation %d is not supported by object file format."),
667 (int) fixp
->fx_r_type
);
671 /* Since we use Rel instead of Rela, encode the vtable entry to be
672 used in the relocation's section offset. */
673 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
674 reloc
->address
= fixp
->fx_offset
;
676 reloc
->addend
= fixp
->fx_offset
;
683 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec ATTRIBUTE_UNUSED
,
684 fragS
*fragP ATTRIBUTE_UNUSED
)
686 as_fatal (_("md_convert_frag invoked\n"));
689 /* Force truly undefined symbols to their maximum size, and generally set up
690 the frag list to be relaxed. */
692 md_estimate_size_before_relax (fragS
*fragP ATTRIBUTE_UNUSED
, asection
*segment ATTRIBUTE_UNUSED
)
694 as_fatal (_("zpu_estimate_size_before_relax invoked\n"));
699 md_apply_fix3 (fixS
*fixP
, valueT
*valP ATTRIBUTE_UNUSED
, segT seg ATTRIBUTE_UNUSED
)
703 /* We shouldn't ever get here because linkrelax is nonzero. */
712 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
715 /* We don't actually support subtracting a symbol. */
716 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
717 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("Expression too complex."));
719 op_type
= fixP
->fx_r_type
;
721 /* The BFD_RELOC_32 is necessary for the support of --gstabs. */
722 where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
724 switch (fixP
->fx_r_type
)
726 case BFD_RELOC_ZPU_IM_7
:
727 case BFD_RELOC_ZPU_IM_14
:
728 case BFD_RELOC_ZPU_IM_21
:
729 case BFD_RELOC_ZPU_IM_28
:
730 case BFD_RELOC_ZPU_IM_32
:
731 case BFD_RELOC_ZPU_IM_7_PCREL
:
732 case BFD_RELOC_ZPU_IM_14_PCREL
:
733 case BFD_RELOC_ZPU_IM_21_PCREL
:
734 case BFD_RELOC_ZPU_IM_28_PCREL
:
735 case BFD_RELOC_ZPU_IM_32_PCREL
:
738 case BFD_RELOC_ZPU_IM_14_NONRELAX
:
742 case BFD_RELOC_32_PCREL
:
743 case BFD_RELOC_24_PCREL
:
744 case BFD_RELOC_16_PCREL
:
745 case BFD_RELOC_8_PCREL
:
746 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("Unsupported relocatoin type."));
751 md_number_to_chars (where
, value
, fixP
->fx_size
);
755 bfd_putb32 ((bfd_vma
) value
, (unsigned char *) where
);
758 bfd_putb16 ((bfd_vma
) value
, (unsigned char *) where
);
762 as_fatal (_("Line %d: unknown 1 relocation type: 0x%x."),
763 fixP
->fx_line
, fixP
->fx_r_type
);
769 md_pcrel_from (fixS
*fixP ATTRIBUTE_UNUSED
)
771 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
776 int ignore ATTRIBUTE_UNUSED
;
778 /* We don't support putting frags in the BSS segment, we fake it
779 by marking in_bss, then looking at s_skip for clues. */
781 subseg_set (bss_section
, 0);
782 demand_empty_rest_of_line ();
785 /* See whether we need to force a relocation into the output file. */
787 tc_zpu_force_relocation (fixS
*fixP
)
789 switch (fixP
->fx_r_type
)
791 case BFD_RELOC_ZPU_IM_7
:
792 case BFD_RELOC_ZPU_IM_14
:
793 case BFD_RELOC_ZPU_IM_21
:
794 case BFD_RELOC_ZPU_IM_28
:
795 case BFD_RELOC_ZPU_IM_32
:
796 case BFD_RELOC_ZPU_IM_7_PCREL
:
797 case BFD_RELOC_ZPU_IM_14_PCREL
:
798 case BFD_RELOC_ZPU_IM_21_PCREL
:
799 case BFD_RELOC_ZPU_IM_28_PCREL
:
800 case BFD_RELOC_ZPU_IM_32_PCREL
:
801 case BFD_RELOC_ZPU_IM_14_NONRELAX
:
807 return generic_force_reloc (fixP
);
811 tc_zpu_fix_adjustable (fixS
*fixP
)
813 switch (fixP
->fx_r_type
)
815 /* For the linker relaxation to work correctly, these relocs
816 need to be on the symbol itself. */
821 case BFD_RELOC_ZPU_IM_7
:
822 case BFD_RELOC_ZPU_IM_14
:
823 case BFD_RELOC_ZPU_IM_21
:
824 case BFD_RELOC_ZPU_IM_28
:
825 case BFD_RELOC_ZPU_IM_32
:
826 case BFD_RELOC_ZPU_IM_7_PCREL
:
827 case BFD_RELOC_ZPU_IM_14_PCREL
:
828 case BFD_RELOC_ZPU_IM_21_PCREL
:
829 case BFD_RELOC_ZPU_IM_28_PCREL
:
830 case BFD_RELOC_ZPU_IM_32_PCREL
:
831 case BFD_RELOC_ZPU_IM_14_NONRELAX
: