OSDN Git Service

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