PR debug/47590
[official-gcc.git] / gcc / config / picochip / picochip.c
blobf755020b2f6bcf396d24367d5da9c335cf8f08d1
1 /* Subroutines used for code generation on picoChip processors.
2 Copyright (C) 2001, 2008, 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Picochip Ltd. (http://www.picochip.com)
4 Maintained by Daniel Towner (daniel.towner@picochip.com) and
5 Hariharan Sandanagobalane (hariharan@picochip.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not, see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "obstack.h"
36 #include "tree.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "except.h"
40 #include "function.h"
41 #include "output.h"
42 #include "basic-block.h"
43 #include "integrate.h"
44 #include "diagnostic-core.h"
45 #include "ggc.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "params.h"
54 #include "picochip-protos.h"
56 #include "insn-attr.h" /* For DFA state_t. */
57 #include "insn-config.h" /* Required by recog.h */
58 #include "insn-codes.h" /* For CODE_FOR_? */
59 #include "optabs.h" /* For GEN_FCN */
60 #include "basic-block.h" /* UPDATE_LIFE_GLOBAL* for picochip_reorg. */
61 #include "timevar.h" /* For TV_SCHED2, in picochip_reorg. */
62 #include "libfuncs.h" /* For memcpy_libfuncs, etc. */
63 #include "df.h" /* For df_regs_ever_live_df_regs_ever_live_pp, etc. */
66 /* Target AE ISA information. */
67 enum picochip_dfa_type picochip_schedule_type;
69 bool picochip_has_mul_unit = false;
70 bool picochip_has_mac_unit = false;
72 /* targetm hook function prototypes. */
74 void picochip_asm_file_start (void);
75 void picochip_asm_file_end (void);
77 void picochip_init_libfuncs (void);
78 void picochip_reorg (void);
80 int picochip_arg_partial_bytes (CUMULATIVE_ARGS * p_cum,
81 enum machine_mode mode,
82 tree type, bool named);
83 rtx picochip_function_arg (CUMULATIVE_ARGS * p_cum,
84 enum machine_mode mode,
85 const_tree type, bool named);
86 rtx picochip_incoming_function_arg (CUMULATIVE_ARGS * p_cum,
87 enum machine_mode mode,
88 const_tree type, bool named);
89 void picochip_arg_advance (CUMULATIVE_ARGS * p_cum, enum machine_mode mode,
90 const_tree type, bool named);
91 unsigned int picochip_function_arg_boundary (enum machine_mode mode,
92 const_tree type);
94 int picochip_sched_lookahead (void);
95 int picochip_sched_issue_rate (void);
96 int picochip_sched_adjust_cost (rtx insn, rtx link,
97 rtx dep_insn, int cost);
98 int picochip_sched_reorder (FILE * file, int verbose, rtx * ready,
99 int *n_readyp, int clock);
101 void picochip_init_builtins (void);
102 rtx picochip_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
104 bool picochip_rtx_costs (rtx x, int code, int outer_code, int* total, bool speed);
105 bool picochip_return_in_memory(const_tree type,
106 const_tree fntype ATTRIBUTE_UNUSED);
107 bool picochip_legitimate_address_p (enum machine_mode, rtx, bool);
108 rtx picochip_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
109 enum machine_mode mode);
110 int picochip_legitimize_reload_address (rtx *x, enum machine_mode mode,
111 int opnum, int type, int ind_levels);
113 rtx picochip_struct_value_rtx(tree fntype ATTRIBUTE_UNUSED, int incoming ATTRIBUTE_UNUSED);
114 rtx picochip_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
115 bool outgoing ATTRIBUTE_UNUSED);
116 static reg_class_t
117 picochip_secondary_reload (bool in_p,
118 rtx x ATTRIBUTE_UNUSED,
119 reg_class_t cla ATTRIBUTE_UNUSED,
120 enum machine_mode mode,
121 secondary_reload_info *sri);
122 void
123 picochip_asm_named_section (const char *name,
124 unsigned int flags ATTRIBUTE_UNUSED,
125 tree decl ATTRIBUTE_UNUSED);
127 static rtx picochip_static_chain (const_tree, bool);
129 static void picochip_option_override (void);
131 /* Lookup table mapping a register number to the earliest containing
132 class. Used by REGNO_REG_CLASS. */
133 const enum reg_class picochip_regno_reg_class[FIRST_PSEUDO_REGISTER] =
135 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
136 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
137 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
138 GR_REGS, FRAME_REGS, PTR_REGS, CONST_REGS,
139 ACC_REGS, CC_REGS, GR_REGS, GR_REGS
142 /* picoChip register names. */
143 const char *picochip_regnames[] = REGISTER_NAMES;
145 /* Define the maximum number of registers which may be used to pass
146 * parameters to functions. */
147 #define MAX_CALL_PARAMETER_REGS 6
150 /* Target scheduling information. */
152 /* This flag indicates whether the next instruction to be output is a
153 VLIW continuation instruction. It is used to communicate between
154 final_prescan_insn and asm_output_opcode. */
155 static int picochip_vliw_continuation = 0;
157 /* This variable is used to communicate the current instruction
158 between final_prescan_insn and functions such as asm_output_opcode,
159 and picochip_get_vliw_alu_id (which are otherwise unable to determine the
160 current instruction. */
161 static rtx picochip_current_prescan_insn;
163 static bool picochip_is_delay_slot_pending = 0;
165 /* When final_prescan_insn is called, it computes information about
166 the current VLIW packet, and stores it in this structure. When
167 instructions are output, this state is used to make sure that the
168 instructions are output in the correct way (e.g., which ALU to use,
169 whether a macro branch was ever previously a real branch, etc.). */
170 struct vliw_state
172 int contains_pico_alu_insn;
173 int contains_non_cc_alu_insn;
174 int num_alu_insns_so_far;
176 /* Record how many instructions are contained in the packet. */
177 int num_insns_in_packet;
179 /* There was a case for this to be more than 1 */
180 int num_cfi_labels_deferred;
181 char cfi_label_name[2][256]; /* Used to record the name of a CFI label
182 emitted inside a VLIW packet. */
183 char lm_label_name[256]; /* Used to record the name of an LM label. */
186 struct vliw_state picochip_current_vliw_state;
188 /* Save/restore recog_data. */
189 static int picochip_saved_which_alternative;
190 static struct recog_data picochip_saved_recog_data;
192 /* Determine which ALU to use for the instruction in
193 picochip_current_prescan_insn. */
194 static char picochip_get_vliw_alu_id (void);
196 /* Implement TARGET_OPTION_OPTIMIZATION_TABLE. */
197 static const struct default_options picochip_option_optimization_table[] =
199 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
200 { OPT_LEVELS_NONE, 0, NULL, 0 }
203 /* Initialize the GCC target structure. */
205 #undef TARGET_ASM_FUNCTION_PROLOGUE
206 #define TARGET_ASM_FUNCTION_PROLOGUE picochip_function_prologue
208 #undef TARGET_ASM_FUNCTION_EPILOGUE
209 #define TARGET_ASM_FUNCTION_EPILOGUE picochip_function_epilogue
211 #undef TARGET_ASM_INTERNAL_LABEL
212 #define TARGET_ASM_INTERNAL_LABEL picochip_output_internal_label
214 #undef TARGET_ASM_GLOBALIZE_LABEL
215 #define TARGET_ASM_GLOBALIZE_LABEL picochip_output_global
217 #undef TARGET_ASM_BYTE_OP
218 #define TARGET_ASM_BYTE_OP ".initByte "
219 #undef TARGET_ASM_ALIGNED_HI_OP
220 #define TARGET_ASM_ALIGNED_HI_OP ".initWord "
221 #undef TARGET_ASM_UNALIGNED_HI_OP
222 #define TARGET_ASM_UNALIGNED_HI_OP ".unalignedInitWord "
223 #undef TARGET_ASM_ALIGNED_SI_OP
224 #define TARGET_ASM_ALIGNED_SI_OP ".initLong "
225 #undef TARGET_ASM_UNALIGNED_SI_OP
226 #define TARGET_ASM_UNALIGNED_SI_OP ".unalignedInitLong "
228 #undef TARGET_INIT_BUILTINS
229 #define TARGET_INIT_BUILTINS picochip_init_builtins
231 #undef TARGET_EXPAND_BUILTIN
232 #define TARGET_EXPAND_BUILTIN picochip_expand_builtin
234 #undef TARGET_RTX_COSTS
235 #define TARGET_RTX_COSTS picochip_rtx_costs
237 #undef TARGET_SCHED_ISSUE_RATE
238 #define TARGET_SCHED_ISSUE_RATE picochip_sched_issue_rate
240 #undef TARGET_SCHED_REORDER
241 #define TARGET_SCHED_REORDER picochip_sched_reorder
243 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
244 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
245 picochip_sched_lookahead
247 #undef TARGET_SCHED_ADJUST_COST
248 #define TARGET_SCHED_ADJUST_COST picochip_sched_adjust_cost
250 #undef TARGET_ASM_NAMED_SECTION
251 #define TARGET_ASM_NAMED_SECTION picochip_asm_named_section
253 #undef TARGET_HAVE_NAMED_SECTIONS
254 #define TARGET_HAVE_NAMED_SECTIONS 1
256 #undef TARGET_HAVE_SWITCHABLE_BSS_SECTIONS
257 #define TARGET_HAVE_SWITCHABLE_BSS_SECTIONS 1
259 #undef TARGET_INIT_LIBFUNCS
260 #define TARGET_INIT_LIBFUNCS picochip_init_libfuncs
262 #undef TARGET_ASM_FILE_START
263 #define TARGET_ASM_FILE_START picochip_asm_file_start
265 #undef TARGET_ASM_FILE_END
266 #define TARGET_ASM_FILE_END picochip_asm_file_end
268 #undef TARGET_MACHINE_DEPENDENT_REORG
269 #define TARGET_MACHINE_DEPENDENT_REORG picochip_reorg
271 #undef TARGET_ARG_PARTIAL_BYTES
272 #define TARGET_ARG_PARTIAL_BYTES picochip_arg_partial_bytes
274 #undef TARGET_FUNCTION_ARG
275 #define TARGET_FUNCTION_ARG picochip_function_arg
277 #undef TARGET_FUNCTION_INCOMING_ARG
278 #define TARGET_FUNCTION_INCOMING_ARG picochip_incoming_function_arg
280 #undef TARGET_FUNCTION_ARG_ADVANCE
281 #define TARGET_FUNCTION_ARG_ADVANCE picochip_arg_advance
283 #undef TARGET_FUNCTION_ARG_BOUNDARY
284 #define TARGET_FUNCTION_ARG_BOUNDARY picochip_function_arg_boundary
286 #undef TARGET_PROMOTE_FUNCTION_MODE
287 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
288 #undef TARGET_PROMOTE_PROTOTYPES
289 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
291 /* Target support for Anchored Addresses optimization */
292 #undef TARGET_MIN_ANCHOR_OFFSET
293 #define TARGET_MIN_ANCHOR_OFFSET 0
294 #undef TARGET_MAX_ANCHOR_OFFSET
295 #define TARGET_MAX_ANCHOR_OFFSET 7
296 #undef TARGET_ASM_OUTPUT_ANCHOR
297 #define TARGET_ASM_OUTPUT_ANCHOR picochip_asm_output_anchor
299 #undef TARGET_FUNCTION_VALUE
300 #define TARGET_FUNCTION_VALUE picochip_function_value
302 #undef TARGET_LIBGCC_CMP_RETURN_MODE
303 #define TARGET_LIBGCC_CMP_RETURN_MODE picochip_libgcc_cmp_return_mode
306 #undef TARGET_LEGITIMATE_ADDRESS_P
307 #define TARGET_LEGITIMATE_ADDRESS_P picochip_legitimate_address_p
309 #undef TARGET_LEGITIMIZE_ADDRESS
310 #define TARGET_LEGITIMIZE_ADDRESS picochip_legitimize_address
312 /* Loading and storing QImode values to and from memory
313 usually requires a scratch register. */
314 #undef TARGET_SECONDARY_RELOAD
315 #define TARGET_SECONDARY_RELOAD picochip_secondary_reload
316 #undef DONT_USE_BUILTIN_SETJMP
317 #define DONT_USE_BUILTIN_SETJMP 1
319 /* How Large Values are Returned */
321 #undef TARGET_RETURN_IN_MEMORY
322 #define TARGET_RETURN_IN_MEMORY picochip_return_in_memory
324 #undef TARGET_STATIC_CHAIN
325 #define TARGET_STATIC_CHAIN picochip_static_chain
327 #undef TARGET_OPTION_OVERRIDE
328 #define TARGET_OPTION_OVERRIDE picochip_option_override
330 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
331 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE picochip_option_override
333 #undef TARGET_OPTION_OPTIMIZATION_TABLE
334 #define TARGET_OPTION_OPTIMIZATION_TABLE picochip_option_optimization_table
336 #undef TARGET_EXCEPT_UNWIND_INFO
337 #define TARGET_EXCEPT_UNWIND_INFO sjlj_except_unwind_info
339 /* The 2nd scheduling pass option is switched off, and a machine
340 dependent reorganisation ensures that it is run later on, after the
341 second jump optimisation. */
342 #undef TARGET_DELAY_SCHED2
343 #define TARGET_DELAY_SCHED2 true
345 /* Variable tracking should be run after all optimizations which
346 change order of insns. It also needs a valid CFG. */
347 #undef TARGET_DELAY_VARTRACK
348 #define TARGET_DELAY_VARTRACK true
350 struct gcc_target targetm = TARGET_INITIALIZER;
353 /* Only return a value in memory if it is greater than 4 bytes.
354 int_size_in_bytes returns -1 for variable size objects, which go in
355 memory always. The cast to unsigned makes -1 > 8. */
357 bool
358 picochip_return_in_memory(const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
360 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 4);
363 /* Allow some options to be overriden. */
365 static void
366 picochip_option_override (void)
368 /* If we are optimizing for stack, dont let inliner to inline functions
369 that could potentially increase stack size.*/
370 if (flag_conserve_stack)
372 maybe_set_param_value (PARAM_LARGE_STACK_FRAME, 0,
373 global_options.x_param_values,
374 global_options_set.x_param_values);
375 maybe_set_param_value (PARAM_STACK_FRAME_GROWTH, 0,
376 global_options.x_param_values,
377 global_options_set.x_param_values);
380 /* Turn off the elimination of unused types. The elaborator
381 generates various interesting types to represent constants,
382 generics, and so on, and it is useful to retain this information
383 in the debug output. The increased size of the debug information
384 is not really an issue for us. */
385 flag_eliminate_unused_debug_types = 0;
387 /* Even if the user specifies a -fno-omit-frame-pointer on the
388 command line, we still want to go ahead and omit frame pointer
389 usages, since we dont really have a frame pointer register.
390 So, all accesses to FP need to be converted to accesses off
391 stack pointer.*/
392 flag_omit_frame_pointer = 1;
394 /* Turning on anchored addresses by default. This is an optimization
395 that could decrease the code size by placing anchors in data and
396 accessing offsets from the anchor for file local data variables.*/
397 if (optimize >= 1)
398 flag_section_anchors = 1;
400 /* The second scheduling pass runs within picochip_reorg, to avoid
401 having the second jump optimisation trash the instruction modes
402 (e.g., instructions are changed to TImode to mark the beginning
403 of cycles). Two types of DFA scheduling are possible: space and
404 speed. In both cases, instructions are reordered to avoid stalls
405 (e.g., memory loads stall for one cycle). Speed scheduling will
406 also enable VLIW instruction packing. VLIW instructions use more
407 code space, so VLIW scheduling is disabled when scheduling for
408 size. */
409 if (flag_schedule_insns_after_reload)
411 if (optimize_size)
412 picochip_schedule_type = DFA_TYPE_SPACE;
413 else
415 picochip_schedule_type = DFA_TYPE_SPEED;
416 flag_delayed_branch = 0;
419 else
420 picochip_schedule_type = DFA_TYPE_NONE;
422 /* Ensure that the debug level is always at least -g2. The flow
423 analyser works at its best if it always has debug
424 information. DWARF is non-intrusive, so it makes no difference to
425 code quality if debug is always enabled. */
426 if (debug_info_level < DINFO_LEVEL_NORMAL)
428 debug_info_level = DINFO_LEVEL_NORMAL;
429 write_symbols = DWARF2_DEBUG;
432 /* Options of the form -mae=mac, and so on will be substituted by
433 the compiler driver for the appropriate byte access and multiply
434 unit ISA options. Any unrecognised AE types will end up being
435 passed to the compiler, which should reject them as invalid. */
436 if (picochip_ae_type_string != NULL)
437 error ("invalid AE type specified (%s)", picochip_ae_type_string);
439 /* Override any specific capabilities of the instruction set. These
440 take precedence over any capabilities inferred from the AE type,
441 regardless of where the options appear on the command line. */
442 if (picochip_mul_type_string == NULL)
444 /* Default to MEM-type multiply, for historical compatibility. */
445 picochip_has_mac_unit = false;
446 picochip_has_mul_unit = true;
448 else
450 picochip_has_mac_unit = false;
451 picochip_has_mul_unit = false;
453 if (strcmp (picochip_mul_type_string, "mul") == 0)
454 picochip_has_mul_unit = true;
455 else if (strcmp (picochip_mul_type_string, "mac") == 0)
456 picochip_has_mac_unit = true;
457 else if (strcmp (picochip_mul_type_string, "none") == 0)
458 { /* Do nothing. Unit types already set to false. */ }
459 else
460 error ("invalid mul type specified (%s) - expected mac, mul or none",
461 picochip_mul_type_string);
466 /* Initialise the library functions to handle arithmetic on some of
467 the larger modes. */
468 void
469 picochip_init_libfuncs (void)
471 /* 64-bit shifts */
472 set_optab_libfunc (ashr_optab, DImode, "__ashrdi3");
473 set_optab_libfunc (ashl_optab, DImode, "__ashldi3");
474 set_optab_libfunc (lshr_optab, DImode, "__lshrdi3");
476 /* 64-bit signed multiplication. */
477 set_optab_libfunc (smul_optab, DImode, "__muldi3");
479 /* Signed division */
480 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
481 set_optab_libfunc (sdiv_optab, DImode, "__divdi3");
483 /* Signed modulus */
484 set_optab_libfunc (smod_optab, HImode, "__modhi3");
485 set_optab_libfunc (smod_optab, DImode, "__moddi3");
487 /* 32-bit count leading Zeros*/
488 set_optab_libfunc (clz_optab, SImode, "_clzsi2");
490 /* 64-bit comparison */
491 set_optab_libfunc (ucmp_optab, DImode, "__ucmpdi2");
492 set_optab_libfunc (cmp_optab, DImode, "__cmpdi2");
494 /* 64-bit addition and subtraction*/
495 set_optab_libfunc (add_optab, DImode, "_adddi3");
496 set_optab_libfunc (sub_optab, DImode, "_subdi3");
499 /* Memcpy function */
501 picochip_expand_movmemhi (rtx *operands)
503 rtx src_addr_reg, dst_addr_reg, count_reg, src_mem, dst_mem, tmp_reg;
504 rtx start_label;
505 int align, size;
506 src_addr_reg = gen_reg_rtx(HImode);
507 dst_addr_reg = gen_reg_rtx(HImode);
508 count_reg = gen_reg_rtx(HImode);
509 emit_insn (gen_movhi (count_reg, operands[2]));
510 emit_insn (gen_movqi (src_addr_reg, XEXP(operands[1], 0)));
511 emit_insn (gen_movqi (dst_addr_reg, XEXP(operands[0], 0)));
512 gcc_assert (GET_CODE(count_reg) == REG);
513 start_label = gen_label_rtx ();
514 emit_label (start_label);
516 /* We can specialise the code for different alignments */
517 align = INTVAL(operands[3]);
518 size = INTVAL(operands[2]);
519 gcc_assert(align >= 0 && size >= 0);
520 if (size != 0)
522 if (size % 4 == 0 && align % 4 == 0)
524 src_mem = gen_rtx_MEM(SImode, src_addr_reg);
525 dst_mem = gen_rtx_MEM(SImode, dst_addr_reg);
526 tmp_reg = gen_reg_rtx(SImode);
527 emit_insn (gen_movsi (tmp_reg, src_mem));
528 emit_insn (gen_movsi (dst_mem, tmp_reg));
529 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, GEN_INT(4)));
530 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, GEN_INT(4)));
531 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-4)));
532 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
533 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
535 else if (size % 2 == 0 && align % 2 == 0)
537 src_mem = gen_rtx_MEM(HImode, src_addr_reg);
538 dst_mem = gen_rtx_MEM(HImode, dst_addr_reg);
539 tmp_reg = gen_reg_rtx(HImode);
540 emit_insn (gen_movhi (tmp_reg, src_mem));
541 emit_insn (gen_movhi (dst_mem, tmp_reg));
542 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, const2_rtx));
543 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, const2_rtx));
544 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-2)));
545 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
546 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
548 else
550 src_mem = gen_rtx_MEM(QImode, src_addr_reg);
551 dst_mem = gen_rtx_MEM(QImode, dst_addr_reg);
552 tmp_reg = gen_reg_rtx(QImode);
553 emit_insn (gen_movqi (tmp_reg, src_mem));
554 emit_insn (gen_movqi (dst_mem, tmp_reg));
555 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, const1_rtx));
556 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, const1_rtx));
557 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-1)));
558 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
559 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
562 return 1;
566 /* Return the register class for letter C. */
567 enum reg_class
568 picochip_reg_class_from_letter (unsigned c)
570 switch (c)
572 case 'k':
573 return FRAME_REGS;
574 case 'f':
575 return PTR_REGS;
576 case 't':
577 return TWIN_REGS;
578 case 'r':
579 return GR_REGS;
580 default:
581 return NO_REGS;
585 static const int
586 pico_leaf_reg_alloc_order[] = LEAF_REG_ALLOC_ORDER;
587 static const int
588 pico_nonleaf_reg_alloc_order[] = REG_ALLOC_ORDER;
590 void
591 picochip_order_regs_for_local_alloc (void)
593 /* We change the order for leaf functions alone. We put r12 at
594 the end since using it will prevent us to combine stw/ldws to
595 stl/ldl and it gives no benefit. In non-leaf functions, we
596 would anyway saveup/restore r12, so it makes sense to use it.*/
598 if (leaf_function_p())
600 memcpy ((char *)reg_alloc_order, (const char *) pico_leaf_reg_alloc_order,
601 FIRST_PSEUDO_REGISTER * sizeof (int));
603 else
605 memcpy ((char *)reg_alloc_order, (const char *) pico_nonleaf_reg_alloc_order,
606 FIRST_PSEUDO_REGISTER * sizeof (int));
610 /* Check that VALUE (an INT_CST) is ok as a constant of type C. */
612 picochip_const_ok_for_letter_p (unsigned HOST_WIDE_INT value, unsigned c)
615 switch (c)
617 case 'I': /* 4 bits signed. */
618 return value + 8 < 16;
619 case 'J': /* 4 bits unsigned. */
620 return value < 16;
621 case 'K': /* 8 bits signed. */
622 return value + 128 < 256;
623 case 'M': /* 4-bit magnitude. */
624 return abs (value) < 16;
625 case 'N': /* 10 bits signed. */
626 return value + 512 > 1024;
627 case 'O': /* 16 bits signed. */
628 return value + 32768 < 65536;
629 default: /* Unknown letter. */
630 return 0;
634 /* Stack utility functions. */
636 picochip_return_addr_rtx(int count, rtx frameaddr ATTRIBUTE_UNUSED)
638 if (count==0)
639 return gen_rtx_REG (Pmode, LINK_REGNUM);
640 else
641 return NULL_RTX;
645 /* Emit a set of parallel register expressions used to store
646 blockmode values to pass to functions. */
647 static rtx
648 picochip_emit_register_parallel (int size_in_units, int offset)
650 int num_regs = 0;
651 rtx result;
652 rtx vector[MAX_CALL_PARAMETER_REGS];
653 int base_reg = 0;
654 int i = 0;
656 /* Compute the base register, and number of required registers. */
657 base_reg = offset / 2;
658 num_regs = size_in_units / 2;
659 if (size_in_units % 2 == 1)
660 num_regs++;
662 /* Emit a register for each part of the block mode value to be
663 passed in a register. */
664 for (i = 0; i < num_regs; i++)
665 vector[i] = gen_rtx_EXPR_LIST (VOIDmode,
666 gen_rtx_REG (HImode, base_reg + i),
667 GEN_INT (i * 2));
668 result = gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (num_regs, vector));
670 return result;
674 /* Emit an instruction to allocate a suitable amount of space on the
675 stack, by decrementing the stack pointer. */
676 static void
677 picochip_emit_stack_allocate (int adjustment)
679 rtx insn;
680 rtx stack_pointer_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
682 /* Use an addition of a negative value. */
683 insn = emit_insn (gen_addhi3 (stack_pointer_reg, stack_pointer_reg,
684 GEN_INT (-adjustment)));
686 /* Make the instruction frame related. Also add an expression note,
687 so that the correct Dwarf information is generated (see documention
688 for RTX_FRAME_RELATED_P for more details). */
689 RTX_FRAME_RELATED_P (insn) = 1;
690 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
691 gen_rtx_SET (VOIDmode, stack_pointer_reg,
692 gen_rtx_PLUS (Pmode, stack_pointer_reg,
693 GEN_INT (-adjustment))));
697 /* Emit an instruction to save a register of the given mode. The
698 offset at which to save the register is given relative to the stack
699 pointer. */
700 static void
701 picochip_emit_save_register (rtx reg, int offset)
703 rtx stack_pointer, address, mem, insn;
705 stack_pointer = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
707 address = gen_rtx_PLUS (Pmode, stack_pointer, GEN_INT (offset));
709 mem = gen_rtx_MEM (GET_MODE (reg), address);
711 insn = emit_move_insn (mem, reg);
712 RTX_FRAME_RELATED_P (insn) = 1;
714 /* For modes other than HImode, create a note explaining that
715 multiple registers have been saved. This allows the correct DWARF
716 call frame information to be generated. */
717 switch (GET_MODE (reg))
719 case HImode:
720 /* The RTL is sufficient to explain HImode register saves. */
721 break;
723 case SImode:
724 /* SImode must be broken down into parallel HImode register saves. */
726 rtvec p;
727 p = rtvec_alloc (2);
729 RTVEC_ELT (p, 0) =
730 gen_rtx_SET (HImode,
731 gen_rtx_MEM (HImode,
732 gen_rtx_PLUS (Pmode, stack_pointer,
733 GEN_INT (offset))),
734 gen_rtx_REG (HImode, REGNO (reg)));
735 RTX_FRAME_RELATED_P (RTVEC_ELT (p, 0)) = 1;
737 RTVEC_ELT (p, 1) =
738 gen_rtx_SET (HImode, gen_rtx_MEM (HImode,
739 gen_rtx_PLUS (Pmode,
740 stack_pointer,
741 GEN_INT (offset +
742 2))),
743 gen_rtx_REG (HImode, REGNO (reg) + 1));
744 RTX_FRAME_RELATED_P (RTVEC_ELT (p, 1)) = 1;
746 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
747 gen_rtx_PARALLEL (VOIDmode, p));
750 break;
752 default:
753 internal_error
754 ("unexpected mode %s encountered in picochip_emit_save_register",
755 GET_MODE_NAME (GET_MODE (reg)));
760 /* Emit an instruction to restore a register of the given mode. The
761 offset from which to restore the register is given relative to the
762 stack pointer. */
763 static void
764 picochip_emit_restore_register (rtx reg, int offset)
766 rtx stack_pointer, address, mem;
768 stack_pointer = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
770 address = gen_rtx_PLUS (Pmode, stack_pointer, GEN_INT (offset));
772 mem = gen_rtx_MEM (GET_MODE (reg), address);
774 emit_move_insn (reg, mem);
778 /* Check that the given byte offset is aligned to the given number of
779 bits. */
780 static int
781 picochip_is_aligned (int byte_offset, int bit_alignment)
783 int byte_alignment = bit_alignment / BITS_PER_UNIT;
784 return (byte_offset % byte_alignment) == 0;
787 /*****************************************************************************
788 * Stack layout.
790 * The following section contains code which controls how the stack is
791 * laid out.
793 * The stack is laid out as follows (high addresses first):
795 * Incoming arguments
796 * Pretend arguments (ARG PTR)
797 * Special registers
798 * General registers
799 * Frame (FP)
800 * Outgoing arguments (SP)
802 * The (constant) offsets of the different areas must be calculated
803 * relative to the stack area immediately below, and aligned
804 * appropriately. For example, the frame offset is computed by
805 * determining the offset of the special register area, adding the
806 * size of the special register area, and then aligning the resulting
807 * offset correctly. In turn, the special register offset is computed
808 * from the general register offset, and so on. This enables the
809 * different offsets to change size and alignment, without requiring
810 * the code for other offset calculations to be rewritten.
812 * The argument pointer, and the frame pointer are eliminated wherever
813 * possible, by replacing them with a constant offset from the stack
814 * pointer. In the rare cases where constant offsets from the stack
815 * pointer cannot be computed, another register will be allocated to
816 * serve as the argument pointer, or the frame pointer.
818 * The save registers are stored at small offsets from the caller, to
819 * enable the more efficient SP-based ISA instructions to be used.
821 ****************************************************************************/
823 /* Compute the size of an argument in units. */
824 static int
825 picochip_compute_arg_size (const_tree type, enum machine_mode mode)
827 int type_size_in_units = 0;
829 if (type)
830 type_size_in_units = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
831 else
832 type_size_in_units = GET_MODE_SIZE (mode);
834 return type_size_in_units;
838 /* Determine where the next outgoing arg should be placed. */
840 picochip_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
841 const_tree type, bool named ATTRIBUTE_UNUSED)
843 int reg = 0;
844 int type_align_in_units = 0;
845 int type_size_in_units;
846 int new_offset = 0;
847 int offset_overflow = 0;
849 /* VOIDmode is passed when computing the second argument to a `call'
850 pattern. This can be ignored. */
851 if (mode == VOIDmode)
852 return 0;
854 /* Compute the alignment and size of the parameter. */
855 type_align_in_units =
856 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
857 type_size_in_units = picochip_compute_arg_size (type, mode);
859 /* Compute the correct offset (i.e., ensure that the offset meets
860 the alignment requirements). */
861 offset_overflow = *cum % type_align_in_units;
862 if (offset_overflow == 0)
863 new_offset = *cum;
864 else
865 new_offset = (*cum - offset_overflow) + type_align_in_units;
867 if (TARGET_DEBUG)
869 printf ("Function arg:\n");
870 printf (" Type valid: %s\n", (type ? "yes" : "no"));
871 printf (" Cumulative Value: %d\n", *cum);
872 printf (" Mode: %s\n", GET_MODE_NAME (mode));
873 printf (" Type size: %i units\n", type_size_in_units);
874 printf (" Alignment: %i units\n", type_align_in_units);
875 printf (" New offset: %i\n", new_offset);
876 printf ("\n");
879 /* If the new offset is outside the register space, return. */
880 if (new_offset >= MAX_CALL_PARAMETER_REGS * 2)
881 return 0;
883 /* If the end of the argument is outside the register space, then
884 the argument must overlap the register space. Return the first
885 available register. */
886 if ((new_offset + type_size_in_units) > (MAX_CALL_PARAMETER_REGS * 2))
887 return gen_rtx_REG (HImode, new_offset / 2);
889 /* Create a register of the required mode to hold the parameter. */
890 reg = new_offset / 2;
891 switch (mode)
893 case QImode:
894 case HImode:
895 case SImode:
896 case SFmode:
897 case DImode:
898 case DFmode:
899 case SDmode:
900 case DDmode:
901 case CHImode:
902 case CSImode:
903 case SCmode:
904 case CQImode:
905 return gen_rtx_REG (mode, reg);
907 case BLKmode:
909 /* Empty blockmode values can be passed as arguments (e.g.,
910 * empty structs). These require no registers
911 * whatsoever. Non-empty blockmode values are passed in a set
912 * of parallel registers. */
913 if (type_size_in_units == 0)
914 return 0;
915 else
916 return picochip_emit_register_parallel (type_size_in_units, new_offset);
919 default:
920 warning
921 (0, "defaulting to stack for %s register creation",
922 GET_MODE_NAME (mode));
923 break;
926 return 0;
930 /* Determine where the next incoming function argument will
931 appear. Normally, this works in exactly the same way as
932 picochip_function_arg, except when the function in question is a
933 varadic function. In this case, the incoming arguments all appear
934 to be passed on the stack (actually, some of the arguments are
935 passed in registers, which are then pushed onto the stack by the
936 function prologue). */
938 picochip_incoming_function_arg (CUMULATIVE_ARGS *cum,
939 enum machine_mode mode,
940 const_tree type, bool named)
943 if (cfun->stdarg)
944 return 0;
945 else
946 return picochip_function_arg (cum, mode, type, named);
950 /* Gives the alignment boundary, in bits, of an argument with the
951 specified mode. */
952 unsigned int
953 picochip_function_arg_boundary (enum machine_mode mode,
954 const_tree type ATTRIBUTE_UNUSED)
956 int align;
958 if (mode == BLKmode)
959 align = STACK_BOUNDARY;
960 else
961 align = GET_MODE_ALIGNMENT (mode);
963 if (align < PARM_BOUNDARY)
964 align = PARM_BOUNDARY;
966 return align;
970 /* Compute partial registers. */
972 picochip_arg_partial_bytes (CUMULATIVE_ARGS * p_cum, enum machine_mode mode,
973 tree type, bool named ATTRIBUTE_UNUSED)
975 int type_align_in_units = 0;
976 int type_size_in_units;
977 int new_offset = 0;
978 int offset_overflow = 0;
980 unsigned cum = *((unsigned *) p_cum);
982 /* VOIDmode is passed when computing the second argument to a `call'
983 pattern. This can be ignored. */
984 if (mode == VOIDmode)
985 return 0;
987 /* Compute the alignment and size of the parameter. */
988 type_align_in_units =
989 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
990 type_size_in_units = picochip_compute_arg_size (type, mode);
992 /* Compute the correct offset (i.e., ensure that the offset meets
993 the alignment requirements). */
994 offset_overflow = cum % type_align_in_units;
995 if (offset_overflow == 0)
996 new_offset = cum;
997 else
998 new_offset = (cum - offset_overflow) + type_align_in_units;
1000 if (TARGET_DEBUG)
1002 printf ("Partial function arg nregs:\n");
1003 printf (" Type valid: %s\n", (type ? "yes" : "no"));
1004 printf (" Cumulative Value: %d\n", cum);
1005 printf (" Mode: %s\n", GET_MODE_NAME (mode));
1006 printf (" Type size: %i units\n", type_size_in_units);
1007 printf (" Alignment: %i units\n", type_align_in_units);
1008 printf (" New offset: %i\n", new_offset);
1009 printf ("\n");
1012 /* If the new offset is outside the register space, return. */
1013 if (new_offset >= (MAX_CALL_PARAMETER_REGS * 2))
1014 return 0;
1016 /* If the end of the argument is outside the register space, then
1017 the argument must overlap the register space. Return the number
1018 of bytes which are passed in registers. */
1019 if ((new_offset + type_size_in_units) > (MAX_CALL_PARAMETER_REGS * 2))
1020 return ((MAX_CALL_PARAMETER_REGS * 2) - new_offset);
1022 return 0;
1026 /* Advance the cumulative args counter CUM. */
1027 void
1028 picochip_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1029 const_tree type, bool named ATTRIBUTE_UNUSED)
1031 int type_align_in_units = 0;
1032 int type_size_in_units;
1033 int new_offset = 0;
1034 int offset_overflow = 0;
1036 /* VOIDmode is passed when computing the second argument to a `call'
1037 pattern. This can be ignored. */
1038 if (mode == VOIDmode)
1039 return;
1041 /* Compute the alignment and size of the parameter. */
1042 type_align_in_units =
1043 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
1044 type_size_in_units = picochip_compute_arg_size (type, mode);
1046 /* Compute the correct offset (i.e., ensure that the offset meets
1047 the alignment requirements). */
1048 offset_overflow = *cum % type_align_in_units;
1049 if (offset_overflow == 0)
1050 new_offset = *cum;
1051 else
1052 new_offset = (*cum - offset_overflow) + type_align_in_units;
1054 /* Advance past the last argument. */
1055 new_offset += type_size_in_units;
1057 *cum = new_offset;
1060 /* Determine whether a register needs saving/restoring. It does if it
1061 is live in a function, and isn't a call-used register. */
1062 static int
1063 picochip_reg_needs_saving (int reg_num)
1065 return df_regs_ever_live_p(reg_num) && !call_used_regs[reg_num];
1068 /* Compute and return offset of the main frame. */
1069 static int
1070 picochip_frame_byte_offset (void)
1072 gcc_assert(picochip_is_aligned
1073 (crtl->outgoing_args_size, BITS_PER_WORD));
1075 return crtl->outgoing_args_size;
1078 /* Return the size of the main frame. */
1079 static int
1080 picochip_frame_size_in_bytes (void)
1082 int frame_size = get_frame_size();
1083 int stack_align = STACK_BOUNDARY/BITS_PER_UNIT;
1084 if (!picochip_is_aligned (frame_size, STACK_BOUNDARY))
1085 frame_size = frame_size + (stack_align - frame_size%stack_align);
1086 gcc_assert(picochip_is_aligned (frame_size, STACK_BOUNDARY));
1087 return frame_size;
1090 /* Compute and return the size (in bytes) of the register save/restore
1091 area for the current function. This only includes the general
1092 purpose registers - the special purpose stack pointer and link
1093 registers are not included in this area. */
1094 static int
1095 picochip_save_area_size_in_bytes (void)
1097 int num_regs_to_save = 0;
1098 int i = 0;
1100 /* Read through all the registers, determining which need to be saved. */
1101 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1103 if (picochip_reg_needs_saving (i))
1104 num_regs_to_save += 1;
1107 return num_regs_to_save * UNITS_PER_WORD;
1111 /* Compute and return offset of the save area base. */
1112 static int
1113 picochip_save_area_byte_offset (void)
1115 int base_offset = (picochip_frame_byte_offset () +
1116 picochip_frame_size_in_bytes ());
1118 gcc_assert(picochip_is_aligned (base_offset, BITS_PER_WORD));
1120 return base_offset;
1124 /* Compute and return offset of the special register save area. This
1125 area can be found immediately above the normal save area. It must
1126 be aligned, to allow the registers to be saved and restored as a
1127 pair. */
1128 static int
1129 picochip_special_save_area_byte_offset (void)
1131 int byte_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
1132 int offset = (picochip_save_area_byte_offset () +
1133 picochip_save_area_size_in_bytes ());
1135 if ((offset % byte_alignment) != 0)
1136 offset = ((offset / byte_alignment) + 1) * byte_alignment;
1138 return offset;
1142 /* Determine whether the LNK/SP register save/restores can be eliminated. */
1143 static int
1144 picochip_can_eliminate_link_sp_save (void)
1146 /* This deserves some reasoning. The df_regs_ever_live_p call keeps
1147 changing during optimizations phases. So, this function returns different
1148 values when called from initial_elimination_offset and then again when it
1149 is called from prologue/epilogue generation. This means that argument
1150 accesses become wrong. This wouldnt happen only if we were not using the
1151 stack at all. The following conditions ensures that.*/
1153 return (current_function_is_leaf &&
1154 !df_regs_ever_live_p(LINK_REGNUM) &&
1155 !df_regs_ever_live_p(STACK_POINTER_REGNUM) &&
1156 (picochip_special_save_area_byte_offset() == 0) &&
1157 (crtl->args.size == 0) &&
1158 (crtl->args.pretend_args_size == 0));
1161 /* Compute the size of the special reg save area (SP and LNK). If the
1162 SP/LNK registers don't need to be saved, this area can shrink to
1163 nothing. */
1164 static int
1165 picochip_special_save_area_size_in_bytes (void)
1169 if (picochip_can_eliminate_link_sp_save ())
1170 return 0;
1171 else
1172 return 2 * UNITS_PER_WORD;
1175 /* Return the number of pretend arguments. If this function is
1176 varadic, all the incoming arguments are effectively passed on the
1177 stack. If this function has real pretend arguments (caused by a
1178 value being passed partially on the stack and partially in
1179 registers), then return the number of registers used. */
1180 static int
1181 picochip_pretend_arg_area_size (void)
1184 if (crtl->args.pretend_args_size != 0)
1186 gcc_assert(crtl->args.pretend_args_size % 4 == 0);
1188 return crtl->args.pretend_args_size;
1190 else if (cfun->stdarg)
1191 return 12;
1192 else
1193 return 0;
1197 /* Compute and return the offset of the pretend arguments. The pretend
1198 arguments are contiguous with the incoming arguments, and must be
1199 correctly aligned. */
1200 static int
1201 picochip_pretend_arg_area_byte_offset (void)
1203 int base_offset = 0;
1205 base_offset = (picochip_special_save_area_byte_offset () +
1206 picochip_special_save_area_size_in_bytes ());
1208 gcc_assert(picochip_is_aligned (base_offset, STACK_BOUNDARY));
1209 gcc_assert(picochip_is_aligned
1210 (base_offset + picochip_pretend_arg_area_size (), STACK_BOUNDARY));
1212 return base_offset;
1216 /* Compute and return the offset of the incoming arguments. If a
1217 static chain is in use, this will be passed just before the other
1218 arguments. This means that the pretend argument mechanism, used in
1219 variadic functions, doesn't work properly. Thus, static chains work
1220 on their own, as do variadic functions, but not the combination of
1221 the two. This isn't really a problem. */
1222 static int
1223 picochip_arg_area_byte_offset (void)
1225 int base_offset = (picochip_pretend_arg_area_byte_offset () +
1226 picochip_pretend_arg_area_size ());
1228 /* Add an extra 4 bytes - only an extra 16-bits are required, but
1229 the alignment on a 32-bit boundary must be maintained. */
1230 if (cfun->static_chain_decl != NULL)
1232 gcc_assert (!cfun->stdarg);
1233 base_offset += 4;
1236 gcc_assert(picochip_is_aligned (base_offset, STACK_BOUNDARY));
1238 return base_offset;
1243 picochip_regno_nregs (int regno ATTRIBUTE_UNUSED, int mode)
1246 /* Special case - only one register needed. */
1247 if (GET_MODE_CLASS (mode) == MODE_CC)
1248 return 1;
1250 /* We actually do not allocate acc0 ever. But, it seems like we need to
1251 make it look like a allocatable register for the dataflow checks to work
1252 properly. Note that hard_regno_mode_ok will always return 0 for acc0*/
1254 if (regno == 16)
1255 return 1;
1257 /* General case - compute how much space in terms of units. */
1258 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1263 picochip_class_max_nregs (int reg_class, int mode)
1265 int size = ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1267 if (reg_class == ACC_REGS)
1268 return 1;
1270 if (GET_MODE_CLASS (mode) == MODE_CC)
1271 return 1;
1272 else
1273 return size;
1277 /* Eliminate a register that addresses the stack (e.g., frame pointer,
1278 argument pointer) by replacing it with a constant offset from the
1279 main stack register. */
1281 initial_elimination_offset (int from, int to)
1283 int offset_from_sp = 0;
1285 if (FRAME_POINTER_REGNUM == from && STACK_POINTER_REGNUM == to)
1286 offset_from_sp = picochip_frame_byte_offset ();
1287 else if (ARG_POINTER_REGNUM == from && STACK_POINTER_REGNUM == to)
1288 offset_from_sp = picochip_pretend_arg_area_byte_offset ();
1289 else
1290 gcc_unreachable();
1292 return offset_from_sp;
1296 /* Compute and return the size of the incoming argument area. */
1297 static int
1298 picochip_arg_area_size_in_bytes (void)
1300 return crtl->args.size;
1303 /* Determine whether the given register is valid. When the strict mode
1304 is used, only hard registers are valid, otherwise any register is
1305 valid. */
1306 static int
1307 picochip_legitimate_address_register (rtx x, unsigned strict)
1310 /* Sanity check - non-registers shouldn't make it here, but... */
1311 if (REG != GET_CODE (x))
1312 return 0;
1314 if (strict)
1315 return REGNO (x) < FIRST_NONHARD_REGISTER;
1316 else
1317 return 1;
1321 /* Determine whether the given constant is in the range required for
1322 the given base register. */
1323 static int
1324 picochip_const_ok_for_base (enum machine_mode mode, int regno, int offset)
1326 HOST_WIDE_INT corrected_offset;
1328 if (GET_MODE_SIZE (mode) != 0)
1330 if (GET_MODE_SIZE(mode) <= 4)
1332 /* We used to allow incorrect offsets if strict is 0. But, this would
1333 then rely on reload doing the right thing. We have had problems
1334 there before, and on > 4.3 compiler, there are no benefits. */
1335 if (offset % GET_MODE_SIZE (mode) != 0)
1336 return 0;
1337 corrected_offset = offset / GET_MODE_SIZE (mode);
1339 else
1341 if (offset % 4 != 0)
1342 return 0;
1343 corrected_offset = offset / 4;
1346 else
1348 /* Default to the byte offset as supplied. */
1349 corrected_offset = offset;
1352 /* The offset from the base register can be different depending upon
1353 the base register. The stack/frame/argument pointer offsets can
1354 all be greater than a simple register-based offset. Note that the
1355 frame/argument pointer registers are actually eliminations of the
1356 stack pointer, so a value which is valid for an offset to, for
1357 example, the frame pointer, might be invalid for the stack
1358 pointer once the elimination has occurred. However, there is no
1359 need to handle this special case here, as the stack offset is
1360 always checked after elimination anyway, and the generated code
1361 seems to have identical performance. */
1362 if (regno == STACK_POINTER_REGNUM ||
1363 regno == FRAME_POINTER_REGNUM || regno == ARG_POINTER_REGNUM)
1364 return picochip_const_ok_for_letter_p (corrected_offset, 'K');
1365 else
1366 return picochip_const_ok_for_letter_p (corrected_offset, 'J');
1370 /* Determine whether a given rtx is a legitimate address for machine_mode
1371 MODE. STRICT is non-zero if we're being strict - any pseudo that
1372 is not a hard register must be a memory reference. */
1373 bool
1374 picochip_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1376 int valid = 0;
1378 switch (GET_CODE (x))
1380 case REG:
1381 valid = picochip_legitimate_address_register (x, strict);
1382 break;
1384 case PLUS:
1386 rtx base = XEXP (x, 0);
1387 rtx offset = XEXP (x, 1);
1388 if (strict && !REGNO_OK_FOR_BASE_P (REGNO(base)))
1390 valid = 0;
1391 break;
1394 valid = (REG == GET_CODE (base) &&
1395 picochip_legitimate_address_register (base, strict) &&
1396 CONST_INT == GET_CODE (offset) &&
1397 picochip_const_ok_for_base (mode, REGNO (base),
1398 INTVAL (offset)));
1399 break;
1402 case SYMBOL_REF:
1403 /* The user can select whether a symbol can be used as a memory
1404 address. Typically, this will decrease execution time (no
1405 register load is required first), but will increase code size
1406 (because the symbol will be used several times, rather than
1407 loaded once into a register.*/
1408 valid = TARGET_SYMBOL_AS_ADDRESS;
1409 break;
1411 case CONST:
1413 /* A constant memory address must be a (plus (symbol_ref)
1414 (const_int)), and is only allowed when the symbols are
1415 permitted addresses. */
1416 rtx inner = XEXP (x, 0);
1418 valid = (TARGET_SYMBOL_AS_ADDRESS &&
1419 PLUS == GET_CODE (inner) &&
1420 SYMBOL_REF == GET_CODE (XEXP (inner, 0)) &&
1421 CONST_INT == GET_CODE (XEXP (inner, 1)));
1423 break;
1427 default:
1428 valid = 0;
1431 return valid;
1435 /* For all memory operations, picochip allows a uconst4 offset value. It
1436 is hence beneficial to turn an
1437 addr = <reg + long_const>
1438 ld/st addr
1440 into
1442 X = reg + long_const & FFF0
1443 diff = long_const - (long_const & FFF0)
1444 ld/st <X + diff>
1446 X can be reused in subsequent memory operations.
1449 picochip_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1450 enum machine_mode mode)
1452 unsigned mask_val;
1454 if (!optimize)
1455 return x;
1457 /* Depending on mode, the offsets allowed are either 16/32/64.*/
1458 switch (mode)
1460 case QImode:
1461 mask_val = 0xFFF0;
1462 break;
1463 case HImode:
1464 mask_val = 0xFFE0;
1465 break;
1466 case SImode:
1467 mask_val = 0xFFC0;
1468 break;
1469 default:
1470 return x;
1473 if (GET_CODE (x) == PLUS
1474 && GET_CODE (XEXP (x, 0)) == REG
1475 && GET_CODE (XEXP (x, 1)) == CONST_INT)
1477 int high_val, low_val, offset;
1478 offset = INTVAL (XEXP (x, 1));
1479 /* Ignore cases with negative offsets. */
1480 if (offset < 0)
1481 return x;
1482 high_val = offset & mask_val;
1483 low_val = offset - high_val;
1484 if (high_val != 0)
1486 rtx temp_reg = force_reg (Pmode, gen_rtx_PLUS (Pmode, XEXP (x, 0), GEN_INT(high_val)));
1487 x = gen_rtx_PLUS (Pmode, temp_reg, GEN_INT(low_val));
1488 return x;
1491 return x;
1494 /* For all memory operations, picochip allows a uconst4 offset value. It
1495 is hence beneficial to turn an
1496 addr = <reg + long_const>
1497 ld/st addr
1499 into
1501 X = reg + long_const & FFF0
1502 diff = long_const - (long_const & FFF0)
1503 ld/st <X + diff>
1505 X can be reused in subsequent memory operations.
1508 picochip_legitimize_reload_address (rtx *x,
1509 enum machine_mode mode,
1510 int opnum, int type,
1511 int ind_levels ATTRIBUTE_UNUSED)
1513 unsigned mask_val;
1515 if (picochip_symbol_offset(*x))
1517 *x = gen_rtx_CONST(mode, *x);
1518 return 0;
1520 if (!optimize)
1521 return 0;
1523 /* We should recognise addresses that we created.*/
1524 if (GET_CODE (*x) == PLUS
1525 && GET_CODE (XEXP (*x, 0)) == PLUS
1526 && GET_CODE (XEXP (XEXP (*x, 0), 0)) == REG
1527 && GET_CODE (XEXP (XEXP (*x, 0), 1)) == CONST_INT
1528 && GET_CODE (XEXP (*x, 1)) == CONST_INT)
1530 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1531 BASE_REG_CLASS, GET_MODE (*x), VOIDmode, 0, 0,
1532 opnum, (enum reload_type)type);
1533 return 1;
1536 /* Depending on mode, the offsets allowed are either 16/32/64. */
1537 switch (mode)
1539 case QImode:
1540 mask_val = 0xFFF0;
1541 break;
1542 case HImode:
1543 mask_val = 0xFFE0;
1544 break;
1545 case SImode:
1546 mask_val = 0xFFC0;
1547 break;
1548 default:
1549 return 0;
1552 if (GET_CODE (*x) == PLUS
1553 && GET_CODE (XEXP (*x, 0)) == REG
1554 && GET_CODE (XEXP (*x, 1)) == CONST_INT)
1556 int high_val, low_val, offset;
1557 offset = INTVAL (XEXP (*x, 1));
1558 /* Ignore cases with negative offsets. */
1559 if (offset < 0)
1560 return 0;
1561 high_val = offset & mask_val;
1562 low_val = offset - high_val;
1563 if (high_val != 0)
1565 rtx temp_reg = gen_rtx_PLUS (Pmode, XEXP (*x, 0), GEN_INT(high_val));
1566 *x = gen_rtx_PLUS (Pmode, temp_reg, GEN_INT(low_val));
1567 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1568 BASE_REG_CLASS, GET_MODE (*x), VOIDmode, 0, 0,
1569 opnum, (enum reload_type)type);
1570 return 1;
1574 return 0;
1577 /* Detect an rtx which matches (plus (symbol_ref) (const_int)). */
1579 picochip_symbol_offset (rtx operand)
1582 return (PLUS == GET_CODE (operand) &&
1583 SYMBOL_REF == GET_CODE (XEXP (operand, 0)) &&
1584 CONST_INT == GET_CODE (XEXP (operand, 1)));
1588 /* Assembly output. */
1590 /* The format here should match the format used in the output of
1591 symbol_ref's elsewhere in this file. */
1592 void
1593 picochip_output_label (FILE * stream, const char name[])
1595 int is_cfi_label = (strncmp (name, "picoMark_LCFI", 13) == 0);
1597 /* If VLIW scheduling is in use, any Call Frame Information labels
1598 generated inside a packet must have their output deferred until
1599 the end of the packet. */
1600 if (picochip_schedule_type == DFA_TYPE_SPEED &&
1601 is_cfi_label && picochip_vliw_continuation)
1603 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
1605 internal_error ("LCFI labels have already been deferred");
1607 strcpy (picochip_current_vliw_state.cfi_label_name[
1608 picochip_current_vliw_state.num_cfi_labels_deferred], name);
1609 picochip_current_vliw_state.num_cfi_labels_deferred++;
1611 else
1613 assemble_name (stream, name);
1615 if (strncmp (name, "picoMark_", 9) == 0)
1616 fprintf (stream, "=\n");
1617 else
1618 fprintf (stream, ":\n");
1624 /* The format here should match the format used in the output of
1625 symbol_ref's elsewhere in this file. */
1626 void
1627 picochip_output_labelref (FILE * stream, const char name[])
1629 fprintf (stream, "_%s", name);
1632 void
1633 picochip_weaken_label (FILE * stream, const char name[])
1635 fprintf (stream, ".weak ");
1636 assemble_name (stream, name);
1637 fprintf (stream, "\n");
1640 /* Return true if the given label (or label prefix) denotes a marker
1641 label which should be emitted in the form LABEL= */
1642 static int
1643 picochip_is_marker_prefix (const char *prefix)
1645 return (strcmp (prefix, "L") != 0 && strcmp (prefix, "LC") != 0
1646 && strcmp (prefix, "LP") != 0);
1649 void
1650 picochip_output_internal_label (FILE * stream, const char *prefix,
1651 unsigned long num)
1654 /* Emit different types of label, based upon their prefix. They
1655 are handled differently to allow the assembler to ensure that
1656 branch target labels are properly aligned, while other labels
1657 will only serve as code markers, not branch targets. Aligning
1658 labels unnecessarily can result in much code wastage. */
1659 if (picochip_is_marker_prefix (prefix))
1661 /* Special label marker. If it appears in the middle of a VLIW
1662 packet, defer it until the end of the packet. There has
1663 never been a need to handle more than one lm label at a time. */
1664 if (picochip_schedule_type == DFA_TYPE_SPEED &&
1665 (strcmp (prefix, "LM")) == 0 && picochip_vliw_continuation)
1667 if (strlen (picochip_current_vliw_state.lm_label_name) != 0)
1668 internal_error ("LM label has already been deferred");
1670 sprintf (picochip_current_vliw_state.lm_label_name,
1671 "picoMark_%s%ld", prefix, num);
1673 else if (picochip_schedule_type == DFA_TYPE_SPEED &&
1674 (strcmp (prefix, "LCFI")) == 0 && picochip_vliw_continuation)
1676 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
1678 internal_error ("LCFI labels have already been deferred.");
1680 sprintf(picochip_current_vliw_state.cfi_label_name[
1681 picochip_current_vliw_state.num_cfi_labels_deferred],
1682 "picoMark_%s%ld", prefix, num);
1683 picochip_current_vliw_state.num_cfi_labels_deferred++;
1685 else
1687 /* Marker label. */
1688 fprintf (stream, "_picoMark_%s%ld=\n", prefix, num);
1692 else
1694 /* Normal label. */
1695 fprintf (stream, "_%s%ld:\n", prefix, num);
1700 void
1701 picochip_generate_internal_label (char *str, const char *prefix, long num)
1703 /* Two types of internal label can be generated: branch target
1704 labels and code marker labels. Branch target labels must always
1705 be aligned (since code will execute at these
1706 points). Differentiate between the two by prepending markers with
1707 a unique prefix, which can later be used in output_label to
1708 figure out which label syntax to use. */
1709 if (picochip_is_marker_prefix (prefix))
1710 sprintf (str, "picoMark_%s%ld", prefix, num);
1711 else
1712 sprintf (str, "%s%ld", prefix, num);
1716 void
1717 picochip_asm_output_anchor (rtx symbol)
1719 fprintf (asm_out_file, ".offsetData _%s, ",XSTR (symbol, 0));
1720 fprintf (asm_out_file, "+ " HOST_WIDE_INT_PRINT_DEC"\n",SYMBOL_REF_BLOCK_OFFSET(symbol));
1723 void
1724 picochip_output_aligned_common (FILE * stream, const char *name,
1725 unsigned size, unsigned alignment)
1728 fprintf (stream, ".commonData ");
1729 assemble_name (stream, name);
1730 fprintf (stream, ", %u, %u\n", size, alignment / 8);
1731 picochip_output_global (stream, name);
1735 void
1736 picochip_output_aligned_local (FILE * stream, const char *name,
1737 unsigned size, unsigned alignment)
1740 fprintf (stream, ".commonData ");
1741 assemble_name (stream, name);
1742 fprintf (stream, ", %u, %u\n", size, alignment / 8);
1746 void
1747 picochip_output_global (FILE * stream, const char *name)
1749 fprintf (stream, ".global ");
1750 assemble_name (stream, name);
1751 fprintf (stream, "\n");
1754 /* Output an assembly language string. Output as a sequence of decimal
1755 numbers, followed by the literal string to make it obvious what the
1756 numbers represent. */
1757 void
1758 picochip_output_ascii (FILE * file, const char *str, int length)
1760 int i = 0;
1762 fprintf (file, ".ascii ");
1764 for (i = 0; i < length; ++i)
1766 fprintf (file, "16#%x# ", (char) (str[i]));
1769 fprintf (file, " ; ");
1771 for (i = 0; i < length; ++i)
1773 char c = str[i];
1775 switch (c)
1777 case '\n':
1778 fprintf (file, "\\n");
1779 break;
1780 case '\t':
1781 fprintf (file, "\\t");
1782 break;
1783 case '\0':
1784 fprintf (file, "\\0");
1785 break;
1786 default:
1787 fprintf (file, "%c", c);
1792 fprintf (file, "\n");
1796 /* Output the beginning of an ASM file. */
1797 void
1798 picochip_asm_file_start (void)
1800 default_file_start ();
1802 fprintf (asm_out_file, "// picoChip ASM file\n");
1803 fprintf (asm_out_file, "//.file \"%s\"\n", main_input_filename);
1805 fprintf (asm_out_file, "// Has byte access: %s\n",
1806 (TARGET_HAS_BYTE_ACCESS ? "Yes" : "No"));
1808 if (TARGET_HAS_MUL_UNIT)
1809 fprintf (asm_out_file, "// Has multiply: Yes (Multiply unit)\n");
1810 else if (TARGET_HAS_MAC_UNIT)
1811 fprintf (asm_out_file, "// Has multiply: Yes (Mac unit)\n");
1812 else
1813 fprintf (asm_out_file, "// Has multiply: No\n");
1816 /* Output the end of an ASM file. */
1817 void
1818 picochip_asm_file_end (void)
1820 /* Include a segment end to make it easy for PERL scripts to grab
1821 segments. This is now done by assembler*/
1823 fprintf (asm_out_file, "// End of picoChip ASM file\n");
1827 /* Output frame debug information to the given stream. */
1828 static void
1829 picochip_output_frame_debug (FILE * file)
1831 int i = 0;
1833 if (current_function_is_leaf)
1834 fprintf (file, "\t\t// Leaf function\n");
1835 else
1836 fprintf (file, "\t\t// Non-leaf function\n");
1838 if (picochip_can_eliminate_link_sp_save ())
1839 fprintf (file, "\t\t// Link/fp save/restore can be eliminated\n");
1841 if (cfun->static_chain_decl != NULL)
1842 fprintf (file, "\t\t// Static chain in use\n");
1844 fprintf (file, "\t\t// Incoming argument size: %d bytes\n",
1845 picochip_arg_area_size_in_bytes ());
1846 fprintf (file, "\t\t// Incoming arg offset: %d\n",
1847 picochip_arg_area_byte_offset ());
1848 fprintf (file, "\t\t// Pretend arg size: %d\n",
1849 picochip_pretend_arg_area_size ());
1850 fprintf (file, "\t\t// Pretend arg offset (ARGP): %d\n",
1851 picochip_pretend_arg_area_byte_offset ());
1852 fprintf (file, "\t\t// Special reg area size: %d bytes\n",
1853 picochip_special_save_area_size_in_bytes ());
1854 fprintf (file, "\t\t// Special reg area offset: %d\n",
1855 picochip_special_save_area_byte_offset ());
1857 /* Output which registers are saved. */
1858 fprintf (file, "\t\t// Saved regs: ");
1859 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1861 if (picochip_reg_needs_saving (i))
1862 fprintf (file, "%s ", picochip_regnames[i]);
1864 fprintf (file, "\t\t\n");
1866 fprintf (file, "\t\t// Save area size: %d bytes\n",
1867 picochip_save_area_size_in_bytes ());
1868 fprintf (file, "\t\t// Save area offset: %d\n",
1869 picochip_save_area_byte_offset ());
1871 fprintf (file, "\t\t// Frame size: %ld bytes\n", get_frame_size ());
1872 fprintf (file, "\t\t// Frame offset (FP): %d\n",
1873 picochip_frame_byte_offset ());
1875 fprintf (file, "\t\t// Outgoing argument area size: %d bytes\n",
1876 crtl->outgoing_args_size);
1880 /* Output picoChip function prologue. This contains human-readable
1881 information about the function. */
1882 void
1883 picochip_function_prologue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1885 /* Get the function's name, as described by its RTL. This may be
1886 different from the DECL_NAME name used in the source file. The
1887 real declaration name must be used, to ensure that the prologue
1888 emits the right information for the linker. */
1889 rtx x;
1890 const char *fnname;
1891 x = DECL_RTL (current_function_decl);
1892 gcc_assert (MEM_P (x));
1893 x = XEXP (x, 0);
1894 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1895 fnname = XSTR (x, 0);
1897 /* Note that the name of the function is given in the &_%s
1898 form. This matches the name of the function as used in labels,
1899 and function calls, and enables processCallGraph to match
1900 function calls to the name of the function, as defined here. */
1901 fprintf (file, "// picoChip Function Prologue : &_%s = %d bytes\n",
1902 fnname, picochip_arg_area_byte_offset ());
1904 picochip_output_frame_debug (file);
1905 fprintf (file, "\n");
1909 /* Output picoChip function epilogue. */
1910 void
1911 picochip_function_epilogue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1914 rtx x;
1915 const char *fnname;
1916 x = DECL_RTL (current_function_decl);
1917 gcc_assert (MEM_P (x));
1918 x = XEXP (x, 0);
1919 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1920 fnname = XSTR (x, 0);
1921 fprintf (file, "\n// picoChip Function Epilogue : %s\n\n",
1922 fnname);
1925 /* Manipulate the asm output. Some machines only execute the code when
1926 there is actually a chance of needing it (e.g., FRV doesn't execute
1927 it if the scheduling pass wasn't used). We always execute it,
1928 simple to ensure that it is exercised more often, and bugs are more
1929 likely to be found.
1931 This function's prime reason for existence is to insert the VLIW
1932 separators where appropriate. The separators must be inserted
1933 before any comments which appear at the end of the file.
1936 const char *
1937 picochip_asm_output_opcode (FILE * f, const char *ptr)
1939 int c;
1941 /* Flag to specify when a VLIW continuation has been inserted onto
1942 the line. Continuations are either inserted before any comments,
1943 or before the end of the line is reached. The flag ensures that
1944 we don't insert continuations twice (i.e., at the comment and the
1945 end of line). */
1946 int continuation_inserted = 0;
1948 /* If the instruction uses multiple lines (i.e., a new line
1949 character appears in the opcode), then ensure that no attempt is
1950 made to pack it into a VLIW. */
1951 if (strchr (ptr, '\n') != NULL && picochip_vliw_continuation)
1952 internal_error
1953 ("picochip_asm_output_opcode - Found multiple lines in VLIW packet %s",
1954 ptr);
1957 /* If a delay slot is pending, output the directive to the assembler
1958 before the instruction. */
1959 if (picochip_is_delay_slot_pending)
1961 picochip_is_delay_slot_pending = 0;
1962 fputs ("=->\t", f);
1965 /* Keep going for entire opcode. All substitution performed ourselves. */
1966 while (*ptr)
1968 c = *ptr++;
1970 /* Determine whether a VLIW continuation must be inserted before
1971 any comments, or the end of the opcode. A flag is set to show
1972 that we have inserted a continuation on this line, so that we
1973 don't try to insert another continuation when the end of the
1974 opcode is reached. The only other case for a continuation
1975 might have been a newline, but these aren't allowed in
1976 conjunction with VLIW continuations (see above code). */
1977 if (picochip_vliw_continuation &&
1978 !continuation_inserted &&
1979 ((c == '/' && (*ptr == '/')) || *ptr == '\0'))
1981 fprintf (f, "\\ ");
1982 continuation_inserted = 1;
1985 /* Detect an explicit VLIW separator. */
1986 if (c == '%' && (*ptr == '|'))
1988 fprintf (f, "\\");
1989 ptr++;
1991 /* Detect the need for an ALU id operand. */
1992 else if (c == '%' && (*ptr == '#'))
1994 fputc (picochip_get_vliw_alu_id (), f);
1996 if (TARGET_DEBUG)
1997 printf ("Generated ALU char at %s for insn %d\n", ptr,
1998 INSN_UID (picochip_current_prescan_insn));
2000 /* Skip past unwanted # */
2001 ptr++;
2003 /* Detect the need for branch delay slot. */
2004 else if (c == '%' && (*ptr == '>'))
2006 /* Only emit delay slots (NOP's, or otherwise) when delay
2007 * slot scheduling has actually been enabled, otherwise VLIW
2008 * scheduling and delay slot scheduling output combine to
2009 * produce nasty effects. */
2010 if (flag_delayed_branch)
2012 if (dbr_sequence_length () == 0)
2013 fputs ("\n=->\tNOP", f);
2014 else
2015 picochip_is_delay_slot_pending = 1;
2018 /* Skip past unwanted > */
2019 ptr++;
2021 /* Detect any %digit specifiers. */
2022 else if (c == '%' && (*ptr >= '0' && *ptr <= '9'))
2024 c = atoi (ptr);
2025 picochip_print_operand (f, recog_data.operand[c], 0);
2026 while ((c = *ptr) >= '0' && c <= '9')
2027 ptr++;
2029 /* Detect any %letterdigit specifiers. */
2030 else if (c == '%' && ((*ptr >= 'a' && *ptr <= 'z')
2031 || (*ptr >= 'A' && *ptr <= 'Z')))
2033 int letter = *ptr++;
2035 c = atoi (ptr);
2037 switch (letter)
2039 case 'l':
2040 output_asm_label (recog_data.operand[c]);
2041 break;
2043 case 'a':
2044 output_address (recog_data.operand[c]);
2045 break;
2047 default:
2048 picochip_print_operand (f, recog_data.operand[c], letter);
2051 while ((c = *ptr) >= '0' && c <= '9')
2052 ptr++;
2054 else if (c == '%')
2055 internal_error
2056 ("picochip_asm_output_opcode - can%'t output unknown operator %c",
2057 *ptr);
2058 else
2059 fputc (c, f);
2062 /* Reached the end of the packet. If any labels were deferred
2063 during output, emit them now. */
2064 if (!picochip_vliw_continuation)
2066 if (picochip_current_vliw_state.num_cfi_labels_deferred != 0)
2068 fprintf (f, "\n");
2069 assemble_name (f, picochip_current_vliw_state.cfi_label_name[0]);
2070 fprintf (f, "=");
2071 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
2073 fprintf (f, "\n");
2074 assemble_name (f, picochip_current_vliw_state.cfi_label_name[1]);
2075 fprintf (f, "=");
2079 if (strlen (picochip_current_vliw_state.lm_label_name) != 0)
2081 fprintf (f, "\n");
2082 assemble_name (f, picochip_current_vliw_state.lm_label_name);
2083 fprintf (f, "=");
2087 /* Output an end-of-packet marker if requested. */
2088 if (!picochip_vliw_continuation &&
2089 TARGET_DEBUG && picochip_schedule_type == DFA_TYPE_SPEED)
2090 fprintf (f, "\n\t//-------------- End of VLIW packet -----------------");
2092 return ptr;
2095 /* Function RTL expansion. */
2097 /* Expand the prologue into RTL. */
2098 void
2099 picochip_expand_prologue (void)
2101 int stack_adjustment = 0;
2102 int special_save_offset = 0;
2103 int general_save_offset = 0;
2104 int reg_save_offset = 0;
2105 int i = 0;
2107 stack_adjustment = picochip_arg_area_byte_offset ();
2108 general_save_offset =
2109 -(stack_adjustment - picochip_save_area_byte_offset ());
2110 special_save_offset =
2111 -(stack_adjustment - picochip_special_save_area_byte_offset ());
2113 /* Save the link registers. We could try to save just one register
2114 here. This would reduce the amount of stack space required.
2115 There hasnt been a good reason to do that so far. */
2116 if (!picochip_can_eliminate_link_sp_save ())
2117 picochip_emit_save_register (gen_rtx_REG (SImode, LINK_REGNUM),
2118 special_save_offset);
2120 /* Save callee-save registers. */
2121 reg_save_offset = 0;
2122 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2124 if (picochip_reg_needs_saving (i))
2127 /* If this register is an even numbered register, and the
2128 next register also needs to be saved, use a SImode save,
2129 which does both in one instruction. Note that a special
2130 check is performed to ensure that the double word aligned
2131 store is valid (e.g., it is possible that r6, r8, r9 need
2132 to be saved, in which case once r6 has been saved, the
2133 stored offset is no longer aligned, and an STL/LDL
2134 instruction becomes invalid). Alternately, we could store all
2135 aligned registers first and then save the single one(s). */
2136 if ((i % 2 == 0) &&
2137 picochip_reg_needs_saving (i + 1) &&
2138 picochip_is_aligned (reg_save_offset, LONG_TYPE_SIZE))
2140 picochip_emit_save_register (gen_rtx_REG (SImode, i),
2141 general_save_offset +
2142 reg_save_offset);
2143 reg_save_offset += 2 * UNITS_PER_WORD;
2144 i++;
2146 else
2148 picochip_emit_save_register (gen_rtx_REG (HImode, i),
2149 general_save_offset +
2150 reg_save_offset);
2151 reg_save_offset += UNITS_PER_WORD;
2157 /* Emit a stack adjustment where required. */
2158 if (stack_adjustment != 0)
2159 picochip_emit_stack_allocate (stack_adjustment);
2161 /* If this function uses varadic arguments, write any unnamed
2162 registers to the stack. */
2163 if (cfun->stdarg)
2165 int stdarg_offset = picochip_pretend_arg_area_byte_offset ();
2167 /* Sanity check. The pretend argument offset should be 32-bit aligned. */
2168 gcc_assert(picochip_pretend_arg_area_byte_offset () % 4 == 0);
2170 picochip_emit_save_register (gen_rtx_REG (SImode, 0), stdarg_offset);
2171 picochip_emit_save_register (gen_rtx_REG (SImode, 2),
2172 stdarg_offset + 4);
2173 picochip_emit_save_register (gen_rtx_REG (SImode, 4),
2174 stdarg_offset + 8);
2180 /* Expand the epilogue into RTL. */
2181 void
2182 picochip_expand_epilogue (int is_sibling_call ATTRIBUTE_UNUSED)
2184 int stack_adjustment = 0;
2185 int special_save_offset = 0;
2186 int general_save_offset = 0;
2187 int reg_save_offset = 0;
2188 int i = 0;
2189 int use_link_fp_restore_stack_adjust = 0; /* Default to using an explicit
2190 stack restore. */
2192 stack_adjustment = picochip_arg_area_byte_offset ();
2193 general_save_offset =
2194 -(stack_adjustment - picochip_save_area_byte_offset ());
2195 special_save_offset =
2196 -(stack_adjustment - picochip_special_save_area_byte_offset ());
2198 /* Emit a stack adjustment where required. */
2199 if (stack_adjustment != 0)
2201 /* If the link/fp is already being restored, and the offset to
2202 their save location is small enough, don't bother adjusting
2203 the stack explicitly. */
2204 if (picochip_special_save_area_byte_offset () < 512 &&
2205 !picochip_can_eliminate_link_sp_save ())
2206 use_link_fp_restore_stack_adjust = 1;
2207 else
2208 /* Explicitly restore the stack. */
2209 picochip_emit_stack_allocate (-stack_adjustment);
2212 /* Restore the Link/FP registers. Only save the link register? */
2213 if (!picochip_can_eliminate_link_sp_save ())
2215 if (use_link_fp_restore_stack_adjust)
2216 picochip_emit_restore_register (gen_rtx_REG (SImode, LINK_REGNUM),
2217 picochip_special_save_area_byte_offset
2218 ());
2219 else
2220 picochip_emit_restore_register (gen_rtx_REG (SImode, LINK_REGNUM),
2221 special_save_offset);
2224 /* Restore callee-save registers. */
2225 reg_save_offset = 0;
2226 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2228 if (picochip_reg_needs_saving (i))
2231 /* If this register is an even numbered register, and the
2232 next register also needs to be saved, use a SImode save,
2233 which does both in one instruction. Note that a special
2234 check is performed to ensure that the double word aligned
2235 store is valid (e.g., it is possible that r6, r8, r9 need
2236 to be saved, in which case once r6 has been saved, the
2237 stored offset is no longer aligned, and an STL/LDL
2238 instruction becomes invalid). We could store all aligned
2239 registers first, and then save the single one(s). */
2240 if ((i % 2 == 0) &&
2241 picochip_reg_needs_saving (i + 1) &&
2242 picochip_is_aligned (reg_save_offset, LONG_TYPE_SIZE))
2244 picochip_emit_restore_register (gen_rtx_REG (SImode, i),
2245 general_save_offset +
2246 reg_save_offset);
2247 reg_save_offset += 2 * UNITS_PER_WORD;
2248 i++;
2250 else
2252 picochip_emit_restore_register (gen_rtx_REG (HImode, i),
2253 general_save_offset +
2254 reg_save_offset);
2255 reg_save_offset += UNITS_PER_WORD;
2261 /* Emit a return instruction, which matches a (parallel
2262 [(return) (use r12)]) */
2264 rtvec p;
2265 p = rtvec_alloc (2);
2267 RTVEC_ELT (p, 0) = ret_rtx;
2268 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
2269 gen_rtx_REG (Pmode, LINK_REGNUM));
2270 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
2275 /* Assembly instruction output. */
2277 /* Test whether the given branch instruction is short, or long. Short
2278 * branches are equivalent to real branches, and may be DFA
2279 * scheduled. Long branches expand to a macro which is handled by the
2280 * elaborator, and cannot be scheduled. Occasionally, the branch
2281 * shortening pass, which is run after DFA scheduling, will change the
2282 * code layout and cause the short branch to be reverted into a long
2283 * branch. Instead of having to fix this up by emitting new assembly,
2284 * the short branch is emitted anyway. There is plenty of slack in the
2285 * calculation of long and short branches (10-bit offset, but only
2286 * 9-bits used in computation), so there is enough slack for this to
2287 * be safe. */
2288 static int
2289 picochip_is_short_branch (rtx insn)
2291 int isRealShortBranch = (get_attr_length(insn) == SHORT_BRANCH_LENGTH);
2293 return (isRealShortBranch ||
2294 picochip_current_vliw_state.num_insns_in_packet > 1);
2297 /* Output a compare-and-branch instruction (matching the cbranch
2298 pattern). */
2299 const char *
2300 picochip_output_cbranch (rtx operands[])
2303 if (HImode != GET_MODE (operands[1]) ||
2304 (HImode != GET_MODE (operands[2]) &&
2305 GET_CODE (operands[2]) != CONST_INT))
2307 internal_error ("%s: at least one operand can%'t be handled",
2308 __FUNCTION__);
2311 /* Use the type of comparison to output the appropriate condition
2312 test. */
2313 switch (GET_CODE (operands[0]))
2315 case NE:
2316 return ("// if (%1 != %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPNE %l3");
2318 case EQ:
2319 return ("// if (%1 == %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPEQ %l3");
2321 case LE:
2322 /* Reverse the operand order to be GE */
2323 return ("// if (%1 <= %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPGE %l3");
2325 case LEU:
2326 /* Reverse operand order of GEU. */
2327 return ("// if (%1 <= %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPHS %l3");
2329 case GE:
2330 return ("// if (%1 >= %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPGE %l3");
2332 case GEU:
2333 return ("// if (%1 >= %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPHS %l3");
2335 case LT:
2336 return ("// if (%1 < %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPLT %l3");
2338 case LTU:
2339 return ("// if (%1 <{U} %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPLO %l3");
2341 case GT:
2342 /* Reversed operand version of LT. */
2343 return ("// if (%1 > %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPLT %l3");
2345 case GTU:
2346 /* Reverse an LTU. */
2347 return ("// if (%1 >{U} %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPLO %l3");
2349 default:
2350 gcc_unreachable();
2354 /* Output a compare-and-branch instruction (matching the cbranch
2355 pattern). This function is current unused since the cbranch
2356 split is disabled. The function is kept around so we can use
2357 it when we understand how to do cbranch split safely. */
2358 const char *
2359 picochip_output_compare (rtx operands[])
2361 int code;
2363 if (HImode != GET_MODE (operands[1]) ||
2364 (HImode != GET_MODE (operands[2]) &&
2365 GET_CODE (operands[2]) != CONST_INT))
2367 internal_error ("%s: at least one operand can%'t be handled",
2368 __FUNCTION__);
2371 code = GET_CODE (operands[0]);
2372 /* Use the type of comparison to output the appropriate condition
2373 test. */
2374 switch (code)
2376 case NE:
2377 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2379 case EQ:
2380 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2382 case LE:
2383 /* Reverse the operand order to be GE */
2384 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2386 case LEU:
2387 /* Reverse operand order of GEU. */
2388 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2390 case GE:
2391 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2393 case GEU:
2394 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2396 case LT:
2397 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2399 case LTU:
2400 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2402 case GT:
2403 /* Reversed operand version of LT. */
2404 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2406 case GTU:
2407 /* Reverse an LTU. */
2408 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2410 default:
2411 gcc_unreachable();
2415 /* Output the branch insn part of a compare-and-branch split. */
2416 const char *
2417 picochip_output_branch (rtx operands[], rtx insn)
2420 int code = GET_CODE(operands[2]);
2421 if (picochip_is_short_branch (insn))
2423 /* Short branches can be output directly using the
2424 appropriate instruction. */
2425 switch (code)
2427 case NE:
2428 return ("BNE %l0 %>");
2429 case EQ:
2430 return ("BEQ %l0 %>");
2431 case LE:
2432 return ("BGE %l0 %>");
2433 case LEU:
2434 return ("BHS %l0 %>");
2435 case GE:
2436 return ("BGE %l0 %>");
2437 case GEU:
2438 return ("BHS %l0 %>");
2439 case LT:
2440 return ("BLT %l0 %>");
2441 case LTU:
2442 return ("BLO %l0 %>");
2443 case GT:
2444 return ("BLT %l0 %>");
2445 case GTU:
2446 return ("BLO %l0 %>");
2447 default:
2448 internal_error ("unknown short branch in %s (type %d)",
2449 __FUNCTION__, (int) INTVAL (operands[1]));
2450 return "UNKNOWN_BRANCH";
2453 else
2455 /* Long branches result in the emission of a special
2456 instruction, which the assembler expands into a suitable long
2457 branch. */
2459 /* Use the type of comparison to output the appropriate condition
2460 test. */
2461 switch (code)
2463 case NE:
2464 return ("JMPNE %l0 %>");
2465 case EQ:
2466 return ("JMPEQ %l0 %>");
2467 case LE:
2468 return ("JMPGE %l0 %>");
2469 case LEU:
2470 return ("JMPHS %l0 %>");
2471 case GE:
2472 return ("JMPGE %l0 %>");
2473 case GEU:
2474 return ("JMPHS %l0 %>");
2475 case LT:
2476 return ("JMPLT %l0 %>");
2477 case LTU:
2478 return ("JMPLO %l0 %>");
2479 case GT:
2480 return ("JMPLT %l0 %>");
2481 case GTU:
2482 return ("JMPLO %l0 %>");
2484 default:
2485 internal_error ("unknown long branch in %s (type %d)",
2486 __FUNCTION__, (int) INTVAL (operands[1]));
2487 return "UNKNOWN_BRANCH";
2493 /* Output a jump instruction. */
2494 const char *
2495 picochip_output_jump (rtx insn)
2497 if (picochip_is_short_branch (insn))
2498 return "BRA %l0%>";
2499 else
2500 return "JMPRA %l0%>";
2503 const char *
2504 picochip_output_put_array (int alternative, rtx operands[])
2506 /* Local output buffer. */
2507 char buf[256];
2509 int portArraySize = INTVAL(operands[1]);
2510 int portBaseIndex = INTVAL(operands[2]);
2512 if (alternative == 0)
2514 sprintf (buf, "// Array put\n\tadd.0 [lsl %%0,2],&__commTable_put_%d_%d,lr\n\tjl (lr)",
2515 portArraySize, portBaseIndex);
2516 output_asm_insn (buf, operands);
2518 else if (alternative == 1)
2520 /* Constant port id. Emit a real instruction. */
2521 int portIndex = INTVAL(operands[0]) + portBaseIndex;
2522 if (portIndex < portBaseIndex ||
2523 portIndex >= (portBaseIndex + portArraySize))
2525 error ("PUT uses port array index %d, which is out of range [%d..%d)",
2526 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2528 sprintf(buf, "PUT R[0:1],%d", portIndex);
2529 output_asm_insn (buf, operands);
2531 else
2532 gcc_unreachable();
2534 /* Both alternatives output the insn directly. */
2535 return "";
2538 const char *picochip_output_get_array (int alternative, rtx operands[])
2540 /* Local output buffer. */
2541 char buf[256];
2543 int portArraySize = INTVAL(operands[1]);
2544 int portBaseIndex = INTVAL(operands[2]);
2546 if (alternative == 0)
2548 sprintf (buf, "// Array get\n\tadd.0 [lsl %%0,2],&__commTable_get_%d_%d,lr\n\tjl (lr)",
2549 portArraySize, portBaseIndex);
2550 output_asm_insn (buf, operands);
2552 else if (alternative == 1)
2554 /* Constant port id. Emit a real instruction. */
2555 int portIndex = INTVAL(operands[0]) + portBaseIndex;
2556 if (portIndex < portBaseIndex ||
2557 portIndex >= (portBaseIndex + portArraySize))
2559 error ("GET uses port array index %d, which is out of range [%d..%d)",
2560 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2562 sprintf(buf, "GET %d,R[0:1]", portIndex);
2563 output_asm_insn (buf, operands);
2565 else
2566 gcc_unreachable();
2568 /* Both alternatives output the insn directly. */
2569 return "";
2572 const char *picochip_output_testport_array (int alternative, rtx operands[])
2574 /* Local output buffer. */
2575 char buf[256];
2577 int portArraySize = INTVAL(operands[2]);
2578 int portBaseIndex = INTVAL(operands[3]);
2580 if (alternative == 0)
2582 sprintf (buf, "// Array tstport\n\tadd.0 [lsl %%1,2],&__commTable_tstport_%d_%d,lr\n\tjl (lr)\n=->\tcopy.0 0,%%0\n\tcopyeq 1,%%0",
2583 portArraySize, portBaseIndex);
2584 output_asm_insn (buf, operands);
2586 else if (alternative == 1)
2588 /* Constant port id. Emit a real instruction. */
2589 int portIndex = INTVAL(operands[1]) + portBaseIndex;
2590 if (portIndex < portBaseIndex ||
2591 portIndex >= (portBaseIndex + portArraySize))
2593 error ("PUT uses port array index %d, which is out of range [%d..%d)",
2594 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2596 sprintf(buf, "copy.1 0,%%0 %%| TSTPORT %d\n\tcopyeq 1,%%0", portIndex);
2597 output_asm_insn (buf, operands);
2599 else
2600 gcc_unreachable();
2602 /* Both alternatives output the insn directly. */
2603 return "";
2606 /* Output a comparison operand as a symbol (e.g., >). */
2607 static void
2608 picochip_print_comparison (FILE * file, rtx operand, int letter)
2611 if (letter == 'i')
2613 /* Output just the comparison symbol. */
2614 switch (GET_CODE (operand))
2616 case NE:
2617 fprintf (file, "!=");
2618 break;
2619 case EQ:
2620 fprintf (file, "==");
2621 break;
2622 case GE:
2623 fprintf (file, ">=");
2624 break;
2625 case GEU:
2626 fprintf (file, ">={U}");
2627 break;
2628 case LT:
2629 fprintf (file, "<");
2630 break;
2631 case LTU:
2632 fprintf (file, "<{U}");
2633 break;
2634 case LE:
2635 fprintf (file, "<=");
2636 break;
2637 case LEU:
2638 fprintf (file, "<={U}");
2639 break;
2640 case GT:
2641 fprintf (file, ">");
2642 break;
2643 case GTU:
2644 fprintf (file, ">{U}");
2645 break;
2646 default:
2647 gcc_unreachable();
2650 else
2652 /* Output the comparison formatted as operand,symbol,operand */
2653 rtx op0 = XEXP (operand, 0);
2654 rtx op1 = XEXP (operand, 1);
2656 picochip_print_operand (file, op0, 0);
2657 picochip_print_comparison (file, operand, 'i');
2658 picochip_print_operand (file, op1, 0);
2662 /* This function generates a memory address operand in the given
2663 mode. That is, if the address contains a constant offset, then the
2664 offset is divided by the required mode size to compute the
2665 mode specific offset. By default, picochip_print_operand_address calls
2666 this function using the natural mode of the operand, but special
2667 operand codes can be used to invoke the computation using an
2668 unnatural mode (e.g., compute the HI aligned address of an SI mode
2669 address). */
2670 static void
2671 picochip_print_memory_address (FILE * file, rtx operand,
2672 enum machine_mode mode)
2674 rtx address = XEXP (operand, 0);
2676 /* Sanity check. */
2677 if (MEM != GET_CODE (operand))
2678 fatal_insn ("picochip_print_memory_address - Operand isn't memory based",
2679 operand);
2681 if (TARGET_DEBUG)
2683 printf ("picochip_print_memory_address: ");
2684 print_rtl (stdout, operand);
2685 printf ("\n");
2688 switch (GET_CODE (address))
2690 case PLUS:
2692 /* Grab the address components. */
2693 rtx base = XEXP (address, 0);
2694 rtx offset = XEXP (address, 1);
2696 /* Only handle reg+const addresses */
2697 if (REG == GET_CODE (base) && CONST_INT == GET_CODE (offset))
2699 /* Sanity check. If an FP+offset address is given, ensure
2700 that the offset lies within the given frame, or a lower
2701 frame. */
2702 if (REGNO (base) == STACK_POINTER_REGNUM )
2703 gcc_assert (INTVAL (offset) <= (picochip_arg_area_byte_offset () +
2704 crtl->args.size));
2706 /* Print the base register - identical for all modes. */
2707 fprintf (file, "(");
2708 picochip_print_operand (file, base, 'r');
2709 fprintf (file, ")");
2711 /* Print the constant offset with compensation for the mode. */
2712 switch (mode)
2714 case QImode:
2715 picochip_print_operand (file, offset, 'Q');
2716 break;
2718 case HImode:
2719 picochip_print_operand (file, offset, 'H');
2720 break;
2722 case SImode:
2723 case SFmode:
2724 picochip_print_operand (file, offset, 'S');
2725 break;
2727 case DImode:
2728 picochip_print_operand (file, offset, 'D');
2729 break;
2731 default:
2732 gcc_unreachable();
2739 break;
2741 case SYMBOL_REF:
2742 picochip_print_operand (file, address, 's');
2743 break;
2745 case CONST:
2747 rtx inner;
2748 rtx base;
2749 rtx offset;
2751 inner = XEXP (address, 0);
2753 /* Sanity check - the CONST memory address must be a base+offset. */
2754 gcc_assert (PLUS == GET_CODE (inner));
2756 base = XEXP (inner, 0);
2757 offset = XEXP (inner, 1);
2759 fprintf (file, "&_%s%+d", XSTR (base, 0), XINT (offset, 0));
2761 break;
2764 case REG:
2765 /* Register operand. Provide a zero offset. */
2766 fprintf (file, "(");
2767 picochip_print_operand (file, address, 'r');
2768 fprintf (file, ")0");
2769 break;
2771 default:
2772 gcc_unreachable();
2777 /* Output an operand. Formatting letters allow particular parts of
2778 the operand to be output. */
2779 void
2780 picochip_print_operand (FILE * file, rtx operand, int letter)
2783 /* Handle special cases. */
2784 switch (letter)
2786 /* VLIW continuation, for explicit VLIW sequences. */
2787 case '|':
2788 fprintf (file, "\\");
2789 return;
2791 /* ALU selector. */
2792 case '#':
2793 fputc (picochip_get_vliw_alu_id (), file);
2794 return;
2796 /* Delay slot specifier. */
2797 case '>':
2798 /* This should be handled in asm_output_opcode. */
2799 gcc_unreachable();
2801 /* Instruction mnemonics (e.g., lshift becomes LSL). */
2802 case 'I':
2803 switch (GET_CODE (operand))
2805 case AND:
2806 fprintf (file, "AND");
2807 break;
2808 case IOR:
2809 fprintf (file, "OR");
2810 break;
2811 case XOR:
2812 fprintf (file, "XOR");
2813 break;
2814 case PLUS:
2815 fprintf (file, "ADD");
2816 break;
2817 case MINUS:
2818 fprintf (file, "SUB");
2819 break;
2820 default:
2821 gcc_unreachable();
2823 return;
2825 /* Symbolic instructions (e.g., lshift becomes <<). */
2826 case 'i':
2827 switch (GET_CODE (operand))
2829 case AND:
2830 fprintf (file, "&");
2831 break;
2832 case IOR:
2833 fprintf (file, "|");
2834 break;
2835 case XOR:
2836 fprintf (file, "^");
2837 break;
2838 case PLUS:
2839 fprintf (file, "+");
2840 break;
2841 case MINUS:
2842 fprintf (file, "-");
2843 break;
2844 default:
2845 fprintf (file, "UNKNOWN_INSN");
2846 break;
2848 return;
2850 default: /* Not a punctuation character - process as normal. */
2851 break;
2854 switch (GET_CODE (operand))
2856 case REG:
2857 switch (letter)
2859 case 'R':
2860 /* Write a range of registers. */
2861 fprintf (file, "R[%d:%d]", REGNO (operand) + 1, REGNO (operand));
2862 break;
2864 case 'U':
2865 /* The upper register of a pair is requested. */
2866 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 1]);
2867 break;
2869 case 'L':
2870 /* The lower register of a pair is requested. Equivalent to the
2871 default, but included for completeness. */
2872 fprintf (file, "%s", picochip_regnames[REGNO (operand)]);
2873 break;
2875 case 'X':
2876 /* The 3rd register of a DI mode register. */
2877 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 2]);
2878 break;
2880 case 'Y':
2881 /* The 4th register of a DI mode register. */
2882 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 3]);
2883 break;
2885 default:
2886 fprintf (file, "%s", picochip_regnames[REGNO (operand)]);
2888 break;
2890 case CONST_INT:
2891 /* A range of letters can be used to format integers. The
2892 letters Q/H/S are used to divide the constant by the width of
2893 QI/HI/SI mode integers in bytes. The U/L modifiers are used
2894 to obtain the upper and lower 16-bits of a 32-bit
2895 constant. Where possible, signed numbers are used, since
2896 signed representations of numbers may be more compact (e.g.,
2897 65535 can be represented as -1, which fits into a small
2898 constant, whereas 65535 requires a large constant). */
2899 switch (letter)
2901 case 'Q':
2902 fprintf (file, "%ld", INTVAL (operand));
2903 break;
2905 case 'H':
2906 fprintf (file, "%ld", INTVAL (operand) / 2);
2907 break;
2909 case 'S':
2910 fprintf (file, "%ld", INTVAL (operand) / 4);
2911 break;
2913 case 'P':
2914 fprintf (file, "%d", exact_log2 (INTVAL(operand)));
2915 break;
2917 case 'U':
2918 fprintf (file, "%hi", (short) ((INTVAL (operand) >> 16) & 0xFFFF));
2919 break;
2921 case 'L':
2922 fprintf (file, "%hi", (short) (INTVAL (operand) & 0xFFFF));
2923 break;
2925 default:
2926 fprintf (file, "%ld", INTVAL (operand));
2927 break;
2929 break;
2931 case CONST_DOUBLE:
2933 long val;
2934 REAL_VALUE_TYPE rv;
2936 if (GET_MODE (operand) != SFmode)
2937 fatal_insn ("Unknown mode in print_operand (CONST_DOUBLE) :",
2938 operand);
2939 REAL_VALUE_FROM_CONST_DOUBLE (rv, operand);
2940 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
2942 switch (letter)
2944 case 'U':
2945 fprintf (file, "%hi", (short) ((val >> 16) & 0xFFFF));
2946 break;
2948 case 'L':
2949 fprintf (file, "%hi", (short) (val & 0xFFFF));
2950 break;
2953 break;
2957 /* Output a symbol. The output format must match that of
2958 picochip_output_label. */
2959 case SYMBOL_REF:
2960 /* Ensure that the symbol is marked as referenced. Gcc can
2961 occasionally omit the function bodies when it believes them
2962 to be unreferenced. */
2963 if (SYMBOL_REF_DECL (operand))
2964 mark_decl_referenced (SYMBOL_REF_DECL (operand));
2965 fprintf (file, "&");
2966 assemble_name (file, XSTR (operand, 0));
2967 break;
2969 case LABEL_REF:
2970 /* This format must match that of picochip_output_label. */
2971 fprintf (file, "&");
2972 output_asm_label (operand);
2973 break;
2975 case MEM:
2977 rtx addr = XEXP (operand, 0);
2979 switch (letter)
2981 case 'o':
2982 if (PLUS != GET_CODE (addr))
2983 fatal_insn ("Bad address, not (reg+disp):", addr);
2984 else
2985 picochip_print_operand (file, XEXP (addr, 1), 0);
2986 break;
2988 case 'M':
2989 /* Output a memory address in byte mode notation (i.e., the
2990 constant address (if any) is the actual byte address. */
2991 picochip_print_memory_address (file, operand, QImode);
2992 break;
2994 /* Output a constant offset of the given mode (i.e., divide
2995 the constant by the number of units in the mode to get the
2996 constant). */
2997 case 'Q':
2998 picochip_print_memory_address (file, operand, QImode);
2999 break;
3001 case 'H':
3002 picochip_print_memory_address (file, operand, HImode);
3003 break;
3005 case 'S':
3006 picochip_print_memory_address (file, operand, SImode);
3007 break;
3009 case 'F':
3010 picochip_print_memory_address (file, operand, SFmode);
3011 break;
3013 case 'b':
3014 if (PLUS != GET_CODE (addr))
3015 fatal_insn ("Bad address, not (reg+disp):", addr);
3016 else
3017 picochip_print_operand (file, XEXP (addr, 0), 0);
3018 break;
3020 /* When the mem operand is (reg + big offset) which cannot
3021 be represented in an instruction as operand, the compiler
3022 automatically generates the instruction to put in (reg +
3023 big offset) into another register. In such cases, it
3024 returns '0' as the character. This needs to be handled
3025 as well. */
3026 case 0:
3027 case 'r':
3028 if (REG != GET_CODE (addr))
3029 fatal_insn ("Bad address, not register:", addr);
3030 else
3031 picochip_print_operand (file, addr, 0);
3032 break;
3034 default:
3035 fprintf (file, "Unknown mem operand - letter %c ",
3036 (char) (letter));
3037 print_rtl (file, operand);
3040 break;
3043 case CONST:
3045 rtx const_exp = XEXP (operand, 0);
3047 /* Handle constant offsets to symbol references. */
3048 if (PLUS == GET_CODE (const_exp) &&
3049 SYMBOL_REF == GET_CODE (XEXP (const_exp, 0)) &&
3050 CONST_INT == GET_CODE (XEXP (const_exp, 1)))
3053 picochip_print_operand (file, XEXP (const_exp, 0), 0);
3054 if (INTVAL (XEXP (const_exp, 1)) >= 0)
3055 fprintf (file, "+");
3056 /* else use the - from the operand (i.e., AP-2)) */
3058 picochip_print_operand (file, XEXP (const_exp, 1), letter);
3062 break;
3065 case PLUS:
3067 /* PLUS expressions are of the form (base + offset). Different
3068 options (analagous to those of memory PLUS expressions) are used
3069 to extract the base and offset components. */
3071 switch (letter)
3073 case 'b':
3074 picochip_print_operand (file, XEXP (operand, 0), 0);
3075 break;
3077 case 'o':
3078 picochip_print_operand (file, XEXP (operand, 1), 0);
3079 break;
3081 default:
3083 /* If the expression is composed entirely of constants,
3084 evaluate the result. This should only occur with the
3085 picoChip specific comms instructions, which are emitted as
3086 base+offset expressions. */
3087 if (CONST_INT == GET_CODE (XEXP (operand, 0)) &&
3088 CONST_INT == GET_CODE (XEXP (operand, 1)))
3090 HOST_WIDE_INT result = (XINT (XEXP (operand, 0), 0) +
3091 XINT (XEXP (operand, 1), 0));
3092 fprintf (file, "%ld", result);
3094 else
3096 fprintf (file, "(");
3097 picochip_print_operand (file, XEXP (operand, 0), 0);
3098 fprintf (file, "+");
3099 picochip_print_operand (file, XEXP (operand, 1), 0);
3100 fprintf (file, ")");
3104 break;
3107 /* Comparison operations. */
3108 case NE:
3109 case EQ:
3110 case GE:
3111 case GEU:
3112 case LT:
3113 case LTU:
3114 case LE:
3115 case LEU:
3116 case GT:
3117 case GTU:
3118 picochip_print_comparison (file, operand, letter);
3119 return;
3121 default:
3122 fprintf (stderr, "Unknown operand encountered in %s\n", __FUNCTION__);
3123 print_rtl (file, operand);
3124 break;
3130 /* Output an operand address */
3131 void
3132 picochip_print_operand_address (FILE * file, rtx operand)
3135 switch (GET_CODE (operand))
3138 case SYMBOL_REF:
3139 /* This format must match that of picochip_output_label. */
3140 assemble_name (file, XSTR (operand, 0));
3141 break;
3143 case CODE_LABEL:
3144 /* Note this format must match that of picochip_output_label. */
3145 fprintf (file, "_L%d", XINT (operand, 5));
3146 break;
3148 case MEM:
3149 /* Pass on to a specialised memory address generator. */
3150 picochip_print_memory_address (file, operand, GET_MODE (operand));
3151 break;
3153 default:
3154 gcc_unreachable();
3161 /* Scheduling functions. */
3163 /* Save some of the contents of recog_data. */
3164 static void
3165 picochip_save_recog_data (void)
3167 picochip_saved_which_alternative = which_alternative;
3168 memcpy (&picochip_saved_recog_data, &recog_data,
3169 sizeof (struct recog_data));
3172 /* Restore some of the contents of global variable recog_data. */
3173 static void
3174 picochip_restore_recog_data (void)
3176 which_alternative = picochip_saved_which_alternative;
3177 memcpy (&recog_data, &picochip_saved_recog_data,
3178 sizeof (struct recog_data));
3181 /* Ensure that no var tracking notes are emitted in the middle of a
3182 three-instruction bundle. */
3183 static void
3184 reorder_var_tracking_notes (void)
3186 basic_block bb;
3188 FOR_EACH_BB (bb)
3190 rtx insn, next, last_insn = NULL_RTX;
3191 rtx queue = NULL_RTX;
3193 /* Iterate through the bb and find the last non-debug insn */
3194 for (insn = BB_HEAD (bb); insn != NEXT_INSN(BB_END (bb)); insn = NEXT_INSN(insn))
3196 if (NONDEBUG_INSN_P(insn))
3197 last_insn = insn;
3200 /* In all normal cases, queue up notes and emit them just before a TImode
3201 instruction. For the last instruction, emit the queued notes just after
3202 the last instruction. */
3203 for (insn = BB_HEAD (bb); insn != NEXT_INSN(BB_END (bb)); insn = next)
3205 next = NEXT_INSN (insn);
3207 if (insn == last_insn)
3209 while (queue)
3211 rtx next_queue = PREV_INSN (queue);
3212 PREV_INSN (NEXT_INSN(insn)) = queue;
3213 NEXT_INSN(queue) = NEXT_INSN(insn);
3214 PREV_INSN(queue) = insn;
3215 NEXT_INSN(insn) = queue;
3216 queue = next_queue;
3218 /* There is no more to do for this bb. break*/
3219 break;
3221 else if (NONDEBUG_INSN_P (insn))
3223 /* Emit queued up notes before the first instruction of a bundle. */
3224 if (GET_MODE (insn) == TImode)
3226 while (queue)
3228 rtx next_queue = PREV_INSN (queue);
3229 NEXT_INSN (PREV_INSN(insn)) = queue;
3230 PREV_INSN (queue) = PREV_INSN(insn);
3231 PREV_INSN (insn) = queue;
3232 NEXT_INSN (queue) = insn;
3233 queue = next_queue;
3237 else if (NOTE_P (insn))
3239 rtx prev = PREV_INSN (insn);
3240 PREV_INSN (next) = prev;
3241 NEXT_INSN (prev) = next;
3242 /* Ignore call_arg notes. They are expected to be just after the
3243 call insn. If the call is start of a long VLIW, labels are
3244 emitted in the middle of a VLIW, which our assembler can not
3245 handle. */
3246 if (NOTE_KIND (insn) != NOTE_INSN_CALL_ARG_LOCATION)
3248 PREV_INSN (insn) = queue;
3249 queue = insn;
3253 /* Make sure we are not dropping debug instructions.*/
3254 gcc_assert (queue == NULL_RTX);
3258 /* Perform machine dependent operations on the rtl chain INSNS. */
3259 void
3260 picochip_reorg (void)
3262 rtx insn, insn1, vliw_start = NULL_RTX;
3263 int vliw_insn_location = 0;
3265 /* We are freeing block_for_insn in the toplev to keep compatibility
3266 with old MDEP_REORGS that are not CFG based. Recompute it now. */
3267 compute_bb_for_insn ();
3269 if (optimize == 0)
3270 split_all_insns ();
3272 if (picochip_schedule_type != DFA_TYPE_NONE)
3274 timevar_push (TV_SCHED2);
3276 /* Process the instruction list, computing the sizes of each
3277 instruction, and consequently branch distances. This can
3278 result in some branches becoming short enough to be treated
3279 as a real branch instruction, rather than an assembly branch
3280 macro which may expand into multiple instructions. The
3281 benefit of shortening branches is that real branch
3282 instructions can be properly DFA scheduled, whereas macro
3283 branches cannot. */
3284 shorten_branches (get_insns ());
3286 /* Do control and data sched analysis again,
3287 and write some more of the results to dump file. */
3289 split_all_insns ();
3291 schedule_ebbs ();
3293 timevar_pop (TV_SCHED2);
3295 ggc_collect ();
3297 if (picochip_schedule_type == DFA_TYPE_SPEED)
3299 /* Whenever a VLIW packet is generated, all instructions in
3300 that packet must appear to come from the same source
3301 location. The following code finds all the VLIW packets,
3302 and tags their instructions with the location of the first
3303 instruction from the packet. Clearly this will result in
3304 strange behaviour when debugging the code, but since
3305 debugging and optimisation are being used in conjunction,
3306 strange behaviour is certain to occur anyway. */
3307 /* Slight bit of change. If the vliw set contains a branch
3308 or call instruction, we pick its location.*/
3309 for (insn = get_insns (); insn; insn = next_real_insn (insn))
3312 /* If this is the first instruction in the VLIW packet,
3313 extract its location. */
3314 if (GET_MODE (insn) == TImode)
3316 vliw_start = insn;
3317 vliw_insn_location = INSN_LOCATOR (insn);
3319 if (JUMP_P (insn) || CALL_P(insn))
3321 vliw_insn_location = INSN_LOCATOR (insn);
3322 for (insn1 = vliw_start; insn1 != insn ; insn1 = next_real_insn (insn1))
3323 INSN_LOCATOR (insn1) = vliw_insn_location;
3325 /* Tag subsequent instructions with the same location. */
3326 INSN_LOCATOR (insn) = vliw_insn_location;
3332 /* Locate the note marking the end of the function's prologue. If
3333 the note appears in the middle of a VLIW packet, move the note to
3334 the end. This avoids unpleasant consequences such as trying to
3335 emit prologue markers (e.g., .loc/.file directives) in the middle
3336 of VLIW packets. */
3337 if (picochip_schedule_type == DFA_TYPE_SPEED)
3339 rtx prologue_end_note = NULL;
3340 rtx last_insn_in_packet = NULL;
3342 for (insn = get_insns (); insn; insn = next_insn (insn))
3344 /* The prologue end must be moved to the end of the VLIW packet. */
3345 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
3347 prologue_end_note = insn;
3348 break;
3352 /* Find the last instruction in this packet. */
3353 for (insn = prologue_end_note; insn; insn = next_real_insn (insn))
3355 if (GET_MODE (insn) == TImode)
3356 break;
3357 else
3358 last_insn_in_packet = insn;
3361 if (last_insn_in_packet != NULL)
3363 rtx tmp_note
3364 = emit_note_after ((enum insn_note) NOTE_KIND (prologue_end_note),
3365 last_insn_in_packet);
3366 memcpy(&NOTE_DATA (tmp_note), &NOTE_DATA(prologue_end_note), sizeof(NOTE_DATA(prologue_end_note)));
3367 delete_insn (prologue_end_note);
3371 if (flag_var_tracking)
3373 timevar_push (TV_VAR_TRACKING);
3374 variable_tracking_main ();
3375 /* We also have to deal with variable tracking notes in the
3376 middle of VLIW packets. */
3377 reorder_var_tracking_notes();
3378 timevar_pop (TV_VAR_TRACKING);
3382 /* Return the ALU character identifier for the current
3383 instruction. This will be 0 or 1. */
3384 static char
3385 picochip_get_vliw_alu_id (void)
3387 int attr_type = 0;
3389 /* Always use ALU 0 if VLIW scheduling is disabled. */
3390 if (picochip_schedule_type != DFA_TYPE_SPEED)
3391 return '0';
3393 /* Get the attribute type of the instruction. Note that this can
3394 ruin the contents of recog_data, so save/restore around the
3395 call. */
3396 picochip_save_recog_data ();
3397 attr_type = get_attr_type (picochip_current_prescan_insn);
3398 picochip_restore_recog_data ();
3400 if (picochip_current_vliw_state.contains_pico_alu_insn)
3403 /* If this a picoAlu insn? If it is, then stuff it into ALU 0,
3404 else it must be the other ALU (either basic or nonCc)
3405 instruction which goes into 1. */
3406 if (attr_type == TYPE_PICOALU)
3407 return '0';
3408 else
3409 return '1';
3412 else if (picochip_current_vliw_state.contains_non_cc_alu_insn)
3414 /* Is this the non CC instruction? If it is, then stuff it into
3415 ALU 1, else it must be a picoAlu or basicAlu, in which case
3416 it goes into ALU 0. */
3417 if (attr_type == TYPE_NONCCALU)
3418 return '1';
3419 else
3420 return '0';
3422 else
3424 /* No picoAlu/nonCc instructions in use, so purely dependent upon
3425 whether an ALU instruction has already been scheduled in this
3426 cycle. */
3427 switch (picochip_current_vliw_state.num_alu_insns_so_far)
3429 case 0:
3430 picochip_current_vliw_state.num_alu_insns_so_far++;
3431 return '0';
3433 case 1:
3434 picochip_current_vliw_state.num_alu_insns_so_far++;
3435 return '1';
3437 default:
3438 internal_error ("too many ALU instructions emitted (%d)",
3439 picochip_current_vliw_state.num_alu_insns_so_far);
3440 return 'X';
3446 /* Reset any information about the current VLIW packing status. */
3447 static void
3448 picochip_reset_vliw (rtx insn)
3450 rtx local_insn = insn;
3452 /* Nothing to do if VLIW scheduling isn't being used. */
3453 if (picochip_schedule_type != DFA_TYPE_SPEED)
3454 return;
3456 if (TARGET_DEBUG)
3457 printf ("%s on insn %d\n", __FUNCTION__, INSN_UID (insn));
3459 /* Reset. */
3460 picochip_current_vliw_state.contains_pico_alu_insn = 0;
3461 picochip_current_vliw_state.contains_non_cc_alu_insn = 0;
3462 picochip_current_vliw_state.num_alu_insns_so_far = 0;
3463 picochip_current_vliw_state.num_cfi_labels_deferred = 0;
3464 picochip_current_vliw_state.lm_label_name[0] = 0;
3465 picochip_current_vliw_state.num_insns_in_packet = 0;
3467 /* Read through the VLIW packet, classifying the instructions where
3468 appropriate. */
3469 local_insn = insn;
3472 if (NOTE_P (local_insn) || DEBUG_INSN_P(local_insn))
3474 local_insn = NEXT_INSN (local_insn);
3475 continue;
3477 else if (!INSN_P (local_insn))
3478 break;
3479 else
3481 /* It is an instruction, but is it ours? */
3482 if (INSN_CODE (local_insn) != -1)
3484 int attr_type = 0;
3486 picochip_current_vliw_state.num_insns_in_packet += 1;
3488 /* Is it a picoAlu or nonCcAlu instruction? Note that the
3489 get_attr_type function can overwrite the values in
3490 the recog_data global, hence this is saved and
3491 restored around the call. Not doing so results in
3492 asm_output_opcode being called with a different
3493 instruction to final_prescan_insn, which is fatal. */
3494 picochip_save_recog_data ();
3495 attr_type = get_attr_type (local_insn);
3496 picochip_restore_recog_data ();
3498 if (attr_type == TYPE_PICOALU)
3499 picochip_current_vliw_state.contains_pico_alu_insn = 1;
3500 if (attr_type == TYPE_NONCCALU)
3501 picochip_current_vliw_state.contains_non_cc_alu_insn = 1;
3506 /* Get the next instruction. */
3507 local_insn = NEXT_INSN (local_insn);
3509 /* Keep going while the next instruction is part of the same
3510 VLIW packet (i.e., its a valid instruction and doesn't mark
3511 the start of a new VLIW packet. */
3513 while (local_insn &&
3514 (GET_MODE (local_insn) != TImode) && (INSN_CODE (local_insn) != -1));
3519 picochip_sched_reorder (FILE * file, int verbose,
3520 rtx * ready ATTRIBUTE_UNUSED,
3521 int *n_readyp ATTRIBUTE_UNUSED, int clock)
3524 if (verbose > 0)
3525 fprintf (file, ";;\tClock %d\n", clock);
3527 return picochip_sched_issue_rate ();
3532 picochip_sched_lookahead (void)
3534 /* It should always be enough to lookahead by 2 insns. Only slot0/1 could
3535 have a conflict. */
3536 return 2;
3540 picochip_sched_issue_rate (void)
3542 return 3;
3545 /* Adjust the scheduling cost between the two given instructions,
3546 which have the given dependency. */
3548 picochip_sched_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
3551 if (TARGET_DEBUG)
3553 printf ("Sched Adjust Cost: %d->%d is %d\n",
3554 INSN_UID (insn), INSN_UID (dep_insn), cost);
3556 printf (" Dependency type:");
3557 switch (REG_NOTE_KIND (link))
3559 case 0:
3560 printf ("Data\n");
3561 break;
3562 case REG_DEP_ANTI:
3563 printf ("ANTI\n");
3564 break;
3565 case REG_DEP_OUTPUT:
3566 printf ("OUTPUT\n");
3567 break;
3568 default:
3569 printf ("Unknown (%d)\n", REG_NOTE_KIND (link));
3573 /* Anti-dependencies are used to enforce the ordering between a
3574 * branch, and any subsequent instructions. For example:
3576 * BNE someLabel
3577 * ADD.0 r0,r1,r2
3579 * The ADD instruction must execute after the branch, and this is
3580 * enforced using an anti-dependency. Unfortunately, VLIW machines
3581 * are happy to execute anti-dependent instructions in the same
3582 * cycle, which then results in a schedule like the following being
3583 * created:
3585 * BNE someLabel \ ADD.0 r0,r1,r2
3587 * The instruction which would normally be conditionally executed
3588 * depending upon the outcome of the branch, is now unconditionally
3589 * executed every time. To prevent this happening, any
3590 * anti-dependencies between a branch and another instruction are
3591 * promoted to become real dependencies.
3593 if ((JUMP_P (dep_insn) || CALL_P(dep_insn)) && REG_NOTE_KIND (link) == REG_DEP_ANTI)
3596 if (TARGET_DEBUG)
3597 printf ("Promoting anti-dependency %d->%d to a true-dependency\n",
3598 INSN_UID (insn), INSN_UID (dep_insn));
3600 return 1;
3603 return cost;
3607 /* Return the minimum of the two values */
3608 static int
3609 minimum (int a, int b)
3611 if (a < b)
3612 return a;
3613 if (b < a)
3614 return b;
3615 /* I dont expect to get to this function with a==b.*/
3616 gcc_unreachable();
3620 /* This function checks if the memory of the two stores are just off by 2 bytes.
3621 It returns the lower memory operand's index.*/
3623 static int
3624 memory_just_off (rtx opnd1, rtx opnd2)
3626 int offset1 = 0, offset2 = 0;
3627 int reg1, reg2;
3629 if (GET_CODE(XEXP(opnd1, 0)) == PLUS && GET_CODE(XEXP(XEXP(opnd1, 0),1)) == CONST_INT)
3631 offset1 = INTVAL(XEXP(XEXP(opnd1, 0), 1));
3632 reg1 = REGNO(XEXP(XEXP(opnd1, 0), 0));
3634 else
3636 reg1 = REGNO(XEXP(opnd1, 0));
3638 if (GET_CODE(XEXP(opnd2, 0)) == PLUS && GET_CODE(XEXP(XEXP(opnd2, 0), 1)) == CONST_INT)
3640 offset2 = INTVAL(XEXP(XEXP(opnd2, 0), 1));
3641 reg2 = REGNO(XEXP(XEXP(opnd2, 0), 0));
3643 else
3645 reg2 = REGNO(XEXP(opnd2, 0));
3648 /* Peepholing 2 STW/LDWs has the restriction that the resulting STL/LDL's address
3649 should be 4 byte aligned. We can currently guarentee that only if the base
3650 address is FP(R13) and the offset is aligned. */
3652 if (reg1 == reg2 && reg1 == 13 && abs(offset1-offset2) == 2 && minimum(offset1, offset2) % 4 == 0)
3653 return (minimum(offset1, offset2) == offset1) ? 1:2;
3655 return 0;
3658 static int
3659 registers_just_off (rtx opnd1, rtx opnd2)
3661 int reg1, reg2;
3662 reg1 = REGNO(opnd1);
3663 reg2 = REGNO(opnd2);
3664 if (abs(reg1-reg2) == 1 && minimum(reg1, reg2) % 2 == 0)
3665 return (minimum(reg1, reg2) == reg1)?1:2;
3666 return 0;
3669 /* Check to see if the two LDWs can be peepholed together into a LDL
3670 They can be if the registers getting loaded into are contiguous
3671 and the memory addresses are contiguous as well.
3672 for eg.
3673 LDW r2,[r11]x
3674 LDW r3,[r11]x+1
3675 can be merged together into
3676 LDL r[3:2],[r11]
3678 NOTE:
3679 1. The LDWs themselves only guarentee that r11 will be a 2-byte
3680 aligned address. Only FP can be assumed to be 4 byte aligned.
3681 2. The progression of addresses and the register numbers should
3682 be similar. For eg., if you swap r2 and r3 in the above instructions,
3683 the resultant pair cannot be merged.
3686 bool
3687 ok_to_peephole_ldw(rtx opnd0, rtx opnd1, rtx opnd2, rtx opnd3)
3689 int memtest=0,regtest=0;
3690 regtest = registers_just_off(opnd1,opnd3);
3691 if (regtest == 0)
3692 return false;
3694 memtest = memory_just_off(opnd0,opnd2);
3695 if (memtest == 0)
3696 return false;
3698 if (regtest == memtest)
3700 return true;
3702 return false;
3705 /* Similar to LDW peephole */
3706 bool
3707 ok_to_peephole_stw(rtx opnd0, rtx opnd1, rtx opnd2, rtx opnd3)
3709 int memtest=0,regtest=0;
3710 regtest = registers_just_off(opnd1,opnd3);
3711 if (regtest == 0)
3712 return false;
3714 memtest = memory_just_off(opnd0,opnd2);
3715 if (memtest == 0)
3716 return false;
3718 if (regtest == memtest)
3720 return true;
3722 return false;
3726 /* Generate a SImode register with the register number that is the smaller of the two */
3728 gen_min_reg(rtx opnd1,rtx opnd2)
3730 return gen_rtx_REG (SImode, minimum(REGNO(opnd1),REGNO(opnd2)));
3733 /* Generate a SImode memory with the address that is the smaller of the two */
3735 gen_SImode_mem(rtx opnd1,rtx opnd2)
3737 int offset1=0,offset2=0;
3738 rtx reg;
3739 rtx address;
3740 if (GET_CODE(XEXP(opnd1,0)) == PLUS && GET_CODE(XEXP(XEXP(opnd1,0),1)) == CONST_INT)
3742 offset1 = INTVAL(XEXP(XEXP(opnd1,0),1));
3743 reg = XEXP(XEXP(opnd1,0),0);
3745 else
3747 reg = XEXP(opnd1,0);
3749 if (GET_CODE(XEXP(opnd2,0)) == PLUS && GET_CODE(XEXP(XEXP(opnd2,0),1)) == CONST_INT)
3751 offset2 = INTVAL(XEXP(XEXP(opnd2,0),1));
3753 address = gen_rtx_PLUS (HImode, reg, GEN_INT(minimum(offset1,offset2)));
3754 return gen_rtx_MEM(SImode,address);
3757 bool
3758 picochip_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, int* total, bool speed)
3761 int localTotal = 0;
3763 if (!speed)
3765 /* Need to penalize immediates that need to be encoded as long constants.*/
3766 if (code == CONST_INT && !(INTVAL (x) >= 0 && INTVAL (x) < 16))
3768 *total = COSTS_N_INSNS(1);
3769 return true;
3772 switch (code)
3774 case SYMBOL_REF:
3775 case LABEL_REF:
3776 *total = COSTS_N_INSNS (outer_code != MEM);
3777 return true;
3778 break;
3780 case IF_THEN_ELSE:
3781 /* if_then_else come out of cbranch instructions. It will get split into
3782 a condition code generating subtraction and a branch */
3783 *total = COSTS_N_INSNS (2);
3784 return true;
3785 break;
3787 case AND:
3788 case IOR:
3789 case XOR:
3790 if (GET_MODE(x) == SImode)
3791 *total = COSTS_N_INSNS (2);
3792 if (GET_MODE(x) == DImode)
3793 *total = COSTS_N_INSNS (4);
3794 return false;
3796 case MEM:
3797 /* Byte Memory access on a NO_BYTE_ACCESS machine would be expensive */
3798 if (GET_MODE(x) == QImode && !TARGET_HAS_BYTE_ACCESS)
3799 *total = COSTS_N_INSNS (10);
3801 /* 64-bit accesses have to be done through 2 32-bit access */
3802 if (GET_MODE(x) == DImode)
3803 *total = COSTS_N_INSNS (2);
3804 return false;
3805 break;
3807 case ASHIFTRT:
3809 /* SImode shifts are expensive */
3810 if (GET_MODE(x) == SImode)
3811 *total = COSTS_N_INSNS (10);
3813 /* Register shift by constant is cheap. */
3814 if ((GET_MODE(x) == QImode || GET_MODE(x) == HImode)
3815 && GET_CODE(XEXP(x, 0)) == REG
3816 && GET_CODE(XEXP(x, 1)) == CONST_INT)
3817 *total = COSTS_N_INSNS (1);
3818 else
3819 *total = COSTS_N_INSNS (4);
3820 return false;
3821 break;
3823 case DIV:
3824 case MOD:
3826 /* Divisions are more expensive than the default 7*/
3827 if (GET_MODE(x) == SImode)
3828 *total = COSTS_N_INSNS (20);
3829 else
3830 *total = COSTS_N_INSNS (12);
3831 return false;
3832 break;
3834 case MULT:
3835 /* Look for the simple cases of multiplying register*register or
3836 register*constant. */
3837 if ((GET_MODE(x) == QImode || GET_MODE(x) == HImode)
3838 && ((GET_CODE(XEXP(x, 0)) == REG
3839 && (GET_CODE(XEXP(x, 1)) == REG || GET_CODE(XEXP(x,1)) == CONST_INT))
3840 || (GET_CODE(XEXP(x, 0)) == ZERO_EXTEND
3841 && GET_CODE(XEXP(XEXP(x, 0),0)) == REG
3842 && GET_CODE(XEXP(x, 1)) == ZERO_EXTEND
3843 && GET_CODE(XEXP(XEXP(x, 1),0)) == REG)))
3846 /* When optimising for size, multiplication by constant
3847 should be discouraged slightly over multiplication by a
3848 register. */
3849 if (picochip_has_mac_unit)
3851 /* Single cycle multiplication, but the result must be
3852 loaded back into a general register afterwards. */
3853 *total = COSTS_N_INSNS(2);
3854 return true;
3856 else if (picochip_has_mul_unit)
3858 /* Single cycle multiplication. */
3859 *total = COSTS_N_INSNS(1);
3860 return true;
3862 /* Else no multiply available. Use default cost. */
3865 break;
3867 default:
3868 /* Do nothing. */
3869 break;
3872 if (localTotal != 0)
3874 *total = localTotal;
3875 return true;
3877 else
3879 return false;
3884 void
3885 picochip_final_prescan_insn (rtx insn, rtx * opvec ATTRIBUTE_UNUSED,
3886 int num_operands ATTRIBUTE_UNUSED)
3888 rtx local_insn;
3890 picochip_current_prescan_insn = insn;
3892 if (TARGET_DEBUG)
3893 printf ("Final prescan on INSN %d with mode %s\n",
3894 INSN_UID (insn), GET_MODE_NAME (GET_MODE (insn)));
3896 /* If this is the start of a new instruction cycle, or no scheduling
3897 is used, then reset the VLIW status. */
3898 if (GET_MODE (insn) == TImode || !picochip_schedule_type == DFA_TYPE_SPEED)
3899 picochip_reset_vliw (insn);
3901 /* No VLIW scheduling occured, so don't go any further. */
3902 if (picochip_schedule_type != DFA_TYPE_SPEED)
3903 return;
3905 /* Look for the next printable instruction. This loop terminates on
3906 any recognisable instruction, and on any unrecognisable
3907 instruction with TImode. */
3908 local_insn = insn;
3909 for (local_insn = NEXT_INSN (local_insn); local_insn;
3910 local_insn = NEXT_INSN (local_insn))
3912 if (NOTE_P (local_insn) || DEBUG_INSN_P(local_insn))
3913 continue;
3914 else if (!INSN_P (local_insn))
3915 break;
3916 else if (GET_MODE (local_insn) == TImode
3917 || INSN_CODE (local_insn) != -1)
3918 break;
3921 /* Set the continuation flag if the next instruction can be packed
3922 with the current instruction (i.e., the next instruction is
3923 valid, and isn't the start of a new cycle). */
3924 picochip_vliw_continuation = (local_insn && NONDEBUG_INSN_P (local_insn) &&
3925 (GET_MODE (local_insn) != TImode));
3929 /* Builtin functions. */
3930 /* Given a builtin function taking 2 operands (i.e., target + source),
3931 emit the RTL for the underlying instruction. */
3932 static rtx
3933 picochip_expand_builtin_2op (enum insn_code icode, tree call, rtx target)
3935 tree arg0;
3936 rtx op0, pat;
3937 enum machine_mode tmode, mode0;
3939 /* Grab the incoming argument and emit its RTL. */
3940 arg0 = CALL_EXPR_ARG (call, 0);
3941 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3943 /* Determine the modes of the instruction operands. */
3944 tmode = insn_data[icode].operand[0].mode;
3945 mode0 = insn_data[icode].operand[1].mode;
3947 /* Ensure that the incoming argument RTL is in a register of the
3948 correct mode. */
3949 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
3950 op0 = copy_to_mode_reg (mode0, op0);
3952 /* If there isn't a suitable target, emit a target register. */
3953 if (target == 0
3954 || GET_MODE (target) != tmode
3955 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
3956 target = gen_reg_rtx (tmode);
3958 /* Emit and return the new instruction. */
3959 pat = GEN_FCN (icode) (target, op0);
3960 if (!pat)
3961 return 0;
3962 emit_insn (pat);
3964 return target;
3968 /* Given a builtin function taking 3 operands (i.e., target + two
3969 source), emit the RTL for the underlying instruction. */
3970 static rtx
3971 picochip_expand_builtin_3op (enum insn_code icode, tree call, rtx target)
3973 tree arg0, arg1;
3974 rtx op0, op1, pat;
3975 enum machine_mode tmode, mode0, mode1;
3977 /* Grab the function's arguments. */
3978 arg0 = CALL_EXPR_ARG (call, 0);
3979 arg1 = CALL_EXPR_ARG (call, 1);
3981 /* Emit rtl sequences for the function arguments. */
3982 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3983 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3985 /* Get the mode's of each of the instruction operands. */
3986 tmode = insn_data[icode].operand[0].mode;
3987 mode0 = insn_data[icode].operand[1].mode;
3988 mode1 = insn_data[icode].operand[2].mode;
3990 /* Ensure that each of the function argument rtl sequences are in a
3991 register of the correct mode. */
3992 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
3993 op0 = copy_to_mode_reg (mode0, op0);
3994 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
3995 op1 = copy_to_mode_reg (mode1, op1);
3997 /* If no target has been given, create a register to use as the target. */
3998 if (target == 0
3999 || GET_MODE (target) != tmode
4000 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
4001 target = gen_reg_rtx (tmode);
4003 /* Emit and return the new instruction. */
4004 pat = GEN_FCN (icode) (target, op0, op1);
4005 if (!pat)
4006 return 0;
4007 emit_insn (pat);
4009 return target;
4013 /* Expand a builtin function which takes two arguments, and returns a void. */
4014 static rtx
4015 picochip_expand_builtin_2opvoid (enum insn_code icode, tree call)
4017 tree arg0, arg1;
4018 rtx op0, op1, pat;
4019 enum machine_mode mode0, mode1;
4021 /* Grab the function's arguments. */
4022 arg0 = CALL_EXPR_ARG (call, 0);
4023 arg1 = CALL_EXPR_ARG (call, 1);
4025 /* Emit rtl sequences for the function arguments. */
4026 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4027 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4029 /* Get the mode's of each of the instruction operands. */
4030 mode0 = insn_data[icode].operand[0].mode;
4031 mode1 = insn_data[icode].operand[1].mode;
4033 /* Ensure that each of the function argument rtl sequences are in a
4034 register of the correct mode. */
4035 if (!(*insn_data[icode].operand[0].predicate) (op0, mode0))
4036 op0 = copy_to_mode_reg (mode0, op0);
4037 if (!(*insn_data[icode].operand[1].predicate) (op1, mode1))
4038 op1 = copy_to_mode_reg (mode1, op1);
4040 /* Emit and return the new instruction. */
4041 pat = GEN_FCN (icode) (op0, op1);
4042 if (!pat)
4043 return 0;
4044 emit_insn (pat);
4046 return NULL_RTX;
4050 /* Expand an array get into the corresponding RTL. */
4051 static rtx
4052 picochip_expand_array_get (tree call, rtx target)
4054 tree arg0, arg1, arg2;
4055 rtx op0, op1, op2, pat;
4057 /* Grab the function's arguments. */
4058 arg0 = CALL_EXPR_ARG (call, 0);
4059 arg1 = CALL_EXPR_ARG (call, 1);
4060 arg2 = CALL_EXPR_ARG (call, 2) ;
4062 /* Emit rtl sequences for the function arguments. */
4063 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4064 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4065 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4067 /* The second and third operands must be constant. Nothing else will
4068 do. */
4069 if (CONST_INT != GET_CODE (op1))
4070 internal_error ("%s: Second source operand is not a constant",
4071 __FUNCTION__);
4072 if (CONST_INT != GET_CODE (op2))
4073 internal_error ("%s: Third source operand is not a constant",
4074 __FUNCTION__);
4076 /* If no target has been given, create a register to use as the target. */
4077 if (target == 0 || GET_MODE (target) != SImode)
4078 target = gen_reg_rtx (SImode);
4080 /* The first operand must be a HImode register or a constant. If it
4081 isn't, force it into a HImode register. */
4082 if (GET_MODE (op0) != HImode || REG != GET_CODE (op0))
4083 op0 = copy_to_mode_reg (HImode, op0);
4086 /* Emit and return the new instruction. */
4087 pat = gen_commsArrayGet (target, op0, op1, op2);
4088 emit_insn (pat);
4090 return target;
4094 /* Expand an array put into the corresponding RTL. */
4095 static rtx
4096 picochip_expand_array_put (tree call, rtx target)
4098 tree arg0, arg1, arg2, arg3;
4099 rtx op0, op1, op2, op3, pat;
4101 /* Grab the function's arguments. */
4102 arg0 = CALL_EXPR_ARG (call, 0);
4103 arg1 = CALL_EXPR_ARG (call, 1);
4104 arg2 = CALL_EXPR_ARG (call, 2);
4105 arg3 = CALL_EXPR_ARG (call, 3);
4107 /* Emit rtl sequences for the function arguments. */
4108 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4109 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4110 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4111 op3 = expand_expr (arg3, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4113 /* The first operand must be an SImode register. */
4114 if (GET_MODE (op0) != SImode || REG != GET_CODE (op0))
4115 op0 = copy_to_mode_reg (SImode, op0);
4117 /* The second (index) operand must be a HImode register, or a
4118 constant. If it isn't, force it into a HImode register. */
4119 if (GET_MODE (op1) != HImode || REG != GET_CODE (op1))
4120 op1 = copy_to_mode_reg (HImode, op1);
4122 /* The remaining operands must be constant. Nothing else will do. */
4123 if (CONST_INT != GET_CODE (op2))
4124 internal_error ("%s: Third source operand is not a constant",
4125 __FUNCTION__);
4126 if (CONST_INT != GET_CODE (op3))
4127 internal_error ("%s: Fourth source operand is not a constant",
4128 __FUNCTION__);
4130 /* Emit and return the new instruction. */
4131 pat = gen_commsArrayPut (op0, op1, op2, op3);
4132 emit_insn (pat);
4134 return target;
4138 /* Expand an array testport into the corresponding RTL. */
4139 static rtx
4140 picochip_expand_array_testport (tree call, rtx target)
4142 tree arg0, arg1, arg2;
4143 rtx op0, op1, op2, pat;
4145 /* Grab the function's arguments. */
4146 arg0 = CALL_EXPR_ARG (call, 0);
4147 arg1 = CALL_EXPR_ARG (call, 1);
4148 arg2 = CALL_EXPR_ARG (call, 2);
4150 /* Emit rtl sequences for the function arguments. */
4151 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4152 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4153 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4155 /* The first operand must be a HImode register, or a constant. If it
4156 isn't, force it into a HImode register. */
4157 if (GET_MODE (op0) != HImode || REG != GET_CODE (op0))
4158 op0 = copy_to_mode_reg (HImode, op0);
4160 /* The second and third operands must be constant. Nothing else will
4161 do. */
4162 if (CONST_INT != GET_CODE (op1))
4163 internal_error ("%s: Second source operand is not a constant",
4164 __FUNCTION__);
4165 if (CONST_INT != GET_CODE (op2))
4166 internal_error ("%s: Third source operand is not a constant",
4167 __FUNCTION__);
4169 /* If no target has been given, create a HImode register to use as
4170 the target. */
4171 if (target == 0 || GET_MODE (target) != HImode)
4172 target = gen_reg_rtx (HImode);
4174 /* Emit and return the new instruction. */
4175 pat = gen_commsArrayTestPort (target, op0, op1, op2);
4176 emit_insn (pat);
4178 return target;
4182 /* Generate a unique HALT instruction by giving the instruction a
4183 unique integer. This integer makes no difference to the assembly
4184 output (other than a comment indicating the supplied id), but the
4185 presence of the unique integer prevents the compiler from combining
4186 several different halt instructions into one instruction. This
4187 means that each use of the halt instruction is unique, which in
4188 turn means that assertions work as expected. */
4189 static rtx
4190 picochip_generate_halt (void)
4192 static int currentId = 0;
4193 rtx insns;
4194 rtx id = GEN_INT (currentId);
4195 currentId += 1;
4197 start_sequence();
4198 emit_insn (gen_halt (id));
4200 /* A barrier is inserted to prevent the compiler from thinking that
4201 it has to continue execution after the HALT.*/
4202 emit_barrier ();
4204 insns = get_insns();
4205 end_sequence();
4206 emit_insn (insns);
4208 return const0_rtx;
4211 /* Initialise the builtin functions. Start by initialising
4212 descriptions of different types of functions (e.g., void fn(int),
4213 int fn(void)), and then use these to define the builtins. */
4214 void
4215 picochip_init_builtins (void)
4217 tree noreturn;
4219 tree int_ftype_int, int_ftype_int_int;
4220 tree long_ftype_int, long_ftype_int_int_int;
4221 tree void_ftype_int_long, int_ftype_int_int_int,
4222 void_ftype_long_int_int_int;
4223 tree void_ftype_void, unsigned_ftype_unsigned;
4225 /* void func (void) */
4226 void_ftype_void = build_function_type_list (void_type_node, NULL_TREE);
4228 /* int func (int) */
4229 int_ftype_int = build_function_type_list (integer_type_node,
4230 integer_type_node, NULL_TREE);
4232 /* unsigned int func (unsigned int) */
4233 unsigned_ftype_unsigned
4234 = build_function_type_list (unsigned_type_node,
4235 unsigned_type_node, NULL_TREE);
4237 /* int func(int, int) */
4238 int_ftype_int_int
4239 = build_function_type_list (integer_type_node,
4240 integer_type_node, integer_type_node,
4241 NULL_TREE);
4243 /* long func(int) */
4244 long_ftype_int = build_function_type_list (long_integer_type_node,
4245 integer_type_node, NULL_TREE);
4247 /* long func(int, int, int) */
4248 long_ftype_int_int_int
4249 = build_function_type_list (long_integer_type_node,
4250 integer_type_node, integer_type_node,
4251 integer_type_node, NULL_TREE);
4253 /* int func(int, int, int) */
4254 int_ftype_int_int_int
4255 = build_function_type_list (integer_type_node,
4256 integer_type_node, integer_type_node,
4257 integer_type_node, NULL_TREE);
4259 /* void func(int, long) */
4260 void_ftype_int_long
4261 = build_function_type_list (void_type_node,
4262 integer_type_node, long_integer_type_node,
4263 NULL_TREE);
4265 /* void func(long, int, int, int) */
4266 void_ftype_long_int_int_int
4267 = build_function_type_list (void_type_node,
4268 long_integer_type_node, integer_type_node,
4269 integer_type_node, integer_type_node,
4270 NULL_TREE);
4272 /* Initialise the sign-bit-count function. */
4273 add_builtin_function ("__builtin_sbc", int_ftype_int,
4274 PICOCHIP_BUILTIN_SBC, BUILT_IN_MD, NULL,
4275 NULL_TREE);
4276 add_builtin_function ("picoSbc", int_ftype_int, PICOCHIP_BUILTIN_SBC,
4277 BUILT_IN_MD, NULL, NULL_TREE);
4279 /* Initialise the bit reverse function. */
4280 add_builtin_function ("__builtin_brev", unsigned_ftype_unsigned,
4281 PICOCHIP_BUILTIN_BREV, BUILT_IN_MD, NULL,
4282 NULL_TREE);
4283 add_builtin_function ("picoBrev", unsigned_ftype_unsigned,
4284 PICOCHIP_BUILTIN_BREV, BUILT_IN_MD, NULL,
4285 NULL_TREE);
4287 /* Initialise the byte swap function. */
4288 add_builtin_function ("__builtin_byteswap", unsigned_ftype_unsigned,
4289 PICOCHIP_BUILTIN_BYTESWAP, BUILT_IN_MD, NULL,
4290 NULL_TREE);
4291 add_builtin_function ("picoByteSwap", unsigned_ftype_unsigned,
4292 PICOCHIP_BUILTIN_BYTESWAP, BUILT_IN_MD, NULL,
4293 NULL_TREE);
4295 /* Initialise the ASRI function (note that while this can be coded
4296 using a signed shift in C, extra scratch registers are required,
4297 which we avoid by having a direct builtin to map to the
4298 instruction). */
4299 add_builtin_function ("__builtin_asri", int_ftype_int_int,
4300 PICOCHIP_BUILTIN_ASRI, BUILT_IN_MD, NULL,
4301 NULL_TREE);
4303 /* Initialise saturating addition. */
4304 add_builtin_function ("__builtin_adds", int_ftype_int_int,
4305 PICOCHIP_BUILTIN_ADDS, BUILT_IN_MD, NULL,
4306 NULL_TREE);
4307 add_builtin_function ("picoAdds", int_ftype_int_int,
4308 PICOCHIP_BUILTIN_ADDS, BUILT_IN_MD, NULL,
4309 NULL_TREE);
4311 /* Initialise saturating subtraction. */
4312 add_builtin_function ("__builtin_subs", int_ftype_int_int,
4313 PICOCHIP_BUILTIN_SUBS, BUILT_IN_MD, NULL,
4314 NULL_TREE);
4315 add_builtin_function ("picoSubs", int_ftype_int_int,
4316 PICOCHIP_BUILTIN_SUBS, BUILT_IN_MD, NULL,
4317 NULL_TREE);
4319 /* Scalar comms builtins. */
4320 add_builtin_function ("__builtin_get", long_ftype_int,
4321 PICOCHIP_BUILTIN_GET, BUILT_IN_MD, NULL,
4322 NULL_TREE);
4323 add_builtin_function ("__builtin_put", void_ftype_int_long,
4324 PICOCHIP_BUILTIN_PUT, BUILT_IN_MD, NULL,
4325 NULL_TREE);
4326 add_builtin_function ("__builtin_testport", int_ftype_int,
4327 PICOCHIP_BUILTIN_TESTPORT, BUILT_IN_MD, NULL,
4328 NULL_TREE);
4330 /* Array comms builtins. */
4331 add_builtin_function ("__builtin_put_array",
4332 void_ftype_long_int_int_int,
4333 PICOCHIP_BUILTIN_PUT_ARRAY, BUILT_IN_MD, NULL,
4334 NULL_TREE);
4335 add_builtin_function ("__builtin_get_array", long_ftype_int_int_int,
4336 PICOCHIP_BUILTIN_GET_ARRAY, BUILT_IN_MD, NULL,
4337 NULL_TREE);
4338 add_builtin_function ("__builtin_testport_array",
4339 int_ftype_int_int_int,
4340 PICOCHIP_BUILTIN_TESTPORT_ARRAY, BUILT_IN_MD,
4341 NULL, NULL_TREE);
4343 /* Halt instruction. Note that the builtin function is marked as
4344 having the attribute `noreturn' so that the compiler realises
4345 that the halt stops the program dead. */
4346 noreturn = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4347 add_builtin_function ("__builtin_halt", void_ftype_void,
4348 PICOCHIP_BUILTIN_HALT, BUILT_IN_MD, NULL,
4349 noreturn);
4350 add_builtin_function ("picoHalt", void_ftype_void,
4351 PICOCHIP_BUILTIN_HALT, BUILT_IN_MD, NULL,
4352 noreturn);
4356 /* Expand a call to a builtin function. */
4358 picochip_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
4359 enum machine_mode mode ATTRIBUTE_UNUSED,
4360 int ignore ATTRIBUTE_UNUSED)
4362 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4363 int fcode = DECL_FUNCTION_CODE (fndecl);
4365 switch (fcode)
4367 case PICOCHIP_BUILTIN_ASRI:
4368 return picochip_expand_builtin_3op (CODE_FOR_builtin_asri, exp,
4369 target);
4371 case PICOCHIP_BUILTIN_ADDS:
4372 return picochip_expand_builtin_3op (CODE_FOR_sataddhi3, exp,
4373 target);
4375 case PICOCHIP_BUILTIN_SUBS:
4376 return picochip_expand_builtin_3op (CODE_FOR_satsubhi3, exp,
4377 target);
4379 case PICOCHIP_BUILTIN_SBC:
4380 return picochip_expand_builtin_2op (CODE_FOR_sbc, exp, target);
4382 case PICOCHIP_BUILTIN_BREV:
4383 return picochip_expand_builtin_2op (CODE_FOR_brev, exp, target);
4385 case PICOCHIP_BUILTIN_BYTESWAP:
4386 return picochip_expand_builtin_2op (CODE_FOR_bswaphi2, exp, target);
4388 case PICOCHIP_BUILTIN_GET:
4389 return picochip_expand_builtin_2op (CODE_FOR_commsGet, exp, target);
4391 case PICOCHIP_BUILTIN_PUT:
4392 return picochip_expand_builtin_2opvoid (CODE_FOR_commsPut, exp);
4394 case PICOCHIP_BUILTIN_TESTPORT:
4395 return picochip_expand_builtin_2op (CODE_FOR_commsTestPort, exp,
4396 target);
4398 case PICOCHIP_BUILTIN_PUT_ARRAY:
4399 return picochip_expand_array_put (exp, target);
4401 case PICOCHIP_BUILTIN_GET_ARRAY:
4402 return picochip_expand_array_get (exp, target);
4404 case PICOCHIP_BUILTIN_TESTPORT_ARRAY:
4405 return picochip_expand_array_testport (exp, target);
4407 case PICOCHIP_BUILTIN_HALT:
4408 return picochip_generate_halt ();
4410 default:
4411 gcc_unreachable();
4415 /* Should really do something sensible here. */
4416 return NULL_RTX;
4419 /* Emit warnings. */
4420 static void
4421 picochip_warn_inefficient (const char *msg)
4423 if (TARGET_INEFFICIENT_WARNINGS)
4424 warning (OPT_minefficient_warnings,
4425 "%s (disable warning using -mno-inefficient-warnings)", msg);
4428 void
4429 warn_of_byte_access (void)
4431 static int warned = 0;
4433 if (!warned)
4435 picochip_warn_inefficient
4436 ("byte access is synthesised - consider using MUL AE");
4437 warned = 1;
4443 picochip_function_value (const_tree valtype, const_tree func,
4444 bool outgoing ATTRIBUTE_UNUSED)
4446 enum machine_mode mode = TYPE_MODE (valtype);
4447 int unsignedp = TYPE_UNSIGNED (valtype);
4449 /* Since we define PROMOTE_FUNCTION_RETURN, we must promote the mode
4450 just as PROMOTE_MODE does. */
4451 mode = promote_function_mode (valtype, mode, &unsignedp, func, 1);
4453 return gen_rtx_REG (mode, 0);
4457 /* Check that the value of the given mode will fit in the register of
4458 the given mode. */
4460 picochip_hard_regno_mode_ok (int regno, enum machine_mode mode)
4463 if (GET_MODE_CLASS (mode) == MODE_CC)
4464 return regno == CC_REGNUM;
4466 /* If the CC register is being used, then only CC mode values are
4467 allowed (which have already been tested). */
4468 if (regno == CC_REGNUM || regno == ACC_REGNUM)
4469 return 0;
4471 /* Must be a valid register. */
4472 if (regno > 16)
4473 return 0;
4475 /* Modes QI and HI may be placed in any register except the CC. */
4476 if (mode == QImode || mode == HImode)
4477 return 1;
4479 /* DI must be in a quad register. */
4480 if (mode == DImode)
4481 return (regno % 4 == 0);
4483 /* All other modes must be placed in a even numbered register. */
4484 return !(regno & 1);
4488 /* Extract the lower and upper components of a constant value. */
4491 picochip_get_low_const (rtx value)
4493 return gen_int_mode (INTVAL (value) & 0xFFFF, HImode);
4497 picochip_get_high_const (rtx value)
4499 /*return GEN_INT ((((INTVAL (value) >> 16) & 0xFFFF) ^ 0x8000) - 0x8000); */
4500 return gen_int_mode ((INTVAL (value) >> 16) & 0xFFFF, HImode);
4504 /* Loading and storing QImode values to and from memory in a machine
4505 without byte access requires might require a scratch
4506 register. However, the scratch register might correspond to the
4507 register in which the value is being loaded. To ensure that a
4508 scratch register is supplied which is definitely different to the
4509 output register, request a register pair. This effectively gives a
4510 choice of two registers to choose from, so that we a guaranteed to
4511 get at least one register which is different to the output
4512 register. This trick is taken from the alpha implementation. */
4513 static reg_class_t
4514 picochip_secondary_reload (bool in_p,
4515 rtx x ATTRIBUTE_UNUSED,
4516 reg_class_t cla ATTRIBUTE_UNUSED,
4517 enum machine_mode mode,
4518 secondary_reload_info *sri)
4520 if (mode == QImode && !TARGET_HAS_BYTE_ACCESS)
4522 if (in_p == 0)
4523 sri->icode = CODE_FOR_reload_outqi;
4524 else
4525 sri->icode = CODE_FOR_reload_inqi;
4528 /* We dont need to return a register class type when we need only a
4529 scratch register. It realizes the scratch register type by looking
4530 at the instruction definition for sri->icode. We only need to
4531 return the register type when we need intermediaries for copies.*/
4532 return NO_REGS;
4535 /* Return true if the given memory operand can be aligned to a
4536 word+offset memory reference (e.g., FP+3 can be converted into the
4537 memory operand FP+2, with the offset 1). */
4539 picochip_alignable_memory_operand (rtx mem_operand,
4540 enum machine_mode mode ATTRIBUTE_UNUSED)
4542 rtx address;
4544 /* Not a mem operand. Refuse immediately. */
4545 if (MEM != GET_CODE (mem_operand))
4546 return 0;
4548 address = XEXP (mem_operand, 0);
4550 /* Return true if a PLUS of the SP and a (valid) constant, or SP itself. */
4551 return ((PLUS == GET_CODE (address) &&
4552 REGNO (XEXP (address, 0)) == STACK_POINTER_REGNUM &&
4553 CONST_INT == GET_CODE (XEXP (address, 1)) &&
4554 picochip_const_ok_for_letter_p (INTVAL (XEXP (address, 1)), 'K'))
4555 || (REG == GET_CODE (address)
4556 && REGNO (address) == STACK_POINTER_REGNUM));
4560 /* Return true if the given memory reference is to a word aligned
4561 address. Currently this means it must be either SP, or
4562 SP+offset. We could replace this function with alignable
4563 memory references in the above function?. */
4565 picochip_word_aligned_memory_reference (rtx operand)
4569 /* The address must be the SP register, or a constant, aligned
4570 offset from SP which doesn't exceed the FP+offset
4571 restrictions. */
4572 return ((PLUS == GET_CODE (operand)
4573 && REGNO (XEXP (operand, 0)) == STACK_POINTER_REGNUM
4574 && picochip_is_aligned (INTVAL (XEXP (operand, 1)), 16)
4575 && picochip_const_ok_for_letter_p (INTVAL (XEXP (operand, 1)),
4576 'K'))
4577 || (REG == GET_CODE (operand)
4578 && REGNO (operand) == STACK_POINTER_REGNUM));
4582 /* Given an alignable memory location, convert the memory location
4583 into a HI mode access, storing the new memory reference in
4584 paligned_mem, and the number of bits by which to shift in pbitnum
4585 (i.e., given a reference to FP+3, this creates an aligned reference
4586 of FP+2, with an 8-bit shift). This code is a modification of that
4587 found in the Alpha port. */
4588 void
4589 picochip_get_hi_aligned_mem (rtx ref, rtx * paligned_mem, rtx * pbitnum)
4591 rtx base;
4592 HOST_WIDE_INT offset = 0;
4594 gcc_assert (GET_CODE (ref) == MEM);
4596 if (reload_in_progress && !memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
4598 base = find_replacement (&XEXP (ref, 0));
4600 gcc_assert(memory_address_p (GET_MODE (ref), base));
4602 else
4604 base = XEXP (ref, 0);
4607 if (GET_CODE (base) == PLUS)
4609 offset += INTVAL (XEXP (base, 1));
4610 base = XEXP (base, 0);
4613 *paligned_mem = widen_memory_access (ref, HImode, (offset & ~1) - offset);
4615 if (offset > 0)
4617 if (TARGET_DEBUG)
4619 printf
4620 ("Found non-zero offset in get_hi_aligned_mem - check that the correct value is being used (as this functionality hasn't been exploited yet).\n");
4624 *pbitnum = GEN_INT ((offset & 1) * 8);
4628 /* Return true if the given operand is an absolute address in memory
4629 (i.e., a symbolic offset). */
4631 picochip_absolute_memory_operand (rtx op,
4632 enum machine_mode mode ATTRIBUTE_UNUSED)
4635 if (MEM == GET_CODE (op))
4637 rtx address = XEXP (op, 0);
4639 /* Symbols are valid absolute addresses. */
4640 if (SYMBOL_REF == GET_CODE (address))
4641 return 1;
4643 /* Constant offsets to symbols are valid absolute addresses. */
4644 if (CONST == GET_CODE (address) &&
4645 PLUS == GET_CODE (XEXP (address, 0)) &&
4646 SYMBOL_REF == GET_CODE (XEXP (XEXP (address, 0), 0)) &&
4647 CONST_INT == GET_CODE (XEXP (XEXP (address, 0), 1)))
4648 return 1;
4651 else
4652 return 0;
4654 /* Symbols are valid absolute addresses. */
4655 if (SYMBOL_REF == GET_CODE (XEXP (op, 0)))
4656 return 1;
4659 return 0;
4663 void
4664 picochip_asm_named_section (const char *name,
4665 unsigned int flags ATTRIBUTE_UNUSED,
4666 tree decl ATTRIBUTE_UNUSED)
4668 fprintf (asm_out_file, ".section %s\n", name);
4672 /* Check if we can make a conditional copy instruction. This is emitted as an
4673 instruction to set the condition register, followed by an instruction which
4674 uses the condition registers to perform the conditional move. */
4676 picochip_check_conditional_copy (rtx * operands)
4679 rtx branch_op_0 = XEXP (operands[1], 0);
4680 rtx branch_op_1 = XEXP (operands[1], 1);
4682 /* Only HI mode conditional moves are currently allowed. Can we add
4683 SI mode moves? */
4684 if (GET_CODE (operands[1]) != EQ && GET_CODE (operands[1]) != NE)
4685 return 0;
4687 /* Is the comparison valid? Only allow operands which are registers
4688 if they are HImode. SI mode comparisons against 0 could be
4689 handled using logical operations (e.g., SIreg != 0 when low ||
4690 high). Need to find test cases to provoke this though (fixunssfdi
4691 in libgcc does, but is complicated). */
4692 if (register_operand(branch_op_0, GET_MODE(branch_op_0)) &&
4693 GET_MODE(branch_op_0) != HImode)
4694 return 0;
4695 if (register_operand(branch_op_1, GET_MODE(branch_op_1)) &&
4696 GET_MODE(branch_op_1) != HImode)
4697 return 0;
4699 return 1;
4704 static rtx
4705 picochip_static_chain (const_tree ARG_UNUSED (fndecl), bool incoming_p)
4707 rtx addr;
4708 if (incoming_p)
4709 addr = arg_pointer_rtx;
4710 else
4711 addr = plus_constant (stack_pointer_rtx, -2 * UNITS_PER_WORD);
4712 return gen_frame_mem (Pmode, addr);