]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/nds32/nds32.cc
Merge remote-tracking branch 'origin/master' into devel/c++-contracts
[thirdparty/gcc.git] / gcc / config / nds32 / nds32.cc
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;
4fe34cdc
JM
2380 if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl)))
2381 num_of_used_regs
2382 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, arg.mode, arg.type)
2383 + NDS32_NEED_N_REGS_FOR_ARG (arg.mode, arg.type);
2384 else
2385 num_of_used_regs = cum->gpr_offset + NDS32_GPR_ARG_FIRST_REGNUM;
d4a6a4d9
CJW
2386
2387 remaining_reg_count = total_args_regs - num_of_used_regs;
2388 *pretend_args_size = remaining_reg_count * UNITS_PER_WORD;
2389
2390 return;
2391}
2392
9304f876
CJW
2393static bool
2394nds32_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
2395{
d4a6a4d9
CJW
2396 /* If this hook returns true, the named argument of FUNCTION_ARG is always
2397 true for named arguments, and false for unnamed arguments. */
9304f876
CJW
2398 return true;
2399}
2400
2401\f
2402/* Trampolines for Nested Functions. */
2403
2404static void
2405nds32_asm_trampoline_template (FILE *f)
2406{
2407 if (TARGET_REDUCED_REGS)
2408 {
2409 /* Trampoline is not supported on reduced-set registers yet. */
2410 sorry ("a nested function is not supported for reduced registers");
2411 }
2412 else
2413 {
2414 asm_fprintf (f, "\t! Trampoline code template\n");
2415 asm_fprintf (f, "\t! This code fragment will be copied "
2416 "into stack on demand\n");
2417
2418 asm_fprintf (f, "\tmfusr\t$r16,$pc\n");
2419 asm_fprintf (f, "\tlwi\t$r15,[$r16 + 20] "
2420 "! load nested function address\n");
2421 asm_fprintf (f, "\tlwi\t$r16,[$r16 + 16] "
2422 "! load chain_value\n");
2423 asm_fprintf (f, "\tjr\t$r15\n");
2424 }
2425
2426 /* Preserve space ($pc + 16) for saving chain_value,
2427 nds32_trampoline_init will fill the value in this slot. */
2428 asm_fprintf (f, "\t! space for saving chain_value\n");
2429 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2430
2431 /* Preserve space ($pc + 20) for saving nested function address,
2432 nds32_trampoline_init will fill the value in this slot. */
2433 asm_fprintf (f, "\t! space for saving nested function address\n");
2434 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2435}
2436
2437/* Emit RTL insns to initialize the variable parts of a trampoline. */
2438static void
2439nds32_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2440{
2441 int i;
2442
2443 /* Nested function address. */
2444 rtx fnaddr;
2445 /* The memory rtx that is going to
2446 be filled with chain_value. */
2447 rtx chain_value_mem;
2448 /* The memory rtx that is going to
2449 be filled with nested function address. */
2450 rtx nested_func_mem;
2451
2452 /* Start address of trampoline code in stack, for doing cache sync. */
2453 rtx sync_cache_addr;
2454 /* Temporary register for sync instruction. */
2455 rtx tmp_reg;
2456 /* Instruction-cache sync instruction,
2457 requesting an argument as starting address. */
2458 rtx isync_insn;
2459 /* For convenience reason of doing comparison. */
2460 int tramp_align_in_bytes;
2461
2462 /* Trampoline is not supported on reduced-set registers yet. */
2463 if (TARGET_REDUCED_REGS)
2464 sorry ("a nested function is not supported for reduced registers");
2465
2466 /* STEP 1: Copy trampoline code template into stack,
8a498f99 2467 fill up essential data into stack. */
9304f876
CJW
2468
2469 /* Extract nested function address rtx. */
2470 fnaddr = XEXP (DECL_RTL (fndecl), 0);
2471
2472 /* m_tramp is memory rtx that is going to be filled with trampoline code.
2473 We have nds32_asm_trampoline_template() to emit template pattern. */
2474 emit_block_move (m_tramp, assemble_trampoline_template (),
2475 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2476
2477 /* After copying trampoline code into stack,
2478 fill chain_value into stack. */
2479 chain_value_mem = adjust_address (m_tramp, SImode, 16);
2480 emit_move_insn (chain_value_mem, chain_value);
2481 /* After copying trampoline code int stack,
2482 fill nested function address into stack. */
2483 nested_func_mem = adjust_address (m_tramp, SImode, 20);
2484 emit_move_insn (nested_func_mem, fnaddr);
2485
2486 /* STEP 2: Sync instruction-cache. */
2487
2488 /* We have successfully filled trampoline code into stack.
2489 However, in order to execute code in stack correctly,
2490 we must sync instruction cache. */
2491 sync_cache_addr = XEXP (m_tramp, 0);
2492 tmp_reg = gen_reg_rtx (SImode);
2493 isync_insn = gen_unspec_volatile_isync (tmp_reg);
2494
2495 /* Because nds32_cache_block_size is in bytes,
2496 we get trampoline alignment in bytes for convenient comparison. */
2497 tramp_align_in_bytes = TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT;
2498
2499 if (tramp_align_in_bytes >= nds32_cache_block_size
2500 && (tramp_align_in_bytes % nds32_cache_block_size) == 0)
2501 {
2502 /* Under this condition, the starting address of trampoline
8a498f99
CJW
2503 must be aligned to the starting address of each cache block
2504 and we do not have to worry about cross-boundary issue. */
9304f876
CJW
2505 for (i = 0;
2506 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2507 / nds32_cache_block_size;
2508 i++)
2509 {
2510 emit_move_insn (tmp_reg,
2511 plus_constant (Pmode, sync_cache_addr,
2512 nds32_cache_block_size * i));
2513 emit_insn (isync_insn);
2514 }
2515 }
2516 else if (TRAMPOLINE_SIZE > nds32_cache_block_size)
2517 {
2518 /* The starting address of trampoline code
8a498f99
CJW
2519 may not be aligned to the cache block,
2520 so the trampoline code may be across two cache block.
2521 We need to sync the last element, which is 4-byte size,
2522 of trampoline template. */
9304f876
CJW
2523 for (i = 0;
2524 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
2525 / nds32_cache_block_size;
2526 i++)
2527 {
2528 emit_move_insn (tmp_reg,
2529 plus_constant (Pmode, sync_cache_addr,
2530 nds32_cache_block_size * i));
2531 emit_insn (isync_insn);
2532 }
2533
2534 /* The last element of trampoline template is 4-byte size. */
2535 emit_move_insn (tmp_reg,
2536 plus_constant (Pmode, sync_cache_addr,
2537 TRAMPOLINE_SIZE - 4));
2538 emit_insn (isync_insn);
2539 }
2540 else
2541 {
2542 /* This is the simplest case.
8a498f99
CJW
2543 Because TRAMPOLINE_SIZE is less than or
2544 equal to nds32_cache_block_size,
2545 we can just sync start address and
2546 the last element of trampoline code. */
9304f876
CJW
2547
2548 /* Sync starting address of tampoline code. */
2549 emit_move_insn (tmp_reg, sync_cache_addr);
2550 emit_insn (isync_insn);
2551 /* Sync the last element, which is 4-byte size,
8a498f99 2552 of trampoline template. */
9304f876
CJW
2553 emit_move_insn (tmp_reg,
2554 plus_constant (Pmode, sync_cache_addr,
2555 TRAMPOLINE_SIZE - 4));
2556 emit_insn (isync_insn);
2557 }
2558
2559 /* Set instruction serialization barrier
2560 to guarantee the correct operations. */
2561 emit_insn (gen_unspec_volatile_isb ());
2562}
2563
2564\f
2565/* Addressing Modes. */
2566
2567static bool
ef4bddc2 2568nds32_legitimate_address_p (machine_mode mode, rtx x, bool strict)
9304f876 2569{
e2286268
MC
2570 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
2571 {
2572 /* When using floating-point instructions,
2573 we don't allow 'addr' to be [symbol_ref], [CONST] pattern. */
2574 if ((mode == DFmode || mode == SFmode)
2575 && (GET_CODE (x) == SYMBOL_REF
2576 || GET_CODE(x) == CONST))
2577 return false;
2578
2579 /* Allow [post_modify] addressing mode, when using FPU instructions. */
2580 if (GET_CODE (x) == POST_MODIFY
2581 && mode == DFmode)
2582 {
2583 if (GET_CODE (XEXP (x, 0)) == REG
2584 && GET_CODE (XEXP (x, 1)) == PLUS)
2585 {
2586 rtx plus_op = XEXP (x, 1);
2587 rtx op0 = XEXP (plus_op, 0);
2588 rtx op1 = XEXP (plus_op, 1);
2589
2590 if (nds32_address_register_rtx_p (op0, strict)
2591 && CONST_INT_P (op1))
2592 {
2593 if (satisfies_constraint_Is14 (op1))
2594 {
5f2a98c3
CJW
2595 /* If it is not under strictly aligned situation,
2596 we can return true without checking alignment. */
2597 if (!cfun->machine->strict_aligned_p)
2598 return true;
e2286268
MC
2599 /* Make sure address is word alignment.
2600 Currently we do not have 64-bit load/store yet,
2601 so we will use two 32-bit load/store instructions to do
2602 memory access and they are single word alignment. */
5f2a98c3 2603 else if (NDS32_SINGLE_WORD_ALIGN_P (INTVAL (op1)))
e2286268
MC
2604 return true;
2605 }
2606 }
2607 }
2608 }
2609 }
2610
9304f876
CJW
2611 /* For (mem:DI addr) or (mem:DF addr) case,
2612 we only allow 'addr' to be [reg], [symbol_ref],
8a498f99 2613 [const], or [reg + const_int] pattern. */
9304f876
CJW
2614 if (mode == DImode || mode == DFmode)
2615 {
2616 /* Allow [Reg + const_int] addressing mode. */
2617 if (GET_CODE (x) == PLUS)
2618 {
2619 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2620 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict)
2621 && CONST_INT_P (XEXP (x, 1)))
2622 return true;
9304f876
CJW
2623 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2624 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict)
2625 && CONST_INT_P (XEXP (x, 0)))
2626 return true;
2627 }
2628
e2286268
MC
2629 /* Allow [post_inc] and [post_dec] addressing mode. */
2630 if (GET_CODE (x) == POST_INC || GET_CODE (x) == POST_DEC)
2631 {
2632 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2633 return true;
2634 }
2635
9304f876
CJW
2636 /* Now check [reg], [symbol_ref], and [const]. */
2637 if (GET_CODE (x) != REG
2638 && GET_CODE (x) != SYMBOL_REF
2639 && GET_CODE (x) != CONST)
2640 return false;
2641 }
2642
2643 /* Check if 'x' is a valid address. */
2644 switch (GET_CODE (x))
2645 {
2646 case REG:
2647 /* (mem (reg A)) => [Ra] */
2648 return nds32_address_register_rtx_p (x, strict);
2649
2650 case SYMBOL_REF:
4855be84 2651 /* (mem (symbol_ref A)) => [symbol_ref] */
b26fa4f9
KLC
2652
2653 if (flag_pic || SYMBOL_REF_TLS_MODEL (x))
2654 return false;
2655
85a98076
KLC
2656 if (TARGET_ICT_MODEL_LARGE && nds32_indirect_call_referenced_p (x))
2657 return false;
2658
4855be84 2659 /* If -mcmodel=large, the 'symbol_ref' is not a valid address
8a498f99 2660 during or after LRA/reload phase. */
4855be84 2661 if (TARGET_CMODEL_LARGE
9304f876
CJW
2662 && (reload_completed
2663 || reload_in_progress
2664 || lra_in_progress))
2665 return false;
4855be84 2666 /* If -mcmodel=medium and the symbol references to rodata section,
8a498f99
CJW
2667 the 'symbol_ref' is not a valid address during or after
2668 LRA/reload phase. */
4855be84 2669 if (TARGET_CMODEL_MEDIUM
b26fa4f9
KLC
2670 && (NDS32_SYMBOL_REF_RODATA_P (x)
2671 || CONSTANT_POOL_ADDRESS_P (x))
9304f876
CJW
2672 && (reload_completed
2673 || reload_in_progress
2674 || lra_in_progress))
2675 return false;
2676
4855be84
CJW
2677 return true;
2678
2679 case CONST:
9304f876 2680 /* (mem (const (...)))
8a498f99 2681 => [ + const_addr ], where const_addr = symbol_ref + const_int */
9304f876
CJW
2682 if (GET_CODE (XEXP (x, 0)) == PLUS)
2683 {
2684 rtx plus_op = XEXP (x, 0);
2685
2686 rtx op0 = XEXP (plus_op, 0);
2687 rtx op1 = XEXP (plus_op, 1);
2688
2689 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
4855be84
CJW
2690 {
2691 /* Now we see the [ + const_addr ] pattern, but we need
8a498f99 2692 some further checking. */
b26fa4f9 2693
4a7df256 2694 if (flag_pic || SYMBOL_REF_TLS_MODEL (op0))
b26fa4f9
KLC
2695 return false;
2696
4855be84 2697 /* If -mcmodel=large, the 'const_addr' is not a valid address
8a498f99 2698 during or after LRA/reload phase. */
4855be84
CJW
2699 if (TARGET_CMODEL_LARGE
2700 && (reload_completed
2701 || reload_in_progress
2702 || lra_in_progress))
2703 return false;
2704 /* If -mcmodel=medium and the symbol references to rodata section,
8a498f99
CJW
2705 the 'const_addr' is not a valid address during or after
2706 LRA/reload phase. */
4855be84
CJW
2707 if (TARGET_CMODEL_MEDIUM
2708 && NDS32_SYMBOL_REF_RODATA_P (op0)
2709 && (reload_completed
2710 || reload_in_progress
2711 || lra_in_progress))
2712 return false;
2713
2714 /* At this point we can make sure 'const_addr' is a
2715 valid address. */
2716 return true;
2717 }
9304f876
CJW
2718 }
2719
2720 return false;
2721
2722 case POST_MODIFY:
2723 /* (mem (post_modify (reg) (plus (reg) (reg))))
8a498f99 2724 => [Ra], Rb */
9304f876 2725 /* (mem (post_modify (reg) (plus (reg) (const_int))))
8a498f99 2726 => [Ra], const_int */
9304f876
CJW
2727 if (GET_CODE (XEXP (x, 0)) == REG
2728 && GET_CODE (XEXP (x, 1)) == PLUS)
2729 {
2730 rtx plus_op = XEXP (x, 1);
2731
2732 rtx op0 = XEXP (plus_op, 0);
2733 rtx op1 = XEXP (plus_op, 1);
2734
2735 if (nds32_address_register_rtx_p (op0, strict)
2736 && nds32_legitimate_index_p (mode, op1, strict))
2737 return true;
2738 else
2739 return false;
2740 }
2741
2742 return false;
2743
2744 case POST_INC:
2745 case POST_DEC:
2746 /* (mem (post_inc reg)) => [Ra], 1/2/4 */
2747 /* (mem (post_dec reg)) => [Ra], -1/-2/-4 */
2748 /* The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
8a498f99 2749 We only need to deal with register Ra. */
9304f876
CJW
2750 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2751 return true;
2752 else
2753 return false;
2754
2755 case PLUS:
2756 /* (mem (plus reg const_int))
8a498f99 2757 => [Ra + imm] */
9304f876 2758 /* (mem (plus reg reg))
8a498f99 2759 => [Ra + Rb] */
9304f876 2760 /* (mem (plus (mult reg const_int) reg))
8a498f99 2761 => [Ra + Rb << sv] */
9304f876
CJW
2762 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2763 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict))
2764 return true;
2765 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2766 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict))
2767 return true;
2768 else
2769 return false;
2770
2771 case LO_SUM:
34425025 2772 /* (mem (lo_sum (reg) (symbol_ref))) */
85a98076 2773 /* (mem (lo_sum (reg) (const (plus (symbol_ref) (reg)))) */
b26fa4f9
KLC
2774 /* TLS case: (mem (lo_sum (reg) (const (unspec symbol_ref X)))) */
2775 /* The LO_SUM is a valid address if and only if we would like to
2776 generate 32-bit full address memory access with any of following
2777 circumstance:
2778 1. -mcmodel=large.
2779 2. -mcmodel=medium and the symbol_ref references to rodata. */
85a98076
KLC
2780 {
2781 rtx sym = NULL_RTX;
2782
b26fa4f9
KLC
2783 if (flag_pic)
2784 return false;
2785
85a98076
KLC
2786 if (!REG_P (XEXP (x, 0)))
2787 return false;
2788
2789 if (GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
2790 sym = XEXP (x, 1);
2791 else if (GET_CODE (XEXP (x, 1)) == CONST)
2792 {
2793 rtx plus = XEXP(XEXP (x, 1), 0);
2794 if (GET_CODE (plus) == PLUS)
2795 sym = XEXP (plus, 0);
2796 else if (GET_CODE (plus) == UNSPEC)
2797 sym = XVECEXP (plus, 0, 0);
2798 }
2799 else
2800 return false;
2801
2802 gcc_assert (GET_CODE (sym) == SYMBOL_REF);
2803
2804 if (TARGET_ICT_MODEL_LARGE
2805 && nds32_indirect_call_referenced_p (sym))
2806 return true;
2807
2808 if (TARGET_CMODEL_LARGE)
2809 return true;
2810 else if (TARGET_CMODEL_MEDIUM
2811 && NDS32_SYMBOL_REF_RODATA_P (sym))
2812 return true;
2813 else
2814 return false;
2815 }
9304f876
CJW
2816
2817 default:
2818 return false;
2819 }
2820}
2821
85a98076
KLC
2822static rtx
2823nds32_legitimize_address (rtx x,
2824 rtx oldx ATTRIBUTE_UNUSED,
2825 machine_mode mode ATTRIBUTE_UNUSED)
2826{
b26fa4f9
KLC
2827 if (nds32_tls_referenced_p (x))
2828 x = nds32_legitimize_tls_address (x);
2829 else if (flag_pic && SYMBOLIC_CONST_P (x))
2830 x = nds32_legitimize_pic_address (x);
2831 else if (TARGET_ICT_MODEL_LARGE && nds32_indirect_call_referenced_p (x))
85a98076
KLC
2832 x = nds32_legitimize_ict_address (x);
2833
2834 return x;
2835}
2836
2837static bool
2838nds32_legitimate_constant_p (machine_mode mode, rtx x)
2839{
2840 switch (GET_CODE (x))
2841 {
2842 case CONST_DOUBLE:
2843 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
2844 && (mode == DFmode || mode == SFmode))
2845 return false;
2846 break;
2847 case CONST:
2848 x = XEXP (x, 0);
2849
2850 if (GET_CODE (x) == PLUS)
2851 {
2852 if (!CONST_INT_P (XEXP (x, 1)))
2853 return false;
2854 x = XEXP (x, 0);
2855 }
2856
2857 if (GET_CODE (x) == UNSPEC)
2858 {
2859 switch (XINT (x, 1))
2860 {
b26fa4f9
KLC
2861 case UNSPEC_GOT:
2862 case UNSPEC_GOTOFF:
2863 case UNSPEC_PLT:
2864 case UNSPEC_TLSGD:
2865 case UNSPEC_TLSLD:
2866 case UNSPEC_TLSIE:
2867 case UNSPEC_TLSLE:
85a98076
KLC
2868 case UNSPEC_ICT:
2869 return false;
2870 default:
2871 return true;
2872 }
2873 }
2874 break;
b26fa4f9
KLC
2875 case SYMBOL_REF:
2876 /* TLS symbols need a call to resolve in
2877 precompute_register_parameters. */
2878 if (SYMBOL_REF_TLS_MODEL (x))
2879 return false;
2880 break;
85a98076
KLC
2881 default:
2882 return true;
2883 }
2884
2885 return true;
2886}
2887
2888/* Reorgnize the UNSPEC CONST and return its direct symbol. */
2889static rtx
2890nds32_delegitimize_address (rtx x)
2891{
2892 x = delegitimize_mem_from_attrs (x);
2893
2894 if (GET_CODE(x) == CONST)
2895 {
2896 rtx inner = XEXP (x, 0);
2897
2898 /* Handle for GOTOFF. */
2899 if (GET_CODE (inner) == PLUS)
2900 inner = XEXP (inner, 0);
2901
2902 if (GET_CODE (inner) == UNSPEC)
2903 {
2904 switch (XINT (inner, 1))
2905 {
b26fa4f9
KLC
2906 case UNSPEC_GOTINIT:
2907 case UNSPEC_GOT:
2908 case UNSPEC_GOTOFF:
2909 case UNSPEC_PLT:
2910 case UNSPEC_TLSGD:
2911 case UNSPEC_TLSLD:
2912 case UNSPEC_TLSIE:
2913 case UNSPEC_TLSLE:
85a98076
KLC
2914 case UNSPEC_ICT:
2915 x = XVECEXP (inner, 0, 0);
2916 break;
2917 default:
2918 break;
2919 }
2920 }
2921 }
2922 return x;
2923}
2924
7c32ef41
MC
2925static machine_mode
2926nds32_vectorize_preferred_simd_mode (scalar_mode mode)
2927{
2928 if (!NDS32_EXT_DSP_P ())
2929 return word_mode;
2930
2931 switch (mode)
2932 {
2933 case E_QImode:
2934 return V4QImode;
2935 case E_HImode:
2936 return V2HImode;
2937 default:
2938 return word_mode;
2939 }
2940}
2941
85a98076
KLC
2942static bool
2943nds32_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2944{
2945 switch (GET_CODE (x))
2946 {
2947 case CONST:
2948 return !nds32_legitimate_constant_p (mode, x);
2949 case SYMBOL_REF:
2950 /* All symbols have to be accessed through gp-relative in PIC mode. */
2951 /* We don't want to force symbol as constant pool in .text section,
2952 because we use the gp-relatived instruction to load in small
2953 or medium model. */
b26fa4f9
KLC
2954 if (flag_pic
2955 || SYMBOL_REF_TLS_MODEL (x)
85a98076
KLC
2956 || TARGET_CMODEL_SMALL
2957 || TARGET_CMODEL_MEDIUM)
2958 return true;
2959 break;
b26fa4f9
KLC
2960 case CONST_INT:
2961 case CONST_DOUBLE:
2962 if (flag_pic && (lra_in_progress || reload_completed))
2963 return true;
2964 break;
85a98076
KLC
2965 default:
2966 return false;
2967 }
2968 return false;
2969}
2970
5b6f2bf3
CJW
2971\f
2972/* Condition Code Status. */
2973
2974/* -- Representation of condition codes using registers. */
2975
2976static void
2977nds32_canonicalize_comparison (int *code,
2978 rtx *op0 ATTRIBUTE_UNUSED,
2979 rtx *op1,
2980 bool op0_preserve_value ATTRIBUTE_UNUSED)
2981{
2982 /* When the instruction combination pass tries to combine a comparison insn
2983 with its previous insns, it also transforms the operator in order to
2984 minimize its constant field. For example, it tries to transform a
2985 comparison insn from
2986 (set (reg:SI 54)
2987 (ltu:SI (reg:SI 52)
2988 (const_int 10 [0xa])))
2989 to
2990 (set (reg:SI 54)
2991 (leu:SI (reg:SI 52)
2992 (const_int 9 [0x9])))
2993
2994 However, the nds32 target only provides instructions supporting the LTU
2995 operation directly, and the implementation of the pattern "cbranchsi4"
2996 only expands the LTU form. In order to handle the non-LTU operations
2997 generated from passes other than the RTL expansion pass, we have to
2998 implement this hook to revert those changes. Since we only expand the LTU
2999 operator in the RTL expansion pass, we might only need to handle the LEU
3000 case, unless we find other optimization passes perform more aggressive
3001 transformations. */
3002
3003 if (*code == LEU && CONST_INT_P (*op1))
3004 {
3005 *op1 = gen_int_mode (INTVAL (*op1) + 1, SImode);
3006 *code = LTU;
3007 }
3008}
3009
9304f876
CJW
3010\f
3011/* Describing Relative Costs of Operations. */
3012
19ac960a 3013static int
69e7672a 3014nds32_register_move_cost (machine_mode mode,
19ac960a
CJW
3015 reg_class_t from,
3016 reg_class_t to)
9304f876 3017{
69e7672a
KC
3018 /* In garywolf cpu, FPR to GPR is chaper than other cpu. */
3019 if (TARGET_PIPELINE_GRAYWOLF)
3020 {
3021 if (GET_MODE_SIZE (mode) == 8)
3022 {
3023 /* DPR to GPR. */
3024 if (from == FP_REGS && to != FP_REGS)
3025 return 3;
3026 /* GPR to DPR. */
3027 if (from != FP_REGS && to == FP_REGS)
3028 return 2;
3029 }
3030 else
3031 {
3032 if ((from == FP_REGS && to != FP_REGS)
3033 || (from != FP_REGS && to == FP_REGS))
3034 return 2;
3035 }
3036 }
3037
e2286268
MC
3038 if ((from == FP_REGS && to != FP_REGS)
3039 || (from != FP_REGS && to == FP_REGS))
69e7672a 3040 return 3;
e2286268
MC
3041 else if (from == HIGH_REGS || to == HIGH_REGS)
3042 return optimize_size ? 6 : 2;
3043 else
3044 return 2;
9304f876
CJW
3045}
3046
19ac960a 3047static int
ef4bddc2 3048nds32_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
19ac960a
CJW
3049 reg_class_t rclass ATTRIBUTE_UNUSED,
3050 bool in ATTRIBUTE_UNUSED)
9304f876
CJW
3051{
3052 return 8;
3053}
3054
3055/* This target hook describes the relative costs of RTL expressions.
3056 Return 'true' when all subexpressions of x have been processed.
3057 Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
e53b6e56 3058 Refer to gcc/rtlanal.cc for more information. */
9304f876
CJW
3059static bool
3060nds32_rtx_costs (rtx x,
e548c9df 3061 machine_mode mode,
9304f876 3062 int outer_code,
89a4b547 3063 int opno,
9304f876
CJW
3064 int *total,
3065 bool speed)
3066{
e548c9df 3067 return nds32_rtx_costs_impl (x, mode, outer_code, opno, total, speed);
9304f876
CJW
3068}
3069
19ac960a
CJW
3070static int
3071nds32_address_cost (rtx address,
ef4bddc2 3072 machine_mode mode,
19ac960a
CJW
3073 addr_space_t as,
3074 bool speed)
9304f876 3075{
89a4b547 3076 return nds32_address_cost_impl (address, mode, as, speed);
9304f876
CJW
3077}
3078
511a41d7
CJW
3079\f
3080/* Dividing the Output into Sections (Texts, Data, . . . ). */
3081
3082/* If references to a symbol or a constant must be treated differently
3083 depending on something about the variable or function named by the symbol
3084 (such as what section it is in), we use this hook to store flags
3085 in symbol_ref rtx. */
3086static void
3087nds32_encode_section_info (tree decl, rtx rtl, int new_decl_p)
3088{
3089 default_encode_section_info (decl, rtl, new_decl_p);
3090
3091 /* For the memory rtx, if it references to rodata section, we can store
3092 NDS32_SYMBOL_FLAG_RODATA flag into symbol_ref rtx so that the
3093 nds32_legitimate_address_p() can determine how to treat such symbol_ref
3094 based on -mcmodel=X and this information. */
3095 if (MEM_P (rtl) && MEM_READONLY_P (rtl))
3096 {
3097 rtx addr = XEXP (rtl, 0);
3098
3099 if (GET_CODE (addr) == SYMBOL_REF)
3100 {
3101 /* For (mem (symbol_ref X)) case. */
3102 SYMBOL_REF_FLAGS (addr) |= NDS32_SYMBOL_FLAG_RODATA;
3103 }
3104 else if (GET_CODE (addr) == CONST
3105 && GET_CODE (XEXP (addr, 0)) == PLUS)
3106 {
3107 /* For (mem (const (plus (symbol_ref X) (const_int N)))) case. */
3108 rtx plus_op = XEXP (addr, 0);
3109 rtx op0 = XEXP (plus_op, 0);
3110 rtx op1 = XEXP (plus_op, 1);
3111
3112 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
3113 SYMBOL_REF_FLAGS (op0) |= NDS32_SYMBOL_FLAG_RODATA;
3114 }
3115 }
3116}
3117
9304f876
CJW
3118\f
3119/* Defining the Output Assembler Language. */
3120
3121/* -- The Overall Framework of an Assembler File. */
3122
3123static void
3124nds32_asm_file_start (void)
3125{
9304f876
CJW
3126 default_file_start ();
3127
b26fa4f9
KLC
3128 if (flag_pic)
3129 fprintf (asm_out_file, "\t.pic\n");
3130
9304f876
CJW
3131 /* Tell assembler which ABI we are using. */
3132 fprintf (asm_out_file, "\t! ABI version\n");
e2286268
MC
3133 if (TARGET_HARD_FLOAT)
3134 fprintf (asm_out_file, "\t.abi_2fp_plus\n");
3135 else
3136 fprintf (asm_out_file, "\t.abi_2\n");
9304f876
CJW
3137
3138 /* Tell assembler that this asm code is generated by compiler. */
3139 fprintf (asm_out_file, "\t! This asm file is generated by compiler\n");
3140 fprintf (asm_out_file, "\t.flag\tverbatim\n");
85a98076 3141
cf3cd43d
CJW
3142 /* Insert directive for linker to distinguish object's ict flag. */
3143 if (!TARGET_LINUX_ABI)
3144 {
3145 if (TARGET_ICT_MODEL_LARGE)
3146 fprintf (asm_out_file, "\t.ict_model\tlarge\n");
3147 else
3148 fprintf (asm_out_file, "\t.ict_model\tsmall\n");
3149 }
3150
3151 /* We need to provide the size of each vector for interrupt handler
3152 under elf toolchain. */
3153 if (!TARGET_LINUX_ABI)
3154 {
3155 fprintf (asm_out_file, "\t! This vector size directive is required "
3156 "for checking inconsistency on interrupt handler\n");
3157 fprintf (asm_out_file, "\t.vec_size\t%d\n", nds32_isr_vector_size);
3158 }
9304f876 3159
2140297c
CJW
3160 /* If user enables '-mforce-fp-as-gp' or compiles programs with -Os,
3161 the compiler may produce 'la $fp,_FP_BASE_' instruction
3162 at prologue for fp-as-gp optimization.
3163 We should emit weak reference of _FP_BASE_ to avoid undefined reference
3164 in case user does not pass '--relax' option to linker. */
cf3cd43d 3165 if (!TARGET_LINUX_ABI && (TARGET_FORCE_FP_AS_GP || optimize_size))
2140297c
CJW
3166 {
3167 fprintf (asm_out_file, "\t! This weak reference is required to do "
3168 "fp-as-gp link time optimization\n");
3169 fprintf (asm_out_file, "\t.weak\t_FP_BASE_\n");
3170 }
3171
9304f876
CJW
3172 fprintf (asm_out_file, "\t! ------------------------------------\n");
3173
3174 if (TARGET_ISA_V2)
3175 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V2");
3176 if (TARGET_ISA_V3)
3177 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3");
3178 if (TARGET_ISA_V3M)
3179 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3M");
3180
dd000841
CJW
3181 switch (nds32_cpu_option)
3182 {
3183 case CPU_N6:
3184 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N6");
3185 break;
3186
3187 case CPU_N7:
3188 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N7");
3189 break;
3190
3191 case CPU_N8:
3192 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N8");
3193 break;
3194
3195 case CPU_E8:
3196 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "E8");
3197 break;
3198
3199 case CPU_N9:
3200 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N9");
3201 break;
3202
3203 case CPU_N10:
3204 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N10");
3205 break;
3206
3207 case CPU_GRAYWOLF:
3208 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "Graywolf");
3209 break;
3210
3211 case CPU_N12:
3212 case CPU_N13:
3213 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "N13");
3214 break;
3215
3216 case CPU_SIMPLE:
3217 fprintf (asm_out_file, "\t! Pipeline model\t: %s\n", "SIMPLE");
3218 break;
3219
3220 default:
3221 gcc_unreachable ();
3222 }
3223
77b7a1ca
CJW
3224 if (TARGET_CMODEL_SMALL)
3225 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "SMALL");
3226 if (TARGET_CMODEL_MEDIUM)
3227 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "MEDIUM");
3228 if (TARGET_CMODEL_LARGE)
3229 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "LARGE");
3230
9304f876
CJW
3231 fprintf (asm_out_file, "\t! Endian setting\t: %s\n",
3232 ((TARGET_BIG_ENDIAN) ? "big-endian"
3233 : "little-endian"));
e2286268
MC
3234 fprintf (asm_out_file, "\t! Use SP floating-point instruction\t: %s\n",
3235 ((TARGET_FPU_SINGLE) ? "Yes"
3236 : "No"));
3237 fprintf (asm_out_file, "\t! Use DP floating-point instruction\t: %s\n",
3238 ((TARGET_FPU_DOUBLE) ? "Yes"
3239 : "No"));
3240 fprintf (asm_out_file, "\t! ABI version\t\t: %s\n",
3241 ((TARGET_HARD_FLOAT) ? "ABI2FP+"
3242 : "ABI2"));
9304f876
CJW
3243
3244 fprintf (asm_out_file, "\t! ------------------------------------\n");
3245
3246 fprintf (asm_out_file, "\t! Use conditional move\t\t: %s\n",
3247 ((TARGET_CMOV) ? "Yes"
3248 : "No"));
3249 fprintf (asm_out_file, "\t! Use performance extension\t: %s\n",
aa4b851c 3250 ((TARGET_EXT_PERF) ? "Yes"
9304f876 3251 : "No"));
aa4b851c
CJW
3252 fprintf (asm_out_file, "\t! Use performance extension 2\t: %s\n",
3253 ((TARGET_EXT_PERF2) ? "Yes"
3254 : "No"));
3255 fprintf (asm_out_file, "\t! Use string extension\t\t: %s\n",
3256 ((TARGET_EXT_STRING) ? "Yes"
3257 : "No"));
9304f876
CJW
3258
3259 fprintf (asm_out_file, "\t! ------------------------------------\n");
3260
3261 fprintf (asm_out_file, "\t! V3PUSH instructions\t: %s\n",
3262 ((TARGET_V3PUSH) ? "Yes"
3263 : "No"));
3264 fprintf (asm_out_file, "\t! 16-bit instructions\t: %s\n",
3265 ((TARGET_16_BIT) ? "Yes"
3266 : "No"));
9304f876
CJW
3267 fprintf (asm_out_file, "\t! Reduced registers set\t: %s\n",
3268 ((TARGET_REDUCED_REGS) ? "Yes"
3269 : "No"));
3270
bc8a8810
MC
3271 fprintf (asm_out_file, "\t! Support unaligned access\t\t: %s\n",
3272 (flag_unaligned_access ? "Yes"
3273 : "No"));
3274
9304f876
CJW
3275 fprintf (asm_out_file, "\t! ------------------------------------\n");
3276
3277 if (optimize_size)
3278 fprintf (asm_out_file, "\t! Optimization level\t: -Os\n");
10c8bd03
CJW
3279 else if (optimize_fast)
3280 fprintf (asm_out_file, "\t! Optimization level\t: -Ofast\n");
3281 else if (optimize_debug)
3282 fprintf (asm_out_file, "\t! Optimization level\t: -Og\n");
9304f876
CJW
3283 else
3284 fprintf (asm_out_file, "\t! Optimization level\t: -O%d\n", optimize);
3285
3286 fprintf (asm_out_file, "\t! ------------------------------------\n");
3287
3288 fprintf (asm_out_file, "\t! Cache block size\t: %d\n",
3289 nds32_cache_block_size);
3290
3291 fprintf (asm_out_file, "\t! ------------------------------------\n");
3292
c23a919b 3293 nds32_asm_file_start_for_isr ();
9304f876
CJW
3294}
3295
3296static void
3297nds32_asm_file_end (void)
3298{
c23a919b 3299 nds32_asm_file_end_for_isr ();
9304f876 3300
cf3cd43d
CJW
3301 /* The NDS32 Linux stack is mapped non-executable by default, so add a
3302 .note.GNU-stack section. */
3303 if (TARGET_LINUX_ABI)
3304 file_end_indicate_exec_stack ();
3305
9304f876
CJW
3306 fprintf (asm_out_file, "\t! ------------------------------------\n");
3307}
3308
85a98076
KLC
3309static bool
3310nds32_asm_output_addr_const_extra (FILE *file, rtx x)
3311{
3312 if (GET_CODE (x) == UNSPEC)
3313 {
3314 switch (XINT (x, 1))
3315 {
b26fa4f9
KLC
3316 case UNSPEC_GOTINIT:
3317 output_addr_const (file, XVECEXP (x, 0, 0));
3318 break;
3319 case UNSPEC_GOTOFF:
3320 output_addr_const (file, XVECEXP (x, 0, 0));
3321 fputs ("@GOTOFF", file);
3322 break;
3323 case UNSPEC_GOT:
3324 output_addr_const (file, XVECEXP (x, 0, 0));
3325 fputs ("@GOT", file);
3326 break;
3327 case UNSPEC_PLT:
3328 output_addr_const (file, XVECEXP (x, 0, 0));
3329 fputs ("@PLT", file);
3330 break;
3331 case UNSPEC_TLSGD:
3332 output_addr_const (file, XVECEXP (x, 0, 0));
3333 fputs ("@TLSDESC", file);
3334 break;
3335 case UNSPEC_TLSLD:
3336 output_addr_const (file, XVECEXP (x, 0, 0));
3337 fputs ("@TLSDESC", file);
3338 break;
3339 case UNSPEC_TLSIE:
3340 output_addr_const (file, XVECEXP (x, 0, 0));
3341 fputs ("@GOTTPOFF", file);
3342 break;
3343 case UNSPEC_TLSLE:
3344 output_addr_const (file, XVECEXP (x, 0, 0));
3345 fputs ("@TPOFF", file);
3346 break;
85a98076
KLC
3347 case UNSPEC_ICT:
3348 output_addr_const (file, XVECEXP (x, 0, 0));
3349 fputs ("@ICT", file);
3350 break;
3351 default:
3352 return false;
3353 }
3354 return true;
3355 }
3356 else
3357 return false;
3358}
3359
9304f876
CJW
3360/* -- Output and Generation of Labels. */
3361
3362static void
3363nds32_asm_globalize_label (FILE *stream, const char *name)
3364{
3365 fputs ("\t.global\t", stream);
3366 assemble_name (stream, name);
3367 fputs ("\n", stream);
3368}
3369
3370/* -- Output of Assembler Instructions. */
3371
3372static void
3373nds32_print_operand (FILE *stream, rtx x, int code)
3374{
bfeb6685 3375 HOST_WIDE_INT op_value = 0;
a3b13564
KC
3376 HOST_WIDE_INT one_position;
3377 HOST_WIDE_INT zero_position;
e2286268
MC
3378 bool pick_lsb_p = false;
3379 bool pick_msb_p = false;
3380 int regno;
3381
bfeb6685
CJW
3382 if (CONST_INT_P (x))
3383 op_value = INTVAL (x);
9304f876
CJW
3384
3385 switch (code)
3386 {
3387 case 0 :
3388 /* Do nothing special. */
3389 break;
3390
a3b13564
KC
3391 case 'b':
3392 /* Use exact_log2() to search the 0-bit position. */
3393 gcc_assert (CONST_INT_P (x));
3394 zero_position = exact_log2 (~UINTVAL (x) & GET_MODE_MASK (SImode));
3395 gcc_assert (zero_position != -1);
3396 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, zero_position);
3397
3398 /* No need to handle following process, so return immediately. */
3399 return;
3400
8656bbcf
SC
3401 case 'e':
3402 gcc_assert (MEM_P (x)
3403 && GET_CODE (XEXP (x, 0)) == PLUS
3404 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
3405 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (XEXP (x, 0), 1)));
3406
3407 /* No need to handle following process, so return immediately. */
3408 return;
7c32ef41
MC
3409
3410 case 'v':
3411 gcc_assert (CONST_INT_P (x)
3412 && (INTVAL (x) == 0
3413 || INTVAL (x) == 8
3414 || INTVAL (x) == 16
3415 || INTVAL (x) == 24));
3416 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) / 8);
3417
3418 /* No need to handle following process, so return immediately. */
3419 return;
3420
a3b13564
KC
3421 case 'B':
3422 /* Use exact_log2() to search the 1-bit position. */
3423 gcc_assert (CONST_INT_P (x));
3424 one_position = exact_log2 (UINTVAL (x) & GET_MODE_MASK (SImode));
3425 gcc_assert (one_position != -1);
3426 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, one_position);
3427
3428 /* No need to handle following process, so return immediately. */
3429 return;
3430
e2286268
MC
3431 case 'L':
3432 /* X is supposed to be REG rtx. */
3433 gcc_assert (REG_P (x));
3434 /* Claim that we are going to pick LSB part of X. */
3435 pick_lsb_p = true;
3436 break;
3437
3438 case 'H':
3439 /* X is supposed to be REG rtx. */
3440 gcc_assert (REG_P (x));
3441 /* Claim that we are going to pick MSB part of X. */
3442 pick_msb_p = true;
3443 break;
3444
9304f876
CJW
3445 case 'V':
3446 /* 'x' is supposed to be CONST_INT, get the value. */
3447 gcc_assert (CONST_INT_P (x));
9304f876
CJW
3448
3449 /* According to the Andes architecture,
8a498f99
CJW
3450 the system/user register index range is 0 ~ 1023.
3451 In order to avoid conflict between user-specified-integer value
3452 and enum-specified-register value,
3453 the 'enum nds32_intrinsic_registers' value
3454 in nds32_intrinsic.h starts from 1024. */
9304f876
CJW
3455 if (op_value < 1024 && op_value >= 0)
3456 {
3457 /* If user gives integer value directly (0~1023),
3458 we just print out the value. */
5af50159 3459 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, op_value);
9304f876
CJW
3460 }
3461 else if (op_value < 0
3462 || op_value >= ((int) ARRAY_SIZE (nds32_intrinsic_register_names)
3463 + 1024))
3464 {
3465 /* The enum index value for array size is out of range. */
3466 error ("intrinsic register index is out of range");
3467 }
3468 else
3469 {
3470 /* If user applies normal way with __NDS32_REG_XXX__ enum data,
3471 we can print out register name. Remember to substract 1024. */
3472 fprintf (stream, "%s",
3473 nds32_intrinsic_register_names[op_value - 1024]);
3474 }
3475
3476 /* No need to handle following process, so return immediately. */
3477 return;
3478
f1a0afe2
MC
3479 case 'R': /* cctl valck */
3480 /* Note the cctl divide to 5 group and share the same name table. */
3481 if (op_value < 0 || op_value > 4)
67f84a80 3482 error ("CCTL intrinsic function subtype out of range");
f1a0afe2
MC
3483 fprintf (stream, "%s", nds32_cctl_names[op_value]);
3484 return;
3485
3486 case 'T': /* cctl idxwbinv */
3487 /* Note the cctl divide to 5 group and share the same name table. */
3488 if (op_value < 0 || op_value > 4)
67f84a80 3489 error ("CCTL intrinsic function subtype out of range");
f1a0afe2
MC
3490 fprintf (stream, "%s", nds32_cctl_names[op_value + 4]);
3491 return;
3492
3493 case 'U': /* cctl vawbinv */
3494 /* Note the cctl divide to 5 group and share the same name table. */
3495 if (op_value < 0 || op_value > 4)
67f84a80 3496 error ("CCTL intrinsic function subtype out of range");
f1a0afe2
MC
3497 fprintf (stream, "%s", nds32_cctl_names[op_value + 8]);
3498 return;
3499
3500 case 'X': /* cctl idxread */
3501 /* Note the cctl divide to 5 group and share the same name table. */
3502 if (op_value < 0 || op_value > 4)
67f84a80 3503 error ("CCTL intrinsic function subtype out of range");
f1a0afe2
MC
3504 fprintf (stream, "%s", nds32_cctl_names[op_value + 12]);
3505 return;
3506
3507 case 'W': /* cctl idxwitre */
3508 /* Note the cctl divide to 5 group and share the same name table. */
3509 if (op_value < 0 || op_value > 4)
67f84a80 3510 error ("CCTL intrinsic function subtype out of range");
f1a0afe2
MC
3511 fprintf (stream, "%s", nds32_cctl_names[op_value + 16]);
3512 return;
3513
57aaf0cc
MC
3514 case 'Z': /* dpref */
3515 fprintf (stream, "%s", nds32_dpref_names[op_value]);
3516 return;
3517
9304f876
CJW
3518 default :
3519 /* Unknown flag. */
3520 output_operand_lossage ("invalid operand output code");
3521 break;
3522 }
3523
3524 switch (GET_CODE (x))
3525 {
3526 case LABEL_REF:
85a98076
KLC
3527 output_addr_const (stream, x);
3528 break;
3529
9304f876
CJW
3530 case SYMBOL_REF:
3531 output_addr_const (stream, x);
85a98076 3532
cf3cd43d 3533 if (!TARGET_LINUX_ABI && nds32_indirect_call_referenced_p (x))
85a98076
KLC
3534 fprintf (stream, "@ICT");
3535
9304f876
CJW
3536 break;
3537
3538 case REG:
e2286268
MC
3539 /* Print a Double-precision register name. */
3540 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3541 && NDS32_IS_FPR_REGNUM (REGNO (x)))
3542 {
3543 regno = REGNO (x);
3544 if (!NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno))
3545 {
3546 output_operand_lossage ("invalid operand for code '%c'", code);
3547 break;
3548 }
3549 fprintf (stream, "$fd%d", (regno - NDS32_FIRST_FPR_REGNUM) >> 1);
3550 break;
3551 }
3552
3553 /* Print LSB or MSB part of register pair if the
3554 constraint modifier 'L' or 'H' is specified. */
3555 if ((GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
3556 && NDS32_IS_GPR_REGNUM (REGNO (x)))
3557 {
3558 if ((pick_lsb_p && WORDS_BIG_ENDIAN)
3559 || (pick_msb_p && !WORDS_BIG_ENDIAN))
3560 {
3561 /* If we would like to print out LSB register under big-endian,
3562 or print out MSB register under little-endian, we need to
3563 increase register number. */
3564 regno = REGNO (x);
3565 regno++;
3566 fputs (reg_names[regno], stream);
3567 break;
3568 }
3569 }
3570
9304f876 3571 /* Forbid using static chain register ($r16)
8a498f99 3572 on reduced-set registers configuration. */
9304f876
CJW
3573 if (TARGET_REDUCED_REGS
3574 && REGNO (x) == STATIC_CHAIN_REGNUM)
3575 sorry ("a nested function is not supported for reduced registers");
3576
3577 /* Normal cases, print out register name. */
3578 fputs (reg_names[REGNO (x)], stream);
3579 break;
3580
3581 case MEM:
cc8ca59e 3582 output_address (GET_MODE (x), XEXP (x, 0));
9304f876
CJW
3583 break;
3584
e2286268
MC
3585 case HIGH:
3586 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE)
3587 {
3588 const REAL_VALUE_TYPE *rv;
3589 long val;
3590 gcc_assert (GET_MODE (x) == SFmode);
3591
3592 rv = CONST_DOUBLE_REAL_VALUE (XEXP (x, 0));
3593 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3594
3595 fprintf (stream, "hi20(0x%lx)", val);
3596 }
3597 else
3598 gcc_unreachable ();
3599 break;
3600
3601 case CONST_DOUBLE:
3602 const REAL_VALUE_TYPE *rv;
3603 long val;
3604 gcc_assert (GET_MODE (x) == SFmode);
3605
3606 rv = CONST_DOUBLE_REAL_VALUE (x);
3607 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
3608
3609 fprintf (stream, "0x%lx", val);
3610 break;
3611
9304f876
CJW
3612 case CODE_LABEL:
3613 case CONST_INT:
3614 case CONST:
3615 output_addr_const (stream, x);
3616 break;
3617
7c32ef41
MC
3618 case CONST_VECTOR:
3619 fprintf (stream, HOST_WIDE_INT_PRINT_HEX, const_vector_to_hwint (x));
3620 break;
3621
85a98076
KLC
3622 case LO_SUM:
3623 /* This is a special case for inline assembly using memory address 'p'.
3624 The inline assembly code is expected to use pesudo instruction
3625 for the operand. EX: la */
3626 output_addr_const (stream, XEXP(x, 1));
3627 break;
3628
9304f876
CJW
3629 default:
3630 /* Generally, output_addr_const () is able to handle most cases.
8a498f99
CJW
3631 We want to see what CODE could appear,
3632 so we use gcc_unreachable() to stop it. */
9304f876
CJW
3633 debug_rtx (x);
3634 gcc_unreachable ();
3635 break;
3636 }
3637}
3638
3639static void
85a98076
KLC
3640nds32_print_operand_address (FILE *stream,
3641 machine_mode mode ATTRIBUTE_UNUSED,
3642 rtx x)
9304f876
CJW
3643{
3644 rtx op0, op1;
3645
3646 switch (GET_CODE (x))
3647 {
3648 case SYMBOL_REF:
3649 case CONST:
3650 /* [ + symbol_ref] */
3651 /* [ + const_addr], where const_addr = symbol_ref + const_int */
3652 fputs ("[ + ", stream);
3653 output_addr_const (stream, x);
3654 fputs ("]", stream);
3655 break;
3656
85a98076
KLC
3657 case LO_SUM:
3658 /* This is a special case for inline assembly using memory operand 'm'.
3659 The inline assembly code is expected to use pesudo instruction
3660 for the operand. EX: [ls].[bhw] */
3661 fputs ("[ + ", stream);
3662 op1 = XEXP (x, 1);
3663 output_addr_const (stream, op1);
3664 fputs ("]", stream);
3665 break;
3666
9304f876
CJW
3667 case REG:
3668 /* Forbid using static chain register ($r16)
8a498f99 3669 on reduced-set registers configuration. */
9304f876
CJW
3670 if (TARGET_REDUCED_REGS
3671 && REGNO (x) == STATIC_CHAIN_REGNUM)
3672 sorry ("a nested function is not supported for reduced registers");
3673
3674 /* [Ra] */
3675 fprintf (stream, "[%s]", reg_names[REGNO (x)]);
3676 break;
3677
3678 case PLUS:
3679 op0 = XEXP (x, 0);
3680 op1 = XEXP (x, 1);
3681
3682 /* Checking op0, forbid using static chain register ($r16)
8a498f99 3683 on reduced-set registers configuration. */
9304f876
CJW
3684 if (TARGET_REDUCED_REGS
3685 && REG_P (op0)
3686 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3687 sorry ("a nested function is not supported for reduced registers");
3688 /* Checking op1, forbid using static chain register ($r16)
8a498f99 3689 on reduced-set registers configuration. */
9304f876
CJW
3690 if (TARGET_REDUCED_REGS
3691 && REG_P (op1)
3692 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3693 sorry ("a nested function is not supported for reduced registers");
3694
3695 if (REG_P (op0) && CONST_INT_P (op1))
3696 {
3697 /* [Ra + imm] */
5af50159
KC
3698 fprintf (stream, "[%s + (" HOST_WIDE_INT_PRINT_DEC ")]",
3699 reg_names[REGNO (op0)], INTVAL (op1));
9304f876
CJW
3700 }
3701 else if (REG_P (op0) && REG_P (op1))
3702 {
3703 /* [Ra + Rb] */
3704 fprintf (stream, "[%s + %s]",
3705 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3706 }
3707 else if (GET_CODE (op0) == MULT && REG_P (op1))
3708 {
3709 /* [Ra + Rb << sv]
3710 From observation, the pattern looks like:
3711 (plus:SI (mult:SI (reg:SI 58)
8a498f99
CJW
3712 (const_int 4 [0x4]))
3713 (reg/f:SI 57)) */
9304f876
CJW
3714 int sv;
3715
3716 /* We need to set sv to output shift value. */
3717 if (INTVAL (XEXP (op0, 1)) == 1)
3718 sv = 0;
3719 else if (INTVAL (XEXP (op0, 1)) == 2)
3720 sv = 1;
3721 else if (INTVAL (XEXP (op0, 1)) == 4)
3722 sv = 2;
f62a2af5
CJW
3723 else if (INTVAL (XEXP (op0, 1)) == 8)
3724 sv = 3;
9304f876
CJW
3725 else
3726 gcc_unreachable ();
3727
3728 fprintf (stream, "[%s + %s << %d]",
3729 reg_names[REGNO (op1)],
3730 reg_names[REGNO (XEXP (op0, 0))],
3731 sv);
3732 }
7c32ef41
MC
3733 else if (GET_CODE (op0) == ASHIFT && REG_P (op1))
3734 {
3735 /* [Ra + Rb << sv]
3736 In normal, ASHIFT can be converted to MULT like above case.
3737 But when the address rtx does not go through canonicalize_address
3738 defined in fwprop, we'll need this case. */
3739 int sv = INTVAL (XEXP (op0, 1));
3740 gcc_assert (sv <= 3 && sv >=0);
3741
3742 fprintf (stream, "[%s + %s << %d]",
3743 reg_names[REGNO (op1)],
3744 reg_names[REGNO (XEXP (op0, 0))],
3745 sv);
3746 }
9304f876
CJW
3747 else
3748 {
3749 /* The control flow is not supposed to be here. */
3750 debug_rtx (x);
3751 gcc_unreachable ();
3752 }
3753
3754 break;
3755
3756 case POST_MODIFY:
3757 /* (post_modify (regA) (plus (regA) (regB)))
8a498f99
CJW
3758 (post_modify (regA) (plus (regA) (const_int)))
3759 We would like to extract
3760 regA and regB (or const_int) from plus rtx. */
9304f876
CJW
3761 op0 = XEXP (XEXP (x, 1), 0);
3762 op1 = XEXP (XEXP (x, 1), 1);
3763
3764 /* Checking op0, forbid using static chain register ($r16)
8a498f99 3765 on reduced-set registers configuration. */
9304f876
CJW
3766 if (TARGET_REDUCED_REGS
3767 && REG_P (op0)
3768 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3769 sorry ("a nested function is not supported for reduced registers");
3770 /* Checking op1, forbid using static chain register ($r16)
8a498f99 3771 on reduced-set registers configuration. */
9304f876
CJW
3772 if (TARGET_REDUCED_REGS
3773 && REG_P (op1)
3774 && REGNO (op1) == STATIC_CHAIN_REGNUM)
3775 sorry ("a nested function is not supported for reduced registers");
3776
3777 if (REG_P (op0) && REG_P (op1))
3778 {
3779 /* [Ra], Rb */
3780 fprintf (stream, "[%s], %s",
3781 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
3782 }
3783 else if (REG_P (op0) && CONST_INT_P (op1))
3784 {
3785 /* [Ra], imm */
5af50159
KC
3786 fprintf (stream, "[%s], " HOST_WIDE_INT_PRINT_DEC,
3787 reg_names[REGNO (op0)], INTVAL (op1));
9304f876
CJW
3788 }
3789 else
3790 {
3791 /* The control flow is not supposed to be here. */
3792 debug_rtx (x);
3793 gcc_unreachable ();
3794 }
3795
3796 break;
3797
3798 case POST_INC:
3799 case POST_DEC:
3800 op0 = XEXP (x, 0);
3801
3802 /* Checking op0, forbid using static chain register ($r16)
8a498f99 3803 on reduced-set registers configuration. */
9304f876
CJW
3804 if (TARGET_REDUCED_REGS
3805 && REG_P (op0)
3806 && REGNO (op0) == STATIC_CHAIN_REGNUM)
3807 sorry ("a nested function is not supported for reduced registers");
3808
3809 if (REG_P (op0))
3810 {
3811 /* "[Ra], 1/2/4" or "[Ra], -1/-2/-4"
3812 The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
3813 We only need to deal with register Ra. */
3814 fprintf (stream, "[%s]", reg_names[REGNO (op0)]);
3815 }
3816 else
3817 {
3818 /* The control flow is not supposed to be here. */
3819 debug_rtx (x);
3820 gcc_unreachable ();
3821 }
3822
3823 break;
3824
3825 default :
3826 /* Generally, output_addr_const () is able to handle most cases.
8a498f99
CJW
3827 We want to see what CODE could appear,
3828 so we use gcc_unreachable() to stop it. */
9304f876
CJW
3829 debug_rtx (x);
3830 gcc_unreachable ();
3831 break;
3832 }
3833}
3834
e2286268
MC
3835/* -- Assembler Commands for Exception Regions. */
3836
3837static rtx
3838nds32_dwarf_register_span (rtx reg)
3839{
3840 rtx dwarf_high, dwarf_low;
3841 rtx dwarf_single;
3842 machine_mode mode;
3843 int regno;
3844
3845 mode = GET_MODE (reg);
3846 regno = REGNO (reg);
3847
3848 /* We need to adjust dwarf register information for floating-point registers
3849 rather than using default register number mapping. */
3850 if (regno >= NDS32_FIRST_FPR_REGNUM
3851 && regno <= NDS32_LAST_FPR_REGNUM)
3852 {
3853 if (mode == DFmode || mode == SCmode)
3854 {
3855 /* By default, GCC maps increasing register numbers to increasing
3856 memory locations, but paired FPRs in NDS32 target are always
3857 big-endian, i.e.:
3858
3859 fd0 : fs0 fs1
3860 (MSB) (LSB)
3861
3862 We must return parallel rtx to represent such layout. */
3863 dwarf_high = gen_rtx_REG (word_mode, regno);
3864 dwarf_low = gen_rtx_REG (word_mode, regno + 1);
3865 return gen_rtx_PARALLEL (VOIDmode,
3866 gen_rtvec (2, dwarf_low, dwarf_high));
3867 }
3868 else if (mode == DCmode)
3869 {
3870 rtx dwarf_high_re = gen_rtx_REG (word_mode, regno);
3871 rtx dwarf_low_re = gen_rtx_REG (word_mode, regno + 1);
3872 rtx dwarf_high_im = gen_rtx_REG (word_mode, regno);
3873 rtx dwarf_low_im = gen_rtx_REG (word_mode, regno + 1);
3874 return gen_rtx_PARALLEL (VOIDmode,
3875 gen_rtvec (4, dwarf_low_re, dwarf_high_re,
3876 dwarf_high_im, dwarf_low_im));
3877 }
4b23af6d 3878 else if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
e2286268 3879 {
4b23af6d 3880 return NULL_RTX;
e2286268
MC
3881 }
3882 else
3883 {
3884 /* We should not be here. */
3885 gcc_unreachable ();
3886 }
3887 }
3888
3889 return NULL_RTX;
3890}
3891
3892/* Map internal gcc register numbers to DWARF2 register numbers. */
3893
3894unsigned int
ca60bd93 3895nds32_debugger_regno (unsigned int regno)
e2286268
MC
3896{
3897 /* The nds32 port in GDB maintains a mapping between dwarf register
3898 number and displayed register name. For backward compatibility to
3899 previous toolchain, currently our gdb still has four registers
3900 (d0.l, d0.h, d1.l, and d1.h) between GPR and FPR while compiler
3901 does not count those four registers in its register number table.
3902 So we have to add 4 on its register number and then create new
3903 dwarf information. Hopefully we can discard such workaround
3904 in the future. */
3905 if (NDS32_IS_FPR_REGNUM (regno))
3906 return regno + 4;
3907
3908 return regno;
3909}
3910
9304f876
CJW
3911\f
3912/* Defining target-specific uses of __attribute__. */
3913
3914/* Add some checking after merging attributes. */
3915static tree
3916nds32_merge_decl_attributes (tree olddecl, tree newdecl)
3917{
3918 tree combined_attrs;
3919
3920 /* Create combined attributes. */
3921 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
3922 DECL_ATTRIBUTES (newdecl));
3923
59043e75 3924 /* Since newdecl is acutally a duplicate of olddecl,
9304f876
CJW
3925 we can take olddecl for some operations. */
3926 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3927 {
3928 /* Check isr-specific attributes conflict. */
3929 nds32_check_isr_attrs_conflict (olddecl, combined_attrs);
3930 }
3931
3932 return combined_attrs;
3933}
3934
3935/* Add some checking when inserting attributes. */
3936static void
3937nds32_insert_attributes (tree decl, tree *attributes)
3938{
85a98076
KLC
3939 /* A "indirect_call" function attribute implies "noinline" and "noclone"
3940 for elf toolchain to support ROM patch mechanism. */
3941 if (TREE_CODE (decl) == FUNCTION_DECL
3942 && lookup_attribute ("indirect_call", *attributes) != NULL)
3943 {
3944 tree new_attrs = *attributes;
3945
cf3cd43d 3946 if (TARGET_LINUX_ABI)
67f84a80 3947 error ("cannot use %<indirect_call%> attribute under linux toolchain");
cf3cd43d 3948
85a98076
KLC
3949 if (lookup_attribute ("noinline", new_attrs) == NULL)
3950 new_attrs = tree_cons (get_identifier ("noinline"), NULL, new_attrs);
3951 if (lookup_attribute ("noclone", new_attrs) == NULL)
3952 new_attrs = tree_cons (get_identifier ("noclone"), NULL, new_attrs);
3953
3954 if (!TREE_PUBLIC (decl))
67f84a80 3955 error ("%<indirect_call%> attribute cannot apply for static function");
85a98076
KLC
3956
3957 *attributes = new_attrs;
3958 }
3959
9304f876
CJW
3960 /* For function declaration, we need to check isr-specific attributes:
3961 1. Call nds32_check_isr_attrs_conflict() to check any conflict.
3962 2. Check valid integer value for interrupt/exception.
3963 3. Check valid integer value for reset.
3964 4. Check valid function for nmi/warm. */
3965 if (TREE_CODE (decl) == FUNCTION_DECL)
3966 {
3967 tree func_attrs;
3968 tree intr, excp, reset;
3969
3970 /* Pick up function attributes. */
3971 func_attrs = *attributes;
3972
3973 /* 1. Call nds32_check_isr_attrs_conflict() to check any conflict. */
3974 nds32_check_isr_attrs_conflict (decl, func_attrs);
3975
3976 /* Now we are starting to check valid id value
8a498f99
CJW
3977 for interrupt/exception/reset.
3978 Note that we ONLY check its validity here.
3979 To construct isr vector information, it is still performed
3980 by nds32_construct_isr_vectors_information(). */
9304f876
CJW
3981 intr = lookup_attribute ("interrupt", func_attrs);
3982 excp = lookup_attribute ("exception", func_attrs);
3983 reset = lookup_attribute ("reset", func_attrs);
3984
a4931745
CJW
3985 /* The following code may use attribute arguments. If there is no
3986 argument from source code, it will cause segmentation fault.
3987 Therefore, return dircetly and report error message later. */
3988 if ((intr && TREE_VALUE (intr) == NULL)
3989 || (excp && TREE_VALUE (excp) == NULL)
3990 || (reset && TREE_VALUE (reset) == NULL))
3991 return;
3992
3993 /* ------------------------------------------------------------- */
3994 /* FIXME:
3995 FOR BACKWARD COMPATIBILITY, we need to support following patterns:
3996
3997 __attribute__((interrupt("XXX;YYY;id=ZZZ")))
3998 __attribute__((exception("XXX;YYY;id=ZZZ")))
3999 __attribute__((reset("vectors=XXX;nmi_func=YYY;warm_func=ZZZ")))
4000
4001 If interrupt/exception/reset appears and its argument is a
4002 STRING_CST, we will use other functions to parse string in the
4003 nds32_construct_isr_vectors_information() and then set necessary
4004 isr information in the nds32_isr_vectors[] array. Here we can
4005 just return immediately to avoid new-syntax checking. */
4006 if (intr != NULL_TREE
4007 && TREE_CODE (TREE_VALUE (TREE_VALUE (intr))) == STRING_CST)
4008 return;
4009 if (excp != NULL_TREE
4010 && TREE_CODE (TREE_VALUE (TREE_VALUE (excp))) == STRING_CST)
4011 return;
4012 if (reset != NULL_TREE
4013 && TREE_CODE (TREE_VALUE (TREE_VALUE (reset))) == STRING_CST)
4014 return;
4015 /* ------------------------------------------------------------- */
4016
9304f876
CJW
4017 if (intr || excp)
4018 {
4019 /* Deal with interrupt/exception. */
4020 tree id_list;
4021 unsigned int lower_bound, upper_bound;
4022
4023 /* The way to handle interrupt or exception is the same,
4024 we just need to take care of actual vector number.
4025 For interrupt(0..63), the actual vector number is (9..72).
4026 For exception(1..8), the actual vector number is (1..8). */
4027 lower_bound = (intr) ? (0) : (1);
4028 upper_bound = (intr) ? (63) : (8);
4029
4030 /* Prepare id list so that we can traverse id value. */
4031 id_list = (intr) ? (TREE_VALUE (intr)) : (TREE_VALUE (excp));
4032
4033 /* 2. Check valid integer value for interrupt/exception. */
4034 while (id_list)
4035 {
4036 tree id;
4037
4038 /* Pick up each vector id value. */
4039 id = TREE_VALUE (id_list);
4040 /* Issue error if it is not a valid integer value. */
4041 if (TREE_CODE (id) != INTEGER_CST
8e6cdc90
RS
4042 || wi::ltu_p (wi::to_wide (id), lower_bound)
4043 || wi::gtu_p (wi::to_wide (id), upper_bound))
9304f876
CJW
4044 error ("invalid id value for interrupt/exception attribute");
4045
4046 /* Advance to next id. */
4047 id_list = TREE_CHAIN (id_list);
4048 }
4049 }
4050 else if (reset)
4051 {
4052 /* Deal with reset. */
4053 tree id_list;
4054 tree id;
4055 tree nmi, warm;
4056 unsigned int lower_bound;
4057 unsigned int upper_bound;
4058
4059 /* Prepare id_list and identify id value so that
4060 we can check if total number of vectors is valid. */
4061 id_list = TREE_VALUE (reset);
4062 id = TREE_VALUE (id_list);
4063
4064 /* The maximum numbers for user's interrupt is 64. */
4065 lower_bound = 0;
4066 upper_bound = 64;
4067
4068 /* 3. Check valid integer value for reset. */
4069 if (TREE_CODE (id) != INTEGER_CST
8e6cdc90
RS
4070 || wi::ltu_p (wi::to_wide (id), lower_bound)
4071 || wi::gtu_p (wi::to_wide (id), upper_bound))
9304f876
CJW
4072 error ("invalid id value for reset attribute");
4073
4074 /* 4. Check valid function for nmi/warm. */
4075 nmi = lookup_attribute ("nmi", func_attrs);
4076 warm = lookup_attribute ("warm", func_attrs);
4077
4078 if (nmi != NULL_TREE)
4079 {
4080 tree nmi_func_list;
4081 tree nmi_func;
4082
4083 nmi_func_list = TREE_VALUE (nmi);
4084 nmi_func = TREE_VALUE (nmi_func_list);
4085
4086 /* Issue error if it is not a valid nmi function. */
4087 if (TREE_CODE (nmi_func) != IDENTIFIER_NODE)
4088 error ("invalid nmi function for reset attribute");
4089 }
4090
4091 if (warm != NULL_TREE)
4092 {
4093 tree warm_func_list;
4094 tree warm_func;
4095
4096 warm_func_list = TREE_VALUE (warm);
4097 warm_func = TREE_VALUE (warm_func_list);
4098
4099 /* Issue error if it is not a valid warm function. */
4100 if (TREE_CODE (warm_func) != IDENTIFIER_NODE)
4101 error ("invalid warm function for reset attribute");
4102 }
4103 }
4104 else
4105 {
4106 /* No interrupt, exception, or reset attribute is set. */
4107 return;
4108 }
4109 }
4110}
4111
4112static bool
4113nds32_option_pragma_parse (tree args ATTRIBUTE_UNUSED,
4114 tree pop_target ATTRIBUTE_UNUSED)
4115{
4116 /* Currently, we do not parse any pragma target by ourself,
4117 so just simply return false. */
4118 return false;
4119}
4120
4121static void
4122nds32_option_override (void)
4123{
4124 /* After all the command options have been parsed,
4125 we shall deal with some flags for changing compiler settings. */
4126
4127 /* At first, we check if we have to strictly
4128 set some flags based on ISA family. */
4129 if (TARGET_ISA_V2)
4130 {
4131 /* Under V2 ISA, we need to strictly disable TARGET_V3PUSH. */
4132 target_flags &= ~MASK_V3PUSH;
4133 }
4134 if (TARGET_ISA_V3)
4135 {
37d8f611
CJW
4136 /* If this is ARCH_V3J, we need to enable TARGET_REDUCED_REGS. */
4137 if (nds32_arch_option == ARCH_V3J)
4138 target_flags |= MASK_REDUCED_REGS;
9304f876
CJW
4139 }
4140 if (TARGET_ISA_V3M)
4141 {
4142 /* Under V3M ISA, we need to strictly enable TARGET_REDUCED_REGS. */
4143 target_flags |= MASK_REDUCED_REGS;
aa4b851c
CJW
4144 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF. */
4145 target_flags &= ~MASK_EXT_PERF;
4146 /* Under V3M ISA, we need to strictly disable TARGET_EXT_PERF2. */
4147 target_flags &= ~MASK_EXT_PERF2;
4148 /* Under V3M ISA, we need to strictly disable TARGET_EXT_STRING. */
4149 target_flags &= ~MASK_EXT_STRING;
b26fa4f9
KLC
4150
4151 if (flag_pic)
a3f9f006 4152 error ("not support %<-fpic%> option for v3m toolchain");
9304f876
CJW
4153 }
4154
4155 /* See if we are using reduced-set registers:
4156 $r0~$r5, $r6~$r10, $r15, $r28, $r29, $r30, $r31
4157 If so, we must forbid using $r11~$r14, $r16~$r27. */
4158 if (TARGET_REDUCED_REGS)
4159 {
4160 int r;
4161
4162 /* Prevent register allocator from
8a498f99 4163 choosing it as doing register allocation. */
9304f876
CJW
4164 for (r = 11; r <= 14; r++)
4165 fixed_regs[r] = call_used_regs[r] = 1;
4166 for (r = 16; r <= 27; r++)
4167 fixed_regs[r] = call_used_regs[r] = 1;
4168 }
4169
2140297c
CJW
4170 /* See if user explicitly would like to use fp-as-gp optimization.
4171 If so, we must prevent $fp from being allocated
4172 during register allocation. */
4173 if (TARGET_FORCE_FP_AS_GP)
4174 fixed_regs[FP_REGNUM] = call_used_regs[FP_REGNUM] = 1;
4175
9304f876
CJW
4176 if (!TARGET_16_BIT)
4177 {
4178 /* Under no 16 bit ISA, we need to strictly disable TARGET_V3PUSH. */
4179 target_flags &= ~MASK_V3PUSH;
4180 }
4181
e2286268
MC
4182 if (TARGET_HARD_FLOAT && !(TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE))
4183 {
4184 if (nds32_arch_option == ARCH_V3S || nds32_arch_option == ARCH_V3F)
4185 error ("Disable FPU ISA, "
904f3daa 4186 "the ABI option must be enable %<-mfloat-abi=soft%>");
e2286268 4187 else
904f3daa
ML
4188 error ("%<-mabi=2fp+%> option only support when FPU available, "
4189 "must be enable %<-mext-fpu-sp%> or %<-mext-fpu-dp%>");
e2286268
MC
4190 }
4191
19c93809 4192 nds32_init_rtx_costs ();
c4d8d050
CJW
4193
4194 nds32_register_passes ();
9304f876
CJW
4195}
4196
4197\f
4198/* Miscellaneous Parameters. */
4199
823835a9
CJW
4200static rtx_insn *
4201nds32_md_asm_adjust (vec<rtx> &outputs ATTRIBUTE_UNUSED,
4202 vec<rtx> &inputs ATTRIBUTE_UNUSED,
e52ef6e6 4203 vec<machine_mode> &input_modes ATTRIBUTE_UNUSED,
823835a9 4204 vec<const char *> &constraints ATTRIBUTE_UNUSED,
8d76ff99
TS
4205 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs,
4206 location_t /*loc*/)
823835a9 4207{
b28c01ab
CJW
4208 if (!flag_inline_asm_r15)
4209 {
4210 clobbers.safe_push (gen_rtx_REG (SImode, TA_REGNUM));
4211 SET_HARD_REG_BIT (clobbered_regs, TA_REGNUM);
4212 }
823835a9
CJW
4213 return NULL;
4214}
4215
9304f876
CJW
4216static void
4217nds32_init_builtins (void)
4218{
aaa44d2d 4219 nds32_init_builtins_impl ();
9304f876
CJW
4220}
4221
7a12ea32
MC
4222static tree
4223nds32_builtin_decl (unsigned code, bool initialize_p)
4224{
e53b6e56 4225 /* Implement in nds32-intrinsic.cc. */
7a12ea32
MC
4226 return nds32_builtin_decl_impl (code, initialize_p);
4227}
4228
9304f876
CJW
4229static rtx
4230nds32_expand_builtin (tree exp,
4231 rtx target,
aaa44d2d 4232 rtx subtarget,
ef4bddc2 4233 machine_mode mode,
aaa44d2d 4234 int ignore)
9304f876 4235{
aaa44d2d 4236 return nds32_expand_builtin_impl (exp, target, subtarget, mode, ignore);
9304f876
CJW
4237}
4238
cf3cd43d
CJW
4239/* Implement TARGET_INIT_LIBFUNCS. */
4240static void
4241nds32_init_libfuncs (void)
4242{
4243 if (TARGET_LINUX_ABI)
4244 init_sync_libfuncs (UNITS_PER_WORD);
4245}
9304f876
CJW
4246
4247/* ------------------------------------------------------------------------ */
4248
4249/* PART 4: Implemet extern function definitions,
4250 the prototype is in nds32-protos.h. */
72b7e5e1
KC
4251\f
4252/* Run-time Target Specification. */
4253
4254void
4255nds32_cpu_cpp_builtins(struct cpp_reader *pfile)
4256{
4257#define builtin_define(TXT) cpp_define (pfile, TXT)
4258#define builtin_assert(TXT) cpp_assert (pfile, TXT)
4259 builtin_define ("__nds32__");
4260 builtin_define ("__NDS32__");
4261
a4931745
CJW
4262 /* We need to provide builtin macro to describe the size of
4263 each vector for interrupt handler under elf toolchain. */
4264 if (!TARGET_LINUX_ABI)
4265 {
4266 if (TARGET_ISR_VECTOR_SIZE_4_BYTE)
4267 builtin_define ("__NDS32_ISR_VECTOR_SIZE_4__");
4268 else
4269 builtin_define ("__NDS32_ISR_VECTOR_SIZE_16__");
4270 }
4271
e2286268
MC
4272 if (TARGET_HARD_FLOAT)
4273 builtin_define ("__NDS32_ABI_2FP_PLUS__");
4274 else
4275 builtin_define ("__NDS32_ABI_2__");
4276
72b7e5e1
KC
4277 if (TARGET_ISA_V2)
4278 builtin_define ("__NDS32_ISA_V2__");
4279 if (TARGET_ISA_V3)
4280 builtin_define ("__NDS32_ISA_V3__");
4281 if (TARGET_ISA_V3M)
4282 builtin_define ("__NDS32_ISA_V3M__");
4283
e2286268
MC
4284 if (TARGET_FPU_SINGLE)
4285 builtin_define ("__NDS32_EXT_FPU_SP__");
4286 if (TARGET_FPU_DOUBLE)
4287 builtin_define ("__NDS32_EXT_FPU_DP__");
4288
4289 if (TARGET_EXT_FPU_FMA)
4290 builtin_define ("__NDS32_EXT_FPU_FMA__");
4291 if (NDS32_EXT_FPU_DOT_E)
4292 builtin_define ("__NDS32_EXT_FPU_DOT_E__");
4293 if (TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
4294 {
4295 switch (nds32_fp_regnum)
4296 {
4297 case 0:
4298 case 4:
4299 builtin_define ("__NDS32_EXT_FPU_CONFIG_0__");
4300 break;
4301 case 1:
4302 case 5:
4303 builtin_define ("__NDS32_EXT_FPU_CONFIG_1__");
4304 break;
4305 case 2:
4306 case 6:
4307 builtin_define ("__NDS32_EXT_FPU_CONFIG_2__");
4308 break;
4309 case 3:
4310 case 7:
4311 builtin_define ("__NDS32_EXT_FPU_CONFIG_3__");
4312 break;
4313 default:
4314 abort ();
4315 }
4316 }
4317
72b7e5e1
KC
4318 if (TARGET_BIG_ENDIAN)
4319 builtin_define ("__NDS32_EB__");
4320 else
4321 builtin_define ("__NDS32_EL__");
4322
4323 if (TARGET_REDUCED_REGS)
4324 builtin_define ("__NDS32_REDUCED_REGS__");
4325 if (TARGET_CMOV)
4326 builtin_define ("__NDS32_CMOV__");
4327 if (TARGET_EXT_PERF)
4328 builtin_define ("__NDS32_EXT_PERF__");
4329 if (TARGET_EXT_PERF2)
4330 builtin_define ("__NDS32_EXT_PERF2__");
4331 if (TARGET_EXT_STRING)
4332 builtin_define ("__NDS32_EXT_STRING__");
4333 if (TARGET_16_BIT)
4334 builtin_define ("__NDS32_16_BIT__");
4335 if (TARGET_GP_DIRECT)
4336 builtin_define ("__NDS32_GP_DIRECT__");
ff77f6e8
KC
4337 if (TARGET_VH)
4338 builtin_define ("__NDS32_VH__");
7c32ef41
MC
4339 if (NDS32_EXT_DSP_P ())
4340 builtin_define ("__NDS32_EXT_DSP__");
72b7e5e1
KC
4341
4342 if (TARGET_BIG_ENDIAN)
4343 builtin_define ("__big_endian__");
4344
4345 builtin_assert ("cpu=nds32");
4346 builtin_assert ("machine=nds32");
e2286268
MC
4347
4348 if (TARGET_HARD_FLOAT)
4349 builtin_define ("__NDS32_ABI_2FP_PLUS");
4350 else
4351 builtin_define ("__NDS32_ABI_2");
4352
72b7e5e1
KC
4353#undef builtin_define
4354#undef builtin_assert
4355}
4356
9304f876
CJW
4357\f
4358/* Defining Data Structures for Per-function Information. */
4359
4360void
4361nds32_init_expanders (void)
4362{
4363 /* Arrange to initialize and mark the machine per-function status. */
4364 init_machine_status = nds32_init_machine_status;
4365}
4366
4367\f
4368/* Register Usage. */
4369
5e6ae0cc
CJW
4370/* -- Order of Allocation of Registers. */
4371
4372void
4373nds32_adjust_reg_alloc_order (void)
4374{
4375 const int nds32_reg_alloc_order[] = REG_ALLOC_ORDER;
4376
4377 /* Copy the default register allocation order, which is designed
4378 to optimize for code size. */
4379 memcpy(reg_alloc_order, nds32_reg_alloc_order, sizeof (reg_alloc_order));
4380
4381 /* Adjust few register allocation order when optimizing for speed. */
4382 if (!optimize_size)
4383 {
4384 memcpy (reg_alloc_order, nds32_reg_alloc_order_for_speed,
4385 sizeof (nds32_reg_alloc_order_for_speed));
4386 }
4387}
4388
9304f876
CJW
4389/* -- How Values Fit in Registers. */
4390
e2286268
MC
4391static unsigned
4392nds32_hard_regno_nregs (unsigned regno ATTRIBUTE_UNUSED,
4393 machine_mode mode)
4394{
4395 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
4396}
4397
f939c3e6
RS
4398/* Implement TARGET_HARD_REGNO_MODE_OK. */
4399
4400static bool
4401nds32_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
9304f876 4402{
65e87462 4403 if (regno >= FIRST_PSEUDO_REGISTER)
f939c3e6 4404 return true;
9304f876 4405
e2286268
MC
4406 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE) && NDS32_IS_FPR_REGNUM (regno))
4407 {
4408 if (NDS32_IS_EXT_FPR_REGNUM(regno))
4409 return (NDS32_FPR_REGNO_OK_FOR_DOUBLE(regno) && (mode == DFmode));
4410 else if (mode == SFmode || mode == SImode)
4411 return NDS32_FPR_REGNO_OK_FOR_SINGLE (regno);
4412 else if (mode == DFmode)
4413 return NDS32_FPR_REGNO_OK_FOR_DOUBLE (regno);
4414
4415 return false;
4416 }
4417
4418 /* Restrict double-word quantities to even register pairs. */
4419 if (regno <= NDS32_LAST_GPR_REGNUM)
4420 return (targetm.hard_regno_nregs (regno, mode) == 1
4421 || !((regno) & 1));
4422
f939c3e6 4423 return false;
9304f876
CJW
4424}
4425
99e1629f
RS
4426/* Implement TARGET_MODES_TIEABLE_P. We can use general registers to
4427 tie QI/HI/SI modes together. */
4428
4429static bool
4430nds32_modes_tieable_p (machine_mode mode1, machine_mode mode2)
4431{
e2286268
MC
4432 if ((GET_MODE_CLASS (mode1) == MODE_INT
4433 && GET_MODE_CLASS (mode2) == MODE_INT)
4434 && GET_MODE_SIZE (mode1) <= UNITS_PER_WORD
4435 && GET_MODE_SIZE (mode2) <= UNITS_PER_WORD)
4436 return true;
4437
4438 if (GET_MODE_SIZE (mode1) == GET_MODE_SIZE (mode2))
4439 {
4440 if ((TARGET_FPU_SINGLE && !TARGET_FPU_DOUBLE)
4441 && (mode1 == DFmode || mode2 == DFmode))
4442 return false;
4443 else
4444 return true;
4445 }
4446
4447 return false;
99e1629f 4448}
9304f876
CJW
4449\f
4450/* Register Classes. */
4451
4452enum reg_class
4453nds32_regno_reg_class (int regno)
4454{
4455 /* Refer to nds32.h for more register class details. */
4456
4457 if (regno >= 0 && regno <= 7)
4458 return LOW_REGS;
4459 else if (regno >= 8 && regno <= 11)
4460 return MIDDLE_REGS;
4461 else if (regno >= 12 && regno <= 14)
4462 return HIGH_REGS;
4463 else if (regno == 15)
4464 return R15_TA_REG;
4465 else if (regno >= 16 && regno <= 19)
4466 return MIDDLE_REGS;
4467 else if (regno >= 20 && regno <= 31)
4468 return HIGH_REGS;
4469 else if (regno == 32 || regno == 33)
e2286268
MC
4470 {
4471 /* $SFP and $AP is FRAME_REGS in fact, However prevent IRA don't
4472 know how to allocate register for $SFP and $AP, just tell IRA they
4473 are GENERAL_REGS, and ARM do this hack too. */
4474 return GENERAL_REGS;
4475 }
4476 else if (regno >= 34 && regno <= 97)
4477 return FP_REGS;
9304f876
CJW
4478 else
4479 return NO_REGS;
4480}
4481
4482\f
4483/* Stack Layout and Calling Conventions. */
4484
4485/* -- Basic Stack Layout. */
4486
ca3a4a55
CJW
4487rtx
4488nds32_dynamic_chain_address (rtx frameaddr)
4489{
4490 if (TARGET_V3PUSH)
4491 {
4492 /* If -mv3push is specified, we push $fp, $gp, and $lp into stack.
4493 We can access dynamic chain address from stack by [$fp - 12]. */
4494 return plus_constant (Pmode, frameaddr, -12);
4495 }
4496 else
4497 {
4498 /* For general case we push $fp and $lp into stack at prologue.
4499 We can access dynamic chain address from stack by [$fp - 8]. */
4500 return plus_constant (Pmode, frameaddr, -8);
4501 }
4502}
4503
9304f876
CJW
4504rtx
4505nds32_return_addr_rtx (int count,
ca3a4a55 4506 rtx frameaddr)
9304f876 4507{
ca3a4a55
CJW
4508 int offset;
4509 rtx addr;
4510
9304f876 4511 if (count != 0)
ca3a4a55
CJW
4512 {
4513 /* In nds32 ABI design, we can expect that $lp is always available
4514 from stack by [$fp - 4] location. */
4515 offset = -4;
4516 addr = plus_constant (Pmode, frameaddr, offset);
4517 addr = memory_address (Pmode, addr);
4518
4519 return gen_rtx_MEM (Pmode, addr);
4520 }
9304f876
CJW
4521
4522 /* If count == 0, it means we are at current frame,
4523 the return address is $r30 ($lp). */
4524 return get_hard_reg_initial_val (Pmode, LP_REGNUM);
4525}
4526
4527/* -- Eliminating Frame Pointer and Arg Pointer. */
4528
4529HOST_WIDE_INT
4530nds32_initial_elimination_offset (unsigned int from_reg, unsigned int to_reg)
4531{
4532 HOST_WIDE_INT offset;
4533
4534 /* Compute and setup stack frame size.
4535 The result will be in cfun->machine. */
4536 nds32_compute_stack_frame ();
4537
4538 /* Remember to consider
ca3a4a55
CJW
4539 cfun->machine->callee_saved_area_gpr_padding_bytes and
4540 cfun->machine->eh_return_data_regs_size
9304f876
CJW
4541 when calculating offset. */
4542 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
4543 {
4544 offset = (cfun->machine->fp_size
8a498f99 4545 + cfun->machine->gp_size
9304f876 4546 + cfun->machine->lp_size
c457f751
CJW
4547 + cfun->machine->callee_saved_gpr_regs_size
4548 + cfun->machine->callee_saved_area_gpr_padding_bytes
e2286268 4549 + cfun->machine->callee_saved_fpr_regs_size
ca3a4a55 4550 + cfun->machine->eh_return_data_regs_size
9304f876
CJW
4551 + cfun->machine->local_size
4552 + cfun->machine->out_args_size);
4553 }
4554 else if (from_reg == ARG_POINTER_REGNUM
4555 && to_reg == HARD_FRAME_POINTER_REGNUM)
4556 {
4557 offset = 0;
4558 }
4559 else if (from_reg == FRAME_POINTER_REGNUM
4560 && to_reg == STACK_POINTER_REGNUM)
4561 {
4562 offset = (cfun->machine->local_size + cfun->machine->out_args_size);
4563 }
4564 else if (from_reg == FRAME_POINTER_REGNUM
4565 && to_reg == HARD_FRAME_POINTER_REGNUM)
4566 {
4567 offset = (-1) * (cfun->machine->fp_size
4568 + cfun->machine->gp_size
4569 + cfun->machine->lp_size
c457f751 4570 + cfun->machine->callee_saved_gpr_regs_size
e2286268 4571 + cfun->machine->callee_saved_area_gpr_padding_bytes
ca3a4a55
CJW
4572 + cfun->machine->callee_saved_fpr_regs_size
4573 + cfun->machine->eh_return_data_regs_size);
9304f876
CJW
4574 }
4575 else
4576 {
4577 gcc_unreachable ();
4578 }
4579
4580 return offset;
4581}
4582
4583/* -- Passing Arguments in Registers. */
4584
4585void
4586nds32_init_cumulative_args (CUMULATIVE_ARGS *cum,
4587 tree fntype ATTRIBUTE_UNUSED,
4588 rtx libname ATTRIBUTE_UNUSED,
4589 tree fndecl ATTRIBUTE_UNUSED,
4590 int n_named_args ATTRIBUTE_UNUSED)
4591{
e2286268
MC
4592 /* Initial available registers. The values are offset against
4593 NDS32_GPR_ARG_FIRST_REGNUM and NDS32_FPR_ARG_FIRST_REGNUM
9304f876 4594 for passing arguments. */
9d93cc24 4595 cum->gpr_offset = 0;
e2286268 4596 cum->fpr_offset = 0;
9304f876
CJW
4597}
4598
4599/* -- Function Entry and Exit. */
4600
4601/* Function for normal multiple push prologue. */
4602void
4603nds32_expand_prologue (void)
4604{
4605 int fp_adjust;
4606 int sp_adjust;
a6c7e777 4607 unsigned Rb, Re;
9304f876 4608
9304f876
CJW
4609 /* Compute and setup stack frame size.
4610 The result will be in cfun->machine. */
4611 nds32_compute_stack_frame ();
4612
2140297c
CJW
4613 /* Check frame_pointer_needed again to prevent fp is need after reload. */
4614 if (frame_pointer_needed)
4615 cfun->machine->fp_as_gp_p = false;
4616
557430f1
CJW
4617 /* If this is a variadic function, first we need to push argument
4618 registers that hold the unnamed argument value. */
4619 if (cfun->machine->va_args_size != 0)
4620 {
a6c7e777
MC
4621 Rb = cfun->machine->va_args_first_regno;
4622 Re = cfun->machine->va_args_last_regno;
4623 /* No need to push $fp, $gp, or $lp. */
4624 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, true);
557430f1
CJW
4625
4626 /* We may also need to adjust stack pointer for padding bytes
4627 because varargs may cause $sp not 8-byte aligned. */
4628 if (cfun->machine->va_args_area_padding_bytes)
4629 {
4630 /* Generate sp adjustment instruction. */
4631 sp_adjust = cfun->machine->va_args_area_padding_bytes;
557430f1 4632
a6c7e777
MC
4633 nds32_emit_adjust_frame (stack_pointer_rtx,
4634 stack_pointer_rtx,
4635 -1 * sp_adjust);
557430f1
CJW
4636 }
4637 }
4638
9304f876
CJW
4639 /* If the function is 'naked',
4640 we do not have to generate prologue code fragment. */
b26fa4f9 4641 if (cfun->machine->naked_p && !flag_pic)
9304f876
CJW
4642 return;
4643
4644 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
4645 Rb = cfun->machine->callee_saved_first_gpr_regno;
4646 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
4647
4648 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4649 to be saved, we don't have to create multiple push instruction.
4650 Otherwise, a multiple push instruction is needed. */
a6c7e777
MC
4651 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4652 && cfun->machine->fp_size == 0
4653 && cfun->machine->gp_size == 0
4654 && cfun->machine->lp_size == 0))
9304f876
CJW
4655 {
4656 /* Create multiple push instruction rtx. */
a6c7e777
MC
4657 nds32_emit_stack_push_multiple (
4658 Rb, Re,
4659 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size,
4660 false);
9304f876
CJW
4661 }
4662
ca3a4a55
CJW
4663 /* Save eh data registers. */
4664 if (cfun->machine->use_eh_return_p)
4665 {
4666 Rb = cfun->machine->eh_return_data_first_regno;
4667 Re = cfun->machine->eh_return_data_last_regno;
4668
4669 /* No need to push $fp, $gp, or $lp.
4670 Also, this is not variadic arguments push. */
4671 nds32_emit_stack_push_multiple (Rb, Re, false, false, false, false);
4672 }
4673
9304f876
CJW
4674 /* Check frame_pointer_needed to see
4675 if we shall emit fp adjustment instruction. */
4676 if (frame_pointer_needed)
4677 {
4678 /* adjust $fp = $sp + ($fp size) + ($gp size) + ($lp size)
8a498f99 4679 + (4 * callee-saved-registers)
ca3a4a55 4680 + (4 * exception-handling-data-registers)
8a498f99
CJW
4681 Note: No need to adjust
4682 cfun->machine->callee_saved_area_gpr_padding_bytes,
4683 because, at this point, stack pointer is just
4684 at the position after push instruction. */
9304f876
CJW
4685 fp_adjust = cfun->machine->fp_size
4686 + cfun->machine->gp_size
4687 + cfun->machine->lp_size
ca3a4a55
CJW
4688 + cfun->machine->callee_saved_gpr_regs_size
4689 + cfun->machine->eh_return_data_regs_size;
7064dcad 4690
a6c7e777
MC
4691 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
4692 stack_pointer_rtx,
4693 fp_adjust);
9304f876
CJW
4694 }
4695
e2286268
MC
4696 /* Save fpu registers. */
4697 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4698 {
4699 /* When $sp moved to bottom of stack, we need to check whether
4700 the range of offset in the FPU instruction. */
4701 int fpr_offset = cfun->machine->local_size
4702 + cfun->machine->out_args_size
4703 + cfun->machine->callee_saved_fpr_regs_size;
4704
4705 /* Check FPU instruction offset imm14s. */
4706 if (!satisfies_constraint_Is14 (GEN_INT (fpr_offset)))
4707 {
4708 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
4709 + cfun->machine->callee_saved_fpr_regs_size;
4710
4711 /* Save fpu registers, need to allocate stack space
4712 for fpu callee registers. And now $sp position
4713 on callee saved fpr registers. */
4714 nds32_emit_adjust_frame (stack_pointer_rtx,
4715 stack_pointer_rtx,
4716 -1 * fpr_space);
4717
4718 /* Emit fpu store instruction, using [$sp + offset] store
4719 fpu registers. */
4720 nds32_emit_push_fpr_callee_saved (0);
4721
4722 /* Adjust $sp = $sp - local_size - out_args_size. */
4723 sp_adjust = cfun->machine->local_size
4724 + cfun->machine->out_args_size;
4725
4726 /* Allocate stack space for local size and out args size. */
4727 nds32_emit_adjust_frame (stack_pointer_rtx,
4728 stack_pointer_rtx,
4729 -1 * sp_adjust);
4730 }
4731 else
4732 {
4733 /* Offset range in Is14, so $sp moved to bottom of stack. */
4734
4735 /* Adjust $sp = $sp - local_size - out_args_size
4736 - callee_saved_area_gpr_padding_bytes
4737 - callee_saved_fpr_regs_size. */
4738 sp_adjust = cfun->machine->local_size
4739 + cfun->machine->out_args_size
4740 + cfun->machine->callee_saved_area_gpr_padding_bytes
4741 + cfun->machine->callee_saved_fpr_regs_size;
4742
4743 nds32_emit_adjust_frame (stack_pointer_rtx,
4744 stack_pointer_rtx,
4745 -1 * sp_adjust);
4746
4747 /* Emit fpu store instruction, using [$sp + offset] store
4748 fpu registers. */
4749 int fpr_position = cfun->machine->out_args_size
4750 + cfun->machine->local_size;
4751 nds32_emit_push_fpr_callee_saved (fpr_position);
4752 }
4753 }
4754 else
4755 {
4756 /* Adjust $sp = $sp - local_size - out_args_size
4757 - callee_saved_area_gpr_padding_bytes. */
4758 sp_adjust = cfun->machine->local_size
4759 + cfun->machine->out_args_size
4760 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4761
4762 /* sp_adjust value may be out of range of the addi instruction,
4763 create alternative add behavior with TA_REGNUM if necessary,
4764 using NEGATIVE value to tell that we are decreasing address. */
4765 nds32_emit_adjust_frame (stack_pointer_rtx,
4766 stack_pointer_rtx,
4767 -1 * sp_adjust);
4768 }
9304f876 4769
b26fa4f9
KLC
4770 /* Emit gp setup instructions for -fpic. */
4771 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
4772 nds32_emit_load_gp ();
4773
d057a470
CJW
4774 /* If user applies -mno-sched-prolog-epilog option,
4775 we need to prevent instructions of function body from being
4776 scheduled with stack adjustment in prologue. */
4777 if (!flag_sched_prolog_epilog)
4778 emit_insn (gen_blockage ());
9304f876
CJW
4779}
4780
4781/* Function for normal multiple pop epilogue. */
4782void
d6529176 4783nds32_expand_epilogue (bool sibcall_p)
9304f876
CJW
4784{
4785 int sp_adjust;
a6c7e777 4786 unsigned Rb, Re;
9304f876
CJW
4787
4788 /* Compute and setup stack frame size.
4789 The result will be in cfun->machine. */
4790 nds32_compute_stack_frame ();
4791
d057a470
CJW
4792 /* If user applies -mno-sched-prolog-epilog option,
4793 we need to prevent instructions of function body from being
4794 scheduled with stack adjustment in epilogue. */
4795 if (!flag_sched_prolog_epilog)
4796 emit_insn (gen_blockage ());
9304f876
CJW
4797
4798 /* If the function is 'naked', we do not have to generate
557430f1
CJW
4799 epilogue code fragment BUT 'ret' instruction.
4800 However, if this function is also a variadic function,
4801 we need to create adjust stack pointer before 'ret' instruction. */
9304f876
CJW
4802 if (cfun->machine->naked_p)
4803 {
557430f1 4804 /* If this is a variadic function, we do not have to restore argument
8a498f99
CJW
4805 registers but need to adjust stack pointer back to previous stack
4806 frame location before return. */
557430f1
CJW
4807 if (cfun->machine->va_args_size != 0)
4808 {
4809 /* Generate sp adjustment instruction.
4810 We need to consider padding bytes here. */
4811 sp_adjust = cfun->machine->va_args_size
4812 + cfun->machine->va_args_area_padding_bytes;
a6c7e777
MC
4813
4814 nds32_emit_adjust_frame (stack_pointer_rtx,
4815 stack_pointer_rtx,
4816 sp_adjust);
557430f1
CJW
4817 }
4818
03390cda 4819 /* Generate return instruction by using 'return_internal' pattern.
8a498f99 4820 Make sure this instruction is after gen_blockage(). */
d6529176 4821 if (!sibcall_p)
54c537e6
CJW
4822 {
4823 /* We need to further check attributes to determine whether
4824 there should be return instruction at epilogue.
4825 If the attribute naked exists but -mno-ret-in-naked-func
4826 is issued, there is NO need to generate return instruction. */
4827 if (cfun->machine->attr_naked_p && !flag_ret_in_naked_func)
4828 return;
4829
4830 emit_jump_insn (gen_return_internal ());
4831 }
9304f876
CJW
4832 return;
4833 }
4834
4835 if (frame_pointer_needed)
4836 {
e2286268
MC
4837 /* Restore fpu registers. */
4838 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4839 {
4840 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
4841
4842 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4843 - (4 * callee-saved-registers)
4844 - (4 * exception-handling-data-registers)
4845 - (4 * callee-saved-gpr-registers padding byte)
4846 - (4 * callee-saved-fpr-registers)
4847 Note: we want to adjust stack pointer
4848 to the position for callee-saved fpr register,
4849 And restore fpu register use .bi instruction to adjust $sp
4850 from callee-saved fpr register to pop instruction. */
4851 sp_adjust = cfun->machine->fp_size
4852 + cfun->machine->gp_size
4853 + cfun->machine->lp_size
4854 + cfun->machine->callee_saved_gpr_regs_size
ca3a4a55 4855 + cfun->machine->eh_return_data_regs_size
e2286268
MC
4856 + cfun->machine->callee_saved_area_gpr_padding_bytes
4857 + cfun->machine->callee_saved_fpr_regs_size;
557430f1 4858
e2286268
MC
4859 nds32_emit_adjust_frame (stack_pointer_rtx,
4860 hard_frame_pointer_rtx,
4861 -1 * sp_adjust);
4862
4863 /* Emit fpu load instruction, using .bi instruction
4864 load fpu registers. */
4865 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4866 }
4867 else
4868 {
4869 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
4870 - (4 * callee-saved-registers)
4871 - (4 * exception-handling-data-registers)
4872 Note: No need to adjust
4873 cfun->machine->callee_saved_area_gpr_padding_bytes,
4874 because we want to adjust stack pointer
4875 to the position for pop instruction. */
4876 sp_adjust = cfun->machine->fp_size
4877 + cfun->machine->gp_size
4878 + cfun->machine->lp_size
ca3a4a55
CJW
4879 + cfun->machine->callee_saved_gpr_regs_size
4880 + cfun->machine->eh_return_data_regs_size;
e2286268
MC
4881
4882 nds32_emit_adjust_frame (stack_pointer_rtx,
4883 hard_frame_pointer_rtx,
4884 -1 * sp_adjust);
4885 }
9304f876
CJW
4886 }
4887 else
4888 {
e2286268
MC
4889 /* Restore fpu registers. */
4890 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
4891 {
4892 int gpr_padding = cfun->machine->callee_saved_area_gpr_padding_bytes;
9304f876 4893
e2286268
MC
4894 /* Adjust $sp = $sp + local_size + out_args_size. */
4895 sp_adjust = cfun->machine->local_size
4896 + cfun->machine->out_args_size;
a6c7e777 4897
e2286268
MC
4898 nds32_emit_adjust_frame (stack_pointer_rtx,
4899 stack_pointer_rtx,
4900 sp_adjust);
4901
4902 /* Emit fpu load instruction, using .bi instruction
4903 load fpu registers, and adjust $sp from callee-saved fpr register
4904 to callee-saved gpr register. */
4905 nds32_emit_pop_fpr_callee_saved (gpr_padding);
4906 }
4907 else
4908 {
4909 /* If frame pointer is NOT needed,
4910 we cannot calculate the sp adjustment from frame pointer.
4911 Instead, we calculate the adjustment by local_size,
4912 out_args_size, and callee_saved_area_gpr_padding_bytes.
4913 Notice that such sp adjustment value may be out of range,
4914 so we have to deal with it as well. */
4915
4916 /* Adjust $sp = $sp + local_size + out_args_size
4917 + callee_saved_area_gpr_padding_bytes. */
4918 sp_adjust = cfun->machine->local_size
4919 + cfun->machine->out_args_size
4920 + cfun->machine->callee_saved_area_gpr_padding_bytes;
4921
4922 nds32_emit_adjust_frame (stack_pointer_rtx,
4923 stack_pointer_rtx,
4924 sp_adjust);
4925 }
9304f876
CJW
4926 }
4927
ca3a4a55
CJW
4928 /* Restore eh data registers. */
4929 if (cfun->machine->use_eh_return_p)
4930 {
4931 Rb = cfun->machine->eh_return_data_first_regno;
4932 Re = cfun->machine->eh_return_data_last_regno;
4933
4934 /* No need to pop $fp, $gp, or $lp. */
4935 nds32_emit_stack_pop_multiple (Rb, Re, false, false, false);
4936 }
4937
9304f876 4938 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
4939 Rb = cfun->machine->callee_saved_first_gpr_regno;
4940 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
4941
4942 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
4943 to be saved, we don't have to create multiple pop instruction.
4944 Otherwise, a multiple pop instruction is needed. */
a6c7e777
MC
4945 if (!(Rb == SP_REGNUM && Re == SP_REGNUM
4946 && cfun->machine->fp_size == 0
4947 && cfun->machine->gp_size == 0
4948 && cfun->machine->lp_size == 0))
9304f876
CJW
4949 {
4950 /* Create multiple pop instruction rtx. */
a6c7e777
MC
4951 nds32_emit_stack_pop_multiple (
4952 Rb, Re,
4953 cfun->machine->fp_size, cfun->machine->gp_size, cfun->machine->lp_size);
9304f876
CJW
4954 }
4955
557430f1
CJW
4956 /* If this is a variadic function, we do not have to restore argument
4957 registers but need to adjust stack pointer back to previous stack
4958 frame location before return. */
4959 if (cfun->machine->va_args_size != 0)
4960 {
4961 /* Generate sp adjustment instruction.
8a498f99 4962 We need to consider padding bytes here. */
557430f1
CJW
4963 sp_adjust = cfun->machine->va_args_size
4964 + cfun->machine->va_args_area_padding_bytes;
557430f1 4965
a6c7e777
MC
4966 nds32_emit_adjust_frame (stack_pointer_rtx,
4967 stack_pointer_rtx,
4968 sp_adjust);
557430f1
CJW
4969 }
4970
ca3a4a55
CJW
4971 /* If this function uses __builtin_eh_return, make stack adjustment
4972 for exception handler. */
4973 if (cfun->machine->use_eh_return_p)
4974 {
4975 /* We need to unwind the stack by the offset computed by
4976 EH_RETURN_STACKADJ_RTX. However, at this point the CFA is
4977 based on SP. Ideally we would update the SP and define the
4978 CFA along the lines of:
4979
4980 SP = SP + EH_RETURN_STACKADJ_RTX
4981 (regnote CFA = SP - EH_RETURN_STACKADJ_RTX)
4982
4983 However the dwarf emitter only understands a constant
4984 register offset.
4985
4986 The solution chosen here is to use the otherwise $ta ($r15)
4987 as a temporary register to hold the current SP value. The
4988 CFA is described using $ta then SP is modified. */
4989
4990 rtx ta_reg;
4991 rtx insn;
4992
4993 ta_reg = gen_rtx_REG (SImode, TA_REGNUM);
4994
4995 insn = emit_move_insn (ta_reg, stack_pointer_rtx);
4996 add_reg_note (insn, REG_CFA_DEF_CFA, ta_reg);
4997 RTX_FRAME_RELATED_P (insn) = 1;
4998
4999 emit_insn (gen_addsi3 (stack_pointer_rtx,
5000 stack_pointer_rtx,
5001 EH_RETURN_STACKADJ_RTX));
5002
5003 /* Ensure the assignment to $ta does not get optimized away. */
5004 emit_use (ta_reg);
5005 }
5006
03390cda 5007 /* Generate return instruction. */
d6529176
CJW
5008 if (!sibcall_p)
5009 emit_jump_insn (gen_return_internal ());
9304f876
CJW
5010}
5011
5012/* Function for v3push prologue. */
5013void
5014nds32_expand_prologue_v3push (void)
5015{
5016 int fp_adjust;
5017 int sp_adjust;
e2286268 5018 int fpr_space = 0;
a6c7e777 5019 unsigned Rb, Re;
9304f876 5020
9304f876
CJW
5021 /* Compute and setup stack frame size.
5022 The result will be in cfun->machine. */
5023 nds32_compute_stack_frame ();
5024
a6c7e777
MC
5025 if (cfun->machine->callee_saved_gpr_regs_size > 0)
5026 df_set_regs_ever_live (FP_REGNUM, 1);
5027
2140297c
CJW
5028 /* Check frame_pointer_needed again to prevent fp is need after reload. */
5029 if (frame_pointer_needed)
5030 cfun->machine->fp_as_gp_p = false;
5031
9304f876
CJW
5032 /* If the function is 'naked',
5033 we do not have to generate prologue code fragment. */
b26fa4f9 5034 if (cfun->machine->naked_p && !flag_pic)
9304f876
CJW
5035 return;
5036
5037 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
5038 Rb = cfun->machine->callee_saved_first_gpr_regno;
5039 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
5040
5041 /* Calculate sp_adjust first to test if 'push25 Re,imm8u' is available,
5042 where imm8u has to be 8-byte alignment. */
5043 sp_adjust = cfun->machine->local_size
5044 + cfun->machine->out_args_size
e2286268
MC
5045 + cfun->machine->callee_saved_area_gpr_padding_bytes
5046 + cfun->machine->callee_saved_fpr_regs_size;
9304f876
CJW
5047
5048 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
5049 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust))
5050 {
5051 /* We can use 'push25 Re,imm8u'. */
5052
88437f39 5053 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
a6c7e777
MC
5054 the pattern 'stack_v3push' is implemented in nds32.md. */
5055 nds32_emit_stack_v3push (Rb, Re, sp_adjust);
e2286268
MC
5056
5057 /* Save fpu registers. */
5058 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5059 {
5060 /* Calculate fpr position. */
5061 int fpr_position = cfun->machine->local_size
5062 + cfun->machine->out_args_size;
5063 /* Emit fpu store instruction, using [$sp + offset] store
5064 fpu registers. */
5065 nds32_emit_push_fpr_callee_saved (fpr_position);
5066 }
5067
9304f876 5068 /* Check frame_pointer_needed to see
8a498f99 5069 if we shall emit fp adjustment instruction. */
9304f876
CJW
5070 if (frame_pointer_needed)
5071 {
5072 /* adjust $fp = $sp + 4 ($fp size)
8a498f99
CJW
5073 + 4 ($gp size)
5074 + 4 ($lp size)
5075 + (4 * n) (callee-saved registers)
5076 + sp_adjust ('push25 Re,imm8u')
9304f876 5077 Note: Since we use 'push25 Re,imm8u',
8a498f99
CJW
5078 the position of stack pointer is further
5079 changed after push instruction.
5080 Hence, we need to take sp_adjust value
5081 into consideration. */
9304f876
CJW
5082 fp_adjust = cfun->machine->fp_size
5083 + cfun->machine->gp_size
5084 + cfun->machine->lp_size
c457f751 5085 + cfun->machine->callee_saved_gpr_regs_size
9304f876 5086 + sp_adjust;
a6c7e777
MC
5087
5088 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
5089 stack_pointer_rtx,
5090 fp_adjust);
9304f876
CJW
5091 }
5092 }
5093 else
5094 {
e2286268
MC
5095 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5096 {
5097 /* Calculate fpr space. */
5098 fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
5099 + cfun->machine->callee_saved_fpr_regs_size;
5100
5101 /* We have to use 'push25 Re, fpr_space', to pre-allocate
5102 callee saved fpr registers space. */
5103 nds32_emit_stack_v3push (Rb, Re, fpr_space);
5104 nds32_emit_push_fpr_callee_saved (0);
5105 }
5106 else
5107 {
5108 /* We have to use 'push25 Re,0' and
5109 expand one more instruction to adjust $sp later. */
9304f876 5110
e2286268
MC
5111 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
5112 the pattern 'stack_v3push' is implemented in nds32.md. */
5113 nds32_emit_stack_v3push (Rb, Re, 0);
5114 }
9304f876
CJW
5115
5116 /* Check frame_pointer_needed to see
8a498f99 5117 if we shall emit fp adjustment instruction. */
9304f876
CJW
5118 if (frame_pointer_needed)
5119 {
5120 /* adjust $fp = $sp + 4 ($fp size)
8a498f99
CJW
5121 + 4 ($gp size)
5122 + 4 ($lp size)
5123 + (4 * n) (callee-saved registers)
9304f876 5124 Note: Since we use 'push25 Re,0',
8a498f99
CJW
5125 the stack pointer is just at the position
5126 after push instruction.
5127 No need to take sp_adjust into consideration. */
9304f876
CJW
5128 fp_adjust = cfun->machine->fp_size
5129 + cfun->machine->gp_size
5130 + cfun->machine->lp_size
c457f751 5131 + cfun->machine->callee_saved_gpr_regs_size;
a6c7e777 5132
e2286268
MC
5133 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5134 {
5135 /* We use 'push25 Re, fpr_space', the $sp is
5136 on callee saved fpr position, so need to consider
5137 fpr space. */
5138 fp_adjust = fp_adjust + fpr_space;
5139 }
5140
a6c7e777
MC
5141 nds32_emit_adjust_frame (hard_frame_pointer_rtx,
5142 stack_pointer_rtx,
5143 fp_adjust);
9304f876
CJW
5144 }
5145
e2286268
MC
5146 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5147 {
5148 /* We use 'push25 Re, fpr_space',
5149 the $sp is on callee saved fpr position,
5150 no need to consider fpr space. */
5151 sp_adjust = sp_adjust - fpr_space;
5152 }
5153
9304f876 5154 /* Because we use 'push25 Re,0',
8a498f99 5155 we need to expand one more instruction to adjust $sp.
8a498f99 5156 using NEGATIVE value to tell that we are decreasing address. */
a6c7e777
MC
5157 nds32_emit_adjust_frame (stack_pointer_rtx,
5158 stack_pointer_rtx,
5159 -1 * sp_adjust);
9304f876
CJW
5160 }
5161
b26fa4f9
KLC
5162 /* Emit gp setup instructions for -fpic. */
5163 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
5164 nds32_emit_load_gp ();
5165
9304f876
CJW
5166 /* Prevent the instruction scheduler from
5167 moving instructions across the boundary. */
5168 emit_insn (gen_blockage ());
5169}
5170
5171/* Function for v3pop epilogue. */
5172void
d6529176 5173nds32_expand_epilogue_v3pop (bool sibcall_p)
9304f876
CJW
5174{
5175 int sp_adjust;
a6c7e777 5176 unsigned Rb, Re;
9304f876
CJW
5177
5178 /* Compute and setup stack frame size.
5179 The result will be in cfun->machine. */
5180 nds32_compute_stack_frame ();
5181
5182 /* Prevent the instruction scheduler from
5183 moving instructions across the boundary. */
5184 emit_insn (gen_blockage ());
5185
5186 /* If the function is 'naked', we do not have to generate
5187 epilogue code fragment BUT 'ret' instruction. */
5188 if (cfun->machine->naked_p)
5189 {
03390cda 5190 /* Generate return instruction by using 'return_internal' pattern.
54c537e6
CJW
5191 Make sure this instruction is after gen_blockage().
5192 First we need to check this is a function without sibling call. */
d6529176 5193 if (!sibcall_p)
54c537e6
CJW
5194 {
5195 /* We need to further check attributes to determine whether
5196 there should be return instruction at epilogue.
5197 If the attribute naked exists but -mno-ret-in-naked-func
5198 is issued, there is NO need to generate return instruction. */
5199 if (cfun->machine->attr_naked_p && !flag_ret_in_naked_func)
5200 return;
5201
5202 emit_jump_insn (gen_return_internal ());
5203 }
9304f876
CJW
5204 return;
5205 }
5206
5207 /* Get callee_first_regno and callee_last_regno. */
a6c7e777
MC
5208 Rb = cfun->machine->callee_saved_first_gpr_regno;
5209 Re = cfun->machine->callee_saved_last_gpr_regno;
9304f876
CJW
5210
5211 /* Calculate sp_adjust first to test if 'pop25 Re,imm8u' is available,
5212 where imm8u has to be 8-byte alignment. */
5213 sp_adjust = cfun->machine->local_size
5214 + cfun->machine->out_args_size
e2286268
MC
5215 + cfun->machine->callee_saved_area_gpr_padding_bytes
5216 + cfun->machine->callee_saved_fpr_regs_size;
9304f876
CJW
5217
5218 /* We have to consider alloca issue as well.
5219 If the function does call alloca(), the stack pointer is not fixed.
5220 In that case, we cannot use 'pop25 Re,imm8u' directly.
5221 We have to caculate stack pointer from frame pointer
5222 and then use 'pop25 Re,0'.
5223 Of course, the frame_pointer_needed should be nonzero
5224 if the function calls alloca(). */
5225 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
5226 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
5227 && !cfun->calls_alloca)
5228 {
e2286268
MC
5229 /* Restore fpu registers. */
5230 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5231 {
5232 int fpr_position = cfun->machine->local_size
5233 + cfun->machine->out_args_size;
5234 /* Emit fpu load instruction, using [$sp + offset] restore
5235 fpu registers. */
5236 nds32_emit_v3pop_fpr_callee_saved (fpr_position);
5237 }
5238
9304f876
CJW
5239 /* We can use 'pop25 Re,imm8u'. */
5240
88437f39 5241 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
a6c7e777
MC
5242 the pattern 'stack_v3pop' is implementad in nds32.md. */
5243 nds32_emit_stack_v3pop (Rb, Re, sp_adjust);
9304f876
CJW
5244 }
5245 else
5246 {
5247 /* We have to use 'pop25 Re,0', and prior to it,
8a498f99 5248 we must expand one more instruction to adjust $sp. */
9304f876
CJW
5249
5250 if (frame_pointer_needed)
5251 {
5252 /* adjust $sp = $fp - 4 ($fp size)
8a498f99
CJW
5253 - 4 ($gp size)
5254 - 4 ($lp size)
5255 - (4 * n) (callee-saved registers)
9304f876 5256 Note: No need to adjust
8a498f99
CJW
5257 cfun->machine->callee_saved_area_gpr_padding_bytes,
5258 because we want to adjust stack pointer
5259 to the position for pop instruction. */
9304f876
CJW
5260 sp_adjust = cfun->machine->fp_size
5261 + cfun->machine->gp_size
5262 + cfun->machine->lp_size
c457f751 5263 + cfun->machine->callee_saved_gpr_regs_size;
a6c7e777 5264
e2286268
MC
5265 /* Restore fpu registers. */
5266 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5267 {
5268 /* Set $sp to callee saved fpr position, we need to restore
5269 fpr registers. */
5270 sp_adjust = sp_adjust
5271 + cfun->machine->callee_saved_area_gpr_padding_bytes
5272 + cfun->machine->callee_saved_fpr_regs_size;
5273
5274 nds32_emit_adjust_frame (stack_pointer_rtx,
5275 hard_frame_pointer_rtx,
5276 -1 * sp_adjust);
5277
5278 /* Emit fpu load instruction, using [$sp + offset] restore
5279 fpu registers. */
5280 nds32_emit_v3pop_fpr_callee_saved (0);
5281 }
5282 else
5283 {
5284 nds32_emit_adjust_frame (stack_pointer_rtx,
5285 hard_frame_pointer_rtx,
5286 -1 * sp_adjust);
5287 }
9304f876
CJW
5288 }
5289 else
5290 {
5291 /* If frame pointer is NOT needed,
5292 we cannot calculate the sp adjustment from frame pointer.
5293 Instead, we calculate the adjustment by local_size,
5294 out_args_size, and callee_saved_area_padding_bytes.
5295 Notice that such sp adjustment value may be out of range,
5296 so we have to deal with it as well. */
5297
5298 /* Adjust $sp = $sp + local_size + out_args_size
e2286268
MC
5299 + callee_saved_area_gpr_padding_bytes
5300 + callee_saved_fpr_regs_size. */
9304f876
CJW
5301 sp_adjust = cfun->machine->local_size
5302 + cfun->machine->out_args_size
e2286268
MC
5303 + cfun->machine->callee_saved_area_gpr_padding_bytes
5304 + cfun->machine->callee_saved_fpr_regs_size;
5305
5306 /* Restore fpu registers. */
5307 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5308 {
5309 /* Set $sp to callee saved fpr position, we need to restore
5310 fpr registers. */
5311 sp_adjust = sp_adjust
5312 - cfun->machine->callee_saved_area_gpr_padding_bytes
5313 - cfun->machine->callee_saved_fpr_regs_size;
5314
5315 nds32_emit_adjust_frame (stack_pointer_rtx,
5316 stack_pointer_rtx,
5317 sp_adjust);
5318
5319 /* Emit fpu load instruction, using [$sp + offset] restore
5320 fpu registers. */
5321 nds32_emit_v3pop_fpr_callee_saved (0);
5322 }
5323 else
5324 {
5325 /* sp_adjust value may be out of range of the addi instruction,
5326 create alternative add behavior with TA_REGNUM if necessary,
5327 using POSITIVE value to tell that we are increasing
5328 address. */
5329 nds32_emit_adjust_frame (stack_pointer_rtx,
5330 stack_pointer_rtx,
5331 sp_adjust);
5332 }
9304f876
CJW
5333 }
5334
e2286268
MC
5335 if (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)
5336 {
5337 /* We have fpr need to restore, so $sp is set on callee saved fpr
5338 position. And we use 'pop25 Re, fpr_space' to adjust $sp. */
5339 int fpr_space = cfun->machine->callee_saved_area_gpr_padding_bytes
5340 + cfun->machine->callee_saved_fpr_regs_size;
5341 nds32_emit_stack_v3pop (Rb, Re, fpr_space);
5342 }
5343 else
5344 {
5345 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
5346 the pattern 'stack_v3pop' is implementad in nds32.md. */
5347 nds32_emit_stack_v3pop (Rb, Re, 0);
5348 }
9304f876 5349 }
aa2642ef
CJW
5350 /* Generate return instruction. */
5351 emit_jump_insn (gen_pop25return ());
9304f876
CJW
5352}
5353
03390cda
CJW
5354/* Return nonzero if this function is known to have a null epilogue.
5355 This allows the optimizer to omit jumps to jumps if no stack
5356 was created. */
5357int
5358nds32_can_use_return_insn (void)
5359{
e2286268
MC
5360 int sp_adjust;
5361
03390cda 5362 /* Prior to reloading, we can't tell how many registers must be saved.
67914693 5363 Thus we cannot determine whether this function has null epilogue. */
03390cda
CJW
5364 if (!reload_completed)
5365 return 0;
5366
54c537e6
CJW
5367 /* If attribute 'naked' appears but -mno-ret-in-naked-func is used,
5368 we cannot use return instruction. */
5369 if (cfun->machine->attr_naked_p && !flag_ret_in_naked_func)
5370 return 0;
5371
e2286268
MC
5372 sp_adjust = cfun->machine->local_size
5373 + cfun->machine->out_args_size
5374 + cfun->machine->callee_saved_area_gpr_padding_bytes
5375 + cfun->machine->callee_saved_fpr_regs_size;
5376 if (!cfun->machine->fp_as_gp_p
5377 && satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
5378 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
5379 && !cfun->calls_alloca
5380 && NDS32_V3PUSH_AVAILABLE_P
5381 && !(TARGET_HARD_FLOAT
5382 && (cfun->machine->callee_saved_first_fpr_regno != SP_REGNUM)))
5383 return 1;
5384
03390cda
CJW
5385 /* If no stack was created, two conditions must be satisfied:
5386 1. This is a naked function.
8a498f99 5387 So there is no callee-saved, local size, or outgoing size.
03390cda 5388 2. This is NOT a variadic function.
8a498f99 5389 So there is no pushing arguement registers into the stack. */
03390cda
CJW
5390 return (cfun->machine->naked_p && (cfun->machine->va_args_size == 0));
5391}
5392
2f1bb3ca
SC
5393scalar_int_mode
5394nds32_case_vector_shorten_mode (int min_offset, int max_offset,
5395 rtx body ATTRIBUTE_UNUSED)
5396{
5397 if (min_offset < 0 || max_offset >= 0x2000)
5398 return SImode;
5399 else
5400 {
5401 /* The jump table maybe need to 2 byte alignment,
5402 so reserved 1 byte for check max_offset. */
5403 if (max_offset >= 0xff)
5404 return HImode;
5405 else
5406 return QImode;
5407 }
5408}
5409
9304f876
CJW
5410/* ------------------------------------------------------------------------ */
5411
43fa41c1
CJW
5412/* Return alignment for the label. */
5413int
5414nds32_target_alignment (rtx_insn *label)
5415{
5416 rtx_insn *insn;
5417
5418 if (!NDS32_ALIGN_P ())
5419 return 0;
5420
5421 insn = next_active_insn (label);
5422
5423 /* Always align to 4 byte when first instruction after label is jump
5424 instruction since length for that might changed, so let's always align
5425 it for make sure we don't lose any perfomance here. */
5426 if (insn == 0
5427 || (get_attr_length (insn) == 2
5428 && !JUMP_P (insn) && !CALL_P (insn)))
5429 return 0;
5430 else
5431 return 2;
5432}
5433
79498ad8
CJW
5434/* Return alignment for data. */
5435unsigned int
5436nds32_data_alignment (tree data,
5437 unsigned int basic_align)
5438{
5439 if ((basic_align < BITS_PER_WORD)
5440 && (TREE_CODE (data) == ARRAY_TYPE
5441 || TREE_CODE (data) == UNION_TYPE
5442 || TREE_CODE (data) == RECORD_TYPE))
5443 return BITS_PER_WORD;
5444 else
5445 return basic_align;
5446}
5447
5448/* Return alignment for constant value. */
5449static HOST_WIDE_INT
5450nds32_constant_alignment (const_tree constant,
5451 HOST_WIDE_INT basic_align)
5452{
5453 /* Make string literal and constant for constructor to word align. */
5454 if (((TREE_CODE (constant) == STRING_CST
5455 || TREE_CODE (constant) == CONSTRUCTOR
5456 || TREE_CODE (constant) == UNION_TYPE
5457 || TREE_CODE (constant) == RECORD_TYPE
5458 || TREE_CODE (constant) == ARRAY_TYPE)
5459 && basic_align < BITS_PER_WORD))
5460 return BITS_PER_WORD;
5461 else
5462 return basic_align;
5463}
5464
5465/* Return alignment for local variable. */
5466unsigned int
5467nds32_local_alignment (tree local ATTRIBUTE_UNUSED,
5468 unsigned int basic_align)
5469{
5470 bool at_least_align_to_word = false;
5471 /* Make local array, struct and union at least align to word for make
5472 sure it can unroll memcpy when initialize by constant. */
5473 switch (TREE_CODE (local))
5474 {
5475 case ARRAY_TYPE:
5476 case RECORD_TYPE:
5477 case UNION_TYPE:
5478 at_least_align_to_word = true;
5479 break;
5480 default:
5481 at_least_align_to_word = false;
5482 break;
5483 }
5484 if (at_least_align_to_word
5485 && (basic_align < BITS_PER_WORD))
5486 return BITS_PER_WORD;
5487 else
5488 return basic_align;
5489}
5490
e2286268
MC
5491bool
5492nds32_split_double_word_load_store_p(rtx *operands, bool load_p)
5493{
5494 rtx mem = load_p ? operands[1] : operands[0];
5495 /* Do split at split2 if -O0 or schedule 2 not enable. */
5496 if (optimize == 0 || !flag_schedule_insns_after_reload)
5497 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
5498
5499 /* Split double word load store after copy propgation. */
5500 if (current_pass == NULL)
5501 return false;
5502
5503 const char *pass_name = current_pass->name;
f4777088 5504 if (pass_name && ((strcmp (pass_name, "split3") == 0)
e2286268
MC
5505 || (strcmp (pass_name, "split5") == 0)))
5506 return !satisfies_constraint_Da (mem) || MEM_VOLATILE_P (mem);
5507
5508 return false;
5509}
5510
5511static bool
5512nds32_use_blocks_for_constant_p (machine_mode mode,
5513 const_rtx x ATTRIBUTE_UNUSED)
5514{
5515 if ((TARGET_FPU_SINGLE || TARGET_FPU_DOUBLE)
5516 && (mode == DFmode || mode == SFmode))
5517 return true;
5518 else
5519 return false;
5520}
9304f876 5521
9304f876
CJW
5522/* ------------------------------------------------------------------------ */
5523
5524/* PART 5: Initialize target hook structure and definitions. */
5525\f
5526/* Controlling the Compilation Driver. */
5527
5528\f
5529/* Run-time Target Specification. */
5530
5531\f
5532/* Defining Data Structures for Per-function Information. */
5533
5534\f
5535/* Storage Layout. */
5536
5537#undef TARGET_PROMOTE_FUNCTION_MODE
5538#define TARGET_PROMOTE_FUNCTION_MODE \
5539 default_promote_function_mode_always_promote
5540
5f2a98c3
CJW
5541#undef TARGET_EXPAND_TO_RTL_HOOK
5542#define TARGET_EXPAND_TO_RTL_HOOK nds32_expand_to_rtl_hook
5543
79498ad8
CJW
5544#undef TARGET_CONSTANT_ALIGNMENT
5545#define TARGET_CONSTANT_ALIGNMENT nds32_constant_alignment
5546
9304f876
CJW
5547\f
5548/* Layout of Source Language Data Types. */
5549
5550\f
5551/* Register Usage. */
5552
5553/* -- Basic Characteristics of Registers. */
5554
e2286268
MC
5555#undef TARGET_CONDITIONAL_REGISTER_USAGE
5556#define TARGET_CONDITIONAL_REGISTER_USAGE nds32_conditional_register_usage
5557
9304f876
CJW
5558/* -- Order of Allocation of Registers. */
5559
5560/* -- How Values Fit in Registers. */
5561
e2286268
MC
5562#undef TARGET_HARD_REGNO_NREGS
5563#define TARGET_HARD_REGNO_NREGS nds32_hard_regno_nregs
5564
50256c75
CJW
5565#undef TARGET_HARD_REGNO_MODE_OK
5566#define TARGET_HARD_REGNO_MODE_OK nds32_hard_regno_mode_ok
5567
5568#undef TARGET_MODES_TIEABLE_P
5569#define TARGET_MODES_TIEABLE_P nds32_modes_tieable_p
5570
9304f876
CJW
5571/* -- Handling Leaf Functions. */
5572
5573/* -- Registers That Form a Stack. */
5574
5575\f
5576/* Register Classes. */
5577
5578#undef TARGET_CLASS_MAX_NREGS
5579#define TARGET_CLASS_MAX_NREGS nds32_class_max_nregs
5580
9304f876
CJW
5581#undef TARGET_REGISTER_PRIORITY
5582#define TARGET_REGISTER_PRIORITY nds32_register_priority
5583
e2286268
MC
5584#undef TARGET_CAN_CHANGE_MODE_CLASS
5585#define TARGET_CAN_CHANGE_MODE_CLASS nds32_can_change_mode_class
5586
9304f876
CJW
5587\f
5588/* Obsolete Macros for Defining Constraints. */
5589
5590\f
5591/* Stack Layout and Calling Conventions. */
5592
5593/* -- Basic Stack Layout. */
5594
5595/* -- Exception Handling Support. */
5596
5597/* -- Specifying How Stack Checking is Done. */
5598
5599/* -- Registers That Address the Stack Frame. */
5600
5601/* -- Eliminating Frame Pointer and Arg Pointer. */
5602
5603#undef TARGET_CAN_ELIMINATE
5604#define TARGET_CAN_ELIMINATE nds32_can_eliminate
5605
5606/* -- Passing Function Arguments on the Stack. */
5607
5608/* -- Passing Arguments in Registers. */
5609
5610#undef TARGET_FUNCTION_ARG
5611#define TARGET_FUNCTION_ARG nds32_function_arg
5612
d40f3c40
CJW
5613#undef TARGET_MUST_PASS_IN_STACK
5614#define TARGET_MUST_PASS_IN_STACK nds32_must_pass_in_stack
5615
650fc469
CJW
5616#undef TARGET_ARG_PARTIAL_BYTES
5617#define TARGET_ARG_PARTIAL_BYTES nds32_arg_partial_bytes
5618
9304f876
CJW
5619#undef TARGET_FUNCTION_ARG_ADVANCE
5620#define TARGET_FUNCTION_ARG_ADVANCE nds32_function_arg_advance
5621
5622#undef TARGET_FUNCTION_ARG_BOUNDARY
5623#define TARGET_FUNCTION_ARG_BOUNDARY nds32_function_arg_boundary
5624
7c32ef41
MC
5625#undef TARGET_VECTOR_MODE_SUPPORTED_P
5626#define TARGET_VECTOR_MODE_SUPPORTED_P nds32_vector_mode_supported_p
5627
9304f876
CJW
5628/* -- How Scalar Function Values Are Returned. */
5629
5630#undef TARGET_FUNCTION_VALUE
5631#define TARGET_FUNCTION_VALUE nds32_function_value
5632
5633#undef TARGET_LIBCALL_VALUE
5634#define TARGET_LIBCALL_VALUE nds32_libcall_value
5635
5636#undef TARGET_FUNCTION_VALUE_REGNO_P
5637#define TARGET_FUNCTION_VALUE_REGNO_P nds32_function_value_regno_p
5638
5639/* -- How Large Values Are Returned. */
5640
e2286268
MC
5641#undef TARGET_RETURN_IN_MEMORY
5642#define TARGET_RETURN_IN_MEMORY nds32_return_in_memory
5643
9304f876
CJW
5644/* -- Caller-Saves Register Allocation. */
5645
5646/* -- Function Entry and Exit. */
5647
5648#undef TARGET_ASM_FUNCTION_PROLOGUE
5649#define TARGET_ASM_FUNCTION_PROLOGUE nds32_asm_function_prologue
5650
5651#undef TARGET_ASM_FUNCTION_END_PROLOGUE
5652#define TARGET_ASM_FUNCTION_END_PROLOGUE nds32_asm_function_end_prologue
5653
5654#undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
5655#define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE nds32_asm_function_begin_epilogue
5656
5657#undef TARGET_ASM_FUNCTION_EPILOGUE
5658#define TARGET_ASM_FUNCTION_EPILOGUE nds32_asm_function_epilogue
5659
5660#undef TARGET_ASM_OUTPUT_MI_THUNK
5661#define TARGET_ASM_OUTPUT_MI_THUNK nds32_asm_output_mi_thunk
5662
5663#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
5664#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
5665
5666/* -- Generating Code for Profiling. */
5667
5668/* -- Permitting tail calls. */
5669
cc48a87f
CJW
5670#undef TARGET_FUNCTION_OK_FOR_SIBCALL
5671#define TARGET_FUNCTION_OK_FOR_SIBCALL nds32_function_ok_for_sibcall
5672
9304f876
CJW
5673#undef TARGET_WARN_FUNC_RETURN
5674#define TARGET_WARN_FUNC_RETURN nds32_warn_func_return
5675
5676/* Stack smashing protection. */
5677
5678\f
5679/* Implementing the Varargs Macros. */
5680
d4a6a4d9
CJW
5681#undef TARGET_SETUP_INCOMING_VARARGS
5682#define TARGET_SETUP_INCOMING_VARARGS nds32_setup_incoming_varargs
5683
9304f876
CJW
5684#undef TARGET_STRICT_ARGUMENT_NAMING
5685#define TARGET_STRICT_ARGUMENT_NAMING nds32_strict_argument_naming
5686
5687\f
5688/* Trampolines for Nested Functions. */
5689
5690#undef TARGET_ASM_TRAMPOLINE_TEMPLATE
5691#define TARGET_ASM_TRAMPOLINE_TEMPLATE nds32_asm_trampoline_template
5692
5693#undef TARGET_TRAMPOLINE_INIT
5694#define TARGET_TRAMPOLINE_INIT nds32_trampoline_init
5695
5696\f
5697/* Implicit Calls to Library Routines. */
5698
5699\f
5700/* Addressing Modes. */
5701
5702#undef TARGET_LEGITIMATE_ADDRESS_P
5703#define TARGET_LEGITIMATE_ADDRESS_P nds32_legitimate_address_p
5704
85a98076
KLC
5705#undef TARGET_LEGITIMIZE_ADDRESS
5706#define TARGET_LEGITIMIZE_ADDRESS nds32_legitimize_address
5707
5708#undef TARGET_LEGITIMATE_CONSTANT_P
5709#define TARGET_LEGITIMATE_CONSTANT_P nds32_legitimate_constant_p
5710
7c32ef41
MC
5711#undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
5712#define TARGET_VECTORIZE_PREFERRED_SIMD_MODE nds32_vectorize_preferred_simd_mode
5713
85a98076
KLC
5714#undef TARGET_CANNOT_FORCE_CONST_MEM
5715#define TARGET_CANNOT_FORCE_CONST_MEM nds32_cannot_force_const_mem
5716
5717#undef TARGET_DELEGITIMIZE_ADDRESS
5718#define TARGET_DELEGITIMIZE_ADDRESS nds32_delegitimize_address
5719
9304f876
CJW
5720\f
5721/* Anchored Addresses. */
5722
5723\f
5724/* Condition Code Status. */
5725
5726/* -- Representation of condition codes using (cc0). */
5727
5728/* -- Representation of condition codes using registers. */
5729
5b6f2bf3
CJW
5730#undef TARGET_CANONICALIZE_COMPARISON
5731#define TARGET_CANONICALIZE_COMPARISON nds32_canonicalize_comparison
5732
9304f876
CJW
5733/* -- Macros to control conditional execution. */
5734
5735\f
5736/* Describing Relative Costs of Operations. */
5737
5738#undef TARGET_REGISTER_MOVE_COST
5739#define TARGET_REGISTER_MOVE_COST nds32_register_move_cost
5740
5741#undef TARGET_MEMORY_MOVE_COST
5742#define TARGET_MEMORY_MOVE_COST nds32_memory_move_cost
5743
5744#undef TARGET_RTX_COSTS
5745#define TARGET_RTX_COSTS nds32_rtx_costs
5746
5747#undef TARGET_ADDRESS_COST
5748#define TARGET_ADDRESS_COST nds32_address_cost
5749
5750\f
5751/* Adjusting the Instruction Scheduler. */
5752
5753\f
5754/* Dividing the Output into Sections (Texts, Data, . . . ). */
5755
511a41d7
CJW
5756#undef TARGET_ENCODE_SECTION_INFO
5757#define TARGET_ENCODE_SECTION_INFO nds32_encode_section_info
5758
9304f876
CJW
5759\f
5760/* Position Independent Code. */
5761
5762\f
5763/* Defining the Output Assembler Language. */
5764
5765/* -- The Overall Framework of an Assembler File. */
5766
5767#undef TARGET_ASM_FILE_START
5768#define TARGET_ASM_FILE_START nds32_asm_file_start
5769#undef TARGET_ASM_FILE_END
5770#define TARGET_ASM_FILE_END nds32_asm_file_end
5771
5772/* -- Output of Data. */
5773
5774#undef TARGET_ASM_ALIGNED_HI_OP
5775#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
5776
5777#undef TARGET_ASM_ALIGNED_SI_OP
5778#define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
5779
85a98076
KLC
5780#undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
5781#define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA nds32_asm_output_addr_const_extra
5782
9304f876
CJW
5783/* -- Output of Uninitialized Variables. */
5784
5785/* -- Output and Generation of Labels. */
5786
5787#undef TARGET_ASM_GLOBALIZE_LABEL
5788#define TARGET_ASM_GLOBALIZE_LABEL nds32_asm_globalize_label
5789
5790/* -- How Initialization Functions Are Handled. */
5791
5792/* -- Macros Controlling Initialization Routines. */
5793
5794/* -- Output of Assembler Instructions. */
5795
5796#undef TARGET_PRINT_OPERAND
5797#define TARGET_PRINT_OPERAND nds32_print_operand
5798#undef TARGET_PRINT_OPERAND_ADDRESS
5799#define TARGET_PRINT_OPERAND_ADDRESS nds32_print_operand_address
5800
5801/* -- Output of Dispatch Tables. */
5802
5803/* -- Assembler Commands for Exception Regions. */
5804
e2286268
MC
5805#undef TARGET_DWARF_REGISTER_SPAN
5806#define TARGET_DWARF_REGISTER_SPAN nds32_dwarf_register_span
5807
9304f876
CJW
5808/* -- Assembler Commands for Alignment. */
5809
5810\f
5811/* Controlling Debugging Information Format. */
5812
5813/* -- Macros Affecting All Debugging Formats. */
5814
180295ed 5815/* -- Macros for DWARF Output. */
9304f876
CJW
5816
5817/* -- Macros for VMS Debug Format. */
5818
5819\f
5820/* Cross Compilation and Floating Point. */
5821
5822\f
5823/* Mode Switching Instructions. */
5824
5825\f
5826/* Defining target-specific uses of __attribute__. */
5827
5828#undef TARGET_ATTRIBUTE_TABLE
5829#define TARGET_ATTRIBUTE_TABLE nds32_attribute_table
5830
5831#undef TARGET_MERGE_DECL_ATTRIBUTES
5832#define TARGET_MERGE_DECL_ATTRIBUTES nds32_merge_decl_attributes
5833
5834#undef TARGET_INSERT_ATTRIBUTES
5835#define TARGET_INSERT_ATTRIBUTES nds32_insert_attributes
5836
5837#undef TARGET_OPTION_PRAGMA_PARSE
5838#define TARGET_OPTION_PRAGMA_PARSE nds32_option_pragma_parse
5839
5840#undef TARGET_OPTION_OVERRIDE
5841#define TARGET_OPTION_OVERRIDE nds32_option_override
5842
5843\f
5844/* Emulating TLS. */
5845
cf3cd43d
CJW
5846#undef TARGET_HAVE_TLS
5847#define TARGET_HAVE_TLS TARGET_LINUX_ABI
5848
9304f876
CJW
5849\f
5850/* Defining coprocessor specifics for MIPS targets. */
5851
5852\f
5853/* Parameters for Precompiled Header Validity Checking. */
5854
5855\f
5856/* C++ ABI parameters. */
5857
5858\f
5859/* Adding support for named address spaces. */
5860
5861\f
5862/* Miscellaneous Parameters. */
5863
823835a9
CJW
5864#undef TARGET_MD_ASM_ADJUST
5865#define TARGET_MD_ASM_ADJUST nds32_md_asm_adjust
5866
9304f876
CJW
5867#undef TARGET_INIT_BUILTINS
5868#define TARGET_INIT_BUILTINS nds32_init_builtins
5869
7a12ea32
MC
5870#undef TARGET_BUILTIN_DECL
5871#define TARGET_BUILTIN_DECL nds32_builtin_decl
5872
9304f876
CJW
5873#undef TARGET_EXPAND_BUILTIN
5874#define TARGET_EXPAND_BUILTIN nds32_expand_builtin
5875
cf3cd43d
CJW
5876#undef TARGET_INIT_LIBFUNCS
5877#define TARGET_INIT_LIBFUNCS nds32_init_libfuncs
e2286268
MC
5878
5879#undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
5880#define TARGET_USE_BLOCKS_FOR_CONSTANT_P nds32_use_blocks_for_constant_p
5881
091a8640
CJW
5882#undef TARGET_HAVE_SPECULATION_SAFE_VALUE
5883#define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
5884
9304f876
CJW
5885\f
5886/* ------------------------------------------------------------------------ */
5887
5888/* Initialize the GCC target structure. */
5889
5890struct gcc_target targetm = TARGET_INITIALIZER;
5891
5892/* ------------------------------------------------------------------------ */