]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/nds32/nds32.c
.
[thirdparty/gcc.git] / gcc / config / nds32 / nds32.c
CommitLineData
eae21ed1 1/* Subroutines used for code generation of Andes NDS32 cpu for GNU compiler
aad93da1 2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
eae21ed1 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
7a878515 21/* ------------------------------------------------------------------------ */
eae21ed1 22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
9ef16211 26#include "backend.h"
c1eb80de 27#include "target.h"
9ef16211 28#include "rtl.h"
c1eb80de 29#include "tree.h"
30a86690 30#include "stringpool.h"
31#include "attribs.h"
9ef16211 32#include "df.h"
ad7b10a2 33#include "memmodel.h"
c1eb80de 34#include "tm_p.h"
35#include "optabs.h" /* For GEN_FCN. */
36#include "regs.h"
37#include "emit-rtl.h"
38#include "recog.h"
39#include "diagnostic-core.h"
9ed99284 40#include "stor-layout.h"
41#include "varasm.h"
42#include "calls.h"
eae21ed1 43#include "output.h"
d53441c8 44#include "explow.h"
eae21ed1 45#include "expr.h"
eae21ed1 46#include "tm-constrs.h"
f7715905 47#include "builtins.h"
eae21ed1 48
0c71fb4f 49/* This file should be included last. */
4b498588 50#include "target-def.h"
51
eae21ed1 52/* ------------------------------------------------------------------------ */
53
54/* This file is divided into five parts:
55
56 PART 1: Auxiliary static variable definitions and
57 target hook static variable definitions.
58
59 PART 2: Auxiliary static function definitions.
60
61 PART 3: Implement target hook stuff definitions.
62
63 PART 4: Implemet extern function definitions,
64 the prototype is in nds32-protos.h.
65
66 PART 5: Initialize target hook structure and definitions. */
67
68/* ------------------------------------------------------------------------ */
69
70/* PART 1: Auxiliary static variable definitions and
71 target hook static variable definitions. */
72
eae21ed1 73/* Define intrinsic register names.
74 Please refer to nds32_intrinsic.h file, the index is corresponding to
75 'enum nds32_intrinsic_registers' data type values.
76 NOTE that the base value starting from 1024. */
77static const char * const nds32_intrinsic_register_names[] =
78{
79 "$PSW", "$IPSW", "$ITYPE", "$IPC"
80};
81
82/* Defining target-specific uses of __attribute__. */
83static const struct attribute_spec nds32_attribute_table[] =
84{
85 /* Syntax: { name, min_len, max_len, decl_required, type_required,
86 function_type_required, handler, affects_type_identity } */
87
88 /* The interrupt vid: [0-63]+ (actual vector number starts from 9 to 72). */
89 { "interrupt", 1, 64, false, false, false, NULL, false },
90 /* The exception vid: [1-8]+ (actual vector number starts from 1 to 8). */
91 { "exception", 1, 8, false, false, false, NULL, false },
92 /* Argument is user's interrupt numbers. The vector number is always 0. */
93 { "reset", 1, 1, false, false, false, NULL, false },
94
95 /* The attributes describing isr nested type. */
96 { "nested", 0, 0, false, false, false, NULL, false },
97 { "not_nested", 0, 0, false, false, false, NULL, false },
98 { "nested_ready", 0, 0, false, false, false, NULL, false },
99
100 /* The attributes describing isr register save scheme. */
101 { "save_all", 0, 0, false, false, false, NULL, false },
102 { "partial_save", 0, 0, false, false, false, NULL, false },
103
104 /* The attributes used by reset attribute. */
105 { "nmi", 1, 1, false, false, false, NULL, false },
106 { "warm", 1, 1, false, false, false, NULL, false },
107
108 /* The attribute telling no prologue/epilogue. */
109 { "naked", 0, 0, false, false, false, NULL, false },
110
111 /* The last attribute spec is set to be NULL. */
112 { NULL, 0, 0, false, false, false, NULL, false }
113};
114
115
116/* ------------------------------------------------------------------------ */
117
118/* PART 2: Auxiliary static function definitions. */
119
120/* Function to save and restore machine-specific function data. */
121static struct machine_function *
122nds32_init_machine_status (void)
123{
124 struct machine_function *machine;
25a27413 125 machine = ggc_cleared_alloc<machine_function> ();
eae21ed1 126
127 /* Initially assume this function needs prologue/epilogue. */
128 machine->naked_p = 0;
129
130 /* Initially assume this function does NOT use fp_as_gp optimization. */
131 machine->fp_as_gp_p = 0;
132
133 return machine;
134}
135
136/* Function to compute stack frame size and
137 store into cfun->machine structure. */
138static void
139nds32_compute_stack_frame (void)
140{
141 int r;
142 int block_size;
143
144 /* Because nds32_compute_stack_frame() will be called from different place,
145 everytime we enter this function, we have to assume this function
146 needs prologue/epilogue. */
147 cfun->machine->naked_p = 0;
148
149 /* Get variadic arguments size to prepare pretend arguments and
eee9efd9 150 we will push them into stack at prologue by ourself. */
151 cfun->machine->va_args_size = crtl->args.pretend_args_size;
152 if (cfun->machine->va_args_size != 0)
153 {
154 cfun->machine->va_args_first_regno
155 = NDS32_GPR_ARG_FIRST_REGNUM
156 + NDS32_MAX_GPR_REGS_FOR_ARGS
157 - (crtl->args.pretend_args_size / UNITS_PER_WORD);
158 cfun->machine->va_args_last_regno
159 = NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS - 1;
160 }
161 else
162 {
163 cfun->machine->va_args_first_regno = SP_REGNUM;
164 cfun->machine->va_args_last_regno = SP_REGNUM;
165 }
166
167 /* Important: We need to make sure that varargs area is 8-byte alignment. */
168 block_size = cfun->machine->va_args_size;
169 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
170 {
171 cfun->machine->va_args_area_padding_bytes
172 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
173 }
eae21ed1 174
175 /* Get local variables, incoming variables, and temporary variables size.
176 Note that we need to make sure it is 8-byte alignment because
177 there may be no padding bytes if we are using LRA. */
178 cfun->machine->local_size = NDS32_ROUND_UP_DOUBLE_WORD (get_frame_size ());
179
180 /* Get outgoing arguments size. */
181 cfun->machine->out_args_size = crtl->outgoing_args_size;
182
183 /* If $fp value is required to be saved on stack, it needs 4 bytes space.
184 Check whether $fp is ever live. */
185 cfun->machine->fp_size = (df_regs_ever_live_p (FP_REGNUM)) ? 4 : 0;
186
187 /* If $gp value is required to be saved on stack, it needs 4 bytes space.
188 Check whether we are using PIC code genration. */
189 cfun->machine->gp_size = (flag_pic) ? 4 : 0;
190
191 /* If $lp value is required to be saved on stack, it needs 4 bytes space.
192 Check whether $lp is ever live. */
193 cfun->machine->lp_size = (df_regs_ever_live_p (LP_REGNUM)) ? 4 : 0;
194
195 /* Initially there is no padding bytes. */
afddacc6 196 cfun->machine->callee_saved_area_gpr_padding_bytes = 0;
eae21ed1 197
198 /* Calculate the bytes of saving callee-saved registers on stack. */
afddacc6 199 cfun->machine->callee_saved_gpr_regs_size = 0;
200 cfun->machine->callee_saved_first_gpr_regno = SP_REGNUM;
201 cfun->machine->callee_saved_last_gpr_regno = SP_REGNUM;
eae21ed1 202 /* Currently, there is no need to check $r28~$r31
203 because we will save them in another way. */
204 for (r = 0; r < 28; r++)
205 {
206 if (NDS32_REQUIRED_CALLEE_SAVED_P (r))
207 {
208 /* Mark the first required callee-saved register
209 (only need to set it once).
210 If first regno == SP_REGNUM, we can tell that
211 it is the first time to be here. */
afddacc6 212 if (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM)
213 cfun->machine->callee_saved_first_gpr_regno = r;
eae21ed1 214 /* Mark the last required callee-saved register. */
afddacc6 215 cfun->machine->callee_saved_last_gpr_regno = r;
eae21ed1 216 }
217 }
218
219 /* Check if this function can omit prologue/epilogue code fragment.
220 If there is 'naked' attribute in this function,
221 we can set 'naked_p' flag to indicate that
222 we do not have to generate prologue/epilogue.
223 Or, if all the following conditions succeed,
224 we can set this function 'naked_p' as well:
225 condition 1: first_regno == last_regno == SP_REGNUM,
226 which means we do not have to save
227 any callee-saved registers.
228 condition 2: Both $lp and $fp are NOT live in this function,
ab707896 229 which means we do not need to save them and there
230 is no outgoing size.
eae21ed1 231 condition 3: There is no local_size, which means
232 we do not need to adjust $sp. */
233 if (lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl))
afddacc6 234 || (cfun->machine->callee_saved_first_gpr_regno == SP_REGNUM
235 && cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM
eae21ed1 236 && !df_regs_ever_live_p (FP_REGNUM)
237 && !df_regs_ever_live_p (LP_REGNUM)
238 && cfun->machine->local_size == 0))
239 {
ab707896 240 /* Set this function 'naked_p' and other functions can check this flag.
241 Note that in nds32 port, the 'naked_p = 1' JUST means there is no
242 callee-saved, local size, and outgoing size.
243 The varargs space and ret instruction may still present in
244 the prologue/epilogue expanding. */
eae21ed1 245 cfun->machine->naked_p = 1;
246
247 /* No need to save $fp, $gp, and $lp.
248 We should set these value to be zero
249 so that nds32_initial_elimination_offset() can work properly. */
250 cfun->machine->fp_size = 0;
251 cfun->machine->gp_size = 0;
252 cfun->machine->lp_size = 0;
253
254 /* If stack usage computation is required,
255 we need to provide the static stack size. */
256 if (flag_stack_usage_info)
257 current_function_static_stack_size = 0;
258
259 /* No need to do following adjustment, return immediately. */
260 return;
261 }
262
263 /* Adjustment for v3push instructions:
264 If we are using v3push (push25/pop25) instructions,
265 we need to make sure Rb is $r6 and Re is
266 located on $r6, $r8, $r10, or $r14.
267 Some results above will be discarded and recomputed.
ab707896 268 Note that it is only available under V3/V3M ISA and we
269 DO NOT setup following stuff for isr or variadic function. */
270 if (TARGET_V3PUSH
271 && !nds32_isr_function_p (current_function_decl)
272 && (cfun->machine->va_args_size == 0))
eae21ed1 273 {
274 /* Recompute:
275 cfun->machine->fp_size
276 cfun->machine->gp_size
277 cfun->machine->lp_size
278 cfun->machine->callee_saved_regs_first_regno
279 cfun->machine->callee_saved_regs_last_regno */
280
281 /* For v3push instructions, $fp, $gp, and $lp are always saved. */
282 cfun->machine->fp_size = 4;
283 cfun->machine->gp_size = 4;
284 cfun->machine->lp_size = 4;
285
286 /* Remember to set Rb = $r6. */
afddacc6 287 cfun->machine->callee_saved_first_gpr_regno = 6;
eae21ed1 288
afddacc6 289 if (cfun->machine->callee_saved_last_gpr_regno <= 6)
eae21ed1 290 {
291 /* Re = $r6 */
afddacc6 292 cfun->machine->callee_saved_last_gpr_regno = 6;
eae21ed1 293 }
afddacc6 294 else if (cfun->machine->callee_saved_last_gpr_regno <= 8)
eae21ed1 295 {
296 /* Re = $r8 */
afddacc6 297 cfun->machine->callee_saved_last_gpr_regno = 8;
eae21ed1 298 }
afddacc6 299 else if (cfun->machine->callee_saved_last_gpr_regno <= 10)
eae21ed1 300 {
301 /* Re = $r10 */
afddacc6 302 cfun->machine->callee_saved_last_gpr_regno = 10;
eae21ed1 303 }
afddacc6 304 else if (cfun->machine->callee_saved_last_gpr_regno <= 14)
eae21ed1 305 {
306 /* Re = $r14 */
afddacc6 307 cfun->machine->callee_saved_last_gpr_regno = 14;
eae21ed1 308 }
afddacc6 309 else if (cfun->machine->callee_saved_last_gpr_regno == SP_REGNUM)
eae21ed1 310 {
311 /* If last_regno is SP_REGNUM, which means
312 it is never changed, so set it to Re = $r6. */
afddacc6 313 cfun->machine->callee_saved_last_gpr_regno = 6;
eae21ed1 314 }
315 else
316 {
317 /* The program flow should not go here. */
318 gcc_unreachable ();
319 }
320 }
321
322 /* We have correctly set callee_saved_regs_first_regno
323 and callee_saved_regs_last_regno.
324 Initially, the callee_saved_regs_size is supposed to be 0.
325 As long as callee_saved_regs_last_regno is not SP_REGNUM,
326 we can update callee_saved_regs_size with new size. */
afddacc6 327 if (cfun->machine->callee_saved_last_gpr_regno != SP_REGNUM)
eae21ed1 328 {
329 /* Compute pushed size of callee-saved registers. */
afddacc6 330 cfun->machine->callee_saved_gpr_regs_size
331 = 4 * (cfun->machine->callee_saved_last_gpr_regno
332 - cfun->machine->callee_saved_first_gpr_regno
eae21ed1 333 + 1);
334 }
335
336 /* Important: We need to make sure that
eee9efd9 337 (fp_size + gp_size + lp_size + callee_saved_regs_size)
eae21ed1 338 is 8-byte alignment.
339 If it is not, calculate the padding bytes. */
eee9efd9 340 block_size = cfun->machine->fp_size
eae21ed1 341 + cfun->machine->gp_size
342 + cfun->machine->lp_size
afddacc6 343 + cfun->machine->callee_saved_gpr_regs_size;
eae21ed1 344 if (!NDS32_DOUBLE_WORD_ALIGN_P (block_size))
345 {
afddacc6 346 cfun->machine->callee_saved_area_gpr_padding_bytes
eae21ed1 347 = NDS32_ROUND_UP_DOUBLE_WORD (block_size) - block_size;
348 }
349
350 /* If stack usage computation is required,
351 we need to provide the static stack size. */
352 if (flag_stack_usage_info)
353 {
354 current_function_static_stack_size
355 = NDS32_ROUND_UP_DOUBLE_WORD (block_size)
356 + cfun->machine->local_size
357 + cfun->machine->out_args_size;
358 }
359}
360
361/* Function to create a parallel rtx pattern
362 which presents stack push multiple behavior.
363 The overall concept are:
364 "push registers to memory",
365 "adjust stack pointer". */
cd3fd5ae 366static void
aa51c130 367nds32_emit_stack_push_multiple (rtx Rb, rtx Re, rtx En4, bool vaarg_p)
eae21ed1 368{
369 int regno;
370 int extra_count;
371 int num_use_regs;
372 int par_index;
373 int offset;
cd3fd5ae 374 int save_fp, save_gp, save_lp;
eae21ed1 375
376 rtx reg;
377 rtx mem;
378 rtx push_rtx;
379 rtx adjust_sp_rtx;
380 rtx parallel_insn;
aa51c130 381 rtx dwarf;
eae21ed1 382
383 /* We need to provide a customized rtx which contains
384 necessary information for data analysis,
385 so we create a parallel rtx like this:
386 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
387 (reg:SI Rb))
388 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
389 (reg:SI Rb+1))
390 ...
391 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
392 (reg:SI Re))
393 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
394 (reg:SI FP_REGNUM))
395 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
396 (reg:SI GP_REGNUM))
397 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
398 (reg:SI LP_REGNUM))
399 (set (reg:SI SP_REGNUM)
400 (plus (reg:SI SP_REGNUM) (const_int -32)))]) */
401
cd3fd5ae 402 /* Determine whether we need to save $fp, $gp, or $lp. */
403 save_fp = INTVAL (En4) & 0x8;
404 save_gp = INTVAL (En4) & 0x4;
405 save_lp = INTVAL (En4) & 0x2;
406
eae21ed1 407 /* Calculate the number of registers that will be pushed. */
408 extra_count = 0;
cd3fd5ae 409 if (save_fp)
eae21ed1 410 extra_count++;
cd3fd5ae 411 if (save_gp)
eae21ed1 412 extra_count++;
cd3fd5ae 413 if (save_lp)
eae21ed1 414 extra_count++;
415 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
416 if (REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM)
417 num_use_regs = extra_count;
418 else
419 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + extra_count;
420
421 /* In addition to used registers,
422 we need one more space for (set sp sp-x) rtx. */
423 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
424 rtvec_alloc (num_use_regs + 1));
425 par_index = 0;
426
427 /* Initialize offset and start to create push behavior. */
428 offset = -(num_use_regs * 4);
429
430 /* Create (set mem regX) from Rb, Rb+1 up to Re. */
431 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
432 {
433 /* Rb and Re may be SP_REGNUM.
434 We need to break this loop immediately. */
435 if (regno == SP_REGNUM)
cd3fd5ae 436 break;
eae21ed1 437
438 reg = gen_rtx_REG (SImode, regno);
439 mem = gen_frame_mem (SImode, plus_constant (Pmode,
440 stack_pointer_rtx,
441 offset));
d1f9b275 442 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 443 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
444 RTX_FRAME_RELATED_P (push_rtx) = 1;
445 offset = offset + 4;
446 par_index++;
447 }
448
449 /* Create (set mem fp), (set mem gp), and (set mem lp) if necessary. */
cd3fd5ae 450 if (save_fp)
eae21ed1 451 {
452 reg = gen_rtx_REG (SImode, FP_REGNUM);
453 mem = gen_frame_mem (SImode, plus_constant (Pmode,
454 stack_pointer_rtx,
455 offset));
d1f9b275 456 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 457 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
458 RTX_FRAME_RELATED_P (push_rtx) = 1;
459 offset = offset + 4;
460 par_index++;
461 }
cd3fd5ae 462 if (save_gp)
eae21ed1 463 {
464 reg = gen_rtx_REG (SImode, GP_REGNUM);
465 mem = gen_frame_mem (SImode, plus_constant (Pmode,
466 stack_pointer_rtx,
467 offset));
d1f9b275 468 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 469 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
470 RTX_FRAME_RELATED_P (push_rtx) = 1;
471 offset = offset + 4;
472 par_index++;
473 }
cd3fd5ae 474 if (save_lp)
eae21ed1 475 {
476 reg = gen_rtx_REG (SImode, LP_REGNUM);
477 mem = gen_frame_mem (SImode, plus_constant (Pmode,
478 stack_pointer_rtx,
479 offset));
d1f9b275 480 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 481 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
482 RTX_FRAME_RELATED_P (push_rtx) = 1;
483 offset = offset + 4;
484 par_index++;
485 }
486
487 /* Create (set sp sp-x). */
488
489 /* We need to re-calculate the offset value again for adjustment. */
490 offset = -(num_use_regs * 4);
491 adjust_sp_rtx
d1f9b275 492 = gen_rtx_SET (stack_pointer_rtx,
eae21ed1 493 plus_constant (Pmode, stack_pointer_rtx, offset));
494 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
495 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
496
cd3fd5ae 497 parallel_insn = emit_insn (parallel_insn);
498
499 /* The insn rtx 'parallel_insn' will change frame layout.
500 We need to use RTX_FRAME_RELATED_P so that GCC is able to
501 generate CFI (Call Frame Information) stuff. */
502 RTX_FRAME_RELATED_P (parallel_insn) = 1;
aa51c130 503
504 /* Don't use GCC's logic for CFI info if we are generate a push for VAARG
505 since we will not restore those register at epilogue. */
506 if (vaarg_p)
507 {
508 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA,
509 copy_rtx (adjust_sp_rtx), NULL_RTX);
510 REG_NOTES (parallel_insn) = dwarf;
511 }
eae21ed1 512}
513
514/* Function to create a parallel rtx pattern
515 which presents stack pop multiple behavior.
516 The overall concept are:
517 "pop registers from memory",
518 "adjust stack pointer". */
cd3fd5ae 519static void
520nds32_emit_stack_pop_multiple (rtx Rb, rtx Re, rtx En4)
eae21ed1 521{
522 int regno;
523 int extra_count;
524 int num_use_regs;
525 int par_index;
526 int offset;
cd3fd5ae 527 int save_fp, save_gp, save_lp;
eae21ed1 528
529 rtx reg;
530 rtx mem;
531 rtx pop_rtx;
532 rtx adjust_sp_rtx;
533 rtx parallel_insn;
cd3fd5ae 534 rtx dwarf = NULL_RTX;
eae21ed1 535
536 /* We need to provide a customized rtx which contains
537 necessary information for data analysis,
538 so we create a parallel rtx like this:
539 (parallel [(set (reg:SI Rb)
540 (mem (reg:SI SP_REGNUM)))
541 (set (reg:SI Rb+1)
542 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
543 ...
544 (set (reg:SI Re)
545 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
546 (set (reg:SI FP_REGNUM)
547 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
548 (set (reg:SI GP_REGNUM)
549 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
550 (set (reg:SI LP_REGNUM)
551 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
552 (set (reg:SI SP_REGNUM)
553 (plus (reg:SI SP_REGNUM) (const_int 32)))]) */
554
cd3fd5ae 555 /* Determine whether we need to restore $fp, $gp, or $lp. */
556 save_fp = INTVAL (En4) & 0x8;
557 save_gp = INTVAL (En4) & 0x4;
558 save_lp = INTVAL (En4) & 0x2;
559
eae21ed1 560 /* Calculate the number of registers that will be poped. */
561 extra_count = 0;
cd3fd5ae 562 if (save_fp)
eae21ed1 563 extra_count++;
cd3fd5ae 564 if (save_gp)
eae21ed1 565 extra_count++;
cd3fd5ae 566 if (save_lp)
eae21ed1 567 extra_count++;
568 /* Note that Rb and Re may be SP_REGNUM. DO NOT count it in. */
569 if (REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM)
570 num_use_regs = extra_count;
571 else
572 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + extra_count;
573
574 /* In addition to used registers,
575 we need one more space for (set sp sp+x) rtx. */
576 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
577 rtvec_alloc (num_use_regs + 1));
578 par_index = 0;
579
580 /* Initialize offset and start to create pop behavior. */
581 offset = 0;
582
583 /* Create (set regX mem) from Rb, Rb+1 up to Re. */
584 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
585 {
586 /* Rb and Re may be SP_REGNUM.
587 We need to break this loop immediately. */
588 if (regno == SP_REGNUM)
cd3fd5ae 589 break;
eae21ed1 590
591 reg = gen_rtx_REG (SImode, regno);
592 mem = gen_frame_mem (SImode, plus_constant (Pmode,
593 stack_pointer_rtx,
594 offset));
d1f9b275 595 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 596 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
597 RTX_FRAME_RELATED_P (pop_rtx) = 1;
598 offset = offset + 4;
599 par_index++;
cd3fd5ae 600
601 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
eae21ed1 602 }
603
604 /* Create (set fp mem), (set gp mem), and (set lp mem) if necessary. */
cd3fd5ae 605 if (save_fp)
eae21ed1 606 {
607 reg = gen_rtx_REG (SImode, FP_REGNUM);
608 mem = gen_frame_mem (SImode, plus_constant (Pmode,
609 stack_pointer_rtx,
610 offset));
d1f9b275 611 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 612 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
613 RTX_FRAME_RELATED_P (pop_rtx) = 1;
614 offset = offset + 4;
615 par_index++;
cd3fd5ae 616
617 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
eae21ed1 618 }
cd3fd5ae 619 if (save_gp)
eae21ed1 620 {
621 reg = gen_rtx_REG (SImode, GP_REGNUM);
622 mem = gen_frame_mem (SImode, plus_constant (Pmode,
623 stack_pointer_rtx,
624 offset));
d1f9b275 625 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 626 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
627 RTX_FRAME_RELATED_P (pop_rtx) = 1;
628 offset = offset + 4;
629 par_index++;
cd3fd5ae 630
631 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
eae21ed1 632 }
cd3fd5ae 633 if (save_lp)
eae21ed1 634 {
635 reg = gen_rtx_REG (SImode, LP_REGNUM);
636 mem = gen_frame_mem (SImode, plus_constant (Pmode,
637 stack_pointer_rtx,
638 offset));
d1f9b275 639 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 640 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
641 RTX_FRAME_RELATED_P (pop_rtx) = 1;
642 offset = offset + 4;
643 par_index++;
cd3fd5ae 644
645 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
eae21ed1 646 }
647
648 /* Create (set sp sp+x). */
649
650 /* The offset value is already in place. No need to re-calculate it. */
651 adjust_sp_rtx
d1f9b275 652 = gen_rtx_SET (stack_pointer_rtx,
eae21ed1 653 plus_constant (Pmode, stack_pointer_rtx, offset));
654 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
eae21ed1 655
cd3fd5ae 656 /* Tell gcc we adjust SP in this insn. */
657 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
658
659 parallel_insn = emit_insn (parallel_insn);
660
661 /* The insn rtx 'parallel_insn' will change frame layout.
662 We need to use RTX_FRAME_RELATED_P so that GCC is able to
663 generate CFI (Call Frame Information) stuff. */
664 RTX_FRAME_RELATED_P (parallel_insn) = 1;
665
666 /* Add CFI info by manual. */
667 REG_NOTES (parallel_insn) = dwarf;
eae21ed1 668}
669
670/* Function to create a parallel rtx pattern
671 which presents stack v3push behavior.
672 The overall concept are:
673 "push registers to memory",
674 "adjust stack pointer". */
66978196 675static void
676nds32_emit_stack_v3push (rtx Rb,
677 rtx Re,
678 rtx En4 ATTRIBUTE_UNUSED,
679 rtx imm8u)
eae21ed1 680{
681 int regno;
682 int num_use_regs;
683 int par_index;
684 int offset;
685
686 rtx reg;
687 rtx mem;
688 rtx push_rtx;
689 rtx adjust_sp_rtx;
690 rtx parallel_insn;
691
692 /* We need to provide a customized rtx which contains
693 necessary information for data analysis,
694 so we create a parallel rtx like this:
66978196 695 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
eae21ed1 696 (reg:SI Rb))
697 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
698 (reg:SI Rb+1))
699 ...
700 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
701 (reg:SI Re))
702 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
703 (reg:SI FP_REGNUM))
704 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
705 (reg:SI GP_REGNUM))
706 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
707 (reg:SI LP_REGNUM))
708 (set (reg:SI SP_REGNUM)
709 (plus (reg:SI SP_REGNUM) (const_int -32-imm8u)))]) */
710
711 /* Calculate the number of registers that will be pushed.
712 Since $fp, $gp, and $lp is always pushed with v3push instruction,
713 we need to count these three registers.
714 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
715 So there is no need to worry about Rb=Re=SP_REGNUM case. */
716 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + 3;
717
718 /* In addition to used registers,
719 we need one more space for (set sp sp-x-imm8u) rtx. */
720 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
721 rtvec_alloc (num_use_regs + 1));
722 par_index = 0;
723
724 /* Initialize offset and start to create push behavior. */
725 offset = -(num_use_regs * 4);
726
727 /* Create (set mem regX) from Rb, Rb+1 up to Re.
728 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
729 So there is no need to worry about Rb=Re=SP_REGNUM case. */
730 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
731 {
732 reg = gen_rtx_REG (SImode, regno);
733 mem = gen_frame_mem (SImode, plus_constant (Pmode,
734 stack_pointer_rtx,
735 offset));
d1f9b275 736 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 737 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
738 RTX_FRAME_RELATED_P (push_rtx) = 1;
739 offset = offset + 4;
740 par_index++;
741 }
742
743 /* Create (set mem fp). */
744 reg = gen_rtx_REG (SImode, FP_REGNUM);
745 mem = gen_frame_mem (SImode, plus_constant (Pmode,
746 stack_pointer_rtx,
747 offset));
d1f9b275 748 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 749 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
750 RTX_FRAME_RELATED_P (push_rtx) = 1;
751 offset = offset + 4;
752 par_index++;
753 /* Create (set mem gp). */
754 reg = gen_rtx_REG (SImode, GP_REGNUM);
755 mem = gen_frame_mem (SImode, plus_constant (Pmode,
756 stack_pointer_rtx,
757 offset));
d1f9b275 758 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 759 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
760 RTX_FRAME_RELATED_P (push_rtx) = 1;
761 offset = offset + 4;
762 par_index++;
763 /* Create (set mem lp). */
764 reg = gen_rtx_REG (SImode, LP_REGNUM);
765 mem = gen_frame_mem (SImode, plus_constant (Pmode,
766 stack_pointer_rtx,
767 offset));
d1f9b275 768 push_rtx = gen_rtx_SET (mem, reg);
eae21ed1 769 XVECEXP (parallel_insn, 0, par_index) = push_rtx;
770 RTX_FRAME_RELATED_P (push_rtx) = 1;
771 offset = offset + 4;
772 par_index++;
773
774 /* Create (set sp sp-x-imm8u). */
775
776 /* We need to re-calculate the offset value again for adjustment. */
777 offset = -(num_use_regs * 4);
778 adjust_sp_rtx
d1f9b275 779 = gen_rtx_SET (stack_pointer_rtx,
eae21ed1 780 plus_constant (Pmode,
781 stack_pointer_rtx,
782 offset - INTVAL (imm8u)));
783 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
784 RTX_FRAME_RELATED_P (adjust_sp_rtx) = 1;
785
66978196 786 parallel_insn = emit_insn (parallel_insn);
787
788 /* The insn rtx 'parallel_insn' will change frame layout.
789 We need to use RTX_FRAME_RELATED_P so that GCC is able to
790 generate CFI (Call Frame Information) stuff. */
791 RTX_FRAME_RELATED_P (parallel_insn) = 1;
eae21ed1 792}
793
794/* Function to create a parallel rtx pattern
795 which presents stack v3pop behavior.
796 The overall concept are:
797 "pop registers from memory",
798 "adjust stack pointer". */
66978196 799static void
800nds32_emit_stack_v3pop (rtx Rb,
801 rtx Re,
802 rtx En4 ATTRIBUTE_UNUSED,
803 rtx imm8u)
eae21ed1 804{
805 int regno;
806 int num_use_regs;
807 int par_index;
808 int offset;
809
810 rtx reg;
811 rtx mem;
812 rtx pop_rtx;
813 rtx adjust_sp_rtx;
814 rtx parallel_insn;
66978196 815 rtx dwarf = NULL_RTX;
eae21ed1 816
817 /* We need to provide a customized rtx which contains
818 necessary information for data analysis,
819 so we create a parallel rtx like this:
820 (parallel [(set (reg:SI Rb)
821 (mem (reg:SI SP_REGNUM)))
822 (set (reg:SI Rb+1)
823 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
824 ...
825 (set (reg:SI Re)
826 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
827 (set (reg:SI FP_REGNUM)
828 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
829 (set (reg:SI GP_REGNUM)
830 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
831 (set (reg:SI LP_REGNUM)
832 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
833 (set (reg:SI SP_REGNUM)
834 (plus (reg:SI SP_REGNUM) (const_int 32+imm8u)))]) */
835
836 /* Calculate the number of registers that will be poped.
837 Since $fp, $gp, and $lp is always poped with v3pop instruction,
838 we need to count these three registers.
839 Under v3push, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
840 So there is no need to worry about Rb=Re=SP_REGNUM case. */
841 num_use_regs = REGNO (Re) - REGNO (Rb) + 1 + 3;
842
843 /* In addition to used registers,
844 we need one more space for (set sp sp+x+imm8u) rtx. */
845 parallel_insn = gen_rtx_PARALLEL (VOIDmode,
846 rtvec_alloc (num_use_regs + 1));
847 par_index = 0;
848
849 /* Initialize offset and start to create pop behavior. */
850 offset = 0;
851
852 /* Create (set regX mem) from Rb, Rb+1 up to Re.
853 Under v3pop, Rb is $r6, while Re is $r6, $r8, $r10, or $r14.
854 So there is no need to worry about Rb=Re=SP_REGNUM case. */
855 for (regno = REGNO (Rb); regno <= (int) REGNO (Re); regno++)
856 {
857 reg = gen_rtx_REG (SImode, regno);
858 mem = gen_frame_mem (SImode, plus_constant (Pmode,
859 stack_pointer_rtx,
860 offset));
d1f9b275 861 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 862 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
863 RTX_FRAME_RELATED_P (pop_rtx) = 1;
864 offset = offset + 4;
865 par_index++;
66978196 866
867 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
eae21ed1 868 }
869
870 /* Create (set fp mem). */
871 reg = gen_rtx_REG (SImode, FP_REGNUM);
872 mem = gen_frame_mem (SImode, plus_constant (Pmode,
873 stack_pointer_rtx,
874 offset));
d1f9b275 875 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 876 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
877 RTX_FRAME_RELATED_P (pop_rtx) = 1;
878 offset = offset + 4;
879 par_index++;
66978196 880 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
881
eae21ed1 882 /* Create (set gp mem). */
883 reg = gen_rtx_REG (SImode, GP_REGNUM);
884 mem = gen_frame_mem (SImode, plus_constant (Pmode,
885 stack_pointer_rtx,
886 offset));
d1f9b275 887 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 888 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
889 RTX_FRAME_RELATED_P (pop_rtx) = 1;
890 offset = offset + 4;
891 par_index++;
66978196 892 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
893
eae21ed1 894 /* Create (set lp mem ). */
895 reg = gen_rtx_REG (SImode, LP_REGNUM);
896 mem = gen_frame_mem (SImode, plus_constant (Pmode,
897 stack_pointer_rtx,
898 offset));
d1f9b275 899 pop_rtx = gen_rtx_SET (reg, mem);
eae21ed1 900 XVECEXP (parallel_insn, 0, par_index) = pop_rtx;
901 RTX_FRAME_RELATED_P (pop_rtx) = 1;
902 offset = offset + 4;
903 par_index++;
66978196 904 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
eae21ed1 905
906 /* Create (set sp sp+x+imm8u). */
907
908 /* The offset value is already in place. No need to re-calculate it. */
909 adjust_sp_rtx
d1f9b275 910 = gen_rtx_SET (stack_pointer_rtx,
eae21ed1 911 plus_constant (Pmode,
912 stack_pointer_rtx,
913 offset + INTVAL (imm8u)));
914 XVECEXP (parallel_insn, 0, par_index) = adjust_sp_rtx;
eae21ed1 915
66978196 916 /* Tell gcc we adjust SP in this insn. */
917 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, copy_rtx (adjust_sp_rtx), dwarf);
918
919 parallel_insn = emit_insn (parallel_insn);
920
921 /* The insn rtx 'parallel_insn' will change frame layout.
922 We need to use RTX_FRAME_RELATED_P so that GCC is able to
923 generate CFI (Call Frame Information) stuff. */
924 RTX_FRAME_RELATED_P (parallel_insn) = 1;
925
926 /* Add CFI info by manual. */
927 REG_NOTES (parallel_insn) = dwarf;
eae21ed1 928}
929
eae21ed1 930/* Function that may creates more instructions
931 for large value on adjusting stack pointer.
932
933 In nds32 target, 'addi' can be used for stack pointer
934 adjustment in prologue/epilogue stage.
935 However, sometimes there are too many local variables so that
936 the adjustment value is not able to be fit in the 'addi' instruction.
937 One solution is to move value into a register
938 and then use 'add' instruction.
939 In practice, we use TA_REGNUM ($r15) to accomplish this purpose.
940 Also, we need to return zero for sp adjustment so that
941 proglogue/epilogue knows there is no need to create 'addi' instruction. */
942static int
943nds32_force_addi_stack_int (int full_value)
944{
945 int adjust_value;
946
947 rtx tmp_reg;
948 rtx sp_adjust_insn;
949
950 if (!satisfies_constraint_Is15 (GEN_INT (full_value)))
951 {
952 /* The value is not able to fit in single addi instruction.
953 Create more instructions of moving value into a register
954 and then add stack pointer with it. */
955
956 /* $r15 is going to be temporary register to hold the value. */
957 tmp_reg = gen_rtx_REG (SImode, TA_REGNUM);
958
959 /* Create one more instruction to move value
960 into the temporary register. */
961 emit_move_insn (tmp_reg, GEN_INT (full_value));
962
963 /* Create new 'add' rtx. */
964 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
965 stack_pointer_rtx,
966 tmp_reg);
967 /* Emit rtx into insn list and receive its transformed insn rtx. */
968 sp_adjust_insn = emit_insn (sp_adjust_insn);
969
970 /* At prologue, we need to tell GCC that this is frame related insn,
971 so that we can consider this instruction to output debug information.
972 If full_value is NEGATIVE, it means this function
973 is invoked by expand_prologue. */
974 if (full_value < 0)
975 {
976 /* Because (tmp_reg <- full_value) may be split into two
977 rtl patterns, we can not set its RTX_FRAME_RELATED_P.
978 We need to construct another (sp <- sp + full_value)
979 and then insert it into sp_adjust_insn's reg note to
980 represent a frame related expression.
981 GCC knows how to refer it and output debug information. */
982
983 rtx plus_rtx;
984 rtx set_rtx;
985
986 plus_rtx = plus_constant (Pmode, stack_pointer_rtx, full_value);
d1f9b275 987 set_rtx = gen_rtx_SET (stack_pointer_rtx, plus_rtx);
eae21ed1 988 add_reg_note (sp_adjust_insn, REG_FRAME_RELATED_EXPR, set_rtx);
989
990 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
991 }
992
993 /* We have used alternative way to adjust stack pointer value.
994 Return zero so that prologue/epilogue
995 will not generate other instructions. */
996 return 0;
997 }
998 else
999 {
1000 /* The value is able to fit in addi instruction.
1001 However, remember to make it to be positive value
1002 because we want to return 'adjustment' result. */
1003 adjust_value = (full_value < 0) ? (-full_value) : (full_value);
1004
1005 return adjust_value;
1006 }
1007}
1008
1009/* Return true if MODE/TYPE need double word alignment. */
1010static bool
3754d046 1011nds32_needs_double_word_align (machine_mode mode, const_tree type)
eae21ed1 1012{
1013 unsigned int align;
1014
433ee92e 1015 /* Pick up the alignment according to the mode or type. */
1016 align = NDS32_MODE_TYPE_ALIGN (mode, type);
eae21ed1 1017
1018 return (align > PARM_BOUNDARY);
1019}
1020
1021/* Return true if FUNC is a naked function. */
223d3656 1022static bool
1023nds32_naked_function_p (tree func)
eae21ed1 1024{
1025 tree t;
1026
1027 if (TREE_CODE (func) != FUNCTION_DECL)
1028 abort ();
1029
1030 t = lookup_attribute ("naked", DECL_ATTRIBUTES (func));
1031
1032 return (t != NULL_TREE);
1033}
1034
1035/* Function that check if 'X' is a valid address register.
1036 The variable 'STRICT' is very important to
1037 make decision for register number.
1038
1039 STRICT : true
1040 => We are in reload pass or after reload pass.
1041 The register number should be strictly limited in general registers.
1042
1043 STRICT : false
1044 => Before reload pass, we are free to use any register number. */
1045static bool
1046nds32_address_register_rtx_p (rtx x, bool strict)
1047{
1048 int regno;
1049
1050 if (GET_CODE (x) != REG)
1051 return false;
1052
1053 regno = REGNO (x);
1054
1055 if (strict)
1056 return REGNO_OK_FOR_BASE_P (regno);
1057 else
1058 return true;
1059}
1060
1061/* Function that check if 'INDEX' is valid to be a index rtx for address.
1062
1063 OUTER_MODE : Machine mode of outer address rtx.
1064 INDEX : Check if this rtx is valid to be a index for address.
1065 STRICT : If it is true, we are in reload pass or after reload pass. */
1066static bool
3754d046 1067nds32_legitimate_index_p (machine_mode outer_mode,
eae21ed1 1068 rtx index,
1069 bool strict)
1070{
1071 int regno;
1072 rtx op0;
1073 rtx op1;
1074
1075 switch (GET_CODE (index))
1076 {
1077 case REG:
1078 regno = REGNO (index);
1079 /* If we are in reload pass or after reload pass,
1080 we need to limit it to general register. */
1081 if (strict)
1082 return REGNO_OK_FOR_INDEX_P (regno);
1083 else
1084 return true;
1085
1086 case CONST_INT:
1087 /* The alignment of the integer value is determined by 'outer_mode'. */
1088 if (GET_MODE_SIZE (outer_mode) == 1)
1089 {
1090 /* Further check if the value is legal for the 'outer_mode'. */
1091 if (!satisfies_constraint_Is15 (index))
1092 return false;
1093
1094 /* Pass all test, the value is valid, return true. */
1095 return true;
1096 }
1097 if (GET_MODE_SIZE (outer_mode) == 2
1098 && NDS32_HALF_WORD_ALIGN_P (INTVAL (index)))
1099 {
1100 /* Further check if the value is legal for the 'outer_mode'. */
1101 if (!satisfies_constraint_Is16 (index))
1102 return false;
1103
1104 /* Pass all test, the value is valid, return true. */
1105 return true;
1106 }
1107 if (GET_MODE_SIZE (outer_mode) == 4
1108 && NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1109 {
1110 /* Further check if the value is legal for the 'outer_mode'. */
1111 if (!satisfies_constraint_Is17 (index))
1112 return false;
1113
1114 /* Pass all test, the value is valid, return true. */
1115 return true;
1116 }
1117 if (GET_MODE_SIZE (outer_mode) == 8
1118 && NDS32_SINGLE_WORD_ALIGN_P (INTVAL (index)))
1119 {
1120 /* Further check if the value is legal for the 'outer_mode'. */
1121 if (!satisfies_constraint_Is17 (gen_int_mode (INTVAL (index) + 4,
1122 SImode)))
1123 return false;
1124
1125 /* Pass all test, the value is valid, return true. */
1126 return true;
1127 }
1128
1129 return false;
1130
1131 case MULT:
1132 op0 = XEXP (index, 0);
1133 op1 = XEXP (index, 1);
1134
1135 if (REG_P (op0) && CONST_INT_P (op1))
1136 {
1137 int multiplier;
1138 multiplier = INTVAL (op1);
1139
1140 /* We only allow (mult reg const_int_1)
1141 or (mult reg const_int_2) or (mult reg const_int_4). */
1142 if (multiplier != 1 && multiplier != 2 && multiplier != 4)
1143 return false;
1144
1145 regno = REGNO (op0);
1146 /* Limit it in general registers if we are
1147 in reload pass or after reload pass. */
1148 if(strict)
1149 return REGNO_OK_FOR_INDEX_P (regno);
1150 else
1151 return true;
1152 }
1153
1154 return false;
1155
1156 case ASHIFT:
1157 op0 = XEXP (index, 0);
1158 op1 = XEXP (index, 1);
1159
1160 if (REG_P (op0) && CONST_INT_P (op1))
1161 {
1162 int sv;
1163 /* op1 is already the sv value for use to do left shift. */
1164 sv = INTVAL (op1);
1165
1166 /* We only allow (ashift reg const_int_0)
1167 or (ashift reg const_int_1) or (ashift reg const_int_2). */
1168 if (sv != 0 && sv != 1 && sv !=2)
1169 return false;
1170
1171 regno = REGNO (op0);
1172 /* Limit it in general registers if we are
1173 in reload pass or after reload pass. */
1174 if(strict)
1175 return REGNO_OK_FOR_INDEX_P (regno);
1176 else
1177 return true;
1178 }
1179
1180 return false;
1181
1182 default:
1183 return false;
1184 }
1185}
1186
eae21ed1 1187/* ------------------------------------------------------------------------ */
1188
1189/* PART 3: Implement target hook stuff definitions. */
1190\f
1191/* Register Classes. */
1192
1193static unsigned char
1194nds32_class_max_nregs (reg_class_t rclass ATTRIBUTE_UNUSED,
3754d046 1195 machine_mode mode)
eae21ed1 1196{
1197 /* Return the maximum number of consecutive registers
1198 needed to represent "mode" in a register of "rclass". */
1199 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1200}
1201
1202static int
1203nds32_register_priority (int hard_regno)
1204{
1205 /* Encourage to use r0-r7 for LRA when optimize for size. */
1206 if (optimize_size && hard_regno < 8)
1207 return 4;
1208 return 3;
1209}
1210
1211\f
1212/* Stack Layout and Calling Conventions. */
1213
1214/* There are three kinds of pointer concepts using in GCC compiler:
1215
1216 frame pointer: A pointer to the first location of local variables.
1217 stack pointer: A pointer to the top of a stack frame.
1218 argument pointer: A pointer to the incoming arguments.
1219
1220 In nds32 target calling convention, we are using 8-byte alignment.
1221 Besides, we would like to have each stack frame of a function includes:
1222
1223 [Block A]
1224 1. previous hard frame pointer
1225 2. return address
1226 3. callee-saved registers
1227 4. <padding bytes> (we will calculte in nds32_compute_stack_frame()
1228 and save it at
1229 cfun->machine->callee_saved_area_padding_bytes)
1230
1231 [Block B]
1232 1. local variables
1233 2. spilling location
1234 3. <padding bytes> (it will be calculated by GCC itself)
1235 4. incoming arguments
1236 5. <padding bytes> (it will be calculated by GCC itself)
1237
1238 [Block C]
1239 1. <padding bytes> (it will be calculated by GCC itself)
1240 2. outgoing arguments
1241
1242 We 'wrap' these blocks together with
1243 hard frame pointer ($r28) and stack pointer ($r31).
1244 By applying the basic frame/stack/argument pointers concept,
1245 the layout of a stack frame shoule be like this:
1246
1247 | |
1248 old stack pointer -> ----
1249 | | \
1250 | | saved arguments for
1251 | | vararg functions
1252 | | /
1253 hard frame pointer -> --
1254 & argument pointer | | \
1255 | | previous hardware frame pointer
1256 | | return address
1257 | | callee-saved registers
1258 | | /
1259 frame pointer -> --
1260 | | \
1261 | | local variables
1262 | | and incoming arguments
1263 | | /
1264 --
1265 | | \
1266 | | outgoing
1267 | | arguments
1268 | | /
1269 stack pointer -> ----
1270
1271 $SFP and $AP are used to represent frame pointer and arguments pointer,
1272 which will be both eliminated as hard frame pointer. */
1273
1274/* -- Eliminating Frame Pointer and Arg Pointer. */
1275
7a878515 1276static bool
1277nds32_can_eliminate (const int from_reg, const int to_reg)
eae21ed1 1278{
1279 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1280 return true;
1281
1282 if (from_reg == ARG_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1283 return true;
1284
1285 if (from_reg == FRAME_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
1286 return true;
1287
1288 if (from_reg == FRAME_POINTER_REGNUM && to_reg == HARD_FRAME_POINTER_REGNUM)
1289 return true;
1290
1291 return false;
1292}
1293
1294/* -- Passing Arguments in Registers. */
1295
1296static rtx
3754d046 1297nds32_function_arg (cumulative_args_t ca, machine_mode mode,
eae21ed1 1298 const_tree type, bool named)
1299{
74022996 1300 unsigned int regno;
eae21ed1 1301 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1302
1303 /* The last time this hook is called,
1304 it is called with MODE == VOIDmode. */
1305 if (mode == VOIDmode)
1306 return NULL_RTX;
1307
74022996 1308 /* For nameless arguments, we need to take care it individually. */
eae21ed1 1309 if (!named)
eae21ed1 1310 {
74022996 1311 /* If we are under hard float abi, we have arguments passed on the
1312 stack and all situation can be handled by GCC itself. */
1313 if (TARGET_HARD_FLOAT)
1314 return NULL_RTX;
1315
1316 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1317 {
1318 /* If we still have enough registers to pass argument, pick up
1319 next available register number. */
1320 regno
1321 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1322 return gen_rtx_REG (mode, regno);
1323 }
433ee92e 1324
eae21ed1 1325 /* No register available, return NULL_RTX.
1326 The compiler will use stack to pass argument instead. */
1327 return NULL_RTX;
1328 }
74022996 1329
1330 /* The following is to handle named argument.
1331 Note that the strategies of TARGET_HARD_FLOAT and !TARGET_HARD_FLOAT
1332 are different. */
1333 if (TARGET_HARD_FLOAT)
1334 {
1335 /* Currently we have not implemented hard float yet. */
1336 gcc_unreachable ();
1337 }
1338 else
1339 {
1340 /* For !TARGET_HARD_FLOAT calling convention, we always use GPR to pass
1341 argument. Since we allow to pass argument partially in registers,
1342 we can just return it if there are still registers available. */
1343 if (NDS32_ARG_PARTIAL_IN_GPR_REG_P (cum->gpr_offset, mode, type))
1344 {
1345 /* Pick up the next available register number. */
1346 regno
1347 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type);
1348 return gen_rtx_REG (mode, regno);
1349 }
1350
1351 }
1352
1353 /* No register available, return NULL_RTX.
1354 The compiler will use stack to pass argument instead. */
1355 return NULL_RTX;
eae21ed1 1356}
1357
1d9df010 1358static bool
3754d046 1359nds32_must_pass_in_stack (machine_mode mode, const_tree type)
1d9df010 1360{
1361 /* Return true if a type must be passed in memory.
1362 If it is NOT using hard float abi, small aggregates can be
1363 passed in a register even we are calling a variadic function.
1364 So there is no need to take padding into consideration. */
1365 if (TARGET_HARD_FLOAT)
1366 return must_pass_in_stack_var_size_or_pad (mode, type);
1367 else
1368 return must_pass_in_stack_var_size (mode, type);
1369}
1370
a87d4efb 1371static int
3754d046 1372nds32_arg_partial_bytes (cumulative_args_t ca, machine_mode mode,
a87d4efb 1373 tree type, bool named ATTRIBUTE_UNUSED)
1374{
1375 /* Returns the number of bytes at the beginning of an argument that
1376 must be put in registers. The value must be zero for arguments that are
1377 passed entirely in registers or that are entirely pushed on the stack.
1378 Besides, TARGET_FUNCTION_ARG for these arguments should return the
1379 first register to be used by the caller for this argument. */
1380 unsigned int needed_reg_count;
1381 unsigned int remaining_reg_count;
1382 CUMULATIVE_ARGS *cum;
1383
1384 cum = get_cumulative_args (ca);
1385
1386 /* Under hard float abi, we better have argument entirely passed in
1387 registers or pushed on the stack so that we can reduce the complexity
1388 of dealing with cum->gpr_offset and cum->fpr_offset. */
1389 if (TARGET_HARD_FLOAT)
1390 return 0;
1391
1392 /* If we have already runned out of argument registers, return zero
1393 so that the argument will be entirely pushed on the stack. */
1394 if (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1395 >= NDS32_GPR_ARG_FIRST_REGNUM + NDS32_MAX_GPR_REGS_FOR_ARGS)
1396 return 0;
1397
1398 /* Calculate how many registers do we need for this argument. */
1399 needed_reg_count = NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1400
1401 /* Calculate how many argument registers have left for passing argument.
1402 Note that we should count it from next available register number. */
1403 remaining_reg_count
1404 = NDS32_MAX_GPR_REGS_FOR_ARGS
1405 - (NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1406 - NDS32_GPR_ARG_FIRST_REGNUM);
1407
1408 /* Note that we have to return the nubmer of bytes, not registers count. */
1409 if (needed_reg_count > remaining_reg_count)
1410 return remaining_reg_count * UNITS_PER_WORD;
1411
1412 return 0;
1413}
1414
eae21ed1 1415static void
3754d046 1416nds32_function_arg_advance (cumulative_args_t ca, machine_mode mode,
eae21ed1 1417 const_tree type, bool named)
1418{
3754d046 1419 machine_mode sub_mode;
eae21ed1 1420 CUMULATIVE_ARGS *cum = get_cumulative_args (ca);
1421
eae21ed1 1422 if (named)
1423 {
74022996 1424 /* We need to further check TYPE and MODE so that we can determine
1425 which kind of register we shall advance. */
1426 if (type && TREE_CODE (type) == COMPLEX_TYPE)
1427 sub_mode = TYPE_MODE (TREE_TYPE (type));
1428 else
1429 sub_mode = mode;
1430
1431 /* Under hard float abi, we may advance FPR registers. */
1432 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (sub_mode) == MODE_FLOAT)
1433 {
1434 /* Currently we have not implemented hard float yet. */
1435 gcc_unreachable ();
1436 }
1437 else
1438 {
1439 cum->gpr_offset
1440 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1441 - NDS32_GPR_ARG_FIRST_REGNUM
1442 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1443 }
1444 }
1445 else
1446 {
1447 /* If this nameless argument is NOT under TARGET_HARD_FLOAT,
1448 we can advance next register as well so that caller is
1449 able to pass arguments in registers and callee must be
1450 in charge of pushing all of them into stack. */
1451 if (!TARGET_HARD_FLOAT)
1452 {
1453 cum->gpr_offset
1454 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1455 - NDS32_GPR_ARG_FIRST_REGNUM
1456 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1457 }
eae21ed1 1458 }
1459}
1460
1461static unsigned int
3754d046 1462nds32_function_arg_boundary (machine_mode mode, const_tree type)
eae21ed1 1463{
1464 return (nds32_needs_double_word_align (mode, type)
1465 ? NDS32_DOUBLE_WORD_ALIGNMENT
1466 : PARM_BOUNDARY);
1467}
1468
1469/* -- How Scalar Function Values Are Returned. */
1470
1471static rtx
1472nds32_function_value (const_tree ret_type,
1473 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1474 bool outgoing ATTRIBUTE_UNUSED)
1475{
3754d046 1476 machine_mode mode;
eae21ed1 1477 int unsignedp;
1478
1479 mode = TYPE_MODE (ret_type);
1480 unsignedp = TYPE_UNSIGNED (ret_type);
1481
1482 mode = promote_mode (ret_type, mode, &unsignedp);
1483
1484 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
1485}
1486
1487static rtx
3754d046 1488nds32_libcall_value (machine_mode mode,
eae21ed1 1489 const_rtx fun ATTRIBUTE_UNUSED)
1490{
1491 return gen_rtx_REG (mode, NDS32_GPR_RET_FIRST_REGNUM);
1492}
1493
1494static bool
1495nds32_function_value_regno_p (const unsigned int regno)
1496{
1497 return (regno == NDS32_GPR_RET_FIRST_REGNUM);
1498}
1499
1500/* -- Function Entry and Exit. */
1501
1502/* The content produced from this function
1503 will be placed before prologue body. */
1504static void
1505nds32_asm_function_prologue (FILE *file,
1506 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1507{
1508 int r;
1509 const char *func_name;
1510 tree attrs;
1511 tree name;
1512
1513 /* All stack frame information is supposed to be
1514 already computed when expanding prologue.
1515 The result is in cfun->machine.
1516 DO NOT call nds32_compute_stack_frame() here
1517 because it may corrupt the essential information. */
1518
1519 fprintf (file, "\t! BEGIN PROLOGUE\n");
1520 fprintf (file, "\t! fp needed: %d\n", frame_pointer_needed);
1521 fprintf (file, "\t! pretend_args: %d\n", cfun->machine->va_args_size);
1522 fprintf (file, "\t! local_size: %d\n", cfun->machine->local_size);
1523 fprintf (file, "\t! out_args_size: %d\n", cfun->machine->out_args_size);
1524
1525 /* Use df_regs_ever_live_p() to detect if the register
1526 is ever used in the current function. */
1527 fprintf (file, "\t! registers ever_live: ");
1528 for (r = 0; r < 32; r++)
1529 {
1530 if (df_regs_ever_live_p (r))
1531 fprintf (file, "%s, ", reg_names[r]);
1532 }
1533 fputc ('\n', file);
1534
1535 /* Display the attributes of this function. */
1536 fprintf (file, "\t! function attributes: ");
0f32a27e 1537 /* Get the attributes tree list.
1538 Note that GCC builds attributes list with reverse order. */
1539 attrs = DECL_ATTRIBUTES (current_function_decl);
eae21ed1 1540
1541 /* If there is no any attribute, print out "None". */
1542 if (!attrs)
1543 fprintf (file, "None");
1544
1545 /* If there are some attributes, try if we need to
1546 construct isr vector information. */
1547 func_name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
1548 nds32_construct_isr_vectors_information (attrs, func_name);
1549
1550 /* Display all attributes of this function. */
1551 while (attrs)
1552 {
1553 name = TREE_PURPOSE (attrs);
1554 fprintf (file, "%s ", IDENTIFIER_POINTER (name));
1555
1556 /* Pick up the next attribute. */
1557 attrs = TREE_CHAIN (attrs);
1558 }
1559 fputc ('\n', file);
1560}
1561
1562/* After rtl prologue has been expanded, this function is used. */
1563static void
1564nds32_asm_function_end_prologue (FILE *file)
1565{
1566 fprintf (file, "\t! END PROLOGUE\n");
1567
1568 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
1569 we can generate special directive: ".omit_fp_begin"
1570 to guide linker doing fp-as-gp optimization.
1571 However, for a naked function, which means
1572 it should not have prologue/epilogue,
1573 using fp-as-gp still requires saving $fp by push/pop behavior and
1574 there is no benefit to use fp-as-gp on such small function.
1575 So we need to make sure this function is NOT naked as well. */
1576 if (!frame_pointer_needed
1577 && !cfun->machine->naked_p
1578 && cfun->machine->fp_as_gp_p)
1579 {
1580 fprintf (file, "\t! ----------------------------------------\n");
1581 fprintf (file, "\t! Guide linker to do "
1582 "link time optimization: fp-as-gp\n");
1583 fprintf (file, "\t! We add one more instruction to "
1584 "initialize $fp near to $gp location.\n");
1585 fprintf (file, "\t! If linker fails to use fp-as-gp transformation,\n");
1586 fprintf (file, "\t! this extra instruction should be "
1587 "eliminated at link stage.\n");
1588 fprintf (file, "\t.omit_fp_begin\n");
1589 fprintf (file, "\tla\t$fp,_FP_BASE_\n");
1590 fprintf (file, "\t! ----------------------------------------\n");
1591 }
1592}
1593
1594/* Before rtl epilogue has been expanded, this function is used. */
1595static void
1596nds32_asm_function_begin_epilogue (FILE *file)
1597{
1598 /* If frame pointer is NOT needed and -mfp-as-gp is issued,
1599 we can generate special directive: ".omit_fp_end"
1600 to claim fp-as-gp optimization range.
1601 However, for a naked function,
1602 which means it should not have prologue/epilogue,
1603 using fp-as-gp still requires saving $fp by push/pop behavior and
1604 there is no benefit to use fp-as-gp on such small function.
1605 So we need to make sure this function is NOT naked as well. */
1606 if (!frame_pointer_needed
1607 && !cfun->machine->naked_p
1608 && cfun->machine->fp_as_gp_p)
1609 {
1610 fprintf (file, "\t! ----------------------------------------\n");
1611 fprintf (file, "\t! Claim the range of fp-as-gp "
1612 "link time optimization\n");
1613 fprintf (file, "\t.omit_fp_end\n");
1614 fprintf (file, "\t! ----------------------------------------\n");
1615 }
1616
1617 fprintf (file, "\t! BEGIN EPILOGUE\n");
1618}
1619
1620/* The content produced from this function
1621 will be placed after epilogue body. */
1622static void
1623nds32_asm_function_epilogue (FILE *file,
1624 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1625{
1626 fprintf (file, "\t! END EPILOGUE\n");
1627}
1628
1629static void
1630nds32_asm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
1631 HOST_WIDE_INT delta,
1632 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
1633 tree function)
1634{
1635 int this_regno;
1636
1637 /* Make sure unwind info is emitted for the thunk if needed. */
1638 final_start_function (emit_barrier (), file, 1);
1639
1640 this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
1641 ? 1
1642 : 0);
1643
1644 if (delta != 0)
1645 {
1646 if (satisfies_constraint_Is15 (GEN_INT (delta)))
1647 {
1648 fprintf (file, "\taddi\t$r%d, $r%d, %ld\n",
1649 this_regno, this_regno, delta);
1650 }
1651 else if (satisfies_constraint_Is20 (GEN_INT (delta)))
1652 {
1653 fprintf (file, "\tmovi\t$ta, %ld\n", delta);
1654 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
1655 }
1656 else
1657 {
1658 fprintf (file, "\tsethi\t$ta, hi20(%ld)\n", delta);
1659 fprintf (file, "\tori\t$ta, $ta, lo12(%ld)\n", delta);
1660 fprintf (file, "\tadd\t$r%d, $r%d, $ta\n", this_regno, this_regno);
1661 }
1662 }
1663
1664 fprintf (file, "\tb\t");
1665 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
1666 fprintf (file, "\n");
1667
1668 final_end_function ();
1669}
1670
1671/* -- Permitting tail calls. */
1672
1673/* Determine whether we need to enable warning for function return check. */
1674static bool
1675nds32_warn_func_return (tree decl)
1676{
1677/* Naked functions are implemented entirely in assembly, including the
1678 return sequence, so suppress warnings about this. */
1679 return !nds32_naked_function_p (decl);
1680}
1681
1682\f
1683/* Implementing the Varargs Macros. */
1684
fb0c8d3a 1685static void
1686nds32_setup_incoming_varargs (cumulative_args_t ca,
3754d046 1687 machine_mode mode,
fb0c8d3a 1688 tree type,
1689 int *pretend_args_size,
1690 int second_time ATTRIBUTE_UNUSED)
1691{
1692 unsigned int total_args_regs;
1693 unsigned int num_of_used_regs;
1694 unsigned int remaining_reg_count;
1695 CUMULATIVE_ARGS *cum;
1696
1697 /* If we are under hard float abi, we do not need to set *pretend_args_size.
1698 So that all nameless arguments are pushed by caller and all situation
1699 can be handled by GCC itself. */
1700 if (TARGET_HARD_FLOAT)
1701 return;
1702
1703 /* We are using NDS32_MAX_GPR_REGS_FOR_ARGS registers,
1704 counting from NDS32_GPR_ARG_FIRST_REGNUM, for saving incoming arguments.
1705 However, for nameless(anonymous) arguments, we should push them on the
1706 stack so that all the nameless arguments appear to have been passed
1707 consecutively in the memory for accessing. Hence, we need to check and
1708 exclude the registers that are used for named arguments. */
1709
1710 cum = get_cumulative_args (ca);
1711
1712 /* The MODE and TYPE describe the last argument.
1713 We need those information to determine the remaining registers
1714 for varargs. */
1715 total_args_regs
1716 = NDS32_MAX_GPR_REGS_FOR_ARGS + NDS32_GPR_ARG_FIRST_REGNUM;
1717 num_of_used_regs
1718 = NDS32_AVAILABLE_REGNUM_FOR_GPR_ARG (cum->gpr_offset, mode, type)
1719 + NDS32_NEED_N_REGS_FOR_ARG (mode, type);
1720
1721 remaining_reg_count = total_args_regs - num_of_used_regs;
1722 *pretend_args_size = remaining_reg_count * UNITS_PER_WORD;
1723
1724 return;
1725}
1726
eae21ed1 1727static bool
1728nds32_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
1729{
fb0c8d3a 1730 /* If this hook returns true, the named argument of FUNCTION_ARG is always
1731 true for named arguments, and false for unnamed arguments. */
eae21ed1 1732 return true;
1733}
1734
1735\f
1736/* Trampolines for Nested Functions. */
1737
1738static void
1739nds32_asm_trampoline_template (FILE *f)
1740{
1741 if (TARGET_REDUCED_REGS)
1742 {
1743 /* Trampoline is not supported on reduced-set registers yet. */
1744 sorry ("a nested function is not supported for reduced registers");
1745 }
1746 else
1747 {
1748 asm_fprintf (f, "\t! Trampoline code template\n");
1749 asm_fprintf (f, "\t! This code fragment will be copied "
1750 "into stack on demand\n");
1751
1752 asm_fprintf (f, "\tmfusr\t$r16,$pc\n");
1753 asm_fprintf (f, "\tlwi\t$r15,[$r16 + 20] "
1754 "! load nested function address\n");
1755 asm_fprintf (f, "\tlwi\t$r16,[$r16 + 16] "
1756 "! load chain_value\n");
1757 asm_fprintf (f, "\tjr\t$r15\n");
1758 }
1759
1760 /* Preserve space ($pc + 16) for saving chain_value,
1761 nds32_trampoline_init will fill the value in this slot. */
1762 asm_fprintf (f, "\t! space for saving chain_value\n");
1763 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
1764
1765 /* Preserve space ($pc + 20) for saving nested function address,
1766 nds32_trampoline_init will fill the value in this slot. */
1767 asm_fprintf (f, "\t! space for saving nested function address\n");
1768 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
1769}
1770
1771/* Emit RTL insns to initialize the variable parts of a trampoline. */
1772static void
1773nds32_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1774{
1775 int i;
1776
1777 /* Nested function address. */
1778 rtx fnaddr;
1779 /* The memory rtx that is going to
1780 be filled with chain_value. */
1781 rtx chain_value_mem;
1782 /* The memory rtx that is going to
1783 be filled with nested function address. */
1784 rtx nested_func_mem;
1785
1786 /* Start address of trampoline code in stack, for doing cache sync. */
1787 rtx sync_cache_addr;
1788 /* Temporary register for sync instruction. */
1789 rtx tmp_reg;
1790 /* Instruction-cache sync instruction,
1791 requesting an argument as starting address. */
1792 rtx isync_insn;
1793 /* For convenience reason of doing comparison. */
1794 int tramp_align_in_bytes;
1795
1796 /* Trampoline is not supported on reduced-set registers yet. */
1797 if (TARGET_REDUCED_REGS)
1798 sorry ("a nested function is not supported for reduced registers");
1799
1800 /* STEP 1: Copy trampoline code template into stack,
1801 fill up essential data into stack. */
1802
1803 /* Extract nested function address rtx. */
1804 fnaddr = XEXP (DECL_RTL (fndecl), 0);
1805
1806 /* m_tramp is memory rtx that is going to be filled with trampoline code.
1807 We have nds32_asm_trampoline_template() to emit template pattern. */
1808 emit_block_move (m_tramp, assemble_trampoline_template (),
1809 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
1810
1811 /* After copying trampoline code into stack,
1812 fill chain_value into stack. */
1813 chain_value_mem = adjust_address (m_tramp, SImode, 16);
1814 emit_move_insn (chain_value_mem, chain_value);
1815 /* After copying trampoline code int stack,
1816 fill nested function address into stack. */
1817 nested_func_mem = adjust_address (m_tramp, SImode, 20);
1818 emit_move_insn (nested_func_mem, fnaddr);
1819
1820 /* STEP 2: Sync instruction-cache. */
1821
1822 /* We have successfully filled trampoline code into stack.
1823 However, in order to execute code in stack correctly,
1824 we must sync instruction cache. */
1825 sync_cache_addr = XEXP (m_tramp, 0);
1826 tmp_reg = gen_reg_rtx (SImode);
1827 isync_insn = gen_unspec_volatile_isync (tmp_reg);
1828
1829 /* Because nds32_cache_block_size is in bytes,
1830 we get trampoline alignment in bytes for convenient comparison. */
1831 tramp_align_in_bytes = TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT;
1832
1833 if (tramp_align_in_bytes >= nds32_cache_block_size
1834 && (tramp_align_in_bytes % nds32_cache_block_size) == 0)
1835 {
1836 /* Under this condition, the starting address of trampoline
1837 must be aligned to the starting address of each cache block
1838 and we do not have to worry about cross-boundary issue. */
1839 for (i = 0;
1840 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
1841 / nds32_cache_block_size;
1842 i++)
1843 {
1844 emit_move_insn (tmp_reg,
1845 plus_constant (Pmode, sync_cache_addr,
1846 nds32_cache_block_size * i));
1847 emit_insn (isync_insn);
1848 }
1849 }
1850 else if (TRAMPOLINE_SIZE > nds32_cache_block_size)
1851 {
1852 /* The starting address of trampoline code
1853 may not be aligned to the cache block,
1854 so the trampoline code may be across two cache block.
1855 We need to sync the last element, which is 4-byte size,
1856 of trampoline template. */
1857 for (i = 0;
1858 i < (TRAMPOLINE_SIZE + nds32_cache_block_size - 1)
1859 / nds32_cache_block_size;
1860 i++)
1861 {
1862 emit_move_insn (tmp_reg,
1863 plus_constant (Pmode, sync_cache_addr,
1864 nds32_cache_block_size * i));
1865 emit_insn (isync_insn);
1866 }
1867
1868 /* The last element of trampoline template is 4-byte size. */
1869 emit_move_insn (tmp_reg,
1870 plus_constant (Pmode, sync_cache_addr,
1871 TRAMPOLINE_SIZE - 4));
1872 emit_insn (isync_insn);
1873 }
1874 else
1875 {
1876 /* This is the simplest case.
1877 Because TRAMPOLINE_SIZE is less than or
1878 equal to nds32_cache_block_size,
1879 we can just sync start address and
1880 the last element of trampoline code. */
1881
1882 /* Sync starting address of tampoline code. */
1883 emit_move_insn (tmp_reg, sync_cache_addr);
1884 emit_insn (isync_insn);
1885 /* Sync the last element, which is 4-byte size,
1886 of trampoline template. */
1887 emit_move_insn (tmp_reg,
1888 plus_constant (Pmode, sync_cache_addr,
1889 TRAMPOLINE_SIZE - 4));
1890 emit_insn (isync_insn);
1891 }
1892
1893 /* Set instruction serialization barrier
1894 to guarantee the correct operations. */
1895 emit_insn (gen_unspec_volatile_isb ());
1896}
1897
1898\f
1899/* Addressing Modes. */
1900
1901static bool
3754d046 1902nds32_legitimate_address_p (machine_mode mode, rtx x, bool strict)
eae21ed1 1903{
1904 /* For (mem:DI addr) or (mem:DF addr) case,
1905 we only allow 'addr' to be [reg], [symbol_ref],
1906 [const], or [reg + const_int] pattern. */
1907 if (mode == DImode || mode == DFmode)
1908 {
1909 /* Allow [Reg + const_int] addressing mode. */
1910 if (GET_CODE (x) == PLUS)
1911 {
1912 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
1913 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict)
1914 && CONST_INT_P (XEXP (x, 1)))
1915 return true;
1916
1917 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
1918 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict)
1919 && CONST_INT_P (XEXP (x, 0)))
1920 return true;
1921 }
1922
1923 /* Now check [reg], [symbol_ref], and [const]. */
1924 if (GET_CODE (x) != REG
1925 && GET_CODE (x) != SYMBOL_REF
1926 && GET_CODE (x) != CONST)
1927 return false;
1928 }
1929
1930 /* Check if 'x' is a valid address. */
1931 switch (GET_CODE (x))
1932 {
1933 case REG:
1934 /* (mem (reg A)) => [Ra] */
1935 return nds32_address_register_rtx_p (x, strict);
1936
1937 case SYMBOL_REF:
34cb34cb 1938 /* (mem (symbol_ref A)) => [symbol_ref] */
1939 /* If -mcmodel=large, the 'symbol_ref' is not a valid address
1940 during or after LRA/reload phase. */
1941 if (TARGET_CMODEL_LARGE
eae21ed1 1942 && (reload_completed
1943 || reload_in_progress
1944 || lra_in_progress))
1945 return false;
34cb34cb 1946 /* If -mcmodel=medium and the symbol references to rodata section,
1947 the 'symbol_ref' is not a valid address during or after
1948 LRA/reload phase. */
1949 if (TARGET_CMODEL_MEDIUM
1950 && NDS32_SYMBOL_REF_RODATA_P (x)
eae21ed1 1951 && (reload_completed
1952 || reload_in_progress
1953 || lra_in_progress))
1954 return false;
1955
34cb34cb 1956 return true;
1957
1958 case CONST:
eae21ed1 1959 /* (mem (const (...)))
1960 => [ + const_addr ], where const_addr = symbol_ref + const_int */
1961 if (GET_CODE (XEXP (x, 0)) == PLUS)
1962 {
1963 rtx plus_op = XEXP (x, 0);
1964
1965 rtx op0 = XEXP (plus_op, 0);
1966 rtx op1 = XEXP (plus_op, 1);
1967
1968 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
34cb34cb 1969 {
1970 /* Now we see the [ + const_addr ] pattern, but we need
1971 some further checking. */
1972 /* If -mcmodel=large, the 'const_addr' is not a valid address
1973 during or after LRA/reload phase. */
1974 if (TARGET_CMODEL_LARGE
1975 && (reload_completed
1976 || reload_in_progress
1977 || lra_in_progress))
1978 return false;
1979 /* If -mcmodel=medium and the symbol references to rodata section,
1980 the 'const_addr' is not a valid address during or after
1981 LRA/reload phase. */
1982 if (TARGET_CMODEL_MEDIUM
1983 && NDS32_SYMBOL_REF_RODATA_P (op0)
1984 && (reload_completed
1985 || reload_in_progress
1986 || lra_in_progress))
1987 return false;
1988
1989 /* At this point we can make sure 'const_addr' is a
1990 valid address. */
1991 return true;
1992 }
eae21ed1 1993 }
1994
1995 return false;
1996
1997 case POST_MODIFY:
1998 /* (mem (post_modify (reg) (plus (reg) (reg))))
1999 => [Ra], Rb */
2000 /* (mem (post_modify (reg) (plus (reg) (const_int))))
2001 => [Ra], const_int */
2002 if (GET_CODE (XEXP (x, 0)) == REG
2003 && GET_CODE (XEXP (x, 1)) == PLUS)
2004 {
2005 rtx plus_op = XEXP (x, 1);
2006
2007 rtx op0 = XEXP (plus_op, 0);
2008 rtx op1 = XEXP (plus_op, 1);
2009
2010 if (nds32_address_register_rtx_p (op0, strict)
2011 && nds32_legitimate_index_p (mode, op1, strict))
2012 return true;
2013 else
2014 return false;
2015 }
2016
2017 return false;
2018
2019 case POST_INC:
2020 case POST_DEC:
2021 /* (mem (post_inc reg)) => [Ra], 1/2/4 */
2022 /* (mem (post_dec reg)) => [Ra], -1/-2/-4 */
2023 /* The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
2024 We only need to deal with register Ra. */
2025 if (nds32_address_register_rtx_p (XEXP (x, 0), strict))
2026 return true;
2027 else
2028 return false;
2029
2030 case PLUS:
2031 /* (mem (plus reg const_int))
2032 => [Ra + imm] */
2033 /* (mem (plus reg reg))
2034 => [Ra + Rb] */
2035 /* (mem (plus (mult reg const_int) reg))
2036 => [Ra + Rb << sv] */
2037 if (nds32_address_register_rtx_p (XEXP (x, 0), strict)
2038 && nds32_legitimate_index_p (mode, XEXP (x, 1), strict))
2039 return true;
2040 else if (nds32_address_register_rtx_p (XEXP (x, 1), strict)
2041 && nds32_legitimate_index_p (mode, XEXP (x, 0), strict))
2042 return true;
2043 else
2044 return false;
2045
2046 case LO_SUM:
ec2d68da 2047 /* (mem (lo_sum (reg) (symbol_ref))) */
2048 /* (mem (lo_sum (reg) (const))) */
2049 gcc_assert (REG_P (XEXP (x, 0)));
2050 if (GET_CODE (XEXP (x, 1)) == SYMBOL_REF
2051 || GET_CODE (XEXP (x, 1)) == CONST)
2052 return nds32_legitimate_address_p (mode, XEXP (x, 1), strict);
2053 else
2054 return false;
eae21ed1 2055
2056 default:
2057 return false;
2058 }
2059}
2060
2061\f
2062/* Describing Relative Costs of Operations. */
2063
7a878515 2064static int
3754d046 2065nds32_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
7a878515 2066 reg_class_t from,
2067 reg_class_t to)
eae21ed1 2068{
2069 if (from == HIGH_REGS || to == HIGH_REGS)
2070 return 6;
2071
2072 return 2;
2073}
2074
7a878515 2075static int
3754d046 2076nds32_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
7a878515 2077 reg_class_t rclass ATTRIBUTE_UNUSED,
2078 bool in ATTRIBUTE_UNUSED)
eae21ed1 2079{
2080 return 8;
2081}
2082
2083/* This target hook describes the relative costs of RTL expressions.
2084 Return 'true' when all subexpressions of x have been processed.
2085 Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
2086 Refer to gcc/rtlanal.c for more information. */
2087static bool
2088nds32_rtx_costs (rtx x,
5ae4887d 2089 machine_mode mode,
eae21ed1 2090 int outer_code,
469e4c9b 2091 int opno,
eae21ed1 2092 int *total,
2093 bool speed)
2094{
5ae4887d 2095 return nds32_rtx_costs_impl (x, mode, outer_code, opno, total, speed);
eae21ed1 2096}
2097
7a878515 2098static int
2099nds32_address_cost (rtx address,
3754d046 2100 machine_mode mode,
7a878515 2101 addr_space_t as,
2102 bool speed)
eae21ed1 2103{
469e4c9b 2104 return nds32_address_cost_impl (address, mode, as, speed);
eae21ed1 2105}
2106
8e4f6b00 2107\f
2108/* Dividing the Output into Sections (Texts, Data, . . . ). */
2109
2110/* If references to a symbol or a constant must be treated differently
2111 depending on something about the variable or function named by the symbol
2112 (such as what section it is in), we use this hook to store flags
2113 in symbol_ref rtx. */
2114static void
2115nds32_encode_section_info (tree decl, rtx rtl, int new_decl_p)
2116{
2117 default_encode_section_info (decl, rtl, new_decl_p);
2118
2119 /* For the memory rtx, if it references to rodata section, we can store
2120 NDS32_SYMBOL_FLAG_RODATA flag into symbol_ref rtx so that the
2121 nds32_legitimate_address_p() can determine how to treat such symbol_ref
2122 based on -mcmodel=X and this information. */
2123 if (MEM_P (rtl) && MEM_READONLY_P (rtl))
2124 {
2125 rtx addr = XEXP (rtl, 0);
2126
2127 if (GET_CODE (addr) == SYMBOL_REF)
2128 {
2129 /* For (mem (symbol_ref X)) case. */
2130 SYMBOL_REF_FLAGS (addr) |= NDS32_SYMBOL_FLAG_RODATA;
2131 }
2132 else if (GET_CODE (addr) == CONST
2133 && GET_CODE (XEXP (addr, 0)) == PLUS)
2134 {
2135 /* For (mem (const (plus (symbol_ref X) (const_int N)))) case. */
2136 rtx plus_op = XEXP (addr, 0);
2137 rtx op0 = XEXP (plus_op, 0);
2138 rtx op1 = XEXP (plus_op, 1);
2139
2140 if (GET_CODE (op0) == SYMBOL_REF && CONST_INT_P (op1))
2141 SYMBOL_REF_FLAGS (op0) |= NDS32_SYMBOL_FLAG_RODATA;
2142 }
2143 }
2144}
2145
eae21ed1 2146\f
2147/* Defining the Output Assembler Language. */
2148
2149/* -- The Overall Framework of an Assembler File. */
2150
2151static void
2152nds32_asm_file_start (void)
2153{
eae21ed1 2154 default_file_start ();
2155
2156 /* Tell assembler which ABI we are using. */
2157 fprintf (asm_out_file, "\t! ABI version\n");
2158 fprintf (asm_out_file, "\t.abi_2\n");
2159
2160 /* Tell assembler that this asm code is generated by compiler. */
2161 fprintf (asm_out_file, "\t! This asm file is generated by compiler\n");
2162 fprintf (asm_out_file, "\t.flag\tverbatim\n");
2163 /* Give assembler the size of each vector for interrupt handler. */
2164 fprintf (asm_out_file, "\t! This vector size directive is required "
2165 "for checking inconsistency on interrupt handler\n");
2166 fprintf (asm_out_file, "\t.vec_size\t%d\n", nds32_isr_vector_size);
2167
eae21ed1 2168 fprintf (asm_out_file, "\t! ------------------------------------\n");
2169
2170 if (TARGET_ISA_V2)
2171 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V2");
2172 if (TARGET_ISA_V3)
2173 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3");
2174 if (TARGET_ISA_V3M)
2175 fprintf (asm_out_file, "\t! ISA family\t\t: %s\n", "V3M");
2176
abc509c0 2177 if (TARGET_CMODEL_SMALL)
2178 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "SMALL");
2179 if (TARGET_CMODEL_MEDIUM)
2180 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "MEDIUM");
2181 if (TARGET_CMODEL_LARGE)
2182 fprintf (asm_out_file, "\t! Code model\t\t: %s\n", "LARGE");
2183
eae21ed1 2184 fprintf (asm_out_file, "\t! Endian setting\t: %s\n",
2185 ((TARGET_BIG_ENDIAN) ? "big-endian"
2186 : "little-endian"));
2187
2188 fprintf (asm_out_file, "\t! ------------------------------------\n");
2189
2190 fprintf (asm_out_file, "\t! Use conditional move\t\t: %s\n",
2191 ((TARGET_CMOV) ? "Yes"
2192 : "No"));
2193 fprintf (asm_out_file, "\t! Use performance extension\t: %s\n",
2194 ((TARGET_PERF_EXT) ? "Yes"
2195 : "No"));
2196
2197 fprintf (asm_out_file, "\t! ------------------------------------\n");
2198
2199 fprintf (asm_out_file, "\t! V3PUSH instructions\t: %s\n",
2200 ((TARGET_V3PUSH) ? "Yes"
2201 : "No"));
2202 fprintf (asm_out_file, "\t! 16-bit instructions\t: %s\n",
2203 ((TARGET_16_BIT) ? "Yes"
2204 : "No"));
eae21ed1 2205 fprintf (asm_out_file, "\t! Reduced registers set\t: %s\n",
2206 ((TARGET_REDUCED_REGS) ? "Yes"
2207 : "No"));
2208
2209 fprintf (asm_out_file, "\t! ------------------------------------\n");
2210
2211 if (optimize_size)
2212 fprintf (asm_out_file, "\t! Optimization level\t: -Os\n");
2213 else
2214 fprintf (asm_out_file, "\t! Optimization level\t: -O%d\n", optimize);
2215
2216 fprintf (asm_out_file, "\t! ------------------------------------\n");
2217
2218 fprintf (asm_out_file, "\t! Cache block size\t: %d\n",
2219 nds32_cache_block_size);
2220
2221 fprintf (asm_out_file, "\t! ------------------------------------\n");
2222
fd1dccd6 2223 nds32_asm_file_start_for_isr ();
eae21ed1 2224}
2225
2226static void
2227nds32_asm_file_end (void)
2228{
fd1dccd6 2229 nds32_asm_file_end_for_isr ();
eae21ed1 2230
2231 fprintf (asm_out_file, "\t! ------------------------------------\n");
2232}
2233
2234/* -- Output and Generation of Labels. */
2235
2236static void
2237nds32_asm_globalize_label (FILE *stream, const char *name)
2238{
2239 fputs ("\t.global\t", stream);
2240 assemble_name (stream, name);
2241 fputs ("\n", stream);
2242}
2243
2244/* -- Output of Assembler Instructions. */
2245
2246static void
2247nds32_print_operand (FILE *stream, rtx x, int code)
2248{
2249 int op_value;
2250
2251 switch (code)
2252 {
2253 case 0 :
2254 /* Do nothing special. */
2255 break;
2256
2257 case 'V':
2258 /* 'x' is supposed to be CONST_INT, get the value. */
2259 gcc_assert (CONST_INT_P (x));
2260 op_value = INTVAL (x);
2261
2262 /* According to the Andes architecture,
2263 the system/user register index range is 0 ~ 1023.
2264 In order to avoid conflict between user-specified-integer value
2265 and enum-specified-register value,
2266 the 'enum nds32_intrinsic_registers' value
2267 in nds32_intrinsic.h starts from 1024. */
2268 if (op_value < 1024 && op_value >= 0)
2269 {
2270 /* If user gives integer value directly (0~1023),
2271 we just print out the value. */
2272 fprintf (stream, "%d", op_value);
2273 }
2274 else if (op_value < 0
2275 || op_value >= ((int) ARRAY_SIZE (nds32_intrinsic_register_names)
2276 + 1024))
2277 {
2278 /* The enum index value for array size is out of range. */
2279 error ("intrinsic register index is out of range");
2280 }
2281 else
2282 {
2283 /* If user applies normal way with __NDS32_REG_XXX__ enum data,
2284 we can print out register name. Remember to substract 1024. */
2285 fprintf (stream, "%s",
2286 nds32_intrinsic_register_names[op_value - 1024]);
2287 }
2288
2289 /* No need to handle following process, so return immediately. */
2290 return;
2291
2292 default :
2293 /* Unknown flag. */
2294 output_operand_lossage ("invalid operand output code");
2295 break;
2296 }
2297
2298 switch (GET_CODE (x))
2299 {
2300 case LABEL_REF:
2301 case SYMBOL_REF:
2302 output_addr_const (stream, x);
2303 break;
2304
2305 case REG:
2306 /* Forbid using static chain register ($r16)
2307 on reduced-set registers configuration. */
2308 if (TARGET_REDUCED_REGS
2309 && REGNO (x) == STATIC_CHAIN_REGNUM)
2310 sorry ("a nested function is not supported for reduced registers");
2311
2312 /* Normal cases, print out register name. */
2313 fputs (reg_names[REGNO (x)], stream);
2314 break;
2315
2316 case MEM:
3c047fe9 2317 output_address (GET_MODE (x), XEXP (x, 0));
eae21ed1 2318 break;
2319
2320 case CODE_LABEL:
2321 case CONST_INT:
2322 case CONST:
2323 output_addr_const (stream, x);
2324 break;
2325
2326 default:
2327 /* Generally, output_addr_const () is able to handle most cases.
2328 We want to see what CODE could appear,
2329 so we use gcc_unreachable() to stop it. */
2330 debug_rtx (x);
2331 gcc_unreachable ();
2332 break;
2333 }
2334}
2335
2336static void
3c047fe9 2337nds32_print_operand_address (FILE *stream, machine_mode /*mode*/, rtx x)
eae21ed1 2338{
2339 rtx op0, op1;
2340
2341 switch (GET_CODE (x))
2342 {
2343 case SYMBOL_REF:
2344 case CONST:
2345 /* [ + symbol_ref] */
2346 /* [ + const_addr], where const_addr = symbol_ref + const_int */
2347 fputs ("[ + ", stream);
2348 output_addr_const (stream, x);
2349 fputs ("]", stream);
2350 break;
2351
2352 case REG:
2353 /* Forbid using static chain register ($r16)
2354 on reduced-set registers configuration. */
2355 if (TARGET_REDUCED_REGS
2356 && REGNO (x) == STATIC_CHAIN_REGNUM)
2357 sorry ("a nested function is not supported for reduced registers");
2358
2359 /* [Ra] */
2360 fprintf (stream, "[%s]", reg_names[REGNO (x)]);
2361 break;
2362
2363 case PLUS:
2364 op0 = XEXP (x, 0);
2365 op1 = XEXP (x, 1);
2366
2367 /* Checking op0, forbid using static chain register ($r16)
2368 on reduced-set registers configuration. */
2369 if (TARGET_REDUCED_REGS
2370 && REG_P (op0)
2371 && REGNO (op0) == STATIC_CHAIN_REGNUM)
2372 sorry ("a nested function is not supported for reduced registers");
2373 /* Checking op1, forbid using static chain register ($r16)
2374 on reduced-set registers configuration. */
2375 if (TARGET_REDUCED_REGS
2376 && REG_P (op1)
2377 && REGNO (op1) == STATIC_CHAIN_REGNUM)
2378 sorry ("a nested function is not supported for reduced registers");
2379
2380 if (REG_P (op0) && CONST_INT_P (op1))
2381 {
2382 /* [Ra + imm] */
2383 fprintf (stream, "[%s + (%d)]",
2384 reg_names[REGNO (op0)], (int)INTVAL (op1));
2385 }
2386 else if (REG_P (op0) && REG_P (op1))
2387 {
2388 /* [Ra + Rb] */
2389 fprintf (stream, "[%s + %s]",
2390 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
2391 }
2392 else if (GET_CODE (op0) == MULT && REG_P (op1))
2393 {
2394 /* [Ra + Rb << sv]
2395 From observation, the pattern looks like:
2396 (plus:SI (mult:SI (reg:SI 58)
2397 (const_int 4 [0x4]))
2398 (reg/f:SI 57)) */
2399 int sv;
2400
2401 /* We need to set sv to output shift value. */
2402 if (INTVAL (XEXP (op0, 1)) == 1)
2403 sv = 0;
2404 else if (INTVAL (XEXP (op0, 1)) == 2)
2405 sv = 1;
2406 else if (INTVAL (XEXP (op0, 1)) == 4)
2407 sv = 2;
2408 else
2409 gcc_unreachable ();
2410
2411 fprintf (stream, "[%s + %s << %d]",
2412 reg_names[REGNO (op1)],
2413 reg_names[REGNO (XEXP (op0, 0))],
2414 sv);
2415 }
2416 else
2417 {
2418 /* The control flow is not supposed to be here. */
2419 debug_rtx (x);
2420 gcc_unreachable ();
2421 }
2422
2423 break;
2424
2425 case POST_MODIFY:
2426 /* (post_modify (regA) (plus (regA) (regB)))
2427 (post_modify (regA) (plus (regA) (const_int)))
2428 We would like to extract
2429 regA and regB (or const_int) from plus rtx. */
2430 op0 = XEXP (XEXP (x, 1), 0);
2431 op1 = XEXP (XEXP (x, 1), 1);
2432
2433 /* Checking op0, forbid using static chain register ($r16)
2434 on reduced-set registers configuration. */
2435 if (TARGET_REDUCED_REGS
2436 && REG_P (op0)
2437 && REGNO (op0) == STATIC_CHAIN_REGNUM)
2438 sorry ("a nested function is not supported for reduced registers");
2439 /* Checking op1, forbid using static chain register ($r16)
2440 on reduced-set registers configuration. */
2441 if (TARGET_REDUCED_REGS
2442 && REG_P (op1)
2443 && REGNO (op1) == STATIC_CHAIN_REGNUM)
2444 sorry ("a nested function is not supported for reduced registers");
2445
2446 if (REG_P (op0) && REG_P (op1))
2447 {
2448 /* [Ra], Rb */
2449 fprintf (stream, "[%s], %s",
2450 reg_names[REGNO (op0)], reg_names[REGNO (op1)]);
2451 }
2452 else if (REG_P (op0) && CONST_INT_P (op1))
2453 {
2454 /* [Ra], imm */
2455 fprintf (stream, "[%s], %d",
2456 reg_names[REGNO (op0)], (int)INTVAL (op1));
2457 }
2458 else
2459 {
2460 /* The control flow is not supposed to be here. */
2461 debug_rtx (x);
2462 gcc_unreachable ();
2463 }
2464
2465 break;
2466
2467 case POST_INC:
2468 case POST_DEC:
2469 op0 = XEXP (x, 0);
2470
2471 /* Checking op0, forbid using static chain register ($r16)
2472 on reduced-set registers configuration. */
2473 if (TARGET_REDUCED_REGS
2474 && REG_P (op0)
2475 && REGNO (op0) == STATIC_CHAIN_REGNUM)
2476 sorry ("a nested function is not supported for reduced registers");
2477
2478 if (REG_P (op0))
2479 {
2480 /* "[Ra], 1/2/4" or "[Ra], -1/-2/-4"
2481 The 1/2/4 or -1/-2/-4 have been displayed in nds32.md.
2482 We only need to deal with register Ra. */
2483 fprintf (stream, "[%s]", reg_names[REGNO (op0)]);
2484 }
2485 else
2486 {
2487 /* The control flow is not supposed to be here. */
2488 debug_rtx (x);
2489 gcc_unreachable ();
2490 }
2491
2492 break;
2493
2494 default :
2495 /* Generally, output_addr_const () is able to handle most cases.
2496 We want to see what CODE could appear,
2497 so we use gcc_unreachable() to stop it. */
2498 debug_rtx (x);
2499 gcc_unreachable ();
2500 break;
2501 }
2502}
2503
2504\f
2505/* Defining target-specific uses of __attribute__. */
2506
2507/* Add some checking after merging attributes. */
2508static tree
2509nds32_merge_decl_attributes (tree olddecl, tree newdecl)
2510{
2511 tree combined_attrs;
2512
2513 /* Create combined attributes. */
2514 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
2515 DECL_ATTRIBUTES (newdecl));
2516
416cdb53 2517 /* Since newdecl is acutally a duplicate of olddecl,
eae21ed1 2518 we can take olddecl for some operations. */
2519 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2520 {
2521 /* Check isr-specific attributes conflict. */
2522 nds32_check_isr_attrs_conflict (olddecl, combined_attrs);
2523 }
2524
2525 return combined_attrs;
2526}
2527
2528/* Add some checking when inserting attributes. */
2529static void
2530nds32_insert_attributes (tree decl, tree *attributes)
2531{
2532 /* For function declaration, we need to check isr-specific attributes:
2533 1. Call nds32_check_isr_attrs_conflict() to check any conflict.
2534 2. Check valid integer value for interrupt/exception.
2535 3. Check valid integer value for reset.
2536 4. Check valid function for nmi/warm. */
2537 if (TREE_CODE (decl) == FUNCTION_DECL)
2538 {
2539 tree func_attrs;
2540 tree intr, excp, reset;
2541
2542 /* Pick up function attributes. */
2543 func_attrs = *attributes;
2544
2545 /* 1. Call nds32_check_isr_attrs_conflict() to check any conflict. */
2546 nds32_check_isr_attrs_conflict (decl, func_attrs);
2547
2548 /* Now we are starting to check valid id value
2549 for interrupt/exception/reset.
2550 Note that we ONLY check its validity here.
2551 To construct isr vector information, it is still performed
2552 by nds32_construct_isr_vectors_information(). */
2553 intr = lookup_attribute ("interrupt", func_attrs);
2554 excp = lookup_attribute ("exception", func_attrs);
2555 reset = lookup_attribute ("reset", func_attrs);
2556
2557 if (intr || excp)
2558 {
2559 /* Deal with interrupt/exception. */
2560 tree id_list;
2561 unsigned int lower_bound, upper_bound;
2562
2563 /* The way to handle interrupt or exception is the same,
2564 we just need to take care of actual vector number.
2565 For interrupt(0..63), the actual vector number is (9..72).
2566 For exception(1..8), the actual vector number is (1..8). */
2567 lower_bound = (intr) ? (0) : (1);
2568 upper_bound = (intr) ? (63) : (8);
2569
2570 /* Prepare id list so that we can traverse id value. */
2571 id_list = (intr) ? (TREE_VALUE (intr)) : (TREE_VALUE (excp));
2572
2573 /* 2. Check valid integer value for interrupt/exception. */
2574 while (id_list)
2575 {
2576 tree id;
2577
2578 /* Pick up each vector id value. */
2579 id = TREE_VALUE (id_list);
2580 /* Issue error if it is not a valid integer value. */
2581 if (TREE_CODE (id) != INTEGER_CST
3ae25430 2582 || wi::ltu_p (id, lower_bound)
2583 || wi::gtu_p (id, upper_bound))
eae21ed1 2584 error ("invalid id value for interrupt/exception attribute");
2585
2586 /* Advance to next id. */
2587 id_list = TREE_CHAIN (id_list);
2588 }
2589 }
2590 else if (reset)
2591 {
2592 /* Deal with reset. */
2593 tree id_list;
2594 tree id;
2595 tree nmi, warm;
2596 unsigned int lower_bound;
2597 unsigned int upper_bound;
2598
2599 /* Prepare id_list and identify id value so that
2600 we can check if total number of vectors is valid. */
2601 id_list = TREE_VALUE (reset);
2602 id = TREE_VALUE (id_list);
2603
2604 /* The maximum numbers for user's interrupt is 64. */
2605 lower_bound = 0;
2606 upper_bound = 64;
2607
2608 /* 3. Check valid integer value for reset. */
2609 if (TREE_CODE (id) != INTEGER_CST
3ae25430 2610 || wi::ltu_p (id, lower_bound)
2611 || wi::gtu_p (id, upper_bound))
eae21ed1 2612 error ("invalid id value for reset attribute");
2613
2614 /* 4. Check valid function for nmi/warm. */
2615 nmi = lookup_attribute ("nmi", func_attrs);
2616 warm = lookup_attribute ("warm", func_attrs);
2617
2618 if (nmi != NULL_TREE)
2619 {
2620 tree nmi_func_list;
2621 tree nmi_func;
2622
2623 nmi_func_list = TREE_VALUE (nmi);
2624 nmi_func = TREE_VALUE (nmi_func_list);
2625
2626 /* Issue error if it is not a valid nmi function. */
2627 if (TREE_CODE (nmi_func) != IDENTIFIER_NODE)
2628 error ("invalid nmi function for reset attribute");
2629 }
2630
2631 if (warm != NULL_TREE)
2632 {
2633 tree warm_func_list;
2634 tree warm_func;
2635
2636 warm_func_list = TREE_VALUE (warm);
2637 warm_func = TREE_VALUE (warm_func_list);
2638
2639 /* Issue error if it is not a valid warm function. */
2640 if (TREE_CODE (warm_func) != IDENTIFIER_NODE)
2641 error ("invalid warm function for reset attribute");
2642 }
2643 }
2644 else
2645 {
2646 /* No interrupt, exception, or reset attribute is set. */
2647 return;
2648 }
2649 }
2650}
2651
2652static bool
2653nds32_option_pragma_parse (tree args ATTRIBUTE_UNUSED,
2654 tree pop_target ATTRIBUTE_UNUSED)
2655{
2656 /* Currently, we do not parse any pragma target by ourself,
2657 so just simply return false. */
2658 return false;
2659}
2660
2661static void
2662nds32_option_override (void)
2663{
2664 /* After all the command options have been parsed,
2665 we shall deal with some flags for changing compiler settings. */
2666
2667 /* At first, we check if we have to strictly
2668 set some flags based on ISA family. */
2669 if (TARGET_ISA_V2)
2670 {
2671 /* Under V2 ISA, we need to strictly disable TARGET_V3PUSH. */
2672 target_flags &= ~MASK_V3PUSH;
2673 }
2674 if (TARGET_ISA_V3)
2675 {
2676 /* Under V3 ISA, currently nothing should be strictly set. */
2677 }
2678 if (TARGET_ISA_V3M)
2679 {
2680 /* Under V3M ISA, we need to strictly enable TARGET_REDUCED_REGS. */
2681 target_flags |= MASK_REDUCED_REGS;
2682 /* Under V3M ISA, we need to strictly disable TARGET_PERF_EXT. */
2683 target_flags &= ~MASK_PERF_EXT;
2684 }
2685
2686 /* See if we are using reduced-set registers:
2687 $r0~$r5, $r6~$r10, $r15, $r28, $r29, $r30, $r31
2688 If so, we must forbid using $r11~$r14, $r16~$r27. */
2689 if (TARGET_REDUCED_REGS)
2690 {
2691 int r;
2692
2693 /* Prevent register allocator from
2694 choosing it as doing register allocation. */
2695 for (r = 11; r <= 14; r++)
2696 fixed_regs[r] = call_used_regs[r] = 1;
2697 for (r = 16; r <= 27; r++)
2698 fixed_regs[r] = call_used_regs[r] = 1;
2699 }
2700
eae21ed1 2701 if (!TARGET_16_BIT)
2702 {
2703 /* Under no 16 bit ISA, we need to strictly disable TARGET_V3PUSH. */
2704 target_flags &= ~MASK_V3PUSH;
2705 }
2706
2707 /* Currently, we don't support PIC code generation yet. */
2708 if (flag_pic)
8e2255a9 2709 sorry ("position-independent code not supported");
eae21ed1 2710}
2711
2712\f
2713/* Miscellaneous Parameters. */
2714
2715static void
2716nds32_init_builtins (void)
2717{
2d272c9f 2718 nds32_init_builtins_impl ();
eae21ed1 2719}
2720
2721static rtx
2722nds32_expand_builtin (tree exp,
2723 rtx target,
2d272c9f 2724 rtx subtarget,
3754d046 2725 machine_mode mode,
2d272c9f 2726 int ignore)
eae21ed1 2727{
2d272c9f 2728 return nds32_expand_builtin_impl (exp, target, subtarget, mode, ignore);
eae21ed1 2729}
2730
2731
2732/* ------------------------------------------------------------------------ */
2733
2734/* PART 4: Implemet extern function definitions,
2735 the prototype is in nds32-protos.h. */
2736\f
2737/* Defining Data Structures for Per-function Information. */
2738
2739void
2740nds32_init_expanders (void)
2741{
2742 /* Arrange to initialize and mark the machine per-function status. */
2743 init_machine_status = nds32_init_machine_status;
2744}
2745
2746\f
2747/* Register Usage. */
2748
2749/* -- How Values Fit in Registers. */
2750
2751int
2752nds32_hard_regno_nregs (int regno ATTRIBUTE_UNUSED,
3754d046 2753 machine_mode mode)
eae21ed1 2754{
2755 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
2756}
2757
2758int
3754d046 2759nds32_hard_regno_mode_ok (int regno, machine_mode mode)
eae21ed1 2760{
2761 /* Restrict double-word quantities to even register pairs. */
2762 if (HARD_REGNO_NREGS (regno, mode) == 1
2763 || !((regno) & 1))
2764 return 1;
2765
2766 return 0;
2767}
2768
2769\f
2770/* Register Classes. */
2771
2772enum reg_class
2773nds32_regno_reg_class (int regno)
2774{
2775 /* Refer to nds32.h for more register class details. */
2776
2777 if (regno >= 0 && regno <= 7)
2778 return LOW_REGS;
2779 else if (regno >= 8 && regno <= 11)
2780 return MIDDLE_REGS;
2781 else if (regno >= 12 && regno <= 14)
2782 return HIGH_REGS;
2783 else if (regno == 15)
2784 return R15_TA_REG;
2785 else if (regno >= 16 && regno <= 19)
2786 return MIDDLE_REGS;
2787 else if (regno >= 20 && regno <= 31)
2788 return HIGH_REGS;
2789 else if (regno == 32 || regno == 33)
2790 return FRAME_REGS;
2791 else
2792 return NO_REGS;
2793}
2794
2795\f
2796/* Stack Layout and Calling Conventions. */
2797
2798/* -- Basic Stack Layout. */
2799
2800rtx
2801nds32_return_addr_rtx (int count,
2802 rtx frameaddr ATTRIBUTE_UNUSED)
2803{
2804 /* There is no way to determine the return address
2805 if frameaddr is the frame that has 'count' steps
2806 up from current frame. */
2807 if (count != 0)
2808 return NULL_RTX;
2809
2810 /* If count == 0, it means we are at current frame,
2811 the return address is $r30 ($lp). */
2812 return get_hard_reg_initial_val (Pmode, LP_REGNUM);
2813}
2814
2815/* -- Eliminating Frame Pointer and Arg Pointer. */
2816
2817HOST_WIDE_INT
2818nds32_initial_elimination_offset (unsigned int from_reg, unsigned int to_reg)
2819{
2820 HOST_WIDE_INT offset;
2821
2822 /* Compute and setup stack frame size.
2823 The result will be in cfun->machine. */
2824 nds32_compute_stack_frame ();
2825
2826 /* Remember to consider
2827 cfun->machine->callee_saved_area_padding_bytes
2828 when calculating offset. */
2829 if (from_reg == ARG_POINTER_REGNUM && to_reg == STACK_POINTER_REGNUM)
2830 {
2831 offset = (cfun->machine->fp_size
2832 + cfun->machine->gp_size
2833 + cfun->machine->lp_size
afddacc6 2834 + cfun->machine->callee_saved_gpr_regs_size
2835 + cfun->machine->callee_saved_area_gpr_padding_bytes
eae21ed1 2836 + cfun->machine->local_size
2837 + cfun->machine->out_args_size);
2838 }
2839 else if (from_reg == ARG_POINTER_REGNUM
2840 && to_reg == HARD_FRAME_POINTER_REGNUM)
2841 {
2842 offset = 0;
2843 }
2844 else if (from_reg == FRAME_POINTER_REGNUM
2845 && to_reg == STACK_POINTER_REGNUM)
2846 {
2847 offset = (cfun->machine->local_size + cfun->machine->out_args_size);
2848 }
2849 else if (from_reg == FRAME_POINTER_REGNUM
2850 && to_reg == HARD_FRAME_POINTER_REGNUM)
2851 {
2852 offset = (-1) * (cfun->machine->fp_size
2853 + cfun->machine->gp_size
2854 + cfun->machine->lp_size
afddacc6 2855 + cfun->machine->callee_saved_gpr_regs_size
2856 + cfun->machine->callee_saved_area_gpr_padding_bytes);
eae21ed1 2857 }
2858 else
2859 {
2860 gcc_unreachable ();
2861 }
2862
2863 return offset;
2864}
2865
2866/* -- Passing Arguments in Registers. */
2867
2868void
2869nds32_init_cumulative_args (CUMULATIVE_ARGS *cum,
2870 tree fntype ATTRIBUTE_UNUSED,
2871 rtx libname ATTRIBUTE_UNUSED,
2872 tree fndecl ATTRIBUTE_UNUSED,
2873 int n_named_args ATTRIBUTE_UNUSED)
2874{
2875 /* Initial available registers
2876 (in offset, corresponding to NDS32_GPR_ARG_FIRST_REGNUM)
2877 for passing arguments. */
397b728f 2878 cum->gpr_offset = 0;
eae21ed1 2879}
2880
2881/* -- Function Entry and Exit. */
2882
2883/* Function for normal multiple push prologue. */
2884void
2885nds32_expand_prologue (void)
2886{
2887 int fp_adjust;
2888 int sp_adjust;
2889 int en4_const;
2890
2891 rtx Rb, Re;
eae21ed1 2892 rtx fp_adjust_insn, sp_adjust_insn;
2893
eae21ed1 2894 /* Compute and setup stack frame size.
2895 The result will be in cfun->machine. */
2896 nds32_compute_stack_frame ();
2897
4ef80444 2898 /* If this is a variadic function, first we need to push argument
2899 registers that hold the unnamed argument value. */
2900 if (cfun->machine->va_args_size != 0)
2901 {
2902 Rb = gen_rtx_REG (SImode, cfun->machine->va_args_first_regno);
2903 Re = gen_rtx_REG (SImode, cfun->machine->va_args_last_regno);
2904 /* No need to push $fp, $gp, or $lp, so use GEN_INT(0). */
aa51c130 2905 nds32_emit_stack_push_multiple (Rb, Re, GEN_INT (0), true);
4ef80444 2906
2907 /* We may also need to adjust stack pointer for padding bytes
2908 because varargs may cause $sp not 8-byte aligned. */
2909 if (cfun->machine->va_args_area_padding_bytes)
2910 {
2911 /* Generate sp adjustment instruction. */
2912 sp_adjust = cfun->machine->va_args_area_padding_bytes;
2913 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
2914 stack_pointer_rtx,
2915 GEN_INT (-1 * sp_adjust));
2916
2917 /* Emit rtx into instructions list and receive INSN rtx form. */
2918 sp_adjust_insn = emit_insn (sp_adjust_insn);
2919
2920 /* The insn rtx 'sp_adjust_insn' will change frame layout.
2921 We need to use RTX_FRAME_RELATED_P so that GCC is able to
2922 generate CFI (Call Frame Information) stuff. */
2923 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
2924 }
2925 }
2926
eae21ed1 2927 /* If the function is 'naked',
2928 we do not have to generate prologue code fragment. */
2929 if (cfun->machine->naked_p)
2930 return;
2931
2932 /* Get callee_first_regno and callee_last_regno. */
afddacc6 2933 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_first_gpr_regno);
2934 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_last_gpr_regno);
eae21ed1 2935
cd3fd5ae 2936 /* nds32_emit_stack_push_multiple(first_regno, last_regno),
eae21ed1 2937 the pattern 'stack_push_multiple' is implemented in nds32.md.
2938 For En4 field, we have to calculate its constant value.
2939 Refer to Andes ISA for more information. */
2940 en4_const = 0;
2941 if (cfun->machine->fp_size)
2942 en4_const += 8;
2943 if (cfun->machine->gp_size)
2944 en4_const += 4;
2945 if (cfun->machine->lp_size)
2946 en4_const += 2;
2947
2948 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
2949 to be saved, we don't have to create multiple push instruction.
2950 Otherwise, a multiple push instruction is needed. */
2951 if (!(REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM && en4_const == 0))
2952 {
2953 /* Create multiple push instruction rtx. */
aa51c130 2954 nds32_emit_stack_push_multiple (Rb, Re, GEN_INT (en4_const), false);
eae21ed1 2955 }
2956
2957 /* Check frame_pointer_needed to see
2958 if we shall emit fp adjustment instruction. */
2959 if (frame_pointer_needed)
2960 {
2961 /* adjust $fp = $sp + ($fp size) + ($gp size) + ($lp size)
2962 + (4 * callee-saved-registers)
2963 Note: No need to adjust
2964 cfun->machine->callee_saved_area_padding_bytes,
2965 because, at this point, stack pointer is just
2966 at the position after push instruction. */
2967 fp_adjust = cfun->machine->fp_size
2968 + cfun->machine->gp_size
2969 + cfun->machine->lp_size
afddacc6 2970 + cfun->machine->callee_saved_gpr_regs_size;
eae21ed1 2971 fp_adjust_insn = gen_addsi3 (hard_frame_pointer_rtx,
2972 stack_pointer_rtx,
2973 GEN_INT (fp_adjust));
2974 /* Emit rtx into instructions list and receive INSN rtx form. */
2975 fp_adjust_insn = emit_insn (fp_adjust_insn);
09b701a4 2976
2977 /* The insn rtx 'fp_adjust_insn' will change frame layout. */
2978 RTX_FRAME_RELATED_P (fp_adjust_insn) = 1;
eae21ed1 2979 }
2980
2981 /* Adjust $sp = $sp - local_size - out_args_size
2982 - callee_saved_area_padding_bytes. */
2983 sp_adjust = cfun->machine->local_size
2984 + cfun->machine->out_args_size
afddacc6 2985 + cfun->machine->callee_saved_area_gpr_padding_bytes;
eae21ed1 2986 /* sp_adjust value may be out of range of the addi instruction,
2987 create alternative add behavior with TA_REGNUM if necessary,
2988 using NEGATIVE value to tell that we are decreasing address. */
2989 sp_adjust = nds32_force_addi_stack_int ( (-1) * sp_adjust);
2990 if (sp_adjust)
2991 {
2992 /* Generate sp adjustment instruction if and only if sp_adjust != 0. */
2993 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
2994 stack_pointer_rtx,
2995 GEN_INT (-1 * sp_adjust));
2996 /* Emit rtx into instructions list and receive INSN rtx form. */
2997 sp_adjust_insn = emit_insn (sp_adjust_insn);
2998
2999 /* The insn rtx 'sp_adjust_insn' will change frame layout.
3000 We need to use RTX_FRAME_RELATED_P so that GCC is able to
3001 generate CFI (Call Frame Information) stuff. */
3002 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
3003 }
3004
3005 /* Prevent the instruction scheduler from
3006 moving instructions across the boundary. */
3007 emit_insn (gen_blockage ());
3008}
3009
3010/* Function for normal multiple pop epilogue. */
3011void
7ea565c0 3012nds32_expand_epilogue (bool sibcall_p)
eae21ed1 3013{
3014 int sp_adjust;
3015 int en4_const;
3016
3017 rtx Rb, Re;
eae21ed1 3018 rtx sp_adjust_insn;
3019
3020 /* Compute and setup stack frame size.
3021 The result will be in cfun->machine. */
3022 nds32_compute_stack_frame ();
3023
3024 /* Prevent the instruction scheduler from
3025 moving instructions across the boundary. */
3026 emit_insn (gen_blockage ());
3027
3028 /* If the function is 'naked', we do not have to generate
4ef80444 3029 epilogue code fragment BUT 'ret' instruction.
3030 However, if this function is also a variadic function,
3031 we need to create adjust stack pointer before 'ret' instruction. */
eae21ed1 3032 if (cfun->machine->naked_p)
3033 {
4ef80444 3034 /* If this is a variadic function, we do not have to restore argument
3035 registers but need to adjust stack pointer back to previous stack
3036 frame location before return. */
3037 if (cfun->machine->va_args_size != 0)
3038 {
3039 /* Generate sp adjustment instruction.
3040 We need to consider padding bytes here. */
3041 sp_adjust = cfun->machine->va_args_size
3042 + cfun->machine->va_args_area_padding_bytes;
3043 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3044 stack_pointer_rtx,
3045 GEN_INT (sp_adjust));
3046 /* Emit rtx into instructions list and receive INSN rtx form. */
3047 sp_adjust_insn = emit_insn (sp_adjust_insn);
3048
3049 /* The insn rtx 'sp_adjust_insn' will change frame layout.
3050 We need to use RTX_FRAME_RELATED_P so that GCC is able to
3051 generate CFI (Call Frame Information) stuff. */
3052 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
3053 }
3054
c2927786 3055 /* Generate return instruction by using 'return_internal' pattern.
3056 Make sure this instruction is after gen_blockage(). */
7ea565c0 3057 if (!sibcall_p)
3058 emit_jump_insn (gen_return_internal ());
eae21ed1 3059 return;
3060 }
3061
3062 if (frame_pointer_needed)
3063 {
3064 /* adjust $sp = $fp - ($fp size) - ($gp size) - ($lp size)
3065 - (4 * callee-saved-registers)
3066 Note: No need to adjust
3067 cfun->machine->callee_saved_area_padding_bytes,
3068 because we want to adjust stack pointer
3069 to the position for pop instruction. */
3070 sp_adjust = cfun->machine->fp_size
3071 + cfun->machine->gp_size
3072 + cfun->machine->lp_size
afddacc6 3073 + cfun->machine->callee_saved_gpr_regs_size;
eae21ed1 3074 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3075 hard_frame_pointer_rtx,
3076 GEN_INT (-1 * sp_adjust));
3077 /* Emit rtx into instructions list and receive INSN rtx form. */
3078 sp_adjust_insn = emit_insn (sp_adjust_insn);
4ef80444 3079
3080 /* The insn rtx 'sp_adjust_insn' will change frame layout. */
3081 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
eae21ed1 3082 }
3083 else
3084 {
3085 /* If frame pointer is NOT needed,
3086 we cannot calculate the sp adjustment from frame pointer.
3087 Instead, we calculate the adjustment by local_size,
3088 out_args_size, and callee_saved_area_padding_bytes.
3089 Notice that such sp adjustment value may be out of range,
3090 so we have to deal with it as well. */
3091
3092 /* Adjust $sp = $sp + local_size + out_args_size
3093 + callee_saved_area_padding_bytes. */
3094 sp_adjust = cfun->machine->local_size
3095 + cfun->machine->out_args_size
afddacc6 3096 + cfun->machine->callee_saved_area_gpr_padding_bytes;
eae21ed1 3097 /* sp_adjust value may be out of range of the addi instruction,
3098 create alternative add behavior with TA_REGNUM if necessary,
3099 using POSITIVE value to tell that we are increasing address. */
3100 sp_adjust = nds32_force_addi_stack_int (sp_adjust);
3101 if (sp_adjust)
3102 {
3103 /* Generate sp adjustment instruction
3104 if and only if sp_adjust != 0. */
3105 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3106 stack_pointer_rtx,
3107 GEN_INT (sp_adjust));
3108 /* Emit rtx into instructions list and receive INSN rtx form. */
3109 sp_adjust_insn = emit_insn (sp_adjust_insn);
4ef80444 3110
3111 /* The insn rtx 'sp_adjust_insn' will change frame layout. */
3112 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
eae21ed1 3113 }
3114 }
3115
3116 /* Get callee_first_regno and callee_last_regno. */
afddacc6 3117 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_first_gpr_regno);
3118 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_last_gpr_regno);
eae21ed1 3119
cd3fd5ae 3120 /* nds32_emit_stack_pop_multiple(first_regno, last_regno),
eae21ed1 3121 the pattern 'stack_pop_multiple' is implementad in nds32.md.
3122 For En4 field, we have to calculate its constant value.
3123 Refer to Andes ISA for more information. */
3124 en4_const = 0;
3125 if (cfun->machine->fp_size)
3126 en4_const += 8;
3127 if (cfun->machine->gp_size)
3128 en4_const += 4;
3129 if (cfun->machine->lp_size)
3130 en4_const += 2;
3131
3132 /* If $fp, $gp, $lp, and all callee-save registers are NOT required
3133 to be saved, we don't have to create multiple pop instruction.
3134 Otherwise, a multiple pop instruction is needed. */
3135 if (!(REGNO (Rb) == SP_REGNUM && REGNO (Re) == SP_REGNUM && en4_const == 0))
3136 {
3137 /* Create multiple pop instruction rtx. */
cd3fd5ae 3138 nds32_emit_stack_pop_multiple (Rb, Re, GEN_INT (en4_const));
eae21ed1 3139 }
3140
4ef80444 3141 /* If this is a variadic function, we do not have to restore argument
3142 registers but need to adjust stack pointer back to previous stack
3143 frame location before return. */
3144 if (cfun->machine->va_args_size != 0)
3145 {
3146 /* Generate sp adjustment instruction.
3147 We need to consider padding bytes here. */
3148 sp_adjust = cfun->machine->va_args_size
3149 + cfun->machine->va_args_area_padding_bytes;
3150 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3151 stack_pointer_rtx,
3152 GEN_INT (sp_adjust));
3153 /* Emit rtx into instructions list and receive INSN rtx form. */
3154 sp_adjust_insn = emit_insn (sp_adjust_insn);
3155
3156 /* The insn rtx 'sp_adjust_insn' will change frame layout.
3157 We need to use RTX_FRAME_RELATED_P so that GCC is able to
3158 generate CFI (Call Frame Information) stuff. */
3159 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
3160 }
3161
c2927786 3162 /* Generate return instruction. */
7ea565c0 3163 if (!sibcall_p)
3164 emit_jump_insn (gen_return_internal ());
eae21ed1 3165}
3166
3167/* Function for v3push prologue. */
3168void
3169nds32_expand_prologue_v3push (void)
3170{
3171 int fp_adjust;
3172 int sp_adjust;
3173
3174 rtx Rb, Re;
eae21ed1 3175 rtx fp_adjust_insn, sp_adjust_insn;
3176
eae21ed1 3177 /* Compute and setup stack frame size.
3178 The result will be in cfun->machine. */
3179 nds32_compute_stack_frame ();
3180
3181 /* If the function is 'naked',
3182 we do not have to generate prologue code fragment. */
3183 if (cfun->machine->naked_p)
3184 return;
3185
3186 /* Get callee_first_regno and callee_last_regno. */
afddacc6 3187 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_first_gpr_regno);
3188 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_last_gpr_regno);
eae21ed1 3189
3190 /* Calculate sp_adjust first to test if 'push25 Re,imm8u' is available,
3191 where imm8u has to be 8-byte alignment. */
3192 sp_adjust = cfun->machine->local_size
3193 + cfun->machine->out_args_size
afddacc6 3194 + cfun->machine->callee_saved_area_gpr_padding_bytes;
eae21ed1 3195
3196 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
3197 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust))
3198 {
3199 /* We can use 'push25 Re,imm8u'. */
3200
66978196 3201 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
eae21ed1 3202 the pattern 'stack_v3push' is implemented in nds32.md.
3203 The (const_int 14) means v3push always push { $fp $gp $lp }. */
66978196 3204 nds32_emit_stack_v3push (Rb, Re,
3205 GEN_INT (14), GEN_INT (sp_adjust));
eae21ed1 3206
3207 /* Check frame_pointer_needed to see
3208 if we shall emit fp adjustment instruction. */
3209 if (frame_pointer_needed)
3210 {
3211 /* adjust $fp = $sp + 4 ($fp size)
3212 + 4 ($gp size)
3213 + 4 ($lp size)
3214 + (4 * n) (callee-saved registers)
3215 + sp_adjust ('push25 Re,imm8u')
3216 Note: Since we use 'push25 Re,imm8u',
3217 the position of stack pointer is further
3218 changed after push instruction.
3219 Hence, we need to take sp_adjust value
3220 into consideration. */
3221 fp_adjust = cfun->machine->fp_size
3222 + cfun->machine->gp_size
3223 + cfun->machine->lp_size
afddacc6 3224 + cfun->machine->callee_saved_gpr_regs_size
eae21ed1 3225 + sp_adjust;
3226 fp_adjust_insn = gen_addsi3 (hard_frame_pointer_rtx,
3227 stack_pointer_rtx,
3228 GEN_INT (fp_adjust));
3229 /* Emit rtx into instructions list and receive INSN rtx form. */
3230 fp_adjust_insn = emit_insn (fp_adjust_insn);
3231 }
3232 }
3233 else
3234 {
3235 /* We have to use 'push25 Re,0' and
3236 expand one more instruction to adjust $sp later. */
3237
66978196 3238 /* nds32_emit_stack_v3push(last_regno, sp_adjust),
eae21ed1 3239 the pattern 'stack_v3push' is implemented in nds32.md.
3240 The (const_int 14) means v3push always push { $fp $gp $lp }. */
66978196 3241 nds32_emit_stack_v3push (Rb, Re,
3242 GEN_INT (14), GEN_INT (0));
eae21ed1 3243
3244 /* Check frame_pointer_needed to see
3245 if we shall emit fp adjustment instruction. */
3246 if (frame_pointer_needed)
3247 {
3248 /* adjust $fp = $sp + 4 ($fp size)
3249 + 4 ($gp size)
3250 + 4 ($lp size)
3251 + (4 * n) (callee-saved registers)
3252 Note: Since we use 'push25 Re,0',
3253 the stack pointer is just at the position
3254 after push instruction.
3255 No need to take sp_adjust into consideration. */
3256 fp_adjust = cfun->machine->fp_size
3257 + cfun->machine->gp_size
3258 + cfun->machine->lp_size
afddacc6 3259 + cfun->machine->callee_saved_gpr_regs_size;
eae21ed1 3260 fp_adjust_insn = gen_addsi3 (hard_frame_pointer_rtx,
3261 stack_pointer_rtx,
3262 GEN_INT (fp_adjust));
3263 /* Emit rtx into instructions list and receive INSN rtx form. */
3264 fp_adjust_insn = emit_insn (fp_adjust_insn);
3265 }
3266
3267 /* Because we use 'push25 Re,0',
3268 we need to expand one more instruction to adjust $sp.
3269 However, sp_adjust value may be out of range of the addi instruction,
3270 create alternative add behavior with TA_REGNUM if necessary,
3271 using NEGATIVE value to tell that we are decreasing address. */
3272 sp_adjust = nds32_force_addi_stack_int ( (-1) * sp_adjust);
3273 if (sp_adjust)
3274 {
3275 /* Generate sp adjustment instruction
3276 if and only if sp_adjust != 0. */
3277 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3278 stack_pointer_rtx,
3279 GEN_INT (-1 * sp_adjust));
3280 /* Emit rtx into instructions list and receive INSN rtx form. */
3281 sp_adjust_insn = emit_insn (sp_adjust_insn);
3282
3283 /* The insn rtx 'sp_adjust_insn' will change frame layout.
3284 We need to use RTX_FRAME_RELATED_P so that GCC is able to
3285 generate CFI (Call Frame Information) stuff. */
3286 RTX_FRAME_RELATED_P (sp_adjust_insn) = 1;
3287 }
3288 }
3289
3290 /* Prevent the instruction scheduler from
3291 moving instructions across the boundary. */
3292 emit_insn (gen_blockage ());
3293}
3294
3295/* Function for v3pop epilogue. */
3296void
7ea565c0 3297nds32_expand_epilogue_v3pop (bool sibcall_p)
eae21ed1 3298{
3299 int sp_adjust;
3300
3301 rtx Rb, Re;
eae21ed1 3302 rtx sp_adjust_insn;
3303
3304 /* Compute and setup stack frame size.
3305 The result will be in cfun->machine. */
3306 nds32_compute_stack_frame ();
3307
3308 /* Prevent the instruction scheduler from
3309 moving instructions across the boundary. */
3310 emit_insn (gen_blockage ());
3311
3312 /* If the function is 'naked', we do not have to generate
3313 epilogue code fragment BUT 'ret' instruction. */
3314 if (cfun->machine->naked_p)
3315 {
c2927786 3316 /* Generate return instruction by using 'return_internal' pattern.
3317 Make sure this instruction is after gen_blockage(). */
7ea565c0 3318 if (!sibcall_p)
3319 emit_jump_insn (gen_return_internal ());
eae21ed1 3320 return;
3321 }
3322
3323 /* Get callee_first_regno and callee_last_regno. */
afddacc6 3324 Rb = gen_rtx_REG (SImode, cfun->machine->callee_saved_first_gpr_regno);
3325 Re = gen_rtx_REG (SImode, cfun->machine->callee_saved_last_gpr_regno);
eae21ed1 3326
3327 /* Calculate sp_adjust first to test if 'pop25 Re,imm8u' is available,
3328 where imm8u has to be 8-byte alignment. */
3329 sp_adjust = cfun->machine->local_size
3330 + cfun->machine->out_args_size
afddacc6 3331 + cfun->machine->callee_saved_area_gpr_padding_bytes;
eae21ed1 3332
3333 /* We have to consider alloca issue as well.
3334 If the function does call alloca(), the stack pointer is not fixed.
3335 In that case, we cannot use 'pop25 Re,imm8u' directly.
3336 We have to caculate stack pointer from frame pointer
3337 and then use 'pop25 Re,0'.
3338 Of course, the frame_pointer_needed should be nonzero
3339 if the function calls alloca(). */
3340 if (satisfies_constraint_Iu08 (GEN_INT (sp_adjust))
3341 && NDS32_DOUBLE_WORD_ALIGN_P (sp_adjust)
3342 && !cfun->calls_alloca)
3343 {
3344 /* We can use 'pop25 Re,imm8u'. */
3345
66978196 3346 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
eae21ed1 3347 the pattern 'stack_v3pop' is implementad in nds32.md.
3348 The (const_int 14) means v3pop always pop { $fp $gp $lp }. */
66978196 3349 nds32_emit_stack_v3pop (Rb, Re,
3350 GEN_INT (14), GEN_INT (sp_adjust));
eae21ed1 3351 }
3352 else
3353 {
3354 /* We have to use 'pop25 Re,0', and prior to it,
3355 we must expand one more instruction to adjust $sp. */
3356
3357 if (frame_pointer_needed)
3358 {
3359 /* adjust $sp = $fp - 4 ($fp size)
3360 - 4 ($gp size)
3361 - 4 ($lp size)
3362 - (4 * n) (callee-saved registers)
3363 Note: No need to adjust
3364 cfun->machine->callee_saved_area_padding_bytes,
3365 because we want to adjust stack pointer
3366 to the position for pop instruction. */
3367 sp_adjust = cfun->machine->fp_size
3368 + cfun->machine->gp_size
3369 + cfun->machine->lp_size
afddacc6 3370 + cfun->machine->callee_saved_gpr_regs_size;
eae21ed1 3371 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3372 hard_frame_pointer_rtx,
3373 GEN_INT (-1 * sp_adjust));
3374 /* Emit rtx into instructions list and receive INSN rtx form. */
3375 sp_adjust_insn = emit_insn (sp_adjust_insn);
3376 }
3377 else
3378 {
3379 /* If frame pointer is NOT needed,
3380 we cannot calculate the sp adjustment from frame pointer.
3381 Instead, we calculate the adjustment by local_size,
3382 out_args_size, and callee_saved_area_padding_bytes.
3383 Notice that such sp adjustment value may be out of range,
3384 so we have to deal with it as well. */
3385
3386 /* Adjust $sp = $sp + local_size + out_args_size
3387 + callee_saved_area_padding_bytes. */
3388 sp_adjust = cfun->machine->local_size
3389 + cfun->machine->out_args_size
afddacc6 3390 + cfun->machine->callee_saved_area_gpr_padding_bytes;
eae21ed1 3391 /* sp_adjust value may be out of range of the addi instruction,
3392 create alternative add behavior with TA_REGNUM if necessary,
3393 using POSITIVE value to tell that we are increasing address. */
3394 sp_adjust = nds32_force_addi_stack_int (sp_adjust);
3395 if (sp_adjust)
3396 {
3397 /* Generate sp adjustment instruction
3398 if and only if sp_adjust != 0. */
3399 sp_adjust_insn = gen_addsi3 (stack_pointer_rtx,
3400 stack_pointer_rtx,
3401 GEN_INT (sp_adjust));
3402 /* Emit rtx into instructions list and receive INSN rtx form. */
3403 sp_adjust_insn = emit_insn (sp_adjust_insn);
3404 }
3405 }
3406
66978196 3407 /* nds32_emit_stack_v3pop(last_regno, sp_adjust),
eae21ed1 3408 the pattern 'stack_v3pop' is implementad in nds32.md. */
3409 /* The (const_int 14) means v3pop always pop { $fp $gp $lp }. */
66978196 3410 nds32_emit_stack_v3pop (Rb, Re,
3411 GEN_INT (14), GEN_INT (0));
eae21ed1 3412 }
405249c1 3413
3414 /* Generate return instruction. */
3415 emit_jump_insn (gen_pop25return ());
eae21ed1 3416}
3417
c2927786 3418/* Return nonzero if this function is known to have a null epilogue.
3419 This allows the optimizer to omit jumps to jumps if no stack
3420 was created. */
3421int
3422nds32_can_use_return_insn (void)
3423{
3424 /* Prior to reloading, we can't tell how many registers must be saved.
3425 Thus we can not determine whether this function has null epilogue. */
3426 if (!reload_completed)
3427 return 0;
3428
3429 /* If no stack was created, two conditions must be satisfied:
3430 1. This is a naked function.
3431 So there is no callee-saved, local size, or outgoing size.
3432 2. This is NOT a variadic function.
3433 So there is no pushing arguement registers into the stack. */
3434 return (cfun->machine->naked_p && (cfun->machine->va_args_size == 0));
3435}
3436
eae21ed1 3437/* ------------------------------------------------------------------------ */
3438
3439/* Function to test 333-form for load/store instructions.
3440 This is auxiliary extern function for auxiliary macro in nds32.h.
3441 Because it is a little complicated, we use function instead of macro. */
3442bool
3754d046 3443nds32_ls_333_p (rtx rt, rtx ra, rtx imm, machine_mode mode)
eae21ed1 3444{
3445 if (REGNO_REG_CLASS (REGNO (rt)) == LOW_REGS
3446 && REGNO_REG_CLASS (REGNO (ra)) == LOW_REGS)
3447 {
3448 if (GET_MODE_SIZE (mode) == 4)
3449 return satisfies_constraint_Iu05 (imm);
3450
3451 if (GET_MODE_SIZE (mode) == 2)
3452 return satisfies_constraint_Iu04 (imm);
3453
3454 if (GET_MODE_SIZE (mode) == 1)
3455 return satisfies_constraint_Iu03 (imm);
3456 }
3457
3458 return false;
3459}
3460
3461
eae21ed1 3462/* Computing the Length of an Insn.
3463 Modifies the length assigned to instruction INSN.
3464 LEN is the initially computed length of the insn. */
3465int
b387f41a 3466nds32_adjust_insn_length (rtx_insn *insn, int length)
eae21ed1 3467{
3468 rtx src, dst;
3469
3470 switch (recog_memoized (insn))
3471 {
3472 case CODE_FOR_move_df:
3473 case CODE_FOR_move_di:
3474 /* Adjust length of movd44 to 2. */
3475 src = XEXP (PATTERN (insn), 1);
3476 dst = XEXP (PATTERN (insn), 0);
3477
3478 if (REG_P (src)
3479 && REG_P (dst)
3480 && (REGNO (src) % 2) == 0
3481 && (REGNO (dst) % 2) == 0)
3482 length = 2;
3483 break;
3484
3485 default:
3486 break;
3487 }
3488
3489 return length;
3490}
3491
3492
eae21ed1 3493/* Return align 2 (log base 2) if the next instruction of LABEL is 4 byte. */
3494int
56d95ea5 3495nds32_target_alignment (rtx_insn *label)
eae21ed1 3496{
d3ffa7b4 3497 rtx_insn *insn;
eae21ed1 3498
3499 if (optimize_size)
3500 return 0;
3501
3502 insn = next_active_insn (label);
3503
3504 if (insn == 0)
3505 return 0;
3506 else if ((get_attr_length (insn) % 4) == 0)
3507 return 2;
3508 else
3509 return 0;
3510}
3511
3512/* ------------------------------------------------------------------------ */
3513
3514/* PART 5: Initialize target hook structure and definitions. */
3515\f
3516/* Controlling the Compilation Driver. */
3517
3518\f
3519/* Run-time Target Specification. */
3520
3521\f
3522/* Defining Data Structures for Per-function Information. */
3523
3524\f
3525/* Storage Layout. */
3526
3527#undef TARGET_PROMOTE_FUNCTION_MODE
3528#define TARGET_PROMOTE_FUNCTION_MODE \
3529 default_promote_function_mode_always_promote
3530
3531\f
3532/* Layout of Source Language Data Types. */
3533
3534\f
3535/* Register Usage. */
3536
3537/* -- Basic Characteristics of Registers. */
3538
3539/* -- Order of Allocation of Registers. */
3540
3541/* -- How Values Fit in Registers. */
3542
3543/* -- Handling Leaf Functions. */
3544
3545/* -- Registers That Form a Stack. */
3546
3547\f
3548/* Register Classes. */
3549
3550#undef TARGET_CLASS_MAX_NREGS
3551#define TARGET_CLASS_MAX_NREGS nds32_class_max_nregs
3552
eae21ed1 3553#undef TARGET_REGISTER_PRIORITY
3554#define TARGET_REGISTER_PRIORITY nds32_register_priority
3555
3556\f
3557/* Obsolete Macros for Defining Constraints. */
3558
3559\f
3560/* Stack Layout and Calling Conventions. */
3561
3562/* -- Basic Stack Layout. */
3563
3564/* -- Exception Handling Support. */
3565
3566/* -- Specifying How Stack Checking is Done. */
3567
3568/* -- Registers That Address the Stack Frame. */
3569
3570/* -- Eliminating Frame Pointer and Arg Pointer. */
3571
3572#undef TARGET_CAN_ELIMINATE
3573#define TARGET_CAN_ELIMINATE nds32_can_eliminate
3574
3575/* -- Passing Function Arguments on the Stack. */
3576
3577/* -- Passing Arguments in Registers. */
3578
3579#undef TARGET_FUNCTION_ARG
3580#define TARGET_FUNCTION_ARG nds32_function_arg
3581
1d9df010 3582#undef TARGET_MUST_PASS_IN_STACK
3583#define TARGET_MUST_PASS_IN_STACK nds32_must_pass_in_stack
3584
a87d4efb 3585#undef TARGET_ARG_PARTIAL_BYTES
3586#define TARGET_ARG_PARTIAL_BYTES nds32_arg_partial_bytes
3587
eae21ed1 3588#undef TARGET_FUNCTION_ARG_ADVANCE
3589#define TARGET_FUNCTION_ARG_ADVANCE nds32_function_arg_advance
3590
3591#undef TARGET_FUNCTION_ARG_BOUNDARY
3592#define TARGET_FUNCTION_ARG_BOUNDARY nds32_function_arg_boundary
3593
3594/* -- How Scalar Function Values Are Returned. */
3595
3596#undef TARGET_FUNCTION_VALUE
3597#define TARGET_FUNCTION_VALUE nds32_function_value
3598
3599#undef TARGET_LIBCALL_VALUE
3600#define TARGET_LIBCALL_VALUE nds32_libcall_value
3601
3602#undef TARGET_FUNCTION_VALUE_REGNO_P
3603#define TARGET_FUNCTION_VALUE_REGNO_P nds32_function_value_regno_p
3604
3605/* -- How Large Values Are Returned. */
3606
3607/* -- Caller-Saves Register Allocation. */
3608
3609/* -- Function Entry and Exit. */
3610
3611#undef TARGET_ASM_FUNCTION_PROLOGUE
3612#define TARGET_ASM_FUNCTION_PROLOGUE nds32_asm_function_prologue
3613
3614#undef TARGET_ASM_FUNCTION_END_PROLOGUE
3615#define TARGET_ASM_FUNCTION_END_PROLOGUE nds32_asm_function_end_prologue
3616
3617#undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
3618#define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE nds32_asm_function_begin_epilogue
3619
3620#undef TARGET_ASM_FUNCTION_EPILOGUE
3621#define TARGET_ASM_FUNCTION_EPILOGUE nds32_asm_function_epilogue
3622
3623#undef TARGET_ASM_OUTPUT_MI_THUNK
3624#define TARGET_ASM_OUTPUT_MI_THUNK nds32_asm_output_mi_thunk
3625
3626#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3627#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
3628
3629/* -- Generating Code for Profiling. */
3630
3631/* -- Permitting tail calls. */
3632
3633#undef TARGET_WARN_FUNC_RETURN
3634#define TARGET_WARN_FUNC_RETURN nds32_warn_func_return
3635
3636/* Stack smashing protection. */
3637
3638\f
3639/* Implementing the Varargs Macros. */
3640
fb0c8d3a 3641#undef TARGET_SETUP_INCOMING_VARARGS
3642#define TARGET_SETUP_INCOMING_VARARGS nds32_setup_incoming_varargs
3643
eae21ed1 3644#undef TARGET_STRICT_ARGUMENT_NAMING
3645#define TARGET_STRICT_ARGUMENT_NAMING nds32_strict_argument_naming
3646
3647\f
3648/* Trampolines for Nested Functions. */
3649
3650#undef TARGET_ASM_TRAMPOLINE_TEMPLATE
3651#define TARGET_ASM_TRAMPOLINE_TEMPLATE nds32_asm_trampoline_template
3652
3653#undef TARGET_TRAMPOLINE_INIT
3654#define TARGET_TRAMPOLINE_INIT nds32_trampoline_init
3655
3656\f
3657/* Implicit Calls to Library Routines. */
3658
3659\f
3660/* Addressing Modes. */
3661
3662#undef TARGET_LEGITIMATE_ADDRESS_P
3663#define TARGET_LEGITIMATE_ADDRESS_P nds32_legitimate_address_p
3664
3665\f
3666/* Anchored Addresses. */
3667
3668\f
3669/* Condition Code Status. */
3670
3671/* -- Representation of condition codes using (cc0). */
3672
3673/* -- Representation of condition codes using registers. */
3674
3675/* -- Macros to control conditional execution. */
3676
3677\f
3678/* Describing Relative Costs of Operations. */
3679
3680#undef TARGET_REGISTER_MOVE_COST
3681#define TARGET_REGISTER_MOVE_COST nds32_register_move_cost
3682
3683#undef TARGET_MEMORY_MOVE_COST
3684#define TARGET_MEMORY_MOVE_COST nds32_memory_move_cost
3685
3686#undef TARGET_RTX_COSTS
3687#define TARGET_RTX_COSTS nds32_rtx_costs
3688
3689#undef TARGET_ADDRESS_COST
3690#define TARGET_ADDRESS_COST nds32_address_cost
3691
3692\f
3693/* Adjusting the Instruction Scheduler. */
3694
3695\f
3696/* Dividing the Output into Sections (Texts, Data, . . . ). */
3697
8e4f6b00 3698#undef TARGET_ENCODE_SECTION_INFO
3699#define TARGET_ENCODE_SECTION_INFO nds32_encode_section_info
3700
eae21ed1 3701\f
3702/* Position Independent Code. */
3703
3704\f
3705/* Defining the Output Assembler Language. */
3706
3707/* -- The Overall Framework of an Assembler File. */
3708
3709#undef TARGET_ASM_FILE_START
3710#define TARGET_ASM_FILE_START nds32_asm_file_start
3711#undef TARGET_ASM_FILE_END
3712#define TARGET_ASM_FILE_END nds32_asm_file_end
3713
3714/* -- Output of Data. */
3715
3716#undef TARGET_ASM_ALIGNED_HI_OP
3717#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
3718
3719#undef TARGET_ASM_ALIGNED_SI_OP
3720#define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
3721
3722/* -- Output of Uninitialized Variables. */
3723
3724/* -- Output and Generation of Labels. */
3725
3726#undef TARGET_ASM_GLOBALIZE_LABEL
3727#define TARGET_ASM_GLOBALIZE_LABEL nds32_asm_globalize_label
3728
3729/* -- How Initialization Functions Are Handled. */
3730
3731/* -- Macros Controlling Initialization Routines. */
3732
3733/* -- Output of Assembler Instructions. */
3734
3735#undef TARGET_PRINT_OPERAND
3736#define TARGET_PRINT_OPERAND nds32_print_operand
3737#undef TARGET_PRINT_OPERAND_ADDRESS
3738#define TARGET_PRINT_OPERAND_ADDRESS nds32_print_operand_address
3739
3740/* -- Output of Dispatch Tables. */
3741
3742/* -- Assembler Commands for Exception Regions. */
3743
3744/* -- Assembler Commands for Alignment. */
3745
3746\f
3747/* Controlling Debugging Information Format. */
3748
3749/* -- Macros Affecting All Debugging Formats. */
3750
3751/* -- Specific Options for DBX Output. */
3752
3753/* -- Open-Ended Hooks for DBX Format. */
3754
3755/* -- File Names in DBX Format. */
3756
3757/* -- Macros for SDB and DWARF Output. */
3758
3759/* -- Macros for VMS Debug Format. */
3760
3761\f
3762/* Cross Compilation and Floating Point. */
3763
3764\f
3765/* Mode Switching Instructions. */
3766
3767\f
3768/* Defining target-specific uses of __attribute__. */
3769
3770#undef TARGET_ATTRIBUTE_TABLE
3771#define TARGET_ATTRIBUTE_TABLE nds32_attribute_table
3772
3773#undef TARGET_MERGE_DECL_ATTRIBUTES
3774#define TARGET_MERGE_DECL_ATTRIBUTES nds32_merge_decl_attributes
3775
3776#undef TARGET_INSERT_ATTRIBUTES
3777#define TARGET_INSERT_ATTRIBUTES nds32_insert_attributes
3778
3779#undef TARGET_OPTION_PRAGMA_PARSE
3780#define TARGET_OPTION_PRAGMA_PARSE nds32_option_pragma_parse
3781
3782#undef TARGET_OPTION_OVERRIDE
3783#define TARGET_OPTION_OVERRIDE nds32_option_override
3784
3785\f
3786/* Emulating TLS. */
3787
3788\f
3789/* Defining coprocessor specifics for MIPS targets. */
3790
3791\f
3792/* Parameters for Precompiled Header Validity Checking. */
3793
3794\f
3795/* C++ ABI parameters. */
3796
3797\f
3798/* Adding support for named address spaces. */
3799
3800\f
3801/* Miscellaneous Parameters. */
3802
3803#undef TARGET_INIT_BUILTINS
3804#define TARGET_INIT_BUILTINS nds32_init_builtins
3805
3806#undef TARGET_EXPAND_BUILTIN
3807#define TARGET_EXPAND_BUILTIN nds32_expand_builtin
3808
3809\f
3810/* ------------------------------------------------------------------------ */
3811
3812/* Initialize the GCC target structure. */
3813
3814struct gcc_target targetm = TARGET_INITIALIZER;
3815
3816/* ------------------------------------------------------------------------ */