1 /* tc-ip2k.c -- Assembler for the Scenix IP2xxx.
2 Copyright (C) 2000, 2002, 2003, 2005, 2006 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, 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 #include "opcodes/ip2k-desc.h"
25 #include "opcodes/ip2k-opc.h"
27 #include "elf/common.h"
31 /* Structure to hold all of the different components describing
32 an individual instruction. */
35 const CGEN_INSN
* insn
;
36 const CGEN_INSN
* orig_insn
;
39 CGEN_INSN_INT buffer
[1];
40 #define INSN_VALUE(buf) (*(buf))
42 unsigned char buffer
[CGEN_MAX_INSN_SIZE
];
43 #define INSN_VALUE(buf) (buf)
48 fixS
* fixups
[GAS_CGEN_MAX_FIXUPS
];
49 int indices
[MAX_OPERAND_INSTANCES
];
53 const char comment_chars
[] = ";";
54 const char line_comment_chars
[] = "#";
55 const char line_separator_chars
[] = "";
56 const char EXP_CHARS
[] = "eE";
57 const char FLT_CHARS
[] = "dD";
59 /* Flag to detect when switching to code section where insn alignment is
61 static int force_code_align
= 0;
63 /* Mach selected from command line. */
64 static int ip2k_mach
= 0;
65 static unsigned ip2k_mach_bitmask
= 0;
69 ip2k_elf_section_rtn (int i
)
75 /* The s_align_ptwo function expects that we are just after a .align
76 directive and it will either try and read the align value or stop
77 if end of line so we must fake it out so it thinks we are at the
79 char *old_input_line_pointer
= input_line_pointer
;
80 input_line_pointer
= "\n";
84 input_line_pointer
= old_input_line_pointer
;
89 ip2k_elf_section_text (int i
)
91 char *old_input_line_pointer
;
94 /* the s_align_ptwo function expects that we are just after a .align
95 directive and it will either try and read the align value or stop if
96 end of line so we must fake it out so it thinks we are at the end of
98 old_input_line_pointer
= input_line_pointer
;
99 input_line_pointer
= "\n";
101 force_code_align
= 0;
103 input_line_pointer
= old_input_line_pointer
;
106 /* The target specific pseudo-ops which we support. */
107 const pseudo_typeS md_pseudo_table
[] =
109 { "text", ip2k_elf_section_text
, 0 },
110 { "sect", ip2k_elf_section_rtn
, 0 },
118 OPTION_CPU_IP2022
= OPTION_MD_BASE
,
122 struct option md_longopts
[] =
124 { "mip2022", no_argument
, NULL
, OPTION_CPU_IP2022
},
125 { "mip2022ext", no_argument
, NULL
, OPTION_CPU_IP2022EXT
},
126 { NULL
, no_argument
, NULL
, 0 },
128 size_t md_longopts_size
= sizeof (md_longopts
);
130 const char * md_shortopts
= "";
133 md_parse_option (int c ATTRIBUTE_UNUSED
, char * arg ATTRIBUTE_UNUSED
)
137 case OPTION_CPU_IP2022
:
138 ip2k_mach
= bfd_mach_ip2022
;
139 ip2k_mach_bitmask
= 1 << MACH_IP2022
;
142 case OPTION_CPU_IP2022EXT
:
143 ip2k_mach
= bfd_mach_ip2022ext
;
144 ip2k_mach_bitmask
= 1 << MACH_IP2022EXT
;
155 md_show_usage (FILE * stream
)
157 fprintf (stream
, _("IP2K specific command line options:\n"));
158 fprintf (stream
, _(" -mip2022 restrict to IP2022 insns \n"));
159 fprintf (stream
, _(" -mip2022ext permit extended IP2022 insn\n"));
166 /* Initialize the `cgen' interface. */
168 /* Set the machine number and endian. */
169 gas_cgen_cpu_desc
= ip2k_cgen_cpu_open (CGEN_CPU_OPEN_MACHS
,
171 CGEN_CPU_OPEN_ENDIAN
,
174 ip2k_cgen_init_asm (gas_cgen_cpu_desc
);
176 /* This is a callback from cgen to gas to parse operands. */
177 cgen_set_parse_operand_fn (gas_cgen_cpu_desc
, gas_cgen_parse_operand
);
179 /* Set the machine type. */
180 bfd_default_set_arch_mach (stdoutput
, bfd_arch_ip2k
, ip2k_mach
);
185 md_assemble (char * str
)
190 /* Initialize GAS's cgen interface for a new instruction. */
191 gas_cgen_init_parse ();
193 insn
.insn
= ip2k_cgen_assemble_insn
194 (gas_cgen_cpu_desc
, str
, & insn
.fields
, insn
.buffer
, & errmsg
);
198 as_bad ("%s", errmsg
);
202 /* Check for special relocation required by SKIP instructions. */
203 if (CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_SKIPA
))
204 /* Unconditional skip has a 1-bit relocation of the current pc, so
205 that we emit either sb pcl.0 or snb pcl.0 depending on whether
206 the PCL (pc + 2) >> 1 is odd or even. */
208 enum cgen_parse_operand_result result_type
;
210 const char *curpc_plus_2
= ".+2";
213 err
= cgen_parse_address (gas_cgen_cpu_desc
, & curpc_plus_2
,
214 IP2K_OPERAND_ADDR16CJP
,
215 BFD_RELOC_IP2K_PC_SKIP
,
216 & result_type
, & value
);
224 /* Doesn't really matter what we pass for RELAX_P here. */
225 gas_cgen_finish_insn (insn
.insn
, insn
.buffer
,
226 CGEN_FIELDS_BITSIZE (& insn
.fields
), 1, NULL
);
230 md_section_align (segT segment
, valueT size
)
232 int align
= bfd_get_section_alignment (stdoutput
, segment
);
234 return ((size
+ (1 << align
) - 1) & (-1 << align
));
239 md_undefined_symbol (char * name ATTRIBUTE_UNUSED
)
245 md_estimate_size_before_relax (fragS
* fragP ATTRIBUTE_UNUSED
,
246 segT segment ATTRIBUTE_UNUSED
)
248 as_fatal (_("md_estimate_size_before_relax\n"));
253 /* *fragP has been relaxed to its final size, and now needs to have
254 the bytes inside it modified to conform to the new size.
256 Called after relaxation is finished.
257 fragP->fr_type == rs_machine_dependent.
258 fragP->fr_subtype is the subtype of what the address relaxed to. */
261 md_convert_frag (bfd
* abfd ATTRIBUTE_UNUSED
,
262 segT sec ATTRIBUTE_UNUSED
,
263 fragS
* fragP ATTRIBUTE_UNUSED
)
268 /* Functions concerning relocs. */
271 md_pcrel_from (fixS
*fixP
)
273 as_fatal (_("md_pcrel_from\n"));
275 /* Return the address of the delay slot. */
276 return fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
280 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
281 Returns BFD_RELOC_NONE if no reloc type can be found.
282 *FIXP may be modified if desired. */
284 bfd_reloc_code_real_type
285 md_cgen_lookup_reloc (const CGEN_INSN
* insn ATTRIBUTE_UNUSED
,
286 const CGEN_OPERAND
* operand
,
287 fixS
* fixP ATTRIBUTE_UNUSED
)
289 bfd_reloc_code_real_type result
;
291 result
= BFD_RELOC_NONE
;
293 switch (operand
->type
)
295 case IP2K_OPERAND_FR
:
296 case IP2K_OPERAND_ADDR16L
:
297 case IP2K_OPERAND_ADDR16H
:
298 case IP2K_OPERAND_LIT8
:
299 /* These may have been processed at parse time. */
300 if (fixP
->fx_cgen
.opinfo
!= 0)
301 result
= fixP
->fx_cgen
.opinfo
;
302 fixP
->fx_no_overflow
= 1;
305 case IP2K_OPERAND_ADDR16CJP
:
306 result
= fixP
->fx_cgen
.opinfo
;
307 if (result
== 0 || result
== BFD_RELOC_NONE
)
308 result
= BFD_RELOC_IP2K_ADDR16CJP
;
309 fixP
->fx_no_overflow
= 1;
312 case IP2K_OPERAND_ADDR16P
:
313 result
= BFD_RELOC_IP2K_PAGE3
;
314 fixP
->fx_no_overflow
= 1;
318 result
= BFD_RELOC_NONE
;
326 /* Write a value out to the object file, using the appropriate endianness. */
329 md_number_to_chars (char * buf
, valueT val
, int n
)
331 number_to_chars_bigendian (buf
, val
, n
);
334 /* Turn a string in input_line_pointer into a floating point constant of type
335 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
336 emitted is stored in *sizeP . An error message is returned, or NULL on
339 /* Equal to MAX_PRECISION in atof-ieee.c */
340 #define MAX_LITTLENUMS 6
343 md_atof (int type
, char * litP
, int * sizeP
)
346 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
347 LITTLENUM_TYPE
*wordP
;
366 /* FIXME: Some targets allow other format chars for bigger sizes here. */
370 return _("Bad call to md_atof()");
373 t
= atof_ieee (input_line_pointer
, type
, words
);
375 input_line_pointer
= t
;
376 * sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
378 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
379 the ip2k endianness. */
380 for (wordP
= words
; prec
--;)
382 md_number_to_chars (litP
, (valueT
) (*wordP
++), sizeof (LITTLENUM_TYPE
));
383 litP
+= sizeof (LITTLENUM_TYPE
);
390 /* See whether we need to force a relocation into the output file.
391 Force most of them, since the linker's bfd relocation engine
392 understands range limits better than gas' cgen fixup engine.
393 Consider the case of a fixup intermediate value being larger than
394 the instruction it will be eventually encoded within. */
397 ip2k_force_relocation (fixS
* fix
)
399 switch (fix
->fx_r_type
)
401 case BFD_RELOC_IP2K_FR9
:
402 case BFD_RELOC_IP2K_FR_OFFSET
:
403 case BFD_RELOC_IP2K_BANK
:
404 case BFD_RELOC_IP2K_ADDR16CJP
:
405 case BFD_RELOC_IP2K_PAGE3
:
406 case BFD_RELOC_IP2K_LO8DATA
:
407 case BFD_RELOC_IP2K_HI8DATA
:
408 case BFD_RELOC_IP2K_EX8DATA
:
409 case BFD_RELOC_IP2K_LO8INSN
:
410 case BFD_RELOC_IP2K_HI8INSN
:
411 case BFD_RELOC_IP2K_PC_SKIP
:
412 case BFD_RELOC_IP2K_TEXT
:
416 if (fix
->fx_subsy
&& S_IS_DEFINED (fix
->fx_subsy
)
417 && fix
->fx_addsy
&& S_IS_DEFINED (fix
->fx_addsy
)
418 && (S_GET_SEGMENT (fix
->fx_addsy
)->flags
& SEC_CODE
))
420 fix
->fx_r_type
= BFD_RELOC_IP2K_TEXT
;
429 return generic_force_reloc (fix
);
433 ip2k_apply_fix (fixS
*fixP
, valueT
*valueP
, segT seg
)
435 if (fixP
->fx_r_type
== BFD_RELOC_IP2K_TEXT
439 *valueP
= ((int)(* valueP
)) / 2;
440 fixP
->fx_r_type
= BFD_RELOC_16
;
442 else if (fixP
->fx_r_type
== BFD_RELOC_UNUSED
+ IP2K_OPERAND_FR
)
444 /* Must be careful when we are fixing up an FR. We could be
445 fixing up an offset to (SP) or (DP) in which case we don't
446 want to step on the top 2 bits of the FR operand. The
447 gas_cgen_md_apply_fix doesn't know any better and overwrites
448 the entire operand. We counter this by adding the bits
450 char *where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
452 /* Canonical name, since used a lot. */
453 CGEN_CPU_DESC cd
= gas_cgen_cpu_desc
;
454 CGEN_INSN_INT insn_value
455 = cgen_get_insn_value (cd
, (unsigned char *) where
,
456 CGEN_INSN_BITSIZE (fixP
->fx_cgen
.insn
));
457 /* Preserve (DP) or (SP) specification. */
458 *valueP
+= (insn_value
& 0x180);
461 gas_cgen_md_apply_fix (fixP
, valueP
, seg
);
465 ip2k_elf_section_flags (int flags
,
466 int attr ATTRIBUTE_UNUSED
,
467 int type ATTRIBUTE_UNUSED
)
469 /* This is used to detect when the section changes to an executable section.
470 This function is called by the elf section processing. When we note an
471 executable section specifier we set an internal flag to denote when
472 word alignment should be forced. */
473 if (flags
& SEC_CODE
)
474 force_code_align
= 1;