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