1 /* tc-ft32.c -- Assemble code for ft32
2 Copyright (C) 2008-2017 Free Software Foundation, Inc.
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 3, 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, 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* Contributed by Anthony Green <green@spindazzle.org>. */
24 #include "safe-ctype.h"
25 #include "opcode/ft32.h"
27 extern const ft32_opc_info_t ft32_opc_info
[128];
29 const char comment_chars
[] = "#";
30 const char line_separator_chars
[] = ";";
31 const char line_comment_chars
[] = "#";
33 static int pending_reloc
;
34 static struct hash_control
*opcode_hash_control
;
36 static valueT
md_chars_to_number (char * buf
, int n
);
38 const pseudo_typeS md_pseudo_table
[] =
43 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
44 const char EXP_CHARS
[] = "eE";
46 /* This function is called once, at assembler startup time. It sets
47 up the hash table with all the opcodes in it, and also initializes
48 some aliases for compatibility with other assemblers. */
53 const ft32_opc_info_t
*opcode
;
54 opcode_hash_control
= hash_new ();
56 /* Insert names into hash table. */
57 for (opcode
= ft32_opc_info
; opcode
->name
; opcode
++)
58 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
60 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, 0);
63 /* Parse an expression and then restore the input line pointer. */
66 parse_exp_save_ilp (char *s
, expressionS
*op
)
68 char *save
= input_line_pointer
;
70 input_line_pointer
= s
;
72 s
= input_line_pointer
;
73 input_line_pointer
= save
;
78 parse_condition (char **ptr
)
85 { "gt," , (2 << FT32_FLD_CR_BIT
) | (5 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
86 { "gte," , (2 << FT32_FLD_CR_BIT
) | (4 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
87 { "lt," , (2 << FT32_FLD_CR_BIT
) | (4 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
88 { "lte," , (2 << FT32_FLD_CR_BIT
) | (5 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
89 { "a," , (2 << FT32_FLD_CR_BIT
) | (6 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
90 { "ae," , (2 << FT32_FLD_CR_BIT
) | (1 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
91 { "be," , (2 << FT32_FLD_CR_BIT
) | (6 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
92 { "b," , (2 << FT32_FLD_CR_BIT
) | (1 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
93 { "nz," , (2 << FT32_FLD_CR_BIT
) | (0 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
94 { "z," , (2 << FT32_FLD_CR_BIT
) | (0 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
95 { "nc," , (2 << FT32_FLD_CR_BIT
) | (1 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
96 { "c," , (2 << FT32_FLD_CR_BIT
) | (1 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
97 { "no," , (2 << FT32_FLD_CR_BIT
) | (2 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
98 { "o," , (2 << FT32_FLD_CR_BIT
) | (2 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
99 { "ns," , (2 << FT32_FLD_CR_BIT
) | (3 << FT32_FLD_CB_BIT
) | (0 << FT32_FLD_CV_BIT
)},
100 { "s," , (2 << FT32_FLD_CR_BIT
) | (3 << FT32_FLD_CB_BIT
) | (1 << FT32_FLD_CV_BIT
)},
104 for (pc
= ccs
; pc
->name
; pc
++)
106 if (memcmp(pc
->name
, s
, strlen(pc
->name
)) == 0)
108 *ptr
+= strlen(pc
->name
) - 1;
116 parse_decimal (char **ptr
)
121 while (('0' <= *s
) && (*s
<= '9'))
131 parse_register_operand (char **ptr
)
138 as_bad (_("expecting register"));
139 ignore_rest_of_line ();
142 if ((s
[1] == 's') && (s
[2] == 'p'))
146 else if ((s
[1] == 'c') && (s
[2] == 'c'))
150 else if ((s
[1] == 'f') && (s
[2] == 'p'))
154 else if (s
[1] == 'r')
157 if ((reg
< 0) || (reg
> 9))
159 as_bad (_("illegal register number"));
160 ignore_rest_of_line ();
163 if ((reg
== 1) || (reg
== 2) || (reg
== 3))
166 if ((r2
>= 0) && (r2
<= 9))
168 reg
= (reg
* 10) + r2
;
175 as_bad (_("illegal register number"));
176 ignore_rest_of_line ();
185 /* This is the guts of the machine-dependent assembler. STR points to
186 a machine dependent instruction. This function is supposed to emit
187 the frags/bytes it assembles to. */
190 md_assemble (char *str
)
195 ft32_opc_info_t
*opcode
;
207 /* Drop leading whitespace. */
211 /* Find the op code end. */
214 *op_end
&& !is_end_of_line
[*op_end
& 0xff] && *op_end
!= ' ' && *op_end
!= '.';
222 as_bad (_("can't find opcode "));
224 opcode
= (ft32_opc_info_t
*) hash_find (opcode_hash_control
, op_start
);
229 as_bad (_("unknown opcode %s"), op_start
);
253 as_bad (_("unknown width specifier '.%c'"), op_end
[1]);
260 dw
= 2; /* default is ".l" */
262 b
|= dw
<< FT32_FLD_DW_BIT
;
265 while (ISSPACE (*op_end
))
268 output
= frag_more (4);
277 case FT32_FLD_CBCRCV
:
278 b
|= parse_condition( &op_end
);
281 b
|= parse_decimal (&op_end
) << FT32_FLD_CB_BIT
;
284 b
|= parse_register_operand (&op_end
) << FT32_FLD_R_D_BIT
;
287 b
|= (parse_register_operand (&op_end
) - 28) << FT32_FLD_CR_BIT
;
290 b
|= parse_decimal (&op_end
) << FT32_FLD_CV_BIT
;
293 b
|= parse_register_operand (&op_end
) << FT32_FLD_R_1_BIT
;
298 b
|= parse_register_operand (&op_end
) << FT32_FLD_RIMM_BIT
;
302 b
|= 0x400 << FT32_FLD_RIMM_BIT
;
303 op_end
= parse_exp_save_ilp (op_end
, &arg
);
304 fix_new_exp (frag_now
,
305 (output
- frag_now
->fr_literal
),
313 b
|= parse_register_operand (&op_end
) << FT32_FLD_R_2_BIT
;
316 op_end
= parse_exp_save_ilp (op_end
, &arg
);
317 fix_new_exp (frag_now
,
318 (output
- frag_now
->fr_literal
),
325 op_end
= parse_exp_save_ilp (op_end
, &arg
);
326 fix_new_exp (frag_now
,
327 (output
- frag_now
->fr_literal
),
334 op_end
= parse_exp_save_ilp (op_end
, &arg
);
335 fix_new_exp (frag_now
,
336 (output
- frag_now
->fr_literal
),
343 op_end
= parse_exp_save_ilp (op_end
, &arg
);
344 fix_new_exp (frag_now
,
345 (output
- frag_now
->fr_literal
),
352 op_end
= parse_exp_save_ilp (op_end
, &arg
);
353 fix_new_exp (frag_now
,
354 (output
- frag_now
->fr_literal
),
360 case FT32_FLD_R_D_POST
:
361 b
|= parse_register_operand (&op_end
) << FT32_FLD_R_D_BIT
;
363 case FT32_FLD_R_1_POST
:
364 b
|= parse_register_operand (&op_end
) << FT32_FLD_R_1_BIT
;
367 as_bad (_("internal error in argument parsing"));
373 while (ISSPACE (*op_end
))
378 as_bad (_("expected comma separator"));
379 ignore_rest_of_line ();
383 while (ISSPACE (*op_end
))
389 as_warn (_("extra stuff on line ignored"));
391 output
[idx
++] = 0xff & (b
>> 0);
392 output
[idx
++] = 0xff & (b
>> 8);
393 output
[idx
++] = 0xff & (b
>> 16);
394 output
[idx
++] = 0xff & (b
>> 24);
396 dwarf2_emit_insn (4);
398 while (ISSPACE (*op_end
))
402 as_warn ("extra stuff on line ignored");
405 as_bad ("Something forgot to clean up\n");
408 /* Turn a string in input_line_pointer into a floating point constant
409 of type type, and store the appropriate bytes in *LITP. The number
410 of LITTLENUMS emitted is stored in *SIZEP . An error message is
411 returned, or NULL on OK. */
414 md_atof (int type
, char *litP
, int *sizeP
)
417 LITTLENUM_TYPE words
[4];
433 return _("bad call to md_atof");
436 t
= atof_ieee (input_line_pointer
, type
, words
);
438 input_line_pointer
= t
;
442 for (i
= prec
- 1; i
>= 0; i
--)
444 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
451 const char *md_shortopts
= "";
453 struct option md_longopts
[] =
455 {NULL
, no_argument
, NULL
, 0}
457 size_t md_longopts_size
= sizeof (md_longopts
);
459 /* We have no target specific options yet, so these next
460 two functions are empty. */
462 md_parse_option (int c ATTRIBUTE_UNUSED
, const char *arg ATTRIBUTE_UNUSED
)
468 md_show_usage (FILE *stream ATTRIBUTE_UNUSED
)
472 /* Convert from target byte order to host byte order. */
475 md_chars_to_number (char * buf
, int n
)
478 unsigned char * where
= (unsigned char *) buf
;
483 result
|= (where
[n
] & 255);
488 /* Apply a fixup to the object file. */
491 md_apply_fix (fixS
*fixP ATTRIBUTE_UNUSED
,
492 valueT
* valP ATTRIBUTE_UNUSED
, segT seg ATTRIBUTE_UNUSED
)
494 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
498 switch (fixP
->fx_r_type
)
516 case BFD_RELOC_FT32_10
:
519 newval
= md_chars_to_number (buf
, 2);
520 newval
|= (val
& ((1 << 10) - 1)) << FT32_FLD_RIMM_BIT
;
521 md_number_to_chars (buf
, newval
, 2);
524 case BFD_RELOC_FT32_20
:
527 newval
= md_chars_to_number (buf
, 3);
528 newval
|= val
& ((1 << 20) - 1);
529 md_number_to_chars (buf
, newval
, 3);
532 case BFD_RELOC_FT32_17
:
535 newval
= md_chars_to_number (buf
, 3);
536 newval
|= val
& ((1 << 17) - 1);
537 md_number_to_chars (buf
, newval
, 3);
540 case BFD_RELOC_FT32_18
:
543 newval
= md_chars_to_number (buf
, 4);
544 newval
|= (val
>> 2) & ((1 << 18) - 1);
545 md_number_to_chars (buf
, newval
, 4);
552 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
554 // printf("fx_addsy=%p fixP->fx_pcrel=%d fx_done=%d\n", fixP->fx_addsy, fixP->fx_pcrel, fixP->fx_done);
558 md_number_to_chars (char *ptr
, valueT use
, int nbytes
)
560 number_to_chars_littleendian (ptr
, use
, nbytes
);
563 /* Generate a machine-dependent relocation. */
565 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixP
)
568 bfd_reloc_code_real_type code
;
570 switch (fixP
->fx_r_type
)
575 case BFD_RELOC_FT32_10
:
576 case BFD_RELOC_FT32_20
:
577 case BFD_RELOC_FT32_17
:
578 case BFD_RELOC_FT32_18
:
579 code
= fixP
->fx_r_type
;
582 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
583 _("Semantics error. This type of operand can not be relocated, it must be an assembly-time constant"));
587 relP
= XNEW (arelent
);
588 gas_assert (relP
!= 0);
589 relP
->sym_ptr_ptr
= XNEW (asymbol
*);
590 *relP
->sym_ptr_ptr
= symbol_get_bfdsym (fixP
->fx_addsy
);
591 relP
->address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
593 relP
->addend
= fixP
->fx_offset
;
595 relP
->howto
= bfd_reloc_type_lookup (stdoutput
, code
);
600 name
= S_GET_NAME (fixP
->fx_addsy
);
602 name
= _("<unknown>");
603 as_fatal (_("Cannot generate relocation type for symbol %s, code %s"),
604 name
, bfd_get_reloc_code_name (code
));