]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/nds32/nds32.c
tree-ssa-pre.c (alloc_expression_id): Use quick_grow_cleared.
[thirdparty/gcc.git] / gcc / config / nds32 / nds32.c
CommitLineData
9304f876 1/* Subroutines used for code generation of Andes NDS32 cpu for GNU compiler
23a5b65a 2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
9304f876
CJW
3 Contributed by Andes Technology Corporation.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
19ac960a 21/* ------------------------------------------------------------------------ */
9304f876
CJW
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "tree.h"
d8a2d370
DN
28#include "stor-layout.h"
29#include "varasm.h"
30#include "calls.h"
9304f876
CJW
31#include "rtl.h"
32#include "regs.h"
33#include "hard-reg-set.h"
34#include "insn-config.h" /* Required by recog.h. */
35#include "conditions.h"
36#include "output.h"
37#include "insn-attr.h" /* For DFA state_t. */
38#include "insn-codes.h" /* For CODE_FOR_xxx. */
39#include "reload.h" /* For push_reload(). */
40#include "flags.h"
41#include "function.h"
42#include "expr.h"
43#include "recog.h"
44#include "diagnostic-core.h"
45#include "df.h"
46#include "tm_p.h"
47#include "tm-constrs.h"
48#include "optabs.h" /* For GEN_FCN. */
49#include "target.h"
50#include "target-def.h"
51#include "langhooks.h" /* For add_builtin_function(). */
52#include "ggc.h"
9b2b7279 53#include "builtins.h"
9304f876
CJW
54
55/* ------------------------------------------------------------------------ */
56
57/* This file is divided into five parts:
58
59 PART 1: Auxiliary static variable definitions and
60 target hook static variable definitions.
61
62 PART 2: Auxiliary static function definitions.
63
64 PART 3: Implement target hook stuff definitions.
65
66 PART 4: Implemet extern function definitions,
67 the prototype is in nds32-protos.h.
68
69 PART 5: Initialize target hook structure and definitions. */
70
71/* ------------------------------------------------------------------------ */
72
73/* PART 1: Auxiliary static variable definitions and
74 target hook static variable definitions. */
75
9304f876
CJW
76/* Define intrinsic register names.
77 Please refer to nds32_intrinsic.h file, the index is corresponding to
78 'enum nds32_intrinsic_registers' data type values.
79 NOTE that the base value starting from 1024. */
80static const char * const nds32_intrinsic_register_names[] =
81{
82 "$PSW", "$IPSW", "$ITYPE", "$IPC"
83};
84
85/* Defining target-specific uses of __attribute__. */
86static const struct attribute_spec nds32_attribute_table[] =
87{
88 /* Syntax: { name, min_len, max_len, decl_required, type_required,
89 function_type_required, handler, affects_type_identity } */
90
91 /* The interrupt vid: [0-63]+ (actual vector number starts from 9 to 72). */
92 { "interrupt", 1, 64, false, false, false, NULL, false },
93 /* The exception vid: [1-8]+ (actual vector number starts from 1 to 8). */
94 { "exception", 1, 8, false, false, false, NULL, false },
95 /* Argument is user's interrupt numbers. The vector number is always 0. */
96 { "reset", 1, 1, false, false, false, NULL, false },
97
98 /* The attributes describing isr nested type. */
99 { "nested", 0, 0, false, false, false, NULL, false },
100 { "not_nested", 0, 0, false, false, false, NULL, false },
101 { "nested_ready", 0, 0, false, false, false, NULL, false },
102
103 /* The attributes describing isr register save scheme. */
104 { "save_all", 0, 0, false, false, false, NULL, false },
105 { "partial_save", 0, 0, false, false, false, NULL, false },
106
107 /* The attributes used by reset attribute. */
108 { "nmi", 1, 1, false, false, false, NULL, false },
109 { "warm", 1, 1, false, false, false, NULL, false },
110
111 /* The attribute telling no prologue/epilogue. */
112 { "naked", 0, 0, false, false, false, NULL, false },
113
114 /* The last attribute spec is set to be NULL. */
115 { NULL, 0, 0, false, false, false, NULL, false }
116};
117
118
119/* ------------------------------------------------------------------------ */
120
121/* PART 2: Auxiliary static function definitions. */
122
123/* Function to save and restore machine-specific function data. */
124static struct machine_function *
125nds32_init_machine_status (void)
126{
127 struct machine_function *machine;
766090c2 128 machine = ggc_cleared_alloc<machine_function> ();
9304f876
CJW
129
130 /* Initially assume this function needs prologue/epilogue. */
131 machine->naked_p = 0;
132
133 /* Initially assume this function does NOT use fp_as_gp optimization. */
134 machine->fp_as_gp_p = 0;
135
136 return machine;
137}
138
139/* Function to compute stack frame size and
140 store into cfun->machine structure. */
141static void
142nds32_compute_stack_frame (void)
143{
144 int r;
145 int block_size;
146
147 /* Because nds32_compute_stack_frame() will be called from different place,
148 everytime we enter this function, we have to assume this function
149 needs prologue/epilogue. */
150 cfun->machine->naked_p = 0;
151
152 /* Get variadic arguments size to prepare pretend arguments and
35da54a6
CJW
153 we will push them into stack at prologue by ourself. */
154 cfun->machine->va_args_size = crtl->args.pretend_args_size;
155 if (cfun->machine->va_args_size != 0)
156 {
157 cfun->machine->va_args_first_regno
158 = NDS32_GPR_ARG_FIRST_REGNUM
159 + NDS32_MAX_GPR_REGS_FOR_ARGS
160 - (crtl->args.pretend_args_size / UNITS_PER_WORD);
161 cfun->machine->va_args_last_regno
162 = NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS - 1;
163 }
164 else
165 {
166 cfun->machine->va_args_first_regno = SP_REGNUM;
167 cfun->machine->va_args_last_regno = SP_REGNUM;
168 }
169
170 /* Important: We need to make sure that varargs area is 8-byte alignment. */
171 block_size = cfun->machine->va_args_size;
172 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
173 {
174 cfun->machine->va_args_area_padding_bytes
175 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
176 }
9304f876
CJW
177
178 /* Get local variables, incoming variables, and temporary variables size.
179 Note that we need to make sure it is 8-byte alignment because
180 there may be no padding bytes if we are using LRA. */
181 cfun->machine->local_size = NDS32_ROUND_UP_DOUBLE_WORD (get_frame_size ());
182
183 /* Get outgoing arguments size. */
184 cfun->machine->out_args_size = crtl->outgoing_args_size;
185
186 /* If $fp value is required to be saved on stack, it needs 4 bytes space.
187 Check whether $fp is ever live. */
188 cfun->machine->fp_size = (df_regs_ever_live_p (FP_REGNUM)) ? 4 : 0;
189
190 /* If $gp value is required to be saved on stack, it needs 4 bytes space.
191 Check whether we are using PIC code genration. */
192 cfun->machine->gp_size = (flag_pic) ? 4 : 0;
193
194 /* If $lp value is required to be saved on stack, it needs 4 bytes space.
195 Check whether $lp is ever live. */
196 cfun->machine->lp_size = (df_regs_ever_live_p (LP_REGNUM)) ? 4 : 0;
197
198 /* Initially there is no padding bytes. */
199 cfun->machine->callee_saved_area_padding_bytes = 0;
200
201 /* Calculate the bytes of saving callee-saved registers on stack. */
202 cfun->machine->callee_saved_regs_size = 0;
203 cfun->machine->callee_saved_regs_first_regno = SP_REGNUM;
204 cfun->machine->callee_saved_regs_last_regno = SP_REGNUM;
205 /* Currently, there is no need to check $r28~$r31
206 because we will save them in another way. */
207 for (r = 0; r < 28; r++)
208 {
209 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
210 {
211 /* Mark the first required callee-saved register
212 (only need to set it once).
213 If first regno == SP_REGNUM, we can tell that
214 it is the first time to be here. */
215 if (cfun->machine->callee_saved_regs_first_regno == SP_REGNUM)
216 cfun->machine->callee_saved_regs_first_regno = r;
217 /* Mark the last required callee-saved register. */
218 cfun->machine->callee_saved_regs_last_regno = r;
219 }
220 }
221
222 /* Check if this function can omit prologue/epilogue code fragment.
223 If there is 'naked' attribute in this function,
224 we can set 'naked_p' flag to indicate that
225 we do not have to generate prologue/epilogue.
226 Or, if all the following conditions succeed,
227 we can set this function 'naked_p' as well:
228 condition 1: first_regno == last_regno == SP_REGNUM,
229 which means we do not have to save
230 any callee-saved registers.
231 condition 2: Both $lp and $fp are NOT live in this function,
2da1e7c0
CJW
232 which means we do not need to save them and there
233 is no outgoing size.
9304f876
CJW
234 condition 3: There is no local_size, which means
235 we do not need to adjust $sp. */
236 if (lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl))
237 || (cfun->machine->callee_saved_regs_first_regno == SP_REGNUM
238 && cfun->machine->callee_saved_regs_last_regno == SP_REGNUM
239 && !df_regs_ever_live_p (FP_REGNUM)
240 && !df_regs_ever_live_p (LP_REGNUM)
241 && cfun->machine->local_size == 0))
242 {
2da1e7c0
CJW
243 /* Set this function 'naked_p' and other functions can check this flag.
244 Note that in nds32 port, the 'naked_p = 1' JUST means there is no
245 callee-saved, local size, and outgoing size.
246 The varargs space and ret instruction may still present in
247 the prologue/epilogue expanding. */
9304f876
CJW
248 cfun->machine->naked_p = 1;
249
250 /* No need to save $fp, $gp, and $lp.
251 We should set these value to be zero
252 so that nds32_initial_elimination_offset() can work properly. */
253 cfun->machine->fp_size = 0;
254 cfun->machine->gp_size = 0;
255 cfun->machine->lp_size = 0;
256
257 /* If stack usage computation is required,
258 we need to provide the static stack size. */
259 if (flag_stack_usage_info)
260 current_function_static_stack_size = 0;
261
262 /* No need to do following adjustment, return immediately. */
263 return;
264 }
265
266 /* Adjustment for v3push instructions:
267 If we are using v3push (push25/pop25) instructions,
268 we need to make sure Rb is $r6 and Re is
269 located on $r6, $r8, $r10, or $r14.
270 Some results above will be discarded and recomputed.
2da1e7c0
CJW
271 Note that it is only available under V3/V3M ISA and we
272 DO NOT setup following stuff for isr or variadic function. */
273 if (TARGET_V3PUSH
274 && !nds32_isr_function_p (current_function_decl)
275 && (cfun->machine->va_args_size == 0))
9304f876
CJW
276 {
277 /* Recompute:
278 cfun->machine->fp_size
279 cfun->machine->gp_size
280 cfun->machine->lp_size
281 cfun->machine->callee_saved_regs_first_regno
282 cfun->machine->callee_saved_regs_last_regno */
283
284 /* For v3push instructions, $fp, $gp, and $lp are always saved. */
285 cfun->machine->fp_size = 4;
286 cfun->machine->gp_size = 4;
287 cfun->machine->lp_size = 4;
288
289 /* Remember to set Rb = $r6. */
290 cfun->machine->callee_saved_regs_first_regno = 6;
291
292 if (cfun->machine->callee_saved_regs_last_regno <= 6)
293 {
294 /* Re = $r6 */
295 cfun->machine->callee_saved_regs_last_regno = 6;
296 }
297 else if (cfun->machine->callee_saved_regs_last_regno <= 8)
298 {
299 /* Re = $r8 */
300 cfun->machine->callee_saved_regs_last_regno = 8;
301 }
302 else if (cfun->machine->callee_saved_regs_last_regno <= 10)
303 {
304 /* Re = $r10 */
305 cfun->machine->callee_saved_regs_last_regno = 10;
306 }
307 else if (cfun->machine->callee_saved_regs_last_regno <= 14)
308 {
309 /* Re = $r14 */
310 cfun->machine->callee_saved_regs_last_regno = 14;
311 }
312 else if (cfun->machine->callee_saved_regs_last_regno == SP_REGNUM)
313 {
314 /* If last_regno is SP_REGNUM, which means
315 it is never changed, so set it to Re = $r6. */
316 cfun->machine->callee_saved_regs_last_regno = 6;
317 }
318 else
319 {
320 /* The program flow should not go here. */
321 gcc_unreachable ();
322 }
323 }
324
325 /* We have correctly set callee_saved_regs_first_regno
326 and callee_saved_regs_last_regno.
327 Initially, the callee_saved_regs_size is supposed to be 0.
328 As long as callee_saved_regs_last_regno is not SP_REGNUM,
329 we can update callee_saved_regs_size with new size. */
330 if (cfun->machine->callee_saved_regs_last_regno != SP_REGNUM)
331 {
332 /* Compute pushed size of callee-saved registers. */
333 cfun->machine->callee_saved_regs_size
334 = 4 * (cfun->machine->callee_saved_regs_last_regno
335 - cfun->machine->callee_saved_regs_first_regno
336 + 1);
337 }
338
339 /* Important: We need to make sure that
35da54a6 340 (fp_size + gp_size + lp_size + callee_saved_regs_size)
9304f876
CJW
341 is 8-byte alignment.
342 If it is not, calculate the padding bytes. */
35da54a6 343 block_size = cfun->machine->fp_size
9304f876
CJW
344 + cfun->machine->gp_size
345 + cfun->machine->lp_size
346 + cfun->machine->callee_saved_regs_size;
347 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
348 {
349 cfun->machine->callee_saved_area_padding_bytes
350 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
351 }
352
353 /* If stack usage computation is required,
354 we need to provide the static stack size. */
355 if (flag_stack_usage_info)
356 {
357 current_function_static_stack_size
358 = NDS32_ROUND_UP_DOUBLE_WORD (block_size)
359 + cfun->machine->local_size
360 + cfun->machine->out_args_size;
361 }
362}
363
364/* Function to create a parallel rtx pattern
365 which presents stack push multiple behavior.
366 The overall concept are:
367 "push registers to memory",
368 "adjust stack pointer". */
4e9a2848
CJW
369static void
370nds32_emit_stack_push_multiple (rtx Rb, rtx Re, rtx En4)
9304f876
CJW
371{
372 int regno;
373 int extra_count;
374 int num_use_regs;
375 int par_index;
376 int offset;
4e9a2848 377 int save_fp, save_gp, save_lp;
9304f876
CJW
378
379 rtx reg;
380 rtx mem;
381 rtx push_rtx;
382 rtx adjust_sp_rtx;
383 rtx parallel_insn;
384
385 /* We need to provide a customized rtx which contains
386 necessary information for data analysis,
387 so we create a parallel rtx like this:
388 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
389 (reg:SI Rb))
390 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
391 (reg:SI Rb+1))
392 ...
393 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
394 (reg:SI Re))
395 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
396 (reg:SI FP_REGNUM))
397 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
398 (reg:SI GP_REGNUM))
399 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
400 (reg:SI LP_REGNUM))
401 (set (reg:SI SP_REGNUM)
402 (plus (reg:SI SP_REGNUM) (const_int -32)))]) */
403
4e9a2848
CJW
404 /* Determine whether we need to save $fp, $gp, or $lp. */
405 save_fp = INTVAL (En4) & 0x8;
406 save_gp = INTVAL (En4) & 0x4;
407 save_lp = INTVAL (En4) & 0x2;
408
9304f876
CJW
409 /* Calculate the number of registers that will be pushed. */
410 extra_count = 0;
4e9a2848 411 if (save_fp)
9304f876 412 extra_count++;
4e9a2848 413 if (save_gp)
9304f876 414 extra_count++;
4e9a2848 415 if (save_lp)
9304f876
CJW
416 extra_count++;
417 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
418 if (REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM)
419 num_use_regs = extra_count;
420 else
421 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + extra_count;
422
423 /* In addition to used registers,
424 we need one more space for (set sp sp-x) rtx. */
425 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
426 rtvec_alloc (num_use_regs + 1));
427 par_index = 0;
428
429 /* Initialize offset and start to create push behavior. */
430 offset = -(num_use_regs * 4);
431
432 /* Create (set mem regX) from Rb, Rb+1 up to Re. */
433 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
434 {
435 /* Rb and Re may be SP_REGNUM.
436 We need to break this loop immediately. */
437 if (regno == SP_REGNUM)
4e9a2848 438 break;
9304f876
CJW
439
440 reg = gen_rtx_REG (SImode, regno);
441 mem = gen_frame_mem (SImode, plus_constant (Pmode,
442 stack_pointer_rtx,
443 offset));
444 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
445 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
446 RTX_FRAME_RELATED_P (push_rtx) = 1;
447 offset = offset + 4;
448 par_index++;
449 }
450
451 /* Create (set mem fp), (set mem gp), and (set mem lp) if necessary. */
4e9a2848 452 if (save_fp)
9304f876
CJW
453 {
454 reg = gen_rtx_REG (SImode, FP_REGNUM);
455 mem = gen_frame_mem (SImode, plus_constant (Pmode,
456 stack_pointer_rtx,
457 offset));
458 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
459 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
460 RTX_FRAME_RELATED_P (push_rtx) = 1;
461 offset = offset + 4;
462 par_index++;
463 }
4e9a2848 464 if (save_gp)
9304f876
CJW
465 {
466 reg = gen_rtx_REG (SImode, GP_REGNUM);
467 mem = gen_frame_mem (SImode, plus_constant (Pmode,
468 stack_pointer_rtx,
469 offset));
470 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
471 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
472 RTX_FRAME_RELATED_P (push_rtx) = 1;
473 offset = offset + 4;
474 par_index++;
475 }
4e9a2848 476 if (save_lp)
9304f876
CJW
477 {
478 reg = gen_rtx_REG (SImode, LP_REGNUM);
479 mem = gen_frame_mem (SImode, plus_constant (Pmode,
480 stack_pointer_rtx,
481 offset));
482 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
483 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
484 RTX_FRAME_RELATED_P (push_rtx) = 1;
485 offset = offset + 4;
486 par_index++;
487 }
488
489 /* Create (set sp sp-x). */
490
491 /* We need to re-calculate the offset value again for adjustment. */
492 offset = -(num_use_regs * 4);
493 adjust_sp_rtx
494 = gen_rtx_SET (VOIDmode,
495 stack_pointer_rtx,
496 plus_constant (Pmode, stack_pointer_rtx, offset));
497 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
498 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
499
4e9a2848
CJW
500 parallel_insn = emit_insn (parallel_insn);
501
502 /* The insn rtx 'parallel_insn' will change frame layout.
503 We need to use RTX_FRAME_RELATED_P so that GCC is able to
504 generate CFI (Call Frame Information) stuff. */
505 RTX_FRAME_RELATED_P (parallel_insn) = 1;
9304f876
CJW
506}
507
508/* Function to create a parallel rtx pattern
509 which presents stack pop multiple behavior.
510 The overall concept are:
511 "pop registers from memory",
512 "adjust stack pointer". */
4e9a2848
CJW
513static void
514nds32_emit_stack_pop_multiple (rtx Rb, rtx Re, rtx En4)
9304f876
CJW
515{
516 int regno;
517 int extra_count;
518 int num_use_regs;
519 int par_index;
520 int offset;
4e9a2848 521 int save_fp, save_gp, save_lp;
9304f876
CJW
522
523 rtx reg;
524 rtx mem;
525 rtx pop_rtx;
526 rtx adjust_sp_rtx;
527 rtx parallel_insn;
4e9a2848 528 rtx dwarf = NULL_RTX;
9304f876
CJW
529
530 /* We need to provide a customized rtx which contains
531 necessary information for data analysis,
532 so we create a parallel rtx like this:
533 (parallel [(set (reg:SI Rb)
534 (mem (reg:SI SP_REGNUM)))
535 (set (reg:SI Rb+1)
536 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
537 ...
538 (set (reg:SI Re)
539 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
540 (set (reg:SI FP_REGNUM)
541 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
542 (set (reg:SI GP_REGNUM)
543 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
544 (set (reg:SI LP_REGNUM)
545 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
546 (set (reg:SI SP_REGNUM)
547 (plus (reg:SI SP_REGNUM) (const_int 32)))]) */
548
4e9a2848
CJW
549 /* Determine whether we need to restore $fp, $gp, or $lp. */
550 save_fp = INTVAL (En4) & 0x8;
551 save_gp = INTVAL (En4) & 0x4;
552 save_lp = INTVAL (En4) & 0x2;
553
9304f876
CJW
554 /* Calculate the number of registers that will be poped. */
555 extra_count = 0;
4e9a2848 556 if (save_fp)
9304f876 557 extra_count++;
4e9a2848 558 if (save_gp)
9304f876 559 extra_count++;
4e9a2848 560 if (save_lp)
9304f876
CJW
561 extra_count++;
562 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
563 if (REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM)
564 num_use_regs = extra_count;
565 else
566 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + extra_count;
567
568 /* In addition to used registers,
569 we need one more space for (set sp sp+x) rtx. */
570 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
571 rtvec_alloc (num_use_regs + 1));
572 par_index = 0;
573
574 /* Initialize offset and start to create pop behavior. */
575 offset = 0;
576
577 /* Create (set regX mem) from Rb, Rb+1 up to Re. */
578 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
579 {
580 /* Rb and Re may be SP_REGNUM.
581 We need to break this loop immediately. */
582 if (regno == SP_REGNUM)
4e9a2848 583 break;
9304f876
CJW
584
585 reg = gen_rtx_REG (SImode, regno);
586 mem = gen_frame_mem (SImode, plus_constant (Pmode,
587 stack_pointer_rtx,
588 offset));
589 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
590 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
591 RTX_FRAME_RELATED_P (pop_rtx) = 1;
592 offset = offset + 4;
593 par_index++;
4e9a2848
CJW
594
595 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
596 }
597
598 /* Create (set fp mem), (set gp mem), and (set lp mem) if necessary. */
4e9a2848 599 if (save_fp)
9304f876
CJW
600 {
601 reg = gen_rtx_REG (SImode, FP_REGNUM);
602 mem = gen_frame_mem (SImode, plus_constant (Pmode,
603 stack_pointer_rtx,
604 offset));
605 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
606 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
607 RTX_FRAME_RELATED_P (pop_rtx) = 1;
608 offset = offset + 4;
609 par_index++;
4e9a2848
CJW
610
611 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876 612 }
4e9a2848 613 if (save_gp)
9304f876
CJW
614 {
615 reg = gen_rtx_REG (SImode, GP_REGNUM);
616 mem = gen_frame_mem (SImode, plus_constant (Pmode,
617 stack_pointer_rtx,
618 offset));
619 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
620 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
621 RTX_FRAME_RELATED_P (pop_rtx) = 1;
622 offset = offset + 4;
623 par_index++;
4e9a2848
CJW
624
625 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876 626 }
4e9a2848 627 if (save_lp)
9304f876
CJW
628 {
629 reg = gen_rtx_REG (SImode, LP_REGNUM);
630 mem = gen_frame_mem (SImode, plus_constant (Pmode,
631 stack_pointer_rtx,
632 offset));
633 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
634 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
635 RTX_FRAME_RELATED_P (pop_rtx) = 1;
636 offset = offset + 4;
637 par_index++;
4e9a2848
CJW
638
639 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
640 }
641
642 /* Create (set sp sp+x). */
643
644 /* The offset value is already in place. No need to re-calculate it. */
645 adjust_sp_rtx
646 = gen_rtx_SET (VOIDmode,
647 stack_pointer_rtx,
648 plus_constant (Pmode, stack_pointer_rtx, offset));
649 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
9304f876 650
4e9a2848
CJW
651 /* Tell gcc we adjust SP in this insn. */
652 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
653
654 parallel_insn = emit_insn (parallel_insn);
655
656 /* The insn rtx 'parallel_insn' will change frame layout.
657 We need to use RTX_FRAME_RELATED_P so that GCC is able to
658 generate CFI (Call Frame Information) stuff. */
659 RTX_FRAME_RELATED_P (parallel_insn) = 1;
660
661 /* Add CFI info by manual. */
662 REG_NOTES (parallel_insn) = dwarf;
9304f876
CJW
663}
664
665/* Function to create a parallel rtx pattern
666 which presents stack v3push behavior.
667 The overall concept are:
668 "push registers to memory",
669 "adjust stack pointer". */
88437f39
CJW
670static void
671nds32_emit_stack_v3push (rtx Rb,
672 rtx Re,
673 rtx En4 ATTRIBUTE_UNUSED,
674 rtx imm8u)
9304f876
CJW
675{
676 int regno;
677 int num_use_regs;
678 int par_index;
679 int offset;
680
681 rtx reg;
682 rtx mem;
683 rtx push_rtx;
684 rtx adjust_sp_rtx;
685 rtx parallel_insn;
686
687 /* We need to provide a customized rtx which contains
688 necessary information for data analysis,
689 so we create a parallel rtx like this:
88437f39 690 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
9304f876
CJW
691 (reg:SI Rb))
692 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
693 (reg:SI Rb+1))
694 ...
695 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
696 (reg:SI Re))
697 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
698 (reg:SI FP_REGNUM))
699 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
700 (reg:SI GP_REGNUM))
701 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
702 (reg:SI LP_REGNUM))
703 (set (reg:SI SP_REGNUM)
704 (plus (reg:SI SP_REGNUM) (const_int -32-imm8u)))]) */
705
706 /* Calculate the number of registers that will be pushed.
707 Since $fp, $gp, and $lp is always pushed with v3push instruction,
708 we need to count these three registers.
709 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
710 So there is no need to worry about Rb=Re=SP_REGNUM case. */
711 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + 3;
712
713 /* In addition to used registers,
714 we need one more space for (set sp sp-x-imm8u) rtx. */
715 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
716 rtvec_alloc (num_use_regs + 1));
717 par_index = 0;
718
719 /* Initialize offset and start to create push behavior. */
720 offset = -(num_use_regs * 4);
721
722 /* Create (set mem regX) from Rb, Rb+1 up to Re.
723 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
724 So there is no need to worry about Rb=Re=SP_REGNUM case. */
725 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
726 {
727 reg = gen_rtx_REG (SImode, regno);
728 mem = gen_frame_mem (SImode, plus_constant (Pmode,
729 stack_pointer_rtx,
730 offset));
731 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
732 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
733 RTX_FRAME_RELATED_P (push_rtx) = 1;
734 offset = offset + 4;
735 par_index++;
736 }
737
738 /* Create (set mem fp). */
739 reg = gen_rtx_REG (SImode, FP_REGNUM);
740 mem = gen_frame_mem (SImode, plus_constant (Pmode,
741 stack_pointer_rtx,
742 offset));
743 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
744 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
745 RTX_FRAME_RELATED_P (push_rtx) = 1;
746 offset = offset + 4;
747 par_index++;
748 /* Create (set mem gp). */
749 reg = gen_rtx_REG (SImode, GP_REGNUM);
750 mem = gen_frame_mem (SImode, plus_constant (Pmode,
751 stack_pointer_rtx,
752 offset));
753 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
754 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
755 RTX_FRAME_RELATED_P (push_rtx) = 1;
756 offset = offset + 4;
757 par_index++;
758 /* Create (set mem lp). */
759 reg = gen_rtx_REG (SImode, LP_REGNUM);
760 mem = gen_frame_mem (SImode, plus_constant (Pmode,
761 stack_pointer_rtx,
762 offset));
763 push_rtx = gen_rtx_SET (VOIDmode, mem, reg);
764 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
765 RTX_FRAME_RELATED_P (push_rtx) = 1;
766 offset = offset + 4;
767 par_index++;
768
769 /* Create (set sp sp-x-imm8u). */
770
771 /* We need to re-calculate the offset value again for adjustment. */
772 offset = -(num_use_regs * 4);
773 adjust_sp_rtx
774 = gen_rtx_SET (VOIDmode,
775 stack_pointer_rtx,
776 plus_constant (Pmode,
777 stack_pointer_rtx,
778 offset - INTVAL (imm8u)));
779 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
780 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
781
88437f39
CJW
782 parallel_insn = emit_insn (parallel_insn);
783
784 /* The insn rtx 'parallel_insn' will change frame layout.
785 We need to use RTX_FRAME_RELATED_P so that GCC is able to
786 generate CFI (Call Frame Information) stuff. */
787 RTX_FRAME_RELATED_P (parallel_insn) = 1;
9304f876
CJW
788}
789
790/* Function to create a parallel rtx pattern
791 which presents stack v3pop behavior.
792 The overall concept are:
793 "pop registers from memory",
794 "adjust stack pointer". */
88437f39
CJW
795static void
796nds32_emit_stack_v3pop (rtx Rb,
797 rtx Re,
798 rtx En4 ATTRIBUTE_UNUSED,
799 rtx imm8u)
9304f876
CJW
800{
801 int regno;
802 int num_use_regs;
803 int par_index;
804 int offset;
805
806 rtx reg;
807 rtx mem;
808 rtx pop_rtx;
809 rtx adjust_sp_rtx;
810 rtx parallel_insn;
88437f39 811 rtx dwarf = NULL_RTX;
9304f876
CJW
812
813 /* We need to provide a customized rtx which contains
814 necessary information for data analysis,
815 so we create a parallel rtx like this:
816 (parallel [(set (reg:SI Rb)
817 (mem (reg:SI SP_REGNUM)))
818 (set (reg:SI Rb+1)
819 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
820 ...
821 (set (reg:SI Re)
822 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
823 (set (reg:SI FP_REGNUM)
824 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
825 (set (reg:SI GP_REGNUM)
826 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
827 (set (reg:SI LP_REGNUM)
828 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
829 (set (reg:SI SP_REGNUM)
830 (plus (reg:SI SP_REGNUM) (const_int 32+imm8u)))]) */
831
832 /* Calculate the number of registers that will be poped.
833 Since $fp, $gp, and $lp is always poped with v3pop instruction,
834 we need to count these three registers.
835 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
836 So there is no need to worry about Rb=Re=SP_REGNUM case. */
837 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + 3;
838
839 /* In addition to used registers,
840 we need one more space for (set sp sp+x+imm8u) rtx. */
841 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
842 rtvec_alloc (num_use_regs + 1));
843 par_index = 0;
844
845 /* Initialize offset and start to create pop behavior. */
846 offset = 0;
847
848 /* Create (set regX mem) from Rb, Rb+1 up to Re.
849 Under v3pop, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
850 So there is no need to worry about Rb=Re=SP_REGNUM case. */
851 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
852 {
853 reg = gen_rtx_REG (SImode, regno);
854 mem = gen_frame_mem (SImode, plus_constant (Pmode,
855 stack_pointer_rtx,
856 offset));
857 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
858 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
859 RTX_FRAME_RELATED_P (pop_rtx) = 1;
860 offset = offset + 4;
861 par_index++;
88437f39
CJW
862
863 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
864 }
865
866 /* Create (set fp mem). */
867 reg = gen_rtx_REG (SImode, FP_REGNUM);
868 mem = gen_frame_mem (SImode, plus_constant (Pmode,
869 stack_pointer_rtx,
870 offset));
871 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
872 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
873 RTX_FRAME_RELATED_P (pop_rtx) = 1;
874 offset = offset + 4;
875 par_index++;
88437f39
CJW
876 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
877
9304f876
CJW
878 /* Create (set gp mem). */
879 reg = gen_rtx_REG (SImode, GP_REGNUM);
880 mem = gen_frame_mem (SImode, plus_constant (Pmode,
881 stack_pointer_rtx,
882 offset));
883 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
884 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
885 RTX_FRAME_RELATED_P (pop_rtx) = 1;
886 offset = offset + 4;
887 par_index++;
88437f39
CJW
888 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
889
9304f876
CJW
890 /* Create (set lp mem ). */
891 reg = gen_rtx_REG (SImode, LP_REGNUM);
892 mem = gen_frame_mem (SImode, plus_constant (Pmode,
893 stack_pointer_rtx,
894 offset));
895 pop_rtx = gen_rtx_SET (VOIDmode, reg, mem);
896 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
897 RTX_FRAME_RELATED_P (pop_rtx) = 1;
898 offset = offset + 4;
899 par_index++;
88437f39 900 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
901
902 /* Create (set sp sp+x+imm8u). */
903
904 /* The offset value is already in place. No need to re-calculate it. */
905 adjust_sp_rtx
906 = gen_rtx_SET (VOIDmode,
907 stack_pointer_rtx,
908 plus_constant (Pmode,
909 stack_pointer_rtx,
910 offset + INTVAL (imm8u)));
911 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
9304f876 912
88437f39
CJW
913 /* Tell gcc we adjust SP in this insn. */
914 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
915
916 parallel_insn = emit_insn (parallel_insn);
917
918 /* The insn rtx 'parallel_insn' will change frame layout.
919 We need to use RTX_FRAME_RELATED_P so that GCC is able to
920 generate CFI (Call Frame Information) stuff. */
921 RTX_FRAME_RELATED_P (parallel_insn) = 1;
922
923 /* Add CFI info by manual. */
924 REG_NOTES (parallel_insn) = dwarf;
9304f876
CJW
925}
926
9304f876
CJW
927/* Function that may creates more instructions
928 for large value on adjusting stack pointer.
929
930 In nds32 target, 'addi' can be used for stack pointer
931 adjustment in prologue/epilogue stage.
932 However, sometimes there are too many local variables so that
933 the adjustment value is not able to be fit in the 'addi' instruction.
934 One solution is to move value into a register
935 and then use 'add' instruction.
936 In practice, we use TA_REGNUM ($r15) to accomplish this purpose.
937 Also, we need to return zero for sp adjustment so that
938 proglogue/epilogue knows there is no need to create 'addi' instruction. */
939static int
940nds32_force_addi_stack_int (int full_value)
941{
942 int adjust_value;
943
944 rtx tmp_reg;
945 rtx sp_adjust_insn;
946
947 if (!satisfies_constraint_Is15 (GEN_INT (full_value)))
948 {
949 /* The value is not able to fit in single addi instruction.
950 Create more instructions of moving value into a register
951 and then add stack pointer with it. */
952
953 /* $r15 is going to be temporary register to hold the value. */
954 tmp_reg = gen_rtx_REG (SImode, TA_REGNUM);
955
956 /* Create one more instruction to move value
957 into the temporary register. */
958 emit_move_insn (tmp_reg, GEN_INT (full_value));
959
960 /* Create new 'add' rtx. */
961 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
962 stack_pointer_rtx,
963 tmp_reg);
964 /* Emit rtx into insn list and receive its transformed insn rtx. */
965 sp_adjust_insn = emit_insn (sp_adjust_insn);
966
967 /* At prologue, we need to tell GCC that this is frame related insn,
968 so that we can consider this instruction to output debug information.
969 If full_value is NEGATIVE, it means this function
970 is invoked by expand_prologue. */
971 if (full_value < 0)
972 {
973 /* Because (tmp_reg <- full_value) may be split into two
974 rtl patterns, we can not set its RTX_FRAME_RELATED_P.
975 We need to construct another (sp <- sp + full_value)
976 and then insert it into sp_adjust_insn's reg note to
977 represent a frame related expression.
978 GCC knows how to refer it and output debug information. */
979
980 rtx plus_rtx;
981 rtx set_rtx;
982
983 plus_rtx = plus_constant (Pmode, stack_pointer_rtx, full_value);
984 set_rtx = gen_rtx_SET (VOIDmode, stack_pointer_rtx, plus_rtx);
985 add_reg_note (sp_adjust_insn, REG_FRAME_RELATED_EXPR, set_rtx);
986
987 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
988 }
989
990 /* We have used alternative way to adjust stack pointer value.
991 Return zero so that prologue/epilogue
992 will not generate other instructions. */
993 return 0;
994 }
995 else
996 {
997 /* The value is able to fit in addi instruction.
998 However, remember to make it to be positive value
999 because we want to return 'adjustment' result. */
1000 adjust_value = (full_value < 0) ? (-full_value) : (full_value);
1001
1002 return adjust_value;
1003 }
1004}
1005
1006/* Return true if MODE/TYPE need double word alignment. */
1007static bool
1008nds32_needs_double_word_align (enum machine_mode mode, const_tree type)
1009{
1010 unsigned int align;
1011
634bdae9
CJW
1012 /* Pick up the alignment according to the mode or type. */
1013 align = NDS32_MODE_TYPE_ALIGN (mode, type);
9304f876
CJW
1014
1015 return (align > PARM_BOUNDARY);
1016}
1017
1018/* Return true if FUNC is a naked function. */
810f736f
CJW
1019static bool
1020nds32_naked_function_p (tree func)
9304f876
CJW
1021{
1022 tree t;
1023
1024 if (TREE_CODE (func) != FUNCTION_DECL)
1025 abort ();
1026
1027 t = lookup_attribute ("naked", DECL_ATTRIBUTES (func));
1028
1029 return (t != NULL_TREE);
1030}
1031
1032/* Function that check if 'X' is a valid address register.
1033 The variable 'STRICT' is very important to
1034 make decision for register number.
1035
1036 STRICT : true
1037 => We are in reload pass or after reload pass.
1038 The register number should be strictly limited in general registers.
1039
1040 STRICT : false
1041 => Before reload pass, we are free to use any register number. */
1042static bool
1043nds32_address_register_rtx_p (rtx x, bool strict)
1044{
1045 int regno;
1046
1047 if (GET_CODE (x) != REG)
1048 return false;
1049
1050 regno = REGNO (x);
1051
1052 if (strict)
1053 return REGNO_OK_FOR_BASE_P (regno);
1054 else
1055 return true;
1056}
1057
1058/* Function that check if 'INDEX' is valid to be a index rtx for address.
1059
1060 OUTER_MODE : Machine mode of outer address rtx.
1061 INDEX : Check if this rtx is valid to be a index for address.
1062 STRICT : If it is true, we are in reload pass or after reload pass. */
1063static bool
1064nds32_legitimate_index_p (enum machine_mode outer_mode,
1065 rtx index,
1066 bool strict)
1067{
1068 int regno;
1069 rtx op0;
1070 rtx op1;
1071
1072 switch (GET_CODE (index))
1073 {
1074 case REG:
1075 regno = REGNO (index);
1076 /* If we are in reload pass or after reload pass,
1077 we need to limit it to general register. */
1078 if (strict)
1079 return REGNO_OK_FOR_INDEX_P (regno);
1080 else
1081 return true;
1082
1083 case CONST_INT:
1084 /* The alignment of the integer value is determined by 'outer_mode'. */
1085 if (GET_MODE_SIZE (outer_mode) == 1)
1086 {
1087 /* Further check if the value is legal for the 'outer_mode'. */
1088 if (!satisfies_constraint_Is15 (index))
1089 return false;
1090
1091 /* Pass all test, the value is valid, return true. */
1092 return true;
1093 }
1094 if (GET_MODE_SIZE (outer_mode) == 2
1095 && NDS32_HALF_WORD_ALIGN_P (INTVAL (index)))
1096 {
1097 /* Further check if the value is legal for the 'outer_mode'. */
1098 if (!satisfies_constraint_Is16 (index))
1099 return false;
1100
1101 /* Pass all test, the value is valid, return true. */
1102 return true;
1103 }
1104 if (GET_MODE_SIZE (outer_mode) == 4
1105 && NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1106 {
1107 /* Further check if the value is legal for the 'outer_mode'. */
1108 if (!satisfies_constraint_Is17 (index))
1109 return false;
1110
1111 /* Pass all test, the value is valid, return true. */
1112 return true;
1113 }
1114 if (GET_MODE_SIZE (outer_mode) == 8
1115 && NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1116 {
1117 /* Further check if the value is legal for the 'outer_mode'. */
1118 if (!satisfies_constraint_Is17 (gen_int_mode (INTVAL (index) + 4,
1119 SImode)))
1120 return false;
1121
1122 /* Pass all test, the value is valid, return true. */
1123 return true;
1124 }
1125
1126 return false;
1127
1128 case MULT:
1129 op0 = XEXP (index, 0);
1130 op1 = XEXP (index, 1);
1131
1132 if (REG_P (op0) && CONST_INT_P (op1))
1133 {
1134 int multiplier;
1135 multiplier = INTVAL (op1);
1136
1137 /* We only allow (mult reg const_int_1)
1138 or (mult reg const_int_2) or (mult reg const_int_4). */
1139 if (multiplier != 1 && multiplier != 2 && multiplier != 4)
1140 return false;
1141
1142 regno = REGNO (op0);
1143 /* Limit it in general registers if we are
1144 in reload pass or after reload pass. */
1145 if(strict)
1146 return REGNO_OK_FOR_INDEX_P (regno);
1147 else
1148 return true;
1149 }
1150
1151 return false;
1152
1153 case ASHIFT:
1154 op0 = XEXP (index, 0);
1155 op1 = XEXP (index, 1);
1156
1157 if (REG_P (op0) && CONST_INT_P (op1))
1158 {
1159 int sv;
1160 /* op1 is already the sv value for use to do left shift. */
1161 sv = INTVAL (op1);
1162
1163 /* We only allow (ashift reg const_int_0)
1164 or (ashift reg const_int_1) or (ashift reg const_int_2). */
1165 if (sv != 0 && sv != 1 && sv !=2)
1166 return false;
1167
1168 regno = REGNO (op0);
1169 /* Limit it in general registers if we are
1170 in reload pass or after reload pass. */
1171 if(strict)
1172 return REGNO_OK_FOR_INDEX_P (regno);
1173 else
1174 return true;
1175 }
1176
1177 return false;
1178
1179 default:
1180 return false;
1181 }
1182}
1183
9304f876
CJW
1184/* ------------------------------------------------------------------------ */
1185
1186/* PART 3: Implement target hook stuff definitions. */
1187\f
1188/* Register Classes. */
1189
1190static unsigned char
1191nds32_class_max_nregs (reg_class_t rclass ATTRIBUTE_UNUSED,
1192 enum machine_mode mode)
1193{
1194 /* Return the maximum number of consecutive registers
1195 needed to represent "mode" in a register of "rclass". */
1196 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1197}
1198
1199static int
1200nds32_register_priority (int hard_regno)
1201{
1202 /* Encourage to use r0-r7 for LRA when optimize for size. */
1203 if (optimize_size && hard_regno < 8)
1204 return 4;
1205 return 3;
1206}
1207
1208\f
1209/* Stack Layout and Calling Conventions. */
1210
1211/* There are three kinds of pointer concepts using in GCC compiler:
1212
1213 frame pointer: A pointer to the first location of local variables.
1214 stack pointer: A pointer to the top of a stack frame.
1215 argument pointer: A pointer to the incoming arguments.
1216
1217 In nds32 target calling convention, we are using 8-byte alignment.
1218 Besides, we would like to have each stack frame of a function includes:
1219
1220 [Block A]
1221 1. previous hard frame pointer
1222 2. return address
1223 3. callee-saved registers
1224 4. <padding bytes> (we will calculte in nds32_compute_stack_frame()
1225 and save it at
1226 cfun->machine->callee_saved_area_padding_bytes)
1227
1228 [Block B]
1229 1. local variables
1230 2. spilling location
1231 3. <padding bytes> (it will be calculated by GCC itself)
1232 4. incoming arguments
1233 5. <padding bytes> (it will be calculated by GCC itself)
1234
1235 [Block C]
1236 1. <padding bytes> (it will be calculated by GCC itself)
1237 2. outgoing arguments
1238
1239 We 'wrap' these blocks together with
1240 hard frame pointer ($r28) and stack pointer ($r31).
1241 By applying the basic frame/stack/argument pointers concept,
1242 the layout of a stack frame shoule be like this:
1243
1244 | |
1245 old stack pointer -> ----
1246 | | \
1247 | | saved arguments for
1248 | | vararg functions
1249 | | /
1250 hard frame pointer -> --
1251 & argument pointer | | \
1252 | | previous hardware frame pointer
1253 | | return address
1254 | | callee-saved registers
1255 | | /
1256 frame pointer -> --
1257 | | \
1258 | | local variables
1259 | | and incoming arguments
1260 | | /
1261 --
1262 | | \
1263 | | outgoing
1264 | | arguments
1265 | | /
1266 stack pointer -> ----
1267
1268 $SFP and $AP are used to represent frame pointer and arguments pointer,
1269 which will be both eliminated as hard frame pointer. */
1270
1271/* -- Eliminating Frame Pointer and Arg Pointer. */
1272
19ac960a
CJW
1273static bool
1274nds32_can_eliminate (const int from_reg, const int to_reg)
9304f876
CJW
1275{
1276 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1277 return true;
1278
1279 if (from_reg == ARG_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1280 return true;
1281
1282 if (from_reg == FRAME_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1283 return true;
1284
1285 if (from_reg == FRAME_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1286 return true;
1287
1288 return false;
1289}
1290
1291/* -- Passing Arguments in Registers. */
1292
1293static rtx
1294nds32_function_arg (cumulative_args_t ca, enum machine_mode mode,
1295 const_tree type, bool named)
1296{
1297 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1298
1299 /* The last time this hook is called,
1300 it is called with MODE == VOIDmode. */
1301 if (mode == VOIDmode)
1302 return NULL_RTX;
1303
1304 /* For nameless arguments, they are passed on the stack. */
1305 if (!named)
1306 return NULL_RTX;
1307
1308 /* If there are still registers available, return it. */
9d93cc24 1309 if (NDS32_ARG_PASS_IN_REG_P (cum->gpr_offset, mode, type))
9304f876
CJW
1310 {
1311 /* Pick up the next available register number. */
634bdae9
CJW
1312 unsigned int regno;
1313
9d93cc24 1314 regno = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
634bdae9 1315 return gen_rtx_REG (mode, regno);
9304f876
CJW
1316 }
1317 else
1318 {
1319 /* No register available, return NULL_RTX.
1320 The compiler will use stack to pass argument instead. */
1321 return NULL_RTX;
1322 }
1323}
1324
1325static void
1326nds32_function_arg_advance (cumulative_args_t ca, enum machine_mode mode,
1327 const_tree type, bool named)
1328{
1329 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1330
1331 /* Advance next register for use.
1332 Only named argument could be advanced. */
1333 if (named)
1334 {
9d93cc24
CJW
1335 cum->gpr_offset
1336 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
9304f876
CJW
1337 - NDS32_GPR_ARG_FIRST_REGNUM
1338 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1339 }
1340}
1341
1342static unsigned int
1343nds32_function_arg_boundary (enum machine_mode mode, const_tree type)
1344{
1345 return (nds32_needs_double_word_align (mode, type)
1346 ? NDS32_DOUBLE_WORD_ALIGNMENT
1347 : PARM_BOUNDARY);
1348}
1349
1350/* -- How Scalar Function Values Are Returned. */
1351
1352static rtx
1353nds32_function_value (const_tree ret_type,
1354 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1355 bool outgoing ATTRIBUTE_UNUSED)
1356{
1357 enum machine_mode mode;
1358 int unsignedp;
1359
1360 mode = TYPE_MODE (ret_type);
1361 unsignedp = TYPE_UNSIGNED (ret_type);
1362
1363 mode = promote_mode (ret_type, mode, &unsignedp);
1364
1365 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
1366}
1367
1368static rtx
1369nds32_libcall_value (enum machine_mode mode,
1370 const_rtx fun ATTRIBUTE_UNUSED)
1371{
1372 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
1373}
1374
1375static bool
1376nds32_function_value_regno_p (const unsigned int regno)
1377{
1378 return (regno == NDS32_GPR_RET_FIRST_REGNUM);
1379}
1380
1381/* -- Function Entry and Exit. */
1382
1383/* The content produced from this function
1384 will be placed before prologue body. */
1385static void
1386nds32_asm_function_prologue (FILE *file,
1387 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1388{
1389 int r;
1390 const char *func_name;
1391 tree attrs;
1392 tree name;
1393
1394 /* All stack frame information is supposed to be
1395 already computed when expanding prologue.
1396 The result is in cfun->machine.
1397 DO NOT call nds32_compute_stack_frame() here
1398 because it may corrupt the essential information. */
1399
1400 fprintf (file, "\t! BEGIN PROLOGUE\n");
1401 fprintf (file, "\t! fp needed: %d\n", frame_pointer_needed);
1402 fprintf (file, "\t! pretend_args: %d\n", cfun->machine->va_args_size);
1403 fprintf (file, "\t! local_size: %d\n", cfun->machine->local_size);
1404 fprintf (file, "\t! out_args_size: %d\n", cfun->machine->out_args_size);
1405
1406 /* Use df_regs_ever_live_p() to detect if the register
1407 is ever used in the current function. */
1408 fprintf (file, "\t! registers ever_live: ");
1409 for (r = 0; r < 32; r++)
1410 {
1411 if (df_regs_ever_live_p (r))
1412 fprintf (file, "%s, ", reg_names[r]);
1413 }
1414 fputc ('\n', file);
1415
1416 /* Display the attributes of this function. */
1417 fprintf (file, "\t! function attributes: ");
f2dafb91
CJW
1418 /* Get the attributes tree list.
1419 Note that GCC builds attributes list with reverse order. */
1420 attrs = DECL_ATTRIBUTES (current_function_decl);
9304f876
CJW
1421
1422 /* If there is no any attribute, print out "None". */
1423 if (!attrs)
1424 fprintf (file, "None");
1425
1426 /* If there are some attributes, try if we need to
1427 construct isr vector information. */
1428 func_name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
1429 nds32_construct_isr_vectors_information (attrs, func_name);
1430
1431 /* Display all attributes of this function. */
1432 while (attrs)
1433 {
1434 name = TREE_PURPOSE (attrs);
1435 fprintf (file, "%s ", IDENTIFIER_POINTER (name));
1436
1437 /* Pick up the next attribute. */
1438 attrs = TREE_CHAIN (attrs);
1439 }
1440 fputc ('\n', file);
1441}
1442
1443/* After rtl prologue has been expanded, this function is used. */
1444static void
1445nds32_asm_function_end_prologue (FILE *file)
1446{
1447 fprintf (file, "\t! END PROLOGUE\n");
1448
1449 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
1450 we can generate special directive: ".omit_fp_begin"
1451 to guide linker doing fp-as-gp optimization.
1452 However, for a naked function, which means
1453 it should not have prologue/epilogue,
1454 using fp-as-gp still requires saving $fp by push/pop behavior and
1455 there is no benefit to use fp-as-gp on such small function.
1456 So we need to make sure this function is NOT naked as well. */
1457 if (!frame_pointer_needed
1458 && !cfun->machine->naked_p
1459 && cfun->machine->fp_as_gp_p)
1460 {
1461 fprintf (file, "\t! ----------------------------------------\n");
1462 fprintf (file, "\t! Guide linker to do "
1463 "link time optimization: fp-as-gp\n");
1464 fprintf (file, "\t! We add one more instruction to "
1465 "initialize $fp near to $gp location.\n");
1466 fprintf (file, "\t! If linker fails to use fp-as-gp transformation,\n");
1467 fprintf (file, "\t! this extra instruction should be "
1468 "eliminated at link stage.\n");
1469 fprintf (file, "\t.omit_fp_begin\n");
1470 fprintf (file, "\tla\t$fp,_FP_BASE_\n");
1471 fprintf (file, "\t! ----------------------------------------\n");
1472 }
1473}
1474
1475/* Before rtl epilogue has been expanded, this function is used. */
1476static void
1477nds32_asm_function_begin_epilogue (FILE *file)
1478{
1479 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
1480 we can generate special directive: ".omit_fp_end"
1481 to claim fp-as-gp optimization range.
1482 However, for a naked function,
1483 which means it should not have prologue/epilogue,
1484 using fp-as-gp still requires saving $fp by push/pop behavior and
1485 there is no benefit to use fp-as-gp on such small function.
1486 So we need to make sure this function is NOT naked as well. */
1487 if (!frame_pointer_needed
1488 && !cfun->machine->naked_p
1489 && cfun->machine->fp_as_gp_p)
1490 {
1491 fprintf (file, "\t! ----------------------------------------\n");
1492 fprintf (file, "\t! Claim the range of fp-as-gp "
1493 "link time optimization\n");
1494 fprintf (file, "\t.omit_fp_end\n");
1495 fprintf (file, "\t! ----------------------------------------\n");
1496 }
1497
1498 fprintf (file, "\t! BEGIN EPILOGUE\n");
1499}
1500
1501/* The content produced from this function
1502 will be placed after epilogue body. */
1503static void
1504nds32_asm_function_epilogue (FILE *file,
1505 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1506{
1507 fprintf (file, "\t! END EPILOGUE\n");
1508}
1509
1510static void
1511nds32_asm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
1512 HOST_WIDE_INT delta,
1513 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
1514 tree function)
1515{
1516 int this_regno;
1517
1518 /* Make sure unwind info is emitted for the thunk if needed. */
1519 final_start_function (emit_barrier (), file, 1);
1520
1521 this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
1522 ? 1
1523 : 0);
1524
1525 if (delta != 0)
1526 {
1527 if (satisfies_constraint_Is15 (GEN_INT (delta)))
1528 {
1529 fprintf (file, "\taddi\t$r%d, $r%d, %ld\n",
1530 this_regno, this_regno, delta);
1531 }
1532 else if (satisfies_constraint_Is20 (GEN_INT (delta)))
1533 {
1534 fprintf (file, "\tmovi\t$ta, %ld\n", delta);
1535 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
1536 }
1537 else
1538 {
1539 fprintf (file, "\tsethi\t$ta, hi20(%ld)\n", delta);
1540 fprintf (file, "\tori\t$ta, $ta, lo12(%ld)\n", delta);
1541 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
1542 }
1543 }
1544
1545 fprintf (file, "\tb\t");
1546 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
1547 fprintf (file, "\n");
1548
1549 final_end_function ();
1550}
1551
1552/* -- Permitting tail calls. */
1553
1554/* Determine whether we need to enable warning for function return check. */
1555static bool
1556nds32_warn_func_return (tree decl)
1557{
1558/* Naked functions are implemented entirely in assembly, including the
1559 return sequence, so suppress warnings about this. */
1560 return !nds32_naked_function_p (decl);
1561}
1562
1563\f
1564/* Implementing the Varargs Macros. */
1565
d4a6a4d9
CJW
1566static void
1567nds32_setup_incoming_varargs (cumulative_args_t ca,
1568 enum machine_mode mode,
1569 tree type,
1570 int *pretend_args_size,
1571 int second_time ATTRIBUTE_UNUSED)
1572{
1573 unsigned int total_args_regs;
1574 unsigned int num_of_used_regs;
1575 unsigned int remaining_reg_count;
1576 CUMULATIVE_ARGS *cum;
1577
1578 /* If we are under hard float abi, we do not need to set *pretend_args_size.
1579 So that all nameless arguments are pushed by caller and all situation
1580 can be handled by GCC itself. */
1581 if (TARGET_HARD_FLOAT)
1582 return;
1583
1584 /* We are using NDS32_MAX_GPR_REGS_FOR_ARGS registers,
1585 counting from NDS32_GPR_ARG_FIRST_REGNUM, for saving incoming arguments.
1586 However, for nameless(anonymous) arguments, we should push them on the
1587 stack so that all the nameless arguments appear to have been passed
1588 consecutively in the memory for accessing. Hence, we need to check and
1589 exclude the registers that are used for named arguments. */
1590
1591 cum = get_cumulative_args (ca);
1592
1593 /* The MODE and TYPE describe the last argument.
1594 We need those information to determine the remaining registers
1595 for varargs. */
1596 total_args_regs
1597 = NDS32_MAX_GPR_REGS_FOR_ARGS + NDS32_GPR_ARG_FIRST_REGNUM;
1598 num_of_used_regs
1599 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1600 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1601
1602 remaining_reg_count = total_args_regs - num_of_used_regs;
1603 *pretend_args_size = remaining_reg_count * UNITS_PER_WORD;
1604
1605 return;
1606}
1607
9304f876
CJW
1608static bool
1609nds32_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
1610{
d4a6a4d9
CJW
1611 /* If this hook returns true, the named argument of FUNCTION_ARG is always
1612 true for named arguments, and false for unnamed arguments. */
9304f876
CJW
1613 return true;
1614}
1615
1616\f
1617/* Trampolines for Nested Functions. */
1618
1619static void
1620nds32_asm_trampoline_template (FILE *f)
1621{
1622 if (TARGET_REDUCED_REGS)
1623 {
1624 /* Trampoline is not supported on reduced-set registers yet. */
1625 sorry ("a nested function is not supported for reduced registers");
1626 }
1627 else
1628 {
1629 asm_fprintf (f, "\t! Trampoline code template\n");
1630 asm_fprintf (f, "\t! This code fragment will be copied "
1631 "into stack on demand\n");
1632
1633 asm_fprintf (f, "\tmfusr\t$r16,$pc\n");
1634 asm_fprintf (f, "\tlwi\t$r15,[$r16 + 20] "
1635 "! load nested function address\n");
1636 asm_fprintf (f, "\tlwi\t$r16,[$r16 + 16] "
1637 "! load chain_value\n");
1638 asm_fprintf (f, "\tjr\t$r15\n");
1639 }
1640
1641 /* Preserve space ($pc + 16) for saving chain_value,
1642 nds32_trampoline_init will fill the value in this slot. */
1643 asm_fprintf (f, "\t! space for saving chain_value\n");
1644 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
1645
1646 /* Preserve space ($pc + 20) for saving nested function address,
1647 nds32_trampoline_init will fill the value in this slot. */
1648 asm_fprintf (f, "\t! space for saving nested function address\n");
1649 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
1650}
1651
1652/* Emit RTL insns to initialize the variable parts of a trampoline. */
1653static void
1654nds32_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1655{
1656 int i;
1657
1658 /* Nested function address. */
1659 rtx fnaddr;
1660 /* The memory rtx that is going to
1661 be filled with chain_value. */
1662 rtx chain_value_mem;
1663 /* The memory rtx that is going to
1664 be filled with nested function address. */
1665 rtx nested_func_mem;
1666
1667 /* Start address of trampoline code in stack, for doing cache sync. */
1668 rtx sync_cache_addr;
1669 /* Temporary register for sync instruction. */
1670 rtx tmp_reg;
1671 /* Instruction-cache sync instruction,
1672 requesting an argument as starting address. */
1673 rtx isync_insn;
1674 /* For convenience reason of doing comparison. */
1675 int tramp_align_in_bytes;
1676
1677 /* Trampoline is not supported on reduced-set registers yet. */
1678 if (TARGET_REDUCED_REGS)
1679 sorry ("a nested function is not supported for reduced registers");
1680
1681 /* STEP 1: Copy trampoline code template into stack,
1682 fill up essential data into stack. */
1683
1684 /* Extract nested function address rtx. */
1685 fnaddr = XEXP (DECL_RTL (fndecl), 0);
1686
1687 /* m_tramp is memory rtx that is going to be filled with trampoline code.
1688 We have nds32_asm_trampoline_template() to emit template pattern. */
1689 emit_block_move (m_tramp, assemble_trampoline_template (),
1690 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
1691
1692 /* After copying trampoline code into stack,
1693 fill chain_value into stack. */
1694 chain_value_mem = adjust_address (m_tramp, SImode, 16);
1695 emit_move_insn (chain_value_mem, chain_value);
1696 /* After copying trampoline code int stack,
1697 fill nested function address into stack. */
1698 nested_func_mem = adjust_address (m_tramp, SImode, 20);
1699 emit_move_insn (nested_func_mem, fnaddr);
1700
1701 /* STEP 2: Sync instruction-cache. */
1702
1703 /* We have successfully filled trampoline code into stack.
1704 However, in order to execute code in stack correctly,
1705 we must sync instruction cache. */
1706 sync_cache_addr = XEXP (m_tramp, 0);
1707 tmp_reg = gen_reg_rtx (SImode);
1708 isync_insn = gen_unspec_volatile_isync (tmp_reg);
1709
1710 /* Because nds32_cache_block_size is in bytes,
1711 we get trampoline alignment in bytes for convenient comparison. */
1712 tramp_align_in_bytes = TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT;
1713
1714 if (tramp_align_in_bytes >= nds32_cache_block_size
1715 && (tramp_align_in_bytes % nds32_cache_block_size) == 0)
1716 {
1717 /* Under this condition, the starting address of trampoline
1718 must be aligned to the starting address of each cache block
1719 and we do not have to worry about cross-boundary issue. */
1720 for (i = 0;
1721 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
1722 / nds32_cache_block_size;
1723 i++)
1724 {
1725 emit_move_insn (tmp_reg,
1726 plus_constant (Pmode, sync_cache_addr,
1727 nds32_cache_block_size * i));
1728 emit_insn (isync_insn);
1729 }
1730 }
1731 else if (TRAMPOLINE_SIZE > nds32_cache_block_size)
1732 {
1733 /* The starting address of trampoline code
1734 may not be aligned to the cache block,
1735 so the trampoline code may be across two cache block.
1736 We need to sync the last element, which is 4-byte size,
1737 of trampoline template. */
1738 for (i = 0;
1739 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
1740 / nds32_cache_block_size;
1741 i++)
1742 {
1743 emit_move_insn (tmp_reg,
1744 plus_constant (Pmode, sync_cache_addr,
1745 nds32_cache_block_size * i));
1746 emit_insn (isync_insn);
1747 }
1748
1749 /* The last element of trampoline template is 4-byte size. */
1750 emit_move_insn (tmp_reg,
1751 plus_constant (Pmode, sync_cache_addr,
1752 TRAMPOLINE_SIZE - 4));
1753 emit_insn (isync_insn);
1754 }
1755 else
1756 {
1757 /* This is the simplest case.
1758 Because TRAMPOLINE_SIZE is less than or
1759 equal to nds32_cache_block_size,
1760 we can just sync start address and
1761 the last element of trampoline code. */
1762
1763 /* Sync starting address of tampoline code. */
1764 emit_move_insn (tmp_reg, sync_cache_addr);
1765 emit_insn (isync_insn);
1766 /* Sync the last element, which is 4-byte size,
1767 of trampoline template. */
1768 emit_move_insn (tmp_reg,
1769 plus_constant (Pmode, sync_cache_addr,
1770 TRAMPOLINE_SIZE - 4));
1771 emit_insn (isync_insn);
1772 }
1773
1774 /* Set instruction serialization barrier
1775 to guarantee the correct operations. */
1776 emit_insn (gen_unspec_volatile_isb ());
1777}
1778
1779\f
1780/* Addressing Modes. */
1781
1782static bool
1783nds32_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1784{
1785 /* For (mem:DI addr) or (mem:DF addr) case,
1786 we only allow 'addr' to be [reg], [symbol_ref],
1787 [const], or [reg + const_int] pattern. */
1788 if (mode == DImode || mode == DFmode)
1789 {
1790 /* Allow [Reg + const_int] addressing mode. */
1791 if (GET_CODE (x) == PLUS)
1792 {
1793 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
1794 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict)
1795 && CONST_INT_P (XEXP (x, 1)))
1796 return true;
1797
1798 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
1799 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict)
1800 && CONST_INT_P (XEXP (x, 0)))
1801 return true;
1802 }
1803
1804 /* Now check [reg], [symbol_ref], and [const]. */
1805 if (GET_CODE (x) != REG
1806 && GET_CODE (x) != SYMBOL_REF
1807 && GET_CODE (x) != CONST)
1808 return false;
1809 }
1810
1811 /* Check if 'x' is a valid address. */
1812 switch (GET_CODE (x))
1813 {
1814 case REG:
1815 /* (mem (reg A)) => [Ra] */
1816 return nds32_address_register_rtx_p (x, strict);
1817
1818 case SYMBOL_REF:
1819
1820 if (!TARGET_GP_DIRECT
1821 && (reload_completed
1822 || reload_in_progress
1823 || lra_in_progress))
1824 return false;
1825
1826 /* (mem (symbol_ref A)) => [symbol_ref] */
1827 return !currently_expanding_to_rtl;
1828
1829 case CONST:
1830
1831 if (!TARGET_GP_DIRECT
1832 && (reload_completed
1833 || reload_in_progress
1834 || lra_in_progress))
1835 return false;
1836
1837 /* (mem (const (...)))
1838 => [ + const_addr ], where const_addr = symbol_ref + const_int */
1839 if (GET_CODE (XEXP (x, 0)) == PLUS)
1840 {
1841 rtx plus_op = XEXP (x, 0);
1842
1843 rtx op0 = XEXP (plus_op, 0);
1844 rtx op1 = XEXP (plus_op, 1);
1845
1846 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
1847 return true;
1848 else
1849 return false;
1850 }
1851
1852 return false;
1853
1854 case POST_MODIFY:
1855 /* (mem (post_modify (reg) (plus (reg) (reg))))
1856 => [Ra], Rb */
1857 /* (mem (post_modify (reg) (plus (reg) (const_int))))
1858 => [Ra], const_int */
1859 if (GET_CODE (XEXP (x, 0)) == REG
1860 && GET_CODE (XEXP (x, 1)) == PLUS)
1861 {
1862 rtx plus_op = XEXP (x, 1);
1863
1864 rtx op0 = XEXP (plus_op, 0);
1865 rtx op1 = XEXP (plus_op, 1);
1866
1867 if (nds32_address_register_rtx_p (op0, strict)
1868 && nds32_legitimate_index_p (mode, op1, strict))
1869 return true;
1870 else
1871 return false;
1872 }
1873
1874 return false;
1875
1876 case POST_INC:
1877 case POST_DEC:
1878 /* (mem (post_inc reg)) => [Ra], 1/2/4 */
1879 /* (mem (post_dec reg)) => [Ra], -1/-2/-4 */
1880 /* The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
1881 We only need to deal with register Ra. */
1882 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
1883 return true;
1884 else
1885 return false;
1886
1887 case PLUS:
1888 /* (mem (plus reg const_int))
1889 => [Ra + imm] */
1890 /* (mem (plus reg reg))
1891 => [Ra + Rb] */
1892 /* (mem (plus (mult reg const_int) reg))
1893 => [Ra + Rb << sv] */
1894 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
1895 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict))
1896 return true;
1897 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
1898 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict))
1899 return true;
1900 else
1901 return false;
1902
1903 case LO_SUM:
1904 if (!TARGET_GP_DIRECT)
1905 return true;
1906
1907 default:
1908 return false;
1909 }
1910}
1911
1912\f
1913/* Describing Relative Costs of Operations. */
1914
19ac960a
CJW
1915static int
1916nds32_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
1917 reg_class_t from,
1918 reg_class_t to)
9304f876
CJW
1919{
1920 if (from == HIGH_REGS || to == HIGH_REGS)
1921 return 6;
1922
1923 return 2;
1924}
1925
19ac960a
CJW
1926static int
1927nds32_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
1928 reg_class_t rclass ATTRIBUTE_UNUSED,
1929 bool in ATTRIBUTE_UNUSED)
9304f876
CJW
1930{
1931 return 8;
1932}
1933
1934/* This target hook describes the relative costs of RTL expressions.
1935 Return 'true' when all subexpressions of x have been processed.
1936 Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
1937 Refer to gcc/rtlanal.c for more information. */
1938static bool
1939nds32_rtx_costs (rtx x,
1940 int code,
1941 int outer_code,
89a4b547 1942 int opno,
9304f876
CJW
1943 int *total,
1944 bool speed)
1945{
89a4b547 1946 return nds32_rtx_costs_impl (x, code, outer_code, opno, total, speed);
9304f876
CJW
1947}
1948
19ac960a
CJW
1949static int
1950nds32_address_cost (rtx address,
1951 enum machine_mode mode,
1952 addr_space_t as,
1953 bool speed)
9304f876 1954{
89a4b547 1955 return nds32_address_cost_impl (address, mode, as, speed);
9304f876
CJW
1956}
1957
1958\f
1959/* Defining the Output Assembler Language. */
1960
1961/* -- The Overall Framework of an Assembler File. */
1962
1963static void
1964nds32_asm_file_start (void)
1965{
9304f876
CJW
1966 default_file_start ();
1967
1968 /* Tell assembler which ABI we are using. */
1969 fprintf (asm_out_file, "\t! ABI version\n");
1970 fprintf (asm_out_file, "\t.abi_2\n");
1971
1972 /* Tell assembler that this asm code is generated by compiler. */
1973 fprintf (asm_out_file, "\t! This asm file is generated by compiler\n");
1974 fprintf (asm_out_file, "\t.flag\tverbatim\n");
1975 /* Give assembler the size of each vector for interrupt handler. */
1976 fprintf (asm_out_file, "\t! This vector size directive is required "
1977 "for checking inconsistency on interrupt handler\n");
1978 fprintf (asm_out_file, "\t.vec_size\t%d\n", nds32_isr_vector_size);
1979
1980 /* If user enables '-mforce-fp-as-gp' or compiles programs with -Os,
1981 the compiler may produce 'la $fp,_FP_BASE_' instruction
1982 at prologue for fp-as-gp optimization.
1983 We should emit weak reference of _FP_BASE_ to avoid undefined reference
1984 in case user does not pass '--relax' option to linker. */
1985 if (TARGET_FORCE_FP_AS_GP || optimize_size)
1986 {
1987 fprintf (asm_out_file, "\t! This weak reference is required to do "
1988 "fp-as-gp link time optimization\n");
1989 fprintf (asm_out_file, "\t.weak\t_FP_BASE_\n");
1990 }
1991 /* If user enables '-mex9', we should emit relaxation directive
1992 to tell linker that this file is allowed to do ex9 optimization. */
1993 if (TARGET_EX9)
1994 {
1995 fprintf (asm_out_file, "\t! This relaxation directive is required "
1996 "to do ex9 link time optimization\n");
1997 fprintf (asm_out_file, "\t.relax\tex9\n");
1998 }
1999
2000 fprintf (asm_out_file, "\t! ------------------------------------\n");
2001
2002 if (TARGET_ISA_V2)
2003 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V2");
2004 if (TARGET_ISA_V3)
2005 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3");
2006 if (TARGET_ISA_V3M)
2007 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3M");
2008
2009 fprintf (asm_out_file, "\t! Endian setting\t: %s\n",
2010 ((TARGET_BIG_ENDIAN) ? "big-endian"
2011 : "little-endian"));
2012
2013 fprintf (asm_out_file, "\t! ------------------------------------\n");
2014
2015 fprintf (asm_out_file, "\t! Use conditional move\t\t: %s\n",
2016 ((TARGET_CMOV) ? "Yes"
2017 : "No"));
2018 fprintf (asm_out_file, "\t! Use performance extension\t: %s\n",
2019 ((TARGET_PERF_EXT) ? "Yes"
2020 : "No"));
2021
2022 fprintf (asm_out_file, "\t! ------------------------------------\n");
2023
2024 fprintf (asm_out_file, "\t! V3PUSH instructions\t: %s\n",
2025 ((TARGET_V3PUSH) ? "Yes"
2026 : "No"));
2027 fprintf (asm_out_file, "\t! 16-bit instructions\t: %s\n",
2028 ((TARGET_16_BIT) ? "Yes"
2029 : "No"));
2030 fprintf (asm_out_file, "\t! GP base access\t: %s\n",
2031 ((TARGET_GP_DIRECT) ? "Yes"
2032 : "No"));
2033 fprintf (asm_out_file, "\t! Reduced registers set\t: %s\n",
2034 ((TARGET_REDUCED_REGS) ? "Yes"
2035 : "No"));
2036
2037 fprintf (asm_out_file, "\t! ------------------------------------\n");
2038
2039 if (optimize_size)
2040 fprintf (asm_out_file, "\t! Optimization level\t: -Os\n");
2041 else
2042 fprintf (asm_out_file, "\t! Optimization level\t: -O%d\n", optimize);
2043
2044 fprintf (asm_out_file, "\t! ------------------------------------\n");
2045
2046 fprintf (asm_out_file, "\t! Cache block size\t: %d\n",
2047 nds32_cache_block_size);
2048
2049 fprintf (asm_out_file, "\t! ------------------------------------\n");
2050
c23a919b 2051 nds32_asm_file_start_for_isr ();
9304f876
CJW
2052}
2053
2054static void
2055nds32_asm_file_end (void)
2056{
c23a919b 2057 nds32_asm_file_end_for_isr ();
9304f876
CJW
2058
2059 fprintf (asm_out_file, "\t! ------------------------------------\n");
2060}
2061
2062/* -- Output and Generation of Labels. */
2063
2064static void
2065nds32_asm_globalize_label (FILE *stream, const char *name)
2066{
2067 fputs ("\t.global\t", stream);
2068 assemble_name (stream, name);
2069 fputs ("\n", stream);
2070}
2071
2072/* -- Output of Assembler Instructions. */
2073
2074static void
2075nds32_print_operand (FILE *stream, rtx x, int code)
2076{
2077 int op_value;
2078
2079 switch (code)
2080 {
2081 case 0 :
2082 /* Do nothing special. */
2083 break;
2084
2085 case 'V':
2086 /* 'x' is supposed to be CONST_INT, get the value. */
2087 gcc_assert (CONST_INT_P (x));
2088 op_value = INTVAL (x);
2089
2090 /* According to the Andes architecture,
2091 the system/user register index range is 0 ~ 1023.
2092 In order to avoid conflict between user-specified-integer value
2093 and enum-specified-register value,
2094 the 'enum nds32_intrinsic_registers' value
2095 in nds32_intrinsic.h starts from 1024. */
2096 if (op_value < 1024 && op_value >= 0)
2097 {
2098 /* If user gives integer value directly (0~1023),
2099 we just print out the value. */
2100 fprintf (stream, "%d", op_value);
2101 }
2102 else if (op_value < 0
2103 || op_value >= ((int) ARRAY_SIZE (nds32_intrinsic_register_names)
2104 + 1024))
2105 {
2106 /* The enum index value for array size is out of range. */
2107 error ("intrinsic register index is out of range");
2108 }
2109 else
2110 {
2111 /* If user applies normal way with __NDS32_REG_XXX__ enum data,
2112 we can print out register name. Remember to substract 1024. */
2113 fprintf (stream, "%s",
2114 nds32_intrinsic_register_names[op_value - 1024]);
2115 }
2116
2117 /* No need to handle following process, so return immediately. */
2118 return;
2119
2120 default :
2121 /* Unknown flag. */
2122 output_operand_lossage ("invalid operand output code");
2123 break;
2124 }
2125
2126 switch (GET_CODE (x))
2127 {
2128 case LABEL_REF:
2129 case SYMBOL_REF:
2130 output_addr_const (stream, x);
2131 break;
2132
2133 case REG:
2134 /* Forbid using static chain register ($r16)
2135 on reduced-set registers configuration. */
2136 if (TARGET_REDUCED_REGS
2137 && REGNO (x) == STATIC_CHAIN_REGNUM)
2138 sorry ("a nested function is not supported for reduced registers");
2139
2140 /* Normal cases, print out register name. */
2141 fputs (reg_names[REGNO (x)], stream);
2142 break;
2143
2144 case MEM:
2145 output_address (XEXP (x, 0));
2146 break;
2147
2148 case CODE_LABEL:
2149 case CONST_INT:
2150 case CONST:
2151 output_addr_const (stream, x);
2152 break;
2153
2154 default:
2155 /* Generally, output_addr_const () is able to handle most cases.
2156 We want to see what CODE could appear,
2157 so we use gcc_unreachable() to stop it. */
2158 debug_rtx (x);
2159 gcc_unreachable ();
2160 break;
2161 }
2162}
2163
2164static void
2165nds32_print_operand_address (FILE *stream, rtx x)
2166{
2167 rtx op0, op1;
2168
2169 switch (GET_CODE (x))
2170 {
2171 case SYMBOL_REF:
2172 case CONST:
2173 /* [ + symbol_ref] */
2174 /* [ + const_addr], where const_addr = symbol_ref + const_int */
2175 fputs ("[ + ", stream);
2176 output_addr_const (stream, x);
2177 fputs ("]", stream);
2178 break;
2179
2180 case REG:
2181 /* Forbid using static chain register ($r16)
2182 on reduced-set registers configuration. */
2183 if (TARGET_REDUCED_REGS
2184 && REGNO (x) == STATIC_CHAIN_REGNUM)
2185 sorry ("a nested function is not supported for reduced registers");
2186
2187 /* [Ra] */
2188 fprintf (stream, "[%s]", reg_names[REGNO (x)]);
2189 break;
2190
2191 case PLUS:
2192 op0 = XEXP (x, 0);
2193 op1 = XEXP (x, 1);
2194
2195 /* Checking op0, forbid using static chain register ($r16)
2196 on reduced-set registers configuration. */
2197 if (TARGET_REDUCED_REGS
2198 && REG_P (op0)
2199 && REGNO (op0) == STATIC_CHAIN_REGNUM)
2200 sorry ("a nested function is not supported for reduced registers");
2201 /* Checking op1, forbid using static chain register ($r16)
2202 on reduced-set registers configuration. */
2203 if (TARGET_REDUCED_REGS
2204 && REG_P (op1)
2205 && REGNO (op1) == STATIC_CHAIN_REGNUM)
2206 sorry ("a nested function is not supported for reduced registers");
2207
2208 if (REG_P (op0) && CONST_INT_P (op1))
2209 {
2210 /* [Ra + imm] */
2211 fprintf (stream, "[%s + (%d)]",
2212 reg_names[REGNO (op0)], (int)INTVAL (op1));
2213 }
2214 else if (REG_P (op0) && REG_P (op1))
2215 {
2216 /* [Ra + Rb] */
2217 fprintf (stream, "[%s + %s]",
2218 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
2219 }
2220 else if (GET_CODE (op0) == MULT && REG_P (op1))
2221 {
2222 /* [Ra + Rb << sv]
2223 From observation, the pattern looks like:
2224 (plus:SI (mult:SI (reg:SI 58)
2225 (const_int 4 [0x4]))
2226 (reg/f:SI 57)) */
2227 int sv;
2228
2229 /* We need to set sv to output shift value. */
2230 if (INTVAL (XEXP (op0, 1)) == 1)
2231 sv = 0;
2232 else if (INTVAL (XEXP (op0, 1)) == 2)
2233 sv = 1;
2234 else if (INTVAL (XEXP (op0, 1)) == 4)
2235 sv = 2;
2236 else
2237 gcc_unreachable ();
2238
2239 fprintf (stream, "[%s + %s << %d]",
2240 reg_names[REGNO (op1)],
2241 reg_names[REGNO (XEXP (op0, 0))],
2242 sv);
2243 }
2244 else
2245 {
2246 /* The control flow is not supposed to be here. */
2247 debug_rtx (x);
2248 gcc_unreachable ();
2249 }
2250
2251 break;
2252
2253 case POST_MODIFY:
2254 /* (post_modify (regA) (plus (regA) (regB)))
2255 (post_modify (regA) (plus (regA) (const_int)))
2256 We would like to extract
2257 regA and regB (or const_int) from plus rtx. */
2258 op0 = XEXP (XEXP (x, 1), 0);
2259 op1 = XEXP (XEXP (x, 1), 1);
2260
2261 /* Checking op0, forbid using static chain register ($r16)
2262 on reduced-set registers configuration. */
2263 if (TARGET_REDUCED_REGS
2264 && REG_P (op0)
2265 && REGNO (op0) == STATIC_CHAIN_REGNUM)
2266 sorry ("a nested function is not supported for reduced registers");
2267 /* Checking op1, forbid using static chain register ($r16)
2268 on reduced-set registers configuration. */
2269 if (TARGET_REDUCED_REGS
2270 && REG_P (op1)
2271 && REGNO (op1) == STATIC_CHAIN_REGNUM)
2272 sorry ("a nested function is not supported for reduced registers");
2273
2274 if (REG_P (op0) && REG_P (op1))
2275 {
2276 /* [Ra], Rb */
2277 fprintf (stream, "[%s], %s",
2278 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
2279 }
2280 else if (REG_P (op0) && CONST_INT_P (op1))
2281 {
2282 /* [Ra], imm */
2283 fprintf (stream, "[%s], %d",
2284 reg_names[REGNO (op0)], (int)INTVAL (op1));
2285 }
2286 else
2287 {
2288 /* The control flow is not supposed to be here. */
2289 debug_rtx (x);
2290 gcc_unreachable ();
2291 }
2292
2293 break;
2294
2295 case POST_INC:
2296 case POST_DEC:
2297 op0 = XEXP (x, 0);
2298
2299 /* Checking op0, forbid using static chain register ($r16)
2300 on reduced-set registers configuration. */
2301 if (TARGET_REDUCED_REGS
2302 && REG_P (op0)
2303 && REGNO (op0) == STATIC_CHAIN_REGNUM)
2304 sorry ("a nested function is not supported for reduced registers");
2305
2306 if (REG_P (op0))
2307 {
2308 /* "[Ra], 1/2/4" or "[Ra], -1/-2/-4"
2309 The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
2310 We only need to deal with register Ra. */
2311 fprintf (stream, "[%s]", reg_names[REGNO (op0)]);
2312 }
2313 else
2314 {
2315 /* The control flow is not supposed to be here. */
2316 debug_rtx (x);
2317 gcc_unreachable ();
2318 }
2319
2320 break;
2321
2322 default :
2323 /* Generally, output_addr_const () is able to handle most cases.
2324 We want to see what CODE could appear,
2325 so we use gcc_unreachable() to stop it. */
2326 debug_rtx (x);
2327 gcc_unreachable ();
2328 break;
2329 }
2330}
2331
2332\f
2333/* Defining target-specific uses of __attribute__. */
2334
2335/* Add some checking after merging attributes. */
2336static tree
2337nds32_merge_decl_attributes (tree olddecl, tree newdecl)
2338{
2339 tree combined_attrs;
2340
2341 /* Create combined attributes. */
2342 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
2343 DECL_ATTRIBUTES (newdecl));
2344
59043e75 2345 /* Since newdecl is acutally a duplicate of olddecl,
9304f876
CJW
2346 we can take olddecl for some operations. */
2347 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2348 {
2349 /* Check isr-specific attributes conflict. */
2350 nds32_check_isr_attrs_conflict (olddecl, combined_attrs);
2351 }
2352
2353 return combined_attrs;
2354}
2355
2356/* Add some checking when inserting attributes. */
2357static void
2358nds32_insert_attributes (tree decl, tree *attributes)
2359{
2360 /* For function declaration, we need to check isr-specific attributes:
2361 1. Call nds32_check_isr_attrs_conflict() to check any conflict.
2362 2. Check valid integer value for interrupt/exception.
2363 3. Check valid integer value for reset.
2364 4. Check valid function for nmi/warm. */
2365 if (TREE_CODE (decl) == FUNCTION_DECL)
2366 {
2367 tree func_attrs;
2368 tree intr, excp, reset;
2369
2370 /* Pick up function attributes. */
2371 func_attrs = *attributes;
2372
2373 /* 1. Call nds32_check_isr_attrs_conflict() to check any conflict. */
2374 nds32_check_isr_attrs_conflict (decl, func_attrs);
2375
2376 /* Now we are starting to check valid id value
2377 for interrupt/exception/reset.
2378 Note that we ONLY check its validity here.
2379 To construct isr vector information, it is still performed
2380 by nds32_construct_isr_vectors_information(). */
2381 intr = lookup_attribute ("interrupt", func_attrs);
2382 excp = lookup_attribute ("exception", func_attrs);
2383 reset = lookup_attribute ("reset", func_attrs);
2384
2385 if (intr || excp)
2386 {
2387 /* Deal with interrupt/exception. */
2388 tree id_list;
2389 unsigned int lower_bound, upper_bound;
2390
2391 /* The way to handle interrupt or exception is the same,
2392 we just need to take care of actual vector number.
2393 For interrupt(0..63), the actual vector number is (9..72).
2394 For exception(1..8), the actual vector number is (1..8). */
2395 lower_bound = (intr) ? (0) : (1);
2396 upper_bound = (intr) ? (63) : (8);
2397
2398 /* Prepare id list so that we can traverse id value. */
2399 id_list = (intr) ? (TREE_VALUE (intr)) : (TREE_VALUE (excp));
2400
2401 /* 2. Check valid integer value for interrupt/exception. */
2402 while (id_list)
2403 {
2404 tree id;
2405
2406 /* Pick up each vector id value. */
2407 id = TREE_VALUE (id_list);
2408 /* Issue error if it is not a valid integer value. */
2409 if (TREE_CODE (id) != INTEGER_CST
807e902e
KZ
2410 || wi::ltu_p (id, lower_bound)
2411 || wi::gtu_p (id, upper_bound))
9304f876
CJW
2412 error ("invalid id value for interrupt/exception attribute");
2413
2414 /* Advance to next id. */
2415 id_list = TREE_CHAIN (id_list);
2416 }
2417 }
2418 else if (reset)
2419 {
2420 /* Deal with reset. */
2421 tree id_list;
2422 tree id;
2423 tree nmi, warm;
2424 unsigned int lower_bound;
2425 unsigned int upper_bound;
2426
2427 /* Prepare id_list and identify id value so that
2428 we can check if total number of vectors is valid. */
2429 id_list = TREE_VALUE (reset);
2430 id = TREE_VALUE (id_list);
2431
2432 /* The maximum numbers for user's interrupt is 64. */
2433 lower_bound = 0;
2434 upper_bound = 64;
2435
2436 /* 3. Check valid integer value for reset. */
2437 if (TREE_CODE (id) != INTEGER_CST
807e902e
KZ
2438 || wi::ltu_p (id, lower_bound)
2439 || wi::gtu_p (id, upper_bound))
9304f876
CJW
2440 error ("invalid id value for reset attribute");
2441
2442 /* 4. Check valid function for nmi/warm. */
2443 nmi = lookup_attribute ("nmi", func_attrs);
2444 warm = lookup_attribute ("warm", func_attrs);
2445
2446 if (nmi != NULL_TREE)
2447 {
2448 tree nmi_func_list;
2449 tree nmi_func;
2450
2451 nmi_func_list = TREE_VALUE (nmi);
2452 nmi_func = TREE_VALUE (nmi_func_list);
2453
2454 /* Issue error if it is not a valid nmi function. */
2455 if (TREE_CODE (nmi_func) != IDENTIFIER_NODE)
2456 error ("invalid nmi function for reset attribute");
2457 }
2458
2459 if (warm != NULL_TREE)
2460 {
2461 tree warm_func_list;
2462 tree warm_func;
2463
2464 warm_func_list = TREE_VALUE (warm);
2465 warm_func = TREE_VALUE (warm_func_list);
2466
2467 /* Issue error if it is not a valid warm function. */
2468 if (TREE_CODE (warm_func) != IDENTIFIER_NODE)
2469 error ("invalid warm function for reset attribute");
2470 }
2471 }
2472 else
2473 {
2474 /* No interrupt, exception, or reset attribute is set. */
2475 return;
2476 }
2477 }
2478}
2479
2480static bool
2481nds32_option_pragma_parse (tree args ATTRIBUTE_UNUSED,
2482 tree pop_target ATTRIBUTE_UNUSED)
2483{
2484 /* Currently, we do not parse any pragma target by ourself,
2485 so just simply return false. */
2486 return false;
2487}
2488
2489static void
2490nds32_option_override (void)
2491{
2492 /* After all the command options have been parsed,
2493 we shall deal with some flags for changing compiler settings. */
2494
2495 /* At first, we check if we have to strictly
2496 set some flags based on ISA family. */
2497 if (TARGET_ISA_V2)
2498 {
2499 /* Under V2 ISA, we need to strictly disable TARGET_V3PUSH. */
2500 target_flags &= ~MASK_V3PUSH;
2501 }
2502 if (TARGET_ISA_V3)
2503 {
2504 /* Under V3 ISA, currently nothing should be strictly set. */
2505 }
2506 if (TARGET_ISA_V3M)
2507 {
2508 /* Under V3M ISA, we need to strictly enable TARGET_REDUCED_REGS. */
2509 target_flags |= MASK_REDUCED_REGS;
2510 /* Under V3M ISA, we need to strictly disable TARGET_PERF_EXT. */
2511 target_flags &= ~MASK_PERF_EXT;
2512 }
2513
2514 /* See if we are using reduced-set registers:
2515 $r0~$r5, $r6~$r10, $r15, $r28, $r29, $r30, $r31
2516 If so, we must forbid using $r11~$r14, $r16~$r27. */
2517 if (TARGET_REDUCED_REGS)
2518 {
2519 int r;
2520
2521 /* Prevent register allocator from
2522 choosing it as doing register allocation. */
2523 for (r = 11; r <= 14; r++)
2524 fixed_regs[r] = call_used_regs[r] = 1;
2525 for (r = 16; r <= 27; r++)
2526 fixed_regs[r] = call_used_regs[r] = 1;
2527 }
2528
2529 /* See if user explicitly would like to use fp-as-gp optimization.
2530 If so, we must prevent $fp from being allocated
2531 during register allocation. */
2532 if (TARGET_FORCE_FP_AS_GP)
2533 fixed_regs[FP_REGNUM] = call_used_regs[FP_REGNUM] = 1;
2534
2535 if (!TARGET_16_BIT)
2536 {
2537 /* Under no 16 bit ISA, we need to strictly disable TARGET_V3PUSH. */
2538 target_flags &= ~MASK_V3PUSH;
2539 }
2540
2541 /* Currently, we don't support PIC code generation yet. */
2542 if (flag_pic)
2543 sorry ("not support -fpic");
2544}
2545
2546\f
2547/* Miscellaneous Parameters. */
2548
2549static void
2550nds32_init_builtins (void)
2551{
aaa44d2d 2552 nds32_init_builtins_impl ();
9304f876
CJW
2553}
2554
2555static rtx
2556nds32_expand_builtin (tree exp,
2557 rtx target,
aaa44d2d
CJW
2558 rtx subtarget,
2559 enum machine_mode mode,
2560 int ignore)
9304f876 2561{
aaa44d2d 2562 return nds32_expand_builtin_impl (exp, target, subtarget, mode, ignore);
9304f876
CJW
2563}
2564
2565
2566/* ------------------------------------------------------------------------ */
2567
2568/* PART 4: Implemet extern function definitions,
2569 the prototype is in nds32-protos.h. */
2570\f
2571/* Defining Data Structures for Per-function Information. */
2572
2573void
2574nds32_init_expanders (void)
2575{
2576 /* Arrange to initialize and mark the machine per-function status. */
2577 init_machine_status = nds32_init_machine_status;
2578}
2579
2580\f
2581/* Register Usage. */
2582
2583/* -- How Values Fit in Registers. */
2584
2585int
2586nds32_hard_regno_nregs (int regno ATTRIBUTE_UNUSED,
2587 enum machine_mode mode)
2588{
2589 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
2590}
2591
2592int
2593nds32_hard_regno_mode_ok (int regno, enum machine_mode mode)
2594{
2595 /* Restrict double-word quantities to even register pairs. */
2596 if (HARD_REGNO_NREGS (regno, mode) == 1
2597 || !((regno) & 1))
2598 return 1;
2599
2600 return 0;
2601}
2602
2603\f
2604/* Register Classes. */
2605
2606enum reg_class
2607nds32_regno_reg_class (int regno)
2608{
2609 /* Refer to nds32.h for more register class details. */
2610
2611 if (regno >= 0 && regno <= 7)
2612 return LOW_REGS;
2613 else if (regno >= 8 && regno <= 11)
2614 return MIDDLE_REGS;
2615 else if (regno >= 12 && regno <= 14)
2616 return HIGH_REGS;
2617 else if (regno == 15)
2618 return R15_TA_REG;
2619 else if (regno >= 16 && regno <= 19)
2620 return MIDDLE_REGS;
2621 else if (regno >= 20 && regno <= 31)
2622 return HIGH_REGS;
2623 else if (regno == 32 || regno == 33)
2624 return FRAME_REGS;
2625 else
2626 return NO_REGS;
2627}
2628
2629\f
2630/* Stack Layout and Calling Conventions. */
2631
2632/* -- Basic Stack Layout. */
2633
2634rtx
2635nds32_return_addr_rtx (int count,
2636 rtx frameaddr ATTRIBUTE_UNUSED)
2637{
2638 /* There is no way to determine the return address
2639 if frameaddr is the frame that has 'count' steps
2640 up from current frame. */
2641 if (count != 0)
2642 return NULL_RTX;
2643
2644 /* If count == 0, it means we are at current frame,
2645 the return address is $r30 ($lp). */
2646 return get_hard_reg_initial_val (Pmode, LP_REGNUM);
2647}
2648
2649/* -- Eliminating Frame Pointer and Arg Pointer. */
2650
2651HOST_WIDE_INT
2652nds32_initial_elimination_offset (unsigned int from_reg, unsigned int to_reg)
2653{
2654 HOST_WIDE_INT offset;
2655
2656 /* Compute and setup stack frame size.
2657 The result will be in cfun->machine. */
2658 nds32_compute_stack_frame ();
2659
2660 /* Remember to consider
2661 cfun->machine->callee_saved_area_padding_bytes
2662 when calculating offset. */
2663 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
2664 {
2665 offset = (cfun->machine->fp_size
2666 + cfun->machine->gp_size
2667 + cfun->machine->lp_size
2668 + cfun->machine->callee_saved_regs_size
2669 + cfun->machine->callee_saved_area_padding_bytes
2670 + cfun->machine->local_size
2671 + cfun->machine->out_args_size);
2672 }
2673 else if (from_reg == ARG_POINTER_REGNUM
2674 && to_reg == HARD_FRAME_POINTER_REGNUM)
2675 {
2676 offset = 0;
2677 }
2678 else if (from_reg == FRAME_POINTER_REGNUM
2679 && to_reg == STACK_POINTER_REGNUM)
2680 {
2681 offset = (cfun->machine->local_size + cfun->machine->out_args_size);
2682 }
2683 else if (from_reg == FRAME_POINTER_REGNUM
2684 && to_reg == HARD_FRAME_POINTER_REGNUM)
2685 {
2686 offset = (-1) * (cfun->machine->fp_size
2687 + cfun->machine->gp_size
2688 + cfun->machine->lp_size
2689 + cfun->machine->callee_saved_regs_size
2690 + cfun->machine->callee_saved_area_padding_bytes);
2691 }
2692 else
2693 {
2694 gcc_unreachable ();
2695 }
2696
2697 return offset;
2698}
2699
2700/* -- Passing Arguments in Registers. */
2701
2702void
2703nds32_init_cumulative_args (CUMULATIVE_ARGS *cum,
2704 tree fntype ATTRIBUTE_UNUSED,
2705 rtx libname ATTRIBUTE_UNUSED,
2706 tree fndecl ATTRIBUTE_UNUSED,
2707 int n_named_args ATTRIBUTE_UNUSED)
2708{
2709 /* Initial available registers
2710 (in offset, corresponding to NDS32_GPR_ARG_FIRST_REGNUM)
2711 for passing arguments. */
9d93cc24 2712 cum->gpr_offset = 0;
9304f876
CJW
2713}
2714
2715/* -- Function Entry and Exit. */
2716
2717/* Function for normal multiple push prologue. */
2718void
2719nds32_expand_prologue (void)
2720{
2721 int fp_adjust;
2722 int sp_adjust;
2723 int en4_const;
2724
2725 rtx Rb, Re;
9304f876
CJW
2726 rtx fp_adjust_insn, sp_adjust_insn;
2727
2728 /* Before computing everything for stack frame size,
2729 we check if it is still worth to use fp_as_gp optimization.
2730 If it is, the 'df_regs_ever_live_p (FP_REGNUM)' will be set
2731 so that $fp will be saved on stack. */
2732 cfun->machine->fp_as_gp_p = nds32_fp_as_gp_check_available ();
2733
2734 /* Compute and setup stack frame size.
2735 The result will be in cfun->machine. */
2736 nds32_compute_stack_frame ();
2737
2738 /* If the function is 'naked',
2739 we do not have to generate prologue code fragment. */
2740 if (cfun->machine->naked_p)
2741 return;
2742
2743 /* Get callee_first_regno and callee_last_regno. */
2744 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_first_regno);
2745 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_last_regno);
2746
4e9a2848 2747 /* nds32_emit_stack_push_multiple(first_regno, last_regno),
9304f876
CJW
2748 the pattern 'stack_push_multiple' is implemented in nds32.md.
2749 For En4 field, we have to calculate its constant value.
2750 Refer to Andes ISA for more information. */
2751 en4_const = 0;
2752 if (cfun->machine->fp_size)
2753 en4_const += 8;
2754 if (cfun->machine->gp_size)
2755 en4_const += 4;
2756 if (cfun->machine->lp_size)
2757 en4_const += 2;
2758
2759 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
2760 to be saved, we don't have to create multiple push instruction.
2761 Otherwise, a multiple push instruction is needed. */
2762 if (!(REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM && en4_const == 0))
2763 {
2764 /* Create multiple push instruction rtx. */
4e9a2848 2765 nds32_emit_stack_push_multiple (Rb, Re, GEN_INT (en4_const));
9304f876
CJW
2766 }
2767
2768 /* Check frame_pointer_needed to see
2769 if we shall emit fp adjustment instruction. */
2770 if (frame_pointer_needed)
2771 {
2772 /* adjust $fp = $sp + ($fp size) + ($gp size) + ($lp size)
2773 + (4 * callee-saved-registers)
2774 Note: No need to adjust
2775 cfun->machine->callee_saved_area_padding_bytes,
2776 because, at this point, stack pointer is just
2777 at the position after push instruction. */
2778 fp_adjust = cfun->machine->fp_size
2779 + cfun->machine->gp_size
2780 + cfun->machine->lp_size
2781 + cfun->machine->callee_saved_regs_size;
2782 fp_adjust_insn = gen_addsi3 (hard_frame_pointer_rtx,
2783 stack_pointer_rtx,
2784 GEN_INT (fp_adjust));
2785 /* Emit rtx into instructions list and receive INSN rtx form. */
2786 fp_adjust_insn = emit_insn (fp_adjust_insn);
2787 }
2788
2789 /* Adjust $sp = $sp - local_size - out_args_size
2790 - callee_saved_area_padding_bytes. */
2791 sp_adjust = cfun->machine->local_size
2792 + cfun->machine->out_args_size
2793 + cfun->machine->callee_saved_area_padding_bytes;
2794 /* sp_adjust value may be out of range of the addi instruction,
2795 create alternative add behavior with TA_REGNUM if necessary,
2796 using NEGATIVE value to tell that we are decreasing address. */
2797 sp_adjust = nds32_force_addi_stack_int ( (-1) * sp_adjust);
2798 if (sp_adjust)
2799 {
2800 /* Generate sp adjustment instruction if and only if sp_adjust != 0. */
2801 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
2802 stack_pointer_rtx,
2803 GEN_INT (-1 * sp_adjust));
2804 /* Emit rtx into instructions list and receive INSN rtx form. */
2805 sp_adjust_insn = emit_insn (sp_adjust_insn);
2806
2807 /* The insn rtx 'sp_adjust_insn' will change frame layout.
2808 We need to use RTX_FRAME_RELATED_P so that GCC is able to
2809 generate CFI (Call Frame Information) stuff. */
2810 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
2811 }
2812
2813 /* Prevent the instruction scheduler from
2814 moving instructions across the boundary. */
2815 emit_insn (gen_blockage ());
2816}
2817
2818/* Function for normal multiple pop epilogue. */
2819void
2820nds32_expand_epilogue (void)
2821{
2822 int sp_adjust;
2823 int en4_const;
2824
2825 rtx Rb, Re;
9304f876
CJW
2826 rtx sp_adjust_insn;
2827
2828 /* Compute and setup stack frame size.
2829 The result will be in cfun->machine. */
2830 nds32_compute_stack_frame ();
2831
2832 /* Prevent the instruction scheduler from
2833 moving instructions across the boundary. */
2834 emit_insn (gen_blockage ());
2835
2836 /* If the function is 'naked', we do not have to generate
2837 epilogue code fragment BUT 'ret' instruction. */
2838 if (cfun->machine->naked_p)
2839 {
2840 /* Generate return instruction by using
2841 unspec_volatile_func_return pattern.
2842 Make sure this instruction is after gen_blockage().
2843 NOTE that $lp will become 'live'
2844 after this instruction has been emitted. */
2845 emit_insn (gen_unspec_volatile_func_return ());
2846 return;
2847 }
2848
2849 if (frame_pointer_needed)
2850 {
2851 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
2852 - (4 * callee-saved-registers)
2853 Note: No need to adjust
2854 cfun->machine->callee_saved_area_padding_bytes,
2855 because we want to adjust stack pointer
2856 to the position for pop instruction. */
2857 sp_adjust = cfun->machine->fp_size
2858 + cfun->machine->gp_size
2859 + cfun->machine->lp_size
2860 + cfun->machine->callee_saved_regs_size;
2861 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
2862 hard_frame_pointer_rtx,
2863 GEN_INT (-1 * sp_adjust));
2864 /* Emit rtx into instructions list and receive INSN rtx form. */
2865 sp_adjust_insn = emit_insn (sp_adjust_insn);
2866 }
2867 else
2868 {
2869 /* If frame pointer is NOT needed,
2870 we cannot calculate the sp adjustment from frame pointer.
2871 Instead, we calculate the adjustment by local_size,
2872 out_args_size, and callee_saved_area_padding_bytes.
2873 Notice that such sp adjustment value may be out of range,
2874 so we have to deal with it as well. */
2875
2876 /* Adjust $sp = $sp + local_size + out_args_size
2877 + callee_saved_area_padding_bytes. */
2878 sp_adjust = cfun->machine->local_size
2879 + cfun->machine->out_args_size
2880 + cfun->machine->callee_saved_area_padding_bytes;
2881 /* sp_adjust value may be out of range of the addi instruction,
2882 create alternative add behavior with TA_REGNUM if necessary,
2883 using POSITIVE value to tell that we are increasing address. */
2884 sp_adjust = nds32_force_addi_stack_int (sp_adjust);
2885 if (sp_adjust)
2886 {
2887 /* Generate sp adjustment instruction
2888 if and only if sp_adjust != 0. */
2889 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
2890 stack_pointer_rtx,
2891 GEN_INT (sp_adjust));
2892 /* Emit rtx into instructions list and receive INSN rtx form. */
2893 sp_adjust_insn = emit_insn (sp_adjust_insn);
2894 }
2895 }
2896
2897 /* Get callee_first_regno and callee_last_regno. */
2898 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_first_regno);
2899 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_last_regno);
2900
4e9a2848 2901 /* nds32_emit_stack_pop_multiple(first_regno, last_regno),
9304f876
CJW
2902 the pattern 'stack_pop_multiple' is implementad in nds32.md.
2903 For En4 field, we have to calculate its constant value.
2904 Refer to Andes ISA for more information. */
2905 en4_const = 0;
2906 if (cfun->machine->fp_size)
2907 en4_const += 8;
2908 if (cfun->machine->gp_size)
2909 en4_const += 4;
2910 if (cfun->machine->lp_size)
2911 en4_const += 2;
2912
2913 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
2914 to be saved, we don't have to create multiple pop instruction.
2915 Otherwise, a multiple pop instruction is needed. */
2916 if (!(REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM && en4_const == 0))
2917 {
2918 /* Create multiple pop instruction rtx. */
4e9a2848 2919 nds32_emit_stack_pop_multiple (Rb, Re, GEN_INT (en4_const));
9304f876
CJW
2920 }
2921
2922 /* Generate return instruction by using
2923 unspec_volatile_func_return pattern. */
2924 emit_insn (gen_unspec_volatile_func_return ());
2925}
2926
2927/* Function for v3push prologue. */
2928void
2929nds32_expand_prologue_v3push (void)
2930{
2931 int fp_adjust;
2932 int sp_adjust;
2933
2934 rtx Rb, Re;
9304f876
CJW
2935 rtx fp_adjust_insn, sp_adjust_insn;
2936
2937 /* Before computing everything for stack frame size,
2938 we check if it is still worth to use fp_as_gp optimization.
2939 If it is, the 'df_regs_ever_live_p (FP_REGNUM)' will be set
2940 so that $fp will be saved on stack. */
2941 cfun->machine->fp_as_gp_p = nds32_fp_as_gp_check_available ();
2942
2943 /* Compute and setup stack frame size.
2944 The result will be in cfun->machine. */
2945 nds32_compute_stack_frame ();
2946
2947 /* If the function is 'naked',
2948 we do not have to generate prologue code fragment. */
2949 if (cfun->machine->naked_p)
2950 return;
2951
2952 /* Get callee_first_regno and callee_last_regno. */
2953 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_first_regno);
2954 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_last_regno);
2955
2956 /* Calculate sp_adjust first to test if 'push25 Re,imm8u' is available,
2957 where imm8u has to be 8-byte alignment. */
2958 sp_adjust = cfun->machine->local_size
2959 + cfun->machine->out_args_size
2960 + cfun->machine->callee_saved_area_padding_bytes;
2961
2962 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
2963 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust))
2964 {
2965 /* We can use 'push25 Re,imm8u'. */
2966
88437f39 2967 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
9304f876
CJW
2968 the pattern 'stack_v3push' is implemented in nds32.md.
2969 The (const_int 14) means v3push always push { $fp $gp $lp }. */
88437f39
CJW
2970 nds32_emit_stack_v3push (Rb, Re,
2971 GEN_INT (14), GEN_INT (sp_adjust));
9304f876
CJW
2972
2973 /* Check frame_pointer_needed to see
2974 if we shall emit fp adjustment instruction. */
2975 if (frame_pointer_needed)
2976 {
2977 /* adjust $fp = $sp + 4 ($fp size)
2978 + 4 ($gp size)
2979 + 4 ($lp size)
2980 + (4 * n) (callee-saved registers)
2981 + sp_adjust ('push25 Re,imm8u')
2982 Note: Since we use 'push25 Re,imm8u',
2983 the position of stack pointer is further
2984 changed after push instruction.
2985 Hence, we need to take sp_adjust value
2986 into consideration. */
2987 fp_adjust = cfun->machine->fp_size
2988 + cfun->machine->gp_size
2989 + cfun->machine->lp_size
2990 + cfun->machine->callee_saved_regs_size
2991 + sp_adjust;
2992 fp_adjust_insn = gen_addsi3 (hard_frame_pointer_rtx,
2993 stack_pointer_rtx,
2994 GEN_INT (fp_adjust));
2995 /* Emit rtx into instructions list and receive INSN rtx form. */
2996 fp_adjust_insn = emit_insn (fp_adjust_insn);
2997 }
2998 }
2999 else
3000 {
3001 /* We have to use 'push25 Re,0' and
3002 expand one more instruction to adjust $sp later. */
3003
88437f39 3004 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
9304f876
CJW
3005 the pattern 'stack_v3push' is implemented in nds32.md.
3006 The (const_int 14) means v3push always push { $fp $gp $lp }. */
88437f39
CJW
3007 nds32_emit_stack_v3push (Rb, Re,
3008 GEN_INT (14), GEN_INT (0));
9304f876
CJW
3009
3010 /* Check frame_pointer_needed to see
3011 if we shall emit fp adjustment instruction. */
3012 if (frame_pointer_needed)
3013 {
3014 /* adjust $fp = $sp + 4 ($fp size)
3015 + 4 ($gp size)
3016 + 4 ($lp size)
3017 + (4 * n) (callee-saved registers)
3018 Note: Since we use 'push25 Re,0',
3019 the stack pointer is just at the position
3020 after push instruction.
3021 No need to take sp_adjust into consideration. */
3022 fp_adjust = cfun->machine->fp_size
3023 + cfun->machine->gp_size
3024 + cfun->machine->lp_size
3025 + cfun->machine->callee_saved_regs_size;
3026 fp_adjust_insn = gen_addsi3 (hard_frame_pointer_rtx,
3027 stack_pointer_rtx,
3028 GEN_INT (fp_adjust));
3029 /* Emit rtx into instructions list and receive INSN rtx form. */
3030 fp_adjust_insn = emit_insn (fp_adjust_insn);
3031 }
3032
3033 /* Because we use 'push25 Re,0',
3034 we need to expand one more instruction to adjust $sp.
3035 However, sp_adjust value may be out of range of the addi instruction,
3036 create alternative add behavior with TA_REGNUM if necessary,
3037 using NEGATIVE value to tell that we are decreasing address. */
3038 sp_adjust = nds32_force_addi_stack_int ( (-1) * sp_adjust);
3039 if (sp_adjust)
3040 {
3041 /* Generate sp adjustment instruction
3042 if and only if sp_adjust != 0. */
3043 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3044 stack_pointer_rtx,
3045 GEN_INT (-1 * sp_adjust));
3046 /* Emit rtx into instructions list and receive INSN rtx form. */
3047 sp_adjust_insn = emit_insn (sp_adjust_insn);
3048
3049 /* The insn rtx 'sp_adjust_insn' will change frame layout.
3050 We need to use RTX_FRAME_RELATED_P so that GCC is able to
3051 generate CFI (Call Frame Information) stuff. */
3052 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
3053 }
3054 }
3055
3056 /* Prevent the instruction scheduler from
3057 moving instructions across the boundary. */
3058 emit_insn (gen_blockage ());
3059}
3060
3061/* Function for v3pop epilogue. */
3062void
3063nds32_expand_epilogue_v3pop (void)
3064{
3065 int sp_adjust;
3066
3067 rtx Rb, Re;
9304f876
CJW
3068 rtx sp_adjust_insn;
3069
3070 /* Compute and setup stack frame size.
3071 The result will be in cfun->machine. */
3072 nds32_compute_stack_frame ();
3073
3074 /* Prevent the instruction scheduler from
3075 moving instructions across the boundary. */
3076 emit_insn (gen_blockage ());
3077
3078 /* If the function is 'naked', we do not have to generate
3079 epilogue code fragment BUT 'ret' instruction. */
3080 if (cfun->machine->naked_p)
3081 {
3082 /* Generate return instruction by using
3083 unspec_volatile_func_return pattern.
3084 Make sure this instruction is after gen_blockage().
3085 NOTE that $lp will become 'live'
3086 after this instruction has been emitted. */
3087 emit_insn (gen_unspec_volatile_func_return ());
3088 return;
3089 }
3090
3091 /* Get callee_first_regno and callee_last_regno. */
3092 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_first_regno);
3093 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_regs_last_regno);
3094
3095 /* Calculate sp_adjust first to test if 'pop25 Re,imm8u' is available,
3096 where imm8u has to be 8-byte alignment. */
3097 sp_adjust = cfun->machine->local_size
3098 + cfun->machine->out_args_size
3099 + cfun->machine->callee_saved_area_padding_bytes;
3100
3101 /* We have to consider alloca issue as well.
3102 If the function does call alloca(), the stack pointer is not fixed.
3103 In that case, we cannot use 'pop25 Re,imm8u' directly.
3104 We have to caculate stack pointer from frame pointer
3105 and then use 'pop25 Re,0'.
3106 Of course, the frame_pointer_needed should be nonzero
3107 if the function calls alloca(). */
3108 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
3109 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
3110 && !cfun->calls_alloca)
3111 {
3112 /* We can use 'pop25 Re,imm8u'. */
3113
88437f39 3114 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
9304f876
CJW
3115 the pattern 'stack_v3pop' is implementad in nds32.md.
3116 The (const_int 14) means v3pop always pop { $fp $gp $lp }. */
88437f39
CJW
3117 nds32_emit_stack_v3pop (Rb, Re,
3118 GEN_INT (14), GEN_INT (sp_adjust));
9304f876
CJW
3119 }
3120 else
3121 {
3122 /* We have to use 'pop25 Re,0', and prior to it,
3123 we must expand one more instruction to adjust $sp. */
3124
3125 if (frame_pointer_needed)
3126 {
3127 /* adjust $sp = $fp - 4 ($fp size)
3128 - 4 ($gp size)
3129 - 4 ($lp size)
3130 - (4 * n) (callee-saved registers)
3131 Note: No need to adjust
3132 cfun->machine->callee_saved_area_padding_bytes,
3133 because we want to adjust stack pointer
3134 to the position for pop instruction. */
3135 sp_adjust = cfun->machine->fp_size
3136 + cfun->machine->gp_size
3137 + cfun->machine->lp_size
3138 + cfun->machine->callee_saved_regs_size;
3139 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3140 hard_frame_pointer_rtx,
3141 GEN_INT (-1 * sp_adjust));
3142 /* Emit rtx into instructions list and receive INSN rtx form. */
3143 sp_adjust_insn = emit_insn (sp_adjust_insn);
3144 }
3145 else
3146 {
3147 /* If frame pointer is NOT needed,
3148 we cannot calculate the sp adjustment from frame pointer.
3149 Instead, we calculate the adjustment by local_size,
3150 out_args_size, and callee_saved_area_padding_bytes.
3151 Notice that such sp adjustment value may be out of range,
3152 so we have to deal with it as well. */
3153
3154 /* Adjust $sp = $sp + local_size + out_args_size
3155 + callee_saved_area_padding_bytes. */
3156 sp_adjust = cfun->machine->local_size
3157 + cfun->machine->out_args_size
3158 + cfun->machine->callee_saved_area_padding_bytes;
3159 /* sp_adjust value may be out of range of the addi instruction,
3160 create alternative add behavior with TA_REGNUM if necessary,
3161 using POSITIVE value to tell that we are increasing address. */
3162 sp_adjust = nds32_force_addi_stack_int (sp_adjust);
3163 if (sp_adjust)
3164 {
3165 /* Generate sp adjustment instruction
3166 if and only if sp_adjust != 0. */
3167 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3168 stack_pointer_rtx,
3169 GEN_INT (sp_adjust));
3170 /* Emit rtx into instructions list and receive INSN rtx form. */
3171 sp_adjust_insn = emit_insn (sp_adjust_insn);
3172 }
3173 }
3174
88437f39 3175 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
9304f876
CJW
3176 the pattern 'stack_v3pop' is implementad in nds32.md. */
3177 /* The (const_int 14) means v3pop always pop { $fp $gp $lp }. */
88437f39
CJW
3178 nds32_emit_stack_v3pop (Rb, Re,
3179 GEN_INT (14), GEN_INT (0));
9304f876
CJW
3180 }
3181}
3182
3183/* ------------------------------------------------------------------------ */
3184
3185/* Function to test 333-form for load/store instructions.
3186 This is auxiliary extern function for auxiliary macro in nds32.h.
3187 Because it is a little complicated, we use function instead of macro. */
3188bool
3189nds32_ls_333_p (rtx rt, rtx ra, rtx imm, enum machine_mode mode)
3190{
3191 if (REGNO_REG_CLASS (REGNO (rt)) == LOW_REGS
3192 && REGNO_REG_CLASS (REGNO (ra)) == LOW_REGS)
3193 {
3194 if (GET_MODE_SIZE (mode) == 4)
3195 return satisfies_constraint_Iu05 (imm);
3196
3197 if (GET_MODE_SIZE (mode) == 2)
3198 return satisfies_constraint_Iu04 (imm);
3199
3200 if (GET_MODE_SIZE (mode) == 1)
3201 return satisfies_constraint_Iu03 (imm);
3202 }
3203
3204 return false;
3205}
3206
3207
9304f876
CJW
3208/* Computing the Length of an Insn.
3209 Modifies the length assigned to instruction INSN.
3210 LEN is the initially computed length of the insn. */
3211int
ca009aee 3212nds32_adjust_insn_length (rtx_insn *insn, int length)
9304f876
CJW
3213{
3214 rtx src, dst;
3215
3216 switch (recog_memoized (insn))
3217 {
3218 case CODE_FOR_move_df:
3219 case CODE_FOR_move_di:
3220 /* Adjust length of movd44 to 2. */
3221 src = XEXP (PATTERN (insn), 1);
3222 dst = XEXP (PATTERN (insn), 0);
3223
3224 if (REG_P (src)
3225 && REG_P (dst)
3226 && (REGNO (src) % 2) == 0
3227 && (REGNO (dst) % 2) == 0)
3228 length = 2;
3229 break;
3230
3231 default:
3232 break;
3233 }
3234
3235 return length;
3236}
3237
3238
9304f876
CJW
3239/* Return align 2 (log base 2) if the next instruction of LABEL is 4 byte. */
3240int
3241nds32_target_alignment (rtx label)
3242{
3243 rtx insn;
3244
3245 if (optimize_size)
3246 return 0;
3247
3248 insn = next_active_insn (label);
3249
3250 if (insn == 0)
3251 return 0;
3252 else if ((get_attr_length (insn) % 4) == 0)
3253 return 2;
3254 else
3255 return 0;
3256}
3257
3258/* ------------------------------------------------------------------------ */
3259
3260/* PART 5: Initialize target hook structure and definitions. */
3261\f
3262/* Controlling the Compilation Driver. */
3263
3264\f
3265/* Run-time Target Specification. */
3266
3267\f
3268/* Defining Data Structures for Per-function Information. */
3269
3270\f
3271/* Storage Layout. */
3272
3273#undef TARGET_PROMOTE_FUNCTION_MODE
3274#define TARGET_PROMOTE_FUNCTION_MODE \
3275 default_promote_function_mode_always_promote
3276
3277\f
3278/* Layout of Source Language Data Types. */
3279
3280\f
3281/* Register Usage. */
3282
3283/* -- Basic Characteristics of Registers. */
3284
3285/* -- Order of Allocation of Registers. */
3286
3287/* -- How Values Fit in Registers. */
3288
3289/* -- Handling Leaf Functions. */
3290
3291/* -- Registers That Form a Stack. */
3292
3293\f
3294/* Register Classes. */
3295
3296#undef TARGET_CLASS_MAX_NREGS
3297#define TARGET_CLASS_MAX_NREGS nds32_class_max_nregs
3298
3299#undef TARGET_LRA_P
3300#define TARGET_LRA_P hook_bool_void_true
3301
3302#undef TARGET_REGISTER_PRIORITY
3303#define TARGET_REGISTER_PRIORITY nds32_register_priority
3304
3305\f
3306/* Obsolete Macros for Defining Constraints. */
3307
3308\f
3309/* Stack Layout and Calling Conventions. */
3310
3311/* -- Basic Stack Layout. */
3312
3313/* -- Exception Handling Support. */
3314
3315/* -- Specifying How Stack Checking is Done. */
3316
3317/* -- Registers That Address the Stack Frame. */
3318
3319/* -- Eliminating Frame Pointer and Arg Pointer. */
3320
3321#undef TARGET_CAN_ELIMINATE
3322#define TARGET_CAN_ELIMINATE nds32_can_eliminate
3323
3324/* -- Passing Function Arguments on the Stack. */
3325
3326/* -- Passing Arguments in Registers. */
3327
3328#undef TARGET_FUNCTION_ARG
3329#define TARGET_FUNCTION_ARG nds32_function_arg
3330
3331#undef TARGET_FUNCTION_ARG_ADVANCE
3332#define TARGET_FUNCTION_ARG_ADVANCE nds32_function_arg_advance
3333
3334#undef TARGET_FUNCTION_ARG_BOUNDARY
3335#define TARGET_FUNCTION_ARG_BOUNDARY nds32_function_arg_boundary
3336
3337/* -- How Scalar Function Values Are Returned. */
3338
3339#undef TARGET_FUNCTION_VALUE
3340#define TARGET_FUNCTION_VALUE nds32_function_value
3341
3342#undef TARGET_LIBCALL_VALUE
3343#define TARGET_LIBCALL_VALUE nds32_libcall_value
3344
3345#undef TARGET_FUNCTION_VALUE_REGNO_P
3346#define TARGET_FUNCTION_VALUE_REGNO_P nds32_function_value_regno_p
3347
3348/* -- How Large Values Are Returned. */
3349
3350/* -- Caller-Saves Register Allocation. */
3351
3352/* -- Function Entry and Exit. */
3353
3354#undef TARGET_ASM_FUNCTION_PROLOGUE
3355#define TARGET_ASM_FUNCTION_PROLOGUE nds32_asm_function_prologue
3356
3357#undef TARGET_ASM_FUNCTION_END_PROLOGUE
3358#define TARGET_ASM_FUNCTION_END_PROLOGUE nds32_asm_function_end_prologue
3359
3360#undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
3361#define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE nds32_asm_function_begin_epilogue
3362
3363#undef TARGET_ASM_FUNCTION_EPILOGUE
3364#define TARGET_ASM_FUNCTION_EPILOGUE nds32_asm_function_epilogue
3365
3366#undef TARGET_ASM_OUTPUT_MI_THUNK
3367#define TARGET_ASM_OUTPUT_MI_THUNK nds32_asm_output_mi_thunk
3368
3369#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3370#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
3371
3372/* -- Generating Code for Profiling. */
3373
3374/* -- Permitting tail calls. */
3375
3376#undef TARGET_WARN_FUNC_RETURN
3377#define TARGET_WARN_FUNC_RETURN nds32_warn_func_return
3378
3379/* Stack smashing protection. */
3380
3381\f
3382/* Implementing the Varargs Macros. */
3383
d4a6a4d9
CJW
3384#undef TARGET_SETUP_INCOMING_VARARGS
3385#define TARGET_SETUP_INCOMING_VARARGS nds32_setup_incoming_varargs
3386
9304f876
CJW
3387#undef TARGET_STRICT_ARGUMENT_NAMING
3388#define TARGET_STRICT_ARGUMENT_NAMING nds32_strict_argument_naming
3389
3390\f
3391/* Trampolines for Nested Functions. */
3392
3393#undef TARGET_ASM_TRAMPOLINE_TEMPLATE
3394#define TARGET_ASM_TRAMPOLINE_TEMPLATE nds32_asm_trampoline_template
3395
3396#undef TARGET_TRAMPOLINE_INIT
3397#define TARGET_TRAMPOLINE_INIT nds32_trampoline_init
3398
3399\f
3400/* Implicit Calls to Library Routines. */
3401
3402\f
3403/* Addressing Modes. */
3404
3405#undef TARGET_LEGITIMATE_ADDRESS_P
3406#define TARGET_LEGITIMATE_ADDRESS_P nds32_legitimate_address_p
3407
3408\f
3409/* Anchored Addresses. */
3410
3411\f
3412/* Condition Code Status. */
3413
3414/* -- Representation of condition codes using (cc0). */
3415
3416/* -- Representation of condition codes using registers. */
3417
3418/* -- Macros to control conditional execution. */
3419
3420\f
3421/* Describing Relative Costs of Operations. */
3422
3423#undef TARGET_REGISTER_MOVE_COST
3424#define TARGET_REGISTER_MOVE_COST nds32_register_move_cost
3425
3426#undef TARGET_MEMORY_MOVE_COST
3427#define TARGET_MEMORY_MOVE_COST nds32_memory_move_cost
3428
3429#undef TARGET_RTX_COSTS
3430#define TARGET_RTX_COSTS nds32_rtx_costs
3431
3432#undef TARGET_ADDRESS_COST
3433#define TARGET_ADDRESS_COST nds32_address_cost
3434
3435\f
3436/* Adjusting the Instruction Scheduler. */
3437
3438\f
3439/* Dividing the Output into Sections (Texts, Data, . . . ). */
3440
3441\f
3442/* Position Independent Code. */
3443
3444\f
3445/* Defining the Output Assembler Language. */
3446
3447/* -- The Overall Framework of an Assembler File. */
3448
3449#undef TARGET_ASM_FILE_START
3450#define TARGET_ASM_FILE_START nds32_asm_file_start
3451#undef TARGET_ASM_FILE_END
3452#define TARGET_ASM_FILE_END nds32_asm_file_end
3453
3454/* -- Output of Data. */
3455
3456#undef TARGET_ASM_ALIGNED_HI_OP
3457#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
3458
3459#undef TARGET_ASM_ALIGNED_SI_OP
3460#define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
3461
3462/* -- Output of Uninitialized Variables. */
3463
3464/* -- Output and Generation of Labels. */
3465
3466#undef TARGET_ASM_GLOBALIZE_LABEL
3467#define TARGET_ASM_GLOBALIZE_LABEL nds32_asm_globalize_label
3468
3469/* -- How Initialization Functions Are Handled. */
3470
3471/* -- Macros Controlling Initialization Routines. */
3472
3473/* -- Output of Assembler Instructions. */
3474
3475#undef TARGET_PRINT_OPERAND
3476#define TARGET_PRINT_OPERAND nds32_print_operand
3477#undef TARGET_PRINT_OPERAND_ADDRESS
3478#define TARGET_PRINT_OPERAND_ADDRESS nds32_print_operand_address
3479
3480/* -- Output of Dispatch Tables. */
3481
3482/* -- Assembler Commands for Exception Regions. */
3483
3484/* -- Assembler Commands for Alignment. */
3485
3486\f
3487/* Controlling Debugging Information Format. */
3488
3489/* -- Macros Affecting All Debugging Formats. */
3490
3491/* -- Specific Options for DBX Output. */
3492
3493/* -- Open-Ended Hooks for DBX Format. */
3494
3495/* -- File Names in DBX Format. */
3496
3497/* -- Macros for SDB and DWARF Output. */
3498
3499/* -- Macros for VMS Debug Format. */
3500
3501\f
3502/* Cross Compilation and Floating Point. */
3503
3504\f
3505/* Mode Switching Instructions. */
3506
3507\f
3508/* Defining target-specific uses of __attribute__. */
3509
3510#undef TARGET_ATTRIBUTE_TABLE
3511#define TARGET_ATTRIBUTE_TABLE nds32_attribute_table
3512
3513#undef TARGET_MERGE_DECL_ATTRIBUTES
3514#define TARGET_MERGE_DECL_ATTRIBUTES nds32_merge_decl_attributes
3515
3516#undef TARGET_INSERT_ATTRIBUTES
3517#define TARGET_INSERT_ATTRIBUTES nds32_insert_attributes
3518
3519#undef TARGET_OPTION_PRAGMA_PARSE
3520#define TARGET_OPTION_PRAGMA_PARSE nds32_option_pragma_parse
3521
3522#undef TARGET_OPTION_OVERRIDE
3523#define TARGET_OPTION_OVERRIDE nds32_option_override
3524
3525\f
3526/* Emulating TLS. */
3527
3528\f
3529/* Defining coprocessor specifics for MIPS targets. */
3530
3531\f
3532/* Parameters for Precompiled Header Validity Checking. */
3533
3534\f
3535/* C++ ABI parameters. */
3536
3537\f
3538/* Adding support for named address spaces. */
3539
3540\f
3541/* Miscellaneous Parameters. */
3542
3543#undef TARGET_INIT_BUILTINS
3544#define TARGET_INIT_BUILTINS nds32_init_builtins
3545
3546#undef TARGET_EXPAND_BUILTIN
3547#define TARGET_EXPAND_BUILTIN nds32_expand_builtin
3548
3549\f
3550/* ------------------------------------------------------------------------ */
3551
3552/* Initialize the GCC target structure. */
3553
3554struct gcc_target targetm = TARGET_INITIALIZER;
3555
3556/* ------------------------------------------------------------------------ */