]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/nds32/nds32.c
Fix memory leak of pretty_printer prefixes
[thirdparty/gcc.git] / gcc / config / nds32 / nds32.c
CommitLineData
9304f876 1/* Subroutines used for code generation of Andes NDS32 cpu for GNU compiler
85ec4feb 2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
9304f876
CJW
3 Contributed by Andes Technology Corporation.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
19ac960a 21/* ------------------------------------------------------------------------ */
9304f876 22
8fcc61f8
RS
23#define IN_TARGET_CODE 1
24
9304f876
CJW
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
c7131fb2 28#include "backend.h"
e11c4407 29#include "target.h"
c7131fb2 30#include "rtl.h"
e11c4407 31#include "tree.h"
c4d8d050 32#include "tree-pass.h"
314e6352
ML
33#include "stringpool.h"
34#include "attribs.h"
c7131fb2 35#include "df.h"
4d0cdd0c 36#include "memmodel.h"
e11c4407
AM
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"
d8a2d370
DN
43#include "stor-layout.h"
44#include "varasm.h"
45#include "calls.h"
9304f876 46#include "output.h"
36566b39 47#include "explow.h"
9304f876 48#include "expr.h"
9304f876 49#include "tm-constrs.h"
9b2b7279 50#include "builtins.h"
72b7e5e1 51#include "cpplib.h"
c4d8d050 52#include "context.h"
9304f876 53
994c5d85 54/* This file should be included last. */
d58627a0
RS
55#include "target-def.h"
56
9304f876
CJW
57/* ------------------------------------------------------------------------ */
58
59/* This file is divided into five parts:
60
61 PART 1: Auxiliary static variable definitions and
62 target hook static variable definitions.
63
64 PART 2: Auxiliary static function definitions.
65
66 PART 3: Implement target hook stuff definitions.
67
68 PART 4: Implemet extern function definitions,
69 the prototype is in nds32-protos.h.
70
71 PART 5: Initialize target hook structure and definitions. */
72
73/* ------------------------------------------------------------------------ */
74
75/* PART 1: Auxiliary static variable definitions and
76 target hook static variable definitions. */
77
9304f876
CJW
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. */
82static const char * const nds32_intrinsic_register_names[] =
83{
2095a9d5
MC
84 "$CPU_VER",
85 "$ICM_CFG",
86 "$DCM_CFG",
87 "$MMU_CFG",
88 "$MSC_CFG",
89 "$MSC_CFG2",
90 "$CORE_ID",
91 "$FUCOP_EXIST",
92
93 "$PSW",
94 "$IPSW",
95 "$P_IPSW",
96 "$IVB",
97 "$EVA",
98 "$P_EVA",
99 "$ITYPE",
100 "$P_ITYPE",
101
102 "$MERR",
103 "$IPC",
104 "$P_IPC",
105 "$OIPC",
106 "$P_P0",
107 "$P_P1",
108
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",
125
126 "$MMU_CTL",
127 "$L1_PPTB",
128 "$TLB_VPN",
129 "$TLB_DATA",
130 "$TLB_MISC",
131 "$VLPT_IDX",
132 "$ILMB",
133 "$DLMB",
134
135 "$CACHE_CTL",
136 "$HSMP_SADDR",
137 "$HSMP_EADDR",
138 "$SDZ_CTL",
139 "$N12MISC_CTL",
140 "$MISC_CTL",
141 "$ECC_MISC",
142
143 "$BPC0",
144 "$BPC1",
145 "$BPC2",
146 "$BPC3",
147 "$BPC4",
148 "$BPC5",
149 "$BPC6",
150 "$BPC7",
151
152 "$BPA0",
153 "$BPA1",
154 "$BPA2",
155 "$BPA3",
156 "$BPA4",
157 "$BPA5",
158 "$BPA6",
159 "$BPA7",
160
161 "$BPAM0",
162 "$BPAM1",
163 "$BPAM2",
164 "$BPAM3",
165 "$BPAM4",
166 "$BPAM5",
167 "$BPAM6",
168 "$BPAM7",
169
170 "$BPV0",
171 "$BPV1",
172 "$BPV2",
173 "$BPV3",
174 "$BPV4",
175 "$BPV5",
176 "$BPV6",
177 "$BPV7",
178
179 "$BPCID0",
180 "$BPCID1",
181 "$BPCID2",
182 "$BPCID3",
183 "$BPCID4",
184 "$BPCID5",
185 "$BPCID6",
186 "$BPCID7",
187
188 "$EDM_CFG",
189 "$EDMSW",
190 "$EDM_CTL",
191 "$EDM_DTR",
192 "$BPMTC",
193 "$DIMBR",
194
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",
209
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",
223
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"
9304f876
CJW
238};
239
f1a0afe2
MC
240/* Define instrinsic cctl names. */
241static const char * const nds32_cctl_names[] =
242{
243 "L1D_VA_FILLCK",
244 "L1D_VA_ULCK",
245 "L1I_VA_FILLCK",
246 "L1I_VA_ULCK",
247
248 "L1D_IX_WBINVAL",
249 "L1D_IX_INVAL",
250 "L1D_IX_WB",
251 "L1I_IX_INVAL",
252
253 "L1D_VA_INVAL",
254 "L1D_VA_WB",
255 "L1D_VA_WBINVAL",
256 "L1I_VA_INVAL",
257
258 "L1D_IX_RTAG",
259 "L1D_IX_RWD",
260 "L1I_IX_RTAG",
261 "L1I_IX_RWD",
262
263 "L1D_IX_WTAG",
264 "L1D_IX_WWD",
265 "L1I_IX_WTAG",
266 "L1I_IX_WWD"
267};
5e6ae0cc 268
57aaf0cc
MC
269static const char * const nds32_dpref_names[] =
270{
271 "SRD",
272 "MRD",
273 "SWR",
274 "MWR",
275 "PTE",
276 "CLWR"
277};
278
5e6ae0cc
CJW
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(). */
282static const int nds32_reg_alloc_order_for_speed[] =
283{
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
288};
289
9304f876
CJW
290/* Defining target-specific uses of __attribute__. */
291static const struct attribute_spec nds32_attribute_table[] =
292{
293 /* Syntax: { name, min_len, max_len, decl_required, type_required,
4849deb1
JJ
294 function_type_required, affects_type_identity, handler,
295 exclude } */
9304f876
CJW
296
297 /* The interrupt vid: [0-63]+ (actual vector number starts from 9 to 72). */
4849deb1 298 { "interrupt", 1, 64, false, false, false, false, NULL, NULL },
9304f876 299 /* The exception vid: [1-8]+ (actual vector number starts from 1 to 8). */
4849deb1 300 { "exception", 1, 8, false, false, false, false, NULL, NULL },
9304f876 301 /* Argument is user's interrupt numbers. The vector number is always 0. */
4849deb1 302 { "reset", 1, 1, false, false, false, false, NULL, NULL },
9304f876
CJW
303
304 /* The attributes describing isr nested type. */
4849deb1
JJ
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 },
9304f876
CJW
308
309 /* The attributes describing isr register save scheme. */
4849deb1
JJ
310 { "save_all", 0, 0, false, false, false, false, NULL, NULL },
311 { "partial_save", 0, 0, false, false, false, false, NULL, NULL },
9304f876
CJW
312
313 /* The attributes used by reset attribute. */
4849deb1
JJ
314 { "nmi", 1, 1, false, false, false, false, NULL, NULL },
315 { "warm", 1, 1, false, false, false, false, NULL, NULL },
9304f876
CJW
316
317 /* The attribute telling no prologue/epilogue. */
4849deb1 318 { "naked", 0, 0, false, false, false, false, NULL, NULL },
9304f876 319
85a98076
KLC
320 /* The attribute is used to tell this function to be ROM patch. */
321 { "indirect_call",0, 0, false, false, false, false, NULL, NULL },
322
54c537e6
CJW
323 /* FOR BACKWARD COMPATIBILITY,
324 this attribute also tells no prologue/epilogue. */
325 { "no_prologue", 0, 0, false, false, false, false, NULL, NULL },
326
9304f876 327 /* The last attribute spec is set to be NULL. */
4849deb1 328 { NULL, 0, 0, false, false, false, false, NULL, NULL }
9304f876
CJW
329};
330
331
332/* ------------------------------------------------------------------------ */
333
334/* PART 2: Auxiliary static function definitions. */
335
336/* Function to save and restore machine-specific function data. */
337static struct machine_function *
338nds32_init_machine_status (void)
339{
340 struct machine_function *machine;
766090c2 341 machine = ggc_cleared_alloc<machine_function> ();
9304f876 342
ca3a4a55
CJW
343 /* Initially assume this function does not use __builtin_eh_return. */
344 machine->use_eh_return_p = 0;
345
9304f876
CJW
346 /* Initially assume this function needs prologue/epilogue. */
347 machine->naked_p = 0;
348
349 /* Initially assume this function does NOT use fp_as_gp optimization. */
350 machine->fp_as_gp_p = 0;
351
5f2a98c3
CJW
352 /* Initially this function is not under strictly aligned situation. */
353 machine->strict_aligned_p = 0;
354
54c537e6
CJW
355 /* Initially this function has no naked and no_prologue attributes. */
356 machine->attr_naked_p = 0;
357 machine->attr_no_prologue_p = 0;
358
9304f876
CJW
359 return machine;
360}
361
362/* Function to compute stack frame size and
363 store into cfun->machine structure. */
364static void
365nds32_compute_stack_frame (void)
366{
367 int r;
368 int block_size;
a6c7e777 369 bool v3pushpop_p;
9304f876
CJW
370
371 /* Because nds32_compute_stack_frame() will be called from different place,
372 everytime we enter this function, we have to assume this function
373 needs prologue/epilogue. */
374 cfun->machine->naked_p = 0;
375
54c537e6
CJW
376 /* We need to mark whether this function has naked and no_prologue
377 attribute so that we can distinguish the difference if users applies
378 -mret-in-naked-func option. */
379 cfun->machine->attr_naked_p
380 = lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl))
381 ? 1 : 0;
382 cfun->machine->attr_no_prologue_p
383 = lookup_attribute ("no_prologue", DECL_ATTRIBUTES (current_function_decl))
384 ? 1 : 0;
ca3a4a55
CJW
385
386 /* If __builtin_eh_return is used, we better have frame pointer needed
387 so that we can easily locate the stack slot of return address. */
388 if (crtl->calls_eh_return)
389 {
390 frame_pointer_needed = 1;
391
392 /* We need to mark eh data registers that need to be saved
393 in the stack. */
394 cfun->machine->eh_return_data_first_regno = EH_RETURN_DATA_REGNO (0);
395 for (r = 0; EH_RETURN_DATA_REGNO (r) != INVALID_REGNUM; r++)
396 cfun->machine->eh_return_data_last_regno = r;
397
398 cfun->machine->eh_return_data_regs_size
399 = 4 * (cfun->machine->eh_return_data_last_regno
400 - cfun->machine->eh_return_data_first_regno
401 + 1);
402 cfun->machine->use_eh_return_p = 1;
403 }
404 else
405 {
406 /* Assigning SP_REGNUM to eh_first_regno and eh_last_regno means we
407 do not need to handle __builtin_eh_return case in this function. */
408 cfun->machine->eh_return_data_first_regno = SP_REGNUM;
409 cfun->machine->eh_return_data_last_regno = SP_REGNUM;
410
411 cfun->machine->eh_return_data_regs_size = 0;
412 cfun->machine->use_eh_return_p = 0;
413 }
414
9304f876 415 /* Get variadic arguments size to prepare pretend arguments and
35da54a6
CJW
416 we will push them into stack at prologue by ourself. */
417 cfun->machine->va_args_size = crtl->args.pretend_args_size;
418 if (cfun->machine->va_args_size != 0)
419 {
420 cfun->machine->va_args_first_regno
8a498f99
CJW
421 = NDS32_GPR_ARG_FIRST_REGNUM
422 + NDS32_MAX_GPR_REGS_FOR_ARGS
423 - (crtl->args.pretend_args_size / UNITS_PER_WORD);
35da54a6 424 cfun->machine->va_args_last_regno
8a498f99 425 = NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS - 1;
35da54a6
CJW
426 }
427 else
428 {
429 cfun->machine->va_args_first_regno = SP_REGNUM;
430 cfun->machine->va_args_last_regno = SP_REGNUM;
431 }
432
433 /* Important: We need to make sure that varargs area is 8-byte alignment. */
434 block_size = cfun->machine->va_args_size;
435 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
436 {
437 cfun->machine->va_args_area_padding_bytes
438 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
439 }
9304f876
CJW
440
441 /* Get local variables, incoming variables, and temporary variables size.
442 Note that we need to make sure it is 8-byte alignment because
443 there may be no padding bytes if we are using LRA. */
444 cfun->machine->local_size = NDS32_ROUND_UP_DOUBLE_WORD (get_frame_size ());
445
446 /* Get outgoing arguments size. */
447 cfun->machine->out_args_size = crtl->outgoing_args_size;
448
449 /* If $fp value is required to be saved on stack, it needs 4 bytes space.
450 Check whether $fp is ever live. */
451 cfun->machine->fp_size = (df_regs_ever_live_p (FP_REGNUM)) ? 4 : 0;
452
453 /* If $gp value is required to be saved on stack, it needs 4 bytes space.
454 Check whether we are using PIC code genration. */
b26fa4f9
KLC
455 cfun->machine->gp_size =
456 (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)) ? 4 : 0;
9304f876
CJW
457
458 /* If $lp value is required to be saved on stack, it needs 4 bytes space.
459 Check whether $lp is ever live. */
13116651
CJW
460 cfun->machine->lp_size
461 = (flag_always_save_lp || df_regs_ever_live_p (LP_REGNUM)) ? 4 : 0;
9304f876
CJW
462
463 /* Initially there is no padding bytes. */
c457f751 464 cfun->machine->callee_saved_area_gpr_padding_bytes = 0;
9304f876
CJW
465
466 /* Calculate the bytes of saving callee-saved registers on stack. */
c457f751
CJW
467 cfun->machine->callee_saved_gpr_regs_size = 0;
468 cfun->machine->callee_saved_first_gpr_regno = SP_REGNUM;
469 cfun->machine->callee_saved_last_gpr_regno = SP_REGNUM;
e2286268
MC
470 cfun->machine->callee_saved_fpr_regs_size = 0;
471 cfun->machine->callee_saved_first_fpr_regno = SP_REGNUM;
472 cfun->machine->callee_saved_last_fpr_regno = SP_REGNUM;
473
9304f876
CJW
474 /* Currently, there is no need to check $r28~$r31
475 because we will save them in another way. */
476 for (r = 0; r < 28; r++)
477 {
478 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
479 {
480 /* Mark the first required callee-saved register
481 (only need to set it once).
482 If first regno == SP_REGNUM, we can tell that
483 it is the first time to be here. */
c457f751
CJW
484 if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
485 cfun->machine->callee_saved_first_gpr_regno = r;
9304f876 486 /* Mark the last required callee-saved register. */
c457f751 487 cfun->machine->callee_saved_last_gpr_regno = r;
9304f876
CJW
488 }
489 }
490
e2286268
MC
491 /* Recording fpu callee-saved register. */
492 if (TARGET_HARD_FLOAT)
493 {
494 for (r = NDS32_FIRST_FPR_REGNUM; r < NDS32_LAST_FPR_REGNUM; r++)
495 {
496 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
497 {
498 /* Mark the first required callee-saved register. */
499 if (cfun->machine->callee_saved_first_fpr_regno == SP_REGNUM)
500 {
501 /* Make first callee-saved number is even,
502 bacause we use doubleword access, and this way
503 promise 8-byte alignemt. */
504 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (r))
505 cfun->machine->callee_saved_first_fpr_regno = r - 1;
506 else
507 cfun->machine->callee_saved_first_fpr_regno = r;
508 }
509 cfun->machine->callee_saved_last_fpr_regno = r;
510 }
511 }
512
513 /* Make last callee-saved register number is odd,
514 we hope callee-saved register is even. */
515 int last_fpr = cfun->machine->callee_saved_last_fpr_regno;
516 if (NDS32_FPR_REGNO_OK_FOR_DOUBLE (last_fpr))
517 cfun->machine->callee_saved_last_fpr_regno++;
518 }
519
9304f876 520 /* Check if this function can omit prologue/epilogue code fragment.
54c537e6 521 If there is 'no_prologue'/'naked' attribute in this function,
9304f876
CJW
522 we can set 'naked_p' flag to indicate that
523 we do not have to generate prologue/epilogue.
524 Or, if all the following conditions succeed,
525 we can set this function 'naked_p' as well:
526 condition 1: first_regno == last_regno == SP_REGNUM,
8a498f99
CJW
527 which means we do not have to save
528 any callee-saved registers.
9304f876 529 condition 2: Both $lp and $fp are NOT live in this function,
8a498f99
CJW
530 which means we do not need to save them and there
531 is no outgoing size.
9304f876 532 condition 3: There is no local_size, which means
8a498f99 533 we do not need to adjust $sp. */
54c537e6
CJW
534 if (lookup_attribute ("no_prologue", DECL_ATTRIBUTES (current_function_decl))
535 || lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl))
c457f751
CJW
536 || (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM
537 && cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM
e2286268
MC
538 && cfun->machine->callee_saved_first_fpr_regno == SP_REGNUM
539 && cfun->machine->callee_saved_last_fpr_regno == SP_REGNUM
9304f876
CJW
540 && !df_regs_ever_live_p (FP_REGNUM)
541 && !df_regs_ever_live_p (LP_REGNUM)
b26fa4f9
KLC
542 && cfun->machine->local_size == 0
543 && !flag_pic))
9304f876 544 {
2da1e7c0 545 /* Set this function 'naked_p' and other functions can check this flag.
8a498f99
CJW
546 Note that in nds32 port, the 'naked_p = 1' JUST means there is no
547 callee-saved, local size, and outgoing size.
548 The varargs space and ret instruction may still present in
549 the prologue/epilogue expanding. */
9304f876
CJW
550 cfun->machine->naked_p = 1;
551
552 /* No need to save $fp, $gp, and $lp.
8a498f99
CJW
553 We should set these value to be zero
554 so that nds32_initial_elimination_offset() can work properly. */
9304f876
CJW
555 cfun->machine->fp_size = 0;
556 cfun->machine->gp_size = 0;
557 cfun->machine->lp_size = 0;
558
559 /* If stack usage computation is required,
8a498f99 560 we need to provide the static stack size. */
9304f876
CJW
561 if (flag_stack_usage_info)
562 current_function_static_stack_size = 0;
563
564 /* No need to do following adjustment, return immediately. */
565 return;
566 }
567
a6c7e777
MC
568 v3pushpop_p = NDS32_V3PUSH_AVAILABLE_P;
569
9304f876
CJW
570 /* Adjustment for v3push instructions:
571 If we are using v3push (push25/pop25) instructions,
572 we need to make sure Rb is $r6 and Re is
573 located on $r6, $r8, $r10, or $r14.
574 Some results above will be discarded and recomputed.
2da1e7c0
CJW
575 Note that it is only available under V3/V3M ISA and we
576 DO NOT setup following stuff for isr or variadic function. */
a6c7e777 577 if (v3pushpop_p)
9304f876
CJW
578 {
579 /* Recompute:
8a498f99
CJW
580 cfun->machine->fp_size
581 cfun->machine->gp_size
582 cfun->machine->lp_size
583 cfun->machine->callee_saved_first_gpr_regno
584 cfun->machine->callee_saved_last_gpr_regno */
9304f876
CJW
585
586 /* For v3push instructions, $fp, $gp, and $lp are always saved. */
587 cfun->machine->fp_size = 4;
588 cfun->machine->gp_size = 4;
589 cfun->machine->lp_size = 4;
590
591 /* Remember to set Rb = $r6. */
c457f751 592 cfun->machine->callee_saved_first_gpr_regno = 6;
9304f876 593
c457f751 594 if (cfun->machine->callee_saved_last_gpr_regno <= 6)
9304f876
CJW
595 {
596 /* Re = $r6 */
c457f751 597 cfun->machine->callee_saved_last_gpr_regno = 6;
9304f876 598 }
c457f751 599 else if (cfun->machine->callee_saved_last_gpr_regno <= 8)
9304f876
CJW
600 {
601 /* Re = $r8 */
c457f751 602 cfun->machine->callee_saved_last_gpr_regno = 8;
9304f876 603 }
c457f751 604 else if (cfun->machine->callee_saved_last_gpr_regno <= 10)
9304f876
CJW
605 {
606 /* Re = $r10 */
c457f751 607 cfun->machine->callee_saved_last_gpr_regno = 10;
9304f876 608 }
c457f751 609 else if (cfun->machine->callee_saved_last_gpr_regno <= 14)
9304f876
CJW
610 {
611 /* Re = $r14 */
c457f751 612 cfun->machine->callee_saved_last_gpr_regno = 14;
9304f876 613 }
c457f751 614 else if (cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM)
9304f876
CJW
615 {
616 /* If last_regno is SP_REGNUM, which means
617 it is never changed, so set it to Re = $r6. */
c457f751 618 cfun->machine->callee_saved_last_gpr_regno = 6;
9304f876
CJW
619 }
620 else
621 {
622 /* The program flow should not go here. */
623 gcc_unreachable ();
624 }
625 }
626
a6c7e777
MC
627 int sp_adjust = cfun->machine->local_size
628 + cfun->machine->out_args_size
e2286268
MC
629 + cfun->machine->callee_saved_area_gpr_padding_bytes
630 + cfun->machine->callee_saved_fpr_regs_size;
a6c7e777
MC
631
632 if (!v3pushpop_p
633 && sp_adjust == 0
634 && !frame_pointer_needed)
635 {
636 block_size = cfun->machine->fp_size
637 + cfun->machine->gp_size
361292be
CJW
638 + cfun->machine->lp_size;
639
640 if (cfun->machine->callee_saved_last_gpr_regno != SP_REGNUM)
641 block_size += (4 * (cfun->machine->callee_saved_last_gpr_regno
642 - cfun->machine->callee_saved_first_gpr_regno
643 + 1));
a6c7e777
MC
644
645 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
646 {
647 /* $r14 is last callee save register. */
648 if (cfun->machine->callee_saved_last_gpr_regno
649 < NDS32_LAST_CALLEE_SAVE_GPR_REGNUM)
650 {
651 cfun->machine->callee_saved_last_gpr_regno++;
652 }
653 else if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
654 {
655 cfun->machine->callee_saved_first_gpr_regno
656 = NDS32_FIRST_CALLEE_SAVE_GPR_REGNUM;
657 cfun->machine->callee_saved_last_gpr_regno
658 = NDS32_FIRST_CALLEE_SAVE_GPR_REGNUM;
659 }
660 }
661 }
662
8a498f99
CJW
663 /* We have correctly set callee_saved_first_gpr_regno
664 and callee_saved_last_gpr_regno.
665 Initially, the callee_saved_gpr_regs_size is supposed to be 0.
666 As long as callee_saved_last_gpr_regno is not SP_REGNUM,
667 we can update callee_saved_gpr_regs_size with new size. */
c457f751 668 if (cfun->machine->callee_saved_last_gpr_regno != SP_REGNUM)
9304f876
CJW
669 {
670 /* Compute pushed size of callee-saved registers. */
c457f751
CJW
671 cfun->machine->callee_saved_gpr_regs_size
672 = 4 * (cfun->machine->callee_saved_last_gpr_regno
673 - cfun->machine->callee_saved_first_gpr_regno
9304f876
CJW
674 + 1);
675 }
676
e2286268
MC
677 if (TARGET_HARD_FLOAT)
678 {
679 /* Compute size of callee svaed floating-point registers. */
680 if (cfun->machine->callee_saved_last_fpr_regno != SP_REGNUM)
681 {
682 cfun->machine->callee_saved_fpr_regs_size
683 = 4 * (cfun->machine->callee_saved_last_fpr_regno
684 - cfun->machine->callee_saved_first_fpr_regno
685 + 1);
686 }
687 }
688
9304f876 689 /* Important: We need to make sure that
8a498f99
CJW
690 (fp_size + gp_size + lp_size + callee_saved_gpr_regs_size)
691 is 8-byte alignment.
692 If it is not, calculate the padding bytes. */
35da54a6 693 block_size = cfun->machine->fp_size
9304f876
CJW
694 + cfun->machine->gp_size
695 + cfun->machine->lp_size
c457f751 696 + cfun->machine->callee_saved_gpr_regs_size;
9304f876
CJW
697 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
698 {
c457f751 699 cfun->machine->callee_saved_area_gpr_padding_bytes
9304f876
CJW
700 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
701 }
702
703 /* If stack usage computation is required,
704 we need to provide the static stack size. */
705 if (flag_stack_usage_info)
706 {
707 current_function_static_stack_size
708 = NDS32_ROUND_UP_DOUBLE_WORD (block_size)
709 + cfun->machine->local_size
710 + cfun->machine->out_args_size;
711 }
712}
713
714/* Function to create a parallel rtx pattern
715 which presents stack push multiple behavior.
716 The overall concept are:
717 "push registers to memory",
718 "adjust stack pointer". */
4e9a2848 719static void
a6c7e777
MC
720nds32_emit_stack_push_multiple (unsigned Rb, unsigned Re,
721 bool save_fp_p, bool save_gp_p, bool save_lp_p,
722 bool vaarg_p)
9304f876 723{
a6c7e777 724 unsigned regno;
9304f876
CJW
725 int extra_count;
726 int num_use_regs;
727 int par_index;
728 int offset;
729
730 rtx reg;
731 rtx mem;
732 rtx push_rtx;
733 rtx adjust_sp_rtx;
734 rtx parallel_insn;
47e0e7d2 735 rtx dwarf;
9304f876
CJW
736
737 /* We need to provide a customized rtx which contains
738 necessary information for data analysis,
739 so we create a parallel rtx like this:
740 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
8a498f99
CJW
741 (reg:SI Rb))
742 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
743 (reg:SI Rb+1))
744 ...
745 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
746 (reg:SI Re))
747 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
748 (reg:SI FP_REGNUM))
749 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
750 (reg:SI GP_REGNUM))
751 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
752 (reg:SI LP_REGNUM))
753 (set (reg:SI SP_REGNUM)
754 (plus (reg:SI SP_REGNUM) (const_int -32)))]) */
9304f876
CJW
755
756 /* Calculate the number of registers that will be pushed. */
757 extra_count = 0;
a6c7e777 758 if (save_fp_p)
9304f876 759 extra_count++;
a6c7e777 760 if (save_gp_p)
9304f876 761 extra_count++;
a6c7e777 762 if (save_lp_p)
9304f876
CJW
763 extra_count++;
764 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
a6c7e777 765 if (Rb == SP_REGNUM && Re == SP_REGNUM)
9304f876
CJW
766 num_use_regs = extra_count;
767 else
a6c7e777 768 num_use_regs = Re - Rb + 1 + extra_count;
9304f876
CJW
769
770 /* In addition to used registers,
771 we need one more space for (set sp sp-x) rtx. */
772 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
773 rtvec_alloc (num_use_regs + 1));
774 par_index = 0;
775
776 /* Initialize offset and start to create push behavior. */
777 offset = -(num_use_regs * 4);
778
779 /* Create (set mem regX) from Rb, Rb+1 up to Re. */
a6c7e777 780 for (regno = Rb; regno <= Re; regno++)
9304f876
CJW
781 {
782 /* Rb and Re may be SP_REGNUM.
8a498f99 783 We need to break this loop immediately. */
9304f876 784 if (regno == SP_REGNUM)
4e9a2848 785 break;
9304f876
CJW
786
787 reg = gen_rtx_REG (SImode, regno);
788 mem = gen_frame_mem (SImode, plus_constant (Pmode,
789 stack_pointer_rtx,
790 offset));
f7df4a84 791 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
792 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
793 RTX_FRAME_RELATED_P (push_rtx) = 1;
794 offset = offset + 4;
795 par_index++;
796 }
797
798 /* Create (set mem fp), (set mem gp), and (set mem lp) if necessary. */
a6c7e777 799 if (save_fp_p)
9304f876
CJW
800 {
801 reg = gen_rtx_REG (SImode, FP_REGNUM);
802 mem = gen_frame_mem (SImode, plus_constant (Pmode,
803 stack_pointer_rtx,
804 offset));
f7df4a84 805 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
806 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
807 RTX_FRAME_RELATED_P (push_rtx) = 1;
808 offset = offset + 4;
809 par_index++;
810 }
a6c7e777 811 if (save_gp_p)
9304f876
CJW
812 {
813 reg = gen_rtx_REG (SImode, GP_REGNUM);
814 mem = gen_frame_mem (SImode, plus_constant (Pmode,
815 stack_pointer_rtx,
816 offset));
f7df4a84 817 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
818 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
819 RTX_FRAME_RELATED_P (push_rtx) = 1;
820 offset = offset + 4;
821 par_index++;
822 }
a6c7e777 823 if (save_lp_p)
9304f876
CJW
824 {
825 reg = gen_rtx_REG (SImode, LP_REGNUM);
826 mem = gen_frame_mem (SImode, plus_constant (Pmode,
827 stack_pointer_rtx,
828 offset));
f7df4a84 829 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
830 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
831 RTX_FRAME_RELATED_P (push_rtx) = 1;
832 offset = offset + 4;
833 par_index++;
834 }
835
836 /* Create (set sp sp-x). */
837
838 /* We need to re-calculate the offset value again for adjustment. */
839 offset = -(num_use_regs * 4);
840 adjust_sp_rtx
f7df4a84 841 = gen_rtx_SET (stack_pointer_rtx,
9304f876
CJW
842 plus_constant (Pmode, stack_pointer_rtx, offset));
843 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
844 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
845
4e9a2848
CJW
846 parallel_insn = emit_insn (parallel_insn);
847
848 /* The insn rtx 'parallel_insn' will change frame layout.
849 We need to use RTX_FRAME_RELATED_P so that GCC is able to
850 generate CFI (Call Frame Information) stuff. */
851 RTX_FRAME_RELATED_P (parallel_insn) = 1;
47e0e7d2
CJW
852
853 /* Don't use GCC's logic for CFI info if we are generate a push for VAARG
854 since we will not restore those register at epilogue. */
855 if (vaarg_p)
856 {
857 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
858 copy_rtx (adjust_sp_rtx), NULL_RTX);
859 REG_NOTES (parallel_insn) = dwarf;
860 }
9304f876
CJW
861}
862
863/* Function to create a parallel rtx pattern
864 which presents stack pop multiple behavior.
865 The overall concept are:
866 "pop registers from memory",
867 "adjust stack pointer". */
4e9a2848 868static void
a6c7e777
MC
869nds32_emit_stack_pop_multiple (unsigned Rb, unsigned Re,
870 bool save_fp_p, bool save_gp_p, bool save_lp_p)
9304f876 871{
a6c7e777 872 unsigned regno;
9304f876
CJW
873 int extra_count;
874 int num_use_regs;
875 int par_index;
876 int offset;
877
878 rtx reg;
879 rtx mem;
880 rtx pop_rtx;
881 rtx adjust_sp_rtx;
882 rtx parallel_insn;
4e9a2848 883 rtx dwarf = NULL_RTX;
9304f876
CJW
884
885 /* We need to provide a customized rtx which contains
886 necessary information for data analysis,
887 so we create a parallel rtx like this:
888 (parallel [(set (reg:SI Rb)
8a498f99
CJW
889 (mem (reg:SI SP_REGNUM)))
890 (set (reg:SI Rb+1)
891 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
892 ...
893 (set (reg:SI Re)
894 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
895 (set (reg:SI FP_REGNUM)
896 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
897 (set (reg:SI GP_REGNUM)
898 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
899 (set (reg:SI LP_REGNUM)
900 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
901 (set (reg:SI SP_REGNUM)
902 (plus (reg:SI SP_REGNUM) (const_int 32)))]) */
9304f876
CJW
903
904 /* Calculate the number of registers that will be poped. */
905 extra_count = 0;
a6c7e777 906 if (save_fp_p)
9304f876 907 extra_count++;
a6c7e777 908 if (save_gp_p)
9304f876 909 extra_count++;
a6c7e777 910 if (save_lp_p)
9304f876
CJW
911 extra_count++;
912 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
a6c7e777 913 if (Rb == SP_REGNUM && Re == SP_REGNUM)
9304f876
CJW
914 num_use_regs = extra_count;
915 else
a6c7e777 916 num_use_regs = Re - Rb + 1 + extra_count;
9304f876
CJW
917
918 /* In addition to used registers,
919 we need one more space for (set sp sp+x) rtx. */
920 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
921 rtvec_alloc (num_use_regs + 1));
922 par_index = 0;
923
924 /* Initialize offset and start to create pop behavior. */
925 offset = 0;
926
927 /* Create (set regX mem) from Rb, Rb+1 up to Re. */
a6c7e777 928 for (regno = Rb; regno <= Re; regno++)
9304f876
CJW
929 {
930 /* Rb and Re may be SP_REGNUM.
8a498f99 931 We need to break this loop immediately. */
9304f876 932 if (regno == SP_REGNUM)
4e9a2848 933 break;
9304f876
CJW
934
935 reg = gen_rtx_REG (SImode, regno);
936 mem = gen_frame_mem (SImode, plus_constant (Pmode,
937 stack_pointer_rtx,
938 offset));
f7df4a84 939 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
940 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
941 RTX_FRAME_RELATED_P (pop_rtx) = 1;
942 offset = offset + 4;
943 par_index++;
4e9a2848
CJW
944
945 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
946 }
947
948 /* Create (set fp mem), (set gp mem), and (set lp mem) if necessary. */
a6c7e777 949 if (save_fp_p)
9304f876
CJW
950 {
951 reg = gen_rtx_REG (SImode, FP_REGNUM);
952 mem = gen_frame_mem (SImode, plus_constant (Pmode,
953 stack_pointer_rtx,
954 offset));
f7df4a84 955 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
956 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
957 RTX_FRAME_RELATED_P (pop_rtx) = 1;
958 offset = offset + 4;
959 par_index++;
4e9a2848
CJW
960
961 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876 962 }
a6c7e777 963 if (save_gp_p)
9304f876
CJW
964 {
965 reg = gen_rtx_REG (SImode, GP_REGNUM);
966 mem = gen_frame_mem (SImode, plus_constant (Pmode,
967 stack_pointer_rtx,
968 offset));
f7df4a84 969 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
970 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
971 RTX_FRAME_RELATED_P (pop_rtx) = 1;
972 offset = offset + 4;
973 par_index++;
4e9a2848
CJW
974
975 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876 976 }
a6c7e777 977 if (save_lp_p)
9304f876
CJW
978 {
979 reg = gen_rtx_REG (SImode, LP_REGNUM);
980 mem = gen_frame_mem (SImode, plus_constant (Pmode,
981 stack_pointer_rtx,
982 offset));
f7df4a84 983 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
984 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
985 RTX_FRAME_RELATED_P (pop_rtx) = 1;
986 offset = offset + 4;
987 par_index++;
4e9a2848
CJW
988
989 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
990 }
991
992 /* Create (set sp sp+x). */
993
994 /* The offset value is already in place. No need to re-calculate it. */
995 adjust_sp_rtx
f7df4a84 996 = gen_rtx_SET (stack_pointer_rtx,
9304f876
CJW
997 plus_constant (Pmode, stack_pointer_rtx, offset));
998 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
9304f876 999
4e9a2848
CJW
1000 /* Tell gcc we adjust SP in this insn. */
1001 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
1002
1003 parallel_insn = emit_insn (parallel_insn);
1004
1005 /* The insn rtx 'parallel_insn' will change frame layout.
1006 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1007 generate CFI (Call Frame Information) stuff. */
1008 RTX_FRAME_RELATED_P (parallel_insn) = 1;
1009
1010 /* Add CFI info by manual. */
1011 REG_NOTES (parallel_insn) = dwarf;
9304f876
CJW
1012}
1013
1014/* Function to create a parallel rtx pattern
1015 which presents stack v3push behavior.
1016 The overall concept are:
1017 "push registers to memory",
1018 "adjust stack pointer". */
88437f39 1019static void
a6c7e777
MC
1020nds32_emit_stack_v3push (unsigned Rb,
1021 unsigned Re,
1022 unsigned imm8u)
9304f876 1023{
a6c7e777 1024 unsigned regno;
9304f876
CJW
1025 int num_use_regs;
1026 int par_index;
1027 int offset;
1028
1029 rtx reg;
1030 rtx mem;
1031 rtx push_rtx;
1032 rtx adjust_sp_rtx;
1033 rtx parallel_insn;
1034
1035 /* We need to provide a customized rtx which contains
1036 necessary information for data analysis,
1037 so we create a parallel rtx like this:
88437f39 1038 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
8a498f99
CJW
1039 (reg:SI Rb))
1040 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
1041 (reg:SI Rb+1))
1042 ...
1043 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
1044 (reg:SI Re))
1045 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
1046 (reg:SI FP_REGNUM))
1047 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
1048 (reg:SI GP_REGNUM))
1049 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
1050 (reg:SI LP_REGNUM))
1051 (set (reg:SI SP_REGNUM)
1052 (plus (reg:SI SP_REGNUM) (const_int -32-imm8u)))]) */
9304f876
CJW
1053
1054 /* Calculate the number of registers that will be pushed.
1055 Since $fp, $gp, and $lp is always pushed with v3push instruction,
1056 we need to count these three registers.
1057 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1058 So there is no need to worry about Rb=Re=SP_REGNUM case. */
a6c7e777 1059 num_use_regs = Re - Rb + 1 + 3;
9304f876
CJW
1060
1061 /* In addition to used registers,
1062 we need one more space for (set sp sp-x-imm8u) rtx. */
1063 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
1064 rtvec_alloc (num_use_regs + 1));
1065 par_index = 0;
1066
1067 /* Initialize offset and start to create push behavior. */
1068 offset = -(num_use_regs * 4);
1069
1070 /* Create (set mem regX) from Rb, Rb+1 up to Re.
1071 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1072 So there is no need to worry about Rb=Re=SP_REGNUM case. */
a6c7e777 1073 for (regno = Rb; regno <= Re; regno++)
9304f876
CJW
1074 {
1075 reg = gen_rtx_REG (SImode, regno);
1076 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1077 stack_pointer_rtx,
1078 offset));
f7df4a84 1079 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
1080 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1081 RTX_FRAME_RELATED_P (push_rtx) = 1;
1082 offset = offset + 4;
1083 par_index++;
1084 }
1085
1086 /* Create (set mem fp). */
1087 reg = gen_rtx_REG (SImode, FP_REGNUM);
1088 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1089 stack_pointer_rtx,
1090 offset));
f7df4a84 1091 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
1092 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1093 RTX_FRAME_RELATED_P (push_rtx) = 1;
1094 offset = offset + 4;
1095 par_index++;
1096 /* Create (set mem gp). */
1097 reg = gen_rtx_REG (SImode, GP_REGNUM);
1098 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1099 stack_pointer_rtx,
1100 offset));
f7df4a84 1101 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
1102 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1103 RTX_FRAME_RELATED_P (push_rtx) = 1;
1104 offset = offset + 4;
1105 par_index++;
1106 /* Create (set mem lp). */
1107 reg = gen_rtx_REG (SImode, LP_REGNUM);
1108 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1109 stack_pointer_rtx,
1110 offset));
f7df4a84 1111 push_rtx = gen_rtx_SET (mem, reg);
9304f876
CJW
1112 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
1113 RTX_FRAME_RELATED_P (push_rtx) = 1;
1114 offset = offset + 4;
1115 par_index++;
1116
1117 /* Create (set sp sp-x-imm8u). */
1118
1119 /* We need to re-calculate the offset value again for adjustment. */
1120 offset = -(num_use_regs * 4);
1121 adjust_sp_rtx
f7df4a84 1122 = gen_rtx_SET (stack_pointer_rtx,
9304f876
CJW
1123 plus_constant (Pmode,
1124 stack_pointer_rtx,
a6c7e777 1125 offset - imm8u));
9304f876
CJW
1126 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
1127 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
1128
88437f39
CJW
1129 parallel_insn = emit_insn (parallel_insn);
1130
1131 /* The insn rtx 'parallel_insn' will change frame layout.
1132 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1133 generate CFI (Call Frame Information) stuff. */
1134 RTX_FRAME_RELATED_P (parallel_insn) = 1;
9304f876
CJW
1135}
1136
1137/* Function to create a parallel rtx pattern
1138 which presents stack v3pop behavior.
1139 The overall concept are:
1140 "pop registers from memory",
1141 "adjust stack pointer". */
88437f39 1142static void
a6c7e777
MC
1143nds32_emit_stack_v3pop (unsigned Rb,
1144 unsigned Re,
1145 unsigned imm8u)
9304f876 1146{
a6c7e777 1147 unsigned regno;
9304f876
CJW
1148 int num_use_regs;
1149 int par_index;
1150 int offset;
1151
1152 rtx reg;
1153 rtx mem;
1154 rtx pop_rtx;
1155 rtx adjust_sp_rtx;
1156 rtx parallel_insn;
88437f39 1157 rtx dwarf = NULL_RTX;
9304f876
CJW
1158
1159 /* We need to provide a customized rtx which contains
1160 necessary information for data analysis,
1161 so we create a parallel rtx like this:
1162 (parallel [(set (reg:SI Rb)
8a498f99
CJW
1163 (mem (reg:SI SP_REGNUM)))
1164 (set (reg:SI Rb+1)
1165 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
1166 ...
1167 (set (reg:SI Re)
1168 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
1169 (set (reg:SI FP_REGNUM)
1170 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
1171 (set (reg:SI GP_REGNUM)
1172 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
1173 (set (reg:SI LP_REGNUM)
1174 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
1175 (set (reg:SI SP_REGNUM)
1176 (plus (reg:SI SP_REGNUM) (const_int 32+imm8u)))]) */
9304f876
CJW
1177
1178 /* Calculate the number of registers that will be poped.
1179 Since $fp, $gp, and $lp is always poped with v3pop instruction,
1180 we need to count these three registers.
1181 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1182 So there is no need to worry about Rb=Re=SP_REGNUM case. */
a6c7e777 1183 num_use_regs = Re - Rb + 1 + 3;
9304f876
CJW
1184
1185 /* In addition to used registers,
1186 we need one more space for (set sp sp+x+imm8u) rtx. */
1187 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
1188 rtvec_alloc (num_use_regs + 1));
1189 par_index = 0;
1190
1191 /* Initialize offset and start to create pop behavior. */
1192 offset = 0;
1193
1194 /* Create (set regX mem) from Rb, Rb+1 up to Re.
1195 Under v3pop, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
1196 So there is no need to worry about Rb=Re=SP_REGNUM case. */
a6c7e777 1197 for (regno = Rb; regno <= Re; regno++)
9304f876
CJW
1198 {
1199 reg = gen_rtx_REG (SImode, regno);
1200 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1201 stack_pointer_rtx,
1202 offset));
f7df4a84 1203 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
1204 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1205 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1206 offset = offset + 4;
1207 par_index++;
88437f39
CJW
1208
1209 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
1210 }
1211
1212 /* Create (set fp mem). */
1213 reg = gen_rtx_REG (SImode, FP_REGNUM);
1214 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1215 stack_pointer_rtx,
1216 offset));
f7df4a84 1217 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
1218 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1219 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1220 offset = offset + 4;
1221 par_index++;
88437f39
CJW
1222 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1223
9304f876
CJW
1224 /* Create (set gp mem). */
1225 reg = gen_rtx_REG (SImode, GP_REGNUM);
1226 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1227 stack_pointer_rtx,
1228 offset));
f7df4a84 1229 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
1230 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1231 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1232 offset = offset + 4;
1233 par_index++;
88437f39
CJW
1234 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1235
9304f876
CJW
1236 /* Create (set lp mem ). */
1237 reg = gen_rtx_REG (SImode, LP_REGNUM);
1238 mem = gen_frame_mem (SImode, plus_constant (Pmode,
1239 stack_pointer_rtx,
1240 offset));
f7df4a84 1241 pop_rtx = gen_rtx_SET (reg, mem);
9304f876
CJW
1242 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
1243 RTX_FRAME_RELATED_P (pop_rtx) = 1;
1244 offset = offset + 4;
1245 par_index++;
88437f39 1246 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
9304f876
CJW
1247
1248 /* Create (set sp sp+x+imm8u). */
1249
1250 /* The offset value is already in place. No need to re-calculate it. */
1251 adjust_sp_rtx
f7df4a84 1252 = gen_rtx_SET (stack_pointer_rtx,
9304f876
CJW
1253 plus_constant (Pmode,
1254 stack_pointer_rtx,
a6c7e777 1255 offset + imm8u));
9304f876 1256 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
9304f876 1257
a6c7e777
MC
1258 if (frame_pointer_needed)
1259 {
1260 /* (expr_list:REG_CFA_DEF_CFA (plus:SI (reg/f:SI $sp)
1261 (const_int 0))
1262 mean reset frame pointer to $sp and reset to offset 0. */
1263 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1264 const0_rtx);
1265 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
1266 }
1267 else
1268 {
1269 /* Tell gcc we adjust SP in this insn. */
1270 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
1271 copy_rtx (adjust_sp_rtx), dwarf);
1272 }
88437f39
CJW
1273
1274 parallel_insn = emit_insn (parallel_insn);
1275
1276 /* The insn rtx 'parallel_insn' will change frame layout.
1277 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1278 generate CFI (Call Frame Information) stuff. */
1279 RTX_FRAME_RELATED_P (parallel_insn) = 1;
1280
1281 /* Add CFI info by manual. */
1282 REG_NOTES (parallel_insn) = dwarf;
9304f876
CJW
1283}
1284
b26fa4f9
KLC
1285static void
1286nds32_emit_load_gp (void)
1287{
1288 rtx got_symbol, pat;
1289
1290 /* Initial GLOBAL OFFSET TABLE don't do the scheduling. */
1291 emit_insn (gen_blockage ());
1292
1293 got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1294 /* sethi $gp, _GLOBAL_OFFSET_TABLE_ -8 */
1295 pat = gen_rtx_UNSPEC (SImode, gen_rtvec (1, got_symbol), UNSPEC_GOTINIT);
1296 pat = gen_rtx_CONST (SImode, gen_rtx_PLUS (Pmode, pat, GEN_INT (-8)));
1297 emit_insn (gen_sethi (pic_offset_table_rtx,pat));
1298
1299 /* ori $gp, $gp, _GLOBAL_OFFSET_TABLE_ -4 */
1300 pat = gen_rtx_UNSPEC (SImode, gen_rtvec (1, got_symbol), UNSPEC_GOTINIT);
1301 pat = gen_rtx_CONST (SImode, gen_rtx_PLUS (Pmode, pat, GEN_INT (-4)));
1302 emit_insn (gen_lo_sum (pic_offset_table_rtx, pic_offset_table_rtx, pat));
1303
1304 /* add5.pc $gp */
1305 emit_insn (gen_add_pc (pic_offset_table_rtx, pic_offset_table_rtx));
1306
1307 /* Initial GLOBAL OFFSET TABLE don't do the scheduling. */
1308 emit_insn (gen_blockage ());
1309}
1310
9304f876
CJW
1311/* Function that may creates more instructions
1312 for large value on adjusting stack pointer.
1313
1314 In nds32 target, 'addi' can be used for stack pointer
1315 adjustment in prologue/epilogue stage.
1316 However, sometimes there are too many local variables so that
1317 the adjustment value is not able to be fit in the 'addi' instruction.
1318 One solution is to move value into a register
1319 and then use 'add' instruction.
a6c7e777
MC
1320 In practice, we use TA_REGNUM ($r15) to accomplish this purpose. */
1321static void
1322nds32_emit_adjust_frame (rtx to_reg, rtx from_reg, int adjust_value)
9304f876 1323{
9304f876 1324 rtx tmp_reg;
a6c7e777
MC
1325 rtx frame_adjust_insn;
1326 rtx adjust_value_rtx = GEN_INT (adjust_value);
9304f876 1327
a6c7e777
MC
1328 if (adjust_value == 0)
1329 return;
1330
1331 if (!satisfies_constraint_Is15 (adjust_value_rtx))
9304f876
CJW
1332 {
1333 /* The value is not able to fit in single addi instruction.
8a498f99
CJW
1334 Create more instructions of moving value into a register
1335 and then add stack pointer with it. */
9304f876
CJW
1336
1337 /* $r15 is going to be temporary register to hold the value. */
1338 tmp_reg = gen_rtx_REG (SImode, TA_REGNUM);
1339
1340 /* Create one more instruction to move value
8a498f99 1341 into the temporary register. */
a6c7e777 1342 emit_move_insn (tmp_reg, adjust_value_rtx);
9304f876
CJW
1343
1344 /* Create new 'add' rtx. */
a6c7e777
MC
1345 frame_adjust_insn = gen_addsi3 (to_reg,
1346 from_reg,
1347 tmp_reg);
9304f876 1348 /* Emit rtx into insn list and receive its transformed insn rtx. */
a6c7e777 1349 frame_adjust_insn = emit_insn (frame_adjust_insn);
9304f876 1350
a6c7e777
MC
1351 /* Because (tmp_reg <- full_value) may be split into two
1352 rtl patterns, we can not set its RTX_FRAME_RELATED_P.
1353 We need to construct another (sp <- sp + full_value)
1354 and then insert it into sp_adjust_insn's reg note to
1355 represent a frame related expression.
1356 GCC knows how to refer it and output debug information. */
9304f876 1357
a6c7e777
MC
1358 rtx plus_rtx;
1359 rtx set_rtx;
9304f876 1360
a6c7e777
MC
1361 plus_rtx = plus_constant (Pmode, from_reg, adjust_value);
1362 set_rtx = gen_rtx_SET (to_reg, plus_rtx);
1363 add_reg_note (frame_adjust_insn, REG_FRAME_RELATED_EXPR, set_rtx);
9304f876
CJW
1364 }
1365 else
1366 {
a6c7e777
MC
1367 /* Generate sp adjustment instruction if and only if sp_adjust != 0. */
1368 frame_adjust_insn = gen_addsi3 (to_reg,
1369 from_reg,
1370 adjust_value_rtx);
1371 /* Emit rtx into instructions list and receive INSN rtx form. */
1372 frame_adjust_insn = emit_insn (frame_adjust_insn);
9304f876 1373 }
a6c7e777
MC
1374
1375 /* The insn rtx 'sp_adjust_insn' will change frame layout.
1376 We need to use RTX_FRAME_RELATED_P so that GCC is able to
1377 generate CFI (Call Frame Information) stuff. */
1378 RTX_FRAME_RELATED_P (frame_adjust_insn) = 1;
9304f876
CJW
1379}
1380
1381/* Return true if MODE/TYPE need double word alignment. */
1382static bool
ef4bddc2 1383nds32_needs_double_word_align (machine_mode mode, const_tree type)
9304f876
CJW
1384{
1385 unsigned int align;
1386
634bdae9
CJW
1387 /* Pick up the alignment according to the mode or type. */
1388 align = NDS32_MODE_TYPE_ALIGN (mode, type);
9304f876
CJW
1389
1390 return (align > PARM_BOUNDARY);
1391}
1392
1393/* Return true if FUNC is a naked function. */
2140297c 1394bool
810f736f 1395nds32_naked_function_p (tree func)
9304f876 1396{
54c537e6
CJW
1397 /* FOR BACKWARD COMPATIBILITY,
1398 we need to support 'no_prologue' attribute as well. */
1399 tree t_naked;
1400 tree t_no_prologue;
9304f876
CJW
1401
1402 if (TREE_CODE (func) != FUNCTION_DECL)
1403 abort ();
1404
54c537e6
CJW
1405 /* We have to use lookup_attribute() to check attributes.
1406 Because attr_naked_p and attr_no_prologue_p are set in
1407 nds32_compute_stack_frame() and the function has not been
1408 invoked yet. */
1409 t_naked = lookup_attribute ("naked", DECL_ATTRIBUTES (func));
1410 t_no_prologue = lookup_attribute ("no_prologue", DECL_ATTRIBUTES (func));
9304f876 1411
54c537e6 1412 return ((t_naked != NULL_TREE) || (t_no_prologue != NULL_TREE));
9304f876
CJW
1413}
1414
c411e9aa
SC
1415/* Function that determine whether a load postincrement is a good thing to use
1416 for a given mode. */
1417bool
1418nds32_use_load_post_increment (machine_mode mode)
1419{
1420 return (GET_MODE_SIZE (mode) <= GET_MODE_SIZE(E_DImode));
1421}
1422
9304f876
CJW
1423/* Function that check if 'X' is a valid address register.
1424 The variable 'STRICT' is very important to
1425 make decision for register number.
1426
1427 STRICT : true
1428 => We are in reload pass or after reload pass.
8a498f99 1429 The register number should be strictly limited in general registers.
9304f876
CJW
1430
1431 STRICT : false
1432 => Before reload pass, we are free to use any register number. */
1433static bool
1434nds32_address_register_rtx_p (rtx x, bool strict)
1435{
1436 int regno;
1437
1438 if (GET_CODE (x) != REG)
1439 return false;
1440
1441 regno = REGNO (x);
1442
1443 if (strict)
1444 return REGNO_OK_FOR_BASE_P (regno);
1445 else
1446 return true;
1447}
1448
1449/* Function that check if 'INDEX' is valid to be a index rtx for address.
1450
1451 OUTER_MODE : Machine mode of outer address rtx.
8a498f99 1452 INDEX : Check if this rtx is valid to be a index for address.
9304f876
CJW
1453 STRICT : If it is true, we are in reload pass or after reload pass. */
1454static bool
ef4bddc2 1455nds32_legitimate_index_p (machine_mode outer_mode,
9304f876
CJW
1456 rtx index,
1457 bool strict)
1458{
1459 int regno;
1460 rtx op0;
1461 rtx op1;
1462
1463 switch (GET_CODE (index))
1464 {
1465 case REG:
1466 regno = REGNO (index);
1467 /* If we are in reload pass or after reload pass,
8a498f99 1468 we need to limit it to general register. */
9304f876
CJW
1469 if (strict)
1470 return REGNO_OK_FOR_INDEX_P (regno);
1471 else
1472 return true;
1473
1474 case CONST_INT:
1475 /* The alignment of the integer value is determined by 'outer_mode'. */
e2286268 1476 switch (GET_MODE_SIZE (outer_mode))
9304f876 1477 {
e2286268 1478 case 1:
9304f876 1479 /* Further check if the value is legal for the 'outer_mode'. */
e2286268
MC
1480 if (satisfies_constraint_Is15 (index))
1481 return true;
1482 break;
9304f876 1483
e2286268 1484 case 2:
9304f876 1485 /* Further check if the value is legal for the 'outer_mode'. */
e2286268
MC
1486 if (satisfies_constraint_Is16 (index))
1487 {
5f2a98c3
CJW
1488 /* If it is not under strictly aligned situation,
1489 we can return true without checking alignment. */
1490 if (!cfun->machine->strict_aligned_p)
1491 return true;
e2286268 1492 /* Make sure address is half word alignment. */
5f2a98c3 1493 else if (NDS32_HALF_WORD_ALIGN_P (INTVAL (index)))
e2286268
MC
1494 return true;
1495 }
1496 break;
9304f876 1497
e2286268 1498 case 4:
9304f876 1499 /* Further check if the value is legal for the 'outer_mode'. */
e2286268
MC
1500 if (satisfies_constraint_Is17 (index))
1501 {
1502 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
1503 {
1504 if (!satisfies_constraint_Is14 (index))
1505 return false;
1506 }
1507
5f2a98c3
CJW
1508 /* If it is not under strictly aligned situation,
1509 we can return true without checking alignment. */
1510 if (!cfun->machine->strict_aligned_p)
1511 return true;
e2286268 1512 /* Make sure address is word alignment. */
5f2a98c3 1513 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
e2286268
MC
1514 return true;
1515 }
1516 break;
9304f876 1517
e2286268
MC
1518 case 8:
1519 if (satisfies_constraint_Is17 (gen_int_mode (INTVAL (index) + 4,
1520 SImode)))
1521 {
1522 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
1523 {
1524 if (!satisfies_constraint_Is14 (index))
1525 return false;
1526 }
1527
5f2a98c3
CJW
1528 /* If it is not under strictly aligned situation,
1529 we can return true without checking alignment. */
1530 if (!cfun->machine->strict_aligned_p)
1531 return true;
e2286268
MC
1532 /* Make sure address is word alignment.
1533 Currently we do not have 64-bit load/store yet,
1534 so we will use two 32-bit load/store instructions to do
1535 memory access and they are single word alignment. */
5f2a98c3 1536 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
e2286268
MC
1537 return true;
1538 }
1539 break;
9304f876 1540
e2286268
MC
1541 default:
1542 return false;
9304f876
CJW
1543 }
1544
1545 return false;
1546
1547 case MULT:
1548 op0 = XEXP (index, 0);
1549 op1 = XEXP (index, 1);
1550
1551 if (REG_P (op0) && CONST_INT_P (op1))
1552 {
1553 int multiplier;
1554 multiplier = INTVAL (op1);
1555
f62a2af5
CJW
1556 /* We only allow (mult reg const_int_1), (mult reg const_int_2),
1557 (mult reg const_int_4) or (mult reg const_int_8). */
1558 if (multiplier != 1 && multiplier != 2
1559 && multiplier != 4 && multiplier != 8)
9304f876
CJW
1560 return false;
1561
1562 regno = REGNO (op0);
1563 /* Limit it in general registers if we are
1564 in reload pass or after reload pass. */
1565 if(strict)
1566 return REGNO_OK_FOR_INDEX_P (regno);
1567 else
1568 return true;
1569 }
1570
1571 return false;
1572
1573 case ASHIFT:
1574 op0 = XEXP (index, 0);
1575 op1 = XEXP (index, 1);
1576
1577 if (REG_P (op0) && CONST_INT_P (op1))
1578 {
1579 int sv;
1580 /* op1 is already the sv value for use to do left shift. */
1581 sv = INTVAL (op1);
1582
1583 /* We only allow (ashift reg const_int_0)
f62a2af5
CJW
1584 or (ashift reg const_int_1) or (ashift reg const_int_2) or
1585 (ashift reg const_int_3). */
1586 if (sv != 0 && sv != 1 && sv !=2 && sv != 3)
9304f876
CJW
1587 return false;
1588
1589 regno = REGNO (op0);
1590 /* Limit it in general registers if we are
1591 in reload pass or after reload pass. */
1592 if(strict)
1593 return REGNO_OK_FOR_INDEX_P (regno);
1594 else
1595 return true;
1596 }
1597
1598 return false;
1599
1600 default:
1601 return false;
1602 }
1603}
1604
c4d8d050
CJW
1605static void
1606nds32_register_pass (
1607 rtl_opt_pass *(*make_pass_func) (gcc::context *),
1608 enum pass_positioning_ops pass_pos,
1609 const char *ref_pass_name)
1610{
1611 opt_pass *new_opt_pass = make_pass_func (g);
1612
1613 struct register_pass_info insert_pass =
1614 {
1615 new_opt_pass, /* pass */
1616 ref_pass_name, /* reference_pass_name */
1617 1, /* ref_pass_instance_number */
1618 pass_pos /* po_op */
1619 };
1620
1621 register_pass (&insert_pass);
1622}
1623
1624/* This function is called from nds32_option_override ().
1625 All new passes should be registered here. */
1626static void
1627nds32_register_passes (void)
1628{
2140297c
CJW
1629 nds32_register_pass (
1630 make_pass_nds32_fp_as_gp,
1631 PASS_POS_INSERT_BEFORE,
1632 "ira");
1633
c4d8d050
CJW
1634 nds32_register_pass (
1635 make_pass_nds32_relax_opt,
1636 PASS_POS_INSERT_AFTER,
1637 "mach");
1638}
1639
9304f876
CJW
1640/* ------------------------------------------------------------------------ */
1641
1642/* PART 3: Implement target hook stuff definitions. */
e2286268
MC
1643\f
1644
a5876228
CJW
1645/* Computing the Length of an Insn.
1646 Modifies the length assigned to instruction INSN.
1647 LEN is the initially computed length of the insn. */
1648int
1649nds32_adjust_insn_length (rtx_insn *insn, int length)
1650{
1651 int adjust_value = 0;
1652 switch (recog_memoized (insn))
1653 {
1654 case CODE_FOR_call_internal:
1655 case CODE_FOR_call_value_internal:
1656 {
43fa41c1
CJW
1657 if (NDS32_ALIGN_P ())
1658 {
1659 rtx_insn *next_insn = next_active_insn (insn);
1660 if (next_insn && get_attr_length (next_insn) != 2)
1661 adjust_value += 2;
1662 }
a5876228
CJW
1663 /* We need insert a nop after a noretun function call
1664 to prevent software breakpoint corrupt the next function. */
1665 if (find_reg_note (insn, REG_NORETURN, NULL_RTX))
1666 {
1667 if (TARGET_16_BIT)
1668 adjust_value += 2;
1669 else
1670 adjust_value += 4;
1671 }
1672 }
1673 return length + adjust_value;
1674
1675 default:
1676 return length;
1677 }
1678}
1679
5f2a98c3
CJW
1680/* Storage Layout. */
1681
1682/* This function will be called just before expansion into rtl. */
1683static void
1684nds32_expand_to_rtl_hook (void)
1685{
1686 /* We need to set strictly aligned situation.
1687 After that, the memory address checking in nds32_legitimate_address_p()
1688 will take alignment offset into consideration so that it will not create
1689 unaligned [base + offset] access during the rtl optimization. */
1690 cfun->machine->strict_aligned_p = 1;
1691}
1692
1693\f
e2286268
MC
1694/* Register Usage. */
1695
1696static void
1697nds32_conditional_register_usage (void)
1698{
1699 int regno;
1700
cf3cd43d
CJW
1701 if (TARGET_LINUX_ABI)
1702 fixed_regs[TP_REGNUM] = 1;
1703
e2286268
MC
1704 if (TARGET_HARD_FLOAT)
1705 {
1706 for (regno = NDS32_FIRST_FPR_REGNUM;
1707 regno <= NDS32_LAST_FPR_REGNUM; regno++)
1708 {
1709 fixed_regs[regno] = 0;
1710 if (regno < NDS32_FIRST_FPR_REGNUM + NDS32_MAX_FPR_REGS_FOR_ARGS)
1711 call_used_regs[regno] = 1;
1712 else if (regno >= NDS32_FIRST_FPR_REGNUM + 22
1713 && regno < NDS32_FIRST_FPR_REGNUM + 48)
1714 call_used_regs[regno] = 1;
1715 else
1716 call_used_regs[regno] = 0;
1717 }
1718 }
1719 else if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
1720 {
1721 for (regno = NDS32_FIRST_FPR_REGNUM;
1722 regno <= NDS32_LAST_FPR_REGNUM;
1723 regno++)
1724 fixed_regs[regno] = 0;
1725 }
1726}
1727
9304f876
CJW
1728\f
1729/* Register Classes. */
1730
1731static unsigned char
1732nds32_class_max_nregs (reg_class_t rclass ATTRIBUTE_UNUSED,
ef4bddc2 1733 machine_mode mode)
9304f876
CJW
1734{
1735 /* Return the maximum number of consecutive registers
1736 needed to represent "mode" in a register of "rclass". */
1737 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1738}
1739
1740static int
1741nds32_register_priority (int hard_regno)
1742{
1743 /* Encourage to use r0-r7 for LRA when optimize for size. */
6a5a7ee3
CJW
1744 if (optimize_size)
1745 {
1746 if (hard_regno < 8)
1747 return 4;
1748 else if (hard_regno < 16)
1749 return 3;
1750 else if (hard_regno < 28)
1751 return 2;
1752 else
1753 return 1;
1754 }
1755 else
1756 {
1757 if (hard_regno > 27)
1758 return 1;
1759 else
1760 return 4;
1761 }
9304f876
CJW
1762}
1763
e2286268
MC
1764static bool
1765nds32_can_change_mode_class (machine_mode from,
1766 machine_mode to,
1767 reg_class_t rclass)
1768{
1769 /* Don't spill double-precision register to two singal-precision
1770 registers */
1771 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
1772 && GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
1773 {
1774 return !reg_classes_intersect_p (rclass, FP_REGS);
1775 }
1776
1777 return true;
1778}
1779
9304f876
CJW
1780\f
1781/* Stack Layout and Calling Conventions. */
1782
1783/* There are three kinds of pointer concepts using in GCC compiler:
1784
1785 frame pointer: A pointer to the first location of local variables.
1786 stack pointer: A pointer to the top of a stack frame.
1787 argument pointer: A pointer to the incoming arguments.
1788
1789 In nds32 target calling convention, we are using 8-byte alignment.
1790 Besides, we would like to have each stack frame of a function includes:
1791
1792 [Block A]
1793 1. previous hard frame pointer
1794 2. return address
1795 3. callee-saved registers
1796 4. <padding bytes> (we will calculte in nds32_compute_stack_frame()
8a498f99
CJW
1797 and save it at
1798 cfun->machine->callee_saved_area_padding_bytes)
9304f876
CJW
1799
1800 [Block B]
1801 1. local variables
1802 2. spilling location
1803 3. <padding bytes> (it will be calculated by GCC itself)
1804 4. incoming arguments
1805 5. <padding bytes> (it will be calculated by GCC itself)
1806
1807 [Block C]
1808 1. <padding bytes> (it will be calculated by GCC itself)
1809 2. outgoing arguments
1810
1811 We 'wrap' these blocks together with
1812 hard frame pointer ($r28) and stack pointer ($r31).
1813 By applying the basic frame/stack/argument pointers concept,
1814 the layout of a stack frame shoule be like this:
1815
8a498f99 1816 | |
9304f876 1817 old stack pointer -> ----
8a498f99
CJW
1818 | | \
1819 | | saved arguments for
1820 | | vararg functions
1821 | | /
9304f876
CJW
1822 hard frame pointer -> --
1823 & argument pointer | | \
8a498f99
CJW
1824 | | previous hardware frame pointer
1825 | | return address
1826 | | callee-saved registers
1827 | | /
1828 frame pointer -> --
1829 | | \
1830 | | local variables
1831 | | and incoming arguments
1832 | | /
1833 --
1834 | | \
1835 | | outgoing
1836 | | arguments
1837 | | /
1838 stack pointer -> ----
9304f876
CJW
1839
1840 $SFP and $AP are used to represent frame pointer and arguments pointer,
1841 which will be both eliminated as hard frame pointer. */
1842
1843/* -- Eliminating Frame Pointer and Arg Pointer. */
1844
19ac960a
CJW
1845static bool
1846nds32_can_eliminate (const int from_reg, const int to_reg)
9304f876
CJW
1847{
1848 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1849 return true;
1850
1851 if (from_reg == ARG_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1852 return true;
1853
1854 if (from_reg == FRAME_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1855 return true;
1856
1857 if (from_reg == FRAME_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1858 return true;
1859
1860 return false;
1861}
1862
1863/* -- Passing Arguments in Registers. */
1864
1865static rtx
ef4bddc2 1866nds32_function_arg (cumulative_args_t ca, machine_mode mode,
9304f876
CJW
1867 const_tree type, bool named)
1868{
7f6cd86b 1869 unsigned int regno;
9304f876
CJW
1870 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1871
1872 /* The last time this hook is called,
1873 it is called with MODE == VOIDmode. */
1874 if (mode == VOIDmode)
1875 return NULL_RTX;
1876
7f6cd86b 1877 /* For nameless arguments, we need to take care it individually. */
9304f876 1878 if (!named)
9304f876 1879 {
7f6cd86b 1880 /* If we are under hard float abi, we have arguments passed on the
8a498f99 1881 stack and all situation can be handled by GCC itself. */
7f6cd86b
CJW
1882 if (TARGET_HARD_FLOAT)
1883 return NULL_RTX;
1884
1885 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1886 {
1887 /* If we still have enough registers to pass argument, pick up
1888 next available register number. */
1889 regno
1890 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1891 return gen_rtx_REG (mode, regno);
1892 }
634bdae9 1893
9304f876 1894 /* No register available, return NULL_RTX.
8a498f99 1895 The compiler will use stack to pass argument instead. */
9304f876
CJW
1896 return NULL_RTX;
1897 }
7f6cd86b
CJW
1898
1899 /* The following is to handle named argument.
1900 Note that the strategies of TARGET_HARD_FLOAT and !TARGET_HARD_FLOAT
1901 are different. */
1902 if (TARGET_HARD_FLOAT)
1903 {
e2286268
MC
1904 /* For TARGET_HARD_FLOAT calling convention, we use GPR and FPR
1905 to pass argument. We have to further check TYPE and MODE so
1906 that we can determine which kind of register we shall use. */
1907
1908 /* Note that we need to pass argument entirely in registers under
1909 hard float abi. */
1910 if (GET_MODE_CLASS (mode) == MODE_FLOAT
1911 && NDS32_ARG_ENTIRE_IN_FPR_REG_P (cum->fpr_offset, mode, type))
1912 {
1913 /* Pick up the next available FPR register number. */
1914 regno
1915 = NDS32_AVAILABLE_REGNUM_FOR_FPR_ARG (cum->fpr_offset, mode, type);
1916 return gen_rtx_REG (mode, regno);
1917 }
1918 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
1919 && NDS32_ARG_ENTIRE_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1920 {
1921 /* Pick up the next available GPR register number. */
1922 regno
1923 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1924 return gen_rtx_REG (mode, regno);
1925 }
7f6cd86b
CJW
1926 }
1927 else
1928 {
1929 /* For !TARGET_HARD_FLOAT calling convention, we always use GPR to pass
8a498f99
CJW
1930 argument. Since we allow to pass argument partially in registers,
1931 we can just return it if there are still registers available. */
7f6cd86b
CJW
1932 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1933 {
1934 /* Pick up the next available register number. */
1935 regno
1936 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1937 return gen_rtx_REG (mode, regno);
1938 }
1939
1940 }
1941
1942 /* No register available, return NULL_RTX.
1943 The compiler will use stack to pass argument instead. */
1944 return NULL_RTX;
9304f876
CJW
1945}
1946
d40f3c40 1947static bool
ef4bddc2 1948nds32_must_pass_in_stack (machine_mode mode, const_tree type)
d40f3c40
CJW
1949{
1950 /* Return true if a type must be passed in memory.
1951 If it is NOT using hard float abi, small aggregates can be
1952 passed in a register even we are calling a variadic function.
1953 So there is no need to take padding into consideration. */
1954 if (TARGET_HARD_FLOAT)
1955 return must_pass_in_stack_var_size_or_pad (mode, type);
1956 else
1957 return must_pass_in_stack_var_size (mode, type);
1958}
1959
650fc469 1960static int
ef4bddc2 1961nds32_arg_partial_bytes (cumulative_args_t ca, machine_mode mode,
650fc469
CJW
1962 tree type, bool named ATTRIBUTE_UNUSED)
1963{
1964 /* Returns the number of bytes at the beginning of an argument that
1965 must be put in registers. The value must be zero for arguments that are
1966 passed entirely in registers or that are entirely pushed on the stack.
1967 Besides, TARGET_FUNCTION_ARG for these arguments should return the
1968 first register to be used by the caller for this argument. */
1969 unsigned int needed_reg_count;
1970 unsigned int remaining_reg_count;
1971 CUMULATIVE_ARGS *cum;
1972
1973 cum = get_cumulative_args (ca);
1974
1975 /* Under hard float abi, we better have argument entirely passed in
1976 registers or pushed on the stack so that we can reduce the complexity
1977 of dealing with cum->gpr_offset and cum->fpr_offset. */
1978 if (TARGET_HARD_FLOAT)
1979 return 0;
1980
1981 /* If we have already runned out of argument registers, return zero
1982 so that the argument will be entirely pushed on the stack. */
1983 if (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1984 >= NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS)
1985 return 0;
1986
1987 /* Calculate how many registers do we need for this argument. */
1988 needed_reg_count = NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1989
1990 /* Calculate how many argument registers have left for passing argument.
1991 Note that we should count it from next available register number. */
1992 remaining_reg_count
1993 = NDS32_MAX_GPR_REGS_FOR_ARGS
1994 - (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
8a498f99 1995 - NDS32_GPR_ARG_FIRST_REGNUM);
650fc469
CJW
1996
1997 /* Note that we have to return the nubmer of bytes, not registers count. */
1998 if (needed_reg_count > remaining_reg_count)
1999 return remaining_reg_count * UNITS_PER_WORD;
2000
2001 return 0;
2002}
2003
9304f876 2004static void
ef4bddc2 2005nds32_function_arg_advance (cumulative_args_t ca, machine_mode mode,
9304f876
CJW
2006 const_tree type, bool named)
2007{
2008 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
2009
9304f876
CJW
2010 if (named)
2011 {
7f6cd86b 2012 /* We need to further check TYPE and MODE so that we can determine
e2286268 2013 which kind of register we shall advance. */
7f6cd86b
CJW
2014
2015 /* Under hard float abi, we may advance FPR registers. */
e2286268 2016 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
7f6cd86b 2017 {
e2286268
MC
2018 cum->fpr_offset
2019 = NDS32_AVAILABLE_REGNUM_FOR_FPR_ARG (cum->fpr_offset, mode, type)
2020 - NDS32_FPR_ARG_FIRST_REGNUM
2021 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
7f6cd86b
CJW
2022 }
2023 else
2024 {
2025 cum->gpr_offset
2026 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
2027 - NDS32_GPR_ARG_FIRST_REGNUM
2028 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
2029 }
2030 }
2031 else
2032 {
2033 /* If this nameless argument is NOT under TARGET_HARD_FLOAT,
8a498f99
CJW
2034 we can advance next register as well so that caller is
2035 able to pass arguments in registers and callee must be
2036 in charge of pushing all of them into stack. */
7f6cd86b
CJW
2037 if (!TARGET_HARD_FLOAT)
2038 {
2039 cum->gpr_offset
2040 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
2041 - NDS32_GPR_ARG_FIRST_REGNUM
2042 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
2043 }
9304f876
CJW
2044 }
2045}
2046
2047static unsigned int
ef4bddc2 2048nds32_function_arg_boundary (machine_mode mode, const_tree type)
9304f876
CJW
2049{
2050 return (nds32_needs_double_word_align (mode, type)
2051 ? NDS32_DOUBLE_WORD_ALIGNMENT
2052 : PARM_BOUNDARY);
2053}
2054
7c32ef41
MC
2055bool
2056nds32_vector_mode_supported_p (machine_mode mode)
2057{
2058 if (mode == V4QImode
2059 || mode == V2HImode)
2060 return NDS32_EXT_DSP_P ();
2061
2062 return false;
2063}
2064
9304f876
CJW
2065/* -- How Scalar Function Values Are Returned. */
2066
2067static rtx
2068nds32_function_value (const_tree ret_type,
2069 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
2070 bool outgoing ATTRIBUTE_UNUSED)
2071{
ef4bddc2 2072 machine_mode mode;
9304f876
CJW
2073 int unsignedp;
2074
2075 mode = TYPE_MODE (ret_type);
2076 unsignedp = TYPE_UNSIGNED (ret_type);
2077
e2286268
MC
2078 if (INTEGRAL_TYPE_P (ret_type))
2079 mode = promote_mode (ret_type, mode, &unsignedp);
9304f876 2080
e2286268
MC
2081 if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
2082 return gen_rtx_REG (mode, NDS32_FPR_RET_FIRST_REGNUM);
2083 else
2084 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
9304f876
CJW
2085}
2086
2087static rtx
ef4bddc2 2088nds32_libcall_value (machine_mode mode,
9304f876
CJW
2089 const_rtx fun ATTRIBUTE_UNUSED)
2090{
e2286268
MC
2091 if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
2092 return gen_rtx_REG (mode, NDS32_FPR_RET_FIRST_REGNUM);
2093
9304f876
CJW
2094 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
2095}
2096
2097static bool
2098nds32_function_value_regno_p (const unsigned int regno)
2099{
e2286268
MC
2100 if (regno == NDS32_GPR_RET_FIRST_REGNUM
2101 || (TARGET_HARD_FLOAT
2102 && regno == NDS32_FPR_RET_FIRST_REGNUM))
2103 return true;
2104
2105 return false;
2106}
2107
2108/* -- How Large Values Are Returned. */
2109
2110static bool
2111nds32_return_in_memory (const_tree type,
2112 const_tree fntype ATTRIBUTE_UNUSED)
2113{
2114 /* Note that int_size_in_bytes can return -1 if the size can vary
2115 or is larger than an integer. */
2116 HOST_WIDE_INT size = int_size_in_bytes (type);
2117
2118 /* For COMPLEX_TYPE, if the total size cannot be hold within two registers,
2119 the return value is supposed to be in memory. We need to be aware of
2120 that the size may be -1. */
2121 if (TREE_CODE (type) == COMPLEX_TYPE)
2122 if (size < 0 || size > 2 * UNITS_PER_WORD)
2123 return true;
2124
2125 /* If it is BLKmode and the total size cannot be hold within two registers,
2126 the return value is supposed to be in memory. We need to be aware of
2127 that the size may be -1. */
2128 if (TYPE_MODE (type) == BLKmode)
2129 if (size < 0 || size > 2 * UNITS_PER_WORD)
2130 return true;
2131
2132 /* For other cases, having result in memory is unnecessary. */
2133 return false;
9304f876
CJW
2134}
2135
2136/* -- Function Entry and Exit. */
2137
2138/* The content produced from this function
2139 will be placed before prologue body. */
2140static void
42776416 2141nds32_asm_function_prologue (FILE *file)
9304f876
CJW
2142{
2143 int r;
2144 const char *func_name;
2145 tree attrs;
2146 tree name;
2147
2148 /* All stack frame information is supposed to be
2149 already computed when expanding prologue.
2150 The result is in cfun->machine.
2151 DO NOT call nds32_compute_stack_frame() here
2152 because it may corrupt the essential information. */
2153
2154 fprintf (file, "\t! BEGIN PROLOGUE\n");
2155 fprintf (file, "\t! fp needed: %d\n", frame_pointer_needed);
2156 fprintf (file, "\t! pretend_args: %d\n", cfun->machine->va_args_size);
2157 fprintf (file, "\t! local_size: %d\n", cfun->machine->local_size);
2158 fprintf (file, "\t! out_args_size: %d\n", cfun->machine->out_args_size);
2159
2160 /* Use df_regs_ever_live_p() to detect if the register
2161 is ever used in the current function. */
2162 fprintf (file, "\t! registers ever_live: ");
e2286268 2163 for (r = 0; r < 65; r++)
9304f876
CJW
2164 {
2165 if (df_regs_ever_live_p (r))
2166 fprintf (file, "%s, ", reg_names[r]);
2167 }
2168 fputc ('\n', file);
2169
2170 /* Display the attributes of this function. */
2171 fprintf (file, "\t! function attributes: ");
f2dafb91
CJW
2172 /* Get the attributes tree list.
2173 Note that GCC builds attributes list with reverse order. */
2174 attrs = DECL_ATTRIBUTES (current_function_decl);
9304f876
CJW
2175
2176 /* If there is no any attribute, print out "None". */
2177 if (!attrs)
2178 fprintf (file, "None");
2179
2180 /* If there are some attributes, try if we need to
2181 construct isr vector information. */
2182 func_name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2183 nds32_construct_isr_vectors_information (attrs, func_name);
2184
2185 /* Display all attributes of this function. */
2186 while (attrs)
2187 {
2188 name = TREE_PURPOSE (attrs);
2189 fprintf (file, "%s ", IDENTIFIER_POINTER (name));
2190
2191 /* Pick up the next attribute. */
2192 attrs = TREE_CHAIN (attrs);
2193 }
2194 fputc ('\n', file);
2195}
2196
2197/* After rtl prologue has been expanded, this function is used. */
2198static void
2199nds32_asm_function_end_prologue (FILE *file)
2200{
2201 fprintf (file, "\t! END PROLOGUE\n");
9304f876
CJW
2202}
2203
2204/* Before rtl epilogue has been expanded, this function is used. */
2205static void
2206nds32_asm_function_begin_epilogue (FILE *file)
2207{
9304f876
CJW
2208 fprintf (file, "\t! BEGIN EPILOGUE\n");
2209}
2210
2211/* The content produced from this function
2212 will be placed after epilogue body. */
2213static void
42776416 2214nds32_asm_function_epilogue (FILE *file)
9304f876
CJW
2215{
2216 fprintf (file, "\t! END EPILOGUE\n");
2217}
2218
2219static void
2220nds32_asm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
2221 HOST_WIDE_INT delta,
2222 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2223 tree function)
2224{
2225 int this_regno;
2226
2227 /* Make sure unwind info is emitted for the thunk if needed. */
2228 final_start_function (emit_barrier (), file, 1);
2229
2230 this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
2231 ? 1
2232 : 0);
2233
b26fa4f9
KLC
2234 if (flag_pic)
2235 {
2236 fprintf (file, "\tsmw.adm\t$r31, [$r31], $r31, 4\n");
2237 fprintf (file, "\tsethi\t%s, hi20(_GLOBAL_OFFSET_TABLE_-8)\n",
2238 reg_names [PIC_OFFSET_TABLE_REGNUM]);
2239 fprintf (file, "\tori\t%s, %s, lo12(_GLOBAL_OFFSET_TABLE_-4)\n",
2240 reg_names [PIC_OFFSET_TABLE_REGNUM],
2241 reg_names [PIC_OFFSET_TABLE_REGNUM]);
2242
2243 if (TARGET_ISA_V3)
2244 fprintf (file, "\tadd5.pc\t$gp\n");
2245 else
2246 {
2247 fprintf (file, "\tmfusr\t$ta, $pc\n");
2248 fprintf (file, "\tadd\t%s, $ta, %s\n",
2249 reg_names [PIC_OFFSET_TABLE_REGNUM],
2250 reg_names [PIC_OFFSET_TABLE_REGNUM]);
2251 }
2252 }
2253
9304f876
CJW
2254 if (delta != 0)
2255 {
2256 if (satisfies_constraint_Is15 (GEN_INT (delta)))
2257 {
5af50159 2258 fprintf (file, "\taddi\t$r%d, $r%d, " HOST_WIDE_INT_PRINT_DEC "\n",
9304f876
CJW
2259 this_regno, this_regno, delta);
2260 }
2261 else if (satisfies_constraint_Is20 (GEN_INT (delta)))
2262 {
5af50159 2263 fprintf (file, "\tmovi\t$ta, " HOST_WIDE_INT_PRINT_DEC "\n", delta);
9304f876
CJW
2264 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
2265 }
2266 else
2267 {
5af50159
KC
2268 fprintf (file,
2269 "\tsethi\t$ta, hi20(" HOST_WIDE_INT_PRINT_DEC ")\n",
2270 delta);
2271 fprintf (file,
2272 "\tori\t$ta, $ta, lo12(" HOST_WIDE_INT_PRINT_DEC ")\n",
2273 delta);
9304f876
CJW
2274 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
2275 }
2276 }
2277
b26fa4f9
KLC
2278 if (flag_pic)
2279 {
2280 fprintf (file, "\tla\t$ta, ");
2281 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2282 fprintf (file, "@PLT\n");
2283 fprintf (file, "\t! epilogue\n");
2284 fprintf (file, "\tlwi.bi\t%s, [%s], 4\n",
2285 reg_names[PIC_OFFSET_TABLE_REGNUM],
2286 reg_names[STACK_POINTER_REGNUM]);
2287 fprintf (file, "\tbr\t$ta\n");
2288 }
2289 else
2290 {
2291 fprintf (file, "\tb\t");
2292 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2293 fprintf (file, "\n");
2294 }
9304f876
CJW
2295
2296 final_end_function ();
2297}
2298
2299/* -- Permitting tail calls. */
2300
cc48a87f
CJW
2301/* Return true if it is ok to do sibling call optimization. */
2302static bool
2303nds32_function_ok_for_sibcall (tree decl,
2304 tree exp ATTRIBUTE_UNUSED)
2305{
2306 /* The DECL is NULL if it is an indirect call. */
2307
2308 /* 1. Do not apply sibling call if -mv3push is enabled,
2309 because pop25 instruction also represents return behavior.
2310 2. If this function is a variadic function, do not apply sibling call
2311 because the stack layout may be a mess.
2312 3. We don't want to apply sibling call optimization for indirect
2313 sibcall because the pop behavior in epilogue may pollute the
2314 content of caller-saved regsiter when the register is used for
b26fa4f9
KLC
2315 indirect sibcall.
2316 4. In pic mode, it may use some registers for PLT call. */
cc48a87f
CJW
2317 return (!TARGET_V3PUSH
2318 && (cfun->machine->va_args_size == 0)
b26fa4f9
KLC
2319 && decl
2320 && !flag_pic);
cc48a87f
CJW
2321}
2322
9304f876
CJW
2323/* Determine whether we need to enable warning for function return check. */
2324static bool
2325nds32_warn_func_return (tree decl)
2326{
8a498f99
CJW
2327 /* Naked functions are implemented entirely in assembly, including the
2328 return sequence, so suppress warnings about this. */
9304f876
CJW
2329 return !nds32_naked_function_p (decl);
2330}
2331
2332\f
2333/* Implementing the Varargs Macros. */
2334
d4a6a4d9
CJW
2335static void
2336nds32_setup_incoming_varargs (cumulative_args_t ca,
ef4bddc2 2337 machine_mode mode,
d4a6a4d9
CJW
2338 tree type,
2339 int *pretend_args_size,
2340 int second_time ATTRIBUTE_UNUSED)
2341{
2342 unsigned int total_args_regs;
2343 unsigned int num_of_used_regs;
2344 unsigned int remaining_reg_count;
2345 CUMULATIVE_ARGS *cum;
2346
2347 /* If we are under hard float abi, we do not need to set *pretend_args_size.
2348 So that all nameless arguments are pushed by caller and all situation
2349 can be handled by GCC itself. */
2350 if (TARGET_HARD_FLOAT)
2351 return;
2352
2353 /* We are using NDS32_MAX_GPR_REGS_FOR_ARGS registers,
2354 counting from NDS32_GPR_ARG_FIRST_REGNUM, for saving incoming arguments.
2355 However, for nameless(anonymous) arguments, we should push them on the
2356 stack so that all the nameless arguments appear to have been passed
2357 consecutively in the memory for accessing. Hence, we need to check and
2358 exclude the registers that are used for named arguments. */
2359
2360 cum = get_cumulative_args (ca);
2361
2362 /* The MODE and TYPE describe the last argument.
2363 We need those information to determine the remaining registers
2364 for varargs. */
2365 total_args_regs
2366 = NDS32_MAX_GPR_REGS_FOR_ARGS + NDS32_GPR_ARG_FIRST_REGNUM;
2367 num_of_used_regs
2368 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
2369 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
2370
2371 remaining_reg_count = total_args_regs - num_of_used_regs;
2372 *pretend_args_size = remaining_reg_count * UNITS_PER_WORD;
2373
2374 return;
2375}
2376
9304f876
CJW
2377static bool
2378nds32_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
2379{
d4a6a4d9
CJW
2380 /* If this hook returns true, the named argument of FUNCTION_ARG is always
2381 true for named arguments, and false for unnamed arguments. */
9304f876
CJW
2382 return true;
2383}
2384
2385\f
2386/* Trampolines for Nested Functions. */
2387
2388static void
2389nds32_asm_trampoline_template (FILE *f)
2390{
2391 if (TARGET_REDUCED_REGS)
2392 {
2393 /* Trampoline is not supported on reduced-set registers yet. */
2394 sorry ("a nested function is not supported for reduced registers");
2395 }
2396 else
2397 {
2398 asm_fprintf (f, "\t! Trampoline code template\n");
2399 asm_fprintf (f, "\t! This code fragment will be copied "
2400 "into stack on demand\n");
2401
2402 asm_fprintf (f, "\tmfusr\t$r16,$pc\n");
2403 asm_fprintf (f, "\tlwi\t$r15,[$r16 + 20] "
2404 "! load nested function address\n");
2405 asm_fprintf (f, "\tlwi\t$r16,[$r16 + 16] "
2406 "! load chain_value\n");
2407 asm_fprintf (f, "\tjr\t$r15\n");
2408 }
2409
2410 /* Preserve space ($pc + 16) for saving chain_value,
2411 nds32_trampoline_init will fill the value in this slot. */
2412 asm_fprintf (f, "\t! space for saving chain_value\n");
2413 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2414
2415 /* Preserve space ($pc + 20) for saving nested function address,
2416 nds32_trampoline_init will fill the value in this slot. */
2417 asm_fprintf (f, "\t! space for saving nested function address\n");
2418 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2419}
2420
2421/* Emit RTL insns to initialize the variable parts of a trampoline. */
2422static void
2423nds32_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2424{
2425 int i;
2426
2427 /* Nested function address. */
2428 rtx fnaddr;
2429 /* The memory rtx that is going to
2430 be filled with chain_value. */
2431 rtx chain_value_mem;
2432 /* The memory rtx that is going to
2433 be filled with nested function address. */
2434 rtx nested_func_mem;
2435
2436 /* Start address of trampoline code in stack, for doing cache sync. */
2437 rtx sync_cache_addr;
2438 /* Temporary register for sync instruction. */
2439 rtx tmp_reg;
2440 /* Instruction-cache sync instruction,
2441 requesting an argument as starting address. */
2442 rtx isync_insn;
2443 /* For convenience reason of doing comparison. */
2444 int tramp_align_in_bytes;
2445
2446 /* Trampoline is not supported on reduced-set registers yet. */
2447 if (TARGET_REDUCED_REGS)
2448 sorry ("a nested function is not supported for reduced registers");
2449
2450 /* STEP 1: Copy trampoline code template into stack,
8a498f99 2451 fill up essential data into stack. */
9304f876
CJW
2452
2453 /* Extract nested function address rtx. */
2454 fnaddr = XEXP (DECL_RTL (fndecl), 0);
2455
2456 /* m_tramp is memory rtx that is going to be filled with trampoline code.
2457 We have nds32_asm_trampoline_template() to emit template pattern. */
2458 emit_block_move (m_tramp, assemble_trampoline_template (),
2459 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2460
2461 /* After copying trampoline code into stack,
2462 fill chain_value into stack. */
2463 chain_value_mem = adjust_address (m_tramp, SImode, 16);
2464 emit_move_insn (chain_value_mem, chain_value);
2465 /* After copying trampoline code int stack,
2466 fill nested function address into stack. */
2467 nested_func_mem = adjust_address (m_tramp, SImode, 20);
2468 emit_move_insn (nested_func_mem, fnaddr);
2469
2470 /* STEP 2: Sync instruction-cache. */
2471
2472 /* We have successfully filled trampoline code into stack.
2473 However, in order to execute code in stack correctly,
2474 we must sync instruction cache. */
2475 sync_cache_addr = XEXP (m_tramp, 0);
2476 tmp_reg = gen_reg_rtx (SImode);
2477 isync_insn = gen_unspec_volatile_isync (tmp_reg);
2478
2479 /* Because nds32_cache_block_size is in bytes,
2480 we get trampoline alignment in bytes for convenient comparison. */
2481 tramp_align_in_bytes = TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT;
2482
2483 if (tramp_align_in_bytes >= nds32_cache_block_size
2484 && (tramp_align_in_bytes % nds32_cache_block_size) == 0)
2485 {
2486 /* Under this condition, the starting address of trampoline
8a498f99
CJW
2487 must be aligned to the starting address of each cache block
2488 and we do not have to worry about cross-boundary issue. */
9304f876
CJW
2489 for (i = 0;
2490 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2491 / nds32_cache_block_size;
2492 i++)
2493 {
2494 emit_move_insn (tmp_reg,
2495 plus_constant (Pmode, sync_cache_addr,
2496 nds32_cache_block_size * i));
2497 emit_insn (isync_insn);
2498 }
2499 }
2500 else if (TRAMPOLINE_SIZE > nds32_cache_block_size)
2501 {
2502 /* The starting address of trampoline code
8a498f99
CJW
2503 may not be aligned to the cache block,
2504 so the trampoline code may be across two cache block.
2505 We need to sync the last element, which is 4-byte size,
2506 of trampoline template. */
9304f876
CJW
2507 for (i = 0;
2508 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2509 / nds32_cache_block_size;
2510 i++)
2511 {
2512 emit_move_insn (tmp_reg,
2513 plus_constant (Pmode, sync_cache_addr,
2514 nds32_cache_block_size * i));
2515 emit_insn (isync_insn);
2516 }
2517
2518 /* The last element of trampoline template is 4-byte size. */
2519 emit_move_insn (tmp_reg,
2520 plus_constant (Pmode, sync_cache_addr,
2521 TRAMPOLINE_SIZE - 4));
2522 emit_insn (isync_insn);
2523 }
2524 else
2525 {
2526 /* This is the simplest case.
8a498f99
CJW
2527 Because TRAMPOLINE_SIZE is less than or
2528 equal to nds32_cache_block_size,
2529 we can just sync start address and
2530 the last element of trampoline code. */
9304f876
CJW
2531
2532 /* Sync starting address of tampoline code. */
2533 emit_move_insn (tmp_reg, sync_cache_addr);
2534 emit_insn (isync_insn);
2535 /* Sync the last element, which is 4-byte size,
8a498f99 2536 of trampoline template. */
9304f876
CJW
2537 emit_move_insn (tmp_reg,
2538 plus_constant (Pmode, sync_cache_addr,
2539 TRAMPOLINE_SIZE - 4));
2540 emit_insn (isync_insn);
2541 }
2542
2543 /* Set instruction serialization barrier
2544 to guarantee the correct operations. */
2545 emit_insn (gen_unspec_volatile_isb ());
2546}
2547
2548\f
2549/* Addressing Modes. */
2550
2551static bool
ef4bddc2 2552nds32_legitimate_address_p (machine_mode mode, rtx x, bool strict)
9304f876 2553{
e2286268
MC
2554 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
2555 {
2556 /* When using floating-point instructions,
2557 we don't allow 'addr' to be [symbol_ref], [CONST] pattern. */
2558 if ((mode == DFmode || mode == SFmode)
2559 && (GET_CODE (x) == SYMBOL_REF
2560 || GET_CODE(x) == CONST))
2561 return false;
2562
2563 /* Allow [post_modify] addressing mode, when using FPU instructions. */
2564 if (GET_CODE (x) == POST_MODIFY
2565 && mode == DFmode)
2566 {
2567 if (GET_CODE (XEXP (x, 0)) == REG
2568 && GET_CODE (XEXP (x, 1)) == PLUS)
2569 {
2570 rtx plus_op = XEXP (x, 1);
2571 rtx op0 = XEXP (plus_op, 0);
2572 rtx op1 = XEXP (plus_op, 1);
2573
2574 if (nds32_address_register_rtx_p (op0, strict)
2575 && CONST_INT_P (op1))
2576 {
2577 if (satisfies_constraint_Is14 (op1))
2578 {
5f2a98c3
CJW
2579 /* If it is not under strictly aligned situation,
2580 we can return true without checking alignment. */
2581 if (!cfun->machine->strict_aligned_p)
2582 return true;
e2286268
MC
2583 /* Make sure address is word alignment.
2584 Currently we do not have 64-bit load/store yet,
2585 so we will use two 32-bit load/store instructions to do
2586 memory access and they are single word alignment. */
5f2a98c3 2587 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (op1)))
e2286268
MC
2588 return true;
2589 }
2590 }
2591 }
2592 }
2593 }
2594
9304f876
CJW
2595 /* For (mem:DI addr) or (mem:DF addr) case,
2596 we only allow 'addr' to be [reg], [symbol_ref],
8a498f99 2597 [const], or [reg + const_int] pattern. */
9304f876
CJW
2598 if (mode == DImode || mode == DFmode)
2599 {
2600 /* Allow [Reg + const_int] addressing mode. */
2601 if (GET_CODE (x) == PLUS)
2602 {
2603 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2604 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict)
2605 && CONST_INT_P (XEXP (x, 1)))
2606 return true;
9304f876
CJW
2607 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2608 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict)
2609 && CONST_INT_P (XEXP (x, 0)))
2610 return true;
2611 }
2612
e2286268
MC
2613 /* Allow [post_inc] and [post_dec] addressing mode. */
2614 if (GET_CODE (x) == POST_INC || GET_CODE (x) == POST_DEC)
2615 {
2616 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2617 return true;
2618 }
2619
9304f876
CJW
2620 /* Now check [reg], [symbol_ref], and [const]. */
2621 if (GET_CODE (x) != REG
2622 && GET_CODE (x) != SYMBOL_REF
2623 && GET_CODE (x) != CONST)
2624 return false;
2625 }
2626
2627 /* Check if 'x' is a valid address. */
2628 switch (GET_CODE (x))
2629 {
2630 case REG:
2631 /* (mem (reg A)) => [Ra] */
2632 return nds32_address_register_rtx_p (x, strict);
2633
2634 case SYMBOL_REF:
4855be84 2635 /* (mem (symbol_ref A)) => [symbol_ref] */
b26fa4f9
KLC
2636
2637 if (flag_pic || SYMBOL_REF_TLS_MODEL (x))
2638 return false;
2639
85a98076
KLC
2640 if (TARGET_ICT_MODEL_LARGE && nds32_indirect_call_referenced_p (x))
2641 return false;
2642
4855be84 2643 /* If -mcmodel=large, the 'symbol_ref' is not a valid address
8a498f99 2644 during or after LRA/reload phase. */
4855be84 2645 if (TARGET_CMODEL_LARGE
9304f876
CJW
2646 && (reload_completed
2647 || reload_in_progress
2648 || lra_in_progress))
2649 return false;
4855be84 2650 /* If -mcmodel=medium and the symbol references to rodata section,
8a498f99
CJW
2651 the 'symbol_ref' is not a valid address during or after
2652 LRA/reload phase. */
4855be84 2653 if (TARGET_CMODEL_MEDIUM
b26fa4f9
KLC
2654 && (NDS32_SYMBOL_REF_RODATA_P (x)
2655 || CONSTANT_POOL_ADDRESS_P (x))
9304f876
CJW
2656 && (reload_completed
2657 || reload_in_progress
2658 || lra_in_progress))
2659 return false;
2660
4855be84
CJW
2661 return true;
2662
2663 case CONST:
9304f876 2664 /* (mem (const (...)))
8a498f99 2665 => [ + const_addr ], where const_addr = symbol_ref + const_int */
9304f876
CJW
2666 if (GET_CODE (XEXP (x, 0)) == PLUS)
2667 {
2668 rtx plus_op = XEXP (x, 0);
2669
2670 rtx op0 = XEXP (plus_op, 0);
2671 rtx op1 = XEXP (plus_op, 1);
2672
2673 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
4855be84
CJW
2674 {
2675 /* Now we see the [ + const_addr ] pattern, but we need
8a498f99 2676 some further checking. */
b26fa4f9
KLC
2677
2678 if (flag_pic)
2679 return false;
2680
4855be84 2681 /* If -mcmodel=large, the 'const_addr' is not a valid address
8a498f99 2682 during or after LRA/reload phase. */
4855be84
CJW
2683 if (TARGET_CMODEL_LARGE
2684 && (reload_completed
2685 || reload_in_progress
2686 || lra_in_progress))
2687 return false;
2688 /* If -mcmodel=medium and the symbol references to rodata section,
8a498f99
CJW
2689 the 'const_addr' is not a valid address during or after
2690 LRA/reload phase. */
4855be84
CJW
2691 if (TARGET_CMODEL_MEDIUM
2692 && NDS32_SYMBOL_REF_RODATA_P (op0)
2693 && (reload_completed
2694 || reload_in_progress
2695 || lra_in_progress))
2696 return false;
2697
2698 /* At this point we can make sure 'const_addr' is a
2699 valid address. */
2700 return true;
2701 }
9304f876
CJW
2702 }
2703
2704 return false;
2705
2706 case POST_MODIFY:
2707 /* (mem (post_modify (reg) (plus (reg) (reg))))
8a498f99 2708 => [Ra], Rb */
9304f876 2709 /* (mem (post_modify (reg) (plus (reg) (const_int))))
8a498f99 2710 => [Ra], const_int */
9304f876
CJW
2711 if (GET_CODE (XEXP (x, 0)) == REG
2712 && GET_CODE (XEXP (x, 1)) == PLUS)
2713 {
2714 rtx plus_op = XEXP (x, 1);
2715
2716 rtx op0 = XEXP (plus_op, 0);
2717 rtx op1 = XEXP (plus_op, 1);
2718
2719 if (nds32_address_register_rtx_p (op0, strict)
2720 && nds32_legitimate_index_p (mode, op1, strict))
2721 return true;
2722 else
2723 return false;
2724 }
2725
2726 return false;
2727
2728 case POST_INC:
2729 case POST_DEC:
2730 /* (mem (post_inc reg)) => [Ra], 1/2/4 */
2731 /* (mem (post_dec reg)) => [Ra], -1/-2/-4 */
2732 /* The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
8a498f99 2733 We only need to deal with register Ra. */
9304f876
CJW
2734 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2735 return true;
2736 else
2737 return false;
2738
2739 case PLUS:
2740 /* (mem (plus reg const_int))
8a498f99 2741 => [Ra + imm] */
9304f876 2742 /* (mem (plus reg reg))
8a498f99 2743 => [Ra + Rb] */
9304f876 2744 /* (mem (plus (mult reg const_int) reg))
8a498f99 2745 => [Ra + Rb << sv] */
9304f876
CJW
2746 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2747 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict))
2748 return true;
2749 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2750 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict))
2751 return true;
2752 else
2753 return false;
2754
2755 case LO_SUM:
34425025 2756 /* (mem (lo_sum (reg) (symbol_ref))) */
85a98076 2757 /* (mem (lo_sum (reg) (const (plus (symbol_ref) (reg)))) */
b26fa4f9
KLC
2758 /* TLS case: (mem (lo_sum (reg) (const (unspec symbol_ref X)))) */
2759 /* The LO_SUM is a valid address if and only if we would like to
2760 generate 32-bit full address memory access with any of following
2761 circumstance:
2762 1. -mcmodel=large.
2763 2. -mcmodel=medium and the symbol_ref references to rodata. */
85a98076
KLC
2764 {
2765 rtx sym = NULL_RTX;
2766
b26fa4f9
KLC
2767 if (flag_pic)
2768 return false;
2769
85a98076
KLC
2770 if (!REG_P (XEXP (x, 0)))
2771 return false;
2772
2773 if (GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
2774 sym = XEXP (x, 1);
2775 else if (GET_CODE (XEXP (x, 1)) == CONST)
2776 {
2777 rtx plus = XEXP(XEXP (x, 1), 0);
2778 if (GET_CODE (plus) == PLUS)
2779 sym = XEXP (plus, 0);
2780 else if (GET_CODE (plus) == UNSPEC)
2781 sym = XVECEXP (plus, 0, 0);
2782 }
2783 else
2784 return false;
2785
2786 gcc_assert (GET_CODE (sym) == SYMBOL_REF);
2787
2788 if (TARGET_ICT_MODEL_LARGE
2789 && nds32_indirect_call_referenced_p (sym))
2790 return true;
2791
2792 if (TARGET_CMODEL_LARGE)
2793 return true;
2794 else if (TARGET_CMODEL_MEDIUM
2795 && NDS32_SYMBOL_REF_RODATA_P (sym))
2796 return true;
2797 else
2798 return false;
2799 }
9304f876
CJW
2800
2801 default:
2802 return false;
2803 }
2804}
2805
85a98076
KLC
2806static rtx
2807nds32_legitimize_address (rtx x,
2808 rtx oldx ATTRIBUTE_UNUSED,
2809 machine_mode mode ATTRIBUTE_UNUSED)
2810{
b26fa4f9
KLC
2811 if (nds32_tls_referenced_p (x))
2812 x = nds32_legitimize_tls_address (x);
2813 else if (flag_pic && SYMBOLIC_CONST_P (x))
2814 x = nds32_legitimize_pic_address (x);
2815 else if (TARGET_ICT_MODEL_LARGE && nds32_indirect_call_referenced_p (x))
85a98076
KLC
2816 x = nds32_legitimize_ict_address (x);
2817
2818 return x;
2819}
2820
2821static bool
2822nds32_legitimate_constant_p (machine_mode mode, rtx x)
2823{
2824 switch (GET_CODE (x))
2825 {
2826 case CONST_DOUBLE:
2827 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
2828 && (mode == DFmode || mode == SFmode))
2829 return false;
2830 break;
2831 case CONST:
2832 x = XEXP (x, 0);
2833
2834 if (GET_CODE (x) == PLUS)
2835 {
2836 if (!CONST_INT_P (XEXP (x, 1)))
2837 return false;
2838 x = XEXP (x, 0);
2839 }
2840
2841 if (GET_CODE (x) == UNSPEC)
2842 {
2843 switch (XINT (x, 1))
2844 {
b26fa4f9
KLC
2845 case UNSPEC_GOT:
2846 case UNSPEC_GOTOFF:
2847 case UNSPEC_PLT:
2848 case UNSPEC_TLSGD:
2849 case UNSPEC_TLSLD:
2850 case UNSPEC_TLSIE:
2851 case UNSPEC_TLSLE:
85a98076
KLC
2852 case UNSPEC_ICT:
2853 return false;
2854 default:
2855 return true;
2856 }
2857 }
2858 break;
b26fa4f9
KLC
2859 case SYMBOL_REF:
2860 /* TLS symbols need a call to resolve in
2861 precompute_register_parameters. */
2862 if (SYMBOL_REF_TLS_MODEL (x))
2863 return false;
2864 break;
85a98076
KLC
2865 default:
2866 return true;
2867 }
2868
2869 return true;
2870}
2871
2872/* Reorgnize the UNSPEC CONST and return its direct symbol. */
2873static rtx
2874nds32_delegitimize_address (rtx x)
2875{
2876 x = delegitimize_mem_from_attrs (x);
2877
2878 if (GET_CODE(x) == CONST)
2879 {
2880 rtx inner = XEXP (x, 0);
2881
2882 /* Handle for GOTOFF. */
2883 if (GET_CODE (inner) == PLUS)
2884 inner = XEXP (inner, 0);
2885
2886 if (GET_CODE (inner) == UNSPEC)
2887 {
2888 switch (XINT (inner, 1))
2889 {
b26fa4f9
KLC
2890 case UNSPEC_GOTINIT:
2891 case UNSPEC_GOT:
2892 case UNSPEC_GOTOFF:
2893 case UNSPEC_PLT:
2894 case UNSPEC_TLSGD:
2895 case UNSPEC_TLSLD:
2896 case UNSPEC_TLSIE:
2897 case UNSPEC_TLSLE:
85a98076
KLC
2898 case UNSPEC_ICT:
2899 x = XVECEXP (inner, 0, 0);
2900 break;
2901 default:
2902 break;
2903 }
2904 }
2905 }
2906 return x;
2907}
2908
7c32ef41
MC
2909static machine_mode
2910nds32_vectorize_preferred_simd_mode (scalar_mode mode)
2911{
2912 if (!NDS32_EXT_DSP_P ())
2913 return word_mode;
2914
2915 switch (mode)
2916 {
2917 case E_QImode:
2918 return V4QImode;
2919 case E_HImode:
2920 return V2HImode;
2921 default:
2922 return word_mode;
2923 }
2924}
2925
85a98076
KLC
2926static bool
2927nds32_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2928{
2929 switch (GET_CODE (x))
2930 {
2931 case CONST:
2932 return !nds32_legitimate_constant_p (mode, x);
2933 case SYMBOL_REF:
2934 /* All symbols have to be accessed through gp-relative in PIC mode. */
2935 /* We don't want to force symbol as constant pool in .text section,
2936 because we use the gp-relatived instruction to load in small
2937 or medium model. */
b26fa4f9
KLC
2938 if (flag_pic
2939 || SYMBOL_REF_TLS_MODEL (x)
85a98076
KLC
2940 || TARGET_CMODEL_SMALL
2941 || TARGET_CMODEL_MEDIUM)
2942 return true;
2943 break;
b26fa4f9
KLC
2944 case CONST_INT:
2945 case CONST_DOUBLE:
2946 if (flag_pic && (lra_in_progress || reload_completed))
2947 return true;
2948 break;
85a98076
KLC
2949 default:
2950 return false;
2951 }
2952 return false;
2953}
2954
5b6f2bf3
CJW
2955\f
2956/* Condition Code Status. */
2957
2958/* -- Representation of condition codes using registers. */
2959
2960static void
2961nds32_canonicalize_comparison (int *code,
2962 rtx *op0 ATTRIBUTE_UNUSED,
2963 rtx *op1,
2964 bool op0_preserve_value ATTRIBUTE_UNUSED)
2965{
2966 /* When the instruction combination pass tries to combine a comparison insn
2967 with its previous insns, it also transforms the operator in order to
2968 minimize its constant field. For example, it tries to transform a
2969 comparison insn from
2970 (set (reg:SI 54)
2971 (ltu:SI (reg:SI 52)
2972 (const_int 10 [0xa])))
2973 to
2974 (set (reg:SI 54)
2975 (leu:SI (reg:SI 52)
2976 (const_int 9 [0x9])))
2977
2978 However, the nds32 target only provides instructions supporting the LTU
2979 operation directly, and the implementation of the pattern "cbranchsi4"
2980 only expands the LTU form. In order to handle the non-LTU operations
2981 generated from passes other than the RTL expansion pass, we have to
2982 implement this hook to revert those changes. Since we only expand the LTU
2983 operator in the RTL expansion pass, we might only need to handle the LEU
2984 case, unless we find other optimization passes perform more aggressive
2985 transformations. */
2986
2987 if (*code == LEU && CONST_INT_P (*op1))
2988 {
2989 *op1 = gen_int_mode (INTVAL (*op1) + 1, SImode);
2990 *code = LTU;
2991 }
2992}
2993
9304f876
CJW
2994\f
2995/* Describing Relative Costs of Operations. */
2996
19ac960a 2997static int
69e7672a 2998nds32_register_move_cost (machine_mode mode,
19ac960a
CJW
2999 reg_class_t from,
3000 reg_class_t to)
9304f876 3001{
69e7672a
KC
3002 /* In garywolf cpu, FPR to GPR is chaper than other cpu. */
3003 if (TARGET_PIPELINE_GRAYWOLF)
3004 {
3005 if (GET_MODE_SIZE (mode) == 8)
3006 {
3007 /* DPR to GPR. */
3008 if (from == FP_REGS && to != FP_REGS)
3009 return 3;
3010 /* GPR to DPR. */
3011 if (from != FP_REGS && to == FP_REGS)
3012 return 2;
3013 }
3014 else
3015 {
3016 if ((from == FP_REGS && to != FP_REGS)
3017 || (from != FP_REGS && to == FP_REGS))
3018 return 2;
3019 }
3020 }
3021
e2286268
MC
3022 if ((from == FP_REGS && to != FP_REGS)
3023 || (from != FP_REGS && to == FP_REGS))
69e7672a 3024 return 3;
e2286268
MC
3025 else if (from == HIGH_REGS || to == HIGH_REGS)
3026 return optimize_size ? 6 : 2;
3027 else
3028 return 2;
9304f876
CJW
3029}
3030
19ac960a 3031static int
ef4bddc2 3032nds32_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
19ac960a
CJW
3033 reg_class_t rclass ATTRIBUTE_UNUSED,
3034 bool in ATTRIBUTE_UNUSED)
9304f876
CJW
3035{
3036 return 8;
3037}
3038
3039/* This target hook describes the relative costs of RTL expressions.
3040 Return 'true' when all subexpressions of x have been processed.
3041 Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
3042 Refer to gcc/rtlanal.c for more information. */
3043static bool
3044nds32_rtx_costs (rtx x,
e548c9df 3045 machine_mode mode,
9304f876 3046 int outer_code,
89a4b547 3047 int opno,
9304f876
CJW
3048 int *total,
3049 bool speed)
3050{
e548c9df 3051 return nds32_rtx_costs_impl (x, mode, outer_code, opno, total, speed);
9304f876
CJW
3052}
3053
19ac960a
CJW
3054static int
3055nds32_address_cost (rtx address,
ef4bddc2 3056 machine_mode mode,
19ac960a
CJW
3057 addr_space_t as,
3058 bool speed)
9304f876 3059{
89a4b547 3060 return nds32_address_cost_impl (address, mode, as, speed);
9304f876
CJW
3061}
3062
511a41d7
CJW
3063\f
3064/* Dividing the Output into Sections (Texts, Data, . . . ). */
3065
3066/* If references to a symbol or a constant must be treated differently
3067 depending on something about the variable or function named by the symbol
3068 (such as what section it is in), we use this hook to store flags
3069 in symbol_ref rtx. */
3070static void
3071nds32_encode_section_info (tree decl, rtx rtl, int new_decl_p)
3072{
3073 default_encode_section_info (decl, rtl, new_decl_p);
3074
3075 /* For the memory rtx, if it references to rodata section, we can store
3076 NDS32_SYMBOL_FLAG_RODATA flag into symbol_ref rtx so that the
3077 nds32_legitimate_address_p() can determine how to treat such symbol_ref
3078 based on -mcmodel=X and this information. */
3079 if (MEM_P (rtl) && MEM_READONLY_P (rtl))
3080 {
3081 rtx addr = XEXP (rtl, 0);
3082
3083 if (GET_CODE (addr) == SYMBOL_REF)
3084 {
3085 /* For (mem (symbol_ref X)) case. */
3086 SYMBOL_REF_FLAGS (addr) |= NDS32_SYMBOL_FLAG_RODATA;
3087 }
3088 else if (GET_CODE (addr) == CONST
3089 && GET_CODE (XEXP (addr, 0)) == PLUS)
3090 {
3091 /* For (mem (const (plus (symbol_ref X) (const_int N)))) case. */
3092 rtx plus_op = XEXP (addr, 0);
3093 rtx op0 = XEXP (plus_op, 0);
3094 rtx op1 = XEXP (plus_op, 1);
3095
3096 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
3097 SYMBOL_REF_FLAGS (op0) |= NDS32_SYMBOL_FLAG_RODATA;
3098 }
3099 }
3100}
3101
9304f876
CJW
3102\f
3103/* Defining the Output Assembler Language. */
3104
3105/* -- The Overall Framework of an Assembler File. */
3106
3107static void
3108nds32_asm_file_start (void)
3109{
9304f876
CJW
3110 default_file_start ();
3111
b26fa4f9
KLC
3112 if (flag_pic)
3113 fprintf (asm_out_file, "\t.pic\n");
3114
9304f876
CJW
3115 /* Tell assembler which ABI we are using. */
3116 fprintf (asm_out_file, "\t! ABI version\n");
e2286268
MC
3117 if (TARGET_HARD_FLOAT)
3118 fprintf (asm_out_file, "\t.abi_2fp_plus\n");
3119 else
3120 fprintf (asm_out_file, "\t.abi_2\n");
9304f876
CJW
3121
3122 /* Tell assembler that this asm code is generated by compiler. */
3123 fprintf (asm_out_file, "\t! This asm file is generated by compiler\n");
3124 fprintf (asm_out_file, "\t.flag\tverbatim\n");
85a98076 3125
cf3cd43d
CJW
3126 /* Insert directive for linker to distinguish object's ict flag. */
3127 if (!TARGET_LINUX_ABI)
3128 {
3129 if (TARGET_ICT_MODEL_LARGE)
3130 fprintf (asm_out_file, "\t.ict_model\tlarge\n");
3131 else
3132 fprintf (asm_out_file, "\t.ict_model\tsmall\n");
3133 }
3134
3135 /* We need to provide the size of each vector for interrupt handler
3136 under elf toolchain. */
3137 if (!TARGET_LINUX_ABI)
3138 {
3139 fprintf (asm_out_file, "\t! This vector size directive is required "
3140 "for checking inconsistency on interrupt handler\n");
3141 fprintf (asm_out_file, "\t.vec_size\t%d\n", nds32_isr_vector_size);
3142 }
9304f876 3143
2140297c
CJW
3144 /* If user enables '-mforce-fp-as-gp' or compiles programs with -Os,
3145 the compiler may produce 'la $fp,_FP_BASE_' instruction
3146 at prologue for fp-as-gp optimization.
3147 We should emit weak reference of _FP_BASE_ to avoid undefined reference
3148 in case user does not pass '--relax' option to linker. */
cf3cd43d 3149 if (!TARGET_LINUX_ABI && (TARGET_FORCE_FP_AS_GP || optimize_size))
2140297c
CJW
3150 {
3151 fprintf (asm_out_file, "\t! This weak reference is required to do "
3152 "fp-as-gp link time optimization\n");
3153 fprintf (asm_out_file, "\t.weak\t_FP_BASE_\n");
3154 }
3155
9304f876
CJW
3156 fprintf (asm_out_file, "\t! ------------------------------------\n");
3157
3158 if (TARGET_ISA_V2)
3159 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V2");
3160 if (TARGET_ISA_V3)
3161 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3");
3162 if (TARGET_ISA_V3M)
3163 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3M");
3164
dd000841
CJW
3165 switch (nds32_cpu_option)
3166 {
3167 case CPU_N6:
3168 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N6");
3169 break;
3170
3171 case CPU_N7:
3172 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N7");
3173 break;
3174
3175 case CPU_N8:
3176 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N8");
3177 break;
3178
3179 case CPU_E8:
3180 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "E8");
3181 break;
3182
3183 case CPU_N9:
3184 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N9");
3185 break;
3186
3187 case CPU_N10:
3188 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N10");
3189 break;
3190
3191 case CPU_GRAYWOLF:
3192 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "Graywolf");
3193 break;
3194
3195 case CPU_N12:
3196 case CPU_N13:
3197 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N13");
3198 break;
3199
3200 case CPU_SIMPLE:
3201 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "SIMPLE");
3202 break;
3203
3204 default:
3205 gcc_unreachable ();
3206 }
3207
77b7a1ca
CJW
3208 if (TARGET_CMODEL_SMALL)
3209 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "SMALL");
3210 if (TARGET_CMODEL_MEDIUM)
3211 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "MEDIUM");
3212 if (TARGET_CMODEL_LARGE)
3213 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "LARGE");
3214
9304f876
CJW
3215 fprintf (asm_out_file, "\t! Endian setting\t: %s\n",
3216 ((TARGET_BIG_ENDIAN) ? "big-endian"
3217 : "little-endian"));
e2286268
MC
3218 fprintf (asm_out_file, "\t! Use SP floating-point instruction\t: %s\n",
3219 ((TARGET_FPU_SINGLE) ? "Yes"
3220 : "No"));
3221 fprintf (asm_out_file, "\t! Use DP floating-point instruction\t: %s\n",
3222 ((TARGET_FPU_DOUBLE) ? "Yes"
3223 : "No"));
3224 fprintf (asm_out_file, "\t! ABI version\t\t: %s\n",
3225 ((TARGET_HARD_FLOAT) ? "ABI2FP+"
3226 : "ABI2"));
9304f876
CJW
3227
3228 fprintf (asm_out_file, "\t! ------------------------------------\n");
3229
3230 fprintf (asm_out_file, "\t! Use conditional move\t\t: %s\n",
3231 ((TARGET_CMOV) ? "Yes"
3232 : "No"));
3233 fprintf (asm_out_file, "\t! Use performance extension\t: %s\n",
aa4b851c 3234 ((TARGET_EXT_PERF) ? "Yes"
9304f876 3235 : "No"));
aa4b851c
CJW
3236 fprintf (asm_out_file, "\t! Use performance extension 2\t: %s\n",
3237 ((TARGET_EXT_PERF2) ? "Yes"
3238 : "No"));
3239 fprintf (asm_out_file, "\t! Use string extension\t\t: %s\n",
3240 ((TARGET_EXT_STRING) ? "Yes"
3241 : "No"));
9304f876
CJW
3242
3243 fprintf (asm_out_file, "\t! ------------------------------------\n");
3244
3245 fprintf (asm_out_file, "\t! V3PUSH instructions\t: %s\n",
3246 ((TARGET_V3PUSH) ? "Yes"
3247 : "No"));
3248 fprintf (asm_out_file, "\t! 16-bit instructions\t: %s\n",
3249 ((TARGET_16_BIT) ? "Yes"
3250 : "No"));
9304f876
CJW
3251 fprintf (asm_out_file, "\t! Reduced registers set\t: %s\n",
3252 ((TARGET_REDUCED_REGS) ? "Yes"
3253 : "No"));
3254
bc8a8810
MC
3255 fprintf (asm_out_file, "\t! Support unaligned access\t\t: %s\n",
3256 (flag_unaligned_access ? "Yes"
3257 : "No"));
3258
9304f876
CJW
3259 fprintf (asm_out_file, "\t! ------------------------------------\n");
3260
3261 if (optimize_size)
3262 fprintf (asm_out_file, "\t! Optimization level\t: -Os\n");
10c8bd03
CJW
3263 else if (optimize_fast)
3264 fprintf (asm_out_file, "\t! Optimization level\t: -Ofast\n");
3265 else if (optimize_debug)
3266 fprintf (asm_out_file, "\t! Optimization level\t: -Og\n");
9304f876
CJW
3267 else
3268 fprintf (asm_out_file, "\t! Optimization level\t: -O%d\n", optimize);
3269
3270 fprintf (asm_out_file, "\t! ------------------------------------\n");
3271
3272 fprintf (asm_out_file, "\t! Cache block size\t: %d\n",
3273 nds32_cache_block_size);
3274
3275 fprintf (asm_out_file, "\t! ------------------------------------\n");
3276
c23a919b 3277 nds32_asm_file_start_for_isr ();
9304f876
CJW
3278}
3279
3280static void
3281nds32_asm_file_end (void)
3282{
c23a919b 3283 nds32_asm_file_end_for_isr ();
9304f876 3284
cf3cd43d
CJW
3285 /* The NDS32 Linux stack is mapped non-executable by default, so add a
3286 .note.GNU-stack section. */
3287 if (TARGET_LINUX_ABI)
3288 file_end_indicate_exec_stack ();
3289
9304f876
CJW
3290 fprintf (asm_out_file, "\t! ------------------------------------\n");
3291}
3292
85a98076
KLC
3293static bool
3294nds32_asm_output_addr_const_extra (FILE *file, rtx x)
3295{
3296 if (GET_CODE (x) == UNSPEC)
3297 {
3298 switch (XINT (x, 1))
3299 {
b26fa4f9
KLC
3300 case UNSPEC_GOTINIT:
3301 output_addr_const (file, XVECEXP (x, 0, 0));
3302 break;
3303 case UNSPEC_GOTOFF:
3304 output_addr_const (file, XVECEXP (x, 0, 0));
3305 fputs ("@GOTOFF", file);
3306 break;
3307 case UNSPEC_GOT:
3308 output_addr_const (file, XVECEXP (x, 0, 0));
3309 fputs ("@GOT", file);
3310 break;
3311 case UNSPEC_PLT:
3312 output_addr_const (file, XVECEXP (x, 0, 0));
3313 fputs ("@PLT", file);
3314 break;
3315 case UNSPEC_TLSGD:
3316 output_addr_const (file, XVECEXP (x, 0, 0));
3317 fputs ("@TLSDESC", file);
3318 break;
3319 case UNSPEC_TLSLD:
3320 output_addr_const (file, XVECEXP (x, 0, 0));
3321 fputs ("@TLSDESC", file);
3322 break;
3323 case UNSPEC_TLSIE:
3324 output_addr_const (file, XVECEXP (x, 0, 0));
3325 fputs ("@GOTTPOFF", file);
3326 break;
3327 case UNSPEC_TLSLE:
3328 output_addr_const (file, XVECEXP (x, 0, 0));
3329 fputs ("@TPOFF", file);
3330 break;
85a98076
KLC
3331 case UNSPEC_ICT:
3332 output_addr_const (file, XVECEXP (x, 0, 0));
3333 fputs ("@ICT", file);
3334 break;
3335 default:
3336 return false;
3337 }
3338 return true;
3339 }
3340 else
3341 return false;
3342}
3343
9304f876
CJW
3344/* -- Output and Generation of Labels. */
3345
3346static void
3347nds32_asm_globalize_label (FILE *stream, const char *name)
3348{
3349 fputs ("\t.global\t", stream);
3350 assemble_name (stream, name);
3351 fputs ("\n", stream);
3352}
3353
3354/* -- Output of Assembler Instructions. */
3355
3356static void
3357nds32_print_operand (FILE *stream, rtx x, int code)
3358{
bfeb6685 3359 HOST_WIDE_INT op_value = 0;
a3b13564
KC
3360 HOST_WIDE_INT one_position;
3361 HOST_WIDE_INT zero_position;
e2286268
MC
3362 bool pick_lsb_p = false;
3363 bool pick_msb_p = false;
3364 int regno;
3365
bfeb6685
CJW
3366 if (CONST_INT_P (x))
3367 op_value = INTVAL (x);
9304f876
CJW
3368
3369 switch (code)
3370 {
3371 case 0 :
3372 /* Do nothing special. */
3373 break;
3374
a3b13564
KC
3375 case 'b':
3376 /* Use exact_log2() to search the 0-bit position. */
3377 gcc_assert (CONST_INT_P (x));
3378 zero_position = exact_log2 (~UINTVAL (x) & GET_MODE_MASK (SImode));
3379 gcc_assert (zero_position != -1);
3380 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, zero_position);
3381
3382 /* No need to handle following process, so return immediately. */
3383 return;
3384
8656bbcf
SC
3385 case 'e':
3386 gcc_assert (MEM_P (x)
3387 && GET_CODE (XEXP (x, 0)) == PLUS
3388 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
3389 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (XEXP (x, 0), 1)));
3390
3391 /* No need to handle following process, so return immediately. */
3392 return;
7c32ef41
MC
3393
3394 case 'v':
3395 gcc_assert (CONST_INT_P (x)
3396 && (INTVAL (x) == 0
3397 || INTVAL (x) == 8
3398 || INTVAL (x) == 16
3399 || INTVAL (x) == 24));
3400 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) / 8);
3401
3402 /* No need to handle following process, so return immediately. */
3403 return;
3404
a3b13564
KC
3405 case 'B':
3406 /* Use exact_log2() to search the 1-bit position. */
3407 gcc_assert (CONST_INT_P (x));
3408 one_position = exact_log2 (UINTVAL (x) & GET_MODE_MASK (SImode));
3409 gcc_assert (one_position != -1);
3410 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, one_position);
3411
3412 /* No need to handle following process, so return immediately. */
3413 return;
3414
e2286268
MC
3415 case 'L':
3416 /* X is supposed to be REG rtx. */
3417 gcc_assert (REG_P (x));
3418 /* Claim that we are going to pick LSB part of X. */
3419 pick_lsb_p = true;
3420 break;
3421
3422 case 'H':
3423 /* X is supposed to be REG rtx. */
3424 gcc_assert (REG_P (x));
3425 /* Claim that we are going to pick MSB part of X. */
3426 pick_msb_p = true;
3427 break;
3428
9304f876
CJW
3429 case 'V':
3430 /* 'x' is supposed to be CONST_INT, get the value. */
3431 gcc_assert (CONST_INT_P (x));
9304f876
CJW
3432
3433 /* According to the Andes architecture,
8a498f99
CJW
3434 the system/user register index range is 0 ~ 1023.
3435 In order to avoid conflict between user-specified-integer value
3436 and enum-specified-register value,
3437 the 'enum nds32_intrinsic_registers' value
3438 in nds32_intrinsic.h starts from 1024. */
9304f876
CJW
3439 if (op_value < 1024 && op_value >= 0)
3440 {
3441 /* If user gives integer value directly (0~1023),
3442 we just print out the value. */
5af50159 3443 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, op_value);
9304f876
CJW
3444 }
3445 else if (op_value < 0
3446 || op_value >= ((int) ARRAY_SIZE (nds32_intrinsic_register_names)
3447 + 1024))
3448 {
3449 /* The enum index value for array size is out of range. */
3450 error ("intrinsic register index is out of range");
3451 }
3452 else
3453 {
3454 /* If user applies normal way with __NDS32_REG_XXX__ enum data,
3455 we can print out register name. Remember to substract 1024. */
3456 fprintf (stream, "%s",
3457 nds32_intrinsic_register_names[op_value - 1024]);
3458 }
3459
3460 /* No need to handle following process, so return immediately. */
3461 return;
3462
f1a0afe2
MC
3463 case 'R': /* cctl valck */
3464 /* Note the cctl divide to 5 group and share the same name table. */
3465 if (op_value < 0 || op_value > 4)
3466 error ("CCTL intrinsic function subtype out of range!");
3467 fprintf (stream, "%s", nds32_cctl_names[op_value]);
3468 return;
3469
3470 case 'T': /* cctl idxwbinv */
3471 /* Note the cctl divide to 5 group and share the same name table. */
3472 if (op_value < 0 || op_value > 4)
3473 error ("CCTL intrinsic function subtype out of range!");
3474 fprintf (stream, "%s", nds32_cctl_names[op_value + 4]);
3475 return;
3476
3477 case 'U': /* cctl vawbinv */
3478 /* Note the cctl divide to 5 group and share the same name table. */
3479 if (op_value < 0 || op_value > 4)
3480 error ("CCTL intrinsic function subtype out of range!");
3481 fprintf (stream, "%s", nds32_cctl_names[op_value + 8]);
3482 return;
3483
3484 case 'X': /* cctl idxread */
3485 /* Note the cctl divide to 5 group and share the same name table. */
3486 if (op_value < 0 || op_value > 4)
3487 error ("CCTL intrinsic function subtype out of range!");
3488 fprintf (stream, "%s", nds32_cctl_names[op_value + 12]);
3489 return;
3490
3491 case 'W': /* cctl idxwitre */
3492 /* Note the cctl divide to 5 group and share the same name table. */
3493 if (op_value < 0 || op_value > 4)
3494 error ("CCTL intrinsic function subtype out of range!");
3495 fprintf (stream, "%s", nds32_cctl_names[op_value + 16]);
3496 return;
3497
57aaf0cc
MC
3498 case 'Z': /* dpref */
3499 fprintf (stream, "%s", nds32_dpref_names[op_value]);
3500 return;
3501
9304f876
CJW
3502 default :
3503 /* Unknown flag. */
3504 output_operand_lossage ("invalid operand output code");
3505 break;
3506 }
3507
3508 switch (GET_CODE (x))
3509 {
3510 case LABEL_REF:
85a98076
KLC
3511 output_addr_const (stream, x);
3512 break;
3513
9304f876
CJW
3514 case SYMBOL_REF:
3515 output_addr_const (stream, x);
85a98076 3516
cf3cd43d 3517 if (!TARGET_LINUX_ABI && nds32_indirect_call_referenced_p (x))
85a98076
KLC
3518 fprintf (stream, "@ICT");
3519
9304f876
CJW
3520 break;
3521
3522 case REG:
e2286268
MC
3523 /* Print a Double-precision register name. */
3524 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3525 && NDS32_IS_FPR_REGNUM (REGNO (x)))
3526 {
3527 regno = REGNO (x);
3528 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno))
3529 {
3530 output_operand_lossage ("invalid operand for code '%c'", code);
3531 break;
3532 }
3533 fprintf (stream, "$fd%d", (regno - NDS32_FIRST_FPR_REGNUM) >> 1);
3534 break;
3535 }
3536
3537 /* Print LSB or MSB part of register pair if the
3538 constraint modifier 'L' or 'H' is specified. */
3539 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3540 && NDS32_IS_GPR_REGNUM (REGNO (x)))
3541 {
3542 if ((pick_lsb_p && WORDS_BIG_ENDIAN)
3543 || (pick_msb_p && !WORDS_BIG_ENDIAN))
3544 {
3545 /* If we would like to print out LSB register under big-endian,
3546 or print out MSB register under little-endian, we need to
3547 increase register number. */
3548 regno = REGNO (x);
3549 regno++;
3550 fputs (reg_names[regno], stream);
3551 break;
3552 }
3553 }
3554
9304f876 3555 /* Forbid using static chain register ($r16)
8a498f99 3556 on reduced-set registers configuration. */
9304f876
CJW
3557 if (TARGET_REDUCED_REGS
3558 && REGNO (x) == STATIC_CHAIN_REGNUM)
3559 sorry ("a nested function is not supported for reduced registers");
3560
3561 /* Normal cases, print out register name. */
3562 fputs (reg_names[REGNO (x)], stream);
3563 break;
3564
3565 case MEM:
cc8ca59e 3566 output_address (GET_MODE (x), XEXP (x, 0));
9304f876
CJW
3567 break;
3568
e2286268
MC
3569 case HIGH:
3570 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE)
3571 {
3572 const REAL_VALUE_TYPE *rv;
3573 long val;
3574 gcc_assert (GET_MODE (x) == SFmode);
3575
3576 rv = CONST_DOUBLE_REAL_VALUE (XEXP (x, 0));
3577 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3578
3579 fprintf (stream, "hi20(0x%lx)", val);
3580 }
3581 else
3582 gcc_unreachable ();
3583 break;
3584
3585 case CONST_DOUBLE:
3586 const REAL_VALUE_TYPE *rv;
3587 long val;
3588 gcc_assert (GET_MODE (x) == SFmode);
3589
3590 rv = CONST_DOUBLE_REAL_VALUE (x);
3591 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3592
3593 fprintf (stream, "0x%lx", val);
3594 break;
3595
9304f876
CJW
3596 case CODE_LABEL:
3597 case CONST_INT:
3598 case CONST:
3599 output_addr_const (stream, x);
3600 break;
3601
7c32ef41
MC
3602 case CONST_VECTOR:
3603 fprintf (stream, HOST_WIDE_INT_PRINT_HEX, const_vector_to_hwint (x));
3604 break;
3605
85a98076
KLC
3606 case LO_SUM:
3607 /* This is a special case for inline assembly using memory address 'p'.
3608 The inline assembly code is expected to use pesudo instruction
3609 for the operand. EX: la */
3610 output_addr_const (stream, XEXP(x, 1));
3611 break;
3612
9304f876
CJW
3613 default:
3614 /* Generally, output_addr_const () is able to handle most cases.
8a498f99
CJW
3615 We want to see what CODE could appear,
3616 so we use gcc_unreachable() to stop it. */
9304f876
CJW
3617 debug_rtx (x);
3618 gcc_unreachable ();
3619 break;
3620 }
3621}
3622
3623static void
85a98076
KLC
3624nds32_print_operand_address (FILE *stream,
3625 machine_mode mode ATTRIBUTE_UNUSED,
3626 rtx x)
9304f876
CJW
3627{
3628 rtx op0, op1;
3629
3630 switch (GET_CODE (x))
3631 {
3632 case SYMBOL_REF:
3633 case CONST:
3634 /* [ + symbol_ref] */
3635 /* [ + const_addr], where const_addr = symbol_ref + const_int */
3636 fputs ("[ + ", stream);
3637 output_addr_const (stream, x);
3638 fputs ("]", stream);
3639 break;
3640
85a98076
KLC
3641 case LO_SUM:
3642 /* This is a special case for inline assembly using memory operand 'm'.
3643 The inline assembly code is expected to use pesudo instruction
3644 for the operand. EX: [ls].[bhw] */
3645 fputs ("[ + ", stream);
3646 op1 = XEXP (x, 1);
3647 output_addr_const (stream, op1);
3648 fputs ("]", stream);
3649 break;
3650
9304f876
CJW
3651 case REG:
3652 /* Forbid using static chain register ($r16)
8a498f99 3653 on reduced-set registers configuration. */
9304f876
CJW
3654 if (TARGET_REDUCED_REGS
3655 && REGNO (x) == STATIC_CHAIN_REGNUM)
3656 sorry ("a nested function is not supported for reduced registers");
3657
3658 /* [Ra] */
3659 fprintf (stream, "[%s]", reg_names[REGNO (x)]);
3660 break;
3661
3662 case PLUS:
3663 op0 = XEXP (x, 0);
3664 op1 = XEXP (x, 1);
3665
3666 /* Checking op0, forbid using static chain register ($r16)
8a498f99 3667 on reduced-set registers configuration. */
9304f876
CJW
3668 if (TARGET_REDUCED_REGS
3669 && REG_P (op0)
3670 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3671 sorry ("a nested function is not supported for reduced registers");
3672 /* Checking op1, forbid using static chain register ($r16)
8a498f99 3673 on reduced-set registers configuration. */
9304f876
CJW
3674 if (TARGET_REDUCED_REGS
3675 && REG_P (op1)
3676 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3677 sorry ("a nested function is not supported for reduced registers");
3678
3679 if (REG_P (op0) && CONST_INT_P (op1))
3680 {
3681 /* [Ra + imm] */
5af50159
KC
3682 fprintf (stream, "[%s + (" HOST_WIDE_INT_PRINT_DEC ")]",
3683 reg_names[REGNO (op0)], INTVAL (op1));
9304f876
CJW
3684 }
3685 else if (REG_P (op0) && REG_P (op1))
3686 {
3687 /* [Ra + Rb] */
3688 fprintf (stream, "[%s + %s]",
3689 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3690 }
3691 else if (GET_CODE (op0) == MULT && REG_P (op1))
3692 {
3693 /* [Ra + Rb << sv]
3694 From observation, the pattern looks like:
3695 (plus:SI (mult:SI (reg:SI 58)
8a498f99
CJW
3696 (const_int 4 [0x4]))
3697 (reg/f:SI 57)) */
9304f876
CJW
3698 int sv;
3699
3700 /* We need to set sv to output shift value. */
3701 if (INTVAL (XEXP (op0, 1)) == 1)
3702 sv = 0;
3703 else if (INTVAL (XEXP (op0, 1)) == 2)
3704 sv = 1;
3705 else if (INTVAL (XEXP (op0, 1)) == 4)
3706 sv = 2;
f62a2af5
CJW
3707 else if (INTVAL (XEXP (op0, 1)) == 8)
3708 sv = 3;
9304f876
CJW
3709 else
3710 gcc_unreachable ();
3711
3712 fprintf (stream, "[%s + %s << %d]",
3713 reg_names[REGNO (op1)],
3714 reg_names[REGNO (XEXP (op0, 0))],
3715 sv);
3716 }
7c32ef41
MC
3717 else if (GET_CODE (op0) == ASHIFT && REG_P (op1))
3718 {
3719 /* [Ra + Rb << sv]
3720 In normal, ASHIFT can be converted to MULT like above case.
3721 But when the address rtx does not go through canonicalize_address
3722 defined in fwprop, we'll need this case. */
3723 int sv = INTVAL (XEXP (op0, 1));
3724 gcc_assert (sv <= 3 && sv >=0);
3725
3726 fprintf (stream, "[%s + %s << %d]",
3727 reg_names[REGNO (op1)],
3728 reg_names[REGNO (XEXP (op0, 0))],
3729 sv);
3730 }
9304f876
CJW
3731 else
3732 {
3733 /* The control flow is not supposed to be here. */
3734 debug_rtx (x);
3735 gcc_unreachable ();
3736 }
3737
3738 break;
3739
3740 case POST_MODIFY:
3741 /* (post_modify (regA) (plus (regA) (regB)))
8a498f99
CJW
3742 (post_modify (regA) (plus (regA) (const_int)))
3743 We would like to extract
3744 regA and regB (or const_int) from plus rtx. */
9304f876
CJW
3745 op0 = XEXP (XEXP (x, 1), 0);
3746 op1 = XEXP (XEXP (x, 1), 1);
3747
3748 /* Checking op0, forbid using static chain register ($r16)
8a498f99 3749 on reduced-set registers configuration. */
9304f876
CJW
3750 if (TARGET_REDUCED_REGS
3751 && REG_P (op0)
3752 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3753 sorry ("a nested function is not supported for reduced registers");
3754 /* Checking op1, forbid using static chain register ($r16)
8a498f99 3755 on reduced-set registers configuration. */
9304f876
CJW
3756 if (TARGET_REDUCED_REGS
3757 && REG_P (op1)
3758 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3759 sorry ("a nested function is not supported for reduced registers");
3760
3761 if (REG_P (op0) && REG_P (op1))
3762 {
3763 /* [Ra], Rb */
3764 fprintf (stream, "[%s], %s",
3765 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3766 }
3767 else if (REG_P (op0) && CONST_INT_P (op1))
3768 {
3769 /* [Ra], imm */
5af50159
KC
3770 fprintf (stream, "[%s], " HOST_WIDE_INT_PRINT_DEC,
3771 reg_names[REGNO (op0)], INTVAL (op1));
9304f876
CJW
3772 }
3773 else
3774 {
3775 /* The control flow is not supposed to be here. */
3776 debug_rtx (x);
3777 gcc_unreachable ();
3778 }
3779
3780 break;
3781
3782 case POST_INC:
3783 case POST_DEC:
3784 op0 = XEXP (x, 0);
3785
3786 /* Checking op0, forbid using static chain register ($r16)
8a498f99 3787 on reduced-set registers configuration. */
9304f876
CJW
3788 if (TARGET_REDUCED_REGS
3789 && REG_P (op0)
3790 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3791 sorry ("a nested function is not supported for reduced registers");
3792
3793 if (REG_P (op0))
3794 {
3795 /* "[Ra], 1/2/4" or "[Ra], -1/-2/-4"
3796 The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
3797 We only need to deal with register Ra. */
3798 fprintf (stream, "[%s]", reg_names[REGNO (op0)]);
3799 }
3800 else
3801 {
3802 /* The control flow is not supposed to be here. */
3803 debug_rtx (x);
3804 gcc_unreachable ();
3805 }
3806
3807 break;
3808
3809 default :
3810 /* Generally, output_addr_const () is able to handle most cases.
8a498f99
CJW
3811 We want to see what CODE could appear,
3812 so we use gcc_unreachable() to stop it. */
9304f876
CJW
3813 debug_rtx (x);
3814 gcc_unreachable ();
3815 break;
3816 }
3817}
3818
e2286268
MC
3819/* -- Assembler Commands for Exception Regions. */
3820
3821static rtx
3822nds32_dwarf_register_span (rtx reg)
3823{
3824 rtx dwarf_high, dwarf_low;
3825 rtx dwarf_single;
3826 machine_mode mode;
3827 int regno;
3828
3829 mode = GET_MODE (reg);
3830 regno = REGNO (reg);
3831
3832 /* We need to adjust dwarf register information for floating-point registers
3833 rather than using default register number mapping. */
3834 if (regno >= NDS32_FIRST_FPR_REGNUM
3835 && regno <= NDS32_LAST_FPR_REGNUM)
3836 {
3837 if (mode == DFmode || mode == SCmode)
3838 {
3839 /* By default, GCC maps increasing register numbers to increasing
3840 memory locations, but paired FPRs in NDS32 target are always
3841 big-endian, i.e.:
3842
3843 fd0 : fs0 fs1
3844 (MSB) (LSB)
3845
3846 We must return parallel rtx to represent such layout. */
3847 dwarf_high = gen_rtx_REG (word_mode, regno);
3848 dwarf_low = gen_rtx_REG (word_mode, regno + 1);
3849 return gen_rtx_PARALLEL (VOIDmode,
3850 gen_rtvec (2, dwarf_low, dwarf_high));
3851 }
3852 else if (mode == DCmode)
3853 {
3854 rtx dwarf_high_re = gen_rtx_REG (word_mode, regno);
3855 rtx dwarf_low_re = gen_rtx_REG (word_mode, regno + 1);
3856 rtx dwarf_high_im = gen_rtx_REG (word_mode, regno);
3857 rtx dwarf_low_im = gen_rtx_REG (word_mode, regno + 1);
3858 return gen_rtx_PARALLEL (VOIDmode,
3859 gen_rtvec (4, dwarf_low_re, dwarf_high_re,
3860 dwarf_high_im, dwarf_low_im));
3861 }
3862 else if (mode == SFmode || mode == SImode)
3863 {
3864 /* Create new dwarf information with adjusted register number. */
3865 dwarf_single = gen_rtx_REG (word_mode, regno);
3866 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, dwarf_single));
3867 }
3868 else
3869 {
3870 /* We should not be here. */
3871 gcc_unreachable ();
3872 }
3873 }
3874
3875 return NULL_RTX;
3876}
3877
3878/* Map internal gcc register numbers to DWARF2 register numbers. */
3879
3880unsigned int
3881nds32_dbx_register_number (unsigned int regno)
3882{
3883 /* The nds32 port in GDB maintains a mapping between dwarf register
3884 number and displayed register name. For backward compatibility to
3885 previous toolchain, currently our gdb still has four registers
3886 (d0.l, d0.h, d1.l, and d1.h) between GPR and FPR while compiler
3887 does not count those four registers in its register number table.
3888 So we have to add 4 on its register number and then create new
3889 dwarf information. Hopefully we can discard such workaround
3890 in the future. */
3891 if (NDS32_IS_FPR_REGNUM (regno))
3892 return regno + 4;
3893
3894 return regno;
3895}
3896
9304f876
CJW
3897\f
3898/* Defining target-specific uses of __attribute__. */
3899
3900/* Add some checking after merging attributes. */
3901static tree
3902nds32_merge_decl_attributes (tree olddecl, tree newdecl)
3903{
3904 tree combined_attrs;
3905
3906 /* Create combined attributes. */
3907 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
3908 DECL_ATTRIBUTES (newdecl));
3909
59043e75 3910 /* Since newdecl is acutally a duplicate of olddecl,
9304f876
CJW
3911 we can take olddecl for some operations. */
3912 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3913 {
3914 /* Check isr-specific attributes conflict. */
3915 nds32_check_isr_attrs_conflict (olddecl, combined_attrs);
3916 }
3917
3918 return combined_attrs;
3919}
3920
3921/* Add some checking when inserting attributes. */
3922static void
3923nds32_insert_attributes (tree decl, tree *attributes)
3924{
85a98076
KLC
3925 /* A "indirect_call" function attribute implies "noinline" and "noclone"
3926 for elf toolchain to support ROM patch mechanism. */
3927 if (TREE_CODE (decl) == FUNCTION_DECL
3928 && lookup_attribute ("indirect_call", *attributes) != NULL)
3929 {
3930 tree new_attrs = *attributes;
3931
cf3cd43d
CJW
3932 if (TARGET_LINUX_ABI)
3933 error("cannot use indirect_call attribute under linux toolchain");
3934
85a98076
KLC
3935 if (lookup_attribute ("noinline", new_attrs) == NULL)
3936 new_attrs = tree_cons (get_identifier ("noinline"), NULL, new_attrs);
3937 if (lookup_attribute ("noclone", new_attrs) == NULL)
3938 new_attrs = tree_cons (get_identifier ("noclone"), NULL, new_attrs);
3939
3940 if (!TREE_PUBLIC (decl))
3941 error("indirect_call attribute can't apply for static function");
3942
3943 *attributes = new_attrs;
3944 }
3945
9304f876
CJW
3946 /* For function declaration, we need to check isr-specific attributes:
3947 1. Call nds32_check_isr_attrs_conflict() to check any conflict.
3948 2. Check valid integer value for interrupt/exception.
3949 3. Check valid integer value for reset.
3950 4. Check valid function for nmi/warm. */
3951 if (TREE_CODE (decl) == FUNCTION_DECL)
3952 {
3953 tree func_attrs;
3954 tree intr, excp, reset;
3955
3956 /* Pick up function attributes. */
3957 func_attrs = *attributes;
3958
3959 /* 1. Call nds32_check_isr_attrs_conflict() to check any conflict. */
3960 nds32_check_isr_attrs_conflict (decl, func_attrs);
3961
3962 /* Now we are starting to check valid id value
8a498f99
CJW
3963 for interrupt/exception/reset.
3964 Note that we ONLY check its validity here.
3965 To construct isr vector information, it is still performed
3966 by nds32_construct_isr_vectors_information(). */
9304f876
CJW
3967 intr = lookup_attribute ("interrupt", func_attrs);
3968 excp = lookup_attribute ("exception", func_attrs);
3969 reset = lookup_attribute ("reset", func_attrs);
3970
3971 if (intr || excp)
3972 {
3973 /* Deal with interrupt/exception. */
3974 tree id_list;
3975 unsigned int lower_bound, upper_bound;
3976
3977 /* The way to handle interrupt or exception is the same,
3978 we just need to take care of actual vector number.
3979 For interrupt(0..63), the actual vector number is (9..72).
3980 For exception(1..8), the actual vector number is (1..8). */
3981 lower_bound = (intr) ? (0) : (1);
3982 upper_bound = (intr) ? (63) : (8);
3983
3984 /* Prepare id list so that we can traverse id value. */
3985 id_list = (intr) ? (TREE_VALUE (intr)) : (TREE_VALUE (excp));
3986
3987 /* 2. Check valid integer value for interrupt/exception. */
3988 while (id_list)
3989 {
3990 tree id;
3991
3992 /* Pick up each vector id value. */
3993 id = TREE_VALUE (id_list);
3994 /* Issue error if it is not a valid integer value. */
3995 if (TREE_CODE (id) != INTEGER_CST
8e6cdc90
RS
3996 || wi::ltu_p (wi::to_wide (id), lower_bound)
3997 || wi::gtu_p (wi::to_wide (id), upper_bound))
9304f876
CJW
3998 error ("invalid id value for interrupt/exception attribute");
3999
4000 /* Advance to next id. */
4001 id_list = TREE_CHAIN (id_list);
4002 }
4003 }
4004 else if (reset)
4005 {
4006 /* Deal with reset. */
4007 tree id_list;
4008 tree id;
4009 tree nmi, warm;
4010 unsigned int lower_bound;
4011 unsigned int upper_bound;
4012
4013 /* Prepare id_list and identify id value so that
4014 we can check if total number of vectors is valid. */
4015 id_list = TREE_VALUE (reset);
4016 id = TREE_VALUE (id_list);
4017
4018 /* The maximum numbers for user's interrupt is 64. */
4019 lower_bound = 0;
4020 upper_bound = 64;
4021
4022 /* 3. Check valid integer value for reset. */
4023 if (TREE_CODE (id) != INTEGER_CST
8e6cdc90
RS
4024 || wi::ltu_p (wi::to_wide (id), lower_bound)
4025 || wi::gtu_p (wi::to_wide (id), upper_bound))
9304f876
CJW
4026 error ("invalid id value for reset attribute");
4027
4028 /* 4. Check valid function for nmi/warm. */
4029 nmi = lookup_attribute ("nmi", func_attrs);
4030 warm = lookup_attribute ("warm", func_attrs);
4031
4032 if (nmi != NULL_TREE)
4033 {
4034 tree nmi_func_list;
4035 tree nmi_func;
4036
4037 nmi_func_list = TREE_VALUE (nmi);
4038 nmi_func = TREE_VALUE (nmi_func_list);
4039
4040 /* Issue error if it is not a valid nmi function. */
4041 if (TREE_CODE (nmi_func) != IDENTIFIER_NODE)
4042 error ("invalid nmi function for reset attribute");
4043 }
4044
4045 if (warm != NULL_TREE)
4046 {
4047 tree warm_func_list;
4048 tree warm_func;
4049
4050 warm_func_list = TREE_VALUE (warm);
4051 warm_func = TREE_VALUE (warm_func_list);
4052
4053 /* Issue error if it is not a valid warm function. */
4054 if (TREE_CODE (warm_func) != IDENTIFIER_NODE)
4055 error ("invalid warm function for reset attribute");
4056 }
4057 }
4058 else
4059 {
4060 /* No interrupt, exception, or reset attribute is set. */
4061 return;
4062 }
4063 }
4064}
4065
4066static bool
4067nds32_option_pragma_parse (tree args ATTRIBUTE_UNUSED,
4068 tree pop_target ATTRIBUTE_UNUSED)
4069{
4070 /* Currently, we do not parse any pragma target by ourself,
4071 so just simply return false. */
4072 return false;
4073}
4074
4075static void
4076nds32_option_override (void)
4077{
4078 /* After all the command options have been parsed,
4079 we shall deal with some flags for changing compiler settings. */
4080
4081 /* At first, we check if we have to strictly
4082 set some flags based on ISA family. */
4083 if (TARGET_ISA_V2)
4084 {
4085 /* Under V2 ISA, we need to strictly disable TARGET_V3PUSH. */
4086 target_flags &= ~MASK_V3PUSH;
4087 }
4088 if (TARGET_ISA_V3)
4089 {
37d8f611
CJW
4090 /* If this is ARCH_V3J, we need to enable TARGET_REDUCED_REGS. */
4091 if (nds32_arch_option == ARCH_V3J)
4092 target_flags |= MASK_REDUCED_REGS;
9304f876
CJW
4093 }
4094 if (TARGET_ISA_V3M)
4095 {
4096 /* Under V3M ISA, we need to strictly enable TARGET_REDUCED_REGS. */
4097 target_flags |= MASK_REDUCED_REGS;
aa4b851c
CJW
4098 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF. */
4099 target_flags &= ~MASK_EXT_PERF;
4100 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF2. */
4101 target_flags &= ~MASK_EXT_PERF2;
4102 /* Under V3M ISA, we need to strictly disable TARGET_EXT_STRING. */
4103 target_flags &= ~MASK_EXT_STRING;
b26fa4f9
KLC
4104
4105 if (flag_pic)
4106 error ("not support -fpic option for v3m toolchain");
9304f876
CJW
4107 }
4108
4109 /* See if we are using reduced-set registers:
4110 $r0~$r5, $r6~$r10, $r15, $r28, $r29, $r30, $r31
4111 If so, we must forbid using $r11~$r14, $r16~$r27. */
4112 if (TARGET_REDUCED_REGS)
4113 {
4114 int r;
4115
4116 /* Prevent register allocator from
8a498f99 4117 choosing it as doing register allocation. */
9304f876
CJW
4118 for (r = 11; r <= 14; r++)
4119 fixed_regs[r] = call_used_regs[r] = 1;
4120 for (r = 16; r <= 27; r++)
4121 fixed_regs[r] = call_used_regs[r] = 1;
4122 }
4123
2140297c
CJW
4124 /* See if user explicitly would like to use fp-as-gp optimization.
4125 If so, we must prevent $fp from being allocated
4126 during register allocation. */
4127 if (TARGET_FORCE_FP_AS_GP)
4128 fixed_regs[FP_REGNUM] = call_used_regs[FP_REGNUM] = 1;
4129
9304f876
CJW
4130 if (!TARGET_16_BIT)
4131 {
4132 /* Under no 16 bit ISA, we need to strictly disable TARGET_V3PUSH. */
4133 target_flags &= ~MASK_V3PUSH;
4134 }
4135
e2286268
MC
4136 if (TARGET_HARD_FLOAT && !(TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
4137 {
4138 if (nds32_arch_option == ARCH_V3S || nds32_arch_option == ARCH_V3F)
4139 error ("Disable FPU ISA, "
4140 "the ABI option must be enable '-mfloat-abi=soft'");
4141 else
4142 error ("'-mabi=2fp+' option only support when FPU available, "
4143 "must be enable '-mext-fpu-sp' or '-mext-fpu-dp'");
4144 }
4145
19c93809 4146 nds32_init_rtx_costs ();
c4d8d050
CJW
4147
4148 nds32_register_passes ();
9304f876
CJW
4149}
4150
4151\f
4152/* Miscellaneous Parameters. */
4153
823835a9
CJW
4154static rtx_insn *
4155nds32_md_asm_adjust (vec<rtx> &outputs ATTRIBUTE_UNUSED,
4156 vec<rtx> &inputs ATTRIBUTE_UNUSED,
4157 vec<const char *> &constraints ATTRIBUTE_UNUSED,
4158 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
4159{
b28c01ab
CJW
4160 if (!flag_inline_asm_r15)
4161 {
4162 clobbers.safe_push (gen_rtx_REG (SImode, TA_REGNUM));
4163 SET_HARD_REG_BIT (clobbered_regs, TA_REGNUM);
4164 }
823835a9
CJW
4165 return NULL;
4166}
4167
9304f876
CJW
4168static void
4169nds32_init_builtins (void)
4170{
aaa44d2d 4171 nds32_init_builtins_impl ();
9304f876
CJW
4172}
4173
7a12ea32
MC
4174static tree
4175nds32_builtin_decl (unsigned code, bool initialize_p)
4176{
4177 /* Implement in nds32-intrinsic.c. */
4178 return nds32_builtin_decl_impl (code, initialize_p);
4179}
4180
9304f876
CJW
4181static rtx
4182nds32_expand_builtin (tree exp,
4183 rtx target,
aaa44d2d 4184 rtx subtarget,
ef4bddc2 4185 machine_mode mode,
aaa44d2d 4186 int ignore)
9304f876 4187{
aaa44d2d 4188 return nds32_expand_builtin_impl (exp, target, subtarget, mode, ignore);
9304f876
CJW
4189}
4190
cf3cd43d
CJW
4191/* Implement TARGET_INIT_LIBFUNCS. */
4192static void
4193nds32_init_libfuncs (void)
4194{
4195 if (TARGET_LINUX_ABI)
4196 init_sync_libfuncs (UNITS_PER_WORD);
4197}
9304f876
CJW
4198
4199/* ------------------------------------------------------------------------ */
4200
4201/* PART 4: Implemet extern function definitions,
4202 the prototype is in nds32-protos.h. */
72b7e5e1
KC
4203\f
4204/* Run-time Target Specification. */
4205
4206void
4207nds32_cpu_cpp_builtins(struct cpp_reader *pfile)
4208{
4209#define builtin_define(TXT) cpp_define (pfile, TXT)
4210#define builtin_assert(TXT) cpp_assert (pfile, TXT)
4211 builtin_define ("__nds32__");
4212 builtin_define ("__NDS32__");
4213
e2286268
MC
4214 if (TARGET_HARD_FLOAT)
4215 builtin_define ("__NDS32_ABI_2FP_PLUS__");
4216 else
4217 builtin_define ("__NDS32_ABI_2__");
4218
72b7e5e1
KC
4219 if (TARGET_ISA_V2)
4220 builtin_define ("__NDS32_ISA_V2__");
4221 if (TARGET_ISA_V3)
4222 builtin_define ("__NDS32_ISA_V3__");
4223 if (TARGET_ISA_V3M)
4224 builtin_define ("__NDS32_ISA_V3M__");
4225
e2286268
MC
4226 if (TARGET_FPU_SINGLE)
4227 builtin_define ("__NDS32_EXT_FPU_SP__");
4228 if (TARGET_FPU_DOUBLE)
4229 builtin_define ("__NDS32_EXT_FPU_DP__");
4230
4231 if (TARGET_EXT_FPU_FMA)
4232 builtin_define ("__NDS32_EXT_FPU_FMA__");
4233 if (NDS32_EXT_FPU_DOT_E)
4234 builtin_define ("__NDS32_EXT_FPU_DOT_E__");
4235 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
4236 {
4237 switch (nds32_fp_regnum)
4238 {
4239 case 0:
4240 case 4:
4241 builtin_define ("__NDS32_EXT_FPU_CONFIG_0__");
4242 break;
4243 case 1:
4244 case 5:
4245 builtin_define ("__NDS32_EXT_FPU_CONFIG_1__");
4246 break;
4247 case 2:
4248 case 6:
4249 builtin_define ("__NDS32_EXT_FPU_CONFIG_2__");
4250 break;
4251 case 3:
4252 case 7:
4253 builtin_define ("__NDS32_EXT_FPU_CONFIG_3__");
4254 break;
4255 default:
4256 abort ();
4257 }
4258 }
4259
72b7e5e1
KC
4260 if (TARGET_BIG_ENDIAN)
4261 builtin_define ("__NDS32_EB__");
4262 else
4263 builtin_define ("__NDS32_EL__");
4264
4265 if (TARGET_REDUCED_REGS)
4266 builtin_define ("__NDS32_REDUCED_REGS__");
4267 if (TARGET_CMOV)
4268 builtin_define ("__NDS32_CMOV__");
4269 if (TARGET_EXT_PERF)
4270 builtin_define ("__NDS32_EXT_PERF__");
4271 if (TARGET_EXT_PERF2)
4272 builtin_define ("__NDS32_EXT_PERF2__");
4273 if (TARGET_EXT_STRING)
4274 builtin_define ("__NDS32_EXT_STRING__");
4275 if (TARGET_16_BIT)
4276 builtin_define ("__NDS32_16_BIT__");
4277 if (TARGET_GP_DIRECT)
4278 builtin_define ("__NDS32_GP_DIRECT__");
ff77f6e8
KC
4279 if (TARGET_VH)
4280 builtin_define ("__NDS32_VH__");
7c32ef41
MC
4281 if (NDS32_EXT_DSP_P ())
4282 builtin_define ("__NDS32_EXT_DSP__");
72b7e5e1
KC
4283
4284 if (TARGET_BIG_ENDIAN)
4285 builtin_define ("__big_endian__");
4286
4287 builtin_assert ("cpu=nds32");
4288 builtin_assert ("machine=nds32");
e2286268
MC
4289
4290 if (TARGET_HARD_FLOAT)
4291 builtin_define ("__NDS32_ABI_2FP_PLUS");
4292 else
4293 builtin_define ("__NDS32_ABI_2");
4294
72b7e5e1
KC
4295#undef builtin_define
4296#undef builtin_assert
4297}
4298
9304f876
CJW
4299\f
4300/* Defining Data Structures for Per-function Information. */
4301
4302void
4303nds32_init_expanders (void)
4304{
4305 /* Arrange to initialize and mark the machine per-function status. */
4306 init_machine_status = nds32_init_machine_status;
4307}
4308
4309\f
4310/* Register Usage. */
4311
5e6ae0cc
CJW
4312/* -- Order of Allocation of Registers. */
4313
4314void
4315nds32_adjust_reg_alloc_order (void)
4316{
4317 const int nds32_reg_alloc_order[] = REG_ALLOC_ORDER;
4318
4319 /* Copy the default register allocation order, which is designed
4320 to optimize for code size. */
4321 memcpy(reg_alloc_order, nds32_reg_alloc_order, sizeof (reg_alloc_order));
4322
4323 /* Adjust few register allocation order when optimizing for speed. */
4324 if (!optimize_size)
4325 {
4326 memcpy (reg_alloc_order, nds32_reg_alloc_order_for_speed,
4327 sizeof (nds32_reg_alloc_order_for_speed));
4328 }
4329}
4330
9304f876
CJW
4331/* -- How Values Fit in Registers. */
4332
e2286268
MC
4333static unsigned
4334nds32_hard_regno_nregs (unsigned regno ATTRIBUTE_UNUSED,
4335 machine_mode mode)
4336{
4337 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
4338}
4339
f939c3e6
RS
4340/* Implement TARGET_HARD_REGNO_MODE_OK. */
4341
4342static bool
4343nds32_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
9304f876 4344{
e2286268 4345 if (regno > FIRST_PSEUDO_REGISTER)
f939c3e6 4346 return true;
9304f876 4347
e2286268
MC
4348 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE) && NDS32_IS_FPR_REGNUM (regno))
4349 {
4350 if (NDS32_IS_EXT_FPR_REGNUM(regno))
4351 return (NDS32_FPR_REGNO_OK_FOR_DOUBLE(regno) && (mode == DFmode));
4352 else if (mode == SFmode || mode == SImode)
4353 return NDS32_FPR_REGNO_OK_FOR_SINGLE (regno);
4354 else if (mode == DFmode)
4355 return NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno);
4356
4357 return false;
4358 }
4359
4360 /* Restrict double-word quantities to even register pairs. */
4361 if (regno <= NDS32_LAST_GPR_REGNUM)
4362 return (targetm.hard_regno_nregs (regno, mode) == 1
4363 || !((regno) & 1));
4364
f939c3e6 4365 return false;
9304f876
CJW
4366}
4367
99e1629f
RS
4368/* Implement TARGET_MODES_TIEABLE_P. We can use general registers to
4369 tie QI/HI/SI modes together. */
4370
4371static bool
4372nds32_modes_tieable_p (machine_mode mode1, machine_mode mode2)
4373{
e2286268
MC
4374 if ((GET_MODE_CLASS (mode1) == MODE_INT
4375 && GET_MODE_CLASS (mode2) == MODE_INT)
4376 && GET_MODE_SIZE (mode1) <= UNITS_PER_WORD
4377 && GET_MODE_SIZE (mode2) <= UNITS_PER_WORD)
4378 return true;
4379
4380 if (GET_MODE_SIZE (mode1) == GET_MODE_SIZE (mode2))
4381 {
4382 if ((TARGET_FPU_SINGLE && !TARGET_FPU_DOUBLE)
4383 && (mode1 == DFmode || mode2 == DFmode))
4384 return false;
4385 else
4386 return true;
4387 }
4388
4389 return false;
99e1629f 4390}
9304f876
CJW
4391\f
4392/* Register Classes. */
4393
4394enum reg_class
4395nds32_regno_reg_class (int regno)
4396{
4397 /* Refer to nds32.h for more register class details. */
4398
4399 if (regno >= 0 && regno <= 7)
4400 return LOW_REGS;
4401 else if (regno >= 8 && regno <= 11)
4402 return MIDDLE_REGS;
4403 else if (regno >= 12 && regno <= 14)
4404 return HIGH_REGS;
4405 else if (regno == 15)
4406 return R15_TA_REG;
4407 else if (regno >= 16 && regno <= 19)
4408 return MIDDLE_REGS;
4409 else if (regno >= 20 && regno <= 31)
4410 return HIGH_REGS;
4411 else if (regno == 32 || regno == 33)
e2286268
MC
4412 {
4413 /* $SFP and $AP is FRAME_REGS in fact, However prevent IRA don't
4414 know how to allocate register for $SFP and $AP, just tell IRA they
4415 are GENERAL_REGS, and ARM do this hack too. */
4416 return GENERAL_REGS;
4417 }
4418 else if (regno >= 34 && regno <= 97)
4419 return FP_REGS;
9304f876
CJW
4420 else
4421 return NO_REGS;
4422}
4423
4424\f
4425/* Stack Layout and Calling Conventions. */
4426
4427/* -- Basic Stack Layout. */
4428
ca3a4a55
CJW
4429rtx
4430nds32_dynamic_chain_address (rtx frameaddr)
4431{
4432 if (TARGET_V3PUSH)
4433 {
4434 /* If -mv3push is specified, we push $fp, $gp, and $lp into stack.
4435 We can access dynamic chain address from stack by [$fp - 12]. */
4436 return plus_constant (Pmode, frameaddr, -12);
4437 }
4438 else
4439 {
4440 /* For general case we push $fp and $lp into stack at prologue.
4441 We can access dynamic chain address from stack by [$fp - 8]. */
4442 return plus_constant (Pmode, frameaddr, -8);
4443 }
4444}
4445
9304f876
CJW
4446rtx
4447nds32_return_addr_rtx (int count,
ca3a4a55 4448 rtx frameaddr)
9304f876 4449{
ca3a4a55
CJW
4450 int offset;
4451 rtx addr;
4452
9304f876 4453 if (count != 0)
ca3a4a55
CJW
4454 {
4455 /* In nds32 ABI design, we can expect that $lp is always available
4456 from stack by [$fp - 4] location. */
4457 offset = -4;
4458 addr = plus_constant (Pmode, frameaddr, offset);
4459 addr = memory_address (Pmode, addr);
4460
4461 return gen_rtx_MEM (Pmode, addr);
4462 }
9304f876
CJW
4463
4464 /* If count == 0, it means we are at current frame,
4465 the return address is $r30 ($lp). */
4466 return get_hard_reg_initial_val (Pmode, LP_REGNUM);
4467}
4468
4469/* -- Eliminating Frame Pointer and Arg Pointer. */
4470
4471HOST_WIDE_INT
4472nds32_initial_elimination_offset (unsigned int from_reg, unsigned int to_reg)
4473{
4474 HOST_WIDE_INT offset;
4475
4476 /* Compute and setup stack frame size.
4477 The result will be in cfun->machine. */
4478 nds32_compute_stack_frame ();
4479
4480 /* Remember to consider
ca3a4a55
CJW
4481 cfun->machine->callee_saved_area_gpr_padding_bytes and
4482 cfun->machine->eh_return_data_regs_size
9304f876
CJW
4483 when calculating offset. */
4484 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
4485 {
4486 offset = (cfun->machine->fp_size
8a498f99 4487 + cfun->machine->gp_size
9304f876 4488 + cfun->machine->lp_size
c457f751
CJW
4489 + cfun->machine->callee_saved_gpr_regs_size
4490 + cfun->machine->callee_saved_area_gpr_padding_bytes
e2286268 4491 + cfun->machine->callee_saved_fpr_regs_size
ca3a4a55 4492 + cfun->machine->eh_return_data_regs_size
9304f876
CJW
4493 + cfun->machine->local_size
4494 + cfun->machine->out_args_size);
4495 }
4496 else if (from_reg == ARG_POINTER_REGNUM
4497 && to_reg == HARD_FRAME_POINTER_REGNUM)
4498 {
4499 offset = 0;
4500 }
4501 else if (from_reg == FRAME_POINTER_REGNUM
4502 && to_reg == STACK_POINTER_REGNUM)
4503 {
4504 offset = (cfun->machine->local_size + cfun->machine->out_args_size);
4505 }
4506 else if (from_reg == FRAME_POINTER_REGNUM
4507 && to_reg == HARD_FRAME_POINTER_REGNUM)
4508 {
4509 offset = (-1) * (cfun->machine->fp_size
4510 + cfun->machine->gp_size
4511 + cfun->machine->lp_size
c457f751 4512 + cfun->machine->callee_saved_gpr_regs_size
e2286268 4513 + cfun->machine->callee_saved_area_gpr_padding_bytes
ca3a4a55
CJW
4514 + cfun->machine->callee_saved_fpr_regs_size
4515 + cfun->machine->eh_return_data_regs_size);
9304f876
CJW
4516 }
4517 else
4518 {
4519 gcc_unreachable ();
4520 }
4521
4522 return offset;
4523}
4524
4525/* -- Passing Arguments in Registers. */
4526
4527void
4528nds32_init_cumulative_args (CUMULATIVE_ARGS *cum,
4529 tree fntype ATTRIBUTE_UNUSED,
4530 rtx libname ATTRIBUTE_UNUSED,
4531 tree fndecl ATTRIBUTE_UNUSED,
4532 int n_named_args ATTRIBUTE_UNUSED)
4533{
e2286268
MC
4534 /* Initial available registers. The values are offset against
4535 NDS32_GPR_ARG_FIRST_REGNUM and NDS32_FPR_ARG_FIRST_REGNUM
9304f876 4536 for passing arguments. */
9d93cc24 4537 cum->gpr_offset = 0;
e2286268 4538 cum->fpr_offset = 0;
9304f876
CJW
4539}
4540
4541/* -- Function Entry and Exit. */
4542
4543/* Function for normal multiple push prologue. */
4544void
4545nds32_expand_prologue (void)
4546{
4547 int fp_adjust;
4548 int sp_adjust;
a6c7e777 4549 unsigned Rb, Re;
9304f876 4550
9304f876
CJW
4551 /* Compute and setup stack frame size.
4552 The result will be in cfun->machine. */
4553 nds32_compute_stack_frame ();
4554
2140297c
CJW
4555 /* Check frame_pointer_needed again to prevent fp is need after reload. */
4556 if (frame_pointer_needed)
4557 cfun->machine->fp_as_gp_p = false;
4558
557430f1
CJW
4559 /* If this is a variadic function, first we need to push argument
4560 registers that hold the unnamed argument value. */
4561 if (cfun->machine->va_args_size != 0)
4562 {
a6c7e777
MC
4563 Rb = cfun->machine->va_args_first_regno;
4564 Re = cfun->machine->va_args_last_regno;
4565 /* No need to push $fp, $gp, or $lp. */
4566 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, true);
557430f1
CJW
4567
4568 /* We may also need to adjust stack pointer for padding bytes
4569 because varargs may cause $sp not 8-byte aligned. */
4570 if (cfun->machine->va_args_area_padding_bytes)
4571 {
4572 /* Generate sp adjustment instruction. */
4573 sp_adjust = cfun->machine->va_args_area_padding_bytes;
557430f1 4574
a6c7e777
MC
4575 nds32_emit_adjust_frame (stack_pointer_rtx,
4576 stack_pointer_rtx,
4577 -1 * sp_adjust);
557430f1
CJW
4578 }
4579 }
4580
9304f876
CJW
4581 /* If the function is 'naked',
4582 we do not have to generate prologue code fragment. */
b26fa4f9 4583 if (cfun->machine->naked_p && !flag_pic)
9304f876
CJW
4584 return;
4585
4586 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
4587 Rb = cfun->machine->callee_saved_first_gpr_regno;
4588 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
4589
4590 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4591 to be saved, we don't have to create multiple push instruction.
4592 Otherwise, a multiple push instruction is needed. */
a6c7e777
MC
4593 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4594 && cfun->machine->fp_size == 0
4595 && cfun->machine->gp_size == 0
4596 && cfun->machine->lp_size == 0))
9304f876
CJW
4597 {
4598 /* Create multiple push instruction rtx. */
a6c7e777
MC
4599 nds32_emit_stack_push_multiple (
4600 Rb, Re,
4601 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size,
4602 false);
9304f876
CJW
4603 }
4604
ca3a4a55
CJW
4605 /* Save eh data registers. */
4606 if (cfun->machine->use_eh_return_p)
4607 {
4608 Rb = cfun->machine->eh_return_data_first_regno;
4609 Re = cfun->machine->eh_return_data_last_regno;
4610
4611 /* No need to push $fp, $gp, or $lp.
4612 Also, this is not variadic arguments push. */
4613 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, false);
4614 }
4615
9304f876
CJW
4616 /* Check frame_pointer_needed to see
4617 if we shall emit fp adjustment instruction. */
4618 if (frame_pointer_needed)
4619 {
4620 /* adjust $fp = $sp + ($fp size) + ($gp size) + ($lp size)
8a498f99 4621 + (4 * callee-saved-registers)
ca3a4a55 4622 + (4 * exception-handling-data-registers)
8a498f99
CJW
4623 Note: No need to adjust
4624 cfun->machine->callee_saved_area_gpr_padding_bytes,
4625 because, at this point, stack pointer is just
4626 at the position after push instruction. */
9304f876
CJW
4627 fp_adjust = cfun->machine->fp_size
4628 + cfun->machine->gp_size
4629 + cfun->machine->lp_size
ca3a4a55
CJW
4630 + cfun->machine->callee_saved_gpr_regs_size
4631 + cfun->machine->eh_return_data_regs_size;
7064dcad 4632
a6c7e777
MC
4633 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4634 stack_pointer_rtx,
4635 fp_adjust);
9304f876
CJW
4636 }
4637
e2286268
MC
4638 /* Save fpu registers. */
4639 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4640 {
4641 /* When $sp moved to bottom of stack, we need to check whether
4642 the range of offset in the FPU instruction. */
4643 int fpr_offset = cfun->machine->local_size
4644 + cfun->machine->out_args_size
4645 + cfun->machine->callee_saved_fpr_regs_size;
4646
4647 /* Check FPU instruction offset imm14s. */
4648 if (!satisfies_constraint_Is14 (GEN_INT (fpr_offset)))
4649 {
4650 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4651 + cfun->machine->callee_saved_fpr_regs_size;
4652
4653 /* Save fpu registers, need to allocate stack space
4654 for fpu callee registers. And now $sp position
4655 on callee saved fpr registers. */
4656 nds32_emit_adjust_frame (stack_pointer_rtx,
4657 stack_pointer_rtx,
4658 -1 * fpr_space);
4659
4660 /* Emit fpu store instruction, using [$sp + offset] store
4661 fpu registers. */
4662 nds32_emit_push_fpr_callee_saved (0);
4663
4664 /* Adjust $sp = $sp - local_size - out_args_size. */
4665 sp_adjust = cfun->machine->local_size
4666 + cfun->machine->out_args_size;
4667
4668 /* Allocate stack space for local size and out args size. */
4669 nds32_emit_adjust_frame (stack_pointer_rtx,
4670 stack_pointer_rtx,
4671 -1 * sp_adjust);
4672 }
4673 else
4674 {
4675 /* Offset range in Is14, so $sp moved to bottom of stack. */
4676
4677 /* Adjust $sp = $sp - local_size - out_args_size
4678 - callee_saved_area_gpr_padding_bytes
4679 - callee_saved_fpr_regs_size. */
4680 sp_adjust = cfun->machine->local_size
4681 + cfun->machine->out_args_size
4682 + cfun->machine->callee_saved_area_gpr_padding_bytes
4683 + cfun->machine->callee_saved_fpr_regs_size;
4684
4685 nds32_emit_adjust_frame (stack_pointer_rtx,
4686 stack_pointer_rtx,
4687 -1 * sp_adjust);
4688
4689 /* Emit fpu store instruction, using [$sp + offset] store
4690 fpu registers. */
4691 int fpr_position = cfun->machine->out_args_size
4692 + cfun->machine->local_size;
4693 nds32_emit_push_fpr_callee_saved (fpr_position);
4694 }
4695 }
4696 else
4697 {
4698 /* Adjust $sp = $sp - local_size - out_args_size
4699 - callee_saved_area_gpr_padding_bytes. */
4700 sp_adjust = cfun->machine->local_size
4701 + cfun->machine->out_args_size
4702 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4703
4704 /* sp_adjust value may be out of range of the addi instruction,
4705 create alternative add behavior with TA_REGNUM if necessary,
4706 using NEGATIVE value to tell that we are decreasing address. */
4707 nds32_emit_adjust_frame (stack_pointer_rtx,
4708 stack_pointer_rtx,
4709 -1 * sp_adjust);
4710 }
9304f876 4711
b26fa4f9
KLC
4712 /* Emit gp setup instructions for -fpic. */
4713 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
4714 nds32_emit_load_gp ();
4715
9304f876
CJW
4716 /* Prevent the instruction scheduler from
4717 moving instructions across the boundary. */
4718 emit_insn (gen_blockage ());
4719}
4720
4721/* Function for normal multiple pop epilogue. */
4722void
d6529176 4723nds32_expand_epilogue (bool sibcall_p)
9304f876
CJW
4724{
4725 int sp_adjust;
a6c7e777 4726 unsigned Rb, Re;
9304f876
CJW
4727
4728 /* Compute and setup stack frame size.
4729 The result will be in cfun->machine. */
4730 nds32_compute_stack_frame ();
4731
4732 /* Prevent the instruction scheduler from
4733 moving instructions across the boundary. */
4734 emit_insn (gen_blockage ());
4735
4736 /* If the function is 'naked', we do not have to generate
557430f1
CJW
4737 epilogue code fragment BUT 'ret' instruction.
4738 However, if this function is also a variadic function,
4739 we need to create adjust stack pointer before 'ret' instruction. */
9304f876
CJW
4740 if (cfun->machine->naked_p)
4741 {
557430f1 4742 /* If this is a variadic function, we do not have to restore argument
8a498f99
CJW
4743 registers but need to adjust stack pointer back to previous stack
4744 frame location before return. */
557430f1
CJW
4745 if (cfun->machine->va_args_size != 0)
4746 {
4747 /* Generate sp adjustment instruction.
4748 We need to consider padding bytes here. */
4749 sp_adjust = cfun->machine->va_args_size
4750 + cfun->machine->va_args_area_padding_bytes;
a6c7e777
MC
4751
4752 nds32_emit_adjust_frame (stack_pointer_rtx,
4753 stack_pointer_rtx,
4754 sp_adjust);
557430f1
CJW
4755 }
4756
03390cda 4757 /* Generate return instruction by using 'return_internal' pattern.
8a498f99 4758 Make sure this instruction is after gen_blockage(). */
d6529176 4759 if (!sibcall_p)
54c537e6
CJW
4760 {
4761 /* We need to further check attributes to determine whether
4762 there should be return instruction at epilogue.
4763 If the attribute naked exists but -mno-ret-in-naked-func
4764 is issued, there is NO need to generate return instruction. */
4765 if (cfun->machine->attr_naked_p && !flag_ret_in_naked_func)
4766 return;
4767
4768 emit_jump_insn (gen_return_internal ());
4769 }
9304f876
CJW
4770 return;
4771 }
4772
4773 if (frame_pointer_needed)
4774 {
e2286268
MC
4775 /* Restore fpu registers. */
4776 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4777 {
4778 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
4779
4780 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4781 - (4 * callee-saved-registers)
4782 - (4 * exception-handling-data-registers)
4783 - (4 * callee-saved-gpr-registers padding byte)
4784 - (4 * callee-saved-fpr-registers)
4785 Note: we want to adjust stack pointer
4786 to the position for callee-saved fpr register,
4787 And restore fpu register use .bi instruction to adjust $sp
4788 from callee-saved fpr register to pop instruction. */
4789 sp_adjust = cfun->machine->fp_size
4790 + cfun->machine->gp_size
4791 + cfun->machine->lp_size
4792 + cfun->machine->callee_saved_gpr_regs_size
ca3a4a55 4793 + cfun->machine->eh_return_data_regs_size
e2286268
MC
4794 + cfun->machine->callee_saved_area_gpr_padding_bytes
4795 + cfun->machine->callee_saved_fpr_regs_size;
557430f1 4796
e2286268
MC
4797 nds32_emit_adjust_frame (stack_pointer_rtx,
4798 hard_frame_pointer_rtx,
4799 -1 * sp_adjust);
4800
4801 /* Emit fpu load instruction, using .bi instruction
4802 load fpu registers. */
4803 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4804 }
4805 else
4806 {
4807 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4808 - (4 * callee-saved-registers)
4809 - (4 * exception-handling-data-registers)
4810 Note: No need to adjust
4811 cfun->machine->callee_saved_area_gpr_padding_bytes,
4812 because we want to adjust stack pointer
4813 to the position for pop instruction. */
4814 sp_adjust = cfun->machine->fp_size
4815 + cfun->machine->gp_size
4816 + cfun->machine->lp_size
ca3a4a55
CJW
4817 + cfun->machine->callee_saved_gpr_regs_size
4818 + cfun->machine->eh_return_data_regs_size;
e2286268
MC
4819
4820 nds32_emit_adjust_frame (stack_pointer_rtx,
4821 hard_frame_pointer_rtx,
4822 -1 * sp_adjust);
4823 }
9304f876
CJW
4824 }
4825 else
4826 {
e2286268
MC
4827 /* Restore fpu registers. */
4828 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4829 {
4830 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
9304f876 4831
e2286268
MC
4832 /* Adjust $sp = $sp + local_size + out_args_size. */
4833 sp_adjust = cfun->machine->local_size
4834 + cfun->machine->out_args_size;
a6c7e777 4835
e2286268
MC
4836 nds32_emit_adjust_frame (stack_pointer_rtx,
4837 stack_pointer_rtx,
4838 sp_adjust);
4839
4840 /* Emit fpu load instruction, using .bi instruction
4841 load fpu registers, and adjust $sp from callee-saved fpr register
4842 to callee-saved gpr register. */
4843 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4844 }
4845 else
4846 {
4847 /* If frame pointer is NOT needed,
4848 we cannot calculate the sp adjustment from frame pointer.
4849 Instead, we calculate the adjustment by local_size,
4850 out_args_size, and callee_saved_area_gpr_padding_bytes.
4851 Notice that such sp adjustment value may be out of range,
4852 so we have to deal with it as well. */
4853
4854 /* Adjust $sp = $sp + local_size + out_args_size
4855 + callee_saved_area_gpr_padding_bytes. */
4856 sp_adjust = cfun->machine->local_size
4857 + cfun->machine->out_args_size
4858 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4859
4860 nds32_emit_adjust_frame (stack_pointer_rtx,
4861 stack_pointer_rtx,
4862 sp_adjust);
4863 }
9304f876
CJW
4864 }
4865
ca3a4a55
CJW
4866 /* Restore eh data registers. */
4867 if (cfun->machine->use_eh_return_p)
4868 {
4869 Rb = cfun->machine->eh_return_data_first_regno;
4870 Re = cfun->machine->eh_return_data_last_regno;
4871
4872 /* No need to pop $fp, $gp, or $lp. */
4873 nds32_emit_stack_pop_multiple (Rb, Re, false, false, false);
4874 }
4875
9304f876 4876 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
4877 Rb = cfun->machine->callee_saved_first_gpr_regno;
4878 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
4879
4880 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4881 to be saved, we don't have to create multiple pop instruction.
4882 Otherwise, a multiple pop instruction is needed. */
a6c7e777
MC
4883 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4884 && cfun->machine->fp_size == 0
4885 && cfun->machine->gp_size == 0
4886 && cfun->machine->lp_size == 0))
9304f876
CJW
4887 {
4888 /* Create multiple pop instruction rtx. */
a6c7e777
MC
4889 nds32_emit_stack_pop_multiple (
4890 Rb, Re,
4891 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size);
9304f876
CJW
4892 }
4893
557430f1
CJW
4894 /* If this is a variadic function, we do not have to restore argument
4895 registers but need to adjust stack pointer back to previous stack
4896 frame location before return. */
4897 if (cfun->machine->va_args_size != 0)
4898 {
4899 /* Generate sp adjustment instruction.
8a498f99 4900 We need to consider padding bytes here. */
557430f1
CJW
4901 sp_adjust = cfun->machine->va_args_size
4902 + cfun->machine->va_args_area_padding_bytes;
557430f1 4903
a6c7e777
MC
4904 nds32_emit_adjust_frame (stack_pointer_rtx,
4905 stack_pointer_rtx,
4906 sp_adjust);
557430f1
CJW
4907 }
4908
ca3a4a55
CJW
4909 /* If this function uses __builtin_eh_return, make stack adjustment
4910 for exception handler. */
4911 if (cfun->machine->use_eh_return_p)
4912 {
4913 /* We need to unwind the stack by the offset computed by
4914 EH_RETURN_STACKADJ_RTX. However, at this point the CFA is
4915 based on SP. Ideally we would update the SP and define the
4916 CFA along the lines of:
4917
4918 SP = SP + EH_RETURN_STACKADJ_RTX
4919 (regnote CFA = SP - EH_RETURN_STACKADJ_RTX)
4920
4921 However the dwarf emitter only understands a constant
4922 register offset.
4923
4924 The solution chosen here is to use the otherwise $ta ($r15)
4925 as a temporary register to hold the current SP value. The
4926 CFA is described using $ta then SP is modified. */
4927
4928 rtx ta_reg;
4929 rtx insn;
4930
4931 ta_reg = gen_rtx_REG (SImode, TA_REGNUM);
4932
4933 insn = emit_move_insn (ta_reg, stack_pointer_rtx);
4934 add_reg_note (insn, REG_CFA_DEF_CFA, ta_reg);
4935 RTX_FRAME_RELATED_P (insn) = 1;
4936
4937 emit_insn (gen_addsi3 (stack_pointer_rtx,
4938 stack_pointer_rtx,
4939 EH_RETURN_STACKADJ_RTX));
4940
4941 /* Ensure the assignment to $ta does not get optimized away. */
4942 emit_use (ta_reg);
4943 }
4944
03390cda 4945 /* Generate return instruction. */
d6529176
CJW
4946 if (!sibcall_p)
4947 emit_jump_insn (gen_return_internal ());
9304f876
CJW
4948}
4949
4950/* Function for v3push prologue. */
4951void
4952nds32_expand_prologue_v3push (void)
4953{
4954 int fp_adjust;
4955 int sp_adjust;
e2286268 4956 int fpr_space = 0;
a6c7e777 4957 unsigned Rb, Re;
9304f876 4958
9304f876
CJW
4959 /* Compute and setup stack frame size.
4960 The result will be in cfun->machine. */
4961 nds32_compute_stack_frame ();
4962
a6c7e777
MC
4963 if (cfun->machine->callee_saved_gpr_regs_size > 0)
4964 df_set_regs_ever_live (FP_REGNUM, 1);
4965
2140297c
CJW
4966 /* Check frame_pointer_needed again to prevent fp is need after reload. */
4967 if (frame_pointer_needed)
4968 cfun->machine->fp_as_gp_p = false;
4969
9304f876
CJW
4970 /* If the function is 'naked',
4971 we do not have to generate prologue code fragment. */
b26fa4f9 4972 if (cfun->machine->naked_p && !flag_pic)
9304f876
CJW
4973 return;
4974
4975 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
4976 Rb = cfun->machine->callee_saved_first_gpr_regno;
4977 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
4978
4979 /* Calculate sp_adjust first to test if 'push25 Re,imm8u' is available,
4980 where imm8u has to be 8-byte alignment. */
4981 sp_adjust = cfun->machine->local_size
4982 + cfun->machine->out_args_size
e2286268
MC
4983 + cfun->machine->callee_saved_area_gpr_padding_bytes
4984 + cfun->machine->callee_saved_fpr_regs_size;
9304f876
CJW
4985
4986 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
4987 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust))
4988 {
4989 /* We can use 'push25 Re,imm8u'. */
4990
88437f39 4991 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
a6c7e777
MC
4992 the pattern 'stack_v3push' is implemented in nds32.md. */
4993 nds32_emit_stack_v3push (Rb, Re, sp_adjust);
e2286268
MC
4994
4995 /* Save fpu registers. */
4996 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4997 {
4998 /* Calculate fpr position. */
4999 int fpr_position = cfun->machine->local_size
5000 + cfun->machine->out_args_size;
5001 /* Emit fpu store instruction, using [$sp + offset] store
5002 fpu registers. */
5003 nds32_emit_push_fpr_callee_saved (fpr_position);
5004 }
5005
9304f876 5006 /* Check frame_pointer_needed to see
8a498f99 5007 if we shall emit fp adjustment instruction. */
9304f876
CJW
5008 if (frame_pointer_needed)
5009 {
5010 /* adjust $fp = $sp + 4 ($fp size)
8a498f99
CJW
5011 + 4 ($gp size)
5012 + 4 ($lp size)
5013 + (4 * n) (callee-saved registers)
5014 + sp_adjust ('push25 Re,imm8u')
9304f876 5015 Note: Since we use 'push25 Re,imm8u',
8a498f99
CJW
5016 the position of stack pointer is further
5017 changed after push instruction.
5018 Hence, we need to take sp_adjust value
5019 into consideration. */
9304f876
CJW
5020 fp_adjust = cfun->machine->fp_size
5021 + cfun->machine->gp_size
5022 + cfun->machine->lp_size
c457f751 5023 + cfun->machine->callee_saved_gpr_regs_size
9304f876 5024 + sp_adjust;
a6c7e777
MC
5025
5026 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
5027 stack_pointer_rtx,
5028 fp_adjust);
9304f876
CJW
5029 }
5030 }
5031 else
5032 {
e2286268
MC
5033 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5034 {
5035 /* Calculate fpr space. */
5036 fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
5037 + cfun->machine->callee_saved_fpr_regs_size;
5038
5039 /* We have to use 'push25 Re, fpr_space', to pre-allocate
5040 callee saved fpr registers space. */
5041 nds32_emit_stack_v3push (Rb, Re, fpr_space);
5042 nds32_emit_push_fpr_callee_saved (0);
5043 }
5044 else
5045 {
5046 /* We have to use 'push25 Re,0' and
5047 expand one more instruction to adjust $sp later. */
9304f876 5048
e2286268
MC
5049 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
5050 the pattern 'stack_v3push' is implemented in nds32.md. */
5051 nds32_emit_stack_v3push (Rb, Re, 0);
5052 }
9304f876
CJW
5053
5054 /* Check frame_pointer_needed to see
8a498f99 5055 if we shall emit fp adjustment instruction. */
9304f876
CJW
5056 if (frame_pointer_needed)
5057 {
5058 /* adjust $fp = $sp + 4 ($fp size)
8a498f99
CJW
5059 + 4 ($gp size)
5060 + 4 ($lp size)
5061 + (4 * n) (callee-saved registers)
9304f876 5062 Note: Since we use 'push25 Re,0',
8a498f99
CJW
5063 the stack pointer is just at the position
5064 after push instruction.
5065 No need to take sp_adjust into consideration. */
9304f876
CJW
5066 fp_adjust = cfun->machine->fp_size
5067 + cfun->machine->gp_size
5068 + cfun->machine->lp_size
c457f751 5069 + cfun->machine->callee_saved_gpr_regs_size;
a6c7e777 5070
e2286268
MC
5071 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5072 {
5073 /* We use 'push25 Re, fpr_space', the $sp is
5074 on callee saved fpr position, so need to consider
5075 fpr space. */
5076 fp_adjust = fp_adjust + fpr_space;
5077 }
5078
a6c7e777
MC
5079 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
5080 stack_pointer_rtx,
5081 fp_adjust);
9304f876
CJW
5082 }
5083
e2286268
MC
5084 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5085 {
5086 /* We use 'push25 Re, fpr_space',
5087 the $sp is on callee saved fpr position,
5088 no need to consider fpr space. */
5089 sp_adjust = sp_adjust - fpr_space;
5090 }
5091
9304f876 5092 /* Because we use 'push25 Re,0',
8a498f99 5093 we need to expand one more instruction to adjust $sp.
8a498f99 5094 using NEGATIVE value to tell that we are decreasing address. */
a6c7e777
MC
5095 nds32_emit_adjust_frame (stack_pointer_rtx,
5096 stack_pointer_rtx,
5097 -1 * sp_adjust);
9304f876
CJW
5098 }
5099
b26fa4f9
KLC
5100 /* Emit gp setup instructions for -fpic. */
5101 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
5102 nds32_emit_load_gp ();
5103
9304f876
CJW
5104 /* Prevent the instruction scheduler from
5105 moving instructions across the boundary. */
5106 emit_insn (gen_blockage ());
5107}
5108
5109/* Function for v3pop epilogue. */
5110void
d6529176 5111nds32_expand_epilogue_v3pop (bool sibcall_p)
9304f876
CJW
5112{
5113 int sp_adjust;
a6c7e777 5114 unsigned Rb, Re;
9304f876
CJW
5115
5116 /* Compute and setup stack frame size.
5117 The result will be in cfun->machine. */
5118 nds32_compute_stack_frame ();
5119
5120 /* Prevent the instruction scheduler from
5121 moving instructions across the boundary. */
5122 emit_insn (gen_blockage ());
5123
5124 /* If the function is 'naked', we do not have to generate
5125 epilogue code fragment BUT 'ret' instruction. */
5126 if (cfun->machine->naked_p)
5127 {
03390cda 5128 /* Generate return instruction by using 'return_internal' pattern.
54c537e6
CJW
5129 Make sure this instruction is after gen_blockage().
5130 First we need to check this is a function without sibling call. */
d6529176 5131 if (!sibcall_p)
54c537e6
CJW
5132 {
5133 /* We need to further check attributes to determine whether
5134 there should be return instruction at epilogue.
5135 If the attribute naked exists but -mno-ret-in-naked-func
5136 is issued, there is NO need to generate return instruction. */
5137 if (cfun->machine->attr_naked_p && !flag_ret_in_naked_func)
5138 return;
5139
5140 emit_jump_insn (gen_return_internal ());
5141 }
9304f876
CJW
5142 return;
5143 }
5144
5145 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
5146 Rb = cfun->machine->callee_saved_first_gpr_regno;
5147 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
5148
5149 /* Calculate sp_adjust first to test if 'pop25 Re,imm8u' is available,
5150 where imm8u has to be 8-byte alignment. */
5151 sp_adjust = cfun->machine->local_size
5152 + cfun->machine->out_args_size
e2286268
MC
5153 + cfun->machine->callee_saved_area_gpr_padding_bytes
5154 + cfun->machine->callee_saved_fpr_regs_size;
9304f876
CJW
5155
5156 /* We have to consider alloca issue as well.
5157 If the function does call alloca(), the stack pointer is not fixed.
5158 In that case, we cannot use 'pop25 Re,imm8u' directly.
5159 We have to caculate stack pointer from frame pointer
5160 and then use 'pop25 Re,0'.
5161 Of course, the frame_pointer_needed should be nonzero
5162 if the function calls alloca(). */
5163 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
5164 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
5165 && !cfun->calls_alloca)
5166 {
e2286268
MC
5167 /* Restore fpu registers. */
5168 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5169 {
5170 int fpr_position = cfun->machine->local_size
5171 + cfun->machine->out_args_size;
5172 /* Emit fpu load instruction, using [$sp + offset] restore
5173 fpu registers. */
5174 nds32_emit_v3pop_fpr_callee_saved (fpr_position);
5175 }
5176
9304f876
CJW
5177 /* We can use 'pop25 Re,imm8u'. */
5178
88437f39 5179 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
a6c7e777
MC
5180 the pattern 'stack_v3pop' is implementad in nds32.md. */
5181 nds32_emit_stack_v3pop (Rb, Re, sp_adjust);
9304f876
CJW
5182 }
5183 else
5184 {
5185 /* We have to use 'pop25 Re,0', and prior to it,
8a498f99 5186 we must expand one more instruction to adjust $sp. */
9304f876
CJW
5187
5188 if (frame_pointer_needed)
5189 {
5190 /* adjust $sp = $fp - 4 ($fp size)
8a498f99
CJW
5191 - 4 ($gp size)
5192 - 4 ($lp size)
5193 - (4 * n) (callee-saved registers)
9304f876 5194 Note: No need to adjust
8a498f99
CJW
5195 cfun->machine->callee_saved_area_gpr_padding_bytes,
5196 because we want to adjust stack pointer
5197 to the position for pop instruction. */
9304f876
CJW
5198 sp_adjust = cfun->machine->fp_size
5199 + cfun->machine->gp_size
5200 + cfun->machine->lp_size
c457f751 5201 + cfun->machine->callee_saved_gpr_regs_size;
a6c7e777 5202
e2286268
MC
5203 /* Restore fpu registers. */
5204 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5205 {
5206 /* Set $sp to callee saved fpr position, we need to restore
5207 fpr registers. */
5208 sp_adjust = sp_adjust
5209 + cfun->machine->callee_saved_area_gpr_padding_bytes
5210 + cfun->machine->callee_saved_fpr_regs_size;
5211
5212 nds32_emit_adjust_frame (stack_pointer_rtx,
5213 hard_frame_pointer_rtx,
5214 -1 * sp_adjust);
5215
5216 /* Emit fpu load instruction, using [$sp + offset] restore
5217 fpu registers. */
5218 nds32_emit_v3pop_fpr_callee_saved (0);
5219 }
5220 else
5221 {
5222 nds32_emit_adjust_frame (stack_pointer_rtx,
5223 hard_frame_pointer_rtx,
5224 -1 * sp_adjust);
5225 }
9304f876
CJW
5226 }
5227 else
5228 {
5229 /* If frame pointer is NOT needed,
5230 we cannot calculate the sp adjustment from frame pointer.
5231 Instead, we calculate the adjustment by local_size,
5232 out_args_size, and callee_saved_area_padding_bytes.
5233 Notice that such sp adjustment value may be out of range,
5234 so we have to deal with it as well. */
5235
5236 /* Adjust $sp = $sp + local_size + out_args_size
e2286268
MC
5237 + callee_saved_area_gpr_padding_bytes
5238 + callee_saved_fpr_regs_size. */
9304f876
CJW
5239 sp_adjust = cfun->machine->local_size
5240 + cfun->machine->out_args_size
e2286268
MC
5241 + cfun->machine->callee_saved_area_gpr_padding_bytes
5242 + cfun->machine->callee_saved_fpr_regs_size;
5243
5244 /* Restore fpu registers. */
5245 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5246 {
5247 /* Set $sp to callee saved fpr position, we need to restore
5248 fpr registers. */
5249 sp_adjust = sp_adjust
5250 - cfun->machine->callee_saved_area_gpr_padding_bytes
5251 - cfun->machine->callee_saved_fpr_regs_size;
5252
5253 nds32_emit_adjust_frame (stack_pointer_rtx,
5254 stack_pointer_rtx,
5255 sp_adjust);
5256
5257 /* Emit fpu load instruction, using [$sp + offset] restore
5258 fpu registers. */
5259 nds32_emit_v3pop_fpr_callee_saved (0);
5260 }
5261 else
5262 {
5263 /* sp_adjust value may be out of range of the addi instruction,
5264 create alternative add behavior with TA_REGNUM if necessary,
5265 using POSITIVE value to tell that we are increasing
5266 address. */
5267 nds32_emit_adjust_frame (stack_pointer_rtx,
5268 stack_pointer_rtx,
5269 sp_adjust);
5270 }
9304f876
CJW
5271 }
5272
e2286268
MC
5273 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5274 {
5275 /* We have fpr need to restore, so $sp is set on callee saved fpr
5276 position. And we use 'pop25 Re, fpr_space' to adjust $sp. */
5277 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
5278 + cfun->machine->callee_saved_fpr_regs_size;
5279 nds32_emit_stack_v3pop (Rb, Re, fpr_space);
5280 }
5281 else
5282 {
5283 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
5284 the pattern 'stack_v3pop' is implementad in nds32.md. */
5285 nds32_emit_stack_v3pop (Rb, Re, 0);
5286 }
9304f876 5287 }
aa2642ef
CJW
5288 /* Generate return instruction. */
5289 emit_jump_insn (gen_pop25return ());
9304f876
CJW
5290}
5291
03390cda
CJW
5292/* Return nonzero if this function is known to have a null epilogue.
5293 This allows the optimizer to omit jumps to jumps if no stack
5294 was created. */
5295int
5296nds32_can_use_return_insn (void)
5297{
e2286268
MC
5298 int sp_adjust;
5299
03390cda
CJW
5300 /* Prior to reloading, we can't tell how many registers must be saved.
5301 Thus we can not determine whether this function has null epilogue. */
5302 if (!reload_completed)
5303 return 0;
5304
54c537e6
CJW
5305 /* If attribute 'naked' appears but -mno-ret-in-naked-func is used,
5306 we cannot use return instruction. */
5307 if (cfun->machine->attr_naked_p && !flag_ret_in_naked_func)
5308 return 0;
5309
e2286268
MC
5310 sp_adjust = cfun->machine->local_size
5311 + cfun->machine->out_args_size
5312 + cfun->machine->callee_saved_area_gpr_padding_bytes
5313 + cfun->machine->callee_saved_fpr_regs_size;
5314 if (!cfun->machine->fp_as_gp_p
5315 && satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
5316 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
5317 && !cfun->calls_alloca
5318 && NDS32_V3PUSH_AVAILABLE_P
5319 && !(TARGET_HARD_FLOAT
5320 && (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)))
5321 return 1;
5322
03390cda
CJW
5323 /* If no stack was created, two conditions must be satisfied:
5324 1. This is a naked function.
8a498f99 5325 So there is no callee-saved, local size, or outgoing size.
03390cda 5326 2. This is NOT a variadic function.
8a498f99 5327 So there is no pushing arguement registers into the stack. */
03390cda
CJW
5328 return (cfun->machine->naked_p && (cfun->machine->va_args_size == 0));
5329}
5330
2f1bb3ca
SC
5331scalar_int_mode
5332nds32_case_vector_shorten_mode (int min_offset, int max_offset,
5333 rtx body ATTRIBUTE_UNUSED)
5334{
5335 if (min_offset < 0 || max_offset >= 0x2000)
5336 return SImode;
5337 else
5338 {
5339 /* The jump table maybe need to 2 byte alignment,
5340 so reserved 1 byte for check max_offset. */
5341 if (max_offset >= 0xff)
5342 return HImode;
5343 else
5344 return QImode;
5345 }
5346}
5347
9304f876
CJW
5348/* ------------------------------------------------------------------------ */
5349
43fa41c1
CJW
5350/* Return alignment for the label. */
5351int
5352nds32_target_alignment (rtx_insn *label)
5353{
5354 rtx_insn *insn;
5355
5356 if (!NDS32_ALIGN_P ())
5357 return 0;
5358
5359 insn = next_active_insn (label);
5360
5361 /* Always align to 4 byte when first instruction after label is jump
5362 instruction since length for that might changed, so let's always align
5363 it for make sure we don't lose any perfomance here. */
5364 if (insn == 0
5365 || (get_attr_length (insn) == 2
5366 && !JUMP_P (insn) && !CALL_P (insn)))
5367 return 0;
5368 else
5369 return 2;
5370}
5371
79498ad8
CJW
5372/* Return alignment for data. */
5373unsigned int
5374nds32_data_alignment (tree data,
5375 unsigned int basic_align)
5376{
5377 if ((basic_align < BITS_PER_WORD)
5378 && (TREE_CODE (data) == ARRAY_TYPE
5379 || TREE_CODE (data) == UNION_TYPE
5380 || TREE_CODE (data) == RECORD_TYPE))
5381 return BITS_PER_WORD;
5382 else
5383 return basic_align;
5384}
5385
5386/* Return alignment for constant value. */
5387static HOST_WIDE_INT
5388nds32_constant_alignment (const_tree constant,
5389 HOST_WIDE_INT basic_align)
5390{
5391 /* Make string literal and constant for constructor to word align. */
5392 if (((TREE_CODE (constant) == STRING_CST
5393 || TREE_CODE (constant) == CONSTRUCTOR
5394 || TREE_CODE (constant) == UNION_TYPE
5395 || TREE_CODE (constant) == RECORD_TYPE
5396 || TREE_CODE (constant) == ARRAY_TYPE)
5397 && basic_align < BITS_PER_WORD))
5398 return BITS_PER_WORD;
5399 else
5400 return basic_align;
5401}
5402
5403/* Return alignment for local variable. */
5404unsigned int
5405nds32_local_alignment (tree local ATTRIBUTE_UNUSED,
5406 unsigned int basic_align)
5407{
5408 bool at_least_align_to_word = false;
5409 /* Make local array, struct and union at least align to word for make
5410 sure it can unroll memcpy when initialize by constant. */
5411 switch (TREE_CODE (local))
5412 {
5413 case ARRAY_TYPE:
5414 case RECORD_TYPE:
5415 case UNION_TYPE:
5416 at_least_align_to_word = true;
5417 break;
5418 default:
5419 at_least_align_to_word = false;
5420 break;
5421 }
5422 if (at_least_align_to_word
5423 && (basic_align < BITS_PER_WORD))
5424 return BITS_PER_WORD;
5425 else
5426 return basic_align;
5427}
5428
e2286268
MC
5429bool
5430nds32_split_double_word_load_store_p(rtx *operands, bool load_p)
5431{
5432 rtx mem = load_p ? operands[1] : operands[0];
5433 /* Do split at split2 if -O0 or schedule 2 not enable. */
5434 if (optimize == 0 || !flag_schedule_insns_after_reload)
5435 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
5436
5437 /* Split double word load store after copy propgation. */
5438 if (current_pass == NULL)
5439 return false;
5440
5441 const char *pass_name = current_pass->name;
5442 if (pass_name && ((strcmp (pass_name, "split4") == 0)
5443 || (strcmp (pass_name, "split5") == 0)))
5444 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
5445
5446 return false;
5447}
5448
5449static bool
5450nds32_use_blocks_for_constant_p (machine_mode mode,
5451 const_rtx x ATTRIBUTE_UNUSED)
5452{
5453 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
5454 && (mode == DFmode || mode == SFmode))
5455 return true;
5456 else
5457 return false;
5458}
9304f876 5459
9304f876
CJW
5460/* ------------------------------------------------------------------------ */
5461
5462/* PART 5: Initialize target hook structure and definitions. */
5463\f
5464/* Controlling the Compilation Driver. */
5465
5466\f
5467/* Run-time Target Specification. */
5468
5469\f
5470/* Defining Data Structures for Per-function Information. */
5471
5472\f
5473/* Storage Layout. */
5474
5475#undef TARGET_PROMOTE_FUNCTION_MODE
5476#define TARGET_PROMOTE_FUNCTION_MODE \
5477 default_promote_function_mode_always_promote
5478
5f2a98c3
CJW
5479#undef TARGET_EXPAND_TO_RTL_HOOK
5480#define TARGET_EXPAND_TO_RTL_HOOK nds32_expand_to_rtl_hook
5481
79498ad8
CJW
5482#undef TARGET_CONSTANT_ALIGNMENT
5483#define TARGET_CONSTANT_ALIGNMENT nds32_constant_alignment
5484
9304f876
CJW
5485\f
5486/* Layout of Source Language Data Types. */
5487
5488\f
5489/* Register Usage. */
5490
5491/* -- Basic Characteristics of Registers. */
5492
e2286268
MC
5493#undef TARGET_CONDITIONAL_REGISTER_USAGE
5494#define TARGET_CONDITIONAL_REGISTER_USAGE nds32_conditional_register_usage
5495
9304f876
CJW
5496/* -- Order of Allocation of Registers. */
5497
5498/* -- How Values Fit in Registers. */
5499
e2286268
MC
5500#undef TARGET_HARD_REGNO_NREGS
5501#define TARGET_HARD_REGNO_NREGS nds32_hard_regno_nregs
5502
50256c75
CJW
5503#undef TARGET_HARD_REGNO_MODE_OK
5504#define TARGET_HARD_REGNO_MODE_OK nds32_hard_regno_mode_ok
5505
5506#undef TARGET_MODES_TIEABLE_P
5507#define TARGET_MODES_TIEABLE_P nds32_modes_tieable_p
5508
9304f876
CJW
5509/* -- Handling Leaf Functions. */
5510
5511/* -- Registers That Form a Stack. */
5512
5513\f
5514/* Register Classes. */
5515
5516#undef TARGET_CLASS_MAX_NREGS
5517#define TARGET_CLASS_MAX_NREGS nds32_class_max_nregs
5518
9304f876
CJW
5519#undef TARGET_REGISTER_PRIORITY
5520#define TARGET_REGISTER_PRIORITY nds32_register_priority
5521
e2286268
MC
5522#undef TARGET_CAN_CHANGE_MODE_CLASS
5523#define TARGET_CAN_CHANGE_MODE_CLASS nds32_can_change_mode_class
5524
9304f876
CJW
5525\f
5526/* Obsolete Macros for Defining Constraints. */
5527
5528\f
5529/* Stack Layout and Calling Conventions. */
5530
5531/* -- Basic Stack Layout. */
5532
5533/* -- Exception Handling Support. */
5534
5535/* -- Specifying How Stack Checking is Done. */
5536
5537/* -- Registers That Address the Stack Frame. */
5538
5539/* -- Eliminating Frame Pointer and Arg Pointer. */
5540
5541#undef TARGET_CAN_ELIMINATE
5542#define TARGET_CAN_ELIMINATE nds32_can_eliminate
5543
5544/* -- Passing Function Arguments on the Stack. */
5545
5546/* -- Passing Arguments in Registers. */
5547
5548#undef TARGET_FUNCTION_ARG
5549#define TARGET_FUNCTION_ARG nds32_function_arg
5550
d40f3c40
CJW
5551#undef TARGET_MUST_PASS_IN_STACK
5552#define TARGET_MUST_PASS_IN_STACK nds32_must_pass_in_stack
5553
650fc469
CJW
5554#undef TARGET_ARG_PARTIAL_BYTES
5555#define TARGET_ARG_PARTIAL_BYTES nds32_arg_partial_bytes
5556
9304f876
CJW
5557#undef TARGET_FUNCTION_ARG_ADVANCE
5558#define TARGET_FUNCTION_ARG_ADVANCE nds32_function_arg_advance
5559
5560#undef TARGET_FUNCTION_ARG_BOUNDARY
5561#define TARGET_FUNCTION_ARG_BOUNDARY nds32_function_arg_boundary
5562
7c32ef41
MC
5563#undef TARGET_VECTOR_MODE_SUPPORTED_P
5564#define TARGET_VECTOR_MODE_SUPPORTED_P nds32_vector_mode_supported_p
5565
9304f876
CJW
5566/* -- How Scalar Function Values Are Returned. */
5567
5568#undef TARGET_FUNCTION_VALUE
5569#define TARGET_FUNCTION_VALUE nds32_function_value
5570
5571#undef TARGET_LIBCALL_VALUE
5572#define TARGET_LIBCALL_VALUE nds32_libcall_value
5573
5574#undef TARGET_FUNCTION_VALUE_REGNO_P
5575#define TARGET_FUNCTION_VALUE_REGNO_P nds32_function_value_regno_p
5576
5577/* -- How Large Values Are Returned. */
5578
e2286268
MC
5579#undef TARGET_RETURN_IN_MEMORY
5580#define TARGET_RETURN_IN_MEMORY nds32_return_in_memory
5581
9304f876
CJW
5582/* -- Caller-Saves Register Allocation. */
5583
5584/* -- Function Entry and Exit. */
5585
5586#undef TARGET_ASM_FUNCTION_PROLOGUE
5587#define TARGET_ASM_FUNCTION_PROLOGUE nds32_asm_function_prologue
5588
5589#undef TARGET_ASM_FUNCTION_END_PROLOGUE
5590#define TARGET_ASM_FUNCTION_END_PROLOGUE nds32_asm_function_end_prologue
5591
5592#undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
5593#define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE nds32_asm_function_begin_epilogue
5594
5595#undef TARGET_ASM_FUNCTION_EPILOGUE
5596#define TARGET_ASM_FUNCTION_EPILOGUE nds32_asm_function_epilogue
5597
5598#undef TARGET_ASM_OUTPUT_MI_THUNK
5599#define TARGET_ASM_OUTPUT_MI_THUNK nds32_asm_output_mi_thunk
5600
5601#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
5602#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
5603
5604/* -- Generating Code for Profiling. */
5605
5606/* -- Permitting tail calls. */
5607
cc48a87f
CJW
5608#undef TARGET_FUNCTION_OK_FOR_SIBCALL
5609#define TARGET_FUNCTION_OK_FOR_SIBCALL nds32_function_ok_for_sibcall
5610
9304f876
CJW
5611#undef TARGET_WARN_FUNC_RETURN
5612#define TARGET_WARN_FUNC_RETURN nds32_warn_func_return
5613
5614/* Stack smashing protection. */
5615
5616\f
5617/* Implementing the Varargs Macros. */
5618
d4a6a4d9
CJW
5619#undef TARGET_SETUP_INCOMING_VARARGS
5620#define TARGET_SETUP_INCOMING_VARARGS nds32_setup_incoming_varargs
5621
9304f876
CJW
5622#undef TARGET_STRICT_ARGUMENT_NAMING
5623#define TARGET_STRICT_ARGUMENT_NAMING nds32_strict_argument_naming
5624
5625\f
5626/* Trampolines for Nested Functions. */
5627
5628#undef TARGET_ASM_TRAMPOLINE_TEMPLATE
5629#define TARGET_ASM_TRAMPOLINE_TEMPLATE nds32_asm_trampoline_template
5630
5631#undef TARGET_TRAMPOLINE_INIT
5632#define TARGET_TRAMPOLINE_INIT nds32_trampoline_init
5633
5634\f
5635/* Implicit Calls to Library Routines. */
5636
5637\f
5638/* Addressing Modes. */
5639
5640#undef TARGET_LEGITIMATE_ADDRESS_P
5641#define TARGET_LEGITIMATE_ADDRESS_P nds32_legitimate_address_p
5642
85a98076
KLC
5643#undef TARGET_LEGITIMIZE_ADDRESS
5644#define TARGET_LEGITIMIZE_ADDRESS nds32_legitimize_address
5645
5646#undef TARGET_LEGITIMATE_CONSTANT_P
5647#define TARGET_LEGITIMATE_CONSTANT_P nds32_legitimate_constant_p
5648
7c32ef41
MC
5649#undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
5650#define TARGET_VECTORIZE_PREFERRED_SIMD_MODE nds32_vectorize_preferred_simd_mode
5651
85a98076
KLC
5652#undef TARGET_CANNOT_FORCE_CONST_MEM
5653#define TARGET_CANNOT_FORCE_CONST_MEM nds32_cannot_force_const_mem
5654
5655#undef TARGET_DELEGITIMIZE_ADDRESS
5656#define TARGET_DELEGITIMIZE_ADDRESS nds32_delegitimize_address
5657
9304f876
CJW
5658\f
5659/* Anchored Addresses. */
5660
5661\f
5662/* Condition Code Status. */
5663
5664/* -- Representation of condition codes using (cc0). */
5665
5666/* -- Representation of condition codes using registers. */
5667
5b6f2bf3
CJW
5668#undef TARGET_CANONICALIZE_COMPARISON
5669#define TARGET_CANONICALIZE_COMPARISON nds32_canonicalize_comparison
5670
9304f876
CJW
5671/* -- Macros to control conditional execution. */
5672
5673\f
5674/* Describing Relative Costs of Operations. */
5675
5676#undef TARGET_REGISTER_MOVE_COST
5677#define TARGET_REGISTER_MOVE_COST nds32_register_move_cost
5678
5679#undef TARGET_MEMORY_MOVE_COST
5680#define TARGET_MEMORY_MOVE_COST nds32_memory_move_cost
5681
5682#undef TARGET_RTX_COSTS
5683#define TARGET_RTX_COSTS nds32_rtx_costs
5684
5685#undef TARGET_ADDRESS_COST
5686#define TARGET_ADDRESS_COST nds32_address_cost
5687
5688\f
5689/* Adjusting the Instruction Scheduler. */
5690
5691\f
5692/* Dividing the Output into Sections (Texts, Data, . . . ). */
5693
511a41d7
CJW
5694#undef TARGET_ENCODE_SECTION_INFO
5695#define TARGET_ENCODE_SECTION_INFO nds32_encode_section_info
5696
9304f876
CJW
5697\f
5698/* Position Independent Code. */
5699
5700\f
5701/* Defining the Output Assembler Language. */
5702
5703/* -- The Overall Framework of an Assembler File. */
5704
5705#undef TARGET_ASM_FILE_START
5706#define TARGET_ASM_FILE_START nds32_asm_file_start
5707#undef TARGET_ASM_FILE_END
5708#define TARGET_ASM_FILE_END nds32_asm_file_end
5709
5710/* -- Output of Data. */
5711
5712#undef TARGET_ASM_ALIGNED_HI_OP
5713#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
5714
5715#undef TARGET_ASM_ALIGNED_SI_OP
5716#define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
5717
85a98076
KLC
5718#undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
5719#define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA nds32_asm_output_addr_const_extra
5720
9304f876
CJW
5721/* -- Output of Uninitialized Variables. */
5722
5723/* -- Output and Generation of Labels. */
5724
5725#undef TARGET_ASM_GLOBALIZE_LABEL
5726#define TARGET_ASM_GLOBALIZE_LABEL nds32_asm_globalize_label
5727
5728/* -- How Initialization Functions Are Handled. */
5729
5730/* -- Macros Controlling Initialization Routines. */
5731
5732/* -- Output of Assembler Instructions. */
5733
5734#undef TARGET_PRINT_OPERAND
5735#define TARGET_PRINT_OPERAND nds32_print_operand
5736#undef TARGET_PRINT_OPERAND_ADDRESS
5737#define TARGET_PRINT_OPERAND_ADDRESS nds32_print_operand_address
5738
5739/* -- Output of Dispatch Tables. */
5740
5741/* -- Assembler Commands for Exception Regions. */
5742
e2286268
MC
5743#undef TARGET_DWARF_REGISTER_SPAN
5744#define TARGET_DWARF_REGISTER_SPAN nds32_dwarf_register_span
5745
9304f876
CJW
5746/* -- Assembler Commands for Alignment. */
5747
5748\f
5749/* Controlling Debugging Information Format. */
5750
5751/* -- Macros Affecting All Debugging Formats. */
5752
5753/* -- Specific Options for DBX Output. */
5754
5755/* -- Open-Ended Hooks for DBX Format. */
5756
5757/* -- File Names in DBX Format. */
5758
180295ed 5759/* -- Macros for DWARF Output. */
9304f876
CJW
5760
5761/* -- Macros for VMS Debug Format. */
5762
5763\f
5764/* Cross Compilation and Floating Point. */
5765
5766\f
5767/* Mode Switching Instructions. */
5768
5769\f
5770/* Defining target-specific uses of __attribute__. */
5771
5772#undef TARGET_ATTRIBUTE_TABLE
5773#define TARGET_ATTRIBUTE_TABLE nds32_attribute_table
5774
5775#undef TARGET_MERGE_DECL_ATTRIBUTES
5776#define TARGET_MERGE_DECL_ATTRIBUTES nds32_merge_decl_attributes
5777
5778#undef TARGET_INSERT_ATTRIBUTES
5779#define TARGET_INSERT_ATTRIBUTES nds32_insert_attributes
5780
5781#undef TARGET_OPTION_PRAGMA_PARSE
5782#define TARGET_OPTION_PRAGMA_PARSE nds32_option_pragma_parse
5783
5784#undef TARGET_OPTION_OVERRIDE
5785#define TARGET_OPTION_OVERRIDE nds32_option_override
5786
5787\f
5788/* Emulating TLS. */
5789
cf3cd43d
CJW
5790#undef TARGET_HAVE_TLS
5791#define TARGET_HAVE_TLS TARGET_LINUX_ABI
5792
9304f876
CJW
5793\f
5794/* Defining coprocessor specifics for MIPS targets. */
5795
5796\f
5797/* Parameters for Precompiled Header Validity Checking. */
5798
5799\f
5800/* C++ ABI parameters. */
5801
5802\f
5803/* Adding support for named address spaces. */
5804
5805\f
5806/* Miscellaneous Parameters. */
5807
823835a9
CJW
5808#undef TARGET_MD_ASM_ADJUST
5809#define TARGET_MD_ASM_ADJUST nds32_md_asm_adjust
5810
9304f876
CJW
5811#undef TARGET_INIT_BUILTINS
5812#define TARGET_INIT_BUILTINS nds32_init_builtins
5813
7a12ea32
MC
5814#undef TARGET_BUILTIN_DECL
5815#define TARGET_BUILTIN_DECL nds32_builtin_decl
5816
9304f876
CJW
5817#undef TARGET_EXPAND_BUILTIN
5818#define TARGET_EXPAND_BUILTIN nds32_expand_builtin
5819
cf3cd43d
CJW
5820#undef TARGET_INIT_LIBFUNCS
5821#define TARGET_INIT_LIBFUNCS nds32_init_libfuncs
e2286268
MC
5822
5823#undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
5824#define TARGET_USE_BLOCKS_FOR_CONSTANT_P nds32_use_blocks_for_constant_p
5825
9304f876
CJW
5826\f
5827/* ------------------------------------------------------------------------ */
5828
5829/* Initialize the GCC target structure. */
5830
5831struct gcc_target targetm = TARGET_INITIALIZER;
5832
5833/* ------------------------------------------------------------------------ */