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