xref: /netbsd/external/gpl3/gdb/dist/gas/config/tc-m32c.c (revision 1424dfb3)
1*1424dfb3Schristos /* tc-m32c.c -- Assembler for the Renesas M32C.
2*1424dfb3Schristos    Copyright (C) 2005-2020 Free Software Foundation, Inc.
3*1424dfb3Schristos    Contributed by RedHat.
4*1424dfb3Schristos 
5*1424dfb3Schristos    This file is part of GAS, the GNU Assembler.
6*1424dfb3Schristos 
7*1424dfb3Schristos    GAS is free software; you can redistribute it and/or modify
8*1424dfb3Schristos    it under the terms of the GNU General Public License as published by
9*1424dfb3Schristos    the Free Software Foundation; either version 3, or (at your option)
10*1424dfb3Schristos    any later version.
11*1424dfb3Schristos 
12*1424dfb3Schristos    GAS is distributed in the hope that it will be useful,
13*1424dfb3Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*1424dfb3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*1424dfb3Schristos    GNU General Public License for more details.
16*1424dfb3Schristos 
17*1424dfb3Schristos    You should have received a copy of the GNU General Public License
18*1424dfb3Schristos    along with this program; if not, write to the Free Software
19*1424dfb3Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20*1424dfb3Schristos    MA 02110-1301, USA.  */
21*1424dfb3Schristos 
22*1424dfb3Schristos #include "as.h"
23*1424dfb3Schristos #include "subsegs.h"
24*1424dfb3Schristos #include "symcat.h"
25*1424dfb3Schristos #include "opcodes/m32c-desc.h"
26*1424dfb3Schristos #include "opcodes/m32c-opc.h"
27*1424dfb3Schristos #include "cgen.h"
28*1424dfb3Schristos #include "elf/common.h"
29*1424dfb3Schristos #include "elf/m32c.h"
30*1424dfb3Schristos #include "safe-ctype.h"
31*1424dfb3Schristos 
32*1424dfb3Schristos /* Structure to hold all of the different components
33*1424dfb3Schristos    describing an individual instruction.  */
34*1424dfb3Schristos typedef struct
35*1424dfb3Schristos {
36*1424dfb3Schristos   const CGEN_INSN *	insn;
37*1424dfb3Schristos   const CGEN_INSN *	orig_insn;
38*1424dfb3Schristos   CGEN_FIELDS		fields;
39*1424dfb3Schristos #if CGEN_INT_INSN_P
40*1424dfb3Schristos   CGEN_INSN_INT         buffer [1];
41*1424dfb3Schristos #define INSN_VALUE(buf) (*(buf))
42*1424dfb3Schristos #else
43*1424dfb3Schristos   unsigned char         buffer [CGEN_MAX_INSN_SIZE];
44*1424dfb3Schristos #define INSN_VALUE(buf) (buf)
45*1424dfb3Schristos #endif
46*1424dfb3Schristos   char *		addr;
47*1424dfb3Schristos   fragS *		frag;
48*1424dfb3Schristos   int                   num_fixups;
49*1424dfb3Schristos   fixS *                fixups [GAS_CGEN_MAX_FIXUPS];
50*1424dfb3Schristos   int                   indices [MAX_OPERAND_INSTANCES];
51*1424dfb3Schristos }
52*1424dfb3Schristos m32c_insn;
53*1424dfb3Schristos 
54*1424dfb3Schristos #define rl_for(_insn) (CGEN_ATTR_CGEN_INSN_RL_TYPE_VALUE (&((_insn).insn->base->attrs)))
55*1424dfb3Schristos #define relaxable(_insn) (CGEN_ATTR_CGEN_INSN_RELAXABLE_VALUE (&((_insn).insn->base->attrs)))
56*1424dfb3Schristos 
57*1424dfb3Schristos const char comment_chars[]        = ";";
58*1424dfb3Schristos const char line_comment_chars[]   = "#";
59*1424dfb3Schristos const char line_separator_chars[] = "|";
60*1424dfb3Schristos const char EXP_CHARS[]            = "eE";
61*1424dfb3Schristos const char FLT_CHARS[]            = "dD";
62*1424dfb3Schristos 
63*1424dfb3Schristos #define M32C_SHORTOPTS ""
64*1424dfb3Schristos const char * md_shortopts = M32C_SHORTOPTS;
65*1424dfb3Schristos 
66*1424dfb3Schristos /* assembler options */
67*1424dfb3Schristos #define OPTION_CPU_M16C	       (OPTION_MD_BASE)
68*1424dfb3Schristos #define OPTION_CPU_M32C        (OPTION_MD_BASE + 1)
69*1424dfb3Schristos #define OPTION_LINKRELAX       (OPTION_MD_BASE + 2)
70*1424dfb3Schristos #define OPTION_H_TICK_HEX      (OPTION_MD_BASE + 3)
71*1424dfb3Schristos 
72*1424dfb3Schristos struct option md_longopts[] =
73*1424dfb3Schristos {
74*1424dfb3Schristos   { "m16c",       no_argument,	      NULL, OPTION_CPU_M16C   },
75*1424dfb3Schristos   { "m32c",       no_argument,	      NULL, OPTION_CPU_M32C   },
76*1424dfb3Schristos   { "relax",      no_argument,	      NULL, OPTION_LINKRELAX   },
77*1424dfb3Schristos   { "h-tick-hex", no_argument,	      NULL, OPTION_H_TICK_HEX  },
78*1424dfb3Schristos   {NULL, no_argument, NULL, 0}
79*1424dfb3Schristos };
80*1424dfb3Schristos size_t md_longopts_size = sizeof (md_longopts);
81*1424dfb3Schristos 
82*1424dfb3Schristos /* Default machine */
83*1424dfb3Schristos 
84*1424dfb3Schristos #define DEFAULT_MACHINE bfd_mach_m16c
85*1424dfb3Schristos #define DEFAULT_FLAGS	EF_M32C_CPU_M16C
86*1424dfb3Schristos 
87*1424dfb3Schristos static unsigned long m32c_mach = bfd_mach_m16c;
88*1424dfb3Schristos static int cpu_mach = (1 << MACH_M16C);
89*1424dfb3Schristos static int m32c_relax = 0;
90*1424dfb3Schristos 
91*1424dfb3Schristos /* Flags to set in the elf header */
92*1424dfb3Schristos static flagword m32c_flags = DEFAULT_FLAGS;
93*1424dfb3Schristos 
94*1424dfb3Schristos static char default_isa = 1 << (7 - ISA_M16C);
95*1424dfb3Schristos static CGEN_BITSET m32c_isa = {1, & default_isa};
96*1424dfb3Schristos 
97*1424dfb3Schristos static void
set_isa(enum isa_attr isa_num)98*1424dfb3Schristos set_isa (enum isa_attr isa_num)
99*1424dfb3Schristos {
100*1424dfb3Schristos   cgen_bitset_set (& m32c_isa, isa_num);
101*1424dfb3Schristos }
102*1424dfb3Schristos 
103*1424dfb3Schristos static void s_bss (int);
104*1424dfb3Schristos 
105*1424dfb3Schristos int
md_parse_option(int c,const char * arg ATTRIBUTE_UNUSED)106*1424dfb3Schristos md_parse_option (int c, const char * arg ATTRIBUTE_UNUSED)
107*1424dfb3Schristos {
108*1424dfb3Schristos   switch (c)
109*1424dfb3Schristos     {
110*1424dfb3Schristos     case OPTION_CPU_M16C:
111*1424dfb3Schristos       m32c_flags = (m32c_flags & ~EF_M32C_CPU_MASK) | EF_M32C_CPU_M16C;
112*1424dfb3Schristos       m32c_mach = bfd_mach_m16c;
113*1424dfb3Schristos       cpu_mach = (1 << MACH_M16C);
114*1424dfb3Schristos       set_isa (ISA_M16C);
115*1424dfb3Schristos       break;
116*1424dfb3Schristos 
117*1424dfb3Schristos     case OPTION_CPU_M32C:
118*1424dfb3Schristos       m32c_flags = (m32c_flags & ~EF_M32C_CPU_MASK) | EF_M32C_CPU_M32C;
119*1424dfb3Schristos       m32c_mach = bfd_mach_m32c;
120*1424dfb3Schristos       cpu_mach = (1 << MACH_M32C);
121*1424dfb3Schristos       set_isa (ISA_M32C);
122*1424dfb3Schristos       break;
123*1424dfb3Schristos 
124*1424dfb3Schristos     case OPTION_LINKRELAX:
125*1424dfb3Schristos       m32c_relax = 1;
126*1424dfb3Schristos       break;
127*1424dfb3Schristos 
128*1424dfb3Schristos     case OPTION_H_TICK_HEX:
129*1424dfb3Schristos       enable_h_tick_hex = 1;
130*1424dfb3Schristos       break;
131*1424dfb3Schristos 
132*1424dfb3Schristos     default:
133*1424dfb3Schristos       return 0;
134*1424dfb3Schristos     }
135*1424dfb3Schristos   return 1;
136*1424dfb3Schristos }
137*1424dfb3Schristos 
138*1424dfb3Schristos void
md_show_usage(FILE * stream)139*1424dfb3Schristos md_show_usage (FILE * stream)
140*1424dfb3Schristos {
141*1424dfb3Schristos   fprintf (stream, _(" M32C specific command line options:\n"));
142*1424dfb3Schristos }
143*1424dfb3Schristos 
144*1424dfb3Schristos static void
s_bss(int ignore ATTRIBUTE_UNUSED)145*1424dfb3Schristos s_bss (int ignore ATTRIBUTE_UNUSED)
146*1424dfb3Schristos {
147*1424dfb3Schristos   int temp;
148*1424dfb3Schristos 
149*1424dfb3Schristos   temp = get_absolute_expression ();
150*1424dfb3Schristos   subseg_set (bss_section, (subsegT) temp);
151*1424dfb3Schristos   demand_empty_rest_of_line ();
152*1424dfb3Schristos }
153*1424dfb3Schristos 
154*1424dfb3Schristos /* The target specific pseudo-ops which we support.  */
155*1424dfb3Schristos const pseudo_typeS md_pseudo_table[] =
156*1424dfb3Schristos {
157*1424dfb3Schristos   { "bss",	s_bss, 		0},
158*1424dfb3Schristos   { "3byte",	cons,		3 },
159*1424dfb3Schristos   { "word",	cons,		4 },
160*1424dfb3Schristos   { NULL, 	NULL, 		0 }
161*1424dfb3Schristos };
162*1424dfb3Schristos 
163*1424dfb3Schristos 
164*1424dfb3Schristos void
md_begin(void)165*1424dfb3Schristos md_begin (void)
166*1424dfb3Schristos {
167*1424dfb3Schristos   /* Initialize the `cgen' interface.  */
168*1424dfb3Schristos 
169*1424dfb3Schristos   /* Set the machine number and endian.  */
170*1424dfb3Schristos   gas_cgen_cpu_desc = m32c_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, cpu_mach,
171*1424dfb3Schristos 					  CGEN_CPU_OPEN_ENDIAN,
172*1424dfb3Schristos 					  CGEN_ENDIAN_BIG,
173*1424dfb3Schristos 					  CGEN_CPU_OPEN_ISAS, & m32c_isa,
174*1424dfb3Schristos 					  CGEN_CPU_OPEN_END);
175*1424dfb3Schristos 
176*1424dfb3Schristos   m32c_cgen_init_asm (gas_cgen_cpu_desc);
177*1424dfb3Schristos 
178*1424dfb3Schristos   /* This is a callback from cgen to gas to parse operands.  */
179*1424dfb3Schristos   cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
180*1424dfb3Schristos 
181*1424dfb3Schristos   /* Set the ELF flags if desired. */
182*1424dfb3Schristos   if (m32c_flags)
183*1424dfb3Schristos     bfd_set_private_flags (stdoutput, m32c_flags);
184*1424dfb3Schristos 
185*1424dfb3Schristos   /* Set the machine type */
186*1424dfb3Schristos   bfd_default_set_arch_mach (stdoutput, bfd_arch_m32c, m32c_mach);
187*1424dfb3Schristos }
188*1424dfb3Schristos 
189*1424dfb3Schristos void
m32c_start_line_hook(void)190*1424dfb3Schristos m32c_start_line_hook (void)
191*1424dfb3Schristos {
192*1424dfb3Schristos #if 0 /* not necessary....handled in the .cpu file */
193*1424dfb3Schristos   char *s = input_line_pointer;
194*1424dfb3Schristos   char *sg;
195*1424dfb3Schristos 
196*1424dfb3Schristos   for (s = input_line_pointer ; s && s[0] != '\n'; s++)
197*1424dfb3Schristos     {
198*1424dfb3Schristos       if (s[0] == ':')
199*1424dfb3Schristos 	{
200*1424dfb3Schristos 	  /* Remove :g suffix.  Squeeze out blanks.  */
201*1424dfb3Schristos 	  if (s[1] == 'g')
202*1424dfb3Schristos 	    {
203*1424dfb3Schristos 	      for (sg = s - 1; sg && sg >= input_line_pointer; sg--)
204*1424dfb3Schristos 		{
205*1424dfb3Schristos 		  sg[2] = sg[0];
206*1424dfb3Schristos 		}
207*1424dfb3Schristos 	      sg[1] = ' ';
208*1424dfb3Schristos 	      sg[2] = ' ';
209*1424dfb3Schristos 	      input_line_pointer += 2;
210*1424dfb3Schristos 	    }
211*1424dfb3Schristos 	}
212*1424dfb3Schristos     }
213*1424dfb3Schristos #endif
214*1424dfb3Schristos }
215*1424dfb3Schristos 
216*1424dfb3Schristos /* Process [[indirect-operands]] in instruction str.  */
217*1424dfb3Schristos 
218*1424dfb3Schristos static bfd_boolean
m32c_indirect_operand(char * str)219*1424dfb3Schristos m32c_indirect_operand (char *str)
220*1424dfb3Schristos {
221*1424dfb3Schristos   char *new_str;
222*1424dfb3Schristos   char *s;
223*1424dfb3Schristos   char *ns;
224*1424dfb3Schristos   int ns_len;
225*1424dfb3Schristos   char *ns_end;
226*1424dfb3Schristos   enum indirect_type {none, relative, absolute} ;
227*1424dfb3Schristos   enum indirect_type indirection [3] = { none, none, none };
228*1424dfb3Schristos   int brace_n [3] = { 0, 0, 0 };
229*1424dfb3Schristos   int operand;
230*1424dfb3Schristos 
231*1424dfb3Schristos   s = str;
232*1424dfb3Schristos   operand = 1;
233*1424dfb3Schristos   for (s = str; *s; s++)
234*1424dfb3Schristos     {
235*1424dfb3Schristos       if (s[0] == ',')
236*1424dfb3Schristos 	operand = 2;
237*1424dfb3Schristos       /* [abs] where abs is not a0 or a1  */
238*1424dfb3Schristos       if (s[1] == '[' && ! (s[2] == 'a' && (s[3] == '0' || s[3] == '1'))
239*1424dfb3Schristos 	  && (ISBLANK (s[0]) || s[0] == ','))
240*1424dfb3Schristos 	indirection[operand] = absolute;
241*1424dfb3Schristos       if (s[0] == ']' && s[1] == ']')
242*1424dfb3Schristos 	indirection[operand] = relative;
243*1424dfb3Schristos       if (s[0] == '[' && s[1] == '[')
244*1424dfb3Schristos 	indirection[operand] = relative;
245*1424dfb3Schristos     }
246*1424dfb3Schristos 
247*1424dfb3Schristos   if (indirection[1] == none && indirection[2] == none)
248*1424dfb3Schristos     return FALSE;
249*1424dfb3Schristos 
250*1424dfb3Schristos   operand = 1;
251*1424dfb3Schristos   ns_len = strlen (str);
252*1424dfb3Schristos   new_str = XNEWVEC (char, ns_len);
253*1424dfb3Schristos   ns = new_str;
254*1424dfb3Schristos   ns_end = ns + ns_len;
255*1424dfb3Schristos 
256*1424dfb3Schristos   for (s = str; *s; s++)
257*1424dfb3Schristos     {
258*1424dfb3Schristos       if (s[0] == ',')
259*1424dfb3Schristos 	operand = 2;
260*1424dfb3Schristos 
261*1424dfb3Schristos       if (s[0] == '[' && ! brace_n[operand])
262*1424dfb3Schristos 	{
263*1424dfb3Schristos 	  brace_n[operand] += 1;
264*1424dfb3Schristos 	  /* Squeeze [[ to [ if this is an indirect operand.  */
265*1424dfb3Schristos 	  if (indirection[operand] != none)
266*1424dfb3Schristos 	    continue;
267*1424dfb3Schristos 	}
268*1424dfb3Schristos 
269*1424dfb3Schristos       else if (s[0] == '[' && brace_n[operand])
270*1424dfb3Schristos 	{
271*1424dfb3Schristos 	  brace_n[operand] += 1;
272*1424dfb3Schristos 	}
273*1424dfb3Schristos       else if (s[0] == ']' && s[1] == ']' && indirection[operand] == relative)
274*1424dfb3Schristos 	{
275*1424dfb3Schristos 	  s += 1;		/* skip one ].  */
276*1424dfb3Schristos 	  brace_n[operand] -= 2; /* allow for 2 [.  */
277*1424dfb3Schristos 	}
278*1424dfb3Schristos       else if (s[0] == ']' && indirection[operand] == absolute)
279*1424dfb3Schristos 	{
280*1424dfb3Schristos 	  brace_n[operand] -= 1;
281*1424dfb3Schristos 	  continue;		/* skip closing ].  */
282*1424dfb3Schristos 	}
283*1424dfb3Schristos       else if (s[0] == ']')
284*1424dfb3Schristos 	{
285*1424dfb3Schristos 	  brace_n[operand] -= 1;
286*1424dfb3Schristos 	}
287*1424dfb3Schristos       *ns = s[0];
288*1424dfb3Schristos       ns += 1;
289*1424dfb3Schristos       if (ns >= ns_end)
290*1424dfb3Schristos 	return FALSE;
291*1424dfb3Schristos       if (s[0] == 0)
292*1424dfb3Schristos 	break;
293*1424dfb3Schristos     }
294*1424dfb3Schristos   *ns = '\0';
295*1424dfb3Schristos   for (operand = 1; operand <= 2; operand++)
296*1424dfb3Schristos     if (brace_n[operand])
297*1424dfb3Schristos       {
298*1424dfb3Schristos 	fprintf (stderr, "Unmatched [[operand-%d]] %d\n", operand, brace_n[operand]);
299*1424dfb3Schristos       }
300*1424dfb3Schristos 
301*1424dfb3Schristos   if (indirection[1] != none && indirection[2] != none)
302*1424dfb3Schristos     md_assemble ((char *) "src-dest-indirect");
303*1424dfb3Schristos   else if (indirection[1] != none)
304*1424dfb3Schristos     md_assemble ((char *) "src-indirect");
305*1424dfb3Schristos   else if (indirection[2] != none)
306*1424dfb3Schristos     md_assemble ((char *) "dest-indirect");
307*1424dfb3Schristos 
308*1424dfb3Schristos   md_assemble (new_str);
309*1424dfb3Schristos   free (new_str);
310*1424dfb3Schristos   return TRUE;
311*1424dfb3Schristos }
312*1424dfb3Schristos 
313*1424dfb3Schristos void
md_assemble(char * str)314*1424dfb3Schristos md_assemble (char * str)
315*1424dfb3Schristos {
316*1424dfb3Schristos   static int last_insn_had_delay_slot = 0;
317*1424dfb3Schristos   m32c_insn insn;
318*1424dfb3Schristos   char *    errmsg;
319*1424dfb3Schristos   finished_insnS results;
320*1424dfb3Schristos   int rl_type;
321*1424dfb3Schristos   int insn_size;
322*1424dfb3Schristos 
323*1424dfb3Schristos   if (m32c_mach == bfd_mach_m32c && m32c_indirect_operand (str))
324*1424dfb3Schristos     return;
325*1424dfb3Schristos 
326*1424dfb3Schristos   /* Initialize GAS's cgen interface for a new instruction.  */
327*1424dfb3Schristos   gas_cgen_init_parse ();
328*1424dfb3Schristos 
329*1424dfb3Schristos   insn.insn = m32c_cgen_assemble_insn
330*1424dfb3Schristos     (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
331*1424dfb3Schristos 
332*1424dfb3Schristos   if (!insn.insn)
333*1424dfb3Schristos     {
334*1424dfb3Schristos       as_bad ("%s", errmsg);
335*1424dfb3Schristos       return;
336*1424dfb3Schristos     }
337*1424dfb3Schristos 
338*1424dfb3Schristos   results.num_fixups = 0;
339*1424dfb3Schristos   /* Doesn't really matter what we pass for RELAX_P here.  */
340*1424dfb3Schristos   gas_cgen_finish_insn (insn.insn, insn.buffer,
341*1424dfb3Schristos 			CGEN_FIELDS_BITSIZE (& insn.fields), 1, &results);
342*1424dfb3Schristos 
343*1424dfb3Schristos   last_insn_had_delay_slot
344*1424dfb3Schristos     = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
345*1424dfb3Schristos   (void) last_insn_had_delay_slot;
346*1424dfb3Schristos   insn_size = CGEN_INSN_BITSIZE(insn.insn);
347*1424dfb3Schristos 
348*1424dfb3Schristos   rl_type = rl_for (insn);
349*1424dfb3Schristos 
350*1424dfb3Schristos   /* We have to mark all the jumps, because we need to adjust them
351*1424dfb3Schristos      when we delete bytes, but we only need to mark the displacements
352*1424dfb3Schristos      if they're symbolic - if they're not, we've already picked the
353*1424dfb3Schristos      shortest opcode by now.  The linker, however, will still have to
354*1424dfb3Schristos      check any operands to see if they're the displacement type, since
355*1424dfb3Schristos      we don't know (nor record) *which* operands are relaxable.  */
356*1424dfb3Schristos   if (m32c_relax
357*1424dfb3Schristos       && rl_type != RL_TYPE_NONE
358*1424dfb3Schristos       && (rl_type == RL_TYPE_JUMP || results.num_fixups)
359*1424dfb3Schristos       && !relaxable (insn))
360*1424dfb3Schristos     {
361*1424dfb3Schristos       int reloc = 0;
362*1424dfb3Schristos       int addend = results.num_fixups + 16 * insn_size/8;
363*1424dfb3Schristos 
364*1424dfb3Schristos       switch (rl_for (insn))
365*1424dfb3Schristos 	{
366*1424dfb3Schristos 	case RL_TYPE_JUMP:  reloc = BFD_RELOC_M32C_RL_JUMP;  break;
367*1424dfb3Schristos 	case RL_TYPE_1ADDR: reloc = BFD_RELOC_M32C_RL_1ADDR; break;
368*1424dfb3Schristos 	case RL_TYPE_2ADDR: reloc = BFD_RELOC_M32C_RL_2ADDR; break;
369*1424dfb3Schristos 	}
370*1424dfb3Schristos       if (insn.insn->base->num == M32C_INSN_JMP16_S
371*1424dfb3Schristos 	  || insn.insn->base->num == M32C_INSN_JMP32_S)
372*1424dfb3Schristos 	addend = 0x10;
373*1424dfb3Schristos 
374*1424dfb3Schristos       fix_new (results.frag,
375*1424dfb3Schristos 	       results.addr - results.frag->fr_literal,
376*1424dfb3Schristos 	       0, abs_section_sym, addend, 0,
377*1424dfb3Schristos 	       reloc);
378*1424dfb3Schristos     }
379*1424dfb3Schristos }
380*1424dfb3Schristos 
381*1424dfb3Schristos /* The syntax in the manual says constants begin with '#'.
382*1424dfb3Schristos    We just ignore it.  */
383*1424dfb3Schristos 
384*1424dfb3Schristos void
md_operand(expressionS * exp)385*1424dfb3Schristos md_operand (expressionS * exp)
386*1424dfb3Schristos {
387*1424dfb3Schristos   /* In case of a syntax error, escape back to try next syntax combo. */
388*1424dfb3Schristos   if (exp->X_op == O_absent)
389*1424dfb3Schristos     gas_cgen_md_operand (exp);
390*1424dfb3Schristos }
391*1424dfb3Schristos 
392*1424dfb3Schristos valueT
md_section_align(segT segment,valueT size)393*1424dfb3Schristos md_section_align (segT segment, valueT size)
394*1424dfb3Schristos {
395*1424dfb3Schristos   int align = bfd_section_alignment (segment);
396*1424dfb3Schristos   return ((size + (1 << align) - 1) & -(1 << align));
397*1424dfb3Schristos }
398*1424dfb3Schristos 
399*1424dfb3Schristos symbolS *
md_undefined_symbol(char * name ATTRIBUTE_UNUSED)400*1424dfb3Schristos md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
401*1424dfb3Schristos {
402*1424dfb3Schristos   return 0;
403*1424dfb3Schristos }
404*1424dfb3Schristos 
405*1424dfb3Schristos const relax_typeS md_relax_table[] =
406*1424dfb3Schristos {
407*1424dfb3Schristos   /* The fields are:
408*1424dfb3Schristos      1) most positive reach of this state,
409*1424dfb3Schristos      2) most negative reach of this state,
410*1424dfb3Schristos      3) how many bytes this mode will have in the variable part of the frag
411*1424dfb3Schristos      4) which index into the table to try if we can't fit into this one.  */
412*1424dfb3Schristos 
413*1424dfb3Schristos   /* 0 */ {     0,      0, 0,  0 }, /* unused */
414*1424dfb3Schristos   /* 1 */ {     0,      0, 0,  0 }, /* marker for "don't know yet" */
415*1424dfb3Schristos 
416*1424dfb3Schristos   /* 2 */ {   127,   -128, 2,  3 }, /* jcnd16_5.b */
417*1424dfb3Schristos   /* 3 */ { 32767, -32768, 5,  4 }, /* jcnd16_5.w */
418*1424dfb3Schristos   /* 4 */ {     0,      0, 6,  0 }, /* jcnd16_5.a */
419*1424dfb3Schristos 
420*1424dfb3Schristos   /* 5 */ {   127,   -128, 2,  6 }, /* jcnd16.b */
421*1424dfb3Schristos   /* 6 */ { 32767, -32768, 5,  7 }, /* jcnd16.w */
422*1424dfb3Schristos   /* 7 */ {     0,      0, 6,  0 }, /* jcnd16.a */
423*1424dfb3Schristos 
424*1424dfb3Schristos   /* 8 */ {     8,      1, 1,  9 }, /* jmp16.s */
425*1424dfb3Schristos   /* 9 */ {   127,   -128, 2, 10 }, /* jmp16.b */
426*1424dfb3Schristos  /* 10 */ { 32767, -32768, 3, 11 }, /* jmp16.w */
427*1424dfb3Schristos  /* 11 */ {     0,      0, 4,  0 }, /* jmp16.a */
428*1424dfb3Schristos 
429*1424dfb3Schristos  /* 12 */ {   127,   -128, 2, 13 }, /* jcnd32.b */
430*1424dfb3Schristos  /* 13 */ { 32767, -32768, 5, 14 }, /* jcnd32.w */
431*1424dfb3Schristos  /* 14 */ {     0,      0, 6,  0 }, /* jcnd32.a */
432*1424dfb3Schristos 
433*1424dfb3Schristos  /* 15 */ {     8,      1, 1, 16 }, /* jmp32.s */
434*1424dfb3Schristos  /* 16 */ {   127,   -128, 2, 17 }, /* jmp32.b */
435*1424dfb3Schristos  /* 17 */ { 32767, -32768, 3, 18 }, /* jmp32.w */
436*1424dfb3Schristos  /* 18 */ {     0,      0, 4,  0 }, /* jmp32.a */
437*1424dfb3Schristos 
438*1424dfb3Schristos  /* 19 */ { 32767, -32768, 3, 20 }, /* jsr16.w */
439*1424dfb3Schristos  /* 20 */ {     0,      0, 4,  0 }, /* jsr16.a */
440*1424dfb3Schristos  /* 21 */ { 32767, -32768, 3, 11 }, /* jsr32.w */
441*1424dfb3Schristos  /* 22 */ {     0,      0, 4,  0 }, /* jsr32.a */
442*1424dfb3Schristos 
443*1424dfb3Schristos  /* 23 */ {     0,      0, 3,  0 }, /* adjnz pc8 */
444*1424dfb3Schristos  /* 24 */ {     0,      0, 4,  0 }, /* adjnz disp8 pc8 */
445*1424dfb3Schristos  /* 25 */ {     0,      0, 5,  0 }, /* adjnz disp16 pc8 */
446*1424dfb3Schristos  /* 26 */ {     0,      0, 6,  0 }  /* adjnz disp24 pc8 */
447*1424dfb3Schristos };
448*1424dfb3Schristos 
449*1424dfb3Schristos enum {
450*1424dfb3Schristos   M32C_MACRO_JCND16_5_W,
451*1424dfb3Schristos   M32C_MACRO_JCND16_5_A,
452*1424dfb3Schristos   M32C_MACRO_JCND16_W,
453*1424dfb3Schristos   M32C_MACRO_JCND16_A,
454*1424dfb3Schristos   M32C_MACRO_JCND32_W,
455*1424dfb3Schristos   M32C_MACRO_JCND32_A,
456*1424dfb3Schristos   /* the digit is the array index of the pcrel byte */
457*1424dfb3Schristos   M32C_MACRO_ADJNZ_2,
458*1424dfb3Schristos   M32C_MACRO_ADJNZ_3,
459*1424dfb3Schristos   M32C_MACRO_ADJNZ_4,
460*1424dfb3Schristos   M32C_MACRO_ADJNZ_5,
461*1424dfb3Schristos };
462*1424dfb3Schristos 
463*1424dfb3Schristos static struct {
464*1424dfb3Schristos   int insn;
465*1424dfb3Schristos   int bytes;
466*1424dfb3Schristos   int insn_for_extern;
467*1424dfb3Schristos   int pcrel_aim_offset;
468*1424dfb3Schristos } subtype_mappings[] = {
469*1424dfb3Schristos   /* 0 */ { 0, 0, 0, 0 },
470*1424dfb3Schristos   /* 1 */ { 0, 0, 0, 0 },
471*1424dfb3Schristos 
472*1424dfb3Schristos   /* 2 */ {  M32C_INSN_JCND16_5,    2, -M32C_MACRO_JCND16_5_A, 1 },
473*1424dfb3Schristos   /* 3 */ { -M32C_MACRO_JCND16_5_W, 5, -M32C_MACRO_JCND16_5_A, 4 },
474*1424dfb3Schristos   /* 4 */ { -M32C_MACRO_JCND16_5_A, 6, -M32C_MACRO_JCND16_5_A, 0 },
475*1424dfb3Schristos 
476*1424dfb3Schristos   /* 5 */ {  M32C_INSN_JCND16,      3, -M32C_MACRO_JCND16_A,   1 },
477*1424dfb3Schristos   /* 6 */ { -M32C_MACRO_JCND16_W,   6, -M32C_MACRO_JCND16_A,   4 },
478*1424dfb3Schristos   /* 7 */ { -M32C_MACRO_JCND16_A,   7, -M32C_MACRO_JCND16_A,   0 },
479*1424dfb3Schristos 
480*1424dfb3Schristos   /* 8 */ {  M32C_INSN_JMP16_S,     1, M32C_INSN_JMP16_A,     0 },
481*1424dfb3Schristos   /* 9 */ {  M32C_INSN_JMP16_B,     2, M32C_INSN_JMP16_A,     1 },
482*1424dfb3Schristos  /* 10 */ {  M32C_INSN_JMP16_W,     3, M32C_INSN_JMP16_A,     2 },
483*1424dfb3Schristos  /* 11 */ {  M32C_INSN_JMP16_A,     4, M32C_INSN_JMP16_A,     0 },
484*1424dfb3Schristos 
485*1424dfb3Schristos  /* 12 */ {  M32C_INSN_JCND32,      2, -M32C_MACRO_JCND32_A,   1 },
486*1424dfb3Schristos  /* 13 */ { -M32C_MACRO_JCND32_W,   5, -M32C_MACRO_JCND32_A,   4 },
487*1424dfb3Schristos  /* 14 */ { -M32C_MACRO_JCND32_A,   6, -M32C_MACRO_JCND32_A,   0 },
488*1424dfb3Schristos 
489*1424dfb3Schristos  /* 15 */ {  M32C_INSN_JMP32_S,     1, M32C_INSN_JMP32_A,     0 },
490*1424dfb3Schristos  /* 16 */ {  M32C_INSN_JMP32_B,     2, M32C_INSN_JMP32_A,     1 },
491*1424dfb3Schristos  /* 17 */ {  M32C_INSN_JMP32_W,     3, M32C_INSN_JMP32_A,     2 },
492*1424dfb3Schristos  /* 18 */ {  M32C_INSN_JMP32_A,     4, M32C_INSN_JMP32_A,     0 },
493*1424dfb3Schristos 
494*1424dfb3Schristos  /* 19 */ {  M32C_INSN_JSR16_W,     3, M32C_INSN_JSR16_A,     2 },
495*1424dfb3Schristos  /* 20 */ {  M32C_INSN_JSR16_A,     4, M32C_INSN_JSR16_A,     0 },
496*1424dfb3Schristos  /* 21 */ {  M32C_INSN_JSR32_W,     3, M32C_INSN_JSR32_A,     2 },
497*1424dfb3Schristos  /* 22 */ {  M32C_INSN_JSR32_A,     4, M32C_INSN_JSR32_A,     0 },
498*1424dfb3Schristos 
499*1424dfb3Schristos  /* 23 */ { -M32C_MACRO_ADJNZ_2,    3, -M32C_MACRO_ADJNZ_2,    0 },
500*1424dfb3Schristos  /* 24 */ { -M32C_MACRO_ADJNZ_3,    4, -M32C_MACRO_ADJNZ_3,    0 },
501*1424dfb3Schristos  /* 25 */ { -M32C_MACRO_ADJNZ_4,    5, -M32C_MACRO_ADJNZ_4,    0 },
502*1424dfb3Schristos  /* 26 */ { -M32C_MACRO_ADJNZ_5,    6, -M32C_MACRO_ADJNZ_5,    0 }
503*1424dfb3Schristos };
504*1424dfb3Schristos #define NUM_MAPPINGS (sizeof (subtype_mappings) / sizeof (subtype_mappings[0]))
505*1424dfb3Schristos 
506*1424dfb3Schristos void
m32c_prepare_relax_scan(fragS * fragP,offsetT * aim,relax_substateT this_state)507*1424dfb3Schristos m32c_prepare_relax_scan (fragS *fragP, offsetT *aim, relax_substateT this_state)
508*1424dfb3Schristos {
509*1424dfb3Schristos   symbolS *symbolP = fragP->fr_symbol;
510*1424dfb3Schristos   if (symbolP && !S_IS_DEFINED (symbolP))
511*1424dfb3Schristos     *aim = 0;
512*1424dfb3Schristos   /* Adjust for m32c pcrel not being relative to the next opcode.  */
513*1424dfb3Schristos   *aim += subtype_mappings[this_state].pcrel_aim_offset;
514*1424dfb3Schristos }
515*1424dfb3Schristos 
516*1424dfb3Schristos static int
insn_to_subtype(int inum,const CGEN_INSN * insn)517*1424dfb3Schristos insn_to_subtype (int inum, const CGEN_INSN *insn)
518*1424dfb3Schristos {
519*1424dfb3Schristos   unsigned int i;
520*1424dfb3Schristos 
521*1424dfb3Schristos   if (insn
522*1424dfb3Schristos       && (strncmp (insn->base->mnemonic, "adjnz", 5) == 0
523*1424dfb3Schristos 	  || strncmp (insn->base->mnemonic, "sbjnz", 5) == 0))
524*1424dfb3Schristos     {
525*1424dfb3Schristos       i = 23 + insn->base->bitsize/8 - 3;
526*1424dfb3Schristos       /*printf("mapping %d used for %s\n", i, insn->base->mnemonic);*/
527*1424dfb3Schristos       return i;
528*1424dfb3Schristos     }
529*1424dfb3Schristos 
530*1424dfb3Schristos   for (i=0; i<NUM_MAPPINGS; i++)
531*1424dfb3Schristos     if (inum == subtype_mappings[i].insn)
532*1424dfb3Schristos       {
533*1424dfb3Schristos 	/*printf("mapping %d used\n", i);*/
534*1424dfb3Schristos 	return i;
535*1424dfb3Schristos       }
536*1424dfb3Schristos   abort ();
537*1424dfb3Schristos }
538*1424dfb3Schristos 
539*1424dfb3Schristos /* Return an initial guess of the length by which a fragment must grow to
540*1424dfb3Schristos    hold a branch to reach its destination.
541*1424dfb3Schristos    Also updates fr_type/fr_subtype as necessary.
542*1424dfb3Schristos 
543*1424dfb3Schristos    Called just before doing relaxation.
544*1424dfb3Schristos    Any symbol that is now undefined will not become defined.
545*1424dfb3Schristos    The guess for fr_var is ACTUALLY the growth beyond fr_fix.
546*1424dfb3Schristos    Whatever we do to grow fr_fix or fr_var contributes to our returned value.
547*1424dfb3Schristos    Although it may not be explicit in the frag, pretend fr_var starts with a
548*1424dfb3Schristos    0 value.  */
549*1424dfb3Schristos 
550*1424dfb3Schristos int
md_estimate_size_before_relax(fragS * fragP,segT segment ATTRIBUTE_UNUSED)551*1424dfb3Schristos md_estimate_size_before_relax (fragS * fragP, segT segment ATTRIBUTE_UNUSED)
552*1424dfb3Schristos {
553*1424dfb3Schristos   int where = fragP->fr_opcode - fragP->fr_literal;
554*1424dfb3Schristos 
555*1424dfb3Schristos   if (fragP->fr_subtype == 1)
556*1424dfb3Schristos     fragP->fr_subtype = insn_to_subtype (fragP->fr_cgen.insn->base->num, fragP->fr_cgen.insn);
557*1424dfb3Schristos 
558*1424dfb3Schristos   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
559*1424dfb3Schristos     {
560*1424dfb3Schristos       int new_insn;
561*1424dfb3Schristos 
562*1424dfb3Schristos       new_insn = subtype_mappings[fragP->fr_subtype].insn_for_extern;
563*1424dfb3Schristos       fragP->fr_subtype = insn_to_subtype (new_insn, 0);
564*1424dfb3Schristos     }
565*1424dfb3Schristos 
566*1424dfb3Schristos   if (fragP->fr_cgen.insn->base
567*1424dfb3Schristos       && fragP->fr_cgen.insn->base->num
568*1424dfb3Schristos          != subtype_mappings[fragP->fr_subtype].insn
569*1424dfb3Schristos       && subtype_mappings[fragP->fr_subtype].insn > 0)
570*1424dfb3Schristos     {
571*1424dfb3Schristos       int new_insn= subtype_mappings[fragP->fr_subtype].insn;
572*1424dfb3Schristos       if (new_insn >= 0)
573*1424dfb3Schristos 	{
574*1424dfb3Schristos 	  fragP->fr_cgen.insn = (fragP->fr_cgen.insn
575*1424dfb3Schristos 				 - fragP->fr_cgen.insn->base->num
576*1424dfb3Schristos 				 + new_insn);
577*1424dfb3Schristos 	}
578*1424dfb3Schristos     }
579*1424dfb3Schristos 
580*1424dfb3Schristos   return subtype_mappings[fragP->fr_subtype].bytes - (fragP->fr_fix - where);
581*1424dfb3Schristos }
582*1424dfb3Schristos 
583*1424dfb3Schristos /* *fragP has been relaxed to its final size, and now needs to have
584*1424dfb3Schristos    the bytes inside it modified to conform to the new size.
585*1424dfb3Schristos 
586*1424dfb3Schristos    Called after relaxation is finished.
587*1424dfb3Schristos    fragP->fr_type == rs_machine_dependent.
588*1424dfb3Schristos    fragP->fr_subtype is the subtype of what the address relaxed to.  */
589*1424dfb3Schristos 
590*1424dfb3Schristos static int
target_address_for(fragS * frag)591*1424dfb3Schristos target_address_for (fragS *frag)
592*1424dfb3Schristos {
593*1424dfb3Schristos   int rv = frag->fr_offset;
594*1424dfb3Schristos   symbolS *sym = frag->fr_symbol;
595*1424dfb3Schristos 
596*1424dfb3Schristos   if (sym)
597*1424dfb3Schristos     rv += S_GET_VALUE (sym);
598*1424dfb3Schristos 
599*1424dfb3Schristos   /*printf("target_address_for returns %d\n", rv);*/
600*1424dfb3Schristos   return rv;
601*1424dfb3Schristos }
602*1424dfb3Schristos 
603*1424dfb3Schristos void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,segT sec ATTRIBUTE_UNUSED,fragS * fragP ATTRIBUTE_UNUSED)604*1424dfb3Schristos md_convert_frag (bfd *   abfd ATTRIBUTE_UNUSED,
605*1424dfb3Schristos 		 segT    sec ATTRIBUTE_UNUSED,
606*1424dfb3Schristos 		 fragS * fragP ATTRIBUTE_UNUSED)
607*1424dfb3Schristos {
608*1424dfb3Schristos   int addend;
609*1424dfb3Schristos   int operand;
610*1424dfb3Schristos   int where = fragP->fr_opcode - fragP->fr_literal;
611*1424dfb3Schristos   int rl_where = fragP->fr_opcode - fragP->fr_literal;
612*1424dfb3Schristos   unsigned char *op = (unsigned char *)fragP->fr_opcode;
613*1424dfb3Schristos   int rl_addend = 0;
614*1424dfb3Schristos 
615*1424dfb3Schristos   addend = target_address_for (fragP) - (fragP->fr_address + where);
616*1424dfb3Schristos 
617*1424dfb3Schristos   fragP->fr_fix = where + subtype_mappings[fragP->fr_subtype].bytes;
618*1424dfb3Schristos 
619*1424dfb3Schristos   switch (subtype_mappings[fragP->fr_subtype].insn)
620*1424dfb3Schristos     {
621*1424dfb3Schristos     case M32C_INSN_JCND16_5:
622*1424dfb3Schristos       op[1] = addend - 1;
623*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_8;
624*1424dfb3Schristos       rl_addend = 0x21;
625*1424dfb3Schristos       break;
626*1424dfb3Schristos 
627*1424dfb3Schristos     case -M32C_MACRO_JCND16_5_W:
628*1424dfb3Schristos       op[0] ^= 0x04;
629*1424dfb3Schristos       op[1] = 4;
630*1424dfb3Schristos       op[2] = 0xf4;
631*1424dfb3Schristos       op[3] = addend - 3;
632*1424dfb3Schristos       op[4] = (addend - 3) >> 8;
633*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
634*1424dfb3Schristos       where += 2;
635*1424dfb3Schristos       rl_addend = 0x51;
636*1424dfb3Schristos       break;
637*1424dfb3Schristos 
638*1424dfb3Schristos     case -M32C_MACRO_JCND16_5_A:
639*1424dfb3Schristos       op[0] ^= 0x04;
640*1424dfb3Schristos       op[1] = 5;
641*1424dfb3Schristos       op[2] = 0xfc;
642*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
643*1424dfb3Schristos       where += 2;
644*1424dfb3Schristos       rl_addend = 0x61;
645*1424dfb3Schristos       break;
646*1424dfb3Schristos 
647*1424dfb3Schristos 
648*1424dfb3Schristos     case M32C_INSN_JCND16:
649*1424dfb3Schristos       op[2] = addend - 2;
650*1424dfb3Schristos       operand = M32C_OPERAND_LAB_16_8;
651*1424dfb3Schristos       rl_addend = 0x31;
652*1424dfb3Schristos       break;
653*1424dfb3Schristos 
654*1424dfb3Schristos     case -M32C_MACRO_JCND16_W:
655*1424dfb3Schristos       op[1] ^= 0x04;
656*1424dfb3Schristos       op[2] = 4;
657*1424dfb3Schristos       op[3] = 0xf4;
658*1424dfb3Schristos       op[4] = addend - 4;
659*1424dfb3Schristos       op[5] = (addend - 4) >> 8;
660*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
661*1424dfb3Schristos       where += 3;
662*1424dfb3Schristos       rl_addend = 0x61;
663*1424dfb3Schristos       break;
664*1424dfb3Schristos 
665*1424dfb3Schristos     case -M32C_MACRO_JCND16_A:
666*1424dfb3Schristos       op[1] ^= 0x04;
667*1424dfb3Schristos       op[2] = 5;
668*1424dfb3Schristos       op[3] = 0xfc;
669*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
670*1424dfb3Schristos       where += 3;
671*1424dfb3Schristos       rl_addend = 0x71;
672*1424dfb3Schristos       break;
673*1424dfb3Schristos 
674*1424dfb3Schristos     case M32C_INSN_JMP16_S:
675*1424dfb3Schristos       op[0] = 0x60 | ((addend-2) & 0x07);
676*1424dfb3Schristos       operand = M32C_OPERAND_LAB_5_3;
677*1424dfb3Schristos       rl_addend = 0x10;
678*1424dfb3Schristos       break;
679*1424dfb3Schristos 
680*1424dfb3Schristos     case M32C_INSN_JMP16_B:
681*1424dfb3Schristos       op[0] = 0xfe;
682*1424dfb3Schristos       op[1] = addend - 1;
683*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_8;
684*1424dfb3Schristos       rl_addend = 0x21;
685*1424dfb3Schristos       break;
686*1424dfb3Schristos 
687*1424dfb3Schristos     case M32C_INSN_JMP16_W:
688*1424dfb3Schristos       op[0] = 0xf4;
689*1424dfb3Schristos       op[1] = addend - 1;
690*1424dfb3Schristos       op[2] = (addend - 1) >> 8;
691*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
692*1424dfb3Schristos       rl_addend = 0x31;
693*1424dfb3Schristos       break;
694*1424dfb3Schristos 
695*1424dfb3Schristos     case M32C_INSN_JMP16_A:
696*1424dfb3Schristos       op[0] = 0xfc;
697*1424dfb3Schristos       op[1] = 0;
698*1424dfb3Schristos       op[2] = 0;
699*1424dfb3Schristos       op[3] = 0;
700*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
701*1424dfb3Schristos       rl_addend = 0x41;
702*1424dfb3Schristos       break;
703*1424dfb3Schristos 
704*1424dfb3Schristos     case M32C_INSN_JCND32:
705*1424dfb3Schristos       op[1] = addend - 1;
706*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_8;
707*1424dfb3Schristos       rl_addend = 0x21;
708*1424dfb3Schristos       break;
709*1424dfb3Schristos 
710*1424dfb3Schristos     case -M32C_MACRO_JCND32_W:
711*1424dfb3Schristos       op[0] ^= 0x40;
712*1424dfb3Schristos       op[1] = 4;
713*1424dfb3Schristos       op[2] = 0xce;
714*1424dfb3Schristos       op[3] = addend - 3;
715*1424dfb3Schristos       op[4] = (addend - 3) >> 8;
716*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
717*1424dfb3Schristos       where += 2;
718*1424dfb3Schristos       rl_addend = 0x51;
719*1424dfb3Schristos       break;
720*1424dfb3Schristos 
721*1424dfb3Schristos     case -M32C_MACRO_JCND32_A:
722*1424dfb3Schristos       op[0] ^= 0x40;
723*1424dfb3Schristos       op[1] = 5;
724*1424dfb3Schristos       op[2] = 0xcc;
725*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
726*1424dfb3Schristos       where += 2;
727*1424dfb3Schristos       rl_addend = 0x61;
728*1424dfb3Schristos       break;
729*1424dfb3Schristos 
730*1424dfb3Schristos     case M32C_INSN_JMP32_S:
731*1424dfb3Schristos       addend = ((addend-2) & 0x07);
732*1424dfb3Schristos       op[0] = 0x4a | (addend & 0x01) | ((addend << 3) & 0x30);
733*1424dfb3Schristos       operand = M32C_OPERAND_LAB32_JMP_S;
734*1424dfb3Schristos       rl_addend = 0x10;
735*1424dfb3Schristos       break;
736*1424dfb3Schristos 
737*1424dfb3Schristos     case M32C_INSN_JMP32_B:
738*1424dfb3Schristos       op[0] = 0xbb;
739*1424dfb3Schristos       op[1] = addend - 1;
740*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_8;
741*1424dfb3Schristos       rl_addend = 0x21;
742*1424dfb3Schristos       break;
743*1424dfb3Schristos 
744*1424dfb3Schristos     case M32C_INSN_JMP32_W:
745*1424dfb3Schristos       op[0] = 0xce;
746*1424dfb3Schristos       op[1] = addend - 1;
747*1424dfb3Schristos       op[2] = (addend - 1) >> 8;
748*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
749*1424dfb3Schristos       rl_addend = 0x31;
750*1424dfb3Schristos       break;
751*1424dfb3Schristos 
752*1424dfb3Schristos     case M32C_INSN_JMP32_A:
753*1424dfb3Schristos       op[0] = 0xcc;
754*1424dfb3Schristos       op[1] = 0;
755*1424dfb3Schristos       op[2] = 0;
756*1424dfb3Schristos       op[3] = 0;
757*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
758*1424dfb3Schristos       rl_addend = 0x41;
759*1424dfb3Schristos       break;
760*1424dfb3Schristos 
761*1424dfb3Schristos 
762*1424dfb3Schristos     case M32C_INSN_JSR16_W:
763*1424dfb3Schristos       op[0] = 0xf5;
764*1424dfb3Schristos       op[1] = addend - 1;
765*1424dfb3Schristos       op[2] = (addend - 1) >> 8;
766*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
767*1424dfb3Schristos       rl_addend = 0x31;
768*1424dfb3Schristos       break;
769*1424dfb3Schristos 
770*1424dfb3Schristos     case M32C_INSN_JSR16_A:
771*1424dfb3Schristos       op[0] = 0xfd;
772*1424dfb3Schristos       op[1] = 0;
773*1424dfb3Schristos       op[2] = 0;
774*1424dfb3Schristos       op[3] = 0;
775*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
776*1424dfb3Schristos       rl_addend = 0x41;
777*1424dfb3Schristos       break;
778*1424dfb3Schristos 
779*1424dfb3Schristos     case M32C_INSN_JSR32_W:
780*1424dfb3Schristos       op[0] = 0xcf;
781*1424dfb3Schristos       op[1] = addend - 1;
782*1424dfb3Schristos       op[2] = (addend - 1) >> 8;
783*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_16;
784*1424dfb3Schristos       rl_addend = 0x31;
785*1424dfb3Schristos       break;
786*1424dfb3Schristos 
787*1424dfb3Schristos     case M32C_INSN_JSR32_A:
788*1424dfb3Schristos       op[0] = 0xcd;
789*1424dfb3Schristos       op[1] = 0;
790*1424dfb3Schristos       op[2] = 0;
791*1424dfb3Schristos       op[3] = 0;
792*1424dfb3Schristos       operand = M32C_OPERAND_LAB_8_24;
793*1424dfb3Schristos       rl_addend = 0x41;
794*1424dfb3Schristos       break;
795*1424dfb3Schristos 
796*1424dfb3Schristos     case -M32C_MACRO_ADJNZ_2:
797*1424dfb3Schristos       rl_addend = 0x31;
798*1424dfb3Schristos       op[2] = addend - 2;
799*1424dfb3Schristos       operand = M32C_OPERAND_LAB_16_8;
800*1424dfb3Schristos       break;
801*1424dfb3Schristos     case -M32C_MACRO_ADJNZ_3:
802*1424dfb3Schristos       rl_addend = 0x41;
803*1424dfb3Schristos       op[3] = addend - 2;
804*1424dfb3Schristos       operand = M32C_OPERAND_LAB_24_8;
805*1424dfb3Schristos       break;
806*1424dfb3Schristos     case -M32C_MACRO_ADJNZ_4:
807*1424dfb3Schristos       rl_addend = 0x51;
808*1424dfb3Schristos       op[4] = addend - 2;
809*1424dfb3Schristos       operand = M32C_OPERAND_LAB_32_8;
810*1424dfb3Schristos       break;
811*1424dfb3Schristos     case -M32C_MACRO_ADJNZ_5:
812*1424dfb3Schristos       rl_addend = 0x61;
813*1424dfb3Schristos       op[5] = addend - 2;
814*1424dfb3Schristos       operand = M32C_OPERAND_LAB_40_8;
815*1424dfb3Schristos       break;
816*1424dfb3Schristos 
817*1424dfb3Schristos     default:
818*1424dfb3Schristos       printf("\nHey!  Need more opcode converters! missing: %d %s\n\n",
819*1424dfb3Schristos 	     fragP->fr_subtype,
820*1424dfb3Schristos 	     fragP->fr_cgen.insn->base->name);
821*1424dfb3Schristos       abort();
822*1424dfb3Schristos     }
823*1424dfb3Schristos 
824*1424dfb3Schristos   if (m32c_relax)
825*1424dfb3Schristos     {
826*1424dfb3Schristos       if (operand != M32C_OPERAND_LAB_8_24)
827*1424dfb3Schristos 	fragP->fr_offset = (fragP->fr_address + where);
828*1424dfb3Schristos 
829*1424dfb3Schristos       fix_new (fragP,
830*1424dfb3Schristos 	       rl_where,
831*1424dfb3Schristos 	       0, abs_section_sym, rl_addend, 0,
832*1424dfb3Schristos 	       BFD_RELOC_M32C_RL_JUMP);
833*1424dfb3Schristos     }
834*1424dfb3Schristos 
835*1424dfb3Schristos   if (S_GET_SEGMENT (fragP->fr_symbol) != sec
836*1424dfb3Schristos       || operand == M32C_OPERAND_LAB_8_24
837*1424dfb3Schristos       || (m32c_relax && (operand != M32C_OPERAND_LAB_5_3
838*1424dfb3Schristos 			 && operand != M32C_OPERAND_LAB32_JMP_S)))
839*1424dfb3Schristos     {
840*1424dfb3Schristos       gas_assert (fragP->fr_cgen.insn != 0);
841*1424dfb3Schristos       gas_cgen_record_fixup (fragP,
842*1424dfb3Schristos 			     where,
843*1424dfb3Schristos 			     fragP->fr_cgen.insn,
844*1424dfb3Schristos 			     (fragP->fr_fix - where) * 8,
845*1424dfb3Schristos 			     cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
846*1424dfb3Schristos 							 operand),
847*1424dfb3Schristos 			     fragP->fr_cgen.opinfo,
848*1424dfb3Schristos 			     fragP->fr_symbol,
849*1424dfb3Schristos 			     fragP->fr_offset);
850*1424dfb3Schristos     }
851*1424dfb3Schristos }
852*1424dfb3Schristos 
853*1424dfb3Schristos /* Functions concerning relocs.  */
854*1424dfb3Schristos 
855*1424dfb3Schristos /* The location from which a PC relative jump should be calculated,
856*1424dfb3Schristos    given a PC relative reloc.  */
857*1424dfb3Schristos 
858*1424dfb3Schristos long
md_pcrel_from_section(fixS * fixP,segT sec)859*1424dfb3Schristos md_pcrel_from_section (fixS * fixP, segT sec)
860*1424dfb3Schristos {
861*1424dfb3Schristos   if (fixP->fx_addsy != (symbolS *) NULL
862*1424dfb3Schristos       && (! S_IS_DEFINED (fixP->fx_addsy)
863*1424dfb3Schristos 	  || S_GET_SEGMENT (fixP->fx_addsy) != sec))
864*1424dfb3Schristos     /* The symbol is undefined (or is defined but not in this section).
865*1424dfb3Schristos        Let the linker figure it out.  */
866*1424dfb3Schristos     return 0;
867*1424dfb3Schristos 
868*1424dfb3Schristos   return (fixP->fx_frag->fr_address + fixP->fx_where);
869*1424dfb3Schristos }
870*1424dfb3Schristos 
871*1424dfb3Schristos /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
872*1424dfb3Schristos    Returns BFD_RELOC_NONE if no reloc type can be found.
873*1424dfb3Schristos    *FIXP may be modified if desired.  */
874*1424dfb3Schristos 
875*1424dfb3Schristos bfd_reloc_code_real_type
md_cgen_lookup_reloc(const CGEN_INSN * insn ATTRIBUTE_UNUSED,const CGEN_OPERAND * operand,fixS * fixP ATTRIBUTE_UNUSED)876*1424dfb3Schristos md_cgen_lookup_reloc (const CGEN_INSN *    insn ATTRIBUTE_UNUSED,
877*1424dfb3Schristos 		      const CGEN_OPERAND * operand,
878*1424dfb3Schristos 		      fixS *               fixP ATTRIBUTE_UNUSED)
879*1424dfb3Schristos {
880*1424dfb3Schristos   static const struct op_reloc {
881*1424dfb3Schristos     /* A CGEN operand type that can be a relocatable expression.  */
882*1424dfb3Schristos     CGEN_OPERAND_TYPE operand;
883*1424dfb3Schristos 
884*1424dfb3Schristos     /* The appropriate BFD reloc type to use for that.  */
885*1424dfb3Schristos     bfd_reloc_code_real_type reloc;
886*1424dfb3Schristos 
887*1424dfb3Schristos     /* The offset from the start of the instruction to the field to be
888*1424dfb3Schristos        relocated, in bytes.  */
889*1424dfb3Schristos     int offset;
890*1424dfb3Schristos   } op_reloc_table[] = {
891*1424dfb3Schristos 
892*1424dfb3Schristos     /* PC-REL relocs for 8-bit fields.  */
893*1424dfb3Schristos     { M32C_OPERAND_LAB_8_8,    BFD_RELOC_8_PCREL, 1 },
894*1424dfb3Schristos     { M32C_OPERAND_LAB_16_8,   BFD_RELOC_8_PCREL, 2 },
895*1424dfb3Schristos     { M32C_OPERAND_LAB_24_8,   BFD_RELOC_8_PCREL, 3 },
896*1424dfb3Schristos     { M32C_OPERAND_LAB_32_8,   BFD_RELOC_8_PCREL, 4 },
897*1424dfb3Schristos     { M32C_OPERAND_LAB_40_8,   BFD_RELOC_8_PCREL, 5 },
898*1424dfb3Schristos 
899*1424dfb3Schristos     /* PC-REL relocs for 16-bit fields.  */
900*1424dfb3Schristos     { M32C_OPERAND_LAB_8_16,   BFD_RELOC_16_PCREL, 1 },
901*1424dfb3Schristos 
902*1424dfb3Schristos     /* Absolute relocs for 8-bit fields.  */
903*1424dfb3Schristos     { M32C_OPERAND_IMM_8_QI,   BFD_RELOC_8, 1 },
904*1424dfb3Schristos     { M32C_OPERAND_IMM_16_QI,  BFD_RELOC_8, 2 },
905*1424dfb3Schristos     { M32C_OPERAND_IMM_24_QI,  BFD_RELOC_8, 3 },
906*1424dfb3Schristos     { M32C_OPERAND_IMM_32_QI,  BFD_RELOC_8, 4 },
907*1424dfb3Schristos     { M32C_OPERAND_IMM_40_QI,  BFD_RELOC_8, 5 },
908*1424dfb3Schristos     { M32C_OPERAND_IMM_48_QI,  BFD_RELOC_8, 6 },
909*1424dfb3Schristos     { M32C_OPERAND_IMM_56_QI,  BFD_RELOC_8, 7 },
910*1424dfb3Schristos     { M32C_OPERAND_DSP_8_S8,   BFD_RELOC_8, 1 },
911*1424dfb3Schristos     { M32C_OPERAND_DSP_16_S8,  BFD_RELOC_8, 2 },
912*1424dfb3Schristos     { M32C_OPERAND_DSP_24_S8,  BFD_RELOC_8, 3 },
913*1424dfb3Schristos     { M32C_OPERAND_DSP_32_S8,  BFD_RELOC_8, 4 },
914*1424dfb3Schristos     { M32C_OPERAND_DSP_40_S8,  BFD_RELOC_8, 5 },
915*1424dfb3Schristos     { M32C_OPERAND_DSP_48_S8,  BFD_RELOC_8, 6 },
916*1424dfb3Schristos     { M32C_OPERAND_DSP_8_U8,   BFD_RELOC_8, 1 },
917*1424dfb3Schristos     { M32C_OPERAND_DSP_16_U8,  BFD_RELOC_8, 2 },
918*1424dfb3Schristos     { M32C_OPERAND_DSP_24_U8,  BFD_RELOC_8, 3 },
919*1424dfb3Schristos     { M32C_OPERAND_DSP_32_U8,  BFD_RELOC_8, 4 },
920*1424dfb3Schristos     { M32C_OPERAND_DSP_40_U8,  BFD_RELOC_8, 5 },
921*1424dfb3Schristos     { M32C_OPERAND_DSP_48_U8,  BFD_RELOC_8, 6 },
922*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_16_S11_UNPREFIXED, BFD_RELOC_8, 2 },
923*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_16_U11_UNPREFIXED, BFD_RELOC_8, 2 },
924*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_24_S11_PREFIXED, BFD_RELOC_8, 3 },
925*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_24_U11_PREFIXED, BFD_RELOC_8, 3 },
926*1424dfb3Schristos 
927*1424dfb3Schristos     /* Absolute relocs for 16-bit fields.  */
928*1424dfb3Schristos     { M32C_OPERAND_IMM_8_HI,   BFD_RELOC_16, 1 },
929*1424dfb3Schristos     { M32C_OPERAND_IMM_16_HI,  BFD_RELOC_16, 2 },
930*1424dfb3Schristos     { M32C_OPERAND_IMM_24_HI,  BFD_RELOC_16, 3 },
931*1424dfb3Schristos     { M32C_OPERAND_IMM_32_HI,  BFD_RELOC_16, 4 },
932*1424dfb3Schristos     { M32C_OPERAND_IMM_40_HI,  BFD_RELOC_16, 5 },
933*1424dfb3Schristos     { M32C_OPERAND_IMM_48_HI,  BFD_RELOC_16, 6 },
934*1424dfb3Schristos     { M32C_OPERAND_IMM_56_HI,  BFD_RELOC_16, 7 },
935*1424dfb3Schristos     { M32C_OPERAND_IMM_64_HI,  BFD_RELOC_16, 8 },
936*1424dfb3Schristos     { M32C_OPERAND_DSP_16_S16, BFD_RELOC_16, 2 },
937*1424dfb3Schristos     { M32C_OPERAND_DSP_24_S16, BFD_RELOC_16, 3 },
938*1424dfb3Schristos     { M32C_OPERAND_DSP_32_S16, BFD_RELOC_16, 4 },
939*1424dfb3Schristos     { M32C_OPERAND_DSP_40_S16, BFD_RELOC_16, 5 },
940*1424dfb3Schristos     { M32C_OPERAND_DSP_8_U16,  BFD_RELOC_16, 1 },
941*1424dfb3Schristos     { M32C_OPERAND_DSP_16_U16, BFD_RELOC_16, 2 },
942*1424dfb3Schristos     { M32C_OPERAND_DSP_24_U16, BFD_RELOC_16, 3 },
943*1424dfb3Schristos     { M32C_OPERAND_DSP_32_U16, BFD_RELOC_16, 4 },
944*1424dfb3Schristos     { M32C_OPERAND_DSP_40_U16, BFD_RELOC_16, 5 },
945*1424dfb3Schristos     { M32C_OPERAND_DSP_48_U16, BFD_RELOC_16, 6 },
946*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_16_S19_UNPREFIXED, BFD_RELOC_16, 2 },
947*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_16_U19_UNPREFIXED, BFD_RELOC_16, 2 },
948*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_24_S19_PREFIXED, BFD_RELOC_16, 3 },
949*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_24_U19_PREFIXED, BFD_RELOC_16, 3 },
950*1424dfb3Schristos 
951*1424dfb3Schristos     /* Absolute relocs for 24-bit fields.  */
952*1424dfb3Schristos     { M32C_OPERAND_LAB_8_24,   BFD_RELOC_24, 1 },
953*1424dfb3Schristos     { M32C_OPERAND_DSP_8_S24,  BFD_RELOC_24, 1 },
954*1424dfb3Schristos     { M32C_OPERAND_DSP_8_U24,  BFD_RELOC_24, 1 },
955*1424dfb3Schristos     { M32C_OPERAND_DSP_16_U24, BFD_RELOC_24, 2 },
956*1424dfb3Schristos     { M32C_OPERAND_DSP_24_U24, BFD_RELOC_24, 3 },
957*1424dfb3Schristos     { M32C_OPERAND_DSP_32_U24, BFD_RELOC_24, 4 },
958*1424dfb3Schristos     { M32C_OPERAND_DSP_40_U24, BFD_RELOC_24, 5 },
959*1424dfb3Schristos     { M32C_OPERAND_DSP_48_U24, BFD_RELOC_24, 6 },
960*1424dfb3Schristos     { M32C_OPERAND_DSP_16_U20, BFD_RELOC_24, 2 },
961*1424dfb3Schristos     { M32C_OPERAND_DSP_24_U20, BFD_RELOC_24, 3 },
962*1424dfb3Schristos     { M32C_OPERAND_DSP_32_U20, BFD_RELOC_24, 4 },
963*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_16_U27_UNPREFIXED, BFD_RELOC_24, 2 },
964*1424dfb3Schristos     { M32C_OPERAND_BITBASE32_24_U27_PREFIXED, BFD_RELOC_24, 3 },
965*1424dfb3Schristos 
966*1424dfb3Schristos     /* Absolute relocs for 32-bit fields.  */
967*1424dfb3Schristos     { M32C_OPERAND_IMM_16_SI,  BFD_RELOC_32, 2 },
968*1424dfb3Schristos     { M32C_OPERAND_IMM_24_SI,  BFD_RELOC_32, 3 },
969*1424dfb3Schristos     { M32C_OPERAND_IMM_32_SI,  BFD_RELOC_32, 4 },
970*1424dfb3Schristos     { M32C_OPERAND_IMM_40_SI,  BFD_RELOC_32, 5 },
971*1424dfb3Schristos 
972*1424dfb3Schristos   };
973*1424dfb3Schristos 
974*1424dfb3Schristos   int i;
975*1424dfb3Schristos 
976*1424dfb3Schristos   for (i = ARRAY_SIZE (op_reloc_table); --i >= 0; )
977*1424dfb3Schristos     {
978*1424dfb3Schristos       const struct op_reloc *or = &op_reloc_table[i];
979*1424dfb3Schristos 
980*1424dfb3Schristos       if (or->operand == operand->type)
981*1424dfb3Schristos         {
982*1424dfb3Schristos           fixP->fx_where += or->offset;
983*1424dfb3Schristos           fixP->fx_size -= or->offset;
984*1424dfb3Schristos 
985*1424dfb3Schristos 	  if (fixP->fx_cgen.opinfo
986*1424dfb3Schristos 	      && fixP->fx_cgen.opinfo != BFD_RELOC_NONE)
987*1424dfb3Schristos 	    return fixP->fx_cgen.opinfo;
988*1424dfb3Schristos 
989*1424dfb3Schristos           return or->reloc;
990*1424dfb3Schristos         }
991*1424dfb3Schristos     }
992*1424dfb3Schristos 
993*1424dfb3Schristos   fprintf
994*1424dfb3Schristos     (stderr,
995*1424dfb3Schristos      "Error: tc-m32c.c:md_cgen_lookup_reloc Unimplemented relocation for operand %s\n",
996*1424dfb3Schristos      operand->name);
997*1424dfb3Schristos 
998*1424dfb3Schristos   return BFD_RELOC_NONE;
999*1424dfb3Schristos }
1000*1424dfb3Schristos 
1001*1424dfb3Schristos void
m32c_cons_fix_new(fragS * frag,int where,int size,expressionS * exp,bfd_reloc_code_real_type type)1002*1424dfb3Schristos m32c_cons_fix_new (fragS *	frag,
1003*1424dfb3Schristos 		   int		where,
1004*1424dfb3Schristos 		   int		size,
1005*1424dfb3Schristos 		   expressionS *exp,
1006*1424dfb3Schristos 		   bfd_reloc_code_real_type type)
1007*1424dfb3Schristos {
1008*1424dfb3Schristos   switch (size)
1009*1424dfb3Schristos     {
1010*1424dfb3Schristos     case 1:
1011*1424dfb3Schristos       type = BFD_RELOC_8;
1012*1424dfb3Schristos       break;
1013*1424dfb3Schristos     case 2:
1014*1424dfb3Schristos       type = BFD_RELOC_16;
1015*1424dfb3Schristos       break;
1016*1424dfb3Schristos     case 3:
1017*1424dfb3Schristos       type = BFD_RELOC_24;
1018*1424dfb3Schristos       break;
1019*1424dfb3Schristos     case 4:
1020*1424dfb3Schristos     default:
1021*1424dfb3Schristos       type = BFD_RELOC_32;
1022*1424dfb3Schristos       break;
1023*1424dfb3Schristos     case 8:
1024*1424dfb3Schristos       type = BFD_RELOC_64;
1025*1424dfb3Schristos       break;
1026*1424dfb3Schristos     }
1027*1424dfb3Schristos 
1028*1424dfb3Schristos   fix_new_exp (frag, where, (int) size, exp, 0, type);
1029*1424dfb3Schristos }
1030*1424dfb3Schristos 
1031*1424dfb3Schristos void
m32c_apply_fix(struct fix * f,valueT * t,segT s)1032*1424dfb3Schristos m32c_apply_fix (struct fix *f, valueT *t, segT s)
1033*1424dfb3Schristos {
1034*1424dfb3Schristos   if (f->fx_r_type == BFD_RELOC_M32C_RL_JUMP
1035*1424dfb3Schristos       || f->fx_r_type == BFD_RELOC_M32C_RL_1ADDR
1036*1424dfb3Schristos       || f->fx_r_type == BFD_RELOC_M32C_RL_2ADDR)
1037*1424dfb3Schristos     return;
1038*1424dfb3Schristos   gas_cgen_md_apply_fix (f, t, s);
1039*1424dfb3Schristos }
1040*1424dfb3Schristos 
1041*1424dfb3Schristos arelent *
tc_gen_reloc(asection * sec,fixS * fx)1042*1424dfb3Schristos tc_gen_reloc (asection *sec, fixS *fx)
1043*1424dfb3Schristos {
1044*1424dfb3Schristos   if (fx->fx_r_type == BFD_RELOC_M32C_RL_JUMP
1045*1424dfb3Schristos       || fx->fx_r_type == BFD_RELOC_M32C_RL_1ADDR
1046*1424dfb3Schristos       || fx->fx_r_type == BFD_RELOC_M32C_RL_2ADDR)
1047*1424dfb3Schristos     {
1048*1424dfb3Schristos       arelent * reloc;
1049*1424dfb3Schristos 
1050*1424dfb3Schristos       reloc = XNEW (arelent);
1051*1424dfb3Schristos 
1052*1424dfb3Schristos       reloc->sym_ptr_ptr = XNEW (asymbol *);
1053*1424dfb3Schristos       *reloc->sym_ptr_ptr = symbol_get_bfdsym (fx->fx_addsy);
1054*1424dfb3Schristos       reloc->address = fx->fx_frag->fr_address + fx->fx_where;
1055*1424dfb3Schristos       reloc->howto = bfd_reloc_type_lookup (stdoutput, fx->fx_r_type);
1056*1424dfb3Schristos       reloc->addend = fx->fx_offset;
1057*1424dfb3Schristos       return reloc;
1058*1424dfb3Schristos 
1059*1424dfb3Schristos     }
1060*1424dfb3Schristos   return gas_cgen_tc_gen_reloc (sec, fx);
1061*1424dfb3Schristos }
1062*1424dfb3Schristos 
1063*1424dfb3Schristos /* See whether we need to force a relocation into the output file.
1064*1424dfb3Schristos    This is used to force out switch and PC relative relocations when
1065*1424dfb3Schristos    relaxing.  */
1066*1424dfb3Schristos 
1067*1424dfb3Schristos int
m32c_force_relocation(fixS * fixp)1068*1424dfb3Schristos m32c_force_relocation (fixS * fixp)
1069*1424dfb3Schristos {
1070*1424dfb3Schristos   int reloc = fixp->fx_r_type;
1071*1424dfb3Schristos 
1072*1424dfb3Schristos   if (reloc > (int)BFD_RELOC_UNUSED)
1073*1424dfb3Schristos     {
1074*1424dfb3Schristos       reloc -= (int)BFD_RELOC_UNUSED;
1075*1424dfb3Schristos       switch (reloc)
1076*1424dfb3Schristos 	{
1077*1424dfb3Schristos 	case M32C_OPERAND_DSP_32_S16:
1078*1424dfb3Schristos 	case M32C_OPERAND_DSP_32_U16:
1079*1424dfb3Schristos 	case M32C_OPERAND_IMM_32_HI:
1080*1424dfb3Schristos 	case M32C_OPERAND_DSP_16_S16:
1081*1424dfb3Schristos 	case M32C_OPERAND_DSP_16_U16:
1082*1424dfb3Schristos 	case M32C_OPERAND_IMM_16_HI:
1083*1424dfb3Schristos 	case M32C_OPERAND_DSP_24_S16:
1084*1424dfb3Schristos 	case M32C_OPERAND_DSP_24_U16:
1085*1424dfb3Schristos 	case M32C_OPERAND_IMM_24_HI:
1086*1424dfb3Schristos 	  return 1;
1087*1424dfb3Schristos 
1088*1424dfb3Schristos         /* If we're doing linker relaxing, we need to keep all the
1089*1424dfb3Schristos 	   pc-relative jumps in case we need to fix them due to
1090*1424dfb3Schristos 	   deleted bytes between the jump and its destination.  */
1091*1424dfb3Schristos 	case M32C_OPERAND_LAB_8_8:
1092*1424dfb3Schristos 	case M32C_OPERAND_LAB_8_16:
1093*1424dfb3Schristos 	case M32C_OPERAND_LAB_8_24:
1094*1424dfb3Schristos 	case M32C_OPERAND_LAB_16_8:
1095*1424dfb3Schristos 	case M32C_OPERAND_LAB_24_8:
1096*1424dfb3Schristos 	case M32C_OPERAND_LAB_32_8:
1097*1424dfb3Schristos 	case M32C_OPERAND_LAB_40_8:
1098*1424dfb3Schristos 	  if (m32c_relax)
1099*1424dfb3Schristos 	    return 1;
1100*1424dfb3Schristos 	default:
1101*1424dfb3Schristos 	  break;
1102*1424dfb3Schristos 	}
1103*1424dfb3Schristos     }
1104*1424dfb3Schristos   else
1105*1424dfb3Schristos     {
1106*1424dfb3Schristos       switch (fixp->fx_r_type)
1107*1424dfb3Schristos 	{
1108*1424dfb3Schristos 	case BFD_RELOC_16:
1109*1424dfb3Schristos 	  return 1;
1110*1424dfb3Schristos 
1111*1424dfb3Schristos 	case BFD_RELOC_M32C_RL_JUMP:
1112*1424dfb3Schristos 	case BFD_RELOC_M32C_RL_1ADDR:
1113*1424dfb3Schristos 	case BFD_RELOC_M32C_RL_2ADDR:
1114*1424dfb3Schristos 	case BFD_RELOC_8_PCREL:
1115*1424dfb3Schristos 	case BFD_RELOC_16_PCREL:
1116*1424dfb3Schristos 	  if (m32c_relax)
1117*1424dfb3Schristos 	    return 1;
1118*1424dfb3Schristos 	default:
1119*1424dfb3Schristos 	  break;
1120*1424dfb3Schristos 	}
1121*1424dfb3Schristos     }
1122*1424dfb3Schristos 
1123*1424dfb3Schristos   return generic_force_reloc (fixp);
1124*1424dfb3Schristos }
1125*1424dfb3Schristos 
1126*1424dfb3Schristos /* Write a value out to the object file, using the appropriate endianness.  */
1127*1424dfb3Schristos 
1128*1424dfb3Schristos void
md_number_to_chars(char * buf,valueT val,int n)1129*1424dfb3Schristos md_number_to_chars (char * buf, valueT val, int n)
1130*1424dfb3Schristos {
1131*1424dfb3Schristos   number_to_chars_littleendian (buf, val, n);
1132*1424dfb3Schristos }
1133*1424dfb3Schristos 
1134*1424dfb3Schristos /* Turn a string in input_line_pointer into a floating point constant of type
1135*1424dfb3Schristos    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1136*1424dfb3Schristos    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.  */
1137*1424dfb3Schristos 
1138*1424dfb3Schristos const char *
md_atof(int type,char * litP,int * sizeP)1139*1424dfb3Schristos md_atof (int type, char * litP, int * sizeP)
1140*1424dfb3Schristos {
1141*1424dfb3Schristos   return ieee_md_atof (type, litP, sizeP, TRUE);
1142*1424dfb3Schristos }
1143*1424dfb3Schristos 
1144*1424dfb3Schristos bfd_boolean
m32c_fix_adjustable(fixS * fixP)1145*1424dfb3Schristos m32c_fix_adjustable (fixS * fixP)
1146*1424dfb3Schristos {
1147*1424dfb3Schristos   int reloc;
1148*1424dfb3Schristos   if (fixP->fx_addsy == NULL)
1149*1424dfb3Schristos     return 1;
1150*1424dfb3Schristos 
1151*1424dfb3Schristos   /* We need the symbol name for the VTABLE entries.  */
1152*1424dfb3Schristos   reloc = fixP->fx_r_type;
1153*1424dfb3Schristos   if (reloc > (int)BFD_RELOC_UNUSED)
1154*1424dfb3Schristos     {
1155*1424dfb3Schristos       reloc -= (int)BFD_RELOC_UNUSED;
1156*1424dfb3Schristos       switch (reloc)
1157*1424dfb3Schristos 	{
1158*1424dfb3Schristos 	case M32C_OPERAND_DSP_32_S16:
1159*1424dfb3Schristos 	case M32C_OPERAND_DSP_32_U16:
1160*1424dfb3Schristos 	case M32C_OPERAND_IMM_32_HI:
1161*1424dfb3Schristos 	case M32C_OPERAND_DSP_16_S16:
1162*1424dfb3Schristos 	case M32C_OPERAND_DSP_16_U16:
1163*1424dfb3Schristos 	case M32C_OPERAND_IMM_16_HI:
1164*1424dfb3Schristos 	case M32C_OPERAND_DSP_24_S16:
1165*1424dfb3Schristos 	case M32C_OPERAND_DSP_24_U16:
1166*1424dfb3Schristos 	case M32C_OPERAND_IMM_24_HI:
1167*1424dfb3Schristos 	  return 0;
1168*1424dfb3Schristos 	}
1169*1424dfb3Schristos     }
1170*1424dfb3Schristos   else
1171*1424dfb3Schristos     {
1172*1424dfb3Schristos       if (fixP->fx_r_type == BFD_RELOC_16)
1173*1424dfb3Schristos 	return 0;
1174*1424dfb3Schristos     }
1175*1424dfb3Schristos 
1176*1424dfb3Schristos   /* Do not adjust relocations involving symbols in merged sections.
1177*1424dfb3Schristos 
1178*1424dfb3Schristos      A reloc patching in the value of some symbol S plus some addend A
1179*1424dfb3Schristos      can be produced in different ways:
1180*1424dfb3Schristos 
1181*1424dfb3Schristos      1) It might simply be a reference to the data at S + A.  Clearly,
1182*1424dfb3Schristos         if linker merging shift that data around, the value patched in
1183*1424dfb3Schristos         by the reloc needs to be adjusted accordingly.
1184*1424dfb3Schristos 
1185*1424dfb3Schristos      2) Or, it might be a reference to S, with A added in as a constant
1186*1424dfb3Schristos 	bias.  For example, given code like this:
1187*1424dfb3Schristos 
1188*1424dfb3Schristos 	  static int S[100];
1189*1424dfb3Schristos 
1190*1424dfb3Schristos 	  ... S[i - 8] ...
1191*1424dfb3Schristos 
1192*1424dfb3Schristos 	it would be reasonable for the compiler to rearrange the array
1193*1424dfb3Schristos 	reference to something like:
1194*1424dfb3Schristos 
1195*1424dfb3Schristos 	  ... (S-8)[i] ...
1196*1424dfb3Schristos 
1197*1424dfb3Schristos 	and emit assembly code that refers to S - (8 * sizeof (int)),
1198*1424dfb3Schristos 	so the subtraction is done entirely at compile-time.  In this
1199*1424dfb3Schristos 	case, the reloc's addend A would be -(8 * sizeof (int)), and
1200*1424dfb3Schristos 	shifting around code or data at S + A should not affect the
1201*1424dfb3Schristos 	reloc: the reloc isn't referring to that code or data at all.
1202*1424dfb3Schristos 
1203*1424dfb3Schristos      The linker has no way of knowing which case it has in hand.  So,
1204*1424dfb3Schristos      to disambiguate, we have the linker always treat reloc addends as
1205*1424dfb3Schristos      in case 2): they're constants that should be simply added to the
1206*1424dfb3Schristos      symbol value, just like the reloc says.  And we express case 1)
1207*1424dfb3Schristos      in different way: we have the compiler place a label at the real
1208*1424dfb3Schristos      target, and reference that label with an addend of zero.  (The
1209*1424dfb3Schristos      compiler is unlikely to reference code using a label plus an
1210*1424dfb3Schristos      offset anyway, since it doesn't know the sizes of the
1211*1424dfb3Schristos      instructions.)
1212*1424dfb3Schristos 
1213*1424dfb3Schristos      The simplification being done by gas/write.c:adjust_reloc_syms,
1214*1424dfb3Schristos      however, turns the explicit-label usage into the label-plus-
1215*1424dfb3Schristos      offset usage, re-introducing the ambiguity the compiler avoided.
1216*1424dfb3Schristos      So we need to disable that simplification for symbols referring
1217*1424dfb3Schristos      to merged data.
1218*1424dfb3Schristos 
1219*1424dfb3Schristos      This only affects object size a little bit.  */
1220*1424dfb3Schristos   if (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE)
1221*1424dfb3Schristos     return 0;
1222*1424dfb3Schristos 
1223*1424dfb3Schristos   if (m32c_relax)
1224*1424dfb3Schristos     return 0;
1225*1424dfb3Schristos 
1226*1424dfb3Schristos   return 1;
1227*1424dfb3Schristos }
1228*1424dfb3Schristos 
1229*1424dfb3Schristos /* Worker function for m32c_is_colon_insn().  */
1230*1424dfb3Schristos static int
restore_colon(char * next_i_l_p,char * nul_char)1231*1424dfb3Schristos restore_colon (char *next_i_l_p, char *nul_char)
1232*1424dfb3Schristos {
1233*1424dfb3Schristos   /* Restore the colon, and advance input_line_pointer to
1234*1424dfb3Schristos      the end of the new symbol.  */
1235*1424dfb3Schristos   *input_line_pointer = *nul_char;
1236*1424dfb3Schristos   input_line_pointer = next_i_l_p;
1237*1424dfb3Schristos   *nul_char = *next_i_l_p;
1238*1424dfb3Schristos   *next_i_l_p = 0;
1239*1424dfb3Schristos   return 1;
1240*1424dfb3Schristos }
1241*1424dfb3Schristos 
1242*1424dfb3Schristos /* Determines if the symbol starting at START and ending in
1243*1424dfb3Schristos    a colon that was at the location pointed to by INPUT_LINE_POINTER
1244*1424dfb3Schristos    (but which has now been replaced bu a NUL) is in fact an
1245*1424dfb3Schristos    :Z, :S, :Q, or :G suffix.
1246*1424dfb3Schristos    If it is, then it restores the colon, advances INPUT_LINE_POINTER
1247*1424dfb3Schristos    to the real end of the instruction/symbol, saves the char there to
1248*1424dfb3Schristos    NUL_CHAR and pokes a NUL, and returns 1.  Otherwise it returns 0.  */
1249*1424dfb3Schristos int
m32c_is_colon_insn(char * start ATTRIBUTE_UNUSED,char * nul_char)1250*1424dfb3Schristos m32c_is_colon_insn (char *start ATTRIBUTE_UNUSED, char *nul_char)
1251*1424dfb3Schristos {
1252*1424dfb3Schristos   char * i_l_p = input_line_pointer;
1253*1424dfb3Schristos 
1254*1424dfb3Schristos   if (*nul_char == '"')
1255*1424dfb3Schristos     ++i_l_p;
1256*1424dfb3Schristos 
1257*1424dfb3Schristos   /* Check to see if the text following the colon is 'G' */
1258*1424dfb3Schristos   if (TOLOWER (i_l_p[1]) == 'g' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1259*1424dfb3Schristos     return restore_colon (i_l_p + 2, nul_char);
1260*1424dfb3Schristos 
1261*1424dfb3Schristos   /* Check to see if the text following the colon is 'Q' */
1262*1424dfb3Schristos   if (TOLOWER (i_l_p[1]) == 'q' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1263*1424dfb3Schristos     return restore_colon (i_l_p + 2, nul_char);
1264*1424dfb3Schristos 
1265*1424dfb3Schristos   /* Check to see if the text following the colon is 'S' */
1266*1424dfb3Schristos   if (TOLOWER (i_l_p[1]) == 's' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1267*1424dfb3Schristos     return restore_colon (i_l_p + 2, nul_char);
1268*1424dfb3Schristos 
1269*1424dfb3Schristos   /* Check to see if the text following the colon is 'Z' */
1270*1424dfb3Schristos   if (TOLOWER (i_l_p[1]) == 'z' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1271*1424dfb3Schristos     return restore_colon (i_l_p + 2, nul_char);
1272*1424dfb3Schristos 
1273*1424dfb3Schristos   return 0;
1274*1424dfb3Schristos }
1275