Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / config / picochip / picochip.c
blob20a5b37deddc4b7cc2329979bda2b02664732ddc
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 "toplev.h"
46 #include "ggc.h"
47 #include "hashtab.h"
48 #include "tm_p.h"
49 #include "target.h"
50 #include "target-def.h"
51 #include "langhooks.h"
52 #include "reload.h"
53 #include "params.h"
55 #include "picochip-protos.h"
57 #include "insn-attr.h" /* For DFA state_t. */
58 #include "insn-config.h" /* Required by recog.h */
59 #include "insn-codes.h" /* For CODE_FOR_? */
60 #include "optabs.h" /* For GEN_FCN */
61 #include "basic-block.h" /* UPDATE_LIFE_GLOBAL* for picochip_reorg. */
62 #include "timevar.h" /* For TV_SCHED2, in picochip_reorg. */
63 #include "libfuncs.h" /* For memcpy_libfuncs, etc. */
64 #include "df.h" /* For df_regs_ever_live_df_regs_ever_live_pp, etc. */
67 /* Target AE ISA information. */
68 enum picochip_dfa_type picochip_schedule_type;
70 bool picochip_has_mul_unit = false;
71 bool picochip_has_mac_unit = false;
73 /* targetm hook function prototypes. */
75 void picochip_asm_file_start (void);
76 void picochip_asm_file_end (void);
78 void picochip_init_libfuncs (void);
79 void picochip_reorg (void);
81 int picochip_arg_partial_bytes (CUMULATIVE_ARGS * p_cum,
82 enum machine_mode mode,
83 tree type, bool named);
84 rtx picochip_function_arg (CUMULATIVE_ARGS * p_cum,
85 enum machine_mode mode,
86 const_tree type, bool named);
87 rtx picochip_incoming_function_arg (CUMULATIVE_ARGS * p_cum,
88 enum machine_mode mode,
89 const_tree type, bool named);
90 void picochip_arg_advance (CUMULATIVE_ARGS * p_cum, enum machine_mode mode,
91 const_tree type, bool named);
92 unsigned int picochip_function_arg_boundary (enum machine_mode mode,
93 const_tree type);
95 int picochip_sched_lookahead (void);
96 int picochip_sched_issue_rate (void);
97 int picochip_sched_adjust_cost (rtx insn, rtx link,
98 rtx dep_insn, int cost);
99 int picochip_sched_reorder (FILE * file, int verbose, rtx * ready,
100 int *n_readyp, int clock);
102 void picochip_init_builtins (void);
103 rtx picochip_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
105 bool picochip_rtx_costs (rtx x, int code, int outer_code, int* total, bool speed);
106 bool picochip_return_in_memory(const_tree type,
107 const_tree fntype ATTRIBUTE_UNUSED);
108 bool picochip_legitimate_address_p (enum machine_mode, rtx, bool);
109 rtx picochip_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
110 enum machine_mode mode);
111 int picochip_legitimize_reload_address (rtx *x, enum machine_mode mode,
112 int opnum, int type, int ind_levels);
114 rtx picochip_struct_value_rtx(tree fntype ATTRIBUTE_UNUSED, int incoming ATTRIBUTE_UNUSED);
115 rtx picochip_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
116 bool outgoing ATTRIBUTE_UNUSED);
117 static reg_class_t
118 picochip_secondary_reload (bool in_p,
119 rtx x ATTRIBUTE_UNUSED,
120 reg_class_t cla ATTRIBUTE_UNUSED,
121 enum machine_mode mode,
122 secondary_reload_info *sri);
123 void
124 picochip_asm_named_section (const char *name,
125 unsigned int flags ATTRIBUTE_UNUSED,
126 tree decl ATTRIBUTE_UNUSED);
128 static rtx picochip_static_chain (const_tree, bool);
130 static void picochip_option_override (void);
132 /* Lookup table mapping a register number to the earliest containing
133 class. Used by REGNO_REG_CLASS. */
134 const enum reg_class picochip_regno_reg_class[FIRST_PSEUDO_REGISTER] =
136 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
137 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
138 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
139 GR_REGS, FRAME_REGS, PTR_REGS, CONST_REGS,
140 ACC_REGS, CC_REGS, GR_REGS, GR_REGS
143 /* picoChip register names. */
144 const char *picochip_regnames[] = REGISTER_NAMES;
146 /* Define the maximum number of registers which may be used to pass
147 * parameters to functions. */
148 #define MAX_CALL_PARAMETER_REGS 6
151 /* Target scheduling information. */
153 /* Determine whether we run our final scheduling pass or not. We always
154 avoid the normal second scheduling pass. */
155 int picochip_flag_schedule_insns2;
157 /* Check if variable tracking needs to be run. */
158 int picochip_flag_var_tracking;
160 /* This flag indicates whether the next instruction to be output is a
161 VLIW continuation instruction. It is used to communicate between
162 final_prescan_insn and asm_output_opcode. */
163 static int picochip_vliw_continuation = 0;
165 /* This variable is used to communicate the current instruction
166 between final_prescan_insn and functions such as asm_output_opcode,
167 and picochip_get_vliw_alu_id (which are otherwise unable to determine the
168 current instruction. */
169 static rtx picochip_current_prescan_insn;
171 static bool picochip_is_delay_slot_pending = 0;
173 /* When final_prescan_insn is called, it computes information about
174 the current VLIW packet, and stores it in this structure. When
175 instructions are output, this state is used to make sure that the
176 instructions are output in the correct way (e.g., which ALU to use,
177 whether a macro branch was ever previously a real branch, etc.). */
178 struct vliw_state
180 int contains_pico_alu_insn;
181 int contains_non_cc_alu_insn;
182 int num_alu_insns_so_far;
184 /* Record how many instructions are contained in the packet. */
185 int num_insns_in_packet;
187 /* There was a case for this to be more than 1 */
188 int num_cfi_labels_deferred;
189 char cfi_label_name[2][256]; /* Used to record the name of a CFI label
190 emitted inside a VLIW packet. */
191 char lm_label_name[256]; /* Used to record the name of an LM label. */
194 struct vliw_state picochip_current_vliw_state;
196 /* Save/restore recog_data. */
197 static int picochip_saved_which_alternative;
198 static struct recog_data picochip_saved_recog_data;
200 /* Determine which ALU to use for the instruction in
201 picochip_current_prescan_insn. */
202 static char picochip_get_vliw_alu_id (void);
204 /* Implement TARGET_OPTION_OPTIMIZATION_TABLE. */
205 static const struct default_options picochip_option_optimization_table[] =
207 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
208 { OPT_LEVELS_NONE, 0, NULL, 0 }
211 /* Initialize the GCC target structure. */
213 #undef TARGET_ASM_FUNCTION_PROLOGUE
214 #define TARGET_ASM_FUNCTION_PROLOGUE picochip_function_prologue
216 #undef TARGET_ASM_FUNCTION_EPILOGUE
217 #define TARGET_ASM_FUNCTION_EPILOGUE picochip_function_epilogue
219 #undef TARGET_ASM_INTERNAL_LABEL
220 #define TARGET_ASM_INTERNAL_LABEL picochip_output_internal_label
222 #undef TARGET_ASM_GLOBALIZE_LABEL
223 #define TARGET_ASM_GLOBALIZE_LABEL picochip_output_global
225 #undef TARGET_ASM_BYTE_OP
226 #define TARGET_ASM_BYTE_OP ".initByte "
227 #undef TARGET_ASM_ALIGNED_HI_OP
228 #define TARGET_ASM_ALIGNED_HI_OP ".initWord "
229 #undef TARGET_ASM_UNALIGNED_HI_OP
230 #define TARGET_ASM_UNALIGNED_HI_OP ".unalignedInitWord "
231 #undef TARGET_ASM_ALIGNED_SI_OP
232 #define TARGET_ASM_ALIGNED_SI_OP ".initLong "
233 #undef TARGET_ASM_UNALIGNED_SI_OP
234 #define TARGET_ASM_UNALIGNED_SI_OP ".unalignedInitLong "
236 #undef TARGET_INIT_BUILTINS
237 #define TARGET_INIT_BUILTINS picochip_init_builtins
239 #undef TARGET_EXPAND_BUILTIN
240 #define TARGET_EXPAND_BUILTIN picochip_expand_builtin
242 #undef TARGET_RTX_COSTS
243 #define TARGET_RTX_COSTS picochip_rtx_costs
245 #undef TARGET_SCHED_ISSUE_RATE
246 #define TARGET_SCHED_ISSUE_RATE picochip_sched_issue_rate
248 #undef TARGET_SCHED_REORDER
249 #define TARGET_SCHED_REORDER picochip_sched_reorder
251 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
252 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
253 picochip_sched_lookahead
255 #undef TARGET_SCHED_ADJUST_COST
256 #define TARGET_SCHED_ADJUST_COST picochip_sched_adjust_cost
258 #undef TARGET_ASM_NAMED_SECTION
259 #define TARGET_ASM_NAMED_SECTION picochip_asm_named_section
261 #undef TARGET_HAVE_NAMED_SECTIONS
262 #define TARGET_HAVE_NAMED_SECTIONS 1
264 #undef TARGET_HAVE_SWITCHABLE_BSS_SECTIONS
265 #define TARGET_HAVE_SWITCHABLE_BSS_SECTIONS 1
267 #undef TARGET_INIT_LIBFUNCS
268 #define TARGET_INIT_LIBFUNCS picochip_init_libfuncs
270 #undef TARGET_ASM_FILE_START
271 #define TARGET_ASM_FILE_START picochip_asm_file_start
273 #undef TARGET_ASM_FILE_END
274 #define TARGET_ASM_FILE_END picochip_asm_file_end
276 #undef TARGET_MACHINE_DEPENDENT_REORG
277 #define TARGET_MACHINE_DEPENDENT_REORG picochip_reorg
279 #undef TARGET_ARG_PARTIAL_BYTES
280 #define TARGET_ARG_PARTIAL_BYTES picochip_arg_partial_bytes
282 #undef TARGET_FUNCTION_ARG
283 #define TARGET_FUNCTION_ARG picochip_function_arg
285 #undef TARGET_FUNCTION_INCOMING_ARG
286 #define TARGET_FUNCTION_INCOMING_ARG picochip_incoming_function_arg
288 #undef TARGET_FUNCTION_ARG_ADVANCE
289 #define TARGET_FUNCTION_ARG_ADVANCE picochip_arg_advance
291 #undef TARGET_FUNCTION_ARG_BOUNDARY
292 #define TARGET_FUNCTION_ARG_BOUNDARY picochip_function_arg_boundary
294 #undef TARGET_PROMOTE_FUNCTION_MODE
295 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
296 #undef TARGET_PROMOTE_PROTOTYPES
297 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
299 /* Target support for Anchored Addresses optimization */
300 #undef TARGET_MIN_ANCHOR_OFFSET
301 #define TARGET_MIN_ANCHOR_OFFSET 0
302 #undef TARGET_MAX_ANCHOR_OFFSET
303 #define TARGET_MAX_ANCHOR_OFFSET 7
304 #undef TARGET_ASM_OUTPUT_ANCHOR
305 #define TARGET_ASM_OUTPUT_ANCHOR picochip_asm_output_anchor
307 #undef TARGET_FUNCTION_VALUE
308 #define TARGET_FUNCTION_VALUE picochip_function_value
310 #undef TARGET_LIBGCC_CMP_RETURN_MODE
311 #define TARGET_LIBGCC_CMP_RETURN_MODE picochip_libgcc_cmp_return_mode
314 #undef TARGET_LEGITIMATE_ADDRESS_P
315 #define TARGET_LEGITIMATE_ADDRESS_P picochip_legitimate_address_p
317 #undef TARGET_LEGITIMIZE_ADDRESS
318 #define TARGET_LEGITIMIZE_ADDRESS picochip_legitimize_address
320 /* Loading and storing QImode values to and from memory
321 usually requires a scratch register. */
322 #undef TARGET_SECONDARY_RELOAD
323 #define TARGET_SECONDARY_RELOAD picochip_secondary_reload
324 #undef DONT_USE_BUILTIN_SETJMP
325 #define DONT_USE_BUILTIN_SETJMP 1
327 /* How Large Values are Returned */
329 #undef TARGET_RETURN_IN_MEMORY
330 #define TARGET_RETURN_IN_MEMORY picochip_return_in_memory
332 #undef TARGET_STATIC_CHAIN
333 #define TARGET_STATIC_CHAIN picochip_static_chain
335 #undef TARGET_OPTION_OVERRIDE
336 #define TARGET_OPTION_OVERRIDE picochip_option_override
338 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
339 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE picochip_option_override
341 #undef TARGET_OPTION_OPTIMIZATION_TABLE
342 #define TARGET_OPTION_OPTIMIZATION_TABLE picochip_option_optimization_table
344 #undef TARGET_EXCEPT_UNWIND_INFO
345 #define TARGET_EXCEPT_UNWIND_INFO sjlj_except_unwind_info
347 struct gcc_target targetm = TARGET_INITIALIZER;
350 /* Only return a value in memory if it is greater than 4 bytes.
351 int_size_in_bytes returns -1 for variable size objects, which go in
352 memory always. The cast to unsigned makes -1 > 8. */
354 bool
355 picochip_return_in_memory(const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
357 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 4);
360 /* Allow some options to be overriden. In particular, the 2nd
361 scheduling pass option is switched off, and a machine dependent
362 reorganisation ensures that it is run later on, after the second
363 jump optimisation. */
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 /* Turn off the second scheduling pass, and move it to
401 picochip_reorg, to avoid having the second jump optimisation
402 trash the instruction modes (e.g., instructions are changed to
403 TImode to mark the beginning of cycles). Two types of DFA
404 scheduling are possible: space and speed. In both cases,
405 instructions are reordered to avoid stalls (e.g., memory loads
406 stall for one cycle). Speed scheduling will also enable VLIW
407 instruction packing. VLIW instructions use more code space, so
408 VLIW scheduling is disabled when scheduling for size. */
409 picochip_flag_schedule_insns2 = flag_schedule_insns_after_reload;
410 flag_schedule_insns_after_reload = 0;
411 if (picochip_flag_schedule_insns2)
413 if (optimize_size)
414 picochip_schedule_type = DFA_TYPE_SPACE;
415 else
417 picochip_schedule_type = DFA_TYPE_SPEED;
418 flag_delayed_branch = 0;
421 else
422 picochip_schedule_type = DFA_TYPE_NONE;
424 /* Ensure that the debug level is always at least -g2. The flow
425 analyser works at its best if it always has debug
426 information. DWARF is non-intrusive, so it makes no difference to
427 code quality if debug is always enabled. */
428 if (debug_info_level < DINFO_LEVEL_NORMAL)
430 debug_info_level = DINFO_LEVEL_NORMAL;
431 write_symbols = DWARF2_DEBUG;
434 /* Options of the form -mae=mac, and so on will be substituted by
435 the compiler driver for the appropriate byte access and multiply
436 unit ISA options. Any unrecognised AE types will end up being
437 passed to the compiler, which should reject them as invalid. */
438 if (picochip_ae_type_string != NULL)
439 error ("invalid AE type specified (%s)", picochip_ae_type_string);
441 /* Override any specific capabilities of the instruction set. These
442 take precedence over any capabilities inferred from the AE type,
443 regardless of where the options appear on the command line. */
444 if (picochip_mul_type_string == NULL)
446 /* Default to MEM-type multiply, for historical compatibility. */
447 picochip_has_mac_unit = false;
448 picochip_has_mul_unit = true;
450 else
452 picochip_has_mac_unit = false;
453 picochip_has_mul_unit = false;
455 if (strcmp (picochip_mul_type_string, "mul") == 0)
456 picochip_has_mul_unit = true;
457 else if (strcmp (picochip_mul_type_string, "mac") == 0)
458 picochip_has_mac_unit = true;
459 else if (strcmp (picochip_mul_type_string, "none") == 0)
460 { /* Do nothing. Unit types already set to false. */ }
461 else
462 error ("invalid mul type specified (%s) - expected mac, mul or none",
463 picochip_mul_type_string);
469 /* Initialise the library functions to handle arithmetic on some of
470 the larger modes. */
471 void
472 picochip_init_libfuncs (void)
474 /* 64-bit shifts */
475 set_optab_libfunc (ashr_optab, DImode, "__ashrdi3");
476 set_optab_libfunc (ashl_optab, DImode, "__ashldi3");
477 set_optab_libfunc (lshr_optab, DImode, "__lshrdi3");
479 /* 64-bit signed multiplication. */
480 set_optab_libfunc (smul_optab, DImode, "__muldi3");
482 /* Signed division */
483 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
484 set_optab_libfunc (sdiv_optab, DImode, "__divdi3");
486 /* Signed modulus */
487 set_optab_libfunc (smod_optab, HImode, "__modhi3");
488 set_optab_libfunc (smod_optab, DImode, "__moddi3");
490 /* 32-bit count leading Zeros*/
491 set_optab_libfunc (clz_optab, SImode, "_clzsi2");
493 /* 64-bit comparison */
494 set_optab_libfunc (ucmp_optab, DImode, "__ucmpdi2");
495 set_optab_libfunc (cmp_optab, DImode, "__cmpdi2");
497 /* 64-bit addition and subtraction*/
498 set_optab_libfunc (add_optab, DImode, "_adddi3");
499 set_optab_libfunc (sub_optab, DImode, "_subdi3");
502 /* Memcpy function */
504 picochip_expand_movmemhi (rtx *operands)
506 rtx src_addr_reg, dst_addr_reg, count_reg, src_mem, dst_mem, tmp_reg;
507 rtx start_label;
508 int align, size;
509 src_addr_reg = gen_reg_rtx(HImode);
510 dst_addr_reg = gen_reg_rtx(HImode);
511 count_reg = gen_reg_rtx(HImode);
512 emit_insn (gen_movhi (count_reg, operands[2]));
513 emit_insn (gen_movqi (src_addr_reg, XEXP(operands[1], 0)));
514 emit_insn (gen_movqi (dst_addr_reg, XEXP(operands[0], 0)));
515 gcc_assert (GET_CODE(count_reg) == REG);
516 start_label = gen_label_rtx ();
517 emit_label (start_label);
519 /* We can specialise the code for different alignments */
520 align = INTVAL(operands[3]);
521 size = INTVAL(operands[2]);
522 gcc_assert(align >= 0 && size >= 0);
523 if (size != 0)
525 if (size % 4 == 0 && align % 4 == 0)
527 src_mem = gen_rtx_MEM(SImode, src_addr_reg);
528 dst_mem = gen_rtx_MEM(SImode, dst_addr_reg);
529 tmp_reg = gen_reg_rtx(SImode);
530 emit_insn (gen_movsi (tmp_reg, src_mem));
531 emit_insn (gen_movsi (dst_mem, tmp_reg));
532 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, GEN_INT(4)));
533 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, GEN_INT(4)));
534 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-4)));
535 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
536 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
538 else if (size % 2 == 0 && align % 2 == 0)
540 src_mem = gen_rtx_MEM(HImode, src_addr_reg);
541 dst_mem = gen_rtx_MEM(HImode, dst_addr_reg);
542 tmp_reg = gen_reg_rtx(HImode);
543 emit_insn (gen_movhi (tmp_reg, src_mem));
544 emit_insn (gen_movhi (dst_mem, tmp_reg));
545 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, const2_rtx));
546 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, const2_rtx));
547 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-2)));
548 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
549 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
551 else
553 src_mem = gen_rtx_MEM(QImode, src_addr_reg);
554 dst_mem = gen_rtx_MEM(QImode, dst_addr_reg);
555 tmp_reg = gen_reg_rtx(QImode);
556 emit_insn (gen_movqi (tmp_reg, src_mem));
557 emit_insn (gen_movqi (dst_mem, tmp_reg));
558 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, const1_rtx));
559 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, const1_rtx));
560 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-1)));
561 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
562 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
565 return 1;
569 /* Return the register class for letter C. */
570 enum reg_class
571 picochip_reg_class_from_letter (unsigned c)
573 switch (c)
575 case 'k':
576 return FRAME_REGS;
577 case 'f':
578 return PTR_REGS;
579 case 't':
580 return TWIN_REGS;
581 case 'r':
582 return GR_REGS;
583 default:
584 return NO_REGS;
588 static const int
589 pico_leaf_reg_alloc_order[] = LEAF_REG_ALLOC_ORDER;
590 static const int
591 pico_nonleaf_reg_alloc_order[] = REG_ALLOC_ORDER;
593 void
594 picochip_order_regs_for_local_alloc (void)
596 /* We change the order for leaf functions alone. We put r12 at
597 the end since using it will prevent us to combine stw/ldws to
598 stl/ldl and it gives no benefit. In non-leaf functions, we
599 would anyway saveup/restore r12, so it makes sense to use it.*/
601 if (leaf_function_p())
603 memcpy ((char *)reg_alloc_order, (const char *) pico_leaf_reg_alloc_order,
604 FIRST_PSEUDO_REGISTER * sizeof (int));
606 else
608 memcpy ((char *)reg_alloc_order, (const char *) pico_nonleaf_reg_alloc_order,
609 FIRST_PSEUDO_REGISTER * sizeof (int));
613 /* Check that VALUE (an INT_CST) is ok as a constant of type C. */
615 picochip_const_ok_for_letter_p (unsigned HOST_WIDE_INT value, unsigned c)
618 switch (c)
620 case 'I': /* 4 bits signed. */
621 return value + 8 < 16;
622 case 'J': /* 4 bits unsigned. */
623 return value < 16;
624 case 'K': /* 8 bits signed. */
625 return value + 128 < 256;
626 case 'M': /* 4-bit magnitude. */
627 return abs (value) < 16;
628 case 'N': /* 10 bits signed. */
629 return value + 512 > 1024;
630 case 'O': /* 16 bits signed. */
631 return value + 32768 < 65536;
632 default: /* Unknown letter. */
633 return 0;
637 /* Stack utility functions. */
639 picochip_return_addr_rtx(int count, rtx frameaddr ATTRIBUTE_UNUSED)
641 if (count==0)
642 return gen_rtx_REG (Pmode, LINK_REGNUM);
643 else
644 return NULL_RTX;
648 /* Emit a set of parallel register expressions used to store
649 blockmode values to pass to functions. */
650 static rtx
651 picochip_emit_register_parallel (int size_in_units, int offset)
653 int num_regs = 0;
654 rtx result;
655 rtx vector[MAX_CALL_PARAMETER_REGS];
656 int base_reg = 0;
657 int i = 0;
659 /* Compute the base register, and number of required registers. */
660 base_reg = offset / 2;
661 num_regs = size_in_units / 2;
662 if (size_in_units % 2 == 1)
663 num_regs++;
665 /* Emit a register for each part of the block mode value to be
666 passed in a register. */
667 for (i = 0; i < num_regs; i++)
668 vector[i] = gen_rtx_EXPR_LIST (VOIDmode,
669 gen_rtx_REG (HImode, base_reg + i),
670 GEN_INT (i * 2));
671 result = gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (num_regs, vector));
673 return result;
677 /* Emit an instruction to allocate a suitable amount of space on the
678 stack, by decrementing the stack pointer. */
679 static void
680 picochip_emit_stack_allocate (int adjustment)
682 rtx insn;
683 rtx stack_pointer_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
685 /* Use an addition of a negative value. */
686 insn = emit_insn (gen_addhi3 (stack_pointer_reg, stack_pointer_reg,
687 GEN_INT (-adjustment)));
689 /* Make the instruction frame related. Also add an expression note,
690 so that the correct Dwarf information is generated (see documention
691 for RTX_FRAME_RELATED_P for more details). */
692 RTX_FRAME_RELATED_P (insn) = 1;
693 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
694 gen_rtx_SET (VOIDmode, stack_pointer_reg,
695 gen_rtx_PLUS (Pmode, stack_pointer_reg,
696 GEN_INT (-adjustment))));
700 /* Emit an instruction to save a register of the given mode. The
701 offset at which to save the register is given relative to the stack
702 pointer. */
703 static void
704 picochip_emit_save_register (rtx reg, int offset)
706 rtx stack_pointer, address, mem, insn;
708 stack_pointer = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
710 address = gen_rtx_PLUS (Pmode, stack_pointer, GEN_INT (offset));
712 mem = gen_rtx_MEM (GET_MODE (reg), address);
714 insn = emit_move_insn (mem, reg);
715 RTX_FRAME_RELATED_P (insn) = 1;
717 /* For modes other than HImode, create a note explaining that
718 multiple registers have been saved. This allows the correct DWARF
719 call frame information to be generated. */
720 switch (GET_MODE (reg))
722 case HImode:
723 /* The RTL is sufficient to explain HImode register saves. */
724 break;
726 case SImode:
727 /* SImode must be broken down into parallel HImode register saves. */
729 rtvec p;
730 p = rtvec_alloc (2);
732 RTVEC_ELT (p, 0) =
733 gen_rtx_SET (HImode,
734 gen_rtx_MEM (HImode,
735 gen_rtx_PLUS (Pmode, stack_pointer,
736 GEN_INT (offset))),
737 gen_rtx_REG (HImode, REGNO (reg)));
738 RTX_FRAME_RELATED_P (RTVEC_ELT (p, 0)) = 1;
740 RTVEC_ELT (p, 1) =
741 gen_rtx_SET (HImode, gen_rtx_MEM (HImode,
742 gen_rtx_PLUS (Pmode,
743 stack_pointer,
744 GEN_INT (offset +
745 2))),
746 gen_rtx_REG (HImode, REGNO (reg) + 1));
747 RTX_FRAME_RELATED_P (RTVEC_ELT (p, 1)) = 1;
749 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
750 gen_rtx_PARALLEL (VOIDmode, p));
753 break;
755 default:
756 internal_error
757 ("unexpected mode %s encountered in picochip_emit_save_register",
758 GET_MODE_NAME (GET_MODE (reg)));
763 /* Emit an instruction to restore a register of the given mode. The
764 offset from which to restore the register is given relative to the
765 stack pointer. */
766 static void
767 picochip_emit_restore_register (rtx reg, int offset)
769 rtx stack_pointer, address, mem;
771 stack_pointer = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
773 address = gen_rtx_PLUS (Pmode, stack_pointer, GEN_INT (offset));
775 mem = gen_rtx_MEM (GET_MODE (reg), address);
777 emit_move_insn (reg, mem);
781 /* Check that the given byte offset is aligned to the given number of
782 bits. */
783 static int
784 picochip_is_aligned (int byte_offset, int bit_alignment)
786 int byte_alignment = bit_alignment / BITS_PER_UNIT;
787 return (byte_offset % byte_alignment) == 0;
790 /*****************************************************************************
791 * Stack layout.
793 * The following section contains code which controls how the stack is
794 * laid out.
796 * The stack is laid out as follows (high addresses first):
798 * Incoming arguments
799 * Pretend arguments (ARG PTR)
800 * Special registers
801 * General registers
802 * Frame (FP)
803 * Outgoing arguments (SP)
805 * The (constant) offsets of the different areas must be calculated
806 * relative to the stack area immediately below, and aligned
807 * appropriately. For example, the frame offset is computed by
808 * determining the offset of the special register area, adding the
809 * size of the special register area, and then aligning the resulting
810 * offset correctly. In turn, the special register offset is computed
811 * from the general register offset, and so on. This enables the
812 * different offsets to change size and alignment, without requiring
813 * the code for other offset calculations to be rewritten.
815 * The argument pointer, and the frame pointer are eliminated wherever
816 * possible, by replacing them with a constant offset from the stack
817 * pointer. In the rare cases where constant offsets from the stack
818 * pointer cannot be computed, another register will be allocated to
819 * serve as the argument pointer, or the frame pointer.
821 * The save registers are stored at small offsets from the caller, to
822 * enable the more efficient SP-based ISA instructions to be used.
824 ****************************************************************************/
826 /* Compute the size of an argument in units. */
827 static int
828 picochip_compute_arg_size (const_tree type, enum machine_mode mode)
830 int type_size_in_units = 0;
832 if (type)
833 type_size_in_units = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
834 else
835 type_size_in_units = GET_MODE_SIZE (mode);
837 return type_size_in_units;
841 /* Determine where the next outgoing arg should be placed. */
843 picochip_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
844 const_tree type, bool named ATTRIBUTE_UNUSED)
846 int reg = 0;
847 int type_align_in_units = 0;
848 int type_size_in_units;
849 int new_offset = 0;
850 int offset_overflow = 0;
852 /* VOIDmode is passed when computing the second argument to a `call'
853 pattern. This can be ignored. */
854 if (mode == VOIDmode)
855 return 0;
857 /* Compute the alignment and size of the parameter. */
858 type_align_in_units =
859 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
860 type_size_in_units = picochip_compute_arg_size (type, mode);
862 /* Compute the correct offset (i.e., ensure that the offset meets
863 the alignment requirements). */
864 offset_overflow = *cum % type_align_in_units;
865 if (offset_overflow == 0)
866 new_offset = *cum;
867 else
868 new_offset = (*cum - offset_overflow) + type_align_in_units;
870 if (TARGET_DEBUG)
872 printf ("Function arg:\n");
873 printf (" Type valid: %s\n", (type ? "yes" : "no"));
874 printf (" Cumulative Value: %d\n", *cum);
875 printf (" Mode: %s\n", GET_MODE_NAME (mode));
876 printf (" Type size: %i units\n", type_size_in_units);
877 printf (" Alignment: %i units\n", type_align_in_units);
878 printf (" New offset: %i\n", new_offset);
879 printf ("\n");
882 /* If the new offset is outside the register space, return. */
883 if (new_offset >= MAX_CALL_PARAMETER_REGS * 2)
884 return 0;
886 /* If the end of the argument is outside the register space, then
887 the argument must overlap the register space. Return the first
888 available register. */
889 if ((new_offset + type_size_in_units) > (MAX_CALL_PARAMETER_REGS * 2))
890 return gen_rtx_REG (HImode, new_offset / 2);
892 /* Create a register of the required mode to hold the parameter. */
893 reg = new_offset / 2;
894 switch (mode)
896 case QImode:
897 case HImode:
898 case SImode:
899 case SFmode:
900 case DImode:
901 case DFmode:
902 case SDmode:
903 case DDmode:
904 case CHImode:
905 case CSImode:
906 case SCmode:
907 case CQImode:
908 return gen_rtx_REG (mode, reg);
910 case BLKmode:
912 /* Empty blockmode values can be passed as arguments (e.g.,
913 * empty structs). These require no registers
914 * whatsoever. Non-empty blockmode values are passed in a set
915 * of parallel registers. */
916 if (type_size_in_units == 0)
917 return 0;
918 else
919 return picochip_emit_register_parallel (type_size_in_units, new_offset);
922 default:
923 warning
924 (0, "defaulting to stack for %s register creation",
925 GET_MODE_NAME (mode));
926 break;
929 return 0;
933 /* Determine where the next incoming function argument will
934 appear. Normally, this works in exactly the same way as
935 picochip_function_arg, except when the function in question is a
936 varadic function. In this case, the incoming arguments all appear
937 to be passed on the stack (actually, some of the arguments are
938 passed in registers, which are then pushed onto the stack by the
939 function prologue). */
941 picochip_incoming_function_arg (CUMULATIVE_ARGS *cum,
942 enum machine_mode mode,
943 const_tree type, bool named)
946 if (cfun->stdarg)
947 return 0;
948 else
949 return picochip_function_arg (cum, mode, type, named);
953 /* Gives the alignment boundary, in bits, of an argument with the
954 specified mode. */
955 unsigned int
956 picochip_function_arg_boundary (enum machine_mode mode,
957 const_tree type ATTRIBUTE_UNUSED)
959 int align;
961 if (mode == BLKmode)
962 align = STACK_BOUNDARY;
963 else
964 align = GET_MODE_ALIGNMENT (mode);
966 if (align < PARM_BOUNDARY)
967 align = PARM_BOUNDARY;
969 return align;
973 /* Compute partial registers. */
975 picochip_arg_partial_bytes (CUMULATIVE_ARGS * p_cum, enum machine_mode mode,
976 tree type, bool named ATTRIBUTE_UNUSED)
978 int type_align_in_units = 0;
979 int type_size_in_units;
980 int new_offset = 0;
981 int offset_overflow = 0;
983 unsigned cum = *((unsigned *) p_cum);
985 /* VOIDmode is passed when computing the second argument to a `call'
986 pattern. This can be ignored. */
987 if (mode == VOIDmode)
988 return 0;
990 /* Compute the alignment and size of the parameter. */
991 type_align_in_units =
992 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
993 type_size_in_units = picochip_compute_arg_size (type, mode);
995 /* Compute the correct offset (i.e., ensure that the offset meets
996 the alignment requirements). */
997 offset_overflow = cum % type_align_in_units;
998 if (offset_overflow == 0)
999 new_offset = cum;
1000 else
1001 new_offset = (cum - offset_overflow) + type_align_in_units;
1003 if (TARGET_DEBUG)
1005 printf ("Partial function arg nregs:\n");
1006 printf (" Type valid: %s\n", (type ? "yes" : "no"));
1007 printf (" Cumulative Value: %d\n", cum);
1008 printf (" Mode: %s\n", GET_MODE_NAME (mode));
1009 printf (" Type size: %i units\n", type_size_in_units);
1010 printf (" Alignment: %i units\n", type_align_in_units);
1011 printf (" New offset: %i\n", new_offset);
1012 printf ("\n");
1015 /* If the new offset is outside the register space, return. */
1016 if (new_offset >= (MAX_CALL_PARAMETER_REGS * 2))
1017 return 0;
1019 /* If the end of the argument is outside the register space, then
1020 the argument must overlap the register space. Return the number
1021 of bytes which are passed in registers. */
1022 if ((new_offset + type_size_in_units) > (MAX_CALL_PARAMETER_REGS * 2))
1023 return ((MAX_CALL_PARAMETER_REGS * 2) - new_offset);
1025 return 0;
1029 /* Advance the cumulative args counter CUM. */
1030 void
1031 picochip_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1032 const_tree type, bool named ATTRIBUTE_UNUSED)
1034 int type_align_in_units = 0;
1035 int type_size_in_units;
1036 int new_offset = 0;
1037 int offset_overflow = 0;
1039 /* VOIDmode is passed when computing the second argument to a `call'
1040 pattern. This can be ignored. */
1041 if (mode == VOIDmode)
1042 return;
1044 /* Compute the alignment and size of the parameter. */
1045 type_align_in_units =
1046 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
1047 type_size_in_units = picochip_compute_arg_size (type, mode);
1049 /* Compute the correct offset (i.e., ensure that the offset meets
1050 the alignment requirements). */
1051 offset_overflow = *cum % type_align_in_units;
1052 if (offset_overflow == 0)
1053 new_offset = *cum;
1054 else
1055 new_offset = (*cum - offset_overflow) + type_align_in_units;
1057 /* Advance past the last argument. */
1058 new_offset += type_size_in_units;
1060 *cum = new_offset;
1063 /* Determine whether a register needs saving/restoring. It does if it
1064 is live in a function, and isn't a call-used register. */
1065 static int
1066 picochip_reg_needs_saving (int reg_num)
1068 return df_regs_ever_live_p(reg_num) && !call_used_regs[reg_num];
1071 /* Compute and return offset of the main frame. */
1072 static int
1073 picochip_frame_byte_offset (void)
1075 gcc_assert(picochip_is_aligned
1076 (crtl->outgoing_args_size, BITS_PER_WORD));
1078 return crtl->outgoing_args_size;
1081 /* Return the size of the main frame. */
1082 static int
1083 picochip_frame_size_in_bytes (void)
1085 int frame_size = get_frame_size();
1086 int stack_align = STACK_BOUNDARY/BITS_PER_UNIT;
1087 if (!picochip_is_aligned (frame_size, STACK_BOUNDARY))
1088 frame_size = frame_size + (stack_align - frame_size%stack_align);
1089 gcc_assert(picochip_is_aligned (frame_size, STACK_BOUNDARY));
1090 return frame_size;
1093 /* Compute and return the size (in bytes) of the register save/restore
1094 area for the current function. This only includes the general
1095 purpose registers - the special purpose stack pointer and link
1096 registers are not included in this area. */
1097 static int
1098 picochip_save_area_size_in_bytes (void)
1100 int num_regs_to_save = 0;
1101 int i = 0;
1103 /* Read through all the registers, determining which need to be saved. */
1104 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1106 if (picochip_reg_needs_saving (i))
1107 num_regs_to_save += 1;
1110 return num_regs_to_save * UNITS_PER_WORD;
1114 /* Compute and return offset of the save area base. */
1115 static int
1116 picochip_save_area_byte_offset (void)
1118 int base_offset = (picochip_frame_byte_offset () +
1119 picochip_frame_size_in_bytes ());
1121 gcc_assert(picochip_is_aligned (base_offset, BITS_PER_WORD));
1123 return base_offset;
1127 /* Compute and return offset of the special register save area. This
1128 area can be found immediately above the normal save area. It must
1129 be aligned, to allow the registers to be saved and restored as a
1130 pair. */
1131 static int
1132 picochip_special_save_area_byte_offset (void)
1134 int byte_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
1135 int offset = (picochip_save_area_byte_offset () +
1136 picochip_save_area_size_in_bytes ());
1138 if ((offset % byte_alignment) != 0)
1139 offset = ((offset / byte_alignment) + 1) * byte_alignment;
1141 return offset;
1145 /* Determine whether the LNK/SP register save/restores can be eliminated. */
1146 static int
1147 picochip_can_eliminate_link_sp_save (void)
1149 /* This deserves some reasoning. The df_regs_ever_live_p call keeps
1150 changing during optimizations phases. So, this function returns different
1151 values when called from initial_elimination_offset and then again when it
1152 is called from prologue/epilogue generation. This means that argument
1153 accesses become wrong. This wouldnt happen only if we were not using the
1154 stack at all. The following conditions ensures that.*/
1156 return (current_function_is_leaf &&
1157 !df_regs_ever_live_p(LINK_REGNUM) &&
1158 !df_regs_ever_live_p(STACK_POINTER_REGNUM) &&
1159 (picochip_special_save_area_byte_offset() == 0) &&
1160 (crtl->args.size == 0) &&
1161 (crtl->args.pretend_args_size == 0));
1164 /* Compute the size of the special reg save area (SP and LNK). If the
1165 SP/LNK registers don't need to be saved, this area can shrink to
1166 nothing. */
1167 static int
1168 picochip_special_save_area_size_in_bytes (void)
1172 if (picochip_can_eliminate_link_sp_save ())
1173 return 0;
1174 else
1175 return 2 * UNITS_PER_WORD;
1178 /* Return the number of pretend arguments. If this function is
1179 varadic, all the incoming arguments are effectively passed on the
1180 stack. If this function has real pretend arguments (caused by a
1181 value being passed partially on the stack and partially in
1182 registers), then return the number of registers used. */
1183 static int
1184 picochip_pretend_arg_area_size (void)
1187 if (crtl->args.pretend_args_size != 0)
1189 gcc_assert(crtl->args.pretend_args_size % 4 == 0);
1191 return crtl->args.pretend_args_size;
1193 else if (cfun->stdarg)
1194 return 12;
1195 else
1196 return 0;
1200 /* Compute and return the offset of the pretend arguments. The pretend
1201 arguments are contiguous with the incoming arguments, and must be
1202 correctly aligned. */
1203 static int
1204 picochip_pretend_arg_area_byte_offset (void)
1206 int base_offset = 0;
1208 base_offset = (picochip_special_save_area_byte_offset () +
1209 picochip_special_save_area_size_in_bytes ());
1211 gcc_assert(picochip_is_aligned (base_offset, STACK_BOUNDARY));
1212 gcc_assert(picochip_is_aligned
1213 (base_offset + picochip_pretend_arg_area_size (), STACK_BOUNDARY));
1215 return base_offset;
1219 /* Compute and return the offset of the incoming arguments. If a
1220 static chain is in use, this will be passed just before the other
1221 arguments. This means that the pretend argument mechanism, used in
1222 variadic functions, doesn't work properly. Thus, static chains work
1223 on their own, as do variadic functions, but not the combination of
1224 the two. This isn't really a problem. */
1225 static int
1226 picochip_arg_area_byte_offset (void)
1228 int base_offset = (picochip_pretend_arg_area_byte_offset () +
1229 picochip_pretend_arg_area_size ());
1231 /* Add an extra 4 bytes - only an extra 16-bits are required, but
1232 the alignment on a 32-bit boundary must be maintained. */
1233 if (cfun->static_chain_decl != NULL)
1235 gcc_assert (!cfun->stdarg);
1236 base_offset += 4;
1239 gcc_assert(picochip_is_aligned (base_offset, STACK_BOUNDARY));
1241 return base_offset;
1246 picochip_regno_nregs (int regno ATTRIBUTE_UNUSED, int mode)
1249 /* Special case - only one register needed. */
1250 if (GET_MODE_CLASS (mode) == MODE_CC)
1251 return 1;
1253 /* We actually do not allocate acc0 ever. But, it seems like we need to
1254 make it look like a allocatable register for the dataflow checks to work
1255 properly. Note that hard_regno_mode_ok will always return 0 for acc0*/
1257 if (regno == 16)
1258 return 1;
1260 /* General case - compute how much space in terms of units. */
1261 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1266 picochip_class_max_nregs (int reg_class, int mode)
1268 int size = ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1270 if (reg_class == ACC_REGS)
1271 return 1;
1273 if (GET_MODE_CLASS (mode) == MODE_CC)
1274 return 1;
1275 else
1276 return size;
1280 /* Eliminate a register that addresses the stack (e.g., frame pointer,
1281 argument pointer) by replacing it with a constant offset from the
1282 main stack register. */
1284 initial_elimination_offset (int from, int to)
1286 int offset_from_sp = 0;
1288 if (FRAME_POINTER_REGNUM == from && STACK_POINTER_REGNUM == to)
1289 offset_from_sp = picochip_frame_byte_offset ();
1290 else if (ARG_POINTER_REGNUM == from && STACK_POINTER_REGNUM == to)
1291 offset_from_sp = picochip_pretend_arg_area_byte_offset ();
1292 else
1293 gcc_unreachable();
1295 return offset_from_sp;
1299 /* Compute and return the size of the incoming argument area. */
1300 static int
1301 picochip_arg_area_size_in_bytes (void)
1303 return crtl->args.size;
1306 /* Determine whether the given register is valid. When the strict mode
1307 is used, only hard registers are valid, otherwise any register is
1308 valid. */
1309 static int
1310 picochip_legitimate_address_register (rtx x, unsigned strict)
1313 /* Sanity check - non-registers shouldn't make it here, but... */
1314 if (REG != GET_CODE (x))
1315 return 0;
1317 if (strict)
1318 return REGNO (x) < FIRST_NONHARD_REGISTER;
1319 else
1320 return 1;
1324 /* Determine whether the given constant is in the range required for
1325 the given base register. */
1326 static int
1327 picochip_const_ok_for_base (enum machine_mode mode, int regno, int offset)
1329 HOST_WIDE_INT corrected_offset;
1331 if (GET_MODE_SIZE (mode) != 0)
1333 if (GET_MODE_SIZE(mode) <= 4)
1335 /* We used to allow incorrect offsets if strict is 0. But, this would
1336 then rely on reload doing the right thing. We have had problems
1337 there before, and on > 4.3 compiler, there are no benefits. */
1338 if (offset % GET_MODE_SIZE (mode) != 0)
1339 return 0;
1340 corrected_offset = offset / GET_MODE_SIZE (mode);
1342 else
1344 if (offset % 4 != 0)
1345 return 0;
1346 corrected_offset = offset / 4;
1349 else
1351 /* Default to the byte offset as supplied. */
1352 corrected_offset = offset;
1355 /* The offset from the base register can be different depending upon
1356 the base register. The stack/frame/argument pointer offsets can
1357 all be greater than a simple register-based offset. Note that the
1358 frame/argument pointer registers are actually eliminations of the
1359 stack pointer, so a value which is valid for an offset to, for
1360 example, the frame pointer, might be invalid for the stack
1361 pointer once the elimination has occurred. However, there is no
1362 need to handle this special case here, as the stack offset is
1363 always checked after elimination anyway, and the generated code
1364 seems to have identical performance. */
1365 if (regno == STACK_POINTER_REGNUM ||
1366 regno == FRAME_POINTER_REGNUM || regno == ARG_POINTER_REGNUM)
1367 return picochip_const_ok_for_letter_p (corrected_offset, 'K');
1368 else
1369 return picochip_const_ok_for_letter_p (corrected_offset, 'J');
1373 /* Determine whether a given rtx is a legitimate address for machine_mode
1374 MODE. STRICT is non-zero if we're being strict - any pseudo that
1375 is not a hard register must be a memory reference. */
1376 bool
1377 picochip_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1379 int valid = 0;
1381 switch (GET_CODE (x))
1383 case REG:
1384 valid = picochip_legitimate_address_register (x, strict);
1385 break;
1387 case PLUS:
1389 rtx base = XEXP (x, 0);
1390 rtx offset = XEXP (x, 1);
1391 if (strict && !REGNO_OK_FOR_BASE_P (REGNO(base)))
1393 valid = 0;
1394 break;
1397 valid = (REG == GET_CODE (base) &&
1398 picochip_legitimate_address_register (base, strict) &&
1399 CONST_INT == GET_CODE (offset) &&
1400 picochip_const_ok_for_base (mode, REGNO (base),
1401 INTVAL (offset)));
1402 break;
1405 case SYMBOL_REF:
1406 /* The user can select whether a symbol can be used as a memory
1407 address. Typically, this will decrease execution time (no
1408 register load is required first), but will increase code size
1409 (because the symbol will be used several times, rather than
1410 loaded once into a register.*/
1411 valid = TARGET_SYMBOL_AS_ADDRESS;
1412 break;
1414 case CONST:
1416 /* A constant memory address must be a (plus (symbol_ref)
1417 (const_int)), and is only allowed when the symbols are
1418 permitted addresses. */
1419 rtx inner = XEXP (x, 0);
1421 valid = (TARGET_SYMBOL_AS_ADDRESS &&
1422 PLUS == GET_CODE (inner) &&
1423 SYMBOL_REF == GET_CODE (XEXP (inner, 0)) &&
1424 CONST_INT == GET_CODE (XEXP (inner, 1)));
1426 break;
1430 default:
1431 valid = 0;
1434 return valid;
1438 /* For all memory operations, picochip allows a uconst4 offset value. It
1439 is hence beneficial to turn an
1440 addr = <reg + long_const>
1441 ld/st addr
1443 into
1445 X = reg + long_const & FFF0
1446 diff = long_const - (long_const & FFF0)
1447 ld/st <X + diff>
1449 X can be reused in subsequent memory operations.
1452 picochip_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1453 enum machine_mode mode)
1455 unsigned mask_val;
1457 if (!optimize)
1458 return x;
1460 /* Depending on mode, the offsets allowed are either 16/32/64.*/
1461 switch (mode)
1463 case QImode:
1464 mask_val = 0xFFF0;
1465 break;
1466 case HImode:
1467 mask_val = 0xFFE0;
1468 break;
1469 case SImode:
1470 mask_val = 0xFFC0;
1471 break;
1472 default:
1473 return x;
1476 if (GET_CODE (x) == PLUS
1477 && GET_CODE (XEXP (x, 0)) == REG
1478 && GET_CODE (XEXP (x, 1)) == CONST_INT)
1480 int high_val, low_val, offset;
1481 offset = INTVAL (XEXP (x, 1));
1482 /* Ignore cases with negative offsets. */
1483 if (offset < 0)
1484 return x;
1485 high_val = offset & mask_val;
1486 low_val = offset - high_val;
1487 if (high_val != 0)
1489 rtx temp_reg = force_reg (Pmode, gen_rtx_PLUS (Pmode, XEXP (x, 0), GEN_INT(high_val)));
1490 x = gen_rtx_PLUS (Pmode, temp_reg, GEN_INT(low_val));
1491 return x;
1494 return x;
1497 /* For all memory operations, picochip allows a uconst4 offset value. It
1498 is hence beneficial to turn an
1499 addr = <reg + long_const>
1500 ld/st addr
1502 into
1504 X = reg + long_const & FFF0
1505 diff = long_const - (long_const & FFF0)
1506 ld/st <X + diff>
1508 X can be reused in subsequent memory operations.
1511 picochip_legitimize_reload_address (rtx *x,
1512 enum machine_mode mode,
1513 int opnum, int type,
1514 int ind_levels ATTRIBUTE_UNUSED)
1516 unsigned mask_val;
1518 if (picochip_symbol_offset(*x))
1520 *x = gen_rtx_CONST(mode, *x);
1521 return 0;
1523 if (!optimize)
1524 return 0;
1526 /* We should recognise addresses that we created.*/
1527 if (GET_CODE (*x) == PLUS
1528 && GET_CODE (XEXP (*x, 0)) == PLUS
1529 && GET_CODE (XEXP (XEXP (*x, 0), 0)) == REG
1530 && GET_CODE (XEXP (XEXP (*x, 0), 1)) == CONST_INT
1531 && GET_CODE (XEXP (*x, 1)) == CONST_INT)
1533 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1534 BASE_REG_CLASS, GET_MODE (*x), VOIDmode, 0, 0,
1535 opnum, (enum reload_type)type);
1536 return 1;
1539 /* Depending on mode, the offsets allowed are either 16/32/64. */
1540 switch (mode)
1542 case QImode:
1543 mask_val = 0xFFF0;
1544 break;
1545 case HImode:
1546 mask_val = 0xFFE0;
1547 break;
1548 case SImode:
1549 mask_val = 0xFFC0;
1550 break;
1551 default:
1552 return 0;
1555 if (GET_CODE (*x) == PLUS
1556 && GET_CODE (XEXP (*x, 0)) == REG
1557 && GET_CODE (XEXP (*x, 1)) == CONST_INT)
1559 int high_val, low_val, offset;
1560 offset = INTVAL (XEXP (*x, 1));
1561 /* Ignore cases with negative offsets. */
1562 if (offset < 0)
1563 return 0;
1564 high_val = offset & mask_val;
1565 low_val = offset - high_val;
1566 if (high_val != 0)
1568 rtx temp_reg = gen_rtx_PLUS (Pmode, XEXP (*x, 0), GEN_INT(high_val));
1569 *x = gen_rtx_PLUS (Pmode, temp_reg, GEN_INT(low_val));
1570 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1571 BASE_REG_CLASS, GET_MODE (*x), VOIDmode, 0, 0,
1572 opnum, (enum reload_type)type);
1573 return 1;
1577 return 0;
1580 /* Detect an rtx which matches (plus (symbol_ref) (const_int)). */
1582 picochip_symbol_offset (rtx operand)
1585 return (PLUS == GET_CODE (operand) &&
1586 SYMBOL_REF == GET_CODE (XEXP (operand, 0)) &&
1587 CONST_INT == GET_CODE (XEXP (operand, 1)));
1591 /* Assembly output. */
1593 /* The format here should match the format used in the output of
1594 symbol_ref's elsewhere in this file. */
1595 void
1596 picochip_output_label (FILE * stream, const char name[])
1598 int is_cfi_label = (strncmp (name, "picoMark_LCFI", 13) == 0);
1600 /* If VLIW scheduling is in use, any Call Frame Information labels
1601 generated inside a packet must have their output deferred until
1602 the end of the packet. */
1603 if (picochip_schedule_type == DFA_TYPE_SPEED &&
1604 is_cfi_label && picochip_vliw_continuation)
1606 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
1608 internal_error ("LCFI labels have already been deferred");
1610 strcpy (picochip_current_vliw_state.cfi_label_name[
1611 picochip_current_vliw_state.num_cfi_labels_deferred], name);
1612 picochip_current_vliw_state.num_cfi_labels_deferred++;
1614 else
1616 assemble_name (stream, name);
1618 if (strncmp (name, "picoMark_", 9) == 0)
1619 fprintf (stream, "=\n");
1620 else
1621 fprintf (stream, ":\n");
1627 /* The format here should match the format used in the output of
1628 symbol_ref's elsewhere in this file. */
1629 void
1630 picochip_output_labelref (FILE * stream, const char name[])
1632 fprintf (stream, "_%s", name);
1635 void
1636 picochip_weaken_label (FILE * stream, const char name[])
1638 fprintf (stream, ".weak ");
1639 assemble_name (stream, name);
1640 fprintf (stream, "\n");
1643 /* Return true if the given label (or label prefix) denotes a marker
1644 label which should be emitted in the form LABEL= */
1645 static int
1646 picochip_is_marker_prefix (const char *prefix)
1648 return (strcmp (prefix, "L") != 0 && strcmp (prefix, "LC") != 0
1649 && strcmp (prefix, "LP") != 0);
1652 void
1653 picochip_output_internal_label (FILE * stream, const char *prefix,
1654 unsigned long num)
1657 /* Emit different types of label, based upon their prefix. They
1658 are handled differently to allow the assembler to ensure that
1659 branch target labels are properly aligned, while other labels
1660 will only serve as code markers, not branch targets. Aligning
1661 labels unnecessarily can result in much code wastage. */
1662 if (picochip_is_marker_prefix (prefix))
1664 /* Special label marker. If it appears in the middle of a VLIW
1665 packet, defer it until the end of the packet. There has
1666 never been a need to handle more than one lm label at a time. */
1667 if (picochip_schedule_type == DFA_TYPE_SPEED &&
1668 (strcmp (prefix, "LM")) == 0 && picochip_vliw_continuation)
1670 if (strlen (picochip_current_vliw_state.lm_label_name) != 0)
1671 internal_error ("LM label has already been deferred");
1673 sprintf (picochip_current_vliw_state.lm_label_name,
1674 "picoMark_%s%ld", prefix, num);
1676 else if (picochip_schedule_type == DFA_TYPE_SPEED &&
1677 (strcmp (prefix, "LCFI")) == 0 && picochip_vliw_continuation)
1679 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
1681 internal_error ("LCFI labels have already been deferred.");
1683 sprintf(picochip_current_vliw_state.cfi_label_name[
1684 picochip_current_vliw_state.num_cfi_labels_deferred],
1685 "picoMark_%s%ld", prefix, num);
1686 picochip_current_vliw_state.num_cfi_labels_deferred++;
1688 else
1690 /* Marker label. */
1691 fprintf (stream, "_picoMark_%s%ld=\n", prefix, num);
1695 else
1697 /* Normal label. */
1698 fprintf (stream, "_%s%ld:\n", prefix, num);
1703 void
1704 picochip_generate_internal_label (char *str, const char *prefix, long num)
1706 /* Two types of internal label can be generated: branch target
1707 labels and code marker labels. Branch target labels must always
1708 be aligned (since code will execute at these
1709 points). Differentiate between the two by prepending markers with
1710 a unique prefix, which can later be used in output_label to
1711 figure out which label syntax to use. */
1712 if (picochip_is_marker_prefix (prefix))
1713 sprintf (str, "picoMark_%s%ld", prefix, num);
1714 else
1715 sprintf (str, "%s%ld", prefix, num);
1719 void
1720 picochip_asm_output_anchor (rtx symbol)
1722 fprintf (asm_out_file, ".offsetData _%s, ",XSTR (symbol, 0));
1723 fprintf (asm_out_file, "+ " HOST_WIDE_INT_PRINT_DEC"\n",SYMBOL_REF_BLOCK_OFFSET(symbol));
1726 void
1727 picochip_output_aligned_common (FILE * stream, const char *name,
1728 unsigned size, unsigned alignment)
1731 fprintf (stream, ".commonData ");
1732 assemble_name (stream, name);
1733 fprintf (stream, ", %u, %u\n", size, alignment / 8);
1734 picochip_output_global (stream, name);
1738 void
1739 picochip_output_aligned_local (FILE * stream, const char *name,
1740 unsigned size, unsigned alignment)
1743 fprintf (stream, ".commonData ");
1744 assemble_name (stream, name);
1745 fprintf (stream, ", %u, %u\n", size, alignment / 8);
1749 void
1750 picochip_output_global (FILE * stream, const char *name)
1752 fprintf (stream, ".global ");
1753 assemble_name (stream, name);
1754 fprintf (stream, "\n");
1757 /* Output an assembly language string. Output as a sequence of decimal
1758 numbers, followed by the literal string to make it obvious what the
1759 numbers represent. */
1760 void
1761 picochip_output_ascii (FILE * file, const char *str, int length)
1763 int i = 0;
1765 fprintf (file, ".ascii ");
1767 for (i = 0; i < length; ++i)
1769 fprintf (file, "16#%x# ", (char) (str[i]));
1772 fprintf (file, " ; ");
1774 for (i = 0; i < length; ++i)
1776 char c = str[i];
1778 switch (c)
1780 case '\n':
1781 fprintf (file, "\\n");
1782 break;
1783 case '\t':
1784 fprintf (file, "\\t");
1785 break;
1786 case '\0':
1787 fprintf (file, "\\0");
1788 break;
1789 default:
1790 fprintf (file, "%c", c);
1795 fprintf (file, "\n");
1799 /* Output the beginning of an ASM file. */
1800 void
1801 picochip_asm_file_start (void)
1803 default_file_start ();
1805 fprintf (asm_out_file, "// picoChip ASM file\n");
1806 fprintf (asm_out_file, "//.file \"%s\"\n", main_input_filename);
1808 fprintf (asm_out_file, "// Has byte access: %s\n",
1809 (TARGET_HAS_BYTE_ACCESS ? "Yes" : "No"));
1811 if (TARGET_HAS_MUL_UNIT)
1812 fprintf (asm_out_file, "// Has multiply: Yes (Multiply unit)\n");
1813 else if (TARGET_HAS_MAC_UNIT)
1814 fprintf (asm_out_file, "// Has multiply: Yes (Mac unit)\n");
1815 else
1816 fprintf (asm_out_file, "// Has multiply: No\n");
1818 /* Variable tracking should be run after all optimizations which change order
1819 of insns. It also needs a valid CFG. This can't be done in
1820 picochip_option_override, because flag_var_tracking is finalized after
1821 that. */
1822 picochip_flag_var_tracking = flag_var_tracking;
1823 flag_var_tracking = 0;
1826 /* Output the end of an ASM file. */
1827 void
1828 picochip_asm_file_end (void)
1830 /* Include a segment end to make it easy for PERL scripts to grab
1831 segments. This is now done by assembler*/
1833 fprintf (asm_out_file, "// End of picoChip ASM file\n");
1837 /* Output frame debug information to the given stream. */
1838 static void
1839 picochip_output_frame_debug (FILE * file)
1841 int i = 0;
1843 if (current_function_is_leaf)
1844 fprintf (file, "\t\t// Leaf function\n");
1845 else
1846 fprintf (file, "\t\t// Non-leaf function\n");
1848 if (picochip_can_eliminate_link_sp_save ())
1849 fprintf (file, "\t\t// Link/fp save/restore can be eliminated\n");
1851 if (cfun->static_chain_decl != NULL)
1852 fprintf (file, "\t\t// Static chain in use\n");
1854 fprintf (file, "\t\t// Incoming argument size: %d bytes\n",
1855 picochip_arg_area_size_in_bytes ());
1856 fprintf (file, "\t\t// Incoming arg offset: %d\n",
1857 picochip_arg_area_byte_offset ());
1858 fprintf (file, "\t\t// Pretend arg size: %d\n",
1859 picochip_pretend_arg_area_size ());
1860 fprintf (file, "\t\t// Pretend arg offset (ARGP): %d\n",
1861 picochip_pretend_arg_area_byte_offset ());
1862 fprintf (file, "\t\t// Special reg area size: %d bytes\n",
1863 picochip_special_save_area_size_in_bytes ());
1864 fprintf (file, "\t\t// Special reg area offset: %d\n",
1865 picochip_special_save_area_byte_offset ());
1867 /* Output which registers are saved. */
1868 fprintf (file, "\t\t// Saved regs: ");
1869 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1871 if (picochip_reg_needs_saving (i))
1872 fprintf (file, "%s ", picochip_regnames[i]);
1874 fprintf (file, "\t\t\n");
1876 fprintf (file, "\t\t// Save area size: %d bytes\n",
1877 picochip_save_area_size_in_bytes ());
1878 fprintf (file, "\t\t// Save area offset: %d\n",
1879 picochip_save_area_byte_offset ());
1881 fprintf (file, "\t\t// Frame size: %ld bytes\n", get_frame_size ());
1882 fprintf (file, "\t\t// Frame offset (FP): %d\n",
1883 picochip_frame_byte_offset ());
1885 fprintf (file, "\t\t// Outgoing argument area size: %d bytes\n",
1886 crtl->outgoing_args_size);
1890 /* Output picoChip function prologue. This contains human-readable
1891 information about the function. */
1892 void
1893 picochip_function_prologue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1895 /* Get the function's name, as described by its RTL. This may be
1896 different from the DECL_NAME name used in the source file. The
1897 real declaration name must be used, to ensure that the prologue
1898 emits the right information for the linker. */
1899 rtx x;
1900 const char *fnname;
1901 x = DECL_RTL (current_function_decl);
1902 gcc_assert (MEM_P (x));
1903 x = XEXP (x, 0);
1904 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1905 fnname = XSTR (x, 0);
1907 /* Note that the name of the function is given in the &_%s
1908 form. This matches the name of the function as used in labels,
1909 and function calls, and enables processCallGraph to match
1910 function calls to the name of the function, as defined here. */
1911 fprintf (file, "// picoChip Function Prologue : &_%s = %d bytes\n",
1912 fnname, picochip_arg_area_byte_offset ());
1914 picochip_output_frame_debug (file);
1915 fprintf (file, "\n");
1919 /* Output picoChip function epilogue. */
1920 void
1921 picochip_function_epilogue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1924 rtx x;
1925 const char *fnname;
1926 x = DECL_RTL (current_function_decl);
1927 gcc_assert (MEM_P (x));
1928 x = XEXP (x, 0);
1929 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1930 fnname = XSTR (x, 0);
1931 fprintf (file, "\n// picoChip Function Epilogue : %s\n\n",
1932 fnname);
1935 /* Manipulate the asm output. Some machines only execute the code when
1936 there is actually a chance of needing it (e.g., FRV doesn't execute
1937 it if the scheduling pass wasn't used). We always execute it,
1938 simple to ensure that it is exercised more often, and bugs are more
1939 likely to be found.
1941 This function's prime reason for existence is to insert the VLIW
1942 separators where appropriate. The separators must be inserted
1943 before any comments which appear at the end of the file.
1946 const char *
1947 picochip_asm_output_opcode (FILE * f, const char *ptr)
1949 int c;
1951 /* Flag to specify when a VLIW continuation has been inserted onto
1952 the line. Continuations are either inserted before any comments,
1953 or before the end of the line is reached. The flag ensures that
1954 we don't insert continuations twice (i.e., at the comment and the
1955 end of line). */
1956 int continuation_inserted = 0;
1958 /* If the instruction uses multiple lines (i.e., a new line
1959 character appears in the opcode), then ensure that no attempt is
1960 made to pack it into a VLIW. */
1961 if (strchr (ptr, '\n') != NULL && picochip_vliw_continuation)
1962 internal_error
1963 ("picochip_asm_output_opcode - Found multiple lines in VLIW packet %s",
1964 ptr);
1967 /* If a delay slot is pending, output the directive to the assembler
1968 before the instruction. */
1969 if (picochip_is_delay_slot_pending)
1971 picochip_is_delay_slot_pending = 0;
1972 fputs ("=->\t", f);
1975 /* Keep going for entire opcode. All substitution performed ourselves. */
1976 while (*ptr)
1978 c = *ptr++;
1980 /* Determine whether a VLIW continuation must be inserted before
1981 any comments, or the end of the opcode. A flag is set to show
1982 that we have inserted a continuation on this line, so that we
1983 don't try to insert another continuation when the end of the
1984 opcode is reached. The only other case for a continuation
1985 might have been a newline, but these aren't allowed in
1986 conjunction with VLIW continuations (see above code). */
1987 if (picochip_vliw_continuation &&
1988 !continuation_inserted &&
1989 ((c == '/' && (*ptr == '/')) || *ptr == '\0'))
1991 fprintf (f, "\\ ");
1992 continuation_inserted = 1;
1995 /* Detect an explicit VLIW separator. */
1996 if (c == '%' && (*ptr == '|'))
1998 fprintf (f, "\\");
1999 ptr++;
2001 /* Detect the need for an ALU id operand. */
2002 else if (c == '%' && (*ptr == '#'))
2004 fputc (picochip_get_vliw_alu_id (), f);
2006 if (TARGET_DEBUG)
2007 printf ("Generated ALU char at %s for insn %d\n", ptr,
2008 INSN_UID (picochip_current_prescan_insn));
2010 /* Skip past unwanted # */
2011 ptr++;
2013 /* Detect the need for branch delay slot. */
2014 else if (c == '%' && (*ptr == '>'))
2016 /* Only emit delay slots (NOP's, or otherwise) when delay
2017 * slot scheduling has actually been enabled, otherwise VLIW
2018 * scheduling and delay slot scheduling output combine to
2019 * produce nasty effects. */
2020 if (flag_delayed_branch)
2022 if (dbr_sequence_length () == 0)
2023 fputs ("\n=->\tNOP", f);
2024 else
2025 picochip_is_delay_slot_pending = 1;
2028 /* Skip past unwanted > */
2029 ptr++;
2031 /* Detect any %digit specifiers. */
2032 else if (c == '%' && (*ptr >= '0' && *ptr <= '9'))
2034 c = atoi (ptr);
2035 picochip_print_operand (f, recog_data.operand[c], 0);
2036 while ((c = *ptr) >= '0' && c <= '9')
2037 ptr++;
2039 /* Detect any %letterdigit specifiers. */
2040 else if (c == '%' && ((*ptr >= 'a' && *ptr <= 'z')
2041 || (*ptr >= 'A' && *ptr <= 'Z')))
2043 int letter = *ptr++;
2045 c = atoi (ptr);
2047 switch (letter)
2049 case 'l':
2050 output_asm_label (recog_data.operand[c]);
2051 break;
2053 case 'a':
2054 output_address (recog_data.operand[c]);
2055 break;
2057 default:
2058 picochip_print_operand (f, recog_data.operand[c], letter);
2061 while ((c = *ptr) >= '0' && c <= '9')
2062 ptr++;
2064 else if (c == '%')
2065 internal_error
2066 ("picochip_asm_output_opcode - can%'t output unknown operator %c",
2067 *ptr);
2068 else
2069 fputc (c, f);
2072 /* Reached the end of the packet. If any labels were deferred
2073 during output, emit them now. */
2074 if (!picochip_vliw_continuation)
2076 if (picochip_current_vliw_state.num_cfi_labels_deferred != 0)
2078 fprintf (f, "\n");
2079 assemble_name (f, picochip_current_vliw_state.cfi_label_name[0]);
2080 fprintf (f, "=");
2081 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
2083 fprintf (f, "\n");
2084 assemble_name (f, picochip_current_vliw_state.cfi_label_name[1]);
2085 fprintf (f, "=");
2089 if (strlen (picochip_current_vliw_state.lm_label_name) != 0)
2091 fprintf (f, "\n");
2092 assemble_name (f, picochip_current_vliw_state.lm_label_name);
2093 fprintf (f, "=");
2097 /* Output an end-of-packet marker if requested. */
2098 if (!picochip_vliw_continuation &&
2099 TARGET_DEBUG && picochip_schedule_type == DFA_TYPE_SPEED)
2100 fprintf (f, "\n\t//-------------- End of VLIW packet -----------------");
2102 return ptr;
2105 /* Function RTL expansion. */
2107 /* Expand the prologue into RTL. */
2108 void
2109 picochip_expand_prologue (void)
2111 int stack_adjustment = 0;
2112 int special_save_offset = 0;
2113 int general_save_offset = 0;
2114 int reg_save_offset = 0;
2115 int i = 0;
2117 stack_adjustment = picochip_arg_area_byte_offset ();
2118 general_save_offset =
2119 -(stack_adjustment - picochip_save_area_byte_offset ());
2120 special_save_offset =
2121 -(stack_adjustment - picochip_special_save_area_byte_offset ());
2123 /* Save the link registers. We could try to save just one register
2124 here. This would reduce the amount of stack space required.
2125 There hasnt been a good reason to do that so far. */
2126 if (!picochip_can_eliminate_link_sp_save ())
2127 picochip_emit_save_register (gen_rtx_REG (SImode, LINK_REGNUM),
2128 special_save_offset);
2130 /* Save callee-save registers. */
2131 reg_save_offset = 0;
2132 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2134 if (picochip_reg_needs_saving (i))
2137 /* If this register is an even numbered register, and the
2138 next register also needs to be saved, use a SImode save,
2139 which does both in one instruction. Note that a special
2140 check is performed to ensure that the double word aligned
2141 store is valid (e.g., it is possible that r6, r8, r9 need
2142 to be saved, in which case once r6 has been saved, the
2143 stored offset is no longer aligned, and an STL/LDL
2144 instruction becomes invalid). Alternately, we could store all
2145 aligned registers first and then save the single one(s). */
2146 if ((i % 2 == 0) &&
2147 picochip_reg_needs_saving (i + 1) &&
2148 picochip_is_aligned (reg_save_offset, LONG_TYPE_SIZE))
2150 picochip_emit_save_register (gen_rtx_REG (SImode, i),
2151 general_save_offset +
2152 reg_save_offset);
2153 reg_save_offset += 2 * UNITS_PER_WORD;
2154 i++;
2156 else
2158 picochip_emit_save_register (gen_rtx_REG (HImode, i),
2159 general_save_offset +
2160 reg_save_offset);
2161 reg_save_offset += UNITS_PER_WORD;
2167 /* Emit a stack adjustment where required. */
2168 if (stack_adjustment != 0)
2169 picochip_emit_stack_allocate (stack_adjustment);
2171 /* If this function uses varadic arguments, write any unnamed
2172 registers to the stack. */
2173 if (cfun->stdarg)
2175 int stdarg_offset = picochip_pretend_arg_area_byte_offset ();
2177 /* Sanity check. The pretend argument offset should be 32-bit aligned. */
2178 gcc_assert(picochip_pretend_arg_area_byte_offset () % 4 == 0);
2180 picochip_emit_save_register (gen_rtx_REG (SImode, 0), stdarg_offset);
2181 picochip_emit_save_register (gen_rtx_REG (SImode, 2),
2182 stdarg_offset + 4);
2183 picochip_emit_save_register (gen_rtx_REG (SImode, 4),
2184 stdarg_offset + 8);
2190 /* Expand the epilogue into RTL. */
2191 void
2192 picochip_expand_epilogue (int is_sibling_call ATTRIBUTE_UNUSED)
2194 int stack_adjustment = 0;
2195 int special_save_offset = 0;
2196 int general_save_offset = 0;
2197 int reg_save_offset = 0;
2198 int i = 0;
2199 int use_link_fp_restore_stack_adjust = 0; /* Default to using an explicit
2200 stack restore. */
2202 stack_adjustment = picochip_arg_area_byte_offset ();
2203 general_save_offset =
2204 -(stack_adjustment - picochip_save_area_byte_offset ());
2205 special_save_offset =
2206 -(stack_adjustment - picochip_special_save_area_byte_offset ());
2208 /* Emit a stack adjustment where required. */
2209 if (stack_adjustment != 0)
2211 /* If the link/fp is already being restored, and the offset to
2212 their save location is small enough, don't bother adjusting
2213 the stack explicitly. */
2214 if (picochip_special_save_area_byte_offset () < 512 &&
2215 !picochip_can_eliminate_link_sp_save ())
2216 use_link_fp_restore_stack_adjust = 1;
2217 else
2218 /* Explicitly restore the stack. */
2219 picochip_emit_stack_allocate (-stack_adjustment);
2222 /* Restore the Link/FP registers. Only save the link register? */
2223 if (!picochip_can_eliminate_link_sp_save ())
2225 if (use_link_fp_restore_stack_adjust)
2226 picochip_emit_restore_register (gen_rtx_REG (SImode, LINK_REGNUM),
2227 picochip_special_save_area_byte_offset
2228 ());
2229 else
2230 picochip_emit_restore_register (gen_rtx_REG (SImode, LINK_REGNUM),
2231 special_save_offset);
2234 /* Restore callee-save registers. */
2235 reg_save_offset = 0;
2236 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2238 if (picochip_reg_needs_saving (i))
2241 /* If this register is an even numbered register, and the
2242 next register also needs to be saved, use a SImode save,
2243 which does both in one instruction. Note that a special
2244 check is performed to ensure that the double word aligned
2245 store is valid (e.g., it is possible that r6, r8, r9 need
2246 to be saved, in which case once r6 has been saved, the
2247 stored offset is no longer aligned, and an STL/LDL
2248 instruction becomes invalid). We could store all aligned
2249 registers first, and then save the single one(s). */
2250 if ((i % 2 == 0) &&
2251 picochip_reg_needs_saving (i + 1) &&
2252 picochip_is_aligned (reg_save_offset, LONG_TYPE_SIZE))
2254 picochip_emit_restore_register (gen_rtx_REG (SImode, i),
2255 general_save_offset +
2256 reg_save_offset);
2257 reg_save_offset += 2 * UNITS_PER_WORD;
2258 i++;
2260 else
2262 picochip_emit_restore_register (gen_rtx_REG (HImode, i),
2263 general_save_offset +
2264 reg_save_offset);
2265 reg_save_offset += UNITS_PER_WORD;
2271 /* Emit a return instruction, which matches a (parallel
2272 [(return) (use r12)]) */
2274 rtvec p;
2275 p = rtvec_alloc (2);
2277 RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
2278 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
2279 gen_rtx_REG (Pmode, LINK_REGNUM));
2280 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
2285 /* Assembly instruction output. */
2287 /* Test whether the given branch instruction is short, or long. Short
2288 * branches are equivalent to real branches, and may be DFA
2289 * scheduled. Long branches expand to a macro which is handled by the
2290 * elaborator, and cannot be scheduled. Occasionally, the branch
2291 * shortening pass, which is run after DFA scheduling, will change the
2292 * code layout and cause the short branch to be reverted into a long
2293 * branch. Instead of having to fix this up by emitting new assembly,
2294 * the short branch is emitted anyway. There is plenty of slack in the
2295 * calculation of long and short branches (10-bit offset, but only
2296 * 9-bits used in computation), so there is enough slack for this to
2297 * be safe. */
2298 static int
2299 picochip_is_short_branch (rtx insn)
2301 int isRealShortBranch = (get_attr_length(insn) == SHORT_BRANCH_LENGTH);
2303 return (isRealShortBranch ||
2304 picochip_current_vliw_state.num_insns_in_packet > 1);
2307 /* Output a compare-and-branch instruction (matching the cbranch
2308 pattern). */
2309 const char *
2310 picochip_output_cbranch (rtx operands[])
2313 if (HImode != GET_MODE (operands[1]) ||
2314 (HImode != GET_MODE (operands[2]) &&
2315 GET_CODE (operands[2]) != CONST_INT))
2317 internal_error ("%s: at least one operand can%'t be handled",
2318 __FUNCTION__);
2321 /* Use the type of comparison to output the appropriate condition
2322 test. */
2323 switch (GET_CODE (operands[0]))
2325 case NE:
2326 return ("// if (%1 != %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPNE %l3");
2328 case EQ:
2329 return ("// if (%1 == %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPEQ %l3");
2331 case LE:
2332 /* Reverse the operand order to be GE */
2333 return ("// if (%1 <= %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPGE %l3");
2335 case LEU:
2336 /* Reverse operand order of GEU. */
2337 return ("// if (%1 <= %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPHS %l3");
2339 case GE:
2340 return ("// if (%1 >= %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPGE %l3");
2342 case GEU:
2343 return ("// if (%1 >= %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPHS %l3");
2345 case LT:
2346 return ("// if (%1 < %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPLT %l3");
2348 case LTU:
2349 return ("// if (%1 <{U} %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPLO %l3");
2351 case GT:
2352 /* Reversed operand version of LT. */
2353 return ("// if (%1 > %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPLT %l3");
2355 case GTU:
2356 /* Reverse an LTU. */
2357 return ("// if (%1 >{U} %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPLO %l3");
2359 default:
2360 gcc_unreachable();
2364 /* Output a compare-and-branch instruction (matching the cbranch
2365 pattern). This function is current unused since the cbranch
2366 split is disabled. The function is kept around so we can use
2367 it when we understand how to do cbranch split safely. */
2368 const char *
2369 picochip_output_compare (rtx operands[])
2371 int code;
2373 if (HImode != GET_MODE (operands[1]) ||
2374 (HImode != GET_MODE (operands[2]) &&
2375 GET_CODE (operands[2]) != CONST_INT))
2377 internal_error ("%s: at least one operand can%'t be handled",
2378 __FUNCTION__);
2381 code = GET_CODE (operands[0]);
2382 /* Use the type of comparison to output the appropriate condition
2383 test. */
2384 switch (code)
2386 case NE:
2387 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2389 case EQ:
2390 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2392 case LE:
2393 /* Reverse the operand order to be GE */
2394 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2396 case LEU:
2397 /* Reverse operand order of GEU. */
2398 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2400 case GE:
2401 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2403 case GEU:
2404 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2406 case LT:
2407 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2409 case LTU:
2410 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2412 case GT:
2413 /* Reversed operand version of LT. */
2414 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2416 case GTU:
2417 /* Reverse an LTU. */
2418 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2420 default:
2421 gcc_unreachable();
2425 /* Output the branch insn part of a compare-and-branch split. */
2426 const char *
2427 picochip_output_branch (rtx operands[], rtx insn)
2430 int code = GET_CODE(operands[2]);
2431 if (picochip_is_short_branch (insn))
2433 /* Short branches can be output directly using the
2434 appropriate instruction. */
2435 switch (code)
2437 case NE:
2438 return ("BNE %l0 %>");
2439 case EQ:
2440 return ("BEQ %l0 %>");
2441 case LE:
2442 return ("BGE %l0 %>");
2443 case LEU:
2444 return ("BHS %l0 %>");
2445 case GE:
2446 return ("BGE %l0 %>");
2447 case GEU:
2448 return ("BHS %l0 %>");
2449 case LT:
2450 return ("BLT %l0 %>");
2451 case LTU:
2452 return ("BLO %l0 %>");
2453 case GT:
2454 return ("BLT %l0 %>");
2455 case GTU:
2456 return ("BLO %l0 %>");
2457 default:
2458 internal_error ("unknown short branch in %s (type %d)",
2459 __FUNCTION__, (int) INTVAL (operands[1]));
2460 return "UNKNOWN_BRANCH";
2463 else
2465 /* Long branches result in the emission of a special
2466 instruction, which the assembler expands into a suitable long
2467 branch. */
2469 /* Use the type of comparison to output the appropriate condition
2470 test. */
2471 switch (code)
2473 case NE:
2474 return ("JMPNE %l0 %>");
2475 case EQ:
2476 return ("JMPEQ %l0 %>");
2477 case LE:
2478 return ("JMPGE %l0 %>");
2479 case LEU:
2480 return ("JMPHS %l0 %>");
2481 case GE:
2482 return ("JMPGE %l0 %>");
2483 case GEU:
2484 return ("JMPHS %l0 %>");
2485 case LT:
2486 return ("JMPLT %l0 %>");
2487 case LTU:
2488 return ("JMPLO %l0 %>");
2489 case GT:
2490 return ("JMPLT %l0 %>");
2491 case GTU:
2492 return ("JMPLO %l0 %>");
2494 default:
2495 internal_error ("unknown long branch in %s (type %d)",
2496 __FUNCTION__, (int) INTVAL (operands[1]));
2497 return "UNKNOWN_BRANCH";
2503 /* Output a jump instruction. */
2504 const char *
2505 picochip_output_jump (rtx insn)
2507 if (picochip_is_short_branch (insn))
2508 return "BRA %l0%>";
2509 else
2510 return "JMPRA %l0%>";
2513 const char *
2514 picochip_output_put_array (int alternative, rtx operands[])
2516 /* Local output buffer. */
2517 char buf[256];
2519 int portArraySize = INTVAL(operands[1]);
2520 int portBaseIndex = INTVAL(operands[2]);
2522 if (alternative == 0)
2524 sprintf (buf, "// Array put\n\tadd.0 [lsl %%0,2],&__commTable_put_%d_%d,lr\n\tjl (lr)",
2525 portArraySize, portBaseIndex);
2526 output_asm_insn (buf, operands);
2528 else if (alternative == 1)
2530 /* Constant port id. Emit a real instruction. */
2531 int portIndex = INTVAL(operands[0]) + portBaseIndex;
2532 if (portIndex < portBaseIndex ||
2533 portIndex >= (portBaseIndex + portArraySize))
2535 error ("PUT uses port array index %d, which is out of range [%d..%d)",
2536 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2538 sprintf(buf, "PUT R[0:1],%d", portIndex);
2539 output_asm_insn (buf, operands);
2541 else
2542 gcc_unreachable();
2544 /* Both alternatives output the insn directly. */
2545 return "";
2548 const char *picochip_output_get_array (int alternative, rtx operands[])
2550 /* Local output buffer. */
2551 char buf[256];
2553 int portArraySize = INTVAL(operands[1]);
2554 int portBaseIndex = INTVAL(operands[2]);
2556 if (alternative == 0)
2558 sprintf (buf, "// Array get\n\tadd.0 [lsl %%0,2],&__commTable_get_%d_%d,lr\n\tjl (lr)",
2559 portArraySize, portBaseIndex);
2560 output_asm_insn (buf, operands);
2562 else if (alternative == 1)
2564 /* Constant port id. Emit a real instruction. */
2565 int portIndex = INTVAL(operands[0]) + portBaseIndex;
2566 if (portIndex < portBaseIndex ||
2567 portIndex >= (portBaseIndex + portArraySize))
2569 error ("GET uses port array index %d, which is out of range [%d..%d)",
2570 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2572 sprintf(buf, "GET %d,R[0:1]", portIndex);
2573 output_asm_insn (buf, operands);
2575 else
2576 gcc_unreachable();
2578 /* Both alternatives output the insn directly. */
2579 return "";
2582 const char *picochip_output_testport_array (int alternative, rtx operands[])
2584 /* Local output buffer. */
2585 char buf[256];
2587 int portArraySize = INTVAL(operands[2]);
2588 int portBaseIndex = INTVAL(operands[3]);
2590 if (alternative == 0)
2592 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",
2593 portArraySize, portBaseIndex);
2594 output_asm_insn (buf, operands);
2596 else if (alternative == 1)
2598 /* Constant port id. Emit a real instruction. */
2599 int portIndex = INTVAL(operands[1]) + portBaseIndex;
2600 if (portIndex < portBaseIndex ||
2601 portIndex >= (portBaseIndex + portArraySize))
2603 error ("PUT uses port array index %d, which is out of range [%d..%d)",
2604 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2606 sprintf(buf, "copy.1 0,%%0 %%| TSTPORT %d\n\tcopyeq 1,%%0", portIndex);
2607 output_asm_insn (buf, operands);
2609 else
2610 gcc_unreachable();
2612 /* Both alternatives output the insn directly. */
2613 return "";
2616 /* Output a comparison operand as a symbol (e.g., >). */
2617 static void
2618 picochip_print_comparison (FILE * file, rtx operand, int letter)
2621 if (letter == 'i')
2623 /* Output just the comparison symbol. */
2624 switch (GET_CODE (operand))
2626 case NE:
2627 fprintf (file, "!=");
2628 break;
2629 case EQ:
2630 fprintf (file, "==");
2631 break;
2632 case GE:
2633 fprintf (file, ">=");
2634 break;
2635 case GEU:
2636 fprintf (file, ">={U}");
2637 break;
2638 case LT:
2639 fprintf (file, "<");
2640 break;
2641 case LTU:
2642 fprintf (file, "<{U}");
2643 break;
2644 case LE:
2645 fprintf (file, "<=");
2646 break;
2647 case LEU:
2648 fprintf (file, "<={U}");
2649 break;
2650 case GT:
2651 fprintf (file, ">");
2652 break;
2653 case GTU:
2654 fprintf (file, ">{U}");
2655 break;
2656 default:
2657 gcc_unreachable();
2660 else
2662 /* Output the comparison formatted as operand,symbol,operand */
2663 rtx op0 = XEXP (operand, 0);
2664 rtx op1 = XEXP (operand, 1);
2666 picochip_print_operand (file, op0, 0);
2667 picochip_print_comparison (file, operand, 'i');
2668 picochip_print_operand (file, op1, 0);
2672 /* This function generates a memory address operand in the given
2673 mode. That is, if the address contains a constant offset, then the
2674 offset is divided by the required mode size to compute the
2675 mode specific offset. By default, picochip_print_operand_address calls
2676 this function using the natural mode of the operand, but special
2677 operand codes can be used to invoke the computation using an
2678 unnatural mode (e.g., compute the HI aligned address of an SI mode
2679 address). */
2680 static void
2681 picochip_print_memory_address (FILE * file, rtx operand,
2682 enum machine_mode mode)
2684 rtx address = XEXP (operand, 0);
2686 /* Sanity check. */
2687 if (MEM != GET_CODE (operand))
2688 fatal_insn ("picochip_print_memory_address - Operand isn't memory based",
2689 operand);
2691 if (TARGET_DEBUG)
2693 printf ("picochip_print_memory_address: ");
2694 print_rtl (stdout, operand);
2695 printf ("\n");
2698 switch (GET_CODE (address))
2700 case PLUS:
2702 /* Grab the address components. */
2703 rtx base = XEXP (address, 0);
2704 rtx offset = XEXP (address, 1);
2706 /* Only handle reg+const addresses */
2707 if (REG == GET_CODE (base) && CONST_INT == GET_CODE (offset))
2709 /* Sanity check. If an FP+offset address is given, ensure
2710 that the offset lies within the given frame, or a lower
2711 frame. */
2712 if (REGNO (base) == STACK_POINTER_REGNUM )
2713 gcc_assert (INTVAL (offset) <= (picochip_arg_area_byte_offset () +
2714 crtl->args.size));
2716 /* Print the base register - identical for all modes. */
2717 fprintf (file, "(");
2718 picochip_print_operand (file, base, 'r');
2719 fprintf (file, ")");
2721 /* Print the constant offset with compensation for the mode. */
2722 switch (mode)
2724 case QImode:
2725 picochip_print_operand (file, offset, 'Q');
2726 break;
2728 case HImode:
2729 picochip_print_operand (file, offset, 'H');
2730 break;
2732 case SImode:
2733 case SFmode:
2734 picochip_print_operand (file, offset, 'S');
2735 break;
2737 case DImode:
2738 picochip_print_operand (file, offset, 'D');
2739 break;
2741 default:
2742 gcc_unreachable();
2749 break;
2751 case SYMBOL_REF:
2752 picochip_print_operand (file, address, 's');
2753 break;
2755 case CONST:
2757 rtx inner;
2758 rtx base;
2759 rtx offset;
2761 inner = XEXP (address, 0);
2763 /* Sanity check - the CONST memory address must be a base+offset. */
2764 gcc_assert (PLUS == GET_CODE (inner));
2766 base = XEXP (inner, 0);
2767 offset = XEXP (inner, 1);
2769 fprintf (file, "&_%s%+d", XSTR (base, 0), XINT (offset, 0));
2771 break;
2774 case REG:
2775 /* Register operand. Provide a zero offset. */
2776 fprintf (file, "(");
2777 picochip_print_operand (file, address, 'r');
2778 fprintf (file, ")0");
2779 break;
2781 default:
2782 gcc_unreachable();
2787 /* Output an operand. Formatting letters allow particular parts of
2788 the operand to be output. */
2789 void
2790 picochip_print_operand (FILE * file, rtx operand, int letter)
2793 /* Handle special cases. */
2794 switch (letter)
2796 /* VLIW continuation, for explicit VLIW sequences. */
2797 case '|':
2798 fprintf (file, "\\");
2799 return;
2801 /* ALU selector. */
2802 case '#':
2803 fputc (picochip_get_vliw_alu_id (), file);
2804 return;
2806 /* Delay slot specifier. */
2807 case '>':
2808 /* This should be handled in asm_output_opcode. */
2809 gcc_unreachable();
2811 /* Instruction mnemonics (e.g., lshift becomes LSL). */
2812 case 'I':
2813 switch (GET_CODE (operand))
2815 case AND:
2816 fprintf (file, "AND");
2817 break;
2818 case IOR:
2819 fprintf (file, "OR");
2820 break;
2821 case XOR:
2822 fprintf (file, "XOR");
2823 break;
2824 case PLUS:
2825 fprintf (file, "ADD");
2826 break;
2827 case MINUS:
2828 fprintf (file, "SUB");
2829 break;
2830 default:
2831 gcc_unreachable();
2833 return;
2835 /* Symbolic instructions (e.g., lshift becomes <<). */
2836 case 'i':
2837 switch (GET_CODE (operand))
2839 case AND:
2840 fprintf (file, "&");
2841 break;
2842 case IOR:
2843 fprintf (file, "|");
2844 break;
2845 case XOR:
2846 fprintf (file, "^");
2847 break;
2848 case PLUS:
2849 fprintf (file, "+");
2850 break;
2851 case MINUS:
2852 fprintf (file, "-");
2853 break;
2854 default:
2855 fprintf (file, "UNKNOWN_INSN");
2856 break;
2858 return;
2860 default: /* Not a punctuation character - process as normal. */
2861 break;
2864 switch (GET_CODE (operand))
2866 case REG:
2867 switch (letter)
2869 case 'R':
2870 /* Write a range of registers. */
2871 fprintf (file, "R[%d:%d]", REGNO (operand) + 1, REGNO (operand));
2872 break;
2874 case 'U':
2875 /* The upper register of a pair is requested. */
2876 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 1]);
2877 break;
2879 case 'L':
2880 /* The lower register of a pair is requested. Equivalent to the
2881 default, but included for completeness. */
2882 fprintf (file, "%s", picochip_regnames[REGNO (operand)]);
2883 break;
2885 case 'X':
2886 /* The 3rd register of a DI mode register. */
2887 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 2]);
2888 break;
2890 case 'Y':
2891 /* The 4th register of a DI mode register. */
2892 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 3]);
2893 break;
2895 default:
2896 fprintf (file, "%s", picochip_regnames[REGNO (operand)]);
2898 break;
2900 case CONST_INT:
2901 /* A range of letters can be used to format integers. The
2902 letters Q/H/S are used to divide the constant by the width of
2903 QI/HI/SI mode integers in bytes. The U/L modifiers are used
2904 to obtain the upper and lower 16-bits of a 32-bit
2905 constant. Where possible, signed numbers are used, since
2906 signed representations of numbers may be more compact (e.g.,
2907 65535 can be represented as -1, which fits into a small
2908 constant, whereas 65535 requires a large constant). */
2909 switch (letter)
2911 case 'Q':
2912 fprintf (file, "%ld", INTVAL (operand));
2913 break;
2915 case 'H':
2916 fprintf (file, "%ld", INTVAL (operand) / 2);
2917 break;
2919 case 'S':
2920 fprintf (file, "%ld", INTVAL (operand) / 4);
2921 break;
2923 case 'P':
2924 fprintf (file, "%d", exact_log2 (INTVAL(operand)));
2925 break;
2927 case 'U':
2928 fprintf (file, "%hi", (short) ((INTVAL (operand) >> 16) & 0xFFFF));
2929 break;
2931 case 'L':
2932 fprintf (file, "%hi", (short) (INTVAL (operand) & 0xFFFF));
2933 break;
2935 default:
2936 fprintf (file, "%ld", INTVAL (operand));
2937 break;
2939 break;
2941 case CONST_DOUBLE:
2943 long val;
2944 REAL_VALUE_TYPE rv;
2946 if (GET_MODE (operand) != SFmode)
2947 fatal_insn ("Unknown mode in print_operand (CONST_DOUBLE) :",
2948 operand);
2949 REAL_VALUE_FROM_CONST_DOUBLE (rv, operand);
2950 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
2952 switch (letter)
2954 case 'U':
2955 fprintf (file, "%hi", (short) ((val >> 16) & 0xFFFF));
2956 break;
2958 case 'L':
2959 fprintf (file, "%hi", (short) (val & 0xFFFF));
2960 break;
2963 break;
2967 /* Output a symbol. The output format must match that of
2968 picochip_output_label. */
2969 case SYMBOL_REF:
2970 /* Ensure that the symbol is marked as referenced. Gcc can
2971 occasionally omit the function bodies when it believes them
2972 to be unreferenced. */
2973 if (SYMBOL_REF_DECL (operand))
2974 mark_decl_referenced (SYMBOL_REF_DECL (operand));
2975 fprintf (file, "&");
2976 assemble_name (file, XSTR (operand, 0));
2977 break;
2979 case LABEL_REF:
2980 /* This format must match that of picochip_output_label. */
2981 fprintf (file, "&");
2982 output_asm_label (operand);
2983 break;
2985 case MEM:
2987 rtx addr = XEXP (operand, 0);
2989 switch (letter)
2991 case 'o':
2992 if (PLUS != GET_CODE (addr))
2993 fatal_insn ("Bad address, not (reg+disp):", addr);
2994 else
2995 picochip_print_operand (file, XEXP (addr, 1), 0);
2996 break;
2998 case 'M':
2999 /* Output a memory address in byte mode notation (i.e., the
3000 constant address (if any) is the actual byte address. */
3001 picochip_print_memory_address (file, operand, QImode);
3002 break;
3004 /* Output a constant offset of the given mode (i.e., divide
3005 the constant by the number of units in the mode to get the
3006 constant). */
3007 case 'Q':
3008 picochip_print_memory_address (file, operand, QImode);
3009 break;
3011 case 'H':
3012 picochip_print_memory_address (file, operand, HImode);
3013 break;
3015 case 'S':
3016 picochip_print_memory_address (file, operand, SImode);
3017 break;
3019 case 'F':
3020 picochip_print_memory_address (file, operand, SFmode);
3021 break;
3023 case 'b':
3024 if (PLUS != GET_CODE (addr))
3025 fatal_insn ("Bad address, not (reg+disp):", addr);
3026 else
3027 picochip_print_operand (file, XEXP (addr, 0), 0);
3028 break;
3030 /* When the mem operand is (reg + big offset) which cannot
3031 be represented in an instruction as operand, the compiler
3032 automatically generates the instruction to put in (reg +
3033 big offset) into another register. In such cases, it
3034 returns '0' as the character. This needs to be handled
3035 as well. */
3036 case 0:
3037 case 'r':
3038 if (REG != GET_CODE (addr))
3039 fatal_insn ("Bad address, not register:", addr);
3040 else
3041 picochip_print_operand (file, addr, 0);
3042 break;
3044 default:
3045 fprintf (file, "Unknown mem operand - letter %c ",
3046 (char) (letter));
3047 print_rtl (file, operand);
3050 break;
3053 case CONST:
3055 rtx const_exp = XEXP (operand, 0);
3057 /* Handle constant offsets to symbol references. */
3058 if (PLUS == GET_CODE (const_exp) &&
3059 SYMBOL_REF == GET_CODE (XEXP (const_exp, 0)) &&
3060 CONST_INT == GET_CODE (XEXP (const_exp, 1)))
3063 picochip_print_operand (file, XEXP (const_exp, 0), 0);
3064 if (INTVAL (XEXP (const_exp, 1)) >= 0)
3065 fprintf (file, "+");
3066 /* else use the - from the operand (i.e., AP-2)) */
3068 picochip_print_operand (file, XEXP (const_exp, 1), letter);
3072 break;
3075 case PLUS:
3077 /* PLUS expressions are of the form (base + offset). Different
3078 options (analagous to those of memory PLUS expressions) are used
3079 to extract the base and offset components. */
3081 switch (letter)
3083 case 'b':
3084 picochip_print_operand (file, XEXP (operand, 0), 0);
3085 break;
3087 case 'o':
3088 picochip_print_operand (file, XEXP (operand, 1), 0);
3089 break;
3091 default:
3093 /* If the expression is composed entirely of constants,
3094 evaluate the result. This should only occur with the
3095 picoChip specific comms instructions, which are emitted as
3096 base+offset expressions. */
3097 if (CONST_INT == GET_CODE (XEXP (operand, 0)) &&
3098 CONST_INT == GET_CODE (XEXP (operand, 1)))
3100 HOST_WIDE_INT result = (XINT (XEXP (operand, 0), 0) +
3101 XINT (XEXP (operand, 1), 0));
3102 fprintf (file, "%ld", result);
3104 else
3106 fprintf (file, "(");
3107 picochip_print_operand (file, XEXP (operand, 0), 0);
3108 fprintf (file, "+");
3109 picochip_print_operand (file, XEXP (operand, 1), 0);
3110 fprintf (file, ")");
3114 break;
3117 /* Comparison operations. */
3118 case NE:
3119 case EQ:
3120 case GE:
3121 case GEU:
3122 case LT:
3123 case LTU:
3124 case LE:
3125 case LEU:
3126 case GT:
3127 case GTU:
3128 picochip_print_comparison (file, operand, letter);
3129 return;
3131 default:
3132 fprintf (stderr, "Unknown operand encountered in %s\n", __FUNCTION__);
3133 print_rtl (file, operand);
3134 break;
3140 /* Output an operand address */
3141 void
3142 picochip_print_operand_address (FILE * file, rtx operand)
3145 switch (GET_CODE (operand))
3148 case SYMBOL_REF:
3149 /* This format must match that of picochip_output_label. */
3150 assemble_name (file, XSTR (operand, 0));
3151 break;
3153 case CODE_LABEL:
3154 /* Note this format must match that of picochip_output_label. */
3155 fprintf (file, "_L%d", XINT (operand, 5));
3156 break;
3158 case MEM:
3159 /* Pass on to a specialised memory address generator. */
3160 picochip_print_memory_address (file, operand, GET_MODE (operand));
3161 break;
3163 default:
3164 gcc_unreachable();
3171 /* Scheduling functions. */
3173 /* Save some of the contents of recog_data. */
3174 static void
3175 picochip_save_recog_data (void)
3177 picochip_saved_which_alternative = which_alternative;
3178 memcpy (&picochip_saved_recog_data, &recog_data,
3179 sizeof (struct recog_data));
3182 /* Restore some of the contents of global variable recog_data. */
3183 static void
3184 picochip_restore_recog_data (void)
3186 which_alternative = picochip_saved_which_alternative;
3187 memcpy (&recog_data, &picochip_saved_recog_data,
3188 sizeof (struct recog_data));
3191 /* Ensure that no var tracking notes are emitted in the middle of a
3192 three-instruction bundle. */
3193 static void
3194 reorder_var_tracking_notes (void)
3196 basic_block bb;
3198 FOR_EACH_BB (bb)
3200 rtx insn, next, last_insn = NULL_RTX;
3201 rtx queue = NULL_RTX;
3203 /* Iterate through the bb and find the last non-debug insn */
3204 for (insn = BB_HEAD (bb); insn != NEXT_INSN(BB_END (bb)); insn = NEXT_INSN(insn))
3206 if (NONDEBUG_INSN_P(insn))
3207 last_insn = insn;
3210 /* In all normal cases, queue up notes and emit them just before a TImode
3211 instruction. For the last instruction, emit the queued notes just after
3212 the last instruction. */
3213 for (insn = BB_HEAD (bb); insn != NEXT_INSN(BB_END (bb)); insn = next)
3215 next = NEXT_INSN (insn);
3217 if (insn == last_insn)
3219 while (queue)
3221 rtx next_queue = PREV_INSN (queue);
3222 PREV_INSN (NEXT_INSN(insn)) = queue;
3223 NEXT_INSN(queue) = NEXT_INSN(insn);
3224 PREV_INSN(queue) = insn;
3225 NEXT_INSN(insn) = queue;
3226 queue = next_queue;
3228 /* There is no more to do for this bb. break*/
3229 break;
3231 else if (NONDEBUG_INSN_P (insn))
3233 /* Emit queued up notes before the first instruction of a bundle. */
3234 if (GET_MODE (insn) == TImode)
3236 while (queue)
3238 rtx next_queue = PREV_INSN (queue);
3239 NEXT_INSN (PREV_INSN(insn)) = queue;
3240 PREV_INSN (queue) = PREV_INSN(insn);
3241 PREV_INSN (insn) = queue;
3242 NEXT_INSN (queue) = insn;
3243 queue = next_queue;
3247 else if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
3249 rtx prev = PREV_INSN (insn);
3250 PREV_INSN (next) = prev;
3251 NEXT_INSN (prev) = next;
3252 PREV_INSN (insn) = queue;
3253 queue = insn;
3256 /* Make sure we are not dropping debug instructions.*/
3257 gcc_assert (queue == NULL_RTX);
3261 /* Perform machine dependent operations on the rtl chain INSNS. */
3262 void
3263 picochip_reorg (void)
3265 rtx insn, insn1, vliw_start = NULL_RTX;
3266 int vliw_insn_location = 0;
3268 /* We are freeing block_for_insn in the toplev to keep compatibility
3269 with old MDEP_REORGS that are not CFG based. Recompute it now. */
3270 compute_bb_for_insn ();
3272 if (optimize == 0)
3273 split_all_insns ();
3275 if (picochip_schedule_type != DFA_TYPE_NONE)
3277 timevar_push (TV_SCHED2);
3279 /* Process the instruction list, computing the sizes of each
3280 instruction, and consequently branch distances. This can
3281 result in some branches becoming short enough to be treated
3282 as a real branch instruction, rather than an assembly branch
3283 macro which may expand into multiple instructions. The
3284 benefit of shortening branches is that real branch
3285 instructions can be properly DFA scheduled, whereas macro
3286 branches cannot. */
3287 shorten_branches (get_insns ());
3289 /* Do control and data sched analysis again,
3290 and write some more of the results to dump file. */
3292 split_all_insns ();
3294 schedule_ebbs ();
3296 timevar_pop (TV_SCHED2);
3298 ggc_collect ();
3300 if (picochip_schedule_type == DFA_TYPE_SPEED)
3302 /* Whenever a VLIW packet is generated, all instructions in
3303 that packet must appear to come from the same source
3304 location. The following code finds all the VLIW packets,
3305 and tags their instructions with the location of the first
3306 instruction from the packet. Clearly this will result in
3307 strange behaviour when debugging the code, but since
3308 debugging and optimisation are being used in conjunction,
3309 strange behaviour is certain to occur anyway. */
3310 /* Slight bit of change. If the vliw set contains a branch
3311 or call instruction, we pick its location.*/
3312 for (insn = get_insns (); insn; insn = next_real_insn (insn))
3315 /* If this is the first instruction in the VLIW packet,
3316 extract its location. */
3317 if (GET_MODE (insn) == TImode)
3319 vliw_start = insn;
3320 vliw_insn_location = INSN_LOCATOR (insn);
3322 if (JUMP_P (insn) || CALL_P(insn))
3324 vliw_insn_location = INSN_LOCATOR (insn);
3325 for (insn1 = vliw_start; insn1 != insn ; insn1 = next_real_insn (insn1))
3326 INSN_LOCATOR (insn1) = vliw_insn_location;
3328 /* Tag subsequent instructions with the same location. */
3329 INSN_LOCATOR (insn) = vliw_insn_location;
3335 /* Locate the note marking the end of the function's prologue. If
3336 the note appears in the middle of a VLIW packet, move the note to
3337 the end. This avoids unpleasant consequences such as trying to
3338 emit prologue markers (e.g., .loc/.file directives) in the middle
3339 of VLIW packets. */
3340 if (picochip_schedule_type == DFA_TYPE_SPEED)
3342 rtx prologue_end_note = NULL;
3343 rtx last_insn_in_packet = NULL;
3345 for (insn = get_insns (); insn; insn = next_insn (insn))
3347 /* The prologue end must be moved to the end of the VLIW packet. */
3348 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
3350 prologue_end_note = insn;
3351 break;
3355 /* Find the last instruction in this packet. */
3356 for (insn = prologue_end_note; insn; insn = next_real_insn (insn))
3358 if (GET_MODE (insn) == TImode)
3359 break;
3360 else
3361 last_insn_in_packet = insn;
3364 if (last_insn_in_packet != NULL)
3366 rtx tmp_note
3367 = emit_note_after ((enum insn_note) NOTE_KIND (prologue_end_note),
3368 last_insn_in_packet);
3369 memcpy(&NOTE_DATA (tmp_note), &NOTE_DATA(prologue_end_note), sizeof(NOTE_DATA(prologue_end_note)));
3370 delete_insn (prologue_end_note);
3373 if (picochip_flag_var_tracking)
3375 timevar_push (TV_VAR_TRACKING);
3376 variable_tracking_main ();
3377 /* We also have to deal with variable tracking notes in the middle
3378 of VLIW packets. */
3379 reorder_var_tracking_notes();
3380 timevar_pop (TV_VAR_TRACKING);
3384 /* Return the ALU character identifier for the current
3385 instruction. This will be 0 or 1. */
3386 static char
3387 picochip_get_vliw_alu_id (void)
3389 int attr_type = 0;
3391 /* Always use ALU 0 if VLIW scheduling is disabled. */
3392 if (picochip_schedule_type != DFA_TYPE_SPEED)
3393 return '0';
3395 /* Get the attribute type of the instruction. Note that this can
3396 ruin the contents of recog_data, so save/restore around the
3397 call. */
3398 picochip_save_recog_data ();
3399 attr_type = get_attr_type (picochip_current_prescan_insn);
3400 picochip_restore_recog_data ();
3402 if (picochip_current_vliw_state.contains_pico_alu_insn)
3405 /* If this a picoAlu insn? If it is, then stuff it into ALU 0,
3406 else it must be the other ALU (either basic or nonCc)
3407 instruction which goes into 1. */
3408 if (attr_type == TYPE_PICOALU)
3409 return '0';
3410 else
3411 return '1';
3414 else if (picochip_current_vliw_state.contains_non_cc_alu_insn)
3416 /* Is this the non CC instruction? If it is, then stuff it into
3417 ALU 1, else it must be a picoAlu or basicAlu, in which case
3418 it goes into ALU 0. */
3419 if (attr_type == TYPE_NONCCALU)
3420 return '1';
3421 else
3422 return '0';
3424 else
3426 /* No picoAlu/nonCc instructions in use, so purely dependent upon
3427 whether an ALU instruction has already been scheduled in this
3428 cycle. */
3429 switch (picochip_current_vliw_state.num_alu_insns_so_far)
3431 case 0:
3432 picochip_current_vliw_state.num_alu_insns_so_far++;
3433 return '0';
3435 case 1:
3436 picochip_current_vliw_state.num_alu_insns_so_far++;
3437 return '1';
3439 default:
3440 internal_error ("too many ALU instructions emitted (%d)",
3441 picochip_current_vliw_state.num_alu_insns_so_far);
3442 return 'X';
3448 /* Reset any information about the current VLIW packing status. */
3449 static void
3450 picochip_reset_vliw (rtx insn)
3452 rtx local_insn = insn;
3454 /* Nothing to do if VLIW scheduling isn't being used. */
3455 if (picochip_schedule_type != DFA_TYPE_SPEED)
3456 return;
3458 if (TARGET_DEBUG)
3459 printf ("%s on insn %d\n", __FUNCTION__, INSN_UID (insn));
3461 /* Reset. */
3462 picochip_current_vliw_state.contains_pico_alu_insn = 0;
3463 picochip_current_vliw_state.contains_non_cc_alu_insn = 0;
3464 picochip_current_vliw_state.num_alu_insns_so_far = 0;
3465 picochip_current_vliw_state.num_cfi_labels_deferred = 0;
3466 picochip_current_vliw_state.lm_label_name[0] = 0;
3467 picochip_current_vliw_state.num_insns_in_packet = 0;
3469 /* Read through the VLIW packet, classifying the instructions where
3470 appropriate. */
3471 local_insn = insn;
3474 if (NOTE_P (local_insn) || DEBUG_INSN_P(local_insn))
3476 local_insn = NEXT_INSN (local_insn);
3477 continue;
3479 else if (!INSN_P (local_insn))
3480 break;
3481 else
3483 /* It is an instruction, but is it ours? */
3484 if (INSN_CODE (local_insn) != -1)
3486 int attr_type = 0;
3488 picochip_current_vliw_state.num_insns_in_packet += 1;
3490 /* Is it a picoAlu or nonCcAlu instruction? Note that the
3491 get_attr_type function can overwrite the values in
3492 the recog_data global, hence this is saved and
3493 restored around the call. Not doing so results in
3494 asm_output_opcode being called with a different
3495 instruction to final_prescan_insn, which is fatal. */
3496 picochip_save_recog_data ();
3497 attr_type = get_attr_type (local_insn);
3498 picochip_restore_recog_data ();
3500 if (attr_type == TYPE_PICOALU)
3501 picochip_current_vliw_state.contains_pico_alu_insn = 1;
3502 if (attr_type == TYPE_NONCCALU)
3503 picochip_current_vliw_state.contains_non_cc_alu_insn = 1;
3508 /* Get the next instruction. */
3509 local_insn = NEXT_INSN (local_insn);
3511 /* Keep going while the next instruction is part of the same
3512 VLIW packet (i.e., its a valid instruction and doesn't mark
3513 the start of a new VLIW packet. */
3515 while (local_insn &&
3516 (GET_MODE (local_insn) != TImode) && (INSN_CODE (local_insn) != -1));
3521 picochip_sched_reorder (FILE * file, int verbose,
3522 rtx * ready ATTRIBUTE_UNUSED,
3523 int *n_readyp ATTRIBUTE_UNUSED, int clock)
3526 if (verbose > 0)
3527 fprintf (file, ";;\tClock %d\n", clock);
3529 return picochip_sched_issue_rate ();
3534 picochip_sched_lookahead (void)
3536 /* It should always be enough to lookahead by 2 insns. Only slot0/1 could
3537 have a conflict. */
3538 return 2;
3542 picochip_sched_issue_rate (void)
3544 return 3;
3547 /* Adjust the scheduling cost between the two given instructions,
3548 which have the given dependency. */
3550 picochip_sched_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
3553 if (TARGET_DEBUG)
3555 printf ("Sched Adjust Cost: %d->%d is %d\n",
3556 INSN_UID (insn), INSN_UID (dep_insn), cost);
3558 printf (" Dependency type:");
3559 switch (REG_NOTE_KIND (link))
3561 case 0:
3562 printf ("Data\n");
3563 break;
3564 case REG_DEP_ANTI:
3565 printf ("ANTI\n");
3566 break;
3567 case REG_DEP_OUTPUT:
3568 printf ("OUTPUT\n");
3569 break;
3570 default:
3571 printf ("Unknown (%d)\n", REG_NOTE_KIND (link));
3575 /* Anti-dependencies are used to enforce the ordering between a
3576 * branch, and any subsequent instructions. For example:
3578 * BNE someLabel
3579 * ADD.0 r0,r1,r2
3581 * The ADD instruction must execute after the branch, and this is
3582 * enforced using an anti-dependency. Unfortunately, VLIW machines
3583 * are happy to execute anti-dependent instructions in the same
3584 * cycle, which then results in a schedule like the following being
3585 * created:
3587 * BNE someLabel \ ADD.0 r0,r1,r2
3589 * The instruction which would normally be conditionally executed
3590 * depending upon the outcome of the branch, is now unconditionally
3591 * executed every time. To prevent this happening, any
3592 * anti-dependencies between a branch and another instruction are
3593 * promoted to become real dependencies.
3595 if ((JUMP_P (dep_insn) || CALL_P(dep_insn)) && REG_NOTE_KIND (link) == REG_DEP_ANTI)
3598 if (TARGET_DEBUG)
3599 printf ("Promoting anti-dependency %d->%d to a true-dependency\n",
3600 INSN_UID (insn), INSN_UID (dep_insn));
3602 return 1;
3605 return cost;
3609 /* Return the minimum of the two values */
3610 static int
3611 minimum (int a, int b)
3613 if (a < b)
3614 return a;
3615 if (b < a)
3616 return b;
3617 /* I dont expect to get to this function with a==b.*/
3618 gcc_unreachable();
3622 /* This function checks if the memory of the two stores are just off by 2 bytes.
3623 It returns the lower memory operand's index.*/
3625 static int
3626 memory_just_off (rtx opnd1, rtx opnd2)
3628 int offset1 = 0, offset2 = 0;
3629 int reg1, reg2;
3631 if (GET_CODE(XEXP(opnd1, 0)) == PLUS && GET_CODE(XEXP(XEXP(opnd1, 0),1)) == CONST_INT)
3633 offset1 = INTVAL(XEXP(XEXP(opnd1, 0), 1));
3634 reg1 = REGNO(XEXP(XEXP(opnd1, 0), 0));
3636 else
3638 reg1 = REGNO(XEXP(opnd1, 0));
3640 if (GET_CODE(XEXP(opnd2, 0)) == PLUS && GET_CODE(XEXP(XEXP(opnd2, 0), 1)) == CONST_INT)
3642 offset2 = INTVAL(XEXP(XEXP(opnd2, 0), 1));
3643 reg2 = REGNO(XEXP(XEXP(opnd2, 0), 0));
3645 else
3647 reg2 = REGNO(XEXP(opnd2, 0));
3650 /* Peepholing 2 STW/LDWs has the restriction that the resulting STL/LDL's address
3651 should be 4 byte aligned. We can currently guarentee that only if the base
3652 address is FP(R13) and the offset is aligned. */
3654 if (reg1 == reg2 && reg1 == 13 && abs(offset1-offset2) == 2 && minimum(offset1, offset2) % 4 == 0)
3655 return (minimum(offset1, offset2) == offset1) ? 1:2;
3657 return 0;
3660 static int
3661 registers_just_off (rtx opnd1, rtx opnd2)
3663 int reg1, reg2;
3664 reg1 = REGNO(opnd1);
3665 reg2 = REGNO(opnd2);
3666 if (abs(reg1-reg2) == 1 && minimum(reg1, reg2) % 2 == 0)
3667 return (minimum(reg1, reg2) == reg1)?1:2;
3668 return 0;
3671 /* Check to see if the two LDWs can be peepholed together into a LDL
3672 They can be if the registers getting loaded into are contiguous
3673 and the memory addresses are contiguous as well.
3674 for eg.
3675 LDW r2,[r11]x
3676 LDW r3,[r11]x+1
3677 can be merged together into
3678 LDL r[3:2],[r11]
3680 NOTE:
3681 1. The LDWs themselves only guarentee that r11 will be a 2-byte
3682 aligned address. Only FP can be assumed to be 4 byte aligned.
3683 2. The progression of addresses and the register numbers should
3684 be similar. For eg., if you swap r2 and r3 in the above instructions,
3685 the resultant pair cannot be merged.
3688 bool
3689 ok_to_peephole_ldw(rtx opnd0, rtx opnd1, rtx opnd2, rtx opnd3)
3691 int memtest=0,regtest=0;
3692 regtest = registers_just_off(opnd1,opnd3);
3693 if (regtest == 0)
3694 return false;
3696 memtest = memory_just_off(opnd0,opnd2);
3697 if (memtest == 0)
3698 return false;
3700 if (regtest == memtest)
3702 return true;
3704 return false;
3707 /* Similar to LDW peephole */
3708 bool
3709 ok_to_peephole_stw(rtx opnd0, rtx opnd1, rtx opnd2, rtx opnd3)
3711 int memtest=0,regtest=0;
3712 regtest = registers_just_off(opnd1,opnd3);
3713 if (regtest == 0)
3714 return false;
3716 memtest = memory_just_off(opnd0,opnd2);
3717 if (memtest == 0)
3718 return false;
3720 if (regtest == memtest)
3722 return true;
3724 return false;
3728 /* Generate a SImode register with the register number that is the smaller of the two */
3730 gen_min_reg(rtx opnd1,rtx opnd2)
3732 return gen_rtx_REG (SImode, minimum(REGNO(opnd1),REGNO(opnd2)));
3735 /* Generate a SImode memory with the address that is the smaller of the two */
3737 gen_SImode_mem(rtx opnd1,rtx opnd2)
3739 int offset1=0,offset2=0;
3740 rtx reg;
3741 rtx address;
3742 if (GET_CODE(XEXP(opnd1,0)) == PLUS && GET_CODE(XEXP(XEXP(opnd1,0),1)) == CONST_INT)
3744 offset1 = INTVAL(XEXP(XEXP(opnd1,0),1));
3745 reg = XEXP(XEXP(opnd1,0),0);
3747 else
3749 reg = XEXP(opnd1,0);
3751 if (GET_CODE(XEXP(opnd2,0)) == PLUS && GET_CODE(XEXP(XEXP(opnd2,0),1)) == CONST_INT)
3753 offset2 = INTVAL(XEXP(XEXP(opnd2,0),1));
3755 address = gen_rtx_PLUS (HImode, reg, GEN_INT(minimum(offset1,offset2)));
3756 return gen_rtx_MEM(SImode,address);
3759 bool
3760 picochip_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, int* total, bool speed)
3763 int localTotal = 0;
3765 if (!speed)
3767 /* Need to penalize immediates that need to be encoded as long constants.*/
3768 if (code == CONST_INT && !(INTVAL (x) >= 0 && INTVAL (x) < 16))
3770 *total = COSTS_N_INSNS(1);
3771 return true;
3774 switch (code)
3776 case SYMBOL_REF:
3777 case LABEL_REF:
3778 *total = COSTS_N_INSNS (outer_code != MEM);
3779 return true;
3780 break;
3782 case IF_THEN_ELSE:
3783 /* if_then_else come out of cbranch instructions. It will get split into
3784 a condition code generating subtraction and a branch */
3785 *total = COSTS_N_INSNS (2);
3786 return true;
3787 break;
3789 case AND:
3790 case IOR:
3791 case XOR:
3792 if (GET_MODE(x) == SImode)
3793 *total = COSTS_N_INSNS (2);
3794 if (GET_MODE(x) == DImode)
3795 *total = COSTS_N_INSNS (4);
3796 return false;
3798 case MEM:
3799 /* Byte Memory access on a NO_BYTE_ACCESS machine would be expensive */
3800 if (GET_MODE(x) == QImode && !TARGET_HAS_BYTE_ACCESS)
3801 *total = COSTS_N_INSNS (10);
3803 /* 64-bit accesses have to be done through 2 32-bit access */
3804 if (GET_MODE(x) == DImode)
3805 *total = COSTS_N_INSNS (2);
3806 return false;
3807 break;
3809 case ASHIFTRT:
3811 /* SImode shifts are expensive */
3812 if (GET_MODE(x) == SImode)
3813 *total = COSTS_N_INSNS (10);
3815 /* Register shift by constant is cheap. */
3816 if ((GET_MODE(x) == QImode || GET_MODE(x) == HImode)
3817 && GET_CODE(XEXP(x, 0)) == REG
3818 && GET_CODE(XEXP(x, 1)) == CONST_INT)
3819 *total = COSTS_N_INSNS (1);
3820 else
3821 *total = COSTS_N_INSNS (4);
3822 return false;
3823 break;
3825 case DIV:
3826 case MOD:
3828 /* Divisions are more expensive than the default 7*/
3829 if (GET_MODE(x) == SImode)
3830 *total = COSTS_N_INSNS (20);
3831 else
3832 *total = COSTS_N_INSNS (12);
3833 return false;
3834 break;
3836 case MULT:
3837 /* Look for the simple cases of multiplying register*register or
3838 register*constant. */
3839 if ((GET_MODE(x) == QImode || GET_MODE(x) == HImode)
3840 && ((GET_CODE(XEXP(x, 0)) == REG
3841 && (GET_CODE(XEXP(x, 1)) == REG || GET_CODE(XEXP(x,1)) == CONST_INT))
3842 || (GET_CODE(XEXP(x, 0)) == ZERO_EXTEND
3843 && GET_CODE(XEXP(XEXP(x, 0),0)) == REG
3844 && GET_CODE(XEXP(x, 1)) == ZERO_EXTEND
3845 && GET_CODE(XEXP(XEXP(x, 1),0)) == REG)))
3848 /* When optimising for size, multiplication by constant
3849 should be discouraged slightly over multiplication by a
3850 register. */
3851 if (picochip_has_mac_unit)
3853 /* Single cycle multiplication, but the result must be
3854 loaded back into a general register afterwards. */
3855 *total = COSTS_N_INSNS(2);
3856 return true;
3858 else if (picochip_has_mul_unit)
3860 /* Single cycle multiplication. */
3861 *total = COSTS_N_INSNS(1);
3862 return true;
3864 /* Else no multiply available. Use default cost. */
3867 break;
3869 default:
3870 /* Do nothing. */
3871 break;
3874 if (localTotal != 0)
3876 *total = localTotal;
3877 return true;
3879 else
3881 return false;
3886 void
3887 picochip_final_prescan_insn (rtx insn, rtx * opvec ATTRIBUTE_UNUSED,
3888 int num_operands ATTRIBUTE_UNUSED)
3890 rtx local_insn;
3892 picochip_current_prescan_insn = insn;
3894 if (TARGET_DEBUG)
3895 printf ("Final prescan on INSN %d with mode %s\n",
3896 INSN_UID (insn), GET_MODE_NAME (GET_MODE (insn)));
3898 /* If this is the start of a new instruction cycle, or no scheduling
3899 is used, then reset the VLIW status. */
3900 if (GET_MODE (insn) == TImode || !picochip_schedule_type == DFA_TYPE_SPEED)
3901 picochip_reset_vliw (insn);
3903 /* No VLIW scheduling occured, so don't go any further. */
3904 if (picochip_schedule_type != DFA_TYPE_SPEED)
3905 return;
3907 /* Look for the next printable instruction. This loop terminates on
3908 any recognisable instruction, and on any unrecognisable
3909 instruction with TImode. */
3910 local_insn = insn;
3911 for (local_insn = NEXT_INSN (local_insn); local_insn;
3912 local_insn = NEXT_INSN (local_insn))
3914 if (NOTE_P (local_insn) || DEBUG_INSN_P(local_insn))
3915 continue;
3916 else if (!INSN_P (local_insn))
3917 break;
3918 else if (GET_MODE (local_insn) == TImode
3919 || INSN_CODE (local_insn) != -1)
3920 break;
3923 /* Set the continuation flag if the next instruction can be packed
3924 with the current instruction (i.e., the next instruction is
3925 valid, and isn't the start of a new cycle). */
3926 picochip_vliw_continuation = (local_insn && NONDEBUG_INSN_P (local_insn) &&
3927 (GET_MODE (local_insn) != TImode));
3931 /* Builtin functions. */
3932 /* Given a builtin function taking 2 operands (i.e., target + source),
3933 emit the RTL for the underlying instruction. */
3934 static rtx
3935 picochip_expand_builtin_2op (enum insn_code icode, tree call, rtx target)
3937 tree arg0;
3938 rtx op0, pat;
3939 enum machine_mode tmode, mode0;
3941 /* Grab the incoming argument and emit its RTL. */
3942 arg0 = CALL_EXPR_ARG (call, 0);
3943 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3945 /* Determine the modes of the instruction operands. */
3946 tmode = insn_data[icode].operand[0].mode;
3947 mode0 = insn_data[icode].operand[1].mode;
3949 /* Ensure that the incoming argument RTL is in a register of the
3950 correct mode. */
3951 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
3952 op0 = copy_to_mode_reg (mode0, op0);
3954 /* If there isn't a suitable target, emit a target register. */
3955 if (target == 0
3956 || GET_MODE (target) != tmode
3957 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
3958 target = gen_reg_rtx (tmode);
3960 /* Emit and return the new instruction. */
3961 pat = GEN_FCN (icode) (target, op0);
3962 if (!pat)
3963 return 0;
3964 emit_insn (pat);
3966 return target;
3970 /* Given a builtin function taking 3 operands (i.e., target + two
3971 source), emit the RTL for the underlying instruction. */
3972 static rtx
3973 picochip_expand_builtin_3op (enum insn_code icode, tree call, rtx target)
3975 tree arg0, arg1;
3976 rtx op0, op1, pat;
3977 enum machine_mode tmode, mode0, mode1;
3979 /* Grab the function's arguments. */
3980 arg0 = CALL_EXPR_ARG (call, 0);
3981 arg1 = CALL_EXPR_ARG (call, 1);
3983 /* Emit rtl sequences for the function arguments. */
3984 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3985 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3987 /* Get the mode's of each of the instruction operands. */
3988 tmode = insn_data[icode].operand[0].mode;
3989 mode0 = insn_data[icode].operand[1].mode;
3990 mode1 = insn_data[icode].operand[2].mode;
3992 /* Ensure that each of the function argument rtl sequences are in a
3993 register of the correct mode. */
3994 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
3995 op0 = copy_to_mode_reg (mode0, op0);
3996 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
3997 op1 = copy_to_mode_reg (mode1, op1);
3999 /* If no target has been given, create a register to use as the target. */
4000 if (target == 0
4001 || GET_MODE (target) != tmode
4002 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
4003 target = gen_reg_rtx (tmode);
4005 /* Emit and return the new instruction. */
4006 pat = GEN_FCN (icode) (target, op0, op1);
4007 if (!pat)
4008 return 0;
4009 emit_insn (pat);
4011 return target;
4015 /* Expand a builtin function which takes two arguments, and returns a void. */
4016 static rtx
4017 picochip_expand_builtin_2opvoid (enum insn_code icode, tree call)
4019 tree arg0, arg1;
4020 rtx op0, op1, pat;
4021 enum machine_mode mode0, mode1;
4023 /* Grab the function's arguments. */
4024 arg0 = CALL_EXPR_ARG (call, 0);
4025 arg1 = CALL_EXPR_ARG (call, 1);
4027 /* Emit rtl sequences for the function arguments. */
4028 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4029 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4031 /* Get the mode's of each of the instruction operands. */
4032 mode0 = insn_data[icode].operand[0].mode;
4033 mode1 = insn_data[icode].operand[1].mode;
4035 /* Ensure that each of the function argument rtl sequences are in a
4036 register of the correct mode. */
4037 if (!(*insn_data[icode].operand[0].predicate) (op0, mode0))
4038 op0 = copy_to_mode_reg (mode0, op0);
4039 if (!(*insn_data[icode].operand[1].predicate) (op1, mode1))
4040 op1 = copy_to_mode_reg (mode1, op1);
4042 /* Emit and return the new instruction. */
4043 pat = GEN_FCN (icode) (op0, op1);
4044 if (!pat)
4045 return 0;
4046 emit_insn (pat);
4048 return NULL_RTX;
4052 /* Expand an array get into the corresponding RTL. */
4053 static rtx
4054 picochip_expand_array_get (tree call, rtx target)
4056 tree arg0, arg1, arg2;
4057 rtx op0, op1, op2, pat;
4059 /* Grab the function's arguments. */
4060 arg0 = CALL_EXPR_ARG (call, 0);
4061 arg1 = CALL_EXPR_ARG (call, 1);
4062 arg2 = CALL_EXPR_ARG (call, 2) ;
4064 /* Emit rtl sequences for the function arguments. */
4065 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4066 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4067 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4069 /* The second and third operands must be constant. Nothing else will
4070 do. */
4071 if (CONST_INT != GET_CODE (op1))
4072 internal_error ("%s: Second source operand is not a constant",
4073 __FUNCTION__);
4074 if (CONST_INT != GET_CODE (op2))
4075 internal_error ("%s: Third source operand is not a constant",
4076 __FUNCTION__);
4078 /* If no target has been given, create a register to use as the target. */
4079 if (target == 0 || GET_MODE (target) != SImode)
4080 target = gen_reg_rtx (SImode);
4082 /* The first operand must be a HImode register or a constant. If it
4083 isn't, force it into a HImode register. */
4084 if (GET_MODE (op0) != HImode || REG != GET_CODE (op0))
4085 op0 = copy_to_mode_reg (HImode, op0);
4088 /* Emit and return the new instruction. */
4089 pat = gen_commsArrayGet (target, op0, op1, op2);
4090 emit_insn (pat);
4092 return target;
4096 /* Expand an array put into the corresponding RTL. */
4097 static rtx
4098 picochip_expand_array_put (tree call, rtx target)
4100 tree arg0, arg1, arg2, arg3;
4101 rtx op0, op1, op2, op3, pat;
4103 /* Grab the function's arguments. */
4104 arg0 = CALL_EXPR_ARG (call, 0);
4105 arg1 = CALL_EXPR_ARG (call, 1);
4106 arg2 = CALL_EXPR_ARG (call, 2);
4107 arg3 = CALL_EXPR_ARG (call, 3);
4109 /* Emit rtl sequences for the function arguments. */
4110 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4111 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4112 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4113 op3 = expand_expr (arg3, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4115 /* The first operand must be an SImode register. */
4116 if (GET_MODE (op0) != SImode || REG != GET_CODE (op0))
4117 op0 = copy_to_mode_reg (SImode, op0);
4119 /* The second (index) operand must be a HImode register, or a
4120 constant. If it isn't, force it into a HImode register. */
4121 if (GET_MODE (op1) != HImode || REG != GET_CODE (op1))
4122 op1 = copy_to_mode_reg (HImode, op1);
4124 /* The remaining operands must be constant. Nothing else will do. */
4125 if (CONST_INT != GET_CODE (op2))
4126 internal_error ("%s: Third source operand is not a constant",
4127 __FUNCTION__);
4128 if (CONST_INT != GET_CODE (op3))
4129 internal_error ("%s: Fourth source operand is not a constant",
4130 __FUNCTION__);
4132 /* Emit and return the new instruction. */
4133 pat = gen_commsArrayPut (op0, op1, op2, op3);
4134 emit_insn (pat);
4136 return target;
4140 /* Expand an array testport into the corresponding RTL. */
4141 static rtx
4142 picochip_expand_array_testport (tree call, rtx target)
4144 tree arg0, arg1, arg2;
4145 rtx op0, op1, op2, pat;
4147 /* Grab the function's arguments. */
4148 arg0 = CALL_EXPR_ARG (call, 0);
4149 arg1 = CALL_EXPR_ARG (call, 1);
4150 arg2 = CALL_EXPR_ARG (call, 2);
4152 /* Emit rtl sequences for the function arguments. */
4153 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4154 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4155 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4157 /* The first operand must be a HImode register, or a constant. If it
4158 isn't, force it into a HImode register. */
4159 if (GET_MODE (op0) != HImode || REG != GET_CODE (op0))
4160 op0 = copy_to_mode_reg (HImode, op0);
4162 /* The second and third operands must be constant. Nothing else will
4163 do. */
4164 if (CONST_INT != GET_CODE (op1))
4165 internal_error ("%s: Second source operand is not a constant",
4166 __FUNCTION__);
4167 if (CONST_INT != GET_CODE (op2))
4168 internal_error ("%s: Third source operand is not a constant",
4169 __FUNCTION__);
4171 /* If no target has been given, create a HImode register to use as
4172 the target. */
4173 if (target == 0 || GET_MODE (target) != HImode)
4174 target = gen_reg_rtx (HImode);
4176 /* Emit and return the new instruction. */
4177 pat = gen_commsArrayTestPort (target, op0, op1, op2);
4178 emit_insn (pat);
4180 return target;
4184 /* Generate a unique HALT instruction by giving the instruction a
4185 unique integer. This integer makes no difference to the assembly
4186 output (other than a comment indicating the supplied id), but the
4187 presence of the unique integer prevents the compiler from combining
4188 several different halt instructions into one instruction. This
4189 means that each use of the halt instruction is unique, which in
4190 turn means that assertions work as expected. */
4191 static rtx
4192 picochip_generate_halt (void)
4194 static int currentId = 0;
4195 rtx insns;
4196 rtx id = GEN_INT (currentId);
4197 currentId += 1;
4199 start_sequence();
4200 emit_insn (gen_halt (id));
4202 /* A barrier is inserted to prevent the compiler from thinking that
4203 it has to continue execution after the HALT.*/
4204 emit_barrier ();
4206 insns = get_insns();
4207 end_sequence();
4208 emit_insn (insns);
4210 return const0_rtx;
4213 /* Initialise the builtin functions. Start by initialising
4214 descriptions of different types of functions (e.g., void fn(int),
4215 int fn(void)), and then use these to define the builtins. */
4216 void
4217 picochip_init_builtins (void)
4219 tree noreturn;
4220 tree endlink = void_list_node;
4221 tree int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4222 tree unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
4223 tree long_endlink = tree_cons (NULL_TREE, long_integer_type_node, endlink);
4224 tree int_int_endlink =
4225 tree_cons (NULL_TREE, integer_type_node, int_endlink);
4226 tree int_int_int_endlink =
4227 tree_cons (NULL_TREE, integer_type_node, int_int_endlink);
4228 tree int_long_endlink =
4229 tree_cons (NULL_TREE, integer_type_node, long_endlink);
4230 tree long_int_int_int_endlink =
4231 tree_cons (NULL_TREE, long_integer_type_node, int_int_int_endlink);
4233 tree int_ftype_int, int_ftype_int_int;
4234 tree long_ftype_int, long_ftype_int_int_int;
4235 tree void_ftype_int_long, int_ftype_int_int_int,
4236 void_ftype_long_int_int_int;
4237 tree void_ftype_void, unsigned_ftype_unsigned;
4239 /* void func (void) */
4240 void_ftype_void = build_function_type (void_type_node, endlink);
4242 /* int func (int) */
4243 int_ftype_int = build_function_type (integer_type_node, int_endlink);
4245 /* unsigned int func (unsigned int) */
4246 unsigned_ftype_unsigned = build_function_type (unsigned_type_node, unsigned_endlink);
4248 /* int func(int, int) */
4249 int_ftype_int_int
4250 = build_function_type (integer_type_node, int_int_endlink);
4252 /* long func(int) */
4253 long_ftype_int = build_function_type (long_integer_type_node, int_endlink);
4255 /* long func(int, int, int) */
4256 long_ftype_int_int_int
4257 = build_function_type (long_integer_type_node, int_int_int_endlink);
4259 /* int func(int, int, int) */
4260 int_ftype_int_int_int
4261 = build_function_type (integer_type_node, int_int_int_endlink);
4263 /* void func(int, long) */
4264 void_ftype_int_long
4265 = build_function_type (void_type_node, int_long_endlink);
4267 /* void func(long, int, int, int) */
4268 void_ftype_long_int_int_int
4269 = build_function_type (void_type_node, long_int_int_int_endlink);
4271 /* Initialise the sign-bit-count function. */
4272 add_builtin_function ("__builtin_sbc", int_ftype_int,
4273 PICOCHIP_BUILTIN_SBC, BUILT_IN_MD, NULL,
4274 NULL_TREE);
4275 add_builtin_function ("picoSbc", int_ftype_int, PICOCHIP_BUILTIN_SBC,
4276 BUILT_IN_MD, NULL, NULL_TREE);
4278 /* Initialise the bit reverse function. */
4279 add_builtin_function ("__builtin_brev", unsigned_ftype_unsigned,
4280 PICOCHIP_BUILTIN_BREV, BUILT_IN_MD, NULL,
4281 NULL_TREE);
4282 add_builtin_function ("picoBrev", unsigned_ftype_unsigned,
4283 PICOCHIP_BUILTIN_BREV, BUILT_IN_MD, NULL,
4284 NULL_TREE);
4286 /* Initialise the byte swap function. */
4287 add_builtin_function ("__builtin_byteswap", unsigned_ftype_unsigned,
4288 PICOCHIP_BUILTIN_BYTESWAP, BUILT_IN_MD, NULL,
4289 NULL_TREE);
4290 add_builtin_function ("picoByteSwap", unsigned_ftype_unsigned,
4291 PICOCHIP_BUILTIN_BYTESWAP, BUILT_IN_MD, NULL,
4292 NULL_TREE);
4294 /* Initialise the ASRI function (note that while this can be coded
4295 using a signed shift in C, extra scratch registers are required,
4296 which we avoid by having a direct builtin to map to the
4297 instruction). */
4298 add_builtin_function ("__builtin_asri", int_ftype_int_int,
4299 PICOCHIP_BUILTIN_ASRI, BUILT_IN_MD, NULL,
4300 NULL_TREE);
4302 /* Initialise saturating addition. */
4303 add_builtin_function ("__builtin_adds", int_ftype_int_int,
4304 PICOCHIP_BUILTIN_ADDS, BUILT_IN_MD, NULL,
4305 NULL_TREE);
4306 add_builtin_function ("picoAdds", int_ftype_int_int,
4307 PICOCHIP_BUILTIN_ADDS, BUILT_IN_MD, NULL,
4308 NULL_TREE);
4310 /* Initialise saturating subtraction. */
4311 add_builtin_function ("__builtin_subs", int_ftype_int_int,
4312 PICOCHIP_BUILTIN_SUBS, BUILT_IN_MD, NULL,
4313 NULL_TREE);
4314 add_builtin_function ("picoSubs", int_ftype_int_int,
4315 PICOCHIP_BUILTIN_SUBS, BUILT_IN_MD, NULL,
4316 NULL_TREE);
4318 /* Scalar comms builtins. */
4319 add_builtin_function ("__builtin_get", long_ftype_int,
4320 PICOCHIP_BUILTIN_GET, BUILT_IN_MD, NULL,
4321 NULL_TREE);
4322 add_builtin_function ("__builtin_put", void_ftype_int_long,
4323 PICOCHIP_BUILTIN_PUT, BUILT_IN_MD, NULL,
4324 NULL_TREE);
4325 add_builtin_function ("__builtin_testport", int_ftype_int,
4326 PICOCHIP_BUILTIN_TESTPORT, BUILT_IN_MD, NULL,
4327 NULL_TREE);
4329 /* Array comms builtins. */
4330 add_builtin_function ("__builtin_put_array",
4331 void_ftype_long_int_int_int,
4332 PICOCHIP_BUILTIN_PUT_ARRAY, BUILT_IN_MD, NULL,
4333 NULL_TREE);
4334 add_builtin_function ("__builtin_get_array", long_ftype_int_int_int,
4335 PICOCHIP_BUILTIN_GET_ARRAY, BUILT_IN_MD, NULL,
4336 NULL_TREE);
4337 add_builtin_function ("__builtin_testport_array",
4338 int_ftype_int_int_int,
4339 PICOCHIP_BUILTIN_TESTPORT_ARRAY, BUILT_IN_MD,
4340 NULL, NULL_TREE);
4342 /* Halt instruction. Note that the builtin function is marked as
4343 having the attribute `noreturn' so that the compiler realises
4344 that the halt stops the program dead. */
4345 noreturn = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4346 add_builtin_function ("__builtin_halt", void_ftype_void,
4347 PICOCHIP_BUILTIN_HALT, BUILT_IN_MD, NULL,
4348 noreturn);
4349 add_builtin_function ("picoHalt", void_ftype_void,
4350 PICOCHIP_BUILTIN_HALT, BUILT_IN_MD, NULL,
4351 noreturn);
4355 /* Expand a call to a builtin function. */
4357 picochip_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
4358 enum machine_mode mode ATTRIBUTE_UNUSED,
4359 int ignore ATTRIBUTE_UNUSED)
4361 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4362 int fcode = DECL_FUNCTION_CODE (fndecl);
4364 switch (fcode)
4366 case PICOCHIP_BUILTIN_ASRI:
4367 return picochip_expand_builtin_3op (CODE_FOR_builtin_asri, exp,
4368 target);
4370 case PICOCHIP_BUILTIN_ADDS:
4371 return picochip_expand_builtin_3op (CODE_FOR_sataddhi3, exp,
4372 target);
4374 case PICOCHIP_BUILTIN_SUBS:
4375 return picochip_expand_builtin_3op (CODE_FOR_satsubhi3, exp,
4376 target);
4378 case PICOCHIP_BUILTIN_SBC:
4379 return picochip_expand_builtin_2op (CODE_FOR_sbc, exp, target);
4381 case PICOCHIP_BUILTIN_BREV:
4382 return picochip_expand_builtin_2op (CODE_FOR_brev, exp, target);
4384 case PICOCHIP_BUILTIN_BYTESWAP:
4385 return picochip_expand_builtin_2op (CODE_FOR_bswaphi2, exp, target);
4387 case PICOCHIP_BUILTIN_GET:
4388 return picochip_expand_builtin_2op (CODE_FOR_commsGet, exp, target);
4390 case PICOCHIP_BUILTIN_PUT:
4391 return picochip_expand_builtin_2opvoid (CODE_FOR_commsPut, exp);
4393 case PICOCHIP_BUILTIN_TESTPORT:
4394 return picochip_expand_builtin_2op (CODE_FOR_commsTestPort, exp,
4395 target);
4397 case PICOCHIP_BUILTIN_PUT_ARRAY:
4398 return picochip_expand_array_put (exp, target);
4400 case PICOCHIP_BUILTIN_GET_ARRAY:
4401 return picochip_expand_array_get (exp, target);
4403 case PICOCHIP_BUILTIN_TESTPORT_ARRAY:
4404 return picochip_expand_array_testport (exp, target);
4406 case PICOCHIP_BUILTIN_HALT:
4407 return picochip_generate_halt ();
4409 default:
4410 gcc_unreachable();
4414 /* Should really do something sensible here. */
4415 return NULL_RTX;
4418 /* Emit warnings. */
4419 static void
4420 picochip_warn_inefficient (const char *msg)
4422 if (TARGET_INEFFICIENT_WARNINGS)
4423 warning (OPT_minefficient_warnings,
4424 "%s (disable warning using -mno-inefficient-warnings)", msg);
4427 void
4428 warn_of_byte_access (void)
4430 static int warned = 0;
4432 if (!warned)
4434 picochip_warn_inefficient
4435 ("byte access is synthesised - consider using MUL AE");
4436 warned = 1;
4442 picochip_function_value (const_tree valtype, const_tree func,
4443 bool outgoing ATTRIBUTE_UNUSED)
4445 enum machine_mode mode = TYPE_MODE (valtype);
4446 int unsignedp = TYPE_UNSIGNED (valtype);
4448 /* Since we define PROMOTE_FUNCTION_RETURN, we must promote the mode
4449 just as PROMOTE_MODE does. */
4450 mode = promote_function_mode (valtype, mode, &unsignedp, func, 1);
4452 return gen_rtx_REG (mode, 0);
4456 /* Check that the value of the given mode will fit in the register of
4457 the given mode. */
4459 picochip_hard_regno_mode_ok (int regno, enum machine_mode mode)
4462 if (GET_MODE_CLASS (mode) == MODE_CC)
4463 return regno == CC_REGNUM;
4465 /* If the CC register is being used, then only CC mode values are
4466 allowed (which have already been tested). */
4467 if (regno == CC_REGNUM || regno == ACC_REGNUM)
4468 return 0;
4470 /* Must be a valid register. */
4471 if (regno > 16)
4472 return 0;
4474 /* Modes QI and HI may be placed in any register except the CC. */
4475 if (mode == QImode || mode == HImode)
4476 return 1;
4478 /* DI must be in a quad register. */
4479 if (mode == DImode)
4480 return (regno % 4 == 0);
4482 /* All other modes must be placed in a even numbered register. */
4483 return !(regno & 1);
4487 /* Extract the lower and upper components of a constant value. */
4490 picochip_get_low_const (rtx value)
4492 return gen_int_mode (INTVAL (value) & 0xFFFF, HImode);
4496 picochip_get_high_const (rtx value)
4498 /*return GEN_INT ((((INTVAL (value) >> 16) & 0xFFFF) ^ 0x8000) - 0x8000); */
4499 return gen_int_mode ((INTVAL (value) >> 16) & 0xFFFF, HImode);
4503 /* Loading and storing QImode values to and from memory in a machine
4504 without byte access requires might require a scratch
4505 register. However, the scratch register might correspond to the
4506 register in which the value is being loaded. To ensure that a
4507 scratch register is supplied which is definitely different to the
4508 output register, request a register pair. This effectively gives a
4509 choice of two registers to choose from, so that we a guaranteed to
4510 get at least one register which is different to the output
4511 register. This trick is taken from the alpha implementation. */
4512 static reg_class_t
4513 picochip_secondary_reload (bool in_p,
4514 rtx x ATTRIBUTE_UNUSED,
4515 reg_class_t cla ATTRIBUTE_UNUSED,
4516 enum machine_mode mode,
4517 secondary_reload_info *sri)
4519 if (mode == QImode && !TARGET_HAS_BYTE_ACCESS)
4521 if (in_p == 0)
4522 sri->icode = CODE_FOR_reload_outqi;
4523 else
4524 sri->icode = CODE_FOR_reload_inqi;
4527 /* We dont need to return a register class type when we need only a
4528 scratch register. It realizes the scratch register type by looking
4529 at the instruction definition for sri->icode. We only need to
4530 return the register type when we need intermediaries for copies.*/
4531 return NO_REGS;
4534 /* Return true if the given memory operand can be aligned to a
4535 word+offset memory reference (e.g., FP+3 can be converted into the
4536 memory operand FP+2, with the offset 1). */
4538 picochip_alignable_memory_operand (rtx mem_operand,
4539 enum machine_mode mode ATTRIBUTE_UNUSED)
4541 rtx address;
4543 /* Not a mem operand. Refuse immediately. */
4544 if (MEM != GET_CODE (mem_operand))
4545 return 0;
4547 address = XEXP (mem_operand, 0);
4549 /* Return true if a PLUS of the SP and a (valid) constant, or SP itself. */
4550 return ((PLUS == GET_CODE (address) &&
4551 REGNO (XEXP (address, 0)) == STACK_POINTER_REGNUM &&
4552 CONST_INT == GET_CODE (XEXP (address, 1)) &&
4553 picochip_const_ok_for_letter_p (INTVAL (XEXP (address, 1)), 'K'))
4554 || (REG == GET_CODE (address)
4555 && REGNO (address) == STACK_POINTER_REGNUM));
4559 /* Return true if the given memory reference is to a word aligned
4560 address. Currently this means it must be either SP, or
4561 SP+offset. We could replace this function with alignable
4562 memory references in the above function?. */
4564 picochip_word_aligned_memory_reference (rtx operand)
4568 /* The address must be the SP register, or a constant, aligned
4569 offset from SP which doesn't exceed the FP+offset
4570 restrictions. */
4571 return ((PLUS == GET_CODE (operand)
4572 && REGNO (XEXP (operand, 0)) == STACK_POINTER_REGNUM
4573 && picochip_is_aligned (INTVAL (XEXP (operand, 1)), 16)
4574 && picochip_const_ok_for_letter_p (INTVAL (XEXP (operand, 1)),
4575 'K'))
4576 || (REG == GET_CODE (operand)
4577 && REGNO (operand) == STACK_POINTER_REGNUM));
4581 /* Given an alignable memory location, convert the memory location
4582 into a HI mode access, storing the new memory reference in
4583 paligned_mem, and the number of bits by which to shift in pbitnum
4584 (i.e., given a reference to FP+3, this creates an aligned reference
4585 of FP+2, with an 8-bit shift). This code is a modification of that
4586 found in the Alpha port. */
4587 void
4588 picochip_get_hi_aligned_mem (rtx ref, rtx * paligned_mem, rtx * pbitnum)
4590 rtx base;
4591 HOST_WIDE_INT offset = 0;
4593 gcc_assert (GET_CODE (ref) == MEM);
4595 if (reload_in_progress && !memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
4597 base = find_replacement (&XEXP (ref, 0));
4599 gcc_assert(memory_address_p (GET_MODE (ref), base));
4601 else
4603 base = XEXP (ref, 0);
4606 if (GET_CODE (base) == PLUS)
4608 offset += INTVAL (XEXP (base, 1));
4609 base = XEXP (base, 0);
4612 *paligned_mem = widen_memory_access (ref, HImode, (offset & ~1) - offset);
4614 if (offset > 0)
4616 if (TARGET_DEBUG)
4618 printf
4619 ("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");
4623 *pbitnum = GEN_INT ((offset & 1) * 8);
4627 /* Return true if the given operand is an absolute address in memory
4628 (i.e., a symbolic offset). */
4630 picochip_absolute_memory_operand (rtx op,
4631 enum machine_mode mode ATTRIBUTE_UNUSED)
4634 if (MEM == GET_CODE (op))
4636 rtx address = XEXP (op, 0);
4638 /* Symbols are valid absolute addresses. */
4639 if (SYMBOL_REF == GET_CODE (address))
4640 return 1;
4642 /* Constant offsets to symbols are valid absolute addresses. */
4643 if (CONST == GET_CODE (address) &&
4644 PLUS == GET_CODE (XEXP (address, 0)) &&
4645 SYMBOL_REF == GET_CODE (XEXP (XEXP (address, 0), 0)) &&
4646 CONST_INT == GET_CODE (XEXP (XEXP (address, 0), 1)))
4647 return 1;
4650 else
4651 return 0;
4653 /* Symbols are valid absolute addresses. */
4654 if (SYMBOL_REF == GET_CODE (XEXP (op, 0)))
4655 return 1;
4658 return 0;
4662 void
4663 picochip_asm_named_section (const char *name,
4664 unsigned int flags ATTRIBUTE_UNUSED,
4665 tree decl ATTRIBUTE_UNUSED)
4667 fprintf (asm_out_file, ".section %s\n", name);
4671 /* Check if we can make a conditional copy instruction. This is emitted as an
4672 instruction to set the condition register, followed by an instruction which
4673 uses the condition registers to perform the conditional move. */
4675 picochip_check_conditional_copy (rtx * operands)
4678 rtx branch_op_0 = XEXP (operands[1], 0);
4679 rtx branch_op_1 = XEXP (operands[1], 1);
4681 /* Only HI mode conditional moves are currently allowed. Can we add
4682 SI mode moves? */
4683 if (GET_CODE (operands[1]) != EQ && GET_CODE (operands[1]) != NE)
4684 return 0;
4686 /* Is the comparison valid? Only allow operands which are registers
4687 if they are HImode. SI mode comparisons against 0 could be
4688 handled using logical operations (e.g., SIreg != 0 when low ||
4689 high). Need to find test cases to provoke this though (fixunssfdi
4690 in libgcc does, but is complicated). */
4691 if (register_operand(branch_op_0, GET_MODE(branch_op_0)) &&
4692 GET_MODE(branch_op_0) != HImode)
4693 return 0;
4694 if (register_operand(branch_op_1, GET_MODE(branch_op_1)) &&
4695 GET_MODE(branch_op_1) != HImode)
4696 return 0;
4698 return 1;
4703 static rtx
4704 picochip_static_chain (const_tree ARG_UNUSED (fndecl), bool incoming_p)
4706 rtx addr;
4707 if (incoming_p)
4708 addr = arg_pointer_rtx;
4709 else
4710 addr = plus_constant (stack_pointer_rtx, -2 * UNITS_PER_WORD);
4711 return gen_frame_mem (Pmode, addr);