[NDS32] New option -malways-align and -malign-functions.
[official-gcc.git] / gcc / config / nds32 / nds32.c
blob76980d3ab1c0ad3ba762db68a2c24b76bfb1ba9b
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 return machine;
348 /* Function to compute stack frame size and
349 store into cfun->machine structure. */
350 static void
351 nds32_compute_stack_frame (void)
353 int r;
354 int block_size;
355 bool v3pushpop_p;
357 /* Because nds32_compute_stack_frame() will be called from different place,
358 everytime we enter this function, we have to assume this function
359 needs prologue/epilogue. */
360 cfun->machine->naked_p = 0;
363 /* If __builtin_eh_return is used, we better have frame pointer needed
364 so that we can easily locate the stack slot of return address. */
365 if (crtl->calls_eh_return)
367 frame_pointer_needed = 1;
369 /* We need to mark eh data registers that need to be saved
370 in the stack. */
371 cfun->machine->eh_return_data_first_regno = EH_RETURN_DATA_REGNO (0);
372 for (r = 0; EH_RETURN_DATA_REGNO (r) != INVALID_REGNUM; r++)
373 cfun->machine->eh_return_data_last_regno = r;
375 cfun->machine->eh_return_data_regs_size
376 = 4 * (cfun->machine->eh_return_data_last_regno
377 - cfun->machine->eh_return_data_first_regno
378 + 1);
379 cfun->machine->use_eh_return_p = 1;
381 else
383 /* Assigning SP_REGNUM to eh_first_regno and eh_last_regno means we
384 do not need to handle __builtin_eh_return case in this function. */
385 cfun->machine->eh_return_data_first_regno = SP_REGNUM;
386 cfun->machine->eh_return_data_last_regno = SP_REGNUM;
388 cfun->machine->eh_return_data_regs_size = 0;
389 cfun->machine->use_eh_return_p = 0;
392 /* Get variadic arguments size to prepare pretend arguments and
393 we will push them into stack at prologue by ourself. */
394 cfun->machine->va_args_size = crtl->args.pretend_args_size;
395 if (cfun->machine->va_args_size != 0)
397 cfun->machine->va_args_first_regno
398 = NDS32_GPR_ARG_FIRST_REGNUM
399 + NDS32_MAX_GPR_REGS_FOR_ARGS
400 - (crtl->args.pretend_args_size / UNITS_PER_WORD);
401 cfun->machine->va_args_last_regno
402 = NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS - 1;
404 else
406 cfun->machine->va_args_first_regno = SP_REGNUM;
407 cfun->machine->va_args_last_regno = SP_REGNUM;
410 /* Important: We need to make sure that varargs area is 8-byte alignment. */
411 block_size = cfun->machine->va_args_size;
412 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
414 cfun->machine->va_args_area_padding_bytes
415 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
418 /* Get local variables, incoming variables, and temporary variables size.
419 Note that we need to make sure it is 8-byte alignment because
420 there may be no padding bytes if we are using LRA. */
421 cfun->machine->local_size = NDS32_ROUND_UP_DOUBLE_WORD (get_frame_size ());
423 /* Get outgoing arguments size. */
424 cfun->machine->out_args_size = crtl->outgoing_args_size;
426 /* If $fp value is required to be saved on stack, it needs 4 bytes space.
427 Check whether $fp is ever live. */
428 cfun->machine->fp_size = (df_regs_ever_live_p (FP_REGNUM)) ? 4 : 0;
430 /* If $gp value is required to be saved on stack, it needs 4 bytes space.
431 Check whether we are using PIC code genration. */
432 cfun->machine->gp_size = (flag_pic) ? 4 : 0;
434 /* If $lp value is required to be saved on stack, it needs 4 bytes space.
435 Check whether $lp is ever live. */
436 cfun->machine->lp_size = (df_regs_ever_live_p (LP_REGNUM)) ? 4 : 0;
438 /* Initially there is no padding bytes. */
439 cfun->machine->callee_saved_area_gpr_padding_bytes = 0;
441 /* Calculate the bytes of saving callee-saved registers on stack. */
442 cfun->machine->callee_saved_gpr_regs_size = 0;
443 cfun->machine->callee_saved_first_gpr_regno = SP_REGNUM;
444 cfun->machine->callee_saved_last_gpr_regno = SP_REGNUM;
445 cfun->machine->callee_saved_fpr_regs_size = 0;
446 cfun->machine->callee_saved_first_fpr_regno = SP_REGNUM;
447 cfun->machine->callee_saved_last_fpr_regno = SP_REGNUM;
449 /* Currently, there is no need to check $r28~$r31
450 because we will save them in another way. */
451 for (r = 0; r < 28; r++)
453 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
455 /* Mark the first required callee-saved register
456 (only need to set it once).
457 If first regno == SP_REGNUM, we can tell that
458 it is the first time to be here. */
459 if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
460 cfun->machine->callee_saved_first_gpr_regno = r;
461 /* Mark the last required callee-saved register. */
462 cfun->machine->callee_saved_last_gpr_regno = r;
466 /* Recording fpu callee-saved register. */
467 if (TARGET_HARD_FLOAT)
469 for (r = NDS32_FIRST_FPR_REGNUM; r < NDS32_LAST_FPR_REGNUM; r++)
471 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
473 /* Mark the first required callee-saved register. */
474 if (cfun->machine->callee_saved_first_fpr_regno == SP_REGNUM)
476 /* Make first callee-saved number is even,
477 bacause we use doubleword access, and this way
478 promise 8-byte alignemt. */
479 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (r))
480 cfun->machine->callee_saved_first_fpr_regno = r - 1;
481 else
482 cfun->machine->callee_saved_first_fpr_regno = r;
484 cfun->machine->callee_saved_last_fpr_regno = r;
488 /* Make last callee-saved register number is odd,
489 we hope callee-saved register is even. */
490 int last_fpr = cfun->machine->callee_saved_last_fpr_regno;
491 if (NDS32_FPR_REGNO_OK_FOR_DOUBLE (last_fpr))
492 cfun->machine->callee_saved_last_fpr_regno++;
495 /* Check if this function can omit prologue/epilogue code fragment.
496 If there is 'naked' attribute in this function,
497 we can set 'naked_p' flag to indicate that
498 we do not have to generate prologue/epilogue.
499 Or, if all the following conditions succeed,
500 we can set this function 'naked_p' as well:
501 condition 1: first_regno == last_regno == SP_REGNUM,
502 which means we do not have to save
503 any callee-saved registers.
504 condition 2: Both $lp and $fp are NOT live in this function,
505 which means we do not need to save them and there
506 is no outgoing size.
507 condition 3: There is no local_size, which means
508 we do not need to adjust $sp. */
509 if (lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl))
510 || (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM
511 && cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM
512 && cfun->machine->callee_saved_first_fpr_regno == SP_REGNUM
513 && cfun->machine->callee_saved_last_fpr_regno == SP_REGNUM
514 && !df_regs_ever_live_p (FP_REGNUM)
515 && !df_regs_ever_live_p (LP_REGNUM)
516 && cfun->machine->local_size == 0))
518 /* Set this function 'naked_p' and other functions can check this flag.
519 Note that in nds32 port, the 'naked_p = 1' JUST means there is no
520 callee-saved, local size, and outgoing size.
521 The varargs space and ret instruction may still present in
522 the prologue/epilogue expanding. */
523 cfun->machine->naked_p = 1;
525 /* No need to save $fp, $gp, and $lp.
526 We should set these value to be zero
527 so that nds32_initial_elimination_offset() can work properly. */
528 cfun->machine->fp_size = 0;
529 cfun->machine->gp_size = 0;
530 cfun->machine->lp_size = 0;
532 /* If stack usage computation is required,
533 we need to provide the static stack size. */
534 if (flag_stack_usage_info)
535 current_function_static_stack_size = 0;
537 /* No need to do following adjustment, return immediately. */
538 return;
541 v3pushpop_p = NDS32_V3PUSH_AVAILABLE_P;
543 /* Adjustment for v3push instructions:
544 If we are using v3push (push25/pop25) instructions,
545 we need to make sure Rb is $r6 and Re is
546 located on $r6, $r8, $r10, or $r14.
547 Some results above will be discarded and recomputed.
548 Note that it is only available under V3/V3M ISA and we
549 DO NOT setup following stuff for isr or variadic function. */
550 if (v3pushpop_p)
552 /* Recompute:
553 cfun->machine->fp_size
554 cfun->machine->gp_size
555 cfun->machine->lp_size
556 cfun->machine->callee_saved_first_gpr_regno
557 cfun->machine->callee_saved_last_gpr_regno */
559 /* For v3push instructions, $fp, $gp, and $lp are always saved. */
560 cfun->machine->fp_size = 4;
561 cfun->machine->gp_size = 4;
562 cfun->machine->lp_size = 4;
564 /* Remember to set Rb = $r6. */
565 cfun->machine->callee_saved_first_gpr_regno = 6;
567 if (cfun->machine->callee_saved_last_gpr_regno <= 6)
569 /* Re = $r6 */
570 cfun->machine->callee_saved_last_gpr_regno = 6;
572 else if (cfun->machine->callee_saved_last_gpr_regno <= 8)
574 /* Re = $r8 */
575 cfun->machine->callee_saved_last_gpr_regno = 8;
577 else if (cfun->machine->callee_saved_last_gpr_regno <= 10)
579 /* Re = $r10 */
580 cfun->machine->callee_saved_last_gpr_regno = 10;
582 else if (cfun->machine->callee_saved_last_gpr_regno <= 14)
584 /* Re = $r14 */
585 cfun->machine->callee_saved_last_gpr_regno = 14;
587 else if (cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM)
589 /* If last_regno is SP_REGNUM, which means
590 it is never changed, so set it to Re = $r6. */
591 cfun->machine->callee_saved_last_gpr_regno = 6;
593 else
595 /* The program flow should not go here. */
596 gcc_unreachable ();
600 int sp_adjust = cfun->machine->local_size
601 + cfun->machine->out_args_size
602 + cfun->machine->callee_saved_area_gpr_padding_bytes
603 + cfun->machine->callee_saved_fpr_regs_size;
605 if (!v3pushpop_p
606 && sp_adjust == 0
607 && !frame_pointer_needed)
609 block_size = cfun->machine->fp_size
610 + cfun->machine->gp_size
611 + cfun->machine->lp_size
612 + (4 * (cfun->machine->callee_saved_last_gpr_regno
613 - cfun->machine->callee_saved_first_gpr_regno
614 + 1));
616 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
618 /* $r14 is last callee save register. */
619 if (cfun->machine->callee_saved_last_gpr_regno
620 < NDS32_LAST_CALLEE_SAVE_GPR_REGNUM)
622 cfun->machine->callee_saved_last_gpr_regno++;
624 else if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
626 cfun->machine->callee_saved_first_gpr_regno
627 = NDS32_FIRST_CALLEE_SAVE_GPR_REGNUM;
628 cfun->machine->callee_saved_last_gpr_regno
629 = NDS32_FIRST_CALLEE_SAVE_GPR_REGNUM;
634 /* We have correctly set callee_saved_first_gpr_regno
635 and callee_saved_last_gpr_regno.
636 Initially, the callee_saved_gpr_regs_size is supposed to be 0.
637 As long as callee_saved_last_gpr_regno is not SP_REGNUM,
638 we can update callee_saved_gpr_regs_size with new size. */
639 if (cfun->machine->callee_saved_last_gpr_regno != SP_REGNUM)
641 /* Compute pushed size of callee-saved registers. */
642 cfun->machine->callee_saved_gpr_regs_size
643 = 4 * (cfun->machine->callee_saved_last_gpr_regno
644 - cfun->machine->callee_saved_first_gpr_regno
645 + 1);
648 if (TARGET_HARD_FLOAT)
650 /* Compute size of callee svaed floating-point registers. */
651 if (cfun->machine->callee_saved_last_fpr_regno != SP_REGNUM)
653 cfun->machine->callee_saved_fpr_regs_size
654 = 4 * (cfun->machine->callee_saved_last_fpr_regno
655 - cfun->machine->callee_saved_first_fpr_regno
656 + 1);
660 /* Important: We need to make sure that
661 (fp_size + gp_size + lp_size + callee_saved_gpr_regs_size)
662 is 8-byte alignment.
663 If it is not, calculate the padding bytes. */
664 block_size = cfun->machine->fp_size
665 + cfun->machine->gp_size
666 + cfun->machine->lp_size
667 + cfun->machine->callee_saved_gpr_regs_size;
668 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
670 cfun->machine->callee_saved_area_gpr_padding_bytes
671 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
674 /* If stack usage computation is required,
675 we need to provide the static stack size. */
676 if (flag_stack_usage_info)
678 current_function_static_stack_size
679 = NDS32_ROUND_UP_DOUBLE_WORD (block_size)
680 + cfun->machine->local_size
681 + cfun->machine->out_args_size;
685 /* Function to create a parallel rtx pattern
686 which presents stack push multiple behavior.
687 The overall concept are:
688 "push registers to memory",
689 "adjust stack pointer". */
690 static void
691 nds32_emit_stack_push_multiple (unsigned Rb, unsigned Re,
692 bool save_fp_p, bool save_gp_p, bool save_lp_p,
693 bool vaarg_p)
695 unsigned regno;
696 int extra_count;
697 int num_use_regs;
698 int par_index;
699 int offset;
701 rtx reg;
702 rtx mem;
703 rtx push_rtx;
704 rtx adjust_sp_rtx;
705 rtx parallel_insn;
706 rtx dwarf;
708 /* We need to provide a customized rtx which contains
709 necessary information for data analysis,
710 so we create a parallel rtx like this:
711 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
712 (reg:SI Rb))
713 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
714 (reg:SI Rb+1))
716 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
717 (reg:SI Re))
718 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
719 (reg:SI FP_REGNUM))
720 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
721 (reg:SI GP_REGNUM))
722 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
723 (reg:SI LP_REGNUM))
724 (set (reg:SI SP_REGNUM)
725 (plus (reg:SI SP_REGNUM) (const_int -32)))]) */
727 /* Calculate the number of registers that will be pushed. */
728 extra_count = 0;
729 if (save_fp_p)
730 extra_count++;
731 if (save_gp_p)
732 extra_count++;
733 if (save_lp_p)
734 extra_count++;
735 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
736 if (Rb == SP_REGNUM && Re == SP_REGNUM)
737 num_use_regs = extra_count;
738 else
739 num_use_regs = Re - Rb + 1 + extra_count;
741 /* In addition to used registers,
742 we need one more space for (set sp sp-x) rtx. */
743 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
744 rtvec_alloc (num_use_regs + 1));
745 par_index = 0;
747 /* Initialize offset and start to create push behavior. */
748 offset = -(num_use_regs * 4);
750 /* Create (set mem regX) from Rb, Rb+1 up to Re. */
751 for (regno = Rb; regno <= Re; regno++)
753 /* Rb and Re may be SP_REGNUM.
754 We need to break this loop immediately. */
755 if (regno == SP_REGNUM)
756 break;
758 reg = gen_rtx_REG (SImode, regno);
759 mem = gen_frame_mem (SImode, plus_constant (Pmode,
760 stack_pointer_rtx,
761 offset));
762 push_rtx = gen_rtx_SET (mem, reg);
763 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
764 RTX_FRAME_RELATED_P (push_rtx) = 1;
765 offset = offset + 4;
766 par_index++;
769 /* Create (set mem fp), (set mem gp), and (set mem lp) if necessary. */
770 if (save_fp_p)
772 reg = gen_rtx_REG (SImode, FP_REGNUM);
773 mem = gen_frame_mem (SImode, plus_constant (Pmode,
774 stack_pointer_rtx,
775 offset));
776 push_rtx = gen_rtx_SET (mem, reg);
777 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
778 RTX_FRAME_RELATED_P (push_rtx) = 1;
779 offset = offset + 4;
780 par_index++;
782 if (save_gp_p)
784 reg = gen_rtx_REG (SImode, GP_REGNUM);
785 mem = gen_frame_mem (SImode, plus_constant (Pmode,
786 stack_pointer_rtx,
787 offset));
788 push_rtx = gen_rtx_SET (mem, reg);
789 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
790 RTX_FRAME_RELATED_P (push_rtx) = 1;
791 offset = offset + 4;
792 par_index++;
794 if (save_lp_p)
796 reg = gen_rtx_REG (SImode, LP_REGNUM);
797 mem = gen_frame_mem (SImode, plus_constant (Pmode,
798 stack_pointer_rtx,
799 offset));
800 push_rtx = gen_rtx_SET (mem, reg);
801 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
802 RTX_FRAME_RELATED_P (push_rtx) = 1;
803 offset = offset + 4;
804 par_index++;
807 /* Create (set sp sp-x). */
809 /* We need to re-calculate the offset value again for adjustment. */
810 offset = -(num_use_regs * 4);
811 adjust_sp_rtx
812 = gen_rtx_SET (stack_pointer_rtx,
813 plus_constant (Pmode, stack_pointer_rtx, offset));
814 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
815 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
817 parallel_insn = emit_insn (parallel_insn);
819 /* The insn rtx 'parallel_insn' will change frame layout.
820 We need to use RTX_FRAME_RELATED_P so that GCC is able to
821 generate CFI (Call Frame Information) stuff. */
822 RTX_FRAME_RELATED_P (parallel_insn) = 1;
824 /* Don't use GCC's logic for CFI info if we are generate a push for VAARG
825 since we will not restore those register at epilogue. */
826 if (vaarg_p)
828 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
829 copy_rtx (adjust_sp_rtx), NULL_RTX);
830 REG_NOTES (parallel_insn) = dwarf;
834 /* Function to create a parallel rtx pattern
835 which presents stack pop multiple behavior.
836 The overall concept are:
837 "pop registers from memory",
838 "adjust stack pointer". */
839 static void
840 nds32_emit_stack_pop_multiple (unsigned Rb, unsigned Re,
841 bool save_fp_p, bool save_gp_p, bool save_lp_p)
843 unsigned regno;
844 int extra_count;
845 int num_use_regs;
846 int par_index;
847 int offset;
849 rtx reg;
850 rtx mem;
851 rtx pop_rtx;
852 rtx adjust_sp_rtx;
853 rtx parallel_insn;
854 rtx dwarf = NULL_RTX;
856 /* We need to provide a customized rtx which contains
857 necessary information for data analysis,
858 so we create a parallel rtx like this:
859 (parallel [(set (reg:SI Rb)
860 (mem (reg:SI SP_REGNUM)))
861 (set (reg:SI Rb+1)
862 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
864 (set (reg:SI Re)
865 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
866 (set (reg:SI FP_REGNUM)
867 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
868 (set (reg:SI GP_REGNUM)
869 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
870 (set (reg:SI LP_REGNUM)
871 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
872 (set (reg:SI SP_REGNUM)
873 (plus (reg:SI SP_REGNUM) (const_int 32)))]) */
875 /* Calculate the number of registers that will be poped. */
876 extra_count = 0;
877 if (save_fp_p)
878 extra_count++;
879 if (save_gp_p)
880 extra_count++;
881 if (save_lp_p)
882 extra_count++;
883 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
884 if (Rb == SP_REGNUM && Re == SP_REGNUM)
885 num_use_regs = extra_count;
886 else
887 num_use_regs = Re - Rb + 1 + extra_count;
889 /* In addition to used registers,
890 we need one more space for (set sp sp+x) rtx. */
891 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
892 rtvec_alloc (num_use_regs + 1));
893 par_index = 0;
895 /* Initialize offset and start to create pop behavior. */
896 offset = 0;
898 /* Create (set regX mem) from Rb, Rb+1 up to Re. */
899 for (regno = Rb; regno <= Re; regno++)
901 /* Rb and Re may be SP_REGNUM.
902 We need to break this loop immediately. */
903 if (regno == SP_REGNUM)
904 break;
906 reg = gen_rtx_REG (SImode, regno);
907 mem = gen_frame_mem (SImode, plus_constant (Pmode,
908 stack_pointer_rtx,
909 offset));
910 pop_rtx = gen_rtx_SET (reg, mem);
911 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
912 RTX_FRAME_RELATED_P (pop_rtx) = 1;
913 offset = offset + 4;
914 par_index++;
916 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
919 /* Create (set fp mem), (set gp mem), and (set lp mem) if necessary. */
920 if (save_fp_p)
922 reg = gen_rtx_REG (SImode, FP_REGNUM);
923 mem = gen_frame_mem (SImode, plus_constant (Pmode,
924 stack_pointer_rtx,
925 offset));
926 pop_rtx = gen_rtx_SET (reg, mem);
927 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
928 RTX_FRAME_RELATED_P (pop_rtx) = 1;
929 offset = offset + 4;
930 par_index++;
932 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
934 if (save_gp_p)
936 reg = gen_rtx_REG (SImode, GP_REGNUM);
937 mem = gen_frame_mem (SImode, plus_constant (Pmode,
938 stack_pointer_rtx,
939 offset));
940 pop_rtx = gen_rtx_SET (reg, mem);
941 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
942 RTX_FRAME_RELATED_P (pop_rtx) = 1;
943 offset = offset + 4;
944 par_index++;
946 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
948 if (save_lp_p)
950 reg = gen_rtx_REG (SImode, LP_REGNUM);
951 mem = gen_frame_mem (SImode, plus_constant (Pmode,
952 stack_pointer_rtx,
953 offset));
954 pop_rtx = gen_rtx_SET (reg, mem);
955 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
956 RTX_FRAME_RELATED_P (pop_rtx) = 1;
957 offset = offset + 4;
958 par_index++;
960 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
963 /* Create (set sp sp+x). */
965 /* The offset value is already in place. No need to re-calculate it. */
966 adjust_sp_rtx
967 = gen_rtx_SET (stack_pointer_rtx,
968 plus_constant (Pmode, stack_pointer_rtx, offset));
969 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
971 /* Tell gcc we adjust SP in this insn. */
972 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
974 parallel_insn = emit_insn (parallel_insn);
976 /* The insn rtx 'parallel_insn' will change frame layout.
977 We need to use RTX_FRAME_RELATED_P so that GCC is able to
978 generate CFI (Call Frame Information) stuff. */
979 RTX_FRAME_RELATED_P (parallel_insn) = 1;
981 /* Add CFI info by manual. */
982 REG_NOTES (parallel_insn) = dwarf;
985 /* Function to create a parallel rtx pattern
986 which presents stack v3push behavior.
987 The overall concept are:
988 "push registers to memory",
989 "adjust stack pointer". */
990 static void
991 nds32_emit_stack_v3push (unsigned Rb,
992 unsigned Re,
993 unsigned imm8u)
995 unsigned regno;
996 int num_use_regs;
997 int par_index;
998 int offset;
1000 rtx reg;
1001 rtx mem;
1002 rtx push_rtx;
1003 rtx adjust_sp_rtx;
1004 rtx parallel_insn;
1006 /* We need to provide a customized rtx which contains
1007 necessary information for data analysis,
1008 so we create a parallel rtx like this:
1009 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
1010 (reg:SI Rb))
1011 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
1012 (reg:SI Rb+1))
1014 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
1015 (reg:SI Re))
1016 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
1017 (reg:SI FP_REGNUM))
1018 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
1019 (reg:SI GP_REGNUM))
1020 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
1021 (reg:SI LP_REGNUM))
1022 (set (reg:SI SP_REGNUM)
1023 (plus (reg:SI SP_REGNUM) (const_int -32-imm8u)))]) */
1025 /* Calculate the number of registers that will be pushed.
1026 Since $fp, $gp, and $lp is always pushed with v3push instruction,
1027 we need to count these three registers.
1028 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1029 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1030 num_use_regs = Re - Rb + 1 + 3;
1032 /* In addition to used registers,
1033 we need one more space for (set sp sp-x-imm8u) rtx. */
1034 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
1035 rtvec_alloc (num_use_regs + 1));
1036 par_index = 0;
1038 /* Initialize offset and start to create push behavior. */
1039 offset = -(num_use_regs * 4);
1041 /* Create (set mem regX) from Rb, Rb+1 up to Re.
1042 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1043 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1044 for (regno = Rb; regno <= Re; regno++)
1046 reg = gen_rtx_REG (SImode, regno);
1047 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1048 stack_pointer_rtx,
1049 offset));
1050 push_rtx = gen_rtx_SET (mem, reg);
1051 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1052 RTX_FRAME_RELATED_P (push_rtx) = 1;
1053 offset = offset + 4;
1054 par_index++;
1057 /* Create (set mem fp). */
1058 reg = gen_rtx_REG (SImode, FP_REGNUM);
1059 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1060 stack_pointer_rtx,
1061 offset));
1062 push_rtx = gen_rtx_SET (mem, reg);
1063 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1064 RTX_FRAME_RELATED_P (push_rtx) = 1;
1065 offset = offset + 4;
1066 par_index++;
1067 /* Create (set mem gp). */
1068 reg = gen_rtx_REG (SImode, GP_REGNUM);
1069 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1070 stack_pointer_rtx,
1071 offset));
1072 push_rtx = gen_rtx_SET (mem, reg);
1073 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1074 RTX_FRAME_RELATED_P (push_rtx) = 1;
1075 offset = offset + 4;
1076 par_index++;
1077 /* Create (set mem lp). */
1078 reg = gen_rtx_REG (SImode, LP_REGNUM);
1079 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1080 stack_pointer_rtx,
1081 offset));
1082 push_rtx = gen_rtx_SET (mem, reg);
1083 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1084 RTX_FRAME_RELATED_P (push_rtx) = 1;
1085 offset = offset + 4;
1086 par_index++;
1088 /* Create (set sp sp-x-imm8u). */
1090 /* We need to re-calculate the offset value again for adjustment. */
1091 offset = -(num_use_regs * 4);
1092 adjust_sp_rtx
1093 = gen_rtx_SET (stack_pointer_rtx,
1094 plus_constant (Pmode,
1095 stack_pointer_rtx,
1096 offset - imm8u));
1097 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
1098 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
1100 parallel_insn = emit_insn (parallel_insn);
1102 /* The insn rtx 'parallel_insn' will change frame layout.
1103 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1104 generate CFI (Call Frame Information) stuff. */
1105 RTX_FRAME_RELATED_P (parallel_insn) = 1;
1108 /* Function to create a parallel rtx pattern
1109 which presents stack v3pop behavior.
1110 The overall concept are:
1111 "pop registers from memory",
1112 "adjust stack pointer". */
1113 static void
1114 nds32_emit_stack_v3pop (unsigned Rb,
1115 unsigned Re,
1116 unsigned imm8u)
1118 unsigned regno;
1119 int num_use_regs;
1120 int par_index;
1121 int offset;
1123 rtx reg;
1124 rtx mem;
1125 rtx pop_rtx;
1126 rtx adjust_sp_rtx;
1127 rtx parallel_insn;
1128 rtx dwarf = NULL_RTX;
1130 /* We need to provide a customized rtx which contains
1131 necessary information for data analysis,
1132 so we create a parallel rtx like this:
1133 (parallel [(set (reg:SI Rb)
1134 (mem (reg:SI SP_REGNUM)))
1135 (set (reg:SI Rb+1)
1136 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
1138 (set (reg:SI Re)
1139 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
1140 (set (reg:SI FP_REGNUM)
1141 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
1142 (set (reg:SI GP_REGNUM)
1143 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
1144 (set (reg:SI LP_REGNUM)
1145 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
1146 (set (reg:SI SP_REGNUM)
1147 (plus (reg:SI SP_REGNUM) (const_int 32+imm8u)))]) */
1149 /* Calculate the number of registers that will be poped.
1150 Since $fp, $gp, and $lp is always poped with v3pop instruction,
1151 we need to count these three registers.
1152 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1153 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1154 num_use_regs = Re - Rb + 1 + 3;
1156 /* In addition to used registers,
1157 we need one more space for (set sp sp+x+imm8u) rtx. */
1158 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
1159 rtvec_alloc (num_use_regs + 1));
1160 par_index = 0;
1162 /* Initialize offset and start to create pop behavior. */
1163 offset = 0;
1165 /* Create (set regX mem) from Rb, Rb+1 up to Re.
1166 Under v3pop, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1167 So there is no need to worry about Rb=Re=SP_REGNUM case. */
1168 for (regno = Rb; regno <= Re; regno++)
1170 reg = gen_rtx_REG (SImode, regno);
1171 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1172 stack_pointer_rtx,
1173 offset));
1174 pop_rtx = gen_rtx_SET (reg, mem);
1175 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1176 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1177 offset = offset + 4;
1178 par_index++;
1180 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1183 /* Create (set fp mem). */
1184 reg = gen_rtx_REG (SImode, FP_REGNUM);
1185 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1186 stack_pointer_rtx,
1187 offset));
1188 pop_rtx = gen_rtx_SET (reg, mem);
1189 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1190 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1191 offset = offset + 4;
1192 par_index++;
1193 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1195 /* Create (set gp mem). */
1196 reg = gen_rtx_REG (SImode, GP_REGNUM);
1197 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1198 stack_pointer_rtx,
1199 offset));
1200 pop_rtx = gen_rtx_SET (reg, mem);
1201 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1202 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1203 offset = offset + 4;
1204 par_index++;
1205 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1207 /* Create (set lp mem ). */
1208 reg = gen_rtx_REG (SImode, LP_REGNUM);
1209 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1210 stack_pointer_rtx,
1211 offset));
1212 pop_rtx = gen_rtx_SET (reg, mem);
1213 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1214 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1215 offset = offset + 4;
1216 par_index++;
1217 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1219 /* Create (set sp sp+x+imm8u). */
1221 /* The offset value is already in place. No need to re-calculate it. */
1222 adjust_sp_rtx
1223 = gen_rtx_SET (stack_pointer_rtx,
1224 plus_constant (Pmode,
1225 stack_pointer_rtx,
1226 offset + imm8u));
1227 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
1229 if (frame_pointer_needed)
1231 /* (expr_list:REG_CFA_DEF_CFA (plus:SI (reg/f:SI $sp)
1232 (const_int 0))
1233 mean reset frame pointer to $sp and reset to offset 0. */
1234 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1235 const0_rtx);
1236 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
1238 else
1240 /* Tell gcc we adjust SP in this insn. */
1241 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
1242 copy_rtx (adjust_sp_rtx), dwarf);
1245 parallel_insn = emit_insn (parallel_insn);
1247 /* The insn rtx 'parallel_insn' will change frame layout.
1248 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1249 generate CFI (Call Frame Information) stuff. */
1250 RTX_FRAME_RELATED_P (parallel_insn) = 1;
1252 /* Add CFI info by manual. */
1253 REG_NOTES (parallel_insn) = dwarf;
1256 /* Function that may creates more instructions
1257 for large value on adjusting stack pointer.
1259 In nds32 target, 'addi' can be used for stack pointer
1260 adjustment in prologue/epilogue stage.
1261 However, sometimes there are too many local variables so that
1262 the adjustment value is not able to be fit in the 'addi' instruction.
1263 One solution is to move value into a register
1264 and then use 'add' instruction.
1265 In practice, we use TA_REGNUM ($r15) to accomplish this purpose. */
1266 static void
1267 nds32_emit_adjust_frame (rtx to_reg, rtx from_reg, int adjust_value)
1269 rtx tmp_reg;
1270 rtx frame_adjust_insn;
1271 rtx adjust_value_rtx = GEN_INT (adjust_value);
1273 if (adjust_value == 0)
1274 return;
1276 if (!satisfies_constraint_Is15 (adjust_value_rtx))
1278 /* The value is not able to fit in single addi instruction.
1279 Create more instructions of moving value into a register
1280 and then add stack pointer with it. */
1282 /* $r15 is going to be temporary register to hold the value. */
1283 tmp_reg = gen_rtx_REG (SImode, TA_REGNUM);
1285 /* Create one more instruction to move value
1286 into the temporary register. */
1287 emit_move_insn (tmp_reg, adjust_value_rtx);
1289 /* Create new 'add' rtx. */
1290 frame_adjust_insn = gen_addsi3 (to_reg,
1291 from_reg,
1292 tmp_reg);
1293 /* Emit rtx into insn list and receive its transformed insn rtx. */
1294 frame_adjust_insn = emit_insn (frame_adjust_insn);
1296 /* Because (tmp_reg <- full_value) may be split into two
1297 rtl patterns, we can not set its RTX_FRAME_RELATED_P.
1298 We need to construct another (sp <- sp + full_value)
1299 and then insert it into sp_adjust_insn's reg note to
1300 represent a frame related expression.
1301 GCC knows how to refer it and output debug information. */
1303 rtx plus_rtx;
1304 rtx set_rtx;
1306 plus_rtx = plus_constant (Pmode, from_reg, adjust_value);
1307 set_rtx = gen_rtx_SET (to_reg, plus_rtx);
1308 add_reg_note (frame_adjust_insn, REG_FRAME_RELATED_EXPR, set_rtx);
1310 else
1312 /* Generate sp adjustment instruction if and only if sp_adjust != 0. */
1313 frame_adjust_insn = gen_addsi3 (to_reg,
1314 from_reg,
1315 adjust_value_rtx);
1316 /* Emit rtx into instructions list and receive INSN rtx form. */
1317 frame_adjust_insn = emit_insn (frame_adjust_insn);
1320 /* The insn rtx 'sp_adjust_insn' will change frame layout.
1321 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1322 generate CFI (Call Frame Information) stuff. */
1323 RTX_FRAME_RELATED_P (frame_adjust_insn) = 1;
1326 /* Return true if MODE/TYPE need double word alignment. */
1327 static bool
1328 nds32_needs_double_word_align (machine_mode mode, const_tree type)
1330 unsigned int align;
1332 /* Pick up the alignment according to the mode or type. */
1333 align = NDS32_MODE_TYPE_ALIGN (mode, type);
1335 return (align > PARM_BOUNDARY);
1338 /* Return true if FUNC is a naked function. */
1339 static bool
1340 nds32_naked_function_p (tree func)
1342 tree t;
1344 if (TREE_CODE (func) != FUNCTION_DECL)
1345 abort ();
1347 t = lookup_attribute ("naked", DECL_ATTRIBUTES (func));
1349 return (t != NULL_TREE);
1352 /* Function that check if 'X' is a valid address register.
1353 The variable 'STRICT' is very important to
1354 make decision for register number.
1356 STRICT : true
1357 => We are in reload pass or after reload pass.
1358 The register number should be strictly limited in general registers.
1360 STRICT : false
1361 => Before reload pass, we are free to use any register number. */
1362 static bool
1363 nds32_address_register_rtx_p (rtx x, bool strict)
1365 int regno;
1367 if (GET_CODE (x) != REG)
1368 return false;
1370 regno = REGNO (x);
1372 if (strict)
1373 return REGNO_OK_FOR_BASE_P (regno);
1374 else
1375 return true;
1378 /* Function that check if 'INDEX' is valid to be a index rtx for address.
1380 OUTER_MODE : Machine mode of outer address rtx.
1381 INDEX : Check if this rtx is valid to be a index for address.
1382 STRICT : If it is true, we are in reload pass or after reload pass. */
1383 static bool
1384 nds32_legitimate_index_p (machine_mode outer_mode,
1385 rtx index,
1386 bool strict)
1388 int regno;
1389 rtx op0;
1390 rtx op1;
1392 switch (GET_CODE (index))
1394 case REG:
1395 regno = REGNO (index);
1396 /* If we are in reload pass or after reload pass,
1397 we need to limit it to general register. */
1398 if (strict)
1399 return REGNO_OK_FOR_INDEX_P (regno);
1400 else
1401 return true;
1403 case CONST_INT:
1404 /* The alignment of the integer value is determined by 'outer_mode'. */
1405 switch (GET_MODE_SIZE (outer_mode))
1407 case 1:
1408 /* Further check if the value is legal for the 'outer_mode'. */
1409 if (satisfies_constraint_Is15 (index))
1410 return true;
1411 break;
1413 case 2:
1414 /* Further check if the value is legal for the 'outer_mode'. */
1415 if (satisfies_constraint_Is16 (index))
1417 /* Make sure address is half word alignment. */
1418 if (NDS32_HALF_WORD_ALIGN_P (INTVAL (index)))
1419 return true;
1421 break;
1423 case 4:
1424 /* Further check if the value is legal for the 'outer_mode'. */
1425 if (satisfies_constraint_Is17 (index))
1427 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
1429 if (!satisfies_constraint_Is14 (index))
1430 return false;
1433 /* Make sure address is word alignment. */
1434 if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1435 return true;
1437 break;
1439 case 8:
1440 if (satisfies_constraint_Is17 (gen_int_mode (INTVAL (index) + 4,
1441 SImode)))
1443 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
1445 if (!satisfies_constraint_Is14 (index))
1446 return false;
1449 /* Make sure address is word alignment.
1450 Currently we do not have 64-bit load/store yet,
1451 so we will use two 32-bit load/store instructions to do
1452 memory access and they are single word alignment. */
1453 if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1454 return true;
1456 break;
1458 default:
1459 return false;
1462 return false;
1464 case MULT:
1465 op0 = XEXP (index, 0);
1466 op1 = XEXP (index, 1);
1468 if (REG_P (op0) && CONST_INT_P (op1))
1470 int multiplier;
1471 multiplier = INTVAL (op1);
1473 /* We only allow (mult reg const_int_1), (mult reg const_int_2),
1474 (mult reg const_int_4) or (mult reg const_int_8). */
1475 if (multiplier != 1 && multiplier != 2
1476 && multiplier != 4 && multiplier != 8)
1477 return false;
1479 regno = REGNO (op0);
1480 /* Limit it in general registers if we are
1481 in reload pass or after reload pass. */
1482 if(strict)
1483 return REGNO_OK_FOR_INDEX_P (regno);
1484 else
1485 return true;
1488 return false;
1490 case ASHIFT:
1491 op0 = XEXP (index, 0);
1492 op1 = XEXP (index, 1);
1494 if (REG_P (op0) && CONST_INT_P (op1))
1496 int sv;
1497 /* op1 is already the sv value for use to do left shift. */
1498 sv = INTVAL (op1);
1500 /* We only allow (ashift reg const_int_0)
1501 or (ashift reg const_int_1) or (ashift reg const_int_2) or
1502 (ashift reg const_int_3). */
1503 if (sv != 0 && sv != 1 && sv !=2 && sv != 3)
1504 return false;
1506 regno = REGNO (op0);
1507 /* Limit it in general registers if we are
1508 in reload pass or after reload pass. */
1509 if(strict)
1510 return REGNO_OK_FOR_INDEX_P (regno);
1511 else
1512 return true;
1515 return false;
1517 default:
1518 return false;
1522 static void
1523 nds32_register_pass (
1524 rtl_opt_pass *(*make_pass_func) (gcc::context *),
1525 enum pass_positioning_ops pass_pos,
1526 const char *ref_pass_name)
1528 opt_pass *new_opt_pass = make_pass_func (g);
1530 struct register_pass_info insert_pass =
1532 new_opt_pass, /* pass */
1533 ref_pass_name, /* reference_pass_name */
1534 1, /* ref_pass_instance_number */
1535 pass_pos /* po_op */
1538 register_pass (&insert_pass);
1541 /* This function is called from nds32_option_override ().
1542 All new passes should be registered here. */
1543 static void
1544 nds32_register_passes (void)
1546 nds32_register_pass (
1547 make_pass_nds32_relax_opt,
1548 PASS_POS_INSERT_AFTER,
1549 "mach");
1552 /* ------------------------------------------------------------------------ */
1554 /* PART 3: Implement target hook stuff definitions. */
1557 /* Computing the Length of an Insn.
1558 Modifies the length assigned to instruction INSN.
1559 LEN is the initially computed length of the insn. */
1561 nds32_adjust_insn_length (rtx_insn *insn, int length)
1563 int adjust_value = 0;
1564 switch (recog_memoized (insn))
1566 case CODE_FOR_call_internal:
1567 case CODE_FOR_call_value_internal:
1569 if (NDS32_ALIGN_P ())
1571 rtx_insn *next_insn = next_active_insn (insn);
1572 if (next_insn && get_attr_length (next_insn) != 2)
1573 adjust_value += 2;
1575 /* We need insert a nop after a noretun function call
1576 to prevent software breakpoint corrupt the next function. */
1577 if (find_reg_note (insn, REG_NORETURN, NULL_RTX))
1579 if (TARGET_16_BIT)
1580 adjust_value += 2;
1581 else
1582 adjust_value += 4;
1585 return length + adjust_value;
1587 default:
1588 return length;
1592 /* Register Usage. */
1594 static void
1595 nds32_conditional_register_usage (void)
1597 int regno;
1599 if (TARGET_HARD_FLOAT)
1601 for (regno = NDS32_FIRST_FPR_REGNUM;
1602 regno <= NDS32_LAST_FPR_REGNUM; regno++)
1604 fixed_regs[regno] = 0;
1605 if (regno < NDS32_FIRST_FPR_REGNUM + NDS32_MAX_FPR_REGS_FOR_ARGS)
1606 call_used_regs[regno] = 1;
1607 else if (regno >= NDS32_FIRST_FPR_REGNUM + 22
1608 && regno < NDS32_FIRST_FPR_REGNUM + 48)
1609 call_used_regs[regno] = 1;
1610 else
1611 call_used_regs[regno] = 0;
1614 else if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
1616 for (regno = NDS32_FIRST_FPR_REGNUM;
1617 regno <= NDS32_LAST_FPR_REGNUM;
1618 regno++)
1619 fixed_regs[regno] = 0;
1624 /* Register Classes. */
1626 static unsigned char
1627 nds32_class_max_nregs (reg_class_t rclass ATTRIBUTE_UNUSED,
1628 machine_mode mode)
1630 /* Return the maximum number of consecutive registers
1631 needed to represent "mode" in a register of "rclass". */
1632 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1635 static int
1636 nds32_register_priority (int hard_regno)
1638 /* Encourage to use r0-r7 for LRA when optimize for size. */
1639 if (optimize_size)
1641 if (hard_regno < 8)
1642 return 4;
1643 else if (hard_regno < 16)
1644 return 3;
1645 else if (hard_regno < 28)
1646 return 2;
1647 else
1648 return 1;
1650 else
1652 if (hard_regno > 27)
1653 return 1;
1654 else
1655 return 4;
1659 static bool
1660 nds32_can_change_mode_class (machine_mode from,
1661 machine_mode to,
1662 reg_class_t rclass)
1664 /* Don't spill double-precision register to two singal-precision
1665 registers */
1666 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
1667 && GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
1669 return !reg_classes_intersect_p (rclass, FP_REGS);
1672 return true;
1676 /* Stack Layout and Calling Conventions. */
1678 /* There are three kinds of pointer concepts using in GCC compiler:
1680 frame pointer: A pointer to the first location of local variables.
1681 stack pointer: A pointer to the top of a stack frame.
1682 argument pointer: A pointer to the incoming arguments.
1684 In nds32 target calling convention, we are using 8-byte alignment.
1685 Besides, we would like to have each stack frame of a function includes:
1687 [Block A]
1688 1. previous hard frame pointer
1689 2. return address
1690 3. callee-saved registers
1691 4. <padding bytes> (we will calculte in nds32_compute_stack_frame()
1692 and save it at
1693 cfun->machine->callee_saved_area_padding_bytes)
1695 [Block B]
1696 1. local variables
1697 2. spilling location
1698 3. <padding bytes> (it will be calculated by GCC itself)
1699 4. incoming arguments
1700 5. <padding bytes> (it will be calculated by GCC itself)
1702 [Block C]
1703 1. <padding bytes> (it will be calculated by GCC itself)
1704 2. outgoing arguments
1706 We 'wrap' these blocks together with
1707 hard frame pointer ($r28) and stack pointer ($r31).
1708 By applying the basic frame/stack/argument pointers concept,
1709 the layout of a stack frame shoule be like this:
1712 old stack pointer -> ----
1713 | | \
1714 | | saved arguments for
1715 | | vararg functions
1716 | | /
1717 hard frame pointer -> --
1718 & argument pointer | | \
1719 | | previous hardware frame pointer
1720 | | return address
1721 | | callee-saved registers
1722 | | /
1723 frame pointer -> --
1724 | | \
1725 | | local variables
1726 | | and incoming arguments
1727 | | /
1729 | | \
1730 | | outgoing
1731 | | arguments
1732 | | /
1733 stack pointer -> ----
1735 $SFP and $AP are used to represent frame pointer and arguments pointer,
1736 which will be both eliminated as hard frame pointer. */
1738 /* -- Eliminating Frame Pointer and Arg Pointer. */
1740 static bool
1741 nds32_can_eliminate (const int from_reg, const int to_reg)
1743 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1744 return true;
1746 if (from_reg == ARG_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1747 return true;
1749 if (from_reg == FRAME_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1750 return true;
1752 if (from_reg == FRAME_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1753 return true;
1755 return false;
1758 /* -- Passing Arguments in Registers. */
1760 static rtx
1761 nds32_function_arg (cumulative_args_t ca, machine_mode mode,
1762 const_tree type, bool named)
1764 unsigned int regno;
1765 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1767 /* The last time this hook is called,
1768 it is called with MODE == VOIDmode. */
1769 if (mode == VOIDmode)
1770 return NULL_RTX;
1772 /* For nameless arguments, we need to take care it individually. */
1773 if (!named)
1775 /* If we are under hard float abi, we have arguments passed on the
1776 stack and all situation can be handled by GCC itself. */
1777 if (TARGET_HARD_FLOAT)
1778 return NULL_RTX;
1780 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1782 /* If we still have enough registers to pass argument, pick up
1783 next available register number. */
1784 regno
1785 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1786 return gen_rtx_REG (mode, regno);
1789 /* No register available, return NULL_RTX.
1790 The compiler will use stack to pass argument instead. */
1791 return NULL_RTX;
1794 /* The following is to handle named argument.
1795 Note that the strategies of TARGET_HARD_FLOAT and !TARGET_HARD_FLOAT
1796 are different. */
1797 if (TARGET_HARD_FLOAT)
1799 /* For TARGET_HARD_FLOAT calling convention, we use GPR and FPR
1800 to pass argument. We have to further check TYPE and MODE so
1801 that we can determine which kind of register we shall use. */
1803 /* Note that we need to pass argument entirely in registers under
1804 hard float abi. */
1805 if (GET_MODE_CLASS (mode) == MODE_FLOAT
1806 && NDS32_ARG_ENTIRE_IN_FPR_REG_P (cum->fpr_offset, mode, type))
1808 /* Pick up the next available FPR register number. */
1809 regno
1810 = NDS32_AVAILABLE_REGNUM_FOR_FPR_ARG (cum->fpr_offset, mode, type);
1811 return gen_rtx_REG (mode, regno);
1813 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
1814 && NDS32_ARG_ENTIRE_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1816 /* Pick up the next available GPR register number. */
1817 regno
1818 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1819 return gen_rtx_REG (mode, regno);
1822 else
1824 /* For !TARGET_HARD_FLOAT calling convention, we always use GPR to pass
1825 argument. Since we allow to pass argument partially in registers,
1826 we can just return it if there are still registers available. */
1827 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1829 /* Pick up the next available register number. */
1830 regno
1831 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1832 return gen_rtx_REG (mode, regno);
1837 /* No register available, return NULL_RTX.
1838 The compiler will use stack to pass argument instead. */
1839 return NULL_RTX;
1842 static bool
1843 nds32_must_pass_in_stack (machine_mode mode, const_tree type)
1845 /* Return true if a type must be passed in memory.
1846 If it is NOT using hard float abi, small aggregates can be
1847 passed in a register even we are calling a variadic function.
1848 So there is no need to take padding into consideration. */
1849 if (TARGET_HARD_FLOAT)
1850 return must_pass_in_stack_var_size_or_pad (mode, type);
1851 else
1852 return must_pass_in_stack_var_size (mode, type);
1855 static int
1856 nds32_arg_partial_bytes (cumulative_args_t ca, machine_mode mode,
1857 tree type, bool named ATTRIBUTE_UNUSED)
1859 /* Returns the number of bytes at the beginning of an argument that
1860 must be put in registers. The value must be zero for arguments that are
1861 passed entirely in registers or that are entirely pushed on the stack.
1862 Besides, TARGET_FUNCTION_ARG for these arguments should return the
1863 first register to be used by the caller for this argument. */
1864 unsigned int needed_reg_count;
1865 unsigned int remaining_reg_count;
1866 CUMULATIVE_ARGS *cum;
1868 cum = get_cumulative_args (ca);
1870 /* Under hard float abi, we better have argument entirely passed in
1871 registers or pushed on the stack so that we can reduce the complexity
1872 of dealing with cum->gpr_offset and cum->fpr_offset. */
1873 if (TARGET_HARD_FLOAT)
1874 return 0;
1876 /* If we have already runned out of argument registers, return zero
1877 so that the argument will be entirely pushed on the stack. */
1878 if (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1879 >= NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS)
1880 return 0;
1882 /* Calculate how many registers do we need for this argument. */
1883 needed_reg_count = NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1885 /* Calculate how many argument registers have left for passing argument.
1886 Note that we should count it from next available register number. */
1887 remaining_reg_count
1888 = NDS32_MAX_GPR_REGS_FOR_ARGS
1889 - (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1890 - NDS32_GPR_ARG_FIRST_REGNUM);
1892 /* Note that we have to return the nubmer of bytes, not registers count. */
1893 if (needed_reg_count > remaining_reg_count)
1894 return remaining_reg_count * UNITS_PER_WORD;
1896 return 0;
1899 static void
1900 nds32_function_arg_advance (cumulative_args_t ca, machine_mode mode,
1901 const_tree type, bool named)
1903 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1905 if (named)
1907 /* We need to further check TYPE and MODE so that we can determine
1908 which kind of register we shall advance. */
1910 /* Under hard float abi, we may advance FPR registers. */
1911 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
1913 cum->fpr_offset
1914 = NDS32_AVAILABLE_REGNUM_FOR_FPR_ARG (cum->fpr_offset, mode, type)
1915 - NDS32_FPR_ARG_FIRST_REGNUM
1916 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1918 else
1920 cum->gpr_offset
1921 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1922 - NDS32_GPR_ARG_FIRST_REGNUM
1923 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1926 else
1928 /* If this nameless argument is NOT under TARGET_HARD_FLOAT,
1929 we can advance next register as well so that caller is
1930 able to pass arguments in registers and callee must be
1931 in charge of pushing all of them into stack. */
1932 if (!TARGET_HARD_FLOAT)
1934 cum->gpr_offset
1935 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1936 - NDS32_GPR_ARG_FIRST_REGNUM
1937 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1942 static unsigned int
1943 nds32_function_arg_boundary (machine_mode mode, const_tree type)
1945 return (nds32_needs_double_word_align (mode, type)
1946 ? NDS32_DOUBLE_WORD_ALIGNMENT
1947 : PARM_BOUNDARY);
1950 /* -- How Scalar Function Values Are Returned. */
1952 static rtx
1953 nds32_function_value (const_tree ret_type,
1954 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1955 bool outgoing ATTRIBUTE_UNUSED)
1957 machine_mode mode;
1958 int unsignedp;
1960 mode = TYPE_MODE (ret_type);
1961 unsignedp = TYPE_UNSIGNED (ret_type);
1963 if (INTEGRAL_TYPE_P (ret_type))
1964 mode = promote_mode (ret_type, mode, &unsignedp);
1966 if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
1967 return gen_rtx_REG (mode, NDS32_FPR_RET_FIRST_REGNUM);
1968 else
1969 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
1972 static rtx
1973 nds32_libcall_value (machine_mode mode,
1974 const_rtx fun ATTRIBUTE_UNUSED)
1976 if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
1977 return gen_rtx_REG (mode, NDS32_FPR_RET_FIRST_REGNUM);
1979 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
1982 static bool
1983 nds32_function_value_regno_p (const unsigned int regno)
1985 if (regno == NDS32_GPR_RET_FIRST_REGNUM
1986 || (TARGET_HARD_FLOAT
1987 && regno == NDS32_FPR_RET_FIRST_REGNUM))
1988 return true;
1990 return false;
1993 /* -- How Large Values Are Returned. */
1995 static bool
1996 nds32_return_in_memory (const_tree type,
1997 const_tree fntype ATTRIBUTE_UNUSED)
1999 /* Note that int_size_in_bytes can return -1 if the size can vary
2000 or is larger than an integer. */
2001 HOST_WIDE_INT size = int_size_in_bytes (type);
2003 /* For COMPLEX_TYPE, if the total size cannot be hold within two registers,
2004 the return value is supposed to be in memory. We need to be aware of
2005 that the size may be -1. */
2006 if (TREE_CODE (type) == COMPLEX_TYPE)
2007 if (size < 0 || size > 2 * UNITS_PER_WORD)
2008 return true;
2010 /* If it is BLKmode and the total size cannot be hold within two registers,
2011 the return value is supposed to be in memory. We need to be aware of
2012 that the size may be -1. */
2013 if (TYPE_MODE (type) == BLKmode)
2014 if (size < 0 || size > 2 * UNITS_PER_WORD)
2015 return true;
2017 /* For other cases, having result in memory is unnecessary. */
2018 return false;
2021 /* -- Function Entry and Exit. */
2023 /* The content produced from this function
2024 will be placed before prologue body. */
2025 static void
2026 nds32_asm_function_prologue (FILE *file)
2028 int r;
2029 const char *func_name;
2030 tree attrs;
2031 tree name;
2033 /* All stack frame information is supposed to be
2034 already computed when expanding prologue.
2035 The result is in cfun->machine.
2036 DO NOT call nds32_compute_stack_frame() here
2037 because it may corrupt the essential information. */
2039 fprintf (file, "\t! BEGIN PROLOGUE\n");
2040 fprintf (file, "\t! fp needed: %d\n", frame_pointer_needed);
2041 fprintf (file, "\t! pretend_args: %d\n", cfun->machine->va_args_size);
2042 fprintf (file, "\t! local_size: %d\n", cfun->machine->local_size);
2043 fprintf (file, "\t! out_args_size: %d\n", cfun->machine->out_args_size);
2045 /* Use df_regs_ever_live_p() to detect if the register
2046 is ever used in the current function. */
2047 fprintf (file, "\t! registers ever_live: ");
2048 for (r = 0; r < 65; r++)
2050 if (df_regs_ever_live_p (r))
2051 fprintf (file, "%s, ", reg_names[r]);
2053 fputc ('\n', file);
2055 /* Display the attributes of this function. */
2056 fprintf (file, "\t! function attributes: ");
2057 /* Get the attributes tree list.
2058 Note that GCC builds attributes list with reverse order. */
2059 attrs = DECL_ATTRIBUTES (current_function_decl);
2061 /* If there is no any attribute, print out "None". */
2062 if (!attrs)
2063 fprintf (file, "None");
2065 /* If there are some attributes, try if we need to
2066 construct isr vector information. */
2067 func_name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2068 nds32_construct_isr_vectors_information (attrs, func_name);
2070 /* Display all attributes of this function. */
2071 while (attrs)
2073 name = TREE_PURPOSE (attrs);
2074 fprintf (file, "%s ", IDENTIFIER_POINTER (name));
2076 /* Pick up the next attribute. */
2077 attrs = TREE_CHAIN (attrs);
2079 fputc ('\n', file);
2082 /* After rtl prologue has been expanded, this function is used. */
2083 static void
2084 nds32_asm_function_end_prologue (FILE *file)
2086 fprintf (file, "\t! END PROLOGUE\n");
2088 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
2089 we can generate special directive: ".omit_fp_begin"
2090 to guide linker doing fp-as-gp optimization.
2091 However, for a naked function, which means
2092 it should not have prologue/epilogue,
2093 using fp-as-gp still requires saving $fp by push/pop behavior and
2094 there is no benefit to use fp-as-gp on such small function.
2095 So we need to make sure this function is NOT naked as well. */
2096 if (!frame_pointer_needed
2097 && !cfun->machine->naked_p
2098 && cfun->machine->fp_as_gp_p)
2100 fprintf (file, "\t! ----------------------------------------\n");
2101 fprintf (file, "\t! Guide linker to do "
2102 "link time optimization: fp-as-gp\n");
2103 fprintf (file, "\t! We add one more instruction to "
2104 "initialize $fp near to $gp location.\n");
2105 fprintf (file, "\t! If linker fails to use fp-as-gp transformation,\n");
2106 fprintf (file, "\t! this extra instruction should be "
2107 "eliminated at link stage.\n");
2108 fprintf (file, "\t.omit_fp_begin\n");
2109 fprintf (file, "\tla\t$fp,_FP_BASE_\n");
2110 fprintf (file, "\t! ----------------------------------------\n");
2114 /* Before rtl epilogue has been expanded, this function is used. */
2115 static void
2116 nds32_asm_function_begin_epilogue (FILE *file)
2118 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
2119 we can generate special directive: ".omit_fp_end"
2120 to claim fp-as-gp optimization range.
2121 However, for a naked function,
2122 which means it should not have prologue/epilogue,
2123 using fp-as-gp still requires saving $fp by push/pop behavior and
2124 there is no benefit to use fp-as-gp on such small function.
2125 So we need to make sure this function is NOT naked as well. */
2126 if (!frame_pointer_needed
2127 && !cfun->machine->naked_p
2128 && cfun->machine->fp_as_gp_p)
2130 fprintf (file, "\t! ----------------------------------------\n");
2131 fprintf (file, "\t! Claim the range of fp-as-gp "
2132 "link time optimization\n");
2133 fprintf (file, "\t.omit_fp_end\n");
2134 fprintf (file, "\t! ----------------------------------------\n");
2137 fprintf (file, "\t! BEGIN EPILOGUE\n");
2140 /* The content produced from this function
2141 will be placed after epilogue body. */
2142 static void
2143 nds32_asm_function_epilogue (FILE *file)
2145 fprintf (file, "\t! END EPILOGUE\n");
2148 static void
2149 nds32_asm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
2150 HOST_WIDE_INT delta,
2151 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2152 tree function)
2154 int this_regno;
2156 /* Make sure unwind info is emitted for the thunk if needed. */
2157 final_start_function (emit_barrier (), file, 1);
2159 this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
2161 : 0);
2163 if (delta != 0)
2165 if (satisfies_constraint_Is15 (GEN_INT (delta)))
2167 fprintf (file, "\taddi\t$r%d, $r%d, " HOST_WIDE_INT_PRINT_DEC "\n",
2168 this_regno, this_regno, delta);
2170 else if (satisfies_constraint_Is20 (GEN_INT (delta)))
2172 fprintf (file, "\tmovi\t$ta, " HOST_WIDE_INT_PRINT_DEC "\n", delta);
2173 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
2175 else
2177 fprintf (file,
2178 "\tsethi\t$ta, hi20(" HOST_WIDE_INT_PRINT_DEC ")\n",
2179 delta);
2180 fprintf (file,
2181 "\tori\t$ta, $ta, lo12(" HOST_WIDE_INT_PRINT_DEC ")\n",
2182 delta);
2183 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
2187 fprintf (file, "\tb\t");
2188 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2189 fprintf (file, "\n");
2191 final_end_function ();
2194 /* -- Permitting tail calls. */
2196 /* Return true if it is ok to do sibling call optimization. */
2197 static bool
2198 nds32_function_ok_for_sibcall (tree decl,
2199 tree exp ATTRIBUTE_UNUSED)
2201 /* The DECL is NULL if it is an indirect call. */
2203 /* 1. Do not apply sibling call if -mv3push is enabled,
2204 because pop25 instruction also represents return behavior.
2205 2. If this function is a variadic function, do not apply sibling call
2206 because the stack layout may be a mess.
2207 3. We don't want to apply sibling call optimization for indirect
2208 sibcall because the pop behavior in epilogue may pollute the
2209 content of caller-saved regsiter when the register is used for
2210 indirect sibcall. */
2211 return (!TARGET_V3PUSH
2212 && (cfun->machine->va_args_size == 0)
2213 && decl);
2216 /* Determine whether we need to enable warning for function return check. */
2217 static bool
2218 nds32_warn_func_return (tree decl)
2220 /* Naked functions are implemented entirely in assembly, including the
2221 return sequence, so suppress warnings about this. */
2222 return !nds32_naked_function_p (decl);
2226 /* Implementing the Varargs Macros. */
2228 static void
2229 nds32_setup_incoming_varargs (cumulative_args_t ca,
2230 machine_mode mode,
2231 tree type,
2232 int *pretend_args_size,
2233 int second_time ATTRIBUTE_UNUSED)
2235 unsigned int total_args_regs;
2236 unsigned int num_of_used_regs;
2237 unsigned int remaining_reg_count;
2238 CUMULATIVE_ARGS *cum;
2240 /* If we are under hard float abi, we do not need to set *pretend_args_size.
2241 So that all nameless arguments are pushed by caller and all situation
2242 can be handled by GCC itself. */
2243 if (TARGET_HARD_FLOAT)
2244 return;
2246 /* We are using NDS32_MAX_GPR_REGS_FOR_ARGS registers,
2247 counting from NDS32_GPR_ARG_FIRST_REGNUM, for saving incoming arguments.
2248 However, for nameless(anonymous) arguments, we should push them on the
2249 stack so that all the nameless arguments appear to have been passed
2250 consecutively in the memory for accessing. Hence, we need to check and
2251 exclude the registers that are used for named arguments. */
2253 cum = get_cumulative_args (ca);
2255 /* The MODE and TYPE describe the last argument.
2256 We need those information to determine the remaining registers
2257 for varargs. */
2258 total_args_regs
2259 = NDS32_MAX_GPR_REGS_FOR_ARGS + NDS32_GPR_ARG_FIRST_REGNUM;
2260 num_of_used_regs
2261 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
2262 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
2264 remaining_reg_count = total_args_regs - num_of_used_regs;
2265 *pretend_args_size = remaining_reg_count * UNITS_PER_WORD;
2267 return;
2270 static bool
2271 nds32_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
2273 /* If this hook returns true, the named argument of FUNCTION_ARG is always
2274 true for named arguments, and false for unnamed arguments. */
2275 return true;
2279 /* Trampolines for Nested Functions. */
2281 static void
2282 nds32_asm_trampoline_template (FILE *f)
2284 if (TARGET_REDUCED_REGS)
2286 /* Trampoline is not supported on reduced-set registers yet. */
2287 sorry ("a nested function is not supported for reduced registers");
2289 else
2291 asm_fprintf (f, "\t! Trampoline code template\n");
2292 asm_fprintf (f, "\t! This code fragment will be copied "
2293 "into stack on demand\n");
2295 asm_fprintf (f, "\tmfusr\t$r16,$pc\n");
2296 asm_fprintf (f, "\tlwi\t$r15,[$r16 + 20] "
2297 "! load nested function address\n");
2298 asm_fprintf (f, "\tlwi\t$r16,[$r16 + 16] "
2299 "! load chain_value\n");
2300 asm_fprintf (f, "\tjr\t$r15\n");
2303 /* Preserve space ($pc + 16) for saving chain_value,
2304 nds32_trampoline_init will fill the value in this slot. */
2305 asm_fprintf (f, "\t! space for saving chain_value\n");
2306 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2308 /* Preserve space ($pc + 20) for saving nested function address,
2309 nds32_trampoline_init will fill the value in this slot. */
2310 asm_fprintf (f, "\t! space for saving nested function address\n");
2311 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2314 /* Emit RTL insns to initialize the variable parts of a trampoline. */
2315 static void
2316 nds32_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2318 int i;
2320 /* Nested function address. */
2321 rtx fnaddr;
2322 /* The memory rtx that is going to
2323 be filled with chain_value. */
2324 rtx chain_value_mem;
2325 /* The memory rtx that is going to
2326 be filled with nested function address. */
2327 rtx nested_func_mem;
2329 /* Start address of trampoline code in stack, for doing cache sync. */
2330 rtx sync_cache_addr;
2331 /* Temporary register for sync instruction. */
2332 rtx tmp_reg;
2333 /* Instruction-cache sync instruction,
2334 requesting an argument as starting address. */
2335 rtx isync_insn;
2336 /* For convenience reason of doing comparison. */
2337 int tramp_align_in_bytes;
2339 /* Trampoline is not supported on reduced-set registers yet. */
2340 if (TARGET_REDUCED_REGS)
2341 sorry ("a nested function is not supported for reduced registers");
2343 /* STEP 1: Copy trampoline code template into stack,
2344 fill up essential data into stack. */
2346 /* Extract nested function address rtx. */
2347 fnaddr = XEXP (DECL_RTL (fndecl), 0);
2349 /* m_tramp is memory rtx that is going to be filled with trampoline code.
2350 We have nds32_asm_trampoline_template() to emit template pattern. */
2351 emit_block_move (m_tramp, assemble_trampoline_template (),
2352 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2354 /* After copying trampoline code into stack,
2355 fill chain_value into stack. */
2356 chain_value_mem = adjust_address (m_tramp, SImode, 16);
2357 emit_move_insn (chain_value_mem, chain_value);
2358 /* After copying trampoline code int stack,
2359 fill nested function address into stack. */
2360 nested_func_mem = adjust_address (m_tramp, SImode, 20);
2361 emit_move_insn (nested_func_mem, fnaddr);
2363 /* STEP 2: Sync instruction-cache. */
2365 /* We have successfully filled trampoline code into stack.
2366 However, in order to execute code in stack correctly,
2367 we must sync instruction cache. */
2368 sync_cache_addr = XEXP (m_tramp, 0);
2369 tmp_reg = gen_reg_rtx (SImode);
2370 isync_insn = gen_unspec_volatile_isync (tmp_reg);
2372 /* Because nds32_cache_block_size is in bytes,
2373 we get trampoline alignment in bytes for convenient comparison. */
2374 tramp_align_in_bytes = TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT;
2376 if (tramp_align_in_bytes >= nds32_cache_block_size
2377 && (tramp_align_in_bytes % nds32_cache_block_size) == 0)
2379 /* Under this condition, the starting address of trampoline
2380 must be aligned to the starting address of each cache block
2381 and we do not have to worry about cross-boundary issue. */
2382 for (i = 0;
2383 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2384 / nds32_cache_block_size;
2385 i++)
2387 emit_move_insn (tmp_reg,
2388 plus_constant (Pmode, sync_cache_addr,
2389 nds32_cache_block_size * i));
2390 emit_insn (isync_insn);
2393 else if (TRAMPOLINE_SIZE > nds32_cache_block_size)
2395 /* The starting address of trampoline code
2396 may not be aligned to the cache block,
2397 so the trampoline code may be across two cache block.
2398 We need to sync the last element, which is 4-byte size,
2399 of trampoline template. */
2400 for (i = 0;
2401 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2402 / nds32_cache_block_size;
2403 i++)
2405 emit_move_insn (tmp_reg,
2406 plus_constant (Pmode, sync_cache_addr,
2407 nds32_cache_block_size * i));
2408 emit_insn (isync_insn);
2411 /* The last element of trampoline template is 4-byte size. */
2412 emit_move_insn (tmp_reg,
2413 plus_constant (Pmode, sync_cache_addr,
2414 TRAMPOLINE_SIZE - 4));
2415 emit_insn (isync_insn);
2417 else
2419 /* This is the simplest case.
2420 Because TRAMPOLINE_SIZE is less than or
2421 equal to nds32_cache_block_size,
2422 we can just sync start address and
2423 the last element of trampoline code. */
2425 /* Sync starting address of tampoline code. */
2426 emit_move_insn (tmp_reg, sync_cache_addr);
2427 emit_insn (isync_insn);
2428 /* Sync the last element, which is 4-byte size,
2429 of trampoline template. */
2430 emit_move_insn (tmp_reg,
2431 plus_constant (Pmode, sync_cache_addr,
2432 TRAMPOLINE_SIZE - 4));
2433 emit_insn (isync_insn);
2436 /* Set instruction serialization barrier
2437 to guarantee the correct operations. */
2438 emit_insn (gen_unspec_volatile_isb ());
2442 /* Addressing Modes. */
2444 static bool
2445 nds32_legitimate_address_p (machine_mode mode, rtx x, bool strict)
2447 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
2449 /* When using floating-point instructions,
2450 we don't allow 'addr' to be [symbol_ref], [CONST] pattern. */
2451 if ((mode == DFmode || mode == SFmode)
2452 && (GET_CODE (x) == SYMBOL_REF
2453 || GET_CODE(x) == CONST))
2454 return false;
2456 /* Allow [post_modify] addressing mode, when using FPU instructions. */
2457 if (GET_CODE (x) == POST_MODIFY
2458 && mode == DFmode)
2460 if (GET_CODE (XEXP (x, 0)) == REG
2461 && GET_CODE (XEXP (x, 1)) == PLUS)
2463 rtx plus_op = XEXP (x, 1);
2464 rtx op0 = XEXP (plus_op, 0);
2465 rtx op1 = XEXP (plus_op, 1);
2467 if (nds32_address_register_rtx_p (op0, strict)
2468 && CONST_INT_P (op1))
2470 if (satisfies_constraint_Is14 (op1))
2472 /* Make sure address is word alignment.
2473 Currently we do not have 64-bit load/store yet,
2474 so we will use two 32-bit load/store instructions to do
2475 memory access and they are single word alignment. */
2476 if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (op1)))
2477 return true;
2484 /* For (mem:DI addr) or (mem:DF addr) case,
2485 we only allow 'addr' to be [reg], [symbol_ref],
2486 [const], or [reg + const_int] pattern. */
2487 if (mode == DImode || mode == DFmode)
2489 /* Allow [Reg + const_int] addressing mode. */
2490 if (GET_CODE (x) == PLUS)
2492 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2493 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict)
2494 && CONST_INT_P (XEXP (x, 1)))
2495 return true;
2496 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2497 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict)
2498 && CONST_INT_P (XEXP (x, 0)))
2499 return true;
2502 /* Allow [post_inc] and [post_dec] addressing mode. */
2503 if (GET_CODE (x) == POST_INC || GET_CODE (x) == POST_DEC)
2505 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2506 return true;
2509 /* Now check [reg], [symbol_ref], and [const]. */
2510 if (GET_CODE (x) != REG
2511 && GET_CODE (x) != SYMBOL_REF
2512 && GET_CODE (x) != CONST)
2513 return false;
2516 /* Check if 'x' is a valid address. */
2517 switch (GET_CODE (x))
2519 case REG:
2520 /* (mem (reg A)) => [Ra] */
2521 return nds32_address_register_rtx_p (x, strict);
2523 case SYMBOL_REF:
2524 /* (mem (symbol_ref A)) => [symbol_ref] */
2525 /* If -mcmodel=large, the 'symbol_ref' is not a valid address
2526 during or after LRA/reload phase. */
2527 if (TARGET_CMODEL_LARGE
2528 && (reload_completed
2529 || reload_in_progress
2530 || lra_in_progress))
2531 return false;
2532 /* If -mcmodel=medium and the symbol references to rodata section,
2533 the 'symbol_ref' is not a valid address during or after
2534 LRA/reload phase. */
2535 if (TARGET_CMODEL_MEDIUM
2536 && NDS32_SYMBOL_REF_RODATA_P (x)
2537 && (reload_completed
2538 || reload_in_progress
2539 || lra_in_progress))
2540 return false;
2542 return true;
2544 case CONST:
2545 /* (mem (const (...)))
2546 => [ + const_addr ], where const_addr = symbol_ref + const_int */
2547 if (GET_CODE (XEXP (x, 0)) == PLUS)
2549 rtx plus_op = XEXP (x, 0);
2551 rtx op0 = XEXP (plus_op, 0);
2552 rtx op1 = XEXP (plus_op, 1);
2554 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
2556 /* Now we see the [ + const_addr ] pattern, but we need
2557 some further checking. */
2558 /* If -mcmodel=large, the 'const_addr' 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 'const_addr' is not a valid address during or after
2567 LRA/reload phase. */
2568 if (TARGET_CMODEL_MEDIUM
2569 && NDS32_SYMBOL_REF_RODATA_P (op0)
2570 && (reload_completed
2571 || reload_in_progress
2572 || lra_in_progress))
2573 return false;
2575 /* At this point we can make sure 'const_addr' is a
2576 valid address. */
2577 return true;
2581 return false;
2583 case POST_MODIFY:
2584 /* (mem (post_modify (reg) (plus (reg) (reg))))
2585 => [Ra], Rb */
2586 /* (mem (post_modify (reg) (plus (reg) (const_int))))
2587 => [Ra], const_int */
2588 if (GET_CODE (XEXP (x, 0)) == REG
2589 && GET_CODE (XEXP (x, 1)) == PLUS)
2591 rtx plus_op = XEXP (x, 1);
2593 rtx op0 = XEXP (plus_op, 0);
2594 rtx op1 = XEXP (plus_op, 1);
2596 if (nds32_address_register_rtx_p (op0, strict)
2597 && nds32_legitimate_index_p (mode, op1, strict))
2598 return true;
2599 else
2600 return false;
2603 return false;
2605 case POST_INC:
2606 case POST_DEC:
2607 /* (mem (post_inc reg)) => [Ra], 1/2/4 */
2608 /* (mem (post_dec reg)) => [Ra], -1/-2/-4 */
2609 /* The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
2610 We only need to deal with register Ra. */
2611 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2612 return true;
2613 else
2614 return false;
2616 case PLUS:
2617 /* (mem (plus reg const_int))
2618 => [Ra + imm] */
2619 /* (mem (plus reg reg))
2620 => [Ra + Rb] */
2621 /* (mem (plus (mult reg const_int) reg))
2622 => [Ra + Rb << sv] */
2623 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2624 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict))
2625 return true;
2626 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2627 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict))
2628 return true;
2629 else
2630 return false;
2632 case LO_SUM:
2633 /* (mem (lo_sum (reg) (symbol_ref))) */
2634 /* (mem (lo_sum (reg) (const))) */
2635 gcc_assert (REG_P (XEXP (x, 0)));
2636 if (GET_CODE (XEXP (x, 1)) == SYMBOL_REF
2637 || GET_CODE (XEXP (x, 1)) == CONST)
2638 return nds32_legitimate_address_p (mode, XEXP (x, 1), strict);
2639 else
2640 return false;
2642 default:
2643 return false;
2648 /* Condition Code Status. */
2650 /* -- Representation of condition codes using registers. */
2652 static void
2653 nds32_canonicalize_comparison (int *code,
2654 rtx *op0 ATTRIBUTE_UNUSED,
2655 rtx *op1,
2656 bool op0_preserve_value ATTRIBUTE_UNUSED)
2658 /* When the instruction combination pass tries to combine a comparison insn
2659 with its previous insns, it also transforms the operator in order to
2660 minimize its constant field. For example, it tries to transform a
2661 comparison insn from
2662 (set (reg:SI 54)
2663 (ltu:SI (reg:SI 52)
2664 (const_int 10 [0xa])))
2666 (set (reg:SI 54)
2667 (leu:SI (reg:SI 52)
2668 (const_int 9 [0x9])))
2670 However, the nds32 target only provides instructions supporting the LTU
2671 operation directly, and the implementation of the pattern "cbranchsi4"
2672 only expands the LTU form. In order to handle the non-LTU operations
2673 generated from passes other than the RTL expansion pass, we have to
2674 implement this hook to revert those changes. Since we only expand the LTU
2675 operator in the RTL expansion pass, we might only need to handle the LEU
2676 case, unless we find other optimization passes perform more aggressive
2677 transformations. */
2679 if (*code == LEU && CONST_INT_P (*op1))
2681 *op1 = gen_int_mode (INTVAL (*op1) + 1, SImode);
2682 *code = LTU;
2687 /* Describing Relative Costs of Operations. */
2689 static int
2690 nds32_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2691 reg_class_t from,
2692 reg_class_t to)
2694 if ((from == FP_REGS && to != FP_REGS)
2695 || (from != FP_REGS && to == FP_REGS))
2696 return 9;
2697 else if (from == HIGH_REGS || to == HIGH_REGS)
2698 return optimize_size ? 6 : 2;
2699 else
2700 return 2;
2703 static int
2704 nds32_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2705 reg_class_t rclass ATTRIBUTE_UNUSED,
2706 bool in ATTRIBUTE_UNUSED)
2708 return 8;
2711 /* This target hook describes the relative costs of RTL expressions.
2712 Return 'true' when all subexpressions of x have been processed.
2713 Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
2714 Refer to gcc/rtlanal.c for more information. */
2715 static bool
2716 nds32_rtx_costs (rtx x,
2717 machine_mode mode,
2718 int outer_code,
2719 int opno,
2720 int *total,
2721 bool speed)
2723 return nds32_rtx_costs_impl (x, mode, outer_code, opno, total, speed);
2726 static int
2727 nds32_address_cost (rtx address,
2728 machine_mode mode,
2729 addr_space_t as,
2730 bool speed)
2732 return nds32_address_cost_impl (address, mode, as, speed);
2736 /* Dividing the Output into Sections (Texts, Data, . . . ). */
2738 /* If references to a symbol or a constant must be treated differently
2739 depending on something about the variable or function named by the symbol
2740 (such as what section it is in), we use this hook to store flags
2741 in symbol_ref rtx. */
2742 static void
2743 nds32_encode_section_info (tree decl, rtx rtl, int new_decl_p)
2745 default_encode_section_info (decl, rtl, new_decl_p);
2747 /* For the memory rtx, if it references to rodata section, we can store
2748 NDS32_SYMBOL_FLAG_RODATA flag into symbol_ref rtx so that the
2749 nds32_legitimate_address_p() can determine how to treat such symbol_ref
2750 based on -mcmodel=X and this information. */
2751 if (MEM_P (rtl) && MEM_READONLY_P (rtl))
2753 rtx addr = XEXP (rtl, 0);
2755 if (GET_CODE (addr) == SYMBOL_REF)
2757 /* For (mem (symbol_ref X)) case. */
2758 SYMBOL_REF_FLAGS (addr) |= NDS32_SYMBOL_FLAG_RODATA;
2760 else if (GET_CODE (addr) == CONST
2761 && GET_CODE (XEXP (addr, 0)) == PLUS)
2763 /* For (mem (const (plus (symbol_ref X) (const_int N)))) case. */
2764 rtx plus_op = XEXP (addr, 0);
2765 rtx op0 = XEXP (plus_op, 0);
2766 rtx op1 = XEXP (plus_op, 1);
2768 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
2769 SYMBOL_REF_FLAGS (op0) |= NDS32_SYMBOL_FLAG_RODATA;
2775 /* Defining the Output Assembler Language. */
2777 /* -- The Overall Framework of an Assembler File. */
2779 static void
2780 nds32_asm_file_start (void)
2782 default_file_start ();
2784 /* Tell assembler which ABI we are using. */
2785 fprintf (asm_out_file, "\t! ABI version\n");
2786 if (TARGET_HARD_FLOAT)
2787 fprintf (asm_out_file, "\t.abi_2fp_plus\n");
2788 else
2789 fprintf (asm_out_file, "\t.abi_2\n");
2791 /* Tell assembler that this asm code is generated by compiler. */
2792 fprintf (asm_out_file, "\t! This asm file is generated by compiler\n");
2793 fprintf (asm_out_file, "\t.flag\tverbatim\n");
2794 /* Give assembler the size of each vector for interrupt handler. */
2795 fprintf (asm_out_file, "\t! This vector size directive is required "
2796 "for checking inconsistency on interrupt handler\n");
2797 fprintf (asm_out_file, "\t.vec_size\t%d\n", nds32_isr_vector_size);
2799 fprintf (asm_out_file, "\t! ------------------------------------\n");
2801 if (TARGET_ISA_V2)
2802 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V2");
2803 if (TARGET_ISA_V3)
2804 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3");
2805 if (TARGET_ISA_V3M)
2806 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3M");
2808 if (TARGET_CMODEL_SMALL)
2809 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "SMALL");
2810 if (TARGET_CMODEL_MEDIUM)
2811 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "MEDIUM");
2812 if (TARGET_CMODEL_LARGE)
2813 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "LARGE");
2815 fprintf (asm_out_file, "\t! Endian setting\t: %s\n",
2816 ((TARGET_BIG_ENDIAN) ? "big-endian"
2817 : "little-endian"));
2818 fprintf (asm_out_file, "\t! Use SP floating-point instruction\t: %s\n",
2819 ((TARGET_FPU_SINGLE) ? "Yes"
2820 : "No"));
2821 fprintf (asm_out_file, "\t! Use DP floating-point instruction\t: %s\n",
2822 ((TARGET_FPU_DOUBLE) ? "Yes"
2823 : "No"));
2824 fprintf (asm_out_file, "\t! ABI version\t\t: %s\n",
2825 ((TARGET_HARD_FLOAT) ? "ABI2FP+"
2826 : "ABI2"));
2828 fprintf (asm_out_file, "\t! ------------------------------------\n");
2830 fprintf (asm_out_file, "\t! Use conditional move\t\t: %s\n",
2831 ((TARGET_CMOV) ? "Yes"
2832 : "No"));
2833 fprintf (asm_out_file, "\t! Use performance extension\t: %s\n",
2834 ((TARGET_EXT_PERF) ? "Yes"
2835 : "No"));
2836 fprintf (asm_out_file, "\t! Use performance extension 2\t: %s\n",
2837 ((TARGET_EXT_PERF2) ? "Yes"
2838 : "No"));
2839 fprintf (asm_out_file, "\t! Use string extension\t\t: %s\n",
2840 ((TARGET_EXT_STRING) ? "Yes"
2841 : "No"));
2843 fprintf (asm_out_file, "\t! ------------------------------------\n");
2845 fprintf (asm_out_file, "\t! V3PUSH instructions\t: %s\n",
2846 ((TARGET_V3PUSH) ? "Yes"
2847 : "No"));
2848 fprintf (asm_out_file, "\t! 16-bit instructions\t: %s\n",
2849 ((TARGET_16_BIT) ? "Yes"
2850 : "No"));
2851 fprintf (asm_out_file, "\t! Reduced registers set\t: %s\n",
2852 ((TARGET_REDUCED_REGS) ? "Yes"
2853 : "No"));
2855 fprintf (asm_out_file, "\t! ------------------------------------\n");
2857 if (optimize_size)
2858 fprintf (asm_out_file, "\t! Optimization level\t: -Os\n");
2859 else
2860 fprintf (asm_out_file, "\t! Optimization level\t: -O%d\n", optimize);
2862 fprintf (asm_out_file, "\t! ------------------------------------\n");
2864 fprintf (asm_out_file, "\t! Cache block size\t: %d\n",
2865 nds32_cache_block_size);
2867 fprintf (asm_out_file, "\t! ------------------------------------\n");
2869 nds32_asm_file_start_for_isr ();
2872 static void
2873 nds32_asm_file_end (void)
2875 nds32_asm_file_end_for_isr ();
2877 fprintf (asm_out_file, "\t! ------------------------------------\n");
2880 /* -- Output and Generation of Labels. */
2882 static void
2883 nds32_asm_globalize_label (FILE *stream, const char *name)
2885 fputs ("\t.global\t", stream);
2886 assemble_name (stream, name);
2887 fputs ("\n", stream);
2890 /* -- Output of Assembler Instructions. */
2892 static void
2893 nds32_print_operand (FILE *stream, rtx x, int code)
2895 HOST_WIDE_INT one_position;
2896 HOST_WIDE_INT zero_position;
2897 bool pick_lsb_p = false;
2898 bool pick_msb_p = false;
2899 int regno;
2901 int op_value;
2903 switch (code)
2905 case 0 :
2906 /* Do nothing special. */
2907 break;
2909 case 'b':
2910 /* Use exact_log2() to search the 0-bit position. */
2911 gcc_assert (CONST_INT_P (x));
2912 zero_position = exact_log2 (~UINTVAL (x) & GET_MODE_MASK (SImode));
2913 gcc_assert (zero_position != -1);
2914 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, zero_position);
2916 /* No need to handle following process, so return immediately. */
2917 return;
2919 case 'e':
2920 gcc_assert (MEM_P (x)
2921 && GET_CODE (XEXP (x, 0)) == PLUS
2922 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
2923 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (XEXP (x, 0), 1)));
2925 /* No need to handle following process, so return immediately. */
2926 return;
2927 case 'B':
2928 /* Use exact_log2() to search the 1-bit position. */
2929 gcc_assert (CONST_INT_P (x));
2930 one_position = exact_log2 (UINTVAL (x) & GET_MODE_MASK (SImode));
2931 gcc_assert (one_position != -1);
2932 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, one_position);
2934 /* No need to handle following process, so return immediately. */
2935 return;
2937 case 'L':
2938 /* X is supposed to be REG rtx. */
2939 gcc_assert (REG_P (x));
2940 /* Claim that we are going to pick LSB part of X. */
2941 pick_lsb_p = true;
2942 break;
2944 case 'H':
2945 /* X is supposed to be REG rtx. */
2946 gcc_assert (REG_P (x));
2947 /* Claim that we are going to pick MSB part of X. */
2948 pick_msb_p = true;
2949 break;
2951 case 'V':
2952 /* 'x' is supposed to be CONST_INT, get the value. */
2953 gcc_assert (CONST_INT_P (x));
2954 op_value = INTVAL (x);
2956 /* According to the Andes architecture,
2957 the system/user register index range is 0 ~ 1023.
2958 In order to avoid conflict between user-specified-integer value
2959 and enum-specified-register value,
2960 the 'enum nds32_intrinsic_registers' value
2961 in nds32_intrinsic.h starts from 1024. */
2962 if (op_value < 1024 && op_value >= 0)
2964 /* If user gives integer value directly (0~1023),
2965 we just print out the value. */
2966 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, op_value);
2968 else if (op_value < 0
2969 || op_value >= ((int) ARRAY_SIZE (nds32_intrinsic_register_names)
2970 + 1024))
2972 /* The enum index value for array size is out of range. */
2973 error ("intrinsic register index is out of range");
2975 else
2977 /* If user applies normal way with __NDS32_REG_XXX__ enum data,
2978 we can print out register name. Remember to substract 1024. */
2979 fprintf (stream, "%s",
2980 nds32_intrinsic_register_names[op_value - 1024]);
2983 /* No need to handle following process, so return immediately. */
2984 return;
2986 case 'R': /* cctl valck */
2987 /* Note the cctl divide to 5 group and share the same name table. */
2988 if (op_value < 0 || op_value > 4)
2989 error ("CCTL intrinsic function subtype out of range!");
2990 fprintf (stream, "%s", nds32_cctl_names[op_value]);
2991 return;
2993 case 'T': /* cctl idxwbinv */
2994 /* Note the cctl divide to 5 group and share the same name table. */
2995 if (op_value < 0 || op_value > 4)
2996 error ("CCTL intrinsic function subtype out of range!");
2997 fprintf (stream, "%s", nds32_cctl_names[op_value + 4]);
2998 return;
3000 case 'U': /* cctl vawbinv */
3001 /* Note the cctl divide to 5 group and share the same name table. */
3002 if (op_value < 0 || op_value > 4)
3003 error ("CCTL intrinsic function subtype out of range!");
3004 fprintf (stream, "%s", nds32_cctl_names[op_value + 8]);
3005 return;
3007 case 'X': /* cctl idxread */
3008 /* Note the cctl divide to 5 group and share the same name table. */
3009 if (op_value < 0 || op_value > 4)
3010 error ("CCTL intrinsic function subtype out of range!");
3011 fprintf (stream, "%s", nds32_cctl_names[op_value + 12]);
3012 return;
3014 case 'W': /* cctl idxwitre */
3015 /* Note the cctl divide to 5 group and share the same name table. */
3016 if (op_value < 0 || op_value > 4)
3017 error ("CCTL intrinsic function subtype out of range!");
3018 fprintf (stream, "%s", nds32_cctl_names[op_value + 16]);
3019 return;
3021 case 'Z': /* dpref */
3022 fprintf (stream, "%s", nds32_dpref_names[op_value]);
3023 return;
3025 default :
3026 /* Unknown flag. */
3027 output_operand_lossage ("invalid operand output code");
3028 break;
3031 switch (GET_CODE (x))
3033 case LABEL_REF:
3034 case SYMBOL_REF:
3035 output_addr_const (stream, x);
3036 break;
3038 case REG:
3039 /* Print a Double-precision register name. */
3040 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3041 && NDS32_IS_FPR_REGNUM (REGNO (x)))
3043 regno = REGNO (x);
3044 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno))
3046 output_operand_lossage ("invalid operand for code '%c'", code);
3047 break;
3049 fprintf (stream, "$fd%d", (regno - NDS32_FIRST_FPR_REGNUM) >> 1);
3050 break;
3053 /* Print LSB or MSB part of register pair if the
3054 constraint modifier 'L' or 'H' is specified. */
3055 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3056 && NDS32_IS_GPR_REGNUM (REGNO (x)))
3058 if ((pick_lsb_p && WORDS_BIG_ENDIAN)
3059 || (pick_msb_p && !WORDS_BIG_ENDIAN))
3061 /* If we would like to print out LSB register under big-endian,
3062 or print out MSB register under little-endian, we need to
3063 increase register number. */
3064 regno = REGNO (x);
3065 regno++;
3066 fputs (reg_names[regno], stream);
3067 break;
3071 /* Forbid using static chain register ($r16)
3072 on reduced-set registers configuration. */
3073 if (TARGET_REDUCED_REGS
3074 && REGNO (x) == STATIC_CHAIN_REGNUM)
3075 sorry ("a nested function is not supported for reduced registers");
3077 /* Normal cases, print out register name. */
3078 fputs (reg_names[REGNO (x)], stream);
3079 break;
3081 case MEM:
3082 output_address (GET_MODE (x), XEXP (x, 0));
3083 break;
3085 case HIGH:
3086 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE)
3088 const REAL_VALUE_TYPE *rv;
3089 long val;
3090 gcc_assert (GET_MODE (x) == SFmode);
3092 rv = CONST_DOUBLE_REAL_VALUE (XEXP (x, 0));
3093 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3095 fprintf (stream, "hi20(0x%lx)", val);
3097 else
3098 gcc_unreachable ();
3099 break;
3101 case CONST_DOUBLE:
3102 const REAL_VALUE_TYPE *rv;
3103 long val;
3104 gcc_assert (GET_MODE (x) == SFmode);
3106 rv = CONST_DOUBLE_REAL_VALUE (x);
3107 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3109 fprintf (stream, "0x%lx", val);
3110 break;
3112 case CODE_LABEL:
3113 case CONST_INT:
3114 case CONST:
3115 output_addr_const (stream, x);
3116 break;
3118 default:
3119 /* Generally, output_addr_const () is able to handle most cases.
3120 We want to see what CODE could appear,
3121 so we use gcc_unreachable() to stop it. */
3122 debug_rtx (x);
3123 gcc_unreachable ();
3124 break;
3128 static void
3129 nds32_print_operand_address (FILE *stream, machine_mode /*mode*/, rtx x)
3131 rtx op0, op1;
3133 switch (GET_CODE (x))
3135 case SYMBOL_REF:
3136 case CONST:
3137 /* [ + symbol_ref] */
3138 /* [ + const_addr], where const_addr = symbol_ref + const_int */
3139 fputs ("[ + ", stream);
3140 output_addr_const (stream, x);
3141 fputs ("]", stream);
3142 break;
3144 case REG:
3145 /* Forbid using static chain register ($r16)
3146 on reduced-set registers configuration. */
3147 if (TARGET_REDUCED_REGS
3148 && REGNO (x) == STATIC_CHAIN_REGNUM)
3149 sorry ("a nested function is not supported for reduced registers");
3151 /* [Ra] */
3152 fprintf (stream, "[%s]", reg_names[REGNO (x)]);
3153 break;
3155 case PLUS:
3156 op0 = XEXP (x, 0);
3157 op1 = XEXP (x, 1);
3159 /* Checking op0, forbid using static chain register ($r16)
3160 on reduced-set registers configuration. */
3161 if (TARGET_REDUCED_REGS
3162 && REG_P (op0)
3163 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3164 sorry ("a nested function is not supported for reduced registers");
3165 /* Checking op1, forbid using static chain register ($r16)
3166 on reduced-set registers configuration. */
3167 if (TARGET_REDUCED_REGS
3168 && REG_P (op1)
3169 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3170 sorry ("a nested function is not supported for reduced registers");
3172 if (REG_P (op0) && CONST_INT_P (op1))
3174 /* [Ra + imm] */
3175 fprintf (stream, "[%s + (" HOST_WIDE_INT_PRINT_DEC ")]",
3176 reg_names[REGNO (op0)], INTVAL (op1));
3178 else if (REG_P (op0) && REG_P (op1))
3180 /* [Ra + Rb] */
3181 fprintf (stream, "[%s + %s]",
3182 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3184 else if (GET_CODE (op0) == MULT && REG_P (op1))
3186 /* [Ra + Rb << sv]
3187 From observation, the pattern looks like:
3188 (plus:SI (mult:SI (reg:SI 58)
3189 (const_int 4 [0x4]))
3190 (reg/f:SI 57)) */
3191 int sv;
3193 /* We need to set sv to output shift value. */
3194 if (INTVAL (XEXP (op0, 1)) == 1)
3195 sv = 0;
3196 else if (INTVAL (XEXP (op0, 1)) == 2)
3197 sv = 1;
3198 else if (INTVAL (XEXP (op0, 1)) == 4)
3199 sv = 2;
3200 else if (INTVAL (XEXP (op0, 1)) == 8)
3201 sv = 3;
3202 else
3203 gcc_unreachable ();
3205 fprintf (stream, "[%s + %s << %d]",
3206 reg_names[REGNO (op1)],
3207 reg_names[REGNO (XEXP (op0, 0))],
3208 sv);
3210 else
3212 /* The control flow is not supposed to be here. */
3213 debug_rtx (x);
3214 gcc_unreachable ();
3217 break;
3219 case POST_MODIFY:
3220 /* (post_modify (regA) (plus (regA) (regB)))
3221 (post_modify (regA) (plus (regA) (const_int)))
3222 We would like to extract
3223 regA and regB (or const_int) from plus rtx. */
3224 op0 = XEXP (XEXP (x, 1), 0);
3225 op1 = XEXP (XEXP (x, 1), 1);
3227 /* Checking op0, forbid using static chain register ($r16)
3228 on reduced-set registers configuration. */
3229 if (TARGET_REDUCED_REGS
3230 && REG_P (op0)
3231 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3232 sorry ("a nested function is not supported for reduced registers");
3233 /* Checking op1, forbid using static chain register ($r16)
3234 on reduced-set registers configuration. */
3235 if (TARGET_REDUCED_REGS
3236 && REG_P (op1)
3237 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3238 sorry ("a nested function is not supported for reduced registers");
3240 if (REG_P (op0) && REG_P (op1))
3242 /* [Ra], Rb */
3243 fprintf (stream, "[%s], %s",
3244 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3246 else if (REG_P (op0) && CONST_INT_P (op1))
3248 /* [Ra], imm */
3249 fprintf (stream, "[%s], " HOST_WIDE_INT_PRINT_DEC,
3250 reg_names[REGNO (op0)], INTVAL (op1));
3252 else
3254 /* The control flow is not supposed to be here. */
3255 debug_rtx (x);
3256 gcc_unreachable ();
3259 break;
3261 case POST_INC:
3262 case POST_DEC:
3263 op0 = XEXP (x, 0);
3265 /* Checking op0, forbid using static chain register ($r16)
3266 on reduced-set registers configuration. */
3267 if (TARGET_REDUCED_REGS
3268 && REG_P (op0)
3269 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3270 sorry ("a nested function is not supported for reduced registers");
3272 if (REG_P (op0))
3274 /* "[Ra], 1/2/4" or "[Ra], -1/-2/-4"
3275 The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
3276 We only need to deal with register Ra. */
3277 fprintf (stream, "[%s]", reg_names[REGNO (op0)]);
3279 else
3281 /* The control flow is not supposed to be here. */
3282 debug_rtx (x);
3283 gcc_unreachable ();
3286 break;
3288 default :
3289 /* Generally, output_addr_const () is able to handle most cases.
3290 We want to see what CODE could appear,
3291 so we use gcc_unreachable() to stop it. */
3292 debug_rtx (x);
3293 gcc_unreachable ();
3294 break;
3298 /* -- Assembler Commands for Exception Regions. */
3300 static rtx
3301 nds32_dwarf_register_span (rtx reg)
3303 rtx dwarf_high, dwarf_low;
3304 rtx dwarf_single;
3305 machine_mode mode;
3306 int regno;
3308 mode = GET_MODE (reg);
3309 regno = REGNO (reg);
3311 /* We need to adjust dwarf register information for floating-point registers
3312 rather than using default register number mapping. */
3313 if (regno >= NDS32_FIRST_FPR_REGNUM
3314 && regno <= NDS32_LAST_FPR_REGNUM)
3316 if (mode == DFmode || mode == SCmode)
3318 /* By default, GCC maps increasing register numbers to increasing
3319 memory locations, but paired FPRs in NDS32 target are always
3320 big-endian, i.e.:
3322 fd0 : fs0 fs1
3323 (MSB) (LSB)
3325 We must return parallel rtx to represent such layout. */
3326 dwarf_high = gen_rtx_REG (word_mode, regno);
3327 dwarf_low = gen_rtx_REG (word_mode, regno + 1);
3328 return gen_rtx_PARALLEL (VOIDmode,
3329 gen_rtvec (2, dwarf_low, dwarf_high));
3331 else if (mode == DCmode)
3333 rtx dwarf_high_re = gen_rtx_REG (word_mode, regno);
3334 rtx dwarf_low_re = gen_rtx_REG (word_mode, regno + 1);
3335 rtx dwarf_high_im = gen_rtx_REG (word_mode, regno);
3336 rtx dwarf_low_im = gen_rtx_REG (word_mode, regno + 1);
3337 return gen_rtx_PARALLEL (VOIDmode,
3338 gen_rtvec (4, dwarf_low_re, dwarf_high_re,
3339 dwarf_high_im, dwarf_low_im));
3341 else if (mode == SFmode || mode == SImode)
3343 /* Create new dwarf information with adjusted register number. */
3344 dwarf_single = gen_rtx_REG (word_mode, regno);
3345 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, dwarf_single));
3347 else
3349 /* We should not be here. */
3350 gcc_unreachable ();
3354 return NULL_RTX;
3357 /* Map internal gcc register numbers to DWARF2 register numbers. */
3359 unsigned int
3360 nds32_dbx_register_number (unsigned int regno)
3362 /* The nds32 port in GDB maintains a mapping between dwarf register
3363 number and displayed register name. For backward compatibility to
3364 previous toolchain, currently our gdb still has four registers
3365 (d0.l, d0.h, d1.l, and d1.h) between GPR and FPR while compiler
3366 does not count those four registers in its register number table.
3367 So we have to add 4 on its register number and then create new
3368 dwarf information. Hopefully we can discard such workaround
3369 in the future. */
3370 if (NDS32_IS_FPR_REGNUM (regno))
3371 return regno + 4;
3373 return regno;
3377 /* Defining target-specific uses of __attribute__. */
3379 /* Add some checking after merging attributes. */
3380 static tree
3381 nds32_merge_decl_attributes (tree olddecl, tree newdecl)
3383 tree combined_attrs;
3385 /* Create combined attributes. */
3386 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
3387 DECL_ATTRIBUTES (newdecl));
3389 /* Since newdecl is acutally a duplicate of olddecl,
3390 we can take olddecl for some operations. */
3391 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3393 /* Check isr-specific attributes conflict. */
3394 nds32_check_isr_attrs_conflict (olddecl, combined_attrs);
3397 return combined_attrs;
3400 /* Add some checking when inserting attributes. */
3401 static void
3402 nds32_insert_attributes (tree decl, tree *attributes)
3404 /* For function declaration, we need to check isr-specific attributes:
3405 1. Call nds32_check_isr_attrs_conflict() to check any conflict.
3406 2. Check valid integer value for interrupt/exception.
3407 3. Check valid integer value for reset.
3408 4. Check valid function for nmi/warm. */
3409 if (TREE_CODE (decl) == FUNCTION_DECL)
3411 tree func_attrs;
3412 tree intr, excp, reset;
3414 /* Pick up function attributes. */
3415 func_attrs = *attributes;
3417 /* 1. Call nds32_check_isr_attrs_conflict() to check any conflict. */
3418 nds32_check_isr_attrs_conflict (decl, func_attrs);
3420 /* Now we are starting to check valid id value
3421 for interrupt/exception/reset.
3422 Note that we ONLY check its validity here.
3423 To construct isr vector information, it is still performed
3424 by nds32_construct_isr_vectors_information(). */
3425 intr = lookup_attribute ("interrupt", func_attrs);
3426 excp = lookup_attribute ("exception", func_attrs);
3427 reset = lookup_attribute ("reset", func_attrs);
3429 if (intr || excp)
3431 /* Deal with interrupt/exception. */
3432 tree id_list;
3433 unsigned int lower_bound, upper_bound;
3435 /* The way to handle interrupt or exception is the same,
3436 we just need to take care of actual vector number.
3437 For interrupt(0..63), the actual vector number is (9..72).
3438 For exception(1..8), the actual vector number is (1..8). */
3439 lower_bound = (intr) ? (0) : (1);
3440 upper_bound = (intr) ? (63) : (8);
3442 /* Prepare id list so that we can traverse id value. */
3443 id_list = (intr) ? (TREE_VALUE (intr)) : (TREE_VALUE (excp));
3445 /* 2. Check valid integer value for interrupt/exception. */
3446 while (id_list)
3448 tree id;
3450 /* Pick up each vector id value. */
3451 id = TREE_VALUE (id_list);
3452 /* Issue error if it is not a valid integer value. */
3453 if (TREE_CODE (id) != INTEGER_CST
3454 || wi::ltu_p (wi::to_wide (id), lower_bound)
3455 || wi::gtu_p (wi::to_wide (id), upper_bound))
3456 error ("invalid id value for interrupt/exception attribute");
3458 /* Advance to next id. */
3459 id_list = TREE_CHAIN (id_list);
3462 else if (reset)
3464 /* Deal with reset. */
3465 tree id_list;
3466 tree id;
3467 tree nmi, warm;
3468 unsigned int lower_bound;
3469 unsigned int upper_bound;
3471 /* Prepare id_list and identify id value so that
3472 we can check if total number of vectors is valid. */
3473 id_list = TREE_VALUE (reset);
3474 id = TREE_VALUE (id_list);
3476 /* The maximum numbers for user's interrupt is 64. */
3477 lower_bound = 0;
3478 upper_bound = 64;
3480 /* 3. Check valid integer value for reset. */
3481 if (TREE_CODE (id) != INTEGER_CST
3482 || wi::ltu_p (wi::to_wide (id), lower_bound)
3483 || wi::gtu_p (wi::to_wide (id), upper_bound))
3484 error ("invalid id value for reset attribute");
3486 /* 4. Check valid function for nmi/warm. */
3487 nmi = lookup_attribute ("nmi", func_attrs);
3488 warm = lookup_attribute ("warm", func_attrs);
3490 if (nmi != NULL_TREE)
3492 tree nmi_func_list;
3493 tree nmi_func;
3495 nmi_func_list = TREE_VALUE (nmi);
3496 nmi_func = TREE_VALUE (nmi_func_list);
3498 /* Issue error if it is not a valid nmi function. */
3499 if (TREE_CODE (nmi_func) != IDENTIFIER_NODE)
3500 error ("invalid nmi function for reset attribute");
3503 if (warm != NULL_TREE)
3505 tree warm_func_list;
3506 tree warm_func;
3508 warm_func_list = TREE_VALUE (warm);
3509 warm_func = TREE_VALUE (warm_func_list);
3511 /* Issue error if it is not a valid warm function. */
3512 if (TREE_CODE (warm_func) != IDENTIFIER_NODE)
3513 error ("invalid warm function for reset attribute");
3516 else
3518 /* No interrupt, exception, or reset attribute is set. */
3519 return;
3524 static bool
3525 nds32_option_pragma_parse (tree args ATTRIBUTE_UNUSED,
3526 tree pop_target ATTRIBUTE_UNUSED)
3528 /* Currently, we do not parse any pragma target by ourself,
3529 so just simply return false. */
3530 return false;
3533 static void
3534 nds32_option_override (void)
3536 /* After all the command options have been parsed,
3537 we shall deal with some flags for changing compiler settings. */
3539 /* At first, we check if we have to strictly
3540 set some flags based on ISA family. */
3541 if (TARGET_ISA_V2)
3543 /* Under V2 ISA, we need to strictly disable TARGET_V3PUSH. */
3544 target_flags &= ~MASK_V3PUSH;
3546 if (TARGET_ISA_V3)
3548 /* Under V3 ISA, currently nothing should be strictly set. */
3550 if (TARGET_ISA_V3M)
3552 /* Under V3M ISA, we need to strictly enable TARGET_REDUCED_REGS. */
3553 target_flags |= MASK_REDUCED_REGS;
3554 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF. */
3555 target_flags &= ~MASK_EXT_PERF;
3556 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF2. */
3557 target_flags &= ~MASK_EXT_PERF2;
3558 /* Under V3M ISA, we need to strictly disable TARGET_EXT_STRING. */
3559 target_flags &= ~MASK_EXT_STRING;
3562 /* See if we are using reduced-set registers:
3563 $r0~$r5, $r6~$r10, $r15, $r28, $r29, $r30, $r31
3564 If so, we must forbid using $r11~$r14, $r16~$r27. */
3565 if (TARGET_REDUCED_REGS)
3567 int r;
3569 /* Prevent register allocator from
3570 choosing it as doing register allocation. */
3571 for (r = 11; r <= 14; r++)
3572 fixed_regs[r] = call_used_regs[r] = 1;
3573 for (r = 16; r <= 27; r++)
3574 fixed_regs[r] = call_used_regs[r] = 1;
3577 if (!TARGET_16_BIT)
3579 /* Under no 16 bit ISA, we need to strictly disable TARGET_V3PUSH. */
3580 target_flags &= ~MASK_V3PUSH;
3583 if (TARGET_HARD_FLOAT && !(TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
3585 if (nds32_arch_option == ARCH_V3S || nds32_arch_option == ARCH_V3F)
3586 error ("Disable FPU ISA, "
3587 "the ABI option must be enable '-mfloat-abi=soft'");
3588 else
3589 error ("'-mabi=2fp+' option only support when FPU available, "
3590 "must be enable '-mext-fpu-sp' or '-mext-fpu-dp'");
3593 /* Currently, we don't support PIC code generation yet. */
3594 if (flag_pic)
3595 sorry ("position-independent code not supported");
3597 nds32_register_passes ();
3601 /* Miscellaneous Parameters. */
3603 static rtx_insn *
3604 nds32_md_asm_adjust (vec<rtx> &outputs ATTRIBUTE_UNUSED,
3605 vec<rtx> &inputs ATTRIBUTE_UNUSED,
3606 vec<const char *> &constraints ATTRIBUTE_UNUSED,
3607 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
3609 clobbers.safe_push (gen_rtx_REG (SImode, TA_REGNUM));
3610 SET_HARD_REG_BIT (clobbered_regs, TA_REGNUM);
3611 return NULL;
3614 static void
3615 nds32_init_builtins (void)
3617 nds32_init_builtins_impl ();
3620 static tree
3621 nds32_builtin_decl (unsigned code, bool initialize_p)
3623 /* Implement in nds32-intrinsic.c. */
3624 return nds32_builtin_decl_impl (code, initialize_p);
3627 static rtx
3628 nds32_expand_builtin (tree exp,
3629 rtx target,
3630 rtx subtarget,
3631 machine_mode mode,
3632 int ignore)
3634 return nds32_expand_builtin_impl (exp, target, subtarget, mode, ignore);
3638 /* ------------------------------------------------------------------------ */
3640 /* PART 4: Implemet extern function definitions,
3641 the prototype is in nds32-protos.h. */
3643 /* Run-time Target Specification. */
3645 void
3646 nds32_cpu_cpp_builtins(struct cpp_reader *pfile)
3648 #define builtin_define(TXT) cpp_define (pfile, TXT)
3649 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
3650 builtin_define ("__nds32__");
3651 builtin_define ("__NDS32__");
3653 if (TARGET_HARD_FLOAT)
3654 builtin_define ("__NDS32_ABI_2FP_PLUS__");
3655 else
3656 builtin_define ("__NDS32_ABI_2__");
3658 if (TARGET_ISA_V2)
3659 builtin_define ("__NDS32_ISA_V2__");
3660 if (TARGET_ISA_V3)
3661 builtin_define ("__NDS32_ISA_V3__");
3662 if (TARGET_ISA_V3M)
3663 builtin_define ("__NDS32_ISA_V3M__");
3665 if (TARGET_FPU_SINGLE)
3666 builtin_define ("__NDS32_EXT_FPU_SP__");
3667 if (TARGET_FPU_DOUBLE)
3668 builtin_define ("__NDS32_EXT_FPU_DP__");
3670 if (TARGET_EXT_FPU_FMA)
3671 builtin_define ("__NDS32_EXT_FPU_FMA__");
3672 if (NDS32_EXT_FPU_DOT_E)
3673 builtin_define ("__NDS32_EXT_FPU_DOT_E__");
3674 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
3676 switch (nds32_fp_regnum)
3678 case 0:
3679 case 4:
3680 builtin_define ("__NDS32_EXT_FPU_CONFIG_0__");
3681 break;
3682 case 1:
3683 case 5:
3684 builtin_define ("__NDS32_EXT_FPU_CONFIG_1__");
3685 break;
3686 case 2:
3687 case 6:
3688 builtin_define ("__NDS32_EXT_FPU_CONFIG_2__");
3689 break;
3690 case 3:
3691 case 7:
3692 builtin_define ("__NDS32_EXT_FPU_CONFIG_3__");
3693 break;
3694 default:
3695 abort ();
3699 if (TARGET_BIG_ENDIAN)
3700 builtin_define ("__NDS32_EB__");
3701 else
3702 builtin_define ("__NDS32_EL__");
3704 if (TARGET_REDUCED_REGS)
3705 builtin_define ("__NDS32_REDUCED_REGS__");
3706 if (TARGET_CMOV)
3707 builtin_define ("__NDS32_CMOV__");
3708 if (TARGET_EXT_PERF)
3709 builtin_define ("__NDS32_EXT_PERF__");
3710 if (TARGET_EXT_PERF2)
3711 builtin_define ("__NDS32_EXT_PERF2__");
3712 if (TARGET_EXT_STRING)
3713 builtin_define ("__NDS32_EXT_STRING__");
3714 if (TARGET_16_BIT)
3715 builtin_define ("__NDS32_16_BIT__");
3716 if (TARGET_GP_DIRECT)
3717 builtin_define ("__NDS32_GP_DIRECT__");
3718 if (TARGET_VH)
3719 builtin_define ("__NDS32_VH__");
3721 if (TARGET_BIG_ENDIAN)
3722 builtin_define ("__big_endian__");
3724 builtin_assert ("cpu=nds32");
3725 builtin_assert ("machine=nds32");
3727 if (TARGET_HARD_FLOAT)
3728 builtin_define ("__NDS32_ABI_2FP_PLUS");
3729 else
3730 builtin_define ("__NDS32_ABI_2");
3732 #undef builtin_define
3733 #undef builtin_assert
3737 /* Defining Data Structures for Per-function Information. */
3739 void
3740 nds32_init_expanders (void)
3742 /* Arrange to initialize and mark the machine per-function status. */
3743 init_machine_status = nds32_init_machine_status;
3747 /* Register Usage. */
3749 /* -- Order of Allocation of Registers. */
3751 void
3752 nds32_adjust_reg_alloc_order (void)
3754 const int nds32_reg_alloc_order[] = REG_ALLOC_ORDER;
3756 /* Copy the default register allocation order, which is designed
3757 to optimize for code size. */
3758 memcpy(reg_alloc_order, nds32_reg_alloc_order, sizeof (reg_alloc_order));
3760 /* Adjust few register allocation order when optimizing for speed. */
3761 if (!optimize_size)
3763 memcpy (reg_alloc_order, nds32_reg_alloc_order_for_speed,
3764 sizeof (nds32_reg_alloc_order_for_speed));
3768 /* -- How Values Fit in Registers. */
3770 static unsigned
3771 nds32_hard_regno_nregs (unsigned regno ATTRIBUTE_UNUSED,
3772 machine_mode mode)
3774 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
3777 /* Implement TARGET_HARD_REGNO_MODE_OK. */
3779 static bool
3780 nds32_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
3782 if (regno > FIRST_PSEUDO_REGISTER)
3783 return true;
3785 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE) && NDS32_IS_FPR_REGNUM (regno))
3787 if (NDS32_IS_EXT_FPR_REGNUM(regno))
3788 return (NDS32_FPR_REGNO_OK_FOR_DOUBLE(regno) && (mode == DFmode));
3789 else if (mode == SFmode || mode == SImode)
3790 return NDS32_FPR_REGNO_OK_FOR_SINGLE (regno);
3791 else if (mode == DFmode)
3792 return NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno);
3794 return false;
3797 /* Restrict double-word quantities to even register pairs. */
3798 if (regno <= NDS32_LAST_GPR_REGNUM)
3799 return (targetm.hard_regno_nregs (regno, mode) == 1
3800 || !((regno) & 1));
3802 return false;
3805 #undef TARGET_HARD_REGNO_MODE_OK
3806 #define TARGET_HARD_REGNO_MODE_OK nds32_hard_regno_mode_ok
3808 /* Implement TARGET_MODES_TIEABLE_P. We can use general registers to
3809 tie QI/HI/SI modes together. */
3811 static bool
3812 nds32_modes_tieable_p (machine_mode mode1, machine_mode mode2)
3814 if ((GET_MODE_CLASS (mode1) == MODE_INT
3815 && GET_MODE_CLASS (mode2) == MODE_INT)
3816 && GET_MODE_SIZE (mode1) <= UNITS_PER_WORD
3817 && GET_MODE_SIZE (mode2) <= UNITS_PER_WORD)
3818 return true;
3820 if (GET_MODE_SIZE (mode1) == GET_MODE_SIZE (mode2))
3822 if ((TARGET_FPU_SINGLE && !TARGET_FPU_DOUBLE)
3823 && (mode1 == DFmode || mode2 == DFmode))
3824 return false;
3825 else
3826 return true;
3829 return false;
3832 #undef TARGET_MODES_TIEABLE_P
3833 #define TARGET_MODES_TIEABLE_P nds32_modes_tieable_p
3835 /* Register Classes. */
3837 enum reg_class
3838 nds32_regno_reg_class (int regno)
3840 /* Refer to nds32.h for more register class details. */
3842 if (regno >= 0 && regno <= 7)
3843 return LOW_REGS;
3844 else if (regno >= 8 && regno <= 11)
3845 return MIDDLE_REGS;
3846 else if (regno >= 12 && regno <= 14)
3847 return HIGH_REGS;
3848 else if (regno == 15)
3849 return R15_TA_REG;
3850 else if (regno >= 16 && regno <= 19)
3851 return MIDDLE_REGS;
3852 else if (regno >= 20 && regno <= 31)
3853 return HIGH_REGS;
3854 else if (regno == 32 || regno == 33)
3856 /* $SFP and $AP is FRAME_REGS in fact, However prevent IRA don't
3857 know how to allocate register for $SFP and $AP, just tell IRA they
3858 are GENERAL_REGS, and ARM do this hack too. */
3859 return GENERAL_REGS;
3861 else if (regno >= 34 && regno <= 97)
3862 return FP_REGS;
3863 else
3864 return NO_REGS;
3868 /* Stack Layout and Calling Conventions. */
3870 /* -- Basic Stack Layout. */
3873 nds32_dynamic_chain_address (rtx frameaddr)
3875 if (TARGET_V3PUSH)
3877 /* If -mv3push is specified, we push $fp, $gp, and $lp into stack.
3878 We can access dynamic chain address from stack by [$fp - 12]. */
3879 return plus_constant (Pmode, frameaddr, -12);
3881 else
3883 /* For general case we push $fp and $lp into stack at prologue.
3884 We can access dynamic chain address from stack by [$fp - 8]. */
3885 return plus_constant (Pmode, frameaddr, -8);
3890 nds32_return_addr_rtx (int count,
3891 rtx frameaddr)
3893 int offset;
3894 rtx addr;
3896 if (count != 0)
3898 /* In nds32 ABI design, we can expect that $lp is always available
3899 from stack by [$fp - 4] location. */
3900 offset = -4;
3901 addr = plus_constant (Pmode, frameaddr, offset);
3902 addr = memory_address (Pmode, addr);
3904 return gen_rtx_MEM (Pmode, addr);
3907 /* If count == 0, it means we are at current frame,
3908 the return address is $r30 ($lp). */
3909 return get_hard_reg_initial_val (Pmode, LP_REGNUM);
3912 /* -- Eliminating Frame Pointer and Arg Pointer. */
3914 HOST_WIDE_INT
3915 nds32_initial_elimination_offset (unsigned int from_reg, unsigned int to_reg)
3917 HOST_WIDE_INT offset;
3919 /* Compute and setup stack frame size.
3920 The result will be in cfun->machine. */
3921 nds32_compute_stack_frame ();
3923 /* Remember to consider
3924 cfun->machine->callee_saved_area_gpr_padding_bytes and
3925 cfun->machine->eh_return_data_regs_size
3926 when calculating offset. */
3927 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
3929 offset = (cfun->machine->fp_size
3930 + cfun->machine->gp_size
3931 + cfun->machine->lp_size
3932 + cfun->machine->callee_saved_gpr_regs_size
3933 + cfun->machine->callee_saved_area_gpr_padding_bytes
3934 + cfun->machine->callee_saved_fpr_regs_size
3935 + cfun->machine->eh_return_data_regs_size
3936 + cfun->machine->local_size
3937 + cfun->machine->out_args_size);
3939 else if (from_reg == ARG_POINTER_REGNUM
3940 && to_reg == HARD_FRAME_POINTER_REGNUM)
3942 offset = 0;
3944 else if (from_reg == FRAME_POINTER_REGNUM
3945 && to_reg == STACK_POINTER_REGNUM)
3947 offset = (cfun->machine->local_size + cfun->machine->out_args_size);
3949 else if (from_reg == FRAME_POINTER_REGNUM
3950 && to_reg == HARD_FRAME_POINTER_REGNUM)
3952 offset = (-1) * (cfun->machine->fp_size
3953 + cfun->machine->gp_size
3954 + cfun->machine->lp_size
3955 + cfun->machine->callee_saved_gpr_regs_size
3956 + cfun->machine->callee_saved_area_gpr_padding_bytes
3957 + cfun->machine->callee_saved_fpr_regs_size
3958 + cfun->machine->eh_return_data_regs_size);
3960 else
3962 gcc_unreachable ();
3965 return offset;
3968 /* -- Passing Arguments in Registers. */
3970 void
3971 nds32_init_cumulative_args (CUMULATIVE_ARGS *cum,
3972 tree fntype ATTRIBUTE_UNUSED,
3973 rtx libname ATTRIBUTE_UNUSED,
3974 tree fndecl ATTRIBUTE_UNUSED,
3975 int n_named_args ATTRIBUTE_UNUSED)
3977 /* Initial available registers. The values are offset against
3978 NDS32_GPR_ARG_FIRST_REGNUM and NDS32_FPR_ARG_FIRST_REGNUM
3979 for passing arguments. */
3980 cum->gpr_offset = 0;
3981 cum->fpr_offset = 0;
3984 /* -- Function Entry and Exit. */
3986 /* Function for normal multiple push prologue. */
3987 void
3988 nds32_expand_prologue (void)
3990 int fp_adjust;
3991 int sp_adjust;
3992 unsigned Rb, Re;
3994 /* Compute and setup stack frame size.
3995 The result will be in cfun->machine. */
3996 nds32_compute_stack_frame ();
3998 /* If this is a variadic function, first we need to push argument
3999 registers that hold the unnamed argument value. */
4000 if (cfun->machine->va_args_size != 0)
4002 Rb = cfun->machine->va_args_first_regno;
4003 Re = cfun->machine->va_args_last_regno;
4004 /* No need to push $fp, $gp, or $lp. */
4005 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, true);
4007 /* We may also need to adjust stack pointer for padding bytes
4008 because varargs may cause $sp not 8-byte aligned. */
4009 if (cfun->machine->va_args_area_padding_bytes)
4011 /* Generate sp adjustment instruction. */
4012 sp_adjust = cfun->machine->va_args_area_padding_bytes;
4014 nds32_emit_adjust_frame (stack_pointer_rtx,
4015 stack_pointer_rtx,
4016 -1 * sp_adjust);
4020 /* If the function is 'naked',
4021 we do not have to generate prologue code fragment. */
4022 if (cfun->machine->naked_p)
4023 return;
4025 /* Get callee_first_regno and callee_last_regno. */
4026 Rb = cfun->machine->callee_saved_first_gpr_regno;
4027 Re = cfun->machine->callee_saved_last_gpr_regno;
4029 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4030 to be saved, we don't have to create multiple push instruction.
4031 Otherwise, a multiple push instruction is needed. */
4032 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4033 && cfun->machine->fp_size == 0
4034 && cfun->machine->gp_size == 0
4035 && cfun->machine->lp_size == 0))
4037 /* Create multiple push instruction rtx. */
4038 nds32_emit_stack_push_multiple (
4039 Rb, Re,
4040 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size,
4041 false);
4044 /* Save eh data registers. */
4045 if (cfun->machine->use_eh_return_p)
4047 Rb = cfun->machine->eh_return_data_first_regno;
4048 Re = cfun->machine->eh_return_data_last_regno;
4050 /* No need to push $fp, $gp, or $lp.
4051 Also, this is not variadic arguments push. */
4052 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, false);
4055 /* Check frame_pointer_needed to see
4056 if we shall emit fp adjustment instruction. */
4057 if (frame_pointer_needed)
4059 /* adjust $fp = $sp + ($fp size) + ($gp size) + ($lp size)
4060 + (4 * callee-saved-registers)
4061 + (4 * exception-handling-data-registers)
4062 Note: No need to adjust
4063 cfun->machine->callee_saved_area_gpr_padding_bytes,
4064 because, at this point, stack pointer is just
4065 at the position after push instruction. */
4066 fp_adjust = cfun->machine->fp_size
4067 + cfun->machine->gp_size
4068 + cfun->machine->lp_size
4069 + cfun->machine->callee_saved_gpr_regs_size
4070 + cfun->machine->eh_return_data_regs_size;
4072 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4073 stack_pointer_rtx,
4074 fp_adjust);
4077 /* Save fpu registers. */
4078 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4080 /* When $sp moved to bottom of stack, we need to check whether
4081 the range of offset in the FPU instruction. */
4082 int fpr_offset = cfun->machine->local_size
4083 + cfun->machine->out_args_size
4084 + cfun->machine->callee_saved_fpr_regs_size;
4086 /* Check FPU instruction offset imm14s. */
4087 if (!satisfies_constraint_Is14 (GEN_INT (fpr_offset)))
4089 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4090 + cfun->machine->callee_saved_fpr_regs_size;
4092 /* Save fpu registers, need to allocate stack space
4093 for fpu callee registers. And now $sp position
4094 on callee saved fpr registers. */
4095 nds32_emit_adjust_frame (stack_pointer_rtx,
4096 stack_pointer_rtx,
4097 -1 * fpr_space);
4099 /* Emit fpu store instruction, using [$sp + offset] store
4100 fpu registers. */
4101 nds32_emit_push_fpr_callee_saved (0);
4103 /* Adjust $sp = $sp - local_size - out_args_size. */
4104 sp_adjust = cfun->machine->local_size
4105 + cfun->machine->out_args_size;
4107 /* Allocate stack space for local size and out args size. */
4108 nds32_emit_adjust_frame (stack_pointer_rtx,
4109 stack_pointer_rtx,
4110 -1 * sp_adjust);
4112 else
4114 /* Offset range in Is14, so $sp moved to bottom of stack. */
4116 /* Adjust $sp = $sp - local_size - out_args_size
4117 - callee_saved_area_gpr_padding_bytes
4118 - callee_saved_fpr_regs_size. */
4119 sp_adjust = cfun->machine->local_size
4120 + cfun->machine->out_args_size
4121 + cfun->machine->callee_saved_area_gpr_padding_bytes
4122 + cfun->machine->callee_saved_fpr_regs_size;
4124 nds32_emit_adjust_frame (stack_pointer_rtx,
4125 stack_pointer_rtx,
4126 -1 * sp_adjust);
4128 /* Emit fpu store instruction, using [$sp + offset] store
4129 fpu registers. */
4130 int fpr_position = cfun->machine->out_args_size
4131 + cfun->machine->local_size;
4132 nds32_emit_push_fpr_callee_saved (fpr_position);
4135 else
4137 /* Adjust $sp = $sp - local_size - out_args_size
4138 - callee_saved_area_gpr_padding_bytes. */
4139 sp_adjust = cfun->machine->local_size
4140 + cfun->machine->out_args_size
4141 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4143 /* sp_adjust value may be out of range of the addi instruction,
4144 create alternative add behavior with TA_REGNUM if necessary,
4145 using NEGATIVE value to tell that we are decreasing address. */
4146 nds32_emit_adjust_frame (stack_pointer_rtx,
4147 stack_pointer_rtx,
4148 -1 * sp_adjust);
4151 /* Prevent the instruction scheduler from
4152 moving instructions across the boundary. */
4153 emit_insn (gen_blockage ());
4156 /* Function for normal multiple pop epilogue. */
4157 void
4158 nds32_expand_epilogue (bool sibcall_p)
4160 int sp_adjust;
4161 unsigned Rb, Re;
4163 /* Compute and setup stack frame size.
4164 The result will be in cfun->machine. */
4165 nds32_compute_stack_frame ();
4167 /* Prevent the instruction scheduler from
4168 moving instructions across the boundary. */
4169 emit_insn (gen_blockage ());
4171 /* If the function is 'naked', we do not have to generate
4172 epilogue code fragment BUT 'ret' instruction.
4173 However, if this function is also a variadic function,
4174 we need to create adjust stack pointer before 'ret' instruction. */
4175 if (cfun->machine->naked_p)
4177 /* If this is a variadic function, we do not have to restore argument
4178 registers but need to adjust stack pointer back to previous stack
4179 frame location before return. */
4180 if (cfun->machine->va_args_size != 0)
4182 /* Generate sp adjustment instruction.
4183 We need to consider padding bytes here. */
4184 sp_adjust = cfun->machine->va_args_size
4185 + cfun->machine->va_args_area_padding_bytes;
4187 nds32_emit_adjust_frame (stack_pointer_rtx,
4188 stack_pointer_rtx,
4189 sp_adjust);
4192 /* Generate return instruction by using 'return_internal' pattern.
4193 Make sure this instruction is after gen_blockage(). */
4194 if (!sibcall_p)
4195 emit_jump_insn (gen_return_internal ());
4196 return;
4199 if (frame_pointer_needed)
4201 /* Restore fpu registers. */
4202 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4204 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
4206 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4207 - (4 * callee-saved-registers)
4208 - (4 * exception-handling-data-registers)
4209 - (4 * callee-saved-gpr-registers padding byte)
4210 - (4 * callee-saved-fpr-registers)
4211 Note: we want to adjust stack pointer
4212 to the position for callee-saved fpr register,
4213 And restore fpu register use .bi instruction to adjust $sp
4214 from callee-saved fpr register to pop instruction. */
4215 sp_adjust = cfun->machine->fp_size
4216 + cfun->machine->gp_size
4217 + cfun->machine->lp_size
4218 + cfun->machine->callee_saved_gpr_regs_size
4219 + cfun->machine->eh_return_data_regs_size
4220 + cfun->machine->callee_saved_area_gpr_padding_bytes
4221 + cfun->machine->callee_saved_fpr_regs_size;
4223 nds32_emit_adjust_frame (stack_pointer_rtx,
4224 hard_frame_pointer_rtx,
4225 -1 * sp_adjust);
4227 /* Emit fpu load instruction, using .bi instruction
4228 load fpu registers. */
4229 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4231 else
4233 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4234 - (4 * callee-saved-registers)
4235 - (4 * exception-handling-data-registers)
4236 Note: No need to adjust
4237 cfun->machine->callee_saved_area_gpr_padding_bytes,
4238 because we want to adjust stack pointer
4239 to the position for pop instruction. */
4240 sp_adjust = cfun->machine->fp_size
4241 + cfun->machine->gp_size
4242 + cfun->machine->lp_size
4243 + cfun->machine->callee_saved_gpr_regs_size
4244 + cfun->machine->eh_return_data_regs_size;
4246 nds32_emit_adjust_frame (stack_pointer_rtx,
4247 hard_frame_pointer_rtx,
4248 -1 * sp_adjust);
4251 else
4253 /* Restore fpu registers. */
4254 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4256 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
4258 /* Adjust $sp = $sp + local_size + out_args_size. */
4259 sp_adjust = cfun->machine->local_size
4260 + cfun->machine->out_args_size;
4262 nds32_emit_adjust_frame (stack_pointer_rtx,
4263 stack_pointer_rtx,
4264 sp_adjust);
4266 /* Emit fpu load instruction, using .bi instruction
4267 load fpu registers, and adjust $sp from callee-saved fpr register
4268 to callee-saved gpr register. */
4269 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4271 else
4273 /* If frame pointer is NOT needed,
4274 we cannot calculate the sp adjustment from frame pointer.
4275 Instead, we calculate the adjustment by local_size,
4276 out_args_size, and callee_saved_area_gpr_padding_bytes.
4277 Notice that such sp adjustment value may be out of range,
4278 so we have to deal with it as well. */
4280 /* Adjust $sp = $sp + local_size + out_args_size
4281 + callee_saved_area_gpr_padding_bytes. */
4282 sp_adjust = cfun->machine->local_size
4283 + cfun->machine->out_args_size
4284 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4286 nds32_emit_adjust_frame (stack_pointer_rtx,
4287 stack_pointer_rtx,
4288 sp_adjust);
4292 /* Restore eh data registers. */
4293 if (cfun->machine->use_eh_return_p)
4295 Rb = cfun->machine->eh_return_data_first_regno;
4296 Re = cfun->machine->eh_return_data_last_regno;
4298 /* No need to pop $fp, $gp, or $lp. */
4299 nds32_emit_stack_pop_multiple (Rb, Re, false, false, false);
4302 /* Get callee_first_regno and callee_last_regno. */
4303 Rb = cfun->machine->callee_saved_first_gpr_regno;
4304 Re = cfun->machine->callee_saved_last_gpr_regno;
4306 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4307 to be saved, we don't have to create multiple pop instruction.
4308 Otherwise, a multiple pop instruction is needed. */
4309 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4310 && cfun->machine->fp_size == 0
4311 && cfun->machine->gp_size == 0
4312 && cfun->machine->lp_size == 0))
4314 /* Create multiple pop instruction rtx. */
4315 nds32_emit_stack_pop_multiple (
4316 Rb, Re,
4317 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size);
4320 /* If this is a variadic function, we do not have to restore argument
4321 registers but need to adjust stack pointer back to previous stack
4322 frame location before return. */
4323 if (cfun->machine->va_args_size != 0)
4325 /* Generate sp adjustment instruction.
4326 We need to consider padding bytes here. */
4327 sp_adjust = cfun->machine->va_args_size
4328 + cfun->machine->va_args_area_padding_bytes;
4330 nds32_emit_adjust_frame (stack_pointer_rtx,
4331 stack_pointer_rtx,
4332 sp_adjust);
4335 /* If this function uses __builtin_eh_return, make stack adjustment
4336 for exception handler. */
4337 if (cfun->machine->use_eh_return_p)
4339 /* We need to unwind the stack by the offset computed by
4340 EH_RETURN_STACKADJ_RTX. However, at this point the CFA is
4341 based on SP. Ideally we would update the SP and define the
4342 CFA along the lines of:
4344 SP = SP + EH_RETURN_STACKADJ_RTX
4345 (regnote CFA = SP - EH_RETURN_STACKADJ_RTX)
4347 However the dwarf emitter only understands a constant
4348 register offset.
4350 The solution chosen here is to use the otherwise $ta ($r15)
4351 as a temporary register to hold the current SP value. The
4352 CFA is described using $ta then SP is modified. */
4354 rtx ta_reg;
4355 rtx insn;
4357 ta_reg = gen_rtx_REG (SImode, TA_REGNUM);
4359 insn = emit_move_insn (ta_reg, stack_pointer_rtx);
4360 add_reg_note (insn, REG_CFA_DEF_CFA, ta_reg);
4361 RTX_FRAME_RELATED_P (insn) = 1;
4363 emit_insn (gen_addsi3 (stack_pointer_rtx,
4364 stack_pointer_rtx,
4365 EH_RETURN_STACKADJ_RTX));
4367 /* Ensure the assignment to $ta does not get optimized away. */
4368 emit_use (ta_reg);
4371 /* Generate return instruction. */
4372 if (!sibcall_p)
4373 emit_jump_insn (gen_return_internal ());
4376 /* Function for v3push prologue. */
4377 void
4378 nds32_expand_prologue_v3push (void)
4380 int fp_adjust;
4381 int sp_adjust;
4382 int fpr_space = 0;
4383 unsigned Rb, Re;
4385 /* Compute and setup stack frame size.
4386 The result will be in cfun->machine. */
4387 nds32_compute_stack_frame ();
4389 if (cfun->machine->callee_saved_gpr_regs_size > 0)
4390 df_set_regs_ever_live (FP_REGNUM, 1);
4392 /* If the function is 'naked',
4393 we do not have to generate prologue code fragment. */
4394 if (cfun->machine->naked_p)
4395 return;
4397 /* Get callee_first_regno and callee_last_regno. */
4398 Rb = cfun->machine->callee_saved_first_gpr_regno;
4399 Re = cfun->machine->callee_saved_last_gpr_regno;
4401 /* Calculate sp_adjust first to test if 'push25 Re,imm8u' is available,
4402 where imm8u has to be 8-byte alignment. */
4403 sp_adjust = cfun->machine->local_size
4404 + cfun->machine->out_args_size
4405 + cfun->machine->callee_saved_area_gpr_padding_bytes
4406 + cfun->machine->callee_saved_fpr_regs_size;
4408 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4409 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust))
4411 /* We can use 'push25 Re,imm8u'. */
4413 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
4414 the pattern 'stack_v3push' is implemented in nds32.md. */
4415 nds32_emit_stack_v3push (Rb, Re, sp_adjust);
4417 /* Save fpu registers. */
4418 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4420 /* Calculate fpr position. */
4421 int fpr_position = cfun->machine->local_size
4422 + cfun->machine->out_args_size;
4423 /* Emit fpu store instruction, using [$sp + offset] store
4424 fpu registers. */
4425 nds32_emit_push_fpr_callee_saved (fpr_position);
4428 /* Check frame_pointer_needed to see
4429 if we shall emit fp adjustment instruction. */
4430 if (frame_pointer_needed)
4432 /* adjust $fp = $sp + 4 ($fp size)
4433 + 4 ($gp size)
4434 + 4 ($lp size)
4435 + (4 * n) (callee-saved registers)
4436 + sp_adjust ('push25 Re,imm8u')
4437 Note: Since we use 'push25 Re,imm8u',
4438 the position of stack pointer is further
4439 changed after push instruction.
4440 Hence, we need to take sp_adjust value
4441 into consideration. */
4442 fp_adjust = cfun->machine->fp_size
4443 + cfun->machine->gp_size
4444 + cfun->machine->lp_size
4445 + cfun->machine->callee_saved_gpr_regs_size
4446 + sp_adjust;
4448 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4449 stack_pointer_rtx,
4450 fp_adjust);
4453 else
4455 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4457 /* Calculate fpr space. */
4458 fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4459 + cfun->machine->callee_saved_fpr_regs_size;
4461 /* We have to use 'push25 Re, fpr_space', to pre-allocate
4462 callee saved fpr registers space. */
4463 nds32_emit_stack_v3push (Rb, Re, fpr_space);
4464 nds32_emit_push_fpr_callee_saved (0);
4466 else
4468 /* We have to use 'push25 Re,0' and
4469 expand one more instruction to adjust $sp later. */
4471 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
4472 the pattern 'stack_v3push' is implemented in nds32.md. */
4473 nds32_emit_stack_v3push (Rb, Re, 0);
4476 /* Check frame_pointer_needed to see
4477 if we shall emit fp adjustment instruction. */
4478 if (frame_pointer_needed)
4480 /* adjust $fp = $sp + 4 ($fp size)
4481 + 4 ($gp size)
4482 + 4 ($lp size)
4483 + (4 * n) (callee-saved registers)
4484 Note: Since we use 'push25 Re,0',
4485 the stack pointer is just at the position
4486 after push instruction.
4487 No need to take sp_adjust into consideration. */
4488 fp_adjust = cfun->machine->fp_size
4489 + cfun->machine->gp_size
4490 + cfun->machine->lp_size
4491 + cfun->machine->callee_saved_gpr_regs_size;
4493 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4495 /* We use 'push25 Re, fpr_space', the $sp is
4496 on callee saved fpr position, so need to consider
4497 fpr space. */
4498 fp_adjust = fp_adjust + fpr_space;
4501 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4502 stack_pointer_rtx,
4503 fp_adjust);
4506 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4508 /* We use 'push25 Re, fpr_space',
4509 the $sp is on callee saved fpr position,
4510 no need to consider fpr space. */
4511 sp_adjust = sp_adjust - fpr_space;
4514 /* Because we use 'push25 Re,0',
4515 we need to expand one more instruction to adjust $sp.
4516 using NEGATIVE value to tell that we are decreasing address. */
4517 nds32_emit_adjust_frame (stack_pointer_rtx,
4518 stack_pointer_rtx,
4519 -1 * sp_adjust);
4522 /* Prevent the instruction scheduler from
4523 moving instructions across the boundary. */
4524 emit_insn (gen_blockage ());
4527 /* Function for v3pop epilogue. */
4528 void
4529 nds32_expand_epilogue_v3pop (bool sibcall_p)
4531 int sp_adjust;
4532 unsigned Rb, Re;
4534 /* Compute and setup stack frame size.
4535 The result will be in cfun->machine. */
4536 nds32_compute_stack_frame ();
4538 /* Prevent the instruction scheduler from
4539 moving instructions across the boundary. */
4540 emit_insn (gen_blockage ());
4542 /* If the function is 'naked', we do not have to generate
4543 epilogue code fragment BUT 'ret' instruction. */
4544 if (cfun->machine->naked_p)
4546 /* Generate return instruction by using 'return_internal' pattern.
4547 Make sure this instruction is after gen_blockage(). */
4548 if (!sibcall_p)
4549 emit_jump_insn (gen_return_internal ());
4550 return;
4553 /* Get callee_first_regno and callee_last_regno. */
4554 Rb = cfun->machine->callee_saved_first_gpr_regno;
4555 Re = cfun->machine->callee_saved_last_gpr_regno;
4557 /* Calculate sp_adjust first to test if 'pop25 Re,imm8u' is available,
4558 where imm8u has to be 8-byte alignment. */
4559 sp_adjust = cfun->machine->local_size
4560 + cfun->machine->out_args_size
4561 + cfun->machine->callee_saved_area_gpr_padding_bytes
4562 + cfun->machine->callee_saved_fpr_regs_size;
4564 /* We have to consider alloca issue as well.
4565 If the function does call alloca(), the stack pointer is not fixed.
4566 In that case, we cannot use 'pop25 Re,imm8u' directly.
4567 We have to caculate stack pointer from frame pointer
4568 and then use 'pop25 Re,0'.
4569 Of course, the frame_pointer_needed should be nonzero
4570 if the function calls alloca(). */
4571 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4572 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
4573 && !cfun->calls_alloca)
4575 /* Restore fpu registers. */
4576 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4578 int fpr_position = cfun->machine->local_size
4579 + cfun->machine->out_args_size;
4580 /* Emit fpu load instruction, using [$sp + offset] restore
4581 fpu registers. */
4582 nds32_emit_v3pop_fpr_callee_saved (fpr_position);
4585 /* We can use 'pop25 Re,imm8u'. */
4587 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
4588 the pattern 'stack_v3pop' is implementad in nds32.md. */
4589 nds32_emit_stack_v3pop (Rb, Re, sp_adjust);
4591 else
4593 /* We have to use 'pop25 Re,0', and prior to it,
4594 we must expand one more instruction to adjust $sp. */
4596 if (frame_pointer_needed)
4598 /* adjust $sp = $fp - 4 ($fp size)
4599 - 4 ($gp size)
4600 - 4 ($lp size)
4601 - (4 * n) (callee-saved registers)
4602 Note: No need to adjust
4603 cfun->machine->callee_saved_area_gpr_padding_bytes,
4604 because we want to adjust stack pointer
4605 to the position for pop instruction. */
4606 sp_adjust = cfun->machine->fp_size
4607 + cfun->machine->gp_size
4608 + cfun->machine->lp_size
4609 + cfun->machine->callee_saved_gpr_regs_size;
4611 /* Restore fpu registers. */
4612 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4614 /* Set $sp to callee saved fpr position, we need to restore
4615 fpr registers. */
4616 sp_adjust = sp_adjust
4617 + cfun->machine->callee_saved_area_gpr_padding_bytes
4618 + cfun->machine->callee_saved_fpr_regs_size;
4620 nds32_emit_adjust_frame (stack_pointer_rtx,
4621 hard_frame_pointer_rtx,
4622 -1 * sp_adjust);
4624 /* Emit fpu load instruction, using [$sp + offset] restore
4625 fpu registers. */
4626 nds32_emit_v3pop_fpr_callee_saved (0);
4628 else
4630 nds32_emit_adjust_frame (stack_pointer_rtx,
4631 hard_frame_pointer_rtx,
4632 -1 * sp_adjust);
4635 else
4637 /* If frame pointer is NOT needed,
4638 we cannot calculate the sp adjustment from frame pointer.
4639 Instead, we calculate the adjustment by local_size,
4640 out_args_size, and callee_saved_area_padding_bytes.
4641 Notice that such sp adjustment value may be out of range,
4642 so we have to deal with it as well. */
4644 /* Adjust $sp = $sp + local_size + out_args_size
4645 + callee_saved_area_gpr_padding_bytes
4646 + callee_saved_fpr_regs_size. */
4647 sp_adjust = cfun->machine->local_size
4648 + cfun->machine->out_args_size
4649 + cfun->machine->callee_saved_area_gpr_padding_bytes
4650 + cfun->machine->callee_saved_fpr_regs_size;
4652 /* Restore fpu registers. */
4653 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4655 /* Set $sp to callee saved fpr position, we need to restore
4656 fpr registers. */
4657 sp_adjust = sp_adjust
4658 - cfun->machine->callee_saved_area_gpr_padding_bytes
4659 - cfun->machine->callee_saved_fpr_regs_size;
4661 nds32_emit_adjust_frame (stack_pointer_rtx,
4662 stack_pointer_rtx,
4663 sp_adjust);
4665 /* Emit fpu load instruction, using [$sp + offset] restore
4666 fpu registers. */
4667 nds32_emit_v3pop_fpr_callee_saved (0);
4669 else
4671 /* sp_adjust value may be out of range of the addi instruction,
4672 create alternative add behavior with TA_REGNUM if necessary,
4673 using POSITIVE value to tell that we are increasing
4674 address. */
4675 nds32_emit_adjust_frame (stack_pointer_rtx,
4676 stack_pointer_rtx,
4677 sp_adjust);
4681 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4683 /* We have fpr need to restore, so $sp is set on callee saved fpr
4684 position. And we use 'pop25 Re, fpr_space' to adjust $sp. */
4685 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4686 + cfun->machine->callee_saved_fpr_regs_size;
4687 nds32_emit_stack_v3pop (Rb, Re, fpr_space);
4689 else
4691 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
4692 the pattern 'stack_v3pop' is implementad in nds32.md. */
4693 nds32_emit_stack_v3pop (Rb, Re, 0);
4696 /* Generate return instruction. */
4697 emit_jump_insn (gen_pop25return ());
4700 /* Return nonzero if this function is known to have a null epilogue.
4701 This allows the optimizer to omit jumps to jumps if no stack
4702 was created. */
4704 nds32_can_use_return_insn (void)
4706 int sp_adjust;
4708 /* Prior to reloading, we can't tell how many registers must be saved.
4709 Thus we can not determine whether this function has null epilogue. */
4710 if (!reload_completed)
4711 return 0;
4713 sp_adjust = cfun->machine->local_size
4714 + cfun->machine->out_args_size
4715 + cfun->machine->callee_saved_area_gpr_padding_bytes
4716 + cfun->machine->callee_saved_fpr_regs_size;
4717 if (!cfun->machine->fp_as_gp_p
4718 && satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4719 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
4720 && !cfun->calls_alloca
4721 && NDS32_V3PUSH_AVAILABLE_P
4722 && !(TARGET_HARD_FLOAT
4723 && (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)))
4724 return 1;
4726 /* If no stack was created, two conditions must be satisfied:
4727 1. This is a naked function.
4728 So there is no callee-saved, local size, or outgoing size.
4729 2. This is NOT a variadic function.
4730 So there is no pushing arguement registers into the stack. */
4731 return (cfun->machine->naked_p && (cfun->machine->va_args_size == 0));
4734 /* ------------------------------------------------------------------------ */
4736 /* Function to test 333-form for load/store instructions.
4737 This is auxiliary extern function for auxiliary macro in nds32.h.
4738 Because it is a little complicated, we use function instead of macro. */
4739 bool
4740 nds32_ls_333_p (rtx rt, rtx ra, rtx imm, machine_mode mode)
4742 if (REGNO_REG_CLASS (REGNO (rt)) == LOW_REGS
4743 && REGNO_REG_CLASS (REGNO (ra)) == LOW_REGS)
4745 if (GET_MODE_SIZE (mode) == 4)
4746 return satisfies_constraint_Iu05 (imm);
4748 if (GET_MODE_SIZE (mode) == 2)
4749 return satisfies_constraint_Iu04 (imm);
4751 if (GET_MODE_SIZE (mode) == 1)
4752 return satisfies_constraint_Iu03 (imm);
4755 return false;
4758 /* Return alignment for the label. */
4760 nds32_target_alignment (rtx_insn *label)
4762 rtx_insn *insn;
4764 if (!NDS32_ALIGN_P ())
4765 return 0;
4767 insn = next_active_insn (label);
4769 /* Always align to 4 byte when first instruction after label is jump
4770 instruction since length for that might changed, so let's always align
4771 it for make sure we don't lose any perfomance here. */
4772 if (insn == 0
4773 || (get_attr_length (insn) == 2
4774 && !JUMP_P (insn) && !CALL_P (insn)))
4775 return 0;
4776 else
4777 return 2;
4780 bool
4781 nds32_split_double_word_load_store_p(rtx *operands, bool load_p)
4783 rtx mem = load_p ? operands[1] : operands[0];
4784 /* Do split at split2 if -O0 or schedule 2 not enable. */
4785 if (optimize == 0 || !flag_schedule_insns_after_reload)
4786 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
4788 /* Split double word load store after copy propgation. */
4789 if (current_pass == NULL)
4790 return false;
4792 const char *pass_name = current_pass->name;
4793 if (pass_name && ((strcmp (pass_name, "split4") == 0)
4794 || (strcmp (pass_name, "split5") == 0)))
4795 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
4797 return false;
4800 static bool
4801 nds32_use_blocks_for_constant_p (machine_mode mode,
4802 const_rtx x ATTRIBUTE_UNUSED)
4804 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
4805 && (mode == DFmode || mode == SFmode))
4806 return true;
4807 else
4808 return false;
4811 /* ------------------------------------------------------------------------ */
4813 /* PART 5: Initialize target hook structure and definitions. */
4815 /* Controlling the Compilation Driver. */
4818 /* Run-time Target Specification. */
4821 /* Defining Data Structures for Per-function Information. */
4824 /* Storage Layout. */
4826 #undef TARGET_PROMOTE_FUNCTION_MODE
4827 #define TARGET_PROMOTE_FUNCTION_MODE \
4828 default_promote_function_mode_always_promote
4831 /* Layout of Source Language Data Types. */
4834 /* Register Usage. */
4836 /* -- Basic Characteristics of Registers. */
4838 #undef TARGET_CONDITIONAL_REGISTER_USAGE
4839 #define TARGET_CONDITIONAL_REGISTER_USAGE nds32_conditional_register_usage
4841 /* -- Order of Allocation of Registers. */
4843 /* -- How Values Fit in Registers. */
4845 #undef TARGET_HARD_REGNO_NREGS
4846 #define TARGET_HARD_REGNO_NREGS nds32_hard_regno_nregs
4848 /* -- Handling Leaf Functions. */
4850 /* -- Registers That Form a Stack. */
4853 /* Register Classes. */
4855 #undef TARGET_CLASS_MAX_NREGS
4856 #define TARGET_CLASS_MAX_NREGS nds32_class_max_nregs
4858 #undef TARGET_REGISTER_PRIORITY
4859 #define TARGET_REGISTER_PRIORITY nds32_register_priority
4861 #undef TARGET_CAN_CHANGE_MODE_CLASS
4862 #define TARGET_CAN_CHANGE_MODE_CLASS nds32_can_change_mode_class
4865 /* Obsolete Macros for Defining Constraints. */
4868 /* Stack Layout and Calling Conventions. */
4870 /* -- Basic Stack Layout. */
4872 /* -- Exception Handling Support. */
4874 /* -- Specifying How Stack Checking is Done. */
4876 /* -- Registers That Address the Stack Frame. */
4878 /* -- Eliminating Frame Pointer and Arg Pointer. */
4880 #undef TARGET_CAN_ELIMINATE
4881 #define TARGET_CAN_ELIMINATE nds32_can_eliminate
4883 /* -- Passing Function Arguments on the Stack. */
4885 /* -- Passing Arguments in Registers. */
4887 #undef TARGET_FUNCTION_ARG
4888 #define TARGET_FUNCTION_ARG nds32_function_arg
4890 #undef TARGET_MUST_PASS_IN_STACK
4891 #define TARGET_MUST_PASS_IN_STACK nds32_must_pass_in_stack
4893 #undef TARGET_ARG_PARTIAL_BYTES
4894 #define TARGET_ARG_PARTIAL_BYTES nds32_arg_partial_bytes
4896 #undef TARGET_FUNCTION_ARG_ADVANCE
4897 #define TARGET_FUNCTION_ARG_ADVANCE nds32_function_arg_advance
4899 #undef TARGET_FUNCTION_ARG_BOUNDARY
4900 #define TARGET_FUNCTION_ARG_BOUNDARY nds32_function_arg_boundary
4902 /* -- How Scalar Function Values Are Returned. */
4904 #undef TARGET_FUNCTION_VALUE
4905 #define TARGET_FUNCTION_VALUE nds32_function_value
4907 #undef TARGET_LIBCALL_VALUE
4908 #define TARGET_LIBCALL_VALUE nds32_libcall_value
4910 #undef TARGET_FUNCTION_VALUE_REGNO_P
4911 #define TARGET_FUNCTION_VALUE_REGNO_P nds32_function_value_regno_p
4913 /* -- How Large Values Are Returned. */
4915 #undef TARGET_RETURN_IN_MEMORY
4916 #define TARGET_RETURN_IN_MEMORY nds32_return_in_memory
4918 /* -- Caller-Saves Register Allocation. */
4920 /* -- Function Entry and Exit. */
4922 #undef TARGET_ASM_FUNCTION_PROLOGUE
4923 #define TARGET_ASM_FUNCTION_PROLOGUE nds32_asm_function_prologue
4925 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
4926 #define TARGET_ASM_FUNCTION_END_PROLOGUE nds32_asm_function_end_prologue
4928 #undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
4929 #define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE nds32_asm_function_begin_epilogue
4931 #undef TARGET_ASM_FUNCTION_EPILOGUE
4932 #define TARGET_ASM_FUNCTION_EPILOGUE nds32_asm_function_epilogue
4934 #undef TARGET_ASM_OUTPUT_MI_THUNK
4935 #define TARGET_ASM_OUTPUT_MI_THUNK nds32_asm_output_mi_thunk
4937 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
4938 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
4940 /* -- Generating Code for Profiling. */
4942 /* -- Permitting tail calls. */
4944 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
4945 #define TARGET_FUNCTION_OK_FOR_SIBCALL nds32_function_ok_for_sibcall
4947 #undef TARGET_WARN_FUNC_RETURN
4948 #define TARGET_WARN_FUNC_RETURN nds32_warn_func_return
4950 /* Stack smashing protection. */
4953 /* Implementing the Varargs Macros. */
4955 #undef TARGET_SETUP_INCOMING_VARARGS
4956 #define TARGET_SETUP_INCOMING_VARARGS nds32_setup_incoming_varargs
4958 #undef TARGET_STRICT_ARGUMENT_NAMING
4959 #define TARGET_STRICT_ARGUMENT_NAMING nds32_strict_argument_naming
4962 /* Trampolines for Nested Functions. */
4964 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
4965 #define TARGET_ASM_TRAMPOLINE_TEMPLATE nds32_asm_trampoline_template
4967 #undef TARGET_TRAMPOLINE_INIT
4968 #define TARGET_TRAMPOLINE_INIT nds32_trampoline_init
4971 /* Implicit Calls to Library Routines. */
4974 /* Addressing Modes. */
4976 #undef TARGET_LEGITIMATE_ADDRESS_P
4977 #define TARGET_LEGITIMATE_ADDRESS_P nds32_legitimate_address_p
4980 /* Anchored Addresses. */
4983 /* Condition Code Status. */
4985 /* -- Representation of condition codes using (cc0). */
4987 /* -- Representation of condition codes using registers. */
4989 #undef TARGET_CANONICALIZE_COMPARISON
4990 #define TARGET_CANONICALIZE_COMPARISON nds32_canonicalize_comparison
4992 /* -- Macros to control conditional execution. */
4995 /* Describing Relative Costs of Operations. */
4997 #undef TARGET_REGISTER_MOVE_COST
4998 #define TARGET_REGISTER_MOVE_COST nds32_register_move_cost
5000 #undef TARGET_MEMORY_MOVE_COST
5001 #define TARGET_MEMORY_MOVE_COST nds32_memory_move_cost
5003 #undef TARGET_RTX_COSTS
5004 #define TARGET_RTX_COSTS nds32_rtx_costs
5006 #undef TARGET_ADDRESS_COST
5007 #define TARGET_ADDRESS_COST nds32_address_cost
5010 /* Adjusting the Instruction Scheduler. */
5013 /* Dividing the Output into Sections (Texts, Data, . . . ). */
5015 #undef TARGET_ENCODE_SECTION_INFO
5016 #define TARGET_ENCODE_SECTION_INFO nds32_encode_section_info
5019 /* Position Independent Code. */
5022 /* Defining the Output Assembler Language. */
5024 /* -- The Overall Framework of an Assembler File. */
5026 #undef TARGET_ASM_FILE_START
5027 #define TARGET_ASM_FILE_START nds32_asm_file_start
5028 #undef TARGET_ASM_FILE_END
5029 #define TARGET_ASM_FILE_END nds32_asm_file_end
5031 /* -- Output of Data. */
5033 #undef TARGET_ASM_ALIGNED_HI_OP
5034 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
5036 #undef TARGET_ASM_ALIGNED_SI_OP
5037 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
5039 /* -- Output of Uninitialized Variables. */
5041 /* -- Output and Generation of Labels. */
5043 #undef TARGET_ASM_GLOBALIZE_LABEL
5044 #define TARGET_ASM_GLOBALIZE_LABEL nds32_asm_globalize_label
5046 /* -- How Initialization Functions Are Handled. */
5048 /* -- Macros Controlling Initialization Routines. */
5050 /* -- Output of Assembler Instructions. */
5052 #undef TARGET_PRINT_OPERAND
5053 #define TARGET_PRINT_OPERAND nds32_print_operand
5054 #undef TARGET_PRINT_OPERAND_ADDRESS
5055 #define TARGET_PRINT_OPERAND_ADDRESS nds32_print_operand_address
5057 /* -- Output of Dispatch Tables. */
5059 /* -- Assembler Commands for Exception Regions. */
5061 #undef TARGET_DWARF_REGISTER_SPAN
5062 #define TARGET_DWARF_REGISTER_SPAN nds32_dwarf_register_span
5064 /* -- Assembler Commands for Alignment. */
5067 /* Controlling Debugging Information Format. */
5069 /* -- Macros Affecting All Debugging Formats. */
5071 /* -- Specific Options for DBX Output. */
5073 /* -- Open-Ended Hooks for DBX Format. */
5075 /* -- File Names in DBX Format. */
5077 /* -- Macros for DWARF Output. */
5079 /* -- Macros for VMS Debug Format. */
5082 /* Cross Compilation and Floating Point. */
5085 /* Mode Switching Instructions. */
5088 /* Defining target-specific uses of __attribute__. */
5090 #undef TARGET_ATTRIBUTE_TABLE
5091 #define TARGET_ATTRIBUTE_TABLE nds32_attribute_table
5093 #undef TARGET_MERGE_DECL_ATTRIBUTES
5094 #define TARGET_MERGE_DECL_ATTRIBUTES nds32_merge_decl_attributes
5096 #undef TARGET_INSERT_ATTRIBUTES
5097 #define TARGET_INSERT_ATTRIBUTES nds32_insert_attributes
5099 #undef TARGET_OPTION_PRAGMA_PARSE
5100 #define TARGET_OPTION_PRAGMA_PARSE nds32_option_pragma_parse
5102 #undef TARGET_OPTION_OVERRIDE
5103 #define TARGET_OPTION_OVERRIDE nds32_option_override
5106 /* Emulating TLS. */
5109 /* Defining coprocessor specifics for MIPS targets. */
5112 /* Parameters for Precompiled Header Validity Checking. */
5115 /* C++ ABI parameters. */
5118 /* Adding support for named address spaces. */
5121 /* Miscellaneous Parameters. */
5123 #undef TARGET_MD_ASM_ADJUST
5124 #define TARGET_MD_ASM_ADJUST nds32_md_asm_adjust
5126 #undef TARGET_INIT_BUILTINS
5127 #define TARGET_INIT_BUILTINS nds32_init_builtins
5129 #undef TARGET_BUILTIN_DECL
5130 #define TARGET_BUILTIN_DECL nds32_builtin_decl
5132 #undef TARGET_EXPAND_BUILTIN
5133 #define TARGET_EXPAND_BUILTIN nds32_expand_builtin
5136 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
5137 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P nds32_use_blocks_for_constant_p
5140 /* ------------------------------------------------------------------------ */
5142 /* Initialize the GCC target structure. */
5144 struct gcc_target targetm = TARGET_INITIALIZER;
5146 /* ------------------------------------------------------------------------ */