gcc/
[official-gcc.git] / gcc / config / moxie / moxie.c
blob14ab63a28818b7976f184d17c270bc7989a7a370
1 /* Target Code for moxie
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 Contributed by Anthony Green.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "reload.h"
36 #include "diagnostic-core.h"
37 #include "obstack.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "tree.h"
41 #include "stor-layout.h"
42 #include "varasm.h"
43 #include "calls.h"
44 #include "function.h"
45 #include "expmed.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "emit-rtl.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "insn-codes.h"
52 #include "optabs.h"
53 #include "except.h"
54 #include "target.h"
55 #include "tm_p.h"
56 #include "langhooks.h"
57 #include "dominance.h"
58 #include "cfg.h"
59 #include "cfgrtl.h"
60 #include "cfganal.h"
61 #include "lcm.h"
62 #include "cfgbuild.h"
63 #include "cfgcleanup.h"
64 #include "predict.h"
65 #include "basic-block.h"
66 #include "df.h"
67 #include "builtins.h"
69 #include "target-def.h"
71 #define LOSE_AND_RETURN(msgid, x) \
72 do \
73 { \
74 moxie_operand_lossage (msgid, x); \
75 return; \
76 } while (0)
78 /* Worker function for TARGET_RETURN_IN_MEMORY. */
80 static bool
81 moxie_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
83 const HOST_WIDE_INT size = int_size_in_bytes (type);
84 return (size == -1 || size > 2 * UNITS_PER_WORD);
87 /* Define how to find the value returned by a function.
88 VALTYPE is the data type of the value (as a tree).
89 If the precise function being called is known, FUNC is its
90 FUNCTION_DECL; otherwise, FUNC is 0.
92 We always return values in register $r0 for moxie. */
94 static rtx
95 moxie_function_value (const_tree valtype,
96 const_tree fntype_or_decl ATTRIBUTE_UNUSED,
97 bool outgoing ATTRIBUTE_UNUSED)
99 return gen_rtx_REG (TYPE_MODE (valtype), MOXIE_R0);
102 /* Define how to find the value returned by a library function.
104 We always return values in register $r0 for moxie. */
106 static rtx
107 moxie_libcall_value (machine_mode mode,
108 const_rtx fun ATTRIBUTE_UNUSED)
110 return gen_rtx_REG (mode, MOXIE_R0);
113 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
115 We always return values in register $r0 for moxie. */
117 static bool
118 moxie_function_value_regno_p (const unsigned int regno)
120 return (regno == MOXIE_R0);
123 /* Emit an error message when we're in an asm, and a fatal error for
124 "normal" insns. Formatted output isn't easily implemented, since we
125 use output_operand_lossage to output the actual message and handle the
126 categorization of the error. */
128 static void
129 moxie_operand_lossage (const char *msgid, rtx op)
131 debug_rtx (op);
132 output_operand_lossage ("%s", msgid);
135 /* The PRINT_OPERAND_ADDRESS worker. */
137 void
138 moxie_print_operand_address (FILE *file, rtx x)
140 switch (GET_CODE (x))
142 case REG:
143 fprintf (file, "(%s)", reg_names[REGNO (x)]);
144 break;
146 case PLUS:
147 switch (GET_CODE (XEXP (x, 1)))
149 case CONST_INT:
150 fprintf (file, "%ld(%s)",
151 INTVAL(XEXP (x, 1)), reg_names[REGNO (XEXP (x, 0))]);
152 break;
153 case SYMBOL_REF:
154 output_addr_const (file, XEXP (x, 1));
155 fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
156 break;
157 case CONST:
159 rtx plus = XEXP (XEXP (x, 1), 0);
160 if (GET_CODE (XEXP (plus, 0)) == SYMBOL_REF
161 && CONST_INT_P (XEXP (plus, 1)))
163 output_addr_const(file, XEXP (plus, 0));
164 fprintf (file,"+%ld(%s)", INTVAL (XEXP (plus, 1)),
165 reg_names[REGNO (XEXP (x, 0))]);
167 else
168 abort();
170 break;
171 default:
172 abort();
174 break;
176 default:
177 output_addr_const (file, x);
178 break;
182 /* The PRINT_OPERAND worker. */
184 void
185 moxie_print_operand (FILE *file, rtx x, int code)
187 rtx operand = x;
189 /* New code entries should just be added to the switch below. If
190 handling is finished, just return. If handling was just a
191 modification of the operand, the modified operand should be put in
192 "operand", and then do a break to let default handling
193 (zero-modifier) output the operand. */
195 switch (code)
197 case 0:
198 /* No code, print as usual. */
199 break;
201 default:
202 LOSE_AND_RETURN ("invalid operand modifier letter", x);
205 /* Print an operand as without a modifier letter. */
206 switch (GET_CODE (operand))
208 case REG:
209 if (REGNO (operand) > MOXIE_R13)
210 internal_error ("internal error: bad register: %d", REGNO (operand));
211 fprintf (file, "%s", reg_names[REGNO (operand)]);
212 return;
214 case MEM:
215 output_address (XEXP (operand, 0));
216 return;
218 default:
219 /* No need to handle all strange variants, let output_addr_const
220 do it for us. */
221 if (CONSTANT_P (operand))
223 output_addr_const (file, operand);
224 return;
227 LOSE_AND_RETURN ("unexpected operand", x);
231 /* Per-function machine data. */
232 struct GTY(()) machine_function
234 /* Number of bytes saved on the stack for callee saved registers. */
235 int callee_saved_reg_size;
237 /* Number of bytes saved on the stack for local variables. */
238 int local_vars_size;
240 /* The sum of 2 sizes: locals vars and padding byte for saving the
241 * registers. Used in expand_prologue () and expand_epilogue(). */
242 int size_for_adjusting_sp;
245 /* Zero initialization is OK for all current fields. */
247 static struct machine_function *
248 moxie_init_machine_status (void)
250 return ggc_cleared_alloc<machine_function> ();
254 /* The TARGET_OPTION_OVERRIDE worker. */
255 static void
256 moxie_option_override (void)
258 /* Set the per-function-data initializer. */
259 init_machine_status = moxie_init_machine_status;
261 #ifdef TARGET_MOXIEBOX
262 target_flags |= MASK_HAS_MULX;
263 #endif
266 /* Compute the size of the local area and the size to be adjusted by the
267 * prologue and epilogue. */
269 static void
270 moxie_compute_frame (void)
272 /* For aligning the local variables. */
273 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
274 int padding_locals;
275 int regno;
277 /* Padding needed for each element of the frame. */
278 cfun->machine->local_vars_size = get_frame_size ();
280 /* Align to the stack alignment. */
281 padding_locals = cfun->machine->local_vars_size % stack_alignment;
282 if (padding_locals)
283 padding_locals = stack_alignment - padding_locals;
285 cfun->machine->local_vars_size += padding_locals;
287 cfun->machine->callee_saved_reg_size = 0;
289 /* Save callee-saved registers. */
290 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
291 if (df_regs_ever_live_p (regno) && (! call_used_regs[regno]))
292 cfun->machine->callee_saved_reg_size += 4;
294 cfun->machine->size_for_adjusting_sp =
295 crtl->args.pretend_args_size
296 + cfun->machine->local_vars_size
297 + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0);
300 void
301 moxie_expand_prologue (void)
303 int regno;
304 rtx insn;
306 moxie_compute_frame ();
308 if (flag_stack_usage_info)
309 current_function_static_stack_size = cfun->machine->size_for_adjusting_sp;
311 /* Save callee-saved registers. */
312 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
314 if (!fixed_regs[regno] && df_regs_ever_live_p (regno) && !call_used_regs[regno])
316 insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
317 RTX_FRAME_RELATED_P (insn) = 1;
321 if (cfun->machine->size_for_adjusting_sp > 0)
323 int i = cfun->machine->size_for_adjusting_sp;
324 while ((i >= 255) && (i <= 510))
326 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
327 stack_pointer_rtx,
328 GEN_INT (255)));
329 RTX_FRAME_RELATED_P (insn) = 1;
330 i -= 255;
332 if (i <= 255)
334 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
335 stack_pointer_rtx,
336 GEN_INT (i)));
337 RTX_FRAME_RELATED_P (insn) = 1;
339 else
341 rtx reg = gen_rtx_REG (SImode, MOXIE_R12);
342 insn = emit_move_insn (reg, GEN_INT (i));
343 RTX_FRAME_RELATED_P (insn) = 1;
344 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
345 stack_pointer_rtx,
346 reg));
347 RTX_FRAME_RELATED_P (insn) = 1;
352 void
353 moxie_expand_epilogue (void)
355 int regno;
356 rtx reg;
358 if (cfun->machine->callee_saved_reg_size != 0)
360 reg = gen_rtx_REG (Pmode, MOXIE_R12);
361 if (cfun->machine->callee_saved_reg_size <= 255)
363 emit_move_insn (reg, hard_frame_pointer_rtx);
364 emit_insn (gen_subsi3
365 (reg, reg,
366 GEN_INT (cfun->machine->callee_saved_reg_size)));
368 else
370 emit_move_insn (reg,
371 GEN_INT (-cfun->machine->callee_saved_reg_size));
372 emit_insn (gen_addsi3 (reg, reg, hard_frame_pointer_rtx));
374 for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
375 if (!fixed_regs[regno] && !call_used_regs[regno]
376 && df_regs_ever_live_p (regno))
378 rtx preg = gen_rtx_REG (Pmode, regno);
379 emit_insn (gen_movsi_pop (reg, preg));
383 emit_jump_insn (gen_returner ());
386 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
389 moxie_initial_elimination_offset (int from, int to)
391 int ret;
393 if ((from) == FRAME_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
395 /* Compute this since we need to use cfun->machine->local_vars_size. */
396 moxie_compute_frame ();
397 ret = -cfun->machine->callee_saved_reg_size;
399 else if ((from) == ARG_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
400 ret = 0x00;
401 else
402 abort ();
404 return ret;
407 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
409 static void
410 moxie_setup_incoming_varargs (cumulative_args_t cum_v,
411 machine_mode mode ATTRIBUTE_UNUSED,
412 tree type ATTRIBUTE_UNUSED,
413 int *pretend_size, int no_rtl)
415 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
416 int regno;
417 int regs = 8 - *cum;
419 *pretend_size = regs < 0 ? 0 : GET_MODE_SIZE (SImode) * regs;
421 if (no_rtl)
422 return;
424 for (regno = *cum; regno < 8; regno++)
426 rtx reg = gen_rtx_REG (SImode, regno);
427 rtx slot = gen_rtx_PLUS (Pmode,
428 gen_rtx_REG (SImode, ARG_POINTER_REGNUM),
429 GEN_INT (UNITS_PER_WORD * (3 + (regno-2))));
431 emit_move_insn (gen_rtx_MEM (SImode, slot), reg);
436 /* Return the fixed registers used for condition codes. */
438 static bool
439 moxie_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
441 *p1 = CC_REG;
442 *p2 = INVALID_REGNUM;
443 return true;
446 /* Return the next register to be used to hold a function argument or
447 NULL_RTX if there's no more space. */
449 static rtx
450 moxie_function_arg (cumulative_args_t cum_v, machine_mode mode,
451 const_tree type ATTRIBUTE_UNUSED,
452 bool named ATTRIBUTE_UNUSED)
454 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
456 if (*cum < 8)
457 return gen_rtx_REG (mode, *cum);
458 else
459 return NULL_RTX;
462 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
463 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
464 : (unsigned) int_size_in_bytes (TYPE))
466 static void
467 moxie_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
468 const_tree type, bool named ATTRIBUTE_UNUSED)
470 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
472 *cum = (*cum < MOXIE_R6
473 ? *cum + ((3 + MOXIE_FUNCTION_ARG_SIZE (mode, type)) / 4)
474 : *cum);
477 /* Return non-zero if the function argument described by TYPE is to be
478 passed by reference. */
480 static bool
481 moxie_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
482 machine_mode mode, const_tree type,
483 bool named ATTRIBUTE_UNUSED)
485 unsigned HOST_WIDE_INT size;
487 if (type)
489 if (AGGREGATE_TYPE_P (type))
490 return true;
491 size = int_size_in_bytes (type);
493 else
494 size = GET_MODE_SIZE (mode);
496 return size > 4*6;
499 /* Some function arguments will only partially fit in the registers
500 that hold arguments. Given a new arg, return the number of bytes
501 that fit in argument passing registers. */
503 static int
504 moxie_arg_partial_bytes (cumulative_args_t cum_v,
505 machine_mode mode,
506 tree type, bool named)
508 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
509 int bytes_left, size;
511 if (*cum >= 8)
512 return 0;
514 if (moxie_pass_by_reference (cum_v, mode, type, named))
515 size = 4;
516 else if (type)
518 if (AGGREGATE_TYPE_P (type))
519 return 0;
520 size = int_size_in_bytes (type);
522 else
523 size = GET_MODE_SIZE (mode);
525 bytes_left = (4 * 6) - ((*cum - 2) * 4);
527 if (size > bytes_left)
528 return bytes_left;
529 else
530 return 0;
533 /* Worker function for TARGET_STATIC_CHAIN. */
535 static rtx
536 moxie_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
538 rtx addr, mem;
540 if (incoming_p)
541 addr = plus_constant (Pmode, arg_pointer_rtx, 2 * UNITS_PER_WORD);
542 else
543 addr = plus_constant (Pmode, stack_pointer_rtx, -UNITS_PER_WORD);
545 mem = gen_rtx_MEM (Pmode, addr);
546 MEM_NOTRAP_P (mem) = 1;
548 return mem;
551 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
553 static void
554 moxie_asm_trampoline_template (FILE *f)
556 fprintf (f, "\tpush $sp, $r0\n");
557 fprintf (f, "\tldi.l $r0, 0x0\n");
558 fprintf (f, "\tsto.l 0x8($fp), $r0\n");
559 fprintf (f, "\tpop $sp, $r0\n");
560 fprintf (f, "\tjmpa 0x0\n");
563 /* Worker function for TARGET_TRAMPOLINE_INIT. */
565 static void
566 moxie_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
568 rtx mem, fnaddr = XEXP (DECL_RTL (fndecl), 0);
570 emit_block_move (m_tramp, assemble_trampoline_template (),
571 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
573 mem = adjust_address (m_tramp, SImode, 4);
574 emit_move_insn (mem, chain_value);
575 mem = adjust_address (m_tramp, SImode, 16);
576 emit_move_insn (mem, fnaddr);
579 /* Return true for memory offset addresses between -32768 and 32767. */
580 bool
581 moxie_offset_address_p (rtx x)
583 x = XEXP (x, 0);
585 if (GET_CODE (x) == PLUS)
587 x = XEXP (x, 1);
588 if (GET_CODE (x) == CONST_INT)
590 unsigned int v = INTVAL (x) & 0xFFFF8000;
591 return (v == 0xFFFF8000 || v == 0x00000000);
594 return 0;
597 /* The Global `targetm' Variable. */
599 /* Initialize the GCC target structure. */
601 #undef TARGET_PROMOTE_PROTOTYPES
602 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
604 #undef TARGET_RETURN_IN_MEMORY
605 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
606 #undef TARGET_MUST_PASS_IN_STACK
607 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
608 #undef TARGET_PASS_BY_REFERENCE
609 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
610 #undef TARGET_ARG_PARTIAL_BYTES
611 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
612 #undef TARGET_FUNCTION_ARG
613 #define TARGET_FUNCTION_ARG moxie_function_arg
614 #undef TARGET_FUNCTION_ARG_ADVANCE
615 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
618 #undef TARGET_SETUP_INCOMING_VARARGS
619 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
621 #undef TARGET_FIXED_CONDITION_CODE_REGS
622 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
624 /* Define this to return an RTX representing the place where a
625 function returns or receives a value of data type RET_TYPE, a tree
626 node node representing a data type. */
627 #undef TARGET_FUNCTION_VALUE
628 #define TARGET_FUNCTION_VALUE moxie_function_value
629 #undef TARGET_LIBCALL_VALUE
630 #define TARGET_LIBCALL_VALUE moxie_libcall_value
631 #undef TARGET_FUNCTION_VALUE_REGNO_P
632 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
634 #undef TARGET_FRAME_POINTER_REQUIRED
635 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
637 #undef TARGET_STATIC_CHAIN
638 #define TARGET_STATIC_CHAIN moxie_static_chain
639 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
640 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
641 #undef TARGET_TRAMPOLINE_INIT
642 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
644 #undef TARGET_OPTION_OVERRIDE
645 #define TARGET_OPTION_OVERRIDE moxie_option_override
647 struct gcc_target targetm = TARGET_INITIALIZER;
649 #include "gt-moxie.h"