OSDN Git Service

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