gas reloc rewrite.
[external/binutils.git] / gas / config / tc-ip2k.c
1 /* tc-ip2k.c -- Assembler for the Scenix IP2xxx.
2    Copyright (C) 2000, 2002 Free Software Foundation.
3
4    This file is part of GAS, the GNU Assembler.
5
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)
9    any later version.
10
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.
15
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.  */
20
21 #include <stdio.h>
22 #include <ctype.h>
23
24 #include "as.h"
25 #include "dwarf2dbg.h"
26 #include "subsegs.h"     
27 #include "symcat.h"
28 #include "opcodes/ip2k-desc.h"
29 #include "opcodes/ip2k-opc.h"
30 #include "cgen.h"
31 #include "elf/common.h"
32 #include "elf/ip2k.h"
33 #include "libbfd.h"
34
35 /* Structure to hold all of the different components describing
36    an individual instruction.  */
37 typedef struct
38 {
39   const CGEN_INSN *     insn;
40   const CGEN_INSN *     orig_insn;
41   CGEN_FIELDS           fields;
42 #if CGEN_INT_INSN_P
43   CGEN_INSN_INT         buffer [1];
44 #define INSN_VALUE(buf) (*(buf))
45 #else
46   unsigned char         buffer [CGEN_MAX_INSN_SIZE];
47 #define INSN_VALUE(buf) (buf)
48 #endif
49   char *                addr;
50   fragS *               frag;
51   int                   num_fixups;
52   fixS *                fixups [GAS_CGEN_MAX_FIXUPS];
53   int                   indices [MAX_OPERAND_INSTANCES];
54 }
55 ip2k_insn;
56
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";
62
63 static void ip2k_elf_section_text (int);
64 static void ip2k_elf_section_rtn (int);
65
66 /* The target specific pseudo-ops which we support.  */
67 const pseudo_typeS md_pseudo_table[] =
68 {
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 },
73     { NULL,     NULL,                   0 }
74 };
75
76 \f
77
78 #define OPTION_CPU_IP2022    (OPTION_MD_BASE)
79 #define OPTION_CPU_IP2022EXT (OPTION_MD_BASE+1)
80
81 struct option md_longopts[] = 
82 {
83   { "mip2022",     no_argument, NULL, OPTION_CPU_IP2022 },
84   { "mip2022ext",  no_argument, NULL, OPTION_CPU_IP2022EXT },
85   { NULL,           no_argument, NULL, 0 },
86 };
87 size_t md_longopts_size = sizeof (md_longopts);
88
89 const char * md_shortopts = "";
90
91 /* Flag to detect when switching to code section where insn alignment is
92    implied.  */
93 static int force_code_align = 0;
94
95 /* Mach selected from command line.  */
96 int ip2k_mach = 0;
97 unsigned ip2k_mach_bitmask = 0;
98
99 int
100 md_parse_option (c, arg)
101     int c ATTRIBUTE_UNUSED;
102     char * arg ATTRIBUTE_UNUSED;
103 {
104   switch (c)
105     {
106     case OPTION_CPU_IP2022:
107       ip2k_mach = bfd_mach_ip2022;
108       ip2k_mach_bitmask = 1 << MACH_IP2022;
109       break;
110
111     case OPTION_CPU_IP2022EXT:
112       ip2k_mach = bfd_mach_ip2022ext;
113       ip2k_mach_bitmask = 1 << MACH_IP2022EXT;
114       break;
115
116     default:
117       return 0;
118     }
119
120   return 1;
121 }
122
123
124 void
125 md_show_usage (stream)
126     FILE * stream;
127 {
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"));
131 }
132
133 \f
134 void
135 md_begin ()
136 {
137   /* Initialize the `cgen' interface.  */
138   
139   /* Set the machine number and endian.  */
140   gas_cgen_cpu_desc = ip2k_cgen_cpu_open (CGEN_CPU_OPEN_MACHS,
141                                           ip2k_mach_bitmask,
142                                           CGEN_CPU_OPEN_ENDIAN,
143                                           CGEN_ENDIAN_BIG,
144                                           CGEN_CPU_OPEN_END);
145   ip2k_cgen_init_asm (gas_cgen_cpu_desc);
146
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);
149
150   /* Set the machine type.  */
151   bfd_default_set_arch_mach (stdoutput, bfd_arch_ip2k, ip2k_mach);
152 }
153
154
155 void
156 md_assemble (str)
157      char * str;
158 {
159   ip2k_insn insn;
160   char * errmsg;
161
162   /* Initialize GAS's cgen interface for a new instruction.  */
163   gas_cgen_init_parse ();
164
165   insn.insn = ip2k_cgen_assemble_insn
166       (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
167
168   if (!insn.insn)
169     {
170       as_bad ("%s", errmsg);
171       return;
172     }
173
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.  */
179     {
180       enum cgen_parse_operand_result result_type;
181       long value;
182       const char *curpc_plus_2 = ".+2";
183
184       errmsg = cgen_parse_address (gas_cgen_cpu_desc, & curpc_plus_2,
185                                    IP2K_OPERAND_ADDR16CJP,
186                                    BFD_RELOC_IP2K_PC_SKIP,
187                                    & result_type, & value);
188       if (errmsg)
189         {
190           as_bad ("%s", errmsg);
191           return;
192         }
193     }
194
195   /* Doesn't really matter what we pass for RELAX_P here.  */
196   gas_cgen_finish_insn (insn.insn, insn.buffer,
197                         CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
198 }
199
200 valueT
201 md_section_align (segment, size)
202      segT   segment;
203      valueT size;
204 {
205   int align = bfd_get_section_alignment (stdoutput, segment);
206
207   return ((size + (1 << align) - 1) & (-1 << align));
208 }
209
210
211 symbolS *
212 md_undefined_symbol (name)
213     char * name ATTRIBUTE_UNUSED;
214 {
215     return 0;
216 }
217 \f
218 int
219 md_estimate_size_before_relax (fragP, segment)
220      fragS * fragP ATTRIBUTE_UNUSED;
221      segT    segment ATTRIBUTE_UNUSED;
222 {
223   as_fatal (_("md_estimate_size_before_relax\n"));
224   return 1;
225
226
227
228 /* *fragP has been relaxed to its final size, and now needs to have
229    the bytes inside it modified to conform to the new size.
230
231    Called after relaxation is finished.
232    fragP->fr_type == rs_machine_dependent.
233    fragP->fr_subtype is the subtype of what the address relaxed to.  */
234
235 void
236 md_convert_frag (abfd, sec, fragP)
237     bfd   * abfd  ATTRIBUTE_UNUSED;
238     segT    sec   ATTRIBUTE_UNUSED;
239     fragS * fragP ATTRIBUTE_UNUSED;
240 {
241 }
242
243 \f
244 /* Functions concerning relocs.  */
245
246 long
247 md_pcrel_from (fixP)
248      fixS *fixP;
249 {
250   as_fatal (_("md_pcrel_from\n"));
251
252   /* Return the address of the delay slot. */
253   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
254 }
255
256
257 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
258    Returns BFD_RELOC_NONE if no reloc type can be found.
259    *FIXP may be modified if desired.  */
260
261 bfd_reloc_code_real_type
262 md_cgen_lookup_reloc (insn, operand, fixP)
263      const CGEN_INSN *    insn     ATTRIBUTE_UNUSED;
264      const CGEN_OPERAND * operand;
265      fixS *               fixP     ATTRIBUTE_UNUSED;
266 {
267   bfd_reloc_code_real_type result;
268
269   result = BFD_RELOC_NONE;
270
271   switch (operand->type)
272     {
273     case IP2K_OPERAND_FR:
274     case IP2K_OPERAND_ADDR16L:
275     case IP2K_OPERAND_ADDR16H:
276     case IP2K_OPERAND_LIT8:
277       /* These may have been processed at parse time.  */
278       if (fixP->fx_cgen.opinfo != 0)
279         result = fixP->fx_cgen.opinfo;
280       fixP->fx_no_overflow = 1;
281       break;
282
283     case IP2K_OPERAND_ADDR16CJP:
284       result = fixP->fx_cgen.opinfo;
285       if (result == 0 || result == BFD_RELOC_NONE)
286         result = BFD_RELOC_IP2K_ADDR16CJP;
287       fixP->fx_no_overflow = 1;
288       break;
289
290     case IP2K_OPERAND_ADDR16P:
291       result = BFD_RELOC_IP2K_PAGE3;
292       fixP->fx_no_overflow = 1;
293       break;
294
295     default:
296       result = BFD_RELOC_NONE;
297       break;
298     }
299
300   return result;
301 }
302
303
304 /* Write a value out to the object file, using the appropriate endianness.  */
305
306 void
307 md_number_to_chars (buf, val, n)
308      char * buf;
309      valueT val;
310      int    n;
311 {
312   number_to_chars_bigendian (buf, val, n);
313 }
314
315 /* Turn a string in input_line_pointer into a floating point constant of type
316    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
317    emitted is stored in *sizeP .  An error message is returned, or NULL on
318    OK.  */
319
320 /* Equal to MAX_PRECISION in atof-ieee.c  */
321 #define MAX_LITTLENUMS 6
322
323 char *
324 md_atof (type, litP, sizeP)
325      char   type;
326      char * litP;
327      int *  sizeP;
328 {
329   int              prec;
330   LITTLENUM_TYPE   words [MAX_LITTLENUMS];
331   LITTLENUM_TYPE  *wordP;
332   char *           t;
333   char *           atof_ieee PARAMS ((char *, int, LITTLENUM_TYPE *));
334
335   switch (type)
336     {
337     case 'f':
338     case 'F':
339     case 's':
340     case 'S':
341       prec = 2;
342       break;
343
344     case 'd':
345     case 'D':
346     case 'r':
347     case 'R':
348       prec = 4;
349       break;
350
351    /* FIXME: Some targets allow other format chars for bigger sizes here.  */
352
353     default:
354       * sizeP = 0;
355       return _("Bad call to md_atof()");
356     }
357
358   t = atof_ieee (input_line_pointer, type, words);
359   if (t)
360     input_line_pointer = t;
361   * sizeP = prec * sizeof (LITTLENUM_TYPE);
362
363   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
364      the ip2k endianness.  */
365   for (wordP = words; prec--;)
366     {
367       md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
368       litP += sizeof (LITTLENUM_TYPE);
369     }
370      
371   return 0;
372 }
373
374
375 /* See whether we need to force a relocation into the output file.
376    Force most of them, since the linker's bfd relocation engine
377    understands range limits better than gas' cgen fixup engine.
378    Consider the case of a fixup intermediate value being larger than
379    the instruction it will be eventually encoded within.  */
380
381 int
382 ip2k_force_relocation (fix)
383      fixS * fix;
384 {
385   switch (fix->fx_r_type)
386     {
387       /* (No C++ support in ip2k.  */
388       /* case BFD_RELOC_VTABLE_INHERIT: */
389       /* case BFD_RELOC_VTABLE_ENTRY: */
390
391     case BFD_RELOC_IP2K_FR9:
392     case BFD_RELOC_IP2K_FR_OFFSET:
393     case BFD_RELOC_IP2K_BANK:
394     case BFD_RELOC_IP2K_ADDR16CJP:
395     case BFD_RELOC_IP2K_PAGE3:
396     case BFD_RELOC_IP2K_LO8DATA:
397     case BFD_RELOC_IP2K_HI8DATA:
398     case BFD_RELOC_IP2K_EX8DATA:
399     case BFD_RELOC_IP2K_LO8INSN:
400     case BFD_RELOC_IP2K_HI8INSN:
401     case BFD_RELOC_IP2K_PC_SKIP:
402     case BFD_RELOC_IP2K_TEXT:
403       return 1;
404
405     case BFD_RELOC_16:
406       if (fix->fx_subsy && S_IS_DEFINED (fix->fx_subsy)
407           && fix->fx_addsy && S_IS_DEFINED (fix->fx_addsy)
408           && (S_GET_SEGMENT (fix->fx_addsy)->flags & SEC_CODE))
409         {
410           fix->fx_r_type = BFD_RELOC_IP2K_TEXT;
411           return 0;
412         }
413       break;
414
415     default:
416       break;
417     }
418
419   return S_FORCE_RELOC (fix->fx_addsy);
420 }
421
422 void
423 ip2k_apply_fix3 (fixP, valueP, seg)
424      fixS *fixP;
425      valueT *valueP;
426      segT seg;
427 {
428   if (fixP->fx_r_type == BFD_RELOC_IP2K_TEXT
429       && ! fixP->fx_addsy
430       && ! fixP->fx_subsy)
431     {
432       *valueP = ((int)(*valueP)) / 2;
433       fixP->fx_r_type = BFD_RELOC_16;
434     }
435   else if (fixP->fx_r_type == BFD_RELOC_UNUSED + IP2K_OPERAND_FR)
436     {
437       /* Must be careful when we are fixing up an FR.  We could be
438          fixing up an offset to (SP) or (DP) in which case we don't
439          want to step on the top 2 bits of the FR operand.  The
440          gas_cgen_md_apply_fix3 doesn't know any better and overwrites
441          the entire operand.  We counter this by adding the bits
442          to the new value.  */
443       char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
444
445       /* Canonical name, since used a lot.  */
446       CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
447       CGEN_INSN_INT insn_value
448         = cgen_get_insn_value (cd, where,
449                                CGEN_INSN_BITSIZE (fixP->fx_cgen.insn));
450       /* Preserve (DP) or (SP) specification.  */
451       *valueP += (insn_value & 0x180);
452     }
453
454   gas_cgen_md_apply_fix3 (fixP, valueP, seg);
455 }
456
457 int
458 ip2k_elf_section_flags (flags, attr, type)
459      int flags;
460      int attr ATTRIBUTE_UNUSED;
461      int type ATTRIBUTE_UNUSED;
462 {
463   /* This is used to detect when the section changes to an executable section.
464      This function is called by the elf section processing.  When we note an
465      executable section specifier we set an internal flag to denote when
466      word alignment should be forced.  */
467   if (flags & SEC_CODE)
468     force_code_align = 1;
469  
470   return flags;
471 }
472
473 static void
474 ip2k_elf_section_rtn (int i)
475 {
476   obj_elf_section(i);
477
478   if (force_code_align)
479     {
480       /* The s_align_ptwo function expects that we are just after a .align
481          directive and it will either try and read the align value or stop
482          if end of line so we must fake it out so it thinks we are at the
483          end of the line.  */
484       char *old_input_line_pointer = input_line_pointer;
485       input_line_pointer = "\n";
486       s_align_ptwo (1);
487       force_code_align = 0;
488       /* Restore.  */
489       input_line_pointer = old_input_line_pointer;
490     }
491 }
492
493 static void
494 ip2k_elf_section_text (int i)
495 {
496   char *old_input_line_pointer;
497   obj_elf_text(i);
498
499   /* the s_align_ptwo function expects that we are just after a .align
500      directive and it will either try and read the align value or stop if
501      end of line so we must fake it out so it thinks we are at the end of
502      the line.  */
503   old_input_line_pointer = input_line_pointer;
504   input_line_pointer = "\n";
505   s_align_ptwo (1);
506   force_code_align = 0;
507   /* Restore.  */
508   input_line_pointer = old_input_line_pointer;
509 }