OSDN Git Service

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