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