OSDN Git Service

b8cc2fbd2b9e4c604199cdb688ca81794ee3bb77
[pf3gnuchains/gcc-fork.git] / gcc / config / mmix / mmix.c
1 /* Definitions of target machine for GNU compiler, for MMIX.
2    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Hans-Peter Nilsson (hp@bitrange.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public 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 COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "hashtab.h"
30 #include "insn-config.h"
31 #include "output.h"
32 #include "flags.h"
33 #include "tree.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "toplev.h"
37 #include "recog.h"
38 #include "ggc.h"
39 #include "dwarf2.h"
40 #include "debug.h"
41 #include "tm_p.h"
42 #include "integrate.h"
43 #include "target.h"
44 #include "target-def.h"
45 #include "real.h"
46
47 /* First some local helper definitions.  */
48 #define MMIX_FIRST_GLOBAL_REGNUM 32
49
50 /* We'd need a current_function_has_landing_pad.  It's marked as such when
51    a nonlocal_goto_receiver is expanded.  Not just a C++ thing, but
52    mostly.  */
53 #define MMIX_CFUN_HAS_LANDING_PAD (cfun->machine->has_landing_pad != 0)
54
55 /* We have no means to tell DWARF 2 about the register stack, so we need
56    to store the return address on the stack if an exception can get into
57    this function.  FIXME: Narrow condition.  Before any whole-function
58    analysis, regs_ever_live[] isn't initialized.  We know it's up-to-date
59    after reload_completed; it may contain incorrect information some time
60    before that.  Within a RTL sequence (after a call to start_sequence,
61    such as in RTL expanders), leaf_function_p doesn't see all insns
62    (perhaps any insn).  But regs_ever_live is up-to-date when
63    leaf_function_p () isn't, so we "or" them together to get accurate
64    information.  FIXME: Some tweak to leaf_function_p might be
65    preferrable.  */
66 #define MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS                 \
67  (flag_exceptions                                               \
68   && ((reload_completed && regs_ever_live[MMIX_rJ_REGNUM])      \
69       || !leaf_function_p ()))
70
71 #define IS_MMIX_EH_RETURN_DATA_REG(REGNO)       \
72  (current_function_calls_eh_return              \
73   && (EH_RETURN_DATA_REGNO (0) == REGNO         \
74       || EH_RETURN_DATA_REGNO (1) == REGNO      \
75       || EH_RETURN_DATA_REGNO (2) == REGNO      \
76       || EH_RETURN_DATA_REGNO (3) == REGNO))
77
78 /* For the default ABI, we rename registers at output-time to fill the gap
79    between the (statically partitioned) saved registers and call-clobbered
80    registers.  In effect this makes unused call-saved registers to be used
81    as call-clobbered registers.  The benefit comes from keeping the number
82    of local registers (value of rL) low, since there's a cost of
83    increasing rL and clearing unused (unset) registers with lower numbers.
84    Don't translate while outputting the prologue.  */
85 #define MMIX_OUTPUT_REGNO(N)                                    \
86  (TARGET_ABI_GNU                                                \
87   || (int) (N) < MMIX_RETURN_VALUE_REGNUM                       \
88   || (int) (N) > MMIX_LAST_STACK_REGISTER_REGNUM                \
89   || cfun == NULL                                               \
90   || cfun->machine == NULL                                      \
91   || cfun->machine->in_prologue                                 \
92   ? (N) : ((N) - MMIX_RETURN_VALUE_REGNUM                       \
93            + cfun->machine->highest_saved_stack_register + 1))
94
95 /* The %d in "POP %d,0".  */
96 #define MMIX_POP_ARGUMENT()                                             \
97  ((! TARGET_ABI_GNU                                                     \
98    && current_function_return_rtx != NULL                               \
99    && ! current_function_returns_struct)                                \
100   ? (GET_CODE (current_function_return_rtx) == PARALLEL                 \
101      ? GET_NUM_ELEM (XVEC (current_function_return_rtx, 0)) : 1)        \
102   : 0)
103
104 /* The canonical saved comparison operands for non-cc0 machines, set in
105    the compare expander.  */
106 rtx mmix_compare_op0;
107 rtx mmix_compare_op1;
108
109 /* We ignore some options with arguments.  They are passed to the linker,
110    but also ends up here because they start with "-m".  We tell the driver
111    to store them in a variable we don't inspect.  */
112 const char *mmix_cc1_ignored_option;
113
114 /* Declarations of locals.  */
115
116 /* Intermediate for insn output.  */
117 static int mmix_output_destination_register;
118
119 static void mmix_output_shiftvalue_op_from_str
120   PARAMS ((FILE *, const char *, HOST_WIDEST_INT));
121 static void mmix_output_shifted_value PARAMS ((FILE *, HOST_WIDEST_INT));
122 static void mmix_output_condition PARAMS ((FILE *, rtx, int));
123 static HOST_WIDEST_INT mmix_intval PARAMS ((rtx));
124 static void mmix_output_octa PARAMS ((FILE *, HOST_WIDEST_INT, int));
125 static bool mmix_assemble_integer PARAMS ((rtx, unsigned int, int));
126 static struct machine_function * mmix_init_machine_status PARAMS ((void));
127 static void mmix_encode_section_info PARAMS ((tree, int));
128 static const char *mmix_strip_name_encoding PARAMS ((const char *));
129 static void mmix_emit_sp_add PARAMS ((HOST_WIDE_INT offset));
130 static void mmix_target_asm_function_prologue
131   PARAMS ((FILE *, HOST_WIDE_INT));
132 static void mmix_target_asm_function_end_prologue PARAMS ((FILE *));
133 static void mmix_target_asm_function_epilogue
134   PARAMS ((FILE *, HOST_WIDE_INT));
135 static void mmix_asm_output_mi_thunk
136   PARAMS ((FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT, tree));
137 static bool mmix_rtx_costs
138   PARAMS ((rtx, int, int, int *));
139
140
141 /* Target structure macros.  Listed by node.  See `Using and Porting GCC'
142    for a general description.  */
143
144 /* Node: Function Entry */
145
146 #undef TARGET_ASM_BYTE_OP
147 #define TARGET_ASM_BYTE_OP NULL
148 #undef TARGET_ASM_ALIGNED_HI_OP
149 #define TARGET_ASM_ALIGNED_HI_OP NULL
150 #undef TARGET_ASM_ALIGNED_SI_OP
151 #define TARGET_ASM_ALIGNED_SI_OP NULL
152 #undef TARGET_ASM_ALIGNED_DI_OP
153 #define TARGET_ASM_ALIGNED_DI_OP NULL
154 #undef TARGET_ASM_INTEGER
155 #define TARGET_ASM_INTEGER mmix_assemble_integer
156
157 #undef TARGET_ASM_FUNCTION_PROLOGUE
158 #define TARGET_ASM_FUNCTION_PROLOGUE mmix_target_asm_function_prologue
159
160 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
161 #define TARGET_ASM_FUNCTION_END_PROLOGUE mmix_target_asm_function_end_prologue
162
163 #undef TARGET_ASM_FUNCTION_EPILOGUE
164 #define TARGET_ASM_FUNCTION_EPILOGUE mmix_target_asm_function_epilogue
165
166 #undef TARGET_ENCODE_SECTION_INFO
167 #define TARGET_ENCODE_SECTION_INFO  mmix_encode_section_info
168 #undef TARGET_STRIP_NAME_ENCODING
169 #define TARGET_STRIP_NAME_ENCODING  mmix_strip_name_encoding
170
171 #undef TARGET_ASM_OUTPUT_MI_THUNK
172 #define TARGET_ASM_OUTPUT_MI_THUNK mmix_asm_output_mi_thunk
173 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
174 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
175
176 #undef TARGET_RTX_COSTS
177 #define TARGET_RTX_COSTS mmix_rtx_costs
178
179 struct gcc_target targetm = TARGET_INITIALIZER;
180
181 /* Functions that are expansions for target macros.
182    See Target Macros in `Using and Porting GCC'.  */
183
184 /* OVERRIDE_OPTIONS.  */
185
186 void
187 mmix_override_options ()
188 {
189   /* Should we err or should we warn?  Hmm.  At least we must neutralize
190      it.  For example the wrong kind of case-tables will be generated with
191      PIC; we use absolute address items for mmixal compatibility.  FIXME:
192      They could be relative if we just elide them to after all pertinent
193      labels.  */
194   if (flag_pic)
195     {
196       warning ("-f%s not supported: ignored", (flag_pic > 1) ? "PIC" : "pic");
197       flag_pic = 0;
198     }
199 }
200
201 /* INIT_EXPANDERS.  */
202
203 void
204 mmix_init_expanders ()
205 {
206   init_machine_status = mmix_init_machine_status;
207 }
208
209 /* Set the per-function data.  */
210
211 static struct machine_function *
212 mmix_init_machine_status ()
213 {
214   return ggc_alloc_cleared (sizeof (struct machine_function));
215 }
216
217 /* DATA_ALIGNMENT.
218    We have trouble getting the address of stuff that is located at other
219    than 32-bit alignments (GETA requirements), so try to give everything
220    at least 32-bit alignment. */
221
222 int
223 mmix_data_alignment (type, basic_align)
224      tree type ATTRIBUTE_UNUSED;
225      int basic_align;
226 {
227   if (basic_align < 32)
228     return 32;
229
230   return basic_align;
231 }
232
233 /* CONSTANT_ALIGNMENT.  */
234
235 int
236 mmix_constant_alignment (constant, basic_align)
237      tree constant ATTRIBUTE_UNUSED;
238      int basic_align;
239 {
240   if (basic_align < 32)
241     return 32;
242
243   return basic_align;
244 }
245
246 /* LOCAL_ALIGNMENT.  */
247
248 int
249 mmix_local_alignment (type, basic_align)
250      tree type ATTRIBUTE_UNUSED;
251      int basic_align;
252 {
253   if (basic_align < 32)
254     return 32;
255
256   return basic_align;
257 }
258
259 /* CONDITIONAL_REGISTER_USAGE.  */
260
261 void
262 mmix_conditional_register_usage ()
263 {
264   int i;
265
266   if (TARGET_ABI_GNU)
267     {
268       static const int gnu_abi_reg_alloc_order[]
269         = MMIX_GNU_ABI_REG_ALLOC_ORDER;
270
271       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
272         reg_alloc_order[i] = gnu_abi_reg_alloc_order[i];
273
274       /* Change the default from the mmixware ABI.  For the GNU ABI,
275          $15..$30 are call-saved just as $0..$14.  There must be one
276          call-clobbered local register for the "hole" that holds the
277          number of saved local registers saved by PUSHJ/PUSHGO during the
278          function call, receiving the return value at return.  So best is
279          to use the highest, $31.  It's already marked call-clobbered for
280          the mmixware ABI.  */
281       for (i = 15; i <= 30; i++)
282         call_used_regs[i] = 0;
283
284       /* "Unfix" the parameter registers.  */
285       for (i = MMIX_RESERVED_GNU_ARG_0_REGNUM;
286            i < MMIX_RESERVED_GNU_ARG_0_REGNUM + MMIX_MAX_ARGS_IN_REGS;
287            i++)
288         fixed_regs[i] = 0;
289     }
290
291   /* Step over the ":" in special register names.  */
292   if (! TARGET_TOPLEVEL_SYMBOLS)
293     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
294       if (reg_names[i][0] == ':')
295         reg_names[i]++;
296 }
297
298 /* LOCAL_REGNO.
299    All registers that are part of the register stack and that will be
300    saved are local.  */
301
302 int
303 mmix_local_regno (regno)
304      int regno;
305 {
306   return regno <= MMIX_LAST_STACK_REGISTER_REGNUM && !call_used_regs[regno];
307 }
308
309 /* PREFERRED_RELOAD_CLASS.
310    We need to extend the reload class of REMAINDER_REG and HIMULT_REG.  */
311
312 enum reg_class
313 mmix_preferred_reload_class (x, class)
314      rtx x ATTRIBUTE_UNUSED;
315      enum reg_class class;
316 {
317   /* FIXME: Revisit.  */
318   return GET_CODE (x) == MOD && GET_MODE (x) == DImode
319     ? REMAINDER_REG : class;
320 }
321
322 /* PREFERRED_OUTPUT_RELOAD_CLASS.
323    We need to extend the reload class of REMAINDER_REG and HIMULT_REG.  */
324
325 enum reg_class
326 mmix_preferred_output_reload_class (x, class)
327      rtx x ATTRIBUTE_UNUSED;
328      enum reg_class class;
329 {
330   /* FIXME: Revisit.  */
331   return GET_CODE (x) == MOD && GET_MODE (x) == DImode
332     ? REMAINDER_REG : class;
333 }
334
335 /* SECONDARY_RELOAD_CLASS.
336    We need to reload regs of REMAINDER_REG and HIMULT_REG elsewhere.  */
337
338 enum reg_class
339 mmix_secondary_reload_class (class, mode, x, in_p)
340      enum reg_class class;
341      enum machine_mode mode ATTRIBUTE_UNUSED;
342      rtx x ATTRIBUTE_UNUSED;
343      int in_p ATTRIBUTE_UNUSED;
344 {
345   if (class == REMAINDER_REG
346       || class == HIMULT_REG
347       || class == SYSTEM_REGS)
348     return GENERAL_REGS;
349
350   return NO_REGS;
351 }
352
353 /* CONST_OK_FOR_LETTER_P.  */
354
355 int
356 mmix_const_ok_for_letter_p (value, c)
357      HOST_WIDE_INT value;
358      int c;
359 {
360   return
361     (c == 'I' ? value >= 0 && value <= 255
362      : c == 'J' ? value >= 0 && value <= 65535
363      : c == 'K' ? value <= 0 && value >= -255
364      : c == 'L' ? mmix_shiftable_wyde_value (value)
365      : c == 'M' ? value == 0
366      : c == 'N' ? mmix_shiftable_wyde_value (~value)
367      : c == 'O' ? (value == 3 || value == 5 || value == 9
368                    || value == 17)
369      : 0);
370 }
371
372 /* CONST_DOUBLE_OK_FOR_LETTER_P.  */
373
374 int
375 mmix_const_double_ok_for_letter_p (value, c)
376      rtx value;
377      int c;
378 {
379   return
380     (c == 'G' ? value == CONST0_RTX (GET_MODE (value))
381      : 0);
382 }
383
384 /* EXTRA_CONSTRAINT.
385    We need this since our constants are not always expressible as
386    CONST_INT:s, but rather often as CONST_DOUBLE:s.  */
387
388 int
389 mmix_extra_constraint (x, c, strict)
390      rtx x;
391      int c;
392      int strict;
393 {
394   HOST_WIDEST_INT value;
395
396   /* When checking for an address, we need to handle strict vs. non-strict
397      register checks.  Don't use address_operand, but instead its
398      equivalent (its callee, which it is just a wrapper for),
399      memory_operand_p and the strict-equivalent strict_memory_address_p.  */
400   if (c == 'U')
401     return
402       strict
403       ? strict_memory_address_p (Pmode, x)
404       : memory_address_p (Pmode, x);
405
406   /* R asks whether x is to be loaded with GETA or something else.  Right
407      now, only a SYMBOL_REF and LABEL_REF can fit for
408      TARGET_BASE_ADDRESSES.
409
410      Only constant symbolic addresses apply.  With TARGET_BASE_ADDRESSES,
411      we just allow straight LABEL_REF or SYMBOL_REFs with SYMBOL_REF_FLAG
412      set right now; only function addresses and code labels.  If we change
413      to let SYMBOL_REF_FLAG be set on other symbols, we have to check
414      inside CONST expressions.  When TARGET_BASE_ADDRESSES is not in
415      effect, a "raw" constant check together with mmix_constant_address_p
416      is all that's needed; we want all constant addresses to be loaded
417      with GETA then.  */
418   if (c == 'R')
419     return
420       GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_DOUBLE
421       && mmix_constant_address_p (x)
422       && (! TARGET_BASE_ADDRESSES
423           || (GET_CODE (x) == LABEL_REF
424               || (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_FLAG (x))));
425
426   if (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode)
427     return 0;
428
429   value = mmix_intval (x);
430
431   /* We used to map Q->J, R->K, S->L, T->N, U->O, but we don't have to any
432      more ('U' taken for address_operand, 'R' similarly).  Some letters map
433      outside of CONST_INT, though; we still use 'S' and 'T'.  */
434   if (c == 'S')
435     return mmix_shiftable_wyde_value (value);
436   else if (c == 'T')
437     return mmix_shiftable_wyde_value (~value);
438   return 0;
439 }
440
441 /* DYNAMIC_CHAIN_ADDRESS.  */
442
443 rtx
444 mmix_dynamic_chain_address (frame)
445      rtx frame;
446 {
447   /* FIXME: the frame-pointer is stored at offset -8 from the current
448      frame-pointer.  Unfortunately, the caller assumes that a
449      frame-pointer is present for *all* previous frames.  There should be
450      a way to say that that cannot be done, like for RETURN_ADDR_RTX.  */
451   return plus_constant (frame, -8);
452 }
453
454 /* STARTING_FRAME_OFFSET.  */
455
456 int
457 mmix_starting_frame_offset ()
458 {
459   /* The old frame pointer is in the slot below the new one, so
460      FIRST_PARM_OFFSET does not need to depend on whether the
461      frame-pointer is needed or not.  We have to adjust for the register
462      stack pointer being located below the saved frame pointer.
463      Similarly, we store the return address on the stack too, for
464      exception handling, and always if we save the register stack pointer.  */
465   return
466     (-8
467      + (MMIX_CFUN_HAS_LANDING_PAD
468         ? -16 : (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS ? -8 : 0)));
469 }
470
471 /* RETURN_ADDR_RTX.  */
472
473 rtx
474 mmix_return_addr_rtx (count, frame)
475      int count;
476      rtx frame ATTRIBUTE_UNUSED;
477 {
478   return count == 0
479     ? (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS
480        /* FIXME: Set frame_alias_set on the following.  (Why?)
481           See mmix_initial_elimination_offset for the reason we can't use
482           get_hard_reg_initial_val for both.  Always using a stack slot
483           and not a register would be suboptimal.  */
484        ? validize_mem (gen_rtx_MEM (Pmode, plus_constant (frame_pointer_rtx, -16)))
485        : get_hard_reg_initial_val (Pmode, MMIX_INCOMING_RETURN_ADDRESS_REGNUM))
486     : NULL_RTX;
487 }
488
489 /* SETUP_FRAME_ADDRESSES.  */
490
491 void
492 mmix_setup_frame_addresses ()
493 {
494   /* Nothing needed at the moment.  */
495 }
496
497 /* The difference between the (imaginary) frame pointer and the stack
498    pointer.  Used to eliminate the frame pointer.  */
499
500 int
501 mmix_initial_elimination_offset (fromreg, toreg)
502      int fromreg;
503      int toreg;
504 {
505   int regno;
506   int fp_sp_offset
507     = (get_frame_size () + current_function_outgoing_args_size + 7) & ~7;
508
509   /* There is no actual offset between these two virtual values, but for
510      the frame-pointer, we have the old one in the stack position below
511      it, so the offset for the frame-pointer to the stack-pointer is one
512      octabyte larger.  */
513   if (fromreg == MMIX_ARG_POINTER_REGNUM
514       && toreg == MMIX_FRAME_POINTER_REGNUM)
515     return 0;
516
517   /* The difference is the size of local variables plus the size of
518      outgoing function arguments that would normally be passed as
519      registers but must be passed on stack because we're out of
520      function-argument registers.  Only global saved registers are
521      counted; the others go on the register stack.
522
523      The frame-pointer is counted too if it is what is eliminated, as we
524      need to balance the offset for it from STARTING_FRAME_OFFSET.
525
526      Also add in the slot for the register stack pointer we save if we
527      have a landing pad.
528
529      Unfortunately, we can't access $0..$14, from unwinder code easily, so
530      store the return address in a frame slot too.  FIXME: Only for
531      non-leaf functions.  FIXME: Always with a landing pad, because it's
532      hard to know whether we need the other at the time we know we need
533      the offset for one (and have to state it).  It's a kludge until we
534      can express the register stack in the EH frame info.
535
536      We have to do alignment here; get_frame_size will not return a
537      multiple of STACK_BOUNDARY.  FIXME: Add note in manual.  */
538
539   for (regno = MMIX_FIRST_GLOBAL_REGNUM;
540        regno <= 255;
541        regno++)
542     if ((regs_ever_live[regno] && ! call_used_regs[regno])
543         || IS_MMIX_EH_RETURN_DATA_REG (regno))
544       fp_sp_offset += 8;
545
546   return fp_sp_offset
547     + (MMIX_CFUN_HAS_LANDING_PAD
548        ? 16 : (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS ? 8 : 0))
549     + (fromreg == MMIX_ARG_POINTER_REGNUM ? 0 : 8);
550 }
551
552 /* Return an rtx for a function argument to go in a register, and 0 for
553    one that must go on stack.  */
554
555 rtx
556 mmix_function_arg (argsp, mode, type, named, incoming)
557      const CUMULATIVE_ARGS * argsp;
558      enum machine_mode mode;
559      tree type;
560      int named ATTRIBUTE_UNUSED;
561      int incoming;
562 {
563   /* Last-argument marker.  */
564   if (type == void_type_node)
565     return (argsp->regs < MMIX_MAX_ARGS_IN_REGS)
566       ? gen_rtx_REG (mode,
567                      (incoming
568                       ? MMIX_FIRST_INCOMING_ARG_REGNUM
569                       : MMIX_FIRST_ARG_REGNUM) + argsp->regs)
570       : NULL_RTX;
571
572   return (argsp->regs < MMIX_MAX_ARGS_IN_REGS
573           && !MUST_PASS_IN_STACK (mode, type)
574           && (GET_MODE_BITSIZE (mode) <= 64
575               || argsp->lib
576               || TARGET_LIBFUNC))
577     ? gen_rtx_REG (mode,
578                    (incoming
579                     ? MMIX_FIRST_INCOMING_ARG_REGNUM
580                     : MMIX_FIRST_ARG_REGNUM)
581                    + argsp->regs)
582     : NULL_RTX;
583 }
584
585 /* Returns nonzero for everything that goes by reference, 0 for
586    everything that goes by value.  */
587
588 int
589 mmix_function_arg_pass_by_reference (argsp, mode, type, named)
590      const CUMULATIVE_ARGS * argsp;
591      enum machine_mode mode;
592      tree type;
593      int named ATTRIBUTE_UNUSED;
594 {
595   /* FIXME: Check: I'm not sure the MUST_PASS_IN_STACK check is
596      necessary.  */
597   return
598     MUST_PASS_IN_STACK (mode, type)
599     || (MMIX_FUNCTION_ARG_SIZE (mode, type) > 8
600         && !TARGET_LIBFUNC
601         && !argsp->lib);
602 }
603
604 /* Return nonzero if regno is a register number where a parameter is
605    passed, and 0 otherwise.  */
606
607 int
608 mmix_function_arg_regno_p (regno, incoming)
609      int regno;
610      int incoming;
611 {
612   int first_arg_regnum
613     = incoming ? MMIX_FIRST_INCOMING_ARG_REGNUM : MMIX_FIRST_ARG_REGNUM;
614
615   return regno >= first_arg_regnum
616     && regno < first_arg_regnum + MMIX_MAX_ARGS_IN_REGS;
617 }
618
619 /* FUNCTION_OUTGOING_VALUE.  */
620
621 rtx
622 mmix_function_outgoing_value (valtype, func)
623      tree valtype;
624      tree func ATTRIBUTE_UNUSED;
625 {
626   enum machine_mode mode = TYPE_MODE (valtype);
627   enum machine_mode cmode;
628   int first_val_regnum = MMIX_OUTGOING_RETURN_VALUE_REGNUM;
629   rtx vec[MMIX_MAX_REGS_FOR_VALUE];
630   int i;
631   int nregs;
632
633   /* Return values that fit in a register need no special handling.
634      There's no register hole when parameters are passed in global
635      registers.  */
636   if (TARGET_ABI_GNU
637       || GET_MODE_BITSIZE (mode) <= BITS_PER_WORD)
638     return
639       gen_rtx_REG (mode, MMIX_OUTGOING_RETURN_VALUE_REGNUM);
640
641   /* A complex type, made up of components.  */
642   cmode = TYPE_MODE (TREE_TYPE (valtype));
643   nregs = ((GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD);
644
645   /* We need to take care of the effect of the register hole on return
646      values of large sizes; the last register will appear as the first
647      register, with the rest shifted.  (For complex modes, this is just
648      swapped registers.)  */
649
650   if (nregs > MMIX_MAX_REGS_FOR_VALUE)
651     internal_error ("too large function value type, needs %d registers,\
652  have only %d registers for this", nregs, MMIX_MAX_REGS_FOR_VALUE);
653
654   /* FIXME: Maybe we should handle structure values like this too
655      (adjusted for BLKmode), perhaps for both ABI:s.  */
656   for (i = 0; i < nregs - 1; i++)
657     vec[i]
658       = gen_rtx_EXPR_LIST (VOIDmode,
659                            gen_rtx_REG (cmode, first_val_regnum + i),
660                            GEN_INT ((i + 1) * BITS_PER_UNIT));
661
662   vec[nregs - 1]
663     = gen_rtx_EXPR_LIST (VOIDmode,
664                          gen_rtx_REG (cmode, first_val_regnum + nregs - 1),
665                          GEN_INT (0));
666
667   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nregs, vec));
668 }
669
670 /* FUNCTION_VALUE_REGNO_P.  */
671
672 int
673 mmix_function_value_regno_p (regno)
674      int regno;
675 {
676   return regno == MMIX_RETURN_VALUE_REGNUM;
677 }
678
679 /* EH_RETURN_DATA_REGNO. */
680
681 int
682 mmix_eh_return_data_regno (n)
683      int n ATTRIBUTE_UNUSED;
684 {
685   if (n >= 0 && n < 4)
686     return MMIX_EH_RETURN_DATA_REGNO_START + n;
687
688   return INVALID_REGNUM;
689 }
690
691 /* EH_RETURN_STACKADJ_RTX. */
692
693 rtx
694 mmix_eh_return_stackadj_rtx ()
695 {
696   return gen_rtx_REG (Pmode, MMIX_EH_RETURN_STACKADJ_REGNUM);
697 }
698
699 /* EH_RETURN_HANDLER_RTX.  */
700
701 rtx
702 mmix_eh_return_handler_rtx ()
703 {
704   return
705     gen_rtx_REG (Pmode, MMIX_INCOMING_RETURN_ADDRESS_REGNUM);
706 }
707
708 /* ASM_PREFERRED_EH_DATA_FORMAT. */
709
710 int
711 mmix_asm_preferred_eh_data_format (code, global)
712      int code ATTRIBUTE_UNUSED;
713      int global ATTRIBUTE_UNUSED;
714 {
715   /* This is the default (was at 2001-07-20).  Revisit when needed.  */
716   return DW_EH_PE_absptr;
717 }
718
719 /* Make a note that we've seen the beginning of the prologue.  This
720    matters to whether we'll translate register numbers as calculated by
721    mmix_machine_dependent_reorg.  */
722
723 static void
724 mmix_target_asm_function_prologue (stream, framesize)
725      FILE *stream ATTRIBUTE_UNUSED;
726      HOST_WIDE_INT framesize ATTRIBUTE_UNUSED;
727 {
728   cfun->machine->in_prologue = 1;
729 }
730
731 /* Make a note that we've seen the end of the prologue.  */
732
733 static void
734 mmix_target_asm_function_end_prologue (stream)
735      FILE *stream ATTRIBUTE_UNUSED;
736 {
737   cfun->machine->in_prologue = 0;
738 }
739
740 /* MACHINE_DEPENDENT_REORG.
741    No actual rearrangements done here; just virtually by calculating the
742    highest saved stack register number used to modify the register numbers
743    at output time.  */
744
745 void
746 mmix_machine_dependent_reorg (first)
747      rtx first ATTRIBUTE_UNUSED;
748 {
749   int regno;
750
751   /* We put the number of the highest saved register-file register in a
752      location convenient for the call-patterns to output.  Note that we
753      don't tell dwarf2 about these registers, since it can't restore them
754      anyway.  */
755   for (regno = MMIX_LAST_STACK_REGISTER_REGNUM;
756        regno >= 0;
757        regno--)
758     if ((regs_ever_live[regno] && !call_used_regs[regno])
759         || (regno == MMIX_FRAME_POINTER_REGNUM && frame_pointer_needed))
760       break;
761
762   /* Regardless of whether they're saved (they might be just read), we
763      mustn't include registers that carry parameters.  We could scan the
764      insns to see whether they're actually used (and indeed do other less
765      trivial register usage analysis and transformations), but it seems
766      wasteful to optimize for unused parameter registers.  As of
767      2002-04-30, regs_ever_live[n] seems to be set for only-reads too, but
768      that might change.  */
769   if (!TARGET_ABI_GNU && regno < current_function_args_info.regs - 1)
770     {
771       regno = current_function_args_info.regs - 1;
772
773       /* We don't want to let this cause us to go over the limit and make
774          incoming parameter registers be misnumbered and treating the last
775          parameter register and incoming return value register call-saved.
776          Stop things at the unmodified scheme.  */
777       if (regno > MMIX_RETURN_VALUE_REGNUM - 1)
778         regno = MMIX_RETURN_VALUE_REGNUM - 1;
779     }
780
781   cfun->machine->highest_saved_stack_register = regno;
782 }
783
784 /* TARGET_ASM_FUNCTION_EPILOGUE.  */
785
786 static void
787 mmix_target_asm_function_epilogue (stream, locals_size)
788      FILE *stream;
789      HOST_WIDE_INT locals_size ATTRIBUTE_UNUSED;
790
791 {
792   /* Emit an \n for readability of the generated assembly.  */
793   fputc ('\n', stream);
794 }
795
796 /* TARGET_ASM_OUTPUT_MI_THUNK.  */
797
798 static void
799 mmix_asm_output_mi_thunk (stream, fndecl, delta, vcall_offset, func)
800      FILE * stream;
801      tree fndecl ATTRIBUTE_UNUSED;
802      HOST_WIDE_INT delta;
803      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED;
804      tree func;
805 {
806   /* If you define STRUCT_VALUE to 0, rather than use STRUCT_VALUE_REGNUM,
807      (i.e. pass location of structure to return as invisible first
808      argument) you need to tweak this code too.  */
809   const char *regname = reg_names[MMIX_FIRST_INCOMING_ARG_REGNUM];
810
811   if (delta >= 0 && delta < 65536)
812     fprintf (stream, "\tINCL %s,%d\n", regname, (int)delta);
813   else if (delta < 0 && delta >= -255)
814     fprintf (stream, "\tSUBU %s,%s,%d\n", regname, regname, (int)-delta);
815   else
816     {
817       mmix_output_register_setting (stream, 255, delta, 1);
818       fprintf (stream, "\tADDU %s,%s,$255\n", regname, regname);
819     }
820
821   fprintf (stream, "\tJMP ");
822   assemble_name (stream, XSTR (XEXP (DECL_RTL (func), 0), 0));
823   fprintf (stream, "\n");
824 }
825
826 /* FUNCTION_PROFILER.  */
827
828 void
829 mmix_function_profiler (stream, labelno)
830      FILE *stream ATTRIBUTE_UNUSED;
831      int labelno ATTRIBUTE_UNUSED;
832 {
833   sorry ("function_profiler support for MMIX");
834 }
835
836 /* SETUP_INCOMING_VARARGS.  */
837
838 void
839 mmix_setup_incoming_varargs (args_so_farp, mode, vartype, pretend_sizep,
840                              second_time)
841      CUMULATIVE_ARGS * args_so_farp;
842      enum machine_mode mode;
843      tree vartype;
844      int * pretend_sizep;
845      int second_time ATTRIBUTE_UNUSED;
846 {
847   /* The last named variable has been handled, but
848      args_so_farp has not been advanced for it.  */
849   if (args_so_farp->regs + 1 < MMIX_MAX_ARGS_IN_REGS)
850     *pretend_sizep = (MMIX_MAX_ARGS_IN_REGS - (args_so_farp->regs + 1)) * 8;
851
852   /* We assume that one argument takes up one register here.  That should
853      be true until we start messing with multi-reg parameters.   */
854   if ((7 + (MMIX_FUNCTION_ARG_SIZE (mode, vartype))) / 8 != 1)
855     internal_error ("MMIX Internal: Last named vararg would not fit in a register");
856 }
857
858 /* EXPAND_BUILTIN_VA_ARG.  */
859
860 /* This is modified from the "standard" implementation of va_arg: read the
861    value from the current (padded) address and increment by the (padded)
862    size.  The difference for MMIX is that if the type is
863    pass-by-reference, then perform an indirection.  */
864
865 rtx
866 mmix_expand_builtin_va_arg (valist, type)
867      tree valist;
868      tree type;
869 {
870   tree ptr_size = size_int (BITS_PER_WORD / BITS_PER_UNIT);
871   tree addr_tree, type_size = NULL;
872   tree align, alignm1;
873   tree rounded_size;
874   rtx addr;
875
876   /* Compute the rounded size of the type.  */
877
878   /* Get AP.  */
879   addr_tree = valist;
880   align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
881   alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
882   if (type == error_mark_node
883       || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
884       || TREE_OVERFLOW (type_size))
885     /* Presumably an error; the size isn't computable.  A message has
886        supposedly been emitted elsewhere.  */
887     rounded_size = size_zero_node;
888   else
889     rounded_size = fold (build (MULT_EXPR, sizetype,
890                                 fold (build (TRUNC_DIV_EXPR, sizetype,
891                                              fold (build (PLUS_EXPR, sizetype,
892                                                           type_size, alignm1)),
893                                              align)),
894                                 align));
895
896  if (AGGREGATE_TYPE_P (type)
897      && GET_MODE_UNIT_SIZE (TYPE_MODE (type)) < 8
898      && GET_MODE_UNIT_SIZE (TYPE_MODE (type)) != 0)
899    {
900      /* Adjust for big-endian the location of aggregates passed in a
901         register, but where the aggregate is accessed in a shorter mode
902         than the natural register mode (i.e. it is accessed as SFmode(?),
903         SImode, HImode or QImode rather than DImode or DFmode(?)).  FIXME:
904         Or should we adjust the mode in which the aggregate is read, to be
905         a register size mode?  (Hum, nah, a small offset is generally
906         cheaper than a wider memory access on MMIX.)  */
907      addr_tree
908        = build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
909                 size_int ((BITS_PER_WORD / BITS_PER_UNIT)
910                           - GET_MODE_UNIT_SIZE (TYPE_MODE (type))));
911    }
912  else if (!integer_zerop (rounded_size))
913    {
914      if (!really_constant_p (type_size))
915        /* Varying-size types come in by reference.  */
916        addr_tree
917          = build1 (INDIRECT_REF, build_pointer_type (type), addr_tree);
918      else
919        {
920          /* If the size is less than a register, then we need to pad the
921             address by adding the difference.  */
922          tree addend
923            = fold (build (COND_EXPR, sizetype,
924                           fold (build (GT_EXPR, sizetype,
925                                        rounded_size,
926                                        align)),
927                           size_zero_node,
928                           fold (build (MINUS_EXPR, sizetype,
929                                        rounded_size,
930                                        type_size))));
931          tree addr_tree1
932            = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
933                           addend));
934
935          /* If this type is larger than what fits in a register, then it
936             is passed by reference.  */
937          addr_tree
938            = fold (build (COND_EXPR, TREE_TYPE (addr_tree1),
939                           fold (build (GT_EXPR, sizetype,
940                                        rounded_size,
941                                        ptr_size)),
942                           build1 (INDIRECT_REF, build_pointer_type (type),
943                                   addr_tree1),
944                           addr_tree1));
945        }
946    }
947
948   addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
949   addr = copy_to_reg (addr);
950
951   if (!integer_zerop (rounded_size))
952     {
953       /* Compute new value for AP.  For MMIX, it is always advanced by the
954          size of a register.  */
955       tree t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
956                       build (PLUS_EXPR, TREE_TYPE (valist), valist,
957                              ptr_size));
958       TREE_SIDE_EFFECTS (t) = 1;
959       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
960     }
961
962   return addr;
963 }
964
965 /* TRAMPOLINE_SIZE.  */
966 /* Four 4-byte insns plus two 8-byte values.  */
967 int mmix_trampoline_size = 32;
968
969
970 /* TRAMPOLINE_TEMPLATE.  */
971
972 void
973 mmix_trampoline_template (stream)
974      FILE * stream;
975 {
976   /* Read a value into the static-chain register and jump somewhere.  The
977      static chain is stored at offset 16, and the function address is
978      stored at offset 24.  */
979   /* FIXME: GCC copies this using *intsize* (tetra), when it should use
980      register size (octa).  */
981   fprintf (stream, "\tGETA $255,1F\n\t");
982   fprintf (stream, "LDOU %s,$255,0\n\t",
983            reg_names[MMIX_STATIC_CHAIN_REGNUM]);
984   fprintf (stream, "LDOU $255,$255,8\n\t");
985   fprintf (stream, "GO $255,$255,0\n");
986   fprintf (stream, "1H\tOCTA 0\n\t");
987   fprintf (stream, "OCTA 0\n");
988 }
989
990 /* INITIALIZE_TRAMPOLINE.  */
991 /* Set the static chain and function pointer field in the trampoline.
992    We also SYNCID here to be sure (doesn't matter in the simulator, but
993    some day it will).  */
994
995 void
996 mmix_initialize_trampoline (trampaddr, fnaddr, static_chain)
997      rtx trampaddr;
998      rtx fnaddr;
999      rtx static_chain;
1000 {
1001   emit_move_insn (gen_rtx_MEM (DImode, plus_constant (trampaddr, 16)),
1002                   static_chain);
1003   emit_move_insn (gen_rtx_MEM (DImode,
1004                                plus_constant (trampaddr, 24)),
1005                   fnaddr);
1006   emit_insn (gen_sync_icache (validize_mem (gen_rtx_MEM (DImode,
1007                                                          trampaddr)),
1008                               GEN_INT (mmix_trampoline_size - 1)));
1009 }
1010
1011 /* We must exclude constant addresses that have an increment that is not a
1012    multiple of four bytes because of restrictions of the GETA
1013    instruction, unless TARGET_BASE_ADDRESSES.  */
1014
1015 int
1016 mmix_constant_address_p (x)
1017      rtx x;
1018 {
1019   RTX_CODE code = GET_CODE (x);
1020   int addend = 0;
1021   /* When using "base addresses", anything constant goes.  */
1022   int constant_ok = TARGET_BASE_ADDRESSES != 0;
1023
1024   switch (code)
1025     {
1026     case LABEL_REF:
1027     case SYMBOL_REF:
1028       return 1;
1029
1030     case CONSTANT_P_RTX:
1031     case HIGH:
1032       /* FIXME: Don't know how to dissect these.  Avoid them for now,
1033          except we know they're constants.  */
1034       return constant_ok;
1035
1036     case CONST_INT:
1037       addend = INTVAL (x);
1038       break;
1039
1040     case CONST_DOUBLE:
1041       if (GET_MODE (x) != VOIDmode)
1042         /* Strange that we got here.  FIXME: Check if we do.  */
1043         return constant_ok;
1044       addend = CONST_DOUBLE_LOW (x);
1045       break;
1046
1047     case CONST:
1048       /* Note that expressions with arithmetic on forward references don't
1049          work in mmixal.  People using gcc assembly code with mmixal might
1050          need to move arrays and such to before the point of use.  */
1051       if (GET_CODE (XEXP (x, 0)) == PLUS)
1052         {
1053           rtx x0 = XEXP (XEXP (x, 0), 0);
1054           rtx x1 = XEXP (XEXP (x, 0), 1);
1055
1056           if ((GET_CODE (x0) == SYMBOL_REF
1057                || GET_CODE (x0) == LABEL_REF)
1058               && (GET_CODE (x1) == CONST_INT
1059                   || (GET_CODE (x1) == CONST_DOUBLE
1060                       && GET_MODE (x1) == VOIDmode)))
1061             addend = mmix_intval (x1);
1062           else
1063             return constant_ok;
1064         }
1065       else
1066         return constant_ok;
1067       break;
1068
1069     default:
1070       return 0;
1071     }
1072
1073   return constant_ok || (addend & 3) == 0;
1074 }
1075
1076 /* Return 1 if the address is OK, otherwise 0.
1077    Used by GO_IF_LEGITIMATE_ADDRESS.  */
1078
1079 int
1080 mmix_legitimate_address (mode, x, strict_checking)
1081      enum machine_mode mode ATTRIBUTE_UNUSED;
1082      rtx x;
1083      int strict_checking;
1084 {
1085 #define MMIX_REG_OK(X)                                                  \
1086   ((strict_checking                                                     \
1087     && (REGNO (X) <= MMIX_LAST_GENERAL_REGISTER                         \
1088         || (reg_renumber[REGNO (X)] > 0                                 \
1089             && reg_renumber[REGNO (X)] <= MMIX_LAST_GENERAL_REGISTER))) \
1090    || (!strict_checking                                                 \
1091        && (REGNO (X) <= MMIX_LAST_GENERAL_REGISTER                      \
1092            || REGNO (X) >= FIRST_PSEUDO_REGISTER                        \
1093            || REGNO (X) == ARG_POINTER_REGNUM)))
1094
1095   /* We only accept:
1096      (mem reg)
1097      (mem (plus reg reg))
1098      (mem (plus reg 0..255)).
1099      unless TARGET_BASE_ADDRESSES, in which case we accept all
1100      (mem constant_address) too.  */
1101
1102
1103     /* (mem reg) */
1104   if (REG_P (x) && MMIX_REG_OK (x))
1105     return 1;
1106
1107   if (GET_CODE(x) == PLUS)
1108     {
1109       rtx x1 = XEXP (x, 0);
1110       rtx x2 = XEXP (x, 1);
1111
1112       /* Try swapping the order.  FIXME: Do we need this?  */
1113       if (! REG_P (x1))
1114         {
1115           rtx tem = x1;
1116           x1 = x2;
1117           x2 = tem;
1118         }
1119
1120       /* (mem (plus (reg?) (?))) */
1121       if (!REG_P (x1) || !MMIX_REG_OK (x1))
1122         return TARGET_BASE_ADDRESSES && mmix_constant_address_p (x);
1123
1124       /* (mem (plus (reg) (reg?))) */
1125       if (REG_P (x2) && MMIX_REG_OK (x2))
1126         return 1;
1127
1128       /* (mem (plus (reg) (0..255?))) */
1129       if (GET_CODE (x2) == CONST_INT
1130           && CONST_OK_FOR_LETTER_P (INTVAL (x2), 'I'))
1131         return 1;
1132
1133       return 0;
1134     }
1135
1136   return TARGET_BASE_ADDRESSES && mmix_constant_address_p (x);
1137 }
1138
1139 /* LEGITIMATE_CONSTANT_P.  */
1140
1141 int
1142 mmix_legitimate_constant_p (x)
1143      rtx x;
1144 {
1145   RTX_CODE code = GET_CODE (x);
1146
1147   /* We must allow any number due to the way the cse passes works; if we
1148      do not allow any number here, general_operand will fail, and insns
1149      will fatally fail recognition instead of "softly".  */
1150   if (code == CONST_INT || code == CONST_DOUBLE)
1151     return 1;
1152
1153   return CONSTANT_ADDRESS_P (x);
1154 }
1155
1156 /* SELECT_CC_MODE.  */
1157
1158 enum machine_mode
1159 mmix_select_cc_mode (op, x, y)
1160      RTX_CODE op;
1161      rtx x;
1162      rtx y ATTRIBUTE_UNUSED;
1163 {
1164   /* We use CCmode, CC_UNSmode, CC_FPmode, CC_FPEQmode and CC_FUNmode to
1165      output different compare insns.  Note that we do not check the
1166      validity of the comparison here.  */
1167
1168   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1169     {
1170       if (op == ORDERED || op == UNORDERED || op == UNGE
1171           || op == UNGT || op == UNLE || op == UNLT)
1172         return CC_FUNmode;
1173
1174       if (op == EQ || op == NE)
1175         return CC_FPEQmode;
1176
1177       return CC_FPmode;
1178     }
1179
1180   if (op == GTU || op == LTU || op == GEU || op == LEU)
1181     return CC_UNSmode;
1182
1183   return CCmode;
1184 }
1185
1186 /* REVERSIBLE_CC_MODE.  */
1187
1188 int
1189 mmix_reversible_cc_mode (mode)
1190      enum machine_mode mode;
1191 {
1192   /* That is, all integer and the EQ, NE, ORDERED and UNORDERED float
1193      compares.  */
1194   return mode != CC_FPmode;
1195 }
1196
1197 /* TARGET_RTX_COSTS.  */
1198
1199 static bool
1200 mmix_rtx_costs (x, code, outer_code, total)
1201      rtx x ATTRIBUTE_UNUSED;
1202      int code ATTRIBUTE_UNUSED;
1203      int outer_code ATTRIBUTE_UNUSED;
1204      int *total ATTRIBUTE_UNUSED;
1205 {
1206   /* For the time being, this is just a stub and we'll accept the
1207      generic calculations, until we can do measurements, at least.
1208      Say we did not modify any calculated costs.  */
1209   return false;
1210 }
1211
1212 /* ADDRESS_COST.  */
1213
1214 int
1215 mmix_address_cost (addr)
1216      rtx addr ATTRIBUTE_UNUSED;
1217 {
1218   /* There's no difference in the address costs and we have lots of
1219      registers.  Some targets use constant 0, many others use 1 to say
1220      this.  Let's start with 1.  */
1221   return 1;
1222 }
1223
1224 /* REGISTER_MOVE_COST.  */
1225
1226 int
1227 mmix_register_move_cost (mode, from, to)
1228      enum machine_mode mode ATTRIBUTE_UNUSED;
1229      enum reg_class from;
1230      enum reg_class to;
1231 {
1232   return (from == GENERAL_REGS && from == to) ? 2 : 3;
1233 }
1234
1235 /* Note that we don't have a TEXT_SECTION_ASM_OP, because it has to be a
1236    compile-time constant; it's used in an asm in crtstuff.c, compiled for
1237    the target.  */
1238
1239 /* DATA_SECTION_ASM_OP.  */
1240
1241 const char *
1242 mmix_data_section_asm_op ()
1243 {
1244   return "\t.data ! mmixal:= 8H LOC 9B";
1245 }
1246
1247 static void
1248 mmix_encode_section_info (decl, first)
1249      tree decl;
1250      int first;
1251 {
1252   /* Test for an external declaration, and do nothing if it is one.  */
1253   if ((TREE_CODE (decl) == VAR_DECL
1254        && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)))
1255       || (TREE_CODE (decl) == FUNCTION_DECL && TREE_PUBLIC (decl)))
1256     ;
1257   else if (first && DECL_P (decl))
1258     {
1259       /* For non-visible declarations, add a "@" prefix, which we skip
1260          when the label is output.  If the label does not have this
1261          prefix, a ":" is output if -mtoplevel-symbols.
1262
1263          Note that this does not work for data that is declared extern and
1264          later defined as static.  If there's code in between, that code
1265          will refer to the extern declaration, and vice versa.  This just
1266          means that when -mtoplevel-symbols is in use, we can just handle
1267          well-behaved ISO-compliant code.  */
1268
1269       const char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1270       int len = strlen (str);
1271       char *newstr;
1272
1273       /* Why is the return type of ggc_alloc_string const?  */
1274       newstr = (char *) ggc_alloc_string ("", len + 1);
1275
1276       strcpy (newstr + 1, str);
1277       *newstr = '@';
1278       XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
1279     }
1280
1281   /* Set SYMBOL_REF_FLAG for things that we want to access with GETA.  We
1282      may need different options to reach for different things with GETA.
1283      For now, functions and things we know or have been told are constant.  */
1284   if (TREE_CODE (decl) == FUNCTION_DECL
1285       || TREE_CONSTANT (decl)
1286       || (TREE_CODE (decl) == VAR_DECL
1287           && TREE_READONLY (decl)
1288           && !TREE_SIDE_EFFECTS (decl)
1289           && (!DECL_INITIAL (decl)
1290               || TREE_CONSTANT (DECL_INITIAL (decl)))))
1291     {
1292       rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
1293                  ? TREE_CST_RTL (decl) : DECL_RTL (decl));
1294       SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
1295     }
1296 }
1297
1298 static const char *
1299 mmix_strip_name_encoding (name)
1300      const char *name;
1301 {
1302   for (; (*name == '@' || *name == '*'); name++)
1303     ;
1304
1305   return name;
1306 }
1307
1308 /* ASM_FILE_START.  */
1309
1310 void
1311 mmix_asm_file_start (stream)
1312      FILE * stream;
1313 {
1314   /* We just emit a little comment for the time being.  FIXME: Perhaps add
1315      -mstandalone and some segment and prefix setup here.  */
1316   ASM_OUTPUT_SOURCE_FILENAME (stream, main_input_filename);
1317
1318   fprintf (stream, "! mmixal:= 8H LOC Data_Section\n");
1319
1320   /* Make sure each file starts with the text section. */
1321   text_section ();
1322 }
1323
1324 /* ASM_FILE_END.  */
1325
1326 void
1327 mmix_asm_file_end (stream)
1328      FILE * stream ATTRIBUTE_UNUSED;
1329 {
1330   /* Make sure each file ends with the data section. */
1331   data_section ();
1332 }
1333
1334 /* ASM_OUTPUT_SOURCE_FILENAME.  */
1335
1336 void
1337 mmix_asm_output_source_filename (stream, name)
1338      FILE * stream;
1339      const char * name;
1340 {
1341   fprintf (stream, "# 1 ");
1342   OUTPUT_QUOTED_STRING (stream, name);
1343   fprintf (stream, "\n");
1344 }
1345
1346 /* OUTPUT_QUOTED_STRING.  */
1347
1348 void
1349 mmix_output_quoted_string (stream, string, length)
1350      FILE * stream;
1351      const char * string;
1352      int length;
1353 {
1354   const char * string_end = string + length;
1355   static const char *const unwanted_chars = "\"[]\\";
1356
1357   /* Output "any character except newline and double quote character".  We
1358      play it safe and avoid all control characters too.  We also do not
1359      want [] as characters, should input be passed through m4 with [] as
1360      quotes.  Further, we avoid "\", because the GAS port handles it as a
1361      quoting character.  */
1362   while (string < string_end)
1363     {
1364       if (*string
1365           && (unsigned char) *string < 128
1366           && !ISCNTRL (*string)
1367           && strchr (unwanted_chars, *string) == NULL)
1368         {
1369           fputc ('"', stream);
1370           while (*string
1371                  && (unsigned char) *string < 128
1372                  && !ISCNTRL (*string)
1373                  && strchr (unwanted_chars, *string) == NULL
1374                  && string < string_end)
1375             {
1376               fputc (*string, stream);
1377               string++;
1378             }
1379           fputc ('"', stream);
1380           if (string < string_end)
1381             fprintf (stream, ",");
1382         }
1383       if (string < string_end)
1384         {
1385           fprintf (stream, "#%x", *string & 255);
1386           string++;
1387           if (string < string_end)
1388             fprintf (stream, ",");
1389         }
1390     }
1391 }
1392
1393 /* ASM_OUTPUT_SOURCE_LINE.  */
1394
1395 void
1396 mmix_asm_output_source_line  (stream, lineno)
1397      FILE * stream;
1398      int lineno;
1399 {
1400   fprintf (stream, "# %d ", lineno);
1401   OUTPUT_QUOTED_STRING (stream, main_input_filename);
1402   fprintf (stream, "\n");
1403 }
1404
1405 /* Target hook for assembling integer objects.  Use mmix_print_operand
1406    for WYDE and TETRA.  Use mmix_output_octa to output 8-byte
1407    CONST_DOUBLEs.  */
1408
1409 static bool
1410 mmix_assemble_integer (x, size, aligned_p)
1411      rtx x;
1412      unsigned int size;
1413      int aligned_p;
1414 {
1415   if (aligned_p)
1416     switch (size)
1417       {
1418         /* We handle a limited number of types of operands in here.  But
1419            that's ok, because we can punt to generic functions.  We then
1420            pretend that aligned data isn't needed, so the usual .<pseudo>
1421            syntax is used (which works for aligned data too).  We actually
1422            *must* do that, since we say we don't have simple aligned
1423            pseudos, causing this function to be called.  We just try and
1424            keep as much compatibility as possible with mmixal syntax for
1425            normal cases (i.e. without GNU extensions and C only).  */
1426       case 1:
1427         if (GET_CODE (x) != CONST_INT)
1428           {
1429             aligned_p = 0;
1430             break;
1431           }
1432         fputs ("\tBYTE\t", asm_out_file);
1433         mmix_print_operand (asm_out_file, x, 'B');
1434         fputc ('\n', asm_out_file);
1435         return true;
1436
1437       case 2:
1438         if (GET_CODE (x) != CONST_INT)
1439           {
1440             aligned_p = 0;
1441             break;
1442           }
1443         fputs ("\tWYDE\t", asm_out_file);
1444         mmix_print_operand (asm_out_file, x, 'W');
1445         fputc ('\n', asm_out_file);
1446         return true;
1447
1448       case 4:
1449         if (GET_CODE (x) != CONST_INT)
1450           {
1451             aligned_p = 0;
1452             break;
1453           }
1454         fputs ("\tTETRA\t", asm_out_file);
1455         mmix_print_operand (asm_out_file, x, 'L');
1456         fputc ('\n', asm_out_file);
1457         return true;
1458
1459       case 8:
1460         if (GET_CODE (x) == CONST_DOUBLE)
1461           /* We don't get here anymore for CONST_DOUBLE, because DImode
1462              isn't expressed as CONST_DOUBLE, and DFmode is handled
1463              elsewhere.  */
1464           abort ();
1465         assemble_integer_with_op ("\tOCTA\t", x);
1466         return true;
1467       }
1468   return default_assemble_integer (x, size, aligned_p);
1469 }
1470
1471 /* ASM_OUTPUT_ASCII.  */
1472
1473 void
1474 mmix_asm_output_ascii (stream, string, length)
1475      FILE *stream;
1476      const char *string;
1477      int length;
1478 {
1479   while (length > 0)
1480     {
1481       int chunk_size = length > 60 ? 60 : length;
1482       fprintf (stream, "\tBYTE ");
1483       mmix_output_quoted_string (stream, string, chunk_size);
1484       string += chunk_size;
1485       length -= chunk_size;
1486       fprintf (stream, "\n");
1487     }
1488 }
1489
1490 /* ASM_OUTPUT_ALIGNED_COMMON.  */
1491
1492 void
1493 mmix_asm_output_aligned_common (stream, name, size, align)
1494      FILE *stream;
1495      const char *name;
1496      int size;
1497      int align;
1498 {
1499   /* This is mostly the elfos.h one.  There doesn't seem to be a way to
1500      express this in a mmixal-compatible way.  */
1501   fprintf (stream, "\t.comm\t");
1502   assemble_name (stream, name);
1503   fprintf (stream, ",%u,%u ! mmixal-incompatible COMMON\n",
1504            size, align / BITS_PER_UNIT);
1505 }
1506
1507 /* ASM_OUTPUT_ALIGNED_LOCAL.  */
1508
1509 void
1510 mmix_asm_output_aligned_local (stream, name, size, align)
1511      FILE * stream;
1512      const char * name;
1513      int size;
1514      int align;
1515 {
1516   data_section ();
1517
1518   ASM_OUTPUT_ALIGN (stream, exact_log2 (align/BITS_PER_UNIT));
1519   assemble_name (stream, name);
1520   fprintf (stream, "\tLOC @+%d\n", size);
1521 }
1522
1523 /* ASM_OUTPUT_LABEL.  */
1524
1525 void
1526 mmix_asm_output_label (stream, name)
1527      FILE *stream;
1528      const char * name;
1529 {
1530   assemble_name (stream, name);
1531   fprintf (stream, "\tIS @\n");
1532 }
1533
1534 /* ASM_DECLARE_REGISTER_GLOBAL.  */
1535
1536 void
1537 mmix_asm_declare_register_global (stream, decl, regno, name)
1538      FILE *stream ATTRIBUTE_UNUSED;
1539      tree decl ATTRIBUTE_UNUSED;
1540      int regno ATTRIBUTE_UNUSED;
1541      const char *name ATTRIBUTE_UNUSED;
1542 {
1543   /* Nothing to do here, but there *will* be, therefore the framework is
1544      here.  */
1545 }
1546
1547 /* ASM_WEAKEN_LABEL.  */
1548
1549 void
1550 mmix_asm_weaken_label (stream, name)
1551      FILE * stream ATTRIBUTE_UNUSED;
1552      const char * name ATTRIBUTE_UNUSED;
1553 {
1554   fprintf (stream, "\t.weak ");
1555   assemble_name (stream, name);
1556   fprintf (stream, " ! mmixal-incompatible\n");
1557 }
1558
1559 /* MAKE_DECL_ONE_ONLY.  */
1560
1561 void
1562 mmix_make_decl_one_only (decl)
1563      tree decl;
1564 {
1565   DECL_WEAK (decl) = 1;
1566 }
1567
1568 /* ASM_OUTPUT_LABELREF.
1569    Strip GCC's '*' and our own '@'.  No order is assumed.  */
1570
1571 void
1572 mmix_asm_output_labelref (stream, name)
1573      FILE *stream;
1574      const char *name;
1575 {
1576   int is_extern = 1;
1577
1578   for (; (*name == '@' || *name == '*'); name++)
1579     if (*name == '@')
1580       is_extern = 0;
1581
1582   asm_fprintf (stream, "%s%U%s",
1583                is_extern && TARGET_TOPLEVEL_SYMBOLS ? ":" : "",
1584                name);
1585 }
1586
1587 /* ASM_OUTPUT_DEF.  */
1588
1589 void
1590 mmix_asm_output_def (stream, name, value)
1591      FILE * stream;
1592      const char * name;
1593      const char * value;
1594 {
1595   assemble_name (stream, name);
1596   fprintf (stream, "\tIS ");
1597   assemble_name (stream, value);
1598   fputc ('\n', stream);
1599 }
1600
1601 /* PRINT_OPERAND.  */
1602
1603 void
1604 mmix_print_operand (stream, x, code)
1605      FILE * stream;
1606      rtx x;
1607      int code;
1608 {
1609   /* When we add support for different codes later, we can, when needed,
1610      drop through to the main handler with a modified operand.  */
1611   rtx modified_x = x;
1612   int regno = x != NULL_RTX && REG_P (x) ? REGNO (x) : 0;
1613
1614   switch (code)
1615     {
1616       /* Unrelated codes are in alphabetic order.  */
1617
1618     case '+':
1619       /* For conditional branches, output "P" for a probable branch.  */
1620       if (TARGET_BRANCH_PREDICT)
1621         {
1622           x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
1623           if (x && INTVAL (XEXP (x, 0)) > REG_BR_PROB_BASE / 2)
1624             putc ('P', stream);
1625         }
1626       return;
1627
1628     case '.':
1629       /* For the %d in POP %d,0.  */
1630       fprintf (stream, "%d", MMIX_POP_ARGUMENT ());
1631       return;
1632
1633     case 'B':
1634       if (GET_CODE (x) != CONST_INT)
1635         fatal_insn ("MMIX Internal: Expected a CONST_INT, not this", x);
1636       fprintf (stream, "%d", (int) (INTVAL (x) & 0xff));
1637       return;
1638
1639     case 'H':
1640       /* Highpart.  Must be general register, and not the last one, as
1641          that one cannot be part of a consecutive register pair.  */
1642       if (regno > MMIX_LAST_GENERAL_REGISTER - 1)
1643         internal_error ("MMIX Internal: Bad register: %d", regno);
1644
1645       /* This is big-endian, so the high-part is the first one.  */
1646       fprintf (stream, "%s", reg_names[MMIX_OUTPUT_REGNO (regno)]);
1647       return;
1648
1649     case 'L':
1650       /* Lowpart.  Must be CONST_INT or general register, and not the last
1651          one, as that one cannot be part of a consecutive register pair.  */
1652       if (GET_CODE (x) == CONST_INT)
1653         {
1654           fprintf (stream, "#%lx",
1655                    (unsigned long) (INTVAL (x)
1656                                     & ((unsigned int) 0x7fffffff * 2 + 1)));
1657           return;
1658         }
1659
1660       if (GET_CODE (x) == SYMBOL_REF)
1661         {
1662           output_addr_const (stream, x);
1663           return;
1664         }
1665
1666       if (regno > MMIX_LAST_GENERAL_REGISTER - 1)
1667         internal_error ("MMIX Internal: Bad register: %d", regno);
1668
1669       /* This is big-endian, so the low-part is + 1.  */
1670       fprintf (stream, "%s", reg_names[MMIX_OUTPUT_REGNO (regno) + 1]);
1671       return;
1672
1673       /* Can't use 'a' because that's a generic modifier for address
1674          output.  */
1675     case 'A':
1676       mmix_output_shiftvalue_op_from_str (stream, "ANDN",
1677                                           ~(unsigned HOST_WIDEST_INT)
1678                                           mmix_intval (x));
1679       return;
1680
1681     case 'i':
1682       mmix_output_shiftvalue_op_from_str (stream, "INC",
1683                                           (unsigned HOST_WIDEST_INT)
1684                                           mmix_intval (x));
1685       return;
1686
1687     case 'o':
1688       mmix_output_shiftvalue_op_from_str (stream, "OR",
1689                                           (unsigned HOST_WIDEST_INT)
1690                                           mmix_intval (x));
1691       return;
1692
1693     case 's':
1694       mmix_output_shiftvalue_op_from_str (stream, "SET",
1695                                           (unsigned HOST_WIDEST_INT)
1696                                           mmix_intval (x));
1697       return;
1698
1699     case 'd':
1700     case 'D':
1701       mmix_output_condition (stream, x, (code == 'D'));
1702       return;
1703
1704     case 'e':
1705       /* Output an extra "e" to make fcmpe, fune.  */
1706       if (TARGET_FCMP_EPSILON)
1707         fprintf (stream, "e");
1708       return;
1709
1710     case 'm':
1711       /* Output the number minus 1.  */
1712       if (GET_CODE (x) != CONST_INT)
1713         {
1714           fatal_insn ("MMIX Internal: Bad value for 'm', not a CONST_INT",
1715                       x);
1716         }
1717       fprintf (stream, HOST_WIDEST_INT_PRINT_DEC,
1718                (HOST_WIDEST_INT) (mmix_intval (x) - 1));
1719       return;
1720
1721     case 'p':
1722       /* Store the number of registers we want to save.  This was setup
1723          by the prologue.  The actual operand contains the number of
1724          registers to pass, but we don't use it currently.  Anyway, we
1725          need to output the number of saved registers here.  */
1726       fprintf (stream, "%d",
1727                cfun->machine->highest_saved_stack_register + 1);
1728       return;
1729
1730     case 'r':
1731       /* Store the register to output a constant to.  */
1732       if (! REG_P (x))
1733         fatal_insn ("MMIX Internal: Expected a register, not this", x);
1734       mmix_output_destination_register = MMIX_OUTPUT_REGNO (regno);
1735       return;
1736
1737     case 'I':
1738       /* Output the constant.  Note that we use this for floats as well.  */
1739       if (GET_CODE (x) != CONST_INT
1740           && (GET_CODE (x) != CONST_DOUBLE
1741               || (GET_MODE (x) != VOIDmode && GET_MODE (x) != DFmode
1742                   && GET_MODE (x) != SFmode)))
1743         fatal_insn ("MMIX Internal: Expected a constant, not this", x);
1744       mmix_output_register_setting (stream,
1745                                     mmix_output_destination_register,
1746                                     mmix_intval (x), 0);
1747       return;
1748
1749     case 'U':
1750       /* An U for unsigned, if TARGET_ZERO_EXTEND.  Ignore the operand.  */
1751       if (TARGET_ZERO_EXTEND)
1752         putc ('U', stream);
1753       return;
1754
1755     case 'v':
1756       mmix_output_shifted_value (stream, (HOST_WIDEST_INT) mmix_intval (x));
1757       return;
1758
1759     case 'V':
1760       mmix_output_shifted_value (stream, (HOST_WIDEST_INT) ~mmix_intval (x));
1761       return;
1762
1763     case 'W':
1764       if (GET_CODE (x) != CONST_INT)
1765         fatal_insn ("MMIX Internal: Expected a CONST_INT, not this", x);
1766       fprintf (stream, "#%x", (int) (INTVAL (x) & 0xffff));
1767       return;
1768
1769     case 0:
1770       /* Nothing to do.  */
1771       break;
1772
1773     default:
1774       /* Presumably there's a missing case above if we get here.  */
1775       internal_error ("MMIX Internal: Missing `%c' case in mmix_print_operand", code);
1776     }
1777
1778   switch (GET_CODE (modified_x))
1779     {
1780     case REG:
1781       regno = REGNO (modified_x);
1782       if (regno >= FIRST_PSEUDO_REGISTER)
1783         internal_error ("MMIX Internal: Bad register: %d", regno);
1784       fprintf (stream, "%s", reg_names[MMIX_OUTPUT_REGNO (regno)]);
1785       return;
1786
1787     case MEM:
1788       output_address (XEXP (modified_x, 0));
1789       return;
1790
1791     case CONST_INT:
1792       /* For -2147483648, mmixal complains that the constant does not fit
1793          in 4 bytes, so let's output it as hex.  Take care to handle hosts
1794          where HOST_WIDE_INT is longer than an int.
1795
1796          Print small constants +-255 using decimal.  */
1797
1798       if (INTVAL (modified_x) > -256 && INTVAL (modified_x) < 256)
1799         fprintf (stream, "%d", (int) (INTVAL (modified_x)));
1800       else
1801         fprintf (stream, "#%x",
1802                  (int) (INTVAL (modified_x)) & (unsigned int) ~0);
1803       return;
1804
1805     case CONST_DOUBLE:
1806       /* Do somewhat as CONST_INT.  */
1807       mmix_output_octa (stream, mmix_intval (modified_x), 0);
1808       return;
1809
1810     case CONST:
1811       output_addr_const (stream, modified_x);
1812       return;
1813
1814     default:
1815       /* No need to test for all strange things.  Let output_addr_const do
1816          it for us.  */
1817       if (CONSTANT_P (modified_x)
1818           /* Strangely enough, this is not included in CONSTANT_P.
1819              FIXME: Ask/check about sanity here.  */
1820           || GET_CODE (modified_x) == CODE_LABEL)
1821         {
1822           output_addr_const (stream, modified_x);
1823           return;
1824         }
1825
1826       /* We need the original here.  */
1827       fatal_insn ("MMIX Internal: Cannot decode this operand", x);
1828     }
1829 }
1830
1831 /* PRINT_OPERAND_PUNCT_VALID_P.  */
1832
1833 int
1834 mmix_print_operand_punct_valid_p (code)
1835      int code ATTRIBUTE_UNUSED;
1836 {
1837   /* A '+' is used for branch prediction, similar to other ports.  */
1838   return code == '+'
1839     /* A '.' is used for the %d in the POP %d,0 return insn.  */
1840     || code == '.';
1841 }
1842
1843 /* PRINT_OPERAND_ADDRESS.  */
1844
1845 void
1846 mmix_print_operand_address (stream, x)
1847      FILE *stream;
1848      rtx x;
1849 {
1850   if (REG_P (x))
1851     {
1852       /* I find the generated assembly code harder to read without
1853          the ",0".  */
1854       fprintf (stream, "%s,0", reg_names[MMIX_OUTPUT_REGNO (REGNO (x))]);
1855       return;
1856     }
1857   else if (GET_CODE (x) == PLUS)
1858     {
1859       rtx x1 = XEXP (x, 0);
1860       rtx x2 = XEXP (x, 1);
1861
1862       if (REG_P (x1))
1863         {
1864           fprintf (stream, "%s,", reg_names[MMIX_OUTPUT_REGNO (REGNO (x1))]);
1865
1866           if (REG_P (x2))
1867             {
1868               fprintf (stream, "%s",
1869                        reg_names[MMIX_OUTPUT_REGNO (REGNO (x2))]);
1870               return;
1871             }
1872           else if (GET_CODE (x2) == CONST_INT
1873                    && CONST_OK_FOR_LETTER_P (INTVAL (x2), 'I'))
1874             {
1875               output_addr_const (stream, x2);
1876               return;
1877             }
1878         }
1879     }
1880
1881   if (TARGET_BASE_ADDRESSES && mmix_legitimate_constant_p (x))
1882     {
1883       output_addr_const (stream, x);
1884       return;
1885     }
1886
1887   fatal_insn ("MMIX Internal: This is not a recognized address", x);
1888 }
1889
1890 /* ASM_OUTPUT_REG_PUSH.  */
1891
1892 void
1893 mmix_asm_output_reg_push (stream, regno)
1894      FILE * stream;
1895      int regno;
1896 {
1897   fprintf (stream, "\tSUBU %s,%s,8\n\tSTOU %s,%s,0\n",
1898            reg_names[MMIX_STACK_POINTER_REGNUM],
1899            reg_names[MMIX_STACK_POINTER_REGNUM],
1900            reg_names[MMIX_OUTPUT_REGNO (regno)],
1901            reg_names[MMIX_STACK_POINTER_REGNUM]);
1902 }
1903
1904 /* ASM_OUTPUT_REG_POP.  */
1905
1906 void
1907 mmix_asm_output_reg_pop (stream, regno)
1908      FILE * stream;
1909      int regno;
1910 {
1911   fprintf (stream, "\tLDOU %s,%s,0\n\tINCL %s,8\n",
1912            reg_names[MMIX_OUTPUT_REGNO (regno)],
1913            reg_names[MMIX_STACK_POINTER_REGNUM],
1914            reg_names[MMIX_STACK_POINTER_REGNUM]);
1915 }
1916
1917 /* ASM_OUTPUT_ADDR_DIFF_ELT.  */
1918
1919 void
1920 mmix_asm_output_addr_diff_elt (stream, body, value, rel)
1921      FILE *stream;
1922      rtx body ATTRIBUTE_UNUSED;
1923      int value;
1924      int rel;
1925 {
1926   fprintf (stream, "\tTETRA L%d-L%d\n", value, rel);
1927 }
1928
1929 /* ASM_OUTPUT_ADDR_VEC_ELT.  */
1930
1931 void
1932 mmix_asm_output_addr_vec_elt (stream, value)
1933      FILE *stream;
1934      int value;
1935 {
1936   fprintf (stream, "\tOCTA L:%d\n", value);
1937 }
1938
1939 /* ASM_OUTPUT_SKIP.  */
1940
1941 void
1942 mmix_asm_output_skip (stream, nbytes)
1943      FILE *stream;
1944      int nbytes;
1945 {
1946   fprintf (stream, "\tLOC @+%d\n", nbytes);
1947 }
1948
1949 /* ASM_OUTPUT_ALIGN.  */
1950
1951 void
1952 mmix_asm_output_align (stream, power)
1953      FILE *stream;
1954      int power;
1955 {
1956   /* We need to record the needed alignment of this section in the object,
1957      so we have to output an alignment directive.  Use a .p2align (not
1958      .align) so people will never have to wonder about whether the
1959      argument is in number of bytes or the log2 thereof.  We do it in
1960      addition to the LOC directive, so nothing needs tweaking when
1961      copy-pasting assembly into mmixal.  */
1962  fprintf (stream, "\t.p2align %d\n", power);
1963  fprintf (stream, "\tLOC @+(%d-@)&%d\n", 1 << power, (1 << power) - 1);
1964 }
1965
1966 /* DBX_REGISTER_NUMBER.  */
1967
1968 int
1969 mmix_dbx_register_number (regno)
1970      int regno;
1971 {
1972   /* Adjust the register number to the one it will be output as, dammit.
1973      It'd be nice if we could check the assumption that we're filling a
1974      gap, but every register between the last saved register and parameter
1975      registers might be a valid parameter register.  */
1976   regno = MMIX_OUTPUT_REGNO (regno);
1977
1978   /* We need to renumber registers to get the number of the return address
1979      register in the range 0..255.  It is also space-saving if registers
1980      mentioned in the call-frame information (which uses this function by
1981      defaulting DWARF_FRAME_REGNUM to DBX_REGISTER_NUMBER) are numbered
1982      0 .. 63.  So map 224 .. 256+15 -> 0 .. 47 and 0 .. 223 -> 48..223+48.  */
1983   return regno >= 224 ? (regno - 224) : (regno + 48);
1984 }
1985
1986 /* End of target macro support functions.
1987
1988    Now MMIX's own functions.  First the exported ones.  */
1989
1990 /* Wrapper for get_hard_reg_initial_val since integrate.h isn't included
1991    from insn-emit.c.  */
1992
1993 rtx
1994 mmix_get_hard_reg_initial_val (mode, regno)
1995      enum machine_mode mode;
1996      int regno;
1997 {
1998   return get_hard_reg_initial_val (mode, regno);
1999 }
2000
2001 /* Nonzero when the function epilogue is simple enough that a single
2002    "POP %d,0" should be used even within the function.  */
2003
2004 int
2005 mmix_use_simple_return ()
2006 {
2007   int regno;
2008
2009   int stack_space_to_allocate
2010     = (current_function_outgoing_args_size
2011        + current_function_pretend_args_size
2012        + get_frame_size () + 7) & ~7;
2013
2014   if (!TARGET_USE_RETURN_INSN || !reload_completed)
2015     return 0;
2016
2017   for (regno = 255;
2018        regno >= MMIX_FIRST_GLOBAL_REGNUM;
2019        regno--)
2020     /* Note that we assume that the frame-pointer-register is one of these
2021        registers, in which case we don't count it here.  */
2022     if ((((regno != MMIX_FRAME_POINTER_REGNUM || !frame_pointer_needed)
2023           && regs_ever_live[regno] && !call_used_regs[regno]))
2024         || IS_MMIX_EH_RETURN_DATA_REG (regno))
2025       return 0;
2026
2027   if (frame_pointer_needed)
2028     stack_space_to_allocate += 8;
2029
2030   if (MMIX_CFUN_HAS_LANDING_PAD)
2031     stack_space_to_allocate += 16;
2032   else if (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS)
2033     stack_space_to_allocate += 8;
2034
2035   return stack_space_to_allocate == 0;
2036 }
2037
2038
2039 /* Expands the function prologue into RTX.  */
2040
2041 void
2042 mmix_expand_prologue ()
2043 {
2044   HOST_WIDE_INT locals_size = get_frame_size ();
2045   int regno;
2046   HOST_WIDE_INT stack_space_to_allocate
2047     = (current_function_outgoing_args_size
2048        + current_function_pretend_args_size
2049        + locals_size + 7) & ~7;
2050   HOST_WIDE_INT offset = -8;
2051
2052   /* Add room needed to save global non-register-stack registers.  */
2053   for (regno = 255;
2054        regno >= MMIX_FIRST_GLOBAL_REGNUM;
2055        regno--)
2056     /* Note that we assume that the frame-pointer-register is one of these
2057        registers, in which case we don't count it here.  */
2058     if ((((regno != MMIX_FRAME_POINTER_REGNUM || !frame_pointer_needed)
2059           && regs_ever_live[regno] && !call_used_regs[regno]))
2060         || IS_MMIX_EH_RETURN_DATA_REG (regno))
2061       stack_space_to_allocate += 8;
2062
2063   /* If we do have a frame-pointer, add room for it.  */
2064   if (frame_pointer_needed)
2065     stack_space_to_allocate += 8;
2066
2067   /* If we have a non-local label, we need to be able to unwind to it, so
2068      store the current register stack pointer.  Also store the return
2069      address if we do that.  */
2070   if (MMIX_CFUN_HAS_LANDING_PAD)
2071     stack_space_to_allocate += 16;
2072   else if (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS)
2073     /* If we do have a saved return-address slot, add room for it.  */
2074     stack_space_to_allocate += 8;
2075
2076   /* Make sure we don't get an unaligned stack.  */
2077   if ((stack_space_to_allocate % 8) != 0)
2078     internal_error ("stack frame not a multiple of 8 bytes: %d",
2079                     stack_space_to_allocate);
2080
2081   if (current_function_pretend_args_size)
2082     {
2083       int mmix_first_vararg_reg
2084         = (MMIX_FIRST_INCOMING_ARG_REGNUM
2085            + (MMIX_MAX_ARGS_IN_REGS
2086               - current_function_pretend_args_size / 8));
2087
2088       for (regno
2089              = MMIX_FIRST_INCOMING_ARG_REGNUM + MMIX_MAX_ARGS_IN_REGS - 1;
2090            regno >= mmix_first_vararg_reg;
2091            regno--)
2092         {
2093           if (offset < 0)
2094             {
2095               HOST_WIDE_INT stack_chunk
2096                 = stack_space_to_allocate > (256 - 8)
2097                 ? (256 - 8) : stack_space_to_allocate;
2098
2099               mmix_emit_sp_add (-stack_chunk);
2100               offset += stack_chunk;
2101               stack_space_to_allocate -= stack_chunk;
2102             }
2103
2104           /* These registers aren't actually saved (as in "will be
2105              restored"), so don't tell DWARF2 they're saved.  */
2106           emit_move_insn (gen_rtx_MEM (DImode,
2107                                        plus_constant (stack_pointer_rtx,
2108                                                       offset)),
2109                           gen_rtx_REG (DImode, regno));
2110           offset -= 8;
2111         }
2112     }
2113
2114   /* Store the frame-pointer.  */
2115
2116   if (frame_pointer_needed)
2117     {
2118       rtx insn;
2119
2120       if (offset < 0)
2121         {
2122           /* Get 8 less than otherwise, since we need to reach offset + 8.  */
2123           HOST_WIDE_INT stack_chunk
2124             = stack_space_to_allocate > (256 - 8 - 8)
2125             ? (256 - 8 - 8) : stack_space_to_allocate;
2126
2127           mmix_emit_sp_add (-stack_chunk);
2128
2129           offset += stack_chunk;
2130           stack_space_to_allocate -= stack_chunk;
2131         }
2132
2133       insn = emit_move_insn (gen_rtx_MEM (DImode,
2134                                           plus_constant (stack_pointer_rtx,
2135                                                          offset)),
2136                              hard_frame_pointer_rtx);
2137       RTX_FRAME_RELATED_P (insn) = 1;
2138       insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
2139                                     stack_pointer_rtx,
2140                                     GEN_INT (offset + 8)));
2141       RTX_FRAME_RELATED_P (insn) = 1;
2142       offset -= 8;
2143     }
2144
2145   if (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS)
2146     {
2147       rtx tmpreg, retreg;
2148       rtx insn;
2149
2150       /* Store the return-address, if one is needed on the stack.  We
2151          usually store it in a register when needed, but that doesn't work
2152          with -fexceptions.  */
2153
2154       if (offset < 0)
2155         {
2156           /* Get 8 less than otherwise, since we need to reach offset + 8.  */
2157           HOST_WIDE_INT stack_chunk
2158             = stack_space_to_allocate > (256 - 8 - 8)
2159             ? (256 - 8 - 8) : stack_space_to_allocate;
2160
2161           mmix_emit_sp_add (-stack_chunk);
2162
2163           offset += stack_chunk;
2164           stack_space_to_allocate -= stack_chunk;
2165         }
2166
2167       tmpreg = gen_rtx_REG (DImode, 255);
2168       retreg = gen_rtx_REG (DImode, MMIX_rJ_REGNUM);
2169
2170       /* Dwarf2 code is confused by the use of a temporary register for
2171          storing the return address, so we have to express it as a note,
2172          which we attach to the actual store insn.  */
2173       emit_move_insn (tmpreg, retreg);
2174
2175       insn = emit_move_insn (gen_rtx_MEM (DImode,
2176                                           plus_constant (stack_pointer_rtx,
2177                                                          offset)),
2178                              tmpreg);
2179       RTX_FRAME_RELATED_P (insn) = 1;
2180       REG_NOTES (insn)
2181         = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2182                              gen_rtx_SET (VOIDmode,
2183                                           gen_rtx_MEM (DImode,
2184                                                        plus_constant (stack_pointer_rtx,
2185                                                                       offset)),
2186                                           retreg),
2187                              REG_NOTES (insn));
2188
2189       offset -= 8;
2190     }
2191   else if (MMIX_CFUN_HAS_LANDING_PAD)
2192     offset -= 8;
2193
2194   if (MMIX_CFUN_HAS_LANDING_PAD)
2195     {
2196       /* Store the register defining the numbering of local registers, so
2197          we know how long to unwind the register stack.  */
2198
2199       if (offset < 0)
2200         {
2201           /* Get 8 less than otherwise, since we need to reach offset + 8.  */
2202           HOST_WIDE_INT stack_chunk
2203             = stack_space_to_allocate > (256 - 8 - 8)
2204             ? (256 - 8 - 8) : stack_space_to_allocate;
2205
2206           mmix_emit_sp_add (-stack_chunk);
2207
2208           offset += stack_chunk;
2209           stack_space_to_allocate -= stack_chunk;
2210         }
2211
2212       /* We don't tell dwarf2 about this one; we just have it to unwind
2213          the register stack at landing pads.  FIXME: It's a kludge because
2214          we can't describe the effect of the PUSHJ and PUSHGO insns on the
2215          register stack at the moment.  Best thing would be to handle it
2216          like stack-pointer offsets.  Better: some hook into dwarf2out.c
2217          to produce DW_CFA_expression:s that specify the increment of rO,
2218          and unwind it at eh_return (preferred) or at the landing pad.
2219          Then saves to $0..$G-1 could be specified through that register.  */
2220
2221       emit_move_insn (gen_rtx_REG (DImode, 255),
2222                       gen_rtx_REG (DImode,
2223                                    MMIX_rO_REGNUM));
2224       emit_move_insn (gen_rtx_MEM (DImode,
2225                                    plus_constant (stack_pointer_rtx, offset)),
2226                       gen_rtx_REG (DImode, 255));
2227       offset -= 8;
2228     }
2229
2230   /* After the return-address and the frame-pointer, we have the local
2231      variables.  They're the ones that may have an "unaligned" size.  */
2232   offset -= (locals_size + 7) & ~7;
2233
2234   /* Now store all registers that are global, i.e. not saved by the
2235      register file machinery.
2236
2237      It is assumed that the frame-pointer is one of these registers, so it
2238      is explicitly excluded in the count.  */
2239
2240   for (regno = 255;
2241        regno >= MMIX_FIRST_GLOBAL_REGNUM;
2242        regno--)
2243     if (((regno != MMIX_FRAME_POINTER_REGNUM || !frame_pointer_needed)
2244          && regs_ever_live[regno] && ! call_used_regs[regno])
2245         || IS_MMIX_EH_RETURN_DATA_REG (regno))
2246       {
2247         rtx insn;
2248
2249         if (offset < 0)
2250           {
2251             HOST_WIDE_INT stack_chunk
2252               = (stack_space_to_allocate > (256 - offset - 8)
2253                  ? (256 - offset - 8) : stack_space_to_allocate);
2254
2255             mmix_emit_sp_add (-stack_chunk);
2256             offset += stack_chunk;
2257             stack_space_to_allocate -= stack_chunk;
2258           }
2259
2260         insn = emit_move_insn (gen_rtx_MEM (DImode,
2261                                             plus_constant (stack_pointer_rtx,
2262                                                            offset)),
2263                                gen_rtx_REG (DImode, regno));
2264         RTX_FRAME_RELATED_P (insn) = 1;
2265         offset -= 8;
2266       }
2267
2268   /* Finally, allocate room for outgoing args and local vars if room
2269      wasn't allocated above.  */
2270   if (stack_space_to_allocate)
2271     mmix_emit_sp_add (-stack_space_to_allocate);
2272 }
2273
2274 /* Expands the function epilogue into RTX.  */
2275
2276 void
2277 mmix_expand_epilogue ()
2278 {
2279   HOST_WIDE_INT locals_size = get_frame_size ();
2280   int regno;
2281   HOST_WIDE_INT stack_space_to_deallocate
2282     = (current_function_outgoing_args_size
2283        + current_function_pretend_args_size
2284        + locals_size + 7) & ~7;
2285
2286   /* The assumption that locals_size fits in an int is asserted in
2287      mmix_expand_prologue.  */
2288
2289   /* The first address to access is beyond the outgoing_args area.  */
2290   int offset = current_function_outgoing_args_size;
2291
2292   /* Add the space for global non-register-stack registers.
2293      It is assumed that the frame-pointer register can be one of these
2294      registers, in which case it is excluded from the count when needed.  */
2295   for (regno = 255;
2296        regno >= MMIX_FIRST_GLOBAL_REGNUM;
2297        regno--)
2298     if (((regno != MMIX_FRAME_POINTER_REGNUM || !frame_pointer_needed)
2299          && regs_ever_live[regno] && !call_used_regs[regno])
2300         || IS_MMIX_EH_RETURN_DATA_REG (regno))
2301       stack_space_to_deallocate += 8;
2302
2303   /* Add in the space for register stack-pointer.  If so, always add room
2304      for the saved PC.  */
2305   if (MMIX_CFUN_HAS_LANDING_PAD)
2306     stack_space_to_deallocate += 16;
2307   else if (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS)
2308     /* If we have a saved return-address slot, add it in.  */
2309     stack_space_to_deallocate += 8;
2310
2311   /* Add in the frame-pointer.  */
2312   if (frame_pointer_needed)
2313     stack_space_to_deallocate += 8;
2314
2315   /* Make sure we don't get an unaligned stack.  */
2316   if ((stack_space_to_deallocate % 8) != 0)
2317     internal_error ("stack frame not a multiple of octabyte: %d",
2318                     stack_space_to_deallocate);
2319
2320   /* We will add back small offsets to the stack pointer as we go.
2321      First, we restore all registers that are global, i.e. not saved by
2322      the register file machinery.  */
2323
2324   for (regno = MMIX_FIRST_GLOBAL_REGNUM;
2325        regno <= 255;
2326        regno++)
2327     if (((regno != MMIX_FRAME_POINTER_REGNUM || !frame_pointer_needed)
2328          && regs_ever_live[regno] && !call_used_regs[regno])
2329         || IS_MMIX_EH_RETURN_DATA_REG (regno))
2330       {
2331         if (offset > 255)
2332           {
2333             mmix_emit_sp_add (offset);
2334             stack_space_to_deallocate -= offset;
2335             offset = 0;
2336           }
2337
2338         emit_move_insn (gen_rtx_REG (DImode, regno),
2339                         gen_rtx_MEM (DImode,
2340                                      plus_constant (stack_pointer_rtx,
2341                                                     offset)));
2342         offset += 8;
2343       }
2344
2345   /* Here is where the local variables were.  As in the prologue, they
2346      might be of an unaligned size.  */
2347   offset += (locals_size + 7) & ~7;
2348
2349
2350   /* The saved register stack pointer is just below the frame-pointer
2351      register.  We don't need to restore it "manually"; the POP
2352      instruction does that.  */
2353   if (MMIX_CFUN_HAS_LANDING_PAD)
2354     offset += 16;
2355   else if (MMIX_CFUN_NEEDS_SAVED_EH_RETURN_ADDRESS)
2356     /* The return-address slot is just below the frame-pointer register.
2357        We don't need to restore it because we don't really use it.  */
2358     offset += 8;
2359
2360   /* Get back the old frame-pointer-value.  */
2361   if (frame_pointer_needed)
2362     {
2363       if (offset > 255)
2364         {
2365           mmix_emit_sp_add (offset);
2366
2367           stack_space_to_deallocate -= offset;
2368           offset = 0;
2369         }
2370
2371       emit_move_insn (hard_frame_pointer_rtx,
2372                       gen_rtx_MEM (DImode,
2373                                    plus_constant (stack_pointer_rtx,
2374                                                   offset)));
2375       offset += 8;
2376     }
2377
2378   /* We do not need to restore pretended incoming args, just add back
2379      offset to sp.  */
2380   if (stack_space_to_deallocate != 0)
2381     mmix_emit_sp_add (stack_space_to_deallocate);
2382
2383   if (current_function_calls_eh_return)
2384     /* Adjust the (normal) stack-pointer to that of the receiver.
2385        FIXME: It would be nice if we could also adjust the register stack
2386        here, but we need to express it through DWARF 2 too.  */
2387     emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
2388                            gen_rtx_REG (DImode,
2389                                         MMIX_EH_RETURN_STACKADJ_REGNUM)));
2390 }
2391
2392 /* Output an optimal sequence for setting a register to a specific
2393    constant.  Used in an alternative for const_ints in movdi, and when
2394    using large stack-frame offsets.
2395
2396    Use do_begin_end to say if a line-starting TAB and newline before the
2397    first insn and after the last insn is wanted.  */
2398
2399 void
2400 mmix_output_register_setting (stream, regno, value, do_begin_end)
2401      FILE *stream;
2402      int regno;
2403      HOST_WIDEST_INT value;
2404      int do_begin_end;
2405 {
2406   if (do_begin_end)
2407     fprintf (stream, "\t");
2408
2409   if (mmix_shiftable_wyde_value ((unsigned HOST_WIDEST_INT) value))
2410     {
2411       /* First, the one-insn cases.  */
2412       mmix_output_shiftvalue_op_from_str (stream, "SET",
2413                                           (unsigned HOST_WIDEST_INT)
2414                                           value);
2415       fprintf (stream, " %s,", reg_names[regno]);
2416       mmix_output_shifted_value (stream, (unsigned HOST_WIDEST_INT) value);
2417     }
2418   else if (mmix_shiftable_wyde_value (-(unsigned HOST_WIDEST_INT) value))
2419     {
2420       /* We do this to get a bit more legible assembly code.  The next
2421          alternative is mostly redundant with this.  */
2422
2423       mmix_output_shiftvalue_op_from_str (stream, "SET",
2424                                           -(unsigned HOST_WIDEST_INT)
2425                                           value);
2426       fprintf (stream, " %s,", reg_names[regno]);
2427       mmix_output_shifted_value (stream, -(unsigned HOST_WIDEST_INT) value);
2428       fprintf (stream, "\n\tNEGU %s,0,%s", reg_names[regno],
2429                reg_names[regno]);
2430     }
2431   else if (mmix_shiftable_wyde_value (~(unsigned HOST_WIDEST_INT) value))
2432     {
2433       /* Slightly more expensive, the two-insn cases.  */
2434
2435       /* FIXME: We could of course also test if 0..255-N or ~(N | 1..255)
2436          is shiftable, or any other one-insn transformation of the value.
2437          FIXME: Check first if the value is "shiftable" by two loading
2438          with two insns, since it makes more readable assembly code (if
2439          anyone else cares).  */
2440
2441       mmix_output_shiftvalue_op_from_str (stream, "SET",
2442                                           ~(unsigned HOST_WIDEST_INT)
2443                                           value);
2444       fprintf (stream, " %s,", reg_names[regno]);
2445       mmix_output_shifted_value (stream, ~(unsigned HOST_WIDEST_INT) value);
2446       fprintf (stream, "\n\tNOR %s,%s,0", reg_names[regno],
2447                reg_names[regno]);
2448     }
2449   else
2450     {
2451       /* The generic case.  2..4 insns.  */
2452       static const char *const higher_parts[] = {"L", "ML", "MH", "H"};
2453       const char *op = "SET";
2454       const char *line_begin = "";
2455       int insns = 0;
2456       int i;
2457       HOST_WIDEST_INT tmpvalue = value;
2458
2459       /* Compute the number of insns needed to output this constant.  */
2460       for (i = 0; i < 4 && tmpvalue != 0; i++)
2461         {
2462           if (tmpvalue & 65535)
2463             insns++;
2464           tmpvalue >>= 16;
2465         }
2466       if (TARGET_BASE_ADDRESSES && insns == 3)
2467         {
2468           /* The number three is based on a static observation on
2469              ghostscript-6.52.  Two and four are excluded because there
2470              are too many such constants, and each unique constant (maybe
2471              offset by 1..255) were used few times compared to other uses,
2472              e.g. addresses.
2473
2474              We use base-plus-offset addressing to force it into a global
2475              register; we just use a "LDA reg,VALUE", which will cause the
2476              assembler and linker to DTRT (for constants as well as
2477              addresses).  */
2478           fprintf (stream, "LDA %s,", reg_names[regno]);
2479           mmix_output_octa (stream, value, 0);
2480         }
2481       else
2482         {
2483           /* Output pertinent parts of the 4-wyde sequence.
2484              Still more to do if we want this to be optimal, but hey...
2485              Note that the zero case has been handled above.  */
2486           for (i = 0; i < 4 && value != 0; i++)
2487             {
2488               if (value & 65535)
2489                 {
2490                   fprintf (stream, "%s%s%s %s,#%x", line_begin, op,
2491                            higher_parts[i], reg_names[regno],
2492                            (int) (value & 65535));
2493                   /* The first one sets the rest of the bits to 0, the next
2494                      ones add set bits.  */
2495                   op = "INC";
2496                   line_begin = "\n\t";
2497                 }
2498
2499               value >>= 16;
2500             }
2501         }
2502     }
2503
2504   if (do_begin_end)
2505     fprintf (stream, "\n");
2506 }
2507
2508 /* Return 1 if value is 0..65535*2**(16*N) for N=0..3.
2509    else return 0.  */
2510
2511 int
2512 mmix_shiftable_wyde_value (value)
2513      unsigned HOST_WIDEST_INT value;
2514 {
2515   /* Shift by 16 bits per group, stop when we've found two groups with
2516      nonzero bits.  */
2517   int i;
2518   int has_candidate = 0;
2519
2520   for (i = 0; i < 4; i++)
2521     {
2522       if (value & 65535)
2523         {
2524           if (has_candidate)
2525             return 0;
2526           else
2527             has_candidate = 1;
2528         }
2529
2530       value >>= 16;
2531     }
2532
2533   return 1;
2534 }
2535
2536 /* True if this is an address_operand or a symbolic operand.  */
2537
2538 int
2539 mmix_symbolic_or_address_operand (op, mode)
2540      rtx op;
2541      enum machine_mode mode;
2542 {
2543   switch (GET_CODE (op))
2544     {
2545     case SYMBOL_REF:
2546     case LABEL_REF:
2547       return 1;
2548     case CONST:
2549       op = XEXP (op, 0);
2550       if ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
2551            || GET_CODE (XEXP (op, 0)) == LABEL_REF)
2552           && (GET_CODE (XEXP (op, 1)) == CONST_INT
2553               || (GET_CODE (XEXP (op, 1)) == CONST_DOUBLE
2554                   && GET_MODE (XEXP (op, 1)) == VOIDmode)))
2555         return 1;
2556       /* FALLTHROUGH */
2557     default:
2558       return address_operand (op, mode);
2559     }
2560 }
2561
2562 /* True if this is a register or CONST_INT (or CONST_DOUBLE for DImode).
2563    We could narrow the value down with a couple of predicated, but that
2564    doesn't seem to be worth it at the moment.  */
2565
2566 int
2567 mmix_reg_or_constant_operand (op, mode)
2568      rtx op;
2569      enum machine_mode mode;
2570 {
2571   return register_operand (op, mode)
2572     || (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == VOIDmode)
2573     || GET_CODE (op) == CONST_INT;
2574 }
2575
2576 /* True if this is a register with a condition-code mode.  */
2577
2578 int
2579 mmix_reg_cc_operand (op, mode)
2580      rtx op;
2581      enum machine_mode mode;
2582 {
2583   if (mode == VOIDmode)
2584     mode = GET_MODE (op);
2585
2586   return register_operand (op, mode)
2587     && (mode == CCmode || mode == CC_UNSmode || mode == CC_FPmode
2588         || mode == CC_FPEQmode || mode == CC_FUNmode);
2589 }
2590
2591 /* True if this is a foldable comparison operator
2592    - one where a the result of (compare:CC (reg) (const_int 0)) can be
2593    replaced by (reg).  */
2594
2595 int
2596 mmix_foldable_comparison_operator (op, mode)
2597      rtx op;
2598      enum machine_mode mode;
2599 {
2600   RTX_CODE code = GET_CODE (op);
2601
2602   if (mode == VOIDmode)
2603     mode = GET_MODE (op);
2604
2605   if (mode == VOIDmode && GET_RTX_CLASS (GET_CODE (op)) == '<')
2606     mode = GET_MODE (XEXP (op, 0));
2607
2608   return ((mode == CCmode || mode == DImode)
2609           && (code == NE || code == EQ || code == GE || code == GT
2610               || code == LE))
2611     /* FIXME: This may be a stupid trick.  What happens when GCC wants to
2612        reverse the condition?  Can it do that by itself?  Maybe it can
2613        even reverse the condition to fit a foldable one in the first
2614        place?  */
2615     || (mode == CC_UNSmode && (code == GTU || code == LEU));
2616 }
2617
2618 /* Like comparison_operator, but only true if this comparison operator is
2619    applied to a valid mode.  Needed to avoid jump.c generating invalid
2620    code with -ffast-math (gcc.dg/20001228-1.c).  */
2621
2622 int
2623 mmix_comparison_operator (op, mode)
2624      rtx op;
2625      enum machine_mode mode;
2626 {
2627   RTX_CODE code = GET_CODE (op);
2628
2629   /* Comparison operators usually don't have a mode, but let's try and get
2630      one anyway for the day that changes.  */
2631   if (mode == VOIDmode)
2632     mode = GET_MODE (op);
2633
2634   /* Get the mode from the first operand if we don't have one.  */
2635   if (mode == VOIDmode && GET_RTX_CLASS (GET_CODE (op)) == '<')
2636     mode = GET_MODE (XEXP (op, 0));
2637
2638   /* FIXME: This needs to be kept in sync with the tables in
2639      mmix_output_condition.  */
2640   return
2641     (mode == VOIDmode && GET_RTX_CLASS (GET_CODE (op)) == '<')
2642     || (mode == CC_FUNmode
2643         && (code == ORDERED || code == UNORDERED))
2644     || (mode == CC_FPmode
2645         && (code == GT || code == LT))
2646     || (mode == CC_FPEQmode
2647         && (code == NE || code == EQ))
2648     || (mode == CC_UNSmode
2649         && (code == GEU || code == GTU || code == LEU || code == LTU))
2650     || (mode == CCmode
2651         && (code == NE || code == EQ || code == GE || code == GT
2652             || code == LE || code == LT))
2653     || (mode == DImode
2654         && (code == NE || code == EQ || code == GE || code == GT
2655             || code == LE || code == LT || code == LEU || code == GTU));
2656 }
2657
2658 /* True if this is a register or 0 (int or float).  */
2659
2660 int
2661 mmix_reg_or_0_operand (op, mode)
2662      rtx op;
2663      enum machine_mode mode;
2664 {
2665   /* FIXME: Is mode calculation necessary and correct?  */
2666   return
2667     op == CONST0_RTX (mode == VOIDmode ? GET_MODE (op) : mode)
2668     || register_operand (op, mode);
2669 }
2670
2671 /* True if this is a register or an int 0..255.  */
2672
2673 int
2674 mmix_reg_or_8bit_operand (op, mode)
2675      rtx op;
2676      enum machine_mode mode;
2677 {
2678   return register_operand (op, mode)
2679     || (GET_CODE (op) == CONST_INT
2680         && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
2681 }
2682
2683 /* Returns zero if code and mode is not a valid condition from a
2684    compare-type insn.  Nonzero if it is.  The parameter op, if non-NULL,
2685    is the comparison of mode is CC-somethingmode.  */
2686
2687 int
2688 mmix_valid_comparison (code, mode, op)
2689      RTX_CODE code;
2690      enum machine_mode mode;
2691      rtx op;
2692 {
2693   if (mode == VOIDmode && op != NULL_RTX)
2694     mode = GET_MODE (op);
2695
2696   /* We don't care to look at these, they should always be valid.  */
2697   if (mode == CCmode || mode == CC_UNSmode || mode == DImode)
2698     return 1;
2699
2700   if ((mode == CC_FPmode || mode == DFmode)
2701       && (code == GT || code == LT))
2702     return 1;
2703
2704   if ((mode == CC_FPEQmode || mode == DFmode)
2705       && (code == EQ || code == NE))
2706     return 1;
2707
2708   if ((mode == CC_FUNmode || mode == DFmode)
2709       && (code == ORDERED || code == UNORDERED))
2710     return 1;
2711
2712   return 0;
2713 }
2714
2715 /* X and Y are two things to compare using CODE.  Emit a compare insn if
2716    possible and return the rtx for the cc-reg in the proper mode, or
2717    NULL_RTX if this is not a valid comparison.  */
2718
2719 rtx
2720 mmix_gen_compare_reg (code, x, y)
2721      RTX_CODE code;
2722      rtx x, y;
2723 {
2724   enum machine_mode ccmode = SELECT_CC_MODE (code, x, y);
2725   rtx cc_reg;
2726
2727   /* FIXME: Do we get constants here?  Of double mode?  */
2728   enum machine_mode mode
2729     = GET_MODE (x) == VOIDmode
2730     ? GET_MODE (y)
2731     : GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT ? DFmode : DImode;
2732
2733   if (! mmix_valid_comparison (code, mode, x))
2734     return NULL_RTX;
2735
2736   cc_reg = gen_reg_rtx (ccmode);
2737
2738   /* FIXME:  Can we avoid emitting a compare insn here?  */
2739   if (! REG_P (x) && ! REG_P (y))
2740     x = force_reg (mode, x);
2741
2742   /* If it's not quite right yet, put y in a register.  */
2743   if (! REG_P (y)
2744       && (GET_CODE (y) != CONST_INT
2745           || ! CONST_OK_FOR_LETTER_P (INTVAL (y), 'I')))
2746     y = force_reg (mode, y);
2747
2748   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
2749                           gen_rtx_COMPARE (ccmode, x, y)));
2750
2751   return cc_reg;
2752 }
2753
2754 /* Local (static) helper functions.  */
2755
2756 static void
2757 mmix_emit_sp_add (offset)
2758      HOST_WIDE_INT offset;
2759 {
2760   rtx insn;
2761
2762   if (offset < 0)
2763     {
2764       /* Negative stack-pointer adjustments are allocations and appear in
2765          the prologue only.  We mark them as frame-related so unwind and
2766          debug info is properly emitted for them.  */
2767       if (offset > -255)
2768         insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
2769                                       stack_pointer_rtx,
2770                                       GEN_INT (offset)));
2771       else
2772         {
2773           rtx tmpr = gen_rtx_REG (DImode, 255);
2774           RTX_FRAME_RELATED_P (emit_move_insn (tmpr, GEN_INT (offset))) = 1;
2775           insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
2776                                         stack_pointer_rtx, tmpr));
2777         }
2778       RTX_FRAME_RELATED_P (insn) = 1;
2779     }
2780   else
2781     {
2782       /* Positive adjustments are in the epilogue only.  Don't mark them
2783          as "frame-related" for unwind info.  */
2784       if (CONST_OK_FOR_LETTER_P (offset, 'L'))
2785         emit_insn (gen_adddi3 (stack_pointer_rtx,
2786                                stack_pointer_rtx,
2787                                GEN_INT (offset)));
2788       else
2789         {
2790           rtx tmpr = gen_rtx_REG (DImode, 255);
2791           emit_move_insn (tmpr, GEN_INT (offset));
2792           insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
2793                                         stack_pointer_rtx, tmpr));
2794         }
2795     }
2796 }
2797
2798 /* Print operator suitable for doing something with a shiftable
2799    wyde.  The type of operator is passed as an asm output modifier.  */
2800
2801 static void
2802 mmix_output_shiftvalue_op_from_str (stream, mainop, value)
2803      FILE *stream;
2804      const char *mainop;
2805      HOST_WIDEST_INT value;
2806 {
2807   static const char *const op_part[] = {"L", "ML", "MH", "H"};
2808   int i;
2809
2810   if (! mmix_shiftable_wyde_value (value))
2811     {
2812       char s[sizeof ("0xffffffffffffffff")];
2813       sprintf (s, HOST_WIDEST_INT_PRINT_HEX, value);
2814       internal_error ("MMIX Internal: %s is not a shiftable int", s);
2815     }
2816
2817   for (i = 0; i < 4; i++)
2818     {
2819       /* We know we're through when we find one-bits in the low
2820          16 bits.  */
2821       if (value & 0xffff)
2822         {
2823           fprintf (stream, "%s%s", mainop, op_part[i]);
2824           return;
2825         }
2826       value >>= 16;
2827     }
2828
2829   /* No bits set?  Then it must have been zero.  */
2830   fprintf (stream, "%sL", mainop);
2831 }
2832
2833 /* Print a 64-bit value, optionally prefixed by assembly pseudo.  */
2834
2835 static void
2836 mmix_output_octa (stream, value, do_begin_end)
2837      FILE *stream;
2838      HOST_WIDEST_INT value;
2839      int do_begin_end;
2840 {
2841   /* Snipped from final.c:output_addr_const.  We need to avoid the
2842      presumed universal "0x" prefix.  We can do it by replacing "0x" with
2843      "#0" here; we must avoid a space in the operands and no, the zero
2844      won't cause the number to be assumed in octal format.  */
2845   char hex_format[sizeof (HOST_WIDEST_INT_PRINT_HEX)];
2846
2847   if (do_begin_end)
2848     fprintf (stream, "\tOCTA ");
2849
2850   strcpy (hex_format, HOST_WIDEST_INT_PRINT_HEX);
2851   hex_format[0] = '#';
2852   hex_format[1] = '0';
2853
2854   /* Provide a few alternative output formats depending on the number, to
2855      improve legibility of assembler output.  */
2856   if ((value < (HOST_WIDEST_INT) 0 && value > (HOST_WIDEST_INT) -10000)
2857       || (value >= (HOST_WIDEST_INT) 0 && value <= (HOST_WIDEST_INT) 16384))
2858     fprintf (stream, "%d", (int) value);
2859   else if (value > (HOST_WIDEST_INT) 0
2860            && value < ((HOST_WIDEST_INT) 1 << 31) * 2)
2861     fprintf (stream, "#%x", (unsigned int) value);
2862   else
2863     fprintf (stream, hex_format, value);
2864
2865   if (do_begin_end)
2866     fprintf (stream, "\n");
2867 }
2868
2869 /* Print the presumed shiftable wyde argument shifted into place (to
2870    be output with an operand).  */
2871
2872 static void
2873 mmix_output_shifted_value (stream, value)
2874      FILE * stream;
2875      HOST_WIDEST_INT value;
2876 {
2877   int i;
2878
2879   if (! mmix_shiftable_wyde_value (value))
2880     {
2881       char s[16+2+1];
2882       sprintf (s, HOST_WIDEST_INT_PRINT_HEX, value);
2883       internal_error ("MMIX Internal: %s is not a shiftable int", s);
2884     }
2885
2886   for (i = 0; i < 4; i++)
2887   {
2888     /* We know we're through when we find one-bits in the low 16 bits.  */
2889     if (value & 0xffff)
2890     {
2891       fprintf (stream, "#%x", (int) (value & 0xffff));
2892       return;
2893     }
2894
2895     value >>= 16;
2896   }
2897
2898   /* No bits set?  Then it must have been zero.  */
2899   fprintf (stream, "0");
2900 }
2901
2902 /* Output an MMIX condition name corresponding to an operator
2903    and operands:
2904    (comparison_operator [(comparison_operator ...) (const_int 0)])
2905    which means we have to look at *two* operators.
2906
2907    The argument "reversed" refers to reversal of the condition (not the
2908    same as swapping the arguments).  */
2909
2910 static void
2911 mmix_output_condition (stream, x, reversed)
2912      FILE *stream;
2913      rtx x;
2914      int reversed;
2915 {
2916   struct cc_conv
2917   {
2918     RTX_CODE cc;
2919
2920     /* The normal output cc-code.  */
2921     const char *const normal;
2922
2923     /* The reversed cc-code, or NULL if invalid.  */
2924     const char *const reversed;
2925   };
2926
2927   struct cc_type_conv
2928   {
2929     enum machine_mode cc_mode;
2930
2931     /* Terminated with {NIL, NULL, NULL} */
2932     const struct cc_conv *const convs;
2933   };
2934
2935 #undef CCEND
2936 #define CCEND {NIL, NULL, NULL}
2937
2938   static const struct cc_conv cc_fun_convs[]
2939     = {{ORDERED, "Z", "P"},
2940        {UNORDERED, "P", "Z"},
2941        CCEND};
2942   static const struct cc_conv cc_fp_convs[]
2943     = {{GT, "P", NULL},
2944        {LT, "N", NULL},
2945        CCEND};
2946   static const struct cc_conv cc_fpeq_convs[]
2947     = {{NE, "Z", "P"},
2948        {EQ, "P", "Z"},
2949        CCEND};
2950   static const struct cc_conv cc_uns_convs[]
2951     = {{GEU, "NN", "N"},
2952        {GTU, "P", "NP"},
2953        {LEU, "NP", "P"},
2954        {LTU, "N", "NN"},
2955        CCEND};
2956   static const struct cc_conv cc_signed_convs[]
2957     = {{NE, "NZ", "Z"},
2958        {EQ, "Z", "NZ"},
2959        {GE, "NN", "N"},
2960        {GT, "P", "NP"},
2961        {LE, "NP", "P"},
2962        {LT, "N", "NN"},
2963        CCEND};
2964   static const struct cc_conv cc_di_convs[]
2965     = {{NE, "NZ", "Z"},
2966        {EQ, "Z", "NZ"},
2967        {GE, "NN", "N"},
2968        {GT, "P", "NP"},
2969        {LE, "NP", "P"},
2970        {LT, "N", "NN"},
2971        {GTU, "NZ", "Z"},
2972        {LEU, "Z", "NZ"},
2973        CCEND};
2974 #undef CCEND
2975
2976   static const struct cc_type_conv cc_convs[]
2977     = {{CC_FUNmode, cc_fun_convs},
2978        {CC_FPmode, cc_fp_convs},
2979        {CC_FPEQmode, cc_fpeq_convs},
2980        {CC_UNSmode, cc_uns_convs},
2981        {CCmode, cc_signed_convs},
2982        {DImode, cc_di_convs}};
2983
2984   size_t i;
2985   int j;
2986
2987   enum machine_mode mode = GET_MODE (XEXP (x, 0));
2988   RTX_CODE cc = GET_CODE (x);
2989
2990   for (i = 0; i < ARRAY_SIZE (cc_convs); i++)
2991     {
2992       if (mode == cc_convs[i].cc_mode)
2993         {
2994           for (j = 0; cc_convs[i].convs[j].cc != NIL; j++)
2995             if (cc == cc_convs[i].convs[j].cc)
2996               {
2997                 const char *mmix_cc
2998                   = (reversed ? cc_convs[i].convs[j].reversed
2999                      : cc_convs[i].convs[j].normal);
3000
3001                 if (mmix_cc == NULL)
3002                   fatal_insn ("MMIX Internal: Trying to output invalidly\
3003  reversed condition:", x);
3004
3005                 fprintf (stream, "%s", mmix_cc);
3006                 return;
3007               }
3008
3009           fatal_insn ("MMIX Internal: What's the CC of this?", x);
3010         }
3011     }
3012
3013   fatal_insn ("MMIX Internal: What is the CC of this?", x);
3014 }
3015
3016 /* Return the bit-value for a const_int or const_double.  */
3017
3018 static HOST_WIDEST_INT
3019 mmix_intval (x)
3020      rtx x;
3021 {
3022   unsigned HOST_WIDEST_INT retval;
3023
3024   if (GET_CODE (x) == CONST_INT)
3025     return INTVAL (x);
3026
3027   /* We make a little song and dance because converting to long long in
3028      gcc-2.7.2 is broken.  I still want people to be able to use it for
3029      cross-compilation to MMIX.  */
3030   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == VOIDmode)
3031     {
3032       if (sizeof (HOST_WIDE_INT) < sizeof (HOST_WIDEST_INT))
3033         {
3034           retval = (unsigned) CONST_DOUBLE_LOW (x) / 2;
3035           retval *= 2;
3036           retval |= CONST_DOUBLE_LOW (x) & 1;
3037
3038           retval |=
3039             (unsigned HOST_WIDEST_INT) CONST_DOUBLE_HIGH (x)
3040               << (HOST_BITS_PER_LONG);
3041         }
3042       else
3043         retval = CONST_DOUBLE_HIGH (x);
3044
3045       return retval;
3046     }
3047
3048   if (GET_CODE (x) == CONST_DOUBLE)
3049     {
3050       REAL_VALUE_TYPE value;
3051
3052       /* FIXME:  This macro is not in the manual but should be.  */
3053       REAL_VALUE_FROM_CONST_DOUBLE (value, x);
3054
3055       if (GET_MODE (x) == DFmode)
3056         {
3057           long bits[2];
3058
3059           REAL_VALUE_TO_TARGET_DOUBLE (value, bits);
3060
3061           if (sizeof (long) < sizeof (HOST_WIDEST_INT))
3062             {
3063               retval = (unsigned long) bits[1] / 2;
3064               retval *= 2;
3065               retval |= (unsigned long) bits[1] & 1;
3066               retval
3067                 |= (unsigned HOST_WIDEST_INT) bits[0]
3068                   << (sizeof (bits[0]) * 8);
3069             }
3070           else
3071             retval = (unsigned long) bits[1];
3072
3073           return retval;
3074         }
3075       else if (GET_MODE (x) == SFmode)
3076         {
3077           long bits;
3078           REAL_VALUE_TO_TARGET_SINGLE (value, bits);
3079
3080           return (unsigned long) bits;
3081         }
3082     }
3083
3084   fatal_insn ("MMIX Internal: This is not a constant:", x);
3085 }
3086
3087 /*
3088  * Local variables:
3089  * eval: (c-set-style "gnu")
3090  * indent-tabs-mode: t
3091  * End:
3092  */