1 /* tc-ip2k.c -- Assembler for the Scenix IP2xxx.
2 Copyright (C) 2000, 2002 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 "dwarf2dbg.h"
28 #include "opcodes/ip2k-desc.h"
29 #include "opcodes/ip2k-opc.h"
31 #include "elf/common.h"
35 /* Structure to hold all of the different components describing
36 an individual instruction. */
39 const CGEN_INSN * insn;
40 const CGEN_INSN * orig_insn;
43 CGEN_INSN_INT buffer [1];
44 #define INSN_VALUE(buf) (*(buf))
46 unsigned char buffer [CGEN_MAX_INSN_SIZE];
47 #define INSN_VALUE(buf) (buf)
52 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
53 int indices [MAX_OPERAND_INSTANCES];
57 const char comment_chars[] = ";";
58 const char line_comment_chars[] = "#";
59 const char line_separator_chars[] = "";
60 const char EXP_CHARS[] = "eE";
61 const char FLT_CHARS[] = "dD";
63 static void ip2k_elf_section_text (int);
64 static void ip2k_elf_section_rtn (int);
66 /* The target specific pseudo-ops which we support. */
67 const pseudo_typeS md_pseudo_table[] =
69 { "file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0 },
70 { "loc", dwarf2_directive_loc, 0 },
71 { "text", ip2k_elf_section_text, 0 },
72 { "sect", ip2k_elf_section_rtn, 0 },
78 #define OPTION_CPU_IP2022 (OPTION_MD_BASE)
79 #define OPTION_CPU_IP2022EXT (OPTION_MD_BASE+1)
81 struct option md_longopts[] =
83 { "mip2022", no_argument, NULL, OPTION_CPU_IP2022 },
84 { "mip2022ext", no_argument, NULL, OPTION_CPU_IP2022EXT },
85 { NULL, no_argument, NULL, 0 },
87 size_t md_longopts_size = sizeof (md_longopts);
89 const char * md_shortopts = "";
91 /* Flag to detect when switching to code section where insn alignment is
93 static int force_code_align = 0;
95 /* Mach selected from command line. */
97 unsigned ip2k_mach_bitmask = 0;
100 md_parse_option (c, arg)
101 int c ATTRIBUTE_UNUSED;
102 char * arg ATTRIBUTE_UNUSED;
106 case OPTION_CPU_IP2022:
107 ip2k_mach = bfd_mach_ip2022;
108 ip2k_mach_bitmask = 1 << MACH_IP2022;
111 case OPTION_CPU_IP2022EXT:
112 ip2k_mach = bfd_mach_ip2022ext;
113 ip2k_mach_bitmask = 1 << MACH_IP2022EXT;
125 md_show_usage (stream)
128 fprintf (stream, _("IP2K specific command line options:\n"));
129 fprintf (stream, _(" -mip2022 restrict to IP2022 insns \n"));
130 fprintf (stream, _(" -mip2022ext permit extended IP2022 insn\n"));
137 /* Initialize the `cgen' interface. */
139 /* Set the machine number and endian. */
140 gas_cgen_cpu_desc = ip2k_cgen_cpu_open (CGEN_CPU_OPEN_MACHS,
142 CGEN_CPU_OPEN_ENDIAN,
145 ip2k_cgen_init_asm (gas_cgen_cpu_desc);
147 /* This is a callback from cgen to gas to parse operands. */
148 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
150 /* Set the machine type. */
151 bfd_default_set_arch_mach (stdoutput, bfd_arch_ip2k, ip2k_mach);
162 /* Initialize GAS's cgen interface for a new instruction. */
163 gas_cgen_init_parse ();
165 insn.insn = ip2k_cgen_assemble_insn
166 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
170 as_bad ("%s", errmsg);
174 /* Check for special relocation required by SKIP instructions. */
175 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SKIPA))
176 /* Unconditional skip has a 1-bit relocation of the current pc, so
177 that we emit either sb pcl.0 or snb pcl.0 depending on whether
178 the PCL (pc + 2) >> 1 is odd or even. */
180 enum cgen_parse_operand_result result_type;
182 const char *curpc_plus_2 = ".+2";
185 err = cgen_parse_address (gas_cgen_cpu_desc, & curpc_plus_2,
186 IP2K_OPERAND_ADDR16CJP,
187 BFD_RELOC_IP2K_PC_SKIP,
188 & result_type, & value);
196 /* Doesn't really matter what we pass for RELAX_P here. */
197 gas_cgen_finish_insn (insn.insn, insn.buffer,
198 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
202 md_section_align (segment, size)
206 int align = bfd_get_section_alignment (stdoutput, segment);
208 return ((size + (1 << align) - 1) & (-1 << align));
213 md_undefined_symbol (name)
214 char * name ATTRIBUTE_UNUSED;
220 md_estimate_size_before_relax (fragP, segment)
221 fragS * fragP ATTRIBUTE_UNUSED;
222 segT segment ATTRIBUTE_UNUSED;
224 as_fatal (_("md_estimate_size_before_relax\n"));
229 /* *fragP has been relaxed to its final size, and now needs to have
230 the bytes inside it modified to conform to the new size.
232 Called after relaxation is finished.
233 fragP->fr_type == rs_machine_dependent.
234 fragP->fr_subtype is the subtype of what the address relaxed to. */
237 md_convert_frag (abfd, sec, fragP)
238 bfd * abfd ATTRIBUTE_UNUSED;
239 segT sec ATTRIBUTE_UNUSED;
240 fragS * fragP ATTRIBUTE_UNUSED;
245 /* Functions concerning relocs. */
251 as_fatal (_("md_pcrel_from\n"));
253 /* Return the address of the delay slot. */
254 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
258 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
259 Returns BFD_RELOC_NONE if no reloc type can be found.
260 *FIXP may be modified if desired. */
262 bfd_reloc_code_real_type
263 md_cgen_lookup_reloc (insn, operand, fixP)
264 const CGEN_INSN * insn ATTRIBUTE_UNUSED;
265 const CGEN_OPERAND * operand;
266 fixS * fixP ATTRIBUTE_UNUSED;
268 bfd_reloc_code_real_type result;
270 result = BFD_RELOC_NONE;
272 switch (operand->type)
274 case IP2K_OPERAND_FR:
275 case IP2K_OPERAND_ADDR16L:
276 case IP2K_OPERAND_ADDR16H:
277 case IP2K_OPERAND_LIT8:
278 /* These may have been processed at parse time. */
279 if (fixP->fx_cgen.opinfo != 0)
280 result = fixP->fx_cgen.opinfo;
281 fixP->fx_no_overflow = 1;
284 case IP2K_OPERAND_ADDR16CJP:
285 result = fixP->fx_cgen.opinfo;
286 if (result == 0 || result == BFD_RELOC_NONE)
287 result = BFD_RELOC_IP2K_ADDR16CJP;
288 fixP->fx_no_overflow = 1;
291 case IP2K_OPERAND_ADDR16P:
292 result = BFD_RELOC_IP2K_PAGE3;
293 fixP->fx_no_overflow = 1;
297 result = BFD_RELOC_NONE;
305 /* Write a value out to the object file, using the appropriate endianness. */
308 md_number_to_chars (buf, val, n)
313 number_to_chars_bigendian (buf, val, n);
316 /* Turn a string in input_line_pointer into a floating point constant of type
317 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
318 emitted is stored in *sizeP . An error message is returned, or NULL on
321 /* Equal to MAX_PRECISION in atof-ieee.c */
322 #define MAX_LITTLENUMS 6
325 md_atof (type, litP, sizeP)
331 LITTLENUM_TYPE words [MAX_LITTLENUMS];
332 LITTLENUM_TYPE *wordP;
334 char * atof_ieee PARAMS ((char *, int, LITTLENUM_TYPE *));
352 /* FIXME: Some targets allow other format chars for bigger sizes here. */
356 return _("Bad call to md_atof()");
359 t = atof_ieee (input_line_pointer, type, words);
361 input_line_pointer = t;
362 * sizeP = prec * sizeof (LITTLENUM_TYPE);
364 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
365 the ip2k endianness. */
366 for (wordP = words; prec--;)
368 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
369 litP += sizeof (LITTLENUM_TYPE);
376 /* See whether we need to force a relocation into the output file.
377 Force most of them, since the linker's bfd relocation engine
378 understands range limits better than gas' cgen fixup engine.
379 Consider the case of a fixup intermediate value being larger than
380 the instruction it will be eventually encoded within. */
383 ip2k_force_relocation (fix)
386 switch (fix->fx_r_type)
388 /* (No C++ support in ip2k. */
389 /* case BFD_RELOC_VTABLE_INHERIT: */
390 /* case BFD_RELOC_VTABLE_ENTRY: */
392 case BFD_RELOC_IP2K_FR9:
393 case BFD_RELOC_IP2K_FR_OFFSET:
394 case BFD_RELOC_IP2K_BANK:
395 case BFD_RELOC_IP2K_ADDR16CJP:
396 case BFD_RELOC_IP2K_PAGE3:
397 case BFD_RELOC_IP2K_LO8DATA:
398 case BFD_RELOC_IP2K_HI8DATA:
399 case BFD_RELOC_IP2K_EX8DATA:
400 case BFD_RELOC_IP2K_LO8INSN:
401 case BFD_RELOC_IP2K_HI8INSN:
402 case BFD_RELOC_IP2K_PC_SKIP:
403 case BFD_RELOC_IP2K_TEXT:
407 if (fix->fx_subsy && S_IS_DEFINED (fix->fx_subsy)
408 && fix->fx_addsy && S_IS_DEFINED (fix->fx_addsy)
409 && (S_GET_SEGMENT (fix->fx_addsy)->flags & SEC_CODE))
411 fix->fx_r_type = BFD_RELOC_IP2K_TEXT;
420 return S_FORCE_RELOC (fix->fx_addsy);
424 ip2k_apply_fix3 (fixP, valueP, seg)
429 if (fixP->fx_r_type == BFD_RELOC_IP2K_TEXT
433 *valueP = ((int)(*valueP)) / 2;
434 fixP->fx_r_type = BFD_RELOC_16;
436 else if (fixP->fx_r_type == BFD_RELOC_UNUSED + IP2K_OPERAND_FR)
438 /* Must be careful when we are fixing up an FR. We could be
439 fixing up an offset to (SP) or (DP) in which case we don't
440 want to step on the top 2 bits of the FR operand. The
441 gas_cgen_md_apply_fix3 doesn't know any better and overwrites
442 the entire operand. We counter this by adding the bits
444 char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
446 /* Canonical name, since used a lot. */
447 CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
448 CGEN_INSN_INT insn_value
449 = cgen_get_insn_value (cd, where,
450 CGEN_INSN_BITSIZE (fixP->fx_cgen.insn));
451 /* Preserve (DP) or (SP) specification. */
452 *valueP += (insn_value & 0x180);
455 gas_cgen_md_apply_fix3 (fixP, valueP, seg);
459 ip2k_elf_section_flags (flags, attr, type)
461 int attr ATTRIBUTE_UNUSED;
462 int type ATTRIBUTE_UNUSED;
464 /* This is used to detect when the section changes to an executable section.
465 This function is called by the elf section processing. When we note an
466 executable section specifier we set an internal flag to denote when
467 word alignment should be forced. */
468 if (flags & SEC_CODE)
469 force_code_align = 1;
475 ip2k_elf_section_rtn (int i)
479 if (force_code_align)
481 /* The s_align_ptwo function expects that we are just after a .align
482 directive and it will either try and read the align value or stop
483 if end of line so we must fake it out so it thinks we are at the
485 char *old_input_line_pointer = input_line_pointer;
486 input_line_pointer = "\n";
488 force_code_align = 0;
490 input_line_pointer = old_input_line_pointer;
495 ip2k_elf_section_text (int i)
497 char *old_input_line_pointer;
500 /* the s_align_ptwo function expects that we are just after a .align
501 directive and it will either try and read the align value or stop if
502 end of line so we must fake it out so it thinks we are at the end of
504 old_input_line_pointer = input_line_pointer;
505 input_line_pointer = "\n";
507 force_code_align = 0;
509 input_line_pointer = old_input_line_pointer;