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