]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/cr16/cr16.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / config / cr16 / cr16.c
1 /* Output routines for CR16 processor.
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
3 Contributed by KPIT Cummins Infosystems Limited.
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "df.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "calls.h"
32 #include "tm_p.h"
33 #include "regs.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-codes.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "except.h"
41 #include "recog.h"
42 #include "expmed.h"
43 #include "dojump.h"
44 #include "explow.h"
45 #include "emit-rtl.h"
46 #include "varasm.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "optabs.h"
50 #include "diagnostic-core.h"
51 #include "cfgrtl.h"
52 #include "cfganal.h"
53 #include "lcm.h"
54 #include "cfgbuild.h"
55 #include "cfgcleanup.h"
56 #include "target.h"
57 #include "builtins.h"
58
59 /* This file should be included last. */
60 #include "target-def.h"
61
62 /* Definitions. */
63
64 /* Maximum number of register used for passing parameters. */
65 #define MAX_REG_FOR_PASSING_ARGS 6
66
67 /* Minimum number register used for passing parameters. */
68 #define MIN_REG_FOR_PASSING_ARGS 2
69
70 /* The maximum count of words supported in the assembly of the architecture in
71 a push/pop instruction. */
72 #define MAX_COUNT 8
73
74 /* Predicate is true if the current function is a 'noreturn' function,
75 i.e. it is qualified as volatile. */
76 #define FUNC_IS_NORETURN_P(decl) (TREE_THIS_VOLATILE (decl))
77
78 /* Predicate that holds when we need to save registers even for 'noreturn'
79 functions, to accommodate for unwinding. */
80 #define MUST_SAVE_REGS_P() \
81 (flag_unwind_tables || (flag_exceptions && !UI_SJLJ))
82
83 /* Nonzero if the rtx X is a signed const int of n bits. */
84 #define RTX_SIGNED_INT_FITS_N_BITS(X, n) \
85 ((GET_CODE (X) == CONST_INT \
86 && SIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
87
88 /* Nonzero if the rtx X is an unsigned const int of n bits. */
89 #define RTX_UNSIGNED_INT_FITS_N_BITS(X, n) \
90 ((GET_CODE (X) == CONST_INT \
91 && UNSIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
92
93 /* Structure for stack computations. */
94
95 /* variable definitions in the struture
96 args_size Number of bytes saved on the stack for local
97 variables
98
99 reg_size Number of bytes saved on the stack for
100 non-scratch registers
101
102 total_size The sum of 2 sizes: locals vars and padding byte
103 for saving the registers. Used in expand_prologue()
104 and expand_epilogue()
105
106 last_reg_to_save Will hold the number of the last register the
107 prologue saves, -1 if no register is saved
108
109 save_regs[16] Each object in the array is a register number.
110 Mark 1 for registers that need to be saved
111
112 num_regs Number of registers saved
113
114 initialized Non-zero if frame size already calculated, not
115 used yet
116
117 function_makes_calls Does the function make calls ? not used yet. */
118
119 struct cr16_frame_info
120 {
121 unsigned long var_size;
122 unsigned long args_size;
123 unsigned int reg_size;
124 unsigned long total_size;
125 long last_reg_to_save;
126 long save_regs[FIRST_PSEUDO_REGISTER];
127 int num_regs;
128 int initialized;
129 int function_makes_calls;
130 };
131
132 /* Current frame information calculated by cr16_compute_frame_size. */
133 static struct cr16_frame_info current_frame_info;
134
135 /* Static Variables. */
136
137 /* Data model that was supplied by user via command line option
138 This will be overridden in case of invalid combination
139 of core and data model options are supplied. */
140 static enum data_model_type data_model = DM_DEFAULT;
141
142 /* TARGETM Function Prototypes and forward declarations */
143 static void cr16_print_operand (FILE *, rtx, int);
144 static void cr16_print_operand_address (FILE *, rtx);
145
146 /* Stack layout and calling conventions. */
147 #undef TARGET_STRUCT_VALUE_RTX
148 #define TARGET_STRUCT_VALUE_RTX cr16_struct_value_rtx
149 #undef TARGET_RETURN_IN_MEMORY
150 #define TARGET_RETURN_IN_MEMORY cr16_return_in_memory
151
152 /* Target-specific uses of '__attribute__'. */
153 #undef TARGET_ATTRIBUTE_TABLE
154 #define TARGET_ATTRIBUTE_TABLE cr16_attribute_table
155 #undef TARGET_NARROW_VOLATILE_BITFIELD
156 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
157
158 /* EH related. */
159 #undef TARGET_UNWIND_WORD_MODE
160 #define TARGET_UNWIND_WORD_MODE cr16_unwind_word_mode
161
162 /* Override Options. */
163 #undef TARGET_OPTION_OVERRIDE
164 #define TARGET_OPTION_OVERRIDE cr16_override_options
165
166 /* Conditional register usuage. */
167 #undef TARGET_CONDITIONAL_REGISTER_USAGE
168 #define TARGET_CONDITIONAL_REGISTER_USAGE cr16_conditional_register_usage
169
170 /* Controlling register spills. */
171 #undef TARGET_CLASS_LIKELY_SPILLED_P
172 #define TARGET_CLASS_LIKELY_SPILLED_P cr16_class_likely_spilled_p
173
174 /* Passing function arguments. */
175 #undef TARGET_FUNCTION_ARG
176 #define TARGET_FUNCTION_ARG cr16_function_arg
177 #undef TARGET_FUNCTION_ARG_ADVANCE
178 #define TARGET_FUNCTION_ARG_ADVANCE cr16_function_arg_advance
179 #undef TARGET_RETURN_POPS_ARGS
180 #define TARGET_RETURN_POPS_ARGS cr16_return_pops_args
181
182 /* Initialize the GCC target structure. */
183 #undef TARGET_FRAME_POINTER_REQUIRED
184 #define TARGET_FRAME_POINTER_REQUIRED cr16_frame_pointer_required
185 #undef TARGET_CAN_ELIMINATE
186 #define TARGET_CAN_ELIMINATE cr16_can_eliminate
187 #undef TARGET_LEGITIMIZE_ADDRESS
188 #define TARGET_LEGITIMIZE_ADDRESS cr16_legitimize_address
189 #undef TARGET_LEGITIMATE_CONSTANT_P
190 #define TARGET_LEGITIMATE_CONSTANT_P cr16_legitimate_constant_p
191 #undef TARGET_LEGITIMATE_ADDRESS_P
192 #define TARGET_LEGITIMATE_ADDRESS_P cr16_legitimate_address_p
193
194 /* Returning function value. */
195 #undef TARGET_FUNCTION_VALUE
196 #define TARGET_FUNCTION_VALUE cr16_function_value
197 #undef TARGET_LIBCALL_VALUE
198 #define TARGET_LIBCALL_VALUE cr16_libcall_value
199 #undef TARGET_FUNCTION_VALUE_REGNO_P
200 #define TARGET_FUNCTION_VALUE_REGNO_P cr16_function_value_regno_p
201
202 /* printing the values. */
203 #undef TARGET_PRINT_OPERAND
204 #define TARGET_PRINT_OPERAND cr16_print_operand
205 #undef TARGET_PRINT_OPERAND_ADDRESS
206 #define TARGET_PRINT_OPERAND_ADDRESS cr16_print_operand_address
207
208 /* Relative costs of operations. */
209 #undef TARGET_ADDRESS_COST
210 #define TARGET_ADDRESS_COST cr16_address_cost
211 #undef TARGET_REGISTER_MOVE_COST
212 #define TARGET_REGISTER_MOVE_COST cr16_register_move_cost
213 #undef TARGET_MEMORY_MOVE_COST
214 #define TARGET_MEMORY_MOVE_COST cr16_memory_move_cost
215
216 /* Table of machine attributes. */
217 static const struct attribute_spec cr16_attribute_table[] = {
218 /* ISRs have special prologue and epilogue requirements. */
219 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
220 affects_type_identity }. */
221 {"interrupt", 0, 0, false, true, true, NULL, false},
222 {NULL, 0, 0, false, false, false, NULL, false}
223 };
224
225 /* TARGET_ASM_UNALIGNED_xx_OP generates .?byte directive
226 .?byte directive along with @c is not understood by assembler.
227 Therefore, make all TARGET_ASM_UNALIGNED_xx_OP same
228 as TARGET_ASM_ALIGNED_xx_OP. */
229 #undef TARGET_ASM_UNALIGNED_HI_OP
230 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
231 #undef TARGET_ASM_UNALIGNED_SI_OP
232 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
233 #undef TARGET_ASM_UNALIGNED_DI_OP
234 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
235
236 /* Target hook implementations. */
237
238 /* Implements hook TARGET_RETURN_IN_MEMORY. */
239 static bool
240 cr16_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
241 {
242 const HOST_WIDE_INT size = int_size_in_bytes (type);
243 return ((size == -1) || (size > 8));
244 }
245
246 /* Implement TARGET_CLASS_LIKELY_SPILLED_P. */
247 static bool
248 cr16_class_likely_spilled_p (reg_class_t rclass)
249 {
250 if ((rclass) == SHORT_REGS || (rclass) == DOUBLE_BASE_REGS
251 || (rclass) == LONG_REGS || (rclass) == GENERAL_REGS)
252 return true;
253
254 return false;
255 }
256
257 static int
258 cr16_return_pops_args (tree fundecl ATTRIBUTE_UNUSED,
259 tree funtype ATTRIBUTE_UNUSED,
260 int size ATTRIBUTE_UNUSED)
261 {
262 return 0;
263 }
264
265 /* Returns true if data model selected via command line option
266 is same as function argument. */
267 bool
268 cr16_is_data_model (enum data_model_type model)
269 {
270 return (model == data_model);
271 }
272
273 /* Parse relevant options and override. */
274 static void
275 cr16_override_options (void)
276 {
277 /* Disable -fdelete-null-pointer-checks option for CR16 target.
278 Programs which rely on NULL pointer dereferences _not_ halting the
279 program may not work properly with this option. So disable this
280 option. */
281 flag_delete_null_pointer_checks = 0;
282
283 /* FIXME: To avoid spill_failure ICE during exception handling,
284 * disable cse_fllow_jumps. The spill error occurs when compiler
285 * can't find a suitable candidate in GENERAL_REGS class to reload
286 * a 32bit register.
287 * Need to find a better way of avoiding this situation. */
288 if (flag_exceptions)
289 flag_cse_follow_jumps = 0;
290
291 /* If -fpic option, data_model == DM_FAR. */
292 if (flag_pic == NEAR_PIC)
293 {
294 data_model = DM_FAR;
295 }
296
297 /* The only option we want to examine is data model option. */
298 if (cr16_data_model)
299 {
300 if (strcmp (cr16_data_model, "medium") == 0)
301 data_model = DM_DEFAULT;
302 else if (strcmp (cr16_data_model, "near") == 0)
303 data_model = DM_NEAR;
304 else if (strcmp (cr16_data_model, "far") == 0)
305 {
306 if (TARGET_CR16CP)
307 data_model = DM_FAR;
308 else
309 error ("data-model=far not valid for cr16c architecture");
310 }
311 else
312 error ("invalid data model option -mdata-model=%s", cr16_data_model);
313 }
314 else
315 data_model = DM_DEFAULT;
316 }
317
318 /* Implements the macro TARGET_CONDITIONAL_REGISTER_USAGE. */
319 static void
320 cr16_conditional_register_usage (void)
321 {
322 if (flag_pic)
323 {
324 fixed_regs[12] = call_used_regs[12] = 1;
325 }
326 }
327
328 /* Stack layout and calling conventions routines. */
329
330 /* Return nonzero if the current function being compiled is an interrupt
331 function as specified by the "interrupt" attribute. */
332 int
333 cr16_interrupt_function_p (void)
334 {
335 tree attributes;
336
337 attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
338 return (lookup_attribute ("interrupt", attributes) != NULL_TREE);
339 }
340
341 /* Compute values for the array current_frame_info.save_regs and the variable
342 current_frame_info.reg_size. The index of current_frame_info.save_regs
343 is numbers of register, each will get 1 if we need to save it in the
344 current function, 0 if not. current_frame_info.reg_size is the total sum
345 of the registers being saved. */
346 static void
347 cr16_compute_save_regs (void)
348 {
349 unsigned int regno;
350
351 /* Initialize here so in case the function is no-return it will be -1. */
352 current_frame_info.last_reg_to_save = -1;
353
354 /* Initialize the number of bytes to be saved. */
355 current_frame_info.reg_size = 0;
356
357 /* No need to save any registers if the function never returns. */
358 if (FUNC_IS_NORETURN_P (current_function_decl) && !MUST_SAVE_REGS_P ())
359 return;
360
361 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
362 {
363 if (fixed_regs[regno])
364 {
365 current_frame_info.save_regs[regno] = 0;
366 continue;
367 }
368
369 /* If this reg is used and not call-used (except RA), save it. */
370 if (cr16_interrupt_function_p ())
371 {
372 if (!crtl->is_leaf && call_used_regs[regno])
373 /* This is a volatile reg in a non-leaf interrupt routine - save
374 it for the sake of its sons. */
375 current_frame_info.save_regs[regno] = 1;
376 else if (df_regs_ever_live_p (regno))
377 /* This reg is used - save it. */
378 current_frame_info.save_regs[regno] = 1;
379 else
380 /* This reg is not used, and is not a volatile - don't save. */
381 current_frame_info.save_regs[regno] = 0;
382 }
383 else
384 {
385 /* If this reg is used and not call-used (except RA), save it. */
386 if (df_regs_ever_live_p (regno)
387 && (!call_used_regs[regno] || regno == RETURN_ADDRESS_REGNUM))
388 current_frame_info.save_regs[regno] = 1;
389 else
390 current_frame_info.save_regs[regno] = 0;
391 }
392 }
393
394 /* Save registers so the exception handler can modify them. */
395 if (crtl->calls_eh_return)
396 {
397 unsigned int i;
398
399 for (i = 0;; ++i)
400 {
401 regno = EH_RETURN_DATA_REGNO (i);
402 if (INVALID_REGNUM == regno)
403 break;
404 current_frame_info.save_regs[regno] = 1;
405 }
406 }
407
408 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
409 if (current_frame_info.save_regs[regno] == 1)
410 {
411 current_frame_info.last_reg_to_save = regno;
412 if (regno >= CR16_FIRST_DWORD_REGISTER)
413 current_frame_info.reg_size += CR16_UNITS_PER_DWORD;
414 else
415 current_frame_info.reg_size += UNITS_PER_WORD;
416 }
417 }
418
419 /* Compute the size of the local area and the size to be adjusted by the
420 prologue and epilogue. */
421 static void
422 cr16_compute_frame (void)
423 {
424 /* For aligning the local variables. */
425 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
426 int padding_locals;
427
428 /* Padding needed for each element of the frame. */
429 current_frame_info.var_size = get_frame_size ();
430
431 /* Align to the stack alignment. */
432 padding_locals = current_frame_info.var_size % stack_alignment;
433 if (padding_locals)
434 padding_locals = stack_alignment - padding_locals;
435
436 current_frame_info.var_size += padding_locals;
437 current_frame_info.total_size = current_frame_info.var_size
438 + (ACCUMULATE_OUTGOING_ARGS
439 ? crtl->outgoing_args_size : 0);
440 }
441
442 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
443 int
444 cr16_initial_elimination_offset (int from, int to)
445 {
446 /* Compute this since we need to use current_frame_info.reg_size. */
447 cr16_compute_save_regs ();
448
449 /* Compute this since we need to use current_frame_info.var_size. */
450 cr16_compute_frame ();
451
452 if (((from) == FRAME_POINTER_REGNUM) && ((to) == STACK_POINTER_REGNUM))
453 return (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0);
454 else if (((from) == ARG_POINTER_REGNUM) && ((to) == FRAME_POINTER_REGNUM))
455 return (current_frame_info.reg_size + current_frame_info.var_size);
456 else if (((from) == ARG_POINTER_REGNUM) && ((to) == STACK_POINTER_REGNUM))
457 return (current_frame_info.reg_size + current_frame_info.var_size
458 + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0));
459 else
460 gcc_unreachable ();
461 }
462
463 /* Register Usage. */
464
465 /* Return the class number of the smallest class containing reg number REGNO.
466 This could be a conditional expression or could index an array. */
467 enum reg_class
468 cr16_regno_reg_class (int regno)
469 {
470 if ((regno >= 0) && (regno < CR16_FIRST_DWORD_REGISTER))
471 return SHORT_REGS;
472
473 if ((regno >= CR16_FIRST_DWORD_REGISTER) && (regno < FIRST_PSEUDO_REGISTER))
474 return LONG_REGS;
475
476 return NO_REGS;
477 }
478
479 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
480 int
481 cr16_hard_regno_mode_ok (int regno, machine_mode mode)
482 {
483 if ((GET_MODE_SIZE (mode) >= 4) && (regno == 11))
484 return 0;
485
486 if (mode == DImode || mode == DFmode)
487 {
488 if ((regno > 8) || (regno & 1))
489 return 0;
490 return 1;
491 }
492
493 if ((TARGET_INT32)
494 && ((regno >= 12) && (GET_MODE_SIZE (mode) < 4 )))
495 return 0;
496
497 /* CC can only hold CCmode values. */
498 if (GET_MODE_CLASS (mode) == MODE_CC)
499 return 0;
500 return 1;
501 }
502
503 /* Returns register number for function return value.*/
504 static inline unsigned int
505 cr16_ret_register (void)
506 {
507 return 0;
508 }
509
510 /* Implements hook TARGET_STRUCT_VALUE_RTX. */
511 static rtx
512 cr16_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
513 int incoming ATTRIBUTE_UNUSED)
514 {
515 return gen_rtx_REG (Pmode, cr16_ret_register ());
516 }
517
518 /* Returning function value. */
519
520 /* Worker function for TARGET_FUNCTION_VALUE_REGNO_P. */
521 static bool
522 cr16_function_value_regno_p (const unsigned int regno)
523 {
524 return (regno == cr16_ret_register ());
525 }
526
527 /* Create an RTX representing the place where a
528 library function returns a value of mode MODE. */
529 static rtx
530 cr16_libcall_value (machine_mode mode,
531 const_rtx func ATTRIBUTE_UNUSED)
532 {
533 return gen_rtx_REG (mode, cr16_ret_register ());
534 }
535
536 /* Create an RTX representing the place where a
537 function returns a value of data type VALTYPE. */
538 static rtx
539 cr16_function_value (const_tree type,
540 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
541 bool outgoing ATTRIBUTE_UNUSED)
542 {
543 return gen_rtx_REG (TYPE_MODE (type), cr16_ret_register ());
544 }
545
546 /* Passing function arguments. */
547
548 /* If enough param regs are available for passing the param of type TYPE return
549 the number of registers needed else 0. */
550 static int
551 enough_regs_for_param (CUMULATIVE_ARGS * cum, const_tree type,
552 machine_mode mode)
553 {
554 int type_size;
555 int remaining_size;
556
557 if (mode != BLKmode)
558 type_size = GET_MODE_BITSIZE (mode);
559 else
560 type_size = int_size_in_bytes (type) * BITS_PER_UNIT;
561
562 remaining_size = BITS_PER_WORD * (MAX_REG_FOR_PASSING_ARGS
563 - (MIN_REG_FOR_PASSING_ARGS + cum->ints) +
564 1);
565
566 /* Any variable which is too big to pass in two registers, will pass on
567 stack. */
568 if ((remaining_size >= type_size) && (type_size <= 2 * BITS_PER_WORD))
569 return (type_size + BITS_PER_WORD - 1) / BITS_PER_WORD;
570
571 return 0;
572 }
573
574 /* Implements the macro FUNCTION_ARG defined in cr16.h. */
575 static rtx
576 cr16_function_arg (cumulative_args_t cum_v, machine_mode mode,
577 const_tree type, bool named ATTRIBUTE_UNUSED)
578 {
579 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
580 cum->last_parm_in_reg = 0;
581
582 /* function_arg () is called with this type just after all the args have
583 had their registers assigned. The rtx that function_arg returns from
584 this type is supposed to pass to 'gen_call' but currently it is not
585 implemented (see macro GEN_CALL). */
586 if (type == void_type_node)
587 return NULL_RTX;
588
589 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
590 return NULL_RTX;
591
592 if (mode == BLKmode)
593 {
594 /* Enable structures that need padding bytes at the end to pass to a
595 function in registers. */
596 if (enough_regs_for_param (cum, type, mode) != 0)
597 {
598 cum->last_parm_in_reg = 1;
599 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
600 }
601 }
602
603 if ((MIN_REG_FOR_PASSING_ARGS + cum->ints) > MAX_REG_FOR_PASSING_ARGS)
604 return NULL_RTX;
605 else
606 {
607 if (enough_regs_for_param (cum, type, mode) != 0)
608 {
609 cum->last_parm_in_reg = 1;
610 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
611 }
612 }
613
614 return NULL_RTX;
615 }
616
617 /* Implements the macro INIT_CUMULATIVE_ARGS defined in cr16.h. */
618 void
619 cr16_init_cumulative_args (CUMULATIVE_ARGS * cum, tree fntype,
620 rtx libfunc ATTRIBUTE_UNUSED)
621 {
622 tree param, next_param;
623
624 cum->ints = 0;
625
626 /* Determine if this function has variable arguments. This is indicated by
627 the last argument being 'void_type_mode' if there are no variable
628 arguments. Change here for a different vararg. */
629 for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
630 param != NULL_TREE; param = next_param)
631 {
632 next_param = TREE_CHAIN (param);
633 if ((next_param == NULL_TREE) && (TREE_VALUE (param) != void_type_node))
634 {
635 cum->ints = -1;
636 return;
637 }
638 }
639 }
640
641 /* Implements the macro FUNCTION_ARG_ADVANCE defined in cr16.h. */
642 static void
643 cr16_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
644 const_tree type, bool named ATTRIBUTE_UNUSED)
645 {
646 CUMULATIVE_ARGS * cum = get_cumulative_args (cum_v);
647
648 /* l holds the number of registers required. */
649 int l = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
650
651 /* If the parameter isn't passed on a register don't advance cum. */
652 if (!cum->last_parm_in_reg)
653 return;
654
655 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
656 return;
657
658 if ((mode == SImode) || (mode == HImode)
659 || (mode == QImode) || (mode == DImode))
660 {
661 if (l <= 1)
662 cum->ints += 1;
663 else
664 cum->ints += l;
665 }
666 else if ((mode == SFmode) || (mode == DFmode))
667 cum->ints += l;
668 else if ((mode) == BLKmode)
669 {
670 if ((l = enough_regs_for_param (cum, type, mode)) != 0)
671 cum->ints += l;
672 }
673 return;
674 }
675
676 /* Implements the macro FUNCTION_ARG_REGNO_P defined in cr16.h.
677 Return nonzero if N is a register used for passing parameters. */
678 int
679 cr16_function_arg_regno_p (int n)
680 {
681 return ((n <= MAX_REG_FOR_PASSING_ARGS) && (n >= MIN_REG_FOR_PASSING_ARGS));
682 }
683
684 /* Addressing modes.
685 Following set of function implement the macro GO_IF_LEGITIMATE_ADDRESS
686 defined in cr16.h. */
687
688 /* Helper function to check if is a valid base register that can
689 hold address. */
690 static int
691 cr16_addr_reg_p (rtx addr_reg)
692 {
693 rtx reg;
694
695 if (REG_P (addr_reg))
696 reg = addr_reg;
697 else if ((GET_CODE (addr_reg) == SUBREG)
698 && REG_P (SUBREG_REG (addr_reg))
699 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg)))
700 <= UNITS_PER_WORD))
701 reg = SUBREG_REG (addr_reg);
702 else
703 return FALSE;
704
705 if (GET_MODE (reg) != Pmode)
706 return FALSE;
707
708 return TRUE;
709 }
710
711 /* Helper functions: Created specifically for decomposing operand of CONST
712 Recursively look into expression x for code or data symbol.
713 The function expects the expression to contain combination of
714 SYMBOL_REF, CONST_INT, (PLUS or MINUS)
715 LABEL_REF, CONST_INT, (PLUS or MINUS)
716 SYMBOL_REF
717 LABEL_REF
718 All other combinations will result in code = -1 and data = ILLEGAL_DM
719 code data
720 -1 ILLEGAL_DM The expression did not contain SYMBOL_REF or LABEL_REF
721 0 DM_FAR SYMBOL_REF was found and it was far data reference.
722 0 DM_DEFAULT SYMBOL_REF was found and it was medium data reference.
723 1 ILLEGAL_DM LABEL_REF was found.
724 2 ILLEGAL_DM SYMBOL_REF was found and it was function reference. */
725 void
726 cr16_decompose_const (rtx x, int *code, enum data_model_type *data,
727 bool treat_as_const)
728 {
729 *code = -1;
730 *data = ILLEGAL_DM;
731 switch (GET_CODE (x))
732 {
733 case SYMBOL_REF:
734 *code = SYMBOL_REF_FUNCTION_P (x) ? 2 : 0;
735 /* 2 indicates func sym. */
736 if (*code == 0)
737 {
738 if (CR16_TARGET_DATA_NEAR)
739 *data = DM_DEFAULT;
740 else if (CR16_TARGET_DATA_MEDIUM)
741 *data = DM_FAR;
742 else if (CR16_TARGET_DATA_FAR)
743 {
744 if (treat_as_const)
745 /* This will be used only for printing
746 the qualifier. This call is (may be)
747 made by cr16_print_operand_address. */
748 *data = DM_FAR;
749 else
750 /* This call is (may be) made by
751 cr16_legitimate_address_p. */
752 *data = ILLEGAL_DM;
753 }
754 }
755 return;
756
757 case LABEL_REF:
758 /* 1 - indicates non-function symbol. */
759 *code = 1;
760 return;
761
762 case PLUS:
763 case MINUS:
764 /* Look into the tree nodes. */
765 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
766 cr16_decompose_const (XEXP (x, 1), code, data, treat_as_const);
767 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
768 cr16_decompose_const (XEXP (x, 0), code, data, treat_as_const);
769 return;
770 default:
771 return;
772 }
773 }
774
775 /* Decompose Address
776 This function decomposes the address returns the type of address
777 as defined in enum cr16_addrtype. It also fills the parameter *out.
778 The decomposed address can be used for two purposes. One to
779 check if the address is valid and second to print the address
780 operand.
781
782 Following tables list valid address supported in CR16C/C+ architectures.
783 Legend:
784 aN : Absoulte address N-bit address
785 R : One 16-bit register
786 RP : Consecutive two 16-bit registers or one 32-bit register
787 I : One 32-bit register
788 dispN : Signed displacement of N-bits
789
790 ----Code addresses----
791 Branch operands:
792 disp9 : CR16_ABSOLUTE (disp)
793 disp17 : CR16_ABSOLUTE (disp)
794 disp25 : CR16_ABSOLUTE (disp)
795 RP + disp25 : CR16_REGP_REL (base, disp)
796
797 Jump operands:
798 RP : CR16_REGP_REL (base, disp=0)
799 a24 : CR16_ABSOLUTE (disp)
800
801 ----Data addresses----
802 a20 : CR16_ABSOLUTE (disp) near (1M)
803 a24 : CR16_ABSOLUTE (disp) medium (16M)
804 R + d20 : CR16_REG_REL (base, disp) near (1M+64K)
805 RP + d4 : CR16_REGP_REL (base, disp) far (4G)
806 RP + d16 : CR16_REGP_REL (base, disp) far (4G)
807 RP + d20 : CR16_REGP_REL (base, disp) far (4G)
808 I : *** Valid but port does not support this
809 I + a20 : *** Valid but port does not support this
810 I + RP + d14: CR16_INDEX_REGP_REL (base, index, disp) far (4G)
811 I + RP + d20: CR16_INDEX_REGP_REL (base, index, disp) far (4G)
812
813 Decomposing Data model in case of absolute address.
814
815 Target Option Address type Resultant Data ref type
816 ---------------------- ------------ -----------------------
817 CR16_TARGET_MODEL_NEAR ABS20 DM_DEFAULT
818 CR16_TARGET_MODEL_NEAR IMM20 DM_DEFAULT
819 CR16_TARGET_MODEL_NEAR ABS24 Invalid
820 CR16_TARGET_MODEL_NEAR IMM32 Invalid
821
822 CR16_TARGET_MODEL_MEDIUM ABS20 DM_DEFAULT
823 CR16_TARGET_MODEL_MEDIUM IMM20 DM_DEFAULT
824 CR16_TARGET_MODEL_MEDIUM ABS24 DM_FAR
825 CR16_TARGET_MODEL_MEDIUM IMM32 Invalid
826
827 CR16_TARGET_MODEL_FAR ABS20 DM_DEFAULT
828 CR16_TARGET_MODEL_FAR IMM20 DM_DEFAULT
829 CR16_TARGET_MODEL_FAR ABS24 DM_FAR
830 CR16_TARGET_MODEL_FAR IMM32 DM_FAR. */
831 enum cr16_addrtype
832 cr16_decompose_address (rtx addr, struct cr16_address *out,
833 bool debug_print, bool treat_as_const)
834 {
835 rtx base = NULL_RTX, disp = NULL_RTX, index = NULL_RTX;
836 enum data_model_type data = ILLEGAL_DM;
837 int code = -1;
838 enum cr16_addrtype retval = CR16_INVALID;
839
840 switch (GET_CODE (addr))
841 {
842 case CONST_INT:
843 /* Absolute address (known at compile time). */
844 code = 0;
845 if (debug_print)
846 fprintf (stderr, "\ncode:%d", code);
847 disp = addr;
848
849 if (debug_print)
850 {
851 fprintf (stderr, "\ndisp:");
852 debug_rtx (disp);
853 }
854
855 if (UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 20))
856 {
857 data = DM_DEFAULT;
858 if (debug_print)
859 fprintf (stderr, "\ndata:%d", data);
860 retval = CR16_ABSOLUTE;
861 }
862 else if (UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 24))
863 {
864 if (!CR16_TARGET_DATA_NEAR)
865 {
866 data = DM_FAR;
867 if (debug_print)
868 fprintf (stderr, "\ndata:%d", data);
869 retval = CR16_ABSOLUTE;
870 }
871 else
872 return CR16_INVALID; /* ABS24 is not support in NEAR model. */
873 }
874 else
875 return CR16_INVALID;
876 break;
877
878 case CONST:
879 /* A CONST is an expression of PLUS or MINUS with
880 CONST_INT, SYMBOL_REF or LABEL_REF. This is the
881 result of assembly-time arithmetic computation. */
882 retval = CR16_ABSOLUTE;
883 disp = addr;
884 /* Call the helper function to check the validity. */
885 cr16_decompose_const (XEXP (addr, 0), &code, &data, treat_as_const);
886 if ((code == 0) && (data == ILLEGAL_DM))
887 /* CONST is not valid code or data address. */
888 return CR16_INVALID;
889 if (debug_print)
890 {
891 fprintf (stderr, "\ndisp:");
892 debug_rtx (disp);
893 fprintf (stderr, "\ncode:%d", code);
894 fprintf (stderr, "\ndata:%d", data);
895 }
896 break;
897
898 case LABEL_REF:
899 retval = CR16_ABSOLUTE;
900 disp = addr;
901 /* 1 - indicates non-function symbol. */
902 code = 1;
903 if (debug_print)
904 {
905 fprintf (stderr, "\ndisp:");
906 debug_rtx (disp);
907 fprintf (stderr, "\ncode:%d", code);
908 }
909 break;
910
911 case SYMBOL_REF:
912 /* Absolute address (known at link time). */
913 retval = CR16_ABSOLUTE;
914 disp = addr;
915 /* This is a code address if symbol_ref is a function. */
916 /* 2 indicates func sym. */
917 code = SYMBOL_REF_FUNCTION_P (addr) ? 2 : 0;
918 if (debug_print)
919 {
920 fprintf (stderr, "\ndisp:");
921 debug_rtx (disp);
922 fprintf (stderr, "\ncode:%d", code);
923 }
924 /* If not function ref then check if valid data ref. */
925 if (code == 0)
926 {
927 if (CR16_TARGET_DATA_NEAR)
928 data = DM_DEFAULT;
929 else if (CR16_TARGET_DATA_MEDIUM)
930 data = DM_FAR;
931 else if (CR16_TARGET_DATA_FAR)
932 {
933 if (treat_as_const)
934 /* This will be used only for printing the
935 qualifier. This call is (may be) made
936 by cr16_print_operand_address. */
937 data = DM_FAR;
938 else
939 /* This call is (may be) made by
940 cr16_legitimate_address_p. */
941 return CR16_INVALID;
942 }
943 else
944 data = DM_DEFAULT;
945 }
946 if (debug_print)
947 fprintf (stderr, "\ndata:%d", data);
948 break;
949
950 case REG:
951 case SUBREG:
952 /* Register relative address. */
953 /* Assume REG fits in a single register. */
954 retval = CR16_REG_REL;
955 if (GET_MODE_BITSIZE (GET_MODE (addr)) > BITS_PER_WORD)
956 if (!LONG_REG_P (REGNO (addr)))
957 /* REG will result in reg pair. */
958 retval = CR16_REGP_REL;
959 base = addr;
960 if (debug_print)
961 {
962 fprintf (stderr, "\nbase:");
963 debug_rtx (base);
964 }
965 break;
966
967 case PLUS:
968 switch (GET_CODE (XEXP (addr, 0)))
969 {
970 case REG:
971 case SUBREG:
972 /* REG + DISP20. */
973 /* All Reg relative addresses having a displacement needs
974 to fit in 20-bits. */
975 disp = XEXP (addr, 1);
976 if (debug_print)
977 {
978 fprintf (stderr, "\ndisp:");
979 debug_rtx (disp);
980 }
981 switch (GET_CODE (XEXP (addr, 1)))
982 {
983 case CONST_INT:
984 /* Shall fit in 20-bits. */
985 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 20))
986 return CR16_INVALID;
987 code = 0;
988 if (debug_print)
989 fprintf (stderr, "\ncode:%d", code);
990 break;
991
992 case UNSPEC:
993 switch (XINT (XEXP (addr, 1), 1))
994 {
995 case UNSPEC_LIBRARY_OFFSET:
996 default:
997 gcc_unreachable ();
998 }
999 break;
1000
1001 case LABEL_REF:
1002 case SYMBOL_REF:
1003 case CONST:
1004 /* This is also a valid expression for address.
1005 However, we cannot ascertain if the resultant
1006 displacement will be valid 20-bit value. Therefore,
1007 lets not allow such an expression for now. This will
1008 be updated when we find a way to validate this
1009 expression as legitimate address.
1010 Till then fall through CR16_INVALID. */
1011 default:
1012 return CR16_INVALID;
1013 }
1014
1015 /* Now check if REG can fit into single or pair regs. */
1016 retval = CR16_REG_REL;
1017 base = XEXP (addr, 0);
1018 if (debug_print)
1019 {
1020 fprintf (stderr, "\nbase:");
1021 debug_rtx (base);
1022 }
1023 if (GET_MODE_BITSIZE (GET_MODE ((XEXP (addr, 0)))) > BITS_PER_WORD)
1024 {
1025 if (!LONG_REG_P (REGNO ((XEXP (addr, 0)))))
1026 /* REG will result in reg pair. */
1027 retval = CR16_REGP_REL;
1028 }
1029 break;
1030
1031 case PLUS:
1032 /* Valid expr:
1033 plus
1034 /\
1035 / \
1036 plus idx
1037 /\
1038 / \
1039 reg const_int
1040
1041 Check if the operand 1 is valid index register. */
1042 data = ILLEGAL_DM;
1043 if (debug_print)
1044 fprintf (stderr, "\ndata:%d", data);
1045 switch (GET_CODE (XEXP (addr, 1)))
1046 {
1047 case REG:
1048 case SUBREG:
1049 if (!REG_OK_FOR_INDEX_P (XEXP (addr, 1)))
1050 return CR16_INVALID;
1051 /* OK. REG is a valid index register. */
1052 index = XEXP (addr, 1);
1053 if (debug_print)
1054 {
1055 fprintf (stderr, "\nindex:");
1056 debug_rtx (index);
1057 }
1058 break;
1059 default:
1060 return CR16_INVALID;
1061 }
1062 /* Check if operand 0 of operand 0 is REGP. */
1063 switch (GET_CODE (XEXP (XEXP (addr, 0), 0)))
1064 {
1065 case REG:
1066 case SUBREG:
1067 /* Now check if REG is a REGP and not in LONG regs. */
1068 if (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (addr, 0), 0)))
1069 > BITS_PER_WORD)
1070 {
1071 if (REGNO (XEXP (XEXP (addr, 0), 0))
1072 >= CR16_FIRST_DWORD_REGISTER)
1073 return CR16_INVALID;
1074 base = XEXP (XEXP (addr, 0), 0);
1075 if (debug_print)
1076 {
1077 fprintf (stderr, "\nbase:");
1078 debug_rtx (base);
1079 }
1080 }
1081 else
1082 return CR16_INVALID;
1083 break;
1084 default:
1085 return CR16_INVALID;
1086 }
1087 /* Now check if the operand 1 of operand 0 is const_int. */
1088 if (GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
1089 {
1090 disp = XEXP (XEXP (addr, 0), 1);
1091 if (debug_print)
1092 {
1093 fprintf (stderr, "\ndisp:");
1094 debug_rtx (disp);
1095 }
1096 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 20))
1097 return CR16_INVALID;
1098 }
1099 else
1100 return CR16_INVALID;
1101 retval = CR16_INDEX_REGP_REL;
1102 break;
1103 default:
1104 return CR16_INVALID;
1105 }
1106 break;
1107
1108 default:
1109 return CR16_INVALID;
1110 }
1111
1112 /* Check if the base and index registers are valid. */
1113 if (base && !(cr16_addr_reg_p (base)))
1114 return CR16_INVALID;
1115 if (base && !(CR16_REG_OK_FOR_BASE_P (base)))
1116 return CR16_INVALID;
1117 if (index && !(REG_OK_FOR_INDEX_P (index)))
1118 return CR16_INVALID;
1119
1120 /* Write the decomposition to out parameter. */
1121 out->base = base;
1122 out->disp = disp;
1123 out->index = index;
1124 out->data = data;
1125 out->code = code;
1126
1127 return retval;
1128 }
1129
1130 /* Return non-zero value if 'x' is legitimate PIC operand
1131 when generating PIC code. */
1132 int
1133 legitimate_pic_operand_p (rtx x)
1134 {
1135 switch (GET_CODE (x))
1136 {
1137 case SYMBOL_REF:
1138 return 0;
1139 break;
1140 case LABEL_REF:
1141 return 0;
1142 break;
1143 case CONST:
1144 /* REVISIT: Use something like symbol_referenced_p. */
1145 if (GET_CODE (XEXP (x, 0)) == PLUS
1146 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1147 || GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)
1148 && (GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
1149 return 0;
1150 break;
1151 case MEM:
1152 return legitimate_pic_operand_p (XEXP (x, 0));
1153 break;
1154 default:
1155 break;
1156 }
1157 return 1;
1158 }
1159
1160 /* Convert a non-PIC address in `orig' to a PIC address in `reg'.
1161
1162 Input Output (-f pic) Output (-f PIC)
1163 orig reg
1164
1165 C1 symbol symbol@BRO (r12) symbol@GOT (r12)
1166
1167 C2 symbol + offset symbol+offset@BRO (r12) symbol+offset@GOT (r12)
1168
1169 NOTE: @BRO is added using unspec:BRO
1170 NOTE: @GOT is added using unspec:GOT. */
1171 rtx
1172 legitimize_pic_address (rtx orig, machine_mode mode ATTRIBUTE_UNUSED,
1173 rtx reg)
1174 {
1175 /* First handle a simple SYMBOL_REF or LABEL_REF. */
1176 if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
1177 {
1178 if (reg == 0)
1179 reg = gen_reg_rtx (Pmode);
1180
1181 if (flag_pic == NEAR_PIC)
1182 {
1183 /* Unspec to handle -fpic option. */
1184 emit_insn (gen_unspec_bro_addr (reg, orig));
1185 emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
1186 }
1187 else if (flag_pic == FAR_PIC)
1188 {
1189 /* Unspec to handle -fPIC option. */
1190 emit_insn (gen_unspec_got_addr (reg, orig));
1191 }
1192 return reg;
1193 }
1194 else if (GET_CODE (orig) == CONST)
1195 {
1196 /* To handle (symbol + offset). */
1197 rtx base, offset;
1198
1199 if (GET_CODE (XEXP (orig, 0)) == PLUS
1200 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1201 return orig;
1202
1203 if (reg == 0)
1204 {
1205 gcc_assert (can_create_pseudo_p ());
1206 reg = gen_reg_rtx (Pmode);
1207 }
1208
1209 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
1210
1211 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1212 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1213 base == reg ? 0 : reg);
1214
1215 /* REVISIT: Optimize for const-offsets. */
1216 emit_insn (gen_addsi3 (reg, base, offset));
1217
1218 return reg;
1219 }
1220 return orig;
1221 }
1222
1223 /* Implementation of TARGET_LEGITIMATE_ADDRESS_P. */
1224 static bool
1225 cr16_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED,
1226 rtx addr, bool strict)
1227 {
1228 enum cr16_addrtype addrtype;
1229 struct cr16_address address;
1230
1231 if (TARGET_DEBUG_ADDR)
1232 {
1233 fprintf (stderr,
1234 "\n======\nTARGET_LEGITIMATE_ADDRESS_P, mode = %s, strict = %d",
1235 GET_MODE_NAME (mode), strict);
1236 debug_rtx (addr);
1237 }
1238 addrtype = cr16_decompose_address (addr, &address,
1239 (TARGET_DEBUG_ADDR ? 1 : 0), FALSE);
1240
1241 if (TARGET_DEBUG_ADDR)
1242 {
1243 const char *typestr;
1244
1245 switch (addrtype)
1246 {
1247 case CR16_INVALID:
1248 typestr = "invalid";
1249 break;
1250 case CR16_ABSOLUTE:
1251 typestr = "absolute";
1252 break;
1253 case CR16_REG_REL:
1254 typestr = "register relative";
1255 break;
1256 case CR16_REGP_REL:
1257 typestr = "register pair relative";
1258 break;
1259 case CR16_INDEX_REGP_REL:
1260 typestr = "index + register pair relative";
1261 break;
1262 default:
1263 gcc_unreachable ();
1264 }
1265 fprintf (stderr, "\ncr16 address type: %s\n", typestr);
1266 }
1267
1268 if (addrtype == CR16_INVALID)
1269 return FALSE;
1270
1271 if (strict)
1272 {
1273 if (address.base
1274 && !REGNO_MODE_OK_FOR_BASE_P (REGNO (address.base), mode))
1275 {
1276 if (TARGET_DEBUG_ADDR)
1277 fprintf (stderr, "base register not strict\n");
1278 return FALSE;
1279 }
1280 if (address.index && !REGNO_OK_FOR_INDEX_P (REGNO (address.index)))
1281 {
1282 if (TARGET_DEBUG_ADDR)
1283 fprintf (stderr, "index register not strict\n");
1284 return FALSE;
1285 }
1286 }
1287
1288 /* Return true if addressing mode is register relative. */
1289 if (flag_pic)
1290 {
1291 if (addrtype == CR16_REG_REL || addrtype == CR16_REGP_REL)
1292 return TRUE;
1293 else
1294 return FALSE;
1295 }
1296
1297 return TRUE;
1298 }
1299
1300 /* Routines to compute costs. */
1301
1302 /* Return cost of the memory address x. */
1303 static int
1304 cr16_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED,
1305 addr_space_t as ATTRIBUTE_UNUSED,
1306 bool speed ATTRIBUTE_UNUSED)
1307 {
1308 enum cr16_addrtype addrtype;
1309 struct cr16_address address;
1310 int cost = 2;
1311
1312 addrtype = cr16_decompose_address (addr, &address, 0, FALSE);
1313
1314 gcc_assert (addrtype != CR16_INVALID);
1315
1316 /* CR16_ABSOLUTE : 3
1317 CR16_REG_REL (disp !=0) : 4
1318 CR16_REG_REL (disp ==0) : 5
1319 CR16_REGP_REL (disp !=0) : 6
1320 CR16_REGP_REL (disp ==0) : 7
1321 CR16_INDEX_REGP_REL (disp !=0) : 8
1322 CR16_INDEX_REGP_REL (disp ==0) : 9. */
1323 switch (addrtype)
1324 {
1325 case CR16_ABSOLUTE:
1326 cost += 1;
1327 break;
1328 case CR16_REGP_REL:
1329 cost += 2;
1330 /* Fall through. */
1331 case CR16_REG_REL:
1332 cost += 3;
1333 if (address.disp)
1334 cost -= 1;
1335 break;
1336 case CR16_INDEX_REGP_REL:
1337 cost += 7;
1338 if (address.disp)
1339 cost -= 1;
1340 default:
1341 break;
1342 }
1343
1344 if (TARGET_DEBUG_ADDR)
1345 {
1346 fprintf (stderr, "\n======\nmacro TARGET_ADDRESS_COST = %d\n", cost);
1347 debug_rtx (addr);
1348 }
1349
1350 return cost;
1351 }
1352
1353
1354 /* Implement `TARGET_REGISTER_MOVE_COST'. */
1355 static int
1356 cr16_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
1357 reg_class_t from ATTRIBUTE_UNUSED, reg_class_t to)
1358 {
1359 return (to != GENERAL_REGS ? 8 : 2);
1360 }
1361
1362 /* Implement `TARGET_MEMORY_MOVE_COST'. */
1363
1364 /* Return the cost of moving data of mode MODE between a register of class
1365 CLASS and memory; IN is zero if the value is to be written to memory,
1366 nonzero if it is to be read in. This cost is relative to those in
1367 REGISTER_MOVE_COST. */
1368 static int
1369 cr16_memory_move_cost (machine_mode mode,
1370 reg_class_t rclass ATTRIBUTE_UNUSED,
1371 bool in ATTRIBUTE_UNUSED)
1372 {
1373 /* One LD or ST takes twice the time of a simple reg-reg move. */
1374 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
1375 return (4 * HARD_REGNO_NREGS (0, mode));
1376 else
1377 return (100);
1378 }
1379
1380 /* Instruction output. */
1381
1382 /* Check if a const_double is ok for cr16 store-immediate instructions. */
1383 int
1384 cr16_const_double_ok (rtx op)
1385 {
1386 if (GET_MODE (op) == SFmode)
1387 {
1388 REAL_VALUE_TYPE r;
1389 long l;
1390 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
1391 REAL_VALUE_TO_TARGET_SINGLE (r, l);
1392 return UNSIGNED_INT_FITS_N_BITS (l, 4) ? 1 : 0;
1393 }
1394
1395 return ((UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op), 4)) &&
1396 (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op), 4))) ? 1 : 0;
1397 }
1398
1399 /* Returns bit position of first 0 or 1 bit.
1400 It is safe to assume val as 16-bit wide. */
1401 int
1402 cr16_operand_bit_pos (int val, int bitval)
1403 {
1404 int i;
1405 if (bitval == 0)
1406 val = ~val;
1407
1408 for (i = 0; i < 16; i++)
1409 if (val & (1 << i))
1410 break;
1411 return i;
1412 }
1413
1414 /* Implements the macro PRINT_OPERAND defined in cr16.h. */
1415 static void
1416 cr16_print_operand (FILE * file, rtx x, int code)
1417 {
1418 int ptr_dereference = 0;
1419
1420 switch (code)
1421 {
1422 case 'd':
1423 {
1424 const char *cr16_cmp_str;
1425 switch (GET_CODE (x))
1426 {
1427 /* MD: compare (reg, reg or imm) but CR16: cmp (reg or imm, reg)
1428 -> swap all non symmetric ops. */
1429 case EQ:
1430 cr16_cmp_str = "eq";
1431 break;
1432 case NE:
1433 cr16_cmp_str = "ne";
1434 break;
1435 case GT:
1436 cr16_cmp_str = "lt";
1437 break;
1438 case GTU:
1439 cr16_cmp_str = "lo";
1440 break;
1441 case LT:
1442 cr16_cmp_str = "gt";
1443 break;
1444 case LTU:
1445 cr16_cmp_str = "hi";
1446 break;
1447 case GE:
1448 cr16_cmp_str = "le";
1449 break;
1450 case GEU:
1451 cr16_cmp_str = "ls";
1452 break;
1453 case LE:
1454 cr16_cmp_str = "ge";
1455 break;
1456 case LEU:
1457 cr16_cmp_str = "hs";
1458 break;
1459 default:
1460 gcc_unreachable ();
1461 }
1462 fprintf (file, "%s", cr16_cmp_str);
1463 return;
1464 }
1465 case '$':
1466 putc ('$', file);
1467 return;
1468
1469 case 'p':
1470 if (GET_CODE (x) == REG)
1471 {
1472 /* For Push instructions, we should not print register pairs. */
1473 fprintf (file, "%s", reg_names[REGNO (x)]);
1474 return;
1475 }
1476 break;
1477
1478 case 'b':
1479 /* Print the immediate address for bal
1480 'b' is used instead of 'a' to avoid compiler calling
1481 the GO_IF_LEGITIMATE_ADDRESS which cannot
1482 perform checks on const_int code addresses as it
1483 assumes all const_int are data addresses. */
1484 fprintf (file, "0x%lx", INTVAL (x));
1485 return;
1486
1487 case 'r':
1488 /* Print bit position of first 0. */
1489 fprintf (file, "%d", cr16_operand_bit_pos (INTVAL (x), 0));
1490 return;
1491
1492 case 's':
1493 /* Print bit position of first 1. */
1494 fprintf (file, "%d", cr16_operand_bit_pos (INTVAL (x), 1));
1495 return;
1496 case 'g':
1497 /* 'g' is used for implicit mem: dereference. */
1498 ptr_dereference = 1;
1499 case 'f':
1500 case 0:
1501 /* default. */
1502 switch (GET_CODE (x))
1503 {
1504 case REG:
1505 if (GET_MODE_BITSIZE (GET_MODE (x)) > BITS_PER_WORD)
1506 {
1507 if (LONG_REG_P (REGNO (x)))
1508 fprintf (file, "(%s)", reg_names[REGNO (x)]);
1509 else
1510 fprintf (file, "(%s,%s)", reg_names[REGNO (x) + 1],
1511 reg_names[REGNO (x)]);
1512 }
1513 else
1514 fprintf (file, "%s", reg_names[REGNO (x)]);
1515 return;
1516
1517 case MEM:
1518 output_address (XEXP (x, 0));
1519 return;
1520
1521 case CONST_DOUBLE:
1522 {
1523 REAL_VALUE_TYPE r;
1524 long l;
1525
1526 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1527 REAL_VALUE_TO_TARGET_SINGLE (r, l);
1528
1529 fprintf (file, "$0x%lx", l);
1530 return;
1531 }
1532 case CONST_INT:
1533 {
1534 fprintf (file, "$%ld", INTVAL (x));
1535 return;
1536 }
1537 case UNSPEC:
1538 switch (XINT (x, 1))
1539 {
1540 default:
1541 gcc_unreachable ();
1542 }
1543 break;
1544
1545 default:
1546 if (!ptr_dereference)
1547 {
1548 putc ('$', file);
1549 }
1550 cr16_print_operand_address (file, x);
1551 return;
1552 }
1553 default:
1554 output_operand_lossage ("invalid %%xn code");
1555 }
1556
1557 gcc_unreachable ();
1558 }
1559
1560 /* Implements the macro PRINT_OPERAND_ADDRESS defined in cr16.h. */
1561
1562 static void
1563 cr16_print_operand_address (FILE * file, rtx addr)
1564 {
1565 enum cr16_addrtype addrtype;
1566 struct cr16_address address;
1567
1568 /* Decompose the address. Also ask it to treat address as constant. */
1569 addrtype = cr16_decompose_address (addr, &address, 0, TRUE);
1570
1571 if (address.disp && GET_CODE (address.disp) == UNSPEC)
1572 {
1573 debug_rtx (addr);
1574 }
1575
1576 switch (addrtype)
1577 {
1578 case CR16_REG_REL:
1579 if (address.disp)
1580 {
1581 if (GET_CODE (address.disp) == UNSPEC)
1582 cr16_print_operand (file, address.disp, 0);
1583 else
1584 output_addr_const (file, address.disp);
1585 }
1586 else
1587 fprintf (file, "0");
1588 fprintf (file, "(%s)", reg_names[REGNO (address.base)]);
1589 break;
1590
1591 case CR16_ABSOLUTE:
1592 if (address.disp)
1593 output_addr_const (file, address.disp);
1594 else
1595 fprintf (file, "0");
1596 break;
1597
1598 case CR16_INDEX_REGP_REL:
1599 fprintf (file, "[%s]", reg_names[REGNO (address.index)]);
1600 /* Fall through. */
1601 case CR16_REGP_REL:
1602 if (address.disp)
1603 {
1604 if (GET_CODE (address.disp) == UNSPEC)
1605 cr16_print_operand (file, address.disp, 0);
1606 else
1607 output_addr_const (file, address.disp);
1608 }
1609 else
1610 fprintf (file, "0");
1611 fprintf (file, "(%s,%s)", reg_names[REGNO (address.base) + 1],
1612 reg_names[REGNO (address.base)]);
1613 break;
1614 default:
1615 debug_rtx (addr);
1616 gcc_unreachable ();
1617 }
1618 /* Add qualifiers to the address expression that was just printed. */
1619 if (flag_pic < NEAR_PIC && address.code == 0)
1620 {
1621 if (address.data == DM_FAR)
1622 /* Addr contains SYMBOL_REF & far data ptr. */
1623 fprintf (file, "@l");
1624 else if (address.data == DM_DEFAULT)
1625 /* Addr contains SYMBOL_REF & medium data ptr. */
1626 fprintf (file, "@m");
1627 /* Addr contains SYMBOL_REF & medium data ptr. */
1628 else if (address.data == DM_NEAR)
1629 /* Addr contains SYMBOL_REF & near data ptr. */
1630 fprintf (file, "@s");
1631 }
1632 else if (flag_pic == NEAR_PIC
1633 && (address.code == 0) && (address.data == DM_FAR
1634 || address.data == DM_DEFAULT
1635 || address.data == DM_NEAR))
1636 {
1637 fprintf (file, "@l");
1638 }
1639 else if (flag_pic == NEAR_PIC && address.code == 2)
1640 {
1641 fprintf (file, "pic");
1642 }
1643 else if (flag_pic == NEAR_PIC && address.code == 1)
1644 {
1645 fprintf (file, "@cpic");
1646 }
1647
1648 else if (flag_pic == FAR_PIC && address.code == 2)
1649 {
1650 /* REVISIT: cr16 register indirect jump expects a 1-bit right shifted
1651 address ! GOTc tells assembler this symbol is a text-address
1652 This needs to be fixed in such a way that this offset is done
1653 only in the case where an address is being used for indirect jump
1654 or call. Determining the potential usage of loadd is of course not
1655 possible always. Eventually, this has to be fixed in the
1656 processor. */
1657 fprintf (file, "GOT (%s)", reg_names[PIC_OFFSET_TABLE_REGNUM]);
1658 }
1659 else if (flag_pic == FAR_PIC && address.code == 1)
1660 {
1661 fprintf (file, "@cGOT (%s)", reg_names[PIC_OFFSET_TABLE_REGNUM]);
1662 }
1663
1664 else if (flag_pic == FAR_PIC &&
1665 (address.data == DM_FAR || address.data == DM_DEFAULT
1666 || address.data == DM_NEAR))
1667 {
1668 fprintf (file, "@GOT (%s)", reg_names[PIC_OFFSET_TABLE_REGNUM]);
1669 }
1670 }
1671
1672 /* Machine description helper functions. */
1673
1674 /* Called from cr16.md. The return value depends on the parameter push_or_pop:
1675 When push_or_pop is zero -> string for push instructions of prologue.
1676 When push_or_pop is nonzero -> string for pop/popret/retx in epilogue.
1677 Relies on the assumptions:
1678 1. RA is the last register to be saved.
1679 2. The maximal value of the counter is MAX_COUNT. */
1680 char *
1681 cr16_prepare_push_pop_string (int push_or_pop)
1682 {
1683 /* j is the number of registers being saved, takes care that there won't be
1684 more than 8 in one push/pop instruction. */
1685
1686 /* For the register mask string. */
1687 static char one_inst_str[50];
1688
1689 /* i is the index of current_frame_info.save_regs[], going from 0 until
1690 current_frame_info.last_reg_to_save. */
1691 int i, start_reg;
1692 int word_cnt;
1693 int print_ra;
1694 char *return_str;
1695
1696 /* For reversing on the push instructions if there are more than one. */
1697 char *temp_str;
1698
1699 return_str = (char *) xmalloc (160);
1700 temp_str = (char *) xmalloc (160);
1701
1702 /* Initialize. */
1703 memset (return_str, 0, 3);
1704
1705 i = 0;
1706 while (i <= current_frame_info.last_reg_to_save)
1707 {
1708 /* Prepare mask for one instruction. */
1709 one_inst_str[0] = 0;
1710
1711 /* To count number of words in one instruction. */
1712 word_cnt = 0;
1713 start_reg = i;
1714 print_ra = 0;
1715 while ((word_cnt < MAX_COUNT)
1716 && (i <= current_frame_info.last_reg_to_save))
1717 {
1718 /* For each non consecutive save register,
1719 a new instruction shall be generated. */
1720 if (!current_frame_info.save_regs[i])
1721 {
1722 /* Move to next reg and break. */
1723 ++i;
1724 break;
1725 }
1726
1727 if (i == RETURN_ADDRESS_REGNUM)
1728 print_ra = 1;
1729 else
1730 {
1731 /* Check especially if adding 2 does not cross the MAX_COUNT. */
1732 if ((word_cnt + ((i < CR16_FIRST_DWORD_REGISTER) ? 1 : 2))
1733 >= MAX_COUNT)
1734 break;
1735 /* Increase word count by 2 for long registers except RA. */
1736 word_cnt += ((i < CR16_FIRST_DWORD_REGISTER) ? 1 : 2);
1737 }
1738 ++i;
1739 }
1740
1741 /* No need to generate any instruction as
1742 no register or RA needs to be saved. */
1743 if ((word_cnt == 0) && (print_ra == 0))
1744 continue;
1745
1746 /* Now prepare the instruction operands. */
1747 if (word_cnt > 0)
1748 {
1749 sprintf (one_inst_str, "$%d, %s", word_cnt, reg_names[start_reg]);
1750 if (print_ra)
1751 strcat (one_inst_str, ", ra");
1752 }
1753 else
1754 strcat (one_inst_str, "ra");
1755
1756 if (push_or_pop == 1)
1757 {
1758 /* Pop instruction. */
1759 if (print_ra && !cr16_interrupt_function_p ()
1760 && !crtl->calls_eh_return)
1761 /* Print popret if RA is saved and its not a interrupt
1762 function. */
1763 strcpy (temp_str, "\n\tpopret\t");
1764 else
1765 strcpy (temp_str, "\n\tpop\t");
1766
1767 strcat (temp_str, one_inst_str);
1768
1769 /* Add the pop instruction list. */
1770 strcat (return_str, temp_str);
1771 }
1772 else
1773 {
1774 /* Push instruction. */
1775 strcpy (temp_str, "\n\tpush\t");
1776 strcat (temp_str, one_inst_str);
1777
1778 /* We need to reverse the order of the instructions if there
1779 are more than one. (since the pop will not be reversed in
1780 the epilogue. */
1781 strcat (temp_str, return_str);
1782 strcpy (return_str, temp_str);
1783 }
1784 }
1785
1786 if (push_or_pop == 1)
1787 {
1788 /* POP. */
1789 if (cr16_interrupt_function_p ())
1790 strcat (return_str, "\n\tretx\n");
1791 else if (crtl->calls_eh_return)
1792 {
1793 /* Add stack adjustment before returning to exception handler
1794 NOTE: EH_RETURN_STACKADJ_RTX must refer to (r5, r4). */
1795 strcat (return_str, "\n\taddd\t (r5, r4), (sp)\t\n");
1796 strcat (return_str, "\n\tjump\t (ra)\n");
1797
1798 /* But before anything else, undo the adjustment addition done in
1799 cr16_expand_epilogue (). */
1800 strcpy (temp_str, "\n\tsubd\t (r5, r4), (sp)\t\n");
1801 strcat (temp_str, return_str);
1802 strcpy (return_str, temp_str);
1803 }
1804 else if (!FUNC_IS_NORETURN_P (current_function_decl)
1805 && !(current_frame_info.save_regs[RETURN_ADDRESS_REGNUM]))
1806 strcat (return_str, "\n\tjump\t (ra)\n");
1807 }
1808
1809 /* Skip the newline and the tab in the start of return_str. */
1810 return_str += 2;
1811 return return_str;
1812 }
1813
1814
1815 /* Generate DWARF2 annotation for multi-push instruction. */
1816 static void
1817 cr16_create_dwarf_for_multi_push (rtx insn)
1818 {
1819 rtx dwarf, reg, tmp;
1820 int i, j, from, to, word_cnt, dwarf_par_index, inc;
1821 machine_mode mode;
1822 int num_regs = 0, offset = 0, split_here = 0, total_push_bytes = 0;
1823
1824 for (i = 0; i <= current_frame_info.last_reg_to_save; ++i)
1825 {
1826 if (current_frame_info.save_regs[i])
1827 {
1828 ++num_regs;
1829 if (i < CR16_FIRST_DWORD_REGISTER)
1830 total_push_bytes += 2;
1831 else
1832 total_push_bytes += 4;
1833 }
1834 }
1835
1836 if (!num_regs)
1837 return;
1838
1839 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_regs + 1));
1840 dwarf_par_index = num_regs;
1841
1842 from = current_frame_info.last_reg_to_save + 1;
1843 to = current_frame_info.last_reg_to_save;
1844 word_cnt = 0;
1845
1846 for (i = current_frame_info.last_reg_to_save; i >= 0;)
1847 {
1848 if (!current_frame_info.save_regs[i] || 0 == i || split_here)
1849 {
1850 /* This block of regs is pushed in one instruction. */
1851 if (0 == i && current_frame_info.save_regs[i])
1852 from = 0;
1853
1854 for (j = to; j >= from; --j)
1855 {
1856 if (j < CR16_FIRST_DWORD_REGISTER)
1857 {
1858 mode = HImode;
1859 inc = 1;
1860 }
1861 else
1862 {
1863 mode = SImode;
1864 inc = 2;
1865 }
1866 reg = gen_rtx_REG (mode, j);
1867 offset += 2 * inc;
1868 tmp = gen_rtx_SET (gen_frame_mem (mode,
1869 plus_constant
1870 (Pmode, stack_pointer_rtx,
1871 total_push_bytes - offset)),
1872 reg);
1873 RTX_FRAME_RELATED_P (tmp) = 1;
1874 XVECEXP (dwarf, 0, dwarf_par_index--) = tmp;
1875 }
1876 from = i;
1877 to = --i;
1878 split_here = 0;
1879 word_cnt = 0;
1880 continue;
1881 }
1882
1883 if (i != RETURN_ADDRESS_REGNUM)
1884 {
1885 inc = (i < CR16_FIRST_DWORD_REGISTER) ? 1 : 2;
1886 if (word_cnt + inc >= MAX_COUNT || FRAME_POINTER_REGNUM == i)
1887 {
1888 split_here = 1;
1889 from = i;
1890 continue;
1891 }
1892 word_cnt += inc;
1893 }
1894
1895 from = i--;
1896 }
1897
1898 tmp = gen_rtx_SET (stack_pointer_rtx,
1899 gen_rtx_PLUS (SImode, stack_pointer_rtx,
1900 GEN_INT (-offset)));
1901 RTX_FRAME_RELATED_P (tmp) = 1;
1902 XVECEXP (dwarf, 0, 0) = tmp;
1903
1904 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
1905 }
1906
1907 /*
1908 CompactRISC CR16 Architecture stack layout:
1909
1910 0 +---------------------
1911 |
1912 .
1913 .
1914 |
1915 +==================== Sp (x) = Ap (x+1)
1916 A | Args for functions
1917 | | called by X and Dynamically
1918 | | Dynamic allocations allocated and
1919 | | (alloca, variable deallocated
1920 Stack | length arrays).
1921 grows +-------------------- Fp (x)
1922 down| | Local variables of X
1923 ward| +--------------------
1924 | | Regs saved for X-1
1925 | +==================== Sp (x-1) = Ap (x)
1926 | Args for func X
1927 | pushed by X-1
1928 +-------------------- Fp (x-1)
1929 |
1930 |
1931 V
1932 */
1933 void
1934 cr16_expand_prologue (void)
1935 {
1936 rtx insn;
1937
1938 cr16_compute_frame ();
1939 cr16_compute_save_regs ();
1940
1941 /* If there is no need in push and adjustment to sp, return. */
1942 if ((current_frame_info.total_size + current_frame_info.reg_size) == 0)
1943 return;
1944
1945 if (current_frame_info.last_reg_to_save != -1)
1946 {
1947 /* If there are registers to push. */
1948 insn = emit_insn (gen_push_for_prologue
1949 (GEN_INT (current_frame_info.reg_size)));
1950 cr16_create_dwarf_for_multi_push (insn);
1951 RTX_FRAME_RELATED_P (insn) = 1;
1952 }
1953
1954
1955 if (current_frame_info.total_size > 0)
1956 {
1957 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1958 GEN_INT (-current_frame_info.total_size)));
1959 RTX_FRAME_RELATED_P (insn) = 1;
1960 }
1961
1962 if (frame_pointer_needed)
1963 {
1964 /* Initialize the frame pointer with the value of the stack pointer
1965 pointing now to the locals. */
1966 insn = emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1967 }
1968 }
1969
1970 /* Generate insn that updates the stack for local variables and padding
1971 for registers we save. - Generate the appropriate return insn. */
1972 void
1973 cr16_expand_epilogue (void)
1974 {
1975 rtx insn;
1976
1977 /* Nonzero if we need to return and pop only RA. This will generate a
1978 different insn. This differentiate is for the peepholes for call as
1979 last statement in function. */
1980 int only_popret_RA = (current_frame_info.save_regs[RETURN_ADDRESS_REGNUM]
1981 && (current_frame_info.reg_size
1982 == CR16_UNITS_PER_DWORD));
1983
1984 if (frame_pointer_needed)
1985 {
1986 /* Restore the stack pointer with the frame pointers value. */
1987 insn = emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1988 }
1989
1990 if (current_frame_info.total_size > 0)
1991 {
1992 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1993 GEN_INT (current_frame_info.total_size)));
1994 RTX_FRAME_RELATED_P (insn) = 1;
1995 }
1996
1997 if (crtl->calls_eh_return)
1998 {
1999 /* Add this here so that (r5, r4) is actually loaded with the adjustment
2000 value; otherwise, the load might be optimized away...
2001 NOTE: remember to subtract the adjustment before popping the regs
2002 and add it back before returning. */
2003 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2004 EH_RETURN_STACKADJ_RTX));
2005 }
2006
2007 if (cr16_interrupt_function_p ())
2008 {
2009 insn = emit_jump_insn (gen_interrupt_return ());
2010 RTX_FRAME_RELATED_P (insn) = 1;
2011 }
2012 else if (crtl->calls_eh_return)
2013 {
2014 /* Special case, pop what's necessary, adjust SP and jump to (RA). */
2015 insn = emit_jump_insn (gen_pop_and_popret_return
2016 (GEN_INT (current_frame_info.reg_size)));
2017 RTX_FRAME_RELATED_P (insn) = 1;
2018 }
2019 else if (current_frame_info.last_reg_to_save == -1)
2020 /* Nothing to pop. */
2021 /* Don't output jump for interrupt routine, only retx. */
2022 emit_jump_insn (gen_jump_return ());
2023 else if (only_popret_RA)
2024 {
2025 insn = emit_jump_insn (gen_popret_RA_return ());
2026 RTX_FRAME_RELATED_P (insn) = 1;
2027 }
2028 else
2029 {
2030 insn = emit_jump_insn (gen_pop_and_popret_return
2031 (GEN_INT (current_frame_info.reg_size)));
2032 RTX_FRAME_RELATED_P (insn) = 1;
2033 }
2034 }
2035
2036 /* Implements FRAME_POINTER_REQUIRED. */
2037 static bool
2038 cr16_frame_pointer_required (void)
2039 {
2040 return (cfun->calls_alloca || crtl->calls_eh_return
2041 || cfun->has_nonlocal_label || crtl->calls_eh_return);
2042 }
2043
2044 static bool
2045 cr16_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
2046 {
2047 return (to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true);
2048 }
2049
2050
2051 /* A C compound statement that attempts to replace X with
2052 a valid memory address for an operand of mode MODE. WIN
2053 will be a C statement label elsewhere in the code.
2054 X will always be the result of a call to break_out_memory_refs (),
2055 and OLDX will be the operand that was given to that function to
2056 produce X.
2057 The code generated by this macro should not alter the
2058 substructure of X. If it transforms X into a more legitimate form,
2059 it should assign X (which will always be a C variable) a new value. */
2060 static rtx
2061 cr16_legitimize_address (rtx x, rtx orig_x ATTRIBUTE_UNUSED,
2062 machine_mode mode ATTRIBUTE_UNUSED)
2063 {
2064 if (flag_pic)
2065 return legitimize_pic_address (orig_x, mode, NULL_RTX);
2066 else
2067 return x;
2068 }
2069
2070 /* Implement TARGET_LEGITIMATE_CONSTANT_P
2071 Nonzero if X is a legitimate constant for an immediate
2072 operand on the target machine. You can assume that X
2073 satisfies CONSTANT_P. In cr16c treat legitimize float
2074 constant as an immediate operand. */
2075 static bool
2076 cr16_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
2077 rtx x ATTRIBUTE_UNUSED)
2078 {
2079 return 1;
2080 }
2081
2082 void
2083 notice_update_cc (rtx exp)
2084 {
2085 if (GET_CODE (exp) == SET)
2086 {
2087 /* Jumps do not alter the cc's. */
2088 if (SET_DEST (exp) == pc_rtx)
2089 return;
2090
2091 /* Moving register or memory into a register:
2092 it doesn't alter the cc's, but it might invalidate
2093 the RTX's which we remember the cc's came from.
2094 (Note that moving a constant 0 or 1 MAY set the cc's). */
2095 if (REG_P (SET_DEST (exp))
2096 && (REG_P (SET_SRC (exp)) || GET_CODE (SET_SRC (exp)) == MEM))
2097 {
2098 return;
2099 }
2100
2101 /* Moving register into memory doesn't alter the cc's.
2102 It may invalidate the RTX's which we remember the cc's came from. */
2103 if (GET_CODE (SET_DEST (exp)) == MEM && REG_P (SET_SRC (exp)))
2104 {
2105 return;
2106 }
2107 }
2108
2109 CC_STATUS_INIT;
2110 return;
2111 }
2112
2113 static machine_mode
2114 cr16_unwind_word_mode (void)
2115 {
2116 return SImode;
2117 }
2118
2119 /* Helper function for md file. This function is used to emit arithmetic
2120 DI instructions. The argument "num" decides which instruction to be
2121 printed. */
2122 const char *
2123 cr16_emit_add_sub_di (rtx *operands, enum rtx_code code)
2124 {
2125 rtx lo_op[2] ;
2126 rtx hi0_op[2] ;
2127 rtx hi1_op[2] ;
2128
2129 lo_op[0] = gen_lowpart (SImode, operands[0]);
2130 hi0_op[0] = simplify_gen_subreg (HImode, operands[0], DImode, 4);
2131 hi1_op[0] = simplify_gen_subreg (HImode, operands[0], DImode, 6);
2132
2133 lo_op[1] = gen_lowpart (SImode, operands[2]);
2134 hi0_op[1] = simplify_gen_subreg (HImode, operands[2], DImode, 4);
2135 hi1_op[1] = simplify_gen_subreg (HImode, operands[2], DImode, 6);
2136
2137 switch (code)
2138 {
2139 case PLUS:
2140 {
2141 output_asm_insn ("addd\t%1, %0", lo_op) ;
2142 output_asm_insn ("addcw\t%1, %0", hi0_op) ;
2143 output_asm_insn ("addcw\t%1, %0", hi1_op) ;
2144 break;
2145 }
2146 case MINUS:
2147 {
2148 output_asm_insn ("subd\t%1, %0", lo_op) ;
2149 output_asm_insn ("subcw\t%1, %0", hi0_op) ;
2150 output_asm_insn ("subcw\t%1, %0", hi1_op) ;
2151 break;
2152 }
2153 default:
2154 break;
2155 }
2156
2157 return "";
2158 }
2159
2160
2161 /* Helper function for md file. This function is used to emit logical
2162 DI instructions. The argument "num" decides which instruction to be
2163 printed. */
2164 const char *
2165 cr16_emit_logical_di (rtx *operands, enum rtx_code code)
2166 {
2167 rtx lo_op[2] ;
2168 rtx hi_op[2] ;
2169
2170 lo_op[0] = gen_lowpart (SImode, operands[0]);
2171 hi_op[0] = simplify_gen_subreg (SImode, operands[0], DImode, 4);
2172
2173 lo_op[1] = gen_lowpart (SImode, operands[2]);
2174 hi_op[1] = simplify_gen_subreg (SImode, operands[2], DImode, 4);
2175
2176 switch (code)
2177 {
2178 case AND:
2179 {
2180 output_asm_insn ("andd\t%1, %0", lo_op) ;
2181 output_asm_insn ("andd\t%1, %0", hi_op) ;
2182 return "";
2183 }
2184 case IOR:
2185 {
2186 output_asm_insn ("ord\t%1, %0", lo_op) ;
2187 output_asm_insn ("ord\t%1, %0", hi_op) ;
2188 return "";
2189 }
2190 case XOR:
2191 {
2192 output_asm_insn ("xord\t%1, %0", lo_op) ;
2193 output_asm_insn ("xord\t%1, %0", hi_op) ;
2194 return "";
2195 }
2196 default:
2197 break;
2198 }
2199
2200 return "";
2201 }
2202
2203 /* Initialize 'targetm' variable which contains pointers to functions
2204 and data relating to the target machine. */
2205
2206 struct gcc_target targetm = TARGET_INITIALIZER;