1 /* tc-openrisc.c -- Assembler for the OpenRISC family.
2    Copyright 2001, 2002, 2003 Free Software Foundation.
3    Contributed by Johan Rydberg, jrydberg@opencores.org
4 
5    This file is part of GAS, the GNU Assembler.
6 
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11 
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21 
22 #include <stdio.h>
23 #include "as.h"
24 #include "subsegs.h"
25 #include "symcat.h"
26 #include "opcodes/openrisc-desc.h"
27 #include "opcodes/openrisc-opc.h"
28 #include "cgen.h"
29 
30 /* Structure to hold all of the different components describing
31    an individual instruction.  */
32 typedef struct openrisc_insn openrisc_insn;
33 
34 struct openrisc_insn
35 {
36   const CGEN_INSN *	insn;
37   const CGEN_INSN *	orig_insn;
38   CGEN_FIELDS		fields;
39 #if CGEN_INT_INSN_P
40   CGEN_INSN_INT         buffer [1];
41 #define INSN_VALUE(buf) (*(buf))
42 #else
43   unsigned char         buffer [CGEN_MAX_INSN_SIZE];
44 #define INSN_VALUE(buf) (buf)
45 #endif
46   char *		addr;
47   fragS *		frag;
48   int                   num_fixups;
49   fixS *                fixups [GAS_CGEN_MAX_FIXUPS];
50   int                   indices [MAX_OPERAND_INSTANCES];
51 };
52 
53 
54 const char comment_chars[]        = "#";
55 const char line_comment_chars[]   = "#";
56 const char line_separator_chars[] = ";";
57 const char EXP_CHARS[]            = "eE";
58 const char FLT_CHARS[]            = "dD";
59 
60 
61 #define OPENRISC_SHORTOPTS "m:"
62 const char * md_shortopts = OPENRISC_SHORTOPTS;
63 
64 struct option md_longopts[] =
65 {
66   {NULL, no_argument, NULL, 0}
67 };
68 size_t md_longopts_size = sizeof (md_longopts);
69 
70 unsigned long openrisc_machine = 0; /* default */
71 
72 int
73 md_parse_option (c, arg)
74      int    c ATTRIBUTE_UNUSED;
75      char * arg ATTRIBUTE_UNUSED;
76 {
77   return 0;
78 }
79 
80 void
81 md_show_usage (stream)
82   FILE * stream ATTRIBUTE_UNUSED;
83 {
84 }
85 
86 static void ignore_pseudo PARAMS ((int));
87 
88 static void
89 ignore_pseudo (val)
90      int val ATTRIBUTE_UNUSED;
91 {
92   discard_rest_of_line ();
93 }
94 
95 const char openrisc_comment_chars [] = ";#";
96 
97 /* The target specific pseudo-ops which we support.  */
98 const pseudo_typeS md_pseudo_table[] =
99 {
100   { "word",     cons,           4 },
101   { "proc",     ignore_pseudo,  0 },
102   { "endproc",  ignore_pseudo,  0 },
103   { NULL, 	NULL, 		0 }
104 };
105 
106 
107 
108 void
109 md_begin ()
110 {
111   /* Initialize the `cgen' interface.  */
112 
113   /* Set the machine number and endian.  */
114   gas_cgen_cpu_desc = openrisc_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
115                                               CGEN_CPU_OPEN_ENDIAN,
116                                               CGEN_ENDIAN_BIG,
117                                               CGEN_CPU_OPEN_END);
118   openrisc_cgen_init_asm (gas_cgen_cpu_desc);
119 
120   /* This is a callback from cgen to gas to parse operands.  */
121   cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
122 }
123 
124 void
125 md_assemble (str)
126      char * str;
127 {
128   static int last_insn_had_delay_slot = 0;
129   openrisc_insn insn;
130   char *    errmsg;
131 
132   /* Initialize GAS's cgen interface for a new instruction.  */
133   gas_cgen_init_parse ();
134 
135   insn.insn = openrisc_cgen_assemble_insn
136     (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
137 
138   if (!insn.insn)
139     {
140       as_bad (errmsg);
141       return;
142     }
143 
144   /* Doesn't really matter what we pass for RELAX_P here.  */
145   gas_cgen_finish_insn (insn.insn, insn.buffer,
146 			CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
147 
148 #if 0 /* Currently disabled  */
149   /* Warn about invalid insns in delay slots.  */
150   if (last_insn_had_delay_slot
151       && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_NOT_IN_DELAY_SLOT))
152     as_warn (_("Instruction %s not allowed in a delay slot."),
153 	     CGEN_INSN_NAME (insn.insn));
154 #endif
155 
156   last_insn_had_delay_slot
157     = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
158 }
159 
160 
161 /* The syntax in the manual says constants begin with '#'.
162    We just ignore it.  */
163 
164 void
165 md_operand (expressionP)
166      expressionS * expressionP;
167 {
168   if (* input_line_pointer == '#')
169     {
170       input_line_pointer ++;
171       expression (expressionP);
172     }
173 }
174 
175 valueT
176 md_section_align (segment, size)
177      segT   segment;
178      valueT size;
179 {
180   int align = bfd_get_section_alignment (stdoutput, segment);
181   return ((size + (1 << align) - 1) & (-1 << align));
182 }
183 
184 symbolS *
185 md_undefined_symbol (name)
186      char * name ATTRIBUTE_UNUSED;
187 {
188   return 0;
189 }
190 
191 
192 /* Interface to relax_segment.  */
193 
194 /* FIXME: Look through this.  */
195 
196 const relax_typeS md_relax_table[] =
197 {
198 /* The fields are:
199    1) most positive reach of this state,
200    2) most negative reach of this state,
201    3) how many bytes this mode will add to the size of the current frag
202    4) which index into the table to try if we can't fit into this one.  */
203 
204   /* The first entry must be unused because an `rlx_more' value of zero ends
205      each list.  */
206   {1, 1, 0, 0},
207 
208   /* The displacement used by GAS is from the end of the 2 byte insn,
209      so we subtract 2 from the following.  */
210   /* 16 bit insn, 8 bit disp -> 10 bit range.
211      This doesn't handle a branch in the right slot at the border:
212      the "& -4" isn't taken into account.  It's not important enough to
213      complicate things over it, so we subtract an extra 2 (or + 2 in -ve
214      case).  */
215   {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
216   /* 32 bit insn, 24 bit disp -> 26 bit range.  */
217   {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
218   /* Same thing, but with leading nop for alignment.  */
219   {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
220 };
221 
222 long
223 openrisc_relax_frag (segment, fragP, stretch)
224      segT    segment;
225      fragS * fragP;
226      long    stretch;
227 {
228   /* Address of branch insn.  */
229   long address = fragP->fr_address + fragP->fr_fix - 2;
230   long growth = 0;
231 
232   /* Keep 32 bit insns aligned on 32 bit boundaries.  */
233   if (fragP->fr_subtype == 2)
234     {
235       if ((address & 3) != 0)
236 	{
237 	  fragP->fr_subtype = 3;
238 	  growth = 2;
239 	}
240     }
241   else if (fragP->fr_subtype == 3)
242     {
243       if ((address & 3) == 0)
244 	{
245 	  fragP->fr_subtype = 2;
246 	  growth = -2;
247 	}
248     }
249   else
250     {
251       growth = relax_frag (segment, fragP, stretch);
252 
253       /* Long jump on odd halfword boundary?  */
254       if (fragP->fr_subtype == 2 && (address & 3) != 0)
255 	{
256 	  fragP->fr_subtype = 3;
257 	  growth += 2;
258 	}
259     }
260 
261   return growth;
262 }
263 
264 
265 /* Return an initial guess of the length by which a fragment must grow to
266    hold a branch to reach its destination.
267    Also updates fr_type/fr_subtype as necessary.
268 
269    Called just before doing relaxation.
270    Any symbol that is now undefined will not become defined.
271    The guess for fr_var is ACTUALLY the growth beyond fr_fix.
272    Whatever we do to grow fr_fix or fr_var contributes to our returned value.
273    Although it may not be explicit in the frag, pretend fr_var starts with a
274    0 value.  */
275 
276 int
277 md_estimate_size_before_relax (fragP, segment)
278      fragS * fragP;
279      segT    segment;
280 {
281   /* The only thing we have to handle here are symbols outside of the
282      current segment.  They may be undefined or in a different segment in
283      which case linker scripts may place them anywhere.
284      However, we can't finish the fragment here and emit the reloc as insn
285      alignment requirements may move the insn about.  */
286 
287   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
288     {
289       /* The symbol is undefined in this segment.
290 	 Change the relaxation subtype to the max allowable and leave
291 	 all further handling to md_convert_frag.  */
292       fragP->fr_subtype = 2;
293 
294       {
295 	const CGEN_INSN * insn;
296 	int               i;
297 
298 	/* Update the recorded insn.
299 	   Fortunately we don't have to look very far.
300 	   FIXME: Change this to record in the instruction the next higher
301 	   relaxable insn to use.  */
302 	for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
303 	  {
304 	    if ((strcmp (CGEN_INSN_MNEMONIC (insn),
305 			 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
306 		 == 0)
307 		&& CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
308 	      break;
309 	  }
310 	if (i == 4)
311 	  abort ();
312 
313 	fragP->fr_cgen.insn = insn;
314 	return 2;
315       }
316     }
317 
318   return md_relax_table[fragP->fr_subtype].rlx_length;
319 }
320 
321 /* *fragP has been relaxed to its final size, and now needs to have
322    the bytes inside it modified to conform to the new size.
323 
324    Called after relaxation is finished.
325    fragP->fr_type == rs_machine_dependent.
326    fragP->fr_subtype is the subtype of what the address relaxed to.  */
327 
328 void
329 md_convert_frag (abfd, sec, fragP)
330   bfd *   abfd ATTRIBUTE_UNUSED;
331   segT    sec  ATTRIBUTE_UNUSED;
332   fragS * fragP ATTRIBUTE_UNUSED;
333 {
334   /* FIXME */
335 }
336 
337 
338 /* Functions concerning relocs.  */
339 
340 /* The location from which a PC relative jump should be calculated,
341    given a PC relative reloc.  */
342 
343 long
344 md_pcrel_from_section (fixP, sec)
345      fixS * fixP;
346      segT   sec;
347 {
348   if (fixP->fx_addsy != (symbolS *) NULL
349       && (! S_IS_DEFINED (fixP->fx_addsy)
350 	  || S_GET_SEGMENT (fixP->fx_addsy) != sec))
351     {
352       /* The symbol is undefined (or is defined but not in this section).
353 	 Let the linker figure it out.  */
354       return 0;
355     }
356 
357   return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
358 }
359 
360 
361 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
362    Returns BFD_RELOC_NONE if no reloc type can be found.
363    *FIXP may be modified if desired.  */
364 
365 bfd_reloc_code_real_type
366 md_cgen_lookup_reloc (insn, operand, fixP)
367      const CGEN_INSN *    insn ATTRIBUTE_UNUSED;
368      const CGEN_OPERAND * operand;
369      fixS *               fixP;
370 {
371   bfd_reloc_code_real_type type;
372 
373   switch (operand->type)
374     {
375     case OPENRISC_OPERAND_ABS_26:
376       fixP->fx_pcrel = 0;
377       type = BFD_RELOC_OPENRISC_ABS_26;
378       goto emit;
379     case OPENRISC_OPERAND_DISP_26:
380       fixP->fx_pcrel = 1;
381       type = BFD_RELOC_OPENRISC_REL_26;
382       goto emit;
383 
384     case OPENRISC_OPERAND_HI16:
385       type = BFD_RELOC_HI16;
386       goto emit;
387 
388     case OPENRISC_OPERAND_LO16:
389       type = BFD_RELOC_LO16;
390       goto emit;
391 
392     emit:
393       return type;
394 
395     default : /* avoid -Wall warning */
396       break;
397     }
398 
399   return BFD_RELOC_NONE;
400 }
401 
402 /* Write a value out to the object file, using the appropriate endianness.  */
403 
404 void
405 md_number_to_chars (buf, val, n)
406      char * buf;
407      valueT val;
408      int    n;
409 {
410   number_to_chars_bigendian (buf, val, n);
411 }
412 
413 /* Turn a string in input_line_pointer into a floating point constant of type
414    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
415    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
416 */
417 
418 /* Equal to MAX_PRECISION in atof-ieee.c */
419 #define MAX_LITTLENUMS 6
420 
421 char *
422 md_atof (type, litP, sizeP)
423      char   type;
424      char * litP;
425      int *  sizeP;
426 {
427   int              i;
428   int              prec;
429   LITTLENUM_TYPE   words [MAX_LITTLENUMS];
430   char *           t;
431 
432   switch (type)
433     {
434     case 'f':
435     case 'F':
436     case 's':
437     case 'S':
438       prec = 2;
439       break;
440 
441     case 'd':
442     case 'D':
443     case 'r':
444     case 'R':
445       prec = 4;
446       break;
447 
448    /* FIXME: Some targets allow other format chars for bigger sizes here.  */
449 
450     default:
451       * sizeP = 0;
452       return _("Bad call to md_atof()");
453     }
454 
455   t = atof_ieee (input_line_pointer, type, words);
456   if (t)
457     input_line_pointer = t;
458   * sizeP = prec * sizeof (LITTLENUM_TYPE);
459 
460   for (i = 0; i < prec; i++)
461     {
462       md_number_to_chars (litP, (valueT) words[i],
463 			  sizeof (LITTLENUM_TYPE));
464       litP += sizeof (LITTLENUM_TYPE);
465     }
466 
467   return 0;
468 }
469 
470 bfd_boolean
471 openrisc_fix_adjustable (fixP)
472    fixS * fixP;
473 {
474   /* We need the symbol name for the VTABLE entries */
475   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
476       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
477     return 0;
478 
479   return 1;
480 }
481 
482 
483 
484