source: trunk/binutils/gas/config/tc-ip2k.c@ 2446

Last change on this file since 2446 was 607, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.7 KB
Line 
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 "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. */
37typedef 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}
55ip2k_insn;
56
57const char comment_chars[] = ";";
58const char line_comment_chars[] = "#";
59const char line_separator_chars[] = "";
60const char EXP_CHARS[] = "eE";
61const char FLT_CHARS[] = "dD";
62
63static void ip2k_elf_section_text (int);
64static void ip2k_elf_section_rtn (int);
65
66/* The target specific pseudo-ops which we support. */
67const 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
77
78
79#define OPTION_CPU_IP2022 (OPTION_MD_BASE)
80#define OPTION_CPU_IP2022EXT (OPTION_MD_BASE+1)
81
82struct option md_longopts[] =
83{
84 { "mip2022", no_argument, NULL, OPTION_CPU_IP2022 },
85 { "mip2022ext", no_argument, NULL, OPTION_CPU_IP2022EXT },
86 { NULL, no_argument, NULL, 0 },
87};
88size_t md_longopts_size = sizeof (md_longopts);
89
90const char * md_shortopts = "";
91
92/* Flag to detect when switching to code section where insn alignment is
93 implied. */
94static int force_code_align = 0;
95
96/* Mach selected from command line. */
97int ip2k_mach = 0;
98unsigned ip2k_mach_bitmask = 0;
99
100int
101md_parse_option (c, arg)
102 int c ATTRIBUTE_UNUSED;
103 char * arg ATTRIBUTE_UNUSED;
104{
105 switch (c)
106 {
107 case OPTION_CPU_IP2022:
108 ip2k_mach = bfd_mach_ip2022;
109 ip2k_mach_bitmask = 1 << MACH_IP2022;
110 break;
111
112 case OPTION_CPU_IP2022EXT:
113 ip2k_mach = bfd_mach_ip2022ext;
114 ip2k_mach_bitmask = 1 << MACH_IP2022EXT;
115 break;
116
117 default:
118 return 0;
119 }
120
121 return 1;
122}
123
124
125void
126md_show_usage (stream)
127 FILE * stream;
128{
129 fprintf (stream, _("IP2K specific command line options:\n"));
130 fprintf (stream, _(" -mip2022 restrict to IP2022 insns \n"));
131 fprintf (stream, _(" -mip2022ext permit extended IP2022 insn\n"));
132}
133
134
135
136void
137md_begin ()
138{
139 /* Initialize the `cgen' interface. */
140
141 /* Set the machine number and endian. */
142 gas_cgen_cpu_desc = ip2k_cgen_cpu_open (CGEN_CPU_OPEN_MACHS,
143 ip2k_mach_bitmask,
144 CGEN_CPU_OPEN_ENDIAN,
145 CGEN_ENDIAN_BIG,
146 CGEN_CPU_OPEN_END);
147 ip2k_cgen_init_asm (gas_cgen_cpu_desc);
148
149 /* This is a callback from cgen to gas to parse operands. */
150 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
151
152 /* Set the machine type. */
153 bfd_default_set_arch_mach (stdoutput, bfd_arch_ip2k, ip2k_mach);
154}
155
156
157void
158md_assemble (str)
159 char * str;
160{
161 ip2k_insn insn;
162 char * errmsg;
163
164 /* Initialize GAS's cgen interface for a new instruction. */
165 gas_cgen_init_parse ();
166
167 insn.insn = ip2k_cgen_assemble_insn
168 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
169
170 if (!insn.insn)
171 {
172 as_bad ("%s", errmsg);
173 return;
174 }
175
176 /* Check for special relocation required by SKIP instructions. */
177 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SKIPA))
178 /* Unconditional skip has a 1-bit relocation of the current pc, so
179 that we emit either sb pcl.0 or snb pcl.0 depending on whether
180 the PCL (pc + 2) >> 1 is odd or even. */
181 {
182 enum cgen_parse_operand_result result_type;
183 long value;
184 const char *curpc_plus_2 = ".+2";
185 const char *err;
186
187 err = cgen_parse_address (gas_cgen_cpu_desc, & curpc_plus_2,
188 IP2K_OPERAND_ADDR16CJP,
189 BFD_RELOC_IP2K_PC_SKIP,
190 & result_type, & value);
191 if (err)
192 {
193 as_bad ("%s", err);
194 return;
195 }
196 }
197
198 /* Doesn't really matter what we pass for RELAX_P here. */
199 gas_cgen_finish_insn (insn.insn, insn.buffer,
200 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
201}
202
203valueT
204md_section_align (segment, size)
205 segT segment;
206 valueT size;
207{
208 int align = bfd_get_section_alignment (stdoutput, segment);
209
210 return ((size + (1 << align) - 1) & (-1 << align));
211}
212
213
214symbolS *
215md_undefined_symbol (name)
216 char * name ATTRIBUTE_UNUSED;
217{
218 return 0;
219}
220
221
222int
223md_estimate_size_before_relax (fragP, segment)
224 fragS * fragP ATTRIBUTE_UNUSED;
225 segT segment ATTRIBUTE_UNUSED;
226{
227 as_fatal (_("md_estimate_size_before_relax\n"));
228 return 1;
229}
230
231
232/* *fragP has been relaxed to its final size, and now needs to have
233 the bytes inside it modified to conform to the new size.
234
235 Called after relaxation is finished.
236 fragP->fr_type == rs_machine_dependent.
237 fragP->fr_subtype is the subtype of what the address relaxed to. */
238
239void
240md_convert_frag (abfd, sec, fragP)
241 bfd * abfd ATTRIBUTE_UNUSED;
242 segT sec ATTRIBUTE_UNUSED;
243 fragS * fragP ATTRIBUTE_UNUSED;
244{
245}
246
247
248
249/* Functions concerning relocs. */
250
251long
252md_pcrel_from (fixP)
253 fixS *fixP;
254{
255 as_fatal (_("md_pcrel_from\n"));
256
257 /* Return the address of the delay slot. */
258 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
259}
260
261
262/* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
263 Returns BFD_RELOC_NONE if no reloc type can be found.
264 *FIXP may be modified if desired. */
265
266bfd_reloc_code_real_type
267md_cgen_lookup_reloc (insn, operand, fixP)
268 const CGEN_INSN * insn ATTRIBUTE_UNUSED;
269 const CGEN_OPERAND * operand;
270 fixS * fixP ATTRIBUTE_UNUSED;
271{
272 bfd_reloc_code_real_type result;
273
274 result = BFD_RELOC_NONE;
275
276 switch (operand->type)
277 {
278 case IP2K_OPERAND_FR:
279 case IP2K_OPERAND_ADDR16L:
280 case IP2K_OPERAND_ADDR16H:
281 case IP2K_OPERAND_LIT8:
282 /* These may have been processed at parse time. */
283 if (fixP->fx_cgen.opinfo != 0)
284 result = fixP->fx_cgen.opinfo;
285 fixP->fx_no_overflow = 1;
286 break;
287
288 case IP2K_OPERAND_ADDR16CJP:
289 result = fixP->fx_cgen.opinfo;
290 if (result == 0 || result == BFD_RELOC_NONE)
291 result = BFD_RELOC_IP2K_ADDR16CJP;
292 fixP->fx_no_overflow = 1;
293 break;
294
295 case IP2K_OPERAND_ADDR16P:
296 result = BFD_RELOC_IP2K_PAGE3;
297 fixP->fx_no_overflow = 1;
298 break;
299
300 default:
301 result = BFD_RELOC_NONE;
302 break;
303 }
304
305 return result;
306}
307
308
309/* Write a value out to the object file, using the appropriate endianness. */
310
311void
312md_number_to_chars (buf, val, n)
313 char * buf;
314 valueT val;
315 int n;
316{
317 number_to_chars_bigendian (buf, val, n);
318}
319
320/* Turn a string in input_line_pointer into a floating point constant of type
321 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
322 emitted is stored in *sizeP . An error message is returned, or NULL on
323 OK. */
324
325/* Equal to MAX_PRECISION in atof-ieee.c */
326#define MAX_LITTLENUMS 6
327
328char *
329md_atof (type, litP, sizeP)
330 char type;
331 char * litP;
332 int * sizeP;
333{
334 int prec;
335 LITTLENUM_TYPE words [MAX_LITTLENUMS];
336 LITTLENUM_TYPE *wordP;
337 char * t;
338 char * atof_ieee PARAMS ((char *, int, LITTLENUM_TYPE *));
339
340 switch (type)
341 {
342 case 'f':
343 case 'F':
344 case 's':
345 case 'S':
346 prec = 2;
347 break;
348
349 case 'd':
350 case 'D':
351 case 'r':
352 case 'R':
353 prec = 4;
354 break;
355
356 /* FIXME: Some targets allow other format chars for bigger sizes here. */
357
358 default:
359 * sizeP = 0;
360 return _("Bad call to md_atof()");
361 }
362
363 t = atof_ieee (input_line_pointer, type, words);
364 if (t)
365 input_line_pointer = t;
366 * sizeP = prec * sizeof (LITTLENUM_TYPE);
367
368 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
369 the ip2k endianness. */
370 for (wordP = words; prec--;)
371 {
372 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
373 litP += sizeof (LITTLENUM_TYPE);
374 }
375
376 return 0;
377}
378
379
380/* See whether we need to force a relocation into the output file.
381 Force most of them, since the linker's bfd relocation engine
382 understands range limits better than gas' cgen fixup engine.
383 Consider the case of a fixup intermediate value being larger than
384 the instruction it will be eventually encoded within. */
385
386int
387ip2k_force_relocation (fix)
388 fixS * fix;
389{
390 switch (fix->fx_r_type)
391 {
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:
404 return 1;
405
406 case BFD_RELOC_16:
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))
410 {
411 fix->fx_r_type = BFD_RELOC_IP2K_TEXT;
412 return 0;
413 }
414 break;
415
416 default:
417 break;
418 }
419
420 return generic_force_reloc (fix);
421}
422
423void
424ip2k_apply_fix3 (fixP, valueP, seg)
425 fixS *fixP;
426 valueT *valueP;
427 segT seg;
428{
429 if (fixP->fx_r_type == BFD_RELOC_IP2K_TEXT
430 && ! fixP->fx_addsy
431 && ! fixP->fx_subsy)
432 {
433 *valueP = ((int)(*valueP)) / 2;
434 fixP->fx_r_type = BFD_RELOC_16;
435 }
436 else if (fixP->fx_r_type == BFD_RELOC_UNUSED + IP2K_OPERAND_FR)
437 {
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
443 to the new value. */
444 char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
445
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);
453 }
454
455 gas_cgen_md_apply_fix3 (fixP, valueP, seg);
456}
457
458int
459ip2k_elf_section_flags (flags, attr, type)
460 int flags;
461 int attr ATTRIBUTE_UNUSED;
462 int type ATTRIBUTE_UNUSED;
463{
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;
470
471 return flags;
472}
473
474static void
475ip2k_elf_section_rtn (int i)
476{
477 obj_elf_section(i);
478
479 if (force_code_align)
480 {
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
484 end of the line. */
485 char *old_input_line_pointer = input_line_pointer;
486 input_line_pointer = "\n";
487 s_align_ptwo (1);
488 force_code_align = 0;
489 /* Restore. */
490 input_line_pointer = old_input_line_pointer;
491 }
492}
493
494static void
495ip2k_elf_section_text (int i)
496{
497 char *old_input_line_pointer;
498 obj_elf_text(i);
499
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
503 the line. */
504 old_input_line_pointer = input_line_pointer;
505 input_line_pointer = "\n";
506 s_align_ptwo (1);
507 force_code_align = 0;
508 /* Restore. */
509 input_line_pointer = old_input_line_pointer;
510}
Note: See TracBrowser for help on using the repository browser.