1 /* Subroutines used for code generation on Vitesse IQ2000 processors
2    Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC 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 GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "df.h"
28 #include "tm_p.h"
29 #include "optabs.h"
30 #include "regs.h"
31 #include "emit-rtl.h"
32 #include "recog.h"
33 #include "diagnostic-core.h"
34 #include "stor-layout.h"
35 #include "calls.h"
36 #include "varasm.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "explow.h"
40 #include "expr.h"
41 #include "langhooks.h"
42 #include "builtins.h"
43 
44 /* This file should be included last.  */
45 #include "target-def.h"
46 
47 /* Enumeration for all of the relational tests, so that we can build
48    arrays indexed by the test type, and not worry about the order
49    of EQ, NE, etc.  */
50 
51 enum internal_test
52   {
53     ITEST_EQ,
54     ITEST_NE,
55     ITEST_GT,
56     ITEST_GE,
57     ITEST_LT,
58     ITEST_LE,
59     ITEST_GTU,
60     ITEST_GEU,
61     ITEST_LTU,
62     ITEST_LEU,
63     ITEST_MAX
64   };
65 
66 struct constant;
67 
68 
69 /* Structure to be filled in by compute_frame_size with register
70    save masks, and offsets for the current function.  */
71 
72 struct iq2000_frame_info
73 {
74   long total_size;		/* # bytes that the entire frame takes up.  */
75   long var_size;		/* # bytes that variables take up.  */
76   long args_size;		/* # bytes that outgoing arguments take up.  */
77   long extra_size;		/* # bytes of extra gunk.  */
78   int  gp_reg_size;		/* # bytes needed to store gp regs.  */
79   int  fp_reg_size;		/* # bytes needed to store fp regs.  */
80   long mask;			/* Mask of saved gp registers.  */
81   long gp_save_offset;		/* Offset from vfp to store gp registers.  */
82   long fp_save_offset;		/* Offset from vfp to store fp registers.  */
83   long gp_sp_offset;		/* Offset from new sp to store gp registers.  */
84   long fp_sp_offset;		/* Offset from new sp to store fp registers.  */
85   int  initialized;		/* != 0 if frame size already calculated.  */
86   int  num_gp;			/* Number of gp registers saved.  */
87 } iq2000_frame_info;
88 
89 struct GTY(()) machine_function
90 {
91   /* Current frame information, calculated by compute_frame_size.  */
92   long total_size;		/* # bytes that the entire frame takes up.  */
93   long var_size;		/* # bytes that variables take up.  */
94   long args_size;		/* # bytes that outgoing arguments take up.  */
95   long extra_size;		/* # bytes of extra gunk.  */
96   int  gp_reg_size;		/* # bytes needed to store gp regs.  */
97   int  fp_reg_size;		/* # bytes needed to store fp regs.  */
98   long mask;			/* Mask of saved gp registers.  */
99   long gp_save_offset;		/* Offset from vfp to store gp registers.  */
100   long fp_save_offset;		/* Offset from vfp to store fp registers.  */
101   long gp_sp_offset;		/* Offset from new sp to store gp registers.  */
102   long fp_sp_offset;		/* Offset from new sp to store fp registers.  */
103   int  initialized;		/* != 0 if frame size already calculated.  */
104   int  num_gp;			/* Number of gp registers saved.  */
105 };
106 
107 /* Global variables for machine-dependent things.  */
108 
109 /* List of all IQ2000 punctuation characters used by iq2000_print_operand.  */
110 static char iq2000_print_operand_punct[256];
111 
112 /* Which instruction set architecture to use.  */
113 int iq2000_isa;
114 
115 /* Local variables.  */
116 
117 /* The next branch instruction is a branch likely, not branch normal.  */
118 static int iq2000_branch_likely;
119 
120 /* Count of delay slots and how many are filled.  */
121 static int dslots_load_total;
122 static int dslots_load_filled;
123 static int dslots_jump_total;
124 
125 /* # of nops needed by previous insn.  */
126 static int dslots_number_nops;
127 
128 /* Number of 1/2/3 word references to data items (i.e., not jal's).  */
129 static int num_refs[3];
130 
131 /* Registers to check for load delay.  */
132 static rtx iq2000_load_reg;
133 static rtx iq2000_load_reg2;
134 static rtx iq2000_load_reg3;
135 static rtx iq2000_load_reg4;
136 
137 /* Mode used for saving/restoring general purpose registers.  */
138 static machine_mode gpr_mode;
139 
140 
141 /* Initialize the GCC target structure.  */
142 static struct machine_function* iq2000_init_machine_status (void);
143 static void iq2000_option_override    (void);
144 static section *iq2000_select_rtx_section (machine_mode, rtx,
145 					   unsigned HOST_WIDE_INT);
146 static void iq2000_init_builtins      (void);
147 static rtx  iq2000_expand_builtin     (tree, rtx, rtx, machine_mode, int);
148 static bool iq2000_return_in_memory   (const_tree, const_tree);
149 static void iq2000_setup_incoming_varargs (cumulative_args_t,
150 					   machine_mode, tree, int *,
151 					   int);
152 static bool iq2000_rtx_costs          (rtx, machine_mode, int, int, int *, bool);
153 static int  iq2000_address_cost       (rtx, machine_mode, addr_space_t,
154 				       bool);
155 static section *iq2000_select_section (tree, int, unsigned HOST_WIDE_INT);
156 static rtx  iq2000_legitimize_address (rtx, rtx, machine_mode);
157 static bool iq2000_pass_by_reference  (cumulative_args_t, machine_mode,
158 				       const_tree, bool);
159 static int  iq2000_arg_partial_bytes  (cumulative_args_t, machine_mode,
160 				       tree, bool);
161 static rtx iq2000_function_arg	      (cumulative_args_t,
162 				       machine_mode, const_tree, bool);
163 static void iq2000_function_arg_advance (cumulative_args_t,
164 					 machine_mode, const_tree, bool);
165 static unsigned int iq2000_function_arg_boundary (machine_mode,
166 						  const_tree);
167 static void iq2000_va_start	      (tree, rtx);
168 static bool iq2000_legitimate_address_p (machine_mode, rtx, bool);
169 static bool iq2000_can_eliminate      (const int, const int);
170 static void iq2000_asm_trampoline_template (FILE *);
171 static void iq2000_trampoline_init    (rtx, tree, rtx);
172 static rtx iq2000_function_value      (const_tree, const_tree, bool);
173 static rtx iq2000_libcall_value       (machine_mode, const_rtx);
174 static void iq2000_print_operand      (FILE *, rtx, int);
175 static void iq2000_print_operand_address (FILE *, machine_mode, rtx);
176 static bool iq2000_print_operand_punct_valid_p (unsigned char code);
177 
178 #undef  TARGET_INIT_BUILTINS
179 #define TARGET_INIT_BUILTINS 		iq2000_init_builtins
180 #undef  TARGET_EXPAND_BUILTIN
181 #define TARGET_EXPAND_BUILTIN 		iq2000_expand_builtin
182 #undef  TARGET_ASM_SELECT_RTX_SECTION
183 #define TARGET_ASM_SELECT_RTX_SECTION	iq2000_select_rtx_section
184 #undef  TARGET_OPTION_OVERRIDE
185 #define TARGET_OPTION_OVERRIDE		iq2000_option_override
186 #undef  TARGET_RTX_COSTS
187 #define TARGET_RTX_COSTS		iq2000_rtx_costs
188 #undef  TARGET_ADDRESS_COST
189 #define TARGET_ADDRESS_COST		iq2000_address_cost
190 #undef  TARGET_ASM_SELECT_SECTION
191 #define TARGET_ASM_SELECT_SECTION	iq2000_select_section
192 
193 #undef TARGET_LEGITIMIZE_ADDRESS
194 #define TARGET_LEGITIMIZE_ADDRESS	iq2000_legitimize_address
195 
196 /* The assembler supports switchable .bss sections, but
197    iq2000_select_section doesn't yet make use of them.  */
198 #undef  TARGET_HAVE_SWITCHABLE_BSS_SECTIONS
199 #define TARGET_HAVE_SWITCHABLE_BSS_SECTIONS false
200 
201 #undef  TARGET_PRINT_OPERAND
202 #define TARGET_PRINT_OPERAND		iq2000_print_operand
203 #undef  TARGET_PRINT_OPERAND_ADDRESS
204 #define TARGET_PRINT_OPERAND_ADDRESS	iq2000_print_operand_address
205 #undef  TARGET_PRINT_OPERAND_PUNCT_VALID_P
206 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P iq2000_print_operand_punct_valid_p
207 
208 #undef  TARGET_PROMOTE_FUNCTION_MODE
209 #define TARGET_PROMOTE_FUNCTION_MODE	default_promote_function_mode_always_promote
210 #undef  TARGET_PROMOTE_PROTOTYPES
211 #define TARGET_PROMOTE_PROTOTYPES	hook_bool_const_tree_true
212 
213 #undef TARGET_FUNCTION_VALUE
214 #define TARGET_FUNCTION_VALUE 		iq2000_function_value
215 #undef TARGET_LIBCALL_VALUE
216 #define TARGET_LIBCALL_VALUE		iq2000_libcall_value
217 #undef  TARGET_RETURN_IN_MEMORY
218 #define TARGET_RETURN_IN_MEMORY		iq2000_return_in_memory
219 #undef  TARGET_PASS_BY_REFERENCE
220 #define TARGET_PASS_BY_REFERENCE	iq2000_pass_by_reference
221 #undef  TARGET_CALLEE_COPIES
222 #define TARGET_CALLEE_COPIES		hook_callee_copies_named
223 #undef  TARGET_ARG_PARTIAL_BYTES
224 #define TARGET_ARG_PARTIAL_BYTES	iq2000_arg_partial_bytes
225 #undef  TARGET_FUNCTION_ARG
226 #define TARGET_FUNCTION_ARG		iq2000_function_arg
227 #undef  TARGET_FUNCTION_ARG_ADVANCE
228 #define TARGET_FUNCTION_ARG_ADVANCE	iq2000_function_arg_advance
229 #undef  TARGET_FUNCTION_ARG_BOUNDARY
230 #define TARGET_FUNCTION_ARG_BOUNDARY	iq2000_function_arg_boundary
231 
232 #undef  TARGET_SETUP_INCOMING_VARARGS
233 #define TARGET_SETUP_INCOMING_VARARGS	iq2000_setup_incoming_varargs
234 #undef  TARGET_STRICT_ARGUMENT_NAMING
235 #define TARGET_STRICT_ARGUMENT_NAMING	hook_bool_CUMULATIVE_ARGS_true
236 
237 #undef	TARGET_EXPAND_BUILTIN_VA_START
238 #define	TARGET_EXPAND_BUILTIN_VA_START	iq2000_va_start
239 
240 #undef TARGET_LEGITIMATE_ADDRESS_P
241 #define TARGET_LEGITIMATE_ADDRESS_P	iq2000_legitimate_address_p
242 
243 #undef TARGET_CAN_ELIMINATE
244 #define TARGET_CAN_ELIMINATE            iq2000_can_eliminate
245 
246 #undef  TARGET_ASM_TRAMPOLINE_TEMPLATE
247 #define TARGET_ASM_TRAMPOLINE_TEMPLATE	iq2000_asm_trampoline_template
248 #undef  TARGET_TRAMPOLINE_INIT
249 #define TARGET_TRAMPOLINE_INIT		iq2000_trampoline_init
250 
251 struct gcc_target targetm = TARGET_INITIALIZER;
252 
253 /* Return nonzero if we split the address into high and low parts.  */
254 
255 int
iq2000_check_split(rtx address,machine_mode mode)256 iq2000_check_split (rtx address, machine_mode mode)
257 {
258   /* This is the same check used in simple_memory_operand.
259      We use it here because LO_SUM is not offsettable.  */
260   if (GET_MODE_SIZE (mode) > (unsigned) UNITS_PER_WORD)
261     return 0;
262 
263   if ((GET_CODE (address) == SYMBOL_REF)
264       || (GET_CODE (address) == CONST
265 	  && GET_CODE (XEXP (XEXP (address, 0), 0)) == SYMBOL_REF)
266       || GET_CODE (address) == LABEL_REF)
267     return 1;
268 
269   return 0;
270 }
271 
272 /* Return nonzero if REG is valid for MODE.  */
273 
274 int
iq2000_reg_mode_ok_for_base_p(rtx reg,machine_mode mode ATTRIBUTE_UNUSED,int strict)275 iq2000_reg_mode_ok_for_base_p (rtx reg,
276 			       machine_mode mode ATTRIBUTE_UNUSED,
277 			       int strict)
278 {
279   return (strict
280 	  ? REGNO_MODE_OK_FOR_BASE_P (REGNO (reg), mode)
281 	  : GP_REG_OR_PSEUDO_NONSTRICT_P (REGNO (reg), mode));
282 }
283 
284 /* Return a nonzero value if XINSN is a legitimate address for a
285    memory operand of the indicated MODE.  STRICT is nonzero if this
286    function is called during reload.  */
287 
288 bool
iq2000_legitimate_address_p(machine_mode mode,rtx xinsn,bool strict)289 iq2000_legitimate_address_p (machine_mode mode, rtx xinsn, bool strict)
290 {
291   if (TARGET_DEBUG_A_MODE)
292     {
293       GO_PRINTF2 ("\n========== legitimate_address_p, %sstrict\n",
294 		  strict ? "" : "not ");
295       GO_DEBUG_RTX (xinsn);
296     }
297 
298   /* Check for constant before stripping off SUBREG, so that we don't
299      accept (subreg (const_int)) which will fail to reload.  */
300   if (CONSTANT_ADDRESS_P (xinsn)
301       && ! (iq2000_check_split (xinsn, mode))
302       && ! (GET_CODE (xinsn) == CONST_INT && ! SMALL_INT (xinsn)))
303     return 1;
304 
305   while (GET_CODE (xinsn) == SUBREG)
306     xinsn = SUBREG_REG (xinsn);
307 
308   if (GET_CODE (xinsn) == REG
309       && iq2000_reg_mode_ok_for_base_p (xinsn, mode, strict))
310     return 1;
311 
312   if (GET_CODE (xinsn) == LO_SUM)
313     {
314       rtx xlow0 = XEXP (xinsn, 0);
315       rtx xlow1 = XEXP (xinsn, 1);
316 
317       while (GET_CODE (xlow0) == SUBREG)
318 	xlow0 = SUBREG_REG (xlow0);
319       if (GET_CODE (xlow0) == REG
320 	  && iq2000_reg_mode_ok_for_base_p (xlow0, mode, strict)
321 	  && iq2000_check_split (xlow1, mode))
322 	return 1;
323     }
324 
325   if (GET_CODE (xinsn) == PLUS)
326     {
327       rtx xplus0 = XEXP (xinsn, 0);
328       rtx xplus1 = XEXP (xinsn, 1);
329       enum rtx_code code0;
330       enum rtx_code code1;
331 
332       while (GET_CODE (xplus0) == SUBREG)
333 	xplus0 = SUBREG_REG (xplus0);
334       code0 = GET_CODE (xplus0);
335 
336       while (GET_CODE (xplus1) == SUBREG)
337 	xplus1 = SUBREG_REG (xplus1);
338       code1 = GET_CODE (xplus1);
339 
340       if (code0 == REG
341 	  && iq2000_reg_mode_ok_for_base_p (xplus0, mode, strict))
342 	{
343 	  if (code1 == CONST_INT && SMALL_INT (xplus1)
344 	      && SMALL_INT_UNSIGNED (xplus1) /* No negative offsets */)
345 	    return 1;
346 	}
347     }
348 
349   if (TARGET_DEBUG_A_MODE)
350     GO_PRINTF ("Not a machine_mode mode, legitimate address\n");
351 
352   /* The address was not legitimate.  */
353   return 0;
354 }
355 
356 /* Returns an operand string for the given instruction's delay slot,
357    after updating filled delay slot statistics.
358 
359    We assume that operands[0] is the target register that is set.
360 
361    In order to check the next insn, most of this functionality is moved
362    to FINAL_PRESCAN_INSN, and we just set the global variables that
363    it needs.  */
364 
365 const char *
iq2000_fill_delay_slot(const char * ret,enum delay_type type,rtx operands[],rtx_insn * cur_insn)366 iq2000_fill_delay_slot (const char *ret, enum delay_type type, rtx operands[],
367 			rtx_insn *cur_insn)
368 {
369   rtx set_reg;
370   machine_mode mode;
371   rtx_insn *next_insn = cur_insn ? NEXT_INSN (cur_insn) : NULL;
372   int num_nops;
373 
374   if (type == DELAY_LOAD || type == DELAY_FCMP)
375     num_nops = 1;
376 
377   else
378     num_nops = 0;
379 
380   /* Make sure that we don't put nop's after labels.  */
381   next_insn = NEXT_INSN (cur_insn);
382   while (next_insn != 0
383 	 && (NOTE_P (next_insn) || LABEL_P (next_insn)))
384     next_insn = NEXT_INSN (next_insn);
385 
386   dslots_load_total += num_nops;
387   if (TARGET_DEBUG_C_MODE
388       || type == DELAY_NONE
389       || operands == 0
390       || cur_insn == 0
391       || next_insn == 0
392       || LABEL_P (next_insn)
393       || (set_reg = operands[0]) == 0)
394     {
395       dslots_number_nops = 0;
396       iq2000_load_reg  = 0;
397       iq2000_load_reg2 = 0;
398       iq2000_load_reg3 = 0;
399       iq2000_load_reg4 = 0;
400 
401       return ret;
402     }
403 
404   set_reg = operands[0];
405   if (set_reg == 0)
406     return ret;
407 
408   while (GET_CODE (set_reg) == SUBREG)
409     set_reg = SUBREG_REG (set_reg);
410 
411   mode = GET_MODE (set_reg);
412   dslots_number_nops = num_nops;
413   iq2000_load_reg = set_reg;
414   if (GET_MODE_SIZE (mode)
415       > (unsigned) (UNITS_PER_WORD))
416     iq2000_load_reg2 = gen_rtx_REG (SImode, REGNO (set_reg) + 1);
417   else
418     iq2000_load_reg2 = 0;
419 
420   return ret;
421 }
422 
423 /* Determine whether a memory reference takes one (based off of the GP
424    pointer), two (normal), or three (label + reg) instructions, and bump the
425    appropriate counter for -mstats.  */
426 
427 static void
iq2000_count_memory_refs(rtx op,int num)428 iq2000_count_memory_refs (rtx op, int num)
429 {
430   int additional = 0;
431   int n_words = 0;
432   rtx addr, plus0, plus1;
433   enum rtx_code code0, code1;
434   int looping;
435 
436   if (TARGET_DEBUG_B_MODE)
437     {
438       fprintf (stderr, "\n========== iq2000_count_memory_refs:\n");
439       debug_rtx (op);
440     }
441 
442   /* Skip MEM if passed, otherwise handle movsi of address.  */
443   addr = (GET_CODE (op) != MEM) ? op : XEXP (op, 0);
444 
445   /* Loop, going through the address RTL.  */
446   do
447     {
448       looping = FALSE;
449       switch (GET_CODE (addr))
450 	{
451 	case REG:
452 	case CONST_INT:
453 	case LO_SUM:
454 	  break;
455 
456 	case PLUS:
457 	  plus0 = XEXP (addr, 0);
458 	  plus1 = XEXP (addr, 1);
459 	  code0 = GET_CODE (plus0);
460 	  code1 = GET_CODE (plus1);
461 
462 	  if (code0 == REG)
463 	    {
464 	      additional++;
465 	      addr = plus1;
466 	      looping = 1;
467 	      continue;
468 	    }
469 
470 	  if (code0 == CONST_INT)
471 	    {
472 	      addr = plus1;
473 	      looping = 1;
474 	      continue;
475 	    }
476 
477 	  if (code1 == REG)
478 	    {
479 	      additional++;
480 	      addr = plus0;
481 	      looping = 1;
482 	      continue;
483 	    }
484 
485 	  if (code1 == CONST_INT)
486 	    {
487 	      addr = plus0;
488 	      looping = 1;
489 	      continue;
490 	    }
491 
492 	  if (code0 == SYMBOL_REF || code0 == LABEL_REF || code0 == CONST)
493 	    {
494 	      addr = plus0;
495 	      looping = 1;
496 	      continue;
497 	    }
498 
499 	  if (code1 == SYMBOL_REF || code1 == LABEL_REF || code1 == CONST)
500 	    {
501 	      addr = plus1;
502 	      looping = 1;
503 	      continue;
504 	    }
505 
506 	  break;
507 
508 	case LABEL_REF:
509 	  n_words = 2;		/* Always 2 words.  */
510 	  break;
511 
512 	case CONST:
513 	  addr = XEXP (addr, 0);
514 	  looping = 1;
515 	  continue;
516 
517 	case SYMBOL_REF:
518 	  n_words = SYMBOL_REF_FLAG (addr) ? 1 : 2;
519 	  break;
520 
521 	default:
522 	  break;
523 	}
524     }
525   while (looping);
526 
527   if (n_words == 0)
528     return;
529 
530   n_words += additional;
531   if (n_words > 3)
532     n_words = 3;
533 
534   num_refs[n_words-1] += num;
535 }
536 
537 /* Abort after printing out a specific insn.  */
538 
539 static void
abort_with_insn(rtx insn,const char * reason)540 abort_with_insn (rtx insn, const char * reason)
541 {
542   error (reason);
543   debug_rtx (insn);
544   fancy_abort (__FILE__, __LINE__, __FUNCTION__);
545 }
546 
547 /* Return the appropriate instructions to move one operand to another.  */
548 
549 const char *
iq2000_move_1word(rtx operands[],rtx_insn * insn,int unsignedp)550 iq2000_move_1word (rtx operands[], rtx_insn *insn, int unsignedp)
551 {
552   const char *ret = 0;
553   rtx op0 = operands[0];
554   rtx op1 = operands[1];
555   enum rtx_code code0 = GET_CODE (op0);
556   enum rtx_code code1 = GET_CODE (op1);
557   machine_mode mode = GET_MODE (op0);
558   int subreg_offset0 = 0;
559   int subreg_offset1 = 0;
560   enum delay_type delay = DELAY_NONE;
561 
562   while (code0 == SUBREG)
563     {
564       subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
565 					     GET_MODE (SUBREG_REG (op0)),
566 					     SUBREG_BYTE (op0),
567 					     GET_MODE (op0));
568       op0 = SUBREG_REG (op0);
569       code0 = GET_CODE (op0);
570     }
571 
572   while (code1 == SUBREG)
573     {
574       subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
575 					     GET_MODE (SUBREG_REG (op1)),
576 					     SUBREG_BYTE (op1),
577 					     GET_MODE (op1));
578       op1 = SUBREG_REG (op1);
579       code1 = GET_CODE (op1);
580     }
581 
582   /* For our purposes, a condition code mode is the same as SImode.  */
583   if (mode == CCmode)
584     mode = SImode;
585 
586   if (code0 == REG)
587     {
588       int regno0 = REGNO (op0) + subreg_offset0;
589 
590       if (code1 == REG)
591 	{
592 	  int regno1 = REGNO (op1) + subreg_offset1;
593 
594 	  /* Do not do anything for assigning a register to itself */
595 	  if (regno0 == regno1)
596 	    ret = "";
597 
598 	  else if (GP_REG_P (regno0))
599 	    {
600 	      if (GP_REG_P (regno1))
601 		ret = "or\t%0,%%0,%1";
602 	    }
603 
604 	}
605 
606       else if (code1 == MEM)
607 	{
608 	  delay = DELAY_LOAD;
609 
610 	  if (TARGET_STATS)
611 	    iq2000_count_memory_refs (op1, 1);
612 
613 	  if (GP_REG_P (regno0))
614 	    {
615 	      /* For loads, use the mode of the memory item, instead of the
616 		 target, so zero/sign extend can use this code as well.  */
617 	      switch (GET_MODE (op1))
618 		{
619 		default:
620 		  break;
621 		case SFmode:
622 		  ret = "lw\t%0,%1";
623 		  break;
624 		case SImode:
625 		case CCmode:
626 		  ret = "lw\t%0,%1";
627 		  break;
628 		case HImode:
629 		  ret = (unsignedp) ? "lhu\t%0,%1" : "lh\t%0,%1";
630 		  break;
631 		case QImode:
632 		  ret = (unsignedp) ? "lbu\t%0,%1" : "lb\t%0,%1";
633 		  break;
634 		}
635 	    }
636 	}
637 
638       else if (code1 == CONST_INT
639 	       || (code1 == CONST_DOUBLE
640 		   && GET_MODE (op1) == VOIDmode))
641 	{
642 	  if (code1 == CONST_DOUBLE)
643 	    {
644 	      /* This can happen when storing constants into long long
645                  bitfields.  Just store the least significant word of
646                  the value.  */
647 	      operands[1] = op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
648 	    }
649 
650 	  if (INTVAL (op1) == 0)
651 	    {
652 	      if (GP_REG_P (regno0))
653 		ret = "or\t%0,%%0,%z1";
654 	    }
655 	 else if (GP_REG_P (regno0))
656 	    {
657 	      if (SMALL_INT_UNSIGNED (op1))
658 		ret = "ori\t%0,%%0,%x1\t\t\t# %1";
659 	      else if (SMALL_INT (op1))
660 		ret = "addiu\t%0,%%0,%1\t\t\t# %1";
661 	      else
662 		ret = "lui\t%0,%X1\t\t\t# %1\n\tori\t%0,%0,%x1";
663 	    }
664 	}
665 
666       else if (code1 == CONST_DOUBLE && mode == SFmode)
667 	{
668 	  if (op1 == CONST0_RTX (SFmode))
669 	    {
670 	      if (GP_REG_P (regno0))
671 		ret = "or\t%0,%%0,%.";
672 	    }
673 
674 	  else
675 	    {
676 	      delay = DELAY_LOAD;
677 	      ret = "li.s\t%0,%1";
678 	    }
679 	}
680 
681       else if (code1 == LABEL_REF)
682 	{
683 	  if (TARGET_STATS)
684 	    iq2000_count_memory_refs (op1, 1);
685 
686 	  ret = "la\t%0,%a1";
687 	}
688 
689       else if (code1 == SYMBOL_REF || code1 == CONST)
690 	{
691 	  if (TARGET_STATS)
692 	    iq2000_count_memory_refs (op1, 1);
693 
694 	  ret = "la\t%0,%a1";
695 	}
696 
697       else if (code1 == PLUS)
698 	{
699 	  rtx add_op0 = XEXP (op1, 0);
700 	  rtx add_op1 = XEXP (op1, 1);
701 
702 	  if (GET_CODE (XEXP (op1, 1)) == REG
703 	      && GET_CODE (XEXP (op1, 0)) == CONST_INT)
704 	    add_op0 = XEXP (op1, 1), add_op1 = XEXP (op1, 0);
705 
706 	  operands[2] = add_op0;
707 	  operands[3] = add_op1;
708 	  ret = "add%:\t%0,%2,%3";
709 	}
710 
711       else if (code1 == HIGH)
712 	{
713 	  operands[1] = XEXP (op1, 0);
714 	  ret = "lui\t%0,%%hi(%1)";
715 	}
716     }
717 
718   else if (code0 == MEM)
719     {
720       if (TARGET_STATS)
721 	iq2000_count_memory_refs (op0, 1);
722 
723       if (code1 == REG)
724 	{
725 	  int regno1 = REGNO (op1) + subreg_offset1;
726 
727 	  if (GP_REG_P (regno1))
728 	    {
729 	      switch (mode)
730 		{
731 		case SFmode: ret = "sw\t%1,%0"; break;
732 		case SImode: ret = "sw\t%1,%0"; break;
733 		case HImode: ret = "sh\t%1,%0"; break;
734 		case QImode: ret = "sb\t%1,%0"; break;
735 		default: break;
736 		}
737 	    }
738 	}
739 
740       else if (code1 == CONST_INT && INTVAL (op1) == 0)
741 	{
742 	  switch (mode)
743 	    {
744 	    case SFmode: ret = "sw\t%z1,%0"; break;
745 	    case SImode: ret = "sw\t%z1,%0"; break;
746 	    case HImode: ret = "sh\t%z1,%0"; break;
747 	    case QImode: ret = "sb\t%z1,%0"; break;
748 	    default: break;
749 	    }
750 	}
751 
752       else if (code1 == CONST_DOUBLE && op1 == CONST0_RTX (mode))
753 	{
754 	  switch (mode)
755 	    {
756 	    case SFmode: ret = "sw\t%.,%0"; break;
757 	    case SImode: ret = "sw\t%.,%0"; break;
758 	    case HImode: ret = "sh\t%.,%0"; break;
759 	    case QImode: ret = "sb\t%.,%0"; break;
760 	    default: break;
761 	    }
762 	}
763     }
764 
765   if (ret == 0)
766     {
767       abort_with_insn (insn, "Bad move");
768       return 0;
769     }
770 
771   if (delay != DELAY_NONE)
772     return iq2000_fill_delay_slot (ret, delay, operands, insn);
773 
774   return ret;
775 }
776 
777 /* Provide the costs of an addressing mode that contains ADDR.  */
778 
779 static int
iq2000_address_cost(rtx addr,machine_mode mode,addr_space_t as,bool speed)780 iq2000_address_cost (rtx addr, machine_mode mode, addr_space_t as,
781 		     bool speed)
782 {
783   switch (GET_CODE (addr))
784     {
785     case LO_SUM:
786       return 1;
787 
788     case LABEL_REF:
789       return 2;
790 
791     case CONST:
792       {
793 	rtx offset = const0_rtx;
794 
795 	addr = eliminate_constant_term (XEXP (addr, 0), & offset);
796 	if (GET_CODE (addr) == LABEL_REF)
797 	  return 2;
798 
799 	if (GET_CODE (addr) != SYMBOL_REF)
800 	  return 4;
801 
802 	if (! SMALL_INT (offset))
803 	  return 2;
804       }
805 
806       /* Fall through.  */
807 
808     case SYMBOL_REF:
809       return SYMBOL_REF_FLAG (addr) ? 1 : 2;
810 
811     case PLUS:
812       {
813 	rtx plus0 = XEXP (addr, 0);
814 	rtx plus1 = XEXP (addr, 1);
815 
816 	if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
817 	  plus0 = XEXP (addr, 1), plus1 = XEXP (addr, 0);
818 
819 	if (GET_CODE (plus0) != REG)
820 	  break;
821 
822 	switch (GET_CODE (plus1))
823 	  {
824 	  case CONST_INT:
825 	    return SMALL_INT (plus1) ? 1 : 2;
826 
827 	  case CONST:
828 	  case SYMBOL_REF:
829 	  case LABEL_REF:
830 	  case HIGH:
831 	  case LO_SUM:
832 	    return iq2000_address_cost (plus1, mode, as, speed) + 1;
833 
834 	  default:
835 	    break;
836 	  }
837       }
838 
839     default:
840       break;
841     }
842 
843   return 4;
844 }
845 
846 /* Make normal rtx_code into something we can index from an array.  */
847 
848 static enum internal_test
map_test_to_internal_test(enum rtx_code test_code)849 map_test_to_internal_test (enum rtx_code test_code)
850 {
851   enum internal_test test = ITEST_MAX;
852 
853   switch (test_code)
854     {
855     case EQ:  test = ITEST_EQ;  break;
856     case NE:  test = ITEST_NE;  break;
857     case GT:  test = ITEST_GT;  break;
858     case GE:  test = ITEST_GE;  break;
859     case LT:  test = ITEST_LT;  break;
860     case LE:  test = ITEST_LE;  break;
861     case GTU: test = ITEST_GTU; break;
862     case GEU: test = ITEST_GEU; break;
863     case LTU: test = ITEST_LTU; break;
864     case LEU: test = ITEST_LEU; break;
865     default:			break;
866     }
867 
868   return test;
869 }
870 
871 /* Generate the code to do a TEST_CODE comparison on two integer values CMP0
872    and CMP1.  P_INVERT is NULL or ptr if branch needs to reverse its test.
873    The return value RESULT is:
874    (reg:SI xx)		The pseudo register the comparison is in
875    0		       	No register, generate a simple branch.  */
876 
877 rtx
gen_int_relational(enum rtx_code test_code,rtx result,rtx cmp0,rtx cmp1,int * p_invert)878 gen_int_relational (enum rtx_code test_code, rtx result, rtx cmp0, rtx cmp1,
879 		    int *p_invert)
880 {
881   struct cmp_info
882   {
883     enum rtx_code test_code;	/* Code to use in instruction (LT vs. LTU).  */
884     int const_low;		/* Low bound of constant we can accept.  */
885     int const_high;		/* High bound of constant we can accept.  */
886     int const_add;		/* Constant to add (convert LE -> LT).  */
887     int reverse_regs;		/* Reverse registers in test.  */
888     int invert_const;		/* != 0 if invert value if cmp1 is constant.  */
889     int invert_reg;		/* != 0 if invert value if cmp1 is register.  */
890     int unsignedp;		/* != 0 for unsigned comparisons.  */
891   };
892 
893   static struct cmp_info info[ (int)ITEST_MAX ] =
894   {
895     { XOR,	 0,  65535,  0,	 0,  0,	 0, 0 },	/* EQ  */
896     { XOR,	 0,  65535,  0,	 0,  1,	 1, 0 },	/* NE  */
897     { LT,   -32769,  32766,  1,	 1,  1,	 0, 0 },	/* GT  */
898     { LT,   -32768,  32767,  0,	 0,  1,	 1, 0 },	/* GE  */
899     { LT,   -32768,  32767,  0,	 0,  0,	 0, 0 },	/* LT  */
900     { LT,   -32769,  32766,  1,	 1,  0,	 1, 0 },	/* LE  */
901     { LTU,  -32769,  32766,  1,	 1,  1,	 0, 1 },	/* GTU */
902     { LTU,  -32768,  32767,  0,	 0,  1,	 1, 1 },	/* GEU */
903     { LTU,  -32768,  32767,  0,	 0,  0,	 0, 1 },	/* LTU */
904     { LTU,  -32769,  32766,  1,	 1,  0,	 1, 1 },	/* LEU */
905   };
906 
907   enum internal_test test;
908   machine_mode mode;
909   struct cmp_info *p_info;
910   int branch_p;
911   int eqne_p;
912   int invert;
913   rtx reg;
914   rtx reg2;
915 
916   test = map_test_to_internal_test (test_code);
917   gcc_assert (test != ITEST_MAX);
918 
919   p_info = &info[(int) test];
920   eqne_p = (p_info->test_code == XOR);
921 
922   mode = GET_MODE (cmp0);
923   if (mode == VOIDmode)
924     mode = GET_MODE (cmp1);
925 
926   /* Eliminate simple branches.  */
927   branch_p = (result == 0);
928   if (branch_p)
929     {
930       if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
931 	{
932 	  /* Comparisons against zero are simple branches.  */
933 	  if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
934 	    return 0;
935 
936 	  /* Test for beq/bne.  */
937 	  if (eqne_p)
938 	    return 0;
939 	}
940 
941       /* Allocate a pseudo to calculate the value in.  */
942       result = gen_reg_rtx (mode);
943     }
944 
945   /* Make sure we can handle any constants given to us.  */
946   if (GET_CODE (cmp0) == CONST_INT)
947     cmp0 = force_reg (mode, cmp0);
948 
949   if (GET_CODE (cmp1) == CONST_INT)
950     {
951       HOST_WIDE_INT value = INTVAL (cmp1);
952 
953       if (value < p_info->const_low
954 	  || value > p_info->const_high)
955 	cmp1 = force_reg (mode, cmp1);
956     }
957 
958   /* See if we need to invert the result.  */
959   invert = (GET_CODE (cmp1) == CONST_INT
960 	    ? p_info->invert_const : p_info->invert_reg);
961 
962   if (p_invert != (int *)0)
963     {
964       *p_invert = invert;
965       invert = 0;
966     }
967 
968   /* Comparison to constants, may involve adding 1 to change a LT into LE.
969      Comparison between two registers, may involve switching operands.  */
970   if (GET_CODE (cmp1) == CONST_INT)
971     {
972       if (p_info->const_add != 0)
973 	{
974 	  HOST_WIDE_INT new_const = INTVAL (cmp1) + p_info->const_add;
975 
976 	  /* If modification of cmp1 caused overflow,
977 	     we would get the wrong answer if we follow the usual path;
978 	     thus, x > 0xffffffffU would turn into x > 0U.  */
979 	  if ((p_info->unsignedp
980 	       ? (unsigned HOST_WIDE_INT) new_const >
981 	       (unsigned HOST_WIDE_INT) INTVAL (cmp1)
982 	       : new_const > INTVAL (cmp1))
983 	      != (p_info->const_add > 0))
984 	    {
985 	      /* This test is always true, but if INVERT is true then
986 		 the result of the test needs to be inverted so 0 should
987 		 be returned instead.  */
988 	      emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
989 	      return result;
990 	    }
991 	  else
992 	    cmp1 = GEN_INT (new_const);
993 	}
994     }
995 
996   else if (p_info->reverse_regs)
997     {
998       rtx temp = cmp0;
999       cmp0 = cmp1;
1000       cmp1 = temp;
1001     }
1002 
1003   if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
1004     reg = cmp0;
1005   else
1006     {
1007       reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
1008       convert_move (reg, gen_rtx_fmt_ee (p_info->test_code, mode, cmp0, cmp1), 0);
1009     }
1010 
1011   if (test == ITEST_NE)
1012     {
1013       convert_move (result, gen_rtx_GTU (mode, reg, const0_rtx), 0);
1014       if (p_invert != NULL)
1015 	*p_invert = 0;
1016       invert = 0;
1017     }
1018 
1019   else if (test == ITEST_EQ)
1020     {
1021       reg2 = invert ? gen_reg_rtx (mode) : result;
1022       convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
1023       reg = reg2;
1024     }
1025 
1026   if (invert)
1027     {
1028       rtx one;
1029 
1030       one = const1_rtx;
1031       convert_move (result, gen_rtx_XOR (mode, reg, one), 0);
1032     }
1033 
1034   return result;
1035 }
1036 
1037 /* Emit the common code for doing conditional branches.
1038    operand[0] is the label to jump to.
1039    The comparison operands are saved away by cmp{si,di,sf,df}.  */
1040 
1041 void
gen_conditional_branch(rtx operands[],machine_mode mode)1042 gen_conditional_branch (rtx operands[], machine_mode mode)
1043 {
1044   enum rtx_code test_code = GET_CODE (operands[0]);
1045   rtx cmp0 = operands[1];
1046   rtx cmp1 = operands[2];
1047   rtx reg;
1048   int invert;
1049   rtx label1, label2;
1050 
1051   invert = 0;
1052   reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
1053 
1054   if (reg)
1055     {
1056       cmp0 = reg;
1057       cmp1 = const0_rtx;
1058       test_code = NE;
1059     }
1060   else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
1061     /* We don't want to build a comparison against a nonzero
1062        constant.  */
1063     cmp1 = force_reg (mode, cmp1);
1064 
1065   /* Generate the branch.  */
1066   label1 = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
1067   label2 = pc_rtx;
1068 
1069   if (invert)
1070     {
1071       label2 = label1;
1072       label1 = pc_rtx;
1073     }
1074 
1075   emit_jump_insn (gen_rtx_SET (pc_rtx,
1076 			       gen_rtx_IF_THEN_ELSE (VOIDmode,
1077 						     gen_rtx_fmt_ee (test_code,
1078 								     VOIDmode,
1079 								     cmp0, cmp1),
1080 						     label1, label2)));
1081 }
1082 
1083 /* Initialize CUM for a function FNTYPE.  */
1084 
1085 void
init_cumulative_args(CUMULATIVE_ARGS * cum,tree fntype,rtx libname ATTRIBUTE_UNUSED)1086 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
1087 		      rtx libname ATTRIBUTE_UNUSED)
1088 {
1089   static CUMULATIVE_ARGS zero_cum;
1090   tree param;
1091   tree next_param;
1092 
1093   if (TARGET_DEBUG_D_MODE)
1094     {
1095       fprintf (stderr,
1096 	       "\ninit_cumulative_args, fntype = 0x%.8lx", (long) fntype);
1097 
1098       if (!fntype)
1099 	fputc ('\n', stderr);
1100 
1101       else
1102 	{
1103 	  tree ret_type = TREE_TYPE (fntype);
1104 
1105 	  fprintf (stderr, ", fntype code = %s, ret code = %s\n",
1106 		   get_tree_code_name (TREE_CODE (fntype)),
1107 		   get_tree_code_name (TREE_CODE (ret_type)));
1108 	}
1109     }
1110 
1111   *cum = zero_cum;
1112 
1113   /* Determine if this function has variable arguments.  This is
1114      indicated by the last argument being 'void_type_mode' if there
1115      are no variable arguments.  The standard IQ2000 calling sequence
1116      passes all arguments in the general purpose registers in this case.  */
1117 
1118   for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
1119        param != 0; param = next_param)
1120     {
1121       next_param = TREE_CHAIN (param);
1122       if (next_param == 0 && TREE_VALUE (param) != void_type_node)
1123 	cum->gp_reg_found = 1;
1124     }
1125 }
1126 
1127 /* Advance the argument of type TYPE and mode MODE to the next argument
1128    position in CUM.  */
1129 
1130 static void
iq2000_function_arg_advance(cumulative_args_t cum_v,machine_mode mode,const_tree type,bool named)1131 iq2000_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
1132 			     const_tree type, bool named)
1133 {
1134   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1135 
1136   if (TARGET_DEBUG_D_MODE)
1137     {
1138       fprintf (stderr,
1139 	       "function_adv({gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
1140 	       cum->gp_reg_found, cum->arg_number, cum->arg_words,
1141 	       GET_MODE_NAME (mode));
1142       fprintf (stderr, "%p", (const void *) type);
1143       fprintf (stderr, ", %d )\n\n", named);
1144     }
1145 
1146   cum->arg_number++;
1147   switch (mode)
1148     {
1149     case VOIDmode:
1150       break;
1151 
1152     default:
1153       gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
1154 		  || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
1155 
1156       cum->gp_reg_found = 1;
1157       cum->arg_words += ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1)
1158 			 / UNITS_PER_WORD);
1159       break;
1160 
1161     case BLKmode:
1162       cum->gp_reg_found = 1;
1163       cum->arg_words += ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
1164 			 / UNITS_PER_WORD);
1165       break;
1166 
1167     case SFmode:
1168       cum->arg_words ++;
1169       if (! cum->gp_reg_found && cum->arg_number <= 2)
1170 	cum->fp_code += 1 << ((cum->arg_number - 1) * 2);
1171       break;
1172 
1173     case DFmode:
1174       cum->arg_words += 2;
1175       if (! cum->gp_reg_found && cum->arg_number <= 2)
1176 	cum->fp_code += 2 << ((cum->arg_number - 1) * 2);
1177       break;
1178 
1179     case DImode:
1180       cum->gp_reg_found = 1;
1181       cum->arg_words += 2;
1182       break;
1183 
1184     case TImode:
1185       cum->gp_reg_found = 1;
1186       cum->arg_words += 4;
1187       break;
1188 
1189     case QImode:
1190     case HImode:
1191     case SImode:
1192       cum->gp_reg_found = 1;
1193       cum->arg_words ++;
1194       break;
1195     }
1196 }
1197 
1198 /* Return an RTL expression containing the register for the given mode MODE
1199    and type TYPE in CUM, or 0 if the argument is to be passed on the stack.  */
1200 
1201 static rtx
iq2000_function_arg(cumulative_args_t cum_v,machine_mode mode,const_tree type,bool named)1202 iq2000_function_arg (cumulative_args_t cum_v, machine_mode mode,
1203 		     const_tree type, bool named)
1204 {
1205   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1206   rtx ret;
1207   int regbase = -1;
1208   int bias = 0;
1209   unsigned int *arg_words = &cum->arg_words;
1210   int struct_p = (type != 0
1211 		  && (TREE_CODE (type) == RECORD_TYPE
1212 		      || TREE_CODE (type) == UNION_TYPE
1213 		      || TREE_CODE (type) == QUAL_UNION_TYPE));
1214 
1215   if (TARGET_DEBUG_D_MODE)
1216     {
1217       fprintf (stderr,
1218 	       "function_arg( {gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
1219 	       cum->gp_reg_found, cum->arg_number, cum->arg_words,
1220 	       GET_MODE_NAME (mode));
1221       fprintf (stderr, "%p", (const void *) type);
1222       fprintf (stderr, ", %d ) = ", named);
1223     }
1224 
1225 
1226   cum->last_arg_fp = 0;
1227   switch (mode)
1228     {
1229     case SFmode:
1230       regbase = GP_ARG_FIRST;
1231       break;
1232 
1233     case DFmode:
1234       cum->arg_words += cum->arg_words & 1;
1235 
1236       regbase = GP_ARG_FIRST;
1237       break;
1238 
1239     default:
1240       gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
1241 		  || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
1242 
1243       /* Drops through.  */
1244     case BLKmode:
1245       if (type != NULL_TREE && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
1246 	cum->arg_words += (cum->arg_words & 1);
1247       regbase = GP_ARG_FIRST;
1248       break;
1249 
1250     case VOIDmode:
1251     case QImode:
1252     case HImode:
1253     case SImode:
1254       regbase = GP_ARG_FIRST;
1255       break;
1256 
1257     case DImode:
1258       cum->arg_words += (cum->arg_words & 1);
1259       regbase = GP_ARG_FIRST;
1260       break;
1261 
1262     case TImode:
1263       cum->arg_words += (cum->arg_words & 3);
1264       regbase = GP_ARG_FIRST;
1265       break;
1266     }
1267 
1268   if (*arg_words >= (unsigned) MAX_ARGS_IN_REGISTERS)
1269     {
1270       if (TARGET_DEBUG_D_MODE)
1271 	fprintf (stderr, "<stack>%s\n", struct_p ? ", [struct]" : "");
1272 
1273       ret = 0;
1274     }
1275   else
1276     {
1277       gcc_assert (regbase != -1);
1278 
1279       if (! type || TREE_CODE (type) != RECORD_TYPE
1280 	  || ! named  || ! TYPE_SIZE_UNIT (type)
1281 	  || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1282 	ret = gen_rtx_REG (mode, regbase + *arg_words + bias);
1283       else
1284 	{
1285 	  tree field;
1286 
1287 	  for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1288 	    if (TREE_CODE (field) == FIELD_DECL
1289 		&& TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
1290 		&& TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
1291 		&& tree_fits_shwi_p (bit_position (field))
1292 		&& int_bit_position (field) % BITS_PER_WORD == 0)
1293 	      break;
1294 
1295 	  /* If the whole struct fits a DFmode register,
1296 	     we don't need the PARALLEL.  */
1297 	  if (! field || mode == DFmode)
1298 	    ret = gen_rtx_REG (mode, regbase + *arg_words + bias);
1299 	  else
1300 	    {
1301 	      unsigned int chunks;
1302 	      HOST_WIDE_INT bitpos;
1303 	      unsigned int regno;
1304 	      unsigned int i;
1305 
1306 	      /* ??? If this is a packed structure, then the last hunk won't
1307 		 be 64 bits.  */
1308 	      chunks
1309 		= tree_to_uhwi (TYPE_SIZE_UNIT (type)) / UNITS_PER_WORD;
1310 	      if (chunks + *arg_words + bias > (unsigned) MAX_ARGS_IN_REGISTERS)
1311 		chunks = MAX_ARGS_IN_REGISTERS - *arg_words - bias;
1312 
1313 	      /* Assign_parms checks the mode of ENTRY_PARM, so we must
1314 		 use the actual mode here.  */
1315 	      ret = gen_rtx_PARALLEL (mode, rtvec_alloc (chunks));
1316 
1317 	      bitpos = 0;
1318 	      regno = regbase + *arg_words + bias;
1319 	      field = TYPE_FIELDS (type);
1320 	      for (i = 0; i < chunks; i++)
1321 		{
1322 		  rtx reg;
1323 
1324 		  for (; field; field = DECL_CHAIN (field))
1325 		    if (TREE_CODE (field) == FIELD_DECL
1326 			&& int_bit_position (field) >= bitpos)
1327 		      break;
1328 
1329 		  if (field
1330 		      && int_bit_position (field) == bitpos
1331 		      && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
1332 		      && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
1333 		    reg = gen_rtx_REG (DFmode, regno++);
1334 		  else
1335 		    reg = gen_rtx_REG (word_mode, regno);
1336 
1337 		  XVECEXP (ret, 0, i)
1338 		    = gen_rtx_EXPR_LIST (VOIDmode, reg,
1339 					 GEN_INT (bitpos / BITS_PER_UNIT));
1340 
1341 		  bitpos += 64;
1342 		  regno++;
1343 		}
1344 	    }
1345 	}
1346 
1347       if (TARGET_DEBUG_D_MODE)
1348 	fprintf (stderr, "%s%s\n", reg_names[regbase + *arg_words + bias],
1349 		 struct_p ? ", [struct]" : "");
1350     }
1351 
1352   /* We will be called with a mode of VOIDmode after the last argument
1353      has been seen.  Whatever we return will be passed to the call
1354      insn.  If we need any shifts for small structures, return them in
1355      a PARALLEL.  */
1356   if (mode == VOIDmode)
1357     {
1358       if (cum->num_adjusts > 0)
1359 	ret = gen_rtx_PARALLEL ((machine_mode) cum->fp_code,
1360 		       gen_rtvec_v (cum->num_adjusts, cum->adjust));
1361     }
1362 
1363   return ret;
1364 }
1365 
1366 static unsigned int
iq2000_function_arg_boundary(machine_mode mode,const_tree type)1367 iq2000_function_arg_boundary (machine_mode mode, const_tree type)
1368 {
1369   return (type != NULL_TREE
1370 	  ? (TYPE_ALIGN (type) <= PARM_BOUNDARY
1371 	     ? PARM_BOUNDARY
1372 	     : TYPE_ALIGN (type))
1373 	  : (GET_MODE_ALIGNMENT (mode) <= PARM_BOUNDARY
1374 	     ? PARM_BOUNDARY
1375 	     : GET_MODE_ALIGNMENT (mode)));
1376 }
1377 
1378 static int
iq2000_arg_partial_bytes(cumulative_args_t cum_v,machine_mode mode,tree type ATTRIBUTE_UNUSED,bool named ATTRIBUTE_UNUSED)1379 iq2000_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
1380 			  tree type ATTRIBUTE_UNUSED,
1381 			  bool named ATTRIBUTE_UNUSED)
1382 {
1383   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1384 
1385   if (mode == DImode && cum->arg_words == MAX_ARGS_IN_REGISTERS - 1)
1386     {
1387       if (TARGET_DEBUG_D_MODE)
1388 	fprintf (stderr, "iq2000_arg_partial_bytes=%d\n", UNITS_PER_WORD);
1389       return UNITS_PER_WORD;
1390     }
1391 
1392   return 0;
1393 }
1394 
1395 /* Implement va_start.  */
1396 
1397 static void
iq2000_va_start(tree valist,rtx nextarg)1398 iq2000_va_start (tree valist, rtx nextarg)
1399 {
1400   int int_arg_words;
1401   /* Find out how many non-float named formals.  */
1402   int gpr_save_area_size;
1403   /* Note UNITS_PER_WORD is 4 bytes.  */
1404   int_arg_words = crtl->args.info.arg_words;
1405 
1406   if (int_arg_words < 8 )
1407     /* Adjust for the prologue's economy measure.  */
1408     gpr_save_area_size = (8 - int_arg_words) * UNITS_PER_WORD;
1409   else
1410     gpr_save_area_size = 0;
1411 
1412   /* Everything is in the GPR save area, or in the overflow
1413      area which is contiguous with it.  */
1414   nextarg = plus_constant (Pmode, nextarg, - gpr_save_area_size);
1415   std_expand_builtin_va_start (valist, nextarg);
1416 }
1417 
1418 /* Allocate a chunk of memory for per-function machine-dependent data.  */
1419 
1420 static struct machine_function *
iq2000_init_machine_status(void)1421 iq2000_init_machine_status (void)
1422 {
1423   return ggc_cleared_alloc<machine_function> ();
1424 }
1425 
1426 /* Detect any conflicts in the switches.  */
1427 
1428 static void
iq2000_option_override(void)1429 iq2000_option_override (void)
1430 {
1431   target_flags &= ~MASK_GPOPT;
1432 
1433   iq2000_isa = IQ2000_ISA_DEFAULT;
1434 
1435   /* Identify the processor type.  */
1436 
1437   iq2000_print_operand_punct['?'] = 1;
1438   iq2000_print_operand_punct['#'] = 1;
1439   iq2000_print_operand_punct['&'] = 1;
1440   iq2000_print_operand_punct['!'] = 1;
1441   iq2000_print_operand_punct['*'] = 1;
1442   iq2000_print_operand_punct['@'] = 1;
1443   iq2000_print_operand_punct['.'] = 1;
1444   iq2000_print_operand_punct['('] = 1;
1445   iq2000_print_operand_punct[')'] = 1;
1446   iq2000_print_operand_punct['['] = 1;
1447   iq2000_print_operand_punct[']'] = 1;
1448   iq2000_print_operand_punct['<'] = 1;
1449   iq2000_print_operand_punct['>'] = 1;
1450   iq2000_print_operand_punct['{'] = 1;
1451   iq2000_print_operand_punct['}'] = 1;
1452   iq2000_print_operand_punct['^'] = 1;
1453   iq2000_print_operand_punct['$'] = 1;
1454   iq2000_print_operand_punct['+'] = 1;
1455   iq2000_print_operand_punct['~'] = 1;
1456 
1457   /* Save GPR registers in word_mode sized hunks.  word_mode hasn't been
1458      initialized yet, so we can't use that here.  */
1459   gpr_mode = SImode;
1460 
1461   /* Function to allocate machine-dependent function status.  */
1462   init_machine_status = iq2000_init_machine_status;
1463 }
1464 
1465 /* The arg pointer (which is eliminated) points to the virtual frame pointer,
1466    while the frame pointer (which may be eliminated) points to the stack
1467    pointer after the initial adjustments.  */
1468 
1469 HOST_WIDE_INT
iq2000_debugger_offset(rtx addr,HOST_WIDE_INT offset)1470 iq2000_debugger_offset (rtx addr, HOST_WIDE_INT offset)
1471 {
1472   rtx offset2 = const0_rtx;
1473   rtx reg = eliminate_constant_term (addr, & offset2);
1474 
1475   if (offset == 0)
1476     offset = INTVAL (offset2);
1477 
1478   if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
1479       || reg == hard_frame_pointer_rtx)
1480     {
1481       HOST_WIDE_INT frame_size = (!cfun->machine->initialized)
1482 				  ? compute_frame_size (get_frame_size ())
1483 				  : cfun->machine->total_size;
1484 
1485       offset = offset - frame_size;
1486     }
1487 
1488   return offset;
1489 }
1490 
1491 /* If defined, a C statement to be executed just prior to the output of
1492    assembler code for INSN, to modify the extracted operands so they will be
1493    output differently.
1494 
1495    Here the argument OPVEC is the vector containing the operands extracted
1496    from INSN, and NOPERANDS is the number of elements of the vector which
1497    contain meaningful data for this insn.  The contents of this vector are
1498    what will be used to convert the insn template into assembler code, so you
1499    can change the assembler output by changing the contents of the vector.
1500 
1501    We use it to check if the current insn needs a nop in front of it because
1502    of load delays, and also to update the delay slot statistics.  */
1503 
1504 void
final_prescan_insn(rtx_insn * insn,rtx opvec[]ATTRIBUTE_UNUSED,int noperands ATTRIBUTE_UNUSED)1505 final_prescan_insn (rtx_insn *insn, rtx opvec[] ATTRIBUTE_UNUSED,
1506 		    int noperands ATTRIBUTE_UNUSED)
1507 {
1508   if (dslots_number_nops > 0)
1509     {
1510       rtx pattern = PATTERN (insn);
1511       int length = get_attr_length (insn);
1512 
1513       /* Do we need to emit a NOP?  */
1514       if (length == 0
1515 	  || (iq2000_load_reg != 0 && reg_mentioned_p (iq2000_load_reg,  pattern))
1516 	  || (iq2000_load_reg2 != 0 && reg_mentioned_p (iq2000_load_reg2, pattern))
1517 	  || (iq2000_load_reg3 != 0 && reg_mentioned_p (iq2000_load_reg3, pattern))
1518 	  || (iq2000_load_reg4 != 0
1519 	      && reg_mentioned_p (iq2000_load_reg4, pattern)))
1520 	fputs ("\tnop\n", asm_out_file);
1521 
1522       else
1523 	dslots_load_filled ++;
1524 
1525       while (--dslots_number_nops > 0)
1526 	fputs ("\tnop\n", asm_out_file);
1527 
1528       iq2000_load_reg = 0;
1529       iq2000_load_reg2 = 0;
1530       iq2000_load_reg3 = 0;
1531       iq2000_load_reg4 = 0;
1532     }
1533 
1534   if (   (JUMP_P (insn)
1535        || CALL_P (insn)
1536        || (GET_CODE (PATTERN (insn)) == RETURN))
1537 	   && NEXT_INSN (PREV_INSN (insn)) == insn)
1538     {
1539       rtx_insn *nop_insn = emit_insn_after (gen_nop (), insn);
1540 
1541       INSN_ADDRESSES_NEW (nop_insn, -1);
1542     }
1543 
1544   if (TARGET_STATS
1545       && (JUMP_P (insn) || CALL_P (insn)))
1546     dslots_jump_total ++;
1547 }
1548 
1549 /* Return the bytes needed to compute the frame pointer from the current
1550    stack pointer where SIZE is the # of var. bytes allocated.
1551 
1552    IQ2000 stack frames look like:
1553 
1554              Before call		        After call
1555         +-----------------------+	+-----------------------+
1556    high |			|       |      			|
1557    mem. |		        |	|			|
1558         |  caller's temps.    	|       |  caller's temps.    	|
1559 	|       		|       |       	        |
1560         +-----------------------+	+-----------------------+
1561  	|       		|	|		        |
1562         |  arguments on stack.  |	|  arguments on stack.  |
1563 	|       		|	|			|
1564         +-----------------------+	+-----------------------+
1565  	|  4 words to save     	|	|  4 words to save	|
1566 	|  arguments passed	|	|  arguments passed	|
1567 	|  in registers, even	|	|  in registers, even	|
1568     SP->|  if not passed.       |  VFP->|  if not passed.	|
1569 	+-----------------------+       +-----------------------+
1570 					|		        |
1571                                         |  fp register save     |
1572 					|			|
1573 					+-----------------------+
1574 					|		        |
1575                                         |  gp register save     |
1576                                         |       		|
1577 					+-----------------------+
1578 					|			|
1579 					|  local variables	|
1580 					|			|
1581 					+-----------------------+
1582 					|			|
1583                                         |  alloca allocations   |
1584         				|			|
1585 					+-----------------------+
1586 					|			|
1587 					|  GP save for V.4 abi	|
1588 					|			|
1589 					+-----------------------+
1590 					|			|
1591                                         |  arguments on stack   |
1592         				|		        |
1593 					+-----------------------+
1594                                         |  4 words to save      |
1595 					|  arguments passed     |
1596                                         |  in registers, even   |
1597    low                              SP->|  if not passed.       |
1598    memory        			+-----------------------+  */
1599 
1600 HOST_WIDE_INT
compute_frame_size(HOST_WIDE_INT size)1601 compute_frame_size (HOST_WIDE_INT size)
1602 {
1603   int regno;
1604   HOST_WIDE_INT total_size;	/* # bytes that the entire frame takes up.  */
1605   HOST_WIDE_INT var_size;	/* # bytes that variables take up.  */
1606   HOST_WIDE_INT args_size;	/* # bytes that outgoing arguments take up.  */
1607   HOST_WIDE_INT extra_size;	/* # extra bytes.  */
1608   HOST_WIDE_INT gp_reg_rounded;	/* # bytes needed to store gp after rounding.  */
1609   HOST_WIDE_INT gp_reg_size;	/* # bytes needed to store gp regs.  */
1610   HOST_WIDE_INT fp_reg_size;	/* # bytes needed to store fp regs.  */
1611   long mask;			/* mask of saved gp registers.  */
1612 
1613   gp_reg_size = 0;
1614   fp_reg_size = 0;
1615   mask = 0;
1616   extra_size = IQ2000_STACK_ALIGN ((0));
1617   var_size = IQ2000_STACK_ALIGN (size);
1618   args_size = IQ2000_STACK_ALIGN (crtl->outgoing_args_size);
1619 
1620   /* If a function dynamically allocates the stack and
1621      has 0 for STACK_DYNAMIC_OFFSET then allocate some stack space.  */
1622   if (args_size == 0 && cfun->calls_alloca)
1623     args_size = 4 * UNITS_PER_WORD;
1624 
1625   total_size = var_size + args_size + extra_size;
1626 
1627   /* Calculate space needed for gp registers.  */
1628   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
1629     {
1630       if (MUST_SAVE_REGISTER (regno))
1631 	{
1632 	  gp_reg_size += GET_MODE_SIZE (gpr_mode);
1633 	  mask |= 1L << (regno - GP_REG_FIRST);
1634 	}
1635     }
1636 
1637   /* We need to restore these for the handler.  */
1638   if (crtl->calls_eh_return)
1639     {
1640       unsigned int i;
1641 
1642       for (i = 0; ; ++i)
1643 	{
1644 	  regno = EH_RETURN_DATA_REGNO (i);
1645 	  if (regno == (int) INVALID_REGNUM)
1646 	    break;
1647 	  gp_reg_size += GET_MODE_SIZE (gpr_mode);
1648 	  mask |= 1L << (regno - GP_REG_FIRST);
1649 	}
1650     }
1651 
1652   gp_reg_rounded = IQ2000_STACK_ALIGN (gp_reg_size);
1653   total_size += gp_reg_rounded + IQ2000_STACK_ALIGN (fp_reg_size);
1654 
1655   /* The gp reg is caller saved, so there is no need for leaf routines
1656      (total_size == extra_size) to save the gp reg.  */
1657   if (total_size == extra_size
1658       && ! profile_flag)
1659     total_size = extra_size = 0;
1660 
1661   total_size += IQ2000_STACK_ALIGN (crtl->args.pretend_args_size);
1662 
1663   /* Save other computed information.  */
1664   cfun->machine->total_size = total_size;
1665   cfun->machine->var_size = var_size;
1666   cfun->machine->args_size = args_size;
1667   cfun->machine->extra_size = extra_size;
1668   cfun->machine->gp_reg_size = gp_reg_size;
1669   cfun->machine->fp_reg_size = fp_reg_size;
1670   cfun->machine->mask = mask;
1671   cfun->machine->initialized = reload_completed;
1672   cfun->machine->num_gp = gp_reg_size / UNITS_PER_WORD;
1673 
1674   if (mask)
1675     {
1676       unsigned long offset;
1677 
1678       offset = (args_size + extra_size + var_size
1679 		+ gp_reg_size - GET_MODE_SIZE (gpr_mode));
1680 
1681       cfun->machine->gp_sp_offset = offset;
1682       cfun->machine->gp_save_offset = offset - total_size;
1683     }
1684   else
1685     {
1686       cfun->machine->gp_sp_offset = 0;
1687       cfun->machine->gp_save_offset = 0;
1688     }
1689 
1690   cfun->machine->fp_sp_offset = 0;
1691   cfun->machine->fp_save_offset = 0;
1692 
1693   /* Ok, we're done.  */
1694   return total_size;
1695 }
1696 
1697 
1698 /* We can always eliminate to the frame pointer.  We can eliminate to the
1699    stack pointer unless a frame pointer is needed.  */
1700 
1701 bool
iq2000_can_eliminate(const int from,const int to)1702 iq2000_can_eliminate (const int from, const int to)
1703 {
1704   return (from == RETURN_ADDRESS_POINTER_REGNUM
1705           && (! leaf_function_p ()
1706               || (to == GP_REG_FIRST + 31 && leaf_function_p ())))
1707           || (from != RETURN_ADDRESS_POINTER_REGNUM
1708               && (to == HARD_FRAME_POINTER_REGNUM
1709                   || (to == STACK_POINTER_REGNUM
1710                       && ! frame_pointer_needed)));
1711 }
1712 
1713 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame
1714    pointer, argument pointer, or return address pointer.  TO is either
1715    the stack pointer or hard frame pointer.  */
1716 
1717 int
iq2000_initial_elimination_offset(int from,int to ATTRIBUTE_UNUSED)1718 iq2000_initial_elimination_offset (int from, int to ATTRIBUTE_UNUSED)
1719 {
1720   int offset;
1721 
1722   compute_frame_size (get_frame_size ());
1723   if ((from) == FRAME_POINTER_REGNUM)
1724     (offset) = 0;
1725   else if ((from) == ARG_POINTER_REGNUM)
1726     (offset) = (cfun->machine->total_size);
1727   else if ((from) == RETURN_ADDRESS_POINTER_REGNUM)
1728     {
1729       if (leaf_function_p ())
1730 	(offset) = 0;
1731       else (offset) = cfun->machine->gp_sp_offset
1732 	     + ((UNITS_PER_WORD - (POINTER_SIZE / BITS_PER_UNIT))
1733 		* (BYTES_BIG_ENDIAN != 0));
1734     }
1735   else
1736     gcc_unreachable ();
1737 
1738   return offset;
1739 }
1740 
1741 /* Common code to emit the insns (or to write the instructions to a file)
1742    to save/restore registers.
1743    Other parts of the code assume that IQ2000_TEMP1_REGNUM (aka large_reg)
1744    is not modified within save_restore_insns.  */
1745 
1746 #define BITSET_P(VALUE,BIT) (((VALUE) & (1L << (BIT))) != 0)
1747 
1748 /* Emit instructions to load the value (SP + OFFSET) into IQ2000_TEMP2_REGNUM
1749    and return an rtl expression for the register.  Write the assembly
1750    instructions directly to FILE if it is not null, otherwise emit them as
1751    rtl.
1752 
1753    This function is a subroutine of save_restore_insns.  It is used when
1754    OFFSET is too large to add in a single instruction.  */
1755 
1756 static rtx
iq2000_add_large_offset_to_sp(HOST_WIDE_INT offset)1757 iq2000_add_large_offset_to_sp (HOST_WIDE_INT offset)
1758 {
1759   rtx reg = gen_rtx_REG (Pmode, IQ2000_TEMP2_REGNUM);
1760   rtx offset_rtx = GEN_INT (offset);
1761 
1762   emit_move_insn (reg, offset_rtx);
1763   emit_insn (gen_addsi3 (reg, reg, stack_pointer_rtx));
1764   return reg;
1765 }
1766 
1767 /* Make INSN frame related and note that it performs the frame-related
1768    operation DWARF_PATTERN.  */
1769 
1770 static void
iq2000_annotate_frame_insn(rtx_insn * insn,rtx dwarf_pattern)1771 iq2000_annotate_frame_insn (rtx_insn *insn, rtx dwarf_pattern)
1772 {
1773   RTX_FRAME_RELATED_P (insn) = 1;
1774   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
1775 				      dwarf_pattern,
1776 				      REG_NOTES (insn));
1777 }
1778 
1779 /* Emit a move instruction that stores REG in MEM.  Make the instruction
1780    frame related and note that it stores REG at (SP + OFFSET).  */
1781 
1782 static void
iq2000_emit_frame_related_store(rtx mem,rtx reg,HOST_WIDE_INT offset)1783 iq2000_emit_frame_related_store (rtx mem, rtx reg, HOST_WIDE_INT offset)
1784 {
1785   rtx dwarf_address = plus_constant (Pmode, stack_pointer_rtx, offset);
1786   rtx dwarf_mem = gen_rtx_MEM (GET_MODE (reg), dwarf_address);
1787 
1788   iq2000_annotate_frame_insn (emit_move_insn (mem, reg),
1789 			      gen_rtx_SET (dwarf_mem, reg));
1790 }
1791 
1792 /* Emit instructions to save/restore registers, as determined by STORE_P.  */
1793 
1794 static void
save_restore_insns(int store_p)1795 save_restore_insns (int store_p)
1796 {
1797   long mask = cfun->machine->mask;
1798   int regno;
1799   rtx base_reg_rtx;
1800   HOST_WIDE_INT base_offset;
1801   HOST_WIDE_INT gp_offset;
1802   HOST_WIDE_INT end_offset;
1803 
1804   gcc_assert (!frame_pointer_needed
1805 	      || BITSET_P (mask, HARD_FRAME_POINTER_REGNUM - GP_REG_FIRST));
1806 
1807   if (mask == 0)
1808     {
1809       base_reg_rtx = 0, base_offset  = 0;
1810       return;
1811     }
1812 
1813   /* Save registers starting from high to low.  The debuggers prefer at least
1814      the return register be stored at func+4, and also it allows us not to
1815      need a nop in the epilog if at least one register is reloaded in
1816      addition to return address.  */
1817 
1818   /* Save GP registers if needed.  */
1819   /* Pick which pointer to use as a base register.  For small frames, just
1820      use the stack pointer.  Otherwise, use a temporary register.  Save 2
1821      cycles if the save area is near the end of a large frame, by reusing
1822      the constant created in the prologue/epilogue to adjust the stack
1823      frame.  */
1824 
1825   gp_offset = cfun->machine->gp_sp_offset;
1826   end_offset
1827     = gp_offset - (cfun->machine->gp_reg_size
1828 		   - GET_MODE_SIZE (gpr_mode));
1829 
1830   if (gp_offset < 0 || end_offset < 0)
1831     internal_error
1832       ("gp_offset (%ld) or end_offset (%ld) is less than zero",
1833        (long) gp_offset, (long) end_offset);
1834 
1835   else if (gp_offset < 32768)
1836     base_reg_rtx = stack_pointer_rtx, base_offset  = 0;
1837   else
1838     {
1839       int regno;
1840       int reg_save_count = 0;
1841 
1842       for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
1843 	if (BITSET_P (mask, regno - GP_REG_FIRST)) reg_save_count += 1;
1844       base_offset = gp_offset - ((reg_save_count - 1) * 4);
1845       base_reg_rtx = iq2000_add_large_offset_to_sp (base_offset);
1846     }
1847 
1848   for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
1849     {
1850       if (BITSET_P (mask, regno - GP_REG_FIRST))
1851 	{
1852 	  rtx reg_rtx;
1853 	  rtx mem_rtx
1854 	    = gen_rtx_MEM (gpr_mode,
1855 		       gen_rtx_PLUS (Pmode, base_reg_rtx,
1856 				GEN_INT (gp_offset - base_offset)));
1857 
1858 	  reg_rtx = gen_rtx_REG (gpr_mode, regno);
1859 
1860 	  if (store_p)
1861 	    iq2000_emit_frame_related_store (mem_rtx, reg_rtx, gp_offset);
1862 	  else
1863 	    {
1864 	      emit_move_insn (reg_rtx, mem_rtx);
1865 	    }
1866 	  gp_offset -= GET_MODE_SIZE (gpr_mode);
1867 	}
1868     }
1869 }
1870 
1871 /* Expand the prologue into a bunch of separate insns.  */
1872 
1873 void
iq2000_expand_prologue(void)1874 iq2000_expand_prologue (void)
1875 {
1876   int regno;
1877   HOST_WIDE_INT tsize;
1878   int last_arg_is_vararg_marker = 0;
1879   tree fndecl = current_function_decl;
1880   tree fntype = TREE_TYPE (fndecl);
1881   tree fnargs = DECL_ARGUMENTS (fndecl);
1882   rtx next_arg_reg;
1883   int i;
1884   tree next_arg;
1885   tree cur_arg;
1886   CUMULATIVE_ARGS args_so_far_v;
1887   cumulative_args_t args_so_far;
1888   int store_args_on_stack = (iq2000_can_use_return_insn ());
1889 
1890   /* If struct value address is treated as the first argument.  */
1891   if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
1892       && !cfun->returns_pcc_struct
1893       && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
1894     {
1895       tree type = build_pointer_type (fntype);
1896       tree function_result_decl = build_decl (BUILTINS_LOCATION,
1897 					      PARM_DECL, NULL_TREE, type);
1898 
1899       DECL_ARG_TYPE (function_result_decl) = type;
1900       DECL_CHAIN (function_result_decl) = fnargs;
1901       fnargs = function_result_decl;
1902     }
1903 
1904   /* For arguments passed in registers, find the register number
1905      of the first argument in the variable part of the argument list,
1906      otherwise GP_ARG_LAST+1.  Note also if the last argument is
1907      the varargs special argument, and treat it as part of the
1908      variable arguments.
1909 
1910      This is only needed if store_args_on_stack is true.  */
1911   INIT_CUMULATIVE_ARGS (args_so_far_v, fntype, NULL_RTX, 0, 0);
1912   args_so_far = pack_cumulative_args (&args_so_far_v);
1913   regno = GP_ARG_FIRST;
1914 
1915   for (cur_arg = fnargs; cur_arg != 0; cur_arg = next_arg)
1916     {
1917       tree passed_type = DECL_ARG_TYPE (cur_arg);
1918       machine_mode passed_mode = TYPE_MODE (passed_type);
1919       rtx entry_parm;
1920 
1921       if (TREE_ADDRESSABLE (passed_type))
1922 	{
1923 	  passed_type = build_pointer_type (passed_type);
1924 	  passed_mode = Pmode;
1925 	}
1926 
1927       entry_parm = iq2000_function_arg (args_so_far, passed_mode,
1928 					passed_type, true);
1929 
1930       iq2000_function_arg_advance (args_so_far, passed_mode,
1931 				   passed_type, true);
1932       next_arg = DECL_CHAIN (cur_arg);
1933 
1934       if (entry_parm && store_args_on_stack)
1935 	{
1936 	  if (next_arg == 0
1937 	      && DECL_NAME (cur_arg)
1938 	      && ((0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
1939 				"__builtin_va_alist"))
1940 		  || (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
1941 				   "va_alist"))))
1942 	    {
1943 	      last_arg_is_vararg_marker = 1;
1944 	      break;
1945 	    }
1946 	  else
1947 	    {
1948 	      int words;
1949 
1950 	      gcc_assert (GET_CODE (entry_parm) == REG);
1951 
1952 	      /* Passed in a register, so will get homed automatically.  */
1953 	      if (GET_MODE (entry_parm) == BLKmode)
1954 		words = (int_size_in_bytes (passed_type) + 3) / 4;
1955 	      else
1956 		words = (GET_MODE_SIZE (GET_MODE (entry_parm)) + 3) / 4;
1957 
1958 	      regno = REGNO (entry_parm) + words - 1;
1959 	    }
1960 	}
1961       else
1962 	{
1963 	  regno = GP_ARG_LAST+1;
1964 	  break;
1965 	}
1966     }
1967 
1968   /* In order to pass small structures by value in registers we need to
1969      shift the value into the high part of the register.
1970      iq2000_unction_arg has encoded a PARALLEL rtx, holding a vector of
1971      adjustments to be made as the next_arg_reg variable, so we split up
1972      the insns, and emit them separately.  */
1973   next_arg_reg = iq2000_function_arg (args_so_far, VOIDmode,
1974 				      void_type_node, true);
1975   if (next_arg_reg != 0 && GET_CODE (next_arg_reg) == PARALLEL)
1976     {
1977       rtvec adjust = XVEC (next_arg_reg, 0);
1978       int num = GET_NUM_ELEM (adjust);
1979 
1980       for (i = 0; i < num; i++)
1981 	{
1982 	  rtx pattern;
1983 
1984 	  pattern = RTVEC_ELT (adjust, i);
1985 	  if (GET_CODE (pattern) != SET
1986 	      || GET_CODE (SET_SRC (pattern)) != ASHIFT)
1987 	    abort_with_insn (pattern, "Insn is not a shift");
1988 	  PUT_CODE (SET_SRC (pattern), ASHIFTRT);
1989 
1990 	  emit_insn (pattern);
1991 	}
1992     }
1993 
1994   tsize = compute_frame_size (get_frame_size ());
1995 
1996   /* If this function is a varargs function, store any registers that
1997      would normally hold arguments ($4 - $7) on the stack.  */
1998   if (store_args_on_stack
1999       && (stdarg_p (fntype)
2000 	  || last_arg_is_vararg_marker))
2001     {
2002       int offset = (regno - GP_ARG_FIRST) * UNITS_PER_WORD;
2003       rtx ptr = stack_pointer_rtx;
2004 
2005       for (; regno <= GP_ARG_LAST; regno++)
2006 	{
2007 	  if (offset != 0)
2008 	    ptr = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
2009 	  emit_move_insn (gen_rtx_MEM (gpr_mode, ptr),
2010 			  gen_rtx_REG (gpr_mode, regno));
2011 
2012 	  offset += GET_MODE_SIZE (gpr_mode);
2013 	}
2014     }
2015 
2016   if (tsize > 0)
2017     {
2018       rtx tsize_rtx = GEN_INT (tsize);
2019       rtx adjustment_rtx, dwarf_pattern;
2020       rtx_insn *insn;
2021 
2022       if (tsize > 32767)
2023 	{
2024 	  adjustment_rtx = gen_rtx_REG (Pmode, IQ2000_TEMP1_REGNUM);
2025 	  emit_move_insn (adjustment_rtx, tsize_rtx);
2026 	}
2027       else
2028 	adjustment_rtx = tsize_rtx;
2029 
2030       insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
2031 				    adjustment_rtx));
2032 
2033       dwarf_pattern = gen_rtx_SET (stack_pointer_rtx,
2034 				   plus_constant (Pmode, stack_pointer_rtx,
2035 						  -tsize));
2036 
2037       iq2000_annotate_frame_insn (insn, dwarf_pattern);
2038 
2039       save_restore_insns (1);
2040 
2041       if (frame_pointer_needed)
2042 	{
2043 	  rtx_insn *insn = 0;
2044 
2045 	  insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
2046 				       stack_pointer_rtx));
2047 
2048 	  if (insn)
2049 	    RTX_FRAME_RELATED_P (insn) = 1;
2050 	}
2051     }
2052 
2053   if (flag_stack_usage_info)
2054     current_function_static_stack_size = cfun->machine->total_size;
2055 
2056   emit_insn (gen_blockage ());
2057 }
2058 
2059 /* Expand the epilogue into a bunch of separate insns.  */
2060 
2061 void
iq2000_expand_epilogue(void)2062 iq2000_expand_epilogue (void)
2063 {
2064   HOST_WIDE_INT tsize = cfun->machine->total_size;
2065   rtx tsize_rtx = GEN_INT (tsize);
2066   rtx tmp_rtx = (rtx)0;
2067 
2068   if (iq2000_can_use_return_insn ())
2069     {
2070       emit_jump_insn (gen_return ());
2071       return;
2072     }
2073 
2074   if (tsize > 32767)
2075     {
2076       tmp_rtx = gen_rtx_REG (Pmode, IQ2000_TEMP1_REGNUM);
2077       emit_move_insn (tmp_rtx, tsize_rtx);
2078       tsize_rtx = tmp_rtx;
2079     }
2080 
2081   if (tsize > 0)
2082     {
2083       if (frame_pointer_needed)
2084 	{
2085 	  emit_insn (gen_blockage ());
2086 
2087 	  emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
2088 	}
2089 
2090       save_restore_insns (0);
2091 
2092       if (crtl->calls_eh_return)
2093 	{
2094 	  rtx eh_ofs = EH_RETURN_STACKADJ_RTX;
2095 	  emit_insn (gen_addsi3 (eh_ofs, eh_ofs, tsize_rtx));
2096 	  tsize_rtx = eh_ofs;
2097 	}
2098 
2099       emit_insn (gen_blockage ());
2100 
2101       if (tsize != 0 || crtl->calls_eh_return)
2102 	{
2103 	  emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2104 				 tsize_rtx));
2105 	}
2106     }
2107 
2108   if (crtl->calls_eh_return)
2109     {
2110       /* Perform the additional bump for __throw.  */
2111       emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
2112 		      stack_pointer_rtx);
2113       emit_use (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM));
2114       emit_jump_insn (gen_eh_return_internal ());
2115     }
2116   else
2117       emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
2118 						  GP_REG_FIRST + 31)));
2119 }
2120 
2121 void
iq2000_expand_eh_return(rtx address)2122 iq2000_expand_eh_return (rtx address)
2123 {
2124   HOST_WIDE_INT gp_offset = cfun->machine->gp_sp_offset;
2125   rtx scratch;
2126 
2127   scratch = plus_constant (Pmode, stack_pointer_rtx, gp_offset);
2128   emit_move_insn (gen_rtx_MEM (GET_MODE (address), scratch), address);
2129 }
2130 
2131 /* Return nonzero if this function is known to have a null epilogue.
2132    This allows the optimizer to omit jumps to jumps if no stack
2133    was created.  */
2134 
2135 int
iq2000_can_use_return_insn(void)2136 iq2000_can_use_return_insn (void)
2137 {
2138   if (! reload_completed)
2139     return 0;
2140 
2141   if (df_regs_ever_live_p (31) || profile_flag)
2142     return 0;
2143 
2144   if (cfun->machine->initialized)
2145     return cfun->machine->total_size == 0;
2146 
2147   return compute_frame_size (get_frame_size ()) == 0;
2148 }
2149 
2150 /* Choose the section to use for the constant rtx expression X that has
2151    mode MODE.  */
2152 
2153 static section *
iq2000_select_rtx_section(machine_mode mode,rtx x ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT align)2154 iq2000_select_rtx_section (machine_mode mode, rtx x ATTRIBUTE_UNUSED,
2155 			   unsigned HOST_WIDE_INT align)
2156 {
2157   /* For embedded applications, always put constants in read-only data,
2158      in order to reduce RAM usage.  */
2159   return mergeable_constant_section (mode, align, 0);
2160 }
2161 
2162 /* Choose the section to use for DECL.  RELOC is true if its value contains
2163    any relocatable expression.
2164 
2165    Some of the logic used here needs to be replicated in
2166    ENCODE_SECTION_INFO in iq2000.h so that references to these symbols
2167    are done correctly.  */
2168 
2169 static section *
iq2000_select_section(tree decl,int reloc ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)2170 iq2000_select_section (tree decl, int reloc ATTRIBUTE_UNUSED,
2171 		       unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
2172 {
2173   if (TARGET_EMBEDDED_DATA)
2174     {
2175       /* For embedded applications, always put an object in read-only data
2176 	 if possible, in order to reduce RAM usage.  */
2177       if ((TREE_CODE (decl) == VAR_DECL
2178 	   && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
2179 	   && DECL_INITIAL (decl)
2180 	   && (DECL_INITIAL (decl) == error_mark_node
2181 	       || TREE_CONSTANT (DECL_INITIAL (decl))))
2182 	  /* Deal with calls from output_constant_def_contents.  */
2183 	  || TREE_CODE (decl) != VAR_DECL)
2184 	return readonly_data_section;
2185       else
2186 	return data_section;
2187     }
2188   else
2189     {
2190       /* For hosted applications, always put an object in small data if
2191 	 possible, as this gives the best performance.  */
2192       if ((TREE_CODE (decl) == VAR_DECL
2193 	   && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
2194 	   && DECL_INITIAL (decl)
2195 	   && (DECL_INITIAL (decl) == error_mark_node
2196 	       || TREE_CONSTANT (DECL_INITIAL (decl))))
2197 	  /* Deal with calls from output_constant_def_contents.  */
2198 	  || TREE_CODE (decl) != VAR_DECL)
2199 	return readonly_data_section;
2200       else
2201 	return data_section;
2202     }
2203 }
2204 /* Return register to use for a function return value with VALTYPE for function
2205    FUNC.  */
2206 
2207 static rtx
iq2000_function_value(const_tree valtype,const_tree fn_decl_or_type,bool outgoing ATTRIBUTE_UNUSED)2208 iq2000_function_value (const_tree valtype,
2209 		       const_tree fn_decl_or_type,
2210 		       bool outgoing ATTRIBUTE_UNUSED)
2211 {
2212   int reg = GP_RETURN;
2213   machine_mode mode = TYPE_MODE (valtype);
2214   int unsignedp = TYPE_UNSIGNED (valtype);
2215   const_tree func = fn_decl_or_type;
2216 
2217   if (fn_decl_or_type
2218       && !DECL_P (fn_decl_or_type))
2219     fn_decl_or_type = NULL;
2220 
2221   /* Since we promote return types, we must promote the mode here too.  */
2222   mode = promote_function_mode (valtype, mode, &unsignedp, func, 1);
2223 
2224   return gen_rtx_REG (mode, reg);
2225 }
2226 
2227 /* Worker function for TARGET_LIBCALL_VALUE.  */
2228 
2229 static rtx
iq2000_libcall_value(machine_mode mode,const_rtx fun ATTRIBUTE_UNUSED)2230 iq2000_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
2231 {
2232   return gen_rtx_REG (((GET_MODE_CLASS (mode) != MODE_INT
2233 	                || GET_MODE_SIZE (mode) >= 4)
2234 	               ? mode : SImode),
2235 	              GP_RETURN);
2236 }
2237 
2238 /* Worker function for FUNCTION_VALUE_REGNO_P.
2239 
2240    On the IQ2000, R2 and R3 are the only register thus used.  */
2241 
2242 bool
iq2000_function_value_regno_p(const unsigned int regno)2243 iq2000_function_value_regno_p (const unsigned int regno)
2244 {
2245   return (regno == GP_RETURN);
2246 }
2247 
2248 
2249 /* Return true when an argument must be passed by reference.  */
2250 
2251 static bool
iq2000_pass_by_reference(cumulative_args_t cum_v,machine_mode mode,const_tree type,bool named ATTRIBUTE_UNUSED)2252 iq2000_pass_by_reference (cumulative_args_t cum_v, machine_mode mode,
2253 			  const_tree type, bool named ATTRIBUTE_UNUSED)
2254 {
2255   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2256   int size;
2257 
2258   /* We must pass by reference if we would be both passing in registers
2259      and the stack.  This is because any subsequent partial arg would be
2260      handled incorrectly in this case.  */
2261   if (cum && targetm.calls.must_pass_in_stack (mode, type))
2262      {
2263        /* Don't pass the actual CUM to FUNCTION_ARG, because we would
2264 	  get double copies of any offsets generated for small structs
2265 	  passed in registers.  */
2266        CUMULATIVE_ARGS temp;
2267 
2268        temp = *cum;
2269        if (iq2000_function_arg (pack_cumulative_args (&temp), mode, type, named)
2270 	   != 0)
2271 	 return 1;
2272      }
2273 
2274   if (type == NULL_TREE || mode == DImode || mode == DFmode)
2275     return 0;
2276 
2277   size = int_size_in_bytes (type);
2278   return size == -1 || size > UNITS_PER_WORD;
2279 }
2280 
2281 /* Return the length of INSN.  LENGTH is the initial length computed by
2282    attributes in the machine-description file.  */
2283 
2284 int
iq2000_adjust_insn_length(rtx_insn * insn,int length)2285 iq2000_adjust_insn_length (rtx_insn *insn, int length)
2286 {
2287   /* A unconditional jump has an unfilled delay slot if it is not part
2288      of a sequence.  A conditional jump normally has a delay slot.  */
2289   if (simplejump_p (insn)
2290       || (   (JUMP_P (insn)
2291 	   || CALL_P (insn))))
2292     length += 4;
2293 
2294   return length;
2295 }
2296 
2297 /* Output assembly instructions to perform a conditional branch.
2298 
2299    INSN is the branch instruction.  OPERANDS[0] is the condition.
2300    OPERANDS[1] is the target of the branch.  OPERANDS[2] is the target
2301    of the first operand to the condition.  If TWO_OPERANDS_P is
2302    nonzero the comparison takes two operands; OPERANDS[3] will be the
2303    second operand.
2304 
2305    If INVERTED_P is nonzero we are to branch if the condition does
2306    not hold.  If FLOAT_P is nonzero this is a floating-point comparison.
2307 
2308    LENGTH is the length (in bytes) of the sequence we are to generate.
2309    That tells us whether to generate a simple conditional branch, or a
2310    reversed conditional branch around a `jr' instruction.  */
2311 
2312 char *
iq2000_output_conditional_branch(rtx_insn * insn,rtx * operands,int two_operands_p,int float_p,int inverted_p,int length)2313 iq2000_output_conditional_branch (rtx_insn *insn, rtx * operands,
2314 				  int two_operands_p, int float_p,
2315 				  int inverted_p, int length)
2316 {
2317   static char buffer[200];
2318   /* The kind of comparison we are doing.  */
2319   enum rtx_code code = GET_CODE (operands[0]);
2320   /* Nonzero if the opcode for the comparison needs a `z' indicating
2321      that it is a comparison against zero.  */
2322   int need_z_p;
2323   /* A string to use in the assembly output to represent the first
2324      operand.  */
2325   const char *op1 = "%z2";
2326   /* A string to use in the assembly output to represent the second
2327      operand.  Use the hard-wired zero register if there's no second
2328      operand.  */
2329   const char *op2 = (two_operands_p ? ",%z3" : ",%.");
2330   /* The operand-printing string for the comparison.  */
2331   const char *comp = (float_p ? "%F0" : "%C0");
2332   /* The operand-printing string for the inverted comparison.  */
2333   const char *inverted_comp = (float_p ? "%W0" : "%N0");
2334 
2335   /* Likely variants of each branch instruction annul the instruction
2336      in the delay slot if the branch is not taken.  */
2337   iq2000_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
2338 
2339   if (!two_operands_p)
2340     {
2341       /* To compute whether than A > B, for example, we normally
2342 	 subtract B from A and then look at the sign bit.  But, if we
2343 	 are doing an unsigned comparison, and B is zero, we don't
2344 	 have to do the subtraction.  Instead, we can just check to
2345 	 see if A is nonzero.  Thus, we change the CODE here to
2346 	 reflect the simpler comparison operation.  */
2347       switch (code)
2348 	{
2349 	case GTU:
2350 	  code = NE;
2351 	  break;
2352 
2353 	case LEU:
2354 	  code = EQ;
2355 	  break;
2356 
2357 	case GEU:
2358 	  /* A condition which will always be true.  */
2359 	  code = EQ;
2360 	  op1 = "%.";
2361 	  break;
2362 
2363 	case LTU:
2364 	  /* A condition which will always be false.  */
2365 	  code = NE;
2366 	  op1 = "%.";
2367 	  break;
2368 
2369 	default:
2370 	  /* Not a special case.  */
2371 	  break;
2372 	}
2373     }
2374 
2375   /* Relative comparisons are always done against zero.  But
2376      equality comparisons are done between two operands, and therefore
2377      do not require a `z' in the assembly language output.  */
2378   need_z_p = (!float_p && code != EQ && code != NE);
2379   /* For comparisons against zero, the zero is not provided
2380      explicitly.  */
2381   if (need_z_p)
2382     op2 = "";
2383 
2384   /* Begin by terminating the buffer.  That way we can always use
2385      strcat to add to it.  */
2386   buffer[0] = '\0';
2387 
2388   switch (length)
2389     {
2390     case 4:
2391     case 8:
2392       /* Just a simple conditional branch.  */
2393       if (float_p)
2394 	sprintf (buffer, "b%s%%?\t%%Z2%%1",
2395 		 inverted_p ? inverted_comp : comp);
2396       else
2397 	sprintf (buffer, "b%s%s%%?\t%s%s,%%1",
2398 		 inverted_p ? inverted_comp : comp,
2399 		 need_z_p ? "z" : "",
2400 		 op1,
2401 		 op2);
2402       return buffer;
2403 
2404     case 12:
2405     case 16:
2406       {
2407 	/* Generate a reversed conditional branch around ` j'
2408 	   instruction:
2409 
2410 		.set noreorder
2411 		.set nomacro
2412 		bc    l
2413 		nop
2414 		j     target
2415 		.set macro
2416 		.set reorder
2417 	     l:
2418 
2419 	   Because we have to jump four bytes *past* the following
2420 	   instruction if this branch was annulled, we can't just use
2421 	   a label, as in the picture above; there's no way to put the
2422 	   label after the next instruction, as the assembler does not
2423 	   accept `.L+4' as the target of a branch.  (We can't just
2424 	   wait until the next instruction is output; it might be a
2425 	   macro and take up more than four bytes.  Once again, we see
2426 	   why we want to eliminate macros.)
2427 
2428 	   If the branch is annulled, we jump four more bytes that we
2429 	   would otherwise; that way we skip the annulled instruction
2430 	   in the delay slot.  */
2431 
2432 	const char *target
2433 	  = ((iq2000_branch_likely || length == 16) ? ".+16" : ".+12");
2434 	char *c;
2435 
2436 	c = strchr (buffer, '\0');
2437 	/* Generate the reversed comparison.  This takes four
2438 	   bytes.  */
2439 	if (float_p)
2440 	  sprintf (c, "b%s\t%%Z2%s",
2441 		   inverted_p ? comp : inverted_comp,
2442 		   target);
2443 	else
2444 	  sprintf (c, "b%s%s\t%s%s,%s",
2445 		   inverted_p ? comp : inverted_comp,
2446 		   need_z_p ? "z" : "",
2447 		   op1,
2448 		   op2,
2449 		   target);
2450 	strcat (c, "\n\tnop\n\tj\t%1");
2451 	if (length == 16)
2452 	  /* The delay slot was unfilled.  Since we're inside
2453 	     .noreorder, the assembler will not fill in the NOP for
2454 	     us, so we must do it ourselves.  */
2455 	  strcat (buffer, "\n\tnop");
2456 	return buffer;
2457       }
2458 
2459     default:
2460       gcc_unreachable ();
2461     }
2462 
2463   /* NOTREACHED */
2464   return 0;
2465 }
2466 
2467 #define def_builtin(NAME, TYPE, CODE)					\
2468   add_builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,	\
2469 		       NULL, NULL_TREE)
2470 
2471 static void
iq2000_init_builtins(void)2472 iq2000_init_builtins (void)
2473 {
2474   tree void_ftype, void_ftype_int, void_ftype_int_int;
2475   tree void_ftype_int_int_int;
2476   tree int_ftype_int, int_ftype_int_int, int_ftype_int_int_int;
2477   tree int_ftype_int_int_int_int;
2478 
2479   /* func () */
2480   void_ftype
2481     = build_function_type_list (void_type_node, NULL_TREE);
2482 
2483   /* func (int) */
2484   void_ftype_int
2485     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
2486 
2487   /* void func (int, int) */
2488   void_ftype_int_int
2489     = build_function_type_list (void_type_node,
2490                                 integer_type_node,
2491                                 integer_type_node,
2492                                 NULL_TREE);
2493 
2494   /* int func (int) */
2495   int_ftype_int
2496     = build_function_type_list (integer_type_node,
2497                                 integer_type_node, NULL_TREE);
2498 
2499   /* int func (int, int) */
2500   int_ftype_int_int
2501     = build_function_type_list (integer_type_node,
2502                                 integer_type_node,
2503                                 integer_type_node,
2504                                 NULL_TREE);
2505 
2506   /* void func (int, int, int) */
2507   void_ftype_int_int_int
2508     = build_function_type_list (void_type_node,
2509                                 integer_type_node,
2510                                 integer_type_node,
2511                                 integer_type_node,
2512                                 NULL_TREE);
2513 
2514   /* int func (int, int, int) */
2515   int_ftype_int_int_int
2516     = build_function_type_list (integer_type_node,
2517                                 integer_type_node,
2518                                 integer_type_node,
2519                                 integer_type_node,
2520                                 NULL_TREE);
2521 
2522   /* int func (int, int, int, int) */
2523   int_ftype_int_int_int_int
2524     = build_function_type_list (integer_type_node,
2525                                 integer_type_node,
2526                                 integer_type_node,
2527                                 integer_type_node,
2528                                 integer_type_node,
2529                                 NULL_TREE);
2530 
2531   def_builtin ("__builtin_ado16", int_ftype_int_int, IQ2000_BUILTIN_ADO16);
2532   def_builtin ("__builtin_ram", int_ftype_int_int_int_int, IQ2000_BUILTIN_RAM);
2533   def_builtin ("__builtin_chkhdr", void_ftype_int_int, IQ2000_BUILTIN_CHKHDR);
2534   def_builtin ("__builtin_pkrl", void_ftype_int_int, IQ2000_BUILTIN_PKRL);
2535   def_builtin ("__builtin_cfc0", int_ftype_int, IQ2000_BUILTIN_CFC0);
2536   def_builtin ("__builtin_cfc1", int_ftype_int, IQ2000_BUILTIN_CFC1);
2537   def_builtin ("__builtin_cfc2", int_ftype_int, IQ2000_BUILTIN_CFC2);
2538   def_builtin ("__builtin_cfc3", int_ftype_int, IQ2000_BUILTIN_CFC3);
2539   def_builtin ("__builtin_ctc0", void_ftype_int_int, IQ2000_BUILTIN_CTC0);
2540   def_builtin ("__builtin_ctc1", void_ftype_int_int, IQ2000_BUILTIN_CTC1);
2541   def_builtin ("__builtin_ctc2", void_ftype_int_int, IQ2000_BUILTIN_CTC2);
2542   def_builtin ("__builtin_ctc3", void_ftype_int_int, IQ2000_BUILTIN_CTC3);
2543   def_builtin ("__builtin_mfc0", int_ftype_int, IQ2000_BUILTIN_MFC0);
2544   def_builtin ("__builtin_mfc1", int_ftype_int, IQ2000_BUILTIN_MFC1);
2545   def_builtin ("__builtin_mfc2", int_ftype_int, IQ2000_BUILTIN_MFC2);
2546   def_builtin ("__builtin_mfc3", int_ftype_int, IQ2000_BUILTIN_MFC3);
2547   def_builtin ("__builtin_mtc0", void_ftype_int_int, IQ2000_BUILTIN_MTC0);
2548   def_builtin ("__builtin_mtc1", void_ftype_int_int, IQ2000_BUILTIN_MTC1);
2549   def_builtin ("__builtin_mtc2", void_ftype_int_int, IQ2000_BUILTIN_MTC2);
2550   def_builtin ("__builtin_mtc3", void_ftype_int_int, IQ2000_BUILTIN_MTC3);
2551   def_builtin ("__builtin_lur", void_ftype_int_int, IQ2000_BUILTIN_LUR);
2552   def_builtin ("__builtin_rb", void_ftype_int_int, IQ2000_BUILTIN_RB);
2553   def_builtin ("__builtin_rx", void_ftype_int_int, IQ2000_BUILTIN_RX);
2554   def_builtin ("__builtin_srrd", void_ftype_int, IQ2000_BUILTIN_SRRD);
2555   def_builtin ("__builtin_srwr", void_ftype_int_int, IQ2000_BUILTIN_SRWR);
2556   def_builtin ("__builtin_wb", void_ftype_int_int, IQ2000_BUILTIN_WB);
2557   def_builtin ("__builtin_wx", void_ftype_int_int, IQ2000_BUILTIN_WX);
2558   def_builtin ("__builtin_luc32l", void_ftype_int_int, IQ2000_BUILTIN_LUC32L);
2559   def_builtin ("__builtin_luc64", void_ftype_int_int, IQ2000_BUILTIN_LUC64);
2560   def_builtin ("__builtin_luc64l", void_ftype_int_int, IQ2000_BUILTIN_LUC64L);
2561   def_builtin ("__builtin_luk", void_ftype_int_int, IQ2000_BUILTIN_LUK);
2562   def_builtin ("__builtin_lulck", void_ftype_int, IQ2000_BUILTIN_LULCK);
2563   def_builtin ("__builtin_lum32", void_ftype_int_int, IQ2000_BUILTIN_LUM32);
2564   def_builtin ("__builtin_lum32l", void_ftype_int_int, IQ2000_BUILTIN_LUM32L);
2565   def_builtin ("__builtin_lum64", void_ftype_int_int, IQ2000_BUILTIN_LUM64);
2566   def_builtin ("__builtin_lum64l", void_ftype_int_int, IQ2000_BUILTIN_LUM64L);
2567   def_builtin ("__builtin_lurl", void_ftype_int_int, IQ2000_BUILTIN_LURL);
2568   def_builtin ("__builtin_mrgb", int_ftype_int_int_int, IQ2000_BUILTIN_MRGB);
2569   def_builtin ("__builtin_srrdl", void_ftype_int, IQ2000_BUILTIN_SRRDL);
2570   def_builtin ("__builtin_srulck", void_ftype_int, IQ2000_BUILTIN_SRULCK);
2571   def_builtin ("__builtin_srwru", void_ftype_int_int, IQ2000_BUILTIN_SRWRU);
2572   def_builtin ("__builtin_trapqfl", void_ftype, IQ2000_BUILTIN_TRAPQFL);
2573   def_builtin ("__builtin_trapqne", void_ftype, IQ2000_BUILTIN_TRAPQNE);
2574   def_builtin ("__builtin_traprel", void_ftype_int, IQ2000_BUILTIN_TRAPREL);
2575   def_builtin ("__builtin_wbu", void_ftype_int_int_int, IQ2000_BUILTIN_WBU);
2576   def_builtin ("__builtin_syscall", void_ftype, IQ2000_BUILTIN_SYSCALL);
2577 }
2578 
2579 /* Builtin for ICODE having ARGCOUNT args in EXP where each arg
2580    has an rtx CODE.  */
2581 
2582 static rtx
expand_one_builtin(enum insn_code icode,rtx target,tree exp,enum rtx_code * code,int argcount)2583 expand_one_builtin (enum insn_code icode, rtx target, tree exp,
2584 		    enum rtx_code *code, int argcount)
2585 {
2586   rtx pat;
2587   tree arg [5];
2588   rtx op [5];
2589   machine_mode mode [5];
2590   int i;
2591 
2592   mode[0] = insn_data[icode].operand[0].mode;
2593   for (i = 0; i < argcount; i++)
2594     {
2595       arg[i] = CALL_EXPR_ARG (exp, i);
2596       op[i] = expand_normal (arg[i]);
2597       mode[i] = insn_data[icode].operand[i].mode;
2598       if (code[i] == CONST_INT && GET_CODE (op[i]) != CONST_INT)
2599 	error ("argument %qd is not a constant", i + 1);
2600       if (code[i] == REG
2601 	  && ! (*insn_data[icode].operand[i].predicate) (op[i], mode[i]))
2602 	op[i] = copy_to_mode_reg (mode[i], op[i]);
2603     }
2604 
2605   if (insn_data[icode].operand[0].constraint[0] == '=')
2606     {
2607       if (target == 0
2608 	  || GET_MODE (target) != mode[0]
2609 	  || ! (*insn_data[icode].operand[0].predicate) (target, mode[0]))
2610 	target = gen_reg_rtx (mode[0]);
2611     }
2612   else
2613     target = 0;
2614 
2615   switch (argcount)
2616     {
2617     case 0:
2618 	pat = GEN_FCN (icode) (target);
2619     case 1:
2620       if (target)
2621 	pat = GEN_FCN (icode) (target, op[0]);
2622       else
2623 	pat = GEN_FCN (icode) (op[0]);
2624       break;
2625     case 2:
2626       if (target)
2627 	pat = GEN_FCN (icode) (target, op[0], op[1]);
2628       else
2629 	pat = GEN_FCN (icode) (op[0], op[1]);
2630       break;
2631     case 3:
2632       if (target)
2633 	pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
2634       else
2635 	pat = GEN_FCN (icode) (op[0], op[1], op[2]);
2636       break;
2637     case 4:
2638       if (target)
2639 	pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
2640       else
2641 	pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
2642       break;
2643     default:
2644       gcc_unreachable ();
2645     }
2646 
2647   if (! pat)
2648     return 0;
2649   emit_insn (pat);
2650   return target;
2651 }
2652 
2653 /* Expand an expression EXP that calls a built-in function,
2654    with result going to TARGET if that's convenient
2655    (and in mode MODE if that's convenient).
2656    SUBTARGET may be used as the target for computing one of EXP's operands.
2657    IGNORE is nonzero if the value is to be ignored.  */
2658 
2659 static rtx
iq2000_expand_builtin(tree exp,rtx target,rtx subtarget ATTRIBUTE_UNUSED,machine_mode mode ATTRIBUTE_UNUSED,int ignore ATTRIBUTE_UNUSED)2660 iq2000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
2661 		       machine_mode mode ATTRIBUTE_UNUSED,
2662 		       int ignore ATTRIBUTE_UNUSED)
2663 {
2664   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
2665   int fcode = DECL_FUNCTION_CODE (fndecl);
2666   enum rtx_code code [5];
2667 
2668   code[0] = REG;
2669   code[1] = REG;
2670   code[2] = REG;
2671   code[3] = REG;
2672   code[4] = REG;
2673   switch (fcode)
2674     {
2675     default:
2676       break;
2677 
2678     case IQ2000_BUILTIN_ADO16:
2679       return expand_one_builtin (CODE_FOR_ado16, target, exp, code, 2);
2680 
2681     case IQ2000_BUILTIN_RAM:
2682       code[1] = CONST_INT;
2683       code[2] = CONST_INT;
2684       code[3] = CONST_INT;
2685       return expand_one_builtin (CODE_FOR_ram, target, exp, code, 4);
2686 
2687     case IQ2000_BUILTIN_CHKHDR:
2688       return expand_one_builtin (CODE_FOR_chkhdr, target, exp, code, 2);
2689 
2690     case IQ2000_BUILTIN_PKRL:
2691       return expand_one_builtin (CODE_FOR_pkrl, target, exp, code, 2);
2692 
2693     case IQ2000_BUILTIN_CFC0:
2694       code[0] = CONST_INT;
2695       return expand_one_builtin (CODE_FOR_cfc0, target, exp, code, 1);
2696 
2697     case IQ2000_BUILTIN_CFC1:
2698       code[0] = CONST_INT;
2699       return expand_one_builtin (CODE_FOR_cfc1, target, exp, code, 1);
2700 
2701     case IQ2000_BUILTIN_CFC2:
2702       code[0] = CONST_INT;
2703       return expand_one_builtin (CODE_FOR_cfc2, target, exp, code, 1);
2704 
2705     case IQ2000_BUILTIN_CFC3:
2706       code[0] = CONST_INT;
2707       return expand_one_builtin (CODE_FOR_cfc3, target, exp, code, 1);
2708 
2709     case IQ2000_BUILTIN_CTC0:
2710       code[1] = CONST_INT;
2711       return expand_one_builtin (CODE_FOR_ctc0, target, exp, code, 2);
2712 
2713     case IQ2000_BUILTIN_CTC1:
2714       code[1] = CONST_INT;
2715       return expand_one_builtin (CODE_FOR_ctc1, target, exp, code, 2);
2716 
2717     case IQ2000_BUILTIN_CTC2:
2718       code[1] = CONST_INT;
2719       return expand_one_builtin (CODE_FOR_ctc2, target, exp, code, 2);
2720 
2721     case IQ2000_BUILTIN_CTC3:
2722       code[1] = CONST_INT;
2723       return expand_one_builtin (CODE_FOR_ctc3, target, exp, code, 2);
2724 
2725     case IQ2000_BUILTIN_MFC0:
2726       code[0] = CONST_INT;
2727       return expand_one_builtin (CODE_FOR_mfc0, target, exp, code, 1);
2728 
2729     case IQ2000_BUILTIN_MFC1:
2730       code[0] = CONST_INT;
2731       return expand_one_builtin (CODE_FOR_mfc1, target, exp, code, 1);
2732 
2733     case IQ2000_BUILTIN_MFC2:
2734       code[0] = CONST_INT;
2735       return expand_one_builtin (CODE_FOR_mfc2, target, exp, code, 1);
2736 
2737     case IQ2000_BUILTIN_MFC3:
2738       code[0] = CONST_INT;
2739       return expand_one_builtin (CODE_FOR_mfc3, target, exp, code, 1);
2740 
2741     case IQ2000_BUILTIN_MTC0:
2742       code[1] = CONST_INT;
2743       return expand_one_builtin (CODE_FOR_mtc0, target, exp, code, 2);
2744 
2745     case IQ2000_BUILTIN_MTC1:
2746       code[1] = CONST_INT;
2747       return expand_one_builtin (CODE_FOR_mtc1, target, exp, code, 2);
2748 
2749     case IQ2000_BUILTIN_MTC2:
2750       code[1] = CONST_INT;
2751       return expand_one_builtin (CODE_FOR_mtc2, target, exp, code, 2);
2752 
2753     case IQ2000_BUILTIN_MTC3:
2754       code[1] = CONST_INT;
2755       return expand_one_builtin (CODE_FOR_mtc3, target, exp, code, 2);
2756 
2757     case IQ2000_BUILTIN_LUR:
2758       return expand_one_builtin (CODE_FOR_lur, target, exp, code, 2);
2759 
2760     case IQ2000_BUILTIN_RB:
2761       return expand_one_builtin (CODE_FOR_rb, target, exp, code, 2);
2762 
2763     case IQ2000_BUILTIN_RX:
2764       return expand_one_builtin (CODE_FOR_rx, target, exp, code, 2);
2765 
2766     case IQ2000_BUILTIN_SRRD:
2767       return expand_one_builtin (CODE_FOR_srrd, target, exp, code, 1);
2768 
2769     case IQ2000_BUILTIN_SRWR:
2770       return expand_one_builtin (CODE_FOR_srwr, target, exp, code, 2);
2771 
2772     case IQ2000_BUILTIN_WB:
2773       return expand_one_builtin (CODE_FOR_wb, target, exp, code, 2);
2774 
2775     case IQ2000_BUILTIN_WX:
2776       return expand_one_builtin (CODE_FOR_wx, target, exp, code, 2);
2777 
2778     case IQ2000_BUILTIN_LUC32L:
2779       return expand_one_builtin (CODE_FOR_luc32l, target, exp, code, 2);
2780 
2781     case IQ2000_BUILTIN_LUC64:
2782       return expand_one_builtin (CODE_FOR_luc64, target, exp, code, 2);
2783 
2784     case IQ2000_BUILTIN_LUC64L:
2785       return expand_one_builtin (CODE_FOR_luc64l, target, exp, code, 2);
2786 
2787     case IQ2000_BUILTIN_LUK:
2788       return expand_one_builtin (CODE_FOR_luk, target, exp, code, 2);
2789 
2790     case IQ2000_BUILTIN_LULCK:
2791       return expand_one_builtin (CODE_FOR_lulck, target, exp, code, 1);
2792 
2793     case IQ2000_BUILTIN_LUM32:
2794       return expand_one_builtin (CODE_FOR_lum32, target, exp, code, 2);
2795 
2796     case IQ2000_BUILTIN_LUM32L:
2797       return expand_one_builtin (CODE_FOR_lum32l, target, exp, code, 2);
2798 
2799     case IQ2000_BUILTIN_LUM64:
2800       return expand_one_builtin (CODE_FOR_lum64, target, exp, code, 2);
2801 
2802     case IQ2000_BUILTIN_LUM64L:
2803       return expand_one_builtin (CODE_FOR_lum64l, target, exp, code, 2);
2804 
2805     case IQ2000_BUILTIN_LURL:
2806       return expand_one_builtin (CODE_FOR_lurl, target, exp, code, 2);
2807 
2808     case IQ2000_BUILTIN_MRGB:
2809       code[2] = CONST_INT;
2810       return expand_one_builtin (CODE_FOR_mrgb, target, exp, code, 3);
2811 
2812     case IQ2000_BUILTIN_SRRDL:
2813       return expand_one_builtin (CODE_FOR_srrdl, target, exp, code, 1);
2814 
2815     case IQ2000_BUILTIN_SRULCK:
2816       return expand_one_builtin (CODE_FOR_srulck, target, exp, code, 1);
2817 
2818     case IQ2000_BUILTIN_SRWRU:
2819       return expand_one_builtin (CODE_FOR_srwru, target, exp, code, 2);
2820 
2821     case IQ2000_BUILTIN_TRAPQFL:
2822       return expand_one_builtin (CODE_FOR_trapqfl, target, exp, code, 0);
2823 
2824     case IQ2000_BUILTIN_TRAPQNE:
2825       return expand_one_builtin (CODE_FOR_trapqne, target, exp, code, 0);
2826 
2827     case IQ2000_BUILTIN_TRAPREL:
2828       return expand_one_builtin (CODE_FOR_traprel, target, exp, code, 1);
2829 
2830     case IQ2000_BUILTIN_WBU:
2831       return expand_one_builtin (CODE_FOR_wbu, target, exp, code, 3);
2832 
2833     case IQ2000_BUILTIN_SYSCALL:
2834       return expand_one_builtin (CODE_FOR_syscall, target, exp, code, 0);
2835     }
2836 
2837   return NULL_RTX;
2838 }
2839 
2840 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
2841 
2842 static bool
iq2000_return_in_memory(const_tree type,const_tree fntype ATTRIBUTE_UNUSED)2843 iq2000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
2844 {
2845   return ((int_size_in_bytes (type) > (2 * UNITS_PER_WORD))
2846 	  || (int_size_in_bytes (type) == -1));
2847 }
2848 
2849 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
2850 
2851 static void
iq2000_setup_incoming_varargs(cumulative_args_t cum_v,machine_mode mode ATTRIBUTE_UNUSED,tree type ATTRIBUTE_UNUSED,int * pretend_size,int no_rtl)2852 iq2000_setup_incoming_varargs (cumulative_args_t cum_v,
2853 			       machine_mode mode ATTRIBUTE_UNUSED,
2854 			       tree type ATTRIBUTE_UNUSED, int * pretend_size,
2855 			       int no_rtl)
2856 {
2857   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2858   unsigned int iq2000_off = ! cum->last_arg_fp;
2859   unsigned int iq2000_fp_off = cum->last_arg_fp;
2860 
2861   if ((cum->arg_words < MAX_ARGS_IN_REGISTERS - iq2000_off))
2862     {
2863       int iq2000_save_gp_regs
2864 	= MAX_ARGS_IN_REGISTERS - cum->arg_words - iq2000_off;
2865       int iq2000_save_fp_regs
2866         = (MAX_ARGS_IN_REGISTERS - cum->fp_arg_words - iq2000_fp_off);
2867 
2868       if (iq2000_save_gp_regs < 0)
2869 	iq2000_save_gp_regs = 0;
2870       if (iq2000_save_fp_regs < 0)
2871 	iq2000_save_fp_regs = 0;
2872 
2873       *pretend_size = ((iq2000_save_gp_regs * UNITS_PER_WORD)
2874                       + (iq2000_save_fp_regs * UNITS_PER_FPREG));
2875 
2876       if (! (no_rtl))
2877 	{
2878 	  if (cum->arg_words < MAX_ARGS_IN_REGISTERS - iq2000_off)
2879 	    {
2880 	      rtx ptr, mem;
2881 	      ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
2882 				   - (iq2000_save_gp_regs
2883 				      * UNITS_PER_WORD));
2884 	      mem = gen_rtx_MEM (BLKmode, ptr);
2885 	      move_block_from_reg
2886 		(cum->arg_words + GP_ARG_FIRST + iq2000_off,
2887 		 mem,
2888 		 iq2000_save_gp_regs);
2889 	    }
2890 	}
2891     }
2892 }
2893 
2894 /* A C compound statement to output to stdio stream STREAM the
2895    assembler syntax for an instruction operand that is a memory
2896    reference whose address is ADDR.  ADDR is an RTL expression.  */
2897 
2898 static void
iq2000_print_operand_address(FILE * file,machine_mode mode,rtx addr)2899 iq2000_print_operand_address (FILE * file, machine_mode mode, rtx addr)
2900 {
2901   if (!addr)
2902     error ("PRINT_OPERAND_ADDRESS, null pointer");
2903 
2904   else
2905     switch (GET_CODE (addr))
2906       {
2907       case REG:
2908 	if (REGNO (addr) == ARG_POINTER_REGNUM)
2909 	  abort_with_insn (addr, "Arg pointer not eliminated.");
2910 
2911 	fprintf (file, "0(%s)", reg_names [REGNO (addr)]);
2912 	break;
2913 
2914       case LO_SUM:
2915 	{
2916 	  rtx arg0 = XEXP (addr, 0);
2917 	  rtx arg1 = XEXP (addr, 1);
2918 
2919 	  if (GET_CODE (arg0) != REG)
2920 	    abort_with_insn (addr,
2921 			     "PRINT_OPERAND_ADDRESS, LO_SUM with #1 not REG.");
2922 
2923 	  fprintf (file, "%%lo(");
2924 	  iq2000_print_operand_address (file, mode, arg1);
2925 	  fprintf (file, ")(%s)", reg_names [REGNO (arg0)]);
2926 	}
2927 	break;
2928 
2929       case PLUS:
2930 	{
2931 	  rtx reg = 0;
2932 	  rtx offset = 0;
2933 	  rtx arg0 = XEXP (addr, 0);
2934 	  rtx arg1 = XEXP (addr, 1);
2935 
2936 	  if (GET_CODE (arg0) == REG)
2937 	    {
2938 	      reg = arg0;
2939 	      offset = arg1;
2940 	      if (GET_CODE (offset) == REG)
2941 		abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, 2 regs");
2942 	    }
2943 
2944 	  else if (GET_CODE (arg1) == REG)
2945 	      reg = arg1, offset = arg0;
2946 	  else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
2947 	    {
2948 	      output_addr_const (file, addr);
2949 	      break;
2950 	    }
2951 	  else
2952 	    abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, no regs");
2953 
2954 	  if (! CONSTANT_P (offset))
2955 	    abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #2");
2956 
2957 	  if (REGNO (reg) == ARG_POINTER_REGNUM)
2958 	    abort_with_insn (addr, "Arg pointer not eliminated.");
2959 
2960 	  output_addr_const (file, offset);
2961 	  fprintf (file, "(%s)", reg_names [REGNO (reg)]);
2962 	}
2963 	break;
2964 
2965       case LABEL_REF:
2966       case SYMBOL_REF:
2967       case CONST_INT:
2968       case CONST:
2969 	output_addr_const (file, addr);
2970 	if (GET_CODE (addr) == CONST_INT)
2971 	  fprintf (file, "(%s)", reg_names [0]);
2972 	break;
2973 
2974       default:
2975 	abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #1");
2976 	break;
2977     }
2978 }
2979 
2980 /* A C compound statement to output to stdio stream FILE the
2981    assembler syntax for an instruction operand OP.
2982 
2983    LETTER is a value that can be used to specify one of several ways
2984    of printing the operand.  It is used when identical operands
2985    must be printed differently depending on the context.  LETTER
2986    comes from the `%' specification that was used to request
2987    printing of the operand.  If the specification was just `%DIGIT'
2988    then LETTER is 0; if the specification was `%LTR DIGIT' then LETTER
2989    is the ASCII code for LTR.
2990 
2991    If OP is a register, this macro should print the register's name.
2992    The names can be found in an array `reg_names' whose type is
2993    `char *[]'.  `reg_names' is initialized from `REGISTER_NAMES'.
2994 
2995    When the machine description has a specification `%PUNCT' (a `%'
2996    followed by a punctuation character), this macro is called with
2997    a null pointer for X and the punctuation character for LETTER.
2998 
2999    The IQ2000 specific codes are:
3000 
3001    'X'  X is CONST_INT, prints upper 16 bits in hexadecimal format = "0x%04x",
3002    'x'  X is CONST_INT, prints lower 16 bits in hexadecimal format = "0x%04x",
3003    'd'  output integer constant in decimal,
3004    'z'	if the operand is 0, use $0 instead of normal operand.
3005    'D'  print second part of double-word register or memory operand.
3006    'L'  print low-order register of double-word register operand.
3007    'M'  print high-order register of double-word register operand.
3008    'C'  print part of opcode for a branch condition.
3009    'F'  print part of opcode for a floating-point branch condition.
3010    'N'  print part of opcode for a branch condition, inverted.
3011    'W'  print part of opcode for a floating-point branch condition, inverted.
3012    'A'	Print part of opcode for a bit test condition.
3013    'P'  Print label for a bit test.
3014    'p'  Print log for a bit test.
3015    'B'  print 'z' for EQ, 'n' for NE
3016    'b'  print 'n' for EQ, 'z' for NE
3017    'T'  print 'f' for EQ, 't' for NE
3018    't'  print 't' for EQ, 'f' for NE
3019    'Z'  print register and a comma, but print nothing for $fcc0
3020    '?'	Print 'l' if we are to use a branch likely instead of normal branch.
3021    '@'	Print the name of the assembler temporary register (at or $1).
3022    '.'	Print the name of the register with a hard-wired zero (zero or $0).
3023    '$'	Print the name of the stack pointer register (sp or $29).
3024    '+'	Print the name of the gp register (gp or $28).  */
3025 
3026 static void
iq2000_print_operand(FILE * file,rtx op,int letter)3027 iq2000_print_operand (FILE *file, rtx op, int letter)
3028 {
3029   enum rtx_code code;
3030 
3031   if (iq2000_print_operand_punct_valid_p (letter))
3032     {
3033       switch (letter)
3034 	{
3035 	case '?':
3036 	  if (iq2000_branch_likely)
3037 	    putc ('l', file);
3038 	  break;
3039 
3040 	case '@':
3041 	  fputs (reg_names [GP_REG_FIRST + 1], file);
3042 	  break;
3043 
3044 	case '.':
3045 	  fputs (reg_names [GP_REG_FIRST + 0], file);
3046 	  break;
3047 
3048 	case '$':
3049 	  fputs (reg_names[STACK_POINTER_REGNUM], file);
3050 	  break;
3051 
3052 	case '+':
3053 	  fputs (reg_names[GP_REG_FIRST + 28], file);
3054 	  break;
3055 
3056 	default:
3057 	  error ("PRINT_OPERAND: Unknown punctuation '%c'", letter);
3058 	  break;
3059 	}
3060 
3061       return;
3062     }
3063 
3064   if (! op)
3065     {
3066       error ("PRINT_OPERAND null pointer");
3067       return;
3068     }
3069 
3070   code = GET_CODE (op);
3071 
3072   if (code == SIGN_EXTEND)
3073     op = XEXP (op, 0), code = GET_CODE (op);
3074 
3075   if (letter == 'C')
3076     switch (code)
3077       {
3078       case EQ:	fputs ("eq",  file); break;
3079       case NE:	fputs ("ne",  file); break;
3080       case GT:	fputs ("gt",  file); break;
3081       case GE:	fputs ("ge",  file); break;
3082       case LT:	fputs ("lt",  file); break;
3083       case LE:	fputs ("le",  file); break;
3084       case GTU: fputs ("ne", file); break;
3085       case GEU: fputs ("geu", file); break;
3086       case LTU: fputs ("ltu", file); break;
3087       case LEU: fputs ("eq", file); break;
3088       default:
3089 	abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%C");
3090       }
3091 
3092   else if (letter == 'N')
3093     switch (code)
3094       {
3095       case EQ:	fputs ("ne",  file); break;
3096       case NE:	fputs ("eq",  file); break;
3097       case GT:	fputs ("le",  file); break;
3098       case GE:	fputs ("lt",  file); break;
3099       case LT:	fputs ("ge",  file); break;
3100       case LE:	fputs ("gt",  file); break;
3101       case GTU: fputs ("leu", file); break;
3102       case GEU: fputs ("ltu", file); break;
3103       case LTU: fputs ("geu", file); break;
3104       case LEU: fputs ("gtu", file); break;
3105       default:
3106 	abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%N");
3107       }
3108 
3109   else if (letter == 'F')
3110     switch (code)
3111       {
3112       case EQ: fputs ("c1f", file); break;
3113       case NE: fputs ("c1t", file); break;
3114       default:
3115 	abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%F");
3116       }
3117 
3118   else if (letter == 'W')
3119     switch (code)
3120       {
3121       case EQ: fputs ("c1t", file); break;
3122       case NE: fputs ("c1f", file); break;
3123       default:
3124 	abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%W");
3125       }
3126 
3127   else if (letter == 'A')
3128     fputs (code == LABEL_REF ? "i" : "in", file);
3129 
3130   else if (letter == 'P')
3131     {
3132       if (code == LABEL_REF)
3133 	output_addr_const (file, op);
3134       else if (code != PC)
3135 	output_operand_lossage ("invalid %%P operand");
3136     }
3137 
3138   else if (letter == 'p')
3139     {
3140       int value;
3141       if (code != CONST_INT
3142 	  || (value = exact_log2 (INTVAL (op))) < 0)
3143 	output_operand_lossage ("invalid %%p value");
3144       else
3145 	fprintf (file, "%d", value);
3146     }
3147 
3148   else if (letter == 'Z')
3149     {
3150       gcc_unreachable ();
3151     }
3152 
3153   else if (code == REG || code == SUBREG)
3154     {
3155       int regnum;
3156 
3157       if (code == REG)
3158 	regnum = REGNO (op);
3159       else
3160 	regnum = true_regnum (op);
3161 
3162       if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
3163 	  || (letter == 'L' && WORDS_BIG_ENDIAN)
3164 	  || letter == 'D')
3165 	regnum++;
3166 
3167       fprintf (file, "%s", reg_names[regnum]);
3168     }
3169 
3170   else if (code == MEM)
3171     {
3172       machine_mode mode = GET_MODE (op);
3173 
3174       if (letter == 'D')
3175 	output_address (mode, plus_constant (Pmode, XEXP (op, 0), 4));
3176       else
3177 	output_address (mode, XEXP (op, 0));
3178     }
3179 
3180   else if (code == CONST_DOUBLE
3181 	   && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
3182     {
3183       char s[60];
3184 
3185       real_to_decimal (s, CONST_DOUBLE_REAL_VALUE (op), sizeof (s), 0, 1);
3186       fputs (s, file);
3187     }
3188 
3189   else if (letter == 'x' && GET_CODE (op) == CONST_INT)
3190     fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
3191 
3192   else if (letter == 'X' && GET_CODE(op) == CONST_INT)
3193     fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & (INTVAL (op) >> 16));
3194 
3195   else if (letter == 'd' && GET_CODE(op) == CONST_INT)
3196     fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
3197 
3198   else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
3199     fputs (reg_names[GP_REG_FIRST], file);
3200 
3201   else if (letter == 'd' || letter == 'x' || letter == 'X')
3202     output_operand_lossage ("invalid use of %%d, %%x, or %%X");
3203 
3204   else if (letter == 'B')
3205     fputs (code == EQ ? "z" : "n", file);
3206   else if (letter == 'b')
3207     fputs (code == EQ ? "n" : "z", file);
3208   else if (letter == 'T')
3209     fputs (code == EQ ? "f" : "t", file);
3210   else if (letter == 't')
3211     fputs (code == EQ ? "t" : "f", file);
3212 
3213   else if (code == CONST && GET_CODE (XEXP (op, 0)) == REG)
3214     {
3215       iq2000_print_operand (file, XEXP (op, 0), letter);
3216     }
3217 
3218   else
3219     output_addr_const (file, op);
3220 }
3221 
3222 static bool
iq2000_print_operand_punct_valid_p(unsigned char code)3223 iq2000_print_operand_punct_valid_p (unsigned char code)
3224 {
3225   return iq2000_print_operand_punct[code];
3226 }
3227 
3228 /* For the IQ2000, transform:
3229 
3230         memory(X + <large int>)
3231    into:
3232         Y = <large int> & ~0x7fff;
3233         Z = X + Y
3234         memory (Z + (<large int> & 0x7fff));
3235 */
3236 
3237 rtx
iq2000_legitimize_address(rtx xinsn,rtx old_x ATTRIBUTE_UNUSED,machine_mode mode)3238 iq2000_legitimize_address (rtx xinsn, rtx old_x ATTRIBUTE_UNUSED,
3239 			   machine_mode mode)
3240 {
3241   if (TARGET_DEBUG_B_MODE)
3242     {
3243       GO_PRINTF ("\n========== LEGITIMIZE_ADDRESS\n");
3244       GO_DEBUG_RTX (xinsn);
3245     }
3246 
3247   if (iq2000_check_split (xinsn, mode))
3248     {
3249       return gen_rtx_LO_SUM (Pmode,
3250                              copy_to_mode_reg (Pmode,
3251                                                gen_rtx_HIGH (Pmode, xinsn)),
3252                              xinsn);
3253     }
3254 
3255   if (GET_CODE (xinsn) == PLUS)
3256     {
3257       rtx xplus0 = XEXP (xinsn, 0);
3258       rtx xplus1 = XEXP (xinsn, 1);
3259       enum rtx_code code0 = GET_CODE (xplus0);
3260       enum rtx_code code1 = GET_CODE (xplus1);
3261 
3262       if (code0 != REG && code1 == REG)
3263         {
3264           xplus0 = XEXP (xinsn, 1);
3265           xplus1 = XEXP (xinsn, 0);
3266           code0 = GET_CODE (xplus0);
3267           code1 = GET_CODE (xplus1);
3268         }
3269 
3270       if (code0 == REG && REG_MODE_OK_FOR_BASE_P (xplus0, mode)
3271           && code1 == CONST_INT && !SMALL_INT (xplus1))
3272         {
3273           rtx int_reg = gen_reg_rtx (Pmode);
3274           rtx ptr_reg = gen_reg_rtx (Pmode);
3275 
3276           emit_move_insn (int_reg,
3277                           GEN_INT (INTVAL (xplus1) & ~ 0x7fff));
3278 
3279           emit_insn (gen_rtx_SET (ptr_reg,
3280                                   gen_rtx_PLUS (Pmode, xplus0, int_reg)));
3281 
3282           return plus_constant (Pmode, ptr_reg, INTVAL (xplus1) & 0x7fff);
3283         }
3284     }
3285 
3286   if (TARGET_DEBUG_B_MODE)
3287     GO_PRINTF ("LEGITIMIZE_ADDRESS could not fix.\n");
3288 
3289   return xinsn;
3290 }
3291 
3292 
3293 static bool
iq2000_rtx_costs(rtx x,machine_mode mode,int outer_code ATTRIBUTE_UNUSED,int opno ATTRIBUTE_UNUSED,int * total,bool speed ATTRIBUTE_UNUSED)3294 iq2000_rtx_costs (rtx x, machine_mode mode, int outer_code ATTRIBUTE_UNUSED,
3295 		  int opno ATTRIBUTE_UNUSED, int * total,
3296 		  bool speed ATTRIBUTE_UNUSED)
3297 {
3298   int code = GET_CODE (x);
3299 
3300   switch (code)
3301     {
3302     case MEM:
3303       {
3304 	int num_words = (GET_MODE_SIZE (mode) > UNITS_PER_WORD) ? 2 : 1;
3305 
3306 	if (simple_memory_operand (x, mode))
3307 	  return COSTS_N_INSNS (num_words);
3308 
3309 	* total = COSTS_N_INSNS (2 * num_words);
3310 	break;
3311       }
3312 
3313     case FFS:
3314       * total = COSTS_N_INSNS (6);
3315       break;
3316 
3317     case AND:
3318     case IOR:
3319     case XOR:
3320     case NOT:
3321       * total = COSTS_N_INSNS (mode == DImode ? 2 : 1);
3322       break;
3323 
3324     case ASHIFT:
3325     case ASHIFTRT:
3326     case LSHIFTRT:
3327       if (mode == DImode)
3328 	* total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT) ? 4 : 12);
3329       else
3330 	* total = COSTS_N_INSNS (1);
3331     break;
3332 
3333     case ABS:
3334       if (mode == SFmode || mode == DFmode)
3335 	* total = COSTS_N_INSNS (1);
3336       else
3337 	* total = COSTS_N_INSNS (4);
3338       break;
3339 
3340     case PLUS:
3341     case MINUS:
3342       if (mode == SFmode || mode == DFmode)
3343 	* total = COSTS_N_INSNS (6);
3344       else if (mode == DImode)
3345 	* total = COSTS_N_INSNS (4);
3346       else
3347 	* total = COSTS_N_INSNS (1);
3348       break;
3349 
3350     case NEG:
3351       * total = (mode == DImode) ? 4 : 1;
3352       break;
3353 
3354     case MULT:
3355       if (mode == SFmode)
3356 	* total = COSTS_N_INSNS (7);
3357       else if (mode == DFmode)
3358 	* total = COSTS_N_INSNS (8);
3359       else
3360 	* total = COSTS_N_INSNS (10);
3361       break;
3362 
3363     case DIV:
3364     case MOD:
3365       if (mode == SFmode)
3366 	* total = COSTS_N_INSNS (23);
3367       else if (mode == DFmode)
3368 	* total = COSTS_N_INSNS (36);
3369       else
3370 	* total = COSTS_N_INSNS (69);
3371       break;
3372 
3373     case UDIV:
3374     case UMOD:
3375       * total = COSTS_N_INSNS (69);
3376       break;
3377 
3378     case SIGN_EXTEND:
3379       * total = COSTS_N_INSNS (2);
3380       break;
3381 
3382     case ZERO_EXTEND:
3383       * total = COSTS_N_INSNS (1);
3384       break;
3385 
3386     case CONST_INT:
3387       * total = 0;
3388       break;
3389 
3390     case LABEL_REF:
3391       * total = COSTS_N_INSNS (2);
3392       break;
3393 
3394     case CONST:
3395       {
3396 	rtx offset = const0_rtx;
3397 	rtx symref = eliminate_constant_term (XEXP (x, 0), & offset);
3398 
3399 	if (GET_CODE (symref) == LABEL_REF)
3400 	  * total = COSTS_N_INSNS (2);
3401 	else if (GET_CODE (symref) != SYMBOL_REF)
3402 	  * total = COSTS_N_INSNS (4);
3403 	/* Let's be paranoid....  */
3404 	else if (INTVAL (offset) < -32768 || INTVAL (offset) > 32767)
3405 	  * total = COSTS_N_INSNS (2);
3406 	else
3407 	  * total = COSTS_N_INSNS (SYMBOL_REF_FLAG (symref) ? 1 : 2);
3408 	break;
3409       }
3410 
3411     case SYMBOL_REF:
3412       * total = COSTS_N_INSNS (SYMBOL_REF_FLAG (x) ? 1 : 2);
3413       break;
3414 
3415     case CONST_DOUBLE:
3416       {
3417 	rtx high, low;
3418 
3419 	split_double (x, & high, & low);
3420 
3421 	* total = COSTS_N_INSNS (  (high == CONST0_RTX (GET_MODE (high))
3422 				  || low == CONST0_RTX (GET_MODE (low)))
3423 				   ? 2 : 4);
3424 	break;
3425       }
3426 
3427     default:
3428       return false;
3429     }
3430   return true;
3431 }
3432 
3433 /* Worker for TARGET_ASM_TRAMPOLINE_TEMPLATE.  */
3434 
3435 static void
iq2000_asm_trampoline_template(FILE * f)3436 iq2000_asm_trampoline_template (FILE *f)
3437 {
3438   fprintf (f, "\t.word\t0x03e00821\t\t# move   $1,$31\n");
3439   fprintf (f, "\t.word\t0x04110001\t\t# bgezal $0,.+8\n");
3440   fprintf (f, "\t.word\t0x00000000\t\t# nop\n");
3441   if (Pmode == DImode)
3442     {
3443       fprintf (f, "\t.word\t0xdfe30014\t\t# ld     $3,20($31)\n");
3444       fprintf (f, "\t.word\t0xdfe2001c\t\t# ld     $2,28($31)\n");
3445     }
3446   else
3447     {
3448       fprintf (f, "\t.word\t0x8fe30014\t\t# lw     $3,20($31)\n");
3449       fprintf (f, "\t.word\t0x8fe20018\t\t# lw     $2,24($31)\n");
3450     }
3451   fprintf (f, "\t.word\t0x0060c821\t\t# move   $25,$3 (abicalls)\n");
3452   fprintf (f, "\t.word\t0x00600008\t\t# jr     $3\n");
3453   fprintf (f, "\t.word\t0x0020f821\t\t# move   $31,$1\n");
3454   fprintf (f, "\t.word\t0x00000000\t\t# <function address>\n");
3455   fprintf (f, "\t.word\t0x00000000\t\t# <static chain value>\n");
3456 }
3457 
3458 /* Worker for TARGET_TRAMPOLINE_INIT.  */
3459 
3460 static void
iq2000_trampoline_init(rtx m_tramp,tree fndecl,rtx chain_value)3461 iq2000_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
3462 {
3463   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
3464   rtx mem;
3465 
3466   emit_block_move (m_tramp, assemble_trampoline_template (),
3467 		   GEN_INT (TRAMPOLINE_CODE_SIZE), BLOCK_OP_NORMAL);
3468 
3469   mem = adjust_address (m_tramp, Pmode, TRAMPOLINE_CODE_SIZE);
3470   emit_move_insn (mem, fnaddr);
3471   mem = adjust_address (m_tramp, Pmode,
3472 			TRAMPOLINE_CODE_SIZE + GET_MODE_SIZE (Pmode));
3473   emit_move_insn (mem, chain_value);
3474 }
3475 
3476 #include "gt-iq2000.h"
3477