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