OSDN Git Service

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