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