[NDS32] Add unaligned access support.
[official-gcc.git] / gcc / config / nds32 / nds32.c
blob1ce9e06786591521656751f3a944c3025c08b1de
1 /* Subroutines used for code generation of Andes NDS32 cpu for GNU compiler
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of GCC.
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.
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.
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/>. */
21 /* ------------------------------------------------------------------------ */
23 #define IN_TARGET_CODE 1
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "tree-pass.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "df.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "optabs.h" /* For GEN_FCN. */
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "diagnostic-core.h"
43 #include "stor-layout.h"
44 #include "varasm.h"
45 #include "calls.h"
46 #include "output.h"
47 #include "explow.h"
48 #include "expr.h"
49 #include "tm-constrs.h"
50 #include "builtins.h"
51 #include "cpplib.h"
52 #include "context.h"
54 /* This file should be included last. */
55 #include "target-def.h"
57 /* ------------------------------------------------------------------------ */
59 /* This file is divided into five parts:
61 PART 1: Auxiliary static variable definitions and
62 target hook static variable definitions.
64 PART 2: Auxiliary static function definitions.
66 PART 3: Implement target hook stuff definitions.
68 PART 4: Implemet extern function definitions,
69 the prototype is in nds32-protos.h.
71 PART 5: Initialize target hook structure and definitions. */
73 /* ------------------------------------------------------------------------ */
75 /* PART 1: Auxiliary static variable definitions and
76 target hook static variable definitions. */
78 /* Define intrinsic register names.
79 Please refer to nds32_intrinsic.h file, the index is corresponding to
80 'enum nds32_intrinsic_registers' data type values.
81 NOTE that the base value starting from 1024. */
82 static const char * const nds32_intrinsic_register_names[] =
84 "$CPU_VER",
85 "$ICM_CFG",
86 "$DCM_CFG",
87 "$MMU_CFG",
88 "$MSC_CFG",
89 "$MSC_CFG2",
90 "$CORE_ID",
91 "$FUCOP_EXIST",
93 "$PSW",
94 "$IPSW",
95 "$P_IPSW",
96 "$IVB",
97 "$EVA",
98 "$P_EVA",
99 "$ITYPE",
100 "$P_ITYPE",
102 "$MERR",
103 "$IPC",
104 "$P_IPC",
105 "$OIPC",
106 "$P_P0",
107 "$P_P1",
109 "$INT_MASK",
110 "$INT_MASK2",
111 "$INT_MASK3",
112 "$INT_PEND",
113 "$INT_PEND2",
114 "$INT_PEND3",
115 "$SP_USR",
116 "$SP_PRIV",
117 "$INT_PRI",
118 "$INT_PRI2",
119 "$INT_PRI3",
120 "$INT_PRI4",
121 "$INT_CTRL",
122 "$INT_TRIGGER",
123 "$INT_TRIGGER2",
124 "$INT_GPR_PUSH_DIS",
126 "$MMU_CTL",
127 "$L1_PPTB",
128 "$TLB_VPN",
129 "$TLB_DATA",
130 "$TLB_MISC",
131 "$VLPT_IDX",
132 "$ILMB",
133 "$DLMB",
135 "$CACHE_CTL",
136 "$HSMP_SADDR",
137 "$HSMP_EADDR",
138 "$SDZ_CTL",
139 "$N12MISC_CTL",
140 "$MISC_CTL",
141 "$ECC_MISC",
143 "$BPC0",
144 "$BPC1",
145 "$BPC2",
146 "$BPC3",
147 "$BPC4",
148 "$BPC5",
149 "$BPC6",
150 "$BPC7",
152 "$BPA0",
153 "$BPA1",
154 "$BPA2",
155 "$BPA3",
156 "$BPA4",
157 "$BPA5",
158 "$BPA6",
159 "$BPA7",
161 "$BPAM0",
162 "$BPAM1",
163 "$BPAM2",
164 "$BPAM3",
165 "$BPAM4",
166 "$BPAM5",
167 "$BPAM6",
168 "$BPAM7",
170 "$BPV0",
171 "$BPV1",
172 "$BPV2",
173 "$BPV3",
174 "$BPV4",
175 "$BPV5",
176 "$BPV6",
177 "$BPV7",
179 "$BPCID0",
180 "$BPCID1",
181 "$BPCID2",
182 "$BPCID3",
183 "$BPCID4",
184 "$BPCID5",
185 "$BPCID6",
186 "$BPCID7",
188 "$EDM_CFG",
189 "$EDMSW",
190 "$EDM_CTL",
191 "$EDM_DTR",
192 "$BPMTC",
193 "$DIMBR",
195 "$TECR0",
196 "$TECR1",
197 "$PFMC0",
198 "$PFMC1",
199 "$PFMC2",
200 "$PFM_CTL",
201 "$PFT_CTL",
202 "$HSP_CTL",
203 "$SP_BOUND",
204 "$SP_BOUND_PRIV",
205 "$SP_BASE",
206 "$SP_BASE_PRIV",
207 "$FUCOP_CTL",
208 "$PRUSR_ACC_CTL",
210 "$DMA_CFG",
211 "$DMA_GCSW",
212 "$DMA_CHNSEL",
213 "$DMA_ACT",
214 "$DMA_SETUP",
215 "$DMA_ISADDR",
216 "$DMA_ESADDR",
217 "$DMA_TCNT",
218 "$DMA_STATUS",
219 "$DMA_2DSET",
220 "$DMA_2DSCTL",
221 "$DMA_RCNT",
222 "$DMA_HSTATUS",
224 "$PC",
225 "$SP_USR1",
226 "$SP_USR2",
227 "$SP_USR3",
228 "$SP_PRIV1",
229 "$SP_PRIV2",
230 "$SP_PRIV3",
231 "$BG_REGION",
232 "$SFCR",
233 "$SIGN",
234 "$ISIGN",
235 "$P_ISIGN",
236 "$IFC_LP",
237 "$ITB"
240 /* Define instrinsic cctl names. */
241 static const char * const nds32_cctl_names[] =
243 "L1D_VA_FILLCK",
244 "L1D_VA_ULCK",
245 "L1I_VA_FILLCK",
246 "L1I_VA_ULCK",
248 "L1D_IX_WBINVAL",
249 "L1D_IX_INVAL",
250 "L1D_IX_WB",
251 "L1I_IX_INVAL",
253 "L1D_VA_INVAL",
254 "L1D_VA_WB",
255 "L1D_VA_WBINVAL",
256 "L1I_VA_INVAL",
258 "L1D_IX_RTAG",
259 "L1D_IX_RWD",
260 "L1I_IX_RTAG",
261 "L1I_IX_RWD",
263 "L1D_IX_WTAG",
264 "L1D_IX_WWD",
265 "L1I_IX_WTAG",
266 "L1I_IX_WWD"
269 static const char * const nds32_dpref_names[] =
271 "SRD",
272 "MRD",
273 "SWR",
274 "MWR",
275 "PTE",
276 "CLWR"
279 /* Defining register allocation order for performance.
280 We want to allocate callee-saved registers after others.
281 It may be used by nds32_adjust_reg_alloc_order(). */
282 static const int nds32_reg_alloc_order_for_speed[] =
284 0, 1, 2, 3, 4, 5, 16, 17,
285 18, 19, 20, 21, 22, 23, 24, 25,
286 26, 27, 6, 7, 8, 9, 10, 11,
287 12, 13, 14, 15
290 /* Defining target-specific uses of __attribute__. */
291 static const struct attribute_spec nds32_attribute_table[] =
293 /* Syntax: { name, min_len, max_len, decl_required, type_required,
294 function_type_required, affects_type_identity, handler,
295 exclude } */
297 /* The interrupt vid: [0-63]+ (actual vector number starts from 9 to 72). */
298 { "interrupt", 1, 64, false, false, false, false, NULL, NULL },
299 /* The exception vid: [1-8]+ (actual vector number starts from 1 to 8). */
300 { "exception", 1, 8, false, false, false, false, NULL, NULL },
301 /* Argument is user's interrupt numbers. The vector number is always 0. */
302 { "reset", 1, 1, false, false, false, false, NULL, NULL },
304 /* The attributes describing isr nested type. */
305 { "nested", 0, 0, false, false, false, false, NULL, NULL },
306 { "not_nested", 0, 0, false, false, false, false, NULL, NULL },
307 { "nested_ready", 0, 0, false, false, false, false, NULL, NULL },
309 /* The attributes describing isr register save scheme. */
310 { "save_all", 0, 0, false, false, false, false, NULL, NULL },
311 { "partial_save", 0, 0, false, false, false, false, NULL, NULL },
313 /* The attributes used by reset attribute. */
314 { "nmi", 1, 1, false, false, false, false, NULL, NULL },
315 { "warm", 1, 1, false, false, false, false, NULL, NULL },
317 /* The attribute telling no prologue/epilogue. */
318 { "naked", 0, 0, false, false, false, false, NULL, NULL },
320 /* The last attribute spec is set to be NULL. */
321 { NULL, 0, 0, false, false, false, false, NULL, NULL }
325 /* ------------------------------------------------------------------------ */
327 /* PART 2: Auxiliary static function definitions. */
329 /* Function to save and restore machine-specific function data. */
330 static struct machine_function *
331 nds32_init_machine_status (void)
333 struct machine_function *machine;
334 machine = ggc_cleared_alloc<machine_function> ();
336 /* Initially assume this function does not use __builtin_eh_return. */
337 machine->use_eh_return_p = 0;
339 /* Initially assume this function needs prologue/epilogue. */
340 machine->naked_p = 0;
342 /* Initially assume this function does NOT use fp_as_gp optimization. */
343 machine->fp_as_gp_p = 0;
345 /* Initially this function is not under strictly aligned situation. */
346 machine->strict_aligned_p = 0;
348 return machine;
351 /* Function to compute stack frame size and
352 store into cfun->machine structure. */
353 static void
354 nds32_compute_stack_frame (void)
356 int r;
357 int block_size;
358 bool v3pushpop_p;
360 /* Because nds32_compute_stack_frame() will be called from different place,
361 everytime we enter this function, we have to assume this function
362 needs prologue/epilogue. */
363 cfun->machine->naked_p = 0;
366 /* If __builtin_eh_return is used, we better have frame pointer needed
367 so that we can easily locate the stack slot of return address. */
368 if (crtl->calls_eh_return)
370 frame_pointer_needed = 1;
372 /* We need to mark eh data registers that need to be saved
373 in the stack. */
374 cfun->machine->eh_return_data_first_regno = EH_RETURN_DATA_REGNO (0);
375 for (r = 0; EH_RETURN_DATA_REGNO (r) != INVALID_REGNUM; r++)
376 cfun->machine->eh_return_data_last_regno = r;
378 cfun->machine->eh_return_data_regs_size
379 = 4 * (cfun->machine->eh_return_data_last_regno
380 - cfun->machine->eh_return_data_first_regno
381 + 1);
382 cfun->machine->use_eh_return_p = 1;
384 else
386 /* Assigning SP_REGNUM to eh_first_regno and eh_last_regno means we
387 do not need to handle __builtin_eh_return case in this function. */
388 cfun->machine->eh_return_data_first_regno = SP_REGNUM;
389 cfun->machine->eh_return_data_last_regno = SP_REGNUM;
391 cfun->machine->eh_return_data_regs_size = 0;
392 cfun->machine->use_eh_return_p = 0;
395 /* Get variadic arguments size to prepare pretend arguments and
396 we will push them into stack at prologue by ourself. */
397 cfun->machine->va_args_size = crtl->args.pretend_args_size;
398 if (cfun->machine->va_args_size != 0)
400 cfun->machine->va_args_first_regno
401 = NDS32_GPR_ARG_FIRST_REGNUM
402 + NDS32_MAX_GPR_REGS_FOR_ARGS
403 - (crtl->args.pretend_args_size / UNITS_PER_WORD);
404 cfun->machine->va_args_last_regno
405 = NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS - 1;
407 else
409 cfun->machine->va_args_first_regno = SP_REGNUM;
410 cfun->machine->va_args_last_regno = SP_REGNUM;
413 /* Important: We need to make sure that varargs area is 8-byte alignment. */
414 block_size = cfun->machine->va_args_size;
415 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
417 cfun->machine->va_args_area_padding_bytes
418 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
421 /* Get local variables, incoming variables, and temporary variables size.
422 Note that we need to make sure it is 8-byte alignment because
423 there may be no padding bytes if we are using LRA. */
424 cfun->machine->local_size = NDS32_ROUND_UP_DOUBLE_WORD (get_frame_size ());
426 /* Get outgoing arguments size. */
427 cfun->machine->out_args_size = crtl->outgoing_args_size;
429 /* If $fp value is required to be saved on stack, it needs 4 bytes space.
430 Check whether $fp is ever live. */
431 cfun->machine->fp_size = (df_regs_ever_live_p (FP_REGNUM)) ? 4 : 0;
433 /* If $gp value is required to be saved on stack, it needs 4 bytes space.
434 Check whether we are using PIC code genration. */
435 cfun->machine->gp_size = (flag_pic) ? 4 : 0;
437 /* If $lp value is required to be saved on stack, it needs 4 bytes space.
438 Check whether $lp is ever live. */
439 cfun->machine->lp_size = (df_regs_ever_live_p (LP_REGNUM)) ? 4 : 0;
441 /* Initially there is no padding bytes. */
442 cfun->machine->callee_saved_area_gpr_padding_bytes = 0;
444 /* Calculate the bytes of saving callee-saved registers on stack. */
445 cfun->machine->callee_saved_gpr_regs_size = 0;
446 cfun->machine->callee_saved_first_gpr_regno = SP_REGNUM;
447 cfun->machine->callee_saved_last_gpr_regno = SP_REGNUM;
448 cfun->machine->callee_saved_fpr_regs_size = 0;
449 cfun->machine->callee_saved_first_fpr_regno = SP_REGNUM;
450 cfun->machine->callee_saved_last_fpr_regno = SP_REGNUM;
452 /* Currently, there is no need to check $r28~$r31
453 because we will save them in another way. */
454 for (r = 0; r < 28; r++)
456 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
458 /* Mark the first required callee-saved register
459 (only need to set it once).
460 If first regno == SP_REGNUM, we can tell that
461 it is the first time to be here. */
462 if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
463 cfun->machine->callee_saved_first_gpr_regno = r;
464 /* Mark the last required callee-saved register. */
465 cfun->machine->callee_saved_last_gpr_regno = r;
469 /* Recording fpu callee-saved register. */
470 if (TARGET_HARD_FLOAT)
472 for (r = NDS32_FIRST_FPR_REGNUM; r < NDS32_LAST_FPR_REGNUM; r++)
474 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
476 /* Mark the first required callee-saved register. */
477 if (cfun->machine->callee_saved_first_fpr_regno == SP_REGNUM)
479 /* Make first callee-saved number is even,
480 bacause we use doubleword access, and this way
481 promise 8-byte alignemt. */
482 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (r))
483 cfun->machine->callee_saved_first_fpr_regno = r - 1;
484 else
485 cfun->machine->callee_saved_first_fpr_regno = r;
487 cfun->machine->callee_saved_last_fpr_regno = r;
491 /* Make last callee-saved register number is odd,
492 we hope callee-saved register is even. */
493 int last_fpr = cfun->machine->callee_saved_last_fpr_regno;
494 if (NDS32_FPR_REGNO_OK_FOR_DOUBLE (last_fpr))
495 cfun->machine->callee_saved_last_fpr_regno++;
498 /* Check if this function can omit prologue/epilogue code fragment.
499 If there is 'naked' attribute in this function,
500 we can set 'naked_p' flag to indicate that
501 we do not have to generate prologue/epilogue.
502 Or, if all the following conditions succeed,
503 we can set this function 'naked_p' as well:
504 condition 1: first_regno == last_regno == SP_REGNUM,
505 which means we do not have to save
506 any callee-saved registers.
507 condition 2: Both $lp and $fp are NOT live in this function,
508 which means we do not need to save them and there
509 is no outgoing size.
510 condition 3: There is no local_size, which means
511 we do not need to adjust $sp. */
512 if (lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl))
513 || (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM
514 && cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM
515 && cfun->machine->callee_saved_first_fpr_regno == SP_REGNUM
516 && cfun->machine->callee_saved_last_fpr_regno == SP_REGNUM
517 && !df_regs_ever_live_p (FP_REGNUM)
518 && !df_regs_ever_live_p (LP_REGNUM)
519 && cfun->machine->local_size == 0))
521 /* Set this function 'naked_p' and other functions can check this flag.
522 Note that in nds32 port, the 'naked_p = 1' JUST means there is no
523 callee-saved, local size, and outgoing size.
524 The varargs space and ret instruction may still present in
525 the prologue/epilogue expanding. */
526 cfun->machine->naked_p = 1;
528 /* No need to save $fp, $gp, and $lp.
529 We should set these value to be zero
530 so that nds32_initial_elimination_offset() can work properly. */
531 cfun->machine->fp_size = 0;
532 cfun->machine->gp_size = 0;
533 cfun->machine->lp_size = 0;
535 /* If stack usage computation is required,
536 we need to provide the static stack size. */
537 if (flag_stack_usage_info)
538 current_function_static_stack_size = 0;
540 /* No need to do following adjustment, return immediately. */
541 return;
544 v3pushpop_p = NDS32_V3PUSH_AVAILABLE_P;
546 /* Adjustment for v3push instructions:
547 If we are using v3push (push25/pop25) instructions,
548 we need to make sure Rb is $r6 and Re is
549 located on $r6, $r8, $r10, or $r14.
550 Some results above will be discarded and recomputed.
551 Note that it is only available under V3/V3M ISA and we
552 DO NOT setup following stuff for isr or variadic function. */
553 if (v3pushpop_p)
555 /* Recompute:
556 cfun->machine->fp_size
557 cfun->machine->gp_size
558 cfun->machine->lp_size
559 cfun->machine->callee_saved_first_gpr_regno
560 cfun->machine->callee_saved_last_gpr_regno */
562 /* For v3push instructions, $fp, $gp, and $lp are always saved. */
563 cfun->machine->fp_size = 4;
564 cfun->machine->gp_size = 4;
565 cfun->machine->lp_size = 4;
567 /* Remember to set Rb = $r6. */
568 cfun->machine->callee_saved_first_gpr_regno = 6;
570 if (cfun->machine->callee_saved_last_gpr_regno <= 6)
572 /* Re = $r6 */
573 cfun->machine->callee_saved_last_gpr_regno = 6;
575 else if (cfun->machine->callee_saved_last_gpr_regno <= 8)
577 /* Re = $r8 */
578 cfun->machine->callee_saved_last_gpr_regno = 8;
580 else if (cfun->machine->callee_saved_last_gpr_regno <= 10)
582 /* Re = $r10 */
583 cfun->machine->callee_saved_last_gpr_regno = 10;
585 else if (cfun->machine->callee_saved_last_gpr_regno <= 14)
587 /* Re = $r14 */
588 cfun->machine->callee_saved_last_gpr_regno = 14;
590 else if (cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM)
592 /* If last_regno is SP_REGNUM, which means
593 it is never changed, so set it to Re = $r6. */
594 cfun->machine->callee_saved_last_gpr_regno = 6;
596 else
598 /* The program flow should not go here. */
599 gcc_unreachable ();
603 int sp_adjust = cfun->machine->local_size
604 + cfun->machine->out_args_size
605 + cfun->machine->callee_saved_area_gpr_padding_bytes
606 + cfun->machine->callee_saved_fpr_regs_size;
608 if (!v3pushpop_p
609 && sp_adjust == 0
610 && !frame_pointer_needed)
612 block_size = cfun->machine->fp_size
613 + cfun->machine->gp_size
614 + cfun->machine->lp_size
615 + (4 * (cfun->machine->callee_saved_last_gpr_regno
616 - cfun->machine->callee_saved_first_gpr_regno
617 + 1));
619 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
621 /* $r14 is last callee save register. */
622 if (cfun->machine->callee_saved_last_gpr_regno
623 < NDS32_LAST_CALLEE_SAVE_GPR_REGNUM)
625 cfun->machine->callee_saved_last_gpr_regno++;
627 else if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
629 cfun->machine->callee_saved_first_gpr_regno
630 = NDS32_FIRST_CALLEE_SAVE_GPR_REGNUM;
631 cfun->machine->callee_saved_last_gpr_regno
632 = NDS32_FIRST_CALLEE_SAVE_GPR_REGNUM;
637 /* We have correctly set callee_saved_first_gpr_regno
638 and callee_saved_last_gpr_regno.
639 Initially, the callee_saved_gpr_regs_size is supposed to be 0.
640 As long as callee_saved_last_gpr_regno is not SP_REGNUM,
641 we can update callee_saved_gpr_regs_size with new size. */
642 if (cfun->machine->callee_saved_last_gpr_regno != SP_REGNUM)
644 /* Compute pushed size of callee-saved registers. */
645 cfun->machine->callee_saved_gpr_regs_size
646 = 4 * (cfun->machine->callee_saved_last_gpr_regno
647 - cfun->machine->callee_saved_first_gpr_regno
648 + 1);
651 if (TARGET_HARD_FLOAT)
653 /* Compute size of callee svaed floating-point registers. */
654 if (cfun->machine->callee_saved_last_fpr_regno != SP_REGNUM)
656 cfun->machine->callee_saved_fpr_regs_size
657 = 4 * (cfun->machine->callee_saved_last_fpr_regno
658 - cfun->machine->callee_saved_first_fpr_regno
659 + 1);
663 /* Important: We need to make sure that
664 (fp_size + gp_size + lp_size + callee_saved_gpr_regs_size)
665 is 8-byte alignment.
666 If it is not, calculate the padding bytes. */
667 block_size = cfun->machine->fp_size
668 + cfun->machine->gp_size
669 + cfun->machine->lp_size
670 + cfun->machine->callee_saved_gpr_regs_size;
671 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
673 cfun->machine->callee_saved_area_gpr_padding_bytes
674 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
677 /* If stack usage computation is required,
678 we need to provide the static stack size. */
679 if (flag_stack_usage_info)
681 current_function_static_stack_size
682 = NDS32_ROUND_UP_DOUBLE_WORD (block_size)
683 + cfun->machine->local_size
684 + cfun->machine->out_args_size;
688 /* Function to create a parallel rtx pattern
689 which presents stack push multiple behavior.
690 The overall concept are:
691 "push registers to memory",
692 "adjust stack pointer". */
693 static void
694 nds32_emit_stack_push_multiple (unsigned Rb, unsigned Re,
695 bool save_fp_p, bool save_gp_p, bool save_lp_p,
696 bool vaarg_p)
698 unsigned regno;
699 int extra_count;
700 int num_use_regs;
701 int par_index;
702 int offset;
704 rtx reg;
705 rtx mem;
706 rtx push_rtx;
707 rtx adjust_sp_rtx;
708 rtx parallel_insn;
709 rtx dwarf;
711 /* We need to provide a customized rtx which contains
712 necessary information for data analysis,
713 so we create a parallel rtx like this:
714 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
715 (reg:SI Rb))
716 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
717 (reg:SI Rb+1))
719 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
720 (reg:SI Re))
721 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
722 (reg:SI FP_REGNUM))
723 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
724 (reg:SI GP_REGNUM))
725 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
726 (reg:SI LP_REGNUM))
727 (set (reg:SI SP_REGNUM)
728 (plus (reg:SI SP_REGNUM) (const_int -32)))]) */
730 /* Calculate the number of registers that will be pushed. */
731 extra_count = 0;
732 if (save_fp_p)
733 extra_count++;
734 if (save_gp_p)
735 extra_count++;
736 if (save_lp_p)
737 extra_count++;
738 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
739 if (Rb == SP_REGNUM && Re == SP_REGNUM)
740 num_use_regs = extra_count;
741 else
742 num_use_regs = Re - Rb + 1 + extra_count;
744 /* In addition to used registers,
745 we need one more space for (set sp sp-x) rtx. */
746 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
747 rtvec_alloc (num_use_regs + 1));
748 par_index = 0;
750 /* Initialize offset and start to create push behavior. */
751 offset = -(num_use_regs * 4);
753 /* Create (set mem regX) from Rb, Rb+1 up to Re. */
754 for (regno = Rb; regno <= Re; regno++)
756 /* Rb and Re may be SP_REGNUM.
757 We need to break this loop immediately. */
758 if (regno == SP_REGNUM)
759 break;
761 reg = gen_rtx_REG (SImode, regno);
762 mem = gen_frame_mem (SImode, plus_constant (Pmode,
763 stack_pointer_rtx,
764 offset));
765 push_rtx = gen_rtx_SET (mem, reg);
766 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
767 RTX_FRAME_RELATED_P (push_rtx) = 1;
768 offset = offset + 4;
769 par_index++;
772 /* Create (set mem fp), (set mem gp), and (set mem lp) if necessary. */
773 if (save_fp_p)
775 reg = gen_rtx_REG (SImode, FP_REGNUM);
776 mem = gen_frame_mem (SImode, plus_constant (Pmode,
777 stack_pointer_rtx,
778 offset));
779 push_rtx = gen_rtx_SET (mem, reg);
780 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
781 RTX_FRAME_RELATED_P (push_rtx) = 1;
782 offset = offset + 4;
783 par_index++;
785 if (save_gp_p)
787 reg = gen_rtx_REG (SImode, GP_REGNUM);
788 mem = gen_frame_mem (SImode, plus_constant (Pmode,
789 stack_pointer_rtx,
790 offset));
791 push_rtx = gen_rtx_SET (mem, reg);
792 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
793 RTX_FRAME_RELATED_P (push_rtx) = 1;
794 offset = offset + 4;
795 par_index++;
797 if (save_lp_p)
799 reg = gen_rtx_REG (SImode, LP_REGNUM);
800 mem = gen_frame_mem (SImode, plus_constant (Pmode,
801 stack_pointer_rtx,
802 offset));
803 push_rtx = gen_rtx_SET (mem, reg);
804 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
805 RTX_FRAME_RELATED_P (push_rtx) = 1;
806 offset = offset + 4;
807 par_index++;
810 /* Create (set sp sp-x). */
812 /* We need to re-calculate the offset value again for adjustment. */
813 offset = -(num_use_regs * 4);
814 adjust_sp_rtx
815 = gen_rtx_SET (stack_pointer_rtx,
816 plus_constant (Pmode, stack_pointer_rtx, offset));
817 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
818 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
820 parallel_insn = emit_insn (parallel_insn);
822 /* The insn rtx 'parallel_insn' will change frame layout.
823 We need to use RTX_FRAME_RELATED_P so that GCC is able to
824 generate CFI (Call Frame Information) stuff. */
825 RTX_FRAME_RELATED_P (parallel_insn) = 1;
827 /* Don't use GCC's logic for CFI info if we are generate a push for VAARG
828 since we will not restore those register at epilogue. */
829 if (vaarg_p)
831 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
832 copy_rtx (adjust_sp_rtx), NULL_RTX);
833 REG_NOTES (parallel_insn) = dwarf;
837 /* Function to create a parallel rtx pattern
838 which presents stack pop multiple behavior.
839 The overall concept are:
840 "pop registers from memory",
841 "adjust stack pointer". */
842 static void
843 nds32_emit_stack_pop_multiple (unsigned Rb, unsigned Re,
844 bool save_fp_p, bool save_gp_p, bool save_lp_p)
846 unsigned regno;
847 int extra_count;
848 int num_use_regs;
849 int par_index;
850 int offset;
852 rtx reg;
853 rtx mem;
854 rtx pop_rtx;
855 rtx adjust_sp_rtx;
856 rtx parallel_insn;
857 rtx dwarf = NULL_RTX;
859 /* We need to provide a customized rtx which contains
860 necessary information for data analysis,
861 so we create a parallel rtx like this:
862 (parallel [(set (reg:SI Rb)
863 (mem (reg:SI SP_REGNUM)))
864 (set (reg:SI Rb+1)
865 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
867 (set (reg:SI Re)
868 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
869 (set (reg:SI FP_REGNUM)
870 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
871 (set (reg:SI GP_REGNUM)
872 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
873 (set (reg:SI LP_REGNUM)
874 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
875 (set (reg:SI SP_REGNUM)
876 (plus (reg:SI SP_REGNUM) (const_int 32)))]) */
878 /* Calculate the number of registers that will be poped. */
879 extra_count = 0;
880 if (save_fp_p)
881 extra_count++;
882 if (save_gp_p)
883 extra_count++;
884 if (save_lp_p)
885 extra_count++;
886 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
887 if (Rb == SP_REGNUM && Re == SP_REGNUM)
888 num_use_regs = extra_count;
889 else
890 num_use_regs = Re - Rb + 1 + extra_count;
892 /* In addition to used registers,
893 we need one more space for (set sp sp+x) rtx. */
894 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
895 rtvec_alloc (num_use_regs + 1));
896 par_index = 0;
898 /* Initialize offset and start to create pop behavior. */
899 offset = 0;
901 /* Create (set regX mem) from Rb, Rb+1 up to Re. */
902 for (regno = Rb; regno <= Re; regno++)
904 /* Rb and Re may be SP_REGNUM.
905 We need to break this loop immediately. */
906 if (regno == SP_REGNUM)
907 break;
909 reg = gen_rtx_REG (SImode, regno);
910 mem = gen_frame_mem (SImode, plus_constant (Pmode,
911 stack_pointer_rtx,
912 offset));
913 pop_rtx = gen_rtx_SET (reg, mem);
914 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
915 RTX_FRAME_RELATED_P (pop_rtx) = 1;
916 offset = offset + 4;
917 par_index++;
919 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
922 /* Create (set fp mem), (set gp mem), and (set lp mem) if necessary. */
923 if (save_fp_p)
925 reg = gen_rtx_REG (SImode, FP_REGNUM);
926 mem = gen_frame_mem (SImode, plus_constant (Pmode,
927 stack_pointer_rtx,
928 offset));
929 pop_rtx = gen_rtx_SET (reg, mem);
930 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
931 RTX_FRAME_RELATED_P (pop_rtx) = 1;
932 offset = offset + 4;
933 par_index++;
935 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
937 if (save_gp_p)
939 reg = gen_rtx_REG (SImode, GP_REGNUM);
940 mem = gen_frame_mem (SImode, plus_constant (Pmode,
941 stack_pointer_rtx,
942 offset));
943 pop_rtx = gen_rtx_SET (reg, mem);
944 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
945 RTX_FRAME_RELATED_P (pop_rtx) = 1;
946 offset = offset + 4;
947 par_index++;
949 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
951 if (save_lp_p)
953 reg = gen_rtx_REG (SImode, LP_REGNUM);
954 mem = gen_frame_mem (SImode, plus_constant (Pmode,
955 stack_pointer_rtx,
956 offset));
957 pop_rtx = gen_rtx_SET (reg, mem);
958 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
959 RTX_FRAME_RELATED_P (pop_rtx) = 1;
960 offset = offset + 4;
961 par_index++;
963 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
966 /* Create (set sp sp+x). */
968 /* The offset value is already in place. No need to re-calculate it. */
969 adjust_sp_rtx
970 = gen_rtx_SET (stack_pointer_rtx,
971 plus_constant (Pmode, stack_pointer_rtx, offset));
972 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
974 /* Tell gcc we adjust SP in this insn. */
975 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
977 parallel_insn = emit_insn (parallel_insn);
979 /* The insn rtx 'parallel_insn' will change frame layout.
980 We need to use RTX_FRAME_RELATED_P so that GCC is able to
981 generate CFI (Call Frame Information) stuff. */
982 RTX_FRAME_RELATED_P (parallel_insn) = 1;
984 /* Add CFI info by manual. */
985 REG_NOTES (parallel_insn) = dwarf;
988 /* Function to create a parallel rtx pattern
989 which presents stack v3push behavior.
990 The overall concept are:
991 "push registers to memory",
992 "adjust stack pointer". */
993 static void
994 nds32_emit_stack_v3push (unsigned Rb,
995 unsigned Re,
996 unsigned imm8u)
998 unsigned regno;
999 int num_use_regs;
1000 int par_index;
1001 int offset;
1003 rtx reg;
1004 rtx mem;
1005 rtx push_rtx;
1006 rtx adjust_sp_rtx;
1007 rtx parallel_insn;
1009 /* We need to provide a customized rtx which contains
1010 necessary information for data analysis,
1011 so we create a parallel rtx like this:
1012 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
1013 (reg:SI Rb))
1014 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
1015 (reg:SI Rb+1))
1017 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
1018 (reg:SI Re))
1019 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
1020 (reg:SI FP_REGNUM))
1021 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
1022 (reg:SI GP_REGNUM))
1023 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
1024 (reg:SI LP_REGNUM))
1025 (set (reg:SI SP_REGNUM)
1026 (plus (reg:SI SP_REGNUM) (const_int -32-imm8u)))]) */
1028 /* Calculate the number of registers that will be pushed.
1029 Since $fp, $gp, and $lp is always pushed with v3push instruction,
1030 we need to count these three registers.
1031 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1032 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1033 num_use_regs = Re - Rb + 1 + 3;
1035 /* In addition to used registers,
1036 we need one more space for (set sp sp-x-imm8u) rtx. */
1037 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
1038 rtvec_alloc (num_use_regs + 1));
1039 par_index = 0;
1041 /* Initialize offset and start to create push behavior. */
1042 offset = -(num_use_regs * 4);
1044 /* Create (set mem regX) from Rb, Rb+1 up to Re.
1045 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1046 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1047 for (regno = Rb; regno <= Re; regno++)
1049 reg = gen_rtx_REG (SImode, regno);
1050 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1051 stack_pointer_rtx,
1052 offset));
1053 push_rtx = gen_rtx_SET (mem, reg);
1054 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1055 RTX_FRAME_RELATED_P (push_rtx) = 1;
1056 offset = offset + 4;
1057 par_index++;
1060 /* Create (set mem fp). */
1061 reg = gen_rtx_REG (SImode, FP_REGNUM);
1062 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1063 stack_pointer_rtx,
1064 offset));
1065 push_rtx = gen_rtx_SET (mem, reg);
1066 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1067 RTX_FRAME_RELATED_P (push_rtx) = 1;
1068 offset = offset + 4;
1069 par_index++;
1070 /* Create (set mem gp). */
1071 reg = gen_rtx_REG (SImode, GP_REGNUM);
1072 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1073 stack_pointer_rtx,
1074 offset));
1075 push_rtx = gen_rtx_SET (mem, reg);
1076 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1077 RTX_FRAME_RELATED_P (push_rtx) = 1;
1078 offset = offset + 4;
1079 par_index++;
1080 /* Create (set mem lp). */
1081 reg = gen_rtx_REG (SImode, LP_REGNUM);
1082 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1083 stack_pointer_rtx,
1084 offset));
1085 push_rtx = gen_rtx_SET (mem, reg);
1086 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1087 RTX_FRAME_RELATED_P (push_rtx) = 1;
1088 offset = offset + 4;
1089 par_index++;
1091 /* Create (set sp sp-x-imm8u). */
1093 /* We need to re-calculate the offset value again for adjustment. */
1094 offset = -(num_use_regs * 4);
1095 adjust_sp_rtx
1096 = gen_rtx_SET (stack_pointer_rtx,
1097 plus_constant (Pmode,
1098 stack_pointer_rtx,
1099 offset - imm8u));
1100 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
1101 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
1103 parallel_insn = emit_insn (parallel_insn);
1105 /* The insn rtx 'parallel_insn' will change frame layout.
1106 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1107 generate CFI (Call Frame Information) stuff. */
1108 RTX_FRAME_RELATED_P (parallel_insn) = 1;
1111 /* Function to create a parallel rtx pattern
1112 which presents stack v3pop behavior.
1113 The overall concept are:
1114 "pop registers from memory",
1115 "adjust stack pointer". */
1116 static void
1117 nds32_emit_stack_v3pop (unsigned Rb,
1118 unsigned Re,
1119 unsigned imm8u)
1121 unsigned regno;
1122 int num_use_regs;
1123 int par_index;
1124 int offset;
1126 rtx reg;
1127 rtx mem;
1128 rtx pop_rtx;
1129 rtx adjust_sp_rtx;
1130 rtx parallel_insn;
1131 rtx dwarf = NULL_RTX;
1133 /* We need to provide a customized rtx which contains
1134 necessary information for data analysis,
1135 so we create a parallel rtx like this:
1136 (parallel [(set (reg:SI Rb)
1137 (mem (reg:SI SP_REGNUM)))
1138 (set (reg:SI Rb+1)
1139 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
1141 (set (reg:SI Re)
1142 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
1143 (set (reg:SI FP_REGNUM)
1144 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
1145 (set (reg:SI GP_REGNUM)
1146 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
1147 (set (reg:SI LP_REGNUM)
1148 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
1149 (set (reg:SI SP_REGNUM)
1150 (plus (reg:SI SP_REGNUM) (const_int 32+imm8u)))]) */
1152 /* Calculate the number of registers that will be poped.
1153 Since $fp, $gp, and $lp is always poped with v3pop instruction,
1154 we need to count these three registers.
1155 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1156 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1157 num_use_regs = Re - Rb + 1 + 3;
1159 /* In addition to used registers,
1160 we need one more space for (set sp sp+x+imm8u) rtx. */
1161 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
1162 rtvec_alloc (num_use_regs + 1));
1163 par_index = 0;
1165 /* Initialize offset and start to create pop behavior. */
1166 offset = 0;
1168 /* Create (set regX mem) from Rb, Rb+1 up to Re.
1169 Under v3pop, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1170 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1171 for (regno = Rb; regno <= Re; regno++)
1173 reg = gen_rtx_REG (SImode, regno);
1174 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1175 stack_pointer_rtx,
1176 offset));
1177 pop_rtx = gen_rtx_SET (reg, mem);
1178 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1179 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1180 offset = offset + 4;
1181 par_index++;
1183 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1186 /* Create (set fp mem). */
1187 reg = gen_rtx_REG (SImode, FP_REGNUM);
1188 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1189 stack_pointer_rtx,
1190 offset));
1191 pop_rtx = gen_rtx_SET (reg, mem);
1192 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1193 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1194 offset = offset + 4;
1195 par_index++;
1196 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1198 /* Create (set gp mem). */
1199 reg = gen_rtx_REG (SImode, GP_REGNUM);
1200 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1201 stack_pointer_rtx,
1202 offset));
1203 pop_rtx = gen_rtx_SET (reg, mem);
1204 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1205 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1206 offset = offset + 4;
1207 par_index++;
1208 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1210 /* Create (set lp mem ). */
1211 reg = gen_rtx_REG (SImode, LP_REGNUM);
1212 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1213 stack_pointer_rtx,
1214 offset));
1215 pop_rtx = gen_rtx_SET (reg, mem);
1216 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1217 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1218 offset = offset + 4;
1219 par_index++;
1220 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1222 /* Create (set sp sp+x+imm8u). */
1224 /* The offset value is already in place. No need to re-calculate it. */
1225 adjust_sp_rtx
1226 = gen_rtx_SET (stack_pointer_rtx,
1227 plus_constant (Pmode,
1228 stack_pointer_rtx,
1229 offset + imm8u));
1230 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
1232 if (frame_pointer_needed)
1234 /* (expr_list:REG_CFA_DEF_CFA (plus:SI (reg/f:SI $sp)
1235 (const_int 0))
1236 mean reset frame pointer to $sp and reset to offset 0. */
1237 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1238 const0_rtx);
1239 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
1241 else
1243 /* Tell gcc we adjust SP in this insn. */
1244 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
1245 copy_rtx (adjust_sp_rtx), dwarf);
1248 parallel_insn = emit_insn (parallel_insn);
1250 /* The insn rtx 'parallel_insn' will change frame layout.
1251 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1252 generate CFI (Call Frame Information) stuff. */
1253 RTX_FRAME_RELATED_P (parallel_insn) = 1;
1255 /* Add CFI info by manual. */
1256 REG_NOTES (parallel_insn) = dwarf;
1259 /* Function that may creates more instructions
1260 for large value on adjusting stack pointer.
1262 In nds32 target, 'addi' can be used for stack pointer
1263 adjustment in prologue/epilogue stage.
1264 However, sometimes there are too many local variables so that
1265 the adjustment value is not able to be fit in the 'addi' instruction.
1266 One solution is to move value into a register
1267 and then use 'add' instruction.
1268 In practice, we use TA_REGNUM ($r15) to accomplish this purpose. */
1269 static void
1270 nds32_emit_adjust_frame (rtx to_reg, rtx from_reg, int adjust_value)
1272 rtx tmp_reg;
1273 rtx frame_adjust_insn;
1274 rtx adjust_value_rtx = GEN_INT (adjust_value);
1276 if (adjust_value == 0)
1277 return;
1279 if (!satisfies_constraint_Is15 (adjust_value_rtx))
1281 /* The value is not able to fit in single addi instruction.
1282 Create more instructions of moving value into a register
1283 and then add stack pointer with it. */
1285 /* $r15 is going to be temporary register to hold the value. */
1286 tmp_reg = gen_rtx_REG (SImode, TA_REGNUM);
1288 /* Create one more instruction to move value
1289 into the temporary register. */
1290 emit_move_insn (tmp_reg, adjust_value_rtx);
1292 /* Create new 'add' rtx. */
1293 frame_adjust_insn = gen_addsi3 (to_reg,
1294 from_reg,
1295 tmp_reg);
1296 /* Emit rtx into insn list and receive its transformed insn rtx. */
1297 frame_adjust_insn = emit_insn (frame_adjust_insn);
1299 /* Because (tmp_reg <- full_value) may be split into two
1300 rtl patterns, we can not set its RTX_FRAME_RELATED_P.
1301 We need to construct another (sp <- sp + full_value)
1302 and then insert it into sp_adjust_insn's reg note to
1303 represent a frame related expression.
1304 GCC knows how to refer it and output debug information. */
1306 rtx plus_rtx;
1307 rtx set_rtx;
1309 plus_rtx = plus_constant (Pmode, from_reg, adjust_value);
1310 set_rtx = gen_rtx_SET (to_reg, plus_rtx);
1311 add_reg_note (frame_adjust_insn, REG_FRAME_RELATED_EXPR, set_rtx);
1313 else
1315 /* Generate sp adjustment instruction if and only if sp_adjust != 0. */
1316 frame_adjust_insn = gen_addsi3 (to_reg,
1317 from_reg,
1318 adjust_value_rtx);
1319 /* Emit rtx into instructions list and receive INSN rtx form. */
1320 frame_adjust_insn = emit_insn (frame_adjust_insn);
1323 /* The insn rtx 'sp_adjust_insn' will change frame layout.
1324 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1325 generate CFI (Call Frame Information) stuff. */
1326 RTX_FRAME_RELATED_P (frame_adjust_insn) = 1;
1329 /* Return true if MODE/TYPE need double word alignment. */
1330 static bool
1331 nds32_needs_double_word_align (machine_mode mode, const_tree type)
1333 unsigned int align;
1335 /* Pick up the alignment according to the mode or type. */
1336 align = NDS32_MODE_TYPE_ALIGN (mode, type);
1338 return (align > PARM_BOUNDARY);
1341 /* Return true if FUNC is a naked function. */
1342 static bool
1343 nds32_naked_function_p (tree func)
1345 tree t;
1347 if (TREE_CODE (func) != FUNCTION_DECL)
1348 abort ();
1350 t = lookup_attribute ("naked", DECL_ATTRIBUTES (func));
1352 return (t != NULL_TREE);
1355 /* Function that check if 'X' is a valid address register.
1356 The variable 'STRICT' is very important to
1357 make decision for register number.
1359 STRICT : true
1360 => We are in reload pass or after reload pass.
1361 The register number should be strictly limited in general registers.
1363 STRICT : false
1364 => Before reload pass, we are free to use any register number. */
1365 static bool
1366 nds32_address_register_rtx_p (rtx x, bool strict)
1368 int regno;
1370 if (GET_CODE (x) != REG)
1371 return false;
1373 regno = REGNO (x);
1375 if (strict)
1376 return REGNO_OK_FOR_BASE_P (regno);
1377 else
1378 return true;
1381 /* Function that check if 'INDEX' is valid to be a index rtx for address.
1383 OUTER_MODE : Machine mode of outer address rtx.
1384 INDEX : Check if this rtx is valid to be a index for address.
1385 STRICT : If it is true, we are in reload pass or after reload pass. */
1386 static bool
1387 nds32_legitimate_index_p (machine_mode outer_mode,
1388 rtx index,
1389 bool strict)
1391 int regno;
1392 rtx op0;
1393 rtx op1;
1395 switch (GET_CODE (index))
1397 case REG:
1398 regno = REGNO (index);
1399 /* If we are in reload pass or after reload pass,
1400 we need to limit it to general register. */
1401 if (strict)
1402 return REGNO_OK_FOR_INDEX_P (regno);
1403 else
1404 return true;
1406 case CONST_INT:
1407 /* The alignment of the integer value is determined by 'outer_mode'. */
1408 switch (GET_MODE_SIZE (outer_mode))
1410 case 1:
1411 /* Further check if the value is legal for the 'outer_mode'. */
1412 if (satisfies_constraint_Is15 (index))
1413 return true;
1414 break;
1416 case 2:
1417 /* Further check if the value is legal for the 'outer_mode'. */
1418 if (satisfies_constraint_Is16 (index))
1420 /* If it is not under strictly aligned situation,
1421 we can return true without checking alignment. */
1422 if (!cfun->machine->strict_aligned_p)
1423 return true;
1424 /* Make sure address is half word alignment. */
1425 else if (NDS32_HALF_WORD_ALIGN_P (INTVAL (index)))
1426 return true;
1428 break;
1430 case 4:
1431 /* Further check if the value is legal for the 'outer_mode'. */
1432 if (satisfies_constraint_Is17 (index))
1434 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
1436 if (!satisfies_constraint_Is14 (index))
1437 return false;
1440 /* If it is not under strictly aligned situation,
1441 we can return true without checking alignment. */
1442 if (!cfun->machine->strict_aligned_p)
1443 return true;
1444 /* Make sure address is word alignment. */
1445 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1446 return true;
1448 break;
1450 case 8:
1451 if (satisfies_constraint_Is17 (gen_int_mode (INTVAL (index) + 4,
1452 SImode)))
1454 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
1456 if (!satisfies_constraint_Is14 (index))
1457 return false;
1460 /* If it is not under strictly aligned situation,
1461 we can return true without checking alignment. */
1462 if (!cfun->machine->strict_aligned_p)
1463 return true;
1464 /* Make sure address is word alignment.
1465 Currently we do not have 64-bit load/store yet,
1466 so we will use two 32-bit load/store instructions to do
1467 memory access and they are single word alignment. */
1468 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1469 return true;
1471 break;
1473 default:
1474 return false;
1477 return false;
1479 case MULT:
1480 op0 = XEXP (index, 0);
1481 op1 = XEXP (index, 1);
1483 if (REG_P (op0) && CONST_INT_P (op1))
1485 int multiplier;
1486 multiplier = INTVAL (op1);
1488 /* We only allow (mult reg const_int_1), (mult reg const_int_2),
1489 (mult reg const_int_4) or (mult reg const_int_8). */
1490 if (multiplier != 1 && multiplier != 2
1491 && multiplier != 4 && multiplier != 8)
1492 return false;
1494 regno = REGNO (op0);
1495 /* Limit it in general registers if we are
1496 in reload pass or after reload pass. */
1497 if(strict)
1498 return REGNO_OK_FOR_INDEX_P (regno);
1499 else
1500 return true;
1503 return false;
1505 case ASHIFT:
1506 op0 = XEXP (index, 0);
1507 op1 = XEXP (index, 1);
1509 if (REG_P (op0) && CONST_INT_P (op1))
1511 int sv;
1512 /* op1 is already the sv value for use to do left shift. */
1513 sv = INTVAL (op1);
1515 /* We only allow (ashift reg const_int_0)
1516 or (ashift reg const_int_1) or (ashift reg const_int_2) or
1517 (ashift reg const_int_3). */
1518 if (sv != 0 && sv != 1 && sv !=2 && sv != 3)
1519 return false;
1521 regno = REGNO (op0);
1522 /* Limit it in general registers if we are
1523 in reload pass or after reload pass. */
1524 if(strict)
1525 return REGNO_OK_FOR_INDEX_P (regno);
1526 else
1527 return true;
1530 return false;
1532 default:
1533 return false;
1537 static void
1538 nds32_register_pass (
1539 rtl_opt_pass *(*make_pass_func) (gcc::context *),
1540 enum pass_positioning_ops pass_pos,
1541 const char *ref_pass_name)
1543 opt_pass *new_opt_pass = make_pass_func (g);
1545 struct register_pass_info insert_pass =
1547 new_opt_pass, /* pass */
1548 ref_pass_name, /* reference_pass_name */
1549 1, /* ref_pass_instance_number */
1550 pass_pos /* po_op */
1553 register_pass (&insert_pass);
1556 /* This function is called from nds32_option_override ().
1557 All new passes should be registered here. */
1558 static void
1559 nds32_register_passes (void)
1561 nds32_register_pass (
1562 make_pass_nds32_relax_opt,
1563 PASS_POS_INSERT_AFTER,
1564 "mach");
1567 /* ------------------------------------------------------------------------ */
1569 /* PART 3: Implement target hook stuff definitions. */
1572 /* Computing the Length of an Insn.
1573 Modifies the length assigned to instruction INSN.
1574 LEN is the initially computed length of the insn. */
1576 nds32_adjust_insn_length (rtx_insn *insn, int length)
1578 int adjust_value = 0;
1579 switch (recog_memoized (insn))
1581 case CODE_FOR_call_internal:
1582 case CODE_FOR_call_value_internal:
1584 if (NDS32_ALIGN_P ())
1586 rtx_insn *next_insn = next_active_insn (insn);
1587 if (next_insn && get_attr_length (next_insn) != 2)
1588 adjust_value += 2;
1590 /* We need insert a nop after a noretun function call
1591 to prevent software breakpoint corrupt the next function. */
1592 if (find_reg_note (insn, REG_NORETURN, NULL_RTX))
1594 if (TARGET_16_BIT)
1595 adjust_value += 2;
1596 else
1597 adjust_value += 4;
1600 return length + adjust_value;
1602 default:
1603 return length;
1607 /* Storage Layout. */
1609 /* This function will be called just before expansion into rtl. */
1610 static void
1611 nds32_expand_to_rtl_hook (void)
1613 /* We need to set strictly aligned situation.
1614 After that, the memory address checking in nds32_legitimate_address_p()
1615 will take alignment offset into consideration so that it will not create
1616 unaligned [base + offset] access during the rtl optimization. */
1617 cfun->machine->strict_aligned_p = 1;
1621 /* Register Usage. */
1623 static void
1624 nds32_conditional_register_usage (void)
1626 int regno;
1628 if (TARGET_HARD_FLOAT)
1630 for (regno = NDS32_FIRST_FPR_REGNUM;
1631 regno <= NDS32_LAST_FPR_REGNUM; regno++)
1633 fixed_regs[regno] = 0;
1634 if (regno < NDS32_FIRST_FPR_REGNUM + NDS32_MAX_FPR_REGS_FOR_ARGS)
1635 call_used_regs[regno] = 1;
1636 else if (regno >= NDS32_FIRST_FPR_REGNUM + 22
1637 && regno < NDS32_FIRST_FPR_REGNUM + 48)
1638 call_used_regs[regno] = 1;
1639 else
1640 call_used_regs[regno] = 0;
1643 else if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
1645 for (regno = NDS32_FIRST_FPR_REGNUM;
1646 regno <= NDS32_LAST_FPR_REGNUM;
1647 regno++)
1648 fixed_regs[regno] = 0;
1653 /* Register Classes. */
1655 static unsigned char
1656 nds32_class_max_nregs (reg_class_t rclass ATTRIBUTE_UNUSED,
1657 machine_mode mode)
1659 /* Return the maximum number of consecutive registers
1660 needed to represent "mode" in a register of "rclass". */
1661 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1664 static int
1665 nds32_register_priority (int hard_regno)
1667 /* Encourage to use r0-r7 for LRA when optimize for size. */
1668 if (optimize_size)
1670 if (hard_regno < 8)
1671 return 4;
1672 else if (hard_regno < 16)
1673 return 3;
1674 else if (hard_regno < 28)
1675 return 2;
1676 else
1677 return 1;
1679 else
1681 if (hard_regno > 27)
1682 return 1;
1683 else
1684 return 4;
1688 static bool
1689 nds32_can_change_mode_class (machine_mode from,
1690 machine_mode to,
1691 reg_class_t rclass)
1693 /* Don't spill double-precision register to two singal-precision
1694 registers */
1695 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
1696 && GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
1698 return !reg_classes_intersect_p (rclass, FP_REGS);
1701 return true;
1705 /* Stack Layout and Calling Conventions. */
1707 /* There are three kinds of pointer concepts using in GCC compiler:
1709 frame pointer: A pointer to the first location of local variables.
1710 stack pointer: A pointer to the top of a stack frame.
1711 argument pointer: A pointer to the incoming arguments.
1713 In nds32 target calling convention, we are using 8-byte alignment.
1714 Besides, we would like to have each stack frame of a function includes:
1716 [Block A]
1717 1. previous hard frame pointer
1718 2. return address
1719 3. callee-saved registers
1720 4. <padding bytes> (we will calculte in nds32_compute_stack_frame()
1721 and save it at
1722 cfun->machine->callee_saved_area_padding_bytes)
1724 [Block B]
1725 1. local variables
1726 2. spilling location
1727 3. <padding bytes> (it will be calculated by GCC itself)
1728 4. incoming arguments
1729 5. <padding bytes> (it will be calculated by GCC itself)
1731 [Block C]
1732 1. <padding bytes> (it will be calculated by GCC itself)
1733 2. outgoing arguments
1735 We 'wrap' these blocks together with
1736 hard frame pointer ($r28) and stack pointer ($r31).
1737 By applying the basic frame/stack/argument pointers concept,
1738 the layout of a stack frame shoule be like this:
1741 old stack pointer -> ----
1742 | | \
1743 | | saved arguments for
1744 | | vararg functions
1745 | | /
1746 hard frame pointer -> --
1747 & argument pointer | | \
1748 | | previous hardware frame pointer
1749 | | return address
1750 | | callee-saved registers
1751 | | /
1752 frame pointer -> --
1753 | | \
1754 | | local variables
1755 | | and incoming arguments
1756 | | /
1758 | | \
1759 | | outgoing
1760 | | arguments
1761 | | /
1762 stack pointer -> ----
1764 $SFP and $AP are used to represent frame pointer and arguments pointer,
1765 which will be both eliminated as hard frame pointer. */
1767 /* -- Eliminating Frame Pointer and Arg Pointer. */
1769 static bool
1770 nds32_can_eliminate (const int from_reg, const int to_reg)
1772 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1773 return true;
1775 if (from_reg == ARG_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1776 return true;
1778 if (from_reg == FRAME_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1779 return true;
1781 if (from_reg == FRAME_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1782 return true;
1784 return false;
1787 /* -- Passing Arguments in Registers. */
1789 static rtx
1790 nds32_function_arg (cumulative_args_t ca, machine_mode mode,
1791 const_tree type, bool named)
1793 unsigned int regno;
1794 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1796 /* The last time this hook is called,
1797 it is called with MODE == VOIDmode. */
1798 if (mode == VOIDmode)
1799 return NULL_RTX;
1801 /* For nameless arguments, we need to take care it individually. */
1802 if (!named)
1804 /* If we are under hard float abi, we have arguments passed on the
1805 stack and all situation can be handled by GCC itself. */
1806 if (TARGET_HARD_FLOAT)
1807 return NULL_RTX;
1809 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1811 /* If we still have enough registers to pass argument, pick up
1812 next available register number. */
1813 regno
1814 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1815 return gen_rtx_REG (mode, regno);
1818 /* No register available, return NULL_RTX.
1819 The compiler will use stack to pass argument instead. */
1820 return NULL_RTX;
1823 /* The following is to handle named argument.
1824 Note that the strategies of TARGET_HARD_FLOAT and !TARGET_HARD_FLOAT
1825 are different. */
1826 if (TARGET_HARD_FLOAT)
1828 /* For TARGET_HARD_FLOAT calling convention, we use GPR and FPR
1829 to pass argument. We have to further check TYPE and MODE so
1830 that we can determine which kind of register we shall use. */
1832 /* Note that we need to pass argument entirely in registers under
1833 hard float abi. */
1834 if (GET_MODE_CLASS (mode) == MODE_FLOAT
1835 && NDS32_ARG_ENTIRE_IN_FPR_REG_P (cum->fpr_offset, mode, type))
1837 /* Pick up the next available FPR register number. */
1838 regno
1839 = NDS32_AVAILABLE_REGNUM_FOR_FPR_ARG (cum->fpr_offset, mode, type);
1840 return gen_rtx_REG (mode, regno);
1842 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
1843 && NDS32_ARG_ENTIRE_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1845 /* Pick up the next available GPR register number. */
1846 regno
1847 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1848 return gen_rtx_REG (mode, regno);
1851 else
1853 /* For !TARGET_HARD_FLOAT calling convention, we always use GPR to pass
1854 argument. Since we allow to pass argument partially in registers,
1855 we can just return it if there are still registers available. */
1856 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1858 /* Pick up the next available register number. */
1859 regno
1860 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1861 return gen_rtx_REG (mode, regno);
1866 /* No register available, return NULL_RTX.
1867 The compiler will use stack to pass argument instead. */
1868 return NULL_RTX;
1871 static bool
1872 nds32_must_pass_in_stack (machine_mode mode, const_tree type)
1874 /* Return true if a type must be passed in memory.
1875 If it is NOT using hard float abi, small aggregates can be
1876 passed in a register even we are calling a variadic function.
1877 So there is no need to take padding into consideration. */
1878 if (TARGET_HARD_FLOAT)
1879 return must_pass_in_stack_var_size_or_pad (mode, type);
1880 else
1881 return must_pass_in_stack_var_size (mode, type);
1884 static int
1885 nds32_arg_partial_bytes (cumulative_args_t ca, machine_mode mode,
1886 tree type, bool named ATTRIBUTE_UNUSED)
1888 /* Returns the number of bytes at the beginning of an argument that
1889 must be put in registers. The value must be zero for arguments that are
1890 passed entirely in registers or that are entirely pushed on the stack.
1891 Besides, TARGET_FUNCTION_ARG for these arguments should return the
1892 first register to be used by the caller for this argument. */
1893 unsigned int needed_reg_count;
1894 unsigned int remaining_reg_count;
1895 CUMULATIVE_ARGS *cum;
1897 cum = get_cumulative_args (ca);
1899 /* Under hard float abi, we better have argument entirely passed in
1900 registers or pushed on the stack so that we can reduce the complexity
1901 of dealing with cum->gpr_offset and cum->fpr_offset. */
1902 if (TARGET_HARD_FLOAT)
1903 return 0;
1905 /* If we have already runned out of argument registers, return zero
1906 so that the argument will be entirely pushed on the stack. */
1907 if (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1908 >= NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS)
1909 return 0;
1911 /* Calculate how many registers do we need for this argument. */
1912 needed_reg_count = NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1914 /* Calculate how many argument registers have left for passing argument.
1915 Note that we should count it from next available register number. */
1916 remaining_reg_count
1917 = NDS32_MAX_GPR_REGS_FOR_ARGS
1918 - (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1919 - NDS32_GPR_ARG_FIRST_REGNUM);
1921 /* Note that we have to return the nubmer of bytes, not registers count. */
1922 if (needed_reg_count > remaining_reg_count)
1923 return remaining_reg_count * UNITS_PER_WORD;
1925 return 0;
1928 static void
1929 nds32_function_arg_advance (cumulative_args_t ca, machine_mode mode,
1930 const_tree type, bool named)
1932 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1934 if (named)
1936 /* We need to further check TYPE and MODE so that we can determine
1937 which kind of register we shall advance. */
1939 /* Under hard float abi, we may advance FPR registers. */
1940 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
1942 cum->fpr_offset
1943 = NDS32_AVAILABLE_REGNUM_FOR_FPR_ARG (cum->fpr_offset, mode, type)
1944 - NDS32_FPR_ARG_FIRST_REGNUM
1945 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1947 else
1949 cum->gpr_offset
1950 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1951 - NDS32_GPR_ARG_FIRST_REGNUM
1952 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1955 else
1957 /* If this nameless argument is NOT under TARGET_HARD_FLOAT,
1958 we can advance next register as well so that caller is
1959 able to pass arguments in registers and callee must be
1960 in charge of pushing all of them into stack. */
1961 if (!TARGET_HARD_FLOAT)
1963 cum->gpr_offset
1964 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1965 - NDS32_GPR_ARG_FIRST_REGNUM
1966 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1971 static unsigned int
1972 nds32_function_arg_boundary (machine_mode mode, const_tree type)
1974 return (nds32_needs_double_word_align (mode, type)
1975 ? NDS32_DOUBLE_WORD_ALIGNMENT
1976 : PARM_BOUNDARY);
1979 /* -- How Scalar Function Values Are Returned. */
1981 static rtx
1982 nds32_function_value (const_tree ret_type,
1983 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1984 bool outgoing ATTRIBUTE_UNUSED)
1986 machine_mode mode;
1987 int unsignedp;
1989 mode = TYPE_MODE (ret_type);
1990 unsignedp = TYPE_UNSIGNED (ret_type);
1992 if (INTEGRAL_TYPE_P (ret_type))
1993 mode = promote_mode (ret_type, mode, &unsignedp);
1995 if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
1996 return gen_rtx_REG (mode, NDS32_FPR_RET_FIRST_REGNUM);
1997 else
1998 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
2001 static rtx
2002 nds32_libcall_value (machine_mode mode,
2003 const_rtx fun ATTRIBUTE_UNUSED)
2005 if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
2006 return gen_rtx_REG (mode, NDS32_FPR_RET_FIRST_REGNUM);
2008 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
2011 static bool
2012 nds32_function_value_regno_p (const unsigned int regno)
2014 if (regno == NDS32_GPR_RET_FIRST_REGNUM
2015 || (TARGET_HARD_FLOAT
2016 && regno == NDS32_FPR_RET_FIRST_REGNUM))
2017 return true;
2019 return false;
2022 /* -- How Large Values Are Returned. */
2024 static bool
2025 nds32_return_in_memory (const_tree type,
2026 const_tree fntype ATTRIBUTE_UNUSED)
2028 /* Note that int_size_in_bytes can return -1 if the size can vary
2029 or is larger than an integer. */
2030 HOST_WIDE_INT size = int_size_in_bytes (type);
2032 /* For COMPLEX_TYPE, if the total size cannot be hold within two registers,
2033 the return value is supposed to be in memory. We need to be aware of
2034 that the size may be -1. */
2035 if (TREE_CODE (type) == COMPLEX_TYPE)
2036 if (size < 0 || size > 2 * UNITS_PER_WORD)
2037 return true;
2039 /* If it is BLKmode and the total size cannot be hold within two registers,
2040 the return value is supposed to be in memory. We need to be aware of
2041 that the size may be -1. */
2042 if (TYPE_MODE (type) == BLKmode)
2043 if (size < 0 || size > 2 * UNITS_PER_WORD)
2044 return true;
2046 /* For other cases, having result in memory is unnecessary. */
2047 return false;
2050 /* -- Function Entry and Exit. */
2052 /* The content produced from this function
2053 will be placed before prologue body. */
2054 static void
2055 nds32_asm_function_prologue (FILE *file)
2057 int r;
2058 const char *func_name;
2059 tree attrs;
2060 tree name;
2062 /* All stack frame information is supposed to be
2063 already computed when expanding prologue.
2064 The result is in cfun->machine.
2065 DO NOT call nds32_compute_stack_frame() here
2066 because it may corrupt the essential information. */
2068 fprintf (file, "\t! BEGIN PROLOGUE\n");
2069 fprintf (file, "\t! fp needed: %d\n", frame_pointer_needed);
2070 fprintf (file, "\t! pretend_args: %d\n", cfun->machine->va_args_size);
2071 fprintf (file, "\t! local_size: %d\n", cfun->machine->local_size);
2072 fprintf (file, "\t! out_args_size: %d\n", cfun->machine->out_args_size);
2074 /* Use df_regs_ever_live_p() to detect if the register
2075 is ever used in the current function. */
2076 fprintf (file, "\t! registers ever_live: ");
2077 for (r = 0; r < 65; r++)
2079 if (df_regs_ever_live_p (r))
2080 fprintf (file, "%s, ", reg_names[r]);
2082 fputc ('\n', file);
2084 /* Display the attributes of this function. */
2085 fprintf (file, "\t! function attributes: ");
2086 /* Get the attributes tree list.
2087 Note that GCC builds attributes list with reverse order. */
2088 attrs = DECL_ATTRIBUTES (current_function_decl);
2090 /* If there is no any attribute, print out "None". */
2091 if (!attrs)
2092 fprintf (file, "None");
2094 /* If there are some attributes, try if we need to
2095 construct isr vector information. */
2096 func_name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2097 nds32_construct_isr_vectors_information (attrs, func_name);
2099 /* Display all attributes of this function. */
2100 while (attrs)
2102 name = TREE_PURPOSE (attrs);
2103 fprintf (file, "%s ", IDENTIFIER_POINTER (name));
2105 /* Pick up the next attribute. */
2106 attrs = TREE_CHAIN (attrs);
2108 fputc ('\n', file);
2111 /* After rtl prologue has been expanded, this function is used. */
2112 static void
2113 nds32_asm_function_end_prologue (FILE *file)
2115 fprintf (file, "\t! END PROLOGUE\n");
2117 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
2118 we can generate special directive: ".omit_fp_begin"
2119 to guide linker doing fp-as-gp optimization.
2120 However, for a naked function, which means
2121 it should not have prologue/epilogue,
2122 using fp-as-gp still requires saving $fp by push/pop behavior and
2123 there is no benefit to use fp-as-gp on such small function.
2124 So we need to make sure this function is NOT naked as well. */
2125 if (!frame_pointer_needed
2126 && !cfun->machine->naked_p
2127 && cfun->machine->fp_as_gp_p)
2129 fprintf (file, "\t! ----------------------------------------\n");
2130 fprintf (file, "\t! Guide linker to do "
2131 "link time optimization: fp-as-gp\n");
2132 fprintf (file, "\t! We add one more instruction to "
2133 "initialize $fp near to $gp location.\n");
2134 fprintf (file, "\t! If linker fails to use fp-as-gp transformation,\n");
2135 fprintf (file, "\t! this extra instruction should be "
2136 "eliminated at link stage.\n");
2137 fprintf (file, "\t.omit_fp_begin\n");
2138 fprintf (file, "\tla\t$fp,_FP_BASE_\n");
2139 fprintf (file, "\t! ----------------------------------------\n");
2143 /* Before rtl epilogue has been expanded, this function is used. */
2144 static void
2145 nds32_asm_function_begin_epilogue (FILE *file)
2147 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
2148 we can generate special directive: ".omit_fp_end"
2149 to claim fp-as-gp optimization range.
2150 However, for a naked function,
2151 which means it should not have prologue/epilogue,
2152 using fp-as-gp still requires saving $fp by push/pop behavior and
2153 there is no benefit to use fp-as-gp on such small function.
2154 So we need to make sure this function is NOT naked as well. */
2155 if (!frame_pointer_needed
2156 && !cfun->machine->naked_p
2157 && cfun->machine->fp_as_gp_p)
2159 fprintf (file, "\t! ----------------------------------------\n");
2160 fprintf (file, "\t! Claim the range of fp-as-gp "
2161 "link time optimization\n");
2162 fprintf (file, "\t.omit_fp_end\n");
2163 fprintf (file, "\t! ----------------------------------------\n");
2166 fprintf (file, "\t! BEGIN EPILOGUE\n");
2169 /* The content produced from this function
2170 will be placed after epilogue body. */
2171 static void
2172 nds32_asm_function_epilogue (FILE *file)
2174 fprintf (file, "\t! END EPILOGUE\n");
2177 static void
2178 nds32_asm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
2179 HOST_WIDE_INT delta,
2180 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2181 tree function)
2183 int this_regno;
2185 /* Make sure unwind info is emitted for the thunk if needed. */
2186 final_start_function (emit_barrier (), file, 1);
2188 this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
2190 : 0);
2192 if (delta != 0)
2194 if (satisfies_constraint_Is15 (GEN_INT (delta)))
2196 fprintf (file, "\taddi\t$r%d, $r%d, " HOST_WIDE_INT_PRINT_DEC "\n",
2197 this_regno, this_regno, delta);
2199 else if (satisfies_constraint_Is20 (GEN_INT (delta)))
2201 fprintf (file, "\tmovi\t$ta, " HOST_WIDE_INT_PRINT_DEC "\n", delta);
2202 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
2204 else
2206 fprintf (file,
2207 "\tsethi\t$ta, hi20(" HOST_WIDE_INT_PRINT_DEC ")\n",
2208 delta);
2209 fprintf (file,
2210 "\tori\t$ta, $ta, lo12(" HOST_WIDE_INT_PRINT_DEC ")\n",
2211 delta);
2212 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
2216 fprintf (file, "\tb\t");
2217 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2218 fprintf (file, "\n");
2220 final_end_function ();
2223 /* -- Permitting tail calls. */
2225 /* Return true if it is ok to do sibling call optimization. */
2226 static bool
2227 nds32_function_ok_for_sibcall (tree decl,
2228 tree exp ATTRIBUTE_UNUSED)
2230 /* The DECL is NULL if it is an indirect call. */
2232 /* 1. Do not apply sibling call if -mv3push is enabled,
2233 because pop25 instruction also represents return behavior.
2234 2. If this function is a variadic function, do not apply sibling call
2235 because the stack layout may be a mess.
2236 3. We don't want to apply sibling call optimization for indirect
2237 sibcall because the pop behavior in epilogue may pollute the
2238 content of caller-saved regsiter when the register is used for
2239 indirect sibcall. */
2240 return (!TARGET_V3PUSH
2241 && (cfun->machine->va_args_size == 0)
2242 && decl);
2245 /* Determine whether we need to enable warning for function return check. */
2246 static bool
2247 nds32_warn_func_return (tree decl)
2249 /* Naked functions are implemented entirely in assembly, including the
2250 return sequence, so suppress warnings about this. */
2251 return !nds32_naked_function_p (decl);
2255 /* Implementing the Varargs Macros. */
2257 static void
2258 nds32_setup_incoming_varargs (cumulative_args_t ca,
2259 machine_mode mode,
2260 tree type,
2261 int *pretend_args_size,
2262 int second_time ATTRIBUTE_UNUSED)
2264 unsigned int total_args_regs;
2265 unsigned int num_of_used_regs;
2266 unsigned int remaining_reg_count;
2267 CUMULATIVE_ARGS *cum;
2269 /* If we are under hard float abi, we do not need to set *pretend_args_size.
2270 So that all nameless arguments are pushed by caller and all situation
2271 can be handled by GCC itself. */
2272 if (TARGET_HARD_FLOAT)
2273 return;
2275 /* We are using NDS32_MAX_GPR_REGS_FOR_ARGS registers,
2276 counting from NDS32_GPR_ARG_FIRST_REGNUM, for saving incoming arguments.
2277 However, for nameless(anonymous) arguments, we should push them on the
2278 stack so that all the nameless arguments appear to have been passed
2279 consecutively in the memory for accessing. Hence, we need to check and
2280 exclude the registers that are used for named arguments. */
2282 cum = get_cumulative_args (ca);
2284 /* The MODE and TYPE describe the last argument.
2285 We need those information to determine the remaining registers
2286 for varargs. */
2287 total_args_regs
2288 = NDS32_MAX_GPR_REGS_FOR_ARGS + NDS32_GPR_ARG_FIRST_REGNUM;
2289 num_of_used_regs
2290 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
2291 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
2293 remaining_reg_count = total_args_regs - num_of_used_regs;
2294 *pretend_args_size = remaining_reg_count * UNITS_PER_WORD;
2296 return;
2299 static bool
2300 nds32_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
2302 /* If this hook returns true, the named argument of FUNCTION_ARG is always
2303 true for named arguments, and false for unnamed arguments. */
2304 return true;
2308 /* Trampolines for Nested Functions. */
2310 static void
2311 nds32_asm_trampoline_template (FILE *f)
2313 if (TARGET_REDUCED_REGS)
2315 /* Trampoline is not supported on reduced-set registers yet. */
2316 sorry ("a nested function is not supported for reduced registers");
2318 else
2320 asm_fprintf (f, "\t! Trampoline code template\n");
2321 asm_fprintf (f, "\t! This code fragment will be copied "
2322 "into stack on demand\n");
2324 asm_fprintf (f, "\tmfusr\t$r16,$pc\n");
2325 asm_fprintf (f, "\tlwi\t$r15,[$r16 + 20] "
2326 "! load nested function address\n");
2327 asm_fprintf (f, "\tlwi\t$r16,[$r16 + 16] "
2328 "! load chain_value\n");
2329 asm_fprintf (f, "\tjr\t$r15\n");
2332 /* Preserve space ($pc + 16) for saving chain_value,
2333 nds32_trampoline_init will fill the value in this slot. */
2334 asm_fprintf (f, "\t! space for saving chain_value\n");
2335 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2337 /* Preserve space ($pc + 20) for saving nested function address,
2338 nds32_trampoline_init will fill the value in this slot. */
2339 asm_fprintf (f, "\t! space for saving nested function address\n");
2340 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2343 /* Emit RTL insns to initialize the variable parts of a trampoline. */
2344 static void
2345 nds32_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2347 int i;
2349 /* Nested function address. */
2350 rtx fnaddr;
2351 /* The memory rtx that is going to
2352 be filled with chain_value. */
2353 rtx chain_value_mem;
2354 /* The memory rtx that is going to
2355 be filled with nested function address. */
2356 rtx nested_func_mem;
2358 /* Start address of trampoline code in stack, for doing cache sync. */
2359 rtx sync_cache_addr;
2360 /* Temporary register for sync instruction. */
2361 rtx tmp_reg;
2362 /* Instruction-cache sync instruction,
2363 requesting an argument as starting address. */
2364 rtx isync_insn;
2365 /* For convenience reason of doing comparison. */
2366 int tramp_align_in_bytes;
2368 /* Trampoline is not supported on reduced-set registers yet. */
2369 if (TARGET_REDUCED_REGS)
2370 sorry ("a nested function is not supported for reduced registers");
2372 /* STEP 1: Copy trampoline code template into stack,
2373 fill up essential data into stack. */
2375 /* Extract nested function address rtx. */
2376 fnaddr = XEXP (DECL_RTL (fndecl), 0);
2378 /* m_tramp is memory rtx that is going to be filled with trampoline code.
2379 We have nds32_asm_trampoline_template() to emit template pattern. */
2380 emit_block_move (m_tramp, assemble_trampoline_template (),
2381 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2383 /* After copying trampoline code into stack,
2384 fill chain_value into stack. */
2385 chain_value_mem = adjust_address (m_tramp, SImode, 16);
2386 emit_move_insn (chain_value_mem, chain_value);
2387 /* After copying trampoline code int stack,
2388 fill nested function address into stack. */
2389 nested_func_mem = adjust_address (m_tramp, SImode, 20);
2390 emit_move_insn (nested_func_mem, fnaddr);
2392 /* STEP 2: Sync instruction-cache. */
2394 /* We have successfully filled trampoline code into stack.
2395 However, in order to execute code in stack correctly,
2396 we must sync instruction cache. */
2397 sync_cache_addr = XEXP (m_tramp, 0);
2398 tmp_reg = gen_reg_rtx (SImode);
2399 isync_insn = gen_unspec_volatile_isync (tmp_reg);
2401 /* Because nds32_cache_block_size is in bytes,
2402 we get trampoline alignment in bytes for convenient comparison. */
2403 tramp_align_in_bytes = TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT;
2405 if (tramp_align_in_bytes >= nds32_cache_block_size
2406 && (tramp_align_in_bytes % nds32_cache_block_size) == 0)
2408 /* Under this condition, the starting address of trampoline
2409 must be aligned to the starting address of each cache block
2410 and we do not have to worry about cross-boundary issue. */
2411 for (i = 0;
2412 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2413 / nds32_cache_block_size;
2414 i++)
2416 emit_move_insn (tmp_reg,
2417 plus_constant (Pmode, sync_cache_addr,
2418 nds32_cache_block_size * i));
2419 emit_insn (isync_insn);
2422 else if (TRAMPOLINE_SIZE > nds32_cache_block_size)
2424 /* The starting address of trampoline code
2425 may not be aligned to the cache block,
2426 so the trampoline code may be across two cache block.
2427 We need to sync the last element, which is 4-byte size,
2428 of trampoline template. */
2429 for (i = 0;
2430 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2431 / nds32_cache_block_size;
2432 i++)
2434 emit_move_insn (tmp_reg,
2435 plus_constant (Pmode, sync_cache_addr,
2436 nds32_cache_block_size * i));
2437 emit_insn (isync_insn);
2440 /* The last element of trampoline template is 4-byte size. */
2441 emit_move_insn (tmp_reg,
2442 plus_constant (Pmode, sync_cache_addr,
2443 TRAMPOLINE_SIZE - 4));
2444 emit_insn (isync_insn);
2446 else
2448 /* This is the simplest case.
2449 Because TRAMPOLINE_SIZE is less than or
2450 equal to nds32_cache_block_size,
2451 we can just sync start address and
2452 the last element of trampoline code. */
2454 /* Sync starting address of tampoline code. */
2455 emit_move_insn (tmp_reg, sync_cache_addr);
2456 emit_insn (isync_insn);
2457 /* Sync the last element, which is 4-byte size,
2458 of trampoline template. */
2459 emit_move_insn (tmp_reg,
2460 plus_constant (Pmode, sync_cache_addr,
2461 TRAMPOLINE_SIZE - 4));
2462 emit_insn (isync_insn);
2465 /* Set instruction serialization barrier
2466 to guarantee the correct operations. */
2467 emit_insn (gen_unspec_volatile_isb ());
2471 /* Addressing Modes. */
2473 static bool
2474 nds32_legitimate_address_p (machine_mode mode, rtx x, bool strict)
2476 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
2478 /* When using floating-point instructions,
2479 we don't allow 'addr' to be [symbol_ref], [CONST] pattern. */
2480 if ((mode == DFmode || mode == SFmode)
2481 && (GET_CODE (x) == SYMBOL_REF
2482 || GET_CODE(x) == CONST))
2483 return false;
2485 /* Allow [post_modify] addressing mode, when using FPU instructions. */
2486 if (GET_CODE (x) == POST_MODIFY
2487 && mode == DFmode)
2489 if (GET_CODE (XEXP (x, 0)) == REG
2490 && GET_CODE (XEXP (x, 1)) == PLUS)
2492 rtx plus_op = XEXP (x, 1);
2493 rtx op0 = XEXP (plus_op, 0);
2494 rtx op1 = XEXP (plus_op, 1);
2496 if (nds32_address_register_rtx_p (op0, strict)
2497 && CONST_INT_P (op1))
2499 if (satisfies_constraint_Is14 (op1))
2501 /* If it is not under strictly aligned situation,
2502 we can return true without checking alignment. */
2503 if (!cfun->machine->strict_aligned_p)
2504 return true;
2505 /* Make sure address is word alignment.
2506 Currently we do not have 64-bit load/store yet,
2507 so we will use two 32-bit load/store instructions to do
2508 memory access and they are single word alignment. */
2509 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (op1)))
2510 return true;
2517 /* For (mem:DI addr) or (mem:DF addr) case,
2518 we only allow 'addr' to be [reg], [symbol_ref],
2519 [const], or [reg + const_int] pattern. */
2520 if (mode == DImode || mode == DFmode)
2522 /* Allow [Reg + const_int] addressing mode. */
2523 if (GET_CODE (x) == PLUS)
2525 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2526 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict)
2527 && CONST_INT_P (XEXP (x, 1)))
2528 return true;
2529 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2530 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict)
2531 && CONST_INT_P (XEXP (x, 0)))
2532 return true;
2535 /* Allow [post_inc] and [post_dec] addressing mode. */
2536 if (GET_CODE (x) == POST_INC || GET_CODE (x) == POST_DEC)
2538 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2539 return true;
2542 /* Now check [reg], [symbol_ref], and [const]. */
2543 if (GET_CODE (x) != REG
2544 && GET_CODE (x) != SYMBOL_REF
2545 && GET_CODE (x) != CONST)
2546 return false;
2549 /* Check if 'x' is a valid address. */
2550 switch (GET_CODE (x))
2552 case REG:
2553 /* (mem (reg A)) => [Ra] */
2554 return nds32_address_register_rtx_p (x, strict);
2556 case SYMBOL_REF:
2557 /* (mem (symbol_ref A)) => [symbol_ref] */
2558 /* If -mcmodel=large, the 'symbol_ref' is not a valid address
2559 during or after LRA/reload phase. */
2560 if (TARGET_CMODEL_LARGE
2561 && (reload_completed
2562 || reload_in_progress
2563 || lra_in_progress))
2564 return false;
2565 /* If -mcmodel=medium and the symbol references to rodata section,
2566 the 'symbol_ref' is not a valid address during or after
2567 LRA/reload phase. */
2568 if (TARGET_CMODEL_MEDIUM
2569 && NDS32_SYMBOL_REF_RODATA_P (x)
2570 && (reload_completed
2571 || reload_in_progress
2572 || lra_in_progress))
2573 return false;
2575 return true;
2577 case CONST:
2578 /* (mem (const (...)))
2579 => [ + const_addr ], where const_addr = symbol_ref + const_int */
2580 if (GET_CODE (XEXP (x, 0)) == PLUS)
2582 rtx plus_op = XEXP (x, 0);
2584 rtx op0 = XEXP (plus_op, 0);
2585 rtx op1 = XEXP (plus_op, 1);
2587 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
2589 /* Now we see the [ + const_addr ] pattern, but we need
2590 some further checking. */
2591 /* If -mcmodel=large, the 'const_addr' is not a valid address
2592 during or after LRA/reload phase. */
2593 if (TARGET_CMODEL_LARGE
2594 && (reload_completed
2595 || reload_in_progress
2596 || lra_in_progress))
2597 return false;
2598 /* If -mcmodel=medium and the symbol references to rodata section,
2599 the 'const_addr' is not a valid address during or after
2600 LRA/reload phase. */
2601 if (TARGET_CMODEL_MEDIUM
2602 && NDS32_SYMBOL_REF_RODATA_P (op0)
2603 && (reload_completed
2604 || reload_in_progress
2605 || lra_in_progress))
2606 return false;
2608 /* At this point we can make sure 'const_addr' is a
2609 valid address. */
2610 return true;
2614 return false;
2616 case POST_MODIFY:
2617 /* (mem (post_modify (reg) (plus (reg) (reg))))
2618 => [Ra], Rb */
2619 /* (mem (post_modify (reg) (plus (reg) (const_int))))
2620 => [Ra], const_int */
2621 if (GET_CODE (XEXP (x, 0)) == REG
2622 && GET_CODE (XEXP (x, 1)) == PLUS)
2624 rtx plus_op = XEXP (x, 1);
2626 rtx op0 = XEXP (plus_op, 0);
2627 rtx op1 = XEXP (plus_op, 1);
2629 if (nds32_address_register_rtx_p (op0, strict)
2630 && nds32_legitimate_index_p (mode, op1, strict))
2631 return true;
2632 else
2633 return false;
2636 return false;
2638 case POST_INC:
2639 case POST_DEC:
2640 /* (mem (post_inc reg)) => [Ra], 1/2/4 */
2641 /* (mem (post_dec reg)) => [Ra], -1/-2/-4 */
2642 /* The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
2643 We only need to deal with register Ra. */
2644 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2645 return true;
2646 else
2647 return false;
2649 case PLUS:
2650 /* (mem (plus reg const_int))
2651 => [Ra + imm] */
2652 /* (mem (plus reg reg))
2653 => [Ra + Rb] */
2654 /* (mem (plus (mult reg const_int) reg))
2655 => [Ra + Rb << sv] */
2656 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2657 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict))
2658 return true;
2659 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2660 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict))
2661 return true;
2662 else
2663 return false;
2665 case LO_SUM:
2666 /* (mem (lo_sum (reg) (symbol_ref))) */
2667 /* (mem (lo_sum (reg) (const))) */
2668 gcc_assert (REG_P (XEXP (x, 0)));
2669 if (GET_CODE (XEXP (x, 1)) == SYMBOL_REF
2670 || GET_CODE (XEXP (x, 1)) == CONST)
2671 return nds32_legitimate_address_p (mode, XEXP (x, 1), strict);
2672 else
2673 return false;
2675 default:
2676 return false;
2681 /* Condition Code Status. */
2683 /* -- Representation of condition codes using registers. */
2685 static void
2686 nds32_canonicalize_comparison (int *code,
2687 rtx *op0 ATTRIBUTE_UNUSED,
2688 rtx *op1,
2689 bool op0_preserve_value ATTRIBUTE_UNUSED)
2691 /* When the instruction combination pass tries to combine a comparison insn
2692 with its previous insns, it also transforms the operator in order to
2693 minimize its constant field. For example, it tries to transform a
2694 comparison insn from
2695 (set (reg:SI 54)
2696 (ltu:SI (reg:SI 52)
2697 (const_int 10 [0xa])))
2699 (set (reg:SI 54)
2700 (leu:SI (reg:SI 52)
2701 (const_int 9 [0x9])))
2703 However, the nds32 target only provides instructions supporting the LTU
2704 operation directly, and the implementation of the pattern "cbranchsi4"
2705 only expands the LTU form. In order to handle the non-LTU operations
2706 generated from passes other than the RTL expansion pass, we have to
2707 implement this hook to revert those changes. Since we only expand the LTU
2708 operator in the RTL expansion pass, we might only need to handle the LEU
2709 case, unless we find other optimization passes perform more aggressive
2710 transformations. */
2712 if (*code == LEU && CONST_INT_P (*op1))
2714 *op1 = gen_int_mode (INTVAL (*op1) + 1, SImode);
2715 *code = LTU;
2720 /* Describing Relative Costs of Operations. */
2722 static int
2723 nds32_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2724 reg_class_t from,
2725 reg_class_t to)
2727 if ((from == FP_REGS && to != FP_REGS)
2728 || (from != FP_REGS && to == FP_REGS))
2729 return 9;
2730 else if (from == HIGH_REGS || to == HIGH_REGS)
2731 return optimize_size ? 6 : 2;
2732 else
2733 return 2;
2736 static int
2737 nds32_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2738 reg_class_t rclass ATTRIBUTE_UNUSED,
2739 bool in ATTRIBUTE_UNUSED)
2741 return 8;
2744 /* This target hook describes the relative costs of RTL expressions.
2745 Return 'true' when all subexpressions of x have been processed.
2746 Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
2747 Refer to gcc/rtlanal.c for more information. */
2748 static bool
2749 nds32_rtx_costs (rtx x,
2750 machine_mode mode,
2751 int outer_code,
2752 int opno,
2753 int *total,
2754 bool speed)
2756 return nds32_rtx_costs_impl (x, mode, outer_code, opno, total, speed);
2759 static int
2760 nds32_address_cost (rtx address,
2761 machine_mode mode,
2762 addr_space_t as,
2763 bool speed)
2765 return nds32_address_cost_impl (address, mode, as, speed);
2769 /* Dividing the Output into Sections (Texts, Data, . . . ). */
2771 /* If references to a symbol or a constant must be treated differently
2772 depending on something about the variable or function named by the symbol
2773 (such as what section it is in), we use this hook to store flags
2774 in symbol_ref rtx. */
2775 static void
2776 nds32_encode_section_info (tree decl, rtx rtl, int new_decl_p)
2778 default_encode_section_info (decl, rtl, new_decl_p);
2780 /* For the memory rtx, if it references to rodata section, we can store
2781 NDS32_SYMBOL_FLAG_RODATA flag into symbol_ref rtx so that the
2782 nds32_legitimate_address_p() can determine how to treat such symbol_ref
2783 based on -mcmodel=X and this information. */
2784 if (MEM_P (rtl) && MEM_READONLY_P (rtl))
2786 rtx addr = XEXP (rtl, 0);
2788 if (GET_CODE (addr) == SYMBOL_REF)
2790 /* For (mem (symbol_ref X)) case. */
2791 SYMBOL_REF_FLAGS (addr) |= NDS32_SYMBOL_FLAG_RODATA;
2793 else if (GET_CODE (addr) == CONST
2794 && GET_CODE (XEXP (addr, 0)) == PLUS)
2796 /* For (mem (const (plus (symbol_ref X) (const_int N)))) case. */
2797 rtx plus_op = XEXP (addr, 0);
2798 rtx op0 = XEXP (plus_op, 0);
2799 rtx op1 = XEXP (plus_op, 1);
2801 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
2802 SYMBOL_REF_FLAGS (op0) |= NDS32_SYMBOL_FLAG_RODATA;
2808 /* Defining the Output Assembler Language. */
2810 /* -- The Overall Framework of an Assembler File. */
2812 static void
2813 nds32_asm_file_start (void)
2815 default_file_start ();
2817 /* Tell assembler which ABI we are using. */
2818 fprintf (asm_out_file, "\t! ABI version\n");
2819 if (TARGET_HARD_FLOAT)
2820 fprintf (asm_out_file, "\t.abi_2fp_plus\n");
2821 else
2822 fprintf (asm_out_file, "\t.abi_2\n");
2824 /* Tell assembler that this asm code is generated by compiler. */
2825 fprintf (asm_out_file, "\t! This asm file is generated by compiler\n");
2826 fprintf (asm_out_file, "\t.flag\tverbatim\n");
2827 /* Give assembler the size of each vector for interrupt handler. */
2828 fprintf (asm_out_file, "\t! This vector size directive is required "
2829 "for checking inconsistency on interrupt handler\n");
2830 fprintf (asm_out_file, "\t.vec_size\t%d\n", nds32_isr_vector_size);
2832 fprintf (asm_out_file, "\t! ------------------------------------\n");
2834 if (TARGET_ISA_V2)
2835 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V2");
2836 if (TARGET_ISA_V3)
2837 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3");
2838 if (TARGET_ISA_V3M)
2839 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3M");
2841 if (TARGET_CMODEL_SMALL)
2842 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "SMALL");
2843 if (TARGET_CMODEL_MEDIUM)
2844 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "MEDIUM");
2845 if (TARGET_CMODEL_LARGE)
2846 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "LARGE");
2848 fprintf (asm_out_file, "\t! Endian setting\t: %s\n",
2849 ((TARGET_BIG_ENDIAN) ? "big-endian"
2850 : "little-endian"));
2851 fprintf (asm_out_file, "\t! Use SP floating-point instruction\t: %s\n",
2852 ((TARGET_FPU_SINGLE) ? "Yes"
2853 : "No"));
2854 fprintf (asm_out_file, "\t! Use DP floating-point instruction\t: %s\n",
2855 ((TARGET_FPU_DOUBLE) ? "Yes"
2856 : "No"));
2857 fprintf (asm_out_file, "\t! ABI version\t\t: %s\n",
2858 ((TARGET_HARD_FLOAT) ? "ABI2FP+"
2859 : "ABI2"));
2861 fprintf (asm_out_file, "\t! ------------------------------------\n");
2863 fprintf (asm_out_file, "\t! Use conditional move\t\t: %s\n",
2864 ((TARGET_CMOV) ? "Yes"
2865 : "No"));
2866 fprintf (asm_out_file, "\t! Use performance extension\t: %s\n",
2867 ((TARGET_EXT_PERF) ? "Yes"
2868 : "No"));
2869 fprintf (asm_out_file, "\t! Use performance extension 2\t: %s\n",
2870 ((TARGET_EXT_PERF2) ? "Yes"
2871 : "No"));
2872 fprintf (asm_out_file, "\t! Use string extension\t\t: %s\n",
2873 ((TARGET_EXT_STRING) ? "Yes"
2874 : "No"));
2876 fprintf (asm_out_file, "\t! ------------------------------------\n");
2878 fprintf (asm_out_file, "\t! V3PUSH instructions\t: %s\n",
2879 ((TARGET_V3PUSH) ? "Yes"
2880 : "No"));
2881 fprintf (asm_out_file, "\t! 16-bit instructions\t: %s\n",
2882 ((TARGET_16_BIT) ? "Yes"
2883 : "No"));
2884 fprintf (asm_out_file, "\t! Reduced registers set\t: %s\n",
2885 ((TARGET_REDUCED_REGS) ? "Yes"
2886 : "No"));
2888 fprintf (asm_out_file, "\t! Support unaligned access\t\t: %s\n",
2889 (flag_unaligned_access ? "Yes"
2890 : "No"));
2892 fprintf (asm_out_file, "\t! ------------------------------------\n");
2894 if (optimize_size)
2895 fprintf (asm_out_file, "\t! Optimization level\t: -Os\n");
2896 else
2897 fprintf (asm_out_file, "\t! Optimization level\t: -O%d\n", optimize);
2899 fprintf (asm_out_file, "\t! ------------------------------------\n");
2901 fprintf (asm_out_file, "\t! Cache block size\t: %d\n",
2902 nds32_cache_block_size);
2904 fprintf (asm_out_file, "\t! ------------------------------------\n");
2906 nds32_asm_file_start_for_isr ();
2909 static void
2910 nds32_asm_file_end (void)
2912 nds32_asm_file_end_for_isr ();
2914 fprintf (asm_out_file, "\t! ------------------------------------\n");
2917 /* -- Output and Generation of Labels. */
2919 static void
2920 nds32_asm_globalize_label (FILE *stream, const char *name)
2922 fputs ("\t.global\t", stream);
2923 assemble_name (stream, name);
2924 fputs ("\n", stream);
2927 /* -- Output of Assembler Instructions. */
2929 static void
2930 nds32_print_operand (FILE *stream, rtx x, int code)
2932 HOST_WIDE_INT one_position;
2933 HOST_WIDE_INT zero_position;
2934 bool pick_lsb_p = false;
2935 bool pick_msb_p = false;
2936 int regno;
2938 int op_value;
2940 switch (code)
2942 case 0 :
2943 /* Do nothing special. */
2944 break;
2946 case 'b':
2947 /* Use exact_log2() to search the 0-bit position. */
2948 gcc_assert (CONST_INT_P (x));
2949 zero_position = exact_log2 (~UINTVAL (x) & GET_MODE_MASK (SImode));
2950 gcc_assert (zero_position != -1);
2951 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, zero_position);
2953 /* No need to handle following process, so return immediately. */
2954 return;
2956 case 'e':
2957 gcc_assert (MEM_P (x)
2958 && GET_CODE (XEXP (x, 0)) == PLUS
2959 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
2960 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (XEXP (x, 0), 1)));
2962 /* No need to handle following process, so return immediately. */
2963 return;
2964 case 'B':
2965 /* Use exact_log2() to search the 1-bit position. */
2966 gcc_assert (CONST_INT_P (x));
2967 one_position = exact_log2 (UINTVAL (x) & GET_MODE_MASK (SImode));
2968 gcc_assert (one_position != -1);
2969 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, one_position);
2971 /* No need to handle following process, so return immediately. */
2972 return;
2974 case 'L':
2975 /* X is supposed to be REG rtx. */
2976 gcc_assert (REG_P (x));
2977 /* Claim that we are going to pick LSB part of X. */
2978 pick_lsb_p = true;
2979 break;
2981 case 'H':
2982 /* X is supposed to be REG rtx. */
2983 gcc_assert (REG_P (x));
2984 /* Claim that we are going to pick MSB part of X. */
2985 pick_msb_p = true;
2986 break;
2988 case 'V':
2989 /* 'x' is supposed to be CONST_INT, get the value. */
2990 gcc_assert (CONST_INT_P (x));
2991 op_value = INTVAL (x);
2993 /* According to the Andes architecture,
2994 the system/user register index range is 0 ~ 1023.
2995 In order to avoid conflict between user-specified-integer value
2996 and enum-specified-register value,
2997 the 'enum nds32_intrinsic_registers' value
2998 in nds32_intrinsic.h starts from 1024. */
2999 if (op_value < 1024 && op_value >= 0)
3001 /* If user gives integer value directly (0~1023),
3002 we just print out the value. */
3003 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, op_value);
3005 else if (op_value < 0
3006 || op_value >= ((int) ARRAY_SIZE (nds32_intrinsic_register_names)
3007 + 1024))
3009 /* The enum index value for array size is out of range. */
3010 error ("intrinsic register index is out of range");
3012 else
3014 /* If user applies normal way with __NDS32_REG_XXX__ enum data,
3015 we can print out register name. Remember to substract 1024. */
3016 fprintf (stream, "%s",
3017 nds32_intrinsic_register_names[op_value - 1024]);
3020 /* No need to handle following process, so return immediately. */
3021 return;
3023 case 'R': /* cctl valck */
3024 /* Note the cctl divide to 5 group and share the same name table. */
3025 if (op_value < 0 || op_value > 4)
3026 error ("CCTL intrinsic function subtype out of range!");
3027 fprintf (stream, "%s", nds32_cctl_names[op_value]);
3028 return;
3030 case 'T': /* cctl idxwbinv */
3031 /* Note the cctl divide to 5 group and share the same name table. */
3032 if (op_value < 0 || op_value > 4)
3033 error ("CCTL intrinsic function subtype out of range!");
3034 fprintf (stream, "%s", nds32_cctl_names[op_value + 4]);
3035 return;
3037 case 'U': /* cctl vawbinv */
3038 /* Note the cctl divide to 5 group and share the same name table. */
3039 if (op_value < 0 || op_value > 4)
3040 error ("CCTL intrinsic function subtype out of range!");
3041 fprintf (stream, "%s", nds32_cctl_names[op_value + 8]);
3042 return;
3044 case 'X': /* cctl idxread */
3045 /* Note the cctl divide to 5 group and share the same name table. */
3046 if (op_value < 0 || op_value > 4)
3047 error ("CCTL intrinsic function subtype out of range!");
3048 fprintf (stream, "%s", nds32_cctl_names[op_value + 12]);
3049 return;
3051 case 'W': /* cctl idxwitre */
3052 /* Note the cctl divide to 5 group and share the same name table. */
3053 if (op_value < 0 || op_value > 4)
3054 error ("CCTL intrinsic function subtype out of range!");
3055 fprintf (stream, "%s", nds32_cctl_names[op_value + 16]);
3056 return;
3058 case 'Z': /* dpref */
3059 fprintf (stream, "%s", nds32_dpref_names[op_value]);
3060 return;
3062 default :
3063 /* Unknown flag. */
3064 output_operand_lossage ("invalid operand output code");
3065 break;
3068 switch (GET_CODE (x))
3070 case LABEL_REF:
3071 case SYMBOL_REF:
3072 output_addr_const (stream, x);
3073 break;
3075 case REG:
3076 /* Print a Double-precision register name. */
3077 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3078 && NDS32_IS_FPR_REGNUM (REGNO (x)))
3080 regno = REGNO (x);
3081 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno))
3083 output_operand_lossage ("invalid operand for code '%c'", code);
3084 break;
3086 fprintf (stream, "$fd%d", (regno - NDS32_FIRST_FPR_REGNUM) >> 1);
3087 break;
3090 /* Print LSB or MSB part of register pair if the
3091 constraint modifier 'L' or 'H' is specified. */
3092 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3093 && NDS32_IS_GPR_REGNUM (REGNO (x)))
3095 if ((pick_lsb_p && WORDS_BIG_ENDIAN)
3096 || (pick_msb_p && !WORDS_BIG_ENDIAN))
3098 /* If we would like to print out LSB register under big-endian,
3099 or print out MSB register under little-endian, we need to
3100 increase register number. */
3101 regno = REGNO (x);
3102 regno++;
3103 fputs (reg_names[regno], stream);
3104 break;
3108 /* Forbid using static chain register ($r16)
3109 on reduced-set registers configuration. */
3110 if (TARGET_REDUCED_REGS
3111 && REGNO (x) == STATIC_CHAIN_REGNUM)
3112 sorry ("a nested function is not supported for reduced registers");
3114 /* Normal cases, print out register name. */
3115 fputs (reg_names[REGNO (x)], stream);
3116 break;
3118 case MEM:
3119 output_address (GET_MODE (x), XEXP (x, 0));
3120 break;
3122 case HIGH:
3123 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE)
3125 const REAL_VALUE_TYPE *rv;
3126 long val;
3127 gcc_assert (GET_MODE (x) == SFmode);
3129 rv = CONST_DOUBLE_REAL_VALUE (XEXP (x, 0));
3130 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3132 fprintf (stream, "hi20(0x%lx)", val);
3134 else
3135 gcc_unreachable ();
3136 break;
3138 case CONST_DOUBLE:
3139 const REAL_VALUE_TYPE *rv;
3140 long val;
3141 gcc_assert (GET_MODE (x) == SFmode);
3143 rv = CONST_DOUBLE_REAL_VALUE (x);
3144 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3146 fprintf (stream, "0x%lx", val);
3147 break;
3149 case CODE_LABEL:
3150 case CONST_INT:
3151 case CONST:
3152 output_addr_const (stream, x);
3153 break;
3155 default:
3156 /* Generally, output_addr_const () is able to handle most cases.
3157 We want to see what CODE could appear,
3158 so we use gcc_unreachable() to stop it. */
3159 debug_rtx (x);
3160 gcc_unreachable ();
3161 break;
3165 static void
3166 nds32_print_operand_address (FILE *stream, machine_mode /*mode*/, rtx x)
3168 rtx op0, op1;
3170 switch (GET_CODE (x))
3172 case SYMBOL_REF:
3173 case CONST:
3174 /* [ + symbol_ref] */
3175 /* [ + const_addr], where const_addr = symbol_ref + const_int */
3176 fputs ("[ + ", stream);
3177 output_addr_const (stream, x);
3178 fputs ("]", stream);
3179 break;
3181 case REG:
3182 /* Forbid using static chain register ($r16)
3183 on reduced-set registers configuration. */
3184 if (TARGET_REDUCED_REGS
3185 && REGNO (x) == STATIC_CHAIN_REGNUM)
3186 sorry ("a nested function is not supported for reduced registers");
3188 /* [Ra] */
3189 fprintf (stream, "[%s]", reg_names[REGNO (x)]);
3190 break;
3192 case PLUS:
3193 op0 = XEXP (x, 0);
3194 op1 = XEXP (x, 1);
3196 /* Checking op0, forbid using static chain register ($r16)
3197 on reduced-set registers configuration. */
3198 if (TARGET_REDUCED_REGS
3199 && REG_P (op0)
3200 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3201 sorry ("a nested function is not supported for reduced registers");
3202 /* Checking op1, forbid using static chain register ($r16)
3203 on reduced-set registers configuration. */
3204 if (TARGET_REDUCED_REGS
3205 && REG_P (op1)
3206 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3207 sorry ("a nested function is not supported for reduced registers");
3209 if (REG_P (op0) && CONST_INT_P (op1))
3211 /* [Ra + imm] */
3212 fprintf (stream, "[%s + (" HOST_WIDE_INT_PRINT_DEC ")]",
3213 reg_names[REGNO (op0)], INTVAL (op1));
3215 else if (REG_P (op0) && REG_P (op1))
3217 /* [Ra + Rb] */
3218 fprintf (stream, "[%s + %s]",
3219 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3221 else if (GET_CODE (op0) == MULT && REG_P (op1))
3223 /* [Ra + Rb << sv]
3224 From observation, the pattern looks like:
3225 (plus:SI (mult:SI (reg:SI 58)
3226 (const_int 4 [0x4]))
3227 (reg/f:SI 57)) */
3228 int sv;
3230 /* We need to set sv to output shift value. */
3231 if (INTVAL (XEXP (op0, 1)) == 1)
3232 sv = 0;
3233 else if (INTVAL (XEXP (op0, 1)) == 2)
3234 sv = 1;
3235 else if (INTVAL (XEXP (op0, 1)) == 4)
3236 sv = 2;
3237 else if (INTVAL (XEXP (op0, 1)) == 8)
3238 sv = 3;
3239 else
3240 gcc_unreachable ();
3242 fprintf (stream, "[%s + %s << %d]",
3243 reg_names[REGNO (op1)],
3244 reg_names[REGNO (XEXP (op0, 0))],
3245 sv);
3247 else
3249 /* The control flow is not supposed to be here. */
3250 debug_rtx (x);
3251 gcc_unreachable ();
3254 break;
3256 case POST_MODIFY:
3257 /* (post_modify (regA) (plus (regA) (regB)))
3258 (post_modify (regA) (plus (regA) (const_int)))
3259 We would like to extract
3260 regA and regB (or const_int) from plus rtx. */
3261 op0 = XEXP (XEXP (x, 1), 0);
3262 op1 = XEXP (XEXP (x, 1), 1);
3264 /* Checking op0, forbid using static chain register ($r16)
3265 on reduced-set registers configuration. */
3266 if (TARGET_REDUCED_REGS
3267 && REG_P (op0)
3268 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3269 sorry ("a nested function is not supported for reduced registers");
3270 /* Checking op1, forbid using static chain register ($r16)
3271 on reduced-set registers configuration. */
3272 if (TARGET_REDUCED_REGS
3273 && REG_P (op1)
3274 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3275 sorry ("a nested function is not supported for reduced registers");
3277 if (REG_P (op0) && REG_P (op1))
3279 /* [Ra], Rb */
3280 fprintf (stream, "[%s], %s",
3281 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3283 else if (REG_P (op0) && CONST_INT_P (op1))
3285 /* [Ra], imm */
3286 fprintf (stream, "[%s], " HOST_WIDE_INT_PRINT_DEC,
3287 reg_names[REGNO (op0)], INTVAL (op1));
3289 else
3291 /* The control flow is not supposed to be here. */
3292 debug_rtx (x);
3293 gcc_unreachable ();
3296 break;
3298 case POST_INC:
3299 case POST_DEC:
3300 op0 = XEXP (x, 0);
3302 /* Checking op0, forbid using static chain register ($r16)
3303 on reduced-set registers configuration. */
3304 if (TARGET_REDUCED_REGS
3305 && REG_P (op0)
3306 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3307 sorry ("a nested function is not supported for reduced registers");
3309 if (REG_P (op0))
3311 /* "[Ra], 1/2/4" or "[Ra], -1/-2/-4"
3312 The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
3313 We only need to deal with register Ra. */
3314 fprintf (stream, "[%s]", reg_names[REGNO (op0)]);
3316 else
3318 /* The control flow is not supposed to be here. */
3319 debug_rtx (x);
3320 gcc_unreachable ();
3323 break;
3325 default :
3326 /* Generally, output_addr_const () is able to handle most cases.
3327 We want to see what CODE could appear,
3328 so we use gcc_unreachable() to stop it. */
3329 debug_rtx (x);
3330 gcc_unreachable ();
3331 break;
3335 /* -- Assembler Commands for Exception Regions. */
3337 static rtx
3338 nds32_dwarf_register_span (rtx reg)
3340 rtx dwarf_high, dwarf_low;
3341 rtx dwarf_single;
3342 machine_mode mode;
3343 int regno;
3345 mode = GET_MODE (reg);
3346 regno = REGNO (reg);
3348 /* We need to adjust dwarf register information for floating-point registers
3349 rather than using default register number mapping. */
3350 if (regno >= NDS32_FIRST_FPR_REGNUM
3351 && regno <= NDS32_LAST_FPR_REGNUM)
3353 if (mode == DFmode || mode == SCmode)
3355 /* By default, GCC maps increasing register numbers to increasing
3356 memory locations, but paired FPRs in NDS32 target are always
3357 big-endian, i.e.:
3359 fd0 : fs0 fs1
3360 (MSB) (LSB)
3362 We must return parallel rtx to represent such layout. */
3363 dwarf_high = gen_rtx_REG (word_mode, regno);
3364 dwarf_low = gen_rtx_REG (word_mode, regno + 1);
3365 return gen_rtx_PARALLEL (VOIDmode,
3366 gen_rtvec (2, dwarf_low, dwarf_high));
3368 else if (mode == DCmode)
3370 rtx dwarf_high_re = gen_rtx_REG (word_mode, regno);
3371 rtx dwarf_low_re = gen_rtx_REG (word_mode, regno + 1);
3372 rtx dwarf_high_im = gen_rtx_REG (word_mode, regno);
3373 rtx dwarf_low_im = gen_rtx_REG (word_mode, regno + 1);
3374 return gen_rtx_PARALLEL (VOIDmode,
3375 gen_rtvec (4, dwarf_low_re, dwarf_high_re,
3376 dwarf_high_im, dwarf_low_im));
3378 else if (mode == SFmode || mode == SImode)
3380 /* Create new dwarf information with adjusted register number. */
3381 dwarf_single = gen_rtx_REG (word_mode, regno);
3382 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, dwarf_single));
3384 else
3386 /* We should not be here. */
3387 gcc_unreachable ();
3391 return NULL_RTX;
3394 /* Map internal gcc register numbers to DWARF2 register numbers. */
3396 unsigned int
3397 nds32_dbx_register_number (unsigned int regno)
3399 /* The nds32 port in GDB maintains a mapping between dwarf register
3400 number and displayed register name. For backward compatibility to
3401 previous toolchain, currently our gdb still has four registers
3402 (d0.l, d0.h, d1.l, and d1.h) between GPR and FPR while compiler
3403 does not count those four registers in its register number table.
3404 So we have to add 4 on its register number and then create new
3405 dwarf information. Hopefully we can discard such workaround
3406 in the future. */
3407 if (NDS32_IS_FPR_REGNUM (regno))
3408 return regno + 4;
3410 return regno;
3414 /* Defining target-specific uses of __attribute__. */
3416 /* Add some checking after merging attributes. */
3417 static tree
3418 nds32_merge_decl_attributes (tree olddecl, tree newdecl)
3420 tree combined_attrs;
3422 /* Create combined attributes. */
3423 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
3424 DECL_ATTRIBUTES (newdecl));
3426 /* Since newdecl is acutally a duplicate of olddecl,
3427 we can take olddecl for some operations. */
3428 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3430 /* Check isr-specific attributes conflict. */
3431 nds32_check_isr_attrs_conflict (olddecl, combined_attrs);
3434 return combined_attrs;
3437 /* Add some checking when inserting attributes. */
3438 static void
3439 nds32_insert_attributes (tree decl, tree *attributes)
3441 /* For function declaration, we need to check isr-specific attributes:
3442 1. Call nds32_check_isr_attrs_conflict() to check any conflict.
3443 2. Check valid integer value for interrupt/exception.
3444 3. Check valid integer value for reset.
3445 4. Check valid function for nmi/warm. */
3446 if (TREE_CODE (decl) == FUNCTION_DECL)
3448 tree func_attrs;
3449 tree intr, excp, reset;
3451 /* Pick up function attributes. */
3452 func_attrs = *attributes;
3454 /* 1. Call nds32_check_isr_attrs_conflict() to check any conflict. */
3455 nds32_check_isr_attrs_conflict (decl, func_attrs);
3457 /* Now we are starting to check valid id value
3458 for interrupt/exception/reset.
3459 Note that we ONLY check its validity here.
3460 To construct isr vector information, it is still performed
3461 by nds32_construct_isr_vectors_information(). */
3462 intr = lookup_attribute ("interrupt", func_attrs);
3463 excp = lookup_attribute ("exception", func_attrs);
3464 reset = lookup_attribute ("reset", func_attrs);
3466 if (intr || excp)
3468 /* Deal with interrupt/exception. */
3469 tree id_list;
3470 unsigned int lower_bound, upper_bound;
3472 /* The way to handle interrupt or exception is the same,
3473 we just need to take care of actual vector number.
3474 For interrupt(0..63), the actual vector number is (9..72).
3475 For exception(1..8), the actual vector number is (1..8). */
3476 lower_bound = (intr) ? (0) : (1);
3477 upper_bound = (intr) ? (63) : (8);
3479 /* Prepare id list so that we can traverse id value. */
3480 id_list = (intr) ? (TREE_VALUE (intr)) : (TREE_VALUE (excp));
3482 /* 2. Check valid integer value for interrupt/exception. */
3483 while (id_list)
3485 tree id;
3487 /* Pick up each vector id value. */
3488 id = TREE_VALUE (id_list);
3489 /* Issue error if it is not a valid integer value. */
3490 if (TREE_CODE (id) != INTEGER_CST
3491 || wi::ltu_p (wi::to_wide (id), lower_bound)
3492 || wi::gtu_p (wi::to_wide (id), upper_bound))
3493 error ("invalid id value for interrupt/exception attribute");
3495 /* Advance to next id. */
3496 id_list = TREE_CHAIN (id_list);
3499 else if (reset)
3501 /* Deal with reset. */
3502 tree id_list;
3503 tree id;
3504 tree nmi, warm;
3505 unsigned int lower_bound;
3506 unsigned int upper_bound;
3508 /* Prepare id_list and identify id value so that
3509 we can check if total number of vectors is valid. */
3510 id_list = TREE_VALUE (reset);
3511 id = TREE_VALUE (id_list);
3513 /* The maximum numbers for user's interrupt is 64. */
3514 lower_bound = 0;
3515 upper_bound = 64;
3517 /* 3. Check valid integer value for reset. */
3518 if (TREE_CODE (id) != INTEGER_CST
3519 || wi::ltu_p (wi::to_wide (id), lower_bound)
3520 || wi::gtu_p (wi::to_wide (id), upper_bound))
3521 error ("invalid id value for reset attribute");
3523 /* 4. Check valid function for nmi/warm. */
3524 nmi = lookup_attribute ("nmi", func_attrs);
3525 warm = lookup_attribute ("warm", func_attrs);
3527 if (nmi != NULL_TREE)
3529 tree nmi_func_list;
3530 tree nmi_func;
3532 nmi_func_list = TREE_VALUE (nmi);
3533 nmi_func = TREE_VALUE (nmi_func_list);
3535 /* Issue error if it is not a valid nmi function. */
3536 if (TREE_CODE (nmi_func) != IDENTIFIER_NODE)
3537 error ("invalid nmi function for reset attribute");
3540 if (warm != NULL_TREE)
3542 tree warm_func_list;
3543 tree warm_func;
3545 warm_func_list = TREE_VALUE (warm);
3546 warm_func = TREE_VALUE (warm_func_list);
3548 /* Issue error if it is not a valid warm function. */
3549 if (TREE_CODE (warm_func) != IDENTIFIER_NODE)
3550 error ("invalid warm function for reset attribute");
3553 else
3555 /* No interrupt, exception, or reset attribute is set. */
3556 return;
3561 static bool
3562 nds32_option_pragma_parse (tree args ATTRIBUTE_UNUSED,
3563 tree pop_target ATTRIBUTE_UNUSED)
3565 /* Currently, we do not parse any pragma target by ourself,
3566 so just simply return false. */
3567 return false;
3570 static void
3571 nds32_option_override (void)
3573 /* After all the command options have been parsed,
3574 we shall deal with some flags for changing compiler settings. */
3576 /* At first, we check if we have to strictly
3577 set some flags based on ISA family. */
3578 if (TARGET_ISA_V2)
3580 /* Under V2 ISA, we need to strictly disable TARGET_V3PUSH. */
3581 target_flags &= ~MASK_V3PUSH;
3583 if (TARGET_ISA_V3)
3585 /* Under V3 ISA, currently nothing should be strictly set. */
3587 if (TARGET_ISA_V3M)
3589 /* Under V3M ISA, we need to strictly enable TARGET_REDUCED_REGS. */
3590 target_flags |= MASK_REDUCED_REGS;
3591 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF. */
3592 target_flags &= ~MASK_EXT_PERF;
3593 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF2. */
3594 target_flags &= ~MASK_EXT_PERF2;
3595 /* Under V3M ISA, we need to strictly disable TARGET_EXT_STRING. */
3596 target_flags &= ~MASK_EXT_STRING;
3599 /* See if we are using reduced-set registers:
3600 $r0~$r5, $r6~$r10, $r15, $r28, $r29, $r30, $r31
3601 If so, we must forbid using $r11~$r14, $r16~$r27. */
3602 if (TARGET_REDUCED_REGS)
3604 int r;
3606 /* Prevent register allocator from
3607 choosing it as doing register allocation. */
3608 for (r = 11; r <= 14; r++)
3609 fixed_regs[r] = call_used_regs[r] = 1;
3610 for (r = 16; r <= 27; r++)
3611 fixed_regs[r] = call_used_regs[r] = 1;
3614 if (!TARGET_16_BIT)
3616 /* Under no 16 bit ISA, we need to strictly disable TARGET_V3PUSH. */
3617 target_flags &= ~MASK_V3PUSH;
3620 if (TARGET_HARD_FLOAT && !(TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
3622 if (nds32_arch_option == ARCH_V3S || nds32_arch_option == ARCH_V3F)
3623 error ("Disable FPU ISA, "
3624 "the ABI option must be enable '-mfloat-abi=soft'");
3625 else
3626 error ("'-mabi=2fp+' option only support when FPU available, "
3627 "must be enable '-mext-fpu-sp' or '-mext-fpu-dp'");
3630 /* Currently, we don't support PIC code generation yet. */
3631 if (flag_pic)
3632 sorry ("position-independent code not supported");
3634 nds32_register_passes ();
3638 /* Miscellaneous Parameters. */
3640 static rtx_insn *
3641 nds32_md_asm_adjust (vec<rtx> &outputs ATTRIBUTE_UNUSED,
3642 vec<rtx> &inputs ATTRIBUTE_UNUSED,
3643 vec<const char *> &constraints ATTRIBUTE_UNUSED,
3644 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
3646 clobbers.safe_push (gen_rtx_REG (SImode, TA_REGNUM));
3647 SET_HARD_REG_BIT (clobbered_regs, TA_REGNUM);
3648 return NULL;
3651 static void
3652 nds32_init_builtins (void)
3654 nds32_init_builtins_impl ();
3657 static tree
3658 nds32_builtin_decl (unsigned code, bool initialize_p)
3660 /* Implement in nds32-intrinsic.c. */
3661 return nds32_builtin_decl_impl (code, initialize_p);
3664 static rtx
3665 nds32_expand_builtin (tree exp,
3666 rtx target,
3667 rtx subtarget,
3668 machine_mode mode,
3669 int ignore)
3671 return nds32_expand_builtin_impl (exp, target, subtarget, mode, ignore);
3675 /* ------------------------------------------------------------------------ */
3677 /* PART 4: Implemet extern function definitions,
3678 the prototype is in nds32-protos.h. */
3680 /* Run-time Target Specification. */
3682 void
3683 nds32_cpu_cpp_builtins(struct cpp_reader *pfile)
3685 #define builtin_define(TXT) cpp_define (pfile, TXT)
3686 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
3687 builtin_define ("__nds32__");
3688 builtin_define ("__NDS32__");
3690 if (TARGET_HARD_FLOAT)
3691 builtin_define ("__NDS32_ABI_2FP_PLUS__");
3692 else
3693 builtin_define ("__NDS32_ABI_2__");
3695 if (TARGET_ISA_V2)
3696 builtin_define ("__NDS32_ISA_V2__");
3697 if (TARGET_ISA_V3)
3698 builtin_define ("__NDS32_ISA_V3__");
3699 if (TARGET_ISA_V3M)
3700 builtin_define ("__NDS32_ISA_V3M__");
3702 if (TARGET_FPU_SINGLE)
3703 builtin_define ("__NDS32_EXT_FPU_SP__");
3704 if (TARGET_FPU_DOUBLE)
3705 builtin_define ("__NDS32_EXT_FPU_DP__");
3707 if (TARGET_EXT_FPU_FMA)
3708 builtin_define ("__NDS32_EXT_FPU_FMA__");
3709 if (NDS32_EXT_FPU_DOT_E)
3710 builtin_define ("__NDS32_EXT_FPU_DOT_E__");
3711 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
3713 switch (nds32_fp_regnum)
3715 case 0:
3716 case 4:
3717 builtin_define ("__NDS32_EXT_FPU_CONFIG_0__");
3718 break;
3719 case 1:
3720 case 5:
3721 builtin_define ("__NDS32_EXT_FPU_CONFIG_1__");
3722 break;
3723 case 2:
3724 case 6:
3725 builtin_define ("__NDS32_EXT_FPU_CONFIG_2__");
3726 break;
3727 case 3:
3728 case 7:
3729 builtin_define ("__NDS32_EXT_FPU_CONFIG_3__");
3730 break;
3731 default:
3732 abort ();
3736 if (TARGET_BIG_ENDIAN)
3737 builtin_define ("__NDS32_EB__");
3738 else
3739 builtin_define ("__NDS32_EL__");
3741 if (TARGET_REDUCED_REGS)
3742 builtin_define ("__NDS32_REDUCED_REGS__");
3743 if (TARGET_CMOV)
3744 builtin_define ("__NDS32_CMOV__");
3745 if (TARGET_EXT_PERF)
3746 builtin_define ("__NDS32_EXT_PERF__");
3747 if (TARGET_EXT_PERF2)
3748 builtin_define ("__NDS32_EXT_PERF2__");
3749 if (TARGET_EXT_STRING)
3750 builtin_define ("__NDS32_EXT_STRING__");
3751 if (TARGET_16_BIT)
3752 builtin_define ("__NDS32_16_BIT__");
3753 if (TARGET_GP_DIRECT)
3754 builtin_define ("__NDS32_GP_DIRECT__");
3755 if (TARGET_VH)
3756 builtin_define ("__NDS32_VH__");
3758 if (TARGET_BIG_ENDIAN)
3759 builtin_define ("__big_endian__");
3761 builtin_assert ("cpu=nds32");
3762 builtin_assert ("machine=nds32");
3764 if (TARGET_HARD_FLOAT)
3765 builtin_define ("__NDS32_ABI_2FP_PLUS");
3766 else
3767 builtin_define ("__NDS32_ABI_2");
3769 #undef builtin_define
3770 #undef builtin_assert
3774 /* Defining Data Structures for Per-function Information. */
3776 void
3777 nds32_init_expanders (void)
3779 /* Arrange to initialize and mark the machine per-function status. */
3780 init_machine_status = nds32_init_machine_status;
3784 /* Register Usage. */
3786 /* -- Order of Allocation of Registers. */
3788 void
3789 nds32_adjust_reg_alloc_order (void)
3791 const int nds32_reg_alloc_order[] = REG_ALLOC_ORDER;
3793 /* Copy the default register allocation order, which is designed
3794 to optimize for code size. */
3795 memcpy(reg_alloc_order, nds32_reg_alloc_order, sizeof (reg_alloc_order));
3797 /* Adjust few register allocation order when optimizing for speed. */
3798 if (!optimize_size)
3800 memcpy (reg_alloc_order, nds32_reg_alloc_order_for_speed,
3801 sizeof (nds32_reg_alloc_order_for_speed));
3805 /* -- How Values Fit in Registers. */
3807 static unsigned
3808 nds32_hard_regno_nregs (unsigned regno ATTRIBUTE_UNUSED,
3809 machine_mode mode)
3811 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
3814 /* Implement TARGET_HARD_REGNO_MODE_OK. */
3816 static bool
3817 nds32_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
3819 if (regno > FIRST_PSEUDO_REGISTER)
3820 return true;
3822 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE) && NDS32_IS_FPR_REGNUM (regno))
3824 if (NDS32_IS_EXT_FPR_REGNUM(regno))
3825 return (NDS32_FPR_REGNO_OK_FOR_DOUBLE(regno) && (mode == DFmode));
3826 else if (mode == SFmode || mode == SImode)
3827 return NDS32_FPR_REGNO_OK_FOR_SINGLE (regno);
3828 else if (mode == DFmode)
3829 return NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno);
3831 return false;
3834 /* Restrict double-word quantities to even register pairs. */
3835 if (regno <= NDS32_LAST_GPR_REGNUM)
3836 return (targetm.hard_regno_nregs (regno, mode) == 1
3837 || !((regno) & 1));
3839 return false;
3842 #undef TARGET_HARD_REGNO_MODE_OK
3843 #define TARGET_HARD_REGNO_MODE_OK nds32_hard_regno_mode_ok
3845 /* Implement TARGET_MODES_TIEABLE_P. We can use general registers to
3846 tie QI/HI/SI modes together. */
3848 static bool
3849 nds32_modes_tieable_p (machine_mode mode1, machine_mode mode2)
3851 if ((GET_MODE_CLASS (mode1) == MODE_INT
3852 && GET_MODE_CLASS (mode2) == MODE_INT)
3853 && GET_MODE_SIZE (mode1) <= UNITS_PER_WORD
3854 && GET_MODE_SIZE (mode2) <= UNITS_PER_WORD)
3855 return true;
3857 if (GET_MODE_SIZE (mode1) == GET_MODE_SIZE (mode2))
3859 if ((TARGET_FPU_SINGLE && !TARGET_FPU_DOUBLE)
3860 && (mode1 == DFmode || mode2 == DFmode))
3861 return false;
3862 else
3863 return true;
3866 return false;
3869 #undef TARGET_MODES_TIEABLE_P
3870 #define TARGET_MODES_TIEABLE_P nds32_modes_tieable_p
3872 /* Register Classes. */
3874 enum reg_class
3875 nds32_regno_reg_class (int regno)
3877 /* Refer to nds32.h for more register class details. */
3879 if (regno >= 0 && regno <= 7)
3880 return LOW_REGS;
3881 else if (regno >= 8 && regno <= 11)
3882 return MIDDLE_REGS;
3883 else if (regno >= 12 && regno <= 14)
3884 return HIGH_REGS;
3885 else if (regno == 15)
3886 return R15_TA_REG;
3887 else if (regno >= 16 && regno <= 19)
3888 return MIDDLE_REGS;
3889 else if (regno >= 20 && regno <= 31)
3890 return HIGH_REGS;
3891 else if (regno == 32 || regno == 33)
3893 /* $SFP and $AP is FRAME_REGS in fact, However prevent IRA don't
3894 know how to allocate register for $SFP and $AP, just tell IRA they
3895 are GENERAL_REGS, and ARM do this hack too. */
3896 return GENERAL_REGS;
3898 else if (regno >= 34 && regno <= 97)
3899 return FP_REGS;
3900 else
3901 return NO_REGS;
3905 /* Stack Layout and Calling Conventions. */
3907 /* -- Basic Stack Layout. */
3910 nds32_dynamic_chain_address (rtx frameaddr)
3912 if (TARGET_V3PUSH)
3914 /* If -mv3push is specified, we push $fp, $gp, and $lp into stack.
3915 We can access dynamic chain address from stack by [$fp - 12]. */
3916 return plus_constant (Pmode, frameaddr, -12);
3918 else
3920 /* For general case we push $fp and $lp into stack at prologue.
3921 We can access dynamic chain address from stack by [$fp - 8]. */
3922 return plus_constant (Pmode, frameaddr, -8);
3927 nds32_return_addr_rtx (int count,
3928 rtx frameaddr)
3930 int offset;
3931 rtx addr;
3933 if (count != 0)
3935 /* In nds32 ABI design, we can expect that $lp is always available
3936 from stack by [$fp - 4] location. */
3937 offset = -4;
3938 addr = plus_constant (Pmode, frameaddr, offset);
3939 addr = memory_address (Pmode, addr);
3941 return gen_rtx_MEM (Pmode, addr);
3944 /* If count == 0, it means we are at current frame,
3945 the return address is $r30 ($lp). */
3946 return get_hard_reg_initial_val (Pmode, LP_REGNUM);
3949 /* -- Eliminating Frame Pointer and Arg Pointer. */
3951 HOST_WIDE_INT
3952 nds32_initial_elimination_offset (unsigned int from_reg, unsigned int to_reg)
3954 HOST_WIDE_INT offset;
3956 /* Compute and setup stack frame size.
3957 The result will be in cfun->machine. */
3958 nds32_compute_stack_frame ();
3960 /* Remember to consider
3961 cfun->machine->callee_saved_area_gpr_padding_bytes and
3962 cfun->machine->eh_return_data_regs_size
3963 when calculating offset. */
3964 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
3966 offset = (cfun->machine->fp_size
3967 + cfun->machine->gp_size
3968 + cfun->machine->lp_size
3969 + cfun->machine->callee_saved_gpr_regs_size
3970 + cfun->machine->callee_saved_area_gpr_padding_bytes
3971 + cfun->machine->callee_saved_fpr_regs_size
3972 + cfun->machine->eh_return_data_regs_size
3973 + cfun->machine->local_size
3974 + cfun->machine->out_args_size);
3976 else if (from_reg == ARG_POINTER_REGNUM
3977 && to_reg == HARD_FRAME_POINTER_REGNUM)
3979 offset = 0;
3981 else if (from_reg == FRAME_POINTER_REGNUM
3982 && to_reg == STACK_POINTER_REGNUM)
3984 offset = (cfun->machine->local_size + cfun->machine->out_args_size);
3986 else if (from_reg == FRAME_POINTER_REGNUM
3987 && to_reg == HARD_FRAME_POINTER_REGNUM)
3989 offset = (-1) * (cfun->machine->fp_size
3990 + cfun->machine->gp_size
3991 + cfun->machine->lp_size
3992 + cfun->machine->callee_saved_gpr_regs_size
3993 + cfun->machine->callee_saved_area_gpr_padding_bytes
3994 + cfun->machine->callee_saved_fpr_regs_size
3995 + cfun->machine->eh_return_data_regs_size);
3997 else
3999 gcc_unreachable ();
4002 return offset;
4005 /* -- Passing Arguments in Registers. */
4007 void
4008 nds32_init_cumulative_args (CUMULATIVE_ARGS *cum,
4009 tree fntype ATTRIBUTE_UNUSED,
4010 rtx libname ATTRIBUTE_UNUSED,
4011 tree fndecl ATTRIBUTE_UNUSED,
4012 int n_named_args ATTRIBUTE_UNUSED)
4014 /* Initial available registers. The values are offset against
4015 NDS32_GPR_ARG_FIRST_REGNUM and NDS32_FPR_ARG_FIRST_REGNUM
4016 for passing arguments. */
4017 cum->gpr_offset = 0;
4018 cum->fpr_offset = 0;
4021 /* -- Function Entry and Exit. */
4023 /* Function for normal multiple push prologue. */
4024 void
4025 nds32_expand_prologue (void)
4027 int fp_adjust;
4028 int sp_adjust;
4029 unsigned Rb, Re;
4031 /* Compute and setup stack frame size.
4032 The result will be in cfun->machine. */
4033 nds32_compute_stack_frame ();
4035 /* If this is a variadic function, first we need to push argument
4036 registers that hold the unnamed argument value. */
4037 if (cfun->machine->va_args_size != 0)
4039 Rb = cfun->machine->va_args_first_regno;
4040 Re = cfun->machine->va_args_last_regno;
4041 /* No need to push $fp, $gp, or $lp. */
4042 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, true);
4044 /* We may also need to adjust stack pointer for padding bytes
4045 because varargs may cause $sp not 8-byte aligned. */
4046 if (cfun->machine->va_args_area_padding_bytes)
4048 /* Generate sp adjustment instruction. */
4049 sp_adjust = cfun->machine->va_args_area_padding_bytes;
4051 nds32_emit_adjust_frame (stack_pointer_rtx,
4052 stack_pointer_rtx,
4053 -1 * sp_adjust);
4057 /* If the function is 'naked',
4058 we do not have to generate prologue code fragment. */
4059 if (cfun->machine->naked_p)
4060 return;
4062 /* Get callee_first_regno and callee_last_regno. */
4063 Rb = cfun->machine->callee_saved_first_gpr_regno;
4064 Re = cfun->machine->callee_saved_last_gpr_regno;
4066 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4067 to be saved, we don't have to create multiple push instruction.
4068 Otherwise, a multiple push instruction is needed. */
4069 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4070 && cfun->machine->fp_size == 0
4071 && cfun->machine->gp_size == 0
4072 && cfun->machine->lp_size == 0))
4074 /* Create multiple push instruction rtx. */
4075 nds32_emit_stack_push_multiple (
4076 Rb, Re,
4077 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size,
4078 false);
4081 /* Save eh data registers. */
4082 if (cfun->machine->use_eh_return_p)
4084 Rb = cfun->machine->eh_return_data_first_regno;
4085 Re = cfun->machine->eh_return_data_last_regno;
4087 /* No need to push $fp, $gp, or $lp.
4088 Also, this is not variadic arguments push. */
4089 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, false);
4092 /* Check frame_pointer_needed to see
4093 if we shall emit fp adjustment instruction. */
4094 if (frame_pointer_needed)
4096 /* adjust $fp = $sp + ($fp size) + ($gp size) + ($lp size)
4097 + (4 * callee-saved-registers)
4098 + (4 * exception-handling-data-registers)
4099 Note: No need to adjust
4100 cfun->machine->callee_saved_area_gpr_padding_bytes,
4101 because, at this point, stack pointer is just
4102 at the position after push instruction. */
4103 fp_adjust = cfun->machine->fp_size
4104 + cfun->machine->gp_size
4105 + cfun->machine->lp_size
4106 + cfun->machine->callee_saved_gpr_regs_size
4107 + cfun->machine->eh_return_data_regs_size;
4109 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4110 stack_pointer_rtx,
4111 fp_adjust);
4114 /* Save fpu registers. */
4115 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4117 /* When $sp moved to bottom of stack, we need to check whether
4118 the range of offset in the FPU instruction. */
4119 int fpr_offset = cfun->machine->local_size
4120 + cfun->machine->out_args_size
4121 + cfun->machine->callee_saved_fpr_regs_size;
4123 /* Check FPU instruction offset imm14s. */
4124 if (!satisfies_constraint_Is14 (GEN_INT (fpr_offset)))
4126 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4127 + cfun->machine->callee_saved_fpr_regs_size;
4129 /* Save fpu registers, need to allocate stack space
4130 for fpu callee registers. And now $sp position
4131 on callee saved fpr registers. */
4132 nds32_emit_adjust_frame (stack_pointer_rtx,
4133 stack_pointer_rtx,
4134 -1 * fpr_space);
4136 /* Emit fpu store instruction, using [$sp + offset] store
4137 fpu registers. */
4138 nds32_emit_push_fpr_callee_saved (0);
4140 /* Adjust $sp = $sp - local_size - out_args_size. */
4141 sp_adjust = cfun->machine->local_size
4142 + cfun->machine->out_args_size;
4144 /* Allocate stack space for local size and out args size. */
4145 nds32_emit_adjust_frame (stack_pointer_rtx,
4146 stack_pointer_rtx,
4147 -1 * sp_adjust);
4149 else
4151 /* Offset range in Is14, so $sp moved to bottom of stack. */
4153 /* Adjust $sp = $sp - local_size - out_args_size
4154 - callee_saved_area_gpr_padding_bytes
4155 - callee_saved_fpr_regs_size. */
4156 sp_adjust = cfun->machine->local_size
4157 + cfun->machine->out_args_size
4158 + cfun->machine->callee_saved_area_gpr_padding_bytes
4159 + cfun->machine->callee_saved_fpr_regs_size;
4161 nds32_emit_adjust_frame (stack_pointer_rtx,
4162 stack_pointer_rtx,
4163 -1 * sp_adjust);
4165 /* Emit fpu store instruction, using [$sp + offset] store
4166 fpu registers. */
4167 int fpr_position = cfun->machine->out_args_size
4168 + cfun->machine->local_size;
4169 nds32_emit_push_fpr_callee_saved (fpr_position);
4172 else
4174 /* Adjust $sp = $sp - local_size - out_args_size
4175 - callee_saved_area_gpr_padding_bytes. */
4176 sp_adjust = cfun->machine->local_size
4177 + cfun->machine->out_args_size
4178 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4180 /* sp_adjust value may be out of range of the addi instruction,
4181 create alternative add behavior with TA_REGNUM if necessary,
4182 using NEGATIVE value to tell that we are decreasing address. */
4183 nds32_emit_adjust_frame (stack_pointer_rtx,
4184 stack_pointer_rtx,
4185 -1 * sp_adjust);
4188 /* Prevent the instruction scheduler from
4189 moving instructions across the boundary. */
4190 emit_insn (gen_blockage ());
4193 /* Function for normal multiple pop epilogue. */
4194 void
4195 nds32_expand_epilogue (bool sibcall_p)
4197 int sp_adjust;
4198 unsigned Rb, Re;
4200 /* Compute and setup stack frame size.
4201 The result will be in cfun->machine. */
4202 nds32_compute_stack_frame ();
4204 /* Prevent the instruction scheduler from
4205 moving instructions across the boundary. */
4206 emit_insn (gen_blockage ());
4208 /* If the function is 'naked', we do not have to generate
4209 epilogue code fragment BUT 'ret' instruction.
4210 However, if this function is also a variadic function,
4211 we need to create adjust stack pointer before 'ret' instruction. */
4212 if (cfun->machine->naked_p)
4214 /* If this is a variadic function, we do not have to restore argument
4215 registers but need to adjust stack pointer back to previous stack
4216 frame location before return. */
4217 if (cfun->machine->va_args_size != 0)
4219 /* Generate sp adjustment instruction.
4220 We need to consider padding bytes here. */
4221 sp_adjust = cfun->machine->va_args_size
4222 + cfun->machine->va_args_area_padding_bytes;
4224 nds32_emit_adjust_frame (stack_pointer_rtx,
4225 stack_pointer_rtx,
4226 sp_adjust);
4229 /* Generate return instruction by using 'return_internal' pattern.
4230 Make sure this instruction is after gen_blockage(). */
4231 if (!sibcall_p)
4232 emit_jump_insn (gen_return_internal ());
4233 return;
4236 if (frame_pointer_needed)
4238 /* Restore fpu registers. */
4239 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4241 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
4243 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4244 - (4 * callee-saved-registers)
4245 - (4 * exception-handling-data-registers)
4246 - (4 * callee-saved-gpr-registers padding byte)
4247 - (4 * callee-saved-fpr-registers)
4248 Note: we want to adjust stack pointer
4249 to the position for callee-saved fpr register,
4250 And restore fpu register use .bi instruction to adjust $sp
4251 from callee-saved fpr register to pop instruction. */
4252 sp_adjust = cfun->machine->fp_size
4253 + cfun->machine->gp_size
4254 + cfun->machine->lp_size
4255 + cfun->machine->callee_saved_gpr_regs_size
4256 + cfun->machine->eh_return_data_regs_size
4257 + cfun->machine->callee_saved_area_gpr_padding_bytes
4258 + cfun->machine->callee_saved_fpr_regs_size;
4260 nds32_emit_adjust_frame (stack_pointer_rtx,
4261 hard_frame_pointer_rtx,
4262 -1 * sp_adjust);
4264 /* Emit fpu load instruction, using .bi instruction
4265 load fpu registers. */
4266 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4268 else
4270 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4271 - (4 * callee-saved-registers)
4272 - (4 * exception-handling-data-registers)
4273 Note: No need to adjust
4274 cfun->machine->callee_saved_area_gpr_padding_bytes,
4275 because we want to adjust stack pointer
4276 to the position for pop instruction. */
4277 sp_adjust = cfun->machine->fp_size
4278 + cfun->machine->gp_size
4279 + cfun->machine->lp_size
4280 + cfun->machine->callee_saved_gpr_regs_size
4281 + cfun->machine->eh_return_data_regs_size;
4283 nds32_emit_adjust_frame (stack_pointer_rtx,
4284 hard_frame_pointer_rtx,
4285 -1 * sp_adjust);
4288 else
4290 /* Restore fpu registers. */
4291 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4293 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
4295 /* Adjust $sp = $sp + local_size + out_args_size. */
4296 sp_adjust = cfun->machine->local_size
4297 + cfun->machine->out_args_size;
4299 nds32_emit_adjust_frame (stack_pointer_rtx,
4300 stack_pointer_rtx,
4301 sp_adjust);
4303 /* Emit fpu load instruction, using .bi instruction
4304 load fpu registers, and adjust $sp from callee-saved fpr register
4305 to callee-saved gpr register. */
4306 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4308 else
4310 /* If frame pointer is NOT needed,
4311 we cannot calculate the sp adjustment from frame pointer.
4312 Instead, we calculate the adjustment by local_size,
4313 out_args_size, and callee_saved_area_gpr_padding_bytes.
4314 Notice that such sp adjustment value may be out of range,
4315 so we have to deal with it as well. */
4317 /* Adjust $sp = $sp + local_size + out_args_size
4318 + callee_saved_area_gpr_padding_bytes. */
4319 sp_adjust = cfun->machine->local_size
4320 + cfun->machine->out_args_size
4321 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4323 nds32_emit_adjust_frame (stack_pointer_rtx,
4324 stack_pointer_rtx,
4325 sp_adjust);
4329 /* Restore eh data registers. */
4330 if (cfun->machine->use_eh_return_p)
4332 Rb = cfun->machine->eh_return_data_first_regno;
4333 Re = cfun->machine->eh_return_data_last_regno;
4335 /* No need to pop $fp, $gp, or $lp. */
4336 nds32_emit_stack_pop_multiple (Rb, Re, false, false, false);
4339 /* Get callee_first_regno and callee_last_regno. */
4340 Rb = cfun->machine->callee_saved_first_gpr_regno;
4341 Re = cfun->machine->callee_saved_last_gpr_regno;
4343 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4344 to be saved, we don't have to create multiple pop instruction.
4345 Otherwise, a multiple pop instruction is needed. */
4346 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4347 && cfun->machine->fp_size == 0
4348 && cfun->machine->gp_size == 0
4349 && cfun->machine->lp_size == 0))
4351 /* Create multiple pop instruction rtx. */
4352 nds32_emit_stack_pop_multiple (
4353 Rb, Re,
4354 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size);
4357 /* If this is a variadic function, we do not have to restore argument
4358 registers but need to adjust stack pointer back to previous stack
4359 frame location before return. */
4360 if (cfun->machine->va_args_size != 0)
4362 /* Generate sp adjustment instruction.
4363 We need to consider padding bytes here. */
4364 sp_adjust = cfun->machine->va_args_size
4365 + cfun->machine->va_args_area_padding_bytes;
4367 nds32_emit_adjust_frame (stack_pointer_rtx,
4368 stack_pointer_rtx,
4369 sp_adjust);
4372 /* If this function uses __builtin_eh_return, make stack adjustment
4373 for exception handler. */
4374 if (cfun->machine->use_eh_return_p)
4376 /* We need to unwind the stack by the offset computed by
4377 EH_RETURN_STACKADJ_RTX. However, at this point the CFA is
4378 based on SP. Ideally we would update the SP and define the
4379 CFA along the lines of:
4381 SP = SP + EH_RETURN_STACKADJ_RTX
4382 (regnote CFA = SP - EH_RETURN_STACKADJ_RTX)
4384 However the dwarf emitter only understands a constant
4385 register offset.
4387 The solution chosen here is to use the otherwise $ta ($r15)
4388 as a temporary register to hold the current SP value. The
4389 CFA is described using $ta then SP is modified. */
4391 rtx ta_reg;
4392 rtx insn;
4394 ta_reg = gen_rtx_REG (SImode, TA_REGNUM);
4396 insn = emit_move_insn (ta_reg, stack_pointer_rtx);
4397 add_reg_note (insn, REG_CFA_DEF_CFA, ta_reg);
4398 RTX_FRAME_RELATED_P (insn) = 1;
4400 emit_insn (gen_addsi3 (stack_pointer_rtx,
4401 stack_pointer_rtx,
4402 EH_RETURN_STACKADJ_RTX));
4404 /* Ensure the assignment to $ta does not get optimized away. */
4405 emit_use (ta_reg);
4408 /* Generate return instruction. */
4409 if (!sibcall_p)
4410 emit_jump_insn (gen_return_internal ());
4413 /* Function for v3push prologue. */
4414 void
4415 nds32_expand_prologue_v3push (void)
4417 int fp_adjust;
4418 int sp_adjust;
4419 int fpr_space = 0;
4420 unsigned Rb, Re;
4422 /* Compute and setup stack frame size.
4423 The result will be in cfun->machine. */
4424 nds32_compute_stack_frame ();
4426 if (cfun->machine->callee_saved_gpr_regs_size > 0)
4427 df_set_regs_ever_live (FP_REGNUM, 1);
4429 /* If the function is 'naked',
4430 we do not have to generate prologue code fragment. */
4431 if (cfun->machine->naked_p)
4432 return;
4434 /* Get callee_first_regno and callee_last_regno. */
4435 Rb = cfun->machine->callee_saved_first_gpr_regno;
4436 Re = cfun->machine->callee_saved_last_gpr_regno;
4438 /* Calculate sp_adjust first to test if 'push25 Re,imm8u' is available,
4439 where imm8u has to be 8-byte alignment. */
4440 sp_adjust = cfun->machine->local_size
4441 + cfun->machine->out_args_size
4442 + cfun->machine->callee_saved_area_gpr_padding_bytes
4443 + cfun->machine->callee_saved_fpr_regs_size;
4445 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4446 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust))
4448 /* We can use 'push25 Re,imm8u'. */
4450 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
4451 the pattern 'stack_v3push' is implemented in nds32.md. */
4452 nds32_emit_stack_v3push (Rb, Re, sp_adjust);
4454 /* Save fpu registers. */
4455 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4457 /* Calculate fpr position. */
4458 int fpr_position = cfun->machine->local_size
4459 + cfun->machine->out_args_size;
4460 /* Emit fpu store instruction, using [$sp + offset] store
4461 fpu registers. */
4462 nds32_emit_push_fpr_callee_saved (fpr_position);
4465 /* Check frame_pointer_needed to see
4466 if we shall emit fp adjustment instruction. */
4467 if (frame_pointer_needed)
4469 /* adjust $fp = $sp + 4 ($fp size)
4470 + 4 ($gp size)
4471 + 4 ($lp size)
4472 + (4 * n) (callee-saved registers)
4473 + sp_adjust ('push25 Re,imm8u')
4474 Note: Since we use 'push25 Re,imm8u',
4475 the position of stack pointer is further
4476 changed after push instruction.
4477 Hence, we need to take sp_adjust value
4478 into consideration. */
4479 fp_adjust = cfun->machine->fp_size
4480 + cfun->machine->gp_size
4481 + cfun->machine->lp_size
4482 + cfun->machine->callee_saved_gpr_regs_size
4483 + sp_adjust;
4485 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4486 stack_pointer_rtx,
4487 fp_adjust);
4490 else
4492 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4494 /* Calculate fpr space. */
4495 fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4496 + cfun->machine->callee_saved_fpr_regs_size;
4498 /* We have to use 'push25 Re, fpr_space', to pre-allocate
4499 callee saved fpr registers space. */
4500 nds32_emit_stack_v3push (Rb, Re, fpr_space);
4501 nds32_emit_push_fpr_callee_saved (0);
4503 else
4505 /* We have to use 'push25 Re,0' and
4506 expand one more instruction to adjust $sp later. */
4508 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
4509 the pattern 'stack_v3push' is implemented in nds32.md. */
4510 nds32_emit_stack_v3push (Rb, Re, 0);
4513 /* Check frame_pointer_needed to see
4514 if we shall emit fp adjustment instruction. */
4515 if (frame_pointer_needed)
4517 /* adjust $fp = $sp + 4 ($fp size)
4518 + 4 ($gp size)
4519 + 4 ($lp size)
4520 + (4 * n) (callee-saved registers)
4521 Note: Since we use 'push25 Re,0',
4522 the stack pointer is just at the position
4523 after push instruction.
4524 No need to take sp_adjust into consideration. */
4525 fp_adjust = cfun->machine->fp_size
4526 + cfun->machine->gp_size
4527 + cfun->machine->lp_size
4528 + cfun->machine->callee_saved_gpr_regs_size;
4530 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4532 /* We use 'push25 Re, fpr_space', the $sp is
4533 on callee saved fpr position, so need to consider
4534 fpr space. */
4535 fp_adjust = fp_adjust + fpr_space;
4538 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4539 stack_pointer_rtx,
4540 fp_adjust);
4543 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4545 /* We use 'push25 Re, fpr_space',
4546 the $sp is on callee saved fpr position,
4547 no need to consider fpr space. */
4548 sp_adjust = sp_adjust - fpr_space;
4551 /* Because we use 'push25 Re,0',
4552 we need to expand one more instruction to adjust $sp.
4553 using NEGATIVE value to tell that we are decreasing address. */
4554 nds32_emit_adjust_frame (stack_pointer_rtx,
4555 stack_pointer_rtx,
4556 -1 * sp_adjust);
4559 /* Prevent the instruction scheduler from
4560 moving instructions across the boundary. */
4561 emit_insn (gen_blockage ());
4564 /* Function for v3pop epilogue. */
4565 void
4566 nds32_expand_epilogue_v3pop (bool sibcall_p)
4568 int sp_adjust;
4569 unsigned Rb, Re;
4571 /* Compute and setup stack frame size.
4572 The result will be in cfun->machine. */
4573 nds32_compute_stack_frame ();
4575 /* Prevent the instruction scheduler from
4576 moving instructions across the boundary. */
4577 emit_insn (gen_blockage ());
4579 /* If the function is 'naked', we do not have to generate
4580 epilogue code fragment BUT 'ret' instruction. */
4581 if (cfun->machine->naked_p)
4583 /* Generate return instruction by using 'return_internal' pattern.
4584 Make sure this instruction is after gen_blockage(). */
4585 if (!sibcall_p)
4586 emit_jump_insn (gen_return_internal ());
4587 return;
4590 /* Get callee_first_regno and callee_last_regno. */
4591 Rb = cfun->machine->callee_saved_first_gpr_regno;
4592 Re = cfun->machine->callee_saved_last_gpr_regno;
4594 /* Calculate sp_adjust first to test if 'pop25 Re,imm8u' is available,
4595 where imm8u has to be 8-byte alignment. */
4596 sp_adjust = cfun->machine->local_size
4597 + cfun->machine->out_args_size
4598 + cfun->machine->callee_saved_area_gpr_padding_bytes
4599 + cfun->machine->callee_saved_fpr_regs_size;
4601 /* We have to consider alloca issue as well.
4602 If the function does call alloca(), the stack pointer is not fixed.
4603 In that case, we cannot use 'pop25 Re,imm8u' directly.
4604 We have to caculate stack pointer from frame pointer
4605 and then use 'pop25 Re,0'.
4606 Of course, the frame_pointer_needed should be nonzero
4607 if the function calls alloca(). */
4608 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4609 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
4610 && !cfun->calls_alloca)
4612 /* Restore fpu registers. */
4613 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4615 int fpr_position = cfun->machine->local_size
4616 + cfun->machine->out_args_size;
4617 /* Emit fpu load instruction, using [$sp + offset] restore
4618 fpu registers. */
4619 nds32_emit_v3pop_fpr_callee_saved (fpr_position);
4622 /* We can use 'pop25 Re,imm8u'. */
4624 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
4625 the pattern 'stack_v3pop' is implementad in nds32.md. */
4626 nds32_emit_stack_v3pop (Rb, Re, sp_adjust);
4628 else
4630 /* We have to use 'pop25 Re,0', and prior to it,
4631 we must expand one more instruction to adjust $sp. */
4633 if (frame_pointer_needed)
4635 /* adjust $sp = $fp - 4 ($fp size)
4636 - 4 ($gp size)
4637 - 4 ($lp size)
4638 - (4 * n) (callee-saved registers)
4639 Note: No need to adjust
4640 cfun->machine->callee_saved_area_gpr_padding_bytes,
4641 because we want to adjust stack pointer
4642 to the position for pop instruction. */
4643 sp_adjust = cfun->machine->fp_size
4644 + cfun->machine->gp_size
4645 + cfun->machine->lp_size
4646 + cfun->machine->callee_saved_gpr_regs_size;
4648 /* Restore fpu registers. */
4649 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4651 /* Set $sp to callee saved fpr position, we need to restore
4652 fpr registers. */
4653 sp_adjust = sp_adjust
4654 + cfun->machine->callee_saved_area_gpr_padding_bytes
4655 + cfun->machine->callee_saved_fpr_regs_size;
4657 nds32_emit_adjust_frame (stack_pointer_rtx,
4658 hard_frame_pointer_rtx,
4659 -1 * sp_adjust);
4661 /* Emit fpu load instruction, using [$sp + offset] restore
4662 fpu registers. */
4663 nds32_emit_v3pop_fpr_callee_saved (0);
4665 else
4667 nds32_emit_adjust_frame (stack_pointer_rtx,
4668 hard_frame_pointer_rtx,
4669 -1 * sp_adjust);
4672 else
4674 /* If frame pointer is NOT needed,
4675 we cannot calculate the sp adjustment from frame pointer.
4676 Instead, we calculate the adjustment by local_size,
4677 out_args_size, and callee_saved_area_padding_bytes.
4678 Notice that such sp adjustment value may be out of range,
4679 so we have to deal with it as well. */
4681 /* Adjust $sp = $sp + local_size + out_args_size
4682 + callee_saved_area_gpr_padding_bytes
4683 + callee_saved_fpr_regs_size. */
4684 sp_adjust = cfun->machine->local_size
4685 + cfun->machine->out_args_size
4686 + cfun->machine->callee_saved_area_gpr_padding_bytes
4687 + cfun->machine->callee_saved_fpr_regs_size;
4689 /* Restore fpu registers. */
4690 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4692 /* Set $sp to callee saved fpr position, we need to restore
4693 fpr registers. */
4694 sp_adjust = sp_adjust
4695 - cfun->machine->callee_saved_area_gpr_padding_bytes
4696 - cfun->machine->callee_saved_fpr_regs_size;
4698 nds32_emit_adjust_frame (stack_pointer_rtx,
4699 stack_pointer_rtx,
4700 sp_adjust);
4702 /* Emit fpu load instruction, using [$sp + offset] restore
4703 fpu registers. */
4704 nds32_emit_v3pop_fpr_callee_saved (0);
4706 else
4708 /* sp_adjust value may be out of range of the addi instruction,
4709 create alternative add behavior with TA_REGNUM if necessary,
4710 using POSITIVE value to tell that we are increasing
4711 address. */
4712 nds32_emit_adjust_frame (stack_pointer_rtx,
4713 stack_pointer_rtx,
4714 sp_adjust);
4718 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4720 /* We have fpr need to restore, so $sp is set on callee saved fpr
4721 position. And we use 'pop25 Re, fpr_space' to adjust $sp. */
4722 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4723 + cfun->machine->callee_saved_fpr_regs_size;
4724 nds32_emit_stack_v3pop (Rb, Re, fpr_space);
4726 else
4728 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
4729 the pattern 'stack_v3pop' is implementad in nds32.md. */
4730 nds32_emit_stack_v3pop (Rb, Re, 0);
4733 /* Generate return instruction. */
4734 emit_jump_insn (gen_pop25return ());
4737 /* Return nonzero if this function is known to have a null epilogue.
4738 This allows the optimizer to omit jumps to jumps if no stack
4739 was created. */
4741 nds32_can_use_return_insn (void)
4743 int sp_adjust;
4745 /* Prior to reloading, we can't tell how many registers must be saved.
4746 Thus we can not determine whether this function has null epilogue. */
4747 if (!reload_completed)
4748 return 0;
4750 sp_adjust = cfun->machine->local_size
4751 + cfun->machine->out_args_size
4752 + cfun->machine->callee_saved_area_gpr_padding_bytes
4753 + cfun->machine->callee_saved_fpr_regs_size;
4754 if (!cfun->machine->fp_as_gp_p
4755 && satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4756 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
4757 && !cfun->calls_alloca
4758 && NDS32_V3PUSH_AVAILABLE_P
4759 && !(TARGET_HARD_FLOAT
4760 && (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)))
4761 return 1;
4763 /* If no stack was created, two conditions must be satisfied:
4764 1. This is a naked function.
4765 So there is no callee-saved, local size, or outgoing size.
4766 2. This is NOT a variadic function.
4767 So there is no pushing arguement registers into the stack. */
4768 return (cfun->machine->naked_p && (cfun->machine->va_args_size == 0));
4771 /* ------------------------------------------------------------------------ */
4773 /* Function to test 333-form for load/store instructions.
4774 This is auxiliary extern function for auxiliary macro in nds32.h.
4775 Because it is a little complicated, we use function instead of macro. */
4776 bool
4777 nds32_ls_333_p (rtx rt, rtx ra, rtx imm, machine_mode mode)
4779 if (REGNO_REG_CLASS (REGNO (rt)) == LOW_REGS
4780 && REGNO_REG_CLASS (REGNO (ra)) == LOW_REGS)
4782 if (GET_MODE_SIZE (mode) == 4)
4783 return satisfies_constraint_Iu05 (imm);
4785 if (GET_MODE_SIZE (mode) == 2)
4786 return satisfies_constraint_Iu04 (imm);
4788 if (GET_MODE_SIZE (mode) == 1)
4789 return satisfies_constraint_Iu03 (imm);
4792 return false;
4795 /* Return alignment for the label. */
4797 nds32_target_alignment (rtx_insn *label)
4799 rtx_insn *insn;
4801 if (!NDS32_ALIGN_P ())
4802 return 0;
4804 insn = next_active_insn (label);
4806 /* Always align to 4 byte when first instruction after label is jump
4807 instruction since length for that might changed, so let's always align
4808 it for make sure we don't lose any perfomance here. */
4809 if (insn == 0
4810 || (get_attr_length (insn) == 2
4811 && !JUMP_P (insn) && !CALL_P (insn)))
4812 return 0;
4813 else
4814 return 2;
4817 bool
4818 nds32_split_double_word_load_store_p(rtx *operands, bool load_p)
4820 rtx mem = load_p ? operands[1] : operands[0];
4821 /* Do split at split2 if -O0 or schedule 2 not enable. */
4822 if (optimize == 0 || !flag_schedule_insns_after_reload)
4823 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
4825 /* Split double word load store after copy propgation. */
4826 if (current_pass == NULL)
4827 return false;
4829 const char *pass_name = current_pass->name;
4830 if (pass_name && ((strcmp (pass_name, "split4") == 0)
4831 || (strcmp (pass_name, "split5") == 0)))
4832 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
4834 return false;
4837 static bool
4838 nds32_use_blocks_for_constant_p (machine_mode mode,
4839 const_rtx x ATTRIBUTE_UNUSED)
4841 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
4842 && (mode == DFmode || mode == SFmode))
4843 return true;
4844 else
4845 return false;
4848 /* ------------------------------------------------------------------------ */
4850 /* PART 5: Initialize target hook structure and definitions. */
4852 /* Controlling the Compilation Driver. */
4855 /* Run-time Target Specification. */
4858 /* Defining Data Structures for Per-function Information. */
4861 /* Storage Layout. */
4863 #undef TARGET_PROMOTE_FUNCTION_MODE
4864 #define TARGET_PROMOTE_FUNCTION_MODE \
4865 default_promote_function_mode_always_promote
4867 #undef TARGET_EXPAND_TO_RTL_HOOK
4868 #define TARGET_EXPAND_TO_RTL_HOOK nds32_expand_to_rtl_hook
4871 /* Layout of Source Language Data Types. */
4874 /* Register Usage. */
4876 /* -- Basic Characteristics of Registers. */
4878 #undef TARGET_CONDITIONAL_REGISTER_USAGE
4879 #define TARGET_CONDITIONAL_REGISTER_USAGE nds32_conditional_register_usage
4881 /* -- Order of Allocation of Registers. */
4883 /* -- How Values Fit in Registers. */
4885 #undef TARGET_HARD_REGNO_NREGS
4886 #define TARGET_HARD_REGNO_NREGS nds32_hard_regno_nregs
4888 /* -- Handling Leaf Functions. */
4890 /* -- Registers That Form a Stack. */
4893 /* Register Classes. */
4895 #undef TARGET_CLASS_MAX_NREGS
4896 #define TARGET_CLASS_MAX_NREGS nds32_class_max_nregs
4898 #undef TARGET_REGISTER_PRIORITY
4899 #define TARGET_REGISTER_PRIORITY nds32_register_priority
4901 #undef TARGET_CAN_CHANGE_MODE_CLASS
4902 #define TARGET_CAN_CHANGE_MODE_CLASS nds32_can_change_mode_class
4905 /* Obsolete Macros for Defining Constraints. */
4908 /* Stack Layout and Calling Conventions. */
4910 /* -- Basic Stack Layout. */
4912 /* -- Exception Handling Support. */
4914 /* -- Specifying How Stack Checking is Done. */
4916 /* -- Registers That Address the Stack Frame. */
4918 /* -- Eliminating Frame Pointer and Arg Pointer. */
4920 #undef TARGET_CAN_ELIMINATE
4921 #define TARGET_CAN_ELIMINATE nds32_can_eliminate
4923 /* -- Passing Function Arguments on the Stack. */
4925 /* -- Passing Arguments in Registers. */
4927 #undef TARGET_FUNCTION_ARG
4928 #define TARGET_FUNCTION_ARG nds32_function_arg
4930 #undef TARGET_MUST_PASS_IN_STACK
4931 #define TARGET_MUST_PASS_IN_STACK nds32_must_pass_in_stack
4933 #undef TARGET_ARG_PARTIAL_BYTES
4934 #define TARGET_ARG_PARTIAL_BYTES nds32_arg_partial_bytes
4936 #undef TARGET_FUNCTION_ARG_ADVANCE
4937 #define TARGET_FUNCTION_ARG_ADVANCE nds32_function_arg_advance
4939 #undef TARGET_FUNCTION_ARG_BOUNDARY
4940 #define TARGET_FUNCTION_ARG_BOUNDARY nds32_function_arg_boundary
4942 /* -- How Scalar Function Values Are Returned. */
4944 #undef TARGET_FUNCTION_VALUE
4945 #define TARGET_FUNCTION_VALUE nds32_function_value
4947 #undef TARGET_LIBCALL_VALUE
4948 #define TARGET_LIBCALL_VALUE nds32_libcall_value
4950 #undef TARGET_FUNCTION_VALUE_REGNO_P
4951 #define TARGET_FUNCTION_VALUE_REGNO_P nds32_function_value_regno_p
4953 /* -- How Large Values Are Returned. */
4955 #undef TARGET_RETURN_IN_MEMORY
4956 #define TARGET_RETURN_IN_MEMORY nds32_return_in_memory
4958 /* -- Caller-Saves Register Allocation. */
4960 /* -- Function Entry and Exit. */
4962 #undef TARGET_ASM_FUNCTION_PROLOGUE
4963 #define TARGET_ASM_FUNCTION_PROLOGUE nds32_asm_function_prologue
4965 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
4966 #define TARGET_ASM_FUNCTION_END_PROLOGUE nds32_asm_function_end_prologue
4968 #undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
4969 #define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE nds32_asm_function_begin_epilogue
4971 #undef TARGET_ASM_FUNCTION_EPILOGUE
4972 #define TARGET_ASM_FUNCTION_EPILOGUE nds32_asm_function_epilogue
4974 #undef TARGET_ASM_OUTPUT_MI_THUNK
4975 #define TARGET_ASM_OUTPUT_MI_THUNK nds32_asm_output_mi_thunk
4977 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
4978 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
4980 /* -- Generating Code for Profiling. */
4982 /* -- Permitting tail calls. */
4984 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
4985 #define TARGET_FUNCTION_OK_FOR_SIBCALL nds32_function_ok_for_sibcall
4987 #undef TARGET_WARN_FUNC_RETURN
4988 #define TARGET_WARN_FUNC_RETURN nds32_warn_func_return
4990 /* Stack smashing protection. */
4993 /* Implementing the Varargs Macros. */
4995 #undef TARGET_SETUP_INCOMING_VARARGS
4996 #define TARGET_SETUP_INCOMING_VARARGS nds32_setup_incoming_varargs
4998 #undef TARGET_STRICT_ARGUMENT_NAMING
4999 #define TARGET_STRICT_ARGUMENT_NAMING nds32_strict_argument_naming
5002 /* Trampolines for Nested Functions. */
5004 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
5005 #define TARGET_ASM_TRAMPOLINE_TEMPLATE nds32_asm_trampoline_template
5007 #undef TARGET_TRAMPOLINE_INIT
5008 #define TARGET_TRAMPOLINE_INIT nds32_trampoline_init
5011 /* Implicit Calls to Library Routines. */
5014 /* Addressing Modes. */
5016 #undef TARGET_LEGITIMATE_ADDRESS_P
5017 #define TARGET_LEGITIMATE_ADDRESS_P nds32_legitimate_address_p
5020 /* Anchored Addresses. */
5023 /* Condition Code Status. */
5025 /* -- Representation of condition codes using (cc0). */
5027 /* -- Representation of condition codes using registers. */
5029 #undef TARGET_CANONICALIZE_COMPARISON
5030 #define TARGET_CANONICALIZE_COMPARISON nds32_canonicalize_comparison
5032 /* -- Macros to control conditional execution. */
5035 /* Describing Relative Costs of Operations. */
5037 #undef TARGET_REGISTER_MOVE_COST
5038 #define TARGET_REGISTER_MOVE_COST nds32_register_move_cost
5040 #undef TARGET_MEMORY_MOVE_COST
5041 #define TARGET_MEMORY_MOVE_COST nds32_memory_move_cost
5043 #undef TARGET_RTX_COSTS
5044 #define TARGET_RTX_COSTS nds32_rtx_costs
5046 #undef TARGET_ADDRESS_COST
5047 #define TARGET_ADDRESS_COST nds32_address_cost
5050 /* Adjusting the Instruction Scheduler. */
5053 /* Dividing the Output into Sections (Texts, Data, . . . ). */
5055 #undef TARGET_ENCODE_SECTION_INFO
5056 #define TARGET_ENCODE_SECTION_INFO nds32_encode_section_info
5059 /* Position Independent Code. */
5062 /* Defining the Output Assembler Language. */
5064 /* -- The Overall Framework of an Assembler File. */
5066 #undef TARGET_ASM_FILE_START
5067 #define TARGET_ASM_FILE_START nds32_asm_file_start
5068 #undef TARGET_ASM_FILE_END
5069 #define TARGET_ASM_FILE_END nds32_asm_file_end
5071 /* -- Output of Data. */
5073 #undef TARGET_ASM_ALIGNED_HI_OP
5074 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
5076 #undef TARGET_ASM_ALIGNED_SI_OP
5077 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
5079 /* -- Output of Uninitialized Variables. */
5081 /* -- Output and Generation of Labels. */
5083 #undef TARGET_ASM_GLOBALIZE_LABEL
5084 #define TARGET_ASM_GLOBALIZE_LABEL nds32_asm_globalize_label
5086 /* -- How Initialization Functions Are Handled. */
5088 /* -- Macros Controlling Initialization Routines. */
5090 /* -- Output of Assembler Instructions. */
5092 #undef TARGET_PRINT_OPERAND
5093 #define TARGET_PRINT_OPERAND nds32_print_operand
5094 #undef TARGET_PRINT_OPERAND_ADDRESS
5095 #define TARGET_PRINT_OPERAND_ADDRESS nds32_print_operand_address
5097 /* -- Output of Dispatch Tables. */
5099 /* -- Assembler Commands for Exception Regions. */
5101 #undef TARGET_DWARF_REGISTER_SPAN
5102 #define TARGET_DWARF_REGISTER_SPAN nds32_dwarf_register_span
5104 /* -- Assembler Commands for Alignment. */
5107 /* Controlling Debugging Information Format. */
5109 /* -- Macros Affecting All Debugging Formats. */
5111 /* -- Specific Options for DBX Output. */
5113 /* -- Open-Ended Hooks for DBX Format. */
5115 /* -- File Names in DBX Format. */
5117 /* -- Macros for DWARF Output. */
5119 /* -- Macros for VMS Debug Format. */
5122 /* Cross Compilation and Floating Point. */
5125 /* Mode Switching Instructions. */
5128 /* Defining target-specific uses of __attribute__. */
5130 #undef TARGET_ATTRIBUTE_TABLE
5131 #define TARGET_ATTRIBUTE_TABLE nds32_attribute_table
5133 #undef TARGET_MERGE_DECL_ATTRIBUTES
5134 #define TARGET_MERGE_DECL_ATTRIBUTES nds32_merge_decl_attributes
5136 #undef TARGET_INSERT_ATTRIBUTES
5137 #define TARGET_INSERT_ATTRIBUTES nds32_insert_attributes
5139 #undef TARGET_OPTION_PRAGMA_PARSE
5140 #define TARGET_OPTION_PRAGMA_PARSE nds32_option_pragma_parse
5142 #undef TARGET_OPTION_OVERRIDE
5143 #define TARGET_OPTION_OVERRIDE nds32_option_override
5146 /* Emulating TLS. */
5149 /* Defining coprocessor specifics for MIPS targets. */
5152 /* Parameters for Precompiled Header Validity Checking. */
5155 /* C++ ABI parameters. */
5158 /* Adding support for named address spaces. */
5161 /* Miscellaneous Parameters. */
5163 #undef TARGET_MD_ASM_ADJUST
5164 #define TARGET_MD_ASM_ADJUST nds32_md_asm_adjust
5166 #undef TARGET_INIT_BUILTINS
5167 #define TARGET_INIT_BUILTINS nds32_init_builtins
5169 #undef TARGET_BUILTIN_DECL
5170 #define TARGET_BUILTIN_DECL nds32_builtin_decl
5172 #undef TARGET_EXPAND_BUILTIN
5173 #define TARGET_EXPAND_BUILTIN nds32_expand_builtin
5176 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
5177 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P nds32_use_blocks_for_constant_p
5180 /* ------------------------------------------------------------------------ */
5182 /* Initialize the GCC target structure. */
5184 struct gcc_target targetm = TARGET_INITIALIZER;
5186 /* ------------------------------------------------------------------------ */