OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc1/] [gcc/] [config/] [or32/] [or32.c] - Blame information for rev 332

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

Line No. Rev Author Line
1 282 jeremybenn
/* Subroutines for insn-output.c for GNU compiler.  OpenRISC 1000 version.
2
   Copyright (C) 1987, 1992, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
3
   2005, 2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc
4
   Copyright (C) 2010 Embecosm Limited
5
 
6
   Contributed by Damjan Lampret <damjanl@bsemi.com> in 1999.
7
   Major optimizations by Matjaz Breskvar <matjazb@bsemi.com> in 2005.
8
   Updated for GCC 4.5 by Jeremy Bennett <jeremy.bennett@embecoms.com> in 2010
9
 
10
   This file is part of GNU CC.
11
 
12
   This program is free software; you can redistribute it and/or modify it
13
   under the terms of the GNU General Public License as published by the Free
14
   Software Foundation; either version 3 of the License, or (at your option)
15
   any later version.
16
 
17
   This program is distributed in the hope that it will be useful, but WITHOUT
18
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20
   more details.
21
 
22
   You should have received a copy of the GNU General Public License along
23
   with this program.  If not, see <http://www.gnu.org/licenses/>. */
24
 
25
#include "config.h"
26
#include "system.h"
27
#include "coretypes.h"
28
#include "tm.h"
29
#include "rtl.h"
30
#include "tree.h"
31
#include "obstack.h"
32
#include "regs.h"
33
#include "hard-reg-set.h"
34
#include "real.h"
35
#include "insn-config.h"
36
#include "conditions.h"
37
#include "output.h"
38
#include "insn-attr.h"
39
#include "flags.h"
40
#include "reload.h"
41
#include "function.h"
42
#include "expr.h"
43
#include "toplev.h"
44
#include "recog.h"
45
#include "ggc.h"
46
#include "except.h"
47
#include "integrate.h"
48
#include "tm_p.h"
49
#include "target.h"
50
#include "target-def.h"
51
#include "debug.h"
52
#include "langhooks.h"
53
#include "df.h"
54
#include "dwarf2.h"
55
 
56
 
57 332 jeremybenn
/* ========================================================================== */
58
/* Local macros                                                               */
59 282 jeremybenn
 
60 332 jeremybenn
/* Construct a l.movhi instruction for the given reg and value */
61
#define OR32_MOVHI(rd, k)                                               \
62
  ((0x6 << 26) | ((rd) << 21) | (k))
63
 
64
/* Construct a l.ori instruction for the given two regs and value */
65
#define OR32_ORI(rd, ra, k)                                             \
66
  ((0x2a << 26) | ((rd) << 21) | ((ra) << 16) | (k))
67
 
68
/* Construct a l.lwz instruction for the given two registers and offset */
69
#define OR32_LWZ(rd, ra, i)                                             \
70
  ((0x21 << 26) | ((rd) << 21) | ((ra) << 16) | (i))
71
 
72
/* Construct a l.jr instruction for the given register */
73
#define OR32_JR(rb)                                                     \
74
  ((0x11 << 26) | ((rb) << 11))
75
 
76 282 jeremybenn
/* ========================================================================== */
77
/* Static variables (i.e. global to this file only.                           */
78
 
79
 
80
/* Save information from a "cmpxx" pattern until the branch or scc is
81
   emitted. These record the two operands of the "cmpxx" */
82
rtx  or32_compare_op0;
83
rtx  or32_compare_op1;
84
 
85
/*!Stack layout we use for pushing and poping saved registers */
86
static struct
87
{
88
  bool save_lr_p;
89
  int lr_save_offset;
90
  bool save_fp_p;
91
  int fp_save_offset;
92
  int gpr_size;
93
  int gpr_offset;
94
  int total_size;
95
  int vars_size;
96
  int args_size;
97
  HOST_WIDE_INT mask;
98
}  frame_info;
99
 
100
 
101
/* ========================================================================== */
102
/* Local (i.e. static) utility functions */
103
 
104
 
105
/* -------------------------------------------------------------------------- */
106
/*!Must the current function save a register?
107
 
108
   @param[in] regno  The register to consider.
109
 
110
   @return  Non-zero (TRUE) if current function must save "regno", zero
111
            (FALSE) otherwise.                                                */
112
/* -------------------------------------------------------------------------- */
113
static bool
114
or32_save_reg_p (int regno)
115
{
116
  /* No need to save the faked cc0 register.  */
117
  if (regno == OR32_FLAGS_REG)
118
    return false;
119
 
120
  /* Check call-saved registers.  */
121
  if (df_regs_ever_live_p(regno) && !call_used_regs[regno])
122
    return true;
123
 
124
  /* We need to save the old frame pointer before setting up a new
125
     one.  */
126
  if (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)
127
    return true;
128
 
129
  /* We need to save the incoming return address if it is ever clobbered
130
     within the function.  */
131
  if (regno == LINK_REGNUM && df_regs_ever_live_p(regno))
132
    return true;
133
 
134
  return false;
135
 
136
}       /* or32_save_reg_p () */
137
 
138
 
139
/* -------------------------------------------------------------------------- */
140
/*!Compute full frame size and layout.
141
 
142
   Store information in "frame_info".
143
 
144
   @param[in] size  The size of the function's local variables.
145
 
146
   @return  Total size of stack frame.                                        */
147
/* -------------------------------------------------------------------------- */
148
static HOST_WIDE_INT
149
or32_compute_frame_size (HOST_WIDE_INT size)
150
{
151
  HOST_WIDE_INT args_size;
152
  HOST_WIDE_INT vars_size;
153
  HOST_WIDE_INT stack_offset;
154
 
155
  int regno;
156
 
157
  args_size = crtl->outgoing_args_size;
158
  vars_size = OR32_ALIGN (size, 4);
159
 
160
  frame_info.args_size = args_size;
161
  frame_info.vars_size = vars_size;
162
 
163
  /* If the function has local variables, we're committed to
164
     allocating it anyway.  Otherwise reclaim it here.  */
165
  /* FIXME: Verify this.  Got if from the MIPS port.  */
166
  if (vars_size == 0 && current_function_is_leaf)
167
    args_size = 0;
168
 
169
  stack_offset = args_size;
170
 
171
  /* Save link register right after possible outgoing arguments.  */
172
  if (or32_save_reg_p (LINK_REGNUM))
173
    {
174
      frame_info.lr_save_offset = stack_offset;
175
      frame_info.save_lr_p = true;
176
      stack_offset = stack_offset + UNITS_PER_WORD;
177
    }
178
  else
179
    frame_info.save_lr_p = false;
180
 
181
  /* Save frame pointer right after possible link register.  */
182
  if (or32_save_reg_p (FRAME_POINTER_REGNUM))
183
    {
184
      frame_info.fp_save_offset = stack_offset;
185
      frame_info.save_fp_p = true;
186
      stack_offset = stack_offset + UNITS_PER_WORD;
187
    }
188
  else
189
    frame_info.save_fp_p = false;
190
 
191
  frame_info.gpr_size = 0;
192
  frame_info.mask = 0;
193
  frame_info.gpr_offset = stack_offset;
194
 
195
  for (regno = 0; regno <= OR32_LAST_INT_REG; regno++)
196
    {
197
      if (regno == LINK_REGNUM || regno == FRAME_POINTER_REGNUM)
198
        /* These have already been saved if so needed.  */
199
        continue;
200
 
201
      if (or32_save_reg_p (regno))
202
        {
203
          frame_info.gpr_size += UNITS_PER_WORD;
204
          frame_info.mask |= (1 << regno);
205
        }
206
    }
207
 
208
  frame_info.total_size = ((frame_info.save_fp_p ? UNITS_PER_WORD : 0)
209
                           + (frame_info.save_lr_p ? UNITS_PER_WORD : 0)
210
                           + args_size + frame_info.gpr_size + vars_size);
211
 
212
  return frame_info.total_size;
213
 
214
}       /* or32_compute_frame_size () */
215
 
216
 
217
/* -------------------------------------------------------------------------- */
218
/*!Emit a frame related insn.
219
 
220
   Same as emit_insn, but sets RTX_FRAME_RELATED_P to one. Getting this right
221
   will matter for DWARF 2 output, if prologues are handled via the "prologue"
222
   pattern rather than target hooks.
223
 
224
   @param[in] insn  The insn to emit.
225
 
226
   @return  The RTX for the emitted insn.                                     */
227
/* -------------------------------------------------------------------------- */
228
static rtx
229
emit_frame_insn (rtx insn)
230
{
231
  insn = emit_insn (insn);
232
  RTX_FRAME_RELATED_P (insn) = 1;
233
  return (insn);
234
 
235
}       /* emit_frame_insn () */
236
 
237
 
238
/* -------------------------------------------------------------------------- */
239
/*!Generate the RTX for an indexed memory access
240
 
241
   Generate a RTX for the indexed memory address based on a base address and a
242
   displacement
243
 
244
   @param[in] base  The base address RTX
245
   @param[in] disp  The displacement
246
 
247
   @return  The RTX for the generated address.                                */
248
/* -------------------------------------------------------------------------- */
249
static rtx
250
indexed_memory (rtx            base,
251
                HOST_WIDE_INT  disp)
252
{
253
  return gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, base, GEN_INT (disp)));
254
 
255
}       /* indexed_memory () */
256
 
257
 
258
/* -------------------------------------------------------------------------- */
259
/*!Generate insn patterns to do an integer compare of operands.
260
 
261
   @param[in] code  RTX for the condition code.
262
   @param[in] op0   RTX for the first operand.
263
   @param[in] op1   RTX for the second operand.
264
 
265
   @return  RTX for the comparison.                                           */
266
/* -------------------------------------------------------------------------- */
267
static rtx
268
or32_expand_int_compare (enum rtx_code  code,
269
                         rtx            op0,
270
                         rtx            op1)
271
{
272
  enum machine_mode cmpmode;
273
  rtx tmp, flags;
274
 
275
  cmpmode = SELECT_CC_MODE (code, op0, op1);
276
  flags = gen_rtx_REG (cmpmode, OR32_FLAGS_REG);
277
 
278
  /* This is very simple, but making the interface the same as in the
279
     FP case makes the rest of the code easier.  */
280
  tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
281
  emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
282
 
283
  /* Return the test that should be put into the flags user, i.e.
284
     the bcc, scc, or cmov instruction.  */
285
  return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
286
 
287
}       /* or32_expand_int_compare () */
288
 
289
 
290
/* -------------------------------------------------------------------------- */
291
/*!Generate insn patterns to do an integer compare of operands.
292
 
293
   We only deal with the case where the comparison is an integer
294
   comparison. This wrapper function potentially allows reuse for non-integer
295
   comparison in the future.
296
 
297
   @param[in] code  RTX for the condition code.
298
   @param[in] op0   RTX for the first operand.
299
   @param[in] op1   RTX for the second operand.
300
 
301
   @return  RTX for the comparison.                                           */
302
/* -------------------------------------------------------------------------- */
303
static rtx
304
or32_expand_compare (enum rtx_code code, rtx op0, rtx op1)
305
{
306
  return or32_expand_int_compare (code, op0, op1);
307
 
308
}       /* or32_expand_compare () */
309
 
310
 
311
/* -------------------------------------------------------------------------- */
312
/*!Emit insns to use the l.cmov instruction
313
 
314
   Emit a compare and then cmov. Only works for integer first operand.
315
 
316
   @param[in] dest        RTX for the destination operand.
317
   @param[in] op          RTX for the comparison operation
318
   @param[in] true_cond   RTX to move to dest if condition is TRUE.
319
   @param[in] false_cond  RTX to move to dest if condition is FALSE.
320
 
321
   @return  Non-zero (TRUE) if insns were emitted, zero (FALSE) otherwise.    */
322
/* -------------------------------------------------------------------------- */
323
static int
324
or32_emit_int_cmove (rtx  dest,
325
                     rtx  op,
326
                     rtx  true_cond,
327
                     rtx  false_cond)
328
{
329
  rtx condition_rtx, cr;
330
 
331
  if ((GET_MODE (or32_compare_op0) != SImode) &&
332
      (GET_MODE (or32_compare_op0) != HImode) &&
333
      (GET_MODE (or32_compare_op0) != QImode))
334
    {
335
      return 0;
336
    }
337
 
338
  /* We still have to do the compare, because cmov doesn't do a compare, it
339
     just looks at the FLAG bit set by a previous compare instruction.  */
340
  condition_rtx = or32_expand_compare (GET_CODE (op),
341
                                       or32_compare_op0, or32_compare_op1);
342
 
343
  cr = XEXP (condition_rtx, 0);
344
 
345
  emit_insn (gen_cmov (dest, condition_rtx, true_cond, false_cond, cr));
346
 
347
  return 1;
348
 
349
}       /* or32_emit_int_cmove () */
350
 
351
 
352
/* -------------------------------------------------------------------------- */
353
/*!Calculate stack size for current function.
354
 
355
   We need space for:
356
   - any callee-saved registers that are live in the function
357
   - any local variables
358
   - the return address (if saved)
359
   - the frame pointer (if saved)
360
   - any outgoing arguments.
361
 
362
   We also return information on whether the return address and frame pointer
363
   must be saved, the space required to save callee-saved registers and the
364
   sapce required to save the return address, frame pointer and outgoing
365
   arguments.
366
 
367
   Throughout adjust for OR32 alignment requirements.
368
 
369
   @param[in]  vars           Bytes required for local variables (if any).
370
   @param[out] lr_save_area   Space required for return address (if any).
371
   @param[out] fp_save_area   Space required for frame pointer (if any).
372
   @param[out] gpr_save_area  Space required for callee-saved registers (if
373
                              any).
374
   @param[out] save_area      Space required for outgoing arguments (if any) +
375
                              return address (if any) and frame pointer (if
376
                              any).
377
 
378
   @return  Total space required (if any).                                    */
379
/* -------------------------------------------------------------------------- */
380
static int
381
calculate_stack_size (int  vars,
382
                      int *lr_save_area,
383
                      int *fp_save_area,
384
                      int *gpr_save_area,
385
                      int *save_area)
386
{
387
  int regno;
388
 
389
  *gpr_save_area = 0;
390
  for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
391
    {
392
      if (df_regs_ever_live_p(regno) && !call_used_regs[regno])
393
        *gpr_save_area += 4;
394
    }
395
 
396
  *lr_save_area = (!current_function_is_leaf
397
                   || df_regs_ever_live_p(LINK_REGNUM)) ? 4 : 0;
398
  *fp_save_area = frame_pointer_needed ? 4 : 0;
399
  *save_area    = (OR32_ALIGN (crtl->outgoing_args_size, 4)
400
                   + *lr_save_area + *fp_save_area);
401
 
402
  return *save_area + *gpr_save_area + OR32_ALIGN (vars, 4);
403
 
404
}       /* calculate_stack_size () */
405
 
406
 
407
/* -------------------------------------------------------------------------- */
408
/*!Is this a value suitable for an OR32 address displacement?
409
 
410
   Must be an integer (signed) which fits into 16-bits. If the result is a
411
   double word, we had better also check that we can also get at the second
412
   word.
413
 
414
   @param[in] mode  Mode of the result for which this displacement will be
415
                    used.
416
   @param[in] x     RTX for an expression.
417
 
418
   @return  Non-zero (TRUE) if this is a valid 16-bit offset, zero (FALSE)
419
            otherwise.                                                        */
420
/* -------------------------------------------------------------------------- */
421
static int
422
or32_legitimate_displacement_p (enum machine_mode  mode,
423
                                rtx                x)
424
{
425
  if (CONST_INT == GET_CODE(x))
426
    {
427
      HOST_WIDE_INT  disp = INTVAL (x);
428
 
429
      /* Allow for a second access 4 bytes further on if double. */
430
      if ((DFmode == mode) || (DImode == mode))
431
        {
432
          return  (-32768 < disp) && (disp <= 32763);
433
        }
434
      else
435
        {
436
          return  (-32768 < disp) && (disp <= 32767);
437
        }
438
    }
439
  else
440
    {
441
      return  0;
442
    }
443
}       /* or32_legitimate_displacement_p () */
444
 
445
 
446
/* -------------------------------------------------------------------------- */
447
/*!Can this register be used as a base register?
448
 
449
   We need a strict version, for which the register must either be a hard
450
   register, or already renumbered to a hard register.
451
 
452
   For the non-strict version, any register (other than the flag register will
453
   do).
454
 
455
   @todo The code from the old port does not allow r0 as a base when strict,
456
         and does when non-strict. Surely it is always a valid register?
457
 
458
   @param[in] regno   The register to test
459
   @param[in] strict  Non-zero (TRUE) if this is a strict check, zero (FALSE)
460
                      otherwise.
461
 
462
   @return  Non-zero (TRUE) if this register can be used as a base register,
463
            zero (FALSE) otherwise.                                           */
464
/* -------------------------------------------------------------------------- */
465
static bool
466
or32_regnum_ok_for_base_p (HOST_WIDE_INT  num,
467
                           bool           strict)
468
{
469
  if (strict)
470
    {
471
      return (num < FIRST_PSEUDO_REGISTER)
472
        ? (num > 0) && (num <= OR32_LAST_INT_REG)
473
        : (reg_renumber[num] > 0) && (reg_renumber[num] <= OR32_LAST_INT_REG);
474
    }
475
  else
476
    {
477
      return (num <= OR32_LAST_INT_REG) || (num >= FIRST_PSEUDO_REGISTER);
478
    }
479
}       /* or32_regnum_ok_for_base_p () */
480
 
481
 
482 332 jeremybenn
/* -------------------------------------------------------------------------- */
483
/*!Emit a move from SRC to DEST.
484
 
485
   Assume that the move expanders can handle all moves if !can_create_pseudo_p
486
   ().  The distinction is important because, unlike emit_move_insn, the move
487
   expanders know how to force Pmode objects into the constant pool even when
488
   the constant pool address is not itself legitimate.
489
 
490
   @param[in] dest  Destination of the move.
491
   @param[in] src   Source for the move.
492
 
493
   @return  RTX for the move.                                                 */
494
/* -------------------------------------------------------------------------- */
495
rtx
496
or32_emit_move (rtx dest, rtx src)
497
{
498
  return (can_create_pseudo_p ()
499
          ? emit_move_insn (dest, src)
500
          : emit_move_insn_1 (dest, src));
501
 
502
}       /* or32_emit_move () */
503
 
504
 
505
/* -------------------------------------------------------------------------- */
506
/*!Emit an instruction of the form (set TARGET (CODE OP0 OP1)).
507
 
508
   @param[in] code    The code for the operation.
509
   @param[in] target  Destination for the set operation.
510
   @param[in] op0     First operand.
511
   @param[in] op1     Second operand.                                         */
512
/* -------------------------------------------------------------------------- */
513
static void
514
or32_emit_binary (enum rtx_code  code,
515
                  rtx            target,
516
                  rtx            op0,
517
                  rtx            op1)
518
{
519
  emit_insn (gen_rtx_SET (VOIDmode, target,
520
                          gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
521
 
522
}       /* or32_emit_binary () */
523
 
524
 
525
/* -------------------------------------------------------------------------- */
526
/*!Compute the result of an operation into a new register.
527
 
528
   Compute ("code" "op0" "op1") and store the result in a new register of mode
529
   "mode".
530
 
531
   @param[in] mode  Mode of the result
532
   @parma[in] code  RTX for the operation to perform
533
   @param[in] op0   RTX for the first operand
534
   @param[in] op1   RTX for the second operand
535
 
536
   @return  The RTX for the new register.                                     */
537
/* -------------------------------------------------------------------------- */
538
static rtx
539
or32_force_binary (enum machine_mode  mode,
540
                   enum rtx_code      code,
541
                   rtx                op0,
542
                   rtx                op1)
543
{
544
  rtx  reg;
545
 
546
  reg = gen_reg_rtx (mode);
547
  or32_emit_binary (code, reg, op0, op1);
548
 
549
  return reg;
550
 
551
}       /* or32_force_binary () */
552
 
553
 
554 282 jeremybenn
/* ========================================================================== */
555 332 jeremybenn
/* Global support functions                                                   */
556
 
557
/* -------------------------------------------------------------------------- */
558
/* Return the size in bytes of the trampoline code.
559
 
560
   Padded to TRAMPOLINE_ALIGNMENT bits. The code sequence is documented in
561
   or32_trampoline_init ().
562
 
563
   This is just the code size. the static chain pointer and target function
564
   address immediately follow.
565
 
566
   @return  The size of the trampoline code in bytes.                         */
567
/* -------------------------------------------------------------------------- */
568
int
569
or32_trampoline_code_size (void)
570
{
571
  const int  TRAMP_BYTE_ALIGN = TRAMPOLINE_ALIGNMENT / 8;
572
 
573
  /* Five 32-bit code words are needed */
574
  return (5 * 4 + TRAMP_BYTE_ALIGN - 1) / TRAMP_BYTE_ALIGN * TRAMP_BYTE_ALIGN;
575
 
576
}       /* or32_trampoline_code_size () */
577
 
578
 
579
/* ========================================================================== */
580 282 jeremybenn
/* Functions to support the Machine Description                               */
581
 
582
 
583
/* -------------------------------------------------------------------------- */
584
/*!Expand a prologue pattern.
585
 
586
   Called after register allocation to add any instructions needed for the
587
   prologue.  Using a prologue insn is favored compared to putting all of the
588
   instructions in output_function_prologue(), since it allows the scheduler
589
   to intermix instructions with the saves of the caller saved registers.  In
590
   some cases, it might be necessary to emit a barrier instruction as the last
591
   insn to prevent such scheduling.
592
 
593
   For the OR32 this is currently controlled by the -mlogue option. It should
594
   be the default, once it is proved to work.                                 */
595
/* -------------------------------------------------------------------------- */
596
void
597
or32_expand_prologue (void)
598
{
599
  int total_size = or32_compute_frame_size (get_frame_size ());
600
  rtx sp_rtx;
601
  rtx value_rtx;
602
 
603
  if (!total_size)
604
    /* No frame needed.  */
605
    return;
606
 
607
  sp_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
608
 
609
  if (total_size > 32767)
610
    {
611
      value_rtx = gen_rtx_REG (Pmode, GP_ARG_RETURN);
612
      emit_frame_insn (gen_rtx_SET (Pmode, value_rtx, GEN_INT (total_size)));
613
    }
614
  else
615
    value_rtx = GEN_INT (total_size);
616
 
617
  /* Update the stack pointer to reflect frame size.  */
618
  emit_frame_insn
619
    (gen_rtx_SET (Pmode, stack_pointer_rtx,
620
                  gen_rtx_MINUS (Pmode, stack_pointer_rtx, value_rtx)));
621
 
622
  if (frame_info.save_fp_p)
623
    {
624
      emit_frame_insn
625
        (gen_rtx_SET (Pmode,
626
                      indexed_memory (stack_pointer_rtx,
627
                                      frame_info.fp_save_offset),
628
                      frame_pointer_rtx));
629
 
630
      emit_frame_insn
631
        (gen_rtx_SET (Pmode, frame_pointer_rtx,
632
                      gen_rtx_PLUS (Pmode, frame_pointer_rtx, value_rtx)));
633
    }
634
  if (frame_info.save_lr_p)
635
    {
636
 
637
      emit_frame_insn
638
        (gen_rtx_SET (Pmode,
639
                      indexed_memory (stack_pointer_rtx,
640
                                      frame_info.lr_save_offset),
641
                      gen_rtx_REG (Pmode, LINK_REGNUM)));
642
    }
643
  if (frame_info.gpr_size)
644
    {
645
      int offset = 0;
646
      int regno;
647
 
648
      for (regno = 0; regno <= OR32_LAST_INT_REG; regno++)
649
        {
650
          HOST_WIDE_INT disp = frame_info.gpr_offset + offset;
651
 
652
          if (!(frame_info.mask & (1 << regno)))
653
            continue;
654
 
655
          emit_frame_insn
656
            (gen_rtx_SET (Pmode,
657
                          indexed_memory (stack_pointer_rtx, disp),
658
                          gen_rtx_REG (Pmode, regno)));
659
          offset = offset + UNITS_PER_WORD;
660
        }
661
    }
662
}       /* or32_expand_prologue () */
663
 
664
 
665
/* -------------------------------------------------------------------------- */
666
/*!Expand an epilogue pattern.
667
 
668
   Called after register allocation to add any instructions needed for the
669
   epilogue.  Using an epilogue insn is favored compared to putting all of the
670
   instructions in output_function_epilogue(), since it allows the scheduler
671
   to intermix instructions with the restores of the caller saved registers.
672
   In some cases, it might be necessary to emit a barrier instruction as the
673
   first insn to prevent such scheduling.
674
 
675
   For the OR32 this is currently controlled by the -mlogue option. It should
676
   be the default, once it is proved to work.
677
 
678
   @param[in] sibcall  Non-zero (TRUE) if this is a sibcall return, which can
679
                       benefit from tail call optimization. Zero (FALSE)
680
                       otherwise.                                             */
681
/* -------------------------------------------------------------------------- */
682
void
683
or32_expand_epilogue (int sibcall)
684
{
685
  int total_size = or32_compute_frame_size (get_frame_size ());
686
  rtx value_rtx;
687
 
688
  if (total_size > 32767)
689
    {
690
      value_rtx = gen_rtx_REG (Pmode, 3);
691
 
692
      emit_insn (gen_rtx_SET (Pmode, value_rtx, GEN_INT (total_size)));
693
    }
694
  else
695
    value_rtx = GEN_INT (total_size);
696
 
697
  if (frame_info.save_lr_p)
698
    {
699
      emit_insn
700
        (gen_rtx_SET (Pmode, gen_rtx_REG (Pmode, LINK_REGNUM),
701
                      indexed_memory (stack_pointer_rtx,
702
                                      frame_info.lr_save_offset)));
703
    }
704
  if (frame_info.save_fp_p)
705
    {
706
      emit_insn
707
        (gen_rtx_SET (Pmode, gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM),
708
                      indexed_memory (stack_pointer_rtx,
709
                                      frame_info.fp_save_offset)));
710
    }
711
 
712
  if (frame_info.gpr_size)
713
    {
714
      int offset = 0;
715
      int regno;
716
 
717
      for (regno = 0; regno <= OR32_LAST_INT_REG; regno++)
718
        {
719
          HOST_WIDE_INT disp = frame_info.gpr_offset + offset;
720
 
721
          if (!(frame_info.mask & (1 << regno)))
722
            continue;
723
 
724
          emit_insn
725
            (gen_rtx_SET (Pmode, gen_rtx_REG (Pmode, regno),
726
                          indexed_memory (stack_pointer_rtx, disp)));
727
          offset = offset + UNITS_PER_WORD;
728
        }
729
    }
730
 
731
  if (total_size)
732
    {
733
      emit_insn (gen_rtx_SET (Pmode, stack_pointer_rtx,
734
                              gen_rtx_PLUS (Pmode,
735
                                            stack_pointer_rtx, value_rtx)));
736
    }
737
 
738
  if (!sibcall)
739
    emit_jump_insn (gen_return_internal (gen_rtx_REG( Pmode, 9)));
740
 
741
}       /* or32_expand_epilogue () */
742
 
743
 
744
/* -------------------------------------------------------------------------- */
745
/*!Generate assembler code for a movdi/movdf pattern
746
 
747
   @param[in] operands  Operands to the movdx pattern.
748
 
749
   @return  The assembler string to output (always "", since we've done the
750
            output here).                                                     */
751
/* -------------------------------------------------------------------------- */
752
const char *
753
or32_output_move_double (rtx *operands)
754
{
755
  rtx xoperands[3];
756
 
757
  switch (GET_CODE (operands[0]))
758
    {
759
    case REG:
760
      if (GET_CODE (operands[1]) == REG)
761
        {
762
          if (REGNO (operands[0]) == REGNO (operands[1]) + 1)
763
            {
764
              output_asm_insn ("\tl.or    \t%H0, %H1, r0", operands);
765
              output_asm_insn ("\tl.or    \t%0, %1, r0", operands);
766
              return "";
767
            }
768
          else
769
            {
770
              output_asm_insn ("\tl.or    \t%0, %1, r0", operands);
771
              output_asm_insn ("\tl.or    \t%H0, %H1, r0", operands);
772
              return "";
773
            }
774
        }
775
      else if (GET_CODE (operands[1]) == MEM)
776
        {
777
          xoperands[1] = XEXP (operands[1], 0);
778
          if (GET_CODE (xoperands[1]) == REG)
779
            {
780
              xoperands[0] = operands[0];
781
              if (REGNO (xoperands[0]) == REGNO (xoperands[1]))
782
                {
783
                  output_asm_insn ("\tl.lwz   \t%H0, 4(%1)", xoperands);
784
                  output_asm_insn ("\tl.lwz   \t%0, 0(%1)", xoperands);
785
                  return "";
786
                }
787
              else
788
                {
789
                  output_asm_insn ("\tl.lwz   \t%0, 0(%1)", xoperands);
790
                  output_asm_insn ("\tl.lwz   \t%H0, 4(%1)", xoperands);
791
                  return "";
792
                }
793
            }
794
          else if (GET_CODE (xoperands[1]) == PLUS)
795
            {
796
              if (GET_CODE (xoperands[2] = XEXP (xoperands[1], 1)) == REG)
797
                {
798
                  xoperands[0] = operands[0];
799
                  xoperands[1] = XEXP (xoperands[1], 0);
800
                  if (REGNO (xoperands[0]) == REGNO (xoperands[2]))
801
                    {
802
                      output_asm_insn ("\tl.lwz   \t%H0, %1+4(%2)",
803
                                       xoperands);
804
                      output_asm_insn ("\tl.lwz   \t%0, %1(%2)", xoperands);
805
                      return "";
806
                    }
807
                  else
808
                    {
809
                      output_asm_insn ("\tl.lwz   \t%0, %1(%2)", xoperands);
810
                      output_asm_insn ("\tl.lwz   \t%H0, %1+4(%2)",
811
                                       xoperands);
812
                      return "";
813
                    }
814
                }
815
              else if (GET_CODE (xoperands[2] = XEXP (xoperands[1], 0)) ==
816
                       REG)
817
                {
818
                  xoperands[0] = operands[0];
819
                  xoperands[1] = XEXP (xoperands[1], 1);
820
                  if (REGNO (xoperands[0]) == REGNO (xoperands[2]))
821
                    {
822
                      output_asm_insn ("\tl.lwz   \t%H0, %1+4(%2)",
823
                                       xoperands);
824
                      output_asm_insn ("\tl.lwz   \t%0, %1(%2)", xoperands);
825
                      return "";
826
                    }
827
                  else
828
                    {
829
                      output_asm_insn ("\tl.lwz   \t%0, %1(%2)", xoperands);
830
                      output_asm_insn ("\tl.lwz   \t%H0, %1+4(%2)",
831
                                       xoperands);
832
                      return "";
833
                    }
834
                }
835
              else
836
                abort ();
837
            }
838
          else
839
            abort ();
840
        }
841
      else if (GET_CODE (operands[1]) == CONST_INT)
842
        {
843
          if (INTVAL (operands[1]) < 0)
844
            output_asm_insn ("\tl.addi  \t%0, r0, -1", operands);
845
          else
846
            output_asm_insn ("\tl.or    \t%0, r0, r0", operands);
847
          output_asm_insn ("\tl.movhi \t%H0, hi(%1)", operands);
848
          output_asm_insn ("\tl.ori   \t%H0, %H0, lo(%1)", operands);
849
          return "";
850
        }
851
      else
852
        abort ();
853
    case MEM:
854
      xoperands[0] = XEXP (operands[0], 0);
855
      if (GET_CODE (xoperands[0]) == REG)
856
        {
857
          xoperands[1] = operands[1];
858
          output_asm_insn ("\tl.sw    \t0(%0), %1", xoperands);
859
          output_asm_insn ("\tl.sw    \t4(%0), %H1", xoperands);
860
          return "";
861
        }
862
      else if (GET_CODE (xoperands[0]) == PLUS)
863
        {
864
          if (GET_CODE (xoperands[1] = XEXP (xoperands[0], 1)) == REG)
865
            {
866
              xoperands[0] = XEXP (xoperands[0], 0);
867
              xoperands[2] = operands[1];
868
              output_asm_insn ("\tl.sw    \t%0(%1), %2", xoperands);
869
              output_asm_insn ("\tl.sw    \t%0+4(%1), %H2", xoperands);
870
              return "";
871
            }
872
          else if (GET_CODE (xoperands[1] = XEXP (xoperands[0], 0)) == REG)
873
            {
874
              xoperands[0] = XEXP (xoperands[0], 1);
875
              xoperands[2] = operands[1];
876
              output_asm_insn ("\tl.sw    \t%0(%1), %2", xoperands);
877
              output_asm_insn ("\tl.sw    \t%0+4(%1), %H2", xoperands);
878
              return "";
879
            }
880
          else
881
            abort ();
882
        }
883
      else
884
        {
885
          fprintf (stderr, "  O/p error %s\n",
886
                   GET_RTX_NAME (GET_CODE (xoperands[0])));
887
          return "";
888
          /* abort (); */
889
        }
890
    default:
891
      abort ();
892
    }
893
}       /* or32_output_move_double () */
894
 
895
 
896
/* -------------------------------------------------------------------------- */
897
/*!Expand a conditional branch
898
 
899
   @param[in] operands  Operands to the branch.
900
   @param[in] mode      Mode of the comparison.                               */
901
/* -------------------------------------------------------------------------- */
902
void
903
or32_expand_conditional_branch (rtx               *operands,
904
                                enum machine_mode  mode)
905
{
906
  rtx tmp;
907
  enum rtx_code test_code = GET_CODE(operands[0]);
908
 
909
  switch (mode)
910
    {
911
    case SImode:
912
      tmp = or32_expand_compare (test_code, operands[1], operands[2]);
913
      tmp = gen_rtx_IF_THEN_ELSE (VOIDmode,
914
                                  tmp,
915
                                  gen_rtx_LABEL_REF (VOIDmode, operands[3]),
916
                                  pc_rtx);
917
      emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
918
      return;
919
 
920
    case SFmode:
921
      tmp = or32_expand_compare (test_code, operands[1], operands[2]);
922
      tmp = gen_rtx_IF_THEN_ELSE (VOIDmode,
923
                                  tmp,
924
                                  gen_rtx_LABEL_REF (VOIDmode, operands[3]),
925
                                  pc_rtx);
926
      emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
927
      return;
928
 
929
    default:
930
      abort ();
931
    }
932
 
933
}       /* or32_expand_conditional_branch () */
934
 
935
 
936
/* -------------------------------------------------------------------------- */
937
/*!Emit a conditional move
938
 
939
   move "true_cond" to "dest" if "op" of the operands of the last comparison
940
   is nonzero/true, "false_cond" if it is zero/false.
941
 
942
   @param[in] dest        RTX for the destination operand.
943
   @param[in] op          RTX for the comparison operation
944
   @param[in] true_cond   RTX to move to dest if condition is TRUE.
945
   @param[in] false_cond  RTX to move to dest if condition is FALSE.
946
 
947
   @return  Non-zero (TRUE) if the hardware supports such an operation, zero
948
            (FALSE) otherwise.                                                */
949
/* -------------------------------------------------------------------------- */
950
int
951
or32_emit_cmove (rtx  dest,
952
                 rtx  op,
953
                 rtx  true_cond,
954
                 rtx  false_cond)
955
{
956
  enum machine_mode result_mode = GET_MODE (dest);
957
 
958
  if (GET_MODE (true_cond) != result_mode)
959
    return 0;
960
 
961
  if (GET_MODE (false_cond) != result_mode)
962
    return 0;
963
 
964
  /* First, work out if the hardware can do this at all */
965
  return or32_emit_int_cmove (dest, op, true_cond, false_cond);
966
 
967
}       /* or32_emit_cmove () */
968
 
969
 
970
/* -------------------------------------------------------------------------- */
971
/*!Output the assembler for a branch on flag instruction.
972
 
973
   @param[in] operands  Operands to the branch.
974
 
975
   @return  The assembler string to use.                                      */
976
/* -------------------------------------------------------------------------- */
977
const char *
978
or32_output_bf (rtx * operands)
979
{
980
  enum rtx_code code;
981
  enum machine_mode mode_calc, mode_got;
982
 
983
  code      = GET_CODE (operands[1]);
984
  mode_calc = SELECT_CC_MODE (code, or32_compare_op0, or32_compare_op1);
985
  mode_got  = GET_MODE (operands[2]);
986
 
987
  if (!TARGET_MASK_ALIGNED_JUMPS)
988
    {
989
      if (mode_calc != mode_got)
990 332 jeremybenn
        return "l.bnf\t%l0%(";
991 282 jeremybenn
      else
992 332 jeremybenn
        return "l.bf\t%l0%(";
993 282 jeremybenn
    }
994
  else
995
    {
996
      if (mode_calc != mode_got)
997 332 jeremybenn
        return ".balignl\t0x8,0x15000015,0x4\n\tl.bnf\t%l0%(";
998 282 jeremybenn
      else
999 332 jeremybenn
        return ".balignl 0x8,0x15000015,0x4;\n\tl.bf\t%l0%(";
1000 282 jeremybenn
    }
1001
}       /* or32_output_bf () */
1002
 
1003
 
1004
/* -------------------------------------------------------------------------- */
1005
/*!Output the assembler for a conditional move instruction.
1006
 
1007
   @param[in] operands  Operands to the conditional move.
1008
 
1009
   @return  The assembler string to use.                                      */
1010
/* -------------------------------------------------------------------------- */
1011
const char *
1012
or32_output_cmov (rtx * operands)
1013
{
1014
  enum rtx_code code;
1015
  enum machine_mode mode_calc, mode_got;
1016
 
1017
  code      = GET_CODE (operands[1]);
1018
  mode_calc = SELECT_CC_MODE (code, or32_compare_op0, or32_compare_op1);
1019
  mode_got  = GET_MODE (operands[4]);
1020
 
1021
  if (mode_calc != mode_got)
1022
    return "\tl.cmov\t%0,%3,%2";        /* reversed */
1023
  else
1024
    return "\tl.cmov\t%0,%2,%3";
1025
 
1026
}       /* or32_output_cmov () */
1027
 
1028
 
1029
/* -------------------------------------------------------------------------- */
1030
/*!Expand a sibcall pattern.
1031
 
1032
   For now this is very simple way for sibcall support (i.e tail call
1033
   optimization).
1034
 
1035
   @param[in] result     Not sure. RTX for the result location?
1036
   @param[in] addr       Not sure. RXT for the address to call?
1037
   @param[in] args_size  Not sure. RTX for the size of the args (in bytes?)?  */
1038
/* -------------------------------------------------------------------------- */
1039
void
1040
or32_expand_sibcall (rtx  result ATTRIBUTE_UNUSED,
1041
                     rtx  addr,
1042
                     rtx  args_size)
1043
{
1044
  emit_call_insn (gen_sibcall_internal (addr, args_size));
1045
 
1046
}       /* or32_expand_sibcall () */
1047
 
1048
 
1049
/* -------------------------------------------------------------------------- */
1050
/*!Load a 32-bit constant.
1051
 
1052
   We know it can't be done in one insn when we get here, the movsi expander
1053
   guarantees this.
1054
 
1055
   @param[in] op0  RTX for the destination.
1056
   @param[in] op1  RTX for the (constant) source.                             */
1057
/* -------------------------------------------------------------------------- */
1058
void
1059
or32_emit_set_const32 (rtx  op0,
1060
                       rtx  op1)
1061
{
1062
  enum machine_mode mode = GET_MODE (op0);
1063
  rtx temp;
1064
 
1065
  /* Sanity check that we really can't do it in one instruction. I.e that we
1066
     don't have a 16-bit constant. */
1067
  if (GET_CODE (op1) == CONST_INT)
1068
    {
1069
      HOST_WIDE_INT val = INTVAL (op1) & GET_MODE_MASK (mode);
1070
 
1071
      if ((-32768 <= val) && (val <= 32767))
1072
        {
1073
          abort ();
1074
        }
1075
    }
1076
 
1077
  /* Full 2-insn decomposition is needed.  */
1078
  if (reload_in_progress || reload_completed)
1079
    temp = op0;
1080
  else
1081
    temp = gen_reg_rtx (mode);
1082
 
1083
  if (GET_CODE (op1) == CONST_INT)
1084
    {
1085
      /* Emit them as real moves instead of a HIGH/LO_SUM,
1086
         this way CSE can see everything and reuse intermediate
1087
         values if it wants.  */
1088
      emit_insn (gen_rtx_SET (VOIDmode, temp,
1089
                              GEN_INT (INTVAL (op1)
1090
                                       & ~(HOST_WIDE_INT) 0xffff)));
1091
 
1092
      emit_insn (gen_rtx_SET (VOIDmode,
1093
                              op0,
1094
                              gen_rtx_IOR (mode, temp,
1095
                                           GEN_INT (INTVAL (op1) & 0xffff))));
1096
    }
1097
  else
1098
    {
1099
      /* since or32 bfd can not deal with relocs that are not of type
1100
         OR32_CONSTH_RELOC + OR32_CONST_RELOC (ie move high must be
1101
         followed by exactly one lo_sum)
1102
       */
1103
      emit_insn (gen_movsi_insn_big (op0, op1));
1104
    }
1105
}       /* or32_emit_set_const32 () */
1106
 
1107
 
1108
/* ========================================================================== */
1109
/* Target hook functions.
1110
 
1111
   These are initialized at the end of this file, to avoid having to
1112
   predeclare all the functions. They are only needed here, so are static.    */
1113
 
1114
 
1115
/* -------------------------------------------------------------------------- */
1116
/*!Set up the stack and frame pointer (if desired) for the function.
1117
 
1118
   If defined, a function that outputs the assembler code for entry to a
1119
   function. The prologue is responsible for setting up the stack frame,
1120
   initializing the frame pointer register, saving registers that must be
1121
   saved, and allocating "size" additional bytes of storage for the local
1122
   variables. "size" is an integer. "file" is a stdio stream to which the
1123
   assembler code should be output.
1124
 
1125
   The label for the beginning of the function need not be output by this
1126
   macro. That has already been done when the macro is run.
1127
 
1128
   To determine which registers to save, the macro can refer to the array
1129
   "regs_ever_live": element "r" is nonzero if hard register "r" is used
1130
   anywhere within the function.  This implies the function prologue should
1131
   save register r, provided it is not one of the call-used
1132
   registers. (TARGET_ASM_FUNCTION_EPILOGUE must likewise use
1133
   "regs_ever_live".)
1134
 
1135
   On machines that have "register windows", the function entry code does not
1136
   save on the stack the registers that are in the windows, even if they are
1137
   supposed to be preserved by function calls; instead it takes appropriate
1138
   steps to “push” the register stack, if any non-call-used registers are used
1139
   in the function.
1140
 
1141
   On machines where functions may or may not have frame-pointers, the
1142
   function entry code must vary accordingly; it must set up the frame pointer
1143
   if one is wanted, and not otherwise. To determine whether a frame pointer
1144
   is in wanted, the macro can refer to the variable frame_pointer_needed. The
1145
   variable’s value will be 1 at run time in a function that needs a frame
1146
   pointer. See the section on "Eliminating Frame Pointer and Arg Pointer" in
1147
   the "Target Description Macros and Functions" chapter of the GCC internals
1148
   manual.
1149
 
1150
   The function entry code is responsible for allocating any stack space
1151
   required for the function. This stack space consists of the regions listed
1152
   below. In most cases, these regions are allocated in the order listed, with
1153
   the last listed region closest to the top of the stack (the lowest address
1154
   if STACK_GROWS_DOWNWARD is defined, and the highest address if it is not
1155
   defined). You can use a different order for a machine if doing so is more
1156
   convenient or required for compatibility reasons. Except in cases where
1157
   required by standard or by a debugger, there is no reason why the stack
1158
   layout used by GCC need agree with that used by other compilers for a
1159
   machine.
1160
 
1161
   @param[in] file  File handle for any generated code.
1162
   @param[in] size  Number of bytes of storage needed for local variables.    */
1163
/* -------------------------------------------------------------------------- */
1164
static void
1165
or32_output_function_prologue (FILE          *file,
1166
                               HOST_WIDE_INT  size)
1167
{
1168
  int save_area;
1169
  int gpr_save_area;
1170
  int lr_save_area;
1171
  int fp_save_area;
1172
  int stack_size;
1173
  int regno;
1174
 
1175
  /* If we are doing the prologue using the "prologue" pattern in the machine
1176
     description, do nothing more here.
1177
 
1178
     JPB 30-Aug-10: Surely that is not correct. If this option is set, we
1179
     should never even be called! */
1180
  if (TARGET_MASK_SCHED_LOGUE)
1181
    return;
1182
 
1183
  if (size < 0)
1184
    abort ();
1185
 
1186
  /* Work out and log the frame size */
1187
  stack_size = calculate_stack_size (size, &lr_save_area, &fp_save_area,
1188
                                     &gpr_save_area, &save_area);
1189
 
1190
  fprintf (file,
1191
           "\n\t# gpr_save_area %d size %ld crtl->outgoing_args_size %d\n",
1192
           gpr_save_area, size, crtl->outgoing_args_size);
1193
 
1194
  /* Decrement the stack pointer by the total frame size (if we have a
1195
     frame). */
1196
  if (stack_size > 0)
1197
    {
1198
      /* Special code for large stack frames */
1199
      if (stack_size >= 0x8000)
1200
        {
1201
          fprintf (file, "\tl.movhi\tr%d,hi(%d)\n", GP_ARG_RETURN, stack_size);
1202
          fprintf (file, "\tl.ori\tr%d,r%d,lo(%d)\n", GP_ARG_RETURN,
1203
                   GP_ARG_RETURN, stack_size);
1204
          fprintf (file, "\tl.sub\tr%d,r%d,r%d\n", STACK_POINTER_REGNUM,
1205
                   STACK_POINTER_REGNUM, GP_ARG_RETURN);
1206
        }
1207
      else
1208
        {
1209
          fprintf (file, "\tl.addi\tr%d,r%d,%d\n", STACK_POINTER_REGNUM,
1210
                   STACK_POINTER_REGNUM, -stack_size);
1211
        }
1212
 
1213
      /* Update the DWARF2 CFA using the new stack pointer. After this the CFA
1214
         will be the SP + frame size, i.e. the FP (or start of frame if we
1215
         don't actually have a FP). All register refs should relate to this. */
1216
      if (dwarf2out_do_frame ())
1217
        {
1218
          char *l = dwarf2out_cfi_label (false);
1219
 
1220
          dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, stack_size);
1221
        }
1222
    }
1223
 
1224
  /* Update the frame pointer if necessary */
1225
  if (fp_save_area)
1226
    {
1227
      char *l     = dwarf2out_cfi_label (false);
1228
      int  offset = OR32_ALIGN (crtl->outgoing_args_size, 4) + lr_save_area;
1229
 
1230
      fprintf (file, "\tl.sw\t%d(r%d),r%d\n", offset,
1231
               STACK_POINTER_REGNUM, FRAME_POINTER_REGNUM);
1232
 
1233
      if (stack_size >= 0x8000)
1234
        fprintf (file, "\tl.add\tr%d,r%d,r%d\n", FRAME_POINTER_REGNUM,
1235
                 STACK_POINTER_REGNUM, GP_ARG_RETURN);
1236
      else
1237
        fprintf (file, "\tl.addi\tr%d,r%d,%d\n", FRAME_POINTER_REGNUM,
1238
                 STACK_POINTER_REGNUM, stack_size);
1239
 
1240
      /* The CFA is already pointing at the start of our frame (i.e. the new
1241
         FP). The old FP has been saved relative to the SP, so we need to use
1242
         stack_size to work out where. */
1243
      dwarf2out_reg_save (l, FRAME_POINTER_REGNUM, offset - stack_size);
1244
    }
1245
 
1246
  /* Save the return address if necessary */
1247
  if (lr_save_area)
1248
    {
1249
      char *l     = dwarf2out_cfi_label (false);
1250
      int  offset = OR32_ALIGN (crtl->outgoing_args_size, 4);
1251
 
1252
      fprintf (file, "\tl.sw\t%d(r%d),r%d\n", offset, STACK_POINTER_REGNUM,
1253
               LINK_REGNUM);
1254
 
1255
      /* The CFA is already pointing at the start of our frame (i.e. the new
1256
         FP). The LR has been saved relative to the SP, so we need to use
1257
         stack_size to work out where. */
1258
      dwarf2out_reg_save (l, FRAME_POINTER_REGNUM, offset - stack_size);
1259
    }
1260
 
1261
  save_area = (OR32_ALIGN (crtl->outgoing_args_size, 4)
1262
               + lr_save_area + fp_save_area);
1263
 
1264
  /* Save any callee saved registers */
1265
  for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1266
    {
1267
      if (df_regs_ever_live_p(regno) && !call_used_regs[regno])
1268
        {
1269
          char *l = dwarf2out_cfi_label (false);
1270
 
1271
          fprintf (file, "\tl.sw\t%d(r%d),r%d\n", save_area,
1272
                   STACK_POINTER_REGNUM, regno);
1273
 
1274
          /* The CFA is already pointing at the start of our frame (i.e. the
1275
             new FP). The register has been saved relative to the SP, so we
1276
             need to use stack_size to work out where. */
1277
          dwarf2out_reg_save (l, FRAME_POINTER_REGNUM, save_area - stack_size);
1278
          save_area += 4;
1279
        }
1280
    }
1281
}       /* or32_output_function_prologue () */
1282
 
1283
 
1284
/* -------------------------------------------------------------------------- */
1285
/*!Do any necessary cleanup after a function to restore stack, frame, and regs.
1286
 
1287
   This is a function that outputs the assembler code for exit from a
1288
   function. The epilogue is responsible for restoring the saved registers and
1289
   stack pointer to their values when the function was called, and returning
1290
   control to the caller. This macro takes the same arguments as the macro
1291
   TARGET_ASM_FUNCTION_PROLOGUE, and the registers to restore are determined
1292
   from regs_ever_live and CALL_USED_REGISTERS in the same way (@see
1293
   or32_output_function_prologue ()) .
1294
 
1295
   On some machines, there is a single instruction that does all the work of
1296
   returning from the function. On these machines, give that instruction the
1297
   name "return" (in the machine definition) and do not define the macro
1298
   TARGET_ASM_FUNCTION_EPILOGUE at all.
1299
 
1300
   Do not define a pattern named "return" if you want the
1301
   TARGET_ASM_FUNCTION_EPILOGUE to be used. If you want the target switches to
1302
   control whether return instructions or epilogues are used, define a
1303
   "return" pattern with a validity condition that tests the target switches
1304
   appropriately. If the "return" pattern’s validity condition is false,
1305
   epilogues will be used.
1306
 
1307
   On machines where functions may or may not have frame-pointers, the
1308
   function exit code must vary accordingly. Sometimes the code for these two
1309
   cases is completely different. To determine whether a frame pointer is
1310
   wanted, the macro can refer to the variable frame_pointer_needed. The
1311
   variable’s value will be 1 when compiling a function that needs a frame
1312
   pointer.
1313
 
1314
   Normally, TARGET_ASM_FUNCTION_PROLOGUE and TARGET_ASM_FUNCTION_EPILOGUE
1315
   must treat leaf functions specially. The C variable
1316
   "current_function_is_leaf" is nonzero for such a function. See "Handling
1317
   Leaf Functions" in the "Target Description Macros and Functions" section of
1318
   the GCC internals manual.
1319
 
1320
   On some machines, some functions pop their arguments on exit while others
1321
   leave that for the caller to do. For example, the 68020 when given "-mrtd"
1322
   pops arguments in functions that take a fixed number of arguments.
1323
 
1324
   Your definition of the macro RETURN_POPS_ARGS decides which functions pop
1325
   their own arguments. TARGET_ASM_FUNCTION_EPILOGUE needs to know what was
1326
   decided.  The number of bytes of the current function’s arguments that this
1327
   function should pop is available in "crtl->args.pops_args". See "How Scalar
1328
   Function Values Are Returned" in the "Target Description Macros and
1329
   Functions" section of the GCC internals manual.
1330
 
1331
   @param[in] file  File handle for any generated code.
1332
   @param[in] size  Number of bytes of storage needed for local variables.    */
1333
/* -------------------------------------------------------------------------- */
1334
static void
1335
or32_output_function_epilogue (FILE * file, HOST_WIDE_INT size)
1336
{
1337
  int save_area;
1338
  int gpr_save_area;
1339
  int lr_save_area;
1340
  int fp_save_area;
1341
  int stack_size;
1342
  int regno;
1343
 
1344
  /* If we are doing the epilogue using the "epilogue" pattern in the machine
1345
     description, do nothing more here.
1346
 
1347
     JPB 30-Aug-10: Surely that is not correct. If this option is set, we
1348
     should never even be called! */
1349
  if (TARGET_MASK_SCHED_LOGUE)
1350
    return;
1351
 
1352
  /* Work out the frame size */
1353
  stack_size = calculate_stack_size (size, &lr_save_area, &fp_save_area,
1354
                                     &gpr_save_area, &save_area);
1355
 
1356
  /* Restore the return address if necessary */
1357
  if (lr_save_area)
1358
    {
1359
      fprintf (file, "\tl.lwz\tr%d,%d(r%d)\n", LINK_REGNUM,
1360
               OR32_ALIGN (crtl->outgoing_args_size, 4),
1361
               STACK_POINTER_REGNUM);
1362
    }
1363
 
1364
  /* Restore the frame pointer if necessary */
1365
  if (fp_save_area)
1366
    {
1367
      fprintf (file, "\tl.lwz\tr%d,%d(r%d)\n", FRAME_POINTER_REGNUM,
1368
               OR32_ALIGN (crtl->outgoing_args_size, 4)
1369
               + lr_save_area, STACK_POINTER_REGNUM);
1370
    }
1371
 
1372
  save_area = (OR32_ALIGN (crtl->outgoing_args_size, 4)
1373
               + lr_save_area + fp_save_area);
1374
 
1375
  /* Restore any callee-saved registers */
1376
  for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1377
    {
1378
      if (df_regs_ever_live_p(regno) && !call_used_regs[regno])
1379
        {
1380
          fprintf (file, "\tl.lwz\tr%d,%d(r%d)\n", regno, save_area,
1381
                   STACK_POINTER_REGNUM);
1382
          save_area += 4;
1383
        }
1384
    }
1385
 
1386
  /* Restore the stack pointer (if necessary) */
1387
  if (stack_size >= 0x8000)
1388
    {
1389
      fprintf (file, "\tl.movhi\tr3,hi(%d)\n", stack_size);
1390
      fprintf (file, "\tl.ori\tr3,r3,lo(%d)\n", stack_size);
1391
 
1392
      if (!TARGET_MASK_ALIGNED_JUMPS)
1393
        fprintf (file, "\tl.jr\tr%d\n", LINK_REGNUM);
1394
      else
1395
        {
1396
          fprintf (file, "\t.balignl\t0x8,0x15000015,0x4\n");
1397
          fprintf (file, "\tl.jr\tr%d\n", LINK_REGNUM);
1398
        }
1399
 
1400
      fprintf (file, "\tl.add\tr%d,r%d,r3\n", STACK_POINTER_REGNUM,
1401
               STACK_POINTER_REGNUM);
1402
    }
1403
  else if (stack_size > 0)
1404
    {
1405
      if (!TARGET_MASK_ALIGNED_JUMPS)
1406
        fprintf (file, "\tl.jr\tr%d\n", LINK_REGNUM);
1407
      else
1408
        {
1409
          fprintf (file, "\t.balignl 0x8,0x15000015,0x4\n");
1410
          fprintf (file, "\tl.jr\tr%d\n", LINK_REGNUM);
1411
        }
1412
 
1413
      fprintf (file, "\tl.addi\tr%d,r%d,%d\n", STACK_POINTER_REGNUM,
1414
               STACK_POINTER_REGNUM, stack_size);
1415
    }
1416
  else
1417
    {
1418
      if (!TARGET_MASK_ALIGNED_JUMPS)
1419
        fprintf (file, "\tl.jr\tr%d\n", LINK_REGNUM);
1420
      else
1421
        {
1422
          fprintf (file, "\t.balignl\t0x8,0x15000015,0x4\n");
1423
          fprintf (file, "\tl.jr\tr%d\n", LINK_REGNUM);
1424
        }
1425
 
1426
      fprintf (file, "\tl.nop\n");              /* Delay slot */
1427
    }
1428
}       /* or32_output_function_epilogue () */
1429
 
1430
 
1431
/* -------------------------------------------------------------------------- */
1432
/*!Define where a function returns values.
1433
 
1434
   Define this to return an RTX representing the place where a function
1435
   returns or receives a value of data type ret type, a tree node representing
1436
   a data type.  "func" is a tree node representing FUNCTION_DECL or
1437
   FUNCTION_TYPE of a function being called. If "outgoing" is false, the hook
1438
   should compute the register in which the caller will see the return
1439
   value. Otherwise, the hook should return an RTX representing the place
1440
   where a function returns a value.
1441
 
1442
   On many machines, only TYPE_MODE ("ret_type") is relevant. (Actually, on
1443
   most machines, scalar values are returned in the same place regardless of
1444
   mode.) The value of the expression is usually a reg RTX for the hard
1445
   register where the return value is stored. The value can also be a parallel
1446
   RTX, if the return value is in multiple places. See FUNCTION_ARG for an
1447
   explanation of the parallel form. Note that the callee will populate every
1448
   location specified in the parallel, but if the first element of the
1449
   parallel contains the whole return value, callers will use that element as
1450
   the canonical location and ignore the others. The m68k port uses this type
1451
   of parallel to return pointers in both ‘%a0’ (the canonical location) and
1452
   ‘%d0’.
1453
 
1454
   If TARGET_PROMOTE_FUNCTION_RETURN returns true, you must apply the same
1455
   promotion rules specified in PROMOTE_MODE if valtype is a scalar type.
1456
 
1457
   If the precise function being called is known, "func" is a tree node
1458
   (FUNCTION_DECL) for it; otherwise, "func" is a null pointer. This makes it
1459
   possible to use a different value-returning convention for specific
1460
   functions when all their calls are known.
1461
 
1462
   Some target machines have "register windows" so that the register in which
1463
   a function returns its value is not the same as the one in which the caller
1464
   sees the value. For such machines, you should return different RTX
1465
   depending on outgoing.
1466
 
1467
   TARGET_FUNCTION_VALUE is not used for return values with aggregate data
1468
   types, because these are returned in another way. See
1469
   TARGET_STRUCT_VALUE_RTX and related macros.
1470
 
1471
   For the OR32, we can just use the result of LIBCALL_VALUE, since all
1472
   functions return their result in the same place (register rv = r11).
1473
 
1474
   JPB 30-Aug-10: What about 64-bit scalar returns (long long int, double),
1475
                  which also use rvh (=r12)?
1476
 
1477
   @param[in] ret_type  The return type of the function.
1478
   @param[in] func      Tree representing function being called.
1479
   @param[in] outgoing  Non-zero (TRUE) if the result represents where the
1480
                        function places the results, zero (FALSE) if the
1481
                        result represents where the caller sees the result.
1482
 
1483
   @return  A RTX representing where the result can be found.                 */
1484
/* -------------------------------------------------------------------------- */
1485
static rtx
1486
or32_function_value (const_tree  ret_type,
1487
                     const_tree  func ATTRIBUTE_UNUSED,
1488
                     bool        outgoing ATTRIBUTE_UNUSED)
1489
{
1490
  return LIBCALL_VALUE (TYPE_MODE(ret_type));
1491
 
1492
}       /* or32_function_value () */
1493
 
1494
 
1495
/* -------------------------------------------------------------------------- */
1496
/*!Check if a function is suitable for tail call optimization.
1497
 
1498
   True if it is OK to do sibling call optimization for the specified call
1499
   expression "exp". "decl" will be the called function, or NULL if this is an
1500
   indirect call.
1501
 
1502
   It is not uncommon for limitations of calling conventions to prevent tail
1503
   calls to functions outside the current unit of translation, or during PIC
1504
   compilation. The hook is used to enforce these restrictions, as the sibcall
1505
   md pattern can not fail, or fall over to a “normal” call. The criteria for
1506
   successful sibling call optimization may vary greatly between different
1507
   architectures.
1508
 
1509
   For the OR32, we currently allow sibcall optimization if the -msibcall
1510
   argument is passed.
1511
 
1512
   JPB 30-Aug-10: Surely we should always allow this?
1513
 
1514
   @param[in] decl  The function for which we may optimize
1515
   @param[in] exp   The call expression which is candidate for optimization.
1516
 
1517
   @return  Non-zero (TRUE) if sibcall optimization is permitted, zero (FALSE)
1518
            otherwise.                                                        */
1519
/* -------------------------------------------------------------------------- */
1520
static bool
1521
or32_function_ok_for_sibcall (tree  decl ATTRIBUTE_UNUSED,
1522
                              tree  exp ATTRIBUTE_UNUSED)
1523
{
1524
  return TARGET_MASK_SIBCALL;
1525
 
1526
}       /* or32_function_ok_for_sibcall () */
1527
 
1528
 
1529
/* -------------------------------------------------------------------------- */
1530
/*!Should an argument be passed by reference.
1531
 
1532
   This target hook should return true if an argument at the position
1533
   indicated by "cum" should be passed by reference. This predicate is queried
1534
   after target independent reasons for being passed by reference, such as
1535
   TREE_ADDRESSABLE ("type").
1536
 
1537
   If the hook returns TRUE, a copy of that argument is made in memory and a
1538
   pointer to the argument is passed instead of the argument itself. The
1539
   pointer is passed in whatever way is appropriate for passing a pointer to
1540
   that type.
1541
 
1542
   For the OR32, all aggregates and arguments greater than 8 bytes are passed
1543
   this way.
1544
 
1545
   @param[in] cum    Position of argument under consideration.
1546
   @param[in[ mode   Not sure what this relates to.
1547
   @param[in] type   Type of the argument.
1548
   @param[in] named  Not sure what this relates to.
1549
 
1550
   @return  Non-zero (TRUE) if the argument should be passed by reference,
1551
            zero (FALSE) otherwise.                                           */
1552
/* -------------------------------------------------------------------------- */
1553
static bool
1554
or32_pass_by_reference (CUMULATIVE_ARGS   *cum ATTRIBUTE_UNUSED,
1555
                        enum machine_mode  mode ATTRIBUTE_UNUSED,
1556
                        const_tree         type,
1557
                        bool               named ATTRIBUTE_UNUSED)
1558
{
1559
  return (type && (AGGREGATE_TYPE_P (type) || int_size_in_bytes (type) > 8));
1560
 
1561
}       /* or32_pass_by_reference () */
1562
 
1563
 
1564
/* -------------------------------------------------------------------------- */
1565
/*!Is a frame pointer required?
1566
 
1567
   This target hook should return TRUE if a function must have and use a frame
1568
   pointer.  This target hook is called in the reload pass. If its return
1569
   value is TRUE the function will have a frame pointer.
1570
 
1571
   This target hook can in principle examine the current function and decide
1572
   according to the facts, but on most machines the constant false or the
1573
   constant true suffices.  Use FALSE when the machine allows code to be
1574
   generated with no frame pointer, and doing so saves some time or space. Use
1575
   TRUE when there is no possible advantage to avoiding a frame pointer.
1576
 
1577
   In certain cases, the compiler does not know how to produce valid code
1578
   without a frame pointer. The compiler recognizes those cases and
1579
   automatically gives the function a frame pointer regardless of what
1580
   TARGET_FRAME_POINTER_REQUIRED returns.  You don’t need to worry about them.
1581
 
1582
   In a function that does not require a frame pointer, the frame pointer
1583
   register can be allocated for ordinary usage, unless you mark it as a fixed
1584
   register. See FIXED_REGISTERS for more information.
1585
 
1586
   Default return value is false.
1587
 
1588
   For the OR32 we do not need the frame pointer, so the default would have
1589
   sufficed.
1590
 
1591
   JPB 30-Aug-10: The version supplied returned TRUE, which is patently the
1592
                  wrong answer. This function really could be eliminated and
1593
                  the default used.
1594
 
1595
   @return  Non-zero (TRUE) if a frame pointer is not required, zero (FALSE)
1596
            otherwise.                                                        */
1597
/* -------------------------------------------------------------------------- */
1598
static bool
1599
or32_frame_pointer_required (void)
1600
{
1601
        return 1;
1602
 
1603
}       /* or32_frame_pointer_required () */
1604
 
1605
 
1606
/* -------------------------------------------------------------------------- */
1607
/*!How many bytes at the beginning of an argument must be put into registers.
1608
 
1609
   This target hook returns the number of bytes at the beginning of an
1610
   argument that must be put in registers. The value must be zero for
1611
   arguments that are passed entirely in registers or that are entirely pushed
1612
   on the stack.
1613
 
1614
   On some machines, certain arguments must be passed partially in registers
1615
   and partially in memory. On these machines, typically the first few words
1616
   of arguments a re passed in registers, and the rest on the stack. If a
1617
   multi-word argument (a double or a structure) crosses that boundary, its
1618
   first few words must be passed in registers and the rest must be
1619
   pushed. This macro tells the compiler when this occurs, and how many bytes
1620
   should go in registers.
1621
 
1622
   FUNCTION_ARG for these arguments should return the first register to be
1623
   used by the caller for this argument; likewise FUNCTION_INCOMING_ARG, for
1624
   the called function.
1625
 
1626
   On the OR32 we never split argumetns between registers and memory.
1627
 
1628
   JPB 30-Aug-10: Is this correct? Surely we should allow this. The ABI spec
1629
                  is incomplete on this point.
1630
 
1631
   @param[in] cum    Position of argument under consideration.
1632
   @param[in[ mode   Not sure what this relates to.
1633
   @param[in] type   Type of the argument.
1634
   @param[in] named  Not sure what this relates to.
1635
 
1636
   @return  The number of bytes of the argument to go into registers          */
1637
/* -------------------------------------------------------------------------- */
1638
static int
1639
or32_arg_partial_bytes (CUMULATIVE_ARGS   *cum ATTRIBUTE_UNUSED,
1640
                        enum machine_mode  mode ATTRIBUTE_UNUSED,
1641
                        tree               type ATTRIBUTE_UNUSED,
1642
                        bool               named ATTRIBUTE_UNUSED)
1643
{
1644
  return 0;
1645
 
1646
}       /* or32_arg_partial_bytes () */
1647
 
1648
 
1649
/* -------------------------------------------------------------------------- */
1650
/*!Promote the mode of a function's arguments/return value.
1651
 
1652
   Like PROMOTE_MODE, but it is applied to outgoing function arguments or
1653
   function return values. The target hook should return the new mode and
1654
   possibly change "*punsignedp" if the promotion should change
1655
   signedness. This function is called only for scalar or pointer types.
1656
 
1657
   "for_return" allows to distinguish the promotion of arguments and return
1658
   values. If it is 1, a return value is being promoted and
1659
   TARGET_FUNCTION_VALUE must perform the same promotions done here. If it is
1660
   2, the returned mode should be that of the register in which an incoming
1661
   parameter is copied, or the outgoing result is computed; then the hook
1662
   should return the same mode as PROMOTE_MODE, though the signedness may be
1663
   different.
1664
 
1665
   The default is to not promote arguments and return values. You can also
1666
   define the hook to "default_promote_function_mode_always_promote" if you
1667
   would like to apply the same rules given by PROMOTE_MODE.
1668
 
1669
   For the OR32, if the size of the mode is integral and less than 4, we
1670
   promote to SImode, otherwise we return the mode we are supplied.
1671
 
1672
   @param[in]  type        Not sure. Type of the argument?
1673
   @param[in]  mode        The mode of argument/return value to consider.
1674
   @param[out] punsignedp  Signedness of the value.
1675
   @param[in]  fntype      Not sure. Type of the function?
1676
   @param[in]  for_return  1 if a return value, 2 if an incoming value.
1677
 
1678
   @return  The new mode.                                                     */
1679
/* -------------------------------------------------------------------------- */
1680
static enum machine_mode
1681
or32_promote_function_mode (const_tree         type ATTRIBUTE_UNUSED,
1682
                            enum machine_mode  mode,
1683
                            int               *punsignedp ATTRIBUTE_UNUSED,
1684
                            const_tree         fntype ATTRIBUTE_UNUSED,
1685
                            int                for_return ATTRIBUTE_UNUSED)
1686
{
1687
  return (   (GET_MODE_CLASS (mode) == MODE_INT)
1688
          && (GET_MODE_SIZE (mode) < 4)) ? SImode : mode;
1689
 
1690
}       /* or32_promote_function_mode () */
1691
 
1692
 
1693
/* -------------------------------------------------------------------------- */
1694
/*!Is this a legitimate address?
1695
 
1696
  A function that returns whether x (an RTX) is a legitimate memory address on
1697
  the target machine for a memory operand of mode mode.
1698
 
1699
  Legitimate addresses are defined in two variants: a strict variant and a
1700
  non-strict one.  The strict parameter chooses which variant is desired by
1701
  the caller.
1702
 
1703
  The strict variant is used in the reload pass. It must be defined so that
1704
  any pseudo- register that has not been allocated a hard register is
1705
  considered a memory reference.  This is because in contexts where some kind
1706
  of register is required, a pseudo-register with no hard register must be
1707
  rejected. For non-hard registers, the strict variant should look up the
1708
  reg_renumber array; it should then proceed using the hard register number in
1709
  the array, or treat the pseudo as a memory reference if the array holds -1.
1710
 
1711
  The non-strict variant is used in other passes. It must be defined to accept
1712
  all pseudo-registers in every context where some kind of register is
1713
  required.
1714
 
1715
  Normally, constant addresses which are the sum of a symbol_ref and an
1716
  integer are stored inside a const RTX to mark them as constant. Therefore,
1717
  there is no need to recognize such sums specifically as legitimate
1718
  addresses. Normally you would simply recognize any const as legitimate.
1719
 
1720
  Usually PRINT_OPERAND_ADDRESS is not prepared to handle constant sums that
1721
  are not marked with const. It assumes that a naked plus indicates
1722
  indexing. If so, then you must reject such naked constant sums as
1723
  illegitimate addresses, so that none of them will be given to
1724
  PRINT_OPERAND_ADDRESS.
1725
 
1726
  On some machines, whether a symbolic address is legitimate depends on the
1727
  section that the address refers to. On these machines, define the target
1728
  hook TARGET_ENCODE_ SECTION_INFO to store the information into the
1729
  symbol_ref, and then check for it here. When you see a const, you will have
1730
  to look inside it to find the symbol_ref in order to determine the
1731
  section. See the internals manual section on "Assembler Format" for more
1732
  info.
1733
 
1734
  Some ports are still using a deprecated legacy substitute for this hook, the
1735
  GO_IF_LEGITIMATE_ADDRESS macro. This macro has this syntax:
1736
 
1737
    #define GO_IF_LEGITIMATE_ADDRESS (mode, x, label )
1738
 
1739
  and should goto label if the address x is a valid address on the target
1740
  machine for a memory operand of mode mode. Whether the strict or non-strict
1741
  variants are desired is defined by the REG_OK_STRICT macro introduced
1742
  earlier in this section. Using the hook is usually simpler because it limits
1743
  the number of files that are recompiled when changes are made.
1744
 
1745
   The OR32 only has a single addressing mode, which is a base register with
1746
   16-bit displacement. We can accept just 16-bit constants as addresses (they
1747
   can use r0 as base address, and we can accept plain registers as addresses
1748
   (they can use a displacement of zero).
1749
 
1750
   @param[in] mode    The mode of the address
1751
   @param[in] x       The address (RTX)
1752
   @param[in] strict  Non-zero (TRUE) if we are in "strict" mode, zero (FALSE)
1753
                      otherwise.
1754
 
1755
   @return  Non-zero (TRUE) if this is a legitimate address, zero (FALSE)
1756
            otherwise.                                                        */
1757
/* -------------------------------------------------------------------------- */
1758
static bool
1759
or32_legitimate_address_p (enum machine_mode  mode,
1760
                           rtx                x,
1761
                           bool               strict)
1762
{
1763
  /* You might think 16-bit constants are suitable. They can be built into
1764
     addresses using r0 as the base. However this seems to lead to defective
1765
     code. So for now this is a placeholder, and this code is not used.
1766
 
1767
     if (or32_legitimate_displacement_p (mode, x))
1768
       {
1769
         return  1;
1770
       }
1771
  */
1772
 
1773
  /* Addresses consisting of a register and 16-bit displacement are also
1774
     suitable. We need the mode, since for double words, we had better be
1775
     able to address the full 8 bytes. */
1776
  if (GET_CODE(x) == PLUS)
1777
    {
1778
      rtx reg = XEXP(x,0);
1779
 
1780
      /* If valid register... */
1781
      if ((GET_CODE(reg) == REG)
1782
          && or32_regnum_ok_for_base_p (INTVAL (reg), strict))
1783
        {
1784
          rtx offset = XEXP(x,1);
1785
 
1786
          /* ...and valid offset */
1787
          if (or32_legitimate_displacement_p (mode, offset))
1788
            {
1789
              return 1;
1790
            }
1791
        }
1792
    }
1793
 
1794
  /* Addresses consisting of just a register are OK. They can be built into
1795
     addresses using an offset of zero (and an offset of four if double
1796
     word). */
1797
  if (GET_CODE(x) == REG && or32_regnum_ok_for_base_p( INTVAL (x), strict))
1798
    {
1799
      return 1;
1800
    }
1801
 
1802
  return 0;
1803
 
1804
}       /* or32_legitimate_address_p () */
1805
 
1806
 
1807
/* -------------------------------------------------------------------------- */
1808 332 jeremybenn
/*!Initialize a trampoline for nested functions.
1809
 
1810
   A nested function is defined by *two* pieces of information, the address of
1811
   the function (like any other function) and a pointer to the frame of the
1812
   enclosing function. The latter is required to allow the nested function to
1813
   access local variables in the enclosing function's frame.
1814
 
1815
   This represents a problem, since a function in C is represented as an
1816
   address that can be held in a single variable as a pointer. Requiring two
1817
   pointers will not fit.
1818
 
1819
   The solution is documented in "Lexical Closures for C++" by Thomas
1820
   M. Breuel (USENIX C++ Conference Proceedings, October 17-21, 1988). The
1821
   nested function is represented by a small block of code and data on the
1822
   enclosing function's stack frame, which sets up a pointer to the enclosing
1823
   function's stack frame (the static chain pointer) in a register defined by
1824
   the ABI, and then jumps to the code of the function proper.
1825
 
1826
   The function can be represented as a single pointer to this block of code,
1827
   known as a trampoline, which when called generates both pointers
1828
   needed. The nested function (which knows it is a nested function at compile
1829
   time) can then generate code to access the enclosing frame via the static
1830
   chain register.
1831
 
1832
   There is a catch that the trampoline is set up as data, but executed as
1833
   instructions. The former will be via the data cache, the latter via the
1834
   instruction cache. There is a risk that a later trampoline will not be seen
1835
   by the instruction cache, so the wrong code will be executed. So the
1836
   instruction cache should be flushed for the trampoline address range.
1837
 
1838
   This hook is called to initialize a trampoline. "m_tramp" is an RTX for the
1839
   memory block for the trampoline; "fndecl" is the FUNCTION_DECL for the
1840
   nested function; "static_chain" is an RTX for the static chain value that
1841
   should be passed to the function when it is called.
1842
 
1843
   If the target defines TARGET_ASM_TRAMPOLINE_TEMPLATE, then the first thing
1844
   this hook should do is emit a block move into "m_tramp" from the memory
1845
   block returned by assemble_trampoline_template. Note that the block move
1846
   need only cover the constant parts of the trampoline. If the target
1847
   isolates the variable parts of the trampoline to the end, not all
1848
   TRAMPOLINE_SIZE bytes need be copied.
1849
 
1850
   If the target requires any other actions, such as flushing caches or
1851
   enabling stack execution, these actions should be performed after
1852
   initializing the trampoline proper.
1853
 
1854
   For the OR32, no static chain register is used. We choose to use the return
1855
   value (rv) register. The rvh register is used as a temporary. The code is
1856
   based on that for MIPS. The trampoline code is:
1857
 
1858
              l.movhi r12,hi(end_addr)
1859
              l.ori   r12,lo(end_addr)
1860
              l.lwz   r13,4(r12)
1861
              l.jr    r13
1862
              l.lwz   r11,0(r12)
1863
      end_addr:
1864
              .word   <static chain>
1865
              .word   <nested_function>
1866
 
1867
   @note For the OR32 we need to flush the instruction cache, which is a
1868
         privileged operation. Needs fixing.
1869
 
1870
   @param[in] m_tramp      The lowest address of the trampoline on the stack.
1871
   @param[in] fndecl       Declaration of the enclosing function.
1872
   @param[in] chain_value  Static chain pointer to pass to the nested
1873
                           function.                                          */
1874
/* -------------------------------------------------------------------------- */
1875
static void
1876
or32_trampoline_init (rtx   m_tramp,
1877
                      tree  fndecl,
1878
                      rtx   chain_value)
1879
{
1880
  rtx  addr;                            /* Start address of the trampoline */
1881
  rtx  end_addr;                        /* End address of the code block */
1882
 
1883
  rtx  high;                            /* RTX for the high part of end_addr */
1884
  rtx  low;                             /* RTX for the low part of end_addr */
1885
  rtx  opcode;                          /* RTX for generated opcodes */
1886
  rtx  mem;                             /* RTX for trampoline memory */
1887
 
1888
  rtx trampoline[5];                    /* The trampoline code */
1889
 
1890
  unsigned int  i;                      /* Index into trampoline */
1891
  unsigned int  j;                      /* General counter */
1892
 
1893
  HOST_WIDE_INT  end_addr_offset;         /* Offset to end of code */
1894
  HOST_WIDE_INT  static_chain_offset;     /* Offset to stack chain word */
1895
  HOST_WIDE_INT  target_function_offset;  /* Offset to func address word */
1896
 
1897
  /* Work out the offsets of the pointers from the start of the trampoline
1898
     code.  */
1899
  end_addr_offset        = or32_trampoline_code_size ();
1900
  static_chain_offset    = end_addr_offset;
1901
  target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
1902
 
1903
  /* Get pointers in registers to the beginning and end of the code block.  */
1904
  addr     = force_reg (Pmode, XEXP (m_tramp, 0));
1905
  end_addr = or32_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
1906
 
1907
  /* Build up the code in TRAMPOLINE.
1908
 
1909
              l.movhi r12,hi(end_addr)
1910
              l.ori   r12,lo(end_addr)
1911
              l.lwz   r13,4(r12)
1912
              l.jr    r13
1913
              l.lwz   r11,0(r12)
1914
       end_addr:
1915
  */
1916
 
1917
  i = 0;
1918
 
1919
  /* Break out the high and low parts of the end_addr */
1920
  high = expand_simple_binop (SImode, LSHIFTRT, end_addr, GEN_INT (16),
1921
                              NULL, false, OPTAB_WIDEN);
1922
  low  = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
1923
 
1924
  /* Emit the l.movhi, adding an operation to OR in the high bits from the
1925
     RTX. */
1926
  opcode = gen_int_mode (OR32_MOVHI (12, 0), SImode);
1927
  trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high, NULL,
1928
                                         false, OPTAB_WIDEN);
1929
 
1930
  /* Emit the l.ori, adding an operations to OR in the low bits from the
1931
     RTX. */
1932
  opcode = gen_int_mode (OR32_ORI (12, 12, 0), SImode);
1933
  trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low, NULL,
1934
                                         false, OPTAB_WIDEN);
1935
 
1936
  /* Emit the l.lwz of the function address. No bits to OR in here, so we can
1937
     do the opcode directly. */
1938
  trampoline[i++] =
1939
    gen_int_mode (OR32_LWZ (13, 12, target_function_offset - end_addr_offset),
1940
                  SImode);
1941
 
1942
  /* Emit the l.jr of the function. No bits to OR in here, so we can do the
1943
     opcode directly. */
1944
  trampoline[i++] = gen_int_mode (OR32_JR (13), SImode);
1945
 
1946
  /* Emit the l.lwz of the static chain. No bits to OR in here, so we can
1947
     do the opcode directly. */
1948
  trampoline[i++] =
1949
    gen_int_mode (OR32_LWZ (STATIC_CHAIN_REGNUM, 12,
1950
                            static_chain_offset - end_addr_offset), SImode);
1951
 
1952
  /* Copy the trampoline code.  Leave any padding uninitialized.  */
1953
  for (j = 0; j < i; j++)
1954
    {
1955
      mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
1956
      or32_emit_move (mem, trampoline[j]);
1957
    }
1958
 
1959
  /* Set up the static chain pointer field.  */
1960
  mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
1961
  or32_emit_move (mem, chain_value);
1962
 
1963
  /* Set up the target function field.  */
1964
  mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
1965
  or32_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
1966
 
1967
  /* Flushing the trampoline from the instruction cache needs to be done
1968
     here. */
1969
 
1970
}       /* or32_trampoline_init () */
1971
 
1972
 
1973
/* -------------------------------------------------------------------------- */
1974 282 jeremybenn
/*!Provide support for DW_AT_calling_convention
1975
 
1976
   Define this to enable the dwarf attribute DW_AT_calling_convention to be
1977
   emitted for each function. Instead of an integer return the enum value for
1978
   the DW_CC_ tag.
1979
 
1980
   To support optional call frame debugging information, you must also define
1981
   INCOMING_RETURN_ADDR_RTX and either set RTX_FRAME_RELATED_P on the prologue
1982
   insns if you use RTL for the prologue, or call "dwarf2out_def_cfa" and
1983
   "dwarf2out_reg_save" as appropriate from TARGET_ASM_FUNCTION_PROLOGUE if
1984
   you don’t.
1985
 
1986
   For the OR32, it should be sufficient to return DW_CC_normal in all cases.
1987
 
1988
   @param[in] function  The function requiring debug information
1989
 
1990
   @return  The enum of the DW_CC tag.                                        */
1991
/* -------------------------------------------------------------------------- */
1992
static int
1993
or32_dwarf_calling_convention (const_tree  function ATTRIBUTE_UNUSED)
1994
{
1995
  return  DW_CC_normal;
1996
 
1997
}       /* or32_dwarf_calling_convention () */
1998
 
1999
 
2000
/* ========================================================================== */
2001
/* Target hook initialization.
2002
 
2003
   In most cases these use the static functions declared above. They have
2004
   defaults, so must be undefined first, before being redefined.
2005
 
2006
   The description of what they do is found with the function above, unless
2007
   it is a standard function, in which case it is defined here (as with
2008
   TARGET_ASM_NAMED_SECTION).
2009
 
2010
   The final declaration is of the global "targetm" structure. */
2011
 
2012
 
2013
/* Output assembly directives to switch to section name. The section should
2014
   have attributes as specified by flags, which is a bit mask of the SECTION_*
2015
   flags defined in ‘output.h’. If decl is non-NULL, it is the VAR_DECL or
2016
   FUNCTION_DECL with which this section is associated.
2017
 
2018
   For OR32, we use the default ELF sectioning. */
2019 332 jeremybenn
#undef  TARGET_ASM_NAMED_SECTION
2020 282 jeremybenn
#define TARGET_ASM_NAMED_SECTION  default_elf_asm_named_section
2021
 
2022 332 jeremybenn
#undef  TARGET_ASM_FUNCTION_PROLOGUE
2023 282 jeremybenn
#define TARGET_ASM_FUNCTION_PROLOGUE or32_output_function_prologue
2024
 
2025 332 jeremybenn
#undef  TARGET_ASM_FUNCTION_EPILOGUE
2026 282 jeremybenn
#define TARGET_ASM_FUNCTION_EPILOGUE or32_output_function_epilogue
2027
 
2028 332 jeremybenn
#undef  TARGET_FUNCTION_VALUE
2029 282 jeremybenn
#define TARGET_FUNCTION_VALUE or32_function_value
2030
 
2031 332 jeremybenn
#undef  TARGET_FUNCTION_OK_FOR_SIBCALL
2032 282 jeremybenn
#define TARGET_FUNCTION_OK_FOR_SIBCALL or32_function_ok_for_sibcall
2033
 
2034 332 jeremybenn
#undef  TARGET_PASS_BY_REFERENCE
2035 282 jeremybenn
#define TARGET_PASS_BY_REFERENCE or32_pass_by_reference
2036
 
2037 332 jeremybenn
#undef  TARGET_FRAME_POINTER_REQUIRED
2038 282 jeremybenn
#define TARGET_FRAME_POINTER_REQUIRED or32_frame_pointer_required
2039
 
2040 332 jeremybenn
#undef  TARGET_ARG_PARTIAL_BYTES
2041 282 jeremybenn
#define TARGET_ARG_PARTIAL_BYTES or32_arg_partial_bytes
2042
 
2043
/* This target hook returns TRUE if an argument declared in a prototype as an
2044
   integral type smaller than int should actually be passed as an int. In
2045
   addition to avoiding errors in certain cases of mismatch, it also makes for
2046
   better code on certain machines.
2047
 
2048
   The default is to not promote prototypes.
2049
 
2050
   For the OR32 we do require this, so use a utility hook, which always
2051
   returns TRUE. */
2052 332 jeremybenn
#undef  TARGET_PROMOTE_PROTOTYPES
2053 282 jeremybenn
#define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
2054
 
2055 332 jeremybenn
#undef  TARGET_PROMOTE_FUNCTION_MODE
2056 282 jeremybenn
#define TARGET_PROMOTE_FUNCTION_MODE or32_promote_function_mode
2057
 
2058 332 jeremybenn
#undef  TARGET_LEGITIMATE_ADDRESS_P
2059 282 jeremybenn
#define TARGET_LEGITIMATE_ADDRESS_P  or32_legitimate_address_p
2060
 
2061 332 jeremybenn
#undef  TARGET_TRAMPOLINE_INIT
2062
#define TARGET_TRAMPOLINE_INIT  or32_trampoline_init
2063
 
2064 282 jeremybenn
#undef TARGET_DWARF_CALLING_CONVENTION
2065
#define TARGET_DWARF_CALLING_CONVENTION  or32_dwarf_calling_convention
2066
 
2067
/* Trampoline stubs are yet to be written. */
2068
/* #define TARGET_ASM_TRAMPOLINE_TEMPLATE */
2069
/* #define TARGET_TRAMPOLINE_INIT */
2070
 
2071
/* Initialize the GCC target structure.  */
2072
struct gcc_target targetm = TARGET_INITIALIZER;

powered by: WebSVN 2.1.0

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