]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/moxie/moxie.c
ea36db3629184628ffce4bc9164d1ad1c9849459
[thirdparty/gcc.git] / gcc / config / moxie / moxie.c
1 /* Target Code for moxie
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 Contributed by Anthony Green.
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 "cfghooks.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "df.h"
29 #include "regs.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-flags.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "recog.h"
37 #include "reload.h"
38 #include "diagnostic-core.h"
39 #include "alias.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "calls.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "emit-rtl.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "insn-codes.h"
50 #include "optabs.h"
51 #include "except.h"
52 #include "target.h"
53 #include "tm_p.h"
54 #include "langhooks.h"
55 #include "cfgrtl.h"
56 #include "cfganal.h"
57 #include "lcm.h"
58 #include "cfgbuild.h"
59 #include "cfgcleanup.h"
60 #include "builtins.h"
61
62 /* This file should be included last. */
63 #include "target-def.h"
64
65 #define LOSE_AND_RETURN(msgid, x) \
66 do \
67 { \
68 moxie_operand_lossage (msgid, x); \
69 return; \
70 } while (0)
71
72 /* Worker function for TARGET_RETURN_IN_MEMORY. */
73
74 static bool
75 moxie_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
76 {
77 const HOST_WIDE_INT size = int_size_in_bytes (type);
78 return (size == -1 || size > 2 * UNITS_PER_WORD);
79 }
80
81 /* Define how to find the value returned by a function.
82 VALTYPE is the data type of the value (as a tree).
83 If the precise function being called is known, FUNC is its
84 FUNCTION_DECL; otherwise, FUNC is 0.
85
86 We always return values in register $r0 for moxie. */
87
88 static rtx
89 moxie_function_value (const_tree valtype,
90 const_tree fntype_or_decl ATTRIBUTE_UNUSED,
91 bool outgoing ATTRIBUTE_UNUSED)
92 {
93 return gen_rtx_REG (TYPE_MODE (valtype), MOXIE_R0);
94 }
95
96 /* Define how to find the value returned by a library function.
97
98 We always return values in register $r0 for moxie. */
99
100 static rtx
101 moxie_libcall_value (machine_mode mode,
102 const_rtx fun ATTRIBUTE_UNUSED)
103 {
104 return gen_rtx_REG (mode, MOXIE_R0);
105 }
106
107 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
108
109 We always return values in register $r0 for moxie. */
110
111 static bool
112 moxie_function_value_regno_p (const unsigned int regno)
113 {
114 return (regno == MOXIE_R0);
115 }
116
117 /* Emit an error message when we're in an asm, and a fatal error for
118 "normal" insns. Formatted output isn't easily implemented, since we
119 use output_operand_lossage to output the actual message and handle the
120 categorization of the error. */
121
122 static void
123 moxie_operand_lossage (const char *msgid, rtx op)
124 {
125 debug_rtx (op);
126 output_operand_lossage ("%s", msgid);
127 }
128
129 /* The PRINT_OPERAND_ADDRESS worker. */
130
131 static void
132 moxie_print_operand_address (FILE *file, rtx x)
133 {
134 switch (GET_CODE (x))
135 {
136 case REG:
137 fprintf (file, "(%s)", reg_names[REGNO (x)]);
138 break;
139
140 case PLUS:
141 switch (GET_CODE (XEXP (x, 1)))
142 {
143 case CONST_INT:
144 fprintf (file, "%ld(%s)",
145 INTVAL(XEXP (x, 1)), reg_names[REGNO (XEXP (x, 0))]);
146 break;
147 case SYMBOL_REF:
148 output_addr_const (file, XEXP (x, 1));
149 fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
150 break;
151 case CONST:
152 {
153 rtx plus = XEXP (XEXP (x, 1), 0);
154 if (GET_CODE (XEXP (plus, 0)) == SYMBOL_REF
155 && CONST_INT_P (XEXP (plus, 1)))
156 {
157 output_addr_const(file, XEXP (plus, 0));
158 fprintf (file,"+%ld(%s)", INTVAL (XEXP (plus, 1)),
159 reg_names[REGNO (XEXP (x, 0))]);
160 }
161 else
162 abort();
163 }
164 break;
165 default:
166 abort();
167 }
168 break;
169
170 default:
171 output_addr_const (file, x);
172 break;
173 }
174 }
175
176 /* The PRINT_OPERAND worker. */
177
178 static void
179 moxie_print_operand (FILE *file, rtx x, int code)
180 {
181 rtx operand = x;
182
183 /* New code entries should just be added to the switch below. If
184 handling is finished, just return. If handling was just a
185 modification of the operand, the modified operand should be put in
186 "operand", and then do a break to let default handling
187 (zero-modifier) output the operand. */
188
189 switch (code)
190 {
191 case 0:
192 /* No code, print as usual. */
193 break;
194
195 default:
196 LOSE_AND_RETURN ("invalid operand modifier letter", x);
197 }
198
199 /* Print an operand as without a modifier letter. */
200 switch (GET_CODE (operand))
201 {
202 case REG:
203 if (REGNO (operand) > MOXIE_R13)
204 internal_error ("internal error: bad register: %d", REGNO (operand));
205 fprintf (file, "%s", reg_names[REGNO (operand)]);
206 return;
207
208 case MEM:
209 output_address (XEXP (operand, 0));
210 return;
211
212 default:
213 /* No need to handle all strange variants, let output_addr_const
214 do it for us. */
215 if (CONSTANT_P (operand))
216 {
217 output_addr_const (file, operand);
218 return;
219 }
220
221 LOSE_AND_RETURN ("unexpected operand", x);
222 }
223 }
224
225 /* Per-function machine data. */
226 struct GTY(()) machine_function
227 {
228 /* Number of bytes saved on the stack for callee saved registers. */
229 int callee_saved_reg_size;
230
231 /* Number of bytes saved on the stack for local variables. */
232 int local_vars_size;
233
234 /* The sum of 2 sizes: locals vars and padding byte for saving the
235 * registers. Used in expand_prologue () and expand_epilogue(). */
236 int size_for_adjusting_sp;
237 };
238
239 /* Zero initialization is OK for all current fields. */
240
241 static struct machine_function *
242 moxie_init_machine_status (void)
243 {
244 return ggc_cleared_alloc<machine_function> ();
245 }
246
247
248 /* The TARGET_OPTION_OVERRIDE worker. */
249 static void
250 moxie_option_override (void)
251 {
252 /* Set the per-function-data initializer. */
253 init_machine_status = moxie_init_machine_status;
254
255 #ifdef TARGET_MOXIEBOX
256 target_flags |= MASK_HAS_MULX;
257 #endif
258 }
259
260 /* Compute the size of the local area and the size to be adjusted by the
261 * prologue and epilogue. */
262
263 static void
264 moxie_compute_frame (void)
265 {
266 /* For aligning the local variables. */
267 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
268 int padding_locals;
269 int regno;
270
271 /* Padding needed for each element of the frame. */
272 cfun->machine->local_vars_size = get_frame_size ();
273
274 /* Align to the stack alignment. */
275 padding_locals = cfun->machine->local_vars_size % stack_alignment;
276 if (padding_locals)
277 padding_locals = stack_alignment - padding_locals;
278
279 cfun->machine->local_vars_size += padding_locals;
280
281 cfun->machine->callee_saved_reg_size = 0;
282
283 /* Save callee-saved registers. */
284 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
285 if (df_regs_ever_live_p (regno) && (! call_used_regs[regno]))
286 cfun->machine->callee_saved_reg_size += 4;
287
288 cfun->machine->size_for_adjusting_sp =
289 crtl->args.pretend_args_size
290 + cfun->machine->local_vars_size
291 + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0);
292 }
293
294 void
295 moxie_expand_prologue (void)
296 {
297 int regno;
298 rtx insn;
299
300 moxie_compute_frame ();
301
302 if (flag_stack_usage_info)
303 current_function_static_stack_size = cfun->machine->size_for_adjusting_sp;
304
305 /* Save callee-saved registers. */
306 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
307 {
308 if (!fixed_regs[regno] && df_regs_ever_live_p (regno) && !call_used_regs[regno])
309 {
310 insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
311 RTX_FRAME_RELATED_P (insn) = 1;
312 }
313 }
314
315 if (cfun->machine->size_for_adjusting_sp > 0)
316 {
317 int i = cfun->machine->size_for_adjusting_sp;
318 while ((i >= 255) && (i <= 510))
319 {
320 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
321 stack_pointer_rtx,
322 GEN_INT (255)));
323 RTX_FRAME_RELATED_P (insn) = 1;
324 i -= 255;
325 }
326 if (i <= 255)
327 {
328 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
329 stack_pointer_rtx,
330 GEN_INT (i)));
331 RTX_FRAME_RELATED_P (insn) = 1;
332 }
333 else
334 {
335 rtx reg = gen_rtx_REG (SImode, MOXIE_R12);
336 insn = emit_move_insn (reg, GEN_INT (i));
337 RTX_FRAME_RELATED_P (insn) = 1;
338 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
339 stack_pointer_rtx,
340 reg));
341 RTX_FRAME_RELATED_P (insn) = 1;
342 }
343 }
344 }
345
346 void
347 moxie_expand_epilogue (void)
348 {
349 int regno;
350 rtx reg;
351
352 if (cfun->machine->callee_saved_reg_size != 0)
353 {
354 reg = gen_rtx_REG (Pmode, MOXIE_R12);
355 if (cfun->machine->callee_saved_reg_size <= 255)
356 {
357 emit_move_insn (reg, hard_frame_pointer_rtx);
358 emit_insn (gen_subsi3
359 (reg, reg,
360 GEN_INT (cfun->machine->callee_saved_reg_size)));
361 }
362 else
363 {
364 emit_move_insn (reg,
365 GEN_INT (-cfun->machine->callee_saved_reg_size));
366 emit_insn (gen_addsi3 (reg, reg, hard_frame_pointer_rtx));
367 }
368 for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
369 if (!fixed_regs[regno] && !call_used_regs[regno]
370 && df_regs_ever_live_p (regno))
371 {
372 rtx preg = gen_rtx_REG (Pmode, regno);
373 emit_insn (gen_movsi_pop (reg, preg));
374 }
375 }
376
377 emit_jump_insn (gen_returner ());
378 }
379
380 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
381
382 int
383 moxie_initial_elimination_offset (int from, int to)
384 {
385 int ret;
386
387 if ((from) == FRAME_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
388 {
389 /* Compute this since we need to use cfun->machine->local_vars_size. */
390 moxie_compute_frame ();
391 ret = -cfun->machine->callee_saved_reg_size;
392 }
393 else if ((from) == ARG_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
394 ret = 0x00;
395 else
396 abort ();
397
398 return ret;
399 }
400
401 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
402
403 static void
404 moxie_setup_incoming_varargs (cumulative_args_t cum_v,
405 machine_mode mode ATTRIBUTE_UNUSED,
406 tree type ATTRIBUTE_UNUSED,
407 int *pretend_size, int no_rtl)
408 {
409 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
410 int regno;
411 int regs = 8 - *cum;
412
413 *pretend_size = regs < 0 ? 0 : GET_MODE_SIZE (SImode) * regs;
414
415 if (no_rtl)
416 return;
417
418 for (regno = *cum; regno < 8; regno++)
419 {
420 rtx reg = gen_rtx_REG (SImode, regno);
421 rtx slot = gen_rtx_PLUS (Pmode,
422 gen_rtx_REG (SImode, ARG_POINTER_REGNUM),
423 GEN_INT (UNITS_PER_WORD * (3 + (regno-2))));
424
425 emit_move_insn (gen_rtx_MEM (SImode, slot), reg);
426 }
427 }
428
429
430 /* Return the fixed registers used for condition codes. */
431
432 static bool
433 moxie_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
434 {
435 *p1 = CC_REG;
436 *p2 = INVALID_REGNUM;
437 return true;
438 }
439
440 /* Return the next register to be used to hold a function argument or
441 NULL_RTX if there's no more space. */
442
443 static rtx
444 moxie_function_arg (cumulative_args_t cum_v, machine_mode mode,
445 const_tree type ATTRIBUTE_UNUSED,
446 bool named ATTRIBUTE_UNUSED)
447 {
448 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
449
450 if (*cum < 8)
451 return gen_rtx_REG (mode, *cum);
452 else
453 return NULL_RTX;
454 }
455
456 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
457 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
458 : (unsigned) int_size_in_bytes (TYPE))
459
460 static void
461 moxie_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
462 const_tree type, bool named ATTRIBUTE_UNUSED)
463 {
464 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
465
466 *cum = (*cum < MOXIE_R6
467 ? *cum + ((3 + MOXIE_FUNCTION_ARG_SIZE (mode, type)) / 4)
468 : *cum);
469 }
470
471 /* Return non-zero if the function argument described by TYPE is to be
472 passed by reference. */
473
474 static bool
475 moxie_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
476 machine_mode mode, const_tree type,
477 bool named ATTRIBUTE_UNUSED)
478 {
479 unsigned HOST_WIDE_INT size;
480
481 if (type)
482 {
483 if (AGGREGATE_TYPE_P (type))
484 return true;
485 size = int_size_in_bytes (type);
486 }
487 else
488 size = GET_MODE_SIZE (mode);
489
490 return size > 4*6;
491 }
492
493 /* Some function arguments will only partially fit in the registers
494 that hold arguments. Given a new arg, return the number of bytes
495 that fit in argument passing registers. */
496
497 static int
498 moxie_arg_partial_bytes (cumulative_args_t cum_v,
499 machine_mode mode,
500 tree type, bool named)
501 {
502 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
503 int bytes_left, size;
504
505 if (*cum >= 8)
506 return 0;
507
508 if (moxie_pass_by_reference (cum_v, mode, type, named))
509 size = 4;
510 else if (type)
511 {
512 if (AGGREGATE_TYPE_P (type))
513 return 0;
514 size = int_size_in_bytes (type);
515 }
516 else
517 size = GET_MODE_SIZE (mode);
518
519 bytes_left = (4 * 6) - ((*cum - 2) * 4);
520
521 if (size > bytes_left)
522 return bytes_left;
523 else
524 return 0;
525 }
526
527 /* Worker function for TARGET_STATIC_CHAIN. */
528
529 static rtx
530 moxie_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
531 {
532 rtx addr, mem;
533
534 if (incoming_p)
535 addr = plus_constant (Pmode, arg_pointer_rtx, 2 * UNITS_PER_WORD);
536 else
537 addr = plus_constant (Pmode, stack_pointer_rtx, -UNITS_PER_WORD);
538
539 mem = gen_rtx_MEM (Pmode, addr);
540 MEM_NOTRAP_P (mem) = 1;
541
542 return mem;
543 }
544
545 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
546
547 static void
548 moxie_asm_trampoline_template (FILE *f)
549 {
550 fprintf (f, "\tpush $sp, $r0\n");
551 fprintf (f, "\tldi.l $r0, 0x0\n");
552 fprintf (f, "\tsto.l 0x8($fp), $r0\n");
553 fprintf (f, "\tpop $sp, $r0\n");
554 fprintf (f, "\tjmpa 0x0\n");
555 }
556
557 /* Worker function for TARGET_TRAMPOLINE_INIT. */
558
559 static void
560 moxie_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
561 {
562 rtx mem, fnaddr = XEXP (DECL_RTL (fndecl), 0);
563
564 emit_block_move (m_tramp, assemble_trampoline_template (),
565 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
566
567 mem = adjust_address (m_tramp, SImode, 4);
568 emit_move_insn (mem, chain_value);
569 mem = adjust_address (m_tramp, SImode, 16);
570 emit_move_insn (mem, fnaddr);
571 }
572
573 /* Return true for memory offset addresses between -32768 and 32767. */
574 bool
575 moxie_offset_address_p (rtx x)
576 {
577 x = XEXP (x, 0);
578
579 if (GET_CODE (x) == PLUS)
580 {
581 x = XEXP (x, 1);
582 if (GET_CODE (x) == CONST_INT)
583 {
584 unsigned int v = INTVAL (x) & 0xFFFF8000;
585 return (v == 0xFFFF8000 || v == 0x00000000);
586 }
587 }
588 return 0;
589 }
590
591 /* Helper function for `moxie_legitimate_address_p'. */
592
593 static bool
594 moxie_reg_ok_for_base_p (const_rtx reg, bool strict_p)
595 {
596 int regno = REGNO (reg);
597
598 if (strict_p)
599 return HARD_REGNO_OK_FOR_BASE_P (regno)
600 || HARD_REGNO_OK_FOR_BASE_P (reg_renumber[regno]);
601 else
602 return !HARD_REGISTER_NUM_P (regno)
603 || HARD_REGNO_OK_FOR_BASE_P (regno);
604 }
605
606 /* Worker function for TARGET_LEGITIMATE_ADDRESS_P. */
607
608 static bool
609 moxie_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED,
610 rtx x, bool strict_p,
611 addr_space_t as)
612 {
613 gcc_assert (ADDR_SPACE_GENERIC_P (as));
614
615 if (GET_CODE(x) == PLUS
616 && REG_P (XEXP (x, 0))
617 && moxie_reg_ok_for_base_p (XEXP (x, 0), strict_p)
618 && CONST_INT_P (XEXP (x, 1))
619 && IN_RANGE (INTVAL (XEXP (x, 1)), -32768, 32767))
620 return true;
621 if (REG_P (x) && moxie_reg_ok_for_base_p (x, strict_p))
622 return true;
623 if (GET_CODE (x) == SYMBOL_REF
624 || GET_CODE (x) == LABEL_REF
625 || GET_CODE (x) == CONST)
626 return true;
627 return false;
628 }
629
630 /* The Global `targetm' Variable. */
631
632 /* Initialize the GCC target structure. */
633
634 #undef TARGET_PROMOTE_PROTOTYPES
635 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
636
637 #undef TARGET_RETURN_IN_MEMORY
638 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
639 #undef TARGET_MUST_PASS_IN_STACK
640 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
641 #undef TARGET_PASS_BY_REFERENCE
642 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
643 #undef TARGET_ARG_PARTIAL_BYTES
644 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
645 #undef TARGET_FUNCTION_ARG
646 #define TARGET_FUNCTION_ARG moxie_function_arg
647 #undef TARGET_FUNCTION_ARG_ADVANCE
648 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
649
650 #undef TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
651 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P moxie_legitimate_address_p
652
653 #undef TARGET_SETUP_INCOMING_VARARGS
654 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
655
656 #undef TARGET_FIXED_CONDITION_CODE_REGS
657 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
658
659 /* Define this to return an RTX representing the place where a
660 function returns or receives a value of data type RET_TYPE, a tree
661 node representing a data type. */
662 #undef TARGET_FUNCTION_VALUE
663 #define TARGET_FUNCTION_VALUE moxie_function_value
664 #undef TARGET_LIBCALL_VALUE
665 #define TARGET_LIBCALL_VALUE moxie_libcall_value
666 #undef TARGET_FUNCTION_VALUE_REGNO_P
667 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
668
669 #undef TARGET_FRAME_POINTER_REQUIRED
670 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
671
672 #undef TARGET_STATIC_CHAIN
673 #define TARGET_STATIC_CHAIN moxie_static_chain
674 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
675 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
676 #undef TARGET_TRAMPOLINE_INIT
677 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
678
679 #undef TARGET_OPTION_OVERRIDE
680 #define TARGET_OPTION_OVERRIDE moxie_option_override
681
682 #undef TARGET_PRINT_OPERAND
683 #define TARGET_PRINT_OPERAND moxie_print_operand
684 #undef TARGET_PRINT_OPERAND_ADDRESS
685 #define TARGET_PRINT_OPERAND_ADDRESS moxie_print_operand_address
686
687 struct gcc_target targetm = TARGET_INITIALIZER;
688
689 #include "gt-moxie.h"