OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [config/] [mcore/] [mcore.c] - Blame information for rev 841

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/* Output routines for Motorola MCore processor
2
   Copyright (C) 1993, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3
   Free Software Foundation, Inc.
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 "tree.h"
27
#include "tm_p.h"
28
#include "assert.h"
29
#include "mcore.h"
30
#include "regs.h"
31
#include "hard-reg-set.h"
32
#include "real.h"
33
#include "insn-config.h"
34
#include "conditions.h"
35
#include "output.h"
36
#include "insn-attr.h"
37
#include "flags.h"
38
#include "obstack.h"
39
#include "expr.h"
40
#include "reload.h"
41
#include "recog.h"
42
#include "function.h"
43
#include "ggc.h"
44
#include "toplev.h"
45
#include "target.h"
46
#include "target-def.h"
47
 
48
/* Maximum size we are allowed to grow the stack in a single operation.
49
   If we want more, we must do it in increments of at most this size.
50
   If this value is 0, we don't check at all.  */
51
int mcore_stack_increment = STACK_UNITS_MAXSTEP;
52
 
53
/* For dumping information about frame sizes.  */
54
char * mcore_current_function_name = 0;
55
long   mcore_current_compilation_timestamp = 0;
56
 
57
/* Global variables for machine-dependent things.  */
58
 
59
/* Saved operands from the last compare to use when we generate an scc
60
  or bcc insn.  */
61
rtx arch_compare_op0;
62
rtx arch_compare_op1;
63
 
64
/* Provides the class number of the smallest class containing
65
   reg number.  */
66
const int regno_reg_class[FIRST_PSEUDO_REGISTER] =
67
{
68
  GENERAL_REGS, ONLYR1_REGS,  LRW_REGS,     LRW_REGS,
69
  LRW_REGS,     LRW_REGS,     LRW_REGS,     LRW_REGS,
70
  LRW_REGS,     LRW_REGS,     LRW_REGS,     LRW_REGS,
71
  LRW_REGS,     LRW_REGS,     LRW_REGS,     GENERAL_REGS,
72
  GENERAL_REGS, C_REGS,       NO_REGS,      NO_REGS,
73
};
74
 
75
/* Provide reg_class from a letter such as appears in the machine
76
   description.  */
77
const enum reg_class reg_class_from_letter[] =
78
{
79
  /* a */ LRW_REGS, /* b */ ONLYR1_REGS, /* c */ C_REGS,  /* d */ NO_REGS,
80
  /* e */ NO_REGS, /* f */ NO_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
81
  /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ NO_REGS,
82
  /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
83
  /* q */ NO_REGS, /* r */ GENERAL_REGS, /* s */ NO_REGS, /* t */ NO_REGS,
84
  /* u */ NO_REGS, /* v */ NO_REGS, /* w */ NO_REGS, /* x */ ALL_REGS,
85
  /* y */ NO_REGS, /* z */ NO_REGS
86
};
87
 
88
struct mcore_frame
89
{
90
  int arg_size;                 /* Stdarg spills (bytes).  */
91
  int reg_size;                 /* Non-volatile reg saves (bytes).  */
92
  int reg_mask;                 /* Non-volatile reg saves.  */
93
  int local_size;               /* Locals.  */
94
  int outbound_size;            /* Arg overflow on calls out.  */
95
  int pad_outbound;
96
  int pad_local;
97
  int pad_reg;
98
  /* Describe the steps we'll use to grow it.  */
99
#define MAX_STACK_GROWS 4       /* Gives us some spare space.  */
100
  int growth[MAX_STACK_GROWS];
101
  int arg_offset;
102
  int reg_offset;
103
  int reg_growth;
104
  int local_growth;
105
};
106
 
107
typedef enum
108
{
109
  COND_NO,
110
  COND_MOV_INSN,
111
  COND_CLR_INSN,
112
  COND_INC_INSN,
113
  COND_DEC_INSN,
114
  COND_BRANCH_INSN
115
}
116
cond_type;
117
 
118
static void       output_stack_adjust           (int, int);
119
static int        calc_live_regs                (int *);
120
static int        try_constant_tricks           (long, int *, int *);
121
static const char *     output_inline_const     (enum machine_mode, rtx *);
122
static void       layout_mcore_frame            (struct mcore_frame *);
123
static void       mcore_setup_incoming_varargs  (CUMULATIVE_ARGS *, enum machine_mode, tree, int *, int);
124
static cond_type  is_cond_candidate             (rtx);
125
static rtx        emit_new_cond_insn            (rtx, int);
126
static rtx        conditionalize_block          (rtx);
127
static void       conditionalize_optimization   (void);
128
static void       mcore_reorg                   (void);
129
static rtx        handle_structs_in_regs        (enum machine_mode, tree, int);
130
static void       mcore_mark_dllexport          (tree);
131
static void       mcore_mark_dllimport          (tree);
132
static int        mcore_dllexport_p             (tree);
133
static int        mcore_dllimport_p             (tree);
134
const struct attribute_spec mcore_attribute_table[];
135
static tree       mcore_handle_naked_attribute  (tree *, tree, tree, int, bool *);
136
#ifdef OBJECT_FORMAT_ELF
137
static void       mcore_asm_named_section       (const char *,
138
                                                 unsigned int, tree);
139
#endif
140
static void       mcore_unique_section          (tree, int);
141
static void mcore_encode_section_info           (tree, rtx, int);
142
static const char *mcore_strip_name_encoding    (const char *);
143
static int        mcore_const_costs             (rtx, RTX_CODE);
144
static int        mcore_and_cost                (rtx);
145
static int        mcore_ior_cost                (rtx);
146
static bool       mcore_rtx_costs               (rtx, int, int, int *);
147
static void       mcore_external_libcall        (rtx);
148
static bool       mcore_return_in_memory        (tree, tree);
149
static int        mcore_arg_partial_bytes       (CUMULATIVE_ARGS *,
150
                                                 enum machine_mode,
151
                                                 tree, bool);
152
 
153
 
154
/* Initialize the GCC target structure.  */
155
#undef  TARGET_ASM_EXTERNAL_LIBCALL
156
#define TARGET_ASM_EXTERNAL_LIBCALL     mcore_external_libcall
157
 
158
#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
159
#undef  TARGET_MERGE_DECL_ATTRIBUTES
160
#define TARGET_MERGE_DECL_ATTRIBUTES    merge_dllimport_decl_attributes
161
#endif
162
 
163
#ifdef OBJECT_FORMAT_ELF
164
#undef  TARGET_ASM_UNALIGNED_HI_OP
165
#define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
166
#undef  TARGET_ASM_UNALIGNED_SI_OP
167
#define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
168
#endif
169
 
170
#undef  TARGET_ATTRIBUTE_TABLE
171
#define TARGET_ATTRIBUTE_TABLE          mcore_attribute_table
172
#undef  TARGET_ASM_UNIQUE_SECTION
173
#define TARGET_ASM_UNIQUE_SECTION       mcore_unique_section
174
#undef  TARGET_ASM_FUNCTION_RODATA_SECTION
175
#define TARGET_ASM_FUNCTION_RODATA_SECTION default_no_function_rodata_section
176
#undef  TARGET_DEFAULT_TARGET_FLAGS
177
#define TARGET_DEFAULT_TARGET_FLAGS     TARGET_DEFAULT
178
#undef  TARGET_ENCODE_SECTION_INFO
179
#define TARGET_ENCODE_SECTION_INFO      mcore_encode_section_info
180
#undef  TARGET_STRIP_NAME_ENCODING
181
#define TARGET_STRIP_NAME_ENCODING      mcore_strip_name_encoding
182
#undef  TARGET_RTX_COSTS
183
#define TARGET_RTX_COSTS                mcore_rtx_costs
184
#undef  TARGET_ADDRESS_COST
185
#define TARGET_ADDRESS_COST             hook_int_rtx_0
186
#undef  TARGET_MACHINE_DEPENDENT_REORG
187
#define TARGET_MACHINE_DEPENDENT_REORG  mcore_reorg
188
 
189
#undef  TARGET_PROMOTE_FUNCTION_ARGS
190
#define TARGET_PROMOTE_FUNCTION_ARGS    hook_bool_tree_true
191
#undef  TARGET_PROMOTE_FUNCTION_RETURN
192
#define TARGET_PROMOTE_FUNCTION_RETURN  hook_bool_tree_true
193
#undef  TARGET_PROMOTE_PROTOTYPES
194
#define TARGET_PROMOTE_PROTOTYPES       hook_bool_tree_true
195
 
196
#undef  TARGET_RETURN_IN_MEMORY
197
#define TARGET_RETURN_IN_MEMORY         mcore_return_in_memory
198
#undef  TARGET_MUST_PASS_IN_STACK
199
#define TARGET_MUST_PASS_IN_STACK       must_pass_in_stack_var_size
200
#undef  TARGET_PASS_BY_REFERENCE
201
#define TARGET_PASS_BY_REFERENCE  hook_pass_by_reference_must_pass_in_stack
202
#undef  TARGET_ARG_PARTIAL_BYTES
203
#define TARGET_ARG_PARTIAL_BYTES        mcore_arg_partial_bytes
204
 
205
#undef  TARGET_SETUP_INCOMING_VARARGS
206
#define TARGET_SETUP_INCOMING_VARARGS   mcore_setup_incoming_varargs
207
 
208
struct gcc_target targetm = TARGET_INITIALIZER;
209
 
210
/* Adjust the stack and return the number of bytes taken to do it.  */
211
static void
212
output_stack_adjust (int direction, int size)
213
{
214
  /* If extending stack a lot, we do it incrementally.  */
215
  if (direction < 0 && size > mcore_stack_increment && mcore_stack_increment > 0)
216
    {
217
      rtx tmp = gen_rtx_REG (SImode, 1);
218
      rtx memref;
219
 
220
      emit_insn (gen_movsi (tmp, GEN_INT (mcore_stack_increment)));
221
      do
222
        {
223
          emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
224
          memref = gen_rtx_MEM (SImode, stack_pointer_rtx);
225
          MEM_VOLATILE_P (memref) = 1;
226
          emit_insn (gen_movsi (memref, stack_pointer_rtx));
227
          size -= mcore_stack_increment;
228
        }
229
      while (size > mcore_stack_increment);
230
 
231
      /* SIZE is now the residual for the last adjustment,
232
         which doesn't require a probe.  */
233
    }
234
 
235
  if (size)
236
    {
237
      rtx insn;
238
      rtx val = GEN_INT (size);
239
 
240
      if (size > 32)
241
        {
242
          rtx nval = gen_rtx_REG (SImode, 1);
243
          emit_insn (gen_movsi (nval, val));
244
          val = nval;
245
        }
246
 
247
      if (direction > 0)
248
        insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
249
      else
250
        insn = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
251
 
252
      emit_insn (insn);
253
    }
254
}
255
 
256
/* Work out the registers which need to be saved,
257
   both as a mask and a count.  */
258
 
259
static int
260
calc_live_regs (int * count)
261
{
262
  int reg;
263
  int live_regs_mask = 0;
264
 
265
  * count = 0;
266
 
267
  for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
268
    {
269
      if (regs_ever_live[reg] && !call_used_regs[reg])
270
        {
271
          (*count)++;
272
          live_regs_mask |= (1 << reg);
273
        }
274
    }
275
 
276
  return live_regs_mask;
277
}
278
 
279
/* Print the operand address in x to the stream.  */
280
 
281
void
282
mcore_print_operand_address (FILE * stream, rtx x)
283
{
284
  switch (GET_CODE (x))
285
    {
286
    case REG:
287
      fprintf (stream, "(%s)", reg_names[REGNO (x)]);
288
      break;
289
 
290
    case PLUS:
291
      {
292
        rtx base = XEXP (x, 0);
293
        rtx index = XEXP (x, 1);
294
 
295
        if (GET_CODE (base) != REG)
296
          {
297
            /* Ensure that BASE is a register (one of them must be).  */
298
            rtx temp = base;
299
            base = index;
300
            index = temp;
301
          }
302
 
303
        switch (GET_CODE (index))
304
          {
305
          case CONST_INT:
306
            fprintf (stream, "(%s," HOST_WIDE_INT_PRINT_DEC ")",
307
                     reg_names[REGNO(base)], INTVAL (index));
308
            break;
309
 
310
          default:
311
            gcc_unreachable ();
312
          }
313
      }
314
 
315
      break;
316
 
317
    default:
318
      output_addr_const (stream, x);
319
      break;
320
    }
321
}
322
 
323
/* Print operand x (an rtx) in assembler syntax to file stream
324
   according to modifier code.
325
 
326
   'R'  print the next register or memory location along, i.e. the lsw in
327
        a double word value
328
   'O'  print a constant without the #
329
   'M'  print a constant as its negative
330
   'P'  print log2 of a power of two
331
   'Q'  print log2 of an inverse of a power of two
332
   'U'  print register for ldm/stm instruction
333
   'X'  print byte number for xtrbN instruction.  */
334
 
335
void
336
mcore_print_operand (FILE * stream, rtx x, int code)
337
{
338
  switch (code)
339
    {
340
    case 'N':
341
      if (INTVAL(x) == -1)
342
        fprintf (asm_out_file, "32");
343
      else
344
        fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x) + 1));
345
      break;
346
    case 'P':
347
      fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x)));
348
      break;
349
    case 'Q':
350
      fprintf (asm_out_file, "%d", exact_log2 (~INTVAL (x)));
351
      break;
352
    case 'O':
353
      fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
354
      break;
355
    case 'M':
356
      fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, - INTVAL (x));
357
      break;
358
    case 'R':
359
      /* Next location along in memory or register.  */
360
      switch (GET_CODE (x))
361
        {
362
        case REG:
363
          fputs (reg_names[REGNO (x) + 1], (stream));
364
          break;
365
        case MEM:
366
          mcore_print_operand_address
367
            (stream, XEXP (adjust_address (x, SImode, 4), 0));
368
          break;
369
        default:
370
          gcc_unreachable ();
371
        }
372
      break;
373
    case 'U':
374
      fprintf (asm_out_file, "%s-%s", reg_names[REGNO (x)],
375
               reg_names[REGNO (x) + 3]);
376
      break;
377
    case 'x':
378
      fprintf (asm_out_file, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
379
      break;
380
    case 'X':
381
      fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, 3 - INTVAL (x) / 8);
382
      break;
383
 
384
    default:
385
      switch (GET_CODE (x))
386
        {
387
        case REG:
388
          fputs (reg_names[REGNO (x)], (stream));
389
          break;
390
        case MEM:
391
          output_address (XEXP (x, 0));
392
          break;
393
        default:
394
          output_addr_const (stream, x);
395
          break;
396
        }
397
      break;
398
    }
399
}
400
 
401
/* What does a constant cost ?  */
402
 
403
static int
404
mcore_const_costs (rtx exp, enum rtx_code code)
405
{
406
  int val = INTVAL (exp);
407
 
408
  /* Easy constants.  */
409
  if (   CONST_OK_FOR_I (val)
410
      || CONST_OK_FOR_M (val)
411
      || CONST_OK_FOR_N (val)
412
      || (code == PLUS && CONST_OK_FOR_L (val)))
413
    return 1;
414
  else if (code == AND
415
           && (   CONST_OK_FOR_M (~val)
416
               || CONST_OK_FOR_N (~val)))
417
    return 2;
418
  else if (code == PLUS
419
           && (   CONST_OK_FOR_I (-val)
420
               || CONST_OK_FOR_M (-val)
421
               || CONST_OK_FOR_N (-val)))
422
    return 2;
423
 
424
  return 5;
425
}
426
 
427
/* What does an and instruction cost - we do this b/c immediates may
428
   have been relaxed.   We want to ensure that cse will cse relaxed immeds
429
   out.  Otherwise we'll get bad code (multiple reloads of the same const).  */
430
 
431
static int
432
mcore_and_cost (rtx x)
433
{
434
  int val;
435
 
436
  if (GET_CODE (XEXP (x, 1)) != CONST_INT)
437
    return 2;
438
 
439
  val = INTVAL (XEXP (x, 1));
440
 
441
  /* Do it directly.  */
442
  if (CONST_OK_FOR_K (val) || CONST_OK_FOR_M (~val))
443
    return 2;
444
  /* Takes one instruction to load.  */
445
  else if (const_ok_for_mcore (val))
446
    return 3;
447
  /* Takes two instructions to load.  */
448
  else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
449
    return 4;
450
 
451
  /* Takes a lrw to load.  */
452
  return 5;
453
}
454
 
455
/* What does an or cost - see and_cost().  */
456
 
457
static int
458
mcore_ior_cost (rtx x)
459
{
460
  int val;
461
 
462
  if (GET_CODE (XEXP (x, 1)) != CONST_INT)
463
    return 2;
464
 
465
  val = INTVAL (XEXP (x, 1));
466
 
467
  /* Do it directly with bclri.  */
468
  if (CONST_OK_FOR_M (val))
469
    return 2;
470
  /* Takes one instruction to load.  */
471
  else if (const_ok_for_mcore (val))
472
    return 3;
473
  /* Takes two instructions to load.  */
474
  else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
475
    return 4;
476
 
477
  /* Takes a lrw to load.  */
478
  return 5;
479
}
480
 
481
static bool
482
mcore_rtx_costs (rtx x, int code, int outer_code, int * total)
483
{
484
  switch (code)
485
    {
486
    case CONST_INT:
487
      *total = mcore_const_costs (x, outer_code);
488
      return true;
489
    case CONST:
490
    case LABEL_REF:
491
    case SYMBOL_REF:
492
      *total = 5;
493
      return true;
494
    case CONST_DOUBLE:
495
      *total = 10;
496
      return true;
497
 
498
    case AND:
499
      *total = COSTS_N_INSNS (mcore_and_cost (x));
500
      return true;
501
 
502
    case IOR:
503
      *total = COSTS_N_INSNS (mcore_ior_cost (x));
504
      return true;
505
 
506
    case DIV:
507
    case UDIV:
508
    case MOD:
509
    case UMOD:
510
    case FLOAT:
511
    case FIX:
512
      *total = COSTS_N_INSNS (100);
513
      return true;
514
 
515
    default:
516
      return false;
517
    }
518
}
519
 
520
/* Check to see if a comparison against a constant can be made more efficient
521
   by incrementing/decrementing the constant to get one that is more efficient
522
   to load.  */
523
 
524
int
525
mcore_modify_comparison (enum rtx_code code)
526
{
527
  rtx op1 = arch_compare_op1;
528
 
529
  if (GET_CODE (op1) == CONST_INT)
530
    {
531
      int val = INTVAL (op1);
532
 
533
      switch (code)
534
        {
535
        case LE:
536
          if (CONST_OK_FOR_J (val + 1))
537
            {
538
              arch_compare_op1 = GEN_INT (val + 1);
539
              return 1;
540
            }
541
          break;
542
 
543
        default:
544
          break;
545
        }
546
    }
547
 
548
  return 0;
549
}
550
 
551
/* Prepare the operands for a comparison.  */
552
 
553
rtx
554
mcore_gen_compare_reg (enum rtx_code code)
555
{
556
  rtx op0 = arch_compare_op0;
557
  rtx op1 = arch_compare_op1;
558
  rtx cc_reg = gen_rtx_REG (CCmode, CC_REG);
559
 
560
  if (CONSTANT_P (op1) && GET_CODE (op1) != CONST_INT)
561
    op1 = force_reg (SImode, op1);
562
 
563
  /* cmpnei: 0-31 (K immediate)
564
     cmplti: 1-32 (J immediate, 0 using btsti x,31).  */
565
  switch (code)
566
    {
567
    case EQ:    /* Use inverted condition, cmpne.  */
568
      code = NE;
569
      /* Drop through.  */
570
 
571
    case NE:    /* Use normal condition, cmpne.  */
572
      if (GET_CODE (op1) == CONST_INT && ! CONST_OK_FOR_K (INTVAL (op1)))
573
        op1 = force_reg (SImode, op1);
574
      break;
575
 
576
    case LE:    /* Use inverted condition, reversed cmplt.  */
577
      code = GT;
578
      /* Drop through.  */
579
 
580
    case GT:    /* Use normal condition, reversed cmplt.  */
581
      if (GET_CODE (op1) == CONST_INT)
582
        op1 = force_reg (SImode, op1);
583
      break;
584
 
585
    case GE:    /* Use inverted condition, cmplt.  */
586
      code = LT;
587
      /* Drop through.  */
588
 
589
    case LT:    /* Use normal condition, cmplt.  */
590
      if (GET_CODE (op1) == CONST_INT &&
591
          /* covered by btsti x,31.  */
592
          INTVAL (op1) != 0 &&
593
          ! CONST_OK_FOR_J (INTVAL (op1)))
594
        op1 = force_reg (SImode, op1);
595
      break;
596
 
597
    case GTU:   /* Use inverted condition, cmple.  */
598
      /* Unsigned > 0 is the same as != 0, but we need to invert the
599
         condition, so we want to set code = EQ.  This cannot be done
600
         however, as the mcore does not support such a test.  Instead
601
         we cope with this case in the "bgtu" pattern itself so we
602
         should never reach this point.  */
603
      gcc_assert (GET_CODE (op1) != CONST_INT || INTVAL (op1) != 0);
604
      code = LEU;
605
      /* Drop through.  */
606
 
607
    case LEU:   /* Use normal condition, reversed cmphs.  */
608
      if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
609
        op1 = force_reg (SImode, op1);
610
      break;
611
 
612
    case LTU:   /* Use inverted condition, cmphs.  */
613
      code = GEU;
614
      /* Drop through.  */
615
 
616
    case GEU:   /* Use normal condition, cmphs.  */
617
      if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
618
        op1 = force_reg (SImode, op1);
619
      break;
620
 
621
    default:
622
      break;
623
    }
624
 
625
  emit_insn (gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_fmt_ee (code, CCmode, op0, op1)));
626
 
627
  return cc_reg;
628
}
629
 
630
int
631
mcore_symbolic_address_p (rtx x)
632
{
633
  switch (GET_CODE (x))
634
    {
635
    case SYMBOL_REF:
636
    case LABEL_REF:
637
      return 1;
638
    case CONST:
639
      x = XEXP (x, 0);
640
      return (   (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
641
               || GET_CODE (XEXP (x, 0)) == LABEL_REF)
642
              && GET_CODE (XEXP (x, 1)) == CONST_INT);
643
    default:
644
      return 0;
645
    }
646
}
647
 
648
/* Functions to output assembly code for a function call.  */
649
 
650
char *
651
mcore_output_call (rtx operands[], int index)
652
{
653
  static char buffer[20];
654
  rtx addr = operands [index];
655
 
656
  if (REG_P (addr))
657
    {
658
      if (TARGET_CG_DATA)
659
        {
660
          gcc_assert (mcore_current_function_name);
661
 
662
          ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
663
                              "unknown", 1);
664
        }
665
 
666
      sprintf (buffer, "jsr\t%%%d", index);
667
    }
668
  else
669
    {
670
      if (TARGET_CG_DATA)
671
        {
672
          gcc_assert (mcore_current_function_name);
673
          gcc_assert (GET_CODE (addr) == SYMBOL_REF);
674
 
675
          ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
676
                              XSTR (addr, 0), 0);
677
        }
678
 
679
      sprintf (buffer, "jbsr\t%%%d", index);
680
    }
681
 
682
  return buffer;
683
}
684
 
685
/* Can we load a constant with a single instruction ?  */
686
 
687
int
688
const_ok_for_mcore (int value)
689
{
690
  if (value >= 0 && value <= 127)
691
    return 1;
692
 
693
  /* Try exact power of two.  */
694
  if ((value & (value - 1)) == 0)
695
    return 1;
696
 
697
  /* Try exact power of two - 1.  */
698
  if ((value & (value + 1)) == 0)
699
    return 1;
700
 
701
  return 0;
702
}
703
 
704
/* Can we load a constant inline with up to 2 instructions ?  */
705
 
706
int
707
mcore_const_ok_for_inline (long value)
708
{
709
  int x, y;
710
 
711
  return try_constant_tricks (value, & x, & y) > 0;
712
}
713
 
714
/* Are we loading the constant using a not ?  */
715
 
716
int
717
mcore_const_trick_uses_not (long value)
718
{
719
  int x, y;
720
 
721
  return try_constant_tricks (value, & x, & y) == 2;
722
}
723
 
724
/* Try tricks to load a constant inline and return the trick number if
725
   success (0 is non-inlinable).
726
 
727
   0: not inlinable
728
   1: single instruction (do the usual thing)
729
   2: single insn followed by a 'not'
730
   3: single insn followed by a subi
731
   4: single insn followed by an addi
732
   5: single insn followed by rsubi
733
   6: single insn followed by bseti
734
   7: single insn followed by bclri
735
   8: single insn followed by rotli
736
   9: single insn followed by lsli
737
   10: single insn followed by ixh
738
   11: single insn followed by ixw.  */
739
 
740
static int
741
try_constant_tricks (long value, int * x, int * y)
742
{
743
  int i;
744
  unsigned bit, shf, rot;
745
 
746
  if (const_ok_for_mcore (value))
747
    return 1;   /* Do the usual thing.  */
748
 
749
  if (TARGET_HARDLIT)
750
    {
751
      if (const_ok_for_mcore (~value))
752
        {
753
          *x = ~value;
754
          return 2;
755
        }
756
 
757
      for (i = 1; i <= 32; i++)
758
        {
759
          if (const_ok_for_mcore (value - i))
760
            {
761
              *x = value - i;
762
              *y = i;
763
 
764
              return 3;
765
            }
766
 
767
          if (const_ok_for_mcore (value + i))
768
            {
769
              *x = value + i;
770
              *y = i;
771
 
772
              return 4;
773
            }
774
        }
775
 
776
      bit = 0x80000000L;
777
 
778
      for (i = 0; i <= 31; i++)
779
        {
780
          if (const_ok_for_mcore (i - value))
781
            {
782
              *x = i - value;
783
              *y = i;
784
 
785
              return 5;
786
            }
787
 
788
          if (const_ok_for_mcore (value & ~bit))
789
            {
790
              *y = bit;
791
              *x = value & ~bit;
792
 
793
              return 6;
794
            }
795
 
796
          if (const_ok_for_mcore (value | bit))
797
            {
798
              *y = ~bit;
799
              *x = value | bit;
800
 
801
              return 7;
802
            }
803
 
804
          bit >>= 1;
805
        }
806
 
807
      shf = value;
808
      rot = value;
809
 
810
      for (i = 1; i < 31; i++)
811
        {
812
          int c;
813
 
814
          /* MCore has rotate left.  */
815
          c = rot << 31;
816
          rot >>= 1;
817
          rot &= 0x7FFFFFFF;
818
          rot |= c;   /* Simulate rotate.  */
819
 
820
          if (const_ok_for_mcore (rot))
821
            {
822
              *y = i;
823
              *x = rot;
824
 
825
              return 8;
826
            }
827
 
828
          if (shf & 1)
829
            shf = 0;     /* Can't use logical shift, low order bit is one.  */
830
 
831
          shf >>= 1;
832
 
833
          if (shf != 0 && const_ok_for_mcore (shf))
834
            {
835
              *y = i;
836
              *x = shf;
837
 
838
              return 9;
839
            }
840
        }
841
 
842
      if ((value % 3) == 0 && const_ok_for_mcore (value / 3))
843
        {
844
          *x = value / 3;
845
 
846
          return 10;
847
        }
848
 
849
      if ((value % 5) == 0 && const_ok_for_mcore (value / 5))
850
        {
851
          *x = value / 5;
852
 
853
          return 11;
854
        }
855
    }
856
 
857
  return 0;
858
}
859
 
860
/* Check whether reg is dead at first.  This is done by searching ahead
861
   for either the next use (i.e., reg is live), a death note, or a set of
862
   reg.  Don't just use dead_or_set_p() since reload does not always mark
863
   deaths (especially if PRESERVE_DEATH_NOTES_REGNO_P is not defined). We
864
   can ignore subregs by extracting the actual register.  BRC  */
865
 
866
int
867
mcore_is_dead (rtx first, rtx reg)
868
{
869
  rtx insn;
870
 
871
  /* For mcore, subregs can't live independently of their parent regs.  */
872
  if (GET_CODE (reg) == SUBREG)
873
    reg = SUBREG_REG (reg);
874
 
875
  /* Dies immediately.  */
876
  if (dead_or_set_p (first, reg))
877
    return 1;
878
 
879
  /* Look for conclusive evidence of live/death, otherwise we have
880
     to assume that it is live.  */
881
  for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
882
    {
883
      if (GET_CODE (insn) == JUMP_INSN)
884
        return 0;        /* We lose track, assume it is alive.  */
885
 
886
      else if (GET_CODE(insn) == CALL_INSN)
887
        {
888
          /* Call's might use it for target or register parms.  */
889
          if (reg_referenced_p (reg, PATTERN (insn))
890
              || find_reg_fusage (insn, USE, reg))
891
            return 0;
892
          else if (dead_or_set_p (insn, reg))
893
            return 1;
894
        }
895
      else if (GET_CODE (insn) == INSN)
896
        {
897
          if (reg_referenced_p (reg, PATTERN (insn)))
898
            return 0;
899
          else if (dead_or_set_p (insn, reg))
900
            return 1;
901
        }
902
    }
903
 
904
  /* No conclusive evidence either way, we cannot take the chance
905
     that control flow hid the use from us -- "I'm not dead yet".  */
906
  return 0;
907
}
908
 
909
/* Count the number of ones in mask.  */
910
 
911
int
912
mcore_num_ones (int mask)
913
{
914
  /* A trick to count set bits recently posted on comp.compilers.  */
915
  mask =  (mask >> 1  & 0x55555555) + (mask & 0x55555555);
916
  mask = ((mask >> 2) & 0x33333333) + (mask & 0x33333333);
917
  mask = ((mask >> 4) + mask) & 0x0f0f0f0f;
918
  mask = ((mask >> 8) + mask);
919
 
920
  return (mask + (mask >> 16)) & 0xff;
921
}
922
 
923
/* Count the number of zeros in mask.  */
924
 
925
int
926
mcore_num_zeros (int mask)
927
{
928
  return 32 - mcore_num_ones (mask);
929
}
930
 
931
/* Determine byte being masked.  */
932
 
933
int
934
mcore_byte_offset (unsigned int mask)
935
{
936
  if (mask == 0x00ffffffL)
937
    return 0;
938
  else if (mask == 0xff00ffffL)
939
    return 1;
940
  else if (mask == 0xffff00ffL)
941
    return 2;
942
  else if (mask == 0xffffff00L)
943
    return 3;
944
 
945
  return -1;
946
}
947
 
948
/* Determine halfword being masked.  */
949
 
950
int
951
mcore_halfword_offset (unsigned int mask)
952
{
953
  if (mask == 0x0000ffffL)
954
    return 0;
955
  else if (mask == 0xffff0000L)
956
    return 1;
957
 
958
  return -1;
959
}
960
 
961
/* Output a series of bseti's corresponding to mask.  */
962
 
963
const char *
964
mcore_output_bseti (rtx dst, int mask)
965
{
966
  rtx out_operands[2];
967
  int bit;
968
 
969
  out_operands[0] = dst;
970
 
971
  for (bit = 0; bit < 32; bit++)
972
    {
973
      if ((mask & 0x1) == 0x1)
974
        {
975
          out_operands[1] = GEN_INT (bit);
976
 
977
          output_asm_insn ("bseti\t%0,%1", out_operands);
978
        }
979
      mask >>= 1;
980
    }
981
 
982
  return "";
983
}
984
 
985
/* Output a series of bclri's corresponding to mask.  */
986
 
987
const char *
988
mcore_output_bclri (rtx dst, int mask)
989
{
990
  rtx out_operands[2];
991
  int bit;
992
 
993
  out_operands[0] = dst;
994
 
995
  for (bit = 0; bit < 32; bit++)
996
    {
997
      if ((mask & 0x1) == 0x0)
998
        {
999
          out_operands[1] = GEN_INT (bit);
1000
 
1001
          output_asm_insn ("bclri\t%0,%1", out_operands);
1002
        }
1003
 
1004
      mask >>= 1;
1005
    }
1006
 
1007
  return "";
1008
}
1009
 
1010
/* Output a conditional move of two constants that are +/- 1 within each
1011
   other.  See the "movtK" patterns in mcore.md.   I'm not sure this is
1012
   really worth the effort.  */
1013
 
1014
const char *
1015
mcore_output_cmov (rtx operands[], int cmp_t, const char * test)
1016
{
1017
  int load_value;
1018
  int adjust_value;
1019
  rtx out_operands[4];
1020
 
1021
  out_operands[0] = operands[0];
1022
 
1023
  /* Check to see which constant is loadable.  */
1024
  if (const_ok_for_mcore (INTVAL (operands[1])))
1025
    {
1026
      out_operands[1] = operands[1];
1027
      out_operands[2] = operands[2];
1028
    }
1029
  else if (const_ok_for_mcore (INTVAL (operands[2])))
1030
    {
1031
      out_operands[1] = operands[2];
1032
      out_operands[2] = operands[1];
1033
 
1034
      /* Complement test since constants are swapped.  */
1035
      cmp_t = (cmp_t == 0);
1036
    }
1037
  load_value   = INTVAL (out_operands[1]);
1038
  adjust_value = INTVAL (out_operands[2]);
1039
 
1040
  /* First output the test if folded into the pattern.  */
1041
 
1042
  if (test)
1043
    output_asm_insn (test, operands);
1044
 
1045
  /* Load the constant - for now, only support constants that can be
1046
     generated with a single instruction.  maybe add general inlinable
1047
     constants later (this will increase the # of patterns since the
1048
     instruction sequence has a different length attribute).  */
1049
  if (load_value >= 0 && load_value <= 127)
1050
    output_asm_insn ("movi\t%0,%1", out_operands);
1051
  else if ((load_value & (load_value - 1)) == 0)
1052
    output_asm_insn ("bgeni\t%0,%P1", out_operands);
1053
  else if ((load_value & (load_value + 1)) == 0)
1054
    output_asm_insn ("bmaski\t%0,%N1", out_operands);
1055
 
1056
  /* Output the constant adjustment.  */
1057
  if (load_value > adjust_value)
1058
    {
1059
      if (cmp_t)
1060
        output_asm_insn ("decf\t%0", out_operands);
1061
      else
1062
        output_asm_insn ("dect\t%0", out_operands);
1063
    }
1064
  else
1065
    {
1066
      if (cmp_t)
1067
        output_asm_insn ("incf\t%0", out_operands);
1068
      else
1069
        output_asm_insn ("inct\t%0", out_operands);
1070
    }
1071
 
1072
  return "";
1073
}
1074
 
1075
/* Outputs the peephole for moving a constant that gets not'ed followed
1076
   by an and (i.e. combine the not and the and into andn). BRC  */
1077
 
1078
const char *
1079
mcore_output_andn (rtx insn ATTRIBUTE_UNUSED, rtx operands[])
1080
{
1081
  int x, y;
1082
  rtx out_operands[3];
1083
  const char * load_op;
1084
  char buf[256];
1085
  int trick_no;
1086
 
1087
  trick_no = try_constant_tricks (INTVAL (operands[1]), &x, &y);
1088
  gcc_assert (trick_no == 2);
1089
 
1090
  out_operands[0] = operands[0];
1091
  out_operands[1] = GEN_INT(x);
1092
  out_operands[2] = operands[2];
1093
 
1094
  if (x >= 0 && x <= 127)
1095
    load_op = "movi\t%0,%1";
1096
 
1097
  /* Try exact power of two.  */
1098
  else if ((x & (x - 1)) == 0)
1099
    load_op = "bgeni\t%0,%P1";
1100
 
1101
  /* Try exact power of two - 1.  */
1102
  else if ((x & (x + 1)) == 0)
1103
    load_op = "bmaski\t%0,%N1";
1104
 
1105
  else
1106
    load_op = "BADMOVI\t%0,%1";
1107
 
1108
  sprintf (buf, "%s\n\tandn\t%%2,%%0", load_op);
1109
  output_asm_insn (buf, out_operands);
1110
 
1111
  return "";
1112
}
1113
 
1114
/* Output an inline constant.  */
1115
 
1116
static const char *
1117
output_inline_const (enum machine_mode mode, rtx operands[])
1118
{
1119
  int x = 0, y = 0;
1120
  int trick_no;
1121
  rtx out_operands[3];
1122
  char buf[256];
1123
  char load_op[256];
1124
  const char *dst_fmt;
1125
  int value;
1126
 
1127
  value = INTVAL (operands[1]);
1128
 
1129
  trick_no = try_constant_tricks (value, &x, &y);
1130
  /* lrw's are handled separately: Large inlinable constants never get
1131
     turned into lrw's.  Our caller uses try_constant_tricks to back
1132
     off to an lrw rather than calling this routine.  */
1133
  gcc_assert (trick_no != 0);
1134
 
1135
  if (trick_no == 1)
1136
    x = value;
1137
 
1138
  /* operands: 0 = dst, 1 = load immed., 2 = immed. adjustment.  */
1139
  out_operands[0] = operands[0];
1140
  out_operands[1] = GEN_INT (x);
1141
 
1142
  if (trick_no > 2)
1143
    out_operands[2] = GEN_INT (y);
1144
 
1145
  /* Select dst format based on mode.  */
1146
  if (mode == DImode && (! TARGET_LITTLE_END))
1147
    dst_fmt = "%R0";
1148
  else
1149
    dst_fmt = "%0";
1150
 
1151
  if (x >= 0 && x <= 127)
1152
    sprintf (load_op, "movi\t%s,%%1", dst_fmt);
1153
 
1154
  /* Try exact power of two.  */
1155
  else if ((x & (x - 1)) == 0)
1156
    sprintf (load_op, "bgeni\t%s,%%P1", dst_fmt);
1157
 
1158
  /* Try exact power of two - 1.  */
1159
  else if ((x & (x + 1)) == 0)
1160
    sprintf (load_op, "bmaski\t%s,%%N1", dst_fmt);
1161
 
1162
  else
1163
    sprintf (load_op, "BADMOVI\t%s,%%1", dst_fmt);
1164
 
1165
  switch (trick_no)
1166
    {
1167
    case 1:
1168
      strcpy (buf, load_op);
1169
      break;
1170
    case 2:   /* not */
1171
      sprintf (buf, "%s\n\tnot\t%s\t// %d 0x%x", load_op, dst_fmt, value, value);
1172
      break;
1173
    case 3:   /* add */
1174
      sprintf (buf, "%s\n\taddi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1175
      break;
1176
    case 4:   /* sub */
1177
      sprintf (buf, "%s\n\tsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1178
      break;
1179
    case 5:   /* rsub */
1180
      /* Never happens unless -mrsubi, see try_constant_tricks().  */
1181
      sprintf (buf, "%s\n\trsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1182
      break;
1183
    case 6:   /* bset */
1184
      sprintf (buf, "%s\n\tbseti\t%s,%%P2\t// %d 0x%x", load_op, dst_fmt, value, value);
1185
      break;
1186
    case 7:   /* bclr */
1187
      sprintf (buf, "%s\n\tbclri\t%s,%%Q2\t// %d 0x%x", load_op, dst_fmt, value, value);
1188
      break;
1189
    case 8:   /* rotl */
1190
      sprintf (buf, "%s\n\trotli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1191
      break;
1192
    case 9:   /* lsl */
1193
      sprintf (buf, "%s\n\tlsli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1194
      break;
1195
    case 10:  /* ixh */
1196
      sprintf (buf, "%s\n\tixh\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1197
      break;
1198
    case 11:  /* ixw */
1199
      sprintf (buf, "%s\n\tixw\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1200
      break;
1201
    default:
1202
      return "";
1203
    }
1204
 
1205
  output_asm_insn (buf, out_operands);
1206
 
1207
  return "";
1208
}
1209
 
1210
/* Output a move of a word or less value.  */
1211
 
1212
const char *
1213
mcore_output_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
1214
                   enum machine_mode mode ATTRIBUTE_UNUSED)
1215
{
1216
  rtx dst = operands[0];
1217
  rtx src = operands[1];
1218
 
1219
  if (GET_CODE (dst) == REG)
1220
    {
1221
      if (GET_CODE (src) == REG)
1222
        {
1223
          if (REGNO (src) == CC_REG)            /* r-c */
1224
            return "mvc\t%0";
1225
          else
1226
            return "mov\t%0,%1";                /* r-r*/
1227
        }
1228
      else if (GET_CODE (src) == MEM)
1229
        {
1230
          if (GET_CODE (XEXP (src, 0)) == LABEL_REF)
1231
            return "lrw\t%0,[%1]";              /* a-R */
1232
          else
1233
            switch (GET_MODE (src))             /* r-m */
1234
              {
1235
              case SImode:
1236
                return "ldw\t%0,%1";
1237
              case HImode:
1238
                return "ld.h\t%0,%1";
1239
              case QImode:
1240
                return "ld.b\t%0,%1";
1241
              default:
1242
                gcc_unreachable ();
1243
              }
1244
        }
1245
      else if (GET_CODE (src) == CONST_INT)
1246
        {
1247
          int x, y;
1248
 
1249
          if (CONST_OK_FOR_I (INTVAL (src)))       /* r-I */
1250
            return "movi\t%0,%1";
1251
          else if (CONST_OK_FOR_M (INTVAL (src)))  /* r-M */
1252
            return "bgeni\t%0,%P1\t// %1 %x1";
1253
          else if (CONST_OK_FOR_N (INTVAL (src)))  /* r-N */
1254
            return "bmaski\t%0,%N1\t// %1 %x1";
1255
          else if (try_constant_tricks (INTVAL (src), &x, &y))     /* R-P */
1256
            return output_inline_const (SImode, operands);  /* 1-2 insns */
1257
          else
1258
            return "lrw\t%0,%x1\t// %1";        /* Get it from literal pool.  */
1259
        }
1260
      else
1261
        return "lrw\t%0, %1";                /* Into the literal pool.  */
1262
    }
1263
  else if (GET_CODE (dst) == MEM)               /* m-r */
1264
    switch (GET_MODE (dst))
1265
      {
1266
      case SImode:
1267
        return "stw\t%1,%0";
1268
      case HImode:
1269
        return "st.h\t%1,%0";
1270
      case QImode:
1271
        return "st.b\t%1,%0";
1272
      default:
1273
        gcc_unreachable ();
1274
      }
1275
 
1276
  gcc_unreachable ();
1277
}
1278
 
1279
/* Return a sequence of instructions to perform DI or DF move.
1280
   Since the MCORE cannot move a DI or DF in one instruction, we have
1281
   to take care when we see overlapping source and dest registers.  */
1282
 
1283
const char *
1284
mcore_output_movedouble (rtx operands[], enum machine_mode mode ATTRIBUTE_UNUSED)
1285
{
1286
  rtx dst = operands[0];
1287
  rtx src = operands[1];
1288
 
1289
  if (GET_CODE (dst) == REG)
1290
    {
1291
      if (GET_CODE (src) == REG)
1292
        {
1293
          int dstreg = REGNO (dst);
1294
          int srcreg = REGNO (src);
1295
 
1296
          /* Ensure the second source not overwritten.  */
1297
          if (srcreg + 1 == dstreg)
1298
            return "mov %R0,%R1\n\tmov  %0,%1";
1299
          else
1300
            return "mov %0,%1\n\tmov    %R0,%R1";
1301
        }
1302
      else if (GET_CODE (src) == MEM)
1303
        {
1304
          rtx memexp = memexp = XEXP (src, 0);
1305
          int dstreg = REGNO (dst);
1306
          int basereg = -1;
1307
 
1308
          if (GET_CODE (memexp) == LABEL_REF)
1309
            return "lrw\t%0,[%1]\n\tlrw\t%R0,[%R1]";
1310
          else if (GET_CODE (memexp) == REG)
1311
            basereg = REGNO (memexp);
1312
          else if (GET_CODE (memexp) == PLUS)
1313
            {
1314
              if (GET_CODE (XEXP (memexp, 0)) == REG)
1315
                basereg = REGNO (XEXP (memexp, 0));
1316
              else if (GET_CODE (XEXP (memexp, 1)) == REG)
1317
                basereg = REGNO (XEXP (memexp, 1));
1318
              else
1319
                gcc_unreachable ();
1320
            }
1321
          else
1322
            gcc_unreachable ();
1323
 
1324
          /* ??? length attribute is wrong here.  */
1325
          if (dstreg == basereg)
1326
            {
1327
              /* Just load them in reverse order.  */
1328
              return "ldw\t%R0,%R1\n\tldw\t%0,%1";
1329
 
1330
              /* XXX: alternative: move basereg to basereg+1
1331
                 and then fall through.  */
1332
            }
1333
          else
1334
            return "ldw\t%0,%1\n\tldw\t%R0,%R1";
1335
        }
1336
      else if (GET_CODE (src) == CONST_INT)
1337
        {
1338
          if (TARGET_LITTLE_END)
1339
            {
1340
              if (CONST_OK_FOR_I (INTVAL (src)))
1341
                output_asm_insn ("movi  %0,%1", operands);
1342
              else if (CONST_OK_FOR_M (INTVAL (src)))
1343
                output_asm_insn ("bgeni %0,%P1", operands);
1344
              else if (INTVAL (src) == -1)
1345
                output_asm_insn ("bmaski        %0,32", operands);
1346
              else if (CONST_OK_FOR_N (INTVAL (src)))
1347
                output_asm_insn ("bmaski        %0,%N1", operands);
1348
              else
1349
                gcc_unreachable ();
1350
 
1351
              if (INTVAL (src) < 0)
1352
                return "bmaski  %R0,32";
1353
              else
1354
                return "movi    %R0,0";
1355
            }
1356
          else
1357
            {
1358
              if (CONST_OK_FOR_I (INTVAL (src)))
1359
                output_asm_insn ("movi  %R0,%1", operands);
1360
              else if (CONST_OK_FOR_M (INTVAL (src)))
1361
                output_asm_insn ("bgeni %R0,%P1", operands);
1362
              else if (INTVAL (src) == -1)
1363
                output_asm_insn ("bmaski        %R0,32", operands);
1364
              else if (CONST_OK_FOR_N (INTVAL (src)))
1365
                output_asm_insn ("bmaski        %R0,%N1", operands);
1366
              else
1367
                gcc_unreachable ();
1368
 
1369
              if (INTVAL (src) < 0)
1370
                return "bmaski  %0,32";
1371
              else
1372
                return "movi    %0,0";
1373
            }
1374
        }
1375
      else
1376
        gcc_unreachable ();
1377
    }
1378
  else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
1379
    return "stw\t%1,%0\n\tstw\t%R1,%R0";
1380
  else
1381
    gcc_unreachable ();
1382
}
1383
 
1384
/* Predicates used by the templates.  */
1385
 
1386
int
1387
mcore_arith_S_operand (rtx op)
1388
{
1389
  if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (~INTVAL (op)))
1390
    return 1;
1391
 
1392
  return 0;
1393
}
1394
 
1395
/* Expand insert bit field.  BRC  */
1396
 
1397
int
1398
mcore_expand_insv (rtx operands[])
1399
{
1400
  int width = INTVAL (operands[1]);
1401
  int posn = INTVAL (operands[2]);
1402
  int mask;
1403
  rtx mreg, sreg, ereg;
1404
 
1405
  /* To get width 1 insv, the test in store_bit_field() (expmed.c, line 191)
1406
     for width==1 must be removed.  Look around line 368.  This is something
1407
     we really want the md part to do.  */
1408
  if (width == 1 && GET_CODE (operands[3]) == CONST_INT)
1409
    {
1410
      /* Do directly with bseti or bclri.  */
1411
      /* RBE: 2/97 consider only low bit of constant.  */
1412
      if ((INTVAL(operands[3])&1) == 0)
1413
        {
1414
          mask = ~(1 << posn);
1415
          emit_insn (gen_rtx_SET (SImode, operands[0],
1416
                              gen_rtx_AND (SImode, operands[0], GEN_INT (mask))));
1417
        }
1418
      else
1419
        {
1420
          mask = 1 << posn;
1421
          emit_insn (gen_rtx_SET (SImode, operands[0],
1422
                            gen_rtx_IOR (SImode, operands[0], GEN_INT (mask))));
1423
        }
1424
 
1425
      return 1;
1426
    }
1427
 
1428
  /* Look at some bit-field placements that we aren't interested
1429
     in handling ourselves, unless specifically directed to do so.  */
1430
  if (! TARGET_W_FIELD)
1431
    return 0;            /* Generally, give up about now.  */
1432
 
1433
  if (width == 8 && posn % 8 == 0)
1434
    /* Byte sized and aligned; let caller break it up.  */
1435
    return 0;
1436
 
1437
  if (width == 16 && posn % 16 == 0)
1438
    /* Short sized and aligned; let caller break it up.  */
1439
    return 0;
1440
 
1441
  /* The general case - we can do this a little bit better than what the
1442
     machine independent part tries.  This will get rid of all the subregs
1443
     that mess up constant folding in combine when working with relaxed
1444
     immediates.  */
1445
 
1446
  /* If setting the entire field, do it directly.  */
1447
  if (GET_CODE (operands[3]) == CONST_INT &&
1448
      INTVAL (operands[3]) == ((1 << width) - 1))
1449
    {
1450
      mreg = force_reg (SImode, GEN_INT (INTVAL (operands[3]) << posn));
1451
      emit_insn (gen_rtx_SET (SImode, operands[0],
1452
                         gen_rtx_IOR (SImode, operands[0], mreg)));
1453
      return 1;
1454
    }
1455
 
1456
  /* Generate the clear mask.  */
1457
  mreg = force_reg (SImode, GEN_INT (~(((1 << width) - 1) << posn)));
1458
 
1459
  /* Clear the field, to overlay it later with the source.  */
1460
  emit_insn (gen_rtx_SET (SImode, operands[0],
1461
                      gen_rtx_AND (SImode, operands[0], mreg)));
1462
 
1463
  /* If the source is constant 0, we've nothing to add back.  */
1464
  if (GET_CODE (operands[3]) == CONST_INT && INTVAL (operands[3]) == 0)
1465
    return 1;
1466
 
1467
  /* XXX: Should we worry about more games with constant values?
1468
     We've covered the high profile: set/clear single-bit and many-bit
1469
     fields. How often do we see "arbitrary bit pattern" constants?  */
1470
  sreg = copy_to_mode_reg (SImode, operands[3]);
1471
 
1472
  /* Extract src as same width as dst (needed for signed values).  We
1473
     always have to do this since we widen everything to SImode.
1474
     We don't have to mask if we're shifting this up against the
1475
     MSB of the register (e.g., the shift will push out any hi-order
1476
     bits.  */
1477
  if (width + posn != (int) GET_MODE_SIZE (SImode))
1478
    {
1479
      ereg = force_reg (SImode, GEN_INT ((1 << width) - 1));
1480
      emit_insn (gen_rtx_SET (SImode, sreg,
1481
                          gen_rtx_AND (SImode, sreg, ereg)));
1482
    }
1483
 
1484
  /* Insert source value in dest.  */
1485
  if (posn != 0)
1486
    emit_insn (gen_rtx_SET (SImode, sreg,
1487
                        gen_rtx_ASHIFT (SImode, sreg, GEN_INT (posn))));
1488
 
1489
  emit_insn (gen_rtx_SET (SImode, operands[0],
1490
                      gen_rtx_IOR (SImode, operands[0], sreg)));
1491
 
1492
  return 1;
1493
}
1494
 
1495
/* ??? Block move stuff stolen from m88k.  This code has not been
1496
   verified for correctness.  */
1497
 
1498
/* Emit code to perform a block move.  Choose the best method.
1499
 
1500
   OPERANDS[0] is the destination.
1501
   OPERANDS[1] is the source.
1502
   OPERANDS[2] is the size.
1503
   OPERANDS[3] is the alignment safe to use.  */
1504
 
1505
/* Emit code to perform a block move with an offset sequence of ldw/st
1506
   instructions (..., ldw 0, stw 1, ldw 1, stw 0, ...).  SIZE and ALIGN are
1507
   known constants.  DEST and SRC are registers.  OFFSET is the known
1508
   starting point for the output pattern.  */
1509
 
1510
static const enum machine_mode mode_from_align[] =
1511
{
1512
  VOIDmode, QImode, HImode, VOIDmode, SImode,
1513
};
1514
 
1515
static void
1516
block_move_sequence (rtx dst_mem, rtx src_mem, int size, int align)
1517
{
1518
  rtx temp[2];
1519
  enum machine_mode mode[2];
1520
  int amount[2];
1521
  bool active[2];
1522
  int phase = 0;
1523
  int next;
1524
  int offset_ld = 0;
1525
  int offset_st = 0;
1526
  rtx x;
1527
 
1528
  x = XEXP (dst_mem, 0);
1529
  if (!REG_P (x))
1530
    {
1531
      x = force_reg (Pmode, x);
1532
      dst_mem = replace_equiv_address (dst_mem, x);
1533
    }
1534
 
1535
  x = XEXP (src_mem, 0);
1536
  if (!REG_P (x))
1537
    {
1538
      x = force_reg (Pmode, x);
1539
      src_mem = replace_equiv_address (src_mem, x);
1540
    }
1541
 
1542
  active[0] = active[1] = false;
1543
 
1544
  do
1545
    {
1546
      next = phase;
1547
      phase ^= 1;
1548
 
1549
      if (size > 0)
1550
        {
1551
          int next_amount;
1552
 
1553
          next_amount = (size >= 4 ? 4 : (size >= 2 ? 2 : 1));
1554
          next_amount = MIN (next_amount, align);
1555
 
1556
          amount[next] = next_amount;
1557
          mode[next] = mode_from_align[next_amount];
1558
          temp[next] = gen_reg_rtx (mode[next]);
1559
 
1560
          x = adjust_address (src_mem, mode[next], offset_ld);
1561
          emit_insn (gen_rtx_SET (VOIDmode, temp[next], x));
1562
 
1563
          offset_ld += next_amount;
1564
          size -= next_amount;
1565
          active[next] = true;
1566
        }
1567
 
1568
      if (active[phase])
1569
        {
1570
          active[phase] = false;
1571
 
1572
          x = adjust_address (dst_mem, mode[phase], offset_st);
1573
          emit_insn (gen_rtx_SET (VOIDmode, x, temp[phase]));
1574
 
1575
          offset_st += amount[phase];
1576
        }
1577
    }
1578
  while (active[next]);
1579
}
1580
 
1581
bool
1582
mcore_expand_block_move (rtx *operands)
1583
{
1584
  HOST_WIDE_INT align, bytes, max;
1585
 
1586
  if (GET_CODE (operands[2]) != CONST_INT)
1587
    return false;
1588
 
1589
  bytes = INTVAL (operands[2]);
1590
  align = INTVAL (operands[3]);
1591
 
1592
  if (bytes <= 0)
1593
    return false;
1594
  if (align > 4)
1595
    align = 4;
1596
 
1597
  switch (align)
1598
    {
1599
    case 4:
1600
      if (bytes & 1)
1601
        max = 4*4;
1602
      else if (bytes & 3)
1603
        max = 8*4;
1604
      else
1605
        max = 16*4;
1606
      break;
1607
    case 2:
1608
      max = 4*2;
1609
      break;
1610
    case 1:
1611
      max = 4*1;
1612
      break;
1613
    default:
1614
      gcc_unreachable ();
1615
    }
1616
 
1617
  if (bytes <= max)
1618
    {
1619
      block_move_sequence (operands[0], operands[1], bytes, align);
1620
      return true;
1621
    }
1622
 
1623
  return false;
1624
}
1625
 
1626
 
1627
/* Code to generate prologue and epilogue sequences.  */
1628
static int number_of_regs_before_varargs;
1629
 
1630
/* Set by TARGET_SETUP_INCOMING_VARARGS to indicate to prolog that this is
1631
   for a varargs function.  */
1632
static int current_function_anonymous_args;
1633
 
1634
#define STACK_BYTES (STACK_BOUNDARY/BITS_PER_UNIT)
1635
#define STORE_REACH (64)        /* Maximum displace of word store + 4.  */
1636
#define ADDI_REACH (32)         /* Maximum addi operand.  */
1637
 
1638
static void
1639
layout_mcore_frame (struct mcore_frame * infp)
1640
{
1641
  int n;
1642
  unsigned int i;
1643
  int nbytes;
1644
  int regarg;
1645
  int localregarg;
1646
  int localreg;
1647
  int outbounds;
1648
  unsigned int growths;
1649
  int step;
1650
 
1651
  /* Might have to spill bytes to re-assemble a big argument that
1652
     was passed partially in registers and partially on the stack.  */
1653
  nbytes = current_function_pretend_args_size;
1654
 
1655
  /* Determine how much space for spilled anonymous args (e.g., stdarg).  */
1656
  if (current_function_anonymous_args)
1657
    nbytes += (NPARM_REGS - number_of_regs_before_varargs) * UNITS_PER_WORD;
1658
 
1659
  infp->arg_size = nbytes;
1660
 
1661
  /* How much space to save non-volatile registers we stomp.  */
1662
  infp->reg_mask = calc_live_regs (& n);
1663
  infp->reg_size = n * 4;
1664
 
1665
  /* And the rest of it... locals and space for overflowed outbounds.  */
1666
  infp->local_size = get_frame_size ();
1667
  infp->outbound_size = current_function_outgoing_args_size;
1668
 
1669
  /* Make sure we have a whole number of words for the locals.  */
1670
  if (infp->local_size % STACK_BYTES)
1671
    infp->local_size = (infp->local_size + STACK_BYTES - 1) & ~ (STACK_BYTES -1);
1672
 
1673
  /* Only thing we know we have to pad is the outbound space, since
1674
     we've aligned our locals assuming that base of locals is aligned.  */
1675
  infp->pad_local = 0;
1676
  infp->pad_reg = 0;
1677
  infp->pad_outbound = 0;
1678
  if (infp->outbound_size % STACK_BYTES)
1679
    infp->pad_outbound = STACK_BYTES - (infp->outbound_size % STACK_BYTES);
1680
 
1681
  /* Now we see how we want to stage the prologue so that it does
1682
     the most appropriate stack growth and register saves to either:
1683
     (1) run fast,
1684
     (2) reduce instruction space, or
1685
     (3) reduce stack space.  */
1686
  for (i = 0; i < ARRAY_SIZE (infp->growth); i++)
1687
    infp->growth[i] = 0;
1688
 
1689
  regarg      = infp->reg_size + infp->arg_size;
1690
  localregarg = infp->local_size + regarg;
1691
  localreg    = infp->local_size + infp->reg_size;
1692
  outbounds   = infp->outbound_size + infp->pad_outbound;
1693
  growths     = 0;
1694
 
1695
  /* XXX: Consider one where we consider localregarg + outbound too! */
1696
 
1697
  /* Frame of <= 32 bytes and using stm would get <= 2 registers.
1698
     use stw's with offsets and buy the frame in one shot.  */
1699
  if (localregarg <= ADDI_REACH
1700
      && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
1701
    {
1702
      /* Make sure we'll be aligned.  */
1703
      if (localregarg % STACK_BYTES)
1704
        infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
1705
 
1706
      step = localregarg + infp->pad_reg;
1707
      infp->reg_offset = infp->local_size;
1708
 
1709
      if (outbounds + step <= ADDI_REACH && !frame_pointer_needed)
1710
        {
1711
          step += outbounds;
1712
          infp->reg_offset += outbounds;
1713
          outbounds = 0;
1714
        }
1715
 
1716
      infp->arg_offset = step - 4;
1717
      infp->growth[growths++] = step;
1718
      infp->reg_growth = growths;
1719
      infp->local_growth = growths;
1720
 
1721
      /* If we haven't already folded it in.  */
1722
      if (outbounds)
1723
        infp->growth[growths++] = outbounds;
1724
 
1725
      goto finish;
1726
    }
1727
 
1728
  /* Frame can't be done with a single subi, but can be done with 2
1729
     insns.  If the 'stm' is getting <= 2 registers, we use stw's and
1730
     shift some of the stack purchase into the first subi, so both are
1731
     single instructions.  */
1732
  if (localregarg <= STORE_REACH
1733
      && (infp->local_size > ADDI_REACH)
1734
      && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
1735
    {
1736
      int all;
1737
 
1738
      /* Make sure we'll be aligned; use either pad_reg or pad_local.  */
1739
      if (localregarg % STACK_BYTES)
1740
        infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
1741
 
1742
      all = localregarg + infp->pad_reg + infp->pad_local;
1743
      step = ADDI_REACH;        /* As much up front as we can.  */
1744
      if (step > all)
1745
        step = all;
1746
 
1747
      /* XXX: Consider whether step will still be aligned; we believe so.  */
1748
      infp->arg_offset = step - 4;
1749
      infp->growth[growths++] = step;
1750
      infp->reg_growth = growths;
1751
      infp->reg_offset = step - infp->pad_reg - infp->reg_size;
1752
      all -= step;
1753
 
1754
      /* Can we fold in any space required for outbounds?  */
1755
      if (outbounds + all <= ADDI_REACH && !frame_pointer_needed)
1756
        {
1757
          all += outbounds;
1758
          outbounds = 0;
1759
        }
1760
 
1761
      /* Get the rest of the locals in place.  */
1762
      step = all;
1763
      infp->growth[growths++] = step;
1764
      infp->local_growth = growths;
1765
      all -= step;
1766
 
1767
      assert (all == 0);
1768
 
1769
      /* Finish off if we need to do so.  */
1770
      if (outbounds)
1771
        infp->growth[growths++] = outbounds;
1772
 
1773
      goto finish;
1774
    }
1775
 
1776
  /* Registers + args is nicely aligned, so we'll buy that in one shot.
1777
     Then we buy the rest of the frame in 1 or 2 steps depending on
1778
     whether we need a frame pointer.  */
1779
  if ((regarg % STACK_BYTES) == 0)
1780
    {
1781
      infp->growth[growths++] = regarg;
1782
      infp->reg_growth = growths;
1783
      infp->arg_offset = regarg - 4;
1784
      infp->reg_offset = 0;
1785
 
1786
      if (infp->local_size % STACK_BYTES)
1787
        infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
1788
 
1789
      step = infp->local_size + infp->pad_local;
1790
 
1791
      if (!frame_pointer_needed)
1792
        {
1793
          step += outbounds;
1794
          outbounds = 0;
1795
        }
1796
 
1797
      infp->growth[growths++] = step;
1798
      infp->local_growth = growths;
1799
 
1800
      /* If there's any left to be done.  */
1801
      if (outbounds)
1802
        infp->growth[growths++] = outbounds;
1803
 
1804
      goto finish;
1805
    }
1806
 
1807
  /* XXX: optimizations that we'll want to play with....
1808
     -- regarg is not aligned, but it's a small number of registers;
1809
        use some of localsize so that regarg is aligned and then
1810
        save the registers.  */
1811
 
1812
  /* Simple encoding; plods down the stack buying the pieces as it goes.
1813
     -- does not optimize space consumption.
1814
     -- does not attempt to optimize instruction counts.
1815
     -- but it is safe for all alignments.  */
1816
  if (regarg % STACK_BYTES != 0)
1817
    infp->pad_reg = STACK_BYTES - (regarg % STACK_BYTES);
1818
 
1819
  infp->growth[growths++] = infp->arg_size + infp->reg_size + infp->pad_reg;
1820
  infp->reg_growth = growths;
1821
  infp->arg_offset = infp->growth[0] - 4;
1822
  infp->reg_offset = 0;
1823
 
1824
  if (frame_pointer_needed)
1825
    {
1826
      if (infp->local_size % STACK_BYTES != 0)
1827
        infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
1828
 
1829
      infp->growth[growths++] = infp->local_size + infp->pad_local;
1830
      infp->local_growth = growths;
1831
 
1832
      infp->growth[growths++] = outbounds;
1833
    }
1834
  else
1835
    {
1836
      if ((infp->local_size + outbounds) % STACK_BYTES != 0)
1837
        infp->pad_local = STACK_BYTES - ((infp->local_size + outbounds) % STACK_BYTES);
1838
 
1839
      infp->growth[growths++] = infp->local_size + infp->pad_local + outbounds;
1840
      infp->local_growth = growths;
1841
    }
1842
 
1843
  /* Anything else that we've forgotten?, plus a few consistency checks.  */
1844
 finish:
1845
  assert (infp->reg_offset >= 0);
1846
  assert (growths <= MAX_STACK_GROWS);
1847
 
1848
  for (i = 0; i < growths; i++)
1849
    gcc_assert (!(infp->growth[i] % STACK_BYTES));
1850
}
1851
 
1852
/* Define the offset between two registers, one to be eliminated, and
1853
   the other its replacement, at the start of a routine.  */
1854
 
1855
int
1856
mcore_initial_elimination_offset (int from, int to)
1857
{
1858
  int above_frame;
1859
  int below_frame;
1860
  struct mcore_frame fi;
1861
 
1862
  layout_mcore_frame (& fi);
1863
 
1864
  /* fp to ap */
1865
  above_frame = fi.local_size + fi.pad_local + fi.reg_size + fi.pad_reg;
1866
  /* sp to fp */
1867
  below_frame = fi.outbound_size + fi.pad_outbound;
1868
 
1869
  if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1870
    return above_frame;
1871
 
1872
  if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1873
    return above_frame + below_frame;
1874
 
1875
  if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1876
    return below_frame;
1877
 
1878
  gcc_unreachable ();
1879
}
1880
 
1881
/* Keep track of some information about varargs for the prolog.  */
1882
 
1883
static void
1884
mcore_setup_incoming_varargs (CUMULATIVE_ARGS *args_so_far,
1885
                              enum machine_mode mode, tree type,
1886
                              int * ptr_pretend_size ATTRIBUTE_UNUSED,
1887
                              int second_time ATTRIBUTE_UNUSED)
1888
{
1889
  current_function_anonymous_args = 1;
1890
 
1891
  /* We need to know how many argument registers are used before
1892
     the varargs start, so that we can push the remaining argument
1893
     registers during the prologue.  */
1894
  number_of_regs_before_varargs = *args_so_far + mcore_num_arg_regs (mode, type);
1895
 
1896
  /* There is a bug somewhere in the arg handling code.
1897
     Until I can find it this workaround always pushes the
1898
     last named argument onto the stack.  */
1899
  number_of_regs_before_varargs = *args_so_far;
1900
 
1901
  /* The last named argument may be split between argument registers
1902
     and the stack.  Allow for this here.  */
1903
  if (number_of_regs_before_varargs > NPARM_REGS)
1904
    number_of_regs_before_varargs = NPARM_REGS;
1905
}
1906
 
1907
void
1908
mcore_expand_prolog (void)
1909
{
1910
  struct mcore_frame fi;
1911
  int space_allocated = 0;
1912
  int growth = 0;
1913
 
1914
  /* Find out what we're doing.  */
1915
  layout_mcore_frame (&fi);
1916
 
1917
  space_allocated = fi.arg_size + fi.reg_size + fi.local_size +
1918
    fi.outbound_size + fi.pad_outbound + fi.pad_local + fi.pad_reg;
1919
 
1920
  if (TARGET_CG_DATA)
1921
    {
1922
      /* Emit a symbol for this routine's frame size.  */
1923
      rtx x;
1924
 
1925
      x = DECL_RTL (current_function_decl);
1926
 
1927
      gcc_assert (GET_CODE (x) == MEM);
1928
 
1929
      x = XEXP (x, 0);
1930
 
1931
      gcc_assert (GET_CODE (x) == SYMBOL_REF);
1932
 
1933
      if (mcore_current_function_name)
1934
        free (mcore_current_function_name);
1935
 
1936
      mcore_current_function_name = xstrdup (XSTR (x, 0));
1937
 
1938
      ASM_OUTPUT_CG_NODE (asm_out_file, mcore_current_function_name, space_allocated);
1939
 
1940
      if (current_function_calls_alloca)
1941
        ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, "alloca", 1);
1942
 
1943
      /* 970425: RBE:
1944
         We're looking at how the 8byte alignment affects stack layout
1945
         and where we had to pad things. This emits information we can
1946
         extract which tells us about frame sizes and the like.  */
1947
      fprintf (asm_out_file,
1948
               "\t.equ\t__$frame$info$_%s_$_%d_%d_x%x_%d_%d_%d,0\n",
1949
               mcore_current_function_name,
1950
               fi.arg_size, fi.reg_size, fi.reg_mask,
1951
               fi.local_size, fi.outbound_size,
1952
               frame_pointer_needed);
1953
    }
1954
 
1955
  if (mcore_naked_function_p ())
1956
    return;
1957
 
1958
  /* Handle stdarg+regsaves in one shot: can't be more than 64 bytes.  */
1959
  output_stack_adjust (-1, fi.growth[growth++]);        /* Grows it.  */
1960
 
1961
  /* If we have a parameter passed partially in regs and partially in memory,
1962
     the registers will have been stored to memory already in function.c.  So
1963
     we only need to do something here for varargs functions.  */
1964
  if (fi.arg_size != 0 && current_function_pretend_args_size == 0)
1965
    {
1966
      int offset;
1967
      int rn = FIRST_PARM_REG + NPARM_REGS - 1;
1968
      int remaining = fi.arg_size;
1969
 
1970
      for (offset = fi.arg_offset; remaining >= 4; offset -= 4, rn--, remaining -= 4)
1971
        {
1972
          emit_insn (gen_movsi
1973
                     (gen_rtx_MEM (SImode,
1974
                               plus_constant (stack_pointer_rtx, offset)),
1975
                      gen_rtx_REG (SImode, rn)));
1976
        }
1977
    }
1978
 
1979
  /* Do we need another stack adjustment before we do the register saves?  */
1980
  if (growth < fi.reg_growth)
1981
    output_stack_adjust (-1, fi.growth[growth++]);              /* Grows it.  */
1982
 
1983
  if (fi.reg_size != 0)
1984
    {
1985
      int i;
1986
      int offs = fi.reg_offset;
1987
 
1988
      for (i = 15; i >= 0; i--)
1989
        {
1990
          if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
1991
            {
1992
              int first_reg = 15;
1993
 
1994
              while (fi.reg_mask & (1 << first_reg))
1995
                first_reg--;
1996
              first_reg++;
1997
 
1998
              emit_insn (gen_store_multiple (gen_rtx_MEM (SImode, stack_pointer_rtx),
1999
                                             gen_rtx_REG (SImode, first_reg),
2000
                                             GEN_INT (16 - first_reg)));
2001
 
2002
              i -= (15 - first_reg);
2003
              offs += (16 - first_reg) * 4;
2004
            }
2005
          else if (fi.reg_mask & (1 << i))
2006
            {
2007
              emit_insn (gen_movsi
2008
                         (gen_rtx_MEM (SImode,
2009
                                   plus_constant (stack_pointer_rtx, offs)),
2010
                          gen_rtx_REG (SImode, i)));
2011
              offs += 4;
2012
            }
2013
        }
2014
    }
2015
 
2016
  /* Figure the locals + outbounds.  */
2017
  if (frame_pointer_needed)
2018
    {
2019
      /* If we haven't already purchased to 'fp'.  */
2020
      if (growth < fi.local_growth)
2021
        output_stack_adjust (-1, fi.growth[growth++]);          /* Grows it.  */
2022
 
2023
      emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
2024
 
2025
      /* ... and then go any remaining distance for outbounds, etc.  */
2026
      if (fi.growth[growth])
2027
        output_stack_adjust (-1, fi.growth[growth++]);
2028
    }
2029
  else
2030
    {
2031
      if (growth < fi.local_growth)
2032
        output_stack_adjust (-1, fi.growth[growth++]);          /* Grows it.  */
2033
      if (fi.growth[growth])
2034
        output_stack_adjust (-1, fi.growth[growth++]);
2035
    }
2036
}
2037
 
2038
void
2039
mcore_expand_epilog (void)
2040
{
2041
  struct mcore_frame fi;
2042
  int i;
2043
  int offs;
2044
  int growth = MAX_STACK_GROWS - 1 ;
2045
 
2046
 
2047
  /* Find out what we're doing.  */
2048
  layout_mcore_frame(&fi);
2049
 
2050
  if (mcore_naked_function_p ())
2051
    return;
2052
 
2053
  /* If we had a frame pointer, restore the sp from that.  */
2054
  if (frame_pointer_needed)
2055
    {
2056
      emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
2057
      growth = fi.local_growth - 1;
2058
    }
2059
  else
2060
    {
2061
      /* XXX: while loop should accumulate and do a single sell.  */
2062
      while (growth >= fi.local_growth)
2063
        {
2064
          if (fi.growth[growth] != 0)
2065
            output_stack_adjust (1, fi.growth[growth]);
2066
          growth--;
2067
        }
2068
    }
2069
 
2070
  /* Make sure we've shrunk stack back to the point where the registers
2071
     were laid down. This is typically 0/1 iterations.  Then pull the
2072
     register save information back off the stack.  */
2073
  while (growth >= fi.reg_growth)
2074
    output_stack_adjust ( 1, fi.growth[growth--]);
2075
 
2076
  offs = fi.reg_offset;
2077
 
2078
  for (i = 15; i >= 0; i--)
2079
    {
2080
      if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2081
        {
2082
          int first_reg;
2083
 
2084
          /* Find the starting register.  */
2085
          first_reg = 15;
2086
 
2087
          while (fi.reg_mask & (1 << first_reg))
2088
            first_reg--;
2089
 
2090
          first_reg++;
2091
 
2092
          emit_insn (gen_load_multiple (gen_rtx_REG (SImode, first_reg),
2093
                                        gen_rtx_MEM (SImode, stack_pointer_rtx),
2094
                                        GEN_INT (16 - first_reg)));
2095
 
2096
          i -= (15 - first_reg);
2097
          offs += (16 - first_reg) * 4;
2098
        }
2099
      else if (fi.reg_mask & (1 << i))
2100
        {
2101
          emit_insn (gen_movsi
2102
                     (gen_rtx_REG (SImode, i),
2103
                      gen_rtx_MEM (SImode,
2104
                               plus_constant (stack_pointer_rtx, offs))));
2105
          offs += 4;
2106
        }
2107
    }
2108
 
2109
  /* Give back anything else.  */
2110
  /* XXX: Should accumulate total and then give it back.  */
2111
  while (growth >= 0)
2112
    output_stack_adjust ( 1, fi.growth[growth--]);
2113
}
2114
 
2115
/* This code is borrowed from the SH port.  */
2116
 
2117
/* The MCORE cannot load a large constant into a register, constants have to
2118
   come from a pc relative load.  The reference of a pc relative load
2119
   instruction must be less than 1k in front of the instruction.  This
2120
   means that we often have to dump a constant inside a function, and
2121
   generate code to branch around it.
2122
 
2123
   It is important to minimize this, since the branches will slow things
2124
   down and make things bigger.
2125
 
2126
   Worst case code looks like:
2127
 
2128
   lrw   L1,r0
2129
   br    L2
2130
   align
2131
   L1:   .long value
2132
   L2:
2133
   ..
2134
 
2135
   lrw   L3,r0
2136
   br    L4
2137
   align
2138
   L3:   .long value
2139
   L4:
2140
   ..
2141
 
2142
   We fix this by performing a scan before scheduling, which notices which
2143
   instructions need to have their operands fetched from the constant table
2144
   and builds the table.
2145
 
2146
   The algorithm is:
2147
 
2148
   scan, find an instruction which needs a pcrel move.  Look forward, find the
2149
   last barrier which is within MAX_COUNT bytes of the requirement.
2150
   If there isn't one, make one.  Process all the instructions between
2151
   the find and the barrier.
2152
 
2153
   In the above example, we can tell that L3 is within 1k of L1, so
2154
   the first move can be shrunk from the 2 insn+constant sequence into
2155
   just 1 insn, and the constant moved to L3 to make:
2156
 
2157
   lrw          L1,r0
2158
   ..
2159
   lrw          L3,r0
2160
   bra          L4
2161
   align
2162
   L3:.long value
2163
   L4:.long value
2164
 
2165
   Then the second move becomes the target for the shortening process.  */
2166
 
2167
typedef struct
2168
{
2169
  rtx value;                    /* Value in table.  */
2170
  rtx label;                    /* Label of value.  */
2171
} pool_node;
2172
 
2173
/* The maximum number of constants that can fit into one pool, since
2174
   the pc relative range is 0...1020 bytes and constants are at least 4
2175
   bytes long.  We subtract 4 from the range to allow for the case where
2176
   we need to add a branch/align before the constant pool.  */
2177
 
2178
#define MAX_COUNT 1016
2179
#define MAX_POOL_SIZE (MAX_COUNT/4)
2180
static pool_node pool_vector[MAX_POOL_SIZE];
2181
static int pool_size;
2182
 
2183
/* Dump out any constants accumulated in the final pass.  These
2184
   will only be labels.  */
2185
 
2186
const char *
2187
mcore_output_jump_label_table (void)
2188
{
2189
  int i;
2190
 
2191
  if (pool_size)
2192
    {
2193
      fprintf (asm_out_file, "\t.align 2\n");
2194
 
2195
      for (i = 0; i < pool_size; i++)
2196
        {
2197
          pool_node * p = pool_vector + i;
2198
 
2199
          (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (p->label));
2200
 
2201
          output_asm_insn (".long       %0", &p->value);
2202
        }
2203
 
2204
      pool_size = 0;
2205
    }
2206
 
2207
  return "";
2208
}
2209
 
2210
/* Check whether insn is a candidate for a conditional.  */
2211
 
2212
static cond_type
2213
is_cond_candidate (rtx insn)
2214
{
2215
  /* The only things we conditionalize are those that can be directly
2216
     changed into a conditional.  Only bother with SImode items.  If
2217
     we wanted to be a little more aggressive, we could also do other
2218
     modes such as DImode with reg-reg move or load 0.  */
2219
  if (GET_CODE (insn) == INSN)
2220
    {
2221
      rtx pat = PATTERN (insn);
2222
      rtx src, dst;
2223
 
2224
      if (GET_CODE (pat) != SET)
2225
        return COND_NO;
2226
 
2227
      dst = XEXP (pat, 0);
2228
 
2229
      if ((GET_CODE (dst) != REG &&
2230
           GET_CODE (dst) != SUBREG) ||
2231
          GET_MODE (dst) != SImode)
2232
        return COND_NO;
2233
 
2234
      src = XEXP (pat, 1);
2235
 
2236
      if ((GET_CODE (src) == REG ||
2237
           (GET_CODE (src) == SUBREG &&
2238
            GET_CODE (SUBREG_REG (src)) == REG)) &&
2239
          GET_MODE (src) == SImode)
2240
        return COND_MOV_INSN;
2241
      else if (GET_CODE (src) == CONST_INT &&
2242
               INTVAL (src) == 0)
2243
        return COND_CLR_INSN;
2244
      else if (GET_CODE (src) == PLUS &&
2245
               (GET_CODE (XEXP (src, 0)) == REG ||
2246
                (GET_CODE (XEXP (src, 0)) == SUBREG &&
2247
                 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2248
               GET_MODE (XEXP (src, 0)) == SImode &&
2249
               GET_CODE (XEXP (src, 1)) == CONST_INT &&
2250
               INTVAL (XEXP (src, 1)) == 1)
2251
        return COND_INC_INSN;
2252
      else if (((GET_CODE (src) == MINUS &&
2253
                 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2254
                 INTVAL( XEXP (src, 1)) == 1) ||
2255
                (GET_CODE (src) == PLUS &&
2256
                 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2257
                 INTVAL (XEXP (src, 1)) == -1)) &&
2258
               (GET_CODE (XEXP (src, 0)) == REG ||
2259
                (GET_CODE (XEXP (src, 0)) == SUBREG &&
2260
                 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2261
               GET_MODE (XEXP (src, 0)) == SImode)
2262
        return COND_DEC_INSN;
2263
 
2264
      /* Some insns that we don't bother with:
2265
         (set (rx:DI) (ry:DI))
2266
         (set (rx:DI) (const_int 0))
2267
      */
2268
 
2269
    }
2270
  else if (GET_CODE (insn) == JUMP_INSN &&
2271
           GET_CODE (PATTERN (insn)) == SET &&
2272
           GET_CODE (XEXP (PATTERN (insn), 1)) == LABEL_REF)
2273
    return COND_BRANCH_INSN;
2274
 
2275
  return COND_NO;
2276
}
2277
 
2278
/* Emit a conditional version of insn and replace the old insn with the
2279
   new one.  Return the new insn if emitted.  */
2280
 
2281
static rtx
2282
emit_new_cond_insn (rtx insn, int cond)
2283
{
2284
  rtx c_insn = 0;
2285
  rtx pat, dst, src;
2286
  cond_type num;
2287
 
2288
  if ((num = is_cond_candidate (insn)) == COND_NO)
2289
    return NULL;
2290
 
2291
  pat = PATTERN (insn);
2292
 
2293
  if (GET_CODE (insn) == INSN)
2294
    {
2295
      dst = SET_DEST (pat);
2296
      src = SET_SRC (pat);
2297
    }
2298
  else
2299
    {
2300
      dst = JUMP_LABEL (insn);
2301
      src = NULL_RTX;
2302
    }
2303
 
2304
  switch (num)
2305
    {
2306
    case COND_MOV_INSN:
2307
    case COND_CLR_INSN:
2308
      if (cond)
2309
        c_insn = gen_movt0 (dst, src, dst);
2310
      else
2311
        c_insn = gen_movt0 (dst, dst, src);
2312
      break;
2313
 
2314
    case COND_INC_INSN:
2315
      if (cond)
2316
        c_insn = gen_incscc (dst, dst);
2317
      else
2318
        c_insn = gen_incscc_false (dst, dst);
2319
      break;
2320
 
2321
    case COND_DEC_INSN:
2322
      if (cond)
2323
        c_insn = gen_decscc (dst, dst);
2324
      else
2325
        c_insn = gen_decscc_false (dst, dst);
2326
      break;
2327
 
2328
    case COND_BRANCH_INSN:
2329
      if (cond)
2330
        c_insn = gen_branch_true (dst);
2331
      else
2332
        c_insn = gen_branch_false (dst);
2333
      break;
2334
 
2335
    default:
2336
      return NULL;
2337
    }
2338
 
2339
  /* Only copy the notes if they exist.  */
2340
  if (rtx_length [GET_CODE (c_insn)] >= 7 && rtx_length [GET_CODE (insn)] >= 7)
2341
    {
2342
      /* We really don't need to bother with the notes and links at this
2343
         point, but go ahead and save the notes.  This will help is_dead()
2344
         when applying peepholes (links don't matter since they are not
2345
         used any more beyond this point for the mcore).  */
2346
      REG_NOTES (c_insn) = REG_NOTES (insn);
2347
    }
2348
 
2349
  if (num == COND_BRANCH_INSN)
2350
    {
2351
      /* For jumps, we need to be a little bit careful and emit the new jump
2352
         before the old one and to update the use count for the target label.
2353
         This way, the barrier following the old (uncond) jump will get
2354
         deleted, but the label won't.  */
2355
      c_insn = emit_jump_insn_before (c_insn, insn);
2356
 
2357
      ++ LABEL_NUSES (dst);
2358
 
2359
      JUMP_LABEL (c_insn) = dst;
2360
    }
2361
  else
2362
    c_insn = emit_insn_after (c_insn, insn);
2363
 
2364
  delete_insn (insn);
2365
 
2366
  return c_insn;
2367
}
2368
 
2369
/* Attempt to change a basic block into a series of conditional insns.  This
2370
   works by taking the branch at the end of the 1st block and scanning for the
2371
   end of the 2nd block.  If all instructions in the 2nd block have cond.
2372
   versions and the label at the start of block 3 is the same as the target
2373
   from the branch at block 1, then conditionalize all insn in block 2 using
2374
   the inverse condition of the branch at block 1.  (Note I'm bending the
2375
   definition of basic block here.)
2376
 
2377
   e.g., change:
2378
 
2379
                bt      L2             <-- end of block 1 (delete)
2380
                mov     r7,r8
2381
                addu    r7,1
2382
                br      L3             <-- end of block 2
2383
 
2384
        L2:     ...                    <-- start of block 3 (NUSES==1)
2385
        L3:     ...
2386
 
2387
   to:
2388
 
2389
                movf    r7,r8
2390
                incf    r7
2391
                bf      L3
2392
 
2393
        L3:     ...
2394
 
2395
   we can delete the L2 label if NUSES==1 and re-apply the optimization
2396
   starting at the last instruction of block 2.  This may allow an entire
2397
   if-then-else statement to be conditionalized.  BRC  */
2398
static rtx
2399
conditionalize_block (rtx first)
2400
{
2401
  rtx insn;
2402
  rtx br_pat;
2403
  rtx end_blk_1_br = 0;
2404
  rtx end_blk_2_insn = 0;
2405
  rtx start_blk_3_lab = 0;
2406
  int cond;
2407
  int br_lab_num;
2408
  int blk_size = 0;
2409
 
2410
 
2411
  /* Check that the first insn is a candidate conditional jump.  This is
2412
     the one that we'll eliminate.  If not, advance to the next insn to
2413
     try.  */
2414
  if (GET_CODE (first) != JUMP_INSN ||
2415
      GET_CODE (PATTERN (first)) != SET ||
2416
      GET_CODE (XEXP (PATTERN (first), 1)) != IF_THEN_ELSE)
2417
    return NEXT_INSN (first);
2418
 
2419
  /* Extract some information we need.  */
2420
  end_blk_1_br = first;
2421
  br_pat = PATTERN (end_blk_1_br);
2422
 
2423
  /* Complement the condition since we use the reverse cond. for the insns.  */
2424
  cond = (GET_CODE (XEXP (XEXP (br_pat, 1), 0)) == EQ);
2425
 
2426
  /* Determine what kind of branch we have.  */
2427
  if (GET_CODE (XEXP (XEXP (br_pat, 1), 1)) == LABEL_REF)
2428
    {
2429
      /* A normal branch, so extract label out of first arm.  */
2430
      br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 1), 0));
2431
    }
2432
  else
2433
    {
2434
      /* An inverse branch, so extract the label out of the 2nd arm
2435
         and complement the condition.  */
2436
      cond = (cond == 0);
2437
      br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 2), 0));
2438
    }
2439
 
2440
  /* Scan forward for the start of block 2: it must start with a
2441
     label and that label must be the same as the branch target
2442
     label from block 1.  We don't care about whether block 2 actually
2443
     ends with a branch or a label (an uncond. branch is
2444
     conditionalizable).  */
2445
  for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
2446
    {
2447
      enum rtx_code code;
2448
 
2449
      code = GET_CODE (insn);
2450
 
2451
      /* Look for the label at the start of block 3.  */
2452
      if (code == CODE_LABEL && CODE_LABEL_NUMBER (insn) == br_lab_num)
2453
        break;
2454
 
2455
      /* Skip barriers, notes, and conditionalizable insns.  If the
2456
         insn is not conditionalizable or makes this optimization fail,
2457
         just return the next insn so we can start over from that point.  */
2458
      if (code != BARRIER && code != NOTE && !is_cond_candidate (insn))
2459
        return NEXT_INSN (insn);
2460
 
2461
      /* Remember the last real insn before the label (i.e. end of block 2).  */
2462
      if (code == JUMP_INSN || code == INSN)
2463
        {
2464
          blk_size ++;
2465
          end_blk_2_insn = insn;
2466
        }
2467
    }
2468
 
2469
  if (!insn)
2470
    return insn;
2471
 
2472
  /* It is possible for this optimization to slow performance if the blocks
2473
     are long.  This really depends upon whether the branch is likely taken
2474
     or not.  If the branch is taken, we slow performance in many cases.  But,
2475
     if the branch is not taken, we always help performance (for a single
2476
     block, but for a double block (i.e. when the optimization is re-applied)
2477
     this is not true since the 'right thing' depends on the overall length of
2478
     the collapsed block).  As a compromise, don't apply this optimization on
2479
     blocks larger than size 2 (unlikely for the mcore) when speed is important.
2480
     the best threshold depends on the latencies of the instructions (i.e.,
2481
     the branch penalty).  */
2482
  if (optimize > 1 && blk_size > 2)
2483
    return insn;
2484
 
2485
  /* At this point, we've found the start of block 3 and we know that
2486
     it is the destination of the branch from block 1.   Also, all
2487
     instructions in the block 2 are conditionalizable.  So, apply the
2488
     conditionalization and delete the branch.  */
2489
  start_blk_3_lab = insn;
2490
 
2491
  for (insn = NEXT_INSN (end_blk_1_br); insn != start_blk_3_lab;
2492
       insn = NEXT_INSN (insn))
2493
    {
2494
      rtx newinsn;
2495
 
2496
      if (INSN_DELETED_P (insn))
2497
        continue;
2498
 
2499
      /* Try to form a conditional variant of the instruction and emit it.  */
2500
      if ((newinsn = emit_new_cond_insn (insn, cond)))
2501
        {
2502
          if (end_blk_2_insn == insn)
2503
            end_blk_2_insn = newinsn;
2504
 
2505
          insn = newinsn;
2506
        }
2507
    }
2508
 
2509
  /* Note whether we will delete the label starting blk 3 when the jump
2510
     gets deleted.  If so, we want to re-apply this optimization at the
2511
     last real instruction right before the label.  */
2512
  if (LABEL_NUSES (start_blk_3_lab) == 1)
2513
    {
2514
      start_blk_3_lab = 0;
2515
    }
2516
 
2517
  /* ??? we probably should redistribute the death notes for this insn, esp.
2518
     the death of cc, but it doesn't really matter this late in the game.
2519
     The peepholes all use is_dead() which will find the correct death
2520
     regardless of whether there is a note.  */
2521
  delete_insn (end_blk_1_br);
2522
 
2523
  if (! start_blk_3_lab)
2524
    return end_blk_2_insn;
2525
 
2526
  /* Return the insn right after the label at the start of block 3.  */
2527
  return NEXT_INSN (start_blk_3_lab);
2528
}
2529
 
2530
/* Apply the conditionalization of blocks optimization.  This is the
2531
   outer loop that traverses through the insns scanning for a branch
2532
   that signifies an opportunity to apply the optimization.  Note that
2533
   this optimization is applied late.  If we could apply it earlier,
2534
   say before cse 2, it may expose more optimization opportunities.
2535
   but, the pay back probably isn't really worth the effort (we'd have
2536
   to update all reg/flow/notes/links/etc to make it work - and stick it
2537
   in before cse 2).  */
2538
 
2539
static void
2540
conditionalize_optimization (void)
2541
{
2542
  rtx insn;
2543
 
2544
  for (insn = get_insns (); insn; insn = conditionalize_block (insn))
2545
    continue;
2546
}
2547
 
2548
static int saved_warn_return_type = -1;
2549
static int saved_warn_return_type_count = 0;
2550
 
2551
/* This is to handle loads from the constant pool.  */
2552
 
2553
static void
2554
mcore_reorg (void)
2555
{
2556
  /* Reset this variable.  */
2557
  current_function_anonymous_args = 0;
2558
 
2559
  /* Restore the warn_return_type if it has been altered.  */
2560
  if (saved_warn_return_type != -1)
2561
    {
2562
      /* Only restore the value if we have reached another function.
2563
         The test of warn_return_type occurs in final_function () in
2564
         c-decl.c a long time after the code for the function is generated,
2565
         so we need a counter to tell us when we have finished parsing that
2566
         function and can restore the flag.  */
2567
      if (--saved_warn_return_type_count == 0)
2568
        {
2569
          warn_return_type = saved_warn_return_type;
2570
          saved_warn_return_type = -1;
2571
        }
2572
    }
2573
 
2574
  if (optimize == 0)
2575
    return;
2576
 
2577
  /* Conditionalize blocks where we can.  */
2578
  conditionalize_optimization ();
2579
 
2580
  /* Literal pool generation is now pushed off until the assembler.  */
2581
}
2582
 
2583
 
2584
/* Return true if X is something that can be moved directly into r15.  */
2585
 
2586
bool
2587
mcore_r15_operand_p (rtx x)
2588
{
2589
  switch (GET_CODE (x))
2590
    {
2591
    case CONST_INT:
2592
      return mcore_const_ok_for_inline (INTVAL (x));
2593
 
2594
    case REG:
2595
    case SUBREG:
2596
    case MEM:
2597
      return 1;
2598
 
2599
    default:
2600
      return 0;
2601
    }
2602
}
2603
 
2604
/* Implement SECONDARY_RELOAD_CLASS.  If CLASS contains r15, and we can't
2605
   directly move X into it, use r1-r14 as a temporary.  */
2606
 
2607
enum reg_class
2608
mcore_secondary_reload_class (enum reg_class class,
2609
                              enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2610
{
2611
  if (TEST_HARD_REG_BIT (reg_class_contents[class], 15)
2612
      && !mcore_r15_operand_p (x))
2613
    return LRW_REGS;
2614
  return NO_REGS;
2615
}
2616
 
2617
/* Return the reg_class to use when reloading the rtx X into the class
2618
   CLASS.  If X is too complex to move directly into r15, prefer to
2619
   use LRW_REGS instead.  */
2620
 
2621
enum reg_class
2622
mcore_reload_class (rtx x, enum reg_class class)
2623
{
2624
  if (reg_class_subset_p (LRW_REGS, class) && !mcore_r15_operand_p (x))
2625
    return LRW_REGS;
2626
 
2627
  return class;
2628
}
2629
 
2630
/* Tell me if a pair of reg/subreg rtx's actually refer to the same
2631
   register.  Note that the current version doesn't worry about whether
2632
   they are the same mode or note (e.g., a QImode in r2 matches an HImode
2633
   in r2 matches an SImode in r2. Might think in the future about whether
2634
   we want to be able to say something about modes.  */
2635
 
2636
int
2637
mcore_is_same_reg (rtx x, rtx y)
2638
{
2639
  /* Strip any and all of the subreg wrappers.  */
2640
  while (GET_CODE (x) == SUBREG)
2641
    x = SUBREG_REG (x);
2642
 
2643
  while (GET_CODE (y) == SUBREG)
2644
    y = SUBREG_REG (y);
2645
 
2646
  if (GET_CODE(x) == REG && GET_CODE(y) == REG && REGNO(x) == REGNO(y))
2647
    return 1;
2648
 
2649
  return 0;
2650
}
2651
 
2652
void
2653
mcore_override_options (void)
2654
{
2655
  /* Only the m340 supports little endian code.  */
2656
  if (TARGET_LITTLE_END && ! TARGET_M340)
2657
    target_flags |= MASK_M340;
2658
}
2659
 
2660
/* Compute the number of word sized registers needed to
2661
   hold a function argument of mode MODE and type TYPE.  */
2662
 
2663
int
2664
mcore_num_arg_regs (enum machine_mode mode, tree type)
2665
{
2666
  int size;
2667
 
2668
  if (targetm.calls.must_pass_in_stack (mode, type))
2669
    return 0;
2670
 
2671
  if (type && mode == BLKmode)
2672
    size = int_size_in_bytes (type);
2673
  else
2674
    size = GET_MODE_SIZE (mode);
2675
 
2676
  return ROUND_ADVANCE (size);
2677
}
2678
 
2679
static rtx
2680
handle_structs_in_regs (enum machine_mode mode, tree type, int reg)
2681
{
2682
  int size;
2683
 
2684
  /* The MCore ABI defines that a structure whose size is not a whole multiple
2685
     of bytes is passed packed into registers (or spilled onto the stack if
2686
     not enough registers are available) with the last few bytes of the
2687
     structure being packed, left-justified, into the last register/stack slot.
2688
     GCC handles this correctly if the last word is in a stack slot, but we
2689
     have to generate a special, PARALLEL RTX if the last word is in an
2690
     argument register.  */
2691
  if (type
2692
      && TYPE_MODE (type) == BLKmode
2693
      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2694
      && (size = int_size_in_bytes (type)) > UNITS_PER_WORD
2695
      && (size % UNITS_PER_WORD != 0)
2696
      && (reg + mcore_num_arg_regs (mode, type) <= (FIRST_PARM_REG + NPARM_REGS)))
2697
    {
2698
      rtx    arg_regs [NPARM_REGS];
2699
      int    nregs;
2700
      rtx    result;
2701
      rtvec  rtvec;
2702
 
2703
      for (nregs = 0; size > 0; size -= UNITS_PER_WORD)
2704
        {
2705
          arg_regs [nregs] =
2706
            gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, reg ++),
2707
                               GEN_INT (nregs * UNITS_PER_WORD));
2708
          nregs ++;
2709
        }
2710
 
2711
      /* We assume here that NPARM_REGS == 6.  The assert checks this.  */
2712
      assert (ARRAY_SIZE (arg_regs) == 6);
2713
      rtvec = gen_rtvec (nregs, arg_regs[0], arg_regs[1], arg_regs[2],
2714
                          arg_regs[3], arg_regs[4], arg_regs[5]);
2715
 
2716
      result = gen_rtx_PARALLEL (mode, rtvec);
2717
      return result;
2718
    }
2719
 
2720
  return gen_rtx_REG (mode, reg);
2721
}
2722
 
2723
rtx
2724
mcore_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
2725
{
2726
  enum machine_mode mode;
2727
  int unsigned_p;
2728
 
2729
  mode = TYPE_MODE (valtype);
2730
 
2731
  PROMOTE_MODE (mode, unsigned_p, NULL);
2732
 
2733
  return handle_structs_in_regs (mode, valtype, FIRST_RET_REG);
2734
}
2735
 
2736
/* Define where to put the arguments to a function.
2737
   Value is zero to push the argument on the stack,
2738
   or a hard register in which to store the argument.
2739
 
2740
   MODE is the argument's machine mode.
2741
   TYPE is the data type of the argument (as a tree).
2742
    This is null for libcalls where that information may
2743
    not be available.
2744
   CUM is a variable of type CUMULATIVE_ARGS which gives info about
2745
    the preceding args and about the function being called.
2746
   NAMED is nonzero if this argument is a named parameter
2747
    (otherwise it is an extra parameter matching an ellipsis).
2748
 
2749
   On MCore the first args are normally in registers
2750
   and the rest are pushed.  Any arg that starts within the first
2751
   NPARM_REGS words is at least partially passed in a register unless
2752
   its data type forbids.  */
2753
 
2754
rtx
2755
mcore_function_arg (CUMULATIVE_ARGS cum, enum machine_mode mode,
2756
                    tree type, int named)
2757
{
2758
  int arg_reg;
2759
 
2760
  if (! named || mode == VOIDmode)
2761
    return 0;
2762
 
2763
  if (targetm.calls.must_pass_in_stack (mode, type))
2764
    return 0;
2765
 
2766
  arg_reg = ROUND_REG (cum, mode);
2767
 
2768
  if (arg_reg < NPARM_REGS)
2769
    return handle_structs_in_regs (mode, type, FIRST_PARM_REG + arg_reg);
2770
 
2771
  return 0;
2772
}
2773
 
2774
/* Returns the number of bytes of argument registers required to hold *part*
2775
   of a parameter of machine mode MODE and type TYPE (which may be NULL if
2776
   the type is not known).  If the argument fits entirely in the argument
2777
   registers, or entirely on the stack, then 0 is returned.  CUM is the
2778
   number of argument registers already used by earlier parameters to
2779
   the function.  */
2780
 
2781
static int
2782
mcore_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
2783
                         tree type, bool named)
2784
{
2785
  int reg = ROUND_REG (*cum, mode);
2786
 
2787
  if (named == 0)
2788
    return 0;
2789
 
2790
  if (targetm.calls.must_pass_in_stack (mode, type))
2791
    return 0;
2792
 
2793
  /* REG is not the *hardware* register number of the register that holds
2794
     the argument, it is the *argument* register number.  So for example,
2795
     the first argument to a function goes in argument register 0, which
2796
     translates (for the MCore) into hardware register 2.  The second
2797
     argument goes into argument register 1, which translates into hardware
2798
     register 3, and so on.  NPARM_REGS is the number of argument registers
2799
     supported by the target, not the maximum hardware register number of
2800
     the target.  */
2801
  if (reg >= NPARM_REGS)
2802
    return 0;
2803
 
2804
  /* If the argument fits entirely in registers, return 0.  */
2805
  if (reg + mcore_num_arg_regs (mode, type) <= NPARM_REGS)
2806
    return 0;
2807
 
2808
  /* The argument overflows the number of available argument registers.
2809
     Compute how many argument registers have not yet been assigned to
2810
     hold an argument.  */
2811
  reg = NPARM_REGS - reg;
2812
 
2813
  /* Return partially in registers and partially on the stack.  */
2814
  return reg * UNITS_PER_WORD;
2815
}
2816
 
2817
/* Return nonzero if SYMBOL is marked as being dllexport'd.  */
2818
 
2819
int
2820
mcore_dllexport_name_p (const char * symbol)
2821
{
2822
  return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
2823
}
2824
 
2825
/* Return nonzero if SYMBOL is marked as being dllimport'd.  */
2826
 
2827
int
2828
mcore_dllimport_name_p (const char * symbol)
2829
{
2830
  return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
2831
}
2832
 
2833
/* Mark a DECL as being dllexport'd.  */
2834
 
2835
static void
2836
mcore_mark_dllexport (tree decl)
2837
{
2838
  const char * oldname;
2839
  char * newname;
2840
  rtx    rtlname;
2841
  tree   idp;
2842
 
2843
  rtlname = XEXP (DECL_RTL (decl), 0);
2844
 
2845
  if (GET_CODE (rtlname) == MEM)
2846
    rtlname = XEXP (rtlname, 0);
2847
  gcc_assert (GET_CODE (rtlname) == SYMBOL_REF);
2848
  oldname = XSTR (rtlname, 0);
2849
 
2850
  if (mcore_dllexport_name_p (oldname))
2851
    return;  /* Already done.  */
2852
 
2853
  newname = alloca (strlen (oldname) + 4);
2854
  sprintf (newname, "@e.%s", oldname);
2855
 
2856
  /* We pass newname through get_identifier to ensure it has a unique
2857
     address.  RTL processing can sometimes peek inside the symbol ref
2858
     and compare the string's addresses to see if two symbols are
2859
     identical.  */
2860
  /* ??? At least I think that's why we do this.  */
2861
  idp = get_identifier (newname);
2862
 
2863
  XEXP (DECL_RTL (decl), 0) =
2864
    gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
2865
}
2866
 
2867
/* Mark a DECL as being dllimport'd.  */
2868
 
2869
static void
2870
mcore_mark_dllimport (tree decl)
2871
{
2872
  const char * oldname;
2873
  char * newname;
2874
  tree   idp;
2875
  rtx    rtlname;
2876
  rtx    newrtl;
2877
 
2878
  rtlname = XEXP (DECL_RTL (decl), 0);
2879
 
2880
  if (GET_CODE (rtlname) == MEM)
2881
    rtlname = XEXP (rtlname, 0);
2882
  gcc_assert (GET_CODE (rtlname) == SYMBOL_REF);
2883
  oldname = XSTR (rtlname, 0);
2884
 
2885
  gcc_assert (!mcore_dllexport_name_p (oldname));
2886
  if (mcore_dllimport_name_p (oldname))
2887
    return; /* Already done.  */
2888
 
2889
  /* ??? One can well ask why we're making these checks here,
2890
     and that would be a good question.  */
2891
 
2892
  /* Imported variables can't be initialized.  */
2893
  if (TREE_CODE (decl) == VAR_DECL
2894
      && !DECL_VIRTUAL_P (decl)
2895
      && DECL_INITIAL (decl))
2896
    {
2897
      error ("initialized variable %q+D is marked dllimport", decl);
2898
      return;
2899
    }
2900
 
2901
  /* `extern' needn't be specified with dllimport.
2902
     Specify `extern' now and hope for the best.  Sigh.  */
2903
  if (TREE_CODE (decl) == VAR_DECL
2904
      /* ??? Is this test for vtables needed?  */
2905
      && !DECL_VIRTUAL_P (decl))
2906
    {
2907
      DECL_EXTERNAL (decl) = 1;
2908
      TREE_PUBLIC (decl) = 1;
2909
    }
2910
 
2911
  newname = alloca (strlen (oldname) + 11);
2912
  sprintf (newname, "@i.__imp_%s", oldname);
2913
 
2914
  /* We pass newname through get_identifier to ensure it has a unique
2915
     address.  RTL processing can sometimes peek inside the symbol ref
2916
     and compare the string's addresses to see if two symbols are
2917
     identical.  */
2918
  /* ??? At least I think that's why we do this.  */
2919
  idp = get_identifier (newname);
2920
 
2921
  newrtl = gen_rtx_MEM (Pmode,
2922
                    gen_rtx_SYMBOL_REF (Pmode,
2923
                             IDENTIFIER_POINTER (idp)));
2924
  XEXP (DECL_RTL (decl), 0) = newrtl;
2925
}
2926
 
2927
static int
2928
mcore_dllexport_p (tree decl)
2929
{
2930
  if (   TREE_CODE (decl) != VAR_DECL
2931
      && TREE_CODE (decl) != FUNCTION_DECL)
2932
    return 0;
2933
 
2934
  return lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)) != 0;
2935
}
2936
 
2937
static int
2938
mcore_dllimport_p (tree decl)
2939
{
2940
  if (   TREE_CODE (decl) != VAR_DECL
2941
      && TREE_CODE (decl) != FUNCTION_DECL)
2942
    return 0;
2943
 
2944
  return lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) != 0;
2945
}
2946
 
2947
/* We must mark dll symbols specially.  Definitions of dllexport'd objects
2948
   install some info in the .drective (PE) or .exports (ELF) sections.  */
2949
 
2950
static void
2951
mcore_encode_section_info (tree decl, rtx rtl ATTRIBUTE_UNUSED, int first ATTRIBUTE_UNUSED)
2952
{
2953
  /* Mark the decl so we can tell from the rtl whether the object is
2954
     dllexport'd or dllimport'd.  */
2955
  if (mcore_dllexport_p (decl))
2956
    mcore_mark_dllexport (decl);
2957
  else if (mcore_dllimport_p (decl))
2958
    mcore_mark_dllimport (decl);
2959
 
2960
  /* It might be that DECL has already been marked as dllimport, but
2961
     a subsequent definition nullified that.  The attribute is gone
2962
     but DECL_RTL still has @i.__imp_foo.  We need to remove that.  */
2963
  else if ((TREE_CODE (decl) == FUNCTION_DECL
2964
            || TREE_CODE (decl) == VAR_DECL)
2965
           && DECL_RTL (decl) != NULL_RTX
2966
           && GET_CODE (DECL_RTL (decl)) == MEM
2967
           && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
2968
           && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
2969
           && mcore_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
2970
    {
2971
      const char * oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
2972
      tree idp = get_identifier (oldname + 9);
2973
      rtx newrtl = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
2974
 
2975
      XEXP (DECL_RTL (decl), 0) = newrtl;
2976
 
2977
      /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
2978
         ??? We leave these alone for now.  */
2979
    }
2980
}
2981
 
2982
/* Undo the effects of the above.  */
2983
 
2984
static const char *
2985
mcore_strip_name_encoding (const char * str)
2986
{
2987
  return str + (str[0] == '@' ? 3 : 0);
2988
}
2989
 
2990
/* MCore specific attribute support.
2991
   dllexport - for exporting a function/variable that will live in a dll
2992
   dllimport - for importing a function/variable from a dll
2993
   naked     - do not create a function prologue/epilogue.  */
2994
 
2995
const struct attribute_spec mcore_attribute_table[] =
2996
{
2997
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2998
  { "dllexport", 0, 0, true,  false, false, NULL },
2999
  { "dllimport", 0, 0, true,  false, false, NULL },
3000
  { "naked",     0, 0, true,  false, false, mcore_handle_naked_attribute },
3001
  { NULL,        0, 0, false, false, false, NULL }
3002
};
3003
 
3004
/* Handle a "naked" attribute; arguments as in
3005
   struct attribute_spec.handler.  */
3006
 
3007
static tree
3008
mcore_handle_naked_attribute (tree * node, tree name, tree args ATTRIBUTE_UNUSED,
3009
                              int flags ATTRIBUTE_UNUSED, bool * no_add_attrs)
3010
{
3011
  if (TREE_CODE (*node) == FUNCTION_DECL)
3012
    {
3013
      /* PR14310 - don't complain about lack of return statement
3014
         in naked functions.  The solution here is a gross hack
3015
         but this is the only way to solve the problem without
3016
         adding a new feature to GCC.  I did try submitting a patch
3017
         that would add such a new feature, but it was (rightfully)
3018
         rejected on the grounds that it was creeping featurism,
3019
         so hence this code.  */
3020
      if (warn_return_type)
3021
        {
3022
          saved_warn_return_type = warn_return_type;
3023
          warn_return_type = 0;
3024
          saved_warn_return_type_count = 2;
3025
        }
3026
      else if (saved_warn_return_type_count)
3027
        saved_warn_return_type_count = 2;
3028
    }
3029
  else
3030
    {
3031
      warning (OPT_Wattributes, "%qs attribute only applies to functions",
3032
               IDENTIFIER_POINTER (name));
3033
      *no_add_attrs = true;
3034
    }
3035
 
3036
  return NULL_TREE;
3037
}
3038
 
3039
/* ??? It looks like this is PE specific?  Oh well, this is what the
3040
   old code did as well.  */
3041
 
3042
static void
3043
mcore_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
3044
{
3045
  int len;
3046
  const char * name;
3047
  char * string;
3048
  const char * prefix;
3049
 
3050
  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3051
 
3052
  /* Strip off any encoding in name.  */
3053
  name = (* targetm.strip_name_encoding) (name);
3054
 
3055
  /* The object is put in, for example, section .text$foo.
3056
     The linker will then ultimately place them in .text
3057
     (everything from the $ on is stripped).  */
3058
  if (TREE_CODE (decl) == FUNCTION_DECL)
3059
    prefix = ".text$";
3060
  /* For compatibility with EPOC, we ignore the fact that the
3061
     section might have relocs against it.  */
3062
  else if (decl_readonly_section (decl, 0))
3063
    prefix = ".rdata$";
3064
  else
3065
    prefix = ".data$";
3066
 
3067
  len = strlen (name) + strlen (prefix);
3068
  string = alloca (len + 1);
3069
 
3070
  sprintf (string, "%s%s", prefix, name);
3071
 
3072
  DECL_SECTION_NAME (decl) = build_string (len, string);
3073
}
3074
 
3075
int
3076
mcore_naked_function_p (void)
3077
{
3078
  return lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE;
3079
}
3080
 
3081
#ifdef OBJECT_FORMAT_ELF
3082
static void
3083
mcore_asm_named_section (const char *name,
3084
                         unsigned int flags ATTRIBUTE_UNUSED,
3085
                         tree decl ATTRIBUTE_UNUSED)
3086
{
3087
  fprintf (asm_out_file, "\t.section %s\n", name);
3088
}
3089
#endif /* OBJECT_FORMAT_ELF */
3090
 
3091
/* Worker function for TARGET_ASM_EXTERNAL_LIBCALL.  */
3092
 
3093
static void
3094
mcore_external_libcall (rtx fun)
3095
{
3096
  fprintf (asm_out_file, "\t.import\t");
3097
  assemble_name (asm_out_file, XSTR (fun, 0));
3098
  fprintf (asm_out_file, "\n");
3099
}
3100
 
3101
/* Worker function for TARGET_RETURN_IN_MEMORY.  */
3102
 
3103
static bool
3104
mcore_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
3105
{
3106
  HOST_WIDE_INT size = int_size_in_bytes (type);
3107
  return (size == -1 || size > 2 * UNITS_PER_WORD);
3108
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.