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