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