Debian Codegraph @

gcc/config/csky/csky.cc

raw
1 /* GCC backend functions for C-SKY targets.
2 Copyright (C) 2018-2025 Free Software Foundation, Inc.
3 Contributed by C-SKY Microsystems and Mentor Graphics.
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
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "memmodel.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "cfghooks.h"
32 #include "df.h"
33 #include "tm_p.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "emit-rtl.h"
39 #include "recog.h"
40 #include "cgraph.h"
41 #include "c-family/c-common.h"
42 #include "cpplib.h"
43 #include "diagnostic-core.h"
44 #include "alias.h"
45 #include "fold-const.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "varasm.h"
49 #include "output.h"
50 #include "insn-attr.h"
51 #include "flags.h"
52 #include "reload.h"
53 #include "explow.h"
54 #include "expr.h"
55 #include "cfgrtl.h"
56 #include "sched-int.h"
57 #include "common/common-target.h"
58 #include "langhooks.h"
59 #include "intl.h"
60 #include "libfuncs.h"
61 #include "opts.h"
62 #include "dumpfile.h"
63 #include "target-globals.h"
64 #include "builtins.h"
65 #include "tm-constrs.h"
66 #include "rtl-iter.h"
67 #include "pass_manager.h"
68 #include "tree-pass.h"
69 #include "context.h"
70 #include "opts.h"
71
72 /* This file should be included last. */
73 #include "target-def.h"
74
75 /* Stack and register size macros. */
76
77 #define CSKY_NUM_WORDS(SIZE) \
78 (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
79 #define CSKY_NUM_REGS(MODE) \
80 CSKY_NUM_WORDS (GET_MODE_SIZE (MODE))
81 #define CSKY_STACK_ALIGN(SIZE) \
82 (CSKY_NUM_WORDS (SIZE) * UNITS_PER_WORD)
83
84 /* Offsets and range macros. */
85
86 #define CSKY_LD16_MAX_OFFSET(MODE) \
87 (31 * GET_MODE_SIZE (MODE))
88 #define CSKY_LD32_MAX_OFFSET(MODE) \
89 (4095 * GET_MODE_SIZE (MODE))
90 #define CSKY_LD16_OFFSET_MASK(MODE) \
91 (CSKY_LD16_MAX_OFFSET (MODE) + GET_MODE_SIZE (MODE) - 1)
92
93 #define CSKY_ADDI16_MAX_IMM 256
94 #define CSKY_SUBI16_MAX_IMM 256
95
96 #define CSKY_CONSTPOOL_LABEL_PREFIX "LCP"
97
98 /* Array of the smallest class containing reg number REGNO, indexed by
99 REGNO. Used by REGNO_REG_CLASS. */
100 enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER] =
101 {
102 /* Registers r0-r7. */
103 MINI_REGS, MINI_REGS, MINI_REGS, MINI_REGS,
104 MINI_REGS, MINI_REGS, MINI_REGS, MINI_REGS,
105 /* Registers r8-r15. */
106 LOW_REGS, LOW_REGS, LOW_REGS, LOW_REGS,
107 LOW_REGS, LOW_REGS, SP_REGS, LOW_REGS,
108 /* Registers r16-r31. */
109 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
110 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
111 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
112 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
113 /* Reserved. */
114 RESERVE_REGS,
115 /* CC,HI,LO registers. */
116 C_REGS, HILO_REGS, HILO_REGS,
117 /* Reserved. */
118 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
119 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
120 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
121 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
122 /* Vec registers. */
123 V_REGS, V_REGS, V_REGS, V_REGS,
124 V_REGS, V_REGS, V_REGS, V_REGS,
125 V_REGS, V_REGS, V_REGS, V_REGS,
126 V_REGS, V_REGS, V_REGS, V_REGS,
127 /* Reserved. */
128 RESERVE_REGS, RESERVE_REGS,
129 /* Register epc. */
130 OTHER_REGS,
131 /* Vec registers. */
132 V_REGS, V_REGS, V_REGS, V_REGS,
133 V_REGS, V_REGS, V_REGS, V_REGS,
134 V_REGS, V_REGS, V_REGS, V_REGS,
135 V_REGS, V_REGS, V_REGS, V_REGS,
136 /* Reserved. */
137 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
138 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
139 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
140 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
141 /* Reserved. */
142 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
143 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
144 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
145 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
146 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
147 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
148 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
149 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
150
151 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
152 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
153 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
154 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
155 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
156 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
157 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
158 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
159
160 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
161 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
162 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
163 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
164 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
165 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
166 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
167 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS, RESERVE_REGS,
168
169 RESERVE_REGS, RESERVE_REGS, RESERVE_REGS
170 };
171
172 /* Arrays that map GCC register numbers to debugger register numbers,
173 '-1' means that is INVALID_REGNUM.
174 TODO: which rules according to here ? */
175 const int csky_debugger_regno[FIRST_PSEUDO_REGISTER] =
176 {
177 0, 1, 2, 3, 4, 5, 6, 7,
178 8, 9, 10, 11, 12, 13, 14, 15,
179 16, 17, 18, 19, 20, 21, 22, 23,
180 24, 25, 26, 27, 28, 29, 30, 31,
181 -1, -1, 36, 37,
182 75, 79, 83, 87, 91, 95, 99, 103,
183 107, 111, 115, 119, 123, 127, 131, 135,
184 74, 78, 82, 86, 90, 94, 98, 102,
185 106, 110, 114, 118, 122, 126, 130, 134,
186 -1, -1, 72,
187 /* vr: 71 - 86 */
188 139, 143, 147, 151, 155, 159, 163, 167,
189 171, 175, 179, 183, 187, 191, 195, 199,
190 138, 142, 146, 150, 154, 158, 162, 166,
191 170, 174, 178, 182, 186, 190, 194, 198,
192 /* resereved */
193 -1, -1, -1, -1, -1, -1, -1, -1,
194 -1, -1, -1, -1, -1, -1, -1, -1,
195 -1, -1, -1, -1, -1, -1, -1, -1,
196 -1, -1, -1, -1, -1, -1, -1, -1,
197
198 -1, -1, -1, -1, -1, -1, -1, -1,
199 -1, -1, -1, -1, -1, -1, -1, -1,
200 -1, -1, -1, -1, -1, -1, -1, -1,
201 -1, -1, -1, -1, -1, -1, -1, -1,
202
203 -1, -1, -1, -1, -1, -1, -1, -1,
204 -1, -1, -1, -1, -1, -1, -1, -1,
205 -1, -1, -1, -1, -1, -1, -1, -1,
206 -1, -1, -1, -1, -1, -1, -1, -1,
207
208 -1, -1, -1
209 };
210
211 /* Table of machine attributes. */
212 static tree csky_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
213 static tree csky_handle_isr_attribute (tree *, tree, tree, int, bool *);
214 TARGET_GNU_ATTRIBUTES (csky_attribute_table,
215 {
216 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
217 affects_type_identity, handler, exclude } */
218 { "naked", 0, 0, true, false, false, false, csky_handle_fndecl_attribute, NULL },
219 /* Interrupt Service Routines have special prologue and epilogue requirements. */
220 { "interrupt", 0, 1, false, false, false, false, csky_handle_isr_attribute, NULL },
221 { "isr", 0, 1, false, false, false, false, csky_handle_isr_attribute, NULL }
222 });
223
224 /* A C structure for machine-specific, per-function data.
225 This is added to the cfun structure. */
226 typedef struct GTY(()) machine_function
227 {
228 /* Records if LR has to be saved for far jumps. */
229 int far_jump_used;
230 /* Records the type of the current function. */
231 unsigned long func_type;
232 /* Record if the function has a variable argument list. */
233 int uses_anonymous_args;
234
235 /* Stack frame layout information. If frame_init_p is true,
236 these fields have been initialized and don't need to be
237 recomputed. */
238 unsigned int reg_mask; /* non-volatile reg saves */
239 int arg_size; /* stdarg spills (bytes) */
240 int reg_size; /* non-volatile reg saves (bytes) */
241 int local_size; /* locals */
242 int outbound_size; /* arg overflow on calls out */
243 int frame_size; /* total static size of stack frame */
244 int local_offset;
245 int reg_offset;
246 int arg_offset;
247 int frame_init_p;
248
249 } machine_function;
250
251 /* These macros are for the func_type values above. */
252 #define CSKY_FT_TYPE_MASK ((1 << 3) - 1)
253 #define CSKY_FT_UNKNOWN 0 /* Type not been determined */
254 #define CSKY_FT_NORMAL 1 /* Normal function */
255 #define CSKY_FT_ISR 4 /* Interrupt service routine */
256 #define CSKY_FT_FIQ 5 /* Fast interrupt service routine */
257 #define CSKY_FT_EXCEPTION 6 /* Exception handler */
258 #define CSKY_FT_INTERRUPT (1 << 2) /* overlap CSKY_FT_ISR */
259 #define CSKY_FT_NAKED (1 << 3) /* No prologue and epilogue */
260 #define CSKY_FUNCTION_TYPE(t) ((t) & CSKY_FT_TYPE_MASK)
261 #define CSKY_FUNCTION_IS_INTERRUPT(t) ((t) & CSKY_FT_INTERRUPT)
262 #define CSKY_FUNCTION_IS_NAKED(t) ((t) & CSKY_FT_NAKED)
263
264 struct csky_processors
265 {
266 const char *const name;
267 enum csky_processor_type core;
268 const char *arch;
269 enum csky_base_architecture base_arch;
270 enum csky_isa_feature isa_bits[CSKY_ISA_FEATURE_GET (max)];
271 };
272
273 static struct csky_processors all_cores[] =
274 {
275 #undef CSKY_CORE
276 #define CSKY_CORE(NAME, CORE, X, ARCH, ISA) \
277 {NAME, TARGET_CPU_##CORE, #ARCH, CSKY_BASE_ARCH_##ARCH, \
278 {ISA CSKY_ISA_FEATURE_GET (none)}},
279 #include "csky_cores.def"
280 #undef CSKY_CORE
281 {NULL, TARGET_CPU_csky_none, NULL, CSKY_BASE_ARCH_NONE, \
282 {CSKY_ISA_FEATURE_GET (none)}}
283 };
284
285 static struct csky_processors all_architectures[] =
286 {
287 #undef CSKY_ARCH
288 #define CSKY_ARCH(NAME, CORE, ARCH, ISA) \
289 {NAME, TARGET_CPU_##CORE, #ARCH, CSKY_BASE_ARCH_##ARCH, \
290 {ISA CSKY_ISA_FEATURE_GET (none)}},
291 #include "csky_cores.def"
292 #undef CSKY_ARCH
293 {NULL, TARGET_CPU_csky_none, NULL, CSKY_BASE_ARCH_NONE, \
294 {CSKY_ISA_FEATURE_GET (none)}}
295 };
296
297 struct csky_fpu_desc
298 {
299 const char *name;
300 enum csky_isa_feature isa_bits[CSKY_ISA_FEATURE_GET (max)];
301 };
302
303 static const struct csky_fpu_desc all_fpus[] =
304 {
305 #undef CSKY_FPU
306 #define CSKY_FPU(NAME, CNAME, ISA) \
307 {NAME, {ISA CSKY_ISA_FEATURE_GET (none)}},
308 #include "csky_cores.def"
309 #undef CSKY_FPU
310 };
311
312 /* Active target architecture. */
313 struct csky_build_target
314 {
315 /* Name of the target CPU, if known, or NULL if the target CPU was not
316 specified by the user (and inferred from the -march option). */
317 const char *core_name;
318 /* Name of the target ARCH. NULL if there is a selected CPU. */
319 const char *arch_name;
320 /* Preprocessor substring (never NULL). */
321 const char *arch_pp_name;
322 /* CPU identifier for the core we're compiling for (architecturally). */
323 enum csky_processor_type arch_core;
324 /* The base architecture value. */
325 enum csky_base_architecture base_arch;
326 /* Bitmap encapsulating the isa_bits for the target environment. */
327 sbitmap isa;
328 };
329
330 struct csky_build_target csky_active_target;
331
332 /* The following are used in the .md file as equivalents to bits. */
333 int csky_arch_isa_features[CSKY_ISA_FEATURE_GET (max)] = {0};
334
335 /* The highest CSKY architecture version supported by the target. */
336 enum csky_base_architecture csky_base_arch = CSKY_TARGET_ARCH_GET (NONE);
337
338 /* Forward definitions of types. */
339 typedef struct minipool_node Mnode;
340 typedef struct minipool_fixup Mfix;
341
342 static GTY(()) int tls_labelno;
343
344
345 /* Maximum constant offset that can be added/subtracted from SP in a
346 single instruction. For ck801, this is for addsp/subsp, otherwise
347 it is the range of addi/subi. */
348 #define CSKY_MAX_SP_ADJUST \
349 (CSKY_TARGET_ARCH (CK801) ? 508 : 4096)
350
351
352 /* Implement TARGET_CPU_CPP_BUILTINS. */
353
354 #define builtin_define(MACRO) cpp_define (pfile, MACRO)
355
356 void
357 csky_cpu_cpp_builtins (cpp_reader *pfile)
358 {
359 const char *arch_name = csky_active_target.arch_pp_name;
360 char *pp_name = (char *) alloca (1 + strlen (arch_name) + 4);
361 sprintf (pp_name, "__%s__", arch_name);
362 builtin_define (pp_name);
363
364 builtin_define ("__csky__=2");
365 builtin_define ("__CSKY__=2");
366 builtin_define ("__ckcore__=2");
367 builtin_define ("__CKCORE__=2");
368
369 builtin_define ("__CSKYABIV2__");
370 builtin_define ("__cskyabiv2__");
371 builtin_define ("__CSKYABI__=2");
372 builtin_define ("__cskyabi__=2");
373
374 if (TARGET_BIG_ENDIAN)
375 {
376 builtin_define ("__ckcoreBE__");
377 builtin_define ("__cskyBE__");
378 builtin_define ("__cskybe__");
379 builtin_define ("__CSKYBE__");
380 }
381 else
382 {
383 builtin_define ("__ckcoreLE__");
384 builtin_define ("__cskyLE__");
385 builtin_define ("__cskyle__");
386 builtin_define ("__CSKYLE__");
387 }
388
389 if (TARGET_HARD_FLOAT)
390 {
391 builtin_define ("__csky_hard_float__");
392 builtin_define ("__CSKY_HARD_FLOAT__");
393 if (TARGET_HARD_FLOAT_ABI)
394 {
395 builtin_define ("__csky_hard_float_abi__");
396 builtin_define ("__CSKY_HARD_FLOAT_ABI__");
397 }
398 else
399 {
400 builtin_define ("__csky_soft_float_abi__");
401 builtin_define ("__CSKY_SOFT_FLOAT_ABI__");
402 }
403 if (TARGET_SINGLE_FPU)
404 {
405 builtin_define ("__csky_hard_float_fpu_sf__");
406 builtin_define ("__CSKY_HARD_FLOAT_FPU_SF__");
407 }
408 }
409 else
410 {
411 builtin_define ("__csky_soft_float__");
412 builtin_define ("__CSKY_SOFT_FLOAT__");
413 }
414
415 if (CSKY_ISA_FEATURE (fpv2_sf))
416 {
417 builtin_define ("__csky_fpuv2__");
418 builtin_define ("__CSKY_FPUV2__");
419 }
420
421 if (TARGET_SUPPORT_FPV3)
422 {
423 builtin_define ("__csky_fpuv3__");
424 builtin_define ("__CSKY_FPUV3__");
425 }
426
427 if (TARGET_ELRW)
428 {
429 builtin_define ("__csky_elrw__");
430 builtin_define ("__CSKY_ELRW__");
431 }
432 if (TARGET_ISTACK)
433 {
434 builtin_define ("__csky_istack__");
435 builtin_define ("__CSKY_ISTACK__");
436 }
437 if (TARGET_MP)
438 {
439 builtin_define ("__csky_mp__");
440 builtin_define ("__CSKY_MP__");
441 }
442 if (TARGET_CP)
443 {
444 builtin_define ("__csky_cp__");
445 builtin_define ("__CSKY_CP__");
446 }
447 if (TARGET_CACHE)
448 {
449 builtin_define ("__csky_cache__");
450 builtin_define ("__CSKY_CACHE__");
451 }
452 if (TARGET_SECURITY)
453 {
454 builtin_define ("__csky_security__");
455 builtin_define ("__CSKY_SECURITY__");
456 }
457 if (TARGET_TRUST)
458 {
459 builtin_define ("__csky_trust__");
460 builtin_define ("__CSKY_TRUST__");
461 }
462 if (TARGET_DSP)
463 {
464 builtin_define ("__csky_dsp__");
465 builtin_define ("__CSKY_DSP__");
466 }
467 if (TARGET_EDSP)
468 {
469 builtin_define ("__csky_edsp__");
470 builtin_define ("__CSKY_EDSP__");
471 }
472 if (TARGET_VDSP)
473 {
474 builtin_define ("__csky_vdsp__");
475 builtin_define ("__CSKY_VDSP__");
476 }
477 }
478
479
480 /******************************************************************
481 * Storage Layout *
482 ******************************************************************/
483
484 #undef TARGET_PROMOTE_FUNCTION_MODE
485 #define TARGET_PROMOTE_FUNCTION_MODE \
486 default_promote_function_mode_always_promote
487
488 #undef TARGET_CONSTANT_ALIGNMENT
489 #define TARGET_CONSTANT_ALIGNMENT csky_constant_alignment
490
491 #undef TARGET_MANGLE_TYPE
492 #define TARGET_MANGLE_TYPE csky_mangle_type
493
494
495 /******************************************************************
496 * Stack Layout and Calling Conventions *
497 ******************************************************************/
498
499 #undef TARGET_CAN_ELIMINATE
500 #define TARGET_CAN_ELIMINATE csky_can_eliminate
501
502 #undef TARGET_FUNCTION_ARG
503 #define TARGET_FUNCTION_ARG csky_function_arg
504
505 #undef TARGET_FUNCTION_ARG_ADVANCE
506 #define TARGET_FUNCTION_ARG_ADVANCE csky_function_arg_advance
507
508 #undef TARGET_FUNCTION_VALUE
509 #define TARGET_FUNCTION_VALUE csky_function_value
510
511 #undef TARGET_LIBCALL_VALUE
512 #define TARGET_LIBCALL_VALUE csky_libcall_value
513
514 #undef TARGET_FUNCTION_VALUE_REGNO_P
515 #define TARGET_FUNCTION_VALUE_REGNO_P csky_function_value_regno_p
516
517 #undef TARGET_SPLIT_COMPLEX_ARG
518 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
519
520 #undef TARGET_MUST_PASS_IN_STACK
521 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
522
523 #undef TARGET_ARG_PARTIAL_BYTES
524 #define TARGET_ARG_PARTIAL_BYTES csky_arg_partial_bytes
525
526 #undef TARGET_PASS_BY_REFERENCE
527 #define TARGET_PASS_BY_REFERENCE hook_pass_by_reference_must_pass_in_stack
528
529 #undef TARGET_ASM_OUTPUT_MI_THUNK
530 #define TARGET_ASM_OUTPUT_MI_THUNK csky_output_mi_thunk
531
532 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
533 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK \
534 hook_bool_const_tree_hwi_hwi_const_tree_true
535
536 #undef TARGET_ASM_FUNCTION_PROLOGUE
537 #define TARGET_ASM_FUNCTION_PROLOGUE csky_output_function_prologue
538
539 #undef TARGET_ASM_FUNCTION_EPILOGUE
540 #define TARGET_ASM_FUNCTION_EPILOGUE csky_output_function_epilogue
541
542 #undef TARGET_WARN_FUNC_RETURN
543 #define TARGET_WARN_FUNC_RETURN csky_warn_func_return
544
545 #undef TARGET_RETURN_IN_MEMORY
546 #define TARGET_RETURN_IN_MEMORY csky_return_in_memory
547
548
549 /******************************************************************
550 * Implementing the Varargs Macros *
551 ******************************************************************/
552
553
554 #undef TARGET_SETUP_INCOMING_VARARGS
555 #define TARGET_SETUP_INCOMING_VARARGS csky_setup_incoming_varargs
556
557
558 /******************************************************************
559 * Implicit Calls to Library Routines *
560 ******************************************************************/
561
562
563 #undef TARGET_INIT_LIBFUNCS
564 #define TARGET_INIT_LIBFUNCS csky_init_libfuncs
565
566
567 /******************************************************************
568 * Dividing the Output into Sections (Texts, Data, . . . ) *
569 ******************************************************************/
570
571
572 #undef TARGET_HAVE_TLS
573 #define TARGET_HAVE_TLS TARGET_CSKY_LINUX
574
575
576 /******************************************************************
577 * Defining target-specific uses of __attribute__ *
578 ******************************************************************/
579
580
581 #undef TARGET_ATTRIBUTE_TABLE
582 #define TARGET_ATTRIBUTE_TABLE csky_attribute_table
583
584 #undef TARGET_OPTION_OVERRIDE
585 #define TARGET_OPTION_OVERRIDE csky_option_override
586
587
588 /* Implement the BRANCH_COST target macro. */
589
590 int
591 csky_default_branch_cost (bool speed_p ATTRIBUTE_UNUSED,
592 bool predictable_p ATTRIBUTE_UNUSED)
593 {
594 return csky_branch_cost;
595 }
596
597 bool
598 csky_default_logical_op_non_short_circuit (void)
599 {
600 return BRANCH_COST (optimize_function_for_speed_p (cfun), false) >= 2;
601 }
602
603 /******************************************************************
604 * Register Usage *
605 ******************************************************************/
606
607 #undef TARGET_HARD_REGNO_NREGS
608 #define TARGET_HARD_REGNO_NREGS csky_hard_regno_nregs
609
610 #undef TARGET_HARD_REGNO_MODE_OK
611 #define TARGET_HARD_REGNO_MODE_OK csky_hard_regno_mode_ok
612
613 #undef TARGET_MODES_TIEABLE_P
614 #define TARGET_MODES_TIEABLE_P csky_modes_tieable_p
615
616 #undef TARGET_CONDITIONAL_REGISTER_USAGE
617 #define TARGET_CONDITIONAL_REGISTER_USAGE csky_conditional_register_usage
618
619 #undef TARGET_CLASS_LIKELY_SPILLED_P
620 #define TARGET_CLASS_LIKELY_SPILLED_P csky_class_likely_spilled_p
621
622 #undef TARGET_PREFERRED_RELOAD_CLASS
623 #define TARGET_PREFERRED_RELOAD_CLASS csky_preferred_reload_class
624
625 #undef TARGET_CLASS_MAX_NREGS
626 #define TARGET_CLASS_MAX_NREGS csky_class_max_nregs
627
628 #undef TARGET_SECONDARY_RELOAD
629 #define TARGET_SECONDARY_RELOAD csky_secondary_reload
630
631 #undef TARGET_SPILL_CLASS
632 #define TARGET_SPILL_CLASS csky_spill_class
633
634
635 /******************************************************************
636 * Addressing Modes *
637 ******************************************************************/
638
639
640 #undef TARGET_CANNOT_FORCE_CONST_MEM
641 #define TARGET_CANNOT_FORCE_CONST_MEM csky_cannot_force_const_mem
642
643 #undef TARGET_LEGITIMATE_CONSTANT_P
644 #define TARGET_LEGITIMATE_CONSTANT_P csky_legitimate_constant_p
645
646 #undef TARGET_LEGITIMIZE_ADDRESS
647 #define TARGET_LEGITIMIZE_ADDRESS csky_legitimize_address
648
649 #undef TARGET_LEGITIMATE_ADDRESS_P
650 #define TARGET_LEGITIMATE_ADDRESS_P csky_legitimate_address_p
651
652
653 /******************************************************************
654 * Others *
655 ******************************************************************/
656
657
658 #undef TARGET_CANNOT_COPY_INSN_P
659 #define TARGET_CANNOT_COPY_INSN_P csky_cannot_copy_insn_p
660
661
662 /******************************************************************
663 * Assembler Format *
664 ******************************************************************/
665
666
667 #undef TARGET_PRINT_OPERAND
668 #define TARGET_PRINT_OPERAND csky_print_operand
669
670 #undef TARGET_PRINT_OPERAND_ADDRESS
671 #define TARGET_PRINT_OPERAND_ADDRESS csky_print_operand_address
672
673 #undef TARGET_ASM_UNALIGNED_HI_OP
674 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
675
676 #undef TARGET_ASM_UNALIGNED_SI_OP
677 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
678
679 #undef TARGET_DWARF_REGISTER_SPAN
680 #define TARGET_DWARF_REGISTER_SPAN csky_dwarf_register_span
681
682
683 /******************************************************************
684 * Miscellaneous Parameters *
685 ******************************************************************/
686
687
688 #undef TARGET_MACHINE_DEPENDENT_REORG
689 #define TARGET_MACHINE_DEPENDENT_REORG csky_reorg
690
691 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
692 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS csky_allocate_stack_slots_for_args
693
694 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
695 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
696
697
698 /******************************************************************
699 * Trampolines for Nested Functions *
700 ******************************************************************/
701
702
703 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
704 #define TARGET_ASM_TRAMPOLINE_TEMPLATE csky_asm_trampoline_template
705 #undef TARGET_TRAMPOLINE_INIT
706 #define TARGET_TRAMPOLINE_INIT csky_trampoline_init
707
708 /* The low bit is ignored by jsr and jmp instructions so is safe to use. */
709 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
710 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
711
712 /******************************************************************
713 * Describing Relative Costs of Operations *
714 ******************************************************************/
715
716
717 #undef TARGET_REGISTER_MOVE_COST
718 #define TARGET_REGISTER_MOVE_COST csky_register_move_cost
719
720 #undef TARGET_MEMORY_MOVE_COST
721 #define TARGET_MEMORY_MOVE_COST csky_memory_move_cost
722
723 #undef TARGET_RTX_COSTS
724 #define TARGET_RTX_COSTS csky_rtx_costs
725
726 #undef TARGET_ADDRESS_COST
727 #define TARGET_ADDRESS_COST csky_address_cost
728
729
730 /******************************************************************
731 * Anchor address *
732 ******************************************************************/
733
734
735 /* FIXME: the max offset is related to mode size, the following is
736 defined according to SImode. How to deal with HImode and
737 QImode, and should the min offset be defined? */
738 #undef TARGET_MAX_ANCHOR_OFFSET
739 #define TARGET_MAX_ANCHOR_OFFSET \
740 ((TARGET_MINI_REGISTERS && optimize_size) ? 127 : 4095)
741
742
743 /******************************************************************
744 * Condition Code Status *
745 ******************************************************************/
746
747
748 #undef TARGET_FIXED_CONDITION_CODE_REGS
749 #define TARGET_FIXED_CONDITION_CODE_REGS csky_fixed_condition_code_regs
750
751
752 /******************************************************************
753 * Adjusting the Instruction Scheduler *
754 ******************************************************************/
755
756
757 #undef TARGET_SCHED_ISSUE_RATE
758 #define TARGET_SCHED_ISSUE_RATE csky_sched_issue_rate
759
760 #undef TARGET_SCHED_ADJUST_COST
761 #define TARGET_SCHED_ADJUST_COST csky_sched_adjust_cost
762
763
764 /******************************************************************
765 * Builtin *
766 ******************************************************************/
767
768
769 #undef TARGET_INIT_BUILTINS
770 #define TARGET_INIT_BUILTINS csky_init_builtins
771
772
773 /* The declaration of functions. */
774 static void push_csky_minipool_fix (rtx_insn *, HOST_WIDE_INT, rtx *,
775 machine_mode, rtx);
776 static void csky_print_operand (FILE *stream, rtx x, int code);
777
778
779 /* Define a table to map ISR attribute arguments onto function type
780 modifiers. */
781
782 typedef struct
783 {
784 const char *const arg;
785 const unsigned long return_value;
786 } isr_attribute_entry;
787
788 static const isr_attribute_entry isr_attribute_map[] =
789 {
790 {"irq", CSKY_FT_ISR },
791 {"IRQ", CSKY_FT_ISR },
792 {"fiq", CSKY_FT_FIQ },
793 {"FIQ", CSKY_FT_FIQ },
794 {NULL, CSKY_FT_NORMAL }
795 };
796
797
798 /* Return the function type of the current function, if it has not been
799 determined, return CSKY_FT_UNKNOWN. */
800
801 static unsigned long
802 get_csky_isr_type (tree argument)
803 {
804 const isr_attribute_entry *ptr;
805 const char *arg;
806
807 /* if argument is NULL, set default value ISR. */
808 if (argument == NULL_TREE)
809 return CSKY_FT_ISR;
810
811 if (TREE_VALUE (argument) == NULL_TREE
812 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
813 return CSKY_FT_UNKNOWN;
814
815 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
816
817 for (ptr = isr_attribute_map; ptr->arg != NULL; ptr++)
818 if (strcmp (arg, ptr->arg) == 0)
819 return ptr->return_value;
820
821 return CSKY_FT_UNKNOWN;
822 }
823
824 /* Classify cfun as a normal function or some sort of interrupt
825 handler, and set the corresponding bits in cfun->machine->func_type. */
826
827 static unsigned long
828 get_csky_current_func_type (void)
829 {
830 if (CSKY_FUNCTION_TYPE (cfun->machine->func_type) == CSKY_FT_UNKNOWN)
831 {
832 unsigned long type = CSKY_FT_UNKNOWN;
833 tree a;
834 tree attr;
835
836 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
837
838 attr = DECL_ATTRIBUTES (current_function_decl);
839 a = lookup_attribute ("naked", attr);
840 if (a != NULL_TREE)
841 type |= CSKY_FT_NAKED;
842 a = lookup_attribute ("isr", attr);
843 if (a == NULL_TREE)
844 a = lookup_attribute ("interrupt", attr);
845 if (a == NULL_TREE)
846 type |= CSKY_FT_NORMAL;
847 else
848 type |= get_csky_isr_type (TREE_VALUE (a));
849
850 cfun->machine->func_type = type;
851 }
852
853 return cfun->machine->func_type;
854 }
855
856 /* These typedefs are located at the start of this file, so that
857 they can be used in the prototypes there. This comment is to
858 remind readers of that fact so that the following structures
859 can be understood more easily.
860
861 typedef struct minipool_node Mnode;
862 typedef struct minipool_fixup Mfix; */
863
864 struct minipool_node
865 {
866 /* Doubly linked chain of entries. */
867 Mnode *next;
868 Mnode *prev;
869 /* The maximum offset into the code that this entry can be placed. While
870 pushing fixes for forward references, all entries are sorted in order
871 of increasing max_address. */
872 HOST_WIDE_INT max_address;
873 /* Similarly for an entry inserted for a backwards ref. */
874 HOST_WIDE_INT min_address;
875 /* The number of fixes referencing this entry. This can become zero
876 if we "unpush" an entry. In this case we ignore the entry when we
877 come to emit the code. */
878 int refcount;
879 /* The offset from the start of the minipool. */
880 HOST_WIDE_INT offset;
881 /* The value in table. */
882 rtx value;
883 /* The mode of value. */
884 machine_mode mode;
885 /* The size of the value. */
886 int fix_size;
887 };
888
889 struct minipool_fixup
890 {
891 Mfix *next;
892 rtx_insn *insn;
893 HOST_WIDE_INT address;
894 rtx *loc;
895 machine_mode mode;
896 int fix_size;
897 rtx value;
898 Mnode *minipool;
899 HOST_WIDE_INT forwards;
900 HOST_WIDE_INT backwards;
901 };
902
903 static Mnode *minipool_vector_head;
904 static Mnode *minipool_vector_tail;
905 static rtx minipool_vector_label;
906 static HOST_WIDE_INT constpool_label_no = 0;
907
908 /* Obstack for minipool constant handling. */
909 static struct obstack minipool_obstack;
910 static char *minipool_startobj;
911 /* The linked list of all minipool fixes required for this function. */
912 Mfix *minipool_fix_head;
913 Mfix *minipool_fix_tail;
914 /* The fix entry for the current minipool, once it has been placed. */
915 Mfix *minipool_barrier;
916
917 /* Allow GC scanning of the minipool obstack. */
918
919 static void
920 csky_add_gc_roots (void)
921 {
922 gcc_obstack_init (&minipool_obstack);
923 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
924 }
925
926 /* Implement TARGET_CONSTANT_ALIGNMENT.
927 Make strings word-aligned so strcpy from constants will be faster. */
928
929 static HOST_WIDE_INT
930 csky_constant_alignment (const_tree exp, HOST_WIDE_INT align)
931 {
932 if (TREE_CODE (exp) == STRING_CST
933 && !optimize_size
934 && align < BITS_PER_WORD)
935 return BITS_PER_WORD;
936 return align;
937 }
938
939 /* Record that there is a natural barrier in the insn stream at
940 ADDRESS. */
941
942 static void
943 push_csky_minipool_barrier (rtx_insn *insn, HOST_WIDE_INT address)
944 {
945 Mfix *fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (*fix));
946
947 fix->insn = insn;
948 fix->address = address;
949
950 fix->next = NULL;
951 if (minipool_fix_head != NULL)
952 minipool_fix_tail->next = fix;
953 else
954 minipool_fix_head = fix;
955
956 minipool_fix_tail = fix;
957 }
958
959 /* Compute the size of a vector jump table. */
960
961 static HOST_WIDE_INT
962 get_csky_jump_table_size (rtx insn)
963 {
964 /* ADDR_VECs only take room if read-only data does into the text
965 section. */
966 if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
967 {
968 rtx body = PATTERN (insn);
969 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
970 HOST_WIDE_INT size;
971 HOST_WIDE_INT modesize;
972
973 modesize = GET_MODE_SIZE (GET_MODE (body));
974 size = modesize * XVECLEN (body, elt);
975 switch (modesize)
976 {
977 case 1:
978 /* Round up size of TBB table to a halfword boundary. */
979 size = (size + 1) & ~(HOST_WIDE_INT)1;
980 break;
981 case 2:
982 /* No padding necessary for TBH. */
983 break;
984 case 4:
985 break;
986 default:
987 gcc_unreachable ();
988 }
989 return size;
990 }
991
992 return 0;
993 }
994
995
996 /* Scan INSN and note any of its operands that need fixing.
997 If DO_PUSHES is false we do not actually push any of the fixups
998 needed. The function returns TRUE if any fixups were needed/pushed. */
999
1000 static bool
1001 note_csky_invalid_constants (rtx_insn *insn, HOST_WIDE_INT address,
1002 int do_pushes)
1003 {
1004 bool result = false;
1005 int opno;
1006
1007 extract_constrain_insn (insn);
1008
1009 if (recog_data.n_alternatives == 0)
1010 return false;
1011
1012 /* Fill in recog_op_alt with information about the constraints of
1013 this insn. */
1014 preprocess_constraints (insn);
1015
1016 const operand_alternative *op_alt = which_op_alt ();
1017 for (opno = 0; opno < recog_data.n_operands; opno++)
1018 {
1019 /* Things we need to fix can only occur in inputs. */
1020 if (recog_data.operand_type[opno] != OP_IN)
1021 continue;
1022
1023 /* If this alternative is a memory reference, then any mention
1024 of constants in this alternative is really to fool reload
1025 into allowing us to accept one there. We need to fix them up
1026 now so that we output the right code. */
1027 if (op_alt[opno].memory_ok)
1028 {
1029 rtx op = recog_data.operand[opno];
1030
1031 if (CONSTANT_P (op))
1032 {
1033 if (do_pushes)
1034 push_csky_minipool_fix (insn, address,
1035 recog_data.operand_loc[opno],
1036 recog_data.operand_mode[opno], op);
1037 result = true;
1038 }
1039 }
1040 }
1041
1042 return result;
1043 }
1044
1045
1046 /* Add a constant to the minipool for a forward reference. Returns the
1047 node added or NULL if the constant will not fit in this pool. */
1048
1049 static Mnode *
1050 add_csky_minipool_forward_ref (Mfix *fix)
1051 {
1052 /* If set, max_mp is the first pool_entry that has a lower
1053 constraint than the one we are trying to add. */
1054 Mnode *max_mp = NULL;
1055 HOST_WIDE_INT max_address = fix->address + fix->forwards;
1056 Mnode *mp;
1057
1058 /* If the minipool starts before the end of FIX->INSN then this FIX
1059 cannot be placed into the current pool. Furthermore, adding the
1060 new constant pool entry may cause the pool to start FIX_SIZE bytes
1061 earlier. */
1062 if (minipool_vector_head
1063 && (fix->address + get_attr_length (fix->insn)
1064 >= minipool_vector_head->max_address - fix->fix_size))
1065 return NULL;
1066
1067 /* Scan the pool to see if a constant with the same value has
1068 already been added. While we are doing this, also note the
1069 location where we must insert the constant if it doesn't already
1070 exist. */
1071 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
1072 {
1073 if (GET_CODE (fix->value) == GET_CODE (mp->value)
1074 && fix->mode == mp->mode
1075 && (GET_CODE (fix->value) != CODE_LABEL
1076 || (CODE_LABEL_NUMBER (fix->value)
1077 == CODE_LABEL_NUMBER (mp->value)))
1078 && rtx_equal_p (fix->value, mp->value))
1079 {
1080 /* More than one fix references this entry. */
1081 mp->refcount++;
1082 return mp;
1083 }
1084
1085 /* Note the insertion point if necessary. */
1086 if (max_mp == NULL && mp->max_address > max_address)
1087 max_mp = mp;
1088 }
1089
1090 /* The value is not currently in the minipool, so we need to create
1091 a new entry for it. If MAX_MP is NULL, the entry will be put on
1092 the end of the list since the placement is less constrained than
1093 any existing entry. Otherwise, we insert the new fix before
1094 MAX_MP and, if necessary, adjust the constraints on the other
1095 entries. */
1096 mp = XNEW (Mnode);
1097 mp->fix_size = fix->fix_size;
1098 mp->mode = fix->mode;
1099 mp->value = fix->value;
1100 mp->refcount = 1;
1101 /* Not yet required for a backwards ref. */
1102 mp->min_address = -65536;
1103
1104 if (max_mp == NULL)
1105 {
1106 mp->max_address = max_address;
1107 mp->next = NULL;
1108 mp->prev = minipool_vector_tail;
1109
1110 if (mp->prev == NULL)
1111 {
1112 minipool_vector_head = mp;
1113 minipool_vector_label
1114 = gen_csky_constpool_label (gen_rtx_CONST_INT (VOIDmode,
1115 constpool_label_no++));
1116 }
1117 else
1118 mp->prev->next = mp;
1119
1120 minipool_vector_tail = mp;
1121 }
1122 else
1123 {
1124 if (max_address > max_mp->max_address - mp->fix_size)
1125 mp->max_address = max_mp->max_address - mp->fix_size;
1126 else
1127 mp->max_address = max_address;
1128
1129 mp->next = max_mp;
1130 mp->prev = max_mp->prev;
1131 max_mp->prev = mp;
1132 if (mp->prev != NULL)
1133 mp->prev->next = mp;
1134 else
1135 minipool_vector_head = mp;
1136 }
1137
1138 /* Save the new entry. */
1139 max_mp = mp;
1140
1141 /* Scan over the preceding entries and adjust their addresses as
1142 required. */
1143 while (mp->prev != NULL
1144 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
1145 {
1146 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
1147 mp = mp->prev;
1148 }
1149
1150 return max_mp;
1151 }
1152
1153
1154 /* Return the cost of forcibly inserting a barrier after INSN. */
1155
1156 static int
1157 get_csky_barrier_cost (rtx_insn *insn)
1158 {
1159 /* Basing the location of the pool on the loop depth is preferable,
1160 but at the moment, the basic block information seems to be
1161 corrupt by this stage of the compilation. */
1162 int base_cost = 50;
1163 rtx next = next_nonnote_insn (insn);
1164
1165 if (next != NULL && GET_CODE (next) == CODE_LABEL)
1166 base_cost -= 20;
1167
1168 switch (GET_CODE (insn))
1169 {
1170 case CODE_LABEL:
1171 /* It will always be better to place the table before the label, rather
1172 than after it. */
1173 return 50;
1174
1175 case INSN:
1176 case CALL_INSN:
1177 return base_cost;
1178
1179 case JUMP_INSN:
1180 return base_cost - 10;
1181
1182 default:
1183 return base_cost + 10;
1184 }
1185 }
1186
1187
1188 /* Find the best place in the insn stream in the range
1189 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
1190 Create the barrier by inserting a jump and add a new fix entry for
1191 it. */
1192
1193 static Mfix *
1194 create_csky_fix_barrier (Mfix *fix, Mfix *fix_next,
1195 HOST_WIDE_INT max_address)
1196 {
1197 rtx_barrier *barrier;
1198 rtx_insn *from = (fix ? fix->insn : get_insns ());
1199 /* The instruction after which we will insert the jump. */
1200 rtx_insn *selected = NULL;
1201 int selected_cost;
1202 /* The address at which the jump instruction will be placed. */
1203 HOST_WIDE_INT selected_address = 0;
1204 Mfix *new_fix;
1205 HOST_WIDE_INT count = (fix ? fix->address : 0);
1206 HOST_WIDE_INT max_count = max_address;
1207 rtx_code_label *label = gen_label_rtx ();
1208
1209 selected_cost = get_csky_barrier_cost (from);
1210
1211 while (from && count < max_count)
1212 {
1213 int new_cost;
1214 rtx_jump_table_data *table;
1215
1216 /* Count the length of this insn. */
1217 count += get_attr_length (from);
1218
1219 /* If there is a jump table, add its length. */
1220 if (tablejump_p (from, NULL, &table))
1221 {
1222 count += get_csky_jump_table_size (table);
1223
1224 /* Jump tables aren't in a basic block, so base the cost on
1225 the dispatch insn. If we select this location, we will
1226 still put the pool after the table. */
1227 new_cost = get_csky_barrier_cost (from);
1228
1229 if (count < max_count
1230 && (!selected || new_cost <= selected_cost))
1231 {
1232 selected = table;
1233 selected_cost = new_cost;
1234 selected_address = count;
1235 }
1236
1237 /* Continue after the dispatch table. */
1238 from = NEXT_INSN (table);
1239 continue;
1240 }
1241
1242 new_cost = get_csky_barrier_cost (from);
1243
1244 if (count < max_count
1245 && (!selected || new_cost <= selected_cost))
1246 {
1247 selected = from;
1248 selected_cost = new_cost;
1249 selected_address = count;
1250 }
1251
1252 from = NEXT_INSN (from);
1253 }
1254
1255 /* Make sure that we found a place to insert the jump. */
1256 gcc_assert (selected);
1257
1258 /* Create a new JUMP_INSN that branches around a barrier. */
1259 from = emit_jump_insn_after (gen_jump (label), selected);
1260 JUMP_LABEL (from) = label;
1261 barrier = emit_barrier_after (from);
1262 emit_label_after (label, barrier);
1263
1264 /* Create a minipool barrier entry for the new barrier. */
1265 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
1266 new_fix->insn = barrier;
1267 new_fix->address = selected_address;
1268 if (fix)
1269 {
1270 new_fix->next = fix->next;
1271 fix->next = new_fix;
1272 }
1273 else
1274 new_fix->next = fix_next;
1275
1276 return new_fix;
1277 }
1278
1279
1280 /* Print a symbolic form of the constant X to the dump file F.
1281 This is used for dump output for -mconstpool in the target-dependent
1282 reorg pass. */
1283
1284 static void
1285 print_csky_value (FILE *f, rtx x)
1286 {
1287 switch (GET_CODE (x))
1288 {
1289 case CONST_INT:
1290 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
1291 return;
1292
1293 case CONST_DOUBLE:
1294 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
1295 return;
1296
1297 case CONST_VECTOR:
1298 {
1299 int i;
1300
1301 fprintf (f, "<");
1302 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
1303 {
1304 fprintf (f, HOST_WIDE_INT_PRINT_HEX,
1305 INTVAL (CONST_VECTOR_ELT (x, i)));
1306 if (i < (CONST_VECTOR_NUNITS (x) - 1))
1307 fputc (',', f);
1308 }
1309 fprintf (f, ">");
1310 }
1311 return;
1312
1313 case CONST_STRING:
1314 fprintf (f, "\"%s\"", XSTR (x, 0));
1315 return;
1316
1317 case SYMBOL_REF:
1318 fprintf (f, "`%s'", XSTR (x, 0));
1319 return;
1320
1321 case LABEL_REF:
1322 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
1323 return;
1324
1325 case CONST:
1326 print_csky_value (f, XEXP (x, 0));
1327 return;
1328
1329 case PLUS:
1330 print_csky_value (f, XEXP (x, 0));
1331 fprintf (f, "+");
1332 print_csky_value (f, XEXP (x, 1));
1333 return;
1334
1335 case PC:
1336 fprintf (f, "pc");
1337 return;
1338
1339 default:
1340 fprintf (f, "????");
1341 return;
1342 }
1343 }
1344
1345
1346 /* Record INSN, which will need fixing up to load a value from the
1347 minipool. ADDRESS is the offset of the insn since the start of the
1348 function; LOC is a pointer to the part of the insn which requires
1349 fixing; VALUE is the constant that must be loaded, which is of type
1350 MODE. */
1351
1352 static void
1353 push_csky_minipool_fix (rtx_insn *insn, HOST_WIDE_INT address, rtx *loc,
1354 machine_mode mode, rtx value)
1355 {
1356 #define CSKY_ELRW16_RANGE 1400
1357 #define CSKY_LRW16_RANGE 700
1358 #define CSKY_CONSTANT_POOL_RANGE (TARGET_ELRW ? CSKY_ELRW16_RANGE \
1359 : CSKY_LRW16_RANGE)
1360
1361 /* Fixes less than a word need padding out to a word boundary. */
1362 #define CSKY_MINIPOOL_FIX_SIZE(mode) \
1363 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
1364
1365 Mfix *fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (*fix));
1366
1367 fix->insn = insn;
1368 fix->address = address;
1369 fix->loc = loc;
1370 fix->mode = mode;
1371 fix->fix_size = CSKY_MINIPOOL_FIX_SIZE (mode);
1372 fix->value = value;
1373 fix->forwards = CSKY_CONSTANT_POOL_RANGE;
1374 fix->backwards = 0;
1375 fix->minipool = NULL;
1376
1377 /* If an insn doesn't have a range defined for it, then it isn't
1378 expecting to be reworked by this code. Better to stop now than
1379 to generate duff assembly code. */
1380 gcc_assert (fix->forwards || fix->backwards);
1381
1382 if (dump_file)
1383 {
1384 fprintf (dump_file,
1385 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
1386 GET_MODE_NAME (mode),
1387 INSN_UID (insn), (unsigned long) address,
1388 -1 * (long)fix->backwards, (long)fix->forwards);
1389 print_csky_value (dump_file, fix->value);
1390 fprintf (dump_file, "\n");
1391 }
1392
1393 /* Add it to the chain of fixes. */
1394 fix->next = NULL;
1395
1396 if (minipool_fix_head != NULL)
1397 minipool_fix_tail->next = fix;
1398 else
1399 minipool_fix_head = fix;
1400
1401 minipool_fix_tail = fix;
1402 }
1403
1404
1405 /* Fill in the offsets for minipool entries. */
1406
1407 static void
1408 assign_csky_minipool_offsets (Mfix *barrier)
1409 {
1410 HOST_WIDE_INT offset = 0;
1411 Mnode *mp;
1412
1413 minipool_barrier = barrier;
1414
1415 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
1416 {
1417 mp->offset = offset;
1418
1419 if (mp->refcount > 0)
1420 offset += mp->fix_size;
1421 }
1422 }
1423
1424
1425 /* Output the literal table. */
1426
1427 static HOST_WIDE_INT
1428 dump_csky_minipool (rtx_insn *scan)
1429 {
1430 Mnode *mp;
1431 Mnode *nmp;
1432 HOST_WIDE_INT pool_length = 0;
1433
1434 if (dump_file)
1435 fprintf (dump_file,
1436 ";; Emitting minipool after insn %u;\
1437 address %ld; align %d (bytes)\n",
1438 INSN_UID (scan), (unsigned long) minipool_barrier->address, 4);
1439
1440 scan = emit_insn_after (gen_align_4 (), scan);
1441 scan = emit_insn_after (minipool_vector_label, scan);
1442
1443 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
1444 {
1445 if (mp->refcount > 0)
1446 {
1447 if (dump_file)
1448 {
1449 fprintf (dump_file, ";; Offset %u, min %ld, max %ld ",
1450 (unsigned) mp->offset, (unsigned long) mp->min_address,
1451 (unsigned long) mp->max_address);
1452 print_csky_value (dump_file, mp->value);
1453 fputc ('\n', dump_file);
1454 }
1455
1456 switch (mp->fix_size)
1457 {
1458 case 4:
1459 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
1460 pool_length += 4;
1461 break;
1462 case 8:
1463 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
1464 pool_length += 8;
1465 break;
1466 default:
1467 gcc_unreachable ();
1468 }
1469 }
1470
1471 nmp = mp->next;
1472 free (mp);
1473 }
1474
1475 minipool_vector_head = minipool_vector_tail = NULL;
1476 scan = emit_barrier_after (scan);
1477
1478 return pool_length;
1479 }
1480
1481 /* Return true if INSN is a minipool load or instruction that will be
1482 converted to one. It is assumed that INSN has type attribute "load". */
1483
1484 bool
1485 csky_minipool_load_p (rtx_insn *insn)
1486 {
1487 rtx op1, addr;
1488
1489 extract_insn_cached (insn);
1490
1491 op1 = recog_data.operand[1];
1492
1493 /* This is a constant that has not yet been turned into
1494 a minipool load. */
1495 if (CONSTANT_P (op1))
1496 return true;
1497
1498 /* Constant pool loads are label_refs. */
1499 if (GET_CODE (op1) == ZERO_EXTEND || GET_CODE (op1) == SIGN_EXTEND)
1500 op1 = XEXP (op1, 0);
1501 if (GET_CODE (op1) != MEM)
1502 return false;
1503 addr = XEXP (op1, 0);
1504 if (GET_CODE (addr) == PLUS && CONST_INT_P (XEXP (addr, 1)))
1505 addr = XEXP (addr, 0);
1506 return GET_CODE (addr) == LABEL_REF;
1507 }
1508
1509
1510 /* Compute the attribute "length" of push or pop insn, according to
1511 the registers it uses. */
1512
1513 int
1514 csky_compute_pushpop_length (rtx *operands)
1515 {
1516 rtx parallel_op = operands[2];
1517 /* Initialize to elements number of PARALLEL. */
1518 unsigned indx = XVECLEN (parallel_op, 0) - 1;
1519 unsigned first_indx = 0;
1520 unsigned regno = REGNO (operands[1]);
1521
1522 if (regno > CSKY_LR_REGNUM)
1523 return 4;
1524
1525 /* Check each register in the list. */
1526 for (; indx > first_indx; indx--)
1527 {
1528 regno = REGNO (XEXP (XVECEXP (parallel_op, 0, indx), 0));
1529 /* If a register number higher than 15 is included, a 32-bit insn
1530 is used. */
1531 if (regno > CSKY_LR_REGNUM)
1532 return 4;
1533 }
1534
1535 return 2;
1536 }
1537
1538 /* Emit constant pools for -mconstpool. */
1539
1540 static void
1541 csky_emit_constant_pools (void)
1542 {
1543 rtx_insn *insn;
1544 HOST_WIDE_INT address = 0;
1545 Mfix *fix;
1546
1547 minipool_fix_head = minipool_fix_tail = NULL;
1548
1549 /* The first insn must always be a note, or the code below won't
1550 scan it properly. */
1551 insn = get_insns ();
1552 gcc_assert (NOTE_P (insn));
1553
1554 /* Scan the insns and record the operands that need fixing. */
1555 for (insn = next_nonnote_insn (insn); insn;
1556 insn = next_nonnote_insn (insn))
1557 {
1558 if (BARRIER_P (insn))
1559 push_csky_minipool_barrier (insn, address);
1560 else if (INSN_P (insn))
1561 {
1562 rtx_jump_table_data *table;
1563
1564 note_csky_invalid_constants (insn, address, true);
1565 address += get_attr_length (insn);
1566
1567 /* If the insn is a vector jump, add the size of the table
1568 and skip the table. */
1569 if (tablejump_p (insn, NULL, &table))
1570 {
1571 address += get_csky_jump_table_size (table);
1572 insn = table;
1573 }
1574 }
1575 }
1576
1577 fix = minipool_fix_head;
1578
1579 /* Now scan the fixups and perform the required changes. */
1580 while (fix)
1581 {
1582 Mfix *ftmp;
1583 Mfix *last_added_fix;
1584 Mfix *last_barrier = NULL;
1585 Mfix *this_fix;
1586 Mnode *mp;
1587 bool has_pending_const = false;
1588
1589 /* Check if there is any pending constant not processed. */
1590 for (mp = minipool_vector_head; mp; mp = mp->next)
1591 if (mp->refcount > 0)
1592 {
1593 has_pending_const = true;
1594 break;
1595 }
1596
1597 /* If no pending constant, skip over barrier insns. */
1598 if (has_pending_const == false)
1599 {
1600 while (fix && BARRIER_P (fix->insn))
1601 fix = fix->next;
1602 if (fix == NULL)
1603 break;
1604 }
1605
1606 last_added_fix = NULL;
1607
1608 for (ftmp = fix; ftmp; ftmp = ftmp->next)
1609 {
1610 if (BARRIER_P (ftmp->insn))
1611 {
1612 if (minipool_vector_head
1613 && ftmp->address >= minipool_vector_head->max_address)
1614 break;
1615
1616 last_barrier = ftmp;
1617 }
1618 else
1619 {
1620 ftmp->minipool = add_csky_minipool_forward_ref (ftmp);
1621 if (ftmp->minipool == NULL)
1622 break;
1623 }
1624 last_added_fix = ftmp; /* Keep track of the last fix added. */
1625 }
1626
1627 /* If the last added fix is a barrier, dump minipool after it. */
1628 if (last_added_fix && BARRIER_P (last_added_fix->insn))
1629 ftmp = last_barrier;
1630 else
1631 {
1632 /* ftmp is first fix that we can't fit into this pool.
1633 Insert a new barrier in the code somewhere between the previous
1634 fix and this one, and arrange to jump around it. */
1635 HOST_WIDE_INT max_address;
1636
1637 /* The last item on the list of fixes must be a barrier, so
1638 we can never run off the end of the list of fixes without
1639 last_barrier being set. */
1640 gcc_assert (ftmp);
1641
1642 /* Check that there isn't another fix that is in range that
1643 we couldn't fit into this pool because the pool was
1644 already too large: we need to put the pool before such an
1645 instruction. The pool itself may come just after the
1646 fix because create_csky_fix_barrier also allows space for a
1647 jump instruction. */
1648 max_address = minipool_vector_head->max_address;
1649 if (ftmp->address < max_address)
1650 max_address = ftmp->address + 1;
1651 last_barrier = create_csky_fix_barrier (last_added_fix, ftmp,
1652 max_address);
1653 }
1654
1655 assign_csky_minipool_offsets (last_barrier);
1656
1657 /* Scan over the fixes we have identified for this pool, fixing them
1658 up and adding the constants to the pool itself. */
1659 for (this_fix = fix; this_fix && ftmp != this_fix;
1660 this_fix = this_fix->next)
1661 {
1662 if (GET_CODE (this_fix->insn) != BARRIER)
1663 {
1664 rtx addr
1665 = plus_constant (Pmode,
1666 gen_rtx_LABEL_REF (VOIDmode,
1667 minipool_vector_label),
1668 this_fix->minipool->offset);
1669 rtx insn_body = PATTERN (this_fix->insn);
1670 rtx src = XEXP (insn_body, 1);
1671 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
1672 if (GET_CODE (this_fix->value) == SYMBOL_REF)
1673 emit_insn_after (gen_rtx_UNSPEC_VOLATILE (VOIDmode,
1674 gen_rtvec (1, src),
1675 VUNSPEC_SYMBOL_REF),
1676 this_fix->insn);
1677 }
1678 }
1679 dump_csky_minipool (last_barrier->insn);
1680 fix = ftmp;
1681 if (fix->next == NULL)
1682 break;
1683 }
1684
1685 /* Free the minipool memory. */
1686 obstack_free (&minipool_obstack, minipool_startobj);
1687 }
1688
1689
1690 /* Implement TARGET_MACHINE_DEPENDENT_REORG. This handles
1691 -mconstpool output. */
1692
1693 static void
1694 csky_reorg (void)
1695 {
1696 if (TARGET_CONSTANT_POOL)
1697 csky_emit_constant_pools ();
1698 }
1699
1700
1701 /* Check to see if the current function contains a branch insn with the
1702 far jump attribute set. Such a function uses the LR register. */
1703
1704 static bool
1705 csky_far_jump_used_p (void)
1706 {
1707 rtx_insn *insn;
1708 if (cfun->machine->far_jump_used)
1709 return true;
1710
1711 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1712 if (GET_CODE (insn) == JUMP_INSN
1713 /* Ignore tablejump patterns. */
1714 && GET_CODE (PATTERN (insn)) != ADDR_VEC
1715 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
1716 && get_attr_far_jump (insn) == FAR_JUMP_YES)
1717 {
1718 cfun->machine->far_jump_used = 1;
1719 return true;
1720 }
1721 return false;
1722 }
1723
1724
1725 /* Return the mask of registers used by the current function. Set
1726 COUNT to the number of registers used. */
1727
1728 static unsigned int
1729 get_csky_live_regs (int *count)
1730 {
1731 int reg;
1732 unsigned int live_regs_mask = 0;
1733
1734 *count = 0;
1735 for (reg = 0; reg < CSKY_NGPR_REGS; reg++)
1736 {
1737 bool save = false;
1738
1739 /* Ignore unsupported registers. */
1740 if (CSKY_TARGET_ARCH (CK801) && reg > 8 && reg < 13)
1741 continue;
1742 if ((CSKY_TARGET_ARCH (CK801)
1743 || CSKY_TARGET_ARCH (CK802)
1744 || CSKY_TARGET_ARCH (CK803))
1745 && reg > 15)
1746 break;
1747
1748 /* Caller-saved registers marked as used. */
1749 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
1750 save = true;
1751
1752 /* Frame pointer marked used. */
1753 else if (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
1754 save = true;
1755
1756 /* This is required for CK801/802 where FP is a fixed reg, otherwise
1757 we end up with no FP value available to the DWARF-2 unwinder. */
1758 else if (crtl->calls_eh_return && reg == HARD_FRAME_POINTER_REGNUM)
1759 save = true;
1760
1761 /* CK801/802 also need special handling for LR because it's clobbered
1762 by far jumps. */
1763 else if ((CSKY_TARGET_ARCH (CK801) || CSKY_TARGET_ARCH (CK802))
1764 && reg == CSKY_LR_REGNUM
1765 && (!crtl->is_leaf || csky_far_jump_used_p ()))
1766 save = true;
1767
1768 /* Register is used for EH data return. */
1769 else if (crtl->calls_eh_return
1770 && reg >= CSKY_FIRST_EH_RETDATA_REGNUM
1771 && reg <= CSKY_LAST_EH_RETDATA_REGNUM)
1772 save = true;
1773
1774 /* We need a temporary reg to hold the offset for adjusting the SP
1775 for a large stack frame. */
1776 if (reg == CSKY_STACKADJUST_REGNUM
1777 && cfun->machine->reg_offset > CSKY_MAX_SP_ADJUST * 2)
1778 save = true;
1779
1780 /* Add reg to the mask. */
1781 if (save)
1782 {
1783 (*count)++;
1784 live_regs_mask |= (1 << reg);
1785 }
1786 }
1787 return live_regs_mask;
1788 }
1789
1790 /* Compute the stack frame layout, storing sizes of the various pieces
1791 in cfun->machine.
1792
1793 Stack frames constructed in the prologue look like:
1794 ... caller's frame ...
1795 incoming SP -> caller's outbound argument overflow
1796 argument spill
1797 optional FP -> register save
1798 local variables
1799 alloca() space
1800 adjusted SP -> outbound argument overflow
1801
1802 with SP/FP pointing at the base (low address) of the respective area,
1803 and each area aligned to a word boundary. */
1804
1805 static void
1806 csky_layout_stack_frame (void)
1807 {
1808 machine_function *infp = cfun->machine;
1809 int reg_count;
1810
1811 if (infp->frame_init_p)
1812 return;
1813
1814 /* Get sizes of local variables & outbound arguments. */
1815 infp->outbound_size = CSKY_STACK_ALIGN (crtl->outgoing_args_size);
1816 infp->local_offset = infp->outbound_size;
1817 infp->local_size = CSKY_STACK_ALIGN (get_frame_size ());
1818 infp->reg_offset = infp->local_offset + infp->local_size;
1819
1820 /* Now compute size of argument spill + saved regs. These do not
1821 need explicit alignment since they are already word-sized. */
1822 infp->reg_mask = get_csky_live_regs (&reg_count);
1823 infp->reg_size = reg_count * UNITS_PER_WORD;
1824 infp->arg_offset = infp->reg_offset + infp->reg_size;
1825 infp->arg_size = crtl->args.pretend_args_size;
1826 infp->frame_size = infp->arg_offset + infp->arg_size;
1827 infp->frame_init_p = reload_completed;
1828 }
1829
1830 /* Implement TARGET_CAN_ELIMINATE. */
1831 static bool
1832 csky_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1833 {
1834 if (to == FRAME_POINTER_REGNUM)
1835 return from != ARG_POINTER_REGNUM;
1836 if (to == STACK_POINTER_REGNUM)
1837 return !frame_pointer_needed;
1838 return true;
1839 }
1840
1841 /* Worker function for INITIAL_ELIMINATION_OFFSET macro.
1842 Define the offset between two registers, one to be eliminated, and
1843 the other its replacement, at the start of a routine. */
1844
1845 HOST_WIDE_INT
1846 csky_initial_elimination_offset (int from, int to)
1847 {
1848 int offset;
1849
1850 csky_layout_stack_frame ();
1851
1852 /* Set OFFSET to the offset to the initial stack pointer. */
1853 switch (from)
1854 {
1855 case FRAME_POINTER_REGNUM:
1856 case HARD_FRAME_POINTER_REGNUM:
1857 offset = cfun->machine->reg_offset;
1858 break;
1859
1860 case ARG_POINTER_REGNUM:
1861 offset = cfun->machine->arg_offset;
1862 break;
1863
1864 default:
1865 gcc_unreachable ();
1866 }
1867
1868 /* If we are asked for the offset to the frame pointer instead,
1869 then subtract the difference between the frame pointer and stack
1870 pointer. */
1871 if (to == FRAME_POINTER_REGNUM || to == HARD_FRAME_POINTER_REGNUM)
1872 offset -= cfun->machine->reg_offset;
1873 return offset;
1874 }
1875
1876
1877 /* Determine where to put an argument to a function.
1878 Value is zero to push the argument on the stack,
1879 or a hard register in which to store the argument.
1880
1881 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1882 the preceding args and about the function being called.
1883 ARG is a description of the argument. */
1884
1885 static rtx
1886 csky_function_arg (cumulative_args_t pcum_v, const function_arg_info &arg)
1887 {
1888 CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
1889 int reg = pcum->reg;
1890 machine_mode mode = arg.mode;
1891
1892 if (FUNCTION_VARG_MODE_P(mode)
1893 && !pcum->is_stdarg)
1894 {
1895 reg = pcum->freg;
1896
1897 if (reg < CSKY_NPARM_FREGS)
1898 return gen_rtx_REG (mode, CSKY_FIRST_VFP_REGNUM + reg);
1899 else
1900 return NULL_RTX;
1901 }
1902
1903 if (reg < CSKY_NPARM_REGS)
1904 return gen_rtx_REG (mode, CSKY_FIRST_PARM_REGNUM + reg);
1905
1906 return NULL_RTX;
1907 }
1908
1909
1910 /* Return the number of registers (words) needed to pass an argument of
1911 MODE and TYPE. */
1912
1913 static int
1914 csky_num_arg_regs (machine_mode mode, const_tree type, bool is_stdarg)
1915 {
1916 int size;
1917
1918 if (type && mode == BLKmode)
1919 size = int_size_in_bytes (type);
1920 else
1921 size = GET_MODE_SIZE (mode);
1922
1923 if (TARGET_HARD_FLOAT_ABI
1924 && !is_stdarg)
1925 {
1926 if (CSKY_VREG_MODE_P(mode)
1927 && !TARGET_SINGLE_FPU)
1928 return ((CSKY_NUM_WORDS (size) + 1) / 2);
1929 }
1930
1931 return CSKY_NUM_WORDS (size);
1932 }
1933
1934
1935 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
1936
1937 static void
1938 csky_function_arg_advance (cumulative_args_t pcum_v,
1939 const function_arg_info &arg)
1940 {
1941 CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
1942 int *reg = &pcum->reg;
1943 machine_mode mode = arg.mode;
1944
1945 int param_size = csky_num_arg_regs (mode, arg.type, pcum->is_stdarg);
1946 int param_regs_nums = CSKY_NPARM_REGS;
1947
1948 if (FUNCTION_VARG_MODE_P(mode)
1949 && !pcum->is_stdarg)
1950 {
1951 reg = &pcum->freg;
1952 param_regs_nums = CSKY_NPARM_FREGS;
1953 }
1954
1955 if (*reg + param_size > param_regs_nums)
1956 *reg = param_regs_nums;
1957 else
1958 *reg += param_size;
1959 }
1960
1961
1962 /* Implement TARGET_FUNCTION_VALUE. */
1963 static rtx
1964 csky_function_value (const_tree type, const_tree func,
1965 bool outgoing ATTRIBUTE_UNUSED)
1966 {
1967 machine_mode mode;
1968 int unsignedp ATTRIBUTE_UNUSED;
1969 int size;
1970
1971 mode = TYPE_MODE (type);
1972 size = int_size_in_bytes (type);
1973
1974 if (FUNCTION_VARG_MODE_P(mode))
1975 {
1976 mode = promote_function_mode (type, mode, &unsignedp, func, 1);
1977 return gen_rtx_REG (mode, CSKY_FIRST_VFP_REGNUM);
1978 }
1979
1980 /* Since we promote return types, we must promote the mode here too. */
1981 if (INTEGRAL_TYPE_P (type))
1982 {
1983 mode = promote_function_mode (type, mode, &unsignedp, func, 1);
1984 return gen_rtx_REG (mode, CSKY_FIRST_RET_REGNUM);
1985 }
1986
1987 if (mode == BLKmode && size > UNITS_PER_WORD
1988 && size <= UNITS_PER_WORD * 2)
1989 {
1990 rtx ret_regs[2];
1991 ret_regs[0] = gen_rtx_EXPR_LIST (SImode,
1992 gen_rtx_REG (SImode,
1993 CSKY_FIRST_RET_REGNUM),
1994 GEN_INT (0 * UNITS_PER_WORD));
1995 ret_regs[1] = gen_rtx_EXPR_LIST (SImode,
1996 gen_rtx_REG (SImode,
1997 CSKY_FIRST_RET_REGNUM + 1),
1998 GEN_INT (1 * UNITS_PER_WORD));
1999
2000 rtvec vec = gen_rtvec (2, ret_regs[0], ret_regs[1]);
2001
2002 return gen_rtx_PARALLEL (mode, vec);
2003 }
2004
2005 return gen_rtx_REG (mode, CSKY_FIRST_RET_REGNUM);
2006 }
2007
2008
2009 /* Implement TARGET_LIBCALL_VALUE. */
2010
2011 static rtx
2012 csky_libcall_value (machine_mode mode,
2013 const_rtx libcall ATTRIBUTE_UNUSED)
2014 {
2015 if (FUNCTION_VARG_MODE_P(mode))
2016 {
2017 return gen_rtx_REG (mode, CSKY_FIRST_VFP_REGNUM);
2018 }
2019 return gen_rtx_REG (mode, CSKY_FIRST_RET_REGNUM);
2020 }
2021
2022
2023 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
2024 On C-SKY, only r0 can return results. */
2025
2026 static bool
2027 csky_function_value_regno_p (const unsigned int regno)
2028 {
2029 if (regno == CSKY_FIRST_RET_REGNUM
2030 || (TARGET_HARD_FLOAT_ABI
2031 && regno == CSKY_FIRST_VFP_REGNUM))
2032 return true;
2033 return false;
2034 }
2035
2036
2037 /* Return an RTX indicating where the return address to the
2038 calling function can be found. */
2039
2040 rtx
2041 csky_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
2042 {
2043 if (count != 0)
2044 return NULL_RTX;
2045
2046 return get_hard_reg_initial_val (Pmode, CSKY_LR_REGNUM);
2047 }
2048
2049
2050 /* Implement TARGET_ARG_PARTIAL_BYTES.
2051 Return the number of bytes at the beginning of an argument
2052 that must be put in registers. The value must be zero for arguments
2053 that are passed entirely in registers or
2054 that are entirely pushed on the stack. */
2055
2056 static int
2057 csky_arg_partial_bytes (cumulative_args_t pcum_v, const function_arg_info &arg)
2058 {
2059 CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
2060 int param_size = csky_num_arg_regs (arg.mode, arg.type, pcum->is_stdarg);
2061 int reg = pcum->reg;
2062
2063 if (FUNCTION_VARG_MODE_P(arg.mode)
2064 && !pcum->is_stdarg)
2065 return 0;
2066
2067 if (reg < CSKY_NPARM_REGS
2068 && reg + param_size > CSKY_NPARM_REGS)
2069 return (CSKY_NPARM_REGS - reg) * UNITS_PER_WORD;
2070
2071 return 0;
2072 }
2073
2074
2075 /* Implement TARGET_SETUP_INCOMING_VARARGS.
2076 On C-Sky the copy from the argument registers to the stack is emitted
2077 by the prologue hooks, so here we just have to note how much stack space
2078 to save. */
2079
2080 static void
2081 csky_setup_incoming_varargs (cumulative_args_t pcum_v,
2082 const function_arg_info &arg,
2083 int *pretend_size,
2084 int second_time ATTRIBUTE_UNUSED)
2085 {
2086 CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
2087 CUMULATIVE_ARGS local_cum;
2088 cumulative_args_t local_cum_v = pack_cumulative_args (&local_cum);
2089 int regs_to_push;
2090
2091 cfun->machine->uses_anonymous_args = 1;
2092 local_cum = *pcum;
2093 if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl))
2094 || arg.type != NULL_TREE)
2095 csky_function_arg_advance (local_cum_v, arg);
2096 regs_to_push = CSKY_NPARM_REGS - local_cum.reg;
2097 if (regs_to_push)
2098 *pretend_size = regs_to_push * UNITS_PER_WORD;
2099 }
2100
2101
2102 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.
2103 Output code to add DELTA to the first argument, and then jump
2104 to FUNCTION. Used for C++ multiple inheritance. */
2105
2106 static void
2107 csky_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
2108 HOST_WIDE_INT delta,
2109 HOST_WIDE_INT vcall_offset,
2110 tree function)
2111 {
2112 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk));
2113 const char *thiz = "a0";
2114 const char *reg0 = "t0";
2115 const char *reg1 = "t1";
2116 int maxoff = 4096; /* Constant range for addi/subi. */
2117
2118 assemble_start_function (thunk, fnname);
2119 final_start_function (emit_barrier (), file, 1);
2120
2121 rtx fnaddr = XEXP (DECL_RTL (function), 0);
2122
2123 if (CSKY_TARGET_ARCH (CK801))
2124 {
2125 /* CK801 can't use t registers and has only 16-bit addi/subi. */
2126 reg0 = "l0";
2127 reg1 = "l1";
2128 maxoff = 256;
2129 if (vcall_offset > maxoff || vcall_offset < -maxoff)
2130 fprintf (file, "\tpush\tl0, l1\n");
2131 else if (delta > maxoff || delta < -maxoff)
2132 fprintf (file, "\tpush\tl0\n");
2133 }
2134
2135 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2136 thiz = "a1";
2137
2138 /* Add delta to this_rtx. */
2139 if (delta != 0)
2140 {
2141 if (delta > maxoff || delta < -maxoff)
2142 {
2143 fprintf (file, "\tlrw\t%s, %ld\n", reg0, (long)delta);
2144 fprintf (file, "\taddu\t%s, %s, %s\n", thiz, thiz, reg0);
2145 }
2146 else
2147 fprintf (file, "\t%s\t%s, %s, %ld\n",
2148 (delta > 0 ? "addi" : "subi"), thiz, thiz,
2149 (long)(delta > 0 ? delta : -delta));
2150 }
2151
2152 /* If needed, add *(*this_rtx + vcall_offset) to this_rtx. */
2153 if (vcall_offset != 0)
2154 {
2155 fprintf (file, "\tld.w\t%s, (%s, 0)\n", reg0, thiz);
2156
2157 if (vcall_offset > maxoff || vcall_offset < -maxoff)
2158 {
2159 fprintf (file, "\tlrw\t%s, %ld\n", reg1, (long)vcall_offset);
2160 fprintf (file, "\taddu\t%s, %s, %s\n", reg0, reg0, reg1);
2161 }
2162 else
2163 fprintf (file, "\t%s\t%s, %s, %ld\n",
2164 (vcall_offset > 0 ? "addi" : "subi"), reg0, reg0,
2165 (long)(vcall_offset > 0 ? vcall_offset : -vcall_offset));
2166
2167 /* Load the offset and add it to this_rtx */
2168 fprintf (file, "\tld.w\t%s, (%s, 0)\n", reg0, reg0);
2169 fprintf (file, "\taddu\t%s, %s, %s\n", thiz, thiz, reg0);
2170 }
2171
2172 /* We must pop the scratch regs individually instead of using the
2173 "pop" insn, which also does a return. */
2174 if (CSKY_TARGET_ARCH (CK801))
2175 {
2176 if (vcall_offset > maxoff || vcall_offset < -maxoff)
2177 {
2178 fprintf (file, "\tld.w\tl0, (sp, 0)\n");
2179 fprintf (file, "\tld.w\tl1, (sp, 4)\n");
2180 fprintf (file, "\taddi\t sp, sp, 8\n");
2181 }
2182 else if (delta > maxoff || delta < -maxoff)
2183 {
2184 fprintf (file, "\tld.w\tl0, (sp, 0)\n");
2185 fprintf (file, "\taddi\tsp, sp, 4\n");
2186 }
2187 }
2188
2189 fprintf (file, "\tjbr\t");
2190 output_addr_const (file, fnaddr);
2191 fprintf (file, "\n");
2192
2193 final_end_function ();
2194 assemble_end_function (thunk, fnname);
2195 }
2196
2197
2198 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.
2199 Conditionally modify five variables fixed_regs, call_used_regs, global_regs,
2200 reg_names, and reg_class_contents, to take into account any dependence of
2201 these register sets on target flags.
2202
2203 CK801 has registers r0-r8 and r13-r15. CK802 and CK803 have registers
2204 r0-r15 (the "low" registers). Other cpus use registers r0-r31 with
2205 -mhigh-registers, otherwise also only r0-r15.
2206
2207 CK801 only has 16-bit instructions, most of which can only reference
2208 r0-r7 (the "mini" registers). So we mark regs outside that range as
2209 fixed. -msmart can be used on other arch variants to force the same
2210 behavior because it results in smaller code size.
2211
2212 TODO: investigate whether it's beneficial to use r8-r13 as a spill
2213 class when TARGET_MINI_REGISTERS instead of making them unusable by
2214 the register allocator. */
2215
2216 static void
2217 csky_conditional_register_usage (void)
2218 {
2219 /* Only use mini registers in smart mode or 801. */
2220 if (TARGET_MINI_REGISTERS)
2221 {
2222 int i;
2223
2224 for (i = (CSKY_LAST_MINI_REGNUM + 1); i < 32; i++)
2225 {
2226 fixed_regs[i] = 1;
2227 call_used_regs[i] = 1;
2228 }
2229 }
2230 /* For some targets, the high registers are not supported.
2231 CPUs other than ck801/ck802/ck803 use high registers
2232 depending on -mhigh-registers option. */
2233 else if (CSKY_TARGET_ARCH (CK802)
2234 || CSKY_TARGET_ARCH (CK803)
2235 || !TARGET_HIGH_REGISTERS)
2236 {
2237 int i;
2238
2239 for (i = CSKY_FIRST_HIGH_REGNUM; i <= CSKY_LAST_HIGH_REGNUM; i++)
2240 {
2241 fixed_regs[i] = 1;
2242 call_used_regs[i] = 1;
2243 }
2244 }
2245
2246 /* On CK801/CK802 we must mark lr as a fixed register because it is
2247 used to implement far jumps.
2248 FIXME: perhaps there should be a command-line option controlling
2249 use of lr for far jumps on ck802 when !TARGET_MINI_REGS, when
2250 you really want lr to be available to the register allocator and
2251 you know there are no far jumps in the code. */
2252 if (CSKY_TARGET_ARCH (CK801) || CSKY_TARGET_ARCH (CK802))
2253 {
2254 fixed_regs[CSKY_LR_REGNUM] = 1;
2255 call_used_regs[CSKY_LR_REGNUM] = 0;
2256 }
2257
2258 /* The hi/lo registers are only supported in dsp mode. */
2259 if (!TARGET_DSP)
2260 {
2261 fixed_regs[CSKY_HI_REGNUM] = 1;
2262 call_used_regs[CSKY_HI_REGNUM] = 1;
2263
2264 fixed_regs[CSKY_LO_REGNUM] = 1;
2265 call_used_regs[CSKY_LO_REGNUM] = 1;
2266 }
2267
2268 /* The V_REGS are only supported in hard float mode. */
2269 if (!TARGET_HARD_FLOAT)
2270 {
2271 int regno;
2272
2273 for (regno = CSKY_FIRST_VFP_REGNUM;
2274 regno <= CSKY_LAST_VFP3_REGNUM; regno++)
2275 {
2276 fixed_regs[regno] = 1;
2277 call_used_regs[regno] = 1;
2278 }
2279 }
2280
2281 if (!TARGET_SUPPORT_FPV3)
2282 {
2283 int regno;
2284
2285 for (regno = CSKY_FIRST_VFP3_REGNUM;
2286 regno <= CSKY_LAST_VFP3_REGNUM; regno++)
2287 {
2288 fixed_regs[regno] = 1;
2289 call_used_regs[regno] = 1;
2290 }
2291 }
2292
2293 /* In pic mode, the gb register is not available for register
2294 allocation. Since gb is not clobbered by function
2295 calls, set its call_used_regs to 0. */
2296 if (flag_pic)
2297 {
2298 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2299 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 0;
2300 }
2301 }
2302
2303 /* Implement TARGET_HARD_REGNO_NREGS. */
2304
2305 static unsigned int
2306 csky_hard_regno_nregs (unsigned int regno, machine_mode mode)
2307 {
2308 if (regno >= CSKY_FIRST_VFP_REGNUM && !CSKY_TARGET_ARCH (CK803))
2309 return 1;
2310 else
2311 return CSKY_NUM_REGS (mode);
2312 }
2313
2314 /* Implement TARGET_HARD_REGNO_MODE_OK. Return true if REGNO is a
2315 valid register for holding a quantity of type MODE. */
2316
2317 static bool
2318 csky_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2319 {
2320 int nregs = CSKY_NUM_REGS (mode);
2321
2322 /* We can't handle more than doubleword sizes for any register. */
2323 if (nregs > 2)
2324 return false;
2325
2326 /* For general registers, return true if mode is one word size.
2327 When the size is larger than one word size, there should
2328 be two successive hard registers to put the data. */
2329 if (regno < CSKY_NGPR_REGS)
2330 {
2331 if (nregs < 2)
2332 return true;
2333 else if (TARGET_MINI_REGISTERS)
2334 return (regno < CSKY_LAST_MINI_REGNUM);
2335 else if (CSKY_TARGET_ARCH (CK802)
2336 || CSKY_TARGET_ARCH (CK803)
2337 || !TARGET_HIGH_REGISTERS)
2338 /* Without high register, r15 cannot hold doubleword data. */
2339 return (regno < (CSKY_SP_REGNUM - 1));
2340 else
2341 return (regno < (CSKY_SP_REGNUM - 1)
2342 || (regno >= CSKY_LR_REGNUM
2343 && regno < CSKY_LAST_HIGH_UNFIXED_REGNUM));
2344 }
2345 else if (regno == CSKY_CC_REGNUM)
2346 return (mode == CCmode);
2347 else if (regno == CSKY_HI_REGNUM || regno == CSKY_LO_REGNUM)
2348 {
2349 /* Don't allocate hi,lo register for float data even
2350 if in dsp mode, because it will cause high cost
2351 to reload data from hi,lo register. */
2352 if (!TARGET_DSP || mode == SFmode || mode == DFmode)
2353 return false;
2354 else if (nregs == 2)
2355 return (regno == CSKY_HI_REGNUM);
2356 else
2357 return true;
2358 }
2359 else if (CSKY_VREG_P (regno) && TARGET_HARD_FLOAT)
2360 return true;
2361
2362 return false;
2363 }
2364
2365 /* Implement TARGET_MODES_TIEABLE_P. We can't tie DFmode with other modes
2366 when V_REGs might be in use because those registers mess with the stored
2367 bits. */
2368
2369 static bool
2370 csky_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2371 {
2372 return !(TARGET_HARD_FLOAT
2373 && mode1 != mode2
2374 && (mode1 == DFmode || mode2 == DFmode));
2375 }
2376
2377 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.
2378 We need to define this for MINI_REGS when we only use r0 - r7.
2379 Otherwise we can end up using r0-r4 for function arguments, and don't
2380 have enough left over to do doubleword arithmetic. */
2381
2382 static bool
2383 csky_class_likely_spilled_p (reg_class_t rclass)
2384 {
2385 if ((TARGET_MINI_REGISTERS && rclass == MINI_REGS)
2386 || rclass == C_REGS)
2387 return true;
2388
2389 return false;
2390 }
2391
2392
2393 /* Implement TARGET_PREFERRED_RELOAD_CLASS.
2394 Given an rtx X being reloaded into a reg required to be
2395 in class CLASS, return the class of reg to actually use.
2396 In general this is just CLASS. */
2397
2398 static reg_class_t
2399 csky_preferred_reload_class (rtx x, reg_class_t rclass)
2400 {
2401 if (TARGET_HARD_FLOAT
2402 && CONST_DOUBLE_P (x)
2403 && (GET_MODE (x) == DFmode || GET_MODE (x) == SFmode)
2404 && rclass == NO_REGS)
2405 return GENERAL_REGS;
2406 return rclass;
2407 }
2408
2409
2410 /* Implement TARGET_CLASS_MAX_NREGS.
2411 Return the maximum number of consecutive registers of class rclass needed
2412 to hold a value of mode mode.
2413 On the csky, this is the size of MODE in words,
2414 except in the FP regs, where a single reg is always enough. */
2415
2416 static unsigned char
2417 csky_class_max_nregs (reg_class_t rclass, machine_mode mode)
2418 {
2419 if (rclass == V_REGS)
2420 return 1;
2421 else
2422 return CSKY_NUM_REGS (mode);
2423 }
2424
2425
2426 /* Implement TARGET_SECONDARY_RELOAD.
2427 If copying a register of RCLASS from/to X requires an intermediate
2428 register, the hook should return the REGISTER_CLASS required for this
2429 intermediate register.
2430 If no intermediate register is required, it should return NO_REGS.
2431 If more than one intermediate register is required, describe the one
2432 that is closest in the copy chain to the reload register. */
2433
2434 reg_class_t
2435 csky_secondary_reload (bool in_p ATTRIBUTE_UNUSED, rtx x,
2436 reg_class_t rclass,
2437 machine_mode mode,
2438 secondary_reload_info *sri ATTRIBUTE_UNUSED)
2439 {
2440 int regno = -1;
2441
2442 /* Extract the real regno from X. */
2443 if (GET_CODE (x) == SIGN_EXTEND)
2444 {
2445 int off = 0;
2446
2447 x = XEXP (x, 0);
2448
2449 if (reg_renumber)
2450 regno = true_regnum (x);
2451 else
2452 {
2453 while (GET_CODE (x) == SUBREG)
2454 {
2455 off += subreg_regno_offset (REGNO (SUBREG_REG (x)),
2456 GET_MODE (SUBREG_REG (x)),
2457 SUBREG_BYTE (x), GET_MODE (x));
2458 x = SUBREG_REG (x);
2459 }
2460
2461 if (GET_CODE (x) == REG)
2462 regno = REGNO (x) + off;
2463 }
2464 }
2465 else if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2466 regno = true_regnum (x);
2467
2468 /* We always require a general register when copying anything to
2469 HI/LO_REGNUM, except when copying an SImode value from HI/LO_REGNUM
2470 to a general register, or when copying from register 0. */
2471 if (rclass == HILO_REGS && !CSKY_GENERAL_REGNO_P (regno))
2472 return GENERAL_REGS;
2473
2474 if (rclass == V_REGS && !CSKY_GENERAL_REGNO_P (regno))
2475 {
2476 /* Reload between vector reg and memory does not need an
2477 intermediate register. */
2478 if (MEM_P (x) && (mode == SFmode || mode == DFmode))
2479 return NO_REGS;
2480 else
2481 return GENERAL_REGS;
2482 }
2483
2484 return NO_REGS;
2485 }
2486
2487 /* Implement TARGET_SPILL_CLASS.
2488 Try spilling to a larger register class before spilling to memory. */
2489
2490 static reg_class_t
2491 csky_spill_class (reg_class_t rclass, machine_mode mode ATTRIBUTE_UNUSED)
2492 {
2493 if ((rclass == MINI_REGS && !TARGET_MINI_REGISTERS)
2494 || (rclass == LOW_REGS && TARGET_HIGH_REGISTERS))
2495 return GENERAL_REGS;
2496 return NO_REGS;
2497 }
2498
2499 /* Convert a static initializer array of feature bits to sbitmap
2500 representation. */
2501
2502 static void
2503 csky_initialize_isa (sbitmap isa, const enum csky_isa_feature *isa_bits)
2504 {
2505 bitmap_clear (isa);
2506 while (*isa_bits != CSKY_ISA_FEATURE_GET (none))
2507 bitmap_set_bit (isa, *(isa_bits++));
2508 }
2509
2510
2511 /* Configure a build target TARGET from the user-specified options OPTS and
2512 OPTS_SET. */
2513
2514 static void
2515 csky_configure_build_target (struct csky_build_target *target,
2516 struct cl_target_option *opts,
2517 struct gcc_options *opts_set)
2518 {
2519 const struct csky_processors *csky_selected_tune = NULL;
2520 struct csky_processors *csky_selected_cpu = NULL;
2521 struct csky_processors *csky_selected_arch = NULL;
2522 sbitmap all_sbits = sbitmap_alloc (CSKY_ISA_FEATURE_GET (max));
2523 bitmap_clear (all_sbits);
2524
2525 bitmap_clear (target->isa);
2526 target->core_name = NULL;
2527 target->arch_name = NULL;
2528
2529 if (opts_set->x_csky_arch_option)
2530 csky_selected_arch = &all_architectures[opts->x_csky_arch_option];
2531
2532 if (opts_set->x_csky_cpu_option)
2533 {
2534 csky_selected_cpu = &all_cores[opts->x_csky_cpu_option];
2535 csky_selected_tune = &all_cores[opts->x_csky_cpu_option];
2536 }
2537
2538 if (csky_selected_cpu)
2539 {
2540 /* TODO: support combination of features
2541 between different cpu & arch, should based on arch. */
2542 if (csky_selected_arch
2543 && (csky_selected_cpu->base_arch != csky_selected_arch->base_arch))
2544 warning (0, "cpu %qs is not based on arch %qs, ignoring the arch",
2545 csky_selected_cpu->name, csky_selected_arch->name);
2546 if (!csky_selected_arch)
2547 csky_selected_arch = &all_architectures[csky_selected_cpu->base_arch];
2548 csky_initialize_isa (all_sbits, csky_selected_arch->isa_bits);
2549 target->core_name = csky_selected_cpu->name;
2550 }
2551 else if (csky_selected_arch)
2552 {
2553 csky_selected_cpu = csky_selected_arch;
2554 target->arch_name = csky_selected_arch->name;
2555 }
2556 else /* If the user did not specify a processor, choose one for them. */
2557 {
2558 csky_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
2559 csky_selected_arch = &all_architectures[csky_selected_cpu->base_arch];
2560 csky_initialize_isa (all_sbits, csky_selected_arch->isa_bits);
2561 target->core_name = csky_selected_cpu->name;
2562 }
2563
2564 /* The selected cpu may be an architecture, so lookup tuning by core ID. */
2565 if (!csky_selected_tune)
2566 csky_selected_tune = &all_cores[csky_selected_cpu->core];
2567 gcc_assert (csky_selected_tune);
2568
2569 gcc_assert (csky_selected_arch);
2570 gcc_assert (csky_selected_cpu);
2571 csky_initialize_isa (target->isa, csky_selected_cpu->isa_bits);
2572 bitmap_ior (target->isa, target->isa, all_sbits);
2573
2574 /* Finish initializing the target structure. */
2575 target->arch_pp_name = csky_selected_cpu->arch;
2576 target->base_arch = csky_selected_cpu->base_arch;
2577 target->arch_core = csky_selected_cpu->core;
2578
2579 sbitmap_free (all_sbits);
2580 }
2581
2582
2583 /* Implement TARGET_OPTION_OVERRIDE. */
2584
2585 static void
2586 csky_option_override (void)
2587 {
2588 csky_active_target.isa = sbitmap_alloc (CSKY_ISA_FEATURE_GET (max));
2589
2590 /* Create the default target_options structure. We need this early
2591 to configure the overall build target. */
2592 target_option_default_node = target_option_current_node
2593 = build_target_option_node (&global_options, &global_options_set);
2594
2595 csky_configure_build_target (&csky_active_target,
2596 TREE_TARGET_OPTION (target_option_default_node),
2597 &global_options_set);
2598
2599 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2600 SUBTARGET_OVERRIDE_OPTIONS;
2601 #endif
2602
2603 csky_base_arch = csky_active_target.base_arch;
2604
2605 if (flag_pic && !(CSKY_TARGET_ARCH (CK807)
2606 || CSKY_TARGET_ARCH (CK810)
2607 || CSKY_TARGET_ARCH (CK860)))
2608 {
2609 flag_pic = 0;
2610 warning (0, "%qs is not supported by arch %qs",
2611 "-fPIC", csky_active_target.arch_pp_name);
2612 }
2613
2614 /* Check floating-point options for consistency. */
2615 if (TARGET_HARD_FLOAT)
2616 {
2617 const struct csky_fpu_desc *csky_selected_fpu = NULL;
2618
2619 if (csky_fpu_index == TARGET_FPU_auto)
2620 {
2621 const char *target_fpu_name;
2622 bool ok;
2623 int fpu_index;
2624
2625 if (csky_active_target.core_name != NULL
2626 && !strchr (csky_active_target.core_name, 'f'))
2627 target_fpu_name = "auto";
2628 else if (CSKY_TARGET_ARCH (CK803) || !TARGET_DOUBLE_FLOAT)
2629 target_fpu_name = "fpv2_sf";
2630 else if (CSKY_TARGET_ARCH (CK860))
2631 target_fpu_name = "fpv3";
2632 else if (TARGET_DOUBLE_FLOAT && TARGET_FDIVDU)
2633 target_fpu_name = "fpv2_divd";
2634 else
2635 #ifdef CSKY_FPUTYPE_DEFAULT
2636 target_fpu_name = CSKY_FPUTYPE_DEFAULT;
2637 #else
2638 target_fpu_name = "fpv2";
2639 #endif
2640
2641 ok = opt_enum_arg_to_value (OPT_mfpu_, target_fpu_name, &fpu_index,
2642 CL_TARGET);
2643 gcc_assert (ok);
2644 csky_fpu_index = (enum csky_fpu_type) fpu_index;
2645 }
2646
2647 if (CSKY_TARGET_ARCH (CK801) || CSKY_TARGET_ARCH (CK802))
2648 error ("%qs is not supported by arch %qs",
2649 "-mhard-float", csky_active_target.arch_pp_name);
2650 else if (csky_fpu_index == TARGET_FPU_auto)
2651 error ("%<-mhard-float%> is not supported by the selected CPU");
2652 else
2653 {
2654 csky_selected_fpu = &all_fpus[csky_fpu_index];
2655 sbitmap fpu_bits = sbitmap_alloc (CSKY_ISA_FEATURE_GET (max));
2656 csky_initialize_isa (fpu_bits, csky_selected_fpu->isa_bits);
2657
2658 bitmap_ior (csky_active_target.isa, csky_active_target.isa,
2659 fpu_bits);
2660
2661 sbitmap_free (fpu_bits);
2662 }
2663 }
2664 else
2665 {
2666 if (TARGET_DOUBLE_FLOAT > 0)
2667 warning (0, "%<-mdouble-float%> ignored without %<-mhard-float%>");
2668 TARGET_DOUBLE_FLOAT = 0;
2669 if (TARGET_FDIVDU > 0)
2670 warning (0, "%<-mfdivdu%> ignored without %<-mhard-float%>");
2671 TARGET_FDIVDU = 0;
2672 }
2673
2674 /* Initialize boolean versions of the architectural flags, for use
2675 in the .md file. */
2676
2677 #undef CSKY_ISA
2678 #define CSKY_ISA(IDENT, DESC) \
2679 { \
2680 csky_arch_isa_features[CSKY_ISA_FEATURE_GET (IDENT)] = \
2681 bitmap_bit_p (csky_active_target.isa, CSKY_ISA_FEATURE_GET (IDENT)); \
2682 }
2683 #include "csky_isa.def"
2684 #undef CSKY_ISA
2685
2686 /* Extended LRW instructions are enabled by default on CK801, disabled
2687 otherwise. */
2688 if (TARGET_ELRW == -1)
2689 TARGET_ELRW = CSKY_TARGET_ARCH (CK801);
2690
2691 /* DSP is enabled either by the processor feature or -mdsp
2692 command-line option. There is no -mno-dsp option as the assembler
2693 doesn't take one. */
2694 if (!TARGET_DSP)
2695 TARGET_DSP = CSKY_ISA_FEATURE (dsp);
2696
2697 /* There's both -mdiv and -mno-div. Take default from processor if
2698 neither is specified explicitly. */
2699 if (TARGET_DIV == -1)
2700 TARGET_DIV = CSKY_ISA_FEATURE (div);
2701
2702 /* TARGET_CONSTANT_POOL is mandatory for CK801 and CK802 and optional
2703 for other CPUs.
2704 The reason why the compiler has to generate constant pools for CK801/2
2705 instead of deferring to the assembler is that these cores don't have a
2706 long branch instruction other than jbsr, which clobbers lr. So for
2707 the compiler to correctly save/restore lr it has to know whether there
2708 are long branches, which depends on having accurate branch length
2709 counts, which in turn depends on having control over where constant
2710 pools are placed. */
2711 if ((CSKY_TARGET_ARCH (CK801) || CSKY_TARGET_ARCH (CK802))
2712 && !TARGET_CONSTANT_POOL)
2713 error ("%qs is not supported by arch %qs",
2714 "-mno-constpool", csky_active_target.arch_pp_name);
2715 else if (TARGET_CONSTANT_POOL == -1)
2716 TARGET_CONSTANT_POOL = (CSKY_TARGET_ARCH (CK801)
2717 || CSKY_TARGET_ARCH (CK802));
2718
2719 /* TARGET_MINI_REGISTERS is mandatory for CK801, the default for CK802,
2720 and optional for other CPUs. TARGET_HIGH_REGISTERS is incompatible
2721 with TARGET_MINI_REGISTERS, is not supported by CK801/802/803,
2722 and is the default for other processors.
2723 See csky_conditional_register_usage. */
2724 if (TARGET_MINI_REGISTERS > 0 && TARGET_HIGH_REGISTERS > 0)
2725 error ("%<-msmart%> is incompatible with %<-mhigh-registers%>");
2726 else if (CSKY_TARGET_ARCH (CK801)
2727 || CSKY_TARGET_ARCH (CK802)
2728 || CSKY_TARGET_ARCH (CK803))
2729 {
2730 if (CSKY_TARGET_ARCH (CK801)
2731 || (CSKY_TARGET_ARCH (CK802) && TARGET_MINI_REGISTERS == -1))
2732 TARGET_MINI_REGISTERS = 1;
2733 else if (TARGET_MINI_REGISTERS == -1)
2734 TARGET_MINI_REGISTERS = 0;
2735 if (TARGET_HIGH_REGISTERS > 0)
2736 warning (0, "%qs is not supported by arch %qs",
2737 "-mhigh-registers", csky_active_target.arch_pp_name);
2738 TARGET_HIGH_REGISTERS = 0;
2739 }
2740 else
2741 {
2742 if (TARGET_MINI_REGISTERS == -1)
2743 TARGET_MINI_REGISTERS = 0;
2744 if (TARGET_HIGH_REGISTERS == -1)
2745 TARGET_HIGH_REGISTERS = !TARGET_MINI_REGISTERS;
2746 }
2747
2748 /* -mmultiple-stld is the default for everything but CK801, which
2749 doesn't support it. */
2750 if (CSKY_TARGET_ARCH (CK801))
2751 {
2752 if (TARGET_MULTIPLE_STLD > 0)
2753 warning (0, "%qs is not supported by arch %qs",
2754 "-mmultiple-stld", csky_active_target.arch_pp_name);
2755 TARGET_MULTIPLE_STLD = 0;
2756 }
2757
2758 /* TODO */
2759
2760 /* Resynchronize the saved target options. */
2761 cl_target_option_save (TREE_TARGET_OPTION (target_option_default_node),
2762 &global_options, &global_options_set);
2763
2764 #ifdef ENABLE_TPF_DEBUG
2765 /* Don't emit DWARF4 unless specifically selected. The TPF
2766 debuggers do not yet support DWARF 3/4. */
2767 if (!OPTION_SET_P (dwarf_strict))
2768 dwarf_strict = 1;
2769 if (!OPTION_SET_P (dwarf_version))
2770 dwarf_version = 3;
2771 #endif
2772
2773 /* Don't run the scheduler before reload by default,
2774 since it tends to increase register pressure. */
2775 if (!OPTION_SET_P (flag_schedule_insns))
2776 flag_schedule_insns = 0;
2777
2778 csky_add_gc_roots ();
2779 }
2780
2781
2782 /* Return TRUE if X contains any references to TLS symbols. */
2783
2784 bool
2785 csky_tls_referenced_p (rtx x)
2786 {
2787 if (!TARGET_TLS)
2788 return false;
2789
2790 subrtx_iterator::array_type array;
2791 FOR_EACH_SUBRTX (iter, array, x, ALL)
2792 {
2793 const_rtx x = *iter;
2794 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0)
2795 return true;
2796
2797 /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
2798 TLS offsets, not real symbol references. */
2799 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
2800 iter.skip_subrtxes ();
2801 }
2802 return false;
2803 }
2804
2805
2806 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.
2807 Determine if it's legal to put X into the constant pool. This
2808 is not possible for the address of thread-local symbols, which
2809 is checked above. */
2810
2811 static bool
2812 csky_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED,
2813 rtx x)
2814 {
2815 return csky_tls_referenced_p (x);
2816 }
2817
2818
2819 /* Implement TARGET_LEGITIMATE_CONSTANT_P. Returns nonzero if the
2820 constant value X is a legitimate general operand.
2821 It is given that X satisfies CONSTANT_P or is a CONST_DOUBLE. */
2822
2823 static bool
2824 csky_legitimate_constant_p (machine_mode mode, rtx x)
2825 {
2826 return (!csky_cannot_force_const_mem (mode, x)
2827 && CONSTANT_P (x));
2828 }
2829
2830
2831 /* Return true if X is valid as an CSKY addressing register. */
2832
2833 static bool
2834 is_csky_address_register_rtx_p (rtx x, int strict_p)
2835 {
2836 int regno;
2837
2838 if (!x)
2839 return false;
2840 if (!REG_P (x))
2841 return false;
2842
2843 regno = REGNO (x);
2844
2845 if (strict_p)
2846 return (CSKY_GENERAL_REGNO_P (regno)
2847 || CSKY_GENERAL_REGNO_P (reg_renumber[regno]));
2848 else
2849 return CSKY_GENERAL_REGNO_P (regno) || regno >= FIRST_PSEUDO_REGISTER;
2850 }
2851
2852
2853 /* Return TRUE if X is a thread-local symbol. */
2854
2855 static bool
2856 csky_tls_symbol_p (rtx x)
2857 {
2858 if (!TARGET_TLS)
2859 return false;
2860
2861 if (GET_CODE (x) != SYMBOL_REF)
2862 return false;
2863
2864 return SYMBOL_REF_TLS_MODEL (x) != 0;
2865 }
2866
2867
2868 /* Handle lazy initialization of __tls_get_addr libfunc. */
2869 static GTY(()) rtx tls_get_addr_libfunc;
2870
2871 static rtx
2872 get_tls_get_addr (void)
2873 {
2874 if (!tls_get_addr_libfunc)
2875 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
2876 return tls_get_addr_libfunc;
2877 }
2878
2879
2880 /* Emit a call to __tls_get_addr. */
2881
2882 static rtx_insn *
2883 csky_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
2884 {
2885 rtx label, labelno, unspec, tmp;
2886 rtx_insn *insns;
2887
2888 start_sequence ();
2889
2890 labelno = GEN_INT (tls_labelno++);
2891 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_TLS_LABEL);
2892 unspec = gen_rtx_UNSPEC (Pmode,
2893 gen_rtvec (3, x, GEN_INT (reloc), label),
2894 UNSPEC_TLS);
2895 tmp = gen_reg_rtx (SImode);
2896 emit_move_insn (reg, unspec);
2897 emit_move_insn (tmp, label);
2898 emit_insn (gen_addsi3 (reg, reg, tmp));
2899 *valuep = emit_library_call_value (get_tls_get_addr (),
2900 NULL_RTX, LCT_PURE, /* LCT_CONST? */
2901 Pmode, reg, Pmode);
2902 insns = get_insns ();
2903 end_sequence ();
2904 return insns;
2905 }
2906
2907 /* Helper function for csky_legitimize_address, to handle the TLS cases.
2908 REG is a scratch register and may be null. */
2909
2910 rtx
2911 csky_legitimize_tls_address (rtx x, rtx reg)
2912 {
2913 rtx dest, tp, label, labelno, unspec, ret, eqv, addend, tmp;
2914 rtx_insn *insns;
2915 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
2916
2917 if (!reg)
2918 reg = gen_reg_rtx (SImode);
2919
2920 switch (model)
2921 {
2922 case TLS_MODEL_GLOBAL_DYNAMIC:
2923 insns = csky_call_tls_get_addr (x, reg, &ret, TLS_GD32);
2924 dest = gen_reg_rtx (Pmode);
2925 emit_libcall_block (insns, dest, ret, x);
2926 return dest;
2927
2928 case TLS_MODEL_LOCAL_DYNAMIC:
2929 insns = csky_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
2930
2931 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2932 share the LDM result with other LD model accesses. */
2933 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx), UNSPEC_TLS);
2934 dest = gen_reg_rtx (Pmode);
2935 emit_libcall_block (insns, dest, ret, eqv);
2936
2937 /* Load the addend. */
2938 addend = gen_rtx_UNSPEC (Pmode,
2939 gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
2940 UNSPEC_TLS);
2941 addend = force_reg (SImode, addend);
2942 return gen_rtx_PLUS (Pmode, dest, addend);
2943
2944 case TLS_MODEL_INITIAL_EXEC:
2945 labelno = GEN_INT (tls_labelno++);
2946 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_TLS_LABEL);
2947 unspec = gen_rtx_UNSPEC (Pmode,
2948 gen_rtvec (3, x, GEN_INT (TLS_IE32), label),
2949 UNSPEC_TLS);
2950 tmp = gen_reg_rtx (SImode);
2951 emit_move_insn (reg, unspec);
2952 emit_move_insn (tmp, label);
2953 emit_insn (gen_addsi3 (reg, reg, tmp));
2954 emit_move_insn (reg, gen_const_mem (Pmode, reg));
2955 tp = gen_rtx_REG (SImode, CSKY_TLS_REGNUM);
2956 return gen_rtx_PLUS (Pmode, tp, reg);
2957
2958 case TLS_MODEL_LOCAL_EXEC:
2959 unspec = gen_rtx_UNSPEC (Pmode,
2960 gen_rtvec (2, x, GEN_INT (TLS_LE32)),
2961 UNSPEC_TLS);
2962 emit_move_insn (reg, unspec);
2963 tp = gen_rtx_REG (SImode, CSKY_TLS_REGNUM);
2964 return gen_rtx_PLUS (Pmode, tp, reg);
2965
2966 default:
2967 abort ();
2968 }
2969 }
2970
2971
2972 /* Implement TARGET_LEGITIMIZE_ADDRESS. */
2973
2974 static rtx
2975 csky_legitimize_address (rtx x, rtx orig_x ATTRIBUTE_UNUSED,
2976 machine_mode mode)
2977 {
2978 if (csky_tls_symbol_p (x))
2979 return csky_legitimize_tls_address (x, NULL_RTX);
2980
2981 if (GET_CODE (x) == PLUS)
2982 {
2983 rtx xop0 = XEXP (x, 0);
2984 rtx xop1 = XEXP (x, 1);
2985
2986 if (is_csky_address_register_rtx_p (xop0, 0)
2987 && CONST_INT_P (xop1))
2988 {
2989 HOST_WIDE_INT offset = INTVAL (xop1);
2990
2991 /* Try to replace ld32 rx,(ry, offset), to addi16 rz, oimm8
2992 and ld16 rx,(rz, new_ld_offset) to avoid emitting a
2993 32-bit ld, but this addi has a range limitation. */
2994 if (optimize_size
2995 && offset > CSKY_LD16_MAX_OFFSET (mode)
2996 && offset <= (CSKY_ADDI16_MAX_IMM
2997 + CSKY_LD16_MAX_OFFSET (mode)))
2998 {
2999 HOST_WIDE_INT new_ld_offset
3000 = offset & CSKY_LD16_OFFSET_MASK (mode);
3001
3002 xop0 = force_operand (plus_constant (Pmode, xop0,
3003 offset - new_ld_offset),
3004 NULL_RTX);
3005 x = plus_constant (Pmode, xop0, new_ld_offset);
3006 }
3007 else if (offset < 0 && offset >= (-CSKY_SUBI16_MAX_IMM))
3008 x = force_operand (x, NULL_RTX);
3009 else if (offset > CSKY_LD16_MAX_OFFSET (mode)
3010 || offset < 0)
3011 {
3012 /* For the remaining cases, force the constant into a
3013 register. */
3014 xop1 = force_reg (SImode, xop1);
3015 x = gen_rtx_PLUS (SImode, xop0, xop1);
3016 }
3017 }
3018
3019 /* If the index is store in register, force the
3020 base to register. */
3021 if (is_csky_address_register_rtx_p (xop1, 0)
3022 && !is_csky_address_register_rtx_p (xop0, 0))
3023 {
3024 xop0 = force_operand (xop0, NULL_RTX);
3025 x = gen_rtx_PLUS (SImode, xop0, xop1);
3026 }
3027 }
3028 /* Make sure to take full advantage of the pre-indexed addressing mode
3029 with absolute addresses which often allows for the base register to
3030 be factorized for multiple adjacent memory references, and it might
3031 even allows for the mini pool to be avoided entirely. */
3032 else if (CONST_INT_P (x) && optimize > 0)
3033 {
3034 HOST_WIDE_INT mask, base, index;
3035 rtx base_reg;
3036
3037 mask = CSKY_LD16_OFFSET_MASK (mode);
3038 base = INTVAL (x) & ~mask;
3039 index = INTVAL (x) & mask;
3040 base_reg = force_reg (SImode, GEN_INT (base));
3041 x = plus_constant (Pmode, base_reg, index);
3042 }
3043
3044 return x;
3045 }
3046
3047
3048 /* Return nonzero if INDEX is valid for an address index operand.
3049 ck801 use 16 bits ld
3050 ck802 use 16 and 32 bits ld
3051 others use ld and ldr. */
3052
3053 static int
3054 ck801_legitimate_index_p (machine_mode mode, rtx index,
3055 int strict_p ATTRIBUTE_UNUSED)
3056 {
3057 enum rtx_code code = GET_CODE (index);
3058
3059 /* When the mode size is larger than 4, we may use two ld instruction
3060 to get data, the index and (index+1) should be valid. */
3061 if (GET_MODE_SIZE (mode) >= 8)
3062 return (code == CONST_INT
3063 && INTVAL (index) < CSKY_LD16_MAX_OFFSET (SImode)
3064 && INTVAL (index) >= 0 && (INTVAL (index) & 3) == 0);
3065
3066 if (code == CONST_INT && GET_MODE_SIZE (mode) > 0
3067 && INTVAL (index) <= CSKY_LD16_MAX_OFFSET (mode)
3068 && INTVAL (index) >= 0)
3069 return ((INTVAL (index) % GET_MODE_SIZE (mode)) == 0);
3070
3071 return 0;
3072 }
3073
3074
3075 static int
3076 ck802_legitimate_index_p (machine_mode mode, rtx index,
3077 int strict_p ATTRIBUTE_UNUSED)
3078 {
3079 enum rtx_code code = GET_CODE (index);
3080
3081 /* When the mode size is larger than 4, we may use two ld instruction
3082 to get data, the index and (index+1) should be valid. */
3083 if (GET_MODE_SIZE (mode) >= 8)
3084 return (code == CONST_INT
3085 && INTVAL (index) < CSKY_LD32_MAX_OFFSET (SImode)
3086 && INTVAL (index) >= 0 && (INTVAL (index) & 3) == 0);
3087
3088 if (code == CONST_INT && GET_MODE_SIZE (mode) > 0
3089 && INTVAL (index) <= CSKY_LD32_MAX_OFFSET (mode)
3090 && INTVAL (index) >= 0)
3091 return ((INTVAL (index) % GET_MODE_SIZE (mode)) == 0);
3092
3093 return 0;
3094 }
3095
3096
3097 /* The instruction ldr rz, (rx, ry << i), i can be 0,1,2,3.
3098 Check that SHIFT is valid, that the code is MULT, and that
3099 the shift is a power of 2. */
3100
3101 static bool
3102 is_ldr_shift_p (HOST_WIDE_INT shift, enum rtx_code code)
3103 {
3104 if (code == ASHIFT)
3105 return (shift >= 0 && shift <= 3);
3106 else if (code == MULT)
3107 return (shift == 1
3108 || shift == 2
3109 || shift == 4
3110 || shift == 8);
3111 else
3112 return false;
3113 }
3114
3115
3116 static int
3117 ck810_legitimate_index_p (machine_mode mode, rtx index, int strict_p)
3118 {
3119 enum rtx_code code = GET_CODE (index);
3120
3121 if (code == CONST_INT && TARGET_HARD_FLOAT && CSKY_VREG_MODE_P (mode))
3122 return (INTVAL (index) < 1024 && INTVAL (index) >= 0
3123 && (INTVAL (index) & 3) == 0);
3124
3125 if (code == CONST_INT)
3126 {
3127 /* When the mode size is larger than 4, we may use two ld instruction
3128 to get data, the index and (index+1) should be valid. */
3129 if (GET_MODE_SIZE (mode) >= 8)
3130 return (INTVAL (index) < CSKY_LD32_MAX_OFFSET (SImode)
3131 && INTVAL (index) >= 0 && (INTVAL (index) & 3) == 0);
3132
3133 if (GET_MODE_SIZE (mode) > 0
3134 && INTVAL (index) <= CSKY_LD32_MAX_OFFSET (mode)
3135 && INTVAL (index) >= 0)
3136 return ((INTVAL (index) % GET_MODE_SIZE (mode)) == 0);
3137 }
3138 /* Allow ld.w rx, (gb, sym@got) when -fpic specially. */
3139 else if (code == UNSPEC)
3140 return (flag_pic == 1
3141 && (XINT (index, 1) == UNSPEC_PIC_SYMBOL_PLT
3142 || XINT (index, 1) == UNSPEC_PIC_SYMBOL_GOT));
3143 /* The follow index is for ldr instruction, the ldr cannot
3144 load dword data, so the mode size should not be larger than
3145 4. */
3146 else if (GET_MODE_SIZE (mode) <= 4
3147 || (TARGET_HARD_FLOAT && CSKY_VREG_MODE_P (mode)))
3148 {
3149 if (is_csky_address_register_rtx_p (index, strict_p))
3150 return 1;
3151 else if (code == MULT || code == ASHIFT)
3152 {
3153 rtx xiop0 = XEXP (index, 0);
3154 rtx xiop1 = XEXP (index, 1);
3155
3156 /* FIXME can the xiop1 be the reg and xiop0 be the int when mult? */
3157 return (is_csky_address_register_rtx_p (xiop0, strict_p)
3158 && CONST_INT_P (xiop1)
3159 && is_ldr_shift_p (INTVAL (xiop1), code));
3160 }
3161 }
3162
3163 return 0;
3164 }
3165
3166
3167 static int
3168 csky_legitimate_index_p (machine_mode mode, rtx index, int strict_p)
3169 {
3170 if (CSKY_TARGET_ARCH (CK801))
3171 return ck801_legitimate_index_p (mode, index, strict_p);
3172 else if (CSKY_TARGET_ARCH (CK802))
3173 return ck802_legitimate_index_p (mode, index, strict_p);
3174 else
3175 return ck810_legitimate_index_p (mode, index, strict_p);
3176 }
3177
3178
3179 /* Implement TARGET_LEGITIMATE_ADDRESS_P.
3180 Recognizes RTL expressions that are valid memory addresses for an
3181 instruction. The MODE argument is the machine mode for the MEM
3182 expression that wants to use this address.
3183
3184 It only recognizes address in canonical form. LEGITIMIZE_ADDRESS should
3185 convert common non-canonical forms to canonical form so that they will
3186 be recognized. */
3187
3188 static bool
3189 csky_legitimate_address_p (machine_mode mode, rtx addr, bool strict_p,
3190 code_helper = ERROR_MARK)
3191 {
3192 enum rtx_code code = GET_CODE (addr);
3193
3194 /* Match the RTX form emitted for constant pool references.
3195 After reload constants split into minipools will have addresses
3196 from a LABEL_REF. */
3197 if (reload_completed
3198 && ((code == LABEL_REF)
3199 || (code == CONST
3200 && GET_CODE (XEXP (addr, 0)) == PLUS
3201 && GET_CODE (XEXP (XEXP (addr, 0), 0)) == LABEL_REF
3202 && CONST_INT_P (XEXP (XEXP (addr, 0), 1)))))
3203 return 1;
3204
3205 if (is_csky_address_register_rtx_p (addr, strict_p))
3206 return 1;
3207 /* It is a pc-relative load, may be generated for constpool. */
3208 else if (GET_CODE (addr) == LABEL_REF)
3209 return 1;
3210
3211 if (code == PLUS)
3212 {
3213 rtx xop0 = XEXP (addr, 0);
3214 rtx xop1 = XEXP (addr, 1);
3215
3216 return ((is_csky_address_register_rtx_p (xop0, strict_p)
3217 && csky_legitimate_index_p (mode, xop1, strict_p))
3218 || (is_csky_address_register_rtx_p (xop1, strict_p)
3219 && csky_legitimate_index_p (mode, xop0, strict_p)));
3220 }
3221
3222 return 0;
3223 }
3224
3225
3226 /* Functions to save and restore machine-specific function data. */
3227
3228 static struct machine_function *
3229 csky_init_machine_status (void)
3230 {
3231 struct machine_function *machine;
3232
3233 machine = ggc_cleared_alloc<machine_function> ();
3234
3235 #if CSKY_FT_UNKNOWN != 0
3236 machine->func_type = CSKY_FT_UNKNOWN;
3237 #endif
3238 return machine;
3239 }
3240
3241
3242 /* Implement INIT_EXPANDERS. */
3243
3244 void
3245 csky_init_expanders (void)
3246 {
3247 /* Arrange to initialize and mark the machine per-function status. */
3248 init_machine_status = csky_init_machine_status;
3249 }
3250
3251
3252 /* Implement TARGET_CANNOT_COPY_INSN_P.
3253 We must not copy any rtx that uses a pc-relative address. */
3254
3255 static bool
3256 csky_cannot_copy_insn_p (rtx_insn *insn)
3257 {
3258 subrtx_iterator::array_type array;
3259 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3260 {
3261 const_rtx x = *iter;
3262 if (GET_CODE (x) == UNSPEC
3263 && (XINT (x, 1) == UNSPEC_TLS_LABEL
3264 || XINT (x, 1) == UNSPEC_PIC_SYMBOL_GOTPC_GRS))
3265 return true;
3266 }
3267 return false;
3268 }
3269
3270
3271 /* Extract the parts of an RTL expression that is a valid memory address
3272 for an instruction. Return FALSE if it is a invalid memory address. */
3273
3274 struct csky_address
3275 {
3276 rtx base, index, symbol, label, disp;
3277 HOST_WIDE_INT scale;
3278 };
3279
3280 static bool
3281 decompose_csky_address (rtx addr, struct csky_address *out)
3282 {
3283 rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
3284 HOST_WIDE_INT scale = 0;
3285 rtx scale_rtx = NULL_RTX;
3286 int i;
3287
3288 out->base = out->index = out->symbol = out->label = out->disp = NULL_RTX;
3289 out->scale = 0;
3290
3291 if (REG_P (addr))
3292 {
3293 out->base = addr;
3294 return true;
3295 }
3296
3297 if (GET_CODE (addr) == LABEL_REF)
3298 {
3299 out->label = addr;
3300 return true;
3301 }
3302
3303 if (GET_CODE (addr) == CONST)
3304 addr = XEXP (addr, 0);
3305
3306 if (GET_CODE (addr) == PLUS)
3307 {
3308 rtx addends[2], op;
3309
3310 addends[0] = XEXP (addr, 0);
3311 addends[1] = XEXP (addr, 1);
3312
3313 if (GET_CODE (addends[0]) == LABEL_REF && CONST_INT_P (addends[1]))
3314 {
3315 out->label = addends[0];
3316 out->disp = addends[1];
3317 return true;
3318 }
3319
3320 if (!REG_P (addends[0]))
3321 std::swap (addends[0], addends[1]);
3322
3323 for (i = 0; i < 2; ++i)
3324 {
3325 op = addends[i];
3326 switch (GET_CODE (op))
3327 {
3328 case REG:
3329 if (!base)
3330 base = op;
3331 else if (!index)
3332 {
3333 index = op;
3334 scale = 1;
3335 }
3336 else
3337 return false;
3338 break;
3339 case CONST_INT:
3340 case UNSPEC:
3341 if (disp)
3342 return false;
3343 disp = op;
3344 break;
3345 case MULT:
3346 if (index)
3347 return false;
3348 index = XEXP (op, 0);
3349 scale_rtx = XEXP (op, 1);
3350 if (!CONST_INT_P (index) && !CONST_INT_P (scale_rtx))
3351 return false;
3352 else if (CONST_INT_P (index))
3353 std::swap (index, scale_rtx);
3354 scale = INTVAL (scale_rtx);
3355 break;
3356 case ASHIFT:
3357 if (index)
3358 return false;
3359 index = XEXP (op, 0);
3360 scale_rtx = XEXP (op, 1);
3361 if (!CONST_INT_P (scale_rtx))
3362 return false;
3363 scale = 1 << INTVAL (scale_rtx);
3364 break;
3365 default:
3366 return false;
3367 }
3368 }
3369 }
3370
3371 if (!base)
3372 return false;
3373
3374 out->base = base;
3375 out->index = index;
3376 out->disp = disp;
3377 out->scale = scale;
3378
3379 return true;
3380 }
3381
3382 /* Helper function for the csky_simple_mem_operand predicate. Returns
3383 true if OP is an address of the form reg + displacement. */
3384
3385 bool
3386 csky_simple_addr_operand_p (rtx op)
3387 {
3388 struct csky_address addr;
3389
3390 if (!decompose_csky_address (op, &addr))
3391 return false;
3392
3393 /* FIXME The PIC related code.
3394 Check if load the symbol address from got table. */
3395 if (addr.disp && GET_CODE (addr.disp) == UNSPEC)
3396 return false;
3397 if (!addr.index && !addr.symbol)
3398 return true;
3399 return false;
3400 }
3401
3402
3403 /* Print the UNSPEC operand in X to the STREAM. */
3404
3405 static void
3406 csky_output_pic_addr_const (FILE *stream, rtx x, int code)
3407 {
3408
3409 if (GET_CODE (x) != UNSPEC)
3410 return;
3411
3412 if (UNSPEC_TLS == XINT (x, 1))
3413 {
3414 /* FIXME It is not reached */
3415 return;
3416 }
3417
3418 csky_print_operand (stream, XVECEXP (x, 0, 0), code);
3419
3420 switch (XINT (x, 1))
3421 {
3422 case UNSPEC_PIC_SYMBOL_GOTOFF:
3423 fputs ("@GOTOFF", stream);
3424 break;
3425 case UNSPEC_PIC_SYMBOL_PLT:
3426 fputs ("@PLT", stream);
3427 break;
3428 case UNSPEC_PIC_SYMBOL_GOT:
3429 fputs ("@GOT", stream);
3430 break;
3431 case UNSPEC_PIC_SYMBOL_GOTPC:
3432 fputs ("@GOTPC", stream);
3433 break;
3434 case UNSPEC_PIC_SYMBOL_BSR:
3435 break;
3436 default:
3437 break;
3438 }
3439 }
3440
3441
3442 /* Output the constpool label according to the rtx expression X. */
3443
3444 static void
3445 csky_output_constpool_label (FILE *stream, rtx x)
3446 {
3447 char buf[15];
3448
3449 gcc_assert (GET_CODE (x) == LABEL_REF);
3450 x = XEXP (x, 0);
3451
3452 if (GET_CODE (x) == UNSPEC_VOLATILE && XINT (x, 1) == VUNSPEC_POOL_LABEL)
3453 {
3454 ASM_GENERATE_INTERNAL_LABEL (buf, CSKY_CONSTPOOL_LABEL_PREFIX,
3455 INTVAL (XVECEXP (x, 0, 0)));
3456 assemble_name (stream, buf);
3457 }
3458 }
3459
3460
3461 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
3462
3463 static void
3464 csky_print_operand_address (FILE *stream,
3465 machine_mode mode ATTRIBUTE_UNUSED,
3466 rtx x)
3467 {
3468
3469 struct csky_address addr;
3470
3471 decompose_csky_address (x, &addr);
3472
3473 if (addr.label && addr.disp && GET_CODE (addr.disp) == CONST_INT)
3474 {
3475 fprintf (stream, "[");
3476 csky_output_constpool_label (stream, addr.label);
3477 fprintf (stream, "+%d]", (int) INTVAL (addr.disp));
3478 }
3479 else if (addr.label)
3480 {
3481 fprintf (stream, "[");
3482 csky_output_constpool_label (stream, addr.label);
3483 fprintf (stream, "]");
3484 }
3485 else if (addr.symbol && addr.disp && GET_CODE (addr.disp) == CONST_INT)
3486 {
3487 fprintf (stream, "[");
3488 output_addr_const (stream, addr.symbol);
3489 fprintf (stream, "+%d]", (int) INTVAL (addr.disp));
3490 }
3491 else if (addr.symbol)
3492 {
3493 fprintf (stream, "[");
3494 output_addr_const (stream, addr.symbol);
3495 fprintf (stream, "]");
3496 }
3497 else if (addr.disp && GET_CODE (addr.disp) == CONST_INT)
3498 fprintf (stream, "(%s, %d)",
3499 reg_names[REGNO (addr.base)], (int) INTVAL (addr.disp));
3500 else if (addr.disp && GET_CODE (addr.disp) == UNSPEC)
3501 {
3502 if (REGNO (addr.base) != CSKY_GB_REGNUM)
3503 fprintf (stream, "(%s, ", reg_names[REGNO (addr.base)]);
3504 else
3505 fprintf (stream, "[");
3506 csky_output_pic_addr_const (stream, addr.disp, 0);
3507 fprintf (stream, "%s", (REGNO (addr.base) != CSKY_GB_REGNUM)
3508 ? ")" : "]");
3509 }
3510 else if (addr.index)
3511 fprintf (stream, "(%s, %s << %d)",
3512 reg_names[REGNO (addr.base)], reg_names[REGNO (addr.index)],
3513 exact_log2 ((int) (addr.scale)));
3514 else
3515 fprintf (stream, "(%s, 0)", reg_names[REGNO (addr.base)]);
3516 }
3517
3518
3519 /* Implement TARGET_PRINT_OPERAND.
3520 Print operand X (an rtx) in assembler syntax to file STREAM
3521 according to modifier CODE.
3522
3523 'N' print the log2(X+1), mainly used for bmaski
3524 'P' print the log2(X)
3525 'Q' print the log2(~X)
3526 'O' print a decimal number
3527 'M' print a decimal number as its negative
3528 'R' print the next register or memory location along, i.e. the lsw in
3529 a double word value
3530 'H' print the high 16 bits of a constant. */
3531
3532 static void
3533 csky_print_operand (FILE *stream, rtx x, int code)
3534 {
3535 switch (code)
3536 {
3537 case 'N':
3538 if ((INTVAL (x) & 0xffffffff) == 0xffffffff)
3539 fprintf (stream, "0");
3540 else
3541 fprintf (stream, "%d",
3542 (int) exact_log2 ((INTVAL (x) & 0xffffffff) + 1) % 32);
3543 break;
3544 case 'P':
3545 fprintf (stream, "%d",
3546 (int) exact_log2 (INTVAL (x) & 0xffffffff));
3547 break;
3548 case 'Q':
3549 fprintf (stream, "%d",
3550 (int) exact_log2 (~INTVAL (x) & 0xffffffff));
3551 break;
3552 case 'O':
3553 fprintf (stream, "%d", (int) INTVAL (x));
3554 break;
3555 case 'M':
3556 fprintf (stream, "%d", (int) (-INTVAL (x)));
3557 break;
3558 case 'R':
3559 /* Next location along in memory or register. */
3560 switch (GET_CODE (x))
3561 {
3562 case REG:
3563 fputs (reg_names[REGNO (x) + 1], stream);
3564 break;
3565 case MEM:
3566 csky_print_operand_address
3567 (stream, GET_MODE (x), XEXP (adjust_address (x, SImode, 4), 0));
3568 break;
3569 default:
3570 gcc_unreachable ();
3571 }
3572 break;
3573 case 'H':
3574 fprintf (stream, "%ld", (long)((INTVAL (x) & 0xFFFF0000) >> 16));
3575 break;
3576 default:
3577 switch (GET_CODE (x))
3578 {
3579 case REG:
3580 fputs (reg_names[REGNO (x)], stream);
3581 break;
3582 case MEM:
3583 output_address (GET_MODE (x), XEXP (x, 0));
3584 break;
3585 case UNSPEC:
3586 csky_output_pic_addr_const (stream, x, code);
3587 break;
3588 case CONST_DOUBLE:
3589 {
3590 char fpstr[20];
3591 real_to_decimal ( fpstr, CONST_DOUBLE_REAL_VALUE (x),
3592 sizeof (fpstr), 0, 1);
3593 fprintf (stream, "%s", fpstr);
3594 }
3595 break;
3596 default:
3597 output_addr_const (stream, x);
3598 break;
3599 }
3600 break;
3601 }
3602 }
3603
3604
3605
3606 /* Implement TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS. */
3607
3608 static bool
3609 csky_allocate_stack_slots_for_args (void)
3610 {
3611 /* Naked functions should not allocate stack slots for arguments. */
3612 return !CSKY_FUNCTION_IS_NAKED (get_csky_current_func_type ());
3613 }
3614
3615
3616 /* Can we generate a constant with a single instruction, without using
3617 lrw? */
3618
3619 static int
3620 const_ok_for_cskyv2 (HOST_WIDE_INT value)
3621 {
3622 /* Try exact power of two. It can be generated by bgeni. */
3623 if (CSKY_CONST_OK_FOR_Ub (value))
3624 return 1;
3625
3626 /* Try exact power of two - 1. It can be generated by bmaski. */
3627 if (CSKY_CONST_OK_FOR_Uc (value) && value != -1)
3628 return 1;
3629
3630 /* Try if it can be generated by movi. */
3631 if (CSKY_CONST_OK_FOR_I (value))
3632 return 1;
3633
3634 /* The constant can be generated by movih.
3635 Notice that movih is a 32-bit instruction. */
3636 if (CSKY_CONST_OK_FOR_MOVIH (value))
3637 return 1;
3638
3639 return 0;
3640 }
3641
3642
3643 /* Tricks for synthesizing constants from values that can be directly
3644 manipulated by machine instructions. */
3645
3646 enum csky_inline_const_type
3647 {
3648 IC_UNINLINABLE = 0, /* Not inlineable */
3649 IC_SINGLE, /* Single instruction */
3650 IC_APPEND_NOT, /* Single instruction followed by a not */
3651 IC_APPEND_ADDI, /* Single insn followed by an addi */
3652 IC_APPEND_SUBI, /* Single insn followed by a subi */
3653 IC_BGENI_ADDI, /* Single insn(bgeni) followed by an addi */
3654 IC_BGENI_SUBI, /* Single insn(bgeni) followed by a subi */
3655 IC_APPEND_BSETI, /* Single insn followed by bseti */
3656 IC_APPEND_MOVI, /* Single insn followed by movi */
3657 IC_APPEND_BCLRI, /* Single insn followed by bclri */
3658 IC_APPEND_ROTLI, /* Single insn followed by rotli */
3659 IC_APPEND_LSLI, /* Single insn followed by lsli */
3660 IC_APPEND_IXH, /* Single insn followed by ixh */
3661 IC_APPEND_IXW /* Single insn followed by ixw */
3662 };
3663
3664
3665 /* Try tricks to load a constant inline and return the trick number if
3666 success, or IC_UNINLINABLE. */
3667
3668 static enum csky_inline_const_type
3669 try_csky_constant_tricks (HOST_WIDE_INT value, HOST_WIDE_INT *x,
3670 HOST_WIDE_INT *y)
3671 {
3672 HOST_WIDE_INT i, value_invert;
3673 unsigned HOST_WIDE_INT bit, shf, rot, lobits, hibits;
3674
3675 value &= 0xffffffff;
3676 value_invert = ~value & 0xffffffff;
3677
3678 if (const_ok_for_cskyv2 (value))
3679 {
3680 *x = value;
3681 return IC_SINGLE;
3682 }
3683
3684 /* Since movih is 32 bits, do not use it here, better code may
3685 be generated later. */
3686 if (const_ok_for_cskyv2 (value_invert)
3687 && !CSKY_CONST_OK_FOR_MOVIH (value_invert))
3688 {
3689 *x = value_invert;
3690 return IC_APPEND_NOT;
3691 }
3692
3693 /* One immediate generate instruction, and one 16-bit subi or addi. */
3694 for (i = 1; i <= 32; i++)
3695 {
3696 if (const_ok_for_cskyv2 (value - i)
3697 && !CSKY_CONST_OK_FOR_MOVIH (value - i))
3698 {
3699 *x = value - i;
3700 *y = i;
3701 return IC_APPEND_ADDI;
3702 }
3703
3704 if (const_ok_for_cskyv2 (value + i)
3705 && !CSKY_CONST_OK_FOR_MOVIH (value - i))
3706 {
3707 *x = value + i;
3708 *y = i;
3709 return IC_APPEND_SUBI;
3710 }
3711 }
3712
3713 /* Generate bgeni + addi. */
3714 if (CSKY_CONST_OK_FOR_Ub (value & 0xfffff000))
3715 {
3716 *x = (value & 0xfffff000);
3717 *y = (value & 0xfff);
3718 return IC_BGENI_ADDI;
3719 }
3720
3721 /* Generate bgeni + subi. */
3722 lobits = value & 0xfff;
3723 hibits = (unsigned HOST_WIDE_INT)(value & 0xfffff000) + (1 << 12);
3724 if (exact_log2 (hibits) >= 1
3725 && exact_log2 (hibits) <= 30
3726 && lobits != 0)
3727 {
3728 *x = hibits;
3729 *y = (0x1000 - lobits);
3730 return IC_BGENI_SUBI;
3731 }
3732
3733 /* One immediate generate instruction, and one bseti or bclri. */
3734 bit = 0x80000000ULL;
3735 for (i = 0; i <= 31; i++)
3736 {
3737 if (const_ok_for_cskyv2 (value & ~bit)
3738 && !CSKY_CONST_OK_FOR_MOVIH (value & ~bit))
3739 {
3740 *y = bit;
3741 *x = (value & ~bit);
3742 return IC_APPEND_BSETI;
3743 }
3744
3745 if (const_ok_for_cskyv2 (value | bit)
3746 && !CSKY_CONST_OK_FOR_MOVIH (value | bit))
3747 {
3748 *y = ~bit & 0xffffffff;
3749 *x = value | bit;
3750 return IC_APPEND_BCLRI;
3751 }
3752
3753 bit >>= 1;
3754 }
3755
3756 /* One immediate generate instruction, and one rotli or lsli. */
3757 shf = value;
3758 rot = value;
3759 for (i = 1; i < 31; i++)
3760 {
3761 int c;
3762
3763 /* Rotate left. */
3764 c = rot << 31;
3765 rot >>= 1;
3766 rot &= 0x7FFFFFFF;
3767 rot |= c;
3768
3769 if (const_ok_for_cskyv2 (rot) && !CSKY_CONST_OK_FOR_MOVIH (rot))
3770 {
3771 *y = i;
3772 *x = rot;
3773 return IC_APPEND_ROTLI;
3774 }
3775
3776 /* Can't use logical shift when low order bit is one. */
3777 if (shf & 1)
3778 shf = 0;
3779 else
3780 shf >>= 1;
3781
3782 if (shf != 0 && const_ok_for_cskyv2 (shf)
3783 && !CSKY_CONST_OK_FOR_MOVIH (shf))
3784 {
3785 *y = i;
3786 *x = shf;
3787 return IC_APPEND_LSLI;
3788 }
3789 }
3790
3791 /* One immediate generate instruction, and one ixh. */
3792 if (CSKY_ISA_FEATURE (E2)
3793 && (value % 3) == 0
3794 && const_ok_for_cskyv2 (value / 3)
3795 && !CSKY_CONST_OK_FOR_MOVIH (value / 3))
3796 {
3797 *x = value / 3;
3798 return IC_APPEND_IXH;
3799 }
3800
3801 /* One immediate generate instruction, and one ixw. */
3802 if (CSKY_ISA_FEATURE (E2)
3803 && (value % 5) == 0
3804 && const_ok_for_cskyv2 (value / 5)
3805 && !CSKY_CONST_OK_FOR_MOVIH (value / 5))
3806 {
3807 *x = value / 5;
3808 return IC_APPEND_IXW;
3809 }
3810
3811 /* Generate movih + bseti. */
3812 if (CSKY_CONST_OK_FOR_Ub (value & 0xffff))
3813 {
3814 *x = value & 0xffff0000;
3815 *y = value & 0xffff;
3816 return IC_APPEND_BSETI;
3817 }
3818
3819 /* Generate movih + not. */
3820 if (CSKY_CONST_OK_FOR_MOVIH (value_invert))
3821 {
3822 *x = value_invert;
3823 return IC_APPEND_NOT;
3824 }
3825
3826 /* One movih, and one 16bits addi or subi. */
3827 for (i = 1; i <= 32; i++)
3828 {
3829 if (CSKY_CONST_OK_FOR_MOVIH (value - i))
3830 {
3831 *x = value - i;
3832 *y = i;
3833 return IC_APPEND_ADDI;
3834 }
3835
3836 if (CSKY_CONST_OK_FOR_MOVIH (value + i))
3837 {
3838 *x = value + i;
3839 *y = i;
3840 return IC_APPEND_SUBI;
3841 }
3842 }
3843
3844 /* One movih, and one bseti or bclri. */
3845 bit = 0x80000000ULL;
3846 for (i = 0; i <= 31; i++)
3847 {
3848 if (CSKY_CONST_OK_FOR_MOVIH (value & ~bit))
3849 {
3850 *y = bit;
3851 *x = value & ~bit;
3852 return IC_APPEND_BSETI;
3853 }
3854
3855 if (CSKY_CONST_OK_FOR_MOVIH (value | bit))
3856 {
3857 *y = ~bit & 0xffffffff;
3858 *x = value | bit;
3859 return IC_APPEND_BCLRI;
3860 }
3861
3862 bit >>= 1;
3863 }
3864
3865 /* One movih, and one rotli or lsli. */
3866 shf = value;
3867 rot = value;
3868 for (i = 1; i < 31; i++)
3869 {
3870 int c;
3871
3872 /* Rotate left. */
3873 c = rot << 31;
3874 rot >>= 1;
3875 rot &= 0x7FFFFFFF;
3876 rot |= c;
3877
3878 if (CSKY_CONST_OK_FOR_MOVIH (rot))
3879 {
3880 *y = i;
3881 *x = rot;
3882 return IC_APPEND_ROTLI;
3883 }
3884
3885 /* Can't use logical shift when low order bit is one. */
3886 if (shf & 1)
3887 shf = 0;
3888 else
3889 shf >>= 1;
3890
3891 if (shf != 0 && CSKY_CONST_OK_FOR_MOVIH (shf))
3892 {
3893 *y = i;
3894 *x = shf;
3895 return IC_APPEND_LSLI;
3896 }
3897 }
3898
3899 return IC_UNINLINABLE;
3900 }
3901
3902
3903 /* Actually output a constant using a trick.
3904 FIXME: I think this would be better handled by a splitter than at the
3905 asm output level. */
3906
3907 static const char *
3908 csky_output_inline_const (machine_mode mode, rtx operands[])
3909 {
3910 HOST_WIDE_INT x = 0, y = 0;
3911 enum csky_inline_const_type trick_type;
3912 rtx out_operands[3];
3913 char buf[256];
3914 char load_op[128];
3915 const char *dst_fmt;
3916 HOST_WIDE_INT value = INTVAL (operands[1]);
3917 int ivalue = (int) value;
3918 unsigned int uvalue = (unsigned int) value;
3919
3920 trick_type = try_csky_constant_tricks (value, &x, &y);
3921 /* lrw's are handled separately: Large inlinable constants never get
3922 turned into lrw's. Our caller uses try_csky_constant_tricks to back
3923 off to an lrw rather than calling this routine. */
3924 gcc_assert (trick_type != IC_UNINLINABLE);
3925
3926 /* Operands: 0 = dst, 1 = load immedate., 2 = adjust immedate. */
3927 out_operands[0] = operands[0];
3928 out_operands[1] = GEN_INT (x);
3929 if (trick_type != IC_SINGLE && trick_type != IC_APPEND_NOT)
3930 out_operands[2] = GEN_INT (y);
3931
3932 /* Select dst format based on mode. */
3933 if (mode == DImode && TARGET_BIG_ENDIAN)
3934 dst_fmt = "%R0";
3935 else
3936 dst_fmt = "%0";
3937
3938 /* Try movi16: 0~31,movi32: 0~65535. */
3939 if (CSKY_CONST_OK_FOR_I (x))
3940 sprintf (load_op, "movi\t%s, %%1", dst_fmt);
3941 /* Try exact power of two - 1. */
3942 else if (CSKY_CONST_OK_FOR_Uc (x))
3943 sprintf (load_op, "bmaski\t%s, %%N1", dst_fmt);
3944 /* Try movih. */
3945 else if (CSKY_CONST_OK_FOR_MOVIH (x))
3946 sprintf (load_op, "movih\t%s, %%H1", dst_fmt);
3947 else
3948 {
3949 sprintf (load_op, "BADMOVI-inline_const %s, %%1", dst_fmt);
3950 gcc_unreachable ();
3951 }
3952
3953 switch (trick_type)
3954 {
3955 case IC_SINGLE:
3956 strcpy (buf, load_op);
3957 break;
3958 /* Add instruction 'not'. */
3959 case IC_APPEND_NOT:
3960 sprintf (buf, "%s\n\tnot\t%s, %s\t// %d 0x%x", load_op, dst_fmt,
3961 dst_fmt, ivalue, uvalue);
3962 break;
3963 /* Add instruction 'addi'. */
3964 case IC_APPEND_ADDI:
3965 sprintf (buf, "%s\n\taddi\t%s, %s, %%2\t// %d 0x%x", load_op,
3966 dst_fmt, dst_fmt, ivalue, uvalue);
3967 break;
3968 /* Add instruction 'subi'. */
3969 case IC_APPEND_SUBI:
3970 sprintf (buf, "%s\n\tsubi\t%s, %s, %%2\t// %d 0x%x", load_op,
3971 dst_fmt, dst_fmt, ivalue, uvalue);
3972 break;
3973 /* Add instruction 'addi', the last instruction is bgeni. */
3974 case IC_BGENI_ADDI:
3975 sprintf (buf, "%s\n\taddi\t%s, %s, %%2\t// %d 0x%x", load_op,
3976 dst_fmt, dst_fmt, ivalue, uvalue);
3977 break;
3978 /* Add instruction 'subi', the last instruction is bgeni. */
3979 case IC_BGENI_SUBI:
3980 sprintf (buf, "%s\n\tsubi\t%s, %s, %%2\t// %d 0x%x", load_op,
3981 dst_fmt, dst_fmt, ivalue, uvalue);
3982 break;
3983 /* Add instruction 'bseti'. */
3984 case IC_APPEND_BSETI:
3985 sprintf (buf, "%s\n\tbseti\t%s, %s, %%P2\t// %d 0x%x", load_op,
3986 dst_fmt, dst_fmt, ivalue, uvalue);
3987 break;
3988 /* Add instruction 'movi'. */
3989 case IC_APPEND_MOVI:
3990 sprintf (buf, "%s\n\tmovi\t%s, %%2\t// %d 0x%x", load_op, dst_fmt,
3991 ivalue, uvalue);
3992 break;
3993 /* Add instruction 'bclri'. */
3994 case IC_APPEND_BCLRI:
3995 sprintf (buf, "%s\n\tbclri\t%s, %s, %%Q2\t// %d 0x%x", load_op,
3996 dst_fmt, dst_fmt, ivalue, uvalue);
3997 break;
3998 /* Add instruction 'rotli'. */
3999 case IC_APPEND_ROTLI:
4000 sprintf (buf, "%s\n\trotli\t%s, %s, %%2\t// %d 0x%x", load_op,
4001 dst_fmt, dst_fmt, ivalue, uvalue);
4002 break;
4003 /* Add instruction 'lsli'. */
4004 case IC_APPEND_LSLI:
4005 sprintf (buf, "%s\n\tlsli\t%s, %s, %%2\t// %d 0x%x", load_op,
4006 dst_fmt, dst_fmt, ivalue, uvalue);
4007 break;
4008 /* Add instruction 'ixh'. */
4009 case IC_APPEND_IXH:
4010 sprintf (buf, "%s\n\tixh\t%s, %s, %s\t// %d 0x%x", load_op,
4011 dst_fmt, dst_fmt, dst_fmt, ivalue, uvalue);
4012 break;
4013 /* Add instruction 'ixw'. */
4014 case IC_APPEND_IXW:
4015 sprintf (buf, "%s\n\tixw\t%s, %s, %s\t// %d 0x%x", load_op,
4016 dst_fmt, dst_fmt, dst_fmt, ivalue, uvalue);
4017 break;
4018 default:
4019 return "";
4020 }
4021
4022 output_asm_insn (buf, out_operands);
4023
4024 return "";
4025 }
4026
4027 /* This is a helper function for the Uo constraint for movsi patterns. */
4028
4029 bool
4030 csky_inlinable_constant (HOST_WIDE_INT value)
4031 {
4032 HOST_WIDE_INT x, y;
4033 return (!(CSKY_TARGET_ARCH (CK802) || CSKY_TARGET_ARCH (CK801))
4034 && try_csky_constant_tricks (value, &x, &y));
4035 }
4036
4037
4038 /* Return true if the constant VAL can be expressed by an 8-bit constant
4039 with a shift value, filling in *BASE and *SHIFT. */
4040
4041 bool
4042 csky_shifted_imm8_constant (unsigned HOST_WIDE_INT val,
4043 unsigned int *base, unsigned int *shift)
4044 {
4045 unsigned HOST_WIDE_INT mask = 0xff;
4046 int i;
4047 val = val & (unsigned HOST_WIDE_INT) 0xffffffffu;
4048 if (val == 0)
4049 return 0;
4050
4051 for (i = 0; i < 25; i++)
4052 if ((val & (mask << i)) == val)
4053 {
4054 if (base)
4055 *base = (unsigned int) (val >> i);
4056 if (shift)
4057 *shift = (unsigned int) i;
4058 return true;
4059 }
4060
4061 return false;
4062 }
4063
4064
4065 /* Output a move of a word or less value. */
4066
4067 const char *
4068 csky_output_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
4069 machine_mode mode ATTRIBUTE_UNUSED)
4070 {
4071 rtx dst = operands[0];
4072 rtx src = operands[1];
4073 struct csky_address op0, op1;
4074
4075 if (REG_P (dst))
4076 {
4077 /* The situation mov reg to reg. */
4078 if (REG_P (src))
4079 {
4080 int dstreg = REGNO (dst);
4081 int srcreg = REGNO (src);
4082
4083 /* hilo registers exchange their places,
4084 and their order of Dimode as same as other
4085 general registers in LITTLE_ENDIAN mode. */
4086 if (TARGET_BIG_ENDIAN)
4087 {
4088 if (dstreg == CSKY_HI_REGNUM)
4089 return "mthi\t%1";
4090 else if (dstreg == CSKY_LO_REGNUM)
4091 return "mtlo\t%1";
4092 else if (srcreg == CSKY_HI_REGNUM)
4093 return "mfhi\t%0";
4094 else if (srcreg == CSKY_LO_REGNUM)
4095 return "mflo\t%0";
4096 }
4097 else
4098 {
4099 if (dstreg == CSKY_HI_REGNUM)
4100 return "mtlo\t%1";
4101 else if (dstreg == CSKY_LO_REGNUM)
4102 return "mthi\t%1";
4103 else if (srcreg == CSKY_HI_REGNUM)
4104 return "mflo\t%0";
4105 else if (srcreg == CSKY_LO_REGNUM)
4106 return "mfhi\t%0";
4107 }
4108
4109 if (CSKY_VREG_P (dstreg) && CSKY_VREG_P (srcreg))
4110 {
4111 if (CSKY_ISA_FEATURE (fpv2_sf))
4112 return "fmovs\t%0, %1";
4113 else if (CSKY_ISA_FEATURE (fpv3_sf))
4114 return "fmov.32\t%0, %1";
4115 else
4116 gcc_unreachable ();
4117 }
4118 if (CSKY_VREG_P (dstreg))
4119 {
4120 if (CSKY_ISA_FEATURE (fpv2_sf))
4121 return "fmtvrl\t%0, %1";
4122 else if (CSKY_ISA_FEATURE (fpv3_sf))
4123 return "fmtvr.32.1\t%0, %1";
4124 else
4125 gcc_unreachable ();
4126 }
4127 if (CSKY_VREG_P (srcreg))
4128 {
4129 if (CSKY_ISA_FEATURE (fpv2_sf))
4130 return "fmfvrl\t%0, %1";
4131 else if (CSKY_ISA_FEATURE (fpv3_sf))
4132 return "fmfvr.32.1\t%0, %1";
4133 else
4134 gcc_unreachable ();
4135 }
4136 if (REGNO (src) == CSKY_CC_REGNUM)
4137 return "mvc\t%0";
4138 else
4139 return "mov\t%0, %1";
4140 }
4141 /* The situation mov memory to reg. */
4142 else if (GET_CODE (src) == MEM)
4143 {
4144 decompose_csky_address (XEXP (src, 0), &op1);
4145
4146 if (op1.index)
4147 switch (GET_MODE (src))
4148 {
4149 case E_HImode:
4150 case E_HFmode:
4151 return "ldr.h\t%0, %1";
4152 case E_QImode:
4153 return "ldr.b\t%0, %1";
4154 case E_SImode:
4155 case E_SFmode:
4156 if (CSKY_VREG_P (REGNO (dst)))
4157 {
4158 if (CSKY_ISA_FEATURE(fpv2_sf))
4159 return "fldrs\t%0, %1";
4160 else if (CSKY_ISA_FEATURE(fpv3_sf))
4161 return "fldr.32\t%0, %1";
4162 else
4163 gcc_unreachable ();
4164 }
4165 else
4166 return "ldr.w\t%0, %1";
4167 default:
4168 gcc_unreachable ();
4169 }
4170 /* Generate lrw rx, [LABEL]. This happens when the compiler
4171 generates constant pool references and uses lrw to get the
4172 constant into memory. */
4173 else if (op1.label)
4174 return "lrw\t%0, %1";
4175 /* Generate lrs.w rx, [symbol@GOT/PLT]. */
4176 else if (flag_pic == 1 && op1.disp && GET_CODE (op1.disp) == UNSPEC)
4177 return "lrs.w\t%0, %1";
4178 else
4179 switch (GET_MODE (src))
4180 {
4181 case E_HImode:
4182 case E_HFmode:
4183 return "ld.h\t%0, %1";
4184 case E_QImode:
4185 return "ld.b\t%0, %1";
4186 case E_SFmode:
4187 case E_SImode:
4188 if (CSKY_VREG_P (REGNO (dst)))
4189 {
4190 if (CSKY_ISA_FEATURE(fpv2_sf))
4191 return "flds\t%0, %1";
4192 else if (CSKY_ISA_FEATURE(fpv3_sf))
4193 return "fld.32\t%0, %1";
4194 else
4195 gcc_unreachable ();
4196 }
4197 else
4198 return "ld.w\t%0, %1";
4199 default:
4200 gcc_unreachable ();
4201 }
4202 }
4203 /* The situation mov integer to reg. */
4204 else if (GET_CODE (src) == CONST_INT ||
4205 (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode))
4206 {
4207 HOST_WIDE_INT x, y;
4208 const REAL_VALUE_TYPE *d;
4209 long l;
4210
4211 if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
4212 {
4213 d = CONST_DOUBLE_REAL_VALUE (src);
4214 REAL_VALUE_TO_TARGET_SINGLE (*d, l);
4215 operands[1] = GEN_INT (l);
4216 src = operands[1];
4217 }
4218
4219 if (try_csky_constant_tricks (INTVAL (src), &x, &y))
4220 return csky_output_inline_const (SImode, operands);
4221 /* Return '#' to split it. */
4222 else if (CSKY_CONST_OK_FOR_T (INTVAL (src)))
4223 return "#";
4224 else
4225 return "lrw\t%0, %x1\t";
4226 }
4227 else if (TARGET_ANCHOR && GET_CODE (src) == SYMBOL_REF)
4228 {
4229 if (SYMBOL_REF_FUNCTION_P (src))
4230 return "lrw\t%0, %1@BTEXT";
4231 else
4232 return "lrw\t%0, %1@BDATA";
4233 }
4234 else if (GET_CODE (src) == UNSPEC
4235 && XINT (src, 1) == UNSPEC_PIC_SYMBOL_GRS)
4236 return "grs\t%0, %1";
4237 else
4238 return "lrw\t%0, %1";
4239 }
4240 else if (GET_CODE (dst) == MEM)
4241 {
4242 decompose_csky_address (XEXP (dst, 0), &op0);
4243
4244 if (op0.index)
4245 switch (GET_MODE (src))
4246 {
4247 case E_HImode:
4248 return "str.h\t%1, %0";
4249 case E_QImode:
4250 return "str.b\t%1, %0";
4251 case E_SFmode:
4252 case E_SImode:
4253 if (CSKY_VREG_P (REGNO (src)))
4254 {
4255 if (CSKY_ISA_FEATURE(fpv2_sf))
4256 return "fstrs\t%1, %0";
4257 else if (CSKY_ISA_FEATURE(fpv3_sf))
4258 return "fstr.32\t%1, %0";
4259 else
4260 gcc_unreachable ();
4261 }
4262 else
4263 return "str.w\t%1, %0";
4264 default:
4265 gcc_unreachable ();
4266 }
4267 else
4268 switch (GET_MODE (dst))
4269 {
4270 case E_HImode:
4271 return "st.h\t%1, %0";
4272 case E_QImode:
4273 return "st.b\t%1, %0";
4274 case E_SImode:
4275 case E_SFmode:
4276 if (CSKY_VREG_P (REGNO (src)))
4277 {
4278 if (CSKY_ISA_FEATURE(fpv2_sf))
4279 return "fsts\t%1, %0";
4280 else if (CSKY_ISA_FEATURE(fpv3_sf))
4281 return "fst.32\t%1, %0";
4282 else
4283 gcc_unreachable ();
4284 }
4285 else
4286 return "st.w\t%1, %0";
4287 default:
4288 gcc_unreachable ();
4289 }
4290 }
4291
4292 gcc_unreachable ();
4293 }
4294
4295
4296 /* Output a move of a word or less value. Specific for ck801. */
4297
4298 const char *
4299 csky_output_ck801_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
4300 machine_mode mode ATTRIBUTE_UNUSED)
4301 {
4302 rtx dst = operands[0];
4303 rtx src = operands[1];
4304 struct csky_address op1;
4305
4306 if (REG_P (dst))
4307 {
4308 if (REG_P (src))
4309 return "mov\t%0, %1";
4310 else if (GET_CODE (src) == MEM)
4311 {
4312 decompose_csky_address (XEXP (src, 0), &op1);
4313
4314 /* Generate lrw rx, [LABEL]. This happens when the compiler
4315 generates constant pool references and uses lrw to get the
4316 constant in memory. */
4317 if (op1.label)
4318 return "lrw\t%0, %1";
4319 else
4320 switch (GET_MODE (src))
4321 {
4322 case E_HImode:
4323 return "ld.h\t%0, %1";
4324 case E_QImode:
4325 return "ld.b\t%0, %1";
4326 case E_SFmode:
4327 case E_SImode:
4328 return "ld.w\t%0, %1";
4329 default:
4330 gcc_unreachable ();
4331 }
4332 }
4333 else if (GET_CODE (src) == CONST_INT)
4334 {
4335 if (REGNO (dst) > 7)
4336 return "lrw\t%0, %x1\t";
4337 else if (CSKY_CONST_OK_FOR_N (INTVAL (src) + 1))
4338 return "movi\t%0, %1";
4339 /* Return '#' to split it. */
4340 else if (CSKY_CONST_OK_FOR_T (INTVAL (src)))
4341 return "#";
4342 else if (csky_shifted_imm8_constant (INTVAL (src), NULL, NULL))
4343 return "#";
4344 else
4345 return "lrw\t%0, %x1\t";
4346 }
4347 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
4348 {
4349 const REAL_VALUE_TYPE *d;
4350 long l;
4351
4352 d = CONST_DOUBLE_REAL_VALUE (src);
4353 REAL_VALUE_TO_TARGET_SINGLE (*d, l);
4354 operands[1] = GEN_INT (l);
4355 src = operands[1];
4356
4357 if (CSKY_CONST_OK_FOR_N (INTVAL (src) + 1))
4358 return "movi\t%0, %1";
4359 else
4360 return "lrw\t%0, %x1\t";
4361 }
4362 else if (TARGET_ANCHOR && GET_CODE (src) == SYMBOL_REF)
4363 {
4364 if (SYMBOL_REF_FUNCTION_P (src))
4365 return "lrw\t%0, %1@BTEXT";
4366 else
4367 return "lrw\t%0, %1@BDATA";
4368 }
4369 else
4370 return "lrw\t%0, %1";
4371 }
4372 else if (GET_CODE (dst) == MEM)
4373 switch (GET_MODE (dst))
4374 {
4375 case E_HImode:
4376 return "st.h\t%1, %0";
4377 case E_QImode:
4378 return "st.b\t%1, %0";
4379 case E_SImode:
4380 case E_SFmode:
4381 return "st.w\t%1, %0";
4382 default:
4383 gcc_unreachable ();
4384 }
4385
4386 gcc_unreachable ();
4387 }
4388
4389
4390 /* Return a sequence of instructions to perform DI or DF move.
4391 Since the CSKY cannot move a DI or DF in one instruction, we have
4392 to take care when we see overlapping source and dest registers. */
4393
4394 const char *
4395 csky_output_movedouble (rtx operands[],
4396 machine_mode mode ATTRIBUTE_UNUSED)
4397 {
4398 rtx dst = operands[0];
4399 rtx src = operands[1];
4400
4401 if (REG_P (dst))
4402 {
4403 if (REG_P (src))
4404 {
4405 int dstreg = REGNO (dst);
4406 int srcreg = REGNO (src);
4407
4408 if (CSKY_HILO_REG_P (srcreg))
4409 {
4410 if (TARGET_BIG_ENDIAN)
4411 return "mfhi\t%0\n\tmflo\t%R0";
4412 else
4413 return "mfhi\t%R0\n\tmflo\t%0";
4414 }
4415 else if (CSKY_HILO_REG_P (dstreg))
4416 {
4417 if (TARGET_BIG_ENDIAN)
4418 return "mthi\t%1\n\tmtlo\t%R1";
4419 else
4420 return "mthi\t%R1\n\tmtlo\t%1";
4421 }
4422 else if (CSKY_VREG_P (srcreg) && CSKY_VREG_P (dstreg))
4423 {
4424 if (CSKY_ISA_FEATURE(fpv2_df))
4425 return "fmovd\t%0, %1";
4426 else if (CSKY_ISA_FEATURE(fpv3_df))
4427 return "fmov.64\t%0, %1";
4428 else
4429 gcc_unreachable ();
4430 }
4431 else if (CSKY_VREG_P (srcreg))
4432 {
4433 /* Since the vector registers in fpuv2_soft processors
4434 like ck803f are 32 bits wide, just one insn is needed
4435 to complete the move operation. */
4436 if (TARGET_SOFT_FPU)
4437 return "fmfvrl\t%0, %1";
4438 else if (TARGET_BIG_ENDIAN)
4439 {
4440 if (CSKY_ISA_FEATURE(fpv2_df))
4441 return "fmfvrh\t%0, %1\n\tfmfvrl\t%R0, %1";
4442 else if (CSKY_ISA_FEATURE(fpv3_df))
4443 return "fmfvr.64\t%R0, %0, %1";
4444 else
4445 gcc_unreachable ();
4446 }
4447 else
4448 {
4449 if (CSKY_ISA_FEATURE(fpv2_df))
4450 return "fmfvrh\t%R0, %1\n\tfmfvrl\t%0, %1";
4451 else if (CSKY_ISA_FEATURE(fpv3_df))
4452 return "fmfvr.64\t%0, %R0, %1";
4453 else
4454 gcc_unreachable ();
4455 }
4456 }
4457 else if (CSKY_VREG_P (dstreg))
4458 {
4459 if (TARGET_SOFT_FPU)
4460 return "fmtvrl\t%0, %1";
4461 else if (TARGET_BIG_ENDIAN)
4462 {
4463 if (CSKY_ISA_FEATURE(fpv2_df))
4464 return "fmtvrh\t%0, %1\n\tfmtvrl\t%0, %R1";
4465 else if (CSKY_ISA_FEATURE(fpv3_df))
4466 return "fmtvr.64\t%0, %R1, %1";
4467 else
4468 gcc_unreachable ();
4469 }
4470 else
4471 {
4472 if (CSKY_ISA_FEATURE(fpv2_df))
4473 return "fmtvrh\t%0, %R1\n\tfmtvrl\t%0, %1";
4474 else if (CSKY_ISA_FEATURE(fpv3_df))
4475 return "fmtvr.64\t%0, %1, %R1";
4476 else
4477 gcc_unreachable ();
4478 }
4479 }
4480
4481 /* Ensure the second source not overwritten. */
4482 if (srcreg + 1 == dstreg)
4483 return "mov\t%R0, %R1\n\tmov\t%0, %1";
4484 else
4485 return "mov\t%0, %1\n\tmov\t%R0, %R1";
4486 }
4487 else if (GET_CODE (src) == MEM)
4488 {
4489 rtx memexp = XEXP (src, 0);
4490 int dstreg = REGNO (dst);
4491 int basereg = -1;
4492 struct csky_address op0;
4493
4494 decompose_csky_address (XEXP (src, 0), &op0);
4495
4496 if (GET_CODE (memexp) == LABEL_REF
4497 || (GET_CODE (memexp) == CONST
4498 && GET_CODE (XEXP (memexp, 0)) == PLUS
4499 && GET_CODE (XEXP (XEXP (memexp, 0), 0)) == LABEL_REF))
4500 return "lrw\t%0, [%1]\n\tlrw\t%R0, [%R1]";
4501 else if (GET_CODE (memexp) == REG)
4502 basereg = REGNO (memexp);
4503 else if (GET_CODE (memexp) == PLUS)
4504 {
4505 if (GET_CODE (XEXP (memexp, 0)) == REG)
4506 basereg = REGNO (XEXP (memexp, 0));
4507 else if (GET_CODE (XEXP (memexp, 1)) == REG)
4508 basereg = REGNO (XEXP (memexp, 1));
4509 else
4510 gcc_unreachable ();
4511 }
4512 else
4513 gcc_unreachable ();
4514
4515
4516 /* When FPUV2. */
4517 if (CSKY_VREG_P (dstreg))
4518 {
4519 if (op0.index)
4520 {
4521 if (CSKY_ISA_FEATURE(fpv2_df))
4522 return "fldrd\t%0, %1";
4523 else if (CSKY_ISA_FEATURE(fpv3_df))
4524 return "fldr.64\t%0, %1";
4525 else
4526 gcc_unreachable ();
4527 }
4528 else
4529 {
4530 if (CSKY_ISA_FEATURE(fpv2_df))
4531 return "fldd\t%0, %1";
4532 else if (CSKY_ISA_FEATURE(fpv3_df))
4533 return "fld.64\t%0, %1";
4534 else
4535 gcc_unreachable ();
4536 }
4537 }
4538 /* FIXME length attribute is wrong here. */
4539 if (dstreg == basereg)
4540 /* Just load them in reverse order. */
4541 return "ld.w\t%R0, %R1\n\tld.w\t%0, %1";
4542 else
4543 return "ld.w\t%0, %1\n\tld.w\t%R0, %R1";
4544 }
4545 else if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
4546 {
4547 split_double (src, operands + 2, operands + 3);
4548
4549 if (CSKY_CONST_OK_FOR_I (INTVAL (operands[2])))
4550 output_asm_insn ("movi\t%0, %2", operands);
4551 else if (CSKY_CONST_OK_FOR_Uc (INTVAL (operands[2])))
4552 output_asm_insn ("bmaski\t%0, %N2", operands);
4553 else if (CSKY_CONST_OK_FOR_Ub (INTVAL (operands[2])))
4554 output_asm_insn ("bgeni\t%0, %P2", operands);
4555 else
4556 output_asm_insn ("lrw\t%0, %2", operands);
4557
4558 if (CSKY_CONST_OK_FOR_I (INTVAL (operands[3])))
4559 output_asm_insn ("movi\t%R0, %3", operands);
4560 else if (CSKY_CONST_OK_FOR_Uc (INTVAL (operands[3])))
4561 output_asm_insn ("bmaski\t%R0, %N3", operands);
4562
4563 else if (CSKY_CONST_OK_FOR_Ub (INTVAL (operands[3])))
4564 output_asm_insn ("bgeni\t%R0, %P3", operands);
4565 else
4566 output_asm_insn ("lrw\t%R0, %3", operands);
4567
4568 return "";
4569 }
4570 else
4571 gcc_unreachable ();
4572 }
4573 else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
4574 {
4575 rtx memexp = XEXP (dst, 0);
4576 int srcreg = REGNO (src);
4577 int basereg = -1;
4578 struct csky_address op0;
4579
4580 decompose_csky_address (XEXP (dst, 0), &op0);
4581
4582 if (GET_CODE (memexp) == REG)
4583 basereg = REGNO (memexp);
4584 else if (GET_CODE (memexp) == PLUS)
4585 {
4586 if (GET_CODE (XEXP (memexp, 0)) == REG)
4587 basereg = REGNO (XEXP (memexp, 0));
4588 else if (GET_CODE (XEXP (memexp, 1)) == REG)
4589 basereg = REGNO (XEXP (memexp, 1));
4590 else
4591 gcc_unreachable ();
4592 }
4593 else
4594 gcc_unreachable ();
4595
4596 /* When FPUV2. */
4597 if (CSKY_VREG_P (srcreg))
4598 {
4599 if (op0.index)
4600 {
4601 if (CSKY_ISA_FEATURE(fpv2_df))
4602 return "fstrd\t%1, %0";
4603 else if (CSKY_ISA_FEATURE(fpv3_df))
4604 return "fstr.64\t%1, %0";
4605 else
4606 gcc_unreachable ();
4607 }
4608 else
4609 {
4610 if (CSKY_ISA_FEATURE(fpv2_df))
4611 return "fstd\t%1, %0";
4612 else if (CSKY_ISA_FEATURE(fpv3_df))
4613 return "fst.64\t%1, %0";
4614 else
4615 gcc_unreachable ();
4616 }
4617 }
4618 /* FIXME length attribute is wrong here. */
4619 if (srcreg == basereg)
4620 /* Just load them in reverse order. */
4621 return "st.w\t%R1, %R0\n\tst.w\t%1, %0";
4622 else
4623 return "st.w\t%1, %0\n\tst.w\t%R1, %R0";
4624 }
4625 else
4626 gcc_unreachable ();
4627 }
4628
4629
4630 const char *
4631 csky_output_ck801_movedouble (rtx operands[],
4632 machine_mode mode ATTRIBUTE_UNUSED)
4633 {
4634 rtx dst = operands[0];
4635 rtx src = operands[1];
4636
4637 if (REG_P (dst))
4638 {
4639 if (REG_P (src))
4640 {
4641 int dstreg = REGNO (dst);
4642 int srcreg = REGNO (src);
4643
4644 /* Ensure the second source not overwritten. */
4645 if (srcreg + 1 == dstreg)
4646 return "mov\t%R0, %R1\n\tmov\t%0, %1";
4647 else
4648 return "mov\t%0, %1\n\tmov\t%R0, %R1";
4649 }
4650 else if (GET_CODE (src) == MEM)
4651 {
4652 rtx memexp = XEXP (src, 0);
4653 int dstreg = REGNO (dst);
4654 int basereg = -1;
4655 struct csky_address op0;
4656
4657 decompose_csky_address (XEXP (src, 0), &op0);
4658
4659 if (GET_CODE (memexp) == LABEL_REF
4660 || (GET_CODE (memexp) == CONST
4661 && GET_CODE (XEXP (memexp, 0)) == PLUS
4662 && GET_CODE (XEXP (XEXP (memexp, 0), 0)) == LABEL_REF))
4663 return "lrw\t%0, [%1]\n\tlrw\t%R0, [%R1]";
4664 else if (GET_CODE (memexp) == REG)
4665 basereg = REGNO (memexp);
4666 else if (GET_CODE (memexp) == PLUS)
4667 {
4668 if (GET_CODE (XEXP (memexp, 0)) == REG)
4669 basereg = REGNO (XEXP (memexp, 0));
4670 else if (GET_CODE (XEXP (memexp, 1)) == REG)
4671 basereg = REGNO (XEXP (memexp, 1));
4672 else
4673 gcc_unreachable ();
4674 }
4675 else
4676 gcc_unreachable ();
4677
4678 /* FIXME length attribute is wrong here. */
4679 if (dstreg == basereg)
4680 /* Just load them in reverse order. */
4681 return "ld.w\t%R0, %R1\n\tld.w\t%0, %1";
4682 else
4683 return "ld.w\t%0, %1\n\tld.w\t%R0, %R1";
4684 }
4685 else if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
4686 {
4687 split_double (src, operands + 2, operands + 3);
4688
4689 if (REGNO (dst) <= 7
4690 && CSKY_CONST_OK_FOR_N (INTVAL (operands[2]) + 1))
4691 output_asm_insn ("movi\t%0, %2", operands);
4692 else
4693 output_asm_insn ("lrw\t%0, %2", operands);
4694
4695
4696 if (REGNO (dst) <= 6
4697 && CSKY_CONST_OK_FOR_N (INTVAL (operands[3]) + 1))
4698 output_asm_insn ("movi\t%R0, %3", operands);
4699 else
4700 output_asm_insn ("lrw\t%R0, %3", operands);
4701
4702 return "";
4703
4704
4705 }
4706 else
4707 gcc_unreachable ();
4708 }
4709 else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
4710 {
4711 rtx memexp = XEXP (dst, 0);
4712 int srcreg = REGNO (src);
4713 int basereg = -1;
4714 struct csky_address op0;
4715
4716 decompose_csky_address (XEXP (dst, 0), &op0);
4717
4718 if (GET_CODE (memexp) == REG)
4719 basereg = REGNO (memexp);
4720 else if (GET_CODE (memexp) == PLUS)
4721 {
4722 if (GET_CODE (XEXP (memexp, 0)) == REG)
4723 basereg = REGNO (XEXP (memexp, 0));
4724 else if (GET_CODE (XEXP (memexp, 1)) == REG)
4725 basereg = REGNO (XEXP (memexp, 1));
4726 else
4727 gcc_unreachable ();
4728 }
4729 else
4730 gcc_unreachable ();
4731
4732 /* FIXME length attribute is wrong here. */
4733 if (srcreg == basereg)
4734 /* Just load them in reverse order. */
4735 return "st.w\t%R1, %R0\n\tst.w\t%1, %0";
4736 else
4737 return "st.w\t%1, %0\n\tst.w\t%R1, %R0";
4738 }
4739 else
4740 gcc_unreachable ();
4741 }
4742
4743 /* Calculate the instruction's length for moving double-word data. */
4744
4745 int
4746 csky_get_movedouble_length(rtx operands[])
4747 {
4748 rtx dst = operands[0];
4749 rtx src = operands[1];
4750
4751 if (REG_P (dst))
4752 {
4753 if (REG_P (src))
4754 {
4755 int dstreg = REGNO (dst);
4756 int srcreg = REGNO (src);
4757
4758 if (CSKY_VREG_P (srcreg) && CSKY_VREG_P (dstreg))
4759 return 4;
4760 else
4761 return 8;
4762 }
4763 else if (GET_CODE (src) == MEM)
4764 {
4765 rtx memexp = XEXP (src, 0);
4766 int dstreg = REGNO (dst);
4767 struct csky_address op0;
4768 decompose_csky_address (XEXP (src, 0), &op0);
4769
4770 if (GET_CODE (memexp) == LABEL_REF)
4771 return 8;
4772 if (CSKY_VREG_P (dstreg))
4773 return 4;
4774 return 8;
4775 }
4776 else if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
4777 {
4778 split_double (src, operands + 2, operands + 3);
4779 if (CSKY_CONST_OK_FOR_N (INTVAL (operands[2]) + 1)
4780 && CSKY_CONST_OK_FOR_N (INTVAL (operands[3]) + 1)
4781 && REGNO (operands[0]) < 6)
4782 return 4;
4783 else
4784 return 8;
4785 }
4786 }
4787 else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
4788 {
4789 rtx memexp = XEXP (dst, 0);
4790 int srcreg = REGNO (src);
4791 int offset = -1;
4792 if (CSKY_VREG_P (srcreg))
4793 return 4;
4794
4795 if (GET_CODE (memexp) == REG)
4796 offset = 0;
4797 else if (GET_CODE (memexp) == PLUS)
4798 {
4799 if (GET_CODE (XEXP (memexp, 0)) == REG)
4800 offset = INTVAL (XEXP (memexp, 1));
4801 else if (GET_CODE (XEXP (memexp, 1)) == REG)
4802 offset = INTVAL (XEXP (memexp, 0));
4803 else
4804 gcc_unreachable ();
4805 }
4806 else
4807 gcc_unreachable ();
4808
4809 if (srcreg <= 6 && offset <= 1020)
4810 return 4;
4811 else if ((srcreg == 7 && offset <= 1024) || (srcreg <= 7 && offset == 1024))
4812 return 6;
4813 else
4814 return 8;
4815 }
4816 else
4817 gcc_unreachable ();
4818
4819 return 0;
4820 }
4821
4822 /* Output float point load/store instructions for fpuv3. */
4823
4824 const char *
4825 fpuv3_output_move (rtx *operands)
4826 {
4827 rtx reg, mem, addr, ops[2];
4828 bool isload = REG_P (operands[0]);
4829
4830 const char *templ = "f%s%s.%s\t%%0, %%1";
4831 char buff[50];
4832 machine_mode mode;
4833
4834 reg = operands[isload ? 0 : 1];
4835 mem = operands[isload ? 1 : 0];
4836
4837 gcc_assert (REG_P (reg));
4838 gcc_assert (CSKY_VREG_P (REGNO (reg)));
4839 gcc_assert (MEM_P (mem));
4840
4841 mode = GET_MODE (reg);
4842 const char *type = mode == DFmode ? "64" :
4843 mode == SFmode ? "32" :
4844 mode == HFmode ? "16" :
4845 NULL;
4846 gcc_assert(type != NULL);
4847
4848 addr = XEXP (mem, 0);
4849 struct csky_address caddr;
4850 decompose_csky_address (addr, &caddr);
4851
4852 ops[0] = reg;
4853 ops[1] = mem;
4854 sprintf (buff, templ,
4855 isload ? "ld" : "st",
4856 caddr.index ? "r" : "",
4857 type);
4858 output_asm_insn (buff, ops);
4859
4860 return "";
4861 }
4862
4863 /* Check if a const_double can be used by a VFP fmovi instruction. */
4864
4865 int
4866 fpuv3_const_double_rtx (rtx x)
4867 {
4868 REAL_VALUE_TYPE r, m;
4869 r = *CONST_DOUBLE_REAL_VALUE (x);
4870
4871 /* Fpuv3 doesn't support the following values. */
4872 if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r)
4873 || r.cl == rvc_zero)
4874 return 0;
4875
4876 /* Extract sign, exponent and mantissa. */
4877 int exponent;
4878 r = real_value_abs (&r);
4879 exponent = REAL_EXP (&r);
4880
4881 bool fail;
4882 unsigned HOST_WIDE_INT mantissa, mant_hi;
4883 unsigned HOST_WIDE_INT mask;
4884 int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
4885 real_ldexp (&m, &r, point_pos - exponent);
4886 wide_int w = real_to_integer (&m, &fail, HOST_BITS_PER_WIDE_INT * 2);
4887 mantissa = w.elt (0);
4888 mant_hi = w.elt (1);
4889
4890 exponent -= 1;
4891
4892 if (!IN_RANGE (exponent, -4, 11))
4893 return 0;
4894
4895 /* If there are bits set in the low part of the mantissa, these values are
4896 not supported. */
4897 if (mantissa != 0)
4898 return 0;
4899
4900 /* Now, make the mantissa contain the most-significant bits, and the
4901 point_pos indicates the number of these bits. */
4902 point_pos -= HOST_BITS_PER_WIDE_INT;
4903 mantissa = mant_hi;
4904
4905 /* We can only allow a mantissa of 9 significant digits, top of which is always 1. */
4906 mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 9)) - 1;
4907 if ((mantissa & mask) != 0)
4908 return 0;
4909
4910 return 1;
4911 }
4912
4913
4914 /* Split operands for an AND expression when OPERANDS[2] is a constant.
4915 Note operands[0] is marked earlyclobber in this case and can be
4916 overwritten. Return true if "DONE", false otherwise. */
4917
4918 bool
4919 csky_split_and (rtx *operands)
4920 {
4921 HOST_WIDE_INT mask = INTVAL (operands[2]);
4922 rtx not_value = GEN_INT (~mask);
4923 int i;
4924
4925 /* All zeros or all ones can be handled by a move instruction. */
4926 if (mask == 0)
4927 {
4928 emit_move_insn (operands[0], const0_rtx);
4929 return true;
4930 }
4931 if (mask == -1)
4932 {
4933 emit_move_insn (operands[0], operands[1]);
4934 return true;
4935 }
4936
4937 /* Check for constants that can be handled directly by the 32-bit andi
4938 instruction. */
4939 if (CSKY_ISA_FEATURE (E2) && csky_arith_O_operand (operands[2], SImode))
4940 return false;
4941
4942 /* Try to transform to andni instruction. */
4943 if (CSKY_ISA_FEATURE (E2) && csky_arith_O_operand (not_value, SImode))
4944 {
4945 emit_insn (gen_cskyv2_andnsi3 (operands[0], not_value, operands[1]));
4946 return true;
4947 }
4948
4949 /* If there are only one or two 0 bits in the constant, we can
4950 replace the operation with bclri instructions on those bits.
4951 Note CK801 has only the 16-bit bclri that operates on a single
4952 register, so we must count a move if we are post-reload. */
4953 if (popcount_hwi (~mask & 0xffffffff)
4954 <= (reload_completed && !CSKY_ISA_FEATURE (E2) ? 1 : 2))
4955 {
4956 rtx input = operands[1];
4957
4958 if (!CSKY_ISA_FEATURE (E2))
4959 {
4960 emit_move_insn (operands[0], input);
4961 input = operands[0];
4962 }
4963
4964 for (i = 0; i < 32; i++)
4965 if ((mask & (1 << i)) == 0x0)
4966 {
4967 emit_insn (gen_bclri (operands[0], input, GEN_INT (i)));
4968 input = operands[0];
4969 }
4970 return true;
4971 }
4972
4973 /* If the constant mask is outside the [0, 4095] range for
4974 constraint O, or if constraint O is not allowed (ck801),
4975 maybe the constant is a contiguous bit range that we can
4976 handle by bit extract (low bits) or shifts (high bits). */
4977 for (i = (CSKY_ISA_FEATURE (E2) ? 13 : 1); i < 32; i++)
4978 {
4979 if ((((HOST_WIDE_INT) 1) << i) - 1 == mask)
4980 {
4981 if (CSKY_ISA_FEATURE (2E3))
4982 emit_insn (gen_cskyv2_extzv (operands[0], operands[1],
4983 GEN_INT (i), const0_rtx));
4984 else
4985 {
4986 rtx shift = GEN_INT (32 - i);
4987 rtx reg = (reload_completed
4988 ? operands[0] : gen_reg_rtx (SImode));
4989
4990 emit_insn (gen_ashlsi3 (reg, operands[1], shift));
4991 emit_insn (gen_lshrsi3 (operands[0], reg, shift));
4992 }
4993 return true;
4994 }
4995 else if ((((HOST_WIDE_INT) 1) << i) - 1 == ~mask)
4996 {
4997 rtx shift = GEN_INT (i);
4998 rtx reg = (reload_completed
4999 ? operands[0] : gen_reg_rtx (SImode));
5000
5001 emit_insn (gen_lshrsi3 (reg, operands[1], shift));
5002 emit_insn (gen_ashlsi3 (operands[0], reg, shift));
5003 return true;
5004 }
5005 }
5006
5007 /* If the constant is a negative number, it seems better to use
5008 andn and copy the NOT_VALUE to a register instead of the
5009 original value, since the NOT_VALUE is always smaller and thus
5010 more likely to be representable as a small constant.
5011 This transformation can only be done before reload because
5012 it requires a temporary. Hopefully register allocation can get
5013 rid of the extra move required for CK801. */
5014 if (!reload_completed && INTVAL (operands[2]) < 0)
5015 {
5016 rtx reg = copy_to_mode_reg (SImode, not_value);
5017
5018 if (CSKY_ISA_FEATURE (E2))
5019 emit_insn (gen_cskyv2_andnsi3 (operands[0], reg, operands[1]));
5020 else
5021 {
5022 emit_move_insn (operands[0], operands[1]);
5023 emit_insn (gen_ck801_andnsi3 (operands[0], reg, operands[0]));
5024 }
5025 return true;
5026 }
5027
5028 /* If the above ways are all not working, move the constant
5029 to a register. We can clobber operands[0] as it is
5030 marked earlyclobber in the insn constraints, but then we have to
5031 swap operands 1 and 2 to match the constraints on the 2-operand
5032 16-bit and instruction. */
5033 if (reload_completed)
5034 {
5035 emit_move_insn (operands[0], operands[2]);
5036 operands[2] = operands[1];
5037 operands[1] = operands[0];
5038 }
5039 else
5040 operands[2] = copy_to_mode_reg (SImode, operands[2]);
5041 return false;
5042 }
5043
5044 /* Split operands for an IOR expression when OPERANDS[2] is a constant.
5045 Note operands[0] is marked earlyclobber in this case and can be
5046 overwritten. Return true if "DONE", false otherwise. */
5047
5048 bool
5049 csky_split_ior (rtx *operands)
5050 {
5051 HOST_WIDE_INT mask = INTVAL (operands[2]);
5052 int i;
5053
5054 /* All zeros or all ones can be handled by a move instruction. */
5055 if (mask == 0)
5056 {
5057 emit_move_insn (operands[0], operands[1]);
5058 return true;
5059 }
5060 if (mask == -1)
5061 {
5062 emit_move_insn (operands[0], gen_int_mode (-1, SImode));
5063 return true;
5064 }
5065
5066 /* Check for constants that can be handled directly by the 32-bit ori
5067 instruction. */
5068 if (CSKY_ISA_FEATURE (E2) && csky_literal_I_operand (operands[2], SImode))
5069 return false;
5070
5071 /* If there are only one or two 1 bits in the value, we can replace
5072 the operation with bseti instructions to set those bits.
5073 Note CK801 has only the 16-bit bclri that operates on a single
5074 register, so we must count a move if we are post-reload. */
5075 if (popcount_hwi (mask & 0xffffffff)
5076 <= (reload_completed && !CSKY_ISA_FEATURE (E2) ? 1 : 2))
5077 {
5078 rtx input = operands[1];
5079
5080 if (!CSKY_ISA_FEATURE (E2))
5081 {
5082 emit_move_insn (operands[0], input);
5083 input = operands[0];
5084 }
5085
5086 for (i = 0; i < 32; i++)
5087 if (mask & (1 << i))
5088 {
5089 emit_insn (gen_bseti (operands[0], input, GEN_INT (i)));
5090 input = operands[0];
5091 }
5092 return true;
5093 }
5094
5095 /* If the above ways are all not working, move the constant
5096 to a register. We can clobber operands[0] as it is
5097 marked earlyclobber in the insn constraints, but then we have to
5098 swap operands 1 and 2 to match the constraints on the 2-operand
5099 16-bit ior instruction. */
5100 if (reload_completed)
5101 {
5102 emit_move_insn (operands[0], operands[2]);
5103 operands[2] = operands[1];
5104 operands[1] = operands[0];
5105 }
5106 else
5107 operands[2] = copy_to_mode_reg (SImode, operands[2]);
5108 return false;
5109 }
5110
5111
5112 /* Split operands for an XOR expression when OPERANDS[2] is a constant.
5113 Note operands[0] is marked earlyclobber in this case and can be
5114 overwritten. Return true if "DONE", false otherwise. */
5115
5116 bool
5117 csky_split_xor (rtx *operands)
5118 {
5119 HOST_WIDE_INT mask = INTVAL (operands[2]);
5120
5121 /* All zeros can be turned into move instruction. */
5122 if (mask == 0)
5123 {
5124 emit_move_insn (operands[0], operands[1]);
5125 return true;
5126 }
5127
5128 /* All ones can be turned into a bitwise not. */
5129 if (mask == -1)
5130 {
5131 if (CSKY_ISA_FEATURE (E2))
5132 emit_insn (gen_cskyv2_one_cmplsi2 (operands[0], operands[1]));
5133 else
5134 {
5135 emit_move_insn (operands[0], operands[1]);
5136 emit_insn (gen_ck801_one_cmplsi2 (operands[0], operands[0]));
5137 }
5138 return true;
5139 }
5140
5141 /* Check for constants that can be handled directly by the 32-bit xori
5142 instruction. */
5143 if (CSKY_ISA_FEATURE (E2) && csky_arith_O_operand (operands[2], SImode))
5144 return false;
5145
5146 /* If the above ways are all not working, move the constant
5147 to a register. We can clobber operands[0] as it is
5148 marked earlyclobber in the insn constraints, but then we have to
5149 swap operands 1 and 2 to match the constraints on the 2-operand
5150 16-bit ior instruction. */
5151 if (reload_completed)
5152 {
5153 emit_move_insn (operands[0], operands[2]);
5154 operands[2] = operands[1];
5155 operands[1] = operands[0];
5156 }
5157 else
5158 operands[2] = copy_to_mode_reg (SImode, operands[2]);
5159 return false;
5160 }
5161
5162
5163 /* Return true if X is an address form involving a symbol or label ref. */
5164
5165 bool
5166 csky_symbolic_address_p (rtx x)
5167 {
5168 switch (GET_CODE (x))
5169 {
5170 case SYMBOL_REF:
5171 case LABEL_REF:
5172 return 1;
5173 case CONST:
5174 x = XEXP (x, 0);
5175 return ((GET_CODE (XEXP (x, 0)) == SYMBOL_REF
5176 || GET_CODE (XEXP (x, 0)) == LABEL_REF)
5177 && GET_CODE (XEXP (x, 1)) == CONST_INT);
5178 default:
5179 return 0;
5180 }
5181 }
5182
5183
5184 /* Emit a comparison instruction.
5185 Return true if an inverted comparison is generated. */
5186
5187 bool
5188 csky_emit_compare (enum rtx_code code, rtx op0, rtx op1)
5189 {
5190 bool invert;
5191 rtx cc_reg = gen_rtx_REG (CCmode, CSKY_CC_REGNUM);
5192
5193 if (GET_MODE_CLASS(GET_MODE (op0)) == MODE_FLOAT)
5194 return csky_emit_compare_float(code, op0, op1);
5195
5196 if (GET_CODE (op1) == CONST_INT)
5197 {
5198 HOST_WIDE_INT val = INTVAL (op1);
5199
5200 switch (code)
5201 {
5202 case GTU:
5203 /* Unsigned (GTU 0) is the same as (NE 0); everything else is
5204 converted below to LEU (reversed cmphs). */
5205 if (val == 0)
5206 code = NE;
5207 /* Check whether (GTU A imm) can become (GEU A imm + 1). */
5208 else if (TARGET_MINI_REGISTERS
5209 ? CSKY_CONST_OK_FOR_J (val + 1)
5210 : CSKY_CONST_OK_FOR_Uk (val + 1))
5211 {
5212 op1 = GEN_INT (val + 1);
5213 code = GEU;
5214 }
5215 break;
5216 /* Check whether (LE A imm) can become (LT A imm + 1),
5217 or (GT A imm) can become (GE A imm + 1). */
5218 case GT:
5219 case LE:
5220 if (TARGET_MINI_REGISTERS
5221 ? CSKY_CONST_OK_FOR_J (val + 1)
5222 : CSKY_CONST_OK_FOR_Uk (val + 1))
5223 {
5224 op1 = GEN_INT (val + 1);
5225 code = code == LE ? LT : GE;
5226 }
5227 break;
5228
5229 default:
5230 break;
5231 }
5232 }
5233
5234 if (CONSTANT_P (op1) && GET_CODE (op1) != CONST_INT)
5235 op1 = force_reg (GET_MODE (op1), op1);
5236
5237 /* cmpnei: 0-31 (K immediate)
5238 ti: 1-32 (J immediate, 0 using btsti x,31). */
5239 invert = false;
5240 switch (code)
5241 {
5242 /* Use inverted condition, cmpne. */
5243 case EQ:
5244 code = NE;
5245 invert = true;
5246 /* Fall through. */
5247 /* Use normal condition, cmpne. */
5248 case NE:
5249 if (GET_CODE (op1) == CONST_INT
5250 && (TARGET_MINI_REGISTERS
5251 ? !csky_literal_K_operand (op1, SImode)
5252 : !csky_literal_I_operand (op1, SImode)))
5253 op1 = force_reg (SImode, op1);
5254 break;
5255
5256 /* Use inverted condition, reversed cmplt. */
5257 case LE:
5258 code = GT;
5259 invert = true;
5260 /* Fall through. */
5261 /* Use normal condition, reversed cmplt. */
5262 case GT:
5263 if (GET_CODE (op1) == CONST_INT)
5264 op1 = force_reg (SImode, op1);
5265 break;
5266
5267 /* Use inverted condition, cmplt. */
5268 case GE:
5269 code = LT;
5270 invert = true;
5271 /* Fall through. */
5272 /* Use normal condition, cmplt. */
5273 case LT:
5274 /* covered by btsti x,31. */
5275 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0
5276 && (TARGET_MINI_REGISTERS
5277 ? !csky_literal_J_operand (op1, SImode)
5278 : !csky_literal_Uk_operand (op1, SImode)))
5279 op1 = force_reg (SImode, op1);
5280 break;
5281
5282 /* Use inverted condition, cmple. */
5283 case GTU:
5284 /* We coped with unsigned > 0 above. */
5285 gcc_assert (GET_CODE (op1) != CONST_INT || INTVAL (op1) != 0);
5286 code = LEU;
5287 invert = true;
5288 /* Fall through. */
5289 /* Use normal condition, reversed cmphs. */
5290 case LEU:
5291 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
5292 op1 = force_reg (SImode, op1);
5293 break;
5294
5295 /* Use inverted condition, cmphs. */
5296 case LTU:
5297 code = GEU;
5298 invert = true;
5299 /* Fall through. */
5300 /* Use normal condition, cmphs. */
5301 case GEU:
5302 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0
5303 && (TARGET_MINI_REGISTERS
5304 ? !csky_literal_J_operand (op1, SImode)
5305 : !csky_literal_Uk_operand (op1, SImode)))
5306 op1 = force_reg (SImode, op1);
5307 break;
5308
5309 default:
5310 break;
5311 }
5312
5313 emit_insn (gen_rtx_SET (cc_reg,
5314 gen_rtx_fmt_ee (code, CCmode, op0, op1)));
5315 return invert;
5316 }
5317
5318 /* Return true if push/pop can be used to save/restore all the registers
5319 indicated by MASK. We currently don't attempt to handle situations where
5320 some of the registers could be handled by push/pop and others saved and
5321 restored individually. */
5322
5323 static bool
5324 csky_can_use_pushpop (unsigned int mask)
5325 {
5326 int i;
5327 int end_reg;
5328
5329 if (!TARGET_PUSHPOP)
5330 return false;
5331
5332 if (mask == 0)
5333 return false;
5334
5335 /* Regs 0-3, 12-14, 18-27, 29-31 cannot be in the mask. */
5336 if (mask & 0xeffc700f)
5337 return false;
5338
5339 /* Regs in the range r4-r11 must be contiguous. */
5340 for (end_reg = 0, i = 11; i >= 4; i--)
5341 {
5342 if (!end_reg && (mask & (1 << i)))
5343 end_reg = i;
5344 if (end_reg && !(mask & (1 << i)))
5345 return false;
5346 }
5347
5348 /* Likewise for regs in the range r16-r17. */
5349 for (end_reg = 0, i = 17; i >= 16; i--)
5350 {
5351 if (!end_reg && (mask & (1 << i)))
5352 end_reg = i;
5353 if (end_reg && !(mask & (1 << i)))
5354 return false;
5355 }
5356
5357 return true;
5358 }
5359
5360
5361 /* Return true if store/load multiple instructions can be used to
5362 save/restore at least some of the registers indicated by MASK.
5363 Unlike the push/pop case, this does handle partial ranges.
5364 Set *BR and *ER to the beginning and end (respectively) of the
5365 register range that can be handled. */
5366
5367 static bool
5368 csky_can_use_ldstm (int mask, int *br, int *er)
5369 {
5370 int regno;
5371 int begin_reg = 0, end_reg = 0;
5372 int count = 0;
5373
5374 if (!TARGET_MULTIPLE_STLD)
5375 return false;
5376
5377 /* We'll only handle registers in the range 4-11, the contiguous range
5378 of caller-saved registers. Higher-numbered registers are handled
5379 individually in addition to this, but we'll give up on doing ldstm
5380 entirely if we need to save/restore the low-numbered EH registers. */
5381 if (mask & 0xf)
5382 return false;
5383
5384 for (regno = 4; regno <= 11; regno++)
5385 {
5386 if (mask & 1 << regno)
5387 {
5388 if (!begin_reg)
5389 begin_reg = regno;
5390 end_reg = regno;
5391 count++;
5392 }
5393 else if (begin_reg)
5394 break;
5395 }
5396
5397 if (count >= CSKY_MIN_MULTIPLE_STLD && count <= CSKY_MAX_MULTIPLE_STLD)
5398 {
5399 if (br)
5400 *br = begin_reg;
5401 if (er)
5402 *er = end_reg;
5403 return true;
5404 }
5405 return false;
5406 }
5407
5408
5409 const char *
5410 csky_output_return_instruction (void)
5411 {
5412 unsigned long func_type = get_csky_current_func_type ();
5413
5414 if (CSKY_FUNCTION_IS_NAKED (func_type))
5415 return "";
5416 if (CSKY_FUNCTION_IS_INTERRUPT (func_type))
5417 return "ipop\n\tnir\n";
5418 else
5419 return "rts\n";
5420 }
5421
5422
5423 /* Adjust the stack pointer by OFFSET bytes. OFFSET is negative if this
5424 is in the prologue, positive if in the epilogue. This may require
5425 multiple instructions and/or use of CSKY_STACKADJUST_REGNUM as
5426 a scratch register. Emit CFA notes as appropriate. */
5427 static void
5428 expand_csky_stack_adjust (int offset)
5429 {
5430 rtx set;
5431 rtx_insn *insn;
5432 int size = (offset > 0 ? offset : -offset);
5433
5434 if (offset == 0)
5435 return;
5436
5437 /* If OFFSET is too large for addi/subi, load it into
5438 CSKY_STACKADJUST_REGNUM and use a register add/sub instead.
5439 This case is not mentioned in the ABI documentation, but it is
5440 supported by GDB prologue analysis provided that the instruction(s)
5441 to initialize CSKY_STACKADJUST_REGNUM appear directly before
5442 the sub. Depending on the value of OFFSET, this might be a
5443 lrw instruction or the "tricks" used by csky_output_inline_const to
5444 encode special-case integer constants. */
5445 if (size > CSKY_MAX_SP_ADJUST * 2)
5446 {
5447 rtx tmp, dwarf;
5448
5449 /* We should have reserved the scratch register already in
5450 csky_layout_stack_frame. */
5451 gcc_assert (cfun->machine->reg_size != 0
5452 && (cfun->machine->reg_mask
5453 & (1 << CSKY_STACKADJUST_REGNUM)));
5454
5455 /* Prevent the optimizer from reordering these instructions to
5456 keep GDB happy. */
5457 if (!flag_sched_prolog)
5458 emit_insn (gen_blockage ());
5459
5460 tmp = gen_rtx_REG (SImode, CSKY_STACKADJUST_REGNUM);
5461 emit_move_insn (tmp, GEN_INT (size));
5462
5463 if (offset > 0)
5464 set = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp);
5465 else
5466 set = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp);
5467 insn = emit_insn (set);
5468 RTX_FRAME_RELATED_P (insn) = 1;
5469 dwarf = gen_rtx_SET (stack_pointer_rtx,
5470 plus_constant (Pmode, stack_pointer_rtx, offset));
5471 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
5472
5473 /* More make GDB happy. */
5474 if (!flag_sched_prolog)
5475 emit_insn (gen_blockage ());
5476 }
5477
5478 /* Use one or two addi or subi insns to adjust stack. */
5479 else
5480 while (size)
5481 {
5482 int delta = (size > CSKY_MAX_SP_ADJUST
5483 ? CSKY_MAX_SP_ADJUST : size);
5484
5485 if (offset > 0)
5486 set = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
5487 GEN_INT (delta));
5488 else
5489 set = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
5490 GEN_INT (delta));
5491 insn = emit_insn (set);
5492 RTX_FRAME_RELATED_P (insn) = 1;
5493 size -= delta;
5494 }
5495 }
5496
5497
5498 /* Generate and emit an insn that we will recognize as a push_multi.
5499 Unfortunately, since this insn does not reflect very well the actual
5500 semantics of the operation, we need to annotate the insn for the benefit
5501 of DWARF2 frame unwind information. DWARF_REGS_MASK is a subset of
5502 MASK for registers that should be annotated for DWARF2 frame unwind
5503 information. */
5504
5505 static rtx
5506 emit_csky_regs_push (unsigned long mask)
5507 {
5508 int num_regs = 0;
5509 int i, j;
5510 rtx par;
5511 rtx dwarf;
5512 rtx tmp;
5513 int dwarf_par_index;
5514
5515 for (i = 0; i < CSKY_NGPR_REGS; i++)
5516 {
5517 if (mask & (1 << i))
5518 num_regs++;
5519 }
5520
5521 /* The reg range for push is:r4-r11,r15-r17,r28. */
5522 gcc_assert (num_regs && num_regs <= 12);
5523
5524 /* For the body of the insn we are going to generate an UNSPEC in
5525 parallel with several USEs. This allows the insn to be recognized
5526 by the push_multi pattern in the csky.md file.
5527
5528 The body of the insn looks something like this:
5529
5530 (parallel [
5531 (set (mem:BLK (pre_modify:SI (reg:SI sp)
5532 (const_int:SI <num>)))
5533 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSHPOP_MULT))
5534 (use (reg:SI XX))
5535 (use (reg:SI YY))
5536 ...
5537 ])
5538
5539 For the frame note however, we try to be more explicit and actually
5540 show each register being stored into the stack frame, plus a (single)
5541 decrement of the stack pointer. We do it this way in order to be
5542 friendly to the stack unwinding code, which only wants to see a single
5543 stack decrement per instruction. The RTL we generate for the note looks
5544 something like this:
5545
5546 (sequence [
5547 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
5548 (set (mem:SI (reg:SI sp)) (reg:SI r4))
5549 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
5550 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
5551 ...
5552 ])
5553
5554 FIXME:: In an ideal world the PRE_MODIFY would not exist and
5555 instead we'd have a parallel expression detailing all
5556 the stores to the various memory addresses so that debug
5557 information is more up-to-date. Remember however while writing
5558 this to take care of the constraints with the push instruction.
5559
5560 Note also that this has to be taken care of for the VFP registers.
5561
5562 For more see PR43399. */
5563
5564 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
5565 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_regs + 1));
5566 dwarf_par_index = 1;
5567
5568 for (i = 0; i < CSKY_NGPR_REGS; i++)
5569 if (mask & (1 << i))
5570 {
5571 rtx reg = gen_rtx_REG (SImode, i);
5572 rtx addr = plus_constant (Pmode, stack_pointer_rtx, -4 * num_regs);
5573 tmp = gen_frame_mem (BLKmode,
5574 gen_rtx_PRE_MODIFY (Pmode,
5575 stack_pointer_rtx, addr));
5576 XVECEXP (par, 0, 0)
5577 = gen_rtx_SET (tmp,
5578 gen_rtx_UNSPEC (BLKmode,
5579 gen_rtvec (1, reg),
5580 UNSPEC_PUSHPOP_MULT));
5581 tmp = gen_rtx_SET (gen_frame_mem (SImode, stack_pointer_rtx),
5582 reg);
5583 RTX_FRAME_RELATED_P (tmp) = 1;
5584 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
5585
5586 break;
5587 }
5588
5589 for (j = 1, i++; j < num_regs; i++)
5590 if (mask & (1 << i))
5591 {
5592 rtx reg = gen_rtx_REG (SImode, i);
5593 rtx addr = plus_constant (Pmode, stack_pointer_rtx, 4 * j);
5594 tmp = gen_rtx_SET (gen_frame_mem (SImode, addr), reg);
5595 RTX_FRAME_RELATED_P (tmp) = 1;
5596 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
5597 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
5598 j++;
5599 }
5600
5601 par = emit_insn (par);
5602
5603 tmp = gen_rtx_SET (stack_pointer_rtx,
5604 plus_constant (Pmode, stack_pointer_rtx, -4 * num_regs));
5605 RTX_FRAME_RELATED_P (tmp) = 1;
5606 XVECEXP (dwarf, 0, 0) = tmp;
5607
5608 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
5609 RTX_FRAME_RELATED_P (par) = 1;
5610
5611 return par;
5612 }
5613
5614
5615 /* Generate and emit an insn pattern that we will recognize as a pop_multi.
5616 SAVED_REGS_MASK shows which registers need to be restored.
5617
5618 Unfortunately, since this insn does not reflect very well the actual
5619 semantics of the operation, we need to annotate the insn for the benefit
5620 of DWARF2 frame unwind information. */
5621
5622 static void
5623 emit_csky_regs_pop (unsigned long mask)
5624 {
5625 int num_regs = 0;
5626 int i, j;
5627 rtx par;
5628
5629 for (i = 0; i < CSKY_NGPR_REGS; i++)
5630 if (mask & (1 << i))
5631 num_regs++;
5632
5633 /* The reg range for push is:r4-r11,r15-r17,r28. */
5634 gcc_assert (num_regs && num_regs <= 12);
5635
5636 /* The first element is (return),
5637 the second element is
5638 (set (reg:SI 'first reg number')
5639 (unspec:SI [(mem)] UNSPEC_PUSHPOP_MULT),
5640 the rest elements is (use (reg:SI 'rest reg number')),
5641 so the length should be number of register to be poped
5642 plus one. */
5643 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs + 1));
5644
5645 XVECEXP (par, 0, 0) = ret_rtx;
5646
5647 for (i = 0; i < CSKY_NGPR_REGS; i++)
5648 if (mask & (1 << i))
5649 {
5650 rtx reg = gen_rtx_REG (SImode, i);
5651 rtx addr = plus_constant (Pmode, stack_pointer_rtx, 4 * num_regs);
5652 rtx tmp = gen_frame_mem (SImode,
5653 gen_rtx_POST_MODIFY (Pmode,
5654 stack_pointer_rtx, addr));
5655 XVECEXP (par, 0, 1)
5656 = gen_rtx_SET (reg,
5657 gen_rtx_UNSPEC (SImode,
5658 gen_rtvec (1, tmp),
5659 UNSPEC_PUSHPOP_MULT));
5660 break;
5661 }
5662
5663 for (j = 2, i++; j < (num_regs + 1); i++)
5664 if (mask & (1 << i))
5665 {
5666 rtx reg = gen_rtx_REG (SImode, i);
5667 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
5668 j++;
5669 }
5670
5671 par = emit_jump_insn (par);
5672 }
5673
5674
5675 /* Generate the function prologue. */
5676
5677 void
5678 csky_expand_prologue (void)
5679 {
5680 rtx_insn *insn;
5681 unsigned long func_type = get_csky_current_func_type ();
5682 unsigned int reg_mask;
5683 int reg_size;
5684
5685 if (CSKY_FUNCTION_IS_NAKED (func_type))
5686 {
5687 if (flag_stack_usage_info)
5688 current_function_static_stack_size = 0;
5689 return;
5690 }
5691
5692 csky_layout_stack_frame ();
5693 reg_mask = cfun->machine->reg_mask;
5694 reg_size = cfun->machine->reg_size;
5695
5696 /* Adjust stack pointer past argument overflow area. */
5697 if (cfun->machine->arg_size != 0)
5698 {
5699 int offset = cfun->machine->arg_size;
5700 expand_csky_stack_adjust (- offset);
5701
5702 /* If we have a parameter passed partially in regs and partially
5703 in memory, the registers will have been stored to memory already
5704 in function.cc. So we only need to copy varargs from registers
5705 to stack. */
5706 if (cfun->machine->uses_anonymous_args)
5707 {
5708 int rn = CSKY_FIRST_PARM_REGNUM + CSKY_NPARM_REGS - 1;
5709 for (offset -= 4; offset >= 0; offset -= 4, rn--)
5710 {
5711 rtx dst = gen_frame_mem (SImode,
5712 plus_constant (Pmode,
5713 stack_pointer_rtx,
5714 offset));
5715 insn = emit_move_insn (dst, gen_rtx_REG (SImode, rn));
5716 RTX_FRAME_RELATED_P (insn) = 1;
5717 }
5718 }
5719 }
5720
5721 /* Push caller-saved registers to stack. */
5722 if (csky_can_use_pushpop (reg_mask))
5723 emit_csky_regs_push (reg_mask);
5724 else if (reg_size)
5725 {
5726 int sreg = -1, ereg = -1;
5727 bool stm_p = csky_can_use_ldstm (reg_mask, &sreg, &ereg);
5728 int stm_regs = stm_p ? ereg - sreg + 1 : 0;
5729 int stm_size = stm_regs * 4;
5730
5731 /* First adjust the SP to the low end of the register save area. */
5732 expand_csky_stack_adjust (- reg_size);
5733
5734 /* Emit individual register saves. Even if we are going to emit an
5735 stm, we may need to save individual registers above that too. */
5736 if (reg_size > stm_size)
5737 {
5738 int offset = reg_size - 4;
5739 int regno = 31;
5740 for ( ; regno > ereg; regno--)
5741 if (reg_mask & (1 << regno))
5742 {
5743 rtx dst = gen_rtx_MEM (SImode,
5744 plus_constant (Pmode,
5745 stack_pointer_rtx,
5746 offset));
5747 rtx insn = emit_insn (gen_movsi (dst,
5748 gen_rtx_REG (SImode, regno)));
5749 RTX_FRAME_RELATED_P (insn) = 1;
5750 if (offset == stm_size)
5751 break;
5752 offset -= 4;
5753 }
5754 }
5755
5756 /* If possible, emit a stm to do a bulk store of sequential
5757 registers to the stack. Note that it is an error in the ABI
5758 documentation that it doesn't list stm as a valid prologue
5759 instruction. */
5760 if (stm_p)
5761 {
5762 rtx par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (stm_regs));
5763 int regno, slot;
5764 for (regno = sreg, slot = 0; regno <= ereg; regno++, slot++)
5765 {
5766 rtx reg = gen_rtx_REG (SImode, regno);
5767 rtx addr = plus_constant (Pmode, stack_pointer_rtx, slot * 4);
5768 rtx set = gen_rtx_SET (gen_frame_mem (SImode, addr), reg);
5769 RTX_FRAME_RELATED_P (set) = 1;
5770 XVECEXP (par, 0, slot) = set;
5771 }
5772 insn = emit_insn (par);
5773 RTX_FRAME_RELATED_P (insn) = 1;
5774 }
5775 }
5776
5777 /* Initialize hard frame pointer, if necessary. It points at the base
5778 of the register save area. */
5779 if (frame_pointer_needed)
5780 {
5781 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx, stack_pointer_rtx));
5782 RTX_FRAME_RELATED_P (insn) = 1;
5783 }
5784
5785 /* Reserve stack space for locals and outgoing args. */
5786 expand_csky_stack_adjust (- cfun->machine->reg_offset);
5787
5788 /* Put the GOT address in reg_gb for PIC, using R13 as a scratch.
5789 See section 4.7.1 in the ABI documentation,
5790 "Function Prologue for PIC". */
5791 if (flag_pic && (reg_mask & (1 << PIC_OFFSET_TABLE_REGNUM)))
5792 {
5793 rtx l1 = gen_label_rtx ();
5794 rtx grs_label = gen_rtx_LABEL_REF (SImode, l1);
5795 rtx reg_gb = gen_rtx_REG (SImode, PIC_OFFSET_TABLE_REGNUM);
5796 rtx reg_temp = gen_rtx_REG (SImode, 13);
5797
5798 rtx tmp0_unspec = gen_rtx_UNSPEC (Pmode,
5799 gen_rtvec (1, grs_label),
5800 UNSPEC_PIC_SYMBOL_GOTPC_GRS);
5801 rtx tmp1_unspec = gen_rtx_UNSPEC (Pmode,
5802 gen_rtvec (1, grs_label),
5803 UNSPEC_PIC_SYMBOL_GOTPC);
5804
5805 emit_insn (gen_prologue_get_pc (tmp0_unspec));
5806 emit_move_insn (reg_temp, tmp1_unspec);
5807 emit_insn (gen_addsi3 (reg_gb, reg_gb, reg_temp));
5808 }
5809
5810 if (flag_stack_usage_info)
5811 current_function_static_stack_size = cfun->machine->frame_size;
5812
5813 if (!flag_sched_prolog)
5814 emit_insn (gen_blockage ());
5815 }
5816
5817 void
5818 csky_expand_epilogue (void)
5819 {
5820 unsigned long func_type = get_csky_current_func_type ();
5821 unsigned int reg_mask;
5822 int reg_size;
5823 int adjust;
5824 rtx_insn *insn;
5825
5826 if (!flag_sched_prolog)
5827 emit_insn (gen_blockage ());
5828
5829 if (CSKY_FUNCTION_IS_NAKED (func_type))
5830 {
5831 emit_jump_insn (gen_simple_return ());
5832 return;
5833 }
5834
5835 /* Get the frame information. */
5836 csky_layout_stack_frame ();
5837 reg_mask = cfun->machine->reg_mask;
5838 reg_size = cfun->machine->reg_size;
5839 adjust = reg_size + cfun->machine->arg_size;
5840
5841 /* Restore the SP to the base of the register save area. */
5842 if (frame_pointer_needed)
5843 {
5844 insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
5845 RTX_FRAME_RELATED_P (insn) = 1;
5846 }
5847 else
5848 expand_csky_stack_adjust (cfun->machine->reg_offset);
5849
5850 /* Restore the callee-saved registers. */
5851 if (csky_can_use_pushpop (reg_mask)
5852 && cfun->machine->arg_size == 0
5853 && !CSKY_FUNCTION_IS_INTERRUPT (func_type)
5854 && !crtl->calls_eh_return)
5855 {
5856 /* Pop includes an implicit return, so we are done. */
5857 emit_csky_regs_pop (reg_mask);
5858 return;
5859 }
5860 else if (reg_size)
5861 {
5862 int sreg = -1, ereg = -1;
5863 bool ldm_p = csky_can_use_ldstm (reg_mask, &sreg, &ereg);
5864 int ldm_regs = ldm_p ? ereg - sreg + 1 : 0;
5865 int ldm_size = ldm_regs * 4;
5866
5867 /* Emit individual register loads. Even if we are going to emit an
5868 ldm, we may need to load individual registers above that too. */
5869 if (reg_size > ldm_size)
5870 {
5871 int offset = reg_size - 4;
5872 int regno = 31;
5873 for ( ; regno > ereg; regno--)
5874 if (reg_mask & (1 << regno))
5875 {
5876 rtx src = gen_frame_mem (SImode,
5877 plus_constant (Pmode,
5878 stack_pointer_rtx,
5879 offset));
5880 rtx reg = gen_rtx_REG (SImode, regno);
5881 insn = emit_move_insn (reg, src);
5882 RTX_FRAME_RELATED_P (insn) = 1;
5883 add_reg_note (insn, REG_CFA_RESTORE, reg);
5884 if (offset == ldm_size)
5885 break;
5886 offset -= 4;
5887 }
5888 }
5889
5890 /* If possible, emit a ldm to do a bulk load of sequential
5891 registers from the stack. */
5892 if (ldm_p)
5893 {
5894 rtx par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (ldm_regs));
5895 int regno, slot;
5896 for (regno = sreg, slot = 0; regno <= ereg; regno++, slot++)
5897 {
5898 rtx reg = gen_rtx_REG (SImode, regno);
5899 rtx addr = plus_constant (Pmode, stack_pointer_rtx, slot * 4);
5900 rtx set = gen_rtx_SET (reg, gen_frame_mem (SImode, addr));
5901 XVECEXP (par, 0, slot) = set;
5902 }
5903 insn = emit_insn (par);
5904 RTX_FRAME_RELATED_P (insn) = 1;
5905 for (regno = sreg; regno <= ereg; regno++)
5906 {
5907 rtx reg = gen_rtx_REG (SImode, regno);
5908 add_reg_note (insn, REG_CFA_RESTORE, reg);
5909 }
5910 }
5911 }
5912
5913 /* Emit the final stack pointer adjustment to deallocate the saved
5914 registers and incoming argument area. */
5915 expand_csky_stack_adjust (adjust);
5916
5917 /* Extra stack adjustment for exception handler return. */
5918 if (crtl->calls_eh_return)
5919 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
5920 EH_RETURN_STACKADJ_RTX));
5921
5922 /* Now we can return. */
5923 emit_jump_insn (gen_simple_return ());
5924 }
5925
5926
5927 static void
5928 csky_output_function_prologue (FILE *f)
5929 {
5930 unsigned long func_type = get_csky_current_func_type ();
5931
5932 switch ((int) CSKY_FUNCTION_TYPE (func_type))
5933 {
5934 default:
5935 case CSKY_FT_NORMAL:
5936 break;
5937 case CSKY_FT_INTERRUPT:
5938 {
5939 asm_fprintf (f, "\t# Interrupt Service Routine.\n");
5940 asm_fprintf (f, "\tnie\n\tipush\n");
5941 break;
5942 }
5943 case CSKY_FT_FIQ:
5944 asm_fprintf (f, "\t# Fast Interrupt Service Routine.\n");
5945 break;
5946 case CSKY_FT_EXCEPTION:
5947 asm_fprintf (f, "\t# CSKY Exception Handler.\n");
5948 break;
5949 case CSKY_FT_NAKED:
5950 asm_fprintf (f, "\t# Naked Function: prologue and epilogue \
5951 provided by programmer.\n");
5952 return;
5953 }
5954
5955 csky_layout_stack_frame ();
5956
5957 /* Generate .stack_size function-name, size for callgraph;
5958 the default stack size is 0. */
5959 if (TARGET_STACK_SIZE && cfun->machine->frame_size > 0)
5960 {
5961 gcc_assert (current_function_decl != NULL);
5962 const char *func_name =
5963 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
5964 if (func_name[0] == '*')
5965 asm_fprintf (f, "\t.stack_size %s, %d\n",
5966 &func_name[1], cfun->machine->frame_size);
5967 else
5968 asm_fprintf (f, "\t.stack_size %s, %d\n",
5969 func_name, cfun->machine->frame_size);
5970 }
5971 }
5972
5973
5974 static void
5975 csky_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED)
5976 {
5977
5978 }
5979
5980
5981 /* Helper for csky_eh_return splitter: store the call frame exception
5982 handler address in lr. */
5983 void
5984 csky_set_eh_return_address (rtx source, rtx scratch)
5985 {
5986 HOST_WIDE_INT delta = 0;
5987 rtx basereg, addr;
5988 unsigned int reg_mask;
5989
5990 csky_layout_stack_frame ();
5991 reg_mask = cfun->machine->reg_mask;
5992
5993 if (reg_mask & (1 << CSKY_LR_REGNUM))
5994 {
5995 /* Find LR in the stack frame. */
5996 int i = 0;
5997
5998 if (frame_pointer_needed)
5999 {
6000 basereg = hard_frame_pointer_rtx;
6001 delta = 0;
6002 }
6003 else
6004 {
6005 basereg = stack_pointer_rtx;
6006 delta = cfun->machine->reg_offset;
6007 }
6008
6009 /* At this point, (basereg + delta) points at the low end of
6010 the reg save area. Regs are saved sequentially from low
6011 to high from this address. */
6012 for (i = 0; i < CSKY_LR_REGNUM; i++)
6013 if (reg_mask & (1 << i))
6014 delta += 4;
6015
6016 if ((CSKY_TARGET_ARCH (CK801) && delta >= CSKY_LD16_MAX_OFFSET (Pmode))
6017 || delta >= CSKY_LD32_MAX_OFFSET (Pmode))
6018 {
6019 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
6020 emit_insn (gen_addsi3 (scratch, scratch, basereg));
6021 addr = scratch;
6022 }
6023 else
6024 addr = plus_constant (Pmode, basereg, delta);
6025 emit_move_insn (gen_frame_mem (Pmode, addr), source);
6026 }
6027 else
6028 emit_move_insn (gen_rtx_REG (Pmode, CSKY_LR_REGNUM), source);
6029 }
6030
6031 /* Return TRUE if X references a SYMBOL_REF. */
6032
6033 bool
6034 csky_symbol_mentioned_p (rtx x)
6035 {
6036 const char *fmt;
6037 int i;
6038
6039 if (GET_CODE (x) == SYMBOL_REF)
6040 return true;
6041
6042 fmt = GET_RTX_FORMAT (GET_CODE (x));
6043 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6044 {
6045 if (fmt[i] == 'E')
6046 {
6047 int j;
6048
6049 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6050 if (csky_symbol_mentioned_p (XVECEXP (x, i, j)))
6051 return true;
6052 }
6053 else if (fmt[i] == 'e' && csky_symbol_mentioned_p (XEXP (x, i)))
6054 return true;
6055 }
6056 return false;
6057 }
6058
6059
6060 /* Return TRUE if X references a LABEL_REF. */
6061
6062 bool
6063 csky_label_mentioned_p (rtx x)
6064 {
6065 const char *fmt;
6066 int i;
6067
6068 if (GET_CODE (x) == LABEL_REF)
6069 return true;
6070
6071 fmt = GET_RTX_FORMAT (GET_CODE (x));
6072 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6073 {
6074 if (fmt[i] == 'E')
6075 {
6076 int j;
6077
6078 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6079 if (csky_label_mentioned_p (XVECEXP (x, i, j)))
6080 return true;
6081 }
6082 else if (fmt[i] == 'e' && csky_label_mentioned_p (XEXP (x, i)))
6083 return true;
6084 }
6085
6086 return false;
6087 }
6088
6089
6090 static bool
6091 tls_unspec_mentioned_p (rtx x)
6092 {
6093 switch (GET_CODE (x))
6094 {
6095 case CONST:
6096 return tls_unspec_mentioned_p (XEXP (x, 0));
6097
6098 case UNSPEC:
6099 if (XINT (x, 1) == UNSPEC_TLS)
6100 return true;
6101
6102 /* Fall through. */
6103 default:
6104 return false;
6105 }
6106 }
6107
6108
6109 /* Implement LEGITIMATE_PIC_OPERAND_P. */
6110
6111 bool
6112 csky_legitimate_pic_operand_p (rtx x)
6113 {
6114 if (tls_unspec_mentioned_p (x))
6115 return true;
6116 if (csky_symbol_mentioned_p (x) || csky_label_mentioned_p (x))
6117 return false;
6118 return true;
6119 }
6120
6121 rtx
6122 csky_legitimize_pic_address (rtx orig, rtx reg, bool gotrel_p)
6123 {
6124 rtx pic_reg = gen_rtx_REG (SImode, PIC_OFFSET_TABLE_REGNUM);
6125 bool optimize_p = false;
6126
6127 if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
6128 {
6129 rtx pic_ref, address, rtx_tmp;
6130 rtx insn;
6131 rtx pic_reg = gen_rtx_REG (SImode, PIC_OFFSET_TABLE_REGNUM);
6132 int subregs = 0;
6133
6134 if (reg == 0)
6135 {
6136 gcc_assert (can_create_pseudo_p ());
6137 reg = gen_reg_rtx (Pmode);
6138 subregs = 1;
6139 }
6140
6141 if (subregs)
6142 address = gen_reg_rtx (Pmode);
6143 else
6144 address = reg;
6145
6146 if (GET_CODE (orig) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (orig))
6147 {
6148 /* When gotrel_p generate sym@GOT, otherwise generate sym@PLT. */
6149 rtx_tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
6150 (gotrel_p
6151 ? UNSPEC_PIC_SYMBOL_GOT
6152 : UNSPEC_PIC_SYMBOL_PLT));
6153 optimize_p = gotrel_p;
6154 if (flag_pic != 1)
6155 {
6156 emit_move_insn (address, rtx_tmp);
6157 rtx_tmp = gen_rtx_MULT (Pmode, address, GEN_INT (1));
6158 }
6159 pic_ref = gen_const_mem (Pmode,
6160 gen_rtx_PLUS (Pmode, pic_reg, rtx_tmp));
6161 }
6162 else
6163 {
6164 /* bsr symbol */
6165 if (flag_pic == 1 && !gotrel_p)
6166 {
6167 pic_ref = gen_rtx_UNSPEC (Pmode,
6168 gen_rtvec (1, orig),
6169 UNSPEC_PIC_SYMBOL_BSR);
6170 return pic_ref;
6171 }
6172 /* grs rx, symbol */
6173 else if (flag_pic == 1 && (GET_CODE (orig) == SYMBOL_REF)
6174 && SYMBOL_REF_FUNCTION_P (orig))
6175 {
6176 pic_ref = gen_rtx_UNSPEC (Pmode,
6177 gen_rtvec (1, orig),
6178 UNSPEC_PIC_SYMBOL_GRS);
6179 return pic_ref;
6180 }
6181 /* lrw rx, symbol@GOTOFF; add rx, rx, gb */
6182 else
6183 {
6184 rtx_tmp = gen_rtx_UNSPEC (Pmode,
6185 gen_rtvec (1, orig),
6186 UNSPEC_PIC_SYMBOL_GOTOFF);
6187 emit_move_insn (address, rtx_tmp);
6188 pic_ref = gen_rtx_PLUS (Pmode, address, pic_reg);
6189 optimize_p = true;
6190 }
6191 }
6192
6193 insn = emit_move_insn (reg, pic_ref);
6194 /* Put a REG_EQUAL note on this insn,
6195 so that it can be optimized by loop. */
6196 if (optimize_p)
6197 set_unique_reg_note (insn, REG_EQUAL, orig);
6198
6199 return reg;
6200 }
6201 else if (GET_CODE (orig) == CONST)
6202 {
6203 rtx base, offset;
6204
6205 if (GET_CODE (XEXP (orig, 0)) == PLUS
6206 && XEXP (XEXP (orig, 0), 1) == pic_reg)
6207 return orig;
6208
6209 if (reg == 0)
6210 {
6211 gcc_assert (can_create_pseudo_p ());
6212 reg = gen_reg_rtx (Pmode);
6213 }
6214
6215 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
6216
6217 base = csky_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
6218 reg, gotrel_p);
6219 offset = csky_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
6220 base == reg ? 0 : reg, gotrel_p);
6221
6222 if (GET_CODE (offset) == CONST_INT)
6223 return plus_constant (Pmode, base, INTVAL (offset));
6224
6225 return gen_rtx_PLUS (Pmode, base, offset);
6226 }
6227
6228 return orig;
6229 }
6230
6231
6232 /* Functions to output assembly code for a function call. */
6233
6234 char *
6235 csky_output_call (rtx *operands, int index)
6236 {
6237 static char buffer[20];
6238 rtx addr = operands[index];
6239
6240 if (REG_P (addr))
6241 sprintf (buffer, "jsr\t%%%d", index);
6242 else if (flag_pic && (GET_CODE (addr) == UNSPEC))
6243 sprintf (buffer, "bsr\t%%%d", index);
6244 else
6245 sprintf (buffer, "jbsr\t%%%d", index);
6246
6247 return buffer;
6248 }
6249
6250
6251 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE.
6252 Output assembler code for a block containing the constant parts
6253 of a trampoline, leaving space for the variable parts.
6254 Note that STATIC_CHAIN_REGNUM is t1 (aka r12) on ck801 and
6255 t1 (r13) otherwise. */
6256
6257 static void
6258 csky_asm_trampoline_template (FILE *f)
6259 {
6260 if (CSKY_ISA_FEATURE (2E3))
6261 {
6262 fprintf (f, "\tlrw\t%s, [.Lstatic_chain]\n",
6263 reg_names[STATIC_CHAIN_REGNUM]);
6264 fprintf (f, "\tjmpi\t[.Lfunc_address]\n");
6265 /* 2 32-bit insns = 8 bytes. */
6266 }
6267 else if (CSKY_TARGET_ARCH (CK801))
6268 {
6269 /* It's hard to provide general support for trampolines on this
6270 core. We need a register other than the one holding the
6271 static chain (r13) to hold the function pointer for the
6272 indirect jump to it. But ck801 has such a limited register set
6273 there is no other call-clobbered scratch register available -- in
6274 particular, this core does not have r12, which we use for the
6275 ck802 case below. If we use a callee-saved register like r4,
6276 saving the old value on the stack screws up the stack frame
6277 if there are overflow arguments pushed on the stack
6278 by the caller. In theory we could test for that and handle
6279 limited cases with parameters that all fit in r0-r3 with no
6280 stack overflow, but punt for now. */
6281 sorry ("Nested function trampolines not supported on CK801.");
6282 }
6283 else
6284 {
6285 fprintf (f, "\tlrw\t%s, [.Lfunc_address]\n",
6286 reg_names[CSKY_T1_REGNUM]);
6287 fprintf (f, "\tlrw\t%s, [.Lstatic_chain]\n",
6288 reg_names[STATIC_CHAIN_REGNUM]);
6289 fprintf (f, "\tjmp\t%s\n",
6290 reg_names[CSKY_T1_REGNUM]);
6291 /* To align constant pool on a word boundary. */
6292 fprintf (f, "\t.align 2\n");
6293 /* 2 32-bit lrw insns + 16-bit jump + 16-bit pad = 12 bytes. */
6294 }
6295
6296 fprintf (f, ".Lstatic_chain:\n");
6297 fprintf (f, "\t.long 0\n");
6298 fprintf (f, ".Lfunc_address:\n");
6299 fprintf (f, "\t.long 0\n");
6300 /* 2 words of constant pool = 8 bytes. */
6301 }
6302
6303 /* Worker function for TARGET_TRAMPOLINE_INIT. */
6304
6305 static void
6306 csky_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
6307 {
6308 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
6309 rtx mem, a_tramp;
6310 int pool = TRAMPOLINE_SIZE - 8;
6311
6312 emit_block_move (m_tramp, assemble_trampoline_template (),
6313 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
6314
6315 mem = adjust_address (m_tramp, SImode, pool);
6316 emit_move_insn (mem, chain_value);
6317 mem = adjust_address (m_tramp, SImode, pool + 4);
6318 emit_move_insn (mem, fnaddr);
6319
6320 a_tramp = XEXP (m_tramp, 0);
6321 maybe_emit_call_builtin___clear_cache (a_tramp,
6322 plus_constant (Pmode,
6323 a_tramp,
6324 TRAMPOLINE_SIZE));
6325 }
6326
6327
6328 /* Emit a comparison insn for float values.
6329 Return true if the comparison is inverted. */
6330
6331 bool
6332 csky_emit_compare_float (enum rtx_code code, rtx op0, rtx op1)
6333 {
6334 rtx cc_reg = gen_rtx_REG (CCmode, CSKY_CC_REGNUM);
6335 bool invert;
6336 machine_mode mode = GET_MODE (op1);
6337
6338 if (op1 != CONST0_RTX (mode))
6339 op1 = force_reg (mode, op1);
6340
6341 invert = false;
6342
6343 switch (code)
6344 {
6345 case EQ:
6346 code = NE;
6347 invert = true;
6348 break;
6349 case GT:
6350 case LT:
6351 case LE:
6352 if (op1 == CONST0_RTX (mode) && TARGET_SUPPORT_FPV2)
6353 op1 = force_reg (mode, op1);
6354 break;
6355 case ORDERED:
6356 code = UNORDERED;
6357 invert = true;
6358 break;
6359
6360 default:
6361 break;
6362 }
6363
6364 emit_insn (gen_rtx_SET (cc_reg, gen_rtx_fmt_ee (code, CCmode, op0, op1)));
6365
6366 return invert;
6367 }
6368
6369 /* Support for the Q or W memory constraint. Returns true if OP is a MEM
6370 RTX with an address consisting of base + index or base + displacement. */
6371
6372 bool
6373 csky_valid_mem_constraint_operand (rtx op, const char *constraint)
6374 {
6375 struct csky_address addr;
6376
6377 if (GET_CODE (op) != MEM)
6378 return false;
6379
6380 if (!decompose_csky_address (XEXP (op, 0), &addr))
6381 return false;
6382
6383 /* Verify base register. */
6384 if (!is_csky_address_register_rtx_p (addr.base, 0))
6385 return false;
6386
6387 /* Verify index operand. */
6388 if (addr.index && (constraint[0] == 'Q' || constraint[0] == 'W'))
6389 {
6390 if (!is_csky_address_register_rtx_p (addr.index, 0))
6391 return false;
6392
6393 if (addr.scale == 1 || addr.scale == 2 || addr.scale == 4
6394 || addr.scale == 8)
6395 return true;
6396
6397 return false;
6398 }
6399 /* Verify disp operand. */
6400 else if (addr.disp && constraint[0] == 'Q')
6401 {
6402 rtx disp = addr.disp;
6403
6404 if (!CONST_INT_P (disp))
6405 return false;
6406
6407 if (((unsigned) INTVAL (disp) % 4) == 0
6408 && (unsigned) INTVAL (disp) <= (unsigned) 1020)
6409 return true;
6410
6411 return false;
6412 }
6413 else if (constraint[0] == 'Q')
6414 /* Single reg is valid for 'Q'. */
6415 return true;
6416
6417 return false;
6418 }
6419
6420
6421 /* Returns the (interrupt) function type of the current
6422 function, or CSKY_FT_UNKNOWN if the type cannot be determined. */
6423
6424 static unsigned long
6425 csky_isr_value (tree argument)
6426 {
6427 const isr_attribute_entry *ptr;
6428 const char *arg;
6429
6430 /* No argument - default to IRQ. */
6431 if (argument == NULL_TREE)
6432 return CSKY_FT_ISR;
6433
6434 /* Get the value of the argument. */
6435 if (TREE_VALUE (argument) == NULL_TREE
6436 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
6437 return CSKY_FT_UNKNOWN;
6438
6439 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
6440
6441 /* Check it against the list of known arguments. */
6442 for (ptr = isr_attribute_map; ptr->arg != NULL; ptr++)
6443 if (strcmp (arg, ptr->arg) == 0)
6444 return ptr->return_value;
6445
6446 /* An unrecognized interrupt type. */
6447 return CSKY_FT_UNKNOWN;
6448 }
6449
6450 /* Handle an attribute requiring a FUNCTION_DECL;
6451 arguments as in struct attribute_spec.handler. */
6452
6453 static tree
6454 csky_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
6455 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6456 {
6457 if (TREE_CODE (*node) != FUNCTION_DECL)
6458 {
6459 warning (OPT_Wattributes, "%qE attribute only applies to functions",
6460 name);
6461 *no_add_attrs = true;
6462 }
6463
6464 return NULL_TREE;
6465 }
6466
6467 /* Handle an "interrupt" or "isr" attribute;
6468 arguments as in struct attribute_spec.handler. */
6469
6470 static tree
6471 csky_handle_isr_attribute (tree *node, tree name, tree args, int flags,
6472 bool *no_add_attrs)
6473 {
6474
6475 if (!TARGET_ISTACK)
6476 {
6477 warning (OPT_Wattributes, "%qE attribute ignored without %<-mistack%>",
6478 name);
6479 *no_add_attrs = true;
6480 return NULL_TREE;
6481 }
6482
6483 if (DECL_P (*node))
6484 {
6485 if (TREE_CODE (*node) != FUNCTION_DECL)
6486 {
6487 warning (OPT_Wattributes, "%qE attribute only applies to functions",
6488 name);
6489 *no_add_attrs = true;
6490 }
6491 }
6492 else
6493 {
6494 if (FUNC_OR_METHOD_TYPE_P (*node))
6495 {
6496 if (csky_isr_value (args) == CSKY_FT_UNKNOWN)
6497 {
6498 warning (OPT_Wattributes, "%qE attribute ignored", name);
6499 *no_add_attrs = true;
6500 }
6501 }
6502 else if (TREE_CODE (*node) == POINTER_TYPE
6503 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*node))
6504 && csky_isr_value (args) != CSKY_FT_UNKNOWN)
6505 {
6506 *node = build_variant_type_copy (*node);
6507 TREE_TYPE (*node) = build_type_attribute_variant (TREE_TYPE (*node),
6508 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
6509 *no_add_attrs = true;
6510 }
6511 else if (flags & ((int)ATTR_FLAG_DECL_NEXT
6512 | (int)ATTR_FLAG_FUNCTION_NEXT
6513 | (int)ATTR_FLAG_ARRAY_NEXT))
6514 {
6515 *no_add_attrs = true;
6516 return tree_cons (name, args, NULL_TREE);
6517 }
6518 else
6519 warning (OPT_Wattributes, "%qE attribute ignored", name);
6520 }
6521 return NULL_TREE;
6522 }
6523
6524
6525 /* Implement TARGET_REGISTER_MOVE_COST: compute extra cost of moving data
6526 between one register class and another. */
6527
6528 int
6529 csky_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
6530 reg_class_t from, reg_class_t to)
6531 {
6532 #define GR_REG_CLASS_P(CLASS) \
6533 ((CLASS) == GENERAL_REGS || (CLASS) == MINI_REGS || (CLASS) == SP_REGS \
6534 || (CLASS) == LOW_REGS)
6535
6536 #define HILO_REG_CLASS_P(CLASS) \
6537 ((CLASS) == HILO_REGS)
6538
6539 #define V_REG_CLASS_P(CLASS) \
6540 ((CLASS) == V_REGS)
6541
6542 if (V_REG_CLASS_P (from) && V_REG_CLASS_P (to))
6543 return 2;
6544
6545 if ((V_REG_CLASS_P (from) && GR_REG_CLASS_P (to))
6546 || (GR_REG_CLASS_P (from) && V_REG_CLASS_P (to)))
6547 return 6;
6548
6549 if ((HILO_REG_CLASS_P (from) && GR_REG_CLASS_P (to))
6550 || (GR_REG_CLASS_P (from) && HILO_REG_CLASS_P (to)))
6551 return 16;
6552
6553 if (HILO_REG_CLASS_P (from) && HILO_REG_CLASS_P (to))
6554 return 32;
6555
6556 if ((HILO_REG_CLASS_P (from) && V_REG_CLASS_P (to))
6557 || (V_REG_CLASS_P (from) && HILO_REG_CLASS_P (to)))
6558 return 64;
6559
6560 return 2;
6561 }
6562
6563
6564 /* Implement TARGET_MEMORY_MOVE_COST: compute the cost of moving data
6565 between registers and memory. */
6566
6567 int
6568 csky_memory_move_cost (machine_mode mode, reg_class_t rclass,
6569 bool in)
6570 {
6571 return (4 + memory_move_secondary_cost (mode, rclass, in));
6572 }
6573
6574
6575 /* TARGET_RTX_COSTS helper for ck801/ck802. */
6576
6577 static bool
6578 ck802_ck801_rtx_costs (rtx x, int code, int outer_code, int *total,
6579 bool speed)
6580 {
6581 machine_mode mode = GET_MODE (x);
6582 switch (code)
6583 {
6584 /* Accessing memory costs quite a lot for first word; */
6585 case MEM:
6586 *total = COSTS_N_INSNS (1 + CSKY_NUM_REGS (mode));
6587 return false;
6588 case DIV:
6589 case UDIV:
6590 case MOD:
6591 case UMOD:
6592 *total = 100;
6593 return true;
6594
6595 case ROTATE:
6596 case ROTATERT:
6597 case ASHIFT:
6598 case LSHIFTRT:
6599 case ASHIFTRT:
6600 if (speed)
6601 *total = 2;
6602 else
6603 *total = COSTS_N_INSNS (1);
6604 return false;
6605
6606 case MINUS:
6607 case PLUS:
6608 *total = COSTS_N_INSNS (CSKY_NUM_REGS (mode));
6609 return false;
6610
6611 case AND:
6612 {
6613 enum rtx_code subcode = GET_CODE (XEXP (x, 1));
6614
6615 /* If subcode is "not", we'll try to combine it into e.g. "andn"
6616 instruction, so give AND itself zero cost. */
6617 if (subcode == NOT)
6618 {
6619 *total = 0;
6620 return false;
6621 }
6622 }
6623 /* Fall through. */
6624 case XOR:
6625 case IOR:
6626 *total = COSTS_N_INSNS (CSKY_NUM_REGS (mode));
6627 return false;
6628
6629 case MULT:
6630 /* FIXME: is ixw supported on ck801/ck802? */
6631 /* We can use "ix.h/w" insn to replace multiply by 2 or 4.
6632 "ix.h/w" is a 32-bit insn, so let its cost be a little less than
6633 "mult" insn. */
6634 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1)))
6635 {
6636 unsigned HOST_WIDE_INT m
6637 = (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)));
6638 if ((m == 2 || m == 4) && outer_code == PLUS)
6639 {
6640 *total = 2;
6641 return true;
6642 }
6643 else
6644 {
6645 /* Because mult is relatively slower than other operations,
6646 we try to use other insns when optimizing for speed.
6647 When optimizing for size, give it lower cost. */
6648 if (speed)
6649 {
6650 *total = COSTS_N_INSNS (10 * CSKY_NUM_REGS (mode));
6651 return true;
6652 }
6653 int cycle = 0;
6654 while (m)
6655 {
6656 m >>= 2;
6657 cycle++;
6658 }
6659 *total = COSTS_N_INSNS (1) + cycle;
6660 return false;
6661 }
6662 }
6663 if (!speed)
6664 *total = COSTS_N_INSNS (1);
6665 return false;
6666
6667 case NEG:
6668 /* Usually, we use subtract from 0 to substitute for neg, and
6669 it costs 1 extra insn to move 0 to a register. */
6670 *total = COSTS_N_INSNS (2 * CSKY_NUM_REGS (mode));
6671 return false;
6672
6673 case NOT:
6674 *total = COSTS_N_INSNS (CSKY_NUM_REGS (mode));
6675 return false;
6676
6677 case COMPARE:
6678 *total = COSTS_N_INSNS (1);
6679 return false;
6680
6681 case SIGN_EXTEND:
6682 case ZERO_EXTEND:
6683 *total = COSTS_N_INSNS (CSKY_NUM_REGS (mode));
6684 return false;
6685
6686 case SIGN_EXTRACT:
6687 case ZERO_EXTRACT:
6688 if (REG_P (XEXP (x, 0))
6689 && CONST_INT_P (XEXP (x, 1))
6690 && CONST_INT_P (XEXP (x, 2))
6691 && INTVAL (XEXP (x, 1)) == 8
6692 && INTVAL (XEXP (x, 2)) % 8 == 0)
6693 {
6694 *total = COSTS_N_INSNS (1);
6695 return true;
6696 }
6697 *total = COSTS_N_INSNS (CSKY_NUM_REGS (mode));
6698 return false;
6699
6700 case CONST_INT:
6701 {
6702 unsigned HOST_WIDE_INT t = (unsigned HOST_WIDE_INT) (INTVAL (x));
6703
6704 if (outer_code == COMPARE)
6705 {
6706 if (t < 0x10000)
6707 *total = 0;
6708 else
6709 *total = COSTS_N_INSNS (2);
6710 }
6711 else if (outer_code == AND || outer_code == IOR || outer_code == XOR)
6712 {
6713 /* "andi,xori,ori" are 32-bit insns, so let it cost a
6714 little more. */
6715 if (t < 0x1000)
6716 {
6717 /* Try replacing "andi" by "sextb/h", so let it cost more. */
6718 if (outer_code == AND && (t == 0xff || t == 0xffff))
6719 {
6720 *total = 8;
6721 return true;
6722 }
6723 *total = 2;
6724 }
6725 else if (t < 0x10000)
6726 *total = COSTS_N_INSNS (1);
6727 else
6728 *total = COSTS_N_INSNS (2);
6729 }
6730 else if (outer_code == PLUS || outer_code == MINUS)
6731 {
6732 /* "addi/subi rx,ry,imm", if imm<9, it is more often a
6733 16-bit insn. If imm>=9, use "movi" insn; it's probably
6734 less than "addi/subi". */
6735 if (t < 9)
6736 *total = 0;
6737 else if (t < 0x1000)
6738 *total = 2;
6739 else if (t < 0x10000)
6740 *total = COSTS_N_INSNS (1);
6741 else
6742 *total = COSTS_N_INSNS (2);
6743 }
6744 else if (outer_code == ROTATE || outer_code == ROTATERT
6745 || outer_code == LSHIFTRT || outer_code == ASHIFTRT
6746 || outer_code == ASHIFT)
6747 {
6748 if (t < 32)
6749 *total = 0;
6750 else
6751 *total = COSTS_N_INSNS (2);
6752 }
6753 else
6754 {
6755 if (t < 0x10000)
6756 if (outer_code == SET && t < 256)
6757 *total = 0;
6758 else
6759 *total = COSTS_N_INSNS (1);
6760 else
6761 *total = COSTS_N_INSNS (2);
6762 }
6763 }
6764 return true;
6765
6766 case CONST:
6767 case LABEL_REF:
6768 case SYMBOL_REF:
6769 *total = COSTS_N_INSNS (3);
6770 return true;
6771 default:
6772 return false;
6773 }
6774 }
6775
6776
6777 /* TARGET_RTX_COSTS helper for ck803. */
6778
6779 static bool
6780 ck803_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED,
6781 int *total, bool speed ATTRIBUTE_UNUSED)
6782 {
6783 switch (code)
6784 {
6785 case SET:
6786 if (MEM_P (XEXP (x, 1)))
6787 {
6788 struct csky_address op1;
6789 bool address_valid
6790 = decompose_csky_address (XEXP (XEXP (x, 1), 0), &op1);
6791 if (op1.index)
6792 {
6793 *total = COSTS_N_INSNS (3);
6794 return true;
6795 }
6796 else if (address_valid)
6797 {
6798 *total = COSTS_N_INSNS (1);
6799 return true;
6800 }
6801 }
6802 if (REG_P (XEXP (x, 0)) && (GET_CODE (XEXP (x, 1)) == PLUS))
6803 {
6804 rtx sub_exp = XEXP (x, 1);
6805 if (REG_P (XEXP (sub_exp, 0)) && REG_P (XEXP (sub_exp, 1)))
6806 {
6807 *total = COSTS_N_INSNS (1);
6808 return true;
6809 }
6810 }
6811 return false;
6812 case MULT:
6813 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1)))
6814 {
6815 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6816 if (val % 2 == 0 && val < 0xffffffff && val > 0)
6817 {
6818 *total = COSTS_N_INSNS (1);
6819 return true;
6820 }
6821 }
6822 return false;
6823
6824 case CONST:
6825 case LABEL_REF:
6826 case SYMBOL_REF:
6827 *total = COSTS_N_INSNS (3);
6828 return true;
6829 default:
6830 return false;
6831 }
6832 }
6833
6834 /* TARGET_RTX_COSTS helper for ck807/ck810 arches. */
6835
6836 static bool
6837 ck807_ck810_rtx_costs (rtx x, int code,
6838 int outer_code ATTRIBUTE_UNUSED,
6839 int *total, bool speed ATTRIBUTE_UNUSED)
6840 {
6841 switch (code)
6842 {
6843 case MULT:
6844 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1)))
6845 {
6846 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6847 if (val % 2 == 0 && val < 0xffffffff && val > 0)
6848 {
6849 *total = COSTS_N_INSNS (1);
6850 return true;
6851 }
6852 }
6853 return false;
6854
6855 case CONST:
6856 case LABEL_REF:
6857 case SYMBOL_REF:
6858 *total = COSTS_N_INSNS (3);
6859 return true;
6860 default:
6861 return false;
6862 }
6863 }
6864
6865 /* TARGET_RTX_COSTS helper for ck860 arches. */
6866
6867 static bool
6868 ck860_rtx_costs (rtx x, int code, machine_mode mode,
6869 int outer_code ATTRIBUTE_UNUSED,
6870 int *total, bool speed ATTRIBUTE_UNUSED)
6871 {
6872 switch (code)
6873 {
6874 case PLUS:
6875 /* The costs of mula is 1 more than mult. */
6876 if (GET_CODE (XEXP (x, 0)) == MULT && REG_P (XEXP (x, 1)) && speed)
6877 {
6878 rtx mul_op0 = XEXP (XEXP (x, 0), 0);
6879 rtx mul_op1 = XEXP (XEXP (x, 0), 1);
6880 if (REG_P (mul_op0) && REG_P (mul_op1))
6881 {
6882 *total = COSTS_N_INSNS (1);
6883 *total += rtx_cost (XEXP (x, 0), mode,
6884 (enum rtx_code) code, 0, speed);
6885 return true;
6886 }
6887 }
6888 return false;
6889 case MULT:
6890 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1)))
6891 {
6892 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6893 if (val % 2 == 0 && val < 0xffffffff && val > 0)
6894 {
6895 *total = COSTS_N_INSNS (1);
6896 return true;
6897 }
6898 }
6899 return false;
6900
6901 case CONST:
6902 case LABEL_REF:
6903 case SYMBOL_REF:
6904 *total = COSTS_N_INSNS (3);
6905 return true;
6906 default:
6907 return false;
6908 }
6909 }
6910
6911
6912 /* Implement TARGET_RTX_COSTS, to compute a (partial) cost for rtx X.
6913 Return true if the complete cost has been computed, and false if
6914 subexpressions should be scanned. In either case, *TOTAL contains
6915 the cost result. */
6916
6917 static bool
6918 csky_rtx_costs (rtx x, machine_mode mode ATTRIBUTE_UNUSED, int outer_code,
6919 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
6920 {
6921 int code = GET_CODE (x);
6922
6923 if (CSKY_TARGET_ARCH (CK802) || CSKY_TARGET_ARCH (CK801))
6924 return ck802_ck801_rtx_costs (x, code, outer_code, total, speed);
6925 else if (CSKY_TARGET_ARCH (CK803))
6926 return ck803_rtx_costs (x, code, outer_code, total, speed);
6927 else if (CSKY_TARGET_ARCH (CK807) || CSKY_TARGET_ARCH (CK810))
6928 return ck807_ck810_rtx_costs (x, code, outer_code, total, speed);
6929 else if (CSKY_TARGET_ARCH (CK860))
6930 return ck860_rtx_costs (x, code, mode, outer_code, total, speed);
6931 else
6932 gcc_unreachable ();
6933 }
6934
6935 /* Emit assembly code for CASESI. This is only used on CK801 and CK802
6936 when optimizing for size, and uses helper functions in libgcc instead
6937 of doing the control transfer inline. */
6938
6939 const char *
6940 csky_output_casesi (rtx *operands)
6941 {
6942 rtx diff_vec = PATTERN (NEXT_INSN (as_a <rtx_insn *> (operands[0])));
6943
6944 gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
6945
6946 switch (GET_MODE (diff_vec))
6947 {
6948 case E_QImode:
6949 return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned
6950 ? "jbsr\t___gnu_csky_case_uqi"
6951 : "jbsr\t___gnu_csky_case_sqi");
6952 case E_HImode:
6953 return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned
6954 ? "jbsr\t___gnu_csky_case_uhi"
6955 : "jbsr\t___gnu_csky_case_shi");
6956 case E_SImode:
6957 return "jbsr\t___gnu_csky_case_si";
6958 default:
6959 gcc_unreachable ();
6960 }
6961 }
6962
6963 /* Implement TARGET_SCHED_ISSUE_RATE. Lookup the issue rate in the
6964 per-core tuning structs. */
6965 static int
6966 csky_sched_issue_rate (void)
6967 {
6968 if (CSKY_TARGET_ARCH (CK810))
6969 return 2;
6970 else
6971 return 1;
6972 }
6973
6974
6975 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
6976 It corrects the value of COST based on the relationship between
6977 INSN and DEP through the dependence DEP_TYPE. It returns the new
6978 value. */
6979
6980 static int
6981 csky_sched_adjust_cost (rtx_insn *insn,
6982 int dep_type,
6983 rtx_insn *dep,
6984 int cost,
6985 unsigned int dw ATTRIBUTE_UNUSED)
6986 {
6987 if (dep_type == REG_DEP_ANTI || dep_type == REG_DEP_OUTPUT)
6988 return 0;
6989 /* The REG_DEP_TRUE situation. */
6990 else if (recog_memoized (insn) >= 0 && recog_memoized (dep) >= 0)
6991 {
6992 enum attr_type insn_type = get_attr_type (insn);
6993 if (CSKY_TARGET_ARCH (CK803))
6994 {
6995 /* The ld or st's base reg depends on the pre insn,
6996 it will delay 1 cycle. */
6997 if (insn_type == TYPE_LOAD || insn_type == TYPE_STORE)
6998 {
6999 rtx pattern = PATTERN (insn);
7000
7001 gcc_assert (GET_CODE (pattern) == SET);
7002 rtx addr = (insn_type == TYPE_LOAD
7003 ? SET_SRC (pattern) : SET_DEST (pattern));
7004
7005 enum rtx_code code = GET_CODE (addr);
7006 if (code == ZERO_EXTEND || code == SIGN_EXTEND)
7007 addr = XEXP (addr, 0);
7008 gcc_assert (GET_CODE (addr) == MEM);
7009
7010 rtx base = XEXP (addr, 0);
7011 rtx reg = NULL_RTX;
7012 if (REG_P (base))
7013 reg = base;
7014 if (GET_CODE (base) == PLUS
7015 && GET_CODE (XEXP (base, 0)) == REG)
7016 reg = XEXP (base, 0);
7017 if ((reg != NULL_RTX) && reg_set_p (reg, PATTERN (dep)))
7018 return 2;
7019 }
7020 }
7021 else if (CSKY_TARGET_ARCH (CK802))
7022 {
7023 if ((insn_type == TYPE_CALL_JSR || insn_type == TYPE_BRANCH_JMP)
7024 && get_attr_type (dep) != TYPE_LOAD)
7025 return 1;
7026
7027 if (insn_type == TYPE_LOAD || insn_type == TYPE_STORE)
7028 {
7029 rtx pattern = PATTERN (insn);
7030
7031 gcc_assert (GET_CODE (pattern) == SET);
7032
7033 rtx addr = (insn_type == TYPE_LOAD
7034 ? SET_SRC (pattern) : SET_DEST (pattern));
7035
7036 enum rtx_code code = GET_CODE (addr);
7037 if (code == ZERO_EXTEND || code == SIGN_EXTEND)
7038 addr = XEXP (addr, 0);
7039 gcc_assert (GET_CODE (addr) == MEM);
7040
7041 rtx base = XEXP (addr, 0);
7042 rtx reg = NULL_RTX;
7043 if (REG_P (base))
7044 reg = base;
7045 if (GET_CODE (base) == PLUS
7046 && GET_CODE (XEXP (base, 0)) == REG)
7047 reg = XEXP (base, 0);
7048 if ((reg != NULL_RTX) && reg_set_p (reg, PATTERN (dep))
7049 && get_attr_type (dep) != TYPE_LOAD)
7050 return 1;
7051
7052 if (insn_type == TYPE_STORE
7053 && reg_referenced_p (SET_SRC (pattern), PATTERN (dep)))
7054 return 1;
7055 }
7056 }
7057 }
7058 return cost;
7059 }
7060
7061 static bool
7062 csky_warn_func_return (tree decl)
7063 {
7064 /* Naked functions are implemented entirely in assembly, including the
7065 return sequence, so suppress warnings about this. */
7066 return lookup_attribute ("naked", DECL_ATTRIBUTES (decl)) == NULL_TREE;
7067 }
7068
7069
7070 /* Implement TARGET_RETURN_IN_MEMORY to decide whether TYPE should be
7071 returned in memory (true) or in a register (false).
7072 FNTYPE is the type of the function making the call. */
7073
7074 static bool
7075 csky_return_in_memory (const_tree type,
7076 const_tree fntype ATTRIBUTE_UNUSED)
7077 {
7078 const HOST_WIDE_INT size = int_size_in_bytes (type);
7079 return (size == -1 || size > 2 * UNITS_PER_WORD);
7080 }
7081
7082
7083 /* Implement TARGET_DWARF_REGISTER_SPAN.
7084 Dwarf models VFP registers as 64-bit or 128-bit registers default.
7085 GCC models tham as 32-bit registers, so we need to describe this to
7086 the DWARF generation code. Other registers can use the default. */
7087
7088 static rtx
7089 csky_dwarf_register_span (rtx rtl)
7090 {
7091 machine_mode mode;
7092 unsigned regno;
7093 rtx parts[16];
7094 int nregs;
7095 int i;
7096
7097 regno = REGNO (rtl);
7098 if (!CSKY_VREG_P (regno))
7099 return NULL_RTX;
7100
7101 if (CSKY_VREG_HI_P (regno))
7102 regno += 16;
7103
7104 mode = GET_MODE (rtl);
7105 if (GET_MODE_SIZE (mode) < 8)
7106 return NULL_RTX;
7107
7108
7109 if (TARGET_SINGLE_FPU)
7110 {
7111 nregs = GET_MODE_SIZE (mode) / 4;
7112 for (i = 0; i < nregs; i += 2)
7113 if (TARGET_BIG_ENDIAN)
7114 {
7115 parts[i] = gen_rtx_REG (SImode, regno + i + 1);
7116 parts[i + 1] = gen_rtx_REG (SImode, regno + i);
7117 }
7118 else
7119 {
7120 parts[i] = gen_rtx_REG (SImode, regno + i);
7121 parts[i + 1] = gen_rtx_REG (SImode, regno + i + 1);
7122 }
7123 }
7124 else
7125 {
7126 /* FIXME: dwarf2 considers all general registers to be the same
7127 as the CPU bit width. Transform the 64-bit FPU registers to
7128 32 bits here, and we will modify the unwind processing to
7129 fit CSKY architecture later. */
7130 nregs = GET_MODE_SIZE (mode) / 4;
7131 for (i = 0; i < nregs; i += 2)
7132 if (TARGET_BIG_ENDIAN)
7133 {
7134 parts[i] = gen_rtx_REG (SImode, regno + i - 16);
7135 parts[i + 1] = gen_rtx_REG (SImode, regno + i);
7136 }
7137 else
7138 {
7139 parts[i] = gen_rtx_REG (SImode, regno + i);
7140 parts[i + 1] = gen_rtx_REG (SImode, regno + i - 16);
7141 }
7142 }
7143
7144 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nregs , parts));
7145 }
7146
7147 /* Implement TARGET_INIT_LIBFUNCS. */
7148
7149 static void
7150 csky_init_libfuncs (void)
7151 {
7152 if (TARGET_CSKY_LINUX)
7153 init_sync_libfuncs (UNITS_PER_WORD);
7154 if (!TARGET_LIBCCRT)
7155 return;
7156
7157 #define CSKY_GCC_SYM(sym) "__csky_ccrt_" # sym
7158
7159 /* int */
7160
7161 /* Arithmetic functions */
7162 set_optab_libfunc (ashl_optab, DImode, CSKY_GCC_SYM (ashldi3));
7163 set_optab_libfunc (ashr_optab, DImode, CSKY_GCC_SYM (ashrdi3));
7164 set_optab_libfunc (sdiv_optab, SImode, CSKY_GCC_SYM (divsi3));
7165 set_optab_libfunc (sdiv_optab, DImode, CSKY_GCC_SYM (divdi3));
7166 set_optab_libfunc (lshr_optab, DImode, CSKY_GCC_SYM (lshrdi3));
7167 set_optab_libfunc (smod_optab, SImode, CSKY_GCC_SYM (modsi3));
7168 set_optab_libfunc (smod_optab, DImode, CSKY_GCC_SYM (moddi3));
7169 set_optab_libfunc (smul_optab, DImode, CSKY_GCC_SYM (muldi3));
7170 set_optab_libfunc (neg_optab, DImode, CSKY_GCC_SYM (negdi2));
7171 set_optab_libfunc (udiv_optab, SImode, CSKY_GCC_SYM (udivsi3));
7172 set_optab_libfunc (udiv_optab, DImode, CSKY_GCC_SYM (udivdi3));
7173 set_optab_libfunc (udivmod_optab, DImode, CSKY_GCC_SYM (udivmoddi4));
7174 set_optab_libfunc (umod_optab, SImode, CSKY_GCC_SYM (umodsi3));
7175 set_optab_libfunc (umod_optab, DImode, CSKY_GCC_SYM (umoddi3));
7176
7177 /* Comparison functions */
7178 set_optab_libfunc (cmp_optab, DImode, CSKY_GCC_SYM (cmpdi2));
7179 set_optab_libfunc (ucmp_optab, DImode, CSKY_GCC_SYM (ucmpdi2));
7180
7181 /* Trapping arithmetic functions */
7182 set_optab_libfunc (absv_optab, SImode, CSKY_GCC_SYM (absvsi2));
7183 set_optab_libfunc (absv_optab, DImode, CSKY_GCC_SYM (absvdi2));
7184 set_optab_libfunc (addv_optab, SImode, CSKY_GCC_SYM (addvsi3));
7185 set_optab_libfunc (addv_optab, DImode, CSKY_GCC_SYM (addvdi3));
7186 set_optab_libfunc (smulv_optab, SImode, CSKY_GCC_SYM (mulvsi3));
7187 set_optab_libfunc (smulv_optab, DImode, CSKY_GCC_SYM (mulvdi3));
7188 set_optab_libfunc (negv_optab, SImode, CSKY_GCC_SYM (negvsi2));
7189 set_optab_libfunc (negv_optab, DImode, CSKY_GCC_SYM (negvdi2));
7190 set_optab_libfunc (subv_optab, SImode, CSKY_GCC_SYM (subvsi3));
7191 set_optab_libfunc (subv_optab, DImode, CSKY_GCC_SYM (subvdi3));
7192
7193 /* Bit operations */
7194 set_optab_libfunc (clz_optab, SImode, CSKY_GCC_SYM (clzsi2));
7195 set_optab_libfunc (clz_optab, DImode, CSKY_GCC_SYM (clzdi2));
7196 set_optab_libfunc (ctz_optab, SImode, CSKY_GCC_SYM (ctzsi2));
7197 set_optab_libfunc (ctz_optab, DImode, CSKY_GCC_SYM (ctzdi2));
7198 set_optab_libfunc (ffs_optab, DImode, CSKY_GCC_SYM (ffsdi2));
7199 set_optab_libfunc (parity_optab, SImode, CSKY_GCC_SYM (paritysi2));
7200 set_optab_libfunc (parity_optab, DImode, CSKY_GCC_SYM (paritydi2));
7201 set_optab_libfunc (popcount_optab,SImode, CSKY_GCC_SYM (popcountsi2));
7202 set_optab_libfunc (popcount_optab,DImode, CSKY_GCC_SYM (popcountdi2));
7203 set_optab_libfunc (bswap_optab, SImode, CSKY_GCC_SYM (bswapsi2));
7204 set_optab_libfunc (bswap_optab, DImode, CSKY_GCC_SYM (bswapdi2));
7205
7206 /* float */
7207
7208 /* Arithmetic functions */
7209 set_optab_libfunc (add_optab, SFmode, CSKY_GCC_SYM (addsf3));
7210 set_optab_libfunc (add_optab, DFmode, CSKY_GCC_SYM (adddf3));
7211 set_optab_libfunc (sub_optab, SFmode, CSKY_GCC_SYM (subsf3));
7212 set_optab_libfunc (sub_optab, DFmode, CSKY_GCC_SYM (subdf3));
7213 set_optab_libfunc (smul_optab, SFmode, CSKY_GCC_SYM (mulsf3));
7214 set_optab_libfunc (smul_optab, DFmode, CSKY_GCC_SYM (muldf3));
7215 set_optab_libfunc (sdiv_optab, SFmode, CSKY_GCC_SYM (divsf3));
7216 set_optab_libfunc (sdiv_optab, DFmode, CSKY_GCC_SYM (divdf3));
7217 set_optab_libfunc (neg_optab, SFmode, CSKY_GCC_SYM (negsf2));
7218 set_optab_libfunc (neg_optab, DFmode, CSKY_GCC_SYM (negdf2));
7219
7220 /* Conversion functions */
7221 set_conv_libfunc (sext_optab, DFmode, SFmode, CSKY_GCC_SYM (extendsfdf2));
7222 set_conv_libfunc (trunc_optab, SFmode, DFmode, CSKY_GCC_SYM (truncdfsf2));
7223 set_conv_libfunc (sfix_optab, SImode, SFmode, CSKY_GCC_SYM (fixsfsi));
7224 set_conv_libfunc (sfix_optab, SImode, DFmode, CSKY_GCC_SYM (fixdfsi));
7225 set_conv_libfunc (sfix_optab, DImode, SFmode, CSKY_GCC_SYM (fixsfdi));
7226 set_conv_libfunc (sfix_optab, DImode, DFmode, CSKY_GCC_SYM (fixdfdi));
7227 set_conv_libfunc (ufix_optab, SImode, SFmode, CSKY_GCC_SYM (fixunssfsi));
7228 set_conv_libfunc (ufix_optab, SImode, DFmode, CSKY_GCC_SYM (fixunsdfsi));
7229 set_conv_libfunc (ufix_optab, DImode, SFmode, CSKY_GCC_SYM (fixunssfdi));
7230 set_conv_libfunc (ufix_optab, DImode, DFmode, CSKY_GCC_SYM (fixunsdfdi));
7231 set_conv_libfunc (sfloat_optab, SFmode, SImode, CSKY_GCC_SYM (floatsisf));
7232 set_conv_libfunc (sfloat_optab, DFmode, SImode, CSKY_GCC_SYM (floatsidf));
7233 set_conv_libfunc (sfloat_optab, SFmode, DImode, CSKY_GCC_SYM (floatdisf));
7234 set_conv_libfunc (sfloat_optab, DFmode, DImode, CSKY_GCC_SYM (floatdidf));
7235 set_conv_libfunc (ufloat_optab, SFmode, SImode, CSKY_GCC_SYM (floatunsisf));
7236 set_conv_libfunc (ufloat_optab, DFmode, SImode, CSKY_GCC_SYM (floatunsidf));
7237 set_conv_libfunc (ufloat_optab, SFmode, DImode, CSKY_GCC_SYM (floatundisf));
7238 set_conv_libfunc (ufloat_optab, DFmode, DImode, CSKY_GCC_SYM (floatundidf));
7239
7240 /* Comparison functions */
7241 set_optab_libfunc (cmp_optab, SFmode, CSKY_GCC_SYM (cmpsf2));
7242 set_optab_libfunc (cmp_optab, DFmode, CSKY_GCC_SYM (cmpdf2));
7243 set_optab_libfunc (unord_optab, SFmode, CSKY_GCC_SYM (unordsf2));
7244 set_optab_libfunc (unord_optab, DFmode, CSKY_GCC_SYM (unorddf2));
7245 set_optab_libfunc (eq_optab, SFmode, CSKY_GCC_SYM (eqsf2));
7246 set_optab_libfunc (eq_optab, DFmode, CSKY_GCC_SYM (eqdf2));
7247 set_optab_libfunc (ne_optab, SFmode, CSKY_GCC_SYM (nesf2));
7248 set_optab_libfunc (ne_optab, DFmode, CSKY_GCC_SYM (nedf2));
7249 set_optab_libfunc (ge_optab, SFmode, CSKY_GCC_SYM (gesf2));
7250 set_optab_libfunc (ge_optab, DFmode, CSKY_GCC_SYM (gedf2));
7251 set_optab_libfunc (lt_optab, SFmode, CSKY_GCC_SYM (ltsf2));
7252 set_optab_libfunc (lt_optab, DFmode, CSKY_GCC_SYM (ltdf2));
7253 set_optab_libfunc (le_optab, SFmode, CSKY_GCC_SYM (lesf2));
7254 set_optab_libfunc (le_optab, DFmode, CSKY_GCC_SYM (ledf2));
7255 set_optab_libfunc (gt_optab, SFmode, CSKY_GCC_SYM (gtsf2));
7256 set_optab_libfunc (gt_optab, DFmode, CSKY_GCC_SYM (gtdf2));
7257 }
7258
7259
7260 /* Implement TARGET_ADDRESS_COST to estimate cost of the memory address X.
7261 For C-SKY, (register) and (register + offset) have the same cost.
7262 Other situations cost more. */
7263
7264 static int
7265 csky_address_cost (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
7266 addr_space_t as ATTRIBUTE_UNUSED,
7267 bool speed ATTRIBUTE_UNUSED)
7268 {
7269 enum rtx_code code = GET_CODE (x);
7270
7271 if (code == REG)
7272 return COSTS_N_INSNS (1);
7273 if (code == PLUS
7274 && REG_P (XEXP (x, 0))
7275 && CONST_INT_P (XEXP (x, 1)))
7276 return COSTS_N_INSNS (1);
7277
7278 return COSTS_N_INSNS (3);
7279 }
7280
7281
7282 /* Implement TARGET_FIXED_CONDITION_CODE_REGS. */
7283
7284 static bool
7285 csky_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
7286 {
7287 *p1 = CSKY_CC_REGNUM;
7288 *p2 = INVALID_REGNUM;
7289 return true;
7290 }
7291
7292 void
7293 csky_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
7294 rtx libname ATTRIBUTE_UNUSED,
7295 tree fndecl ATTRIBUTE_UNUSED)
7296 {
7297 memset(pcum, 0, sizeof(*pcum));
7298 if (stdarg_p (fntype))
7299 pcum->is_stdarg = true;
7300 }
7301
7302
7303 /* Implement the TARGET_INIT_BUILTINS target macro. */
7304
7305 void
7306 csky_init_builtins (void)
7307 {
7308 /* Init fp16. */
7309 static tree csky_floatHF_type_node = make_node (REAL_TYPE);
7310 TYPE_PRECISION (csky_floatHF_type_node) = GET_MODE_PRECISION (HFmode);
7311 layout_type (csky_floatHF_type_node);
7312 (*lang_hooks.types.register_builtin_type) (csky_floatHF_type_node, "__fp16");
7313 }
7314
7315
7316 /* Implement TARGET_MANGLE_TYPE. */
7317
7318 static const char *
7319 csky_mangle_type (const_tree type)
7320 {
7321 if (SCALAR_FLOAT_TYPE_P (type)
7322 && TYPE_PRECISION (type) == 16
7323 && TYPE_MAIN_VARIANT (type) != float16_type_node)
7324 return "Dh";
7325
7326 /* Use the default mangling. */
7327 return NULL;
7328 }
7329
7330 struct gcc_target targetm = TARGET_INITIALIZER;
7331
7332 #include "gt-csky.h"