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