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