1 /* Definitions of target machine for Altera Nios II.
2    Copyright (C) 2012-2022 Free Software Foundation, Inc.
3    Contributed by Jonah Graham (jgraham@altera.com),
4    Will Reece (wreece@altera.com), and Jeff DaSilva (jdasilva@altera.com).
5    Contributed by Mentor Graphics, Inc.
6 
7    This file is part of GCC.
8 
9    GCC is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published
11    by the Free Software Foundation; either version 3, or (at your
12    option) any later version.
13 
14    GCC is distributed in the hope that it will be useful, but WITHOUT
15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17    License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with GCC; see the file COPYING3.  If not see
21    <http://www.gnu.org/licenses/>.  */
22 
23 #ifndef GCC_NIOS2_H
24 #define GCC_NIOS2_H
25 
26 /* Indicate R2 ISA level support.  */
27 #define TARGET_ARCH_R2 (nios2_arch_option == ARCH_R2)
28 
29 /* FPU insn codes declared here.  */
30 #include "config/nios2/nios2-opts.h"
31 
32 /* Define built-in preprocessor macros.  */
33 #define TARGET_CPU_CPP_BUILTINS()                   \
34   do                                                \
35     {                                               \
36       builtin_define_std ("NIOS2");                 \
37       builtin_define_std ("nios2");                 \
38       if (TARGET_BIG_ENDIAN)                        \
39         builtin_define_std ("nios2_big_endian");    \
40       else                                          \
41         builtin_define_std ("nios2_little_endian"); \
42       builtin_define_with_int_value (		    \
43         "__nios2_arch__", (int) nios2_arch_option); \
44     }						    \
45   while (0)
46 
47 /* We're little endian, unless otherwise specified by defining
48    BIG_ENDIAN_FLAG.  */
49 #ifndef TARGET_ENDIAN_DEFAULT
50 # define TARGET_ENDIAN_DEFAULT 0
51 #endif
52 
53 /* Default target_flags if no switches specified.  */
54 #ifndef TARGET_DEFAULT
55 # define TARGET_DEFAULT (MASK_HAS_MUL | TARGET_ENDIAN_DEFAULT)
56 #endif
57 
58 #define OPTION_DEFAULT_SPECS \
59   {"arch", "%{!march=*:%{!mcpu=*:-march=%(VALUE)}}" }
60 
61 #define CC1_SPEC "%{G*}"
62 
63 #if TARGET_ENDIAN_DEFAULT == 0
64 # define ASM_SPEC "%{!meb:-EL} %{meb:-EB} %{march=*:-march=%*}"
65 # define LINK_SPEC_ENDIAN "%{!meb:-EL} %{meb:-EB}"
66 # define MULTILIB_DEFAULTS { "EL" }
67 #else
68 # define ASM_SPEC "%{!mel:-EB} %{mel:-EL} %{march=*:-march=%*}"
69 # define LINK_SPEC_ENDIAN "%{!mel:-EB} %{mel:-EL}"
70 # define MULTILIB_DEFAULTS { "EB" }
71 #endif
72 
73 #define LINK_SPEC LINK_SPEC_ENDIAN \
74   " %{shared:-shared} \
75     %{static:-Bstatic}"
76 
77 
78 /* Storage layout.  */
79 
80 #define DEFAULT_SIGNED_CHAR 1
81 #define BITS_BIG_ENDIAN 0
82 #define BYTES_BIG_ENDIAN (TARGET_BIG_ENDIAN != 0)
83 #define WORDS_BIG_ENDIAN (TARGET_BIG_ENDIAN != 0)
84 #define BITS_PER_WORD 32
85 #define UNITS_PER_WORD 4
86 #define POINTER_SIZE 32
87 #define BIGGEST_ALIGNMENT 32
88 #define STRICT_ALIGNMENT 1
89 #define FUNCTION_BOUNDARY 32
90 #define PARM_BOUNDARY 32
91 #define STACK_BOUNDARY 32
92 #define PREFERRED_STACK_BOUNDARY 32
93 #define MAX_FIXED_MODE_SIZE 64
94 
95 #define LABEL_ALIGN(LABEL) nios2_label_align (LABEL)
96 
97 /* Layout of source language data types.  */
98 
99 #define INT_TYPE_SIZE 32
100 #define SHORT_TYPE_SIZE 16
101 #define LONG_TYPE_SIZE 32
102 #define LONG_LONG_TYPE_SIZE 64
103 #define FLOAT_TYPE_SIZE 32
104 #define DOUBLE_TYPE_SIZE 64
105 #define LONG_DOUBLE_TYPE_SIZE DOUBLE_TYPE_SIZE
106 
107 #undef SIZE_TYPE
108 #define SIZE_TYPE "unsigned int"
109 
110 #undef PTRDIFF_TYPE
111 #define PTRDIFF_TYPE "int"
112 
113 
114 /* Basic characteristics of Nios II registers:
115 
116    Regno  Name
117    0      r0       zero    always zero
118    1      r1       at      Assembler Temporary
119    2-3    r2-r3            Return Location
120    4-7    r4-r7            Register Arguments
121    8-15   r8-r15           Caller Saved Registers
122    16-22  r16-r22          Callee Saved Registers
123    22     r22              Global Offset Table pointer (Linux ABI only)
124    23     r23              Thread pointer (Linux ABI only)
125    24     r24      et      Exception Temporary
126    25     r25      bt      Breakpoint Temporary
127    26     r26      gp      Global Pointer
128    27     r27      sp      Stack Pointer
129    28     r28      fp      Frame Pointer
130    29     r29      ea      Exception Return Address
131    30     r30      ba      Breakpoint Return Address
132    31     r31      ra      Return Address
133 
134    32     ctl0     status
135    33     ctl1     estatus STATUS saved by exception
136    34     ctl2     bstatus STATUS saved by break
137    35     ctl3     ipri    Interrupt Priority Mask
138    36     ctl4     ecause  Exception Cause
139 
140    37     pc               Not an actual register
141 
142    38     fake_fp          Fake Frame Pointer (always eliminated)
143    39     fake_ap          Fake Argument Pointer (always eliminated)
144    40                      First Pseudo Register
145 
146    In addition, r12 is used as the static chain register and r13, r14, and r15
147    are clobbered by PLT code sequences.
148 
149    The definitions for all the hard register numbers are located in nios2.md.
150 */
151 
152 #define FIXED_REGISTERS                      \
153   {					     \
154 /*        +0  1  2  3  4  5  6  7  8  9 */   \
155 /*   0 */  1, 1, 0, 0, 0, 0, 0, 0, 0, 0,     \
156 /*  10 */  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     \
157 /*  20 */  0, 0, TARGET_LINUX_ABI, TARGET_LINUX_ABI, 1, 1, 1, 1, 0, 1,     \
158 /*  30 */  1, 0, 1, 1, 1, 1, 1, 1, 1, 1,     \
159   }
160 
161 /* Call used == caller saved + fixed regs + args + ret vals.  */
162 #define CALL_USED_REGISTERS                  \
163   {					     \
164 /*        +0  1  2  3  4  5  6  7  8  9 */   \
165 /*   0 */  1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     \
166 /*  10 */  1, 1, 1, 1, 1, 1, 0, 0, 0, 0,     \
167 /*  20 */  0, 0, TARGET_LINUX_ABI, TARGET_LINUX_ABI, 1, 1, 1, 1, 0, 1,     \
168 /*  30 */  1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     \
169   }
170 
171 /* Order in which to allocate registers.  Each register must be
172    listed once.  This is the default ordering for R1 and non-CDX R2
173    code.  For CDX, we overwrite this in ADJUST_REG_ALLOC_ORDER.  */
174 #define REG_ALLOC_ORDER							\
175   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \
176       20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, \
177       37, 38, 39 }
178 
179 #define ADJUST_REG_ALLOC_ORDER nios2_adjust_reg_alloc_order ()
180 
181 /* Caller-save costs can be less emphasized under R2 CDX, where we can
182    use push.n/pop.n.  */
183 #define HONOR_REG_ALLOC_ORDER (TARGET_HAS_CDX)
184 
185 /* Register Classes.  */
186 
187 enum reg_class
188 {
189   NO_REGS,
190   SIB_REGS,
191   IJMP_REGS,
192   GP_REGS,
193   ALL_REGS,
194   LIM_REG_CLASSES
195 };
196 
197 #define N_REG_CLASSES (int) LIM_REG_CLASSES
198 
199 #define REG_CLASS_NAMES   \
200   {  "NO_REGS",		  \
201      "SIB_REGS",	  \
202      "IJMP_REGS",	  \
203      "GP_REGS",           \
204      "ALL_REGS" }
205 
206 #define GENERAL_REGS ALL_REGS
207 
208 #define REG_CLASS_CONTENTS			\
209   {						\
210     /* NO_REGS    */ { 0, 0},			\
211     /* SIB_REGS   */ { 0xfe0c, 0},		\
212     /* IJMP_REGS  */ { 0x7fffffff, 0},		\
213     /* GP_REGS    */ {~0, 0},			\
214     /* ALL_REGS   */ {~0,~0}			\
215   }
216 
217 
218 #define GP_REG_P(REGNO) ((unsigned)(REGNO) <= LAST_GP_REG)
219 #define REGNO_REG_CLASS(REGNO) (GP_REG_P (REGNO) ? GP_REGS : ALL_REGS)
220 #define CLASS_MAX_NREGS(CLASS, MODE)					\
221   ((GET_MODE_SIZE (MODE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
222 
223 #define CDX_REG_P(REGNO)						\
224   ((REGNO) == 16 || (REGNO) == 17 || ((REGNO) >= 2 && (REGNO) <= 7))
225 
226 /* Tests for various kinds of constants used in the Nios II port.  */
227 
228 #define SMALL_INT(X) ((unsigned HOST_WIDE_INT)(X) + 0x8000 < 0x10000)
229 #define SMALL_INT12(X) ((unsigned HOST_WIDE_INT)(X) + 0x800 < 0x1000)
230 #define SMALL_INT_UNSIGNED(X) ((X) >= 0 && (X) < 0x10000)
231 #define UPPER16_INT(X) (((X) & 0xffff) == 0)
232 #define SHIFT_INT(X) ((X) >= 0 && (X) <= 31)
233 #define RDWRCTL_INT(X) ((X) >= 0 && (X) <= 31)
234 #define CUSTOM_INSN_OPCODE(X) ((X) >= 0 && (X) <= 255)
235 #define ANDCLEAR_INT(X) \
236   (((X) & 0xffff) == 0xffff || (((X) >> 16) & 0xffff) == 0xffff)
237 
238 /* Say that the epilogue uses the return address register.  Note that
239    in the case of sibcalls, the values "used by the epilogue" are
240    considered live at the start of the called function.  */
241 #define EPILOGUE_USES(REGNO) (epilogue_completed && (REGNO) == RA_REGNO)
242 
243 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
244    the stack pointer does not matter.  The value is tested only in
245    functions that have frame pointers.
246    No definition is equivalent to always zero.  */
247 
248 #define EXIT_IGNORE_STACK 1
249 
250 /* Trampolines use a 5-instruction sequence.  */
251 #define TRAMPOLINE_SIZE 20
252 
253 /* Stack layout.  */
254 #define STACK_GROWS_DOWNWARD 1
255 #define FRAME_GROWS_DOWNWARD 1
256 #define FIRST_PARM_OFFSET(FUNDECL) 0
257 
258 /* Before the prologue, RA lives in r31.  */
259 #define INCOMING_RETURN_ADDR_RTX  gen_rtx_REG (Pmode, RA_REGNO)
260 #define RETURN_ADDR_RTX(C,F) nios2_get_return_address (C)
261 
262 #define DWARF_FRAME_RETURN_COLUMN RA_REGNO
263 
264 /* The CFA includes the pretend args.  */
265 #define ARG_POINTER_CFA_OFFSET(FNDECL) \
266   (gcc_assert ((FNDECL) == current_function_decl), \
267    FIRST_PARM_OFFSET (FNDECL) + crtl->args.pretend_args_size)
268 
269 /* Frame/arg pointer elimination settings.  */
270 #define ELIMINABLE_REGS                                                 \
271 {{ ARG_POINTER_REGNUM,   STACK_POINTER_REGNUM},                         \
272  { ARG_POINTER_REGNUM,   HARD_FRAME_POINTER_REGNUM},                    \
273  { FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM},                         \
274  { FRAME_POINTER_REGNUM, HARD_FRAME_POINTER_REGNUM}}
275 
276 #define INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET) \
277   (OFFSET) = nios2_initial_elimination_offset ((FROM), (TO))
278 
279 /* Calling convention definitions.  */
280 typedef struct nios2_args
281 {
282   int regs_used;
283 } CUMULATIVE_ARGS;
284 
285 #define NUM_ARG_REGS (LAST_ARG_REGNO - FIRST_ARG_REGNO + 1)
286 
287 #define INIT_CUMULATIVE_ARGS(CUM, FNTYPE, LIBNAME, FNDECL, N_NAMED_ARGS) \
288   do { (CUM).regs_used = 0; } while (0)
289 
290 #define PAD_VARARGS_DOWN \
291   (targetm.calls.function_arg_padding (TYPE_MODE (type), type) == PAD_DOWNWARD)
292 
293 #define BLOCK_REG_PADDING(MODE, TYPE, FIRST) \
294   (nios2_block_reg_padding ((MODE), (TYPE), (FIRST)))
295 
296 #define FUNCTION_ARG_REGNO_P(REGNO) \
297   ((REGNO) >= FIRST_ARG_REGNO && (REGNO) <= LAST_ARG_REGNO)
298 
299 /* Passing function arguments on stack.  */
300 #define ACCUMULATE_OUTGOING_ARGS 1
301 
302 /* We define TARGET_RETURN_IN_MEMORY, so set to zero.  */
303 #define DEFAULT_PCC_STRUCT_RETURN 0
304 
305 /* Profiling.  */
306 #define PROFILE_BEFORE_PROLOGUE
307 #define NO_PROFILE_COUNTERS 1
308 #define FUNCTION_PROFILER(FILE, LABELNO) \
309   nios2_function_profiler ((FILE), (LABELNO))
310 
311 /* Addressing modes.  */
312 
313 #define CONSTANT_ADDRESS_P(X) \
314   (CONSTANT_P (X) && memory_address_p (SImode, X))
315 
316 #define MAX_REGS_PER_ADDRESS 1
317 #define BASE_REG_CLASS ALL_REGS
318 #define INDEX_REG_CLASS NO_REGS
319 
320 #define REGNO_OK_FOR_BASE_P(REGNO) nios2_regno_ok_for_base_p ((REGNO), true)
321 #define REGNO_OK_FOR_INDEX_P(REGNO) 0
322 
323 /* Describing Relative Costs of Operations.  */
324 #define MOVE_MAX 4
325 #define SLOW_BYTE_ACCESS 1
326 
327 /* It is as good to call a constant function address as to call an address
328    kept in a register.  */
329 #define NO_FUNCTION_CSE 1
330 
331 /* Position independent code.  */
332 
333 #define PIC_OFFSET_TABLE_REGNUM 22
334 #define LEGITIMATE_PIC_OPERAND_P(X) nios2_legitimate_pic_operand_p (X)
335 
336 /* Define output assembler language.  */
337 
338 #define ASM_APP_ON "#APP\n"
339 #define ASM_APP_OFF "#NO_APP\n"
340 
341 #define ASM_COMMENT_START "# "
342 
343 #define GLOBAL_ASM_OP "\t.global\t"
344 
345 #define REGISTER_NAMES \
346   {		       \
347     "zero", \
348     "at", \
349     "r2", \
350     "r3", \
351     "r4", \
352     "r5", \
353     "r6", \
354     "r7", \
355     "r8", \
356     "r9", \
357     "r10", \
358     "r11", \
359     "r12", \
360     "r13", \
361     "r14", \
362     "r15", \
363     "r16", \
364     "r17", \
365     "r18", \
366     "r19", \
367     "r20", \
368     "r21", \
369     "r22", \
370     "r23", \
371     "et", \
372     "bt", \
373     "gp", \
374     "sp", \
375     "fp", \
376     "ta", \
377     "ba", \
378     "ra", \
379     "status", \
380     "estatus", \
381     "bstatus", \
382     "ipri", \
383     "ecause", \
384     "pc", \
385     "fake_fp", \
386     "fake_ap", \
387 }
388 
389 #define ADDITIONAL_REGISTER_NAMES       \
390 {					\
391   {"r0", 0},				\
392   {"r1", 1},				\
393   {"r24", 24},                          \
394   {"r25", 25},                          \
395   {"r26", 26},                          \
396   {"r27", 27},                          \
397   {"r28", 28},                          \
398   {"r29", 29},                          \
399   {"r30", 30},                          \
400   {"r31", 31}                           \
401 }
402 
403 #define ASM_OUTPUT_ADDR_VEC_ELT(FILE, VALUE)  \
404   do									\
405     {									\
406       fputs (integer_asm_op (POINTER_SIZE / BITS_PER_UNIT, TRUE), FILE); \
407       fprintf (FILE, ".L%u\n", (unsigned) (VALUE));			\
408     }									\
409   while (0)
410 
411 #define ASM_OUTPUT_ADDR_DIFF_ELT(STREAM, BODY, VALUE, REL)\
412   do									\
413     {									\
414       fputs (integer_asm_op (POINTER_SIZE / BITS_PER_UNIT, TRUE), STREAM); \
415       fprintf (STREAM, ".L%u-.L%u\n", (unsigned) (VALUE), (unsigned) (REL)); \
416     }									\
417   while (0)
418 
419 /* Section directives.  */
420 
421 /* Output before read-only data.  */
422 #define TEXT_SECTION_ASM_OP "\t.section\t.text"
423 
424 /* Output before writable data.  */
425 #define DATA_SECTION_ASM_OP "\t.section\t.data"
426 
427 /* Output before uninitialized data.  */
428 #define BSS_SECTION_ASM_OP "\t.section\t.bss"
429 
430 /* Output before 'small' uninitialized data.  */
431 #define SBSS_SECTION_ASM_OP "\t.section\t.sbss"
432 
433 #ifndef USED_FOR_TARGET
434 /* Default the definition of "small data" to 8 bytes.  */
435 extern unsigned HOST_WIDE_INT nios2_section_threshold;
436 #endif
437 
438 #define NIOS2_DEFAULT_GVALUE 8
439 
440 /* This says how to output assembler code to declare an
441    uninitialized external linkage data object.  Under SVR4,
442    the linker seems to want the alignment of data objects
443    to depend on their types.  We do exactly that here.  */
444 #undef COMMON_ASM_OP
445 #define COMMON_ASM_OP   "\t.comm\t"
446 
447 #define ASM_OUTPUT_ALIGN(FILE, LOG)		     \
448   do {						     \
449     fprintf ((FILE), "%s%d\n", ALIGN_ASM_OP, (LOG)); \
450   } while (0)
451 
452 #undef  ASM_OUTPUT_ALIGNED_COMMON
453 #define ASM_OUTPUT_ALIGNED_COMMON(FILE, NAME, SIZE, ALIGN)              \
454 do                                                                      \
455   {									\
456     fprintf ((FILE), "%s", COMMON_ASM_OP);				\
457     assemble_name ((FILE), (NAME));					\
458     fprintf ((FILE), "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", (SIZE),	\
459 	     (ALIGN) / BITS_PER_UNIT);					\
460   }									\
461 while (0)
462 
463 
464 /* This says how to output assembler code to declare an
465    uninitialized internal linkage data object.  Under SVR4,
466    the linker seems to want the alignment of data objects
467    to depend on their types.  We do exactly that here.  */
468 
469 #undef  ASM_OUTPUT_ALIGNED_DECL_LOCAL
470 #define ASM_OUTPUT_ALIGNED_DECL_LOCAL(FILE, DECL, NAME, SIZE, ALIGN)	\
471 do {                                                                    \
472  if (targetm.in_small_data_p (DECL))					\
473     switch_to_section (sbss_section);					\
474   else                                                                  \
475     switch_to_section (bss_section);					\
476   ASM_OUTPUT_TYPE_DIRECTIVE (FILE, NAME, "object");                     \
477   if (!flag_inhibit_size_directive)                                     \
478     ASM_OUTPUT_SIZE_DIRECTIVE (FILE, NAME, SIZE);                       \
479   ASM_OUTPUT_ALIGN ((FILE), exact_log2((ALIGN) / BITS_PER_UNIT));       \
480   ASM_OUTPUT_LABEL(FILE, NAME);                                         \
481   ASM_OUTPUT_SKIP((FILE), (SIZE) ? (SIZE) : 1);                         \
482 } while (0)
483 
484 /* Put the jump tables in .text because when using position-independent code,
485    Nios II elf has no relocation that can represent arbitrary differences
486    between symbols in different sections.  */
487 #define JUMP_TABLES_IN_TEXT_SECTION 1
488 
489 /* Exception handling.  */
490 
491 /* Describe __builtin_eh_return.  */
492 #define EH_RETURN_STACKADJ_RTX gen_rtx_REG (Pmode, LAST_RETVAL_REGNO)
493 #define EH_RETURN_DATA_REGNO(N) ((N) <= (LAST_ARG_REGNO - FIRST_ARG_REGNO) \
494 				 ? (N) + FIRST_ARG_REGNO : INVALID_REGNUM)
495 
496 /* For PIC, use indirect for global references; it'll end up using a dynamic
497    relocation, which we want to keep out of read-only EH sections.
498    For local references, we want to use GOT-relative offsets provided
499    the assembler supports them.  For non-PIC, use an absolute encoding.  */
500 #ifdef HAVE_AS_NIOS2_GOTOFF_RELOCATION
501 #define ASM_PREFERRED_EH_DATA_FORMAT(CODE, GLOBAL)		\
502   (flag_pic							\
503    ? ((GLOBAL)							\
504       ? DW_EH_PE_indirect | DW_EH_PE_absptr			\
505       : DW_EH_PE_datarel | DW_EH_PE_sdata4)			\
506    : DW_EH_PE_absptr)
507 
508 #define ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX(FILE, ENCODING, SIZE, ADDR, DONE) \
509   do {									\
510       if (((ENCODING) & 0xf0) == DW_EH_PE_datarel)			\
511       {									\
512 	fputs ("\t.4byte %gotoff(", FILE);				\
513 	output_addr_const (FILE, ADDR);					\
514 	fputs (")", FILE);						\
515 	goto DONE;							\
516       }									\
517   } while (0)
518 
519 #else
520 /* We don't have %gotoff support in the assembler.  Fall back to the encoding
521    it used to use instead before the assembler was fixed.  This has known
522    bugs but mostly works.  */
523 #define ASM_PREFERRED_EH_DATA_FORMAT(CODE, GLOBAL)		\
524   (flag_pic							\
525    ? ((GLOBAL)							\
526       ? DW_EH_PE_indirect | DW_EH_PE_absptr			\
527       : DW_EH_PE_aligned)					\
528    : DW_EH_PE_absptr)
529 #endif
530 
531 /* Misc. parameters.  */
532 
533 #define STORE_FLAG_VALUE 1
534 #define Pmode SImode
535 #define FUNCTION_MODE QImode
536 
537 #define CASE_VECTOR_MODE Pmode
538 
539 #define LOAD_EXTEND_OP(MODE) (ZERO_EXTEND)
540 
541 #define WORD_REGISTER_OPERATIONS 1
542 
543 #endif /* GCC_NIOS2_H */
544