1 /* tc-xstormy16.c -- Assembler for the Sanyo XSTORMY16.
2 Copyright 2000, 2001, 2002, 2003 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "opcodes/xstormy16-desc.h"
26 #include "opcodes/xstormy16-opc.h"
29 /* Structure to hold all of the different components describing
30 an individual instruction. */
33 const CGEN_INSN
* insn
;
34 const CGEN_INSN
* orig_insn
;
37 CGEN_INSN_INT buffer
[1];
38 #define INSN_VALUE(buf) (*(buf))
40 unsigned char buffer
[CGEN_MAX_INSN_SIZE
];
41 #define INSN_VALUE(buf) (buf)
46 fixS
* fixups
[GAS_CGEN_MAX_FIXUPS
];
47 int indices
[MAX_OPERAND_INSTANCES
];
51 const char comment_chars
[] = ";";
52 const char line_comment_chars
[] = "#";
53 const char line_separator_chars
[] = "|";
54 const char EXP_CHARS
[] = "eE";
55 const char FLT_CHARS
[] = "dD";
57 #define O_fptr_symbol (O_max + 1)
59 #define XSTORMY16_SHORTOPTS ""
60 const char * md_shortopts
= XSTORMY16_SHORTOPTS
;
62 struct option md_longopts
[] =
64 {NULL
, no_argument
, NULL
, 0}
66 size_t md_longopts_size
= sizeof (md_longopts
);
69 md_parse_option (c
, arg
)
70 int c ATTRIBUTE_UNUSED
;
71 char * arg ATTRIBUTE_UNUSED
;
77 md_show_usage (stream
)
80 fprintf (stream
, _(" XSTORMY16 specific command line options:\n"));
83 /* The target specific pseudo-ops which we support. */
84 const pseudo_typeS md_pseudo_table
[] =
94 /* Initialize the `cgen' interface. */
96 /* Set the machine number and endian. */
97 gas_cgen_cpu_desc
= xstormy16_cgen_cpu_open (CGEN_CPU_OPEN_MACHS
, 0,
101 xstormy16_cgen_init_asm (gas_cgen_cpu_desc
);
103 /* This is a callback from cgen to gas to parse operands. */
104 cgen_set_parse_operand_fn (gas_cgen_cpu_desc
, gas_cgen_parse_operand
);
107 static bfd_boolean skipping_fptr
= FALSE
;
116 /* Make sure that if we had an erroneous input line which triggered
117 the skipping_fptr boolean that it does not affect following lines. */
118 skipping_fptr
= FALSE
;
120 /* Initialize GAS's cgen interface for a new instruction. */
121 gas_cgen_init_parse ();
123 insn
.insn
= xstormy16_cgen_assemble_insn
124 (gas_cgen_cpu_desc
, str
, & insn
.fields
, insn
.buffer
, & errmsg
);
132 /* Doesn't really matter what we pass for RELAX_P here. */
133 gas_cgen_finish_insn (insn
.insn
, insn
.buffer
,
134 CGEN_FIELDS_BITSIZE (& insn
.fields
), 0, NULL
);
141 if (*input_line_pointer
!= '@')
144 if (strncmp (input_line_pointer
+1, "fptr", 4) == 0)
146 input_line_pointer
+= 5;
148 if (*input_line_pointer
!= '(')
150 as_bad ("Expected '('");
153 input_line_pointer
++;
157 if (*input_line_pointer
!= ')')
159 as_bad ("Missing ')'");
162 input_line_pointer
++;
165 if (e
->X_op
!= O_symbol
)
166 as_bad ("Not a symbolic expression");
167 else if (* input_line_pointer
== '-')
168 /* We are computing the difference of two function pointers
171 .hword @fptr (foo) - @fptr (bar)
173 In this situation we do not want to generate O_fptr_symbol
174 operands because the result is an absolute value, not a
177 We need to make the check here, rather than when the fixup
178 is generated as the function names (foo & bar in the above
179 example) might be local symbols and we want the expression
180 to be evaluated now. This kind of thing can happen when
181 gcc is generating computed gotos. */
182 skipping_fptr
= TRUE
;
183 else if (skipping_fptr
)
184 skipping_fptr
= FALSE
;
186 e
->X_op
= O_fptr_symbol
;
191 ignore_rest_of_line ();
194 /* Called while parsing data to create a fixup.
195 Create BFD_RELOC_XSTORMY16_FPTR16 relocations. */
198 xstormy16_cons_fix_new (f
, where
, nbytes
, exp
)
204 bfd_reloc_code_real_type code
;
207 if (exp
->X_op
== O_fptr_symbol
)
211 as_bad ("unsupported fptr fixup size %d", nbytes
);
214 exp
->X_op
= O_symbol
;
215 code
= BFD_RELOC_XSTORMY16_FPTR16
;
217 else if (nbytes
== 1)
219 else if (nbytes
== 2)
221 else if (nbytes
== 4)
225 as_bad ("unsupported fixup size %d", nbytes
);
229 fix
= fix_new_exp (f
, where
, nbytes
, exp
, 0, code
);
232 /* Called while parsing an instruction to create a fixup.
233 Create BFD_RELOC_XSTORMY16_FPTR16 relocations. */
236 xstormy16_cgen_record_fixup_exp (frag
, where
, insn
, length
, operand
, opinfo
, exp
)
239 const CGEN_INSN
* insn
;
241 const CGEN_OPERAND
* operand
;
246 operatorT op
= exp
->X_op
;
248 if (op
== O_fptr_symbol
)
249 exp
->X_op
= O_symbol
;
251 fixP
= gas_cgen_record_fixup_exp (frag
, where
, insn
, length
,
252 operand
, opinfo
, exp
);
254 if (op
== O_fptr_symbol
)
256 if (operand
->type
!= XSTORMY16_OPERAND_IMM16
)
257 as_bad ("unsupported fptr fixup");
260 fixP
->fx_r_type
= BFD_RELOC_XSTORMY16_FPTR16
;
269 md_section_align (segment
, size
)
273 int align
= bfd_get_section_alignment (stdoutput
, segment
);
274 return ((size
+ (1 << align
) - 1) & (-1 << align
));
278 md_undefined_symbol (name
)
279 char * name ATTRIBUTE_UNUSED
;
284 /* Return an initial guess of the length by which a fragment must grow to
285 hold a branch to reach its destination.
286 Also updates fr_type/fr_subtype as necessary.
288 Called just before doing relaxation.
289 Any symbol that is now undefined will not become defined.
290 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
291 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
292 Although it may not be explicit in the frag, pretend fr_var starts with a
296 md_estimate_size_before_relax (fragP
, segment
)
297 fragS
* fragP ATTRIBUTE_UNUSED
;
298 segT segment ATTRIBUTE_UNUSED
;
300 /* No assembler relaxation is defined (or necessary) for this port. */
304 /* *fragP has been relaxed to its final size, and now needs to have
305 the bytes inside it modified to conform to the new size.
307 Called after relaxation is finished.
308 fragP->fr_type == rs_machine_dependent.
309 fragP->fr_subtype is the subtype of what the address relaxed to. */
312 md_convert_frag (abfd
, sec
, fragP
)
313 bfd
* abfd ATTRIBUTE_UNUSED
;
314 segT sec ATTRIBUTE_UNUSED
;
315 fragS
* fragP ATTRIBUTE_UNUSED
;
317 /* No assembler relaxation is defined (or necessary) for this port. */
321 /* Functions concerning relocs. */
323 /* The location from which a PC relative jump should be calculated,
324 given a PC relative reloc. */
327 md_pcrel_from_section (fixP
, sec
)
331 if (fixP
->fx_addsy
!= (symbolS
*) NULL
332 && (! S_IS_DEFINED (fixP
->fx_addsy
)
333 || S_GET_SEGMENT (fixP
->fx_addsy
) != sec
))
335 /* The symbol is undefined (or is defined but not in this section).
336 Let the linker figure it out. */
340 return fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
343 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
344 Returns BFD_RELOC_NONE if no reloc type can be found.
345 *FIXP may be modified if desired. */
347 bfd_reloc_code_real_type
348 md_cgen_lookup_reloc (insn
, operand
, fixP
)
349 const CGEN_INSN
* insn ATTRIBUTE_UNUSED
;
350 const CGEN_OPERAND
* operand
;
353 switch (operand
->type
)
355 case XSTORMY16_OPERAND_IMM2
:
356 case XSTORMY16_OPERAND_IMM3
:
357 case XSTORMY16_OPERAND_IMM3B
:
358 case XSTORMY16_OPERAND_IMM4
:
359 case XSTORMY16_OPERAND_HMEM8
:
360 return BFD_RELOC_NONE
;
362 case XSTORMY16_OPERAND_IMM12
:
364 return BFD_RELOC_XSTORMY16_12
;
366 case XSTORMY16_OPERAND_IMM8
:
367 case XSTORMY16_OPERAND_LMEM8
:
368 return fixP
->fx_pcrel
? BFD_RELOC_8_PCREL
: BFD_RELOC_8
;
370 case XSTORMY16_OPERAND_IMM16
:
371 /* This might have been processed at parse time. */
373 if (fixP
->fx_cgen
.opinfo
&& fixP
->fx_cgen
.opinfo
!= BFD_RELOC_NONE
)
374 return fixP
->fx_cgen
.opinfo
;
375 return fixP
->fx_pcrel
? BFD_RELOC_16_PCREL
: BFD_RELOC_16
;
377 case XSTORMY16_OPERAND_ABS24
:
378 return BFD_RELOC_XSTORMY16_24
;
380 case XSTORMY16_OPERAND_REL8_4
:
381 fixP
->fx_addnumber
-= 2;
382 case XSTORMY16_OPERAND_REL8_2
:
383 fixP
->fx_addnumber
-= 2;
385 return BFD_RELOC_8_PCREL
;
387 case XSTORMY16_OPERAND_REL12
:
389 /* Fall through... */
390 case XSTORMY16_OPERAND_REL12A
:
391 fixP
->fx_addnumber
-= 2;
393 return BFD_RELOC_XSTORMY16_REL_12
;
395 default : /* avoid -Wall warning */
400 /* See whether we need to force a relocation into the output file.
401 This is used to force out switch and PC relative relocations when
405 xstormy16_force_relocation (fix
)
408 if (fix
->fx_r_type
== BFD_RELOC_XSTORMY16_FPTR16
)
411 return generic_force_reloc (fix
);
414 /* Return true if a relocation against a symbol may be replaced with
415 a relocation against section+offset. */
418 xstormy16_fix_adjustable (fixP
)
421 /* We need the symbol name for the VTABLE entries. */
422 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
423 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
426 if (fixP
->fx_r_type
== BFD_RELOC_XSTORMY16_FPTR16
)
432 /* This is a copy of gas_cgen_md_apply_fix3, with some enhancements to
433 do various things that would not be valid for all ports. */
436 xstormy16_md_apply_fix3 (fixP
, valueP
, seg
)
439 segT seg ATTRIBUTE_UNUSED
;
441 char *where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
442 valueT value
= *valueP
;
443 /* Canonical name, since used a lot. */
444 CGEN_CPU_DESC cd
= gas_cgen_cpu_desc
;
446 /* md_cgen_lookup_reloc() will adjust this to compensate for where
447 in the opcode the relocation happens, for pcrel relocations. We
448 have no other way of keeping track of what this offset needs to
450 fixP
->fx_addnumber
= 0;
452 /* This port has pc-relative relocs and DIFF_EXPR_OK defined, so
453 it must deal with turning a BFD_RELOC_{8,16,32,64} into a
454 BFD_RELOC_*_PCREL for the case of
459 switch (fixP
->fx_r_type
)
462 fixP
->fx_r_type
= BFD_RELOC_8_PCREL
;
465 fixP
->fx_r_type
= BFD_RELOC_16_PCREL
;
468 fixP
->fx_r_type
= BFD_RELOC_32_PCREL
;
471 fixP
->fx_r_type
= BFD_RELOC_64_PCREL
;
477 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
480 /* We don't actually support subtracting a symbol. */
481 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
482 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
484 if ((int) fixP
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
486 int opindex
= (int) fixP
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
487 const CGEN_OPERAND
*operand
= cgen_operand_lookup_by_num (cd
, opindex
);
489 bfd_reloc_code_real_type reloc_type
;
490 CGEN_FIELDS
*fields
= alloca (CGEN_CPU_SIZEOF_FIELDS (cd
));
491 const CGEN_INSN
*insn
= fixP
->fx_cgen
.insn
;
493 /* If the reloc has been fully resolved finish the operand here. */
494 /* FIXME: This duplicates the capabilities of code in BFD. */
497 CGEN_CPU_SET_FIELDS_BITSIZE (cd
) (fields
, CGEN_INSN_BITSIZE (insn
));
498 CGEN_CPU_SET_VMA_OPERAND (cd
) (cd
, opindex
, fields
, (bfd_vma
) value
);
502 CGEN_INSN_INT insn_value
=
503 cgen_get_insn_value (cd
, where
, CGEN_INSN_BITSIZE (insn
));
505 /* ??? 0 is passed for `pc'. */
506 errmsg
= CGEN_CPU_INSERT_OPERAND (cd
) (cd
, opindex
, fields
,
507 &insn_value
, (bfd_vma
) 0);
508 cgen_put_insn_value (cd
, where
, CGEN_INSN_BITSIZE (insn
),
512 /* ??? 0 is passed for `pc'. */
513 errmsg
= CGEN_CPU_INSERT_OPERAND (cd
) (cd
, opindex
, fields
, where
,
517 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, "%s", errmsg
);
523 /* The operand isn't fully resolved. Determine a BFD reloc value
524 based on the operand information and leave it to
525 bfd_install_relocation. Note that this doesn't work when
528 reloc_type
= md_cgen_lookup_reloc (insn
, operand
, fixP
);
529 if (reloc_type
!= BFD_RELOC_NONE
)
531 fixP
->fx_r_type
= reloc_type
;
535 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
536 _("unresolved expression that must be resolved"));
541 else if (fixP
->fx_done
)
543 /* We're finished with this fixup. Install it because
544 bfd_install_relocation won't be called to do it. */
545 switch (fixP
->fx_r_type
)
548 md_number_to_chars (where
, value
, 1);
551 md_number_to_chars (where
, value
, 2);
554 md_number_to_chars (where
, value
, 4);
557 md_number_to_chars (where
, value
, 8);
560 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
561 _("internal error: can't install fix for reloc type %d (`%s')"),
562 fixP
->fx_r_type
, bfd_get_reloc_code_name (fixP
->fx_r_type
));
568 /* bfd_install_relocation will be called to finish things up. */
571 /* This is a RELA port. Thus, it does not need to store a
572 value if it is going to make a reloc. What's more, when
573 assembling a line like
577 we'll get a spurious error message if we try to stuff 0x7f00 into
582 /* Tuck `value' away for use by tc_gen_reloc.
583 See the comment describing fx_addnumber in write.h.
584 This field is misnamed (or misused :-).
585 We do not do this for pc-relative relocs against a
586 defined symbol, since it will be done for us. */
587 if ((! fixP
->fx_pcrel
)
588 || (fixP
->fx_addsy
&& ! S_IS_DEFINED (fixP
->fx_addsy
)))
589 fixP
->fx_addnumber
+= value
;
593 /* Write a value out to the object file, using the appropriate endianness. */
596 md_number_to_chars (buf
, val
, n
)
601 number_to_chars_littleendian (buf
, val
, n
);
604 /* Turn a string in input_line_pointer into a floating point constant of type
605 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
606 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
609 /* Equal to MAX_PRECISION in atof-ieee.c */
610 #define MAX_LITTLENUMS 6
613 md_atof (type
, litP
, sizeP
)
619 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
620 LITTLENUM_TYPE
*wordP
;
635 /* FIXME: Some targets allow other format chars for bigger sizes here. */
639 return _("Bad call to md_atof()");
642 t
= atof_ieee (input_line_pointer
, type
, words
);
644 input_line_pointer
= t
;
645 * sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
647 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
648 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
649 the littleendianness of the processor. */
650 for (wordP
= words
+ prec
- 1; prec
--;)
652 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
653 litP
+= sizeof (LITTLENUM_TYPE
);