]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/mn10300/mn10300.c
Makefile.in (check-gcc-c++): Renamed from check-c++.
[thirdparty/gcc.git] / gcc / config / mn10300 / mn10300.c
CommitLineData
11bb1f11 1/* Subroutines for insn-output.c for Matsushita MN10300 series
92547f12
AO
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
11bb1f11
JL
4 Contributed by Jeff Law (law@cygnus.com).
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
22
11bb1f11 23#include "config.h"
c5c76735 24#include "system.h"
11bb1f11 25#include "rtl.h"
bf6bb899 26#include "tree.h"
11bb1f11
JL
27#include "regs.h"
28#include "hard-reg-set.h"
29#include "real.h"
30#include "insn-config.h"
31#include "conditions.h"
11bb1f11
JL
32#include "output.h"
33#include "insn-attr.h"
34#include "flags.h"
35#include "recog.h"
36#include "expr.h"
e78d8e51 37#include "optabs.h"
bf6bb899 38#include "function.h"
11bb1f11 39#include "obstack.h"
69bc71fa
KG
40#include "toplev.h"
41#include "tm_p.h"
672a6f42
NB
42#include "target.h"
43#include "target-def.h"
11bb1f11 44
4d1a91c2
JL
45/* The size of the callee register save area. Right now we save everything
46 on entry since it costs us nothing in code size. It does cost us from a
47 speed standpoint, so we want to optimize this sooner or later. */
980d0e81
JL
48#define REG_SAVE_BYTES (4 * regs_ever_live[2] \
49 + 4 * regs_ever_live[3] \
705ac34f
JL
50 + 4 * regs_ever_live[6] \
51 + 4 * regs_ever_live[7] \
52 + 16 * (regs_ever_live[14] || regs_ever_live[15] \
53 || regs_ever_live[16] || regs_ever_live[17]))
672a6f42
NB
54\f
55/* Initialize the GCC target structure. */
301d03af
RS
56#undef TARGET_ASM_ALIGNED_HI_OP
57#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
4d1a91c2 58
f6897b10 59struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 60\f
11bb1f11
JL
61void
62asm_file_start (file)
63 FILE *file;
64{
65 fprintf (file, "#\tGCC For the Matsushita MN10300\n");
66 if (optimize)
67 fprintf (file, "# -O%d\n", optimize);
68 else
69 fprintf (file, "\n\n");
705ac34f
JL
70
71 if (TARGET_AM33)
72 fprintf (file, "\t.am33\n");
11bb1f11
JL
73 output_file_directive (file, main_input_filename);
74}
75\f
76
11bb1f11
JL
77/* Print operand X using operand code CODE to assembly language output file
78 FILE. */
79
80void
81print_operand (file, x, code)
82 FILE *file;
83 rtx x;
84 int code;
85{
86 switch (code)
87 {
88 case 'b':
89 case 'B':
90 /* These are normal and reversed branches. */
91 switch (code == 'b' ? GET_CODE (x) : reverse_condition (GET_CODE (x)))
92 {
93 case NE:
94 fprintf (file, "ne");
95 break;
96 case EQ:
97 fprintf (file, "eq");
98 break;
99 case GE:
100 fprintf (file, "ge");
101 break;
102 case GT:
103 fprintf (file, "gt");
104 break;
105 case LE:
106 fprintf (file, "le");
107 break;
108 case LT:
109 fprintf (file, "lt");
110 break;
111 case GEU:
112 fprintf (file, "cc");
113 break;
114 case GTU:
115 fprintf (file, "hi");
116 break;
117 case LEU:
118 fprintf (file, "ls");
119 break;
120 case LTU:
121 fprintf (file, "cs");
122 break;
123 default:
124 abort ();
125 }
126 break;
127 case 'C':
128 /* This is used for the operand to a call instruction;
129 if it's a REG, enclose it in parens, else output
130 the operand normally. */
131 if (GET_CODE (x) == REG)
132 {
133 fputc ('(', file);
134 print_operand (file, x, 0);
135 fputc (')', file);
136 }
137 else
138 print_operand (file, x, 0);
139 break;
140
38c37a0e
JL
141 /* These are the least significant word in a 64bit value. */
142 case 'L':
143 switch (GET_CODE (x))
144 {
145 case MEM:
146 fputc ('(', file);
147 output_address (XEXP (x, 0));
148 fputc (')', file);
149 break;
150
151 case REG:
152 fprintf (file, "%s", reg_names[REGNO (x)]);
153 break;
154
155 case SUBREG:
ddef6bc7 156 fprintf (file, "%s", reg_names[subreg_regno (x)]);
38c37a0e
JL
157 break;
158
159 case CONST_DOUBLE:
160 {
161 long val[2];
162 REAL_VALUE_TYPE rv;
163
164 switch (GET_MODE (x))
165 {
166 case DFmode:
167 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
168 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
cf31fafa 169 fprintf (file, "0x%lx", val[0]);
38c37a0e
JL
170 break;;
171 case SFmode:
172 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
173 REAL_VALUE_TO_TARGET_SINGLE (rv, val[0]);
cf31fafa 174 fprintf (file, "0x%lx", val[0]);
38c37a0e
JL
175 break;;
176 case VOIDmode:
177 case DImode:
178 print_operand_address (file,
179 GEN_INT (CONST_DOUBLE_LOW (x)));
180 break;
69bc71fa
KG
181 default:
182 break;
38c37a0e
JL
183 }
184 break;
185 }
186
187 case CONST_INT:
212bc5fa
AO
188 {
189 rtx low, high;
190 split_double (x, &low, &high);
191 fprintf (file, "%ld", (long)INTVAL (low));
192 break;
193 }
38c37a0e
JL
194
195 default:
196 abort ();
197 }
198 break;
199
200 /* Similarly, but for the most significant word. */
201 case 'H':
202 switch (GET_CODE (x))
203 {
204 case MEM:
205 fputc ('(', file);
b72f00af 206 x = adjust_address (x, SImode, 4);
38c37a0e
JL
207 output_address (XEXP (x, 0));
208 fputc (')', file);
209 break;
210
211 case REG:
212 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
213 break;
214
215 case SUBREG:
ddef6bc7 216 fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
38c37a0e
JL
217 break;
218
219 case CONST_DOUBLE:
220 {
221 long val[2];
222 REAL_VALUE_TYPE rv;
223
224 switch (GET_MODE (x))
225 {
226 case DFmode:
227 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
228 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
cf31fafa 229 fprintf (file, "0x%lx", val[1]);
38c37a0e
JL
230 break;;
231 case SFmode:
232 abort ();
233 case VOIDmode:
234 case DImode:
235 print_operand_address (file,
236 GEN_INT (CONST_DOUBLE_HIGH (x)));
237 break;
69bc71fa
KG
238 default:
239 break;
38c37a0e
JL
240 }
241 break;
242 }
243
244 case CONST_INT:
212bc5fa
AO
245 {
246 rtx low, high;
247 split_double (x, &low, &high);
248 fprintf (file, "%ld", (long)INTVAL (high));
249 break;
250 }
251
38c37a0e
JL
252 default:
253 abort ();
254 }
255 break;
256
257 case 'A':
258 fputc ('(', file);
259 if (GET_CODE (XEXP (x, 0)) == REG)
c5c76735 260 output_address (gen_rtx_PLUS (SImode, XEXP (x, 0), GEN_INT (0)));
38c37a0e
JL
261 else
262 output_address (XEXP (x, 0));
263 fputc (')', file);
264 break;
265
6fafc523
JL
266 case 'N':
267 output_address (GEN_INT ((~INTVAL (x)) & 0xff));
268 break;
269
576e5acc
JL
270 /* For shift counts. The hardware ignores the upper bits of
271 any immediate, but the assembler will flag an out of range
272 shift count as an error. So we mask off the high bits
273 of the immediate here. */
274 case 'S':
275 if (GET_CODE (x) == CONST_INT)
276 {
277 fprintf (file, "%d", INTVAL (x) & 0x1f);
278 break;
279 }
280 /* FALL THROUGH */
281
11bb1f11
JL
282 default:
283 switch (GET_CODE (x))
284 {
285 case MEM:
286 fputc ('(', file);
287 output_address (XEXP (x, 0));
288 fputc (')', file);
289 break;
290
38c37a0e
JL
291 case PLUS:
292 output_address (x);
293 break;
294
11bb1f11
JL
295 case REG:
296 fprintf (file, "%s", reg_names[REGNO (x)]);
297 break;
298
299 case SUBREG:
ddef6bc7 300 fprintf (file, "%s", reg_names[subreg_regno (x)]);
11bb1f11
JL
301 break;
302
38c37a0e
JL
303 /* This will only be single precision.... */
304 case CONST_DOUBLE:
305 {
306 unsigned long val;
307 REAL_VALUE_TYPE rv;
308
309 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
310 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
cf31fafa 311 fprintf (file, "0x%lx", val);
38c37a0e
JL
312 break;
313 }
314
11bb1f11
JL
315 case CONST_INT:
316 case SYMBOL_REF:
317 case CONST:
318 case LABEL_REF:
319 case CODE_LABEL:
320 print_operand_address (file, x);
321 break;
322 default:
323 abort ();
324 }
325 break;
326 }
327}
328
329/* Output assembly language output for the address ADDR to FILE. */
330
331void
332print_operand_address (file, addr)
333 FILE *file;
334 rtx addr;
335{
336 switch (GET_CODE (addr))
337 {
705ac34f
JL
338 case POST_INC:
339 print_operand_address (file, XEXP (addr, 0));
340 fputc ('+', file);
341 break;
11bb1f11 342 case REG:
79e4122c 343 print_operand (file, addr, 0);
11bb1f11
JL
344 break;
345 case PLUS:
346 {
347 rtx base, index;
348 if (REG_P (XEXP (addr, 0))
349 && REG_OK_FOR_BASE_P (XEXP (addr, 0)))
350 base = XEXP (addr, 0), index = XEXP (addr, 1);
351 else if (REG_P (XEXP (addr, 1))
352 && REG_OK_FOR_BASE_P (XEXP (addr, 1)))
353 base = XEXP (addr, 1), index = XEXP (addr, 0);
354 else
355 abort ();
356 print_operand (file, index, 0);
357 fputc (',', file);
358 print_operand (file, base, 0);;
359 break;
360 }
361 case SYMBOL_REF:
362 output_addr_const (file, addr);
363 break;
364 default:
365 output_addr_const (file, addr);
366 break;
367 }
368}
369
f6cd7c62
RS
370/* Print a set of registers in the format required by "movm" and "ret".
371 Register K is saved if bit K of MASK is set. The data and address
372 registers can be stored individually, but the extended registers cannot.
373 We assume that the mask alread takes that into account. For instance,
374 bits 14 to 17 must have the same value. */
375
376void
377mn10300_print_reg_list (file, mask)
378 FILE *file;
379 int mask;
380{
381 int need_comma;
382 int i;
383
384 need_comma = 0;
385 fputc ('[', file);
386
387 for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
388 if ((mask & (1 << i)) != 0)
389 {
390 if (need_comma)
391 fputc (',', file);
392 fputs (reg_names [i], file);
393 need_comma = 1;
394 }
395
396 if ((mask & 0x3c000) != 0)
397 {
398 if ((mask & 0x3c000) != 0x3c000)
399 abort();
400 if (need_comma)
401 fputc (',', file);
402 fputs ("exreg1", file);
403 need_comma = 1;
404 }
405
406 fputc (']', file);
407}
408
38c37a0e
JL
409int
410can_use_return_insn ()
411{
460f4b9d
JL
412 /* size includes the fixed stack space needed for function calls. */
413 int size = get_frame_size () + current_function_outgoing_args_size;
414
415 /* And space for the return pointer. */
416 size += current_function_outgoing_args_size ? 4 : 0;
38c37a0e
JL
417
418 return (reload_completed
419 && size == 0
420 && !regs_ever_live[2]
421 && !regs_ever_live[3]
422 && !regs_ever_live[6]
423 && !regs_ever_live[7]
705ac34f
JL
424 && !regs_ever_live[14]
425 && !regs_ever_live[15]
426 && !regs_ever_live[16]
427 && !regs_ever_live[17]
38c37a0e
JL
428 && !frame_pointer_needed);
429}
430
f6cd7c62
RS
431/* Returns the set of live, callee-saved registers as a bitmask. The
432 callee-saved extended registers cannot be stored individually, so
433 all of them will be included in the mask if any one of them is used. */
434
435int
436mn10300_get_live_callee_saved_regs ()
437{
438 int mask;
439 int i;
440
441 mask = 0;
442 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
443 if (regs_ever_live[i] && ! call_used_regs[i])
444 mask |= (1 << i);
445 if ((mask & 0x3c000) != 0)
446 mask |= 0x3c000;
447
448 return mask;
449}
450
451/* Generate an instruction that pushes several registers onto the stack.
452 Register K will be saved if bit K in MASK is set. The function does
453 nothing if MASK is zero.
454
455 To be compatible with the "movm" instruction, the lowest-numbered
456 register must be stored in the lowest slot. If MASK is the set
457 { R1,...,RN }, where R1...RN are ordered least first, the generated
458 instruction will have the form:
459
460 (parallel
461 (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
462 (set (mem:SI (plus:SI (reg:SI 9)
463 (const_int -1*4)))
464 (reg:SI RN))
465 ...
466 (set (mem:SI (plus:SI (reg:SI 9)
467 (const_int -N*4)))
468 (reg:SI R1))) */
469
470void
471mn10300_gen_multiple_store (mask)
472 int mask;
473{
474 if (mask != 0)
475 {
476 int i;
477 int count;
478 rtx par;
479 int pari;
480
481 /* Count how many registers need to be saved. */
482 count = 0;
483 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
484 if ((mask & (1 << i)) != 0)
485 count += 1;
486
487 /* We need one PARALLEL element to update the stack pointer and
488 an additional element for each register that is stored. */
489 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count + 1));
490
491 /* Create the instruction that updates the stack pointer. */
492 XVECEXP (par, 0, 0)
493 = gen_rtx_SET (SImode,
494 stack_pointer_rtx,
495 gen_rtx_PLUS (SImode,
496 stack_pointer_rtx,
497 GEN_INT (-count * 4)));
498
499 /* Create each store. */
500 pari = 1;
501 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
502 if ((mask & (1 << i)) != 0)
503 {
504 rtx address = gen_rtx_PLUS (SImode,
505 stack_pointer_rtx,
506 GEN_INT (-pari * 4));
507 XVECEXP(par, 0, pari)
508 = gen_rtx_SET (VOIDmode,
509 gen_rtx_MEM (SImode, address),
510 gen_rtx_REG (SImode, i));
511 pari += 1;
512 }
513
514 par = emit_insn (par);
515 RTX_FRAME_RELATED_P (par) = 1;
516 }
517}
518
11bb1f11
JL
519void
520expand_prologue ()
521{
036182e7 522 HOST_WIDE_INT size;
3dbc43d1 523
3dbc43d1 524 /* SIZE includes the fixed stack space needed for function calls. */
460f4b9d
JL
525 size = get_frame_size () + current_function_outgoing_args_size;
526 size += (current_function_outgoing_args_size ? 4 : 0);
11bb1f11 527
f6cd7c62
RS
528 /* If we use any of the callee-saved registers, save them now. */
529 mn10300_gen_multiple_store (mn10300_get_live_callee_saved_regs ());
777fbf09
JL
530
531 /* Now put the frame pointer into the frame pointer register. */
11bb1f11 532 if (frame_pointer_needed)
6e86170d 533 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
11bb1f11 534
777fbf09 535 /* Allocate stack for this frame. */
11bb1f11
JL
536 if (size)
537 emit_insn (gen_addsi3 (stack_pointer_rtx,
538 stack_pointer_rtx,
539 GEN_INT (-size)));
540}
541
542void
543expand_epilogue ()
544{
036182e7 545 HOST_WIDE_INT size;
3dbc43d1 546
3dbc43d1 547 /* SIZE includes the fixed stack space needed for function calls. */
460f4b9d
JL
548 size = get_frame_size () + current_function_outgoing_args_size;
549 size += (current_function_outgoing_args_size ? 4 : 0);
11bb1f11 550
5d29a95f
JL
551 /* Maybe cut back the stack, except for the register save area.
552
553 If the frame pointer exists, then use the frame pointer to
554 cut back the stack.
555
556 If the stack size + register save area is more than 255 bytes,
557 then the stack must be cut back here since the size + register
558 save size is too big for a ret/retf instruction.
559
560 Else leave it alone, it will be cut back as part of the
561 ret/retf instruction, or there wasn't any stack to begin with.
562
563 Under no circumstanes should the register save area be
564 deallocated here, that would leave a window where an interrupt
565 could occur and trash the register save area. */
11bb1f11
JL
566 if (frame_pointer_needed)
567 {
11bb1f11 568 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
4246e0c5
JL
569 size = 0;
570 }
ed6089d6 571 else if (size + REG_SAVE_BYTES > 255)
4246e0c5
JL
572 {
573 emit_insn (gen_addsi3 (stack_pointer_rtx,
574 stack_pointer_rtx,
575 GEN_INT (size)));
576 size = 0;
11bb1f11 577 }
11bb1f11 578
ed6089d6
AO
579 /* Adjust the stack and restore callee-saved registers, if any. */
580 if (size || regs_ever_live[2] || regs_ever_live[3]
777fbf09 581 || regs_ever_live[6] || regs_ever_live[7]
705ac34f
JL
582 || regs_ever_live[14] || regs_ever_live[15]
583 || regs_ever_live[16] || regs_ever_live[17]
777fbf09 584 || frame_pointer_needed)
ed6089d6
AO
585 emit_jump_insn (gen_return_internal_regs
586 (GEN_INT (size + REG_SAVE_BYTES)));
777fbf09 587 else
ed6089d6 588 emit_jump_insn (gen_return_internal ());
11bb1f11
JL
589}
590
591/* Update the condition code from the insn. */
592
593void
594notice_update_cc (body, insn)
595 rtx body;
596 rtx insn;
597{
11bb1f11
JL
598 switch (get_attr_cc (insn))
599 {
600 case CC_NONE:
601 /* Insn does not affect CC at all. */
602 break;
603
604 case CC_NONE_0HIT:
605 /* Insn does not change CC, but the 0'th operand has been changed. */
606 if (cc_status.value1 != 0
1ccbefce 607 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
11bb1f11
JL
608 cc_status.value1 = 0;
609 break;
610
d116300b 611 case CC_SET_ZN:
1ccbefce 612 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
d116300b 613 V,C are unusable. */
11bb1f11 614 CC_STATUS_INIT;
d116300b 615 cc_status.flags |= CC_NO_CARRY | CC_OVERFLOW_UNUSABLE;
1ccbefce 616 cc_status.value1 = recog_data.operand[0];
11bb1f11
JL
617 break;
618
d116300b 619 case CC_SET_ZNV:
1ccbefce 620 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
d116300b 621 C is unusable. */
82c6faa8 622 CC_STATUS_INIT;
d116300b 623 cc_status.flags |= CC_NO_CARRY;
1ccbefce 624 cc_status.value1 = recog_data.operand[0];
82c6faa8
JL
625 break;
626
11bb1f11
JL
627 case CC_COMPARE:
628 /* The insn is a compare instruction. */
629 CC_STATUS_INIT;
630 cc_status.value1 = SET_SRC (body);
631 break;
632
3b800f71
JL
633 case CC_INVERT:
634 /* The insn is a compare instruction. */
635 CC_STATUS_INIT;
636 cc_status.value1 = SET_SRC (body);
637 cc_status.flags |= CC_INVERTED;
638 break;
639
11bb1f11
JL
640 case CC_CLOBBER:
641 /* Insn doesn't leave CC in a usable state. */
642 CC_STATUS_INIT;
643 break;
82c6faa8
JL
644
645 default:
646 abort ();
11bb1f11 647 }
11bb1f11
JL
648}
649
05713b80 650/* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
f6cd7c62
RS
651 This function is for MATCH_PARALLEL and so assumes OP is known to be
652 parallel. If OP is a multiple store, return a mask indicating which
653 registers it saves. Return 0 otherwise. */
654
655int
656store_multiple_operation (op, mode)
657 rtx op;
658 enum machine_mode mode ATTRIBUTE_UNUSED;
659{
660 int count;
661 int mask;
662 int i;
663 unsigned int last;
664 rtx elt;
665
666 count = XVECLEN (op, 0);
667 if (count < 2)
668 return 0;
669
670 /* Check that first instruction has the form (set (sp) (plus A B)) */
671 elt = XVECEXP (op, 0, 0);
672 if (GET_CODE (elt) != SET
673 || GET_CODE (SET_DEST (elt)) != REG
674 || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
675 || GET_CODE (SET_SRC (elt)) != PLUS)
676 return 0;
677
678 /* Check that A is the stack pointer and B is the expected stack size.
679 For OP to match, each subsequent instruction should push a word onto
680 the stack. We therefore expect the first instruction to create
681 COUNT-1 stack slots. */
682 elt = SET_SRC (elt);
683 if (GET_CODE (XEXP (elt, 0)) != REG
684 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
685 || GET_CODE (XEXP (elt, 1)) != CONST_INT
686 || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
687 return 0;
688
689 /* Now go through the rest of the vector elements. They must be
690 ordered so that the first instruction stores the highest-numbered
691 register to the highest stack slot and that subsequent instructions
692 store a lower-numbered register to the slot below.
693
694 LAST keeps track of the smallest-numbered register stored so far.
695 MASK is the set of stored registers. */
696 last = FIRST_PSEUDO_REGISTER;
697 mask = 0;
698 for (i = 1; i < count; i++)
699 {
700 /* Check that element i is a (set (mem M) R) and that R is valid. */
701 elt = XVECEXP (op, 0, i);
702 if (GET_CODE (elt) != SET
703 || GET_CODE (SET_DEST (elt)) != MEM
704 || GET_CODE (SET_SRC (elt)) != REG
705 || REGNO (SET_SRC (elt)) >= last)
706 return 0;
707
708 /* R was OK, so provisionally add it to MASK. We return 0 in any
709 case if the rest of the instruction has a flaw. */
710 last = REGNO (SET_SRC (elt));
711 mask |= (1 << last);
712
713 /* Check that M has the form (plus (sp) (const_int -I*4)) */
714 elt = XEXP (SET_DEST (elt), 0);
715 if (GET_CODE (elt) != PLUS
716 || GET_CODE (XEXP (elt, 0)) != REG
717 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
718 || GET_CODE (XEXP (elt, 1)) != CONST_INT
719 || INTVAL (XEXP (elt, 1)) != -i * 4)
720 return 0;
721 }
722
723 /* All or none of the callee-saved extended registers must be in the set. */
724 if ((mask & 0x3c000) != 0
725 && (mask & 0x3c000) != 0x3c000)
726 return 0;
727
728 return mask;
729}
730
11bb1f11
JL
731/* Return true if OP is a valid call operand. */
732
733int
734call_address_operand (op, mode)
735 rtx op;
69bc71fa 736 enum machine_mode mode ATTRIBUTE_UNUSED;
11bb1f11
JL
737{
738 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
739}
740
741/* What (if any) secondary registers are needed to move IN with mode
8493cfcb 742 MODE into a register in register class CLASS.
11bb1f11
JL
743
744 We might be able to simplify this. */
745enum reg_class
746secondary_reload_class (class, mode, in)
747 enum reg_class class;
748 enum machine_mode mode;
749 rtx in;
750{
11bb1f11
JL
751 /* Memory loads less than a full word wide can't have an
752 address or stack pointer destination. They must use
753 a data register as an intermediate register. */
15c09738
AH
754 if ((GET_CODE (in) == MEM
755 || (GET_CODE (in) == REG
756 && REGNO (in) >= FIRST_PSEUDO_REGISTER)
757 || (GET_CODE (in) == SUBREG
758 && GET_CODE (SUBREG_REG (in)) == REG
759 && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER))
11bb1f11 760 && (mode == QImode || mode == HImode)
15c09738 761 && (class == ADDRESS_REGS || class == SP_REGS
842a0788 762 || class == SP_OR_ADDRESS_REGS))
4d1a91c2 763 {
705ac34f
JL
764 if (TARGET_AM33)
765 return DATA_OR_EXTENDED_REGS;
4d1a91c2
JL
766 return DATA_REGS;
767 }
11bb1f11
JL
768
769 /* We can't directly load sp + const_int into a data register;
770 we must use an address register as an intermediate. */
777fbf09
JL
771 if (class != SP_REGS
772 && class != ADDRESS_REGS
773 && class != SP_OR_ADDRESS_REGS
705ac34f
JL
774 && class != SP_OR_EXTENDED_REGS
775 && class != ADDRESS_OR_EXTENDED_REGS
776 && class != SP_OR_ADDRESS_OR_EXTENDED_REGS
11bb1f11
JL
777 && (in == stack_pointer_rtx
778 || (GET_CODE (in) == PLUS
777fbf09
JL
779 && (XEXP (in, 0) == stack_pointer_rtx
780 || XEXP (in, 1) == stack_pointer_rtx))))
11bb1f11
JL
781 return ADDRESS_REGS;
782
4c742813
JL
783 if (GET_CODE (in) == PLUS
784 && (XEXP (in, 0) == stack_pointer_rtx
785 || XEXP (in, 1) == stack_pointer_rtx))
4d1a91c2 786 {
705ac34f
JL
787 if (TARGET_AM33)
788 return DATA_OR_EXTENDED_REGS;
4d1a91c2
JL
789 return DATA_REGS;
790 }
4c742813 791
777fbf09
JL
792 /* Otherwise assume no secondary reloads are needed. */
793 return NO_REGS;
794}
795
796int
797initial_offset (from, to)
798 int from, to;
799{
3dbc43d1
JL
800 /* The difference between the argument pointer and the frame pointer
801 is the size of the callee register save area. */
777fbf09 802 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
11bb1f11 803 {
777fbf09
JL
804 if (regs_ever_live[2] || regs_ever_live[3]
805 || regs_ever_live[6] || regs_ever_live[7]
705ac34f
JL
806 || regs_ever_live[14] || regs_ever_live[15]
807 || regs_ever_live[16] || regs_ever_live[17]
777fbf09 808 || frame_pointer_needed)
4d1a91c2 809 return REG_SAVE_BYTES;
777fbf09 810 else
22ef4e9b 811 return 0;
11bb1f11
JL
812 }
813
3dbc43d1
JL
814 /* The difference between the argument pointer and the stack pointer is
815 the sum of the size of this function's frame, the callee register save
816 area, and the fixed stack space needed for function calls (if any). */
777fbf09
JL
817 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
818 {
819 if (regs_ever_live[2] || regs_ever_live[3]
820 || regs_ever_live[6] || regs_ever_live[7]
705ac34f
JL
821 || regs_ever_live[14] || regs_ever_live[15]
822 || regs_ever_live[16] || regs_ever_live[17]
777fbf09 823 || frame_pointer_needed)
4d1a91c2 824 return (get_frame_size () + REG_SAVE_BYTES
460f4b9d
JL
825 + (current_function_outgoing_args_size
826 ? current_function_outgoing_args_size + 4 : 0));
777fbf09 827 else
460f4b9d
JL
828 return (get_frame_size ()
829 + (current_function_outgoing_args_size
830 ? current_function_outgoing_args_size + 4 : 0));
777fbf09 831 }
11bb1f11 832
3dbc43d1
JL
833 /* The difference between the frame pointer and stack pointer is the sum
834 of the size of this function's frame and the fixed stack space needed
835 for function calls (if any). */
777fbf09 836 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
460f4b9d
JL
837 return (get_frame_size ()
838 + (current_function_outgoing_args_size
839 ? current_function_outgoing_args_size + 4 : 0));
777fbf09
JL
840
841 abort ();
11bb1f11 842}
22ef4e9b
JL
843
844/* Flush the argument registers to the stack for a stdarg function;
845 return the new argument pointer. */
846rtx
648d2ffc 847mn10300_builtin_saveregs ()
22ef4e9b 848{
fc2acc87 849 rtx offset, mem;
22ef4e9b
JL
850 tree fntype = TREE_TYPE (current_function_decl);
851 int argadj = ((!(TYPE_ARG_TYPES (fntype) != 0
852 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
853 != void_type_node)))
854 ? UNITS_PER_WORD : 0);
fc2acc87 855 int set = get_varargs_alias_set ();
22ef4e9b
JL
856
857 if (argadj)
858 offset = plus_constant (current_function_arg_offset_rtx, argadj);
859 else
860 offset = current_function_arg_offset_rtx;
861
fc2acc87 862 mem = gen_rtx_MEM (SImode, current_function_internal_arg_pointer);
ba4828e0 863 set_mem_alias_set (mem, set);
fc2acc87
RH
864 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
865
866 mem = gen_rtx_MEM (SImode,
867 plus_constant (current_function_internal_arg_pointer, 4));
ba4828e0 868 set_mem_alias_set (mem, set);
fc2acc87
RH
869 emit_move_insn (mem, gen_rtx_REG (SImode, 1));
870
22ef4e9b
JL
871 return copy_to_reg (expand_binop (Pmode, add_optab,
872 current_function_internal_arg_pointer,
873 offset, 0, 0, OPTAB_LIB_WIDEN));
874}
875
fc2acc87 876void
e5faf155 877mn10300_va_start (valist, nextarg)
fc2acc87
RH
878 tree valist;
879 rtx nextarg;
880{
6c535c69 881 nextarg = expand_builtin_saveregs ();
e5faf155 882 std_expand_builtin_va_start (valist, nextarg);
fc2acc87
RH
883}
884
885rtx
886mn10300_va_arg (valist, type)
887 tree valist, type;
888{
889 HOST_WIDE_INT align, rsize;
890 tree t, ptr, pptr;
891
892 /* Compute the rounded size of the type. */
893 align = PARM_BOUNDARY / BITS_PER_UNIT;
894 rsize = (((int_size_in_bytes (type) + align - 1) / align) * align);
895
896 t = build (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist,
897 build_int_2 ((rsize > 8 ? 4 : rsize), 0));
898 TREE_SIDE_EFFECTS (t) = 1;
899
900 ptr = build_pointer_type (type);
901
902 /* "Large" types are passed by reference. */
903 if (rsize > 8)
904 {
905 pptr = build_pointer_type (ptr);
906 t = build1 (NOP_EXPR, pptr, t);
907 TREE_SIDE_EFFECTS (t) = 1;
908
909 t = build1 (INDIRECT_REF, ptr, t);
910 TREE_SIDE_EFFECTS (t) = 1;
911 }
912 else
913 {
914 t = build1 (NOP_EXPR, ptr, t);
915 TREE_SIDE_EFFECTS (t) = 1;
916 }
917
918 /* Calculate! */
919 return expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
920}
921
22ef4e9b
JL
922/* Return an RTX to represent where a value with mode MODE will be returned
923 from a function. If the result is 0, the argument is pushed. */
924
925rtx
926function_arg (cum, mode, type, named)
927 CUMULATIVE_ARGS *cum;
928 enum machine_mode mode;
929 tree type;
69bc71fa 930 int named ATTRIBUTE_UNUSED;
22ef4e9b
JL
931{
932 rtx result = 0;
933 int size, align;
934
935 /* We only support using 2 data registers as argument registers. */
936 int nregs = 2;
937
938 /* Figure out the size of the object to be passed. */
939 if (mode == BLKmode)
940 size = int_size_in_bytes (type);
941 else
942 size = GET_MODE_SIZE (mode);
943
944 /* Figure out the alignment of the object to be passed. */
945 align = size;
946
947 cum->nbytes = (cum->nbytes + 3) & ~3;
948
949 /* Don't pass this arg via a register if all the argument registers
950 are used up. */
951 if (cum->nbytes > nregs * UNITS_PER_WORD)
952 return 0;
953
954 /* Don't pass this arg via a register if it would be split between
955 registers and memory. */
956 if (type == NULL_TREE
957 && cum->nbytes + size > nregs * UNITS_PER_WORD)
958 return 0;
959
960 switch (cum->nbytes / UNITS_PER_WORD)
961 {
962 case 0:
c5c76735 963 result = gen_rtx_REG (mode, 0);
22ef4e9b
JL
964 break;
965 case 1:
c5c76735 966 result = gen_rtx_REG (mode, 1);
22ef4e9b
JL
967 break;
968 default:
969 result = 0;
970 }
971
972 return result;
973}
974
975/* Return the number of registers to use for an argument passed partially
976 in registers and partially in memory. */
977
978int
979function_arg_partial_nregs (cum, mode, type, named)
980 CUMULATIVE_ARGS *cum;
981 enum machine_mode mode;
982 tree type;
69bc71fa 983 int named ATTRIBUTE_UNUSED;
22ef4e9b
JL
984{
985 int size, align;
986
987 /* We only support using 2 data registers as argument registers. */
988 int nregs = 2;
989
990 /* Figure out the size of the object to be passed. */
991 if (mode == BLKmode)
992 size = int_size_in_bytes (type);
993 else
994 size = GET_MODE_SIZE (mode);
995
996 /* Figure out the alignment of the object to be passed. */
997 align = size;
998
999 cum->nbytes = (cum->nbytes + 3) & ~3;
1000
1001 /* Don't pass this arg via a register if all the argument registers
1002 are used up. */
1003 if (cum->nbytes > nregs * UNITS_PER_WORD)
1004 return 0;
1005
1006 if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1007 return 0;
1008
1009 /* Don't pass this arg via a register if it would be split between
1010 registers and memory. */
1011 if (type == NULL_TREE
1012 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1013 return 0;
1014
1015 return (nregs * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
1016}
1017
1018/* Output a tst insn. */
1943c2c1 1019const char *
22ef4e9b
JL
1020output_tst (operand, insn)
1021 rtx operand, insn;
1022{
22ef4e9b
JL
1023 rtx temp;
1024 int past_call = 0;
1025
1026 /* We can save a byte if we can find a register which has the value
1027 zero in it. */
1028 temp = PREV_INSN (insn);
74452ac3 1029 while (optimize && temp)
22ef4e9b
JL
1030 {
1031 rtx set;
1032
1033 /* We allow the search to go through call insns. We record
1034 the fact that we've past a CALL_INSN and reject matches which
1035 use call clobbered registers. */
1036 if (GET_CODE (temp) == CODE_LABEL
1037 || GET_CODE (temp) == JUMP_INSN
1038 || GET_CODE (temp) == BARRIER)
1039 break;
1040
1041 if (GET_CODE (temp) == CALL_INSN)
1042 past_call = 1;
1043
1044 if (GET_CODE (temp) == NOTE)
1045 {
1046 temp = PREV_INSN (temp);
1047 continue;
1048 }
1049
1050 /* It must be an insn, see if it is a simple set. */
1051 set = single_set (temp);
1052 if (!set)
1053 {
1054 temp = PREV_INSN (temp);
1055 continue;
1056 }
1057
1058 /* Are we setting a data register to zero (this does not win for
1059 address registers)?
1060
1061 If it's a call clobbered register, have we past a call?
1062
1063 Make sure the register we find isn't the same as ourself;
4d1a91c2
JL
1064 the mn10300 can't encode that.
1065
1066 ??? reg_set_between_p return nonzero anytime we pass a CALL_INSN
1067 so the code to detect calls here isn't doing anything useful. */
22ef4e9b
JL
1068 if (REG_P (SET_DEST (set))
1069 && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1070 && !reg_set_between_p (SET_DEST (set), temp, insn)
74452ac3
JL
1071 && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1072 == REGNO_REG_CLASS (REGNO (operand)))
705ac34f
JL
1073 && REGNO_REG_CLASS (REGNO (SET_DEST (set))) != EXTENDED_REGS
1074 && REGNO (SET_DEST (set)) != REGNO (operand)
1075 && (!past_call
1076 || !call_used_regs[REGNO (SET_DEST (set))]))
1077 {
1078 rtx xoperands[2];
1079 xoperands[0] = operand;
1080 xoperands[1] = SET_DEST (set);
1081
1082 output_asm_insn ("cmp %1,%0", xoperands);
1083 return "";
1084 }
1085
1086 if (REGNO_REG_CLASS (REGNO (operand)) == EXTENDED_REGS
1087 && REG_P (SET_DEST (set))
1088 && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1089 && !reg_set_between_p (SET_DEST (set), temp, insn)
1090 && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1091 != REGNO_REG_CLASS (REGNO (operand)))
1092 && REGNO_REG_CLASS (REGNO (SET_DEST (set))) == EXTENDED_REGS
22ef4e9b
JL
1093 && REGNO (SET_DEST (set)) != REGNO (operand)
1094 && (!past_call
1095 || !call_used_regs[REGNO (SET_DEST (set))]))
1096 {
1097 rtx xoperands[2];
1098 xoperands[0] = operand;
1099 xoperands[1] = SET_DEST (set);
1100
1101 output_asm_insn ("cmp %1,%0", xoperands);
1102 return "";
1103 }
1104 temp = PREV_INSN (temp);
1105 }
1106 return "cmp 0,%0";
1107}
460f4b9d
JL
1108
1109int
1110impossible_plus_operand (op, mode)
1111 rtx op;
69bc71fa 1112 enum machine_mode mode ATTRIBUTE_UNUSED;
460f4b9d 1113{
460f4b9d
JL
1114 if (GET_CODE (op) != PLUS)
1115 return 0;
1116
4c742813
JL
1117 if (XEXP (op, 0) == stack_pointer_rtx
1118 || XEXP (op, 1) == stack_pointer_rtx)
460f4b9d
JL
1119 return 1;
1120
460f4b9d
JL
1121 return 0;
1122}
e9ad4573 1123
f8912297
JL
1124/* Return 1 if X is a CONST_INT that is only 8 bits wide. This is used
1125 for the btst insn which may examine memory or a register (the memory
1126 variant only allows an unsigned 8 bit integer). */
1127int
1128const_8bit_operand (op, mode)
1129 register rtx op;
69bc71fa 1130 enum machine_mode mode ATTRIBUTE_UNUSED;
f8912297
JL
1131{
1132 return (GET_CODE (op) == CONST_INT
1133 && INTVAL (op) >= 0
1134 && INTVAL (op) < 256);
1135}
1136
1137/* Similarly, but when using a zero_extract pattern for a btst where
1138 the source operand might end up in memory. */
1139int
1140mask_ok_for_mem_btst (len, bit)
1141 int len;
1142 int bit;
1143{
1144 int mask = 0;
1145
1146 while (len > 0)
1147 {
1148 mask |= (1 << bit);
1149 bit++;
1150 len--;
1151 }
1152
1153 /* MASK must bit into an 8bit value. */
1154 return (((mask & 0xff) == mask)
1155 || ((mask & 0xff00) == mask)
1156 || ((mask & 0xff0000) == mask)
1157 || ((mask & 0xff000000) == mask));
1158}
1159
e9ad4573
JL
1160/* Return 1 if X contains a symbolic expression. We know these
1161 expressions will have one of a few well defined forms, so
1162 we need only check those forms. */
1163int
1164symbolic_operand (op, mode)
1165 register rtx op;
69bc71fa 1166 enum machine_mode mode ATTRIBUTE_UNUSED;
e9ad4573
JL
1167{
1168 switch (GET_CODE (op))
1169 {
1170 case SYMBOL_REF:
1171 case LABEL_REF:
1172 return 1;
1173 case CONST:
1174 op = XEXP (op, 0);
1175 return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1176 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1177 && GET_CODE (XEXP (op, 1)) == CONST_INT);
1178 default:
1179 return 0;
1180 }
1181}
1182
1183/* Try machine dependent ways of modifying an illegitimate address
1184 to be legitimate. If we find one, return the new valid address.
1185 This macro is used in only one place: `memory_address' in explow.c.
1186
1187 OLDX is the address as it was before break_out_memory_refs was called.
1188 In some cases it is useful to look at this to decide what needs to be done.
1189
1190 MODE and WIN are passed so that this macro can use
1191 GO_IF_LEGITIMATE_ADDRESS.
1192
1193 Normally it is always safe for this macro to do nothing. It exists to
1194 recognize opportunities to optimize the output.
1195
1196 But on a few ports with segmented architectures and indexed addressing
1197 (mn10300, hppa) it is used to rewrite certain problematical addresses. */
1198rtx
1199legitimize_address (x, oldx, mode)
1200 rtx x;
69bc71fa
KG
1201 rtx oldx ATTRIBUTE_UNUSED;
1202 enum machine_mode mode ATTRIBUTE_UNUSED;
e9ad4573
JL
1203{
1204 /* Uh-oh. We might have an address for x[n-100000]. This needs
1205 special handling to avoid creating an indexed memory address
1206 with x-100000 as the base. */
1207 if (GET_CODE (x) == PLUS
1208 && symbolic_operand (XEXP (x, 1), VOIDmode))
1209 {
1210 /* Ugly. We modify things here so that the address offset specified
1211 by the index expression is computed first, then added to x to form
1212 the entire address. */
1213
69bc71fa 1214 rtx regx1, regy1, regy2, y;
e9ad4573
JL
1215
1216 /* Strip off any CONST. */
1217 y = XEXP (x, 1);
1218 if (GET_CODE (y) == CONST)
1219 y = XEXP (y, 0);
1220
bf4219f0
JL
1221 if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1222 {
1223 regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1224 regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1225 regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1226 regx1 = force_reg (Pmode,
1227 gen_rtx (GET_CODE (y), Pmode, regx1, regy2));
c5c76735 1228 return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
bf4219f0 1229 }
e9ad4573 1230 }
371036e0 1231 return x;
e9ad4573 1232}
460ad325
AO
1233
1234int
1235mn10300_address_cost (x, unsig)
1236 rtx x;
1237 int *unsig;
1238{
1239 int _s = 0;
1240 if (unsig == 0)
1241 unsig = &_s;
1242
1243 switch (GET_CODE (x))
1244 {
1245 case REG:
1246 switch (REGNO_REG_CLASS (REGNO (x)))
1247 {
1248 case SP_REGS:
1249 *unsig = 1;
1250 return 0;
1251
1252 case ADDRESS_REGS:
1253 return 1;
1254
1255 case DATA_REGS:
1256 case EXTENDED_REGS:
1257 return 3;
1258
1259 case NO_REGS:
1260 return 5;
1261
1262 default:
1263 abort ();
1264 }
1265
1266 case PLUS:
1267 case MINUS:
d82704fb
AO
1268 case ASHIFT:
1269 case AND:
460ad325
AO
1270 case IOR:
1271 return (mn10300_address_cost (XEXP (x, 0), unsig)
1272 + mn10300_address_cost (XEXP (x, 1), unsig));
1273
1274 case EXPR_LIST:
1275 case SUBREG:
1276 case MEM:
1277 return ADDRESS_COST (XEXP (x, 0));
1278
1279 case ZERO_EXTEND:
1280 *unsig = 1;
1281 return mn10300_address_cost (XEXP (x, 0), unsig);
1282
1283 case CONST_INT:
1284 if (INTVAL (x) == 0)
1285 return 0;
1286 if (INTVAL (x) + (*unsig ? 0 : 0x80) < 0x100)
1287 return 1;
1288 if (INTVAL (x) + (*unsig ? 0 : 0x8000) < 0x10000)
1289 return 3;
1290 if (INTVAL (x) + (*unsig ? 0 : 0x800000) < 0x1000000)
1291 return 5;
1292 return 7;
1293
1294 case CONST:
1295 case SYMBOL_REF:
d82704fb 1296 case LABEL_REF:
460ad325
AO
1297 return 8;
1298
1299 case ADDRESSOF:
1300 switch (GET_CODE (XEXP (x, 0)))
1301 {
1302 case MEM:
1303 return ADDRESS_COST (XEXP (x, 0));
1304
1305 case REG:
1306 return 1;
1307
1308 default:
1309 abort ();
1310 }
1311
1312 default:
1313 abort ();
1314
1315 }
1316}