Debian Codegraph @

gcc/config/mmix/mmix.cc

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