1d57d60e2697fd3ead3968f4d3350e2aa61eb7c4
[deliverable/binutils-gdb.git] / gas / config / tc-xstormy16.c
1 /* tc-xstormy16.c -- Assembler for the Sanyo XSTORMY16.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
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 3, 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, 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21 #include "as.h"
22 #include "subsegs.h"
23 #include "symcat.h"
24 #include "opcodes/xstormy16-desc.h"
25 #include "opcodes/xstormy16-opc.h"
26 #include "cgen.h"
27
28 /* Structure to hold all of the different components describing
29 an individual instruction. */
30 typedef struct
31 {
32 const CGEN_INSN * insn;
33 const CGEN_INSN * orig_insn;
34 CGEN_FIELDS fields;
35 #if CGEN_INT_INSN_P
36 CGEN_INSN_INT buffer [1];
37 #define INSN_VALUE(buf) (*(buf))
38 #else
39 unsigned char buffer [CGEN_MAX_INSN_SIZE];
40 #define INSN_VALUE(buf) (buf)
41 #endif
42 char * addr;
43 fragS * frag;
44 int num_fixups;
45 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
46 int indices [MAX_OPERAND_INSTANCES];
47 }
48 xstormy16_insn;
49
50 const char comment_chars[] = ";";
51 const char line_comment_chars[] = "#";
52 const char line_separator_chars[] = "|";
53 const char EXP_CHARS[] = "eE";
54 const char FLT_CHARS[] = "dD";
55
56 #define O_fptr_symbol (O_max + 1)
57 \f
58 #define XSTORMY16_SHORTOPTS ""
59 const char * md_shortopts = XSTORMY16_SHORTOPTS;
60
61 struct option md_longopts[] =
62 {
63 {NULL, no_argument, NULL, 0}
64 };
65 size_t md_longopts_size = sizeof (md_longopts);
66
67 int
68 md_parse_option (int c ATTRIBUTE_UNUSED,
69 char * arg ATTRIBUTE_UNUSED)
70 {
71 return 0;
72 }
73
74 void
75 md_show_usage (FILE * stream)
76 {
77 fprintf (stream, _(" XSTORMY16 specific command line options:\n"));
78 }
79
80 /* The target specific pseudo-ops which we support. */
81 const pseudo_typeS md_pseudo_table[] =
82 {
83 { "word", cons, 4 },
84 { NULL, NULL, 0 }
85 };
86
87 \f
88 void
89 md_begin (void)
90 {
91 /* Initialize the `cgen' interface. */
92
93 /* Set the machine number and endian. */
94 gas_cgen_cpu_desc = xstormy16_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
95 CGEN_CPU_OPEN_ENDIAN,
96 CGEN_ENDIAN_LITTLE,
97 CGEN_CPU_OPEN_END);
98 xstormy16_cgen_init_asm (gas_cgen_cpu_desc);
99
100 /* This is a callback from cgen to gas to parse operands. */
101 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
102 }
103
104 static bfd_boolean skipping_fptr = FALSE;
105
106 void
107 md_assemble (char * str)
108 {
109 xstormy16_insn insn;
110 char * errmsg;
111
112 /* Make sure that if we had an erroneous input line which triggered
113 the skipping_fptr boolean that it does not affect following lines. */
114 skipping_fptr = FALSE;
115
116 /* Initialize GAS's cgen interface for a new instruction. */
117 gas_cgen_init_parse ();
118
119 insn.insn = xstormy16_cgen_assemble_insn
120 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
121
122 if (!insn.insn)
123 {
124 as_bad ("%s", errmsg);
125 return;
126 }
127
128 /* Doesn't really matter what we pass for RELAX_P here. */
129 gas_cgen_finish_insn (insn.insn, insn.buffer,
130 CGEN_FIELDS_BITSIZE (& insn.fields), 0, NULL);
131 }
132
133 void
134 md_operand (expressionS * e)
135 {
136 if (*input_line_pointer != '@')
137 return;
138
139 if (strncmp (input_line_pointer + 1, "fptr", 4) == 0)
140 {
141 input_line_pointer += 5;
142 SKIP_WHITESPACE ();
143 if (*input_line_pointer != '(')
144 {
145 as_bad (_("Expected '('"));
146 goto err;
147 }
148 input_line_pointer++;
149
150 expression (e);
151
152 if (*input_line_pointer != ')')
153 {
154 as_bad (_("Missing ')'"));
155 goto err;
156 }
157 input_line_pointer++;
158 SKIP_WHITESPACE ();
159
160 if (e->X_op != O_symbol)
161 as_bad (_("Not a symbolic expression"));
162 else if (* input_line_pointer == '-')
163 /* We are computing the difference of two function pointers
164 like this:
165
166 .hword @fptr (foo) - @fptr (bar)
167
168 In this situation we do not want to generate O_fptr_symbol
169 operands because the result is an absolute value, not a
170 function pointer.
171
172 We need to make the check here, rather than when the fixup
173 is generated as the function names (foo & bar in the above
174 example) might be local symbols and we want the expression
175 to be evaluated now. This kind of thing can happen when
176 gcc is generating computed gotos. */
177 skipping_fptr = TRUE;
178 else if (skipping_fptr)
179 skipping_fptr = FALSE;
180 else
181 e->X_op = O_fptr_symbol;
182 }
183
184 return;
185 err:
186 ignore_rest_of_line ();
187 }
188
189 /* Called while parsing data to create a fixup.
190 Create BFD_RELOC_XSTORMY16_FPTR16 relocations. */
191
192 void
193 xstormy16_cons_fix_new (fragS *f,
194 int where,
195 int nbytes,
196 expressionS *exp)
197 {
198 bfd_reloc_code_real_type code;
199
200 if (exp->X_op == O_fptr_symbol)
201 {
202 switch (nbytes)
203 {
204 case 4:
205 /* This can happen when gcc is generating debug output.
206 For example it can create a stab with the address of
207 a function:
208
209 .stabs "foo:F(0,21)",36,0,0,@fptr(foo)
210
211 Since this does not involve switching code pages, we
212 just allow the reloc to be generated without any
213 @fptr behaviour. */
214 exp->X_op = O_symbol;
215 code = BFD_RELOC_32;
216 break;
217
218 case 2:
219 exp->X_op = O_symbol;
220 code = BFD_RELOC_XSTORMY16_FPTR16;
221 break;
222
223 default:
224 as_bad (_("unsupported fptr fixup size %d"), nbytes);
225 return;
226 }
227 }
228 else if (nbytes == 1)
229 code = BFD_RELOC_8;
230 else if (nbytes == 2)
231 code = BFD_RELOC_16;
232 else if (nbytes == 4)
233 code = BFD_RELOC_32;
234 else
235 {
236 as_bad (_("unsupported fixup size %d"), nbytes);
237 return;
238 }
239
240 fix_new_exp (f, where, nbytes, exp, 0, code);
241 }
242
243 /* Called while parsing an instruction to create a fixup.
244 Create BFD_RELOC_XSTORMY16_FPTR16 relocations. */
245
246 fixS *
247 xstormy16_cgen_record_fixup_exp (fragS * frag,
248 int where,
249 const CGEN_INSN * insn,
250 int length,
251 const CGEN_OPERAND * operand,
252 int opinfo,
253 expressionS * exp)
254 {
255 fixS *fixP;
256 operatorT op = exp->X_op;
257
258 if (op == O_fptr_symbol)
259 exp->X_op = O_symbol;
260
261 fixP = gas_cgen_record_fixup_exp (frag, where, insn, length,
262 operand, opinfo, exp);
263
264 if (op == O_fptr_symbol)
265 {
266 if (operand->type != XSTORMY16_OPERAND_IMM16)
267 as_bad (_("unsupported fptr fixup"));
268 else
269 {
270 fixP->fx_r_type = BFD_RELOC_XSTORMY16_FPTR16;
271 fixP->fx_where += 2;
272 }
273 }
274
275 return fixP;
276 }
277
278 valueT
279 md_section_align (segT segment, valueT size)
280 {
281 int align = bfd_get_section_alignment (stdoutput, segment);
282
283 return ((size + (1 << align) - 1) & (-1 << align));
284 }
285
286 symbolS *
287 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
288 {
289 return 0;
290 }
291 \f
292 /* Return an initial guess of the length by which a fragment must grow to
293 hold a branch to reach its destination.
294 Also updates fr_type/fr_subtype as necessary.
295
296 Called just before doing relaxation.
297 Any symbol that is now undefined will not become defined.
298 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
299 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
300 Although it may not be explicit in the frag, pretend fr_var starts with a
301 0 value. */
302
303 int
304 md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED,
305 segT segment ATTRIBUTE_UNUSED)
306 {
307 /* No assembler relaxation is defined (or necessary) for this port. */
308 abort ();
309 }
310
311 /* *fragP has been relaxed to its final size, and now needs to have
312 the bytes inside it modified to conform to the new size.
313
314 Called after relaxation is finished.
315 fragP->fr_type == rs_machine_dependent.
316 fragP->fr_subtype is the subtype of what the address relaxed to. */
317
318 void
319 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
320 segT sec ATTRIBUTE_UNUSED,
321 fragS * fragP ATTRIBUTE_UNUSED)
322 {
323 /* No assembler relaxation is defined (or necessary) for this port. */
324 abort ();
325 }
326 \f
327 /* Functions concerning relocs. */
328
329 /* The location from which a PC relative jump should be calculated,
330 given a PC relative reloc. */
331
332 long
333 md_pcrel_from_section (fixS * fixP, segT sec)
334 {
335 if ((fixP->fx_addsy != (symbolS *) NULL
336 && (! S_IS_DEFINED (fixP->fx_addsy)
337 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
338 || xstormy16_force_relocation (fixP))
339 /* The symbol is undefined,
340 or it is defined but not in this section,
341 or the relocation will be relative to this symbol not the section symbol.
342 Let the linker figure it out. */
343 return 0;
344
345 return fixP->fx_frag->fr_address + fixP->fx_where;
346 }
347
348 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
349 Returns BFD_RELOC_NONE if no reloc type can be found.
350 *FIXP may be modified if desired. */
351
352 bfd_reloc_code_real_type
353 md_cgen_lookup_reloc (const CGEN_INSN * insn ATTRIBUTE_UNUSED,
354 const CGEN_OPERAND * operand,
355 fixS * fixP)
356 {
357 switch (operand->type)
358 {
359 case XSTORMY16_OPERAND_IMM2:
360 case XSTORMY16_OPERAND_IMM3:
361 case XSTORMY16_OPERAND_IMM3B:
362 case XSTORMY16_OPERAND_IMM4:
363 case XSTORMY16_OPERAND_HMEM8:
364 return BFD_RELOC_NONE;
365
366 case XSTORMY16_OPERAND_IMM12:
367 fixP->fx_where += 2;
368 return BFD_RELOC_XSTORMY16_12;
369
370 case XSTORMY16_OPERAND_IMM8:
371 case XSTORMY16_OPERAND_LMEM8:
372 return fixP->fx_pcrel ? BFD_RELOC_8_PCREL : BFD_RELOC_8;
373
374 case XSTORMY16_OPERAND_IMM16:
375 /* This might have been processed at parse time. */
376 fixP->fx_where += 2;
377 if (fixP->fx_cgen.opinfo && fixP->fx_cgen.opinfo != BFD_RELOC_NONE)
378 return fixP->fx_cgen.opinfo;
379 return fixP->fx_pcrel ? BFD_RELOC_16_PCREL : BFD_RELOC_16;
380
381 case XSTORMY16_OPERAND_ABS24:
382 return BFD_RELOC_XSTORMY16_24;
383
384 case XSTORMY16_OPERAND_REL8_4:
385 fixP->fx_addnumber -= 2;
386 case XSTORMY16_OPERAND_REL8_2:
387 fixP->fx_addnumber -= 2;
388 fixP->fx_pcrel = 1;
389 return BFD_RELOC_8_PCREL;
390
391 case XSTORMY16_OPERAND_REL12:
392 fixP->fx_where += 2;
393 /* Fall through... */
394 case XSTORMY16_OPERAND_REL12A:
395 fixP->fx_addnumber -= 2;
396 fixP->fx_pcrel = 1;
397 return BFD_RELOC_XSTORMY16_REL_12;
398
399 default : /* avoid -Wall warning */
400 abort ();
401 }
402 }
403
404 /* See whether we need to force a relocation into the output file.
405 This is used to force out switch and PC relative relocations when
406 relaxing. */
407
408 int
409 xstormy16_force_relocation (fixS * fix)
410 {
411 if (fix->fx_r_type == BFD_RELOC_XSTORMY16_FPTR16)
412 return 1;
413
414 return generic_force_reloc (fix);
415 }
416
417 /* Return true if a relocation against a symbol may be replaced with
418 a relocation against section+offset. */
419
420 bfd_boolean
421 xstormy16_fix_adjustable (fixS * fixP)
422 {
423 /* We need the symbol name for the VTABLE entries. */
424 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
425 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
426 return FALSE;
427
428 if (fixP->fx_r_type == BFD_RELOC_XSTORMY16_FPTR16)
429 return FALSE;
430
431 return TRUE;
432 }
433
434 /* This is a copy of gas_cgen_md_apply_fix, with some enhancements to
435 do various things that would not be valid for all ports. */
436
437 void
438 xstormy16_md_apply_fix (fixS * fixP,
439 valueT * valueP,
440 segT seg ATTRIBUTE_UNUSED)
441 {
442 char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
443 valueT value = *valueP;
444 /* Canonical name, since used a lot. */
445 CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
446
447 /* md_cgen_lookup_reloc() will adjust this to compensate for where
448 in the opcode the relocation happens, for pcrel relocations. We
449 have no other way of keeping track of what this offset needs to
450 be. */
451 fixP->fx_addnumber = 0;
452
453 /* This port has pc-relative relocs and DIFF_EXPR_OK defined, so
454 it must deal with turning a BFD_RELOC_{8,16,32,64} into a
455 BFD_RELOC_*_PCREL for the case of
456
457 .word something-. */
458 if (fixP->fx_pcrel)
459 switch (fixP->fx_r_type)
460 {
461 case BFD_RELOC_8:
462 fixP->fx_r_type = BFD_RELOC_8_PCREL;
463 break;
464 case BFD_RELOC_16:
465 fixP->fx_r_type = BFD_RELOC_16_PCREL;
466 break;
467 case BFD_RELOC_32:
468 fixP->fx_r_type = BFD_RELOC_32_PCREL;
469 break;
470 case BFD_RELOC_64:
471 fixP->fx_r_type = BFD_RELOC_64_PCREL;
472 break;
473 default:
474 break;
475 }
476
477 if (fixP->fx_addsy == (symbolS *) NULL)
478 fixP->fx_done = 1;
479
480 /* We don't actually support subtracting a symbol. */
481 if (fixP->fx_subsy != (symbolS *) NULL)
482 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
483
484 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
485 {
486 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
487 const CGEN_OPERAND *operand = cgen_operand_lookup_by_num (cd, opindex);
488 const char *errmsg;
489 bfd_reloc_code_real_type reloc_type;
490 CGEN_FIELDS *fields = alloca (CGEN_CPU_SIZEOF_FIELDS (cd));
491 const CGEN_INSN *insn = fixP->fx_cgen.insn;
492
493 /* If the reloc has been fully resolved finish the operand here. */
494 /* FIXME: This duplicates the capabilities of code in BFD. */
495 if (fixP->fx_done)
496 {
497 CGEN_CPU_SET_FIELDS_BITSIZE (cd) (fields, CGEN_INSN_BITSIZE (insn));
498 CGEN_CPU_SET_VMA_OPERAND (cd) (cd, opindex, fields, (bfd_vma) value);
499
500 #if CGEN_INT_INSN_P
501 {
502 CGEN_INSN_INT insn_value =
503 cgen_get_insn_value (cd, (unsigned char *) where,
504 CGEN_INSN_BITSIZE (insn));
505
506 /* ??? 0 is passed for `pc'. */
507 errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
508 &insn_value, (bfd_vma) 0);
509 cgen_put_insn_value (cd, (unsigned char *) where,
510 CGEN_INSN_BITSIZE (insn), insn_value);
511 }
512 #else
513 /* ??? 0 is passed for `pc'. */
514 errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
515 (unsigned char *) where,
516 (bfd_vma) 0);
517 #endif
518 if (errmsg)
519 as_bad_where (fixP->fx_file, fixP->fx_line, "%s", errmsg);
520 }
521
522 if (fixP->fx_done)
523 return;
524
525 /* The operand isn't fully resolved. Determine a BFD reloc value
526 based on the operand information and leave it to
527 bfd_install_relocation. Note that this doesn't work when
528 !partial_inplace. */
529
530 reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
531 if (reloc_type != BFD_RELOC_NONE)
532 fixP->fx_r_type = reloc_type;
533 else
534 {
535 as_bad_where (fixP->fx_file, fixP->fx_line,
536 _("unresolved expression that must be resolved"));
537 fixP->fx_done = 1;
538 return;
539 }
540 }
541 else if (fixP->fx_done)
542 {
543 /* We're finished with this fixup. Install it because
544 bfd_install_relocation won't be called to do it. */
545 switch (fixP->fx_r_type)
546 {
547 case BFD_RELOC_8:
548 md_number_to_chars (where, value, 1);
549 break;
550 case BFD_RELOC_16:
551 md_number_to_chars (where, value, 2);
552 break;
553 case BFD_RELOC_32:
554 md_number_to_chars (where, value, 4);
555 break;
556 case BFD_RELOC_64:
557 md_number_to_chars (where, value, 8);
558 break;
559 default:
560 as_bad_where (fixP->fx_file, fixP->fx_line,
561 _("internal error: can't install fix for reloc type %d (`%s')"),
562 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
563 break;
564 }
565 }
566 else
567 {
568 /* bfd_install_relocation will be called to finish things up. */
569 }
570
571 /* This is a RELA port. Thus, it does not need to store a
572 value if it is going to make a reloc. What's more, when
573 assembling a line like
574
575 .byte global-0x7f00
576
577 we'll get a spurious error message if we try to stuff 0x7f00 into
578 the byte. */
579 if (! fixP->fx_done)
580 *valueP = 0;
581
582 /* Tuck `value' away for use by tc_gen_reloc.
583 See the comment describing fx_addnumber in write.h.
584 This field is misnamed (or misused :-). */
585 fixP->fx_addnumber += value;
586 }
587
588 \f
589 /* Write a value out to the object file, using the appropriate endianness. */
590
591 void
592 md_number_to_chars (char * buf, valueT val, int n)
593 {
594 number_to_chars_littleendian (buf, val, n);
595 }
596
597 char *
598 md_atof (int type, char * litP, int * sizeP)
599 {
600 return ieee_md_atof (type, litP, sizeP, FALSE);
601 }
This page took 0.055285 seconds and 4 git commands to generate.